Coverage Report

Created: 2025-06-22 06:33

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