Remove out-parameters from FlateOrLZWDecode()

Return DataAndBytesConsumed instead.

Change-Id: Ica33715610319bfda8e061c0d260b927dbbb4cb8
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/119171
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/fpdfapi/page/cpdf_streamparser.cpp b/core/fpdfapi/page/cpdf_streamparser.cpp
index 34d5173..1086371 100644
--- a/core/fpdfapi/page/cpdf_streamparser.cpp
+++ b/core/fpdfapi/page/cpdf_streamparser.cpp
@@ -29,6 +29,7 @@
 #include "core/fpdfapi/parser/cpdf_string.h"
 #include "core/fpdfapi/parser/fpdf_parser_decode.h"
 #include "core/fpdfapi/parser/fpdf_parser_utility.h"
+#include "core/fxcodec/data_and_bytes_consumed.h"
 #include "core/fxcodec/jpeg/jpegmodule.h"
 #include "core/fxcodec/scanlinedecoder.h"
 #include "core/fxcrt/check.h"
@@ -91,16 +92,19 @@
   DCHECK(decoder != "LZW");
   DCHECK(decoder != "RL");
 
-  std::unique_ptr<uint8_t, FxFreeDeleter> ignored_result;
-  uint32_t ignored_size;
   if (decoder == "FlateDecode") {
-    return FlateOrLZWDecode(false, src_span, pParam.Get(), orig_size,
-                            &ignored_result, &ignored_size);
+    return FlateOrLZWDecode(/*use_lzw=*/false, src_span, pParam.Get(),
+                            /*estimated_size=*/orig_size)
+        .bytes_consumed;
   }
   if (decoder == "LZWDecode") {
-    return FlateOrLZWDecode(true, src_span, pParam.Get(), 0, &ignored_result,
-                            &ignored_size);
+    return FlateOrLZWDecode(
+               /*use_lzw=*/true, src_span, pParam.Get(),
+               /*estimated_size=*/0)
+        .bytes_consumed;
   }
+  std::unique_ptr<uint8_t, FxFreeDeleter> ignored_result;
+  uint32_t ignored_size;
   if (decoder == "DCTDecode") {
     std::unique_ptr<ScanlineDecoder> pDecoder = JpegModule::CreateDecoder(
         src_span, width, height, 0,
diff --git a/core/fpdfapi/parser/fpdf_parser_decode.cpp b/core/fpdfapi/parser/fpdf_parser_decode.cpp
index 0a7f338..c6dc9b2 100644
--- a/core/fpdfapi/parser/fpdf_parser_decode.cpp
+++ b/core/fpdfapi/parser/fpdf_parser_decode.cpp
@@ -344,12 +344,10 @@
                                     Columns);
 }
 
