Change CFX_BufferSeekableReadStream to take a span.

Change-Id: Ib9e20fdfc637b2ba0358586e23ad72454b0b8ad1
Reviewed-on: https://pdfium-review.googlesource.com/38851
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/parser/cpdf_cross_ref_avail_unittest.cpp b/core/fpdfapi/parser/cpdf_cross_ref_avail_unittest.cpp
index 4ee6b59..eab7330 100644
--- a/core/fpdfapi/parser/cpdf_cross_ref_avail_unittest.cpp
+++ b/core/fpdfapi/parser/cpdf_cross_ref_avail_unittest.cpp
@@ -15,10 +15,9 @@
 namespace {
 
 std::unique_ptr<CPDF_SyntaxParser> MakeParserForBuffer(
-    const unsigned char* buffer,
-    size_t buffer_size) {
+    pdfium::span<const uint8_t> buffer) {
   return pdfium::MakeUnique<CPDF_SyntaxParser>(
-      pdfium::MakeRetain<CFX_BufferSeekableReadStream>(buffer, buffer_size));
+      pdfium::MakeRetain<CFX_BufferSeekableReadStream>(buffer));
 }
 
 }  // namespace
@@ -39,7 +38,7 @@
       "/Info 15 0 R/Size 16>>";
   const FX_FILESIZE last_crossref_offset = 0;
 
-  auto parser = MakeParserForBuffer(xref_table, FX_ArraySize(xref_table));
+  auto parser = MakeParserForBuffer(xref_table);
   auto cross_ref_avail = pdfium::MakeUnique<CPDF_CrossRefAvail>(
       parser.get(), last_crossref_offset);
 
@@ -56,7 +55,7 @@
       "endobj\n";
   const FX_FILESIZE last_crossref_offset = 0;
 
-  auto parser = MakeParserForBuffer(xref_stream, FX_ArraySize(xref_stream));
+  auto parser = MakeParserForBuffer(xref_stream);
   auto cross_ref_avail = pdfium::MakeUnique<CPDF_CrossRefAvail>(
       parser.get(), last_crossref_offset);
 
@@ -74,7 +73,7 @@
 
   const FX_FILESIZE last_crossref_offset = 70000;
 
-  auto parser = MakeParserForBuffer(xref_stream, FX_ArraySize(xref_stream));
+  auto parser = MakeParserForBuffer(xref_stream);
   auto cross_ref_avail = pdfium::MakeUnique<CPDF_CrossRefAvail>(
       parser.get(), last_crossref_offset);
 
@@ -91,7 +90,7 @@
       "endobj\n";
   const FX_FILESIZE last_crossref_offset = 0;
 
-  auto parser = MakeParserForBuffer(xref_stream, FX_ArraySize(xref_stream));
+  auto parser = MakeParserForBuffer(xref_stream);
   auto cross_ref_avail = pdfium::MakeUnique<CPDF_CrossRefAvail>(
       parser.get(), last_crossref_offset);
   EXPECT_EQ(CPDF_DataAvail::DataError, cross_ref_avail->CheckAvail());
@@ -113,7 +112,7 @@
       "/Info 15 0 R/Size 16 /XRefStm 70000>>";
   const FX_FILESIZE last_crossref_offset = 0;
 
-  auto parser = MakeParserForBuffer(xref_table, FX_ArraySize(xref_table));
+  auto parser = MakeParserForBuffer(xref_table);
   auto cross_ref_avail = pdfium::MakeUnique<CPDF_CrossRefAvail>(
       parser.get(), last_crossref_offset);
   EXPECT_EQ(CPDF_DataAvail::DataError, cross_ref_avail->CheckAvail());
@@ -125,8 +124,7 @@
       "wfoihoiwfghouiafghwoigahfi";
   const FX_FILESIZE last_crossref_offset = 0;
 
-  auto parser =
-      MakeParserForBuffer(incorrect_data, FX_ArraySize(incorrect_data));
+  auto parser = MakeParserForBuffer(incorrect_data);
   auto cross_ref_avail = pdfium::MakeUnique<CPDF_CrossRefAvail>(
       parser.get(), last_crossref_offset);
   EXPECT_EQ(CPDF_DataAvail::DataError, cross_ref_avail->CheckAvail());
@@ -174,8 +172,7 @@
            FXSYS_itoa(static_cast<int>(prev_offset), int_buffer, 10) + ">>\n";
   const FX_FILESIZE last_crossref_offset = static_cast<FX_FILESIZE>(cur_offset);
 
