Remove JBig2_Image::data()

Either spanify callers and remove UNSAFE_TODO(), or change callers to
use JBig2_Image::span().data() as an escape hatch. Inside JBig2_Image,
just call data_.Get() directly.

Change-Id: I27f62cf2acf37252606e8c6db6bed8bcb55041aa
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/140932
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fxcodec/jbig2/JBig2_BitStream.cpp b/core/fxcodec/jbig2/JBig2_BitStream.cpp
index 011e116..9b75066 100644
--- a/core/fxcodec/jbig2/JBig2_BitStream.cpp
+++ b/core/fxcodec/jbig2/JBig2_BitStream.cpp
@@ -177,8 +177,9 @@
   bit_idx_ = dwBitPos & 7;
 }
 
-const uint8_t* CJBig2_BitStream::getPointer() const {
-  return span_.subspan(byte_idx_).data();
+pdfium::span<const uint8_t> CJBig2_BitStream::getCurrentSpan(
+    uint32_t size) const {
+  return span_.subspan(byte_idx_, size);
 }
 
 uint32_t CJBig2_BitStream::getByteLeft() const {
diff --git a/core/fxcodec/jbig2/JBig2_BitStream.h b/core/fxcodec/jbig2/JBig2_BitStream.h
index dbbde52..f86395f 100644
--- a/core/fxcodec/jbig2/JBig2_BitStream.h
+++ b/core/fxcodec/jbig2/JBig2_BitStream.h
@@ -37,7 +37,7 @@
   uint32_t getBitPos() const;
   void setBitPos(uint32_t dwBitPos);
   pdfium::span<const uint8_t> getBufSpan() const { return span_; }
-  const uint8_t* getPointer() const;
+  pdfium::span<const uint8_t> getCurrentSpan(uint32_t size) const;
   uint32_t getByteLeft() const;
   uint64_t getKey() const { return key_; }
   bool IsInBounds() const;
diff --git a/core/fxcodec/jbig2/JBig2_GrdProc.cpp b/core/fxcodec/jbig2/JBig2_GrdProc.cpp
index a1d1575..9eb60df 100644
--- a/core/fxcodec/jbig2/JBig2_GrdProc.cpp
+++ b/core/fxcodec/jbig2/JBig2_GrdProc.cpp
@@ -532,8 +532,8 @@
   bitpos = FaxModule::FaxG4Decode(pStream->getBufSpan(), bitpos, GBW, GBH,
                                   image->stride(), image_span);
   pStream->setBitPos(bitpos);
-  for (uint32_t i = 0; i < image->stride() * GBH; ++i) {
-    UNSAFE_TODO(image->data()[i] = ~image->data()[i]);
+  for (uint8_t& elem : image->span()) {
+    elem = ~elem;
   }
 
   progressive_status_ = FXCODEC_STATUS::kDecodeFinished;
diff --git a/core/fxcodec/jbig2/JBig2_GrrdProc.cpp b/core/fxcodec/jbig2/JBig2_GrrdProc.cpp
index 7b245ad..ea1fb04 100644
--- a/core/fxcodec/jbig2/JBig2_GrrdProc.cpp
+++ b/core/fxcodec/jbig2/JBig2_GrrdProc.cpp
@@ -163,8 +163,8 @@
   }
 
   int LTP = 0;
-  uint8_t* pLine = GRREG->data();
-  uint8_t* pLineR = GRREFERENCE->data();
+  uint8_t* pLine = GRREG->span().data();
+  uint8_t* pLineR = GRREFERENCE->span().data();
   intptr_t nStride = GRREG->stride();
   intptr_t nStrideR = GRREFERENCE->stride();
   int32_t GRWR = GRREFERENCE->width();
@@ -431,8 +431,8 @@
   }
 
   int LTP = 0;
-  uint8_t* pLine = GRREG->data();
-  uint8_t* pLineR = GRREFERENCE->data();
+  uint8_t* pLine = GRREG->span().data();
+  uint8_t* pLineR = GRREFERENCE->span().data();
   intptr_t nStride = GRREG->stride();
   intptr_t nStrideR = GRREFERENCE->stride();
   int32_t GRWR = GRREFERENCE->width();
diff --git a/core/fxcodec/jbig2/JBig2_Image.cpp b/core/fxcodec/jbig2/JBig2_Image.cpp
index 0325bb1..95a8962 100644
--- a/core/fxcodec/jbig2/JBig2_Image.cpp
+++ b/core/fxcodec/jbig2/JBig2_Image.cpp
@@ -133,12 +133,14 @@
   // SAFETY: If `data_` is owned, then `this` must have allocate the right
   // amount. If `data_` is not owned, then safety requires correctness from the
   // caller that constructed `this`.
-  return UNSAFE_BUFFERS(pdfium::span(data(), Fx2DSizeOrDie(stride_, height_)));
+  return UNSAFE_BUFFERS(
+      pdfium::span(data_.Get(), Fx2DSizeOrDie(stride_, height_)));
 }
 
 pdfium::span<uint8_t> CJBig2_Image::span() {
   // SAFETY: Same as const-version of span() above.
-  return UNSAFE_BUFFERS(pdfium::span(data(), Fx2DSizeOrDie(stride_, height_)));
+  return UNSAFE_BUFFERS(
+      pdfium::span(data_.Get(), Fx2DSizeOrDie(stride_, height_)));
 }
 
 int CJBig2_Image::GetPixel(int32_t x, int32_t y) const {
diff --git a/core/fxcodec/jbig2/JBig2_Image.h b/core/fxcodec/jbig2/JBig2_Image.h
index 7e37a86..06345b3 100644
--- a/core/fxcodec/jbig2/JBig2_Image.h
+++ b/core/fxcodec/jbig2/JBig2_Image.h
@@ -46,7 +46,6 @@
   int32_t stride() const { return stride_; }
 
   bool has_data() const { return static_cast<bool>(data_); }
-  uint8_t* data() const { return data_.Get(); }
 
   pdfium::span<const uint8_t> span() const;
   pdfium::span<uint8_t> span();
diff --git a/core/fxcodec/jbig2/JBig2_SddProc.cpp b/core/fxcodec/jbig2/JBig2_SddProc.cpp
index 50b172c..08e77eb 100644
--- a/core/fxcodec/jbig2/JBig2_SddProc.cpp
+++ b/core/fxcodec/jbig2/JBig2_SddProc.cpp
@@ -20,8 +20,8 @@
 #include "core/fxcodec/jbig2/JBig2_HuffmanTable.h"
 #include "core/fxcodec/jbig2/JBig2_SymbolDict.h"
 #include "core/fxcodec/jbig2/JBig2_TrdProc.h"
-#include "core/fxcrt/fx_memcpy_wrappers.h"
 #include "core/fxcrt/fx_safe_types.h"
+#include "core/fxcrt/span_util.h"
 #include "core/fxcrt/stl_util.h"
 
 CJBig2_SDDProc::CJBig2_SDDProc() = default;
@@ -440,8 +440,7 @@
 
         BHC = std::make_unique<CJBig2_Image>(TOTWIDTH, HCHEIGHT);
         for (uint32_t i = 0; i < HCHEIGHT; ++i) {
-          UNSAFE_TODO(FXSYS_memcpy(BHC->data() + i * BHC->stride(),
-                                   pStream->getPointer(), stride));
+          fxcrt::spancpy(BHC->GetLine(i), pStream->getCurrentSpan(stride));
           pStream->addOffset(stride);
         }
       } else {