Reland "Use DataVector in CFX_MemoryStream."

This is a reland of commit a015667fdd1157552c9b652d959322d89d5cd179

Fix the incorrect offset in WriteBlockAtOffset().

Original change's description:
> Use DataVector in CFX_MemoryStream.
>
> Bug: pdfium:1872
> Change-Id: I67eef8a307fe6375154939b68bc3b25c51759fc1
> Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/97710
> Commit-Queue: Lei Zhang <thestig@chromium.org>
> Reviewed-by: Tom Sepez <tsepez@chromium.org>

Bug: pdfium:1872
Change-Id: Ic63bca340c307be1950003db17a57d72fe48cda2
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/98073
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fxcrt/cfx_memorystream.cpp b/core/fxcrt/cfx_memorystream.cpp
index 9c89bc7..5ca97ed 100644
--- a/core/fxcrt/cfx_memorystream.cpp
+++ b/core/fxcrt/cfx_memorystream.cpp
@@ -10,6 +10,7 @@
 #include <utility>
 
 #include "core/fxcrt/fx_safe_types.h"
+#include "core/fxcrt/span_util.h"
 
 CFX_MemoryStream::CFX_MemoryStream() = default;
 
@@ -72,7 +73,7 @@
     return false;
 
   size_t new_pos = safe_new_pos.ValueOrDie();
-  if (new_pos > m_nTotalSize) {
+  if (new_pos > m_data.size()) {
     static constexpr size_t kBlockSize = 64 * 1024;
     FX_SAFE_SIZE_T new_size = new_pos;
     new_size *= 2;
@@ -82,15 +83,14 @@
     if (!new_size.IsValid())
       return false;
 
-    m_nTotalSize = new_size.ValueOrDie();
-    if (m_data)
-      m_data.reset(FX_Realloc(uint8_t, m_data.release(), m_nTotalSize));
-    else
-      m_data.reset(FX_Alloc(uint8_t, m_nTotalSize));
+    m_data.resize(new_size.ValueOrDie());
   }
   m_nCurPos = new_pos;
 
-  memcpy(&m_data.get()[offset], buffer, size);
+  // Safe to cast `offset` because it was used to calculate `safe_new_pos`
+  // above, and `safe_new_pos` is valid.
+  fxcrt::spancpy(pdfium::make_span(m_data).subspan(static_cast<size_t>(offset)),
+                 pdfium::make_span(static_cast<const uint8_t*>(buffer), size));
   m_nCurSize = std::max(m_nCurSize, m_nCurPos);
 
   return true;
diff --git a/core/fxcrt/cfx_memorystream.h b/core/fxcrt/cfx_memorystream.h
index f72a211..a573a60 100644
--- a/core/fxcrt/cfx_memorystream.h
+++ b/core/fxcrt/cfx_memorystream.h
@@ -7,9 +7,7 @@
 #ifndef CORE_FXCRT_CFX_MEMORYSTREAM_H_
 #define CORE_FXCRT_CFX_MEMORYSTREAM_H_
 
-#include <memory>
-
-#include "core/fxcrt/fx_memory_wrappers.h"
+#include "core/fxcrt/data_vector.h"
 #include "core/fxcrt/fx_stream.h"
 #include "core/fxcrt/retain_ptr.h"
 
@@ -30,14 +28,13 @@
                           size_t size) override;
   bool Flush() override;
 
-  const uint8_t* GetBuffer() const { return m_data.get(); }
+  const uint8_t* GetBuffer() const { return m_data.data(); }
 
  private:
   CFX_MemoryStream();
   ~CFX_MemoryStream() override;
 
-  std::unique_ptr<uint8_t, FxFreeDeleter> m_data;
-  size_t m_nTotalSize = 0;
+  DataVector<uint8_t> m_data;
   size_t m_nCurSize = 0;
   size_t m_nCurPos = 0;
 };