From 20f22a0a38a6c6d9ccd0ead2e65093e79f0bd051 Mon Sep 17 00:00:00 2001 From: Henrique Nakashima Date: Wed, 8 Nov 2017 16:19:50 +0000 Subject: Fix FPDF_SaveAsCopy for linearized PDFs. Only the last trailer was written to the new PDF. In linearized PDFs, this means Info and Root were not copied to the newly written PDF. This CL makes CPDF_Parser remember the Root and provide that to CPDF_Creator. Bug: pdfium:614 Change-Id: Ia61f5f6a337f7de3010ee0ed39b022c1b2732ea2 Reviewed-on: https://pdfium-review.googlesource.com/17987 Reviewed-by: dsinclair Commit-Queue: Henrique Nakashima --- core/fpdfapi/parser/cpdf_parser.cpp | 40 +++++++++++++++++++++++++++++++------ core/fpdfapi/parser/cpdf_parser.h | 6 ++++++ 2 files changed, 40 insertions(+), 6 deletions(-) (limited to 'core/fpdfapi/parser') diff --git a/core/fpdfapi/parser/cpdf_parser.cpp b/core/fpdfapi/parser/cpdf_parser.cpp index d02989000c..5b57b949ba 100644 --- a/core/fpdfapi/parser/cpdf_parser.cpp +++ b/core/fpdfapi/parser/cpdf_parser.cpp @@ -61,6 +61,20 @@ class CPDF_Parser::TrailerData { CPDF_Dictionary* GetMainTrailer() const { return main_trailer_.get(); } + std::unique_ptr GetCombinedTrailer() const { + std::unique_ptr result = + ToDictionary(main_trailer_->Clone()); + + // Info is optional. + uint32_t info_obj_num = GetInfoObjNum(); + if (info_obj_num > 0) + result->SetNewFor("Info", nullptr, GetInfoObjNum()); + + // Root is required. + result->SetNewFor("Root", nullptr, GetRootObjNum()); + return result; + } + void SetMainTrailer(std::unique_ptr trailer) { ASSERT(trailer); main_trailer_ = std::move(trailer); @@ -75,6 +89,7 @@ class CPDF_Parser::TrailerData { void Clear() { main_trailer_.reset(); last_info_obj_num_ = 0; + last_root_obj_num_ = 0; } uint32_t GetInfoObjNum() const { @@ -83,6 +98,12 @@ class CPDF_Parser::TrailerData { return pRef ? pRef->GetRefObjNum() : last_info_obj_num_; } + uint32_t GetRootObjNum() const { + const CPDF_Reference* pRef = ToReference( + GetMainTrailer() ? GetMainTrailer()->GetObjectFor("Root") : nullptr); + return pRef ? pRef->GetRefObjNum() : last_root_obj_num_; + } + private: void ApplyTrailer(const CPDF_Dictionary* dict) { // The most recent Info object number contained in last added trailer. @@ -90,10 +111,15 @@ class CPDF_Parser::TrailerData { const auto* pRef = ToReference(dict->GetObjectFor("Info")); if (pRef) last_info_obj_num_ = pRef->GetRefObjNum(); + + const auto* pRoot = ToReference(dict->GetObjectFor("Root")); + if (pRoot) + last_root_obj_num_ = pRoot->GetRefObjNum(); } std::unique_ptr main_trailer_; uint32_t last_info_obj_num_ = 0; + uint32_t last_root_obj_num_ = 0; }; CPDF_Parser::CPDF_Parser() @@ -1145,20 +1171,22 @@ const CPDF_Array* CPDF_Parser::GetIDArray() const { return GetTrailer() ? GetTrailer()->GetArrayFor("ID") : nullptr; } -uint32_t CPDF_Parser::GetRootObjNum() { - CPDF_Reference* pRef = - ToReference(GetTrailer() ? GetTrailer()->GetObjectFor("Root") : nullptr); - return pRef ? pRef->GetRefObjNum() : 0; -} - CPDF_Dictionary* CPDF_Parser::GetTrailer() const { return m_TrailerData->GetMainTrailer(); } +std::unique_ptr CPDF_Parser::GetCombinedTrailer() const { + return m_TrailerData->GetCombinedTrailer(); +} + uint32_t CPDF_Parser::GetInfoObjNum() { return m_TrailerData->GetInfoObjNum(); } +uint32_t CPDF_Parser::GetRootObjNum() { + return m_TrailerData->GetRootObjNum(); +} + std::unique_ptr CPDF_Parser::ParseIndirectObject( CPDF_IndirectObjectHolder* pObjList, uint32_t objnum) { diff --git a/core/fpdfapi/parser/cpdf_parser.h b/core/fpdfapi/parser/cpdf_parser.h index 375bc01621..45b8cb9f2e 100644 --- a/core/fpdfapi/parser/cpdf_parser.h +++ b/core/fpdfapi/parser/cpdf_parser.h @@ -57,7 +57,13 @@ class CPDF_Parser { void SetPassword(const char* password) { m_Password = password; } ByteString GetPassword() { return m_Password; } + CPDF_Dictionary* GetTrailer() const; + + // Returns a new trailer which combines the last read trailer with the /Root + // and /Info from previous ones. + std::unique_ptr GetCombinedTrailer() const; + FX_FILESIZE GetLastXRefOffset() const { return m_LastXRefOffset; } uint32_t GetPermissions() const; -- cgit v1.2.3