Coverage Report

Created: 2025-07-18 06:59

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