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