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 2008 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 | */ |
Dan Sinclair | bcd1e70 | 2017-08-31 13:19:18 -0400 | [diff] [blame] | 22 | #include "fxbarcode/common/BC_CommonByteArray.h" |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 23 | |
| 24 | #include <algorithm> |
| 25 | |
Dan Sinclair | bcd1e70 | 2017-08-31 13:19:18 -0400 | [diff] [blame] | 26 | #include "core/fxcrt/fx_memory.h" |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 27 | |
| 28 | CBC_CommonByteArray::CBC_CommonByteArray() { |
thestig | 6dc1d77 | 2016-06-09 18:39:33 -0700 | [diff] [blame] | 29 | m_bytes = nullptr; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 30 | m_size = 0; |
| 31 | m_index = 0; |
| 32 | } |
| 33 | CBC_CommonByteArray::CBC_CommonByteArray(int32_t size) { |
| 34 | m_size = size; |
| 35 | m_bytes = FX_Alloc(uint8_t, size); |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 36 | memset(m_bytes, 0, size); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 37 | m_index = 0; |
| 38 | } |
| 39 | CBC_CommonByteArray::CBC_CommonByteArray(uint8_t* byteArray, int32_t size) { |
| 40 | m_size = size; |
| 41 | m_bytes = FX_Alloc(uint8_t, size); |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 42 | memcpy(m_bytes, byteArray, size); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 43 | m_index = size; |
| 44 | } |
| 45 | CBC_CommonByteArray::~CBC_CommonByteArray() { |
| 46 | FX_Free(m_bytes); |
| 47 | } |
weili | e76203d | 2016-08-09 13:45:03 -0700 | [diff] [blame] | 48 | int32_t CBC_CommonByteArray::At(int32_t index) const { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 49 | return m_bytes[index] & 0xff; |
| 50 | } |
| 51 | void CBC_CommonByteArray::Set(int32_t index, int32_t value) { |
| 52 | m_bytes[index] = (uint8_t)value; |
| 53 | } |
weili | e76203d | 2016-08-09 13:45:03 -0700 | [diff] [blame] | 54 | int32_t CBC_CommonByteArray::Size() const { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 55 | return m_size; |
| 56 | } |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 57 | bool CBC_CommonByteArray::IsEmpty() const { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 58 | return m_size == 0; |
| 59 | } |
| 60 | void CBC_CommonByteArray::AppendByte(int32_t value) { |
| 61 | if (m_size == 0 || m_index >= m_size) { |
| 62 | int32_t newSize = std::max(32, m_size << 1); |
| 63 | Reserve(newSize); |
| 64 | } |
| 65 | m_bytes[m_index] = (uint8_t)value; |
| 66 | m_index++; |
| 67 | } |
| 68 | void CBC_CommonByteArray::Reserve(int32_t capacity) { |
thestig | 6dc1d77 | 2016-06-09 18:39:33 -0700 | [diff] [blame] | 69 | if (!m_bytes || m_size < capacity) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 70 | uint8_t* newArray = FX_Alloc(uint8_t, capacity); |
| 71 | if (m_bytes) { |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 72 | memcpy(newArray, m_bytes, m_size); |
| 73 | memset(newArray + m_size, 0, capacity - m_size); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 74 | } else { |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 75 | memset(newArray, 0, capacity); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 76 | } |
| 77 | FX_Free(m_bytes); |
| 78 | m_bytes = newArray; |
| 79 | m_size = capacity; |
| 80 | } |
| 81 | } |
Lei Zhang | b2a4047 | 2017-04-04 16:15:13 -0700 | [diff] [blame] | 82 | void CBC_CommonByteArray::Set(const uint8_t* source, |
| 83 | int32_t offset, |
| 84 | int32_t count) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 85 | FX_Free(m_bytes); |
| 86 | m_bytes = FX_Alloc(uint8_t, count); |
| 87 | m_size = count; |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 88 | memcpy(m_bytes, source + offset, count); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 89 | m_index = count; |
| 90 | } |
Tom Sepez | 8b6186f | 2017-03-28 12:06:45 -0700 | [diff] [blame] | 91 | void CBC_CommonByteArray::Set(std::vector<uint8_t>* source, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 92 | int32_t offset, |
| 93 | int32_t count) { |
| 94 | FX_Free(m_bytes); |
| 95 | m_bytes = FX_Alloc(uint8_t, count); |
| 96 | m_size = count; |
| 97 | int32_t i; |
| 98 | for (i = 0; i < count; i++) { |
| 99 | m_bytes[i] = source->operator[](i + offset); |
| 100 | } |
| 101 | m_index = m_size; |
| 102 | } |