other-licenses/7zstub/src/7zip/Common/InBuffer.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 // InBuffer.h
     3 #ifndef __INBUFFER_H
     4 #define __INBUFFER_H
     6 #include "../IStream.h"
     7 #include "../../Common/MyCom.h"
     9 #ifndef _NO_EXCEPTIONS
    10 class CInBufferException
    11 {
    12 public:
    13   HRESULT ErrorCode;
    14   CInBufferException(HRESULT errorCode): ErrorCode(errorCode) {}
    15 };
    16 #endif
    18 class CInBuffer
    19 {
    20   Byte *_buffer;
    21   Byte *_bufferLimit;
    22   Byte *_bufferBase;
    23   CMyComPtr<ISequentialInStream> _stream;
    24   UInt64 _processedSize;
    25   UInt32 _bufferSize;
    26   bool _wasFinished;
    28   bool ReadBlock();
    29   Byte ReadBlock2();
    31 public:
    32   #ifdef _NO_EXCEPTIONS
    33   HRESULT ErrorCode;
    34   #endif
    36   CInBuffer();
    37   ~CInBuffer() { Free(); }
    39   bool Create(UInt32 bufferSize);
    40   void Free();
    42   void SetStream(ISequentialInStream *stream);
    43   void Init();
    44   void ReleaseStream() { _stream.Release(); }
    46   bool ReadByte(Byte &b)
    47   {
    48     if(_buffer >= _bufferLimit)
    49       if(!ReadBlock())
    50         return false;
    51     b = *_buffer++;
    52     return true;
    53   }
    54   Byte ReadByte()
    55   {
    56     if(_buffer >= _bufferLimit)
    57       return ReadBlock2();
    58     return *_buffer++;
    59   }
    60   void ReadBytes(void *data, UInt32 size, UInt32 &processedSize)
    61   {
    62     for(processedSize = 0; processedSize < size; processedSize++)
    63       if (!ReadByte(((Byte *)data)[processedSize]))
    64         return;
    65   }
    66   bool ReadBytes(void *data, UInt32 size)
    67   {
    68     UInt32 processedSize;
    69     ReadBytes(data, size, processedSize);
    70     return (processedSize == size);
    71   }
    72   UInt64 GetProcessedSize() const { return _processedSize + (_buffer - _bufferBase); }
    73   bool WasFinished() const { return _wasFinished; }
    74 };
    76 #endif

mercurial