Coverage Report

Created: 2026-09-19 07:07

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
7.74k
Z_FORCEINLINE static void quick_start_block(deflate_state *s, uint32_t strstart, int last) {
32
7.74k
    zng_tr_emit_tree(s, STATIC_TREES, last);
33
7.74k
    s->block_open = 1 + last;
34
7.74k
    s->block_start = (int)strstart;
35
7.74k
}
36
37
15.1k
Z_FORCEINLINE static int quick_end_block(deflate_state *s, uint32_t strstart, int last) {
38
15.1k
    if (s->block_open) {
39
7.74k
        zng_tr_emit_end_block(s, static_ltree, last);
40
7.74k
        s->block_open = 0;
41
7.74k
        s->block_start = (int)strstart;
42
7.74k
        PREFIX(flush_pending)(s->strm);
43
7.74k
        return (s->strm->avail_out == 0);
44
7.74k
    }
45
7.36k
    return 0;
46
15.1k
}
47
48
Z_FORCEINLINE static block_state deflate_quick_impl(deflate_state *s, int flush,
49
8.45k
                                                   uint32_t strstart, uint32_t lookahead) {
50
8.45k
    unsigned char *window;
51
8.45k
    unsigned last = (flush == Z_FINISH) ? 1 : 0;
52
53
8.45k
    if (UNLIKELY(last && s->block_open != 2)) {
54
        /* Emit end of previous block */
55
7.56k
        if (quick_end_block(s, strstart, 0))
56
7
            return need_more;
57
        /* Emit start of last block */
58
7.55k
        quick_start_block(s, strstart, last);
59
7.55k
    } 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
8.44k
    window = s->window;
66
67
193M
    for (;;) {
68
193M
        if (UNLIKELY(s->pending + ((BIT_BUF_SIZE + 7) >> 3) >= s->pending_buf_size)) {
69
2.50k
            PREFIX(flush_pending)(s->strm);
70
2.50k
            if (s->strm->avail_out == 0) {
71
165
                s->lookahead = lookahead;
72
165
                s->strstart = strstart;
73
165
                return (last && s->strm->avail_in == 0 && s->bi_valid == 0 && s->block_open == 0) ? finish_started : need_more;
74
165
            }
75
2.50k
        }
76
77
193M
        if (UNLIKELY(lookahead < MIN_LOOKAHEAD)) {
78
485k
            s->lookahead = lookahead;
79
485k
            s->strstart = strstart;
80
485k
            PREFIX(fill_window)(s);
81
485k
            lookahead = s->lookahead;
82
485k
            strstart = s->strstart;
83
485k
            if (UNLIKELY(lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH))
84
726
                return need_more;
85
484k
            if (UNLIKELY(lookahead == 0))
86
7.55k
                break;
87
88
477k
            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
194
                quick_start_block(s, strstart, last);
92
194
            }
93
477k
        }
94
95
193M
        uint32_t str_val = Z_U32_FROM_LE(zng_memread_4(window + strstart));
96
97
193M
        if (LIKELY(lookahead >= WANT_MIN_MATCH)) {
98
193M
            uint32_t hash_head = insert_knuth_val_head(s, strstart, str_val);
99
193M
            int64_t dist = (int64_t)strstart - hash_head;
100
101
193M
            if (dist <= MAX_DIST(s) && dist > 0) {
102
92.8M
                const uint8_t *match_start = window + hash_head;
103
92.8M
                uint32_t match_val = Z_U32_FROM_LE(zng_memread_4(match_start));
104
105
92.8M
                if (str_val == match_val) {
106
5.69M
                    const uint8_t *scan_start = window + strstart;
107
5.69M
                    uint32_t match_len = FUNCTABLE_CALL(compare256)(scan_start+2, match_start+2) + 2;
108
109
5.69M
                    if (match_len >= WANT_MIN_MATCH) {
110
5.69M
                        if (UNLIKELY(match_len > lookahead))
111
614
                            match_len = lookahead;
112
113
5.69M
                        Assert(match_len <= STD_MAX_MATCH, "match too long");
114
5.69M
                        Assert(strstart <= UINT16_MAX, "strstart should fit in uint16_t");
115
5.69M
                        check_match(s, strstart, hash_head, match_len);
116
117
5.69M
                        zng_tr_emit_dist(s, static_ltree, static_dtree, match_len - STD_MIN_MATCH, (uint32_t)dist);
118
5.69M
                        lookahead -= match_len;
119
5.69M
                        strstart += match_len;
120
5.69M
                        continue;
121
5.69M
                    }
122
5.69M
                }
123
92.8M
            }
124
193M
        }
125
126
187M
        zng_tr_emit_lit(s, static_ltree, (uint8_t)str_val);
127
187M
        strstart++;
128
187M
        lookahead--;
129
187M
    }
130
131
7.55k
    s->lookahead = lookahead;
132
7.55k
    s->strstart = strstart;
133
7.55k
    s->insert = strstart < (STD_MIN_MATCH - 1) ? strstart : (STD_MIN_MATCH - 1);
134
7.55k
    if (UNLIKELY(last)) {
135
7.55k
        if (quick_end_block(s, strstart, 1))
136
179
            return finish_started;
137
7.37k
        return finish_done;
138
7.55k
    }
139
140
0
    if (quick_end_block(s, strstart, 0))
141
0
        return need_more;
142
0
    return block_done;
143
0
}
144
145
8.45k
Z_INTERNAL block_state deflate_quick(deflate_state *s, int flush) {
146
8.45k
    return deflate_quick_impl(s, flush, s->strstart, s->lookahead);
147
8.45k
}