From c68b1e73ce8210e2b33491da160dda5dd48599c0 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Mon, 8 May 2017 16:59:54 -0400 Subject: Remove AppendObject from CPDF_Creator The AppendObject method has been removed and the functionality moved to the individual CPDF_Object classes. Change-Id: I5446c6cc3e792d849acf77caed34b63a88f3a2d2 Reviewed-on: https://pdfium-review.googlesource.com/5072 Commit-Queue: dsinclair Reviewed-by: Tom Sepez --- core/fpdfapi/parser/cpdf_array.cpp | 29 ++++++++++++++++++++++++ core/fpdfapi/parser/cpdf_array.h | 2 ++ core/fpdfapi/parser/cpdf_boolean.cpp | 12 ++++++++++ core/fpdfapi/parser/cpdf_boolean.h | 2 ++ core/fpdfapi/parser/cpdf_dictionary.cpp | 39 +++++++++++++++++++++++++++++++++ core/fpdfapi/parser/cpdf_dictionary.h | 2 ++ core/fpdfapi/parser/cpdf_name.cpp | 13 +++++++++++ core/fpdfapi/parser/cpdf_name.h | 2 ++ core/fpdfapi/parser/cpdf_null.cpp | 8 +++++++ core/fpdfapi/parser/cpdf_null.h | 2 ++ core/fpdfapi/parser/cpdf_number.cpp | 12 ++++++++++ core/fpdfapi/parser/cpdf_number.h | 2 ++ core/fpdfapi/parser/cpdf_object.h | 4 ++++ core/fpdfapi/parser/cpdf_reference.cpp | 13 +++++++++++ core/fpdfapi/parser/cpdf_reference.h | 2 ++ core/fpdfapi/parser/cpdf_stream.cpp | 22 +++++++++++++++++++ core/fpdfapi/parser/cpdf_stream.h | 2 ++ core/fpdfapi/parser/cpdf_string.cpp | 11 ++++++++++ core/fpdfapi/parser/cpdf_string.h | 2 ++ 19 files changed, 181 insertions(+) (limited to 'core/fpdfapi/parser') diff --git a/core/fpdfapi/parser/cpdf_array.cpp b/core/fpdfapi/parser/cpdf_array.cpp index f3c23f37be..aa10588f87 100644 --- a/core/fpdfapi/parser/cpdf_array.cpp +++ b/core/fpdfapi/parser/cpdf_array.cpp @@ -193,3 +193,32 @@ CPDF_Object* CPDF_Array::Add(std::unique_ptr pObj) { m_Objects.push_back(std::move(pObj)); return pRet; } + +bool CPDF_Array::WriteTo(CFX_FileBufferArchive* archive, + FX_FILESIZE* offset) const { + if (archive->AppendString("[") < 0) + return false; + *offset += 1; + + for (size_t i = 0; i < GetCount(); ++i) { + CPDF_Object* pElement = GetObjectAt(i); + if (!pElement->IsInline()) { + if (archive->AppendString(" ") < 0) + return false; + + int32_t len = archive->AppendDWord(pElement->GetObjNum()); + if (len < 0) + return false; + if (archive->AppendString(" 0 R") < 0) + return false; + *offset += len + 5; + } else { + if (!pElement->WriteTo(archive, offset)) + return false; + } + } + if (archive->AppendString("]") < 0) + return false; + *offset += 1; + return true; +} diff --git a/core/fpdfapi/parser/cpdf_array.h b/core/fpdfapi/parser/cpdf_array.h index 8f8b600c7e..b788d1c075 100644 --- a/core/fpdfapi/parser/cpdf_array.h +++ b/core/fpdfapi/parser/cpdf_array.h @@ -34,6 +34,8 @@ class CPDF_Array : public CPDF_Object { bool IsArray() const override; CPDF_Array* AsArray() override; const CPDF_Array* AsArray() const override; + bool WriteTo(CFX_FileBufferArchive* archive, + FX_FILESIZE* offset) const override; bool IsEmpty() const { return m_Objects.empty(); } size_t GetCount() const { return m_Objects.size(); } diff --git a/core/fpdfapi/parser/cpdf_boolean.cpp b/core/fpdfapi/parser/cpdf_boolean.cpp index 0204fd9eb0..1cee3a3a49 100644 --- a/core/fpdfapi/parser/cpdf_boolean.cpp +++ b/core/fpdfapi/parser/cpdf_boolean.cpp @@ -44,3 +44,15 @@ CPDF_Boolean* CPDF_Boolean::AsBoolean() { const CPDF_Boolean* CPDF_Boolean::AsBoolean() const { return this; } + +bool CPDF_Boolean::WriteTo(CFX_FileBufferArchive* archive, + FX_FILESIZE* offset) const { + if (archive->AppendString(" ") < 0) + return false; + + int32_t len = archive->AppendString(GetString().AsStringC()); + if (len < 0) + return false; + *offset += len + 1; + return true; +} diff --git a/core/fpdfapi/parser/cpdf_boolean.h b/core/fpdfapi/parser/cpdf_boolean.h index afebc29448..e859f4cf9c 100644 --- a/core/fpdfapi/parser/cpdf_boolean.h +++ b/core/fpdfapi/parser/cpdf_boolean.h @@ -28,6 +28,8 @@ class CPDF_Boolean : public CPDF_Object { bool IsBoolean() const override; CPDF_Boolean* AsBoolean() override; const CPDF_Boolean* AsBoolean() const override; + bool WriteTo(CFX_FileBufferArchive* archive, + FX_FILESIZE* offset) const override; protected: bool m_bValue; diff --git a/core/fpdfapi/parser/cpdf_dictionary.cpp b/core/fpdfapi/parser/cpdf_dictionary.cpp index d4e4080d31..7d84083829 100644 --- a/core/fpdfapi/parser/cpdf_dictionary.cpp +++ b/core/fpdfapi/parser/cpdf_dictionary.cpp @@ -16,6 +16,7 @@ #include "core/fpdfapi/parser/cpdf_reference.h" #include "core/fpdfapi/parser/cpdf_stream.h" #include "core/fpdfapi/parser/cpdf_string.h" +#include "core/fpdfapi/parser/fpdf_parser_decode.h" #include "third_party/base/logging.h" #include "third_party/base/stl_util.h" @@ -237,3 +238,41 @@ void CPDF_Dictionary::SetMatrixFor(const CFX_ByteString& key, CFX_ByteString CPDF_Dictionary::MaybeIntern(const CFX_ByteString& str) { return m_pPool ? m_pPool->Intern(str) : str; } + +bool CPDF_Dictionary::WriteTo(CFX_FileBufferArchive* archive, + FX_FILESIZE* offset) const { + if (archive->AppendString("<<") < 0) + return false; + *offset += 2; + + for (const auto& it : *this) { + const CFX_ByteString& key = it.first; + CPDF_Object* pValue = it.second.get(); + if (archive->AppendString("/") < 0) + return false; + + int32_t len = archive->AppendString(PDF_NameEncode(key).AsStringC()); + if (len < 0) + return false; + *offset += len + 1; + + if (!pValue->IsInline()) { + if (archive->AppendString(" ") < 0) + return false; + + len = archive->AppendDWord(pValue->GetObjNum()); + if (len < 0) + return false; + if (archive->AppendString(" 0 R") < 0) + return false; + *offset += len + 5; + } else { + if (!pValue->WriteTo(archive, offset)) + return false; + } + } + if (archive->AppendString(">>") < 0) + return false; + *offset += 2; + return true; +} diff --git a/core/fpdfapi/parser/cpdf_dictionary.h b/core/fpdfapi/parser/cpdf_dictionary.h index b14574fd61..e4144c1524 100644 --- a/core/fpdfapi/parser/cpdf_dictionary.h +++ b/core/fpdfapi/parser/cpdf_dictionary.h @@ -37,6 +37,8 @@ class CPDF_Dictionary : public CPDF_Object { bool IsDictionary() const override; CPDF_Dictionary* AsDictionary() override; const CPDF_Dictionary* AsDictionary() const override; + bool WriteTo(CFX_FileBufferArchive* archive, + FX_FILESIZE* offset) const override; size_t GetCount() const { return m_Map.size(); } CPDF_Object* GetObjectFor(const CFX_ByteString& key) const; diff --git a/core/fpdfapi/parser/cpdf_name.cpp b/core/fpdfapi/parser/cpdf_name.cpp index 5be64d39d5..74c83b9d28 100644 --- a/core/fpdfapi/parser/cpdf_name.cpp +++ b/core/fpdfapi/parser/cpdf_name.cpp @@ -49,3 +49,16 @@ const CPDF_Name* CPDF_Name::AsName() const { CFX_WideString CPDF_Name::GetUnicodeText() const { return PDF_DecodeText(m_Name); } + +bool CPDF_Name::WriteTo(CFX_FileBufferArchive* archive, + FX_FILESIZE* offset) const { + if (archive->AppendString("/") < 0) + return false; + + CFX_ByteString str = GetString(); + int32_t len = archive->AppendString(PDF_NameEncode(str).AsStringC()); + if (len < 0) + return false; + *offset += len + 1; + return true; +} diff --git a/core/fpdfapi/parser/cpdf_name.h b/core/fpdfapi/parser/cpdf_name.h index 61318d4afb..8855f9785c 100644 --- a/core/fpdfapi/parser/cpdf_name.h +++ b/core/fpdfapi/parser/cpdf_name.h @@ -27,6 +27,8 @@ class CPDF_Name : public CPDF_Object { bool IsName() const override; CPDF_Name* AsName() override; const CPDF_Name* AsName() const override; + bool WriteTo(CFX_FileBufferArchive* archive, + FX_FILESIZE* offset) const override; protected: CFX_ByteString m_Name; diff --git a/core/fpdfapi/parser/cpdf_null.cpp b/core/fpdfapi/parser/cpdf_null.cpp index 41478d7b4c..b5e07e0c55 100644 --- a/core/fpdfapi/parser/cpdf_null.cpp +++ b/core/fpdfapi/parser/cpdf_null.cpp @@ -16,3 +16,11 @@ CPDF_Object::Type CPDF_Null::GetType() const { std::unique_ptr CPDF_Null::Clone() const { return pdfium::MakeUnique(); } + +bool CPDF_Null::WriteTo(CFX_FileBufferArchive* archive, + FX_FILESIZE* offset) const { + if (archive->AppendString(" null") < 0) + return false; + *offset += 5; + return true; +} diff --git a/core/fpdfapi/parser/cpdf_null.h b/core/fpdfapi/parser/cpdf_null.h index 4f8420f29d..b6b781b01a 100644 --- a/core/fpdfapi/parser/cpdf_null.h +++ b/core/fpdfapi/parser/cpdf_null.h @@ -18,6 +18,8 @@ class CPDF_Null : public CPDF_Object { // CPDF_Object. Type GetType() const override; std::unique_ptr Clone() const override; + bool WriteTo(CFX_FileBufferArchive* archive, + FX_FILESIZE* offset) const override; }; #endif // CORE_FPDFAPI_PARSER_CPDF_NULL_H_ diff --git a/core/fpdfapi/parser/cpdf_number.cpp b/core/fpdfapi/parser/cpdf_number.cpp index c83b9dcd38..24665fff60 100644 --- a/core/fpdfapi/parser/cpdf_number.cpp +++ b/core/fpdfapi/parser/cpdf_number.cpp @@ -55,3 +55,15 @@ CFX_ByteString CPDF_Number::GetString() const { return m_bInteger ? CFX_ByteString::FormatInteger(m_Integer, FXFORMAT_SIGNED) : CFX_ByteString::FormatFloat(m_Float); } + +bool CPDF_Number::WriteTo(CFX_FileBufferArchive* archive, + FX_FILESIZE* offset) const { + if (archive->AppendString(" ") < 0) + return false; + + int32_t len = archive->AppendString(GetString().AsStringC()); + if (len < 0) + return false; + *offset += len + 1; + return true; +} diff --git a/core/fpdfapi/parser/cpdf_number.h b/core/fpdfapi/parser/cpdf_number.h index 6e85044d2a..a5d866f835 100644 --- a/core/fpdfapi/parser/cpdf_number.h +++ b/core/fpdfapi/parser/cpdf_number.h @@ -31,6 +31,8 @@ class CPDF_Number : public CPDF_Object { bool IsNumber() const override; CPDF_Number* AsNumber() override; const CPDF_Number* AsNumber() const override; + bool WriteTo(CFX_FileBufferArchive* archive, + FX_FILESIZE* offset) const override; bool IsInteger() const { return m_bInteger; } diff --git a/core/fpdfapi/parser/cpdf_object.h b/core/fpdfapi/parser/cpdf_object.h index 616573724b..8194256f5a 100644 --- a/core/fpdfapi/parser/cpdf_object.h +++ b/core/fpdfapi/parser/cpdf_object.h @@ -11,6 +11,7 @@ #include #include +#include "core/fxcrt/fx_basic.h" #include "core/fxcrt/fx_string.h" #include "core/fxcrt/fx_system.h" @@ -88,6 +89,9 @@ class CPDF_Object { virtual CPDF_String* AsString(); virtual const CPDF_String* AsString() const; + virtual bool WriteTo(CFX_FileBufferArchive* archive, + FX_FILESIZE* offset) const = 0; + protected: friend class CPDF_Array; friend class CPDF_Dictionary; diff --git a/core/fpdfapi/parser/cpdf_reference.cpp b/core/fpdfapi/parser/cpdf_reference.cpp index 942bae58ba..560fd27934 100644 --- a/core/fpdfapi/parser/cpdf_reference.cpp +++ b/core/fpdfapi/parser/cpdf_reference.cpp @@ -82,3 +82,16 @@ CPDF_Object* CPDF_Reference::GetDirect() const { return m_pObjList ? m_pObjList->GetOrParseIndirectObject(m_RefObjNum) : nullptr; } + +bool CPDF_Reference::WriteTo(CFX_FileBufferArchive* archive, + FX_FILESIZE* offset) const { + if (archive->AppendString(" ") < 0) + return false; + int32_t len = archive->AppendDWord(GetRefObjNum()); + if (len < 0) + return false; + if (archive->AppendString(" 0 R ") < 0) + return false; + *offset += len + 6; + return true; +} diff --git a/core/fpdfapi/parser/cpdf_reference.h b/core/fpdfapi/parser/cpdf_reference.h index ff8875572c..4830cb5bf6 100644 --- a/core/fpdfapi/parser/cpdf_reference.h +++ b/core/fpdfapi/parser/cpdf_reference.h @@ -30,6 +30,8 @@ class CPDF_Reference : public CPDF_Object { bool IsReference() const override; CPDF_Reference* AsReference() override; const CPDF_Reference* AsReference() const override; + bool WriteTo(CFX_FileBufferArchive* archive, + FX_FILESIZE* offset) const override; CPDF_IndirectObjectHolder* GetObjList() const { return m_pObjList; } uint32_t GetRefObjNum() const { return m_RefObjNum; } diff --git a/core/fpdfapi/parser/cpdf_stream.cpp b/core/fpdfapi/parser/cpdf_stream.cpp index e4f279a32d..7e2529e1d7 100644 --- a/core/fpdfapi/parser/cpdf_stream.cpp +++ b/core/fpdfapi/parser/cpdf_stream.cpp @@ -131,3 +131,25 @@ CFX_WideString CPDF_Stream::GetUnicodeText() const { pAcc->LoadAllData(false); return PDF_DecodeText(pAcc->GetData(), pAcc->GetSize()); } + +bool CPDF_Stream::WriteTo(CFX_FileBufferArchive* archive, + FX_FILESIZE* offset) const { + if (!GetDict()->WriteTo(archive, offset)) + return false; + if (archive->AppendString("stream\r\n") < 0) + return false; + *offset += 8; + + auto pAcc = pdfium::MakeRetain(this); + pAcc->LoadAllData(true); + if (archive->AppendBlock(pAcc->GetData(), pAcc->GetSize()) < 0) + return false; + *offset += pAcc->GetSize(); + + int32_t len = archive->AppendString("\r\nendstream"); + if (len < 0) + return false; + + *offset += len; + return true; +} diff --git a/core/fpdfapi/parser/cpdf_stream.h b/core/fpdfapi/parser/cpdf_stream.h index 902cd75365..4f76953efe 100644 --- a/core/fpdfapi/parser/cpdf_stream.h +++ b/core/fpdfapi/parser/cpdf_stream.h @@ -33,6 +33,8 @@ class CPDF_Stream : public CPDF_Object { bool IsStream() const override; CPDF_Stream* AsStream() override; const CPDF_Stream* AsStream() const override; + bool WriteTo(CFX_FileBufferArchive* archive, + FX_FILESIZE* offset) const override; uint32_t GetRawSize() const { return m_dwSize; } uint8_t* GetRawData() const { return m_pDataBuf.get(); } diff --git a/core/fpdfapi/parser/cpdf_string.cpp b/core/fpdfapi/parser/cpdf_string.cpp index 06bae54916..01cf3ff1b0 100644 --- a/core/fpdfapi/parser/cpdf_string.cpp +++ b/core/fpdfapi/parser/cpdf_string.cpp @@ -64,3 +64,14 @@ const CPDF_String* CPDF_String::AsString() const { CFX_WideString CPDF_String::GetUnicodeText() const { return PDF_DecodeText(m_String); } + +bool CPDF_String::WriteTo(CFX_FileBufferArchive* archive, + FX_FILESIZE* offset) const { + CFX_ByteString str = GetString(); + int32_t len = + archive->AppendString(PDF_EncodeString(str, IsHex()).AsStringC()); + if (len < 0) + return false; + *offset += len; + return true; +} diff --git a/core/fpdfapi/parser/cpdf_string.h b/core/fpdfapi/parser/cpdf_string.h index 6698d8c5be..f895535a92 100644 --- a/core/fpdfapi/parser/cpdf_string.h +++ b/core/fpdfapi/parser/cpdf_string.h @@ -33,6 +33,8 @@ class CPDF_String : public CPDF_Object { bool IsString() const override; CPDF_String* AsString() override; const CPDF_String* AsString() const override; + bool WriteTo(CFX_FileBufferArchive* archive, + FX_FILESIZE* offset) const override; bool IsHex() const { return m_bHex; } -- cgit v1.2.3