Coverage Report

Created: 2025-10-23 06:32

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
#elif defined(HAVE_BUILTIN_CTZLL)
16
0
#  define compare256_rle compare256_rle_64
17
#elif defined(HAVE_BUILTIN_CTZ)
18
#  define compare256_rle compare256_rle_32
19
#else
20
#  define compare256_rle compare256_rle_16
21
#endif
22
23
/* ===========================================================================
24
 * For Z_RLE, simply look for runs of bytes, generate matches only of distance
25
 * one.  Do not maintain a hash table.  (It will be regenerated if this run of
26
 * deflate switches away from Z_RLE.)
27
 */
28
0
Z_INTERNAL block_state deflate_rle(deflate_state *s, int flush) {
29
0
    int bflush = 0;                 /* set if current block must be flushed */
30
0
    unsigned char *scan;            /* scan goes up to strend for length of run */
31
0
    uint32_t match_len = 0;
32
33
0
    for (;;) {
34
        /* Make sure that we always have enough lookahead, except
35
         * at the end of the input file. We need STD_MAX_MATCH bytes
36
         * for the longest run, plus one for the unrolled loop.
37
         */
38
0
        if (s->lookahead <= STD_MAX_MATCH) {
39
0
            PREFIX(fill_window)(s);
40
0
            if (s->lookahead <= STD_MAX_MATCH && flush == Z_NO_FLUSH)
41
0
                return need_more;
42
0
            if (s->lookahead == 0)
43
0
                break; /* flush the current block */
44
0
        }
45
46
        /* See how many times the previous byte repeats */
47
0
        if (s->lookahead >= STD_MIN_MATCH && s->strstart > 0) {
48
0
            scan = s->window + s->strstart - 1;
49
0
            if (scan[0] == scan[1] && scan[1] == scan[2]) {
50
0
                match_len = compare256_rle(scan, scan+3)+2;
51
0
                match_len = MIN(match_len, s->lookahead);
52
0
                match_len = MIN(match_len, STD_MAX_MATCH);
53
0
            }
54
0
            Assert(scan+match_len <= s->window + s->window_size - 1, "wild scan");
55
0
        }
56
57
        /* Emit match if have run of STD_MIN_MATCH or longer, else emit literal */
58
0
        if (match_len >= STD_MIN_MATCH) {
59
0
            Assert(s->strstart <= UINT16_MAX, "strstart should fit in uint16_t");
60
0
            check_match(s, (Pos)s->strstart, (Pos)(s->strstart - 1), match_len);
61
62
0
            bflush = zng_tr_tally_dist(s, 1, match_len - STD_MIN_MATCH);
63
64
0
            s->lookahead -= match_len;
65
0
            s->strstart += match_len;
66
0
            match_len = 0;
67
0
        } else {
68
            /* No match, output a literal byte */
69
0
            bflush = zng_tr_tally_lit(s, s->window[s->strstart]);
70
0
            s->lookahead--;
71
0
            s->strstart++;
72
0
        }
73
0
        if (bflush)
74
0
            FLUSH_BLOCK(s, 0);
75
0
    }
76
0
    s->insert = 0;
77
0
    if (flush == Z_FINISH) {
78
0
        FLUSH_BLOCK(s, 1);
79
0
        return finish_done;
80
0
    }
81
0
    if (s->sym_next)
82
0
        FLUSH_BLOCK(s, 0);
83
0
    return block_done;
84
0
}