bzlib_private.h 13 KB

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