Welcome to mirror list, hosted at ThFree Co, Russian Federation.

StreamObjects.cpp « Common « 7zip - github.com/kornelski/7z.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8632cff4ac7f068a040634742fb8f581e82aa617 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// StreamObjects.cpp

#include "StdAfx.h"

#include "StreamObjects.h"
#include "../../Common/Defs.h"

/*
STDMETHODIMP COutStreamImp::Read(void *data, ULONG size, ULONG *processedSize)
  {  return E_NOTIMPL; }

STDMETHODIMP COutStreamImp::Write(void const *data, ULONG size, ULONG *processedSize)
{
  size_t newCapacity = _size + size;
  _buffer.EnsureCapacity(newCapacity);
  memmove(_buffer + _size, data, size);
  if(processedSize != NULL)
    *processedSize = size;
  _size += size;
  return S_OK; 
}

void CInStreamImp::Init(Byte *dataPointer, size_t size)
{
  _dataPointer = dataPointer;
  _size = size;
  _pos = 0;
}

STDMETHODIMP CInStreamImp::Read(void *data, ULONG size, ULONG *processedSize)
{
  UInt32 numBytesToRead = MyMin(_pos + (UInt32)size, _size) - _pos;
  if(processedSize != NULL)
    *processedSize = numBytesToRead;
  memmove(data, _dataPointer + _pos, numBytesToRead);
  _pos += numBytesToRead;
  if(numBytesToRead == size)
    return S_OK;
  else
    return S_FALSE;
}

STDMETHODIMP CInStreamImp::Write(void const *data, ULONG size, ULONG *processedSize)
  {  return E_NOTIMPL; }
*/


STDMETHODIMP CSequentialInStreamImp::Read(void *data, UInt32 size, UInt32 *processedSize)
{
  UInt32 numBytesToRead = (UInt32)(MyMin(_pos + size, _size) - _pos);
  memmove(data, _dataPointer + _pos, numBytesToRead);
  _pos += numBytesToRead;
  if(processedSize != NULL)
    *processedSize = numBytesToRead;
  return S_OK;
}

STDMETHODIMP CSequentialInStreamImp::ReadPart(void *data, UInt32 size, UInt32 *processedSize)
{
  return Read(data, size, processedSize);
}

////////////////////


void CWriteBuffer::Write(const void *data, size_t size)
{
  size_t newCapacity = _size + size;
  _buffer.EnsureCapacity(newCapacity);
  memmove(_buffer + _size, data, size);
  _size += size;
}

STDMETHODIMP CSequentialOutStreamImp::Write(const void *data, UInt32 size, UInt32 *processedSize)
{
  _writeBuffer.Write(data, size);
  if(processedSize != NULL)
    *processedSize = size;
  return S_OK; 
}

STDMETHODIMP CSequentialOutStreamImp::WritePart(const void *data, UInt32 size, UInt32 *processedSize)
{
  return Write(data, size, processedSize);
}

STDMETHODIMP CSequentialOutStreamImp2::Write(const void *data, UInt32 size, UInt32 *processedSize)
{
  UInt32 newSize = size;
  if (_pos + size > _size)
    newSize = (UInt32)(_size - _pos);
  memmove(_buffer + _pos, data, newSize);
  if(processedSize != NULL)
    *processedSize = newSize;
  _pos += newSize;
  if (newSize != size)
    return E_FAIL;
  return S_OK; 
}

STDMETHODIMP CSequentialOutStreamImp2::WritePart(const void *data, UInt32 size, UInt32 *processedSize)
{
  return Write(data, size, processedSize);
}



STDMETHODIMP CSequentialInStreamSizeCount::Read(void *data, UInt32 size, UInt32 *processedSize)
{
  UInt32 realProcessedSize;
  HRESULT result = _stream->Read(data, size, &realProcessedSize);
  _size += realProcessedSize;
  if (processedSize != 0)
    *processedSize = realProcessedSize;
  return result; 
}

STDMETHODIMP CSequentialInStreamSizeCount::ReadPart(void *data, UInt32 size, UInt32 *processedSize)
{
  UInt32 realProcessedSize;
  HRESULT result = _stream->ReadPart(data, size, &realProcessedSize);
  _size += realProcessedSize;
  if (processedSize != 0)
    *processedSize = realProcessedSize;
  return result; 
}

STDMETHODIMP CSequentialInStreamRollback::Read(void *data, UInt32 size, UInt32 *processedSize)
{
  HRESULT result = S_OK;
  UInt32 realProcessedSizeTotal = 0;
  while (size > 0)
  {
    UInt32 realProcessedSize = 0;
    result = ReadPart(data, size, &realProcessedSize);
    size -= realProcessedSize;
    data = ((Byte *)data + realProcessedSize);
    realProcessedSizeTotal += realProcessedSize;
    if (realProcessedSize == 0 || result != S_OK)
      break;
  }
  if (processedSize != 0)
    *processedSize = realProcessedSizeTotal;
  return result;
}

STDMETHODIMP CSequentialInStreamRollback::ReadPart(void *data, UInt32 size, UInt32 *processedSize)
{
  if (_currentPos != _currentSize)
  {
    size_t curSize = _currentSize - _currentPos;
    if (size > curSize)
      size = (UInt32)curSize;
    memmove(data, _buffer + _currentPos, size);
    _currentPos += size;
    if (processedSize != 0)
      *processedSize = size;
    return S_OK;
  }
  UInt32 realProcessedSize;
  if (size > _bufferSize)
    size = (UInt32)_bufferSize;
  HRESULT result = _stream->ReadPart(_buffer, size, &realProcessedSize);
  memmove(data, _buffer, realProcessedSize);
  _size += realProcessedSize;
  _currentSize = realProcessedSize;
  _currentPos = realProcessedSize;
  if (processedSize != 0)
    *processedSize = realProcessedSize;
  return result; 
}

HRESULT CSequentialInStreamRollback::Rollback(size_t rollbackSize)
{
  if (rollbackSize > _currentPos)
    return E_INVALIDARG;
  _currentPos -= rollbackSize;
  return S_OK;
}


STDMETHODIMP CSequentialOutStreamSizeCount::Write(const void *data, UInt32 size, UInt32 *processedSize)
{
  UInt32 realProcessedSize;
  HRESULT result = _stream->Write(data, size, &realProcessedSize);
  _size += realProcessedSize;
  if (processedSize != 0)
    *processedSize = realProcessedSize;
  return result; 
}

STDMETHODIMP CSequentialOutStreamSizeCount::WritePart(const void *data, UInt32 size, UInt32 *processedSize)
{
  UInt32 realProcessedSize;
  HRESULT result = _stream->WritePart(data, size, &realProcessedSize);
  _size += realProcessedSize;
  if (processedSize != 0)
    *processedSize = realProcessedSize;
  return result; 
}