-  auto parser = MakeParserForBuffer(
-      reinterpret_cast<const unsigned char*>(table.data()), table.size());
+  auto parser = MakeParserForBuffer(pdfium::as_bytes(pdfium::make_span(table)));
   auto cross_ref_avail = pdfium::MakeUnique<CPDF_CrossRefAvail>(
       parser.get(), last_crossref_offset);
   EXPECT_EQ(CPDF_DataAvail::DataAvailable, cross_ref_avail->CheckAvail());
@@ -219,8 +216,7 @@
            "endobj\n";
   const FX_FILESIZE last_crossref_offset = static_cast<FX_FILESIZE>(cur_offset);
 
-  auto parser = MakeParserForBuffer(
-      reinterpret_cast<const unsigned char*>(table.data()), table.size());
+  auto parser = MakeParserForBuffer(pdfium::as_bytes(pdfium::make_span(table)));
   auto cross_ref_avail = pdfium::MakeUnique<CPDF_CrossRefAvail>(
       parser.get(), last_crossref_offset);
   EXPECT_EQ(CPDF_DataAvail::DataAvailable, cross_ref_avail->CheckAvail());
@@ -269,8 +265,7 @@
            FXSYS_itoa(first_v5_table_offset, int_buffer, 10) + ">>\n";
   const FX_FILESIZE last_crossref_offset = last_v4_table_offset;
 
-  auto parser = MakeParserForBuffer(
-      reinterpret_cast<const unsigned char*>(table.data()), table.size());
+  auto parser = MakeParserForBuffer(pdfium::as_bytes(pdfium::make_span(table)));
   auto cross_ref_avail = pdfium::MakeUnique<CPDF_CrossRefAvail>(
       parser.get(), last_crossref_offset);
   EXPECT_EQ(CPDF_DataAvail::DataAvailable, cross_ref_avail->CheckAvail());
@@ -284,8 +279,7 @@
       "endobj\n";
   const FX_FILESIZE last_crossref_offset = 0;
 
-  auto parser = MakeParserForBuffer(invalid_xref_stream,
-                                    FX_ArraySize(invalid_xref_stream));
+  auto parser = MakeParserForBuffer(invalid_xref_stream);
   auto cross_ref_avail = pdfium::MakeUnique<CPDF_CrossRefAvail>(
       parser.get(), last_crossref_offset);
   EXPECT_EQ(CPDF_DataAvail::DataError, cross_ref_avail->CheckAvail());
@@ -308,7 +302,7 @@
       "/Info 15 0 R/Size 16>>";
   const FX_FILESIZE last_crossref_offset = 0;
 
-  auto parser = MakeParserForBuffer(xref_table, FX_ArraySize(xref_table));
+  auto parser = MakeParserForBuffer(xref_table);
   auto cross_ref_avail = pdfium::MakeUnique<CPDF_CrossRefAvail>(
       parser.get(), last_crossref_offset);
   EXPECT_EQ(CPDF_DataAvail::DataError, cross_ref_avail->CheckAvail());
@@ -324,7 +318,7 @@
       "endobj\n";
   const FX_FILESIZE last_crossref_offset = 0;
 
-  auto parser = MakeParserForBuffer(xref_stream, FX_ArraySize(xref_stream));
+  auto parser = MakeParserForBuffer(xref_stream);
   auto cross_ref_avail = pdfium::MakeUnique<CPDF_CrossRefAvail>(
       parser.get(), last_crossref_offset);
   EXPECT_EQ(CPDF_DataAvail::DataError, cross_ref_avail->CheckAvail());
diff --git a/core/fpdfapi/parser/cpdf_hint_tables_unittest.cpp b/core/fpdfapi/parser/cpdf_hint_tables_unittest.cpp
index 34c1694..dba39f5 100644
--- a/core/fpdfapi/parser/cpdf_hint_tables_unittest.cpp
+++ b/core/fpdfapi/parser/cpdf_hint_tables_unittest.cpp
@@ -49,8 +49,7 @@
   static std::unique_ptr<CPDF_LinearizedHeader> MakeHeader(
       const std::string& inline_data) {
     CPDF_SyntaxParser parser(pdfium::MakeRetain<CFX_BufferSeekableReadStream>(
-        reinterpret_cast<const unsigned char*>(inline_data.data()),
-        inline_data.size()));
+        pdfium::as_bytes(pdfium::make_span(inline_data))));
     std::unique_ptr<CPDF_Dictionary> dict =
         ToDictionary(parser.GetObjectBody(nullptr));
     ASSERT(dict);
