⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 decompress.c

📁 minibzip2
💻 C
📖 第 1 页 / 共 2 页
字号:
/*-------------------------------------------------------------*//*--- Decompression machinery                               ---*//*---                                          decompress.c ---*//*-------------------------------------------------------------*//*--  This file is a part of bzip2 and/or libbzip2, a program and  library for lossless, block-sorting data compression.  Copyright (C) 1996-2002 Julian R Seward.  All rights reserved.  Redistribution and use in source and binary forms, with or without  modification, are permitted provided that the following conditions  are met:  1. Redistributions of source code must retain the above copyright     notice, this list of conditions and the following disclaimer.  2. The origin of this software must not be misrepresented; you must      not claim that you wrote the original software.  If you use this      software in a product, an acknowledgment in the product      documentation would be appreciated but is not required.  3. Altered source versions must be plainly marked as such, and must     not be misrepresented as being the original software.  4. The name of the author may not be used to endorse or promote      products derived from this software without specific prior written      permission.  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  Julian Seward, Cambridge, UK.  jseward@acm.org  bzip2/libbzip2 version 1.0 of 21 March 2000  This program is based on (at least) the work of:     Mike Burrows     David Wheeler     Peter Fenwick     Alistair Moffat     Radford Neal     Ian H. Witten     Robert Sedgewick     Jon L. Bentley  For more information on these sources, see the manual.--*/#include "bzlib_private.h"/*---------------------------------------------------*/staticvoid makeMaps_d ( DState* s ){   Int32 i;   s->nInUse = 0;   for (i = 0; i < 256; i++)      if (s->inUse[i]) {         s->seqToUnseq[s->nInUse] = i;         s->nInUse++;      }}/*---------------------------------------------------*/#define RETURN(rrr)                               \   { retVal = rrr; goto save_state_and_return; };#define GET_BITS(lll,vvv,nnn)                     \   case lll: s->state = lll;                      \   while (True) {                                 \      if (s->bsLive >= nnn) {                     \         UInt32 v;                                \         v = (s->bsBuff >>                        \             (s->bsLive-nnn)) & ((1 << nnn)-1);   \         s->bsLive -= nnn;                        \         vvv = v;                                 \         break;                                   \      }                                           \      if (s->strm->avail_in == 0) RETURN(BZ_OK);  \      s->bsBuff                                   \         = (s->bsBuff << 8) |                     \           ((UInt32)                              \              (*((UChar*)(s->strm->next_in))));   \      s->bsLive += 8;                             \      s->strm->next_in++;                         \      s->strm->avail_in--;                        \      s->strm->total_in_lo32++;                   \      if (s->strm->total_in_lo32 == 0)            \         s->strm->total_in_hi32++;                \   }#define GET_UCHAR(lll,uuu)                        \   GET_BITS(lll,uuu,8)#define GET_BIT(lll,uuu)                          \   GET_BITS(lll,uuu,1)/*---------------------------------------------------*/#define GET_MTF_VAL(label1,label2,lval)           \{                                                 \   if (groupPos == 0) {                           \      groupNo++;                                  \      if (groupNo >= nSelectors)                  \         RETURN(BZ_DATA_ERROR);                   \      groupPos = BZ_G_SIZE;                       \      gSel = s->selector[groupNo];                \      gMinlen = s->minLens[gSel];                 \      gLimit = &(s->limit[gSel][0]);              \      gPerm = &(s->perm[gSel][0]);                \      gBase = &(s->base[gSel][0]);                \   }                                              \   groupPos--;                                    \   zn = gMinlen;                                  \   GET_BITS(label1, zvec, zn);                    \   while (1) {                                    \      if (zn > 20 /* the longest code */)         \         RETURN(BZ_DATA_ERROR);                   \      if (zvec <= gLimit[zn]) break;              \      zn++;                                       \      GET_BIT(label2, zj);                        \      zvec = (zvec << 1) | zj;                    \   };                                             \   if (zvec - gBase[zn] < 0                       \       || zvec - gBase[zn] >= BZ_MAX_ALPHA_SIZE)  \      RETURN(BZ_DATA_ERROR);                      \   lval = gPerm[zvec - gBase[zn]];                \}/*---------------------------------------------------*/Int32 BZ2_decompress ( DState* s ){   UChar      uc;   Int32      retVal;   Int32      minLen, maxLen;   bz_stream* strm = s->strm;   /* stuff that needs to be saved/restored */   Int32  i;   Int32  j;   Int32  t;   Int32  alphaSize;   Int32  nGroups;   Int32  nSelectors;   Int32  EOB;   Int32  groupNo;   Int32  groupPos;   Int32  nextSym;   Int32  nblockMAX;   Int32  nblock;   Int32  es;   Int32  N;   Int32  curr;   Int32  zt;   Int32  zn;    Int32  zvec;   Int32  zj;   Int32  gSel;   Int32  gMinlen;   Int32* gLimit;   Int32* gBase;   Int32* gPerm;   if (s->state == BZ_X_MAGIC_1) {      /*initialise the save area*/      s->save_i           = 0;      s->save_j           = 0;      s->save_t           = 0;      s->save_alphaSize   = 0;      s->save_nGroups     = 0;      s->save_nSelectors  = 0;      s->save_EOB         = 0;      s->save_groupNo     = 0;      s->save_groupPos    = 0;      s->save_nextSym     = 0;      s->save_nblockMAX   = 0;      s->save_nblock      = 0;      s->save_es          = 0;      s->save_N           = 0;      s->save_curr        = 0;      s->save_zt          = 0;      s->save_zn          = 0;      s->save_zvec        = 0;      s->save_zj          = 0;      s->save_gSel        = 0;      s->save_gMinlen     = 0;      s->save_gLimit      = NULL;      s->save_gBase       = NULL;      s->save_gPerm       = NULL;   }   /*restore from the save area*/   i           = s->save_i;   j           = s->save_j;   t           = s->save_t;   alphaSize   = s->save_alphaSize;   nGroups     = s->save_nGroups;   nSelectors  = s->save_nSelectors;   EOB         = s->save_EOB;   groupNo     = s->save_groupNo;   groupPos    = s->save_groupPos;   nextSym     = s->save_nextSym;   nblockMAX   = s->save_nblockMAX;   nblock      = s->save_nblock;   es          = s->save_es;   N           = s->save_N;   curr        = s->save_curr;   zt          = s->save_zt;   zn          = s->save_zn;    zvec        = s->save_zvec;   zj          = s->save_zj;   gSel        = s->save_gSel;   gMinlen     = s->save_gMinlen;   gLimit      = s->save_gLimit;   gBase       = s->save_gBase;   gPerm       = s->save_gPerm;   retVal = BZ_OK;   switch (s->state) {      GET_UCHAR(BZ_X_MAGIC_1, uc);      if (uc != BZ_HDR_B) RETURN(BZ_DATA_ERROR_MAGIC);      GET_UCHAR(BZ_X_MAGIC_2, uc);      if (uc != BZ_HDR_Z) RETURN(BZ_DATA_ERROR_MAGIC);      GET_UCHAR(BZ_X_MAGIC_3, uc)      if (uc != BZ_HDR_h) RETURN(BZ_DATA_ERROR_MAGIC);      GET_BITS(BZ_X_MAGIC_4, s->blockSize100k, 8)      if (s->blockSize100k < (BZ_HDR_0 + 1) ||           s->blockSize100k > (BZ_HDR_0 + 9)) RETURN(BZ_DATA_ERROR_MAGIC);      s->blockSize100k -= BZ_HDR_0;      if (s->smallDecompress) {         s->ll16 = BZALLOC( s->blockSize100k * 100000 * sizeof(UInt16) );         s->ll4  = BZALLOC(                       ((1 + s->blockSize100k * 100000) >> 1) * sizeof(UChar)                    );         if (s->ll16 == NULL || s->ll4 == NULL) RETURN(BZ_MEM_ERROR);      } else {         s->tt  = BZALLOC( s->blockSize100k * 100000 * sizeof(Int32) );         if (s->tt == NULL) RETURN(BZ_MEM_ERROR);      }      GET_UCHAR(BZ_X_BLKHDR_1, uc);      if (uc == 0x17) goto endhdr_2;      if (uc != 0x31) RETURN(BZ_DATA_ERROR);      GET_UCHAR(BZ_X_BLKHDR_2, uc);      if (uc != 0x41) RETURN(BZ_DATA_ERROR);      GET_UCHAR(BZ_X_BLKHDR_3, uc);      if (uc != 0x59) RETURN(BZ_DATA_ERROR);      GET_UCHAR(BZ_X_BLKHDR_4, uc);      if (uc != 0x26) RETURN(BZ_DATA_ERROR);      GET_UCHAR(BZ_X_BLKHDR_5, uc);      if (uc != 0x53) RETURN(BZ_DATA_ERROR);      GET_UCHAR(BZ_X_BLKHDR_6, uc);      if (uc != 0x59) RETURN(BZ_DATA_ERROR);      s->currBlockNo++;      if (s->verbosity >= 2)         VPrintf1 ( "\n    [%d: huff+mtf ", s->currBlockNo );       s->storedBlockCRC = 0;      GET_UCHAR(BZ_X_BCRC_1, uc);      s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc);      GET_UCHAR(BZ_X_BCRC_2, uc);      s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc);      GET_UCHAR(BZ_X_BCRC_3, uc);      s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc);      GET_UCHAR(BZ_X_BCRC_4, uc);      s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc);      GET_BITS(BZ_X_RANDBIT, s->blockRandomised, 1);      s->origPtr = 0;      GET_UCHAR(BZ_X_ORIGPTR_1, uc);      s->origPtr = (s->origPtr << 8) | ((Int32)uc);      GET_UCHAR(BZ_X_ORIGPTR_2, uc);      s->origPtr = (s->origPtr << 8) | ((Int32)uc);      GET_UCHAR(BZ_X_ORIGPTR_3, uc);      s->origPtr = (s->origPtr << 8) | ((Int32)uc);      if (s->origPtr < 0)         RETURN(BZ_DATA_ERROR);      if (s->origPtr > 10 + 100000*s->blockSize100k)          RETURN(BZ_DATA_ERROR);      /*--- Receive the mapping table ---*/      for (i = 0; i < 16; i++) {         GET_BIT(BZ_X_MAPPING_1, uc);         if (uc == 1)             s->inUse16[i] = True; else             s->inUse16[i] = False;      }      for (i = 0; i < 256; i++) s->inUse[i] = False;      for (i = 0; i < 16; i++)         if (s->inUse16[i])            for (j = 0; j < 16; j++) {               GET_BIT(BZ_X_MAPPING_2, uc);               if (uc == 1) s->inUse[i * 16 + j] = True;            }      makeMaps_d ( s );      if (s->nInUse == 0) RETURN(BZ_DATA_ERROR);      alphaSize = s->nInUse+2;      /*--- Now the selectors ---*/      GET_BITS(BZ_X_SELECTOR_1, nGroups, 3);      if (nGroups < 2 || nGroups > 6) RETURN(BZ_DATA_ERROR);      GET_BITS(BZ_X_SELECTOR_2, nSelectors, 15);      if (nSelectors < 1) RETURN(BZ_DATA_ERROR);      for (i = 0; i < nSelectors; i++) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -