Coverage Report

Created: 2025-06-10 07:19

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