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 <map> 11 #include <memory> 12 #include <vector> 13 14 #include "core/fxcrt/fx_stream.h" 15 #include "core/fxcrt/maybe_owned.h" 16 #include "core/fxcrt/retain_ptr.h" 17 #include "core/fxcrt/unowned_ptr.h" 18 19 class CPDF_Array; 20 class CPDF_CryptoHandler; 21 class CPDF_SecurityHandler; 22 class CPDF_Dictionary; 23 class CPDF_Document; 24 class CPDF_Object; 25 class CPDF_Parser; 26 27 #define FPDFCREATE_INCREMENTAL 1 28 #define FPDFCREATE_NO_ORIGINAL 2 29 30 class CPDF_Creator { 31 public: 32 explicit CPDF_Creator(CPDF_Document* pDoc, 33 const RetainPtr<IFX_WriteStream>& archive); 34 ~CPDF_Creator(); 35 36 void RemoveSecurity(); 37 bool Create(uint32_t flags); 38 int32_t Continue(); 39 bool SetFileVersion(int32_t fileVersion); 40 41 IFX_ArchiveStream* GetArchive() { return m_Archive.get(); } 42 43 uint32_t GetNextObjectNumber() { return ++m_dwLastObjNum; } 44 uint32_t GetLastObjectNumber() const { return m_dwLastObjNum; } 45 CPDF_CryptoHandler* GetCryptoHandler(); 46 CPDF_Document* GetDocument() const { return m_pDocument.Get(); } 47 CPDF_Array* GetIDArray() const { return m_pIDArray.get(); } 48 CPDF_Dictionary* GetEncryptDict() const { return m_pEncryptDict.Get(); } 49 uint32_t GetEncryptObjectNumber() const { return m_dwEncryptObjNum; } 50 51 uint32_t GetObjectOffset(uint32_t objnum) { return m_ObjectOffsets[objnum]; } 52 bool HasObjectNumber(uint32_t objnum) { 53 return m_ObjectOffsets.find(objnum) != m_ObjectOffsets.end(); 54 } 55 void SetObjectOffset(uint32_t objnum, FX_FILESIZE offset) { 56 m_ObjectOffsets[objnum] = offset; 57 } 58 bool IsIncremental() const { return !!(m_dwFlags & FPDFCREATE_INCREMENTAL); } 59 bool IsOriginal() const { return !(m_dwFlags & FPDFCREATE_NO_ORIGINAL); } 60 61 private: 62 void Clear(); 63 64 void InitOldObjNumOffsets(); 65 void InitNewObjNumOffsets(); 66 void InitID(); 67 68 int32_t WriteDoc_Stage1(); 69 int32_t WriteDoc_Stage2(); 70 int32_t WriteDoc_Stage3(); 71 int32_t WriteDoc_Stage4(); 72 73 bool WriteOldIndirectObject(uint32_t objnum); 74 bool WriteOldObjs(); 75 bool WriteNewObjs(); 76 bool WriteDirectObj(uint32_t objnum, const CPDF_Object* pObj, bool bEncrypt); 77 bool WriteIndirectObj(uint32_t objnum, const CPDF_Object* pObj); 78 79 bool WriteStream(const CPDF_Object* pStream, 80 uint32_t objnum, 81 CPDF_CryptoHandler* pCrypto); 82 83 bool IsXRefNeedEnd(); 84 85 UnownedPtr<CPDF_Document> const m_pDocument; 86 UnownedPtr<CPDF_Parser> const m_pParser; 87 bool m_bSecurityChanged; 88 UnownedPtr<CPDF_Dictionary> m_pEncryptDict; 89 uint32_t m_dwEncryptObjNum; 90 fxcrt::MaybeOwned<CPDF_SecurityHandler> m_pSecurityHandler; 91 UnownedPtr<CPDF_Object> m_pMetadata; 92 uint32_t m_dwLastObjNum; 93 std::unique_ptr<IFX_ArchiveStream> m_Archive; 94 FX_FILESIZE m_SavedOffset; 95 int32_t m_iStage; 96 uint32_t m_dwFlags; 97 uint32_t m_CurObjNum; 98 FX_FILESIZE m_XrefStart; 99 std::map<uint32_t, FX_FILESIZE> m_ObjectOffsets; 100 std::vector<uint32_t> m_NewObjNumArray; // Sorted, ascending. 101 std::unique_ptr<CPDF_Array> m_pIDArray; 102 int32_t m_FileVersion; 103 }; 104 105 #endif // CORE_FPDFAPI_EDIT_CPDF_CREATOR_H_ 106