Coverage Report

Created: 2025-10-10 06:18

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