From ccf206a7731e0df773a4c5941736953446ec4d9d Mon Sep 17 00:00:00 2001 From: Ryan Harrison Date: Tue, 14 Nov 2017 16:05:53 +0000 Subject: Move CXFA_FileRead out of XFA and rename This code doesn't depend on XFA, but is only used by it. Moving it out, so it will be easier for me to write some tools for extracting data out of PDFs. Bug: Change-Id: Ic18613b46abed5124c47f539833b01b12c1c6e56 Reviewed-on: https://pdfium-review.googlesource.com/18410 Reviewed-by: dsinclair Commit-Queue: Ryan Harrison --- BUILD.gn | 6 +- core/fxcrt/cfx_seekablemultistream.cpp | 85 ++++++++++++++++++++++++ core/fxcrt/cfx_seekablemultistream.h | 36 +++++++++++ core/fxcrt/cfx_seekablemultistream_unittest.cpp | 85 ++++++++++++++++++++++++ xfa/fxfa/cxfa_ffapp_unittest.cpp | 86 ------------------------- xfa/fxfa/cxfa_ffdoc.cpp | 4 +- xfa/fxfa/cxfa_fileread.cpp | 82 ----------------------- xfa/fxfa/cxfa_fileread.h | 36 ----------- 8 files changed, 211 insertions(+), 209 deletions(-) create mode 100644 core/fxcrt/cfx_seekablemultistream.cpp create mode 100644 core/fxcrt/cfx_seekablemultistream.h create mode 100644 core/fxcrt/cfx_seekablemultistream_unittest.cpp delete mode 100644 xfa/fxfa/cxfa_ffapp_unittest.cpp delete mode 100644 xfa/fxfa/cxfa_fileread.cpp delete mode 100644 xfa/fxfa/cxfa_fileread.h diff --git a/BUILD.gn b/BUILD.gn index 7d98e1f37a..2715b55824 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -819,6 +819,8 @@ static_library("fxcrt") { "core/fxcrt/cfx_fixedbufgrow.h", "core/fxcrt/cfx_memorystream.cpp", "core/fxcrt/cfx_memorystream.h", + "core/fxcrt/cfx_seekablemultistream.cpp", + "core/fxcrt/cfx_seekablemultistream.h", "core/fxcrt/cfx_utf8decoder.cpp", "core/fxcrt/cfx_utf8decoder.h", "core/fxcrt/cfx_widetextbuf.cpp", @@ -1727,8 +1729,6 @@ if (pdf_enable_xfa) { "xfa/fxfa/cxfa_ffwidget.h", "xfa/fxfa/cxfa_ffwidgethandler.cpp", "xfa/fxfa/cxfa_ffwidgethandler.h", - "xfa/fxfa/cxfa_fileread.cpp", - "xfa/fxfa/cxfa_fileread.h", "xfa/fxfa/cxfa_fontmgr.cpp", "xfa/fxfa/cxfa_fontmgr.h", "xfa/fxfa/cxfa_fwladapterwidgetmgr.cpp", @@ -1962,6 +1962,7 @@ test("pdfium_unittests") { "core/fxcodec/jbig2/JBig2_BitStream_unittest.cpp", "core/fxcodec/jbig2/JBig2_Image_unittest.cpp", "core/fxcrt/bytestring_unittest.cpp", + "core/fxcrt/cfx_seekablemultistream_unittest.cpp", "core/fxcrt/fx_bidi_unittest.cpp", "core/fxcrt/fx_coordinates_unittest.cpp", "core/fxcrt/fx_extension_unittest.cpp", @@ -2010,7 +2011,6 @@ test("pdfium_unittests") { "xfa/fgas/crt/cfgas_formatstring_unittest.cpp", "xfa/fgas/layout/cfx_rtfbreak_unittest.cpp", "xfa/fwl/cfx_barcode_unittest.cpp", - "xfa/fxfa/cxfa_ffapp_unittest.cpp", "xfa/fxfa/cxfa_ffbarcode_unittest.cpp", "xfa/fxfa/cxfa_textparser_unittest.cpp", "xfa/fxfa/fm2js/cxfa_fmlexer_unittest.cpp", diff --git a/core/fxcrt/cfx_seekablemultistream.cpp b/core/fxcrt/cfx_seekablemultistream.cpp new file mode 100644 index 0000000000..12682d7678 --- /dev/null +++ b/core/fxcrt/cfx_seekablemultistream.cpp @@ -0,0 +1,85 @@ +// 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/fxcrt/cfx_seekablemultistream.h" + +#include + +#include "core/fpdfapi/parser/cpdf_stream_acc.h" +#include "third_party/base/logging.h" +#include "third_party/base/stl_util.h" + +CFX_SeekableMultiStream::CFX_SeekableMultiStream( + const std::vector& streams) { + for (CPDF_Stream* pStream : streams) { + m_Data.push_back(pdfium::MakeRetain(pStream)); + m_Data.back()->LoadAllData(); + } +} + +CFX_SeekableMultiStream::~CFX_SeekableMultiStream() {} + +FX_FILESIZE CFX_SeekableMultiStream::GetSize() { + uint32_t dwSize = 0; + for (const auto& acc : m_Data) + dwSize += acc->GetSize(); + return dwSize; +} + +bool CFX_SeekableMultiStream::ReadBlock(void* buffer, + FX_FILESIZE offset, + size_t size) { + int32_t iCount = pdfium::CollectionSize(m_Data); + int32_t index = 0; + while (index < iCount) { + const auto& acc = m_Data[index]; + FX_FILESIZE dwSize = acc->GetSize(); + if (offset < dwSize) + break; + + offset -= dwSize; + index++; + } + while (index < iCount) { + const auto& acc = m_Data[index]; + uint32_t dwSize = acc->GetSize(); + size_t dwRead = std::min(size, static_cast(dwSize - offset)); + memcpy(buffer, acc->GetData() + offset, dwRead); + size -= dwRead; + if (size == 0) + return true; + + buffer = static_cast(buffer) + dwRead; + offset = 0; + index++; + } + return false; +} + +size_t CFX_SeekableMultiStream::ReadBlock(void* buffer, size_t size) { + NOTREACHED(); + return 0; +} + +FX_FILESIZE CFX_SeekableMultiStream::GetPosition() { + return 0; +} + +bool CFX_SeekableMultiStream::IsEOF() { + return false; +} + +bool CFX_SeekableMultiStream::Flush() { + NOTREACHED(); + return false; +} + +bool CFX_SeekableMultiStream::WriteBlock(const void* pData, + FX_FILESIZE offset, + size_t size) { + NOTREACHED(); + return false; +} diff --git a/core/fxcrt/cfx_seekablemultistream.h b/core/fxcrt/cfx_seekablemultistream.h new file mode 100644 index 0000000000..9138d7c321 --- /dev/null +++ b/core/fxcrt/cfx_seekablemultistream.h @@ -0,0 +1,36 @@ +// 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 + +#ifndef CORE_FXCRT_CFX_SEEKABLEMULTISTREAM_H_ +#define CORE_FXCRT_CFX_SEEKABLEMULTISTREAM_H_ + +#include + +#include "core/fxcrt/fx_stream.h" +#include "core/fxcrt/retain_ptr.h" + +class CPDF_Stream; +class CPDF_StreamAcc; + +class CFX_SeekableMultiStream : public IFX_SeekableStream { + public: + explicit CFX_SeekableMultiStream(const std::vector& streams); + ~CFX_SeekableMultiStream() override; + + // IFX_SeekableReadStream + FX_FILESIZE GetPosition() override; + FX_FILESIZE GetSize() override; + bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override; + size_t ReadBlock(void* buffer, size_t size) override; + bool IsEOF() override; + bool Flush() override; + bool WriteBlock(const void* pData, FX_FILESIZE offset, size_t size) override; + + private: + std::vector> m_Data; +}; + +#endif // CORE_FXCRT_CFX_SEEKABLEMULTISTREAM_H_ diff --git a/core/fxcrt/cfx_seekablemultistream_unittest.cpp b/core/fxcrt/cfx_seekablemultistream_unittest.cpp new file mode 100644 index 0000000000..89213b13af --- /dev/null +++ b/core/fxcrt/cfx_seekablemultistream_unittest.cpp @@ -0,0 +1,85 @@ +// Copyright 2016 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. + +#include "core/fxcrt/cfx_seekablemultistream.h" + +#include +#include + +#include "core/fpdfapi/parser/cpdf_dictionary.h" +#include "core/fpdfapi/parser/cpdf_stream.h" +#include "core/fxcrt/fx_memory.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "testing/test_support.h" +#include "third_party/base/ptr_util.h" + +TEST(CFX_SeekableMultiStreamTest, NoStreams) { + std::vector streams; + auto fileread = pdfium::MakeRetain(streams); + + uint8_t output_buffer[16]; + memset(output_buffer, 0xbd, sizeof(output_buffer)); + EXPECT_FALSE(fileread->ReadBlock(output_buffer, 0, 0)); + EXPECT_EQ(0xbd, output_buffer[0]); +} + +TEST(CXFAFileReadTest, EmptyStreams) { + std::vector streams; + auto stream1 = pdfium::MakeUnique(); + streams.push_back(stream1.get()); + auto fileread = pdfium::MakeRetain(streams); + + uint8_t output_buffer[16]; + memset(output_buffer, 0xbd, sizeof(output_buffer)); + EXPECT_FALSE(fileread->ReadBlock(output_buffer, 0, 0)); + EXPECT_EQ(0xbd, output_buffer[0]); +} + +TEST(CXFAFileReadTest, NormalStreams) { + std::vector streams; + auto stream1 = pdfium::MakeUnique(); + auto stream2 = pdfium::MakeUnique(); + auto stream3 = pdfium::MakeUnique(); + + // 16 chars total. + stream1->InitStream(reinterpret_cast("one t"), 5, + pdfium::MakeUnique()); + stream2->InitStream(reinterpret_cast("wo "), 3, + pdfium::MakeUnique()); + stream3->InitStream(reinterpret_cast("three!!!"), 8, + pdfium::MakeUnique()); + + streams.push_back(stream1.get()); + streams.push_back(stream2.get()); + streams.push_back(stream3.get()); + auto fileread = pdfium::MakeRetain(streams); + + uint8_t output_buffer[16]; + memset(output_buffer, 0xbd, sizeof(output_buffer)); + EXPECT_TRUE(fileread->ReadBlock(output_buffer, 0, 0)); + EXPECT_EQ(0xbd, output_buffer[0]); + + memset(output_buffer, 0xbd, sizeof(output_buffer)); + EXPECT_TRUE(fileread->ReadBlock(output_buffer, 1, 0)); + EXPECT_EQ(0xbd, output_buffer[0]); + + memset(output_buffer, 0xbd, sizeof(output_buffer)); + EXPECT_TRUE(fileread->ReadBlock(output_buffer, 0, 1)); + EXPECT_EQ(0, memcmp(output_buffer, "o", 1)); + EXPECT_EQ(0xbd, output_buffer[1]); + + memset(output_buffer, 0xbd, sizeof(output_buffer)); + EXPECT_TRUE(fileread->ReadBlock(output_buffer, 0, sizeof(output_buffer))); + EXPECT_EQ(0, memcmp(output_buffer, "one two three!!!", 16)); + + memset(output_buffer, 0xbd, sizeof(output_buffer)); + EXPECT_TRUE(fileread->ReadBlock(output_buffer, 2, 10)); + EXPECT_EQ(0, memcmp(output_buffer, "e two thre", 10)); + EXPECT_EQ(0xbd, output_buffer[11]); + + memset(output_buffer, 0xbd, sizeof(output_buffer)); + EXPECT_FALSE(fileread->ReadBlock(output_buffer, 1, sizeof(output_buffer))); + EXPECT_EQ(0, memcmp(output_buffer, "ne two three!!!", 15)); + EXPECT_EQ(0xbd, output_buffer[15]); +} diff --git a/xfa/fxfa/cxfa_ffapp_unittest.cpp b/xfa/fxfa/cxfa_ffapp_unittest.cpp deleted file mode 100644 index bd35956c39..0000000000 --- a/xfa/fxfa/cxfa_ffapp_unittest.cpp +++ /dev/null @@ -1,86 +0,0 @@ -// Copyright 2016 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. - -#include "xfa/fxfa/cxfa_ffapp.h" - -#include -#include - -#include "core/fpdfapi/parser/cpdf_dictionary.h" -#include "core/fpdfapi/parser/cpdf_stream.h" -#include "core/fxcrt/fx_memory.h" -#include "testing/gtest/include/gtest/gtest.h" -#include "testing/test_support.h" -#include "third_party/base/ptr_util.h" -#include "xfa/fxfa/cxfa_fileread.h" - -TEST(CXFAFileReadTest, NoStreams) { - std::vector streams; - auto fileread = pdfium::MakeRetain(streams); - - uint8_t output_buffer[16]; - memset(output_buffer, 0xbd, sizeof(output_buffer)); - EXPECT_FALSE(fileread->ReadBlock(output_buffer, 0, 0)); - EXPECT_EQ(0xbd, output_buffer[0]); -} - -TEST(CXFAFileReadTest, EmptyStreams) { - std::vector streams; - auto stream1 = pdfium::MakeUnique(); - streams.push_back(stream1.get()); - auto fileread = pdfium::MakeRetain(streams); - - uint8_t output_buffer[16]; - memset(output_buffer, 0xbd, sizeof(output_buffer)); - EXPECT_FALSE(fileread->ReadBlock(output_buffer, 0, 0)); - EXPECT_EQ(0xbd, output_buffer[0]); -} - -TEST(CXFAFileReadTest, NormalStreams) { - std::vector streams; - auto stream1 = pdfium::MakeUnique(); - auto stream2 = pdfium::MakeUnique(); - auto stream3 = pdfium::MakeUnique(); - - // 16 chars total. - stream1->InitStream(reinterpret_cast("one t"), 5, - pdfium::MakeUnique()); - stream2->InitStream(reinterpret_cast("wo "), 3, - pdfium::MakeUnique()); - stream3->InitStream(reinterpret_cast("three!!!"), 8, - pdfium::MakeUnique()); - - streams.push_back(stream1.get()); - streams.push_back(stream2.get()); - streams.push_back(stream3.get()); - auto fileread = pdfium::MakeRetain(streams); - - uint8_t output_buffer[16]; - memset(output_buffer, 0xbd, sizeof(output_buffer)); - EXPECT_TRUE(fileread->ReadBlock(output_buffer, 0, 0)); - EXPECT_EQ(0xbd, output_buffer[0]); - - memset(output_buffer, 0xbd, sizeof(output_buffer)); - EXPECT_TRUE(fileread->ReadBlock(output_buffer, 1, 0)); - EXPECT_EQ(0xbd, output_buffer[0]); - - memset(output_buffer, 0xbd, sizeof(output_buffer)); - EXPECT_TRUE(fileread->ReadBlock(output_buffer, 0, 1)); - EXPECT_EQ(0, memcmp(output_buffer, "o", 1)); - EXPECT_EQ(0xbd, output_buffer[1]); - - memset(output_buffer, 0xbd, sizeof(output_buffer)); - EXPECT_TRUE(fileread->ReadBlock(output_buffer, 0, sizeof(output_buffer))); - EXPECT_EQ(0, memcmp(output_buffer, "one two three!!!", 16)); - - memset(output_buffer, 0xbd, sizeof(output_buffer)); - EXPECT_TRUE(fileread->ReadBlock(output_buffer, 2, 10)); - EXPECT_EQ(0, memcmp(output_buffer, "e two thre", 10)); - EXPECT_EQ(0xbd, output_buffer[11]); - - memset(output_buffer, 0xbd, sizeof(output_buffer)); - EXPECT_FALSE(fileread->ReadBlock(output_buffer, 1, sizeof(output_buffer))); - EXPECT_EQ(0, memcmp(output_buffer, "ne two three!!!", 15)); - EXPECT_EQ(0xbd, output_buffer[15]); -} diff --git a/xfa/fxfa/cxfa_ffdoc.cpp b/xfa/fxfa/cxfa_ffdoc.cpp index 640ac39178..d53f1b2ad7 100644 --- a/xfa/fxfa/cxfa_ffdoc.cpp +++ b/xfa/fxfa/cxfa_ffdoc.cpp @@ -16,6 +16,7 @@ #include "core/fpdfdoc/cpdf_nametree.h" #include "core/fxcrt/cfx_checksumcontext.h" #include "core/fxcrt/cfx_memorystream.h" +#include "core/fxcrt/cfx_seekablemultistream.h" #include "core/fxcrt/fx_extension.h" #include "core/fxcrt/fx_memory.h" #include "core/fxcrt/xml/cfx_xmlelement.h" @@ -26,7 +27,6 @@ #include "xfa/fxfa/cxfa_ffdocview.h" #include "xfa/fxfa/cxfa_ffnotify.h" #include "xfa/fxfa/cxfa_ffwidget.h" -#include "xfa/fxfa/cxfa_fileread.h" #include "xfa/fxfa/cxfa_fontmgr.h" #include "xfa/fxfa/parser/cxfa_dataexporter.h" #include "xfa/fxfa/parser/cxfa_dataimporter.h" @@ -316,7 +316,7 @@ bool CXFA_FFDoc::OpenDoc(CPDF_Document* pPDFDoc) { return false; m_pPDFDoc = pPDFDoc; - m_pStream = pdfium::MakeRetain(xfaStreams); + m_pStream = pdfium::MakeRetain(xfaStreams); return true; } diff --git a/xfa/fxfa/cxfa_fileread.cpp b/xfa/fxfa/cxfa_fileread.cpp deleted file mode 100644 index 12e23646d1..0000000000 --- a/xfa/fxfa/cxfa_fileread.cpp +++ /dev/null @@ -1,82 +0,0 @@ -// 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 "xfa/fxfa/cxfa_fileread.h" - -#include - -#include "core/fpdfapi/parser/cpdf_stream_acc.h" -#include "third_party/base/logging.h" -#include "third_party/base/stl_util.h" - -CXFA_FileRead::CXFA_FileRead(const std::vector& streams) { - for (CPDF_Stream* pStream : streams) { - m_Data.push_back(pdfium::MakeRetain(pStream)); - m_Data.back()->LoadAllData(); - } -} - -CXFA_FileRead::~CXFA_FileRead() {} - -FX_FILESIZE CXFA_FileRead::GetSize() { - uint32_t dwSize = 0; - for (const auto& acc : m_Data) - dwSize += acc->GetSize(); - return dwSize; -} - -bool CXFA_FileRead::ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) { - int32_t iCount = pdfium::CollectionSize(m_Data); - int32_t index = 0; - while (index < iCount) { - const auto& acc = m_Data[index]; - FX_FILESIZE dwSize = acc->GetSize(); - if (offset < dwSize) - break; - - offset -= dwSize; - index++; - } - while (index < iCount) { - const auto& acc = m_Data[index]; - uint32_t dwSize = acc->GetSize(); - size_t dwRead = std::min(size, static_cast(dwSize - offset)); - memcpy(buffer, acc->GetData() + offset, dwRead); - size -= dwRead; - if (size == 0) - return true; - - buffer = static_cast(buffer) + dwRead; - offset = 0; - index++; - } - return false; -} - -size_t CXFA_FileRead::ReadBlock(void* buffer, size_t size) { - NOTREACHED(); - return 0; -} - -FX_FILESIZE CXFA_FileRead::GetPosition() { - return 0; -} - -bool CXFA_FileRead::IsEOF() { - return false; -} - -bool CXFA_FileRead::Flush() { - NOTREACHED(); - return false; -} - -bool CXFA_FileRead::WriteBlock(const void* pData, - FX_FILESIZE offset, - size_t size) { - NOTREACHED(); - return false; -} diff --git a/xfa/fxfa/cxfa_fileread.h b/xfa/fxfa/cxfa_fileread.h deleted file mode 100644 index 62fa4c67bb..0000000000 --- a/xfa/fxfa/cxfa_fileread.h +++ /dev/null @@ -1,36 +0,0 @@ -// 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 - -#ifndef XFA_FXFA_CXFA_FILEREAD_H_ -#define XFA_FXFA_CXFA_FILEREAD_H_ - -#include - -#include "core/fxcrt/fx_stream.h" -#include "core/fxcrt/retain_ptr.h" - -class CPDF_Stream; -class CPDF_StreamAcc; - -class CXFA_FileRead : public IFX_SeekableStream { - public: - explicit CXFA_FileRead(const std::vector& streams); - ~CXFA_FileRead() override; - - // IFX_SeekableReadStream - FX_FILESIZE GetPosition() override; - FX_FILESIZE GetSize() override; - bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override; - size_t ReadBlock(void* buffer, size_t size) override; - bool IsEOF() override; - bool Flush() override; - bool WriteBlock(const void* pData, FX_FILESIZE offset, size_t size) override; - - private: - std::vector> m_Data; -}; - -#endif // XFA_FXFA_CXFA_FILEREAD_H_ -- cgit v1.2.3