Reverting back to original 1.8 version so I can manually merge in patch.
[llvm-complete.git] / lib / Support / bzip2 / bzlib.c
blobff3cd16158a602833858a276c45f14cc68daa87b
2 /*-------------------------------------------------------------*/
3 /*--- Library top-level functions. ---*/
4 /*--- bzlib.c ---*/
5 /*-------------------------------------------------------------*/
7 /*--
8 This file is a part of bzip2 and/or libbzip2, a program and
9 library for lossless, block-sorting data compression.
11 Copyright (C) 1996-2002 Julian R Seward. All rights reserved.
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions
15 are met:
17 1. Redistributions of source code must retain the above copyright
18 notice, this list of conditions and the following disclaimer.
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.
25 3. Altered source versions must be plainly marked as such, and must
26 not be misrepresented as being the original software.
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.
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.
44 Julian Seward, Cambridge, UK.
45 jseward@acm.org
46 bzip2/libbzip2 version 1.0 of 21 March 2000
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
58 For more information on these sources, see the manual.
59 --*/
61 /*--
62 CHANGES
63 ~~~~~~~
64 0.9.0 -- original version.
66 0.9.0a/b -- no changes in this file.
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 --*/
76 #include "bzlib_private.h"
79 /*---------------------------------------------------*/
80 /*--- Compression stuff ---*/
81 /*---------------------------------------------------*/
84 /*---------------------------------------------------*/
85 #ifndef BZ_NO_STDIO
86 void BZ2_bz__AssertH__fail ( int errcode )
88 fprintf(stderr,
89 "\n\nbzip2/libbzip2: internal error number %d.\n"
90 "This is a bug in bzip2/libbzip2, %s.\n"
91 "Please report it to me at: jseward@acm.org. If this happened\n"
92 "when you were using some program which uses libbzip2 as a\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"
95 "timely and accurate bug reports eventually lead to higher\n"
96 "quality software. Thanks. Julian Seward, 30 December 2001.\n\n",
97 errcode,
98 BZ2_bzlibVersion()
101 if (errcode == 1007) {
102 fprintf(stderr,
103 "\n*** A special note about internal error number 1007 ***\n"
104 "\n"
105 "Experience suggests that a common cause of i.e. 1007\n"
106 "is unreliable memory or other hardware. The 1007 assertion\n"
107 "just happens to cross-check the results of huge numbers of\n"
108 "memory reads/writes, and so acts (unintendedly) as a stress\n"
109 "test of your memory system.\n"
110 "\n"
112 fprintf(stderr,
113 "I suggest the following: try compressing the file again,\n"
114 "possibly monitoring progress in detail with the -vv flag.\n"
115 "\n"
116 "* If the error cannot be reproduced, and/or happens at different\n"
117 " points in compression, you may have a flaky memory system.\n"
118 " Try a memory-test program. I have used Memtest86\n"
119 " (www.memtest86.com). At the time of writing it is free (GPLd).\n"
120 " Memtest86 tests memory much more thorougly than your BIOSs\n"
121 " power-on test, and may find failures that the BIOS doesn't.\n"
122 "\n"
124 fprintf(stderr,
125 "* If the error can be repeatably reproduced, this is a bug in\n"
126 " bzip2, and I would very much like to hear about it. Please\n"
127 " let me know, and, ideally, save a copy of the file causing the\n"
128 " problem -- without which I will be unable to investigate it.\n"
129 "\n"
133 exit(3);
135 #endif
138 /*---------------------------------------------------*/
139 static
140 int bz_config_ok ( void )
142 if (sizeof(int) != 4) return 0;
143 if (sizeof(short) != 2) return 0;
144 if (sizeof(char) != 1) return 0;
145 return 1;
149 /*---------------------------------------------------*/
150 static
151 void* default_bzalloc ( void* opaque, Int32 items, Int32 size )
153 void* v = malloc ( items * size );
154 return v;
157 static
158 void default_bzfree ( void* opaque, void* addr )
160 if (addr != NULL) free ( addr );
164 /*---------------------------------------------------*/
165 static
166 void prepare_new_block ( EState* s )
168 Int32 i;
169 s->nblock = 0;
170 s->numZ = 0;
171 s->state_out_pos = 0;
172 BZ_INITIALISE_CRC ( s->blockCRC );
173 for (i = 0; i < 256; i++) s->inUse[i] = False;
174 s->blockNo++;
178 /*---------------------------------------------------*/
179 static
180 void init_RL ( EState* s )
182 s->state_in_ch = 256;
183 s->state_in_len = 0;
187 static
188 Bool isempty_RL ( EState* s )
190 if (s->state_in_ch < 256 && s->state_in_len > 0)
191 return False; else
192 return True;
196 /*---------------------------------------------------*/
197 int BZ_API(BZ2_bzCompressInit)
198 ( bz_stream* strm,
199 int blockSize100k,
200 int verbosity,
201 int workFactor )
203 Int32 n;
204 EState* s;
206 if (!bz_config_ok()) return BZ_CONFIG_ERROR;
208 if (strm == NULL ||
209 blockSize100k < 1 || blockSize100k > 9 ||
210 workFactor < 0 || workFactor > 250)
211 return BZ_PARAM_ERROR;
213 if (workFactor == 0) workFactor = 30;
214 if (strm->bzalloc == NULL) strm->bzalloc = default_bzalloc;
215 if (strm->bzfree == NULL) strm->bzfree = default_bzfree;
217 s = BZALLOC( sizeof(EState) );
218 if (s == NULL) return BZ_MEM_ERROR;
219 s->strm = strm;
221 s->arr1 = NULL;
222 s->arr2 = NULL;
223 s->ftab = NULL;
225 n = 100000 * blockSize100k;
226 s->arr1 = BZALLOC( n * sizeof(UInt32) );
227 s->arr2 = BZALLOC( (n+BZ_N_OVERSHOOT) * sizeof(UInt32) );
228 s->ftab = BZALLOC( 65537 * sizeof(UInt32) );
230 if (s->arr1 == NULL || s->arr2 == NULL || s->ftab == NULL) {
231 if (s->arr1 != NULL) BZFREE(s->arr1);
232 if (s->arr2 != NULL) BZFREE(s->arr2);
233 if (s->ftab != NULL) BZFREE(s->ftab);
234 if (s != NULL) BZFREE(s);
235 return BZ_MEM_ERROR;
238 s->blockNo = 0;
239 s->state = BZ_S_INPUT;
240 s->mode = BZ_M_RUNNING;
241 s->combinedCRC = 0;
242 s->blockSize100k = blockSize100k;
243 s->nblockMAX = 100000 * blockSize100k - 19;
244 s->verbosity = verbosity;
245 s->workFactor = workFactor;
247 s->block = (UChar*)s->arr2;
248 s->mtfv = (UInt16*)s->arr1;
249 s->zbits = NULL;
250 s->ptr = (UInt32*)s->arr1;
252 strm->state = s;
253 strm->total_in_lo32 = 0;
254 strm->total_in_hi32 = 0;
255 strm->total_out_lo32 = 0;
256 strm->total_out_hi32 = 0;
257 init_RL ( s );
258 prepare_new_block ( s );
259 return BZ_OK;
263 /*---------------------------------------------------*/
264 static
265 void add_pair_to_block ( EState* s )
267 Int32 i;
268 UChar ch = (UChar)(s->state_in_ch);
269 for (i = 0; i < s->state_in_len; i++) {
270 BZ_UPDATE_CRC( s->blockCRC, ch );
272 s->inUse[s->state_in_ch] = True;
273 switch (s->state_in_len) {
274 case 1:
275 s->block[s->nblock] = (UChar)ch; s->nblock++;
276 break;
277 case 2:
278 s->block[s->nblock] = (UChar)ch; s->nblock++;
279 s->block[s->nblock] = (UChar)ch; s->nblock++;
280 break;
281 case 3:
282 s->block[s->nblock] = (UChar)ch; s->nblock++;
283 s->block[s->nblock] = (UChar)ch; s->nblock++;
284 s->block[s->nblock] = (UChar)ch; s->nblock++;
285 break;
286 default:
287 s->inUse[s->state_in_len-4] = True;
288 s->block[s->nblock] = (UChar)ch; s->nblock++;
289 s->block[s->nblock] = (UChar)ch; s->nblock++;
290 s->block[s->nblock] = (UChar)ch; s->nblock++;
291 s->block[s->nblock] = (UChar)ch; s->nblock++;
292 s->block[s->nblock] = ((UChar)(s->state_in_len-4));
293 s->nblock++;
294 break;
299 /*---------------------------------------------------*/
300 static
301 void flush_RL ( EState* s )
303 if (s->state_in_ch < 256) add_pair_to_block ( s );
304 init_RL ( s );
308 /*---------------------------------------------------*/
309 #define ADD_CHAR_TO_BLOCK(zs,zchh0) \
311 UInt32 zchh = (UInt32)(zchh0); \
312 /*-- fast track the common case --*/ \
313 if (zchh != zs->state_in_ch && \
314 zs->state_in_len == 1) { \
315 UChar ch = (UChar)(zs->state_in_ch); \
316 BZ_UPDATE_CRC( zs->blockCRC, ch ); \
317 zs->inUse[zs->state_in_ch] = True; \
318 zs->block[zs->nblock] = (UChar)ch; \
319 zs->nblock++; \
320 zs->state_in_ch = zchh; \
322 else \
323 /*-- general, uncommon cases --*/ \
324 if (zchh != zs->state_in_ch || \
325 zs->state_in_len == 255) { \
326 if (zs->state_in_ch < 256) \
327 add_pair_to_block ( zs ); \
328 zs->state_in_ch = zchh; \
329 zs->state_in_len = 1; \
330 } else { \
331 zs->state_in_len++; \
336 /*---------------------------------------------------*/
337 static
338 Bool copy_input_until_stop ( EState* s )
340 Bool progress_in = False;
342 if (s->mode == BZ_M_RUNNING) {
344 /*-- fast track the common case --*/
345 while (True) {
346 /*-- block full? --*/
347 if (s->nblock >= s->nblockMAX) break;
348 /*-- no input? --*/
349 if (s->strm->avail_in == 0) break;
350 progress_in = True;
351 ADD_CHAR_TO_BLOCK ( s, (UInt32)(*((UChar*)(s->strm->next_in))) );
352 s->strm->next_in++;
353 s->strm->avail_in--;
354 s->strm->total_in_lo32++;
355 if (s->strm->total_in_lo32 == 0) s->strm->total_in_hi32++;
358 } else {
360 /*-- general, uncommon case --*/
361 while (True) {
362 /*-- block full? --*/
363 if (s->nblock >= s->nblockMAX) break;
364 /*-- no input? --*/
365 if (s->strm->avail_in == 0) break;
366 /*-- flush/finish end? --*/
367 if (s->avail_in_expect == 0) break;
368 progress_in = True;
369 ADD_CHAR_TO_BLOCK ( s, (UInt32)(*((UChar*)(s->strm->next_in))) );
370 s->strm->next_in++;
371 s->strm->avail_in--;
372 s->strm->total_in_lo32++;
373 if (s->strm->total_in_lo32 == 0) s->strm->total_in_hi32++;
374 s->avail_in_expect--;
377 return progress_in;
381 /*---------------------------------------------------*/
382 static
383 Bool copy_output_until_stop ( EState* s )
385 Bool progress_out = False;
387 while (True) {
389 /*-- no output space? --*/
390 if (s->strm->avail_out == 0) break;
392 /*-- block done? --*/
393 if (s->state_out_pos >= s->numZ) break;
395 progress_out = True;
396 *(s->strm->next_out) = s->zbits[s->state_out_pos];
397 s->state_out_pos++;
398 s->strm->avail_out--;
399 s->strm->next_out++;
400 s->strm->total_out_lo32++;
401 if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++;
404 return progress_out;
408 /*---------------------------------------------------*/
409 static
410 Bool handle_compress ( bz_stream* strm )
412 Bool progress_in = False;
413 Bool progress_out = False;
414 EState* s = strm->state;
416 while (True) {
418 if (s->state == BZ_S_OUTPUT) {
419 progress_out |= copy_output_until_stop ( s );
420 if (s->state_out_pos < s->numZ) break;
421 if (s->mode == BZ_M_FINISHING &&
422 s->avail_in_expect == 0 &&
423 isempty_RL(s)) break;
424 prepare_new_block ( s );
425 s->state = BZ_S_INPUT;
426 if (s->mode == BZ_M_FLUSHING &&
427 s->avail_in_expect == 0 &&
428 isempty_RL(s)) break;
431 if (s->state == BZ_S_INPUT) {
432 progress_in |= copy_input_until_stop ( s );
433 if (s->mode != BZ_M_RUNNING && s->avail_in_expect == 0) {
434 flush_RL ( s );
435 BZ2_compressBlock ( s, (Bool)(s->mode == BZ_M_FINISHING) );
436 s->state = BZ_S_OUTPUT;
438 else
439 if (s->nblock >= s->nblockMAX) {
440 BZ2_compressBlock ( s, False );
441 s->state = BZ_S_OUTPUT;
443 else
444 if (s->strm->avail_in == 0) {
445 break;
451 return progress_in || progress_out;
455 /*---------------------------------------------------*/
456 int BZ_API(BZ2_bzCompress) ( bz_stream *strm, int action )
458 Bool progress;
459 EState* s;
460 if (strm == NULL) return BZ_PARAM_ERROR;
461 s = strm->state;
462 if (s == NULL) return BZ_PARAM_ERROR;
463 if (s->strm != strm) return BZ_PARAM_ERROR;
465 preswitch:
466 switch (s->mode) {
468 case BZ_M_IDLE:
469 return BZ_SEQUENCE_ERROR;
471 case BZ_M_RUNNING:
472 if (action == BZ_RUN) {
473 progress = handle_compress ( strm );
474 return progress ? BZ_RUN_OK : BZ_PARAM_ERROR;
476 else
477 if (action == BZ_FLUSH) {
478 s->avail_in_expect = strm->avail_in;
479 s->mode = BZ_M_FLUSHING;
480 goto preswitch;
482 else
483 if (action == BZ_FINISH) {
484 s->avail_in_expect = strm->avail_in;
485 s->mode = BZ_M_FINISHING;
486 goto preswitch;
488 else
489 return BZ_PARAM_ERROR;
491 case BZ_M_FLUSHING:
492 if (action != BZ_FLUSH) return BZ_SEQUENCE_ERROR;
493 if (s->avail_in_expect != s->strm->avail_in)
494 return BZ_SEQUENCE_ERROR;
495 progress = handle_compress ( strm );
496 if (s->avail_in_expect > 0 || !isempty_RL(s) ||
497 s->state_out_pos < s->numZ) return BZ_FLUSH_OK;
498 s->mode = BZ_M_RUNNING;
499 return BZ_RUN_OK;
501 case BZ_M_FINISHING:
502 if (action != BZ_FINISH) return BZ_SEQUENCE_ERROR;
503 if (s->avail_in_expect != s->strm->avail_in)
504 return BZ_SEQUENCE_ERROR;
505 progress = handle_compress ( strm );
506 if (!progress) return BZ_SEQUENCE_ERROR;
507 if (s->avail_in_expect > 0 || !isempty_RL(s) ||
508 s->state_out_pos < s->numZ) return BZ_FINISH_OK;
509 s->mode = BZ_M_IDLE;
510 return BZ_STREAM_END;
512 return BZ_OK; /*--not reached--*/
516 /*---------------------------------------------------*/
517 int BZ_API(BZ2_bzCompressEnd) ( bz_stream *strm )
519 EState* s;
520 if (strm == NULL) return BZ_PARAM_ERROR;
521 s = strm->state;
522 if (s == NULL) return BZ_PARAM_ERROR;
523 if (s->strm != strm) return BZ_PARAM_ERROR;
525 if (s->arr1 != NULL) BZFREE(s->arr1);
526 if (s->arr2 != NULL) BZFREE(s->arr2);
527 if (s->ftab != NULL) BZFREE(s->ftab);
528 BZFREE(strm->state);
530 strm->state = NULL;
532 return BZ_OK;
536 /*---------------------------------------------------*/
537 /*--- Decompression stuff ---*/
538 /*---------------------------------------------------*/
540 /*---------------------------------------------------*/
541 int BZ_API(BZ2_bzDecompressInit)
542 ( bz_stream* strm,
543 int verbosity,
544 int small )
546 DState* s;
548 if (!bz_config_ok()) return BZ_CONFIG_ERROR;
550 if (strm == NULL) return BZ_PARAM_ERROR;
551 if (small != 0 && small != 1) return BZ_PARAM_ERROR;
552 if (verbosity < 0 || verbosity > 4) return BZ_PARAM_ERROR;
554 if (strm->bzalloc == NULL) strm->bzalloc = default_bzalloc;
555 if (strm->bzfree == NULL) strm->bzfree = default_bzfree;
557 s = BZALLOC( sizeof(DState) );
558 if (s == NULL) return BZ_MEM_ERROR;
559 s->strm = strm;
560 strm->state = s;
561 s->state = BZ_X_MAGIC_1;
562 s->bsLive = 0;
563 s->bsBuff = 0;
564 s->calculatedCombinedCRC = 0;
565 strm->total_in_lo32 = 0;
566 strm->total_in_hi32 = 0;
567 strm->total_out_lo32 = 0;
568 strm->total_out_hi32 = 0;
569 s->smallDecompress = (Bool)small;
570 s->ll4 = NULL;
571 s->ll16 = NULL;
572 s->tt = NULL;
573 s->currBlockNo = 0;
574 s->verbosity = verbosity;
576 return BZ_OK;
580 /*---------------------------------------------------*/
581 static
582 void unRLE_obuf_to_output_FAST ( DState* s )
584 UChar k1;
586 if (s->blockRandomised) {
588 while (True) {
589 /* try to finish existing run */
590 while (True) {
591 if (s->strm->avail_out == 0) return;
592 if (s->state_out_len == 0) break;
593 *( (UChar*)(s->strm->next_out) ) = s->state_out_ch;
594 BZ_UPDATE_CRC ( s->calculatedBlockCRC, s->state_out_ch );
595 s->state_out_len--;
596 s->strm->next_out++;
597 s->strm->avail_out--;
598 s->strm->total_out_lo32++;
599 if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++;
602 /* can a new run be started? */
603 if (s->nblock_used == s->save_nblock+1) return;
606 s->state_out_len = 1;
607 s->state_out_ch = s->k0;
608 BZ_GET_FAST(k1); BZ_RAND_UPD_MASK;
609 k1 ^= BZ_RAND_MASK; s->nblock_used++;
610 if (s->nblock_used == s->save_nblock+1) continue;
611 if (k1 != s->k0) { s->k0 = k1; continue; };
613 s->state_out_len = 2;
614 BZ_GET_FAST(k1); BZ_RAND_UPD_MASK;
615 k1 ^= BZ_RAND_MASK; s->nblock_used++;
616 if (s->nblock_used == s->save_nblock+1) continue;
617 if (k1 != s->k0) { s->k0 = k1; continue; };
619 s->state_out_len = 3;
620 BZ_GET_FAST(k1); BZ_RAND_UPD_MASK;
621 k1 ^= BZ_RAND_MASK; s->nblock_used++;
622 if (s->nblock_used == s->save_nblock+1) continue;
623 if (k1 != s->k0) { s->k0 = k1; continue; };
625 BZ_GET_FAST(k1); BZ_RAND_UPD_MASK;
626 k1 ^= BZ_RAND_MASK; s->nblock_used++;
627 s->state_out_len = ((Int32)k1) + 4;
628 BZ_GET_FAST(s->k0); BZ_RAND_UPD_MASK;
629 s->k0 ^= BZ_RAND_MASK; s->nblock_used++;
632 } else {
634 /* restore */
635 UInt32 c_calculatedBlockCRC = s->calculatedBlockCRC;
636 UChar c_state_out_ch = s->state_out_ch;
637 Int32 c_state_out_len = s->state_out_len;
638 Int32 c_nblock_used = s->nblock_used;
639 Int32 c_k0 = s->k0;
640 UInt32* c_tt = s->tt;
641 UInt32 c_tPos = s->tPos;
642 char* cs_next_out = s->strm->next_out;
643 unsigned int cs_avail_out = s->strm->avail_out;
644 /* end restore */
646 UInt32 avail_out_INIT = cs_avail_out;
647 Int32 s_save_nblockPP = s->save_nblock+1;
648 unsigned int total_out_lo32_old;
650 while (True) {
652 /* try to finish existing run */
653 if (c_state_out_len > 0) {
654 while (True) {
655 if (cs_avail_out == 0) goto return_notr;
656 if (c_state_out_len == 1) break;
657 *( (UChar*)(cs_next_out) ) = c_state_out_ch;
658 BZ_UPDATE_CRC ( c_calculatedBlockCRC, c_state_out_ch );
659 c_state_out_len--;
660 cs_next_out++;
661 cs_avail_out--;
663 s_state_out_len_eq_one:
665 if (cs_avail_out == 0) {
666 c_state_out_len = 1; goto return_notr;
668 *( (UChar*)(cs_next_out) ) = c_state_out_ch;
669 BZ_UPDATE_CRC ( c_calculatedBlockCRC, c_state_out_ch );
670 cs_next_out++;
671 cs_avail_out--;
674 /* can a new run be started? */
675 if (c_nblock_used == s_save_nblockPP) {
676 c_state_out_len = 0; goto return_notr;
678 c_state_out_ch = c_k0;
679 BZ_GET_FAST_C(k1); c_nblock_used++;
680 if (k1 != c_k0) {
681 c_k0 = k1; goto s_state_out_len_eq_one;
683 if (c_nblock_used == s_save_nblockPP)
684 goto s_state_out_len_eq_one;
686 c_state_out_len = 2;
687 BZ_GET_FAST_C(k1); c_nblock_used++;
688 if (c_nblock_used == s_save_nblockPP) continue;
689 if (k1 != c_k0) { c_k0 = k1; continue; };
691 c_state_out_len = 3;
692 BZ_GET_FAST_C(k1); c_nblock_used++;
693 if (c_nblock_used == s_save_nblockPP) continue;
694 if (k1 != c_k0) { c_k0 = k1; continue; };
696 BZ_GET_FAST_C(k1); c_nblock_used++;
697 c_state_out_len = ((Int32)k1) + 4;
698 BZ_GET_FAST_C(c_k0); c_nblock_used++;
701 return_notr:
702 total_out_lo32_old = s->strm->total_out_lo32;
703 s->strm->total_out_lo32 += (avail_out_INIT - cs_avail_out);
704 if (s->strm->total_out_lo32 < total_out_lo32_old)
705 s->strm->total_out_hi32++;
707 /* save */
708 s->calculatedBlockCRC = c_calculatedBlockCRC;
709 s->state_out_ch = c_state_out_ch;
710 s->state_out_len = c_state_out_len;
711 s->nblock_used = c_nblock_used;
712 s->k0 = c_k0;
713 s->tt = c_tt;
714 s->tPos = c_tPos;
715 s->strm->next_out = cs_next_out;
716 s->strm->avail_out = cs_avail_out;
717 /* end save */
723 /*---------------------------------------------------*/
724 __inline__ Int32 BZ2_indexIntoF ( Int32 indx, Int32 *cftab )
726 Int32 nb, na, mid;
727 nb = 0;
728 na = 256;
729 do {
730 mid = (nb + na) >> 1;
731 if (indx >= cftab[mid]) nb = mid; else na = mid;
733 while (na - nb != 1);
734 return nb;
738 /*---------------------------------------------------*/
739 static
740 void unRLE_obuf_to_output_SMALL ( DState* s )
742 UChar k1;
744 if (s->blockRandomised) {
746 while (True) {
747 /* try to finish existing run */
748 while (True) {
749 if (s->strm->avail_out == 0) return;
750 if (s->state_out_len == 0) break;
751 *( (UChar*)(s->strm->next_out) ) = s->state_out_ch;
752 BZ_UPDATE_CRC ( s->calculatedBlockCRC, s->state_out_ch );
753 s->state_out_len--;
754 s->strm->next_out++;
755 s->strm->avail_out--;
756 s->strm->total_out_lo32++;
757 if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++;
760 /* can a new run be started? */
761 if (s->nblock_used == s->save_nblock+1) return;
764 s->state_out_len = 1;
765 s->state_out_ch = s->k0;
766 BZ_GET_SMALL(k1); BZ_RAND_UPD_MASK;
767 k1 ^= BZ_RAND_MASK; s->nblock_used++;
768 if (s->nblock_used == s->save_nblock+1) continue;
769 if (k1 != s->k0) { s->k0 = k1; continue; };
771 s->state_out_len = 2;
772 BZ_GET_SMALL(k1); BZ_RAND_UPD_MASK;
773 k1 ^= BZ_RAND_MASK; s->nblock_used++;
774 if (s->nblock_used == s->save_nblock+1) continue;
775 if (k1 != s->k0) { s->k0 = k1; continue; };
777 s->state_out_len = 3;
778 BZ_GET_SMALL(k1); BZ_RAND_UPD_MASK;
779 k1 ^= BZ_RAND_MASK; s->nblock_used++;
780 if (s->nblock_used == s->save_nblock+1) continue;
781 if (k1 != s->k0) { s->k0 = k1; continue; };
783 BZ_GET_SMALL(k1); BZ_RAND_UPD_MASK;
784 k1 ^= BZ_RAND_MASK; s->nblock_used++;
785 s->state_out_len = ((Int32)k1) + 4;
786 BZ_GET_SMALL(s->k0); BZ_RAND_UPD_MASK;
787 s->k0 ^= BZ_RAND_MASK; s->nblock_used++;
790 } else {
792 while (True) {
793 /* try to finish existing run */
794 while (True) {
795 if (s->strm->avail_out == 0) return;
796 if (s->state_out_len == 0) break;
797 *( (UChar*)(s->strm->next_out) ) = s->state_out_ch;
798 BZ_UPDATE_CRC ( s->calculatedBlockCRC, s->state_out_ch );
799 s->state_out_len--;
800 s->strm->next_out++;
801 s->strm->avail_out--;
802 s->strm->total_out_lo32++;
803 if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++;
806 /* can a new run be started? */
807 if (s->nblock_used == s->save_nblock+1) return;
809 s->state_out_len = 1;
810 s->state_out_ch = s->k0;
811 BZ_GET_SMALL(k1); s->nblock_used++;
812 if (s->nblock_used == s->save_nblock+1) continue;
813 if (k1 != s->k0) { s->k0 = k1; continue; };
815 s->state_out_len = 2;
816 BZ_GET_SMALL(k1); s->nblock_used++;
817 if (s->nblock_used == s->save_nblock+1) continue;
818 if (k1 != s->k0) { s->k0 = k1; continue; };
820 s->state_out_len = 3;
821 BZ_GET_SMALL(k1); s->nblock_used++;
822 if (s->nblock_used == s->save_nblock+1) continue;
823 if (k1 != s->k0) { s->k0 = k1; continue; };
825 BZ_GET_SMALL(k1); s->nblock_used++;
826 s->state_out_len = ((Int32)k1) + 4;
827 BZ_GET_SMALL(s->k0); s->nblock_used++;
834 /*---------------------------------------------------*/
835 int BZ_API(BZ2_bzDecompress) ( bz_stream *strm )
837 DState* s;
838 if (strm == NULL) return BZ_PARAM_ERROR;
839 s = strm->state;
840 if (s == NULL) return BZ_PARAM_ERROR;
841 if (s->strm != strm) return BZ_PARAM_ERROR;
843 while (True) {
844 if (s->state == BZ_X_IDLE) return BZ_SEQUENCE_ERROR;
845 if (s->state == BZ_X_OUTPUT) {
846 if (s->smallDecompress)
847 unRLE_obuf_to_output_SMALL ( s ); else
848 unRLE_obuf_to_output_FAST ( s );
849 if (s->nblock_used == s->save_nblock+1 && s->state_out_len == 0) {
850 BZ_FINALISE_CRC ( s->calculatedBlockCRC );
851 if (s->verbosity >= 3)
852 VPrintf2 ( " {0x%x, 0x%x}", s->storedBlockCRC,
853 s->calculatedBlockCRC );
854 if (s->verbosity >= 2) VPrintf0 ( "]" );
855 if (s->calculatedBlockCRC != s->storedBlockCRC)
856 return BZ_DATA_ERROR;
857 s->calculatedCombinedCRC
858 = (s->calculatedCombinedCRC << 1) |
859 (s->calculatedCombinedCRC >> 31);
860 s->calculatedCombinedCRC ^= s->calculatedBlockCRC;
861 s->state = BZ_X_BLKHDR_1;
862 } else {
863 return BZ_OK;
866 if (s->state >= BZ_X_MAGIC_1) {
867 Int32 r = BZ2_decompress ( s );
868 if (r == BZ_STREAM_END) {
869 if (s->verbosity >= 3)
870 VPrintf2 ( "\n combined CRCs: stored = 0x%x, computed = 0x%x",
871 s->storedCombinedCRC, s->calculatedCombinedCRC );
872 if (s->calculatedCombinedCRC != s->storedCombinedCRC)
873 return BZ_DATA_ERROR;
874 return r;
876 if (s->state != BZ_X_OUTPUT) return r;
880 AssertH ( 0, 6001 );
882 return 0; /*NOTREACHED*/
886 /*---------------------------------------------------*/
887 int BZ_API(BZ2_bzDecompressEnd) ( bz_stream *strm )
889 DState* s;
890 if (strm == NULL) return BZ_PARAM_ERROR;
891 s = strm->state;
892 if (s == NULL) return BZ_PARAM_ERROR;
893 if (s->strm != strm) return BZ_PARAM_ERROR;
895 if (s->tt != NULL) BZFREE(s->tt);
896 if (s->ll16 != NULL) BZFREE(s->ll16);
897 if (s->ll4 != NULL) BZFREE(s->ll4);
899 BZFREE(strm->state);
900 strm->state = NULL;
902 return BZ_OK;
906 #ifndef BZ_NO_STDIO
907 /*---------------------------------------------------*/
908 /*--- File I/O stuff ---*/
909 /*---------------------------------------------------*/
911 #define BZ_SETERR(eee) \
913 if (bzerror != NULL) *bzerror = eee; \
914 if (bzf != NULL) bzf->lastErr = eee; \
917 typedef
918 struct {
919 FILE* handle;
920 Char buf[BZ_MAX_UNUSED];
921 Int32 bufN;
922 Bool writing;
923 bz_stream strm;
924 Int32 lastErr;
925 Bool initialisedOk;
927 bzFile;
930 /*---------------------------------------------*/
931 static Bool myfeof ( FILE* f )
933 Int32 c = fgetc ( f );
934 if (c == EOF) return True;
935 ungetc ( c, f );
936 return False;
940 /*---------------------------------------------------*/
941 BZFILE* BZ_API(BZ2_bzWriteOpen)
942 ( int* bzerror,
943 FILE* f,
944 int blockSize100k,
945 int verbosity,
946 int workFactor )
948 Int32 ret;
949 bzFile* bzf = NULL;
951 BZ_SETERR(BZ_OK);
953 if (f == NULL ||
954 (blockSize100k < 1 || blockSize100k > 9) ||
955 (workFactor < 0 || workFactor > 250) ||
956 (verbosity < 0 || verbosity > 4))
957 { BZ_SETERR(BZ_PARAM_ERROR); return NULL; };
959 if (ferror(f))
960 { BZ_SETERR(BZ_IO_ERROR); return NULL; };
962 bzf = malloc ( sizeof(bzFile) );
963 if (bzf == NULL)
964 { BZ_SETERR(BZ_MEM_ERROR); return NULL; };
966 BZ_SETERR(BZ_OK);
967 bzf->initialisedOk = False;
968 bzf->bufN = 0;
969 bzf->handle = f;
970 bzf->writing = True;
971 bzf->strm.bzalloc = NULL;
972 bzf->strm.bzfree = NULL;
973 bzf->strm.opaque = NULL;
975 if (workFactor == 0) workFactor = 30;
976 ret = BZ2_bzCompressInit ( &(bzf->strm), blockSize100k,
977 verbosity, workFactor );
978 if (ret != BZ_OK)
979 { BZ_SETERR(ret); free(bzf); return NULL; };
981 bzf->strm.avail_in = 0;
982 bzf->initialisedOk = True;
983 return bzf;
988 /*---------------------------------------------------*/
989 void BZ_API(BZ2_bzWrite)
990 ( int* bzerror,
991 BZFILE* b,
992 void* buf,
993 int len )
995 Int32 n, n2, ret;
996 bzFile* bzf = (bzFile*)b;
998 BZ_SETERR(BZ_OK);
999 if (bzf == NULL || buf == NULL || len < 0)
1000 { BZ_SETERR(BZ_PARAM_ERROR); return; };
1001 if (!(bzf->writing))
1002 { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
1003 if (ferror(bzf->handle))
1004 { BZ_SETERR(BZ_IO_ERROR); return; };
1006 if (len == 0)
1007 { BZ_SETERR(BZ_OK); return; };
1009 bzf->strm.avail_in = len;
1010 bzf->strm.next_in = buf;
1012 while (True) {
1013 bzf->strm.avail_out = BZ_MAX_UNUSED;
1014 bzf->strm.next_out = bzf->buf;
1015 ret = BZ2_bzCompress ( &(bzf->strm), BZ_RUN );
1016 if (ret != BZ_RUN_OK)
1017 { BZ_SETERR(ret); return; };
1019 if (bzf->strm.avail_out < BZ_MAX_UNUSED) {
1020 n = BZ_MAX_UNUSED - bzf->strm.avail_out;
1021 n2 = fwrite ( (void*)(bzf->buf), sizeof(UChar),
1022 n, bzf->handle );
1023 if (n != n2 || ferror(bzf->handle))
1024 { BZ_SETERR(BZ_IO_ERROR); return; };
1027 if (bzf->strm.avail_in == 0)
1028 { BZ_SETERR(BZ_OK); return; };
1033 /*---------------------------------------------------*/
1034 void BZ_API(BZ2_bzWriteClose)
1035 ( int* bzerror,
1036 BZFILE* b,
1037 int abandon,
1038 unsigned int* nbytes_in,
1039 unsigned int* nbytes_out )
1041 BZ2_bzWriteClose64 ( bzerror, b, abandon,
1042 nbytes_in, NULL, nbytes_out, NULL );
1046 void BZ_API(BZ2_bzWriteClose64)
1047 ( int* bzerror,
1048 BZFILE* b,
1049 int abandon,
1050 unsigned int* nbytes_in_lo32,
1051 unsigned int* nbytes_in_hi32,
1052 unsigned int* nbytes_out_lo32,
1053 unsigned int* nbytes_out_hi32 )
1055 Int32 n, n2, ret;
1056 bzFile* bzf = (bzFile*)b;
1058 if (bzf == NULL)
1059 { BZ_SETERR(BZ_OK); return; };
1060 if (!(bzf->writing))
1061 { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
1062 if (ferror(bzf->handle))
1063 { BZ_SETERR(BZ_IO_ERROR); return; };
1065 if (nbytes_in_lo32 != NULL) *nbytes_in_lo32 = 0;
1066 if (nbytes_in_hi32 != NULL) *nbytes_in_hi32 = 0;
1067 if (nbytes_out_lo32 != NULL) *nbytes_out_lo32 = 0;
1068 if (nbytes_out_hi32 != NULL) *nbytes_out_hi32 = 0;
1070 if ((!abandon) && bzf->lastErr == BZ_OK) {
1071 while (True) {
1072 bzf->strm.avail_out = BZ_MAX_UNUSED;
1073 bzf->strm.next_out = bzf->buf;
1074 ret = BZ2_bzCompress ( &(bzf->strm), BZ_FINISH );
1075 if (ret != BZ_FINISH_OK && ret != BZ_STREAM_END)
1076 { BZ_SETERR(ret); return; };
1078 if (bzf->strm.avail_out < BZ_MAX_UNUSED) {
1079 n = BZ_MAX_UNUSED - bzf->strm.avail_out;
1080 n2 = fwrite ( (void*)(bzf->buf), sizeof(UChar),
1081 n, bzf->handle );
1082 if (n != n2 || ferror(bzf->handle))
1083 { BZ_SETERR(BZ_IO_ERROR); return; };
1086 if (ret == BZ_STREAM_END) break;
1090 if ( !abandon && !ferror ( bzf->handle ) ) {
1091 fflush ( bzf->handle );
1092 if (ferror(bzf->handle))
1093 { BZ_SETERR(BZ_IO_ERROR); return; };
1096 if (nbytes_in_lo32 != NULL)
1097 *nbytes_in_lo32 = bzf->strm.total_in_lo32;
1098 if (nbytes_in_hi32 != NULL)
1099 *nbytes_in_hi32 = bzf->strm.total_in_hi32;
1100 if (nbytes_out_lo32 != NULL)
1101 *nbytes_out_lo32 = bzf->strm.total_out_lo32;
1102 if (nbytes_out_hi32 != NULL)
1103 *nbytes_out_hi32 = bzf->strm.total_out_hi32;
1105 BZ_SETERR(BZ_OK);
1106 BZ2_bzCompressEnd ( &(bzf->strm) );
1107 free ( bzf );
1111 /*---------------------------------------------------*/
1112 BZFILE* BZ_API(BZ2_bzReadOpen)
1113 ( int* bzerror,
1114 FILE* f,
1115 int verbosity,
1116 int small,
1117 void* unused,
1118 int nUnused )
1120 bzFile* bzf = NULL;
1121 int ret;
1123 BZ_SETERR(BZ_OK);
1125 if (f == NULL ||
1126 (small != 0 && small != 1) ||
1127 (verbosity < 0 || verbosity > 4) ||
1128 (unused == NULL && nUnused != 0) ||
1129 (unused != NULL && (nUnused < 0 || nUnused > BZ_MAX_UNUSED)))
1130 { BZ_SETERR(BZ_PARAM_ERROR); return NULL; };
1132 if (ferror(f))
1133 { BZ_SETERR(BZ_IO_ERROR); return NULL; };
1135 bzf = malloc ( sizeof(bzFile) );
1136 if (bzf == NULL)
1137 { BZ_SETERR(BZ_MEM_ERROR); return NULL; };
1139 BZ_SETERR(BZ_OK);
1141 bzf->initialisedOk = False;
1142 bzf->handle = f;
1143 bzf->bufN = 0;
1144 bzf->writing = False;
1145 bzf->strm.bzalloc = NULL;
1146 bzf->strm.bzfree = NULL;
1147 bzf->strm.opaque = NULL;
1149 while (nUnused > 0) {
1150 bzf->buf[bzf->bufN] = *((UChar*)(unused)); bzf->bufN++;
1151 unused = ((void*)( 1 + ((UChar*)(unused)) ));
1152 nUnused--;
1155 ret = BZ2_bzDecompressInit ( &(bzf->strm), verbosity, small );
1156 if (ret != BZ_OK)
1157 { BZ_SETERR(ret); free(bzf); return NULL; };
1159 bzf->strm.avail_in = bzf->bufN;
1160 bzf->strm.next_in = bzf->buf;
1162 bzf->initialisedOk = True;
1163 return bzf;
1167 /*---------------------------------------------------*/
1168 void BZ_API(BZ2_bzReadClose) ( int *bzerror, BZFILE *b )
1170 bzFile* bzf = (bzFile*)b;
1172 BZ_SETERR(BZ_OK);
1173 if (bzf == NULL)
1174 { BZ_SETERR(BZ_OK); return; };
1176 if (bzf->writing)
1177 { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
1179 if (bzf->initialisedOk)
1180 (void)BZ2_bzDecompressEnd ( &(bzf->strm) );
1181 free ( bzf );
1185 /*---------------------------------------------------*/
1186 int BZ_API(BZ2_bzRead)
1187 ( int* bzerror,
1188 BZFILE* b,
1189 void* buf,
1190 int len )
1192 Int32 n, ret;
1193 bzFile* bzf = (bzFile*)b;
1195 BZ_SETERR(BZ_OK);
1197 if (bzf == NULL || buf == NULL || len < 0)
1198 { BZ_SETERR(BZ_PARAM_ERROR); return 0; };
1200 if (bzf->writing)
1201 { BZ_SETERR(BZ_SEQUENCE_ERROR); return 0; };
1203 if (len == 0)
1204 { BZ_SETERR(BZ_OK); return 0; };
1206 bzf->strm.avail_out = len;
1207 bzf->strm.next_out = buf;
1209 while (True) {
1211 if (ferror(bzf->handle))
1212 { BZ_SETERR(BZ_IO_ERROR); return 0; };
1214 if (bzf->strm.avail_in == 0 && !myfeof(bzf->handle)) {
1215 n = fread ( bzf->buf, sizeof(UChar),
1216 BZ_MAX_UNUSED, bzf->handle );
1217 if (ferror(bzf->handle))
1218 { BZ_SETERR(BZ_IO_ERROR); return 0; };
1219 bzf->bufN = n;
1220 bzf->strm.avail_in = bzf->bufN;
1221 bzf->strm.next_in = bzf->buf;
1224 ret = BZ2_bzDecompress ( &(bzf->strm) );
1226 if (ret != BZ_OK && ret != BZ_STREAM_END)
1227 { BZ_SETERR(ret); return 0; };
1229 if (ret == BZ_OK && myfeof(bzf->handle) &&
1230 bzf->strm.avail_in == 0 && bzf->strm.avail_out > 0)
1231 { BZ_SETERR(BZ_UNEXPECTED_EOF); return 0; };
1233 if (ret == BZ_STREAM_END)
1234 { BZ_SETERR(BZ_STREAM_END);
1235 return len - bzf->strm.avail_out; };
1236 if (bzf->strm.avail_out == 0)
1237 { BZ_SETERR(BZ_OK); return len; };
1241 return 0; /*not reached*/
1245 /*---------------------------------------------------*/
1246 void BZ_API(BZ2_bzReadGetUnused)
1247 ( int* bzerror,
1248 BZFILE* b,
1249 void** unused,
1250 int* nUnused )
1252 bzFile* bzf = (bzFile*)b;
1253 if (bzf == NULL)
1254 { BZ_SETERR(BZ_PARAM_ERROR); return; };
1255 if (bzf->lastErr != BZ_STREAM_END)
1256 { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
1257 if (unused == NULL || nUnused == NULL)
1258 { BZ_SETERR(BZ_PARAM_ERROR); return; };
1260 BZ_SETERR(BZ_OK);
1261 *nUnused = bzf->strm.avail_in;
1262 *unused = bzf->strm.next_in;
1264 #endif
1267 /*---------------------------------------------------*/
1268 /*--- Misc convenience stuff ---*/
1269 /*---------------------------------------------------*/
1271 /*---------------------------------------------------*/
1272 int BZ_API(BZ2_bzBuffToBuffCompress)
1273 ( char* dest,
1274 unsigned int* destLen,
1275 char* source,
1276 unsigned int sourceLen,
1277 int blockSize100k,
1278 int verbosity,
1279 int workFactor )
1281 bz_stream strm;
1282 int ret;
1284 if (dest == NULL || destLen == NULL ||
1285 source == NULL ||
1286 blockSize100k < 1 || blockSize100k > 9 ||
1287 verbosity < 0 || verbosity > 4 ||
1288 workFactor < 0 || workFactor > 250)
1289 return BZ_PARAM_ERROR;
1291 if (workFactor == 0) workFactor = 30;
1292 strm.bzalloc = NULL;
1293 strm.bzfree = NULL;
1294 strm.opaque = NULL;
1295 ret = BZ2_bzCompressInit ( &strm, blockSize100k,
1296 verbosity, workFactor );
1297 if (ret != BZ_OK) return ret;
1299 strm.next_in = source;
1300 strm.next_out = dest;
1301 strm.avail_in = sourceLen;
1302 strm.avail_out = *destLen;
1304 ret = BZ2_bzCompress ( &strm, BZ_FINISH );
1305 if (ret == BZ_FINISH_OK) goto output_overflow;
1306 if (ret != BZ_STREAM_END) goto errhandler;
1308 /* normal termination */
1309 *destLen -= strm.avail_out;
1310 BZ2_bzCompressEnd ( &strm );
1311 return BZ_OK;
1313 output_overflow:
1314 BZ2_bzCompressEnd ( &strm );
1315 return BZ_OUTBUFF_FULL;
1317 errhandler:
1318 BZ2_bzCompressEnd ( &strm );
1319 return ret;
1323 /*---------------------------------------------------*/
1324 int BZ_API(BZ2_bzBuffToBuffDecompress)
1325 ( char* dest,
1326 unsigned int* destLen,
1327 char* source,
1328 unsigned int sourceLen,
1329 int small,
1330 int verbosity )
1332 bz_stream strm;
1333 int ret;
1335 if (dest == NULL || destLen == NULL ||
1336 source == NULL ||
1337 (small != 0 && small != 1) ||
1338 verbosity < 0 || verbosity > 4)
1339 return BZ_PARAM_ERROR;
1341 strm.bzalloc = NULL;
1342 strm.bzfree = NULL;
1343 strm.opaque = NULL;
1344 ret = BZ2_bzDecompressInit ( &strm, verbosity, small );
1345 if (ret != BZ_OK) return ret;
1347 strm.next_in = source;
1348 strm.next_out = dest;
1349 strm.avail_in = sourceLen;
1350 strm.avail_out = *destLen;
1352 ret = BZ2_bzDecompress ( &strm );
1353 if (ret == BZ_OK) goto output_overflow_or_eof;
1354 if (ret != BZ_STREAM_END) goto errhandler;
1356 /* normal termination */
1357 *destLen -= strm.avail_out;
1358 BZ2_bzDecompressEnd ( &strm );
1359 return BZ_OK;
1361 output_overflow_or_eof:
1362 if (strm.avail_out > 0) {
1363 BZ2_bzDecompressEnd ( &strm );
1364 return BZ_UNEXPECTED_EOF;
1365 } else {
1366 BZ2_bzDecompressEnd ( &strm );
1367 return BZ_OUTBUFF_FULL;
1370 errhandler:
1371 BZ2_bzDecompressEnd ( &strm );
1372 return ret;
1376 /*---------------------------------------------------*/
1377 /*--
1378 Code contributed by Yoshioka Tsuneo
1379 (QWF00133@niftyserve.or.jp/tsuneo-y@is.aist-nara.ac.jp),
1380 to support better zlib compatibility.
1381 This code is not _officially_ part of libbzip2 (yet);
1382 I haven't tested it, documented it, or considered the
1383 threading-safeness of it.
1384 If this code breaks, please contact both Yoshioka and me.
1385 --*/
1386 /*---------------------------------------------------*/
1388 /*---------------------------------------------------*/
1389 /*--
1390 return version like "0.9.0c".
1391 --*/
1392 const char * BZ_API(BZ2_bzlibVersion)(void)
1394 return BZ_VERSION;
1398 #ifndef BZ_NO_STDIO
1399 /*---------------------------------------------------*/
1401 #if defined(_WIN32) || defined(OS2) || defined(MSDOS)
1402 # include <fcntl.h>
1403 # include <io.h>
1404 # define SET_BINARY_MODE(file) setmode(fileno(file),O_BINARY)
1405 #else
1406 # define SET_BINARY_MODE(file)
1407 #endif
1408 static
1409 BZFILE * bzopen_or_bzdopen
1410 ( const char *path, /* no use when bzdopen */
1411 int fd, /* no use when bzdopen */
1412 const char *mode,
1413 int open_mode) /* bzopen: 0, bzdopen:1 */
1415 int bzerr;
1416 char unused[BZ_MAX_UNUSED];
1417 int blockSize100k = 9;
1418 int writing = 0;
1419 char mode2[10] = "";
1420 FILE *fp = NULL;
1421 BZFILE *bzfp = NULL;
1422 int verbosity = 0;
1423 int workFactor = 30;
1424 int smallMode = 0;
1425 int nUnused = 0;
1427 if (mode == NULL) return NULL;
1428 while (*mode) {
1429 switch (*mode) {
1430 case 'r':
1431 writing = 0; break;
1432 case 'w':
1433 writing = 1; break;
1434 case 's':
1435 smallMode = 1; break;
1436 default:
1437 if (isdigit((int)(*mode))) {
1438 blockSize100k = *mode-BZ_HDR_0;
1441 mode++;
1443 strcat(mode2, writing ? "w" : "r" );
1444 strcat(mode2,"b"); /* binary mode */
1446 if (open_mode==0) {
1447 if (path==NULL || strcmp(path,"")==0) {
1448 fp = (writing ? stdout : stdin);
1449 SET_BINARY_MODE(fp);
1450 } else {
1451 fp = fopen(path,mode2);
1453 } else {
1454 #ifdef BZ_STRICT_ANSI
1455 fp = NULL;
1456 #else
1457 fp = fdopen(fd,mode2);
1458 #endif
1460 if (fp == NULL) return NULL;
1462 if (writing) {
1463 /* Guard against total chaos and anarchy -- JRS */
1464 if (blockSize100k < 1) blockSize100k = 1;
1465 if (blockSize100k > 9) blockSize100k = 9;
1466 bzfp = BZ2_bzWriteOpen(&bzerr,fp,blockSize100k,
1467 verbosity,workFactor);
1468 } else {
1469 bzfp = BZ2_bzReadOpen(&bzerr,fp,verbosity,smallMode,
1470 unused,nUnused);
1472 if (bzfp == NULL) {
1473 if (fp != stdin && fp != stdout) fclose(fp);
1474 return NULL;
1476 return bzfp;
1480 /*---------------------------------------------------*/
1481 /*--
1482 open file for read or write.
1483 ex) bzopen("file","w9")
1484 case path="" or NULL => use stdin or stdout.
1485 --*/
1486 BZFILE * BZ_API(BZ2_bzopen)
1487 ( const char *path,
1488 const char *mode )
1490 return bzopen_or_bzdopen(path,-1,mode,/*bzopen*/0);
1494 /*---------------------------------------------------*/
1495 BZFILE * BZ_API(BZ2_bzdopen)
1496 ( int fd,
1497 const char *mode )
1499 return bzopen_or_bzdopen(NULL,fd,mode,/*bzdopen*/1);
1503 /*---------------------------------------------------*/
1504 int BZ_API(BZ2_bzread) (BZFILE* b, void* buf, int len )
1506 int bzerr, nread;
1507 if (((bzFile*)b)->lastErr == BZ_STREAM_END) return 0;
1508 nread = BZ2_bzRead(&bzerr,b,buf,len);
1509 if (bzerr == BZ_OK || bzerr == BZ_STREAM_END) {
1510 return nread;
1511 } else {
1512 return -1;
1517 /*---------------------------------------------------*/
1518 int BZ_API(BZ2_bzwrite) (BZFILE* b, void* buf, int len )
1520 int bzerr;
1522 BZ2_bzWrite(&bzerr,b,buf,len);
1523 if(bzerr == BZ_OK){
1524 return len;
1525 }else{
1526 return -1;
1531 /*---------------------------------------------------*/
1532 int BZ_API(BZ2_bzflush) (BZFILE *b)
1534 /* do nothing now... */
1535 return 0;
1539 /*---------------------------------------------------*/
1540 void BZ_API(BZ2_bzclose) (BZFILE* b)
1542 int bzerr;
1543 FILE *fp = ((bzFile *)b)->handle;
1545 if (b==NULL) {return;}
1546 if(((bzFile*)b)->writing){
1547 BZ2_bzWriteClose(&bzerr,b,0,NULL,NULL);
1548 if(bzerr != BZ_OK){
1549 BZ2_bzWriteClose(NULL,b,1,NULL,NULL);
1551 }else{
1552 BZ2_bzReadClose(&bzerr,b);
1554 if(fp!=stdin && fp!=stdout){
1555 fclose(fp);
1560 /*---------------------------------------------------*/
1561 /*--
1562 return last error code
1563 --*/
1564 static const char *bzerrorstrings[] = {
1565 "OK"
1566 ,"SEQUENCE_ERROR"
1567 ,"PARAM_ERROR"
1568 ,"MEM_ERROR"
1569 ,"DATA_ERROR"
1570 ,"DATA_ERROR_MAGIC"
1571 ,"IO_ERROR"
1572 ,"UNEXPECTED_EOF"
1573 ,"OUTBUFF_FULL"
1574 ,"CONFIG_ERROR"
1575 ,"???" /* for future */
1576 ,"???" /* for future */
1577 ,"???" /* for future */
1578 ,"???" /* for future */
1579 ,"???" /* for future */
1580 ,"???" /* for future */
1584 const char * BZ_API(BZ2_bzerror) (BZFILE *b, int *errnum)
1586 int err = ((bzFile *)b)->lastErr;
1588 if(err>0) err = 0;
1589 *errnum = err;
1590 return bzerrorstrings[err*-1];
1592 #endif
1595 /*-------------------------------------------------------------*/
1596 /*--- end bzlib.c ---*/
1597 /*-------------------------------------------------------------*/