blob: 8ecda30bcee697b7b8c4e9d68f4912f93cd09d42 [file] [log] [blame]
Tom Sepezd831dc72015-10-19 16:04:22 -07001// Copyright 2015 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "test_support.h"
6
7#include <stdio.h>
8#include <string.h>
9
10#ifdef _WIN32
11#define PATH_SEPARATOR '\\'
12#else
13#define PATH_SEPARATOR '/'
14#endif
15
Lei Zhang8241df72015-11-06 14:38:48 -080016#ifdef PDF_ENABLE_V8
17#include "v8/include/libplatform/libplatform.h"
18#endif
19
Tom Sepezd831dc72015-10-19 16:04:22 -070020namespace {
21
22#ifdef PDF_ENABLE_V8
23#ifdef V8_USE_EXTERNAL_STARTUP_DATA
24// Returns the full path for an external V8 data file based on either
25// the currect exectuable path or an explicit override.
26std::string GetFullPathForSnapshotFile(const std::string& exe_path,
27 const std::string& bin_dir,
28 const std::string& filename) {
29 std::string result;
30 if (!bin_dir.empty()) {
31 result = bin_dir;
32 if (*bin_dir.rbegin() != PATH_SEPARATOR) {
33 result += PATH_SEPARATOR;
34 }
35 } else if (!exe_path.empty()) {
36 size_t last_separator = exe_path.rfind(PATH_SEPARATOR);
37 if (last_separator != std::string::npos) {
38 result = exe_path.substr(0, last_separator + 1);
39 }
40 }
41 result += filename;
42 return result;
43}
44
45bool GetExternalData(const std::string& exe_path,
46 const std::string& bin_dir,
47 const std::string& filename,
48 v8::StartupData* result_data) {
49 std::string full_path =
50 GetFullPathForSnapshotFile(exe_path, bin_dir, filename);
51 size_t data_length = 0;
52 char* data_buffer = GetFileContents(full_path.c_str(), &data_length);
53 if (!data_buffer) {
54 return false;
55 }
56 result_data->data = const_cast<const char*>(data_buffer);
57 result_data->raw_size = data_length;
58 return true;
59}
60#endif // V8_USE_EXTERNAL_STARTUP_DATA
61
62void InitializeV8Common(v8::Platform** platform) {
63 v8::V8::InitializeICU();
64
65 *platform = v8::platform::CreateDefaultPlatform();
66 v8::V8::InitializePlatform(*platform);
67 v8::V8::Initialize();
68
69 // By enabling predictable mode, V8 won't post any background tasks.
70 const char predictable_flag[] = "--predictable";
71 v8::V8::SetFlagsFromString(predictable_flag,
72 static_cast<int>(strlen(predictable_flag)));
73}
74#endif // PDF_ENABLE_V8
75
76} // namespace
77
78char* GetFileContents(const char* filename, size_t* retlen) {
79 FILE* file = fopen(filename, "rb");
80 if (!file) {
81 fprintf(stderr, "Failed to open: %s\n", filename);
82 return nullptr;
83 }
84 (void)fseek(file, 0, SEEK_END);
85 size_t file_length = ftell(file);
86 if (!file_length) {
87 return nullptr;
88 }
89 (void)fseek(file, 0, SEEK_SET);
90 char* buffer = (char*)malloc(file_length);
91 if (!buffer) {
92 return nullptr;
93 }
94 size_t bytes_read = fread(buffer, 1, file_length, file);
95 (void)fclose(file);
96 if (bytes_read != file_length) {
97 fprintf(stderr, "Failed to read: %s\n", filename);
98 free(buffer);
99 return nullptr;
100 }
101 *retlen = bytes_read;
102 return buffer;
103}
104
Lei Zhang79e893a2015-11-04 16:02:47 -0800105std::wstring GetWideString(FPDF_WIDESTRING wstr) {
106 if (!wstr)
107 return nullptr;
108
109 size_t characters = 0;
110 while (wstr[characters])
111 ++characters;
112
113 std::wstring platform_string(characters, L'\0');
114 for (size_t i = 0; i < characters + 1; ++i) {
115 const unsigned char* ptr = reinterpret_cast<const unsigned char*>(&wstr[i]);
116 platform_string[i] = ptr[0] + 256 * ptr[1];
117 }
118 return platform_string;
119}
120
Tom Sepezd831dc72015-10-19 16:04:22 -0700121#ifdef PDF_ENABLE_V8
122#ifdef V8_USE_EXTERNAL_STARTUP_DATA
123bool InitializeV8ForPDFium(const std::string& exe_path,
124 const std::string& bin_dir,
125 v8::StartupData* natives_blob,
126 v8::StartupData* snapshot_blob,
127 v8::Platform** platform) {
128 InitializeV8Common(platform);
129 if (!GetExternalData(exe_path, bin_dir, "natives_blob.bin", natives_blob))
130 return false;
131 if (!GetExternalData(exe_path, bin_dir, "snapshot_blob.bin", snapshot_blob))
132 return false;
133 v8::V8::SetNativesDataBlob(natives_blob);
134 v8::V8::SetSnapshotDataBlob(snapshot_blob);
135 return true;
136}
137#else // V8_USE_EXTERNAL_STARTUP_DATA
138bool InitializeV8ForPDFium(v8::Platform** platform) {
139 InitializeV8Common(platform);
140 return true;
141}
142#endif // V8_USE_EXTERNAL_STARTUP_DATA
143#endif // PDF_ENABLE_V8
144
145TestLoader::TestLoader(const char* pBuf, size_t len)
146 : m_pBuf(pBuf), m_Len(len) {
147}
148
149// static
150int TestLoader::GetBlock(void* param,
151 unsigned long pos,
152 unsigned char* pBuf,
153 unsigned long size) {
154 TestLoader* pLoader = static_cast<TestLoader*>(param);
155 if (pos + size < pos || pos + size > pLoader->m_Len)
156 return 0;
157
158 memcpy(pBuf, pLoader->m_pBuf + pos, size);
159 return 1;
160}