From 9d6a2089c93c94461289b21a29771039eace95e7 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Wed, 12 Apr 2017 11:32:32 -0400 Subject: Remove MakeSeekableReadStream MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This Cl removes the MakeSeekableReadStream call and, at the one place it's used, creates an IFX_MemoryStream which is a seekable read stream. Change-Id: I6b0b23636eff47f8caca5432313ba99703e21e4d Reviewed-on: https://pdfium-review.googlesource.com/4037 Reviewed-by: Nicolás Peña Reviewed-by: Tom Sepez Commit-Queue: dsinclair --- BUILD.gn | 2 + testing/libfuzzer/pdf_cfx_saxreader_fuzzer.cc | 16 ++----- xfa/fgas/crt/ifgas_stream.cpp | 34 -------------- xfa/fgas/crt/ifgas_stream.h | 2 - xfa/fgas/font/cfgas_gefont.cpp | 50 -------------------- xfa/fgas/font/cfgas_gefont.h | 9 ---- xfa/fxfa/cxfa_ffapp.cpp | 66 --------------------------- xfa/fxfa/cxfa_ffapp.h | 5 -- xfa/fxfa/cxfa_ffapp_unittest.cpp | 10 ++-- xfa/fxfa/cxfa_ffdoc.cpp | 3 +- xfa/fxfa/cxfa_fileread.cpp | 56 +++++++++++++++++++++++ xfa/fxfa/cxfa_fileread.h | 31 +++++++++++++ 12 files changed, 99 insertions(+), 185 deletions(-) create mode 100644 xfa/fxfa/cxfa_fileread.cpp create mode 100644 xfa/fxfa/cxfa_fileread.h diff --git a/BUILD.gn b/BUILD.gn index 6260da50f0..c279a6c831 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -1641,6 +1641,8 @@ 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_pdffontmgr.cpp", diff --git a/testing/libfuzzer/pdf_cfx_saxreader_fuzzer.cc b/testing/libfuzzer/pdf_cfx_saxreader_fuzzer.cc index 00b55cdbd4..0dfbbdf639 100644 --- a/testing/libfuzzer/pdf_cfx_saxreader_fuzzer.cc +++ b/testing/libfuzzer/pdf_cfx_saxreader_fuzzer.cc @@ -10,20 +10,12 @@ #include "xfa/fxfa/parser/cxfa_widetextread.h" extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { - CFX_WideString input = CFX_WideString::FromUTF8( - CFX_ByteStringC(data, static_cast(size))); - auto stream = pdfium::MakeRetain(input); - if (!stream) - return 0; - - CFX_RetainPtr fileRead = - stream->MakeSeekableReadStream(); - if (!fileRead) - return 0; - CFX_SAXReader reader; - if (reader.StartParse(fileRead, 0, -1, CFX_SaxParseMode_NotSkipSpace) < 0) + if (reader.StartParse( + IFX_MemoryStream::Create(const_cast(data), size), 0, -1, + CFX_SaxParseMode_NotSkipSpace) < 0) { return 0; + } while (1) { int32_t ret = reader.ContinueParse(nullptr); diff --git a/xfa/fgas/crt/ifgas_stream.cpp b/xfa/fgas/crt/ifgas_stream.cpp index 874330165b..dcd13b162e 100644 --- a/xfa/fgas/crt/ifgas_stream.cpp +++ b/xfa/fgas/crt/ifgas_stream.cpp @@ -169,19 +169,6 @@ class CFGAS_TextStream : public IFGAS_Stream { CFX_RetainPtr m_pStreamImp; }; -class CFGAS_FileRead : public IFX_SeekableReadStream { - public: - explicit CFGAS_FileRead(const CFX_RetainPtr& pStream); - ~CFGAS_FileRead() override; - - // IFX_SeekableReadStream - FX_FILESIZE GetSize() override; - bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override; - - protected: - CFX_RetainPtr m_pStream; -}; - IFGAS_StreamImp::IFGAS_StreamImp() : m_dwAccess(0) {} CFGAS_FileReadStreamImp::CFGAS_FileReadStreamImp() @@ -663,23 +650,6 @@ uint16_t CFGAS_Stream::SetCodePage(uint16_t wCodePage) { #endif } -CFGAS_FileRead::CFGAS_FileRead(const CFX_RetainPtr& pStream) - : m_pStream(pStream) { - ASSERT(m_pStream); -} - -CFGAS_FileRead::~CFGAS_FileRead() {} - -FX_FILESIZE CFGAS_FileRead::GetSize() { - return (FX_FILESIZE)m_pStream->GetLength(); -} - -bool CFGAS_FileRead::ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) { - m_pStream->Seek(FX_STREAMSEEK_Begin, (int32_t)offset); - int32_t iLen = m_pStream->ReadData((uint8_t*)buffer, (int32_t)size); - return iLen == (int32_t)size; -} - } // namespace // static @@ -714,7 +684,3 @@ CFX_RetainPtr IFGAS_Stream::CreateWriteStream( return pdfium::MakeRetain( pdfium::MakeRetain(std::move(pImp), FX_STREAMACCESS_Write)); } - -CFX_RetainPtr IFGAS_Stream::MakeSeekableReadStream() { - return pdfium::MakeRetain(CFX_RetainPtr(this)); -} diff --git a/xfa/fgas/crt/ifgas_stream.h b/xfa/fgas/crt/ifgas_stream.h index cfb0722b21..d8efc41afd 100644 --- a/xfa/fgas/crt/ifgas_stream.h +++ b/xfa/fgas/crt/ifgas_stream.h @@ -42,8 +42,6 @@ class IFGAS_Stream : public CFX_Retainable { virtual int32_t GetBOM(uint8_t bom[4]) const = 0; virtual uint16_t GetCodePage() const = 0; virtual uint16_t SetCodePage(uint16_t wCodePage) = 0; - - CFX_RetainPtr MakeSeekableReadStream(); }; #endif // XFA_FGAS_CRT_IFGAS_STREAM_H_ diff --git a/xfa/fgas/font/cfgas_gefont.cpp b/xfa/fgas/font/cfgas_gefont.cpp index 7abc3bccb0..fdeb2ce533 100644 --- a/xfa/fgas/font/cfgas_gefont.cpp +++ b/xfa/fgas/font/cfgas_gefont.cpp @@ -54,29 +54,6 @@ CFX_RetainPtr CFGAS_GEFont::LoadFont( return pFont; } -#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ -// static -CFX_RetainPtr CFGAS_GEFont::LoadFont(const uint8_t* pBuffer, - int32_t iLength, - CFGAS_FontMgr* pFontMgr) { - auto pFont = pdfium::MakeRetain(pFontMgr); - if (pFont->LoadFontInternal(pBuffer, iLength)) - return nullptr; - return pFont; -} - -// static -CFX_RetainPtr CFGAS_GEFont::LoadFont( - const CFX_RetainPtr& pFontStream, - CFGAS_FontMgr* pFontMgr, - bool bSaveStream) { - auto pFont = pdfium::MakeRetain(pFontMgr); - if (!pFont->LoadFontInternal(pFontStream, bSaveStream)) - return nullptr; - return pFont; -} -#endif // _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ - CFGAS_GEFont::CFGAS_GEFont(CFGAS_FontMgr* pFontMgr) : #if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ @@ -159,33 +136,6 @@ bool CFGAS_GEFont::LoadFontInternal(const wchar_t* pszFontFamily, return false; return InitFont(); } - -bool CFGAS_GEFont::LoadFontInternal(const uint8_t* pBuffer, int32_t length) { - if (m_pFont) - return false; - - m_pFont = new CFX_Font; - if (!m_pFont->LoadEmbedded(pBuffer, length)) - return false; - return InitFont(); -} - -bool CFGAS_GEFont::LoadFontInternal( - const CFX_RetainPtr& pFontStream, - bool bSaveStream) { - if (m_pFont || m_pFileRead || !pFontStream || pFontStream->GetLength() < 1) - return false; - if (bSaveStream) - m_pStream = pFontStream; - - m_pFileRead = pFontStream->MakeSeekableReadStream(); - m_pFont = new CFX_Font; - if (!m_pFont->LoadFile(m_pFileRead)) { - m_pFileRead.Reset(); - return false; - } - return InitFont(); -} #endif // _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ bool CFGAS_GEFont::LoadFontInternal(CFX_Font* pExternalFont) { diff --git a/xfa/fgas/font/cfgas_gefont.h b/xfa/fgas/font/cfgas_gefont.h index b460cee496..14029be1e0 100644 --- a/xfa/fgas/font/cfgas_gefont.h +++ b/xfa/fgas/font/cfgas_gefont.h @@ -37,15 +37,6 @@ class CFGAS_GEFont : public CFX_Retainable { static CFX_RetainPtr LoadFont( std::unique_ptr pInternalFont, CFGAS_FontMgr* pFontMgr); -#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ - static CFX_RetainPtr LoadFont(const uint8_t* pBuffer, - int32_t iLength, - CFGAS_FontMgr* pFontMgr); - static CFX_RetainPtr LoadFont( - const CFX_RetainPtr& pFontStream, - CFGAS_FontMgr* pFontMgr, - bool bSaveStream); -#endif CFX_RetainPtr Derive(uint32_t dwFontStyles, uint16_t wCodePage = 0); diff --git a/xfa/fxfa/cxfa_ffapp.cpp b/xfa/fxfa/cxfa_ffapp.cpp index 7b88626c91..c02fd30e86 100644 --- a/xfa/fxfa/cxfa_ffapp.cpp +++ b/xfa/fxfa/cxfa_ffapp.cpp @@ -23,72 +23,6 @@ #include "xfa/fxfa/cxfa_ffwidgethandler.h" #include "xfa/fxfa/cxfa_fontmgr.h" -namespace { - -class CXFA_FileRead : public IFX_SeekableReadStream { - public: - explicit CXFA_FileRead(const std::vector& streams); - ~CXFA_FileRead() override; - - // IFX_SeekableReadStream - FX_FILESIZE GetSize() override; - bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override; - - private: - std::vector> m_Data; -}; - -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 = (uint8_t*)buffer + dwRead; - offset = 0; - index++; - } - return false; -} - -} // namespace - -CFX_RetainPtr MakeSeekableReadStream( - const std::vector& streams) { - return CFX_RetainPtr(new CXFA_FileRead(streams)); -} - CXFA_FFApp::CXFA_FFApp(IXFA_AppProvider* pProvider) : m_pProvider(pProvider), m_pWidgetMgrDelegate(nullptr), diff --git a/xfa/fxfa/cxfa_ffapp.h b/xfa/fxfa/cxfa_ffapp.h index 01235d655b..0a6409d3d1 100644 --- a/xfa/fxfa/cxfa_ffapp.h +++ b/xfa/fxfa/cxfa_ffapp.h @@ -25,11 +25,6 @@ class CXFA_FontMgr; class IFWL_AdapterTimerMgr; class CFWL_WidgetMgrDelegate; -// Layering prevents fxcrt from knowing about CPDF_Streams; this could go -// in fpdfsdk, but it is XFA-Only. -CFX_RetainPtr MakeSeekableReadStream( - const std::vector& streams); - class CXFA_FFApp { public: explicit CXFA_FFApp(IXFA_AppProvider* pProvider); diff --git a/xfa/fxfa/cxfa_ffapp_unittest.cpp b/xfa/fxfa/cxfa_ffapp_unittest.cpp index 4d65ccbb2f..fc1d038bac 100644 --- a/xfa/fxfa/cxfa_ffapp_unittest.cpp +++ b/xfa/fxfa/cxfa_ffapp_unittest.cpp @@ -13,13 +13,13 @@ #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" class CXFAFileReadTest : public pdfium::FPDF_Test {}; TEST_F(CXFAFileReadTest, NoStreams) { std::vector streams; - CFX_RetainPtr fileread = - MakeSeekableReadStream(streams); + auto fileread = pdfium::MakeRetain(streams); uint8_t output_buffer[16]; memset(output_buffer, 0xbd, sizeof(output_buffer)); @@ -31,8 +31,7 @@ TEST_F(CXFAFileReadTest, EmptyStreams) { std::vector streams; auto stream1 = pdfium::MakeUnique(); streams.push_back(stream1.get()); - CFX_RetainPtr fileread = - MakeSeekableReadStream(streams); + auto fileread = pdfium::MakeRetain(streams); uint8_t output_buffer[16]; memset(output_buffer, 0xbd, sizeof(output_buffer)); @@ -57,8 +56,7 @@ TEST_F(CXFAFileReadTest, NormalStreams) { streams.push_back(stream1.get()); streams.push_back(stream2.get()); streams.push_back(stream3.get()); - CFX_RetainPtr fileread = - MakeSeekableReadStream(streams); + auto fileread = pdfium::MakeRetain(streams); uint8_t output_buffer[16]; memset(output_buffer, 0xbd, sizeof(output_buffer)); diff --git a/xfa/fxfa/cxfa_ffdoc.cpp b/xfa/fxfa/cxfa_ffdoc.cpp index 4b6a1e2e07..2bbdfa1b9c 100644 --- a/xfa/fxfa/cxfa_ffdoc.cpp +++ b/xfa/fxfa/cxfa_ffdoc.cpp @@ -25,6 +25,7 @@ #include "xfa/fxfa/cxfa_ffapp.h" #include "xfa/fxfa/cxfa_ffdocview.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" @@ -315,7 +316,7 @@ bool CXFA_FFDoc::OpenDoc(CPDF_Document* pPDFDoc) { return false; m_pPDFDoc = pPDFDoc; - m_pStream = MakeSeekableReadStream(xfaStreams); + m_pStream = pdfium::MakeRetain(xfaStreams); return true; } diff --git a/xfa/fxfa/cxfa_fileread.cpp b/xfa/fxfa/cxfa_fileread.cpp new file mode 100644 index 0000000000..e2ecad2e93 --- /dev/null +++ b/xfa/fxfa/cxfa_fileread.cpp @@ -0,0 +1,56 @@ +// 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/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; +} diff --git a/xfa/fxfa/cxfa_fileread.h b/xfa/fxfa/cxfa_fileread.h new file mode 100644 index 0000000000..0c3348b8d5 --- /dev/null +++ b/xfa/fxfa/cxfa_fileread.h @@ -0,0 +1,31 @@ +// 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/cfx_retain_ptr.h" +#include "core/fxcrt/fx_stream.h" + +class CPDF_Stream; +class CPDF_StreamAcc; + +class CXFA_FileRead : public IFX_SeekableReadStream { + public: + explicit CXFA_FileRead(const std::vector& streams); + ~CXFA_FileRead() override; + + // IFX_SeekableReadStream + FX_FILESIZE GetSize() override; + bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override; + + private: + std::vector> m_Data; +}; + +#endif // XFA_FXFA_CXFA_FILEREAD_H_ -- cgit v1.2.3