other-licenses/7zstub/src/7zip/Common/MSBFEncoder.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.

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

mercurial