From 5b590337e0778b49dd7092af4a283ed0f9c5a2e9 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Wed, 10 May 2017 13:59:14 -0400 Subject: Store the offset in the archive buffer This Cl moves the implementation of the archive buffer behind an IFX_ArchiveStream interface. The buffer holds the current offset and the offset parameter is removed from the CPDF_Creator and various other methods. Change-Id: Ia54e803b58bbfb6ef03fec4a940d2c056d541356 Reviewed-on: https://pdfium-review.googlesource.com/5255 Reviewed-by: Tom Sepez Commit-Queue: dsinclair --- core/fpdfapi/parser/cpdf_array.cpp | 28 ++++++++----------------- core/fpdfapi/parser/cpdf_array.h | 3 +-- core/fpdfapi/parser/cpdf_boolean.cpp | 13 +++--------- core/fpdfapi/parser/cpdf_boolean.h | 3 +-- core/fpdfapi/parser/cpdf_dictionary.cpp | 37 +++++++++++---------------------- core/fpdfapi/parser/cpdf_dictionary.h | 3 +-- core/fpdfapi/parser/cpdf_name.cpp | 14 +++---------- core/fpdfapi/parser/cpdf_name.h | 3 +-- core/fpdfapi/parser/cpdf_null.cpp | 8 ++----- core/fpdfapi/parser/cpdf_null.h | 3 +-- core/fpdfapi/parser/cpdf_number.cpp | 13 +++--------- core/fpdfapi/parser/cpdf_number.h | 3 +-- core/fpdfapi/parser/cpdf_object.h | 3 +-- core/fpdfapi/parser/cpdf_reference.cpp | 14 +++---------- core/fpdfapi/parser/cpdf_reference.h | 3 +-- core/fpdfapi/parser/cpdf_stream.cpp | 20 ++++-------------- core/fpdfapi/parser/cpdf_stream.h | 3 +-- core/fpdfapi/parser/cpdf_string.cpp | 12 +++-------- core/fpdfapi/parser/cpdf_string.h | 3 +-- 19 files changed, 52 insertions(+), 137 deletions(-) (limited to 'core/fpdfapi/parser') diff --git a/core/fpdfapi/parser/cpdf_array.cpp b/core/fpdfapi/parser/cpdf_array.cpp index aa10588f87..698cd6d0d0 100644 --- a/core/fpdfapi/parser/cpdf_array.cpp +++ b/core/fpdfapi/parser/cpdf_array.cpp @@ -194,31 +194,21 @@ CPDF_Object* CPDF_Array::Add(std::unique_ptr pObj) { return pRet; } -bool CPDF_Array::WriteTo(CFX_FileBufferArchive* archive, - FX_FILESIZE* offset) const { - if (archive->AppendString("[") < 0) +bool CPDF_Array::WriteTo(IFX_ArchiveStream* archive) const { + if (!archive->WriteString("[")) 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)) + if (!archive->WriteString(" ") || + !archive->WriteDWord(pElement->GetObjNum()) || + !archive->WriteString(" 0 R")) { return false; + } + } else if (!pElement->WriteTo(archive)) { + return false; } } - if (archive->AppendString("]") < 0) - return false; - *offset += 1; - return true; + return archive->WriteString("]"); } diff --git a/core/fpdfapi/parser/cpdf_array.h b/core/fpdfapi/parser/cpdf_array.h index b788d1c075..aff65d1454 100644 --- a/core/fpdfapi/parser/cpdf_array.h +++ b/core/fpdfapi/parser/cpdf_array.h @@ -34,8 +34,7 @@ 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 WriteTo(IFX_ArchiveStream* archive) 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 1cee3a3a49..eb01c06332 100644 --- a/core/fpdfapi/parser/cpdf_boolean.cpp +++ b/core/fpdfapi/parser/cpdf_boolean.cpp @@ -45,14 +45,7 @@ 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; +bool CPDF_Boolean::WriteTo(IFX_ArchiveStream* archive) const { + return archive->WriteString(" ") && + archive->WriteString(GetString().AsStringC()); } diff --git a/core/fpdfapi/parser/cpdf_boolean.h b/core/fpdfapi/parser/cpdf_boolean.h index e859f4cf9c..2dd1486b6a 100644 --- a/core/fpdfapi/parser/cpdf_boolean.h +++ b/core/fpdfapi/parser/cpdf_boolean.h @@ -28,8 +28,7 @@ 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; + bool WriteTo(IFX_ArchiveStream* archive) const override; protected: bool m_bValue; diff --git a/core/fpdfapi/parser/cpdf_dictionary.cpp b/core/fpdfapi/parser/cpdf_dictionary.cpp index 7d84083829..cbefb5d56d 100644 --- a/core/fpdfapi/parser/cpdf_dictionary.cpp +++ b/core/fpdfapi/parser/cpdf_dictionary.cpp @@ -239,40 +239,27 @@ 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) +bool CPDF_Dictionary::WriteTo(IFX_ArchiveStream* archive) const { + if (!archive->WriteString("<<")) 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) + if (!archive->WriteString("/") || + !archive->WriteString(PDF_NameEncode(key).AsStringC())) { 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)) + if (!archive->WriteString(" ") || + !archive->WriteDWord(pValue->GetObjNum()) || + !archive->WriteString(" 0 R")) { return false; + } + } else if (!pValue->WriteTo(archive)) { + return false; } } - if (archive->AppendString(">>") < 0) - return false; - *offset += 2; - return true; + return archive->WriteString(">>"); } diff --git a/core/fpdfapi/parser/cpdf_dictionary.h b/core/fpdfapi/parser/cpdf_dictionary.h index e4144c1524..f741299d2d 100644 --- a/core/fpdfapi/parser/cpdf_dictionary.h +++ b/core/fpdfapi/parser/cpdf_dictionary.h @@ -37,8 +37,7 @@ 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; + bool WriteTo(IFX_ArchiveStream* archive) 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 74c83b9d28..0287b7ab17 100644 --- a/core/fpdfapi/parser/cpdf_name.cpp +++ b/core/fpdfapi/parser/cpdf_name.cpp @@ -50,15 +50,7 @@ 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; +bool CPDF_Name::WriteTo(IFX_ArchiveStream* archive) const { + return archive->WriteString("/") && + archive->WriteString(PDF_NameEncode(GetString()).AsStringC()); } diff --git a/core/fpdfapi/parser/cpdf_name.h b/core/fpdfapi/parser/cpdf_name.h index 8855f9785c..616f35f9bc 100644 --- a/core/fpdfapi/parser/cpdf_name.h +++ b/core/fpdfapi/parser/cpdf_name.h @@ -27,8 +27,7 @@ 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; + bool WriteTo(IFX_ArchiveStream* archive) const override; protected: CFX_ByteString m_Name; diff --git a/core/fpdfapi/parser/cpdf_null.cpp b/core/fpdfapi/parser/cpdf_null.cpp index b5e07e0c55..254c86fd7a 100644 --- a/core/fpdfapi/parser/cpdf_null.cpp +++ b/core/fpdfapi/parser/cpdf_null.cpp @@ -17,10 +17,6 @@ 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; +bool CPDF_Null::WriteTo(IFX_ArchiveStream* archive) const { + return archive->WriteString(" null"); } diff --git a/core/fpdfapi/parser/cpdf_null.h b/core/fpdfapi/parser/cpdf_null.h index b6b781b01a..92917281fb 100644 --- a/core/fpdfapi/parser/cpdf_null.h +++ b/core/fpdfapi/parser/cpdf_null.h @@ -18,8 +18,7 @@ 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; + bool WriteTo(IFX_ArchiveStream* archive) 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 24665fff60..73d689fffe 100644 --- a/core/fpdfapi/parser/cpdf_number.cpp +++ b/core/fpdfapi/parser/cpdf_number.cpp @@ -56,14 +56,7 @@ CFX_ByteString CPDF_Number::GetString() const { : 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; +bool CPDF_Number::WriteTo(IFX_ArchiveStream* archive) const { + return archive->WriteString(" ") && + archive->WriteString(GetString().AsStringC()); } diff --git a/core/fpdfapi/parser/cpdf_number.h b/core/fpdfapi/parser/cpdf_number.h index a5d866f835..57f82274cb 100644 --- a/core/fpdfapi/parser/cpdf_number.h +++ b/core/fpdfapi/parser/cpdf_number.h @@ -31,8 +31,7 @@ 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 WriteTo(IFX_ArchiveStream* archive) 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 8194256f5a..b0c1adf955 100644 --- a/core/fpdfapi/parser/cpdf_object.h +++ b/core/fpdfapi/parser/cpdf_object.h @@ -89,8 +89,7 @@ class CPDF_Object { virtual CPDF_String* AsString(); virtual const CPDF_String* AsString() const; - virtual bool WriteTo(CFX_FileBufferArchive* archive, - FX_FILESIZE* offset) const = 0; + virtual bool WriteTo(IFX_ArchiveStream* archive) const = 0; protected: friend class CPDF_Array; diff --git a/core/fpdfapi/parser/cpdf_reference.cpp b/core/fpdfapi/parser/cpdf_reference.cpp index 560fd27934..ff0442b6f5 100644 --- a/core/fpdfapi/parser/cpdf_reference.cpp +++ b/core/fpdfapi/parser/cpdf_reference.cpp @@ -83,15 +83,7 @@ CPDF_Object* CPDF_Reference::GetDirect() const { : 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; +bool CPDF_Reference::WriteTo(IFX_ArchiveStream* archive) const { + return archive->WriteString(" ") && archive->WriteDWord(GetRefObjNum()) && + archive->WriteString(" 0 R "); } diff --git a/core/fpdfapi/parser/cpdf_reference.h b/core/fpdfapi/parser/cpdf_reference.h index 4830cb5bf6..83bb0b48e7 100644 --- a/core/fpdfapi/parser/cpdf_reference.h +++ b/core/fpdfapi/parser/cpdf_reference.h @@ -30,8 +30,7 @@ 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; + bool WriteTo(IFX_ArchiveStream* archive) 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 7e2529e1d7..69a87e6d2e 100644 --- a/core/fpdfapi/parser/cpdf_stream.cpp +++ b/core/fpdfapi/parser/cpdf_stream.cpp @@ -132,24 +132,12 @@ CFX_WideString CPDF_Stream::GetUnicodeText() const { return PDF_DecodeText(pAcc->GetData(), pAcc->GetSize()); } -bool CPDF_Stream::WriteTo(CFX_FileBufferArchive* archive, - FX_FILESIZE* offset) const { - if (!GetDict()->WriteTo(archive, offset)) +bool CPDF_Stream::WriteTo(IFX_ArchiveStream* archive) const { + if (!GetDict()->WriteTo(archive) || !archive->WriteString("stream\r\n")) 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; + return archive->WriteBlock(pAcc->GetData(), pAcc->GetSize()) && + archive->WriteString("\r\nendstream"); } diff --git a/core/fpdfapi/parser/cpdf_stream.h b/core/fpdfapi/parser/cpdf_stream.h index 4f76953efe..82cc01aef3 100644 --- a/core/fpdfapi/parser/cpdf_stream.h +++ b/core/fpdfapi/parser/cpdf_stream.h @@ -33,8 +33,7 @@ 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; + bool WriteTo(IFX_ArchiveStream* archive) 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 01cf3ff1b0..74a2733fc5 100644 --- a/core/fpdfapi/parser/cpdf_string.cpp +++ b/core/fpdfapi/parser/cpdf_string.cpp @@ -65,13 +65,7 @@ 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; +bool CPDF_String::WriteTo(IFX_ArchiveStream* archive) const { + return archive->WriteString( + PDF_EncodeString(GetString(), IsHex()).AsStringC()); } diff --git a/core/fpdfapi/parser/cpdf_string.h b/core/fpdfapi/parser/cpdf_string.h index f895535a92..ccd6f530c1 100644 --- a/core/fpdfapi/parser/cpdf_string.h +++ b/core/fpdfapi/parser/cpdf_string.h @@ -33,8 +33,7 @@ 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 WriteTo(IFX_ArchiveStream* archive) const override; bool IsHex() const { return m_bHex; } -- cgit v1.2.3