Coverage Report

Created: 2025-07-18 07:00

/src/qpdf/libqpdf/Pl_LZWDecoder.cc
Line
Count
Source (jump to first uncovered line)
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
363
    Pipeline(identifier, next),
11
363
    code_change_delta(early_code_change)
12
363
{
13
363
    if (!next) {
14
0
        throw std::logic_error("Attempt to create Pl_LZWDecoder with nullptr as next");
15
0
    }
16
363
}
17
18
void
19
Pl_LZWDecoder::write(unsigned char const* bytes, size_t len)
20
363
{
21
1.12M
    for (size_t i = 0; i < len; ++i) {
22
1.12M
        buf[next_char_++] = bytes[i];
23
1.12M
        if (next_char_ == 3) {
24
374k
            next_char_ = 0;
25
374k
        }
26
1.12M
        bits_available += 8;
27
1.12M
        if (bits_available >= code_size) {
28
887k
            sendNextCode();
29
887k
        }
30
1.12M
    }
31
363
}
32
33
void
34
Pl_LZWDecoder::finish()
35
302
{
36
302
    next()->finish();
37
302
}
38
39
void
40
Pl_LZWDecoder::sendNextCode()
41
887k
{
42
887k
    unsigned int high = byte_pos;
43
887k
    unsigned int med = (byte_pos + 1) % 3;
44
887k
    unsigned int low = (byte_pos + 2) % 3;
45
46
887k
    unsigned int bits_from_high = 8 - bit_pos;
47
887k
    unsigned int bits_from_med = code_size - bits_from_high;
48
887k
    unsigned int bits_from_low = 0;
49
887k
    if (bits_from_med > 8) {
50
166k
        bits_from_low = bits_from_med - 8;
51
166k
        bits_from_med = 8;
52
166k
    }
53
887k
    unsigned int high_mask = (1U << bits_from_high) - 1U;
54
887k
    unsigned int med_mask = 0xff - ((1U << (8 - bits_from_med)) - 1U);
55
887k
    unsigned int low_mask = 0xff - ((1U << (8 - bits_from_low)) - 1U);
56
887k
    unsigned int code = 0;
57
887k
    code += (buf[high] & high_mask) << bits_from_med;
58
887k
    code += ((buf[med] & med_mask) >> (8 - bits_from_med));
59
887k
    if (bits_from_low) {
60
166k
        code <<= bits_from_low;
61
166k
        code += ((buf[low] & low_mask) >> (8 - bits_from_low));
62
166k
        byte_pos = low;
63
166k
        bit_pos = bits_from_low;
64
720k
    } else {
65
720k
        byte_pos = med;
66
720k
        bit_pos = bits_from_med;
67
720k
    }
68
887k
    if (bit_pos == 8) {
69
70.5k
        bit_pos = 0;
70
70.5k
        ++byte_pos;
71
70.5k
        byte_pos %= 3;
72
70.5k
    }
73
887k
    bits_available -= code_size;
74
75
887k
    handleCode(code);
76
887k
}
77
78
unsigned char
79
Pl_LZWDecoder::getFirstChar(unsigned int code)
80
10.4k
{
81
10.4k
    unsigned char result = '\0';
82
10.4k
    if (code < 256) {
83
562
        result = static_cast<unsigned char>(code);
84
9.90k
    } else if (code > 257) {
85
9.90k
        unsigned int idx = code - 258;
86
9.90k
        if (idx >= table.size()) {
87
0
            throw std::runtime_error("Pl_LZWDecoder::getFirstChar: table overflow");
88
0
        }
89
9.90k
        Buffer& b = table.at(idx);
90
9.90k
        result = b.getBuffer()[0];
91
9.90k
    } else {
92
0
        throw std::runtime_error(
93
0
            "Pl_LZWDecoder::getFirstChar called with invalid code (" + std::to_string(code) + ")");
94
0
    }
95
10.4k
    return result;
96
10.4k
}
97
98
void
99
Pl_LZWDecoder::addToTable(unsigned char c)
100
881k
{
101
881k
    unsigned int last_size = 0;
102
881k
    unsigned char const* last_data = nullptr;
103
881k
    unsigned char tmp[1];
104
105
881k
    if (last_code < 256) {
106
871k
        tmp[0] = static_cast<unsigned char>(last_code);
107
871k
        last_data = tmp;
108
871k
        last_size = 1;
109
871k
    } else if (last_code > 257) {
110
10.2k
        unsigned int idx = last_code - 258;
111
10.2k
        if (idx >= table.size()) {
112
0
            throw std::runtime_error("Pl_LZWDecoder::addToTable: table overflow");
113
0
        }
114
10.2k
        Buffer& b = table.at(idx);
115
10.2k
        last_data = b.getBuffer();
116
10.2k
        last_size = QIntC::to_uint(b.getSize());
117
10.2k
    } 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
881k
    Buffer entry(1 + last_size);
124
881k
    unsigned char* new_data = entry.getBuffer();
125
881k
    memcpy(new_data, last_data, last_size);
126
881k
    new_data[last_size] = c;
127
881k
    table.push_back(std::move(entry));
128
881k
}
129
130
void
131
Pl_LZWDecoder::handleCode(unsigned int code)
132
887k
{
133
887k
    if (eod) {
134
909
        return;
135
909
    }
136
137
886k
    if (code == 256) {
138
2.32k
        if (!table.empty()) {
139
1.47k
            QTC::TC("libtests", "Pl_LZWDecoder intermediate reset");
140
1.47k
        }
141
2.32k
        table.clear();
142
2.32k
        code_size = 9;
143
884k
    } else if (code == 257) {
144
14
        eod = true;
145
884k
    } else {
146
884k
        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
882k
            unsigned char next_c = '\0';
150
882k
            unsigned int table_size = QIntC::to_uint(table.size());
151
882k
            if (code < 256) {
152
                // just read < 256; last time's next_c was code
153
871k
                next_c = static_cast<unsigned char>(code);
154
871k
            } else if (code > 257) {
155
10.5k
                size_t idx = code - 258;
156
10.5k
                if (idx > table_size) {
157
48
                    throw std::runtime_error("LZWDecoder: bad code received");
158
10.4k
                } 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
1.30k
                    QTC::TC("libtests", "Pl_LZWDecoder last was table size");
162
1.30k
                    next_c = getFirstChar(last_code);
163
9.16k
                } else {
164
9.16k
                    next_c = getFirstChar(code);
165
9.16k
                }
166
10.5k
            }
167
881k
            unsigned int new_idx = 258 + table_size;
168
881k
            if (new_idx == 4096) {
169
1
                throw std::runtime_error("LZWDecoder: table full");
170
1
            }
171
881k
            addToTable(next_c);
172
881k
            unsigned int change_idx = new_idx + code_change_delta;
173
881k
            if ((change_idx == 511) || (change_idx == 1023) || (change_idx == 2047)) {
174
1.75k
                ++code_size;
175
1.75k
            }
176
881k
        }
177
178
884k
        if (code < 256) {
179
873k
            auto ch = static_cast<unsigned char>(code);
180
873k
            next()->write(&ch, 1);
181
873k
        } else {
182
10.4k
            unsigned int idx = code - 258;
183
10.4k
            if (idx >= table.size()) {
184
12
                throw std::runtime_error("Pl_LZWDecoder::handleCode: table overflow");
185
12
            }
186
10.4k
            Buffer& b = table.at(idx);
187
10.4k
            next()->write(b.getBuffer(), b.getSize());
188
10.4k
        }
189
884k
    }
190
191
886k
    last_code = code;
192
886k
}