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.

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

mercurial