bzlib_private.h 13 KB

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