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 "core/fxcrt/fx_memory.h"
24 #include "xfa/fxbarcode/qrcode/BC_QRCoderBitVector.h"
25 #include "xfa/fxbarcode/utils.h"
26
CBC_QRCoderBitVector()27 CBC_QRCoderBitVector::CBC_QRCoderBitVector() {
28 m_sizeInBits = 0;
29 m_size = 32;
30 }
Init()31 void CBC_QRCoderBitVector::Init() {
32 m_array = FX_Alloc(uint8_t, m_size);
33 }
~CBC_QRCoderBitVector()34 CBC_QRCoderBitVector::~CBC_QRCoderBitVector() {
35 FX_Free(m_array);
36 }
Clear()37 void CBC_QRCoderBitVector::Clear() {
38 FX_Free(m_array);
39 m_sizeInBits = 0;
40 m_size = 32;
41 m_array = FX_Alloc(uint8_t, m_size);
42 }
At(int32_t index,int32_t & e)43 int32_t CBC_QRCoderBitVector::At(int32_t index, int32_t& e) {
44 if (index < 0 || index >= m_sizeInBits) {
45 e = BCExceptionBadIndexException;
46 return 0;
47 }
48 int32_t value = m_array[index >> 3] & 0xff;
49 return (value >> (7 - (index & 0x7))) & 1;
50 }
sizeInBytes()51 int32_t CBC_QRCoderBitVector::sizeInBytes() {
52 return (m_sizeInBits + 7) >> 3;
53 }
Size()54 int32_t CBC_QRCoderBitVector::Size() {
55 return m_sizeInBits;
56 }
AppendBit(int32_t bit,int32_t & e)57 void CBC_QRCoderBitVector::AppendBit(int32_t bit, int32_t& e) {
58 if (!(bit == 0 || bit == 1)) {
59 e = BCExceptionBadValueException;
60 return;
61 }
62 int32_t numBitsInLastByte = m_sizeInBits & 0x7;
63 if (numBitsInLastByte == 0) {
64 AppendByte(0);
65 m_sizeInBits -= 8;
66 }
67 m_array[m_sizeInBits >> 3] |= (bit << (7 - numBitsInLastByte));
68 ++m_sizeInBits;
69 }
AppendBits(int32_t value,int32_t numBits,int32_t & e)70 void CBC_QRCoderBitVector::AppendBits(int32_t value,
71 int32_t numBits,
72 int32_t& e) {
73 if (numBits < 0 || numBits > 32) {
74 e = BCExceptionBadNumBitsException;
75 return;
76 }
77 int32_t numBitsLeft = numBits;
78 while (numBitsLeft > 0) {
79 if ((m_sizeInBits & 0x7) == 0 && numBitsLeft >= 8) {
80 int32_t newByte = (value >> (numBitsLeft - 8)) & 0xff;
81 AppendByte(newByte);
82 numBitsLeft -= 8;
83 } else {
84 int32_t bit = (value >> (numBitsLeft - 1)) & 1;
85 AppendBit(bit, e);
86 if (e != BCExceptionNO)
87 return;
88 --numBitsLeft;
89 }
90 }
91 }
AppendBitVector(CBC_QRCoderBitVector * bits,int32_t & e)92 void CBC_QRCoderBitVector::AppendBitVector(CBC_QRCoderBitVector* bits,
93 int32_t& e) {
94 int32_t size = bits->Size();
95 for (int32_t i = 0; i < size; i++) {
96 int32_t num = bits->At(i, e);
97 if (e != BCExceptionNO)
98 return;
99 AppendBit(num, e);
100 if (e != BCExceptionNO)
101 return;
102 }
103 }
XOR(CBC_QRCoderBitVector * other,int32_t & e)104 void CBC_QRCoderBitVector::XOR(CBC_QRCoderBitVector* other, int32_t& e) {
105 if (m_sizeInBits != other->Size()) {
106 e = BCExceptioncanNotOperatexorOperator;
107 return;
108 }
109 int32_t sizeInBytes = (m_sizeInBits + 7) >> 3;
110 for (int32_t i = 0; i < sizeInBytes; ++i) {
111 m_array[i] ^= (other->GetArray())[i];
112 }
113 }
GetArray()114 uint8_t* CBC_QRCoderBitVector::GetArray() {
115 return m_array;
116 }
AppendByte(int32_t value)117 void CBC_QRCoderBitVector::AppendByte(int32_t value) {
118 if ((m_sizeInBits >> 3) == m_size) {
119 uint8_t* newArray = FX_Alloc(uint8_t, m_size << 1);
120 FXSYS_memcpy(newArray, m_array, m_size);
121 FX_Free(m_array);
122 m_array = newArray;
123 m_size = m_size << 1;
124 }
125 m_array[m_sizeInBits >> 3] = (uint8_t)value;
126 m_sizeInBits += 8;
127 }
128