Use pdfium::span<> in more image modules

Change-Id: Ie344bb37abf7dde158d03cc2897dca3588f1a5e3
Reviewed-on: https://pdfium-review.googlesource.com/41550
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/page/cpdf_streamparser.cpp b/core/fpdfapi/page/cpdf_streamparser.cpp
index bafb582..266f7db 100644
--- a/core/fpdfapi/page/cpdf_streamparser.cpp
+++ b/core/fpdfapi/page/cpdf_streamparser.cpp
@@ -71,7 +71,7 @@
                             uint32_t* dest_size) {
   if (decoder == "CCITTFaxDecode" || decoder == "CCF") {
     std::unique_ptr<CCodec_ScanlineDecoder> pDecoder =
-        FPDFAPI_CreateFaxDecoder(src_buf, limit, width, height, pParam);
+        FPDFAPI_CreateFaxDecoder({src_buf, limit}, width, height, pParam);
     return DecodeAllScanlines(std::move(pDecoder), dest_buf, dest_size);
   }
   if (decoder == "ASCII85Decode" || decoder == "A85")
diff --git a/core/fpdfapi/parser/fpdf_parser_decode.cpp b/core/fpdfapi/parser/fpdf_parser_decode.cpp
index 063d21a..ff4f63f 100644
--- a/core/fpdfapi/parser/fpdf_parser_decode.cpp
+++ b/core/fpdfapi/parser/fpdf_parser_decode.cpp
@@ -258,8 +258,7 @@
 }
 
 std::unique_ptr<CCodec_ScanlineDecoder> FPDFAPI_CreateFaxDecoder(
-    const uint8_t* src_buf,
-    uint32_t src_size,
+    pdfium::span<const uint8_t> src_span,
     int width,
     int height,
     const CPDF_Dictionary* pParams) {
@@ -280,13 +279,12 @@
       Rows = 0;
   }
   return CPDF_ModuleMgr::Get()->GetFaxModule()->CreateDecoder(
-      src_buf, src_size, width, height, K, EndOfLine, ByteAlign, BlackIs1,
-      Columns, Rows);
+      src_span, width, height, K, EndOfLine, ByteAlign, BlackIs1, Columns,
+      Rows);
 }
 
 std::unique_ptr<CCodec_ScanlineDecoder> FPDFAPI_CreateFlateDecoder(
-    const uint8_t* src_buf,
-    uint32_t src_size,
+    pdfium::span<const uint8_t> src_span,
     int width,
     int height,
     int nComps,
@@ -305,8 +303,8 @@
       return nullptr;
   }
   return CPDF_ModuleMgr::Get()->GetFlateModule()->CreateDecoder(
-      src_buf, src_size, width, height, nComps, bpc, predictor, Colors,
-      BitsPerComponent, Columns);
+      src_span, width, height, nComps, bpc, predictor, Colors, BitsPerComponent,
+      Columns);
 }
 
 uint32_t FPDFAPI_FlateOrLZWDecode(bool bLZW,
diff --git a/core/fpdfapi/parser/fpdf_parser_decode.h b/core/fpdfapi/parser/fpdf_parser_decode.h
index 6522999..bf70997 100644
--- a/core/fpdfapi/parser/fpdf_parser_decode.h
+++ b/core/fpdfapi/parser/fpdf_parser_decode.h
@@ -39,15 +39,13 @@
                          uint32_t* dest_size);
 
 std::unique_ptr<CCodec_ScanlineDecoder> FPDFAPI_CreateFaxDecoder(
-    const uint8_t* src_buf,
-    uint32_t src_size,
+    pdfium::span<const uint8_t> src_span,
     int width,
     int height,
     const CPDF_Dictionary* pParams);
 
 std::unique_ptr<CCodec_ScanlineDecoder> FPDFAPI_CreateFlateDecoder(
-    const uint8_t* src_buf,
-    uint32_t src_size,
+    pdfium::span<const uint8_t> src_span,
     int width,
     int height,
     int nComps,
diff --git a/core/fpdfapi/render/cpdf_dibbase.cpp b/core/fpdfapi/render/cpdf_dibbase.cpp
index dd710f6..70aea8e 100644
--- a/core/fpdfapi/render/cpdf_dibbase.cpp
+++ b/core/fpdfapi/render/cpdf_dibbase.cpp
@@ -470,21 +470,19 @@
     return LoadState::kContinue;
   }
 
