Fix all remaining instances of FX_NEW.
[pdfium.git] / core / include / fpdfapi / fpdf_objects.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_OBJECTS_
8 #define _FPDF_OBJECTS_
9 #ifndef _FXCRT_EXTENSION_
10 #include "../fxcrt/fx_ext.h"
11 #endif
12 class CPDF_Document;
13 class CPDF_IndirectObjects;
14 class CPDF_Null;
15 class CPDF_Boolean;
16 class CPDF_Number;
17 class CPDF_String;
18 class CPDF_Stream;
19 class CPDF_StreamAcc;
20 class CPDF_StreamFilter;
21 class CPDF_Array;
22 class CPDF_Dictionary;
23 class CPDF_Reference;
24 class IPDF_DocParser;
25 class IFX_FileRead;
26 class CPDF_CryptoHandler;
27 #define PDFOBJ_INVALID          0
28 #define PDFOBJ_BOOLEAN          1
29 #define PDFOBJ_NUMBER           2
30 #define PDFOBJ_STRING           3
31 #define PDFOBJ_NAME                     4
32 #define PDFOBJ_ARRAY            5
33 #define PDFOBJ_DICTIONARY       6
34 #define PDFOBJ_STREAM           7
35 #define PDFOBJ_NULL                     8
36 #define PDFOBJ_REFERENCE        9
37 typedef IFX_FileStream* (*FPDF_LPFCloneStreamCallback)(CPDF_Stream *pStream, FX_LPVOID pUserData);
38 class CPDF_Object 
39 {
40 public:
41
42     int                                         GetType() const
43     {
44         return m_Type;
45     }
46
47     FX_DWORD                            GetObjNum() const
48     {
49         return m_ObjNum;
50     }
51
52     FX_DWORD                            GetGenNum() const
53     {
54         return m_GenNum;
55     }
56
57     FX_BOOL                                     IsIdentical(CPDF_Object* pObj) const;
58
59     CPDF_Object*                        Clone(FX_BOOL bDirect = FALSE) const;
60
61     CPDF_Object*                        CloneRef(CPDF_IndirectObjects* pObjs) const;
62
63     CPDF_Object*                        GetDirect() const;
64
65     void                                        Release();
66
67     CFX_ByteString                      GetString() const;
68
69     CFX_ByteStringC                     GetConstString() const;
70
71     CFX_WideString                      GetUnicodeText(CFX_CharMap* pCharMap = NULL) const;
72
73     FX_FLOAT                            GetNumber() const;
74
75     FX_FLOAT                            GetNumber16() const;
76
77     int                                         GetInteger() const;
78
79     CPDF_Dictionary*            GetDict() const;
80
81     CPDF_Array*                         GetArray() const;
82
83     void                                        SetString(const CFX_ByteString& str);
84
85     void                                        SetUnicodeText(FX_LPCWSTR pUnicodes, int len = -1);
86
87     int                                         GetDirectType() const;
88
89     FX_BOOL                                     IsModified() const
90     {
91         return FALSE;
92     }
93 protected:
94     CPDF_Object(FX_DWORD type) : m_Type(type), m_ObjNum(0), m_GenNum(0) { }
95     ~CPDF_Object() { }
96
97     void                                        Destroy();
98
99     FX_DWORD                            m_Type;
100     FX_DWORD                            m_ObjNum;
101     FX_DWORD                            m_GenNum;
102
103     friend class                        CPDF_IndirectObjects;
104     friend class                        CPDF_Parser;
105     friend class                        CPDF_SyntaxParser;
106 private:
107     CPDF_Object(const CPDF_Object& src) {}
108     CPDF_Object* CloneInternal(FX_BOOL bDirect, CFX_MapPtrToPtr* visited) const;
109 };
110 class CPDF_Boolean : public CPDF_Object
111 {
112 public:
113
114     static CPDF_Boolean*        Create(FX_BOOL value)
115     {
116         return new CPDF_Boolean(value);
117     }
118
119     CPDF_Boolean() : CPDF_Object(PDFOBJ_BOOLEAN), m_bValue(false) { }
120     CPDF_Boolean(FX_BOOL value) : CPDF_Object(PDFOBJ_BOOLEAN), m_bValue(value) { }
121
122     FX_BOOL                                     Identical(CPDF_Boolean* pOther) const
123     {
124         return m_bValue == pOther->m_bValue;
125     }
126 protected:
127
128     FX_BOOL                                     m_bValue;
129     friend class                        CPDF_Object;
130 };
131 class CPDF_Number : public CPDF_Object
132 {
133 public:
134
135     static CPDF_Number*         Create(int value)
136     {
137         return new CPDF_Number(value);
138     }
139
140     static CPDF_Number*         Create(FX_FLOAT value)
141     {
142         return new CPDF_Number(value);
143     }
144
145     static CPDF_Number*         Create(FX_BSTR str)
146     {
147         return new CPDF_Number(str);
148     }
149
150     static CPDF_Number*         Create(FX_BOOL bInteger, void* pData)
151     {
152         return new CPDF_Number(bInteger, pData);
153     }
154
155     CPDF_Number() : CPDF_Object(PDFOBJ_NUMBER), m_bInteger(false), m_Integer(0) { }
156
157     CPDF_Number(FX_BOOL bInteger, void* pData);
158
159     CPDF_Number(int value);
160
161     CPDF_Number(FX_FLOAT value);
162
163     CPDF_Number(FX_BSTR str);
164
165     FX_BOOL                                     Identical(CPDF_Number* pOther) const;
166
167     CFX_ByteString                      GetString() const;
168
169     void                                        SetString(FX_BSTR str);
170
171     FX_BOOL                                     IsInteger() const
172     {
173         return m_bInteger;
174     }
175
176     int                                         GetInteger() const
177     {
178         return m_bInteger ? m_Integer : (int)m_Float;
179     }
180
181     FX_FLOAT                            GetNumber() const
182     {
183         return m_bInteger ? (FX_FLOAT)m_Integer : m_Float;
184     }
185
186     void                                        SetNumber(FX_FLOAT value);
187
188     FX_FLOAT                    GetNumber16() const
189     {
190         return GetNumber();
191     }
192
193     FX_FLOAT                            GetFloat() const
194     {
195         return m_bInteger ? (FX_FLOAT)m_Integer : m_Float;
196     }
197 protected:
198
199     FX_BOOL                                     m_bInteger;
200
201     union {
202
203         int                                     m_Integer;
204
205         FX_FLOAT                        m_Float;
206     };
207     friend class                        CPDF_Object;
208 };
209 class CPDF_String : public CPDF_Object
210 {
211 public:
212
213     static CPDF_String*         Create(const CFX_ByteString& str, FX_BOOL bHex = FALSE)
214     {
215         return new CPDF_String(str, bHex);
216     }
217
218     static CPDF_String*         Create(const CFX_WideString& str)
219     {
220         return new CPDF_String(str);
221     }
222
223     CPDF_String() : CPDF_Object(PDFOBJ_STRING), m_bHex(FALSE) { }
224
225     CPDF_String(const CFX_ByteString& str, FX_BOOL bHex = FALSE)
226         : CPDF_Object(PDFOBJ_STRING), m_String(str), m_bHex(bHex) {
227     }
228
229     CPDF_String(const CFX_WideString& str);
230
231     CFX_ByteString&                     GetString()
232     {
233         return m_String;
234     }
235
236     FX_BOOL                                     Identical(CPDF_String* pOther) const
237     {
238         return m_String == pOther->m_String;
239     }
240
241     FX_BOOL                                     IsHex() const
242     {
243         return m_bHex;
244     }
245 protected:
246
247     CFX_ByteString                      m_String;
248
249     FX_BOOL                                     m_bHex;
250     friend class                        CPDF_Object;
251 };
252 class CPDF_Name : public CPDF_Object
253 {
254 public:
255
256     static CPDF_Name*           Create(const CFX_ByteString& str)
257     {
258         return new CPDF_Name(str);
259     }
260
261     static CPDF_Name*           Create(FX_BSTR str)
262     {
263         return new CPDF_Name(str);
264     }
265
266     static CPDF_Name*           Create(FX_LPCSTR str)
267     {
268         return new CPDF_Name(str);
269     }
270
271     CPDF_Name(const CFX_ByteString& str) : CPDF_Object(PDFOBJ_NAME), m_Name(str) { }
272     CPDF_Name(FX_BSTR str) : CPDF_Object(PDFOBJ_NAME), m_Name(str) { }
273     CPDF_Name(FX_LPCSTR str) : CPDF_Object(PDFOBJ_NAME), m_Name(str) { }
274
275     CFX_ByteString&                     GetString()
276     {
277         return m_Name;
278     }
279
280     FX_BOOL                                     Identical(CPDF_Name* pOther) const
281     {
282         return m_Name == pOther->m_Name;
283     }
284 protected:
285
286     CFX_ByteString                      m_Name;
287     friend class                        CPDF_Object;
288 };
289 class CPDF_Array : public CPDF_Object
290 {
291 public:
292
293     static CPDF_Array*          Create()
294     {
295         return new CPDF_Array();
296     }
297
298     CPDF_Array() : CPDF_Object(PDFOBJ_ARRAY) { }
299
300     FX_DWORD                            GetCount() const
301     {
302         return m_Objects.GetSize();
303     }
304
305     CPDF_Object*                        GetElement(FX_DWORD index) const;
306
307     CPDF_Object*                        GetElementValue(FX_DWORD index) const;
308
309
310
311     CFX_AffineMatrix            GetMatrix();
312
313     CFX_FloatRect                       GetRect();
314
315
316
317
318     CFX_ByteString                      GetString(FX_DWORD index) const;
319
320     CFX_ByteStringC                     GetConstString(FX_DWORD index) const;
321
322     int                                         GetInteger(FX_DWORD index) const;
323
324     FX_FLOAT                            GetNumber(FX_DWORD index) const;
325
326     CPDF_Dictionary*            GetDict(FX_DWORD index) const;
327
328     CPDF_Stream*                        GetStream(FX_DWORD index) const;
329
330     CPDF_Array*                         GetArray(FX_DWORD index) const;
331
332     FX_FLOAT                            GetFloat(FX_DWORD index) const
333     {
334         return GetNumber(index);
335     }
336
337
338
339
340     void                                        SetAt(FX_DWORD index, CPDF_Object* pObj, CPDF_IndirectObjects* pObjs = NULL);
341
342
343     void                                        InsertAt(FX_DWORD index, CPDF_Object* pObj, CPDF_IndirectObjects* pObjs = NULL);
344
345     void                                        RemoveAt(FX_DWORD index);
346
347
348     void                                        Add(CPDF_Object* pObj, CPDF_IndirectObjects* pObjs = NULL);
349
350
351
352     void                                        AddNumber(FX_FLOAT f);
353
354     void                                        AddInteger(int i);
355
356     void                                        AddString(const CFX_ByteString& str);
357
358     void                                        AddName(const CFX_ByteString& str);
359
360     void                                        AddReference(CPDF_IndirectObjects* pDoc, FX_DWORD objnum);
361
362     void                                        AddReference(CPDF_IndirectObjects* pDoc, CPDF_Object* obj)
363     {
364         AddReference(pDoc, obj->GetObjNum());
365     }
366
367
368     FX_FLOAT                    GetNumber16(FX_DWORD index) const
369     {
370         return GetNumber(index);
371     }
372
373     void                                        AddNumber16(FX_FLOAT value)
374     {
375         AddNumber(value);
376     }
377
378     FX_BOOL                                     Identical(CPDF_Array* pOther) const;
379 protected:
380
381     ~CPDF_Array();
382
383     CFX_PtrArray                        m_Objects;
384     friend class                        CPDF_Object;
385 };
386 class CPDF_Dictionary : public CPDF_Object
387 {
388 public:
389
390     static CPDF_Dictionary*     Create()
391     {
392         return new CPDF_Dictionary();
393     }
394
395     CPDF_Dictionary() : CPDF_Object(PDFOBJ_DICTIONARY) { }
396
397     CPDF_Object*                        GetElement(FX_BSTR key) const;
398
399     CPDF_Object*                        GetElementValue(FX_BSTR key) const;
400
401
402
403
404
405     CFX_ByteString                      GetString(FX_BSTR key) const;
406
407     CFX_ByteStringC                     GetConstString(FX_BSTR key) const;
408
409     CFX_ByteString                      GetString(FX_BSTR key, FX_BSTR default_str) const;
410
411     CFX_ByteStringC                     GetConstString(FX_BSTR key, FX_BSTR default_str) const;
412
413     CFX_WideString                      GetUnicodeText(FX_BSTR key, CFX_CharMap* pCharMap = NULL) const;
414
415     int                                         GetInteger(FX_BSTR key) const;
416
417     int                                         GetInteger(FX_BSTR key, int default_int) const;
418
419     FX_BOOL                                     GetBoolean(FX_BSTR key, FX_BOOL bDefault = FALSE) const;
420
421     FX_FLOAT                            GetNumber(FX_BSTR key) const;
422
423     CPDF_Dictionary*            GetDict(FX_BSTR key) const;
424
425     CPDF_Stream*                        GetStream(FX_BSTR key) const;
426
427     CPDF_Array*                         GetArray(FX_BSTR key) const;
428
429     CFX_FloatRect                       GetRect(FX_BSTR key) const;
430
431     CFX_AffineMatrix            GetMatrix(FX_BSTR key) const;
432
433     FX_FLOAT                            GetFloat(FX_BSTR key) const
434     {
435         return GetNumber(key);
436     }
437
438
439     FX_BOOL                                     KeyExist(FX_BSTR key) const;
440
441     FX_POSITION                         GetStartPos() const;
442
443     CPDF_Object*                        GetNextElement(FX_POSITION& pos, CFX_ByteString& key) const;
444
445     void                                        SetAt(FX_BSTR key, CPDF_Object* pObj, CPDF_IndirectObjects* pObjs = NULL);
446
447
448
449     void                                        SetAtName(FX_BSTR key, const CFX_ByteString& name);
450
451
452     void                                        SetAtString(FX_BSTR key, const CFX_ByteString& string);
453
454
455     void                                        SetAtInteger(FX_BSTR key, int i);
456
457
458     void                                        SetAtNumber(FX_BSTR key, FX_FLOAT f);
459
460     void                                        SetAtReference(FX_BSTR key, CPDF_IndirectObjects* pDoc, FX_DWORD objnum);
461
462     void                                        SetAtReference(FX_BSTR key, CPDF_IndirectObjects* pDoc, CPDF_Object* obj)
463     {
464         SetAtReference(key, pDoc, obj->GetObjNum());
465     }
466
467     void                                        AddReference(FX_BSTR key, CPDF_IndirectObjects* pDoc, FX_DWORD objnum);
468
469     void                                        AddReference(FX_BSTR key, CPDF_IndirectObjects* pDoc, CPDF_Object* obj)
470     {
471         AddReference(key, pDoc, obj->GetObjNum());
472     }
473
474     void                                        SetAtRect(FX_BSTR key, const CFX_FloatRect& rect);
475
476     void                                        SetAtMatrix(FX_BSTR key, const CFX_AffineMatrix& matrix);
477
478     void                                        SetAtBoolean(FX_BSTR key, FX_BOOL bValue);
479
480
481
482     void                                        RemoveAt(FX_BSTR key);
483
484
485     void                                        ReplaceKey(FX_BSTR oldkey, FX_BSTR newkey);
486
487     FX_BOOL                                     Identical(CPDF_Dictionary* pDict) const;
488
489     int                                         GetCount() const
490     {
491         return m_Map.GetCount();
492     }
493
494     void                                        AddValue(FX_BSTR key, CPDF_Object* pObj);
495 protected:
496
497     ~CPDF_Dictionary();
498
499     CFX_CMapByteStringToPtr     m_Map;
500
501     friend class                        CPDF_Object;
502 };
503 class CPDF_Stream : public CPDF_Object
504 {
505 public:
506
507     static CPDF_Stream*         Create(FX_LPBYTE pData, FX_DWORD size, CPDF_Dictionary* pDict)
508     {
509         return new CPDF_Stream(pData, size, pDict);
510     }
511
512     CPDF_Stream(FX_LPBYTE pData, FX_DWORD size, CPDF_Dictionary* pDict);
513
514     CPDF_Dictionary*            GetDict() const
515     {
516         return m_pDict;
517     }
518
519     void                                        SetData(FX_LPCBYTE pData, FX_DWORD size, FX_BOOL bCompressed, FX_BOOL bKeepBuf);
520
521     void                                        InitStream(FX_BYTE* pData, FX_DWORD size, CPDF_Dictionary* pDict);
522
523     void                                        InitStream(IFX_FileRead *pFile, CPDF_Dictionary* pDict);
524
525     FX_BOOL                                     Identical(CPDF_Stream* pOther) const;
526
527     CPDF_StreamFilter*          GetStreamFilter(FX_BOOL bRaw = FALSE) const;
528
529
530
531     FX_DWORD                            GetRawSize() const
532     {
533         return m_dwSize;
534     }
535
536     FX_BOOL                                     ReadRawData(FX_FILESIZE start_pos, FX_LPBYTE pBuf, FX_DWORD buf_size) const;
537
538
539     FX_BOOL                                     IsMemoryBased() const
540     {
541         return m_GenNum == (FX_DWORD) - 1;
542     }
543
544     CPDF_Stream*                        Clone(FX_BOOL bDirect, FPDF_LPFCloneStreamCallback lpfCallback, FX_LPVOID pUserData) const;
545 protected:
546
547     ~CPDF_Stream();
548
549     CPDF_Dictionary*            m_pDict;
550
551     FX_DWORD                            m_dwSize;
552
553     FX_DWORD                            m_GenNum;
554
555     union {
556
557         FX_LPBYTE                       m_pDataBuf;
558
559         IFX_FileRead*           m_pFile;
560     };
561
562     FX_FILESIZE                         m_FileOffset;
563
564     CPDF_CryptoHandler*         m_pCryptoHandler;
565
566     void                                        InitStream(CPDF_Dictionary* pDict);
567     friend class                        CPDF_Object;
568     friend class                        CPDF_StreamAcc;
569     friend class                        CPDF_AttachmentAcc;
570 };
571 class CPDF_StreamAcc 
572 {
573 public:
574
575     CPDF_StreamAcc();
576
577     ~CPDF_StreamAcc();
578
579     void                                        LoadAllData(const CPDF_Stream* pStream, FX_BOOL bRawAccess = FALSE,
580                                         FX_DWORD estimated_size = 0, FX_BOOL bImageAcc = FALSE);
581
582     const CPDF_Stream*          GetStream() const
583     {
584         return m_pStream;
585     }
586
587     CPDF_Dictionary*            GetDict() const
588     {
589         return m_pStream? m_pStream->GetDict() : NULL;
590     }
591
592     FX_LPCBYTE                          GetData() const;
593
594     FX_DWORD                            GetSize() const;
595
596     FX_LPBYTE                           DetachData();
597
598     const CFX_ByteString&       GetImageDecoder()
599     {
600         return m_ImageDecoder;
601     }
602
603     const CPDF_Dictionary*      GetImageParam()
604     {
605         return m_pImageParam;
606     }
607 protected:
608
609     FX_LPBYTE                           m_pData;
610
611     FX_DWORD                            m_dwSize;
612
613     FX_BOOL                                     m_bNewBuf;
614
615     CFX_ByteString                      m_ImageDecoder;
616
617     CPDF_Dictionary*            m_pImageParam;
618
619     const CPDF_Stream*          m_pStream;
620
621     FX_LPBYTE                           m_pSrcData;
622 };
623 CFX_DataFilter* FPDF_CreateFilter(FX_BSTR name, const CPDF_Dictionary* pParam, int width = 0, int height = 0);
624 #define FPDF_FILTER_BUFFER_SIZE         20480
625 class CPDF_StreamFilter 
626 {
627 public:
628
629     ~CPDF_StreamFilter();
630
631     FX_DWORD                    ReadBlock(FX_LPBYTE buffer, FX_DWORD size);
632
633     FX_DWORD                    GetSrcPos()
634     {
635         return m_SrcOffset;
636     }
637
638     const CPDF_Stream*  GetStream()
639     {
640         return m_pStream;
641     }
642 protected:
643
644     CPDF_StreamFilter() {}
645
646     FX_DWORD                    ReadLeftOver(FX_LPBYTE buffer, FX_DWORD buf_size);
647
648     const CPDF_Stream*  m_pStream;
649
650     CFX_DataFilter*             m_pFilter;
651
652     CFX_BinaryBuf*              m_pBuffer;
653
654     FX_DWORD                    m_BufOffset;
655
656     FX_DWORD                    m_SrcOffset;
657
658     FX_BYTE                             m_SrcBuffer[FPDF_FILTER_BUFFER_SIZE];
659     friend class CPDF_Stream;
660 };
661 class CPDF_Null : public CPDF_Object
662 {
663 public:
664
665     static CPDF_Null*           Create()
666     {
667         return new CPDF_Null();
668     }
669
670     CPDF_Null() : CPDF_Object(PDFOBJ_NULL) { }
671 };
672 class CPDF_Reference : public CPDF_Object
673 {
674 public:
675
676     static CPDF_Reference*      Create(CPDF_IndirectObjects* pDoc, int objnum)
677     {
678         return new CPDF_Reference(pDoc, objnum);
679     }
680
681     CPDF_Reference(CPDF_IndirectObjects* pDoc, int objnum)
682         : CPDF_Object(PDFOBJ_REFERENCE), m_pObjList(pDoc), m_RefObjNum(objnum) {
683     }
684
685     CPDF_IndirectObjects*       GetObjList() const
686     {
687         return m_pObjList;
688     }
689
690     FX_DWORD                            GetRefObjNum() const
691     {
692         return m_RefObjNum;
693     }
694
695     void                                        SetRef(CPDF_IndirectObjects* pDoc, FX_DWORD objnum);
696
697     FX_BOOL                                     Identical(CPDF_Reference* pOther) const
698     {
699         return m_RefObjNum == pOther->m_RefObjNum;
700     }
701 protected:
702
703     CPDF_IndirectObjects*       m_pObjList;
704
705     FX_DWORD                            m_RefObjNum;
706     friend class                        CPDF_Object;
707 };
708 class CPDF_IndirectObjects 
709 {
710 public:
711
712     CPDF_IndirectObjects(IPDF_DocParser* pParser);
713
714     ~CPDF_IndirectObjects();
715
716     CPDF_Object*                        GetIndirectObject(FX_DWORD objnum, struct PARSE_CONTEXT* pContext = NULL);
717
718     int                                         GetIndirectType(FX_DWORD objnum);
719
720     FX_DWORD                            AddIndirectObject(CPDF_Object* pObj);
721
722     void                                        ReleaseIndirectObject(FX_DWORD objnum);
723
724     void                                        InsertIndirectObject(FX_DWORD objnum, CPDF_Object* pObj);
725
726     FX_DWORD                            GetLastObjNum() const;
727
728     FX_POSITION                         GetStartPosition() const
729     {
730         return m_IndirectObjs.GetStartPosition();
731     }
732
733     void                                        GetNextAssoc(FX_POSITION& rPos, FX_DWORD& objnum, CPDF_Object*& pObject) const
734     {
735         m_IndirectObjs.GetNextAssoc(rPos, (void*&)objnum, (void*&)pObject);
736     }
737 protected:
738
739     CFX_MapPtrToPtr                     m_IndirectObjs;
740
741     IPDF_DocParser*                     m_pParser;
742
743     FX_DWORD                            m_LastObjNum;
744 };
745 #endif