Convert FlateUncompress() to return DataVectorAndBytesConsumed instead

Convert FlateUncompress(), the last DataAndBytesConsumed user, to return
DataVectorAndBytesConsumed instead. Then remove DataAndBytesConsumed.

Bug: pdfium:1872
Change-Id: I68977817dc85c8bbaefc1107519c484e3ab71e36
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/119324
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Thomas Sepez <tsepez@google.com>
diff --git a/core/fxcodec/data_and_bytes_consumed.cpp b/core/fxcodec/data_and_bytes_consumed.cpp
index 11b91ce..0c449a4 100644
--- a/core/fxcodec/data_and_bytes_consumed.cpp
+++ b/core/fxcodec/data_and_bytes_consumed.cpp
@@ -8,20 +8,6 @@
 
 namespace fxcodec {
 
-DataAndBytesConsumed::DataAndBytesConsumed(
-    std::unique_ptr<uint8_t, FxFreeDeleter> data,
-    uint32_t size,
-    uint32_t bytes_consumed)
-    : data(std::move(data)), size(size), bytes_consumed(bytes_consumed) {}
-
-DataAndBytesConsumed::DataAndBytesConsumed(DataAndBytesConsumed&&) noexcept =
-    default;
-
-DataAndBytesConsumed& DataAndBytesConsumed::operator=(
-    DataAndBytesConsumed&&) noexcept = default;
-
-DataAndBytesConsumed::~DataAndBytesConsumed() = default;
-
 DataVectorAndBytesConsumed::DataVectorAndBytesConsumed(DataVector<uint8_t> data,
                                                        uint32_t bytes_consumed)
     : data(std::move(data)), bytes_consumed(bytes_consumed) {}
diff --git a/core/fxcodec/data_and_bytes_consumed.h b/core/fxcodec/data_and_bytes_consumed.h
index 7e5f295..74b477a 100644
--- a/core/fxcodec/data_and_bytes_consumed.h
+++ b/core/fxcodec/data_and_bytes_consumed.h
@@ -7,30 +7,10 @@
 
 #include <stdint.h>
 
-#include <memory>
-
 #include "core/fxcrt/data_vector.h"
-#include "core/fxcrt/fx_memory_wrappers.h"
 
 namespace fxcodec {
 
-// TODO(crbug.com/pdfium/1872): Replace with DataVectorAndBytesConsumed.
-struct DataAndBytesConsumed {
-  DataAndBytesConsumed(std::unique_ptr<uint8_t, FxFreeDeleter> data,
-                       uint32_t size,
-                       uint32_t bytes_consumed);
-  DataAndBytesConsumed(DataAndBytesConsumed&) = delete;
-  DataAndBytesConsumed& operator=(DataAndBytesConsumed&) = delete;
-  DataAndBytesConsumed(DataAndBytesConsumed&&) noexcept;
-  DataAndBytesConsumed& operator=(DataAndBytesConsumed&&) noexcept;
-  ~DataAndBytesConsumed();
-
-  std::unique_ptr<uint8_t, FxFreeDeleter> data;
-  uint32_t size;
-  // TODO(thestig): Consider replacing with std::optional<size_t>.
-  uint32_t bytes_consumed;
-};
-
 // TODO(crbug.com/pdfium/1872): Rename to DataAndBytesConsumed once the existing
 // struct of that name is no longer used.
 struct DataVectorAndBytesConsumed {
@@ -48,7 +28,6 @@
 
 }  // namespace fxcodec
 
-using DataAndBytesConsumed = fxcodec::DataAndBytesConsumed;
 using DataVectorAndBytesConsumed = fxcodec::DataVectorAndBytesConsumed;
 
 #endif  // CORE_FXCODEC_DATA_AND_BYTES_CONSUMED_H_
diff --git a/core/fxcodec/flate/flatemodule.cpp b/core/fxcodec/flate/flatemodule.cpp
index df8fe3e..e288a93 100644
--- a/core/fxcodec/flate/flatemodule.cpp
+++ b/core/fxcodec/flate/flatemodule.cpp
@@ -484,11 +484,11 @@
   return std::min(guess_size, kMaxInitialAllocSize);
 }
 
-DataAndBytesConsumed FlateUncompress(pdfium::span<const uint8_t> src_buf,
-                                     uint32_t orig_size) {
+DataVectorAndBytesConsumed FlateUncompress(pdfium::span<const uint8_t> src_buf,
+                                           uint32_t orig_size) {
   std::unique_ptr<z_stream, FlateDeleter> context(FlateInit());
   if (!context) {
-    return {nullptr, 0u, 0u};
+    return {DataVector<uint8_t>(), 0u};
   }
 
   FlateInput(context.get(), src_buf);
@@ -496,14 +496,12 @@
   const uint32_t buf_size =
       EstimateFlateUncompressBufferSize(orig_size, src_buf.size());
   uint32_t last_buf_size = buf_size;
-  std::unique_ptr<uint8_t, FxFreeDeleter> guess_buf(
-      FX_Alloc(uint8_t, buf_size));
-  std::vector<std::unique_ptr<uint8_t, FxFreeDeleter>> result_tmp_bufs;
+  DataVector<uint8_t> guess_buf(buf_size);
+  std::vector<DataVector<uint8_t>> result_tmp_bufs;
   {
-    std::unique_ptr<uint8_t, FxFreeDeleter> cur_buf = std::move(guess_buf);
+    DataVector<uint8_t> cur_buf = std::move(guess_buf);
     while (true) {
-      bool ret = FlateOutput(context.get(),
-                             pdfium::make_span(cur_buf.get(), buf_size));
+      bool ret = FlateOutput(context.get(), cur_buf);
       uint32_t avail_buf_size = FlateGetAvailOut(context.get());
       if (!ret || avail_buf_size != 0) {
         last_buf_size = buf_size - avail_buf_size;
@@ -511,7 +509,7 @@
         break;
       }
       result_tmp_bufs.push_back(std::move(cur_buf));
-      cur_buf.reset(FX_Alloc(uint8_t, buf_size));
+      cur_buf = DataVector<uint8_t>(buf_size);
     }
   }
 
@@ -523,24 +521,21 @@
       FlateGetPossiblyTruncatedTotalIn(context.get());
   if (result_tmp_bufs.size() == 1) {
     CHECK_LE(dest_size, buf_size);
-    return {std::move(result_tmp_bufs.front()), dest_size, bytes_consumed};
+    result_tmp_bufs.front().resize(dest_size);
+    return {std::move(result_tmp_bufs.front()), bytes_consumed};
   }
 
-  std::unique_ptr<uint8_t, FxFreeDeleter> result_buf(
-      FX_Alloc(uint8_t, dest_size));
-  auto result_span = pdfium::make_span(result_buf.get(), dest_size);
-  uint32_t remaining = dest_size;
+  DataVector<uint8_t> result_buf(dest_size);
+  auto result_span = pdfium::make_span(result_buf);
   for (size_t i = 0; i < result_tmp_bufs.size(); i++) {
-    std::unique_ptr<uint8_t, FxFreeDeleter> tmp_buf =
-        std::move(result_tmp_bufs[i]);
+    DataVector<uint8_t> tmp_buf = std::move(result_tmp_bufs[i]);
     const uint32_t tmp_buf_size =
         i + 1 < result_tmp_bufs.size() ? buf_size : last_buf_size;
-    uint32_t cp_size = std::min(tmp_buf_size, remaining);
-    fxcrt::spancpy(result_span, pdfium::make_span(tmp_buf.get(), cp_size));
-    result_span = result_span.subspan(cp_size);
-    remaining -= cp_size;
+    size_t cp_size = std::min<size_t>(tmp_buf_size, result_span.size());
+    result_span =
+        fxcrt::spancpy(result_span, pdfium::make_span(tmp_buf).first(cp_size));
   }
-  return {std::move(result_buf), dest_size, bytes_consumed};
+  return {std::move(result_buf), bytes_consumed};
 }
 
 enum class PredictorType : uint8_t { kNone, kFlate, kPng };
@@ -809,10 +804,9 @@
     dest_buf = decoder->TakeDestBuf();
     bytes_consumed = decoder->GetSrcSize();
   } else {
-    DataAndBytesConsumed result = FlateUncompress(src_span, estimated_size);
-    // TODO(crbug.com/pdfium/1872): Avoid copying.
-    dest_buf.resize(result.size);
-    FXSYS_memcpy(dest_buf.data(), result.data.get(), dest_buf.size());
+    DataVectorAndBytesConsumed result =
+        FlateUncompress(src_span, estimated_size);
+    dest_buf = std::move(result.data);
     bytes_consumed = result.bytes_consumed;
   }