Evince
Evince is a document viewer capable of displaying multiple and single page document formats like PDF and Postscript.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
LzmaDec.c File Reference
#include "Precomp.h"
#include "LzmaDec.h"
#include <string.h>
+ Include dependency graph for LzmaDec.c:

Go to the source code of this file.

Macros

#define kNumTopBits   24
 
#define kTopValue   ((UInt32)1 << kNumTopBits)
 
#define kNumBitModelTotalBits   11
 
#define kBitModelTotal   (1 << kNumBitModelTotalBits)
 
#define kNumMoveBits   5
 
#define RC_INIT_SIZE   5
 
#define NORMALIZE   if (range < kTopValue) { range <<= 8; code = (code << 8) | (*buf++); }
 
#define IF_BIT_0(p)   ttt = *(p); NORMALIZE; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)
 
#define UPDATE_0(p)   range = bound; *(p) = (CLzmaProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits));
 
#define UPDATE_1(p)   range -= bound; code -= bound; *(p) = (CLzmaProb)(ttt - (ttt >> kNumMoveBits));
 
#define GET_BIT2(p, i, A0, A1)
 
#define GET_BIT(p, i)   GET_BIT2(p, i, ; , ;)
 
#define TREE_GET_BIT(probs, i)   { GET_BIT((probs + i), i); }
 
#define TREE_DECODE(probs, limit, i)   { i = 1; do { TREE_GET_BIT(probs, i); } while (i < limit); i -= limit; }
 
#define TREE_6_DECODE(probs, i)
 
#define NORMAL_LITER_DEC   GET_BIT(prob + symbol, symbol)
 
#define MATCHED_LITER_DEC
 
#define NORMALIZE_CHECK   if (range < kTopValue) { if (buf >= bufLimit) return DUMMY_ERROR; range <<= 8; code = (code << 8) | (*buf++); }
 
#define IF_BIT_0_CHECK(p)   ttt = *(p); NORMALIZE_CHECK; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)
 
#define UPDATE_0_CHECK   range = bound;
 
#define UPDATE_1_CHECK   range -= bound; code -= bound;
 
#define GET_BIT2_CHECK(p, i, A0, A1)
 
#define GET_BIT_CHECK(p, i)   GET_BIT2_CHECK(p, i, ; , ;)
 
#define TREE_DECODE_CHECK(probs, limit, i)   { i = 1; do { GET_BIT_CHECK(probs + i, i) } while (i < limit); i -= limit; }
 
#define kNumPosBitsMax   4
 
#define kNumPosStatesMax   (1 << kNumPosBitsMax)
 
#define kLenNumLowBits   3
 
#define kLenNumLowSymbols   (1 << kLenNumLowBits)
 
#define kLenNumMidBits   3
 
#define kLenNumMidSymbols   (1 << kLenNumMidBits)
 
#define kLenNumHighBits   8
 
#define kLenNumHighSymbols   (1 << kLenNumHighBits)
 
#define LenChoice   0
 
#define LenChoice2   (LenChoice + 1)
 
#define LenLow   (LenChoice2 + 1)
 
#define LenMid   (LenLow + (kNumPosStatesMax << kLenNumLowBits))
 
#define LenHigh   (LenMid + (kNumPosStatesMax << kLenNumMidBits))
 
#define kNumLenProbs   (LenHigh + kLenNumHighSymbols)
 
#define kNumStates   12
 
#define kNumLitStates   7
 
#define kStartPosModelIndex   4
 
#define kEndPosModelIndex   14
 
#define kNumFullDistances   (1 << (kEndPosModelIndex >> 1))
 
#define kNumPosSlotBits   6
 
#define kNumLenToPosStates   4
 
#define kNumAlignBits   4
 
#define kAlignTableSize   (1 << kNumAlignBits)
 
#define kMatchMinLen   2
 
#define kMatchSpecLenStart   (kMatchMinLen + kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols)
 
#define IsMatch   0
 
#define IsRep   (IsMatch + (kNumStates << kNumPosBitsMax))
 
#define IsRepG0   (IsRep + kNumStates)
 
#define IsRepG1   (IsRepG0 + kNumStates)
 
#define IsRepG2   (IsRepG1 + kNumStates)
 
#define IsRep0Long   (IsRepG2 + kNumStates)
 
#define PosSlot   (IsRep0Long + (kNumStates << kNumPosBitsMax))
 
#define SpecPos   (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))
 
#define Align   (SpecPos + kNumFullDistances - kEndPosModelIndex)
 
#define LenCoder   (Align + kAlignTableSize)
 
#define RepLenCoder   (LenCoder + kNumLenProbs)
 
#define Literal   (RepLenCoder + kNumLenProbs)
 
#define LZMA_BASE_SIZE   1846
 
#define LZMA_LIT_SIZE   0x300
 
#define LzmaProps_GetNumProbs(p)   (Literal + ((UInt32)LZMA_LIT_SIZE << ((p)->lc + (p)->lp)))
 
#define LZMA_DIC_MIN   (1 << 12)
 

Enumerations

enum  ELzmaDummy { DUMMY_ERROR, DUMMY_LIT, DUMMY_MATCH, DUMMY_REP }
 

Functions

static int MY_FAST_CALL LzmaDec_DecodeReal (CLzmaDec *p, SizeT limit, const Byte *bufLimit)
 
static void MY_FAST_CALL LzmaDec_WriteRem (CLzmaDec *p, SizeT limit)
 
static int MY_FAST_CALL LzmaDec_DecodeReal2 (CLzmaDec *p, SizeT limit, const Byte *bufLimit)
 
static ELzmaDummy LzmaDec_TryDummy (const CLzmaDec *p, const Byte *buf, SizeT inSize)
 
static void LzmaDec_InitDicAndState (CLzmaDec *p, Bool initDic, Bool initState)
 
void LzmaDec_Init (CLzmaDec *p)
 
static void LzmaDec_InitStateReal (CLzmaDec *p)
 
SRes LzmaDec_DecodeToDic (CLzmaDec *p, SizeT dicLimit, const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status)
 
