Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: testing/test_support.cpp

Issue 1564583004: Merge to XFA: Return unique_ptrs from test_support functions (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@xfa
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « testing/test_support.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 PDFium Authors. All rights reserved. 1 // Copyright 2015 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "testing/test_support.h" 5 #include "testing/test_support.h"
6 6
7 #include <stdio.h> 7 #include <stdio.h>
8 #include <string.h> 8 #include <string.h>
9 9
10 #include "testing/utils/path_service.h" 10 #include "testing/utils/path_service.h"
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 64
65 // By enabling predictable mode, V8 won't post any background tasks. 65 // By enabling predictable mode, V8 won't post any background tasks.
66 const char predictable_flag[] = "--predictable"; 66 const char predictable_flag[] = "--predictable";
67 v8::V8::SetFlagsFromString(predictable_flag, 67 v8::V8::SetFlagsFromString(predictable_flag,
68 static_cast<int>(strlen(predictable_flag))); 68 static_cast<int>(strlen(predictable_flag)));
69 } 69 }
70 #endif // PDF_ENABLE_V8 70 #endif // PDF_ENABLE_V8
71 71
72 } // namespace 72 } // namespace
73 73
74 char* GetFileContents(const char* filename, size_t* retlen) { 74 std::unique_ptr<char, pdfium::FreeDeleter> GetFileContents(const char* filename,
75 size_t* retlen) {
75 FILE* file = fopen(filename, "rb"); 76 FILE* file = fopen(filename, "rb");
76 if (!file) { 77 if (!file) {
77 fprintf(stderr, "Failed to open: %s\n", filename); 78 fprintf(stderr, "Failed to open: %s\n", filename);
78 return nullptr; 79 return nullptr;
79 } 80 }
80 (void)fseek(file, 0, SEEK_END); 81 (void)fseek(file, 0, SEEK_END);
81 size_t file_length = ftell(file); 82 size_t file_length = ftell(file);
82 if (!file_length) { 83 if (!file_length) {
83 return nullptr; 84 return nullptr;
84 } 85 }
85 (void)fseek(file, 0, SEEK_SET); 86 (void)fseek(file, 0, SEEK_SET);
86 char* buffer = (char*)malloc(file_length); 87 std::unique_ptr<char, pdfium::FreeDeleter> buffer(
88 static_cast<char*>(malloc(file_length)));
87 if (!buffer) { 89 if (!buffer) {
88 return nullptr; 90 return nullptr;
89 } 91 }
90 size_t bytes_read = fread(buffer, 1, file_length, file); 92 size_t bytes_read = fread(buffer.get(), 1, file_length, file);
91 (void)fclose(file); 93 (void)fclose(file);
92 if (bytes_read != file_length) { 94 if (bytes_read != file_length) {
93 fprintf(stderr, "Failed to read: %s\n", filename); 95 fprintf(stderr, "Failed to read: %s\n", filename);
94 free(buffer);
95 return nullptr; 96 return nullptr;
96 } 97 }
97 *retlen = bytes_read; 98 *retlen = bytes_read;
98 return buffer; 99 return buffer;
99 } 100 }
100 101
101 std::wstring GetPlatformWString(FPDF_WIDESTRING wstr) { 102 std::wstring GetPlatformWString(FPDF_WIDESTRING wstr) {
102 if (!wstr) 103 if (!wstr)
103 return nullptr; 104 return nullptr;
104 105
105 size_t characters = 0; 106 size_t characters = 0;
106 while (wstr[characters]) 107 while (wstr[characters])
107 ++characters; 108 ++characters;
108 109
109 std::wstring platform_string(characters, L'\0'); 110 std::wstring platform_string(characters, L'\0');
110 for (size_t i = 0; i < characters + 1; ++i) { 111 for (size_t i = 0; i < characters + 1; ++i) {
111 const unsigned char* ptr = reinterpret_cast<const unsigned char*>(&wstr[i]); 112 const unsigned char* ptr = reinterpret_cast<const unsigned char*>(&wstr[i]);
112 platform_string[i] = ptr[0] + 256 * ptr[1]; 113 platform_string[i] = ptr[0] + 256 * ptr[1];
113 } 114 }
114 return platform_string; 115 return platform_string;
115 } 116 }
116 117
117 FPDF_WIDESTRING GetFPDFWideString(const std::wstring& wstr) { 118 std::unique_ptr<unsigned short, pdfium::FreeDeleter> GetFPDFWideString(
119 const std::wstring& wstr) {
118 size_t length = sizeof(uint16_t) * (wstr.length() + 1); 120 size_t length = sizeof(uint16_t) * (wstr.length() + 1);
119 unsigned char* ptr = static_cast<unsigned char*>(malloc(length)); 121 std::unique_ptr<unsigned short, pdfium::FreeDeleter> result(
122 static_cast<unsigned short*>(malloc(length)));
123 char* ptr = reinterpret_cast<char*>(result.get());
120 size_t i = 0; 124 size_t i = 0;
121 for (wchar_t w : wstr) { 125 for (wchar_t w : wstr) {
122 ptr[i++] = w & 0xff; 126 ptr[i++] = w & 0xff;
123 ptr[i++] = (w >> 8) & 0xff; 127 ptr[i++] = (w >> 8) & 0xff;
124 } 128 }
125 ptr[i++] = 0; 129 ptr[i++] = 0;
126 ptr[i] = 0; 130 ptr[i] = 0;
127 return reinterpret_cast<FPDF_WIDESTRING>(ptr); 131 return result;
128 } 132 }
129 133
130 #ifdef PDF_ENABLE_V8 134 #ifdef PDF_ENABLE_V8
131 #ifdef V8_USE_EXTERNAL_STARTUP_DATA 135 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
132 bool InitializeV8ForPDFium(const std::string& exe_path, 136 bool InitializeV8ForPDFium(const std::string& exe_path,
133 const std::string& bin_dir, 137 const std::string& bin_dir,
134 v8::StartupData* natives_blob, 138 v8::StartupData* natives_blob,
135 v8::StartupData* snapshot_blob, 139 v8::StartupData* snapshot_blob,
136 v8::Platform** platform) { 140 v8::Platform** platform) {
137 InitializeV8Common(platform); 141 InitializeV8Common(platform);
(...skipping 22 matching lines...) Expand all
160 unsigned long pos, 164 unsigned long pos,
161 unsigned char* pBuf, 165 unsigned char* pBuf,
162 unsigned long size) { 166 unsigned long size) {
163 TestLoader* pLoader = static_cast<TestLoader*>(param); 167 TestLoader* pLoader = static_cast<TestLoader*>(param);
164 if (pos + size < pos || pos + size > pLoader->m_Len) 168 if (pos + size < pos || pos + size > pLoader->m_Len)
165 return 0; 169 return 0;
166 170
167 memcpy(pBuf, pLoader->m_pBuf + pos, size); 171 memcpy(pBuf, pLoader->m_pBuf + pos, size);
168 return 1; 172 return 1;
169 } 173 }
OLDNEW
« no previous file with comments | « testing/test_support.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698