< prev index next >

src/java.base/share/native/libzip/zlib/deflate.h

Print this page

  6  * published by the Free Software Foundation.  Oracle designates this
  7  * particular file as subject to the "Classpath" exception as provided
  8  * by Oracle in the LICENSE file that accompanied this code.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  */
 24 
 25 /* deflate.h -- internal compression state
 26  * Copyright (C) 1995-2018 Jean-loup Gailly
 27  * For conditions of distribution and use, see copyright notice in zlib.h
 28  */
 29 
 30 /* WARNING: this file should *not* be used by applications. It is
 31    part of the implementation of the compression library and is
 32    subject to change. Applications should only use zlib.h.
 33  */
 34 
 35 /* @(#) $Id$ */
 36 
 37 #ifndef DEFLATE_H
 38 #define DEFLATE_H
 39 
 40 #include "zutil.h"
 41 
 42 /* define NO_GZIP when compiling if you want to disable gzip header and
 43    trailer creation by deflate().  NO_GZIP would be used to avoid linking in
 44    the crc code when it is not needed.  For shared libraries, gzip encoding
 45    should be left enabled. */
 46 #ifndef NO_GZIP
 47 #  define GZIP
 48 #endif
 49 




 50 /* ===========================================================================
 51  * Internal compression state.
 52  */
 53 
 54 #define LENGTH_CODES 29
 55 /* number of length codes, not counting the special END_BLOCK code */
 56 
 57 #define LITERALS  256
 58 /* number of literal bytes 0..255 */
 59 
 60 #define L_CODES (LITERALS+1+LENGTH_CODES)
 61 /* number of Literal or Length codes, including the END_BLOCK code */
 62 
 63 #define D_CODES   30
 64 /* number of distance codes */
 65 
 66 #define BL_CODES  19
 67 /* number of codes used to transfer the bit lengths */
 68 
 69 #define HEAP_SIZE (2*L_CODES+1)

224     struct ct_data_s bl_tree[2*BL_CODES+1];  /* Huffman tree for bit lengths */
225 
226     struct tree_desc_s l_desc;               /* desc. for literal tree */
227     struct tree_desc_s d_desc;               /* desc. for distance tree */
228     struct tree_desc_s bl_desc;              /* desc. for bit length tree */
229 
230     ush bl_count[MAX_BITS+1];
231     /* number of codes at each bit length for an optimal tree */
232 
233     int heap[2*L_CODES+1];      /* heap used to build the Huffman trees */
234     int heap_len;               /* number of elements in the heap */
235     int heap_max;               /* element of largest frequency */
236     /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
237      * The same heap array is used to build all trees.
238      */
239 
240     uch depth[2*L_CODES+1];
241     /* Depth of each subtree used as tie breaker for trees of equal frequency
242      */
243 






244     uchf *sym_buf;        /* buffer for distances and literals/lengths */

245 
246     uInt  lit_bufsize;
247     /* Size of match buffer for literals/lengths.  There are 4 reasons for
248      * limiting lit_bufsize to 64K:
249      *   - frequencies can be kept in 16 bit counters
250      *   - if compression is not successful for the first block, all input
251      *     data is still in the window so we can still emit a stored block even
252      *     when input comes from standard input.  (This can also be done for
253      *     all blocks if lit_bufsize is not greater than 32K.)
254      *   - if compression is not successful for a file smaller than 64K, we can
255      *     even emit a stored file instead of a stored block (saving 5 bytes).
256      *     This is applicable only for zip (not gzip or zlib).
257      *   - creating new Huffman trees less frequently may not provide fast
258      *     adaptation to changes in the input data statistics. (Take for
259      *     example a binary file with poorly compressible code followed by
260      *     a highly compressible string table.) Smaller buffer sizes give
261      *     fast adaptation but have of course the overhead of transmitting
262      *     trees more frequently.
263      *   - I can't count above 4
264      */
265 
266     uInt sym_next;      /* running index in sym_buf */
267     uInt sym_end;       /* symbol table full when sym_next reaches this */
268 
269     ulg opt_len;        /* bit length of current block with optimal trees */
270     ulg static_len;     /* bit length of current block with static trees */
271     uInt matches;       /* number of string matches in current block */
272     uInt insert;        /* bytes at end of window left to insert */
273 
274 #ifdef ZLIB_DEBUG
275     ulg compressed_len; /* total bit length of compressed file mod 2^32 */
276     ulg bits_sent;      /* bit length of compressed data sent mod 2^32 */
277 #endif
278 
279     ush bi_buf;
280     /* Output buffer. bits are inserted starting at the bottom (least
281      * significant bits).
282      */
283     int bi_valid;
284     /* Number of valid bits in bi_buf.  All bits above the last valid bit
285      * are always zero.
286      */

