CMS 3D CMS Logo

LzmaDec.cc
Go to the documentation of this file.
1 /* LzmaDec.c -- LZMA Decoder
2 2009-09-20 : Igor Pavlov : Public domain */
3 
4 #include "LzmaDec.h"
5 
6 #include <cstring>
7 
8 #define kNumTopBits 24
9 #define kTopValue ((UInt32)1 << kNumTopBits)
10 
11 #define kNumBitModelTotalBits 11
12 #define kBitModelTotal (1 << kNumBitModelTotalBits)
13 #define kNumMoveBits 5
14 
15 #define RC_INIT_SIZE 5
16 
17 #define NORMALIZE \
18  if (range < kTopValue) { \
19  range <<= 8; \
20  code = (code << 8) | (*buf++); \
21  }
22 
23 #define IF_BIT_0(p) \
24  ttt = *(p); \
25  NORMALIZE; \
26  bound = (range >> kNumBitModelTotalBits) * ttt; \
27  if (code < bound)
28 #define UPDATE_0(p) \
29  range = bound; \
30  *(p) = (CLzmaProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits));
31 #define UPDATE_1(p) \
32  range -= bound; \
33  code -= bound; \
34  *(p) = (CLzmaProb)(ttt - (ttt >> kNumMoveBits));
35 #define GET_BIT2(p, i, A0, A1) \
36  IF_BIT_0(p) { \
37  UPDATE_0(p); \
38  i = (i + i); \
39  A0; \
40  } \
41  else { \
42  UPDATE_1(p); \
43  i = (i + i) + 1; \
44  A1; \
45  }
46 #define GET_BIT(p, i) GET_BIT2(p, i, ;, ;)
47 
48 #define TREE_GET_BIT(probs, i) \
49  { GET_BIT((probs + i), i); }
50 #define TREE_DECODE(probs, limit, i) \
51  { \
52  i = 1; \
53  do { \
54  TREE_GET_BIT(probs, i); \
55  } while (i < limit); \
56  i -= limit; \
57  }
58 
59 /* #define _LZMA_SIZE_OPT */
60 
61 #ifdef _LZMA_SIZE_OPT
62 #define TREE_6_DECODE(probs, i) TREE_DECODE(probs, (1 << 6), i)
63 #else
64 #define TREE_6_DECODE(probs, i) \
65  { \
66  i = 1; \
67  TREE_GET_BIT(probs, i); \
68  TREE_GET_BIT(probs, i); \
69  TREE_GET_BIT(probs, i); \
70  TREE_GET_BIT(probs, i); \
71  TREE_GET_BIT(probs, i); \
72  TREE_GET_BIT(probs, i); \
73  i -= 0x40; \
74  }
75 #endif
76 
77 #define NORMALIZE_CHECK \
78  if (range < kTopValue) { \
79  if (buf >= bufLimit) \
80  return DUMMY_ERROR; \
81  range <<= 8; \
82  code = (code << 8) | (*buf++); \
83  }
84 
85 #define IF_BIT_0_CHECK(p) \
86  ttt = *(p); \
87  NORMALIZE_CHECK; \
88  bound = (range >> kNumBitModelTotalBits) * ttt; \
89  if (code < bound)
90 #define UPDATE_0_CHECK range = bound;
91 #define UPDATE_1_CHECK \
92  range -= bound; \
93  code -= bound;
94 #define GET_BIT2_CHECK(p, i, A0, A1) \
95  IF_BIT_0_CHECK(p) { \
96  UPDATE_0_CHECK; \
97  i = (i + i); \
98  A0; \
99  } \
100  else { \
101  UPDATE_1_CHECK; \
102  i = (i + i) + 1; \
103  A1; \
104  }
105 #define GET_BIT_CHECK(p, i) GET_BIT2_CHECK(p, i, ;, ;)
106 #define TREE_DECODE_CHECK(probs, limit, i) \
107  { \
108  i = 1; \
109  do { \
110  GET_BIT_CHECK(probs + i, i) \
111  } while (i < limit); \
112  i -= limit; \
113  }
114 
115 #define kNumPosBitsMax 4
116 #define kNumPosStatesMax (1 << kNumPosBitsMax)
117 
118 #define kLenNumLowBits 3
119 #define kLenNumLowSymbols (1 << kLenNumLowBits)
120 #define kLenNumMidBits 3
121 #define kLenNumMidSymbols (1 << kLenNumMidBits)
122 #define kLenNumHighBits 8
123 #define kLenNumHighSymbols (1 << kLenNumHighBits)
124 
125 #define LenChoice 0
126 #define LenChoice2 (LenChoice + 1)
127 #define LenLow (LenChoice2 + 1)
128 #define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits))
129 #define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits))
130 #define kNumLenProbs (LenHigh + kLenNumHighSymbols)
131 
132 #define kNumStates 12
133 #define kNumLitStates 7
134 
135 #define kStartPosModelIndex 4
136 #define kEndPosModelIndex 14
137 #define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
138 
139 #define kNumPosSlotBits 6
140 #define kNumLenToPosStates 4
141 
142 #define kNumAlignBits 4
143 #define kAlignTableSize (1 << kNumAlignBits)
144 
145 #define kMatchMinLen 2
146 #define kMatchSpecLenStart (kMatchMinLen + kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols)
147 
148 #define IsMatch 0
149 #define IsRep (IsMatch + (kNumStates << kNumPosBitsMax))
150 #define IsRepG0 (IsRep + kNumStates)
151 #define IsRepG1 (IsRepG0 + kNumStates)
152 #define IsRepG2 (IsRepG1 + kNumStates)
153 #define IsRep0Long (IsRepG2 + kNumStates)
154 #define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax))
155 #define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))
156 #define Align (SpecPos + kNumFullDistances - kEndPosModelIndex)
157 #define LenCoder (Align + kAlignTableSize)
158 #define RepLenCoder (LenCoder + kNumLenProbs)
159 #define Literal (RepLenCoder + kNumLenProbs)
160 
161 #define LZMA_BASE_SIZE 1846
162 #define LZMA_LIT_SIZE 768
163 
164 #define LzmaProps_GetNumProbs(p) ((UInt32)LZMA_BASE_SIZE + (LZMA_LIT_SIZE << ((p)->lc + (p)->lp)))
165 
166 #if Literal != LZMA_BASE_SIZE
167 StopCompilingDueBUG
168 #endif
169 
170 #define LZMA_DIC_MIN (1 << 12)
171 
172  /* First LZMA-symbol is always decoded.
173 And it decodes new LZMA-symbols while (buf < bufLimit), but "buf" is without last normalization
174 Out:
175  Result:
176  SZ_OK - OK
177  SZ_ERROR_DATA - Error
178  p->remainLen:
179  < kMatchSpecLenStart : normal remain
180  = kMatchSpecLenStart : finished
181  = kMatchSpecLenStart + 1 : Flush marker
182  = kMatchSpecLenStart + 2 : State Init Marker
183 */
184 
185  static int MY_FAST_CALL
186  LzmaDec_DecodeReal(CLzmaDec *p, SizeT limit, const Byte *bufLimit) {
187  CLzmaProb *probs = p->probs;
188 
189  unsigned state = p->state;
190  UInt32 rep0 = p->reps[0], rep1 = p->reps[1], rep2 = p->reps[2], rep3 = p->reps[3];
191  unsigned pbMask = ((unsigned)1 << (p->prop.pb)) - 1;
192  unsigned lpMask = ((unsigned)1 << (p->prop.lp)) - 1;
193  unsigned lc = p->prop.lc;
194 
195  Byte *dic = p->dic;
196  SizeT dicBufSize = p->dicBufSize;
197  SizeT dicPos = p->dicPos;
198 
199  UInt32 processedPos = p->processedPos;
200  UInt32 checkDicSize = p->checkDicSize;
201  unsigned len = 0;
202 
203  const Byte *buf = p->buf;
204  UInt32 range = p->range;
205  UInt32 code = p->code;
206 
207  do {
208  CLzmaProb *prob;
209  UInt32 bound;
210  unsigned ttt;
211  unsigned posState = processedPos & pbMask;
212 
213  prob = probs + IsMatch + (state << kNumPosBitsMax) + posState;
214  IF_BIT_0(prob) {
215  unsigned symbol;
216  UPDATE_0(prob);
217  prob = probs + Literal;
218  if (checkDicSize != 0 || processedPos != 0)
219  prob += (LZMA_LIT_SIZE *
220  (((processedPos & lpMask) << lc) + (dic[(dicPos == 0 ? dicBufSize : dicPos) - 1] >> (8 - lc))));
221 
222  if (state < kNumLitStates) {
223  state -= (state < 4) ? state : 3;
224  symbol = 1;
225  do {
226  GET_BIT(prob + symbol, symbol)
227  } while (symbol < 0x100);
228  } else {
229  unsigned matchByte = p->dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
230  unsigned offs = 0x100;
231  state -= (state < 10) ? 3 : 6;
232  symbol = 1;
233  do {
234  unsigned bit;
235  CLzmaProb *probLit;
236  matchByte <<= 1;
237  bit = (matchByte & offs);
238  probLit = prob + offs + bit + symbol;
239  GET_BIT2(probLit, symbol, offs &= ~bit, offs &= bit)
240  } while (symbol < 0x100);
241  }
242  dic[dicPos++] = (Byte)symbol;
243  processedPos++;
244  continue;
245  }
246  else {
247  UPDATE_1(prob);
248  prob = probs + IsRep + state;
249  IF_BIT_0(prob) {
250  UPDATE_0(prob);
251  state += kNumStates;
252  prob = probs + LenCoder;
253  }
254  else {
255  UPDATE_1(prob);
256  if (checkDicSize == 0 && processedPos == 0)
257  return SZ_ERROR_DATA;
258  prob = probs + IsRepG0 + state;
259  IF_BIT_0(prob) {
260  UPDATE_0(prob);
261  prob = probs + IsRep0Long + (state << kNumPosBitsMax) + posState;
262  IF_BIT_0(prob) {
263  UPDATE_0(prob);
264  dic[dicPos] = dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
265  dicPos++;
266  processedPos++;
267  state = state < kNumLitStates ? 9 : 11;
268  continue;
269  }
270  UPDATE_1(prob);
271  }
272  else {
274  UPDATE_1(prob);
275  prob = probs + IsRepG1 + state;
276  IF_BIT_0(prob) {
277  UPDATE_0(prob);
278  distance = rep1;
279  }
280  else {
281  UPDATE_1(prob);
282  prob = probs + IsRepG2 + state;
283  IF_BIT_0(prob) {
284  UPDATE_0(prob);
285  distance = rep2;
286  }
287  else {
288  UPDATE_1(prob);
289  distance = rep3;
290  rep3 = rep2;
291  }
292  rep2 = rep1;
293  }
294  rep1 = rep0;
295  rep0 = distance;
296  }
297  state = state < kNumLitStates ? 8 : 11;
298  prob = probs + RepLenCoder;
299  }
300  {
301  unsigned limit, offset;
302  CLzmaProb *probLen = prob + LenChoice;
303  IF_BIT_0(probLen) {
304  UPDATE_0(probLen);
305  probLen = prob + LenLow + (posState << kLenNumLowBits);
306  offset = 0;
307  limit = (1 << kLenNumLowBits);
308  }
309  else {
310  UPDATE_1(probLen);
311  probLen = prob + LenChoice2;
312  IF_BIT_0(probLen) {
313  UPDATE_0(probLen);
314  probLen = prob + LenMid + (posState << kLenNumMidBits);
316  limit = (1 << kLenNumMidBits);
317  }
318  else {
319  UPDATE_1(probLen);
320  probLen = prob + LenHigh;
322  limit = (1 << kLenNumHighBits);
323  }
324  }
325  TREE_DECODE(probLen, limit, len);
326  len += offset;
327  }
328 
329  if (state >= kNumStates) {
331  prob = probs + PosSlot + ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) << kNumPosSlotBits);
333  if (distance >= kStartPosModelIndex) {
334  unsigned posSlot = (unsigned)distance;
335  int numDirectBits = (int)(((distance >> 1) - 1));
336  distance = (2 | (distance & 1));
337  if (posSlot < kEndPosModelIndex) {
338  distance <<= numDirectBits;
339  prob = probs + SpecPos + distance - posSlot - 1;
340  {
341  UInt32 mask = 1;
342  unsigned i = 1;
343  do {
344  GET_BIT2(prob + i, i, ;, distance |= mask);
345  mask <<= 1;
346  } while (--numDirectBits != 0);
347  }
348  } else {
349  numDirectBits -= kNumAlignBits;
350  do {
351  NORMALIZE
352  range >>= 1;
353 
354  {
355  UInt32 t;
356  code -= range;
357  t = (0 - ((UInt32)code >> 31)); /* (UInt32)((Int32)code >> 31) */
358  distance = (distance << 1) + (t + 1);
359  code += range & t;
360  }
361  /*
362  distance <<= 1;
363  if (code >= range)
364  {
365  code -= range;
366  distance |= 1;
367  }
368  */
369  } while (--numDirectBits != 0);
370  prob = probs + Align;
372  {
373  unsigned i = 1;
374  GET_BIT2(prob + i, i, ;, distance |= 1);
375  GET_BIT2(prob + i, i, ;, distance |= 2);
376  GET_BIT2(prob + i, i, ;, distance |= 4);
377  GET_BIT2(prob + i, i, ;, distance |= 8);
378  }
379  if (distance == (UInt32)0xFFFFFFFF) {
380  len += kMatchSpecLenStart;
381  state -= kNumStates;
382  break;
383  }
384  }
385  }
386  rep3 = rep2;
387  rep2 = rep1;
388  rep1 = rep0;
389  rep0 = distance + 1;
390  if (checkDicSize == 0) {
391  if (distance >= processedPos)
392  return SZ_ERROR_DATA;
393  } else if (distance >= checkDicSize)
394  return SZ_ERROR_DATA;
395  state = (state < kNumStates + kNumLitStates) ? kNumLitStates : kNumLitStates + 3;
396  }
397 
398  len += kMatchMinLen;
399 
400  if (limit == dicPos)
401  return SZ_ERROR_DATA;
402  {
403  SizeT rem = limit - dicPos;
404  unsigned curLen = ((rem < len) ? (unsigned)rem : len);
405  SizeT pos = (dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0);
406 
407  processedPos += curLen;
408 
409  len -= curLen;
410  if (pos + curLen <= dicBufSize) {
411  Byte *dest = dic + dicPos;
412  ptrdiff_t src = (ptrdiff_t)pos - (ptrdiff_t)dicPos;
413  const Byte *lim = dest + curLen;
414  dicPos += curLen;
415  do
416  *(dest) = (Byte) * (dest + src);
417  while (++dest != lim);
418  } else {
419  do {
420  dic[dicPos++] = dic[pos];
421  if (++pos == dicBufSize)
422  pos = 0;
423  } while (--curLen != 0);
424  }
425  }
426  }
427  } while (dicPos < limit && buf < bufLimit);
428  NORMALIZE;
429  p->buf = buf;
430  p->range = range;
431  p->code = code;
432  p->remainLen = len;
433  p->dicPos = dicPos;
434  p->processedPos = processedPos;
435  p->reps[0] = rep0;
436  p->reps[1] = rep1;
437  p->reps[2] = rep2;
438  p->reps[3] = rep3;
439  p->state = state;
440 
441  return SZ_OK;
442 }
443 
445  if (p->remainLen != 0 && p->remainLen < kMatchSpecLenStart) {
446  Byte *dic = p->dic;
447  SizeT dicPos = p->dicPos;
448  SizeT dicBufSize = p->dicBufSize;
449  unsigned len = p->remainLen;
450  UInt32 rep0 = p->reps[0];
451  if (limit - dicPos < len)
452  len = (unsigned)(limit - dicPos);
453 
454  if (p->checkDicSize == 0 && p->prop.dicSize - p->processedPos <= len)
455  p->checkDicSize = p->prop.dicSize;
456 
457  p->processedPos += len;
458  p->remainLen -= len;
459  while (len-- != 0) {
460  dic[dicPos] = dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
461  dicPos++;
462  }
463  p->dicPos = dicPos;
464  }
465 }
466 
467 static int MY_FAST_CALL LzmaDec_DecodeReal2(CLzmaDec *p, SizeT limit, const Byte *bufLimit) {
468  do {
469  SizeT limit2 = limit;
470  if (p->checkDicSize == 0) {
471  UInt32 rem = p->prop.dicSize - p->processedPos;
472  if (limit - p->dicPos > rem)
473  limit2 = p->dicPos + rem;
474  }
475  RINOK(LzmaDec_DecodeReal(p, limit2, bufLimit));
476  if (p->processedPos >= p->prop.dicSize)
477  p->checkDicSize = p->prop.dicSize;
479  } while (p->dicPos < limit && p->buf < bufLimit && p->remainLen < kMatchSpecLenStart);
480 
481  if (p->remainLen > kMatchSpecLenStart) {
482  p->remainLen = kMatchSpecLenStart;
483  }
484  return 0;
485 }
486 
487 typedef enum {
488  DUMMY_ERROR, /* unexpected end of input stream */
492 } ELzmaDummy;
493 
494 static ELzmaDummy LzmaDec_TryDummy(const CLzmaDec *p, const Byte *buf, SizeT inSize) {
495  UInt32 range = p->range;
496  UInt32 code = p->code;
497  const Byte *bufLimit = buf + inSize;
498  CLzmaProb *probs = p->probs;
499  unsigned state = p->state;
500  ELzmaDummy res;
501 
502  {
503  CLzmaProb *prob;
504  UInt32 bound;
505  unsigned ttt;
506  unsigned posState = (p->processedPos) & ((1 << p->prop.pb) - 1);
507 
508  prob = probs + IsMatch + (state << kNumPosBitsMax) + posState;
511 
512  /* if (bufLimit - buf >= 7) return DUMMY_LIT; */
513 
514  prob = probs + Literal;
515  if (p->checkDicSize != 0 || p->processedPos != 0)
516  prob += (LZMA_LIT_SIZE * ((((p->processedPos) & ((1 << (p->prop.lp)) - 1)) << p->prop.lc) +
517  (p->dic[(p->dicPos == 0 ? p->dicBufSize : p->dicPos) - 1] >> (8 - p->prop.lc))));
518 
519  if (state < kNumLitStates) {
520  unsigned symbol = 1;
521  do {
522  GET_BIT_CHECK(prob + symbol, symbol)
523  } while (symbol < 0x100);
524  } else {
525  unsigned matchByte = p->dic[p->dicPos - p->reps[0] + ((p->dicPos < p->reps[0]) ? p->dicBufSize : 0)];
526  unsigned offs = 0x100;
527  unsigned symbol = 1;
528  do {
529  unsigned bit;
530  CLzmaProb *probLit;
531  matchByte <<= 1;
532  bit = (matchByte & offs);
533  probLit = prob + offs + bit + symbol;
534  GET_BIT2_CHECK(probLit, symbol, offs &= ~bit, offs &= bit)
535  } while (symbol < 0x100);
536  }
537  res = DUMMY_LIT;
538  }
539  else {
540  unsigned len;
542 
543  prob = probs + IsRep + state;
546  state = 0;
547  prob = probs + LenCoder;
548  res = DUMMY_MATCH;
549  }
550  else {
552  res = DUMMY_REP;
553  prob = probs + IsRepG0 + state;
556  prob = probs + IsRep0Long + (state << kNumPosBitsMax) + posState;
560  return DUMMY_REP;
561  }
562  else {
564  }
565  }
566  else {
568  prob = probs + IsRepG1 + state;
570  else {
572  prob = probs + IsRepG2 + state;
574  else {
576  }
577  }
578  }
579  state = kNumStates;
580  prob = probs + RepLenCoder;
581  }
582  {
583  unsigned limit, offset;
584  CLzmaProb *probLen = prob + LenChoice;
585  IF_BIT_0_CHECK(probLen) {
587  probLen = prob + LenLow + (posState << kLenNumLowBits);
588  offset = 0;
589  limit = 1 << kLenNumLowBits;
590  }
591  else {
593  probLen = prob + LenChoice2;
594  IF_BIT_0_CHECK(probLen) {
596  probLen = prob + LenMid + (posState << kLenNumMidBits);
598  limit = 1 << kLenNumMidBits;
599  }
600  else {
602  probLen = prob + LenHigh;
604  limit = 1 << kLenNumHighBits;
605  }
606  }
607  TREE_DECODE_CHECK(probLen, limit, len);
608  len += offset;
609  }
610 
611  if (state < 4) {
612  unsigned posSlot;
613  prob = probs + PosSlot + ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) << kNumPosSlotBits);
614  TREE_DECODE_CHECK(prob, 1 << kNumPosSlotBits, posSlot);
615  if (posSlot >= kStartPosModelIndex) {
616  int numDirectBits = ((posSlot >> 1) - 1);
617 
618  /* if (bufLimit - buf >= 8) return DUMMY_MATCH; */
619 
620  if (posSlot < kEndPosModelIndex) {
621  prob = probs + SpecPos + ((2 | (posSlot & 1)) << numDirectBits) - posSlot - 1;
622  } else {
623  numDirectBits -= kNumAlignBits;
624  do {
626  range >>= 1;
627  code -= range & (((code - range) >> 31) - 1);
628  /* if (code >= range) code -= range; */
629  } while (--numDirectBits != 0);
630  prob = probs + Align;
631  numDirectBits = kNumAlignBits;
632  }
633  {
634  unsigned i = 1;
635  do {
636  GET_BIT_CHECK(prob + i, i);
637  } while (--numDirectBits != 0);
638  }
639  }
640  }
641  }
642  }
644  return res;
645 }
646 
647 static void LzmaDec_InitRc(CLzmaDec *p, const Byte *data) {
648  p->code = ((UInt32)data[1] << 24) | ((UInt32)data[2] << 16) | ((UInt32)data[3] << 8) | ((UInt32)data[4]);
649  p->range = 0xFFFFFFFF;
650  p->needFlush = 0;
651 }
652 
653 void LzmaDec_InitDicAndState(CLzmaDec *p, Bool initDic, Bool initState) {
654  p->needFlush = 1;
655  p->remainLen = 0;
656  p->tempBufSize = 0;
657 
658  if (initDic) {
659  p->processedPos = 0;
660  p->checkDicSize = 0;
661  p->needInitState = 1;
662  }
663  if (initState)
664  p->needInitState = 1;
665 }
666 
668  p->dicPos = 0;
670 }
671 
673  UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (p->prop.lc + p->prop.lp));
674  UInt32 i;
675  CLzmaProb *probs = p->probs;
676  for (i = 0; i < numProbs; i++)
677  probs[i] = kBitModelTotal >> 1;
678  p->reps[0] = p->reps[1] = p->reps[2] = p->reps[3] = 1;
679  p->state = 0;
680  p->needInitState = 0;
681 }
682 
684  CLzmaDec *p, SizeT dicLimit, const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status) {
685  SizeT inSize = *srcLen;
686  (*srcLen) = 0;
687  LzmaDec_WriteRem(p, dicLimit);
688 
690 
691  while (p->remainLen != kMatchSpecLenStart) {
692  int checkEndMarkNow;
693 
694  if (p->needFlush != 0) {
695  for (; inSize > 0 && p->tempBufSize < RC_INIT_SIZE; (*srcLen)++, inSize--)
696  p->tempBuf[p->tempBufSize++] = *src++;
697  if (p->tempBufSize < RC_INIT_SIZE) {
699  return SZ_OK;
700  }
701  if (p->tempBuf[0] != 0)
702  return SZ_ERROR_DATA;
703 
704  LzmaDec_InitRc(p, p->tempBuf);
705  p->tempBufSize = 0;
706  }
707 
708  checkEndMarkNow = 0;
709  if (p->dicPos >= dicLimit) {
710  if (p->remainLen == 0 && p->code == 0) {
712  return SZ_OK;
713  }
714  if (finishMode == LZMA_FINISH_ANY) {
716  return SZ_OK;
717  }
718  if (p->remainLen != 0) {
720  return SZ_ERROR_DATA;
721  }
722  checkEndMarkNow = 1;
723  }
724 
725  if (p->needInitState)
727 
728  if (p->tempBufSize == 0) {
729  SizeT processed;
730  const Byte *bufLimit;
731  if (inSize < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow) {
732  int dummyRes = LzmaDec_TryDummy(p, src, inSize);
733  if (dummyRes == DUMMY_ERROR) {
734  memcpy(p->tempBuf, src, inSize);
735  p->tempBufSize = (unsigned)inSize;
736  (*srcLen) += inSize;
738  return SZ_OK;
739  }
740  if (checkEndMarkNow && dummyRes != DUMMY_MATCH) {
742  return SZ_ERROR_DATA;
743  }
744  bufLimit = src;
745  } else
746  bufLimit = src + inSize - LZMA_REQUIRED_INPUT_MAX;
747  p->buf = src;
748  if (LzmaDec_DecodeReal2(p, dicLimit, bufLimit) != 0)
749  return SZ_ERROR_DATA;
750  processed = (SizeT)(p->buf - src);
751  (*srcLen) += processed;
752  src += processed;
753  inSize -= processed;
754  } else {
755  unsigned rem = p->tempBufSize, lookAhead = 0;
756  while (rem < LZMA_REQUIRED_INPUT_MAX && lookAhead < inSize)
757  p->tempBuf[rem++] = src[lookAhead++];
758  p->tempBufSize = rem;
759  if (rem < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow) {
760  int dummyRes = LzmaDec_TryDummy(p, p->tempBuf, rem);
761  if (dummyRes == DUMMY_ERROR) {
762  (*srcLen) += lookAhead;
764  return SZ_OK;
765  }
766  if (checkEndMarkNow && dummyRes != DUMMY_MATCH) {
768  return SZ_ERROR_DATA;
769  }
770  }
771  p->buf = p->tempBuf;
772  if (LzmaDec_DecodeReal2(p, dicLimit, p->buf) != 0)
773  return SZ_ERROR_DATA;
774  lookAhead -= (rem - (unsigned)(p->buf - p->tempBuf));
775  (*srcLen) += lookAhead;
776  src += lookAhead;
777  inSize -= lookAhead;
778  p->tempBufSize = 0;
779  }
780  }
781  if (p->code == 0)
783  return (p->code == 0) ? SZ_OK : SZ_ERROR_DATA;
784 }
785 
787  Byte *dest,
788  SizeT *destLen,
789  const Byte *src,
790  SizeT *srcLen,
791  ELzmaFinishMode finishMode,
792  ELzmaStatus *status) {
793  SizeT outSize = *destLen;
794  SizeT inSize = *srcLen;
795  *srcLen = *destLen = 0;
796  for (;;) {
797  SizeT inSizeCur = inSize, outSizeCur, dicPos;
798  ELzmaFinishMode curFinishMode;
799  SRes res;
800  if (p->dicPos == p->dicBufSize)
801  p->dicPos = 0;
802  dicPos = p->dicPos;
803  if (outSize > p->dicBufSize - dicPos) {
804  outSizeCur = p->dicBufSize;
805  curFinishMode = LZMA_FINISH_ANY;
806  } else {
807  outSizeCur = dicPos + outSize;
808  curFinishMode = finishMode;
809  }
810 
811  res = LzmaDec_DecodeToDic(p, outSizeCur, src, &inSizeCur, curFinishMode, status);
812  src += inSizeCur;
813  inSize -= inSizeCur;
814  *srcLen += inSizeCur;
815  outSizeCur = p->dicPos - dicPos;
816  memcpy(dest, p->dic + dicPos, outSizeCur);
817  dest += outSizeCur;
818  outSize -= outSizeCur;
819  *destLen += outSizeCur;
820  if (res != 0)
821  return res;
822  if (outSizeCur == 0 || outSize == 0)
823  return SZ_OK;
824  }
825 }
826 
828  alloc->Free(alloc, p->probs);
829  p->probs = nullptr;
830 }
831 
832 static void LzmaDec_FreeDict(CLzmaDec *p, ISzAlloc *alloc) {
833  alloc->Free(alloc, p->dic);
834  p->dic = nullptr;
835 }
836 
837 void LzmaDec_Free(CLzmaDec *p, ISzAlloc *alloc) {
838  LzmaDec_FreeProbs(p, alloc);
839  LzmaDec_FreeDict(p, alloc);
840 }
841 
843  UInt32 dicSize;
844  Byte d;
845 
846  if (size < LZMA_PROPS_SIZE)
847  return SZ_ERROR_UNSUPPORTED;
848  else
849  dicSize = data[1] | ((UInt32)data[2] << 8) | ((UInt32)data[3] << 16) | ((UInt32)data[4] << 24);
850 
851  if (dicSize < LZMA_DIC_MIN)
852  dicSize = LZMA_DIC_MIN;
853  p->dicSize = dicSize;
854 
855  d = data[0];
856  if (d >= (9 * 5 * 5))
857  return SZ_ERROR_UNSUPPORTED;
858 
859  p->lc = d % 9;
860  d /= 9;
861  p->pb = d / 5;
862  p->lp = d % 5;
863 
864  return SZ_OK;
865 }
866 
867 static SRes LzmaDec_AllocateProbs2(CLzmaDec *p, const CLzmaProps *propNew, ISzAlloc *alloc) {
868  UInt32 numProbs = LzmaProps_GetNumProbs(propNew);
869  if (p->probs == nullptr || numProbs != p->numProbs) {
870  LzmaDec_FreeProbs(p, alloc);
871  p->probs = (CLzmaProb *)alloc->Alloc(alloc, numProbs * sizeof(CLzmaProb));
872  p->numProbs = numProbs;
873  if (p->probs == nullptr)
874  return SZ_ERROR_MEM;
875  }
876  return SZ_OK;
877 }
878 
879 SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc) {
880  CLzmaProps propNew;
881  RINOK(LzmaProps_Decode(&propNew, props, propsSize));
882  RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc));
883  p->prop = propNew;
884  return SZ_OK;
885 }
886 
887 SRes LzmaDec_Allocate(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc) {
888  CLzmaProps propNew;
889  SizeT dicBufSize;
890  RINOK(LzmaProps_Decode(&propNew, props, propsSize));
891  RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc));
892  dicBufSize = propNew.dicSize;
893  if (p->dic == nullptr || dicBufSize != p->dicBufSize) {
894  LzmaDec_FreeDict(p, alloc);
895  p->dic = (Byte *)alloc->Alloc(alloc, dicBufSize);
896  if (p->dic == nullptr) {
897  LzmaDec_FreeProbs(p, alloc);
898  return SZ_ERROR_MEM;
899  }
900  }
901  p->dicBufSize = dicBufSize;
902  p->prop = propNew;
903  return SZ_OK;
904 }
905 
907  SizeT *destLen,
908  const Byte *src,
909  SizeT *srcLen,
910  const Byte *propData,
911  unsigned propSize,
912  ELzmaFinishMode finishMode,
914  ISzAlloc *alloc) {
915  CLzmaDec p;
916  SRes res;
917  SizeT inSize = *srcLen;
918  SizeT outSize = *destLen;
919  *srcLen = *destLen = 0;
920  if (inSize < RC_INIT_SIZE)
921  return SZ_ERROR_INPUT_EOF;
922 
924  res = LzmaDec_AllocateProbs(&p, propData, propSize, alloc);
925  if (res != 0)
926  return res;
927  p.dic = dest;
928  p.dicBufSize = outSize;
929 
930  LzmaDec_Init(&p);
931 
932  *srcLen = inSize;
933  res = LzmaDec_DecodeToDic(&p, outSize, src, srcLen, finishMode, status);
934 
937 
938  (*destLen) = p.dicPos;
939  LzmaDec_FreeProbs(&p, alloc);
940  return res;
941 }
_CLzmaProps
Definition: LzmaDec.h:27
UPDATE_0_CHECK
#define UPDATE_0_CHECK
Definition: LzmaDec.cc:90
FastTimerService_cff.range
range
Definition: FastTimerService_cff.py:34
Byte
unsigned char Byte
Definition: Types.h:60
UPDATE_1_CHECK
#define UPDATE_1_CHECK
Definition: LzmaDec.cc:91
LzmaDec_DecodeToDic
SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit, const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status)
Definition: LzmaDec.cc:683
kNumLenToPosStates
#define kNumLenToPosStates
Definition: LzmaDec.cc:140
mps_fire.i
i
Definition: mps_fire.py:355
LZMA_STATUS_NOT_FINISHED
Definition: LzmaDec.h:102
LzmaDec_Allocate
SRes LzmaDec_Allocate(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc)
Definition: LzmaDec.cc:887
LZMA_STATUS_NEEDS_MORE_INPUT
Definition: LzmaDec.h:103
LZMA_FINISH_ANY
Definition: LzmaDec.h:80
IF_BIT_0_CHECK
#define IF_BIT_0_CHECK(p)
Definition: LzmaDec.cc:85
_CLzmaProps::dicSize
UInt32 dicSize
Definition: LzmaDec.h:29
mps_update.status
status
Definition: mps_update.py:69
IF_BIT_0
#define IF_BIT_0(p)
Definition: LzmaDec.cc:23
kNumLitStates
#define kNumLitStates
Definition: LzmaDec.cc:133
LzmaDec.h
AlCaHLTBitMon_ParallelJobs.p
p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
IsRep
#define IsRep
Definition: LzmaDec.cc:149
Bool
int Bool
Definition: Types.h:100
pos
Definition: PixelAliasList.h:18
kBitModelTotal
#define kBitModelTotal
Definition: LzmaDec.cc:12
HLT_2018_cff.distance
distance
Definition: HLT_2018_cff.py:6417
kNumStates
#define kNumStates
Definition: LzmaDec.cc:132
LzmaDec_AllocateProbs
SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc)
Definition: LzmaDec.cc:879
kLenNumMidBits
#define kLenNumMidBits
Definition: LzmaDec.cc:120
DUMMY_REP
Definition: LzmaDec.cc:491
ELzmaStatus
ELzmaStatus
Definition: LzmaDec.h:99
LzmaDec_AllocateProbs2
static SRes LzmaDec_AllocateProbs2(CLzmaDec *p, const CLzmaProps *propNew, ISzAlloc *alloc)
Definition: LzmaDec.cc:867
PosSlot
#define PosSlot
Definition: LzmaDec.cc:154
UInt32
unsigned int UInt32
Definition: Types.h:69
SZ_OK
#define SZ_OK
Definition: Types.h:25
LzmaDecode
SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode, ELzmaStatus *status, ISzAlloc *alloc)
Definition: LzmaDec.cc:906
LzmaDec_DecodeReal2
static int LzmaDec_DecodeReal2(CLzmaDec *p, SizeT limit, const Byte *bufLimit)
Definition: LzmaDec.cc:467
LzmaDec_DecodeToBuf
SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status)
Definition: LzmaDec.cc:786
TREE_DECODE_CHECK
#define TREE_DECODE_CHECK(probs, limit, i)
Definition: LzmaDec.cc:106
LzmaDec_Init
void LzmaDec_Init(CLzmaDec *p)
Definition: LzmaDec.cc:667
kLenNumLowSymbols
#define kLenNumLowSymbols
Definition: LzmaDec.cc:119
SpecPos
#define SpecPos
Definition: LzmaDec.cc:155
LenLow
#define LenLow
Definition: LzmaDec.cc:127
NORMALIZE_CHECK
#define NORMALIZE_CHECK
Definition: LzmaDec.cc:77
LZMA_DIC_MIN
#define LZMA_DIC_MIN
Definition: LzmaDec.cc:170
kLenNumHighBits
#define kLenNumHighBits
Definition: LzmaDec.cc:122
LZMA_REQUIRED_INPUT_MAX
#define LZMA_REQUIRED_INPUT_MAX
Definition: LzmaDec.h:45
ISzAlloc
Definition: Types.h:215
OrderedSet.t
t
Definition: OrderedSet.py:90
GET_BIT2
#define GET_BIT2(p, i, A0, A1)
Definition: LzmaDec.cc:35
SizeT
size_t SizeT
Definition: Types.h:97
kNumPosSlotBits
#define kNumPosSlotBits
Definition: LzmaDec.cc:139
DUMMY_LIT
Definition: LzmaDec.cc:489
LzmaDec_TryDummy
static ELzmaDummy LzmaDec_TryDummy(const CLzmaDec *p, const Byte *buf, SizeT inSize)
Definition: LzmaDec.cc:494
kMatchMinLen
#define kMatchMinLen
Definition: LzmaDec.cc:145
LzmaProps_GetNumProbs
#define LzmaProps_GetNumProbs(p)
Definition: LzmaDec.cc:164
IsMatch
#define IsMatch
Definition: LzmaDec.cc:148
LZMA_STATUS_NOT_SPECIFIED
Definition: LzmaDec.h:100
CLzmaDec
Definition: LzmaDec.h:47
Literal
#define Literal
Definition: LzmaDec.cc:159
LZMA_LIT_SIZE
#define LZMA_LIT_SIZE
Definition: LzmaDec.cc:162
LzmaDec_DecodeReal
static int LzmaDec_DecodeReal(CLzmaDec *p, SizeT limit, const Byte *bufLimit)
Definition: LzmaDec.cc:186
TrackRefitter_38T_cff.src
src
Definition: TrackRefitter_38T_cff.py:24
ISzAlloc::Free
void(* Free)(void *p, void *address)
Definition: Types.h:217
LenChoice2
#define LenChoice2
Definition: LzmaDec.cc:126
LzmaDec_Free
void LzmaDec_Free(CLzmaDec *p, ISzAlloc *alloc)
Definition: LzmaDec.cc:837
LzmaProps_Decode
SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size)
Definition: LzmaDec.cc:842
RepLenCoder
#define RepLenCoder
Definition: LzmaDec.cc:158
kNumAlignBits
#define kNumAlignBits
Definition: LzmaDec.cc:142
createfilelist.int
int
Definition: createfilelist.py:10
NORMALIZE
#define NORMALIZE
Definition: LzmaDec.cc:17
ELzmaDummy
ELzmaDummy
Definition: LzmaDec.cc:487
IsRepG2
#define IsRepG2
Definition: LzmaDec.cc:152
LzmaDec_InitDicAndState
void LzmaDec_InitDicAndState(CLzmaDec *p, Bool initDic, Bool initState)
Definition: LzmaDec.cc:653
TREE_6_DECODE
#define TREE_6_DECODE(probs, i)
Definition: LzmaDec.cc:64
SRes
int SRes
Definition: Types.h:43
kLenNumLowBits
#define kLenNumLowBits
Definition: LzmaDec.cc:118
res
Definition: Electron.h:6
visDQMUpload.buf
buf
Definition: visDQMUpload.py:154
GET_BIT
#define GET_BIT(p, i)
Definition: LzmaDec.cc:46
LzmaDec_FreeProbs
void LzmaDec_FreeProbs(CLzmaDec *p, ISzAlloc *alloc)
Definition: LzmaDec.cc:827
DUMMY_ERROR
Definition: LzmaDec.cc:488
LenCoder
#define LenCoder
Definition: LzmaDec.cc:157
kStartPosModelIndex
#define kStartPosModelIndex
Definition: LzmaDec.cc:135
LZMA_STATUS_FINISHED_WITH_MARK
Definition: LzmaDec.h:101
LzmaDec_InitStateReal
static void LzmaDec_InitStateReal(CLzmaDec *p)
Definition: LzmaDec.cc:672
ISzAlloc::Alloc
void *(* Alloc)(void *p, size_t size)
Definition: Types.h:216
LzmaDec_WriteRem
static void LzmaDec_WriteRem(CLzmaDec *p, SizeT limit)
Definition: LzmaDec.cc:444
kEndPosModelIndex
#define kEndPosModelIndex
Definition: LzmaDec.cc:136
LzmaDec_Construct
#define LzmaDec_Construct(p)
Definition: LzmaDec.h:67
remoteMonitoring_LED_IterMethod_cfg.limit
limit
Definition: remoteMonitoring_LED_IterMethod_cfg.py:427
CLzmaProb
#define CLzmaProb
Definition: LzmaDec.h:20
UPDATE_1
#define UPDATE_1(p)
Definition: LzmaDec.cc:31
Align
#define Align
Definition: LzmaDec.cc:156
LenHigh
#define LenHigh
Definition: LzmaDec.cc:129
True
#define True
Definition: Types.h:101
kNumPosBitsMax
#define kNumPosBitsMax
Definition: LzmaDec.cc:115
DUMMY_MATCH
Definition: LzmaDec.cc:490
SZ_ERROR_MEM
#define SZ_ERROR_MEM
Definition: Types.h:28
IsRepG1
#define IsRepG1
Definition: LzmaDec.cc:151
SZ_ERROR_UNSUPPORTED
#define SZ_ERROR_UNSUPPORTED
Definition: Types.h:30
data
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
GET_BIT2_CHECK
#define GET_BIT2_CHECK(p, i, A0, A1)
Definition: LzmaDec.cc:94
ztail.d
d
Definition: ztail.py:151
UPDATE_0
#define UPDATE_0(p)
Definition: LzmaDec.cc:28
GET_BIT_CHECK
#define GET_BIT_CHECK(p, i)
Definition: LzmaDec.cc:105
TREE_DECODE
#define TREE_DECODE(probs, limit, i)
Definition: LzmaDec.cc:50
RC_INIT_SIZE
#define RC_INIT_SIZE
Definition: LzmaDec.cc:15
LZMA_PROPS_SIZE
#define LZMA_PROPS_SIZE
Definition: LzmaDec.h:25
LenMid
#define LenMid
Definition: LzmaDec.cc:128
MY_FAST_CALL
#define MY_FAST_CALL
Definition: Types.h:124
hltrates_dqm_sourceclient-live_cfg.offset
offset
Definition: hltrates_dqm_sourceclient-live_cfg.py:82
IsRepG0
#define IsRepG0
Definition: LzmaDec.cc:150
IsRep0Long
#define IsRep0Long
Definition: LzmaDec.cc:153
SZ_ERROR_INPUT_EOF
#define SZ_ERROR_INPUT_EOF
Definition: Types.h:32
kLenNumMidSymbols
#define kLenNumMidSymbols
Definition: LzmaDec.cc:121
LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
Definition: LzmaDec.h:104
TtFullHadEvtBuilder_cfi.prob
prob
Definition: TtFullHadEvtBuilder_cfi.py:33
SZ_ERROR_DATA
#define SZ_ERROR_DATA
Definition: Types.h:27
LzmaDec_FreeDict
static void LzmaDec_FreeDict(CLzmaDec *p, ISzAlloc *alloc)
Definition: LzmaDec.cc:832
mps_fire.dest
dest
Definition: mps_fire.py:179
findQualityFiles.size
size
Write out results.
Definition: findQualityFiles.py:443
RINOK
#define RINOK(x)
Definition: Types.h:52
kMatchSpecLenStart
#define kMatchSpecLenStart
Definition: LzmaDec.cc:146
LzmaDec_InitRc
static void LzmaDec_InitRc(CLzmaDec *p, const Byte *data)
Definition: LzmaDec.cc:647
LenChoice
#define LenChoice
Definition: LzmaDec.cc:125
ELzmaFinishMode
ELzmaFinishMode
Definition: LzmaDec.h:79