Coverage Report

Created: 2026-05-28 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/zlib-ng/deflate_stored.c
Line
Count
Source
1
/* deflate_stored.c -- store data without compression using deflation algorithm
2
 *
3
 * Copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler
4
 * For conditions of distribution and use, see copyright notice in zlib.h
5
 */
6
7
#include "zbuild.h"
8
#include "deflate.h"
9
#include "deflate_p.h"
10
#include "functable.h"
11
12
/* ===========================================================================
13
 * Copy without compression as much as possible from the input stream, return
14
 * the current block state.
15
 *
16
 * In case deflateParams() is used to later switch to a non-zero compression
17
 * level, s->matches (otherwise unused when storing) keeps track of the number
18
 * of hash table slides to perform. If s->matches is 1, then one hash table
19
 * slide will be done when switching. If s->matches is 2, the maximum value
20
 * allowed here, then the hash table will be cleared, since two or more slides
21
 * is the same as a clear.
22
 *
23
 * deflate_stored() is written to minimize the number of times an input byte is
24
 * copied. It is most efficient with large input and output buffers, which
25
 * maximizes the opportunities to have a single copy from next_in to next_out.
26
 */
27
3.50k
Z_INTERNAL block_state deflate_stored(deflate_state *s, int flush) {
28
3.50k
    unsigned char *window = s->window;
29
    /* Smallest worthy block size when not flushing or finishing. By default
30
     * this is 32K. This can be as small as 507 bytes for memLevel == 1. For
31
     * large input and output buffers, the stored block size will be larger.
32
     */
33
3.50k
    unsigned int w_size = s->w_size;
34
3.50k
    unsigned min_block = MIN(s->pending_buf_size - 5, w_size);
35
36
    /* Copy as many min_block or larger stored blocks directly to next_out as
37
     * possible. If flushing, copy the remaining available input to next_out as
38
     * stored blocks, if there is enough space.
39
     */
40
3.50k
    unsigned len, left, have, last = 0;
41
3.50k
    unsigned used = s->strm->avail_in;
42
5.64k
    do {
43
        /* Set len to the maximum size block that we can copy directly with the
44
         * available input data and output space. Set left to how much of that
45
         * would be copied from what's left in the window.
46
         */
47
5.64k
        len = MAX_STORED;       /* maximum deflate stored block length */
48
5.64k
        have = (s->bi_valid + 42) >> 3;         /* number of header bytes */
49
5.64k
        if (s->strm->avail_out < have)          /* need room for header */
50
284
            break;
51
            /* maximum stored block length that will fit in avail_out: */
52
5.36k
        have = s->strm->avail_out - have;
53
5.36k
        left = (int)s->strstart - s->block_start;    /* bytes left in window */
54
5.36k
        if (len > (unsigned long)left + s->strm->avail_in)
55
4.77k
            len = left + s->strm->avail_in;     /* limit len to the input */
56
5.36k
        len = MIN(len, have);                   /* limit len to the output */
57
58
        /* If the stored block would be less than min_block in length, or if
59
         * unable to copy all of the available input when flushing, then try
60
         * copying to the window and the pending buffer instead. Also don't
61
         * write an empty block when flushing -- deflate() does that.
62
         */
63
5.36k
        if (len < min_block && ((len == 0 && flush != Z_FINISH) || flush == Z_NO_FLUSH || len != left + s->strm->avail_in))
64
3.07k
            break;
65
66
        /* Make a dummy stored block in pending to get the header bytes,
67
         * including any pending bits. This also updates the debugging counts.
68
         */
69
2.29k
        last = flush == Z_FINISH && len == left + s->strm->avail_in ? 1 : 0;
70
2.29k
        zng_tr_stored_block(s, NULL, 0L, last);
71
72
        /* Replace the lengths in the dummy stored block with len. */
73
2.29k
        s->pending -= 4;
74
2.29k
        put_short(s, (uint16_t)len);
75
2.29k
        put_short(s, (uint16_t)~len);
76
77
        /* Write the stored block header bytes. */
78
2.29k
        PREFIX(flush_pending)(s->strm);
79
80
        /* Update debugging counts for the data about to be copied. */
81
2.29k
        cmpr_bits_add(s, len << 3);
82
2.29k
        sent_bits_add(s, len << 3);
83
84
        /* Copy uncompressed bytes from the window to next_out. */
85
2.29k
        if (left) {
86
1.78k
            left = MIN(left, len);
87
1.78k
            memcpy(s->strm->next_out, window + s->block_start, left);
88
1.78k
            s->strm->next_out += left;
89
1.78k
            s->strm->avail_out -= left;
90
1.78k
            s->strm->total_out += left;
91
1.78k
            s->block_start += (int)left;
92
1.78k
            len -= left;
93
1.78k
        }
94
95
        /* Copy uncompressed bytes directly from next_in to next_out, updating
96
         * the check value.
97
         */
98
2.29k
        if (len) {
99
757
            read_buf(s->strm, s->strm->next_out, len);
100
757
            s->strm->next_out += len;
101
757
            s->strm->avail_out -= len;
102
757
            s->strm->total_out += len;
103
757
        }
104
2.29k
    } while (last == 0);
105
106
    /* Update the sliding window with the last s->w_size bytes of the copied
107
     * data, or append all of the copied data to the existing window if less
108
     * than s->w_size bytes were copied. Also update the number of bytes to
109
     * insert in the hash tables, in the event that deflateParams() switches to
110
     * a non-zero compression level.
111
     */
112
3.50k
    used -= s->strm->avail_in;      /* number of input bytes directly copied */
113
3.50k
    if (used) {
114
        /* If any input was used, then no unused input remains in the window,
115
         * therefore s->block_start == s->strstart.
116
         */
117
434
        if (used >= w_size) {    /* supplant the previous history */
118
335
            s->matches = 2;         /* clear hash */
119
335
            memcpy(window, s->strm->next_in - w_size, w_size);
120
335
            s->strstart = w_size;
121
335
            s->insert = s->strstart;
122
335
        } else {
123
99
            if (s->window_size - s->strstart <= used) {
124
                /* Slide the window down. */
125
5
                s->strstart -= w_size;
126
5
                memcpy(window, window + w_size, s->strstart);
127
5
                if (s->matches < 2)
128
0
                    s->matches++;   /* add a pending slide_hash() */
129
5
                s->insert = MIN(s->insert, s->strstart);
130
5
            }
131
99
            memcpy(window + s->strstart, s->strm->next_in - used, used);
132
99
            s->strstart += used;
133
99
            s->insert += MIN(used, w_size - s->insert);
134
99
        }
135
434
        s->block_start = (int)s->strstart;
136
434
    }
137
3.50k
    s->high_water = MAX(s->high_water, s->strstart);
138
139
    /* If the last block was written to next_out, then done. */
140
3.50k
    if (last)
141
150
        return finish_done;
142
143
    /* If flushing and all input has been consumed, then done. */
144
3.35k
    if (flush != Z_NO_FLUSH && flush != Z_FINISH && s->strm->avail_in == 0 && (int)s->strstart == s->block_start)
145
1.53k
        return block_done;
146
147
    /* Fill the window with any remaining input. */
148
1.82k
    have = s->window_size - s->strstart;
149
1.82k
    if (s->strm->avail_in > have && s->block_start >= (int)w_size) {
150
        /* Slide the window down. */
151
35
        s->block_start -= (int)w_size;
152
35
        s->strstart -= w_size;
153
35
        memcpy(window, window + w_size, s->strstart);
154
35
        if (s->matches < 2)
155
35
            s->matches++;           /* add a pending slide_hash() */
156
35
        have += w_size;          /* more space now */
157
35
        s->insert = MIN(s->insert, s->strstart);
158
35
    }
159
160
1.82k
    have = MIN(have, s->strm->avail_in);
161
1.82k
    if (have) {
162
1.82k
        read_buf(s->strm, window + s->strstart, have);
163
1.82k
        s->strstart += have;
164
1.82k
        s->insert += MIN(have, w_size - s->insert);
165
1.82k
    }
166
1.82k
    s->high_water = MAX(s->high_water, s->strstart);
167
168
    /* There was not enough avail_out to write a complete worthy or flushed
169
     * stored block to next_out. Write a stored block to pending instead, if we
170
     * have enough input for a worthy block, or if flushing and there is enough
171
     * room for the remaining input as a stored block in the pending buffer.
172
     */
173
1.82k
    have = (s->bi_valid + 42) >> 3;         /* number of header bytes */
174
        /* maximum stored block length that will fit in pending: */
175
1.82k
    have = MIN(s->pending_buf_size - have, MAX_STORED);
176
1.82k
    min_block = MIN(have, w_size);
177
1.82k
    left = (int)s->strstart - s->block_start;
178
1.82k
    if (left >= min_block || ((left || flush == Z_FINISH) && flush != Z_NO_FLUSH && s->strm->avail_in == 0 && left <= have)) {
179
40
        len = MIN(left, have);
180
40
        last = flush == Z_FINISH && s->strm->avail_in == 0 && len == left ? 1 : 0;
181
40
        zng_tr_stored_block(s, window + s->block_start, len, last);
182
40
        s->block_start += (int)len;
183
40
        PREFIX(flush_pending)(s->strm);
184
40
    }
185
186
    /* We've done all we can with the available input and output. */
187
1.82k
    return last ? finish_started : need_more;
188
3.35k
}