1 // Copyright 2014 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 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 6 7 #ifndef _FPDF_PARSER_ 8 #define _FPDF_PARSER_ 9 #ifndef _FX_BASIC_H_ 10 #include "../fxcrt/fx_ext.h" 11 #endif 12 #ifndef _FPDF_OBJECTS_ 13 #include "fpdf_objects.h" 14 #endif 15 class CPDF_Document; 16 class IPDF_DocParser; 17 class CPDF_Parser; 18 class CPDF_SecurityHandler; 19 class CPDF_StandardSecurityHandler; 20 class CPDF_CryptoHandler; 21 class CPDF_Object; 22 class IFX_FileRead; 23 class CFDF_Document; 24 class CFDF_Parser; 25 class CFX_Font; 26 class CFX_AffineMatrix; 27 class CFX_FloatRect; 28 class CPDF_Point; 29 class CPDF_DocPageData; 30 class CPDF_DocRenderData; 31 class CPDF_ModuleMgr; 32 class CFX_DIBSource; 33 class CPDF_Font; 34 class CPDF_Image; 35 class CPDF_ColorSpace; 36 class CPDF_Pattern; 37 class CPDF_FontEncoding; 38 class CPDF_IccProfile; 39 class CFX_PrivateData; 40 #define FPDFPERM_PRINT 0x0004 41 #define FPDFPERM_MODIFY 0x0008 42 #define FPDFPERM_EXTRACT 0x0010 43 #define FPDFPERM_ANNOT_FORM 0x0020 44 #define FPDFPERM_FILL_FORM 0x0100 45 #define FPDFPERM_EXTRACT_ACCESS 0x0200 46 #define FPDFPERM_ASSEMBLE 0x0400 47 #define FPDFPERM_PRINT_HIGH 0x0800 48 #define FPDF_PAGE_MAX_NUM 0xFFFFF 49 class IPDF_EnumPageHandler 50 { 51 public: 52 53 virtual FX_BOOL EnumPage(CPDF_Dictionary* pPageDict) = 0; 54 }; 55 class CPDF_Document : public CFX_PrivateData, public CPDF_IndirectObjects 56 { 57 public: 58 59 CPDF_Document(IPDF_DocParser* pParser); 60 61 CPDF_Document(); 62 63 ~CPDF_Document(); 64 GetParser()65 IPDF_DocParser* GetParser() const 66 { 67 return m_pParser; 68 } 69 GetRoot()70 CPDF_Dictionary* GetRoot() const 71 { 72 return m_pRootDict; 73 } 74 GetInfo()75 CPDF_Dictionary* GetInfo() const 76 { 77 return m_pInfoDict; 78 } 79 GetID(CFX_ByteString & id1,CFX_ByteString & id2)80 void GetID(CFX_ByteString& id1, CFX_ByteString& id2) const 81 { 82 id1 = m_ID1; 83 id2 = m_ID2; 84 } 85 86 int GetPageCount() const; 87 88 CPDF_Dictionary* GetPage(int iPage); 89 90 int GetPageIndex(FX_DWORD objnum); 91 92 void EnumPages(IPDF_EnumPageHandler* pHandler); 93 94 FX_DWORD GetUserPermissions(FX_BOOL bCheckRevision = FALSE) const; 95 96 FX_BOOL IsOwner() const; 97 98 99 GetPageData()100 CPDF_DocPageData* GetPageData() 101 { 102 return GetValidatePageData(); 103 } 104 105 void ClearPageData(); 106 107 void RemoveColorSpaceFromPageData(CPDF_Object* pObject); 108 109 GetRenderData()110 CPDF_DocRenderData* GetRenderData() 111 { 112 return GetValidateRenderData(); 113 } 114 115 void ClearRenderData(); 116 117 void ClearRenderFont(); 118 119 120 FX_BOOL IsFormStream(FX_DWORD objnum, FX_BOOL& bForm) const; 121 122 123 124 125 CPDF_Font* LoadFont(CPDF_Dictionary* pFontDict); 126 127 CPDF_Font* FindFont(CPDF_Dictionary* pFontDict); 128 129 CPDF_ColorSpace* LoadColorSpace(CPDF_Object* pCSObj, CPDF_Dictionary* pResources = NULL); 130 131 CPDF_Pattern* LoadPattern(CPDF_Object* pObj, FX_BOOL bShading, const CFX_AffineMatrix* matrix = NULL); 132 133 CPDF_Image* LoadImageF(CPDF_Object* pObj); 134 135 CPDF_StreamAcc* LoadFontFile(CPDF_Stream* pStream); 136 137 CPDF_IccProfile* LoadIccProfile(CPDF_Stream* pStream, int nComponents); 138 139 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ 140 141 CPDF_Font* AddWindowsFont(LOGFONTA* pLogFont, FX_BOOL bVert, FX_BOOL bTranslateName = FALSE); 142 CPDF_Font* AddWindowsFont(LOGFONTW* pLogFont, FX_BOOL bVert, FX_BOOL bTranslateName = FALSE); 143 #endif 144 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ 145 CPDF_Font* AddMacFont(CTFontRef pFont, FX_BOOL bVert, FX_BOOL bTranslateName = FALSE); 146 #endif 147 148 CPDF_Font* AddStandardFont(const FX_CHAR* font, CPDF_FontEncoding* pEncoding); 149 150 151 CPDF_Font* AddFont(CFX_Font* pFont, int charset, FX_BOOL bVert); 152 153 void CreateNewDoc(); 154 155 CPDF_Dictionary* CreateNewPage(int iPage); 156 157 void DeletePage(int iPage); 158 159 void LoadDoc(); 160 void LoadAsynDoc(CPDF_Dictionary *pLinearized); 161 void LoadPages(); 162 protected: 163 164 CPDF_Dictionary* m_pRootDict; 165 166 CPDF_Dictionary* m_pInfoDict; 167 168 CFX_ByteString m_ID1; 169 170 CFX_ByteString m_ID2; 171 172 173 FX_BOOL m_bLinearized; 174 175 FX_DWORD m_dwFirstPageNo; 176 177 FX_DWORD m_dwFirstPageObjNum; 178 179 CFX_DWordArray m_PageList; 180 181 int _GetPageCount() const; 182 CPDF_Dictionary* _FindPDFPage(CPDF_Dictionary* pPages, int iPage, int nPagesToGo, int level); 183 int _FindPageIndex(CPDF_Dictionary* pNode, FX_DWORD& skip_count, FX_DWORD objnum, int& index, int level = 0); 184 FX_BOOL IsContentUsedElsewhere(FX_DWORD objnum, CPDF_Dictionary* pPageDict); 185 FX_BOOL CheckOCGVisible(CPDF_Dictionary* pOCG, FX_BOOL bPrinting); 186 CPDF_DocPageData* GetValidatePageData(); 187 CPDF_DocRenderData* GetValidateRenderData(); 188 friend class CPDF_Creator; 189 friend class CPDF_Parser; 190 friend class CPDF_DataAvail; 191 friend class CPDF_OCContext; 192 193 194 195 CPDF_DocPageData* m_pDocPage; 196 197 CPDF_DocRenderData* m_pDocRender; 198 199 }; 200 201 #define PDFWORD_EOF 0 202 #define PDFWORD_NUMBER 1 203 #define PDFWORD_TEXT 2 204 #define PDFWORD_DELIMITER 3 205 #define PDFWORD_NAME 4 206 class CPDF_SimpleParser : public CFX_Object 207 { 208 public: 209 210 CPDF_SimpleParser(FX_LPCBYTE pData, FX_DWORD dwSize); 211 212 CPDF_SimpleParser(FX_BSTR str); 213 214 CFX_ByteStringC GetWord(); 215 216 FX_BOOL SearchToken(FX_BSTR token); 217 218 FX_BOOL SkipWord(FX_BSTR token); 219 220 FX_BOOL FindTagPair(FX_BSTR start_token, FX_BSTR end_token, 221 FX_DWORD& start_pos, FX_DWORD& end_pos); 222 223 FX_BOOL FindTagParam(FX_BSTR token, int nParams); 224 GetPos()225 FX_DWORD GetPos() 226 { 227 return m_dwCurPos; 228 } 229 SetPos(FX_DWORD pos)230 void SetPos(FX_DWORD pos) 231 { 232 ASSERT(pos <= m_dwSize); 233 m_dwCurPos = pos; 234 } 235 private: 236 237 void ParseWord(FX_LPCBYTE& pStart, FX_DWORD& dwSize, int& type); 238 239 FX_LPCBYTE m_pData; 240 241 FX_DWORD m_dwSize; 242 243 FX_DWORD m_dwCurPos; 244 }; 245 class CPDF_SyntaxParser : public CFX_Object 246 { 247 public: 248 249 CPDF_SyntaxParser(); 250 251 ~CPDF_SyntaxParser(); 252 253 void InitParser(IFX_FileRead* pFileAccess, FX_DWORD HeaderOffset); 254 SavePos()255 FX_FILESIZE SavePos() 256 { 257 return m_Pos; 258 } 259 RestorePos(FX_FILESIZE pos)260 void RestorePos(FX_FILESIZE pos) 261 { 262 m_Pos = pos; 263 } 264 265 CPDF_Object* GetObject(CPDF_IndirectObjects* pObjList, FX_DWORD objnum, FX_DWORD gennum, int level, struct PARSE_CONTEXT* pContext = NULL, FX_BOOL bDecrypt = TRUE); 266 267 268 CPDF_Object* GetObjectByStrict(CPDF_IndirectObjects* pObjList, FX_DWORD objnum, FX_DWORD gennum, int level, struct PARSE_CONTEXT* pContext = NULL); 269 270 int GetDirectNum(); 271 272 CFX_ByteString GetString(FX_DWORD objnum, FX_DWORD gennum); 273 274 CFX_ByteString GetName(); 275 276 CFX_ByteString GetKeyword(); 277 278 void GetBinary(FX_BYTE* buffer, FX_DWORD size); 279 280 void ToNextLine(); 281 282 void ToNextWord(); 283 284 FX_BOOL SearchWord(FX_BSTR word, FX_BOOL bWholeWord, FX_BOOL bForward, FX_FILESIZE limit); 285 286 int SearchMultiWord(FX_BSTR words, FX_BOOL bWholeWord, FX_FILESIZE limit); 287 288 FX_FILESIZE FindTag(FX_BSTR tag, FX_FILESIZE limit); 289 SetEncrypt(CPDF_CryptoHandler * pCryptoHandler)290 void SetEncrypt(CPDF_CryptoHandler* pCryptoHandler) 291 { 292 m_pCryptoHandler = pCryptoHandler; 293 } 294 IsEncrypted()295 FX_BOOL IsEncrypted() 296 { 297 return m_pCryptoHandler != NULL; 298 } 299 300 FX_BOOL GetCharAt(FX_FILESIZE pos, FX_BYTE& ch); 301 302 FX_BOOL ReadBlock(FX_BYTE* pBuf, FX_DWORD size); 303 304 CFX_ByteString GetNextWord(FX_BOOL& bIsNumber); 305 protected: 306 307 virtual FX_BOOL GetNextChar(FX_BYTE& ch); 308 309 FX_BOOL GetCharAtBackward(FX_FILESIZE pos, FX_BYTE& ch); 310 311 void GetNextWord(); 312 313 FX_BOOL IsWholeWord(FX_FILESIZE startpos, FX_FILESIZE limit, FX_LPCBYTE tag, FX_DWORD taglen); 314 315 CFX_ByteString ReadString(); 316 317 CFX_ByteString ReadHexString(); 318 319 CPDF_Stream* ReadStream(CPDF_Dictionary* pDict, PARSE_CONTEXT* pContext, FX_DWORD objnum, FX_DWORD gennum); 320 321 FX_FILESIZE m_Pos; 322 323 FX_BOOL m_bFileStream; 324 325 int m_MetadataObjnum; 326 327 IFX_FileRead* m_pFileAccess; 328 329 FX_DWORD m_HeaderOffset; 330 331 FX_FILESIZE m_FileLen; 332 333 FX_BYTE* m_pFileBuf; 334 335 FX_DWORD m_BufSize; 336 337 FX_FILESIZE m_BufOffset; 338 339 CPDF_CryptoHandler* m_pCryptoHandler; 340 341 FX_BYTE m_WordBuffer[257]; 342 343 FX_DWORD m_WordSize; 344 345 FX_BOOL m_bIsNumber; 346 347 FX_FILESIZE m_dwWordPos; 348 friend class CPDF_Parser; 349 friend class CPDF_DataAvail; 350 }; 351 352 #define PDFPARSE_TYPEONLY 1 353 #define PDFPARSE_NOSTREAM 2 354 struct PARSE_CONTEXT { 355 356 FX_BOOL m_Flags; 357 358 FX_FILESIZE m_DictStart; 359 360 FX_FILESIZE m_DictEnd; 361 362 FX_FILESIZE m_DataStart; 363 364 FX_FILESIZE m_DataEnd; 365 }; 366 class IPDF_DocParser : public CFX_Object 367 { 368 public: 369 370 virtual FX_DWORD GetRootObjNum() = 0; 371 372 virtual FX_DWORD GetInfoObjNum() = 0; 373 374 virtual CPDF_Object* ParseIndirectObject(CPDF_IndirectObjects* pObjList, FX_DWORD objnum, PARSE_CONTEXT* pContext = NULL) = 0; 375 376 virtual FX_DWORD GetLastObjNum() = 0; 377 378 virtual CPDF_Array* GetIDArray() = 0; 379 380 virtual CPDF_Dictionary* GetEncryptDict() = 0; 381 IsEncrypted()382 FX_BOOL IsEncrypted() 383 { 384 return GetEncryptDict() != NULL; 385 } 386 387 virtual FX_DWORD GetPermissions(FX_BOOL bCheckRevision = FALSE) = 0; 388 389 virtual FX_BOOL IsOwner() = 0; 390 391 virtual FX_BOOL IsFormStream(FX_DWORD objnum, FX_BOOL& bForm) = 0; 392 }; 393 394 #define PDFPARSE_ERROR_SUCCESS 0 395 #define PDFPARSE_ERROR_FILE 1 396 #define PDFPARSE_ERROR_FORMAT 2 397 #define PDFPARSE_ERROR_PASSWORD 3 398 #define PDFPARSE_ERROR_HANDLER 4 399 #define PDFPARSE_ERROR_CERT 5 400 class CPDF_Parser : public IPDF_DocParser 401 { 402 public: 403 404 CPDF_Parser(); 405 406 ~CPDF_Parser(); 407 408 FX_DWORD StartParse(FX_LPCSTR filename, FX_BOOL bReParse = FALSE); 409 410 FX_DWORD StartParse(FX_LPCWSTR filename, FX_BOOL bReParse = FALSE); 411 412 FX_DWORD StartParse(IFX_FileRead* pFile, FX_BOOL bReParse = FALSE, FX_BOOL bOwnFileRead = TRUE); 413 414 void CloseParser(FX_BOOL bReParse = FALSE); 415 416 virtual FX_DWORD GetPermissions(FX_BOOL bCheckRevision = FALSE); 417 418 virtual FX_BOOL IsOwner(); 419 SetPassword(const FX_CHAR * password)420 void SetPassword(const FX_CHAR* password) 421 { 422 m_Password = password; 423 } 424 GetPassword()425 CFX_ByteString GetPassword() 426 { 427 return m_Password; 428 } 429 GetSecurityHandler()430 CPDF_SecurityHandler* GetSecurityHandler() 431 { 432 return m_pSecurityHandler; 433 } 434 GetCryptoHandler()435 CPDF_CryptoHandler* GetCryptoHandler() 436 { 437 return m_Syntax.m_pCryptoHandler; 438 } 439 440 void SetSecurityHandler(CPDF_SecurityHandler* pSecurityHandler, FX_BOOL bForced = FALSE); 441 GetRecipient()442 CFX_ByteString GetRecipient() 443 { 444 return m_bsRecipient; 445 } 446 GetTrailer()447 CPDF_Dictionary* GetTrailer() 448 { 449 return m_pTrailer; 450 } 451 GetLastXRefOffset()452 FX_FILESIZE GetLastXRefOffset() 453 { 454 return m_LastXRefOffset; 455 } 456 GetDocument()457 CPDF_Document* GetDocument() 458 { 459 return m_pDocument; 460 } GetOtherTrailers()461 CFX_ArrayTemplate<CPDF_Dictionary *> * GetOtherTrailers() 462 { 463 return &m_Trailers; 464 } 465 466 virtual FX_DWORD GetRootObjNum(); 467 virtual FX_DWORD GetInfoObjNum(); 468 virtual CPDF_Array* GetIDArray(); GetEncryptDict()469 virtual CPDF_Dictionary* GetEncryptDict() 470 { 471 return m_pEncryptDict; 472 } 473 virtual CPDF_Object* ParseIndirectObject(CPDF_IndirectObjects* pObjList, FX_DWORD objnum, PARSE_CONTEXT* pContext = NULL); 474 virtual FX_DWORD GetLastObjNum(); 475 virtual FX_BOOL IsFormStream(FX_DWORD objnum, FX_BOOL& bForm); 476 477 FX_FILESIZE GetObjectOffset(FX_DWORD objnum); 478 479 FX_FILESIZE GetObjectSize(FX_DWORD objnum); 480 GetObjectVersion(FX_DWORD objnum)481 int GetObjectVersion(FX_DWORD objnum) 482 { 483 return m_ObjVersion[objnum]; 484 } 485 486 void GetIndirectBinary(FX_DWORD objnum, FX_BYTE*& pBuffer, FX_DWORD& size); 487 GetFileStreamOption()488 FX_BOOL GetFileStreamOption() 489 { 490 return m_Syntax.m_bFileStream; 491 } 492 SetFileStreamOption(FX_BOOL b)493 void SetFileStreamOption(FX_BOOL b) 494 { 495 m_Syntax.m_bFileStream = b; 496 } 497 GetFileAccess()498 IFX_FileRead* GetFileAccess() const 499 { 500 return m_Syntax.m_pFileAccess; 501 } 502 GetFileVersion()503 int GetFileVersion() const 504 { 505 return m_FileVersion; 506 } 507 IsXRefStream()508 FX_BOOL IsXRefStream() const 509 { 510 return m_bXRefStream; 511 } 512 CPDF_Object* ParseIndirectObjectAt(CPDF_IndirectObjects* pObjList, FX_FILESIZE pos, FX_DWORD objnum, 513 struct PARSE_CONTEXT* pContext); 514 515 CPDF_Object* ParseIndirectObjectAtByStrict(CPDF_IndirectObjects* pObjList, FX_FILESIZE pos, FX_DWORD objnum, 516 struct PARSE_CONTEXT* pContext, FX_FILESIZE *pResultPos); 517 518 FX_DWORD StartAsynParse(IFX_FileRead* pFile, FX_BOOL bReParse = FALSE, FX_BOOL bOwnFileRead = TRUE); 519 GetFirstPageNo()520 FX_DWORD GetFirstPageNo() 521 { 522 return m_dwFirstPageNo; 523 } 524 protected: 525 526 CPDF_Document* m_pDocument; 527 528 CPDF_SyntaxParser m_Syntax; 529 FX_BOOL m_bOwnFileRead; 530 CPDF_Object* ParseDirect(CPDF_Object* pObj); 531 532 FX_BOOL LoadAllCrossRefV4(FX_FILESIZE pos); 533 534 FX_BOOL LoadAllCrossRefV5(FX_FILESIZE pos); 535 536 FX_BOOL LoadCrossRefV4(FX_FILESIZE pos, FX_FILESIZE streampos, FX_BOOL bSkip, FX_BOOL bFirst); 537 538 FX_BOOL LoadCrossRefV5(FX_FILESIZE pos, FX_FILESIZE& prev, FX_BOOL bMainXRef); 539 540 CPDF_Dictionary* LoadTrailerV4(); 541 542 FX_BOOL RebuildCrossRef(); 543 544 FX_DWORD SetEncryptHandler(); 545 546 void ReleaseEncryptHandler(); 547 548 FX_BOOL LoadLinearizedAllCrossRefV4(FX_FILESIZE pos, FX_DWORD dwObjCount); 549 550 FX_BOOL LoadLinearizedCrossRefV4(FX_FILESIZE pos, FX_DWORD dwObjCount); 551 552 FX_BOOL LoadLinearizedAllCrossRefV5(FX_FILESIZE pos); 553 554 FX_DWORD LoadLinearizedMainXRefTable(); 555 556 CFX_MapPtrToPtr m_ObjectStreamMap; 557 558 CPDF_StreamAcc* GetObjectStream(FX_DWORD number); 559 560 FX_BOOL IsLinearizedFile(IFX_FileRead* pFileAccess, FX_DWORD offset); 561 562 563 564 int m_FileVersion; 565 566 CPDF_Dictionary* m_pTrailer; 567 568 CPDF_Dictionary* m_pEncryptDict; 569 void SetEncryptDictionary(CPDF_Dictionary* pDict); 570 571 FX_FILESIZE m_LastXRefOffset; 572 573 FX_BOOL m_bXRefStream; 574 575 576 CPDF_SecurityHandler* m_pSecurityHandler; 577 578 FX_BOOL m_bForceUseSecurityHandler; 579 580 CFX_ByteString m_bsRecipient; 581 582 CFX_ByteString m_FilePath; 583 584 CFX_ByteString m_Password; 585 586 CFX_FileSizeArray m_CrossRef; 587 588 CFX_ByteArray m_V5Type; 589 590 CFX_FileSizeArray m_SortedOffset; 591 592 CFX_WordArray m_ObjVersion; 593 CFX_ArrayTemplate<CPDF_Dictionary *> m_Trailers; 594 595 FX_BOOL m_bVersionUpdated; 596 597 CPDF_Object* m_pLinearized; 598 599 FX_DWORD m_dwFirstPageNo; 600 601 FX_DWORD m_dwXrefStartObjNum; 602 friend class CPDF_Creator; 603 friend class CPDF_DataAvail; 604 }; 605 #define FXCIPHER_NONE 0 606 #define FXCIPHER_RC4 1 607 #define FXCIPHER_AES 2 608 #define FXCIPHER_AES2 3 609 class CPDF_SecurityHandler : public CFX_Object 610 { 611 public: 612 ~CPDF_SecurityHandler()613 virtual ~CPDF_SecurityHandler() {} 614 615 virtual FX_BOOL OnInit(CPDF_Parser* pParser, CPDF_Dictionary* pEncryptDict) = 0; 616 617 virtual FX_DWORD GetPermissions() = 0; 618 619 virtual FX_BOOL IsOwner() = 0; 620 621 virtual FX_BOOL GetCryptInfo(int& cipher, FX_LPCBYTE& buffer, int& keylen) = 0; 622 IsMetadataEncrypted()623 virtual FX_BOOL IsMetadataEncrypted() 624 { 625 return TRUE; 626 } 627 628 virtual CPDF_CryptoHandler* CreateCryptoHandler() = 0; 629 GetStandardHandler()630 virtual CPDF_StandardSecurityHandler* GetStandardHandler() 631 { 632 return NULL; 633 } 634 }; 635 #define PDF_ENCRYPT_CONTENT 0 636 class CPDF_StandardSecurityHandler : public CPDF_SecurityHandler 637 { 638 public: 639 CPDF_StandardSecurityHandler(); 640 641 virtual ~CPDF_StandardSecurityHandler(); 642 virtual FX_BOOL OnInit(CPDF_Parser* pParser, CPDF_Dictionary* pEncryptDict); 643 virtual FX_DWORD GetPermissions(); IsOwner()644 virtual FX_BOOL IsOwner() 645 { 646 return m_bOwner; 647 } 648 virtual FX_BOOL GetCryptInfo(int& cipher, FX_LPCBYTE& buffer, int& keylen); 649 virtual FX_BOOL IsMetadataEncrypted(); 650 virtual CPDF_CryptoHandler* CreateCryptoHandler(); GetStandardHandler()651 virtual CPDF_StandardSecurityHandler* GetStandardHandler() 652 { 653 return this; 654 } 655 656 void OnCreate(CPDF_Dictionary* pEncryptDict, CPDF_Array* pIdArray, 657 FX_LPCBYTE user_pass, FX_DWORD user_size, 658 FX_LPCBYTE owner_pass, FX_DWORD owner_size, FX_DWORD type = PDF_ENCRYPT_CONTENT); 659 660 void OnCreate(CPDF_Dictionary* pEncryptDict, CPDF_Array* pIdArray, 661 FX_LPCBYTE user_pass, FX_DWORD user_size, FX_DWORD type = PDF_ENCRYPT_CONTENT); 662 663 CFX_ByteString GetUserPassword(FX_LPCBYTE owner_pass, FX_DWORD pass_size); 664 CFX_ByteString GetUserPassword(FX_LPCBYTE owner_pass, FX_DWORD pass_size, FX_INT32 key_len); GetVersion()665 int GetVersion() 666 { 667 return m_Version; 668 } GetRevision()669 int GetRevision() 670 { 671 return m_Revision; 672 } 673 674 int CheckPassword(FX_LPCBYTE password, FX_DWORD pass_size, FX_BOOL bOwner, FX_LPBYTE key); 675 int CheckPassword(FX_LPCBYTE password, FX_DWORD pass_size, FX_BOOL bOwner, FX_LPBYTE key, int key_len); 676 private: 677 678 int m_Version; 679 680 int m_Revision; 681 682 CPDF_Parser* m_pParser; 683 684 CPDF_Dictionary* m_pEncryptDict; 685 686 FX_BOOL LoadDict(CPDF_Dictionary* pEncryptDict); 687 FX_BOOL LoadDict(CPDF_Dictionary* pEncryptDict, FX_DWORD type, int& cipher, int& key_len); 688 689 FX_BOOL CheckUserPassword(FX_LPCBYTE password, FX_DWORD pass_size, 690 FX_BOOL bIgnoreEncryptMeta, FX_LPBYTE key, FX_INT32 key_len); 691 692 FX_BOOL CheckOwnerPassword(FX_LPCBYTE password, FX_DWORD pass_size, FX_LPBYTE key, FX_INT32 key_len); 693 FX_BOOL AES256_CheckPassword(FX_LPCBYTE password, FX_DWORD size, FX_BOOL bOwner, FX_LPBYTE key); 694 void AES256_SetPassword(CPDF_Dictionary* pEncryptDict, FX_LPCBYTE password, FX_DWORD size, FX_BOOL bOwner, FX_LPCBYTE key); 695 void AES256_SetPerms(CPDF_Dictionary* pEncryptDict, FX_DWORD permission, FX_BOOL bEncryptMetadata, FX_LPCBYTE key); 696 void OnCreate(CPDF_Dictionary* pEncryptDict, CPDF_Array* pIdArray, 697 FX_LPCBYTE user_pass, FX_DWORD user_size, 698 FX_LPCBYTE owner_pass, FX_DWORD owner_size, FX_BOOL bDefault, FX_DWORD type); 699 FX_BOOL CheckSecurity(FX_INT32 key_len); 700 701 FX_BOOL m_bOwner; 702 703 FX_DWORD m_Permissions; 704 705 int m_Cipher; 706 707 FX_BYTE m_EncryptKey[32]; 708 709 int m_KeyLen; 710 }; 711 class CPDF_CryptoHandler : public CFX_Object 712 { 713 public: 714 ~CPDF_CryptoHandler()715 virtual ~CPDF_CryptoHandler() {} 716 717 virtual FX_BOOL Init(CPDF_Dictionary* pEncryptDict, CPDF_SecurityHandler* pSecurityHandler) = 0; 718 719 virtual FX_DWORD DecryptGetSize(FX_DWORD src_size) = 0; 720 721 virtual FX_LPVOID DecryptStart(FX_DWORD objnum, FX_DWORD gennum) = 0; 722 723 virtual FX_BOOL DecryptStream(FX_LPVOID context, FX_LPCBYTE src_buf, FX_DWORD src_size, CFX_BinaryBuf& dest_buf) = 0; 724 725 virtual FX_BOOL DecryptFinish(FX_LPVOID context, CFX_BinaryBuf& dest_buf) = 0; 726 727 728 virtual FX_DWORD EncryptGetSize(FX_DWORD objnum, FX_DWORD version, FX_LPCBYTE src_buf, FX_DWORD src_size) = 0; 729 730 virtual FX_BOOL EncryptContent(FX_DWORD objnum, FX_DWORD version, FX_LPCBYTE src_buf, FX_DWORD src_size, 731 FX_LPBYTE dest_buf, FX_DWORD& dest_size) = 0; 732 733 void Decrypt(FX_DWORD objnum, FX_DWORD version, CFX_ByteString& str); 734 }; 735 class CPDF_StandardCryptoHandler : public CPDF_CryptoHandler 736 { 737 public: 738 739 CPDF_StandardCryptoHandler(); 740 741 virtual ~CPDF_StandardCryptoHandler(); 742 743 FX_BOOL Init(int cipher, FX_LPCBYTE key, int keylen); 744 virtual FX_BOOL Init(CPDF_Dictionary* pEncryptDict, CPDF_SecurityHandler* pSecurityHandler); 745 virtual FX_DWORD DecryptGetSize(FX_DWORD src_size); 746 virtual FX_LPVOID DecryptStart(FX_DWORD objnum, FX_DWORD gennum); 747 virtual FX_BOOL DecryptStream(FX_LPVOID context, FX_LPCBYTE src_buf, FX_DWORD src_size, CFX_BinaryBuf& dest_buf); 748 virtual FX_BOOL DecryptFinish(FX_LPVOID context, CFX_BinaryBuf& dest_buf); 749 virtual FX_DWORD EncryptGetSize(FX_DWORD objnum, FX_DWORD version, FX_LPCBYTE src_buf, FX_DWORD src_size); 750 virtual FX_BOOL EncryptContent(FX_DWORD objnum, FX_DWORD version, FX_LPCBYTE src_buf, FX_DWORD src_size, 751 FX_LPBYTE dest_buf, FX_DWORD& dest_size); 752 protected: 753 754 virtual void CryptBlock(FX_BOOL bEncrypt, FX_DWORD objnum, FX_DWORD gennum, FX_LPCBYTE src_buf, FX_DWORD src_size, 755 FX_LPBYTE dest_buf, FX_DWORD& dest_size); 756 virtual FX_LPVOID CryptStart(FX_DWORD objnum, FX_DWORD gennum, FX_BOOL bEncrypt); 757 virtual FX_BOOL CryptStream(FX_LPVOID context, FX_LPCBYTE src_buf, FX_DWORD src_size, CFX_BinaryBuf& dest_buf, FX_BOOL bEncrypt); 758 virtual FX_BOOL CryptFinish(FX_LPVOID context, CFX_BinaryBuf& dest_buf, FX_BOOL bEncrypt); 759 760 FX_BYTE m_EncryptKey[32]; 761 762 int m_KeyLen; 763 764 int m_Cipher; 765 766 FX_LPBYTE m_pAESContext; 767 }; 768 class CPDF_Point : public CFX_Object 769 { 770 public: 771 CPDF_Point(FX_FLOAT xx,FX_FLOAT yy)772 CPDF_Point(FX_FLOAT xx, FX_FLOAT yy) 773 { 774 x = xx; 775 y = yy; 776 } 777 778 FX_FLOAT x; 779 780 FX_FLOAT y; 781 }; 782 783 #define CPDF_Rect CFX_FloatRect 784 #define CPDF_Matrix CFX_AffineMatrix 785 CFX_ByteString PDF_NameDecode(FX_BSTR orig); 786 CFX_ByteString PDF_NameDecode(const CFX_ByteString& orig); 787 CFX_ByteString PDF_NameEncode(const CFX_ByteString& orig); 788 CFX_ByteString PDF_EncodeString(const CFX_ByteString& src, FX_BOOL bHex = FALSE); 789 CFX_WideString PDF_DecodeText(const CFX_ByteString& str, CFX_CharMap* pCharMap = NULL); 790 CFX_WideString PDF_DecodeText(FX_LPCBYTE pData, FX_DWORD size, CFX_CharMap* pCharMap = NULL); 791 CFX_ByteString PDF_EncodeText(FX_LPCWSTR pString, int len = -1, CFX_CharMap* pCharMap = NULL); 792 FX_FLOAT PDF_ClipFloat(FX_FLOAT f); 793 class CFDF_Document : public CPDF_IndirectObjects 794 { 795 public: 796 797 static CFDF_Document* CreateNewDoc(); 798 799 static CFDF_Document* ParseFile(FX_LPCSTR file_path); 800 801 static CFDF_Document* ParseFile(FX_LPCWSTR file_path); 802 803 static CFDF_Document* ParseFile(IFX_FileRead *pFile, FX_BOOL bOwnFile = FALSE); 804 805 static CFDF_Document* ParseMemory(FX_LPCBYTE pData, FX_DWORD size); 806 807 ~CFDF_Document(); 808 809 FX_BOOL WriteFile(FX_LPCSTR file_path) const; 810 811 FX_BOOL WriteFile(FX_LPCWSTR file_path) const; 812 813 FX_BOOL WriteFile(IFX_FileWrite *pFile) const; 814 815 FX_BOOL WriteBuf(CFX_ByteTextBuf& buf) const; 816 GetRoot()817 CPDF_Dictionary* GetRoot() const 818 { 819 return m_pRootDict; 820 } 821 822 CFX_WideString GetWin32Path() const; 823 protected: 824 825 CFDF_Document(); 826 void ParseStream(IFX_FileRead *pFile, FX_BOOL bOwnFile); 827 CPDF_Dictionary* m_pRootDict; 828 IFX_FileRead* m_pFile; 829 FX_BOOL m_bOwnFile; 830 }; 831 832 CFX_WideString FPDF_FileSpec_GetWin32Path(const CPDF_Object* pFileSpec); 833 void FPDF_FileSpec_SetWin32Path(CPDF_Object* pFileSpec, const CFX_WideString& fullpath); 834 835 void FlateEncode(const FX_BYTE* src_buf, FX_DWORD src_size, FX_LPBYTE& dest_buf, FX_DWORD& dest_size); 836 FX_DWORD FlateDecode(const FX_BYTE* src_buf, FX_DWORD src_size, FX_LPBYTE& dest_buf, FX_DWORD& dest_size); 837 FX_DWORD RunLengthDecode(const FX_BYTE* src_buf, FX_DWORD src_size, FX_LPBYTE& dest_buf, FX_DWORD& dest_size); 838 class CPDF_NumberTree : public CFX_Object 839 { 840 public: 841 CPDF_NumberTree(CPDF_Dictionary * pRoot)842 CPDF_NumberTree(CPDF_Dictionary* pRoot) 843 { 844 m_pRoot = pRoot; 845 } 846 847 CPDF_Object* LookupValue(int num); 848 protected: 849 850 CPDF_Dictionary* m_pRoot; 851 }; 852 853 class IFX_FileAvail 854 { 855 public: 856 857 virtual FX_BOOL IsDataAvail( FX_FILESIZE offset, FX_DWORD size) = 0; 858 }; 859 class IFX_DownloadHints 860 { 861 public: 862 863 virtual void AddSegment(FX_FILESIZE offset, FX_DWORD size) = 0; 864 }; 865 #define PDF_IS_LINEARIZED 1 866 #define PDF_NOT_LINEARIZED 0 867 #define PDF_UNKNOW_LINEARIZED -1 868 #define PDFFORM_NOTAVAIL 0 869 #define PDFFORM_AVAIL 1 870 #define PDFFORM_NOTEXIST 2 871 class IPDF_DataAvail 872 { 873 public: 874 875 virtual FX_BOOL IsDocAvail(IFX_DownloadHints* pHints) = 0; 876 877 878 virtual void SetDocument(CPDF_Document* pDoc) = 0; 879 880 881 virtual FX_BOOL IsPageAvail(int iPage, IFX_DownloadHints* pHints) = 0; 882 883 virtual FX_BOOL IsLinearized() = 0; 884 885 virtual FX_INT32 IsFormAvail(IFX_DownloadHints *pHints) = 0; 886 887 virtual FX_INT32 IsLinearizedPDF() = 0; 888 889 virtual void GetLinearizedMainXRefInfo(FX_FILESIZE *pPos, FX_DWORD *pSize) = 0; 890 }; 891 class CPDF_SortObjNumArray : public CFX_Object 892 { 893 public: 894 895 void AddObjNum(FX_DWORD dwObjNum); 896 897 FX_BOOL Find(FX_DWORD dwObjNum); 898 RemoveAll()899 void RemoveAll() 900 { 901 m_number_array.RemoveAll(); 902 } 903 protected: 904 905 FX_BOOL BinarySearch(FX_DWORD value, int &iNext); 906 protected: 907 908 CFX_DWordArray m_number_array; 909 }; 910 enum PDF_PAGENODE_TYPE { 911 PDF_PAGENODE_UNKOWN = 0, 912 PDF_PAGENODE_PAGE, 913 PDF_PAGENODE_PAGES, 914 PDF_PAGENODE_ARRAY, 915 }; 916 class CPDF_PageNode : public CFX_Object 917 { 918 public: CPDF_PageNode()919 CPDF_PageNode() : m_type(PDF_PAGENODE_UNKOWN) {} 920 ~CPDF_PageNode(); 921 PDF_PAGENODE_TYPE m_type; 922 FX_DWORD m_dwPageNo; 923 CFX_PtrArray m_childNode; 924 }; 925 enum PDF_DATAAVAIL_STATUS { 926 PDF_DATAAVAIL_HEADER = 0, 927 PDF_DATAAVAIL_FIRSTPAGE, 928 PDF_DATAAVAIL_FIRSTPAGE_PREPARE, 929 PDF_DATAAVAIL_END, 930 PDF_DATAAVAIL_CROSSREF, 931 PDF_DATAAVAIL_CROSSREF_ITEM, 932 PDF_DATAAVAIL_CROSSREF_STREAM, 933 PDF_DATAAVAIL_TRAILER, 934 PDF_DATAAVAIL_LOADALLCRSOSSREF, 935 PDF_DATAAVAIL_ROOT, 936 PDF_DATAAVAIL_INFO, 937 PDF_DATAAVAIL_ACROFORM, 938 PDF_DATAAVAIL_ACROFORM_SUBOBJECT, 939 PDF_DATAAVAIL_PAGETREE, 940 PDF_DATAAVAIL_PAGE, 941 PDF_DATAAVAIL_PAGE_LATERLOAD, 942 PDF_DATAAVAIL_RESOURCES, 943 PDF_DATAAVAIL_DONE, 944 PDF_DATAAVAIL_ERROR, 945 PDF_DATAAVAIL_LOADALLFILE, 946 PDF_DATAAVAIL_TRAILER_APPEND 947 }; 948 class CPDF_DataAvail : public CFX_Object, public IPDF_DataAvail 949 { 950 public: 951 952 CPDF_DataAvail(IFX_FileAvail* pFileAvail, IFX_FileRead* pFileRead); 953 ~CPDF_DataAvail(); 954 955 virtual FX_BOOL IsDocAvail(IFX_DownloadHints* pHints); 956 957 958 virtual void SetDocument(CPDF_Document* pDoc); 959 960 961 virtual FX_BOOL IsPageAvail(int iPage, IFX_DownloadHints* pHints); 962 963 virtual FX_INT32 IsFormAvail(IFX_DownloadHints *pHints); 964 965 virtual FX_INT32 IsLinearizedPDF(); 966 IsLinearized()967 virtual FX_BOOL IsLinearized() 968 { 969 return m_bLinearized; 970 } 971 972 virtual void GetLinearizedMainXRefInfo(FX_FILESIZE *pPos, FX_DWORD *pSize); GetFileRead()973 IFX_FileRead* GetFileRead() const 974 { 975 return m_pFileRead; 976 } GetFileAvail()977 IFX_FileAvail* GetFileAvail() const 978 { 979 return m_pFileAvail; 980 } 981 protected: 982 FX_DWORD GetObjectSize(FX_DWORD objnum, FX_FILESIZE& offset); 983 FX_BOOL IsObjectsAvail(CFX_PtrArray& obj_array, FX_BOOL bParsePage, IFX_DownloadHints* pHints, CFX_PtrArray &ret_array); 984 FX_BOOL CheckDocStatus(IFX_DownloadHints *pHints); 985 FX_BOOL CheckHeader(IFX_DownloadHints* pHints); 986 FX_BOOL CheckFirstPage(IFX_DownloadHints *pHints); 987 FX_BOOL CheckEnd(IFX_DownloadHints *pHints); 988 FX_BOOL CheckCrossRef(IFX_DownloadHints* pHints); 989 FX_BOOL CheckCrossRefItem(IFX_DownloadHints *pHints); 990 FX_BOOL CheckTrailer(IFX_DownloadHints* pHints); 991 FX_BOOL CheckRoot(IFX_DownloadHints* pHints); 992 FX_BOOL CheckInfo(IFX_DownloadHints* pHints); 993 FX_BOOL CheckPages(IFX_DownloadHints* pHints); 994 FX_BOOL CheckPage(IFX_DownloadHints* pHints); 995 FX_BOOL CheckResources(IFX_DownloadHints* pHints); 996 FX_BOOL CheckAnnots(IFX_DownloadHints* pHints); 997 FX_BOOL CheckAcroForm(IFX_DownloadHints* pHints); 998 FX_BOOL CheckAcroFormSubObject(IFX_DownloadHints* pHints); 999 FX_BOOL CheckTrailerAppend(IFX_DownloadHints* pHints); 1000 FX_BOOL CheckPageStatus(IFX_DownloadHints* pHints); 1001 FX_BOOL CheckAllCrossRefStream(IFX_DownloadHints *pHints); 1002 1003 FX_DWORD CheckCrossRefStream(IFX_DownloadHints *pHints, FX_FILESIZE &xref_offset); 1004 FX_BOOL IsLinearizedFile(FX_LPBYTE pData, FX_DWORD dwLen); 1005 void SetStartOffset(FX_FILESIZE dwOffset); 1006 FX_BOOL GetNextToken(CFX_ByteString &token); 1007 FX_BOOL GetNextChar(FX_BYTE &ch); 1008 CPDF_Object * ParseIndirectObjectAt(FX_FILESIZE pos, FX_DWORD objnum); 1009 CPDF_Object * GetObject(FX_DWORD objnum, IFX_DownloadHints* pHints, FX_BOOL *pExistInFile); 1010 FX_BOOL GetPageKids(CPDF_Parser *pParser, CPDF_Object *pPages); 1011 FX_BOOL PreparePageItem(); 1012 FX_BOOL LoadPages(IFX_DownloadHints* pHints); 1013 FX_BOOL LoadAllXref(IFX_DownloadHints* pHints); 1014 FX_BOOL LoadAllFile(IFX_DownloadHints* pHints); 1015 FX_BOOL CheckLinearizedData(IFX_DownloadHints* pHints); 1016 FX_BOOL CheckFileResources(IFX_DownloadHints* pHints); 1017 FX_BOOL CheckPageAnnots(int iPage, IFX_DownloadHints* pHints); 1018 1019 FX_BOOL CheckLinearizedFirstPage(int iPage, IFX_DownloadHints* pHints); 1020 FX_BOOL HaveResourceAncestor(CPDF_Dictionary *pDict); 1021 FX_BOOL CheckPage(FX_INT32 iPage, IFX_DownloadHints* pHints); 1022 FX_BOOL LoadDocPages(IFX_DownloadHints* pHints); 1023 FX_BOOL LoadDocPage(FX_INT32 iPage, IFX_DownloadHints* pHints); 1024 FX_BOOL CheckPageNode(CPDF_PageNode &pageNodes, FX_INT32 iPage, FX_INT32 &iCount, IFX_DownloadHints* pHints); 1025 FX_BOOL CheckUnkownPageNode(FX_DWORD dwPageNo, CPDF_PageNode *pPageNode, IFX_DownloadHints* pHints); 1026 FX_BOOL CheckArrayPageNode(FX_DWORD dwPageNo, CPDF_PageNode *pPageNode, IFX_DownloadHints* pHints); 1027 FX_BOOL CheckPageCount(IFX_DownloadHints* pHints); 1028 FX_BOOL IsFirstCheck(int iPage); 1029 void ResetFirstCheck(int iPage); 1030 1031 CPDF_Parser m_parser; 1032 1033 CPDF_SyntaxParser m_syntaxParser; 1034 1035 CPDF_Object *m_pRoot; 1036 1037 FX_DWORD m_dwRootObjNum; 1038 1039 FX_DWORD m_dwInfoObjNum; 1040 1041 CPDF_Object *m_pLinearized; 1042 1043 CPDF_Object *m_pTrailer; 1044 1045 FX_BOOL m_bDocAvail; 1046 1047 FX_FILESIZE m_dwHeaderOffset; 1048 1049 FX_FILESIZE m_dwLastXRefOffset; 1050 1051 FX_FILESIZE m_dwXRefOffset; 1052 1053 FX_FILESIZE m_dwTrailerOffset; 1054 1055 FX_FILESIZE m_dwCurrentOffset; 1056 1057 PDF_DATAAVAIL_STATUS m_docStatus; 1058 1059 IFX_FileAvail* m_pFileAvail; 1060 1061 IFX_FileRead* m_pFileRead; 1062 1063 FX_FILESIZE m_dwFileLen; 1064 1065 CPDF_Document* m_pDocument; 1066 1067 CPDF_SortObjNumArray m_objnum_array; 1068 1069 CFX_PtrArray m_objs_array; 1070 1071 FX_FILESIZE m_Pos; 1072 1073 FX_FILESIZE m_bufferOffset; 1074 1075 FX_DWORD m_bufferSize; 1076 1077 CFX_ByteString m_WordBuf; 1078 1079 FX_BYTE m_WordBuffer[257]; 1080 1081 FX_DWORD m_WordSize; 1082 1083 FX_BYTE m_bufferData[512]; 1084 1085 CFX_FileSizeArray m_CrossOffset; 1086 1087 CFX_DWordArray m_XRefStreamList; 1088 1089 CFX_DWordArray m_PageObjList; 1090 1091 FX_DWORD m_PagesObjNum; 1092 1093 FX_BOOL m_bLinearized; 1094 1095 FX_DWORD m_dwFirstPageNo; 1096 1097 FX_BOOL m_bLinearedDataOK; 1098 1099 FX_BOOL m_bMainXRefLoad; 1100 1101 FX_BOOL m_bMainXRefLoadedOK; 1102 1103 FX_BOOL m_bPagesTreeLoad; 1104 1105 FX_BOOL m_bPagesLoad; 1106 1107 CPDF_Parser * m_pCurrentParser; 1108 1109 FX_FILESIZE m_dwCurrentXRefSteam; 1110 1111 FX_BOOL m_bAnnotsLoad; 1112 1113 FX_BOOL m_bHaveAcroForm; 1114 1115 FX_DWORD m_dwAcroFormObjNum; 1116 1117 FX_BOOL m_bAcroFormLoad; 1118 1119 CPDF_Object * m_pAcroForm; 1120 1121 CFX_PtrArray m_arrayAcroforms; 1122 1123 CPDF_Dictionary * m_pPageDict; 1124 1125 CPDF_Object * m_pPageResource; 1126 1127 FX_BOOL m_bNeedDownLoadResource; 1128 1129 FX_BOOL m_bPageLoadedOK; 1130 1131 FX_BOOL m_bLinearizedFormParamLoad; 1132 1133 CFX_PtrArray m_PagesArray; 1134 1135 FX_DWORD m_dwEncryptObjNum; 1136 1137 FX_FILESIZE m_dwPrevXRefOffset; 1138 1139 FX_BOOL m_bTotalLoadPageTree; 1140 1141 FX_BOOL m_bCurPageDictLoadOK; 1142 1143 CPDF_PageNode m_pageNodes; 1144 1145 CFX_CMapDWordToDWord * m_pageMapCheckState; 1146 1147 CFX_CMapDWordToDWord * m_pagesLoadState; 1148 }; 1149 #endif 1150