// Copyright 2016 The PDFium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com #ifndef CORE_FPDFAPI_PARSER_CPDF_DOCUMENT_H_ #define CORE_FPDFAPI_PARSER_CPDF_DOCUMENT_H_ #include #include #include #include #include "core/fpdfapi/parser/cpdf_dictionary.h" #include "core/fpdfapi/parser/cpdf_parser.h" #include "core/fxcrt/fx_memory.h" #include "core/fxcrt/observed_ptr.h" #include "core/fxcrt/retain_ptr.h" #include "core/fxcrt/span.h" #include "core/fxcrt/unowned_ptr.h" class CPDF_ReadValidator; class CPDF_StreamAcc; class IFX_SeekableReadStream; class JBig2_DocumentContext; class CPDF_Document : public Observable, public CPDF_Parser::ParsedObjectsHolder { public: // Type from which the XFA extension can subclass itself. class Extension { public: virtual ~Extension() = default; virtual int GetPageCount() const = 0; virtual uint32_t DeletePage(int page_index) = 0; virtual bool ContainsExtensionForm() const = 0; virtual bool ContainsExtensionFullForm() const = 0; virtual bool ContainsExtensionForegroundForm() const = 0; }; class LinkListIface { public: // CPDF_Document merely helps manage the lifetime. virtual ~LinkListIface() = default; }; class PageDataIface { public: PageDataIface(); virtual ~PageDataIface(); virtual void ClearStockFont() = 0; virtual RetainPtr GetFontFileStreamAcc( RetainPtr pFontStream) = 0; virtual void MaybePurgeFontFileStreamAcc( RetainPtr&& pStreamAcc) = 0; virtual void MaybePurgeImage(uint32_t objnum) = 0; void SetDocument(CPDF_Document* pDoc) { m_pDoc = pDoc; } protected: CPDF_Document* GetDocument() const { return m_pDoc; } private: UnownedPtr m_pDoc; }; class RenderDataIface { public: RenderDataIface(); virtual ~RenderDataIface(); void SetDocument(CPDF_Document* pDoc) { m_pDoc = pDoc; } protected: CPDF_Document* GetDocument() const { return m_pDoc; } private: UnownedPtr m_pDoc; }; static constexpr int kPageMaxNum = 0xFFFFF; static bool IsValidPageObject(const CPDF_Object* obj); CPDF_Document(std::unique_ptr pRenderData, std::unique_ptr pPageData); ~CPDF_Document() override; Extension* GetExtension() const { return m_pExtension.get(); } void SetExtension(std::unique_ptr pExt) { m_pExtension = std::move(pExt); } CPDF_Parser* GetParser() const { return m_pParser.get(); } const CPDF_Dictionary* GetRoot() const { return m_pRootDict.Get(); } RetainPtr GetMutableRoot() { return m_pRootDict; } RetainPtr GetInfo(); RetainPtr GetFileIdentifier() const; // Returns the object number for the deleted page, or 0 on failure. uint32_t DeletePage(int iPage); // `page_obj_num` is the return value from DeletePage(). If it is non-zero, // and it is no longer used in the page tree, then replace the page object // with a null object. void SetPageToNullObject(uint32_t page_obj_num); bool MovePages(pdfium::span page_indices, int dest_page_index); int GetPageCount() const; bool IsPageLoaded(int iPage) const; RetainPtr GetPageDictionary(int iPage); RetainPtr GetMutablePageDictionary(int iPage); int GetPageIndex(uint32_t objnum); // When `get_owner_perms` is true, returns full permissions if unlocked by // owner. uint32_t GetUserPermissions(bool get_owner_perms) const; // PageDataIface wrappers, try to avoid explicit getter calls. RetainPtr GetFontFileStreamAcc( RetainPtr pFontStream); void MaybePurgeFontFileStreamAcc(RetainPtr&& pStreamAcc); void MaybePurgeImage(uint32_t objnum); // Returns a valid pointer, unless it is called during destruction. PageDataIface* GetPageData() const { return m_pDocPage.get(); } RenderDataIface* GetRenderData() const { return m_pDocRender.get(); } void SetPageObjNum(int iPage, uint32_t objNum); JBig2_DocumentContext* GetOrCreateCodecContext(); LinkListIface* GetLinksContext() const { return m_pLinksContext.get(); } void SetLinksContext(std::unique_ptr pContext) { m_pLinksContext = std::move(pContext); } // Behaves like NewIndirect(dict), but keeps track of the object // number assigned to the newly created stream. RetainPtr CreateModifiedAPStream( RetainPtr dict); // Returns whether CreateModifiedAPStream() created `stream`. bool IsModifiedAPStream(const CPDF_Stream* stream) const; // CPDF_Parser::ParsedObjectsHolder: bool TryInit() override; RetainPtr ParseIndirectObject(uint32_t objnum) override; CPDF_Parser::Error LoadDoc(RetainPtr pFileAccess, const ByteString& password); CPDF_Parser::Error LoadLinearizedDoc(RetainPtr validator, const ByteString& password); bool has_valid_cross_reference_table() const { return m_bHasValidCrossReferenceTable; } void LoadPages(); void CreateNewDoc(); RetainPtr CreateNewPage(int iPage); void IncrementParsedPageCount() { ++m_ParsedPageCount; } uint32_t GetParsedPageCountForTesting() { return m_ParsedPageCount; } void SetRootForTesting(RetainPtr root); protected: void SetParser(std::unique_ptr pParser); void ResizePageListForTesting(size_t size); private: class StockFontClearer { public: FX_STACK_ALLOCATED(); explicit StockFontClearer(CPDF_Document::PageDataIface* pPageData); ~StockFontClearer(); private: UnownedPtr const m_pPageData; }; // Retrieve page count information by getting count value from the tree nodes int RetrievePageCount(); // When this method is called, m_pTreeTraversal[level] exists. RetainPtr TraversePDFPages(int iPage, int* nPagesToGo, size_t level); RetainPtr GetPagesDict() const; RetainPtr GetMutablePagesDict(); bool InsertDeletePDFPage(RetainPtr pages_dict, int pages_to_go, RetainPtr page_dict, bool is_insert, std::set>* visited); bool InsertNewPage(int iPage, RetainPtr pPageDict); void ResetTraversal(); CPDF_Parser::Error HandleLoadResult(CPDF_Parser::Error error); std::unique_ptr m_pParser; RetainPtr m_pRootDict; RetainPtr m_pInfoDict; // Vector of pairs to know current position in the page tree. The index in the // vector corresponds to the level being described. The pair contains a // pointer to the dictionary being processed at the level, and an index of the // of the child being processed within the dictionary's /Kids array. std::vector, size_t>> m_pTreeTraversal; // True if the CPDF_Parser succeeded without having to rebuild the cross // reference table. bool m_bHasValidCrossReferenceTable = false; // Index of the next page that will be traversed from the page tree. bool m_bReachedMaxPageLevel = false; int m_iNextPageToTraverse = 0; uint32_t m_ParsedPageCount = 0; std::unique_ptr const m_pDocRender; // Must be after `m_pDocRender`. std::unique_ptr const m_pDocPage; std::unique_ptr m_pCodecContext; std::unique_ptr m_pLinksContext; std::set m_ModifiedAPStreamIDs; std::vector m_PageList; // Page number to page's dict objnum. // Must be second to last. StockFontClearer m_StockFontClearer; // Must be last. Destroy the extension before any non-extension teardown. std::unique_ptr m_pExtension; }; #endif // CORE_FPDFAPI_PARSER_CPDF_DOCUMENT_H_