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