other-licenses/7zstub/src/7zip/Common/MSBFDecoder.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 // MSBFDecoder.h
michael@0 2 // the Most Significant Bit of byte is First
michael@0 3
michael@0 4 #ifndef __STREAM_MSBFDECODER_H
michael@0 5 #define __STREAM_MSBFDECODER_H
michael@0 6
michael@0 7 #include "../../Common/Types.h"
michael@0 8 #include "../IStream.h"
michael@0 9
michael@0 10 namespace NStream {
michael@0 11 namespace NMSBF {
michael@0 12
michael@0 13 const int kNumBigValueBits = 8 * 4;
michael@0 14 const int kNumValueBytes = 3;
michael@0 15 const int kNumValueBits = 8 * kNumValueBytes;
michael@0 16
michael@0 17 const UInt32 kMask = (1 << kNumValueBits) - 1;
michael@0 18
michael@0 19 template<class TInByte>
michael@0 20 class CDecoder
michael@0 21 {
michael@0 22 UInt32 m_BitPos;
michael@0 23 UInt32 m_Value;
michael@0 24 public:
michael@0 25 TInByte m_Stream;
michael@0 26 bool Create(UInt32 bufferSize) { return m_Stream.Create(bufferSize); }
michael@0 27 void SetStream(ISequentialInStream *inStream) { m_Stream.SetStream(inStream);}
michael@0 28 void ReleaseStream() { m_Stream.ReleaseStream();}
michael@0 29
michael@0 30 void Init()
michael@0 31 {
michael@0 32 m_Stream.Init();
michael@0 33 m_BitPos = kNumBigValueBits;
michael@0 34 Normalize();
michael@0 35 }
michael@0 36
michael@0 37 UInt64 GetProcessedSize() const
michael@0 38 { return m_Stream.GetProcessedSize() - (kNumBigValueBits - m_BitPos) / 8; }
michael@0 39 UInt32 GetBitPosition() const { return (m_BitPos & 7); }
michael@0 40
michael@0 41 void Normalize()
michael@0 42 {
michael@0 43 for (;m_BitPos >= 8; m_BitPos -= 8)
michael@0 44 m_Value = (m_Value << 8) | m_Stream.ReadByte();
michael@0 45 }
michael@0 46
michael@0 47 UInt32 GetValue(UInt32 numBits) const
michael@0 48 {
michael@0 49 // return (m_Value << m_BitPos) >> (kNumBigValueBits - numBits);
michael@0 50 return ((m_Value >> (8 - m_BitPos)) & kMask) >> (kNumValueBits - numBits);
michael@0 51 }
michael@0 52
michael@0 53 void MovePos(UInt32 numBits)
michael@0 54 {
michael@0 55 m_BitPos += numBits;
michael@0 56 Normalize();
michael@0 57 }
michael@0 58
michael@0 59 UInt32 ReadBits(UInt32 numBits)
michael@0 60 {
michael@0 61 UInt32 res = GetValue(numBits);
michael@0 62 MovePos(numBits);
michael@0 63 return res;
michael@0 64 }
michael@0 65 };
michael@0 66
michael@0 67 }}
michael@0 68
michael@0 69 #endif

mercurial