Coverage Report

Created: 2025-10-10 06:20

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
27
extern const ct_data static_ltree[L_CODES+2];
28
extern const ct_data static_dtree[D_CODES];
29
30
4.07k
#define QUICK_START_BLOCK(s, last) { \
31
4.07k
    zng_tr_emit_tree(s, STATIC_TREES, last); \
32
4.07k
    s->block_open = 1 + (int)last; \
33
4.07k
    s->block_start = (int)s->strstart; \
34
4.07k
}
35
36
8.15k
#define QUICK_END_BLOCK(s, last) { \
37
8.15k
    if (s->block_open) { \
38
4.07k
        zng_tr_emit_end_block(s, static_ltree, last); \
39
4.07k
        s->block_open = 0; \
40
4.07k
        s->block_start = (int)s->strstart; \
41
4.07k
        PREFIX(flush_pending)(s->strm); \
42
4.07k
        if (s->strm->avail_out == 0) \
43
4.07k
            return (last) ? finish_started : need_more; \
44
4.07k
    } \
45
8.15k
}
46
47
4.07k
Z_INTERNAL block_state deflate_quick(deflate_state *s, int flush) {
48
4.07k
    Pos hash_head;
49
4.07k
    int64_t dist;
50
4.07k
    unsigned match_len, last;
51
52
53
4.07k
    last = (flush == Z_FINISH) ? 1 : 0;
54
4.07k
    if (UNLIKELY(last && s->block_open != 2)) {
55
        /* Emit end of previous block */
56
4.07k
        QUICK_END_BLOCK(s, 0);
57
        /* Emit start of last block */
58
4.07k
        QUICK_START_BLOCK(s, last);
59
4.07k
    } 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
96.7M
    for (;;) {
66
96.7M
        if (UNLIKELY(s->pending + ((BIT_BUF_SIZE + 7) >> 3) >= s->pending_buf_size)) {
67
1.33k
            PREFIX(flush_pending)(s->strm);
68
1.33k
            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
1.33k
        }
72
73
96.7M
        if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD)) {
74
259k
            PREFIX(fill_window)(s);
75
259k
            if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH)) {
76
0
                return need_more;
77
0
            }
78
259k
            if (UNLIKELY(s->lookahead == 0))
79
4.07k
                break;
80
81
255k
            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
255k
        }
87
88
96.7M
        if (LIKELY(s->lookahead >= WANT_MIN_MATCH)) {
89
96.7M
            hash_head = quick_insert_string(s, s->strstart);
90
96.7M
            dist = (int64_t)s->strstart - hash_head;
91
92
96.7M
            if (dist <= MAX_DIST(s) && dist > 0) {
93
44.7M
                const uint8_t *str_start = s->window + s->strstart;
94
44.7M
                const uint8_t *match_start = s->window + hash_head;
95
96
44.7M
                if (zng_memcmp_2(str_start, match_start) == 0) {
97
4.62M
                    match_len = FUNCTABLE_CALL(compare256)(str_start+2, match_start+2) + 2;
98
99
4.62M
                    if (match_len >= WANT_MIN_MATCH) {
100
4.62M
                        if (UNLIKELY(match_len > s->lookahead))
101
313
                            match_len = s->lookahead;
102
4.62M
                        if (UNLIKELY(match_len > STD_MAX_MATCH))
103
0
                            match_len = STD_MAX_MATCH;
104
105
4.62M
                        Assert(s->strstart <= UINT16_MAX, "strstart should fit in uint16_t");
106
4.62M
                        check_match(s, (Pos)s->strstart, hash_head, match_len);
107
108
4.62M
                        zng_tr_emit_dist(s, static_ltree, static_dtree, match_len - STD_MIN_MATCH, (uint32_t)dist);
109
4.62M
                        s->lookahead -= match_len;
110
4.62M
                        s->strstart += match_len;
111
4.62M
                        continue;
112
4.62M
                    }
113
4.62M
                }
114
44.7M
            }
115
96.7M
        }
116
117
92.1M
        zng_tr_emit_lit(s, static_ltree, s->window[s->strstart]);
118
92.1M
        s->strstart++;
119
92.1M
        s->lookahead--;
120
92.1M
    }
121
122
4.07k
    s->insert = s->strstart < (STD_MIN_MATCH - 1) ? s->strstart : (STD_MIN_MATCH - 1);
123
4.07k
    if (UNLIKELY(last)) {
124
4.07k
        QUICK_END_BLOCK(s, 1);
125
4.07k
        return finish_done;
126
4.07k
    }
127
128
0
    QUICK_END_BLOCK(s, 0);
129
0
    return block_done;
130
0
}