Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 1 | // 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 | |
| 16 | namespace { |
| 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. |
| 22 | std::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 | |
| 41 | bool 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 | |
| 58 | void 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 | |
| 74 | char* GetFileContents(const char* filename, size_t* retlen) { |
| 75 | FILE* file = fopen(filename, "rb"); |
| 76 | if (!file) { |
| 77 | fprintf(stderr, "Failed to open: %s\n", filename); |
| 78 | return nullptr; |
| 79 | } |
| 80 | (void)fseek(file, 0, SEEK_END); |
| 81 | size_t file_length = ftell(file); |
| 82 | if (!file_length) { |
| 83 | return nullptr; |
| 84 | } |
| 85 | (void)fseek(file, 0, SEEK_SET); |
| 86 | char* buffer = (char*)malloc(file_length); |
| 87 | if (!buffer) { |
| 88 | return nullptr; |
| 89 | } |
| 90 | size_t bytes_read = fread(buffer, 1, file_length, file); |
| 91 | (void)fclose(file); |
| 92 | if (bytes_read != file_length) { |
| 93 | fprintf(stderr, "Failed to read: %s\n", filename); |
| 94 | free(buffer); |
| 95 | return nullptr; |
| 96 | } |
| 97 | *retlen = bytes_read; |
| 98 | return buffer; |
| 99 | } |
| 100 | |
Lei Zhang | 79e893a | 2015-11-04 16:02:47 -0800 | [diff] [blame] | 101 | std::wstring GetWideString(FPDF_WIDESTRING wstr) { |
| 102 | if (!wstr) |
| 103 | return nullptr; |
| 104 | |
| 105 | size_t characters = 0; |
| 106 | while (wstr[characters]) |
| 107 | ++characters; |
| 108 | |
| 109 | std::wstring platform_string(characters, L'\0'); |
| 110 | for (size_t i = 0; i < characters + 1; ++i) { |
| 111 | const unsigned char* ptr = reinterpret_cast<const unsigned char*>(&wstr[i]); |
| 112 | platform_string[i] = ptr[0] + 256 * ptr[1]; |
| 113 | } |
| 114 | return platform_string; |
| 115 | } |
| 116 | |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 117 | #ifdef PDF_ENABLE_V8 |
| 118 | #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 119 | bool InitializeV8ForPDFium(const std::string& exe_path, |
| 120 | const std::string& bin_dir, |
| 121 | v8::StartupData* natives_blob, |
| 122 | v8::StartupData* snapshot_blob, |
| 123 | v8::Platform** platform) { |
| 124 | InitializeV8Common(platform); |
| 125 | if (!GetExternalData(exe_path, bin_dir, "natives_blob.bin", natives_blob)) |
| 126 | return false; |
| 127 | if (!GetExternalData(exe_path, bin_dir, "snapshot_blob.bin", snapshot_blob)) |
| 128 | return false; |
| 129 | v8::V8::SetNativesDataBlob(natives_blob); |
| 130 | v8::V8::SetSnapshotDataBlob(snapshot_blob); |
| 131 | return true; |
| 132 | } |
| 133 | #else // V8_USE_EXTERNAL_STARTUP_DATA |
| 134 | bool InitializeV8ForPDFium(v8::Platform** platform) { |
| 135 | InitializeV8Common(platform); |
| 136 | return true; |
| 137 | } |
| 138 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 139 | #endif // PDF_ENABLE_V8 |
| 140 | |
| 141 | TestLoader::TestLoader(const char* pBuf, size_t len) |
| 142 | : m_pBuf(pBuf), m_Len(len) { |
| 143 | } |
| 144 | |
| 145 | // static |
| 146 | int TestLoader::GetBlock(void* param, |
| 147 | unsigned long pos, |
| 148 | unsigned char* pBuf, |
| 149 | unsigned long size) { |
| 150 | TestLoader* pLoader = static_cast<TestLoader*>(param); |
| 151 | if (pos + size < pos || pos + size > pLoader->m_Len) |
| 152 | return 0; |
| 153 | |
| 154 | memcpy(pBuf, pLoader->m_pBuf + pos, size); |
| 155 | return 1; |
| 156 | } |