SRes LzmaDec_DecodeToBuf (CLzmaDec *p, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status)
 
void LzmaDec_FreeProbs (CLzmaDec *p, ISzAllocPtr alloc)
 
static void LzmaDec_FreeDict (CLzmaDec *p, ISzAllocPtr alloc)
 
void LzmaDec_Free (CLzmaDec *p, ISzAllocPtr alloc)
 
SRes LzmaProps_Decode (CLzmaProps *p, const Byte *data, unsigned size)
 
static SRes LzmaDec_AllocateProbs2 (CLzmaDec *p, const CLzmaProps *propNew, ISzAllocPtr alloc)
 
SRes LzmaDec_AllocateProbs (CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAllocPtr alloc)
 
SRes LzmaDec_Allocate (CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAllocPtr alloc)
 
SRes LzmaDecode (Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode, ELzmaStatus *status, ISzAllocPtr alloc)
 

Macro Definition Documentation

#define Align   (SpecPos + kNumFullDistances - kEndPosModelIndex)

Definition at line 111 of file LzmaDec.c.

#define GET_BIT (   p,
 
)    GET_BIT2(p, i, ; , ;)

Definition at line 27 of file LzmaDec.c.

#define GET_BIT2 (   p,
  i,
  A0,
  A1 
)
Value:
{ UPDATE_0(p); i = (i + i); A0; } else \
{ UPDATE_1(p); i = (i + i) + 1; A1; }

Definition at line 24 of file LzmaDec.c.

#define GET_BIT2_CHECK (   p,
  i,
  A0,
  A1 
)
Value:
{ UPDATE_0_CHECK; i = (i + i); A0; } else \
{ UPDATE_1_CHECK; i = (i + i) + 1; A1; }

Definition at line 61 of file LzmaDec.c.

#define GET_BIT_CHECK (   p,
 
)    GET_BIT2_CHECK(p, i, ; , ;)

Definition at line 64 of file LzmaDec.c.

#define IF_BIT_0 (   p)    ttt = *(p); NORMALIZE; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)

Definition at line 21 of file LzmaDec.c.

#define IF_BIT_0_CHECK (   p)    ttt = *(p); NORMALIZE_CHECK; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)

Definition at line 58 of file LzmaDec.c.

#define IsMatch   0

Definition at line 103 of file LzmaDec.c.

#define IsRep   (IsMatch + (kNumStates << kNumPosBitsMax))

Definition at line 104 of file LzmaDec.c.

#define IsRep0Long   (IsRepG2 + kNumStates)

Definition at line 108 of file LzmaDec.c.

#define IsRepG0   (IsRep + kNumStates)

Definition at line 105 of file LzmaDec.c.

#define IsRepG1   (IsRepG0 + kNumStates)

Definition at line 106 of file LzmaDec.c.

#define IsRepG2   (IsRepG1 + kNumStates)

Definition at line 107 of file LzmaDec.c.

#define kAlignTableSize   (1 << kNumAlignBits)

Definition at line 98 of file LzmaDec.c.

#define kBitModelTotal   (1 << kNumBitModelTotalBits)

Definition at line 14 of file LzmaDec.c.

#define kEndPosModelIndex   14

Definition at line 91 of file LzmaDec.c.

#define kLenNumHighBits   8

Definition at line 76 of file LzmaDec.c.

#define kLenNumHighSymbols   (1 << kLenNumHighBits)

Definition at line 77 of file LzmaDec.c.

#define kLenNumLowBits   3

Definition at line 72 of file LzmaDec.c.

#define kLenNumLowSymbols   (1 << kLenNumLowBits)

Definition at line 73 of file LzmaDec.c.

#define kLenNumMidBits   3

Definition at line 74 of file LzmaDec.c.

#define kLenNumMidSymbols   (1 << kLenNumMidBits)

Definition at line 75 of file LzmaDec.c.

#define kMatchMinLen   2

Definition at line 100 of file LzmaDec.c.

#define kMatchSpecLenStart   (kMatchMinLen + kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols)

Definition at line 101 of file LzmaDec.c.

#define kNumAlignBits   4

Definition at line 97 of file LzmaDec.c.

#define kNumBitModelTotalBits   11

Definition at line 13 of file LzmaDec.c.

#define kNumFullDistances   (1 << (kEndPosModelIndex >> 1))

Definition at line 92 of file LzmaDec.c.

#define kNumLenProbs   (LenHigh + kLenNumHighSymbols)

Definition at line 84 of file LzmaDec.c.

#define kNumLenToPosStates   4

Definition at line 95 of file LzmaDec.c.

#define kNumLitStates   7

Definition at line 88 of file LzmaDec.c.

#define kNumMoveBits   5

Definition at line 15 of file LzmaDec.c.

#define kNumPosBitsMax   4

Definition at line 69 of file LzmaDec.c.

#define kNumPosSlotBits   6

Definition at line 94 of file LzmaDec.c.

#define kNumPosStatesMax   (1 << kNumPosBitsMax)

Definition at line 70 of file LzmaDec.c.

#define kNumStates   12

Definition at line 87 of file LzmaDec.c.

#define kNumTopBits   24

Definition at line 10 of file LzmaDec.c.

#define kStartPosModelIndex   4

Definition at line 90 of file LzmaDec.c.

#define kTopValue   ((UInt32)1 << kNumTopBits)

Definition at line 11 of file LzmaDec.c.

#define LenChoice   0

Definition at line 79 of file LzmaDec.c.

#define LenChoice2   (LenChoice + 1)

Definition at line 80 of file LzmaDec.c.

#define LenCoder   (Align + kAlignTableSize)

Definition at line 112 of file LzmaDec.c.

#define LenHigh   (LenMid + (kNumPosStatesMax << kLenNumMidBits))

Definition at line 83 of file LzmaDec.c.

#define LenLow   (LenChoice2 + 1)

Definition at line 81 of file LzmaDec.c.

#define LenMid   (LenLow + (kNumPosStatesMax << kLenNumLowBits))

Definition at line 82 of file LzmaDec.c.

#define Literal   (RepLenCoder + kNumLenProbs)

