Coverage Report

Created: 2026-01-09 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qpdf/libqpdf/Pl_RunLength.cc
Line
Count
Source
1
#include <qpdf/Pl_RunLength.hh>
2
3
#include <qpdf/QTC.hh>
4
#include <qpdf/Util.hh>
5
6
using namespace qpdf;
7
8
namespace
9
{
10
    unsigned long long memory_limit{0};
11
} // namespace
12
13
class Pl_RunLength::Members
14
{
15
  public:
16
    Members(action_e action) :
17
171
        action(action)
18
171
    {
19
171
    }
20
    Members(Members const&) = delete;
21
171
    ~Members() = default;
22
23
    action_e action;
24
    state_e state{st_top};
25
    unsigned char buf[128];
26
    unsigned int length{0};
27
    std::string out;
28
};
29
30
Pl_RunLength::Pl_RunLength(char const* identifier, Pipeline* next, action_e action) :
31
171
    Pipeline(identifier, next),
32
171
    m(std::make_unique<Members>(action))
33
171
{
34
171
    util::assertion(next, "Attempt to create Pl_RunLength with nullptr as next");
35
171
}
36
37
void
38
Pl_RunLength::setMemoryLimit(unsigned long long limit)
39
171
{
40
171
    memory_limit = limit;
41
171
}
42
43
171
Pl_RunLength::~Pl_RunLength() = default;
44
45
void
46
Pl_RunLength::write(unsigned char const* data, size_t len)
47
171
{
48
171
    if (m->action == a_encode) {
49
0
        encode(data, len);
50
171
    } else {
51
171
        decode(data, len);
52
171
    }
53
171
}
54
55
void
56
Pl_RunLength::encode(unsigned char const* data, size_t len)
57
0
{
58
0
    for (size_t i = 0; i < len; ++i) {
59
0
        util::assertion(
60
0
            (m->state == st_top) == (m->length <= 1),
61
0
            "Pl_RunLength::encode: state/length inconsistency");
62
0
        unsigned char ch = data[i];
63
0
        if (m->length > 0 && (m->state == st_copying || m->length < 128) &&
64
0
            ch == m->buf[m->length - 1]) {
65
0
            QTC::TC("libtests", "Pl_RunLength: switch to run", (m->length == 128) ? 0 : 1);
66
0
            if (m->state == st_copying) {
67
0
                --m->length;
68
0
                flush_encode();
69
0
                m->buf[0] = ch;
70
0
                m->length = 1;
71
0
            }
72
0
            m->state = st_run;
73
0
            m->buf[m->length] = ch;
74
0
            ++m->length;
75
0
        } else {
76
0
            if (m->length == 128 || m->state == st_run) {
77
0
                flush_encode();
78
0
            } else if (m->length > 0) {
79
0
                m->state = st_copying;
80
0
            }
81
0
            m->buf[m->length] = ch;
82
0
            ++m->length;
83
0
        }
84
0
    }
85
0
}
86
87
void
88
Pl_RunLength::decode(unsigned char const* data, size_t len)
89
171
{
90
171
    util::no_ci_rt_error_if(
91
171
        memory_limit && (len + m->out.size()) > memory_limit, "Pl_RunLength memory limit exceeded");
92
171
    m->out.reserve(len);
93
5.35M
    for (size_t i = 0; i < len; ++i) {
94
5.35M
        unsigned char const& ch = data[i];
95
5.35M
        switch (m->state) {
96
1.56M
        case st_top:
97
1.56M
            if (ch < 128) {
98
                // length represents remaining number of bytes to copy
99
111k
                m->length = 1U + ch;
100
111k
                m->state = st_copying;
101
1.44M
            } else if (ch > 128) {
102
                // length represents number of copies of next byte
103
1.44M
                m->length = 257U - ch;
104
1.44M
                m->state = st_run;
105
1.44M
            } else // ch == 128
106
794
            {
107
                // EOD; stay in this state
108
794
            }
109
1.56M
            break;
110
111
2.34M
        case st_copying:
112
2.34M
            m->out.append(1, static_cast<char>(ch));
113
2.34M
            if (--m->length == 0) {
114
111k
                m->state = st_top;
115
111k
            }
116
2.34M
            break;
117
118
1.44M
        case st_run:
119
1.44M
            m->out.append(m->length, static_cast<char>(ch));
120
1.44M
            m->state = st_top;
121
1.44M
            break;
122
5.35M
        }
123
5.35M
    }
124
171
}
125
126
void
127
Pl_RunLength::flush_encode()
128
0
{
129
0
    if (m->length == 128) {
130
0
        QTC::TC(
131
0
            "libtests",
132
0
            "Pl_RunLength flush full buffer",
133
0
            (m->state == st_copying   ? 0
134
0
                 : m->state == st_run ? 1
135
0
                                      : -1));
136
0
    }
137
0
    if (m->length == 0) {
138
0
        QTC::TC("libtests", "Pl_RunLength flush empty buffer");
139
0
    }
140
0
    if (m->state == st_run) {
141
0
        util::assertion(
142
0
            !(m->length < 2 || m->length > 128),
143
0
            "Pl_RunLength: invalid length in flush_encode for run");
144
0
        auto ch = static_cast<unsigned char>(257 - m->length);
145
0
        next()->write(&ch, 1);
146
0
        next()->write(&m->buf[0], 1);
147
0
    } else if (m->length > 0) {
148
0
        auto ch = static_cast<unsigned char>(m->length - 1);
149
0
        next()->write(&ch, 1);
150
0
        next()->write(m->buf, m->length);
151
0
    }
152
0
    m->state = st_top;
153
0
    m->length = 0;
154
0
}
155
156
void
157
Pl_RunLength::finish()
158
148
{
159
    // When decoding, we might have read a length byte not followed by data, which means the stream
160
    // was terminated early, but we will just ignore this case since this is the only sensible thing
161
    // to do.
162
148
    if (m->action == a_encode) {
163
0
        flush_encode();
164
0
        unsigned char ch = 128;
165
0
        next()->write(&ch, 1);
166
148
    } else {
167
148
        if (memory_limit && (m->out.size()) > memory_limit) {
168
40
            throw std::runtime_error("Pl_RunLength memory limit exceeded");
169
40
        }
170
108
        next()->writeString(m->out);
171
108
    }
172
108
    next()->finish();
173
108
}