2 /*-------------------------------------------------------------*/
3 /*--- Library top-level functions. ---*/
5 /*-------------------------------------------------------------*/
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-2000 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
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
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.
46 bzip2/libbzip2 version 1.0 of 21 March 2000
48 This program is based on (at least) the work of:
58 For more information on these sources, see the manual.
64 0.9.0 -- original version.
66 0.9.0a/b -- no changes in this file.
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.
76 #ifdef BZ_DECOMPRESS_ONLY
78 #include <ddk/ntddk.h>
82 #include "bzlib_private.h"
85 /*---------------------------------------------------*/
86 /*--- Compression stuff ---*/
87 /*---------------------------------------------------*/
90 /*---------------------------------------------------*/
92 void BZ2_bz__AssertH__fail ( int errcode
)
95 "\n\nbzip2/libbzip2: internal error number %d.\n"
96 "This is a bug in bzip2/libbzip2, %s.\n"
97 "Please report it to me at: jseward@acm.org. If this happened\n"
98 "when you were using some program which uses libbzip2 as a\n"
99 "component, you should also report this bug to the author(s)\n"
100 "of that program. Please make an effort to report this bug;\n"
101 "timely and accurate bug reports eventually lead to higher\n"
102 "quality software. Thanks. Julian Seward, 21 March 2000.\n\n",
111 /*---------------------------------------------------*/
113 int bz_config_ok ( void )
115 if (sizeof(int) != 4) return 0;
116 if (sizeof(short) != 2) return 0;
117 if (sizeof(char) != 1) return 0;
121 /*---------------------------------------------------*/
123 void* default_bzalloc ( void* opaque
, Int32 items
, Int32 size
)
125 return ExAllocatePool( PagedPool
, items
* size
);
129 void default_bzfree ( void* opaque
, void* addr
)
134 #ifndef BZ_DECOMPRESS_ONLY
136 /*---------------------------------------------------*/
138 void prepare_new_block ( EState
* s
)
143 s
->state_out_pos
= 0;
144 BZ_INITIALISE_CRC ( s
->blockCRC
);
145 for (i
= 0; i
< 256; i
++) s
->inUse
[i
] = False
;
150 /*---------------------------------------------------*/
152 void init_RL ( EState
* s
)
154 s
->state_in_ch
= 256;
160 Bool
isempty_RL ( EState
* s
)
162 if (s
->state_in_ch
< 256 && s
->state_in_len
> 0)
168 /*---------------------------------------------------*/
169 int BZ_API(BZ2_bzCompressInit
)
178 if (!bz_config_ok()) return BZ_CONFIG_ERROR
;
181 blockSize100k
< 1 || blockSize100k
> 9 ||
182 workFactor
< 0 || workFactor
> 250)
183 return BZ_PARAM_ERROR
;
185 if (workFactor
== 0) workFactor
= 30;
186 if (strm
->bzalloc
== NULL
) strm
->bzalloc
= default_bzalloc
;
187 if (strm
->bzfree
== NULL
) strm
->bzfree
= default_bzfree
;
189 s
= BZALLOC( sizeof(EState
) );
190 if (s
== NULL
) return BZ_MEM_ERROR
;
197 n
= 100000 * blockSize100k
;
198 s
->arr1
= BZALLOC( n
* sizeof(UInt32
) );
199 s
->arr2
= BZALLOC( (n
+BZ_N_OVERSHOOT
) * sizeof(UInt32
) );
200 s
->ftab
= BZALLOC( 65537 * sizeof(UInt32
) );
202 if (s
->arr1
== NULL
|| s
->arr2
== NULL
|| s
->ftab
== NULL
) {
203 if (s
->arr1
!= NULL
) BZFREE(s
->arr1
);
204 if (s
->arr2
!= NULL
) BZFREE(s
->arr2
);
205 if (s
->ftab
!= NULL
) BZFREE(s
->ftab
);
206 if (s
!= NULL
) BZFREE(s
);
211 s
->state
= BZ_S_INPUT
;
212 s
->mode
= BZ_M_RUNNING
;
214 s
->blockSize100k
= blockSize100k
;
215 s
->nblockMAX
= 100000 * blockSize100k
- 19;
216 s
->verbosity
= verbosity
;
217 s
->workFactor
= workFactor
;
219 s
->block
= (UChar
*)s
->arr2
;
220 s
->mtfv
= (UInt16
*)s
->arr1
;
222 s
->ptr
= (UInt32
*)s
->arr1
;
225 strm
->total_in_lo32
= 0;
226 strm
->total_in_hi32
= 0;
227 strm
->total_out_lo32
= 0;
228 strm
->total_out_hi32
= 0;
230 prepare_new_block ( s
);
235 /*---------------------------------------------------*/
237 void add_pair_to_block ( EState
* s
)
240 UChar ch
= (UChar
)(s
->state_in_ch
);
241 for (i
= 0; i
< s
->state_in_len
; i
++) {
242 BZ_UPDATE_CRC( s
->blockCRC
, ch
);
244 s
->inUse
[s
->state_in_ch
] = True
;
245 switch (s
->state_in_len
) {
247 s
->block
[s
->nblock
] = (UChar
)ch
; s
->nblock
++;
250 s
->block
[s
->nblock
] = (UChar
)ch
; s
->nblock
++;
251 s
->block
[s
->nblock
] = (UChar
)ch
; s
->nblock
++;
254 s
->block
[s
->nblock
] = (UChar
)ch
; s
->nblock
++;
255 s
->block
[s
->nblock
] = (UChar
)ch
; s
->nblock
++;
256 s
->block
[s
->nblock
] = (UChar
)ch
; s
->nblock
++;
259 s
->inUse
[s
->state_in_len
-4] = True
;
260 s
->block
[s
->nblock
] = (UChar
)ch
; s
->nblock
++;
261 s
->block
[s
->nblock
] = (UChar
)ch
; s
->nblock
++;
262 s
->block
[s
->nblock
] = (UChar
)ch
; s
->nblock
++;
263 s
->block
[s
->nblock
] = (UChar
)ch
; s
->nblock
++;
264 s
->block
[s
->nblock
] = ((UChar
)(s
->state_in_len
-4));
271 /*---------------------------------------------------*/
273 void flush_RL ( EState
* s
)
275 if (s
->state_in_ch
< 256) add_pair_to_block ( s
);
280 /*---------------------------------------------------*/
281 #define ADD_CHAR_TO_BLOCK(zs,zchh0) \
283 UInt32 zchh = (UInt32)(zchh0); \
284 /*-- fast track the common case --*/ \
285 if (zchh != zs->state_in_ch && \
286 zs->state_in_len == 1) { \
287 UChar ch = (UChar)(zs->state_in_ch); \
288 BZ_UPDATE_CRC( zs->blockCRC, ch ); \
289 zs->inUse[zs->state_in_ch] = True; \
290 zs->block[zs->nblock] = (UChar)ch; \
292 zs->state_in_ch = zchh; \
295 /*-- general, uncommon cases --*/ \
296 if (zchh != zs->state_in_ch || \
297 zs->state_in_len == 255) { \
298 if (zs->state_in_ch < 256) \
299 add_pair_to_block ( zs ); \
300 zs->state_in_ch = zchh; \
301 zs->state_in_len = 1; \
303 zs->state_in_len++; \
308 /*---------------------------------------------------*/
310 Bool
copy_input_until_stop ( EState
* s
)
312 Bool progress_in
= False
;
314 if (s
->mode
== BZ_M_RUNNING
) {
316 /*-- fast track the common case --*/
318 /*-- block full? --*/
319 if (s
->nblock
>= s
->nblockMAX
) break;
321 if (s
->strm
->avail_in
== 0) break;
323 ADD_CHAR_TO_BLOCK ( s
, (UInt32
)(*((UChar
*)(s
->strm
->next_in
))) );
326 s
->strm
->total_in_lo32
++;
327 if (s
->strm
->total_in_lo32
== 0) s
->strm
->total_in_hi32
++;
332 /*-- general, uncommon case --*/
334 /*-- block full? --*/
335 if (s
->nblock
>= s
->nblockMAX
) break;
337 if (s
->strm
->avail_in
== 0) break;
338 /*-- flush/finish end? --*/
339 if (s
->avail_in_expect
== 0) break;
341 ADD_CHAR_TO_BLOCK ( s
, (UInt32
)(*((UChar
*)(s
->strm
->next_in
))) );
344 s
->strm
->total_in_lo32
++;
345 if (s
->strm
->total_in_lo32
== 0) s
->strm
->total_in_hi32
++;
346 s
->avail_in_expect
--;
353 /*---------------------------------------------------*/
355 Bool
copy_output_until_stop ( EState
* s
)
357 Bool progress_out
= False
;
361 /*-- no output space? --*/
362 if (s
->strm
->avail_out
== 0) break;
364 /*-- block done? --*/
365 if (s
->state_out_pos
>= s
->numZ
) break;
368 *(s
->strm
->next_out
) = s
->zbits
[s
->state_out_pos
];
370 s
->strm
->avail_out
--;
372 s
->strm
->total_out_lo32
++;
373 if (s
->strm
->total_out_lo32
== 0) s
->strm
->total_out_hi32
++;
380 /*---------------------------------------------------*/
382 Bool
handle_compress ( bz_stream
* strm
)
384 Bool progress_in
= False
;
385 Bool progress_out
= False
;
386 EState
* s
= strm
->state
;
390 if (s
->state
== BZ_S_OUTPUT
) {
391 progress_out
|= copy_output_until_stop ( s
);
392 if (s
->state_out_pos
< s
->numZ
) break;
393 if (s
->mode
== BZ_M_FINISHING
&&
394 s
->avail_in_expect
== 0 &&
395 isempty_RL(s
)) break;
396 prepare_new_block ( s
);
397 s
->state
= BZ_S_INPUT
;
398 if (s
->mode
== BZ_M_FLUSHING
&&
399 s
->avail_in_expect
== 0 &&
400 isempty_RL(s
)) break;
403 if (s
->state
== BZ_S_INPUT
) {
404 progress_in
|= copy_input_until_stop ( s
);
405 if (s
->mode
!= BZ_M_RUNNING
&& s
->avail_in_expect
== 0) {
407 BZ2_compressBlock ( s
, (Bool
)(s
->mode
== BZ_M_FINISHING
) );
408 s
->state
= BZ_S_OUTPUT
;
411 if (s
->nblock
>= s
->nblockMAX
) {
412 BZ2_compressBlock ( s
, False
);
413 s
->state
= BZ_S_OUTPUT
;
416 if (s
->strm
->avail_in
== 0) {
423 return progress_in
|| progress_out
;
427 /*---------------------------------------------------*/
428 int BZ_API(BZ2_bzCompress
) ( bz_stream
*strm
, int action
)
432 if (strm
== NULL
) return BZ_PARAM_ERROR
;
434 if (s
== NULL
) return BZ_PARAM_ERROR
;
435 if (s
->strm
!= strm
) return BZ_PARAM_ERROR
;
441 return BZ_SEQUENCE_ERROR
;
444 if (action
== BZ_RUN
) {
445 progress
= handle_compress ( strm
);
446 return progress
? BZ_RUN_OK
: BZ_PARAM_ERROR
;
449 if (action
== BZ_FLUSH
) {
450 s
->avail_in_expect
= strm
->avail_in
;
451 s
->mode
= BZ_M_FLUSHING
;
455 if (action
== BZ_FINISH
) {
456 s
->avail_in_expect
= strm
->avail_in
;
457 s
->mode
= BZ_M_FINISHING
;
461 return BZ_PARAM_ERROR
;
464 if (action
!= BZ_FLUSH
) return BZ_SEQUENCE_ERROR
;
465 if (s
->avail_in_expect
!= s
->strm
->avail_in
)
466 return BZ_SEQUENCE_ERROR
;
467 progress
= handle_compress ( strm
);
468 if (s
->avail_in_expect
> 0 || !isempty_RL(s
) ||
469 s
->state_out_pos
< s
->numZ
) return BZ_FLUSH_OK
;
470 s
->mode
= BZ_M_RUNNING
;
474 if (action
!= BZ_FINISH
) return BZ_SEQUENCE_ERROR
;
475 if (s
->avail_in_expect
!= s
->strm
->avail_in
)
476 return BZ_SEQUENCE_ERROR
;
477 progress
= handle_compress ( strm
);
478 if (!progress
) return BZ_SEQUENCE_ERROR
;
479 if (s
->avail_in_expect
> 0 || !isempty_RL(s
) ||
480 s
->state_out_pos
< s
->numZ
) return BZ_FINISH_OK
;
482 return BZ_STREAM_END
;
484 return BZ_OK
; /*--not reached--*/
488 /*---------------------------------------------------*/
489 int BZ_API(BZ2_bzCompressEnd
) ( bz_stream
*strm
)
492 if (strm
== NULL
) return BZ_PARAM_ERROR
;
494 if (s
== NULL
) return BZ_PARAM_ERROR
;
495 if (s
->strm
!= strm
) return BZ_PARAM_ERROR
;
497 if (s
->arr1
!= NULL
) BZFREE(s
->arr1
);
498 if (s
->arr2
!= NULL
) BZFREE(s
->arr2
);
499 if (s
->ftab
!= NULL
) BZFREE(s
->ftab
);
507 #endif // BZ_DECOMPRESS_ONLY
509 /*---------------------------------------------------*/
510 /*--- Decompression stuff ---*/
511 /*---------------------------------------------------*/
513 /*---------------------------------------------------*/
514 int BZ_API(BZ2_bzDecompressInit
)
521 if (!bz_config_ok()) return BZ_CONFIG_ERROR
;
523 if (strm
== NULL
) return BZ_PARAM_ERROR
;
524 if (small
!= 0 && small
!= 1) return BZ_PARAM_ERROR
;
525 if (verbosity
< 0 || verbosity
> 4) return BZ_PARAM_ERROR
;
527 if (strm
->bzalloc
== NULL
) strm
->bzalloc
= default_bzalloc
;
528 if (strm
->bzfree
== NULL
) strm
->bzfree
= default_bzfree
;
530 s
= BZALLOC( sizeof(DState
) );
531 if (s
== NULL
) return BZ_MEM_ERROR
;
534 s
->state
= BZ_X_MAGIC_1
;
537 s
->calculatedCombinedCRC
= 0;
538 strm
->total_in_lo32
= 0;
539 strm
->total_in_hi32
= 0;
540 strm
->total_out_lo32
= 0;
541 strm
->total_out_hi32
= 0;
542 s
->smallDecompress
= (Bool
)small
;
547 s
->verbosity
= verbosity
;
552 #ifndef BZ_DECOMPRESS_ONLY
554 /*---------------------------------------------------*/
556 void unRLE_obuf_to_output_FAST ( DState
* s
)
560 if (s
->blockRandomised
) {
563 /* try to finish existing run */
565 if (s
->strm
->avail_out
== 0) return;
566 if (s
->state_out_len
== 0) break;
567 *( (UChar
*)(s
->strm
->next_out
) ) = s
->state_out_ch
;
568 BZ_UPDATE_CRC ( s
->calculatedBlockCRC
, s
->state_out_ch
);
571 s
->strm
->avail_out
--;
572 s
->strm
->total_out_lo32
++;
573 if (s
->strm
->total_out_lo32
== 0) s
->strm
->total_out_hi32
++;
576 /* can a new run be started? */
577 if (s
->nblock_used
== s
->save_nblock
+1) return;
580 s
->state_out_len
= 1;
581 s
->state_out_ch
= s
->k0
;
582 BZ_GET_FAST(k1
); BZ_RAND_UPD_MASK
;
583 k1
^= BZ_RAND_MASK
; s
->nblock_used
++;
584 if (s
->nblock_used
== s
->save_nblock
+1) continue;
585 if (k1
!= s
->k0
) { s
->k0
= k1
; continue; };
587 s
->state_out_len
= 2;
588 BZ_GET_FAST(k1
); BZ_RAND_UPD_MASK
;
589 k1
^= BZ_RAND_MASK
; s
->nblock_used
++;
590 if (s
->nblock_used
== s
->save_nblock
+1) continue;
591 if (k1
!= s
->k0
) { s
->k0
= k1
; continue; };
593 s
->state_out_len
= 3;
594 BZ_GET_FAST(k1
); BZ_RAND_UPD_MASK
;
595 k1
^= BZ_RAND_MASK
; s
->nblock_used
++;
596 if (s
->nblock_used
== s
->save_nblock
+1) continue;
597 if (k1
!= s
->k0
) { s
->k0
= k1
; continue; };
599 BZ_GET_FAST(k1
); BZ_RAND_UPD_MASK
;
600 k1
^= BZ_RAND_MASK
; s
->nblock_used
++;
601 s
->state_out_len
= ((Int32
)k1
) + 4;
602 BZ_GET_FAST(s
->k0
); BZ_RAND_UPD_MASK
;
603 s
->k0
^= BZ_RAND_MASK
; s
->nblock_used
++;
609 UInt32 c_calculatedBlockCRC
= s
->calculatedBlockCRC
;
610 UChar c_state_out_ch
= s
->state_out_ch
;
611 Int32 c_state_out_len
= s
->state_out_len
;
612 Int32 c_nblock_used
= s
->nblock_used
;
614 UInt32
* c_tt
= s
->tt
;
615 UInt32 c_tPos
= s
->tPos
;
616 char* cs_next_out
= s
->strm
->next_out
;
617 unsigned int cs_avail_out
= s
->strm
->avail_out
;
620 UInt32 avail_out_INIT
= cs_avail_out
;
621 Int32 s_save_nblockPP
= s
->save_nblock
+1;
622 unsigned int total_out_lo32_old
;
626 /* try to finish existing run */
627 if (c_state_out_len
> 0) {
629 if (cs_avail_out
== 0) goto return_notr
;
630 if (c_state_out_len
== 1) break;
631 *( (UChar
*)(cs_next_out
) ) = c_state_out_ch
;
632 BZ_UPDATE_CRC ( c_calculatedBlockCRC
, c_state_out_ch
);
637 s_state_out_len_eq_one
:
639 if (cs_avail_out
== 0) {
640 c_state_out_len
= 1; goto return_notr
;
642 *( (UChar
*)(cs_next_out
) ) = c_state_out_ch
;
643 BZ_UPDATE_CRC ( c_calculatedBlockCRC
, c_state_out_ch
);
648 /* can a new run be started? */
649 if (c_nblock_used
== s_save_nblockPP
) {
650 c_state_out_len
= 0; goto return_notr
;
652 c_state_out_ch
= c_k0
;
653 BZ_GET_FAST_C(k1
); c_nblock_used
++;
655 c_k0
= k1
; goto s_state_out_len_eq_one
;
657 if (c_nblock_used
== s_save_nblockPP
)
658 goto s_state_out_len_eq_one
;
661 BZ_GET_FAST_C(k1
); c_nblock_used
++;
662 if (c_nblock_used
== s_save_nblockPP
) continue;
663 if (k1
!= c_k0
) { c_k0
= k1
; continue; };
666 BZ_GET_FAST_C(k1
); c_nblock_used
++;
667 if (c_nblock_used
== s_save_nblockPP
) continue;
668 if (k1
!= c_k0
) { c_k0
= k1
; continue; };
670 BZ_GET_FAST_C(k1
); c_nblock_used
++;
671 c_state_out_len
= ((Int32
)k1
) + 4;
672 BZ_GET_FAST_C(c_k0
); c_nblock_used
++;
676 total_out_lo32_old
= s
->strm
->total_out_lo32
;
677 s
->strm
->total_out_lo32
+= (avail_out_INIT
- cs_avail_out
);
678 if (s
->strm
->total_out_lo32
< total_out_lo32_old
)
679 s
->strm
->total_out_hi32
++;
682 s
->calculatedBlockCRC
= c_calculatedBlockCRC
;
683 s
->state_out_ch
= c_state_out_ch
;
684 s
->state_out_len
= c_state_out_len
;
685 s
->nblock_used
= c_nblock_used
;
689 s
->strm
->next_out
= cs_next_out
;
690 s
->strm
->avail_out
= cs_avail_out
;
695 #endif // BZ_DECOMPRESS_ONLY
697 /*---------------------------------------------------*/
698 __inline__ Int32
BZ2_indexIntoF ( Int32 indx
, Int32
*cftab
)
704 mid
= (nb
+ na
) >> 1;
705 if (indx
>= cftab
[mid
]) nb
= mid
; else na
= mid
;
707 while (na
- nb
!= 1);
711 /*---------------------------------------------------*/
713 void unRLE_obuf_to_output_SMALL ( DState
* s
)
717 if (s
->blockRandomised
) {
720 /* try to finish existing run */
722 if (s
->strm
->avail_out
== 0) return;
723 if (s
->state_out_len
== 0) break;
724 *( (UChar
*)(s
->strm
->next_out
) ) = s
->state_out_ch
;
725 BZ_UPDATE_CRC ( s
->calculatedBlockCRC
, s
->state_out_ch
);
728 s
->strm
->avail_out
--;
729 s
->strm
->total_out_lo32
++;
730 if (s
->strm
->total_out_lo32
== 0) s
->strm
->total_out_hi32
++;
733 /* can a new run be started? */
734 if (s
->nblock_used
== s
->save_nblock
+1) return;
737 s
->state_out_len
= 1;
738 s
->state_out_ch
= s
->k0
;
739 BZ_GET_SMALL(k1
); BZ_RAND_UPD_MASK
;
740 k1
^= BZ_RAND_MASK
; s
->nblock_used
++;
741 if (s
->nblock_used
== s
->save_nblock
+1) continue;
742 if (k1
!= s
->k0
) { s
->k0
= k1
; continue; };
744 s
->state_out_len
= 2;
745 BZ_GET_SMALL(k1
); BZ_RAND_UPD_MASK
;
746 k1
^= BZ_RAND_MASK
; s
->nblock_used
++;
747 if (s
->nblock_used
== s
->save_nblock
+1) continue;
748 if (k1
!= s
->k0
) { s
->k0
= k1
; continue; };
750 s
->state_out_len
= 3;
751 BZ_GET_SMALL(k1
); BZ_RAND_UPD_MASK
;
752 k1
^= BZ_RAND_MASK
; s
->nblock_used
++;
753 if (s
->nblock_used
== s
->save_nblock
+1) continue;
754 if (k1
!= s
->k0
) { s
->k0
= k1
; continue; };
756 BZ_GET_SMALL(k1
); BZ_RAND_UPD_MASK
;
757 k1
^= BZ_RAND_MASK
; s
->nblock_used
++;
758 s
->state_out_len
= ((Int32
)k1
) + 4;
759 BZ_GET_SMALL(s
->k0
); BZ_RAND_UPD_MASK
;
760 s
->k0
^= BZ_RAND_MASK
; s
->nblock_used
++;
766 /* try to finish existing run */
768 if (s
->strm
->avail_out
== 0) return;
769 if (s
->state_out_len
== 0) break;
770 *( (UChar
*)(s
->strm
->next_out
) ) = s
->state_out_ch
;
771 BZ_UPDATE_CRC ( s
->calculatedBlockCRC
, s
->state_out_ch
);
774 s
->strm
->avail_out
--;
775 s
->strm
->total_out_lo32
++;
776 if (s
->strm
->total_out_lo32
== 0) s
->strm
->total_out_hi32
++;
779 /* can a new run be started? */
780 if (s
->nblock_used
== s
->save_nblock
+1) return;
782 s
->state_out_len
= 1;
783 s
->state_out_ch
= s
->k0
;
784 BZ_GET_SMALL(k1
); s
->nblock_used
++;
785 if (s
->nblock_used
== s
->save_nblock
+1) continue;
786 if (k1
!= s
->k0
) { s
->k0
= k1
; continue; };
788 s
->state_out_len
= 2;
789 BZ_GET_SMALL(k1
); s
->nblock_used
++;
790 if (s
->nblock_used
== s
->save_nblock
+1) continue;
791 if (k1
!= s
->k0
) { s
->k0
= k1
; continue; };
793 s
->state_out_len
= 3;
794 BZ_GET_SMALL(k1
); s
->nblock_used
++;
795 if (s
->nblock_used
== s
->save_nblock
+1) continue;
796 if (k1
!= s
->k0
) { s
->k0
= k1
; continue; };
798 BZ_GET_SMALL(k1
); s
->nblock_used
++;
799 s
->state_out_len
= ((Int32
)k1
) + 4;
800 BZ_GET_SMALL(s
->k0
); s
->nblock_used
++;
806 /*---------------------------------------------------*/
807 int BZ_API(BZ2_bzDecompress
) ( bz_stream
*strm
)
810 if (strm
== NULL
) return BZ_PARAM_ERROR
;
812 if (s
== NULL
) return BZ_PARAM_ERROR
;
813 if (s
->strm
!= strm
) return BZ_PARAM_ERROR
;
816 if (s
->state
== BZ_X_IDLE
) return BZ_SEQUENCE_ERROR
;
817 if (s
->state
== BZ_X_OUTPUT
) {
818 #ifndef BZ_DECOMPRESS_ONLY
819 if (s
->smallDecompress
)
820 unRLE_obuf_to_output_SMALL ( s
); else
821 unRLE_obuf_to_output_FAST ( s
);
823 unRLE_obuf_to_output_SMALL ( s
);
825 if (s
->nblock_used
== s
->save_nblock
+1 && s
->state_out_len
== 0) {
826 BZ_FINALISE_CRC ( s
->calculatedBlockCRC
);
827 if (s
->verbosity
>= 3)
828 VPrintf2 ( " {0x%x, 0x%x}", s
->storedBlockCRC
,
829 s
->calculatedBlockCRC
);
830 if (s
->verbosity
>= 2) VPrintf0 ( "]" );
831 if (s
->calculatedBlockCRC
!= s
->storedBlockCRC
)
832 return BZ_DATA_ERROR
;
833 s
->calculatedCombinedCRC
834 = (s
->calculatedCombinedCRC
<< 1) |
835 (s
->calculatedCombinedCRC
>> 31);
836 s
->calculatedCombinedCRC
^= s
->calculatedBlockCRC
;
837 s
->state
= BZ_X_BLKHDR_1
;
842 if (s
->state
>= BZ_X_MAGIC_1
) {
843 Int32 r
= BZ2_decompress ( s
);
844 if (r
== BZ_STREAM_END
) {
845 if (s
->verbosity
>= 3)
846 VPrintf2 ( "\n combined CRCs: stored = 0x%x, computed = 0x%x",
847 s
->storedCombinedCRC
, s
->calculatedCombinedCRC
);
848 if (s
->calculatedCombinedCRC
!= s
->storedCombinedCRC
)
849 return BZ_DATA_ERROR
;
852 if (s
->state
!= BZ_X_OUTPUT
) return r
;
858 return 0; /*NOTREACHED*/
862 /*---------------------------------------------------*/
863 int BZ_API(BZ2_bzDecompressEnd
) ( bz_stream
*strm
)
866 if (strm
== NULL
) return BZ_PARAM_ERROR
;
868 if (s
== NULL
) return BZ_PARAM_ERROR
;
869 if (s
->strm
!= strm
) return BZ_PARAM_ERROR
;
871 if (s
->tt
!= NULL
) BZFREE(s
->tt
);
872 if (s
->ll16
!= NULL
) BZFREE(s
->ll16
);
873 if (s
->ll4
!= NULL
) BZFREE(s
->ll4
);
883 /*---------------------------------------------------*/
884 /*--- File I/O stuff ---*/
885 /*---------------------------------------------------*/
887 #define BZ_SETERR(eee) \
889 if (bzerror != NULL) *bzerror = eee; \
890 if (bzf != NULL) bzf->lastErr = eee; \
896 Char buf
[BZ_MAX_UNUSED
];
906 /*---------------------------------------------*/
907 static Bool
myfeof ( FILE* f
)
909 Int32 c
= fgetc ( f
);
910 if (c
== EOF
) return True
;
916 /*---------------------------------------------------*/
917 BZFILE
* BZ_API(BZ2_bzWriteOpen
)
930 (blockSize100k
< 1 || blockSize100k
> 9) ||
931 (workFactor
< 0 || workFactor
> 250) ||
932 (verbosity
< 0 || verbosity
> 4))
933 { BZ_SETERR(BZ_PARAM_ERROR
); return NULL
; };
936 { BZ_SETERR(BZ_IO_ERROR
); return NULL
; };
938 bzf
= malloc ( sizeof(bzFile
) );
940 { BZ_SETERR(BZ_MEM_ERROR
); return NULL
; };
943 bzf
->initialisedOk
= False
;
947 bzf
->strm
.bzalloc
= NULL
;
948 bzf
->strm
.bzfree
= NULL
;
949 bzf
->strm
.opaque
= NULL
;
951 if (workFactor
== 0) workFactor
= 30;
952 ret
= BZ2_bzCompressInit ( &(bzf
->strm
), blockSize100k
,
953 verbosity
, workFactor
);
955 { BZ_SETERR(ret
); free(bzf
); return NULL
; };
957 bzf
->strm
.avail_in
= 0;
958 bzf
->initialisedOk
= True
;
964 /*---------------------------------------------------*/
965 void BZ_API(BZ2_bzWrite
)
972 bzFile
* bzf
= (bzFile
*)b
;
975 if (bzf
== NULL
|| buf
== NULL
|| len
< 0)
976 { BZ_SETERR(BZ_PARAM_ERROR
); return; };
978 { BZ_SETERR(BZ_SEQUENCE_ERROR
); return; };
979 if (ferror(bzf
->handle
))
980 { BZ_SETERR(BZ_IO_ERROR
); return; };
983 { BZ_SETERR(BZ_OK
); return; };
985 bzf
->strm
.avail_in
= len
;
986 bzf
->strm
.next_in
= buf
;
989 bzf
->strm
.avail_out
= BZ_MAX_UNUSED
;
990 bzf
->strm
.next_out
= bzf
->buf
;
991 ret
= BZ2_bzCompress ( &(bzf
->strm
), BZ_RUN
);
992 if (ret
!= BZ_RUN_OK
)
993 { BZ_SETERR(ret
); return; };
995 if (bzf
->strm
.avail_out
< BZ_MAX_UNUSED
) {
996 n
= BZ_MAX_UNUSED
- bzf
->strm
.avail_out
;
997 n2
= fwrite ( (void*)(bzf
->buf
), sizeof(UChar
),
999 if (n
!= n2
|| ferror(bzf
->handle
))
1000 { BZ_SETERR(BZ_IO_ERROR
); return; };
1003 if (bzf
->strm
.avail_in
== 0)
1004 { BZ_SETERR(BZ_OK
); return; };
1009 /*---------------------------------------------------*/
1010 void BZ_API(BZ2_bzWriteClose
)
1014 unsigned int* nbytes_in
,
1015 unsigned int* nbytes_out
)
1017 BZ2_bzWriteClose64 ( bzerror
, b
, abandon
,
1018 nbytes_in
, NULL
, nbytes_out
, NULL
);
1022 void BZ_API(BZ2_bzWriteClose64
)
1026 unsigned int* nbytes_in_lo32
,
1027 unsigned int* nbytes_in_hi32
,
1028 unsigned int* nbytes_out_lo32
,
1029 unsigned int* nbytes_out_hi32
)
1032 bzFile
* bzf
= (bzFile
*)b
;
1035 { BZ_SETERR(BZ_OK
); return; };
1036 if (!(bzf
->writing
))
1037 { BZ_SETERR(BZ_SEQUENCE_ERROR
); return; };
1038 if (ferror(bzf
->handle
))
1039 { BZ_SETERR(BZ_IO_ERROR
); return; };
1041 if (nbytes_in_lo32
!= NULL
) *nbytes_in_lo32
= 0;
1042 if (nbytes_in_hi32
!= NULL
) *nbytes_in_hi32
= 0;
1043 if (nbytes_out_lo32
!= NULL
) *nbytes_out_lo32
= 0;
1044 if (nbytes_out_hi32
!= NULL
) *nbytes_out_hi32
= 0;
1046 if ((!abandon
) && bzf
->lastErr
== BZ_OK
) {
1048 bzf
->strm
.avail_out
= BZ_MAX_UNUSED
;
1049 bzf
->strm
.next_out
= bzf
->buf
;
1050 ret
= BZ2_bzCompress ( &(bzf
->strm
), BZ_FINISH
);
1051 if (ret
!= BZ_FINISH_OK
&& ret
!= BZ_STREAM_END
)
1052 { BZ_SETERR(ret
); return; };
1054 if (bzf
->strm
.avail_out
< BZ_MAX_UNUSED
) {
1055 n
= BZ_MAX_UNUSED
- bzf
->strm
.avail_out
;
1056 n2
= fwrite ( (void*)(bzf
->buf
), sizeof(UChar
),
1058 if (n
!= n2
|| ferror(bzf
->handle
))
1059 { BZ_SETERR(BZ_IO_ERROR
); return; };
1062 if (ret
== BZ_STREAM_END
) break;
1066 if ( !abandon
&& !ferror ( bzf
->handle
) ) {
1067 fflush ( bzf
->handle
);
1068 if (ferror(bzf
->handle
))
1069 { BZ_SETERR(BZ_IO_ERROR
); return; };
1072 if (nbytes_in_lo32
!= NULL
)
1073 *nbytes_in_lo32
= bzf
->strm
.total_in_lo32
;
1074 if (nbytes_in_hi32
!= NULL
)
1075 *nbytes_in_hi32
= bzf
->strm
.total_in_hi32
;
1076 if (nbytes_out_lo32
!= NULL
)
1077 *nbytes_out_lo32
= bzf
->strm
.total_out_lo32
;
1078 if (nbytes_out_hi32
!= NULL
)
1079 *nbytes_out_hi32
= bzf
->strm
.total_out_hi32
;
1082 BZ2_bzCompressEnd ( &(bzf
->strm
) );
1087 /*---------------------------------------------------*/
1088 BZFILE
* BZ_API(BZ2_bzReadOpen
)
1102 (small
!= 0 && small
!= 1) ||
1103 (verbosity
< 0 || verbosity
> 4) ||
1104 (unused
== NULL
&& nUnused
!= 0) ||
1105 (unused
!= NULL
&& (nUnused
< 0 || nUnused
> BZ_MAX_UNUSED
)))
1106 { BZ_SETERR(BZ_PARAM_ERROR
); return NULL
; };
1109 { BZ_SETERR(BZ_IO_ERROR
); return NULL
; };
1111 bzf
= malloc ( sizeof(bzFile
) );
1113 { BZ_SETERR(BZ_MEM_ERROR
); return NULL
; };
1117 bzf
->initialisedOk
= False
;
1120 bzf
->writing
= False
;
1121 bzf
->strm
.bzalloc
= NULL
;
1122 bzf
->strm
.bzfree
= NULL
;
1123 bzf
->strm
.opaque
= NULL
;
1125 while (nUnused
> 0) {
1126 bzf
->buf
[bzf
->bufN
] = *((UChar
*)(unused
)); bzf
->bufN
++;
1127 unused
= ((void*)( 1 + ((UChar
*)(unused
)) ));
1131 ret
= BZ2_bzDecompressInit ( &(bzf
->strm
), verbosity
, small
);
1133 { BZ_SETERR(ret
); free(bzf
); return NULL
; };
1135 bzf
->strm
.avail_in
= bzf
->bufN
;
1136 bzf
->strm
.next_in
= bzf
->buf
;
1138 bzf
->initialisedOk
= True
;
1143 /*---------------------------------------------------*/
1144 void BZ_API(BZ2_bzReadClose
) ( int *bzerror
, BZFILE
*b
)
1146 bzFile
* bzf
= (bzFile
*)b
;
1150 { BZ_SETERR(BZ_OK
); return; };
1153 { BZ_SETERR(BZ_SEQUENCE_ERROR
); return; };
1155 if (bzf
->initialisedOk
)
1156 (void)BZ2_bzDecompressEnd ( &(bzf
->strm
) );
1161 /*---------------------------------------------------*/
1162 int BZ_API(BZ2_bzRead
)
1169 bzFile
* bzf
= (bzFile
*)b
;
1173 if (bzf
== NULL
|| buf
== NULL
|| len
< 0)
1174 { BZ_SETERR(BZ_PARAM_ERROR
); return 0; };
1177 { BZ_SETERR(BZ_SEQUENCE_ERROR
); return 0; };
1180 { BZ_SETERR(BZ_OK
); return 0; };
1182 bzf
->strm
.avail_out
= len
;
1183 bzf
->strm
.next_out
= buf
;
1187 if (ferror(bzf
->handle
))
1188 { BZ_SETERR(BZ_IO_ERROR
); return 0; };
1190 if (bzf
->strm
.avail_in
== 0 && !myfeof(bzf
->handle
)) {
1191 n
= fread ( bzf
->buf
, sizeof(UChar
),
1192 BZ_MAX_UNUSED
, bzf
->handle
);
1193 if (ferror(bzf
->handle
))
1194 { BZ_SETERR(BZ_IO_ERROR
); return 0; };
1196 bzf
->strm
.avail_in
= bzf
->bufN
;
1197 bzf
->strm
.next_in
= bzf
->buf
;
1200 ret
= BZ2_bzDecompress ( &(bzf
->strm
) );
1202 if (ret
!= BZ_OK
&& ret
!= BZ_STREAM_END
)
1203 { BZ_SETERR(ret
); return 0; };
1205 if (ret
== BZ_OK
&& myfeof(bzf
->handle
) &&
1206 bzf
->strm
.avail_in
== 0 && bzf
->strm
.avail_out
> 0)
1207 { BZ_SETERR(BZ_UNEXPECTED_EOF
); return 0; };
1209 if (ret
== BZ_STREAM_END
)
1210 { BZ_SETERR(BZ_STREAM_END
);
1211 return len
- bzf
->strm
.avail_out
; };
1212 if (bzf
->strm
.avail_out
== 0)
1213 { BZ_SETERR(BZ_OK
); return len
; };
1217 return 0; /*not reached*/
1221 /*---------------------------------------------------*/
1222 void BZ_API(BZ2_bzReadGetUnused
)
1228 bzFile
* bzf
= (bzFile
*)b
;
1230 { BZ_SETERR(BZ_PARAM_ERROR
); return; };
1231 if (bzf
->lastErr
!= BZ_STREAM_END
)
1232 { BZ_SETERR(BZ_SEQUENCE_ERROR
); return; };
1233 if (unused
== NULL
|| nUnused
== NULL
)
1234 { BZ_SETERR(BZ_PARAM_ERROR
); return; };
1237 *nUnused
= bzf
->strm
.avail_in
;
1238 *unused
= bzf
->strm
.next_in
;
1242 #ifndef BZ_DECOMPRESS_ONLY
1244 /*---------------------------------------------------*/
1245 /*--- Misc convenience stuff ---*/
1246 /*---------------------------------------------------*/
1248 /*---------------------------------------------------*/
1249 int BZ_API(BZ2_bzBuffToBuffCompress
)
1251 unsigned int* destLen
,
1253 unsigned int sourceLen
,
1261 if (dest
== NULL
|| destLen
== NULL
||
1263 blockSize100k
< 1 || blockSize100k
> 9 ||
1264 verbosity
< 0 || verbosity
> 4 ||
1265 workFactor
< 0 || workFactor
> 250)
1266 return BZ_PARAM_ERROR
;
1268 if (workFactor
== 0) workFactor
= 30;
1269 strm
.bzalloc
= NULL
;
1273 ret
= BZ2_bzCompressInit ( &strm
, blockSize100k
,
1274 verbosity
, workFactor
);
1275 if (ret
!= BZ_OK
) return ret
;
1277 strm
.next_in
= source
;
1278 strm
.next_out
= dest
;
1279 strm
.avail_in
= sourceLen
;
1280 strm
.avail_out
= *destLen
;
1282 ret
= BZ2_bzCompress ( &strm
, BZ_FINISH
);
1283 if (ret
== BZ_FINISH_OK
) goto output_overflow
;
1284 if (ret
!= BZ_STREAM_END
) goto errhandler
;
1286 /* normal termination */
1287 *destLen
-= strm
.avail_out
;
1289 BZ2_bzCompressEnd ( &strm
);
1294 BZ2_bzCompressEnd ( &strm
);
1295 return BZ_OUTBUFF_FULL
;
1299 BZ2_bzCompressEnd ( &strm
);
1303 #endif // BZ_DECOMPRESS_0NLY
1305 /*---------------------------------------------------*/
1306 int BZ_API(BZ2_bzBuffToBuffDecompress
)
1308 unsigned int* destLen
,
1310 unsigned int sourceLen
,
1317 if (dest
== NULL
|| destLen
== NULL
||
1319 (small
!= 0 && small
!= 1) ||
1320 verbosity
< 0 || verbosity
> 4)
1321 return BZ_PARAM_ERROR
;
1323 strm
.bzalloc
= NULL
;
1326 ret
= BZ2_bzDecompressInit ( &strm
, verbosity
, small
);
1327 if (ret
!= BZ_OK
) return ret
;
1329 strm
.next_in
= source
;
1330 strm
.next_out
= dest
;
1331 strm
.avail_in
= sourceLen
;
1332 strm
.avail_out
= *destLen
;
1334 ret
= BZ2_bzDecompress ( &strm
);
1335 if (ret
== BZ_OK
) goto output_overflow_or_eof
;
1336 if (ret
!= BZ_STREAM_END
) goto errhandler
;
1338 /* normal termination */
1339 *destLen
-= strm
.avail_out
;
1340 BZ2_bzDecompressEnd ( &strm
);
1343 output_overflow_or_eof
:
1344 if (strm
.avail_out
> 0) {
1345 BZ2_bzDecompressEnd ( &strm
);
1346 return BZ_UNEXPECTED_EOF
;
1348 BZ2_bzDecompressEnd ( &strm
);
1349 return BZ_OUTBUFF_FULL
;
1353 BZ2_bzDecompressEnd ( &strm
);
1358 /*---------------------------------------------------*/
1360 Code contributed by Yoshioka Tsuneo
1361 (QWF00133@niftyserve.or.jp/tsuneo-y@is.aist-nara.ac.jp),
1362 to support better zlib compatibility.
1363 This code is not _officially_ part of libbzip2 (yet);
1364 I haven't tested it, documented it, or considered the
1365 threading-safeness of it.
1366 If this code breaks, please contact both Yoshioka and me.
1368 /*---------------------------------------------------*/
1370 /*---------------------------------------------------*/
1372 return version like "0.9.0c".
1374 const char * BZ_API(BZ2_bzlibVersion
)(void)
1381 /*---------------------------------------------------*/
1383 #if defined(_WIN32) || defined(OS2) || defined(MSDOS)
1386 # define SET_BINARY_MODE(file) setmode(fileno(file),O_BINARY)
1388 # define SET_BINARY_MODE(file)
1391 BZFILE
* bzopen_or_bzdopen
1392 ( const char *path
, /* no use when bzdopen */
1393 int fd
, /* no use when bzdopen */
1395 int open_mode
) /* bzopen: 0, bzdopen:1 */
1398 char unused
[BZ_MAX_UNUSED
];
1399 int blockSize100k
= 9;
1401 char mode2
[10] = "";
1403 BZFILE
*bzfp
= NULL
;
1405 int workFactor
= 30;
1409 if (mode
== NULL
) return NULL
;
1417 smallMode
= 1; break;
1419 if (isdigit((int)(*mode
))) {
1420 blockSize100k
= *mode
-'0';
1425 strcat(mode2
, writing
? "w" : "r" );
1426 strcat(mode2
,"b"); /* binary mode */
1429 if (path
==NULL
|| strcmp(path
,"")==0) {
1430 fp
= (writing
? stdout
: stdin
);
1431 SET_BINARY_MODE(fp
);
1433 fp
= fopen(path
,mode2
);
1436 #ifdef BZ_STRICT_ANSI
1439 fp
= fdopen(fd
,mode2
);
1442 if (fp
== NULL
) return NULL
;
1445 /* Guard against total chaos and anarchy -- JRS */
1446 if (blockSize100k
< 1) blockSize100k
= 1;
1447 if (blockSize100k
> 9) blockSize100k
= 9;
1448 bzfp
= BZ2_bzWriteOpen(&bzerr
,fp
,blockSize100k
,
1449 verbosity
,workFactor
);
1451 bzfp
= BZ2_bzReadOpen(&bzerr
,fp
,verbosity
,smallMode
,
1455 if (fp
!= stdin
&& fp
!= stdout
) fclose(fp
);
1462 /*---------------------------------------------------*/
1464 open file for read or write.
1465 ex) bzopen("file","w9")
1466 case path="" or NULL => use stdin or stdout.
1468 BZFILE
* BZ_API(BZ2_bzopen
)
1472 return bzopen_or_bzdopen(path
,-1,mode
,/*bzopen*/0);
1476 /*---------------------------------------------------*/
1477 BZFILE
* BZ_API(BZ2_bzdopen
)
1481 return bzopen_or_bzdopen(NULL
,fd
,mode
,/*bzdopen*/1);
1485 /*---------------------------------------------------*/
1486 int BZ_API(BZ2_bzread
) (BZFILE
* b
, void* buf
, int len
)
1489 if (((bzFile
*)b
)->lastErr
== BZ_STREAM_END
) return 0;
1490 nread
= BZ2_bzRead(&bzerr
,b
,buf
,len
);
1491 if (bzerr
== BZ_OK
|| bzerr
== BZ_STREAM_END
) {
1499 /*---------------------------------------------------*/
1500 int BZ_API(BZ2_bzwrite
) (BZFILE
* b
, void* buf
, int len
)
1504 BZ2_bzWrite(&bzerr
,b
,buf
,len
);
1513 /*---------------------------------------------------*/
1514 int BZ_API(BZ2_bzflush
) (BZFILE
*b
)
1516 /* do nothing now... */
1521 /*---------------------------------------------------*/
1522 void BZ_API(BZ2_bzclose
) (BZFILE
* b
)
1525 FILE *fp
= ((bzFile
*)b
)->handle
;
1527 if (b
==NULL
) {return;}
1528 if(((bzFile
*)b
)->writing
){
1529 BZ2_bzWriteClose(&bzerr
,b
,0,NULL
,NULL
);
1531 BZ2_bzWriteClose(NULL
,b
,1,NULL
,NULL
);
1534 BZ2_bzReadClose(&bzerr
,b
);
1536 if(fp
!=stdin
&& fp
!=stdout
){
1542 /*---------------------------------------------------*/
1544 return last error code
1546 static char *bzerrorstrings
[] = {
1557 ,"???" /* for future */
1558 ,"???" /* for future */
1559 ,"???" /* for future */
1560 ,"???" /* for future */
1561 ,"???" /* for future */
1562 ,"???" /* for future */
1566 const char * BZ_API(BZ2_bzerror
) (BZFILE
*b
, int *errnum
)
1568 int err
= ((bzFile
*)b
)->lastErr
;
1572 return bzerrorstrings
[err
*-1];
1577 /*-------------------------------------------------------------*/
1578 /*--- end bzlib.c ---*/
1579 /*-------------------------------------------------------------*/