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
0
#  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
0
Z_INTERNAL block_state deflate_rle(deflate_state *s, int flush) {
25
0
    unsigned char *window = s->window;
26
0
    unsigned char *scan;            /* scan goes up to strend for length of run */
27
0
    int bflush = 0;                 /* set if current block must be flushed */
28
0
    uint32_t match_len = 0;
29
0
    unsigned int lookahead = s->lookahead;
30
0
    unsigned int strstart = s->strstart;
31
32
0
    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
0
        if (UNLIKELY(lookahead <= STD_MAX_MATCH)) {
38
0
            s->lookahead = lookahead;
39
0
            s->strstart = strstart;
40
0
            PREFIX(fill_window)(s);
41
0
            lookahead = s->lookahead;
42
0
            strstart = s->strstart;
43
0
            if (UNLIKELY(lookahead <= STD_MAX_MATCH && flush == Z_NO_FLUSH))
44
0
                return need_more;
45
0
            if (UNLIKELY(lookahead == 0))
46
0
                break; /* flush the current block */
47
0
        }
48
49
        /* See how many times the previous byte repeats */
50
0
        if (LIKELY(lookahead >= STD_MIN_MATCH && strstart > 0)) {
51
0
            scan = window + strstart - 1;
52
0
            if (scan[0] == scan[1] && scan[1] == scan[2]) {
53
0
                match_len = compare256_rle(scan, scan+3)+2;
54
0
                match_len = MIN(match_len, lookahead);
55
0
            }
56
0
            Assert(scan+match_len <= window + s->window_size - 1, "wild scan");
57
0
        }
58
59
        /* Emit match if have run of STD_MIN_MATCH or longer, else emit literal */
60
0
        if (match_len >= STD_MIN_MATCH) {
61
0
            Assert(strstart <= UINT16_MAX, "strstart should fit in uint16_t");
62
0
            check_match(s, strstart, strstart - 1, match_len);
63
64
0
            bflush = zng_tr_tally_dist(s, 1, match_len - STD_MIN_MATCH);
65
66
0
            lookahead -= match_len;
67
0
            strstart += match_len;
68
0
            match_len = 0;
69
0
        } else {
70
            /* No match, output a literal byte */
71
0
            bflush = zng_tr_tally_lit(s, window[strstart]);
72
0
            lookahead--;
73
0
            strstart++;
74
0
        }
75
0
        if (bflush) {
76
0
            s->lookahead = lookahead;
77
0
            s->strstart = strstart;
78
0
            FLUSH_BLOCK(s, window, 0);
79
0
        }
80
0
    }
81
0
    s->lookahead = lookahead;
82
0
    s->strstart = strstart;
83
0
    s->insert = 0;
84
0
    if (flush == Z_FINISH) {
85
0
        FLUSH_BLOCK(s, window, 1);
86
0
        return finish_done;
87
0
    }
88
0
    if (s->sym_next)
89
0
        FLUSH_BLOCK(s, window, 0);
90
0
    return block_done;
91
0
}