Line | Count | Source |
1 | | /* deflate_p.h -- Private inline functions and macros shared with more than |
2 | | * one deflate method |
3 | | * |
4 | | * Copyright (C) 1995-2024 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 | | */ |
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 PREFIX(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 | | |
61 | 741M | static inline int zng_tr_tally_lit(deflate_state *s, unsigned char c) { |
62 | | /* c is the unmatched char */ |
63 | 741M | #ifdef LIT_MEM |
64 | 741M | s->d_buf[s->sym_next] = 0; |
65 | 741M | s->l_buf[s->sym_next++] = c; |
66 | | #else |
67 | | s->sym_buf[s->sym_next++] = 0; |
68 | | s->sym_buf[s->sym_next++] = 0; |
69 | | s->sym_buf[s->sym_next++] = c; |
70 | | #endif |
71 | 741M | s->dyn_ltree[c].Freq++; |
72 | 741M | Tracevv((stderr, "%c", c)); |
73 | 741M | Assert(c <= (STD_MAX_MATCH-STD_MIN_MATCH), "zng_tr_tally: bad literal"); |
74 | 741M | return (s->sym_next == s->sym_end); |
75 | 741M | } Unexecuted instantiation: deflate.c:zng_tr_tally_lit deflate_fast.c:zng_tr_tally_lit Line | Count | Source | 61 | 11.9M | static inline int zng_tr_tally_lit(deflate_state *s, unsigned char c) { | 62 | | /* c is the unmatched char */ | 63 | 11.9M | #ifdef LIT_MEM | 64 | 11.9M | s->d_buf[s->sym_next] = 0; | 65 | 11.9M | s->l_buf[s->sym_next++] = c; | 66 | | #else | 67 | | s->sym_buf[s->sym_next++] = 0; | 68 | | s->sym_buf[s->sym_next++] = 0; | 69 | | s->sym_buf[s->sym_next++] = c; | 70 | | #endif | 71 | 11.9M | s->dyn_ltree[c].Freq++; | 72 | 11.9M | Tracevv((stderr, "%c", c)); | 73 | 11.9M | Assert(c <= (STD_MAX_MATCH-STD_MIN_MATCH), "zng_tr_tally: bad literal"); | 74 | 11.9M | return (s->sym_next == s->sym_end); | 75 | 11.9M | } |
deflate_huff.c:zng_tr_tally_lit Line | Count | Source | 61 | 85.0M | static inline int zng_tr_tally_lit(deflate_state *s, unsigned char c) { | 62 | | /* c is the unmatched char */ | 63 | 85.0M | #ifdef LIT_MEM | 64 | 85.0M | s->d_buf[s->sym_next] = 0; | 65 | 85.0M | s->l_buf[s->sym_next++] = c; | 66 | | #else | 67 | | s->sym_buf[s->sym_next++] = 0; | 68 | | s->sym_buf[s->sym_next++] = 0; | 69 | | s->sym_buf[s->sym_next++] = c; | 70 | | #endif | 71 | 85.0M | s->dyn_ltree[c].Freq++; | 72 | 85.0M | Tracevv((stderr, "%c", c)); | 73 | 85.0M | Assert(c <= (STD_MAX_MATCH-STD_MIN_MATCH), "zng_tr_tally: bad literal"); | 74 | 85.0M | return (s->sym_next == s->sym_end); | 75 | 85.0M | } |
deflate_medium.c:zng_tr_tally_lit Line | Count | Source | 61 | 465M | static inline int zng_tr_tally_lit(deflate_state *s, unsigned char c) { | 62 | | /* c is the unmatched char */ | 63 | 465M | #ifdef LIT_MEM | 64 | 465M | s->d_buf[s->sym_next] = 0; | 65 | 465M | s->l_buf[s->sym_next++] = c; | 66 | | #else | 67 | | s->sym_buf[s->sym_next++] = 0; | 68 | | s->sym_buf[s->sym_next++] = 0; | 69 | | s->sym_buf[s->sym_next++] = c; | 70 | | #endif | 71 | 465M | s->dyn_ltree[c].Freq++; | 72 | 465M | Tracevv((stderr, "%c", c)); | 73 | 465M | Assert(c <= (STD_MAX_MATCH-STD_MIN_MATCH), "zng_tr_tally: bad literal"); | 74 | 465M | return (s->sym_next == s->sym_end); | 75 | 465M | } |
Unexecuted instantiation: deflate_quick.c:zng_tr_tally_lit deflate_rle.c:zng_tr_tally_lit Line | Count | Source | 61 | 52.9M | static inline int zng_tr_tally_lit(deflate_state *s, unsigned char c) { | 62 | | /* c is the unmatched char */ | 63 | 52.9M | #ifdef LIT_MEM | 64 | 52.9M | s->d_buf[s->sym_next] = 0; | 65 | 52.9M | s->l_buf[s->sym_next++] = c; | 66 | | #else | 67 | | s->sym_buf[s->sym_next++] = 0; | 68 | | s->sym_buf[s->sym_next++] = 0; | 69 | | s->sym_buf[s->sym_next++] = c; | 70 | | #endif | 71 | 52.9M | s->dyn_ltree[c].Freq++; | 72 | 52.9M | Tracevv((stderr, "%c", c)); | 73 | 52.9M | Assert(c <= (STD_MAX_MATCH-STD_MIN_MATCH), "zng_tr_tally: bad literal"); | 74 | 52.9M | return (s->sym_next == s->sym_end); | 75 | 52.9M | } |
deflate_slow.c:zng_tr_tally_lit Line | Count | Source | 61 | 126M | static inline int zng_tr_tally_lit(deflate_state *s, unsigned char c) { | 62 | | /* c is the unmatched char */ | 63 | 126M | #ifdef LIT_MEM | 64 | 126M | s->d_buf[s->sym_next] = 0; | 65 | 126M | s->l_buf[s->sym_next++] = c; | 66 | | #else | 67 | | s->sym_buf[s->sym_next++] = 0; | 68 | | s->sym_buf[s->sym_next++] = 0; | 69 | | s->sym_buf[s->sym_next++] = c; | 70 | | #endif | 71 | 126M | s->dyn_ltree[c].Freq++; | 72 | 126M | Tracevv((stderr, "%c", c)); | 73 | 126M | Assert(c <= (STD_MAX_MATCH-STD_MIN_MATCH), "zng_tr_tally: bad literal"); | 74 | 126M | return (s->sym_next == s->sym_end); | 75 | 126M | } |
Unexecuted instantiation: deflate_stored.c:zng_tr_tally_lit |
76 | | |
77 | 35.9M | static inline int zng_tr_tally_dist(deflate_state* s, uint32_t dist, uint32_t len) { |
78 | | /* dist: distance of matched string */ |
79 | | /* len: match length-STD_MIN_MATCH */ |
80 | 35.9M | #ifdef LIT_MEM |
81 | 35.9M | Assert(dist <= UINT16_MAX, "dist should fit in uint16_t"); |
82 | 35.9M | Assert(len <= UINT8_MAX, "len should fit in uint8_t"); |
83 | 35.9M | s->d_buf[s->sym_next] = (uint16_t)dist; |
84 | 35.9M | s->l_buf[s->sym_next++] = (uint8_t)len; |
85 | | #else |
86 | | s->sym_buf[s->sym_next++] = (uint8_t)(dist); |
87 | | s->sym_buf[s->sym_next++] = (uint8_t)(dist >> 8); |
88 | | s->sym_buf[s->sym_next++] = (uint8_t)len; |
89 | | #endif |
90 | 35.9M | s->matches++; |
91 | 35.9M | dist--; |
92 | 35.9M | Assert(dist < MAX_DIST(s) && (uint16_t)d_code(dist) < (uint16_t)D_CODES, |
93 | 35.9M | "zng_tr_tally: bad match"); |
94 | | |
95 | 35.9M | s->dyn_ltree[zng_length_code[len] + LITERALS + 1].Freq++; |
96 | 35.9M | s->dyn_dtree[d_code(dist)].Freq++; |
97 | 35.9M | return (s->sym_next == s->sym_end); |
98 | 35.9M | } Unexecuted instantiation: deflate.c:zng_tr_tally_dist deflate_fast.c:zng_tr_tally_dist Line | Count | Source | 77 | 1.60M | static inline int zng_tr_tally_dist(deflate_state* s, uint32_t dist, uint32_t len) { | 78 | | /* dist: distance of matched string */ | 79 | | /* len: match length-STD_MIN_MATCH */ | 80 | 1.60M | #ifdef LIT_MEM | 81 | 1.60M | Assert(dist <= UINT16_MAX, "dist should fit in uint16_t"); | 82 | 1.60M | Assert(len <= UINT8_MAX, "len should fit in uint8_t"); | 83 | 1.60M | s->d_buf[s->sym_next] = (uint16_t)dist; | 84 | 1.60M | s->l_buf[s->sym_next++] = (uint8_t)len; | 85 | | #else | 86 | | s->sym_buf[s->sym_next++] = (uint8_t)(dist); | 87 | | s->sym_buf[s->sym_next++] = (uint8_t)(dist >> 8); | 88 | | s->sym_buf[s->sym_next++] = (uint8_t)len; | 89 | | #endif | 90 | 1.60M | s->matches++; | 91 | 1.60M | dist--; | 92 | 1.60M | Assert(dist < MAX_DIST(s) && (uint16_t)d_code(dist) < (uint16_t)D_CODES, | 93 | 1.60M | "zng_tr_tally: bad match"); | 94 | | | 95 | 1.60M | s->dyn_ltree[zng_length_code[len] + LITERALS + 1].Freq++; | 96 | 1.60M | s->dyn_dtree[d_code(dist)].Freq++; | 97 | 1.60M | return (s->sym_next == s->sym_end); | 98 | 1.60M | } |
Unexecuted instantiation: deflate_huff.c:zng_tr_tally_dist deflate_medium.c:zng_tr_tally_dist Line | Count | Source | 77 | 21.4M | static inline int zng_tr_tally_dist(deflate_state* s, uint32_t dist, uint32_t len) { | 78 | | /* dist: distance of matched string */ | 79 | | /* len: match length-STD_MIN_MATCH */ | 80 | 21.4M | #ifdef LIT_MEM | 81 | 21.4M | Assert(dist <= UINT16_MAX, "dist should fit in uint16_t"); | 82 | 21.4M | Assert(len <= UINT8_MAX, "len should fit in uint8_t"); | 83 | 21.4M | s->d_buf[s->sym_next] = (uint16_t)dist; | 84 | 21.4M | s->l_buf[s->sym_next++] = (uint8_t)len; | 85 | | #else | 86 | | s->sym_buf[s->sym_next++] = (uint8_t)(dist); | 87 | | s->sym_buf[s->sym_next++] = (uint8_t)(dist >> 8); | 88 | | s->sym_buf[s->sym_next++] = (uint8_t)len; | 89 | | #endif | 90 | 21.4M | s->matches++; | 91 | 21.4M | dist--; | 92 | 21.4M | Assert(dist < MAX_DIST(s) && (uint16_t)d_code(dist) < (uint16_t)D_CODES, | 93 | 21.4M | "zng_tr_tally: bad match"); | 94 | | | 95 | 21.4M | s->dyn_ltree[zng_length_code[len] + LITERALS + 1].Freq++; | 96 | 21.4M | s->dyn_dtree[d_code(dist)].Freq++; | 97 | 21.4M | return (s->sym_next == s->sym_end); | 98 | 21.4M | } |
Unexecuted instantiation: deflate_quick.c:zng_tr_tally_dist deflate_rle.c:zng_tr_tally_dist Line | Count | Source | 77 | 1.51M | static inline int zng_tr_tally_dist(deflate_state* s, uint32_t dist, uint32_t len) { | 78 | | /* dist: distance of matched string */ | 79 | | /* len: match length-STD_MIN_MATCH */ | 80 | 1.51M | #ifdef LIT_MEM | 81 | 1.51M | Assert(dist <= UINT16_MAX, "dist should fit in uint16_t"); | 82 | 1.51M | Assert(len <= UINT8_MAX, "len should fit in uint8_t"); | 83 | 1.51M | s->d_buf[s->sym_next] = (uint16_t)dist; | 84 | 1.51M | s->l_buf[s->sym_next++] = (uint8_t)len; | 85 | | #else | 86 | | s->sym_buf[s->sym_next++] = (uint8_t)(dist); | 87 | | s->sym_buf[s->sym_next++] = (uint8_t)(dist >> 8); | 88 | | s->sym_buf[s->sym_next++] = (uint8_t)len; | 89 | | #endif | 90 | 1.51M | s->matches++; | 91 | 1.51M | dist--; | 92 | 1.51M | Assert(dist < MAX_DIST(s) && (uint16_t)d_code(dist) < (uint16_t)D_CODES, | 93 | 1.51M | "zng_tr_tally: bad match"); | 94 | | | 95 | 1.51M | s->dyn_ltree[zng_length_code[len] + LITERALS + 1].Freq++; | 96 | 1.51M | s->dyn_dtree[d_code(dist)].Freq++; | 97 | 1.51M | return (s->sym_next == s->sym_end); | 98 | 1.51M | } |
deflate_slow.c:zng_tr_tally_dist Line | Count | Source | 77 | 11.3M | static inline int zng_tr_tally_dist(deflate_state* s, uint32_t dist, uint32_t len) { | 78 | | /* dist: distance of matched string */ | 79 | | /* len: match length-STD_MIN_MATCH */ | 80 | 11.3M | #ifdef LIT_MEM | 81 | 11.3M | Assert(dist <= UINT16_MAX, "dist should fit in uint16_t"); | 82 | 11.3M | Assert(len <= UINT8_MAX, "len should fit in uint8_t"); | 83 | 11.3M | s->d_buf[s->sym_next] = (uint16_t)dist; | 84 | 11.3M | s->l_buf[s->sym_next++] = (uint8_t)len; | 85 | | #else | 86 | | s->sym_buf[s->sym_next++] = (uint8_t)(dist); | 87 | | s->sym_buf[s->sym_next++] = (uint8_t)(dist >> 8); | 88 | | s->sym_buf[s->sym_next++] = (uint8_t)len; | 89 | | #endif | 90 | 11.3M | s->matches++; | 91 | 11.3M | dist--; | 92 | 11.3M | Assert(dist < MAX_DIST(s) && (uint16_t)d_code(dist) < (uint16_t)D_CODES, | 93 | 11.3M | "zng_tr_tally: bad match"); | 94 | | | 95 | 11.3M | s->dyn_ltree[zng_length_code[len] + LITERALS + 1].Freq++; | 96 | 11.3M | s->dyn_dtree[d_code(dist)].Freq++; | 97 | 11.3M | return (s->sym_next == s->sym_end); | 98 | 11.3M | } |
Unexecuted instantiation: deflate_stored.c:zng_tr_tally_dist |
99 | | |
100 | | /* =========================================================================== |
101 | | * Flush the current block, with given end-of-file flag. |
102 | | * IN assertion: strstart is set to the end of the current match. |
103 | | */ |
104 | 126k | #define FLUSH_BLOCK_ONLY(s, last) { \ |
105 | 126k | zng_tr_flush_block(s, (s->block_start >= 0 ? \ |
106 | 126k | (char *)&s->window[(unsigned)s->block_start] : \ |
107 | 126k | NULL), \ |
108 | 126k | (uint32_t)((int)s->strstart - s->block_start), \ |
109 | 126k | (last)); \ |
110 | 126k | s->block_start = (int)s->strstart; \ |
111 | 126k | PREFIX(flush_pending)(s->strm); \ |
112 | 126k | } |
113 | | |
114 | | /* Same but force premature exit if necessary. */ |
115 | 113k | #define FLUSH_BLOCK(s, last) { \ |
116 | 113k | FLUSH_BLOCK_ONLY(s, last); \ |
117 | 113k | if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \ |
118 | 113k | } |
119 | | |
120 | | /* Maximum stored block length in deflate format (not including header). */ |
121 | 5.32k | #define MAX_STORED 65535 |
122 | | |
123 | | /* Compression function. Returns the block state after the call. */ |
124 | | typedef block_state (*compress_func) (deflate_state *s, int flush); |
125 | | /* Match function. Returns the longest match. */ |
126 | | typedef uint32_t (*match_func) (deflate_state *const s, Pos cur_match); |
127 | | |
128 | | #endif |