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