summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Sinclair <dsinclair@chromium.org>2017-04-12 11:32:32 -0400
committerChromium commit bot <commit-bot@chromium.org>2017-04-12 17:23:27 +0000
commit9d6a2089c93c94461289b21a29771039eace95e7 (patch)
tree5b747786fe129b20aaf2ba3271e7b99051f5ae99
parentb333c9ec415c132451f4a10487b84f91124d08e8 (diff)
downloadpdfium-9d6a2089c93c94461289b21a29771039eace95e7.tar.xz
Remove MakeSeekableReadStream
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 <npm@chromium.org> Reviewed-by: Tom Sepez <tsepez@chromium.org> Commit-Queue: dsinclair <dsinclair@chromium.org>
-rw-r--r--BUILD.gn2
-rw-r--r--testing/libfuzzer/pdf_cfx_saxreader_fuzzer.cc16
-rw-r--r--xfa/fgas/crt/ifgas_stream.cpp34
-rw-r--r--xfa/fgas/crt/ifgas_stream.h2
-rw-r--r--xfa/fgas/font/cfgas_gefont.cpp50
-rw-r--r--xfa/fgas/font/cfgas_gefont.h9
-rw-r--r--xfa/fxfa/cxfa_ffapp.cpp66
-rw-r--r--xfa/fxfa/cxfa_ffapp.h5
-rw-r--r--xfa/fxfa/cxfa_ffapp_unittest.cpp10
-rw-r--r--xfa/fxfa/cxfa_ffdoc.cpp3
-rw-r--r--xfa/fxfa/cxfa_fileread.cpp56
-rw-r--r--xfa/fxfa/cxfa_fileread.h31
12 files changed, 99 insertions, 185 deletions
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<FX_STRSIZE>(size)));
- auto stream = pdfium::MakeRetain<CXFA_WideTextRead>(input);
- if (!stream)
- return 0;
-
- CFX_RetainPtr<IFX_SeekableReadStream> 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<uint8_t*>(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<IFGAS_Stream> m_pStreamImp;
};
-class CFGAS_FileRead : public IFX_SeekableReadStream {
- public:
- explicit CFGAS_FileRead(const CFX_RetainPtr<IFGAS_Stream>& pStream);
- ~CFGAS_FileRead() override;
-
- // IFX_SeekableReadStream
- FX_FILESIZE GetSize() override;
- bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override;
-
- protected:
- CFX_RetainPtr<IFGAS_Stream> 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<IFGAS_Stream>& 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> IFGAS_Stream::CreateWriteStream(
return pdfium::MakeRetain<CFGAS_TextStream>(
pdfium::MakeRetain<CFGAS_Stream>(std::move(pImp), FX_STREAMACCESS_Write));
}
-
-CFX_RetainPtr<IFX_SeekableReadStream> IFGAS_Stream::MakeSeekableReadStream() {
- return pdfium::MakeRetain<CFGAS_FileRead>(CFX_RetainPtr<IFGAS_Stream>(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<IFX_SeekableReadStream> 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> CFGAS_GEFont::LoadFont(
return pFont;
}
-#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
-// static
-CFX_RetainPtr<CFGAS_GEFont> CFGAS_GEFont::LoadFont(const uint8_t* pBuffer,
- int32_t iLength,
- CFGAS_FontMgr* pFontMgr) {
- auto pFont = pdfium::MakeRetain<CFGAS_GEFont>(pFontMgr);
- if (pFont->LoadFontInternal(pBuffer, iLength))
- return nullptr;
- return pFont;
-}
-
-// static
-CFX_RetainPtr<CFGAS_GEFont> CFGAS_GEFont::LoadFont(
- const CFX_RetainPtr<IFGAS_Stream>& pFontStream,
- CFGAS_FontMgr* pFontMgr,
- bool bSaveStream) {
- auto pFont = pdfium::MakeRetain<CFGAS_GEFont>(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<IFGAS_Stream>& 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<CFGAS_GEFont> LoadFont(
std::unique_ptr<CFX_Font> pInternalFont,
CFGAS_FontMgr* pFontMgr);
-#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
- static CFX_RetainPtr<CFGAS_GEFont> LoadFont(const uint8_t* pBuffer,
- int32_t iLength,
- CFGAS_FontMgr* pFontMgr);
- static CFX_RetainPtr<CFGAS_GEFont> LoadFont(
- const CFX_RetainPtr<IFGAS_Stream>& pFontStream,
- CFGAS_FontMgr* pFontMgr,
- bool bSaveStream);
-#endif
CFX_RetainPtr<CFGAS_GEFont> 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<CPDF_Stream*>& streams);
- ~CXFA_FileRead() override;
-
- // IFX_SeekableReadStream
- FX_FILESIZE GetSize() override;
- bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override;
-
- private:
- std::vector<CFX_RetainPtr<CPDF_StreamAcc>> m_Data;
-};
-
-CXFA_FileRead::CXFA_FileRead(const std::vector<CPDF_Stream*>& streams) {
- for (CPDF_Stream* pStream : streams) {
- m_Data.push_back(pdfium::MakeRetain<CPDF_StreamAcc>(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<int32_t>(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<size_t>(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<IFX_SeekableReadStream> MakeSeekableReadStream(
- const std::vector<CPDF_Stream*>& streams) {
- return CFX_RetainPtr<IFX_SeekableReadStream>(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<IFX_SeekableReadStream> MakeSeekableReadStream(
- const std::vector<CPDF_Stream*>& 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<CPDF_Stream*> streams;
- CFX_RetainPtr<IFX_SeekableReadStream> fileread =
- MakeSeekableReadStream(streams);
+ auto fileread = pdfium::MakeRetain<CXFA_FileRead>(streams);
uint8_t output_buffer[16];
memset(output_buffer, 0xbd, sizeof(output_buffer));
@@ -31,8 +31,7 @@ TEST_F(CXFAFileReadTest, EmptyStreams) {
std::vector<CPDF_Stream*> streams;
auto stream1 = pdfium::MakeUnique<CPDF_Stream>();
streams.push_back(stream1.get());
- CFX_RetainPtr<IFX_SeekableReadStream> fileread =
- MakeSeekableReadStream(streams);
+ auto fileread = pdfium::MakeRetain<CXFA_FileRead>(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<IFX_SeekableReadStream> fileread =
- MakeSeekableReadStream(streams);
+ auto fileread = pdfium::MakeRetain<CXFA_FileRead>(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<CXFA_FileRead>(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 <algorithm>
+
+#include "core/fpdfapi/parser/cpdf_stream_acc.h"
+#include "third_party/base/stl_util.h"
+
+CXFA_FileRead::CXFA_FileRead(const std::vector<CPDF_Stream*>& streams) {
+ for (CPDF_Stream* pStream : streams) {
+ m_Data.push_back(pdfium::MakeRetain<CPDF_StreamAcc>(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<int32_t>(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<size_t>(dwSize - offset));
+ memcpy(buffer, acc->GetData() + offset, dwRead);
+ size -= dwRead;
+ if (size == 0)
+ return true;
+
+ buffer = static_cast<uint8_t*>(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 <vector>
+
+#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<CPDF_Stream*>& streams);
+ ~CXFA_FileRead() override;
+
+ // IFX_SeekableReadStream
+ FX_FILESIZE GetSize() override;
+ bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override;
+
+ private:
+ std::vector<CFX_RetainPtr<CPDF_StreamAcc>> m_Data;
+};
+
+#endif // XFA_FXFA_CXFA_FILEREAD_H_