Coverage Report

Created: 2025-07-14 06:20

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