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

📄 bzlib.c

📁 高效率的一种通用压缩/解压程序
💻 C
📖 第 1 页 / 共 3 页
字号:
   if ( !abandon && !ferror ( bzf->handle ) ) {      fflush ( bzf->handle );      if (ferror(bzf->handle))         { BZ_SETERR(BZ_IO_ERROR); return; };   }   if (nbytes_in != NULL) *nbytes_in = bzf->strm.total_in;   if (nbytes_out != NULL) *nbytes_out = bzf->strm.total_out;   BZ_SETERR(BZ_OK);   bzCompressEnd ( &(bzf->strm) );   free ( bzf );}/*---------------------------------------------------*/BZFILE* BZ_API(bzReadOpen)                    ( int*  bzerror,                      FILE* f,                      int   verbosity,                     int   small,                     void* unused,                     int   nUnused ){   bzFile* bzf = NULL;   int     ret;   BZ_SETERR(BZ_OK);   if (f == NULL ||        (small != 0 && small != 1) ||       (verbosity < 0 || verbosity > 4) ||       (unused == NULL && nUnused != 0) ||       (unused != NULL && (nUnused < 0 || nUnused > BZ_MAX_UNUSED)))      { BZ_SETERR(BZ_PARAM_ERROR); return NULL; };   if (ferror(f))      { BZ_SETERR(BZ_IO_ERROR); return NULL; };   bzf = malloc ( sizeof(bzFile) );   if (bzf == NULL)       { BZ_SETERR(BZ_MEM_ERROR); return NULL; };   BZ_SETERR(BZ_OK);   bzf->initialisedOk = False;   bzf->handle        = f;   bzf->bufN          = 0;   bzf->writing       = False;   bzf->strm.bzalloc  = NULL;   bzf->strm.bzfree   = NULL;   bzf->strm.opaque   = NULL;      while (nUnused > 0) {      bzf->buf[bzf->bufN] = *((UChar*)(unused)); bzf->bufN++;      unused = ((void*)( 1 + ((UChar*)(unused))  ));      nUnused--;   }   ret = bzDecompressInit ( &(bzf->strm), verbosity, small );   if (ret != BZ_OK)      { BZ_SETERR(ret); free(bzf); return NULL; };   bzf->strm.avail_in = bzf->bufN;   bzf->strm.next_in  = bzf->buf;   bzf->initialisedOk = True;   return bzf;   }/*---------------------------------------------------*/void BZ_API(bzReadClose) ( int *bzerror, BZFILE *b ){   bzFile* bzf = (bzFile*)b;   BZ_SETERR(BZ_OK);   if (bzf == NULL)      { BZ_SETERR(BZ_OK); return; };   if (bzf->writing)      { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };   if (bzf->initialisedOk)      (void)bzDecompressEnd ( &(bzf->strm) );   free ( bzf );}/*---------------------------------------------------*/int BZ_API(bzRead)            ( int*    bzerror,              BZFILE* b,              void*   buf,              int     len ){   Int32   n, ret;   bzFile* bzf = (bzFile*)b;   BZ_SETERR(BZ_OK);   if (bzf == NULL || buf == NULL || len < 0)      { BZ_SETERR(BZ_PARAM_ERROR); return 0; };   if (bzf->writing)      { BZ_SETERR(BZ_SEQUENCE_ERROR); return 0; };   if (len == 0)      { BZ_SETERR(BZ_OK); return 0; };   bzf->strm.avail_out = len;   bzf->strm.next_out = buf;   while (True) {      if (ferror(bzf->handle))          { BZ_SETERR(BZ_IO_ERROR); return 0; };      if (bzf->strm.avail_in == 0 && !myfeof(bzf->handle)) {         n = fread ( bzf->buf, sizeof(UChar),                      BZ_MAX_UNUSED, bzf->handle );         if (ferror(bzf->handle))            { BZ_SETERR(BZ_IO_ERROR); return 0; };         bzf->bufN = n;         bzf->strm.avail_in = bzf->bufN;         bzf->strm.next_in = bzf->buf;      }      ret = bzDecompress ( &(bzf->strm) );      if (ret != BZ_OK && ret != BZ_STREAM_END)         { BZ_SETERR(ret); return 0; };      if (ret == BZ_OK && myfeof(bzf->handle) &&           bzf->strm.avail_in == 0 && bzf->strm.avail_out > 0)         { BZ_SETERR(BZ_UNEXPECTED_EOF); return 0; };      if (ret == BZ_STREAM_END)         { BZ_SETERR(BZ_STREAM_END);           return len - bzf->strm.avail_out; };      if (bzf->strm.avail_out == 0)         { BZ_SETERR(BZ_OK); return len; };         }   return 0; /*not reached*/}/*---------------------------------------------------*/void BZ_API(bzReadGetUnused)                      ( int*    bzerror,                        BZFILE* b,                        void**  unused,                        int*    nUnused ){   bzFile* bzf = (bzFile*)b;   if (bzf == NULL)      { BZ_SETERR(BZ_PARAM_ERROR); return; };   if (bzf->lastErr != BZ_STREAM_END)      { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };   if (unused == NULL || nUnused == NULL)      { BZ_SETERR(BZ_PARAM_ERROR); return; };   BZ_SETERR(BZ_OK);   *nUnused = bzf->strm.avail_in;   *unused = bzf->strm.next_in;}#endif/*---------------------------------------------------*//*--- Misc convenience stuff                      ---*//*---------------------------------------------------*//*---------------------------------------------------*/int BZ_API(bzBuffToBuffCompress)                          ( char*         dest,                            unsigned int* destLen,                           char*         source,                            unsigned int  sourceLen,                           int           blockSize100k,                            int           verbosity,                            int           workFactor ){   bz_stream strm;   int ret;   if (dest == NULL || destLen == NULL ||        source == NULL ||       blockSize100k < 1 || blockSize100k > 9 ||       verbosity < 0 || verbosity > 4 ||       workFactor < 0 || workFactor > 250)       return BZ_PARAM_ERROR;   if (workFactor == 0) workFactor = 30;   strm.bzalloc = NULL;   strm.bzfree = NULL;   strm.opaque = NULL;   ret = bzCompressInit ( &strm, blockSize100k,                           verbosity, workFactor );   if (ret != BZ_OK) return ret;   strm.next_in = source;   strm.next_out = dest;   strm.avail_in = sourceLen;   strm.avail_out = *destLen;   ret = bzCompress ( &strm, BZ_FINISH );   if (ret == BZ_FINISH_OK) goto output_overflow;   if (ret != BZ_STREAM_END) goto errhandler;   /* normal termination */   *destLen -= strm.avail_out;      bzCompressEnd ( &strm );   return BZ_OK;   output_overflow:   bzCompressEnd ( &strm );   return BZ_OUTBUFF_FULL;   errhandler:   bzCompressEnd ( &strm );   return ret;}/*---------------------------------------------------*/int BZ_API(bzBuffToBuffDecompress)                            ( char*         dest,                              unsigned int* destLen,                             char*         source,                              unsigned int  sourceLen,                             int           small,                             int           verbosity ){   bz_stream strm;   int ret;   if (dest == NULL || destLen == NULL ||        source == NULL ||       (small != 0 && small != 1) ||       verbosity < 0 || verbosity > 4)           return BZ_PARAM_ERROR;   strm.bzalloc = NULL;   strm.bzfree = NULL;   strm.opaque = NULL;   ret = bzDecompressInit ( &strm, verbosity, small );   if (ret != BZ_OK) return ret;   strm.next_in = source;   strm.next_out = dest;   strm.avail_in = sourceLen;   strm.avail_out = *destLen;   ret = bzDecompress ( &strm );   if (ret == BZ_OK) goto output_overflow_or_eof;   if (ret != BZ_STREAM_END) goto errhandler;   /* normal termination */   *destLen -= strm.avail_out;   bzDecompressEnd ( &strm );   return BZ_OK;   output_overflow_or_eof:   if (strm.avail_out > 0) {      bzDecompressEnd ( &strm );      return BZ_UNEXPECTED_EOF;   } else {      bzDecompressEnd ( &strm );      return BZ_OUTBUFF_FULL;   };         errhandler:   bzDecompressEnd ( &strm );   return BZ_SEQUENCE_ERROR;}/*---------------------------------------------------*//*--   Code contributed by Yoshioka Tsuneo   (QWF00133@niftyserve.or.jp/tsuneo-y@is.aist-nara.ac.jp),   to support better zlib compatibility.   This code is not _officially_ part of libbzip2 (yet);   I haven't tested it, documented it, or considered the   threading-safeness of it.   If this code breaks, please contact both Yoshioka and me.--*//*---------------------------------------------------*//*---------------------------------------------------*//*--   return version like "0.9.0c".--*/const char * BZ_API(bzlibVersion)(void){   return BZ_VERSION;}#ifndef BZ_NO_STDIO/*---------------------------------------------------*/#if defined(_WIN32) || defined(OS2) || defined(MSDOS)#   include <fcntl.h>#   include <io.h>#   define SET_BINARY_MODE(file) setmode(fileno(file),O_BINARY)#else#   define SET_BINARY_MODE(file)#endifstaticBZFILE * bzopen_or_bzdopen               ( const char *path,   /* no use when bzdopen */                 int fd,             /* no use when bzdopen */                 const char *mode,                 int open_mode)      /* bzopen: 0, bzdopen:1 */{   int    bzerr;   char   unused[BZ_MAX_UNUSED];   int    blockSize100k = 9;   int    writing       = 0;   char   mode2[10]     = "";   FILE   *fp           = NULL;   BZFILE *bzfp         = NULL;   int    verbosity     = 0;   int    workFactor    = 30;   int    smallMode     = 0;   int    nUnused       = 0;    if(mode==NULL){return NULL;}   while(*mode){      switch(*mode){      case 'r':         writing = 0;break;      case 'w':         writing = 1;break;      case 's':         smallMode = 1;break;      default:         if(isdigit(*mode)){            blockSize100k = 0;            while(isdigit(*mode)){               blockSize100k = blockSize100k*10 + *mode-'0';               mode++;            }         }else{            /* ignore */         }      }      mode++;   }   strcat(mode2, writing ? "w" : "r" );   strcat(mode2,"b");   /* binary mode */   if(open_mode==0){      if(path==NULL || strcmp(path,"")==0){        fp = (writing ? stdout : stdin);        SET_BINARY_MODE(fp);      }else{        fp = fopen(path,mode2);      }   }else{#ifdef BZ_STRICT_ANSI      fp = NULL;#else      fp = fdopen(fd,mode2);#endif   }   if(fp==NULL){return NULL;}   if(writing){      bzfp = bzWriteOpen(&bzerr,fp,blockSize100k,verbosity,workFactor);   }else{      bzfp = bzReadOpen(&bzerr,fp,verbosity,smallMode,unused,nUnused);   }   if(bzfp==NULL){      if(fp!=stdin && fp!=stdout) fclose(fp);      return NULL;   }   return bzfp;}/*---------------------------------------------------*//*--   open file for read or write.      ex) bzopen("file","w9")      case path="" or NULL => use stdin or stdout.--*/BZFILE * BZ_API(bzopen)               ( const char *path,                 const char *mode ){   return bzopen_or_bzdopen(path,-1,mode,/*bzopen*/0);}/*---------------------------------------------------*/BZFILE * BZ_API(bzdopen)               ( int fd,                 const char *mode ){   return bzopen_or_bzdopen(NULL,fd,mode,/*bzdopen*/1);}/*---------------------------------------------------*/int BZ_API(bzread) (BZFILE* b, void* buf, int len ){   int bzerr, nread;   if (((bzFile*)b)->lastErr == BZ_STREAM_END) return 0;   nread = bzRead(&bzerr,b,buf,len);   if (bzerr == BZ_OK || bzerr == BZ_STREAM_END) {      return nread;   } else {      return -1;   }}/*---------------------------------------------------*/int BZ_API(bzwrite) (BZFILE* b, void* buf, int len ){   int bzerr;   bzWrite(&bzerr,b,buf,len);   if(bzerr == BZ_OK){      return len;   }else{      return -1;   }}/*---------------------------------------------------*/int BZ_API(bzflush) (BZFILE *b){   /* do nothing now... */   return 0;}/*---------------------------------------------------*/void BZ_API(bzclose) (BZFILE* b){   int bzerr;   FILE *fp = ((bzFile *)b)->handle;      if(b==NULL){return;}   if(((bzFile*)b)->writing){      bzWriteClose(&bzerr,b,0,NULL,NULL);      if(bzerr != BZ_OK){         bzWriteClose(NULL,b,1,NULL,NULL);      }   }else{      bzReadClose(&bzerr,b);   }   if(fp!=stdin && fp!=stdout){      fclose(fp);   }}/*---------------------------------------------------*//*--   return last error code --*/static char *bzerrorstrings[] = {       "OK"      ,"SEQUENCE_ERROR"      ,"PARAM_ERROR"      ,"MEM_ERROR"      ,"DATA_ERROR"      ,"DATA_ERROR_MAGIC"      ,"IO_ERROR"      ,"UNEXPECTED_EOF"      ,"OUTBUFF_FULL"      ,"???"   /* for future */      ,"???"   /* for future */      ,"???"   /* for future */      ,"???"   /* for future */      ,"???"   /* for future */      ,"???"   /* for future */};const char * BZ_API(bzerror) (BZFILE *b, int *errnum){   int err = ((bzFile *)b)->lastErr;   if(err>0) err = 0;   *errnum = err;   return bzerrorstrings[err*-1];}#endif/*-------------------------------------------------------------*//*--- end                                           bzlib.c ---*//*-------------------------------------------------------------*/

⌨️ 快捷键说明

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