This repository has been archived by the owner on May 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
stream.c
297 lines (247 loc) · 8.44 KB
/
stream.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include "bzlib_private.h"
/* ------------------------------------------------------------------
This file is part of bzip2/libbzip2, a program and library for
lossless, block-sorting data compression.
bzip2/libbzip2 version 1.0.6 of 6 September 2010
Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
Please read the WARNING, DISCLAIMER and PATENTS sections in the
README file.
This program is released under the terms of the license contained
in the file LICENSE.
------------------------------------------------------------------ */
/*---------------------------------------------------*/
/*--- An implementation of 64-bit ints. Sigh. ---*/
/*--- Roll on widespread deployment of ANSI C9X ! ---*/
/*---------------------------------------------------*/
void uInt64_from_UInt32s ( UInt64* n, UInt32 lo32, UInt32 hi32 )
{
n->b[7] = (UChar)((hi32 >> 24) & 0xFF);
n->b[6] = (UChar)((hi32 >> 16) & 0xFF);
n->b[5] = (UChar)((hi32 >> 8) & 0xFF);
n->b[4] = (UChar) (hi32 & 0xFF);
n->b[3] = (UChar)((lo32 >> 24) & 0xFF);
n->b[2] = (UChar)((lo32 >> 16) & 0xFF);
n->b[1] = (UChar)((lo32 >> 8) & 0xFF);
n->b[0] = (UChar) (lo32 & 0xFF);
}
double uInt64_to_double ( UInt64* n )
{
Int32 i;
double base = 1.0;
double sum = 0.0;
for (i = 0; i < 8; i++) {
sum += base * (double)(n->b[i]);
base *= 256.0;
}
return sum;
}
Bool uInt64_isZero ( UInt64* n )
{
Int32 i;
for (i = 0; i < 8; i++)
if (n->b[i] != 0) return 0;
return 1;
}
/* Divide *n by 10, and return the remainder. */
Int32 uInt64_qrm10 ( UInt64* n )
{
UInt32 rem, tmp;
Int32 i;
rem = 0;
for (i = 7; i >= 0; i--) {
tmp = rem * 256 + n->b[i];
n->b[i] = tmp / 10;
rem = tmp % 10;
}
return rem;
}
/* ... and the Whole Entire Point of all this UInt64 stuff is
so that we can supply the following function.
*/
void uInt64_toAscii ( char* outbuf, UInt64* n )
{
Int32 i, q;
UChar buf[32];
Int32 nBuf = 0;
UInt64 n_copy = *n;
do {
q = uInt64_qrm10 ( &n_copy );
buf[nBuf] = q + '0';
nBuf++;
} while (!uInt64_isZero(&n_copy));
outbuf[nBuf] = 0;
for (i = 0; i < nBuf; i++)
outbuf[i] = buf[nBuf-i-1];
}
/*---------------------------------------------------*/
/*--- Processing of complete files and streams ---*/
/*---------------------------------------------------*/
/*---------------------------------------------*/
static
Bool myfeof ( FILE* f )
{
Int32 c = fgetc ( f );
if (c == EOF) return True;
ungetc ( c, f );
return False;
}
/*---------------------------------------------------*/
int BZ_API(BZ2_bzCompressStream)( int ifd,
int ofd,
int blockSize100k,
int verbosity,
int workFactor )
{
FILE* stream;
FILE* zStream;
BZFILE* bzf = NULL;
UChar ibuf[5000];
Int32 nIbuf;
UInt32 nbytes_in_lo32, nbytes_in_hi32;
UInt32 nbytes_out_lo32, nbytes_out_hi32;
Int32 bzerr, ret;
stream = fdopen(ifd, "r");
if (!stream || ferror(stream)) return BZ_IO_ERROR;
zStream = fdopen(ofd, "w");
if (!zStream || ferror(zStream)) return BZ_IO_ERROR;
bzf = BZ2_bzWriteOpen ( &bzerr, zStream,
blockSize100k, verbosity, workFactor );
if (bzerr != BZ_OK) return bzerr;
if (verbosity >= 2) fprintf ( stderr, "\n" );
while (True) {
if (myfeof(stream)) break;
nIbuf = fread ( ibuf, sizeof(UChar), 5000, stream );
if (ferror(stream)) return BZ_IO_ERROR;
if (nIbuf > 0) BZ2_bzWrite ( &bzerr, bzf, (void*)ibuf, nIbuf );
if (bzerr != BZ_OK) return bzerr;
}
BZ2_bzWriteClose64 ( &bzerr, bzf, 0,
&nbytes_in_lo32, &nbytes_in_hi32,
&nbytes_out_lo32, &nbytes_out_hi32 );
if (bzerr != BZ_OK) return bzerr;
if (ferror(zStream)) return BZ_IO_ERROR;
ret = fflush ( zStream );
if (ret == EOF) return BZ_IO_ERROR;
if (verbosity >= 1) {
if (nbytes_in_lo32 == 0 && nbytes_in_hi32 == 0) {
fprintf ( stderr, " no data compressed.\n");
} else {
Char buf_nin[32], buf_nout[32];
UInt64 nbytes_in, nbytes_out;
double nbytes_in_d, nbytes_out_d;
uInt64_from_UInt32s ( &nbytes_in,
nbytes_in_lo32, nbytes_in_hi32 );
uInt64_from_UInt32s ( &nbytes_out,
nbytes_out_lo32, nbytes_out_hi32 );
nbytes_in_d = uInt64_to_double ( &nbytes_in );
nbytes_out_d = uInt64_to_double ( &nbytes_out );
uInt64_toAscii ( buf_nin, &nbytes_in );
uInt64_toAscii ( buf_nout, &nbytes_out );
fprintf ( stderr, "%6.3f:1, %6.3f bits/byte, "
"%5.2f%% saved, %s in, %s out.\n",
nbytes_in_d / nbytes_out_d,
(8.0 * nbytes_out_d) / nbytes_in_d,
100.0 * (1.0 - nbytes_out_d / nbytes_in_d),
buf_nin,
buf_nout
);
}
}
return BZ_OK;
}
/*---------------------------------------------------*/
int BZ_API(BZ2_bzDecompressStream)( int ifd,
int ofd,
int verbosity,
int small )
{
FILE* stream;
FILE* zStream;
BZFILE* bzf = NULL;
Int32 bzerr, ret, nread, streamNo, i;
UChar obuf[5000];
UChar unused[BZ_MAX_UNUSED];
Int32 nUnused;
const void* unusedTmpV;
UChar* unusedTmp;
nUnused = 0;
streamNo = 0;
zStream = fdopen(ifd, "r");
if (!zStream || ferror(zStream)) return BZ_IO_ERROR;
stream = fdopen(ofd, "w");
if (!stream || ferror(stream)) return BZ_IO_ERROR;
while (True) {
bzf = BZ2_bzReadOpen ( &bzerr, zStream, verbosity,
small, unused, nUnused );
if (bzerr != BZ_OK) return bzerr;
if (bzf == NULL) return BZ_IO_ERROR;
streamNo++;
while (bzerr == BZ_OK) {
nread = BZ2_bzRead ( &bzerr, bzf, obuf, 5000 );
if (bzerr == BZ_DATA_ERROR_MAGIC) {
if (streamNo == 1) {
return bzerr;
} else {
return BZ_OK;
}
}
if ((bzerr == BZ_OK || bzerr == BZ_STREAM_END) && nread > 0)
fwrite ( obuf, sizeof(UChar), nread, stream );
if (ferror(stream)) return BZ_IO_ERROR;
}
if (bzerr != BZ_STREAM_END) return bzerr;
BZ2_bzReadGetUnused ( &bzerr, bzf, &unusedTmpV, &nUnused );
if (bzerr != BZ_OK) return bzerr;
unusedTmp = (UChar*)unusedTmpV;
for (i = 0; i < nUnused; i++) unused[i] = unusedTmp[i];
BZ2_bzReadClose ( &bzerr, bzf );
if (bzerr != BZ_OK) return bzerr;
if (nUnused == 0 && myfeof(zStream)) break;
}
if (ferror(zStream)) return BZ_IO_ERROR;
if (ferror(stream)) return BZ_IO_ERROR;
ret = fflush ( stream );
if (ret != 0) return BZ_IO_ERROR;
if (verbosity >= 2) fprintf ( stderr, "\n " );
return BZ_OK;
}
/*---------------------------------------------------*/
int BZ_API(BZ2_bzTestStream)( int ifd,
int verbosity,
int small )
{
FILE* zStream;
BZFILE* bzf = NULL;
Int32 bzerr, streamNo, i;
UChar obuf[5000];
UChar unused[BZ_MAX_UNUSED];
Int32 nUnused;
const void* unusedTmpV;
UChar* unusedTmp;
nUnused = 0;
streamNo = 0;
zStream = fdopen(ifd, "r");
if (!zStream || ferror(zStream)) return BZ_IO_ERROR;
while (True) {
bzf = BZ2_bzReadOpen ( &bzerr, zStream, verbosity,
small, unused, nUnused );
if (bzerr != BZ_OK) return bzerr;
if (bzf == NULL) return BZ_IO_ERROR;
streamNo++;
while (bzerr == BZ_OK) {
BZ2_bzRead ( &bzerr, bzf, obuf, 5000 );
if (bzerr == BZ_DATA_ERROR_MAGIC) return bzerr;
}
if (bzerr != BZ_STREAM_END) return bzerr;
BZ2_bzReadGetUnused ( &bzerr, bzf, &unusedTmpV, &nUnused );
if (bzerr != BZ_OK) return bzerr;
unusedTmp = (UChar*)unusedTmpV;
for (i = 0; i < nUnused; i++) unused[i] = unusedTmp[i];
BZ2_bzReadClose ( &bzerr, bzf );
if (bzerr != BZ_OK) return bzerr;
if (nUnused == 0 && myfeof(zStream)) break;
}
if (ferror(zStream)) return BZ_IO_ERROR;
if (verbosity >= 2) fprintf ( stderr, "\n " );
return BZ_OK;
}