Coverage Report

Created: 2025-07-11 06:58

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