222c53e0436174b8d5f313f8afdf6a555f22c163
[shibboleth/cpp-opensaml.git] / saml / zlib / deflate.h
1 /* deflate.h -- internal compression state\r
2  * Copyright (C) 1995-2004 Jean-loup Gailly\r
3  * For conditions of distribution and use, see copyright notice in zlib.h\r
4  */\r
5 \r
6 /* WARNING: this file should *not* be used by applications. It is\r
7    part of the implementation of the compression library and is\r
8    subject to change. Applications should only use zlib.h.\r
9  */\r
10 \r
11 /* @(#) $Id$ */\r
12 \r
13 #ifndef DEFLATE_H\r
14 #define DEFLATE_H\r
15 \r
16 #include "zutil.h"\r
17 \r
18 /* define NO_GZIP when compiling if you want to disable gzip header and\r
19    trailer creation by deflate().  NO_GZIP would be used to avoid linking in\r
20    the crc code when it is not needed.  For shared libraries, gzip encoding\r
21    should be left enabled. */\r
22 #ifndef NO_GZIP\r
23 #  define GZIP\r
24 #endif\r
25 \r
26 /* ===========================================================================\r
27  * Internal compression state.\r
28  */\r
29 \r
30 #define LENGTH_CODES 29\r
31 /* number of length codes, not counting the special END_BLOCK code */\r
32 \r
33 #define LITERALS  256\r
34 /* number of literal bytes 0..255 */\r
35 \r
36 #define L_CODES (LITERALS+1+LENGTH_CODES)\r
37 /* number of Literal or Length codes, including the END_BLOCK code */\r
38 \r
39 #define D_CODES   30\r
40 /* number of distance codes */\r
41 \r
42 #define BL_CODES  19\r
43 /* number of codes used to transfer the bit lengths */\r
44 \r
45 #define HEAP_SIZE (2*L_CODES+1)\r
46 /* maximum heap size */\r
47 \r
48 #define MAX_BITS 15\r
49 /* All codes must not exceed MAX_BITS bits */\r
50 \r
51 #define INIT_STATE    42\r
52 #define EXTRA_STATE   69\r
53 #define NAME_STATE    73\r
54 #define COMMENT_STATE 91\r
55 #define HCRC_STATE   103\r
56 #define BUSY_STATE   113\r
57 #define FINISH_STATE 666\r
58 /* Stream status */\r
59 \r
60 \r
61 /* Data structure describing a single value and its code string. */\r
62 typedef struct ct_data_s {\r
63     union {\r
64         ush  freq;       /* frequency count */\r
65         ush  code;       /* bit string */\r
66     } fc;\r
67     union {\r
68         ush  dad;        /* father node in Huffman tree */\r
69         ush  len;        /* length of bit string */\r
70     } dl;\r
71 } FAR ct_data;\r
72 \r
73 #define Freq fc.freq\r
74 #define Code fc.code\r
75 #define Dad  dl.dad\r
76 #define Len  dl.len\r
77 \r
78 typedef struct static_tree_desc_s  static_tree_desc;\r
79 \r
80 typedef struct tree_desc_s {\r
81     ct_data *dyn_tree;           /* the dynamic tree */\r
82     int     max_code;            /* largest code with non zero frequency */\r
83     static_tree_desc *stat_desc; /* the corresponding static tree */\r
84 } FAR tree_desc;\r
85 \r
86 typedef ush Pos;\r
87 typedef Pos FAR Posf;\r
88 typedef unsigned IPos;\r
89 \r
90 /* A Pos is an index in the character window. We use short instead of int to\r
91  * save space in the various tables. IPos is used only for parameter passing.\r
92  */\r
93 \r
94 typedef struct internal_state {\r
95     z_streamp strm;      /* pointer back to this zlib stream */\r
96     int   status;        /* as the name implies */\r
97     Bytef *pending_buf;  /* output still pending */\r
98     ulg   pending_buf_size; /* size of pending_buf */\r
99     Bytef *pending_out;  /* next pending byte to output to the stream */\r
100     uInt   pending;      /* nb of bytes in the pending buffer */\r
101     int   wrap;          /* bit 0 true for zlib, bit 1 true for gzip */\r
102     gz_headerp  gzhead;  /* gzip header information to write */\r
103     uInt   gzindex;      /* where in extra, name, or comment */\r
104     Byte  method;        /* STORED (for zip only) or DEFLATED */\r
105     int   last_flush;    /* value of flush param for previous deflate call */\r
106 \r
107                 /* used by deflate.c: */\r
108 \r
109     uInt  w_size;        /* LZ77 window size (32K by default) */\r
110     uInt  w_bits;        /* log2(w_size)  (8..16) */\r
111     uInt  w_mask;        /* w_size - 1 */\r
112 \r
113     Bytef *window;\r
114     /* Sliding window. Input bytes are read into the second half of the window,\r
115      * and move to the first half later to keep a dictionary of at least wSize\r
116      * bytes. With this organization, matches are limited to a distance of\r
117      * wSize-MAX_MATCH bytes, but this ensures that IO is always\r
118      * performed with a length multiple of the block size. Also, it limits\r
119      * the window size to 64K, which is quite useful on MSDOS.\r
120      * To do: use the user input buffer as sliding window.\r
121      */\r
122 \r
123     ulg window_size;\r
124     /* Actual size of window: 2*wSize, except when the user input buffer\r
125      * is directly used as sliding window.\r
126      */\r
127 \r
128     Posf *prev;\r
129     /* Link to older string with same hash index. To limit the size of this\r
130      * array to 64K, this link is maintained only for the last 32K strings.\r
131      * An index in this array is thus a window index modulo 32K.\r
132      */\r
133 \r
134     Posf *head; /* Heads of the hash chains or NIL. */\r
135 \r
136     uInt  ins_h;          /* hash index of string to be inserted */\r
137     uInt  hash_size;      /* number of elements in hash table */\r
138     uInt  hash_bits;      /* log2(hash_size) */\r
139     uInt  hash_mask;      /* hash_size-1 */\r
140 \r
141     uInt  hash_shift;\r
142     /* Number of bits by which ins_h must be shifted at each input\r
143      * step. It must be such that after MIN_MATCH steps, the oldest\r
144      * byte no longer takes part in the hash key, that is:\r
145      *   hash_shift * MIN_MATCH >= hash_bits\r
146      */\r
147 \r
148     long block_start;\r
149     /* Window position at the beginning of the current output block. Gets\r
150      * negative when the window is moved backwards.\r
151      */\r
152 \r
153     uInt match_length;           /* length of best match */\r
154     IPos prev_match;             /* previous match */\r
155     int match_available;         /* set if previous match exists */\r
156     uInt strstart;               /* start of string to insert */\r
157     uInt match_start;            /* start of matching string */\r
158     uInt lookahead;              /* number of valid bytes ahead in window */\r
159 \r
160     uInt prev_length;\r
161     /* Length of the best match at previous step. Matches not greater than this\r
162      * are discarded. This is used in the lazy match evaluation.\r
163      */\r
164 \r
165     uInt max_chain_length;\r
166     /* To speed up deflation, hash chains are never searched beyond this\r
167      * length.  A higher limit improves compression ratio but degrades the\r
168      * speed.\r
169      */\r
170 \r
171     uInt max_lazy_match;\r
172     /* Attempt to find a better match only when the current match is strictly\r
173      * smaller than this value. This mechanism is used only for compression\r
174      * levels >= 4.\r
175      */\r
176 #   define max_insert_length  max_lazy_match\r
177     /* Insert new strings in the hash table only if the match length is not\r
178      * greater than this length. This saves time but degrades compression.\r
179      * max_insert_length is used only for compression levels <= 3.\r
180      */\r
181 \r
182     int level;    /* compression level (1..9) */\r
183     int strategy; /* favor or force Huffman coding*/\r
184 \r
185     uInt good_match;\r
186     /* Use a faster search when the previous match is longer than this */\r
187 \r
188     int nice_match; /* Stop searching when current match exceeds this */\r
189 \r
190                 /* used by trees.c: */\r
191     /* Didn't use ct_data typedef below to supress compiler warning */\r
192     struct ct_data_s dyn_ltree[HEAP_SIZE];   /* literal and length tree */\r
193     struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */\r
194     struct ct_data_s bl_tree[2*BL_CODES+1];  /* Huffman tree for bit lengths */\r
195 \r
196     struct tree_desc_s l_desc;               /* desc. for literal tree */\r
197     struct tree_desc_s d_desc;               /* desc. for distance tree */\r
198     struct tree_desc_s bl_desc;              /* desc. for bit length tree */\r
199 \r
200     ush bl_count[MAX_BITS+1];\r
201     /* number of codes at each bit length for an optimal tree */\r
202 \r
203     int heap[2*L_CODES+1];      /* heap used to build the Huffman trees */\r
204     int heap_len;               /* number of elements in the heap */\r
205     int heap_max;               /* element of largest frequency */\r
206     /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.\r
207      * The same heap array is used to build all trees.\r
208      */\r
209 \r
210     uch depth[2*L_CODES+1];\r
211     /* Depth of each subtree used as tie breaker for trees of equal frequency\r
212      */\r
213 \r
214     uchf *l_buf;          /* buffer for literals or lengths */\r
215 \r
216     uInt  lit_bufsize;\r
217     /* Size of match buffer for literals/lengths.  There are 4 reasons for\r
218      * limiting lit_bufsize to 64K:\r
219      *   - frequencies can be kept in 16 bit counters\r
220      *   - if compression is not successful for the first block, all input\r
221      *     data is still in the window so we can still emit a stored block even\r
222      *     when input comes from standard input.  (This can also be done for\r
223      *     all blocks if lit_bufsize is not greater than 32K.)\r
224      *   - if compression is not successful for a file smaller than 64K, we can\r
225      *     even emit a stored file instead of a stored block (saving 5 bytes).\r
226      *     This is applicable only for zip (not gzip or zlib).\r
227      *   - creating new Huffman trees less frequently may not provide fast\r
228      *     adaptation to changes in the input data statistics. (Take for\r
229      *     example a binary file with poorly compressible code followed by\r
230      *     a highly compressible string table.) Smaller buffer sizes give\r
231      *     fast adaptation but have of course the overhead of transmitting\r
232      *     trees more frequently.\r
233      *   - I can't count above 4\r
234      */\r
235 \r
236     uInt last_lit;      /* running index in l_buf */\r
237 \r
238     ushf *d_buf;\r
239     /* Buffer for distances. To simplify the code, d_buf and l_buf have\r
240      * the same number of elements. To use different lengths, an extra flag\r
241      * array would be necessary.\r
242      */\r
243 \r
244     ulg opt_len;        /* bit length of current block with optimal trees */\r
245     ulg static_len;     /* bit length of current block with static trees */\r
246     uInt matches;       /* number of string matches in current block */\r
247     int last_eob_len;   /* bit length of EOB code for last block */\r
248 \r
249 #ifdef DEBUG\r
250     ulg compressed_len; /* total bit length of compressed file mod 2^32 */\r
251     ulg bits_sent;      /* bit length of compressed data sent mod 2^32 */\r
252 #endif\r
253 \r
254     ush bi_buf;\r
255     /* Output buffer. bits are inserted starting at the bottom (least\r
256      * significant bits).\r
257      */\r
258     int bi_valid;\r
259     /* Number of valid bits in bi_buf.  All bits above the last valid bit\r
260      * are always zero.\r
261      */\r
262 \r
263 } FAR deflate_state;\r
264 \r
265 /* Output a byte on the stream.\r
266  * IN assertion: there is enough room in pending_buf.\r
267  */\r
268 #define put_byte(s, c) {s->pending_buf[s->pending++] = (c);}\r
269 \r
270 \r
271 #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)\r
272 /* Minimum amount of lookahead, except at the end of the input file.\r
273  * See deflate.c for comments about the MIN_MATCH+1.\r
274  */\r
275 \r
276 #define MAX_DIST(s)  ((s)->w_size-MIN_LOOKAHEAD)\r
277 /* In order to simplify the code, particularly on 16 bit machines, match\r
278  * distances are limited to MAX_DIST instead of WSIZE.\r
279  */\r
280 \r
281         /* in trees.c */\r
282 void _tr_init         OF((deflate_state *s));\r
283 int  _tr_tally        OF((deflate_state *s, unsigned dist, unsigned lc));\r
284 void _tr_flush_block  OF((deflate_state *s, charf *buf, ulg stored_len,\r
285                           int eof));\r
286 void _tr_align        OF((deflate_state *s));\r
287 void _tr_stored_block OF((deflate_state *s, charf *buf, ulg stored_len,\r
288                           int eof));\r
289 \r
290 #define d_code(dist) \\r
291    ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)])\r
292 /* Mapping from a distance to a distance code. dist is the distance - 1 and\r
293  * must not have side effects. _dist_code[256] and _dist_code[257] are never\r
294  * used.\r
295  */\r
296 \r
297 #ifndef DEBUG\r
298 /* Inline versions of _tr_tally for speed: */\r
299 \r
300 #if defined(GEN_TREES_H) || !defined(STDC)\r
301   extern uch _length_code[];\r
302   extern uch _dist_code[];\r
303 #else\r
304   extern const uch _length_code[];\r
305   extern const uch _dist_code[];\r
306 #endif\r
307 \r
308 # define _tr_tally_lit(s, c, flush) \\r
309   { uch cc = (c); \\r
310     s->d_buf[s->last_lit] = 0; \\r
311     s->l_buf[s->last_lit++] = cc; \\r
312     s->dyn_ltree[cc].Freq++; \\r
313     flush = (s->last_lit == s->lit_bufsize-1); \\r
314    }\r
315 # define _tr_tally_dist(s, distance, length, flush) \\r
316   { uch len = (length); \\r
317     ush dist = (distance); \\r
318     s->d_buf[s->last_lit] = dist; \\r
319     s->l_buf[s->last_lit++] = len; \\r
320     dist--; \\r
321     s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \\r
322     s->dyn_dtree[d_code(dist)].Freq++; \\r
323     flush = (s->last_lit == s->lit_bufsize-1); \\r
324   }\r
325 #else\r
326 # define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c)\r
327 # define _tr_tally_dist(s, distance, length, flush) \\r
328               flush = _tr_tally(s, distance, length)\r
329 #endif\r
330 \r
331 #endif /* DEFLATE_H */\r