Debian Codegraph @

zgz/old-bzip2/bzlib.c

raw
1
2 /*-------------------------------------------------------------*/
3 /*--- Library top-level functions. ---*/
4 /*--- bzlib.c ---*/
5 /*-------------------------------------------------------------*/
6
7 /*--
8 This file is a part of bzip2 and/or libbzip2, a program and
9 library for lossless, block-sorting data compression.
10
11 Copyright (C) 1996-1999 Julian R Seward. All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions
15 are met:
16
17 1. Redistributions of source code must retain the above copyright
18 notice, this list of conditions and the following disclaimer.
19
20 2. The origin of this software must not be misrepresented; you must
21 not claim that you wrote the original software. If you use this
22 software in a product, an acknowledgment in the product
23 documentation would be appreciated but is not required.
24
25 3. Altered source versions must be plainly marked as such, and must
26 not be misrepresented as being the original software.
27
28 4. The name of the author may not be used to endorse or promote
29 products derived from this software without specific prior written
30 permission.
31
32 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
33 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
34 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
36 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
38 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
40 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
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.
43
44 Julian Seward, Cambridge, UK.
45 [email protected]
46 bzip2/libbzip2 version 0.9.5 of 24 May 1999
47
48 This program is based on (at least) the work of:
49 Mike Burrows
50 David Wheeler
51 Peter Fenwick
52 Alistair Moffat
53 Radford Neal
54 Ian H. Witten
55 Robert Sedgewick
56 Jon L. Bentley
57
58 For more information on these sources, see the manual.
59 --*/
60
61 /*--
62 CHANGES
63 ~~~~~~~
64 0.9.0 -- original version.
65
66 0.9.0a/b -- no changes in this file.
67
68 0.9.0c
69 * made zero-length BZ_FLUSH work correctly in bzCompress().
70 * fixed bzWrite/bzRead to ignore zero-length requests.
71 * fixed bzread to correctly handle read requests after EOF.
72 * wrong parameter order in call to bzDecompressInit in
73 bzBuffToBuffDecompress. Fixed.
74 --*/
75
76 #include "bzlib_private.h"
77
78 void bz__AssertH__fail ( int errcode ) {
79 fprintf(stderr, "bzip2 compressor internal error\n");
80 exit(1);
81 }
82
83 /*---------------------------------------------------*/
84 /*--- Compression stuff ---*/
85 /*---------------------------------------------------*/
86
87 /*---------------------------------------------------*/
88 static
89 void* default_bzalloc ( void* opaque, Int32 items, Int32 size )
90 {
91 void* v = malloc ( items * size );
92 return v;
93 }
94
95 static
96 void default_bzfree ( void* opaque, void* addr )
97 {
98 if (addr != NULL) free ( addr );
99 }
100
101
102 /*---------------------------------------------------*/
103 static
104 void prepare_new_block ( EState* s )
105 {
106 Int32 i;
107 s->nblock = 0;
108 s->numZ = 0;
109 s->state_out_pos = 0;
110 BZ_INITIALISE_CRC ( s->blockCRC );
111 for (i = 0; i < 256; i++) s->inUse[i] = False;
112 s->blockNo++;
113 }
114
115
116 /*---------------------------------------------------*/
117 static
118 void init_RL ( EState* s )
119 {
120 s->state_in_ch = 256;
121 s->state_in_len = 0;
122 }
123
124
125 static
126 Bool isempty_RL ( EState* s )
127 {
128 if (s->state_in_ch < 256 && s->state_in_len > 0)
129 return False; else
130 return True;
131 }
132
133
134 /*---------------------------------------------------*/
135 int BZ_API(bzCompressInit)
136 ( bz_stream* strm,
137 int blockSize100k,
138 int verbosity,
139 int workFactor )
140 {
141 Int32 n;
142 EState* s;
143
144 if (strm == NULL ||
145 blockSize100k < 1 || blockSize100k > 9 ||
146 workFactor < 0 || workFactor > 250)
147 return BZ_PARAM_ERROR;
148
149 if (workFactor == 0) workFactor = 30;
150 if (strm->bzalloc == NULL) strm->bzalloc = default_bzalloc;
151 if (strm->bzfree == NULL) strm->bzfree = default_bzfree;
152
153 s = BZALLOC( sizeof(EState) );
154 if (s == NULL) return BZ_MEM_ERROR;
155 s->strm = strm;
156
157 s->arr1 = NULL;
158 s->arr2 = NULL;
159 s->ftab = NULL;
160
161 n = 100000 * blockSize100k;
162 s->arr1 = BZALLOC( n * sizeof(UInt32) );
163 s->arr2 = BZALLOC( (n+BZ_N_OVERSHOOT) * sizeof(UInt32) );
164 s->ftab = BZALLOC( 65537 * sizeof(UInt32) );
165
166 if (s->arr1 == NULL || s->arr2 == NULL || s->ftab == NULL) {
167 if (s->arr1 != NULL) BZFREE(s->arr1);
168 if (s->arr2 != NULL) BZFREE(s->arr2);
169 if (s->ftab != NULL) BZFREE(s->ftab);
170 if (s != NULL) BZFREE(s);
171 return BZ_MEM_ERROR;
172 }
173
174 s->blockNo = 0;
175 s->state = BZ_S_INPUT;
176 s->mode = BZ_M_RUNNING;
177 s->combinedCRC = 0;
178 s->blockSize100k = blockSize100k;
179 s->nblockMAX = 100000 * blockSize100k - 19;
180 s->verbosity = verbosity;
181 s->workFactor = workFactor;
182
183 s->block = (UInt16*)s->arr2;
184 s->mtfv = (UInt16*)s->arr1;
185 s->zbits = NULL;
186 s->ptr = (UInt32*)s->arr1;
187
188 strm->state = s;
189 strm->total_in = 0;
190 strm->total_out = 0;
191 init_RL ( s );
192 prepare_new_block ( s );
193 return BZ_OK;
194 }
195
196
197 /*---------------------------------------------------*/
198 static
199 void add_pair_to_block ( EState* s )
200 {
201 Int32 i;
202 UChar ch = (UChar)(s->state_in_ch);
203 for (i = 0; i < s->state_in_len; i++) {
204 BZ_UPDATE_CRC( s->blockCRC, ch );
205 }
206 s->inUse[s->state_in_ch] = True;
207 switch (s->state_in_len) {
208 case 1:
209 s->block[s->nblock] = (UInt16)ch; s->nblock++;
210 break;
211 case 2:
212 s->block[s->nblock] = (UInt16)ch; s->nblock++;
213 s->block[s->nblock] = (UInt16)ch; s->nblock++;
214 break;
215 case 3:
216 s->block[s->nblock] = (UInt16)ch; s->nblock++;
217 s->block[s->nblock] = (UInt16)ch; s->nblock++;
218 s->block[s->nblock] = (UInt16)ch; s->nblock++;
219 break;
220 default:
221 s->inUse[s->state_in_len-4] = True;
222 s->block[s->nblock] = (UInt16)ch; s->nblock++;
223 s->block[s->nblock] = (UInt16)ch; s->nblock++;
224 s->block[s->nblock] = (UInt16)ch; s->nblock++;
225 s->block[s->nblock] = (UInt16)ch; s->nblock++;
226 s->block[s->nblock] = ((UInt16)(s->state_in_len-4));
227 s->nblock++;
228 break;
229 }
230 }
231
232
233 /*---------------------------------------------------*/
234 static
235 void flush_RL ( EState* s )
236 {
237 if (s->state_in_ch < 256) add_pair_to_block ( s );
238 init_RL ( s );
239 }
240
241
242 /*---------------------------------------------------*/
243 #define ADD_CHAR_TO_BLOCK(zs,zchh0) \
244 { \
245 UInt32 zchh = (UInt32)(zchh0); \
246 /*-- fast track the common case --*/ \
247 if (zchh != zs->state_in_ch && \
248 zs->state_in_len == 1) { \
249 UChar ch = (UChar)(zs->state_in_ch); \
250 BZ_UPDATE_CRC( zs->blockCRC, ch ); \
251 zs->inUse[zs->state_in_ch] = True; \
252 zs->block[zs->nblock] = (UInt16)ch; \
253 zs->nblock++; \
254 zs->state_in_ch = zchh; \
255 } \
256 else \
257 /*-- general, uncommon cases --*/ \
258 if (zchh != zs->state_in_ch || \
259 zs->state_in_len == 255) { \
260 if (zs->state_in_ch < 256) \
261 add_pair_to_block ( zs ); \
262 zs->state_in_ch = zchh; \
263 zs->state_in_len = 1; \
264 } else { \
265 zs->state_in_len++; \
266 } \
267 }
268
269
270 /*---------------------------------------------------*/
271 static
272 Bool copy_input_until_stop ( EState* s )
273 {
274 Bool progress_in = False;
275
276 if (s->mode == BZ_M_RUNNING) {
277
278 /*-- fast track the common case --*/
279 while (True) {
280 /*-- block full? --*/
281 if (s->nblock >= s->nblockMAX) break;
282 /*-- no input? --*/
283 if (s->strm->avail_in == 0) break;
284 progress_in = True;
285 ADD_CHAR_TO_BLOCK ( s, (UInt32)(*((UChar*)(s->strm->next_in))) );
286 s->strm->next_in++;
287 s->strm->avail_in--;
288 s->strm->total_in++;
289 }
290
291 } else {
292
293 /*-- general, uncommon case --*/
294 while (True) {
295 /*-- block full? --*/
296 if (s->nblock >= s->nblockMAX) break;
297 /*-- no input? --*/
298 if (s->strm->avail_in == 0) break;
299 /*-- flush/finish end? --*/
300 if (s->avail_in_expect == 0) break;
301 progress_in = True;
302 ADD_CHAR_TO_BLOCK ( s, (UInt32)(*((UChar*)(s->strm->next_in))) );
303 s->strm->next_in++;
304 s->strm->avail_in--;
305 s->strm->total_in++;
306 s->avail_in_expect--;
307 }
308 }
309 return progress_in;
310 }
311
312
313 /*---------------------------------------------------*/
314 static
315 Bool copy_output_until_stop ( EState* s )
316 {
317 Bool progress_out = False;
318
319 while (True) {
320
321 /*-- no output space? --*/
322 if (s->strm->avail_out == 0) break;
323
324 /*-- block done? --*/
325 if (s->state_out_pos >= s->numZ) break;
326
327 progress_out = True;
328 *(s->strm->next_out) = s->zbits[s->state_out_pos];
329 s->state_out_pos++;
330 s->strm->avail_out--;
331 s->strm->next_out++;
332 s->strm->total_out++;
333
334 }
335
336 return progress_out;
337 }
338
339
340 /*---------------------------------------------------*/
341 static
342 Bool handle_compress ( bz_stream* strm )
343 {
344 Bool progress_in = False;
345 Bool progress_out = False;
346 EState* s = strm->state;
347
348 while (True) {
349
350 if (s->state == BZ_S_OUTPUT) {
351 progress_out |= copy_output_until_stop ( s );
352 if (s->state_out_pos < s->numZ) break;
353 if (s->mode == BZ_M_FINISHING &&
354 s->avail_in_expect == 0 &&
355 isempty_RL(s)) break;
356 prepare_new_block ( s );
357 s->state = BZ_S_INPUT;
358 if (s->mode == BZ_M_FLUSHING &&
359 s->avail_in_expect == 0 &&
360 isempty_RL(s)) break;
361 }
362
363 if (s->state == BZ_S_INPUT) {
364 progress_in |= copy_input_until_stop ( s );
365 if (s->mode != BZ_M_RUNNING && s->avail_in_expect == 0) {
366 flush_RL ( s );
367 compressBlock ( s, (Bool)(s->mode == BZ_M_FINISHING) );
368 s->state = BZ_S_OUTPUT;
369 }
370 else
371 if (s->nblock >= s->nblockMAX) {
372 compressBlock ( s, False );
373 s->state = BZ_S_OUTPUT;
374 }
375 else
376 if (s->strm->avail_in == 0) {
377 break;
378 }
379 }
380
381 }
382
383 return progress_in || progress_out;
384 }
385
386
387 /*---------------------------------------------------*/
388 int BZ_API(bzCompress) ( bz_stream *strm, int action )
389 {
390 Bool progress;
391 EState* s;
392 if (strm == NULL) return BZ_PARAM_ERROR;
393 s = strm->state;
394 if (s == NULL) return BZ_PARAM_ERROR;
395 if (s->strm != strm) return BZ_PARAM_ERROR;
396
397 preswitch:
398 switch (s->mode) {
399
400 case BZ_M_IDLE:
401 return BZ_SEQUENCE_ERROR;
402
403 case BZ_M_RUNNING:
404 if (action == BZ_RUN) {
405 progress = handle_compress ( strm );
406 return progress ? BZ_RUN_OK : BZ_PARAM_ERROR;
407 }
408 else
409 if (action == BZ_FLUSH) {
410 s->avail_in_expect = strm->avail_in;
411 s->mode = BZ_M_FLUSHING;
412 goto preswitch;
413 }
414 else
415 if (action == BZ_FINISH) {
416 s->avail_in_expect = strm->avail_in;
417 s->mode = BZ_M_FINISHING;
418 goto preswitch;
419 }
420 else
421 return BZ_PARAM_ERROR;
422
423 case BZ_M_FLUSHING:
424 if (action != BZ_FLUSH) return BZ_SEQUENCE_ERROR;
425 if (s->avail_in_expect != s->strm->avail_in) return BZ_SEQUENCE_ERROR;
426 progress = handle_compress ( strm );
427 if (s->avail_in_expect > 0 || !isempty_RL(s) ||
428 s->state_out_pos < s->numZ) return BZ_FLUSH_OK;
429 s->mode = BZ_M_RUNNING;
430 return BZ_RUN_OK;
431
432 case BZ_M_FINISHING:
433 if (action != BZ_FINISH) return BZ_SEQUENCE_ERROR;
434 if (s->avail_in_expect != s->strm->avail_in) return BZ_SEQUENCE_ERROR;
435 progress = handle_compress ( strm );
436 if (!progress) return BZ_SEQUENCE_ERROR;
437 if (s->avail_in_expect > 0 || !isempty_RL(s) ||
438 s->state_out_pos < s->numZ) return BZ_FINISH_OK;
439 s->mode = BZ_M_IDLE;
440 return BZ_STREAM_END;
441 }
442 return BZ_OK; /*--not reached--*/
443 }
444
445
446 /*---------------------------------------------------*/
447 int BZ_API(bzCompressEnd) ( bz_stream *strm )
448 {
449 EState* s;
450 if (strm == NULL) return BZ_PARAM_ERROR;
451 s = strm->state;
452 if (s == NULL) return BZ_PARAM_ERROR;
453 if (s->strm != strm) return BZ_PARAM_ERROR;
454
455 if (s->arr1 != NULL) BZFREE(s->arr1);
456 if (s->arr2 != NULL) BZFREE(s->arr2);
457 if (s->ftab != NULL) BZFREE(s->ftab);
458 BZFREE(strm->state);
459
460 strm->state = NULL;
461
462 return BZ_OK;
463 }
464
465
466 #ifndef BZ_NO_STDIO
467 /*---------------------------------------------------*/
468 /*--- File I/O stuff ---*/
469 /*---------------------------------------------------*/
470
471 #define BZ_SETERR(eee) \
472 { \
473 if (bzerror != NULL) *bzerror = eee; \
474 if (bzf != NULL) bzf->lastErr = eee; \
475 }
476
477 typedef
478 struct {
479 FILE* handle;
480 Char buf[BZ_MAX_UNUSED];
481 Int32 bufN;
482 Bool writing;
483 bz_stream strm;
484 Int32 lastErr;
485 Bool initialisedOk;
486 }
487 bzFile;
488
489
490 /*---------------------------------------------------*/
491 BZFILE* BZ_API(bzWriteOpen)
492 ( int* bzerror,
493 FILE* f,
494 int blockSize100k,
495 int verbosity,
496 int workFactor )
497 {
498 Int32 ret;
499 bzFile* bzf = NULL;
500
501 BZ_SETERR(BZ_OK);
502
503 if (f == NULL ||
504 (blockSize100k < 1 || blockSize100k > 9) ||
505 (workFactor < 0 || workFactor > 250) ||
506 (verbosity < 0 || verbosity > 4))
507 { BZ_SETERR(BZ_PARAM_ERROR); return NULL; };
508
509 if (ferror(f))
510 { BZ_SETERR(BZ_IO_ERROR); return NULL; };
511
512 bzf = malloc ( sizeof(bzFile) );
513 if (bzf == NULL)
514 { BZ_SETERR(BZ_MEM_ERROR); return NULL; };
515
516 BZ_SETERR(BZ_OK);
517 bzf->initialisedOk = False;
518 bzf->bufN = 0;
519 bzf->handle = f;
520 bzf->writing = True;
521 bzf->strm.bzalloc = NULL;
522 bzf->strm.bzfree = NULL;
523 bzf->strm.opaque = NULL;
524
525 if (workFactor == 0) workFactor = 30;
526 ret = bzCompressInit ( &(bzf->strm), blockSize100k,
527 verbosity, workFactor );
528 if (ret != BZ_OK)
529 { BZ_SETERR(ret); free(bzf); return NULL; };
530
531 bzf->strm.avail_in = 0;
532 bzf->initialisedOk = True;
533 return bzf;
534 }
535
536
537
538 /*---------------------------------------------------*/
539 void BZ_API(bzWrite)
540 ( int* bzerror,
541 BZFILE* b,
542 void* buf,
543 int len )
544 {
545 Int32 n, n2, ret;
546 bzFile* bzf = (bzFile*)b;
547
548 BZ_SETERR(BZ_OK);
549 if (bzf == NULL || buf == NULL || len < 0)
550 { BZ_SETERR(BZ_PARAM_ERROR); return; };
551 if (!(bzf->writing))
552 { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
553 if (ferror(bzf->handle))
554 { BZ_SETERR(BZ_IO_ERROR); return; };
555
556 if (len == 0)
557 { BZ_SETERR(BZ_OK); return; };
558
559 bzf->strm.avail_in = len;
560 bzf->strm.next_in = buf;
561
562 while (True) {
563 bzf->strm.avail_out = BZ_MAX_UNUSED;
564 bzf->strm.next_out = bzf->buf;
565 ret = bzCompress ( &(bzf->strm), BZ_RUN );
566 if (ret != BZ_RUN_OK)
567 { BZ_SETERR(ret); return; };
568
569 if (bzf->strm.avail_out < BZ_MAX_UNUSED) {
570 n = BZ_MAX_UNUSED - bzf->strm.avail_out;
571 n2 = fwrite ( (void*)(bzf->buf), sizeof(UChar),
572 n, bzf->handle );
573 if (n != n2 || ferror(bzf->handle))
574 { BZ_SETERR(BZ_IO_ERROR); return; };
575 }
576
577 if (bzf->strm.avail_in == 0)
578 { BZ_SETERR(BZ_OK); return; };
579 }
580 }
581
582
583 /*---------------------------------------------------*/
584 void BZ_API(bzWriteClose)
585 ( int* bzerror,
586 BZFILE* b,
587 int abandon,
588 unsigned int* nbytes_in,
589 unsigned int* nbytes_out )
590 {
591 Int32 n, n2, ret;
592 bzFile* bzf = (bzFile*)b;
593
594 if (bzf == NULL)
595 { BZ_SETERR(BZ_OK); return; };
596 if (!(bzf->writing))
597 { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
598 if (ferror(bzf->handle))
599 { BZ_SETERR(BZ_IO_ERROR); return; };
600
601 if (nbytes_in != NULL) *nbytes_in = 0;
602 if (nbytes_out != NULL) *nbytes_out = 0;
603
604 if ((!abandon) && bzf->lastErr == BZ_OK) {
605 while (True) {
606 bzf->strm.avail_out = BZ_MAX_UNUSED;
607 bzf->strm.next_out = bzf->buf;
608 ret = bzCompress ( &(bzf->strm), BZ_FINISH );
609 if (ret != BZ_FINISH_OK && ret != BZ_STREAM_END)
610 { BZ_SETERR(ret); return; };
611
612 if (bzf->strm.avail_out < BZ_MAX_UNUSED) {
613 n = BZ_MAX_UNUSED - bzf->strm.avail_out;
614 n2 = fwrite ( (void*)(bzf->buf), sizeof(UChar),
615 n, bzf->handle );
616 if (n != n2 || ferror(bzf->handle))
617 { BZ_SETERR(BZ_IO_ERROR); return; };
618 }
619
620 if (ret == BZ_STREAM_END) break;
621 }
622 }
623
624 if ( !abandon && !ferror ( bzf->handle ) ) {
625 fflush ( bzf->handle );
626 if (ferror(bzf->handle))
627 { BZ_SETERR(BZ_IO_ERROR); return; };
628 }
629
630 if (nbytes_in != NULL) *nbytes_in = bzf->strm.total_in;
631 if (nbytes_out != NULL) *nbytes_out = bzf->strm.total_out;
632
633 BZ_SETERR(BZ_OK);
634 bzCompressEnd ( &(bzf->strm) );
635 free ( bzf );
636 }
637
638
639 /*---------------------------------------------------*/
640 void BZ_API(bzReadGetUnused)
641 ( int* bzerror,
642 BZFILE* b,
643 void** unused,
644 int* nUnused )
645 {
646 bzFile* bzf = (bzFile*)b;
647 if (bzf == NULL)
648 { BZ_SETERR(BZ_PARAM_ERROR); return; };
649 if (bzf->lastErr != BZ_STREAM_END)
650 { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
651 if (unused == NULL || nUnused == NULL)
652 { BZ_SETERR(BZ_PARAM_ERROR); return; };
653
654 BZ_SETERR(BZ_OK);
655 *nUnused = bzf->strm.avail_in;
656 *unused = bzf->strm.next_in;
657 }
658 #endif
659
660
661 /*---------------------------------------------------*/
662 /*--- Misc convenience stuff ---*/
663 /*---------------------------------------------------*/
664
665 /*---------------------------------------------------*/
666 int BZ_API(bzBuffToBuffCompress)
667 ( char* dest,
668 unsigned int* destLen,
669 char* source,
670 unsigned int sourceLen,
671 int blockSize100k,
672 int verbosity,
673 int workFactor )
674 {
675 bz_stream strm;
676 int ret;
677
678 if (dest == NULL || destLen == NULL ||
679 source == NULL ||
680 blockSize100k < 1 || blockSize100k > 9 ||
681 verbosity < 0 || verbosity > 4 ||
682 workFactor < 0 || workFactor > 250)
683 return BZ_PARAM_ERROR;
684
685 if (workFactor == 0) workFactor = 30;
686 strm.bzalloc = NULL;
687 strm.bzfree = NULL;
688 strm.opaque = NULL;
689 ret = bzCompressInit ( &strm, blockSize100k,
690 verbosity, workFactor );
691 if (ret != BZ_OK) return ret;
692
693 strm.next_in = source;
694 strm.next_out = dest;
695 strm.avail_in = sourceLen;
696 strm.avail_out = *destLen;
697
698 ret = bzCompress ( &strm, BZ_FINISH );
699 if (ret == BZ_FINISH_OK) goto output_overflow;
700 if (ret != BZ_STREAM_END) goto errhandler;
701
702 /* normal termination */
703 *destLen -= strm.avail_out;
704 bzCompressEnd ( &strm );
705 return BZ_OK;
706
707 output_overflow:
708 bzCompressEnd ( &strm );
709 return BZ_OUTBUFF_FULL;
710
711 errhandler:
712 bzCompressEnd ( &strm );
713 return ret;
714 }
715
716
717 /*---------------------------------------------------*/
718 /*--
719 Code contributed by Yoshioka Tsuneo
720 ([email protected]/[email protected]),
721 to support better zlib compatibility.
722 This code is not _officially_ part of libbzip2 (yet);
723 I haven't tested it, documented it, or considered the
724 threading-safeness of it.
725 If this code breaks, please contact both Yoshioka and me.
726 --*/
727 /*---------------------------------------------------*/
728
729 /*---------------------------------------------------*/
730 /*--
731 return version like "0.9.0c".
732 --*/
733 const char * BZ_API(bzlibVersion)(void)
734 {
735 return BZ_VERSION;
736 }
737
738
739 #ifndef BZ_NO_STDIO
740 /*---------------------------------------------------*/
741
742 #if defined(_WIN32) || defined(OS2) || defined(MSDOS)
743 # include <fcntl.h>
744 # include <io.h>
745 # define SET_BINARY_MODE(file) setmode(fileno(file),O_BINARY)
746 #else
747 # define SET_BINARY_MODE(file)
748 #endif
749 static
750 BZFILE * bzopen_or_bzdopen
751 ( const char *path, /* no use when bzdopen */
752 int fd, /* no use when bzdopen */
753 const char *mode,
754 int open_mode) /* bzopen: 0, bzdopen:1 */
755 {
756 int bzerr;
757 int blockSize100k = 9;
758 int writing = 0;
759 char mode2[10] = "";
760 FILE *fp = NULL;
761 BZFILE *bzfp = NULL;
762 int verbosity = 0;
763 int workFactor = 30;
764
765 if (mode == NULL) return NULL;
766 while (*mode) {
767 switch (*mode) {
768 case 'r':
769 writing = 0; break;
770 case 'w':
771 writing = 1; break;
772 case 's':
773 //smallMode = 1;
774 break;
775 default:
776 if (isdigit((int)(*mode))) {
777 blockSize100k = *mode-'0';
778 }
779 }
780 mode++;
781 }
782 strcat(mode2, writing ? "w" : "r" );
783 strcat(mode2,"b"); /* binary mode */
784
785 if (open_mode==0) {
786 if (path==NULL || strcmp(path,"")==0) {
787 fp = (writing ? stdout : stdin);
788 SET_BINARY_MODE(fp);
789 } else {
790 fp = fopen(path,mode2);
791 }
792 } else {
793 #ifdef BZ_STRICT_ANSI
794 fp = NULL;
795 #else
796 fp = fdopen(fd,mode2);
797 #endif
798 }
799 if (fp == NULL) return NULL;
800
801 if (writing) {
802 /* Guard against total chaos and anarchy -- JRS */
803 if (blockSize100k < 1) blockSize100k = 1;
804 if (blockSize100k > 9) blockSize100k = 9;
805 bzfp = bzWriteOpen(&bzerr,fp,blockSize100k,verbosity,workFactor);
806 }
807 if (bzfp == NULL) {
808 if (fp != stdin && fp != stdout) fclose(fp);
809 return NULL;
810 }
811 return bzfp;
812 }
813
814
815 /*---------------------------------------------------*/
816 /*--
817 open file for read or write.
818 ex) bzopen("file","w9")
819 case path="" or NULL => use stdin or stdout.
820 --*/
821 BZFILE * BZ_API(bzopen)
822 ( const char *path,
823 const char *mode )
824 {
825 return bzopen_or_bzdopen(path,-1,mode,/*bzopen*/0);
826 }
827
828
829 /*---------------------------------------------------*/
830 BZFILE * BZ_API(bzdopen)
831 ( int fd,
832 const char *mode )
833 {
834 return bzopen_or_bzdopen(NULL,fd,mode,/*bzdopen*/1);
835 }
836
837
838 /*---------------------------------------------------*/
839 int BZ_API(bzwrite) (BZFILE* b, void* buf, int len )
840 {
841 int bzerr;
842
843 bzWrite(&bzerr,b,buf,len);
844 if(bzerr == BZ_OK){
845 return len;
846 }else{
847 return -1;
848 }
849 }
850
851
852 /*---------------------------------------------------*/
853 int BZ_API(bzflush) (BZFILE *b)
854 {
855 /* do nothing now... */
856 return 0;
857 }
858
859
860 /*---------------------------------------------------*/
861 void BZ_API(bzclose) (BZFILE* b)
862 {
863 int bzerr;
864 FILE *fp = ((bzFile *)b)->handle;
865
866 if (b==NULL) {return;}
867 if(((bzFile*)b)->writing){
868 bzWriteClose(&bzerr,b,0,NULL,NULL);
869 if(bzerr != BZ_OK){
870 bzWriteClose(NULL,b,1,NULL,NULL);
871 }
872 }
873 if(fp!=stdin && fp!=stdout){
874 fclose(fp);
875 }
876 }
877
878
879 /*---------------------------------------------------*/
880 /*--
881 return last error code
882 --*/
883 static char *bzerrorstrings[] = {
884 "OK"
885 ,"SEQUENCE_ERROR"
886 ,"PARAM_ERROR"
887 ,"MEM_ERROR"
888 ,"DATA_ERROR"
889 ,"DATA_ERROR_MAGIC"
890 ,"IO_ERROR"
891 ,"UNEXPECTED_EOF"
892 ,"OUTBUFF_FULL"
893 ,"???" /* for future */
894 ,"???" /* for future */
895 ,"???" /* for future */
896 ,"???" /* for future */
897 ,"???" /* for future */
898 ,"???" /* for future */
899 };
900
901
902 const char * BZ_API(bzerror) (BZFILE *b, int *errnum)
903 {
904 int err = ((bzFile *)b)->lastErr;
905
906 if(err>0) err = 0;
907 *errnum = err;
908 return bzerrorstrings[err*-1];
909 }
910 #endif
911
912
913 /*-------------------------------------------------------------*/
914 /*--- end bzlib.c ---*/
915 /*-------------------------------------------------------------*/