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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
|
// CWrappers.c
#include "StdAfx.h"
#include "../../../C/Alloc.h"
#include "CWrappers.h"
#include "StreamUtils.h"
SRes HRESULT_To_SRes(HRESULT res, SRes defaultRes) throw()
{
switch (res)
{
case S_OK: return SZ_OK;
case E_OUTOFMEMORY: return SZ_ERROR_MEM;
case E_INVALIDARG: return SZ_ERROR_PARAM;
case E_ABORT: return SZ_ERROR_PROGRESS;
case S_FALSE: return SZ_ERROR_DATA;
case E_NOTIMPL: return SZ_ERROR_UNSUPPORTED;
default: break;
}
return defaultRes;
}
HRESULT SResToHRESULT(SRes res) throw()
{
switch (res)
{
case SZ_OK: return S_OK;
case SZ_ERROR_DATA:
case SZ_ERROR_CRC:
case SZ_ERROR_INPUT_EOF:
case SZ_ERROR_ARCHIVE:
case SZ_ERROR_NO_ARCHIVE:
return S_FALSE;
case SZ_ERROR_MEM: return E_OUTOFMEMORY;
case SZ_ERROR_PARAM: return E_INVALIDARG;
case SZ_ERROR_PROGRESS: return E_ABORT;
case SZ_ERROR_UNSUPPORTED: return E_NOTIMPL;
// case SZ_ERROR_OUTPUT_EOF:
// case SZ_ERROR_READ:
// case SZ_ERROR_WRITE:
// case SZ_ERROR_THREAD:
// case SZ_ERROR_ARCHIVE:
// case SZ_ERROR_NO_ARCHIVE:
// return E_FAIL;
default: break;
}
if (res < 0)
return res;
return E_FAIL;
}
#define PROGRESS_UNKNOWN_VALUE ((UInt64)(Int64)-1)
#define CONVERT_PR_VAL(x) (x == PROGRESS_UNKNOWN_VALUE ? NULL : &x)
static SRes CompressProgress(ICompressProgressPtr pp, UInt64 inSize, UInt64 outSize) throw()
{
Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR_pp_vt_p(CCompressProgressWrap)
p->Res = p->Progress->SetRatioInfo(CONVERT_PR_VAL(inSize), CONVERT_PR_VAL(outSize));
return HRESULT_To_SRes(p->Res, SZ_ERROR_PROGRESS);
}
void CCompressProgressWrap::Init(ICompressProgressInfo *progress) throw()
{
vt.Progress = CompressProgress;
Progress = progress;
Res = SZ_OK;
}
static const UInt32 kStreamStepSize = (UInt32)1 << 31;
static SRes MyRead(ISeqInStreamPtr pp, void *data, size_t *size) throw()
{
Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR_pp_vt_p(CSeqInStreamWrap)
UInt32 curSize = ((*size < kStreamStepSize) ? (UInt32)*size : kStreamStepSize);
p->Res = (p->Stream->Read(data, curSize, &curSize));
*size = curSize;
p->Processed += curSize;
if (p->Res == S_OK)
return SZ_OK;
return HRESULT_To_SRes(p->Res, SZ_ERROR_READ);
}
static size_t MyWrite(ISeqOutStreamPtr pp, const void *data, size_t size) throw()
{
Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR_pp_vt_p(CSeqOutStreamWrap)
if (p->Stream)
{
p->Res = WriteStream(p->Stream, data, size);
if (p->Res != 0)
return 0;
}
else
p->Res = S_OK;
p->Processed += size;
return size;
}
void CSeqInStreamWrap::Init(ISequentialInStream *stream) throw()
{
vt.Read = MyRead;
Stream = stream;
Processed = 0;
Res = S_OK;
}
void CSeqOutStreamWrap::Init(ISequentialOutStream *stream) throw()
{
vt.Write = MyWrite;
Stream = stream;
Res = SZ_OK;
Processed = 0;
}
static SRes InStreamWrap_Read(ISeekInStreamPtr pp, void *data, size_t *size) throw()
{
Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR_pp_vt_p(CSeekInStreamWrap)
UInt32 curSize = ((*size < kStreamStepSize) ? (UInt32)*size : kStreamStepSize);
p->Res = p->Stream->Read(data, curSize, &curSize);
*size = curSize;
return (p->Res == S_OK) ? SZ_OK : SZ_ERROR_READ;
}
static SRes InStreamWrap_Seek(ISeekInStreamPtr pp, Int64 *offset, ESzSeek origin) throw()
{
Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR_pp_vt_p(CSeekInStreamWrap)
UInt32 moveMethod;
/* we need (int)origin to eliminate the clang warning:
default label in switch which covers all enumeration values
[-Wcovered-switch-default */
switch ((int)origin)
{
case SZ_SEEK_SET: moveMethod = STREAM_SEEK_SET; break;
case SZ_SEEK_CUR: moveMethod = STREAM_SEEK_CUR; break;
case SZ_SEEK_END: moveMethod = STREAM_SEEK_END; break;
default: return SZ_ERROR_PARAM;
}
UInt64 newPosition;
p->Res = p->Stream->Seek(*offset, moveMethod, &newPosition);
*offset = (Int64)newPosition;
return (p->Res == S_OK) ? SZ_OK : SZ_ERROR_READ;
}
void CSeekInStreamWrap::Init(IInStream *stream) throw()
{
Stream = stream;
vt.Read = InStreamWrap_Read;
vt.Seek = InStreamWrap_Seek;
Res = S_OK;
}
/* ---------- CByteInBufWrap ---------- */
void CByteInBufWrap::Free() throw()
{
::MidFree(Buf);
Buf = NULL;
}
bool CByteInBufWrap::Alloc(UInt32 size) throw()
{
if (!Buf || size != Size)
{
Free();
Lim = Cur = Buf = (Byte *)::MidAlloc((size_t)size);
Size = size;
}
return (Buf != NULL);
}
Byte CByteInBufWrap::ReadByteFromNewBlock() throw()
{
if (!Extra && Res == S_OK)
{
UInt32 avail;
Res = Stream->Read(Buf, Size, &avail);
Processed += (size_t)(Cur - Buf);
Cur = Buf;
Lim = Buf + avail;
if (avail != 0)
return *Cur++;
}
Extra = true;
return 0;
}
// #pragma GCC diagnostic ignored "-Winvalid-offsetof"
static Byte Wrap_ReadByte(IByteInPtr pp) throw()
{
CByteInBufWrap *p = Z7_CONTAINER_FROM_VTBL_CLS(pp, CByteInBufWrap, vt);
// Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR_pp_vt_p(CByteInBufWrap)
if (p->Cur != p->Lim)
return *p->Cur++;
return p->ReadByteFromNewBlock();
}
CByteInBufWrap::CByteInBufWrap() throw(): Buf(NULL)
{
vt.Read = Wrap_ReadByte;
}
/* ---------- CByteOutBufWrap ---------- */
/*
void CLookToSequentialWrap::Free() throw()
{
::MidFree(BufBase);
BufBase = NULL;
}
bool CLookToSequentialWrap::Alloc(UInt32 size) throw()
{
if (!BufBase || size != Size)
{
Free();
BufBase = (Byte *)::MidAlloc((size_t)size);
Size = size;
}
return (BufBase != NULL);
}
*/
/*
EXTERN_C_BEGIN
void CLookToSequentialWrap_Look(ILookInSeqStreamPtr pp)
{
CLookToSequentialWrap *p = (CLookToSequentialWrap *)pp->Obj;
if (p->Extra || p->Res != S_OK)
return;
{
UInt32 avail;
p->Res = p->Stream->Read(p->BufBase, p->Size, &avail);
p->Processed += avail;
pp->Buf = p->BufBase;
pp->Limit = pp->Buf + avail;
if (avail == 0)
p->Extra = true;
}
}
EXTERN_C_END
*/
/* ---------- CByteOutBufWrap ---------- */
void CByteOutBufWrap::Free() throw()
{
::MidFree(Buf);
Buf = NULL;
}
bool CByteOutBufWrap::Alloc(size_t size) throw()
{
if (!Buf || size != Size)
{
Free();
Buf = (Byte *)::MidAlloc(size);
Size = size;
}
return (Buf != NULL);
}
HRESULT CByteOutBufWrap::Flush() throw()
{
if (Res == S_OK)
{
const size_t size = (size_t)(Cur - Buf);
Res = WriteStream(Stream, Buf, size);
if (Res == S_OK)
Processed += size;
// else throw 11;
}
Cur = Buf; // reset pointer for later Wrap_WriteByte()
return Res;
}
static void Wrap_WriteByte(IByteOutPtr pp, Byte b) throw()
{
CByteOutBufWrap *p = Z7_CONTAINER_FROM_VTBL_CLS(pp, CByteOutBufWrap, vt);
// Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR_pp_vt_p(CByteOutBufWrap)
Byte *dest = p->Cur;
*dest = b;
p->Cur = ++dest;
if (dest == p->Lim)
p->Flush();
}
CByteOutBufWrap::CByteOutBufWrap() throw(): Buf(NULL), Size(0)
{
vt.Write = Wrap_WriteByte;
}
/* ---------- CLookOutWrap ---------- */
/*
void CLookOutWrap::Free() throw()
{
::MidFree(Buf);
Buf = NULL;
}
bool CLookOutWrap::Alloc(size_t size) throw()
{
if (!Buf || size != Size)
{
Free();
Buf = (Byte *)::MidAlloc(size);
Size = size;
}
return (Buf != NULL);
}
static size_t LookOutWrap_GetOutBuf(ILookOutStreamPtr pp, void **buf) throw()
{
CLookOutWrap *p = Z7_CONTAINER_FROM_VTBL_CLS(pp, CLookOutWrap, vt);
*buf = p->Buf;
return p->Size;
}
static size_t LookOutWrap_Write(ILookOutStreamPtr pp, size_t size) throw()
{
CLookOutWrap *p = Z7_CONTAINER_FROM_VTBL_CLS(pp, CLookOutWrap, vt);
if (p->Res == S_OK && size != 0)
{
p->Res = WriteStream(p->Stream, p->Buf, size);
if (p->Res == S_OK)
{
p->Processed += size;
return size;
}
}
return 0;
}
CLookOutWrap::CLookOutWrap() throw(): Buf(NULL), Size(0)
{
vt.GetOutBuf = LookOutWrap_GetOutBuf;
vt.Write = LookOutWrap_Write;
}
*/
|