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

📄 bzip2recover.c

📁 minibzip2
💻 C
📖 第 1 页 / 共 2 页
字号:
/*-----------------------------------------------------------*//*--- Block recoverer program for bzip2                   ---*//*---                                      bzip2recover.c ---*//*-----------------------------------------------------------*//*--  This program is bzip2recover, a program to attempt data   salvage from damaged files created by the accompanying  bzip2-1.0 program.  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 a complete hack and should be rewritten  properly.  It isn't very complicated.--*/#include <stdio.h>#include <errno.h>#include <stdlib.h>#include <string.h>/* This program records bit locations in the file to be recovered.   That means that if 64-bit ints are not supported, we will not   be able to recover .bz2 files over 512MB (2^32 bits) long.   On GNU supported platforms, we take advantage of the 64-bit   int support to circumvent this problem.  Ditto MSVC.   This change occurred in version 1.0.2; all prior versions have   the 512MB limitation.*/#ifdef __GNUC__   typedef  unsigned long long int  MaybeUInt64;#  define MaybeUInt64_FMT "%Lu"#else#ifdef _MSC_VER   typedef  unsigned __int64  MaybeUInt64;#  define MaybeUInt64_FMT "%I64u"#else   typedef  unsigned int   MaybeUInt64;#  define MaybeUInt64_FMT "%u"#endif#endiftypedef  unsigned int   UInt32;typedef  int            Int32;typedef  unsigned char  UChar;typedef  char           Char;typedef  unsigned char  Bool;#define True    ((Bool)1)#define False   ((Bool)0)#define BZ_MAX_FILENAME 2000Char inFileName[BZ_MAX_FILENAME];Char outFileName[BZ_MAX_FILENAME];Char progName[BZ_MAX_FILENAME];MaybeUInt64 bytesOut = 0;MaybeUInt64 bytesIn  = 0;/*---------------------------------------------------*//*--- Header bytes                                ---*//*---------------------------------------------------*/#define BZ_HDR_B 0x42                         /* 'B' */#define BZ_HDR_Z 0x5a                         /* 'Z' */#define BZ_HDR_h 0x68                         /* 'h' */#define BZ_HDR_0 0x30                         /* '0' */ /*---------------------------------------------------*//*--- I/O errors                                  ---*//*---------------------------------------------------*//*---------------------------------------------*/void readError ( void ){   fprintf ( stderr,             "%s: I/O error reading `%s', possible reason follows.\n",            progName, inFileName );   perror ( progName );   fprintf ( stderr, "%s: warning: output file(s) may be incomplete.\n",             progName );   exit ( 1 );}/*---------------------------------------------*/void writeError ( void ){   fprintf ( stderr,             "%s: I/O error reading `%s', possible reason follows.\n",            progName, inFileName );   perror ( progName );   fprintf ( stderr, "%s: warning: output file(s) may be incomplete.\n",             progName );   exit ( 1 );}/*---------------------------------------------*/void mallocFail ( Int32 n ){   fprintf ( stderr,             "%s: malloc failed on request for %d bytes.\n",            progName, n );   fprintf ( stderr, "%s: warning: output file(s) may be incomplete.\n",             progName );   exit ( 1 );}/*---------------------------------------------*/void tooManyBlocks ( Int32 max_handled_blocks ){   fprintf ( stderr,             "%s: `%s' appears to contain more than %d blocks\n",            progName, inFileName, max_handled_blocks );   fprintf ( stderr,             "%s: and cannot be handled.  To fix, increase\n",             progName );   fprintf ( stderr,              "%s: BZ_MAX_HANDLED_BLOCKS in bzip2recover.c, and recompile.\n",             progName );   exit ( 1 );}/*---------------------------------------------------*//*--- Bit stream I/O                              ---*//*---------------------------------------------------*/typedef   struct {      FILE*  handle;      Int32  buffer;      Int32  buffLive;      Char   mode;   }   BitStream;/*---------------------------------------------*/BitStream* bsOpenReadStream ( FILE* stream ){   BitStream *bs = malloc ( sizeof(BitStream) );   if (bs == NULL) mallocFail ( sizeof(BitStream) );   bs->handle = stream;   bs->buffer = 0;   bs->buffLive = 0;   bs->mode = 'r';   return bs;}/*---------------------------------------------*/BitStream* bsOpenWriteStream ( FILE* stream ){   BitStream *bs = malloc ( sizeof(BitStream) );   if (bs == NULL) mallocFail ( sizeof(BitStream) );   bs->handle = stream;   bs->buffer = 0;   bs->buffLive = 0;   bs->mode = 'w';   return bs;}/*---------------------------------------------*/void bsPutBit ( BitStream* bs, Int32 bit ){   if (bs->buffLive == 8) {      Int32 retVal = putc ( (UChar) bs->buffer, bs->handle );      if (retVal == EOF) writeError();      bytesOut++;      bs->buffLive = 1;      bs->buffer = bit & 0x1;   } else {      bs->buffer = ( (bs->buffer << 1) | (bit & 0x1) );      bs->buffLive++;   };}/*---------------------------------------------*//*--   Returns 0 or 1, or 2 to indicate EOF.--*/Int32 bsGetBit ( BitStream* bs ){   if (bs->buffLive > 0) {      bs->buffLive --;      return ( ((bs->buffer) >> (bs->buffLive)) & 0x1 );   } else {      Int32 retVal = getc ( bs->handle );      if ( retVal == EOF ) {         if (errno != 0) readError();         return 2;      }      bs->buffLive = 7;      bs->buffer = retVal;      return ( ((bs->buffer) >> 7) & 0x1 );   }}/*---------------------------------------------*/void bsClose ( BitStream* bs ){   Int32 retVal;   if ( bs->mode == 'w' ) {      while ( bs->buffLive < 8 ) {         bs->buffLive++;         bs->buffer <<= 1;      };      retVal = putc ( (UChar) (bs->buffer), bs->handle );      if (retVal == EOF) writeError();      bytesOut++;      retVal = fflush ( bs->handle );      if (retVal == EOF) writeError();   }   retVal = fclose ( bs->handle );   if (retVal == EOF) {      if (bs->mode == 'w') writeError(); else readError();   }   free ( bs );}/*---------------------------------------------*/void bsPutUChar ( BitStream* bs, UChar c )

⌨️ 快捷键说明

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