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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
// StreamObjects.cpp
#include "StdAfx.h"
#include "../../../C/Alloc.h"
#include "StreamObjects.h"
Z7_COM7F_IMF(CBufferInStream::Read(void *data, UInt32 size, UInt32 *processedSize))
{
if (processedSize)
*processedSize = 0;
if (size == 0)
return S_OK;
if (_pos >= Buf.Size())
return S_OK;
size_t rem = Buf.Size() - (size_t)_pos;
if (rem > size)
rem = (size_t)size;
memcpy(data, (const Byte *)Buf + (size_t)_pos, rem);
_pos += rem;
if (processedSize)
*processedSize = (UInt32)rem;
return S_OK;
}
Z7_COM7F_IMF(CBufferInStream::Seek(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition))
{
switch (seekOrigin)
{
case STREAM_SEEK_SET: break;
case STREAM_SEEK_CUR: offset += _pos; break;
case STREAM_SEEK_END: offset += Buf.Size(); break;
default: return STG_E_INVALIDFUNCTION;
}
if (offset < 0)
return HRESULT_WIN32_ERROR_NEGATIVE_SEEK;
_pos = (UInt64)offset;
if (newPosition)
*newPosition = (UInt64)offset;
return S_OK;
}
Z7_COM7F_IMF(CBufInStream::Read(void *data, UInt32 size, UInt32 *processedSize))
{
if (processedSize)
*processedSize = 0;
if (size == 0)
return S_OK;
if (_pos >= _size)
return S_OK;
size_t rem = _size - (size_t)_pos;
if (rem > size)
rem = (size_t)size;
memcpy(data, _data + (size_t)_pos, rem);
_pos += rem;
if (processedSize)
*processedSize = (UInt32)rem;
return S_OK;
}
Z7_COM7F_IMF(CBufInStream::Seek(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition))
{
switch (seekOrigin)
{
case STREAM_SEEK_SET: break;
case STREAM_SEEK_CUR: offset += _pos; break;
case STREAM_SEEK_END: offset += _size; break;
default: return STG_E_INVALIDFUNCTION;
}
if (offset < 0)
return HRESULT_WIN32_ERROR_NEGATIVE_SEEK;
_pos = (UInt64)offset;
if (newPosition)
*newPosition = (UInt64)offset;
return S_OK;
}
void Create_BufInStream_WithReference(const void *data, size_t size, IUnknown *ref, ISequentialInStream **stream)
{
*stream = NULL;
CBufInStream *inStreamSpec = new CBufInStream;
CMyComPtr<ISequentialInStream> streamTemp = inStreamSpec;
inStreamSpec->Init((const Byte *)data, size, ref);
*stream = streamTemp.Detach();
}
void Create_BufInStream_WithNewBuffer(const void *data, size_t size, ISequentialInStream **stream)
{
*stream = NULL;
CBufferInStream *inStreamSpec = new CBufferInStream;
CMyComPtr<ISequentialInStream> streamTemp = inStreamSpec;
inStreamSpec->Buf.CopyFrom((const Byte *)data, size);
inStreamSpec->Init();
*stream = streamTemp.Detach();
}
void CByteDynBuffer::Free() throw()
{
MyFree(_buf);
_buf = NULL;
_capacity = 0;
}
bool CByteDynBuffer::EnsureCapacity(size_t cap) throw()
{
if (cap <= _capacity)
return true;
const size_t cap2 = _capacity + _capacity / 4;
if (cap < cap2)
cap = cap2;
Byte *buf = (Byte *)MyRealloc(_buf, cap);
if (!buf)
return false;
_buf = buf;
_capacity = cap;
return true;
}
Byte *CDynBufSeqOutStream::GetBufPtrForWriting(size_t addSize)
{
addSize += _size;
if (addSize < _size)
return NULL;
if (!_buffer.EnsureCapacity(addSize))
return NULL;
return (Byte *)_buffer + _size;
}
void CDynBufSeqOutStream::CopyToBuffer(CByteBuffer &dest) const
{
dest.CopyFrom((const Byte *)_buffer, _size);
}
Z7_COM7F_IMF(CDynBufSeqOutStream::Write(const void *data, UInt32 size, UInt32 *processedSize))
{
if (processedSize)
*processedSize = 0;
if (size == 0)
return S_OK;
Byte *buf = GetBufPtrForWriting(size);
if (!buf)
return E_OUTOFMEMORY;
memcpy(buf, data, size);
UpdateSize(size);
if (processedSize)
*processedSize = size;
return S_OK;
}
Z7_COM7F_IMF(CBufPtrSeqOutStream::Write(const void *data, UInt32 size, UInt32 *processedSize))
{
size_t rem = _size - _pos;
if (rem > size)
rem = (size_t)size;
if (rem != 0)
{
memcpy(_buffer + _pos, data, rem);
_pos += rem;
}
if (processedSize)
*processedSize = (UInt32)rem;
return (rem != 0 || size == 0) ? S_OK : E_FAIL;
}
Z7_COM7F_IMF(CSequentialOutStreamSizeCount::Write(const void *data, UInt32 size, UInt32 *processedSize))
{
UInt32 realProcessedSize;
HRESULT result = _stream->Write(data, size, &realProcessedSize);
_size += realProcessedSize;
if (processedSize)
*processedSize = realProcessedSize;
return result;
}
static const UInt64 kEmptyTag = (UInt64)(Int64)-1;
void CCachedInStream::Free() throw()
{
MyFree(_tags);
_tags = NULL;
MidFree(_data);
_data = NULL;
}
bool CCachedInStream::Alloc(unsigned blockSizeLog, unsigned numBlocksLog) throw()
{
unsigned sizeLog = blockSizeLog + numBlocksLog;
if (sizeLog >= sizeof(size_t) * 8)
return false;
size_t dataSize = (size_t)1 << sizeLog;
if (!_data || dataSize != _dataSize)
{
MidFree(_data);
_data = (Byte *)MidAlloc(dataSize);
if (!_data)
return false;
_dataSize = dataSize;
}
if (!_tags || numBlocksLog != _numBlocksLog)
{
MyFree(_tags);
_tags = (UInt64 *)MyAlloc(sizeof(UInt64) << numBlocksLog);
if (!_tags)
return false;
_numBlocksLog = numBlocksLog;
}
_blockSizeLog = blockSizeLog;
return true;
}
void CCachedInStream::Init(UInt64 size) throw()
{
_size = size;
_pos = 0;
const size_t numBlocks = (size_t)1 << _numBlocksLog;
for (size_t i = 0; i < numBlocks; i++)
_tags[i] = kEmptyTag;
}
Z7_COM7F_IMF(CCachedInStream::Read(void *data, UInt32 size, UInt32 *processedSize))
{
if (processedSize)
*processedSize = 0;
if (size == 0)
return S_OK;
if (_pos >= _size)
return S_OK;
{
const UInt64 rem = _size - _pos;
if (size > rem)
size = (UInt32)rem;
}
while (size != 0)
{
const UInt64 cacheTag = _pos >> _blockSizeLog;
const size_t cacheIndex = (size_t)cacheTag & (((size_t)1 << _numBlocksLog) - 1);
Byte *p = _data + (cacheIndex << _blockSizeLog);
if (_tags[cacheIndex] != cacheTag)
{
_tags[cacheIndex] = kEmptyTag;
const UInt64 remInBlock = _size - (cacheTag << _blockSizeLog);
size_t blockSize = (size_t)1 << _blockSizeLog;
if (blockSize > remInBlock)
blockSize = (size_t)remInBlock;
RINOK(ReadBlock(cacheTag, p, blockSize))
_tags[cacheIndex] = cacheTag;
}
const size_t kBlockSize = (size_t)1 << _blockSizeLog;
const size_t offset = (size_t)_pos & (kBlockSize - 1);
UInt32 cur = size;
const size_t rem = kBlockSize - offset;
if (cur > rem)
cur = (UInt32)rem;
memcpy(data, p + offset, cur);
if (processedSize)
*processedSize += cur;
data = (void *)((const Byte *)data + cur);
_pos += cur;
size -= cur;
}
return S_OK;
}
Z7_COM7F_IMF(CCachedInStream::Seek(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition))
{
switch (seekOrigin)
{
case STREAM_SEEK_SET: break;
case STREAM_SEEK_CUR: offset += _pos; break;
case STREAM_SEEK_END: offset += _size; break;
default: return STG_E_INVALIDFUNCTION;
}
if (offset < 0)
return HRESULT_WIN32_ERROR_NEGATIVE_SEEK;
_pos = (UInt64)offset;
if (newPosition)
*newPosition = (UInt64)offset;
return S_OK;
}
|