Fix unsafe buffer usage in fxbarcode/oned
Convert to std::array<>.
Bug: 442271176
Change-Id: I942af55b3cb6d2875016891fef27dc8a7f5ee113
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/119576
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Thomas Sepez <tsepez@google.com>
diff --git a/fxbarcode/oned/BC_OnedCodaBarWriter.cpp b/fxbarcode/oned/BC_OnedCodaBarWriter.cpp
index bc40215..19d7e68 100644
--- a/fxbarcode/oned/BC_OnedCodaBarWriter.cpp
+++ b/fxbarcode/oned/BC_OnedCodaBarWriter.cpp
@@ -20,35 +20,30 @@
* limitations under the License.
*/
-#if defined(UNSAFE_BUFFERS_BUILD)
-// TODO(crbug.com/pdfium/2154): resolve buffer safety issues.
-#pragma allow_unsafe_buffers
-#endif
-
#include "fxbarcode/oned/BC_OnedCodaBarWriter.h"
+#include <array>
#include <iterator>
#include "core/fxcrt/containers/contains.h"
#include "core/fxcrt/fx_2d_size.h"
#include "core/fxcrt/fx_extension.h"
+#include "core/fxcrt/stl_util.h"
#include "fxbarcode/BC_Writer.h"
#include "fxbarcode/common/BC_CommonBitMatrix.h"
#include "fxbarcode/oned/BC_OneDimWriter.h"
namespace {
-const char kOnedCodaAlphabet[] = {'0', '1', '2', '3', '4', '5', '6', '7',
- '8', '9', '-', '$', ':', '/', '.', '+',
- 'A', 'B', 'C', 'D', 'T', 'N'};
+constexpr auto kOnedCodaAlphabet = fxcrt::ToArray<const char>(
+ {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-',
+ '$', ':', '/', '.', '+', 'A', 'B', 'C', 'D', 'T', 'N'});
+static_assert(std::size(kOnedCodaAlphabet) == 22, "Wrong size");
-const int8_t kOnedCodaCharacterEncoding[] = {
- 0x03, 0x06, 0x09, 0x60, 0x12, 0x42, 0x21, 0x24, 0x30, 0x48, 0x0c,
- 0x18, 0x45, 0x51, 0x54, 0x15, 0x1A, 0x29, 0x0B, 0x0E, 0x1A, 0x29};
+constexpr auto kOnedCodaCharacterEncoding = fxcrt::ToArray<const int8_t>(
+ {0x03, 0x06, 0x09, 0x60, 0x12, 0x42, 0x21, 0x24, 0x30, 0x48, 0x0c,
+ 0x18, 0x45, 0x51, 0x54, 0x15, 0x1A, 0x29, 0x0B, 0x0E, 0x1A, 0x29});
static_assert(std::size(kOnedCodaCharacterEncoding) == 22, "Wrong size");
-static_assert(std::size(kOnedCodaCharacterEncoding) ==
- std::size(kOnedCodaAlphabet),
- "Wrong size");
const char kStartEndChars[] = {'A', 'B', 'C', 'D', 'T', 'N', '*', 'E',
'a', 'b', 'c', 'd', 't', 'n', 'e'};
diff --git a/fxbarcode/oned/BC_OnedCode128Writer.cpp b/fxbarcode/oned/BC_OnedCode128Writer.cpp
index 368219e..2e9f7a9 100644
--- a/fxbarcode/oned/BC_OnedCode128Writer.cpp
+++ b/fxbarcode/oned/BC_OnedCode128Writer.cpp
@@ -20,15 +20,11 @@
* limitations under the License.
*/
-#if defined(UNSAFE_BUFFERS_BUILD)
-// TODO(crbug.com/pdfium/2154): resolve buffer safety issues.
-#pragma allow_unsafe_buffers
-#endif
-
#include "fxbarcode/oned/BC_OnedCode128Writer.h"
#include <ctype.h>
+#include <array>
#include <memory>
#include "core/fxcrt/check.h"
@@ -39,43 +35,44 @@
constexpr size_t kPatternSize = 7;
-const uint8_t kCodePatterns[107][kPatternSize] = {
- {2, 1, 2, 2, 2, 2, 0}, {2, 2, 2, 1, 2, 2, 0}, {2, 2, 2, 2, 2, 1, 0},
- {1, 2, 1, 2, 2, 3, 0}, {1, 2, 1, 3, 2, 2, 0}, {1, 3, 1, 2, 2, 2, 0},
- {1, 2, 2, 2, 1, 3, 0}, {1, 2, 2, 3, 1, 2, 0}, {1, 3, 2, 2, 1, 2, 0},
- {2, 2, 1, 2, 1, 3, 0}, {2, 2, 1, 3, 1, 2, 0}, {2, 3, 1, 2, 1, 2, 0},
- {1, 1, 2, 2, 3, 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2, 1, 4, 0},
- {2, 1, 1, 2, 3, 2, 0}, {2, 3, 3, 1, 1, 1, 2}};
+using CodePatternRow = std::array<uint8_t, kPatternSize>;
+constexpr std::array<const CodePatternRow, 107> kCodePatternTable = {
+ {{2, 1, 2, 2, 2, 2, 0}, {2, 2, 2, 1, 2, 2, 0}, {2, 2, 2, 2, 2, 1, 0},
+ {1, 2, 1, 2, 2, 3, 0}, {1, 2, 1, 3, 2, 2, 0}, {1, 3, 1, 2, 2, 2, 0},
+ {1, 2, 2, 2, 1, 3, 0}, {1, 2, 2, 3, 1, 2, 0}, {1, 3, 2, 2, 1, 2, 0},
+ {2, 2, 1, 2, 1, 3, 0}, {2, 2, 1, 3, 1, 2, 0}, {2, 3, 1, 2, 1, 2, 0},
+ {1, 1, 2, 2, 3, 2, 0}, {1, 2, 2, 1, 3, 2, 0}, {1, 2, 2, 2, 3, 1, 0},
+ {1, 1, 3, 2, 2, 2, 0}, {1, 2, 3, 1, 2, 2, 0}, {1, 2, 3, 2, 2, 1, 0},
+ {2, 2, 3, 2, 1, 1, 0}, {2, 2, 1, 1, 3, 2, 0}, {2, 2, 1, 2, 3, 1, 0},
+ {2, 1, 3, 2, 1, 2, 0}, {2, 2, 3, 1, 1, 2, 0}, {3, 1, 2, 1, 3, 1, 0},
+ {3, 1, 1, 2, 2, 2, 0}, {3, 2, 1, 1, 2, 2, 0}, {3, 2, 1, 2, 2, 1, 0},
+ {3, 1, 2, 2, 1, 2, 0}, {3, 2, 2, 1, 1, 2, 0}, {3, 2, 2, 2, 1, 1, 0},
+ {2, 1, 2, 1, 2, 3, 0}, {2, 1, 2, 3, 2, 1, 0}, {2, 3, 2, 1, 2, 1, 0},
+ {1, 1, 1, 3, 2, 3, 0}, {1, 3, 1, 1, 2, 3, 0}, {1, 3, 1, 3, 2, 1, 0},
+ {1, 1, 2, 3, 1, 3, 0}, {1, 3, 2, 1, 1, 3, 0}, {1, 3, 2, 3, 1, 1, 0},
+ {2, 1, 1, 3, 1, 3,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{1, 2, 4, 2, 1, 1, 0}, {4, 1, 1, 2, 1, 2, 0},
+ {4, 2, 1, 1, 1, 2, 0}, {4, 2, 1, 2, 1, 1, 0}, {2, 1, 2, 1, 4, 1, 0},
+ {2, 1, 4, 1, 2, 1, 0}, {4, 1, 2, 1, 2, 1, 0}, {1, 1, 1, 1, 4, 3, 0},
+ {1, 1, 1, 3, 4, 1, 0}, {1, 3, 1, 1, 4, 1, 0}, {1, 1, 4, 1, 1, 3, 0},
+ {1, 1, 4, 3, 1, 1, 0}, {4, 1, 1, 1, 1, 3, 0}, {4, 1, 1, 3, 1, 1, 0},
+ {1, 1, 3, 1, 4, 1, 0}, {1, 1, 4, 1, 3, 1, 0}, {3, 1, 1, 1, 4, 1, 0},
+ {4, 1, 1, 1, 3, 1, 0}, {2, 1, 1, 4, 1, 2, 0}, {2, 1, 1, 2, 1, 4, 0},
+ {2, 1, 1, 2, 3, 2, 0}, {2, 3, 3, 1, 1, 1, 2}}};
const int32_t CODE_START_B = 104;
const int32_t CODE_START_C = 105;
@@ -139,15 +136,14 @@
m_iContentLen = contents.GetLength() + 3;
int32_t codeWidth = 0;
for (const auto& patternIndex : patterns) {
- const uint8_t* pattern = kCodePatterns[patternIndex];
for (size_t i = 0; i < kPatternSize; ++i)
- codeWidth += pattern[i];
+ codeWidth += kCodePatternTable[patternIndex][i];
}
DataVector<uint8_t> result(codeWidth);
auto result_span = pdfium::make_span(result);
for (const int32_t pattern_index : patterns) {
result_span =
- AppendPattern(result_span, kCodePatterns[pattern_index], true);
+ AppendPattern(result_span, kCodePatternTable[pattern_index], true);
}
return result;
}
diff --git a/fxbarcode/oned/BC_OnedCode39Writer.cpp b/fxbarcode/oned/BC_OnedCode39Writer.cpp
index db6cf06..880222c 100644
--- a/fxbarcode/oned/BC_OnedCode39Writer.cpp
+++ b/fxbarcode/oned/BC_OnedCode39Writer.cpp
@@ -20,42 +20,40 @@
* limitations under the License.
*/
-#if defined(UNSAFE_BUFFERS_BUILD)
-// TODO(crbug.com/pdfium/2154): resolve buffer safety issues.
-#pragma allow_unsafe_buffers
-#endif
-
#include "fxbarcode/oned/BC_OnedCode39Writer.h"
#include <algorithm>
+#include <array>
#include <iterator>
#include <memory>
+#include "core/fxcrt/compiler_specific.h"
#include "core/fxcrt/fx_extension.h"
+#include "core/fxcrt/stl_util.h"
#include "fxbarcode/BC_Writer.h"
#include "fxbarcode/common/BC_CommonBitMatrix.h"
#include "fxbarcode/oned/BC_OneDimWriter.h"
namespace {
-const char kOnedCode39Alphabet[] = {
- '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
- 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
- 'U', 'V', 'W', 'X', 'Y', 'Z', '-', '.', ' ', '*', '$', '/', '+', '%'};
+constexpr auto kOnedCode39Alphabet = fxcrt::ToArray<const char>(
+ {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
+ 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
+ 'U', 'V', 'W', 'X', 'Y', 'Z', '-', '.', ' ', '*', '$', '/', '+', '%'});
constexpr size_t kOnedCode39AlphabetLen = std::size(kOnedCode39Alphabet);
-const char kOnedCode39Checksum[] = {
- '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
- 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
- 'U', 'V', 'W', 'X', 'Y', 'Z', '-', '.', ' ', '$', '/', '+', '%'};
+constexpr auto kOnedCode39Checksum = fxcrt::ToArray<const char>(
+ {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
+ 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
+ 'U', 'V', 'W', 'X', 'Y', 'Z', '-', '.', ' ', '$', '/', '+', '%'});
static_assert(std::size(kOnedCode39Checksum) == 43, "Wrong size");
-const int16_t kOnedCode39CharacterEncoding[] = {
- 0x0034, 0x0121, 0x0061, 0x0160, 0x0031, 0x0130, 0x0070, 0x0025, 0x0124,
- 0x0064, 0x0109, 0x0049, 0x0148, 0x0019, 0x0118, 0x0058, 0x000D, 0x010C,
- 0x004C, 0x001C, 0x0103, 0x0043, 0x0142, 0x0013, 0x0112, 0x0052, 0x0007,
- 0x0106, 0x0046, 0x0016, 0x0181, 0x00C1, 0x01C0, 0x0091, 0x0190, 0x00D0,
- 0x0085, 0x0184, 0x00C4, 0x0094, 0x00A8, 0x00A2, 0x008A, 0x002A};
+constexpr auto kOnedCode39CharacterEncoding = fxcrt::ToArray<const uint16_t>(
+ {0x0034, 0x0121, 0x0061, 0x0160, 0x0031, 0x0130, 0x0070, 0x0025, 0x0124,
+ 0x0064, 0x0109, 0x0049, 0x0148, 0x0019, 0x0118, 0x0058, 0x000D, 0x010C,
+ 0x004C, 0x001C, 0x0103, 0x0043, 0x0142, 0x0013, 0x0112, 0x0052, 0x0007,
+ 0x0106, 0x0046, 0x0016, 0x0181, 0x00C1, 0x01C0, 0x0091, 0x0190, 0x00D0,
+ 0x0085, 0x0184, 0x00C4, 0x0094, 0x00A8, 0x00A2, 0x008A, 0x002A});
static_assert(std::size(kOnedCode39CharacterEncoding) == 44, "Wrong size");
bool IsInOnedCode39Alphabet(wchar_t ch) {
@@ -84,6 +82,12 @@
return kOnedCode39Checksum[checksum % std::size(kOnedCode39Checksum)];
}
+void ToIntArray(int16_t value, uint8_t ratio, pdfium::span<uint8_t> span) {
+ for (size_t i = 0; i < span.size(); i++) {
+ span[i] = (value & (1 << i)) == 0 ? 1 : ratio;
+ }
+}
+
} // namespace
CBC_OnedCode39Writer::CBC_OnedCode39Writer() = default;
@@ -143,19 +147,12 @@
return true;
}
-void CBC_OnedCode39Writer::ToIntArray(int16_t value,
- uint8_t array[kArraySize]) const {
- for (size_t i = 0; i < kArraySize; i++) {
- array[i] = (value & (1 << i)) == 0 ? 1 : m_iWideNarrRatio;
- }
-}
-
DataVector<uint8_t> CBC_OnedCode39Writer::Encode(const ByteString& contents) {
char checksum = CalcCheckSum(contents);
if (checksum == '*')
return DataVector<uint8_t>();
- uint8_t widths[kArraySize] = {0};
+ std::array<uint8_t, kArraySize> widths = {};
constexpr int32_t kWideStrideNum = 3;
constexpr int32_t kNarrowStrideNum = kArraySize - kWideStrideNum;
ByteString encodedContents = contents;
@@ -167,17 +164,17 @@
m_iContentLen;
for (size_t j = 0; j < m_iContentLen; j++) {
for (size_t i = 0; i < kOnedCode39AlphabetLen; i++) {
- if (kOnedCode39Alphabet[i] != encodedContents[j])
+ if (kOnedCode39Alphabet[i] != encodedContents[j]) {
continue;
-
- ToIntArray(kOnedCode39CharacterEncoding[i], widths);
+ }
+ ToIntArray(kOnedCode39CharacterEncoding[i], m_iWideNarrRatio, widths);
for (size_t k = 0; k < kArraySize; k++)
code_width += widths[k];
}
}
DataVector<uint8_t> result(code_width);
auto result_span = pdfium::make_span(result);
- ToIntArray(kOnedCode39CharacterEncoding[39], widths);
+ ToIntArray(kOnedCode39CharacterEncoding[39], m_iWideNarrRatio, widths);
result_span = AppendPattern(result_span, widths, true);
static constexpr uint8_t kNarrowWhite[] = {1};
@@ -185,15 +182,15 @@
for (int32_t l = m_iContentLen - 1; l >= 0; l--) {
for (size_t i = 0; i < kOnedCode39AlphabetLen; i++) {
- if (kOnedCode39Alphabet[i] != encodedContents[l])
+ if (kOnedCode39Alphabet[i] != encodedContents[l]) {
continue;
-
- ToIntArray(kOnedCode39CharacterEncoding[i], widths);
+ }
+ ToIntArray(kOnedCode39CharacterEncoding[i], m_iWideNarrRatio, widths);
result_span = AppendPattern(result_span, widths, true);
}
result_span = AppendPattern(result_span, kNarrowWhite, false);
}
- ToIntArray(kOnedCode39CharacterEncoding[39], widths);
+ ToIntArray(kOnedCode39CharacterEncoding[39], m_iWideNarrRatio, widths);
AppendPattern(result_span, widths, true);
for (size_t i = 0; i < code_width / 2; i++) {
diff --git a/fxbarcode/oned/BC_OnedCode39Writer.h b/fxbarcode/oned/BC_OnedCode39Writer.h
index 932d703..55ac3f1 100644
--- a/fxbarcode/oned/BC_OnedCode39Writer.h
+++ b/fxbarcode/oned/BC_OnedCode39Writer.h
@@ -32,8 +32,6 @@
private:
static constexpr size_t kArraySize = 9;
- void ToIntArray(int16_t value, uint8_t array[kArraySize]) const;
-
int8_t m_iWideNarrRatio = 3;
};
diff --git a/fxbarcode/oned/BC_OnedEAN13Writer.cpp b/fxbarcode/oned/BC_OnedEAN13Writer.cpp
index 65917f9..2f43309 100644
--- a/fxbarcode/oned/BC_OnedEAN13Writer.cpp
+++ b/fxbarcode/oned/BC_OnedEAN13Writer.cpp
@@ -20,16 +20,12 @@
* limitations under the License.
*/
-#if defined(UNSAFE_BUFFERS_BUILD)
-// TODO(crbug.com/pdfium/2154): resolve buffer safety issues.
-#pragma allow_unsafe_buffers
-#endif
-
#include "fxbarcode/oned/BC_OnedEAN13Writer.h"
#include <math.h>
#include <algorithm>
+#include <array>
#include <memory>
#include <vector>
@@ -42,18 +38,31 @@
namespace {
-const int8_t kFirstDigitEncodings[10] = {0x00, 0x0B, 0x0D, 0xE, 0x13,
- 0x19, 0x1C, 0x15, 0x16, 0x1A};
+constexpr std::array<const int8_t, 10> kFirstDigitEncodings = {
+ {0x00, 0x0B, 0x0D, 0xE, 0x13, 0x19, 0x1C, 0x15, 0x16, 0x1A}};
+
const uint8_t kOnedEAN13StartPattern[3] = {1, 1, 1};
const uint8_t kOnedEAN13MiddlePattern[5] = {1, 1, 1, 1, 1};
-const uint8_t kOnedEAN13LPattern[10][4] = {
- {3, 2, 1, 1}, {2, 2, 2, 1}, {2, 1, 2, 2}, {1, 4, 1, 1}, {1, 1, 3, 2},
- {1, 2, 3, 1}, {1, 1, 1, 4}, {1, 3, 1, 2}, {1, 2, 1, 3}, {3, 1, 1, 2}};
-const uint8_t kOnedEAN13LGPattern[20][4] = {
- {3, 2, 1, 1}, {2, 2, 2, 1}, {2, 1, 2, 2}, {1, 4, 1, 1}, {1, 1, 3, 2},
- {1, 2, 3, 1}, {1, 1, 1, 4}, {1, 3, 1, 2}, {1, 2, 1, 3}, {3, 1, 1, 2},
- {1, 1, 2, 3}, {1, 2, 2, 2}, {2, 2, 1, 2}, {1, 1, 4, 1}, {2, 3, 1, 1},
- {1, 3, 2, 1}, {4, 1, 1, 1}, {2, 1, 3, 1}, {3, 1, 2, 1}, {2, 1, 1, 3}};
+
+using LPatternRow = std::array<uint8_t, 4>;
+constexpr std::array<const LPatternRow, 10> kOnedEAN13LPatternTable = {
+ {{3, 2, 1, 1},
+ {2, 2, 2, 1},
+ {2, 1, 2, 2},
+ {1, 4, 1, 1},
+ {1, 1, 3, 2},
+ {1, 2, 3, 1},
+ {1, 1, 1, 4},
+ {1, 3, 1, 2},
+ {1, 2, 1, 3},
+ {3, 1, 1, 2}}};
+
+using LGPatternRow = std::array<uint8_t, 4>;
+constexpr std::array<const LGPatternRow, 20> kOnedEAN13LGPatternTable = {
+ {{3, 2, 1, 1}, {2, 2, 2, 1}, {2, 1, 2, 2}, {1, 4, 1, 1}, {1, 1, 3, 2},
+ {1, 2, 3, 1}, {1, 1, 1, 4}, {1, 3, 1, 2}, {1, 2, 1, 3}, {3, 1, 1, 2},
+ {1, 1, 2, 3}, {1, 2, 2, 2}, {2, 2, 1, 2}, {1, 1, 4, 1}, {2, 3, 1, 1},
+ {1, 3, 2, 1}, {4, 1, 1, 1}, {2, 1, 3, 1}, {3, 1, 2, 1}, {2, 1, 1, 3}}};
} // namespace
@@ -105,13 +114,15 @@
if ((parities >> (6 - i) & 1) == 1) {
digit += 10;
}
- result_span = AppendPattern(result_span, kOnedEAN13LGPattern[digit], false);
+ result_span =
+ AppendPattern(result_span, kOnedEAN13LGPatternTable[digit], false);
}
result_span = AppendPattern(result_span, kOnedEAN13MiddlePattern, false);
for (int i = 7; i <= 12; i++) {
int32_t digit = FXSYS_DecimalCharToInt(contents[i]);
- result_span = AppendPattern(result_span, kOnedEAN13LPattern[digit], true);
+ result_span =
+ AppendPattern(result_span, kOnedEAN13LPatternTable[digit], true);
}
AppendPattern(result_span, kOnedEAN13StartPattern, true);
return result;
diff --git a/fxbarcode/oned/BC_OnedEAN8Writer.cpp b/fxbarcode/oned/BC_OnedEAN8Writer.cpp
index 2cf46a6..1a9a7e6 100644
--- a/fxbarcode/oned/BC_OnedEAN8Writer.cpp
+++ b/fxbarcode/oned/BC_OnedEAN8Writer.cpp
@@ -20,16 +20,12 @@
* limitations under the License.
*/
-#if defined(UNSAFE_BUFFERS_BUILD)
-// TODO(crbug.com/pdfium/2154): resolve buffer safety issues.
-#pragma allow_unsafe_buffers
-#endif
-
#include "fxbarcode/oned/BC_OnedEAN8Writer.h"
#include <math.h>
#include <algorithm>
+#include <array>
#include <memory>
#include <vector>
@@ -45,9 +41,19 @@
const uint8_t kOnedEAN8StartPattern[3] = {1, 1, 1};
const uint8_t kOnedEAN8MiddlePattern[5] = {1, 1, 1, 1, 1};
-const uint8_t kOnedEAN8LPattern[10][4] = {
- {3, 2, 1, 1}, {2, 2, 2, 1}, {2, 1, 2, 2}, {1, 4, 1, 1}, {1, 1, 3, 2},
- {1, 2, 3, 1}, {1, 1, 1, 4}, {1, 3, 1, 2}, {1, 2, 1, 3}, {3, 1, 1, 2}};
+
+using LPatternRow = std::array<uint8_t, 4>;
+constexpr std::array<const LPatternRow, 10> kOnedEAN8LPatternTable = {
+ {{3, 2, 1, 1},
+ {2, 2, 2, 1},
+ {2, 1, 2, 2},
+ {1, 4, 1, 1},
+ {1, 1, 3, 2},
+ {1, 2, 3, 1},
+ {1, 1, 1, 4},
+ {1, 3, 1, 2},
+ {1, 2, 1, 3},
+ {3, 1, 1, 2}}};
} // namespace
@@ -102,13 +108,15 @@
for (int i = 0; i <= 3; i++) {
int32_t digit = FXSYS_DecimalCharToInt(contents[i]);
- result_span = AppendPattern(result_span, kOnedEAN8LPattern[digit], false);
+ result_span =
+ AppendPattern(result_span, kOnedEAN8LPatternTable[digit], false);
}
result_span = AppendPattern(result_span, kOnedEAN8MiddlePattern, false);
for (int i = 4; i <= 7; i++) {
int32_t digit = FXSYS_DecimalCharToInt(contents[i]);
- result_span = AppendPattern(result_span, kOnedEAN8LPattern[digit], true);
+ result_span =
+ AppendPattern(result_span, kOnedEAN8LPatternTable[digit], true);
}
AppendPattern(result_span, kOnedEAN8StartPattern, true);
return result;