blob: d66568c59637d96bc5d1a77ca5b216432310f734 [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 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 Sinclairbcd1e702017-08-31 13:19:18 -040022#include "fxbarcode/common/BC_CommonByteArray.h"
Dan Sinclair1770c022016-03-14 14:14:16 -040023
24#include <algorithm>
25
Dan Sinclairbcd1e702017-08-31 13:19:18 -040026#include "core/fxcrt/fx_memory.h"
Dan Sinclair1770c022016-03-14 14:14:16 -040027
28CBC_CommonByteArray::CBC_CommonByteArray() {
thestig6dc1d772016-06-09 18:39:33 -070029 m_bytes = nullptr;
Dan Sinclair1770c022016-03-14 14:14:16 -040030 m_size = 0;
31 m_index = 0;
32}
33CBC_CommonByteArray::CBC_CommonByteArray(int32_t size) {
34 m_size = size;
35 m_bytes = FX_Alloc(uint8_t, size);
Dan Sinclair1c5d0b42017-04-03 15:05:11 -040036 memset(m_bytes, 0, size);
Dan Sinclair1770c022016-03-14 14:14:16 -040037 m_index = 0;
38}
39CBC_CommonByteArray::CBC_CommonByteArray(uint8_t* byteArray, int32_t size) {
40 m_size = size;
41 m_bytes = FX_Alloc(uint8_t, size);
Dan Sinclair1c5d0b42017-04-03 15:05:11 -040042 memcpy(m_bytes, byteArray, size);
Dan Sinclair1770c022016-03-14 14:14:16 -040043 m_index = size;
44}
45CBC_CommonByteArray::~CBC_CommonByteArray() {
46 FX_Free(m_bytes);
47}
weilie76203d2016-08-09 13:45:03 -070048int32_t CBC_CommonByteArray::At(int32_t index) const {
Dan Sinclair1770c022016-03-14 14:14:16 -040049 return m_bytes[index] & 0xff;
50}
51void CBC_CommonByteArray::Set(int32_t index, int32_t value) {
52 m_bytes[index] = (uint8_t)value;
53}
weilie76203d2016-08-09 13:45:03 -070054int32_t CBC_CommonByteArray::Size() const {
Dan Sinclair1770c022016-03-14 14:14:16 -040055 return m_size;
56}
tsepezd19e9122016-11-02 15:43:18 -070057bool CBC_CommonByteArray::IsEmpty() const {
Dan Sinclair1770c022016-03-14 14:14:16 -040058 return m_size == 0;
59}
60void 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}
68void CBC_CommonByteArray::Reserve(int32_t capacity) {
thestig6dc1d772016-06-09 18:39:33 -070069 if (!m_bytes || m_size < capacity) {
Dan Sinclair1770c022016-03-14 14:14:16 -040070 uint8_t* newArray = FX_Alloc(uint8_t, capacity);
71 if (m_bytes) {
Dan Sinclair1c5d0b42017-04-03 15:05:11 -040072 memcpy(newArray, m_bytes, m_size);
73 memset(newArray + m_size, 0, capacity - m_size);
Dan Sinclair1770c022016-03-14 14:14:16 -040074 } else {
Dan Sinclair1c5d0b42017-04-03 15:05:11 -040075 memset(newArray, 0, capacity);
Dan Sinclair1770c022016-03-14 14:14:16 -040076 }
77 FX_Free(m_bytes);
78 m_bytes = newArray;
79 m_size = capacity;
80 }
81}
Lei Zhangb2a40472017-04-04 16:15:13 -070082void CBC_CommonByteArray::Set(const uint8_t* source,
83 int32_t offset,
84 int32_t count) {
Dan Sinclair1770c022016-03-14 14:14:16 -040085 FX_Free(m_bytes);
86 m_bytes = FX_Alloc(uint8_t, count);
87 m_size = count;
Dan Sinclair1c5d0b42017-04-03 15:05:11 -040088 memcpy(m_bytes, source + offset, count);
Dan Sinclair1770c022016-03-14 14:14:16 -040089 m_index = count;
90}
Tom Sepez8b6186f2017-03-28 12:06:45 -070091void CBC_CommonByteArray::Set(std::vector<uint8_t>* source,
Dan Sinclair1770c022016-03-14 14:14:16 -040092 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}