OLD | NEW |
| (Empty) |
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/include/fxcrt/fx_memory.h" | |
24 #include "xfa/src/fxbarcode/qrcode/BC_QRCoderBitVector.h" | |
25 #include "xfa/src/fxbarcode/utils.h" | |
26 | |
27 CBC_QRCoderBitVector::CBC_QRCoderBitVector() { | |
28 m_sizeInBits = 0; | |
29 m_size = 32; | |
30 } | |
31 void CBC_QRCoderBitVector::Init() { | |
32 m_array = FX_Alloc(uint8_t, m_size); | |
33 } | |
34 CBC_QRCoderBitVector::~CBC_QRCoderBitVector() { | |
35 FX_Free(m_array); | |
36 } | |
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 } | |
43 int32_t CBC_QRCoderBitVector::At(int32_t index, int32_t& e) { | |
44 if (index < 0 || index >= m_sizeInBits) { | |
45 e = BCExceptionBadIndexException; | |
46 BC_EXCEPTION_CHECK_ReturnValue(e, 0); | |
47 } | |
48 int32_t value = m_array[index >> 3] & 0xff; | |
49 return (value >> (7 - (index & 0x7))) & 1; | |
50 } | |
51 int32_t CBC_QRCoderBitVector::sizeInBytes() { | |
52 return (m_sizeInBits + 7) >> 3; | |
53 } | |
54 int32_t CBC_QRCoderBitVector::Size() { | |
55 return m_sizeInBits; | |
56 } | |
57 void CBC_QRCoderBitVector::AppendBit(int32_t bit, int32_t& e) { | |
58 if (!(bit == 0 || bit == 1)) { | |
59 e = BCExceptionBadValueException; | |
60 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
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 } | |
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 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
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 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
87 --numBitsLeft; | |
88 } | |
89 } | |
90 } | |
91 void CBC_QRCoderBitVector::AppendBitVector(CBC_QRCoderBitVector* bits, | |
92 int32_t& e) { | |
93 int32_t size = bits->Size(); | |
94 for (int32_t i = 0; i < size; i++) { | |
95 int32_t num = bits->At(i, e); | |
96 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
97 AppendBit(num, e); | |
98 BC_EXCEPTION_CHECK_ReturnVoid(e) | |
99 } | |
100 } | |
101 void CBC_QRCoderBitVector::XOR(CBC_QRCoderBitVector* other, int32_t& e) { | |
102 if (m_sizeInBits != other->Size()) { | |
103 e = BCExceptioncanNotOperatexorOperator; | |
104 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
105 } | |
106 int32_t sizeInBytes = (m_sizeInBits + 7) >> 3; | |
107 for (int32_t i = 0; i < sizeInBytes; ++i) { | |
108 m_array[i] ^= (other->GetArray())[i]; | |
109 } | |
110 } | |
111 uint8_t* CBC_QRCoderBitVector::GetArray() { | |
112 return m_array; | |
113 } | |
114 void CBC_QRCoderBitVector::AppendByte(int32_t value) { | |
115 if ((m_sizeInBits >> 3) == m_size) { | |
116 uint8_t* newArray = FX_Alloc(uint8_t, m_size << 1); | |
117 FXSYS_memcpy(newArray, m_array, m_size); | |
118 FX_Free(m_array); | |
119 m_array = newArray; | |
120 m_size = m_size << 1; | |
121 } | |
122 m_array[m_sizeInBits >> 3] = (uint8_t)value; | |
123 m_sizeInBits += 8; | |
124 } | |
OLD | NEW |