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 #include <vector> | |
| 14 | 15 |
| 15 #include "../fpdfsdk/include/fpdf_dataavail.h" | 16 #include "../fpdfsdk/include/fpdf_dataavail.h" |
| 16 #include "../fpdfsdk/include/fpdf_ext.h" | 17 #include "../fpdfsdk/include/fpdf_ext.h" |
| 17 #include "../fpdfsdk/include/fpdfformfill.h" | 18 #include "../fpdfsdk/include/fpdfformfill.h" |
| 18 #include "../fpdfsdk/include/fpdftext.h" | 19 #include "../fpdfsdk/include/fpdftext.h" |
| 19 #include "../fpdfsdk/include/fpdfview.h" | 20 #include "../fpdfsdk/include/fpdfview.h" |
| 20 #include "../core/include/fxcrt/fx_system.h" | 21 #include "../core/include/fxcrt/fx_system.h" |
| 21 #include "v8/include/v8.h" | 22 #include "v8/include/v8.h" |
| 22 | 23 |
| 23 #ifdef _WIN32 | 24 #ifdef _WIN32 |
| 24 #define snprintf _snprintf | 25 #define snprintf _snprintf |
| 26 #define PATH_SEPARATOR '\\' | |
| 27 #else | |
| 28 #define PATH_SEPARATOR '/' | |
| 25 #endif | 29 #endif |
| 26 | 30 |
| 27 enum OutputFormat { | 31 enum OutputFormat { |
| 28 OUTPUT_NONE, | 32 OUTPUT_NONE, |
| 29 OUTPUT_PPM, | 33 OUTPUT_PPM, |
| 30 #ifdef _WIN32 | 34 #ifdef _WIN32 |
| 31 OUTPUT_BMP, | 35 OUTPUT_BMP, |
| 32 OUTPUT_EMF, | 36 OUTPUT_EMF, |
| 33 #endif | 37 #endif |
| 34 }; | 38 }; |
| 35 | 39 |
| 40 struct Options { | |
| 41 Options() : output_format(OUTPUT_NONE) { } | |
| 42 | |
| 43 OutputFormat output_format; | |
| 44 std::string exe_path; | |
| 45 std::string bin_directory; | |
| 46 }; | |
| 47 | |
| 48 // Reads the entire contents of a file into a newly malloc'd buffer. | |
| 49 static char* GetFileContents(const char* filename, size_t* retlen) { | |
| 50 FILE* file = fopen(filename, "rb"); | |
| 51 if (!file) { | |
| 52 fprintf(stderr, "Failed to open: %s\n", filename); | |
| 53 return NULL; | |
| 54 } | |
| 55 (void) fseek(file, 0, SEEK_END); | |
| 56 size_t file_length = ftell(file); | |
| 57 if (!file_length) { | |
| 58 return NULL; | |
| 59 } | |
| 60 (void) fseek(file, 0, SEEK_SET); | |
| 61 char* buffer = (char*) malloc(file_length); | |
| 62 if (!buffer) { | |
| 63 return NULL; | |
| 64 } | |
| 65 size_t bytes_read = fread(buffer, 1, file_length, file); | |
| 66 (void) fclose(file); | |
| 67 if (bytes_read != file_length) { | |
| 68 fprintf(stderr, "Failed to read: %s\n", filename); | |
| 69 free(buffer); | |
| 70 return NULL; | |
| 71 } | |
| 72 *retlen = bytes_read; | |
| 73 return buffer; | |
| 74 } | |
| 75 | |
| 76 #ifdef V8_USE_EXTERNAL_STARTUP_DATA | |
| 77 // Returns the full path for an external V8 data file based on either | |
| 78 // the currect exectuable path or an explicit override. | |
| 79 static std::string GetFullPathForSnapshotFile(const Options& options, | |
| 80 const std::string& filename) { | |
| 81 std::string result; | |
| 82 if (!options.bin_directory.empty()) { | |
| 83 result = options.bin_directory; | |
| 84 if (*options.bin_directory.rbegin() != PATH_SEPARATOR) { | |
| 85 result += PATH_SEPARATOR; | |
| 86 } | |
| 87 } else if (!options.exe_path.empty()) { | |
| 88 size_t last_separator = options.exe_path.rfind(PATH_SEPARATOR); | |
| 89 if (last_separator != std::string::npos) { | |
| 90 result = options.exe_path.substr(0, last_separator + 1); | |
| 91 } | |
| 92 } | |
| 93 result += filename; | |
| 94 return result; | |
| 95 } | |
| 96 | |
| 97 // Reads an extenal V8 data file from the |options|-indicated location, | |
| 98 // returing true on success and false on error. | |
| 99 static bool GetExternalData(const Options& options, | |
| 100 const std::string& bin_filename, | |
| 101 v8::StartupData* result_data) { | |
| 102 std::string full_path = GetFullPathForSnapshotFile(options, bin_filename); | |
| 103 size_t data_length = 0; | |
| 104 char *data_buffer = GetFileContents(full_path.c_str(), &data_length); | |
|
Lei Zhang
2014/12/18 00:07:01
Still got a couple "char *"s in this file.
Tom Sepez
2014/12/18 00:15:59
Done.
| |
| 105 if (!data_buffer) { | |
| 106 return false; | |
| 107 } | |
| 108 result_data->data = const_cast<const char*>(data_buffer); | |
| 109 result_data->raw_size = data_length; | |
| 110 return true; | |
| 111 } | |
| 112 #endif // V8_USE_EXTERNAL_STARTUP_DATA | |
| 113 | |
| 36 static void WritePpm(const char* pdf_name, int num, const void* buffer_void, | 114 static void WritePpm(const char* pdf_name, int num, const void* buffer_void, |
| 37 int stride, int width, int height) { | 115 int stride, int width, int height) { |
| 38 const char* buffer = reinterpret_cast<const char*>(buffer_void); | 116 const char* buffer = reinterpret_cast<const char*>(buffer_void); |
| 39 | 117 |
| 40 if (stride < 0 || width < 0 || height < 0) | 118 if (stride < 0 || width < 0 || height < 0) |
| 41 return; | 119 return; |
| 42 if (height > 0 && width > INT_MAX / height) | 120 if (height > 0 && width > INT_MAX / height) |
| 43 return; | 121 return; |
| 44 int out_len = width * height; | 122 int out_len = width * height; |
| 45 if (out_len > INT_MAX / 3) | 123 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: | 269 case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA: |
| 192 feature = "Screen"; | 270 feature = "Screen"; |
| 193 break; | 271 break; |
| 194 case FPDF_UNSP_ANNOT_SIG: | 272 case FPDF_UNSP_ANNOT_SIG: |
| 195 feature = "Digital_Signature"; | 273 feature = "Digital_Signature"; |
| 196 break; | 274 break; |
| 197 } | 275 } |
| 198 printf("Unsupported feature: %s.\n", feature.c_str()); | 276 printf("Unsupported feature: %s.\n", feature.c_str()); |
| 199 } | 277 } |
| 200 | 278 |
| 201 bool ParseCommandLine(int argc, const char* argv[], OutputFormat* output_format, | 279 bool ParseCommandLine(const std::vector<std::string>& args, |
| 202 std::list<const char*>* files) { | 280 Options* options, std::list<std::string>* files) { |
| 203 *output_format = OUTPUT_NONE; | 281 if (!args.size()) { |
|
Lei Zhang
2014/12/18 00:07:01
!args.size() -> args.empty()
Tom Sepez
2014/12/18 00:15:59
Done.
| |
| 204 files->clear(); | 282 return false; |
| 205 | 283 } |
| 206 int cur_arg = 1; | 284 options->exe_path = args[0]; |
| 207 for (; cur_arg < argc; ++cur_arg) { | 285 int cur_idx = 1; |
| 208 if (strcmp(argv[cur_arg], "--ppm") == 0) | 286 for (; cur_idx < args.size(); ++cur_idx) { |
| 209 *output_format = OUTPUT_PPM; | 287 const std::string& cur_arg = args[cur_idx]; |
| 288 if (cur_arg == "--ppm") { | |
| 289 if (options->output_format != OUTPUT_NONE) { | |
| 290 fprintf(stderr, "Duplicate or conflicting --ppm argument\n"); | |
| 291 return false; | |
| 292 } | |
| 293 options->output_format = OUTPUT_PPM; | |
| 294 } | |
| 210 #ifdef _WIN32 | 295 #ifdef _WIN32 |
| 211 else if (strcmp(argv[cur_arg], "--emf") == 0) | 296 else if (cur_arg == "--emf") { |
| 212 *output_format = OUTPUT_EMF; | 297 if (options->output_format != OUTPUT_NONE) { |
| 213 else if (strcmp(argv[cur_arg], "--bmp") == 0) | 298 fprintf(stderr, "Duplicate or conflicting --emf argument\n"); |
| 214 *output_format = OUTPUT_BMP; | 299 return false; |
| 215 #endif | 300 } |
| 301 options->output_format = OUTPUT_EMF; | |
| 302 } | |
| 303 else if (cur_arg == "--bmp") { | |
| 304 if (options->output_format != OUTPUT_NONE) { | |
| 305 fprintf(stderr, "Duplicate or conflicting --bmp argument\n"); | |
| 306 return false; | |
| 307 } | |
| 308 options->output_format = OUTPUT_BMP; | |
| 309 } | |
| 310 #endif // _WIN32 | |
| 311 #ifdef V8_USE_EXTERNAL_STARTUP_DATA | |
| 312 else if (cur_arg.find("--bin-dir=") == 0) { | |
| 313 if (!options->bin_directory.empty()) { | |
| 314 fprintf(stderr, "Duplicate --bin-dir argument\n"); | |
| 315 return false; | |
| 316 } | |
| 317 options->bin_directory = cur_arg.substr(10); | |
|
Lei Zhang
2014/12/18 00:07:01
If |cur_arg| is "--bin-dir=" then this goes out of
| |
| 318 } | |
| 319 #endif // V8_USE_EXTERNAL_STARTUP_DATA | |
| 216 else | 320 else |
| 217 break; | 321 break; |
| 218 } | 322 } |
| 219 | 323 if (cur_idx >= args.size()) { |
| 220 if (cur_arg > 2) // Multiple options. | 324 fprintf(stderr, "No input files.\n"); |
| 221 return false; | 325 return false; |
| 222 | 326 } |
| 223 if (cur_arg >= argc) // No input files. | 327 for (int i = cur_idx; i < args.size(); i++) { |
| 224 return false; | 328 files->push_back(args[i]); |
| 225 | 329 } |
| 226 for (int i = cur_arg; i < argc; i++) | |
| 227 files->push_back(argv[i]); | |
| 228 | |
| 229 return true; | 330 return true; |
| 230 } | 331 } |
| 231 | 332 |
| 232 class TestLoader { | 333 class TestLoader { |
| 233 public: | 334 public: |
| 234 TestLoader(const char* pBuf, size_t len); | 335 TestLoader(const char* pBuf, size_t len); |
| 235 | 336 |
| 236 const char* m_pBuf; | 337 const char* m_pBuf; |
| 237 size_t m_Len; | 338 size_t m_Len; |
| 238 }; | 339 }; |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 249 return 1; | 350 return 1; |
| 250 } | 351 } |
| 251 | 352 |
| 252 bool Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) { | 353 bool Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) { |
| 253 return true; | 354 return true; |
| 254 } | 355 } |
| 255 | 356 |
| 256 void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) { | 357 void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) { |
| 257 } | 358 } |
| 258 | 359 |
| 259 void RenderPdf(const char* name, const char* pBuf, size_t len, | 360 void RenderPdf(const std::string& name, const char* pBuf, size_t len, |
| 260 OutputFormat format) { | 361 OutputFormat format) { |
| 261 printf("Rendering PDF file %s.\n", name); | 362 printf("Rendering PDF file %s.\n", name.c_str()); |
| 262 | 363 |
| 263 IPDF_JSPLATFORM platform_callbacks; | 364 IPDF_JSPLATFORM platform_callbacks; |
| 264 memset(&platform_callbacks, '\0', sizeof(platform_callbacks)); | 365 memset(&platform_callbacks, '\0', sizeof(platform_callbacks)); |
| 265 platform_callbacks.version = 1; | 366 platform_callbacks.version = 1; |
| 266 platform_callbacks.app_alert = Form_Alert; | 367 platform_callbacks.app_alert = Form_Alert; |
| 267 | 368 |
| 268 FPDF_FORMFILLINFO form_callbacks; | 369 FPDF_FORMFILLINFO form_callbacks; |
| 269 memset(&form_callbacks, '\0', sizeof(form_callbacks)); | 370 memset(&form_callbacks, '\0', sizeof(form_callbacks)); |
| 270 form_callbacks.version = 2; | 371 form_callbacks.version = 2; |
| 271 form_callbacks.m_pJsPlatform = &platform_callbacks; | 372 form_callbacks.m_pJsPlatform = &platform_callbacks; |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 343 rendered_pages ++; | 444 rendered_pages ++; |
| 344 | 445 |
| 345 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0); | 446 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0); |
| 346 int stride = FPDFBitmap_GetStride(bitmap); | 447 int stride = FPDFBitmap_GetStride(bitmap); |
| 347 const char* buffer = | 448 const char* buffer = |
| 348 reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap)); | 449 reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap)); |
| 349 | 450 |
| 350 switch (format) { | 451 switch (format) { |
| 351 #ifdef _WIN32 | 452 #ifdef _WIN32 |
| 352 case OUTPUT_BMP: | 453 case OUTPUT_BMP: |
| 353 WriteBmp(name, i, buffer, stride, width, height); | 454 WriteBmp(name.c_str(), i, buffer, stride, width, height); |
| 354 break; | 455 break; |
| 355 | 456 |
| 356 case OUTPUT_EMF: | 457 case OUTPUT_EMF: |
| 357 WriteEmf(page, name, i); | 458 WriteEmf(page.c_str(), name, i); |
| 358 break; | 459 break; |
| 359 #endif | 460 #endif |
| 360 case OUTPUT_PPM: | 461 case OUTPUT_PPM: |
| 361 WritePpm(name, i, buffer, stride, width, height); | 462 WritePpm(name.c_str(), i, buffer, stride, width, height); |
| 362 break; | 463 break; |
| 363 default: | 464 default: |
| 364 break; | 465 break; |
| 365 } | 466 } |
| 366 | 467 |
| 367 FPDFBitmap_Destroy(bitmap); | 468 FPDFBitmap_Destroy(bitmap); |
| 368 | 469 |
| 369 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE); | 470 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE); |
| 370 FORM_OnBeforeClosePage(page, form); | 471 FORM_OnBeforeClosePage(page, form); |
| 371 FPDFText_ClosePage(text_page); | 472 FPDFText_ClosePage(text_page); |
| 372 FPDF_ClosePage(page); | 473 FPDF_ClosePage(page); |
| 373 } | 474 } |
| 374 | 475 |
| 375 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC); | 476 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC); |
| 376 FPDF_CloseDocument(doc); | 477 FPDF_CloseDocument(doc); |
| 377 FPDFDOC_ExitFormFillEnvironment(form); | 478 FPDFDOC_ExitFormFillEnvironment(form); |
| 378 FPDFAvail_Destroy(pdf_avail); | 479 FPDFAvail_Destroy(pdf_avail); |
| 379 | 480 |
| 380 printf("Loaded, parsed and rendered %" PRIuS " pages.\n", rendered_pages); | 481 printf("Loaded, parsed and rendered %" PRIuS " pages.\n", rendered_pages); |
| 381 printf("Skipped %" PRIuS " bad pages.\n", bad_pages); | 482 printf("Skipped %" PRIuS " bad pages.\n", bad_pages); |
| 382 } | 483 } |
| 383 | 484 |
| 384 int main(int argc, const char* argv[]) { | 485 int main(int argc, const char* argv[]) { |
| 385 v8::V8::InitializeICU(); | 486 std::vector<std::string> args(argv, argv + argc); |
| 386 OutputFormat format = OUTPUT_NONE; | 487 Options options; |
| 387 std::list<const char*> files; | 488 std::list<std::string> files; |
| 388 if (!ParseCommandLine(argc, argv, &format, &files)) { | 489 if (!ParseCommandLine(args, &options, &files)) { |
| 389 printf("Usage: pdfium_test [OPTION] [FILE]...\n"); | 490 printf("Usage: pdfium_test [OPTION] [FILE]...\n"); |
| 390 printf("--ppm write page images <pdf-name>.<page-number>.ppm\n"); | 491 printf("--bin-dir=<path> - override path to v8 external data\n"); |
| 492 printf("--ppm - write page images <pdf-name>.<page-number>.ppm\n"); | |
| 391 #ifdef _WIN32 | 493 #ifdef _WIN32 |
| 392 printf("--bmp write page images <pdf-name>.<page-number>.bmp\n"); | 494 printf("--bmp - write page images <pdf-name>.<page-number>.bmp\n"); |
| 393 printf("--emf write page meta files <pdf-name>.<page-number>.emf\n"); | 495 printf("--emf - write page meta files <pdf-name>.<page-number>.emf\n"); |
| 394 #endif | 496 #endif |
| 395 return 1; | 497 return 1; |
| 396 } | 498 } |
| 397 | 499 |
| 500 v8::V8::InitializeICU(); | |
| 501 | |
| 502 #ifdef V8_USE_EXTERNAL_STARTUP_DATA | |
| 503 v8::StartupData natives; | |
| 504 v8::StartupData snapshot; | |
| 505 if (!GetExternalData(options, "natives_blob.bin", &natives) || | |
| 506 !GetExternalData(options, "snapshot_blob.bin", &snapshot)) { | |
| 507 return 1; | |
| 508 } | |
| 509 v8::V8::SetNativesDataBlob(&natives); | |
| 510 v8::V8::SetSnapshotDataBlob(&snapshot); | |
| 511 #endif // V8_USE_EXTERNAL_STARTUP_DATA | |
| 512 | |
| 398 FPDF_InitLibrary(); | 513 FPDF_InitLibrary(); |
| 399 | 514 |
| 400 UNSUPPORT_INFO unsuppored_info; | 515 UNSUPPORT_INFO unsuppored_info; |
| 401 memset(&unsuppored_info, '\0', sizeof(unsuppored_info)); | 516 memset(&unsuppored_info, '\0', sizeof(unsuppored_info)); |
| 402 unsuppored_info.version = 1; | 517 unsuppored_info.version = 1; |
| 403 unsuppored_info.FSDK_UnSupport_Handler = Unsupported_Handler; | 518 unsuppored_info.FSDK_UnSupport_Handler = Unsupported_Handler; |
| 404 | 519 |
| 405 FSDK_SetUnSpObjProcessHandler(&unsuppored_info); | 520 FSDK_SetUnSpObjProcessHandler(&unsuppored_info); |
| 406 | 521 |
| 407 while (!files.empty()) { | 522 while (!files.empty()) { |
| 408 const char* filename = files.front(); | 523 std::string filename = files.front(); |
| 409 files.pop_front(); | 524 files.pop_front(); |
| 410 FILE* file = fopen(filename, "rb"); | 525 size_t file_length = 0; |
| 411 if (!file) { | 526 char *file_contents = GetFileContents(filename.c_str(), &file_length); |
| 412 fprintf(stderr, "Failed to open: %s\n", filename); | 527 if (!file_contents) |
| 413 continue; | 528 continue; |
| 414 } | 529 RenderPdf(filename, file_contents, file_length, options.output_format); |
| 415 (void) fseek(file, 0, SEEK_END); | 530 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 } | 531 } |
| 428 | 532 |
| 429 FPDF_DestroyLibrary(); | 533 FPDF_DestroyLibrary(); |
| 430 | 534 |
| 431 return 0; | 535 return 0; |
| 432 } | 536 } |
| OLD | NEW |