-  const uint8_t* src_data = m_pStreamAcc->GetData();
-  uint32_t src_size = m_pStreamAcc->GetSize();
+  pdfium::span<const uint8_t> src_span = m_pStreamAcc->GetSpan();
   const CPDF_Dictionary* pParams = m_pStreamAcc->GetImageParam();
   if (decoder == "CCITTFaxDecode") {
-    m_pDecoder = FPDFAPI_CreateFaxDecoder(src_data, src_size, m_Width, m_Height,
-                                          pParams);
+    m_pDecoder = FPDFAPI_CreateFaxDecoder(src_span, m_Width, m_Height, pParams);
   } else if (decoder == "FlateDecode") {
-    m_pDecoder = FPDFAPI_CreateFlateDecoder(
-        src_data, src_size, m_Width, m_Height, m_nComponents, m_bpc, pParams);
+    m_pDecoder = FPDFAPI_CreateFlateDecoder(src_span, m_Width, m_Height,
+                                            m_nComponents, m_bpc, pParams);
   } else if (decoder == "RunLengthDecode") {
     CCodec_ModuleMgr* pEncoders = CPDF_ModuleMgr::Get()->GetCodecModule();
     m_pDecoder = pEncoders->GetBasicModule()->CreateRunLengthDecoder(
-        {src_data, src_size}, m_Width, m_Height, m_nComponents, m_bpc);
+        src_span, m_Width, m_Height, m_nComponents, m_bpc);
   } else if (decoder == "DCTDecode") {
-    if (!CreateDCTDecoder({src_data, src_size}, pParams))
+    if (!CreateDCTDecoder(src_span, pParams))
       return LoadState::kFail;
   }
   if (!m_pDecoder)
