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