298  * IN assertion: there is enough room in pending_buf.
299  */
300 #define put_byte(s, c) {s->pending_buf[s->pending++] = (Bytef)(c);}
301 
302 
303 #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
304 /* Minimum amount of lookahead, except at the end of the input file.
305  * See deflate.c for comments about the MIN_MATCH+1.
306  */
307 
308 #define MAX_DIST(s)  ((s)->w_size-MIN_LOOKAHEAD)
309 /* In order to simplify the code, particularly on 16 bit machines, match
310  * distances are limited to MAX_DIST instead of WSIZE.
311  */
312 
313 #define WIN_INIT MAX_MATCH
314 /* Number of bytes after end of data in window to initialize in order to avoid
315    memory checker errors from longest match routines */
316 
317         /* in trees.c */
318 void ZLIB_INTERNAL _tr_init OF((deflate_state *s));
319 int ZLIB_INTERNAL _tr_tally OF((deflate_state *s, unsigned dist, unsigned lc));
320 void ZLIB_INTERNAL _tr_flush_block OF((deflate_state *s, charf *buf,
321                         ulg stored_len, int last));
322 void ZLIB_INTERNAL _tr_flush_bits OF((deflate_state *s));
323 void ZLIB_INTERNAL _tr_align OF((deflate_state *s));
324 void ZLIB_INTERNAL _tr_stored_block OF((deflate_state *s, charf *buf,
325                         ulg stored_len, int last));
326 
327 #define d_code(dist) \
328    ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)])
329 /* Mapping from a distance to a distance code. dist is the distance - 1 and
330  * must not have side effects. _dist_code[256] and _dist_code[257] are never
331  * used.
332  */
333 
334 #ifndef ZLIB_DEBUG
335 /* Inline versions of _tr_tally for speed: */
336 
337 #if defined(GEN_TREES_H) || !defined(STDC)
338   extern uch ZLIB_INTERNAL _length_code[];
339   extern uch ZLIB_INTERNAL _dist_code[];
340 #else
341   extern const uch ZLIB_INTERNAL _length_code[];
342   extern const uch ZLIB_INTERNAL _dist_code[];
343 #endif
344 



















345 # define _tr_tally_lit(s, c, flush) \
346   { uch cc = (c); \
347     s->sym_buf[s->sym_next++] = 0; \
348     s->sym_buf[s->sym_next++] = 0; \
349     s->sym_buf[s->sym_next++] = cc; \
350     s->dyn_ltree[cc].Freq++; \
351     flush = (s->sym_next == s->sym_end); \
352    }
353 # define _tr_tally_dist(s, distance, length, flush) \
354   { uch len = (uch)(length); \
355     ush dist = (ush)(distance); \
356     s->sym_buf[s->sym_next++] = (uch)dist; \
357     s->sym_buf[s->sym_next++] = (uch)(dist >> 8); \
358     s->sym_buf[s->sym_next++] = len; \
359     dist--; \
360     s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \
361     s->dyn_dtree[d_code(dist)].Freq++; \
362     flush = (s->sym_next == s->sym_end); \
363   }

