Coverage Report

Created: 2025-08-24 07:50

/src/c-blosc2/internal-complibs/zlib-ng-2.0.7/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 "deflate.h"
22
#include "deflate_p.h"
23
#include "functable.h"
24
#include "trees_emit.h"
25
26
extern const ct_data static_ltree[L_CODES+2];
27
extern const ct_data static_dtree[D_CODES];
28
29
4.42k
#define QUICK_START_BLOCK(s, last) { \
30
4.42k
    zng_tr_emit_tree(s, STATIC_TREES, last); \
31
4.42k
    s->block_open = 1 + (int)last; \
32
4.42k
    s->block_start = (int)s->strstart; \
33
4.42k
}
34
35
8.84k
#define QUICK_END_BLOCK(s, last) { \
36
8.84k
    if (s->block_open) { \
37
4.42k
        zng_tr_emit_end_block(s, static_ltree, last); \
38
4.42k
        s->block_open = 0; \
39
4.42k
        s->block_start = (int)s->strstart; \
40
4.42k
        flush_pending(s->strm); \
41
4.42k
        if (s->strm->avail_out == 0) \
42
4.42k
            return (last) ? finish_started : need_more; \
43
4.42k
    } \
44
8.84k
}
45
46
4.42k
Z_INTERNAL block_state deflate_quick(deflate_state *s, int flush) {
47
4.42k
    Pos hash_head;
48
4.42k
    int64_t dist;
49
4.42k
    unsigned match_len, last;
50
51
52
4.42k
    last = (flush == Z_FINISH) ? 1 : 0;
53
4.42k
    if (UNLIKELY(last && s->block_open != 2)) {
54
        /* Emit end of previous block */
55
4.42k
        QUICK_END_BLOCK(s, 0);
56
        /* Emit start of last block */
57
4.42k
        QUICK_START_BLOCK(s, last);
58
4.42k
    } else if (UNLIKELY(s->block_open == 0 && s->lookahead > 0)) {
59
        /* Start new block only when we have lookahead data, so that if no
60
           input data is given an empty block will not be written */
61
0
        QUICK_START_BLOCK(s, last);
62
0
    }
63
64
8.25M
    for (;;) {
65
8.25M
        if (UNLIKELY(s->pending + ((BIT_BUF_SIZE + 7) >> 3) >= s->pending_buf_size)) {
66
0
            flush_pending(s->strm);
67
0
            if (s->strm->avail_out == 0) {
68
0
                return (last && s->strm->avail_in == 0 && s->bi_valid == 0 && s->block_open == 0) ? finish_started : need_more;
69
0
            }
70
0
        }
71
72
8.25M
        if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD)) {
73
515k
            fill_window(s);
74
515k
            if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH)) {
75
0
                return need_more;
76
0
            }
77
515k
            if (UNLIKELY(s->lookahead == 0))
78
4.42k
                break;
79
80
511k
            if (UNLIKELY(s->block_open == 0)) {
81
                /* Start new block when we have lookahead data, so that if no
82
                   input data is given an empty block will not be written */
83
0
                QUICK_START_BLOCK(s, last);
84
0
            }
85
511k
        }
86
87
8.25M
        if (LIKELY(s->lookahead >= MIN_MATCH)) {
88
8.25M
            hash_head = functable.quick_insert_string(s, s->strstart);
89
8.25M
            dist = (int64_t)s->strstart - hash_head;
90
91
8.25M
            if (dist <= MAX_DIST(s) && dist > 0) {
92
8.24M
                match_len = functable.compare258(s->window + s->strstart, s->window + hash_head);
93
94
8.24M
                if (match_len >= MIN_MATCH) {
95
167k
                    if (UNLIKELY(match_len > s->lookahead))
96
1.43k
                        match_len = s->lookahead;
97
98
167k
                    check_match(s, s->strstart, hash_head, match_len);
99
100
167k
                    zng_tr_emit_dist(s, static_ltree, static_dtree, match_len - MIN_MATCH, (uint32_t)dist);
101
167k
                    s->lookahead -= match_len;
102
167k
                    s->strstart += match_len;
103
167k
                    continue;
104
167k
                }
105
8.24M
            }
106
8.25M
        }
107
108
8.08M
        zng_tr_emit_lit(s, static_ltree, s->window[s->strstart]);
109
8.08M
        s->strstart++;
110
8.08M
        s->lookahead--;
111
8.08M
    }
112
113
4.42k
    s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
114
4.42k
    if (UNLIKELY(last)) {
115
4.42k
        QUICK_END_BLOCK(s, 1);
116
2.72k
        return finish_done;
117
4.42k
    }
118
119
0
    QUICK_END_BLOCK(s, 0);
120
0
    return block_done;
121
0
}