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