other-licenses/7zstub/src/7zip/Common/StreamObjects.cpp

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 // StreamObjects.cpp
     3 #include "StdAfx.h"
     5 #include "StreamObjects.h"
     6 #include "../../Common/Defs.h"
     9 STDMETHODIMP CSequentialInStreamImp::Read(void *data, UInt32 size, UInt32 *processedSize)
    10 {
    11   UInt32 numBytesToRead = (UInt32)(MyMin(_pos + size, _size) - _pos);
    12   memmove(data, _dataPointer + _pos, numBytesToRead);
    13   _pos += numBytesToRead;
    14   if(processedSize != NULL)
    15     *processedSize = numBytesToRead;
    16   return S_OK;
    17 }
    20 void CWriteBuffer::Write(const void *data, size_t size)
    21 {
    22   size_t newCapacity = _size + size;
    23   _buffer.EnsureCapacity(newCapacity);
    24   memmove(_buffer + _size, data, size);
    25   _size += size;
    26 }
    28 STDMETHODIMP CSequentialOutStreamImp::Write(const void *data, UInt32 size, UInt32 *processedSize)
    29 {
    30   _writeBuffer.Write(data, size);
    31   if(processedSize != NULL)
    32     *processedSize = size;
    33   return S_OK; 
    34 }
    36 STDMETHODIMP CSequentialOutStreamImp2::Write(const void *data, UInt32 size, UInt32 *processedSize)
    37 {
    38   UInt32 newSize = size;
    39   if (_pos + size > _size)
    40     newSize = (UInt32)(_size - _pos);
    41   memmove(_buffer + _pos, data, newSize);
    42   if(processedSize != NULL)
    43     *processedSize = newSize;
    44   _pos += newSize;
    45   if (newSize != size)
    46     return E_FAIL;
    47   return S_OK; 
    48 }
    50 STDMETHODIMP CSequentialInStreamSizeCount::Read(void *data, UInt32 size, UInt32 *processedSize)
    51 {
    52   UInt32 realProcessedSize;
    53   HRESULT result = _stream->Read(data, size, &realProcessedSize);
    54   _size += realProcessedSize;
    55   if (processedSize != 0)
    56     *processedSize = realProcessedSize;
    57   return result; 
    58 }
    60 STDMETHODIMP CSequentialInStreamRollback::Read(void *data, UInt32 size, UInt32 *processedSize)
    61 {
    62   if (_currentPos != _currentSize)
    63   {
    64     size_t curSize = _currentSize - _currentPos;
    65     if (size > curSize)
    66       size = (UInt32)curSize;
    67     memmove(data, _buffer + _currentPos, size);
    68     _currentPos += size;
    69     if (processedSize != 0)
    70       *processedSize = size;
    71     return S_OK;
    72   }
    73   UInt32 realProcessedSize;
    74   if (size > _bufferSize)
    75     size = (UInt32)_bufferSize;
    76   HRESULT result = _stream->Read(_buffer, size, &realProcessedSize);
    77   memmove(data, _buffer, realProcessedSize);
    78   _size += realProcessedSize;
    79   _currentSize = realProcessedSize;
    80   _currentPos = realProcessedSize;
    81   if (processedSize != 0)
    82     *processedSize = realProcessedSize;
    83   return result; 
    84 }
    86 HRESULT CSequentialInStreamRollback::Rollback(size_t rollbackSize)
    87 {
    88   if (rollbackSize > _currentPos)
    89     return E_INVALIDARG;
    90   _currentPos -= rollbackSize;
    91   return S_OK;
    92 }
    94 STDMETHODIMP CSequentialOutStreamSizeCount::Write(const void *data, UInt32 size, UInt32 *processedSize)
    95 {
    96   UInt32 realProcessedSize;
    97   HRESULT result = _stream->Write(data, size, &realProcessedSize);
    98   _size += realProcessedSize;
    99   if (processedSize != 0)
   100     *processedSize = realProcessedSize;
   101   return result; 
   102 }

mercurial