Debian Codegraph @

Source/bzip2/bzlib.h

raw
1 /*
2 * This file is a part of the bzip2 compression module for NSIS.
3 *
4 * Copyright and license information can be found below.
5 * Modifications Copyright (C) 1999-2026 Nullsoft and Contributors
6 *
7 * The original zlib source code is available at
8 * http://www.bzip.org/
9 *
10 * This modification is not compatible with the original bzip2.
11 *
12 * This software is provided 'as-is', without any express or implied
13 * warranty.
14 *
15 * Reviewed for Unicode support by Jim Park -- 08/27/2007
16 */
17
18 /*-------------------------------------------------------------*/
19 /*--- Public header file for the library. ---*/
20 /*--- bzlib.h ---*/
21 /*-------------------------------------------------------------*/
22
23 /*--
24 This file is a part of bzip2 and/or libbzip2, a program and
25 library for lossless, block-sorting data compression.
26
27 Copyright (C) 1996-2000 Julian R Seward. All rights reserved.
28
29 Redistribution and use in source and binary forms, with or without
30 modification, are permitted provided that the following conditions
31 are met:
32
33 1. Redistributions of source code must retain the above copyright
34 notice, this list of conditions and the following disclaimer.
35
36 2. The origin of this software must not be misrepresented; you must
37 not claim that you wrote the original software. If you use this
38 software in a product, an acknowledgment in the product
39 documentation would be appreciated but is not required.
40
41 3. Altered source versions must be plainly marked as such, and must
42 not be misrepresented as being the original software.
43
44 4. The name of the author may not be used to endorse or promote
45 products derived from this software without specific prior written
46 permission.
47
48 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
49 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
50 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
52 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
54 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
55 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
56 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
57 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
58 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59
60 Julian Seward, Cambridge, UK.
61 [email protected]
62 bzip2/libbzip2 version 1.0 of 21 March 2000
63
64 This program is based on (at least) the work of:
65 Mike Burrows
66 David Wheeler
67 Peter Fenwick
68 Alistair Moffat
69 Radford Neal
70 Ian H. Witten
71 Robert Sedgewick
72 Jon L. Bentley
73
74 For more information on these sources, see the manual.
75 --*/
76
77
78 #ifndef _BZLIB_H
79 #define _BZLIB_H
80
81 #ifdef __cplusplus
82 extern "C" {
83 #endif
84
85 #include "../exehead/config.h"
86 #include "../Platform.h"
87
88 #define BZ_RUN 0
89 #define BZ_FLUSH 1
90 #define BZ_FINISH 2
91
92 #define BZ_OK 0
93 #define BZ_RUN_OK 1
94 #define BZ_FLUSH_OK 2
95 #define BZ_FINISH_OK 3
96 #define BZ_STREAM_END 4
97 #define BZ_SEQUENCE_ERROR (-1)
98 #define BZ_PARAM_ERROR (-2)
99 #define BZ_MEM_ERROR (-3)
100 #define BZ_DATA_ERROR (-4)
101 #define BZ_DATA_ERROR_MAGIC (-5)
102 #define BZ_IO_ERROR (-6)
103 #define BZ_UNEXPECTED_EOF (-7)
104 #define BZ_OUTBUFF_FULL (-8)
105 #define BZ_CONFIG_ERROR (-9)
106
107 /*-- Constants for the back end. --*/
108
109 #define BZ_MAX_ALPHA_SIZE 258
110 #define BZ_MAX_CODE_LEN 23
111
112 #define BZ_RUNA 0
113 #define BZ_RUNB 1
114
115 #define BZ_N_GROUPS 6
116 #define BZ_G_SIZE 50
117 #define BZ_N_ITERS 4
118
119 #define BZ_MAX_SELECTORS (2 + (900000 / BZ_G_SIZE))
120
121 typedef char Char;
122 typedef unsigned char Bool;
123 typedef unsigned char UChar;
124 typedef int Int32;
125 typedef unsigned int UInt32;
126 typedef short Int16;
127 typedef unsigned short UInt16;
128
129 #define True ((Bool)1)
130 #define False ((Bool)0)
131
132 #define AssertD(cond,msg) /* */
133 #define AssertH(cond,errcode) /* */
134 #define AssertD(cond,msg) /* */
135 #define VPrintf0(zf) /* */
136 #define VPrintf1(zf,za1) /* */
137 #define VPrintf2(zf,za1,za2) /* */
138 #define VPrintf3(zf,za1,za2,za3) /* */
139 #define VPrintf4(zf,za1,za2,za3,za4) /* */
140 #define VPrintf5(zf,za1,za2,za3,za4,za5) /* */
141
142 #ifndef EXEHEAD
143
144 #include <stdlib.h>
145 #define BZALLOC(items) malloc(items)
146 #define BZFREE(addr) { if (addr) free(addr); }
147 #define mini_memcpy memcpy
148
149 typedef struct {
150 unsigned char *next_in;
151 unsigned int avail_in;
152
153 unsigned char *next_out;
154 unsigned int avail_out;
155
156 void *state;
157 } bz_stream;
158
159 /*-- Core (low-level) library functions --*/
160
161 extern int BZ2_bzCompressInit(
162 bz_stream* strm,
163 int blockSize100k,
164 int verbosity,
165 int workFactor
166 );
167
168 extern int BZ2_bzCompress( bz_stream* strm, int action );
169 extern int BZ2_bzCompressEnd(bz_stream* strm );
170
171 /*-- States and modes for compression. --*/
172
173 #define BZ_M_IDLE 1
174 #define BZ_M_RUNNING 2
175 #define BZ_M_FLUSHING 3
176 #define BZ_M_FINISHING 4
177
178 #define BZ_S_OUTPUT 1
179 #define BZ_S_INPUT 2
180
181 #define BZ_N_RADIX 2
182 #define BZ_N_QSORT 12
183 #define BZ_N_SHELL 18
184 #define BZ_N_OVERSHOOT (BZ_N_RADIX + BZ_N_QSORT + BZ_N_SHELL + 2)
185
186 /*-- Structure holding all the compression-side stuff. --*/
187
188 typedef struct {
189 /* pointer back to the struct bz_stream */
190 bz_stream *strm;
191
192 /* mode this stream is in, and whether inputting */
193 /* or outputting data */
194 Int32 mode;
195 Int32 state;
196
197 /* remembers avail_in when flush/finish requested */
198 UInt32 avail_in_expect;
199
200 /* for doing the block sorting */
201 UInt32* arr1;
202 UInt32* arr2;
203 UInt32* ftab;
204 Int32 origPtr;
205
206 /* aliases for arr1 and arr2 */
207 UInt32* ptr;
208 UChar* block;
209 UInt16* mtfv;
210 UChar* zbits;
211
212 /* for deciding when to use the fallback sorting algorithm */
213 Int32 workFactor;
214
215 /* run-length-encoding of the input */
216 UInt32 state_in_ch;
217 Int32 state_in_len;
218
219 /* input and output limits and current posns */
220 Int32 nblock;
221 Int32 nblockMAX;
222 Int32 numZ;
223 Int32 state_out_pos;
224
225 /* map of bytes used in block */
226 Int32 nInUse;
227 Bool inUse[256];
228 UChar unseqToSeq[256];
229
230 /* the buffer for bit stream creation */
231 UInt32 bsBuff;
232 Int32 bsLive;
233
234 /* misc administratium */
235 Int32 blockNo;
236
237 /* stuff for coding the MTF values */
238 Int32 nMTF;
239 Int32 mtfFreq [BZ_MAX_ALPHA_SIZE];
240 UChar selector [BZ_MAX_SELECTORS];
241 UChar selectorMtf[BZ_MAX_SELECTORS];
242
243 UChar len [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
244 Int32 code [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
245 Int32 rfreq [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
246 /* second dimension: only 3 needed; 4 makes index calculations faster */
247 UInt32 len_pack[BZ_MAX_ALPHA_SIZE][4];
248
249 } EState;
250
251
252
253 /*-- externs for compression. --*/
254
255 extern void BZ2_blockSort ( EState* );
256 extern void BZ2_compressBlock ( EState*, Bool );
257 extern void BZ2_bsInitWrite ( EState* );
258 extern void BZ2_hbAssignCodes ( Int32*, UChar*, Int32, Int32, Int32 );
259 extern void BZ2_hbMakeCodeLengths ( UChar*, Int32*, Int32, Int32 );
260
261 #else//EXEHEAD
262
263 /*-- states for decompression. --*/
264
265 #define BZ_X_IDLE 1
266 #define BZ_X_OUTPUT 2
267
268 #define BZ_X_BLKHDR_1 11
269 #define BZ_X_RANDBIT 12
270 #define BZ_X_ORIGPTR_1 13
271 #define BZ_X_ORIGPTR_2 14
272 #define BZ_X_ORIGPTR_3 15
273 #define BZ_X_MAPPING_1 16
274 #define BZ_X_MAPPING_2 17
275 #define BZ_X_SELECTOR_1 18
276 #define BZ_X_SELECTOR_2 19
277 #define BZ_X_SELECTOR_3 20
278 #define BZ_X_CODING_1 21
279 #define BZ_X_CODING_2 22
280 #define BZ_X_CODING_3 23
281 #define BZ_X_MTF_1 24
282 #define BZ_X_MTF_2 25
283 #define BZ_X_MTF_3 26
284 #define BZ_X_MTF_4 27
285 #define BZ_X_MTF_5 28
286 #define BZ_X_MTF_6 29
287
288
289
290 /*-- Constants for the fast MTF decoder. --*/
291
292 #define MTFA_SIZE 4096
293 #define MTFL_SIZE 16
294
295
296
297 /* save area for scalars in the main decompress code */
298 typedef struct {
299 Int32 i;
300 Int32 j;
301 Int32 t;
302 Int32 alphaSize;
303 Int32 nGroups;
304 Int32 nSelectors;
305 Int32 EOB;
306 Int32 groupNo;
307 Int32 groupPos;
308 Int32 nextSym;
309 Int32 nblockMAX;
310 Int32 nblock;
311 Int32 es;
312 Int32 N;
313 Int32 curr;
314 Int32 zt;
315 Int32 zn;
316 Int32 zvec;
317 Int32 zj;
318 Int32 gSel;
319 Int32 gMinlen;
320 Int32* gLimit;
321 Int32* gBase;
322 Int32* gPerm;
323 } DState_save;
324
325 /*-- Structure holding all the decompression-side stuff. --*/
326
327 typedef struct {
328 /* pointer back to the struct bz_stream */
329 unsigned char *next_in;
330 unsigned int avail_in;
331
332 unsigned char *next_out;
333 unsigned int avail_out;
334
335 /* state indicator for this stream */
336 char state;
337
338 UChar state_out_ch;
339 Int32 state_out_len;
340 Int32 nblock_used;
341 Int32 k0;
342 UInt32 tPos;
343
344 /* the buffer for bit stream reading */
345 UInt32 bsBuff;
346 Int32 bsLive;
347
348 /* for undoing the Burrows-Wheeler transform */
349 Int32 origPtr;
350 Int32 unzftab[256];
351 Int32 cftab[257];
352 Int32 cftabCopy[257];
353
354 #ifndef NSIS_COMPRESS_BZIP2_SMALLMODE
355 /* for undoing the Burrows-Wheeler transform (FAST) */
356 UInt32 tt[ NSIS_COMPRESS_BZIP2_LEVEL * 100000 ];
357 #else
358 /* for undoing the Burrows-Wheeler transform (SMALL) */
359 UInt16 ll16 [ NSIS_COMPRESS_BZIP2_LEVEL*100000 ];
360 UChar ll4 [((1 + NSIS_COMPRESS_BZIP2_LEVEL*100000) >> 1) ];
361 #endif
362
363 /* map of bytes used in block */
364 Int32 nInUse;
365 Bool inUse[256];
366 Bool inUse16[16];
367 UChar seqToUnseq[256];
368
369 /* for decoding the MTF values */
370 UChar mtfa [MTFA_SIZE];
371 Int32 mtfbase[256 / MTFL_SIZE];
372 UChar selector [BZ_MAX_SELECTORS];
373 UChar selectorMtf[BZ_MAX_SELECTORS];
374 UChar len [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
375
376 Int32 limit [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
377 Int32 base [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
378 Int32 perm [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
379 Int32 minLens[BZ_N_GROUPS];
380
381 /* save area for scalars in the main decompress code */
382 DState_save save;
383 } DState;
384
385
386 #ifndef NSIS_COMPRESS_BZIP2_SMALLMODE
387 /*-- Macros for decompression. --*/
388
389 #define BZ_GET_FAST(cccc) \
390 s->tPos = s->tt[s->tPos]; \
391 cccc = (UChar)(s->tPos & 0xff); \
392 s->tPos >>= 8;
393
394 #define BZ_GET_FAST_C(cccc) \
395 c_tPos = c_tt[c_tPos]; \
396 cccc = (UChar)(c_tPos & 0xff); \
397 c_tPos >>= 8;
398
399
400 #else//NSIS_COMPRESS_BZIP2_SMALLMODE
401
402 #define SET_LL4(i,n) \
403 { if (((i) & 0x1) == 0) \
404 s->ll4[(i) >> 1] = (s->ll4[(i) >> 1] & 0xf0) | (n); else \
405 s->ll4[(i) >> 1] = (s->ll4[(i) >> 1] & 0x0f) | ((n) << 4); \
406 }
407
408 #define GET_LL4(i) \
409 ((((UInt32)(s->ll4[(i) >> 1])) >> (((i) << 2) & 0x4)) & 0xF)
410
411 #define SET_LL(i,n) \
412 { s->ll16[i] = (UInt16)(n & 0x0000ffff); \
413 SET_LL4(i, n >> 16); \
414 }
415
416 #define GET_LL(i) \
417 (((UInt32)s->ll16[i]) | (GET_LL4(i) << 16))
418
419 #define BZ_GET_SMALL(cccc) \
420 cccc = BZ2_indexIntoF ( s->tPos, s->cftab ); \
421 s->tPos = GET_LL(s->tPos);
422
423 extern Int32 BZ2_indexIntoF( Int32, Int32* );
424
425 #endif//smallmode
426
427 /*-- externs for decompression. --*/
428 extern Int32 NSISCALL BZ2_decompress ( DState* );
429
430 extern void NSISCALL BZ2_hbCreateDecodeTables ( Int32*, Int32*, Int32*, UChar*,
431 Int32, Int32, Int32 );
432
433
434 #define BZ2_bzDecompressInit(s) { (s)->state = BZ_X_BLKHDR_1; (s)->bsLive = 0; }
435 int NSISCALL BZ2_bzDecompress(DState *s);
436
437 #endif//EXEHEAD
438
439 #ifdef __cplusplus
440 }
441 #endif
442
443 #endif
444
445 /*-------------------------------------------------------------*/
446 /*--- end bzlib.h ---*/
447 /*-------------------------------------------------------------*/