1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/other-licenses/7zstub/src/7zip/Common/StreamObjects.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,102 @@ 1.4 +// StreamObjects.cpp 1.5 + 1.6 +#include "StdAfx.h" 1.7 + 1.8 +#include "StreamObjects.h" 1.9 +#include "../../Common/Defs.h" 1.10 + 1.11 + 1.12 +STDMETHODIMP CSequentialInStreamImp::Read(void *data, UInt32 size, UInt32 *processedSize) 1.13 +{ 1.14 + UInt32 numBytesToRead = (UInt32)(MyMin(_pos + size, _size) - _pos); 1.15 + memmove(data, _dataPointer + _pos, numBytesToRead); 1.16 + _pos += numBytesToRead; 1.17 + if(processedSize != NULL) 1.18 + *processedSize = numBytesToRead; 1.19 + return S_OK; 1.20 +} 1.21 + 1.22 + 1.23 +void CWriteBuffer::Write(const void *data, size_t size) 1.24 +{ 1.25 + size_t newCapacity = _size + size; 1.26 + _buffer.EnsureCapacity(newCapacity); 1.27 + memmove(_buffer + _size, data, size); 1.28 + _size += size; 1.29 +} 1.30 + 1.31 +STDMETHODIMP CSequentialOutStreamImp::Write(const void *data, UInt32 size, UInt32 *processedSize) 1.32 +{ 1.33 + _writeBuffer.Write(data, size); 1.34 + if(processedSize != NULL) 1.35 + *processedSize = size; 1.36 + return S_OK; 1.37 +} 1.38 + 1.39 +STDMETHODIMP CSequentialOutStreamImp2::Write(const void *data, UInt32 size, UInt32 *processedSize) 1.40 +{ 1.41 + UInt32 newSize = size; 1.42 + if (_pos + size > _size) 1.43 + newSize = (UInt32)(_size - _pos); 1.44 + memmove(_buffer + _pos, data, newSize); 1.45 + if(processedSize != NULL) 1.46 + *processedSize = newSize; 1.47 + _pos += newSize; 1.48 + if (newSize != size) 1.49 + return E_FAIL; 1.50 + return S_OK; 1.51 +} 1.52 + 1.53 +STDMETHODIMP CSequentialInStreamSizeCount::Read(void *data, UInt32 size, UInt32 *processedSize) 1.54 +{ 1.55 + UInt32 realProcessedSize; 1.56 + HRESULT result = _stream->Read(data, size, &realProcessedSize); 1.57 + _size += realProcessedSize; 1.58 + if (processedSize != 0) 1.59 + *processedSize = realProcessedSize; 1.60 + return result; 1.61 +} 1.62 + 1.63 +STDMETHODIMP CSequentialInStreamRollback::Read(void *data, UInt32 size, UInt32 *processedSize) 1.64 +{ 1.65 + if (_currentPos != _currentSize) 1.66 + { 1.67 + size_t curSize = _currentSize - _currentPos; 1.68 + if (size > curSize) 1.69 + size = (UInt32)curSize; 1.70 + memmove(data, _buffer + _currentPos, size); 1.71 + _currentPos += size; 1.72 + if (processedSize != 0) 1.73 + *processedSize = size; 1.74 + return S_OK; 1.75 + } 1.76 + UInt32 realProcessedSize; 1.77 + if (size > _bufferSize) 1.78 + size = (UInt32)_bufferSize; 1.79 + HRESULT result = _stream->Read(_buffer, size, &realProcessedSize); 1.80 + memmove(data, _buffer, realProcessedSize); 1.81 + _size += realProcessedSize; 1.82 + _currentSize = realProcessedSize; 1.83 + _currentPos = realProcessedSize; 1.84 + if (processedSize != 0) 1.85 + *processedSize = realProcessedSize; 1.86 + return result; 1.87 +} 1.88 + 1.89 +HRESULT CSequentialInStreamRollback::Rollback(size_t rollbackSize) 1.90 +{ 1.91 + if (rollbackSize > _currentPos) 1.92 + return E_INVALIDARG; 1.93 + _currentPos -= rollbackSize; 1.94 + return S_OK; 1.95 +} 1.96 + 1.97 +STDMETHODIMP CSequentialOutStreamSizeCount::Write(const void *data, UInt32 size, UInt32 *processedSize) 1.98 +{ 1.99 + UInt32 realProcessedSize; 1.100 + HRESULT result = _stream->Write(data, size, &realProcessedSize); 1.101 + _size += realProcessedSize; 1.102 + if (processedSize != 0) 1.103 + *processedSize = realProcessedSize; 1.104 + return result; 1.105 +}