Coverage Report

Created: 2026-08-13 06:29

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