364 #else
365 # define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c)
366 # define _tr_tally_dist(s, distance, length, flush) \
367               flush = _tr_tally(s, distance, length)
368 #endif
369 
370 #endif /* DEFLATE_H */

  6  * published by the Free Software Foundation.  Oracle designates this
  7  * particular file as subject to the "Classpath" exception as provided
  8  * by Oracle in the LICENSE file that accompanied this code.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  */
 24 
 25 /* deflate.h -- internal compression state
 26  * Copyright (C) 1995-2024 Jean-loup Gailly
 27  * For conditions of distribution and use, see copyright notice in zlib.h
 28  */
 29 
 30 /* WARNING: this file should *not* be used by applications. It is
 31    part of the implementation of the compression library and is
 32    subject to change. Applications should only use zlib.h.
 33  */
 34 
 35 /* @(#) $Id$ */
 36 
 37 #ifndef DEFLATE_H
 38 #define DEFLATE_H
 39 
 40 #include "zutil.h"
 41 
 42 /* define NO_GZIP when compiling if you want to disable gzip header and
 43    trailer creation by deflate().  NO_GZIP would be used to avoid linking in
 44    the crc code when it is not needed.  For shared libraries, gzip encoding
 45    should be left enabled. */
 46 #ifndef NO_GZIP
 47 #  define GZIP
 48 #endif
 49 
 50 /* define LIT_MEM to slightly increase the speed of deflate (order 1% to 2%) at
 51    the cost of a larger memory footprint */
 52 /* #define LIT_MEM */
 53 
 54 /* ===========================================================================
 55  * Internal compression state.
 56  */
 57 
 58 #define LENGTH_CODES 29
 59 /* number of length codes, not counting the special END_BLOCK code */
 60 
 61 #define LITERALS  256
 62 /* number of literal bytes 0..255 */
 63 
 64 #define L_CODES (LITERALS+1+LENGTH_CODES)
 65 /* number of Literal or Length codes, including the END_BLOCK code */
 66 
 67 #define D_CODES   30
 68 /* number of distance codes */
 69 
 70 #define BL_CODES  19
 71 /* number of codes used to transfer the bit lengths */
 72 
 73 #define HEAP_SIZE (2*L_CODES+1)

228     struct ct_data_s bl_tree[2*BL_CODES+1];  /* Huffman tree for bit lengths */
229 
230     struct tree_desc_s l_desc;               /* desc. for literal tree */
231     struct tree_desc_s d_desc;               /* desc. for distance tree */
232     struct tree_desc_s bl_desc;              /* desc. for bit length tree */
233 
234     ush bl_count[MAX_BITS+1];
235     /* number of codes at each bit length for an optimal tree */
236 
237     int heap[2*L_CODES+1];      /* heap used to build the Huffman trees */
238     int heap_len;               /* number of elements in the heap */
239     int heap_max;               /* element of largest frequency */
240     /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
241      * The same heap array is used to build all trees.
242      */
243 
244     uch depth[2*L_CODES+1];
245     /* Depth of each subtree used as tie breaker for trees of equal frequency
246      */
247 
248 #ifdef LIT_MEM
249 #   define LIT_BUFS 5
250     ushf *d_buf;          /* buffer for distances */
251     uchf *l_buf;          /* buffer for literals/lengths */
252 #else
253 #   define LIT_BUFS 4
254     uchf *sym_buf;        /* buffer for distances and literals/lengths */
255 #endif
256 
257     uInt  lit_bufsize;
258     /* Size of match buffer for literals/lengths.  There are 4 reasons for
259      * limiting lit_bufsize to 64K:
260      *   - frequencies can be kept in 16 bit counters
261      *   - if compression is not successful for the first block, all input
262      *     data is still in the window so we can still emit a stored block even
263      *     when input comes from standard input.  (This can also be done for
264      *     all blocks if lit_bufsize is not greater than 32K.)
265      *   - if compression is not successful for a file smaller than 64K, we can
266      *     even emit a stored file instead of a stored block (saving 5 bytes).
267      *     This is applicable only for zip (not gzip or zlib).
268      *   - creating new Huffman trees less frequently may not provide fast
269      *     adaptation to changes in the input data statistics. (Take for
270      *     example a binary file with poorly compressible code followed by
271      *     a highly compressible string table.) Smaller buffer sizes give
272      *     fast adaptation but have of course the overhead of transmitting
273      *     trees more frequently.
274      *   - I can't count above 4
275      */
276 
277     uInt sym_next;      /* running index in symbol buffer */
278     uInt sym_end;       /* symbol table full when sym_next reaches this */
279 
280     ulg opt_len;        /* bit length of current block with optimal trees */
281     ulg static_len;     /* bit length of current block with static trees */
282     uInt matches;       /* number of string matches in current block */
283     uInt insert;        /* bytes at end of window left to insert */
284 
285 #ifdef ZLIB_DEBUG
286     ulg compressed_len; /* total bit length of compressed file mod 2^32 */
287     ulg bits_sent;      /* bit length of compressed data sent mod 2^32 */
288 #endif
289 
290     ush bi_buf;
291     /* Output buffer. bits are inserted starting at the bottom (least
292      * significant bits).
293      */
294     int bi_valid;
295     /* Number of valid bits in bi_buf.  All bits above the last valid bit
296      * are always zero.
297      */

