bzlib_private.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. /*--- Private header file for the library. ---*/
  7. /*--- bzlib_private.h ---*/
  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. Copyright (C) 1996-2002 Julian R Seward. All rights reserved.
  13. Redistribution and use in source and binary forms, with or without
  14. modification, are permitted provided that the following conditions
  15. are met:
  16. 1. Redistributions of source code must retain the above copyright
  17. notice, this list of conditions and the following disclaimer.
  18. 2. The origin of this software must not be misrepresented; you must
  19. not claim that you wrote the original software. If you use this
  20. software in a product, an acknowledgment in the product
  21. documentation would be appreciated but is not required.
  22. 3. Altered source versions must be plainly marked as such, and must
  23. not be misrepresented as being the original software.
  24. 4. The name of the author may not be used to endorse or promote
  25. products derived from this software without specific prior written
  26. permission.
  27. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  28. OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  29. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  30. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  31. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  33. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  34. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  35. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  36. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  37. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. Julian Seward, Cambridge, UK.
  39. jseward@acm.org
  40. bzip2/libbzip2 version 1.0 of 21 March 2000
  41. This program is based on (at least) the work of:
  42. Mike Burrows
  43. David Wheeler
  44. Peter Fenwick
  45. Alistair Moffat
  46. Radford Neal
  47. Ian H. Witten
  48. Robert Sedgewick
  49. Jon L. Bentley
  50. For more information on these sources, see the manual.
  51. --*/
  52. #ifndef _BZLIB_PRIVATE_H
  53. #define _BZLIB_PRIVATE_H
  54. #include <malloc.h>
  55. #include "bzlib.h"
  56. #ifndef BZ_NO_STDIO
  57. #include <stdio.h>
  58. #include <ctype.h>
  59. #include <string.h>
  60. #endif
  61. /*-- General stuff. --*/
  62. #define BZ_VERSION "1.0.2, 30-Dec-2001"
  63. typedef char Char;
  64. typedef unsigned char Bool;
  65. typedef unsigned char UChar;
  66. typedef int Int32;
  67. typedef unsigned int UInt32;
  68. typedef short Int16;
  69. typedef unsigned short UInt16;
  70. #define True ((Bool)1)
  71. #define False ((Bool)0)
  72. #ifndef __GNUC__
  73. #define __inline__ /* */
  74. #endif
  75. #ifndef BZ_NO_STDIO
  76. extern void BZ2_bz__AssertH__fail ( int errcode );
  77. #define AssertH(cond,errcode) \
  78. { if (!(cond)) BZ2_bz__AssertH__fail ( errcode ); }
  79. #if BZ_DEBUG
  80. #define AssertD(cond,msg) \
  81. { if (!(cond)) { \
  82. fprintf ( stderr, \
  83. "\n\nlibbzip2(debug build): internal error\n\t%s\n", msg );\
  84. exit(1); \
  85. }}
  86. #else
  87. #define AssertD(cond,msg) /* */
  88. #endif
  89. #define VPrintf0(zf) \
  90. fprintf(stderr,zf)
  91. #define VPrintf1(zf,za1) \
  92. fprintf(stderr,zf,za1)
  93. #define VPrintf2(zf,za1,za2) \
  94. fprintf(stderr,zf,za1,za2)
  95. #define VPrintf3(zf,za1,za2,za3) \
  96. fprintf(stderr,zf,za1,za2,za3)
  97. #define VPrintf4(zf,za1,za2,za3,za4) \
  98. fprintf(stderr,zf,za1,za2,za3,za4)
  99. #define VPrintf5(zf,za1,za2,za3,za4,za5) \
  100. fprintf(stderr,zf,za1,za2,za3,za4,za5)
  101. #else
  102. extern void bz_internal_error ( int errcode );
  103. #define AssertH(cond,errcode) \
  104. { if (!(cond)) bz_internal_error ( errcode ); }
  105. #define AssertD(cond,msg) /* */
  106. #define VPrintf0(zf) /* */
  107. #define VPrintf1(zf,za1) /* */
  108. #define VPrintf2(zf,za1,za2) /* */
  109. #define VPrintf3(zf,za1,za2,za3) /* */
  110. #define VPrintf4(zf,za1,za2,za3,za4) /* */
  111. #define VPrintf5(zf,za1,za2,za3,za4,za5) /* */
  112. #endif
  113. #define BZALLOC(nnn) (strm->bzalloc)(strm->opaque,(nnn),1)
  114. #define BZFREE(ppp) (strm->bzfree)(strm->opaque,(ppp))
  115. /*-- Header bytes. --*/
  116. #define BZ_HDR_B 0x42 /* 'B' */
  117. #define BZ_HDR_Z 0x5a /* 'Z' */
  118. #define BZ_HDR_h 0x68 /* 'h' */
  119. #define BZ_HDR_0 0x30 /* '0' */
  120. /*-- Constants for the back end. --*/
  121. #define BZ_MAX_ALPHA_SIZE 258
  122. #define BZ_MAX_CODE_LEN 23
  123. #define BZ_RUNA 0
  124. #define BZ_RUNB 1
  125. #define BZ_N_GROUPS 6
  126. #define BZ_G_SIZE 50
  127. #define BZ_N_ITERS 4
  128. #define BZ_MAX_SELECTORS (2 + (900000 / BZ_G_SIZE))
  129. /*-- Stuff for randomising repetitive blocks. --*/
  130. extern Int32 BZ2_rNums[512];
  131. #define BZ_RAND_DECLS \
  132. Int32 rNToGo; \
  133. Int32 rTPos \
  134. #define BZ_RAND_INIT_MASK \
  135. s->rNToGo = 0; \
  136. s->rTPos = 0 \
  137. #define BZ_RAND_MASK ((s->rNToGo == 1) ? 1 : 0)
  138. #define BZ_RAND_UPD_MASK \
  139. if (s->rNToGo == 0) { \
  140. s->rNToGo = BZ2_rNums[s->rTPos]; \
  141. s->rTPos++; \
  142. if (s->rTPos == 512) s->rTPos = 0; \
  143. } \
  144. s->rNToGo--;
  145. /*-- Stuff for doing CRCs. --*/
  146. extern UInt32 BZ2_crc32Table[256];
  147. #define BZ_INITIALISE_CRC(crcVar) \
  148. { \
  149. crcVar = 0xffffffffL; \
  150. }
  151. #define BZ_FINALISE_CRC(crcVar) \
  152. { \
  153. crcVar = ~(crcVar); \
  154. }
  155. #define BZ_UPDATE_CRC(crcVar,cha) \
  156. { \
  157. crcVar = (crcVar << 8) ^ \
  158. BZ2_crc32Table[(crcVar >> 24) ^ \
  159. ((UChar)cha)]; \
  160. }
  161. /*-- States and modes for compression. --*/
  162. #define BZ_M_IDLE 1
  163. #define BZ_M_RUNNING 2
  164. #define BZ_M_FLUSHING 3
  165. #define BZ_M_FINISHING 4
  166. #define BZ_S_OUTPUT 1
  167. #define BZ_S_INPUT 2
  168. #define BZ_N_RADIX 2
  169. #define BZ_N_QSORT 12
  170. #define BZ_N_SHELL 18
  171. #define BZ_N_OVERSHOOT (BZ_N_RADIX + BZ_N_QSORT + BZ_N_SHELL + 2)
  172. /*-- Structure holding all the compression-side stuff. --*/
  173. typedef
  174. struct {
  175. /* pointer back to the struct bz_stream */
  176. bz_stream* strm;
  177. /* mode this stream is in, and whether inputting */
  178. /* or outputting data */
  179. Int32 mode;
  180. Int32 state;
  181. /* remembers avail_in when flush/finish requested */
  182. UInt32 avail_in_expect;
  183. /* for doing the block sorting */
  184. UInt32* arr1;
  185. UInt32* arr2;
  186. UInt32* ftab;
  187. Int32 origPtr;
  188. /* aliases for arr1 and arr2 */
  189. UInt32* ptr;
  190. UChar* block;
  191. UInt16* mtfv;
  192. UChar* zbits;
  193. /* for deciding when to use the fallback sorting algorithm */
  194. Int32 workFactor;
  195. /* run-length-encoding of the input */
  196. UInt32 state_in_ch;
  197. Int32 state_in_len;
  198. BZ_RAND_DECLS;
  199. /* input and output limits and current posns */
  200. Int32 nblock;
  201. Int32 nblockMAX;
  202. Int32 numZ;
  203. Int32 state_out_pos;
  204. /* map of bytes used in block */
  205. Int32 nInUse;
  206. Bool inUse[256];
  207. UChar unseqToSeq[256];
  208. /* the buffer for bit stream creation */
  209. UInt32 bsBuff;
  210. Int32 bsLive;
  211. /* block and combined CRCs */
  212. UInt32 blockCRC;
  213. UInt32 combinedCRC;
  214. /* misc administratium */
  215. Int32 verbosity;
  216. Int32 blockNo;
  217. Int32 blockSize100k;
  218. /* stuff for coding the MTF values */
  219. Int32 nMTF;
  220. Int32 mtfFreq [BZ_MAX_ALPHA_SIZE];
  221. UChar selector [BZ_MAX_SELECTORS];
  222. UChar selectorMtf[BZ_MAX_SELECTORS];
  223. UChar len [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
  224. Int32 code [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
  225. Int32 rfreq [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
  226. /* second dimension: only 3 needed; 4 makes index calculations faster */
  227. UInt32 len_pack[BZ_MAX_ALPHA_SIZE][4];
  228. }
  229. EState;
  230. /*-- externs for compression. --*/
  231. extern void
  232. BZ2_blockSort ( EState* );
  233. extern void
  234. BZ2_compressBlock ( EState*, Bool );
  235. extern void
  236. BZ2_bsInitWrite ( EState* );
  237. extern void
  238. BZ2_hbAssignCodes ( Int32*, UChar*, Int32, Int32, Int32 );
  239. extern void
  240. BZ2_hbMakeCodeLengths ( UChar*, Int32*, Int32, Int32 );
  241. /*-- states for decompression. --*/
  242. #define BZ_X_IDLE 1
  243. #define BZ_X_OUTPUT 2
  244. #define BZ_X_MAGIC_1 10
  245. #define BZ_X_MAGIC_2 11
  246. #define BZ_X_MAGIC_3 12
  247. #define BZ_X_MAGIC_4 13
  248. #define BZ_X_BLKHDR_1 14
  249. #define BZ_X_BLKHDR_2 15
  250. #define BZ_X_BLKHDR_3 16
  251. #define BZ_X_BLKHDR_4 17
  252. #define BZ_X_BLKHDR_5 18
  253. #define BZ_X_BLKHDR_6 19
  254. #define BZ_X_BCRC_1 20
  255. #define BZ_X_BCRC_2 21
  256. #define BZ_X_BCRC_3 22
  257. #define BZ_X_BCRC_4 23
  258. #define BZ_X_RANDBIT 24
  259. #define BZ_X_ORIGPTR_1 25
  260. #define BZ_X_ORIGPTR_2 26
  261. #define BZ_X_ORIGPTR_3 27
  262. #define BZ_X_MAPPING_1 28
  263. #define BZ_X_MAPPING_2 29
  264. #define BZ_X_SELECTOR_1 30
  265. #define BZ_X_SELECTOR_2 31
  266. #define BZ_X_SELECTOR_3 32
  267. #define BZ_X_CODING_1 33
  268. #define BZ_X_CODING_2 34
  269. #define BZ_X_CODING_3 35
  270. #define BZ_X_MTF_1 36
  271. #define BZ_X_MTF_2 37
  272. #define BZ_X_MTF_3 38
  273. #define BZ_X_MTF_4 39
  274. #define BZ_X_MTF_5 40
  275. #define BZ_X_MTF_6 41
  276. #define BZ_X_ENDHDR_2 42
  277. #define BZ_X_ENDHDR_3 43
  278. #define BZ_X_ENDHDR_4 44
  279. #define BZ_X_ENDHDR_5 45
  280. #define BZ_X_ENDHDR_6 46
  281. #define BZ_X_CCRC_1 47
  282. #define BZ_X_CCRC_2 48
  283. #define BZ_X_CCRC_3 49
  284. #define BZ_X_CCRC_4 50
  285. /*-- Constants for the fast MTF decoder. --*/
  286. #define MTFA_SIZE 4096
  287. #define MTFL_SIZE 16
  288. /*-- Structure holding all the decompression-side stuff. --*/
  289. typedef
  290. struct {
  291. /* pointer back to the struct bz_stream */
  292. bz_stream* strm;
  293. /* state indicator for this stream */
  294. Int32 state;
  295. /* for doing the final run-length decoding */
  296. UChar state_out_ch;
  297. Int32 state_out_len;
  298. Bool blockRandomised;
  299. BZ_RAND_DECLS;
  300. /* the buffer for bit stream reading */
  301. UInt32 bsBuff;
  302. Int32 bsLive;
  303. /* misc administratium */
  304. Int32 blockSize100k;
  305. Bool smallDecompress;
  306. Int32 currBlockNo;
  307. Int32 verbosity;
  308. /* for undoing the Burrows-Wheeler transform */
  309. Int32 origPtr;
  310. UInt32 tPos;
  311. Int32 k0;
  312. Int32 unzftab[256];
  313. Int32 nblock_used;
  314. Int32 cftab[257];
  315. Int32 cftabCopy[257];
  316. /* for undoing the Burrows-Wheeler transform (FAST) */
  317. UInt32 *tt;
  318. /* for undoing the Burrows-Wheeler transform (SMALL) */
  319. UInt16 *ll16;
  320. UChar *ll4;
  321. /* stored and calculated CRCs */
  322. UInt32 storedBlockCRC;
  323. UInt32 storedCombinedCRC;
  324. UInt32 calculatedBlockCRC;
  325. UInt32 calculatedCombinedCRC;
  326. /* map of bytes used in block */
  327. Int32 nInUse;
  328. Bool inUse[256];
  329. Bool inUse16[16];
  330. UChar seqToUnseq[256];
  331. /* for decoding the MTF values */
  332. UChar mtfa [MTFA_SIZE];
  333. Int32 mtfbase[256 / MTFL_SIZE];
  334. UChar selector [BZ_MAX_SELECTORS];
  335. UChar selectorMtf[BZ_MAX_SELECTORS];
  336. UChar len [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
  337. Int32 limit [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
  338. Int32 base [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
  339. Int32 perm [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
  340. Int32 minLens[BZ_N_GROUPS];
  341. /* save area for scalars in the main decompress code */
  342. Int32 save_i;
  343. Int32 save_j;
  344. Int32 save_t;
  345. Int32 save_alphaSize;
  346. Int32 save_nGroups;
  347. Int32 save_nSelectors;
  348. Int32 save_EOB;
  349. Int32 save_groupNo;
  350. Int32 save_groupPos;
  351. Int32 save_nextSym;
  352. Int32 save_nblockMAX;
  353. Int32 save_nblock;
  354. Int32 save_es;
  355. Int32 save_N;
  356. Int32 save_curr;
  357. Int32 save_zt;
  358. Int32 save_zn;
  359. Int32 save_zvec;
  360. Int32 save_zj;
  361. Int32 save_gSel;
  362. Int32 save_gMinlen;
  363. Int32* save_gLimit;
  364. Int32* save_gBase;
  365. Int32* save_gPerm;
  366. }
  367. DState;
  368. /*-- Macros for decompression. --*/
  369. #define BZ_GET_FAST(cccc) \
  370. s->tPos = s->tt[s->tPos]; \
  371. cccc = (UChar)(s->tPos & 0xff); \
  372. s->tPos >>= 8;
  373. #define BZ_GET_FAST_C(cccc) \
  374. c_tPos = c_tt[c_tPos]; \
  375. cccc = (UChar)(c_tPos & 0xff); \
  376. c_tPos >>= 8;
  377. #define SET_LL4(i,n) \
  378. { if (((i) & 0x1) == 0) \
  379. s->ll4[(i) >> 1] = (s->ll4[(i) >> 1] & 0xf0) | (n); else \
  380. s->ll4[(i) >> 1] = (s->ll4[(i) >> 1] & 0x0f) | ((n) << 4); \
  381. }
  382. #define GET_LL4(i) \
  383. ((((UInt32)(s->ll4[(i) >> 1])) >> (((i) << 2) & 0x4)) & 0xF)
  384. #define SET_LL(i,n) \
  385. { s->ll16[i] = (UInt16)(n & 0x0000ffff); \
  386. SET_LL4(i, n >> 16); \
  387. }
  388. #define GET_LL(i) \
  389. (((UInt32)s->ll16[i]) | (GET_LL4(i) << 16))
  390. #define BZ_GET_SMALL(cccc) \
  391. cccc = BZ2_indexIntoF ( s->tPos, s->cftab ); \
  392. s->tPos = GET_LL(s->tPos);
  393. /*-- externs for decompression. --*/
  394. extern Int32
  395. BZ2_indexIntoF ( Int32, Int32* );
  396. extern Int32
  397. BZ2_decompress ( DState* );
  398. extern void
  399. BZ2_hbCreateDecodeTables ( Int32*, Int32*, Int32*, UChar*,
  400. Int32, Int32, Int32 );
  401. #endif
  402. /*-- BZ_NO_STDIO seems to make NULL disappear on some platforms. --*/
  403. #ifdef BZ_NO_STDIO
  404. #ifndef NULL
  405. #define NULL 0
  406. #endif
  407. #endif
  408. /*-------------------------------------------------------------*/
  409. /*--- end bzlib_private.h ---*/
  410. /*-------------------------------------------------------------*/