blob: ce143ae8e9b75153f00c072fde2616eeec296445 [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
Wei Li091f7a02015-11-09 12:09:55 -08005#include "testing/test_support.h"
Tom Sepezd831dc72015-10-19 16:04:22 -07006
7#include <stdio.h>
8#include <string.h>
9
Wei Li091f7a02015-11-09 12:09:55 -080010#include "testing/utils/path_service.h"
Tom Sepezd831dc72015-10-19 16:04:22 -070011
Lei Zhang8241df72015-11-06 14:38:48 -080012#ifdef PDF_ENABLE_V8
13#include "v8/include/libplatform/libplatform.h"
14#endif
15
Tom Sepezd831dc72015-10-19 16:04:22 -070016namespace {
17
18#ifdef PDF_ENABLE_V8
19#ifdef V8_USE_EXTERNAL_STARTUP_DATA
20// Returns the full path for an external V8 data file based on either
21// the currect exectuable path or an explicit override.
22std::string GetFullPathForSnapshotFile(const std::string& exe_path,
23 const std::string& bin_dir,
24 const std::string& filename) {
25 std::string result;
26 if (!bin_dir.empty()) {
27 result = bin_dir;
28 if (*bin_dir.rbegin() != PATH_SEPARATOR) {
29 result += PATH_SEPARATOR;
30 }
31 } else if (!exe_path.empty()) {
32 size_t last_separator = exe_path.rfind(PATH_SEPARATOR);
33 if (last_separator != std::string::npos) {
34 result = exe_path.substr(0, last_separator + 1);
35 }
36 }
37 result += filename;
38 return result;
39}
40
41bool GetExternalData(const std::string& exe_path,
42 const std::string& bin_dir,
43 const std::string& filename,
44 v8::StartupData* result_data) {
45 std::string full_path =
46 GetFullPathForSnapshotFile(exe_path, bin_dir, filename);
47 size_t data_length = 0;
48 char* data_buffer = GetFileContents(full_path.c_str(), &data_length);
49 if (!data_buffer) {
50 return false;
51 }
52 result_data->data = const_cast<const char*>(data_buffer);
53 result_data->raw_size = data_length;
54 return true;
55}
56#endif // V8_USE_EXTERNAL_STARTUP_DATA
57
58void InitializeV8Common(v8::Platform** platform) {
59 v8::V8::InitializeICU();
60
61 *platform = v8::platform::CreateDefaultPlatform();
62 v8::V8::InitializePlatform(*platform);
63 v8::V8::Initialize();
64
65 // By enabling predictable mode, V8 won't post any background tasks.
66 const char predictable_flag[] = "--predictable";
67 v8::V8::SetFlagsFromString(predictable_flag,
68 static_cast<int>(strlen(predictable_flag)));
69}
70#endif // PDF_ENABLE_V8
71
72} // namespace
73
Tom Sepez0aa35312016-01-06 10:16:32 -080074std::unique_ptr<char, pdfium::FreeDeleter> GetFileContents(const char* filename,
75 size_t* retlen) {
Tom Sepezd831dc72015-10-19 16:04:22 -070076 FILE* file = fopen(filename, "rb");
77 if (!file) {
78 fprintf(stderr, "Failed to open: %s\n", filename);
79 return nullptr;
80 }
81 (void)fseek(file, 0, SEEK_END);
82 size_t file_length = ftell(file);
83 if (!file_length) {
84 return nullptr;
85 }
86 (void)fseek(file, 0, SEEK_SET);
Tom Sepez0aa35312016-01-06 10:16:32 -080087 std::unique_ptr<char, pdfium::FreeDeleter> buffer(
88 static_cast<char*>(malloc(file_length)));
Tom Sepezd831dc72015-10-19 16:04:22 -070089 if (!buffer) {
90 return nullptr;
91 }
Tom Sepez0aa35312016-01-06 10:16:32 -080092 size_t bytes_read = fread(buffer.get(), 1, file_length, file);
Tom Sepezd831dc72015-10-19 16:04:22 -070093 (void)fclose(file);
94 if (bytes_read != file_length) {
95 fprintf(stderr, "Failed to read: %s\n", filename);
Tom Sepezd831dc72015-10-19 16:04:22 -070096 return nullptr;
97 }
98 *retlen = bytes_read;
99 return buffer;
100}
101
Tom Sepez8ab45ea2016-01-05 10:17:30 -0800102std::wstring GetPlatformWString(FPDF_WIDESTRING wstr) {
Lei Zhang79e893a2015-11-04 16:02:47 -0800103 if (!wstr)
104 return nullptr;
105
106 size_t characters = 0;
107 while (wstr[characters])
108 ++characters;
109
110 std::wstring platform_string(characters, L'\0');
111 for (size_t i = 0; i < characters + 1; ++i) {
112 const unsigned char* ptr = reinterpret_cast<const unsigned char*>(&wstr[i]);
113 platform_string[i] = ptr[0] + 256 * ptr[1];
114 }
115 return platform_string;
116}
117
Tom Sepez0aa35312016-01-06 10:16:32 -0800118std::unique_ptr<unsigned short, pdfium::FreeDeleter> GetFPDFWideString(
119 const std::wstring& wstr) {
Tom Sepez8ab45ea2016-01-05 10:17:30 -0800120 size_t length = sizeof(uint16_t) * (wstr.length() + 1);
Tom Sepez0aa35312016-01-06 10:16:32 -0800121 std::unique_ptr<unsigned short, pdfium::FreeDeleter> result(
122 static_cast<unsigned short*>(malloc(length)));
123 char* ptr = reinterpret_cast<char*>(result.get());
Tom Sepez8ab45ea2016-01-05 10:17:30 -0800124 size_t i = 0;
125 for (wchar_t w : wstr) {
126 ptr[i++] = w & 0xff;
127 ptr[i++] = (w >> 8) & 0xff;
128 }
129 ptr[i++] = 0;
130 ptr[i] = 0;
Tom Sepez0aa35312016-01-06 10:16:32 -0800131 return result;
Tom Sepez8ab45ea2016-01-05 10:17:30 -0800132}
133
Tom Sepezd831dc72015-10-19 16:04:22 -0700134#ifdef PDF_ENABLE_V8
135#ifdef V8_USE_EXTERNAL_STARTUP_DATA
136bool InitializeV8ForPDFium(const std::string& exe_path,
137 const std::string& bin_dir,
138 v8::StartupData* natives_blob,
139 v8::StartupData* snapshot_blob,
140 v8::Platform** platform) {
141 InitializeV8Common(platform);
142 if (!GetExternalData(exe_path, bin_dir, "natives_blob.bin", natives_blob))
143 return false;
144 if (!GetExternalData(exe_path, bin_dir, "snapshot_blob.bin", snapshot_blob))
145 return false;
146 v8::V8::SetNativesDataBlob(natives_blob);
147 v8::V8::SetSnapshotDataBlob(snapshot_blob);
148 return true;
149}
150#else // V8_USE_EXTERNAL_STARTUP_DATA
151bool InitializeV8ForPDFium(v8::Platform** platform) {
152 InitializeV8Common(platform);
153 return true;
154}
155#endif // V8_USE_EXTERNAL_STARTUP_DATA
156#endif // PDF_ENABLE_V8
157
158TestLoader::TestLoader(const char* pBuf, size_t len)
159 : m_pBuf(pBuf), m_Len(len) {
160}
161
162// static
163int TestLoader::GetBlock(void* param,
164 unsigned long pos,
165 unsigned char* pBuf,
166 unsigned long size) {
167 TestLoader* pLoader = static_cast<TestLoader*>(param);
168 if (pos + size < pos || pos + size > pLoader->m_Len)
169 return 0;
170
171 memcpy(pBuf, pLoader->m_pBuf + pos, size);
172 return 1;
173}