Coverage Report

Created: 2025-06-24 07:01

/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
256
{
31
256
    stream_RLD_state *const ss = (stream_RLD_state *) st;
32
33
256
    s_RLD_set_defaults_inline(ss);
34
256
}
35
36
/* Initialize */
37
static int
38
s_RLD_init(stream_state * st)
39
128
{
40
128
    stream_RLD_state *const ss = (stream_RLD_state *) st;
41
42
128
    return s_RLD_init_inline(ss);
43
128
}
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
1.24k
{
50
1.24k
    stream_RLD_state *const ss = (stream_RLD_state *) st;
51
1.24k
    register const byte *p = pr->ptr;
52
1.24k
    register byte *q = pw->ptr;
53
1.24k
    const byte *rlimit = pr->limit;
54
1.24k
    byte *wlimit = pw->limit;
55
1.24k
    int left;
56
1.24k
    int status = 0;
57
58
2.16k
top:
59
2.16k
    if ((left = ss->copy_left) > 0) {
60
        /*
61
         * We suspended because the output buffer was full:;
62
         * try again now.
63
         */
64
1.82k
        uint avail = wlimit - q;
65
1.82k
        int copy_status = 1;
66
67
1.82k
        if (left > avail)
68
800
            left = avail;
69
1.82k
        if (ss->copy_data >= 0)
70
1.39k
            memset(q + 1, ss->copy_data, left);
71
428
        else {
72
428
            avail = rlimit - p;
73
428
            if (left >= avail) {
74
167
                copy_status = 0;
75
167
                left = avail;
76
167
            }
77
428
            memcpy(q + 1, p + 1, left);
78
428
            p += left;
79
428
        }
80
1.82k
        q += left;
81
1.82k
        if ((ss->copy_left -= left) > 0) {
82
966
            status = copy_status;
83
966
            goto x;
84
966
        }
85
1.82k
    }
86
123k
    while (p < rlimit) {
87
123k
        int b = *++p;
88
89
123k
        if (b < 128) {
90
61.5k
            if (++b > rlimit - p || b > wlimit - q) {
91
219
                ss->copy_left = b;
92
219
                ss->copy_data = -1;
93
219
                goto top;
94
219
            }
95
61.2k
            memcpy(q + 1, p + 1, b);
96
61.2k
            p += b;
97
61.2k
            q += b;
98
61.6k
        } else if (b == 128) { /* end of data */
99
51
            if (ss->EndOfData) {
100
51
                status = EOFC;
101
51
                break;
102
51
            }
103
61.6k
        } else if (p == rlimit) {
104
52
            p--;
105
52
            break;
106
61.5k
        } else if ((b = 257 - b) > wlimit - q) {
107
700
            ss->copy_left = b;
108
700
            ss->copy_data = *++p;
109
700
            goto top;
110
60.8k
        } else {
111
60.8k
            memset(q + 1, *++p, b);
112
60.8k
            q += b;
113
60.8k
        }
114
123k
    }
115
1.24k
x:  pr->ptr = p;
116
1.24k
    pw->ptr = q;
117
1.24k
    return status;
118
1.19k
}
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
};