home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 March / CMCD0304.ISO / Software / Freeware / Programare / nullsoft / nsis20.exe / Source / bzip2 / decompress.c < prev    next >
C/C++ Source or Header  |  2002-10-03  |  16KB  |  523 lines

  1. #include "bzlib.h"
  2.  
  3. #if defined(NSIS_COMPRESS_USE_BZIP2) && defined(NSIS_CONFIG_COMPRESSION_SUPPORT)
  4.  
  5. /*-------------------------------------------------------------*/
  6. /*--- Decompression machinery                               ---*/
  7. /*---                                          decompress.c ---*/
  8. /*-------------------------------------------------------------*/
  9.  
  10. /*--
  11.   This file is a part of bzip2 and/or libbzip2, a program and
  12.   library for lossless, block-sorting data compression.
  13.  
  14.   Copyright (C) 1996-2000 Julian R Seward.  All rights reserved.
  15.  
  16.   Redistribution and use in source and binary forms, with or without
  17.   modification, are permitted provided that the following conditions
  18.   are met:
  19.  
  20.   1. Redistributions of source code must retain the above copyright
  21.      notice, this list of conditions and the following disclaimer.
  22.  
  23.   2. The origin of this software must not be misrepresented; you must
  24.      not claim that you wrote the original software.  If you use this
  25.      software in a product, an acknowledgment in the product
  26.      documentation would be appreciated but is not required.
  27.  
  28.   3. Altered source versions must be plainly marked as such, and must
  29.      not be misrepresented as being the original software.
  30.  
  31.   4. The name of the author may not be used to endorse or promote
  32.      products derived from this software without specific prior written
  33.      permission.
  34.  
  35.   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  36.   OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  37.   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  38.   ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  39.   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  40.   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  41.   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  42.   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  43.   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  44.   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  45.   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46.  
  47.   Julian Seward, Cambridge, UK.
  48.   jseward@acm.org
  49.   bzip2/libbzip2 version 1.0 of 21 March 2000
  50.  
  51.   This program is based on (at least) the work of:
  52.      Mike Burrows
  53.      David Wheeler
  54.      Peter Fenwick
  55.      Alistair Moffat
  56.      Radford Neal
  57.      Ian H. Witten
  58.      Robert Sedgewick
  59.      Jon L. Bentley
  60.  
  61.   For more information on these sources, see the manual.
  62. --*/
  63.  
  64.  
  65. /*---------------------------------------------------*/
  66. #define RETURN(rrr)                               \
  67.    { retVal = rrr; goto save_state_and_return; };
  68.  
  69.  
  70. static int __mygetbits(int *vtmp, int nnn, DState* s)
  71. {
  72.    for (;;) {
  73.       if (s->bsLive >= nnn) {
  74.          UInt32 v;
  75.          v = (s->bsBuff >>
  76.              (s->bsLive-nnn)) & ((1 << nnn)-1);
  77.          s->bsLive -= nnn;
  78.          *vtmp = v;
  79.         return 0;
  80.       }
  81.       if (s->avail_in == 0) return 1;
  82.       s->bsBuff = (s->bsBuff << 8) | ((UInt32) (*((UChar*)(s->next_in))));
  83.       s->bsLive += 8;
  84.       s->next_in++;
  85.       s->avail_in--;
  86.    }
  87. }
  88.  
  89. #define GET_BITS(lll,vvv,nnn)                     \
  90.    case lll: s->state = lll;                      \
  91.     if (__mygetbits(&vvv,nnn,s)) RETURN(BZ_OK)
  92.  
  93. #define GET_UCHAR(lll,uuu)                        \
  94.    GET_BITS(lll,uuu,8)
  95.  
  96. #define GET_BIT(lll,uuu)                          \
  97.    GET_BITS(lll,uuu,1)
  98.  
  99. static int getmtf1(DState_save *sv,DState* s)
  100. {
  101.    if (sv->groupPos == 0) {
  102.       sv->groupNo++;
  103.       if (sv->groupNo >= sv->nSelectors) return 1;
  104.       sv->groupPos = BZ_G_SIZE;
  105.       sv->gSel = s->selector[sv->groupNo];
  106.       sv->gMinlen = s->minLens[sv->gSel];
  107.       sv->gLimit = &(s->limit[sv->gSel][0]);
  108.       sv->gPerm = &(s->perm[sv->gSel][0]);
  109.       sv->gBase = &(s->base[sv->gSel][0]);
  110.    }
  111.    sv->groupPos--;
  112.    sv->zn = sv->gMinlen;
  113.    return 0;
  114. }
  115.  
  116. /*---------------------------------------------------*/
  117. #define GET_MTF_VAL(label1,label2,lval)           \
  118. {                                                 \
  119.    if (getmtf1(&sv,s)) RETURN(BZ_DATA_ERROR);  \
  120.    GET_BITS(label1, zvec, zn);                    \
  121.    for (;;)  {                                    \
  122.       if (zn > 20 /* the longest code */) RETURN(BZ_DATA_ERROR);                   \
  123.       if (zvec <= gLimit[zn]) break;              \
  124.       zn++;                                       \
  125.       GET_BIT(label2, zj);                        \
  126.       zvec = (zvec << 1) | zj;                    \
  127.    };                                             \
  128.    if (zvec - gBase[zn] < 0                       \
  129.        || zvec - gBase[zn] >= BZ_MAX_ALPHA_SIZE)  \
  130.       RETURN(BZ_DATA_ERROR);                      \
  131.    lval = gPerm[zvec - gBase[zn]];                \
  132. }
  133.  
  134.  
  135. /*---------------------------------------------------*/
  136. Int32 BZ2_decompress ( DState* s )
  137. {
  138.    Int32 uc;
  139.    Int32      retVal;
  140.    Int32      minLen, maxLen;
  141.  
  142.    /* stuff that needs to be saved/restored */
  143.    DState_save sv;
  144.  
  145.    /*restore from the save area*/
  146.    sv=s->save;//mini_memcpy(&sv, &(s->save), sizeof(sv));
  147.  
  148.    #define i (sv.i)
  149.    #define j (sv.j)
  150.    #define t (sv.t)
  151.    #define alphaSize (sv.alphaSize)
  152.    #define nGroups (sv.nGroups)
  153.    #define nSelectors (sv.nSelectors)
  154.    #define EOB (sv.EOB)
  155.    #define groupNo (sv.groupNo)
  156.    #define groupPos (sv.groupPos)
  157.    #define nextSym (sv.nextSym)
  158.    #define nblockMAX (sv.nblockMAX)
  159.    #define nblock (sv.nblock)
  160.    #define es (sv.es)
  161.    #define N (sv.N)
  162.    #define curr (sv.curr)
  163.    #define zt (sv.zt)
  164.    #define zn (sv.zn)
  165.    #define zvec (sv.zvec)
  166.    #define zj (sv.zj)
  167.    #define gSel (sv.gSel)
  168.    #define gMinlen (sv.gMinlen)
  169.    #define gLimit (sv.gLimit)
  170.    #define gBase (sv.gBase)
  171.    #define gPerm (sv.gPerm)
  172.  
  173.    retVal = BZ_OK;
  174.  
  175.    switch (s->state) {
  176.  
  177.  
  178.       GET_UCHAR(BZ_X_BLKHDR_1, uc);
  179.  
  180.       if (uc == 0x17)
  181.       {
  182.         s->state = BZ_X_IDLE;
  183.         RETURN(BZ_STREAM_END);
  184.       }
  185.       if (uc != 0x31) RETURN(BZ_DATA_ERROR);
  186.  
  187.       s->origPtr = 0;
  188.       GET_UCHAR(BZ_X_ORIGPTR_1, uc);
  189.       s->origPtr = (s->origPtr << 8) | ((Int32)uc);
  190.       GET_UCHAR(BZ_X_ORIGPTR_2, uc);
  191.       s->origPtr = (s->origPtr << 8) | ((Int32)uc);
  192.       GET_UCHAR(BZ_X_ORIGPTR_3, uc);
  193.       s->origPtr = (s->origPtr << 8) | ((Int32)uc);
  194.  
  195.       if (s->origPtr < 0)
  196.          RETURN(BZ_DATA_ERROR);
  197.       if (s->origPtr > 10 + NSIS_COMPRESS_BZIP2_LEVEL*100000)
  198.          RETURN(BZ_DATA_ERROR);
  199.  
  200.       /*--- Receive the mapping table ---*/
  201.       for (i = 0; i < 16; i++) {
  202.          GET_BIT(BZ_X_MAPPING_1, uc);
  203.          if (uc == 1)
  204.             s->inUse16[i] = True; else
  205.             s->inUse16[i] = False;
  206.       }
  207.  
  208.       for (i = 0; i < 256; i++) s->inUse[i] = False;
  209.  
  210.       for (i = 0; i < 16; i++)
  211.          if (s->inUse16[i])
  212.             for (j = 0; j < 16; j++) {
  213.                GET_BIT(BZ_X_MAPPING_2, uc);
  214.                if (uc == 1) s->inUse[i * 16 + j] = True;
  215.             }
  216.       {
  217.          Int32 qi;
  218.          s->nInUse = 0;
  219.          for (qi = 0; qi < 256; qi++)
  220.             if (s->inUse[qi])
  221.                s->seqToUnseq[s->nInUse++] = qi;
  222.       }
  223.  
  224.       if (s->nInUse == 0) RETURN(BZ_DATA_ERROR);
  225.       alphaSize = s->nInUse+2;
  226.  
  227.       /*--- Now the selectors ---*/
  228.       GET_BITS(BZ_X_SELECTOR_1, nGroups, 3);
  229.       if (nGroups < 2 || nGroups > 6) RETURN(BZ_DATA_ERROR);
  230.       GET_BITS(BZ_X_SELECTOR_2, nSelectors, 15);
  231.       if (nSelectors < 1) RETURN(BZ_DATA_ERROR);
  232.       for (i = 0; i < nSelectors; i++) {
  233.          j = 0;
  234.          while (True) {
  235.             GET_BIT(BZ_X_SELECTOR_3, uc);
  236.             if (uc == 0) break;
  237.             j++;
  238.             if (j >= nGroups) RETURN(BZ_DATA_ERROR);
  239.          }
  240.          s->selectorMtf[i] = j;
  241.       }
  242.  
  243.       /*--- Undo the MTF values for the selectors. ---*/
  244.       {
  245.          UChar pos[BZ_N_GROUPS], tmp, v;
  246.          for (v = 0; v < nGroups; v++) pos[v] = v;
  247.  
  248.          for (i = 0; i < nSelectors; i++) {
  249.             v = s->selectorMtf[i];
  250.             tmp = pos[v];
  251.             while (v > 0) { pos[v] = pos[v-1]; v--; }
  252.             pos[0] = tmp;
  253.             s->selector[i] = tmp;
  254.          }
  255.       }
  256.  
  257.       /*--- Now the coding tables ---*/
  258.       for (t = 0; t < nGroups; t++) {
  259.          GET_BITS(BZ_X_CODING_1, curr, 5);
  260.          for (i = 0; i < alphaSize; i++) {
  261.             while (True) {
  262.                if (curr < 1 || curr > 20) RETURN(BZ_DATA_ERROR);
  263.                GET_BIT(BZ_X_CODING_2, uc);
  264.                if (uc == 0) break;
  265.                GET_BIT(BZ_X_CODING_3, uc);
  266.                if (uc == 0) curr++; else curr--;
  267.             }
  268.             s->len[t][i] = curr;
  269.          }
  270.       }
  271.  
  272.       /*--- Create the Huffman decoding tables ---*/
  273.       for (t = 0; t < nGroups; t++) {
  274.          minLen = 32;
  275.          maxLen = 0;
  276.          for (i = 0; i < alphaSize; i++) {
  277.             if (s->len[t][i] > maxLen) maxLen = s->len[t][i];
  278.             if (s->len[t][i] < minLen) minLen = s->len[t][i];
  279.          }
  280.          BZ2_hbCreateDecodeTables (
  281.             &(s->limit[t][0]),
  282.             &(s->base[t][0]),
  283.             &(s->perm[t][0]),
  284.             &(s->len[t][0]),
  285.             minLen, maxLen, alphaSize
  286.          );
  287.          s->minLens[t] = minLen;
  288.       }
  289.  
  290.       /*--- Now the MTF values ---*/
  291.  
  292.       EOB      = s->nInUse+1;
  293.       nblockMAX = NSIS_COMPRESS_BZIP2_LEVEL*100000;
  294.       groupNo  = -1;
  295.       groupPos = 0;
  296.  
  297.       for (i = 0; i <= 255; i++) s->unzftab[i] = 0;
  298.  
  299.       /*-- MTF init --*/
  300.       {
  301.          Int32 ii, jj, kk = MTFA_SIZE-1;
  302.          for (ii = 256 / MTFL_SIZE - 1; ii >= 0; ii--) {
  303.             for (jj = MTFL_SIZE-1; jj >= 0; jj--) {
  304.                s->mtfa[kk] = (UChar)(ii * MTFL_SIZE + jj);
  305.                kk--;
  306.             }
  307.             s->mtfbase[ii] = kk + 1;
  308.          }
  309.       }
  310.       /*-- end MTF init --*/
  311.  
  312.       nblock = 0;
  313.       GET_MTF_VAL(BZ_X_MTF_1, BZ_X_MTF_2, nextSym);
  314.  
  315.       while (True) {
  316.  
  317.          if (nextSym == EOB) break;
  318.  
  319.          if (nextSym == BZ_RUNA || nextSym == BZ_RUNB) {
  320.  
  321.             es = -1;
  322.             N = 1;
  323.             while (nextSym == BZ_RUNA || nextSym == BZ_RUNB)
  324.             {
  325.                if (nextSym == BZ_RUNA) es += N;
  326.                N = N << 1;
  327.                if (nextSym == BZ_RUNB) es += N;
  328.                GET_MTF_VAL(BZ_X_MTF_3, BZ_X_MTF_4, nextSym);
  329.             }
  330.  
  331.             es++;
  332.             uc = s->seqToUnseq[ s->mtfa[s->mtfbase[0]] ];
  333.             s->unzftab[uc] += es;
  334.  
  335. #ifdef NSIS_COMPRESS_BZIP2_SMALLMODE
  336.               while (es > 0) {
  337.                  if (nblock >= nblockMAX) RETURN(BZ_DATA_ERROR);
  338.                  s->ll16[nblock] = (UInt16)uc;
  339.                  nblock++;
  340.                  es--;
  341.               }
  342. #else
  343.              while (es > 0) {
  344.                 if (nblock >= nblockMAX) RETURN(BZ_DATA_ERROR);
  345.                 s->tt[nblock] = (UInt32)uc;
  346.                 nblock++;
  347.                 es--;
  348.              }
  349. #endif
  350.             continue;
  351.  
  352.          } else {
  353.  
  354.             if (nblock >= nblockMAX) RETURN(BZ_DATA_ERROR);
  355.  
  356.             /*-- uc = MTF ( nextSym-1 ) --*/
  357.             {
  358.                Int32 ii, jj, kk, pp, lno, off;
  359.                UInt32 nn;
  360.                nn = (UInt32)(nextSym - 1);
  361.  
  362.                if (nn < MTFL_SIZE) {
  363.                   /* avoid general-case expense */
  364.                   pp = s->mtfbase[0];
  365.                   uc = s->mtfa[pp+nn];
  366.                   /*while (nn > 3) {
  367.                      Int32 z = pp+nn;
  368.                      s->mtfa[(z)  ] = s->mtfa[(z)-1];
  369.                      s->mtfa[(z)-1] = s->mtfa[(z)-2];
  370.                      s->mtfa[(z)-2] = s->mtfa[(z)-3];
  371.                      s->mtfa[(z)-3] = s->mtfa[(z)-4];
  372.                      nn -= 4;
  373.                   }
  374.                   */
  375.                   while (nn > 0) {
  376.                      s->mtfa[(pp+nn)] = s->mtfa[(pp+nn)-1]; nn--;
  377.                   };
  378.                   s->mtfa[pp] = uc;
  379.                } else {
  380.                   /* general case */
  381.                   lno = nn / MTFL_SIZE;
  382.                   off = nn % MTFL_SIZE;
  383.                   pp = s->mtfbase[lno] + off;
  384.                   uc = s->mtfa[pp];
  385.                   while (pp > s->mtfbase[lno]) {
  386.                      s->mtfa[pp] = s->mtfa[pp-1]; pp--;
  387.                   };
  388.                   s->mtfbase[lno]++;
  389.                   while (lno > 0) {
  390.                      s->mtfbase[lno]--;
  391.                      s->mtfa[s->mtfbase[lno]]
  392.                         = s->mtfa[s->mtfbase[lno-1] + MTFL_SIZE - 1];
  393.                      lno--;
  394.                   }
  395.                   s->mtfbase[0]--;
  396.                   s->mtfa[s->mtfbase[0]] = uc;
  397.                   if (s->mtfbase[0] == 0) {
  398.                      kk = MTFA_SIZE-1;
  399.                      for (ii = 256 / MTFL_SIZE-1; ii >= 0; ii--) {
  400.                         for (jj = MTFL_SIZE-1; jj >= 0; jj--) {
  401.                            s->mtfa[kk] = s->mtfa[s->mtfbase[ii] + jj];
  402.                            kk--;
  403.                         }
  404.                         s->mtfbase[ii] = kk + 1;
  405.                      }
  406.                   }
  407.                }
  408.             }
  409.             /*-- end uc = MTF ( nextSym-1 ) --*/
  410.  
  411.             s->unzftab[s->seqToUnseq[uc]]++;
  412. #ifdef NSIS_COMPRESS_BZIP2_SMALLMODE
  413.             s->ll16[nblock] = (UInt16)(s->seqToUnseq[uc]);
  414. #else
  415.             s->tt[nblock]   = (UInt32)(s->seqToUnseq[uc]);
  416. #endif
  417.             nblock++;
  418.  
  419.             GET_MTF_VAL(BZ_X_MTF_5, BZ_X_MTF_6, nextSym);
  420.             continue;
  421.          }
  422.       }
  423.  
  424.       /* Now we know what nblock is, we can do a better sanity
  425.          check on s->origPtr.
  426.       */
  427.       if (s->origPtr < 0 || s->origPtr >= nblock)
  428.          RETURN(BZ_DATA_ERROR);
  429.  
  430.       s->state_out_len = 0;
  431.       s->state_out_ch  = 0;
  432.       s->state = BZ_X_OUTPUT;
  433.  
  434.       /*-- Set up cftab to facilitate generation of T^(-1) --*/
  435.       s->cftab[0] = 0;
  436.       for (i = 1; i <= 256; i++) s->cftab[i] = s->unzftab[i-1]+s->cftab[i-1];
  437. //      for (i = 1; i <= 256; i++) s->cftab[i] += s->cftab[i-1];
  438.  
  439. #ifdef NSIS_COMPRESS_BZIP2_SMALLMODE
  440.       {
  441.          /*-- Make a copy of cftab, used in generation of T --*/
  442.          for (i = 0; i <= 256; i++) s->cftabCopy[i] = s->cftab[i];
  443.  
  444.          /*-- compute the T vector --*/
  445.          for (i = 0; i < nblock; i++) {
  446.             uc = (UChar)(s->ll16[i]);
  447.             SET_LL(i, s->cftabCopy[uc]);
  448.             s->cftabCopy[uc]++;
  449.          }
  450.  
  451.          /*-- Compute T^(-1) by pointer reversal on T --*/
  452.          i = s->origPtr;
  453.          j = GET_LL(i);
  454.          do {
  455.             Int32 tmp = GET_LL(j);
  456.             SET_LL(j, i);
  457.             i = j;
  458.             j = tmp;
  459.          }
  460.             while (i != s->origPtr);
  461.  
  462.          s->tPos = s->origPtr;
  463.          s->nblock_used = 0;
  464.          BZ_GET_SMALL(s->k0); s->nblock_used++;
  465.       }
  466. #else//!small
  467.  
  468.          /*-- compute the T^(-1) vector --*/
  469.          for (i = 0; i < nblock; i++) {
  470.             uc = (UChar)(s->tt[i] & 0xff);
  471.             s->tt[s->cftab[uc]] |= (i << 8);
  472.             s->cftab[uc]++;
  473.          }
  474.  
  475.          s->tPos = s->tt[s->origPtr] >> 8;
  476.          s->nblock_used = 0;
  477.          BZ_GET_FAST(s->k0); s->nblock_used++;
  478. #endif
  479.       RETURN(BZ_OK);
  480.  
  481.       default: AssertH ( False, 4001 );
  482.    }
  483.  
  484.    AssertH ( False, 4002 );
  485.  
  486.    save_state_and_return:
  487.  
  488.    s->save=sv; //mini_memcpy(&(s->save), &sv, sizeof(sv));
  489.  
  490.    #undef i
  491.    #undef j
  492.    #undef t
  493.    #undef alphaSize
  494.    #undef nGroups
  495.    #undef nSelectors
  496.    #undef EOB
  497.    #undef groupNo
  498.    #undef groupPos
  499.    #undef nextSym
  500.    #undef nblockMAX
  501.    #undef nblock
  502.    #undef es
  503.    #undef N
  504.    #undef curr
  505.    #undef zt
  506.    #undef zn
  507.    #undef zvec
  508.    #undef zj
  509.    #undef gSel
  510.    #undef gMinlen
  511.    #undef gLimit
  512.    #undef gBase
  513.    #undef gPerm
  514.  
  515.    return retVal;
  516. }
  517.  
  518.  
  519. /*-------------------------------------------------------------*/
  520. /*--- end                                      decompress.c ---*/
  521. /*-------------------------------------------------------------*/
  522. #endif
  523.