From ed176992eb7ddbb0fee7b79625501df06f8ccf93 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Mon, 13 Aug 2018 23:19:29 +0000 Subject: Make CFX_ReadOnlyMemoryStream take a span. Change-Id: Id097320ab2d9b5d1579582e5797e29c701499501 Reviewed-on: https://pdfium-review.googlesource.com/39991 Commit-Queue: Lei Zhang Reviewed-by: Tom Sepez --- core/fpdfapi/parser/cfdf_document.cpp | 3 +-- core/fpdfdoc/cpdf_metadata.cpp | 3 +-- core/fxcrt/cfx_readonlymemorystream.cpp | 16 ++++++++-------- core/fxcrt/cfx_readonlymemorystream.h | 6 +++--- fpdfsdk/fpdf_view.cpp | 8 +++++--- testing/fuzzers/pdf_xml_fuzzer.cc | 4 +++- xfa/fxfa/cxfa_ffdoc.cpp | 4 ++-- xfa/fxfa/parser/cxfa_document_parser.cpp | 4 ++-- xfa/fxfa/parser/cxfa_node.cpp | 9 ++++----- xfa/fxfa/parser/cxfa_xmllocale.cpp | 3 +-- 10 files changed, 30 insertions(+), 30 deletions(-) diff --git a/core/fpdfapi/parser/cfdf_document.cpp b/core/fpdfapi/parser/cfdf_document.cpp index d37c8d5457..421af48d80 100644 --- a/core/fpdfapi/parser/cfdf_document.cpp +++ b/core/fpdfapi/parser/cfdf_document.cpp @@ -32,8 +32,7 @@ std::unique_ptr CFDF_Document::CreateNewDoc() { std::unique_ptr CFDF_Document::ParseMemory( pdfium::span span) { auto pDoc = pdfium::MakeUnique(); - pDoc->ParseStream( - pdfium::MakeRetain(span.data(), span.size())); + pDoc->ParseStream(pdfium::MakeRetain(span)); return pDoc->m_pRootDict ? std::move(pDoc) : nullptr; } diff --git a/core/fpdfdoc/cpdf_metadata.cpp b/core/fpdfdoc/cpdf_metadata.cpp index b9a92963dd..99fd965452 100644 --- a/core/fpdfdoc/cpdf_metadata.cpp +++ b/core/fpdfdoc/cpdf_metadata.cpp @@ -67,8 +67,7 @@ std::vector CPDF_Metadata::CheckForSharedForm() const { auto pAcc = pdfium::MakeRetain(stream_.Get()); pAcc->LoadAllDataFiltered(); - auto stream = pdfium::MakeRetain(pAcc->GetData(), - pAcc->GetSize()); + auto stream = pdfium::MakeRetain(pAcc->GetSpan()); CFX_XMLParser parser(stream); std::unique_ptr doc = parser.Parse(); if (!doc) diff --git a/core/fxcrt/cfx_readonlymemorystream.cpp b/core/fxcrt/cfx_readonlymemorystream.cpp index 1d09759208..a01ed2e474 100644 --- a/core/fxcrt/cfx_readonlymemorystream.cpp +++ b/core/fxcrt/cfx_readonlymemorystream.cpp @@ -8,14 +8,14 @@ #include "core/fxcrt/fx_safe_types.h" -CFX_ReadOnlyMemoryStream::CFX_ReadOnlyMemoryStream(const uint8_t* pBuf, - FX_FILESIZE size) - : m_pBuf(pBuf), m_size(size) {} +CFX_ReadOnlyMemoryStream::CFX_ReadOnlyMemoryStream( + pdfium::span span) + : m_span(span) {} CFX_ReadOnlyMemoryStream::~CFX_ReadOnlyMemoryStream() = default; FX_FILESIZE CFX_ReadOnlyMemoryStream::GetSize() { - return m_size; + return pdfium::base::checked_cast(m_span.size()); } bool CFX_ReadOnlyMemoryStream::ReadBlock(void* buffer, @@ -24,11 +24,11 @@ bool CFX_ReadOnlyMemoryStream::ReadBlock(void* buffer, if (offset < 0) return false; - FX_SAFE_FILESIZE newPos = pdfium::base::checked_cast(size); - newPos += offset; - if (!newPos.IsValid() || newPos.ValueOrDie() > m_size) + FX_SAFE_SIZE_T pos = size; + pos += offset; + if (!pos.IsValid() || pos.ValueOrDie() > m_span.size()) return false; - memcpy(buffer, m_pBuf + offset, size); + memcpy(buffer, &m_span[offset], size); return true; } diff --git a/core/fxcrt/cfx_readonlymemorystream.h b/core/fxcrt/cfx_readonlymemorystream.h index e9d18222a4..a6645b82c7 100644 --- a/core/fxcrt/cfx_readonlymemorystream.h +++ b/core/fxcrt/cfx_readonlymemorystream.h @@ -9,6 +9,7 @@ #include "core/fxcrt/fx_stream.h" #include "core/fxcrt/retain_ptr.h" +#include "third_party/base/span.h" class CFX_ReadOnlyMemoryStream final : public IFX_SeekableReadStream { public: @@ -20,11 +21,10 @@ class CFX_ReadOnlyMemoryStream final : public IFX_SeekableReadStream { bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override; private: - CFX_ReadOnlyMemoryStream(const uint8_t* pBuf, FX_FILESIZE size); + explicit CFX_ReadOnlyMemoryStream(pdfium::span span); ~CFX_ReadOnlyMemoryStream() override; - const uint8_t* const m_pBuf; - const FX_FILESIZE m_size; + const pdfium::span m_span; }; #endif // CORE_FXCRT_CFX_READONLYMEMORYSTREAM_H_ diff --git a/fpdfsdk/fpdf_view.cpp b/fpdfsdk/fpdf_view.cpp index bd8cfe1e25..0cfa362904 100644 --- a/fpdfsdk/fpdf_view.cpp +++ b/fpdfsdk/fpdf_view.cpp @@ -41,6 +41,7 @@ #include "fxjs/ijs_runtime.h" #include "public/fpdf_formfill.h" #include "third_party/base/ptr_util.h" +#include "third_party/base/span.h" #ifdef PDF_ENABLE_XFA #include "fpdfsdk/fpdfxfa/cpdfxfa_context.h" @@ -269,9 +270,10 @@ FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDF_LoadXFA(FPDF_DOCUMENT document) { FPDF_EXPORT FPDF_DOCUMENT FPDF_CALLCONV FPDF_LoadMemDocument(const void* data_buf, int size, FPDF_BYTESTRING password) { - return LoadDocumentImpl(pdfium::MakeRetain( - static_cast(data_buf), size), - password); + return LoadDocumentImpl( + pdfium::MakeRetain( + pdfium::make_span(static_cast(data_buf), size)), + password); } FPDF_EXPORT FPDF_DOCUMENT FPDF_CALLCONV diff --git a/testing/fuzzers/pdf_xml_fuzzer.cc b/testing/fuzzers/pdf_xml_fuzzer.cc index 7f5fdb1532..efc171671c 100644 --- a/testing/fuzzers/pdf_xml_fuzzer.cc +++ b/testing/fuzzers/pdf_xml_fuzzer.cc @@ -13,13 +13,15 @@ #include "core/fxcrt/xml/cfx_xmlelement.h" #include "core/fxcrt/xml/cfx_xmlparser.h" #include "third_party/base/ptr_util.h" +#include "third_party/base/span.h" extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { FX_SAFE_SIZE_T safe_size = size; if (!safe_size.IsValid()) return 0; - auto stream = pdfium::MakeRetain(data, size); + auto stream = pdfium::MakeRetain( + pdfium::make_span(data, size)); CFX_XMLParser parser(stream); std::unique_ptr doc = parser.Parse(); if (!doc || !doc->GetRoot()) diff --git a/xfa/fxfa/cxfa_ffdoc.cpp b/xfa/fxfa/cxfa_ffdoc.cpp index 7852d30036..44c1b7912b 100644 --- a/xfa/fxfa/cxfa_ffdoc.cpp +++ b/xfa/fxfa/cxfa_ffdoc.cpp @@ -215,8 +215,8 @@ RetainPtr CXFA_FFDoc::GetPDFNamedImage( auto pAcc = pdfium::MakeRetain(pStream); pAcc->LoadAllDataFiltered(); - auto pImageFileRead = pdfium::MakeRetain( - pAcc->GetData(), pAcc->GetSize()); + auto pImageFileRead = + pdfium::MakeRetain(pAcc->GetSpan()); RetainPtr pDibSource = XFA_LoadImageFromBuffer( pImageFileRead, FXCODEC_IMAGE_UNKNOWN, iImageXDpi, iImageYDpi); diff --git a/xfa/fxfa/parser/cxfa_document_parser.cpp b/xfa/fxfa/parser/cxfa_document_parser.cpp index 5cb08863c8..d0f137da7c 100644 --- a/xfa/fxfa/parser/cxfa_document_parser.cpp +++ b/xfa/fxfa/parser/cxfa_document_parser.cpp @@ -332,8 +332,8 @@ bool CXFA_DocumentParser::Parse( } CFX_XMLNode* CXFA_DocumentParser::ParseXMLData(const ByteString& wsXML) { - auto pStream = pdfium::MakeRetain( - wsXML.raw_str(), wsXML.GetLength()); + auto pStream = + pdfium::MakeRetain(wsXML.AsRawSpan()); xml_doc_ = LoadXML(pStream); if (!xml_doc_) return nullptr; diff --git a/xfa/fxfa/parser/cxfa_node.cpp b/xfa/fxfa/parser/cxfa_node.cpp index 06bbb0936a..a60e66584b 100644 --- a/xfa/fxfa/parser/cxfa_node.cpp +++ b/xfa/fxfa/parser/cxfa_node.cpp @@ -29,6 +29,7 @@ #include "third_party/base/compiler_specific.h" #include "third_party/base/logging.h" #include "third_party/base/ptr_util.h" +#include "third_party/base/span.h" #include "third_party/base/stl_util.h" #include "xfa/fde/cfde_textout.h" #include "xfa/fgas/font/cfgas_fontmgr.h" @@ -199,7 +200,6 @@ RetainPtr XFA_LoadImageData(CXFA_FFDoc* pDoc, return nullptr; FXCODEC_IMAGE_TYPE type = XFA_GetImageType(pImage->GetContentType()); - ByteString bsContent; std::vector buffer; RetainPtr pImageFileRead; if (wsImage.GetLength() > 0) { @@ -209,13 +209,12 @@ RetainPtr XFA_LoadImageData(CXFA_FFDoc* pDoc, buffer.resize(bsData.GetLength()); int32_t iRead = XFA_Base64Decode(bsData.c_str(), buffer.data()); if (iRead > 0) { - pImageFileRead = - pdfium::MakeRetain(buffer.data(), iRead); + pImageFileRead = pdfium::MakeRetain( + pdfium::make_span(buffer.data(), iRead)); } } else { - bsContent = wsImage.ToDefANSI(); pImageFileRead = pdfium::MakeRetain( - bsContent.raw_str(), bsContent.GetLength()); + wsImage.ToDefANSI().AsRawSpan()); } } else { WideString wsURL = wsHref; diff --git a/xfa/fxfa/parser/cxfa_xmllocale.cpp b/xfa/fxfa/parser/cxfa_xmllocale.cpp index 2806a084d6..20557dd59c 100644 --- a/xfa/fxfa/parser/cxfa_xmllocale.cpp +++ b/xfa/fxfa/parser/cxfa_xmllocale.cpp @@ -31,8 +31,7 @@ constexpr wchar_t kCurrencySymbol[] = L"currencySymbol"; // static std::unique_ptr CXFA_XMLLocale::Create( pdfium::span data) { - auto stream = - pdfium::MakeRetain(data.data(), data.size()); + auto stream = pdfium::MakeRetain(data); CFX_XMLParser parser(stream); auto doc = parser.Parse(); if (!doc) -- cgit v1.2.3