Create function for getting raw stream data from CPDF_Stream*

Currently a part of the function FPDFImageObj_GetImageDataRaw() takes a
CPDF_Stream* and extracts its raw data into a buffer. Moving this portion
of the function to a new helper function will allow other functions to
reuse this generally useful logic and reduce the complexity of
FPDFImageObj_GetImageDataRaw().

Bug: pdfium:1333
Change-Id: I28e825eaa262d728d50c7850e6d4d7b8b5da57ae
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/56451
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/fpdfsdk/cpdfsdk_helpers.cpp b/fpdfsdk/cpdfsdk_helpers.cpp
index 28609e7..b49f816 100644
--- a/fpdfsdk/cpdfsdk_helpers.cpp
+++ b/fpdfsdk/cpdfsdk_helpers.cpp
@@ -47,6 +47,26 @@
   return true;
 }
 
+unsigned long GetStreamMaybeCopyAndReturnLengthImpl(const CPDF_Stream* stream,
+                                                    void* buffer,
+                                                    unsigned long buflen,
+                                                    bool decode) {
+  ASSERT(stream);
+  auto stream_acc = pdfium::MakeRetain<CPDF_StreamAcc>(stream);
+
+  if (decode)
+    stream_acc->LoadAllDataFiltered();
+  else
+    stream_acc->LoadAllDataRaw();
+
+  const auto stream_data_size = stream_acc->GetSize();
+  if (!buffer || buflen < stream_data_size)
+    return stream_data_size;
+
+  memcpy(buffer, stream_acc->GetData(), stream_data_size);
+  return stream_data_size;
+}
+
 #ifdef PDF_ENABLE_XFA
 class FPDF_FileHandlerContext final : public IFX_SeekableStream {
  public:
@@ -262,18 +282,18 @@
   return len;
 }
 
+unsigned long GetRawStreamMaybeCopyAndReturnLength(const CPDF_Stream* stream,
+                                                   void* buffer,
+                                                   unsigned long buflen) {
+  return GetStreamMaybeCopyAndReturnLengthImpl(stream, buffer, buflen,
+                                               /*decode=*/false);
+}
+
 unsigned long DecodeStreamMaybeCopyAndReturnLength(const CPDF_Stream* stream,
                                                    void* buffer,
                                                    unsigned long buflen) {
-  ASSERT(stream);
-  auto stream_acc = pdfium::MakeRetain<CPDF_StreamAcc>(stream);
-  stream_acc->LoadAllDataFiltered();
-  const auto stream_data_size = stream_acc->GetSize();
-  if (!buffer || buflen < stream_data_size)
-    return stream_data_size;
-
-  memcpy(buffer, stream_acc->GetData(), stream_data_size);
-  return stream_data_size;
+  return GetStreamMaybeCopyAndReturnLengthImpl(stream, buffer, buflen,
+                                               /*decode=*/true);
 }
 
 void FSDK_SetSandBoxPolicy(FPDF_DWORD policy, FPDF_BOOL enable) {
diff --git a/fpdfsdk/cpdfsdk_helpers.h b/fpdfsdk/cpdfsdk_helpers.h
index bac2dff..97cebfd 100644
--- a/fpdfsdk/cpdfsdk_helpers.h
+++ b/fpdfsdk/cpdfsdk_helpers.h
@@ -244,6 +244,19 @@
 unsigned long Utf16EncodeMaybeCopyAndReturnLength(const WideString& text,
                                                   void* buffer,
                                                   unsigned long buflen);
+
+// Returns the length of the raw stream data from |stream|. The raw data is the
+// stream's data as stored in the PDF without applying any filters. If |buffer|
+// is non-nullptr and |buflen| is large enough to contain the raw data, then
+// the raw data is copied into |buffer|.
+unsigned long GetRawStreamMaybeCopyAndReturnLength(const CPDF_Stream* stream,
+                                                   void* buffer,
+                                                   unsigned long buflen);
+
+// Return the length of the decoded stream data of |stream|. The decoded data is
+// the uncompressed stream data, i.e. the raw stream data after having all
+// filters applied. If |buffer| is non-nullptr and |buflen| is large enough to
+// contain the decoded data, then the decoded data is copied into |buffer|.
 unsigned long DecodeStreamMaybeCopyAndReturnLength(const CPDF_Stream* stream,
                                                    void* buffer,
                                                    unsigned long buflen);
diff --git a/fpdfsdk/fpdf_editimg.cpp b/fpdfsdk/fpdf_editimg.cpp
index fccd9cb..6f4e614 100644
--- a/fpdfsdk/fpdf_editimg.cpp
+++ b/fpdfsdk/fpdf_editimg.cpp
@@ -244,15 +244,7 @@
   if (!pImgStream)
     return 0;
 
-  auto streamAcc = pdfium::MakeRetain<CPDF_StreamAcc>(pImgStream);
-  streamAcc->LoadAllDataRaw();
-
-  const uint32_t len = streamAcc->GetSize();
-  if (!buffer || buflen < len)
-    return len;
-
-  memcpy(buffer, streamAcc->GetData(), len);
-  return len;
+  return GetRawStreamMaybeCopyAndReturnLength(pImgStream, buffer, buflen);
 }
 
 FPDF_EXPORT int FPDF_CALLCONV