Remove CBC_CommonByteArray and CBC_QRCoderBlockPair.

CBC_CommonByteArray is just a std::vector. CBC_QRCoderBlockPair is just
a struct with two vectors.

Change-Id: I9e5fdab18f07a1cff7ee486dfce619f9391c80dc
Reviewed-on: https://pdfium-review.googlesource.com/42454
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Wei Li <weili@chromium.org>
diff --git a/BUILD.gn b/BUILD.gn
index 6df4732..8a72c71 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -1727,8 +1727,6 @@
       "fxbarcode/common/BC_CommonBitArray.h",
       "fxbarcode/common/BC_CommonBitMatrix.cpp",
       "fxbarcode/common/BC_CommonBitMatrix.h",
-      "fxbarcode/common/BC_CommonByteArray.cpp",
-      "fxbarcode/common/BC_CommonByteArray.h",
       "fxbarcode/common/BC_CommonByteMatrix.cpp",
       "fxbarcode/common/BC_CommonByteMatrix.h",
       "fxbarcode/common/reedsolomon/BC_ReedSolomon.cpp",
@@ -1801,8 +1799,6 @@
       "fxbarcode/qrcode/BC_QRCoder.h",
       "fxbarcode/qrcode/BC_QRCoderBitVector.cpp",
       "fxbarcode/qrcode/BC_QRCoderBitVector.h",
-      "fxbarcode/qrcode/BC_QRCoderBlockPair.cpp",
-      "fxbarcode/qrcode/BC_QRCoderBlockPair.h",
       "fxbarcode/qrcode/BC_QRCoderECBlocks.cpp",
       "fxbarcode/qrcode/BC_QRCoderECBlocks.h",
       "fxbarcode/qrcode/BC_QRCoderECBlocksData.cpp",
