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 CORE_INCLUDE_FPDFAPI_FPDF_PARSER_H_ 8 #define CORE_INCLUDE_FPDFAPI_FPDF_PARSER_H_ 9 10 #include "../fxcrt/fx_ext.h" 11 #include "fpdf_objects.h" 12 13 class CPDF_Document; 14 class CPDF_Parser; 15 class CPDF_SecurityHandler; 16 class CPDF_StandardSecurityHandler; 17 class CPDF_CryptoHandler; 18 class CPDF_Object; 19 class IFX_FileRead; 20 class CFDF_Document; 21 class CFDF_Parser; 22 class CFX_Font; 23 class CFX_AffineMatrix; 24 class CFX_FloatRect; 25 class CPDF_Point; 26 class CPDF_DocPageData; 27 class CPDF_DocRenderData; 28 class CPDF_ModuleMgr; 29 class CFX_DIBSource; 30 class CPDF_Font; 31 class CPDF_Image; 32 class CPDF_ColorSpace; 33 class CPDF_Pattern; 34 class CPDF_FontEncoding; 35 class CPDF_IccProfile; 36 class CFX_PrivateData; 37 #define FPDFPERM_PRINT 0x0004 38 #define FPDFPERM_MODIFY 0x0008 39 #define FPDFPERM_EXTRACT 0x0010 40 #define FPDFPERM_ANNOT_FORM 0x0020 41 #define FPDFPERM_FILL_FORM 0x0100 42 #define FPDFPERM_EXTRACT_ACCESS 0x0200 43 #define FPDFPERM_ASSEMBLE 0x0400 44 #define FPDFPERM_PRINT_HIGH 0x0800 45 #define FPDF_PAGE_MAX_NUM 0xFFFFF 46 47 // Indexed by 8-bit character code, contains either: 48 // 'W' - for whitespace: NUL, TAB, CR, LF, FF, 0x80, 0xff 49 // 'N' - for numeric: 0123456789+-. 50 // 'D' - for delimiter: %()/<>[]{} 51 // 'R' - otherwise. 52 extern const char PDF_CharType[256]; 53 54 class CPDF_Document : public CFX_PrivateData, public CPDF_IndirectObjects 55 { 56 public: 57 CPDF_Document(); 58 explicit CPDF_Document(CPDF_Parser* pParser); 59 60 ~CPDF_Document(); 61 GetParser()62 CPDF_Parser* GetParser() const 63 { 64 return m_pParser; 65 } 66 GetRoot()67 CPDF_Dictionary* GetRoot() const 68 { 69 return m_pRootDict; 70 } 71 GetInfo()72 CPDF_Dictionary* GetInfo() const 73 { 74 return m_pInfoDict; 75 } 76 GetID(CFX_ByteString & id1,CFX_ByteString & id2)77 void GetID(CFX_ByteString& id1, CFX_ByteString& id2) const 78 { 79 id1 = m_ID1; 80 id2 = m_ID2; 81 } 82 83 int GetPageCount() const; 84 85 CPDF_Dictionary* GetPage(int iPage); 86 87 int GetPageIndex(FX_DWORD objnum); 88 89 FX_DWORD GetUserPermissions(FX_BOOL bCheckRevision = FALSE) const; 90 91 FX_BOOL IsOwner() const; 92 93 94 GetPageData()95 CPDF_DocPageData* GetPageData() 96 { 97 return GetValidatePageData(); 98 } 99 100 void ClearPageData(); 101 102 void RemoveColorSpaceFromPageData(CPDF_Object* pObject); 103 104 GetRenderData()105 CPDF_DocRenderData* GetRenderData() 106 { 107 return GetValidateRenderData(); 108 } 109 110 void ClearRenderData(); 111 112 void ClearRenderFont(); 113 114 115 FX_BOOL IsFormStream(FX_DWORD objnum, FX_BOOL& bForm) const; 116 117 118 119 120 CPDF_Font* LoadFont(CPDF_Dictionary* pFontDict); 121 122 CPDF_Font* FindFont(CPDF_Dictionary* pFontDict); 123 124 CPDF_ColorSpace* LoadColorSpace(CPDF_Object* pCSObj, CPDF_Dictionary* pResources = NULL); 125 126 CPDF_Pattern* LoadPattern(CPDF_Object* pObj, FX_BOOL bShading, const CFX_AffineMatrix* matrix = NULL); 127 128 CPDF_Image* LoadImageF(CPDF_Object* pObj); 129 130 CPDF_StreamAcc* LoadFontFile(CPDF_Stream* pStream); 131 132 CPDF_IccProfile* LoadIccProfile(CPDF_Stream* pStream); 133 134 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ 135 136 CPDF_Font* AddWindowsFont(LOGFONTA* pLogFont, FX_BOOL bVert, FX_BOOL bTranslateName = FALSE); 137 CPDF_Font* AddWindowsFont(LOGFONTW* pLogFont, FX_BOOL bVert, FX_BOOL bTranslateName = FALSE); 138 #endif 139 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ 140 CPDF_Font* AddMacFont(CTFontRef pFont, FX_BOOL bVert, FX_BOOL bTranslateName = FALSE); 141 #endif 142 143 CPDF_Font* AddStandardFont(const FX_CHAR* font, CPDF_FontEncoding* pEncoding); 144 145 146 CPDF_Font* AddFont(CFX_Font* pFont, int charset, FX_BOOL bVert); 147 148 void CreateNewDoc(); 149 150 CPDF_Dictionary* CreateNewPage(int iPage); 151 152 void DeletePage(int iPage); 153 154 void LoadDoc(); 155 void LoadAsynDoc(CPDF_Dictionary *pLinearized); 156 void LoadPages(); 157 protected: 158 159 CPDF_Dictionary* m_pRootDict; 160 161 CPDF_Dictionary* m_pInfoDict; 162 163 CFX_ByteString m_ID1; 164 165 CFX_ByteString m_ID2; 166 167 168 FX_BOOL m_bLinearized; 169 170 FX_DWORD m_dwFirstPageNo; 171 172 FX_DWORD m_dwFirstPageObjNum; 173 174 CFX_DWordArray m_PageList; 175 176 int _GetPageCount() const; 177 CPDF_Dictionary* _FindPDFPage(CPDF_Dictionary* pPages, int iPage, int nPagesToGo, int level); 178 int _FindPageIndex(CPDF_Dictionary* pNode, FX_DWORD& skip_count, FX_DWORD objnum, int& index, int level = 0); 179 FX_BOOL IsContentUsedElsewhere(FX_DWORD objnum, CPDF_Dictionary* pPageDict); 180 FX_BOOL CheckOCGVisible(CPDF_Dictionary* pOCG, FX_BOOL bPrinting); 181 CPDF_DocPageData* GetValidatePageData(); 182 CPDF_DocRenderData* GetValidateRenderData(); 183 friend class CPDF_Creator; 184 friend class CPDF_Parser; 185 friend class CPDF_DataAvail; 186 friend class CPDF_OCContext; 187 188 189 190 CPDF_DocPageData* m_pDocPage; 191 192 CPDF_DocRenderData* m_pDocRender; 193 194 }; 195 196 #define PDFWORD_EOF 0 197 #define PDFWORD_NUMBER 1 198 #define PDFWORD_TEXT 2 199 #define PDFWORD_DELIMITER 3 200 #define PDFWORD_NAME 4 201 class CPDF_SimpleParser 202 { 203 public: 204 205 CPDF_SimpleParser(FX_LPCBYTE pData, FX_DWORD dwSize); 206 207 CPDF_SimpleParser(FX_BSTR str); 208 209 CFX_ByteStringC GetWord(); 210 211 FX_BOOL SearchToken(FX_BSTR token); 212 213 FX_BOOL SkipWord(FX_BSTR token); 214 215 FX_BOOL FindTagPair(FX_BSTR start_token, FX_BSTR end_token, 216 FX_DWORD& start_pos, FX_DWORD& end_pos); 217 218 FX_BOOL FindTagParam(FX_BSTR token, int nParams); 219 GetPos()220 FX_DWORD GetPos() 221 { 222 return m_dwCurPos; 223 } 224 SetPos(FX_DWORD pos)225 void SetPos(FX_DWORD pos) 226 { 227 ASSERT(pos <= m_dwSize); 228 m_dwCurPos = pos; 229 } 230 private: 231 232 void ParseWord(FX_LPCBYTE& pStart, FX_DWORD& dwSize, int& type); 233 234 FX_LPCBYTE m_pData; 235 236 FX_DWORD m_dwSize; 237 238 FX_DWORD m_dwCurPos; 239 }; 240 class CPDF_SyntaxParser 241 { 242 public: 243 244 CPDF_SyntaxParser(); 245 virtual ~CPDF_SyntaxParser(); 246 247 void InitParser(IFX_FileRead* pFileAccess, FX_DWORD HeaderOffset); 248 SavePos()249 FX_FILESIZE SavePos() 250 { 251 return m_Pos; 252 } 253 RestorePos(FX_FILESIZE pos)254 void RestorePos(FX_FILESIZE pos) 255 { 256 m_Pos = pos; 257 } 258 259 CPDF_Object* GetObject(CPDF_IndirectObjects* pObjList, FX_DWORD objnum, FX_DWORD gennum, struct PARSE_CONTEXT* pContext = NULL, FX_BOOL bDecrypt = TRUE); 260 261 262 CPDF_Object* GetObjectByStrict(CPDF_IndirectObjects* pObjList, FX_DWORD objnum, FX_DWORD gennum, struct PARSE_CONTEXT* pContext = NULL); 263 264 int GetDirectNum(); 265 266 CFX_ByteString GetString(FX_DWORD objnum, FX_DWORD gennum); 267 268 CFX_ByteString GetName(); 269 270 CFX_ByteString GetKeyword(); 271 272 void GetBinary(FX_BYTE* buffer, FX_DWORD size); 273 274 void ToNextLine(); 275 276 void ToNextWord(); 277 278 FX_BOOL SearchWord(FX_BSTR word, FX_BOOL bWholeWord, FX_BOOL bForward, FX_FILESIZE limit); 279 280 int SearchMultiWord(FX_BSTR words, FX_BOOL bWholeWord, FX_FILESIZE limit); 281 282 FX_FILESIZE FindTag(FX_BSTR tag, FX_FILESIZE limit); 283 SetEncrypt(CPDF_CryptoHandler * pCryptoHandler)284 void SetEncrypt(CPDF_CryptoHandler* pCryptoHandler) 285 { 286 m_pCryptoHandler = pCryptoHandler; 287 } 288 IsEncrypted()289 FX_BOOL IsEncrypted() 290 { 291 return m_pCryptoHandler != NULL; 292 } 293 294 FX_BOOL GetCharAt(FX_FILESIZE pos, FX_BYTE& ch); 295 296 FX_BOOL ReadBlock(FX_BYTE* pBuf, FX_DWORD size); 297 298 CFX_ByteString GetNextWord(FX_BOOL& bIsNumber); 299 protected: 300 static const int kParserMaxRecursionDepth = 64; 301 static int s_CurrentRecursionDepth; 302 303 virtual FX_BOOL GetNextChar(FX_BYTE& ch); 304 305 FX_BOOL GetCharAtBackward(FX_FILESIZE pos, FX_BYTE& ch); 306 307 void GetNextWord(); 308 309 FX_BOOL IsWholeWord(FX_FILESIZE startpos, FX_FILESIZE limit, FX_LPCBYTE tag, FX_DWORD taglen); 310 311 CFX_ByteString ReadString(); 312 313 CFX_ByteString ReadHexString(); 314 315 CPDF_Stream* ReadStream(CPDF_Dictionary* pDict, PARSE_CONTEXT* pContext, FX_DWORD objnum, FX_DWORD gennum); 316 317 FX_FILESIZE m_Pos; 318 319 FX_BOOL m_bFileStream; 320 321 int m_MetadataObjnum; 322 323 IFX_FileRead* m_pFileAccess; 324 325 FX_DWORD m_HeaderOffset; 326 327 FX_FILESIZE m_FileLen; 328 329 FX_BYTE* m_pFileBuf; 330 331 FX_DWORD m_BufSize; 332 333 FX_FILESIZE m_BufOffset; 334 335 CPDF_CryptoHandler* m_pCryptoHandler; 336 337 FX_BYTE m_WordBuffer[257]; 338 339 FX_DWORD m_WordSize; 340 341 FX_BOOL m_bIsNumber; 342 343 FX_FILESIZE m_dwWordPos; 344 friend class CPDF_Parser; 345 friend class CPDF_DataAvail; 346 }; 347 348 #define PDFPARSE_TYPEONLY 1 349 #define PDFPARSE_NOSTREAM 2 350 struct PARSE_CONTEXT { 351 352 FX_BOOL m_Flags; 353 354 FX_FILESIZE m_DictStart; 355 356 FX_FILESIZE m_DictEnd; 357 358 FX_FILESIZE m_DataStart; 359 360 FX_FILESIZE m_DataEnd; 361 }; 362 363 #define PDFPARSE_ERROR_SUCCESS 0 364 #define PDFPARSE_ERROR_FILE 1 365 #define PDFPARSE_ERROR_FORMAT 2 366 #define PDFPARSE_ERROR_PASSWORD 3 367 #define PDFPARSE_ERROR_HANDLER 4 368 #define PDFPARSE_ERROR_CERT 5 369 370 class CPDF_Parser 371 { 372 public: 373 CPDF_Parser(); 374 ~CPDF_Parser(); 375 376 FX_DWORD StartParse(FX_LPCSTR filename, FX_BOOL bReParse = FALSE); 377 FX_DWORD StartParse(FX_LPCWSTR filename, FX_BOOL bReParse = FALSE); 378 FX_DWORD StartParse(IFX_FileRead* pFile, FX_BOOL bReParse = FALSE, FX_BOOL bOwnFileRead = TRUE); 379 380 void CloseParser(FX_BOOL bReParse = FALSE); 381 382 FX_DWORD GetPermissions(FX_BOOL bCheckRevision = FALSE); 383 384 FX_BOOL IsOwner(); 385 SetPassword(const FX_CHAR * password)386 void SetPassword(const FX_CHAR* password) 387 { 388 m_Password = password; 389 } 390 GetPassword()391 CFX_ByteString GetPassword() 392 { 393 return m_Password; 394 } 395 GetSecurityHandler()396 CPDF_SecurityHandler* GetSecurityHandler() 397 { 398 return m_pSecurityHandler; 399 } 400 GetCryptoHandler()401 CPDF_CryptoHandler* GetCryptoHandler() 402 { 403 return m_Syntax.m_pCryptoHandler; 404 } 405 406 void SetSecurityHandler(CPDF_SecurityHandler* pSecurityHandler, FX_BOOL bForced = FALSE); 407 GetRecipient()408 CFX_ByteString GetRecipient() 409 { 410 return m_bsRecipient; 411 } 412 GetTrailer()413 CPDF_Dictionary* GetTrailer() 414 { 415 return m_pTrailer; 416 } 417 GetLastXRefOffset()418 FX_FILESIZE GetLastXRefOffset() 419 { 420 return m_LastXRefOffset; 421 } 422 GetDocument()423 CPDF_Document* GetDocument() 424 { 425 return m_pDocument; 426 } 427 GetOtherTrailers()428 CFX_ArrayTemplate<CPDF_Dictionary*>* GetOtherTrailers() 429 { 430 return &m_Trailers; 431 } 432 433 FX_DWORD GetRootObjNum(); 434 FX_DWORD GetInfoObjNum() ; 435 CPDF_Array* GetIDArray() ; 436 GetEncryptDict()437 CPDF_Dictionary* GetEncryptDict() 438 { 439 return m_pEncryptDict; 440 } 441 IsEncrypted()442 FX_BOOL IsEncrypted() 443 { 444 return GetEncryptDict() != NULL; 445 } 446 447 448 CPDF_Object* ParseIndirectObject(CPDF_IndirectObjects* pObjList, FX_DWORD objnum, PARSE_CONTEXT* pContext = NULL) ; 449 FX_DWORD GetLastObjNum(); 450 FX_BOOL IsFormStream(FX_DWORD objnum, FX_BOOL& bForm); 451 452 FX_FILESIZE GetObjectOffset(FX_DWORD objnum); 453 454 FX_FILESIZE GetObjectSize(FX_DWORD objnum); 455 GetObjectVersion(FX_DWORD objnum)456 int GetObjectVersion(FX_DWORD objnum) 457 { 458 return m_ObjVersion[objnum]; 459 } 460 461 void GetIndirectBinary(FX_DWORD objnum, FX_BYTE*& pBuffer, FX_DWORD& size); 462 GetFileStreamOption()463 FX_BOOL GetFileStreamOption() 464 { 465 return m_Syntax.m_bFileStream; 466 } 467 SetFileStreamOption(FX_BOOL b)468 void SetFileStreamOption(FX_BOOL b) 469 { 470 m_Syntax.m_bFileStream = b; 471 } 472 GetFileAccess()473 IFX_FileRead* GetFileAccess() const 474 { 475 return m_Syntax.m_pFileAccess; 476 } 477 GetFileVersion()478 int GetFileVersion() const 479 { 480 return m_FileVersion; 481 } 482 IsXRefStream()483 FX_BOOL IsXRefStream() const 484 { 485 return m_bXRefStream; 486 } 487 CPDF_Object* ParseIndirectObjectAt(CPDF_IndirectObjects* pObjList, FX_FILESIZE pos, FX_DWORD objnum, 488 struct PARSE_CONTEXT* pContext); 489 490 CPDF_Object* ParseIndirectObjectAtByStrict(CPDF_IndirectObjects* pObjList, FX_FILESIZE pos, FX_DWORD objnum, 491 struct PARSE_CONTEXT* pContext, FX_FILESIZE *pResultPos); 492 493 FX_DWORD StartAsynParse(IFX_FileRead* pFile, FX_BOOL bReParse = FALSE, FX_BOOL bOwnFileRead = TRUE); 494 GetFirstPageNo()495 FX_DWORD GetFirstPageNo() 496 { 497 return m_dwFirstPageNo; 498 } 499 protected: 500 CPDF_Document* m_pDocument; 501 502 CPDF_SyntaxParser m_Syntax; 503 FX_BOOL m_bOwnFileRead; 504 CPDF_Object* ParseDirect(CPDF_Object* pObj); 505 506 FX_BOOL LoadAllCrossRefV4(FX_FILESIZE pos); 507 508 FX_BOOL LoadAllCrossRefV5(FX_FILESIZE pos); 509 510 FX_BOOL LoadCrossRefV4(FX_FILESIZE pos, FX_FILESIZE streampos, FX_BOOL bSkip, FX_BOOL bFirst); 511 512 FX_BOOL LoadCrossRefV5(FX_FILESIZE pos, FX_FILESIZE& prev, FX_BOOL bMainXRef); 513 514 CPDF_Dictionary* LoadTrailerV4(); 515 516 FX_BOOL RebuildCrossRef(); 517 518 FX_DWORD SetEncryptHandler(); 519 520 void ReleaseEncryptHandler(); 521 522 FX_BOOL LoadLinearizedAllCrossRefV4(FX_FILESIZE pos, FX_DWORD dwObjCount); 523 524 FX_BOOL LoadLinearizedCrossRefV4(FX_FILESIZE pos, FX_DWORD dwObjCount); 525 526 FX_BOOL LoadLinearizedAllCrossRefV5(FX_FILESIZE pos); 527 528 FX_DWORD LoadLinearizedMainXRefTable(); 529 530 CFX_MapPtrToPtr m_ObjectStreamMap; 531 532 CPDF_StreamAcc* GetObjectStream(FX_DWORD number); 533 534 FX_BOOL IsLinearizedFile(IFX_FileRead* pFileAccess, FX_DWORD offset); 535 536 537 538 int m_FileVersion; 539 540 CPDF_Dictionary* m_pTrailer; 541 542 CPDF_Dictionary* m_pEncryptDict; 543 void SetEncryptDictionary(CPDF_Dictionary* pDict); 544 545 FX_FILESIZE m_LastXRefOffset; 546 547 FX_BOOL m_bXRefStream; 548 549 550 CPDF_SecurityHandler* m_pSecurityHandler; 551 552 FX_BOOL m_bForceUseSecurityHandler; 553 554 CFX_ByteString m_bsRecipient; 555 556 CFX_ByteString m_FilePath; 557 558 CFX_ByteString m_Password; 559 560 CFX_FileSizeArray m_CrossRef; 561 562 CFX_ByteArray m_V5Type; 563 564 CFX_FileSizeArray m_SortedOffset; 565 566 CFX_WordArray m_ObjVersion; 567 CFX_ArrayTemplate<CPDF_Dictionary *> m_Trailers; 568 569 FX_BOOL m_bVersionUpdated; 570 571 CPDF_Object* m_pLinearized; 572 573 FX_DWORD m_dwFirstPageNo; 574 575 FX_DWORD m_dwXrefStartObjNum; 576 friend class CPDF_Creator; 577 friend class CPDF_DataAvail; 578 }; 579 #define FXCIPHER_NONE 0 580 #define FXCIPHER_RC4 1 581 #define FXCIPHER_AES 2 582 #define FXCIPHER_AES2 3 583 class CPDF_SecurityHandler 584 { 585 public: 586 ~CPDF_SecurityHandler()587 virtual ~CPDF_SecurityHandler() {} 588 589 virtual FX_BOOL OnInit(CPDF_Parser* pParser, CPDF_Dictionary* pEncryptDict) = 0; 590 591 virtual FX_DWORD GetPermissions() = 0; 592 593 virtual FX_BOOL IsOwner() = 0; 594 595 virtual FX_BOOL GetCryptInfo(int& cipher, FX_LPCBYTE& buffer, int& keylen) = 0; 596 IsMetadataEncrypted()597 virtual FX_BOOL IsMetadataEncrypted() 598 { 599 return TRUE; 600 } 601 602 virtual CPDF_CryptoHandler* CreateCryptoHandler() = 0; 603 GetStandardHandler()604 virtual CPDF_StandardSecurityHandler* GetStandardHandler() 605 { 606 return NULL; 607 } 608 }; 609 #define PDF_ENCRYPT_CONTENT 0 610 class CPDF_StandardSecurityHandler : public CPDF_SecurityHandler 611 { 612 public: 613 CPDF_StandardSecurityHandler(); 614 615 virtual ~CPDF_StandardSecurityHandler(); 616 virtual FX_BOOL OnInit(CPDF_Parser* pParser, CPDF_Dictionary* pEncryptDict); 617 virtual FX_DWORD GetPermissions(); IsOwner()618 virtual FX_BOOL IsOwner() 619 { 620 return m_bOwner; 621 } 622 virtual FX_BOOL GetCryptInfo(int& cipher, FX_LPCBYTE& buffer, int& keylen); 623 virtual FX_BOOL IsMetadataEncrypted(); 624 virtual CPDF_CryptoHandler* CreateCryptoHandler(); GetStandardHandler()625 virtual CPDF_StandardSecurityHandler* GetStandardHandler() 626 { 627 return this; 628 } 629 630 void OnCreate(CPDF_Dictionary* pEncryptDict, CPDF_Array* pIdArray, 631 FX_LPCBYTE user_pass, FX_DWORD user_size, 632 FX_LPCBYTE owner_pass, FX_DWORD owner_size, FX_DWORD type = PDF_ENCRYPT_CONTENT); 633 634 void OnCreate(CPDF_Dictionary* pEncryptDict, CPDF_Array* pIdArray, 635 FX_LPCBYTE user_pass, FX_DWORD user_size, FX_DWORD type = PDF_ENCRYPT_CONTENT); 636 637 CFX_ByteString GetUserPassword(FX_LPCBYTE owner_pass, FX_DWORD pass_size); 638 CFX_ByteString GetUserPassword(FX_LPCBYTE owner_pass, FX_DWORD pass_size, FX_INT32 key_len); GetVersion()639 int GetVersion() 640 { 641 return m_Version; 642 } GetRevision()643 int GetRevision() 644 { 645 return m_Revision; 646 } 647 648 int CheckPassword(FX_LPCBYTE password, FX_DWORD pass_size, FX_BOOL bOwner, FX_LPBYTE key); 649 int CheckPassword(FX_LPCBYTE password, FX_DWORD pass_size, FX_BOOL bOwner, FX_LPBYTE key, int key_len); 650 private: 651 652 int m_Version; 653 654 int m_Revision; 655 656 CPDF_Parser* m_pParser; 657 658 CPDF_Dictionary* m_pEncryptDict; 659 660 FX_BOOL LoadDict(CPDF_Dictionary* pEncryptDict); 661 FX_BOOL LoadDict(CPDF_Dictionary* pEncryptDict, FX_DWORD type, int& cipher, int& key_len); 662 663 FX_BOOL CheckUserPassword(FX_LPCBYTE password, FX_DWORD pass_size, 664 FX_BOOL bIgnoreEncryptMeta, FX_LPBYTE key, FX_INT32 key_len); 665 666 FX_BOOL CheckOwnerPassword(FX_LPCBYTE password, FX_DWORD pass_size, FX_LPBYTE key, FX_INT32 key_len); 667 FX_BOOL AES256_CheckPassword(FX_LPCBYTE password, FX_DWORD size, FX_BOOL bOwner, FX_LPBYTE key); 668 void AES256_SetPassword(CPDF_Dictionary* pEncryptDict, FX_LPCBYTE password, FX_DWORD size, FX_BOOL bOwner, FX_LPCBYTE key); 669 void AES256_SetPerms(CPDF_Dictionary* pEncryptDict, FX_DWORD permission, FX_BOOL bEncryptMetadata, FX_LPCBYTE key); 670 void OnCreate(CPDF_Dictionary* pEncryptDict, CPDF_Array* pIdArray, 671 FX_LPCBYTE user_pass, FX_DWORD user_size, 672 FX_LPCBYTE owner_pass, FX_DWORD owner_size, FX_BOOL bDefault, FX_DWORD type); 673 FX_BOOL CheckSecurity(FX_INT32 key_len); 674 675 FX_BOOL m_bOwner; 676 677 FX_DWORD m_Permissions; 678 679 int m_Cipher; 680 681 FX_BYTE m_EncryptKey[32]; 682 683 int m_KeyLen; 684 }; 685 class CPDF_CryptoHandler 686 { 687 public: 688 ~CPDF_CryptoHandler()689 virtual ~CPDF_CryptoHandler() {} 690 691 virtual FX_BOOL Init(CPDF_Dictionary* pEncryptDict, CPDF_SecurityHandler* pSecurityHandler) = 0; 692 693 virtual FX_DWORD DecryptGetSize(FX_DWORD src_size) = 0; 694 695 virtual FX_LPVOID DecryptStart(FX_DWORD objnum, FX_DWORD gennum) = 0; 696 697 virtual FX_BOOL DecryptStream(FX_LPVOID context, FX_LPCBYTE src_buf, FX_DWORD src_size, CFX_BinaryBuf& dest_buf) = 0; 698 699 virtual FX_BOOL DecryptFinish(FX_LPVOID context, CFX_BinaryBuf& dest_buf) = 0; 700 701 702 virtual FX_DWORD EncryptGetSize(FX_DWORD objnum, FX_DWORD version, FX_LPCBYTE src_buf, FX_DWORD src_size) = 0; 703 704 virtual FX_BOOL EncryptContent(FX_DWORD objnum, FX_DWORD version, FX_LPCBYTE src_buf, FX_DWORD src_size, 705 FX_LPBYTE dest_buf, FX_DWORD& dest_size) = 0; 706 707 void Decrypt(FX_DWORD objnum, FX_DWORD version, CFX_ByteString& str); 708 }; 709 class CPDF_StandardCryptoHandler : public CPDF_CryptoHandler 710 { 711 public: 712 713 CPDF_StandardCryptoHandler(); 714 715 virtual ~CPDF_StandardCryptoHandler(); 716 717 FX_BOOL Init(int cipher, FX_LPCBYTE key, int keylen); 718 virtual FX_BOOL Init(CPDF_Dictionary* pEncryptDict, CPDF_SecurityHandler* pSecurityHandler); 719 virtual FX_DWORD DecryptGetSize(FX_DWORD src_size); 720 virtual FX_LPVOID DecryptStart(FX_DWORD objnum, FX_DWORD gennum); 721 virtual FX_BOOL DecryptStream(FX_LPVOID context, FX_LPCBYTE src_buf, FX_DWORD src_size, CFX_BinaryBuf& dest_buf); 722 virtual FX_BOOL DecryptFinish(FX_LPVOID context, CFX_BinaryBuf& dest_buf); 723 virtual FX_DWORD EncryptGetSize(FX_DWORD objnum, FX_DWORD version, FX_LPCBYTE src_buf, FX_DWORD src_size); 724 virtual FX_BOOL EncryptContent(FX_DWORD objnum, FX_DWORD version, FX_LPCBYTE src_buf, FX_DWORD src_size, 725 FX_LPBYTE dest_buf, FX_DWORD& dest_size); 726 protected: 727 728 virtual void CryptBlock(FX_BOOL bEncrypt, FX_DWORD objnum, FX_DWORD gennum, FX_LPCBYTE src_buf, FX_DWORD src_size, 729 FX_LPBYTE dest_buf, FX_DWORD& dest_size); 730 virtual FX_LPVOID CryptStart(FX_DWORD objnum, FX_DWORD gennum, FX_BOOL bEncrypt); 731 virtual FX_BOOL CryptStream(FX_LPVOID context, FX_LPCBYTE src_buf, FX_DWORD src_size, CFX_BinaryBuf& dest_buf, FX_BOOL bEncrypt); 732 virtual FX_BOOL CryptFinish(FX_LPVOID context, CFX_BinaryBuf& dest_buf, FX_BOOL bEncrypt); 733 734 FX_BYTE m_EncryptKey[32]; 735 736 int m_KeyLen; 737 738 int m_Cipher; 739 740 FX_LPBYTE m_pAESContext; 741 }; 742 class CPDF_Point 743 { 744 public: 745 CPDF_Point(FX_FLOAT xx,FX_FLOAT yy)746 CPDF_Point(FX_FLOAT xx, FX_FLOAT yy) 747 { 748 x = xx; 749 y = yy; 750 } 751 752 FX_FLOAT x; 753 754 FX_FLOAT y; 755 }; 756 757 #define CPDF_Rect CFX_FloatRect 758 #define CPDF_Matrix CFX_AffineMatrix 759 CFX_ByteString PDF_NameDecode(FX_BSTR orig); 760 CFX_ByteString PDF_NameDecode(const CFX_ByteString& orig); 761 CFX_ByteString PDF_NameEncode(const CFX_ByteString& orig); 762 CFX_ByteString PDF_EncodeString(const CFX_ByteString& src, FX_BOOL bHex = FALSE); 763 CFX_WideString PDF_DecodeText(FX_LPCBYTE pData, FX_DWORD size, CFX_CharMap* pCharMap = NULL); 764 inline CFX_WideString PDF_DecodeText(const CFX_ByteString& bstr, CFX_CharMap* pCharMap = NULL) { 765 return PDF_DecodeText((FX_LPCBYTE)bstr.c_str(), bstr.GetLength(), pCharMap); 766 } 767 CFX_ByteString PDF_EncodeText(FX_LPCWSTR pString, int len = -1, CFX_CharMap* pCharMap = NULL); 768 inline CFX_ByteString PDF_EncodeText(const CFX_WideString& str, CFX_CharMap* pCharMap = NULL) { 769 return PDF_EncodeText(str.c_str(), str.GetLength(), pCharMap); 770 } 771 FX_FLOAT PDF_ClipFloat(FX_FLOAT f); 772 class CFDF_Document : public CPDF_IndirectObjects 773 { 774 public: 775 static CFDF_Document* CreateNewDoc(); 776 static CFDF_Document* ParseFile(IFX_FileRead *pFile, FX_BOOL bOwnFile = FALSE); 777 static CFDF_Document* ParseMemory(FX_LPCBYTE pData, FX_DWORD size); 778 779 ~CFDF_Document(); 780 781 FX_BOOL WriteBuf(CFX_ByteTextBuf& buf) const; 782 GetRoot()783 CPDF_Dictionary* GetRoot() const 784 { 785 return m_pRootDict; 786 } 787 788 CFX_WideString GetWin32Path() const; 789 protected: 790 791 CFDF_Document(); 792 void ParseStream(IFX_FileRead *pFile, FX_BOOL bOwnFile); 793 CPDF_Dictionary* m_pRootDict; 794 IFX_FileRead* m_pFile; 795 FX_BOOL m_bOwnFile; 796 }; 797 798 CFX_WideString FPDF_FileSpec_GetWin32Path(const CPDF_Object* pFileSpec); 799 void FPDF_FileSpec_SetWin32Path(CPDF_Object* pFileSpec, const CFX_WideString& fullpath); 800 801 void FlateEncode(const FX_BYTE* src_buf, FX_DWORD src_size, FX_LPBYTE& dest_buf, FX_DWORD& dest_size); 802 FX_DWORD FlateDecode(const FX_BYTE* src_buf, FX_DWORD src_size, FX_LPBYTE& dest_buf, FX_DWORD& dest_size); 803 FX_DWORD RunLengthDecode(const FX_BYTE* src_buf, FX_DWORD src_size, FX_LPBYTE& dest_buf, FX_DWORD& dest_size); 804 class CPDF_NumberTree 805 { 806 public: 807 CPDF_NumberTree(CPDF_Dictionary * pRoot)808 CPDF_NumberTree(CPDF_Dictionary* pRoot) 809 { 810 m_pRoot = pRoot; 811 } 812 813 CPDF_Object* LookupValue(int num); 814 protected: 815 816 CPDF_Dictionary* m_pRoot; 817 }; 818 819 class IFX_FileAvail 820 { 821 public: ~IFX_FileAvail()822 virtual ~IFX_FileAvail() { } 823 virtual FX_BOOL IsDataAvail( FX_FILESIZE offset, FX_DWORD size) = 0; 824 }; 825 class IFX_DownloadHints 826 { 827 public: ~IFX_DownloadHints()828 virtual ~IFX_DownloadHints() { } 829 virtual void AddSegment(FX_FILESIZE offset, FX_DWORD size) = 0; 830 }; 831 #define PDF_IS_LINEARIZED 1 832 #define PDF_NOT_LINEARIZED 0 833 #define PDF_UNKNOW_LINEARIZED -1 834 #define PDFFORM_NOTAVAIL 0 835 #define PDFFORM_AVAIL 1 836 #define PDFFORM_NOTEXIST 2 837 class IPDF_DataAvail 838 { 839 public: 840 static IPDF_DataAvail* Create(IFX_FileAvail* pFileAvail, IFX_FileRead* pFileRead); ~IPDF_DataAvail()841 virtual ~IPDF_DataAvail() { } 842 GetFileAvail()843 IFX_FileAvail* GetFileAvail() const { return m_pFileAvail; } GetFileRead()844 IFX_FileRead* GetFileRead() const { return m_pFileRead; } 845 846 virtual FX_BOOL IsDocAvail(IFX_DownloadHints* pHints) = 0; 847 virtual void SetDocument(CPDF_Document* pDoc) = 0; 848 virtual FX_BOOL IsPageAvail(int iPage, IFX_DownloadHints* pHints) = 0; 849 virtual FX_BOOL IsLinearized() = 0; 850 virtual FX_INT32 IsFormAvail(IFX_DownloadHints *pHints) = 0; 851 virtual FX_INT32 IsLinearizedPDF() = 0; 852 virtual void GetLinearizedMainXRefInfo(FX_FILESIZE *pPos, FX_DWORD *pSize) = 0; 853 854 protected: 855 IPDF_DataAvail(IFX_FileAvail* pFileAvail, IFX_FileRead* pFileRead); 856 857 IFX_FileAvail* m_pFileAvail; 858 IFX_FileRead* m_pFileRead; 859 }; 860 class CPDF_SortObjNumArray 861 { 862 public: 863 864 void AddObjNum(FX_DWORD dwObjNum); 865 866 FX_BOOL Find(FX_DWORD dwObjNum); 867 RemoveAll()868 void RemoveAll() 869 { 870 m_number_array.RemoveAll(); 871 } 872 protected: 873 874 FX_BOOL BinarySearch(FX_DWORD value, int &iNext); 875 protected: 876 877 CFX_DWordArray m_number_array; 878 }; 879 enum PDF_PAGENODE_TYPE { 880 PDF_PAGENODE_UNKOWN = 0, 881 PDF_PAGENODE_PAGE, 882 PDF_PAGENODE_PAGES, 883 PDF_PAGENODE_ARRAY, 884 }; 885 class CPDF_PageNode 886 { 887 public: CPDF_PageNode()888 CPDF_PageNode() : m_type(PDF_PAGENODE_UNKOWN) {} 889 ~CPDF_PageNode(); 890 PDF_PAGENODE_TYPE m_type; 891 FX_DWORD m_dwPageNo; 892 CFX_PtrArray m_childNode; 893 }; 894 enum PDF_DATAAVAIL_STATUS { 895 PDF_DATAAVAIL_HEADER = 0, 896 PDF_DATAAVAIL_FIRSTPAGE, 897 PDF_DATAAVAIL_FIRSTPAGE_PREPARE, 898 PDF_DATAAVAIL_END, 899 PDF_DATAAVAIL_CROSSREF, 900 PDF_DATAAVAIL_CROSSREF_ITEM, 901 PDF_DATAAVAIL_CROSSREF_STREAM, 902 PDF_DATAAVAIL_TRAILER, 903 PDF_DATAAVAIL_LOADALLCRSOSSREF, 904 PDF_DATAAVAIL_ROOT, 905 PDF_DATAAVAIL_INFO, 906 PDF_DATAAVAIL_ACROFORM, 907 PDF_DATAAVAIL_ACROFORM_SUBOBJECT, 908 PDF_DATAAVAIL_PAGETREE, 909 PDF_DATAAVAIL_PAGE, 910 PDF_DATAAVAIL_PAGE_LATERLOAD, 911 PDF_DATAAVAIL_RESOURCES, 912 PDF_DATAAVAIL_DONE, 913 PDF_DATAAVAIL_ERROR, 914 PDF_DATAAVAIL_LOADALLFILE, 915 PDF_DATAAVAIL_TRAILER_APPEND 916 }; 917 918 #endif // CORE_INCLUDE_FPDFAPI_FPDF_PARSER_H_ 919