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 */
22
23#include <algorithm>
24
25#include "xfa/src/fxbarcode/barcode.h"
26#include "BC_CommonByteArray.h"
27CBC_CommonByteArray::CBC_CommonByteArray() {
28  m_bytes = NULL;
29  m_size = 0;
30  m_index = 0;
31}
32CBC_CommonByteArray::CBC_CommonByteArray(int32_t size) {
33  m_size = size;
34  m_bytes = FX_Alloc(uint8_t, size);
35  FXSYS_memset(m_bytes, 0, size);
36  m_index = 0;
37}
38CBC_CommonByteArray::CBC_CommonByteArray(uint8_t* byteArray, int32_t size) {
39  m_size = size;
40  m_bytes = FX_Alloc(uint8_t, size);
41  FXSYS_memcpy(m_bytes, byteArray, size);
42  m_index = size;
43}
44CBC_CommonByteArray::~CBC_CommonByteArray() {
45  if (m_bytes != NULL) {
46    FX_Free(m_bytes);
47    m_bytes = NULL;
48  }
49  m_index = 0;
50  m_size = 0;
51}
52int32_t CBC_CommonByteArray::At(int32_t index) {
53  return m_bytes[index] & 0xff;
54}
55void CBC_CommonByteArray::Set(int32_t index, int32_t value) {
56  m_bytes[index] = (uint8_t)value;
57}
58int32_t CBC_CommonByteArray::Size() {
59  return m_size;
60}
61FX_BOOL CBC_CommonByteArray::IsEmpty() {
62  return m_size == 0;
63}
64void CBC_CommonByteArray::AppendByte(int32_t value) {
65  if (m_size == 0 || m_index >= m_size) {
66    int32_t newSize = std::max(32, m_size << 1);
67    Reserve(newSize);
68  }
69  m_bytes[m_index] = (uint8_t)value;
70  m_index++;
71}
72void CBC_CommonByteArray::Reserve(int32_t capacity) {
73  if (m_bytes == NULL || m_size < capacity) {
74    uint8_t* newArray = FX_Alloc(uint8_t, capacity);
75    FXSYS_memset(newArray, 0, capacity);
76    if (m_bytes != NULL) {
77      FXSYS_memcpy(newArray, m_bytes, m_size);
78      FX_Free(m_bytes);
79    }
80    m_bytes = newArray;
81    m_size = capacity;
82  }
83}
84void CBC_CommonByteArray::Set(uint8_t* source, int32_t offset, int32_t count) {
85  if (m_bytes != NULL) {
86    FX_Free(m_bytes);
87  }
88  m_bytes = FX_Alloc(uint8_t, count);
89  m_size = count;
90  FXSYS_memcpy(m_bytes, source + offset, count);
91  m_index = count;
92}
93void CBC_CommonByteArray::Set(CFX_ByteArray* source,
94                              int32_t offset,
95                              int32_t count) {
96  if (m_bytes != NULL) {
97    FX_Free(m_bytes);
98  }
99  m_bytes = FX_Alloc(uint8_t, count);
100  m_size = count;
101  int32_t i;
102  for (i = 0; i < count; i++) {
103    m_bytes[i] = source->operator[](i + offset);
104  }
105  m_index = m_size;
106}
107