diff --git a/fxbarcode/common/BC_CommonByteArray.cpp b/fxbarcode/common/BC_CommonByteArray.cpp
deleted file mode 100644
index bb4fbf8..0000000
--- a/fxbarcode/common/BC_CommonByteArray.cpp
+++ /dev/null
@@ -1,101 +0,0 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-// Original code is licensed as follows:
-/*
- * Copyright 2008 ZXing authors
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#include "fxbarcode/common/BC_CommonByteArray.h"
-
-#include <algorithm>
-
-#include "core/fxcrt/fx_memory.h"
-
-CBC_CommonByteArray::CBC_CommonByteArray() {
-  m_bytes = nullptr;
-  m_size = 0;
-  m_index = 0;
-}
-CBC_CommonByteArray::CBC_CommonByteArray(int32_t size) {
-  m_size = size;
-  m_bytes = FX_Alloc(uint8_t, size);
-  memset(m_bytes, 0, size);
-  m_index = 0;
-}
-CBC_CommonByteArray::CBC_CommonByteArray(uint8_t* byteArray, int32_t size) {
-  m_size = size;
-  m_bytes = FX_Alloc(uint8_t, size);
-  memcpy(m_bytes, byteArray, size);
-  m_index = size;
-}
-CBC_CommonByteArray::~CBC_CommonByteArray() {
-  FX_Free(m_bytes);
-}
-int32_t CBC_CommonByteArray::At(int32_t index) const {
-  return m_bytes[index] & 0xff;
-}
-void CBC_CommonByteArray::Set(int32_t index, int32_t value) {
-  m_bytes[index] = (uint8_t)value;
-}
-int32_t CBC_CommonByteArray::Size() const {
-  return m_size;
-}
-bool CBC_CommonByteArray::IsEmpty() const {
-  return m_size == 0;
-}
-void CBC_CommonByteArray::AppendByte(int32_t value) {
-  if (m_size == 0 || m_index >= m_size) {
-    int32_t newSize = std::max(32, m_size << 1);
-    Reserve(newSize);
-  }
-  m_bytes[m_index] = (uint8_t)value;
-  m_index++;
-}
-void CBC_CommonByteArray::Reserve(int32_t capacity) {
-  if (!m_bytes || m_size < capacity) {
-    uint8_t* newArray = FX_Alloc(uint8_t, capacity);
-    if (m_bytes) {
-      memcpy(newArray, m_bytes, m_size);
-      memset(newArray + m_size, 0, capacity - m_size);
-    } else {
-      memset(newArray, 0, capacity);
-    }
-    FX_Free(m_bytes);
-    m_bytes = newArray;
-    m_size = capacity;
-  }
-}
-void CBC_CommonByteArray::Set(const uint8_t* source,
-                              int32_t offset,
-                              int32_t count) {
-  FX_Free(m_bytes);
-  m_bytes = FX_Alloc(uint8_t, count);
-  m_size = count;
-  memcpy(m_bytes, source + offset, count);
-  m_index = count;
-}
-void CBC_CommonByteArray::Set(std::vector<uint8_t>* source,
-                              int32_t offset,
-                              int32_t count) {
-  FX_Free(m_bytes);
-  m_bytes = FX_Alloc(uint8_t, count);
-  m_size = count;
-  int32_t i;
-  for (i = 0; i < count; i++)
-    m_bytes[i] = (*source)[i + offset];
-  m_index = m_size;
-}
diff --git a/fxbarcode/common/BC_CommonByteArray.h b/fxbarcode/common/BC_CommonByteArray.h
deleted file mode 100644
index 285a567..0000000
--- a/fxbarcode/common/BC_CommonByteArray.h
+++ /dev/null
@@ -1,38 +0,0 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#ifndef FXBARCODE_COMMON_BC_COMMONBYTEARRAY_H_
-#define FXBARCODE_COMMON_BC_COMMONBYTEARRAY_H_
-
-#include <stdint.h>
-
-#include <vector>
-
-// TODO(weili): The usage of this class should be replaced by
-// std::vector<uint8_t>.
-class CBC_CommonByteArray {
- public:
-  CBC_CommonByteArray();
-  explicit CBC_CommonByteArray(int32_t size);
-  CBC_CommonByteArray(uint8_t* byteArray, int32_t size);
-  virtual ~CBC_CommonByteArray();
-
-  int32_t At(int32_t index) const;
-  int32_t Size() const;
-  bool IsEmpty() const;
-  void Set(int32_t index, int32_t value);
-  void AppendByte(int32_t value);
-  void Reserve(int32_t capacity);
-  void Set(const uint8_t* source, int32_t offset, int32_t count);
-  void Set(std::vector<uint8_t>* source, int32_t offset, int32_t count);
-
- private:
-  int32_t m_size;
-  int32_t m_index;
-  uint8_t* m_bytes;
-};
-
-#endif  // FXBARCODE_COMMON_BC_COMMONBYTEARRAY_H_
diff --git a/fxbarcode/common/reedsolomon/BC_ReedSolomon.cpp b/fxbarcode/common/reedsolomon/BC_ReedSolomon.cpp
index 2be74c7..8ce4ebf 100644
--- a/fxbarcode/common/reedsolomon/BC_ReedSolomon.cpp
+++ b/fxbarcode/common/reedsolomon/BC_ReedSolomon.cpp
@@ -30,15 +30,13 @@
 #include "third_party/base/ptr_util.h"
 
 CBC_ReedSolomonEncoder::CBC_ReedSolomonEncoder(CBC_ReedSolomonGF256* field)
-    : m_field(field) {}
-
-CBC_ReedSolomonEncoder::~CBC_ReedSolomonEncoder() {}
-
-void CBC_ReedSolomonEncoder::Init() {
+    : m_field(field) {
   m_cachedGenerators.push_back(
       pdfium::MakeUnique<CBC_ReedSolomonGF256Poly>(m_field.Get(), 1));
 }
 
+CBC_ReedSolomonEncoder::~CBC_ReedSolomonEncoder() {}
+
 CBC_ReedSolomonGF256Poly* CBC_ReedSolomonEncoder::BuildGenerator(
     size_t degree) {
   if (degree >= m_cachedGenerators.size()) {
diff --git a/fxbarcode/common/reedsolomon/BC_ReedSolomon.h b/fxbarcode/common/reedsolomon/BC_ReedSolomon.h
index e45c8a4..772ca3c 100644
--- a/fxbarcode/common/reedsolomon/BC_ReedSolomon.h
+++ b/fxbarcode/common/reedsolomon/BC_ReedSolomon.h
@@ -20,7 +20,6 @@
   explicit CBC_ReedSolomonEncoder(CBC_ReedSolomonGF256* field);
   ~CBC_ReedSolomonEncoder();
 
-  void Init();
   bool Encode(std::vector<int32_t>* toEncode, size_t ecBytes);
 
  private:
diff --git a/fxbarcode/qrcode/BC_QRCoderBlockPair.cpp b/fxbarcode/qrcode/BC_QRCoderBlockPair.cpp
deleted file mode 100644
index 9ac93e9..0000000
--- a/fxbarcode/qrcode/BC_QRCoderBlockPair.cpp
+++ /dev/null
@@ -1,47 +0,0 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-// Original code is licensed as follows:
-/*
- * Copyright 2008 ZXing authors
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "fxbarcode/qrcode/BC_QRCoderBlockPair.h"
-
-#include <utility>
-
-#include "fxbarcode/common/BC_CommonByteArray.h"
-
-CBC_QRCoderBlockPair::CBC_QRCoderBlockPair() {}
-
-CBC_QRCoderBlockPair::~CBC_QRCoderBlockPair() {}
-
-const CBC_CommonByteArray* CBC_QRCoderBlockPair::GetDataBytes() const {
-  return m_dataBytes.get();
-}
-
-const CBC_CommonByteArray* CBC_QRCoderBlockPair::GetErrorCorrectionBytes()
-    const {
-  return m_errorCorrectionBytes.get();
-}
-
-void CBC_QRCoderBlockPair::SetData(
-    std::unique_ptr<CBC_CommonByteArray> data,
-    std::unique_ptr<CBC_CommonByteArray> errorCorrection) {
-  m_dataBytes = std::move(data);
-  m_errorCorrectionBytes = std::move(errorCorrection);
-}
diff --git a/fxbarcode/qrcode/BC_QRCoderBlockPair.h b/fxbarcode/qrcode/BC_QRCoderBlockPair.h
deleted file mode 100644
index 9d571f3..0000000
--- a/fxbarcode/qrcode/BC_QRCoderBlockPair.h
+++ /dev/null
@@ -1,29 +0,0 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#ifndef FXBARCODE_QRCODE_BC_QRCODERBLOCKPAIR_H_
-#define FXBARCODE_QRCODE_BC_QRCODERBLOCKPAIR_H_
-
-#include <memory>
-
-class CBC_CommonByteArray;
-
-class CBC_QRCoderBlockPair {
- public:
-  CBC_QRCoderBlockPair();
-  ~CBC_QRCoderBlockPair();
-
-  const CBC_CommonByteArray* GetDataBytes() const;
-  const CBC_CommonByteArray* GetErrorCorrectionBytes() const;
-  void SetData(std::unique_ptr<CBC_CommonByteArray> data,
-               std::unique_ptr<CBC_CommonByteArray> errorCorrection);
-
- private:
-  std::unique_ptr<CBC_CommonByteArray> m_dataBytes;
-  std::unique_ptr<CBC_CommonByteArray> m_errorCorrectionBytes;
-};
-
-#endif  // FXBARCODE_QRCODE_BC_QRCODERBLOCKPAIR_H_
diff --git a/fxbarcode/qrcode/BC_QRCoderEncoder.cpp b/fxbarcode/qrcode/BC_QRCoderEncoder.cpp
index fe911f8..052ad49 100644
--- a/fxbarcode/qrcode/BC_QRCoderEncoder.cpp
+++ b/fxbarcode/qrcode/BC_QRCoderEncoder.cpp
@@ -28,13 +28,11 @@
 #include <vector>
 
 #include "fxbarcode/BC_UtilCodingConvert.h"
-#include "fxbarcode/common/BC_CommonByteArray.h"
 #include "fxbarcode/common/BC_CommonByteMatrix.h"
 #include "fxbarcode/common/reedsolomon/BC_ReedSolomon.h"
 #include "fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h"
 #include "fxbarcode/qrcode/BC_QRCoder.h"
 #include "fxbarcode/qrcode/BC_QRCoderBitVector.h"
-#include "fxbarcode/qrcode/BC_QRCoderBlockPair.h"
 #include "fxbarcode/qrcode/BC_QRCoderECBlocks.h"
 #include "fxbarcode/qrcode/BC_QRCoderMaskUtil.h"
 #include "fxbarcode/qrcode/BC_QRCoderMatrixUtil.h"
@@ -46,6 +44,11 @@
 
 namespace {
 
+struct QRCoderBlockPair {
+  std::vector<uint8_t> data;
+  std::vector<uint8_t> ecc;
+};
+
 // This is a mapping for an ASCII table, starting at an index of 32.
 const int8_t g_alphaNumericTable[] = {
     36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43,  // 32-47
@@ -222,20 +225,20 @@
   return false;
 }
 
-std::unique_ptr<CBC_CommonByteArray> GenerateECBytes(
-    CBC_CommonByteArray* dataBytes,
-    int32_t numEcBytesInBlock) {
-  int32_t numDataBytes = dataBytes->Size();
-  std::vector<int32_t> toEncode(numDataBytes + numEcBytesInBlock);
-  for (int32_t i = 0; i < numDataBytes; ++i)
-    toEncode[i] = dataBytes->At(i);
-  CBC_ReedSolomonEncoder encode(CBC_ReedSolomonGF256::QRCodeField);
-  encode.Init();
-  if (!encode.Encode(&toEncode, numEcBytesInBlock))
-    return nullptr;
-  auto ecBytes = pdfium::MakeUnique<CBC_CommonByteArray>(numEcBytesInBlock);
-  for (int32_t i = 0; i < numEcBytesInBlock; ++i)
-    ecBytes->Set(i, toEncode[numDataBytes + i]);
+std::vector<uint8_t> GenerateECBytes(const std::vector<uint8_t>& dataBytes,
+                                     size_t numEcBytesInBlock) {
+  // If |numEcBytesInBlock| is 0, the encoder will fail anyway.
+  ASSERT(numEcBytesInBlock > 0);
+  std::vector<int32_t> toEncode(dataBytes.size() + numEcBytesInBlock);
+  std::copy(dataBytes.begin(), dataBytes.end(), toEncode.begin());
+
+  std::vector<uint8_t> ecBytes;
+  CBC_ReedSolomonEncoder encoder(CBC_ReedSolomonGF256::QRCodeField);
+  if (encoder.Encode(&toEncode, numEcBytesInBlock)) {
+    ecBytes = std::vector<uint8_t>(toEncode.begin() + dataBytes.size(),
+                                   toEncode.end());
+    ASSERT(ecBytes.size() == static_cast<size_t>(numEcBytesInBlock));
+  }
   return ecBytes;
 }
 
@@ -272,8 +275,8 @@
                                             int32_t numDataBytes,
                                             int32_t numRSBlocks,
                                             int32_t blockID,
-                                            int32_t& numDataBytesInBlock,
-                                            int32_t& numECBytesInBlock) {
+                                            int32_t* numDataBytesInBlock,
+                                            int32_t* numECBytesInBlock) {
   if (blockID >= numRSBlocks)
     return;
 
@@ -286,11 +289,11 @@
   int32_t numEcBytesInGroup1 = numTotalBytesInGroup1 - numDataBytesInGroup1;
   int32_t numEcBytesInGroup2 = numTotalBytesInGroup2 - numDataBytesInGroup2;
   if (blockID < numRsBlocksInGroup1) {
-    numDataBytesInBlock = numDataBytesInGroup1;
-    numECBytesInBlock = numEcBytesInGroup1;
+    *numDataBytesInBlock = numDataBytesInGroup1;
+    *numECBytesInBlock = numEcBytesInGroup1;
   } else {
-    numDataBytesInBlock = numDataBytesInGroup2;
-    numECBytesInBlock = numEcBytesInGroup2;
+    *numDataBytesInBlock = numDataBytesInGroup2;
+    *numECBytesInBlock = numEcBytesInGroup2;
   }
 }
 
@@ -411,42 +414,47 @@
     return false;
 
   int32_t dataBytesOffset = 0;
-  int32_t maxNumDataBytes = 0;
-  int32_t maxNumEcBytes = 0;
-  std::vector<CBC_QRCoderBlockPair> blocks(numRSBlocks);
+  size_t maxNumDataBytes = 0;
+  size_t maxNumEcBytes = 0;
+  std::vector<QRCoderBlockPair> blocks(numRSBlocks);
   for (int32_t i = 0; i < numRSBlocks; i++) {
     int32_t numDataBytesInBlock;
-    int32_t numEcBytesInBlosk;
+    int32_t numEcBytesInBlock;
     GetNumDataBytesAndNumECBytesForBlockID(numTotalBytes, numDataBytes,
-                                           numRSBlocks, i, numDataBytesInBlock,
-                                           numEcBytesInBlosk);
-    auto dataBytes = pdfium::MakeUnique<CBC_CommonByteArray>();
-    dataBytes->Set(bits->GetArray(), dataBytesOffset, numDataBytesInBlock);
-    std::unique_ptr<CBC_CommonByteArray> ecBytes =
-        GenerateECBytes(dataBytes.get(), numEcBytesInBlosk);
-    if (!ecBytes)
+                                           numRSBlocks, i, &numDataBytesInBlock,
+                                           &numEcBytesInBlock);
+    if (numDataBytesInBlock < 0 || numEcBytesInBlock <= 0)
       return false;
 
-    maxNumDataBytes = std::max(maxNumDataBytes, dataBytes->Size());
-    maxNumEcBytes = std::max(maxNumEcBytes, ecBytes->Size());
-    blocks[i].SetData(std::move(dataBytes), std::move(ecBytes));
+    std::vector<uint8_t> dataBytes(numDataBytesInBlock);
+    memcpy(dataBytes.data(), bits->GetArray() + dataBytesOffset,
+           numDataBytesInBlock);
+    std::vector<uint8_t> ecBytes =
+        GenerateECBytes(dataBytes, numEcBytesInBlock);
+    if (ecBytes.empty())
+      return false;
+
+    maxNumDataBytes = std::max(maxNumDataBytes, dataBytes.size());
+    maxNumEcBytes = std::max(maxNumEcBytes, ecBytes.size());
+    blocks[i].data = std::move(dataBytes);
+    blocks[i].ecc = std::move(ecBytes);
     dataBytesOffset += numDataBytesInBlock;
   }
   if (numDataBytes != dataBytesOffset)
     return false;
 
-  for (int32_t x = 0; x < maxNumDataBytes; x++) {
+  for (size_t x = 0; x < maxNumDataBytes; x++) {
     for (size_t j = 0; j < blocks.size(); j++) {
-      const CBC_CommonByteArray* dataBytes = blocks[j].GetDataBytes();
-      if (x < dataBytes->Size())
-        result->AppendBits(dataBytes->At(x), 8);
+      const std::vector<uint8_t>& dataBytes = blocks[j].data;
+      if (x < dataBytes.size())
+        result->AppendBits(dataBytes[x], 8);
     }
   }
-  for (int32_t y = 0; y < maxNumEcBytes; y++) {
+  for (size_t y = 0; y < maxNumEcBytes; y++) {
     for (size_t l = 0; l < blocks.size(); l++) {
-      const CBC_CommonByteArray* ecBytes = blocks[l].GetErrorCorrectionBytes();
-      if (y < ecBytes->Size())
-        result->AppendBits(ecBytes->At(y), 8);
+      const std::vector<uint8_t>& ecBytes = blocks[l].ecc;
+      if (y < ecBytes.size())
+        result->AppendBits(ecBytes[y], 8);
     }
   }
   return static_cast<size_t>(numTotalBytes) == result->sizeInBytes();