Coverage Report

Created: 2025-12-14 06:55

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