Definition at line 114 of file LzmaDec.c.

#define LZMA_BASE_SIZE   1846

Definition at line 116 of file LzmaDec.c.

#define LZMA_DIC_MIN   (1 << 12)

Definition at line 125 of file LzmaDec.c.

#define LZMA_LIT_SIZE   0x300

Definition at line 117 of file LzmaDec.c.

#define LzmaProps_GetNumProbs (   p)    (Literal + ((UInt32)LZMA_LIT_SIZE << ((p)->lc + (p)->lp)))

Definition at line 123 of file LzmaDec.c.

#define MATCHED_LITER_DEC
Value:
matchByte <<= 1; \
bit = (matchByte & offs); \
probLit = prob + offs + bit + symbol; \
GET_BIT2(probLit, symbol, offs &= ~bit, offs &= bit)

Definition at line 50 of file LzmaDec.c.

#define NORMAL_LITER_DEC   GET_BIT(prob + symbol, symbol)

Definition at line 49 of file LzmaDec.c.

#define NORMALIZE   if (range < kTopValue) { range <<= 8; code = (code << 8) | (*buf++); }

Definition at line 19 of file LzmaDec.c.

#define NORMALIZE_CHECK   if (range < kTopValue) { if (buf >= bufLimit) return DUMMY_ERROR; range <<= 8; code = (code << 8) | (*buf++); }

Definition at line 56 of file LzmaDec.c.

#define PosSlot   (IsRep0Long + (kNumStates << kNumPosBitsMax))

Definition at line 109 of file LzmaDec.c.

#define RC_INIT_SIZE   5

Definition at line 17 of file LzmaDec.c.

#define RepLenCoder   (LenCoder + kNumLenProbs)

Definition at line 113 of file LzmaDec.c.

#define SpecPos   (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))

Definition at line 110 of file LzmaDec.c.

#define TREE_6_DECODE (   probs,
 
)
Value:
{ i = 1; \
TREE_GET_BIT(probs, i); \
TREE_GET_BIT(probs, i); \
TREE_GET_BIT(probs, i); \
TREE_GET_BIT(probs, i); \
TREE_GET_BIT(probs, i); \
TREE_GET_BIT(probs, i); \
i -= 0x40; }

Definition at line 38 of file LzmaDec.c.

#define TREE_DECODE (   probs,
  limit,
 
)    { i = 1; do { TREE_GET_BIT(probs, i); } while (i < limit); i -= limit; }

Definition at line 30 of file LzmaDec.c.

#define TREE_DECODE_CHECK (   probs,
  limit,
 
)    { i = 1; do { GET_BIT_CHECK(probs + i, i) } while (i < limit); i -= limit; }

Definition at line 65 of file LzmaDec.c.

#define TREE_GET_BIT (   probs,
 
)    { GET_BIT((probs + i), i); }

Definition at line 29 of file LzmaDec.c.

#define UPDATE_0 (   p)    range = bound; *(p) = (CLzmaProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits));

Definition at line 22 of file LzmaDec.c.

#define UPDATE_0_CHECK   range = bound;

Definition at line 59 of file LzmaDec.c.

#define UPDATE_1 (   p)    range -= bound; code -= bound; *(p) = (CLzmaProb)(ttt - (ttt >> kNumMoveBits));

Definition at line 23 of file LzmaDec.c.

#define UPDATE_1_CHECK   range -= bound; code -= bound;

Definition at line 60 of file LzmaDec.c.

Enumeration Type Documentation

enum ELzmaDummy
Enumerator
DUMMY_ERROR 
DUMMY_LIT 
DUMMY_MATCH 
DUMMY_REP 

Definition at line 570 of file LzmaDec.c.

571 {
572  DUMMY_ERROR, /* unexpected end of input stream */
573  DUMMY_LIT,
574  DUMMY_MATCH,
575  DUMMY_REP
576 } ELzmaDummy;

Function Documentation

SRes LzmaDec_Allocate ( CLzmaDec p,
const Byte props,
unsigned  propsSize,
ISzAllocPtr  alloc 
)

Definition at line 1045 of file LzmaDec.c.

1046 {
1047  CLzmaProps propNew;
1048  SizeT dicBufSize;
1049  RINOK(LzmaProps_Decode(&propNew, props, propsSize));
1050  RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc));
1051 
1052  {
1053  UInt32 dictSize = propNew.dicSize;
1054  SizeT mask = ((UInt32)1 << 12) - 1;
1055  if (dictSize >= ((UInt32)1 << 30)) mask = ((UInt32)1 << 22) - 1;
1056  else if (dictSize >= ((UInt32)1 << 22)) mask = ((UInt32)1 << 20) - 1;;
1057  dicBufSize = ((SizeT)dictSize + mask) & ~mask;
1058  if (dicBufSize < dictSize)
1059  dicBufSize = dictSize;
1060  }
1061 
1062  if (!p->dic || dicBufSize != p->dicBufSize)
1063  {
1064  LzmaDec_FreeDict(p, alloc);
1065  p->dic = (Byte *)ISzAlloc_Alloc(alloc, dicBufSize);
1066  if (!p->dic)
1067  {
1068  LzmaDec_FreeProbs(p, alloc);
1069  return SZ_ERROR_MEM;
1070  }
1071  }
1072  p->dicBufSize = dicBufSize;
1073  p->prop = propNew;
1074  return SZ_OK;
1075 }
SRes LzmaDec_AllocateProbs ( CLzmaDec p,
const Byte props,
unsigned  propsSize,
ISzAllocPtr  alloc 
)

Definition at line 1036 of file LzmaDec.c.

1037 {
1038  CLzmaProps propNew;
1039  RINOK(LzmaProps_Decode(&propNew, props, propsSize));
1040  RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc));
1041  p->prop = propNew;
1042  return SZ_OK;
1043 }

+ Here is the caller graph for this function:

static SRes LzmaDec_AllocateProbs2 ( CLzmaDec p,
const CLzmaProps propNew,
ISzAllocPtr  alloc 
)
static

