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_FPDFAPI_EDIT_CPDF_CREATOR_H_
8 #define CORE_FPDFAPI_EDIT_CPDF_CREATOR_H_
9 
10 #include <memory>
11 #include <vector>
12 
13 #include "core/fxcrt/cfx_retain_ptr.h"
14 #include "core/fxcrt/fx_basic.h"
15 
16 class CPDF_Array;
17 class CPDF_CryptoHandler;
18 class CPDF_Dictionary;
19 class CPDF_Document;
20 class CPDF_Object;
21 class CPDF_Parser;
22 class CPDF_XRefStream;
23 
24 #define FPDFCREATE_INCREMENTAL 1
25 #define FPDFCREATE_NO_ORIGINAL 2
26 #define FPDFCREATE_PROGRESSIVE 4
27 #define FPDFCREATE_OBJECTSTREAM 8
28 
29 CFX_ByteTextBuf& operator<<(CFX_ByteTextBuf& buf, const CPDF_Object* pObj);
30 
31 class CPDF_Creator {
32  public:
33   explicit CPDF_Creator(CPDF_Document* pDoc);
34   ~CPDF_Creator();
35 
36   void RemoveSecurity();
37   bool Create(const CFX_RetainPtr<IFX_WriteStream>& pFile, uint32_t flags = 0);
38   int32_t Continue(IFX_Pause* pPause = nullptr);
39   bool SetFileVersion(int32_t fileVersion = 17);
40 
41  private:
42   friend class CPDF_ObjectStream;
43   friend class CPDF_XRefStream;
44 
45   bool Create(uint32_t flags);
46   void ResetStandardSecurity();
47   void Clear();
48 
49   void InitOldObjNumOffsets();
50   void InitNewObjNumOffsets();
51   void InitID(bool bDefault = true);
52 
53   void AppendNewObjNum(uint32_t objbum);
54   int32_t AppendObjectNumberToXRef(uint32_t objnum);
55 
56   int32_t WriteDoc_Stage1(IFX_Pause* pPause);
57   int32_t WriteDoc_Stage2(IFX_Pause* pPause);
58   int32_t WriteDoc_Stage3(IFX_Pause* pPause);
59   int32_t WriteDoc_Stage4(IFX_Pause* pPause);
60 
61   int32_t WriteOldIndirectObject(uint32_t objnum);
62   int32_t WriteOldObjs(IFX_Pause* pPause);
63   int32_t WriteNewObjs(bool bIncremental, IFX_Pause* pPause);
64   int32_t WriteIndirectObj(const CPDF_Object* pObj);
65   int32_t WriteDirectObj(uint32_t objnum,
66                          const CPDF_Object* pObj,
67                          bool bEncrypt = true);
68   int32_t WriteIndirectObjectToStream(const CPDF_Object* pObj);
69   int32_t WriteIndirectObj(uint32_t objnum, const CPDF_Object* pObj);
70   int32_t WriteIndirectObjectToStream(uint32_t objnum,
71                                       const uint8_t* pBuffer,
72                                       uint32_t dwSize);
73 
74   int32_t WriteStream(const CPDF_Object* pStream,
75                       uint32_t objnum,
76                       CPDF_CryptoHandler* pCrypto);
77 
78   CPDF_Document* const m_pDocument;
79   CPDF_Parser* const m_pParser;
80   bool m_bSecurityChanged;
81   CPDF_Dictionary* m_pEncryptDict;
82   uint32_t m_dwEncryptObjNum;
83   bool m_bEncryptCloned;
84   CPDF_CryptoHandler* m_pCryptoHandler;
85   // Whether this owns the crypto handler |m_pCryptoHandler|.
86   bool m_bLocalCryptoHandler;
87   CPDF_Object* m_pMetadata;
88   std::unique_ptr<CPDF_XRefStream> m_pXRefStream;
89   int32_t m_ObjectStreamSize;
90   uint32_t m_dwLastObjNum;
91   CFX_FileBufferArchive m_File;
92   FX_FILESIZE m_Offset;
93   int32_t m_iStage;
94   uint32_t m_dwFlags;
95   FX_POSITION m_Pos;
96   FX_FILESIZE m_XrefStart;
97   CFX_FileSizeListArray m_ObjectOffset;
98   std::vector<uint32_t> m_NewObjNumArray;  // Sorted, ascending.
99   std::unique_ptr<CPDF_Array> m_pIDArray;
100   int32_t m_FileVersion;
101 };
102 
103 #endif  // CORE_FPDFAPI_EDIT_CPDF_CREATOR_H_
104