Coverage Report

Created: 2026-04-01 07:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/base/srld.c
Line
Count
Source
1
/* Copyright (C) 2001-2023 Artifex Software, Inc.
2
   All Rights Reserved.
3
4
   This software is provided AS-IS with no warranty, either express or
5
   implied.
6
7
   This software is distributed under license and may not be copied,
8
   modified or distributed except as expressly authorized under the terms
9
   of the license contained in the file LICENSE in this distribution.
10
11
   Refer to licensing information at http://www.artifex.com or contact
12
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
13
   CA 94129, USA, for further information.
14
*/
15
16
17
/* RunLengthDecode filter */
18
#include "stdio_.h"   /* includes std.h */
19
#include "memory_.h"
20
#include "strimpl.h"
21
#include "srlx.h"
22
23
/* ------ RunLengthDecode ------ */
24
25
private_st_RLD_state();
26
27
/* Set defaults */
28
static void
29
s_RLD_set_defaults(stream_state * st)
30
436
{
31
436
    stream_RLD_state *const ss = (stream_RLD_state *) st;
32
33
436
    s_RLD_set_defaults_inline(ss);
34
436
}
35
36
/* Initialize */
37
static int
38
s_RLD_init(stream_state * st)
39
218
{
40
218
    stream_RLD_state *const ss = (stream_RLD_state *) st;
41
42
218
    return s_RLD_init_inline(ss);
43
218
}
44
45
/* Refill the buffer */
46
static int
47
s_RLD_process(stream_state * st, stream_cursor_read * pr,
48
              stream_cursor_write * pw, bool last)
49
4.90k
{
50
4.90k
    stream_RLD_state *const ss = (stream_RLD_state *) st;
51
4.90k
    register const byte *p = pr->ptr;
52
4.90k
    register byte *q = pw->ptr;
53
4.90k
    const byte *rlimit = pr->limit;
54
4.90k
    byte *wlimit = pw->limit;
55
4.90k
    int left;
56
4.90k
    int status = 0;
57
58
9.21k
top:
59
9.21k
    if ((left = ss->copy_left) > 0) {
60
        /*
61
         * We suspended because the output buffer was full:;
62
         * try again now.
63
         */
64
8.60k
        uint avail = wlimit - q;
65
8.60k
        int copy_status = 1;
66
67
8.60k
        if (left > avail)
68
4.12k
            left = avail;
69
8.60k
        if (ss->copy_data >= 0)
70
7.96k
            memset(q + 1, ss->copy_data, left);
71
637
        else {
72
637
            avail = rlimit - p;
73
637
            if (left >= avail) {
74
263
                copy_status = 0;
75
263
                left = avail;
76
263
            }
77
637
            memcpy(q + 1, p + 1, left);
78
637
            p += left;
79
637
        }
80
8.60k
        q += left;
81
8.60k
        if ((ss->copy_left -= left) > 0) {
82
4.38k
            status = copy_status;
83
4.38k
            goto x;
84
4.38k
        }
85
8.60k
    }
86
254k
    while (p < rlimit) {
87
254k
        int b = *++p;
88
89
254k
        if (b < 128) {
90
83.7k
            if (++b > rlimit - p || b > wlimit - q) {
91
323
                ss->copy_left = b;
92
323
                ss->copy_data = -1;
93
323
                goto top;
94
323
            }
95
83.3k
            memcpy(q + 1, p + 1, b);
96
83.3k
            p += b;
97
83.3k
            q += b;
98
170k
        } else if (b == 128) { /* end of data */
99
98
            if (ss->EndOfData) {
100
98
                status = EOFC;
101
98
                break;
102
98
            }
103
170k
        } else if (p == rlimit) {
104
121
            p--;
105
121
            break;
106
170k
        } else if ((b = 257 - b) > wlimit - q) {
107
3.98k
            ss->copy_left = b;
108
3.98k
            ss->copy_data = *++p;
109
3.98k
            goto top;
110
166k
        } else {
111
166k
            memset(q + 1, *++p, b);
112
166k
            q += b;
113
166k
        }
114
254k
    }
115
4.90k
x:  pr->ptr = p;
116
4.90k
    pw->ptr = q;
117
4.90k
    return status;
118
4.83k
}
119
120
/* Stream template */
121
const stream_template s_RLD_template = {
122
    &st_RLD_state, s_RLD_init, s_RLD_process, 1, 1, NULL,
123
    s_RLD_set_defaults
124
};