Coverage Report

Created: 2025-06-22 06:27

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