Coverage Report

Created: 2026-08-31 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/zlib-ng/deflate_rle.c
Line
Count
Source
1
/* deflate_rle.c -- compress data using RLE strategy of 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
#include "compare256_rle.h"
12
13
#if OPTIMAL_CMP == 8
14
#  define compare256_rle compare256_rle_8
15
#else
16
3.59M
#  define compare256_rle compare256_rle_64
17
#endif
18
19
/* ===========================================================================
20
 * For Z_RLE, simply look for runs of bytes, generate matches only of distance
21
 * one.  Do not maintain a hash table.  (It will be regenerated if this run of
22
 * deflate switches away from Z_RLE.)
23
 */
24
4.39k
Z_INTERNAL block_state deflate_rle(deflate_state *s, int flush) {
25
4.39k
    unsigned char *window = s->window;
26
4.39k
    unsigned char *scan;            /* scan goes up to strend for length of run */
27
4.39k
    int bflush = 0;                 /* set if current block must be flushed */
28
4.39k
    uint32_t match_len = 0;
29
4.39k
    unsigned int lookahead = s->lookahead;
30
4.39k
    unsigned int strstart = s->strstart;
31
32
51.6M
    for (;;) {
33
        /* Make sure that we always have enough lookahead, except
34
         * at the end of the input file. We need STD_MAX_MATCH bytes
35
         * for the longest run, plus one for the unrolled loop.
36
         */
37
51.6M
        if (UNLIKELY(lookahead <= STD_MAX_MATCH)) {
38
121k
            s->lookahead = lookahead;
39
121k
            s->strstart = strstart;
40
121k
            PREFIX(fill_window)(s);
41
121k
            lookahead = s->lookahead;
42
121k
            strstart = s->strstart;
43
121k
            if (UNLIKELY(lookahead <= STD_MAX_MATCH && flush == Z_NO_FLUSH))
44
397
                return need_more;
45
120k
            if (UNLIKELY(lookahead == 0))
46
3.84k
                break; /* flush the current block */
47
120k
        }
48
49
        /* See how many times the previous byte repeats */
50
51.6M
        if (LIKELY(lookahead >= STD_MIN_MATCH && strstart > 0)) {
51
51.6M
            scan = window + strstart - 1;
52
51.6M
            if (scan[0] == scan[1] && scan[1] == scan[2]) {
53
3.59M
                match_len = compare256_rle(scan, scan+3)+2;
54
3.59M
                match_len = MIN(match_len, lookahead);
55
3.59M
            }
56
51.6M
            Assert(scan+match_len <= window + s->window_size - 1, "wild scan");
57
51.6M
        }
58
59
        /* Emit match if have run of STD_MIN_MATCH or longer, else emit literal */
60
51.6M
        if (match_len >= STD_MIN_MATCH) {
61
3.42M
            Assert(strstart <= UINT16_MAX, "strstart should fit in uint16_t");
62
3.42M
            check_match(s, strstart, strstart - 1, match_len);
63
64
3.42M
            bflush = zng_tr_tally_dist(s, 1, match_len - STD_MIN_MATCH);
65
66
3.42M
            lookahead -= match_len;
67
3.42M
            strstart += match_len;
68
3.42M
            match_len = 0;
69
48.2M
        } else {
70
            /* No match, output a literal byte */
71
48.2M
            bflush = zng_tr_tally_lit(s, window[strstart]);
72
48.2M
            lookahead--;
73
48.2M
            strstart++;
74
48.2M
        }
75
51.6M
        if (bflush) {
76
2.99k
            s->lookahead = lookahead;
77
2.99k
            s->strstart = strstart;
78
2.99k
            FLUSH_BLOCK(s, window, 0);
79
2.84k
        }
80
51.6M
    }
81
3.84k
    s->lookahead = lookahead;
82
3.84k
    s->strstart = strstart;
83
3.84k
    s->insert = 0;
84
3.84k
    if (flush == Z_FINISH) {
85
3.84k
        FLUSH_BLOCK(s, window, 1);
86
3.81k
        return finish_done;
87
3.84k
    }
88
0
    if (s->sym_next)
89
0
        FLUSH_BLOCK(s, window, 0);
90
0
    return block_done;
91
0
}