1 // StreamObjects.cpp
2 
3 #include "StdAfx.h"
4 
5 #include "StreamObjects.h"
6 #include "../../Common/Defs.h"
7 
8 
Read(void * data,UInt32 size,UInt32 * processedSize)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 }
18 
19 
Write(const void * data,size_t size)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 }
27 
Write(const void * data,UInt32 size,UInt32 * processedSize)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 }
35 
Write(const void * data,UInt32 size,UInt32 * processedSize)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 }
49 
Read(void * data,UInt32 size,UInt32 * processedSize)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 }
59 
Read(void * data,UInt32 size,UInt32 * processedSize)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 }
85 
Rollback(size_t rollbackSize)86 HRESULT CSequentialInStreamRollback::Rollback(size_t rollbackSize)
87 {
88   if (rollbackSize > _currentPos)
89     return E_INVALIDARG;
90   _currentPos -= rollbackSize;
91   return S_OK;
92 }
93 
Write(const void * data,UInt32 size,UInt32 * processedSize)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 }
103