defutil.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. #ifndef DEFUTIL_H
  2. #define DEFUTIL_H
  3. #include <linux/zutil.h>
  4. #define Assert(err, str)
  5. #define Trace(dummy)
  6. #define Tracev(dummy)
  7. #define Tracecv(err, dummy)
  8. #define Tracevv(dummy)
  9. #define LENGTH_CODES 29
  10. /* number of length codes, not counting the special END_BLOCK code */
  11. #define LITERALS 256
  12. /* number of literal bytes 0..255 */
  13. #define L_CODES (LITERALS+1+LENGTH_CODES)
  14. /* number of Literal or Length codes, including the END_BLOCK code */
  15. #define D_CODES 30
  16. /* number of distance codes */
  17. #define BL_CODES 19
  18. /* number of codes used to transfer the bit lengths */
  19. #define HEAP_SIZE (2*L_CODES+1)
  20. /* maximum heap size */
  21. #define MAX_BITS 15
  22. /* All codes must not exceed MAX_BITS bits */
  23. #define INIT_STATE 42
  24. #define BUSY_STATE 113
  25. #define FINISH_STATE 666
  26. /* Stream status */
  27. /* Data structure describing a single value and its code string. */
  28. typedef struct ct_data_s {
  29. union {
  30. ush freq; /* frequency count */
  31. ush code; /* bit string */
  32. } fc;
  33. union {
  34. ush dad; /* father node in Huffman tree */
  35. ush len; /* length of bit string */
  36. } dl;
  37. } ct_data;
  38. #define Freq fc.freq
  39. #define Code fc.code
  40. #define Dad dl.dad
  41. #define Len dl.len
  42. typedef struct static_tree_desc_s static_tree_desc;
  43. typedef struct tree_desc_s {
  44. ct_data *dyn_tree; /* the dynamic tree */
  45. int max_code; /* largest code with non zero frequency */
  46. static_tree_desc *stat_desc; /* the corresponding static tree */
  47. } tree_desc;
  48. typedef ush Pos;
  49. typedef unsigned IPos;
  50. /* A Pos is an index in the character window. We use short instead of int to
  51. * save space in the various tables. IPos is used only for parameter passing.
  52. */
  53. typedef struct deflate_state {
  54. z_streamp strm; /* pointer back to this zlib stream */
  55. int status; /* as the name implies */
  56. Byte *pending_buf; /* output still pending */
  57. ulg pending_buf_size; /* size of pending_buf */
  58. Byte *pending_out; /* next pending byte to output to the stream */
  59. int pending; /* nb of bytes in the pending buffer */
  60. int noheader; /* suppress zlib header and adler32 */
  61. Byte data_type; /* UNKNOWN, BINARY or ASCII */
  62. Byte method; /* STORED (for zip only) or DEFLATED */
  63. int last_flush; /* value of flush param for previous deflate call */
  64. /* used by deflate.c: */
  65. uInt w_size; /* LZ77 window size (32K by default) */
  66. uInt w_bits; /* log2(w_size) (8..16) */
  67. uInt w_mask; /* w_size - 1 */
  68. Byte *window;
  69. /* Sliding window. Input bytes are read into the second half of the window,
  70. * and move to the first half later to keep a dictionary of at least wSize
  71. * bytes. With this organization, matches are limited to a distance of
  72. * wSize-MAX_MATCH bytes, but this ensures that IO is always
  73. * performed with a length multiple of the block size. Also, it limits
  74. * the window size to 64K, which is quite useful on MSDOS.
  75. * To do: use the user input buffer as sliding window.
  76. */
  77. ulg window_size;
  78. /* Actual size of window: 2*wSize, except when the user input buffer
  79. * is directly used as sliding window.
  80. */
  81. Pos *prev;
  82. /* Link to older string with same hash index. To limit the size of this
  83. * array to 64K, this link is maintained only for the last 32K strings.
  84. * An index in this array is thus a window index modulo 32K.
  85. */
  86. Pos *head; /* Heads of the hash chains or NIL. */
  87. uInt ins_h; /* hash index of string to be inserted */
  88. uInt hash_size; /* number of elements in hash table */
  89. uInt hash_bits; /* log2(hash_size) */
  90. uInt hash_mask; /* hash_size-1 */
  91. uInt hash_shift;
  92. /* Number of bits by which ins_h must be shifted at each input
  93. * step. It must be such that after MIN_MATCH steps, the oldest
  94. * byte no longer takes part in the hash key, that is:
  95. * hash_shift * MIN_MATCH >= hash_bits
  96. */
  97. long block_start;
  98. /* Window position at the beginning of the current output block. Gets
  99. * negative when the window is moved backwards.
  100. */
  101. uInt match_length; /* length of best match */
  102. IPos prev_match; /* previous match */
  103. int match_available; /* set if previous match exists */
  104. uInt strstart; /* start of string to insert */
  105. uInt match_start; /* start of matching string */
  106. uInt lookahead; /* number of valid bytes ahead in window */
  107. uInt prev_length;
  108. /* Length of the best match at previous step. Matches not greater than this
  109. * are discarded. This is used in the lazy match evaluation.
  110. */
  111. uInt max_chain_length;
  112. /* To speed up deflation, hash chains are never searched beyond this
  113. * length. A higher limit improves compression ratio but degrades the
  114. * speed.
  115. */
  116. uInt max_lazy_match;
  117. /* Attempt to find a better match only when the current match is strictly
  118. * smaller than this value. This mechanism is used only for compression
  119. * levels >= 4.
  120. */
  121. # define max_insert_length max_lazy_match
  122. /* Insert new strings in the hash table only if the match length is not
  123. * greater than this length. This saves time but degrades compression.
  124. * max_insert_length is used only for compression levels <= 3.
  125. */
  126. int level; /* compression level (1..9) */
  127. int strategy; /* favor or force Huffman coding*/
  128. uInt good_match;
  129. /* Use a faster search when the previous match is longer than this */
  130. int nice_match; /* Stop searching when current match exceeds this */
  131. /* used by trees.c: */
  132. /* Didn't use ct_data typedef below to suppress compiler warning */
  133. struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */
  134. struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
  135. struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */
  136. struct tree_desc_s l_desc; /* desc. for literal tree */
  137. struct tree_desc_s d_desc; /* desc. for distance tree */
  138. struct tree_desc_s bl_desc; /* desc. for bit length tree */
  139. ush bl_count[MAX_BITS+1];
  140. /* number of codes at each bit length for an optimal tree */
  141. int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */
  142. int heap_len; /* number of elements in the heap */
  143. int heap_max; /* element of largest frequency */
  144. /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
  145. * The same heap array is used to build all trees.
  146. */
  147. uch depth[2*L_CODES+1];
  148. /* Depth of each subtree used as tie breaker for trees of equal frequency
  149. */
  150. uch *l_buf; /* buffer for literals or lengths */
  151. uInt lit_bufsize;
  152. /* Size of match buffer for literals/lengths. There are 4 reasons for
  153. * limiting lit_bufsize to 64K:
  154. * - frequencies can be kept in 16 bit counters
  155. * - if compression is not successful for the first block, all input
  156. * data is still in the window so we can still emit a stored block even
  157. * when input comes from standard input. (This can also be done for
  158. * all blocks if lit_bufsize is not greater than 32K.)
  159. * - if compression is not successful for a file smaller than 64K, we can
  160. * even emit a stored file instead of a stored block (saving 5 bytes).
  161. * This is applicable only for zip (not gzip or zlib).
  162. * - creating new Huffman trees less frequently may not provide fast
  163. * adaptation to changes in the input data statistics. (Take for
  164. * example a binary file with poorly compressible code followed by
  165. * a highly compressible string table.) Smaller buffer sizes give
  166. * fast adaptation but have of course the overhead of transmitting
  167. * trees more frequently.
  168. * - I can't count above 4
  169. */
  170. uInt last_lit; /* running index in l_buf */
  171. ush *d_buf;
  172. /* Buffer for distances. To simplify the code, d_buf and l_buf have
  173. * the same number of elements. To use different lengths, an extra flag
  174. * array would be necessary.
  175. */
  176. ulg opt_len; /* bit length of current block with optimal trees */
  177. ulg static_len; /* bit length of current block with static trees */
  178. ulg compressed_len; /* total bit length of compressed file */
  179. uInt matches; /* number of string matches in current block */
  180. int last_eob_len; /* bit length of EOB code for last block */
  181. #ifdef DEBUG_ZLIB
  182. ulg bits_sent; /* bit length of the compressed data */
  183. #endif
  184. ush bi_buf;
  185. /* Output buffer. bits are inserted starting at the bottom (least
  186. * significant bits).
  187. */
  188. int bi_valid;
  189. /* Number of valid bits in bi_buf. All bits above the last valid bit
  190. * are always zero.
  191. */
  192. } deflate_state;
  193. #ifdef CONFIG_ZLIB_DFLTCC
  194. #define zlib_deflate_window_memsize(windowBits) \
  195. (2 * (1 << (windowBits)) * sizeof(Byte) + PAGE_SIZE)
  196. #else
  197. #define zlib_deflate_window_memsize(windowBits) \
  198. (2 * (1 << (windowBits)) * sizeof(Byte))
  199. #endif
  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(
  241. unsigned code, /* the value to invert */
  242. int len /* its bit length */
  243. )
  244. {
  245. register unsigned res = 0;
  246. do {
  247. res |= code & 1;
  248. code >>= 1, res <<= 1;
  249. } while (--len > 0);
  250. return res >> 1;
  251. }
  252. /* ===========================================================================
  253. * Flush the bit buffer, keeping at most 7 bits in it.
  254. */
  255. static inline void bi_flush(deflate_state *s)
  256. {
  257. if (s->bi_valid == 16) {
  258. put_short(s, s->bi_buf);
  259. s->bi_buf = 0;
  260. s->bi_valid = 0;
  261. } else if (s->bi_valid >= 8) {
  262. put_byte(s, (Byte)s->bi_buf);
  263. s->bi_buf >>= 8;
  264. s->bi_valid -= 8;
  265. }
  266. }
  267. /* ===========================================================================
  268. * Flush the bit buffer and align the output on a byte boundary
  269. */
  270. static inline void bi_windup(deflate_state *s)
  271. {
  272. if (s->bi_valid > 8) {
  273. put_short(s, s->bi_buf);
  274. } else if (s->bi_valid > 0) {
  275. put_byte(s, (Byte)s->bi_buf);
  276. }
  277. s->bi_buf = 0;
  278. s->bi_valid = 0;
  279. #ifdef DEBUG_ZLIB
  280. s->bits_sent = (s->bits_sent+7) & ~7;
  281. #endif
  282. }
  283. typedef enum {
  284. need_more, /* block not completed, need more input or more output */
  285. block_done, /* block flush performed */
  286. finish_started, /* finish started, need only more output at next deflate */
  287. finish_done /* finish done, accept no more input or output */
  288. } block_state;
  289. #define Buf_size (8 * 2*sizeof(char))
  290. /* Number of bits used within bi_buf. (bi_buf might be implemented on
  291. * more than 16 bits on some systems.)
  292. */
  293. /* ===========================================================================
  294. * Send a value on a given number of bits.
  295. * IN assertion: length <= 16 and value fits in length bits.
  296. */
  297. #ifdef DEBUG_ZLIB
  298. static void send_bits (deflate_state *s, int value, int length);
  299. static void send_bits(
  300. deflate_state *s,
  301. int value, /* value to send */
  302. int length /* number of bits */
  303. )
  304. {
  305. Tracevv((stderr," l %2d v %4x ", length, value));
  306. Assert(length > 0 && length <= 15, "invalid length");
  307. s->bits_sent += (ulg)length;
  308. /* If not enough room in bi_buf, use (valid) bits from bi_buf and
  309. * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
  310. * unused bits in value.
  311. */
  312. if (s->bi_valid > (int)Buf_size - length) {
  313. s->bi_buf |= (value << s->bi_valid);
  314. put_short(s, s->bi_buf);
  315. s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);
  316. s->bi_valid += length - Buf_size;
  317. } else {
  318. s->bi_buf |= value << s->bi_valid;
  319. s->bi_valid += length;
  320. }
  321. }
  322. #else /* !DEBUG_ZLIB */
  323. #define send_bits(s, value, length) \
  324. { int len = length;\
  325. if (s->bi_valid > (int)Buf_size - len) {\
  326. int val = value;\
  327. s->bi_buf |= (val << s->bi_valid);\
  328. put_short(s, s->bi_buf);\
  329. s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
  330. s->bi_valid += len - Buf_size;\
  331. } else {\
  332. s->bi_buf |= (value) << s->bi_valid;\
  333. s->bi_valid += len;\
  334. }\
  335. }
  336. #endif /* DEBUG_ZLIB */
  337. static inline void zlib_tr_send_bits(
  338. deflate_state *s,
  339. int value,
  340. int length
  341. )
  342. {
  343. send_bits(s, value, length);
  344. }
  345. /* =========================================================================
  346. * Flush as much pending output as possible. All deflate() output goes
  347. * through this function so some applications may wish to modify it
  348. * to avoid allocating a large strm->next_out buffer and copying into it.
  349. * (See also read_buf()).
  350. */
  351. static inline void flush_pending(
  352. z_streamp strm
  353. )
  354. {
  355. deflate_state *s = (deflate_state *) strm->state;
  356. unsigned len = s->pending;
  357. if (len > strm->avail_out) len = strm->avail_out;
  358. if (len == 0) return;
  359. if (strm->next_out != NULL) {
  360. memcpy(strm->next_out, s->pending_out, len);
  361. strm->next_out += len;
  362. }
  363. s->pending_out += len;
  364. strm->total_out += len;
  365. strm->avail_out -= len;
  366. s->pending -= len;
  367. if (s->pending == 0) {
  368. s->pending_out = s->pending_buf;
  369. }
  370. }
  371. #endif /* DEFUTIL_H */