Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

558 lines
15 KiB

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