Coverage Report

Created: 2025-10-12 07:06

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