blob: 608e4ae75bcd5b131b2788c0783243f3f6cab0dd [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
Dan Sinclair3ebd1212016-03-09 09:59:23 -050010#include <string>
11
Chris Palmer2b797292017-04-06 14:45:39 -070012#include "core/fxcrt/fx_memory.h"
Wei Li091f7a02015-11-09 12:09:55 -080013#include "testing/utils/path_service.h"
Tom Sepezd831dc72015-10-19 16:04:22 -070014
Lei Zhang8241df72015-11-06 14:38:48 -080015#ifdef PDF_ENABLE_V8
16#include "v8/include/libplatform/libplatform.h"
17#endif
18
Tom Sepezd831dc72015-10-19 16:04:22 -070019namespace {
20
21#ifdef PDF_ENABLE_V8
22#ifdef V8_USE_EXTERNAL_STARTUP_DATA
23// Returns the full path for an external V8 data file based on either
24// the currect exectuable path or an explicit override.
25std::string GetFullPathForSnapshotFile(const std::string& exe_path,
26 const std::string& bin_dir,
27 const std::string& filename) {
28 std::string result;
29 if (!bin_dir.empty()) {
30 result = bin_dir;
31 if (*bin_dir.rbegin() != PATH_SEPARATOR) {
32 result += PATH_SEPARATOR;
33 }
34 } else if (!exe_path.empty()) {
35 size_t last_separator = exe_path.rfind(PATH_SEPARATOR);
36 if (last_separator != std::string::npos) {
37 result = exe_path.substr(0, last_separator + 1);
38 }
39 }
40 result += filename;
41 return result;
42}
43
44bool GetExternalData(const std::string& exe_path,
45 const std::string& bin_dir,
46 const std::string& filename,
47 v8::StartupData* result_data) {
48 std::string full_path =
49 GetFullPathForSnapshotFile(exe_path, bin_dir, filename);
50 size_t data_length = 0;
Lei Zhangbcc03a42016-01-07 00:48:00 -080051 std::unique_ptr<char, pdfium::FreeDeleter> data_buffer =
52 GetFileContents(full_path.c_str(), &data_length);
53 if (!data_buffer)
Tom Sepezd831dc72015-10-19 16:04:22 -070054 return false;
Lei Zhangbcc03a42016-01-07 00:48:00 -080055
56 result_data->data = data_buffer.release();
Tom Sepezd831dc72015-10-19 16:04:22 -070057 result_data->raw_size = data_length;
58 return true;
59}
60#endif // V8_USE_EXTERNAL_STARTUP_DATA
61
jochen9e077d22016-06-09 02:51:13 -070062void InitializeV8Common(const char* exe_path, v8::Platform** platform) {
63 v8::V8::InitializeICUDefaultLocation(exe_path);
Tom Sepezd831dc72015-10-19 16:04:22 -070064
65 *platform = v8::platform::CreateDefaultPlatform();
66 v8::V8::InitializePlatform(*platform);
Tom Sepezd831dc72015-10-19 16:04:22 -070067
68 // By enabling predictable mode, V8 won't post any background tasks.
tsepez98822432017-01-11 05:53:10 -080069 // By enabling GC, it makes it easier to chase use-after-free.
70 const char v8_flags[] = "--predictable --expose-gc";
71 v8::V8::SetFlagsFromString(v8_flags, static_cast<int>(strlen(v8_flags)));
72 v8::V8::Initialize();
Tom Sepezd831dc72015-10-19 16:04:22 -070073}
74#endif // PDF_ENABLE_V8
75
76} // namespace
77
Tom Sepez0aa35312016-01-06 10:16:32 -080078std::unique_ptr<char, pdfium::FreeDeleter> GetFileContents(const char* filename,
79 size_t* retlen) {
Tom Sepezd831dc72015-10-19 16:04:22 -070080 FILE* file = fopen(filename, "rb");
81 if (!file) {
82 fprintf(stderr, "Failed to open: %s\n", filename);
83 return nullptr;
84 }
85 (void)fseek(file, 0, SEEK_END);
86 size_t file_length = ftell(file);
87 if (!file_length) {
88 return nullptr;
89 }
90 (void)fseek(file, 0, SEEK_SET);
Tom Sepez0aa35312016-01-06 10:16:32 -080091 std::unique_ptr<char, pdfium::FreeDeleter> buffer(
92 static_cast<char*>(malloc(file_length)));
Tom Sepezd831dc72015-10-19 16:04:22 -070093 if (!buffer) {
94 return nullptr;
95 }
Tom Sepez0aa35312016-01-06 10:16:32 -080096 size_t bytes_read = fread(buffer.get(), 1, file_length, file);
Tom Sepezd831dc72015-10-19 16:04:22 -070097 (void)fclose(file);
98 if (bytes_read != file_length) {
99 fprintf(stderr, "Failed to read: %s\n", filename);
Tom Sepezd831dc72015-10-19 16:04:22 -0700100 return nullptr;
101 }
102 *retlen = bytes_read;
103 return buffer;
104}
105
Tom Sepez8ab45ea2016-01-05 10:17:30 -0800106std::wstring GetPlatformWString(FPDF_WIDESTRING wstr) {
Lei Zhang79e893a2015-11-04 16:02:47 -0800107 if (!wstr)
108 return nullptr;
109
110 size_t characters = 0;
111 while (wstr[characters])
112 ++characters;
113
114 std::wstring platform_string(characters, L'\0');
115 for (size_t i = 0; i < characters + 1; ++i) {
116 const unsigned char* ptr = reinterpret_cast<const unsigned char*>(&wstr[i]);
117 platform_string[i] = ptr[0] + 256 * ptr[1];
118 }
119 return platform_string;
120}
121
tsepezf09bdfa2016-04-18 16:08:26 -0700122std::vector<std::string> StringSplit(const std::string& str, char delimiter) {
123 std::vector<std::string> result;
124 size_t pos = 0;
125 while (1) {
126 size_t found = str.find(delimiter, pos);
127 if (found == std::string::npos)
128 break;
129
130 result.push_back(str.substr(pos, found - pos));
131 pos = found + 1;
132 }
133 result.push_back(str.substr(pos));
134 return result;
135}
136
Tom Sepez0aa35312016-01-06 10:16:32 -0800137std::unique_ptr<unsigned short, pdfium::FreeDeleter> GetFPDFWideString(
138 const std::wstring& wstr) {
Tom Sepez8ab45ea2016-01-05 10:17:30 -0800139 size_t length = sizeof(uint16_t) * (wstr.length() + 1);
Tom Sepez0aa35312016-01-06 10:16:32 -0800140 std::unique_ptr<unsigned short, pdfium::FreeDeleter> result(
141 static_cast<unsigned short*>(malloc(length)));
142 char* ptr = reinterpret_cast<char*>(result.get());
Tom Sepez8ab45ea2016-01-05 10:17:30 -0800143 size_t i = 0;
144 for (wchar_t w : wstr) {
145 ptr[i++] = w & 0xff;
146 ptr[i++] = (w >> 8) & 0xff;
147 }
148 ptr[i++] = 0;
149 ptr[i] = 0;
Tom Sepez0aa35312016-01-06 10:16:32 -0800150 return result;
Tom Sepez8ab45ea2016-01-05 10:17:30 -0800151}
152
Lei Zhangd9e0e6e2017-04-28 16:54:10 -0700153std::string CryptToBase16(const uint8_t* digest) {
154 static char const zEncode[] = "0123456789abcdef";
155 std::string ret;
156 ret.resize(32);
157 for (int i = 0, j = 0; i < 16; i++, j += 2) {
158 uint8_t a = digest[i];
159 ret[j] = zEncode[(a >> 4) & 0xf];
160 ret[j + 1] = zEncode[a & 0xf];
161 }
162 return ret;
163}
164
165std::string GenerateMD5Base16(const uint8_t* data, uint32_t size) {
166 uint8_t digest[16];
167 CRYPT_MD5Generate(data, size, digest);
168 return CryptToBase16(digest);
169}
170
Tom Sepezd831dc72015-10-19 16:04:22 -0700171#ifdef PDF_ENABLE_V8
172#ifdef V8_USE_EXTERNAL_STARTUP_DATA
173bool InitializeV8ForPDFium(const std::string& exe_path,
174 const std::string& bin_dir,
175 v8::StartupData* natives_blob,
176 v8::StartupData* snapshot_blob,
177 v8::Platform** platform) {
jochen9e077d22016-06-09 02:51:13 -0700178 InitializeV8Common(exe_path.c_str(), platform);
thestigc08cd7a2016-06-27 09:47:59 -0700179 if (natives_blob && snapshot_blob) {
180 if (!GetExternalData(exe_path, bin_dir, "natives_blob.bin", natives_blob))
181 return false;
182 if (!GetExternalData(exe_path, bin_dir, "snapshot_blob.bin", snapshot_blob))
183 return false;
184 v8::V8::SetNativesDataBlob(natives_blob);
185 v8::V8::SetSnapshotDataBlob(snapshot_blob);
186 }
Tom Sepezd831dc72015-10-19 16:04:22 -0700187 return true;
188}
189#else // V8_USE_EXTERNAL_STARTUP_DATA
jochen9e077d22016-06-09 02:51:13 -0700190bool InitializeV8ForPDFium(const std::string& exe_path,
191 v8::Platform** platform) {
192 InitializeV8Common(exe_path.c_str(), platform);
Tom Sepezd831dc72015-10-19 16:04:22 -0700193 return true;
194}
195#endif // V8_USE_EXTERNAL_STARTUP_DATA
196#endif // PDF_ENABLE_V8
197
198TestLoader::TestLoader(const char* pBuf, size_t len)
199 : m_pBuf(pBuf), m_Len(len) {
200}
201
202// static
203int TestLoader::GetBlock(void* param,
204 unsigned long pos,
205 unsigned char* pBuf,
206 unsigned long size) {
207 TestLoader* pLoader = static_cast<TestLoader*>(param);
208 if (pos + size < pos || pos + size > pLoader->m_Len)
209 return 0;
210
211 memcpy(pBuf, pLoader->m_pBuf + pos, size);
212 return 1;
213}
Tom Sepez0aec19b2016-01-07 12:22:44 -0800214
215TestSaver::TestSaver() {
216 FPDF_FILEWRITE::version = 1;
217 FPDF_FILEWRITE::WriteBlock = WriteBlockCallback;
218}
219
220void TestSaver::ClearString() {
221 m_String.clear();
222}
223
224// static
225int TestSaver::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
226 const void* data,
227 unsigned long size) {
228 TestSaver* pThis = static_cast<TestSaver*>(pFileWrite);
229 pThis->m_String.append(static_cast<const char*>(data), size);
230 return 1;
231}
Chris Palmere4b035b2017-03-26 15:48:34 -0700232
Nicolas Pena742977f2017-04-13 15:28:20 -0400233// static
234int TestSaver::GetBlockFromString(void* param,
235 unsigned long pos,
236 unsigned char* buf,
237 unsigned long size) {
238 std::string* new_file = static_cast<std::string*>(param);
239 if (!new_file || pos + size < pos)
240 return 0;
241
242 unsigned long file_size = new_file->size();
243 if (pos + size > file_size)
244 return 0;
245
246 memcpy(buf, new_file->data() + pos, size);
247 return 1;
248}