Avoid reinterpret span when casting to bytes

Most reinterpret_span<> usage should be avoided, and there are better
functions for the single-byte conversions in span.h itself.

Change-Id: I5ed8311855756c4696c14fa5e8e4da1ed9938924
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/120630
Reviewed-by: Lei Zhang <thestig@chromium.org>
Reviewed-by: Thomas Sepez <tsepez@google.com>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/edit/cpdf_stringarchivestream.cpp b/core/fpdfapi/edit/cpdf_stringarchivestream.cpp
index 87978fd..10ee993 100644
--- a/core/fpdfapi/edit/cpdf_stringarchivestream.cpp
+++ b/core/fpdfapi/edit/cpdf_stringarchivestream.cpp
@@ -7,7 +7,6 @@
 #include <sstream>
 
 #include "core/fxcrt/notreached.h"
-#include "core/fxcrt/span_util.h"
 
 CPDF_StringArchiveStream::CPDF_StringArchiveStream(fxcrt::ostringstream* stream)
     : stream_(stream) {}
@@ -19,7 +18,7 @@
 }
 
 bool CPDF_StringArchiveStream::WriteBlock(pdfium::span<const uint8_t> buffer) {
-  auto chars = fxcrt::reinterpret_span<const char>(buffer);
+  auto chars = pdfium::as_chars(buffer);
   stream_->write(chars.data(), chars.size());
   return true;
 }
diff --git a/core/fpdfapi/parser/cpdf_security_handler.cpp b/core/fpdfapi/parser/cpdf_security_handler.cpp
index c956be0..9c255f0 100644
--- a/core/fpdfapi/parser/cpdf_security_handler.cpp
+++ b/core/fpdfapi/parser/cpdf_security_handler.cpp
@@ -677,8 +677,10 @@
 
   // In ISO 32000 Supplement for ExtensionLevel 3, Algorithm 3.10 says bytes 12
   // to 15 should be random data.
-  auto random_span = pdfium::make_span(buf).subspan(12, 4);
-  FX_Random_GenerateMT(fxcrt::reinterpret_span<uint32_t>(random_span));
+  uint32_t random_value;
+  FX_Random_GenerateMT(pdfium::span_from_ref(random_value));
+  fxcrt::spancpy(pdfium::make_span(buf).subspan(12, 4),
+                 pdfium::byte_span_from_ref(random_value));
 
   CRYPT_aes_context aes = {};
   CRYPT_AESSetKey(&aes, m_EncryptKey.data(), m_EncryptKey.size());
diff --git a/core/fpdfapi/parser/fpdf_parser_utility.cpp b/core/fpdfapi/parser/fpdf_parser_utility.cpp
index db94e36..f82bfe4 100644
--- a/core/fpdfapi/parser/fpdf_parser_utility.cpp
+++ b/core/fpdfapi/parser/fpdf_parser_utility.cpp
@@ -21,7 +21,6 @@
 #include "core/fxcrt/check.h"
 #include "core/fxcrt/fx_extension.h"
 #include "core/fxcrt/fx_stream.h"
-#include "core/fxcrt/span_util.h"
 
 // Indexed by 8-bit character code, contains either:
 //   'W' - for whitespace: NUL, TAB, CR, LF, FF, SPACE, 0x80, 0xff
@@ -244,7 +243,7 @@
       buf << p->GetDict().Get() << "stream\r\n";
       auto pAcc = pdfium::MakeRetain<CPDF_StreamAcc>(std::move(p));
       pAcc->LoadAllDataRaw();
-      auto span = fxcrt::reinterpret_span<const char>(pAcc->GetSpan());
+      auto span = pdfium::as_chars(pAcc->GetSpan());
       buf.write(span.data(), span.size());
       buf << "\r\nendstream";
       break;
diff --git a/core/fxcrt/fx_stream.cpp b/core/fxcrt/fx_stream.cpp
index 55130bf..05e2b51 100644
--- a/core/fxcrt/fx_stream.cpp
+++ b/core/fxcrt/fx_stream.cpp
@@ -62,8 +62,7 @@
 bool IFX_WriteStream::WriteFilesize(FX_FILESIZE size) {
   char buf[20] = {};
   FXSYS_i64toa(size, buf, 10);
-  auto buf_span = fxcrt::reinterpret_span<uint8_t>(pdfium::make_span(buf));
-  return WriteBlock(buf_span.first(strlen(buf)));
+  return WriteBlock(pdfium::as_writable_byte_span(buf).first(strlen(buf)));
 }
 
 // static
diff --git a/core/fxge/win32/cfx_psrenderer.cpp b/core/fxge/win32/cfx_psrenderer.cpp
index a9e43a4..dfe0a79 100644
--- a/core/fxge/win32/cfx_psrenderer.cpp
+++ b/core/fxge/win32/cfx_psrenderer.cpp
@@ -913,7 +913,7 @@
   DataVector<uint8_t> encoded_data = m_pEncoderIface->pA85EncodeFunc(data);
   pdfium::span<const uint8_t> result =
       encoded_data.empty() ? data : encoded_data;
-  auto chars = fxcrt::reinterpret_span<const char>(result);
+  auto chars = pdfium::as_chars(result);
   m_Output.write(chars.data(), chars.size());
 }
 
diff --git a/testing/string_write_stream.cpp b/testing/string_write_stream.cpp
index feb11f9..b721328 100644
--- a/testing/string_write_stream.cpp
+++ b/testing/string_write_stream.cpp
@@ -13,7 +13,7 @@
 StringWriteStream::~StringWriteStream() = default;
 
 bool StringWriteStream::WriteBlock(pdfium::span<const uint8_t> buffer) {
-  auto chars = fxcrt::reinterpret_span<const char>(buffer);
+  auto chars = pdfium::as_chars(buffer);
   stream_.write(chars.data(), chars.size());
   return true;
 }