Debian Codegraph @

Source/7zip/LZMADecode.c

raw
1 /*
2 * LZMADecode.c
3 *
4 * This file is a part of LZMA compression module for NSIS.
5 *
6 * Original LZMA SDK Copyright (C) 1999-2006 Igor Pavlov
7 * Modifications Copyright (C) 2003-2026 Amir Szekely <[email protected]>
8 *
9 * Licensed under the Common Public License version 1.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 *
12 * Licence details can be found in the file COPYING.
13 *
14 * This software is provided 'as-is', without any express or implied
15 * warranty.
16 *
17 * Reviewed for Unicode support by Jim Park -- 08/24/2007
18 */
19
20 #include <stdlib.h>
21 #include "LZMADecode.h"
22
23 #define LEAVE { goto saveStateAndReturn; }
24 #define NEED_BYTE(c) case c: if (!avail_in) { mode = c; LEAVE; }
25 #define NEED_BYTE_ if (!avail_in) LEAVE;
26 #define NEXT_BYTE (avail_in--, *next_in++)
27 #define NEED_OUT(c) case c: if (!avail_out) { mode = c; LEAVE; }
28 #define PUT_BYTE_(b) { *next_out = b; next_out++; avail_out--; }
29 #define PUT_BYTE(b) { totalOut++; PUT_BYTE_(b) }
30 #define DECODE_BIT(c, x) prob = x; last = c; goto _LZMA_C_RDBD; case c:
31 #define DECODE_LEN(c, x) probs = x; last2 = c; goto _LZMA_C_LEND; case c:
32 #define DECODE_BIT_TREE(c, x, y) probs = x; numLevels = y; last3 = c; goto _LZMA_C_BTD; case c:
33
34 enum {
35 /* 0 */ LZMA_C_INIT = 0,
36 /* 1 */ LZMA_C_GETDICT,
37 /* 2 */ LZMA_C_BLOCK,
38 /* 3 */ LZMA_C_RDI, /* RangeDecoderInit */
39 /* 4 */ LZMA_C_RDBD, /* RangeDecoderBitDecode */
40 /* 5 */ LZMA_C_RDBD_IN, /* RangeDecoderBitDecode */
41 /* 6 */ LZMA_C_TYPE,
42 /* 7 */ LZMA_C_ISREP,
43 /* 8 */ LZMA_C_ISREPG0,
44 /* 9 */ LZMA_C_ISREP0LONG,
45 /* 10 */ LZMA_C_ISREPG1,
46 /* 11 */ LZMA_C_ISREPG2,
47 /* 12 */ LZMA_C_NORM,
48 /* 13 */ LZMA_C_LITDM1, /* LzmaLiteralDecodeMatch */
49 /* 14 */ LZMA_C_LITDM2, /* LzmaLiteralDecodeMatch */
50 /* 15 */ LZMA_C_LITD, /* LzmaLiteralDecode */
51 /* 16 */ LZMA_C_RDRBTD, /* RangeDecoderReverseBitTreeDecode */
52 /* 17 */ LZMA_C_LEND, /* LzmaLenDecode */
53 /* 18 */ LZMA_C_LEND1, /* LzmaLenDecode */
54 /* 19 */ LZMA_C_LEND2, /* LzmaLenDecode */
55 /* 20 */ LZMA_C_LEND_RES, /* LzmaLenDecode */
56 /* 21 */ LZMA_C_LEND_C1,
57 /* 22 */ LZMA_C_LEND_C2,
58 /* 23 */ LZMA_C_BTD, /* RangeDecoderBitTreeDecode */
59 /* 24 */ LZMA_C_BTD_LOOP,
60 /* 25 */ LZMA_C_BTD_C1,
61 /* 26 */ LZMA_C_OUTPUT_1,
62 /* 27 */ LZMA_C_OUTPUT_2,
63 /* 28 */ LZMA_C_OUTPUT_3
64 };
65
66 #define kNumTopBits 24
67 #define kTopValue ((UInt32)1 << kNumTopBits)
68
69 #define kNumBitModelTotalBits 11
70 #define kBitModelTotal (1 << kNumBitModelTotalBits)
71 #define kNumMoveBits 5
72
73 #define RC_NORMALIZE(c) if (range < kTopValue) { NEED_BYTE(c); range <<= 8; code = (code << 8) | NEXT_BYTE; }
74
75 #define RC_GET_BIT2(c, prob, mi, A0, A1) { \
76 UInt32 bound = (range >> kNumBitModelTotalBits) * *prob; \
77 if (code < bound) \
78 { A0; range = bound; *prob += (kBitModelTotal - *prob) >> kNumMoveBits; mi <<= 1; } \
79 else \
80 { A1; range -= bound; code -= bound; *prob -= (*prob) >> kNumMoveBits; mi = (mi + mi) + 1; } \
81 RC_NORMALIZE(c) \
82 }
83
84 #define RC_GET_BIT(c, prob, mi) RC_GET_BIT2(c, prob, mi, ; , ;)
85
86 #define kNumPosBitsMax 4
87 #define kNumPosStatesMax (1 << kNumPosBitsMax)
88
89 #define kLenNumLowBits 3
90 #define kLenNumLowSymbols (1 << kLenNumLowBits)
91 #define kLenNumMidBits 3
92 #define kLenNumMidSymbols (1 << kLenNumMidBits)
93 #define kLenNumHighBits 8
94 #define kLenNumHighSymbols (1 << kLenNumHighBits)
95
96 #define LenChoice 0
97 #define LenChoice2 (LenChoice + 1)
98 #define LenLow (LenChoice2 + 1)
99 #define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits))
100 #define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits))
101 #define kNumLenProbs (LenHigh + kLenNumHighSymbols)
102
103 #define kNumStates 12
104
105 #define kStartPosModelIndex 4
106 #define kEndPosModelIndex 14
107 #define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
108
109 #define kNumPosSlotBits 6
110 #define kNumLenToPosStates 4
111
112 #define kNumAlignBits 4
113 #define kAlignTableSize (1 << kNumAlignBits)
114
115 #define kMatchMinLen 2
116
117 #define IsMatch 0
118 #define IsRep (IsMatch + (kNumStates << kNumPosBitsMax))
119 #define IsRepG0 (IsRep + kNumStates)
120 #define IsRepG1 (IsRepG0 + kNumStates)
121 #define IsRepG2 (IsRepG1 + kNumStates)
122 #define IsRep0Long (IsRepG2 + kNumStates)
123 #define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax))
124 #define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))
125 #define Align (SpecPos + kNumFullDistances - kEndPosModelIndex)
126 #define LenCoder (Align + kAlignTableSize)
127 #define RepLenCoder (LenCoder + kNumLenProbs)
128 #define Literal (RepLenCoder + kNumLenProbs)
129
130 #define LZMA_BASE_SIZE 1846
131 #define LZMA_LIT_SIZE 768
132
133 #if Literal != LZMA_BASE_SIZE
134 StopCompilingDueBUG
135 #endif
136
137 void LZMACALL lzmaInit(lzma_stream *s)
138 {
139 /* size of lzma_stream minus the size of the two allocated buffer pointers.
140 we don't want to lose to pointer or else we won't be able to free them. */
141 size_t i = sizeof(lzma_stream) - (sizeof(unsigned char *) * 2);
142 while (i--)
143 ((Byte *)s)[i] = 0;
144
145 s->rep0 = s->rep1 = s->rep2 = s->rep3 = 1;
146 s->range = (0xFFFFFFFF);
147 }
148
149 int LZMACALL lzmaDecode(lzma_stream *s)
150 {
151 /* restore decoder state */
152 lzma_stream _s = *s;
153
154 #define mode _s.mode
155 #define last _s.last
156 #define last2 _s.last2
157 #define last3 _s.last3
158
159 #define p (*(CProb **) &_s.dynamicData)
160 #define dynamicDataSize _s.dynamicDataSize
161
162 #define state _s.state
163 #define isPreviousMatch _s.isPreviousMatch
164 #define previousByte _s.previousByte
165 #define rep0 _s.rep0
166 #define rep1 _s.rep1
167 #define rep2 _s.rep2
168 #define rep3 _s.rep3
169 #define lc _s.lc
170 #define len _s.len
171 #define totalOut _s.totalOut
172
173 #define dictionary _s.dictionary
174 #define dictionarySize _s.dictionarySize
175 #define dictionaryPos _s.dictionaryPos
176
177 #define posStateMask _s.posStateMask
178 #define literalPosMask _s.literalPosMask
179
180 #define avail_in _s.avail_in
181 #define next_in _s.next_in
182 #define avail_out _s.avail_out
183 #define next_out _s.next_out
184
185 #define range _s.range
186 #define code _s.code
187
188 #define probs _s.probs
189 #define prob _s.prob
190
191 #define symbol _s.temp2
192 #define bit _s.temp3
193 #define matchBit _s.temp1
194 #define i _s.temp1
195 #define result _s.temp2
196 #define numLevels _s.temp3
197 #define posSlot _s.temp2
198 #define newDictionarySize (*(UInt32*) &_s.temp3)
199
200 #define matchByte _s.matchByte
201 #define mi _s.mi
202 #define posState _s.posState
203
204 if (len == -1)
205 return LZMA_STREAM_END;
206
207 for (;;) switch (mode)
208 {
209 case LZMA_C_INIT:
210 {
211 Byte firstByte;
212 UInt32 newDynamicDataSize;
213 UInt32 numProbs;
214 int lp;
215 int pb;
216
217 NEED_BYTE_;
218
219 firstByte = NEXT_BYTE;
220
221 if (firstByte > (9*5*5))
222 return LZMA_DATA_ERROR;
223
224 pb = firstByte / (9*5);
225 firstByte %= (9*5);
226 lp = firstByte / 9;
227 firstByte %= 9;
228 lc = firstByte;
229
230 posStateMask = (1 << (pb)) - 1;
231 literalPosMask = (1 << (lp)) - 1;
232
233 numProbs = Literal + (LZMA_LIT_SIZE << (lc + pb));
234 newDynamicDataSize = numProbs * sizeof(CProb);
235
236 if (newDynamicDataSize != dynamicDataSize)
237 {
238 if (p)
239 lzmafree(p);
240 p = lzmaalloc(newDynamicDataSize);
241 if (!p)
242 return LZMA_NOT_ENOUGH_MEM;
243 dynamicDataSize = newDynamicDataSize;
244 }
245
246 while (numProbs--)
247 p[numProbs] = kBitModelTotal >> 1;
248
249 for (i = 0, newDictionarySize = 0; i < 4; i++)
250 {
251 NEED_BYTE(LZMA_C_GETDICT);
252 newDictionarySize |= NEXT_BYTE << (i * 8);
253 }
254
255 if (newDictionarySize != dictionarySize)
256 {
257 dictionarySize = newDictionarySize;
258 if (dictionary)
259 lzmafree(dictionary);
260 dictionary = lzmaalloc(dictionarySize);
261 if (!dictionary)
262 return LZMA_NOT_ENOUGH_MEM;
263 }
264
265 dictionary[dictionarySize - 1] = 0;
266
267 i = 5;
268 while (i--)
269 {
270 NEED_BYTE(LZMA_C_RDI);
271 code = (code << 8) | NEXT_BYTE;
272 }
273 }
274 case LZMA_C_BLOCK:
275 posState = (int)(totalOut & posStateMask);
276 DECODE_BIT(LZMA_C_TYPE, p + IsMatch + (state << kNumPosBitsMax) + posState);
277 if (bit == 0)
278 {
279 probs = p + Literal + (LZMA_LIT_SIZE *
280 (((totalOut & literalPosMask) << lc) + (previousByte >> (8 - lc))));
281
282 if (state < 4) state = 0;
283 else if (state < 10) state -= 3;
284 else state -= 6;
285 if (isPreviousMatch)
286 {
287 UInt32 pos = dictionaryPos - rep0;
288 if (pos >= dictionarySize)
289 pos += dictionarySize;
290 matchByte = dictionary[pos];
291 {
292 symbol = 1;
293 do
294 {
295 matchBit = (matchByte >> 7) & 1;
296 matchByte <<= 1;
297 {
298 prob = probs + ((1 + matchBit) << 8) + symbol;
299 RC_GET_BIT2(LZMA_C_LITDM1, prob, symbol, bit = 0, bit = 1)
300 }
301 if (matchBit != bit)
302 {
303 while (symbol < 0x100)
304 {
305 prob = probs + symbol;
306 RC_GET_BIT(LZMA_C_LITDM2, prob, symbol)
307 }
308 break;
309 }
310 }
311 while (symbol < 0x100);
312 previousByte = symbol;
313 }
314 isPreviousMatch = 0;
315 }
316 else
317 {
318 symbol = 1;
319 do
320 {
321 prob = probs + symbol;
322 RC_GET_BIT(LZMA_C_LITD, prob, symbol)
323 }
324 while (symbol < 0x100);
325 previousByte = symbol;
326 }
327 NEED_OUT(LZMA_C_OUTPUT_1);
328 PUT_BYTE(previousByte);
329 dictionary[dictionaryPos] = previousByte;
330 dictionaryPos = (dictionaryPos + 1) % dictionarySize;
331 }
332 /* bit == 1 */
333 else
334 {
335 isPreviousMatch = 1;
336 DECODE_BIT(LZMA_C_ISREP, p + IsRep + state);
337 if (bit == 1)
338 {
339 DECODE_BIT(LZMA_C_ISREPG0, p + IsRepG0 + state);
340 if (bit == 0)
341 {
342 DECODE_BIT(LZMA_C_ISREP0LONG, p + IsRep0Long + (state << kNumPosBitsMax) + posState);
343 if (bit == 0)
344 {
345 UInt32 pos;
346 if (totalOut == 0)
347 return LZMA_DATA_ERROR;
348 state = state < 7 ? 9 : 11;
349 NEED_OUT(LZMA_C_OUTPUT_2);
350 pos = dictionaryPos - rep0;
351 if (pos >= dictionarySize)
352 pos += dictionarySize;
353 previousByte = dictionary[pos];
354 dictionary[dictionaryPos] = previousByte;
355 dictionaryPos = (dictionaryPos + 1) % dictionarySize;
356 PUT_BYTE(previousByte);
357 mode = LZMA_C_BLOCK;
358 break;
359 }
360 }
361 else
362 {
363 UInt32 distance;
364 DECODE_BIT(LZMA_C_ISREPG1, p + IsRepG1 + state);
365 if (bit == 0)
366 {
367 distance = rep1;
368 }
369 else
370 {
371 DECODE_BIT(LZMA_C_ISREPG2, p + IsRepG2 + state);
372 if (bit == 0)
373 distance = rep2;
374 else
375 {
376 distance = rep3;
377 rep3 = rep2;
378 }
379 rep2 = rep1;
380 }
381 rep1 = rep0;
382 rep0 = distance;
383 }
384 DECODE_LEN(LZMA_C_LEND_C1, p + RepLenCoder);
385 state = state < 7 ? 8 : 11;
386 }
387 else
388 {
389 rep3 = rep2;
390 rep2 = rep1;
391 rep1 = rep0;
392 state = state < 7 ? 7 : 10;
393 DECODE_LEN(LZMA_C_LEND_C2, p + LenCoder);
394 DECODE_BIT_TREE(
395 LZMA_C_BTD_C1,
396 p + PosSlot + ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) << kNumPosSlotBits),
397 kNumPosSlotBits
398 );
399 if (posSlot >= kStartPosModelIndex)
400 {
401 int numDirectBits = ((posSlot >> 1) - 1);
402 rep0 = ((2 | ((UInt32)posSlot & 1)) << numDirectBits);
403 if (posSlot < kEndPosModelIndex)
404 {
405 probs = p + SpecPos + rep0 - posSlot - 1;
406 numLevels = numDirectBits;
407 }
408 else
409 {
410 int numTotalBits = numDirectBits - kNumAlignBits;
411 result = 0;
412 for (i = numTotalBits; i > 0; i--)
413 {
414 /* UInt32 t; */
415 range >>= 1;
416
417 result <<= 1;
418 if (code >= range)
419 {
420 code -= range;
421 result |= 1;
422 }
423 /*
424 t = (code - range) >> 31;
425 t &= 1;
426 code -= range & (t - 1);
427 result = (result + result) | (1 - t);
428 */
429 RC_NORMALIZE(LZMA_C_NORM)
430 }
431 rep0 += result << kNumAlignBits;
432 probs = p + Align;
433 numLevels = kNumAlignBits;
434 }
435 mi = 1;
436 symbol = 0;
437 for(i = 0; i < numLevels; i++)
438 {
439 prob = probs + mi;
440 RC_GET_BIT2(LZMA_C_RDRBTD, prob, mi, ; , symbol |= (1 << i));
441 }
442 rep0 += symbol;
443 }
444 else
445 rep0 = posSlot;
446 rep0++;
447 }
448 if (rep0 == (UInt32)(0))
449 {
450 len = -1;
451 LEAVE;
452 }
453 if (rep0 > totalOut)
454 {
455 return LZMA_DATA_ERROR;
456 }
457 len += kMatchMinLen;
458 totalOut += len;
459 do
460 {
461 UInt32 pos;
462 NEED_OUT(LZMA_C_OUTPUT_3);
463 pos = dictionaryPos - rep0;
464 if (pos >= dictionarySize)
465 pos += dictionarySize;
466 previousByte = dictionary[pos];
467 dictionary[dictionaryPos] = previousByte;
468 dictionaryPos = (dictionaryPos + 1) % dictionarySize;
469 PUT_BYTE_(previousByte);
470 len--;
471 }
472 while(len > 0);
473 }
474 mode = LZMA_C_BLOCK;
475 break;
476 case LZMA_C_RDBD:
477 _LZMA_C_RDBD:
478 {
479 UInt32 bound = (range >> kNumBitModelTotalBits) * *prob;
480 if (code < bound)
481 {
482 range = bound;
483 *prob += (kBitModelTotal - *prob) >> kNumMoveBits;
484 bit = 0;
485 }
486 else
487 {
488 range -= bound;
489 code -= bound;
490 *prob -= (*prob) >> kNumMoveBits;
491 bit = 1;
492 }
493 RC_NORMALIZE(LZMA_C_RDBD_IN);
494 }
495 mode = last;
496 break;
497 case LZMA_C_LEND:
498 _LZMA_C_LEND:
499 DECODE_BIT(LZMA_C_LEND1, probs + LenChoice);
500 if (bit == 0)
501 {
502 len = 0;
503 probs += LenLow + (posState << kLenNumLowBits);
504 numLevels = kLenNumLowBits;
505 }
506 else {
507 DECODE_BIT(LZMA_C_LEND2, probs + LenChoice2);
508 if (bit == 0)
509 {
510 len = kLenNumLowSymbols;
511 probs += + LenMid + (posState << kLenNumMidBits);
512 numLevels = kLenNumMidBits;
513 }
514 else
515 {
516 len = kLenNumLowSymbols + kLenNumMidSymbols;
517 probs += LenHigh;
518 numLevels = kLenNumHighBits;
519 }
520 }
521
522 last3 = LZMA_C_LEND_RES;
523 case LZMA_C_BTD:
524 _LZMA_C_BTD:
525 mi = 1;
526 for(i = numLevels; i > 0; i--)
527 {
528 prob = probs + mi;
529 RC_GET_BIT(LZMA_C_BTD_LOOP, prob, mi)
530 }
531 result = mi - (1 << numLevels);
532 mode = last3;
533 break;
534 case LZMA_C_LEND_RES:
535 len += result;
536 mode = last2;
537 break;
538 default:
539 return LZMA_DATA_ERROR;
540 }
541
542 saveStateAndReturn:
543
544 /* save decoder state */
545 *s = _s;
546
547 return LZMA_OK;
548 }