Definition at line 1022 of file LzmaDec.c.

1023 {
1024  UInt32 numProbs = LzmaProps_GetNumProbs(propNew);
1025  if (!p->probs || numProbs != p->numProbs)
1026  {
1027  LzmaDec_FreeProbs(p, alloc);
1028  p->probs = (CLzmaProb *)ISzAlloc_Alloc(alloc, numProbs * sizeof(CLzmaProb));
1029  p->numProbs = numProbs;
1030  if (!p->probs)
1031  return SZ_ERROR_MEM;
1032  }
1033  return SZ_OK;
1034 }

+ Here is the caller graph for this function:

static int MY_FAST_CALL LzmaDec_DecodeReal ( CLzmaDec p,
SizeT  limit,
const Byte bufLimit 
)
static

Definition at line 140 of file LzmaDec.c.

141 {
142  CLzmaProb *probs = p->probs;
143 
144  unsigned state = p->state;
145  UInt32 rep0 = p->reps[0], rep1 = p->reps[1], rep2 = p->reps[2], rep3 = p->reps[3];
146  unsigned pbMask = ((unsigned)1 << (p->prop.pb)) - 1;
147  unsigned lpMask = ((unsigned)1 << (p->prop.lp)) - 1;
148  unsigned lc = p->prop.lc;
149 
150  Byte *dic = p->dic;
151  SizeT dicBufSize = p->dicBufSize;
152  SizeT dicPos = p->dicPos;
153 
154  UInt32 processedPos = p->processedPos;
155  UInt32 checkDicSize = p->checkDicSize;
156  unsigned len = 0;
157 
158  const Byte *buf = p->buf;
159  UInt32 range = p->range;
160  UInt32 code = p->code;
161 
162  do
163  {
164  CLzmaProb *prob;
165  UInt32 bound;
166  unsigned ttt;
167  unsigned posState = processedPos & pbMask;
168 
169  prob = probs + IsMatch + (state << kNumPosBitsMax) + posState;
170  IF_BIT_0(prob)
171  {
172  unsigned symbol;
173  UPDATE_0(prob);
174  prob = probs + Literal;
175  if (processedPos != 0 || checkDicSize != 0)
176  prob += ((UInt32)LZMA_LIT_SIZE * (((processedPos & lpMask) << lc) +
177  (dic[(dicPos == 0 ? dicBufSize : dicPos) - 1] >> (8 - lc))));
178  processedPos++;
179 
180  if (state < kNumLitStates)
181  {
182  state -= (state < 4) ? state : 3;
183  symbol = 1;
184  #ifdef _LZMA_SIZE_OPT
185  do { NORMAL_LITER_DEC } while (symbol < 0x100);
186  #else
195  #endif
196  }
197  else
198  {
199  unsigned matchByte = dic[dicPos - rep0 + (dicPos < rep0 ? dicBufSize : 0)];
200  unsigned offs = 0x100;
201  state -= (state < 10) ? 3 : 6;
202  symbol = 1;
203  #ifdef _LZMA_SIZE_OPT
204  do
205  {
206  unsigned bit;
207  CLzmaProb *probLit;
209  }
210  while (symbol < 0x100);
211  #else
212  {
213  unsigned bit;
214  CLzmaProb *probLit;
223  }
224  #endif
225  }
226 
227  dic[dicPos++] = (Byte)symbol;
228  continue;
229  }
230 
231  {
232  UPDATE_1(prob);
233  prob = probs + IsRep + state;
234  IF_BIT_0(prob)
235  {
236  UPDATE_0(prob);
237  state += kNumStates;
238  prob = probs + LenCoder;
239  }
240  else
241  {
242  UPDATE_1(prob);
243  if (checkDicSize == 0 && processedPos == 0)
244  return SZ_ERROR_DATA;
245  prob = probs + IsRepG0 + state;
246  IF_BIT_0(prob)
247  {
248  UPDATE_0(prob);
249  prob = probs + IsRep0Long + (state << kNumPosBitsMax) + posState;
250  IF_BIT_0(prob)
251  {
252  UPDATE_0(prob);
253  dic[dicPos] = dic[dicPos - rep0 + (dicPos < rep0 ? dicBufSize : 0)];
254  dicPos++;
255  processedPos++;
256  state = state < kNumLitStates ? 9 : 11;
257  continue;
258  }
259  UPDATE_1(prob);
260  }
261  else
262  {
263  UInt32 distance;
264  UPDATE_1(prob);
265  prob = probs + IsRepG1 + state;
266  IF_BIT_0(prob)
267  {
268  UPDATE_0(prob);
269  distance = rep1;
270  }
271  else
272  {
273  UPDATE_1(prob);
274  prob = probs + IsRepG2 + state;
275  IF_BIT_0(prob)
276  {
277  UPDATE_0(prob);
278  distance = rep2;
279  }
280  else
281  {
282  UPDATE_1(prob);
283  distance = rep3;
284  rep3 = rep2;
285  }
286  rep2 = rep1;
287  }
288  rep1 = rep0;
289  rep0 = distance;
290  }
291  state = state < kNumLitStates ? 8 : 11;
292  prob = probs + RepLenCoder;
293  }
294 
295  #ifdef _LZMA_SIZE_OPT
296  {
297  unsigned lim, offset;
298  CLzmaProb *probLen = prob + LenChoice;
299  IF_BIT_0(probLen)
300  {
301  UPDATE_0(probLen);
302  probLen = prob + LenLow + (posState << kLenNumLowBits);
303  offset = 0;
304  lim = (1 << kLenNumLowBits);
305  }
306  else
307  {
308  UPDATE_1(probLen);
309  probLen = prob + LenChoice2;
310  IF_BIT_0(probLen)
311  {
312  UPDATE_0(probLen);
313  probLen = prob + LenMid + (posState << kLenNumMidBits);
314  offset = kLenNumLowSymbols;
315  lim = (1 << kLenNumMidBits);
316  }
317  else
318  {
319  UPDATE_1(probLen);
320  probLen = prob + LenHigh;
322  lim = (1 << kLenNumHighBits);
323  }
324  }
325  TREE_DECODE(probLen, lim, len);
326  len += offset;
327  }
328  #else
329  {
330  CLzmaProb *probLen = prob + LenChoice;
331  IF_BIT_0(probLen)
332  {
333  UPDATE_0(probLen);
334  probLen = prob + LenLow + (posState << kLenNumLowBits);
335  len = 1;
336  TREE_GET_BIT(probLen, len);
337  TREE_GET_BIT(probLen, len);
338  TREE_GET_BIT(probLen, len);
339  len -= 8;
340  }
341  else
342  {
343  UPDATE_1(probLen);
344  probLen = prob + LenChoice2;
345  IF_BIT_0(probLen)
346  {
347  UPDATE_0(probLen);
348  probLen = prob + LenMid + (posState << kLenNumMidBits);
349  len = 1;
350  TREE_GET_BIT(probLen, len);
351  TREE_GET_BIT(probLen, len);
352  TREE_GET_BIT(probLen, len);
353  }
354  else
355  {
356  UPDATE_1(probLen);
357  probLen = prob + LenHigh;
358  TREE_DECODE(probLen, (1 << kLenNumHighBits), len);
360  }
361  }
362  }
363  #endif
364 
365  if (state >= kNumStates)
366  {
367  UInt32 distance;
368  prob = probs + PosSlot +
369  ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) << kNumPosSlotBits);
370  TREE_6_DECODE(prob, distance);
371  if (distance >= kStartPosModelIndex)
372  {
373  unsigned posSlot = (unsigned)distance;
374  unsigned numDirectBits = (unsigned)(((distance >> 1) - 1));
375  distance = (2 | (distance & 1));
376  if (posSlot < kEndPosModelIndex)
377  {
378  distance <<= numDirectBits;
379  prob = probs + SpecPos + distance - posSlot - 1;
380  {
381  UInt32 mask = 1;
382  unsigned i = 1;
383  do
384  {
385  GET_BIT2(prob + i, i, ; , distance |= mask);
386  mask <<= 1;
387  }
388  while (--numDirectBits != 0);
389  }
390  }
391  else
392  {
393  numDirectBits -= kNumAlignBits;
394  do
395  {
396  NORMALIZE
397  range >>= 1;
398 
399  {
400  UInt32 t;
401  code -= range;
402  t = (0 - ((UInt32)code >> 31)); /* (UInt32)((Int32)code >> 31) */
403  distance = (distance << 1) + (t + 1);
404  code += range & t;
405  }
406  /*
407  distance <<= 1;
408  if (code >= range)
409  {
410  code -= range;
411  distance |= 1;
412  }
413  */
414  }
415  while (--numDirectBits != 0);
416  prob = probs + Align;
417  distance <<= kNumAlignBits;
418  {
419  unsigned i = 1;
420  GET_BIT2(prob + i, i, ; , distance |= 1);
421  GET_BIT2(prob + i, i, ; , distance |= 2);
422  GET_BIT2(prob + i, i, ; , distance |= 4);
423  GET_BIT2(prob + i, i, ; , distance |= 8);
424  }
425  if (distance == (UInt32)0xFFFFFFFF)
426  {
427  len += kMatchSpecLenStart;
428  state -= kNumStates;
429  break;
430  }
431  }
432  }
433 
434  rep3 = rep2;
435  rep2 = rep1;
436  rep1 = rep0;
437  rep0 = distance + 1;
438  if (checkDicSize == 0)
439  {
440  if (distance >= processedPos)
441  {
442  p->dicPos = dicPos;
443  return SZ_ERROR_DATA;
444  }
445  }
446  else if (distance >= checkDicSize)
447  {
448  p->dicPos = dicPos;
449  return SZ_ERROR_DATA;
450  }
451  state = (state < kNumStates + kNumLitStates) ? kNumLitStates : kNumLitStates + 3;
452  }
453 
454  len += kMatchMinLen;
455 
456  {
457  SizeT rem;
458  unsigned curLen;
459  SizeT pos;
460 
461  if ((rem = limit - dicPos) == 0)
462  {
463  p->dicPos = dicPos;
464  return SZ_ERROR_DATA;
465  }
466 
467  curLen = ((rem < len) ? (unsigned)rem : len);
468  pos = dicPos - rep0 + (dicPos < rep0 ? dicBufSize : 0);
469 
470  processedPos += curLen;
471 
472  len -= curLen;
473  if (curLen <= dicBufSize - pos)
474  {
475  Byte *dest = dic + dicPos;
476  ptrdiff_t src = (ptrdiff_t)pos - (ptrdiff_t)dicPos;
477  const Byte *lim = dest + curLen;
478  dicPos += curLen;
479  do
480  *(dest) = (Byte)*(dest + src);
481  while (++dest != lim);
482  }
483  else
484  {
485  do
486  {
487  dic[dicPos++] = dic[pos];
488  if (++pos == dicBufSize)
489  pos = 0;
490  }
491  while (--curLen != 0);
492  }
493  }
494  }
495  }
496  while (dicPos < limit && buf < bufLimit);
497 
498  NORMALIZE;
499 
500  p->buf = buf;
501  p->range = range;
502  p->code = code;
503  p->remainLen = len;
504  p->dicPos = dicPos;
505  p->processedPos = processedPos;
506  p->reps[0] = rep0;
507  p->reps[1] = rep1;
508  p->reps[2] = rep2;
509  p->reps[3] = rep3;
510  p->state = state;
511 
512  return SZ_OK;
513 }

+ Here is the caller graph for this function:

static int MY_FAST_CALL LzmaDec_DecodeReal2 ( CLzmaDec p,
SizeT  limit,
const Byte bufLimit 
)
static

Definition at line 543 of file LzmaDec.c.

544 {
545  do
546  {
547  SizeT limit2 = limit;
548  if (p->checkDicSize == 0)
549  {
550  UInt32 rem = p->prop.dicSize - p->processedPos;
551  if (limit - p->dicPos > rem)
552  limit2 = p->dicPos + rem;
553  }
554 
555  RINOK(LzmaDec_DecodeReal(p, limit2, bufLimit));
556 
557  if (p->checkDicSize == 0 && p->processedPos >= p->prop.dicSize)
558  p->checkDicSize = p->prop.dicSize;
559 
560  LzmaDec_WriteRem(p, limit);
561  }
562  while (p->dicPos < limit && p->buf < bufLimit && p->remainLen < kMatchSpecLenStart);
563 
564  if (p->remainLen > kMatchSpecLenStart)
566 
567  return 0;
568 }

+ Here is the caller graph for this function:

SRes LzmaDec_DecodeToBuf ( CLzmaDec p,
Byte dest,
SizeT destLen,
const Byte src,
SizeT srcLen,
ELzmaFinishMode  finishMode,
ELzmaStatus status 
)

