1370b324cSopenharmony_ci// CWrappers.c
2370b324cSopenharmony_ci
3370b324cSopenharmony_ci#include "StdAfx.h"
4370b324cSopenharmony_ci
5370b324cSopenharmony_ci#include "../../../C/Alloc.h"
6370b324cSopenharmony_ci
7370b324cSopenharmony_ci#include "CWrappers.h"
8370b324cSopenharmony_ci
9370b324cSopenharmony_ci#include "StreamUtils.h"
10370b324cSopenharmony_ci
11370b324cSopenharmony_ciSRes HRESULT_To_SRes(HRESULT res, SRes defaultRes) throw()
12370b324cSopenharmony_ci{
13370b324cSopenharmony_ci  switch (res)
14370b324cSopenharmony_ci  {
15370b324cSopenharmony_ci    case S_OK: return SZ_OK;
16370b324cSopenharmony_ci    case E_OUTOFMEMORY: return SZ_ERROR_MEM;
17370b324cSopenharmony_ci    case E_INVALIDARG: return SZ_ERROR_PARAM;
18370b324cSopenharmony_ci    case E_ABORT: return SZ_ERROR_PROGRESS;
19370b324cSopenharmony_ci    case S_FALSE: return SZ_ERROR_DATA;
20370b324cSopenharmony_ci    case E_NOTIMPL: return SZ_ERROR_UNSUPPORTED;
21370b324cSopenharmony_ci  }
22370b324cSopenharmony_ci  return defaultRes;
23370b324cSopenharmony_ci}
24370b324cSopenharmony_ci
25370b324cSopenharmony_ci
26370b324cSopenharmony_ciHRESULT SResToHRESULT(SRes res) throw()
27370b324cSopenharmony_ci{
28370b324cSopenharmony_ci  switch (res)
29370b324cSopenharmony_ci  {
30370b324cSopenharmony_ci    case SZ_OK: return S_OK;
31370b324cSopenharmony_ci
32370b324cSopenharmony_ci    case SZ_ERROR_DATA:
33370b324cSopenharmony_ci    case SZ_ERROR_CRC:
34370b324cSopenharmony_ci    case SZ_ERROR_INPUT_EOF:
35370b324cSopenharmony_ci      return S_FALSE;
36370b324cSopenharmony_ci
37370b324cSopenharmony_ci    case SZ_ERROR_MEM: return E_OUTOFMEMORY;
38370b324cSopenharmony_ci    case SZ_ERROR_PARAM: return E_INVALIDARG;
39370b324cSopenharmony_ci    case SZ_ERROR_PROGRESS: return E_ABORT;
40370b324cSopenharmony_ci    case SZ_ERROR_UNSUPPORTED: return E_NOTIMPL;
41370b324cSopenharmony_ci    // case SZ_ERROR_OUTPUT_EOF:
42370b324cSopenharmony_ci    // case SZ_ERROR_READ:
43370b324cSopenharmony_ci    // case SZ_ERROR_WRITE:
44370b324cSopenharmony_ci    // case SZ_ERROR_THREAD:
45370b324cSopenharmony_ci    // case SZ_ERROR_ARCHIVE:
46370b324cSopenharmony_ci    // case SZ_ERROR_NO_ARCHIVE:
47370b324cSopenharmony_ci    // return E_FAIL;
48370b324cSopenharmony_ci  }
49370b324cSopenharmony_ci  if (res < 0)
50370b324cSopenharmony_ci    return res;
51370b324cSopenharmony_ci  return E_FAIL;
52370b324cSopenharmony_ci}
53370b324cSopenharmony_ci
54370b324cSopenharmony_ci
55370b324cSopenharmony_ci#define PROGRESS_UNKNOWN_VALUE ((UInt64)(Int64)-1)
56370b324cSopenharmony_ci
57370b324cSopenharmony_ci#define CONVERT_PR_VAL(x) (x == PROGRESS_UNKNOWN_VALUE ? NULL : &x)
58370b324cSopenharmony_ci
59370b324cSopenharmony_ci
60370b324cSopenharmony_cistatic SRes CompressProgress(ICompressProgressPtr pp, UInt64 inSize, UInt64 outSize) throw()
61370b324cSopenharmony_ci{
62370b324cSopenharmony_ci  Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR_pp_vt_p(CCompressProgressWrap)
63370b324cSopenharmony_ci  p->Res = p->Progress->SetRatioInfo(CONVERT_PR_VAL(inSize), CONVERT_PR_VAL(outSize));
64370b324cSopenharmony_ci  return HRESULT_To_SRes(p->Res, SZ_ERROR_PROGRESS);
65370b324cSopenharmony_ci}
66370b324cSopenharmony_ci
67370b324cSopenharmony_civoid CCompressProgressWrap::Init(ICompressProgressInfo *progress) throw()
68370b324cSopenharmony_ci{
69370b324cSopenharmony_ci  vt.Progress = CompressProgress;
70370b324cSopenharmony_ci  Progress = progress;
71370b324cSopenharmony_ci  Res = SZ_OK;
72370b324cSopenharmony_ci}
73370b324cSopenharmony_ci
74370b324cSopenharmony_cistatic const UInt32 kStreamStepSize = (UInt32)1 << 31;
75370b324cSopenharmony_ci
76370b324cSopenharmony_cistatic SRes MyRead(ISeqInStreamPtr pp, void *data, size_t *size) throw()
77370b324cSopenharmony_ci{
78370b324cSopenharmony_ci  Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR_pp_vt_p(CSeqInStreamWrap)
79370b324cSopenharmony_ci  UInt32 curSize = ((*size < kStreamStepSize) ? (UInt32)*size : kStreamStepSize);
80370b324cSopenharmony_ci  p->Res = (p->Stream->Read(data, curSize, &curSize));
81370b324cSopenharmony_ci  *size = curSize;
82370b324cSopenharmony_ci  p->Processed += curSize;
83370b324cSopenharmony_ci  if (p->Res == S_OK)
84370b324cSopenharmony_ci    return SZ_OK;
85370b324cSopenharmony_ci  return HRESULT_To_SRes(p->Res, SZ_ERROR_READ);
86370b324cSopenharmony_ci}
87370b324cSopenharmony_ci
88370b324cSopenharmony_cistatic size_t MyWrite(ISeqOutStreamPtr pp, const void *data, size_t size) throw()
89370b324cSopenharmony_ci{
90370b324cSopenharmony_ci  Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR_pp_vt_p(CSeqOutStreamWrap)
91370b324cSopenharmony_ci  if (p->Stream)
92370b324cSopenharmony_ci  {
93370b324cSopenharmony_ci    p->Res = WriteStream(p->Stream, data, size);
94370b324cSopenharmony_ci    if (p->Res != 0)
95370b324cSopenharmony_ci      return 0;
96370b324cSopenharmony_ci  }
97370b324cSopenharmony_ci  else
98370b324cSopenharmony_ci    p->Res = S_OK;
99370b324cSopenharmony_ci  p->Processed += size;
100370b324cSopenharmony_ci  return size;
101370b324cSopenharmony_ci}
102370b324cSopenharmony_ci
103370b324cSopenharmony_ci
104370b324cSopenharmony_civoid CSeqInStreamWrap::Init(ISequentialInStream *stream) throw()
105370b324cSopenharmony_ci{
106370b324cSopenharmony_ci  vt.Read = MyRead;
107370b324cSopenharmony_ci  Stream = stream;
108370b324cSopenharmony_ci  Processed = 0;
109370b324cSopenharmony_ci  Res = S_OK;
110370b324cSopenharmony_ci}
111370b324cSopenharmony_ci
112370b324cSopenharmony_civoid CSeqOutStreamWrap::Init(ISequentialOutStream *stream) throw()
113370b324cSopenharmony_ci{
114370b324cSopenharmony_ci  vt.Write = MyWrite;
115370b324cSopenharmony_ci  Stream = stream;
116370b324cSopenharmony_ci  Res = SZ_OK;
117370b324cSopenharmony_ci  Processed = 0;
118370b324cSopenharmony_ci}
119370b324cSopenharmony_ci
120370b324cSopenharmony_ci
121370b324cSopenharmony_cistatic SRes InStreamWrap_Read(ISeekInStreamPtr pp, void *data, size_t *size) throw()
122370b324cSopenharmony_ci{
123370b324cSopenharmony_ci  Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR_pp_vt_p(CSeekInStreamWrap)
124370b324cSopenharmony_ci  UInt32 curSize = ((*size < kStreamStepSize) ? (UInt32)*size : kStreamStepSize);
125370b324cSopenharmony_ci  p->Res = p->Stream->Read(data, curSize, &curSize);
126370b324cSopenharmony_ci  *size = curSize;
127370b324cSopenharmony_ci  return (p->Res == S_OK) ? SZ_OK : SZ_ERROR_READ;
128370b324cSopenharmony_ci}
129370b324cSopenharmony_ci
130370b324cSopenharmony_cistatic SRes InStreamWrap_Seek(ISeekInStreamPtr pp, Int64 *offset, ESzSeek origin) throw()
131370b324cSopenharmony_ci{
132370b324cSopenharmony_ci  Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR_pp_vt_p(CSeekInStreamWrap)
133370b324cSopenharmony_ci  UInt32 moveMethod;
134370b324cSopenharmony_ci  /* we need (int)origin to eliminate the clang warning:
135370b324cSopenharmony_ci     default label in switch which covers all enumeration values
136370b324cSopenharmony_ci     [-Wcovered-switch-default */
137370b324cSopenharmony_ci  switch ((int)origin)
138370b324cSopenharmony_ci  {
139370b324cSopenharmony_ci    case SZ_SEEK_SET: moveMethod = STREAM_SEEK_SET; break;
140370b324cSopenharmony_ci    case SZ_SEEK_CUR: moveMethod = STREAM_SEEK_CUR; break;
141370b324cSopenharmony_ci    case SZ_SEEK_END: moveMethod = STREAM_SEEK_END; break;
142370b324cSopenharmony_ci    default: return SZ_ERROR_PARAM;
143370b324cSopenharmony_ci  }
144370b324cSopenharmony_ci  UInt64 newPosition;
145370b324cSopenharmony_ci  p->Res = p->Stream->Seek(*offset, moveMethod, &newPosition);
146370b324cSopenharmony_ci  *offset = (Int64)newPosition;
147370b324cSopenharmony_ci  return (p->Res == S_OK) ? SZ_OK : SZ_ERROR_READ;
148370b324cSopenharmony_ci}
149370b324cSopenharmony_ci
150370b324cSopenharmony_civoid CSeekInStreamWrap::Init(IInStream *stream) throw()
151370b324cSopenharmony_ci{
152370b324cSopenharmony_ci  Stream = stream;
153370b324cSopenharmony_ci  vt.Read = InStreamWrap_Read;
154370b324cSopenharmony_ci  vt.Seek = InStreamWrap_Seek;
155370b324cSopenharmony_ci  Res = S_OK;
156370b324cSopenharmony_ci}
157370b324cSopenharmony_ci
158370b324cSopenharmony_ci
159370b324cSopenharmony_ci/* ---------- CByteInBufWrap ---------- */
160370b324cSopenharmony_ci
161370b324cSopenharmony_civoid CByteInBufWrap::Free() throw()
162370b324cSopenharmony_ci{
163370b324cSopenharmony_ci  ::MidFree(Buf);
164370b324cSopenharmony_ci  Buf = NULL;
165370b324cSopenharmony_ci}
166370b324cSopenharmony_ci
167370b324cSopenharmony_cibool CByteInBufWrap::Alloc(UInt32 size) throw()
168370b324cSopenharmony_ci{
169370b324cSopenharmony_ci  if (!Buf || size != Size)
170370b324cSopenharmony_ci  {
171370b324cSopenharmony_ci    Free();
172370b324cSopenharmony_ci    Lim = Cur = Buf = (Byte *)::MidAlloc((size_t)size);
173370b324cSopenharmony_ci    Size = size;
174370b324cSopenharmony_ci  }
175370b324cSopenharmony_ci  return (Buf != NULL);
176370b324cSopenharmony_ci}
177370b324cSopenharmony_ci
178370b324cSopenharmony_ciByte CByteInBufWrap::ReadByteFromNewBlock() throw()
179370b324cSopenharmony_ci{
180370b324cSopenharmony_ci  if (!Extra && Res == S_OK)
181370b324cSopenharmony_ci  {
182370b324cSopenharmony_ci    UInt32 avail;
183370b324cSopenharmony_ci    Res = Stream->Read(Buf, Size, &avail);
184370b324cSopenharmony_ci    Processed += (size_t)(Cur - Buf);
185370b324cSopenharmony_ci    Cur = Buf;
186370b324cSopenharmony_ci    Lim = Buf + avail;
187370b324cSopenharmony_ci    if (avail != 0)
188370b324cSopenharmony_ci      return *Cur++;
189370b324cSopenharmony_ci  }
190370b324cSopenharmony_ci  Extra = true;
191370b324cSopenharmony_ci  return 0;
192370b324cSopenharmony_ci}
193370b324cSopenharmony_ci
194370b324cSopenharmony_ci// #pragma GCC diagnostic ignored "-Winvalid-offsetof"
195370b324cSopenharmony_ci
196370b324cSopenharmony_cistatic Byte Wrap_ReadByte(IByteInPtr pp) throw()
197370b324cSopenharmony_ci{
198370b324cSopenharmony_ci  CByteInBufWrap *p = Z7_CONTAINER_FROM_VTBL_CLS(pp, CByteInBufWrap, vt);
199370b324cSopenharmony_ci  // Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR_pp_vt_p(CByteInBufWrap)
200370b324cSopenharmony_ci  if (p->Cur != p->Lim)
201370b324cSopenharmony_ci    return *p->Cur++;
202370b324cSopenharmony_ci  return p->ReadByteFromNewBlock();
203370b324cSopenharmony_ci}
204370b324cSopenharmony_ci
205370b324cSopenharmony_ciCByteInBufWrap::CByteInBufWrap() throw(): Buf(NULL)
206370b324cSopenharmony_ci{
207370b324cSopenharmony_ci  vt.Read = Wrap_ReadByte;
208370b324cSopenharmony_ci}
209370b324cSopenharmony_ci
210370b324cSopenharmony_ci
211370b324cSopenharmony_ci
212370b324cSopenharmony_ci/* ---------- CByteOutBufWrap ---------- */
213370b324cSopenharmony_ci
214370b324cSopenharmony_ci/*
215370b324cSopenharmony_civoid CLookToSequentialWrap::Free() throw()
216370b324cSopenharmony_ci{
217370b324cSopenharmony_ci  ::MidFree(BufBase);
218370b324cSopenharmony_ci  BufBase = NULL;
219370b324cSopenharmony_ci}
220370b324cSopenharmony_ci
221370b324cSopenharmony_cibool CLookToSequentialWrap::Alloc(UInt32 size) throw()
222370b324cSopenharmony_ci{
223370b324cSopenharmony_ci  if (!BufBase || size != Size)
224370b324cSopenharmony_ci  {
225370b324cSopenharmony_ci    Free();
226370b324cSopenharmony_ci    BufBase = (Byte *)::MidAlloc((size_t)size);
227370b324cSopenharmony_ci    Size = size;
228370b324cSopenharmony_ci  }
229370b324cSopenharmony_ci  return (BufBase != NULL);
230370b324cSopenharmony_ci}
231370b324cSopenharmony_ci*/
232370b324cSopenharmony_ci
233370b324cSopenharmony_ci/*
234370b324cSopenharmony_ciEXTERN_C_BEGIN
235370b324cSopenharmony_ci
236370b324cSopenharmony_civoid CLookToSequentialWrap_Look(ILookInSeqStreamPtr pp)
237370b324cSopenharmony_ci{
238370b324cSopenharmony_ci  CLookToSequentialWrap *p = (CLookToSequentialWrap *)pp->Obj;
239370b324cSopenharmony_ci
240370b324cSopenharmony_ci  if (p->Extra || p->Res != S_OK)
241370b324cSopenharmony_ci    return;
242370b324cSopenharmony_ci  {
243370b324cSopenharmony_ci    UInt32 avail;
244370b324cSopenharmony_ci    p->Res = p->Stream->Read(p->BufBase, p->Size, &avail);
245370b324cSopenharmony_ci    p->Processed += avail;
246370b324cSopenharmony_ci    pp->Buf = p->BufBase;
247370b324cSopenharmony_ci    pp->Limit = pp->Buf + avail;
248370b324cSopenharmony_ci    if (avail == 0)
249370b324cSopenharmony_ci      p->Extra = true;
250370b324cSopenharmony_ci  }
251370b324cSopenharmony_ci}
252370b324cSopenharmony_ci
253370b324cSopenharmony_ciEXTERN_C_END
254370b324cSopenharmony_ci*/
255370b324cSopenharmony_ci
256370b324cSopenharmony_ci
257370b324cSopenharmony_ci/* ---------- CByteOutBufWrap ---------- */
258370b324cSopenharmony_ci
259370b324cSopenharmony_civoid CByteOutBufWrap::Free() throw()
260370b324cSopenharmony_ci{
261370b324cSopenharmony_ci  ::MidFree(Buf);
262370b324cSopenharmony_ci  Buf = NULL;
263370b324cSopenharmony_ci}
264370b324cSopenharmony_ci
265370b324cSopenharmony_cibool CByteOutBufWrap::Alloc(size_t size) throw()
266370b324cSopenharmony_ci{
267370b324cSopenharmony_ci  if (!Buf || size != Size)
268370b324cSopenharmony_ci  {
269370b324cSopenharmony_ci    Free();
270370b324cSopenharmony_ci    Buf = (Byte *)::MidAlloc(size);
271370b324cSopenharmony_ci    Size = size;
272370b324cSopenharmony_ci  }
273370b324cSopenharmony_ci  return (Buf != NULL);
274370b324cSopenharmony_ci}
275370b324cSopenharmony_ci
276370b324cSopenharmony_ciHRESULT CByteOutBufWrap::Flush() throw()
277370b324cSopenharmony_ci{
278370b324cSopenharmony_ci  if (Res == S_OK)
279370b324cSopenharmony_ci  {
280370b324cSopenharmony_ci    const size_t size = (size_t)(Cur - Buf);
281370b324cSopenharmony_ci    Res = WriteStream(Stream, Buf, size);
282370b324cSopenharmony_ci    if (Res == S_OK)
283370b324cSopenharmony_ci      Processed += size;
284370b324cSopenharmony_ci    // else throw 11;
285370b324cSopenharmony_ci  }
286370b324cSopenharmony_ci  Cur = Buf; // reset pointer for later Wrap_WriteByte()
287370b324cSopenharmony_ci  return Res;
288370b324cSopenharmony_ci}
289370b324cSopenharmony_ci
290370b324cSopenharmony_cistatic void Wrap_WriteByte(IByteOutPtr pp, Byte b) throw()
291370b324cSopenharmony_ci{
292370b324cSopenharmony_ci  CByteOutBufWrap *p = Z7_CONTAINER_FROM_VTBL_CLS(pp, CByteOutBufWrap, vt);
293370b324cSopenharmony_ci  // Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR_pp_vt_p(CByteOutBufWrap)
294370b324cSopenharmony_ci  Byte *dest = p->Cur;
295370b324cSopenharmony_ci  *dest = b;
296370b324cSopenharmony_ci  p->Cur = ++dest;
297370b324cSopenharmony_ci  if (dest == p->Lim)
298370b324cSopenharmony_ci    p->Flush();
299370b324cSopenharmony_ci}
300370b324cSopenharmony_ci
301370b324cSopenharmony_ciCByteOutBufWrap::CByteOutBufWrap() throw(): Buf(NULL), Size(0)
302370b324cSopenharmony_ci{
303370b324cSopenharmony_ci  vt.Write = Wrap_WriteByte;
304370b324cSopenharmony_ci}
305370b324cSopenharmony_ci
306370b324cSopenharmony_ci
307370b324cSopenharmony_ci/* ---------- CLookOutWrap ---------- */
308370b324cSopenharmony_ci
309370b324cSopenharmony_ci/*
310370b324cSopenharmony_civoid CLookOutWrap::Free() throw()
311370b324cSopenharmony_ci{
312370b324cSopenharmony_ci  ::MidFree(Buf);
313370b324cSopenharmony_ci  Buf = NULL;
314370b324cSopenharmony_ci}
315370b324cSopenharmony_ci
316370b324cSopenharmony_cibool CLookOutWrap::Alloc(size_t size) throw()
317370b324cSopenharmony_ci{
318370b324cSopenharmony_ci  if (!Buf || size != Size)
319370b324cSopenharmony_ci  {
320370b324cSopenharmony_ci    Free();
321370b324cSopenharmony_ci    Buf = (Byte *)::MidAlloc(size);
322370b324cSopenharmony_ci    Size = size;
323370b324cSopenharmony_ci  }
324370b324cSopenharmony_ci  return (Buf != NULL);
325370b324cSopenharmony_ci}
326370b324cSopenharmony_ci
327370b324cSopenharmony_cistatic size_t LookOutWrap_GetOutBuf(ILookOutStreamPtr pp, void **buf) throw()
328370b324cSopenharmony_ci{
329370b324cSopenharmony_ci  CLookOutWrap *p = Z7_CONTAINER_FROM_VTBL_CLS(pp, CLookOutWrap, vt);
330370b324cSopenharmony_ci  *buf = p->Buf;
331370b324cSopenharmony_ci  return p->Size;
332370b324cSopenharmony_ci}
333370b324cSopenharmony_ci
334370b324cSopenharmony_cistatic size_t LookOutWrap_Write(ILookOutStreamPtr pp, size_t size) throw()
335370b324cSopenharmony_ci{
336370b324cSopenharmony_ci  CLookOutWrap *p = Z7_CONTAINER_FROM_VTBL_CLS(pp, CLookOutWrap, vt);
337370b324cSopenharmony_ci  if (p->Res == S_OK && size != 0)
338370b324cSopenharmony_ci  {
339370b324cSopenharmony_ci    p->Res = WriteStream(p->Stream, p->Buf, size);
340370b324cSopenharmony_ci    if (p->Res == S_OK)
341370b324cSopenharmony_ci    {
342370b324cSopenharmony_ci      p->Processed += size;
343370b324cSopenharmony_ci      return size;
344370b324cSopenharmony_ci    }
345370b324cSopenharmony_ci  }
346370b324cSopenharmony_ci  return 0;
347370b324cSopenharmony_ci}
348370b324cSopenharmony_ci
349370b324cSopenharmony_ciCLookOutWrap::CLookOutWrap() throw(): Buf(NULL), Size(0)
350370b324cSopenharmony_ci{
351370b324cSopenharmony_ci  vt.GetOutBuf = LookOutWrap_GetOutBuf;
352370b324cSopenharmony_ci  vt.Write = LookOutWrap_Write;
353370b324cSopenharmony_ci}
354370b324cSopenharmony_ci*/
355