@@ -576,8 +574,8 @@
 RetainPtr<CFX_DIBitmap> CPDF_DIBBase::LoadJpxBitmap() {
   CCodec_JpxModule* pJpxModule = CPDF_ModuleMgr::Get()->GetJpxModule();
   auto context = pdfium::MakeUnique<JpxBitMapContext>(pJpxModule);
-  context->set_decoder(pJpxModule->CreateDecoder(
-      m_pStreamAcc->GetData(), m_pStreamAcc->GetSize(), m_pColorSpace.Get()));
+  context->set_decoder(
+      pJpxModule->CreateDecoder(m_pStreamAcc->GetSpan(), m_pColorSpace.Get()));
   if (!context->decoder())
     return nullptr;
 
diff --git a/core/fxcodec/codec/ccodec_faxmodule.cpp b/core/fxcodec/codec/ccodec_faxmodule.cpp
index af218f7..faa40da 100644
--- a/core/fxcodec/codec/ccodec_faxmodule.cpp
+++ b/core/fxcodec/codec/ccodec_faxmodule.cpp
@@ -450,8 +450,7 @@
 
 class CCodec_FaxDecoder final : public CCodec_ScanlineDecoder {
  public:
-  CCodec_FaxDecoder(const uint8_t* src_buf,
-                    uint32_t src_size,
+  CCodec_FaxDecoder(pdfium::span<const uint8_t> src_span,
                     int width,
                     int height,
                     uint32_t pitch,
@@ -468,18 +467,16 @@
 
  private:
   const int m_Encoding;
-  int m_bitpos;
-  bool m_bByteAlign;
+  int m_bitpos = 0;
+  bool m_bByteAlign = false;
   const bool m_bEndOfLine;
   const bool m_bBlack;
-  const uint32_t m_SrcSize;
-  const uint8_t* const m_pSrcBuf;
+  pdfium::span<const uint8_t> const m_SrcSpan;
   std::vector<uint8_t> m_ScanlineBuf;
   std::vector<uint8_t> m_RefBuf;
 };
 
-CCodec_FaxDecoder::CCodec_FaxDecoder(const uint8_t* src_buf,
-                                     uint32_t src_size,
+CCodec_FaxDecoder::CCodec_FaxDecoder(pdfium::span<const uint8_t> src_span,
                                      int width,
                                      int height,
                                      uint32_t pitch,
@@ -489,12 +486,10 @@
                                      bool BlackIs1)
     : CCodec_ScanlineDecoder(width, height, width, height, 1, 1, pitch),
       m_Encoding(K),
-      m_bitpos(0),
       m_bByteAlign(EncodedByteAlign),
       m_bEndOfLine(EndOfLine),
       m_bBlack(BlackIs1),
-      m_SrcSize(src_size),
-      m_pSrcBuf(src_buf),
+      m_SrcSpan(src_span),
       m_ScanlineBuf(pitch),
       m_RefBuf(pitch) {}
 
@@ -507,35 +502,37 @@
 }
 
 uint8_t* CCodec_FaxDecoder::v_GetNextLine() {
-  int bitsize = m_SrcSize * 8;
-  FaxSkipEOL(m_pSrcBuf, bitsize, &m_bitpos);
+  int bitsize = m_SrcSpan.size() * 8;
+  FaxSkipEOL(m_SrcSpan.data(), bitsize, &m_bitpos);
   if (m_bitpos >= bitsize)
     return nullptr;
 
   memset(m_ScanlineBuf.data(), 0xff, m_ScanlineBuf.size());
   if (m_Encoding < 0) {
-    FaxG4GetRow(m_pSrcBuf, bitsize, &m_bitpos, m_ScanlineBuf.data(), m_RefBuf,
-                m_OrigWidth);
+    FaxG4GetRow(m_SrcSpan.data(), bitsize, &m_bitpos, m_ScanlineBuf.data(),
+                m_RefBuf, m_OrigWidth);
     m_RefBuf = m_ScanlineBuf;
   } else if (m_Encoding == 0) {
-    FaxGet1DLine(m_pSrcBuf, bitsize, &m_bitpos, &m_ScanlineBuf, m_OrigWidth);
+    FaxGet1DLine(m_SrcSpan.data(), bitsize, &m_bitpos, &m_ScanlineBuf,
+                 m_OrigWidth);
   } else {
-    if (NextBit(m_pSrcBuf, &m_bitpos)) {
-      FaxGet1DLine(m_pSrcBuf, bitsize, &m_bitpos, &m_ScanlineBuf, m_OrigWidth);
+    if (NextBit(m_SrcSpan.data(), &m_bitpos)) {
+      FaxGet1DLine(m_SrcSpan.data(), bitsize, &m_bitpos, &m_ScanlineBuf,
+                   m_OrigWidth);
     } else {
-      FaxG4GetRow(m_pSrcBuf, bitsize, &m_bitpos, m_ScanlineBuf.data(), m_RefBuf,
-                  m_OrigWidth);
+      FaxG4GetRow(m_SrcSpan.data(), bitsize, &m_bitpos, m_ScanlineBuf.data(),
+                  m_RefBuf, m_OrigWidth);
     }
     m_RefBuf = m_ScanlineBuf;
   }
   if (m_bEndOfLine)
-    FaxSkipEOL(m_pSrcBuf, bitsize, &m_bitpos);
+    FaxSkipEOL(m_SrcSpan.data(), bitsize, &m_bitpos);
 
   if (m_bByteAlign && m_bitpos < bitsize) {
     int bitpos0 = m_bitpos;
     int bitpos1 = (m_bitpos + 7) / 8 * 8;
     while (m_bByteAlign && bitpos0 < bitpos1) {
-      int bit = m_pSrcBuf[bitpos0 / 8] & (1 << (7 - bitpos0 % 8));
+      int bit = m_SrcSpan[bitpos0 / 8] & (1 << (7 - bitpos0 % 8));
       if (bit != 0)
         m_bByteAlign = false;
       else
@@ -555,7 +552,7 @@
 }
 
 uint32_t CCodec_FaxDecoder::GetSrcOffset() {
-  return std::min(static_cast<uint32_t>((m_bitpos + 7) / 8), m_SrcSize);
+  return std::min(static_cast<size_t>((m_bitpos + 7) / 8), m_SrcSpan.size());
 }
 
 void FaxG4Decode(const uint8_t* src_buf,
@@ -580,8 +577,7 @@
 }
 
 std::unique_ptr<CCodec_ScanlineDecoder> CCodec_FaxModule::CreateDecoder(
-    const uint8_t* src_buf,
-    uint32_t src_size,
+    pdfium::span<const uint8_t> src_span,
     int width,
     int height,
     int K,
@@ -603,7 +599,7 @@
 
   uint32_t pitch = (static_cast<uint32_t>(actual_width) + 31) / 32 * 4;
   return pdfium::MakeUnique<CCodec_FaxDecoder>(
-      src_buf, src_size, actual_width, actual_height, pitch, K, EndOfLine,
+      src_span, actual_width, actual_height, pitch, K, EndOfLine,
       EncodedByteAlign, BlackIs1);
 }
 
diff --git a/core/fxcodec/codec/ccodec_faxmodule.h b/core/fxcodec/codec/ccodec_faxmodule.h
index 4f8ab78..3d546d7 100644
--- a/core/fxcodec/codec/ccodec_faxmodule.h
+++ b/core/fxcodec/codec/ccodec_faxmodule.h
@@ -11,21 +11,23 @@
 
 #include "core/fxcrt/fx_memory.h"
 #include "core/fxcrt/fx_system.h"
+#include "third_party/base/span.h"
 
 class CCodec_ScanlineDecoder;
 
 class CCodec_FaxModule {
  public:
-  std::unique_ptr<CCodec_ScanlineDecoder> CreateDecoder(const uint8_t* src_buf,
-                                                        uint32_t src_size,
-                                                        int width,
-                                                        int height,
-                                                        int K,
-                                                        bool EndOfLine,
-                                                        bool EncodedByteAlign,
-                                                        bool BlackIs1,
-                                                        int Columns,
-                                                        int Rows);
+  std::unique_ptr<CCodec_ScanlineDecoder> CreateDecoder(
+      pdfium::span<const uint8_t> src_buf,
+      int width,
+      int height,
+      int K,
+      bool EndOfLine,
+      bool EncodedByteAlign,
+      bool BlackIs1,
+      int Columns,
+      int Rows);
+
 #if _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
   static void FaxEncode(const uint8_t* src_buf,
                         int width,
diff --git a/core/fxcodec/codec/ccodec_flatemodule.cpp b/core/fxcodec/codec/ccodec_flatemodule.cpp
index 9074038..0a4baea 100644
--- a/core/fxcodec/codec/ccodec_flatemodule.cpp
+++ b/core/fxcodec/codec/ccodec_flatemodule.cpp
@@ -568,8 +568,7 @@
 
 class CCodec_FlateScanlineDecoder : public CCodec_ScanlineDecoder {
  public:
-  CCodec_FlateScanlineDecoder(const uint8_t* src_buf,
-                              uint32_t src_size,
+  CCodec_FlateScanlineDecoder(pdfium::span<const uint8_t> src_buf,
                               int width,
                               int height,
                               int nComps,
@@ -587,12 +586,12 @@
   std::unique_ptr<uint8_t, FxFreeDeleter> const m_pScanline;
 };
 
-CCodec_FlateScanlineDecoder::CCodec_FlateScanlineDecoder(const uint8_t* src_buf,
-                                                         uint32_t src_size,
-                                                         int width,
-                                                         int height,
-                                                         int nComps,
-                                                         int bpc)
+CCodec_FlateScanlineDecoder::CCodec_FlateScanlineDecoder(
+    pdfium::span<const uint8_t> src_span,
+    int width,
+    int height,
+    int nComps,
+    int bpc)
     : CCodec_ScanlineDecoder(width,
                              height,
                              width,
@@ -600,7 +599,7 @@
                              nComps,
                              bpc,
                              CalculatePitch8(bpc, nComps, width).ValueOrDie()),
-      m_SrcBuf(src_buf, src_size),
+      m_SrcBuf(src_span),
       m_pScanline(FX_Alloc(uint8_t, m_Pitch)) {}
 
 CCodec_FlateScanlineDecoder::~CCodec_FlateScanlineDecoder() = default;
@@ -626,8 +625,7 @@
 class CCodec_FlatePredictorScanlineDecoder final
     : public CCodec_FlateScanlineDecoder {
  public:
-  CCodec_FlatePredictorScanlineDecoder(const uint8_t* src_buf,
-                                       uint32_t src_size,
+  CCodec_FlatePredictorScanlineDecoder(pdfium::span<const uint8_t> src_buf,
                                        int width,
                                        int height,
                                        int comps,
@@ -658,8 +656,7 @@
 };
 
 CCodec_FlatePredictorScanlineDecoder::CCodec_FlatePredictorScanlineDecoder(
-    const uint8_t* src_buf,
-    uint32_t src_size,
+    pdfium::span<const uint8_t> src_span,
     int width,
     int height,
     int comps,
@@ -668,7 +665,7 @@
     int Colors,
     int BitsPerComponent,
     int Columns)
-    : CCodec_FlateScanlineDecoder(src_buf, src_size, width, height, comps, bpc),
+    : CCodec_FlateScanlineDecoder(src_span, width, height, comps, bpc),
       m_Predictor(predictor) {
   ASSERT(m_Predictor != PredictorType::kNone);
   if (BitsPerComponent * Colors * Columns == 0) {
@@ -765,8 +762,7 @@
 }  // namespace
 
 std::unique_ptr<CCodec_ScanlineDecoder> CCodec_FlateModule::CreateDecoder(
-    const uint8_t* src_buf,
-    uint32_t src_size,
+    pdfium::span<const uint8_t> src_span,
     int width,
     int height,
     int nComps,
@@ -777,11 +773,11 @@
     int Columns) {
   PredictorType predictor_type = GetPredictor(predictor);
   if (predictor_type == PredictorType::kNone) {
-    return pdfium::MakeUnique<CCodec_FlateScanlineDecoder>(
-        src_buf, src_size, width, height, nComps, bpc);
+    return pdfium::MakeUnique<CCodec_FlateScanlineDecoder>(src_span, width,
+                                                           height, nComps, bpc);
   }
   return pdfium::MakeUnique<CCodec_FlatePredictorScanlineDecoder>(
-      src_buf, src_size, width, height, nComps, bpc, predictor_type, Colors,
+      src_span, width, height, nComps, bpc, predictor_type, Colors,
       BitsPerComponent, Columns);
 }
 
diff --git a/core/fxcodec/codec/ccodec_flatemodule.h b/core/fxcodec/codec/ccodec_flatemodule.h
index c84be7c..dab34be 100644
--- a/core/fxcodec/codec/ccodec_flatemodule.h
+++ b/core/fxcodec/codec/ccodec_flatemodule.h
@@ -10,21 +10,23 @@
 #include <memory>
 
 #include "core/fxcrt/fx_system.h"
+#include "third_party/base/span.h"
 
 class CCodec_ScanlineDecoder;
 
 class CCodec_FlateModule {
  public:
-  std::unique_ptr<CCodec_ScanlineDecoder> CreateDecoder(const uint8_t* src_buf,
-                                                        uint32_t src_size,
-                                                        int width,
-                                                        int height,
-                                                        int nComps,
-                                                        int bpc,
-                                                        int predictor,
-                                                        int Colors,
-                                                        int BitsPerComponent,
-                                                        int Columns);
+  std::unique_ptr<CCodec_ScanlineDecoder> CreateDecoder(
+      pdfium::span<const uint8_t> src_buf,
+      int width,
+      int height,
+      int nComps,
+      int bpc,
+      int predictor,
+      int Colors,
+      int BitsPerComponent,
+      int Columns);
+
   uint32_t FlateOrLZWDecode(bool bLZW,
                             const uint8_t* src_buf,
                             uint32_t src_size,
diff --git a/core/fxcodec/codec/ccodec_jpxmodule.cpp b/core/fxcodec/codec/ccodec_jpxmodule.cpp
index ce6a130..28221be 100644
--- a/core/fxcodec/codec/ccodec_jpxmodule.cpp
+++ b/core/fxcodec/codec/ccodec_jpxmodule.cpp
@@ -644,13 +644,13 @@
 CCodec_JpxModule::~CCodec_JpxModule() {}
 
 std::unique_ptr<CJPX_Decoder> CCodec_JpxModule::CreateDecoder(
-    const uint8_t* src_buf,
-    uint32_t src_size,
+    pdfium::span<const uint8_t> src_span,
     CPDF_ColorSpace* cs) {
   auto decoder = pdfium::MakeUnique<CJPX_Decoder>(cs);
-  return decoder->Init(pdfium::make_span(src_buf, src_size))
-             ? std::move(decoder)
-             : nullptr;
+  if (!decoder->Init(src_span))
+    return nullptr;
+
+  return decoder;
 }
 
 void CCodec_JpxModule::GetImageInfo(CJPX_Decoder* pDecoder,
diff --git a/core/fxcodec/codec/ccodec_jpxmodule.h b/core/fxcodec/codec/ccodec_jpxmodule.h
index 2a94986..2269ef3 100644
--- a/core/fxcodec/codec/ccodec_jpxmodule.h
+++ b/core/fxcodec/codec/ccodec_jpxmodule.h
@@ -11,6 +11,7 @@
 #include <vector>
 
 #include "core/fxcrt/fx_system.h"
+#include "third_party/base/span.h"
 
 class CJPX_Decoder;
 class CPDF_ColorSpace;
@@ -20,9 +21,10 @@
   CCodec_JpxModule();
   ~CCodec_JpxModule();
 
-  std::unique_ptr<CJPX_Decoder> CreateDecoder(const uint8_t* src_buf,
-                                              uint32_t src_size,
-                                              CPDF_ColorSpace* cs);
+  std::unique_ptr<CJPX_Decoder> CreateDecoder(
+      pdfium::span<const uint8_t> src_span,
+      CPDF_ColorSpace* cs);
+
   void GetImageInfo(CJPX_Decoder* pDecoder,
                     uint32_t* width,
                     uint32_t* height,
diff --git a/testing/fuzzers/pdf_codec_fax_fuzzer.cc b/testing/fuzzers/pdf_codec_fax_fuzzer.cc
index f6cc1e7..1d0b0f8 100644
--- a/testing/fuzzers/pdf_codec_fax_fuzzer.cc
+++ b/testing/fuzzers/pdf_codec_fax_fuzzer.cc
@@ -30,7 +30,7 @@
 
   CCodec_FaxModule fax_module;
   std::unique_ptr<CCodec_ScanlineDecoder> decoder(
-      fax_module.CreateDecoder(data, size, width, height, K, EndOfLine,
+      fax_module.CreateDecoder({data, size}, width, height, K, EndOfLine,
                                ByteAlign, BlackIs1, Columns, Rows));
 
   if (decoder) {
diff --git a/testing/fuzzers/pdf_jpx_fuzzer.cc b/testing/fuzzers/pdf_jpx_fuzzer.cc
index b74957b..011b171 100644
--- a/testing/fuzzers/pdf_jpx_fuzzer.cc
+++ b/testing/fuzzers/pdf_jpx_fuzzer.cc
@@ -20,7 +20,7 @@
 
 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
   std::unique_ptr<CJPX_Decoder> decoder =
-      g_module.CreateDecoder(data, size, nullptr);
+      g_module.CreateDecoder({data, size}, nullptr);
   if (!decoder)
     return 0;