bzlib_private.h 13 KB

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