summaryrefslogtreecommitdiff
path: root/core/fpdfapi/edit/cpdf_objectstream.cpp
diff options
context:
space:
mode:
authorDan Sinclair <dsinclair@chromium.org>2017-05-09 12:36:08 -0400
committerChromium commit bot <commit-bot@chromium.org>2017-05-09 17:48:44 +0000
commita0b1954794b7a8a279ba00787628cc7f8d1419d9 (patch)
tree4556b78b9550dbb80baa52e0eee016395f223c4e /core/fpdfapi/edit/cpdf_objectstream.cpp
parente02b2bc896b0872ce1a9d0997946b07dc76b9f7b (diff)
downloadpdfium-a0b1954794b7a8a279ba00787628cc7f8d1419d9.tar.xz
Rename fpdf_edit_create.cpp to cpdf_creator.cpp
This Cl renames the fpdf_edit_create file to better match the cpdf_creator content. The CPDF_ObjectStream and CPDF_XRefStream code is moved out to their own .cpp files. Needed anonymous classes are split out as well. Change-Id: Ic83fb319ce28c816db82a4286e22032dc68811bc Reviewed-on: https://pdfium-review.googlesource.com/5171 Commit-Queue: dsinclair <dsinclair@chromium.org> Reviewed-by: Tom Sepez <tsepez@chromium.org> Reviewed-by: Nicolás Peña <npm@chromium.org>
Diffstat (limited to 'core/fpdfapi/edit/cpdf_objectstream.cpp')
-rw-r--r--core/fpdfapi/edit/cpdf_objectstream.cpp109
1 files changed, 109 insertions, 0 deletions
diff --git a/core/fpdfapi/edit/cpdf_objectstream.cpp b/core/fpdfapi/edit/cpdf_objectstream.cpp
new file mode 100644
index 0000000000..e3f6e4c8e9
--- /dev/null
+++ b/core/fpdfapi/edit/cpdf_objectstream.cpp
@@ -0,0 +1,109 @@
+// Copyright 2017 PDFium Authors. All rights reserved.
+// 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
+
+#include "core/fpdfapi/edit/cpdf_objectstream.h"
+
+#include "core/fpdfapi/edit/cpdf_creator.h"
+#include "core/fpdfapi/edit/cpdf_encryptor.h"
+#include "core/fpdfapi/edit/cpdf_flateencoder.h"
+#include "core/fpdfapi/parser/fpdf_parser_utility.h"
+
+namespace {
+
+const int kObjectStreamMaxLength = 256 * 1024;
+
+} // namespace
+
+CPDF_ObjectStream::CPDF_ObjectStream() : m_dwObjNum(0), m_index(0) {}
+
+CPDF_ObjectStream::~CPDF_ObjectStream() {}
+
+bool CPDF_ObjectStream::IsNotFull() const {
+ return m_Buffer.GetLength() < kObjectStreamMaxLength;
+}
+
+void CPDF_ObjectStream::Start() {
+ m_Items.clear();
+ m_Buffer.Clear();
+ m_dwObjNum = 0;
+ m_index = 0;
+}
+
+void CPDF_ObjectStream::CompressIndirectObject(uint32_t dwObjNum,
+ const CPDF_Object* pObj) {
+ m_Items.push_back({dwObjNum, m_Buffer.GetLength()});
+ m_Buffer << pObj;
+}
+
+void CPDF_ObjectStream::CompressIndirectObject(uint32_t dwObjNum,
+ const uint8_t* pBuffer,
+ uint32_t dwSize) {
+ m_Items.push_back({dwObjNum, m_Buffer.GetLength()});
+ m_Buffer.AppendBlock(pBuffer, dwSize);
+}
+
+FX_FILESIZE CPDF_ObjectStream::End(CPDF_Creator* pCreator) {
+ ASSERT(pCreator);
+ if (m_Items.empty())
+ return 0;
+
+ CFX_FileBufferArchive* pFile = pCreator->GetFile();
+ FX_FILESIZE ObjOffset = pCreator->GetOffset();
+ if (!m_dwObjNum)
+ m_dwObjNum = pCreator->GetNextObjectNumber();
+
+ CFX_ByteTextBuf tempBuffer;
+ for (const auto& pair : m_Items)
+ tempBuffer << pair.objnum << " " << pair.offset << " ";
+
+ int32_t len = pFile->AppendDWord(m_dwObjNum);
+ if (len < 0)
+ return -1;
+
+ pCreator->IncrementOffset(len);
+ if ((len = pFile->AppendString(" 0 obj\r\n<</Type /ObjStm /N ")) < 0)
+ return -1;
+
+ pCreator->IncrementOffset(len);
+ uint32_t iCount = pdfium::CollectionSize<uint32_t>(m_Items);
+ if ((len = pFile->AppendDWord(iCount)) < 0)
+ return -1;
+
+ pCreator->IncrementOffset(len);
+ if (pFile->AppendString("/First ") < 0)
+ return -1;
+ if ((len = pFile->AppendDWord((uint32_t)tempBuffer.GetLength())) < 0)
+ return -1;
+ if (pFile->AppendString("/Length ") < 0)
+ return -1;
+
+ pCreator->IncrementOffset(len + 15);
+
+ tempBuffer << m_Buffer;
+ CPDF_FlateEncoder encoder(tempBuffer.GetBuffer(), tempBuffer.GetLength(),
+ true, false);
+ CPDF_Encryptor encryptor(pCreator->GetCryptoHandler(), m_dwObjNum,
+ encoder.GetData(), encoder.GetSize());
+ if ((len = pFile->AppendDWord(encryptor.GetSize())) < 0)
+ return -1;
+
+ pCreator->IncrementOffset(len);
+ if (pFile->AppendString("/Filter /FlateDecode") < 0)
+ return -1;
+
+ pCreator->IncrementOffset(20);
+ if ((len = pFile->AppendString(">>stream\r\n")) < 0)
+ return -1;
+ if (pFile->AppendBlock(encryptor.GetData(), encryptor.GetSize()) < 0)
+ return -1;
+
+ pCreator->IncrementOffset(len + encryptor.GetSize());
+ if ((len = pFile->AppendString("\r\nendstream\r\nendobj\r\n")) < 0)
+ return -1;
+
+ pCreator->IncrementOffset(len);
+ return ObjOffset;
+}