Coverage Report

Created: 2026-02-26 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qpdf/libqpdf/Pl_LZWDecoder.cc
Line
Count
Source
1
#include <qpdf/Pl_LZWDecoder.hh>
2
3
#include <qpdf/QIntC.hh>
4
#include <qpdf/QTC.hh>
5
#include <qpdf/Util.hh>
6
#include <cstring>
7
#include <stdexcept>
8
9
using namespace qpdf;
10
11
Pl_LZWDecoder::Pl_LZWDecoder(char const* identifier, Pipeline* next, bool early_code_change) :
12
2.41k
    Pipeline(identifier, next),
13
2.41k
    code_change_delta(early_code_change)
14
2.41k
{
15
2.41k
    util::assertion(next, "Attempt to create Pl_LZWDecoder with nullptr as next");
16
2.41k
}
17
18
void
19
Pl_LZWDecoder::write(unsigned char const* bytes, size_t len)
20
16.5k
{
21
1.33M
    for (size_t i = 0; i < len; ++i) {
22
1.31M
        buf[next_char_++] = bytes[i];
23
1.31M
        if (next_char_ == 3) {
24
437k
            next_char_ = 0;
25
437k
        }
26
1.31M
        bits_available += 8;
27
1.31M
        if (bits_available >= code_size) {
28
1.06M
            sendNextCode();
29
1.06M
        }
30
1.31M
    }
31
16.5k
}
32
33
void
34
Pl_LZWDecoder::finish()
35
2.27k
{
36
2.27k
    next()->finish();
37
2.27k
}
38
39
void
40
Pl_LZWDecoder::sendNextCode()
41
1.06M
{
42
1.06M
    unsigned int high = byte_pos;
43
1.06M
    unsigned int med = (byte_pos + 1) % 3;
44
1.06M
    unsigned int low = (byte_pos + 2) % 3;
45
46
1.06M
    unsigned int bits_from_high = 8 - bit_pos;
47
1.06M
    unsigned int bits_from_med = code_size - bits_from_high;
48
1.06M
    unsigned int bits_from_low = 0;
49
1.06M
    if (bits_from_med > 8) {
50
65.4k
        bits_from_low = bits_from_med - 8;
51
65.4k
        bits_from_med = 8;
52
65.4k
    }
53
1.06M
    unsigned int high_mask = (1U << bits_from_high) - 1U;
54
1.06M
    unsigned int med_mask = 0xff - ((1U << (8 - bits_from_med)) - 1U);
55
1.06M
    unsigned int low_mask = 0xff - ((1U << (8 - bits_from_low)) - 1U);
56
1.06M
    unsigned int code = 0;
57
1.06M
    code += (buf[high] & high_mask) << bits_from_med;
58
1.06M
    code += ((buf[med] & med_mask) >> (8 - bits_from_med));
59
1.06M
    if (bits_from_low) {
60
65.4k
        code <<= bits_from_low;
61
65.4k
        code += ((buf[low] & low_mask) >> (8 - bits_from_low));
62
65.4k
        byte_pos = low;
63
65.4k
        bit_pos = bits_from_low;
64
996k
    } else {
65
996k
        byte_pos = med;
66
996k
        bit_pos = bits_from_med;
67
996k
    }
68
1.06M
    if (bit_pos == 8) {
69
185k
        bit_pos = 0;
70
185k
        ++byte_pos;
71
185k
        byte_pos %= 3;
72
185k
    }
73
1.06M
    bits_available -= code_size;
74
75
1.06M
    handleCode(code);
76
1.06M
}
77
78
unsigned char
79
Pl_LZWDecoder::getFirstChar(unsigned int code)
80
17.5k
{
81
17.5k
    if (code < 256) {
82
154
        return static_cast<unsigned char>(code);
83
154
    }
84
17.4k
    util::no_ci_rt_error_if(
85
17.4k
        code <= 257,
86
17.4k
        "Pl_LZWDecoder::getFirstChar called with invalid code (" + std::to_string(code) + ")");
87
88
17.4k
    unsigned int idx = code - 258;
89
17.4k
    util::no_ci_rt_error_if(idx >= table.size(), "Pl_LZWDecoder::getFirstChar: table overflow");
90
17.4k
    Buffer& b = table.at(idx);
91
17.4k
    return b.getBuffer()[0];
92
17.5k
}
93
94
void
95
Pl_LZWDecoder::addToTable(unsigned char c)
96
1.05M
{
97
1.05M
    unsigned int last_size = 0;
98
1.05M
    unsigned char const* last_data = nullptr;
99
1.05M
    unsigned char tmp[1];
100
101
1.05M
    if (last_code < 256) {
102
1.03M
        tmp[0] = static_cast<unsigned char>(last_code);
103
1.03M
        last_data = tmp;
104
1.03M
        last_size = 1;
105
1.03M
    } else {
106
17.5k
        util::no_ci_rt_error_if(
107
17.5k
            last_code <= 257,
108
17.5k
            "Pl_LZWDecoder::addToTable called with invalid code (" + std::to_string(last_code) +
109
17.5k
                ")");
110
17.5k
        unsigned int idx = last_code - 258;
111
17.5k
        util::no_ci_rt_error_if(idx >= table.size(), "Pl_LZWDecoder::addToTable: table overflow");
112
17.5k
        Buffer& b = table.at(idx);
113
17.5k
        last_data = b.getBuffer();
114
17.5k
        last_size = QIntC::to_uint(b.getSize());
115
17.5k
    }
116
117
1.05M
    Buffer entry(1 + last_size);
118
1.05M
    unsigned char* new_data = entry.getBuffer();
119
1.05M
    memcpy(new_data, last_data, last_size);
120
1.05M
    new_data[last_size] = c;
121
1.05M
    table.push_back(std::move(entry));
122
1.05M
}
123
124
void
125
Pl_LZWDecoder::handleCode(unsigned int code)
126
1.06M
{
127
1.06M
    if (eod) {
128
3.71k
        return;
129
3.71k
    }
130
131
1.05M
    if (code == 256) {
132
2.91k
        if (!table.empty()) {
133
2.30k
            QTC::TC("libtests", "Pl_LZWDecoder intermediate reset");
134
2.30k
        }
135
2.91k
        table.clear();
136
2.91k
        code_size = 9;
137
1.05M
    } else if (code == 257) {
138
36
        eod = true;
139
1.05M
    } else {
140
1.05M
        if (last_code != 256) {
141
            // Add to the table from last time.  New table entry would be what we read last plus the
142
            // first character of what we're reading now.
143
1.05M
            unsigned char next_c = '\0';
144
1.05M
            unsigned int table_size = QIntC::to_uint(table.size());
145
1.05M
            if (code < 256) {
146
                // just read < 256; last time's next_c was code
147
1.03M
                next_c = static_cast<unsigned char>(code);
148
1.03M
            } else if (code > 257) {
149
17.8k
                size_t idx = code - 258;
150
17.8k
                if (idx > table_size) {
151
273
                    throw std::runtime_error("LZWDecoder: bad code received");
152
17.5k
                } else if (idx == table_size) {
153
                    // The encoder would have just created this entry, so the first character of
154
                    // this entry would have been the same as the first character of the last entry.
155
293
                    next_c = getFirstChar(last_code);
156
17.2k
                } else {
157
17.2k
                    next_c = getFirstChar(code);
158
17.2k
                }
159
17.8k
            }
160
1.05M
            unsigned int new_idx = 258 + table_size;
161
1.05M
            util::no_ci_rt_error_if(new_idx == 4096, "LZWDecoder: table full");
162
1.05M
            addToTable(next_c);
163
1.05M
            unsigned int change_idx = new_idx + code_change_delta;
164
1.05M
            if (change_idx == 511 || change_idx == 1023 || change_idx == 2047) {
165
1.82k
                ++code_size;
166
1.82k
            }
167
1.05M
        }
168
169
1.05M
        if (code < 256) {
170
1.03M
            auto ch = static_cast<unsigned char>(code);
171
1.03M
            next()->write(&ch, 1);
172
1.03M
        } else {
173
17.6k
            unsigned int idx = code - 258;
174
17.6k
            if (idx >= table.size()) {
175
43
                throw std::runtime_error("Pl_LZWDecoder::handleCode: table overflow");
176
43
            }
177
17.5k
            Buffer& b = table.at(idx);
178
17.5k
            next()->write(b.getBuffer(), b.getSize());
179
17.5k
        }
180
1.05M
    }
181
182
1.05M
    last_code = code;
183
1.05M
}