-uint32_t FlateOrLZWDecode(bool bLZW,
-                          pdfium::span<const uint8_t> src_span,
-                          const CPDF_Dictionary* pParams,
-                          uint32_t estimated_size,
-                          std::unique_ptr<uint8_t, FxFreeDeleter>* dest_buf,
-                          uint32_t* dest_size) {
+DataAndBytesConsumed FlateOrLZWDecode(bool use_lzw,
+                                      pdfium::span<const uint8_t> src_span,
+                                      const CPDF_Dictionary* pParams,
+                                      uint32_t estimated_size) {
   int predictor = 0;
   int Colors = 0;
   int BitsPerComponent = 0;
@@ -362,14 +360,11 @@
     BitsPerComponent = pParams->GetIntegerFor("BitsPerComponent", 8);
     Columns = pParams->GetIntegerFor("Columns", 1);
     if (!CheckFlateDecodeParams(Colors, BitsPerComponent, Columns))
-      return FX_INVALID_OFFSET;
+      return {nullptr, 0u, FX_INVALID_OFFSET};
   }
-  DataAndBytesConsumed result = FlateModule::FlateOrLZWDecode(
-      bLZW, src_span, bEarlyChange, predictor, Colors, BitsPerComponent,
-      Columns, estimated_size);
-  *dest_buf = std::move(result.data);
-  *dest_size = result.size;
-  return result.bytes_consumed;
+  return FlateModule::FlateOrLZWDecode(use_lzw, src_span, bEarlyChange,
+                                       predictor, Colors, BitsPerComponent,
+                                       Columns, estimated_size);
 }
 
 std::optional<DecoderArray> GetDecoderArray(
@@ -424,7 +419,7 @@
         ToDictionary(decoder_array[i].second);
     std::unique_ptr<uint8_t, FxFreeDeleter> new_buf;
     uint32_t new_size = 0xFFFFFFFF;
-    uint32_t offset = FX_INVALID_OFFSET;
+    uint32_t bytes_consumed = FX_INVALID_OFFSET;
     if (decoder == "Crypt")
       continue;
     if (decoder == "FlateDecode" || decoder == "Fl") {
@@ -435,15 +430,21 @@
         *pImageParams = std::move(pParam);
         return true;
       }
-      offset = FlateOrLZWDecode(false, last_span, pParam, estimated_size,
-                                &new_buf, &new_size);
+      DataAndBytesConsumed decode_result = FlateOrLZWDecode(
+          /*use_lzw=*/false, last_span, pParam, estimated_size);
+      new_buf = std::move(decode_result.data);
+      new_size = decode_result.size;
+      bytes_consumed = decode_result.bytes_consumed;
     } else if (decoder == "LZWDecode" || decoder == "LZW") {
-      offset = FlateOrLZWDecode(true, last_span, pParam, estimated_size,
-                                &new_buf, &new_size);
+      DataAndBytesConsumed decode_result =
+          FlateOrLZWDecode(/*use_lzw=*/true, last_span, pParam, estimated_size);
+      new_buf = std::move(decode_result.data);
+      new_size = decode_result.size;
+      bytes_consumed = decode_result.bytes_consumed;
     } else if (decoder == "ASCII85Decode" || decoder == "A85") {
-      offset = A85Decode(last_span, &new_buf, &new_size);
+      bytes_consumed = A85Decode(last_span, &new_buf, &new_size);
     } else if (decoder == "ASCIIHexDecode" || decoder == "AHx") {
-      offset = HexDecode(last_span, &new_buf, &new_size);
+      bytes_consumed = HexDecode(last_span, &new_buf, &new_size);
     } else if (decoder == "RunLengthDecode" || decoder == "RL") {
       if (bImageAcc && i == nSize - 1) {
         *ImageEncoding = "RunLengthDecode";
@@ -452,7 +453,7 @@
         *pImageParams = std::move(pParam);
         return true;
       }
-      offset = RunLengthDecode(last_span, &new_buf, &new_size);
+      bytes_consumed = RunLengthDecode(last_span, &new_buf, &new_size);
     } else {
       // If we get here, assume it's an image decoder.
       if (decoder == "DCT")
@@ -465,8 +466,9 @@
       *dest_size = last_span.size();
       return true;
     }
-    if (offset == FX_INVALID_OFFSET)
+    if (bytes_consumed == FX_INVALID_OFFSET) {
       return false;
+    }
 
     // SAFETY: relies on out params of FlateOrLZWDecode().
     last_span = UNSAFE_BUFFERS(pdfium::make_span(new_buf.get(), new_size));
diff --git a/core/fpdfapi/parser/fpdf_parser_decode.h b/core/fpdfapi/parser/fpdf_parser_decode.h
index 7516549..d53d0b2 100644
--- a/core/fpdfapi/parser/fpdf_parser_decode.h
+++ b/core/fpdfapi/parser/fpdf_parser_decode.h
@@ -15,6 +15,7 @@
 #include <utility>
 #include <vector>
 
+#include "core/fxcodec/data_and_bytes_consumed.h"
 #include "core/fxcrt/data_vector.h"
 #include "core/fxcrt/fx_memory_wrappers.h"
 #include "core/fxcrt/fx_string.h"
@@ -65,12 +66,11 @@
                    std::unique_ptr<uint8_t, FxFreeDeleter>* dest_buf,
                    uint32_t* dest_size);
 
-uint32_t FlateOrLZWDecode(bool bLZW,
-                          pdfium::span<const uint8_t> src_span,
-                          const CPDF_Dictionary* pParams,
-                          uint32_t estimated_size,
-                          std::unique_ptr<uint8_t, FxFreeDeleter>* dest_buf,
-                          uint32_t* dest_size);
+fxcodec::DataAndBytesConsumed FlateOrLZWDecode(
+    bool use_lzw,
+    pdfium::span<const uint8_t> src_span,
+    const CPDF_Dictionary* pParams,
+    uint32_t estimated_size);
 
 // Returns std::nullopt if the filter in |pDict| is the wrong type or an
 // invalid decoder pipeline.