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