1 // Copyright 2014 PDFium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 6 // Original code is licensed as follows: 7 /* 8 * Copyright 2007 ZXing authors 9 * 10 * Licensed under the Apache License, Version 2.0 (the "License"); 11 * you may not use this file except in compliance with the License. 12 * You may obtain a copy of the License at 13 * 14 * http://www.apache.org/licenses/LICENSE-2.0 15 * 16 * Unless required by applicable law or agreed to in writing, software 17 * distributed under the License is distributed on an "AS IS" BASIS, 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 * See the License for the specific language governing permissions and 20 * limitations under the License. 21 */ 22 23 #include "fxbarcode/qrcode/BC_QRCoderBitVector.h" 24 25 #include "core/fxcrt/fx_memory.h" 26 #include "fxbarcode/utils.h" 27 CBC_QRCoderBitVector()28CBC_QRCoderBitVector::CBC_QRCoderBitVector() {} 29 ~CBC_QRCoderBitVector()30CBC_QRCoderBitVector::~CBC_QRCoderBitVector() {} 31 At(size_t index,int32_t & e) const32int32_t CBC_QRCoderBitVector::At(size_t index, int32_t& e) const { 33 if (index >= m_sizeInBits) { 34 e = BCExceptionBadIndexException; 35 return 0; 36 } 37 int32_t value = m_array[index >> 3] & 0xff; 38 return (value >> (7 - (index & 0x7))) & 1; 39 } 40 sizeInBytes() const41size_t CBC_QRCoderBitVector::sizeInBytes() const { 42 return (m_sizeInBits + 7) >> 3; 43 } 44 Size() const45size_t CBC_QRCoderBitVector::Size() const { 46 return m_sizeInBits; 47 } 48 AppendBit(int32_t bit)49void CBC_QRCoderBitVector::AppendBit(int32_t bit) { 50 ASSERT(bit == 0 || bit == 1); 51 int32_t numBitsInLastByte = m_sizeInBits & 0x7; 52 if (numBitsInLastByte == 0) { 53 AppendByte(0); 54 m_sizeInBits -= 8; 55 } 56 m_array[m_sizeInBits >> 3] |= (bit << (7 - numBitsInLastByte)); 57 ++m_sizeInBits; 58 } 59 AppendBits(int32_t value,int32_t numBits)60void CBC_QRCoderBitVector::AppendBits(int32_t value, int32_t numBits) { 61 ASSERT(numBits > 0); 62 ASSERT(numBits <= 32); 63 64 int32_t numBitsLeft = numBits; 65 while (numBitsLeft > 0) { 66 if ((m_sizeInBits & 0x7) == 0 && numBitsLeft >= 8) { 67 AppendByte(static_cast<int8_t>((value >> (numBitsLeft - 8)) & 0xff)); 68 numBitsLeft -= 8; 69 } else { 70 AppendBit((value >> (numBitsLeft - 1)) & 1); 71 --numBitsLeft; 72 } 73 } 74 } 75 AppendBitVector(CBC_QRCoderBitVector * bits)76void CBC_QRCoderBitVector::AppendBitVector(CBC_QRCoderBitVector* bits) { 77 int32_t size = bits->Size(); 78 for (int32_t i = 0; i < size; i++) { 79 int e = BCExceptionNO; 80 int32_t num = bits->At(i, e); 81 ASSERT(e == BCExceptionNO); 82 AppendBit(num); 83 } 84 } 85 XOR(const CBC_QRCoderBitVector * other)86bool CBC_QRCoderBitVector::XOR(const CBC_QRCoderBitVector* other) { 87 if (m_sizeInBits != other->Size()) 88 return false; 89 90 const auto* pOther = other->GetArray(); 91 for (size_t i = 0; i < sizeInBytes(); ++i) 92 m_array[i] ^= pOther[i]; 93 return true; 94 } 95 GetArray() const96const uint8_t* CBC_QRCoderBitVector::GetArray() const { 97 return m_array.data(); 98 } 99 AppendByte(int8_t value)100void CBC_QRCoderBitVector::AppendByte(int8_t value) { 101 if ((m_sizeInBits >> 3) == m_array.size()) 102 m_array.push_back(0); 103 m_array[m_sizeInBits >> 3] = value; 104 m_sizeInBits += 8; 105 } 106