Coverage Report

Created: 2025-07-11 06:15

/src/zlib-ng/deflate_quick.c
Line
Count
Source (jump to first uncovered line)
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
27
extern const ct_data static_ltree[L_CODES+2];
28
extern const ct_data static_dtree[D_CODES];
29
30
0
#define QUICK_START_BLOCK(s, last) { \
31
0
    zng_tr_emit_tree(s, STATIC_TREES, last); \
32
0
    s->block_open = 1 + (int)last; \
33
0
    s->block_start = (int)s->strstart; \
34
0
}
35
36
0
#define QUICK_END_BLOCK(s, last) { \
37
0
    if (s->block_open) { \
38
0
        zng_tr_emit_end_block(s, static_ltree, last); \
39
0
        s->block_open = 0; \
40
0
        s->block_start = (int)s->strstart; \
41
0
        PREFIX(flush_pending)(s->strm); \
42
0
        if (s->strm->avail_out == 0) \
43
0
            return (last) ? finish_started : need_more; \
44
0
    } \
45
0
}
46
47
0
Z_INTERNAL block_state deflate_quick(deflate_state *s, int flush) {
48
0
    Pos hash_head;
49
0
    int64_t dist;
50
0
    unsigned match_len, last;
51
52
53
0
    last = (flush == Z_FINISH) ? 1 : 0;
54
0
    if (UNLIKELY(last && s->block_open != 2)) {
55
        /* Emit end of previous block */
56
0
        QUICK_END_BLOCK(s, 0);
57
        /* Emit start of last block */
58
0
        QUICK_START_BLOCK(s, last);
59
0
    } else if (UNLIKELY(s->block_open == 0 && s->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, last);
63
0
    }
64
65
0
    for (;;) {
66
0
        if (UNLIKELY(s->pending + ((BIT_BUF_SIZE + 7) >> 3) >= s->pending_buf_size)) {
67
0
            PREFIX(flush_pending)(s->strm);
68
0
            if (s->strm->avail_out == 0) {
69
0
                return (last && s->strm->avail_in == 0 && s->bi_valid == 0 && s->block_open == 0) ? finish_started : need_more;
70
0
            }
71
0
        }
72
73
0
        if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD)) {
74
0
            PREFIX(fill_window)(s);
75
0
            if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH)) {
76
0
                return need_more;
77
0
            }
78
0
            if (UNLIKELY(s->lookahead == 0))
79
0
                break;
80
81
0
            if (UNLIKELY(s->block_open == 0)) {
82
                /* Start new block when we have lookahead data, so that if no
83
                   input data is given an empty block will not be written */
84
0
                QUICK_START_BLOCK(s, last);
85
0
            }
86
0
        }
87
88
0
        if (LIKELY(s->lookahead >= WANT_MIN_MATCH)) {
89
0
            hash_head = quick_insert_string(s, s->strstart);
90
0
            dist = (int64_t)s->strstart - hash_head;
91
92
0
            if (dist <= MAX_DIST(s) && dist > 0) {
93
0
                const uint8_t *str_start = s->window + s->strstart;
94
0
                const uint8_t *match_start = s->window + hash_head;
95
96
0
                if (zng_memcmp_2(str_start, match_start) == 0) {
97
0
                    match_len = FUNCTABLE_CALL(compare256)(str_start+2, match_start+2) + 2;
98
99
0
                    if (match_len >= WANT_MIN_MATCH) {
100
0
                        if (UNLIKELY(match_len > s->lookahead))
101
0
                            match_len = s->lookahead;
102
0
                        if (UNLIKELY(match_len > STD_MAX_MATCH))
103
0
                            match_len = STD_MAX_MATCH;
104
105
0
                        Assert(s->strstart <= UINT16_MAX, "strstart should fit in uint16_t");
106
0
                        check_match(s, (Pos)s->strstart, hash_head, match_len);
107
108
0
                        zng_tr_emit_dist(s, static_ltree, static_dtree, match_len - STD_MIN_MATCH, (uint32_t)dist);
109
0
                        s->lookahead -= match_len;
110
0
                        s->strstart += match_len;
111
0
                        continue;
112
0
                    }
113
0
                }
114
0
            }
115
0
        }
116
117
0
        zng_tr_emit_lit(s, static_ltree, s->window[s->strstart]);
118
0
        s->strstart++;
119
0
        s->lookahead--;
120
0
    }
121
122
0
    s->insert = s->strstart < (STD_MIN_MATCH - 1) ? s->strstart : (STD_MIN_MATCH - 1);
123
0
    if (UNLIKELY(last)) {
124
0
        QUICK_END_BLOCK(s, 1);
125
0
        return finish_done;
126
0
    }
127
128
0
    QUICK_END_BLOCK(s, 0);
129
0
    return block_done;
130
0
}