blob: 03d243d5b8b360472d7fdf3ffdb2ba2ec80ce354 [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
Lei Zhangd145e4b2018-10-12 18:54:31 +000010#include "core/fdrm/fx_crypt.h"
Jane Liu4442d452017-07-19 10:19:42 -040011#include "core/fxcrt/fx_string.h"
Wei Li091f7a02015-11-09 12:09:55 -080012#include "testing/utils/path_service.h"
Tom Sepezd831dc72015-10-19 16:04:22 -070013
Lei Zhang8241df72015-11-06 14:38:48 -080014#ifdef PDF_ENABLE_V8
15#include "v8/include/libplatform/libplatform.h"
Lei Zhang9a25ede2017-06-22 00:05:12 -070016#include "v8/include/v8.h"
Lei Zhang8241df72015-11-06 14:38:48 -080017#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
Lei Zhangb0fb8cc2018-02-08 14:01:32 +000062std::unique_ptr<v8::Platform> InitializeV8Common(const std::string& exe_path) {
63 v8::V8::InitializeICUDefaultLocation(exe_path.c_str());
Tom Sepezd831dc72015-10-19 16:04:22 -070064
Lei Zhangb0fb8cc2018-02-08 14:01:32 +000065 std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform();
66 v8::V8::InitializePlatform(platform.get());
Tom Sepezd831dc72015-10-19 16:04:22 -070067
Tom Sepez2a5632a2018-11-29 00:12:34 +000068 const char* recommended_v8_flags = FPDF_GetRecommendedV8Flags();
69 v8::V8::SetFlagsFromString(recommended_v8_flags,
70 static_cast<int>(strlen(recommended_v8_flags)));
71
Tom Sepezd831dc72015-10-19 16:04:22 -070072 // By enabling predictable mode, V8 won't post any background tasks.
tsepez98822432017-01-11 05:53:10 -080073 // By enabling GC, it makes it easier to chase use-after-free.
Tom Sepez2a5632a2018-11-29 00:12:34 +000074 static const char kAdditionalV8Flags[] = "--predictable --expose-gc";
75 v8::V8::SetFlagsFromString(kAdditionalV8Flags,
76 static_cast<int>(strlen(kAdditionalV8Flags)));
77
tsepez98822432017-01-11 05:53:10 -080078 v8::V8::Initialize();
Lei Zhangb0fb8cc2018-02-08 14:01:32 +000079 return platform;
Tom Sepezd831dc72015-10-19 16:04:22 -070080}
81#endif // PDF_ENABLE_V8
82
83} // namespace
84
Tom Sepez0aa35312016-01-06 10:16:32 -080085std::unique_ptr<char, pdfium::FreeDeleter> GetFileContents(const char* filename,
86 size_t* retlen) {
Tom Sepezd831dc72015-10-19 16:04:22 -070087 FILE* file = fopen(filename, "rb");
88 if (!file) {
89 fprintf(stderr, "Failed to open: %s\n", filename);
90 return nullptr;
91 }
Ryan Harrison6cec70a2018-06-05 14:06:10 +000092 (void)fseek(file, 0, SEEK_END);
Tom Sepezd831dc72015-10-19 16:04:22 -070093 size_t file_length = ftell(file);
94 if (!file_length) {
95 return nullptr;
96 }
Ryan Harrison6cec70a2018-06-05 14:06:10 +000097 (void)fseek(file, 0, SEEK_SET);
Tom Sepez0aa35312016-01-06 10:16:32 -080098 std::unique_ptr<char, pdfium::FreeDeleter> buffer(
99 static_cast<char*>(malloc(file_length)));
Tom Sepezd831dc72015-10-19 16:04:22 -0700100 if (!buffer) {
101 return nullptr;
102 }
Tom Sepez0aa35312016-01-06 10:16:32 -0800103 size_t bytes_read = fread(buffer.get(), 1, file_length, file);
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000104 (void)fclose(file);
Tom Sepezd831dc72015-10-19 16:04:22 -0700105 if (bytes_read != file_length) {
106 fprintf(stderr, "Failed to read: %s\n", filename);
Tom Sepezd831dc72015-10-19 16:04:22 -0700107 return nullptr;
108 }
109 *retlen = bytes_read;
110 return buffer;
111}
112
Jane Liu4442d452017-07-19 10:19:42 -0400113std::string GetPlatformString(FPDF_WIDESTRING wstr) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400114 WideString wide_string =
115 WideString::FromUTF16LE(wstr, WideString::WStringLength(wstr));
Tom Sepezb4c95fe2018-11-27 01:09:44 +0000116 return std::string(wide_string.ToUTF8().c_str());
Jane Liu4442d452017-07-19 10:19:42 -0400117}
118
Tom Sepez8ab45ea2016-01-05 10:17:30 -0800119std::wstring GetPlatformWString(FPDF_WIDESTRING wstr) {
Lei Zhang79e893a2015-11-04 16:02:47 -0800120 if (!wstr)
121 return nullptr;
122
123 size_t characters = 0;
124 while (wstr[characters])
125 ++characters;
126
127 std::wstring platform_string(characters, L'\0');
128 for (size_t i = 0; i < characters + 1; ++i) {
129 const unsigned char* ptr = reinterpret_cast<const unsigned char*>(&wstr[i]);
130 platform_string[i] = ptr[0] + 256 * ptr[1];
131 }
132 return platform_string;
133}
134
tsepezf09bdfa2016-04-18 16:08:26 -0700135std::vector<std::string> StringSplit(const std::string& str, char delimiter) {
136 std::vector<std::string> result;
137 size_t pos = 0;
138 while (1) {
139 size_t found = str.find(delimiter, pos);
140 if (found == std::string::npos)
141 break;
142
143 result.push_back(str.substr(pos, found - pos));
144 pos = found + 1;
145 }
146 result.push_back(str.substr(pos));
147 return result;
148}
149
Tom Sepez0aa35312016-01-06 10:16:32 -0800150std::unique_ptr<unsigned short, pdfium::FreeDeleter> GetFPDFWideString(
151 const std::wstring& wstr) {
Tom Sepez8ab45ea2016-01-05 10:17:30 -0800152 size_t length = sizeof(uint16_t) * (wstr.length() + 1);
Tom Sepez0aa35312016-01-06 10:16:32 -0800153 std::unique_ptr<unsigned short, pdfium::FreeDeleter> result(
154 static_cast<unsigned short*>(malloc(length)));
155 char* ptr = reinterpret_cast<char*>(result.get());
Tom Sepez8ab45ea2016-01-05 10:17:30 -0800156 size_t i = 0;
157 for (wchar_t w : wstr) {
158 ptr[i++] = w & 0xff;
159 ptr[i++] = (w >> 8) & 0xff;
160 }
161 ptr[i++] = 0;
162 ptr[i] = 0;
Tom Sepez0aa35312016-01-06 10:16:32 -0800163 return result;
Tom Sepez8ab45ea2016-01-05 10:17:30 -0800164}
165
Lei Zhangd9e0e6e2017-04-28 16:54:10 -0700166std::string CryptToBase16(const uint8_t* digest) {
167 static char const zEncode[] = "0123456789abcdef";
168 std::string ret;
169 ret.resize(32);
170 for (int i = 0, j = 0; i < 16; i++, j += 2) {
171 uint8_t a = digest[i];
172 ret[j] = zEncode[(a >> 4) & 0xf];
173 ret[j + 1] = zEncode[a & 0xf];
174 }
175 return ret;
176}
177
178std::string GenerateMD5Base16(const uint8_t* data, uint32_t size) {
179 uint8_t digest[16];
180 CRYPT_MD5Generate(data, size, digest);
181 return CryptToBase16(digest);
182}
183
Tom Sepezd831dc72015-10-19 16:04:22 -0700184#ifdef PDF_ENABLE_V8
185#ifdef V8_USE_EXTERNAL_STARTUP_DATA
Lei Zhangb0fb8cc2018-02-08 14:01:32 +0000186std::unique_ptr<v8::Platform> InitializeV8ForPDFiumWithStartupData(
187 const std::string& exe_path,
188 const std::string& bin_dir,
189 v8::StartupData* natives_blob,
190 v8::StartupData* snapshot_blob) {
191 std::unique_ptr<v8::Platform> platform = InitializeV8Common(exe_path);
192 if (natives_blob && snapshot_blob) {
193 if (!GetExternalData(exe_path, bin_dir, "natives_blob.bin", natives_blob))
194 return nullptr;
195 if (!GetExternalData(exe_path, bin_dir, "snapshot_blob.bin", snapshot_blob))
196 return nullptr;
197 v8::V8::SetNativesDataBlob(natives_blob);
198 v8::V8::SetSnapshotDataBlob(snapshot_blob);
199 }
200 return platform;
201}
Tom Sepezd831dc72015-10-19 16:04:22 -0700202#else // V8_USE_EXTERNAL_STARTUP_DATA
Lei Zhangb0fb8cc2018-02-08 14:01:32 +0000203std::unique_ptr<v8::Platform> InitializeV8ForPDFium(
204 const std::string& exe_path) {
205 return InitializeV8Common(exe_path);
206}
Tom Sepezd831dc72015-10-19 16:04:22 -0700207#endif // V8_USE_EXTERNAL_STARTUP_DATA
208#endif // PDF_ENABLE_V8
209
210TestLoader::TestLoader(const char* pBuf, size_t len)
211 : m_pBuf(pBuf), m_Len(len) {
212}
213
214// static
215int TestLoader::GetBlock(void* param,
216 unsigned long pos,
217 unsigned char* pBuf,
218 unsigned long size) {
219 TestLoader* pLoader = static_cast<TestLoader*>(param);
Lei Zhangcef91f12019-01-08 21:32:51 +0000220 if (pos + size < pos || pos + size > pLoader->m_Len) {
221 NOTREACHED();
Tom Sepezd831dc72015-10-19 16:04:22 -0700222 return 0;
Lei Zhangcef91f12019-01-08 21:32:51 +0000223 }
Tom Sepezd831dc72015-10-19 16:04:22 -0700224
225 memcpy(pBuf, pLoader->m_pBuf + pos, size);
226 return 1;
227}