3
0

bzlib_private.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * bzip2 is written by Julian Seward <jseward@bzip.org>.
  3. * Adapted for busybox by Denys Vlasenko <vda.linux@googlemail.com>.
  4. * See README and LICENSE files in this directory for more information.
  5. */
  6. /*-------------------------------------------------------------*/
  7. /*--- Private header file for the library. ---*/
  8. /*--- bzlib_private.h ---*/
  9. /*-------------------------------------------------------------*/
  10. /* ------------------------------------------------------------------
  11. This file is part of bzip2/libbzip2, a program and library for
  12. lossless, block-sorting data compression.
  13. bzip2/libbzip2 version 1.0.4 of 20 December 2006
  14. Copyright (C) 1996-2006 Julian Seward <jseward@bzip.org>
  15. Please read the WARNING, DISCLAIMER and PATENTS sections in the
  16. README file.
  17. This program is released under the terms of the license contained
  18. in the file LICENSE.
  19. ------------------------------------------------------------------ */
  20. /* #include "bzlib.h" */
  21. /*-- General stuff. --*/
  22. typedef unsigned char Bool;
  23. #define True ((Bool)1)
  24. #define False ((Bool)0)
  25. #if BZ_LIGHT_DEBUG
  26. static void bz_assert_fail(int errcode) NORETURN;
  27. #define AssertH(cond, errcode) \
  28. do { \
  29. if (!(cond)) \
  30. bz_assert_fail(errcode); \
  31. } while (0)
  32. #else
  33. #define AssertH(cond, msg) do { } while (0)
  34. #endif
  35. #if BZ_DEBUG
  36. #define AssertD(cond, msg) \
  37. do { \
  38. if (!(cond)) \
  39. bb_error_msg_and_die("(debug build): internal error %s", msg); \
  40. } while (0)
  41. #else
  42. #define AssertD(cond, msg) do { } while (0)
  43. #endif
  44. /*-- Header bytes. --*/
  45. #define BZ_HDR_B 0x42 /* 'B' */
  46. #define BZ_HDR_Z 0x5a /* 'Z' */
  47. #define BZ_HDR_h 0x68 /* 'h' */
  48. #define BZ_HDR_0 0x30 /* '0' */
  49. #define BZ_HDR_BZh0 0x425a6830
  50. /*-- Constants for the back end. --*/
  51. #define BZ_MAX_ALPHA_SIZE 258
  52. #define BZ_MAX_CODE_LEN 23
  53. #define BZ_RUNA 0
  54. #define BZ_RUNB 1
  55. #define BZ_N_GROUPS 6
  56. #define BZ_G_SIZE 50
  57. #define BZ_N_ITERS 4
  58. #define BZ_MAX_SELECTORS (2 + (900000 / BZ_G_SIZE))
  59. /*-- Stuff for doing CRCs. --*/
  60. #define BZ_INITIALISE_CRC(crcVar) \
  61. { \
  62. crcVar = 0xffffffffL; \
  63. }
  64. #define BZ_FINALISE_CRC(crcVar) \
  65. { \
  66. crcVar = ~(crcVar); \
  67. }
  68. #define BZ_UPDATE_CRC(s, crcVar, cha) \
  69. { \
  70. crcVar = (crcVar << 8) ^ s->crc32table[(crcVar >> 24) ^ ((uint8_t)cha)]; \
  71. }
  72. /*-- States and modes for compression. --*/
  73. #define BZ_M_IDLE 1
  74. #define BZ_M_RUNNING 2
  75. #define BZ_M_FLUSHING 3
  76. #define BZ_M_FINISHING 4
  77. #define BZ_S_OUTPUT 1
  78. #define BZ_S_INPUT 2
  79. #define BZ_N_RADIX 2
  80. #define BZ_N_QSORT 12
  81. #define BZ_N_SHELL 18
  82. #define BZ_N_OVERSHOOT (BZ_N_RADIX + BZ_N_QSORT + BZ_N_SHELL + 2)
  83. /*-- Structure holding all the compression-side stuff. --*/
  84. typedef struct EState {
  85. /* pointer back to the struct bz_stream */
  86. bz_stream *strm;
  87. /* mode this stream is in, and whether inputting */
  88. /* or outputting data */
  89. uint8_t mode;
  90. uint8_t state;
  91. /* misc administratium */
  92. uint8_t blockSize100k;
  93. /* remembers avail_in when flush/finish requested */
  94. /* bbox: not needed, strm->avail_in always has the same value */
  95. /* commented out with '//#' throughout the code */
  96. /* uint32_t avail_in_expect; */
  97. /* for doing the block sorting */
  98. uint32_t *arr1;
  99. uint32_t *arr2;
  100. //uint32_t *ftab; //moved into this struct, see below
  101. uint16_t *quadrant;
  102. int32_t budget;
  103. /* aliases for arr1 and arr2 */
  104. uint32_t *ptr;
  105. uint8_t *block;
  106. uint16_t *mtfv;
  107. uint8_t *zbits;
  108. /* run-length-encoding of the input */
  109. uint32_t state_in_ch;
  110. int32_t state_in_len;
  111. /* input and output limits and current posns */
  112. int32_t nblock;
  113. int32_t nblockMAX;
  114. //int32_t numZ; // index into s->zbits[], replaced by pointer:
  115. uint8_t *posZ;
  116. uint8_t *state_out_pos;
  117. /* the buffer for bit stream creation */
  118. uint32_t bsBuff;
  119. int32_t bsLive;
  120. /* block and combined CRCs */
  121. uint32_t blockCRC;
  122. uint32_t combinedCRC;
  123. /* misc administratium */
  124. int32_t blockNo;
  125. /* stuff for coding the MTF values */
  126. int32_t nMTF;
  127. /* map of bytes used in block */
  128. int32_t nInUse;
  129. Bool inUse[256] ALIGNED(sizeof(long));
  130. uint8_t unseqToSeq[256];
  131. /* stuff for coding the MTF values */
  132. int32_t mtfFreq [BZ_MAX_ALPHA_SIZE];
  133. uint8_t selector [BZ_MAX_SELECTORS];
  134. uint8_t selectorMtf[BZ_MAX_SELECTORS];
  135. uint8_t len[BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
  136. /* guess what */
  137. uint32_t crc32table[256];
  138. /* for doing the block sorting */
  139. uint32_t ftab[65537];
  140. /* stack-saving measures: these can be local, but they are too big */
  141. int32_t sendMTFValues__code [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
  142. int32_t sendMTFValues__rfreq[BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
  143. #if BZIP2_SPEED >= 5
  144. /* second dimension: only 3 needed; 4 makes index calculations faster */
  145. uint32_t sendMTFValues__len_pack[BZ_MAX_ALPHA_SIZE][4];
  146. #endif
  147. int32_t BZ2_hbMakeCodeLengths__heap [BZ_MAX_ALPHA_SIZE + 2];
  148. int32_t BZ2_hbMakeCodeLengths__weight[BZ_MAX_ALPHA_SIZE * 2];
  149. int32_t BZ2_hbMakeCodeLengths__parent[BZ_MAX_ALPHA_SIZE * 2];
  150. int32_t mainSort__copyStart[256];
  151. int32_t mainSort__copyEnd[256];
  152. } EState;
  153. /*-- compression. --*/
  154. static int32_t
  155. BZ2_blockSort(EState*);
  156. static void
  157. BZ2_compressBlock(EState*, int);
  158. static void
  159. BZ2_bsInitWrite(EState*);
  160. static void
  161. BZ2_hbAssignCodes(int32_t*, uint8_t*, int32_t, int32_t, int32_t);
  162. static void
  163. BZ2_hbMakeCodeLengths(EState*, uint8_t*, int32_t*, int32_t, int32_t);
  164. /*-------------------------------------------------------------*/
  165. /*--- end bzlib_private.h ---*/
  166. /*-------------------------------------------------------------*/