Change CBC_PDF417Writer::Encode() to return a vector.

Change-Id: Ide941846b0ab28173b3e1da3909246314c4e0dd8
Reviewed-on: https://pdfium-review.googlesource.com/c/46521
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/fxbarcode/cbc_pdf417i.cpp b/fxbarcode/cbc_pdf417i.cpp
index d2c7525..72a30d6 100644
--- a/fxbarcode/cbc_pdf417i.cpp
+++ b/fxbarcode/cbc_pdf417i.cpp
@@ -21,7 +21,7 @@
 
 #include "fxbarcode/cbc_pdf417i.h"
 
-#include <memory>
+#include <vector>
 
 #include "fxbarcode/pdf417/BC_PDF417Writer.h"
 #include "third_party/base/ptr_util.h"
@@ -35,11 +35,8 @@
   int32_t width;
   int32_t height;
   auto* pWriter = GetPDF417Writer();
-  std::unique_ptr<uint8_t, FxFreeDeleter> data(
-      pWriter->Encode(contents, &width, &height));
-  if (!data)
-    return false;
-  return pWriter->RenderResult(data.get(), width, height);
+  std::vector<uint8_t> data = pWriter->Encode(contents, &width, &height);
+  return !data.empty() && pWriter->RenderResult(data.data(), width, height);
 }
 
 bool CBC_PDF417I::RenderDevice(CFX_RenderDevice* device,
diff --git a/fxbarcode/pdf417/BC_PDF417Writer.cpp b/fxbarcode/pdf417/BC_PDF417Writer.cpp
index 423e8da..71509ff 100644
--- a/fxbarcode/pdf417/BC_PDF417Writer.cpp
+++ b/fxbarcode/pdf417/BC_PDF417Writer.cpp
@@ -29,6 +29,7 @@
 #include "fxbarcode/common/BC_CommonBitMatrix.h"
 #include "fxbarcode/pdf417/BC_PDF417.h"
 #include "fxbarcode/pdf417/BC_PDF417BarcodeMatrix.h"
+#include "third_party/base/stl_util.h"
 
 CBC_PDF417Writer::CBC_PDF417Writer() : CBC_TwoDimWriter(false) {}
 
@@ -42,9 +43,10 @@
   return true;
 }
 
-uint8_t* CBC_PDF417Writer::Encode(const WideStringView& contents,
-                                  int32_t* pOutWidth,
-                                  int32_t* pOutHeight) {
+std::vector<uint8_t> CBC_PDF417Writer::Encode(const WideStringView& contents,
+                                              int32_t* pOutWidth,
+                                              int32_t* pOutHeight) {
+  std::vector<uint8_t> results;
   CBC_PDF417 encoder;
   int32_t col = (m_Width / m_ModuleWidth - 69) / 17;
   int32_t row = m_Height / (m_ModuleWidth * 20);
@@ -55,7 +57,7 @@
   else if (row >= 3 && row <= 90)
     encoder.setDimensions(30, 1, row, row);
   if (!encoder.GenerateBarcodeLogic(contents, error_correction_level()))
-    return nullptr;
+    return results;
 
   CBC_BarcodeMatrix* barcodeMatrix = encoder.getBarcodeMatrix();
   std::vector<uint8_t> matrixData = barcodeMatrix->toBitArray();
@@ -66,11 +68,11 @@
     RotateArray(&matrixData, matrixHeight, matrixWidth);
     std::swap(matrixWidth, matrixHeight);
   }
-  uint8_t* result = FX_Alloc2D(uint8_t, matrixHeight, matrixWidth);
-  memcpy(result, matrixData.data(), matrixHeight * matrixWidth);
   *pOutWidth = matrixWidth;
   *pOutHeight = matrixHeight;
-  return result;
+  results = pdfium::Vector2D<uint8_t>(*pOutWidth, *pOutHeight);
+  memcpy(results.data(), matrixData.data(), *pOutWidth * *pOutHeight);
+  return results;
 }
 
 void CBC_PDF417Writer::RotateArray(std::vector<uint8_t>* bitarray,
diff --git a/fxbarcode/pdf417/BC_PDF417Writer.h b/fxbarcode/pdf417/BC_PDF417Writer.h
index 632d273..c32508b 100644
--- a/fxbarcode/pdf417/BC_PDF417Writer.h
+++ b/fxbarcode/pdf417/BC_PDF417Writer.h
@@ -18,9 +18,9 @@
   CBC_PDF417Writer();
   ~CBC_PDF417Writer() override;
 
-  uint8_t* Encode(const WideStringView& contents,
-                  int32_t* pOutWidth,
-                  int32_t* pOutHeight);
+  std::vector<uint8_t> Encode(const WideStringView& contents,
+                              int32_t* pOutWidth,
+                              int32_t* pOutHeight);
 
   // CBC_TwoDimWriter
   bool SetErrorCorrectionLevel(int32_t level) override;
diff --git a/fxbarcode/pdf417/BC_PDF417Writer_unittest.cpp b/fxbarcode/pdf417/BC_PDF417Writer_unittest.cpp
index dd089d9..1c09d78 100644
--- a/fxbarcode/pdf417/BC_PDF417Writer_unittest.cpp
+++ b/fxbarcode/pdf417/BC_PDF417Writer_unittest.cpp
@@ -4,7 +4,7 @@
 
 #include "fxbarcode/pdf417/BC_PDF417Writer.h"
 
-#include <memory>
+#include <vector>
 
 #include "testing/gtest/include/gtest/gtest.h"
 
@@ -413,13 +413,12 @@
         1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1,
         0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1,
         1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1};
-    std::unique_ptr<uint8_t, FxFreeDeleter> data(
-        writer.Encode(L"", &width, &height));
-    ASSERT_TRUE(data);
+    std::vector<uint8_t> data = writer.Encode(L"", &width, &height);
+    ASSERT_EQ(FX_ArraySize(kExpectedData), data.size());
     ASSERT_EQ(kExpectedWidth, width);
     ASSERT_EQ(kExpectedHeight, height);
     for (size_t i = 0; i < FX_ArraySize(kExpectedData); ++i)
-      EXPECT_EQ(kExpectedData[i], data.get()[i]) << i;
+      EXPECT_EQ(kExpectedData[i], data[i]) << i;
   }
   {
     static constexpr int kExpectedWidth = 579;
@@ -811,12 +810,11 @@
         1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0,
         0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1,
         1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1};
-    std::unique_ptr<uint8_t, FxFreeDeleter> data(
-        writer.Encode(L"hello world", &width, &height));
-    ASSERT_TRUE(data);
+    std::vector<uint8_t> data = writer.Encode(L"hello world", &width, &height);
+    ASSERT_EQ(FX_ArraySize(kExpectedData), data.size());
     ASSERT_EQ(kExpectedWidth, width);
     ASSERT_EQ(kExpectedHeight, height);
     for (size_t i = 0; i < FX_ArraySize(kExpectedData); ++i)
-      EXPECT_EQ(kExpectedData[i], data.get()[i]) << i;
+      EXPECT_EQ(kExpectedData[i], data[i]) << i;
   }
 }