Coverage Report

Created: 2025-10-12 07:05

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