blob: ff3cd16158a602833858a276c45f14cc68daa87b [file] [log] [blame]
Reid Spencera01ef712004-11-25 16:11:36 +00001
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-2002 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 jseward@acm.org
46 bzip2/libbzip2 version 1.0 of 21 March 2000
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
79/*---------------------------------------------------*/
80/*--- Compression stuff ---*/
81/*---------------------------------------------------*/
82
83
84/*---------------------------------------------------*/
85#ifndef BZ_NO_STDIO
86void BZ2_bz__AssertH__fail ( int errcode )
87{
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()
99 );
100
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"
Reid Spencerd8df61a2006-05-31 21:53:42 +0000111 );
112 fprintf(stderr,
Reid Spencera01ef712004-11-25 16:11:36 +0000113 "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"
Reid Spencerd8df61a2006-05-31 21:53:42 +0000123 );
124 fprintf(stderr,
Reid Spencera01ef712004-11-25 16:11:36 +0000125 "* 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"
130 );
131 }
132
133 exit(3);
134}
135#endif
136
137
138/*---------------------------------------------------*/
139static
140int bz_config_ok ( void )
141{
142 if (sizeof(int) != 4) return 0;
143 if (sizeof(short) != 2) return 0;
144 if (sizeof(char) != 1) return 0;
145 return 1;
146}
147
148
149/*---------------------------------------------------*/
150static
151void* default_bzalloc ( void* opaque, Int32 items, Int32 size )
152{
153 void* v = malloc ( items * size );
154 return v;
155}
156
157static
158void default_bzfree ( void* opaque, void* addr )
159{
160 if (addr != NULL) free ( addr );
161}
162
163
164/*---------------------------------------------------*/
165static
166void prepare_new_block ( EState* s )
167{
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++;
175}
176
177
178/*---------------------------------------------------*/
179static
180void init_RL ( EState* s )
181{
182 s->state_in_ch = 256;
183 s->state_in_len = 0;
184}
185
186
187static
188Bool isempty_RL ( EState* s )
189{
190 if (s->state_in_ch < 256 && s->state_in_len > 0)
191 return False; else
192 return True;
193}
194
195
196/*---------------------------------------------------*/
197int BZ_API(BZ2_bzCompressInit)
198 ( bz_stream* strm,
199 int blockSize100k,
200 int verbosity,
201 int workFactor )
202{
203 Int32 n;
204 EState* s;
205
206 if (!bz_config_ok()) return BZ_CONFIG_ERROR;
207
208 if (strm == NULL ||
209 blockSize100k < 1 || blockSize100k > 9 ||
210 workFactor < 0 || workFactor > 250)
211 return BZ_PARAM_ERROR;
212
213 if (workFactor == 0) workFactor = 30;
214 if (strm->bzalloc == NULL) strm->bzalloc = default_bzalloc;
215 if (strm->bzfree == NULL) strm->bzfree = default_bzfree;
216
217 s = BZALLOC( sizeof(EState) );
218 if (s == NULL) return BZ_MEM_ERROR;
219 s->strm = strm;
220
221 s->arr1 = NULL;
222 s->arr2 = NULL;
223 s->ftab = NULL;
224
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) );
229
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;
236 }
237
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;
246
247 s->block = (UChar*)s->arr2;
248 s->mtfv = (UInt16*)s->arr1;
249 s->zbits = NULL;
250 s->ptr = (UInt32*)s->arr1;
251
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;
260}
261
262
263/*---------------------------------------------------*/
264static
265void add_pair_to_block ( EState* s )
266{
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 );
271 }
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;
295 }
296}
297
298
299/*---------------------------------------------------*/
300static
301void flush_RL ( EState* s )
302{
303 if (s->state_in_ch < 256) add_pair_to_block ( s );
304 init_RL ( s );
305}
306
307
308/*---------------------------------------------------*/
309#define ADD_CHAR_TO_BLOCK(zs,zchh0) \
310{ \
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; \
321 } \
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++; \
332 } \
333}
334
335
336/*---------------------------------------------------*/
337static
338Bool copy_input_until_stop ( EState* s )
339{
340 Bool progress_in = False;
341
342 if (s->mode == BZ_M_RUNNING) {
343
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++;
356 }
357
358 } else {
359
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--;
375 }
376 }
377 return progress_in;
378}
379
380
381/*---------------------------------------------------*/
382static
383Bool copy_output_until_stop ( EState* s )
384{
385 Bool progress_out = False;
386
387 while (True) {
388
389 /*-- no output space? --*/
390 if (s->strm->avail_out == 0) break;
391
392 /*-- block done? --*/
393 if (s->state_out_pos >= s->numZ) break;
394
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++;
402 }
403
404 return progress_out;
405}
406
407
408/*---------------------------------------------------*/
409static
410Bool handle_compress ( bz_stream* strm )
411{
412 Bool progress_in = False;
413 Bool progress_out = False;
414 EState* s = strm->state;
415
416 while (True) {
417
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;
429 }
430
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;
437 }
438 else
439 if (s->nblock >= s->nblockMAX) {
440 BZ2_compressBlock ( s, False );
441 s->state = BZ_S_OUTPUT;
442 }
443 else
444 if (s->strm->avail_in == 0) {
445 break;
446 }
447 }
448
449 }
450
451 return progress_in || progress_out;
452}
453
454
455/*---------------------------------------------------*/
456int BZ_API(BZ2_bzCompress) ( bz_stream *strm, int action )
457{
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;
464
465 preswitch:
466 switch (s->mode) {
467
468 case BZ_M_IDLE:
469 return BZ_SEQUENCE_ERROR;
470
471 case BZ_M_RUNNING:
472 if (action == BZ_RUN) {
473 progress = handle_compress ( strm );
474 return progress ? BZ_RUN_OK : BZ_PARAM_ERROR;
475 }
476 else
Misha Brukman5191b4b2005-04-22 04:08:30 +0000477 if (action == BZ_FLUSH) {
Reid Spencera01ef712004-11-25 16:11:36 +0000478 s->avail_in_expect = strm->avail_in;
479 s->mode = BZ_M_FLUSHING;
480 goto preswitch;
481 }
482 else
483 if (action == BZ_FINISH) {
484 s->avail_in_expect = strm->avail_in;
485 s->mode = BZ_M_FINISHING;
486 goto preswitch;
487 }
488 else
489 return BZ_PARAM_ERROR;
490
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;
500
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;
511 }
512 return BZ_OK; /*--not reached--*/
513}
514
515
516/*---------------------------------------------------*/
517int BZ_API(BZ2_bzCompressEnd) ( bz_stream *strm )
518{
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;
524
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);
529
530 strm->state = NULL;
531
532 return BZ_OK;
533}
534
535
536/*---------------------------------------------------*/
537/*--- Decompression stuff ---*/
538/*---------------------------------------------------*/
539
540/*---------------------------------------------------*/
541int BZ_API(BZ2_bzDecompressInit)
542 ( bz_stream* strm,
543 int verbosity,
544 int small )
545{
546 DState* s;
547
548 if (!bz_config_ok()) return BZ_CONFIG_ERROR;
549
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;
553
554 if (strm->bzalloc == NULL) strm->bzalloc = default_bzalloc;
555 if (strm->bzfree == NULL) strm->bzfree = default_bzfree;
556
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;
575
576 return BZ_OK;
577}
578
579
580/*---------------------------------------------------*/
581static
582void unRLE_obuf_to_output_FAST ( DState* s )
583{
584 UChar k1;
585
586 if (s->blockRandomised) {
587
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++;
600 }
601
602 /* can a new run be started? */
603 if (s->nblock_used == s->save_nblock+1) return;
604
605
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; };
612
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; };
618
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; };
624
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++;
630 }
631
632 } else {
633
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 */
645
646 UInt32 avail_out_INIT = cs_avail_out;
647 Int32 s_save_nblockPP = s->save_nblock+1;
648 unsigned int total_out_lo32_old;
649
650 while (True) {
651
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--;
662 }
663 s_state_out_len_eq_one:
664 {
665 if (cs_avail_out == 0) {
666 c_state_out_len = 1; goto return_notr;
667 };
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--;
672 }
673 }
674 /* can a new run be started? */
675 if (c_nblock_used == s_save_nblockPP) {
676 c_state_out_len = 0; goto return_notr;
677 };
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;
682 };
683 if (c_nblock_used == s_save_nblockPP)
684 goto s_state_out_len_eq_one;
685
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; };
690
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; };
695
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++;
699 }
700
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++;
706
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 */
718 }
719}
720
721
722
723/*---------------------------------------------------*/
724__inline__ Int32 BZ2_indexIntoF ( Int32 indx, Int32 *cftab )
725{
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;
732 }
733 while (na - nb != 1);
734 return nb;
735}
736
737
738/*---------------------------------------------------*/
739static
740void unRLE_obuf_to_output_SMALL ( DState* s )
741{
742 UChar k1;
743
744 if (s->blockRandomised) {
745
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++;
758 }
759
760 /* can a new run be started? */
761 if (s->nblock_used == s->save_nblock+1) return;
762
763
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; };
770
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; };
776
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; };
782
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++;
788 }
789
790 } else {
791
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++;
804 }
805
806 /* can a new run be started? */
807 if (s->nblock_used == s->save_nblock+1) return;
808
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; };
814
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; };
819
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; };
824
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++;
828 }
829
830 }
831}
832
833
834/*---------------------------------------------------*/
835int BZ_API(BZ2_bzDecompress) ( bz_stream *strm )
836{
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;
842
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;
864 }
865 }
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;
875 }
876 if (s->state != BZ_X_OUTPUT) return r;
877 }
878 }
879
880 AssertH ( 0, 6001 );
881
882 return 0; /*NOTREACHED*/
883}
884
885
886/*---------------------------------------------------*/
887int BZ_API(BZ2_bzDecompressEnd) ( bz_stream *strm )
888{
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;
894
895 if (s->tt != NULL) BZFREE(s->tt);
896 if (s->ll16 != NULL) BZFREE(s->ll16);
897 if (s->ll4 != NULL) BZFREE(s->ll4);
898
899 BZFREE(strm->state);
900 strm->state = NULL;
901
902 return BZ_OK;
903}
904
905
906#ifndef BZ_NO_STDIO
907/*---------------------------------------------------*/
908/*--- File I/O stuff ---*/
909/*---------------------------------------------------*/
910
911#define BZ_SETERR(eee) \
912{ \
913 if (bzerror != NULL) *bzerror = eee; \
914 if (bzf != NULL) bzf->lastErr = eee; \
915}
916
917typedef
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;
926 }
927 bzFile;
928
929
930/*---------------------------------------------*/
931static Bool myfeof ( FILE* f )
932{
933 Int32 c = fgetc ( f );
934 if (c == EOF) return True;
935 ungetc ( c, f );
936 return False;
937}
938
939
940/*---------------------------------------------------*/
941BZFILE* BZ_API(BZ2_bzWriteOpen)
942 ( int* bzerror,
943 FILE* f,
944 int blockSize100k,
945 int verbosity,
946 int workFactor )
947{
948 Int32 ret;
949 bzFile* bzf = NULL;
950
951 BZ_SETERR(BZ_OK);
952
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; };
958
959 if (ferror(f))
960 { BZ_SETERR(BZ_IO_ERROR); return NULL; };
961
962 bzf = malloc ( sizeof(bzFile) );
963 if (bzf == NULL)
964 { BZ_SETERR(BZ_MEM_ERROR); return NULL; };
965
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;
974
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; };
980
981 bzf->strm.avail_in = 0;
982 bzf->initialisedOk = True;
983 return bzf;
984}
985
986
987
988/*---------------------------------------------------*/
989void BZ_API(BZ2_bzWrite)
990 ( int* bzerror,
991 BZFILE* b,
992 void* buf,
993 int len )
994{
995 Int32 n, n2, ret;
996 bzFile* bzf = (bzFile*)b;
997
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; };
1005
1006 if (len == 0)
1007 { BZ_SETERR(BZ_OK); return; };
1008
1009 bzf->strm.avail_in = len;
1010 bzf->strm.next_in = buf;
1011
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; };
1018
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; };
1025 }
1026
1027 if (bzf->strm.avail_in == 0)
1028 { BZ_SETERR(BZ_OK); return; };
1029 }
1030}
1031
1032
1033/*---------------------------------------------------*/
1034void BZ_API(BZ2_bzWriteClose)
1035 ( int* bzerror,
1036 BZFILE* b,
1037 int abandon,
1038 unsigned int* nbytes_in,
1039 unsigned int* nbytes_out )
1040{
1041 BZ2_bzWriteClose64 ( bzerror, b, abandon,
1042 nbytes_in, NULL, nbytes_out, NULL );
1043}
1044
1045
1046void 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 )
1054{
1055 Int32 n, n2, ret;
1056 bzFile* bzf = (bzFile*)b;
1057
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; };
1064
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;
1069
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; };
1077
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; };
1084 }
1085
1086 if (ret == BZ_STREAM_END) break;
1087 }
1088 }
1089
1090 if ( !abandon && !ferror ( bzf->handle ) ) {
1091 fflush ( bzf->handle );
1092 if (ferror(bzf->handle))
1093 { BZ_SETERR(BZ_IO_ERROR); return; };
1094 }
1095
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;
1104
1105 BZ_SETERR(BZ_OK);
1106 BZ2_bzCompressEnd ( &(bzf->strm) );
1107 free ( bzf );
1108}
1109
1110
1111/*---------------------------------------------------*/
1112BZFILE* BZ_API(BZ2_bzReadOpen)
1113 ( int* bzerror,
1114 FILE* f,
1115 int verbosity,
1116 int small,
1117 void* unused,
1118 int nUnused )
1119{
1120 bzFile* bzf = NULL;
1121 int ret;
1122
1123 BZ_SETERR(BZ_OK);
1124
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; };
1131
1132 if (ferror(f))
1133 { BZ_SETERR(BZ_IO_ERROR); return NULL; };
1134
1135 bzf = malloc ( sizeof(bzFile) );
1136 if (bzf == NULL)
1137 { BZ_SETERR(BZ_MEM_ERROR); return NULL; };
1138
1139 BZ_SETERR(BZ_OK);
1140
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;
1148
1149 while (nUnused > 0) {
1150 bzf->buf[bzf->bufN] = *((UChar*)(unused)); bzf->bufN++;
1151 unused = ((void*)( 1 + ((UChar*)(unused)) ));
1152 nUnused--;
1153 }
1154
1155 ret = BZ2_bzDecompressInit ( &(bzf->strm), verbosity, small );
1156 if (ret != BZ_OK)
1157 { BZ_SETERR(ret); free(bzf); return NULL; };
1158
1159 bzf->strm.avail_in = bzf->bufN;
1160 bzf->strm.next_in = bzf->buf;
1161
1162 bzf->initialisedOk = True;
1163 return bzf;
1164}
1165
1166
1167/*---------------------------------------------------*/
1168void BZ_API(BZ2_bzReadClose) ( int *bzerror, BZFILE *b )
1169{
1170 bzFile* bzf = (bzFile*)b;
1171
1172 BZ_SETERR(BZ_OK);
1173 if (bzf == NULL)
1174 { BZ_SETERR(BZ_OK); return; };
1175
1176 if (bzf->writing)
1177 { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
1178
1179 if (bzf->initialisedOk)
1180 (void)BZ2_bzDecompressEnd ( &(bzf->strm) );
1181 free ( bzf );
1182}
1183
1184
1185/*---------------------------------------------------*/
1186int BZ_API(BZ2_bzRead)
1187 ( int* bzerror,
1188 BZFILE* b,
1189 void* buf,
1190 int len )
1191{
1192 Int32 n, ret;
1193 bzFile* bzf = (bzFile*)b;
1194
1195 BZ_SETERR(BZ_OK);
1196
1197 if (bzf == NULL || buf == NULL || len < 0)
1198 { BZ_SETERR(BZ_PARAM_ERROR); return 0; };
1199
1200 if (bzf->writing)
1201 { BZ_SETERR(BZ_SEQUENCE_ERROR); return 0; };
1202
1203 if (len == 0)
1204 { BZ_SETERR(BZ_OK); return 0; };
1205
1206 bzf->strm.avail_out = len;
1207 bzf->strm.next_out = buf;
1208
1209 while (True) {
1210
1211 if (ferror(bzf->handle))
1212 { BZ_SETERR(BZ_IO_ERROR); return 0; };
1213
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;
1222 }
1223
1224 ret = BZ2_bzDecompress ( &(bzf->strm) );
1225
1226 if (ret != BZ_OK && ret != BZ_STREAM_END)
1227 { BZ_SETERR(ret); return 0; };
1228
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; };
1232
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; };
1238
1239 }
1240
1241 return 0; /*not reached*/
1242}
1243
1244
1245/*---------------------------------------------------*/
1246void BZ_API(BZ2_bzReadGetUnused)
1247 ( int* bzerror,
1248 BZFILE* b,
1249 void** unused,
1250 int* nUnused )
1251{
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; };
1259
1260 BZ_SETERR(BZ_OK);
1261 *nUnused = bzf->strm.avail_in;
1262 *unused = bzf->strm.next_in;
1263}
1264#endif
1265
1266
1267/*---------------------------------------------------*/
1268/*--- Misc convenience stuff ---*/
1269/*---------------------------------------------------*/
1270
1271/*---------------------------------------------------*/
1272int 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 )
1280{
1281 bz_stream strm;
1282 int ret;
1283
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;
1290
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;
1298
1299 strm.next_in = source;
1300 strm.next_out = dest;
1301 strm.avail_in = sourceLen;
1302 strm.avail_out = *destLen;
1303
1304 ret = BZ2_bzCompress ( &strm, BZ_FINISH );
1305 if (ret == BZ_FINISH_OK) goto output_overflow;
1306 if (ret != BZ_STREAM_END) goto errhandler;
1307
1308 /* normal termination */
1309 *destLen -= strm.avail_out;
1310 BZ2_bzCompressEnd ( &strm );
1311 return BZ_OK;
1312
1313 output_overflow:
1314 BZ2_bzCompressEnd ( &strm );
1315 return BZ_OUTBUFF_FULL;
1316
1317 errhandler:
1318 BZ2_bzCompressEnd ( &strm );
1319 return ret;
1320}
1321
1322
1323/*---------------------------------------------------*/
1324int BZ_API(BZ2_bzBuffToBuffDecompress)
1325 ( char* dest,
1326 unsigned int* destLen,
1327 char* source,
1328 unsigned int sourceLen,
1329 int small,
1330 int verbosity )
1331{
1332 bz_stream strm;
1333 int ret;
1334
1335 if (dest == NULL || destLen == NULL ||
1336 source == NULL ||
1337 (small != 0 && small != 1) ||
1338 verbosity < 0 || verbosity > 4)
1339 return BZ_PARAM_ERROR;
1340
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;
1346
1347 strm.next_in = source;
1348 strm.next_out = dest;
1349 strm.avail_in = sourceLen;
1350 strm.avail_out = *destLen;
1351
1352 ret = BZ2_bzDecompress ( &strm );
1353 if (ret == BZ_OK) goto output_overflow_or_eof;
1354 if (ret != BZ_STREAM_END) goto errhandler;
1355
1356 /* normal termination */
1357 *destLen -= strm.avail_out;
1358 BZ2_bzDecompressEnd ( &strm );
1359 return BZ_OK;
1360
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;
1368 };
1369
1370 errhandler:
1371 BZ2_bzDecompressEnd ( &strm );
1372 return ret;
1373}
1374
1375
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/*---------------------------------------------------*/
1387
1388/*---------------------------------------------------*/
1389/*--
1390 return version like "0.9.0c".
1391--*/
1392const char * BZ_API(BZ2_bzlibVersion)(void)
1393{
1394 return BZ_VERSION;
1395}
1396
1397
1398#ifndef BZ_NO_STDIO
1399/*---------------------------------------------------*/
1400
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
1408static
1409BZFILE * 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 */
1414{
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;
1426
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;
1439 }
1440 }
1441 mode++;
1442 }
1443 strcat(mode2, writing ? "w" : "r" );
1444 strcat(mode2,"b"); /* binary mode */
1445
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);
1452 }
1453 } else {
1454#ifdef BZ_STRICT_ANSI
1455 fp = NULL;
1456#else
1457 fp = fdopen(fd,mode2);
1458#endif
1459 }
1460 if (fp == NULL) return NULL;
1461
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);
1471 }
1472 if (bzfp == NULL) {
1473 if (fp != stdin && fp != stdout) fclose(fp);
1474 return NULL;
1475 }
1476 return bzfp;
1477}
1478
1479
1480/*---------------------------------------------------*/
1481/*--
1482 open file for read or write.
1483 ex) bzopen("file","w9")
1484 case path="" or NULL => use stdin or stdout.
1485--*/
1486BZFILE * BZ_API(BZ2_bzopen)
1487 ( const char *path,
1488 const char *mode )
1489{
1490 return bzopen_or_bzdopen(path,-1,mode,/*bzopen*/0);
1491}
1492
1493
1494/*---------------------------------------------------*/
1495BZFILE * BZ_API(BZ2_bzdopen)
1496 ( int fd,
1497 const char *mode )
1498{
1499 return bzopen_or_bzdopen(NULL,fd,mode,/*bzdopen*/1);
1500}
1501
1502
1503/*---------------------------------------------------*/
1504int BZ_API(BZ2_bzread) (BZFILE* b, void* buf, int len )
1505{
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;
1513 }
1514}
1515
1516
1517/*---------------------------------------------------*/
1518int BZ_API(BZ2_bzwrite) (BZFILE* b, void* buf, int len )
1519{
1520 int bzerr;
1521
1522 BZ2_bzWrite(&bzerr,b,buf,len);
1523 if(bzerr == BZ_OK){
1524 return len;
1525 }else{
1526 return -1;
1527 }
1528}
1529
1530
1531/*---------------------------------------------------*/
1532int BZ_API(BZ2_bzflush) (BZFILE *b)
1533{
1534 /* do nothing now... */
1535 return 0;
1536}
1537
1538
1539/*---------------------------------------------------*/
1540void BZ_API(BZ2_bzclose) (BZFILE* b)
1541{
1542 int bzerr;
1543 FILE *fp = ((bzFile *)b)->handle;
1544
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);
1550 }
1551 }else{
1552 BZ2_bzReadClose(&bzerr,b);
1553 }
1554 if(fp!=stdin && fp!=stdout){
1555 fclose(fp);
1556 }
1557}
1558
1559
1560/*---------------------------------------------------*/
1561/*--
1562 return last error code
1563--*/
1564static 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 */
1581};
1582
1583
1584const char * BZ_API(BZ2_bzerror) (BZFILE *b, int *errnum)
1585{
1586 int err = ((bzFile *)b)->lastErr;
1587
1588 if(err>0) err = 0;
1589 *errnum = err;
1590 return bzerrorstrings[err*-1];
1591}
1592#endif
1593
1594
1595/*-------------------------------------------------------------*/
1596/*--- end bzlib.c ---*/
1597/*-------------------------------------------------------------*/