-
Notifications
You must be signed in to change notification settings - Fork 8
/
lzmat_dec.c
342 lines (325 loc) · 8.05 KB
/
lzmat_dec.c
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
/*
** $Id: lzmat_dec.c,v 1.1 2008/07/08 16:58:35 Vitaly Exp $
** $Revision: 1.1 $
** $Date: 2008/07/08 16:58:35 $
**
** $Author: Vitaly $
**
***************************************************************************
** LZMAT ANSI-C decoder 1.01
** Copyright (C) 2007,2008 Vitaly Evseenko. All Rights Reserved.
** lzmat_dec.c
**
** This file is part of the LZMAT real-time data compression library.
**
** The LZMAT library is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License as
** published by the Free Software Foundation; either version 2 of
** the License, or (at your option) any later version.
**
** The LZMAT library is distributed WITHOUT ANY WARRANTY;
** without even the implied warranty of MERCHANTABILITY or
** FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
** License for more details.
**
** You should have received a copy of the GNU General Public License
** along with the LZMAT library; see the file GPL.TXT.
** If not, write to the Free Software Foundation, Inc.,
** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
** Vitaly Evseenko
** <ve@matcode.com>
** http://www.matcode.com/lzmat.htm
***************************************************************************
*/
#include "lzmat.h"
#define LZMAT_SAVE
#define LZMAT_DEFAULT_CNT (0x12)
#define LZMAT_1BYTE_CNT (0xFF + LZMAT_DEFAULT_CNT)
#define LZMAT_2BYTE_CNT (0xFFFF + LZMAT_1BYTE_CNT)
#define LZMAT_MAX_2BYTE_CNT (LZMAT_2BYTE_CNT-1)
#define LZMAT_GET_U4(_p_,_i_,_n_) \
((_n_^=1)?(((MP_U8 *)(_p_))[_i_]&0xF):(((MP_U8 *)(_p_))[_i_++]>>4))
#define LZMAT_GET_U8(_p_,_n_) (MP_U8)(((_n_)?((((MP_U8 *)(_p_))[0]>>4)|(((MP_U8 *)(_p_))[1]<<4)):((MP_U8 *)(_p_))[0]))
#define LZMAT_GET_LE16(_p_,_n_) \
(MP_U16)((_n_)?((((MP_U8 *)(_p_))[0]>>4)|((MP_U16)(GET_LE16((_p_)+1))<<4)):GET_LE16(_p_))
#define MAX_LZMAT_SHORT_DIST0 0x80
#define MAX_LZMAT_SHORT_DIST1 (0x800|MAX_LZMAT_SHORT_DIST0)
#define MAX_LZMAT_LONG_DIST0 0x40
#define MAX_LZMAT_LONG_DIST1 (0x400|MAX_LZMAT_LONG_DIST0)
#define MAX_LZMAT_LONG_DIST2 (0x4000|MAX_LZMAT_LONG_DIST1)
#define MAX_LZMAT_LONG_DIST3 (0x40000|MAX_LZMAT_LONG_DIST2)
#define MAX_LZMAT_GAMMA_DIST (MAX_LZMAT_LONG_DIST3-1)
#define LZMAT_DIST_MSK0 0x3F
#define LZMAT_DIST_MSK1 0x3FF
#ifdef LZMAT_SAVE
int LZMAT_CALLCONV lzmat_decode(MP_U8 *pbOut, MP_U32 *pcbOut,
MP_U8 *pbIn, MP_U32 cbIn)
{
MP_U32 inPos, outPos;
MP_U32 cbOutBuf = *pcbOut;
MP_U8 cur_nib;
*pbOut = *pbIn;
for(inPos=1, outPos=1, cur_nib=0; inPos<(cbIn-cur_nib);)
{
int bc;
MP_U8 tag;
tag = LZMAT_GET_U8(pbIn+inPos,cur_nib);
inPos++;
for(bc=0; bc<8 && inPos<(cbIn-cur_nib) && outPos<cbOutBuf; bc++, tag<<=1)
{
if(tag&0x80) // gamma
{
MP_U32 r_pos, r_cnt, dist;
#define cflag r_cnt
cflag = LZMAT_GET_LE16(pbIn+inPos,cur_nib);
inPos++;
if(outPos>MAX_LZMAT_SHORT_DIST1)
{
dist = cflag>>2;
switch(cflag&3)
{
case 0:
dist=(dist&LZMAT_DIST_MSK0)+1;
break;
case 1:
inPos+=cur_nib;
dist = (dist&LZMAT_DIST_MSK1)+0x41;
cur_nib^=1;
break;
case 2:
inPos++;
dist += 0x441;
break;
case 3:
if((inPos+2+cur_nib)>cbIn)
return LZMAT_STATUS_INTEGRITY_FAILURE+1;
inPos++;
dist = (dist +
((MP_U32)LZMAT_GET_U4(pbIn,inPos,cur_nib)<<14))
+0x4441;
break;
}
}
else
{
dist = cflag>>1;
if(cflag&1)
{
inPos+=cur_nib;
dist = (dist&0x7FF)+0x81;
cur_nib^=1;
}
else
dist = (dist&0x7F)+1;
}
#undef cflag
r_cnt = LZMAT_GET_U4(pbIn,inPos,cur_nib);
if(r_cnt!=0xF)
{
r_cnt += 3;
}
else
{
if((inPos+1+cur_nib)>cbIn)
return LZMAT_STATUS_INTEGRITY_FAILURE+2;
r_cnt = LZMAT_GET_U8(pbIn+inPos,cur_nib);
inPos++;
if(r_cnt!=0xFF)
{
r_cnt += LZMAT_DEFAULT_CNT;
}
else
{
if((inPos+2+cur_nib)>cbIn)
return LZMAT_STATUS_INTEGRITY_FAILURE+3;
r_cnt = LZMAT_GET_LE16(pbIn+inPos,cur_nib)+LZMAT_1BYTE_CNT;
inPos+=2;
if(r_cnt==LZMAT_2BYTE_CNT)
{
// copy chunk
if(cur_nib)
{
r_cnt = ((MP_U32)pbIn[inPos-4]&0xFC)<<5;
inPos++;
cur_nib = 0;
}
else
{
r_cnt = (GET_LE16(pbIn+inPos-5)&0xFC0)<<1;
}
r_cnt+=(tag&0x7F)+4;
r_cnt<<=1;
if((outPos+(r_cnt<<2))>cbOutBuf)
return LZMAT_STATUS_BUFFER_TOO_SMALL;
while(r_cnt-- && outPos<cbOutBuf)
{
*(MP_U32 *)(pbOut+outPos)=*(MP_U32 *)(pbIn+inPos);
inPos+=4;
outPos+=4;
}
break;
}
}
}
if(outPos<dist)
return LZMAT_STATUS_INTEGRITY_FAILURE+4;
if((outPos+r_cnt)>cbOutBuf)
return LZMAT_STATUS_BUFFER_TOO_SMALL+1;
r_pos = outPos-dist;
while(r_cnt-- && outPos<cbOutBuf)
pbOut[outPos++]=pbOut[r_pos++];
}
else
{
pbOut[outPos++]=LZMAT_GET_U8(pbIn+inPos,cur_nib);
inPos++;
}
}
}
*pcbOut = outPos;
return LZMAT_STATUS_OK;
}
#else //!LZMAT_SAVE
#ifdef mkstub
int __fastcall lzmat_decode(MP_U8 *pbOut, MP_U8 *pbIn, MP_U32 cbIn)
#else //!mkstub
int LZMAT_CALLCONV lzmat_decode(MP_U8 *pbOut, MP_U32 *pcbOut,
MP_U8 *pbIn, MP_U32 cbIn)
#endif
{
MP_U32 inPos, outPos;
MP_U8 cur_nib;
#ifndef mkstub
MP_U32 cbOutBuf = *pcbOut;
#endif
*pbOut = *pbIn;
for(inPos=1, outPos=1, cur_nib=0; inPos<(cbIn-cur_nib);)
{
int bc;
MP_U8 tag;
tag = LZMAT_GET_U8(pbIn+inPos,cur_nib);
inPos++;
#ifdef mkstub
for(bc=0; bc<8 && inPos<(cbIn-cur_nib); bc++, tag<<=1)
#else //!mkstub
for(bc=0; bc<8 && inPos<(cbIn-cur_nib) && outPos<cbOutBuf; bc++, tag<<=1)
#endif
{
if(tag&0x80) // gamma
{
MP_U32 r_pos, r_cnt, dist;
#define cflag r_cnt
cflag = LZMAT_GET_LE20_UNSAVE(pbIn+inPos,cur_nib);
inPos++;
if(outPos<0x881)
{
dist = cflag>>1;
if(cflag&1)
{
inPos+=cur_nib;
dist = (dist&0x7FF)+0x81;
cur_nib^=1;
}
else
dist = (dist&0x7F)+1;
}
else
{
dist = cflag>>2;
switch(cflag&3)
{
case 0:
dist=(dist&0x3F)+1;
break;
case 1:
inPos+=cur_nib;
dist = (dist&0x3FF)+0x41;
cur_nib^=1;
break;
case 2:
dist = (dist&0x3FFF)+0x441;
inPos++;
break;
case 3:
inPos+=(1+cur_nib);
dist = (dist&0x3FFFF)+0x4441;
cur_nib^=1;
break;
}
}
#undef cflag
r_cnt = LZMAT_GET_LE12_UNSAVE(pbIn+inPos,cur_nib);
inPos+=cur_nib;
cur_nib^=1;
if((r_cnt&0xF)!=0xF)
{
r_cnt = (r_cnt&0xF)+3;
}
else
{
inPos++;
if(r_cnt!=0xFFF)
{
//r_cnt = LZMAT_GET_U8(pbIn+inPos-1,cur_nib)+0x12;
r_cnt=(r_cnt>>4)+0x12;
}
else
{
r_cnt = LZMAT_GET_LE16_UNSAVE(pbIn+inPos,cur_nib)+0x111;
inPos+=2;
if(r_cnt==LZMAT_2BYTE_CNT)
{
// copy chunk
if(cur_nib)
{
r_cnt = ((MP_U32)pbIn[inPos-4]&0xFC)<<5;
inPos++;
cur_nib = 0;
}
else
{
r_cnt = (GET_LE16(pbIn+inPos-5)&0xFC0)<<1;
//((MP_U32)(pbIn[inPos-5]&0xC0)+(pbIn[inPos-4]<<4))<<1;
}
r_cnt+=(tag&0x7F)+4;
r_cnt<<=1;
#ifdef mkstub
while(r_cnt--)
#else //!mkstub
while(r_cnt-- && outPos<cbOutBuf)
#endif
{
*(MP_U32 *)(pbOut+outPos)=*(MP_U32 *)(pbIn+inPos);
inPos+=4;
outPos+=4;
}
break;
}
}
}
r_pos = outPos-dist;
#ifdef mkstub
while(r_cnt--)
#else //!mkstub
while(r_cnt-- && outPos<cbOutBuf)
#endif
pbOut[outPos++]=pbOut[r_pos++];
}
else
{
pbOut[outPos++]=LZMAT_GET_U8(pbIn+inPos,cur_nib);
inPos++;
}
}
}
#ifdef mkstub
return outPos;
#else //!mkstub
if(inPos<(cbIn-cur_nib))
return -1;
*pcbOut = outPos;
return LZMAT_STATUS_OK;
#endif
}
#endif