Reverted latest changes.
[reactos.git] / reactos / drivers / lib / bzip2 / bzlib.c
1
2 /*-------------------------------------------------------------*/
3 /*--- Library top-level functions. ---*/
4 /*--- bzlib.c ---*/
5 /*-------------------------------------------------------------*/
6
7 /*--
8 This file is a part of bzip2 and/or libbzip2, a program and
9 library for lossless, block-sorting data compression.
10
11 Copyright (C) 1996-2000 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 #ifdef BZ_DECOMPRESS_ONLY
77 #define __NTDRIVER__
78 #include <ntddk.h>
79 #include <debug.h>
80 #endif
81
82 #include "bzlib_private.h"
83
84
85 /*---------------------------------------------------*/
86 /*--- Compression stuff ---*/
87 /*---------------------------------------------------*/
88
89
90 /*---------------------------------------------------*/
91 #ifndef BZ_NO_STDIO
92 void BZ2_bz__AssertH__fail ( int errcode )
93 {
94 fprintf(stderr,
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",
103 errcode,
104 BZ2_bzlibVersion()
105 );
106 exit(3);
107 }
108 #endif
109
110
111 /*---------------------------------------------------*/
112 static
113 int bz_config_ok ( void )
114 {
115 if (sizeof(int) != 4) return 0;
116 if (sizeof(short) != 2) return 0;
117 if (sizeof(char) != 1) return 0;
118 return 1;
119 }
120
121 /*---------------------------------------------------*/
122 static
123 void* default_bzalloc ( void* opaque, Int32 items, Int32 size )
124 {
125 return ExAllocatePool( PagedPool, items * size );
126 }
127
128 static
129 void default_bzfree ( void* opaque, void* addr )
130 {
131 ExFreePool( addr );
132 }
133
134 #ifndef BZ_DECOMPRESS_ONLY
135
136 /*---------------------------------------------------*/
137 static
138 void prepare_new_block ( EState* s )
139 {
140 Int32 i;
141 s->nblock = 0;
142 s->numZ = 0;
143 s->state_out_pos = 0;
144 BZ_INITIALISE_CRC ( s->blockCRC );
145 for (i = 0; i < 256; i++) s->inUse[i] = False;
146 s->blockNo++;
147 }
148
149
150 /*---------------------------------------------------*/
151 static
152 void init_RL ( EState* s )
153 {
154 s->state_in_ch = 256;
155 s->state_in_len = 0;
156 }
157
158
159 static
160 Bool isempty_RL ( EState* s )
161 {
162 if (s->state_in_ch < 256 && s->state_in_len > 0)
163 return False; else
164 return True;
165 }
166
167
168 /*---------------------------------------------------*/
169 int BZ_API(BZ2_bzCompressInit)
170 ( bz_stream* strm,
171 int blockSize100k,
172 int verbosity,
173 int workFactor )
174 {
175 Int32 n;
176 EState* s;
177
178 if (!bz_config_ok()) return BZ_CONFIG_ERROR;
179
180 if (strm == NULL ||
181 blockSize100k < 1 || blockSize100k > 9 ||
182 workFactor < 0 || workFactor > 250)
183 return BZ_PARAM_ERROR;
184
185 if (workFactor == 0) workFactor = 30;
186 if (strm->bzalloc == NULL) strm->bzalloc = default_bzalloc;
187 if (strm->bzfree == NULL) strm->bzfree = default_bzfree;
188
189 s = BZALLOC( sizeof(EState) );
190 if (s == NULL) return BZ_MEM_ERROR;
191 s->strm = strm;
192
193 s->arr1 = NULL;
194 s->arr2 = NULL;
195 s->ftab = NULL;
196
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) );
201
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);
207 return BZ_MEM_ERROR;
208 }
209
210 s->blockNo = 0;
211 s->state = BZ_S_INPUT;
212 s->mode = BZ_M_RUNNING;
213 s->combinedCRC = 0;
214 s->blockSize100k = blockSize100k;
215 s->nblockMAX = 100000 * blockSize100k - 19;
216 s->verbosity = verbosity;
217 s->workFactor = workFactor;
218
219 s->block = (UChar*)s->arr2;
220 s->mtfv = (UInt16*)s->arr1;
221 s->zbits = NULL;
222 s->ptr = (UInt32*)s->arr1;
223
224 strm->state = s;
225 strm->total_in_lo32 = 0;
226 strm->total_in_hi32 = 0;
227 strm->total_out_lo32 = 0;
228 strm->total_out_hi32 = 0;
229 init_RL ( s );
230 prepare_new_block ( s );
231 return BZ_OK;
232 }
233
234
235 /*---------------------------------------------------*/
236 static
237 void add_pair_to_block ( EState* s )
238 {
239 Int32 i;
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 );
243 }
244 s->inUse[s->state_in_ch] = True;
245 switch (s->state_in_len) {
246 case 1:
247 s->block[s->nblock] = (UChar)ch; s->nblock++;
248 break;
249 case 2:
250 s->block[s->nblock] = (UChar)ch; s->nblock++;
251 s->block[s->nblock] = (UChar)ch; s->nblock++;
252 break;
253 case 3:
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++;
257 break;
258 default:
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));
265 s->nblock++;
266 break;
267 }
268 }
269
270
271 /*---------------------------------------------------*/
272 static
273 void flush_RL ( EState* s )
274 {
275 if (s->state_in_ch < 256) add_pair_to_block ( s );
276 init_RL ( s );
277 }
278
279
280 /*---------------------------------------------------*/
281 #define ADD_CHAR_TO_BLOCK(zs,zchh0) \
282 { \
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; \
291 zs->nblock++; \
292 zs->state_in_ch = zchh; \
293 } \
294 else \
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; \
302 } else { \
303 zs->state_in_len++; \
304 } \
305 }
306
307
308 /*---------------------------------------------------*/
309 static
310 Bool copy_input_until_stop ( EState* s )
311 {
312 Bool progress_in = False;
313
314 if (s->mode == BZ_M_RUNNING) {
315
316 /*-- fast track the common case --*/
317 while (True) {
318 /*-- block full? --*/
319 if (s->nblock >= s->nblockMAX) break;
320 /*-- no input? --*/
321 if (s->strm->avail_in == 0) break;
322 progress_in = True;
323 ADD_CHAR_TO_BLOCK ( s, (UInt32)(*((UChar*)(s->strm->next_in))) );
324 s->strm->next_in++;
325 s->strm->avail_in--;
326 s->strm->total_in_lo32++;
327 if (s->strm->total_in_lo32 == 0) s->strm->total_in_hi32++;
328 }
329
330 } else {
331
332 /*-- general, uncommon case --*/
333 while (True) {
334 /*-- block full? --*/
335 if (s->nblock >= s->nblockMAX) break;
336 /*-- no input? --*/
337 if (s->strm->avail_in == 0) break;
338 /*-- flush/finish end? --*/
339 if (s->avail_in_expect == 0) break;
340 progress_in = True;
341 ADD_CHAR_TO_BLOCK ( s, (UInt32)(*((UChar*)(s->strm->next_in))) );
342 s->strm->next_in++;
343 s->strm->avail_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--;
347 }
348 }
349 return progress_in;
350 }
351
352
353 /*---------------------------------------------------*/
354 static
355 Bool copy_output_until_stop ( EState* s )
356 {
357 Bool progress_out = False;
358
359 while (True) {
360
361 /*-- no output space? --*/
362 if (s->strm->avail_out == 0) break;
363
364 /*-- block done? --*/
365 if (s->state_out_pos >= s->numZ) break;
366
367 progress_out = True;
368 *(s->strm->next_out) = s->zbits[s->state_out_pos];
369 s->state_out_pos++;
370 s->strm->avail_out--;
371 s->strm->next_out++;
372 s->strm->total_out_lo32++;
373 if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++;
374 }
375
376 return progress_out;
377 }
378
379
380 /*---------------------------------------------------*/
381 static
382 Bool handle_compress ( bz_stream* strm )
383 {
384 Bool progress_in = False;
385 Bool progress_out = False;
386 EState* s = strm->state;
387
388 while (True) {
389
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;
401 }
402
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) {
406 flush_RL ( s );
407 BZ2_compressBlock ( s, (Bool)(s->mode == BZ_M_FINISHING) );
408 s->state = BZ_S_OUTPUT;
409 }
410 else
411 if (s->nblock >= s->nblockMAX) {
412 BZ2_compressBlock ( s, False );
413 s->state = BZ_S_OUTPUT;
414 }
415 else
416 if (s->strm->avail_in == 0) {
417 break;
418 }
419 }
420
421 }
422
423 return progress_in || progress_out;
424 }
425
426
427 /*---------------------------------------------------*/
428 int BZ_API(BZ2_bzCompress) ( bz_stream *strm, int action )
429 {
430 Bool progress;
431 EState* s;
432 if (strm == NULL) return BZ_PARAM_ERROR;
433 s = strm->state;
434 if (s == NULL) return BZ_PARAM_ERROR;
435 if (s->strm != strm) return BZ_PARAM_ERROR;
436
437 preswitch:
438 switch (s->mode) {
439
440 case BZ_M_IDLE:
441 return BZ_SEQUENCE_ERROR;
442
443 case BZ_M_RUNNING:
444 if (action == BZ_RUN) {
445 progress = handle_compress ( strm );
446 return progress ? BZ_RUN_OK : BZ_PARAM_ERROR;
447 }
448 else
449 if (action == BZ_FLUSH) {
450 s->avail_in_expect = strm->avail_in;
451 s->mode = BZ_M_FLUSHING;
452 goto preswitch;
453 }
454 else
455 if (action == BZ_FINISH) {
456 s->avail_in_expect = strm->avail_in;
457 s->mode = BZ_M_FINISHING;
458 goto preswitch;
459 }
460 else
461 return BZ_PARAM_ERROR;
462
463 case BZ_M_FLUSHING:
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;
471 return BZ_RUN_OK;
472
473 case BZ_M_FINISHING:
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;
481 s->mode = BZ_M_IDLE;
482 return BZ_STREAM_END;
483 }
484 return BZ_OK; /*--not reached--*/
485 }
486
487
488 /*---------------------------------------------------*/
489 int BZ_API(BZ2_bzCompressEnd) ( bz_stream *strm )
490 {
491 EState* s;
492 if (strm == NULL) return BZ_PARAM_ERROR;
493 s = strm->state;
494 if (s == NULL) return BZ_PARAM_ERROR;
495 if (s->strm != strm) return BZ_PARAM_ERROR;
496
497 if (s->arr1 != NULL) BZFREE(s->arr1);
498 if (s->arr2 != NULL) BZFREE(s->arr2);
499 if (s->ftab != NULL) BZFREE(s->ftab);
500 BZFREE(strm->state);
501
502 strm->state = NULL;
503
504 return BZ_OK;
505 }
506
507 #endif // BZ_DECOMPRESS_ONLY
508
509 /*---------------------------------------------------*/
510 /*--- Decompression stuff ---*/
511 /*---------------------------------------------------*/
512
513 /*---------------------------------------------------*/
514 int BZ_API(BZ2_bzDecompressInit)
515 ( bz_stream* strm,
516 int verbosity,
517 int small )
518 {
519 DState* s;
520
521 if (!bz_config_ok()) return BZ_CONFIG_ERROR;
522
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;
526
527 if (strm->bzalloc == NULL) strm->bzalloc = default_bzalloc;
528 if (strm->bzfree == NULL) strm->bzfree = default_bzfree;
529
530 s = BZALLOC( sizeof(DState) );
531 if (s == NULL) return BZ_MEM_ERROR;
532 s->strm = strm;
533 strm->state = s;
534 s->state = BZ_X_MAGIC_1;
535 s->bsLive = 0;
536 s->bsBuff = 0;
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;
543 s->ll4 = NULL;
544 s->ll16 = NULL;
545 s->tt = NULL;
546 s->currBlockNo = 0;
547 s->verbosity = verbosity;
548
549 return BZ_OK;
550 }
551
552 #ifndef BZ_DECOMPRESS_ONLY
553
554 /*---------------------------------------------------*/
555 static
556 void unRLE_obuf_to_output_FAST ( DState* s )
557 {
558 UChar k1;
559
560 if (s->blockRandomised) {
561
562 while (True) {
563 /* try to finish existing run */
564 while (True) {
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 );
569 s->state_out_len--;
570 s->strm->next_out++;
571 s->strm->avail_out--;
572 s->strm->total_out_lo32++;
573 if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++;
574 }
575
576 /* can a new run be started? */
577 if (s->nblock_used == s->save_nblock+1) return;
578
579
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; };
586
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; };
592
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; };
598
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++;
604 }
605
606 } else {
607
608 /* restore */
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;
613 Int32 c_k0 = s->k0;
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;
618 /* end restore */
619
620 UInt32 avail_out_INIT = cs_avail_out;
621 Int32 s_save_nblockPP = s->save_nblock+1;
622 unsigned int total_out_lo32_old;
623
624 while (True) {
625
626 /* try to finish existing run */
627 if (c_state_out_len > 0) {
628 while (True) {
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 );
633 c_state_out_len--;
634 cs_next_out++;
635 cs_avail_out--;
636 }
637 s_state_out_len_eq_one:
638 {
639 if (cs_avail_out == 0) {
640 c_state_out_len = 1; goto return_notr;
641 };
642 *( (UChar*)(cs_next_out) ) = c_state_out_ch;
643 BZ_UPDATE_CRC ( c_calculatedBlockCRC, c_state_out_ch );
644 cs_next_out++;
645 cs_avail_out--;
646 }
647 }
648 /* can a new run be started? */
649 if (c_nblock_used == s_save_nblockPP) {
650 c_state_out_len = 0; goto return_notr;
651 };
652 c_state_out_ch = c_k0;
653 BZ_GET_FAST_C(k1); c_nblock_used++;
654 if (k1 != c_k0) {
655 c_k0 = k1; goto s_state_out_len_eq_one;
656 };
657 if (c_nblock_used == s_save_nblockPP)
658 goto s_state_out_len_eq_one;
659
660 c_state_out_len = 2;
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; };
664
665 c_state_out_len = 3;
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; };
669
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++;
673 }
674
675 return_notr:
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++;
680
681 /* save */
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;
686 s->k0 = c_k0;
687 s->tt = c_tt;
688 s->tPos = c_tPos;
689 s->strm->next_out = cs_next_out;
690 s->strm->avail_out = cs_avail_out;
691 /* end save */
692 }
693 }
694
695 #endif // BZ_DECOMPRESS_ONLY
696
697 /*---------------------------------------------------*/
698 __inline__ Int32 BZ2_indexIntoF ( Int32 indx, Int32 *cftab )
699 {
700 Int32 nb, na, mid;
701 nb = 0;
702 na = 256;
703 do {
704 mid = (nb + na) >> 1;
705 if (indx >= cftab[mid]) nb = mid; else na = mid;
706 }
707 while (na - nb != 1);
708 return nb;
709 }
710
711 /*---------------------------------------------------*/
712 static
713 void unRLE_obuf_to_output_SMALL ( DState* s )
714 {
715 UChar k1;
716
717 if (s->blockRandomised) {
718
719 while (True) {
720 /* try to finish existing run */
721 while (True) {
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 );
726 s->state_out_len--;
727 s->strm->next_out++;
728 s->strm->avail_out--;
729 s->strm->total_out_lo32++;
730 if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++;
731 }
732
733 /* can a new run be started? */
734 if (s->nblock_used == s->save_nblock+1) return;
735
736
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; };
743
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; };
749
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; };
755
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++;
761 }
762
763 } else {
764
765 while (True) {
766 /* try to finish existing run */
767 while (True) {
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 );
772 s->state_out_len--;
773 s->strm->next_out++;
774 s->strm->avail_out--;
775 s->strm->total_out_lo32++;
776 if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++;
777 }
778
779 /* can a new run be started? */
780 if (s->nblock_used == s->save_nblock+1) return;
781
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; };
787
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; };
792
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; };
797
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++;
801 }
802
803 }
804 }
805
806 /*---------------------------------------------------*/
807 int BZ_API(BZ2_bzDecompress) ( bz_stream *strm )
808 {
809 DState* s;
810 if (strm == NULL) return BZ_PARAM_ERROR;
811 s = strm->state;
812 if (s == NULL) return BZ_PARAM_ERROR;
813 if (s->strm != strm) return BZ_PARAM_ERROR;
814
815 while (True) {
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 );
822 #else
823 unRLE_obuf_to_output_SMALL ( s );
824 #endif
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;
838 } else {
839 return BZ_OK;
840 }
841 }
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;
850 return r;
851 }
852 if (s->state != BZ_X_OUTPUT) return r;
853 }
854 }
855
856 AssertH ( 0, 6001 );
857
858 return 0; /*NOTREACHED*/
859 }
860
861
862 /*---------------------------------------------------*/
863 int BZ_API(BZ2_bzDecompressEnd) ( bz_stream *strm )
864 {
865 DState* s;
866 if (strm == NULL) return BZ_PARAM_ERROR;
867 s = strm->state;
868 if (s == NULL) return BZ_PARAM_ERROR;
869 if (s->strm != strm) return BZ_PARAM_ERROR;
870
871 if (s->tt != NULL) BZFREE(s->tt);
872 if (s->ll16 != NULL) BZFREE(s->ll16);
873 if (s->ll4 != NULL) BZFREE(s->ll4);
874
875 BZFREE(strm->state);
876 strm->state = NULL;
877
878 return BZ_OK;
879 }
880
881
882 #ifndef BZ_NO_STDIO
883 /*---------------------------------------------------*/
884 /*--- File I/O stuff ---*/
885 /*---------------------------------------------------*/
886
887 #define BZ_SETERR(eee) \
888 { \
889 if (bzerror != NULL) *bzerror = eee; \
890 if (bzf != NULL) bzf->lastErr = eee; \
891 }
892
893 typedef
894 struct {
895 FILE* handle;
896 Char buf[BZ_MAX_UNUSED];
897 Int32 bufN;
898 Bool writing;
899 bz_stream strm;
900 Int32 lastErr;
901 Bool initialisedOk;
902 }
903 bzFile;
904
905
906 /*---------------------------------------------*/
907 static Bool myfeof ( FILE* f )
908 {
909 Int32 c = fgetc ( f );
910 if (c == EOF) return True;
911 ungetc ( c, f );
912 return False;
913 }
914
915
916 /*---------------------------------------------------*/
917 BZFILE* BZ_API(BZ2_bzWriteOpen)
918 ( int* bzerror,
919 FILE* f,
920 int blockSize100k,
921 int verbosity,
922 int workFactor )
923 {
924 Int32 ret;
925 bzFile* bzf = NULL;
926
927 BZ_SETERR(BZ_OK);
928
929 if (f == NULL ||
930 (blockSize100k < 1 || blockSize100k > 9) ||
931 (workFactor < 0 || workFactor > 250) ||
932 (verbosity < 0 || verbosity > 4))
933 { BZ_SETERR(BZ_PARAM_ERROR); return NULL; };
934
935 if (ferror(f))
936 { BZ_SETERR(BZ_IO_ERROR); return NULL; };
937
938 bzf = malloc ( sizeof(bzFile) );
939 if (bzf == NULL)
940 { BZ_SETERR(BZ_MEM_ERROR); return NULL; };
941
942 BZ_SETERR(BZ_OK);
943 bzf->initialisedOk = False;
944 bzf->bufN = 0;
945 bzf->handle = f;
946 bzf->writing = True;
947 bzf->strm.bzalloc = NULL;
948 bzf->strm.bzfree = NULL;
949 bzf->strm.opaque = NULL;
950
951 if (workFactor == 0) workFactor = 30;
952 ret = BZ2_bzCompressInit ( &(bzf->strm), blockSize100k,
953 verbosity, workFactor );
954 if (ret != BZ_OK)
955 { BZ_SETERR(ret); free(bzf); return NULL; };
956
957 bzf->strm.avail_in = 0;
958 bzf->initialisedOk = True;
959 return bzf;
960 }
961
962
963
964 /*---------------------------------------------------*/
965 void BZ_API(BZ2_bzWrite)
966 ( int* bzerror,
967 BZFILE* b,
968 void* buf,
969 int len )
970 {
971 Int32 n, n2, ret;
972 bzFile* bzf = (bzFile*)b;
973
974 BZ_SETERR(BZ_OK);
975 if (bzf == NULL || buf == NULL || len < 0)
976 { BZ_SETERR(BZ_PARAM_ERROR); return; };
977 if (!(bzf->writing))
978 { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
979 if (ferror(bzf->handle))
980 { BZ_SETERR(BZ_IO_ERROR); return; };
981
982 if (len == 0)
983 { BZ_SETERR(BZ_OK); return; };
984
985 bzf->strm.avail_in = len;
986 bzf->strm.next_in = buf;
987
988 while (True) {
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; };
994
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),
998 n, bzf->handle );
999 if (n != n2 || ferror(bzf->handle))
1000 { BZ_SETERR(BZ_IO_ERROR); return; };
1001 }
1002
1003 if (bzf->strm.avail_in == 0)
1004 { BZ_SETERR(BZ_OK); return; };
1005 }
1006 }
1007
1008
1009 /*---------------------------------------------------*/
1010 void BZ_API(BZ2_bzWriteClose)
1011 ( int* bzerror,
1012 BZFILE* b,
1013 int abandon,
1014 unsigned int* nbytes_in,
1015 unsigned int* nbytes_out )
1016 {
1017 BZ2_bzWriteClose64 ( bzerror, b, abandon,
1018 nbytes_in, NULL, nbytes_out, NULL );
1019 }
1020
1021
1022 void BZ_API(BZ2_bzWriteClose64)
1023 ( int* bzerror,
1024 BZFILE* b,
1025 int abandon,
1026 unsigned int* nbytes_in_lo32,
1027 unsigned int* nbytes_in_hi32,
1028 unsigned int* nbytes_out_lo32,
1029 unsigned int* nbytes_out_hi32 )
1030 {
1031 Int32 n, n2, ret;
1032 bzFile* bzf = (bzFile*)b;
1033
1034 if (bzf == NULL)
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; };
1040
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;
1045
1046 if ((!abandon) && bzf->lastErr == BZ_OK) {
1047 while (True) {
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; };
1053
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),
1057 n, bzf->handle );
1058 if (n != n2 || ferror(bzf->handle))
1059 { BZ_SETERR(BZ_IO_ERROR); return; };
1060 }
1061
1062 if (ret == BZ_STREAM_END) break;
1063 }
1064 }
1065
1066 if ( !abandon && !ferror ( bzf->handle ) ) {
1067 fflush ( bzf->handle );
1068 if (ferror(bzf->handle))
1069 { BZ_SETERR(BZ_IO_ERROR); return; };
1070 }
1071
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;
1080
1081 BZ_SETERR(BZ_OK);
1082 BZ2_bzCompressEnd ( &(bzf->strm) );
1083 free ( bzf );
1084 }
1085
1086
1087 /*---------------------------------------------------*/
1088 BZFILE* BZ_API(BZ2_bzReadOpen)
1089 ( int* bzerror,
1090 FILE* f,
1091 int verbosity,
1092 int small,
1093 void* unused,
1094 int nUnused )
1095 {
1096 bzFile* bzf = NULL;
1097 int ret;
1098
1099 BZ_SETERR(BZ_OK);
1100
1101 if (f == NULL ||
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; };
1107
1108 if (ferror(f))
1109 { BZ_SETERR(BZ_IO_ERROR); return NULL; };
1110
1111 bzf = malloc ( sizeof(bzFile) );
1112 if (bzf == NULL)
1113 { BZ_SETERR(BZ_MEM_ERROR); return NULL; };
1114
1115 BZ_SETERR(BZ_OK);
1116
1117 bzf->initialisedOk = False;
1118 bzf->handle = f;
1119 bzf->bufN = 0;
1120 bzf->writing = False;
1121 bzf->strm.bzalloc = NULL;
1122 bzf->strm.bzfree = NULL;
1123 bzf->strm.opaque = NULL;
1124
1125 while (nUnused > 0) {
1126 bzf->buf[bzf->bufN] = *((UChar*)(unused)); bzf->bufN++;
1127 unused = ((void*)( 1 + ((UChar*)(unused)) ));
1128 nUnused--;
1129 }
1130
1131 ret = BZ2_bzDecompressInit ( &(bzf->strm), verbosity, small );
1132 if (ret != BZ_OK)
1133 { BZ_SETERR(ret); free(bzf); return NULL; };
1134
1135 bzf->strm.avail_in = bzf->bufN;
1136 bzf->strm.next_in = bzf->buf;
1137
1138 bzf->initialisedOk = True;
1139 return bzf;
1140 }
1141
1142
1143 /*---------------------------------------------------*/
1144 void BZ_API(BZ2_bzReadClose) ( int *bzerror, BZFILE *b )
1145 {
1146 bzFile* bzf = (bzFile*)b;
1147
1148 BZ_SETERR(BZ_OK);
1149 if (bzf == NULL)
1150 { BZ_SETERR(BZ_OK); return; };
1151
1152 if (bzf->writing)
1153 { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
1154
1155 if (bzf->initialisedOk)
1156 (void)BZ2_bzDecompressEnd ( &(bzf->strm) );
1157 free ( bzf );
1158 }
1159
1160
1161 /*---------------------------------------------------*/
1162 int BZ_API(BZ2_bzRead)
1163 ( int* bzerror,
1164 BZFILE* b,
1165 void* buf,
1166 int len )
1167 {
1168 Int32 n, ret;
1169 bzFile* bzf = (bzFile*)b;
1170
1171 BZ_SETERR(BZ_OK);
1172
1173 if (bzf == NULL || buf == NULL || len < 0)
1174 { BZ_SETERR(BZ_PARAM_ERROR); return 0; };
1175
1176 if (bzf->writing)
1177 { BZ_SETERR(BZ_SEQUENCE_ERROR); return 0; };
1178
1179 if (len == 0)
1180 { BZ_SETERR(BZ_OK); return 0; };
1181
1182 bzf->strm.avail_out = len;
1183 bzf->strm.next_out = buf;
1184
1185 while (True) {
1186
1187 if (ferror(bzf->handle))
1188 { BZ_SETERR(BZ_IO_ERROR); return 0; };
1189
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; };
1195 bzf->bufN = n;
1196 bzf->strm.avail_in = bzf->bufN;
1197 bzf->strm.next_in = bzf->buf;
1198 }
1199
1200 ret = BZ2_bzDecompress ( &(bzf->strm) );
1201
1202 if (ret != BZ_OK && ret != BZ_STREAM_END)
1203 { BZ_SETERR(ret); return 0; };
1204
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; };
1208
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; };
1214
1215 }
1216
1217 return 0; /*not reached*/
1218 }
1219
1220
1221 /*---------------------------------------------------*/
1222 void BZ_API(BZ2_bzReadGetUnused)
1223 ( int* bzerror,
1224 BZFILE* b,
1225 void** unused,
1226 int* nUnused )
1227 {
1228 bzFile* bzf = (bzFile*)b;
1229 if (bzf == NULL)
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; };
1235
1236 BZ_SETERR(BZ_OK);
1237 *nUnused = bzf->strm.avail_in;
1238 *unused = bzf->strm.next_in;
1239 }
1240 #endif
1241
1242 #ifndef BZ_DECOMPRESS_ONLY
1243
1244 /*---------------------------------------------------*/
1245 /*--- Misc convenience stuff ---*/
1246 /*---------------------------------------------------*/
1247
1248 /*---------------------------------------------------*/
1249 int BZ_API(BZ2_bzBuffToBuffCompress)
1250 ( char* dest,
1251 unsigned int* destLen,
1252 char* source,
1253 unsigned int sourceLen,
1254 int blockSize100k,
1255 int verbosity,
1256 int workFactor )
1257 {
1258 bz_stream strm;
1259 int ret;
1260 CHECKPOINT;
1261 if (dest == NULL || destLen == NULL ||
1262 source == NULL ||
1263 blockSize100k < 1 || blockSize100k > 9 ||
1264 verbosity < 0 || verbosity > 4 ||
1265 workFactor < 0 || workFactor > 250)
1266 return BZ_PARAM_ERROR;
1267
1268 if (workFactor == 0) workFactor = 30;
1269 strm.bzalloc = NULL;
1270 strm.bzfree = NULL;
1271 strm.opaque = NULL;
1272 CHECKPOINT;
1273 ret = BZ2_bzCompressInit ( &strm, blockSize100k,
1274 verbosity, workFactor );
1275 if (ret != BZ_OK) return ret;
1276 CHECKPOINT;
1277 strm.next_in = source;
1278 strm.next_out = dest;
1279 strm.avail_in = sourceLen;
1280 strm.avail_out = *destLen;
1281
1282 ret = BZ2_bzCompress ( &strm, BZ_FINISH );
1283 if (ret == BZ_FINISH_OK) goto output_overflow;
1284 if (ret != BZ_STREAM_END) goto errhandler;
1285
1286 /* normal termination */
1287 *destLen -= strm.avail_out;
1288 CHECKPOINT;
1289 BZ2_bzCompressEnd ( &strm );
1290 return BZ_OK;
1291
1292 output_overflow:
1293 CHECKPOINT;
1294 BZ2_bzCompressEnd ( &strm );
1295 return BZ_OUTBUFF_FULL;
1296
1297 errhandler:
1298 CHECKPOINT;
1299 BZ2_bzCompressEnd ( &strm );
1300 return ret;
1301 }
1302
1303 #endif // BZ_DECOMPRESS_0NLY
1304
1305 /*---------------------------------------------------*/
1306 int BZ_API(BZ2_bzBuffToBuffDecompress)
1307 ( char* dest,
1308 unsigned int* destLen,
1309 char* source,
1310 unsigned int sourceLen,
1311 int small,
1312 int verbosity )
1313 {
1314 bz_stream strm;
1315 int ret;
1316
1317 if (dest == NULL || destLen == NULL ||
1318 source == NULL ||
1319 (small != 0 && small != 1) ||
1320 verbosity < 0 || verbosity > 4)
1321 return BZ_PARAM_ERROR;
1322
1323 strm.bzalloc = NULL;
1324 strm.bzfree = NULL;
1325 strm.opaque = NULL;
1326 ret = BZ2_bzDecompressInit ( &strm, verbosity, small );
1327 if (ret != BZ_OK) return ret;
1328
1329 strm.next_in = source;
1330 strm.next_out = dest;
1331 strm.avail_in = sourceLen;
1332 strm.avail_out = *destLen;
1333
1334 ret = BZ2_bzDecompress ( &strm );
1335 if (ret == BZ_OK) goto output_overflow_or_eof;
1336 if (ret != BZ_STREAM_END) goto errhandler;
1337
1338 /* normal termination */
1339 *destLen -= strm.avail_out;
1340 BZ2_bzDecompressEnd ( &strm );
1341 return BZ_OK;
1342
1343 output_overflow_or_eof:
1344 if (strm.avail_out > 0) {
1345 BZ2_bzDecompressEnd ( &strm );
1346 return BZ_UNEXPECTED_EOF;
1347 } else {
1348 BZ2_bzDecompressEnd ( &strm );
1349 return BZ_OUTBUFF_FULL;
1350 };
1351
1352 errhandler:
1353 BZ2_bzDecompressEnd ( &strm );
1354 return ret;
1355 }
1356
1357
1358 /*---------------------------------------------------*/
1359 /*--
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.
1367 --*/
1368 /*---------------------------------------------------*/
1369
1370 /*---------------------------------------------------*/
1371 /*--
1372 return version like "0.9.0c".
1373 --*/
1374 const char * BZ_API(BZ2_bzlibVersion)(void)
1375 {
1376 return BZ_VERSION;
1377 }
1378
1379
1380 #ifndef BZ_NO_STDIO
1381 /*---------------------------------------------------*/
1382
1383 #if defined(_WIN32) || defined(OS2) || defined(MSDOS)
1384 # include <fcntl.h>
1385 # include <io.h>
1386 # define SET_BINARY_MODE(file) setmode(fileno(file),O_BINARY)
1387 #else
1388 # define SET_BINARY_MODE(file)
1389 #endif
1390 static
1391 BZFILE * bzopen_or_bzdopen
1392 ( const char *path, /* no use when bzdopen */
1393 int fd, /* no use when bzdopen */
1394 const char *mode,
1395 int open_mode) /* bzopen: 0, bzdopen:1 */
1396 {
1397 int bzerr;
1398 char unused[BZ_MAX_UNUSED];
1399 int blockSize100k = 9;
1400 int writing = 0;
1401 char mode2[10] = "";
1402 FILE *fp = NULL;
1403 BZFILE *bzfp = NULL;
1404 int verbosity = 0;
1405 int workFactor = 30;
1406 int smallMode = 0;
1407 int nUnused = 0;
1408
1409 if (mode == NULL) return NULL;
1410 while (*mode) {
1411 switch (*mode) {
1412 case 'r':
1413 writing = 0; break;
1414 case 'w':
1415 writing = 1; break;
1416 case 's':
1417 smallMode = 1; break;
1418 default:
1419 if (isdigit((int)(*mode))) {
1420 blockSize100k = *mode-'0';
1421 }
1422 }
1423 mode++;
1424 }
1425 strcat(mode2, writing ? "w" : "r" );
1426 strcat(mode2,"b"); /* binary mode */
1427
1428 if (open_mode==0) {
1429 if (path==NULL || strcmp(path,"")==0) {
1430 fp = (writing ? stdout : stdin);
1431 SET_BINARY_MODE(fp);
1432 } else {
1433 fp = fopen(path,mode2);
1434 }
1435 } else {
1436 #ifdef BZ_STRICT_ANSI
1437 fp = NULL;
1438 #else
1439 fp = fdopen(fd,mode2);
1440 #endif
1441 }
1442 if (fp == NULL) return NULL;
1443
1444 if (writing) {
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);
1450 } else {
1451 bzfp = BZ2_bzReadOpen(&bzerr,fp,verbosity,smallMode,
1452 unused,nUnused);
1453 }
1454 if (bzfp == NULL) {
1455 if (fp != stdin && fp != stdout) fclose(fp);
1456 return NULL;
1457 }
1458 return bzfp;
1459 }
1460
1461
1462 /*---------------------------------------------------*/
1463 /*--
1464 open file for read or write.
1465 ex) bzopen("file","w9")
1466 case path="" or NULL => use stdin or stdout.
1467 --*/
1468 BZFILE * BZ_API(BZ2_bzopen)
1469 ( const char *path,
1470 const char *mode )
1471 {
1472 return bzopen_or_bzdopen(path,-1,mode,/*bzopen*/0);
1473 }
1474
1475
1476 /*---------------------------------------------------*/
1477 BZFILE * BZ_API(BZ2_bzdopen)
1478 ( int fd,
1479 const char *mode )
1480 {
1481 return bzopen_or_bzdopen(NULL,fd,mode,/*bzdopen*/1);
1482 }
1483
1484
1485 /*---------------------------------------------------*/
1486 int BZ_API(BZ2_bzread) (BZFILE* b, void* buf, int len )
1487 {
1488 int bzerr, nread;
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) {
1492 return nread;
1493 } else {
1494 return -1;
1495 }
1496 }
1497
1498
1499 /*---------------------------------------------------*/
1500 int BZ_API(BZ2_bzwrite) (BZFILE* b, void* buf, int len )
1501 {
1502 int bzerr;
1503
1504 BZ2_bzWrite(&bzerr,b,buf,len);
1505 if(bzerr == BZ_OK){
1506 return len;
1507 }else{
1508 return -1;
1509 }
1510 }
1511
1512
1513 /*---------------------------------------------------*/
1514 int BZ_API(BZ2_bzflush) (BZFILE *b)
1515 {
1516 /* do nothing now... */
1517 return 0;
1518 }
1519
1520
1521 /*---------------------------------------------------*/
1522 void BZ_API(BZ2_bzclose) (BZFILE* b)
1523 {
1524 int bzerr;
1525 FILE *fp = ((bzFile *)b)->handle;
1526
1527 if (b==NULL) {return;}
1528 if(((bzFile*)b)->writing){
1529 BZ2_bzWriteClose(&bzerr,b,0,NULL,NULL);
1530 if(bzerr != BZ_OK){
1531 BZ2_bzWriteClose(NULL,b,1,NULL,NULL);
1532 }
1533 }else{
1534 BZ2_bzReadClose(&bzerr,b);
1535 }
1536 if(fp!=stdin && fp!=stdout){
1537 fclose(fp);
1538 }
1539 }
1540
1541
1542 /*---------------------------------------------------*/
1543 /*--
1544 return last error code
1545 --*/
1546 static char *bzerrorstrings[] = {
1547 "OK"
1548 ,"SEQUENCE_ERROR"
1549 ,"PARAM_ERROR"
1550 ,"MEM_ERROR"
1551 ,"DATA_ERROR"
1552 ,"DATA_ERROR_MAGIC"
1553 ,"IO_ERROR"
1554 ,"UNEXPECTED_EOF"
1555 ,"OUTBUFF_FULL"
1556 ,"CONFIG_ERROR"
1557 ,"???" /* for future */
1558 ,"???" /* for future */
1559 ,"???" /* for future */
1560 ,"???" /* for future */
1561 ,"???" /* for future */
1562 ,"???" /* for future */
1563 };
1564
1565
1566 const char * BZ_API(BZ2_bzerror) (BZFILE *b, int *errnum)
1567 {
1568 int err = ((bzFile *)b)->lastErr;
1569
1570 if(err>0) err = 0;
1571 *errnum = err;
1572 return bzerrorstrings[err*-1];
1573 }
1574 #endif
1575
1576
1577 /*-------------------------------------------------------------*/
1578 /*--- end bzlib.c ---*/
1579 /*-------------------------------------------------------------*/