3
0

bzlib_private.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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) ATTRIBUTE_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. int32_t mode;
  90. int32_t state;
  91. /* remembers avail_in when flush/finish requested */
  92. /* bbox: not needed, strm->avail_in always has the same value */
  93. /* commented out with '//#' throughout the code */
  94. /* uint32_t avail_in_expect; */
  95. /* for doing the block sorting */
  96. int32_t origPtr;
  97. uint32_t *arr1;
  98. uint32_t *arr2;
  99. uint32_t *ftab;
  100. /* aliases for arr1 and arr2 */
  101. uint32_t *ptr;
  102. uint8_t *block;
  103. uint16_t *mtfv;
  104. uint8_t *zbits;
  105. /* guess what */
  106. uint32_t *crc32table;
  107. /* run-length-encoding of the input */
  108. uint32_t state_in_ch;
  109. int32_t state_in_len;
  110. /* input and output limits and current posns */
  111. int32_t nblock;
  112. int32_t nblockMAX;
  113. int32_t numZ;
  114. int32_t state_out_pos;
  115. /* the buffer for bit stream creation */
  116. uint32_t bsBuff;
  117. int32_t bsLive;
  118. /* block and combined CRCs */
  119. uint32_t blockCRC;
  120. uint32_t combinedCRC;
  121. /* misc administratium */
  122. int32_t blockNo;
  123. int32_t blockSize100k;
  124. /* stuff for coding the MTF values */
  125. int32_t nMTF;
  126. /* map of bytes used in block */
  127. int32_t nInUse;
  128. Bool inUse[256] __attribute__(( aligned(sizeof(long)) ));
  129. uint8_t unseqToSeq[256];
  130. /* stuff for coding the MTF values */
  131. int32_t mtfFreq [BZ_MAX_ALPHA_SIZE];
  132. uint8_t selector [BZ_MAX_SELECTORS];
  133. uint8_t selectorMtf[BZ_MAX_SELECTORS];
  134. uint8_t len [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
  135. int32_t code [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
  136. int32_t rfreq[BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
  137. #if CONFIG_BZIP2_FEATURE_SPEED >= 5
  138. /* second dimension: only 3 needed; 4 makes index calculations faster */
  139. uint32_t len_pack[BZ_MAX_ALPHA_SIZE][4];
  140. #endif
  141. } EState;
  142. /*-- compression. --*/
  143. static void
  144. BZ2_blockSort(EState*);
  145. static void
  146. BZ2_compressBlock(EState*, int);
  147. static void
  148. BZ2_bsInitWrite(EState*);
  149. static void
  150. BZ2_hbAssignCodes(int32_t*, uint8_t*, int32_t, int32_t, int32_t);
  151. static void
  152. BZ2_hbMakeCodeLengths(uint8_t*, int32_t*, int32_t, int32_t);
  153. /*-------------------------------------------------------------*/
  154. /*--- end bzlib_private.h ---*/
  155. /*-------------------------------------------------------------*/