-
Notifications
You must be signed in to change notification settings - Fork 31
/
decompresser_demo.h
432 lines (404 loc) · 16.9 KB
/
decompresser_demo.h
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//decompresser_demo.h
// decompresser demo for HPatchLite
//
/*
The MIT License (MIT)
Copyright (c) 2020-2022 HouSisong All Rights Reserved.
*/
#ifndef hpatchi_decompresser_demo_h
#define hpatchi_decompresser_demo_h
#ifndef _IsNeedIncludeDefaultCompressHead
# define _IsNeedIncludeDefaultCompressHead 1
#endif
#ifdef _CompressPlugin_tuz
#if (_IsNeedIncludeDefaultCompressHead)
# include "tuz_dec.h" // "tinyuz/decompress/tuz_dec.h" https://github.com/sisong/tinyuz
#endif
static size_t _tuz_TStream_getReservedMemSize(hpi_TInputStreamHandle codeStream,hpi_TInputStream_read readCode){
const tuz_size_t dictSize=tuz_TStream_read_dict_size(codeStream,readCode);
if (((tuz_size_t)(dictSize-1))>=tuz_kMaxOfDictSize)
return 0;//error
return dictSize;
}
static hpi_BOOL _tuz_TStream_decompress(hpi_TInputStreamHandle diffStream,hpi_byte* out_part_data,hpi_size_t* data_size){
return tuz_STREAM_END>=tuz_TStream_decompress_partial((tuz_TStream*)diffStream,out_part_data,data_size);
}
#endif //_CompressPlugin_tuz
#ifdef _CompressPlugin_zlib
#if (_IsNeedIncludeDefaultCompressHead)
#define Byte __Byte
# include "zlib.h" // http://zlib.net/ https://github.com/madler/zlib
#undef Byte
#define ZLIB_INTERNAL
# include "inftrees.h" //for inflate_state::code
#undef ZLIB_INTERNAL
# include "inflate.h" //for inflate_state
#endif
typedef struct zlib_TStream{
hpi_TInputStreamHandle codeStream;
hpi_TInputStream_read readCode;
hpi_pos_t uncompressSize;
unsigned char* reservedMem;
size_t reservedMemSize;
unsigned char* dec_buf;
hpi_size_t dec_buf_size;
z_stream d_stream;
signed char windowBits;
} zlib_TStream;
static voidpf __zlib_TStream_malloc OF((voidpf opaque, uInt items, uInt size)){
zlib_TStream* self=(zlib_TStream*)opaque;
size_t alloc_size=items*(size_t)size;
voidpf result=self->reservedMem;
if (alloc_size>self->reservedMemSize)
return 0;
self->reservedMem+=alloc_size;
self->reservedMemSize-=alloc_size;
return result;
}
static void __zlib_TStream_free OF((voidpf opaque, voidpf address)) { }
static void _zlib_TStream_init(zlib_TStream* self,hpi_pos_t uncompressSize,
hpi_TInputStreamHandle codeStream,hpi_TInputStream_read readCode){
memset(self,0,sizeof(*self));
self->codeStream=codeStream;
self->readCode=readCode;
self->uncompressSize=uncompressSize;
self->d_stream.opaque=self;
self->d_stream.zalloc=__zlib_TStream_malloc;
self->d_stream.zfree=__zlib_TStream_free;
}
static size_t _zlib_TStream_getReservedMemSize(zlib_TStream* self){
int dictBits;
hpi_size_t rlen=1;
if (!(self->readCode(self->codeStream,(hpi_byte*)&self->windowBits,&rlen)&&(rlen==1)))
return 0;//error
dictBits=(int)self->windowBits;
if (dictBits<0) dictBits=-dictBits;
if (dictBits>15) dictBits-=16;
if ((dictBits<9)|(dictBits>15))
return 0;//error
self->reservedMemSize=sizeof(struct inflate_state)+((size_t)1<<dictBits);
return self->reservedMemSize;
}
static hpi_BOOL _zlib_TStream_open(zlib_TStream* self,hpi_byte* buf,size_t bufSize){
assert(bufSize>self->reservedMemSize);
self->reservedMem=buf;
self->dec_buf=buf+self->reservedMemSize;
self->dec_buf_size=(hpi_size_t)(bufSize-self->reservedMemSize);
return Z_OK==inflateInit2(&self->d_stream,(int)self->windowBits);
}
static hpi_BOOL _zlib_TStream_close(zlib_TStream* self){
if (self->d_stream.state!=0)
return Z_OK==inflateEnd(&self->d_stream);
else
return hpi_TRUE;
}
static hpatch_BOOL __zlib_TStream_inflate(zlib_TStream* self){
int ret;
uInt avail_out_back;
hpi_size_t avail_in_back=self->d_stream.avail_in;
if (avail_in_back==0) {
avail_in_back=self->dec_buf_size;
if (!self->readCode(self->codeStream,self->dec_buf,&avail_in_back))
return hpi_FALSE;//error;
assert(avail_in_back==(uInt)avail_in_back);
self->d_stream.avail_in=(uInt)avail_in_back;
self->d_stream.next_in=self->dec_buf;
}
avail_out_back=self->d_stream.avail_out;
ret=inflate(&self->d_stream,Z_NO_FLUSH);
if (ret==Z_OK){
if ((self->d_stream.avail_in==avail_in_back)&&(self->d_stream.avail_out==avail_out_back))
return hpi_FALSE;//error;
}else if (ret==Z_STREAM_END){//all end
if (self->d_stream.avail_out!=0)
return hpi_FALSE;//error;
}else{
return hpi_FALSE;//error;
}
return hpi_TRUE;
}
static hpi_BOOL _zlib_TStream_decompress(hpi_TInputStreamHandle diffStream,hpi_byte* out_part_data,hpi_size_t* data_size){
zlib_TStream* self=(zlib_TStream*)diffStream;
hpi_pos_t r_size=*data_size;
if (r_size>self->uncompressSize){
r_size=self->uncompressSize;
*data_size=(hpi_pos_t)r_size;
}
self->uncompressSize-=r_size;
self->d_stream.next_out=out_part_data;
self->d_stream.avail_out=(uInt)r_size;
assert(self->d_stream.avail_out==r_size);
while (self->d_stream.avail_out>0) {
if (!__zlib_TStream_inflate(self))
return hpi_FALSE;//error;
}
return hpi_TRUE;
}
#endif //_CompressPlugin_zlib
#ifdef _CompressPlugin_lzma
#if (_IsNeedIncludeDefaultCompressHead)
# include "LzmaDec.h" // "lzma/C/LzmaDec.h" https://github.com/sisong/lzma
#endif
typedef struct lzma_TStream{
ISzAlloc alloc_base;
hpi_TInputStreamHandle codeStream;
hpi_TInputStream_read readCode;
hpi_pos_t uncompressSize;
unsigned char* reservedMem;
size_t reservedMemSize;
unsigned char* dec_buf;
hpi_size_t dec_buf_size;
CLzmaDec decEnv;
SizeT decCopyPos;
SizeT decReadPos;
unsigned char propsSize;
unsigned char props[8];
} lzma_TStream;
static void * __lzma_dec_Alloc_sumSize(ISzAllocPtr p, size_t size){
lzma_TStream* self=(lzma_TStream*)p;
self->reservedMemSize+=size;
return (void*)1;
}
static void * __lzma_dec_Alloc(ISzAllocPtr p, size_t size){
lzma_TStream* self=(lzma_TStream*)p;
void* result=self->reservedMem;
if (size>self->reservedMemSize)
return 0;
self->reservedMem+=size;
self->reservedMemSize-=size;
return result;
}
static void __lzma_dec_Free(ISzAllocPtr p, void *address){ }
static void _lzma_TStream_init(lzma_TStream* self,hpi_pos_t uncompressSize,
hpi_TInputStreamHandle codeStream,hpi_TInputStream_read readCode){
memset(self,0,sizeof(*self));
self->codeStream=codeStream;
self->readCode=readCode;
self->uncompressSize=uncompressSize;
self->alloc_base.Free=__lzma_dec_Free;
}
static size_t _lzma_TStream_getReservedMemSize(lzma_TStream* self){
SRes res;
hpi_size_t rlen=1;
unsigned char propsSize;
unsigned char* props=self->props;
if (!(self->readCode(self->codeStream,&propsSize,&rlen)&&(rlen==1)))
return 0;//error
self->propsSize=propsSize;
rlen=propsSize;
if (((hpi_size_t)(rlen-1))>=sizeof(self->props))
return 0;//error
if (!(self->readCode(self->codeStream,props,&rlen)&&(rlen==propsSize)))
return 0;//error
self->alloc_base.Alloc=__lzma_dec_Alloc_sumSize;
res=LzmaDec_Allocate(&self->decEnv,props,rlen,&self->alloc_base);
memset(&self->decEnv,0,sizeof(self->decEnv));
if (SZ_OK!=res)
return 0;//error
return self->reservedMemSize;
}
static hpi_BOOL _lzma_TStream_open(lzma_TStream* self,hpi_byte* buf,size_t bufSize){
assert(bufSize>self->reservedMemSize);
self->reservedMem=buf;
self->dec_buf=buf+self->reservedMemSize;
self->dec_buf_size=(hpi_size_t)(bufSize-self->reservedMemSize);
//self->decCopyPos=0;
self->decReadPos=self->dec_buf_size;
self->alloc_base.Alloc=__lzma_dec_Alloc;
if (SZ_OK!=LzmaDec_Allocate(&self->decEnv,self->props,self->propsSize,&self->alloc_base))
return hpi_FALSE;//error
LzmaDec_Init(&self->decEnv);
return hpi_TRUE;
}
static hpi_BOOL _lzma_TStream_close(lzma_TStream* self){
LzmaDec_Free(&self->decEnv,&self->alloc_base);
return hpi_TRUE;
}
static hpi_BOOL _lzma_TStream_decompress(hpi_TInputStreamHandle diffStream,hpi_byte* out_part_data,hpi_size_t* data_size){
hpi_byte* out_part_data_end;
unsigned char* out_cur;
hpi_BOOL isReadedEnd=hpi_FALSE;
lzma_TStream* self=(lzma_TStream*)diffStream;
hpi_pos_t r_size=*data_size;
if (r_size>self->uncompressSize){
r_size=self->uncompressSize;
*data_size=(hpi_pos_t)r_size;
}
self->uncompressSize-=r_size;
out_part_data_end=out_part_data+r_size;
out_cur=out_part_data;
while (out_cur<out_part_data_end){
size_t copyLen=(self->decEnv.dicPos-self->decCopyPos);
if (copyLen>0){
if (copyLen>(size_t)(out_part_data_end-out_cur))
copyLen=(out_part_data_end-out_cur);
memcpy(out_cur,self->decEnv.dic+self->decCopyPos,copyLen);
out_cur+=copyLen;
self->decCopyPos+=copyLen;
if ((self->decEnv.dicPos==self->decEnv.dicBufSize)
&&(self->decEnv.dicPos==self->decCopyPos)){
self->decEnv.dicPos=0;
self->decCopyPos=0;
}
}else{
ELzmaStatus status;
SizeT inSize,dicPos_back;
SRes res;
if ((self->decReadPos==self->dec_buf_size)&(!isReadedEnd)) {
hpi_size_t readLen=self->dec_buf_size;
if (!self->readCode(self->codeStream,self->dec_buf,&readLen))
return hpi_FALSE;//error;
self->decReadPos=self->dec_buf_size-readLen;
if (self->decReadPos>0){
isReadedEnd=hpi_TRUE;
memmove(self->dec_buf+self->decReadPos,self->dec_buf,readLen);
}else{
self->decReadPos=0;
}
}
inSize=self->dec_buf_size-self->decReadPos;
dicPos_back=self->decEnv.dicPos;
res=LzmaDec_DecodeToDic(&self->decEnv,self->decEnv.dicBufSize,
self->dec_buf+self->decReadPos,&inSize,LZMA_FINISH_ANY,&status);
if(res==SZ_OK){
if ((inSize==0)&&(self->decEnv.dicPos==dicPos_back))
return hpi_FALSE;//error;
}else{
return hpi_FALSE;//error;
}
self->decReadPos+=inSize;
}
}
return hpi_TRUE;
}
#endif //_CompressPlugin_lzma
#ifdef _CompressPlugin_lzma2
#if (_IsNeedIncludeDefaultCompressHead)
# include "LzmaDec.h" // "lzma/C/LzmaDec.h" https://github.com/sisong/lzma
# include "Lzma2Dec.h"
#endif
typedef struct lzma2_TStream{
ISzAlloc alloc_base;
hpi_TInputStreamHandle codeStream;
hpi_TInputStream_read readCode;
hpi_pos_t uncompressSize;
unsigned char* reservedMem;
size_t reservedMemSize;
unsigned char* dec_buf;
hpi_size_t dec_buf_size;
CLzma2Dec decEnv;
SizeT decCopyPos;
SizeT decReadPos;
unsigned char prop;
} lzma2_TStream;
static void * __lzma2_dec_Alloc_sumSize(ISzAllocPtr p, size_t size){
lzma2_TStream* self=(lzma2_TStream*)p;
self->reservedMemSize+=size;
return (void*)1;
}
static void * __lzma2_dec_Alloc(ISzAllocPtr p, size_t size){
lzma2_TStream* self=(lzma2_TStream*)p;
void* result=self->reservedMem;
if (size>self->reservedMemSize)
return 0;
self->reservedMem+=size;
self->reservedMemSize-=size;
return result;
}
static void __lzma2_dec_Free(ISzAllocPtr p, void *address){ }
static void _lzma2_TStream_init(lzma2_TStream* self,hpi_pos_t uncompressSize,
hpi_TInputStreamHandle codeStream,hpi_TInputStream_read readCode){
memset(self,0,sizeof(*self));
self->codeStream=codeStream;
self->readCode=readCode;
self->uncompressSize=uncompressSize;
self->alloc_base.Free=__lzma2_dec_Free;
}
static size_t _lzma2_TStream_getReservedMemSize(lzma2_TStream* self){
SRes res;
hpi_size_t rlen=1;
if (!(self->readCode(self->codeStream,&self->prop,&rlen)&&(rlen==1)))
return 0;//error
self->alloc_base.Alloc=__lzma2_dec_Alloc_sumSize;
res=Lzma2Dec_Allocate(&self->decEnv,self->prop,&self->alloc_base);
memset(&self->decEnv,0,sizeof(self->decEnv));
if (SZ_OK!=res)
return 0;//error
return self->reservedMemSize;
}
static hpi_BOOL _lzma2_TStream_open(lzma2_TStream* self,hpi_byte* buf,size_t bufSize){
assert(bufSize>self->reservedMemSize);
self->reservedMem=buf;
self->dec_buf=buf+self->reservedMemSize;
self->dec_buf_size=(hpi_size_t)(bufSize-self->reservedMemSize);
//self->decCopyPos=0;
self->decReadPos=self->dec_buf_size;
self->alloc_base.Alloc=__lzma2_dec_Alloc;
if (SZ_OK!=Lzma2Dec_Allocate(&self->decEnv,self->prop,&self->alloc_base))
return hpi_FALSE;//error
Lzma2Dec_Init(&self->decEnv);
return hpi_TRUE;
}
static hpi_BOOL _lzma2_TStream_close(lzma2_TStream* self){
Lzma2Dec_Free(&self->decEnv,&self->alloc_base);
return hpi_TRUE;
}
static hpi_BOOL _lzma2_TStream_decompress(hpi_TInputStreamHandle diffStream,hpi_byte* out_part_data,hpi_size_t* data_size){
hpi_byte* out_part_data_end;
unsigned char* out_cur;
hpi_BOOL isReadedEnd=hpi_FALSE;
lzma2_TStream* self=(lzma2_TStream*)diffStream;
hpi_pos_t r_size=*data_size;
if (r_size>self->uncompressSize){
r_size=self->uncompressSize;
*data_size=(hpi_pos_t)r_size;
}
self->uncompressSize-=r_size;
out_part_data_end=out_part_data+r_size;
out_cur=out_part_data;
while (out_cur<out_part_data_end){
size_t copyLen=(self->decEnv.decoder.dicPos-self->decCopyPos);
if (copyLen>0){
if (copyLen>(size_t)(out_part_data_end-out_cur))
copyLen=(out_part_data_end-out_cur);
memcpy(out_cur,self->decEnv.decoder.dic+self->decCopyPos,copyLen);
out_cur+=copyLen;
self->decCopyPos+=copyLen;
if ((self->decEnv.decoder.dicPos==self->decEnv.decoder.dicBufSize)
&&(self->decEnv.decoder.dicPos==self->decCopyPos)){
self->decEnv.decoder.dicPos=0;
self->decCopyPos=0;
}
}else{
ELzmaStatus status;
SizeT inSize,dicPos_back;
SRes res;
if ((self->decReadPos==self->dec_buf_size)&(!isReadedEnd)) {
hpi_size_t readLen=self->dec_buf_size;
if (!self->readCode(self->codeStream,self->dec_buf,&readLen))
return hpi_FALSE;//error;
self->decReadPos=self->dec_buf_size-readLen;
if (self->decReadPos>0){
isReadedEnd=hpi_TRUE;
memmove(self->dec_buf+self->decReadPos,self->dec_buf,readLen);
}else{
self->decReadPos=0;
}
}
inSize=self->dec_buf_size-self->decReadPos;
dicPos_back=self->decEnv.decoder.dicPos;
res=Lzma2Dec_DecodeToDic(&self->decEnv,self->decEnv.decoder.dicBufSize,
self->dec_buf+self->decReadPos,&inSize,LZMA_FINISH_ANY,&status);
if(res==SZ_OK){
if ((inSize==0)&&(self->decEnv.decoder.dicPos==dicPos_back))
return hpi_FALSE;//error;
}else{
return hpi_FALSE;//error;
}
self->decReadPos+=inSize;
}
}
return hpi_TRUE;
}
#endif //_CompressPlugin_lzma2
#endif //hpatchi_decompresser_demo_h