From b2a40475ade2fe34a406472e53787bdac5a6950a Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Tue, 4 Apr 2017 16:15:13 -0700 Subject: Clean up QRCoderBitVector. Use std::vector and return booleans results when possible. Change-Id: If3ce4559f137fb449fd1ab818750558a1b5f8df0 Reviewed-on: https://pdfium-review.googlesource.com/3561 Commit-Queue: Lei Zhang Reviewed-by: Tom Sepez --- fxbarcode/qrcode/BC_QRCoderBitVector.cpp | 110 ++++++++++-------------- fxbarcode/qrcode/BC_QRCoderBitVector.h | 38 +++++---- fxbarcode/qrcode/BC_QRCoderEncoder.cpp | 135 +++++++++++------------------- fxbarcode/qrcode/BC_QRCoderMatrixUtil.cpp | 42 ++++------ fxbarcode/qrcode/BC_QRCoderVersion.cpp | 2 +- 5 files changed, 129 insertions(+), 198 deletions(-) (limited to 'fxbarcode/qrcode') diff --git a/fxbarcode/qrcode/BC_QRCoderBitVector.cpp b/fxbarcode/qrcode/BC_QRCoderBitVector.cpp index 2e3c341792..7a41ad0910 100644 --- a/fxbarcode/qrcode/BC_QRCoderBitVector.cpp +++ b/fxbarcode/qrcode/BC_QRCoderBitVector.cpp @@ -20,45 +20,34 @@ * limitations under the License. */ -#include "core/fxcrt/fx_memory.h" #include "fxbarcode/qrcode/BC_QRCoderBitVector.h" + +#include "core/fxcrt/fx_memory.h" #include "fxbarcode/utils.h" -CBC_QRCoderBitVector::CBC_QRCoderBitVector() { - m_sizeInBits = 0; - m_size = 32; -} -void CBC_QRCoderBitVector::Init() { - m_array = FX_Alloc(uint8_t, m_size); -} -CBC_QRCoderBitVector::~CBC_QRCoderBitVector() { - FX_Free(m_array); -} -void CBC_QRCoderBitVector::Clear() { - FX_Free(m_array); - m_sizeInBits = 0; - m_size = 32; - m_array = FX_Alloc(uint8_t, m_size); -} -int32_t CBC_QRCoderBitVector::At(int32_t index, int32_t& e) { - if (index < 0 || index >= m_sizeInBits) { +CBC_QRCoderBitVector::CBC_QRCoderBitVector() {} + +CBC_QRCoderBitVector::~CBC_QRCoderBitVector() {} + +int32_t CBC_QRCoderBitVector::At(size_t index, int32_t& e) const { + if (index >= m_sizeInBits) { e = BCExceptionBadIndexException; return 0; } int32_t value = m_array[index >> 3] & 0xff; return (value >> (7 - (index & 0x7))) & 1; } -int32_t CBC_QRCoderBitVector::sizeInBytes() { + +size_t CBC_QRCoderBitVector::sizeInBytes() const { return (m_sizeInBits + 7) >> 3; } -int32_t CBC_QRCoderBitVector::Size() { + +size_t CBC_QRCoderBitVector::Size() const { return m_sizeInBits; } -void CBC_QRCoderBitVector::AppendBit(int32_t bit, int32_t& e) { - if (!(bit == 0 || bit == 1)) { - e = BCExceptionBadValueException; - return; - } + +void CBC_QRCoderBitVector::AppendBit(int32_t bit) { + ASSERT(bit == 0 || bit == 1); int32_t numBitsInLastByte = m_sizeInBits & 0x7; if (numBitsInLastByte == 0) { AppendByte(0); @@ -67,61 +56,50 @@ void CBC_QRCoderBitVector::AppendBit(int32_t bit, int32_t& e) { m_array[m_sizeInBits >> 3] |= (bit << (7 - numBitsInLastByte)); ++m_sizeInBits; } -void CBC_QRCoderBitVector::AppendBits(int32_t value, - int32_t numBits, - int32_t& e) { - if (numBits < 0 || numBits > 32) { - e = BCExceptionBadNumBitsException; - return; - } + +void CBC_QRCoderBitVector::AppendBits(int32_t value, int32_t numBits) { + ASSERT(numBits > 0); + ASSERT(numBits <= 32); + int32_t numBitsLeft = numBits; while (numBitsLeft > 0) { if ((m_sizeInBits & 0x7) == 0 && numBitsLeft >= 8) { - int32_t newByte = (value >> (numBitsLeft - 8)) & 0xff; - AppendByte(newByte); + AppendByte(static_cast((value >> (numBitsLeft - 8)) & 0xff)); numBitsLeft -= 8; } else { - int32_t bit = (value >> (numBitsLeft - 1)) & 1; - AppendBit(bit, e); - if (e != BCExceptionNO) - return; + AppendBit((value >> (numBitsLeft - 1)) & 1); --numBitsLeft; } } } -void CBC_QRCoderBitVector::AppendBitVector(CBC_QRCoderBitVector* bits, - int32_t& e) { + +void CBC_QRCoderBitVector::AppendBitVector(CBC_QRCoderBitVector* bits) { int32_t size = bits->Size(); for (int32_t i = 0; i < size; i++) { + int e = BCExceptionNO; int32_t num = bits->At(i, e); - if (e != BCExceptionNO) - return; - AppendBit(num, e); - if (e != BCExceptionNO) - return; + ASSERT(e == BCExceptionNO); + AppendBit(num); } } -void CBC_QRCoderBitVector::XOR(CBC_QRCoderBitVector* other, int32_t& e) { - if (m_sizeInBits != other->Size()) { - e = BCExceptioncanNotOperatexorOperator; - return; - } - int32_t sizeInBytes = (m_sizeInBits + 7) >> 3; - for (int32_t i = 0; i < sizeInBytes; ++i) { - m_array[i] ^= (other->GetArray())[i]; - } + +bool CBC_QRCoderBitVector::XOR(const CBC_QRCoderBitVector* other) { + if (m_sizeInBits != other->Size()) + return false; + + const auto* pOther = other->GetArray(); + for (size_t i = 0; i < sizeInBytes(); ++i) + m_array[i] ^= pOther[i]; + return true; } -uint8_t* CBC_QRCoderBitVector::GetArray() { - return m_array; + +const uint8_t* CBC_QRCoderBitVector::GetArray() const { + return m_array.data(); } -void CBC_QRCoderBitVector::AppendByte(int32_t value) { - if ((m_sizeInBits >> 3) == m_size) { - uint8_t* newArray = FX_Alloc(uint8_t, m_size << 1); - memcpy(newArray, m_array, m_size); - FX_Free(m_array); - m_array = newArray; - m_size = m_size << 1; - } - m_array[m_sizeInBits >> 3] = (uint8_t)value; + +void CBC_QRCoderBitVector::AppendByte(int8_t value) { + if ((m_sizeInBits >> 3) == m_array.size()) + m_array.push_back(0); + m_array[m_sizeInBits >> 3] = value; m_sizeInBits += 8; } diff --git a/fxbarcode/qrcode/BC_QRCoderBitVector.h b/fxbarcode/qrcode/BC_QRCoderBitVector.h index 59c4360c1e..0ebeb60ab8 100644 --- a/fxbarcode/qrcode/BC_QRCoderBitVector.h +++ b/fxbarcode/qrcode/BC_QRCoderBitVector.h @@ -7,29 +7,31 @@ #ifndef FXBARCODE_QRCODE_BC_QRCODERBITVECTOR_H_ #define FXBARCODE_QRCODE_BC_QRCODERBITVECTOR_H_ +#include #include -class CBC_QRCoderBitVector { - private: - int32_t m_sizeInBits; - uint8_t* m_array; - int32_t m_size; - - void AppendByte(int32_t value); +#include +class CBC_QRCoderBitVector { public: CBC_QRCoderBitVector(); - virtual ~CBC_QRCoderBitVector(); - int32_t At(int32_t index, int32_t& e); - int32_t Size(); - int32_t sizeInBytes(); - void AppendBit(int32_t bit, int32_t& e); - void AppendBits(int32_t value, int32_t numBits, int32_t& e); - void AppendBitVector(CBC_QRCoderBitVector* bits, int32_t& e); - void XOR(CBC_QRCoderBitVector* other, int32_t& e); - uint8_t* GetArray(); - void Clear(); - virtual void Init(); + ~CBC_QRCoderBitVector(); + + const uint8_t* GetArray() const; + int32_t At(size_t index, int32_t& e) const; + size_t Size() const; + size_t sizeInBytes() const; + + void AppendBit(int32_t bit); + void AppendBits(int32_t value, int32_t numBits); + void AppendBitVector(CBC_QRCoderBitVector* bits); + bool XOR(const CBC_QRCoderBitVector* other); + + private: + void AppendByte(int8_t value); + + size_t m_sizeInBits = 0; + std::vector m_array; }; #endif // FXBARCODE_QRCODE_BC_QRCODERBITVECTOR_H_ diff --git a/fxbarcode/qrcode/BC_QRCoderEncoder.cpp b/fxbarcode/qrcode/BC_QRCoderEncoder.cpp index 285bd0a541..d74063d3fe 100644 --- a/fxbarcode/qrcode/BC_QRCoderEncoder.cpp +++ b/fxbarcode/qrcode/BC_QRCoderEncoder.cpp @@ -73,20 +73,14 @@ void AppendNumericBytes(const CFX_ByteString& content, if (i + 2 < length) { int32_t num2 = content[i + 1] - '0'; int32_t num3 = content[i + 2] - '0'; - bits->AppendBits(num1 * 100 + num2 * 10 + num3, 10, e); - if (e != BCExceptionNO) - return; + bits->AppendBits(num1 * 100 + num2 * 10 + num3, 10); i += 3; } else if (i + 1 < length) { int32_t num2 = content[i + 1] - '0'; - bits->AppendBits(num1 * 10 + num2, 7, e); - if (e != BCExceptionNO) - return; + bits->AppendBits(num1 * 10 + num2, 7); i += 2; } else { - bits->AppendBits(num1, 4, e); - if (e != BCExceptionNO) - return; + bits->AppendBits(num1, 4); i++; } } @@ -109,14 +103,10 @@ void AppendAlphaNumericBytes(const CFX_ByteString& content, e = BCExceptionInvalidateCharacter; return; } - bits->AppendBits(code1 * 45 + code2, 11, e); - if (e != BCExceptionNO) - return; + bits->AppendBits(code1 * 45 + code2, 11); i += 2; } else { - bits->AppendBits(code1, 6, e); - if (e != BCExceptionNO) - return; + bits->AppendBits(code1, 6); i++; } } @@ -138,9 +128,7 @@ void AppendGBKBytes(const CFX_ByteString& content, return; } value = (uint32_t)((value >> 8) * 0x60) + (uint32_t)(value & 0xff); - bits->AppendBits(value, 13, e); - if (e != BCExceptionNO) - return; + bits->AppendBits(value, 13); } } @@ -148,11 +136,8 @@ void Append8BitBytes(const CFX_ByteString& content, CBC_QRCoderBitVector* bits, CFX_ByteString encoding, int32_t& e) { - for (int32_t i = 0; i < content.GetLength(); i++) { - bits->AppendBits(content[i], 8, e); - if (e != BCExceptionNO) - return; - } + for (int32_t i = 0; i < content.GetLength(); i++) + bits->AppendBits(content[i], 8); } void AppendKanjiBytes(const CFX_ByteString& content, @@ -171,40 +156,34 @@ void AppendKanjiBytes(const CFX_ByteString& content, return; } value = (uint32_t)((value >> 8) * 0xc0) + (uint32_t)(value & 0xff); - bits->AppendBits(value, 13, e); - if (e != BCExceptionNO) - return; + bits->AppendBits(value, 13); } } -void AppendModeInfo(CBC_QRCoderMode* mode, - CBC_QRCoderBitVector* bits, - int32_t& e) { - bits->AppendBits(mode->GetBits(), 4, e); +void AppendModeInfo(CBC_QRCoderMode* mode, CBC_QRCoderBitVector* bits) { + bits->AppendBits(mode->GetBits(), 4); if (mode == CBC_QRCoderMode::sGBK) - bits->AppendBits(1, 4, e); + bits->AppendBits(1, 4); } -void AppendLengthInfo(int32_t numLetters, +bool AppendLengthInfo(int32_t numLetters, int32_t version, CBC_QRCoderMode* mode, - CBC_QRCoderBitVector* bits, - int32_t& e) { + CBC_QRCoderBitVector* bits) { + int32_t e = BCExceptionNO; CBC_QRCoderVersion* qcv = CBC_QRCoderVersion::GetVersionForNumber(version, e); if (e != BCExceptionNO) - return; + return false; int32_t numBits = mode->GetCharacterCountBits(qcv, e); if (e != BCExceptionNO) - return; - if (numBits > ((1 << numBits) - 1)) { - return; - } - if (mode == CBC_QRCoderMode::sGBK) { - bits->AppendBits(numLetters / 2, numBits, e); - if (e != BCExceptionNO) - return; - } - bits->AppendBits(numLetters, numBits, e); + return false; + if (numBits > ((1 << numBits) - 1)) + return true; + + if (mode == CBC_QRCoderMode::sGBK) + bits->AppendBits(numLetters / 2, numBits); + bits->AppendBits(numLetters, numBits); + return true; } void AppendBytes(const CFX_ByteString& content, @@ -376,35 +355,29 @@ void GetNumDataBytesAndNumECBytesForBlockID(int32_t numTotalBytes, void TerminateBits(int32_t numDataBytes, CBC_QRCoderBitVector* bits, int32_t& e) { - int32_t capacity = numDataBytes << 3; + size_t capacity = numDataBytes << 3; if (bits->Size() > capacity) { e = BCExceptionDataTooMany; return; } - for (int32_t i = 0; i < 4 && bits->Size() < capacity; ++i) { - bits->AppendBit(0, e); - if (e != BCExceptionNO) - return; - } + + for (int32_t i = 0; i < 4 && bits->Size() < capacity; ++i) + bits->AppendBit(0); + int32_t numBitsInLastByte = bits->Size() % 8; if (numBitsInLastByte > 0) { int32_t numPaddingBits = 8 - numBitsInLastByte; - for (int32_t j = 0; j < numPaddingBits; ++j) { - bits->AppendBit(0, e); - if (e != BCExceptionNO) - return; - } + for (int32_t j = 0; j < numPaddingBits; ++j) + bits->AppendBit(0); } + if (bits->Size() % 8 != 0) { e = BCExceptionDigitLengthMustBe8; return; } int32_t numPaddingBytes = numDataBytes - bits->sizeInBytes(); - for (int32_t k = 0; k < numPaddingBytes; ++k) { - bits->AppendBits(k % 2 ? 0x11 : 0xec, 8, e); - if (e != BCExceptionNO) - return; - } + for (int32_t k = 0; k < numPaddingBytes; ++k) + bits->AppendBits(k % 2 ? 0x11 : 0xec, 8); if (bits->Size() != capacity) e = BCExceptionBitsNotEqualCacity; } @@ -499,7 +472,7 @@ void SplitString(const CFX_ByteString& content, if (index >= content.GetLength()) return; - while (index < content.GetLength() && FXSYS_Isdigit(content[index])) + while (index < content.GetLength() && isdigit(content[index])) ++index; if (index != flag) { @@ -552,7 +525,9 @@ void InterleaveWithECBytes(CBC_QRCoderBitVector* bits, int32_t numRSBlocks, CBC_QRCoderBitVector* result, int32_t& e) { - if (bits->sizeInBytes() != numDataBytes) { + ASSERT(numTotalBytes >= 0); + ASSERT(numDataBytes >= 0); + if (bits->sizeInBytes() != static_cast(numDataBytes)) { e = BCExceptionBitsBytesNotMatch; return; } @@ -584,24 +559,18 @@ void InterleaveWithECBytes(CBC_QRCoderBitVector* bits, for (int32_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, e); - if (e != BCExceptionNO) - return; - } + if (x < dataBytes->Size()) + result->AppendBits(dataBytes->At(x), 8); } } for (int32_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, e); - if (e != BCExceptionNO) - return; - } + if (y < ecBytes->Size()) + result->AppendBits(ecBytes->At(y), 8); } } - if (numTotalBytes != result->sizeInBytes()) + if (static_cast(numTotalBytes) != result->sizeInBytes()) e = BCExceptionSizeInBytesDiffer; } @@ -620,7 +589,6 @@ void CBC_QRCoderEncoder::Encode(const CFX_WideString& content, CBC_UtilCodingConvert::UnicodeToUTF8(content, utf8Data); CBC_QRCoderMode* mode = ChooseMode(utf8Data, encoding); CBC_QRCoderBitVector dataBits; - dataBits.Init(); AppendBytes(utf8Data, mode, &dataBits, encoding, e); if (e != BCExceptionNO) return; @@ -629,24 +597,19 @@ void CBC_QRCoderEncoder::Encode(const CFX_WideString& content, if (e != BCExceptionNO) return; CBC_QRCoderBitVector headerAndDataBits; - headerAndDataBits.Init(); - AppendModeInfo(mode, &headerAndDataBits, e); - if (e != BCExceptionNO) - return; + AppendModeInfo(mode, &headerAndDataBits); int32_t numLetters = mode == CBC_QRCoderMode::sBYTE ? dataBits.sizeInBytes() : content.GetLength(); - AppendLengthInfo(numLetters, qrCode->GetVersion(), mode, &headerAndDataBits, - e); - if (e != BCExceptionNO) - return; - headerAndDataBits.AppendBitVector(&dataBits, e); - if (e != BCExceptionNO) + if (!AppendLengthInfo(numLetters, qrCode->GetVersion(), mode, + &headerAndDataBits)) { + e = BCExceptionGeneric; return; + } + headerAndDataBits.AppendBitVector(&dataBits); TerminateBits(qrCode->GetNumDataBytes(), &headerAndDataBits, e); if (e != BCExceptionNO) return; CBC_QRCoderBitVector finalBits; - finalBits.Init(); InterleaveWithECBytes(&headerAndDataBits, qrCode->GetNumTotalBytes(), qrCode->GetNumDataBytes(), qrCode->GetNumRSBlocks(), &finalBits, e); diff --git a/fxbarcode/qrcode/BC_QRCoderMatrixUtil.cpp b/fxbarcode/qrcode/BC_QRCoderMatrixUtil.cpp index b6ffaa00fc..b0be2b6f2a 100644 --- a/fxbarcode/qrcode/BC_QRCoderMatrixUtil.cpp +++ b/fxbarcode/qrcode/BC_QRCoderMatrixUtil.cpp @@ -130,6 +130,7 @@ void CBC_QRCoderMatrixUtil::EmbedBasicPatterns(int32_t version, if (e != BCExceptionNO) return; } + void CBC_QRCoderMatrixUtil::EmbedTypeInfo( CBC_QRCoderErrorCorrectionLevel* ecLevel, int32_t maskPattern, @@ -140,11 +141,11 @@ void CBC_QRCoderMatrixUtil::EmbedTypeInfo( return; } CBC_QRCoderBitVector typeInfoBits; - typeInfoBits.Init(); MakeTypeInfoBits(ecLevel, maskPattern, &typeInfoBits, e); if (e != BCExceptionNO) return; - for (int32_t i = 0; i < typeInfoBits.Size(); i++) { + + for (size_t i = 0; i < typeInfoBits.Size(); i++) { int32_t bit = typeInfoBits.At(typeInfoBits.Size() - 1 - i, e); if (e != BCExceptionNO) return; @@ -162,6 +163,7 @@ void CBC_QRCoderMatrixUtil::EmbedTypeInfo( } } } + void CBC_QRCoderMatrixUtil::MaybeEmbedVersionInfo(int32_t version, CBC_CommonByteMatrix* matrix, int32_t& e) { @@ -173,7 +175,6 @@ void CBC_QRCoderMatrixUtil::MaybeEmbedVersionInfo(int32_t version, return; } CBC_QRCoderBitVector versionInfoBits; - versionInfoBits.Init(); MakeVersionInfoBits(version, &versionInfoBits, e); if (e != BCExceptionNO) return; @@ -197,7 +198,7 @@ void CBC_QRCoderMatrixUtil::EmbedDataBits(CBC_QRCoderBitVector* dataBits, e = BCExceptionNullPointer; return; } - int32_t bitIndex = 0; + size_t bitIndex = 0; int32_t direction = -1; int32_t x = matrix->GetWidth() - 1; int32_t y = matrix->GetHeight() - 1; @@ -266,23 +267,16 @@ void CBC_QRCoderMatrixUtil::MakeTypeInfoBits( return; } int32_t typeInfo = (ecLevel->GetBits() << 3) | maskPattern; - if (e != BCExceptionNO) - return; - bits->AppendBits(typeInfo, 5, e); + bits->AppendBits(typeInfo, 5); int32_t bchCode = CalculateBCHCode(typeInfo, TYPE_INFO_POLY); - if (e != BCExceptionNO) - return; - bits->AppendBits(bchCode, 10, e); + bits->AppendBits(bchCode, 10); CBC_QRCoderBitVector maskBits; - maskBits.Init(); - maskBits.AppendBits(TYPE_INFO_MASK_PATTERN, 15, e); - if (e != BCExceptionNO) + maskBits.AppendBits(TYPE_INFO_MASK_PATTERN, 15); + if (!bits->XOR(&maskBits)) { + e = BCExceptionGeneric; return; - bits->XOR(&maskBits, e); - if (e != BCExceptionNO) - return; - if (bits->Size() != 15) - e = BCExceptionBitSizeNot15; + } + ASSERT(bits->Size() == 15); } void CBC_QRCoderMatrixUtil::MakeVersionInfoBits(int32_t version, @@ -292,17 +286,11 @@ void CBC_QRCoderMatrixUtil::MakeVersionInfoBits(int32_t version, e = BCExceptionNullPointer; return; } - bits->AppendBits(version, 6, e); - if (e != BCExceptionNO) - return; + bits->AppendBits(version, 6); int32_t bchCode = CalculateBCHCode(version, VERSION_INFO_POLY); - bits->AppendBits(bchCode, 12, e); - if (e != BCExceptionNO) - return; - - if (bits->Size() != 18) - e = BCExceptionBitSizeNot18; + bits->AppendBits(bchCode, 12); + ASSERT(bits->Size() == 18); } bool CBC_QRCoderMatrixUtil::IsEmpty(int32_t value) { diff --git a/fxbarcode/qrcode/BC_QRCoderVersion.cpp b/fxbarcode/qrcode/BC_QRCoderVersion.cpp index ff7cbc4007..f0b708dfc3 100644 --- a/fxbarcode/qrcode/BC_QRCoderVersion.cpp +++ b/fxbarcode/qrcode/BC_QRCoderVersion.cpp @@ -382,7 +382,7 @@ CBC_QRCoderVersion* CBC_QRCoderVersion::GetProvisionalVersionForDimension( CBC_QRCoderVersion* CBC_QRCoderVersion::DecodeVersionInformation( int32_t versionBits, int32_t& e) { - int32_t bestDifference = FXSYS_IntMax; + int32_t bestDifference = INT_MAX; int32_t bestVersion = 0; for (int32_t i = 0; i < 34; i++) { int32_t targetVersion = VERSION_DECODE_INFO[i]; -- cgit v1.2.3