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