OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 |
| 7 #include "JBig2_BitStream.h" |
| 8 |
| 9 #include <algorithm> |
| 10 |
| 11 CJBig2_BitStream::CJBig2_BitStream(const uint8_t* pBuffer, FX_DWORD dwLength) |
| 12 : m_pBuf(pBuffer), m_dwLength(dwLength), m_dwByteIdx(0), m_dwBitIdx(0) { |
| 13 if (m_dwLength > 256 * 1024 * 1024) { |
| 14 m_dwLength = 0; |
| 15 m_pBuf = nullptr; |
| 16 } |
| 17 } |
| 18 |
| 19 CJBig2_BitStream::~CJBig2_BitStream() { |
| 20 } |
| 21 |
| 22 int32_t CJBig2_BitStream::readNBits(FX_DWORD dwBits, FX_DWORD* dwResult) { |
| 23 FX_DWORD dwBitPos = getBitPos(); |
| 24 if (dwBitPos > LengthInBits()) |
| 25 return -1; |
| 26 |
| 27 *dwResult = 0; |
| 28 if (dwBitPos + dwBits <= LengthInBits()) |
| 29 dwBitPos = dwBits; |
| 30 else |
| 31 dwBitPos = LengthInBits() - dwBitPos; |
| 32 |
| 33 for (; dwBitPos > 0; --dwBitPos) { |
| 34 *dwResult = |
| 35 (*dwResult << 1) | ((m_pBuf[m_dwByteIdx] >> (7 - m_dwBitIdx)) & 0x01); |
| 36 AdvanceBit(); |
| 37 } |
| 38 return 0; |
| 39 } |
| 40 |
| 41 int32_t CJBig2_BitStream::readNBits(FX_DWORD dwBits, int32_t* nResult) { |
| 42 FX_DWORD dwBitPos = getBitPos(); |
| 43 if (dwBitPos > LengthInBits()) |
| 44 return -1; |
| 45 |
| 46 *nResult = 0; |
| 47 if (dwBitPos + dwBits <= LengthInBits()) |
| 48 dwBitPos = dwBits; |
| 49 else |
| 50 dwBitPos = LengthInBits() - dwBitPos; |
| 51 |
| 52 for (; dwBitPos > 0; --dwBitPos) { |
| 53 *nResult = |
| 54 (*nResult << 1) | ((m_pBuf[m_dwByteIdx] >> (7 - m_dwBitIdx)) & 0x01); |
| 55 AdvanceBit(); |
| 56 } |
| 57 return 0; |
| 58 } |
| 59 |
| 60 int32_t CJBig2_BitStream::read1Bit(FX_DWORD* dwResult) { |
| 61 if (!IsInBound()) |
| 62 return -1; |
| 63 |
| 64 *dwResult = (m_pBuf[m_dwByteIdx] >> (7 - m_dwBitIdx)) & 0x01; |
| 65 AdvanceBit(); |
| 66 return 0; |
| 67 } |
| 68 |
| 69 int32_t CJBig2_BitStream::read1Bit(FX_BOOL* bResult) { |
| 70 if (!IsInBound()) |
| 71 return -1; |
| 72 |
| 73 *bResult = (m_pBuf[m_dwByteIdx] >> (7 - m_dwBitIdx)) & 0x01; |
| 74 AdvanceBit(); |
| 75 return 0; |
| 76 } |
| 77 |
| 78 int32_t CJBig2_BitStream::read1Byte(uint8_t* cResult) { |
| 79 if (!IsInBound()) |
| 80 return -1; |
| 81 |
| 82 *cResult = m_pBuf[m_dwByteIdx]; |
| 83 ++m_dwByteIdx; |
| 84 return 0; |
| 85 } |
| 86 |
| 87 int32_t CJBig2_BitStream::readInteger(FX_DWORD* dwResult) { |
| 88 if (m_dwByteIdx + 3 >= m_dwLength) |
| 89 return -1; |
| 90 |
| 91 *dwResult = (m_pBuf[m_dwByteIdx] << 24) | (m_pBuf[m_dwByteIdx + 1] << 16) | |
| 92 (m_pBuf[m_dwByteIdx + 2] << 8) | m_pBuf[m_dwByteIdx + 3]; |
| 93 m_dwByteIdx += 4; |
| 94 return 0; |
| 95 } |
| 96 |
| 97 int32_t CJBig2_BitStream::readShortInteger(FX_WORD* dwResult) { |
| 98 if (m_dwByteIdx + 1 >= m_dwLength) |
| 99 return -1; |
| 100 |
| 101 *dwResult = (m_pBuf[m_dwByteIdx] << 8) | m_pBuf[m_dwByteIdx + 1]; |
| 102 m_dwByteIdx += 2; |
| 103 return 0; |
| 104 } |
| 105 |
| 106 void CJBig2_BitStream::alignByte() { |
| 107 if (m_dwBitIdx != 0) { |
| 108 ++m_dwByteIdx; |
| 109 m_dwBitIdx = 0; |
| 110 } |
| 111 } |
| 112 |
| 113 uint8_t CJBig2_BitStream::getCurByte() const { |
| 114 return IsInBound() ? m_pBuf[m_dwByteIdx] : 0; |
| 115 } |
| 116 |
| 117 void CJBig2_BitStream::incByteIdx() { |
| 118 if (IsInBound()) |
| 119 ++m_dwByteIdx; |
| 120 } |
| 121 |
| 122 uint8_t CJBig2_BitStream::getCurByte_arith() const { |
| 123 return IsInBound() ? m_pBuf[m_dwByteIdx] : 0xFF; |
| 124 } |
| 125 |
| 126 uint8_t CJBig2_BitStream::getNextByte_arith() const { |
| 127 return m_dwByteIdx + 1 < m_dwLength ? m_pBuf[m_dwByteIdx + 1] : 0xFF; |
| 128 } |
| 129 |
| 130 FX_DWORD CJBig2_BitStream::getOffset() const { |
| 131 return m_dwByteIdx; |
| 132 } |
| 133 |
| 134 void CJBig2_BitStream::setOffset(FX_DWORD dwOffset) { |
| 135 m_dwByteIdx = std::min(dwOffset, m_dwLength); |
| 136 } |
| 137 |
| 138 FX_DWORD CJBig2_BitStream::getBitPos() const { |
| 139 return (m_dwByteIdx << 3) + m_dwBitIdx; |
| 140 } |
| 141 |
| 142 void CJBig2_BitStream::setBitPos(FX_DWORD dwBitPos) { |
| 143 m_dwByteIdx = dwBitPos >> 3; |
| 144 m_dwBitIdx = dwBitPos & 7; |
| 145 } |
| 146 |
| 147 const uint8_t* CJBig2_BitStream::getBuf() const { |
| 148 return m_pBuf; |
| 149 } |
| 150 |
| 151 const uint8_t* CJBig2_BitStream::getPointer() const { |
| 152 return m_pBuf + m_dwByteIdx; |
| 153 } |
| 154 |
| 155 void CJBig2_BitStream::offset(FX_DWORD dwOffset) { |
| 156 m_dwByteIdx += dwOffset; |
| 157 } |
| 158 |
| 159 FX_DWORD CJBig2_BitStream::getByteLeft() const { |
| 160 return m_dwLength - m_dwByteIdx; |
| 161 } |
| 162 |
| 163 void CJBig2_BitStream::AdvanceBit() { |
| 164 if (m_dwBitIdx == 7) { |
| 165 ++m_dwByteIdx; |
| 166 m_dwBitIdx = 0; |
| 167 } else { |
| 168 ++m_dwBitIdx; |
| 169 } |
| 170 } |
| 171 |
| 172 bool CJBig2_BitStream::IsInBound() const { |
| 173 return m_dwByteIdx < m_dwLength; |
| 174 } |
| 175 |
| 176 FX_DWORD CJBig2_BitStream::LengthInBits() const { |
| 177 return m_dwLength << 3; |
| 178 } |
OLD | NEW |