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