Make CFX_ReadOnlyMemoryStream take a span.

Change-Id: Id097320ab2d9b5d1579582e5797e29c701499501
Reviewed-on: https://pdfium-review.googlesource.com/39991
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/parser/cfdf_document.cpp b/core/fpdfapi/parser/cfdf_document.cpp
index d37c8d5..421af48 100644
--- a/core/fpdfapi/parser/cfdf_document.cpp
+++ b/core/fpdfapi/parser/cfdf_document.cpp
@@ -32,8 +32,7 @@
 std::unique_ptr<CFDF_Document> CFDF_Document::ParseMemory(
     pdfium::span<const uint8_t> span) {
   auto pDoc = pdfium::MakeUnique<CFDF_Document>();
-  pDoc->ParseStream(
-      pdfium::MakeRetain<CFX_ReadOnlyMemoryStream>(span.data(), span.size()));
+  pDoc->ParseStream(pdfium::MakeRetain<CFX_ReadOnlyMemoryStream>(span));
   return pDoc->m_pRootDict ? std::move(pDoc) : nullptr;
 }
 
diff --git a/core/fpdfdoc/cpdf_metadata.cpp b/core/fpdfdoc/cpdf_metadata.cpp
index b9a9296..99fd965 100644
--- a/core/fpdfdoc/cpdf_metadata.cpp
+++ b/core/fpdfdoc/cpdf_metadata.cpp
@@ -67,8 +67,7 @@
   auto pAcc = pdfium::MakeRetain<CPDF_StreamAcc>(stream_.Get());
   pAcc->LoadAllDataFiltered();
 
-  auto stream = pdfium::MakeRetain<CFX_ReadOnlyMemoryStream>(pAcc->GetData(),
-                                                             pAcc->GetSize());
+  auto stream = pdfium::MakeRetain<CFX_ReadOnlyMemoryStream>(pAcc->GetSpan());
   CFX_XMLParser parser(stream);
   std::unique_ptr<CFX_XMLDocument> doc = parser.Parse();
   if (!doc)
diff --git a/core/fxcrt/cfx_readonlymemorystream.cpp b/core/fxcrt/cfx_readonlymemorystream.cpp
index 1d09759..a01ed2e 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<const uint8_t> span)
+    : m_span(span) {}
 
 CFX_ReadOnlyMemoryStream::~CFX_ReadOnlyMemoryStream() = default;
 
 FX_FILESIZE CFX_ReadOnlyMemoryStream::GetSize() {
-  return m_size;
+  return pdfium::base::checked_cast<FX_FILESIZE>(m_span.size());
 }
 
 bool CFX_ReadOnlyMemoryStream::ReadBlock(void* buffer,
@@ -24,11 +24,11 @@
   if (offset < 0)
     return false;
 
-  FX_SAFE_FILESIZE newPos = pdfium::base::checked_cast<FX_FILESIZE>(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 e9d1822..a6645b8 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 @@
   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<const uint8_t> span);
   ~CFX_ReadOnlyMemoryStream() override;
 
-  const uint8_t* const m_pBuf;
-  const FX_FILESIZE m_size;
+  const pdfium::span<const uint8_t> m_span;
 };
 
 #endif  // CORE_FXCRT_CFX_READONLYMEMORYSTREAM_H_
diff --git a/fpdfsdk/fpdf_view.cpp b/fpdfsdk/fpdf_view.cpp
index bd8cfe1..0cfa362 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_DOCUMENT FPDF_CALLCONV
 FPDF_LoadMemDocument(const void* data_buf, int size, FPDF_BYTESTRING password) {
-  return LoadDocumentImpl(pdfium::MakeRetain<CFX_ReadOnlyMemoryStream>(
-                              static_cast<const uint8_t*>(data_buf), size),
-                          password);
+  return LoadDocumentImpl(
+      pdfium::MakeRetain<CFX_ReadOnlyMemoryStream>(
+          pdfium::make_span(static_cast<const uint8_t*>(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 7f5fdb1..efc1716 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<CFX_ReadOnlyMemoryStream>(data, size);
+  auto stream = pdfium::MakeRetain<CFX_ReadOnlyMemoryStream>(
+      pdfium::make_span(data, size));
   CFX_XMLParser parser(stream);
   std::unique_ptr<CFX_XMLDocument> doc = parser.Parse();
   if (!doc || !doc->GetRoot())
diff --git a/xfa/fxfa/cxfa_ffdoc.cpp b/xfa/fxfa/cxfa_ffdoc.cpp
index 7852d30..44c1b79 100644
--- a/xfa/fxfa/cxfa_ffdoc.cpp
+++ b/xfa/fxfa/cxfa_ffdoc.cpp
@@ -215,8 +215,8 @@
   auto pAcc = pdfium::MakeRetain<CPDF_StreamAcc>(pStream);
   pAcc->LoadAllDataFiltered();
 
-  auto pImageFileRead = pdfium::MakeRetain<CFX_ReadOnlyMemoryStream>(
-      pAcc->GetData(), pAcc->GetSize());
+  auto pImageFileRead =
+      pdfium::MakeRetain<CFX_ReadOnlyMemoryStream>(pAcc->GetSpan());
 
   RetainPtr<CFX_DIBitmap> 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 5cb0886..d0f137d 100644
--- a/xfa/fxfa/parser/cxfa_document_parser.cpp
+++ b/xfa/fxfa/parser/cxfa_document_parser.cpp
@@ -332,8 +332,8 @@
 }
 
 CFX_XMLNode* CXFA_DocumentParser::ParseXMLData(const ByteString& wsXML) {
-  auto pStream = pdfium::MakeRetain<CFX_ReadOnlyMemoryStream>(
-      wsXML.raw_str(), wsXML.GetLength());
+  auto pStream =
+      pdfium::MakeRetain<CFX_ReadOnlyMemoryStream>(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 06bbb09..a60e665 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 @@
     return nullptr;
 
   FXCODEC_IMAGE_TYPE type = XFA_GetImageType(pImage->GetContentType());
-  ByteString bsContent;
   std::vector<uint8_t> buffer;
   RetainPtr<IFX_SeekableReadStream> pImageFileRead;
   if (wsImage.GetLength() > 0) {
@@ -209,13 +209,12 @@
       buffer.resize(bsData.GetLength());
       int32_t iRead = XFA_Base64Decode(bsData.c_str(), buffer.data());
       if (iRead > 0) {
-        pImageFileRead =
-            pdfium::MakeRetain<CFX_ReadOnlyMemoryStream>(buffer.data(), iRead);
+        pImageFileRead = pdfium::MakeRetain<CFX_ReadOnlyMemoryStream>(
+            pdfium::make_span(buffer.data(), iRead));
       }
     } else {
-      bsContent = wsImage.ToDefANSI();
       pImageFileRead = pdfium::MakeRetain<CFX_ReadOnlyMemoryStream>(
-          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 2806a08..20557dd 100644
--- a/xfa/fxfa/parser/cxfa_xmllocale.cpp
+++ b/xfa/fxfa/parser/cxfa_xmllocale.cpp
@@ -31,8 +31,7 @@
 // static
 std::unique_ptr<CXFA_XMLLocale> CXFA_XMLLocale::Create(
     pdfium::span<uint8_t> data) {
-  auto stream =
-      pdfium::MakeRetain<CFX_ReadOnlyMemoryStream>(data.data(), data.size());
+  auto stream = pdfium::MakeRetain<CFX_ReadOnlyMemoryStream>(data);
   CFX_XMLParser parser(stream);
   auto doc = parser.Parse();
   if (!doc)