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 | // InBuffer.cpp |
michael@0 | 2 | |
michael@0 | 3 | #include "StdAfx.h" |
michael@0 | 4 | |
michael@0 | 5 | #include "InBuffer.h" |
michael@0 | 6 | |
michael@0 | 7 | #include "../../Common/Alloc.h" |
michael@0 | 8 | |
michael@0 | 9 | CInBuffer::CInBuffer(): |
michael@0 | 10 | _buffer(0), |
michael@0 | 11 | _bufferLimit(0), |
michael@0 | 12 | _bufferBase(0), |
michael@0 | 13 | _stream(0), |
michael@0 | 14 | _bufferSize(0) |
michael@0 | 15 | {} |
michael@0 | 16 | |
michael@0 | 17 | bool CInBuffer::Create(UInt32 bufferSize) |
michael@0 | 18 | { |
michael@0 | 19 | const UInt32 kMinBlockSize = 1; |
michael@0 | 20 | if (bufferSize < kMinBlockSize) |
michael@0 | 21 | bufferSize = kMinBlockSize; |
michael@0 | 22 | if (_bufferBase != 0 && _bufferSize == bufferSize) |
michael@0 | 23 | return true; |
michael@0 | 24 | Free(); |
michael@0 | 25 | _bufferSize = bufferSize; |
michael@0 | 26 | _bufferBase = (Byte *)::MidAlloc(bufferSize); |
michael@0 | 27 | return (_bufferBase != 0); |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | void CInBuffer::Free() |
michael@0 | 31 | { |
michael@0 | 32 | ::MidFree(_bufferBase); |
michael@0 | 33 | _bufferBase = 0; |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | void CInBuffer::SetStream(ISequentialInStream *stream) |
michael@0 | 37 | { |
michael@0 | 38 | _stream = stream; |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | void CInBuffer::Init() |
michael@0 | 42 | { |
michael@0 | 43 | _processedSize = 0; |
michael@0 | 44 | _buffer = _bufferBase; |
michael@0 | 45 | _bufferLimit = _buffer; |
michael@0 | 46 | _wasFinished = false; |
michael@0 | 47 | #ifdef _NO_EXCEPTIONS |
michael@0 | 48 | ErrorCode = S_OK; |
michael@0 | 49 | #endif |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | bool CInBuffer::ReadBlock() |
michael@0 | 53 | { |
michael@0 | 54 | #ifdef _NO_EXCEPTIONS |
michael@0 | 55 | if (ErrorCode != S_OK) |
michael@0 | 56 | return false; |
michael@0 | 57 | #endif |
michael@0 | 58 | if (_wasFinished) |
michael@0 | 59 | return false; |
michael@0 | 60 | _processedSize += (_buffer - _bufferBase); |
michael@0 | 61 | UInt32 numProcessedBytes; |
michael@0 | 62 | HRESULT result = _stream->Read(_bufferBase, _bufferSize, &numProcessedBytes); |
michael@0 | 63 | #ifdef _NO_EXCEPTIONS |
michael@0 | 64 | ErrorCode = result; |
michael@0 | 65 | #else |
michael@0 | 66 | if (result != S_OK) |
michael@0 | 67 | throw CInBufferException(result); |
michael@0 | 68 | #endif |
michael@0 | 69 | _buffer = _bufferBase; |
michael@0 | 70 | _bufferLimit = _buffer + numProcessedBytes; |
michael@0 | 71 | _wasFinished = (numProcessedBytes == 0); |
michael@0 | 72 | return (!_wasFinished); |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | Byte CInBuffer::ReadBlock2() |
michael@0 | 76 | { |
michael@0 | 77 | if(!ReadBlock()) |
michael@0 | 78 | return 0xFF; |
michael@0 | 79 | return *_buffer++; |
michael@0 | 80 | } |