1 // Copyright 2014 The PDFium Authors 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 #ifndef CORE_FXCODEC_JBIG2_JBIG2_BITSTREAM_H_ 8 #define CORE_FXCODEC_JBIG2_JBIG2_BITSTREAM_H_ 9 10 #include "core/fxcrt/raw_span.h" 11 #include "core/fxcrt/retain_ptr.h" 12 #include "core/fxcrt/span.h" 13 14 class CJBig2_BitStream { 15 public: 16 CJBig2_BitStream(pdfium::span<const uint8_t> pSrcStream, uint64_t key); 17 CJBig2_BitStream(const CJBig2_BitStream&) = delete; 18 CJBig2_BitStream& operator=(const CJBig2_BitStream&) = delete; 19 ~CJBig2_BitStream(); 20 21 // TODO(thestig): readFoo() should return bool. 22 int32_t readNBits(uint32_t dwBits, uint32_t* dwResult); 23 int32_t readNBits(uint32_t dwBits, int32_t* nResult); 24 int32_t read1Bit(uint32_t* dwResult); 25 int32_t read1Bit(bool* bResult); 26 int32_t read1Byte(uint8_t* cResult); 27 int32_t readInteger(uint32_t* dwResult); 28 int32_t readShortInteger(uint16_t* wResult); 29 void alignByte(); 30 uint8_t getCurByte() const; 31 void incByteIdx(); 32 uint8_t getCurByte_arith() const; 33 uint8_t getNextByte_arith() const; 34 uint32_t getOffset() const; 35 void setOffset(uint32_t dwOffset); 36 void addOffset(uint32_t dwDelta); 37 uint32_t getBitPos() const; 38 void setBitPos(uint32_t dwBitPos); getBufSpan()39 pdfium::span<const uint8_t> getBufSpan() const { return m_Span; } 40 const uint8_t* getPointer() const; 41 uint32_t getByteLeft() const; getKey()42 uint64_t getKey() const { return m_Key; } 43 bool IsInBounds() const; 44 45 private: 46 void AdvanceBit(); 47 uint32_t LengthInBits() const; 48 49 const pdfium::raw_span<const uint8_t> m_Span; 50 uint32_t m_dwByteIdx = 0; // Must always be <= `m_Span.size()`. 51 uint32_t m_dwBitIdx = 0; // Must Always be in [0..7]. 52 const uint64_t m_Key; 53 }; 54 55 #endif // CORE_FXCODEC_JBIG2_JBIG2_BITSTREAM_H_ 56