| 1 |
/*-- |
| 2 |
This file is a part of bzip2 and/or libbzip2, a program and |
| 3 |
library for lossless, block-sorting data compression. |
| 4 |
|
| 5 |
Copyright (C) 1996-1999 Julian R Seward. All rights reserved. |
| 6 |
|
| 7 |
Redistribution and use in source and binary forms, with or without |
| 8 |
modification, are permitted provided that the following conditions |
| 9 |
are met: |
| 10 |
|
| 11 |
1. Redistributions of source code must retain the above copyright |
| 12 |
notice, this list of conditions and the following disclaimer. |
| 13 |
|
| 14 |
2. The origin of this software must not be misrepresented; you must |
| 15 |
not claim that you wrote the original software. If you use this |
| 16 |
software in a product, an acknowledgment in the product |
| 17 |
documentation would be appreciated but is not required. |
| 18 |
|
| 19 |
3. Altered source versions must be plainly marked as such, and must |
| 20 |
not be misrepresented as being the original software. |
| 21 |
|
| 22 |
4. The name of the author may not be used to endorse or promote |
| 23 |
products derived from this software without specific prior written |
| 24 |
permission. |
| 25 |
|
| 26 |
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
| 27 |
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 28 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 29 |
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 30 |
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 31 |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 32 |
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 33 |
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 34 |
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 35 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 36 |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 37 |
|
| 38 |
Julian Seward, Cambridge, UK. |
| 39 |
[email protected] |
| 40 |
bzip2/libbzip2 version 0.9.5 of 24 May 1999 |
| 41 |
|
| 42 |
(Now heavily hacked to be part of zgz; decompression and |
| 43 |
portability ripped out.) |
| 44 |
|
| 45 |
This program is based on (at least) the work of: |
| 46 |
Mike Burrows |
| 47 |
David Wheeler |
| 48 |
Peter Fenwick |
| 49 |
Alistair Moffat |
| 50 |
Radford Neal |
| 51 |
Ian H. Witten |
| 52 |
Robert Sedgewick |
| 53 |
Jon L. Bentley |
| 54 |
|
| 55 |
For more information on these sources, see the manual. |
| 56 |
--*/ |
| 57 |
|
| 58 |
#include <stdio.h> |
| 59 |
#include <stdlib.h> |
| 60 |
#include <string.h> |
| 61 |
#include <signal.h> |
| 62 |
#include <math.h> |
| 63 |
#include <errno.h> |
| 64 |
#include <ctype.h> |
| 65 |
#include "bzlib.h" |
| 66 |
|
| 67 |
#define ERROR_IF_EOF(i) { if ((i) == EOF) ioError(); } |
| 68 |
#define ERROR_IF_NOT_ZERO(i) { if ((i) != 0) ioError(); } |
| 69 |
#define ERROR_IF_MINUS_ONE(i) { if ((i) == (-1)) ioError(); } |
| 70 |
|
| 71 |
#include <sys/types.h> |
| 72 |
#include <utime.h> |
| 73 |
#include <unistd.h> |
| 74 |
#include <sys/stat.h> |
| 75 |
#include <sys/times.h> |
| 76 |
|
| 77 |
typedef char Char; |
| 78 |
typedef unsigned char Bool; |
| 79 |
typedef unsigned char UChar; |
| 80 |
typedef int Int32; |
| 81 |
typedef unsigned int UInt32; |
| 82 |
typedef short Int16; |
| 83 |
typedef unsigned short UInt16; |
| 84 |
|
| 85 |
#define True ((Bool)1) |
| 86 |
#define False ((Bool)0) |
| 87 |
|
| 88 |
/*-- |
| 89 |
IntNative is your platform's `native' int size. |
| 90 |
Only here to avoid probs with 64-bit platforms. |
| 91 |
--*/ |
| 92 |
typedef int IntNative; |
| 93 |
|
| 94 |
Int32 verbosity; |
| 95 |
Int32 blockSize100k; |
| 96 |
Int32 workFactor; |
| 97 |
|
| 98 |
Bool myfeof ( FILE* f ) |
| 99 |
{ |
| 100 |
Int32 c = fgetc ( f ); |
| 101 |
if (c == EOF) return True; |
| 102 |
ungetc ( c, f ); |
| 103 |
return False; |
| 104 |
} |
| 105 |
|
| 106 |
void panic (char *msg) { |
| 107 |
perror("oops"); |
| 108 |
fprintf(stderr, "%s\n", msg); |
| 109 |
exit(1); |
| 110 |
} |
| 111 |
|
| 112 |
void compressStream ( FILE *stream, FILE *zStream ) |
| 113 |
{ |
| 114 |
BZFILE* bzf = NULL; |
| 115 |
UChar ibuf[5000]; |
| 116 |
Int32 nIbuf; |
| 117 |
UInt32 nbytes_in, nbytes_out; |
| 118 |
Int32 bzerr, bzerr_dummy, ret; |
| 119 |
|
| 120 |
if (ferror(stream)) goto errhandler_io; |
| 121 |
if (ferror(zStream)) goto errhandler_io; |
| 122 |
|
| 123 |
bzf = bzWriteOpen ( &bzerr, zStream, |
| 124 |
blockSize100k, verbosity, workFactor ); |
| 125 |
if (bzerr != BZ_OK) goto errhandler; |
| 126 |
|
| 127 |
if (verbosity >= 2) fprintf ( stderr, "\n" ); |
| 128 |
|
| 129 |
while (True) { |
| 130 |
|
| 131 |
if (myfeof(stream)) break; |
| 132 |
nIbuf = fread ( ibuf, sizeof(UChar), 5000, stream ); |
| 133 |
if (ferror(stream)) goto errhandler_io; |
| 134 |
if (nIbuf > 0) bzWrite ( &bzerr, bzf, (void*)ibuf, nIbuf ); |
| 135 |
if (bzerr != BZ_OK) goto errhandler; |
| 136 |
|
| 137 |
} |
| 138 |
|
| 139 |
bzWriteClose ( &bzerr, bzf, 0, &nbytes_in, &nbytes_out ); |
| 140 |
if (bzerr != BZ_OK) goto errhandler; |
| 141 |
|
| 142 |
if (ferror(zStream)) goto errhandler_io; |
| 143 |
ret = fflush ( zStream ); |
| 144 |
if (ret == EOF) goto errhandler_io; |
| 145 |
if (zStream != stdout) { |
| 146 |
ret = fclose ( zStream ); |
| 147 |
if (ret == EOF) goto errhandler_io; |
| 148 |
} |
| 149 |
if (ferror(stream)) goto errhandler_io; |
| 150 |
ret = fclose ( stream ); |
| 151 |
if (ret == EOF) goto errhandler_io; |
| 152 |
|
| 153 |
if (nbytes_in == 0) nbytes_in = 1; |
| 154 |
|
| 155 |
return; |
| 156 |
|
| 157 |
errhandler: |
| 158 |
bzWriteClose ( &bzerr_dummy, bzf, 1, &nbytes_in, &nbytes_out ); |
| 159 |
switch (bzerr) { |
| 160 |
case BZ_MEM_ERROR: |
| 161 |
panic ( "out of memory" ); |
| 162 |
case BZ_IO_ERROR: |
| 163 |
errhandler_io: |
| 164 |
panic ( "io error" ); |
| 165 |
default: |
| 166 |
panic ( "compress:unexpected error" ); |
| 167 |
} |
| 168 |
|
| 169 |
panic ( "compress:end" ); |
| 170 |
/*notreached*/ |
| 171 |
} |
| 172 |
|
| 173 |
void old_bzip2(int level) { |
| 174 |
workFactor = 30; |
| 175 |
blockSize100k = level; |
| 176 |
|
| 177 |
compressStream(stdin, stdout); |
| 178 |
} |