Definition at line 938 of file LzmaDec.c.

939 {
940  SizeT outSize = *destLen;
941  SizeT inSize = *srcLen;
942  *srcLen = *destLen = 0;
943  for (;;)
944  {
945  SizeT inSizeCur = inSize, outSizeCur, dicPos;
946  ELzmaFinishMode curFinishMode;
947  SRes res;
948  if (p->dicPos == p->dicBufSize)
949  p->dicPos = 0;
950  dicPos = p->dicPos;
951  if (outSize > p->dicBufSize - dicPos)
952  {
953  outSizeCur = p->dicBufSize;
954  curFinishMode = LZMA_FINISH_ANY;
955  }
956  else
957  {
958  outSizeCur = dicPos + outSize;
959  curFinishMode = finishMode;
960  }
961 
962  res = LzmaDec_DecodeToDic(p, outSizeCur, src, &inSizeCur, curFinishMode, status);
963  src += inSizeCur;
964  inSize -= inSizeCur;
965  *srcLen += inSizeCur;
966  outSizeCur = p->dicPos - dicPos;
967  memcpy(dest, p->dic + dicPos, outSizeCur);
968  dest += outSizeCur;
969  outSize -= outSizeCur;
970  *destLen += outSizeCur;
971  if (res != 0)
972  return res;
973  if (outSizeCur == 0 || outSize == 0)
974  return SZ_OK;
975  }
976 }
SRes LzmaDec_DecodeToDic ( CLzmaDec p,
SizeT  dicLimit,
const Byte src,
SizeT srcLen,
ELzmaFinishMode  finishMode,
ELzmaStatus status 
)

Definition at line 803 of file LzmaDec.c.

805 {
806  SizeT inSize = *srcLen;
807  (*srcLen) = 0;
808  LzmaDec_WriteRem(p, dicLimit);
809 
810  *status = LZMA_STATUS_NOT_SPECIFIED;
811 
812  while (p->remainLen != kMatchSpecLenStart)
813  {
814  int checkEndMarkNow;
815 
816  if (p->needFlush)
817  {
818  for (; inSize > 0 && p->tempBufSize < RC_INIT_SIZE; (*srcLen)++, inSize--)
819  p->tempBuf[p->tempBufSize++] = *src++;
820  if (p->tempBufSize < RC_INIT_SIZE)
821  {
823  return SZ_OK;
824  }
825  if (p->tempBuf[0] != 0)
826  return SZ_ERROR_DATA;
827  p->code =
828  ((UInt32)p->tempBuf[1] << 24)
829  | ((UInt32)p->tempBuf[2] << 16)
830  | ((UInt32)p->tempBuf[3] << 8)
831  | ((UInt32)p->tempBuf[4]);
832  p->range = 0xFFFFFFFF;
833  p->needFlush = 0;
834  p->tempBufSize = 0;
835  }
836 
837  checkEndMarkNow = 0;
838  if (p->dicPos >= dicLimit)
839  {
840  if (p->remainLen == 0 && p->code == 0)
841  {
843  return SZ_OK;
844  }
845  if (finishMode == LZMA_FINISH_ANY)
846  {
847  *status = LZMA_STATUS_NOT_FINISHED;
848  return SZ_OK;
849  }
850  if (p->remainLen != 0)
851  {
852  *status = LZMA_STATUS_NOT_FINISHED;
853  return SZ_ERROR_DATA;
854  }
855  checkEndMarkNow = 1;
856  }
857 
858  if (p->needInitState)
860 
861  if (p->tempBufSize == 0)
862  {
863  SizeT processed;
864  const Byte *bufLimit;
865  if (inSize < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow)
866  {
867  int dummyRes = LzmaDec_TryDummy(p, src, inSize);
868  if (dummyRes == DUMMY_ERROR)
869  {
870  memcpy(p->tempBuf, src, inSize);
871  p->tempBufSize = (unsigned)inSize;
872  (*srcLen) += inSize;
874  return SZ_OK;
875  }
876  if (checkEndMarkNow && dummyRes != DUMMY_MATCH)
877  {
878  *status = LZMA_STATUS_NOT_FINISHED;
879  return SZ_ERROR_DATA;
880  }
881  bufLimit = src;
882  }
883  else
884  bufLimit = src + inSize - LZMA_REQUIRED_INPUT_MAX;
885  p->buf = src;
886  if (LzmaDec_DecodeReal2(p, dicLimit, bufLimit) != 0)
887  return SZ_ERROR_DATA;
888  processed = (SizeT)(p->buf - src);
889  (*srcLen) += processed;
890  src += processed;
891  inSize -= processed;
892  }
893  else
894  {
895  unsigned rem = p->tempBufSize, lookAhead = 0;
896  while (rem < LZMA_REQUIRED_INPUT_MAX && lookAhead < inSize)
897  p->tempBuf[rem++] = src[lookAhead++];
898  p->tempBufSize = rem;
899  if (rem < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow)
900  {
901  int dummyRes = LzmaDec_TryDummy(p, p->tempBuf, rem);
902  if (dummyRes == DUMMY_ERROR)
903  {
904  (*srcLen) += lookAhead;
906  return SZ_OK;
907  }
908  if (checkEndMarkNow && dummyRes != DUMMY_MATCH)
909  {
910  *status = LZMA_STATUS_NOT_FINISHED;
911  return SZ_ERROR_DATA;
912  }
913  }
914  p->buf = p->tempBuf;
915  if (LzmaDec_DecodeReal2(p, dicLimit, p->buf) != 0)
916  return SZ_ERROR_DATA;
917 
918  {
919  unsigned kkk = (unsigned)(p->buf - p->tempBuf);
920  if (rem < kkk)
921  return SZ_ERROR_FAIL; /* some internal error */
922  rem -= kkk;
923  if (lookAhead < rem)
924  return SZ_ERROR_FAIL; /* some internal error */
925  lookAhead -= rem;
926  }
927  (*srcLen) += lookAhead;
928  src += lookAhead;
929  inSize -= lookAhead;
930  p->tempBufSize = 0;
931  }
932  }
933  if (p->code == 0)
935  return (p->code == 0) ? SZ_OK : SZ_ERROR_DATA;
936 }

