OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 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 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #ifndef CORE_INCLUDE_FPDFAPI_FPDF_PARSER_H_ | 7 #ifndef CORE_INCLUDE_FPDFAPI_FPDF_PARSER_H_ |
8 #define CORE_INCLUDE_FPDFAPI_FPDF_PARSER_H_ | 8 #define CORE_INCLUDE_FPDFAPI_FPDF_PARSER_H_ |
9 | 9 |
10 #include <map> | 10 #include <map> |
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 | 321 |
322 CFX_ByteString ReadHexString(); | 322 CFX_ByteString ReadHexString(); |
323 | 323 |
324 unsigned int ReadEOLMarkers(FX_FILESIZE pos); | 324 unsigned int ReadEOLMarkers(FX_FILESIZE pos); |
325 | 325 |
326 CPDF_Stream* ReadStream(CPDF_Dictionary* pDict, | 326 CPDF_Stream* ReadStream(CPDF_Dictionary* pDict, |
327 FX_DWORD objnum, | 327 FX_DWORD objnum, |
328 FX_DWORD gennum); | 328 FX_DWORD gennum); |
329 | 329 |
330 FX_FILESIZE m_Pos; | 330 FX_FILESIZE m_Pos; |
331 | |
332 int m_MetadataObjnum; | 331 int m_MetadataObjnum; |
333 | |
334 IFX_FileRead* m_pFileAccess; | 332 IFX_FileRead* m_pFileAccess; |
335 | |
336 FX_DWORD m_HeaderOffset; | 333 FX_DWORD m_HeaderOffset; |
337 | |
338 FX_FILESIZE m_FileLen; | 334 FX_FILESIZE m_FileLen; |
339 | |
340 uint8_t* m_pFileBuf; | 335 uint8_t* m_pFileBuf; |
341 | |
342 FX_DWORD m_BufSize; | 336 FX_DWORD m_BufSize; |
343 | |
344 FX_FILESIZE m_BufOffset; | 337 FX_FILESIZE m_BufOffset; |
345 | |
346 std::unique_ptr<CPDF_CryptoHandler> m_pCryptoHandler; | 338 std::unique_ptr<CPDF_CryptoHandler> m_pCryptoHandler; |
347 | |
348 uint8_t m_WordBuffer[257]; | 339 uint8_t m_WordBuffer[257]; |
349 | |
350 FX_DWORD m_WordSize; | 340 FX_DWORD m_WordSize; |
351 }; | 341 }; |
352 | 342 |
353 #define PDFPARSE_ERROR_SUCCESS 0 | |
354 #define PDFPARSE_ERROR_FILE 1 | |
355 #define PDFPARSE_ERROR_FORMAT 2 | |
356 #define PDFPARSE_ERROR_PASSWORD 3 | |
357 #define PDFPARSE_ERROR_HANDLER 4 | |
358 #define PDFPARSE_ERROR_CERT 5 | |
359 | |
360 class CPDF_Parser { | 343 class CPDF_Parser { |
361 public: | 344 public: |
| 345 enum Error { |
| 346 SUCCESS = 0, |
| 347 FILE_ERROR, |
| 348 FORMAT_ERROR, |
| 349 PASSWORD_ERROR, |
| 350 HANDLER_ERROR |
| 351 }; |
| 352 |
362 CPDF_Parser(); | 353 CPDF_Parser(); |
363 ~CPDF_Parser(); | 354 ~CPDF_Parser(); |
364 | 355 |
365 FX_DWORD StartParse(IFX_FileRead* pFile, | 356 Error StartParse(IFX_FileRead* pFile, |
366 FX_BOOL bReParse = FALSE, | 357 FX_BOOL bReParse = FALSE, |
367 FX_BOOL bOwnFileRead = TRUE); | 358 FX_BOOL bOwnFileRead = TRUE); |
368 | |
369 void CloseParser(FX_BOOL bReParse = FALSE); | 359 void CloseParser(FX_BOOL bReParse = FALSE); |
370 | 360 |
371 FX_DWORD GetPermissions(FX_BOOL bCheckRevision = FALSE); | 361 FX_DWORD GetPermissions(FX_BOOL bCheckRevision = FALSE); |
372 | |
373 FX_BOOL IsOwner(); | 362 FX_BOOL IsOwner(); |
374 | 363 |
375 void SetPassword(const FX_CHAR* password) { m_Password = password; } | 364 void SetPassword(const FX_CHAR* password) { m_Password = password; } |
376 | |
377 CFX_ByteString GetPassword() { return m_Password; } | 365 CFX_ByteString GetPassword() { return m_Password; } |
378 | 366 |
379 CPDF_CryptoHandler* GetCryptoHandler() { | 367 CPDF_CryptoHandler* GetCryptoHandler() { |
380 return m_Syntax.m_pCryptoHandler.get(); | 368 return m_Syntax.m_pCryptoHandler.get(); |
381 } | 369 } |
382 | 370 |
383 void SetSecurityHandler(CPDF_SecurityHandler* pSecurityHandler, | 371 void SetSecurityHandler(CPDF_SecurityHandler* pSecurityHandler, |
384 FX_BOOL bForced = FALSE); | 372 FX_BOOL bForced = FALSE); |
385 | 373 |
386 CPDF_Dictionary* GetTrailer() { return m_pTrailer; } | 374 CPDF_Dictionary* GetTrailer() const { return m_pTrailer; } |
387 | 375 FX_FILESIZE GetLastXRefOffset() const { return m_LastXRefOffset; } |
388 FX_FILESIZE GetLastXRefOffset() { return m_LastXRefOffset; } | 376 CPDF_Document* GetDocument() const { return m_pDocument; } |
389 | |
390 CPDF_Document* GetDocument() { return m_pDocument; } | |
391 | 377 |
392 FX_DWORD GetRootObjNum(); | 378 FX_DWORD GetRootObjNum(); |
393 FX_DWORD GetInfoObjNum(); | 379 FX_DWORD GetInfoObjNum(); |
394 CPDF_Array* GetIDArray(); | 380 CPDF_Array* GetIDArray(); |
395 | 381 |
396 CPDF_Dictionary* GetEncryptDict() { return m_pEncryptDict; } | 382 CPDF_Dictionary* GetEncryptDict() const { return m_pEncryptDict; } |
397 | 383 |
398 CPDF_Object* ParseIndirectObject(CPDF_IndirectObjectHolder* pObjList, | 384 CPDF_Object* ParseIndirectObject(CPDF_IndirectObjectHolder* pObjList, |
399 FX_DWORD objnum); | 385 FX_DWORD objnum); |
400 | 386 |
401 FX_DWORD GetLastObjNum() const; | 387 FX_DWORD GetLastObjNum() const; |
402 bool IsValidObjectNumber(FX_DWORD objnum) const; | 388 bool IsValidObjectNumber(FX_DWORD objnum) const; |
403 FX_BOOL IsFormStream(FX_DWORD objnum, FX_BOOL& bForm); | 389 FX_BOOL IsFormStream(FX_DWORD objnum, FX_BOOL& bForm); |
404 | 390 |
405 FX_FILESIZE GetObjectOffset(FX_DWORD objnum) const; | 391 FX_FILESIZE GetObjectOffset(FX_DWORD objnum) const; |
406 | |
407 FX_FILESIZE GetObjectSize(FX_DWORD objnum) const; | 392 FX_FILESIZE GetObjectSize(FX_DWORD objnum) const; |
408 | 393 |
409 void GetIndirectBinary(FX_DWORD objnum, uint8_t*& pBuffer, FX_DWORD& size); | 394 void GetIndirectBinary(FX_DWORD objnum, uint8_t*& pBuffer, FX_DWORD& size); |
| 395 IFX_FileRead* GetFileAccess() const { return m_Syntax.m_pFileAccess; } |
| 396 int GetFileVersion() const { return m_FileVersion; } |
| 397 FX_BOOL IsXRefStream() const { return m_bXRefStream; } |
410 | 398 |
411 IFX_FileRead* GetFileAccess() const { return m_Syntax.m_pFileAccess; } | |
412 | |
413 int GetFileVersion() const { return m_FileVersion; } | |
414 | |
415 FX_BOOL IsXRefStream() const { return m_bXRefStream; } | |
416 CPDF_Object* ParseIndirectObjectAt(CPDF_IndirectObjectHolder* pObjList, | 399 CPDF_Object* ParseIndirectObjectAt(CPDF_IndirectObjectHolder* pObjList, |
417 FX_FILESIZE pos, | 400 FX_FILESIZE pos, |
418 FX_DWORD objnum); | 401 FX_DWORD objnum); |
419 | 402 |
420 CPDF_Object* ParseIndirectObjectAtByStrict( | 403 CPDF_Object* ParseIndirectObjectAtByStrict( |
421 CPDF_IndirectObjectHolder* pObjList, | 404 CPDF_IndirectObjectHolder* pObjList, |
422 FX_FILESIZE pos, | 405 FX_FILESIZE pos, |
423 FX_DWORD objnum, | 406 FX_DWORD objnum, |
424 FX_FILESIZE* pResultPos); | 407 FX_FILESIZE* pResultPos); |
425 | 408 |
426 FX_DWORD StartAsynParse(IFX_FileRead* pFile, | 409 Error StartAsynParse(IFX_FileRead* pFile, |
427 FX_BOOL bReParse = FALSE, | 410 FX_BOOL bReParse = FALSE, |
428 FX_BOOL bOwnFileRead = TRUE); | 411 FX_BOOL bOwnFileRead = TRUE); |
429 | 412 |
430 FX_DWORD GetFirstPageNo() { return m_dwFirstPageNo; } | 413 FX_DWORD GetFirstPageNo() const { return m_dwFirstPageNo; } |
431 | 414 |
432 protected: | 415 protected: |
433 CPDF_Object* ParseDirect(CPDF_Object* pObj); | 416 CPDF_Object* ParseDirect(CPDF_Object* pObj); |
434 | |
435 FX_BOOL LoadAllCrossRefV4(FX_FILESIZE pos); | 417 FX_BOOL LoadAllCrossRefV4(FX_FILESIZE pos); |
436 | |
437 FX_BOOL LoadAllCrossRefV5(FX_FILESIZE pos); | 418 FX_BOOL LoadAllCrossRefV5(FX_FILESIZE pos); |
438 | |
439 bool LoadCrossRefV4(FX_FILESIZE pos, FX_FILESIZE streampos, FX_BOOL bSkip); | 419 bool LoadCrossRefV4(FX_FILESIZE pos, FX_FILESIZE streampos, FX_BOOL bSkip); |
440 | |
441 FX_BOOL LoadCrossRefV5(FX_FILESIZE* pos, FX_BOOL bMainXRef); | 420 FX_BOOL LoadCrossRefV5(FX_FILESIZE* pos, FX_BOOL bMainXRef); |
442 | |
443 CPDF_Dictionary* LoadTrailerV4(); | 421 CPDF_Dictionary* LoadTrailerV4(); |
444 | |
445 FX_BOOL RebuildCrossRef(); | 422 FX_BOOL RebuildCrossRef(); |
446 | 423 Error SetEncryptHandler(); |
447 FX_DWORD SetEncryptHandler(); | |
448 | |
449 void ReleaseEncryptHandler(); | 424 void ReleaseEncryptHandler(); |
450 | |
451 FX_BOOL LoadLinearizedAllCrossRefV4(FX_FILESIZE pos, FX_DWORD dwObjCount); | 425 FX_BOOL LoadLinearizedAllCrossRefV4(FX_FILESIZE pos, FX_DWORD dwObjCount); |
452 | |
453 FX_BOOL LoadLinearizedCrossRefV4(FX_FILESIZE pos, FX_DWORD dwObjCount); | 426 FX_BOOL LoadLinearizedCrossRefV4(FX_FILESIZE pos, FX_DWORD dwObjCount); |
454 | |
455 FX_BOOL LoadLinearizedAllCrossRefV5(FX_FILESIZE pos); | 427 FX_BOOL LoadLinearizedAllCrossRefV5(FX_FILESIZE pos); |
456 | 428 Error LoadLinearizedMainXRefTable(); |
457 FX_DWORD LoadLinearizedMainXRefTable(); | |
458 | |
459 CPDF_StreamAcc* GetObjectStream(FX_DWORD number); | 429 CPDF_StreamAcc* GetObjectStream(FX_DWORD number); |
460 | |
461 FX_BOOL IsLinearizedFile(IFX_FileRead* pFileAccess, FX_DWORD offset); | 430 FX_BOOL IsLinearizedFile(IFX_FileRead* pFileAccess, FX_DWORD offset); |
462 | |
463 bool FindPosInOffsets(FX_FILESIZE pos) const; | 431 bool FindPosInOffsets(FX_FILESIZE pos) const; |
464 | |
465 void SetEncryptDictionary(CPDF_Dictionary* pDict); | 432 void SetEncryptDictionary(CPDF_Dictionary* pDict); |
466 | |
467 FX_FILESIZE GetObjectPositionOrZero(FX_DWORD objnum) const; | 433 FX_FILESIZE GetObjectPositionOrZero(FX_DWORD objnum) const; |
468 void ShrinkObjectMap(FX_DWORD size); | 434 void ShrinkObjectMap(FX_DWORD size); |
469 | 435 |
470 CPDF_Document* m_pDocument; | 436 CPDF_Document* m_pDocument; |
471 | |
472 CPDF_SyntaxParser m_Syntax; | 437 CPDF_SyntaxParser m_Syntax; |
473 FX_BOOL m_bOwnFileRead; | 438 FX_BOOL m_bOwnFileRead; |
474 int m_FileVersion; | 439 int m_FileVersion; |
475 | |
476 CPDF_Dictionary* m_pTrailer; | 440 CPDF_Dictionary* m_pTrailer; |
477 | |
478 CPDF_Dictionary* m_pEncryptDict; | 441 CPDF_Dictionary* m_pEncryptDict; |
479 | |
480 FX_FILESIZE m_LastXRefOffset; | 442 FX_FILESIZE m_LastXRefOffset; |
481 | |
482 FX_BOOL m_bXRefStream; | 443 FX_BOOL m_bXRefStream; |
483 | |
484 std::unique_ptr<CPDF_SecurityHandler> m_pSecurityHandler; | 444 std::unique_ptr<CPDF_SecurityHandler> m_pSecurityHandler; |
485 | |
486 FX_BOOL m_bForceUseSecurityHandler; | 445 FX_BOOL m_bForceUseSecurityHandler; |
487 | |
488 CFX_ByteString m_bsRecipient; | 446 CFX_ByteString m_bsRecipient; |
489 | |
490 CFX_ByteString m_FilePath; | 447 CFX_ByteString m_FilePath; |
491 | |
492 CFX_ByteString m_Password; | 448 CFX_ByteString m_Password; |
493 | 449 |
494 struct ObjectInfo { | 450 struct ObjectInfo { |
495 ObjectInfo() : pos(0) {} | 451 ObjectInfo() : pos(0) {} |
496 | 452 |
497 FX_FILESIZE pos; | 453 FX_FILESIZE pos; |
498 // TODO(thestig): Use fields below in place of |m_V5Type| and |m_ObjVersion| | 454 // TODO(thestig): Use fields below in place of |m_V5Type| and |m_ObjVersion| |
499 #if 0 | 455 #if 0 |
500 uint8_t type; | 456 uint8_t type; |
501 uint16_t gennum; | 457 uint16_t gennum; |
502 #endif | 458 #endif |
503 }; | 459 }; |
504 std::map<FX_DWORD, ObjectInfo> m_ObjectInfo; | 460 std::map<FX_DWORD, ObjectInfo> m_ObjectInfo; |
505 | 461 |
506 CFX_ByteArray m_V5Type; | 462 CFX_ByteArray m_V5Type; |
507 CFX_WordArray m_ObjVersion; | 463 CFX_WordArray m_ObjVersion; |
508 | |
509 CFX_FileSizeArray m_SortedOffset; | 464 CFX_FileSizeArray m_SortedOffset; |
510 | |
511 CFX_ArrayTemplate<CPDF_Dictionary*> m_Trailers; | 465 CFX_ArrayTemplate<CPDF_Dictionary*> m_Trailers; |
512 | |
513 FX_BOOL m_bVersionUpdated; | 466 FX_BOOL m_bVersionUpdated; |
514 | |
515 CPDF_Object* m_pLinearized; | 467 CPDF_Object* m_pLinearized; |
516 | |
517 FX_DWORD m_dwFirstPageNo; | 468 FX_DWORD m_dwFirstPageNo; |
518 | |
519 FX_DWORD m_dwXrefStartObjNum; | 469 FX_DWORD m_dwXrefStartObjNum; |
520 | 470 |
521 // A map of object numbers to indirect streams. Map owns the streams. | 471 // A map of object numbers to indirect streams. Map owns the streams. |
522 std::map<FX_DWORD, std::unique_ptr<CPDF_StreamAcc>> m_ObjectStreamMap; | 472 std::map<FX_DWORD, std::unique_ptr<CPDF_StreamAcc>> m_ObjectStreamMap; |
523 | 473 |
524 // Mapping of object numbers to offsets. The offsets are relative to the first | 474 // Mapping of object numbers to offsets. The offsets are relative to the first |
525 // object in the stream. | 475 // object in the stream. |
526 using StreamObjectCache = std::map<FX_DWORD, FX_DWORD>; | 476 using StreamObjectCache = std::map<FX_DWORD, FX_DWORD>; |
527 | 477 |
528 // Mapping of streams to their object caches. This is valid as long as the | 478 // Mapping of streams to their object caches. This is valid as long as the |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
660 FX_BOOL m_bOwner; | 610 FX_BOOL m_bOwner; |
661 | 611 |
662 FX_DWORD m_Permissions; | 612 FX_DWORD m_Permissions; |
663 | 613 |
664 int m_Cipher; | 614 int m_Cipher; |
665 | 615 |
666 uint8_t m_EncryptKey[32]; | 616 uint8_t m_EncryptKey[32]; |
667 | 617 |
668 int m_KeyLen; | 618 int m_KeyLen; |
669 }; | 619 }; |
| 620 |
| 621 CPDF_SecurityHandler* FPDF_CreateStandardSecurityHandler(); |
| 622 |
670 class CPDF_CryptoHandler { | 623 class CPDF_CryptoHandler { |
671 public: | 624 public: |
672 virtual ~CPDF_CryptoHandler() {} | 625 virtual ~CPDF_CryptoHandler() {} |
673 | 626 |
674 virtual FX_BOOL Init(CPDF_Dictionary* pEncryptDict, | 627 virtual FX_BOOL Init(CPDF_Dictionary* pEncryptDict, |
675 CPDF_SecurityHandler* pSecurityHandler) = 0; | 628 CPDF_SecurityHandler* pSecurityHandler) = 0; |
676 | 629 |
677 virtual FX_DWORD DecryptGetSize(FX_DWORD src_size) = 0; | 630 virtual FX_DWORD DecryptGetSize(FX_DWORD src_size) = 0; |
678 | 631 |
679 virtual void* DecryptStart(FX_DWORD objnum, FX_DWORD gennum) = 0; | 632 virtual void* DecryptStart(FX_DWORD objnum, FX_DWORD gennum) = 0; |
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
971 FX_DWORD src_size, | 924 FX_DWORD src_size, |
972 const CPDF_Dictionary* pDict, | 925 const CPDF_Dictionary* pDict, |
973 uint8_t*& dest_buf, | 926 uint8_t*& dest_buf, |
974 FX_DWORD& dest_size, | 927 FX_DWORD& dest_size, |
975 CFX_ByteString& ImageEncoding, | 928 CFX_ByteString& ImageEncoding, |
976 CPDF_Dictionary*& pImageParms, | 929 CPDF_Dictionary*& pImageParms, |
977 FX_DWORD estimated_size, | 930 FX_DWORD estimated_size, |
978 FX_BOOL bImageAcc); | 931 FX_BOOL bImageAcc); |
979 | 932 |
980 #endif // CORE_INCLUDE_FPDFAPI_FPDF_PARSER_H_ | 933 #endif // CORE_INCLUDE_FPDFAPI_FPDF_PARSER_H_ |
OLD | NEW |