blob: 121fa926cfd9f02590d90f639e01dfeb18ddc828 [file] [log] [blame]
Dan Sinclair1770c022016-03-14 14:14:16 -04001// 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
Dan Sinclaire7786682017-03-29 15:18:41 -040023#include "fxbarcode/qrcode/BC_QRCoderBitVector.h"
Lei Zhangb2a40472017-04-04 16:15:13 -070024
Lei Zhang14e53bd2018-12-05 04:52:18 +000025#include "core/fxcrt/fx_system.h"
26#include "third_party/base/logging.h"
Dan Sinclair1770c022016-03-14 14:14:16 -040027
Lei Zhangcb8a4dd2018-11-28 01:25:52 +000028CBC_QRCoderBitVector::CBC_QRCoderBitVector() = default;
Lei Zhangb2a40472017-04-04 16:15:13 -070029
Lei Zhangcb8a4dd2018-11-28 01:25:52 +000030CBC_QRCoderBitVector::~CBC_QRCoderBitVector() = default;
Lei Zhangb2a40472017-04-04 16:15:13 -070031
Lei Zhangcb8a4dd2018-11-28 01:25:52 +000032int32_t CBC_QRCoderBitVector::At(size_t index) const {
33 CHECK(index < m_sizeInBits);
Dan Sinclair1770c022016-03-14 14:14:16 -040034 int32_t value = m_array[index >> 3] & 0xff;
35 return (value >> (7 - (index & 0x7))) & 1;
36}
Lei Zhangb2a40472017-04-04 16:15:13 -070037
38size_t CBC_QRCoderBitVector::sizeInBytes() const {
Dan Sinclair1770c022016-03-14 14:14:16 -040039 return (m_sizeInBits + 7) >> 3;
40}
Lei Zhangb2a40472017-04-04 16:15:13 -070041
42size_t CBC_QRCoderBitVector::Size() const {
Dan Sinclair1770c022016-03-14 14:14:16 -040043 return m_sizeInBits;
44}
Lei Zhangb2a40472017-04-04 16:15:13 -070045
46void CBC_QRCoderBitVector::AppendBit(int32_t bit) {
47 ASSERT(bit == 0 || bit == 1);
Dan Sinclair1770c022016-03-14 14:14:16 -040048 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}
Lei Zhangb2a40472017-04-04 16:15:13 -070056
57void CBC_QRCoderBitVector::AppendBits(int32_t value, int32_t numBits) {
58 ASSERT(numBits > 0);
59 ASSERT(numBits <= 32);
60
Dan Sinclair1770c022016-03-14 14:14:16 -040061 int32_t numBitsLeft = numBits;
62 while (numBitsLeft > 0) {
63 if ((m_sizeInBits & 0x7) == 0 && numBitsLeft >= 8) {
Lei Zhangb2a40472017-04-04 16:15:13 -070064 AppendByte(static_cast<int8_t>((value >> (numBitsLeft - 8)) & 0xff));
Dan Sinclair1770c022016-03-14 14:14:16 -040065 numBitsLeft -= 8;
66 } else {
Lei Zhangb2a40472017-04-04 16:15:13 -070067 AppendBit((value >> (numBitsLeft - 1)) & 1);
Dan Sinclair1770c022016-03-14 14:14:16 -040068 --numBitsLeft;
69 }
70 }
71}
Lei Zhangb2a40472017-04-04 16:15:13 -070072
Lei Zhangcb8a4dd2018-11-28 01:25:52 +000073void CBC_QRCoderBitVector::AppendBitVector(const CBC_QRCoderBitVector* bits) {
74 for (size_t i = 0; i < bits->Size(); i++)
75 AppendBit(bits->At(i));
Dan Sinclair1770c022016-03-14 14:14:16 -040076}
Lei Zhangb2a40472017-04-04 16:15:13 -070077
78bool 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;
Dan Sinclair1770c022016-03-14 14:14:16 -040086}
Lei Zhangb2a40472017-04-04 16:15:13 -070087
88const uint8_t* CBC_QRCoderBitVector::GetArray() const {
89 return m_array.data();
Dan Sinclair1770c022016-03-14 14:14:16 -040090}
Lei Zhangb2a40472017-04-04 16:15:13 -070091
92void 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;
Dan Sinclair1770c022016-03-14 14:14:16 -040096 m_sizeInBits += 8;
97}