+ Here is the caller graph for this function:

void LzmaDec_Free ( CLzmaDec p,
ISzAllocPtr  alloc 
)

Definition at line 990 of file LzmaDec.c.

991 {
992  LzmaDec_FreeProbs(p, alloc);
993  LzmaDec_FreeDict(p, alloc);
994 }
static void LzmaDec_FreeDict ( CLzmaDec p,
ISzAllocPtr  alloc 
)
static

Definition at line 984 of file LzmaDec.c.

985 {
986  ISzAlloc_Free(alloc, p->dic);
987  p->dic = NULL;
988 }

+ Here is the caller graph for this function:

void LzmaDec_FreeProbs ( CLzmaDec p,
ISzAllocPtr  alloc 
)

Definition at line 978 of file LzmaDec.c.

979 {
980  ISzAlloc_Free(alloc, p->probs);
981  p->probs = NULL;
982 }

+ Here is the caller graph for this function:

void LzmaDec_Init ( CLzmaDec p)

Definition at line 785 of file LzmaDec.c.

786 {
787  p->dicPos = 0;
789 }

+ Here is the caller graph for this function:

static void LzmaDec_InitDicAndState ( CLzmaDec p,
Bool  initDic,
Bool  initState 
)
static

Definition at line 769 of file LzmaDec.c.

770 {
771  p->needFlush = 1;
772  p->remainLen = 0;
773  p->tempBufSize = 0;
774 
775  if (initDic)
776  {
777  p->processedPos = 0;
778  p->checkDicSize = 0;
779  p->needInitState = 1;
780  }
781  if (initState)
782  p->needInitState = 1;
783 }

+ Here is the caller graph for this function:

static void LzmaDec_InitStateReal ( CLzmaDec p)
static

Definition at line 791 of file LzmaDec.c.

792 {
793  SizeT numProbs = LzmaProps_GetNumProbs(&p->prop);
794  SizeT i;
795  CLzmaProb *probs = p->probs;
796  for (i = 0; i < numProbs; i++)
797  probs[i] = kBitModelTotal >> 1;
798  p->reps[0] = p->reps[1] = p->reps[2] = p->reps[3] = 1;
799  p->state = 0;
800  p->needInitState = 0;
801 }

+ Here is the caller graph for this function:

static ELzmaDummy LzmaDec_TryDummy ( const CLzmaDec p,
const Byte buf,
SizeT  inSize 
)
static

Definition at line 578 of file LzmaDec.c.

579 {
580  UInt32 range = p->range;
581  UInt32 code = p->code;
582  const Byte *bufLimit = buf + inSize;
583  const CLzmaProb *probs = p->probs;
584  unsigned state = p->state;
585  ELzmaDummy res;
586 
587  {
588  const CLzmaProb *prob;
589  UInt32 bound;
590  unsigned ttt;
591  unsigned posState = (p->processedPos) & ((1 << p->prop.pb) - 1);
592 
593  prob = probs + IsMatch + (state << kNumPosBitsMax) + posState;
594  IF_BIT_0_CHECK(prob)
595  {
597 
598  /* if (bufLimit - buf >= 7) return DUMMY_LIT; */
599 
600  prob = probs + Literal;
601  if (p->checkDicSize != 0 || p->processedPos != 0)
602  prob += ((UInt32)LZMA_LIT_SIZE *
603  ((((p->processedPos) & ((1 << (p->prop.lp)) - 1)) << p->prop.lc) +
604  (p->dic[(p->dicPos == 0 ? p->dicBufSize : p->dicPos) - 1] >> (8 - p->prop.lc))));
605 
606  if (state < kNumLitStates)
607  {
608  unsigned symbol = 1;
609  do { GET_BIT_CHECK(prob + symbol, symbol) } while (symbol < 0x100);
610  }
611  else
612  {
613  unsigned matchByte = p->dic[p->dicPos - p->reps[0] +
614  (p->dicPos < p->reps[0] ? p->dicBufSize : 0)];
615  unsigned offs = 0x100;
616  unsigned symbol = 1;
617  do
618  {
619  unsigned bit;
620  const CLzmaProb *probLit;
621  matchByte <<= 1;
622  bit = (matchByte & offs);
623  probLit = prob + offs + bit + symbol;
624  GET_BIT2_CHECK(probLit, symbol, offs &= ~bit, offs &= bit)
625  }
626  while (symbol < 0x100);
627  }
628  res = DUMMY_LIT;
629  }
630  else
631  {
632  unsigned len;
634 
635  prob = probs + IsRep + state;
636  IF_BIT_0_CHECK(prob)
637  {
639  state = 0;
640  prob = probs + LenCoder;
641  res = DUMMY_MATCH;
642  }
643  else
644  {
646  res = DUMMY_REP;
647  prob = probs + IsRepG0 + state;
648  IF_BIT_0_CHECK(prob)
649  {
651  prob = probs + IsRep0Long + (state << kNumPosBitsMax) + posState;
652  IF_BIT_0_CHECK(prob)
653  {
656  return DUMMY_REP;
657  }
658  else
659  {
661  }
662  }
663  else
664  {
666  prob = probs + IsRepG1 + state;
667  IF_BIT_0_CHECK(prob)
668  {
670  }
671  else
672  {
674  prob = probs + IsRepG2 + state;
675  IF_BIT_0_CHECK(prob)
676  {
678  }
679  else
680  {
682  }
683  }
684  }
685  state = kNumStates;
686  prob = probs + RepLenCoder;
687  }
688  {
689  unsigned limit, offset;
690  const CLzmaProb *probLen = prob + LenChoice;
691  IF_BIT_0_CHECK(probLen)
692  {
694  probLen = prob + LenLow + (posState << kLenNumLowBits);
695  offset = 0;
696  limit = 1 << kLenNumLowBits;
697  }
698  else
699  {
701  probLen = prob + LenChoice2;
702  IF_BIT_0_CHECK(probLen)
703  {
705  probLen = prob + LenMid + (posState << kLenNumMidBits);
706  offset = kLenNumLowSymbols;
707  limit = 1 << kLenNumMidBits;
708  }
709  else
710  {
712  probLen = prob + LenHigh;
714  limit = 1 << kLenNumHighBits;
715  }
716  }
717  TREE_DECODE_CHECK(probLen, limit, len);
718  len += offset;
719  }
720 
721  if (state < 4)
722  {
723  unsigned posSlot;
724  prob = probs + PosSlot +
725  ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) <<
727  TREE_DECODE_CHECK(prob, 1 << kNumPosSlotBits, posSlot);
728  if (posSlot >= kStartPosModelIndex)
729  {
730  unsigned numDirectBits = ((posSlot >> 1) - 1);
731 
732  /* if (bufLimit - buf >= 8) return DUMMY_MATCH; */
733 
734  if (posSlot < kEndPosModelIndex)
735  {
736  prob = probs + SpecPos + ((2 | (posSlot & 1)) << numDirectBits) - posSlot - 1;
737  }
738  else
739  {
740  numDirectBits -= kNumAlignBits;
741  do
742  {
744  range >>= 1;
745  code -= range & (((code - range) >> 31) - 1);
746  /* if (code >= range) code -= range; */
747  }
748  while (--numDirectBits != 0);
749  prob = probs + Align;
750  numDirectBits = kNumAlignBits;
751  }
752  {
753  unsigned i = 1;
754  do
755  {
756  GET_BIT_CHECK(prob + i, i);
757  }
758  while (--numDirectBits != 0);
759  }
760  }
761  }
762  }
763  }
765  return res;
766 }

