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