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_CommonBitSource.h" CBC_CommonBitSource(CFX_ByteArray * bytes)25CBC_CommonBitSource::CBC_CommonBitSource(CFX_ByteArray* bytes) { 26 m_bytes.Copy((*bytes)); 27 m_bitOffset = 0; 28 m_byteOffset = 0; 29 } ~CBC_CommonBitSource()30CBC_CommonBitSource::~CBC_CommonBitSource() {} ReadBits(int32_t numBits,int32_t & e)31int32_t CBC_CommonBitSource::ReadBits(int32_t numBits, int32_t& e) { 32 if (numBits < 1 || numBits > 32) { 33 e = BCExceptionIllegalArgument; 34 return 0; 35 } 36 int32_t result = 0; 37 if (m_bitOffset > 0) { 38 int32_t bitsLeft = 8 - m_bitOffset; 39 int32_t toRead = numBits < bitsLeft ? numBits : bitsLeft; 40 int32_t bitsToNotRead = bitsLeft - toRead; 41 int32_t mask = (0xff >> (8 - toRead)) << bitsToNotRead; 42 result = (m_bytes[m_byteOffset] & mask) >> bitsToNotRead; 43 numBits -= toRead; 44 m_bitOffset += toRead; 45 if (m_bitOffset == 8) { 46 m_bitOffset = 0; 47 m_byteOffset++; 48 } 49 } 50 if (numBits > 0) { 51 while (numBits >= 8) { 52 result = (result << 8) | (m_bytes[m_byteOffset] & 0xff); 53 m_byteOffset++; 54 numBits -= 8; 55 } 56 if (numBits > 0) { 57 int32_t bitsToNotRead = 8 - numBits; 58 int32_t mask = (0xff >> bitsToNotRead) << bitsToNotRead; 59 result = (result << numBits) | 60 ((m_bytes[m_byteOffset] & mask) >> bitsToNotRead); 61 m_bitOffset += numBits; 62 } 63 } 64 return result; 65 } Available()66int32_t CBC_CommonBitSource::Available() { 67 return 8 * (m_bytes.GetSize() - m_byteOffset) - m_bitOffset; 68 } getByteOffset()69int32_t CBC_CommonBitSource::getByteOffset() { 70 return m_byteOffset; 71 } 72