diff --git a/core/fpdfapi/parser/cpdf_parser_unittest.cpp b/core/fpdfapi/parser/cpdf_parser_unittest.cpp
index 2e7e060..d5166fb 100644
--- a/core/fpdfapi/parser/cpdf_parser_unittest.cpp
+++ b/core/fpdfapi/parser/cpdf_parser_unittest.cpp
@@ -47,17 +47,16 @@
   }
 
   // Setup reading from a buffer and initial states.
-  bool InitTestFromBufferWithOffset(const unsigned char* buffer,
-                                    size_t len,
+  bool InitTestFromBufferWithOffset(pdfium::span<const uint8_t> buffer,
                                     FX_FILESIZE header_offset) {
     m_pSyntax = CPDF_SyntaxParser::CreateForTesting(
-        pdfium::MakeRetain<CFX_BufferSeekableReadStream>(buffer, len),
+        pdfium::MakeRetain<CFX_BufferSeekableReadStream>(buffer),
         header_offset);
     return true;
   }
 
-  bool InitTestFromBuffer(const unsigned char* buffer, size_t len) {
-    return InitTestFromBufferWithOffset(buffer, len, 0 /*header_offset*/);
+  bool InitTestFromBuffer(pdfium::span<const uint8_t> buffer) {
+    return InitTestFromBufferWithOffset(buffer, 0 /*header_offset*/);
   }
 
  private:
@@ -109,8 +108,7 @@
         "0000000409 00000 n \n"
         "trail";  // Needed to end cross ref table reading.
     CPDF_TestParser parser;
-    ASSERT_TRUE(
-        parser.InitTestFromBuffer(xref_table, FX_ArraySize(xref_table)));
+    ASSERT_TRUE(parser.InitTestFromBuffer(xref_table));
 
     ASSERT_TRUE(parser.LoadCrossRefV4(0, false));
     const FX_FILESIZE offsets[] = {0, 17, 81, 0, 331, 409};
@@ -140,8 +138,7 @@
         "0000025777 00000 n \n"
         "trail";  // Needed to end cross ref table reading.
     CPDF_TestParser parser;
-    ASSERT_TRUE(
-        parser.InitTestFromBuffer(xref_table, FX_ArraySize(xref_table)));
+    ASSERT_TRUE(parser.InitTestFromBuffer(xref_table));
 
     ASSERT_TRUE(parser.LoadCrossRefV4(0, false));
     const FX_FILESIZE offsets[] = {0, 0,     0,     25325, 0, 0,    0,
@@ -179,8 +176,7 @@
         "0000025777 00000 n \n"
         "trail";  // Needed to end cross ref table reading.
     CPDF_TestParser parser;
-    ASSERT_TRUE(
-        parser.InitTestFromBuffer(xref_table, FX_ArraySize(xref_table)));
+    ASSERT_TRUE(parser.InitTestFromBuffer(xref_table));
 
     ASSERT_TRUE(parser.LoadCrossRefV4(0, false));
     const FX_FILESIZE offsets[] = {0, 0, 0,     25325, 0, 0,    0,
@@ -217,8 +213,7 @@
         "0000000179 00000 n \n"
         "trail";  // Needed to end cross ref table reading.
     CPDF_TestParser parser;
-    ASSERT_TRUE(
-        parser.InitTestFromBuffer(xref_table, FX_ArraySize(xref_table)));
+    ASSERT_TRUE(parser.InitTestFromBuffer(xref_table));
 
     ASSERT_TRUE(parser.LoadCrossRefV4(0, false));
     const FX_FILESIZE offsets[] = {0, 23, 0, 0, 0, 45, 179};
@@ -264,8 +259,7 @@
   ASSERT_TRUE(pFileAccess->ReadBlock(&data.front() + kTestHeaderOffset, 0,
                                      pFileAccess->GetSize()));
   CPDF_TestParser parser;
-  parser.InitTestFromBufferWithOffset(&data.front(), data.size(),
-                                      kTestHeaderOffset);
+  parser.InitTestFromBufferWithOffset(data, kTestHeaderOffset);
 
   EXPECT_EQ(100940, parser.ParseStartXRef());
   std::unique_ptr<CPDF_Object> cross_ref_v5_obj =
@@ -286,8 +280,7 @@
   ASSERT_TRUE(pFileAccess->ReadBlock(&data.front() + kTestHeaderOffset, 0,
                                      pFileAccess->GetSize()));
   CPDF_TestParser parser;
-  parser.InitTestFromBufferWithOffset(&data.front(), data.size(),
-                                      kTestHeaderOffset);
+  parser.InitTestFromBufferWithOffset(data, kTestHeaderOffset);
 
   EXPECT_TRUE(parser.ParseLinearizedHeader());
 }
diff --git a/testing/fuzzers/xfa_codec_fuzzer.h b/testing/fuzzers/xfa_codec_fuzzer.h
index bfc069a..bc40f65 100644
--- a/testing/fuzzers/xfa_codec_fuzzer.h
+++ b/testing/fuzzers/xfa_codec_fuzzer.h
@@ -12,6 +12,7 @@
 #include "core/fxge/dib/cfx_dibitmap.h"
 #include "testing/fx_string_testhelpers.h"
 #include "third_party/base/ptr_util.h"
+#include "third_party/base/span.h"
 
 #ifdef PDF_ENABLE_XFA_BMP
 #include "core/fxcodec/codec/ccodec_bmpmodule.h"
@@ -52,7 +53,8 @@
 
     std::unique_ptr<CCodec_ProgressiveDecoder> decoder =
         mgr->CreateProgressiveDecoder();
-    auto source = pdfium::MakeRetain<CFX_BufferSeekableReadStream>(data, size);
+    auto source = pdfium::MakeRetain<CFX_BufferSeekableReadStream>(
+        pdfium::make_span(data, size));
     FXCODEC_STATUS status = decoder->LoadImageInfo(source, type, nullptr, true);
     if (status != FXCODEC_STATUS_FRAME_READY)
       return 0;
diff --git a/testing/fx_string_testhelpers.cpp b/testing/fx_string_testhelpers.cpp
index e2ce935..e3c443a 100644
--- a/testing/fx_string_testhelpers.cpp
+++ b/testing/fx_string_testhelpers.cpp
@@ -24,8 +24,22 @@
 CFX_InvalidSeekableReadStream::~CFX_InvalidSeekableReadStream() = default;
 
 CFX_BufferSeekableReadStream::CFX_BufferSeekableReadStream(
-    const unsigned char* src,
-    size_t src_size)
-    : data_(src), data_size_(src_size) {}
+    pdfium::span<const uint8_t> data)
+    : data_(data) {}
 
 CFX_BufferSeekableReadStream::~CFX_BufferSeekableReadStream() = default;
+
+bool CFX_BufferSeekableReadStream::ReadBlock(void* buffer,
+                                             FX_FILESIZE offset,
+                                             size_t size) {
+  if (offset < 0 || static_cast<size_t>(offset) >= data_.size())
+    return false;
+
+  if (static_cast<size_t>(offset) + size > data_.size())
+    size = data_.size() - static_cast<size_t>(offset);
+  if (size == 0)
+    return false;
+
+  memcpy(buffer, &data_[offset], size);
+  return true;
+}
diff --git a/testing/fx_string_testhelpers.h b/testing/fx_string_testhelpers.h
index cb59cfb..4cddefa 100644
--- a/testing/fx_string_testhelpers.h
+++ b/testing/fx_string_testhelpers.h
@@ -9,6 +9,7 @@
 
 #include "core/fxcrt/cfx_datetime.h"
 #include "core/fxcrt/fx_stream.h"
+#include "third_party/base/span.h"
 
 // Output stream operator so GTEST macros work with CFX_DateTime objects.
 std::ostream& operator<<(std::ostream& os, const CFX_DateTime& dt);
@@ -37,29 +38,17 @@
   friend RetainPtr<T> pdfium::MakeRetain(Args&&... args);
 
   // IFX_SeekableReadStream:
-  bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override {
-    if (offset < 0 || static_cast<size_t>(offset) >= data_size_)
-      return false;
-
-    if (static_cast<size_t>(offset) + size > data_size_)
-      size = data_size_ - static_cast<size_t>(offset);
-    if (size == 0)
-      return false;
-
-    memcpy(buffer, data_ + offset, size);
-    return true;
-  }
+  bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override;
 
   FX_FILESIZE GetSize() override {
-    return static_cast<FX_FILESIZE>(data_size_);
+    return static_cast<FX_FILESIZE>(data_.size());
   }
 
  private:
-  CFX_BufferSeekableReadStream(const unsigned char* src, size_t src_size);
+  explicit CFX_BufferSeekableReadStream(pdfium::span<const uint8_t> data);
   ~CFX_BufferSeekableReadStream() override;
 
-  const unsigned char* data_;
-  size_t data_size_;
+  pdfium::span<const uint8_t> data_;
 };
 
 #endif  // TESTING_FX_STRING_TESTHELPERS_H_