1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/other-licenses/7zstub/src/7zip/Common/MSBFEncoder.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,59 @@ 1.4 +// Stream/MSBFEncoder.h 1.5 + 1.6 +#ifndef __STREAM_MSBFENCODER_H 1.7 +#define __STREAM_MSBFENCODER_H 1.8 + 1.9 +#include "Common/Defs.h" 1.10 +#include "../IStream.h" 1.11 +#include "OutBuffer.h" 1.12 + 1.13 +namespace NStream { 1.14 +namespace NMSBF { 1.15 + 1.16 +template<class TOutByte> 1.17 +class CEncoder 1.18 +{ 1.19 + TOutByte m_Stream; 1.20 + int m_BitPos; 1.21 + Byte m_CurByte; 1.22 +public: 1.23 + bool Create(UInt32 bufferSize) { return m_Stream.Create(bufferSize); } 1.24 + void SetStream(ISequentialOutStream *outStream) { m_Stream.SetStream(outStream);} 1.25 + void ReleaseStream() { m_Stream.ReleaseStream(); } 1.26 + void Init() 1.27 + { 1.28 + m_Stream.Init(); 1.29 + m_BitPos = 8; 1.30 + m_CurByte = 0; 1.31 + } 1.32 + HRESULT Flush() 1.33 + { 1.34 + if(m_BitPos < 8) 1.35 + WriteBits(0, m_BitPos); 1.36 + return m_Stream.Flush(); 1.37 + } 1.38 + 1.39 + void WriteBits(UInt32 value, int numBits) 1.40 + { 1.41 + while(numBits > 0) 1.42 + { 1.43 + if (numBits < m_BitPos) 1.44 + { 1.45 + m_CurByte |= ((Byte)value << (m_BitPos -= numBits)); 1.46 + return; 1.47 + } 1.48 + numBits -= m_BitPos; 1.49 + UInt32 newBits = (value >> numBits); 1.50 + value -= (newBits << numBits); 1.51 + m_Stream.WriteByte(m_CurByte | (Byte)newBits); 1.52 + m_BitPos = 8; 1.53 + m_CurByte = 0; 1.54 + } 1.55 + } 1.56 + UInt64 GetProcessedSize() const { 1.57 + return m_Stream.GetProcessedSize() + (8 - m_BitPos + 7) / 8; } 1.58 +}; 1.59 + 1.60 +}} 1.61 + 1.62 +#endif