1 
2 /*-------------------------------------------------------------*/
3 /*--- Private header file for the library.                  ---*/
4 /*---                                       bzlib_private.h ---*/
5 /*-------------------------------------------------------------*/
6 
7 /* ------------------------------------------------------------------
8    This file is part of bzip2/libbzip2, a program and library for
9    lossless, block-sorting data compression.
10 
11    bzip2/libbzip2 version 1.0.4 of 20 December 2006
12    Copyright (C) 1996-2006 Julian Seward <jseward@bzip.org>
13    This file was modified for ClamAV by aCaB <acab@clamav.net>
14 
15    This program is released under the terms of the license contained
16    in the file COPYING.bzip2.
17    ------------------------------------------------------------------ */
18 
19 
20 #ifndef _BZLIB_PRIVATE_H
21 #define _BZLIB_PRIVATE_H
22 
23 #if HAVE_CONFIG_H
24 #include "clamav-config.h"
25 #endif
26 
27 #include <stdlib.h>
28 
29 #ifndef BZ_NO_STDIO
30 #include <stdio.h>
31 #include <ctype.h>
32 #include <string.h>
33 #endif
34 
35 #include "nsis_bzlib.h"
36 
37 
38 
39 /*-- General stuff. --*/
40 
41 #define BZ_VERSION  "1.0.4, 20-Dec-2006"
42 
43 typedef char            Char;
44 typedef unsigned char   Bool;
45 typedef unsigned char   UChar;
46 typedef int             Int32;
47 typedef unsigned int    UInt32;
48 typedef short           Int16;
49 typedef unsigned short  UInt16;
50 
51 #define True  ((Bool)1)
52 #define False ((Bool)0)
53 
54 #ifndef __GNUC__
55 #define __inline__  /* */
56 #endif
57 
58 /* aCaB */
59 /* #ifndef BZ_NO_STDIO */
60 
61 /* extern void BZ2_bz__AssertH__fail ( int errcode ); */
62 /* #define AssertH(cond,errcode) \ */
63 /*    { if (!(cond)) BZ2_bz__AssertH__fail ( errcode ); } */
64 
65 /* #if BZ_DEBUG */
66 /* #define AssertD(cond,msg) \ */
67 /*    { if (!(cond)) {       \ */
68 /*       fprintf ( stderr,   \ */
69 /*         "\n\nlibbzip2(debug build): internal error\n\t%s\n", msg );\ */
70 /*       exit(1); \ */
71 /*    }} */
72 /* #else */
73 /* #define AssertD(cond,msg) /\* *\/ */
74 /* #endif */
75 
76 /* #define VPrintf0(zf) \ */
77 /*    fprintf(stderr,zf) */
78 /* #define VPrintf1(zf,za1) \ */
79 /*    fprintf(stderr,zf,za1) */
80 /* #define VPrintf2(zf,za1,za2) \ */
81 /*    fprintf(stderr,zf,za1,za2) */
82 /* #define VPrintf3(zf,za1,za2,za3) \ */
83 /*    fprintf(stderr,zf,za1,za2,za3) */
84 /* #define VPrintf4(zf,za1,za2,za3,za4) \ */
85 /*    fprintf(stderr,zf,za1,za2,za3,za4) */
86 /* #define VPrintf5(zf,za1,za2,za3,za4,za5) \ */
87 /*    fprintf(stderr,zf,za1,za2,za3,za4,za5) */
88 
89 /* #else */
90 
91 /* extern void bz_internal_error ( int errcode ); */
92 /* #define AssertH(cond,errcode) \ */
93 /*    { if (!(cond)) bz_internal_error ( errcode ); } */
94 /* #define AssertD(cond,msg)                do { } while (0) */
95 #define VPrintf0(zf)                     do { } while (0)
96 #define VPrintf1(zf,za1)                 do { } while (0)
97 #define VPrintf2(zf,za1,za2)             do { } while (0)
98 #define VPrintf3(zf,za1,za2,za3)         do { } while (0)
99 #define VPrintf4(zf,za1,za2,za3,za4)     do { } while (0)
100 #define VPrintf5(zf,za1,za2,za3,za4,za5) do { } while (0)
101 
102 /* #endif */
103 
104 #define BZALLOC(nnn) (strm->bzalloc)(strm->opaque,(nnn),1)
105 #define BZFREE(ppp)  (strm->bzfree)(strm->opaque,(ppp))
106 
107 
108 /*-- Header bytes. --*/
109 
110 /* #define BZ_HDR_B 0x42   /\* 'B' *\/ */
111 /* #define BZ_HDR_Z 0x5a   /\* 'Z' *\/ */
112 /* #define BZ_HDR_h 0x68   /\* 'h' *\/ */
113 /* #define BZ_HDR_0 0x30   /\* '0' *\/ */
114 
115 /*-- Constants for the back end. --*/
116 
117 #define BZ_MAX_ALPHA_SIZE 258
118 #define BZ_MAX_CODE_LEN    23
119 
120 #define BZ_RUNA 0
121 #define BZ_RUNB 1
122 
123 #define BZ_N_GROUPS 6
124 #define BZ_G_SIZE   50
125 #define BZ_N_ITERS  4
126 
127 #define BZ_MAX_SELECTORS (2 + (900000 / BZ_G_SIZE))
128 
129 
130 
131 /*-- Stuff for randomising repetitive blocks. --*/
132 /* aCaB */
133 /* extern Int32 BZ2_rNums[512]; */
134 
135 #define BZ_RAND_DECLS                          \
136    Int32 rNToGo;                               \
137    Int32 rTPos                                 \
138 
139 #define BZ_RAND_INIT_MASK                      \
140    s->rNToGo = 0;                              \
141    s->rTPos  = 0                               \
142 
143 #define BZ_RAND_MASK ((s->rNToGo == 1) ? 1 : 0)
144 
145 #define BZ_RAND_UPD_MASK                       \
146    if (s->rNToGo == 0) {                       \
147       s->rNToGo = BZ2_rNums[s->rTPos];         \
148       s->rTPos++;                              \
149       if (s->rTPos == 512) s->rTPos = 0;       \
150    }                                           \
151    s->rNToGo--;
152 /**/
153 
154 /*-- Stuff for doing CRCs. --*/
155 /* aCaB
156 extern UInt32 BZ2_crc32Table[256];
157 
158 #define BZ_INITIALISE_CRC(crcVar)              \
159 {                                              \
160    crcVar = 0xffffffffL;                       \
161 }
162 
163 #define BZ_FINALISE_CRC(crcVar)                \
164 {                                              \
165    crcVar = ~(crcVar);                         \
166 }
167 
168 #define BZ_UPDATE_CRC(crcVar,cha)              \
169 {                                              \
170    crcVar = (crcVar << 8) ^                    \
171             BZ2_crc32Table[(crcVar >> 24) ^    \
172                            ((UChar)cha)];      \
173 }
174 */
175 
176 /*-- States and modes for compression. --*/
177 
178 #define BZ_M_IDLE      1
179 #define BZ_M_RUNNING   2
180 #define BZ_M_FLUSHING  3
181 #define BZ_M_FINISHING 4
182 
183 #define BZ_S_OUTPUT    1
184 #define BZ_S_INPUT     2
185 
186 #define BZ_N_RADIX 2
187 #define BZ_N_QSORT 12
188 #define BZ_N_SHELL 18
189 #define BZ_N_OVERSHOOT (BZ_N_RADIX + BZ_N_QSORT + BZ_N_SHELL + 2)
190 
191 
192 
193 
194 /*-- Structure holding all the compression-side stuff. --*/
195 
196 typedef
197    struct {
198       /* pointer back to the struct nsis_bzstream */
199       nsis_bzstream* strm;
200 
201       /* mode this stream is in, and whether inputting */
202       /* or outputting data */
203       Int32    mode;
204       Int32    state;
205 
206       /* remembers avail_in when flush/finish requested */
207       UInt32   avail_in_expect;
208 
209       /* for doing the block sorting */
210       UInt32*  arr1;
211       UInt32*  arr2;
212       UInt32*  ftab;
213       Int32    origPtr;
214 
215       /* aliases for arr1 and arr2 */
216       UInt32*  ptr;
217       UChar*   block;
218       UInt16*  mtfv;
219       UChar*   zbits;
220 
221       /* for deciding when to use the fallback sorting algorithm */
222       Int32    workFactor;
223 
224       /* run-length-encoding of the input */
225       UInt32   state_in_ch;
226       Int32    state_in_len;
227       BZ_RAND_DECLS;
228 
229       /* input and output limits and current posns */
230       Int32    nblock;
231       Int32    nblockMAX;
232       Int32    numZ;
233       Int32    state_out_pos;
234 
235       /* map of bytes used in block */
236       Int32    nInUse;
237       Bool     inUse[256];
238       UChar    unseqToSeq[256];
239 
240       /* the buffer for bit stream creation */
241       UInt32   bsBuff;
242       Int32    bsLive;
243 
244       /* block and combined CRCs */
245       UInt32   blockCRC;
246       UInt32   combinedCRC;
247 
248       /* misc administratium */
249       Int32    verbosity;
250       Int32    blockNo;
251       Int32    blockSize100k;
252 
253       /* stuff for coding the MTF values */
254       Int32    nMTF;
255       Int32    mtfFreq    [BZ_MAX_ALPHA_SIZE];
256       UChar    selector   [BZ_MAX_SELECTORS];
257       UChar    selectorMtf[BZ_MAX_SELECTORS];
258 
259       UChar    len     [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
260       Int32    code    [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
261       Int32    rfreq   [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
262       /* second dimension: only 3 needed; 4 makes index calculations faster */
263       UInt32   len_pack[BZ_MAX_ALPHA_SIZE][4];
264 
265    }
266    EState;
267 
268 
269 /*-- states for decompression. --*/
270 
271 #define BZ_X_IDLE        1
272 #define BZ_X_OUTPUT      2
273 
274 #define BZ_X_MAGIC_1     10
275 #define BZ_X_MAGIC_2     11
276 #define BZ_X_MAGIC_3     12
277 #define BZ_X_MAGIC_4     13
278 #define BZ_X_BLKHDR_1    14
279 #define BZ_X_BLKHDR_2    15
280 #define BZ_X_BLKHDR_3    16
281 #define BZ_X_BLKHDR_4    17
282 #define BZ_X_BLKHDR_5    18
283 #define BZ_X_BLKHDR_6    19
284 #define BZ_X_BCRC_1      20
285 #define BZ_X_BCRC_2      21
286 #define BZ_X_BCRC_3      22
287 #define BZ_X_BCRC_4      23
288 #define BZ_X_RANDBIT     24
289 #define BZ_X_ORIGPTR_1   25
290 #define BZ_X_ORIGPTR_2   26
291 #define BZ_X_ORIGPTR_3   27
292 #define BZ_X_MAPPING_1   28
293 #define BZ_X_MAPPING_2   29
294 #define BZ_X_SELECTOR_1  30
295 #define BZ_X_SELECTOR_2  31
296 #define BZ_X_SELECTOR_3  32
297 #define BZ_X_CODING_1    33
298 #define BZ_X_CODING_2    34
299 #define BZ_X_CODING_3    35
300 #define BZ_X_MTF_1       36
301 #define BZ_X_MTF_2       37
302 #define BZ_X_MTF_3       38
303 #define BZ_X_MTF_4       39
304 #define BZ_X_MTF_5       40
305 #define BZ_X_MTF_6       41
306 #define BZ_X_ENDHDR_2    42
307 #define BZ_X_ENDHDR_3    43
308 #define BZ_X_ENDHDR_4    44
309 #define BZ_X_ENDHDR_5    45
310 #define BZ_X_ENDHDR_6    46
311 #define BZ_X_CCRC_1      47
312 #define BZ_X_CCRC_2      48
313 #define BZ_X_CCRC_3      49
314 #define BZ_X_CCRC_4      50
315 
316 
317 
318 /*-- Constants for the fast MTF decoder. --*/
319 
320 #define MTFA_SIZE 4096
321 #define MTFL_SIZE 16
322 
323 
324 
325 /*-- Structure holding all the decompression-side stuff. --*/
326 
327 typedef
328    struct {
329       /* pointer back to the struct nsis_bzstream */
330       nsis_bzstream* strm;
331 
332       /* state indicator for this stream */
333       Int32    state;
334 
335       /* for doing the final run-length decoding */
336       UChar    state_out_ch;
337       Int32    state_out_len;
338       Bool     blockRandomised;
339       BZ_RAND_DECLS;
340 
341       /* the buffer for bit stream reading */
342       UInt32   bsBuff;
343       Int32    bsLive;
344 
345       /* misc administratium */
346       Int32    blockSize100k;
347       Bool     smallDecompress;
348       Int32    currBlockNo;
349       Int32    verbosity;
350 
351       /* for undoing the Burrows-Wheeler transform */
352       Int32    origPtr;
353       UInt32   tPos;
354       Int32    k0;
355       Int32    unzftab[256];
356       Int32    nblock_used;
357       Int32    cftab[257];
358       Int32    cftabCopy[257];
359 
360       /* for undoing the Burrows-Wheeler transform (FAST) */
361       UInt32   *tt;
362 
363       /* for undoing the Burrows-Wheeler transform (SMALL) */
364       UInt16   *ll16;
365       UChar    *ll4;
366 
367       /* stored and calculated CRCs */
368       UInt32   storedBlockCRC;
369       UInt32   storedCombinedCRC;
370       UInt32   calculatedBlockCRC;
371       UInt32   calculatedCombinedCRC;
372 
373       /* map of bytes used in block */
374       Int32    nInUse;
375       Bool     inUse[256];
376       Bool     inUse16[16];
377       UChar    seqToUnseq[256];
378 
379       /* for decoding the MTF values */
380       UChar    mtfa   [MTFA_SIZE];
381       Int32    mtfbase[256 / MTFL_SIZE];
382       UChar    selector   [BZ_MAX_SELECTORS];
383       UChar    selectorMtf[BZ_MAX_SELECTORS];
384       UChar    len  [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
385 
386       Int32    limit  [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
387       Int32    base   [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
388       Int32    perm   [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
389       Int32    minLens[BZ_N_GROUPS];
390 
391       /* save area for scalars in the main decompress code */
392       Int32    save_i;
393       Int32    save_j;
394       Int32    save_t;
395       Int32    save_alphaSize;
396       Int32    save_nGroups;
397       Int32    save_nSelectors;
398       Int32    save_EOB;
399       Int32    save_groupNo;
400       Int32    save_groupPos;
401       Int32    save_nextSym;
402       Int32    save_nblockMAX;
403       Int32    save_nblock;
404       Int32    save_es;
405       Int32    save_N;
406       Int32    save_curr;
407       Int32    save_zt;
408       Int32    save_zn;
409       Int32    save_zvec;
410       Int32    save_zj;
411       Int32    save_gSel;
412       Int32    save_gMinlen;
413       Int32*   save_gLimit;
414       Int32*   save_gBase;
415       Int32*   save_gPerm;
416 
417    }
418    DState;
419 
420 
421 
422 /*-- Macros for decompression. --*/
423 
424 #define BZ_GET_FAST(cccc)                     \
425     /* c_tPos is unsigned, hence test < 0 is pointless. */ \
426     if (s->tPos >= (UInt32)100000 * (UInt32)s->blockSize100k) return True; \
427     s->tPos = s->tt[s->tPos];                 \
428     cccc = (UChar)(s->tPos & 0xff);           \
429     s->tPos >>= 8;
430 
431 #define BZ_GET_FAST_C(cccc)                   \
432     /* c_tPos is unsigned, hence test < 0 is pointless. */ \
433     if (c_tPos >= (UInt32)100000 * (UInt32)ro_blockSize100k) return True; \
434     c_tPos = c_tt[c_tPos];                    \
435     cccc = (UChar)(c_tPos & 0xff);            \
436     c_tPos >>= 8;
437 
438 #define SET_LL4(i,n)                                          \
439    { if (((i) & 0x1) == 0)                                    \
440         s->ll4[(i) >> 1] = (s->ll4[(i) >> 1] & 0xf0) | (n); else    \
441         s->ll4[(i) >> 1] = (s->ll4[(i) >> 1] & 0x0f) | ((n) << 4);  \
442    }
443 
444 #define GET_LL4(i)                             \
445    ((((UInt32)(s->ll4[(i) >> 1])) >> (((i) << 2) & 0x4)) & 0xF)
446 
447 #define SET_LL(i,n)                          \
448    { s->ll16[i] = (UInt16)(n & 0x0000ffff);  \
449      SET_LL4(i, n >> 16);                    \
450    }
451 
452 #define GET_LL(i) \
453    (((UInt32)s->ll16[i]) | (GET_LL4(i) << 16))
454 
455 #define BZ_GET_SMALL(cccc)                            \
456       /* c_tPos is unsigned, hence test < 0 is pointless. */ \
457       if (s->tPos >= (UInt32)100000 * (UInt32)s->blockSize100k) return True; \
458       cccc = indexIntoF ( s->tPos, s->cftab );    \
459       s->tPos = GET_LL(s->tPos);
460 
461 #endif
462 
463 
464 /*-- BZ_NO_STDIO seems to make NULL disappear on some platforms. --*/
465 
466 #ifdef BZ_NO_STDIO
467 #ifndef NULL
468 #define NULL 0
469 #endif
470 #endif
471 
472 
473 /*-------------------------------------------------------------*/
474 /*--- end                                   bzlib_private.h ---*/
475 /*-------------------------------------------------------------*/
476