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/retain_ptr.h" 16 #include "core/fxcrt/unowned_ptr.h" 17 18 class CPDF_Array; 19 class CPDF_CryptoHandler; 20 class CPDF_SecurityHandler; 21 class CPDF_Dictionary; 22 class CPDF_Document; 23 class CPDF_Object; 24 class CPDF_Parser; 25 26 #define FPDFCREATE_INCREMENTAL 1 27 #define FPDFCREATE_NO_ORIGINAL 2 28 29 class CPDF_Creator { 30 public: 31 CPDF_Creator(CPDF_Document* pDoc, 32 const RetainPtr<IFX_RetainableWriteStream>& archive); 33 ~CPDF_Creator(); 34 35 void RemoveSecurity(); 36 bool Create(uint32_t flags); 37 bool SetFileVersion(int32_t fileVersion); 38 39 private: 40 enum class Stage { 41 kInvalid = -1, 42 kInit0 = 0, 43 kWriteHeader10 = 10, 44 kWriteIncremental15 = 15, 45 kInitWriteObjs20 = 20, 46 kWriteOldObjs21 = 21, 47 kInitWriteNewObjs25 = 25, 48 kWriteNewObjs26 = 26, 49 kWriteEncryptDict27 = 27, 50 kInitWriteXRefs80 = 80, 51 kWriteXrefsNotIncremental81 = 81, 52 kWriteXrefsIncremental82 = 82, 53 kWriteTrailerAndFinish90 = 90, 54 kComplete100 = 100, 55 }; 56 57 bool Continue(); 58 void Clear(); 59 60 void InitNewObjNumOffsets(); 61 void InitID(); 62 63 CPDF_Creator::Stage WriteDoc_Stage1(); 64 CPDF_Creator::Stage WriteDoc_Stage2(); 65 CPDF_Creator::Stage WriteDoc_Stage3(); 66 CPDF_Creator::Stage WriteDoc_Stage4(); 67 68 bool WriteOldIndirectObject(uint32_t objnum); 69 bool WriteOldObjs(); 70 bool WriteNewObjs(); 71 bool WriteIndirectObj(uint32_t objnum, const CPDF_Object* pObj); 72 73 CPDF_CryptoHandler* GetCryptoHandler(); 74 75 UnownedPtr<CPDF_Document> const m_pDocument; 76 UnownedPtr<const CPDF_Parser> const m_pParser; 77 RetainPtr<const CPDF_Dictionary> m_pEncryptDict; 78 RetainPtr<CPDF_Dictionary> m_pNewEncryptDict; 79 RetainPtr<CPDF_SecurityHandler> m_pSecurityHandler; 80 RetainPtr<const CPDF_Object> m_pMetadata; 81 uint32_t m_dwLastObjNum; 82 std::unique_ptr<IFX_ArchiveStream> m_Archive; 83 FX_FILESIZE m_SavedOffset = 0; 84 Stage m_iStage = Stage::kInvalid; 85 uint32_t m_CurObjNum = 0; 86 FX_FILESIZE m_XrefStart = 0; 87 std::map<uint32_t, FX_FILESIZE> m_ObjectOffsets; 88 std::vector<uint32_t> m_NewObjNumArray; // Sorted, ascending. 89 RetainPtr<CPDF_Array> m_pIDArray; 90 int32_t m_FileVersion = 0; 91 bool m_bSecurityChanged = false; 92 bool m_IsIncremental = false; 93 bool m_IsOriginal = false; 94 }; 95 96 #endif // CORE_FPDFAPI_EDIT_CPDF_CREATOR_H_ 97