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