/src/lzma-fuzz/sdk/C/LzmaDec.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* LzmaDec.c -- LZMA Decoder |
2 | | 2018-07-04 : Igor Pavlov : Public domain */ |
3 | | |
4 | | #include "Precomp.h" |
5 | | |
6 | | #include <string.h> |
7 | | |
8 | | /* #include "CpuArch.h" */ |
9 | | #include "LzmaDec.h" |
10 | | |
11 | 1.18G | #define kNumTopBits 24 |
12 | 1.18G | #define kTopValue ((UInt32)1 << kNumTopBits) |
13 | | |
14 | 1.83G | #define kNumBitModelTotalBits 11 |
15 | 692M | #define kBitModelTotal (1 << kNumBitModelTotalBits) |
16 | 1.14G | #define kNumMoveBits 5 |
17 | | |
18 | 56.6k | #define RC_INIT_SIZE 5 |
19 | | |
20 | 1.18G | #define NORMALIZE if (range < kTopValue) { range <<= 8; code = (code << 8) | (*buf++); } |
21 | | |
22 | 1.14G | #define IF_BIT_0(p) ttt = *(p); NORMALIZE; bound = (range >> kNumBitModelTotalBits) * (UInt32)ttt; if (code < bound) |
23 | 641M | #define UPDATE_0(p) range = bound; *(p) = (CLzmaProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits)); |
24 | 501M | #define UPDATE_1(p) range -= bound; code -= bound; *(p) = (CLzmaProb)(ttt - (ttt >> kNumMoveBits)); |
25 | 931M | #define GET_BIT2(p, i, A0, A1) IF_BIT_0(p) \ |
26 | 931M | { UPDATE_0(p); i = (i + i); A0; } else \ |
27 | 931M | { UPDATE_1(p); i = (i + i) + 1; A1; } |
28 | | |
29 | 815M | #define TREE_GET_BIT(probs, i) { GET_BIT2(probs + i, i, ;, ;); } |
30 | | |
31 | 22.7M | #define REV_BIT(p, i, A0, A1) IF_BIT_0(p + i) \ |
32 | 22.7M | { UPDATE_0(p + i); A0; } else \ |
33 | 22.7M | { UPDATE_1(p + i); A1; } |
34 | 4.34M | #define REV_BIT_VAR( p, i, m) REV_BIT(p, i, i += m; m += m, m += m; i += m; ) |
35 | 13.8M | #define REV_BIT_CONST(p, i, m) REV_BIT(p, i, i += m; , i += m * 2; ) |
36 | 4.60M | #define REV_BIT_LAST( p, i, m) REV_BIT(p, i, i -= m , ; ) |
37 | | |
38 | | #define TREE_DECODE(probs, limit, i) \ |
39 | 9.68M | { i = 1; do { TREE_GET_BIT(probs, i); } while (i < limit); i -= limit; } |
40 | | |
41 | | /* #define _LZMA_SIZE_OPT */ |
42 | | |
43 | | #ifdef _LZMA_SIZE_OPT |
44 | | #define TREE_6_DECODE(probs, i) TREE_DECODE(probs, (1 << 6), i) |
45 | | #else |
46 | | #define TREE_6_DECODE(probs, i) \ |
47 | 6.37M | { i = 1; \ |
48 | 6.37M | TREE_GET_BIT(probs, i); \ |
49 | 6.37M | TREE_GET_BIT(probs, i); \ |
50 | 6.37M | TREE_GET_BIT(probs, i); \ |
51 | 6.37M | TREE_GET_BIT(probs, i); \ |
52 | 6.37M | TREE_GET_BIT(probs, i); \ |
53 | 6.37M | TREE_GET_BIT(probs, i); \ |
54 | 6.37M | i -= 0x40; } |
55 | | #endif |
56 | | |
57 | 723M | #define NORMAL_LITER_DEC TREE_GET_BIT(prob, symbol) |
58 | | #define MATCHED_LITER_DEC \ |
59 | 115M | matchByte += matchByte; \ |
60 | 115M | bit = offs; \ |
61 | 115M | offs &= matchByte; \ |
62 | 115M | probLit = prob + (offs + bit + symbol); \ |
63 | 115M | GET_BIT2(probLit, symbol, offs ^= bit; , ;) |
64 | | |
65 | | |
66 | | |
67 | 1.88M | #define NORMALIZE_CHECK if (range < kTopValue) { if (buf >= bufLimit) return DUMMY_ERROR; range <<= 8; code = (code << 8) | (*buf++); } |
68 | | |
69 | 1.68M | #define IF_BIT_0_CHECK(p) ttt = *(p); NORMALIZE_CHECK; bound = (range >> kNumBitModelTotalBits) * (UInt32)ttt; if (code < bound) |
70 | 857k | #define UPDATE_0_CHECK range = bound; |
71 | 827k | #define UPDATE_1_CHECK range -= bound; code -= bound; |
72 | 1.17M | #define GET_BIT2_CHECK(p, i, A0, A1) IF_BIT_0_CHECK(p) \ |
73 | 1.17M | { UPDATE_0_CHECK; i = (i + i); A0; } else \ |
74 | 1.17M | { UPDATE_1_CHECK; i = (i + i) + 1; A1; } |
75 | 935k | #define GET_BIT_CHECK(p, i) GET_BIT2_CHECK(p, i, ; , ;) |
76 | | #define TREE_DECODE_CHECK(probs, limit, i) \ |
77 | 424k | { i = 1; do { GET_BIT_CHECK(probs + i, i) } while (i < limit); i -= limit; } |
78 | | |
79 | | |
80 | 40.4k | #define REV_BIT_CHECK(p, i, m) IF_BIT_0_CHECK(p + i) \ |
81 | 40.4k | { UPDATE_0_CHECK; i += m; m += m; } else \ |
82 | 40.4k | { UPDATE_1_CHECK; m += m; i += m; } |
83 | | |
84 | | |
85 | 1.01G | #define kNumPosBitsMax 4 |
86 | 564M | #define kNumPosStatesMax (1 << kNumPosBitsMax) |
87 | | |
88 | 568M | #define kLenNumLowBits 3 |
89 | 1.51M | #define kLenNumLowSymbols (1 << kLenNumLowBits) |
90 | 563M | #define kLenNumHighBits 8 |
91 | 563M | #define kLenNumHighSymbols (1 << kLenNumHighBits) |
92 | | |
93 | 597M | #define LenLow 0 |
94 | 564M | #define LenHigh (LenLow + 2 * (kNumPosStatesMax << kLenNumLowBits)) |
95 | 563M | #define kNumLenProbs (LenHigh + kLenNumHighSymbols) |
96 | | |
97 | 15.9M | #define LenChoice LenLow |
98 | 1.96M | #define LenChoice2 (LenLow + (1 << kLenNumLowBits)) |
99 | | |
100 | 496M | #define kNumStates 12 |
101 | 448M | #define kNumStates2 16 |
102 | 131M | #define kNumLitStates 7 |
103 | | |
104 | 6.39M | #define kStartPosModelIndex 4 |
105 | 310M | #define kEndPosModelIndex 14 |
106 | 304M | #define kNumFullDistances (1 << (kEndPosModelIndex >> 1)) |
107 | | |
108 | 111M | #define kNumPosSlotBits 6 |
109 | 112M | #define kNumLenToPosStates 4 |
110 | | |
111 | 158M | #define kNumAlignBits 4 |
112 | 148M | #define kAlignTableSize (1 << kNumAlignBits) |
113 | | |
114 | 16.1M | #define kMatchMinLen 2 |
115 | 279k | #define kMatchSpecLenStart (kMatchMinLen + kLenNumLowSymbols * 2 + kLenNumHighSymbols) |
116 | | |
117 | | /* External ASM code needs same CLzmaProb array layout. So don't change it. */ |
118 | | |
119 | | /* (probs_1664) is faster and better for code size at some platforms */ |
120 | | /* |
121 | | #ifdef MY_CPU_X86_OR_AMD64 |
122 | | */ |
123 | 306M | #define kStartOffset 1664 |
124 | 354k | #define GET_PROBS p->probs_1664 |
125 | | /* |
126 | | #define GET_PROBS p->probs + kStartOffset |
127 | | #else |
128 | | #define kStartOffset 0 |
129 | | #define GET_PROBS p->probs |
130 | | #endif |
131 | | */ |
132 | | |
133 | 306M | #define SpecPos (-kStartOffset) |
134 | 304M | #define IsRep0Long (SpecPos + kNumFullDistances) |
135 | 294M | #define RepLenCoder (IsRep0Long + (kNumStates2 << kNumPosBitsMax)) |
136 | 284M | #define LenCoder (RepLenCoder + kNumLenProbs) |
137 | 278M | #define IsMatch (LenCoder + kNumLenProbs) |
138 | 153M | #define Align (IsMatch + (kNumStates2 << kNumPosBitsMax)) |
139 | 148M | #define IsRep (Align + kAlignTableSize) |
140 | 129M | #define IsRepG0 (IsRep + kNumStates) |
141 | 115M | #define IsRepG1 (IsRepG0 + kNumStates) |
142 | 112M | #define IsRepG2 (IsRepG1 + kNumStates) |
143 | 111M | #define PosSlot (IsRepG2 + kNumStates) |
144 | 104M | #define Literal (PosSlot + (kNumLenToPosStates << kNumPosSlotBits)) |
145 | 14.0k | #define NUM_BASE_PROBS (Literal + kStartOffset) |
146 | | |
147 | | #if Align != 0 && kStartOffset != 0 |
148 | | #error Stop_Compiling_Bad_LZMA_kAlign |
149 | | #endif |
150 | | |
151 | | #if NUM_BASE_PROBS != 1984 |
152 | | #error Stop_Compiling_Bad_LZMA_PROBS |
153 | | #endif |
154 | | |
155 | | |
156 | 105k | #define LZMA_LIT_SIZE 0x300 |
157 | | |
158 | 14.0k | #define LzmaProps_GetNumProbs(p) (NUM_BASE_PROBS + ((UInt32)LZMA_LIT_SIZE << ((p)->lc + (p)->lp))) |
159 | | |
160 | | |
161 | 124M | #define CALC_POS_STATE(processedPos, pbMask) (((processedPos) & (pbMask)) << 4) |
162 | 135M | #define COMBINED_PS_STATE (posState + state) |
163 | 14.7M | #define GET_LEN_STATE (posState) |
164 | | |
165 | 7.77k | #define LZMA_DIC_MIN (1 << 12) |
166 | | |
167 | | /* |
168 | | p->remainLen : shows status of LZMA decoder: |
169 | | < kMatchSpecLenStart : normal remain |
170 | | = kMatchSpecLenStart : finished |
171 | | = kMatchSpecLenStart + 1 : need init range coder |
172 | | = kMatchSpecLenStart + 2 : need init range coder and state |
173 | | */ |
174 | | |
175 | | /* ---------- LZMA_DECODE_REAL ---------- */ |
176 | | /* |
177 | | LzmaDec_DecodeReal_3() can be implemented in external ASM file. |
178 | | 3 - is the code compatibility version of that function for check at link time. |
179 | | */ |
180 | | |
181 | | #define LZMA_DECODE_REAL LzmaDec_DecodeReal_3 |
182 | | |
183 | | /* |
184 | | LZMA_DECODE_REAL() |
185 | | In: |
186 | | RangeCoder is normalized |
187 | | if (p->dicPos == limit) |
188 | | { |
189 | | LzmaDec_TryDummy() was called before to exclude LITERAL and MATCH-REP cases. |
190 | | So first symbol can be only MATCH-NON-REP. And if that MATCH-NON-REP symbol |
191 | | is not END_OF_PAYALOAD_MARKER, then function returns error code. |
192 | | } |
193 | | |
194 | | Processing: |
195 | | first LZMA symbol will be decoded in any case |
196 | | All checks for limits are at the end of main loop, |
197 | | It will decode new LZMA-symbols while (p->buf < bufLimit && dicPos < limit), |
198 | | RangeCoder is still without last normalization when (p->buf < bufLimit) is being checked. |
199 | | |
200 | | Out: |
201 | | RangeCoder is normalized |
202 | | Result: |
203 | | SZ_OK - OK |
204 | | SZ_ERROR_DATA - Error |
205 | | p->remainLen: |
206 | | < kMatchSpecLenStart : normal remain |
207 | | = kMatchSpecLenStart : finished |
208 | | */ |
209 | | |
210 | | |
211 | | #ifdef _LZMA_DEC_OPT |
212 | | |
213 | | int MY_FAST_CALL LZMA_DECODE_REAL(CLzmaDec *p, SizeT limit, const Byte *bufLimit); |
214 | | |
215 | | #else |
216 | | |
217 | | static |
218 | | int MY_FAST_CALL LZMA_DECODE_REAL(CLzmaDec *p, SizeT limit, const Byte *bufLimit) |
219 | 183k | { |
220 | 183k | CLzmaProb *probs = GET_PROBS; |
221 | 183k | unsigned state = (unsigned)p->state; |
222 | 183k | UInt32 rep0 = p->reps[0], rep1 = p->reps[1], rep2 = p->reps[2], rep3 = p->reps[3]; |
223 | 183k | unsigned pbMask = ((unsigned)1 << (p->prop.pb)) - 1; |
224 | 183k | unsigned lc = p->prop.lc; |
225 | 183k | unsigned lpMask = ((unsigned)0x100 << p->prop.lp) - ((unsigned)0x100 >> lc); |
226 | | |
227 | 183k | Byte *dic = p->dic; |
228 | 183k | SizeT dicBufSize = p->dicBufSize; |
229 | 183k | SizeT dicPos = p->dicPos; |
230 | | |
231 | 183k | UInt32 processedPos = p->processedPos; |
232 | 183k | UInt32 checkDicSize = p->checkDicSize; |
233 | 183k | unsigned len = 0; |
234 | | |
235 | 183k | const Byte *buf = p->buf; |
236 | 183k | UInt32 range = p->range; |
237 | 183k | UInt32 code = p->code; |
238 | | |
239 | 183k | do |
240 | 124M | { |
241 | 124M | CLzmaProb *prob; |
242 | 124M | UInt32 bound; |
243 | 124M | unsigned ttt; |
244 | 124M | unsigned posState = CALC_POS_STATE(processedPos, pbMask); |
245 | | |
246 | 124M | prob = probs + IsMatch + COMBINED_PS_STATE; |
247 | 124M | IF_BIT_0(prob) |
248 | 104M | { |
249 | 104M | unsigned symbol; |
250 | 104M | UPDATE_0(prob); |
251 | 104M | prob = probs + Literal; |
252 | 104M | if (processedPos != 0 || checkDicSize != 0) |
253 | 104M | prob += (UInt32)3 * ((((processedPos << 8) + dic[(dicPos == 0 ? dicBufSize : dicPos) - 1]) & lpMask) << lc); |
254 | 104M | processedPos++; |
255 | | |
256 | 104M | if (state < kNumLitStates) |
257 | 90.4M | { |
258 | 90.4M | state -= (state < 4) ? state : 3; |
259 | 90.4M | symbol = 1; |
260 | | #ifdef _LZMA_SIZE_OPT |
261 | | do { NORMAL_LITER_DEC } while (symbol < 0x100); |
262 | | #else |
263 | 90.4M | NORMAL_LITER_DEC |
264 | 90.4M | NORMAL_LITER_DEC |
265 | 90.4M | NORMAL_LITER_DEC |
266 | 90.4M | NORMAL_LITER_DEC |
267 | 90.4M | NORMAL_LITER_DEC |
268 | 90.4M | NORMAL_LITER_DEC |
269 | 90.4M | NORMAL_LITER_DEC |
270 | 90.4M | NORMAL_LITER_DEC |
271 | 90.4M | #endif |
272 | 90.4M | } |
273 | 14.3M | else |
274 | 14.3M | { |
275 | 14.3M | unsigned matchByte = dic[dicPos - rep0 + (dicPos < rep0 ? dicBufSize : 0)]; |
276 | 14.3M | unsigned offs = 0x100; |
277 | 14.3M | state -= (state < 10) ? 3 : 6; |
278 | 14.3M | symbol = 1; |
279 | | #ifdef _LZMA_SIZE_OPT |
280 | | do |
281 | | { |
282 | | unsigned bit; |
283 | | CLzmaProb *probLit; |
284 | | MATCHED_LITER_DEC |
285 | | } |
286 | | while (symbol < 0x100); |
287 | | #else |
288 | 14.3M | { |
289 | 14.3M | unsigned bit; |
290 | 14.3M | CLzmaProb *probLit; |
291 | 14.3M | MATCHED_LITER_DEC |
292 | 14.3M | MATCHED_LITER_DEC |
293 | 14.3M | MATCHED_LITER_DEC |
294 | 14.3M | MATCHED_LITER_DEC |
295 | 14.3M | MATCHED_LITER_DEC |
296 | 14.3M | MATCHED_LITER_DEC |
297 | 14.3M | MATCHED_LITER_DEC |
298 | 14.3M | MATCHED_LITER_DEC |
299 | 14.3M | } |
300 | 14.3M | #endif |
301 | 14.3M | } |
302 | | |
303 | 104M | dic[dicPos++] = (Byte)symbol; |
304 | 104M | continue; |
305 | 104M | } |
306 | | |
307 | 19.8M | { |
308 | 19.8M | UPDATE_1(prob); |
309 | 19.8M | prob = probs + IsRep + state; |
310 | 19.8M | IF_BIT_0(prob) |
311 | 6.37M | { |
312 | 6.37M | UPDATE_0(prob); |
313 | 6.37M | state += kNumStates; |
314 | 6.37M | prob = probs + LenCoder; |
315 | 6.37M | } |
316 | 13.4M | else |
317 | 13.4M | { |
318 | 13.4M | UPDATE_1(prob); |
319 | | /* |
320 | | // that case was checked before with kBadRepCode |
321 | | if (checkDicSize == 0 && processedPos == 0) |
322 | | return SZ_ERROR_DATA; |
323 | | */ |
324 | 13.4M | prob = probs + IsRepG0 + state; |
325 | 13.4M | IF_BIT_0(prob) |
326 | 10.1M | { |
327 | 10.1M | UPDATE_0(prob); |
328 | 10.1M | prob = probs + IsRep0Long + COMBINED_PS_STATE; |
329 | 10.1M | IF_BIT_0(prob) |
330 | 3.99M | { |
331 | 3.99M | UPDATE_0(prob); |
332 | 3.99M | dic[dicPos] = dic[dicPos - rep0 + (dicPos < rep0 ? dicBufSize : 0)]; |
333 | 3.99M | dicPos++; |
334 | 3.99M | processedPos++; |
335 | 3.99M | state = state < kNumLitStates ? 9 : 11; |
336 | 3.99M | continue; |
337 | 3.99M | } |
338 | 6.19M | UPDATE_1(prob); |
339 | 6.19M | } |
340 | 3.30M | else |
341 | 3.30M | { |
342 | 3.30M | UInt32 distance; |
343 | 3.30M | UPDATE_1(prob); |
344 | 3.30M | prob = probs + IsRepG1 + state; |
345 | 3.30M | IF_BIT_0(prob) |
346 | 2.56M | { |
347 | 2.56M | UPDATE_0(prob); |
348 | 2.56M | distance = rep1; |
349 | 2.56M | } |
350 | 743k | else |
351 | 743k | { |
352 | 743k | UPDATE_1(prob); |
353 | 743k | prob = probs + IsRepG2 + state; |
354 | 743k | IF_BIT_0(prob) |
355 | 460k | { |
356 | 460k | UPDATE_0(prob); |
357 | 460k | distance = rep2; |
358 | 460k | } |
359 | 283k | else |
360 | 283k | { |
361 | 283k | UPDATE_1(prob); |
362 | 283k | distance = rep3; |
363 | 283k | rep3 = rep2; |
364 | 283k | } |
365 | 743k | rep2 = rep1; |
366 | 743k | } |
367 | 3.30M | rep1 = rep0; |
368 | 3.30M | rep0 = distance; |
369 | 3.30M | } |
370 | 9.49M | state = state < kNumLitStates ? 8 : 11; |
371 | 9.49M | prob = probs + RepLenCoder; |
372 | 9.49M | } |
373 | | |
374 | | #ifdef _LZMA_SIZE_OPT |
375 | | { |
376 | | unsigned lim, offset; |
377 | | CLzmaProb *probLen = prob + LenChoice; |
378 | | IF_BIT_0(probLen) |
379 | | { |
380 | | UPDATE_0(probLen); |
381 | | probLen = prob + LenLow + GET_LEN_STATE; |
382 | | offset = 0; |
383 | | lim = (1 << kLenNumLowBits); |
384 | | } |
385 | | else |
386 | | { |
387 | | UPDATE_1(probLen); |
388 | | probLen = prob + LenChoice2; |
389 | | IF_BIT_0(probLen) |
390 | | { |
391 | | UPDATE_0(probLen); |
392 | | probLen = prob + LenLow + GET_LEN_STATE + (1 << kLenNumLowBits); |
393 | | offset = kLenNumLowSymbols; |
394 | | lim = (1 << kLenNumLowBits); |
395 | | } |
396 | | else |
397 | | { |
398 | | UPDATE_1(probLen); |
399 | | probLen = prob + LenHigh; |
400 | | offset = kLenNumLowSymbols * 2; |
401 | | lim = (1 << kLenNumHighBits); |
402 | | } |
403 | | } |
404 | | TREE_DECODE(probLen, lim, len); |
405 | | len += offset; |
406 | | } |
407 | | #else |
408 | 15.8M | { |
409 | 15.8M | CLzmaProb *probLen = prob + LenChoice; |
410 | 15.8M | IF_BIT_0(probLen) |
411 | 13.9M | { |
412 | 13.9M | UPDATE_0(probLen); |
413 | 13.9M | probLen = prob + LenLow + GET_LEN_STATE; |
414 | 13.9M | len = 1; |
415 | 13.9M | TREE_GET_BIT(probLen, len); |
416 | 13.9M | TREE_GET_BIT(probLen, len); |
417 | 13.9M | TREE_GET_BIT(probLen, len); |
418 | 13.9M | len -= 8; |
419 | 13.9M | } |
420 | 1.93M | else |
421 | 1.93M | { |
422 | 1.93M | UPDATE_1(probLen); |
423 | 1.93M | probLen = prob + LenChoice2; |
424 | 1.93M | IF_BIT_0(probLen) |
425 | 723k | { |
426 | 723k | UPDATE_0(probLen); |
427 | 723k | probLen = prob + LenLow + GET_LEN_STATE + (1 << kLenNumLowBits); |
428 | 723k | len = 1; |
429 | 723k | TREE_GET_BIT(probLen, len); |
430 | 723k | TREE_GET_BIT(probLen, len); |
431 | 723k | TREE_GET_BIT(probLen, len); |
432 | 723k | } |
433 | 1.21M | else |
434 | 1.21M | { |
435 | 1.21M | UPDATE_1(probLen); |
436 | 1.21M | probLen = prob + LenHigh; |
437 | 1.21M | TREE_DECODE(probLen, (1 << kLenNumHighBits), len); |
438 | 1.21M | len += kLenNumLowSymbols * 2; |
439 | 1.21M | } |
440 | 1.93M | } |
441 | 15.8M | } |
442 | 15.8M | #endif |
443 | | |
444 | 15.8M | if (state >= kNumStates) |
445 | 6.37M | { |
446 | 6.37M | UInt32 distance; |
447 | 6.37M | prob = probs + PosSlot + |
448 | 6.37M | ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) << kNumPosSlotBits); |
449 | 6.37M | TREE_6_DECODE(prob, distance); |
450 | 6.37M | if (distance >= kStartPosModelIndex) |
451 | 6.03M | { |
452 | 6.03M | unsigned posSlot = (unsigned)distance; |
453 | 6.03M | unsigned numDirectBits = (unsigned)(((distance >> 1) - 1)); |
454 | 6.03M | distance = (2 | (distance & 1)); |
455 | 6.03M | if (posSlot < kEndPosModelIndex) |
456 | 1.42M | { |
457 | 1.42M | distance <<= numDirectBits; |
458 | 1.42M | prob = probs + SpecPos; |
459 | 1.42M | { |
460 | 1.42M | UInt32 m = 1; |
461 | 1.42M | distance++; |
462 | 1.42M | do |
463 | 4.34M | { |
464 | 4.34M | REV_BIT_VAR(prob, distance, m); |
465 | 4.34M | } |
466 | 4.34M | while (--numDirectBits); |
467 | 1.42M | distance -= m; |
468 | 1.42M | } |
469 | 1.42M | } |
470 | 4.60M | else |
471 | 4.60M | { |
472 | 4.60M | numDirectBits -= kNumAlignBits; |
473 | 4.60M | do |
474 | 37.3M | { |
475 | 37.3M | NORMALIZE |
476 | 37.3M | range >>= 1; |
477 | | |
478 | 37.3M | { |
479 | 37.3M | UInt32 t; |
480 | 37.3M | code -= range; |
481 | 37.3M | t = (0 - ((UInt32)code >> 31)); /* (UInt32)((Int32)code >> 31) */ |
482 | 37.3M | distance = (distance << 1) + (t + 1); |
483 | 37.3M | code += range & t; |
484 | 37.3M | } |
485 | | /* |
486 | | distance <<= 1; |
487 | | if (code >= range) |
488 | | { |
489 | | code -= range; |
490 | | distance |= 1; |
491 | | } |
492 | | */ |
493 | 37.3M | } |
494 | 37.3M | while (--numDirectBits); |
495 | 4.60M | prob = probs + Align; |
496 | 4.60M | distance <<= kNumAlignBits; |
497 | 4.60M | { |
498 | 4.60M | unsigned i = 1; |
499 | 4.60M | REV_BIT_CONST(prob, i, 1); |
500 | 4.60M | REV_BIT_CONST(prob, i, 2); |
501 | 4.60M | REV_BIT_CONST(prob, i, 4); |
502 | 4.60M | REV_BIT_LAST (prob, i, 8); |
503 | 4.60M | distance |= i; |
504 | 4.60M | } |
505 | 4.60M | if (distance == (UInt32)0xFFFFFFFF) |
506 | 0 | { |
507 | 0 | len = kMatchSpecLenStart; |
508 | 0 | state -= kNumStates; |
509 | 0 | break; |
510 | 0 | } |
511 | 4.60M | } |
512 | 6.03M | } |
513 | | |
514 | 6.37M | rep3 = rep2; |
515 | 6.37M | rep2 = rep1; |
516 | 6.37M | rep1 = rep0; |
517 | 6.37M | rep0 = distance + 1; |
518 | 6.37M | state = (state < kNumStates + kNumLitStates) ? kNumLitStates : kNumLitStates + 3; |
519 | 6.37M | if (distance >= (checkDicSize == 0 ? processedPos: checkDicSize)) |
520 | 0 | { |
521 | 0 | p->dicPos = dicPos; |
522 | 0 | return SZ_ERROR_DATA; |
523 | 0 | } |
524 | 6.37M | } |
525 | | |
526 | 15.8M | len += kMatchMinLen; |
527 | | |
528 | 15.8M | { |
529 | 15.8M | SizeT rem; |
530 | 15.8M | unsigned curLen; |
531 | 15.8M | SizeT pos; |
532 | | |
533 | 15.8M | if ((rem = limit - dicPos) == 0) |
534 | 0 | { |
535 | 0 | p->dicPos = dicPos; |
536 | 0 | return SZ_ERROR_DATA; |
537 | 0 | } |
538 | | |
539 | 15.8M | curLen = ((rem < len) ? (unsigned)rem : len); |
540 | 15.8M | pos = dicPos - rep0 + (dicPos < rep0 ? dicBufSize : 0); |
541 | | |
542 | 15.8M | processedPos += (UInt32)curLen; |
543 | | |
544 | 15.8M | len -= curLen; |
545 | 15.8M | if (curLen <= dicBufSize - pos) |
546 | 15.8M | { |
547 | 15.8M | Byte *dest = dic + dicPos; |
548 | 15.8M | ptrdiff_t src = (ptrdiff_t)pos - (ptrdiff_t)dicPos; |
549 | 15.8M | const Byte *lim = dest + curLen; |
550 | 15.8M | dicPos += (SizeT)curLen; |
551 | 15.8M | do |
552 | 193M | *(dest) = (Byte)*(dest + src); |
553 | 193M | while (++dest != lim); |
554 | 15.8M | } |
555 | 0 | else |
556 | 0 | { |
557 | 0 | do |
558 | 0 | { |
559 | 0 | dic[dicPos++] = dic[pos]; |
560 | 0 | if (++pos == dicBufSize) |
561 | 0 | pos = 0; |
562 | 0 | } |
563 | 0 | while (--curLen != 0); |
564 | 0 | } |
565 | 15.8M | } |
566 | 15.8M | } |
567 | 15.8M | } |
568 | 124M | while (dicPos < limit && buf < bufLimit); |
569 | | |
570 | 183k | NORMALIZE; |
571 | | |
572 | 183k | p->buf = buf; |
573 | 183k | p->range = range; |
574 | 183k | p->code = code; |
575 | 183k | p->remainLen = (UInt32)len; |
576 | 183k | p->dicPos = dicPos; |
577 | 183k | p->processedPos = processedPos; |
578 | 183k | p->reps[0] = rep0; |
579 | 183k | p->reps[1] = rep1; |
580 | 183k | p->reps[2] = rep2; |
581 | 183k | p->reps[3] = rep3; |
582 | 183k | p->state = (UInt32)state; |
583 | | |
584 | 183k | return SZ_OK; |
585 | 183k | } |
586 | | #endif |
587 | | |
588 | | static void MY_FAST_CALL LzmaDec_WriteRem(CLzmaDec *p, SizeT limit) |
589 | 202k | { |
590 | 202k | if (p->remainLen != 0 && p->remainLen < kMatchSpecLenStart) |
591 | 1.00k | { |
592 | 1.00k | Byte *dic = p->dic; |
593 | 1.00k | SizeT dicPos = p->dicPos; |
594 | 1.00k | SizeT dicBufSize = p->dicBufSize; |
595 | 1.00k | unsigned len = (unsigned)p->remainLen; |
596 | 1.00k | SizeT rep0 = p->reps[0]; /* we use SizeT to avoid the BUG of VC14 for AMD64 */ |
597 | 1.00k | SizeT rem = limit - dicPos; |
598 | 1.00k | if (rem < len) |
599 | 504 | len = (unsigned)(rem); |
600 | | |
601 | 1.00k | if (p->checkDicSize == 0 && p->prop.dicSize - p->processedPos <= len) |
602 | 0 | p->checkDicSize = p->prop.dicSize; |
603 | | |
604 | 1.00k | p->processedPos += (UInt32)len; |
605 | 1.00k | p->remainLen -= (UInt32)len; |
606 | 42.6k | while (len != 0) |
607 | 41.6k | { |
608 | 41.6k | len--; |
609 | 41.6k | dic[dicPos] = dic[dicPos - rep0 + (dicPos < rep0 ? dicBufSize : 0)]; |
610 | 41.6k | dicPos++; |
611 | 41.6k | } |
612 | 1.00k | p->dicPos = dicPos; |
613 | 1.00k | } |
614 | 202k | } |
615 | | |
616 | | |
617 | 18.2k | #define kRange0 0xFFFFFFFF |
618 | 12.1k | #define kBound0 ((kRange0 >> kNumBitModelTotalBits) << (kNumBitModelTotalBits - 1)) |
619 | 6.09k | #define kBadRepCode (kBound0 + (((kRange0 - kBound0) >> kNumBitModelTotalBits) << (kNumBitModelTotalBits - 1))) |
620 | | #if kBadRepCode != (0xC0000000 - 0x400) |
621 | | #error Stop_Compiling_Bad_LZMA_Check |
622 | | #endif |
623 | | |
624 | | static int MY_FAST_CALL LzmaDec_DecodeReal2(CLzmaDec *p, SizeT limit, const Byte *bufLimit) |
625 | 183k | { |
626 | 183k | do |
627 | 183k | { |
628 | 183k | SizeT limit2 = limit; |
629 | 183k | if (p->checkDicSize == 0) |
630 | 183k | { |
631 | 183k | UInt32 rem = p->prop.dicSize - p->processedPos; |
632 | 183k | if (limit - p->dicPos > rem) |
633 | 0 | limit2 = p->dicPos + rem; |
634 | | |
635 | 183k | if (p->processedPos == 0) |
636 | 6.09k | if (p->code >= kBadRepCode) |
637 | 0 | return SZ_ERROR_DATA; |
638 | 183k | } |
639 | | |
640 | 183k | RINOK(LZMA_DECODE_REAL(p, limit2, bufLimit)); |
641 | | |
642 | 183k | if (p->checkDicSize == 0 && p->processedPos >= p->prop.dicSize) |
643 | 0 | p->checkDicSize = p->prop.dicSize; |
644 | | |
645 | 183k | LzmaDec_WriteRem(p, limit); |
646 | 183k | } |
647 | 183k | while (p->dicPos < limit && p->buf < bufLimit && p->remainLen < kMatchSpecLenStart); |
648 | | |
649 | 183k | return 0; |
650 | 183k | } |
651 | | |
652 | | typedef enum |
653 | | { |
654 | | DUMMY_ERROR, /* unexpected end of input stream */ |
655 | | DUMMY_LIT, |
656 | | DUMMY_MATCH, |
657 | | DUMMY_REP |
658 | | } ELzmaDummy; |
659 | | |
660 | | static ELzmaDummy LzmaDec_TryDummy(const CLzmaDec *p, const Byte *buf, SizeT inSize) |
661 | 170k | { |
662 | 170k | UInt32 range = p->range; |
663 | 170k | UInt32 code = p->code; |
664 | 170k | const Byte *bufLimit = buf + inSize; |
665 | 170k | const CLzmaProb *probs = GET_PROBS; |
666 | 170k | unsigned state = (unsigned)p->state; |
667 | 170k | ELzmaDummy res; |
668 | | |
669 | 170k | { |
670 | 170k | const CLzmaProb *prob; |
671 | 170k | UInt32 bound; |
672 | 170k | unsigned ttt; |
673 | 170k | unsigned posState = CALC_POS_STATE(p->processedPos, (1 << p->prop.pb) - 1); |
674 | | |
675 | 170k | prob = probs + IsMatch + COMBINED_PS_STATE; |
676 | 170k | IF_BIT_0_CHECK(prob) |
677 | 93.5k | { |
678 | 93.5k | UPDATE_0_CHECK |
679 | | |
680 | | /* if (bufLimit - buf >= 7) return DUMMY_LIT; */ |
681 | | |
682 | 93.5k | prob = probs + Literal; |
683 | 93.5k | if (p->checkDicSize != 0 || p->processedPos != 0) |
684 | 91.0k | prob += ((UInt32)LZMA_LIT_SIZE * |
685 | 91.0k | ((((p->processedPos) & ((1 << (p->prop.lp)) - 1)) << p->prop.lc) + |
686 | 91.0k | (p->dic[(p->dicPos == 0 ? p->dicBufSize : p->dicPos) - 1] >> (8 - p->prop.lc)))); |
687 | | |
688 | 93.5k | if (state < kNumLitStates) |
689 | 64.0k | { |
690 | 64.0k | unsigned symbol = 1; |
691 | 510k | do { GET_BIT_CHECK(prob + symbol, symbol) } while (symbol < 0x100); |
692 | 64.0k | } |
693 | 29.5k | else |
694 | 29.5k | { |
695 | 29.5k | unsigned matchByte = p->dic[p->dicPos - p->reps[0] + |
696 | 29.5k | (p->dicPos < p->reps[0] ? p->dicBufSize : 0)]; |
697 | 29.5k | unsigned offs = 0x100; |
698 | 29.5k | unsigned symbol = 1; |
699 | 29.5k | do |
700 | 236k | { |
701 | 236k | unsigned bit; |
702 | 236k | const CLzmaProb *probLit; |
703 | 236k | matchByte += matchByte; |
704 | 236k | bit = offs; |
705 | 236k | offs &= matchByte; |
706 | 236k | probLit = prob + (offs + bit + symbol); |
707 | 236k | GET_BIT2_CHECK(probLit, symbol, offs ^= bit; , ; ) |
708 | 236k | } |
709 | 236k | while (symbol < 0x100); |
710 | 29.5k | } |
711 | 93.0k | res = DUMMY_LIT; |
712 | 93.0k | } |
713 | 77.3k | else |
714 | 77.3k | { |
715 | 77.3k | unsigned len; |
716 | 77.3k | UPDATE_1_CHECK; |
717 | | |
718 | 77.3k | prob = probs + IsRep + state; |
719 | 77.3k | IF_BIT_0_CHECK(prob) |
720 | 15.6k | { |
721 | 15.6k | UPDATE_0_CHECK; |
722 | 15.6k | state = 0; |
723 | 15.6k | prob = probs + LenCoder; |
724 | 15.6k | res = DUMMY_MATCH; |
725 | 15.6k | } |
726 | 61.7k | else |
727 | 61.7k | { |
728 | 61.7k | UPDATE_1_CHECK; |
729 | 61.7k | res = DUMMY_REP; |
730 | 61.7k | prob = probs + IsRepG0 + state; |
731 | 61.7k | IF_BIT_0_CHECK(prob) |
732 | 39.3k | { |
733 | 39.3k | UPDATE_0_CHECK; |
734 | 39.3k | prob = probs + IsRep0Long + COMBINED_PS_STATE; |
735 | 39.3k | IF_BIT_0_CHECK(prob) |
736 | 6.73k | { |
737 | 6.73k | UPDATE_0_CHECK; |
738 | 6.73k | NORMALIZE_CHECK; |
739 | 6.72k | return DUMMY_REP; |
740 | 6.73k | } |
741 | 32.5k | else |
742 | 32.5k | { |
743 | 32.5k | UPDATE_1_CHECK; |
744 | 32.5k | } |
745 | 39.3k | } |
746 | 22.3k | else |
747 | 22.3k | { |
748 | 22.3k | UPDATE_1_CHECK; |
749 | 22.3k | prob = probs + IsRepG1 + state; |
750 | 22.3k | IF_BIT_0_CHECK(prob) |
751 | 18.9k | { |
752 | 18.9k | UPDATE_0_CHECK; |
753 | 18.9k | } |
754 | 3.45k | else |
755 | 3.45k | { |
756 | 3.45k | UPDATE_1_CHECK; |
757 | 3.45k | prob = probs + IsRepG2 + state; |
758 | 3.45k | IF_BIT_0_CHECK(prob) |
759 | 2.38k | { |
760 | 2.38k | UPDATE_0_CHECK; |
761 | 2.38k | } |
762 | 1.05k | else |
763 | 1.05k | { |
764 | 1.05k | UPDATE_1_CHECK; |
765 | 1.05k | } |
766 | 3.44k | } |
767 | 22.3k | } |
768 | 54.9k | state = kNumStates; |
769 | 54.9k | prob = probs + RepLenCoder; |
770 | 54.9k | } |
771 | 70.5k | { |
772 | 70.5k | unsigned limit, offset; |
773 | 70.5k | const CLzmaProb *probLen = prob + LenChoice; |
774 | 70.5k | IF_BIT_0_CHECK(probLen) |
775 | 42.8k | { |
776 | 42.8k | UPDATE_0_CHECK; |
777 | 42.8k | probLen = prob + LenLow + GET_LEN_STATE; |
778 | 42.8k | offset = 0; |
779 | 42.8k | limit = 1 << kLenNumLowBits; |
780 | 42.8k | } |
781 | 27.6k | else |
782 | 27.6k | { |
783 | 27.6k | UPDATE_1_CHECK; |
784 | 27.6k | probLen = prob + LenChoice2; |
785 | 27.6k | IF_BIT_0_CHECK(probLen) |
786 | 3.63k | { |
787 | 3.63k | UPDATE_0_CHECK; |
788 | 3.63k | probLen = prob + LenLow + GET_LEN_STATE + (1 << kLenNumLowBits); |
789 | 3.63k | offset = kLenNumLowSymbols; |
790 | 3.63k | limit = 1 << kLenNumLowBits; |
791 | 3.63k | } |
792 | 23.9k | else |
793 | 23.9k | { |
794 | 23.9k | UPDATE_1_CHECK; |
795 | 23.9k | probLen = prob + LenHigh; |
796 | 23.9k | offset = kLenNumLowSymbols * 2; |
797 | 23.9k | limit = 1 << kLenNumHighBits; |
798 | 23.9k | } |
799 | 27.6k | } |
800 | 140k | TREE_DECODE_CHECK(probLen, limit, len); |
801 | 140k | len += offset; |
802 | 140k | } |
803 | | |
804 | 70.4k | if (state < 4) |
805 | 15.5k | { |
806 | 15.5k | unsigned posSlot; |
807 | 15.5k | prob = probs + PosSlot + |
808 | 15.5k | ((len < kNumLenToPosStates - 1 ? len : kNumLenToPosStates - 1) << |
809 | 15.5k | kNumPosSlotBits); |
810 | 15.5k | TREE_DECODE_CHECK(prob, 1 << kNumPosSlotBits, posSlot); |
811 | 15.4k | if (posSlot >= kStartPosModelIndex) |
812 | 12.2k | { |
813 | 12.2k | unsigned numDirectBits = ((posSlot >> 1) - 1); |
814 | | |
815 | | /* if (bufLimit - buf >= 8) return DUMMY_MATCH; */ |
816 | | |
817 | 12.2k | if (posSlot < kEndPosModelIndex) |
818 | 6.92k | { |
819 | 6.92k | prob = probs + SpecPos + ((2 | (posSlot & 1)) << numDirectBits); |
820 | 6.92k | } |
821 | 5.35k | else |
822 | 5.35k | { |
823 | 5.35k | numDirectBits -= kNumAlignBits; |
824 | 5.35k | do |
825 | 31.5k | { |
826 | 31.5k | NORMALIZE_CHECK |
827 | 31.4k | range >>= 1; |
828 | 31.4k | code -= range & (((code - range) >> 31) - 1); |
829 | | /* if (code >= range) code -= range; */ |
830 | 31.4k | } |
831 | 31.4k | while (--numDirectBits); |
832 | 5.28k | prob = probs + Align; |
833 | 5.28k | numDirectBits = kNumAlignBits; |
834 | 5.28k | } |
835 | 12.2k | { |
836 | 12.2k | unsigned i = 1; |
837 | 12.2k | unsigned m = 1; |
838 | 12.2k | do |
839 | 40.4k | { |
840 | 40.4k | REV_BIT_CHECK(prob, i, m); |
841 | 40.4k | } |
842 | 40.4k | while (--numDirectBits); |
843 | 12.2k | } |
844 | 12.2k | } |
845 | 15.4k | } |
846 | 140k | } |
847 | 170k | } |
848 | 163k | NORMALIZE_CHECK; |
849 | 163k | return res; |
850 | 163k | } |
851 | | |
852 | | |
853 | | void LzmaDec_InitDicAndState(CLzmaDec *p, BoolInt initDic, BoolInt initState) |
854 | 20.2k | { |
855 | 20.2k | p->remainLen = kMatchSpecLenStart + 1; |
856 | 20.2k | p->tempBufSize = 0; |
857 | | |
858 | 20.2k | if (initDic) |
859 | 15.5k | { |
860 | 15.5k | p->processedPos = 0; |
861 | 15.5k | p->checkDicSize = 0; |
862 | 15.5k | p->remainLen = kMatchSpecLenStart + 2; |
863 | 15.5k | } |
864 | 20.2k | if (initState) |
865 | 14.0k | p->remainLen = kMatchSpecLenStart + 2; |
866 | 20.2k | } |
867 | | |
868 | | void LzmaDec_Init(CLzmaDec *p) |
869 | 7.77k | { |
870 | 7.77k | p->dicPos = 0; |
871 | 7.77k | LzmaDec_InitDicAndState(p, True, True); |
872 | 7.77k | } |
873 | | |
874 | | |
875 | | SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit, const Byte *src, SizeT *srcLen, |
876 | | ELzmaFinishMode finishMode, ELzmaStatus *status) |
877 | 18.3k | { |
878 | 18.3k | SizeT inSize = *srcLen; |
879 | 18.3k | (*srcLen) = 0; |
880 | | |
881 | 18.3k | *status = LZMA_STATUS_NOT_SPECIFIED; |
882 | | |
883 | 18.3k | if (p->remainLen > kMatchSpecLenStart) |
884 | 8.09k | { |
885 | 48.5k | for (; inSize > 0 && p->tempBufSize < RC_INIT_SIZE; (*srcLen)++, inSize--) |
886 | 40.4k | p->tempBuf[p->tempBufSize++] = *src++; |
887 | 8.09k | if (p->tempBufSize != 0 && p->tempBuf[0] != 0) |
888 | 0 | return SZ_ERROR_DATA; |
889 | 8.09k | if (p->tempBufSize < RC_INIT_SIZE) |
890 | 9 | { |
891 | 9 | *status = LZMA_STATUS_NEEDS_MORE_INPUT; |
892 | 9 | return SZ_OK; |
893 | 9 | } |
894 | 8.08k | p->code = |
895 | 8.08k | ((UInt32)p->tempBuf[1] << 24) |
896 | 8.08k | | ((UInt32)p->tempBuf[2] << 16) |
897 | 8.08k | | ((UInt32)p->tempBuf[3] << 8) |
898 | 8.08k | | ((UInt32)p->tempBuf[4]); |
899 | 8.08k | p->range = 0xFFFFFFFF; |
900 | 8.08k | p->tempBufSize = 0; |
901 | | |
902 | 8.08k | if (p->remainLen > kMatchSpecLenStart + 1) |
903 | 6.23k | { |
904 | 6.23k | SizeT numProbs = LzmaProps_GetNumProbs(&p->prop); |
905 | 6.23k | SizeT i; |
906 | 6.23k | CLzmaProb *probs = p->probs; |
907 | 50.6M | for (i = 0; i < numProbs; i++) |
908 | 50.6M | probs[i] = kBitModelTotal >> 1; |
909 | 6.23k | p->reps[0] = p->reps[1] = p->reps[2] = p->reps[3] = 1; |
910 | 6.23k | p->state = 0; |
911 | 6.23k | } |
912 | | |
913 | 8.08k | p->remainLen = 0; |
914 | 8.08k | } |
915 | | |
916 | 18.3k | LzmaDec_WriteRem(p, dicLimit); |
917 | | |
918 | 202k | while (p->remainLen != kMatchSpecLenStart) |
919 | 202k | { |
920 | 202k | int checkEndMarkNow = 0; |
921 | | |
922 | 202k | if (p->dicPos >= dicLimit) |
923 | 17.1k | { |
924 | 17.1k | if (p->remainLen == 0 && p->code == 0) |
925 | 16.1k | { |
926 | 16.1k | *status = LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK; |
927 | 16.1k | return SZ_OK; |
928 | 16.1k | } |
929 | 988 | if (finishMode == LZMA_FINISH_ANY) |
930 | 988 | { |
931 | 988 | *status = LZMA_STATUS_NOT_FINISHED; |
932 | 988 | return SZ_OK; |
933 | 988 | } |
934 | 0 | if (p->remainLen != 0) |
935 | 0 | { |
936 | 0 | *status = LZMA_STATUS_NOT_FINISHED; |
937 | 0 | return SZ_ERROR_DATA; |
938 | 0 | } |
939 | 0 | checkEndMarkNow = 1; |
940 | 0 | } |
941 | | |
942 | 184k | if (p->tempBufSize == 0) |
943 | 184k | { |
944 | 184k | SizeT processed; |
945 | 184k | const Byte *bufLimit; |
946 | 184k | if (inSize < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow) |
947 | 170k | { |
948 | 170k | int dummyRes = LzmaDec_TryDummy(p, src, inSize); |
949 | 170k | if (dummyRes == DUMMY_ERROR) |
950 | 932 | { |
951 | 932 | memcpy(p->tempBuf, src, inSize); |
952 | 932 | p->tempBufSize = (unsigned)inSize; |
953 | 932 | (*srcLen) += inSize; |
954 | 932 | *status = LZMA_STATUS_NEEDS_MORE_INPUT; |
955 | 932 | return SZ_OK; |
956 | 932 | } |
957 | 169k | if (checkEndMarkNow && dummyRes != DUMMY_MATCH) |
958 | 0 | { |
959 | 0 | *status = LZMA_STATUS_NOT_FINISHED; |
960 | 0 | return SZ_ERROR_DATA; |
961 | 0 | } |
962 | 169k | bufLimit = src; |
963 | 169k | } |
964 | 13.8k | else |
965 | 13.8k | bufLimit = src + inSize - LZMA_REQUIRED_INPUT_MAX; |
966 | 183k | p->buf = src; |
967 | 183k | if (LzmaDec_DecodeReal2(p, dicLimit, bufLimit) != 0) |
968 | 0 | return SZ_ERROR_DATA; |
969 | 183k | processed = (SizeT)(p->buf - src); |
970 | 183k | (*srcLen) += processed; |
971 | 183k | src += processed; |
972 | 183k | inSize -= processed; |
973 | 183k | } |
974 | 338 | else |
975 | 338 | { |
976 | 338 | unsigned rem = p->tempBufSize, lookAhead = 0; |
977 | 2.01k | while (rem < LZMA_REQUIRED_INPUT_MAX && lookAhead < inSize) |
978 | 1.67k | p->tempBuf[rem++] = src[lookAhead++]; |
979 | 338 | p->tempBufSize = rem; |
980 | 338 | if (rem < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow) |
981 | 261 | { |
982 | 261 | int dummyRes = LzmaDec_TryDummy(p, p->tempBuf, (SizeT)rem); |
983 | 261 | if (dummyRes == DUMMY_ERROR) |
984 | 221 | { |
985 | 221 | (*srcLen) += (SizeT)lookAhead; |
986 | 221 | *status = LZMA_STATUS_NEEDS_MORE_INPUT; |
987 | 221 | return SZ_OK; |
988 | 221 | } |
989 | 40 | if (checkEndMarkNow && dummyRes != DUMMY_MATCH) |
990 | 0 | { |
991 | 0 | *status = LZMA_STATUS_NOT_FINISHED; |
992 | 0 | return SZ_ERROR_DATA; |
993 | 0 | } |
994 | 40 | } |
995 | 117 | p->buf = p->tempBuf; |
996 | 117 | if (LzmaDec_DecodeReal2(p, dicLimit, p->buf) != 0) |
997 | 0 | return SZ_ERROR_DATA; |
998 | | |
999 | 117 | { |
1000 | 117 | unsigned kkk = (unsigned)(p->buf - p->tempBuf); |
1001 | 117 | if (rem < kkk) |
1002 | 0 | return SZ_ERROR_FAIL; /* some internal error */ |
1003 | 117 | rem -= kkk; |
1004 | 117 | if (lookAhead < rem) |
1005 | 0 | return SZ_ERROR_FAIL; /* some internal error */ |
1006 | 117 | lookAhead -= rem; |
1007 | 117 | } |
1008 | 0 | (*srcLen) += (SizeT)lookAhead; |
1009 | 117 | src += lookAhead; |
1010 | 117 | inSize -= (SizeT)lookAhead; |
1011 | 117 | p->tempBufSize = 0; |
1012 | 117 | } |
1013 | 184k | } |
1014 | | |
1015 | 0 | if (p->code != 0) |
1016 | 0 | return SZ_ERROR_DATA; |
1017 | 0 | *status = LZMA_STATUS_FINISHED_WITH_MARK; |
1018 | 0 | return SZ_OK; |
1019 | 0 | } |
1020 | | |
1021 | | |
1022 | | SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status) |
1023 | 0 | { |
1024 | 0 | SizeT outSize = *destLen; |
1025 | 0 | SizeT inSize = *srcLen; |
1026 | 0 | *srcLen = *destLen = 0; |
1027 | 0 | for (;;) |
1028 | 0 | { |
1029 | 0 | SizeT inSizeCur = inSize, outSizeCur, dicPos; |
1030 | 0 | ELzmaFinishMode curFinishMode; |
1031 | 0 | SRes res; |
1032 | 0 | if (p->dicPos == p->dicBufSize) |
1033 | 0 | p->dicPos = 0; |
1034 | 0 | dicPos = p->dicPos; |
1035 | 0 | if (outSize > p->dicBufSize - dicPos) |
1036 | 0 | { |
1037 | 0 | outSizeCur = p->dicBufSize; |
1038 | 0 | curFinishMode = LZMA_FINISH_ANY; |
1039 | 0 | } |
1040 | 0 | else |
1041 | 0 | { |
1042 | 0 | outSizeCur = dicPos + outSize; |
1043 | 0 | curFinishMode = finishMode; |
1044 | 0 | } |
1045 | |
|
1046 | 0 | res = LzmaDec_DecodeToDic(p, outSizeCur, src, &inSizeCur, curFinishMode, status); |
1047 | 0 | src += inSizeCur; |
1048 | 0 | inSize -= inSizeCur; |
1049 | 0 | *srcLen += inSizeCur; |
1050 | 0 | outSizeCur = p->dicPos - dicPos; |
1051 | 0 | memcpy(dest, p->dic + dicPos, outSizeCur); |
1052 | 0 | dest += outSizeCur; |
1053 | 0 | outSize -= outSizeCur; |
1054 | 0 | *destLen += outSizeCur; |
1055 | 0 | if (res != 0) |
1056 | 0 | return res; |
1057 | 0 | if (outSizeCur == 0 || outSize == 0) |
1058 | 0 | return SZ_OK; |
1059 | 0 | } |
1060 | 0 | } |
1061 | | |
1062 | | void LzmaDec_FreeProbs(CLzmaDec *p, ISzAllocPtr alloc) |
1063 | 15.5k | { |
1064 | 15.5k | ISzAlloc_Free(alloc, p->probs); |
1065 | 15.5k | p->probs = NULL; |
1066 | 15.5k | } |
1067 | | |
1068 | | static void LzmaDec_FreeDict(CLzmaDec *p, ISzAllocPtr alloc) |
1069 | 15.5k | { |
1070 | 15.5k | ISzAlloc_Free(alloc, p->dic); |
1071 | 15.5k | p->dic = NULL; |
1072 | 15.5k | } |
1073 | | |
1074 | | void LzmaDec_Free(CLzmaDec *p, ISzAllocPtr alloc) |
1075 | 7.77k | { |
1076 | 7.77k | LzmaDec_FreeProbs(p, alloc); |
1077 | 7.77k | LzmaDec_FreeDict(p, alloc); |
1078 | 7.77k | } |
1079 | | |
1080 | | SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size) |
1081 | 7.77k | { |
1082 | 7.77k | UInt32 dicSize; |
1083 | 7.77k | Byte d; |
1084 | | |
1085 | 7.77k | if (size < LZMA_PROPS_SIZE) |
1086 | 0 | return SZ_ERROR_UNSUPPORTED; |
1087 | 7.77k | else |
1088 | 7.77k | dicSize = data[1] | ((UInt32)data[2] << 8) | ((UInt32)data[3] << 16) | ((UInt32)data[4] << 24); |
1089 | | |
1090 | 7.77k | if (dicSize < LZMA_DIC_MIN) |
1091 | 0 | dicSize = LZMA_DIC_MIN; |
1092 | 7.77k | p->dicSize = dicSize; |
1093 | | |
1094 | 7.77k | d = data[0]; |
1095 | 7.77k | if (d >= (9 * 5 * 5)) |
1096 | 0 | return SZ_ERROR_UNSUPPORTED; |
1097 | | |
1098 | 7.77k | p->lc = (Byte)(d % 9); |
1099 | 7.77k | d /= 9; |
1100 | 7.77k | p->pb = (Byte)(d / 5); |
1101 | 7.77k | p->lp = (Byte)(d % 5); |
1102 | | |
1103 | 7.77k | return SZ_OK; |
1104 | 7.77k | } |
1105 | | |
1106 | | static SRes LzmaDec_AllocateProbs2(CLzmaDec *p, const CLzmaProps *propNew, ISzAllocPtr alloc) |
1107 | 7.77k | { |
1108 | 7.77k | UInt32 numProbs = LzmaProps_GetNumProbs(propNew); |
1109 | 7.77k | if (!p->probs || numProbs != p->numProbs) |
1110 | 7.77k | { |
1111 | 7.77k | LzmaDec_FreeProbs(p, alloc); |
1112 | 7.77k | p->probs = (CLzmaProb *)ISzAlloc_Alloc(alloc, numProbs * sizeof(CLzmaProb)); |
1113 | 7.77k | if (!p->probs) |
1114 | 0 | return SZ_ERROR_MEM; |
1115 | 7.77k | p->probs_1664 = p->probs + 1664; |
1116 | 7.77k | p->numProbs = numProbs; |
1117 | 7.77k | } |
1118 | 7.77k | return SZ_OK; |
1119 | 7.77k | } |
1120 | | |
1121 | | SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAllocPtr alloc) |
1122 | 0 | { |
1123 | 0 | CLzmaProps propNew; |
1124 | 0 | RINOK(LzmaProps_Decode(&propNew, props, propsSize)); |
1125 | 0 | RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc)); |
1126 | 0 | p->prop = propNew; |
1127 | 0 | return SZ_OK; |
1128 | 0 | } |
1129 | | |
1130 | | SRes LzmaDec_Allocate(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAllocPtr alloc) |
1131 | 7.77k | { |
1132 | 7.77k | CLzmaProps propNew; |
1133 | 7.77k | SizeT dicBufSize; |
1134 | 7.77k | RINOK(LzmaProps_Decode(&propNew, props, propsSize)); |
1135 | 7.77k | RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc)); |
1136 | | |
1137 | 7.77k | { |
1138 | 7.77k | UInt32 dictSize = propNew.dicSize; |
1139 | 7.77k | SizeT mask = ((UInt32)1 << 12) - 1; |
1140 | 7.77k | if (dictSize >= ((UInt32)1 << 30)) mask = ((UInt32)1 << 22) - 1; |
1141 | 7.77k | else if (dictSize >= ((UInt32)1 << 22)) mask = ((UInt32)1 << 20) - 1;; |
1142 | 7.77k | dicBufSize = ((SizeT)dictSize + mask) & ~mask; |
1143 | 7.77k | if (dicBufSize < dictSize) |
1144 | 0 | dicBufSize = dictSize; |
1145 | 7.77k | } |
1146 | | |
1147 | 7.77k | if (!p->dic || dicBufSize != p->dicBufSize) |
1148 | 7.77k | { |
1149 | 7.77k | LzmaDec_FreeDict(p, alloc); |
1150 | 7.77k | p->dic = (Byte *)ISzAlloc_Alloc(alloc, dicBufSize); |
1151 | 7.77k | if (!p->dic) |
1152 | 0 | { |
1153 | 0 | LzmaDec_FreeProbs(p, alloc); |
1154 | 0 | return SZ_ERROR_MEM; |
1155 | 0 | } |
1156 | 7.77k | } |
1157 | 7.77k | p->dicBufSize = dicBufSize; |
1158 | 7.77k | p->prop = propNew; |
1159 | 7.77k | return SZ_OK; |
1160 | 7.77k | } |
1161 | | |
1162 | | SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, |
1163 | | const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode, |
1164 | | ELzmaStatus *status, ISzAllocPtr alloc) |
1165 | 0 | { |
1166 | 0 | CLzmaDec p; |
1167 | 0 | SRes res; |
1168 | 0 | SizeT outSize = *destLen, inSize = *srcLen; |
1169 | 0 | *destLen = *srcLen = 0; |
1170 | 0 | *status = LZMA_STATUS_NOT_SPECIFIED; |
1171 | 0 | if (inSize < RC_INIT_SIZE) |
1172 | 0 | return SZ_ERROR_INPUT_EOF; |
1173 | 0 | LzmaDec_Construct(&p); |
1174 | 0 | RINOK(LzmaDec_AllocateProbs(&p, propData, propSize, alloc)); |
1175 | 0 | p.dic = dest; |
1176 | 0 | p.dicBufSize = outSize; |
1177 | 0 | LzmaDec_Init(&p); |
1178 | 0 | *srcLen = inSize; |
1179 | 0 | res = LzmaDec_DecodeToDic(&p, outSize, src, srcLen, finishMode, status); |
1180 | 0 | *destLen = p.dicPos; |
1181 | 0 | if (res == SZ_OK && *status == LZMA_STATUS_NEEDS_MORE_INPUT) |
1182 | 0 | res = SZ_ERROR_INPUT_EOF; |
1183 | 0 | LzmaDec_FreeProbs(&p, alloc); |
1184 | 0 | return res; |
1185 | 0 | } |