📄 bzip2.c
字号:
/*-----------------------------------------------------------*//*--- A block-sorting, lossless compressor bzip2.c ---*//*-----------------------------------------------------------*//*-- This file is a part of bzip2 and/or libbzip2, a program and library for lossless, block-sorting data compression. Copyright (C) 1996-1998 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, Guildford, Surrey, UK. jseward@acm.org bzip2/libbzip2 version 0.9.0c of 18 October 1998 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.--*//*----------------------------------------------------*//*--- IMPORTANT ---*//*----------------------------------------------------*//*-- WARNING: This program and library (attempts to) compress data by performing several non-trivial transformations on it. Unless you are 100% familiar with *all* the algorithms contained herein, and with the consequences of modifying them, you should NOT meddle with the compression or decompression machinery. Incorrect changes can and very likely *will* lead to disasterous loss of data. DISCLAIMER: I TAKE NO RESPONSIBILITY FOR ANY LOSS OF DATA ARISING FROM THE USE OF THIS PROGRAM, HOWSOEVER CAUSED. Every compression of a file implies an assumption that the compressed file can be decompressed to reproduce the original. Great efforts in design, coding and testing have been made to ensure that this program works correctly. However, the complexity of the algorithms, and, in particular, the presence of various special cases in the code which occur with very low but non-zero probability make it impossible to rule out the possibility of bugs remaining in the program. DO NOT COMPRESS ANY DATA WITH THIS PROGRAM AND/OR LIBRARY UNLESS YOU ARE PREPARED TO ACCEPT THE POSSIBILITY, HOWEVER SMALL, THAT THE DATA WILL NOT BE RECOVERABLE. That is not to say this program is inherently unreliable. Indeed, I very much hope the opposite is true. bzip2/libbzip2 has been carefully constructed and extensively tested. PATENTS: To the best of my knowledge, bzip2/libbzip2 does not use any patented algorithms. However, I do not have the resources available to carry out a full patent search. Therefore I cannot give any guarantee of the above statement.--*//*----------------------------------------------------*//*--- and now for something much more pleasant :-) ---*//*----------------------------------------------------*//*---------------------------------------------*//*-- Place a 1 beside your platform, and 0 elsewhere.--*//*-- Generic 32-bit Unix. Also works on 64-bit Unix boxes.--*/#define BZ_UNIX 1/*-- Win32, as seen by Jacob Navia's excellent port of (Chris Fraser & David Hanson)'s excellent lcc compiler.--*/#define BZ_LCCWIN32 0#ifdef _WIN32#define BZ_LCCWIN32 1#define BZ_UNIX 0#endif/*---------------------------------------------*//*-- Some stuff for all platforms.--*/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <signal.h>#include <math.h>#include "bzlib.h"#define ERROR_IF_EOF(i) { if ((i) == EOF) ioError(); }#define ERROR_IF_NOT_ZERO(i) { if ((i) != 0) ioError(); }#define ERROR_IF_MINUS_ONE(i) { if ((i) == (-1)) ioError(); }/*---------------------------------------------*//*-- Platform-specific stuff.--*/#if BZ_UNIX# include <sys/types.h># include <utime.h># include <unistd.h># include <sys/stat.h># include <sys/times.h># define PATH_SEP '/'# define MY_LSTAT lstat# define MY_S_IFREG S_ISREG# define MY_STAT stat# define APPEND_FILESPEC(root, name) \ root=snocString((root), (name))# define SET_BINARY_MODE(fd) /**/# ifdef __GNUC__# define NORETURN __attribute__ ((noreturn))# else# define NORETURN /**/# endif#endif#if BZ_LCCWIN32# include <io.h># include <fcntl.h># include <sys\stat.h># define NORETURN /**/# define PATH_SEP '\\'# define MY_LSTAT _stat# define MY_STAT _stat# define MY_S_IFREG(x) ((x) & _S_IFREG)# if 0 /*-- lcc-win32 seems to expand wildcards itself --*/# define APPEND_FILESPEC(root, spec) \ do { \ if ((spec)[0] == '-') { \ root = snocString((root), (spec)); \ } else { \ struct _finddata_t c_file; \ long hFile; \ hFile = _findfirst((spec), &c_file); \ if ( hFile == -1L ) { \ root = snocString ((root), (spec)); \ } else { \ int anInt = 0; \ while ( anInt == 0 ) { \ root = snocString((root), \ &c_file.name[0]); \ anInt = _findnext(hFile, &c_file); \ } \ } \ } \ } while ( 0 )# else# define APPEND_FILESPEC(root, name) \ root = snocString ((root), (name))# endif# define SET_BINARY_MODE(fd) \ do { \ int retVal = setmode ( fileno ( fd ), \ O_BINARY ); \ ERROR_IF_MINUS_ONE ( retVal ); \ } while ( 0 )#endif/*---------------------------------------------*//*-- Some more stuff for all platforms :-)--*/typedef char Char;typedef unsigned char Bool;typedef unsigned char UChar;typedef int Int32;typedef unsigned int UInt32;typedef short Int16;typedef unsigned short UInt16; #define True ((Bool)1)#define False ((Bool)0)/*-- IntNative is your platform's `native' int size. Only here to avoid probs with 64-bit platforms.--*/typedef int IntNative;/*---------------------------------------------------*//*--- Misc (file handling) data decls ---*//*---------------------------------------------------*/Int32 verbosity;Bool keepInputFiles, smallMode;Bool forceOverwrite, testFailsExist;Int32 numFileNames, numFilesProcessed, blockSize100k;/*-- source modes; F==file, I==stdin, O==stdout --*/#define SM_I2O 1#define SM_F2O 2#define SM_F2F 3/*-- operation modes --*/#define OM_Z 1#define OM_UNZ 2#define OM_TEST 3Int32 opMode;Int32 srcMode;#define FILE_NAME_LEN 1034Int32 longestFileName;Char inName[FILE_NAME_LEN];Char outName[FILE_NAME_LEN];Char *progName;Char progNameReally[FILE_NAME_LEN];FILE *outputHandleJustInCase;Int32 workFactor;void panic ( Char* ) NORETURN;void ioError ( void ) NORETURN;void outOfMemory ( void ) NORETURN;void blockOverrun ( void ) NORETURN;void badBlockHeader ( void ) NORETURN;void badBGLengths ( void ) NORETURN;void crcError ( void ) NORETURN;void bitStreamEOF ( void ) NORETURN;void cleanUpAndFail ( Int32 ) NORETURN;void compressedStreamEOF ( void ) NORETURN;void copyFileName ( Char*, Char* );void* myMalloc ( Int32 );/*---------------------------------------------------*//*--- Processing of complete files and streams ---*//*---------------------------------------------------*//*---------------------------------------------*/Bool myfeof ( FILE* f ){ Int32 c = fgetc ( f ); if (c == EOF) return True; ungetc ( c, f ); return False;}/*---------------------------------------------*/void compressStream ( FILE *stream, FILE *zStream ){ BZFILE* bzf = NULL; UChar ibuf[5000]; Int32 nIbuf; UInt32 nbytes_in, nbytes_out; Int32 bzerr, bzerr_dummy, ret; SET_BINARY_MODE(stream); SET_BINARY_MODE(zStream); if (ferror(stream)) goto errhandler_io; if (ferror(zStream)) goto errhandler_io; bzf = bzWriteOpen ( &bzerr, zStream, blockSize100k, verbosity, workFactor ); if (bzerr != BZ_OK) goto errhandler; if (verbosity >= 2) fprintf ( stderr, "\n" ); while (True) { if (myfeof(stream)) break; nIbuf = fread ( ibuf, sizeof(UChar), 5000, stream ); if (ferror(stream)) goto errhandler_io; if (nIbuf > 0) bzWrite ( &bzerr, bzf, (void*)ibuf, nIbuf ); if (bzerr != BZ_OK) goto errhandler; } bzWriteClose ( &bzerr, bzf, 0, &nbytes_in, &nbytes_out ); if (bzerr != BZ_OK) goto errhandler; if (ferror(zStream)) goto errhandler_io; ret = fflush ( zStream ); if (ret == EOF) goto errhandler_io; if (zStream != stdout) { ret = fclose ( zStream ); if (ret == EOF) goto errhandler_io; } if (ferror(stream)) goto errhandler_io; ret = fclose ( stream ); if (ret == EOF) goto errhandler_io; if (nbytes_in == 0) nbytes_in = 1; if (verbosity >= 1) fprintf ( stderr, "%6.3f:1, %6.3f bits/byte, " "%5.2f%% saved, %d in, %d out.\n", (float)nbytes_in / (float)nbytes_out, (8.0 * (float)nbytes_out) / (float)nbytes_in, 100.0 * (1.0 - (float)nbytes_out / (float)nbytes_in), nbytes_in, nbytes_out ); return; errhandler: bzWriteClose ( &bzerr_dummy, bzf, 1, &nbytes_in, &nbytes_out ); switch (bzerr) { case BZ_MEM_ERROR: outOfMemory (); case BZ_IO_ERROR: errhandler_io: ioError(); break; default: panic ( "compress:unexpected error" ); } panic ( "compress:end" ); /*notreached*/}/*---------------------------------------------*/Bool uncompressStream ( FILE *zStream, FILE *stream ){ BZFILE* bzf = NULL; Int32 bzerr, bzerr_dummy, ret, nread, streamNo, i; UChar obuf[5000]; UChar unused[BZ_MAX_UNUSED]; Int32 nUnused; UChar* unusedTmp; nUnused = 0; streamNo = 0; SET_BINARY_MODE(stream); SET_BINARY_MODE(zStream); if (ferror(stream)) goto errhandler_io; if (ferror(zStream)) goto errhandler_io; while (True) { bzf = bzReadOpen ( &bzerr, zStream, verbosity, (int)smallMode, unused, nUnused ); if (bzf == NULL || bzerr != BZ_OK) goto errhandler; streamNo++; while (bzerr == BZ_OK) { nread = bzRead ( &bzerr, bzf, obuf, 5000 ); if (bzerr == BZ_DATA_ERROR_MAGIC) goto errhandler; if ((bzerr == BZ_OK || bzerr == BZ_STREAM_END) && nread > 0) fwrite ( obuf, sizeof(UChar), nread, stream ); if (ferror(stream)) goto errhandler_io; } if (bzerr != BZ_STREAM_END) goto errhandler; bzReadGetUnused ( &bzerr, bzf, (void**)(&unusedTmp), &nUnused ); if (bzerr != BZ_OK) panic ( "decompress:bzReadGetUnused" ); for (i = 0; i < nUnused; i++) unused[i] = unusedTmp[i]; bzReadClose ( &bzerr, bzf ); if (bzerr != BZ_OK) panic ( "decompress:bzReadGetUnused" ); if (nUnused == 0 && myfeof(zStream)) break; } if (ferror(zStream)) goto errhandler_io; ret = fclose ( zStream ); if (ret == EOF) goto errhandler_io; if (ferror(stream)) goto errhandler_io; ret = fflush ( stream ); if (ret != 0) goto errhandler_io; if (stream != stdout) { ret = fclose ( stream ); if (ret == EOF) goto errhandler_io; } if (verbosity >= 2) fprintf ( stderr, "\n " ); return True; errhandler: bzReadClose ( &bzerr_dummy, bzf ); switch (bzerr) { case BZ_IO_ERROR: errhandler_io: ioError(); break; case BZ_DATA_ERROR: crcError(); case BZ_MEM_ERROR: outOfMemory(); case BZ_UNEXPECTED_EOF: compressedStreamEOF(); case BZ_DATA_ERROR_MAGIC: if (streamNo == 1) { return False; } else { fprintf ( stderr, "\n%s: %s: trailing garbage after EOF ignored\n", progName, inName ); return True; } default: panic ( "decompress:unexpected error" ); } panic ( "decompress:end" ); return True; /*notreached*/}/*---------------------------------------------*/Bool testStream ( FILE *zStream ){ BZFILE* bzf = NULL; Int32 bzerr, bzerr_dummy, ret, nread, streamNo, i; UChar obuf[5000]; UChar unused[BZ_MAX_UNUSED]; Int32 nUnused; UChar* unusedTmp; nUnused = 0; streamNo = 0; SET_BINARY_MODE(zStream); if (ferror(zStream)) goto errhandler_io; while (True) { bzf = bzReadOpen ( &bzerr, zStream, verbosity, (int)smallMode, unused, nUnused ); if (bzf == NULL || bzerr != BZ_OK) goto errhandler; streamNo++;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -