Home | History | Annotate | Download | only in common
      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 #include "fxbarcode/common/BC_CommonByteArray.h"
     23 
     24 #include <algorithm>
     25 
     26 #include "core/fxcrt/fx_memory.h"
     27 
     28 CBC_CommonByteArray::CBC_CommonByteArray() {
     29   m_bytes = nullptr;
     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);
     36   memset(m_bytes, 0, size);
     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);
     42   memcpy(m_bytes, byteArray, size);
     43   m_index = size;
     44 }
     45 CBC_CommonByteArray::~CBC_CommonByteArray() {
     46   FX_Free(m_bytes);
     47 }
     48 int32_t CBC_CommonByteArray::At(int32_t index) const {
     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 }
     54 int32_t CBC_CommonByteArray::Size() const {
     55   return m_size;
     56 }
     57 bool CBC_CommonByteArray::IsEmpty() const {
     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) {
     69   if (!m_bytes || m_size < capacity) {
     70     uint8_t* newArray = FX_Alloc(uint8_t, capacity);
     71     if (m_bytes) {
     72       memcpy(newArray, m_bytes, m_size);
     73       memset(newArray + m_size, 0, capacity - m_size);
     74     } else {
     75       memset(newArray, 0, capacity);
     76     }
     77     FX_Free(m_bytes);
     78     m_bytes = newArray;
     79     m_size = capacity;
     80   }
     81 }
     82 void CBC_CommonByteArray::Set(const uint8_t* source,
     83                               int32_t offset,
     84                               int32_t count) {
     85   FX_Free(m_bytes);
     86   m_bytes = FX_Alloc(uint8_t, count);
     87   m_size = count;
     88   memcpy(m_bytes, source + offset, count);
     89   m_index = count;
     90 }
     91 void CBC_CommonByteArray::Set(std::vector<uint8_t>* source,
     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 }
    103