1/* deflate_p.h -- Private inline functions and macros shared with more than
2 * one deflate method
3 *
4 * Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler
5 * For conditions of distribution and use, see copyright notice in zlib.h
6 *
7 */
8
9#ifndef DEFLATE_P_H
10#define DEFLATE_P_H
11
12/* Forward declare common non-inlined functions declared in deflate.c */
13
14#ifdef ZLIB_DEBUG
15void check_match(deflate_state *s, Pos start, Pos match, int length);
16#else
17#define check_match(s, start, match, length)
18#endif
19void flush_pending(PREFIX3(stream) *strm);
20
21/* ===========================================================================
22 * Save the match info and tally the frequency counts. Return true if
23 * the current block must be flushed.
24 */
25
26extern const unsigned char Z_INTERNAL zng_length_code[];
27extern const unsigned char Z_INTERNAL zng_dist_code[];
28
29static inline int zng_tr_tally_lit(deflate_state *s, unsigned char c) {
30 /* c is the unmatched char */
31 s->sym_buf[s->sym_next++] = 0;
32 s->sym_buf[s->sym_next++] = 0;
33 s->sym_buf[s->sym_next++] = c;
34 s->dyn_ltree[c].Freq++;
35 Tracevv((stderr, "%c", c));
36 Assert(c <= (MAX_MATCH-MIN_MATCH), "zng_tr_tally: bad literal");
37 return (s->sym_next == s->sym_end);
38}
39
40static inline int zng_tr_tally_dist(deflate_state *s, uint32_t dist, uint32_t len) {
41 /* dist: distance of matched string */
42 /* len: match length-MIN_MATCH */
43 s->sym_buf[s->sym_next++] = (uint8_t)(dist);
44 s->sym_buf[s->sym_next++] = (uint8_t)(dist >> 8);
45 s->sym_buf[s->sym_next++] = (uint8_t)len;
46 s->matches++;
47 dist--;
48 Assert(dist < MAX_DIST(s) && (uint16_t)d_code(dist) < (uint16_t)D_CODES,
49 "zng_tr_tally: bad match");
50
51 s->dyn_ltree[zng_length_code[len]+LITERALS+1].Freq++;
52 s->dyn_dtree[d_code(dist)].Freq++;
53 return (s->sym_next == s->sym_end);
54}
55
56/* ===========================================================================
57 * Flush the current block, with given end-of-file flag.
58 * IN assertion: strstart is set to the end of the current match.
59 */
60#define FLUSH_BLOCK_ONLY(s, last) { \
61 zng_tr_flush_block(s, (s->block_start >= 0 ? \
62 (char *)&s->window[(unsigned)s->block_start] : \
63 NULL), \
64 (uint32_t)((int)s->strstart - s->block_start), \
65 (last)); \
66 s->block_start = (int)s->strstart; \
67 flush_pending(s->strm); \
68}
69
70/* Same but force premature exit if necessary. */
71#define FLUSH_BLOCK(s, last) { \
72 FLUSH_BLOCK_ONLY(s, last); \
73 if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \
74}
75
76/* Maximum stored block length in deflate format (not including header). */
77#define MAX_STORED 65535
78
79#endif
80