Coverage Report

Created: 2026-07-16 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/zlib-ng/deflate_quick.c
Line
Count
Source
1
/*
2
 * The deflate_quick deflate strategy, designed to be used when cycles are
3
 * at a premium.
4
 *
5
 * Copyright (C) 2013 Intel Corporation. All rights reserved.
6
 * Authors:
7
 *  Wajdi Feghali   <wajdi.k.feghali@intel.com>
8
 *  Jim Guilford    <james.guilford@intel.com>
9
 *  Vinodh Gopal    <vinodh.gopal@intel.com>
10
 *     Erdinc Ozturk   <erdinc.ozturk@intel.com>
11
 *  Jim Kukunas     <james.t.kukunas@linux.intel.com>
12
 *
13
 * Portions are Copyright (C) 2016 12Sided Technology, LLC.
14
 * Author:
15
 *  Phil Vachon     <pvachon@12sidedtech.com>
16
 *
17
 * For conditions of distribution and use, see copyright notice in zlib.h
18
 */
19
20
#include "zbuild.h"
21
#include "zmemory.h"
22
#include "deflate.h"
23
#include "deflate_p.h"
24
#include "functable.h"
25
#include "trees_emit.h"
26
#include "insert_string_p.h"
27
28
extern const ct_data static_ltree[L_CODES+2];
29
extern const ct_data static_dtree[D_CODES];
30
31
1.29k
Z_FORCEINLINE static void quick_start_block(deflate_state *s, uint32_t strstart, int last) {
32
1.29k
    zng_tr_emit_tree(s, STATIC_TREES, last);
33
1.29k
    s->block_open = 1 + last;
34
1.29k
    s->block_start = (int)strstart;
35
1.29k
}
36
37
2.15k
Z_FORCEINLINE static int quick_end_block(deflate_state *s, uint32_t strstart, int last) {
38
2.15k
    if (s->block_open) {
39
1.29k
        zng_tr_emit_end_block(s, static_ltree, last);
40
1.29k
        s->block_open = 0;
41
1.29k
        s->block_start = (int)strstart;
42
1.29k
        PREFIX(flush_pending)(s->strm);
43
1.29k
        return (s->strm->avail_out == 0);
44
1.29k
    }
45
853
    return 0;
46
2.15k
}
47
48
Z_FORCEINLINE static block_state deflate_quick_impl(deflate_state *s, int flush,
49
2.07k
                                                   uint32_t strstart, uint32_t lookahead) {
50
2.07k
    unsigned char *window;
51
2.07k
    unsigned last = (flush == Z_FINISH) ? 1 : 0;
52
53
2.07k
    if (UNLIKELY(last && s->block_open != 2)) {
54
        /* Emit end of previous block */
55
1.07k
        if (quick_end_block(s, strstart, 0))
56
8
            return need_more;
57
        /* Emit start of last block */
58
1.07k
        quick_start_block(s, strstart, last);
59
1.07k
    } else if (UNLIKELY(s->block_open == 0 && lookahead > 0)) {
60
        /* Start new block only when we have lookahead data, so that if no
61
           input data is given an empty block will not be written */
62
0
        quick_start_block(s, strstart, last);
63
0
    }
64
65
2.06k
    window = s->window;
66
67
74.9M
    for (;;) {
68
74.9M
        if (UNLIKELY(s->pending + ((BIT_BUF_SIZE + 7) >> 3) >= s->pending_buf_size)) {
69
957
            PREFIX(flush_pending)(s->strm);
70
957
            if (s->strm->avail_out == 0) {
71
193
                s->lookahead = lookahead;
72
193
                s->strstart = strstart;
73
193
                return (last && s->strm->avail_in == 0 && s->bi_valid == 0 && s->block_open == 0) ? finish_started : need_more;
74
193
            }
75
957
        }
76
77
74.9M
        if (UNLIKELY(lookahead < MIN_LOOKAHEAD)) {
78
88.9k
            s->lookahead = lookahead;
79
88.9k
            s->strstart = strstart;
80
88.9k
            PREFIX(fill_window)(s);
81
88.9k
            lookahead = s->lookahead;
82
88.9k
            strstart = s->strstart;
83
88.9k
            if (UNLIKELY(lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH))
84
800
                return need_more;
85
88.1k
            if (UNLIKELY(lookahead == 0))
86
1.07k
                break;
87
88
87.0k
            if (UNLIKELY(s->block_open == 0)) {
89
                /* Start new block when we have lookahead data, so that if no
90
                   input data is given an empty block will not be written */
91
226
                quick_start_block(s, strstart, last);
92
226
            }
93
87.0k
        }
94
95
74.9M
        uint32_t str_val = Z_U32_FROM_LE(zng_memread_4(window + strstart));
96
97
74.9M
        if (LIKELY(lookahead >= WANT_MIN_MATCH)) {
98
74.9M
            uint32_t hash_head = insert_knuth_val(s, strstart, str_val);
99
74.9M
            int64_t dist = (int64_t)strstart - hash_head;
100
101
74.9M
            if (dist <= MAX_DIST(s) && dist > 0) {
102
35.7M
                const uint8_t *match_start = window + hash_head;
103
35.7M
                uint32_t match_val = Z_U32_FROM_LE(zng_memread_4(match_start));
104
105
35.7M
                if (str_val == match_val) {
106
345k
                    const uint8_t *scan_start = window + strstart;
107
345k
                    uint32_t match_len = FUNCTABLE_CALL(compare256)(scan_start+2, match_start+2) + 2;
108
109
345k
                    if (match_len >= WANT_MIN_MATCH) {
110
345k
                        if (UNLIKELY(match_len > lookahead))
111
96
                            match_len = lookahead;
112
113
345k
                        Assert(match_len <= STD_MAX_MATCH, "match too long");
114
345k
                        Assert(strstart <= UINT16_MAX, "strstart should fit in uint16_t");
115
345k
                        check_match(s, strstart, hash_head, match_len);
116
117
345k
                        zng_tr_emit_dist(s, static_ltree, static_dtree, match_len - STD_MIN_MATCH, (uint32_t)dist);
118
345k
                        lookahead -= match_len;
119
345k
                        strstart += match_len;
120
345k
                        continue;
121
345k
                    }
122
345k
                }
123
35.7M
            }
124
74.9M
        }
125
126
74.6M
        zng_tr_emit_lit(s, static_ltree, (uint8_t)str_val);
127
74.6M
        strstart++;
128
74.6M
        lookahead--;
129
74.6M
    }
130
131
1.07k
    s->lookahead = lookahead;
132
1.07k
    s->strstart = strstart;
133
1.07k
    s->insert = strstart < (STD_MIN_MATCH - 1) ? strstart : (STD_MIN_MATCH - 1);
134
1.07k
    if (UNLIKELY(last)) {
135
1.07k
        if (quick_end_block(s, strstart, 1))
136
209
            return finish_started;
137
862
        return finish_done;
138
1.07k
    }
139
140
0
    if (quick_end_block(s, strstart, 0))
141
0
        return need_more;
142
0
    return block_done;
143
0
}
144
145
2.07k
Z_INTERNAL block_state deflate_quick(deflate_state *s, int flush) {
146
2.07k
    return deflate_quick_impl(s, flush, s->strstart, s->lookahead);
147
2.07k
}