Coverage Report

Created: 2025-06-10 06:58

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