Coverage Report

Created: 2026-06-07 06:54

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
2.65M
#  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
9.36k
Z_INTERNAL block_state deflate_rle(deflate_state *s, int flush) {
25
9.36k
    unsigned char *window = s->window;
26
9.36k
    unsigned char *scan;            /* scan goes up to strend for length of run */
27
9.36k
    int bflush = 0;                 /* set if current block must be flushed */
28
9.36k
    uint32_t match_len = 0;
29
30
45.4M
    for (;;) {
31
        /* Make sure that we always have enough lookahead, except
32
         * at the end of the input file. We need STD_MAX_MATCH bytes
33
         * for the longest run, plus one for the unrolled loop.
34
         */
35
45.4M
        if (s->lookahead <= STD_MAX_MATCH) {
36
208k
            PREFIX(fill_window)(s);
37
208k
            if (s->lookahead <= STD_MAX_MATCH && flush == Z_NO_FLUSH)
38
304
                return need_more;
39
208k
            if (s->lookahead == 0)
40
8.93k
                break; /* flush the current block */
41
208k
        }
42
43
        /* See how many times the previous byte repeats */
44
45.4M
        if (s->lookahead >= STD_MIN_MATCH && s->strstart > 0) {
45
45.4M
            scan = window + s->strstart - 1;
46
45.4M
            if (scan[0] == scan[1] && scan[1] == scan[2]) {
47
2.65M
                match_len = compare256_rle(scan, scan+3)+2;
48
2.65M
                match_len = MIN(match_len, s->lookahead);
49
2.65M
            }
50
45.4M
            Assert(scan+match_len <= window + s->window_size - 1, "wild scan");
51
45.4M
        }
52
53
        /* Emit match if have run of STD_MIN_MATCH or longer, else emit literal */
54
45.4M
        if (match_len >= STD_MIN_MATCH) {
55
2.49M
            Assert(s->strstart <= UINT16_MAX, "strstart should fit in uint16_t");
56
2.49M
            check_match(s, s->strstart, s->strstart - 1, match_len);
57
58
2.49M
            bflush = zng_tr_tally_dist(s, 1, match_len - STD_MIN_MATCH);
59
60
2.49M
            s->lookahead -= match_len;
61
2.49M
            s->strstart += match_len;
62
2.49M
            match_len = 0;
63
42.9M
        } else {
64
            /* No match, output a literal byte */
65
42.9M
            bflush = zng_tr_tally_lit(s, window[s->strstart]);
66
42.9M
            s->lookahead--;
67
42.9M
            s->strstart++;
68
42.9M
        }
69
45.4M
        if (bflush)
70
45.4M
            FLUSH_BLOCK(s, window, 0);
71
45.4M
    }
72
8.93k
    s->insert = 0;
73
8.93k
    if (flush == Z_FINISH) {
74
8.93k
        FLUSH_BLOCK(s, window, 1);
75
8.90k
        return finish_done;
76
8.93k
    }
77
0
    if (s->sym_next)
78
0
        FLUSH_BLOCK(s, window, 0);
79
0
    return block_done;
80
0
}