aboutsummaryrefslogtreecommitdiff
path: root/bzlib.c
diff options
context:
space:
mode:
authorJulian Seward <jseward@acm.org>1999-09-04 22:13:13 +0200
committerJulian Seward <jseward@acm.org>1999-09-04 22:13:13 +0200
commitf93cd82a9a7094ad90fd19bbc6ccf6f4627f8060 (patch)
treec95407df5665f5a7395683f07552f2b13f2e501f /bzlib.c
parent977101ad5f833f5c0a574bfeea408e5301a6b052 (diff)
downloadbzip2-f93cd82a9a7094ad90fd19bbc6ccf6f4627f8060.tar.gz
bzip2-f93cd82a9a7094ad90fd19bbc6ccf6f4627f8060.tar.bz2
bzip2-f93cd82a9a7094ad90fd19bbc6ccf6f4627f8060.zip
bzip2-0.9.5dbzip2-0.9.5d
Diffstat (limited to 'bzlib.c')
-rw-r--r--bzlib.c138
1 files changed, 67 insertions, 71 deletions
diff --git a/bzlib.c b/bzlib.c
index 362e8ff..24e8bd5 100644
--- a/bzlib.c
+++ b/bzlib.c
@@ -8,7 +8,7 @@
8 This file is a part of bzip2 and/or libbzip2, a program and 8 This file is a part of bzip2 and/or libbzip2, a program and
9 library for lossless, block-sorting data compression. 9 library for lossless, block-sorting data compression.
10 10
11 Copyright (C) 1996-1998 Julian R Seward. All rights reserved. 11 Copyright (C) 1996-1999 Julian R Seward. All rights reserved.
12 12
13 Redistribution and use in source and binary forms, with or without 13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions 14 modification, are permitted provided that the following conditions
@@ -41,9 +41,9 @@
41 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 41 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 42 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 43
44 Julian Seward, Guildford, Surrey, UK. 44 Julian Seward, Cambridge, UK.
45 jseward@acm.org 45 jseward@acm.org
46 bzip2/libbzip2 version 0.9.0c of 18 October 1998 46 bzip2/libbzip2 version 0.9.5 of 24 May 1999
47 47
48 This program is based on (at least) the work of: 48 This program is based on (at least) the work of:
49 Mike Burrows 49 Mike Burrows
@@ -86,14 +86,14 @@
86void bz__AssertH__fail ( int errcode ) 86void bz__AssertH__fail ( int errcode )
87{ 87{
88 fprintf(stderr, 88 fprintf(stderr,
89 "\n\nbzip2/libbzip2, v0.9.0c: internal error number %d.\n" 89 "\n\nbzip2/libbzip2, v0.9.5d: internal error number %d.\n"
90 "This is a bug in bzip2/libbzip2, v0.9.0c. Please report\n" 90 "This is a bug in bzip2/libbzip2, v0.9.5d. Please report\n"
91 "it to me at: jseward@acm.org. If this happened when\n" 91 "it to me at: jseward@acm.org. If this happened when\n"
92 "you were using some program which uses libbzip2 as a\n" 92 "you were using some program which uses libbzip2 as a\n"
93 "component, you should also report this bug to the author(s)\n" 93 "component, you should also report this bug to the author(s)\n"
94 "of that program. Please make an effort to report this bug;\n" 94 "of that program. Please make an effort to report this bug;\n"
95 "timely and accurate bug reports eventually lead to higher\n" 95 "timely and accurate bug reports eventually lead to higher\n"
96 "quality software. Thx. Julian Seward, 18 October 1998.\n\n", 96 "quality software. Thanks. Julian Seward, 4 Sept 1999.\n\n",
97 errcode 97 errcode
98 ); 98 );
99 exit(3); 99 exit(3);
@@ -171,29 +171,23 @@ int BZ_API(bzCompressInit)
171 if (s == NULL) return BZ_MEM_ERROR; 171 if (s == NULL) return BZ_MEM_ERROR;
172 s->strm = strm; 172 s->strm = strm;
173 173
174 s->block = NULL; 174 s->arr1 = NULL;
175 s->quadrant = NULL; 175 s->arr2 = NULL;
176 s->zptr = NULL; 176 s->ftab = NULL;
177 s->ftab = NULL; 177
178 178 n = 100000 * blockSize100k;
179 n = 100000 * blockSize100k; 179 s->arr1 = BZALLOC( n * sizeof(UInt32) );
180 s->block = BZALLOC( (n + BZ_NUM_OVERSHOOT_BYTES) * sizeof(UChar) ); 180 s->arr2 = BZALLOC( (n+BZ_N_OVERSHOOT) * sizeof(UInt32) );
181 s->quadrant = BZALLOC( (n + BZ_NUM_OVERSHOOT_BYTES) * sizeof(Int16) ); 181 s->ftab = BZALLOC( 65537 * sizeof(UInt32) );
182 s->zptr = BZALLOC( n * sizeof(Int32) ); 182
183 s->ftab = BZALLOC( 65537 * sizeof(Int32) ); 183 if (s->arr1 == NULL || s->arr2 == NULL || s->ftab == NULL) {
184 184 if (s->arr1 != NULL) BZFREE(s->arr1);
185 if (s->block == NULL || s->quadrant == NULL || 185 if (s->arr2 != NULL) BZFREE(s->arr2);
186 s->zptr == NULL || s->ftab == NULL) { 186 if (s->ftab != NULL) BZFREE(s->ftab);
187 if (s->block != NULL) BZFREE(s->block); 187 if (s != NULL) BZFREE(s);
188 if (s->quadrant != NULL) BZFREE(s->quadrant);
189 if (s->zptr != NULL) BZFREE(s->zptr);
190 if (s->ftab != NULL) BZFREE(s->ftab);
191 if (s != NULL) BZFREE(s);
192 return BZ_MEM_ERROR; 188 return BZ_MEM_ERROR;
193 } 189 }
194 190
195 s->szptr = (UInt16*)(s->zptr);
196
197 s->blockNo = 0; 191 s->blockNo = 0;
198 s->state = BZ_S_INPUT; 192 s->state = BZ_S_INPUT;
199 s->mode = BZ_M_RUNNING; 193 s->mode = BZ_M_RUNNING;
@@ -202,7 +196,12 @@ int BZ_API(bzCompressInit)
202 s->nblockMAX = 100000 * blockSize100k - 19; 196 s->nblockMAX = 100000 * blockSize100k - 19;
203 s->verbosity = verbosity; 197 s->verbosity = verbosity;
204 s->workFactor = workFactor; 198 s->workFactor = workFactor;
205 s->nBlocksRandomised = 0; 199
200 s->block = (UInt16*)s->arr2;
201 s->mtfv = (UInt16*)s->arr1;
202 s->zbits = NULL;
203 s->ptr = (UInt32*)s->arr1;
204
206 strm->state = s; 205 strm->state = s;
207 strm->total_in = 0; 206 strm->total_in = 0;
208 strm->total_out = 0; 207 strm->total_out = 0;
@@ -224,24 +223,24 @@ void add_pair_to_block ( EState* s )
224 s->inUse[s->state_in_ch] = True; 223 s->inUse[s->state_in_ch] = True;
225 switch (s->state_in_len) { 224 switch (s->state_in_len) {
226 case 1: 225 case 1:
227 s->block[s->nblock] = (UChar)ch; s->nblock++; 226 s->block[s->nblock] = (UInt16)ch; s->nblock++;
228 break; 227 break;
229 case 2: 228 case 2:
230 s->block[s->nblock] = (UChar)ch; s->nblock++; 229 s->block[s->nblock] = (UInt16)ch; s->nblock++;
231 s->block[s->nblock] = (UChar)ch; s->nblock++; 230 s->block[s->nblock] = (UInt16)ch; s->nblock++;
232 break; 231 break;
233 case 3: 232 case 3:
234 s->block[s->nblock] = (UChar)ch; s->nblock++; 233 s->block[s->nblock] = (UInt16)ch; s->nblock++;
235 s->block[s->nblock] = (UChar)ch; s->nblock++; 234 s->block[s->nblock] = (UInt16)ch; s->nblock++;
236 s->block[s->nblock] = (UChar)ch; s->nblock++; 235 s->block[s->nblock] = (UInt16)ch; s->nblock++;
237 break; 236 break;
238 default: 237 default:
239 s->inUse[s->state_in_len-4] = True; 238 s->inUse[s->state_in_len-4] = True;
240 s->block[s->nblock] = (UChar)ch; s->nblock++; 239 s->block[s->nblock] = (UInt16)ch; s->nblock++;
241 s->block[s->nblock] = (UChar)ch; s->nblock++; 240 s->block[s->nblock] = (UInt16)ch; s->nblock++;
242 s->block[s->nblock] = (UChar)ch; s->nblock++; 241 s->block[s->nblock] = (UInt16)ch; s->nblock++;
243 s->block[s->nblock] = (UChar)ch; s->nblock++; 242 s->block[s->nblock] = (UInt16)ch; s->nblock++;
244 s->block[s->nblock] = (UChar)(s->state_in_len-4); 243 s->block[s->nblock] = ((UInt16)(s->state_in_len-4));
245 s->nblock++; 244 s->nblock++;
246 break; 245 break;
247 } 246 }
@@ -267,7 +266,7 @@ void flush_RL ( EState* s )
267 UChar ch = (UChar)(zs->state_in_ch); \ 266 UChar ch = (UChar)(zs->state_in_ch); \
268 BZ_UPDATE_CRC( zs->blockCRC, ch ); \ 267 BZ_UPDATE_CRC( zs->blockCRC, ch ); \
269 zs->inUse[zs->state_in_ch] = True; \ 268 zs->inUse[zs->state_in_ch] = True; \
270 zs->block[zs->nblock] = (UChar)ch; \ 269 zs->block[zs->nblock] = (UInt16)ch; \
271 zs->nblock++; \ 270 zs->nblock++; \
272 zs->state_in_ch = zchh; \ 271 zs->state_in_ch = zchh; \
273 } \ 272 } \
@@ -343,7 +342,7 @@ Bool copy_output_until_stop ( EState* s )
343 if (s->state_out_pos >= s->numZ) break; 342 if (s->state_out_pos >= s->numZ) break;
344 343
345 progress_out = True; 344 progress_out = True;
346 *(s->strm->next_out) = ((UChar*)(s->quadrant))[s->state_out_pos]; 345 *(s->strm->next_out) = s->zbits[s->state_out_pos];
347 s->state_out_pos++; 346 s->state_out_pos++;
348 s->strm->avail_out--; 347 s->strm->avail_out--;
349 s->strm->next_out++; 348 s->strm->next_out++;
@@ -382,7 +381,7 @@ Bool handle_compress ( bz_stream* strm )
382 progress_in |= copy_input_until_stop ( s ); 381 progress_in |= copy_input_until_stop ( s );
383 if (s->mode != BZ_M_RUNNING && s->avail_in_expect == 0) { 382 if (s->mode != BZ_M_RUNNING && s->avail_in_expect == 0) {
384 flush_RL ( s ); 383 flush_RL ( s );
385 compressBlock ( s, s->mode == BZ_M_FINISHING ); 384 compressBlock ( s, (Bool)(s->mode == BZ_M_FINISHING) );
386 s->state = BZ_S_OUTPUT; 385 s->state = BZ_S_OUTPUT;
387 } 386 }
388 else 387 else
@@ -470,10 +469,9 @@ int BZ_API(bzCompressEnd) ( bz_stream *strm )
470 if (s == NULL) return BZ_PARAM_ERROR; 469 if (s == NULL) return BZ_PARAM_ERROR;
471 if (s->strm != strm) return BZ_PARAM_ERROR; 470 if (s->strm != strm) return BZ_PARAM_ERROR;
472 471
473 if (s->block != NULL) BZFREE(s->block); 472 if (s->arr1 != NULL) BZFREE(s->arr1);
474 if (s->quadrant != NULL) BZFREE(s->quadrant); 473 if (s->arr2 != NULL) BZFREE(s->arr2);
475 if (s->zptr != NULL) BZFREE(s->zptr); 474 if (s->ftab != NULL) BZFREE(s->ftab);
476 if (s->ftab != NULL) BZFREE(s->ftab);
477 BZFREE(strm->state); 475 BZFREE(strm->state);
478 476
479 strm->state = NULL; 477 strm->state = NULL;
@@ -816,7 +814,8 @@ int BZ_API(bzDecompress) ( bz_stream *strm )
816 } 814 }
817 815
818 AssertH ( 0, 6001 ); 816 AssertH ( 0, 6001 );
819 /*notreached*/ 817
818 return 0; /*NOTREACHED*/
820} 819}
821 820
822 821
@@ -1284,7 +1283,7 @@ int BZ_API(bzBuffToBuffDecompress)
1284 1283
1285 errhandler: 1284 errhandler:
1286 bzDecompressEnd ( &strm ); 1285 bzDecompressEnd ( &strm );
1287 return BZ_SEQUENCE_ERROR; 1286 return ret;
1288} 1287}
1289 1288
1290 1289
@@ -1339,24 +1338,18 @@ BZFILE * bzopen_or_bzdopen
1339 int smallMode = 0; 1338 int smallMode = 0;
1340 int nUnused = 0; 1339 int nUnused = 0;
1341 1340
1342 if(mode==NULL){return NULL;} 1341 if (mode == NULL) return NULL;
1343 while(*mode){ 1342 while (*mode) {
1344 switch(*mode){ 1343 switch (*mode) {
1345 case 'r': 1344 case 'r':
1346 writing = 0;break; 1345 writing = 0; break;
1347 case 'w': 1346 case 'w':
1348 writing = 1;break; 1347 writing = 1; break;
1349 case 's': 1348 case 's':
1350 smallMode = 1;break; 1349 smallMode = 1; break;
1351 default: 1350 default:
1352 if(isdigit(*mode)){ 1351 if (isdigit((int)(*mode))) {
1353 blockSize100k = 0; 1352 blockSize100k = *mode-'0';
1354 while(isdigit(*mode)){
1355 blockSize100k = blockSize100k*10 + *mode-'0';
1356 mode++;
1357 }
1358 }else{
1359 /* ignore */
1360 } 1353 }
1361 } 1354 }
1362 mode++; 1355 mode++;
@@ -1364,29 +1357,32 @@ BZFILE * bzopen_or_bzdopen
1364 strcat(mode2, writing ? "w" : "r" ); 1357 strcat(mode2, writing ? "w" : "r" );
1365 strcat(mode2,"b"); /* binary mode */ 1358 strcat(mode2,"b"); /* binary mode */
1366 1359
1367 if(open_mode==0){ 1360 if (open_mode==0) {
1368 if(path==NULL || strcmp(path,"")==0){ 1361 if (path==NULL || strcmp(path,"")==0) {
1369 fp = (writing ? stdout : stdin); 1362 fp = (writing ? stdout : stdin);
1370 SET_BINARY_MODE(fp); 1363 SET_BINARY_MODE(fp);
1371 }else{ 1364 } else {
1372 fp = fopen(path,mode2); 1365 fp = fopen(path,mode2);
1373 } 1366 }
1374 }else{ 1367 } else {
1375#ifdef BZ_STRICT_ANSI 1368#ifdef BZ_STRICT_ANSI
1376 fp = NULL; 1369 fp = NULL;
1377#else 1370#else
1378 fp = fdopen(fd,mode2); 1371 fp = fdopen(fd,mode2);
1379#endif 1372#endif
1380 } 1373 }
1381 if(fp==NULL){return NULL;} 1374 if (fp == NULL) return NULL;
1382 1375
1383 if(writing){ 1376 if (writing) {
1377 /* Guard against total chaos and anarchy -- JRS */
1378 if (blockSize100k < 1) blockSize100k = 1;
1379 if (blockSize100k > 9) blockSize100k = 9;
1384 bzfp = bzWriteOpen(&bzerr,fp,blockSize100k,verbosity,workFactor); 1380 bzfp = bzWriteOpen(&bzerr,fp,blockSize100k,verbosity,workFactor);
1385 }else{ 1381 } else {
1386 bzfp = bzReadOpen(&bzerr,fp,verbosity,smallMode,unused,nUnused); 1382 bzfp = bzReadOpen(&bzerr,fp,verbosity,smallMode,unused,nUnused);
1387 } 1383 }
1388 if(bzfp==NULL){ 1384 if (bzfp == NULL) {
1389 if(fp!=stdin && fp!=stdout) fclose(fp); 1385 if (fp != stdin && fp != stdout) fclose(fp);
1390 return NULL; 1386 return NULL;
1391 } 1387 }
1392 return bzfp; 1388 return bzfp;
@@ -1458,7 +1454,7 @@ void BZ_API(bzclose) (BZFILE* b)
1458 int bzerr; 1454 int bzerr;
1459 FILE *fp = ((bzFile *)b)->handle; 1455 FILE *fp = ((bzFile *)b)->handle;
1460 1456
1461 if(b==NULL){return;} 1457 if (b==NULL) {return;}
1462 if(((bzFile*)b)->writing){ 1458 if(((bzFile*)b)->writing){
1463 bzWriteClose(&bzerr,b,0,NULL,NULL); 1459 bzWriteClose(&bzerr,b,0,NULL,NULL);
1464 if(bzerr != BZ_OK){ 1460 if(bzerr != BZ_OK){