Coverage Report

Created: 2026-01-10 06:46

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
707
#define QUICK_START_BLOCK(s, last) { \
32
707
    zng_tr_emit_tree(s, STATIC_TREES, last); \
33
707
    s->block_open = 1 + last; \
34
707
    s->block_start = (int)s->strstart; \
35
707
}
36
37
1.23k
#define QUICK_END_BLOCK(s, last) { \
38
1.23k
    if (s->block_open) { \
39
707
        zng_tr_emit_end_block(s, static_ltree, last); \
40
707
        s->block_open = 0; \
41
707
        s->block_start = (int)s->strstart; \
42
707
        PREFIX(flush_pending)(s->strm); \
43
707
        if (s->strm->avail_out == 0) \
44
707
            return (last) ? finish_started : need_more; \
45
707
    } \
46
1.23k
}
47
48
1.10k
Z_INTERNAL block_state deflate_quick(deflate_state *s, int flush) {
49
1.10k
    unsigned char *window;
50
1.10k
    unsigned last = (flush == Z_FINISH) ? 1 : 0;
51
52
1.10k
    if (UNLIKELY(last && s->block_open != 2)) {
53
        /* Emit end of previous block */
54
618
        QUICK_END_BLOCK(s, 0);
55
        /* Emit start of last block */
56
616
        QUICK_START_BLOCK(s, last);
57
616
    } else if (UNLIKELY(s->block_open == 0 && s->lookahead > 0)) {
58
        /* Start new block only when we have lookahead data, so that if no
59
           input data is given an empty block will not be written */
60
0
        QUICK_START_BLOCK(s, last);
61
0
    }
62
63
1.09k
    window = s->window;
64
65
28.4M
    for (;;) {
66
28.4M
        uint8_t lc;
67
68
28.4M
        if (UNLIKELY(s->pending + ((BIT_BUF_SIZE + 7) >> 3) >= s->pending_buf_size)) {
69
368
            PREFIX(flush_pending)(s->strm);
70
368
            if (s->strm->avail_out == 0) {
71
77
                return (last && s->strm->avail_in == 0 && s->bi_valid == 0 && s->block_open == 0) ? finish_started : need_more;
72
77
            }
73
368
        }
74
75
28.4M
        if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD)) {
76
40.5k
            PREFIX(fill_window)(s);
77
40.5k
            if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH)) {
78
405
                return need_more;
79
405
            }
80
40.1k
            if (UNLIKELY(s->lookahead == 0))
81
616
                break;
82
83
39.5k
            if (UNLIKELY(s->block_open == 0)) {
84
                /* Start new block when we have lookahead data, so that if no
85
                   input data is given an empty block will not be written */
86
91
                QUICK_START_BLOCK(s, last);
87
91
            }
88
39.5k
        }
89
90
28.4M
        if (LIKELY(s->lookahead >= WANT_MIN_MATCH)) {
91
28.4M
#if BYTE_ORDER == LITTLE_ENDIAN
92
28.4M
            uint32_t str_val = zng_memread_4(window + s->strstart);
93
#else
94
            uint32_t str_val = ZSWAP32(zng_memread_4(window + s->strstart));
95
#endif
96
28.4M
            uint32_t hash_head = quick_insert_value(s, s->strstart, str_val);
97
28.4M
            int64_t dist = (int64_t)s->strstart - hash_head;
98
28.4M
            lc = (uint8_t)str_val;
99
100
28.4M
            if (dist <= MAX_DIST(s) && dist > 0) {
101
13.4M
                const uint8_t *match_start = window + hash_head;
102
13.4M
#if BYTE_ORDER == LITTLE_ENDIAN
103
13.4M
                uint32_t match_val = zng_memread_4(match_start);
104
#else
105
                uint32_t match_val = ZSWAP32(zng_memread_4(match_start));
106
#endif
107
108
13.4M
                if (str_val == match_val) {
109
1.26M
                    const uint8_t *str_start = window + s->strstart;
110
1.26M
                    uint32_t match_len = FUNCTABLE_CALL(compare256)(str_start+2, match_start+2) + 2;
111
112
1.26M
                    if (match_len >= WANT_MIN_MATCH) {
113
1.26M
                        if (UNLIKELY(match_len > s->lookahead))
114
84
                            match_len = s->lookahead;
115
116
1.26M
                        Assert(match_len <= STD_MAX_MATCH, "match too long");
117
1.26M
                        Assert(s->strstart <= UINT16_MAX, "strstart should fit in uint16_t");
118
1.26M
                        check_match(s, s->strstart, hash_head, match_len);
119
120
1.26M
                        zng_tr_emit_dist(s, static_ltree, static_dtree, match_len - STD_MIN_MATCH, (uint32_t)dist);
121
1.26M
                        s->lookahead -= match_len;
122
1.26M
                        s->strstart += match_len;
123
1.26M
                        continue;
124
1.26M
                    }
125
1.26M
                }
126
13.4M
            }
127
28.4M
        } else {
128
1.08k
            lc = window[s->strstart];
129
1.08k
        }
130
27.1M
        zng_tr_emit_lit(s, static_ltree, lc);
131
27.1M
        s->strstart++;
132
27.1M
        s->lookahead--;
133
27.1M
    }
134
135
616
    s->insert = s->strstart < (STD_MIN_MATCH - 1) ? s->strstart : (STD_MIN_MATCH - 1);
136
616
    if (UNLIKELY(last)) {
137
616
        QUICK_END_BLOCK(s, 1);
138
538
        return finish_done;
139
616
    }
140
141
0
    QUICK_END_BLOCK(s, 0);
142
0
    return block_done;
143
0
}