defutil.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. #define Assert(err, str)
  2. #define Trace(dummy)
  3. #define Tracev(dummy)
  4. #define Tracecv(err, dummy)
  5. #define Tracevv(dummy)
  6. #define LENGTH_CODES 29
  7. /* number of length codes, not counting the special END_BLOCK code */
  8. #define LITERALS 256
  9. /* number of literal bytes 0..255 */
  10. #define L_CODES (LITERALS+1+LENGTH_CODES)
  11. /* number of Literal or Length codes, including the END_BLOCK code */
  12. #define D_CODES 30
  13. /* number of distance codes */
  14. #define BL_CODES 19
  15. /* number of codes used to transfer the bit lengths */
  16. #define HEAP_SIZE (2*L_CODES+1)
  17. /* maximum heap size */
  18. #define MAX_BITS 15
  19. /* All codes must not exceed MAX_BITS bits */
  20. #define INIT_STATE 42
  21. #define BUSY_STATE 113
  22. #define FINISH_STATE 666
  23. /* Stream status */
  24. /* Data structure describing a single value and its code string. */
  25. typedef struct ct_data_s {
  26. union {
  27. ush freq; /* frequency count */
  28. ush code; /* bit string */
  29. } fc;
  30. union {
  31. ush dad; /* father node in Huffman tree */
  32. ush len; /* length of bit string */
  33. } dl;
  34. } ct_data;
  35. #define Freq fc.freq
  36. #define Code fc.code
  37. #define Dad dl.dad
  38. #define Len dl.len
  39. typedef struct static_tree_desc_s static_tree_desc;
  40. typedef struct tree_desc_s {
  41. ct_data *dyn_tree; /* the dynamic tree */
  42. int max_code; /* largest code with non zero frequency */
  43. static_tree_desc *stat_desc; /* the corresponding static tree */
  44. } tree_desc;
  45. typedef ush Pos;
  46. typedef unsigned IPos;
  47. /* A Pos is an index in the character window. We use short instead of int to
  48. * save space in the various tables. IPos is used only for parameter passing.
  49. */
  50. typedef struct deflate_state {
  51. z_streamp strm; /* pointer back to this zlib stream */
  52. int status; /* as the name implies */
  53. Byte *pending_buf; /* output still pending */
  54. ulg pending_buf_size; /* size of pending_buf */
  55. Byte *pending_out; /* next pending byte to output to the stream */
  56. int pending; /* nb of bytes in the pending buffer */
  57. int noheader; /* suppress zlib header and adler32 */
  58. Byte data_type; /* UNKNOWN, BINARY or ASCII */
  59. Byte method; /* STORED (for zip only) or DEFLATED */
  60. int last_flush; /* value of flush param for previous deflate call */
  61. /* used by deflate.c: */
  62. uInt w_size; /* LZ77 window size (32K by default) */
  63. uInt w_bits; /* log2(w_size) (8..16) */
  64. uInt w_mask; /* w_size - 1 */
  65. Byte *window;
  66. /* Sliding window. Input bytes are read into the second half of the window,
  67. * and move to the first half later to keep a dictionary of at least wSize
  68. * bytes. With this organization, matches are limited to a distance of
  69. * wSize-MAX_MATCH bytes, but this ensures that IO is always
  70. * performed with a length multiple of the block size. Also, it limits
  71. * the window size to 64K, which is quite useful on MSDOS.
  72. * To do: use the user input buffer as sliding window.
  73. */
  74. ulg window_size;
  75. /* Actual size of window: 2*wSize, except when the user input buffer
  76. * is directly used as sliding window.
  77. */
  78. Pos *prev;
  79. /* Link to older string with same hash index. To limit the size of this
  80. * array to 64K, this link is maintained only for the last 32K strings.
  81. * An index in this array is thus a window index modulo 32K.
  82. */
  83. Pos *head; /* Heads of the hash chains or NIL. */
  84. uInt ins_h; /* hash index of string to be inserted */
  85. uInt hash_size; /* number of elements in hash table */
  86. uInt hash_bits; /* log2(hash_size) */
  87. uInt hash_mask; /* hash_size-1 */
  88. uInt hash_shift;
  89. /* Number of bits by which ins_h must be shifted at each input
  90. * step. It must be such that after MIN_MATCH steps, the oldest
  91. * byte no longer takes part in the hash key, that is:
  92. * hash_shift * MIN_MATCH >= hash_bits
  93. */
  94. long block_start;
  95. /* Window position at the beginning of the current output block. Gets
  96. * negative when the window is moved backwards.
  97. */
  98. uInt match_length; /* length of best match */
  99. IPos prev_match; /* previous match */
  100. int match_available; /* set if previous match exists */
  101. uInt strstart; /* start of string to insert */
  102. uInt match_start; /* start of matching string */
  103. uInt lookahead; /* number of valid bytes ahead in window */
  104. uInt prev_length;
  105. /* Length of the best match at previous step. Matches not greater than this
  106. * are discarded. This is used in the lazy match evaluation.
  107. */
  108. uInt max_chain_length;
  109. /* To speed up deflation, hash chains are never searched beyond this
  110. * length. A higher limit improves compression ratio but degrades the
  111. * speed.
  112. */
  113. uInt max_lazy_match;
  114. /* Attempt to find a better match only when the current match is strictly
  115. * smaller than this value. This mechanism is used only for compression
  116. * levels >= 4.
  117. */
  118. # define max_insert_length max_lazy_match
  119. /* Insert new strings in the hash table only if the match length is not
  120. * greater than this length. This saves time but degrades compression.
  121. * max_insert_length is used only for compression levels <= 3.
  122. */
  123. int level; /* compression level (1..9) */
  124. int strategy; /* favor or force Huffman coding*/
  125. uInt good_match;
  126. /* Use a faster search when the previous match is longer than this */
  127. int nice_match; /* Stop searching when current match exceeds this */
  128. /* used by trees.c: */
  129. /* Didn't use ct_data typedef below to suppress compiler warning */
  130. struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */
  131. struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
  132. struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */
  133. struct tree_desc_s l_desc; /* desc. for literal tree */
  134. struct tree_desc_s d_desc; /* desc. for distance tree */
  135. struct tree_desc_s bl_desc; /* desc. for bit length tree */
  136. ush bl_count[MAX_BITS+1];
  137. /* number of codes at each bit length for an optimal tree */
  138. int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */
  139. int heap_len; /* number of elements in the heap */
  140. int heap_max; /* element of largest frequency */
  141. /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
  142. * The same heap array is used to build all trees.
  143. */
  144. uch depth[2*L_CODES+1];
  145. /* Depth of each subtree used as tie breaker for trees of equal frequency
  146. */
  147. uch *l_buf; /* buffer for literals or lengths */
  148. uInt lit_bufsize;
  149. /* Size of match buffer for literals/lengths. There are 4 reasons for
  150. * limiting lit_bufsize to 64K:
  151. * - frequencies can be kept in 16 bit counters
  152. * - if compression is not successful for the first block, all input
  153. * data is still in the window so we can still emit a stored block even
  154. * when input comes from standard input. (This can also be done for
  155. * all blocks if lit_bufsize is not greater than 32K.)
  156. * - if compression is not successful for a file smaller than 64K, we can
  157. * even emit a stored file instead of a stored block (saving 5 bytes).
  158. * This is applicable only for zip (not gzip or zlib).
  159. * - creating new Huffman trees less frequently may not provide fast
  160. * adaptation to changes in the input data statistics. (Take for
  161. * example a binary file with poorly compressible code followed by
  162. * a highly compressible string table.) Smaller buffer sizes give
  163. * fast adaptation but have of course the overhead of transmitting
  164. * trees more frequently.
  165. * - I can't count above 4
  166. */
  167. uInt last_lit; /* running index in l_buf */
  168. ush *d_buf;
  169. /* Buffer for distances. To simplify the code, d_buf and l_buf have
  170. * the same number of elements. To use different lengths, an extra flag
  171. * array would be necessary.
  172. */
  173. ulg opt_len; /* bit length of current block with optimal trees */
  174. ulg static_len; /* bit length of current block with static trees */
  175. ulg compressed_len; /* total bit length of compressed file */
  176. uInt matches; /* number of string matches in current block */
  177. int last_eob_len; /* bit length of EOB code for last block */
  178. #ifdef DEBUG_ZLIB
  179. ulg bits_sent; /* bit length of the compressed data */
  180. #endif
  181. ush bi_buf;
  182. /* Output buffer. bits are inserted starting at the bottom (least
  183. * significant bits).
  184. */
  185. int bi_valid;
  186. /* Number of valid bits in bi_buf. All bits above the last valid bit
  187. * are always zero.
  188. */
  189. } deflate_state;
  190. typedef struct deflate_workspace {
  191. /* State memory for the deflator */
  192. deflate_state deflate_memory;
  193. Byte *window_memory;
  194. Pos *prev_memory;
  195. Pos *head_memory;
  196. char *overlay_memory;
  197. } deflate_workspace;
  198. #define zlib_deflate_window_memsize(windowBits) \
  199. (2 * (1 << (windowBits)) * sizeof(Byte))
  200. #define zlib_deflate_prev_memsize(windowBits) \
  201. ((1 << (windowBits)) * sizeof(Pos))
  202. #define zlib_deflate_head_memsize(memLevel) \
  203. ((1 << ((memLevel)+7)) * sizeof(Pos))
  204. #define zlib_deflate_overlay_memsize(memLevel) \
  205. ((1 << ((memLevel)+6)) * (sizeof(ush)+2))
  206. /* Output a byte on the stream.
  207. * IN assertion: there is enough room in pending_buf.
  208. */
  209. #define put_byte(s, c) {s->pending_buf[s->pending++] = (c);}
  210. #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
  211. /* Minimum amount of lookahead, except at the end of the input file.
  212. * See deflate.c for comments about the MIN_MATCH+1.
  213. */
  214. #define MAX_DIST(s) ((s)->w_size-MIN_LOOKAHEAD)
  215. /* In order to simplify the code, particularly on 16 bit machines, match
  216. * distances are limited to MAX_DIST instead of WSIZE.
  217. */
  218. /* in trees.c */
  219. void zlib_tr_init (deflate_state *s);
  220. int zlib_tr_tally (deflate_state *s, unsigned dist, unsigned lc);
  221. ulg zlib_tr_flush_block (deflate_state *s, char *buf, ulg stored_len,
  222. int eof);
  223. void zlib_tr_align (deflate_state *s);
  224. void zlib_tr_stored_block (deflate_state *s, char *buf, ulg stored_len,
  225. int eof);
  226. void zlib_tr_stored_type_only (deflate_state *);
  227. /* ===========================================================================
  228. * Output a short LSB first on the stream.
  229. * IN assertion: there is enough room in pendingBuf.
  230. */
  231. #define put_short(s, w) { \
  232. put_byte(s, (uch)((w) & 0xff)); \
  233. put_byte(s, (uch)((ush)(w) >> 8)); \
  234. }
  235. /* ===========================================================================
  236. * Reverse the first len bits of a code, using straightforward code (a faster
  237. * method would use a table)
  238. * IN assertion: 1 <= len <= 15
  239. */
  240. static inline unsigned bi_reverse(unsigned code, /* the value to invert */
  241. int len) /* its bit length */
  242. {
  243. register unsigned res = 0;
  244. do {
  245. res |= code & 1;
  246. code >>= 1, res <<= 1;
  247. } while (--len > 0);
  248. return res >> 1;
  249. }
  250. /* ===========================================================================
  251. * Flush the bit buffer, keeping at most 7 bits in it.
  252. */
  253. static inline void bi_flush(deflate_state *s)
  254. {
  255. if (s->bi_valid == 16) {
  256. put_short(s, s->bi_buf);
  257. s->bi_buf = 0;
  258. s->bi_valid = 0;
  259. } else if (s->bi_valid >= 8) {
  260. put_byte(s, (Byte)s->bi_buf);
  261. s->bi_buf >>= 8;
  262. s->bi_valid -= 8;
  263. }
  264. }
  265. /* ===========================================================================
  266. * Flush the bit buffer and align the output on a byte boundary
  267. */
  268. static inline void bi_windup(deflate_state *s)
  269. {
  270. if (s->bi_valid > 8) {
  271. put_short(s, s->bi_buf);
  272. } else if (s->bi_valid > 0) {
  273. put_byte(s, (Byte)s->bi_buf);
  274. }
  275. s->bi_buf = 0;
  276. s->bi_valid = 0;
  277. #ifdef DEBUG_ZLIB
  278. s->bits_sent = (s->bits_sent+7) & ~7;
  279. #endif
  280. }