Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 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 | |
Dan Sinclair | e778668 | 2017-03-29 15:18:41 -0400 | [diff] [blame] | 23 | #include "fxbarcode/qrcode/BC_QRCoderBitVector.h" |
Lei Zhang | b2a4047 | 2017-04-04 16:15:13 -0700 | [diff] [blame] | 24 | |
Lei Zhang | 14e53bd | 2018-12-05 04:52:18 +0000 | [diff] [blame] | 25 | #include "core/fxcrt/fx_system.h" |
| 26 | #include "third_party/base/logging.h" |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 27 | |
Lei Zhang | cb8a4dd | 2018-11-28 01:25:52 +0000 | [diff] [blame] | 28 | CBC_QRCoderBitVector::CBC_QRCoderBitVector() = default; |
Lei Zhang | b2a4047 | 2017-04-04 16:15:13 -0700 | [diff] [blame] | 29 | |
Lei Zhang | cb8a4dd | 2018-11-28 01:25:52 +0000 | [diff] [blame] | 30 | CBC_QRCoderBitVector::~CBC_QRCoderBitVector() = default; |
Lei Zhang | b2a4047 | 2017-04-04 16:15:13 -0700 | [diff] [blame] | 31 | |
Lei Zhang | cb8a4dd | 2018-11-28 01:25:52 +0000 | [diff] [blame] | 32 | int32_t CBC_QRCoderBitVector::At(size_t index) const { |
| 33 | CHECK(index < m_sizeInBits); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 34 | int32_t value = m_array[index >> 3] & 0xff; |
| 35 | return (value >> (7 - (index & 0x7))) & 1; |
| 36 | } |
Lei Zhang | b2a4047 | 2017-04-04 16:15:13 -0700 | [diff] [blame] | 37 | |
| 38 | size_t CBC_QRCoderBitVector::sizeInBytes() const { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 39 | return (m_sizeInBits + 7) >> 3; |
| 40 | } |
Lei Zhang | b2a4047 | 2017-04-04 16:15:13 -0700 | [diff] [blame] | 41 | |
| 42 | size_t CBC_QRCoderBitVector::Size() const { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 43 | return m_sizeInBits; |
| 44 | } |
Lei Zhang | b2a4047 | 2017-04-04 16:15:13 -0700 | [diff] [blame] | 45 | |
| 46 | void CBC_QRCoderBitVector::AppendBit(int32_t bit) { |
| 47 | ASSERT(bit == 0 || bit == 1); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 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 | } |
Lei Zhang | b2a4047 | 2017-04-04 16:15:13 -0700 | [diff] [blame] | 56 | |
| 57 | void CBC_QRCoderBitVector::AppendBits(int32_t value, int32_t numBits) { |
| 58 | ASSERT(numBits > 0); |
| 59 | ASSERT(numBits <= 32); |
| 60 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 61 | int32_t numBitsLeft = numBits; |
| 62 | while (numBitsLeft > 0) { |
| 63 | if ((m_sizeInBits & 0x7) == 0 && numBitsLeft >= 8) { |
Lei Zhang | b2a4047 | 2017-04-04 16:15:13 -0700 | [diff] [blame] | 64 | AppendByte(static_cast<int8_t>((value >> (numBitsLeft - 8)) & 0xff)); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 65 | numBitsLeft -= 8; |
| 66 | } else { |
Lei Zhang | b2a4047 | 2017-04-04 16:15:13 -0700 | [diff] [blame] | 67 | AppendBit((value >> (numBitsLeft - 1)) & 1); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 68 | --numBitsLeft; |
| 69 | } |
| 70 | } |
| 71 | } |
Lei Zhang | b2a4047 | 2017-04-04 16:15:13 -0700 | [diff] [blame] | 72 | |
Lei Zhang | cb8a4dd | 2018-11-28 01:25:52 +0000 | [diff] [blame] | 73 | void CBC_QRCoderBitVector::AppendBitVector(const CBC_QRCoderBitVector* bits) { |
| 74 | for (size_t i = 0; i < bits->Size(); i++) |
| 75 | AppendBit(bits->At(i)); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 76 | } |
Lei Zhang | b2a4047 | 2017-04-04 16:15:13 -0700 | [diff] [blame] | 77 | |
| 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; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 86 | } |
Lei Zhang | b2a4047 | 2017-04-04 16:15:13 -0700 | [diff] [blame] | 87 | |
| 88 | const uint8_t* CBC_QRCoderBitVector::GetArray() const { |
| 89 | return m_array.data(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 90 | } |
Lei Zhang | b2a4047 | 2017-04-04 16:15:13 -0700 | [diff] [blame] | 91 | |
| 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; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 96 | m_sizeInBits += 8; |
| 97 | } |