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_system.h"
26 #include "third_party/base/logging.h"
27 
28 CBC_QRCoderBitVector::CBC_QRCoderBitVector() = default;
29 
30 CBC_QRCoderBitVector::~CBC_QRCoderBitVector() = default;
31 
At(size_t index) const32 int32_t CBC_QRCoderBitVector::At(size_t index) const {
33   CHECK(index < m_sizeInBits);
34   int32_t value = m_array[index >> 3] & 0xff;
35   return (value >> (7 - (index & 0x7))) & 1;
36 }
37 
sizeInBytes() const38 size_t CBC_QRCoderBitVector::sizeInBytes() const {
39   return (m_sizeInBits + 7) >> 3;
40 }
41 
Size() const42 size_t CBC_QRCoderBitVector::Size() const {
43   return m_sizeInBits;
44 }
45 
AppendBit(int32_t bit)46 void CBC_QRCoderBitVector::AppendBit(int32_t bit) {
47   ASSERT(bit == 0 || bit == 1);
48   int32_t numBitsInLastByte = m_sizeInBits & 0x7;
49   if (numBitsInLastByte == 0) {
50     AppendByte(0);
51     m_sizeInBits -= 8;
52   }
53   m_array[m_sizeInBits >> 3] |= (bit << (7 - numBitsInLastByte));
54   ++m_sizeInBits;
55 }
56 
AppendBits(int32_t value,int32_t numBits)57 void CBC_QRCoderBitVector::AppendBits(int32_t value, int32_t numBits) {
58   ASSERT(numBits > 0);
59   ASSERT(numBits <= 32);
60 
61   int32_t numBitsLeft = numBits;
62   while (numBitsLeft > 0) {
63     if ((m_sizeInBits & 0x7) == 0 && numBitsLeft >= 8) {
64       AppendByte(static_cast<int8_t>((value >> (numBitsLeft - 8)) & 0xff));
65       numBitsLeft -= 8;
66     } else {
67       AppendBit((value >> (numBitsLeft - 1)) & 1);
68       --numBitsLeft;
69     }
70   }
71 }
72 
AppendBitVector(const CBC_QRCoderBitVector * bits)73 void CBC_QRCoderBitVector::AppendBitVector(const CBC_QRCoderBitVector* bits) {
74   for (size_t i = 0; i < bits->Size(); i++)
75     AppendBit(bits->At(i));
76 }
77 
XOR(const CBC_QRCoderBitVector * other)78 bool CBC_QRCoderBitVector::XOR(const CBC_QRCoderBitVector* other) {
79   if (m_sizeInBits != other->Size())
80     return false;
81 
82   const auto* pOther = other->GetArray();
83   for (size_t i = 0; i < sizeInBytes(); ++i)
84     m_array[i] ^= pOther[i];
85   return true;
86 }
87 
GetArray() const88 const uint8_t* CBC_QRCoderBitVector::GetArray() const {
89   return m_array.data();
90 }
91 
AppendByte(int8_t value)92 void CBC_QRCoderBitVector::AppendByte(int8_t value) {
93   if ((m_sizeInBits >> 3) == m_array.size())
94     m_array.push_back(0);
95   m_array[m_sizeInBits >> 3] = value;
96   m_sizeInBits += 8;
97 }
98