Remove FX_Alloc()/FX_Free() calls in fxbarcode.

Switch to std::vector. Use more size_t along the way.

Change-Id: I2eb24e0739f814d743c280f93973007a28b16a54
Reviewed-on: https://pdfium-review.googlesource.com/c/46032
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/fxbarcode/datamatrix/BC_ErrorCorrection.cpp b/fxbarcode/datamatrix/BC_ErrorCorrection.cpp
index 4036dca..7b22e49 100644
--- a/fxbarcode/datamatrix/BC_ErrorCorrection.cpp
+++ b/fxbarcode/datamatrix/BC_ErrorCorrection.cpp
@@ -139,40 +139,40 @@
     3,   6,   12,  24,  48,  96,  192, 173, 119, 238, 241, 207, 179, 75,  150,
     0};
 
-WideString CreateECCBlock(const WideString& codewords, int32_t numECWords) {
+WideString CreateECCBlock(const WideString& codewords, size_t numECWords) {
   ASSERT(numECWords > 0);
 
-  const int32_t len = codewords.GetLength();
+  const size_t len = codewords.GetLength();
   static const size_t kFactorTableNum = sizeof(FACTOR_SETS) / sizeof(int32_t);
   size_t table = 0;
   while (table < kFactorTableNum && FACTOR_SETS[table] != numECWords)
-    table++;
+    ++table;
 
   if (table >= kFactorTableNum)
     return WideString();
 
-  uint16_t* ecc = FX_Alloc(uint16_t, numECWords);
-  for (int32_t i = 0; i < 0 + len; i++) {
+  std::vector<uint16_t> ecc(numECWords);
+  for (size_t i = 0; i < len; ++i) {
     uint16_t m = ecc[numECWords - 1] ^ codewords[i];
-    for (int32_t k = numECWords - 1; k > 0; k--) {
-      if (m != 0 && FACTORS[table][k] != 0) {
-        ecc[k] = (uint16_t)(ecc[k - 1] ^
-                            ALOG[(LOG[m] + LOG[FACTORS[table][k]]) % 255]);
+    for (int32_t j = numECWords - 1; j > 0; --j) {
+      if (m != 0 && FACTORS[table][j] != 0) {
+        ecc[j] = static_cast<uint16_t>(
+            ecc[j - 1] ^ ALOG[(LOG[m] + LOG[FACTORS[table][j]]) % 255]);
       } else {
-        ecc[k] = ecc[k - 1];
+        ecc[j] = ecc[j - 1];
       }
     }
     if (m != 0 && FACTORS[table][0] != 0) {
-      ecc[0] = (uint16_t)ALOG[(LOG[m] + LOG[FACTORS[table][0]]) % 255];
+      ecc[0] =
+          static_cast<uint16_t>(ALOG[(LOG[m] + LOG[FACTORS[table][0]]) % 255]);
     } else {
       ecc[0] = 0;
     }
   }
   WideString strecc;
-  for (int32_t j = 0; j < numECWords; j++) {
-    strecc += (wchar_t)ecc[numECWords - j - 1];
-  }
-  FX_Free(ecc);
+  strecc.Reserve(numECWords);
+  for (size_t i = 0; i < numECWords; ++i)
+    strecc.InsertAtBack(static_cast<wchar_t>(ecc[numECWords - i - 1]));
 
   ASSERT(!strecc.IsEmpty());
   return strecc;
@@ -193,30 +193,28 @@
       return WideString();
     sb += ecc;
   } else {
-    std::vector<int32_t> dataSizes(blockCount);
-    std::vector<int32_t> errorSizes(blockCount);
-    std::vector<int32_t> startPos(blockCount);
-    for (size_t i = 0; i < blockCount; i++) {
-      dataSizes[i] = symbolInfo->getDataLengthForInterleavedBlock(i + 1);
-      errorSizes[i] = symbolInfo->getErrorLengthForInterleavedBlock(i + 1);
-      startPos[i] = 0;
-      if (i > 0) {
-        startPos[i] = startPos[i - 1] + dataSizes[i];
-      }
+    std::vector<size_t> dataSizes(blockCount);
+    std::vector<size_t> errorSizes(blockCount);
+    std::vector<size_t> startPos(blockCount);
+    for (size_t i = 0; i < blockCount; ++i) {
+      dataSizes[i] = symbolInfo->getDataLengthForInterleavedBlock();
+      errorSizes[i] = symbolInfo->getErrorLengthForInterleavedBlock();
+      startPos[i] = i > 0 ? startPos[i - 1] + dataSizes[i] : 0;
     }
-    for (size_t block = 0; block < blockCount; block++) {
+    for (size_t block = 0; block < blockCount; ++block) {
       WideString temp;
+      if (symbolInfo->dataCapacity() > block)
+        temp.Reserve((symbolInfo->dataCapacity() - block / blockCount) + 1);
       for (size_t d = block; d < symbolInfo->dataCapacity(); d += blockCount)
-        temp += static_cast<wchar_t>(codewords[d]);
+        temp.InsertAtBack(static_cast<wchar_t>(codewords[d]));
 
       WideString ecc = CreateECCBlock(temp, errorSizes[block]);
       if (ecc.IsEmpty())
         return WideString();
 
-      size_t pos = 0;
-      for (size_t i = block; i < errorSizes[block] * blockCount;
-           i += blockCount) {
-        sb.SetAt(symbolInfo->dataCapacity() + i, ecc[pos++]);
+      for (size_t pos = 0, i = block; i < errorSizes[block] * blockCount;
+           ++pos, i += blockCount) {
+        sb.SetAt(symbolInfo->dataCapacity() + i, ecc[pos]);
       }
     }
   }
diff --git a/fxbarcode/datamatrix/BC_SymbolInfo.cpp b/fxbarcode/datamatrix/BC_SymbolInfo.cpp
index c47a6f0..d80afc2 100644
--- a/fxbarcode/datamatrix/BC_SymbolInfo.cpp
+++ b/fxbarcode/datamatrix/BC_SymbolInfo.cpp
@@ -96,8 +96,8 @@
                                int32_t matrixWidth,
                                int32_t matrixHeight,
                                int32_t dataRegions,
-                               int32_t rsBlockData,
-                               int32_t rsBlockError)
+                               size_t rsBlockData,
+                               size_t rsBlockError)
     : m_rectangular(matrixWidth != matrixHeight),
       m_dataCapacity(dataCapacity),
       m_errorCodewords(errorCodewords),
@@ -182,10 +182,10 @@
   return m_dataCapacity / m_rsBlockData;
 }
 
-int32_t CBC_SymbolInfo::getDataLengthForInterleavedBlock(int32_t index) const {
+size_t CBC_SymbolInfo::getDataLengthForInterleavedBlock() const {
   return m_rsBlockData;
 }
 
-int32_t CBC_SymbolInfo::getErrorLengthForInterleavedBlock(int32_t index) const {
+size_t CBC_SymbolInfo::getErrorLengthForInterleavedBlock() const {
   return m_rsBlockError;
 }
diff --git a/fxbarcode/datamatrix/BC_SymbolInfo.h b/fxbarcode/datamatrix/BC_SymbolInfo.h
index bad6d3b..07e563d 100644
--- a/fxbarcode/datamatrix/BC_SymbolInfo.h
+++ b/fxbarcode/datamatrix/BC_SymbolInfo.h
@@ -31,8 +31,8 @@
   int32_t getSymbolHeight() const;
   size_t getCodewordCount() const;
   virtual size_t getInterleavedBlockCount() const;
-  int32_t getDataLengthForInterleavedBlock(int32_t index) const;
-  int32_t getErrorLengthForInterleavedBlock(int32_t index) const;
+  size_t getDataLengthForInterleavedBlock() const;
+  size_t getErrorLengthForInterleavedBlock() const;
 
   size_t dataCapacity() const { return m_dataCapacity; }
   size_t errorCodewords() const { return m_errorCodewords; }
@@ -45,8 +45,8 @@
                  int32_t matrixWidth,
                  int32_t matrixHeight,
                  int32_t dataRegions,
-                 int32_t rsBlockData,
-                 int32_t rsBlockError);
+                 size_t rsBlockData,
+                 size_t rsBlockError);
 
  private:
   int32_t getHorizontalDataRegions() const;
@@ -58,8 +58,8 @@
   const int32_t m_matrixWidth;
   const int32_t m_matrixHeight;
   const int32_t m_dataRegions;
-  const int32_t m_rsBlockData;
-  const int32_t m_rsBlockError;
+  const size_t m_rsBlockData;
+  const size_t m_rsBlockError;
 };
 
 #endif  // FXBARCODE_DATAMATRIX_BC_SYMBOLINFO_H_
diff --git a/fxbarcode/oned/BC_OneDimWriter.cpp b/fxbarcode/oned/BC_OneDimWriter.cpp
index 54f44a3..cd01697 100644
--- a/fxbarcode/oned/BC_OneDimWriter.cpp
+++ b/fxbarcode/oned/BC_OneDimWriter.cpp
@@ -94,10 +94,9 @@
   bool color = startColor;
   int32_t numAdded = 0;
   for (int32_t i = 0; i < patternLength; i++) {
-    for (int32_t j = 0; j < pattern[i]; j++) {
+    for (int32_t j = 0; j < pattern[i]; j++)
       target[pos++] = color ? 1 : 0;
-      numAdded += 1;
-    }
+    numAdded += pattern[i];
     color = !color;
   }
   return numAdded;
@@ -112,12 +111,12 @@
   std::unique_ptr<CFX_UnicodeEncodingEx> encoding =
       FX_CreateFontEncodingEx(cFont);
 
-  size_t length = text.GetLength();
-  uint32_t* pCharCode = FX_Alloc(uint32_t, text.GetLength());
+  const size_t length = text.GetLength();
+  std::vector<uint32_t> charcodes(length);
   float charWidth = 0;
-  for (size_t j = 0; j < length; j++) {
-    pCharCode[j] = encoding->CharCodeFromUnicode(text[j]);
-    int32_t glyph_code = encoding->GlyphFromCharCode(pCharCode[j]);
+  for (size_t i = 0; i < length; ++i) {
+    charcodes[i] = encoding->CharCodeFromUnicode(text[i]);
+    int32_t glyph_code = encoding->GlyphFromCharCode(charcodes[i]);
     uint32_t glyph_value = cFont->GetGlyphWidth(glyph_code);
     float temp = glyph_value * fontSize / 1000.0;
     charWidth += temp;
@@ -132,7 +131,7 @@
   float left = leftPositon;
   float top = 0.0;
   charPos[0].m_Origin = CFX_PointF(penX + left, penY + top);
-  charPos[0].m_GlyphIndex = encoding->GlyphFromCharCode(pCharCode[0]);
+  charPos[0].m_GlyphIndex = encoding->GlyphFromCharCode(charcodes[0]);
   charPos[0].m_FontCharWidth = cFont->GetGlyphWidth(charPos[0].m_GlyphIndex);
 #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
   charPos[0].m_ExtGID = charPos[0].m_GlyphIndex;
@@ -140,14 +139,13 @@
   penX += (float)(charPos[0].m_FontCharWidth) * (float)fontSize / 1000.0f;
   for (size_t i = 1; i < length; i++) {
     charPos[i].m_Origin = CFX_PointF(penX + left, penY + top);
-    charPos[i].m_GlyphIndex = encoding->GlyphFromCharCode(pCharCode[i]);
+    charPos[i].m_GlyphIndex = encoding->GlyphFromCharCode(charcodes[i]);
     charPos[i].m_FontCharWidth = cFont->GetGlyphWidth(charPos[i].m_GlyphIndex);
 #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
     charPos[i].m_ExtGID = charPos[i].m_GlyphIndex;
 #endif
     penX += (float)(charPos[i].m_FontCharWidth) * (float)fontSize / 1000.0f;
   }
-  FX_Free(pCharCode);
 }
 
 void CBC_OneDimWriter::ShowDeviceChars(CFX_RenderDevice* device,