diff options
Diffstat (limited to 'core/fxcrt')
-rw-r--r-- | core/fxcrt/cfx_bitstream.cpp | 57 | ||||
-rw-r--r-- | core/fxcrt/cfx_bitstream.h | 38 | ||||
-rw-r--r-- | core/fxcrt/fx_basic.h | 20 | ||||
-rw-r--r-- | core/fxcrt/fx_basic_buffer.cpp | 44 |
4 files changed, 95 insertions, 64 deletions
diff --git a/core/fxcrt/cfx_bitstream.cpp b/core/fxcrt/cfx_bitstream.cpp new file mode 100644 index 0000000000..7cf6d36adf --- /dev/null +++ b/core/fxcrt/cfx_bitstream.cpp @@ -0,0 +1,57 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "core/fxcrt/cfx_bitstream.h" + +#include <limits> + +#include "core/fxcrt/fx_system.h" + +CFX_BitStream::CFX_BitStream(const uint8_t* pData, uint32_t dwSize) + : m_BitPos(0), m_BitSize(dwSize * 8), m_pData(pData) { + ASSERT(dwSize <= std::numeric_limits<uint32_t>::max() / 8); +} + +CFX_BitStream::~CFX_BitStream() {} + +void CFX_BitStream::ByteAlign() { + m_BitPos = (m_BitPos + 7) & ~7; +} + +uint32_t CFX_BitStream::GetBits(uint32_t nBits) { + if (nBits > m_BitSize || m_BitPos + nBits > m_BitSize) + return 0; + + const uint8_t* data = m_pData.Get(); + + if (nBits == 1) { + int bit = (data[m_BitPos / 8] & (1 << (7 - m_BitPos % 8))) ? 1 : 0; + m_BitPos++; + return bit; + } + + uint32_t byte_pos = m_BitPos / 8; + uint32_t bit_pos = m_BitPos % 8; + uint32_t bit_left = nBits; + uint32_t result = 0; + if (bit_pos) { + if (8 - bit_pos >= bit_left) { + result = (data[byte_pos] & (0xff >> bit_pos)) >> (8 - bit_pos - bit_left); + m_BitPos += bit_left; + return result; + } + bit_left -= 8 - bit_pos; + result = (data[byte_pos++] & ((1 << (8 - bit_pos)) - 1)) << bit_left; + } + while (bit_left >= 8) { + bit_left -= 8; + result |= data[byte_pos++] << bit_left; + } + if (bit_left) + result |= data[byte_pos] >> (8 - bit_left); + m_BitPos += nBits; + return result; +} diff --git a/core/fxcrt/cfx_bitstream.h b/core/fxcrt/cfx_bitstream.h new file mode 100644 index 0000000000..1829f23b92 --- /dev/null +++ b/core/fxcrt/cfx_bitstream.h @@ -0,0 +1,38 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef CORE_FXCRT_CFX_BITSTREAM_H_ +#define CORE_FXCRT_CFX_BITSTREAM_H_ + +#include <stdint.h> + +#include "core/fxcrt/cfx_unowned_ptr.h" + +class CFX_BitStream { + public: + CFX_BitStream(const uint8_t* pData, uint32_t dwSize); + ~CFX_BitStream(); + + void ByteAlign(); + + bool IsEOF() const { return m_BitPos >= m_BitSize; } + uint32_t GetPos() const { return m_BitPos; } + uint32_t GetBits(uint32_t nBits); + + void SkipBits(uint32_t nBits) { m_BitPos += nBits; } + void Rewind() { m_BitPos = 0; } + + uint32_t BitsRemaining() const { + return m_BitSize >= m_BitPos ? m_BitSize - m_BitPos : 0; + } + + private: + uint32_t m_BitPos; + uint32_t m_BitSize; + CFX_UnownedPtr<const uint8_t> m_pData; +}; + +#endif // CORE_FXCRT_CFX_BITSTREAM_H_ diff --git a/core/fxcrt/fx_basic.h b/core/fxcrt/fx_basic.h index 1f05dfb627..167ef5ce70 100644 --- a/core/fxcrt/fx_basic.h +++ b/core/fxcrt/fx_basic.h @@ -136,26 +136,6 @@ class CFX_FixedBufGrow { std::unique_ptr<DataType, FxFreeDeleter> m_pGrowData; }; -class CFX_BitStream { - public: - void Init(const uint8_t* pData, uint32_t dwSize); - - void ByteAlign(); - bool IsEOF() const { return m_BitPos >= m_BitSize; } - uint32_t GetBits(uint32_t nBits); - void SkipBits(uint32_t nBits) { m_BitPos += nBits; } - void Rewind() { m_BitPos = 0; } - uint32_t GetPos() const { return m_BitPos; } - uint32_t BitsRemaining() const { - return m_BitSize >= m_BitPos ? m_BitSize - m_BitPos : 0; - } - - private: - uint32_t m_BitPos; - uint32_t m_BitSize; - const uint8_t* m_pData; -}; - class IFX_Pause { public: virtual ~IFX_Pause() {} diff --git a/core/fxcrt/fx_basic_buffer.cpp b/core/fxcrt/fx_basic_buffer.cpp index 310aec7faf..b93c1bc9da 100644 --- a/core/fxcrt/fx_basic_buffer.cpp +++ b/core/fxcrt/fx_basic_buffer.cpp @@ -145,47 +145,3 @@ CFX_WideTextBuf& CFX_WideTextBuf::operator<<(const CFX_WideTextBuf& buf) { AppendBlock(buf.m_pBuffer.get(), buf.m_DataSize); return *this; } - -void CFX_BitStream::Init(const uint8_t* pData, uint32_t dwSize) { - m_pData = pData; - m_BitSize = dwSize * 8; - m_BitPos = 0; -} - -void CFX_BitStream::ByteAlign() { - m_BitPos = (m_BitPos + 7) & ~7; -} - -uint32_t CFX_BitStream::GetBits(uint32_t nBits) { - if (nBits > m_BitSize || m_BitPos + nBits > m_BitSize) - return 0; - - if (nBits == 1) { - int bit = (m_pData[m_BitPos / 8] & (1 << (7 - m_BitPos % 8))) ? 1 : 0; - m_BitPos++; - return bit; - } - - uint32_t byte_pos = m_BitPos / 8; - uint32_t bit_pos = m_BitPos % 8; - uint32_t bit_left = nBits; - uint32_t result = 0; - if (bit_pos) { - if (8 - bit_pos >= bit_left) { - result = - (m_pData[byte_pos] & (0xff >> bit_pos)) >> (8 - bit_pos - bit_left); - m_BitPos += bit_left; - return result; - } - bit_left -= 8 - bit_pos; - result = (m_pData[byte_pos++] & ((1 << (8 - bit_pos)) - 1)) << bit_left; - } - while (bit_left >= 8) { - bit_left -= 8; - result |= m_pData[byte_pos++] << bit_left; - } - if (bit_left) - result |= m_pData[byte_pos] >> (8 - bit_left); - m_BitPos += nBits; - return result; -} |