1baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// CWrappers.h 2baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 3baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "StdAfx.h" 4baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 5baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "../../../C/Alloc.h" 6baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 7baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "CWrappers.h" 8baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 9baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "StreamUtils.h" 10baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 11baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define PROGRESS_UNKNOWN_VALUE ((UInt64)(Int64)-1) 12baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 13baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define CONVERT_PR_VAL(x) (x == PROGRESS_UNKNOWN_VALUE ? NULL : &x) 14baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 15baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic SRes CompressProgress(void *pp, UInt64 inSize, UInt64 outSize) 16baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{ 17baa3858d3f5d128a5c8466b700098109edcad5f2repo sync CCompressProgressWrap *p = (CCompressProgressWrap *)pp; 18baa3858d3f5d128a5c8466b700098109edcad5f2repo sync p->Res = p->Progress->SetRatioInfo(CONVERT_PR_VAL(inSize), CONVERT_PR_VAL(outSize)); 19baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return (SRes)p->Res; 20baa3858d3f5d128a5c8466b700098109edcad5f2repo sync} 21baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 22baa3858d3f5d128a5c8466b700098109edcad5f2repo syncCCompressProgressWrap::CCompressProgressWrap(ICompressProgressInfo *progress) 23baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{ 24baa3858d3f5d128a5c8466b700098109edcad5f2repo sync p.Progress = CompressProgress; 25baa3858d3f5d128a5c8466b700098109edcad5f2repo sync Progress = progress; 26baa3858d3f5d128a5c8466b700098109edcad5f2repo sync Res = SZ_OK; 27baa3858d3f5d128a5c8466b700098109edcad5f2repo sync} 28baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 29baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic const UInt32 kStreamStepSize = (UInt32)1 << 31; 30baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 31baa3858d3f5d128a5c8466b700098109edcad5f2repo syncSRes HRESULT_To_SRes(HRESULT res, SRes defaultRes) 32baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{ 33baa3858d3f5d128a5c8466b700098109edcad5f2repo sync switch(res) 34baa3858d3f5d128a5c8466b700098109edcad5f2repo sync { 35baa3858d3f5d128a5c8466b700098109edcad5f2repo sync case S_OK: return SZ_OK; 36baa3858d3f5d128a5c8466b700098109edcad5f2repo sync case E_OUTOFMEMORY: return SZ_ERROR_MEM; 37baa3858d3f5d128a5c8466b700098109edcad5f2repo sync case E_INVALIDARG: return SZ_ERROR_PARAM; 38baa3858d3f5d128a5c8466b700098109edcad5f2repo sync case E_ABORT: return SZ_ERROR_PROGRESS; 39baa3858d3f5d128a5c8466b700098109edcad5f2repo sync case S_FALSE: return SZ_ERROR_DATA; 40baa3858d3f5d128a5c8466b700098109edcad5f2repo sync } 41baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return defaultRes; 42baa3858d3f5d128a5c8466b700098109edcad5f2repo sync} 43baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 44baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic SRes MyRead(void *object, void *data, size_t *size) 45baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{ 46baa3858d3f5d128a5c8466b700098109edcad5f2repo sync CSeqInStreamWrap *p = (CSeqInStreamWrap *)object; 47baa3858d3f5d128a5c8466b700098109edcad5f2repo sync UInt32 curSize = ((*size < kStreamStepSize) ? (UInt32)*size : kStreamStepSize); 48baa3858d3f5d128a5c8466b700098109edcad5f2repo sync p->Res = (p->Stream->Read(data, curSize, &curSize)); 49baa3858d3f5d128a5c8466b700098109edcad5f2repo sync *size = curSize; 50baa3858d3f5d128a5c8466b700098109edcad5f2repo sync if (p->Res == S_OK) 51baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return SZ_OK; 52baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return HRESULT_To_SRes(p->Res, SZ_ERROR_READ); 53baa3858d3f5d128a5c8466b700098109edcad5f2repo sync} 54baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 55baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic size_t MyWrite(void *object, const void *data, size_t size) 56baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{ 57baa3858d3f5d128a5c8466b700098109edcad5f2repo sync CSeqOutStreamWrap *p = (CSeqOutStreamWrap *)object; 58baa3858d3f5d128a5c8466b700098109edcad5f2repo sync if (p->Stream) 59baa3858d3f5d128a5c8466b700098109edcad5f2repo sync { 60baa3858d3f5d128a5c8466b700098109edcad5f2repo sync p->Res = WriteStream(p->Stream, data, size); 61baa3858d3f5d128a5c8466b700098109edcad5f2repo sync if (p->Res != 0) 62baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return 0; 63baa3858d3f5d128a5c8466b700098109edcad5f2repo sync } 64baa3858d3f5d128a5c8466b700098109edcad5f2repo sync else 65baa3858d3f5d128a5c8466b700098109edcad5f2repo sync p->Res = S_OK; 66baa3858d3f5d128a5c8466b700098109edcad5f2repo sync p->Processed += size; 67baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return size; 68baa3858d3f5d128a5c8466b700098109edcad5f2repo sync} 69baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 70baa3858d3f5d128a5c8466b700098109edcad5f2repo syncCSeqInStreamWrap::CSeqInStreamWrap(ISequentialInStream *stream) 71baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{ 72baa3858d3f5d128a5c8466b700098109edcad5f2repo sync p.Read = MyRead; 73baa3858d3f5d128a5c8466b700098109edcad5f2repo sync Stream = stream; 74baa3858d3f5d128a5c8466b700098109edcad5f2repo sync} 75baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 76baa3858d3f5d128a5c8466b700098109edcad5f2repo syncCSeqOutStreamWrap::CSeqOutStreamWrap(ISequentialOutStream *stream) 77baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{ 78baa3858d3f5d128a5c8466b700098109edcad5f2repo sync p.Write = MyWrite; 79baa3858d3f5d128a5c8466b700098109edcad5f2repo sync Stream = stream; 80baa3858d3f5d128a5c8466b700098109edcad5f2repo sync Res = SZ_OK; 81baa3858d3f5d128a5c8466b700098109edcad5f2repo sync Processed = 0; 82baa3858d3f5d128a5c8466b700098109edcad5f2repo sync} 83baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 84baa3858d3f5d128a5c8466b700098109edcad5f2repo syncHRESULT SResToHRESULT(SRes res) 85baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{ 86baa3858d3f5d128a5c8466b700098109edcad5f2repo sync switch(res) 87baa3858d3f5d128a5c8466b700098109edcad5f2repo sync { 88baa3858d3f5d128a5c8466b700098109edcad5f2repo sync case SZ_OK: return S_OK; 89baa3858d3f5d128a5c8466b700098109edcad5f2repo sync case SZ_ERROR_MEM: return E_OUTOFMEMORY; 90baa3858d3f5d128a5c8466b700098109edcad5f2repo sync case SZ_ERROR_PARAM: return E_INVALIDARG; 91baa3858d3f5d128a5c8466b700098109edcad5f2repo sync case SZ_ERROR_PROGRESS: return E_ABORT; 92baa3858d3f5d128a5c8466b700098109edcad5f2repo sync case SZ_ERROR_DATA: return S_FALSE; 93baa3858d3f5d128a5c8466b700098109edcad5f2repo sync } 94baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return E_FAIL; 95baa3858d3f5d128a5c8466b700098109edcad5f2repo sync} 96baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 97baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic SRes InStreamWrap_Read(void *pp, void *data, size_t *size) 98baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{ 99baa3858d3f5d128a5c8466b700098109edcad5f2repo sync CSeekInStreamWrap *p = (CSeekInStreamWrap *)pp; 100baa3858d3f5d128a5c8466b700098109edcad5f2repo sync UInt32 curSize = ((*size < kStreamStepSize) ? (UInt32)*size : kStreamStepSize); 101baa3858d3f5d128a5c8466b700098109edcad5f2repo sync p->Res = p->Stream->Read(data, curSize, &curSize); 102baa3858d3f5d128a5c8466b700098109edcad5f2repo sync *size = curSize; 103baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return (p->Res == S_OK) ? SZ_OK : SZ_ERROR_READ; 104baa3858d3f5d128a5c8466b700098109edcad5f2repo sync} 105baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 106baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic SRes InStreamWrap_Seek(void *pp, Int64 *offset, ESzSeek origin) 107baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{ 108baa3858d3f5d128a5c8466b700098109edcad5f2repo sync CSeekInStreamWrap *p = (CSeekInStreamWrap *)pp; 109baa3858d3f5d128a5c8466b700098109edcad5f2repo sync UInt32 moveMethod; 110baa3858d3f5d128a5c8466b700098109edcad5f2repo sync switch(origin) 111baa3858d3f5d128a5c8466b700098109edcad5f2repo sync { 112baa3858d3f5d128a5c8466b700098109edcad5f2repo sync case SZ_SEEK_SET: moveMethod = STREAM_SEEK_SET; break; 113baa3858d3f5d128a5c8466b700098109edcad5f2repo sync case SZ_SEEK_CUR: moveMethod = STREAM_SEEK_CUR; break; 114baa3858d3f5d128a5c8466b700098109edcad5f2repo sync case SZ_SEEK_END: moveMethod = STREAM_SEEK_END; break; 115baa3858d3f5d128a5c8466b700098109edcad5f2repo sync default: return SZ_ERROR_PARAM; 116baa3858d3f5d128a5c8466b700098109edcad5f2repo sync } 117baa3858d3f5d128a5c8466b700098109edcad5f2repo sync UInt64 newPosition; 118baa3858d3f5d128a5c8466b700098109edcad5f2repo sync p->Res = p->Stream->Seek(*offset, moveMethod, &newPosition); 119baa3858d3f5d128a5c8466b700098109edcad5f2repo sync *offset = (Int64)newPosition; 120baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return (p->Res == S_OK) ? SZ_OK : SZ_ERROR_READ; 121baa3858d3f5d128a5c8466b700098109edcad5f2repo sync} 122baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 123baa3858d3f5d128a5c8466b700098109edcad5f2repo syncCSeekInStreamWrap::CSeekInStreamWrap(IInStream *stream) 124baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{ 125baa3858d3f5d128a5c8466b700098109edcad5f2repo sync Stream = stream; 126baa3858d3f5d128a5c8466b700098109edcad5f2repo sync p.Read = InStreamWrap_Read; 127baa3858d3f5d128a5c8466b700098109edcad5f2repo sync p.Seek = InStreamWrap_Seek; 128baa3858d3f5d128a5c8466b700098109edcad5f2repo sync Res = S_OK; 129baa3858d3f5d128a5c8466b700098109edcad5f2repo sync} 130baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 131baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 132baa3858d3f5d128a5c8466b700098109edcad5f2repo sync/* ---------- CByteInBufWrap ---------- */ 133baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 134baa3858d3f5d128a5c8466b700098109edcad5f2repo syncvoid CByteInBufWrap::Free() 135baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{ 136baa3858d3f5d128a5c8466b700098109edcad5f2repo sync ::MidFree(Buf); 137baa3858d3f5d128a5c8466b700098109edcad5f2repo sync Buf = 0; 138baa3858d3f5d128a5c8466b700098109edcad5f2repo sync} 139baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 140baa3858d3f5d128a5c8466b700098109edcad5f2repo syncbool CByteInBufWrap::Alloc(UInt32 size) 141baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{ 142baa3858d3f5d128a5c8466b700098109edcad5f2repo sync if (Buf == 0 || size != Size) 143baa3858d3f5d128a5c8466b700098109edcad5f2repo sync { 144baa3858d3f5d128a5c8466b700098109edcad5f2repo sync Free(); 145baa3858d3f5d128a5c8466b700098109edcad5f2repo sync Lim = Cur = Buf = (Byte *)::MidAlloc((size_t)size); 146baa3858d3f5d128a5c8466b700098109edcad5f2repo sync Size = size; 147baa3858d3f5d128a5c8466b700098109edcad5f2repo sync } 148baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return (Buf != 0); 149baa3858d3f5d128a5c8466b700098109edcad5f2repo sync} 150baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 151baa3858d3f5d128a5c8466b700098109edcad5f2repo syncByte CByteInBufWrap::ReadByteFromNewBlock() 152baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{ 153baa3858d3f5d128a5c8466b700098109edcad5f2repo sync if (Res == S_OK) 154baa3858d3f5d128a5c8466b700098109edcad5f2repo sync { 155baa3858d3f5d128a5c8466b700098109edcad5f2repo sync UInt32 avail; 156baa3858d3f5d128a5c8466b700098109edcad5f2repo sync Processed += (Cur - Buf); 157baa3858d3f5d128a5c8466b700098109edcad5f2repo sync Res = Stream->Read(Buf, Size, &avail); 158baa3858d3f5d128a5c8466b700098109edcad5f2repo sync Cur = Buf; 159baa3858d3f5d128a5c8466b700098109edcad5f2repo sync Lim = Buf + avail; 160baa3858d3f5d128a5c8466b700098109edcad5f2repo sync if (avail != 0) 161baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return *Cur++; 162baa3858d3f5d128a5c8466b700098109edcad5f2repo sync } 163baa3858d3f5d128a5c8466b700098109edcad5f2repo sync Extra = true; 164baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return 0; 165baa3858d3f5d128a5c8466b700098109edcad5f2repo sync} 166baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 167baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic Byte Wrap_ReadByte(void *pp) 168baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{ 169baa3858d3f5d128a5c8466b700098109edcad5f2repo sync CByteInBufWrap *p = (CByteInBufWrap *)pp; 170baa3858d3f5d128a5c8466b700098109edcad5f2repo sync if (p->Cur != p->Lim) 171baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return *p->Cur++; 172baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return p->ReadByteFromNewBlock(); 173baa3858d3f5d128a5c8466b700098109edcad5f2repo sync} 174baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 175baa3858d3f5d128a5c8466b700098109edcad5f2repo syncCByteInBufWrap::CByteInBufWrap(): Buf(0) 176baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{ 177baa3858d3f5d128a5c8466b700098109edcad5f2repo sync p.Read = Wrap_ReadByte; 178baa3858d3f5d128a5c8466b700098109edcad5f2repo sync} 179baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 180baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 181baa3858d3f5d128a5c8466b700098109edcad5f2repo sync/* ---------- CByteOutBufWrap ---------- */ 182baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 183baa3858d3f5d128a5c8466b700098109edcad5f2repo syncvoid CByteOutBufWrap::Free() 184baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{ 185baa3858d3f5d128a5c8466b700098109edcad5f2repo sync ::MidFree(Buf); 186baa3858d3f5d128a5c8466b700098109edcad5f2repo sync Buf = 0; 187baa3858d3f5d128a5c8466b700098109edcad5f2repo sync} 188baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 189baa3858d3f5d128a5c8466b700098109edcad5f2repo syncbool CByteOutBufWrap::Alloc(size_t size) 190baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{ 191baa3858d3f5d128a5c8466b700098109edcad5f2repo sync if (Buf == 0 || size != Size) 192baa3858d3f5d128a5c8466b700098109edcad5f2repo sync { 193baa3858d3f5d128a5c8466b700098109edcad5f2repo sync Free(); 194baa3858d3f5d128a5c8466b700098109edcad5f2repo sync Buf = (Byte *)::MidAlloc(size); 195baa3858d3f5d128a5c8466b700098109edcad5f2repo sync Size = size; 196baa3858d3f5d128a5c8466b700098109edcad5f2repo sync } 197baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return (Buf != 0); 198baa3858d3f5d128a5c8466b700098109edcad5f2repo sync} 199baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 200baa3858d3f5d128a5c8466b700098109edcad5f2repo syncHRESULT CByteOutBufWrap::Flush() 201baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{ 202baa3858d3f5d128a5c8466b700098109edcad5f2repo sync if (Res == S_OK) 203baa3858d3f5d128a5c8466b700098109edcad5f2repo sync { 204baa3858d3f5d128a5c8466b700098109edcad5f2repo sync size_t size = (Cur - Buf); 205baa3858d3f5d128a5c8466b700098109edcad5f2repo sync Res = WriteStream(Stream, Buf, size); 206baa3858d3f5d128a5c8466b700098109edcad5f2repo sync if (Res == S_OK) 207baa3858d3f5d128a5c8466b700098109edcad5f2repo sync Processed += size; 208baa3858d3f5d128a5c8466b700098109edcad5f2repo sync Cur = Buf; 209baa3858d3f5d128a5c8466b700098109edcad5f2repo sync } 210baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return Res; 211baa3858d3f5d128a5c8466b700098109edcad5f2repo sync} 212baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 213baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void Wrap_WriteByte(void *pp, Byte b) 214baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{ 215baa3858d3f5d128a5c8466b700098109edcad5f2repo sync CByteOutBufWrap *p = (CByteOutBufWrap *)pp; 216baa3858d3f5d128a5c8466b700098109edcad5f2repo sync Byte *dest = p->Cur; 217baa3858d3f5d128a5c8466b700098109edcad5f2repo sync *dest = b; 218baa3858d3f5d128a5c8466b700098109edcad5f2repo sync p->Cur = ++dest; 219baa3858d3f5d128a5c8466b700098109edcad5f2repo sync if (dest == p->Lim) 220baa3858d3f5d128a5c8466b700098109edcad5f2repo sync p->Flush(); 221baa3858d3f5d128a5c8466b700098109edcad5f2repo sync} 222baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 223baa3858d3f5d128a5c8466b700098109edcad5f2repo syncCByteOutBufWrap::CByteOutBufWrap(): Buf(0) 224baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{ 225baa3858d3f5d128a5c8466b700098109edcad5f2repo sync p.Write = Wrap_WriteByte; 226baa3858d3f5d128a5c8466b700098109edcad5f2repo sync} 227