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 
23 #include "xfa/src/fxbarcode/barcode.h"
24 #include "BC_CommonBitArray.h"
CBC_CommonBitArray(CBC_CommonBitArray * array)25 CBC_CommonBitArray::CBC_CommonBitArray(CBC_CommonBitArray* array) {
26   m_size = array->GetSize();
27   m_bits.Copy(array->GetBits());
28 }
CBC_CommonBitArray()29 CBC_CommonBitArray::CBC_CommonBitArray() {
30   m_bits.SetSize(1);
31   m_size = 0;
32 }
CBC_CommonBitArray(int32_t size)33 CBC_CommonBitArray::CBC_CommonBitArray(int32_t size) {
34   m_bits.SetSize((size + 31) >> 5);
35   m_size = size;
36 }
~CBC_CommonBitArray()37 CBC_CommonBitArray::~CBC_CommonBitArray() {
38   m_size = 0;
39 }
GetSize()40 int32_t CBC_CommonBitArray::GetSize() {
41   return m_size;
42 }
GetBits()43 CFX_Int32Array& CBC_CommonBitArray::GetBits() {
44   return m_bits;
45 }
GetSizeInBytes()46 int32_t CBC_CommonBitArray::GetSizeInBytes() {
47   return (m_size + 7) >> 3;
48 }
Get(int32_t i)49 FX_BOOL CBC_CommonBitArray::Get(int32_t i) {
50   return (m_bits[i >> 5] & (1 << (i & 0x1f))) != 0;
51 }
Set(int32_t i)52 void CBC_CommonBitArray::Set(int32_t i) {
53   m_bits[i >> 5] |= 1 << (i & 0x1F);
54 }
Flip(int32_t i)55 void CBC_CommonBitArray::Flip(int32_t i) {
56   m_bits[i >> 5] ^= 1 << (i & 0x1F);
57 }
SetBulk(int32_t i,int32_t newBits)58 void CBC_CommonBitArray::SetBulk(int32_t i, int32_t newBits) {
59   m_bits[i >> 5] = newBits;
60 }
Clear()61 void CBC_CommonBitArray::Clear() {
62   FXSYS_memset(&m_bits[0], 0x00, m_bits.GetSize() * sizeof(int32_t));
63 }
IsRange(int32_t start,int32_t end,FX_BOOL value,int32_t & e)64 FX_BOOL CBC_CommonBitArray::IsRange(int32_t start,
65                                     int32_t end,
66                                     FX_BOOL value,
67                                     int32_t& e) {
68   if (end < start) {
69     e = BCExceptionEndLessThanStart;
70     return FALSE;
71   }
72   if (end == start) {
73     return TRUE;
74   }
75   end--;
76   int32_t firstInt = start >> 5;
77   int32_t lastInt = end >> 5;
78   int32_t i;
79   for (i = firstInt; i <= lastInt; i++) {
80     int32_t firstBit = i > firstInt ? 0 : start & 0x1F;
81     int32_t lastBit = i < lastInt ? 31 : end & 0x1F;
82     int32_t mask;
83     if (firstBit == 0 && lastBit == 31) {
84       mask = -1;
85     } else {
86       mask = 0;
87       for (int32_t j = firstBit; j <= lastBit; j++) {
88         mask |= 1 << j;
89       }
90     }
91     if ((m_bits[i] & mask) != (value ? mask : 0)) {
92       return FALSE;
93     }
94   }
95   return TRUE;
96 }
GetBitArray()97 int32_t* CBC_CommonBitArray::GetBitArray() {
98   return &m_bits[0];
99 }
Reverse()100 void CBC_CommonBitArray::Reverse() {
101   int32_t* newBits = FX_Alloc(int32_t, m_bits.GetSize());
102   FXSYS_memset(newBits, 0x00, m_bits.GetSize() * sizeof(int32_t));
103   int32_t size = m_size;
104   int32_t i;
105   for (i = 0; i < size; i++) {
106     if (Get(size - i - 1)) {
107       newBits[i >> 5] |= 1 << (i & 0x1F);
108     }
109   }
110   FXSYS_memcpy(&m_bits[0], newBits, m_bits.GetSize() * sizeof(int32_t));
111   FX_Free(newBits);
112 }
113