Coverage Report

Created: 2026-02-14 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.22k
#define QUICK_START_BLOCK(s, last) { \
32
7.22k
    zng_tr_emit_tree(s, STATIC_TREES, last); \
33
7.22k
    s->block_open = 1 + last; \
34
7.22k
    s->block_start = (int)s->strstart; \
35
7.22k
}
36
37
14.0k
#define QUICK_END_BLOCK(s, last) { \
38
14.0k
    if (s->block_open) { \
39
7.22k
        zng_tr_emit_end_block(s, static_ltree, last); \
40
7.22k
        s->block_open = 0; \
41
7.22k
        s->block_start = (int)s->strstart; \
42
7.22k
        PREFIX(flush_pending)(s->strm); \
43
7.22k
        if (s->strm->avail_out == 0) \
44
7.22k
            return (last) ? finish_started : need_more; \
45
7.22k
    } \
46
14.0k
}
47
48
7.95k
Z_INTERNAL block_state deflate_quick(deflate_state *s, int flush) {
49
7.95k
    unsigned char *window;
50
7.95k
    unsigned last = (flush == Z_FINISH) ? 1 : 0;
51
52
7.95k
    if (UNLIKELY(last && s->block_open != 2)) {
53
        /* Emit end of previous block */
54
7.02k
        QUICK_END_BLOCK(s, 0);
55
        /* Emit start of last block */
56
7.02k
        QUICK_START_BLOCK(s, last);
57
7.02k
    } 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
7.95k
    window = s->window;
64
65
203M
    for (;;) {
66
203M
        uint8_t lc;
67
68
203M
        if (UNLIKELY(s->pending + ((BIT_BUF_SIZE + 7) >> 3) >= s->pending_buf_size)) {
69
2.71k
            PREFIX(flush_pending)(s->strm);
70
2.71k
            if (s->strm->avail_out == 0) {
71
171
                return (last && s->strm->avail_in == 0 && s->bi_valid == 0 && s->block_open == 0) ? finish_started : need_more;
72
171
            }
73
2.71k
        }
74
75
203M
        if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD)) {
76
482k
            PREFIX(fill_window)(s);
77
482k
            if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH)) {
78
758
                return need_more;
79
758
            }
80
482k
            if (UNLIKELY(s->lookahead == 0))
81
7.02k
                break;
82
83
475k
            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
203
                QUICK_START_BLOCK(s, last);
87
203
            }
88
475k
        }
89
90
203M
        if (LIKELY(s->lookahead >= WANT_MIN_MATCH)) {
91
203M
            uint32_t str_val = Z_U32_FROM_LE(zng_memread_4(window + s->strstart));
92
203M
            uint32_t hash_head = quick_insert_value(s, s->strstart, str_val);
93
203M
            int64_t dist = (int64_t)s->strstart - hash_head;
94
203M
            lc = (uint8_t)str_val;
95
96
203M
            if (dist <= MAX_DIST(s) && dist > 0) {
97
98.4M
                const uint8_t *match_start = window + hash_head;
98
98.4M
                uint32_t match_val = Z_U32_FROM_LE(zng_memread_4(match_start));
99
100
98.4M
                if (str_val == match_val) {
101
5.06M
                    const uint8_t *str_start = window + s->strstart;
102
5.06M
                    uint32_t match_len = FUNCTABLE_CALL(compare256)(str_start+2, match_start+2) + 2;
103
104
5.06M
                    if (match_len >= WANT_MIN_MATCH) {
105
5.06M
                        if (UNLIKELY(match_len > s->lookahead))
106
580
                            match_len = s->lookahead;
107
108
5.06M
                        Assert(match_len <= STD_MAX_MATCH, "match too long");
109
5.06M
                        Assert(s->strstart <= UINT16_MAX, "strstart should fit in uint16_t");
110
5.06M
                        check_match(s, s->strstart, hash_head, match_len);
111
112
5.06M
                        zng_tr_emit_dist(s, static_ltree, static_dtree, match_len - STD_MIN_MATCH, (uint32_t)dist);
113
5.06M
                        s->lookahead -= match_len;
114
5.06M
                        s->strstart += match_len;
115
5.06M
                        continue;
116
5.06M
                    }
117
5.06M
                }
118
98.4M
            }
119
203M
        } else {
120
14.1k
            lc = window[s->strstart];
121
14.1k
        }
122
198M
        zng_tr_emit_lit(s, static_ltree, lc);
123
198M
        s->strstart++;
124
198M
        s->lookahead--;
125
198M
    }
126
127
7.02k
    s->insert = s->strstart < (STD_MIN_MATCH - 1) ? s->strstart : (STD_MIN_MATCH - 1);
128
7.02k
    if (UNLIKELY(last)) {
129
7.02k
        QUICK_END_BLOCK(s, 1);
130
6.73k
        return finish_done;
131
7.02k
    }
132
133
0
    QUICK_END_BLOCK(s, 0);
134
0
    return block_done;
135
0
}