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