309  * IN assertion: there is enough room in pending_buf.
310  */
311 #define put_byte(s, c) {s->pending_buf[s->pending++] = (Bytef)(c);}
312 
313 
314 #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
315 /* Minimum amount of lookahead, except at the end of the input file.
316  * See deflate.c for comments about the MIN_MATCH+1.
317  */
318 
319 #define MAX_DIST(s)  ((s)->w_size-MIN_LOOKAHEAD)
320 /* In order to simplify the code, particularly on 16 bit machines, match
321  * distances are limited to MAX_DIST instead of WSIZE.
322  */
323 
324 #define WIN_INIT MAX_MATCH
325 /* Number of bytes after end of data in window to initialize in order to avoid
326    memory checker errors from longest match routines */
327 
328         /* in trees.c */
329 void ZLIB_INTERNAL _tr_init(deflate_state *s);
330 int ZLIB_INTERNAL _tr_tally(deflate_state *s, unsigned dist, unsigned lc);
331 void ZLIB_INTERNAL _tr_flush_block(deflate_state *s, charf *buf,
332                                    ulg stored_len, int last);
333 void ZLIB_INTERNAL _tr_flush_bits(deflate_state *s);
334 void ZLIB_INTERNAL _tr_align(deflate_state *s);
335 void ZLIB_INTERNAL _tr_stored_block(deflate_state *s, charf *buf,
336                                     ulg stored_len, int last);
337 
338 #define d_code(dist) \
339    ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)])
340 /* Mapping from a distance to a distance code. dist is the distance - 1 and
341  * must not have side effects. _dist_code[256] and _dist_code[257] are never
342  * used.
343  */
344 
345 #ifndef ZLIB_DEBUG
346 /* Inline versions of _tr_tally for speed: */
347 
348 #if defined(GEN_TREES_H) || !defined(STDC)
349   extern uch ZLIB_INTERNAL _length_code[];
350   extern uch ZLIB_INTERNAL _dist_code[];
351 #else
352   extern const uch ZLIB_INTERNAL _length_code[];
353   extern const uch ZLIB_INTERNAL _dist_code[];
354 #endif
355 
356 #ifdef LIT_MEM
357 # define _tr_tally_lit(s, c, flush) \
358   { uch cc = (c); \
359     s->d_buf[s->sym_next] = 0; \
360     s->l_buf[s->sym_next++] = cc; \
361     s->dyn_ltree[cc].Freq++; \
362     flush = (s->sym_next == s->sym_end); \
363    }
364 # define _tr_tally_dist(s, distance, length, flush) \
365   { uch len = (uch)(length); \
366     ush dist = (ush)(distance); \
367     s->d_buf[s->sym_next] = dist; \
368     s->l_buf[s->sym_next++] = len; \
369     dist--; \
370     s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \
371     s->dyn_dtree[d_code(dist)].Freq++; \
372     flush = (s->sym_next == s->sym_end); \
373   }
374 #else
375 # define _tr_tally_lit(s, c, flush) \
376   { uch cc = (c); \
377     s->sym_buf[s->sym_next++] = 0; \
378     s->sym_buf[s->sym_next++] = 0; \
379     s->sym_buf[s->sym_next++] = cc; \
380     s->dyn_ltree[cc].Freq++; \
381     flush = (s->sym_next == s->sym_end); \
382    }
383 # define _tr_tally_dist(s, distance, length, flush) \
384   { uch len = (uch)(length); \
385     ush dist = (ush)(distance); \
386     s->sym_buf[s->sym_next++] = (uch)dist; \
387     s->sym_buf[s->sym_next++] = (uch)(dist >> 8); \
388     s->sym_buf[s->sym_next++] = len; \
389     dist--; \
390     s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \
391     s->dyn_dtree[d_code(dist)].Freq++; \
392     flush = (s->sym_next == s->sym_end); \
393   }
394 #endif
395 #else
396 # define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c)
397 # define _tr_tally_dist(s, distance, length, flush) \
398               flush = _tr_tally(s, distance, length)
399 #endif
400 
401 #endif /* DEFLATE_H */
< prev index next >