Commit f071c501901281ae2de7a372ec12270dce91c426
Committed by
Tom Rini
1 parent
48aead71c1
Exists in
v2017.01-smarct4x
and in
37 other branches
lib: bzip2: move bzip2 files to lib/bzip2/ directory
Signed-off-by: Masahiro Yamada <yamada.m@jp.panasonic.com>
Showing 14 changed files with 3307 additions and 3309 deletions Side-by-side Diff
- lib/Makefile
- lib/bzip2/Makefile
- lib/bzip2/bzlib.c
- lib/bzip2/bzlib_crctable.c
- lib/bzip2/bzlib_decompress.c
- lib/bzip2/bzlib_huffman.c
- lib/bzip2/bzlib_private.h
- lib/bzip2/bzlib_randtable.c
- lib/bzlib.c
- lib/bzlib_crctable.c
- lib/bzlib_decompress.c
- lib/bzlib_huffman.c
- lib/bzlib_private.h
- lib/bzlib_randtable.c
lib/Makefile
... | ... | @@ -11,14 +11,10 @@ |
11 | 11 | obj-$(CONFIG_LZMA) += lzma/ |
12 | 12 | obj-$(CONFIG_LZO) += lzo/ |
13 | 13 | obj-$(CONFIG_ZLIB) += zlib/ |
14 | +obj-$(CONFIG_BZIP2) += bzip2/ | |
14 | 15 | obj-$(CONFIG_TIZEN) += tizen/ |
15 | 16 | |
16 | 17 | obj-$(CONFIG_AES) += aes.o |
17 | -obj-$(CONFIG_BZIP2) += bzlib.o | |
18 | -obj-$(CONFIG_BZIP2) += bzlib_crctable.o | |
19 | -obj-$(CONFIG_BZIP2) += bzlib_decompress.o | |
20 | -obj-$(CONFIG_BZIP2) += bzlib_randtable.o | |
21 | -obj-$(CONFIG_BZIP2) += bzlib_huffman.o | |
22 | 18 | obj-$(CONFIG_USB_TTY) += circbuf.o |
23 | 19 | obj-y += crc7.o |
24 | 20 | obj-y += crc8.o |
lib/bzip2/Makefile
lib/bzip2/bzlib.c
Changes suppressed. Click to show
1 | +#include <config.h> | |
2 | +#include <common.h> | |
3 | +#include <watchdog.h> | |
4 | + | |
5 | +/* | |
6 | + * This file is a modified version of bzlib.c from the bzip2-1.0.2 | |
7 | + * distribution which can be found at http://sources.redhat.com/bzip2/ | |
8 | + */ | |
9 | + | |
10 | +/*-------------------------------------------------------------*/ | |
11 | +/*--- Library top-level functions. ---*/ | |
12 | +/*--- bzlib.c ---*/ | |
13 | +/*-------------------------------------------------------------*/ | |
14 | + | |
15 | +/*-- | |
16 | + This file is a part of bzip2 and/or libbzip2, a program and | |
17 | + library for lossless, block-sorting data compression. | |
18 | + | |
19 | + Copyright (C) 1996-2002 Julian R Seward. All rights reserved. | |
20 | + | |
21 | + Redistribution and use in source and binary forms, with or without | |
22 | + modification, are permitted provided that the following conditions | |
23 | + are met: | |
24 | + | |
25 | + 1. Redistributions of source code must retain the above copyright | |
26 | + notice, this list of conditions and the following disclaimer. | |
27 | + | |
28 | + 2. The origin of this software must not be misrepresented; you must | |
29 | + not claim that you wrote the original software. If you use this | |
30 | + software in a product, an acknowledgment in the product | |
31 | + documentation would be appreciated but is not required. | |
32 | + | |
33 | + 3. Altered source versions must be plainly marked as such, and must | |
34 | + not be misrepresented as being the original software. | |
35 | + | |
36 | + 4. The name of the author may not be used to endorse or promote | |
37 | + products derived from this software without specific prior written | |
38 | + permission. | |
39 | + | |
40 | + THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS | |
41 | + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
42 | + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
43 | + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY | |
44 | + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
45 | + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE | |
46 | + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
47 | + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
48 | + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
49 | + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
50 | + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
51 | + | |
52 | + Julian Seward, Cambridge, UK. | |
53 | + jseward@acm.org | |
54 | + bzip2/libbzip2 version 1.0 of 21 March 2000 | |
55 | + | |
56 | + This program is based on (at least) the work of: | |
57 | + Mike Burrows | |
58 | + David Wheeler | |
59 | + Peter Fenwick | |
60 | + Alistair Moffat | |
61 | + Radford Neal | |
62 | + Ian H. Witten | |
63 | + Robert Sedgewick | |
64 | + Jon L. Bentley | |
65 | + | |
66 | + For more information on these sources, see the manual. | |
67 | +--*/ | |
68 | + | |
69 | +/*-- | |
70 | + CHANGES | |
71 | + ~~~~~~~ | |
72 | + 0.9.0 -- original version. | |
73 | + | |
74 | + 0.9.0a/b -- no changes in this file. | |
75 | + | |
76 | + 0.9.0c | |
77 | + * made zero-length BZ_FLUSH work correctly in bzCompress(). | |
78 | + * fixed bzWrite/bzRead to ignore zero-length requests. | |
79 | + * fixed bzread to correctly handle read requests after EOF. | |
80 | + * wrong parameter order in call to bzDecompressInit in | |
81 | + bzBuffToBuffDecompress. Fixed. | |
82 | +--*/ | |
83 | + | |
84 | +#include "bzlib_private.h" | |
85 | + | |
86 | +/*---------------------------------------------------*/ | |
87 | +/*--- Compression stuff ---*/ | |
88 | +/*---------------------------------------------------*/ | |
89 | + | |
90 | + | |
91 | +/*---------------------------------------------------*/ | |
92 | +#ifndef BZ_NO_STDIO | |
93 | +void BZ2_bz__AssertH__fail ( int errcode ) | |
94 | +{ | |
95 | + fprintf(stderr, | |
96 | + "\n\nbzip2/libbzip2: internal error number %d.\n" | |
97 | + "This is a bug in bzip2/libbzip2, %s.\n" | |
98 | + "Please report it to me at: jseward@acm.org. If this happened\n" | |
99 | + "when you were using some program which uses libbzip2 as a\n" | |
100 | + "component, you should also report this bug to the author(s)\n" | |
101 | + "of that program. Please make an effort to report this bug;\n" | |
102 | + "timely and accurate bug reports eventually lead to higher\n" | |
103 | + "quality software. Thanks. Julian Seward, 30 December 2001.\n\n", | |
104 | + errcode, | |
105 | + BZ2_bzlibVersion() | |
106 | + ); | |
107 | + | |
108 | + if (errcode == 1007) { | |
109 | + fprintf(stderr, | |
110 | + "\n*** A special note about internal error number 1007 ***\n" | |
111 | + "\n" | |
112 | + "Experience suggests that a common cause of i.e. 1007\n" | |
113 | + "is unreliable memory or other hardware. The 1007 assertion\n" | |
114 | + "just happens to cross-check the results of huge numbers of\n" | |
115 | + "memory reads/writes, and so acts (unintendedly) as a stress\n" | |
116 | + "test of your memory system.\n" | |
117 | + "\n" | |
118 | + "I suggest the following: try compressing the file again,\n" | |
119 | + "possibly monitoring progress in detail with the -vv flag.\n" | |
120 | + "\n" | |
121 | + "* If the error cannot be reproduced, and/or happens at different\n" | |
122 | + " points in compression, you may have a flaky memory system.\n" | |
123 | + " Try a memory-test program. I have used Memtest86\n" | |
124 | + " (www.memtest86.com). At the time of writing it is free (GPLd).\n" | |
125 | + " Memtest86 tests memory much more thorougly than your BIOSs\n" | |
126 | + " power-on test, and may find failures that the BIOS doesn't.\n" | |
127 | + "\n" | |
128 | + "* If the error can be repeatably reproduced, this is a bug in\n" | |
129 | + " bzip2, and I would very much like to hear about it. Please\n" | |
130 | + " let me know, and, ideally, save a copy of the file causing the\n" | |
131 | + " problem -- without which I will be unable to investigate it.\n" | |
132 | + "\n" | |
133 | + ); | |
134 | + } | |
135 | + | |
136 | + exit(3); | |
137 | +} | |
138 | +#endif | |
139 | + | |
140 | + | |
141 | +/*---------------------------------------------------*/ | |
142 | +static | |
143 | +int bz_config_ok ( void ) | |
144 | +{ | |
145 | + if (sizeof(int) != 4) return 0; | |
146 | + if (sizeof(short) != 2) return 0; | |
147 | + if (sizeof(char) != 1) return 0; | |
148 | + return 1; | |
149 | +} | |
150 | + | |
151 | + | |
152 | +/*---------------------------------------------------*/ | |
153 | +static | |
154 | +void* default_bzalloc ( void* opaque, Int32 items, Int32 size ) | |
155 | +{ | |
156 | + void* v = malloc ( items * size ); | |
157 | + return v; | |
158 | +} | |
159 | + | |
160 | +static | |
161 | +void default_bzfree ( void* opaque, void* addr ) | |
162 | +{ | |
163 | + if (addr != NULL) free ( addr ); | |
164 | +} | |
165 | + | |
166 | +#ifndef BZ_NO_COMPRESS | |
167 | +/*---------------------------------------------------*/ | |
168 | +static | |
169 | +void prepare_new_block ( EState* s ) | |
170 | +{ | |
171 | + Int32 i; | |
172 | + s->nblock = 0; | |
173 | + s->numZ = 0; | |
174 | + s->state_out_pos = 0; | |
175 | + BZ_INITIALISE_CRC ( s->blockCRC ); | |
176 | + for (i = 0; i < 256; i++) s->inUse[i] = False; | |
177 | + s->blockNo++; | |
178 | +} | |
179 | + | |
180 | + | |
181 | +/*---------------------------------------------------*/ | |
182 | +static | |
183 | +void init_RL ( EState* s ) | |
184 | +{ | |
185 | + s->state_in_ch = 256; | |
186 | + s->state_in_len = 0; | |
187 | +} | |
188 | + | |
189 | + | |
190 | +static | |
191 | +Bool isempty_RL ( EState* s ) | |
192 | +{ | |
193 | + if (s->state_in_ch < 256 && s->state_in_len > 0) | |
194 | + return False; else | |
195 | + return True; | |
196 | +} | |
197 | + | |
198 | +/*---------------------------------------------------*/ | |
199 | +int BZ_API(BZ2_bzCompressInit) | |
200 | + ( bz_stream* strm, | |
201 | + int blockSize100k, | |
202 | + int verbosity, | |
203 | + int workFactor ) | |
204 | +{ | |
205 | + Int32 n; | |
206 | + EState* s; | |
207 | + | |
208 | + if (!bz_config_ok()) return BZ_CONFIG_ERROR; | |
209 | + | |
210 | + if (strm == NULL || | |
211 | + blockSize100k < 1 || blockSize100k > 9 || | |
212 | + workFactor < 0 || workFactor > 250) | |
213 | + return BZ_PARAM_ERROR; | |
214 | + | |
215 | + if (workFactor == 0) workFactor = 30; | |
216 | + if (strm->bzalloc == NULL) strm->bzalloc = default_bzalloc; | |
217 | + if (strm->bzfree == NULL) strm->bzfree = default_bzfree; | |
218 | + | |
219 | + s = BZALLOC( sizeof(EState) ); | |
220 | + if (s == NULL) return BZ_MEM_ERROR; | |
221 | + s->strm = strm; | |
222 | + | |
223 | + s->arr1 = NULL; | |
224 | + s->arr2 = NULL; | |
225 | + s->ftab = NULL; | |
226 | + | |
227 | + n = 100000 * blockSize100k; | |
228 | + s->arr1 = BZALLOC( n * sizeof(UInt32) ); | |
229 | + s->arr2 = BZALLOC( (n+BZ_N_OVERSHOOT) * sizeof(UInt32) ); | |
230 | + s->ftab = BZALLOC( 65537 * sizeof(UInt32) ); | |
231 | + | |
232 | + if (s->arr1 == NULL || s->arr2 == NULL || s->ftab == NULL) { | |
233 | + if (s->arr1 != NULL) BZFREE(s->arr1); | |
234 | + if (s->arr2 != NULL) BZFREE(s->arr2); | |
235 | + if (s->ftab != NULL) BZFREE(s->ftab); | |
236 | + if (s != NULL) BZFREE(s); | |
237 | + return BZ_MEM_ERROR; | |
238 | + } | |
239 | + | |
240 | + s->blockNo = 0; | |
241 | + s->state = BZ_S_INPUT; | |
242 | + s->mode = BZ_M_RUNNING; | |
243 | + s->combinedCRC = 0; | |
244 | + s->blockSize100k = blockSize100k; | |
245 | + s->nblockMAX = 100000 * blockSize100k - 19; | |
246 | + s->verbosity = verbosity; | |
247 | + s->workFactor = workFactor; | |
248 | + | |
249 | + s->block = (UChar*)s->arr2; | |
250 | + s->mtfv = (UInt16*)s->arr1; | |
251 | + s->zbits = NULL; | |
252 | + s->ptr = (UInt32*)s->arr1; | |
253 | + | |
254 | + strm->state = s; | |
255 | + strm->total_in_lo32 = 0; | |
256 | + strm->total_in_hi32 = 0; | |
257 | + strm->total_out_lo32 = 0; | |
258 | + strm->total_out_hi32 = 0; | |
259 | + init_RL ( s ); | |
260 | + prepare_new_block ( s ); | |
261 | + return BZ_OK; | |
262 | +} | |
263 | + | |
264 | + | |
265 | +/*---------------------------------------------------*/ | |
266 | +static | |
267 | +void add_pair_to_block ( EState* s ) | |
268 | +{ | |
269 | + Int32 i; | |
270 | + UChar ch = (UChar)(s->state_in_ch); | |
271 | + for (i = 0; i < s->state_in_len; i++) { | |
272 | + BZ_UPDATE_CRC( s->blockCRC, ch ); | |
273 | + } | |
274 | + s->inUse[s->state_in_ch] = True; | |
275 | + switch (s->state_in_len) { | |
276 | + case 1: | |
277 | + s->block[s->nblock] = (UChar)ch; s->nblock++; | |
278 | + break; | |
279 | + case 2: | |
280 | + s->block[s->nblock] = (UChar)ch; s->nblock++; | |
281 | + s->block[s->nblock] = (UChar)ch; s->nblock++; | |
282 | + break; | |
283 | + case 3: | |
284 | + s->block[s->nblock] = (UChar)ch; s->nblock++; | |
285 | + s->block[s->nblock] = (UChar)ch; s->nblock++; | |
286 | + s->block[s->nblock] = (UChar)ch; s->nblock++; | |
287 | + break; | |
288 | + default: | |
289 | + s->inUse[s->state_in_len-4] = True; | |
290 | + s->block[s->nblock] = (UChar)ch; s->nblock++; | |
291 | + s->block[s->nblock] = (UChar)ch; s->nblock++; | |
292 | + s->block[s->nblock] = (UChar)ch; s->nblock++; | |
293 | + s->block[s->nblock] = (UChar)ch; s->nblock++; | |
294 | + s->block[s->nblock] = ((UChar)(s->state_in_len-4)); | |
295 | + s->nblock++; | |
296 | + break; | |
297 | + } | |
298 | +} | |
299 | + | |
300 | + | |
301 | +/*---------------------------------------------------*/ | |
302 | +static | |
303 | +void flush_RL ( EState* s ) | |
304 | +{ | |
305 | + if (s->state_in_ch < 256) add_pair_to_block ( s ); | |
306 | + init_RL ( s ); | |
307 | +} | |
308 | + | |
309 | + | |
310 | +/*---------------------------------------------------*/ | |
311 | +#define ADD_CHAR_TO_BLOCK(zs,zchh0) \ | |
312 | +{ \ | |
313 | + UInt32 zchh = (UInt32)(zchh0); \ | |
314 | + /*-- fast track the common case --*/ \ | |
315 | + if (zchh != zs->state_in_ch && \ | |
316 | + zs->state_in_len == 1) { \ | |
317 | + UChar ch = (UChar)(zs->state_in_ch); \ | |
318 | + BZ_UPDATE_CRC( zs->blockCRC, ch ); \ | |
319 | + zs->inUse[zs->state_in_ch] = True; \ | |
320 | + zs->block[zs->nblock] = (UChar)ch; \ | |
321 | + zs->nblock++; \ | |
322 | + zs->state_in_ch = zchh; \ | |
323 | + } \ | |
324 | + else \ | |
325 | + /*-- general, uncommon cases --*/ \ | |
326 | + if (zchh != zs->state_in_ch || \ | |
327 | + zs->state_in_len == 255) { \ | |
328 | + if (zs->state_in_ch < 256) \ | |
329 | + add_pair_to_block ( zs ); \ | |
330 | + zs->state_in_ch = zchh; \ | |
331 | + zs->state_in_len = 1; \ | |
332 | + } else { \ | |
333 | + zs->state_in_len++; \ | |
334 | + } \ | |
335 | +} | |
336 | + | |
337 | + | |
338 | +/*---------------------------------------------------*/ | |
339 | +static | |
340 | +Bool copy_input_until_stop ( EState* s ) | |
341 | +{ | |
342 | + Bool progress_in = False; | |
343 | + | |
344 | + if (s->mode == BZ_M_RUNNING) { | |
345 | + | |
346 | + /*-- fast track the common case --*/ | |
347 | + while (True) { | |
348 | + /*-- block full? --*/ | |
349 | + if (s->nblock >= s->nblockMAX) break; | |
350 | + /*-- no input? --*/ | |
351 | + if (s->strm->avail_in == 0) break; | |
352 | + progress_in = True; | |
353 | + ADD_CHAR_TO_BLOCK ( s, (UInt32)(*((UChar*)(s->strm->next_in))) ); | |
354 | + s->strm->next_in++; | |
355 | + s->strm->avail_in--; | |
356 | + s->strm->total_in_lo32++; | |
357 | + if (s->strm->total_in_lo32 == 0) s->strm->total_in_hi32++; | |
358 | + } | |
359 | + | |
360 | + } else { | |
361 | + | |
362 | + /*-- general, uncommon case --*/ | |
363 | + while (True) { | |
364 | + /*-- block full? --*/ | |
365 | + if (s->nblock >= s->nblockMAX) break; | |
366 | + /*-- no input? --*/ | |
367 | + if (s->strm->avail_in == 0) break; | |
368 | + /*-- flush/finish end? --*/ | |
369 | + if (s->avail_in_expect == 0) break; | |
370 | + progress_in = True; | |
371 | + ADD_CHAR_TO_BLOCK ( s, (UInt32)(*((UChar*)(s->strm->next_in))) ); | |
372 | + s->strm->next_in++; | |
373 | + s->strm->avail_in--; | |
374 | + s->strm->total_in_lo32++; | |
375 | + if (s->strm->total_in_lo32 == 0) s->strm->total_in_hi32++; | |
376 | + s->avail_in_expect--; | |
377 | + } | |
378 | + } | |
379 | + return progress_in; | |
380 | +} | |
381 | + | |
382 | + | |
383 | +/*---------------------------------------------------*/ | |
384 | +static | |
385 | +Bool copy_output_until_stop ( EState* s ) | |
386 | +{ | |
387 | + Bool progress_out = False; | |
388 | + | |
389 | + while (True) { | |
390 | + | |
391 | + /*-- no output space? --*/ | |
392 | + if (s->strm->avail_out == 0) break; | |
393 | + | |
394 | + /*-- block done? --*/ | |
395 | + if (s->state_out_pos >= s->numZ) break; | |
396 | + | |
397 | + progress_out = True; | |
398 | + *(s->strm->next_out) = s->zbits[s->state_out_pos]; | |
399 | + s->state_out_pos++; | |
400 | + s->strm->avail_out--; | |
401 | + s->strm->next_out++; | |
402 | + s->strm->total_out_lo32++; | |
403 | + if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++; | |
404 | + } | |
405 | + | |
406 | + return progress_out; | |
407 | +} | |
408 | + | |
409 | + | |
410 | +/*---------------------------------------------------*/ | |
411 | +static | |
412 | +Bool handle_compress ( bz_stream* strm ) | |
413 | +{ | |
414 | + Bool progress_in = False; | |
415 | + Bool progress_out = False; | |
416 | + EState* s = strm->state; | |
417 | + | |
418 | + while (True) { | |
419 | + | |
420 | + if (s->state == BZ_S_OUTPUT) { | |
421 | + progress_out |= copy_output_until_stop ( s ); | |
422 | + if (s->state_out_pos < s->numZ) break; | |
423 | + if (s->mode == BZ_M_FINISHING && | |
424 | + s->avail_in_expect == 0 && | |
425 | + isempty_RL(s)) break; | |
426 | + prepare_new_block ( s ); | |
427 | + s->state = BZ_S_INPUT; | |
428 | + if (s->mode == BZ_M_FLUSHING && | |
429 | + s->avail_in_expect == 0 && | |
430 | + isempty_RL(s)) break; | |
431 | + } | |
432 | + | |
433 | + if (s->state == BZ_S_INPUT) { | |
434 | + progress_in |= copy_input_until_stop ( s ); | |
435 | + if (s->mode != BZ_M_RUNNING && s->avail_in_expect == 0) { | |
436 | + flush_RL ( s ); | |
437 | + BZ2_compressBlock ( s, (Bool)(s->mode == BZ_M_FINISHING) ); | |
438 | + s->state = BZ_S_OUTPUT; | |
439 | + } | |
440 | + else | |
441 | + if (s->nblock >= s->nblockMAX) { | |
442 | + BZ2_compressBlock ( s, False ); | |
443 | + s->state = BZ_S_OUTPUT; | |
444 | + } | |
445 | + else | |
446 | + if (s->strm->avail_in == 0) { | |
447 | + break; | |
448 | + } | |
449 | + } | |
450 | + | |
451 | + } | |
452 | + | |
453 | + return progress_in || progress_out; | |
454 | +} | |
455 | + | |
456 | + | |
457 | +/*---------------------------------------------------*/ | |
458 | +int BZ_API(BZ2_bzCompress) ( bz_stream *strm, int action ) | |
459 | +{ | |
460 | + Bool progress; | |
461 | + EState* s; | |
462 | + if (strm == NULL) return BZ_PARAM_ERROR; | |
463 | + s = strm->state; | |
464 | + if (s == NULL) return BZ_PARAM_ERROR; | |
465 | + if (s->strm != strm) return BZ_PARAM_ERROR; | |
466 | + | |
467 | + preswitch: | |
468 | + switch (s->mode) { | |
469 | + | |
470 | + case BZ_M_IDLE: | |
471 | + return BZ_SEQUENCE_ERROR; | |
472 | + | |
473 | + case BZ_M_RUNNING: | |
474 | + if (action == BZ_RUN) { | |
475 | + progress = handle_compress ( strm ); | |
476 | + return progress ? BZ_RUN_OK : BZ_PARAM_ERROR; | |
477 | + } | |
478 | + else | |
479 | + if (action == BZ_FLUSH) { | |
480 | + s->avail_in_expect = strm->avail_in; | |
481 | + s->mode = BZ_M_FLUSHING; | |
482 | + goto preswitch; | |
483 | + } | |
484 | + else | |
485 | + if (action == BZ_FINISH) { | |
486 | + s->avail_in_expect = strm->avail_in; | |
487 | + s->mode = BZ_M_FINISHING; | |
488 | + goto preswitch; | |
489 | + } | |
490 | + else | |
491 | + return BZ_PARAM_ERROR; | |
492 | + | |
493 | + case BZ_M_FLUSHING: | |
494 | + if (action != BZ_FLUSH) return BZ_SEQUENCE_ERROR; | |
495 | + if (s->avail_in_expect != s->strm->avail_in) | |
496 | + return BZ_SEQUENCE_ERROR; | |
497 | + progress = handle_compress ( strm ); | |
498 | + if (s->avail_in_expect > 0 || !isempty_RL(s) || | |
499 | + s->state_out_pos < s->numZ) return BZ_FLUSH_OK; | |
500 | + s->mode = BZ_M_RUNNING; | |
501 | + return BZ_RUN_OK; | |
502 | + | |
503 | + case BZ_M_FINISHING: | |
504 | + if (action != BZ_FINISH) return BZ_SEQUENCE_ERROR; | |
505 | + if (s->avail_in_expect != s->strm->avail_in) | |
506 | + return BZ_SEQUENCE_ERROR; | |
507 | + progress = handle_compress ( strm ); | |
508 | + if (!progress) return BZ_SEQUENCE_ERROR; | |
509 | + if (s->avail_in_expect > 0 || !isempty_RL(s) || | |
510 | + s->state_out_pos < s->numZ) return BZ_FINISH_OK; | |
511 | + s->mode = BZ_M_IDLE; | |
512 | + return BZ_STREAM_END; | |
513 | + } | |
514 | + return BZ_OK; /*--not reached--*/ | |
515 | +} | |
516 | + | |
517 | + | |
518 | +/*---------------------------------------------------*/ | |
519 | +int BZ_API(BZ2_bzCompressEnd) ( bz_stream *strm ) | |
520 | +{ | |
521 | + EState* s; | |
522 | + if (strm == NULL) return BZ_PARAM_ERROR; | |
523 | + s = strm->state; | |
524 | + if (s == NULL) return BZ_PARAM_ERROR; | |
525 | + if (s->strm != strm) return BZ_PARAM_ERROR; | |
526 | + | |
527 | + if (s->arr1 != NULL) BZFREE(s->arr1); | |
528 | + if (s->arr2 != NULL) BZFREE(s->arr2); | |
529 | + if (s->ftab != NULL) BZFREE(s->ftab); | |
530 | + BZFREE(strm->state); | |
531 | + | |
532 | + strm->state = NULL; | |
533 | + | |
534 | + return BZ_OK; | |
535 | +} | |
536 | +#endif /* BZ_NO_COMPRESS */ | |
537 | + | |
538 | +/*---------------------------------------------------*/ | |
539 | +/*--- Decompression stuff ---*/ | |
540 | +/*---------------------------------------------------*/ | |
541 | + | |
542 | +/*---------------------------------------------------*/ | |
543 | +int BZ_API(BZ2_bzDecompressInit) | |
544 | + ( bz_stream* strm, | |
545 | + int verbosity, | |
546 | + int small ) | |
547 | +{ | |
548 | + DState* s; | |
549 | + | |
550 | + if (!bz_config_ok()) return BZ_CONFIG_ERROR; | |
551 | + | |
552 | + if (strm == NULL) return BZ_PARAM_ERROR; | |
553 | + if (small != 0 && small != 1) return BZ_PARAM_ERROR; | |
554 | + if (verbosity < 0 || verbosity > 4) return BZ_PARAM_ERROR; | |
555 | + | |
556 | + if (strm->bzalloc == NULL) strm->bzalloc = default_bzalloc; | |
557 | + if (strm->bzfree == NULL) strm->bzfree = default_bzfree; | |
558 | + | |
559 | + s = BZALLOC( sizeof(DState) ); | |
560 | + if (s == NULL) return BZ_MEM_ERROR; | |
561 | + s->strm = strm; | |
562 | + strm->state = s; | |
563 | + s->state = BZ_X_MAGIC_1; | |
564 | + s->bsLive = 0; | |
565 | + s->bsBuff = 0; | |
566 | + s->calculatedCombinedCRC = 0; | |
567 | + strm->total_in_lo32 = 0; | |
568 | + strm->total_in_hi32 = 0; | |
569 | + strm->total_out_lo32 = 0; | |
570 | + strm->total_out_hi32 = 0; | |
571 | + s->smallDecompress = (Bool)small; | |
572 | + s->ll4 = NULL; | |
573 | + s->ll16 = NULL; | |
574 | + s->tt = NULL; | |
575 | + s->currBlockNo = 0; | |
576 | + s->verbosity = verbosity; | |
577 | + | |
578 | + return BZ_OK; | |
579 | +} | |
580 | + | |
581 | + | |
582 | +/*---------------------------------------------------*/ | |
583 | +static | |
584 | +void unRLE_obuf_to_output_FAST ( DState* s ) | |
585 | +{ | |
586 | + UChar k1; | |
587 | + | |
588 | + if (s->blockRandomised) { | |
589 | + | |
590 | + while (True) { | |
591 | + /* try to finish existing run */ | |
592 | + while (True) { | |
593 | + if (s->strm->avail_out == 0) return; | |
594 | + if (s->state_out_len == 0) break; | |
595 | + *( (UChar*)(s->strm->next_out) ) = s->state_out_ch; | |
596 | + BZ_UPDATE_CRC ( s->calculatedBlockCRC, s->state_out_ch ); | |
597 | + s->state_out_len--; | |
598 | + s->strm->next_out++; | |
599 | + s->strm->avail_out--; | |
600 | + s->strm->total_out_lo32++; | |
601 | + if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++; | |
602 | + } | |
603 | + | |
604 | + /* can a new run be started? */ | |
605 | + if (s->nblock_used == s->save_nblock+1) return; | |
606 | + | |
607 | + | |
608 | + s->state_out_len = 1; | |
609 | + s->state_out_ch = s->k0; | |
610 | + BZ_GET_FAST(k1); BZ_RAND_UPD_MASK; | |
611 | + k1 ^= BZ_RAND_MASK; s->nblock_used++; | |
612 | + if (s->nblock_used == s->save_nblock+1) continue; | |
613 | + if (k1 != s->k0) { s->k0 = k1; continue; }; | |
614 | + | |
615 | + s->state_out_len = 2; | |
616 | + BZ_GET_FAST(k1); BZ_RAND_UPD_MASK; | |
617 | + k1 ^= BZ_RAND_MASK; s->nblock_used++; | |
618 | + if (s->nblock_used == s->save_nblock+1) continue; | |
619 | + if (k1 != s->k0) { s->k0 = k1; continue; }; | |
620 | + | |
621 | + s->state_out_len = 3; | |
622 | + BZ_GET_FAST(k1); BZ_RAND_UPD_MASK; | |
623 | + k1 ^= BZ_RAND_MASK; s->nblock_used++; | |
624 | + if (s->nblock_used == s->save_nblock+1) continue; | |
625 | + if (k1 != s->k0) { s->k0 = k1; continue; }; | |
626 | + | |
627 | + BZ_GET_FAST(k1); BZ_RAND_UPD_MASK; | |
628 | + k1 ^= BZ_RAND_MASK; s->nblock_used++; | |
629 | + s->state_out_len = ((Int32)k1) + 4; | |
630 | + BZ_GET_FAST(s->k0); BZ_RAND_UPD_MASK; | |
631 | + s->k0 ^= BZ_RAND_MASK; s->nblock_used++; | |
632 | + } | |
633 | + | |
634 | + } else { | |
635 | + | |
636 | + /* restore */ | |
637 | + UInt32 c_calculatedBlockCRC = s->calculatedBlockCRC; | |
638 | + UChar c_state_out_ch = s->state_out_ch; | |
639 | + Int32 c_state_out_len = s->state_out_len; | |
640 | + Int32 c_nblock_used = s->nblock_used; | |
641 | + Int32 c_k0 = s->k0; | |
642 | + UInt32* c_tt = s->tt; | |
643 | + UInt32 c_tPos = s->tPos; | |
644 | + char* cs_next_out = s->strm->next_out; | |
645 | + unsigned int cs_avail_out = s->strm->avail_out; | |
646 | + /* end restore */ | |
647 | + | |
648 | + UInt32 avail_out_INIT = cs_avail_out; | |
649 | + Int32 s_save_nblockPP = s->save_nblock+1; | |
650 | + unsigned int total_out_lo32_old; | |
651 | + | |
652 | + while (True) { | |
653 | + | |
654 | + /* try to finish existing run */ | |
655 | + if (c_state_out_len > 0) { | |
656 | + while (True) { | |
657 | + if (cs_avail_out == 0) goto return_notr; | |
658 | + if (c_state_out_len == 1) break; | |
659 | + *( (UChar*)(cs_next_out) ) = c_state_out_ch; | |
660 | + BZ_UPDATE_CRC ( c_calculatedBlockCRC, c_state_out_ch ); | |
661 | + c_state_out_len--; | |
662 | + cs_next_out++; | |
663 | + cs_avail_out--; | |
664 | + } | |
665 | + s_state_out_len_eq_one: | |
666 | + { | |
667 | + if (cs_avail_out == 0) { | |
668 | + c_state_out_len = 1; goto return_notr; | |
669 | + }; | |
670 | + *( (UChar*)(cs_next_out) ) = c_state_out_ch; | |
671 | + BZ_UPDATE_CRC ( c_calculatedBlockCRC, c_state_out_ch ); | |
672 | + cs_next_out++; | |
673 | + cs_avail_out--; | |
674 | + } | |
675 | + } | |
676 | + /* can a new run be started? */ | |
677 | + if (c_nblock_used == s_save_nblockPP) { | |
678 | + c_state_out_len = 0; goto return_notr; | |
679 | + }; | |
680 | + c_state_out_ch = c_k0; | |
681 | + BZ_GET_FAST_C(k1); c_nblock_used++; | |
682 | + if (k1 != c_k0) { | |
683 | + c_k0 = k1; goto s_state_out_len_eq_one; | |
684 | + }; | |
685 | + if (c_nblock_used == s_save_nblockPP) | |
686 | + goto s_state_out_len_eq_one; | |
687 | + | |
688 | + c_state_out_len = 2; | |
689 | + BZ_GET_FAST_C(k1); c_nblock_used++; | |
690 | + if (c_nblock_used == s_save_nblockPP) continue; | |
691 | + if (k1 != c_k0) { c_k0 = k1; continue; }; | |
692 | + | |
693 | + c_state_out_len = 3; | |
694 | + BZ_GET_FAST_C(k1); c_nblock_used++; | |
695 | + if (c_nblock_used == s_save_nblockPP) continue; | |
696 | + if (k1 != c_k0) { c_k0 = k1; continue; }; | |
697 | + | |
698 | + BZ_GET_FAST_C(k1); c_nblock_used++; | |
699 | + c_state_out_len = ((Int32)k1) + 4; | |
700 | + BZ_GET_FAST_C(c_k0); c_nblock_used++; | |
701 | + } | |
702 | + | |
703 | + return_notr: | |
704 | + total_out_lo32_old = s->strm->total_out_lo32; | |
705 | + s->strm->total_out_lo32 += (avail_out_INIT - cs_avail_out); | |
706 | + if (s->strm->total_out_lo32 < total_out_lo32_old) | |
707 | + s->strm->total_out_hi32++; | |
708 | + | |
709 | + /* save */ | |
710 | + s->calculatedBlockCRC = c_calculatedBlockCRC; | |
711 | + s->state_out_ch = c_state_out_ch; | |
712 | + s->state_out_len = c_state_out_len; | |
713 | + s->nblock_used = c_nblock_used; | |
714 | + s->k0 = c_k0; | |
715 | + s->tt = c_tt; | |
716 | + s->tPos = c_tPos; | |
717 | + s->strm->next_out = cs_next_out; | |
718 | + s->strm->avail_out = cs_avail_out; | |
719 | + /* end save */ | |
720 | + } | |
721 | +} | |
722 | + | |
723 | + | |
724 | +/*---------------------------------------------------*/ | |
725 | +__inline__ Int32 BZ2_indexIntoF ( Int32 indx, Int32 *cftab ) | |
726 | +{ | |
727 | + Int32 nb, na, mid; | |
728 | + nb = 0; | |
729 | + na = 256; | |
730 | + do { | |
731 | + mid = (nb + na) >> 1; | |
732 | + if (indx >= cftab[mid]) nb = mid; else na = mid; | |
733 | + } | |
734 | + while (na - nb != 1); | |
735 | + return nb; | |
736 | +} | |
737 | + | |
738 | + | |
739 | +/*---------------------------------------------------*/ | |
740 | +static | |
741 | +void unRLE_obuf_to_output_SMALL ( DState* s ) | |
742 | +{ | |
743 | + UChar k1; | |
744 | + | |
745 | + if (s->blockRandomised) { | |
746 | + | |
747 | + while (True) { | |
748 | + /* try to finish existing run */ | |
749 | + while (True) { | |
750 | + if (s->strm->avail_out == 0) return; | |
751 | + if (s->state_out_len == 0) break; | |
752 | + *( (UChar*)(s->strm->next_out) ) = s->state_out_ch; | |
753 | + BZ_UPDATE_CRC ( s->calculatedBlockCRC, s->state_out_ch ); | |
754 | + s->state_out_len--; | |
755 | + s->strm->next_out++; | |
756 | + s->strm->avail_out--; | |
757 | + s->strm->total_out_lo32++; | |
758 | + if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++; | |
759 | + } | |
760 | + | |
761 | + /* can a new run be started? */ | |
762 | + if (s->nblock_used == s->save_nblock+1) return; | |
763 | + | |
764 | + | |
765 | + s->state_out_len = 1; | |
766 | + s->state_out_ch = s->k0; | |
767 | + BZ_GET_SMALL(k1); BZ_RAND_UPD_MASK; | |
768 | + k1 ^= BZ_RAND_MASK; s->nblock_used++; | |
769 | + if (s->nblock_used == s->save_nblock+1) continue; | |
770 | + if (k1 != s->k0) { s->k0 = k1; continue; }; | |
771 | + | |
772 | + s->state_out_len = 2; | |
773 | + BZ_GET_SMALL(k1); BZ_RAND_UPD_MASK; | |
774 | + k1 ^= BZ_RAND_MASK; s->nblock_used++; | |
775 | + if (s->nblock_used == s->save_nblock+1) continue; | |
776 | + if (k1 != s->k0) { s->k0 = k1; continue; }; | |
777 | + | |
778 | + s->state_out_len = 3; | |
779 | + BZ_GET_SMALL(k1); BZ_RAND_UPD_MASK; | |
780 | + k1 ^= BZ_RAND_MASK; s->nblock_used++; | |
781 | + if (s->nblock_used == s->save_nblock+1) continue; | |
782 | + if (k1 != s->k0) { s->k0 = k1; continue; }; | |
783 | + | |
784 | + BZ_GET_SMALL(k1); BZ_RAND_UPD_MASK; | |
785 | + k1 ^= BZ_RAND_MASK; s->nblock_used++; | |
786 | + s->state_out_len = ((Int32)k1) + 4; | |
787 | + BZ_GET_SMALL(s->k0); BZ_RAND_UPD_MASK; | |
788 | + s->k0 ^= BZ_RAND_MASK; s->nblock_used++; | |
789 | + } | |
790 | + | |
791 | + } else { | |
792 | + | |
793 | + while (True) { | |
794 | + /* try to finish existing run */ | |
795 | + while (True) { | |
796 | + if (s->strm->avail_out == 0) return; | |
797 | + if (s->state_out_len == 0) break; | |
798 | + *( (UChar*)(s->strm->next_out) ) = s->state_out_ch; | |
799 | + BZ_UPDATE_CRC ( s->calculatedBlockCRC, s->state_out_ch ); | |
800 | + s->state_out_len--; | |
801 | + s->strm->next_out++; | |
802 | + s->strm->avail_out--; | |
803 | + s->strm->total_out_lo32++; | |
804 | + if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++; | |
805 | + } | |
806 | + | |
807 | + /* can a new run be started? */ | |
808 | + if (s->nblock_used == s->save_nblock+1) return; | |
809 | + | |
810 | + s->state_out_len = 1; | |
811 | + s->state_out_ch = s->k0; | |
812 | + BZ_GET_SMALL(k1); s->nblock_used++; | |
813 | + if (s->nblock_used == s->save_nblock+1) continue; | |
814 | + if (k1 != s->k0) { s->k0 = k1; continue; }; | |
815 | + | |
816 | + s->state_out_len = 2; | |
817 | + BZ_GET_SMALL(k1); s->nblock_used++; | |
818 | + if (s->nblock_used == s->save_nblock+1) continue; | |
819 | + if (k1 != s->k0) { s->k0 = k1; continue; }; | |
820 | + | |
821 | + s->state_out_len = 3; | |
822 | + BZ_GET_SMALL(k1); s->nblock_used++; | |
823 | + if (s->nblock_used == s->save_nblock+1) continue; | |
824 | + if (k1 != s->k0) { s->k0 = k1; continue; }; | |
825 | + | |
826 | + BZ_GET_SMALL(k1); s->nblock_used++; | |
827 | + s->state_out_len = ((Int32)k1) + 4; | |
828 | + BZ_GET_SMALL(s->k0); s->nblock_used++; | |
829 | + } | |
830 | + | |
831 | + } | |
832 | +} | |
833 | + | |
834 | + | |
835 | +/*---------------------------------------------------*/ | |
836 | +int BZ_API(BZ2_bzDecompress) ( bz_stream *strm ) | |
837 | +{ | |
838 | + DState* s; | |
839 | + if (strm == NULL) return BZ_PARAM_ERROR; | |
840 | + s = strm->state; | |
841 | + if (s == NULL) return BZ_PARAM_ERROR; | |
842 | + if (s->strm != strm) return BZ_PARAM_ERROR; | |
843 | + | |
844 | + while (True) { | |
845 | +#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG) | |
846 | + WATCHDOG_RESET(); | |
847 | +#endif | |
848 | + if (s->state == BZ_X_IDLE) return BZ_SEQUENCE_ERROR; | |
849 | + if (s->state == BZ_X_OUTPUT) { | |
850 | + if (s->smallDecompress) | |
851 | + unRLE_obuf_to_output_SMALL ( s ); else | |
852 | + unRLE_obuf_to_output_FAST ( s ); | |
853 | + if (s->nblock_used == s->save_nblock+1 && s->state_out_len == 0) { | |
854 | + BZ_FINALISE_CRC ( s->calculatedBlockCRC ); | |
855 | + if (s->verbosity >= 3) | |
856 | + VPrintf2 ( " {0x%x, 0x%x}", s->storedBlockCRC, | |
857 | + s->calculatedBlockCRC ); | |
858 | + if (s->verbosity >= 2) VPrintf0 ( "]" ); | |
859 | + if (s->calculatedBlockCRC != s->storedBlockCRC) | |
860 | + return BZ_DATA_ERROR; | |
861 | + s->calculatedCombinedCRC | |
862 | + = (s->calculatedCombinedCRC << 1) | | |
863 | + (s->calculatedCombinedCRC >> 31); | |
864 | + s->calculatedCombinedCRC ^= s->calculatedBlockCRC; | |
865 | + s->state = BZ_X_BLKHDR_1; | |
866 | + } else { | |
867 | + return BZ_OK; | |
868 | + } | |
869 | + } | |
870 | + if (s->state >= BZ_X_MAGIC_1) { | |
871 | + Int32 r = BZ2_decompress ( s ); | |
872 | + if (r == BZ_STREAM_END) { | |
873 | + if (s->verbosity >= 3) | |
874 | + VPrintf2 ( "\n combined CRCs: stored = 0x%x, computed = 0x%x", | |
875 | + s->storedCombinedCRC, s->calculatedCombinedCRC ); | |
876 | + if (s->calculatedCombinedCRC != s->storedCombinedCRC) | |
877 | + return BZ_DATA_ERROR; | |
878 | + return r; | |
879 | + } | |
880 | + if (s->state != BZ_X_OUTPUT) return r; | |
881 | + } | |
882 | + } | |
883 | + | |
884 | + AssertH ( 0, 6001 ); | |
885 | + | |
886 | + return 0; /*NOTREACHED*/ | |
887 | +} | |
888 | + | |
889 | + | |
890 | +/*---------------------------------------------------*/ | |
891 | +int BZ_API(BZ2_bzDecompressEnd) ( bz_stream *strm ) | |
892 | +{ | |
893 | + DState* s; | |
894 | + if (strm == NULL) return BZ_PARAM_ERROR; | |
895 | + s = strm->state; | |
896 | + if (s == NULL) return BZ_PARAM_ERROR; | |
897 | + if (s->strm != strm) return BZ_PARAM_ERROR; | |
898 | + | |
899 | + if (s->tt != NULL) BZFREE(s->tt); | |
900 | + if (s->ll16 != NULL) BZFREE(s->ll16); | |
901 | + if (s->ll4 != NULL) BZFREE(s->ll4); | |
902 | + | |
903 | + BZFREE(strm->state); | |
904 | + strm->state = NULL; | |
905 | + | |
906 | + return BZ_OK; | |
907 | +} | |
908 | + | |
909 | + | |
910 | +#ifndef BZ_NO_STDIO | |
911 | +/*---------------------------------------------------*/ | |
912 | +/*--- File I/O stuff ---*/ | |
913 | +/*---------------------------------------------------*/ | |
914 | + | |
915 | +#define BZ_SETERR(eee) \ | |
916 | +{ \ | |
917 | + if (bzerror != NULL) *bzerror = eee; \ | |
918 | + if (bzf != NULL) bzf->lastErr = eee; \ | |
919 | +} | |
920 | + | |
921 | +typedef | |
922 | + struct { | |
923 | + FILE* handle; | |
924 | + Char buf[BZ_MAX_UNUSED]; | |
925 | + Int32 bufN; | |
926 | + Bool writing; | |
927 | + bz_stream strm; | |
928 | + Int32 lastErr; | |
929 | + Bool initialisedOk; | |
930 | + } | |
931 | + bzFile; | |
932 | + | |
933 | + | |
934 | +/*---------------------------------------------*/ | |
935 | +static Bool myfeof ( FILE* f ) | |
936 | +{ | |
937 | + Int32 c = fgetc ( f ); | |
938 | + if (c == EOF) return True; | |
939 | + ungetc ( c, f ); | |
940 | + return False; | |
941 | +} | |
942 | + | |
943 | + | |
944 | +/*---------------------------------------------------*/ | |
945 | +BZFILE* BZ_API(BZ2_bzWriteOpen) | |
946 | + ( int* bzerror, | |
947 | + FILE* f, | |
948 | + int blockSize100k, | |
949 | + int verbosity, | |
950 | + int workFactor ) | |
951 | +{ | |
952 | + Int32 ret; | |
953 | + bzFile* bzf = NULL; | |
954 | + | |
955 | + BZ_SETERR(BZ_OK); | |
956 | + | |
957 | + if (f == NULL || | |
958 | + (blockSize100k < 1 || blockSize100k > 9) || | |
959 | + (workFactor < 0 || workFactor > 250) || | |
960 | + (verbosity < 0 || verbosity > 4)) | |
961 | + { BZ_SETERR(BZ_PARAM_ERROR); return NULL; }; | |
962 | + | |
963 | + if (ferror(f)) | |
964 | + { BZ_SETERR(BZ_IO_ERROR); return NULL; }; | |
965 | + | |
966 | + bzf = malloc ( sizeof(bzFile) ); | |
967 | + if (bzf == NULL) | |
968 | + { BZ_SETERR(BZ_MEM_ERROR); return NULL; }; | |
969 | + | |
970 | + BZ_SETERR(BZ_OK); | |
971 | + bzf->initialisedOk = False; | |
972 | + bzf->bufN = 0; | |
973 | + bzf->handle = f; | |
974 | + bzf->writing = True; | |
975 | + bzf->strm.bzalloc = NULL; | |
976 | + bzf->strm.bzfree = NULL; | |
977 | + bzf->strm.opaque = NULL; | |
978 | + | |
979 | + if (workFactor == 0) workFactor = 30; | |
980 | + ret = BZ2_bzCompressInit ( &(bzf->strm), blockSize100k, | |
981 | + verbosity, workFactor ); | |
982 | + if (ret != BZ_OK) | |
983 | + { BZ_SETERR(ret); free(bzf); return NULL; }; | |
984 | + | |
985 | + bzf->strm.avail_in = 0; | |
986 | + bzf->initialisedOk = True; | |
987 | + return bzf; | |
988 | +} | |
989 | + | |
990 | + | |
991 | +/*---------------------------------------------------*/ | |
992 | +void BZ_API(BZ2_bzWrite) | |
993 | + ( int* bzerror, | |
994 | + BZFILE* b, | |
995 | + void* buf, | |
996 | + int len ) | |
997 | +{ | |
998 | + Int32 n, n2, ret; | |
999 | + bzFile* bzf = (bzFile*)b; | |
1000 | + | |
1001 | + BZ_SETERR(BZ_OK); | |
1002 | + if (bzf == NULL || buf == NULL || len < 0) | |
1003 | + { BZ_SETERR(BZ_PARAM_ERROR); return; }; | |
1004 | + if (!(bzf->writing)) | |
1005 | + { BZ_SETERR(BZ_SEQUENCE_ERROR); return; }; | |
1006 | + if (ferror(bzf->handle)) | |
1007 | + { BZ_SETERR(BZ_IO_ERROR); return; }; | |
1008 | + | |
1009 | + if (len == 0) | |
1010 | + { BZ_SETERR(BZ_OK); return; }; | |
1011 | + | |
1012 | + bzf->strm.avail_in = len; | |
1013 | + bzf->strm.next_in = buf; | |
1014 | + | |
1015 | + while (True) { | |
1016 | + bzf->strm.avail_out = BZ_MAX_UNUSED; | |
1017 | + bzf->strm.next_out = bzf->buf; | |
1018 | + ret = BZ2_bzCompress ( &(bzf->strm), BZ_RUN ); | |
1019 | + if (ret != BZ_RUN_OK) | |
1020 | + { BZ_SETERR(ret); return; }; | |
1021 | + | |
1022 | + if (bzf->strm.avail_out < BZ_MAX_UNUSED) { | |
1023 | + n = BZ_MAX_UNUSED - bzf->strm.avail_out; | |
1024 | + n2 = fwrite ( (void*)(bzf->buf), sizeof(UChar), | |
1025 | + n, bzf->handle ); | |
1026 | + if (n != n2 || ferror(bzf->handle)) | |
1027 | + { BZ_SETERR(BZ_IO_ERROR); return; }; | |
1028 | + } | |
1029 | + | |
1030 | + if (bzf->strm.avail_in == 0) | |
1031 | + { BZ_SETERR(BZ_OK); return; }; | |
1032 | + } | |
1033 | +} | |
1034 | + | |
1035 | + | |
1036 | +/*---------------------------------------------------*/ | |
1037 | +void BZ_API(BZ2_bzWriteClose) | |
1038 | + ( int* bzerror, | |
1039 | + BZFILE* b, | |
1040 | + int abandon, | |
1041 | + unsigned int* nbytes_in, | |
1042 | + unsigned int* nbytes_out ) | |
1043 | +{ | |
1044 | + BZ2_bzWriteClose64 ( bzerror, b, abandon, | |
1045 | + nbytes_in, NULL, nbytes_out, NULL ); | |
1046 | +} | |
1047 | + | |
1048 | + | |
1049 | +void BZ_API(BZ2_bzWriteClose64) | |
1050 | + ( int* bzerror, | |
1051 | + BZFILE* b, | |
1052 | + int abandon, | |
1053 | + unsigned int* nbytes_in_lo32, | |
1054 | + unsigned int* nbytes_in_hi32, | |
1055 | + unsigned int* nbytes_out_lo32, | |
1056 | + unsigned int* nbytes_out_hi32 ) | |
1057 | +{ | |
1058 | + Int32 n, n2, ret; | |
1059 | + bzFile* bzf = (bzFile*)b; | |
1060 | + | |
1061 | + if (bzf == NULL) | |
1062 | + { BZ_SETERR(BZ_OK); return; }; | |
1063 | + if (!(bzf->writing)) | |
1064 | + { BZ_SETERR(BZ_SEQUENCE_ERROR); return; }; | |
1065 | + if (ferror(bzf->handle)) | |
1066 | + { BZ_SETERR(BZ_IO_ERROR); return; }; | |
1067 | + | |
1068 | + if (nbytes_in_lo32 != NULL) *nbytes_in_lo32 = 0; | |
1069 | + if (nbytes_in_hi32 != NULL) *nbytes_in_hi32 = 0; | |
1070 | + if (nbytes_out_lo32 != NULL) *nbytes_out_lo32 = 0; | |
1071 | + if (nbytes_out_hi32 != NULL) *nbytes_out_hi32 = 0; | |
1072 | + | |
1073 | + if ((!abandon) && bzf->lastErr == BZ_OK) { | |
1074 | + while (True) { | |
1075 | + bzf->strm.avail_out = BZ_MAX_UNUSED; | |
1076 | + bzf->strm.next_out = bzf->buf; | |
1077 | + ret = BZ2_bzCompress ( &(bzf->strm), BZ_FINISH ); | |
1078 | + if (ret != BZ_FINISH_OK && ret != BZ_STREAM_END) | |
1079 | + { BZ_SETERR(ret); return; }; | |
1080 | + | |
1081 | + if (bzf->strm.avail_out < BZ_MAX_UNUSED) { | |
1082 | + n = BZ_MAX_UNUSED - bzf->strm.avail_out; | |
1083 | + n2 = fwrite ( (void*)(bzf->buf), sizeof(UChar), | |
1084 | + n, bzf->handle ); | |
1085 | + if (n != n2 || ferror(bzf->handle)) | |
1086 | + { BZ_SETERR(BZ_IO_ERROR); return; }; | |
1087 | + } | |
1088 | + | |
1089 | + if (ret == BZ_STREAM_END) break; | |
1090 | + } | |
1091 | + } | |
1092 | + | |
1093 | + if ( !abandon && !ferror ( bzf->handle ) ) { | |
1094 | + fflush ( bzf->handle ); | |
1095 | + if (ferror(bzf->handle)) | |
1096 | + { BZ_SETERR(BZ_IO_ERROR); return; }; | |
1097 | + } | |
1098 | + | |
1099 | + if (nbytes_in_lo32 != NULL) | |
1100 | + *nbytes_in_lo32 = bzf->strm.total_in_lo32; | |
1101 | + if (nbytes_in_hi32 != NULL) | |
1102 | + *nbytes_in_hi32 = bzf->strm.total_in_hi32; | |
1103 | + if (nbytes_out_lo32 != NULL) | |
1104 | + *nbytes_out_lo32 = bzf->strm.total_out_lo32; | |
1105 | + if (nbytes_out_hi32 != NULL) | |
1106 | + *nbytes_out_hi32 = bzf->strm.total_out_hi32; | |
1107 | + | |
1108 | + BZ_SETERR(BZ_OK); | |
1109 | + BZ2_bzCompressEnd ( &(bzf->strm) ); | |
1110 | + free ( bzf ); | |
1111 | +} | |
1112 | + | |
1113 | + | |
1114 | +/*---------------------------------------------------*/ | |
1115 | +BZFILE* BZ_API(BZ2_bzReadOpen) | |
1116 | + ( int* bzerror, | |
1117 | + FILE* f, | |
1118 | + int verbosity, | |
1119 | + int small, | |
1120 | + void* unused, | |
1121 | + int nUnused ) | |
1122 | +{ | |
1123 | + bzFile* bzf = NULL; | |
1124 | + int ret; | |
1125 | + | |
1126 | + BZ_SETERR(BZ_OK); | |
1127 | + | |
1128 | + if (f == NULL || | |
1129 | + (small != 0 && small != 1) || | |
1130 | + (verbosity < 0 || verbosity > 4) || | |
1131 | + (unused == NULL && nUnused != 0) || | |
1132 | + (unused != NULL && (nUnused < 0 || nUnused > BZ_MAX_UNUSED))) | |
1133 | + { BZ_SETERR(BZ_PARAM_ERROR); return NULL; }; | |
1134 | + | |
1135 | + if (ferror(f)) | |
1136 | + { BZ_SETERR(BZ_IO_ERROR); return NULL; }; | |
1137 | + | |
1138 | + bzf = malloc ( sizeof(bzFile) ); | |
1139 | + if (bzf == NULL) | |
1140 | + { BZ_SETERR(BZ_MEM_ERROR); return NULL; }; | |
1141 | + | |
1142 | + BZ_SETERR(BZ_OK); | |
1143 | + | |
1144 | + bzf->initialisedOk = False; | |
1145 | + bzf->handle = f; | |
1146 | + bzf->bufN = 0; | |
1147 | + bzf->writing = False; | |
1148 | + bzf->strm.bzalloc = NULL; | |
1149 | + bzf->strm.bzfree = NULL; | |
1150 | + bzf->strm.opaque = NULL; | |
1151 | + | |
1152 | + while (nUnused > 0) { | |
1153 | + bzf->buf[bzf->bufN] = *((UChar*)(unused)); bzf->bufN++; | |
1154 | + unused = ((void*)( 1 + ((UChar*)(unused)) )); | |
1155 | + nUnused--; | |
1156 | + } | |
1157 | + | |
1158 | + ret = BZ2_bzDecompressInit ( &(bzf->strm), verbosity, small ); | |
1159 | + if (ret != BZ_OK) | |
1160 | + { BZ_SETERR(ret); free(bzf); return NULL; }; | |
1161 | + | |
1162 | + bzf->strm.avail_in = bzf->bufN; | |
1163 | + bzf->strm.next_in = bzf->buf; | |
1164 | + | |
1165 | + bzf->initialisedOk = True; | |
1166 | + return bzf; | |
1167 | +} | |
1168 | + | |
1169 | + | |
1170 | +/*---------------------------------------------------*/ | |
1171 | +void BZ_API(BZ2_bzReadClose) ( int *bzerror, BZFILE *b ) | |
1172 | +{ | |
1173 | + bzFile* bzf = (bzFile*)b; | |
1174 | + | |
1175 | + BZ_SETERR(BZ_OK); | |
1176 | + if (bzf == NULL) | |
1177 | + { BZ_SETERR(BZ_OK); return; }; | |
1178 | + | |
1179 | + if (bzf->writing) | |
1180 | + { BZ_SETERR(BZ_SEQUENCE_ERROR); return; }; | |
1181 | + | |
1182 | + if (bzf->initialisedOk) | |
1183 | + (void)BZ2_bzDecompressEnd ( &(bzf->strm) ); | |
1184 | + free ( bzf ); | |
1185 | +} | |
1186 | + | |
1187 | + | |
1188 | +/*---------------------------------------------------*/ | |
1189 | +int BZ_API(BZ2_bzRead) | |
1190 | + ( int* bzerror, | |
1191 | + BZFILE* b, | |
1192 | + void* buf, | |
1193 | + int len ) | |
1194 | +{ | |
1195 | + Int32 n, ret; | |
1196 | + bzFile* bzf = (bzFile*)b; | |
1197 | + | |
1198 | + BZ_SETERR(BZ_OK); | |
1199 | + | |
1200 | + if (bzf == NULL || buf == NULL || len < 0) | |
1201 | + { BZ_SETERR(BZ_PARAM_ERROR); return 0; }; | |
1202 | + | |
1203 | + if (bzf->writing) | |
1204 | + { BZ_SETERR(BZ_SEQUENCE_ERROR); return 0; }; | |
1205 | + | |
1206 | + if (len == 0) | |
1207 | + { BZ_SETERR(BZ_OK); return 0; }; | |
1208 | + | |
1209 | + bzf->strm.avail_out = len; | |
1210 | + bzf->strm.next_out = buf; | |
1211 | + | |
1212 | + while (True) { | |
1213 | + | |
1214 | + if (ferror(bzf->handle)) | |
1215 | + { BZ_SETERR(BZ_IO_ERROR); return 0; }; | |
1216 | + | |
1217 | + if (bzf->strm.avail_in == 0 && !myfeof(bzf->handle)) { | |
1218 | + n = fread ( bzf->buf, sizeof(UChar), | |
1219 | + BZ_MAX_UNUSED, bzf->handle ); | |
1220 | + if (ferror(bzf->handle)) | |
1221 | + { BZ_SETERR(BZ_IO_ERROR); return 0; }; | |
1222 | + bzf->bufN = n; | |
1223 | + bzf->strm.avail_in = bzf->bufN; | |
1224 | + bzf->strm.next_in = bzf->buf; | |
1225 | + } | |
1226 | + | |
1227 | + ret = BZ2_bzDecompress ( &(bzf->strm) ); | |
1228 | + | |
1229 | + if (ret != BZ_OK && ret != BZ_STREAM_END) | |
1230 | + { BZ_SETERR(ret); return 0; }; | |
1231 | + | |
1232 | + if (ret == BZ_OK && myfeof(bzf->handle) && | |
1233 | + bzf->strm.avail_in == 0 && bzf->strm.avail_out > 0) | |
1234 | + { BZ_SETERR(BZ_UNEXPECTED_EOF); return 0; }; | |
1235 | + | |
1236 | + if (ret == BZ_STREAM_END) | |
1237 | + { BZ_SETERR(BZ_STREAM_END); | |
1238 | + return len - bzf->strm.avail_out; }; | |
1239 | + if (bzf->strm.avail_out == 0) | |
1240 | + { BZ_SETERR(BZ_OK); return len; }; | |
1241 | + | |
1242 | + } | |
1243 | + | |
1244 | + return 0; /*not reached*/ | |
1245 | +} | |
1246 | + | |
1247 | + | |
1248 | +/*---------------------------------------------------*/ | |
1249 | +void BZ_API(BZ2_bzReadGetUnused) | |
1250 | + ( int* bzerror, | |
1251 | + BZFILE* b, | |
1252 | + void** unused, | |
1253 | + int* nUnused ) | |
1254 | +{ | |
1255 | + bzFile* bzf = (bzFile*)b; | |
1256 | + if (bzf == NULL) | |
1257 | + { BZ_SETERR(BZ_PARAM_ERROR); return; }; | |
1258 | + if (bzf->lastErr != BZ_STREAM_END) | |
1259 | + { BZ_SETERR(BZ_SEQUENCE_ERROR); return; }; | |
1260 | + if (unused == NULL || nUnused == NULL) | |
1261 | + { BZ_SETERR(BZ_PARAM_ERROR); return; }; | |
1262 | + | |
1263 | + BZ_SETERR(BZ_OK); | |
1264 | + *nUnused = bzf->strm.avail_in; | |
1265 | + *unused = bzf->strm.next_in; | |
1266 | +} | |
1267 | +#endif | |
1268 | + | |
1269 | + | |
1270 | +/*---------------------------------------------------*/ | |
1271 | +/*--- Misc convenience stuff ---*/ | |
1272 | +/*---------------------------------------------------*/ | |
1273 | +#ifndef BZ_NO_COMPRESS | |
1274 | +/*---------------------------------------------------*/ | |
1275 | +int BZ_API(BZ2_bzBuffToBuffCompress) | |
1276 | + ( char* dest, | |
1277 | + unsigned int* destLen, | |
1278 | + char* source, | |
1279 | + unsigned int sourceLen, | |
1280 | + int blockSize100k, | |
1281 | + int verbosity, | |
1282 | + int workFactor ) | |
1283 | +{ | |
1284 | + bz_stream strm; | |
1285 | + int ret; | |
1286 | + | |
1287 | + if (dest == NULL || destLen == NULL || | |
1288 | + source == NULL || | |
1289 | + blockSize100k < 1 || blockSize100k > 9 || | |
1290 | + verbosity < 0 || verbosity > 4 || | |
1291 | + workFactor < 0 || workFactor > 250) | |
1292 | + return BZ_PARAM_ERROR; | |
1293 | + | |
1294 | + if (workFactor == 0) workFactor = 30; | |
1295 | + strm.bzalloc = NULL; | |
1296 | + strm.bzfree = NULL; | |
1297 | + strm.opaque = NULL; | |
1298 | + ret = BZ2_bzCompressInit ( &strm, blockSize100k, | |
1299 | + verbosity, workFactor ); | |
1300 | + if (ret != BZ_OK) return ret; | |
1301 | + | |
1302 | + strm.next_in = source; | |
1303 | + strm.next_out = dest; | |
1304 | + strm.avail_in = sourceLen; | |
1305 | + strm.avail_out = *destLen; | |
1306 | + | |
1307 | + ret = BZ2_bzCompress ( &strm, BZ_FINISH ); | |
1308 | + if (ret == BZ_FINISH_OK) goto output_overflow; | |
1309 | + if (ret != BZ_STREAM_END) goto errhandler; | |
1310 | + | |
1311 | + /* normal termination */ | |
1312 | + *destLen -= strm.avail_out; | |
1313 | + BZ2_bzCompressEnd ( &strm ); | |
1314 | + return BZ_OK; | |
1315 | + | |
1316 | + output_overflow: | |
1317 | + BZ2_bzCompressEnd ( &strm ); | |
1318 | + return BZ_OUTBUFF_FULL; | |
1319 | + | |
1320 | + errhandler: | |
1321 | + BZ2_bzCompressEnd ( &strm ); | |
1322 | + return ret; | |
1323 | +} | |
1324 | +#endif /* BZ_NO_COMPRESS */ | |
1325 | + | |
1326 | +/*---------------------------------------------------*/ | |
1327 | +int BZ_API(BZ2_bzBuffToBuffDecompress) | |
1328 | + ( char* dest, | |
1329 | + unsigned int* destLen, | |
1330 | + char* source, | |
1331 | + unsigned int sourceLen, | |
1332 | + int small, | |
1333 | + int verbosity ) | |
1334 | +{ | |
1335 | + bz_stream strm; | |
1336 | + int ret; | |
1337 | + | |
1338 | + if (destLen == NULL || source == NULL) | |
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 | +--*/ | |
1392 | +const 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 | |
1408 | +static | |
1409 | +BZFILE * bzopen_or_bzdopen | |
1410 | + ( const char *path, /* no use when bzdopen */ | |
1411 | + int fd, /* no use when bzdopen */ | |
1412 | + const char *mode, | |
1413 | + int open_mode) /* bzopen: 0, bzdopen:1 */ | |
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 | +--*/ | |
1486 | +BZFILE * 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 | +/*---------------------------------------------------*/ | |
1495 | +BZFILE * 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 | +/*---------------------------------------------------*/ | |
1504 | +int 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 | +/*---------------------------------------------------*/ | |
1518 | +int 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 | +/*---------------------------------------------------*/ | |
1532 | +int BZ_API(BZ2_bzflush) (BZFILE *b) | |
1533 | +{ | |
1534 | + /* do nothing now... */ | |
1535 | + return 0; | |
1536 | +} | |
1537 | + | |
1538 | + | |
1539 | +/*---------------------------------------------------*/ | |
1540 | +void 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 | +--*/ | |
1564 | +static 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 | + | |
1584 | +const 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 | +void bz_internal_error(int errcode) | |
1595 | +{ | |
1596 | + printf ("BZIP2 internal error %d\n", errcode); | |
1597 | +} | |
1598 | + | |
1599 | +/*-------------------------------------------------------------*/ | |
1600 | +/*--- end bzlib.c ---*/ | |
1601 | +/*-------------------------------------------------------------*/ |
lib/bzip2/bzlib_crctable.c
1 | +#include <config.h> | |
2 | + | |
3 | +/*-------------------------------------------------------------*/ | |
4 | +/*--- Table for doing CRCs ---*/ | |
5 | +/*--- crctable.c ---*/ | |
6 | +/*-------------------------------------------------------------*/ | |
7 | + | |
8 | +/*-- | |
9 | + This file is a part of bzip2 and/or libbzip2, a program and | |
10 | + library for lossless, block-sorting data compression. | |
11 | + | |
12 | + Copyright (C) 1996-2002 Julian R Seward. All rights reserved. | |
13 | + | |
14 | + Redistribution and use in source and binary forms, with or without | |
15 | + modification, are permitted provided that the following conditions | |
16 | + are met: | |
17 | + | |
18 | + 1. Redistributions of source code must retain the above copyright | |
19 | + notice, this list of conditions and the following disclaimer. | |
20 | + | |
21 | + 2. The origin of this software must not be misrepresented; you must | |
22 | + not claim that you wrote the original software. If you use this | |
23 | + software in a product, an acknowledgment in the product | |
24 | + documentation would be appreciated but is not required. | |
25 | + | |
26 | + 3. Altered source versions must be plainly marked as such, and must | |
27 | + not be misrepresented as being the original software. | |
28 | + | |
29 | + 4. The name of the author may not be used to endorse or promote | |
30 | + products derived from this software without specific prior written | |
31 | + permission. | |
32 | + | |
33 | + THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS | |
34 | + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
35 | + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
36 | + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY | |
37 | + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
38 | + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE | |
39 | + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
40 | + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
41 | + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
42 | + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
43 | + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
44 | + | |
45 | + Julian Seward, Cambridge, UK. | |
46 | + jseward@acm.org | |
47 | + bzip2/libbzip2 version 1.0 of 21 March 2000 | |
48 | + | |
49 | + This program is based on (at least) the work of: | |
50 | + Mike Burrows | |
51 | + David Wheeler | |
52 | + Peter Fenwick | |
53 | + Alistair Moffat | |
54 | + Radford Neal | |
55 | + Ian H. Witten | |
56 | + Robert Sedgewick | |
57 | + Jon L. Bentley | |
58 | + | |
59 | + For more information on these sources, see the manual. | |
60 | +--*/ | |
61 | + | |
62 | + | |
63 | +#include "bzlib_private.h" | |
64 | + | |
65 | +/*-- | |
66 | + I think this is an implementation of the AUTODIN-II, | |
67 | + Ethernet & FDDI 32-bit CRC standard. Vaguely derived | |
68 | + from code by Rob Warnock, in Section 51 of the | |
69 | + comp.compression FAQ. | |
70 | +--*/ | |
71 | + | |
72 | +UInt32 BZ2_crc32Table[256] = { | |
73 | + | |
74 | + /*-- Ugly, innit? --*/ | |
75 | + | |
76 | + 0x00000000L, 0x04c11db7L, 0x09823b6eL, 0x0d4326d9L, | |
77 | + 0x130476dcL, 0x17c56b6bL, 0x1a864db2L, 0x1e475005L, | |
78 | + 0x2608edb8L, 0x22c9f00fL, 0x2f8ad6d6L, 0x2b4bcb61L, | |
79 | + 0x350c9b64L, 0x31cd86d3L, 0x3c8ea00aL, 0x384fbdbdL, | |
80 | + 0x4c11db70L, 0x48d0c6c7L, 0x4593e01eL, 0x4152fda9L, | |
81 | + 0x5f15adacL, 0x5bd4b01bL, 0x569796c2L, 0x52568b75L, | |
82 | + 0x6a1936c8L, 0x6ed82b7fL, 0x639b0da6L, 0x675a1011L, | |
83 | + 0x791d4014L, 0x7ddc5da3L, 0x709f7b7aL, 0x745e66cdL, | |
84 | + 0x9823b6e0L, 0x9ce2ab57L, 0x91a18d8eL, 0x95609039L, | |
85 | + 0x8b27c03cL, 0x8fe6dd8bL, 0x82a5fb52L, 0x8664e6e5L, | |
86 | + 0xbe2b5b58L, 0xbaea46efL, 0xb7a96036L, 0xb3687d81L, | |
87 | + 0xad2f2d84L, 0xa9ee3033L, 0xa4ad16eaL, 0xa06c0b5dL, | |
88 | + 0xd4326d90L, 0xd0f37027L, 0xddb056feL, 0xd9714b49L, | |
89 | + 0xc7361b4cL, 0xc3f706fbL, 0xceb42022L, 0xca753d95L, | |
90 | + 0xf23a8028L, 0xf6fb9d9fL, 0xfbb8bb46L, 0xff79a6f1L, | |
91 | + 0xe13ef6f4L, 0xe5ffeb43L, 0xe8bccd9aL, 0xec7dd02dL, | |
92 | + 0x34867077L, 0x30476dc0L, 0x3d044b19L, 0x39c556aeL, | |
93 | + 0x278206abL, 0x23431b1cL, 0x2e003dc5L, 0x2ac12072L, | |
94 | + 0x128e9dcfL, 0x164f8078L, 0x1b0ca6a1L, 0x1fcdbb16L, | |
95 | + 0x018aeb13L, 0x054bf6a4L, 0x0808d07dL, 0x0cc9cdcaL, | |
96 | + 0x7897ab07L, 0x7c56b6b0L, 0x71159069L, 0x75d48ddeL, | |
97 | + 0x6b93dddbL, 0x6f52c06cL, 0x6211e6b5L, 0x66d0fb02L, | |
98 | + 0x5e9f46bfL, 0x5a5e5b08L, 0x571d7dd1L, 0x53dc6066L, | |
99 | + 0x4d9b3063L, 0x495a2dd4L, 0x44190b0dL, 0x40d816baL, | |
100 | + 0xaca5c697L, 0xa864db20L, 0xa527fdf9L, 0xa1e6e04eL, | |
101 | + 0xbfa1b04bL, 0xbb60adfcL, 0xb6238b25L, 0xb2e29692L, | |
102 | + 0x8aad2b2fL, 0x8e6c3698L, 0x832f1041L, 0x87ee0df6L, | |
103 | + 0x99a95df3L, 0x9d684044L, 0x902b669dL, 0x94ea7b2aL, | |
104 | + 0xe0b41de7L, 0xe4750050L, 0xe9362689L, 0xedf73b3eL, | |
105 | + 0xf3b06b3bL, 0xf771768cL, 0xfa325055L, 0xfef34de2L, | |
106 | + 0xc6bcf05fL, 0xc27dede8L, 0xcf3ecb31L, 0xcbffd686L, | |
107 | + 0xd5b88683L, 0xd1799b34L, 0xdc3abdedL, 0xd8fba05aL, | |
108 | + 0x690ce0eeL, 0x6dcdfd59L, 0x608edb80L, 0x644fc637L, | |
109 | + 0x7a089632L, 0x7ec98b85L, 0x738aad5cL, 0x774bb0ebL, | |
110 | + 0x4f040d56L, 0x4bc510e1L, 0x46863638L, 0x42472b8fL, | |
111 | + 0x5c007b8aL, 0x58c1663dL, 0x558240e4L, 0x51435d53L, | |
112 | + 0x251d3b9eL, 0x21dc2629L, 0x2c9f00f0L, 0x285e1d47L, | |
113 | + 0x36194d42L, 0x32d850f5L, 0x3f9b762cL, 0x3b5a6b9bL, | |
114 | + 0x0315d626L, 0x07d4cb91L, 0x0a97ed48L, 0x0e56f0ffL, | |
115 | + 0x1011a0faL, 0x14d0bd4dL, 0x19939b94L, 0x1d528623L, | |
116 | + 0xf12f560eL, 0xf5ee4bb9L, 0xf8ad6d60L, 0xfc6c70d7L, | |
117 | + 0xe22b20d2L, 0xe6ea3d65L, 0xeba91bbcL, 0xef68060bL, | |
118 | + 0xd727bbb6L, 0xd3e6a601L, 0xdea580d8L, 0xda649d6fL, | |
119 | + 0xc423cd6aL, 0xc0e2d0ddL, 0xcda1f604L, 0xc960ebb3L, | |
120 | + 0xbd3e8d7eL, 0xb9ff90c9L, 0xb4bcb610L, 0xb07daba7L, | |
121 | + 0xae3afba2L, 0xaafbe615L, 0xa7b8c0ccL, 0xa379dd7bL, | |
122 | + 0x9b3660c6L, 0x9ff77d71L, 0x92b45ba8L, 0x9675461fL, | |
123 | + 0x8832161aL, 0x8cf30badL, 0x81b02d74L, 0x857130c3L, | |
124 | + 0x5d8a9099L, 0x594b8d2eL, 0x5408abf7L, 0x50c9b640L, | |
125 | + 0x4e8ee645L, 0x4a4ffbf2L, 0x470cdd2bL, 0x43cdc09cL, | |
126 | + 0x7b827d21L, 0x7f436096L, 0x7200464fL, 0x76c15bf8L, | |
127 | + 0x68860bfdL, 0x6c47164aL, 0x61043093L, 0x65c52d24L, | |
128 | + 0x119b4be9L, 0x155a565eL, 0x18197087L, 0x1cd86d30L, | |
129 | + 0x029f3d35L, 0x065e2082L, 0x0b1d065bL, 0x0fdc1becL, | |
130 | + 0x3793a651L, 0x3352bbe6L, 0x3e119d3fL, 0x3ad08088L, | |
131 | + 0x2497d08dL, 0x2056cd3aL, 0x2d15ebe3L, 0x29d4f654L, | |
132 | + 0xc5a92679L, 0xc1683bceL, 0xcc2b1d17L, 0xc8ea00a0L, | |
133 | + 0xd6ad50a5L, 0xd26c4d12L, 0xdf2f6bcbL, 0xdbee767cL, | |
134 | + 0xe3a1cbc1L, 0xe760d676L, 0xea23f0afL, 0xeee2ed18L, | |
135 | + 0xf0a5bd1dL, 0xf464a0aaL, 0xf9278673L, 0xfde69bc4L, | |
136 | + 0x89b8fd09L, 0x8d79e0beL, 0x803ac667L, 0x84fbdbd0L, | |
137 | + 0x9abc8bd5L, 0x9e7d9662L, 0x933eb0bbL, 0x97ffad0cL, | |
138 | + 0xafb010b1L, 0xab710d06L, 0xa6322bdfL, 0xa2f33668L, | |
139 | + 0xbcb4666dL, 0xb8757bdaL, 0xb5365d03L, 0xb1f740b4L | |
140 | +}; | |
141 | + | |
142 | + | |
143 | +/*-------------------------------------------------------------*/ | |
144 | +/*--- end crctable.c ---*/ | |
145 | +/*-------------------------------------------------------------*/ |
lib/bzip2/bzlib_decompress.c
1 | +#include <config.h> | |
2 | +#include <common.h> | |
3 | +#include <watchdog.h> | |
4 | + | |
5 | +/*-------------------------------------------------------------*/ | |
6 | +/*--- Decompression machinery ---*/ | |
7 | +/*--- decompress.c ---*/ | |
8 | +/*-------------------------------------------------------------*/ | |
9 | + | |
10 | +/*-- | |
11 | + This file is a part of bzip2 and/or libbzip2, a program and | |
12 | + library for lossless, block-sorting data compression. | |
13 | + | |
14 | + Copyright (C) 1996-2002 Julian R Seward. All rights reserved. | |
15 | + | |
16 | + Redistribution and use in source and binary forms, with or without | |
17 | + modification, are permitted provided that the following conditions | |
18 | + are met: | |
19 | + | |
20 | + 1. Redistributions of source code must retain the above copyright | |
21 | + notice, this list of conditions and the following disclaimer. | |
22 | + | |
23 | + 2. The origin of this software must not be misrepresented; you must | |
24 | + not claim that you wrote the original software. If you use this | |
25 | + software in a product, an acknowledgment in the product | |
26 | + documentation would be appreciated but is not required. | |
27 | + | |
28 | + 3. Altered source versions must be plainly marked as such, and must | |
29 | + not be misrepresented as being the original software. | |
30 | + | |
31 | + 4. The name of the author may not be used to endorse or promote | |
32 | + products derived from this software without specific prior written | |
33 | + permission. | |
34 | + | |
35 | + THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS | |
36 | + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
37 | + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
38 | + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY | |
39 | + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
40 | + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE | |
41 | + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
42 | + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
43 | + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
44 | + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
45 | + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
46 | + | |
47 | + Julian Seward, Cambridge, UK. | |
48 | + jseward@acm.org | |
49 | + bzip2/libbzip2 version 1.0 of 21 March 2000 | |
50 | + | |
51 | + This program is based on (at least) the work of: | |
52 | + Mike Burrows | |
53 | + David Wheeler | |
54 | + Peter Fenwick | |
55 | + Alistair Moffat | |
56 | + Radford Neal | |
57 | + Ian H. Witten | |
58 | + Robert Sedgewick | |
59 | + Jon L. Bentley | |
60 | + | |
61 | + For more information on these sources, see the manual. | |
62 | +--*/ | |
63 | + | |
64 | + | |
65 | +#include "bzlib_private.h" | |
66 | + | |
67 | + | |
68 | +/*---------------------------------------------------*/ | |
69 | +static | |
70 | +void makeMaps_d ( DState* s ) | |
71 | +{ | |
72 | + Int32 i; | |
73 | + s->nInUse = 0; | |
74 | + for (i = 0; i < 256; i++) | |
75 | + if (s->inUse[i]) { | |
76 | + s->seqToUnseq[s->nInUse] = i; | |
77 | + s->nInUse++; | |
78 | + } | |
79 | +} | |
80 | + | |
81 | + | |
82 | +/*---------------------------------------------------*/ | |
83 | +#define RETURN(rrr) \ | |
84 | + { retVal = rrr; goto save_state_and_return; }; | |
85 | + | |
86 | +#define GET_BITS(lll,vvv,nnn) \ | |
87 | + case lll: s->state = lll; \ | |
88 | + while (True) { \ | |
89 | + if (s->bsLive >= nnn) { \ | |
90 | + UInt32 v; \ | |
91 | + v = (s->bsBuff >> \ | |
92 | + (s->bsLive-nnn)) & ((1 << nnn)-1); \ | |
93 | + s->bsLive -= nnn; \ | |
94 | + vvv = v; \ | |
95 | + break; \ | |
96 | + } \ | |
97 | + if (s->strm->avail_in == 0) RETURN(BZ_OK); \ | |
98 | + s->bsBuff \ | |
99 | + = (s->bsBuff << 8) | \ | |
100 | + ((UInt32) \ | |
101 | + (*((UChar*)(s->strm->next_in)))); \ | |
102 | + s->bsLive += 8; \ | |
103 | + s->strm->next_in++; \ | |
104 | + s->strm->avail_in--; \ | |
105 | + s->strm->total_in_lo32++; \ | |
106 | + if (s->strm->total_in_lo32 == 0) \ | |
107 | + s->strm->total_in_hi32++; \ | |
108 | + } | |
109 | + | |
110 | +#define GET_UCHAR(lll,uuu) \ | |
111 | + GET_BITS(lll,uuu,8) | |
112 | + | |
113 | +#define GET_BIT(lll,uuu) \ | |
114 | + GET_BITS(lll,uuu,1) | |
115 | + | |
116 | +/*---------------------------------------------------*/ | |
117 | +#define GET_MTF_VAL(label1,label2,lval) \ | |
118 | +{ \ | |
119 | + if (groupPos == 0) { \ | |
120 | + groupNo++; \ | |
121 | + if (groupNo >= nSelectors) \ | |
122 | + RETURN(BZ_DATA_ERROR); \ | |
123 | + groupPos = BZ_G_SIZE; \ | |
124 | + gSel = s->selector[groupNo]; \ | |
125 | + gMinlen = s->minLens[gSel]; \ | |
126 | + gLimit = &(s->limit[gSel][0]); \ | |
127 | + gPerm = &(s->perm[gSel][0]); \ | |
128 | + gBase = &(s->base[gSel][0]); \ | |
129 | + } \ | |
130 | + groupPos--; \ | |
131 | + zn = gMinlen; \ | |
132 | + GET_BITS(label1, zvec, zn); \ | |
133 | + while (1) { \ | |
134 | + if (zn > 20 /* the longest code */) \ | |
135 | + RETURN(BZ_DATA_ERROR); \ | |
136 | + if (zvec <= gLimit[zn]) break; \ | |
137 | + zn++; \ | |
138 | + GET_BIT(label2, zj); \ | |
139 | + zvec = (zvec << 1) | zj; \ | |
140 | + }; \ | |
141 | + if (zvec - gBase[zn] < 0 \ | |
142 | + || zvec - gBase[zn] >= BZ_MAX_ALPHA_SIZE) \ | |
143 | + RETURN(BZ_DATA_ERROR); \ | |
144 | + lval = gPerm[zvec - gBase[zn]]; \ | |
145 | +} | |
146 | + | |
147 | + | |
148 | +/*---------------------------------------------------*/ | |
149 | +Int32 BZ2_decompress ( DState* s ) | |
150 | +{ | |
151 | + UChar uc; | |
152 | + Int32 retVal; | |
153 | + Int32 minLen, maxLen; | |
154 | + bz_stream* strm = s->strm; | |
155 | + | |
156 | + /* stuff that needs to be saved/restored */ | |
157 | + Int32 i; | |
158 | + Int32 j; | |
159 | + Int32 t; | |
160 | + Int32 alphaSize; | |
161 | + Int32 nGroups; | |
162 | + Int32 nSelectors; | |
163 | + Int32 EOB; | |
164 | + Int32 groupNo; | |
165 | + Int32 groupPos; | |
166 | + Int32 nextSym; | |
167 | + Int32 nblockMAX; | |
168 | + Int32 nblock; | |
169 | + Int32 es; | |
170 | + Int32 N; | |
171 | + Int32 curr; | |
172 | + Int32 zt; | |
173 | + Int32 zn; | |
174 | + Int32 zvec; | |
175 | + Int32 zj; | |
176 | + Int32 gSel; | |
177 | + Int32 gMinlen; | |
178 | + Int32* gLimit; | |
179 | + Int32* gBase; | |
180 | + Int32* gPerm; | |
181 | + | |
182 | + if (s->state == BZ_X_MAGIC_1) { | |
183 | + /*initialise the save area*/ | |
184 | + s->save_i = 0; | |
185 | + s->save_j = 0; | |
186 | + s->save_t = 0; | |
187 | + s->save_alphaSize = 0; | |
188 | + s->save_nGroups = 0; | |
189 | + s->save_nSelectors = 0; | |
190 | + s->save_EOB = 0; | |
191 | + s->save_groupNo = 0; | |
192 | + s->save_groupPos = 0; | |
193 | + s->save_nextSym = 0; | |
194 | + s->save_nblockMAX = 0; | |
195 | + s->save_nblock = 0; | |
196 | + s->save_es = 0; | |
197 | + s->save_N = 0; | |
198 | + s->save_curr = 0; | |
199 | + s->save_zt = 0; | |
200 | + s->save_zn = 0; | |
201 | + s->save_zvec = 0; | |
202 | + s->save_zj = 0; | |
203 | + s->save_gSel = 0; | |
204 | + s->save_gMinlen = 0; | |
205 | + s->save_gLimit = NULL; | |
206 | + s->save_gBase = NULL; | |
207 | + s->save_gPerm = NULL; | |
208 | + } | |
209 | + | |
210 | + /*restore from the save area*/ | |
211 | + i = s->save_i; | |
212 | + j = s->save_j; | |
213 | + t = s->save_t; | |
214 | + alphaSize = s->save_alphaSize; | |
215 | + nGroups = s->save_nGroups; | |
216 | + nSelectors = s->save_nSelectors; | |
217 | + EOB = s->save_EOB; | |
218 | + groupNo = s->save_groupNo; | |
219 | + groupPos = s->save_groupPos; | |
220 | + nextSym = s->save_nextSym; | |
221 | + nblockMAX = s->save_nblockMAX; | |
222 | + nblock = s->save_nblock; | |
223 | + es = s->save_es; | |
224 | + N = s->save_N; | |
225 | + curr = s->save_curr; | |
226 | + zt = s->save_zt; | |
227 | + zn = s->save_zn; | |
228 | + zvec = s->save_zvec; | |
229 | + zj = s->save_zj; | |
230 | + gSel = s->save_gSel; | |
231 | + gMinlen = s->save_gMinlen; | |
232 | + gLimit = s->save_gLimit; | |
233 | + gBase = s->save_gBase; | |
234 | + gPerm = s->save_gPerm; | |
235 | + | |
236 | + retVal = BZ_OK; | |
237 | + | |
238 | + switch (s->state) { | |
239 | + | |
240 | + GET_UCHAR(BZ_X_MAGIC_1, uc); | |
241 | + if (uc != BZ_HDR_B) RETURN(BZ_DATA_ERROR_MAGIC); | |
242 | + | |
243 | + GET_UCHAR(BZ_X_MAGIC_2, uc); | |
244 | + if (uc != BZ_HDR_Z) RETURN(BZ_DATA_ERROR_MAGIC); | |
245 | + | |
246 | + GET_UCHAR(BZ_X_MAGIC_3, uc) | |
247 | + if (uc != BZ_HDR_h) RETURN(BZ_DATA_ERROR_MAGIC); | |
248 | + | |
249 | + GET_BITS(BZ_X_MAGIC_4, s->blockSize100k, 8) | |
250 | + if (s->blockSize100k < (BZ_HDR_0 + 1) || | |
251 | + s->blockSize100k > (BZ_HDR_0 + 9)) RETURN(BZ_DATA_ERROR_MAGIC); | |
252 | + s->blockSize100k -= BZ_HDR_0; | |
253 | + | |
254 | + if (s->smallDecompress) { | |
255 | + s->ll16 = BZALLOC( s->blockSize100k * 100000 * sizeof(UInt16) ); | |
256 | + s->ll4 = BZALLOC( | |
257 | + ((1 + s->blockSize100k * 100000) >> 1) * sizeof(UChar) | |
258 | + ); | |
259 | + if (s->ll16 == NULL || s->ll4 == NULL) RETURN(BZ_MEM_ERROR); | |
260 | + } else { | |
261 | + s->tt = BZALLOC( s->blockSize100k * 100000 * sizeof(Int32) ); | |
262 | + if (s->tt == NULL) RETURN(BZ_MEM_ERROR); | |
263 | + } | |
264 | + | |
265 | + GET_UCHAR(BZ_X_BLKHDR_1, uc); | |
266 | + | |
267 | + if (uc == 0x17) goto endhdr_2; | |
268 | + if (uc != 0x31) RETURN(BZ_DATA_ERROR); | |
269 | + GET_UCHAR(BZ_X_BLKHDR_2, uc); | |
270 | + if (uc != 0x41) RETURN(BZ_DATA_ERROR); | |
271 | + GET_UCHAR(BZ_X_BLKHDR_3, uc); | |
272 | + if (uc != 0x59) RETURN(BZ_DATA_ERROR); | |
273 | + GET_UCHAR(BZ_X_BLKHDR_4, uc); | |
274 | + if (uc != 0x26) RETURN(BZ_DATA_ERROR); | |
275 | + GET_UCHAR(BZ_X_BLKHDR_5, uc); | |
276 | + if (uc != 0x53) RETURN(BZ_DATA_ERROR); | |
277 | + GET_UCHAR(BZ_X_BLKHDR_6, uc); | |
278 | + if (uc != 0x59) RETURN(BZ_DATA_ERROR); | |
279 | + | |
280 | + s->currBlockNo++; | |
281 | + if (s->verbosity >= 2) | |
282 | + VPrintf1 ( "\n [%d: huff+mtf ", s->currBlockNo ); | |
283 | + | |
284 | + s->storedBlockCRC = 0; | |
285 | + GET_UCHAR(BZ_X_BCRC_1, uc); | |
286 | + s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc); | |
287 | + GET_UCHAR(BZ_X_BCRC_2, uc); | |
288 | + s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc); | |
289 | + GET_UCHAR(BZ_X_BCRC_3, uc); | |
290 | + s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc); | |
291 | + GET_UCHAR(BZ_X_BCRC_4, uc); | |
292 | + s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc); | |
293 | + | |
294 | + GET_BITS(BZ_X_RANDBIT, s->blockRandomised, 1); | |
295 | + | |
296 | + s->origPtr = 0; | |
297 | + GET_UCHAR(BZ_X_ORIGPTR_1, uc); | |
298 | + s->origPtr = (s->origPtr << 8) | ((Int32)uc); | |
299 | + GET_UCHAR(BZ_X_ORIGPTR_2, uc); | |
300 | + s->origPtr = (s->origPtr << 8) | ((Int32)uc); | |
301 | + GET_UCHAR(BZ_X_ORIGPTR_3, uc); | |
302 | + s->origPtr = (s->origPtr << 8) | ((Int32)uc); | |
303 | + | |
304 | + if (s->origPtr < 0) | |
305 | + RETURN(BZ_DATA_ERROR); | |
306 | + if (s->origPtr > 10 + 100000*s->blockSize100k) | |
307 | + RETURN(BZ_DATA_ERROR); | |
308 | + | |
309 | + /*--- Receive the mapping table ---*/ | |
310 | + for (i = 0; i < 16; i++) { | |
311 | + GET_BIT(BZ_X_MAPPING_1, uc); | |
312 | + if (uc == 1) | |
313 | + s->inUse16[i] = True; else | |
314 | + s->inUse16[i] = False; | |
315 | + } | |
316 | + | |
317 | + for (i = 0; i < 256; i++) s->inUse[i] = False; | |
318 | + | |
319 | + for (i = 0; i < 16; i++) | |
320 | + if (s->inUse16[i]) | |
321 | + for (j = 0; j < 16; j++) { | |
322 | + GET_BIT(BZ_X_MAPPING_2, uc); | |
323 | + if (uc == 1) s->inUse[i * 16 + j] = True; | |
324 | + } | |
325 | + makeMaps_d ( s ); | |
326 | + if (s->nInUse == 0) RETURN(BZ_DATA_ERROR); | |
327 | + alphaSize = s->nInUse+2; | |
328 | + | |
329 | + /*--- Now the selectors ---*/ | |
330 | + GET_BITS(BZ_X_SELECTOR_1, nGroups, 3); | |
331 | + if (nGroups < 2 || nGroups > 6) RETURN(BZ_DATA_ERROR); | |
332 | + GET_BITS(BZ_X_SELECTOR_2, nSelectors, 15); | |
333 | + if (nSelectors < 1) RETURN(BZ_DATA_ERROR); | |
334 | + for (i = 0; i < nSelectors; i++) { | |
335 | + j = 0; | |
336 | + while (True) { | |
337 | + GET_BIT(BZ_X_SELECTOR_3, uc); | |
338 | + if (uc == 0) break; | |
339 | + j++; | |
340 | + if (j >= nGroups) RETURN(BZ_DATA_ERROR); | |
341 | + } | |
342 | + s->selectorMtf[i] = j; | |
343 | + } | |
344 | + | |
345 | + /*--- Undo the MTF values for the selectors. ---*/ | |
346 | + { | |
347 | + UChar pos[BZ_N_GROUPS], tmp, v; | |
348 | + for (v = 0; v < nGroups; v++) pos[v] = v; | |
349 | + | |
350 | + for (i = 0; i < nSelectors; i++) { | |
351 | + v = s->selectorMtf[i]; | |
352 | + tmp = pos[v]; | |
353 | + while (v > 0) { pos[v] = pos[v-1]; v--; } | |
354 | + pos[0] = tmp; | |
355 | + s->selector[i] = tmp; | |
356 | + } | |
357 | + } | |
358 | + | |
359 | + /*--- Now the coding tables ---*/ | |
360 | + for (t = 0; t < nGroups; t++) { | |
361 | + GET_BITS(BZ_X_CODING_1, curr, 5); | |
362 | + for (i = 0; i < alphaSize; i++) { | |
363 | + while (True) { | |
364 | + if (curr < 1 || curr > 20) RETURN(BZ_DATA_ERROR); | |
365 | + GET_BIT(BZ_X_CODING_2, uc); | |
366 | + if (uc == 0) break; | |
367 | + GET_BIT(BZ_X_CODING_3, uc); | |
368 | + if (uc == 0) curr++; else curr--; | |
369 | + } | |
370 | + s->len[t][i] = curr; | |
371 | + } | |
372 | + } | |
373 | + | |
374 | + /*--- Create the Huffman decoding tables ---*/ | |
375 | + for (t = 0; t < nGroups; t++) { | |
376 | + minLen = 32; | |
377 | + maxLen = 0; | |
378 | + for (i = 0; i < alphaSize; i++) { | |
379 | + if (s->len[t][i] > maxLen) maxLen = s->len[t][i]; | |
380 | + if (s->len[t][i] < minLen) minLen = s->len[t][i]; | |
381 | + } | |
382 | + BZ2_hbCreateDecodeTables ( | |
383 | + &(s->limit[t][0]), | |
384 | + &(s->base[t][0]), | |
385 | + &(s->perm[t][0]), | |
386 | + &(s->len[t][0]), | |
387 | + minLen, maxLen, alphaSize | |
388 | + ); | |
389 | + s->minLens[t] = minLen; | |
390 | + } | |
391 | + | |
392 | + /*--- Now the MTF values ---*/ | |
393 | + | |
394 | + EOB = s->nInUse+1; | |
395 | + nblockMAX = 100000 * s->blockSize100k; | |
396 | + groupNo = -1; | |
397 | + groupPos = 0; | |
398 | + | |
399 | + for (i = 0; i <= 255; i++) s->unzftab[i] = 0; | |
400 | + | |
401 | + /*-- MTF init --*/ | |
402 | + { | |
403 | + Int32 ii, jj, kk; | |
404 | + kk = MTFA_SIZE-1; | |
405 | + for (ii = 256 / MTFL_SIZE - 1; ii >= 0; ii--) { | |
406 | + for (jj = MTFL_SIZE-1; jj >= 0; jj--) { | |
407 | + s->mtfa[kk] = (UChar)(ii * MTFL_SIZE + jj); | |
408 | + kk--; | |
409 | + } | |
410 | + s->mtfbase[ii] = kk + 1; | |
411 | + } | |
412 | + } | |
413 | + /*-- end MTF init --*/ | |
414 | + | |
415 | + nblock = 0; | |
416 | + GET_MTF_VAL(BZ_X_MTF_1, BZ_X_MTF_2, nextSym); | |
417 | + | |
418 | + while (True) { | |
419 | + | |
420 | +#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG) | |
421 | + WATCHDOG_RESET(); | |
422 | +#endif | |
423 | + if (nextSym == EOB) break; | |
424 | + | |
425 | + if (nextSym == BZ_RUNA || nextSym == BZ_RUNB) { | |
426 | + | |
427 | + es = -1; | |
428 | + N = 1; | |
429 | + do { | |
430 | + if (nextSym == BZ_RUNA) es = es + (0+1) * N; else | |
431 | + if (nextSym == BZ_RUNB) es = es + (1+1) * N; | |
432 | + N = N * 2; | |
433 | + GET_MTF_VAL(BZ_X_MTF_3, BZ_X_MTF_4, nextSym); | |
434 | + } | |
435 | + while (nextSym == BZ_RUNA || nextSym == BZ_RUNB); | |
436 | + | |
437 | + es++; | |
438 | + uc = s->seqToUnseq[ s->mtfa[s->mtfbase[0]] ]; | |
439 | + s->unzftab[uc] += es; | |
440 | + | |
441 | + if (s->smallDecompress) | |
442 | + while (es > 0) { | |
443 | + if (nblock >= nblockMAX) RETURN(BZ_DATA_ERROR); | |
444 | + s->ll16[nblock] = (UInt16)uc; | |
445 | + nblock++; | |
446 | + es--; | |
447 | + } | |
448 | + else | |
449 | + while (es > 0) { | |
450 | + if (nblock >= nblockMAX) RETURN(BZ_DATA_ERROR); | |
451 | + s->tt[nblock] = (UInt32)uc; | |
452 | + nblock++; | |
453 | + es--; | |
454 | + }; | |
455 | + | |
456 | + continue; | |
457 | + | |
458 | + } else { | |
459 | + | |
460 | + if (nblock >= nblockMAX) RETURN(BZ_DATA_ERROR); | |
461 | + | |
462 | + /*-- uc = MTF ( nextSym-1 ) --*/ | |
463 | + { | |
464 | + Int32 ii, jj, kk, pp, lno, off; | |
465 | + UInt32 nn; | |
466 | + nn = (UInt32)(nextSym - 1); | |
467 | + | |
468 | + if (nn < MTFL_SIZE) { | |
469 | + /* avoid general-case expense */ | |
470 | + pp = s->mtfbase[0]; | |
471 | + uc = s->mtfa[pp+nn]; | |
472 | + while (nn > 3) { | |
473 | + Int32 z = pp+nn; | |
474 | + s->mtfa[(z) ] = s->mtfa[(z)-1]; | |
475 | + s->mtfa[(z)-1] = s->mtfa[(z)-2]; | |
476 | + s->mtfa[(z)-2] = s->mtfa[(z)-3]; | |
477 | + s->mtfa[(z)-3] = s->mtfa[(z)-4]; | |
478 | + nn -= 4; | |
479 | + } | |
480 | + while (nn > 0) { | |
481 | + s->mtfa[(pp+nn)] = s->mtfa[(pp+nn)-1]; nn--; | |
482 | + }; | |
483 | + s->mtfa[pp] = uc; | |
484 | + } else { | |
485 | + /* general case */ | |
486 | + lno = nn / MTFL_SIZE; | |
487 | + off = nn % MTFL_SIZE; | |
488 | + pp = s->mtfbase[lno] + off; | |
489 | + uc = s->mtfa[pp]; | |
490 | + while (pp > s->mtfbase[lno]) { | |
491 | + s->mtfa[pp] = s->mtfa[pp-1]; pp--; | |
492 | + }; | |
493 | + s->mtfbase[lno]++; | |
494 | + while (lno > 0) { | |
495 | + s->mtfbase[lno]--; | |
496 | + s->mtfa[s->mtfbase[lno]] | |
497 | + = s->mtfa[s->mtfbase[lno-1] + MTFL_SIZE - 1]; | |
498 | + lno--; | |
499 | + } | |
500 | + s->mtfbase[0]--; | |
501 | + s->mtfa[s->mtfbase[0]] = uc; | |
502 | + if (s->mtfbase[0] == 0) { | |
503 | + kk = MTFA_SIZE-1; | |
504 | + for (ii = 256 / MTFL_SIZE-1; ii >= 0; ii--) { | |
505 | +#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG) | |
506 | + WATCHDOG_RESET(); | |
507 | +#endif | |
508 | + for (jj = MTFL_SIZE-1; jj >= 0; jj--) { | |
509 | + s->mtfa[kk] = s->mtfa[s->mtfbase[ii] + jj]; | |
510 | + kk--; | |
511 | + } | |
512 | + s->mtfbase[ii] = kk + 1; | |
513 | + } | |
514 | + } | |
515 | + } | |
516 | + } | |
517 | + /*-- end uc = MTF ( nextSym-1 ) --*/ | |
518 | + | |
519 | + s->unzftab[s->seqToUnseq[uc]]++; | |
520 | + if (s->smallDecompress) | |
521 | + s->ll16[nblock] = (UInt16)(s->seqToUnseq[uc]); else | |
522 | + s->tt[nblock] = (UInt32)(s->seqToUnseq[uc]); | |
523 | + nblock++; | |
524 | + | |
525 | + GET_MTF_VAL(BZ_X_MTF_5, BZ_X_MTF_6, nextSym); | |
526 | + continue; | |
527 | + } | |
528 | + } | |
529 | + | |
530 | + /* Now we know what nblock is, we can do a better sanity | |
531 | + check on s->origPtr. | |
532 | + */ | |
533 | + if (s->origPtr < 0 || s->origPtr >= nblock) | |
534 | + RETURN(BZ_DATA_ERROR); | |
535 | + | |
536 | + s->state_out_len = 0; | |
537 | + s->state_out_ch = 0; | |
538 | + BZ_INITIALISE_CRC ( s->calculatedBlockCRC ); | |
539 | + s->state = BZ_X_OUTPUT; | |
540 | + if (s->verbosity >= 2) VPrintf0 ( "rt+rld" ); | |
541 | + | |
542 | + /*-- Set up cftab to facilitate generation of T^(-1) --*/ | |
543 | + s->cftab[0] = 0; | |
544 | + for (i = 1; i <= 256; i++) s->cftab[i] = s->unzftab[i-1]; | |
545 | + for (i = 1; i <= 256; i++) s->cftab[i] += s->cftab[i-1]; | |
546 | + | |
547 | + if (s->smallDecompress) { | |
548 | + | |
549 | + /*-- Make a copy of cftab, used in generation of T --*/ | |
550 | + for (i = 0; i <= 256; i++) s->cftabCopy[i] = s->cftab[i]; | |
551 | + | |
552 | + /*-- compute the T vector --*/ | |
553 | + for (i = 0; i < nblock; i++) { | |
554 | + uc = (UChar)(s->ll16[i]); | |
555 | + SET_LL(i, s->cftabCopy[uc]); | |
556 | + s->cftabCopy[uc]++; | |
557 | + } | |
558 | + | |
559 | + /*-- Compute T^(-1) by pointer reversal on T --*/ | |
560 | + i = s->origPtr; | |
561 | + j = GET_LL(i); | |
562 | + do { | |
563 | + Int32 tmp = GET_LL(j); | |
564 | + SET_LL(j, i); | |
565 | + i = j; | |
566 | + j = tmp; | |
567 | + } | |
568 | + while (i != s->origPtr); | |
569 | + | |
570 | +#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG) | |
571 | + WATCHDOG_RESET(); | |
572 | +#endif | |
573 | + s->tPos = s->origPtr; | |
574 | + s->nblock_used = 0; | |
575 | + if (s->blockRandomised) { | |
576 | + BZ_RAND_INIT_MASK; | |
577 | + BZ_GET_SMALL(s->k0); s->nblock_used++; | |
578 | + BZ_RAND_UPD_MASK; s->k0 ^= BZ_RAND_MASK; | |
579 | + } else { | |
580 | + BZ_GET_SMALL(s->k0); s->nblock_used++; | |
581 | + } | |
582 | + | |
583 | + } else { | |
584 | + | |
585 | +#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG) | |
586 | + WATCHDOG_RESET(); | |
587 | +#endif | |
588 | + /*-- compute the T^(-1) vector --*/ | |
589 | + for (i = 0; i < nblock; i++) { | |
590 | + uc = (UChar)(s->tt[i] & 0xff); | |
591 | + s->tt[s->cftab[uc]] |= (i << 8); | |
592 | + s->cftab[uc]++; | |
593 | + } | |
594 | + | |
595 | + s->tPos = s->tt[s->origPtr] >> 8; | |
596 | + s->nblock_used = 0; | |
597 | + if (s->blockRandomised) { | |
598 | + BZ_RAND_INIT_MASK; | |
599 | + BZ_GET_FAST(s->k0); s->nblock_used++; | |
600 | + BZ_RAND_UPD_MASK; s->k0 ^= BZ_RAND_MASK; | |
601 | + } else { | |
602 | + BZ_GET_FAST(s->k0); s->nblock_used++; | |
603 | + } | |
604 | + | |
605 | + } | |
606 | + | |
607 | + RETURN(BZ_OK); | |
608 | + | |
609 | + | |
610 | + endhdr_2: | |
611 | + | |
612 | + GET_UCHAR(BZ_X_ENDHDR_2, uc); | |
613 | + if (uc != 0x72) RETURN(BZ_DATA_ERROR); | |
614 | + GET_UCHAR(BZ_X_ENDHDR_3, uc); | |
615 | + if (uc != 0x45) RETURN(BZ_DATA_ERROR); | |
616 | + GET_UCHAR(BZ_X_ENDHDR_4, uc); | |
617 | + if (uc != 0x38) RETURN(BZ_DATA_ERROR); | |
618 | + GET_UCHAR(BZ_X_ENDHDR_5, uc); | |
619 | + if (uc != 0x50) RETURN(BZ_DATA_ERROR); | |
620 | + GET_UCHAR(BZ_X_ENDHDR_6, uc); | |
621 | + if (uc != 0x90) RETURN(BZ_DATA_ERROR); | |
622 | + | |
623 | + s->storedCombinedCRC = 0; | |
624 | + GET_UCHAR(BZ_X_CCRC_1, uc); | |
625 | + s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc); | |
626 | + GET_UCHAR(BZ_X_CCRC_2, uc); | |
627 | + s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc); | |
628 | + GET_UCHAR(BZ_X_CCRC_3, uc); | |
629 | + s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc); | |
630 | + GET_UCHAR(BZ_X_CCRC_4, uc); | |
631 | + s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc); | |
632 | + | |
633 | + s->state = BZ_X_IDLE; | |
634 | + RETURN(BZ_STREAM_END); | |
635 | + | |
636 | + default: AssertH ( False, 4001 ); | |
637 | + } | |
638 | + | |
639 | + AssertH ( False, 4002 ); | |
640 | + | |
641 | + save_state_and_return: | |
642 | + | |
643 | + s->save_i = i; | |
644 | + s->save_j = j; | |
645 | + s->save_t = t; | |
646 | + s->save_alphaSize = alphaSize; | |
647 | + s->save_nGroups = nGroups; | |
648 | + s->save_nSelectors = nSelectors; | |
649 | + s->save_EOB = EOB; | |
650 | + s->save_groupNo = groupNo; | |
651 | + s->save_groupPos = groupPos; | |
652 | + s->save_nextSym = nextSym; | |
653 | + s->save_nblockMAX = nblockMAX; | |
654 | + s->save_nblock = nblock; | |
655 | + s->save_es = es; | |
656 | + s->save_N = N; | |
657 | + s->save_curr = curr; | |
658 | + s->save_zt = zt; | |
659 | + s->save_zn = zn; | |
660 | + s->save_zvec = zvec; | |
661 | + s->save_zj = zj; | |
662 | + s->save_gSel = gSel; | |
663 | + s->save_gMinlen = gMinlen; | |
664 | + s->save_gLimit = gLimit; | |
665 | + s->save_gBase = gBase; | |
666 | + s->save_gPerm = gPerm; | |
667 | + | |
668 | + return retVal; | |
669 | +} | |
670 | + | |
671 | + | |
672 | +/*-------------------------------------------------------------*/ | |
673 | +/*--- end decompress.c ---*/ | |
674 | +/*-------------------------------------------------------------*/ |
lib/bzip2/bzlib_huffman.c
1 | +#include <config.h> | |
2 | + | |
3 | +/*-------------------------------------------------------------*/ | |
4 | +/*--- Huffman coding low-level stuff ---*/ | |
5 | +/*--- huffman.c ---*/ | |
6 | +/*-------------------------------------------------------------*/ | |
7 | + | |
8 | +/*-- | |
9 | + This file is a part of bzip2 and/or libbzip2, a program and | |
10 | + library for lossless, block-sorting data compression. | |
11 | + | |
12 | + Copyright (C) 1996-2002 Julian R Seward. All rights reserved. | |
13 | + | |
14 | + Redistribution and use in source and binary forms, with or without | |
15 | + modification, are permitted provided that the following conditions | |
16 | + are met: | |
17 | + | |
18 | + 1. Redistributions of source code must retain the above copyright | |
19 | + notice, this list of conditions and the following disclaimer. | |
20 | + | |
21 | + 2. The origin of this software must not be misrepresented; you must | |
22 | + not claim that you wrote the original software. If you use this | |
23 | + software in a product, an acknowledgment in the product | |
24 | + documentation would be appreciated but is not required. | |
25 | + | |
26 | + 3. Altered source versions must be plainly marked as such, and must | |
27 | + not be misrepresented as being the original software. | |
28 | + | |
29 | + 4. The name of the author may not be used to endorse or promote | |
30 | + products derived from this software without specific prior written | |
31 | + permission. | |
32 | + | |
33 | + THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS | |
34 | + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
35 | + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
36 | + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY | |
37 | + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
38 | + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE | |
39 | + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
40 | + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
41 | + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
42 | + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
43 | + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
44 | + | |
45 | + Julian Seward, Cambridge, UK. | |
46 | + jseward@acm.org | |
47 | + bzip2/libbzip2 version 1.0 of 21 March 2000 | |
48 | + | |
49 | + This program is based on (at least) the work of: | |
50 | + Mike Burrows | |
51 | + David Wheeler | |
52 | + Peter Fenwick | |
53 | + Alistair Moffat | |
54 | + Radford Neal | |
55 | + Ian H. Witten | |
56 | + Robert Sedgewick | |
57 | + Jon L. Bentley | |
58 | + | |
59 | + For more information on these sources, see the manual. | |
60 | +--*/ | |
61 | + | |
62 | + | |
63 | +#include "bzlib_private.h" | |
64 | + | |
65 | +/*---------------------------------------------------*/ | |
66 | +#define WEIGHTOF(zz0) ((zz0) & 0xffffff00) | |
67 | +#define DEPTHOF(zz1) ((zz1) & 0x000000ff) | |
68 | +#define MYMAX(zz2,zz3) ((zz2) > (zz3) ? (zz2) : (zz3)) | |
69 | + | |
70 | +#define ADDWEIGHTS(zw1,zw2) \ | |
71 | + (WEIGHTOF(zw1)+WEIGHTOF(zw2)) | \ | |
72 | + (1 + MYMAX(DEPTHOF(zw1),DEPTHOF(zw2))) | |
73 | + | |
74 | +#define UPHEAP(z) \ | |
75 | +{ \ | |
76 | + Int32 zz, tmp; \ | |
77 | + zz = z; tmp = heap[zz]; \ | |
78 | + while (weight[tmp] < weight[heap[zz >> 1]]) { \ | |
79 | + heap[zz] = heap[zz >> 1]; \ | |
80 | + zz >>= 1; \ | |
81 | + } \ | |
82 | + heap[zz] = tmp; \ | |
83 | +} | |
84 | + | |
85 | +#define DOWNHEAP(z) \ | |
86 | +{ \ | |
87 | + Int32 zz, yy, tmp; \ | |
88 | + zz = z; tmp = heap[zz]; \ | |
89 | + while (True) { \ | |
90 | + yy = zz << 1; \ | |
91 | + if (yy > nHeap) break; \ | |
92 | + if (yy < nHeap && \ | |
93 | + weight[heap[yy+1]] < weight[heap[yy]]) \ | |
94 | + yy++; \ | |
95 | + if (weight[tmp] < weight[heap[yy]]) break; \ | |
96 | + heap[zz] = heap[yy]; \ | |
97 | + zz = yy; \ | |
98 | + } \ | |
99 | + heap[zz] = tmp; \ | |
100 | +} | |
101 | + | |
102 | + | |
103 | +/*---------------------------------------------------*/ | |
104 | +void BZ2_hbMakeCodeLengths ( UChar *len, | |
105 | + Int32 *freq, | |
106 | + Int32 alphaSize, | |
107 | + Int32 maxLen ) | |
108 | +{ | |
109 | + /*-- | |
110 | + Nodes and heap entries run from 1. Entry 0 | |
111 | + for both the heap and nodes is a sentinel. | |
112 | + --*/ | |
113 | + Int32 nNodes, nHeap, n1, n2, i, j, k; | |
114 | + Bool tooLong; | |
115 | + | |
116 | + Int32 heap [ BZ_MAX_ALPHA_SIZE + 2 ]; | |
117 | + Int32 weight [ BZ_MAX_ALPHA_SIZE * 2 ]; | |
118 | + Int32 parent [ BZ_MAX_ALPHA_SIZE * 2 ]; | |
119 | + | |
120 | + for (i = 0; i < alphaSize; i++) | |
121 | + weight[i+1] = (freq[i] == 0 ? 1 : freq[i]) << 8; | |
122 | + | |
123 | + while (True) { | |
124 | + | |
125 | + nNodes = alphaSize; | |
126 | + nHeap = 0; | |
127 | + | |
128 | + heap[0] = 0; | |
129 | + weight[0] = 0; | |
130 | + parent[0] = -2; | |
131 | + | |
132 | + for (i = 1; i <= alphaSize; i++) { | |
133 | + parent[i] = -1; | |
134 | + nHeap++; | |
135 | + heap[nHeap] = i; | |
136 | + UPHEAP(nHeap); | |
137 | + } | |
138 | + | |
139 | + AssertH( nHeap < (BZ_MAX_ALPHA_SIZE+2), 2001 ); | |
140 | + | |
141 | + while (nHeap > 1) { | |
142 | + n1 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1); | |
143 | + n2 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1); | |
144 | + nNodes++; | |
145 | + parent[n1] = parent[n2] = nNodes; | |
146 | + weight[nNodes] = ADDWEIGHTS(weight[n1], weight[n2]); | |
147 | + parent[nNodes] = -1; | |
148 | + nHeap++; | |
149 | + heap[nHeap] = nNodes; | |
150 | + UPHEAP(nHeap); | |
151 | + } | |
152 | + | |
153 | + AssertH( nNodes < (BZ_MAX_ALPHA_SIZE * 2), 2002 ); | |
154 | + | |
155 | + tooLong = False; | |
156 | + for (i = 1; i <= alphaSize; i++) { | |
157 | + j = 0; | |
158 | + k = i; | |
159 | + while (parent[k] >= 0) { k = parent[k]; j++; } | |
160 | + len[i-1] = j; | |
161 | + if (j > maxLen) tooLong = True; | |
162 | + } | |
163 | + | |
164 | + if (! tooLong) break; | |
165 | + | |
166 | + for (i = 1; i < alphaSize; i++) { | |
167 | + j = weight[i] >> 8; | |
168 | + j = 1 + (j / 2); | |
169 | + weight[i] = j << 8; | |
170 | + } | |
171 | + } | |
172 | +} | |
173 | + | |
174 | + | |
175 | +/*---------------------------------------------------*/ | |
176 | +void BZ2_hbAssignCodes ( Int32 *code, | |
177 | + UChar *length, | |
178 | + Int32 minLen, | |
179 | + Int32 maxLen, | |
180 | + Int32 alphaSize ) | |
181 | +{ | |
182 | + Int32 n, vec, i; | |
183 | + | |
184 | + vec = 0; | |
185 | + for (n = minLen; n <= maxLen; n++) { | |
186 | + for (i = 0; i < alphaSize; i++) | |
187 | + if (length[i] == n) { code[i] = vec; vec++; }; | |
188 | + vec <<= 1; | |
189 | + } | |
190 | +} | |
191 | + | |
192 | + | |
193 | +/*---------------------------------------------------*/ | |
194 | +void BZ2_hbCreateDecodeTables ( Int32 *limit, | |
195 | + Int32 *base, | |
196 | + Int32 *perm, | |
197 | + UChar *length, | |
198 | + Int32 minLen, | |
199 | + Int32 maxLen, | |
200 | + Int32 alphaSize ) | |
201 | +{ | |
202 | + Int32 pp, i, j, vec; | |
203 | + | |
204 | + pp = 0; | |
205 | + for (i = minLen; i <= maxLen; i++) | |
206 | + for (j = 0; j < alphaSize; j++) | |
207 | + if (length[j] == i) { perm[pp] = j; pp++; }; | |
208 | + | |
209 | + for (i = 0; i < BZ_MAX_CODE_LEN; i++) base[i] = 0; | |
210 | + for (i = 0; i < alphaSize; i++) base[length[i]+1]++; | |
211 | + | |
212 | + for (i = 1; i < BZ_MAX_CODE_LEN; i++) base[i] += base[i-1]; | |
213 | + | |
214 | + for (i = 0; i < BZ_MAX_CODE_LEN; i++) limit[i] = 0; | |
215 | + vec = 0; | |
216 | + | |
217 | + for (i = minLen; i <= maxLen; i++) { | |
218 | + vec += (base[i+1] - base[i]); | |
219 | + limit[i] = vec-1; | |
220 | + vec <<= 1; | |
221 | + } | |
222 | + for (i = minLen + 1; i <= maxLen; i++) | |
223 | + base[i] = ((limit[i-1] + 1) << 1) - base[i]; | |
224 | +} | |
225 | + | |
226 | + | |
227 | +/*-------------------------------------------------------------*/ | |
228 | +/*--- end huffman.c ---*/ | |
229 | +/*-------------------------------------------------------------*/ |
lib/bzip2/bzlib_private.h
1 | +/* | |
2 | + * This file is a modified version of bzlib_private.h from the bzip2-1.0.2 | |
3 | + * distribution which can be found at http://sources.redhat.com/bzip2/ | |
4 | + */ | |
5 | + | |
6 | +/*-------------------------------------------------------------*/ | |
7 | +/*--- Private header file for the library. ---*/ | |
8 | +/*--- bzlib_private.h ---*/ | |
9 | +/*-------------------------------------------------------------*/ | |
10 | + | |
11 | +/*-- | |
12 | + This file is a part of bzip2 and/or libbzip2, a program and | |
13 | + library for lossless, block-sorting data compression. | |
14 | + | |
15 | + Copyright (C) 1996-2002 Julian R Seward. All rights reserved. | |
16 | + | |
17 | + Redistribution and use in source and binary forms, with or without | |
18 | + modification, are permitted provided that the following conditions | |
19 | + are met: | |
20 | + | |
21 | + 1. Redistributions of source code must retain the above copyright | |
22 | + notice, this list of conditions and the following disclaimer. | |
23 | + | |
24 | + 2. The origin of this software must not be misrepresented; you must | |
25 | + not claim that you wrote the original software. If you use this | |
26 | + software in a product, an acknowledgment in the product | |
27 | + documentation would be appreciated but is not required. | |
28 | + | |
29 | + 3. Altered source versions must be plainly marked as such, and must | |
30 | + not be misrepresented as being the original software. | |
31 | + | |
32 | + 4. The name of the author may not be used to endorse or promote | |
33 | + products derived from this software without specific prior written | |
34 | + permission. | |
35 | + | |
36 | + THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS | |
37 | + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
38 | + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
39 | + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY | |
40 | + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
41 | + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE | |
42 | + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
43 | + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
44 | + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
45 | + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
46 | + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
47 | + | |
48 | + Julian Seward, Cambridge, UK. | |
49 | + jseward@acm.org | |
50 | + bzip2/libbzip2 version 1.0 of 21 March 2000 | |
51 | + | |
52 | + This program is based on (at least) the work of: | |
53 | + Mike Burrows | |
54 | + David Wheeler | |
55 | + Peter Fenwick | |
56 | + Alistair Moffat | |
57 | + Radford Neal | |
58 | + Ian H. Witten | |
59 | + Robert Sedgewick | |
60 | + Jon L. Bentley | |
61 | + | |
62 | + For more information on these sources, see the manual. | |
63 | +--*/ | |
64 | + | |
65 | + | |
66 | +#ifndef _BZLIB_PRIVATE_H | |
67 | +#define _BZLIB_PRIVATE_H | |
68 | + | |
69 | +#include <malloc.h> | |
70 | + | |
71 | +#include "bzlib.h" | |
72 | + | |
73 | +#ifndef BZ_NO_STDIO | |
74 | +#include <stdio.h> | |
75 | +#include <ctype.h> | |
76 | +#include <string.h> | |
77 | +#endif | |
78 | + | |
79 | + | |
80 | +/*-- General stuff. --*/ | |
81 | + | |
82 | +#define BZ_VERSION "1.0.2, 30-Dec-2001" | |
83 | + | |
84 | +typedef char Char; | |
85 | +typedef unsigned char Bool; | |
86 | +typedef unsigned char UChar; | |
87 | +typedef int Int32; | |
88 | +typedef unsigned int UInt32; | |
89 | +typedef short Int16; | |
90 | +typedef unsigned short UInt16; | |
91 | + | |
92 | +#define True ((Bool)1) | |
93 | +#define False ((Bool)0) | |
94 | + | |
95 | +#ifndef __GNUC__ | |
96 | +#define __inline__ /* */ | |
97 | +#endif | |
98 | + | |
99 | +#ifndef BZ_NO_STDIO | |
100 | +extern void BZ2_bz__AssertH__fail ( int errcode ); | |
101 | +#define AssertH(cond,errcode) \ | |
102 | + { if (!(cond)) BZ2_bz__AssertH__fail ( errcode ); } | |
103 | +#if BZ_DEBUG | |
104 | +#define AssertD(cond,msg) \ | |
105 | + { if (!(cond)) { \ | |
106 | + fprintf ( stderr, \ | |
107 | + "\n\nlibbzip2(debug build): internal error\n\t%s\n", msg );\ | |
108 | + exit(1); \ | |
109 | + }} | |
110 | +#else | |
111 | +#define AssertD(cond,msg) /* */ | |
112 | +#endif | |
113 | +#define VPrintf0(zf) \ | |
114 | + fprintf(stderr,zf) | |
115 | +#define VPrintf1(zf,za1) \ | |
116 | + fprintf(stderr,zf,za1) | |
117 | +#define VPrintf2(zf,za1,za2) \ | |
118 | + fprintf(stderr,zf,za1,za2) | |
119 | +#define VPrintf3(zf,za1,za2,za3) \ | |
120 | + fprintf(stderr,zf,za1,za2,za3) | |
121 | +#define VPrintf4(zf,za1,za2,za3,za4) \ | |
122 | + fprintf(stderr,zf,za1,za2,za3,za4) | |
123 | +#define VPrintf5(zf,za1,za2,za3,za4,za5) \ | |
124 | + fprintf(stderr,zf,za1,za2,za3,za4,za5) | |
125 | +#else | |
126 | +extern void bz_internal_error ( int errcode ); | |
127 | +#define AssertH(cond,errcode) \ | |
128 | + { if (!(cond)) bz_internal_error ( errcode ); } | |
129 | +#define AssertD(cond,msg) /* */ | |
130 | +#define VPrintf0(zf) /* */ | |
131 | +#define VPrintf1(zf,za1) /* */ | |
132 | +#define VPrintf2(zf,za1,za2) /* */ | |
133 | +#define VPrintf3(zf,za1,za2,za3) /* */ | |
134 | +#define VPrintf4(zf,za1,za2,za3,za4) /* */ | |
135 | +#define VPrintf5(zf,za1,za2,za3,za4,za5) /* */ | |
136 | +#endif | |
137 | + | |
138 | + | |
139 | +#define BZALLOC(nnn) (strm->bzalloc)(strm->opaque,(nnn),1) | |
140 | +#define BZFREE(ppp) (strm->bzfree)(strm->opaque,(ppp)) | |
141 | + | |
142 | + | |
143 | +/*-- Header bytes. --*/ | |
144 | + | |
145 | +#define BZ_HDR_B 0x42 /* 'B' */ | |
146 | +#define BZ_HDR_Z 0x5a /* 'Z' */ | |
147 | +#define BZ_HDR_h 0x68 /* 'h' */ | |
148 | +#define BZ_HDR_0 0x30 /* '0' */ | |
149 | + | |
150 | +/*-- Constants for the back end. --*/ | |
151 | + | |
152 | +#define BZ_MAX_ALPHA_SIZE 258 | |
153 | +#define BZ_MAX_CODE_LEN 23 | |
154 | + | |
155 | +#define BZ_RUNA 0 | |
156 | +#define BZ_RUNB 1 | |
157 | + | |
158 | +#define BZ_N_GROUPS 6 | |
159 | +#define BZ_G_SIZE 50 | |
160 | +#define BZ_N_ITERS 4 | |
161 | + | |
162 | +#define BZ_MAX_SELECTORS (2 + (900000 / BZ_G_SIZE)) | |
163 | + | |
164 | + | |
165 | +/*-- Stuff for randomising repetitive blocks. --*/ | |
166 | + | |
167 | +extern Int32 BZ2_rNums[512]; | |
168 | + | |
169 | +#define BZ_RAND_DECLS \ | |
170 | + Int32 rNToGo; \ | |
171 | + Int32 rTPos \ | |
172 | + | |
173 | +#define BZ_RAND_INIT_MASK \ | |
174 | + s->rNToGo = 0; \ | |
175 | + s->rTPos = 0 \ | |
176 | + | |
177 | +#define BZ_RAND_MASK ((s->rNToGo == 1) ? 1 : 0) | |
178 | + | |
179 | +#define BZ_RAND_UPD_MASK \ | |
180 | + if (s->rNToGo == 0) { \ | |
181 | + s->rNToGo = BZ2_rNums[s->rTPos]; \ | |
182 | + s->rTPos++; \ | |
183 | + if (s->rTPos == 512) s->rTPos = 0; \ | |
184 | + } \ | |
185 | + s->rNToGo--; | |
186 | + | |
187 | + | |
188 | +/*-- Stuff for doing CRCs. --*/ | |
189 | + | |
190 | +extern UInt32 BZ2_crc32Table[256]; | |
191 | + | |
192 | +#define BZ_INITIALISE_CRC(crcVar) \ | |
193 | +{ \ | |
194 | + crcVar = 0xffffffffL; \ | |
195 | +} | |
196 | + | |
197 | +#define BZ_FINALISE_CRC(crcVar) \ | |
198 | +{ \ | |
199 | + crcVar = ~(crcVar); \ | |
200 | +} | |
201 | + | |
202 | +#define BZ_UPDATE_CRC(crcVar,cha) \ | |
203 | +{ \ | |
204 | + crcVar = (crcVar << 8) ^ \ | |
205 | + BZ2_crc32Table[(crcVar >> 24) ^ \ | |
206 | + ((UChar)cha)]; \ | |
207 | +} | |
208 | + | |
209 | + | |
210 | +/*-- States and modes for compression. --*/ | |
211 | + | |
212 | +#define BZ_M_IDLE 1 | |
213 | +#define BZ_M_RUNNING 2 | |
214 | +#define BZ_M_FLUSHING 3 | |
215 | +#define BZ_M_FINISHING 4 | |
216 | + | |
217 | +#define BZ_S_OUTPUT 1 | |
218 | +#define BZ_S_INPUT 2 | |
219 | + | |
220 | +#define BZ_N_RADIX 2 | |
221 | +#define BZ_N_QSORT 12 | |
222 | +#define BZ_N_SHELL 18 | |
223 | +#define BZ_N_OVERSHOOT (BZ_N_RADIX + BZ_N_QSORT + BZ_N_SHELL + 2) | |
224 | + | |
225 | + | |
226 | +/*-- Structure holding all the compression-side stuff. --*/ | |
227 | + | |
228 | +typedef | |
229 | + struct { | |
230 | + /* pointer back to the struct bz_stream */ | |
231 | + bz_stream* strm; | |
232 | + | |
233 | + /* mode this stream is in, and whether inputting */ | |
234 | + /* or outputting data */ | |
235 | + Int32 mode; | |
236 | + Int32 state; | |
237 | + | |
238 | + /* remembers avail_in when flush/finish requested */ | |
239 | + UInt32 avail_in_expect; | |
240 | + | |
241 | + /* for doing the block sorting */ | |
242 | + UInt32* arr1; | |
243 | + UInt32* arr2; | |
244 | + UInt32* ftab; | |
245 | + Int32 origPtr; | |
246 | + | |
247 | + /* aliases for arr1 and arr2 */ | |
248 | + UInt32* ptr; | |
249 | + UChar* block; | |
250 | + UInt16* mtfv; | |
251 | + UChar* zbits; | |
252 | + | |
253 | + /* for deciding when to use the fallback sorting algorithm */ | |
254 | + Int32 workFactor; | |
255 | + | |
256 | + /* run-length-encoding of the input */ | |
257 | + UInt32 state_in_ch; | |
258 | + Int32 state_in_len; | |
259 | + BZ_RAND_DECLS; | |
260 | + | |
261 | + /* input and output limits and current posns */ | |
262 | + Int32 nblock; | |
263 | + Int32 nblockMAX; | |
264 | + Int32 numZ; | |
265 | + Int32 state_out_pos; | |
266 | + | |
267 | + /* map of bytes used in block */ | |
268 | + Int32 nInUse; | |
269 | + Bool inUse[256]; | |
270 | + UChar unseqToSeq[256]; | |
271 | + | |
272 | + /* the buffer for bit stream creation */ | |
273 | + UInt32 bsBuff; | |
274 | + Int32 bsLive; | |
275 | + | |
276 | + /* block and combined CRCs */ | |
277 | + UInt32 blockCRC; | |
278 | + UInt32 combinedCRC; | |
279 | + | |
280 | + /* misc administratium */ | |
281 | + Int32 verbosity; | |
282 | + Int32 blockNo; | |
283 | + Int32 blockSize100k; | |
284 | + | |
285 | + /* stuff for coding the MTF values */ | |
286 | + Int32 nMTF; | |
287 | + Int32 mtfFreq [BZ_MAX_ALPHA_SIZE]; | |
288 | + UChar selector [BZ_MAX_SELECTORS]; | |
289 | + UChar selectorMtf[BZ_MAX_SELECTORS]; | |
290 | + | |
291 | + UChar len [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE]; | |
292 | + Int32 code [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE]; | |
293 | + Int32 rfreq [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE]; | |
294 | + /* second dimension: only 3 needed; 4 makes index calculations faster */ | |
295 | + UInt32 len_pack[BZ_MAX_ALPHA_SIZE][4]; | |
296 | + | |
297 | + } | |
298 | + EState; | |
299 | + | |
300 | + | |
301 | +/*-- externs for compression. --*/ | |
302 | + | |
303 | +extern void | |
304 | +BZ2_blockSort ( EState* ); | |
305 | + | |
306 | +extern void | |
307 | +BZ2_compressBlock ( EState*, Bool ); | |
308 | + | |
309 | +extern void | |
310 | +BZ2_bsInitWrite ( EState* ); | |
311 | + | |
312 | +extern void | |
313 | +BZ2_hbAssignCodes ( Int32*, UChar*, Int32, Int32, Int32 ); | |
314 | + | |
315 | +extern void | |
316 | +BZ2_hbMakeCodeLengths ( UChar*, Int32*, Int32, Int32 ); | |
317 | + | |
318 | + | |
319 | +/*-- states for decompression. --*/ | |
320 | + | |
321 | +#define BZ_X_IDLE 1 | |
322 | +#define BZ_X_OUTPUT 2 | |
323 | + | |
324 | +#define BZ_X_MAGIC_1 10 | |
325 | +#define BZ_X_MAGIC_2 11 | |
326 | +#define BZ_X_MAGIC_3 12 | |
327 | +#define BZ_X_MAGIC_4 13 | |
328 | +#define BZ_X_BLKHDR_1 14 | |
329 | +#define BZ_X_BLKHDR_2 15 | |
330 | +#define BZ_X_BLKHDR_3 16 | |
331 | +#define BZ_X_BLKHDR_4 17 | |
332 | +#define BZ_X_BLKHDR_5 18 | |
333 | +#define BZ_X_BLKHDR_6 19 | |
334 | +#define BZ_X_BCRC_1 20 | |
335 | +#define BZ_X_BCRC_2 21 | |
336 | +#define BZ_X_BCRC_3 22 | |
337 | +#define BZ_X_BCRC_4 23 | |
338 | +#define BZ_X_RANDBIT 24 | |
339 | +#define BZ_X_ORIGPTR_1 25 | |
340 | +#define BZ_X_ORIGPTR_2 26 | |
341 | +#define BZ_X_ORIGPTR_3 27 | |
342 | +#define BZ_X_MAPPING_1 28 | |
343 | +#define BZ_X_MAPPING_2 29 | |
344 | +#define BZ_X_SELECTOR_1 30 | |
345 | +#define BZ_X_SELECTOR_2 31 | |
346 | +#define BZ_X_SELECTOR_3 32 | |
347 | +#define BZ_X_CODING_1 33 | |
348 | +#define BZ_X_CODING_2 34 | |
349 | +#define BZ_X_CODING_3 35 | |
350 | +#define BZ_X_MTF_1 36 | |
351 | +#define BZ_X_MTF_2 37 | |
352 | +#define BZ_X_MTF_3 38 | |
353 | +#define BZ_X_MTF_4 39 | |
354 | +#define BZ_X_MTF_5 40 | |
355 | +#define BZ_X_MTF_6 41 | |
356 | +#define BZ_X_ENDHDR_2 42 | |
357 | +#define BZ_X_ENDHDR_3 43 | |
358 | +#define BZ_X_ENDHDR_4 44 | |
359 | +#define BZ_X_ENDHDR_5 45 | |
360 | +#define BZ_X_ENDHDR_6 46 | |
361 | +#define BZ_X_CCRC_1 47 | |
362 | +#define BZ_X_CCRC_2 48 | |
363 | +#define BZ_X_CCRC_3 49 | |
364 | +#define BZ_X_CCRC_4 50 | |
365 | + | |
366 | + | |
367 | +/*-- Constants for the fast MTF decoder. --*/ | |
368 | + | |
369 | +#define MTFA_SIZE 4096 | |
370 | +#define MTFL_SIZE 16 | |
371 | + | |
372 | + | |
373 | +/*-- Structure holding all the decompression-side stuff. --*/ | |
374 | + | |
375 | +typedef | |
376 | + struct { | |
377 | + /* pointer back to the struct bz_stream */ | |
378 | + bz_stream* strm; | |
379 | + | |
380 | + /* state indicator for this stream */ | |
381 | + Int32 state; | |
382 | + | |
383 | + /* for doing the final run-length decoding */ | |
384 | + UChar state_out_ch; | |
385 | + Int32 state_out_len; | |
386 | + Bool blockRandomised; | |
387 | + BZ_RAND_DECLS; | |
388 | + | |
389 | + /* the buffer for bit stream reading */ | |
390 | + UInt32 bsBuff; | |
391 | + Int32 bsLive; | |
392 | + | |
393 | + /* misc administratium */ | |
394 | + Int32 blockSize100k; | |
395 | + Bool smallDecompress; | |
396 | + Int32 currBlockNo; | |
397 | + Int32 verbosity; | |
398 | + | |
399 | + /* for undoing the Burrows-Wheeler transform */ | |
400 | + Int32 origPtr; | |
401 | + UInt32 tPos; | |
402 | + Int32 k0; | |
403 | + Int32 unzftab[256]; | |
404 | + Int32 nblock_used; | |
405 | + Int32 cftab[257]; | |
406 | + Int32 cftabCopy[257]; | |
407 | + | |
408 | + /* for undoing the Burrows-Wheeler transform (FAST) */ | |
409 | + UInt32 *tt; | |
410 | + | |
411 | + /* for undoing the Burrows-Wheeler transform (SMALL) */ | |
412 | + UInt16 *ll16; | |
413 | + UChar *ll4; | |
414 | + | |
415 | + /* stored and calculated CRCs */ | |
416 | + UInt32 storedBlockCRC; | |
417 | + UInt32 storedCombinedCRC; | |
418 | + UInt32 calculatedBlockCRC; | |
419 | + UInt32 calculatedCombinedCRC; | |
420 | + | |
421 | + /* map of bytes used in block */ | |
422 | + Int32 nInUse; | |
423 | + Bool inUse[256]; | |
424 | + Bool inUse16[16]; | |
425 | + UChar seqToUnseq[256]; | |
426 | + | |
427 | + /* for decoding the MTF values */ | |
428 | + UChar mtfa [MTFA_SIZE]; | |
429 | + Int32 mtfbase[256 / MTFL_SIZE]; | |
430 | + UChar selector [BZ_MAX_SELECTORS]; | |
431 | + UChar selectorMtf[BZ_MAX_SELECTORS]; | |
432 | + UChar len [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE]; | |
433 | + | |
434 | + Int32 limit [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE]; | |
435 | + Int32 base [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE]; | |
436 | + Int32 perm [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE]; | |
437 | + Int32 minLens[BZ_N_GROUPS]; | |
438 | + | |
439 | + /* save area for scalars in the main decompress code */ | |
440 | + Int32 save_i; | |
441 | + Int32 save_j; | |
442 | + Int32 save_t; | |
443 | + Int32 save_alphaSize; | |
444 | + Int32 save_nGroups; | |
445 | + Int32 save_nSelectors; | |
446 | + Int32 save_EOB; | |
447 | + Int32 save_groupNo; | |
448 | + Int32 save_groupPos; | |
449 | + Int32 save_nextSym; | |
450 | + Int32 save_nblockMAX; | |
451 | + Int32 save_nblock; | |
452 | + Int32 save_es; | |
453 | + Int32 save_N; | |
454 | + Int32 save_curr; | |
455 | + Int32 save_zt; | |
456 | + Int32 save_zn; | |
457 | + Int32 save_zvec; | |
458 | + Int32 save_zj; | |
459 | + Int32 save_gSel; | |
460 | + Int32 save_gMinlen; | |
461 | + Int32* save_gLimit; | |
462 | + Int32* save_gBase; | |
463 | + Int32* save_gPerm; | |
464 | + | |
465 | + } | |
466 | + DState; | |
467 | + | |
468 | + | |
469 | +/*-- Macros for decompression. --*/ | |
470 | + | |
471 | +#define BZ_GET_FAST(cccc) \ | |
472 | + s->tPos = s->tt[s->tPos]; \ | |
473 | + cccc = (UChar)(s->tPos & 0xff); \ | |
474 | + s->tPos >>= 8; | |
475 | + | |
476 | +#define BZ_GET_FAST_C(cccc) \ | |
477 | + c_tPos = c_tt[c_tPos]; \ | |
478 | + cccc = (UChar)(c_tPos & 0xff); \ | |
479 | + c_tPos >>= 8; | |
480 | + | |
481 | +#define SET_LL4(i,n) \ | |
482 | + { if (((i) & 0x1) == 0) \ | |
483 | + s->ll4[(i) >> 1] = (s->ll4[(i) >> 1] & 0xf0) | (n); else \ | |
484 | + s->ll4[(i) >> 1] = (s->ll4[(i) >> 1] & 0x0f) | ((n) << 4); \ | |
485 | + } | |
486 | + | |
487 | +#define GET_LL4(i) \ | |
488 | + ((((UInt32)(s->ll4[(i) >> 1])) >> (((i) << 2) & 0x4)) & 0xF) | |
489 | + | |
490 | +#define SET_LL(i,n) \ | |
491 | + { s->ll16[i] = (UInt16)(n & 0x0000ffff); \ | |
492 | + SET_LL4(i, n >> 16); \ | |
493 | + } | |
494 | + | |
495 | +#define GET_LL(i) \ | |
496 | + (((UInt32)s->ll16[i]) | (GET_LL4(i) << 16)) | |
497 | + | |
498 | +#define BZ_GET_SMALL(cccc) \ | |
499 | + cccc = BZ2_indexIntoF ( s->tPos, s->cftab ); \ | |
500 | + s->tPos = GET_LL(s->tPos); | |
501 | + | |
502 | + | |
503 | +/*-- externs for decompression. --*/ | |
504 | + | |
505 | +extern Int32 | |
506 | +BZ2_indexIntoF ( Int32, Int32* ); | |
507 | + | |
508 | +extern Int32 | |
509 | +BZ2_decompress ( DState* ); | |
510 | + | |
511 | +extern void | |
512 | +BZ2_hbCreateDecodeTables ( Int32*, Int32*, Int32*, UChar*, | |
513 | + Int32, Int32, Int32 ); | |
514 | + | |
515 | + | |
516 | +#endif | |
517 | + | |
518 | + | |
519 | +/*-- BZ_NO_STDIO seems to make NULL disappear on some platforms. --*/ | |
520 | + | |
521 | +#ifdef BZ_NO_STDIO | |
522 | +#ifndef NULL | |
523 | +#define NULL 0 | |
524 | +#endif | |
525 | +#endif | |
526 | + | |
527 | + | |
528 | +/*-------------------------------------------------------------*/ | |
529 | +/*--- end bzlib_private.h ---*/ | |
530 | +/*-------------------------------------------------------------*/ |
lib/bzip2/bzlib_randtable.c
1 | +#include <config.h> | |
2 | + | |
3 | +/*-------------------------------------------------------------*/ | |
4 | +/*--- Table for randomising repetitive blocks ---*/ | |
5 | +/*--- randtable.c ---*/ | |
6 | +/*-------------------------------------------------------------*/ | |
7 | + | |
8 | +/*-- | |
9 | + This file is a part of bzip2 and/or libbzip2, a program and | |
10 | + library for lossless, block-sorting data compression. | |
11 | + | |
12 | + Copyright (C) 1996-2002 Julian R Seward. All rights reserved. | |
13 | + | |
14 | + Redistribution and use in source and binary forms, with or without | |
15 | + modification, are permitted provided that the following conditions | |
16 | + are met: | |
17 | + | |
18 | + 1. Redistributions of source code must retain the above copyright | |
19 | + notice, this list of conditions and the following disclaimer. | |
20 | + | |
21 | + 2. The origin of this software must not be misrepresented; you must | |
22 | + not claim that you wrote the original software. If you use this | |
23 | + software in a product, an acknowledgment in the product | |
24 | + documentation would be appreciated but is not required. | |
25 | + | |
26 | + 3. Altered source versions must be plainly marked as such, and must | |
27 | + not be misrepresented as being the original software. | |
28 | + | |
29 | + 4. The name of the author may not be used to endorse or promote | |
30 | + products derived from this software without specific prior written | |
31 | + permission. | |
32 | + | |
33 | + THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS | |
34 | + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
35 | + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
36 | + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY | |
37 | + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
38 | + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE | |
39 | + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
40 | + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
41 | + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
42 | + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
43 | + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
44 | + | |
45 | + Julian Seward, Cambridge, UK. | |
46 | + jseward@acm.org | |
47 | + bzip2/libbzip2 version 1.0 of 21 March 2000 | |
48 | + | |
49 | + This program is based on (at least) the work of: | |
50 | + Mike Burrows | |
51 | + David Wheeler | |
52 | + Peter Fenwick | |
53 | + Alistair Moffat | |
54 | + Radford Neal | |
55 | + Ian H. Witten | |
56 | + Robert Sedgewick | |
57 | + Jon L. Bentley | |
58 | + | |
59 | + For more information on these sources, see the manual. | |
60 | +--*/ | |
61 | + | |
62 | + | |
63 | +#include "bzlib_private.h" | |
64 | + | |
65 | + | |
66 | +/*---------------------------------------------*/ | |
67 | +Int32 BZ2_rNums[512] = { | |
68 | + 619, 720, 127, 481, 931, 816, 813, 233, 566, 247, | |
69 | + 985, 724, 205, 454, 863, 491, 741, 242, 949, 214, | |
70 | + 733, 859, 335, 708, 621, 574, 73, 654, 730, 472, | |
71 | + 419, 436, 278, 496, 867, 210, 399, 680, 480, 51, | |
72 | + 878, 465, 811, 169, 869, 675, 611, 697, 867, 561, | |
73 | + 862, 687, 507, 283, 482, 129, 807, 591, 733, 623, | |
74 | + 150, 238, 59, 379, 684, 877, 625, 169, 643, 105, | |
75 | + 170, 607, 520, 932, 727, 476, 693, 425, 174, 647, | |
76 | + 73, 122, 335, 530, 442, 853, 695, 249, 445, 515, | |
77 | + 909, 545, 703, 919, 874, 474, 882, 500, 594, 612, | |
78 | + 641, 801, 220, 162, 819, 984, 589, 513, 495, 799, | |
79 | + 161, 604, 958, 533, 221, 400, 386, 867, 600, 782, | |
80 | + 382, 596, 414, 171, 516, 375, 682, 485, 911, 276, | |
81 | + 98, 553, 163, 354, 666, 933, 424, 341, 533, 870, | |
82 | + 227, 730, 475, 186, 263, 647, 537, 686, 600, 224, | |
83 | + 469, 68, 770, 919, 190, 373, 294, 822, 808, 206, | |
84 | + 184, 943, 795, 384, 383, 461, 404, 758, 839, 887, | |
85 | + 715, 67, 618, 276, 204, 918, 873, 777, 604, 560, | |
86 | + 951, 160, 578, 722, 79, 804, 96, 409, 713, 940, | |
87 | + 652, 934, 970, 447, 318, 353, 859, 672, 112, 785, | |
88 | + 645, 863, 803, 350, 139, 93, 354, 99, 820, 908, | |
89 | + 609, 772, 154, 274, 580, 184, 79, 626, 630, 742, | |
90 | + 653, 282, 762, 623, 680, 81, 927, 626, 789, 125, | |
91 | + 411, 521, 938, 300, 821, 78, 343, 175, 128, 250, | |
92 | + 170, 774, 972, 275, 999, 639, 495, 78, 352, 126, | |
93 | + 857, 956, 358, 619, 580, 124, 737, 594, 701, 612, | |
94 | + 669, 112, 134, 694, 363, 992, 809, 743, 168, 974, | |
95 | + 944, 375, 748, 52, 600, 747, 642, 182, 862, 81, | |
96 | + 344, 805, 988, 739, 511, 655, 814, 334, 249, 515, | |
97 | + 897, 955, 664, 981, 649, 113, 974, 459, 893, 228, | |
98 | + 433, 837, 553, 268, 926, 240, 102, 654, 459, 51, | |
99 | + 686, 754, 806, 760, 493, 403, 415, 394, 687, 700, | |
100 | + 946, 670, 656, 610, 738, 392, 760, 799, 887, 653, | |
101 | + 978, 321, 576, 617, 626, 502, 894, 679, 243, 440, | |
102 | + 680, 879, 194, 572, 640, 724, 926, 56, 204, 700, | |
103 | + 707, 151, 457, 449, 797, 195, 791, 558, 945, 679, | |
104 | + 297, 59, 87, 824, 713, 663, 412, 693, 342, 606, | |
105 | + 134, 108, 571, 364, 631, 212, 174, 643, 304, 329, | |
106 | + 343, 97, 430, 751, 497, 314, 983, 374, 822, 928, | |
107 | + 140, 206, 73, 263, 980, 736, 876, 478, 430, 305, | |
108 | + 170, 514, 364, 692, 829, 82, 855, 953, 676, 246, | |
109 | + 369, 970, 294, 750, 807, 827, 150, 790, 288, 923, | |
110 | + 804, 378, 215, 828, 592, 281, 565, 555, 710, 82, | |
111 | + 896, 831, 547, 261, 524, 462, 293, 465, 502, 56, | |
112 | + 661, 821, 976, 991, 658, 869, 905, 758, 745, 193, | |
113 | + 768, 550, 608, 933, 378, 286, 215, 979, 792, 961, | |
114 | + 61, 688, 793, 644, 986, 403, 106, 366, 905, 644, | |
115 | + 372, 567, 466, 434, 645, 210, 389, 550, 919, 135, | |
116 | + 780, 773, 635, 389, 707, 100, 626, 958, 165, 504, | |
117 | + 920, 176, 193, 713, 857, 265, 203, 50, 668, 108, | |
118 | + 645, 990, 626, 197, 510, 357, 358, 850, 858, 364, | |
119 | + 936, 638 | |
120 | +}; | |
121 | + | |
122 | + | |
123 | +/*-------------------------------------------------------------*/ | |
124 | +/*--- end randtable.c ---*/ | |
125 | +/*-------------------------------------------------------------*/ |
lib/bzlib.c
Changes suppressed. Click to show
1 | -#include <config.h> | |
2 | -#include <common.h> | |
3 | -#include <watchdog.h> | |
4 | - | |
5 | -/* | |
6 | - * This file is a modified version of bzlib.c from the bzip2-1.0.2 | |
7 | - * distribution which can be found at http://sources.redhat.com/bzip2/ | |
8 | - */ | |
9 | - | |
10 | -/*-------------------------------------------------------------*/ | |
11 | -/*--- Library top-level functions. ---*/ | |
12 | -/*--- bzlib.c ---*/ | |
13 | -/*-------------------------------------------------------------*/ | |
14 | - | |
15 | -/*-- | |
16 | - This file is a part of bzip2 and/or libbzip2, a program and | |
17 | - library for lossless, block-sorting data compression. | |
18 | - | |
19 | - Copyright (C) 1996-2002 Julian R Seward. All rights reserved. | |
20 | - | |
21 | - Redistribution and use in source and binary forms, with or without | |
22 | - modification, are permitted provided that the following conditions | |
23 | - are met: | |
24 | - | |
25 | - 1. Redistributions of source code must retain the above copyright | |
26 | - notice, this list of conditions and the following disclaimer. | |
27 | - | |
28 | - 2. The origin of this software must not be misrepresented; you must | |
29 | - not claim that you wrote the original software. If you use this | |
30 | - software in a product, an acknowledgment in the product | |
31 | - documentation would be appreciated but is not required. | |
32 | - | |
33 | - 3. Altered source versions must be plainly marked as such, and must | |
34 | - not be misrepresented as being the original software. | |
35 | - | |
36 | - 4. The name of the author may not be used to endorse or promote | |
37 | - products derived from this software without specific prior written | |
38 | - permission. | |
39 | - | |
40 | - THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS | |
41 | - OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
42 | - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
43 | - ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY | |
44 | - DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
45 | - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE | |
46 | - GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
47 | - INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
48 | - WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
49 | - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
50 | - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
51 | - | |
52 | - Julian Seward, Cambridge, UK. | |
53 | - jseward@acm.org | |
54 | - bzip2/libbzip2 version 1.0 of 21 March 2000 | |
55 | - | |
56 | - This program is based on (at least) the work of: | |
57 | - Mike Burrows | |
58 | - David Wheeler | |
59 | - Peter Fenwick | |
60 | - Alistair Moffat | |
61 | - Radford Neal | |
62 | - Ian H. Witten | |
63 | - Robert Sedgewick | |
64 | - Jon L. Bentley | |
65 | - | |
66 | - For more information on these sources, see the manual. | |
67 | ---*/ | |
68 | - | |
69 | -/*-- | |
70 | - CHANGES | |
71 | - ~~~~~~~ | |
72 | - 0.9.0 -- original version. | |
73 | - | |
74 | - 0.9.0a/b -- no changes in this file. | |
75 | - | |
76 | - 0.9.0c | |
77 | - * made zero-length BZ_FLUSH work correctly in bzCompress(). | |
78 | - * fixed bzWrite/bzRead to ignore zero-length requests. | |
79 | - * fixed bzread to correctly handle read requests after EOF. | |
80 | - * wrong parameter order in call to bzDecompressInit in | |
81 | - bzBuffToBuffDecompress. Fixed. | |
82 | ---*/ | |
83 | - | |
84 | -#include "bzlib_private.h" | |
85 | - | |
86 | -/*---------------------------------------------------*/ | |
87 | -/*--- Compression stuff ---*/ | |
88 | -/*---------------------------------------------------*/ | |
89 | - | |
90 | - | |
91 | -/*---------------------------------------------------*/ | |
92 | -#ifndef BZ_NO_STDIO | |
93 | -void BZ2_bz__AssertH__fail ( int errcode ) | |
94 | -{ | |
95 | - fprintf(stderr, | |
96 | - "\n\nbzip2/libbzip2: internal error number %d.\n" | |
97 | - "This is a bug in bzip2/libbzip2, %s.\n" | |
98 | - "Please report it to me at: jseward@acm.org. If this happened\n" | |
99 | - "when you were using some program which uses libbzip2 as a\n" | |
100 | - "component, you should also report this bug to the author(s)\n" | |
101 | - "of that program. Please make an effort to report this bug;\n" | |
102 | - "timely and accurate bug reports eventually lead to higher\n" | |
103 | - "quality software. Thanks. Julian Seward, 30 December 2001.\n\n", | |
104 | - errcode, | |
105 | - BZ2_bzlibVersion() | |
106 | - ); | |
107 | - | |
108 | - if (errcode == 1007) { | |
109 | - fprintf(stderr, | |
110 | - "\n*** A special note about internal error number 1007 ***\n" | |
111 | - "\n" | |
112 | - "Experience suggests that a common cause of i.e. 1007\n" | |
113 | - "is unreliable memory or other hardware. The 1007 assertion\n" | |
114 | - "just happens to cross-check the results of huge numbers of\n" | |
115 | - "memory reads/writes, and so acts (unintendedly) as a stress\n" | |
116 | - "test of your memory system.\n" | |
117 | - "\n" | |
118 | - "I suggest the following: try compressing the file again,\n" | |
119 | - "possibly monitoring progress in detail with the -vv flag.\n" | |
120 | - "\n" | |
121 | - "* If the error cannot be reproduced, and/or happens at different\n" | |
122 | - " points in compression, you may have a flaky memory system.\n" | |
123 | - " Try a memory-test program. I have used Memtest86\n" | |
124 | - " (www.memtest86.com). At the time of writing it is free (GPLd).\n" | |
125 | - " Memtest86 tests memory much more thorougly than your BIOSs\n" | |
126 | - " power-on test, and may find failures that the BIOS doesn't.\n" | |
127 | - "\n" | |
128 | - "* If the error can be repeatably reproduced, this is a bug in\n" | |
129 | - " bzip2, and I would very much like to hear about it. Please\n" | |
130 | - " let me know, and, ideally, save a copy of the file causing the\n" | |
131 | - " problem -- without which I will be unable to investigate it.\n" | |
132 | - "\n" | |
133 | - ); | |
134 | - } | |
135 | - | |
136 | - exit(3); | |
137 | -} | |
138 | -#endif | |
139 | - | |
140 | - | |
141 | -/*---------------------------------------------------*/ | |
142 | -static | |
143 | -int bz_config_ok ( void ) | |
144 | -{ | |
145 | - if (sizeof(int) != 4) return 0; | |
146 | - if (sizeof(short) != 2) return 0; | |
147 | - if (sizeof(char) != 1) return 0; | |
148 | - return 1; | |
149 | -} | |
150 | - | |
151 | - | |
152 | -/*---------------------------------------------------*/ | |
153 | -static | |
154 | -void* default_bzalloc ( void* opaque, Int32 items, Int32 size ) | |
155 | -{ | |
156 | - void* v = malloc ( items * size ); | |
157 | - return v; | |
158 | -} | |
159 | - | |
160 | -static | |
161 | -void default_bzfree ( void* opaque, void* addr ) | |
162 | -{ | |
163 | - if (addr != NULL) free ( addr ); | |
164 | -} | |
165 | - | |
166 | -#ifndef BZ_NO_COMPRESS | |
167 | -/*---------------------------------------------------*/ | |
168 | -static | |
169 | -void prepare_new_block ( EState* s ) | |
170 | -{ | |
171 | - Int32 i; | |
172 | - s->nblock = 0; | |
173 | - s->numZ = 0; | |
174 | - s->state_out_pos = 0; | |
175 | - BZ_INITIALISE_CRC ( s->blockCRC ); | |
176 | - for (i = 0; i < 256; i++) s->inUse[i] = False; | |
177 | - s->blockNo++; | |
178 | -} | |
179 | - | |
180 | - | |
181 | -/*---------------------------------------------------*/ | |
182 | -static | |
183 | -void init_RL ( EState* s ) | |
184 | -{ | |
185 | - s->state_in_ch = 256; | |
186 | - s->state_in_len = 0; | |
187 | -} | |
188 | - | |
189 | - | |
190 | -static | |
191 | -Bool isempty_RL ( EState* s ) | |
192 | -{ | |
193 | - if (s->state_in_ch < 256 && s->state_in_len > 0) | |
194 | - return False; else | |
195 | - return True; | |
196 | -} | |
197 | - | |
198 | -/*---------------------------------------------------*/ | |
199 | -int BZ_API(BZ2_bzCompressInit) | |
200 | - ( bz_stream* strm, | |
201 | - int blockSize100k, | |
202 | - int verbosity, | |
203 | - int workFactor ) | |
204 | -{ | |
205 | - Int32 n; | |
206 | - EState* s; | |
207 | - | |
208 | - if (!bz_config_ok()) return BZ_CONFIG_ERROR; | |
209 | - | |
210 | - if (strm == NULL || | |
211 | - blockSize100k < 1 || blockSize100k > 9 || | |
212 | - workFactor < 0 || workFactor > 250) | |
213 | - return BZ_PARAM_ERROR; | |
214 | - | |
215 | - if (workFactor == 0) workFactor = 30; | |
216 | - if (strm->bzalloc == NULL) strm->bzalloc = default_bzalloc; | |
217 | - if (strm->bzfree == NULL) strm->bzfree = default_bzfree; | |
218 | - | |
219 | - s = BZALLOC( sizeof(EState) ); | |
220 | - if (s == NULL) return BZ_MEM_ERROR; | |
221 | - s->strm = strm; | |
222 | - | |
223 | - s->arr1 = NULL; | |
224 | - s->arr2 = NULL; | |
225 | - s->ftab = NULL; | |
226 | - | |
227 | - n = 100000 * blockSize100k; | |
228 | - s->arr1 = BZALLOC( n * sizeof(UInt32) ); | |
229 | - s->arr2 = BZALLOC( (n+BZ_N_OVERSHOOT) * sizeof(UInt32) ); | |
230 | - s->ftab = BZALLOC( 65537 * sizeof(UInt32) ); | |
231 | - | |
232 | - if (s->arr1 == NULL || s->arr2 == NULL || s->ftab == NULL) { | |
233 | - if (s->arr1 != NULL) BZFREE(s->arr1); | |
234 | - if (s->arr2 != NULL) BZFREE(s->arr2); | |
235 | - if (s->ftab != NULL) BZFREE(s->ftab); | |
236 | - if (s != NULL) BZFREE(s); | |
237 | - return BZ_MEM_ERROR; | |
238 | - } | |
239 | - | |
240 | - s->blockNo = 0; | |
241 | - s->state = BZ_S_INPUT; | |
242 | - s->mode = BZ_M_RUNNING; | |
243 | - s->combinedCRC = 0; | |
244 | - s->blockSize100k = blockSize100k; | |
245 | - s->nblockMAX = 100000 * blockSize100k - 19; | |
246 | - s->verbosity = verbosity; | |
247 | - s->workFactor = workFactor; | |
248 | - | |
249 | - s->block = (UChar*)s->arr2; | |
250 | - s->mtfv = (UInt16*)s->arr1; | |
251 | - s->zbits = NULL; | |
252 | - s->ptr = (UInt32*)s->arr1; | |
253 | - | |
254 | - strm->state = s; | |
255 | - strm->total_in_lo32 = 0; | |
256 | - strm->total_in_hi32 = 0; | |
257 | - strm->total_out_lo32 = 0; | |
258 | - strm->total_out_hi32 = 0; | |
259 | - init_RL ( s ); | |
260 | - prepare_new_block ( s ); | |
261 | - return BZ_OK; | |
262 | -} | |
263 | - | |
264 | - | |
265 | -/*---------------------------------------------------*/ | |
266 | -static | |
267 | -void add_pair_to_block ( EState* s ) | |
268 | -{ | |
269 | - Int32 i; | |
270 | - UChar ch = (UChar)(s->state_in_ch); | |
271 | - for (i = 0; i < s->state_in_len; i++) { | |
272 | - BZ_UPDATE_CRC( s->blockCRC, ch ); | |
273 | - } | |
274 | - s->inUse[s->state_in_ch] = True; | |
275 | - switch (s->state_in_len) { | |
276 | - case 1: | |
277 | - s->block[s->nblock] = (UChar)ch; s->nblock++; | |
278 | - break; | |
279 | - case 2: | |
280 | - s->block[s->nblock] = (UChar)ch; s->nblock++; | |
281 | - s->block[s->nblock] = (UChar)ch; s->nblock++; | |
282 | - break; | |
283 | - case 3: | |
284 | - s->block[s->nblock] = (UChar)ch; s->nblock++; | |
285 | - s->block[s->nblock] = (UChar)ch; s->nblock++; | |
286 | - s->block[s->nblock] = (UChar)ch; s->nblock++; | |
287 | - break; | |
288 | - default: | |
289 | - s->inUse[s->state_in_len-4] = True; | |
290 | - s->block[s->nblock] = (UChar)ch; s->nblock++; | |
291 | - s->block[s->nblock] = (UChar)ch; s->nblock++; | |
292 | - s->block[s->nblock] = (UChar)ch; s->nblock++; | |
293 | - s->block[s->nblock] = (UChar)ch; s->nblock++; | |
294 | - s->block[s->nblock] = ((UChar)(s->state_in_len-4)); | |
295 | - s->nblock++; | |
296 | - break; | |
297 | - } | |
298 | -} | |
299 | - | |
300 | - | |
301 | -/*---------------------------------------------------*/ | |
302 | -static | |
303 | -void flush_RL ( EState* s ) | |
304 | -{ | |
305 | - if (s->state_in_ch < 256) add_pair_to_block ( s ); | |
306 | - init_RL ( s ); | |
307 | -} | |
308 | - | |
309 | - | |
310 | -/*---------------------------------------------------*/ | |
311 | -#define ADD_CHAR_TO_BLOCK(zs,zchh0) \ | |
312 | -{ \ | |
313 | - UInt32 zchh = (UInt32)(zchh0); \ | |
314 | - /*-- fast track the common case --*/ \ | |
315 | - if (zchh != zs->state_in_ch && \ | |
316 | - zs->state_in_len == 1) { \ | |
317 | - UChar ch = (UChar)(zs->state_in_ch); \ | |
318 | - BZ_UPDATE_CRC( zs->blockCRC, ch ); \ | |
319 | - zs->inUse[zs->state_in_ch] = True; \ | |
320 | - zs->block[zs->nblock] = (UChar)ch; \ | |
321 | - zs->nblock++; \ | |
322 | - zs->state_in_ch = zchh; \ | |
323 | - } \ | |
324 | - else \ | |
325 | - /*-- general, uncommon cases --*/ \ | |
326 | - if (zchh != zs->state_in_ch || \ | |
327 | - zs->state_in_len == 255) { \ | |
328 | - if (zs->state_in_ch < 256) \ | |
329 | - add_pair_to_block ( zs ); \ | |
330 | - zs->state_in_ch = zchh; \ | |
331 | - zs->state_in_len = 1; \ | |
332 | - } else { \ | |
333 | - zs->state_in_len++; \ | |
334 | - } \ | |
335 | -} | |
336 | - | |
337 | - | |
338 | -/*---------------------------------------------------*/ | |
339 | -static | |
340 | -Bool copy_input_until_stop ( EState* s ) | |
341 | -{ | |
342 | - Bool progress_in = False; | |
343 | - | |
344 | - if (s->mode == BZ_M_RUNNING) { | |
345 | - | |
346 | - /*-- fast track the common case --*/ | |
347 | - while (True) { | |
348 | - /*-- block full? --*/ | |
349 | - if (s->nblock >= s->nblockMAX) break; | |
350 | - /*-- no input? --*/ | |
351 | - if (s->strm->avail_in == 0) break; | |
352 | - progress_in = True; | |
353 | - ADD_CHAR_TO_BLOCK ( s, (UInt32)(*((UChar*)(s->strm->next_in))) ); | |
354 | - s->strm->next_in++; | |
355 | - s->strm->avail_in--; | |
356 | - s->strm->total_in_lo32++; | |
357 | - if (s->strm->total_in_lo32 == 0) s->strm->total_in_hi32++; | |
358 | - } | |
359 | - | |
360 | - } else { | |
361 | - | |
362 | - /*-- general, uncommon case --*/ | |
363 | - while (True) { | |
364 | - /*-- block full? --*/ | |
365 | - if (s->nblock >= s->nblockMAX) break; | |
366 | - /*-- no input? --*/ | |
367 | - if (s->strm->avail_in == 0) break; | |
368 | - /*-- flush/finish end? --*/ | |
369 | - if (s->avail_in_expect == 0) break; | |
370 | - progress_in = True; | |
371 | - ADD_CHAR_TO_BLOCK ( s, (UInt32)(*((UChar*)(s->strm->next_in))) ); | |
372 | - s->strm->next_in++; | |
373 | - s->strm->avail_in--; | |
374 | - s->strm->total_in_lo32++; | |
375 | - if (s->strm->total_in_lo32 == 0) s->strm->total_in_hi32++; | |
376 | - s->avail_in_expect--; | |
377 | - } | |
378 | - } | |
379 | - return progress_in; | |
380 | -} | |
381 | - | |
382 | - | |
383 | -/*---------------------------------------------------*/ | |
384 | -static | |
385 | -Bool copy_output_until_stop ( EState* s ) | |
386 | -{ | |
387 | - Bool progress_out = False; | |
388 | - | |
389 | - while (True) { | |
390 | - | |
391 | - /*-- no output space? --*/ | |
392 | - if (s->strm->avail_out == 0) break; | |
393 | - | |
394 | - /*-- block done? --*/ | |
395 | - if (s->state_out_pos >= s->numZ) break; | |
396 | - | |
397 | - progress_out = True; | |
398 | - *(s->strm->next_out) = s->zbits[s->state_out_pos]; | |
399 | - s->state_out_pos++; | |
400 | - s->strm->avail_out--; | |
401 | - s->strm->next_out++; | |
402 | - s->strm->total_out_lo32++; | |
403 | - if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++; | |
404 | - } | |
405 | - | |
406 | - return progress_out; | |
407 | -} | |
408 | - | |
409 | - | |
410 | -/*---------------------------------------------------*/ | |
411 | -static | |
412 | -Bool handle_compress ( bz_stream* strm ) | |
413 | -{ | |
414 | - Bool progress_in = False; | |
415 | - Bool progress_out = False; | |
416 | - EState* s = strm->state; | |
417 | - | |
418 | - while (True) { | |
419 | - | |
420 | - if (s->state == BZ_S_OUTPUT) { | |
421 | - progress_out |= copy_output_until_stop ( s ); | |
422 | - if (s->state_out_pos < s->numZ) break; | |
423 | - if (s->mode == BZ_M_FINISHING && | |
424 | - s->avail_in_expect == 0 && | |
425 | - isempty_RL(s)) break; | |
426 | - prepare_new_block ( s ); | |
427 | - s->state = BZ_S_INPUT; | |
428 | - if (s->mode == BZ_M_FLUSHING && | |
429 | - s->avail_in_expect == 0 && | |
430 | - isempty_RL(s)) break; | |
431 | - } | |
432 | - | |
433 | - if (s->state == BZ_S_INPUT) { | |
434 | - progress_in |= copy_input_until_stop ( s ); | |
435 | - if (s->mode != BZ_M_RUNNING && s->avail_in_expect == 0) { | |
436 | - flush_RL ( s ); | |
437 | - BZ2_compressBlock ( s, (Bool)(s->mode == BZ_M_FINISHING) ); | |
438 | - s->state = BZ_S_OUTPUT; | |
439 | - } | |
440 | - else | |
441 | - if (s->nblock >= s->nblockMAX) { | |
442 | - BZ2_compressBlock ( s, False ); | |
443 | - s->state = BZ_S_OUTPUT; | |
444 | - } | |
445 | - else | |
446 | - if (s->strm->avail_in == 0) { | |
447 | - break; | |
448 | - } | |
449 | - } | |
450 | - | |
451 | - } | |
452 | - | |
453 | - return progress_in || progress_out; | |
454 | -} | |
455 | - | |
456 | - | |
457 | -/*---------------------------------------------------*/ | |
458 | -int BZ_API(BZ2_bzCompress) ( bz_stream *strm, int action ) | |
459 | -{ | |
460 | - Bool progress; | |
461 | - EState* s; | |
462 | - if (strm == NULL) return BZ_PARAM_ERROR; | |
463 | - s = strm->state; | |
464 | - if (s == NULL) return BZ_PARAM_ERROR; | |
465 | - if (s->strm != strm) return BZ_PARAM_ERROR; | |
466 | - | |
467 | - preswitch: | |
468 | - switch (s->mode) { | |
469 | - | |
470 | - case BZ_M_IDLE: | |
471 | - return BZ_SEQUENCE_ERROR; | |
472 | - | |
473 | - case BZ_M_RUNNING: | |
474 | - if (action == BZ_RUN) { | |
475 | - progress = handle_compress ( strm ); | |
476 | - return progress ? BZ_RUN_OK : BZ_PARAM_ERROR; | |
477 | - } | |
478 | - else | |
479 | - if (action == BZ_FLUSH) { | |
480 | - s->avail_in_expect = strm->avail_in; | |
481 | - s->mode = BZ_M_FLUSHING; | |
482 | - goto preswitch; | |
483 | - } | |
484 | - else | |
485 | - if (action == BZ_FINISH) { | |
486 | - s->avail_in_expect = strm->avail_in; | |
487 | - s->mode = BZ_M_FINISHING; | |
488 | - goto preswitch; | |
489 | - } | |
490 | - else | |
491 | - return BZ_PARAM_ERROR; | |
492 | - | |
493 | - case BZ_M_FLUSHING: | |
494 | - if (action != BZ_FLUSH) return BZ_SEQUENCE_ERROR; | |
495 | - if (s->avail_in_expect != s->strm->avail_in) | |
496 | - return BZ_SEQUENCE_ERROR; | |
497 | - progress = handle_compress ( strm ); | |
498 | - if (s->avail_in_expect > 0 || !isempty_RL(s) || | |
499 | - s->state_out_pos < s->numZ) return BZ_FLUSH_OK; | |
500 | - s->mode = BZ_M_RUNNING; | |
501 | - return BZ_RUN_OK; | |
502 | - | |
503 | - case BZ_M_FINISHING: | |
504 | - if (action != BZ_FINISH) return BZ_SEQUENCE_ERROR; | |
505 | - if (s->avail_in_expect != s->strm->avail_in) | |
506 | - return BZ_SEQUENCE_ERROR; | |
507 | - progress = handle_compress ( strm ); | |
508 | - if (!progress) return BZ_SEQUENCE_ERROR; | |
509 | - if (s->avail_in_expect > 0 || !isempty_RL(s) || | |
510 | - s->state_out_pos < s->numZ) return BZ_FINISH_OK; | |
511 | - s->mode = BZ_M_IDLE; | |
512 | - return BZ_STREAM_END; | |
513 | - } | |
514 | - return BZ_OK; /*--not reached--*/ | |
515 | -} | |
516 | - | |
517 | - | |
518 | -/*---------------------------------------------------*/ | |
519 | -int BZ_API(BZ2_bzCompressEnd) ( bz_stream *strm ) | |
520 | -{ | |
521 | - EState* s; | |
522 | - if (strm == NULL) return BZ_PARAM_ERROR; | |
523 | - s = strm->state; | |
524 | - if (s == NULL) return BZ_PARAM_ERROR; | |
525 | - if (s->strm != strm) return BZ_PARAM_ERROR; | |
526 | - | |
527 | - if (s->arr1 != NULL) BZFREE(s->arr1); | |
528 | - if (s->arr2 != NULL) BZFREE(s->arr2); | |
529 | - if (s->ftab != NULL) BZFREE(s->ftab); | |
530 | - BZFREE(strm->state); | |
531 | - | |
532 | - strm->state = NULL; | |
533 | - | |
534 | - return BZ_OK; | |
535 | -} | |
536 | -#endif /* BZ_NO_COMPRESS */ | |
537 | - | |
538 | -/*---------------------------------------------------*/ | |
539 | -/*--- Decompression stuff ---*/ | |
540 | -/*---------------------------------------------------*/ | |
541 | - | |
542 | -/*---------------------------------------------------*/ | |
543 | -int BZ_API(BZ2_bzDecompressInit) | |
544 | - ( bz_stream* strm, | |
545 | - int verbosity, | |
546 | - int small ) | |
547 | -{ | |
548 | - DState* s; | |
549 | - | |
550 | - if (!bz_config_ok()) return BZ_CONFIG_ERROR; | |
551 | - | |
552 | - if (strm == NULL) return BZ_PARAM_ERROR; | |
553 | - if (small != 0 && small != 1) return BZ_PARAM_ERROR; | |
554 | - if (verbosity < 0 || verbosity > 4) return BZ_PARAM_ERROR; | |
555 | - | |
556 | - if (strm->bzalloc == NULL) strm->bzalloc = default_bzalloc; | |
557 | - if (strm->bzfree == NULL) strm->bzfree = default_bzfree; | |
558 | - | |
559 | - s = BZALLOC( sizeof(DState) ); | |
560 | - if (s == NULL) return BZ_MEM_ERROR; | |
561 | - s->strm = strm; | |
562 | - strm->state = s; | |
563 | - s->state = BZ_X_MAGIC_1; | |
564 | - s->bsLive = 0; | |
565 | - s->bsBuff = 0; | |
566 | - s->calculatedCombinedCRC = 0; | |
567 | - strm->total_in_lo32 = 0; | |
568 | - strm->total_in_hi32 = 0; | |
569 | - strm->total_out_lo32 = 0; | |
570 | - strm->total_out_hi32 = 0; | |
571 | - s->smallDecompress = (Bool)small; | |
572 | - s->ll4 = NULL; | |
573 | - s->ll16 = NULL; | |
574 | - s->tt = NULL; | |
575 | - s->currBlockNo = 0; | |
576 | - s->verbosity = verbosity; | |
577 | - | |
578 | - return BZ_OK; | |
579 | -} | |
580 | - | |
581 | - | |
582 | -/*---------------------------------------------------*/ | |
583 | -static | |
584 | -void unRLE_obuf_to_output_FAST ( DState* s ) | |
585 | -{ | |
586 | - UChar k1; | |
587 | - | |
588 | - if (s->blockRandomised) { | |
589 | - | |
590 | - while (True) { | |
591 | - /* try to finish existing run */ | |
592 | - while (True) { | |
593 | - if (s->strm->avail_out == 0) return; | |
594 | - if (s->state_out_len == 0) break; | |
595 | - *( (UChar*)(s->strm->next_out) ) = s->state_out_ch; | |
596 | - BZ_UPDATE_CRC ( s->calculatedBlockCRC, s->state_out_ch ); | |
597 | - s->state_out_len--; | |
598 | - s->strm->next_out++; | |
599 | - s->strm->avail_out--; | |
600 | - s->strm->total_out_lo32++; | |
601 | - if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++; | |
602 | - } | |
603 | - | |
604 | - /* can a new run be started? */ | |
605 | - if (s->nblock_used == s->save_nblock+1) return; | |
606 | - | |
607 | - | |
608 | - s->state_out_len = 1; | |
609 | - s->state_out_ch = s->k0; | |
610 | - BZ_GET_FAST(k1); BZ_RAND_UPD_MASK; | |
611 | - k1 ^= BZ_RAND_MASK; s->nblock_used++; | |
612 | - if (s->nblock_used == s->save_nblock+1) continue; | |
613 | - if (k1 != s->k0) { s->k0 = k1; continue; }; | |
614 | - | |
615 | - s->state_out_len = 2; | |
616 | - BZ_GET_FAST(k1); BZ_RAND_UPD_MASK; | |
617 | - k1 ^= BZ_RAND_MASK; s->nblock_used++; | |
618 | - if (s->nblock_used == s->save_nblock+1) continue; | |
619 | - if (k1 != s->k0) { s->k0 = k1; continue; }; | |
620 | - | |
621 | - s->state_out_len = 3; | |
622 | - BZ_GET_FAST(k1); BZ_RAND_UPD_MASK; | |
623 | - k1 ^= BZ_RAND_MASK; s->nblock_used++; | |
624 | - if (s->nblock_used == s->save_nblock+1) continue; | |
625 | - if (k1 != s->k0) { s->k0 = k1; continue; }; | |
626 | - | |
627 | - BZ_GET_FAST(k1); BZ_RAND_UPD_MASK; | |
628 | - k1 ^= BZ_RAND_MASK; s->nblock_used++; | |
629 | - s->state_out_len = ((Int32)k1) + 4; | |
630 | - BZ_GET_FAST(s->k0); BZ_RAND_UPD_MASK; | |
631 | - s->k0 ^= BZ_RAND_MASK; s->nblock_used++; | |
632 | - } | |
633 | - | |
634 | - } else { | |
635 | - | |
636 | - /* restore */ | |
637 | - UInt32 c_calculatedBlockCRC = s->calculatedBlockCRC; | |
638 | - UChar c_state_out_ch = s->state_out_ch; | |
639 | - Int32 c_state_out_len = s->state_out_len; | |
640 | - Int32 c_nblock_used = s->nblock_used; | |
641 | - Int32 c_k0 = s->k0; | |
642 | - UInt32* c_tt = s->tt; | |
643 | - UInt32 c_tPos = s->tPos; | |
644 | - char* cs_next_out = s->strm->next_out; | |
645 | - unsigned int cs_avail_out = s->strm->avail_out; | |
646 | - /* end restore */ | |
647 | - | |
648 | - UInt32 avail_out_INIT = cs_avail_out; | |
649 | - Int32 s_save_nblockPP = s->save_nblock+1; | |
650 | - unsigned int total_out_lo32_old; | |
651 | - | |
652 | - while (True) { | |
653 | - | |
654 | - /* try to finish existing run */ | |
655 | - if (c_state_out_len > 0) { | |
656 | - while (True) { | |
657 | - if (cs_avail_out == 0) goto return_notr; | |
658 | - if (c_state_out_len == 1) break; | |
659 | - *( (UChar*)(cs_next_out) ) = c_state_out_ch; | |
660 | - BZ_UPDATE_CRC ( c_calculatedBlockCRC, c_state_out_ch ); | |
661 | - c_state_out_len--; | |
662 | - cs_next_out++; | |
663 | - cs_avail_out--; | |
664 | - } | |
665 | - s_state_out_len_eq_one: | |
666 | - { | |
667 | - if (cs_avail_out == 0) { | |
668 | - c_state_out_len = 1; goto return_notr; | |
669 | - }; | |
670 | - *( (UChar*)(cs_next_out) ) = c_state_out_ch; | |
671 | - BZ_UPDATE_CRC ( c_calculatedBlockCRC, c_state_out_ch ); | |
672 | - cs_next_out++; | |
673 | - cs_avail_out--; | |
674 | - } | |
675 | - } | |
676 | - /* can a new run be started? */ | |
677 | - if (c_nblock_used == s_save_nblockPP) { | |
678 | - c_state_out_len = 0; goto return_notr; | |
679 | - }; | |
680 | - c_state_out_ch = c_k0; | |
681 | - BZ_GET_FAST_C(k1); c_nblock_used++; | |
682 | - if (k1 != c_k0) { | |
683 | - c_k0 = k1; goto s_state_out_len_eq_one; | |
684 | - }; | |
685 | - if (c_nblock_used == s_save_nblockPP) | |
686 | - goto s_state_out_len_eq_one; | |
687 | - | |
688 | - c_state_out_len = 2; | |
689 | - BZ_GET_FAST_C(k1); c_nblock_used++; | |
690 | - if (c_nblock_used == s_save_nblockPP) continue; | |
691 | - if (k1 != c_k0) { c_k0 = k1; continue; }; | |
692 | - | |
693 | - c_state_out_len = 3; | |
694 | - BZ_GET_FAST_C(k1); c_nblock_used++; | |
695 | - if (c_nblock_used == s_save_nblockPP) continue; | |
696 | - if (k1 != c_k0) { c_k0 = k1; continue; }; | |
697 | - | |
698 | - BZ_GET_FAST_C(k1); c_nblock_used++; | |
699 | - c_state_out_len = ((Int32)k1) + 4; | |
700 | - BZ_GET_FAST_C(c_k0); c_nblock_used++; | |
701 | - } | |
702 | - | |
703 | - return_notr: | |
704 | - total_out_lo32_old = s->strm->total_out_lo32; | |
705 | - s->strm->total_out_lo32 += (avail_out_INIT - cs_avail_out); | |
706 | - if (s->strm->total_out_lo32 < total_out_lo32_old) | |
707 | - s->strm->total_out_hi32++; | |
708 | - | |
709 | - /* save */ | |
710 | - s->calculatedBlockCRC = c_calculatedBlockCRC; | |
711 | - s->state_out_ch = c_state_out_ch; | |
712 | - s->state_out_len = c_state_out_len; | |
713 | - s->nblock_used = c_nblock_used; | |
714 | - s->k0 = c_k0; | |
715 | - s->tt = c_tt; | |
716 | - s->tPos = c_tPos; | |
717 | - s->strm->next_out = cs_next_out; | |
718 | - s->strm->avail_out = cs_avail_out; | |
719 | - /* end save */ | |
720 | - } | |
721 | -} | |
722 | - | |
723 | - | |
724 | -/*---------------------------------------------------*/ | |
725 | -__inline__ Int32 BZ2_indexIntoF ( Int32 indx, Int32 *cftab ) | |
726 | -{ | |
727 | - Int32 nb, na, mid; | |
728 | - nb = 0; | |
729 | - na = 256; | |
730 | - do { | |
731 | - mid = (nb + na) >> 1; | |
732 | - if (indx >= cftab[mid]) nb = mid; else na = mid; | |
733 | - } | |
734 | - while (na - nb != 1); | |
735 | - return nb; | |
736 | -} | |
737 | - | |
738 | - | |
739 | -/*---------------------------------------------------*/ | |
740 | -static | |
741 | -void unRLE_obuf_to_output_SMALL ( DState* s ) | |
742 | -{ | |
743 | - UChar k1; | |
744 | - | |
745 | - if (s->blockRandomised) { | |
746 | - | |
747 | - while (True) { | |
748 | - /* try to finish existing run */ | |
749 | - while (True) { | |
750 | - if (s->strm->avail_out == 0) return; | |
751 | - if (s->state_out_len == 0) break; | |
752 | - *( (UChar*)(s->strm->next_out) ) = s->state_out_ch; | |
753 | - BZ_UPDATE_CRC ( s->calculatedBlockCRC, s->state_out_ch ); | |
754 | - s->state_out_len--; | |
755 | - s->strm->next_out++; | |
756 | - s->strm->avail_out--; | |
757 | - s->strm->total_out_lo32++; | |
758 | - if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++; | |
759 | - } | |
760 | - | |
761 | - /* can a new run be started? */ | |
762 | - if (s->nblock_used == s->save_nblock+1) return; | |
763 | - | |
764 | - | |
765 | - s->state_out_len = 1; | |
766 | - s->state_out_ch = s->k0; | |
767 | - BZ_GET_SMALL(k1); BZ_RAND_UPD_MASK; | |
768 | - k1 ^= BZ_RAND_MASK; s->nblock_used++; | |
769 | - if (s->nblock_used == s->save_nblock+1) continue; | |
770 | - if (k1 != s->k0) { s->k0 = k1; continue; }; | |
771 | - | |
772 | - s->state_out_len = 2; | |
773 | - BZ_GET_SMALL(k1); BZ_RAND_UPD_MASK; | |
774 | - k1 ^= BZ_RAND_MASK; s->nblock_used++; | |
775 | - if (s->nblock_used == s->save_nblock+1) continue; | |
776 | - if (k1 != s->k0) { s->k0 = k1; continue; }; | |
777 | - | |
778 | - s->state_out_len = 3; | |
779 | - BZ_GET_SMALL(k1); BZ_RAND_UPD_MASK; | |
780 | - k1 ^= BZ_RAND_MASK; s->nblock_used++; | |
781 | - if (s->nblock_used == s->save_nblock+1) continue; | |
782 | - if (k1 != s->k0) { s->k0 = k1; continue; }; | |
783 | - | |
784 | - BZ_GET_SMALL(k1); BZ_RAND_UPD_MASK; | |
785 | - k1 ^= BZ_RAND_MASK; s->nblock_used++; | |
786 | - s->state_out_len = ((Int32)k1) + 4; | |
787 | - BZ_GET_SMALL(s->k0); BZ_RAND_UPD_MASK; | |
788 | - s->k0 ^= BZ_RAND_MASK; s->nblock_used++; | |
789 | - } | |
790 | - | |
791 | - } else { | |
792 | - | |
793 | - while (True) { | |
794 | - /* try to finish existing run */ | |
795 | - while (True) { | |
796 | - if (s->strm->avail_out == 0) return; | |
797 | - if (s->state_out_len == 0) break; | |
798 | - *( (UChar*)(s->strm->next_out) ) = s->state_out_ch; | |
799 | - BZ_UPDATE_CRC ( s->calculatedBlockCRC, s->state_out_ch ); | |
800 | - s->state_out_len--; | |
801 | - s->strm->next_out++; | |
802 | - s->strm->avail_out--; | |
803 | - s->strm->total_out_lo32++; | |
804 | - if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++; | |
805 | - } | |
806 | - | |
807 | - /* can a new run be started? */ | |
808 | - if (s->nblock_used == s->save_nblock+1) return; | |
809 | - | |
810 | - s->state_out_len = 1; | |
811 | - s->state_out_ch = s->k0; | |
812 | - BZ_GET_SMALL(k1); s->nblock_used++; | |
813 | - if (s->nblock_used == s->save_nblock+1) continue; | |
814 | - if (k1 != s->k0) { s->k0 = k1; continue; }; | |
815 | - | |
816 | - s->state_out_len = 2; | |
817 | - BZ_GET_SMALL(k1); s->nblock_used++; | |
818 | - if (s->nblock_used == s->save_nblock+1) continue; | |
819 | - if (k1 != s->k0) { s->k0 = k1; continue; }; | |
820 | - | |
821 | - s->state_out_len = 3; | |
822 | - BZ_GET_SMALL(k1); s->nblock_used++; | |
823 | - if (s->nblock_used == s->save_nblock+1) continue; | |
824 | - if (k1 != s->k0) { s->k0 = k1; continue; }; | |
825 | - | |
826 | - BZ_GET_SMALL(k1); s->nblock_used++; | |
827 | - s->state_out_len = ((Int32)k1) + 4; | |
828 | - BZ_GET_SMALL(s->k0); s->nblock_used++; | |
829 | - } | |
830 | - | |
831 | - } | |
832 | -} | |
833 | - | |
834 | - | |
835 | -/*---------------------------------------------------*/ | |
836 | -int BZ_API(BZ2_bzDecompress) ( bz_stream *strm ) | |
837 | -{ | |
838 | - DState* s; | |
839 | - if (strm == NULL) return BZ_PARAM_ERROR; | |
840 | - s = strm->state; | |
841 | - if (s == NULL) return BZ_PARAM_ERROR; | |
842 | - if (s->strm != strm) return BZ_PARAM_ERROR; | |
843 | - | |
844 | - while (True) { | |
845 | -#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG) | |
846 | - WATCHDOG_RESET(); | |
847 | -#endif | |
848 | - if (s->state == BZ_X_IDLE) return BZ_SEQUENCE_ERROR; | |
849 | - if (s->state == BZ_X_OUTPUT) { | |
850 | - if (s->smallDecompress) | |
851 | - unRLE_obuf_to_output_SMALL ( s ); else | |
852 | - unRLE_obuf_to_output_FAST ( s ); | |
853 | - if (s->nblock_used == s->save_nblock+1 && s->state_out_len == 0) { | |
854 | - BZ_FINALISE_CRC ( s->calculatedBlockCRC ); | |
855 | - if (s->verbosity >= 3) | |
856 | - VPrintf2 ( " {0x%x, 0x%x}", s->storedBlockCRC, | |
857 | - s->calculatedBlockCRC ); | |
858 | - if (s->verbosity >= 2) VPrintf0 ( "]" ); | |
859 | - if (s->calculatedBlockCRC != s->storedBlockCRC) | |
860 | - return BZ_DATA_ERROR; | |
861 | - s->calculatedCombinedCRC | |
862 | - = (s->calculatedCombinedCRC << 1) | | |
863 | - (s->calculatedCombinedCRC >> 31); | |
864 | - s->calculatedCombinedCRC ^= s->calculatedBlockCRC; | |
865 | - s->state = BZ_X_BLKHDR_1; | |
866 | - } else { | |
867 | - return BZ_OK; | |
868 | - } | |
869 | - } | |
870 | - if (s->state >= BZ_X_MAGIC_1) { | |
871 | - Int32 r = BZ2_decompress ( s ); | |
872 | - if (r == BZ_STREAM_END) { | |
873 | - if (s->verbosity >= 3) | |
874 | - VPrintf2 ( "\n combined CRCs: stored = 0x%x, computed = 0x%x", | |
875 | - s->storedCombinedCRC, s->calculatedCombinedCRC ); | |
876 | - if (s->calculatedCombinedCRC != s->storedCombinedCRC) | |
877 | - return BZ_DATA_ERROR; | |
878 | - return r; | |
879 | - } | |
880 | - if (s->state != BZ_X_OUTPUT) return r; | |
881 | - } | |
882 | - } | |
883 | - | |
884 | - AssertH ( 0, 6001 ); | |
885 | - | |
886 | - return 0; /*NOTREACHED*/ | |
887 | -} | |
888 | - | |
889 | - | |
890 | -/*---------------------------------------------------*/ | |
891 | -int BZ_API(BZ2_bzDecompressEnd) ( bz_stream *strm ) | |
892 | -{ | |
893 | - DState* s; | |
894 | - if (strm == NULL) return BZ_PARAM_ERROR; | |
895 | - s = strm->state; | |
896 | - if (s == NULL) return BZ_PARAM_ERROR; | |
897 | - if (s->strm != strm) return BZ_PARAM_ERROR; | |
898 | - | |
899 | - if (s->tt != NULL) BZFREE(s->tt); | |
900 | - if (s->ll16 != NULL) BZFREE(s->ll16); | |
901 | - if (s->ll4 != NULL) BZFREE(s->ll4); | |
902 | - | |
903 | - BZFREE(strm->state); | |
904 | - strm->state = NULL; | |
905 | - | |
906 | - return BZ_OK; | |
907 | -} | |
908 | - | |
909 | - | |
910 | -#ifndef BZ_NO_STDIO | |
911 | -/*---------------------------------------------------*/ | |
912 | -/*--- File I/O stuff ---*/ | |
913 | -/*---------------------------------------------------*/ | |
914 | - | |
915 | -#define BZ_SETERR(eee) \ | |
916 | -{ \ | |
917 | - if (bzerror != NULL) *bzerror = eee; \ | |
918 | - if (bzf != NULL) bzf->lastErr = eee; \ | |
919 | -} | |
920 | - | |
921 | -typedef | |
922 | - struct { | |
923 | - FILE* handle; | |
924 | - Char buf[BZ_MAX_UNUSED]; | |
925 | - Int32 bufN; | |
926 | - Bool writing; | |
927 | - bz_stream strm; | |
928 | - Int32 lastErr; | |
929 | - Bool initialisedOk; | |
930 | - } | |
931 | - bzFile; | |
932 | - | |
933 | - | |
934 | -/*---------------------------------------------*/ | |
935 | -static Bool myfeof ( FILE* f ) | |
936 | -{ | |
937 | - Int32 c = fgetc ( f ); | |
938 | - if (c == EOF) return True; | |
939 | - ungetc ( c, f ); | |
940 | - return False; | |
941 | -} | |
942 | - | |
943 | - | |
944 | -/*---------------------------------------------------*/ | |
945 | -BZFILE* BZ_API(BZ2_bzWriteOpen) | |
946 | - ( int* bzerror, | |
947 | - FILE* f, | |
948 | - int blockSize100k, | |
949 | - int verbosity, | |
950 | - int workFactor ) | |
951 | -{ | |
952 | - Int32 ret; | |
953 | - bzFile* bzf = NULL; | |
954 | - | |
955 | - BZ_SETERR(BZ_OK); | |
956 | - | |
957 | - if (f == NULL || | |
958 | - (blockSize100k < 1 || blockSize100k > 9) || | |
959 | - (workFactor < 0 || workFactor > 250) || | |
960 | - (verbosity < 0 || verbosity > 4)) | |
961 | - { BZ_SETERR(BZ_PARAM_ERROR); return NULL; }; | |
962 | - | |
963 | - if (ferror(f)) | |
964 | - { BZ_SETERR(BZ_IO_ERROR); return NULL; }; | |
965 | - | |
966 | - bzf = malloc ( sizeof(bzFile) ); | |
967 | - if (bzf == NULL) | |
968 | - { BZ_SETERR(BZ_MEM_ERROR); return NULL; }; | |
969 | - | |
970 | - BZ_SETERR(BZ_OK); | |
971 | - bzf->initialisedOk = False; | |
972 | - bzf->bufN = 0; | |
973 | - bzf->handle = f; | |
974 | - bzf->writing = True; | |
975 | - bzf->strm.bzalloc = NULL; | |
976 | - bzf->strm.bzfree = NULL; | |
977 | - bzf->strm.opaque = NULL; | |
978 | - | |
979 | - if (workFactor == 0) workFactor = 30; | |
980 | - ret = BZ2_bzCompressInit ( &(bzf->strm), blockSize100k, | |
981 | - verbosity, workFactor ); | |
982 | - if (ret != BZ_OK) | |
983 | - { BZ_SETERR(ret); free(bzf); return NULL; }; | |
984 | - | |
985 | - bzf->strm.avail_in = 0; | |
986 | - bzf->initialisedOk = True; | |
987 | - return bzf; | |
988 | -} | |
989 | - | |
990 | - | |
991 | -/*---------------------------------------------------*/ | |
992 | -void BZ_API(BZ2_bzWrite) | |
993 | - ( int* bzerror, | |
994 | - BZFILE* b, | |
995 | - void* buf, | |
996 | - int len ) | |
997 | -{ | |
998 | - Int32 n, n2, ret; | |
999 | - bzFile* bzf = (bzFile*)b; | |
1000 | - | |
1001 | - BZ_SETERR(BZ_OK); | |
1002 | - if (bzf == NULL || buf == NULL || len < 0) | |
1003 | - { BZ_SETERR(BZ_PARAM_ERROR); return; }; | |
1004 | - if (!(bzf->writing)) | |
1005 | - { BZ_SETERR(BZ_SEQUENCE_ERROR); return; }; | |
1006 | - if (ferror(bzf->handle)) | |
1007 | - { BZ_SETERR(BZ_IO_ERROR); return; }; | |
1008 | - | |
1009 | - if (len == 0) | |
1010 | - { BZ_SETERR(BZ_OK); return; }; | |
1011 | - | |
1012 | - bzf->strm.avail_in = len; | |
1013 | - bzf->strm.next_in = buf; | |
1014 | - | |
1015 | - while (True) { | |
1016 | - bzf->strm.avail_out = BZ_MAX_UNUSED; | |
1017 | - bzf->strm.next_out = bzf->buf; | |
1018 | - ret = BZ2_bzCompress ( &(bzf->strm), BZ_RUN ); | |
1019 | - if (ret != BZ_RUN_OK) | |
1020 | - { BZ_SETERR(ret); return; }; | |
1021 | - | |
1022 | - if (bzf->strm.avail_out < BZ_MAX_UNUSED) { | |
1023 | - n = BZ_MAX_UNUSED - bzf->strm.avail_out; | |
1024 | - n2 = fwrite ( (void*)(bzf->buf), sizeof(UChar), | |
1025 | - n, bzf->handle ); | |
1026 | - if (n != n2 || ferror(bzf->handle)) | |
1027 | - { BZ_SETERR(BZ_IO_ERROR); return; }; | |
1028 | - } | |
1029 | - | |
1030 | - if (bzf->strm.avail_in == 0) | |
1031 | - { BZ_SETERR(BZ_OK); return; }; | |
1032 | - } | |
1033 | -} | |
1034 | - | |
1035 | - | |
1036 | -/*---------------------------------------------------*/ | |
1037 | -void BZ_API(BZ2_bzWriteClose) | |
1038 | - ( int* bzerror, | |
1039 | - BZFILE* b, | |
1040 | - int abandon, | |
1041 | - unsigned int* nbytes_in, | |
1042 | - unsigned int* nbytes_out ) | |
1043 | -{ | |
1044 | - BZ2_bzWriteClose64 ( bzerror, b, abandon, | |
1045 | - nbytes_in, NULL, nbytes_out, NULL ); | |
1046 | -} | |
1047 | - | |
1048 | - | |
1049 | -void BZ_API(BZ2_bzWriteClose64) | |
1050 | - ( int* bzerror, | |
1051 | - BZFILE* b, | |
1052 | - int abandon, | |
1053 | - unsigned int* nbytes_in_lo32, | |
1054 | - unsigned int* nbytes_in_hi32, | |
1055 | - unsigned int* nbytes_out_lo32, | |
1056 | - unsigned int* nbytes_out_hi32 ) | |
1057 | -{ | |
1058 | - Int32 n, n2, ret; | |
1059 | - bzFile* bzf = (bzFile*)b; | |
1060 | - | |
1061 | - if (bzf == NULL) | |
1062 | - { BZ_SETERR(BZ_OK); return; }; | |
1063 | - if (!(bzf->writing)) | |
1064 | - { BZ_SETERR(BZ_SEQUENCE_ERROR); return; }; | |
1065 | - if (ferror(bzf->handle)) | |
1066 | - { BZ_SETERR(BZ_IO_ERROR); return; }; | |
1067 | - | |
1068 | - if (nbytes_in_lo32 != NULL) *nbytes_in_lo32 = 0; | |
1069 | - if (nbytes_in_hi32 != NULL) *nbytes_in_hi32 = 0; | |
1070 | - if (nbytes_out_lo32 != NULL) *nbytes_out_lo32 = 0; | |
1071 | - if (nbytes_out_hi32 != NULL) *nbytes_out_hi32 = 0; | |
1072 | - | |
1073 | - if ((!abandon) && bzf->lastErr == BZ_OK) { | |
1074 | - while (True) { | |
1075 | - bzf->strm.avail_out = BZ_MAX_UNUSED; | |
1076 | - bzf->strm.next_out = bzf->buf; | |
1077 | - ret = BZ2_bzCompress ( &(bzf->strm), BZ_FINISH ); | |
1078 | - if (ret != BZ_FINISH_OK && ret != BZ_STREAM_END) | |
1079 | - { BZ_SETERR(ret); return; }; | |
1080 | - | |
1081 | - if (bzf->strm.avail_out < BZ_MAX_UNUSED) { | |
1082 | - n = BZ_MAX_UNUSED - bzf->strm.avail_out; | |
1083 | - n2 = fwrite ( (void*)(bzf->buf), sizeof(UChar), | |
1084 | - n, bzf->handle ); | |
1085 | - if (n != n2 || ferror(bzf->handle)) | |
1086 | - { BZ_SETERR(BZ_IO_ERROR); return; }; | |
1087 | - } | |
1088 | - | |
1089 | - if (ret == BZ_STREAM_END) break; | |
1090 | - } | |
1091 | - } | |
1092 | - | |
1093 | - if ( !abandon && !ferror ( bzf->handle ) ) { | |
1094 | - fflush ( bzf->handle ); | |
1095 | - if (ferror(bzf->handle)) | |
1096 | - { BZ_SETERR(BZ_IO_ERROR); return; }; | |
1097 | - } | |
1098 | - | |
1099 | - if (nbytes_in_lo32 != NULL) | |
1100 | - *nbytes_in_lo32 = bzf->strm.total_in_lo32; | |
1101 | - if (nbytes_in_hi32 != NULL) | |
1102 | - *nbytes_in_hi32 = bzf->strm.total_in_hi32; | |
1103 | - if (nbytes_out_lo32 != NULL) | |
1104 | - *nbytes_out_lo32 = bzf->strm.total_out_lo32; | |
1105 | - if (nbytes_out_hi32 != NULL) | |
1106 | - *nbytes_out_hi32 = bzf->strm.total_out_hi32; | |
1107 | - | |
1108 | - BZ_SETERR(BZ_OK); | |
1109 | - BZ2_bzCompressEnd ( &(bzf->strm) ); | |
1110 | - free ( bzf ); | |
1111 | -} | |
1112 | - | |
1113 | - | |
1114 | -/*---------------------------------------------------*/ | |
1115 | -BZFILE* BZ_API(BZ2_bzReadOpen) | |
1116 | - ( int* bzerror, | |
1117 | - FILE* f, | |
1118 | - int verbosity, | |
1119 | - int small, | |
1120 | - void* unused, | |
1121 | - int nUnused ) | |
1122 | -{ | |
1123 | - bzFile* bzf = NULL; | |
1124 | - int ret; | |
1125 | - | |
1126 | - BZ_SETERR(BZ_OK); | |
1127 | - | |
1128 | - if (f == NULL || | |
1129 | - (small != 0 && small != 1) || | |
1130 | - (verbosity < 0 || verbosity > 4) || | |
1131 | - (unused == NULL && nUnused != 0) || | |
1132 | - (unused != NULL && (nUnused < 0 || nUnused > BZ_MAX_UNUSED))) | |
1133 | - { BZ_SETERR(BZ_PARAM_ERROR); return NULL; }; | |
1134 | - | |
1135 | - if (ferror(f)) | |
1136 | - { BZ_SETERR(BZ_IO_ERROR); return NULL; }; | |
1137 | - | |
1138 | - bzf = malloc ( sizeof(bzFile) ); | |
1139 | - if (bzf == NULL) | |
1140 | - { BZ_SETERR(BZ_MEM_ERROR); return NULL; }; | |
1141 | - | |
1142 | - BZ_SETERR(BZ_OK); | |
1143 | - | |
1144 | - bzf->initialisedOk = False; | |
1145 | - bzf->handle = f; | |
1146 | - bzf->bufN = 0; | |
1147 | - bzf->writing = False; | |
1148 | - bzf->strm.bzalloc = NULL; | |
1149 | - bzf->strm.bzfree = NULL; | |
1150 | - bzf->strm.opaque = NULL; | |
1151 | - | |
1152 | - while (nUnused > 0) { | |
1153 | - bzf->buf[bzf->bufN] = *((UChar*)(unused)); bzf->bufN++; | |
1154 | - unused = ((void*)( 1 + ((UChar*)(unused)) )); | |
1155 | - nUnused--; | |
1156 | - } | |
1157 | - | |
1158 | - ret = BZ2_bzDecompressInit ( &(bzf->strm), verbosity, small ); | |
1159 | - if (ret != BZ_OK) | |
1160 | - { BZ_SETERR(ret); free(bzf); return NULL; }; | |
1161 | - | |
1162 | - bzf->strm.avail_in = bzf->bufN; | |
1163 | - bzf->strm.next_in = bzf->buf; | |
1164 | - | |
1165 | - bzf->initialisedOk = True; | |
1166 | - return bzf; | |
1167 | -} | |
1168 | - | |
1169 | - | |
1170 | -/*---------------------------------------------------*/ | |
1171 | -void BZ_API(BZ2_bzReadClose) ( int *bzerror, BZFILE *b ) | |
1172 | -{ | |
1173 | - bzFile* bzf = (bzFile*)b; | |
1174 | - | |
1175 | - BZ_SETERR(BZ_OK); | |
1176 | - if (bzf == NULL) | |
1177 | - { BZ_SETERR(BZ_OK); return; }; | |
1178 | - | |
1179 | - if (bzf->writing) | |
1180 | - { BZ_SETERR(BZ_SEQUENCE_ERROR); return; }; | |
1181 | - | |
1182 | - if (bzf->initialisedOk) | |
1183 | - (void)BZ2_bzDecompressEnd ( &(bzf->strm) ); | |
1184 | - free ( bzf ); | |
1185 | -} | |
1186 | - | |
1187 | - | |
1188 | -/*---------------------------------------------------*/ | |
1189 | -int BZ_API(BZ2_bzRead) | |
1190 | - ( int* bzerror, | |
1191 | - BZFILE* b, | |
1192 | - void* buf, | |
1193 | - int len ) | |
1194 | -{ | |
1195 | - Int32 n, ret; | |
1196 | - bzFile* bzf = (bzFile*)b; | |
1197 | - | |
1198 | - BZ_SETERR(BZ_OK); | |
1199 | - | |
1200 | - if (bzf == NULL || buf == NULL || len < 0) | |
1201 | - { BZ_SETERR(BZ_PARAM_ERROR); return 0; }; | |
1202 | - | |
1203 | - if (bzf->writing) | |
1204 | - { BZ_SETERR(BZ_SEQUENCE_ERROR); return 0; }; | |
1205 | - | |
1206 | - if (len == 0) | |
1207 | - { BZ_SETERR(BZ_OK); return 0; }; | |
1208 | - | |
1209 | - bzf->strm.avail_out = len; | |
1210 | - bzf->strm.next_out = buf; | |
1211 | - | |
1212 | - while (True) { | |
1213 | - | |
1214 | - if (ferror(bzf->handle)) | |
1215 | - { BZ_SETERR(BZ_IO_ERROR); return 0; }; | |
1216 | - | |
1217 | - if (bzf->strm.avail_in == 0 && !myfeof(bzf->handle)) { | |
1218 | - n = fread ( bzf->buf, sizeof(UChar), | |
1219 | - BZ_MAX_UNUSED, bzf->handle ); | |
1220 | - if (ferror(bzf->handle)) | |
1221 | - { BZ_SETERR(BZ_IO_ERROR); return 0; }; | |
1222 | - bzf->bufN = n; | |
1223 | - bzf->strm.avail_in = bzf->bufN; | |
1224 | - bzf->strm.next_in = bzf->buf; | |
1225 | - } | |
1226 | - | |
1227 | - ret = BZ2_bzDecompress ( &(bzf->strm) ); | |
1228 | - | |
1229 | - if (ret != BZ_OK && ret != BZ_STREAM_END) | |
1230 | - { BZ_SETERR(ret); return 0; }; | |
1231 | - | |
1232 | - if (ret == BZ_OK && myfeof(bzf->handle) && | |
1233 | - bzf->strm.avail_in == 0 && bzf->strm.avail_out > 0) | |
1234 | - { BZ_SETERR(BZ_UNEXPECTED_EOF); return 0; }; | |
1235 | - | |
1236 | - if (ret == BZ_STREAM_END) | |
1237 | - { BZ_SETERR(BZ_STREAM_END); | |
1238 | - return len - bzf->strm.avail_out; }; | |
1239 | - if (bzf->strm.avail_out == 0) | |
1240 | - { BZ_SETERR(BZ_OK); return len; }; | |
1241 | - | |
1242 | - } | |
1243 | - | |
1244 | - return 0; /*not reached*/ | |
1245 | -} | |
1246 | - | |
1247 | - | |
1248 | -/*---------------------------------------------------*/ | |
1249 | -void BZ_API(BZ2_bzReadGetUnused) | |
1250 | - ( int* bzerror, | |
1251 | - BZFILE* b, | |
1252 | - void** unused, | |
1253 | - int* nUnused ) | |
1254 | -{ | |
1255 | - bzFile* bzf = (bzFile*)b; | |
1256 | - if (bzf == NULL) | |
1257 | - { BZ_SETERR(BZ_PARAM_ERROR); return; }; | |
1258 | - if (bzf->lastErr != BZ_STREAM_END) | |
1259 | - { BZ_SETERR(BZ_SEQUENCE_ERROR); return; }; | |
1260 | - if (unused == NULL || nUnused == NULL) | |
1261 | - { BZ_SETERR(BZ_PARAM_ERROR); return; }; | |
1262 | - | |
1263 | - BZ_SETERR(BZ_OK); | |
1264 | - *nUnused = bzf->strm.avail_in; | |
1265 | - *unused = bzf->strm.next_in; | |
1266 | -} | |
1267 | -#endif | |
1268 | - | |
1269 | - | |
1270 | -/*---------------------------------------------------*/ | |
1271 | -/*--- Misc convenience stuff ---*/ | |
1272 | -/*---------------------------------------------------*/ | |
1273 | -#ifndef BZ_NO_COMPRESS | |
1274 | -/*---------------------------------------------------*/ | |
1275 | -int BZ_API(BZ2_bzBuffToBuffCompress) | |
1276 | - ( char* dest, | |
1277 | - unsigned int* destLen, | |
1278 | - char* source, | |
1279 | - unsigned int sourceLen, | |
1280 | - int blockSize100k, | |
1281 | - int verbosity, | |
1282 | - int workFactor ) | |
1283 | -{ | |
1284 | - bz_stream strm; | |
1285 | - int ret; | |
1286 | - | |
1287 | - if (dest == NULL || destLen == NULL || | |
1288 | - source == NULL || | |
1289 | - blockSize100k < 1 || blockSize100k > 9 || | |
1290 | - verbosity < 0 || verbosity > 4 || | |
1291 | - workFactor < 0 || workFactor > 250) | |
1292 | - return BZ_PARAM_ERROR; | |
1293 | - | |
1294 | - if (workFactor == 0) workFactor = 30; | |
1295 | - strm.bzalloc = NULL; | |
1296 | - strm.bzfree = NULL; | |
1297 | - strm.opaque = NULL; | |
1298 | - ret = BZ2_bzCompressInit ( &strm, blockSize100k, | |
1299 | - verbosity, workFactor ); | |
1300 | - if (ret != BZ_OK) return ret; | |
1301 | - | |
1302 | - strm.next_in = source; | |
1303 | - strm.next_out = dest; | |
1304 | - strm.avail_in = sourceLen; | |
1305 | - strm.avail_out = *destLen; | |
1306 | - | |
1307 | - ret = BZ2_bzCompress ( &strm, BZ_FINISH ); | |
1308 | - if (ret == BZ_FINISH_OK) goto output_overflow; | |
1309 | - if (ret != BZ_STREAM_END) goto errhandler; | |
1310 | - | |
1311 | - /* normal termination */ | |
1312 | - *destLen -= strm.avail_out; | |
1313 | - BZ2_bzCompressEnd ( &strm ); | |
1314 | - return BZ_OK; | |
1315 | - | |
1316 | - output_overflow: | |
1317 | - BZ2_bzCompressEnd ( &strm ); | |
1318 | - return BZ_OUTBUFF_FULL; | |
1319 | - | |
1320 | - errhandler: | |
1321 | - BZ2_bzCompressEnd ( &strm ); | |
1322 | - return ret; | |
1323 | -} | |
1324 | -#endif /* BZ_NO_COMPRESS */ | |
1325 | - | |
1326 | -/*---------------------------------------------------*/ | |
1327 | -int BZ_API(BZ2_bzBuffToBuffDecompress) | |
1328 | - ( char* dest, | |
1329 | - unsigned int* destLen, | |
1330 | - char* source, | |
1331 | - unsigned int sourceLen, | |
1332 | - int small, | |
1333 | - int verbosity ) | |
1334 | -{ | |
1335 | - bz_stream strm; | |
1336 | - int ret; | |
1337 | - | |
1338 | - if (destLen == NULL || source == NULL) | |
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 | ---*/ | |
1392 | -const 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 | |
1408 | -static | |
1409 | -BZFILE * bzopen_or_bzdopen | |
1410 | - ( const char *path, /* no use when bzdopen */ | |
1411 | - int fd, /* no use when bzdopen */ | |
1412 | - const char *mode, | |
1413 | - int open_mode) /* bzopen: 0, bzdopen:1 */ | |
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 | ---*/ | |
1486 | -BZFILE * 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 | -/*---------------------------------------------------*/ | |
1495 | -BZFILE * 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 | -/*---------------------------------------------------*/ | |
1504 | -int 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 | -/*---------------------------------------------------*/ | |
1518 | -int 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 | -/*---------------------------------------------------*/ | |
1532 | -int BZ_API(BZ2_bzflush) (BZFILE *b) | |
1533 | -{ | |
1534 | - /* do nothing now... */ | |
1535 | - return 0; | |
1536 | -} | |
1537 | - | |
1538 | - | |
1539 | -/*---------------------------------------------------*/ | |
1540 | -void 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 | ---*/ | |
1564 | -static 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 | - | |
1584 | -const 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 | -void bz_internal_error(int errcode) | |
1595 | -{ | |
1596 | - printf ("BZIP2 internal error %d\n", errcode); | |
1597 | -} | |
1598 | - | |
1599 | -/*-------------------------------------------------------------*/ | |
1600 | -/*--- end bzlib.c ---*/ | |
1601 | -/*-------------------------------------------------------------*/ |
lib/bzlib_crctable.c
1 | -#include <config.h> | |
2 | - | |
3 | -/*-------------------------------------------------------------*/ | |
4 | -/*--- Table for doing CRCs ---*/ | |
5 | -/*--- crctable.c ---*/ | |
6 | -/*-------------------------------------------------------------*/ | |
7 | - | |
8 | -/*-- | |
9 | - This file is a part of bzip2 and/or libbzip2, a program and | |
10 | - library for lossless, block-sorting data compression. | |
11 | - | |
12 | - Copyright (C) 1996-2002 Julian R Seward. All rights reserved. | |
13 | - | |
14 | - Redistribution and use in source and binary forms, with or without | |
15 | - modification, are permitted provided that the following conditions | |
16 | - are met: | |
17 | - | |
18 | - 1. Redistributions of source code must retain the above copyright | |
19 | - notice, this list of conditions and the following disclaimer. | |
20 | - | |
21 | - 2. The origin of this software must not be misrepresented; you must | |
22 | - not claim that you wrote the original software. If you use this | |
23 | - software in a product, an acknowledgment in the product | |
24 | - documentation would be appreciated but is not required. | |
25 | - | |
26 | - 3. Altered source versions must be plainly marked as such, and must | |
27 | - not be misrepresented as being the original software. | |
28 | - | |
29 | - 4. The name of the author may not be used to endorse or promote | |
30 | - products derived from this software without specific prior written | |
31 | - permission. | |
32 | - | |
33 | - THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS | |
34 | - OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
35 | - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
36 | - ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY | |
37 | - DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
38 | - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE | |
39 | - GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
40 | - INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
41 | - WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
42 | - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
43 | - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
44 | - | |
45 | - Julian Seward, Cambridge, UK. | |
46 | - jseward@acm.org | |
47 | - bzip2/libbzip2 version 1.0 of 21 March 2000 | |
48 | - | |
49 | - This program is based on (at least) the work of: | |
50 | - Mike Burrows | |
51 | - David Wheeler | |
52 | - Peter Fenwick | |
53 | - Alistair Moffat | |
54 | - Radford Neal | |
55 | - Ian H. Witten | |
56 | - Robert Sedgewick | |
57 | - Jon L. Bentley | |
58 | - | |
59 | - For more information on these sources, see the manual. | |
60 | ---*/ | |
61 | - | |
62 | - | |
63 | -#include "bzlib_private.h" | |
64 | - | |
65 | -/*-- | |
66 | - I think this is an implementation of the AUTODIN-II, | |
67 | - Ethernet & FDDI 32-bit CRC standard. Vaguely derived | |
68 | - from code by Rob Warnock, in Section 51 of the | |
69 | - comp.compression FAQ. | |
70 | ---*/ | |
71 | - | |
72 | -UInt32 BZ2_crc32Table[256] = { | |
73 | - | |
74 | - /*-- Ugly, innit? --*/ | |
75 | - | |
76 | - 0x00000000L, 0x04c11db7L, 0x09823b6eL, 0x0d4326d9L, | |
77 | - 0x130476dcL, 0x17c56b6bL, 0x1a864db2L, 0x1e475005L, | |
78 | - 0x2608edb8L, 0x22c9f00fL, 0x2f8ad6d6L, 0x2b4bcb61L, | |
79 | - 0x350c9b64L, 0x31cd86d3L, 0x3c8ea00aL, 0x384fbdbdL, | |
80 | - 0x4c11db70L, 0x48d0c6c7L, 0x4593e01eL, 0x4152fda9L, | |
81 | - 0x5f15adacL, 0x5bd4b01bL, 0x569796c2L, 0x52568b75L, | |
82 | - 0x6a1936c8L, 0x6ed82b7fL, 0x639b0da6L, 0x675a1011L, | |
83 | - 0x791d4014L, 0x7ddc5da3L, 0x709f7b7aL, 0x745e66cdL, | |
84 | - 0x9823b6e0L, 0x9ce2ab57L, 0x91a18d8eL, 0x95609039L, | |
85 | - 0x8b27c03cL, 0x8fe6dd8bL, 0x82a5fb52L, 0x8664e6e5L, | |
86 | - 0xbe2b5b58L, 0xbaea46efL, 0xb7a96036L, 0xb3687d81L, | |
87 | - 0xad2f2d84L, 0xa9ee3033L, 0xa4ad16eaL, 0xa06c0b5dL, | |
88 | - 0xd4326d90L, 0xd0f37027L, 0xddb056feL, 0xd9714b49L, | |
89 | - 0xc7361b4cL, 0xc3f706fbL, 0xceb42022L, 0xca753d95L, | |
90 | - 0xf23a8028L, 0xf6fb9d9fL, 0xfbb8bb46L, 0xff79a6f1L, | |
91 | - 0xe13ef6f4L, 0xe5ffeb43L, 0xe8bccd9aL, 0xec7dd02dL, | |
92 | - 0x34867077L, 0x30476dc0L, 0x3d044b19L, 0x39c556aeL, | |
93 | - 0x278206abL, 0x23431b1cL, 0x2e003dc5L, 0x2ac12072L, | |
94 | - 0x128e9dcfL, 0x164f8078L, 0x1b0ca6a1L, 0x1fcdbb16L, | |
95 | - 0x018aeb13L, 0x054bf6a4L, 0x0808d07dL, 0x0cc9cdcaL, | |
96 | - 0x7897ab07L, 0x7c56b6b0L, 0x71159069L, 0x75d48ddeL, | |
97 | - 0x6b93dddbL, 0x6f52c06cL, 0x6211e6b5L, 0x66d0fb02L, | |
98 | - 0x5e9f46bfL, 0x5a5e5b08L, 0x571d7dd1L, 0x53dc6066L, | |
99 | - 0x4d9b3063L, 0x495a2dd4L, 0x44190b0dL, 0x40d816baL, | |
100 | - 0xaca5c697L, 0xa864db20L, 0xa527fdf9L, 0xa1e6e04eL, | |
101 | - 0xbfa1b04bL, 0xbb60adfcL, 0xb6238b25L, 0xb2e29692L, | |
102 | - 0x8aad2b2fL, 0x8e6c3698L, 0x832f1041L, 0x87ee0df6L, | |
103 | - 0x99a95df3L, 0x9d684044L, 0x902b669dL, 0x94ea7b2aL, | |
104 | - 0xe0b41de7L, 0xe4750050L, 0xe9362689L, 0xedf73b3eL, | |
105 | - 0xf3b06b3bL, 0xf771768cL, 0xfa325055L, 0xfef34de2L, | |
106 | - 0xc6bcf05fL, 0xc27dede8L, 0xcf3ecb31L, 0xcbffd686L, | |
107 | - 0xd5b88683L, 0xd1799b34L, 0xdc3abdedL, 0xd8fba05aL, | |
108 | - 0x690ce0eeL, 0x6dcdfd59L, 0x608edb80L, 0x644fc637L, | |
109 | - 0x7a089632L, 0x7ec98b85L, 0x738aad5cL, 0x774bb0ebL, | |
110 | - 0x4f040d56L, 0x4bc510e1L, 0x46863638L, 0x42472b8fL, | |
111 | - 0x5c007b8aL, 0x58c1663dL, 0x558240e4L, 0x51435d53L, | |
112 | - 0x251d3b9eL, 0x21dc2629L, 0x2c9f00f0L, 0x285e1d47L, | |
113 | - 0x36194d42L, 0x32d850f5L, 0x3f9b762cL, 0x3b5a6b9bL, | |
114 | - 0x0315d626L, 0x07d4cb91L, 0x0a97ed48L, 0x0e56f0ffL, | |
115 | - 0x1011a0faL, 0x14d0bd4dL, 0x19939b94L, 0x1d528623L, | |
116 | - 0xf12f560eL, 0xf5ee4bb9L, 0xf8ad6d60L, 0xfc6c70d7L, | |
117 | - 0xe22b20d2L, 0xe6ea3d65L, 0xeba91bbcL, 0xef68060bL, | |
118 | - 0xd727bbb6L, 0xd3e6a601L, 0xdea580d8L, 0xda649d6fL, | |
119 | - 0xc423cd6aL, 0xc0e2d0ddL, 0xcda1f604L, 0xc960ebb3L, | |
120 | - 0xbd3e8d7eL, 0xb9ff90c9L, 0xb4bcb610L, 0xb07daba7L, | |
121 | - 0xae3afba2L, 0xaafbe615L, 0xa7b8c0ccL, 0xa379dd7bL, | |
122 | - 0x9b3660c6L, 0x9ff77d71L, 0x92b45ba8L, 0x9675461fL, | |
123 | - 0x8832161aL, 0x8cf30badL, 0x81b02d74L, 0x857130c3L, | |
124 | - 0x5d8a9099L, 0x594b8d2eL, 0x5408abf7L, 0x50c9b640L, | |
125 | - 0x4e8ee645L, 0x4a4ffbf2L, 0x470cdd2bL, 0x43cdc09cL, | |
126 | - 0x7b827d21L, 0x7f436096L, 0x7200464fL, 0x76c15bf8L, | |
127 | - 0x68860bfdL, 0x6c47164aL, 0x61043093L, 0x65c52d24L, | |
128 | - 0x119b4be9L, 0x155a565eL, 0x18197087L, 0x1cd86d30L, | |
129 | - 0x029f3d35L, 0x065e2082L, 0x0b1d065bL, 0x0fdc1becL, | |
130 | - 0x3793a651L, 0x3352bbe6L, 0x3e119d3fL, 0x3ad08088L, | |
131 | - 0x2497d08dL, 0x2056cd3aL, 0x2d15ebe3L, 0x29d4f654L, | |
132 | - 0xc5a92679L, 0xc1683bceL, 0xcc2b1d17L, 0xc8ea00a0L, | |
133 | - 0xd6ad50a5L, 0xd26c4d12L, 0xdf2f6bcbL, 0xdbee767cL, | |
134 | - 0xe3a1cbc1L, 0xe760d676L, 0xea23f0afL, 0xeee2ed18L, | |
135 | - 0xf0a5bd1dL, 0xf464a0aaL, 0xf9278673L, 0xfde69bc4L, | |
136 | - 0x89b8fd09L, 0x8d79e0beL, 0x803ac667L, 0x84fbdbd0L, | |
137 | - 0x9abc8bd5L, 0x9e7d9662L, 0x933eb0bbL, 0x97ffad0cL, | |
138 | - 0xafb010b1L, 0xab710d06L, 0xa6322bdfL, 0xa2f33668L, | |
139 | - 0xbcb4666dL, 0xb8757bdaL, 0xb5365d03L, 0xb1f740b4L | |
140 | -}; | |
141 | - | |
142 | - | |
143 | -/*-------------------------------------------------------------*/ | |
144 | -/*--- end crctable.c ---*/ | |
145 | -/*-------------------------------------------------------------*/ |
lib/bzlib_decompress.c
1 | -#include <config.h> | |
2 | -#include <common.h> | |
3 | -#include <watchdog.h> | |
4 | - | |
5 | -/*-------------------------------------------------------------*/ | |
6 | -/*--- Decompression machinery ---*/ | |
7 | -/*--- decompress.c ---*/ | |
8 | -/*-------------------------------------------------------------*/ | |
9 | - | |
10 | -/*-- | |
11 | - This file is a part of bzip2 and/or libbzip2, a program and | |
12 | - library for lossless, block-sorting data compression. | |
13 | - | |
14 | - Copyright (C) 1996-2002 Julian R Seward. All rights reserved. | |
15 | - | |
16 | - Redistribution and use in source and binary forms, with or without | |
17 | - modification, are permitted provided that the following conditions | |
18 | - are met: | |
19 | - | |
20 | - 1. Redistributions of source code must retain the above copyright | |
21 | - notice, this list of conditions and the following disclaimer. | |
22 | - | |
23 | - 2. The origin of this software must not be misrepresented; you must | |
24 | - not claim that you wrote the original software. If you use this | |
25 | - software in a product, an acknowledgment in the product | |
26 | - documentation would be appreciated but is not required. | |
27 | - | |
28 | - 3. Altered source versions must be plainly marked as such, and must | |
29 | - not be misrepresented as being the original software. | |
30 | - | |
31 | - 4. The name of the author may not be used to endorse or promote | |
32 | - products derived from this software without specific prior written | |
33 | - permission. | |
34 | - | |
35 | - THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS | |
36 | - OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
37 | - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
38 | - ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY | |
39 | - DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
40 | - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE | |
41 | - GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
42 | - INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
43 | - WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
44 | - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
45 | - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
46 | - | |
47 | - Julian Seward, Cambridge, UK. | |
48 | - jseward@acm.org | |
49 | - bzip2/libbzip2 version 1.0 of 21 March 2000 | |
50 | - | |
51 | - This program is based on (at least) the work of: | |
52 | - Mike Burrows | |
53 | - David Wheeler | |
54 | - Peter Fenwick | |
55 | - Alistair Moffat | |
56 | - Radford Neal | |
57 | - Ian H. Witten | |
58 | - Robert Sedgewick | |
59 | - Jon L. Bentley | |
60 | - | |
61 | - For more information on these sources, see the manual. | |
62 | ---*/ | |
63 | - | |
64 | - | |
65 | -#include "bzlib_private.h" | |
66 | - | |
67 | - | |
68 | -/*---------------------------------------------------*/ | |
69 | -static | |
70 | -void makeMaps_d ( DState* s ) | |
71 | -{ | |
72 | - Int32 i; | |
73 | - s->nInUse = 0; | |
74 | - for (i = 0; i < 256; i++) | |
75 | - if (s->inUse[i]) { | |
76 | - s->seqToUnseq[s->nInUse] = i; | |
77 | - s->nInUse++; | |
78 | - } | |
79 | -} | |
80 | - | |
81 | - | |
82 | -/*---------------------------------------------------*/ | |
83 | -#define RETURN(rrr) \ | |
84 | - { retVal = rrr; goto save_state_and_return; }; | |
85 | - | |
86 | -#define GET_BITS(lll,vvv,nnn) \ | |
87 | - case lll: s->state = lll; \ | |
88 | - while (True) { \ | |
89 | - if (s->bsLive >= nnn) { \ | |
90 | - UInt32 v; \ | |
91 | - v = (s->bsBuff >> \ | |
92 | - (s->bsLive-nnn)) & ((1 << nnn)-1); \ | |
93 | - s->bsLive -= nnn; \ | |
94 | - vvv = v; \ | |
95 | - break; \ | |
96 | - } \ | |
97 | - if (s->strm->avail_in == 0) RETURN(BZ_OK); \ | |
98 | - s->bsBuff \ | |
99 | - = (s->bsBuff << 8) | \ | |
100 | - ((UInt32) \ | |
101 | - (*((UChar*)(s->strm->next_in)))); \ | |
102 | - s->bsLive += 8; \ | |
103 | - s->strm->next_in++; \ | |
104 | - s->strm->avail_in--; \ | |
105 | - s->strm->total_in_lo32++; \ | |
106 | - if (s->strm->total_in_lo32 == 0) \ | |
107 | - s->strm->total_in_hi32++; \ | |
108 | - } | |
109 | - | |
110 | -#define GET_UCHAR(lll,uuu) \ | |
111 | - GET_BITS(lll,uuu,8) | |
112 | - | |
113 | -#define GET_BIT(lll,uuu) \ | |
114 | - GET_BITS(lll,uuu,1) | |
115 | - | |
116 | -/*---------------------------------------------------*/ | |
117 | -#define GET_MTF_VAL(label1,label2,lval) \ | |
118 | -{ \ | |
119 | - if (groupPos == 0) { \ | |
120 | - groupNo++; \ | |
121 | - if (groupNo >= nSelectors) \ | |
122 | - RETURN(BZ_DATA_ERROR); \ | |
123 | - groupPos = BZ_G_SIZE; \ | |
124 | - gSel = s->selector[groupNo]; \ | |
125 | - gMinlen = s->minLens[gSel]; \ | |
126 | - gLimit = &(s->limit[gSel][0]); \ | |
127 | - gPerm = &(s->perm[gSel][0]); \ | |
128 | - gBase = &(s->base[gSel][0]); \ | |
129 | - } \ | |
130 | - groupPos--; \ | |
131 | - zn = gMinlen; \ | |
132 | - GET_BITS(label1, zvec, zn); \ | |
133 | - while (1) { \ | |
134 | - if (zn > 20 /* the longest code */) \ | |
135 | - RETURN(BZ_DATA_ERROR); \ | |
136 | - if (zvec <= gLimit[zn]) break; \ | |
137 | - zn++; \ | |
138 | - GET_BIT(label2, zj); \ | |
139 | - zvec = (zvec << 1) | zj; \ | |
140 | - }; \ | |
141 | - if (zvec - gBase[zn] < 0 \ | |
142 | - || zvec - gBase[zn] >= BZ_MAX_ALPHA_SIZE) \ | |
143 | - RETURN(BZ_DATA_ERROR); \ | |
144 | - lval = gPerm[zvec - gBase[zn]]; \ | |
145 | -} | |
146 | - | |
147 | - | |
148 | -/*---------------------------------------------------*/ | |
149 | -Int32 BZ2_decompress ( DState* s ) | |
150 | -{ | |
151 | - UChar uc; | |
152 | - Int32 retVal; | |
153 | - Int32 minLen, maxLen; | |
154 | - bz_stream* strm = s->strm; | |
155 | - | |
156 | - /* stuff that needs to be saved/restored */ | |
157 | - Int32 i; | |
158 | - Int32 j; | |
159 | - Int32 t; | |
160 | - Int32 alphaSize; | |
161 | - Int32 nGroups; | |
162 | - Int32 nSelectors; | |
163 | - Int32 EOB; | |
164 | - Int32 groupNo; | |
165 | - Int32 groupPos; | |
166 | - Int32 nextSym; | |
167 | - Int32 nblockMAX; | |
168 | - Int32 nblock; | |
169 | - Int32 es; | |
170 | - Int32 N; | |
171 | - Int32 curr; | |
172 | - Int32 zt; | |
173 | - Int32 zn; | |
174 | - Int32 zvec; | |
175 | - Int32 zj; | |
176 | - Int32 gSel; | |
177 | - Int32 gMinlen; | |
178 | - Int32* gLimit; | |
179 | - Int32* gBase; | |
180 | - Int32* gPerm; | |
181 | - | |
182 | - if (s->state == BZ_X_MAGIC_1) { | |
183 | - /*initialise the save area*/ | |
184 | - s->save_i = 0; | |
185 | - s->save_j = 0; | |
186 | - s->save_t = 0; | |
187 | - s->save_alphaSize = 0; | |
188 | - s->save_nGroups = 0; | |
189 | - s->save_nSelectors = 0; | |
190 | - s->save_EOB = 0; | |
191 | - s->save_groupNo = 0; | |
192 | - s->save_groupPos = 0; | |
193 | - s->save_nextSym = 0; | |
194 | - s->save_nblockMAX = 0; | |
195 | - s->save_nblock = 0; | |
196 | - s->save_es = 0; | |
197 | - s->save_N = 0; | |
198 | - s->save_curr = 0; | |
199 | - s->save_zt = 0; | |
200 | - s->save_zn = 0; | |
201 | - s->save_zvec = 0; | |
202 | - s->save_zj = 0; | |
203 | - s->save_gSel = 0; | |
204 | - s->save_gMinlen = 0; | |
205 | - s->save_gLimit = NULL; | |
206 | - s->save_gBase = NULL; | |
207 | - s->save_gPerm = NULL; | |
208 | - } | |
209 | - | |
210 | - /*restore from the save area*/ | |
211 | - i = s->save_i; | |
212 | - j = s->save_j; | |
213 | - t = s->save_t; | |
214 | - alphaSize = s->save_alphaSize; | |
215 | - nGroups = s->save_nGroups; | |
216 | - nSelectors = s->save_nSelectors; | |
217 | - EOB = s->save_EOB; | |
218 | - groupNo = s->save_groupNo; | |
219 | - groupPos = s->save_groupPos; | |
220 | - nextSym = s->save_nextSym; | |
221 | - nblockMAX = s->save_nblockMAX; | |
222 | - nblock = s->save_nblock; | |
223 | - es = s->save_es; | |
224 | - N = s->save_N; | |
225 | - curr = s->save_curr; | |
226 | - zt = s->save_zt; | |
227 | - zn = s->save_zn; | |
228 | - zvec = s->save_zvec; | |
229 | - zj = s->save_zj; | |
230 | - gSel = s->save_gSel; | |
231 | - gMinlen = s->save_gMinlen; | |
232 | - gLimit = s->save_gLimit; | |
233 | - gBase = s->save_gBase; | |
234 | - gPerm = s->save_gPerm; | |
235 | - | |
236 | - retVal = BZ_OK; | |
237 | - | |
238 | - switch (s->state) { | |
239 | - | |
240 | - GET_UCHAR(BZ_X_MAGIC_1, uc); | |
241 | - if (uc != BZ_HDR_B) RETURN(BZ_DATA_ERROR_MAGIC); | |
242 | - | |
243 | - GET_UCHAR(BZ_X_MAGIC_2, uc); | |
244 | - if (uc != BZ_HDR_Z) RETURN(BZ_DATA_ERROR_MAGIC); | |
245 | - | |
246 | - GET_UCHAR(BZ_X_MAGIC_3, uc) | |
247 | - if (uc != BZ_HDR_h) RETURN(BZ_DATA_ERROR_MAGIC); | |
248 | - | |
249 | - GET_BITS(BZ_X_MAGIC_4, s->blockSize100k, 8) | |
250 | - if (s->blockSize100k < (BZ_HDR_0 + 1) || | |
251 | - s->blockSize100k > (BZ_HDR_0 + 9)) RETURN(BZ_DATA_ERROR_MAGIC); | |
252 | - s->blockSize100k -= BZ_HDR_0; | |
253 | - | |
254 | - if (s->smallDecompress) { | |
255 | - s->ll16 = BZALLOC( s->blockSize100k * 100000 * sizeof(UInt16) ); | |
256 | - s->ll4 = BZALLOC( | |
257 | - ((1 + s->blockSize100k * 100000) >> 1) * sizeof(UChar) | |
258 | - ); | |
259 | - if (s->ll16 == NULL || s->ll4 == NULL) RETURN(BZ_MEM_ERROR); | |
260 | - } else { | |
261 | - s->tt = BZALLOC( s->blockSize100k * 100000 * sizeof(Int32) ); | |
262 | - if (s->tt == NULL) RETURN(BZ_MEM_ERROR); | |
263 | - } | |
264 | - | |
265 | - GET_UCHAR(BZ_X_BLKHDR_1, uc); | |
266 | - | |
267 | - if (uc == 0x17) goto endhdr_2; | |
268 | - if (uc != 0x31) RETURN(BZ_DATA_ERROR); | |
269 | - GET_UCHAR(BZ_X_BLKHDR_2, uc); | |
270 | - if (uc != 0x41) RETURN(BZ_DATA_ERROR); | |
271 | - GET_UCHAR(BZ_X_BLKHDR_3, uc); | |
272 | - if (uc != 0x59) RETURN(BZ_DATA_ERROR); | |
273 | - GET_UCHAR(BZ_X_BLKHDR_4, uc); | |
274 | - if (uc != 0x26) RETURN(BZ_DATA_ERROR); | |
275 | - GET_UCHAR(BZ_X_BLKHDR_5, uc); | |
276 | - if (uc != 0x53) RETURN(BZ_DATA_ERROR); | |
277 | - GET_UCHAR(BZ_X_BLKHDR_6, uc); | |
278 | - if (uc != 0x59) RETURN(BZ_DATA_ERROR); | |
279 | - | |
280 | - s->currBlockNo++; | |
281 | - if (s->verbosity >= 2) | |
282 | - VPrintf1 ( "\n [%d: huff+mtf ", s->currBlockNo ); | |
283 | - | |
284 | - s->storedBlockCRC = 0; | |
285 | - GET_UCHAR(BZ_X_BCRC_1, uc); | |
286 | - s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc); | |
287 | - GET_UCHAR(BZ_X_BCRC_2, uc); | |
288 | - s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc); | |
289 | - GET_UCHAR(BZ_X_BCRC_3, uc); | |
290 | - s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc); | |
291 | - GET_UCHAR(BZ_X_BCRC_4, uc); | |
292 | - s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc); | |
293 | - | |
294 | - GET_BITS(BZ_X_RANDBIT, s->blockRandomised, 1); | |
295 | - | |
296 | - s->origPtr = 0; | |
297 | - GET_UCHAR(BZ_X_ORIGPTR_1, uc); | |
298 | - s->origPtr = (s->origPtr << 8) | ((Int32)uc); | |
299 | - GET_UCHAR(BZ_X_ORIGPTR_2, uc); | |
300 | - s->origPtr = (s->origPtr << 8) | ((Int32)uc); | |
301 | - GET_UCHAR(BZ_X_ORIGPTR_3, uc); | |
302 | - s->origPtr = (s->origPtr << 8) | ((Int32)uc); | |
303 | - | |
304 | - if (s->origPtr < 0) | |
305 | - RETURN(BZ_DATA_ERROR); | |
306 | - if (s->origPtr > 10 + 100000*s->blockSize100k) | |
307 | - RETURN(BZ_DATA_ERROR); | |
308 | - | |
309 | - /*--- Receive the mapping table ---*/ | |
310 | - for (i = 0; i < 16; i++) { | |
311 | - GET_BIT(BZ_X_MAPPING_1, uc); | |
312 | - if (uc == 1) | |
313 | - s->inUse16[i] = True; else | |
314 | - s->inUse16[i] = False; | |
315 | - } | |
316 | - | |
317 | - for (i = 0; i < 256; i++) s->inUse[i] = False; | |
318 | - | |
319 | - for (i = 0; i < 16; i++) | |
320 | - if (s->inUse16[i]) | |
321 | - for (j = 0; j < 16; j++) { | |
322 | - GET_BIT(BZ_X_MAPPING_2, uc); | |
323 | - if (uc == 1) s->inUse[i * 16 + j] = True; | |
324 | - } | |
325 | - makeMaps_d ( s ); | |
326 | - if (s->nInUse == 0) RETURN(BZ_DATA_ERROR); | |
327 | - alphaSize = s->nInUse+2; | |
328 | - | |
329 | - /*--- Now the selectors ---*/ | |
330 | - GET_BITS(BZ_X_SELECTOR_1, nGroups, 3); | |
331 | - if (nGroups < 2 || nGroups > 6) RETURN(BZ_DATA_ERROR); | |
332 | - GET_BITS(BZ_X_SELECTOR_2, nSelectors, 15); | |
333 | - if (nSelectors < 1) RETURN(BZ_DATA_ERROR); | |
334 | - for (i = 0; i < nSelectors; i++) { | |
335 | - j = 0; | |
336 | - while (True) { | |
337 | - GET_BIT(BZ_X_SELECTOR_3, uc); | |
338 | - if (uc == 0) break; | |
339 | - j++; | |
340 | - if (j >= nGroups) RETURN(BZ_DATA_ERROR); | |
341 | - } | |
342 | - s->selectorMtf[i] = j; | |
343 | - } | |
344 | - | |
345 | - /*--- Undo the MTF values for the selectors. ---*/ | |
346 | - { | |
347 | - UChar pos[BZ_N_GROUPS], tmp, v; | |
348 | - for (v = 0; v < nGroups; v++) pos[v] = v; | |
349 | - | |
350 | - for (i = 0; i < nSelectors; i++) { | |
351 | - v = s->selectorMtf[i]; | |
352 | - tmp = pos[v]; | |
353 | - while (v > 0) { pos[v] = pos[v-1]; v--; } | |
354 | - pos[0] = tmp; | |
355 | - s->selector[i] = tmp; | |
356 | - } | |
357 | - } | |
358 | - | |
359 | - /*--- Now the coding tables ---*/ | |
360 | - for (t = 0; t < nGroups; t++) { | |
361 | - GET_BITS(BZ_X_CODING_1, curr, 5); | |
362 | - for (i = 0; i < alphaSize; i++) { | |
363 | - while (True) { | |
364 | - if (curr < 1 || curr > 20) RETURN(BZ_DATA_ERROR); | |
365 | - GET_BIT(BZ_X_CODING_2, uc); | |
366 | - if (uc == 0) break; | |
367 | - GET_BIT(BZ_X_CODING_3, uc); | |
368 | - if (uc == 0) curr++; else curr--; | |
369 | - } | |
370 | - s->len[t][i] = curr; | |
371 | - } | |
372 | - } | |
373 | - | |
374 | - /*--- Create the Huffman decoding tables ---*/ | |
375 | - for (t = 0; t < nGroups; t++) { | |
376 | - minLen = 32; | |
377 | - maxLen = 0; | |
378 | - for (i = 0; i < alphaSize; i++) { | |
379 | - if (s->len[t][i] > maxLen) maxLen = s->len[t][i]; | |
380 | - if (s->len[t][i] < minLen) minLen = s->len[t][i]; | |
381 | - } | |
382 | - BZ2_hbCreateDecodeTables ( | |
383 | - &(s->limit[t][0]), | |
384 | - &(s->base[t][0]), | |
385 | - &(s->perm[t][0]), | |
386 | - &(s->len[t][0]), | |
387 | - minLen, maxLen, alphaSize | |
388 | - ); | |
389 | - s->minLens[t] = minLen; | |
390 | - } | |
391 | - | |
392 | - /*--- Now the MTF values ---*/ | |
393 | - | |
394 | - EOB = s->nInUse+1; | |
395 | - nblockMAX = 100000 * s->blockSize100k; | |
396 | - groupNo = -1; | |
397 | - groupPos = 0; | |
398 | - | |
399 | - for (i = 0; i <= 255; i++) s->unzftab[i] = 0; | |
400 | - | |
401 | - /*-- MTF init --*/ | |
402 | - { | |
403 | - Int32 ii, jj, kk; | |
404 | - kk = MTFA_SIZE-1; | |
405 | - for (ii = 256 / MTFL_SIZE - 1; ii >= 0; ii--) { | |
406 | - for (jj = MTFL_SIZE-1; jj >= 0; jj--) { | |
407 | - s->mtfa[kk] = (UChar)(ii * MTFL_SIZE + jj); | |
408 | - kk--; | |
409 | - } | |
410 | - s->mtfbase[ii] = kk + 1; | |
411 | - } | |
412 | - } | |
413 | - /*-- end MTF init --*/ | |
414 | - | |
415 | - nblock = 0; | |
416 | - GET_MTF_VAL(BZ_X_MTF_1, BZ_X_MTF_2, nextSym); | |
417 | - | |
418 | - while (True) { | |
419 | - | |
420 | -#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG) | |
421 | - WATCHDOG_RESET(); | |
422 | -#endif | |
423 | - if (nextSym == EOB) break; | |
424 | - | |
425 | - if (nextSym == BZ_RUNA || nextSym == BZ_RUNB) { | |
426 | - | |
427 | - es = -1; | |
428 | - N = 1; | |
429 | - do { | |
430 | - if (nextSym == BZ_RUNA) es = es + (0+1) * N; else | |
431 | - if (nextSym == BZ_RUNB) es = es + (1+1) * N; | |
432 | - N = N * 2; | |
433 | - GET_MTF_VAL(BZ_X_MTF_3, BZ_X_MTF_4, nextSym); | |
434 | - } | |
435 | - while (nextSym == BZ_RUNA || nextSym == BZ_RUNB); | |
436 | - | |
437 | - es++; | |
438 | - uc = s->seqToUnseq[ s->mtfa[s->mtfbase[0]] ]; | |
439 | - s->unzftab[uc] += es; | |
440 | - | |
441 | - if (s->smallDecompress) | |
442 | - while (es > 0) { | |
443 | - if (nblock >= nblockMAX) RETURN(BZ_DATA_ERROR); | |
444 | - s->ll16[nblock] = (UInt16)uc; | |
445 | - nblock++; | |
446 | - es--; | |
447 | - } | |
448 | - else | |
449 | - while (es > 0) { | |
450 | - if (nblock >= nblockMAX) RETURN(BZ_DATA_ERROR); | |
451 | - s->tt[nblock] = (UInt32)uc; | |
452 | - nblock++; | |
453 | - es--; | |
454 | - }; | |
455 | - | |
456 | - continue; | |
457 | - | |
458 | - } else { | |
459 | - | |
460 | - if (nblock >= nblockMAX) RETURN(BZ_DATA_ERROR); | |
461 | - | |
462 | - /*-- uc = MTF ( nextSym-1 ) --*/ | |
463 | - { | |
464 | - Int32 ii, jj, kk, pp, lno, off; | |
465 | - UInt32 nn; | |
466 | - nn = (UInt32)(nextSym - 1); | |
467 | - | |
468 | - if (nn < MTFL_SIZE) { | |
469 | - /* avoid general-case expense */ | |
470 | - pp = s->mtfbase[0]; | |
471 | - uc = s->mtfa[pp+nn]; | |
472 | - while (nn > 3) { | |
473 | - Int32 z = pp+nn; | |
474 | - s->mtfa[(z) ] = s->mtfa[(z)-1]; | |
475 | - s->mtfa[(z)-1] = s->mtfa[(z)-2]; | |
476 | - s->mtfa[(z)-2] = s->mtfa[(z)-3]; | |
477 | - s->mtfa[(z)-3] = s->mtfa[(z)-4]; | |
478 | - nn -= 4; | |
479 | - } | |
480 | - while (nn > 0) { | |
481 | - s->mtfa[(pp+nn)] = s->mtfa[(pp+nn)-1]; nn--; | |
482 | - }; | |
483 | - s->mtfa[pp] = uc; | |
484 | - } else { | |
485 | - /* general case */ | |
486 | - lno = nn / MTFL_SIZE; | |
487 | - off = nn % MTFL_SIZE; | |
488 | - pp = s->mtfbase[lno] + off; | |
489 | - uc = s->mtfa[pp]; | |
490 | - while (pp > s->mtfbase[lno]) { | |
491 | - s->mtfa[pp] = s->mtfa[pp-1]; pp--; | |
492 | - }; | |
493 | - s->mtfbase[lno]++; | |
494 | - while (lno > 0) { | |
495 | - s->mtfbase[lno]--; | |
496 | - s->mtfa[s->mtfbase[lno]] | |
497 | - = s->mtfa[s->mtfbase[lno-1] + MTFL_SIZE - 1]; | |
498 | - lno--; | |
499 | - } | |
500 | - s->mtfbase[0]--; | |
501 | - s->mtfa[s->mtfbase[0]] = uc; | |
502 | - if (s->mtfbase[0] == 0) { | |
503 | - kk = MTFA_SIZE-1; | |
504 | - for (ii = 256 / MTFL_SIZE-1; ii >= 0; ii--) { | |
505 | -#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG) | |
506 | - WATCHDOG_RESET(); | |
507 | -#endif | |
508 | - for (jj = MTFL_SIZE-1; jj >= 0; jj--) { | |
509 | - s->mtfa[kk] = s->mtfa[s->mtfbase[ii] + jj]; | |
510 | - kk--; | |
511 | - } | |
512 | - s->mtfbase[ii] = kk + 1; | |
513 | - } | |
514 | - } | |
515 | - } | |
516 | - } | |
517 | - /*-- end uc = MTF ( nextSym-1 ) --*/ | |
518 | - | |
519 | - s->unzftab[s->seqToUnseq[uc]]++; | |
520 | - if (s->smallDecompress) | |
521 | - s->ll16[nblock] = (UInt16)(s->seqToUnseq[uc]); else | |
522 | - s->tt[nblock] = (UInt32)(s->seqToUnseq[uc]); | |
523 | - nblock++; | |
524 | - | |
525 | - GET_MTF_VAL(BZ_X_MTF_5, BZ_X_MTF_6, nextSym); | |
526 | - continue; | |
527 | - } | |
528 | - } | |
529 | - | |
530 | - /* Now we know what nblock is, we can do a better sanity | |
531 | - check on s->origPtr. | |
532 | - */ | |
533 | - if (s->origPtr < 0 || s->origPtr >= nblock) | |
534 | - RETURN(BZ_DATA_ERROR); | |
535 | - | |
536 | - s->state_out_len = 0; | |
537 | - s->state_out_ch = 0; | |
538 | - BZ_INITIALISE_CRC ( s->calculatedBlockCRC ); | |
539 | - s->state = BZ_X_OUTPUT; | |
540 | - if (s->verbosity >= 2) VPrintf0 ( "rt+rld" ); | |
541 | - | |
542 | - /*-- Set up cftab to facilitate generation of T^(-1) --*/ | |
543 | - s->cftab[0] = 0; | |
544 | - for (i = 1; i <= 256; i++) s->cftab[i] = s->unzftab[i-1]; | |
545 | - for (i = 1; i <= 256; i++) s->cftab[i] += s->cftab[i-1]; | |
546 | - | |
547 | - if (s->smallDecompress) { | |
548 | - | |
549 | - /*-- Make a copy of cftab, used in generation of T --*/ | |
550 | - for (i = 0; i <= 256; i++) s->cftabCopy[i] = s->cftab[i]; | |
551 | - | |
552 | - /*-- compute the T vector --*/ | |
553 | - for (i = 0; i < nblock; i++) { | |
554 | - uc = (UChar)(s->ll16[i]); | |
555 | - SET_LL(i, s->cftabCopy[uc]); | |
556 | - s->cftabCopy[uc]++; | |
557 | - } | |
558 | - | |
559 | - /*-- Compute T^(-1) by pointer reversal on T --*/ | |
560 | - i = s->origPtr; | |
561 | - j = GET_LL(i); | |
562 | - do { | |
563 | - Int32 tmp = GET_LL(j); | |
564 | - SET_LL(j, i); | |
565 | - i = j; | |
566 | - j = tmp; | |
567 | - } | |
568 | - while (i != s->origPtr); | |
569 | - | |
570 | -#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG) | |
571 | - WATCHDOG_RESET(); | |
572 | -#endif | |
573 | - s->tPos = s->origPtr; | |
574 | - s->nblock_used = 0; | |
575 | - if (s->blockRandomised) { | |
576 | - BZ_RAND_INIT_MASK; | |
577 | - BZ_GET_SMALL(s->k0); s->nblock_used++; | |
578 | - BZ_RAND_UPD_MASK; s->k0 ^= BZ_RAND_MASK; | |
579 | - } else { | |
580 | - BZ_GET_SMALL(s->k0); s->nblock_used++; | |
581 | - } | |
582 | - | |
583 | - } else { | |
584 | - | |
585 | -#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG) | |
586 | - WATCHDOG_RESET(); | |
587 | -#endif | |
588 | - /*-- compute the T^(-1) vector --*/ | |
589 | - for (i = 0; i < nblock; i++) { | |
590 | - uc = (UChar)(s->tt[i] & 0xff); | |
591 | - s->tt[s->cftab[uc]] |= (i << 8); | |
592 | - s->cftab[uc]++; | |
593 | - } | |
594 | - | |
595 | - s->tPos = s->tt[s->origPtr] >> 8; | |
596 | - s->nblock_used = 0; | |
597 | - if (s->blockRandomised) { | |
598 | - BZ_RAND_INIT_MASK; | |
599 | - BZ_GET_FAST(s->k0); s->nblock_used++; | |
600 | - BZ_RAND_UPD_MASK; s->k0 ^= BZ_RAND_MASK; | |
601 | - } else { | |
602 | - BZ_GET_FAST(s->k0); s->nblock_used++; | |
603 | - } | |
604 | - | |
605 | - } | |
606 | - | |
607 | - RETURN(BZ_OK); | |
608 | - | |
609 | - | |
610 | - endhdr_2: | |
611 | - | |
612 | - GET_UCHAR(BZ_X_ENDHDR_2, uc); | |
613 | - if (uc != 0x72) RETURN(BZ_DATA_ERROR); | |
614 | - GET_UCHAR(BZ_X_ENDHDR_3, uc); | |
615 | - if (uc != 0x45) RETURN(BZ_DATA_ERROR); | |
616 | - GET_UCHAR(BZ_X_ENDHDR_4, uc); | |
617 | - if (uc != 0x38) RETURN(BZ_DATA_ERROR); | |
618 | - GET_UCHAR(BZ_X_ENDHDR_5, uc); | |
619 | - if (uc != 0x50) RETURN(BZ_DATA_ERROR); | |
620 | - GET_UCHAR(BZ_X_ENDHDR_6, uc); | |
621 | - if (uc != 0x90) RETURN(BZ_DATA_ERROR); | |
622 | - | |
623 | - s->storedCombinedCRC = 0; | |
624 | - GET_UCHAR(BZ_X_CCRC_1, uc); | |
625 | - s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc); | |
626 | - GET_UCHAR(BZ_X_CCRC_2, uc); | |
627 | - s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc); | |
628 | - GET_UCHAR(BZ_X_CCRC_3, uc); | |
629 | - s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc); | |
630 | - GET_UCHAR(BZ_X_CCRC_4, uc); | |
631 | - s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc); | |
632 | - | |
633 | - s->state = BZ_X_IDLE; | |
634 | - RETURN(BZ_STREAM_END); | |
635 | - | |
636 | - default: AssertH ( False, 4001 ); | |
637 | - } | |
638 | - | |
639 | - AssertH ( False, 4002 ); | |
640 | - | |
641 | - save_state_and_return: | |
642 | - | |
643 | - s->save_i = i; | |
644 | - s->save_j = j; | |
645 | - s->save_t = t; | |
646 | - s->save_alphaSize = alphaSize; | |
647 | - s->save_nGroups = nGroups; | |
648 | - s->save_nSelectors = nSelectors; | |
649 | - s->save_EOB = EOB; | |
650 | - s->save_groupNo = groupNo; | |
651 | - s->save_groupPos = groupPos; | |
652 | - s->save_nextSym = nextSym; | |
653 | - s->save_nblockMAX = nblockMAX; | |
654 | - s->save_nblock = nblock; | |
655 | - s->save_es = es; | |
656 | - s->save_N = N; | |
657 | - s->save_curr = curr; | |
658 | - s->save_zt = zt; | |
659 | - s->save_zn = zn; | |
660 | - s->save_zvec = zvec; | |
661 | - s->save_zj = zj; | |
662 | - s->save_gSel = gSel; | |
663 | - s->save_gMinlen = gMinlen; | |
664 | - s->save_gLimit = gLimit; | |
665 | - s->save_gBase = gBase; | |
666 | - s->save_gPerm = gPerm; | |
667 | - | |
668 | - return retVal; | |
669 | -} | |
670 | - | |
671 | - | |
672 | -/*-------------------------------------------------------------*/ | |
673 | -/*--- end decompress.c ---*/ | |
674 | -/*-------------------------------------------------------------*/ |
lib/bzlib_huffman.c
1 | -#include <config.h> | |
2 | - | |
3 | -/*-------------------------------------------------------------*/ | |
4 | -/*--- Huffman coding low-level stuff ---*/ | |
5 | -/*--- huffman.c ---*/ | |
6 | -/*-------------------------------------------------------------*/ | |
7 | - | |
8 | -/*-- | |
9 | - This file is a part of bzip2 and/or libbzip2, a program and | |
10 | - library for lossless, block-sorting data compression. | |
11 | - | |
12 | - Copyright (C) 1996-2002 Julian R Seward. All rights reserved. | |
13 | - | |
14 | - Redistribution and use in source and binary forms, with or without | |
15 | - modification, are permitted provided that the following conditions | |
16 | - are met: | |
17 | - | |
18 | - 1. Redistributions of source code must retain the above copyright | |
19 | - notice, this list of conditions and the following disclaimer. | |
20 | - | |
21 | - 2. The origin of this software must not be misrepresented; you must | |
22 | - not claim that you wrote the original software. If you use this | |
23 | - software in a product, an acknowledgment in the product | |
24 | - documentation would be appreciated but is not required. | |
25 | - | |
26 | - 3. Altered source versions must be plainly marked as such, and must | |
27 | - not be misrepresented as being the original software. | |
28 | - | |
29 | - 4. The name of the author may not be used to endorse or promote | |
30 | - products derived from this software without specific prior written | |
31 | - permission. | |
32 | - | |
33 | - THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS | |
34 | - OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
35 | - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
36 | - ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY | |
37 | - DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
38 | - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE | |
39 | - GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
40 | - INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
41 | - WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
42 | - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
43 | - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
44 | - | |
45 | - Julian Seward, Cambridge, UK. | |
46 | - jseward@acm.org | |
47 | - bzip2/libbzip2 version 1.0 of 21 March 2000 | |
48 | - | |
49 | - This program is based on (at least) the work of: | |
50 | - Mike Burrows | |
51 | - David Wheeler | |
52 | - Peter Fenwick | |
53 | - Alistair Moffat | |
54 | - Radford Neal | |
55 | - Ian H. Witten | |
56 | - Robert Sedgewick | |
57 | - Jon L. Bentley | |
58 | - | |
59 | - For more information on these sources, see the manual. | |
60 | ---*/ | |
61 | - | |
62 | - | |
63 | -#include "bzlib_private.h" | |
64 | - | |
65 | -/*---------------------------------------------------*/ | |
66 | -#define WEIGHTOF(zz0) ((zz0) & 0xffffff00) | |
67 | -#define DEPTHOF(zz1) ((zz1) & 0x000000ff) | |
68 | -#define MYMAX(zz2,zz3) ((zz2) > (zz3) ? (zz2) : (zz3)) | |
69 | - | |
70 | -#define ADDWEIGHTS(zw1,zw2) \ | |
71 | - (WEIGHTOF(zw1)+WEIGHTOF(zw2)) | \ | |
72 | - (1 + MYMAX(DEPTHOF(zw1),DEPTHOF(zw2))) | |
73 | - | |
74 | -#define UPHEAP(z) \ | |
75 | -{ \ | |
76 | - Int32 zz, tmp; \ | |
77 | - zz = z; tmp = heap[zz]; \ | |
78 | - while (weight[tmp] < weight[heap[zz >> 1]]) { \ | |
79 | - heap[zz] = heap[zz >> 1]; \ | |
80 | - zz >>= 1; \ | |
81 | - } \ | |
82 | - heap[zz] = tmp; \ | |
83 | -} | |
84 | - | |
85 | -#define DOWNHEAP(z) \ | |
86 | -{ \ | |
87 | - Int32 zz, yy, tmp; \ | |
88 | - zz = z; tmp = heap[zz]; \ | |
89 | - while (True) { \ | |
90 | - yy = zz << 1; \ | |
91 | - if (yy > nHeap) break; \ | |
92 | - if (yy < nHeap && \ | |
93 | - weight[heap[yy+1]] < weight[heap[yy]]) \ | |
94 | - yy++; \ | |
95 | - if (weight[tmp] < weight[heap[yy]]) break; \ | |
96 | - heap[zz] = heap[yy]; \ | |
97 | - zz = yy; \ | |
98 | - } \ | |
99 | - heap[zz] = tmp; \ | |
100 | -} | |
101 | - | |
102 | - | |
103 | -/*---------------------------------------------------*/ | |
104 | -void BZ2_hbMakeCodeLengths ( UChar *len, | |
105 | - Int32 *freq, | |
106 | - Int32 alphaSize, | |
107 | - Int32 maxLen ) | |
108 | -{ | |
109 | - /*-- | |
110 | - Nodes and heap entries run from 1. Entry 0 | |
111 | - for both the heap and nodes is a sentinel. | |
112 | - --*/ | |
113 | - Int32 nNodes, nHeap, n1, n2, i, j, k; | |
114 | - Bool tooLong; | |
115 | - | |
116 | - Int32 heap [ BZ_MAX_ALPHA_SIZE + 2 ]; | |
117 | - Int32 weight [ BZ_MAX_ALPHA_SIZE * 2 ]; | |
118 | - Int32 parent [ BZ_MAX_ALPHA_SIZE * 2 ]; | |
119 | - | |
120 | - for (i = 0; i < alphaSize; i++) | |
121 | - weight[i+1] = (freq[i] == 0 ? 1 : freq[i]) << 8; | |
122 | - | |
123 | - while (True) { | |
124 | - | |
125 | - nNodes = alphaSize; | |
126 | - nHeap = 0; | |
127 | - | |
128 | - heap[0] = 0; | |
129 | - weight[0] = 0; | |
130 | - parent[0] = -2; | |
131 | - | |
132 | - for (i = 1; i <= alphaSize; i++) { | |
133 | - parent[i] = -1; | |
134 | - nHeap++; | |
135 | - heap[nHeap] = i; | |
136 | - UPHEAP(nHeap); | |
137 | - } | |
138 | - | |
139 | - AssertH( nHeap < (BZ_MAX_ALPHA_SIZE+2), 2001 ); | |
140 | - | |
141 | - while (nHeap > 1) { | |
142 | - n1 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1); | |
143 | - n2 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1); | |
144 | - nNodes++; | |
145 | - parent[n1] = parent[n2] = nNodes; | |
146 | - weight[nNodes] = ADDWEIGHTS(weight[n1], weight[n2]); | |
147 | - parent[nNodes] = -1; | |
148 | - nHeap++; | |
149 | - heap[nHeap] = nNodes; | |
150 | - UPHEAP(nHeap); | |
151 | - } | |
152 | - | |
153 | - AssertH( nNodes < (BZ_MAX_ALPHA_SIZE * 2), 2002 ); | |
154 | - | |
155 | - tooLong = False; | |
156 | - for (i = 1; i <= alphaSize; i++) { | |
157 | - j = 0; | |
158 | - k = i; | |
159 | - while (parent[k] >= 0) { k = parent[k]; j++; } | |
160 | - len[i-1] = j; | |
161 | - if (j > maxLen) tooLong = True; | |
162 | - } | |
163 | - | |
164 | - if (! tooLong) break; | |
165 | - | |
166 | - for (i = 1; i < alphaSize; i++) { | |
167 | - j = weight[i] >> 8; | |
168 | - j = 1 + (j / 2); | |
169 | - weight[i] = j << 8; | |
170 | - } | |
171 | - } | |
172 | -} | |
173 | - | |
174 | - | |
175 | -/*---------------------------------------------------*/ | |
176 | -void BZ2_hbAssignCodes ( Int32 *code, | |
177 | - UChar *length, | |
178 | - Int32 minLen, | |
179 | - Int32 maxLen, | |
180 | - Int32 alphaSize ) | |
181 | -{ | |
182 | - Int32 n, vec, i; | |
183 | - | |
184 | - vec = 0; | |
185 | - for (n = minLen; n <= maxLen; n++) { | |
186 | - for (i = 0; i < alphaSize; i++) | |
187 | - if (length[i] == n) { code[i] = vec; vec++; }; | |
188 | - vec <<= 1; | |
189 | - } | |
190 | -} | |
191 | - | |
192 | - | |
193 | -/*---------------------------------------------------*/ | |
194 | -void BZ2_hbCreateDecodeTables ( Int32 *limit, | |
195 | - Int32 *base, | |
196 | - Int32 *perm, | |
197 | - UChar *length, | |
198 | - Int32 minLen, | |
199 | - Int32 maxLen, | |
200 | - Int32 alphaSize ) | |
201 | -{ | |
202 | - Int32 pp, i, j, vec; | |
203 | - | |
204 | - pp = 0; | |
205 | - for (i = minLen; i <= maxLen; i++) | |
206 | - for (j = 0; j < alphaSize; j++) | |
207 | - if (length[j] == i) { perm[pp] = j; pp++; }; | |
208 | - | |
209 | - for (i = 0; i < BZ_MAX_CODE_LEN; i++) base[i] = 0; | |
210 | - for (i = 0; i < alphaSize; i++) base[length[i]+1]++; | |
211 | - | |
212 | - for (i = 1; i < BZ_MAX_CODE_LEN; i++) base[i] += base[i-1]; | |
213 | - | |
214 | - for (i = 0; i < BZ_MAX_CODE_LEN; i++) limit[i] = 0; | |
215 | - vec = 0; | |
216 | - | |
217 | - for (i = minLen; i <= maxLen; i++) { | |
218 | - vec += (base[i+1] - base[i]); | |
219 | - limit[i] = vec-1; | |
220 | - vec <<= 1; | |
221 | - } | |
222 | - for (i = minLen + 1; i <= maxLen; i++) | |
223 | - base[i] = ((limit[i-1] + 1) << 1) - base[i]; | |
224 | -} | |
225 | - | |
226 | - | |
227 | -/*-------------------------------------------------------------*/ | |
228 | -/*--- end huffman.c ---*/ | |
229 | -/*-------------------------------------------------------------*/ |
lib/bzlib_private.h
1 | -/* | |
2 | - * This file is a modified version of bzlib_private.h from the bzip2-1.0.2 | |
3 | - * distribution which can be found at http://sources.redhat.com/bzip2/ | |
4 | - */ | |
5 | - | |
6 | -/*-------------------------------------------------------------*/ | |
7 | -/*--- Private header file for the library. ---*/ | |
8 | -/*--- bzlib_private.h ---*/ | |
9 | -/*-------------------------------------------------------------*/ | |
10 | - | |
11 | -/*-- | |
12 | - This file is a part of bzip2 and/or libbzip2, a program and | |
13 | - library for lossless, block-sorting data compression. | |
14 | - | |
15 | - Copyright (C) 1996-2002 Julian R Seward. All rights reserved. | |
16 | - | |
17 | - Redistribution and use in source and binary forms, with or without | |
18 | - modification, are permitted provided that the following conditions | |
19 | - are met: | |
20 | - | |
21 | - 1. Redistributions of source code must retain the above copyright | |
22 | - notice, this list of conditions and the following disclaimer. | |
23 | - | |
24 | - 2. The origin of this software must not be misrepresented; you must | |
25 | - not claim that you wrote the original software. If you use this | |
26 | - software in a product, an acknowledgment in the product | |
27 | - documentation would be appreciated but is not required. | |
28 | - | |
29 | - 3. Altered source versions must be plainly marked as such, and must | |
30 | - not be misrepresented as being the original software. | |
31 | - | |
32 | - 4. The name of the author may not be used to endorse or promote | |
33 | - products derived from this software without specific prior written | |
34 | - permission. | |
35 | - | |
36 | - THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS | |
37 | - OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
38 | - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
39 | - ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY | |
40 | - DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
41 | - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE | |
42 | - GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
43 | - INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
44 | - WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
45 | - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
46 | - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
47 | - | |
48 | - Julian Seward, Cambridge, UK. | |
49 | - jseward@acm.org | |
50 | - bzip2/libbzip2 version 1.0 of 21 March 2000 | |
51 | - | |
52 | - This program is based on (at least) the work of: | |
53 | - Mike Burrows | |
54 | - David Wheeler | |
55 | - Peter Fenwick | |
56 | - Alistair Moffat | |
57 | - Radford Neal | |
58 | - Ian H. Witten | |
59 | - Robert Sedgewick | |
60 | - Jon L. Bentley | |
61 | - | |
62 | - For more information on these sources, see the manual. | |
63 | ---*/ | |
64 | - | |
65 | - | |
66 | -#ifndef _BZLIB_PRIVATE_H | |
67 | -#define _BZLIB_PRIVATE_H | |
68 | - | |
69 | -#include <malloc.h> | |
70 | - | |
71 | -#include "bzlib.h" | |
72 | - | |
73 | -#ifndef BZ_NO_STDIO | |
74 | -#include <stdio.h> | |
75 | -#include <ctype.h> | |
76 | -#include <string.h> | |
77 | -#endif | |
78 | - | |
79 | - | |
80 | -/*-- General stuff. --*/ | |
81 | - | |
82 | -#define BZ_VERSION "1.0.2, 30-Dec-2001" | |
83 | - | |
84 | -typedef char Char; | |
85 | -typedef unsigned char Bool; | |
86 | -typedef unsigned char UChar; | |
87 | -typedef int Int32; | |
88 | -typedef unsigned int UInt32; | |
89 | -typedef short Int16; | |
90 | -typedef unsigned short UInt16; | |
91 | - | |
92 | -#define True ((Bool)1) | |
93 | -#define False ((Bool)0) | |
94 | - | |
95 | -#ifndef __GNUC__ | |
96 | -#define __inline__ /* */ | |
97 | -#endif | |
98 | - | |
99 | -#ifndef BZ_NO_STDIO | |
100 | -extern void BZ2_bz__AssertH__fail ( int errcode ); | |
101 | -#define AssertH(cond,errcode) \ | |
102 | - { if (!(cond)) BZ2_bz__AssertH__fail ( errcode ); } | |
103 | -#if BZ_DEBUG | |
104 | -#define AssertD(cond,msg) \ | |
105 | - { if (!(cond)) { \ | |
106 | - fprintf ( stderr, \ | |
107 | - "\n\nlibbzip2(debug build): internal error\n\t%s\n", msg );\ | |
108 | - exit(1); \ | |
109 | - }} | |
110 | -#else | |
111 | -#define AssertD(cond,msg) /* */ | |
112 | -#endif | |
113 | -#define VPrintf0(zf) \ | |
114 | - fprintf(stderr,zf) | |
115 | -#define VPrintf1(zf,za1) \ | |
116 | - fprintf(stderr,zf,za1) | |
117 | -#define VPrintf2(zf,za1,za2) \ | |
118 | - fprintf(stderr,zf,za1,za2) | |
119 | -#define VPrintf3(zf,za1,za2,za3) \ | |
120 | - fprintf(stderr,zf,za1,za2,za3) | |
121 | -#define VPrintf4(zf,za1,za2,za3,za4) \ | |
122 | - fprintf(stderr,zf,za1,za2,za3,za4) | |
123 | -#define VPrintf5(zf,za1,za2,za3,za4,za5) \ | |
124 | - fprintf(stderr,zf,za1,za2,za3,za4,za5) | |
125 | -#else | |
126 | -extern void bz_internal_error ( int errcode ); | |
127 | -#define AssertH(cond,errcode) \ | |
128 | - { if (!(cond)) bz_internal_error ( errcode ); } | |
129 | -#define AssertD(cond,msg) /* */ | |
130 | -#define VPrintf0(zf) /* */ | |
131 | -#define VPrintf1(zf,za1) /* */ | |
132 | -#define VPrintf2(zf,za1,za2) /* */ | |
133 | -#define VPrintf3(zf,za1,za2,za3) /* */ | |
134 | -#define VPrintf4(zf,za1,za2,za3,za4) /* */ | |
135 | -#define VPrintf5(zf,za1,za2,za3,za4,za5) /* */ | |
136 | -#endif | |
137 | - | |
138 | - | |
139 | -#define BZALLOC(nnn) (strm->bzalloc)(strm->opaque,(nnn),1) | |
140 | -#define BZFREE(ppp) (strm->bzfree)(strm->opaque,(ppp)) | |
141 | - | |
142 | - | |
143 | -/*-- Header bytes. --*/ | |
144 | - | |
145 | -#define BZ_HDR_B 0x42 /* 'B' */ | |
146 | -#define BZ_HDR_Z 0x5a /* 'Z' */ | |
147 | -#define BZ_HDR_h 0x68 /* 'h' */ | |
148 | -#define BZ_HDR_0 0x30 /* '0' */ | |
149 | - | |
150 | -/*-- Constants for the back end. --*/ | |
151 | - | |
152 | -#define BZ_MAX_ALPHA_SIZE 258 | |
153 | -#define BZ_MAX_CODE_LEN 23 | |
154 | - | |
155 | -#define BZ_RUNA 0 | |
156 | -#define BZ_RUNB 1 | |
157 | - | |
158 | -#define BZ_N_GROUPS 6 | |
159 | -#define BZ_G_SIZE 50 | |
160 | -#define BZ_N_ITERS 4 | |
161 | - | |
162 | -#define BZ_MAX_SELECTORS (2 + (900000 / BZ_G_SIZE)) | |
163 | - | |
164 | - | |
165 | -/*-- Stuff for randomising repetitive blocks. --*/ | |
166 | - | |
167 | -extern Int32 BZ2_rNums[512]; | |
168 | - | |
169 | -#define BZ_RAND_DECLS \ | |
170 | - Int32 rNToGo; \ | |
171 | - Int32 rTPos \ | |
172 | - | |
173 | -#define BZ_RAND_INIT_MASK \ | |
174 | - s->rNToGo = 0; \ | |
175 | - s->rTPos = 0 \ | |
176 | - | |
177 | -#define BZ_RAND_MASK ((s->rNToGo == 1) ? 1 : 0) | |
178 | - | |
179 | -#define BZ_RAND_UPD_MASK \ | |
180 | - if (s->rNToGo == 0) { \ | |
181 | - s->rNToGo = BZ2_rNums[s->rTPos]; \ | |
182 | - s->rTPos++; \ | |
183 | - if (s->rTPos == 512) s->rTPos = 0; \ | |
184 | - } \ | |
185 | - s->rNToGo--; | |
186 | - | |
187 | - | |
188 | -/*-- Stuff for doing CRCs. --*/ | |
189 | - | |
190 | -extern UInt32 BZ2_crc32Table[256]; | |
191 | - | |
192 | -#define BZ_INITIALISE_CRC(crcVar) \ | |
193 | -{ \ | |
194 | - crcVar = 0xffffffffL; \ | |
195 | -} | |
196 | - | |
197 | -#define BZ_FINALISE_CRC(crcVar) \ | |
198 | -{ \ | |
199 | - crcVar = ~(crcVar); \ | |
200 | -} | |
201 | - | |
202 | -#define BZ_UPDATE_CRC(crcVar,cha) \ | |
203 | -{ \ | |
204 | - crcVar = (crcVar << 8) ^ \ | |
205 | - BZ2_crc32Table[(crcVar >> 24) ^ \ | |
206 | - ((UChar)cha)]; \ | |
207 | -} | |
208 | - | |
209 | - | |
210 | -/*-- States and modes for compression. --*/ | |
211 | - | |
212 | -#define BZ_M_IDLE 1 | |
213 | -#define BZ_M_RUNNING 2 | |
214 | -#define BZ_M_FLUSHING 3 | |
215 | -#define BZ_M_FINISHING 4 | |
216 | - | |
217 | -#define BZ_S_OUTPUT 1 | |
218 | -#define BZ_S_INPUT 2 | |
219 | - | |
220 | -#define BZ_N_RADIX 2 | |
221 | -#define BZ_N_QSORT 12 | |
222 | -#define BZ_N_SHELL 18 | |
223 | -#define BZ_N_OVERSHOOT (BZ_N_RADIX + BZ_N_QSORT + BZ_N_SHELL + 2) | |
224 | - | |
225 | - | |
226 | -/*-- Structure holding all the compression-side stuff. --*/ | |
227 | - | |
228 | -typedef | |
229 | - struct { | |
230 | - /* pointer back to the struct bz_stream */ | |
231 | - bz_stream* strm; | |
232 | - | |
233 | - /* mode this stream is in, and whether inputting */ | |
234 | - /* or outputting data */ | |
235 | - Int32 mode; | |
236 | - Int32 state; | |
237 | - | |
238 | - /* remembers avail_in when flush/finish requested */ | |
239 | - UInt32 avail_in_expect; | |
240 | - | |
241 | - /* for doing the block sorting */ | |
242 | - UInt32* arr1; | |
243 | - UInt32* arr2; | |
244 | - UInt32* ftab; | |
245 | - Int32 origPtr; | |
246 | - | |
247 | - /* aliases for arr1 and arr2 */ | |
248 | - UInt32* ptr; | |
249 | - UChar* block; | |
250 | - UInt16* mtfv; | |
251 | - UChar* zbits; | |
252 | - | |
253 | - /* for deciding when to use the fallback sorting algorithm */ | |
254 | - Int32 workFactor; | |
255 | - | |
256 | - /* run-length-encoding of the input */ | |
257 | - UInt32 state_in_ch; | |
258 | - Int32 state_in_len; | |
259 | - BZ_RAND_DECLS; | |
260 | - | |
261 | - /* input and output limits and current posns */ | |
262 | - Int32 nblock; | |
263 | - Int32 nblockMAX; | |
264 | - Int32 numZ; | |
265 | - Int32 state_out_pos; | |
266 | - | |
267 | - /* map of bytes used in block */ | |
268 | - Int32 nInUse; | |
269 | - Bool inUse[256]; | |
270 | - UChar unseqToSeq[256]; | |
271 | - | |
272 | - /* the buffer for bit stream creation */ | |
273 | - UInt32 bsBuff; | |
274 | - Int32 bsLive; | |
275 | - | |
276 | - /* block and combined CRCs */ | |
277 | - UInt32 blockCRC; | |
278 | - UInt32 combinedCRC; | |
279 | - | |
280 | - /* misc administratium */ | |
281 | - Int32 verbosity; | |
282 | - Int32 blockNo; | |
283 | - Int32 blockSize100k; | |
284 | - | |
285 | - /* stuff for coding the MTF values */ | |
286 | - Int32 nMTF; | |
287 | - Int32 mtfFreq [BZ_MAX_ALPHA_SIZE]; | |
288 | - UChar selector [BZ_MAX_SELECTORS]; | |
289 | - UChar selectorMtf[BZ_MAX_SELECTORS]; | |
290 | - | |
291 | - UChar len [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE]; | |
292 | - Int32 code [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE]; | |
293 | - Int32 rfreq [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE]; | |
294 | - /* second dimension: only 3 needed; 4 makes index calculations faster */ | |
295 | - UInt32 len_pack[BZ_MAX_ALPHA_SIZE][4]; | |
296 | - | |
297 | - } | |
298 | - EState; | |
299 | - | |
300 | - | |
301 | -/*-- externs for compression. --*/ | |
302 | - | |
303 | -extern void | |
304 | -BZ2_blockSort ( EState* ); | |
305 | - | |
306 | -extern void | |
307 | -BZ2_compressBlock ( EState*, Bool ); | |
308 | - | |
309 | -extern void | |
310 | -BZ2_bsInitWrite ( EState* ); | |
311 | - | |
312 | -extern void | |
313 | -BZ2_hbAssignCodes ( Int32*, UChar*, Int32, Int32, Int32 ); | |
314 | - | |
315 | -extern void | |
316 | -BZ2_hbMakeCodeLengths ( UChar*, Int32*, Int32, Int32 ); | |
317 | - | |
318 | - | |
319 | -/*-- states for decompression. --*/ | |
320 | - | |
321 | -#define BZ_X_IDLE 1 | |
322 | -#define BZ_X_OUTPUT 2 | |
323 | - | |
324 | -#define BZ_X_MAGIC_1 10 | |
325 | -#define BZ_X_MAGIC_2 11 | |
326 | -#define BZ_X_MAGIC_3 12 | |
327 | -#define BZ_X_MAGIC_4 13 | |
328 | -#define BZ_X_BLKHDR_1 14 | |
329 | -#define BZ_X_BLKHDR_2 15 | |
330 | -#define BZ_X_BLKHDR_3 16 | |
331 | -#define BZ_X_BLKHDR_4 17 | |
332 | -#define BZ_X_BLKHDR_5 18 | |
333 | -#define BZ_X_BLKHDR_6 19 | |
334 | -#define BZ_X_BCRC_1 20 | |
335 | -#define BZ_X_BCRC_2 21 | |
336 | -#define BZ_X_BCRC_3 22 | |
337 | -#define BZ_X_BCRC_4 23 | |
338 | -#define BZ_X_RANDBIT 24 | |
339 | -#define BZ_X_ORIGPTR_1 25 | |
340 | -#define BZ_X_ORIGPTR_2 26 | |
341 | -#define BZ_X_ORIGPTR_3 27 | |
342 | -#define BZ_X_MAPPING_1 28 | |
343 | -#define BZ_X_MAPPING_2 29 | |
344 | -#define BZ_X_SELECTOR_1 30 | |
345 | -#define BZ_X_SELECTOR_2 31 | |
346 | -#define BZ_X_SELECTOR_3 32 | |
347 | -#define BZ_X_CODING_1 33 | |
348 | -#define BZ_X_CODING_2 34 | |
349 | -#define BZ_X_CODING_3 35 | |
350 | -#define BZ_X_MTF_1 36 | |
351 | -#define BZ_X_MTF_2 37 | |
352 | -#define BZ_X_MTF_3 38 | |
353 | -#define BZ_X_MTF_4 39 | |
354 | -#define BZ_X_MTF_5 40 | |
355 | -#define BZ_X_MTF_6 41 | |
356 | -#define BZ_X_ENDHDR_2 42 | |
357 | -#define BZ_X_ENDHDR_3 43 | |
358 | -#define BZ_X_ENDHDR_4 44 | |
359 | -#define BZ_X_ENDHDR_5 45 | |
360 | -#define BZ_X_ENDHDR_6 46 | |
361 | -#define BZ_X_CCRC_1 47 | |
362 | -#define BZ_X_CCRC_2 48 | |
363 | -#define BZ_X_CCRC_3 49 | |
364 | -#define BZ_X_CCRC_4 50 | |
365 | - | |
366 | - | |
367 | -/*-- Constants for the fast MTF decoder. --*/ | |
368 | - | |
369 | -#define MTFA_SIZE 4096 | |
370 | -#define MTFL_SIZE 16 | |
371 | - | |
372 | - | |
373 | -/*-- Structure holding all the decompression-side stuff. --*/ | |
374 | - | |
375 | -typedef | |
376 | - struct { | |
377 | - /* pointer back to the struct bz_stream */ | |
378 | - bz_stream* strm; | |
379 | - | |
380 | - /* state indicator for this stream */ | |
381 | - Int32 state; | |
382 | - | |
383 | - /* for doing the final run-length decoding */ | |
384 | - UChar state_out_ch; | |
385 | - Int32 state_out_len; | |
386 | - Bool blockRandomised; | |
387 | - BZ_RAND_DECLS; | |
388 | - | |
389 | - /* the buffer for bit stream reading */ | |
390 | - UInt32 bsBuff; | |
391 | - Int32 bsLive; | |
392 | - | |
393 | - /* misc administratium */ | |
394 | - Int32 blockSize100k; | |
395 | - Bool smallDecompress; | |
396 | - Int32 currBlockNo; | |
397 | - Int32 verbosity; | |
398 | - | |
399 | - /* for undoing the Burrows-Wheeler transform */ | |
400 | - Int32 origPtr; | |
401 | - UInt32 tPos; | |
402 | - Int32 k0; | |
403 | - Int32 unzftab[256]; | |
404 | - Int32 nblock_used; | |
405 | - Int32 cftab[257]; | |
406 | - Int32 cftabCopy[257]; | |
407 | - | |
408 | - /* for undoing the Burrows-Wheeler transform (FAST) */ | |
409 | - UInt32 *tt; | |
410 | - | |
411 | - /* for undoing the Burrows-Wheeler transform (SMALL) */ | |
412 | - UInt16 *ll16; | |
413 | - UChar *ll4; | |
414 | - | |
415 | - /* stored and calculated CRCs */ | |
416 | - UInt32 storedBlockCRC; | |
417 | - UInt32 storedCombinedCRC; | |
418 | - UInt32 calculatedBlockCRC; | |
419 | - UInt32 calculatedCombinedCRC; | |
420 | - | |
421 | - /* map of bytes used in block */ | |
422 | - Int32 nInUse; | |
423 | - Bool inUse[256]; | |
424 | - Bool inUse16[16]; | |
425 | - UChar seqToUnseq[256]; | |
426 | - | |
427 | - /* for decoding the MTF values */ | |
428 | - UChar mtfa [MTFA_SIZE]; | |
429 | - Int32 mtfbase[256 / MTFL_SIZE]; | |
430 | - UChar selector [BZ_MAX_SELECTORS]; | |
431 | - UChar selectorMtf[BZ_MAX_SELECTORS]; | |
432 | - UChar len [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE]; | |
433 | - | |
434 | - Int32 limit [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE]; | |
435 | - Int32 base [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE]; | |
436 | - Int32 perm [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE]; | |
437 | - Int32 minLens[BZ_N_GROUPS]; | |
438 | - | |
439 | - /* save area for scalars in the main decompress code */ | |
440 | - Int32 save_i; | |
441 | - Int32 save_j; | |
442 | - Int32 save_t; | |
443 | - Int32 save_alphaSize; | |
444 | - Int32 save_nGroups; | |
445 | - Int32 save_nSelectors; | |
446 | - Int32 save_EOB; | |
447 | - Int32 save_groupNo; | |
448 | - Int32 save_groupPos; | |
449 | - Int32 save_nextSym; | |
450 | - Int32 save_nblockMAX; | |
451 | - Int32 save_nblock; | |
452 | - Int32 save_es; | |
453 | - Int32 save_N; | |
454 | - Int32 save_curr; | |
455 | - Int32 save_zt; | |
456 | - Int32 save_zn; | |
457 | - Int32 save_zvec; | |
458 | - Int32 save_zj; | |
459 | - Int32 save_gSel; | |
460 | - Int32 save_gMinlen; | |
461 | - Int32* save_gLimit; | |
462 | - Int32* save_gBase; | |
463 | - Int32* save_gPerm; | |
464 | - | |
465 | - } | |
466 | - DState; | |
467 | - | |
468 | - | |
469 | -/*-- Macros for decompression. --*/ | |
470 | - | |
471 | -#define BZ_GET_FAST(cccc) \ | |
472 | - s->tPos = s->tt[s->tPos]; \ | |
473 | - cccc = (UChar)(s->tPos & 0xff); \ | |
474 | - s->tPos >>= 8; | |
475 | - | |
476 | -#define BZ_GET_FAST_C(cccc) \ | |
477 | - c_tPos = c_tt[c_tPos]; \ | |
478 | - cccc = (UChar)(c_tPos & 0xff); \ | |
479 | - c_tPos >>= 8; | |
480 | - | |
481 | -#define SET_LL4(i,n) \ | |
482 | - { if (((i) & 0x1) == 0) \ | |
483 | - s->ll4[(i) >> 1] = (s->ll4[(i) >> 1] & 0xf0) | (n); else \ | |
484 | - s->ll4[(i) >> 1] = (s->ll4[(i) >> 1] & 0x0f) | ((n) << 4); \ | |
485 | - } | |
486 | - | |
487 | -#define GET_LL4(i) \ | |
488 | - ((((UInt32)(s->ll4[(i) >> 1])) >> (((i) << 2) & 0x4)) & 0xF) | |
489 | - | |
490 | -#define SET_LL(i,n) \ | |
491 | - { s->ll16[i] = (UInt16)(n & 0x0000ffff); \ | |
492 | - SET_LL4(i, n >> 16); \ | |
493 | - } | |
494 | - | |
495 | -#define GET_LL(i) \ | |
496 | - (((UInt32)s->ll16[i]) | (GET_LL4(i) << 16)) | |
497 | - | |
498 | -#define BZ_GET_SMALL(cccc) \ | |
499 | - cccc = BZ2_indexIntoF ( s->tPos, s->cftab ); \ | |
500 | - s->tPos = GET_LL(s->tPos); | |
501 | - | |
502 | - | |
503 | -/*-- externs for decompression. --*/ | |
504 | - | |
505 | -extern Int32 | |
506 | -BZ2_indexIntoF ( Int32, Int32* ); | |
507 | - | |
508 | -extern Int32 | |
509 | -BZ2_decompress ( DState* ); | |
510 | - | |
511 | -extern void | |
512 | -BZ2_hbCreateDecodeTables ( Int32*, Int32*, Int32*, UChar*, | |
513 | - Int32, Int32, Int32 ); | |
514 | - | |
515 | - | |
516 | -#endif | |
517 | - | |
518 | - | |
519 | -/*-- BZ_NO_STDIO seems to make NULL disappear on some platforms. --*/ | |
520 | - | |
521 | -#ifdef BZ_NO_STDIO | |
522 | -#ifndef NULL | |
523 | -#define NULL 0 | |
524 | -#endif | |
525 | -#endif | |
526 | - | |
527 | - | |
528 | -/*-------------------------------------------------------------*/ | |
529 | -/*--- end bzlib_private.h ---*/ | |
530 | -/*-------------------------------------------------------------*/ |
lib/bzlib_randtable.c
1 | -#include <config.h> | |
2 | - | |
3 | -/*-------------------------------------------------------------*/ | |
4 | -/*--- Table for randomising repetitive blocks ---*/ | |
5 | -/*--- randtable.c ---*/ | |
6 | -/*-------------------------------------------------------------*/ | |
7 | - | |
8 | -/*-- | |
9 | - This file is a part of bzip2 and/or libbzip2, a program and | |
10 | - library for lossless, block-sorting data compression. | |
11 | - | |
12 | - Copyright (C) 1996-2002 Julian R Seward. All rights reserved. | |
13 | - | |
14 | - Redistribution and use in source and binary forms, with or without | |
15 | - modification, are permitted provided that the following conditions | |
16 | - are met: | |
17 | - | |
18 | - 1. Redistributions of source code must retain the above copyright | |
19 | - notice, this list of conditions and the following disclaimer. | |
20 | - | |
21 | - 2. The origin of this software must not be misrepresented; you must | |
22 | - not claim that you wrote the original software. If you use this | |
23 | - software in a product, an acknowledgment in the product | |
24 | - documentation would be appreciated but is not required. | |
25 | - | |
26 | - 3. Altered source versions must be plainly marked as such, and must | |
27 | - not be misrepresented as being the original software. | |
28 | - | |
29 | - 4. The name of the author may not be used to endorse or promote | |
30 | - products derived from this software without specific prior written | |
31 | - permission. | |
32 | - | |
33 | - THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS | |
34 | - OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
35 | - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
36 | - ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY | |
37 | - DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
38 | - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE | |
39 | - GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
40 | - INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
41 | - WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
42 | - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
43 | - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
44 | - | |
45 | - Julian Seward, Cambridge, UK. | |
46 | - jseward@acm.org | |
47 | - bzip2/libbzip2 version 1.0 of 21 March 2000 | |
48 | - | |
49 | - This program is based on (at least) the work of: | |
50 | - Mike Burrows | |
51 | - David Wheeler | |
52 | - Peter Fenwick | |
53 | - Alistair Moffat | |
54 | - Radford Neal | |
55 | - Ian H. Witten | |
56 | - Robert Sedgewick | |
57 | - Jon L. Bentley | |
58 | - | |
59 | - For more information on these sources, see the manual. | |
60 | ---*/ | |
61 | - | |
62 | - | |
63 | -#include "bzlib_private.h" | |
64 | - | |
65 | - | |
66 | -/*---------------------------------------------*/ | |
67 | -Int32 BZ2_rNums[512] = { | |
68 | - 619, 720, 127, 481, 931, 816, 813, 233, 566, 247, | |
69 | - 985, 724, 205, 454, 863, 491, 741, 242, 949, 214, | |
70 | - 733, 859, 335, 708, 621, 574, 73, 654, 730, 472, | |
71 | - 419, 436, 278, 496, 867, 210, 399, 680, 480, 51, | |
72 | - 878, 465, 811, 169, 869, 675, 611, 697, 867, 561, | |
73 | - 862, 687, 507, 283, 482, 129, 807, 591, 733, 623, | |
74 | - 150, 238, 59, 379, 684, 877, 625, 169, 643, 105, | |
75 | - 170, 607, 520, 932, 727, 476, 693, 425, 174, 647, | |
76 | - 73, 122, 335, 530, 442, 853, 695, 249, 445, 515, | |
77 | - 909, 545, 703, 919, 874, 474, 882, 500, 594, 612, | |
78 | - 641, 801, 220, 162, 819, 984, 589, 513, 495, 799, | |
79 | - 161, 604, 958, 533, 221, 400, 386, 867, 600, 782, | |
80 | - 382, 596, 414, 171, 516, 375, 682, 485, 911, 276, | |
81 | - 98, 553, 163, 354, 666, 933, 424, 341, 533, 870, | |
82 | - 227, 730, 475, 186, 263, 647, 537, 686, 600, 224, | |
83 | - 469, 68, 770, 919, 190, 373, 294, 822, 808, 206, | |
84 | - 184, 943, 795, 384, 383, 461, 404, 758, 839, 887, | |
85 | - 715, 67, 618, 276, 204, 918, 873, 777, 604, 560, | |
86 | - 951, 160, 578, 722, 79, 804, 96, 409, 713, 940, | |
87 | - 652, 934, 970, 447, 318, 353, 859, 672, 112, 785, | |
88 | - 645, 863, 803, 350, 139, 93, 354, 99, 820, 908, | |
89 | - 609, 772, 154, 274, 580, 184, 79, 626, 630, 742, | |
90 | - 653, 282, 762, 623, 680, 81, 927, 626, 789, 125, | |
91 | - 411, 521, 938, 300, 821, 78, 343, 175, 128, 250, | |
92 | - 170, 774, 972, 275, 999, 639, 495, 78, 352, 126, | |
93 | - 857, 956, 358, 619, 580, 124, 737, 594, 701, 612, | |
94 | - 669, 112, 134, 694, 363, 992, 809, 743, 168, 974, | |
95 | - 944, 375, 748, 52, 600, 747, 642, 182, 862, 81, | |
96 | - 344, 805, 988, 739, 511, 655, 814, 334, 249, 515, | |
97 | - 897, 955, 664, 981, 649, 113, 974, 459, 893, 228, | |
98 | - 433, 837, 553, 268, 926, 240, 102, 654, 459, 51, | |
99 | - 686, 754, 806, 760, 493, 403, 415, 394, 687, 700, | |
100 | - 946, 670, 656, 610, 738, 392, 760, 799, 887, 653, | |
101 | - 978, 321, 576, 617, 626, 502, 894, 679, 243, 440, | |
102 | - 680, 879, 194, 572, 640, 724, 926, 56, 204, 700, | |
103 | - 707, 151, 457, 449, 797, 195, 791, 558, 945, 679, | |
104 | - 297, 59, 87, 824, 713, 663, 412, 693, 342, 606, | |
105 | - 134, 108, 571, 364, 631, 212, 174, 643, 304, 329, | |
106 | - 343, 97, 430, 751, 497, 314, 983, 374, 822, 928, | |
107 | - 140, 206, 73, 263, 980, 736, 876, 478, 430, 305, | |
108 | - 170, 514, 364, 692, 829, 82, 855, 953, 676, 246, | |
109 | - 369, 970, 294, 750, 807, 827, 150, 790, 288, 923, | |
110 | - 804, 378, 215, 828, 592, 281, 565, 555, 710, 82, | |
111 | - 896, 831, 547, 261, 524, 462, 293, 465, 502, 56, | |
112 | - 661, 821, 976, 991, 658, 869, 905, 758, 745, 193, | |
113 | - 768, 550, 608, 933, 378, 286, 215, 979, 792, 961, | |
114 | - 61, 688, 793, 644, 986, 403, 106, 366, 905, 644, | |
115 | - 372, 567, 466, 434, 645, 210, 389, 550, 919, 135, | |
116 | - 780, 773, 635, 389, 707, 100, 626, 958, 165, 504, | |
117 | - 920, 176, 193, 713, 857, 265, 203, 50, 668, 108, | |
118 | - 645, 990, 626, 197, 510, 357, 358, 850, 858, 364, | |
119 | - 936, 638 | |
120 | -}; | |
121 | - | |
122 | - | |
123 | -/*-------------------------------------------------------------*/ | |
124 | -/*--- end randtable.c ---*/ | |
125 | -/*-------------------------------------------------------------*/ |