blob: 2048dc87dfede82332d13a8f864a2f7d1b5bad0a [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"
Chris Palmer2b797292017-04-06 14:45:39 -070011#include "core/fxcrt/fx_memory.h"
Jane Liu4442d452017-07-19 10:19:42 -040012#include "core/fxcrt/fx_string.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"
Lei Zhang9a25ede2017-06-22 00:05:12 -070017#include "v8/include/v8.h"
Lei Zhang8241df72015-11-06 14:38:48 -080018#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;
Lei Zhangbcc03a42016-01-07 00:48:00 -080052 std::unique_ptr<char, pdfium::FreeDeleter> data_buffer =
53 GetFileContents(full_path.c_str(), &data_length);
54 if (!data_buffer)
Tom Sepezd831dc72015-10-19 16:04:22 -070055 return false;
Lei Zhangbcc03a42016-01-07 00:48:00 -080056
57 result_data->data = data_buffer.release();
Tom Sepezd831dc72015-10-19 16:04:22 -070058 result_data->raw_size = data_length;
59 return true;
60}
61#endif // V8_USE_EXTERNAL_STARTUP_DATA
62
Lei Zhangb0fb8cc2018-02-08 14:01:32 +000063std::unique_ptr<v8::Platform> InitializeV8Common(const std::string& exe_path) {
64 v8::V8::InitializeICUDefaultLocation(exe_path.c_str());
Tom Sepezd831dc72015-10-19 16:04:22 -070065
Lei Zhangb0fb8cc2018-02-08 14:01:32 +000066 std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform();
67 v8::V8::InitializePlatform(platform.get());
Tom Sepezd831dc72015-10-19 16:04:22 -070068
Tom Sepez2a5632a2018-11-29 00:12:34 +000069 const char* recommended_v8_flags = FPDF_GetRecommendedV8Flags();
70 v8::V8::SetFlagsFromString(recommended_v8_flags,
71 static_cast<int>(strlen(recommended_v8_flags)));
72
Tom Sepezd831dc72015-10-19 16:04:22 -070073 // By enabling predictable mode, V8 won't post any background tasks.
tsepez98822432017-01-11 05:53:10 -080074 // By enabling GC, it makes it easier to chase use-after-free.
Tom Sepez2a5632a2018-11-29 00:12:34 +000075 static const char kAdditionalV8Flags[] = "--predictable --expose-gc";
76 v8::V8::SetFlagsFromString(kAdditionalV8Flags,
77 static_cast<int>(strlen(kAdditionalV8Flags)));
78
tsepez98822432017-01-11 05:53:10 -080079 v8::V8::Initialize();
Lei Zhangb0fb8cc2018-02-08 14:01:32 +000080 return platform;
Tom Sepezd831dc72015-10-19 16:04:22 -070081}
82#endif // PDF_ENABLE_V8
83
84} // namespace
85
Tom Sepez0aa35312016-01-06 10:16:32 -080086std::unique_ptr<char, pdfium::FreeDeleter> GetFileContents(const char* filename,
87 size_t* retlen) {
Tom Sepezd831dc72015-10-19 16:04:22 -070088 FILE* file = fopen(filename, "rb");
89 if (!file) {
90 fprintf(stderr, "Failed to open: %s\n", filename);
91 return nullptr;
92 }
Ryan Harrison6cec70a2018-06-05 14:06:10 +000093 (void)fseek(file, 0, SEEK_END);
Tom Sepezd831dc72015-10-19 16:04:22 -070094 size_t file_length = ftell(file);
95 if (!file_length) {
96 return nullptr;
97 }
Ryan Harrison6cec70a2018-06-05 14:06:10 +000098 (void)fseek(file, 0, SEEK_SET);
Tom Sepez0aa35312016-01-06 10:16:32 -080099 std::unique_ptr<char, pdfium::FreeDeleter> buffer(
100 static_cast<char*>(malloc(file_length)));
Tom Sepezd831dc72015-10-19 16:04:22 -0700101 if (!buffer) {
102 return nullptr;
103 }
Tom Sepez0aa35312016-01-06 10:16:32 -0800104 size_t bytes_read = fread(buffer.get(), 1, file_length, file);
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000105 (void)fclose(file);
Tom Sepezd831dc72015-10-19 16:04:22 -0700106 if (bytes_read != file_length) {
107 fprintf(stderr, "Failed to read: %s\n", filename);
Tom Sepezd831dc72015-10-19 16:04:22 -0700108 return nullptr;
109 }
110 *retlen = bytes_read;
111 return buffer;
112}
113
Jane Liu4442d452017-07-19 10:19:42 -0400114std::string GetPlatformString(FPDF_WIDESTRING wstr) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400115 WideString wide_string =
116 WideString::FromUTF16LE(wstr, WideString::WStringLength(wstr));
Tom Sepezb4c95fe2018-11-27 01:09:44 +0000117 return std::string(wide_string.ToUTF8().c_str());
Jane Liu4442d452017-07-19 10:19:42 -0400118}
119
Tom Sepez8ab45ea2016-01-05 10:17:30 -0800120std::wstring GetPlatformWString(FPDF_WIDESTRING wstr) {
Lei Zhang79e893a2015-11-04 16:02:47 -0800121 if (!wstr)
122 return nullptr;
123
124 size_t characters = 0;
125 while (wstr[characters])
126 ++characters;
127
128 std::wstring platform_string(characters, L'\0');
129 for (size_t i = 0; i < characters + 1; ++i) {
130 const unsigned char* ptr = reinterpret_cast<const unsigned char*>(&wstr[i]);
131 platform_string[i] = ptr[0] + 256 * ptr[1];
132 }
133 return platform_string;
134}
135
tsepezf09bdfa2016-04-18 16:08:26 -0700136std::vector<std::string> StringSplit(const std::string& str, char delimiter) {
137 std::vector<std::string> result;
138 size_t pos = 0;
139 while (1) {
140 size_t found = str.find(delimiter, pos);
141 if (found == std::string::npos)
142 break;
143
144 result.push_back(str.substr(pos, found - pos));
145 pos = found + 1;
146 }
147 result.push_back(str.substr(pos));
148 return result;
149}
150
Tom Sepez0aa35312016-01-06 10:16:32 -0800151std::unique_ptr<unsigned short, pdfium::FreeDeleter> GetFPDFWideString(
152 const std::wstring& wstr) {
Tom Sepez8ab45ea2016-01-05 10:17:30 -0800153 size_t length = sizeof(uint16_t) * (wstr.length() + 1);
Tom Sepez0aa35312016-01-06 10:16:32 -0800154 std::unique_ptr<unsigned short, pdfium::FreeDeleter> result(
155 static_cast<unsigned short*>(malloc(length)));
156 char* ptr = reinterpret_cast<char*>(result.get());
Tom Sepez8ab45ea2016-01-05 10:17:30 -0800157 size_t i = 0;
158 for (wchar_t w : wstr) {
159 ptr[i++] = w & 0xff;
160 ptr[i++] = (w >> 8) & 0xff;
161 }
162 ptr[i++] = 0;
163 ptr[i] = 0;
Tom Sepez0aa35312016-01-06 10:16:32 -0800164 return result;
Tom Sepez8ab45ea2016-01-05 10:17:30 -0800165}
166
Lei Zhangd9e0e6e2017-04-28 16:54:10 -0700167std::string CryptToBase16(const uint8_t* digest) {
168 static char const zEncode[] = "0123456789abcdef";
169 std::string ret;
170 ret.resize(32);
171 for (int i = 0, j = 0; i < 16; i++, j += 2) {
172 uint8_t a = digest[i];
173 ret[j] = zEncode[(a >> 4) & 0xf];
174 ret[j + 1] = zEncode[a & 0xf];
175 }
176 return ret;
177}
178
179std::string GenerateMD5Base16(const uint8_t* data, uint32_t size) {
180 uint8_t digest[16];
181 CRYPT_MD5Generate(data, size, digest);
182 return CryptToBase16(digest);
183}
184
Tom Sepezd831dc72015-10-19 16:04:22 -0700185#ifdef PDF_ENABLE_V8
186#ifdef V8_USE_EXTERNAL_STARTUP_DATA
Lei Zhangb0fb8cc2018-02-08 14:01:32 +0000187std::unique_ptr<v8::Platform> InitializeV8ForPDFiumWithStartupData(
188 const std::string& exe_path,
189 const std::string& bin_dir,
190 v8::StartupData* natives_blob,
191 v8::StartupData* snapshot_blob) {
192 std::unique_ptr<v8::Platform> platform = InitializeV8Common(exe_path);
193 if (natives_blob && snapshot_blob) {
194 if (!GetExternalData(exe_path, bin_dir, "natives_blob.bin", natives_blob))
195 return nullptr;
196 if (!GetExternalData(exe_path, bin_dir, "snapshot_blob.bin", snapshot_blob))
197 return nullptr;
198 v8::V8::SetNativesDataBlob(natives_blob);
199 v8::V8::SetSnapshotDataBlob(snapshot_blob);
200 }
201 return platform;
202}
Tom Sepezd831dc72015-10-19 16:04:22 -0700203#else // V8_USE_EXTERNAL_STARTUP_DATA
Lei Zhangb0fb8cc2018-02-08 14:01:32 +0000204std::unique_ptr<v8::Platform> InitializeV8ForPDFium(
205 const std::string& exe_path) {
206 return InitializeV8Common(exe_path);
207}
Tom Sepezd831dc72015-10-19 16:04:22 -0700208#endif // V8_USE_EXTERNAL_STARTUP_DATA
209#endif // PDF_ENABLE_V8
210
211TestLoader::TestLoader(const char* pBuf, size_t len)
212 : m_pBuf(pBuf), m_Len(len) {
213}
214
215// static
216int TestLoader::GetBlock(void* param,
217 unsigned long pos,
218 unsigned char* pBuf,
219 unsigned long size) {
220 TestLoader* pLoader = static_cast<TestLoader*>(param);
221 if (pos + size < pos || pos + size > pLoader->m_Len)
222 return 0;
223
224 memcpy(pBuf, pLoader->m_pBuf + pos, size);
225 return 1;
226}