Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium 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 <limits.h> | 5 #include <limits.h> |
| 6 #include <stdio.h> | 6 #include <stdio.h> |
| 7 #include <stdlib.h> | 7 #include <stdlib.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 #include <wchar.h> | 9 #include <wchar.h> |
| 10 | 10 |
| 11 #include <list> | 11 #include <list> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <utility> | 13 #include <utility> |
| 14 | 14 |
| 15 #include "../fpdfsdk/include/fpdf_dataavail.h" | 15 #include "../fpdfsdk/include/fpdf_dataavail.h" |
| 16 #include "../fpdfsdk/include/fpdf_ext.h" | 16 #include "../fpdfsdk/include/fpdf_ext.h" |
| 17 #include "../fpdfsdk/include/fpdfformfill.h" | 17 #include "../fpdfsdk/include/fpdfformfill.h" |
| 18 #include "../fpdfsdk/include/fpdftext.h" | 18 #include "../fpdfsdk/include/fpdftext.h" |
| 19 #include "../fpdfsdk/include/fpdfview.h" | 19 #include "../fpdfsdk/include/fpdfview.h" |
| 20 #include "../core/include/fxcrt/fx_system.h" | 20 #include "../core/include/fxcrt/fx_system.h" |
| 21 #include "v8/include/v8.h" | 21 #include "v8/include/v8.h" |
| 22 | 22 |
| 23 #ifdef _WIN32 | 23 #ifdef _WIN32 |
| 24 #define snprintf _snprintf | 24 #define snprintf _snprintf |
| 25 #define SEPARATOR '\\' | |
|
Lei Zhang
2014/12/17 22:35:50
Maybe PATH_SEPARATOR?
Tom Sepez
2014/12/17 23:52:59
Done.
| |
| 26 #else | |
| 27 #define SEPARATOR '/' | |
| 25 #endif | 28 #endif |
| 26 | 29 |
| 27 enum OutputFormat { | 30 enum OutputFormat { |
| 28 OUTPUT_NONE, | 31 OUTPUT_NONE, |
| 29 OUTPUT_PPM, | 32 OUTPUT_PPM, |
| 30 #ifdef _WIN32 | 33 #ifdef _WIN32 |
| 31 OUTPUT_BMP, | 34 OUTPUT_BMP, |
| 32 OUTPUT_EMF, | 35 OUTPUT_EMF, |
| 33 #endif | 36 #endif |
| 34 }; | 37 }; |
| 35 | 38 |
| 39 struct Options { | |
| 40 OutputFormat output_format; | |
| 41 const char* exe_path; | |
| 42 const char* bin_directory; | |
| 43 }; | |
| 44 | |
| 45 static char* GetFileContents(const char* filename, size_t* retlen) { | |
|
Lei Zhang
2014/12/17 22:35:51
It would be nice to document the functions.
Tom Sepez
2014/12/17 23:52:59
Done.
| |
| 46 FILE* file = fopen(filename, "rb"); | |
| 47 if (!file) { | |
| 48 fprintf(stderr, "Failed to open: %s\n", filename); | |
| 49 return NULL; | |
| 50 } | |
| 51 (void) fseek(file, 0, SEEK_END); | |
| 52 size_t file_length = ftell(file); | |
|
Lei Zhang
2014/12/17 22:35:50
Can we get 0 here? If so, don't bother mallocing?
Tom Sepez
2014/12/17 23:52:59
Done.
| |
| 53 (void) fseek(file, 0, SEEK_SET); | |
| 54 char* buffer = (char*) malloc(file_length); | |
| 55 if (!buffer) { | |
| 56 return NULL; | |
| 57 } | |
| 58 size_t bytes_read = fread(buffer, 1, file_length, file); | |
| 59 (void) fclose(file); | |
| 60 if (bytes_read != file_length) { | |
| 61 fprintf(stderr, "Failed to read: %s\n", filename); | |
| 62 free(buffer); | |
| 63 return NULL; | |
| 64 } | |
| 65 *retlen = bytes_read; | |
| 66 return buffer; | |
| 67 } | |
| 68 | |
| 69 #ifdef V8_USE_EXTERNAL_STARTUP_DATA | |
| 70 static bool GetFullPathForSnapshotFile(const Options& options, | |
| 71 const char* filename, | |
| 72 char* output_buffer, | |
|
Lei Zhang
2014/12/17 22:35:51
|output_buffer| and |output_buffer_size| -> a sing
Tom Sepez
2014/12/17 23:52:59
I think I'm going to replace this whole mess with
Lei Zhang
2014/12/18 00:07:01
Oh, so much nicer. :)
| |
| 73 size_t output_buffer_size) { | |
| 74 char* insertion_point = output_buffer; | |
| 75 size_t space_remaining = output_buffer_size; | |
| 76 if (options.bin_directory) { | |
| 77 size_t bin_directory_len = strlen(options.bin_directory); | |
| 78 if (bin_directory_len > space_remaining) { | |
| 79 return false; | |
| 80 } | |
| 81 if (bin_directory_len) { | |
| 82 memcpy(output_buffer, options.bin_directory, bin_directory_len); | |
| 83 insertion_point += bin_directory_len; | |
| 84 space_remaining -= bin_directory_len; | |
| 85 if (*(insertion_point - 1) != SEPARATOR) { | |
| 86 if (space_remaining == 0) { | |
| 87 return false; | |
| 88 } | |
| 89 *insertion_point = SEPARATOR; | |
| 90 insertion_point += 1; | |
| 91 space_remaining -= 1; | |
| 92 } | |
| 93 } | |
| 94 } else if (options.exe_path) { | |
| 95 const char* last_separator = strrchr(options.exe_path, SEPARATOR); | |
| 96 if (last_separator) { | |
| 97 size_t exe_path_len = 1 + last_separator - options.exe_path; | |
| 98 if (exe_path_len > space_remaining) { | |
| 99 return false; | |
| 100 } | |
| 101 memcpy(output_buffer, options.exe_path, exe_path_len); | |
| 102 insertion_point += exe_path_len; | |
| 103 space_remaining -= exe_path_len; | |
| 104 } | |
| 105 } | |
| 106 size_t filename_len = strlen(filename); | |
| 107 if (filename_len + 1 > space_remaining) { | |
| 108 return false; | |
| 109 } | |
| 110 memcpy(insertion_point, filename, filename_len); | |
| 111 insertion_point += filename_len; | |
| 112 space_remaining -= filename_len; | |
| 113 *insertion_point = '\0'; | |
| 114 return true; | |
| 115 } | |
| 116 | |
| 117 static bool GetExternalData(const Options& options, | |
| 118 const char *bin_filename, | |
|
Lei Zhang
2014/12/17 22:35:51
nit: char*, ditto below.
| |
| 119 v8::StartupData* result_data) { | |
| 120 char full_path[4096]; | |
|
Lei Zhang
2014/12/17 22:35:51
4096 -> PATH_MAX ?
Tom Sepez
2014/12/17 23:52:59
Present on windows?
| |
| 121 if (!GetFullPathForSnapshotFile(options, bin_filename, | |
| 122 full_path, sizeof(full_path))) { | |
| 123 fprintf(stderr, "GetFullPathForSnapshotFile() failed\n"); | |
| 124 return false; | |
| 125 } | |
| 126 size_t data_length = 0; | |
| 127 char *data_buffer = GetFileContents(full_path, &data_length); | |
| 128 if (!data_buffer) { | |
| 129 return false; | |
| 130 } | |
| 131 result_data->data = const_cast<const char*>(data_buffer); | |
| 132 result_data->raw_size = data_length; | |
| 133 return true; | |
| 134 } | |
| 135 #endif // V8_USE_EXTERNAL_STARTUP_DATA | |
| 136 | |
| 36 static void WritePpm(const char* pdf_name, int num, const void* buffer_void, | 137 static void WritePpm(const char* pdf_name, int num, const void* buffer_void, |
| 37 int stride, int width, int height) { | 138 int stride, int width, int height) { |
| 38 const char* buffer = reinterpret_cast<const char*>(buffer_void); | 139 const char* buffer = reinterpret_cast<const char*>(buffer_void); |
| 39 | 140 |
| 40 if (stride < 0 || width < 0 || height < 0) | 141 if (stride < 0 || width < 0 || height < 0) |
| 41 return; | 142 return; |
| 42 if (height > 0 && width > INT_MAX / height) | 143 if (height > 0 && width > INT_MAX / height) |
| 43 return; | 144 return; |
| 44 int out_len = width * height; | 145 int out_len = width * height; |
| 45 if (out_len > INT_MAX / 3) | 146 if (out_len > INT_MAX / 3) |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 191 case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA: | 292 case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA: |
| 192 feature = "Screen"; | 293 feature = "Screen"; |
| 193 break; | 294 break; |
| 194 case FPDF_UNSP_ANNOT_SIG: | 295 case FPDF_UNSP_ANNOT_SIG: |
| 195 feature = "Digital_Signature"; | 296 feature = "Digital_Signature"; |
| 196 break; | 297 break; |
| 197 } | 298 } |
| 198 printf("Unsupported feature: %s.\n", feature.c_str()); | 299 printf("Unsupported feature: %s.\n", feature.c_str()); |
| 199 } | 300 } |
| 200 | 301 |
| 201 bool ParseCommandLine(int argc, const char* argv[], OutputFormat* output_format, | 302 bool ParseCommandLine(int argc, const char* argv[], Options* options, |
| 202 std::list<const char*>* files) { | 303 std::list<const char*>* files) { |
| 203 *output_format = OUTPUT_NONE; | 304 options->output_format = OUTPUT_NONE; |
|
Lei Zhang
2014/12/17 22:35:50
Options probably should have a constructor to do t
Tom Sepez
2014/12/17 23:52:59
D'oh. Done.
| |
| 305 options->exe_path = NULL; | |
| 306 options->bin_directory = NULL; | |
| 204 files->clear(); | 307 files->clear(); |
| 205 | 308 if (!argc) { |
| 206 int cur_arg = 1; | 309 return false; |
| 207 for (; cur_arg < argc; ++cur_arg) { | 310 } |
| 208 if (strcmp(argv[cur_arg], "--ppm") == 0) | 311 options->exe_path = argv[0]; |
| 209 *output_format = OUTPUT_PPM; | 312 int cur_idx = 1; |
| 313 for (; cur_idx < argc; ++cur_idx) { | |
| 314 const char* cur_arg = argv[cur_idx]; | |
| 315 size_t arg_length = strlen(cur_arg); | |
| 316 if (strcmp(cur_arg, "--ppm") == 0) { | |
| 317 if (options->output_format != OUTPUT_NONE) { | |
| 318 fprintf(stderr, "Duplicate format argument\n"); | |
| 319 return false; | |
| 320 } | |
| 321 options->output_format = OUTPUT_PPM; | |
| 322 } | |
| 210 #ifdef _WIN32 | 323 #ifdef _WIN32 |
| 211 else if (strcmp(argv[cur_arg], "--emf") == 0) | 324 else if (strcmp(cur_arg, "--emf") == 0) { |
| 212 *output_format = OUTPUT_EMF; | 325 if (options->output_format != OUTPUT_NONE) { |
| 213 else if (strcmp(argv[cur_arg], "--bmp") == 0) | 326 fprintf(stderr, "Duplicate format argument\n"); |
| 214 *output_format = OUTPUT_BMP; | 327 return false; |
| 215 #endif | 328 } |
| 329 options->output_format = OUTPUT_EMF; | |
| 330 } | |
| 331 else if (strcmp(cur_arg, "--bmp") == 0) { | |
| 332 if (options->output_format != OUTPUT_NONE) { | |
| 333 fprintf(stderr, "Duplicate format argument\n"); | |
| 334 return false; | |
| 335 } | |
| 336 options->output_format = OUTPUT_BMP; | |
| 337 } | |
| 338 #endif // _WIN32 | |
| 339 #ifdef V8_USE_EXTERNAL_STARTUP_DATA | |
| 340 else if (arg_length > 10 && memcmp(cur_arg, "--bin-dir=", 10) == 0) { | |
| 341 if (options->bin_directory) { | |
| 342 fprintf(stderr, "Duplicate --bin-dir argument\n"); | |
| 343 return false; | |
| 344 } | |
| 345 options->bin_directory = cur_arg + 10; | |
| 346 } | |
| 347 #endif // V8_USE_EXTERNAL_STARTUP_DATA | |
| 216 else | 348 else |
| 217 break; | 349 break; |
| 218 } | 350 } |
| 219 | 351 |
| 220 if (cur_arg > 2) // Multiple options. | 352 if (cur_idx >= argc) { |
| 353 fprintf(stderr, "No input files.\n"); | |
| 221 return false; | 354 return false; |
| 355 } | |
| 222 | 356 |
| 223 if (cur_arg >= argc) // No input files. | 357 for (int i = cur_idx; i < argc; i++) |
| 224 return false; | |
| 225 | |
| 226 for (int i = cur_arg; i < argc; i++) | |
| 227 files->push_back(argv[i]); | 358 files->push_back(argv[i]); |
| 228 | 359 |
| 229 return true; | 360 return true; |
| 230 } | 361 } |
| 231 | 362 |
| 232 class TestLoader { | 363 class TestLoader { |
| 233 public: | 364 public: |
| 234 TestLoader(const char* pBuf, size_t len); | 365 TestLoader(const char* pBuf, size_t len); |
| 235 | 366 |
| 236 const char* m_pBuf; | 367 const char* m_pBuf; |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 375 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC); | 506 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC); |
| 376 FPDF_CloseDocument(doc); | 507 FPDF_CloseDocument(doc); |
| 377 FPDFDOC_ExitFormFillEnvironment(form); | 508 FPDFDOC_ExitFormFillEnvironment(form); |
| 378 FPDFAvail_Destroy(pdf_avail); | 509 FPDFAvail_Destroy(pdf_avail); |
| 379 | 510 |
| 380 printf("Loaded, parsed and rendered %" PRIuS " pages.\n", rendered_pages); | 511 printf("Loaded, parsed and rendered %" PRIuS " pages.\n", rendered_pages); |
| 381 printf("Skipped %" PRIuS " bad pages.\n", bad_pages); | 512 printf("Skipped %" PRIuS " bad pages.\n", bad_pages); |
| 382 } | 513 } |
| 383 | 514 |
| 384 int main(int argc, const char* argv[]) { | 515 int main(int argc, const char* argv[]) { |
| 385 v8::V8::InitializeICU(); | 516 Options options; |
| 386 OutputFormat format = OUTPUT_NONE; | |
| 387 std::list<const char*> files; | 517 std::list<const char*> files; |
| 388 if (!ParseCommandLine(argc, argv, &format, &files)) { | 518 if (!ParseCommandLine(argc, argv, &options, &files)) { |
| 389 printf("Usage: pdfium_test [OPTION] [FILE]...\n"); | 519 printf("Usage: pdfium_test [OPTION] [FILE]...\n"); |
| 390 printf("--ppm write page images <pdf-name>.<page-number>.ppm\n"); | 520 printf("--bin-dir=<path> - override path to v8 external data\n"); |
| 521 printf("--ppm - write page images <pdf-name>.<page-number>.ppm\n"); | |
| 391 #ifdef _WIN32 | 522 #ifdef _WIN32 |
| 392 printf("--bmp write page images <pdf-name>.<page-number>.bmp\n"); | 523 printf("--bmp - write page images <pdf-name>.<page-number>.bmp\n"); |
| 393 printf("--emf write page meta files <pdf-name>.<page-number>.emf\n"); | 524 printf("--emf - write page meta files <pdf-name>.<page-number>.emf\n"); |
| 394 #endif | 525 #endif |
| 395 return 1; | 526 return 1; |
| 396 } | 527 } |
| 397 | 528 |
| 529 v8::V8::InitializeICU(); | |
| 530 | |
| 531 #ifdef V8_USE_EXTERNAL_STARTUP_DATA | |
| 532 v8::StartupData natives; | |
| 533 v8::StartupData snapshot; | |
| 534 if (!GetExternalData(options, "natives_blob.bin", &natives) || | |
| 535 !GetExternalData(options, "snapshot_blob.bin", &snapshot)) { | |
| 536 return 1; | |
| 537 } | |
| 538 v8::V8::SetNativesDataBlob(&natives); | |
| 539 v8::V8::SetSnapshotDataBlob(&snapshot); | |
| 540 #endif // V8_USE_EXTERNAL_STARTUP_DATA | |
| 541 | |
| 398 FPDF_InitLibrary(); | 542 FPDF_InitLibrary(); |
| 399 | 543 |
| 400 UNSUPPORT_INFO unsuppored_info; | 544 UNSUPPORT_INFO unsuppored_info; |
| 401 memset(&unsuppored_info, '\0', sizeof(unsuppored_info)); | 545 memset(&unsuppored_info, '\0', sizeof(unsuppored_info)); |
| 402 unsuppored_info.version = 1; | 546 unsuppored_info.version = 1; |
| 403 unsuppored_info.FSDK_UnSupport_Handler = Unsupported_Handler; | 547 unsuppored_info.FSDK_UnSupport_Handler = Unsupported_Handler; |
| 404 | 548 |
| 405 FSDK_SetUnSpObjProcessHandler(&unsuppored_info); | 549 FSDK_SetUnSpObjProcessHandler(&unsuppored_info); |
| 406 | 550 |
| 407 while (!files.empty()) { | 551 while (!files.empty()) { |
| 408 const char* filename = files.front(); | 552 const char* filename = files.front(); |
| 409 files.pop_front(); | 553 files.pop_front(); |
| 410 FILE* file = fopen(filename, "rb"); | 554 size_t file_length = 0; |
| 411 if (!file) { | 555 char *file_contents = GetFileContents(filename, &file_length); |
| 412 fprintf(stderr, "Failed to open: %s\n", filename); | 556 if (!file_contents) |
| 413 continue; | 557 continue; |
| 414 } | 558 RenderPdf(filename, file_contents, file_length, options.output_format); |
| 415 (void) fseek(file, 0, SEEK_END); | 559 free(file_contents); |
| 416 size_t len = ftell(file); | |
| 417 (void) fseek(file, 0, SEEK_SET); | |
| 418 char* pBuf = (char*) malloc(len); | |
| 419 size_t ret = fread(pBuf, 1, len, file); | |
| 420 (void) fclose(file); | |
| 421 if (ret != len) { | |
| 422 fprintf(stderr, "Failed to read: %s\n", filename); | |
| 423 } else { | |
| 424 RenderPdf(filename, pBuf, len, format); | |
| 425 } | |
| 426 free(pBuf); | |
| 427 } | 560 } |
| 428 | 561 |
| 429 FPDF_DestroyLibrary(); | 562 FPDF_DestroyLibrary(); |
| 430 | 563 |
| 431 return 0; | 564 return 0; |
| 432 } | 565 } |
| OLD | NEW |