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
15 /* ===========================================================================
16  * Check that the match at match_start is indeed a match.
17  */
check_match(deflate_state * s,Pos start,Pos match,int length)18 static inline void check_match(deflate_state *s, Pos start, Pos match, int length) {
19     /* check that the match length is valid*/
20     if (length < STD_MIN_MATCH || length > STD_MAX_MATCH) {
21         fprintf(stderr, " start %u, match %u, length %d\n", start, match, length);
22         z_error("invalid match length");
23     }
24     /* check that the match isn't at the same position as the start string */
25     if (match == start) {
26         fprintf(stderr, " start %u, match %u, length %d\n", start, match, length);
27         z_error("invalid match position");
28     }
29     /* check that the match is indeed a match */
30     if (memcmp(s->window + match, s->window + start, length) != 0) {
31         int32_t i = 0;
32         fprintf(stderr, " start %u, match %u, length %d\n", start, match, length);
33         do {
34             fprintf(stderr, "  %03d: match [%02x] start [%02x]\n", i++,
35                 s->window[match++], s->window[start++]);
36         } while (--length != 0);
37         z_error("invalid match");
38     }
39     if (z_verbose > 1) {
40         fprintf(stderr, "\\[%u,%d]", start-match, length);
41         do {
42             putc(s->window[start++], stderr);
43         } while (--length != 0);
44     }
45 }
46 #else
47 #define check_match(s, start, match, length)
48 #endif
49 
50 Z_INTERNAL void PREFIX(flush_pending)(PREFIX3(stream) *strm);
51 Z_INTERNAL unsigned read_buf(PREFIX3(stream) *strm, unsigned char *buf, unsigned size);
52 
53 /* ===========================================================================
54  * Save the match info and tally the frequency counts. Return true if
55  * the current block must be flushed.
56  */
57 
58 extern const unsigned char Z_INTERNAL zng_length_code[];
59 extern const unsigned char Z_INTERNAL zng_dist_code[];
60 
zng_tr_tally_lit(deflate_state * s,unsigned char c)61 static inline int zng_tr_tally_lit(deflate_state *s, unsigned char c) {
62     /* c is the unmatched char */
63     s->sym_buf[s->sym_next++] = 0;
64     s->sym_buf[s->sym_next++] = 0;
65     s->sym_buf[s->sym_next++] = c;
66     s->dyn_ltree[c].Freq++;
67     Tracevv((stderr, "%c", c));
68     Assert(c <= (STD_MAX_MATCH-STD_MIN_MATCH), "zng_tr_tally: bad literal");
69     return (s->sym_next == s->sym_end);
70 }
71 
zng_tr_tally_dist(deflate_state * s,uint32_t dist,uint32_t len)72 static inline int zng_tr_tally_dist(deflate_state *s, uint32_t dist, uint32_t len) {
73     /* dist: distance of matched string */
74     /* len: match length-STD_MIN_MATCH */
75     s->sym_buf[s->sym_next++] = (uint8_t)(dist);
76     s->sym_buf[s->sym_next++] = (uint8_t)(dist >> 8);
77     s->sym_buf[s->sym_next++] = (uint8_t)len;
78     s->matches++;
79     dist--;
80     Assert(dist < MAX_DIST(s) && (uint16_t)d_code(dist) < (uint16_t)D_CODES,
81         "zng_tr_tally: bad match");
82 
83     s->dyn_ltree[zng_length_code[len]+LITERALS+1].Freq++;
84     s->dyn_dtree[d_code(dist)].Freq++;
85     return (s->sym_next == s->sym_end);
86 }
87 
88 /* ===========================================================================
89  * Flush the current block, with given end-of-file flag.
90  * IN assertion: strstart is set to the end of the current match.
91  */
92 #define FLUSH_BLOCK_ONLY(s, last) { \
93     zng_tr_flush_block(s, (s->block_start >= 0 ? \
94                    (char *)&s->window[(unsigned)s->block_start] : \
95                    NULL), \
96                    (uint32_t)((int)s->strstart - s->block_start), \
97                    (last)); \
98     s->block_start = (int)s->strstart; \
99     PREFIX(flush_pending)(s->strm); \
100 }
101 
102 /* Same but force premature exit if necessary. */
103 #define FLUSH_BLOCK(s, last) { \
104     FLUSH_BLOCK_ONLY(s, last); \
105     if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \
106 }
107 
108 /* Maximum stored block length in deflate format (not including header). */
109 #define MAX_STORED 65535
110 
111 /* Compression function. Returns the block state after the call. */
112 typedef block_state (*compress_func) (deflate_state *s, int flush);
113 /* Match function. Returns the longest match. */
114 typedef uint32_t    (*match_func)    (deflate_state *const s, Pos cur_match);
115 
116 #endif
117