Subversion Repositories Games.Chess Giants

Rev

Blame | Last modification | View Log | Download | RSS feed

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