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.
michael@0 | 1 | // Stream/MSBFEncoder.h |
michael@0 | 2 | |
michael@0 | 3 | #ifndef __STREAM_MSBFENCODER_H |
michael@0 | 4 | #define __STREAM_MSBFENCODER_H |
michael@0 | 5 | |
michael@0 | 6 | #include "Common/Defs.h" |
michael@0 | 7 | #include "../IStream.h" |
michael@0 | 8 | #include "OutBuffer.h" |
michael@0 | 9 | |
michael@0 | 10 | namespace NStream { |
michael@0 | 11 | namespace NMSBF { |
michael@0 | 12 | |
michael@0 | 13 | template<class TOutByte> |
michael@0 | 14 | class CEncoder |
michael@0 | 15 | { |
michael@0 | 16 | TOutByte m_Stream; |
michael@0 | 17 | int m_BitPos; |
michael@0 | 18 | Byte m_CurByte; |
michael@0 | 19 | public: |
michael@0 | 20 | bool Create(UInt32 bufferSize) { return m_Stream.Create(bufferSize); } |
michael@0 | 21 | void SetStream(ISequentialOutStream *outStream) { m_Stream.SetStream(outStream);} |
michael@0 | 22 | void ReleaseStream() { m_Stream.ReleaseStream(); } |
michael@0 | 23 | void Init() |
michael@0 | 24 | { |
michael@0 | 25 | m_Stream.Init(); |
michael@0 | 26 | m_BitPos = 8; |
michael@0 | 27 | m_CurByte = 0; |
michael@0 | 28 | } |
michael@0 | 29 | HRESULT Flush() |
michael@0 | 30 | { |
michael@0 | 31 | if(m_BitPos < 8) |
michael@0 | 32 | WriteBits(0, m_BitPos); |
michael@0 | 33 | return m_Stream.Flush(); |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | void WriteBits(UInt32 value, int numBits) |
michael@0 | 37 | { |
michael@0 | 38 | while(numBits > 0) |
michael@0 | 39 | { |
michael@0 | 40 | if (numBits < m_BitPos) |
michael@0 | 41 | { |
michael@0 | 42 | m_CurByte |= ((Byte)value << (m_BitPos -= numBits)); |
michael@0 | 43 | return; |
michael@0 | 44 | } |
michael@0 | 45 | numBits -= m_BitPos; |
michael@0 | 46 | UInt32 newBits = (value >> numBits); |
michael@0 | 47 | value -= (newBits << numBits); |
michael@0 | 48 | m_Stream.WriteByte(m_CurByte | (Byte)newBits); |
michael@0 | 49 | m_BitPos = 8; |
michael@0 | 50 | m_CurByte = 0; |
michael@0 | 51 | } |
michael@0 | 52 | } |
michael@0 | 53 | UInt64 GetProcessedSize() const { |
michael@0 | 54 | return m_Stream.GetProcessedSize() + (8 - m_BitPos + 7) / 8; } |
michael@0 | 55 | }; |
michael@0 | 56 | |
michael@0 | 57 | }} |
michael@0 | 58 | |
michael@0 | 59 | #endif |