+ Here is the caller graph for this function:

static void MY_FAST_CALL LzmaDec_WriteRem ( CLzmaDec p,
SizeT  limit 
)
static

Definition at line 515 of file LzmaDec.c.

516 {
517  if (p->remainLen != 0 && p->remainLen < kMatchSpecLenStart)
518  {
519  Byte *dic = p->dic;
520  SizeT dicPos = p->dicPos;
521  SizeT dicBufSize = p->dicBufSize;
522  unsigned len = p->remainLen;
523  SizeT rep0 = p->reps[0]; /* we use SizeT to avoid the BUG of VC14 for AMD64 */
524  SizeT rem = limit - dicPos;
525  if (rem < len)
526  len = (unsigned)(rem);
527 
528  if (p->checkDicSize == 0 && p->prop.dicSize - p->processedPos <= len)
529  p->checkDicSize = p->prop.dicSize;
530 
531  p->processedPos += len;
532  p->remainLen -= len;
533  while (len != 0)
534  {
535  len--;
536  dic[dicPos] = dic[dicPos - rep0 + (dicPos < rep0 ? dicBufSize : 0)];
537  dicPos++;
538  }
539  p->dicPos = dicPos;
540  }
541 }

+ Here is the caller graph for this function:

SRes LzmaDecode ( Byte dest,
SizeT destLen,
const Byte src,
SizeT srcLen,
const Byte propData,
unsigned  propSize,
ELzmaFinishMode  finishMode,
ELzmaStatus status,
ISzAllocPtr  alloc 
)

Definition at line 1077 of file LzmaDec.c.

1080 {
1081  CLzmaDec p;
1082  SRes res;
1083  SizeT outSize = *destLen, inSize = *srcLen;
1084  *destLen = *srcLen = 0;
1085  *status = LZMA_STATUS_NOT_SPECIFIED;
1086  if (inSize < RC_INIT_SIZE)
1087  return SZ_ERROR_INPUT_EOF;
1088  LzmaDec_Construct(&p);
1089  RINOK(LzmaDec_AllocateProbs(&p, propData, propSize, alloc));
1090  p.dic = dest;
1091  p.dicBufSize = outSize;
1092  LzmaDec_Init(&p);
1093  *srcLen = inSize;
1094  res = LzmaDec_DecodeToDic(&p, outSize, src, srcLen, finishMode, status);
1095  *destLen = p.dicPos;
1096  if (res == SZ_OK && *status == LZMA_STATUS_NEEDS_MORE_INPUT)
1097  res = SZ_ERROR_INPUT_EOF;
1098  LzmaDec_FreeProbs(&p, alloc);
1099  return res;
1100 }
SRes LzmaProps_Decode ( CLzmaProps p,
const Byte data,
unsigned  size 
)

Definition at line 996 of file LzmaDec.c.

997 {
998  UInt32 dicSize;
999  Byte d;
1000 
1001  if (size < LZMA_PROPS_SIZE)
1002  return SZ_ERROR_UNSUPPORTED;
1003  else
1004  dicSize = data[1] | ((UInt32)data[2] << 8) | ((UInt32)data[3] << 16) | ((UInt32)data[4] << 24);
1005 
1006  if (dicSize < LZMA_DIC_MIN)
1007  dicSize = LZMA_DIC_MIN;
1008  p->dicSize = dicSize;
1009 
1010  d = data[0];
1011  if (d >= (9 * 5 * 5))
1012  return SZ_ERROR_UNSUPPORTED;
1013 
1014  p->lc = d % 9;
1015  d /= 9;
1016  p->pb = d / 5;
1017  p->lp = d % 5;
1018 
1019  return SZ_OK;
1020 }

+ Here is the caller graph for this function: