Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // OutByte.cpp |
michael@0 | 2 | |
michael@0 | 3 | #include "StdAfx.h" |
michael@0 | 4 | |
michael@0 | 5 | #include "OutBuffer.h" |
michael@0 | 6 | |
michael@0 | 7 | #include "../../Common/Alloc.h" |
michael@0 | 8 | |
michael@0 | 9 | bool COutBuffer::Create(UInt32 bufferSize) |
michael@0 | 10 | { |
michael@0 | 11 | const UInt32 kMinBlockSize = 1; |
michael@0 | 12 | if (bufferSize < kMinBlockSize) |
michael@0 | 13 | bufferSize = kMinBlockSize; |
michael@0 | 14 | if (_buffer != 0 && _bufferSize == bufferSize) |
michael@0 | 15 | return true; |
michael@0 | 16 | Free(); |
michael@0 | 17 | _bufferSize = bufferSize; |
michael@0 | 18 | _buffer = (Byte *)::MidAlloc(bufferSize); |
michael@0 | 19 | return (_buffer != 0); |
michael@0 | 20 | } |
michael@0 | 21 | |
michael@0 | 22 | void COutBuffer::Free() |
michael@0 | 23 | { |
michael@0 | 24 | ::MidFree(_buffer); |
michael@0 | 25 | _buffer = 0; |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | void COutBuffer::SetStream(ISequentialOutStream *stream) |
michael@0 | 29 | { |
michael@0 | 30 | _stream = stream; |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | void COutBuffer::Init() |
michael@0 | 34 | { |
michael@0 | 35 | _streamPos = 0; |
michael@0 | 36 | _limitPos = _bufferSize; |
michael@0 | 37 | _pos = 0; |
michael@0 | 38 | _processedSize = 0; |
michael@0 | 39 | _overDict = false; |
michael@0 | 40 | #ifdef _NO_EXCEPTIONS |
michael@0 | 41 | ErrorCode = S_OK; |
michael@0 | 42 | #endif |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | UInt64 COutBuffer::GetProcessedSize() const |
michael@0 | 46 | { |
michael@0 | 47 | UInt64 res = _processedSize + _pos - _streamPos; |
michael@0 | 48 | if (_streamPos > _pos) |
michael@0 | 49 | res += _bufferSize; |
michael@0 | 50 | return res; |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | |
michael@0 | 54 | HRESULT COutBuffer::FlushPart() |
michael@0 | 55 | { |
michael@0 | 56 | // _streamPos < _bufferSize |
michael@0 | 57 | UInt32 size = (_streamPos >= _pos) ? (_bufferSize - _streamPos) : (_pos - _streamPos); |
michael@0 | 58 | HRESULT result = S_OK; |
michael@0 | 59 | #ifdef _NO_EXCEPTIONS |
michael@0 | 60 | result = ErrorCode; |
michael@0 | 61 | #endif |
michael@0 | 62 | if (_buffer2 != 0) |
michael@0 | 63 | { |
michael@0 | 64 | memmove(_buffer2, _buffer + _streamPos, size); |
michael@0 | 65 | _buffer2 += size; |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | if (_stream != 0 |
michael@0 | 69 | #ifdef _NO_EXCEPTIONS |
michael@0 | 70 | && (ErrorCode == S_OK) |
michael@0 | 71 | #endif |
michael@0 | 72 | ) |
michael@0 | 73 | { |
michael@0 | 74 | UInt32 processedSize = 0; |
michael@0 | 75 | result = _stream->Write(_buffer + _streamPos, size, &processedSize); |
michael@0 | 76 | size = processedSize; |
michael@0 | 77 | } |
michael@0 | 78 | _streamPos += size; |
michael@0 | 79 | if (_streamPos == _bufferSize) |
michael@0 | 80 | _streamPos = 0; |
michael@0 | 81 | if (_pos == _bufferSize) |
michael@0 | 82 | { |
michael@0 | 83 | _overDict = true; |
michael@0 | 84 | _pos = 0; |
michael@0 | 85 | } |
michael@0 | 86 | _limitPos = (_streamPos > _pos) ? _streamPos : _bufferSize; |
michael@0 | 87 | _processedSize += size; |
michael@0 | 88 | return result; |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | HRESULT COutBuffer::Flush() |
michael@0 | 92 | { |
michael@0 | 93 | #ifdef _NO_EXCEPTIONS |
michael@0 | 94 | if (ErrorCode != S_OK) |
michael@0 | 95 | return ErrorCode; |
michael@0 | 96 | #endif |
michael@0 | 97 | |
michael@0 | 98 | while(_streamPos != _pos) |
michael@0 | 99 | { |
michael@0 | 100 | HRESULT result = FlushPart(); |
michael@0 | 101 | if (result != S_OK) |
michael@0 | 102 | return result; |
michael@0 | 103 | } |
michael@0 | 104 | return S_OK; |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | void COutBuffer::FlushWithCheck() |
michael@0 | 108 | { |
michael@0 | 109 | HRESULT result = FlushPart(); |
michael@0 | 110 | #ifdef _NO_EXCEPTIONS |
michael@0 | 111 | ErrorCode = result; |
michael@0 | 112 | #else |
michael@0 | 113 | if (result != S_OK) |
michael@0 | 114 | throw COutBufferException(result); |
michael@0 | 115 | #endif |
michael@0 | 116 | } |