Coverage Report

Created: 2026-03-12 06:58

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/Util.hh>
6
#include <cstring>
7
#include <stdexcept>
8
9
using namespace qpdf;
10
11
Pl_LZWDecoder::Pl_LZWDecoder(char const* identifier, Pipeline* next, bool early_code_change) :
12
3.21k
    Pipeline(identifier, next),
13
3.21k
    code_change_delta(early_code_change)
14
3.21k
{
15
3.21k
    util::assertion(next, "Attempt to create Pl_LZWDecoder with nullptr as next");
16
3.21k
}
17
18
void
19
Pl_LZWDecoder::write(unsigned char const* bytes, size_t len)
20
78.4k
{
21
1.31M
    for (size_t i = 0; i < len; ++i) {
22
1.23M
        buf[next_char_++] = bytes[i];
23
1.23M
        if (next_char_ == 3) {
24
411k
            next_char_ = 0;
25
411k
        }
26
1.23M
        bits_available += 8;
27
1.23M
        if (bits_available >= code_size) {
28
998k
            sendNextCode();
29
998k
        }
30
1.23M
    }
31
78.4k
}
32
33
void
34
Pl_LZWDecoder::finish()
35
3.14k
{
36
3.14k
    next()->finish();
37
3.14k
}
38
39
void
40
Pl_LZWDecoder::sendNextCode()
41
998k
{
42
998k
    unsigned int high = byte_pos;
43
998k
    unsigned int med = (byte_pos + 1) % 3;
44
998k
    unsigned int low = (byte_pos + 2) % 3;
45
46
998k
    unsigned int bits_from_high = 8 - bit_pos;
47
998k
    unsigned int bits_from_med = code_size - bits_from_high;
48
998k
    unsigned int bits_from_low = 0;
49
998k
    if (bits_from_med > 8) {
50
117k
        bits_from_low = bits_from_med - 8;
51
117k
        bits_from_med = 8;
52
117k
    }
53
998k
    unsigned int high_mask = (1U << bits_from_high) - 1U;
54
998k
    unsigned int med_mask = 0xff - ((1U << (8 - bits_from_med)) - 1U);
55
998k
    unsigned int low_mask = 0xff - ((1U << (8 - bits_from_low)) - 1U);
56
998k
    unsigned int code = 0;
57
998k
    code += (buf[high] & high_mask) << bits_from_med;
58
998k
    code += ((buf[med] & med_mask) >> (8 - bits_from_med));
59
998k
    if (bits_from_low) {
60
117k
        code <<= bits_from_low;
61
117k
        code += ((buf[low] & low_mask) >> (8 - bits_from_low));
62
117k
        byte_pos = low;
63
117k
        bit_pos = bits_from_low;
64
881k
    } else {
65
881k
        byte_pos = med;
66
881k
        bit_pos = bits_from_med;
67
881k
    }
68
998k
    if (bit_pos == 8) {
69
117k
        bit_pos = 0;
70
117k
        ++byte_pos;
71
117k
        byte_pos %= 3;
72
117k
    }
73
998k
    bits_available -= code_size;
74
75
998k
    handleCode(code);
76
998k
}
77
78
unsigned char
79
Pl_LZWDecoder::getFirstChar(unsigned int code)
80
2.60k
{
81
2.60k
    if (code < 256) {
82
92
        return static_cast<unsigned char>(code);
83
92
    }
84
2.51k
    util::no_ci_rt_error_if(
85
2.51k
        code <= 257,
86
2.51k
        "Pl_LZWDecoder::getFirstChar called with invalid code (" + std::to_string(code) + ")");
87
88
2.51k
    unsigned int idx = code - 258;
89
2.51k
    util::no_ci_rt_error_if(idx >= table.size(), "Pl_LZWDecoder::getFirstChar: table overflow");
90
2.51k
    Buffer& b = table.at(idx);
91
2.51k
    return b.getBuffer()[0];
92
2.60k
}
93
94
void
95
Pl_LZWDecoder::addToTable(unsigned char c)
96
971k
{
97
971k
    unsigned int last_size = 0;
98
971k
    unsigned char const* last_data = nullptr;
99
971k
    unsigned char tmp[1];
100
101
971k
    if (last_code < 256) {
102
969k
        tmp[0] = static_cast<unsigned char>(last_code);
103
969k
        last_data = tmp;
104
969k
        last_size = 1;
105
969k
    } else {
106
2.58k
        util::no_ci_rt_error_if(
107
2.58k
            last_code <= 257,
108
2.58k
            "Pl_LZWDecoder::addToTable called with invalid code (" + std::to_string(last_code) +
109
2.58k
                ")");
110
2.58k
        unsigned int idx = last_code - 258;
111
2.58k
        util::no_ci_rt_error_if(idx >= table.size(), "Pl_LZWDecoder::addToTable: table overflow");
112
2.58k
        Buffer& b = table.at(idx);
113
2.58k
        last_data = b.getBuffer();
114
2.58k
        last_size = QIntC::to_uint(b.getSize());
115
2.58k
    }
116
117
971k
    Buffer entry(1 + last_size);
118
971k
    unsigned char* new_data = entry.getBuffer();
119
971k
    memcpy(new_data, last_data, last_size);
120
971k
    new_data[last_size] = c;
121
971k
    table.push_back(std::move(entry));
122
971k
}
123
124
void
125
Pl_LZWDecoder::handleCode(unsigned int code)
126
998k
{
127
998k
    if (eod) {
128
3.23k
        return;
129
3.23k
    }
130
131
995k
    if (code == 256) {
132
11.3k
        if (!table.empty()) {
133
10.9k
            QTC::TC("libtests", "Pl_LZWDecoder intermediate reset");
134
10.9k
        }
135
11.3k
        table.clear();
136
11.3k
        code_size = 9;
137
984k
    } else if (code == 257) {
138
39
        eod = true;
139
984k
    } else {
140
984k
        if (last_code != 256) {
141
            // Add to the table from last time.  New table entry would be what we read last plus the
142
            // first character of what we're reading now.
143
972k
            unsigned char next_c = '\0';
144
972k
            unsigned int table_size = QIntC::to_uint(table.size());
145
972k
            if (code < 256) {
146
                // just read < 256; last time's next_c was code
147
969k
                next_c = static_cast<unsigned char>(code);
148
969k
            } else if (code > 257) {
149
2.79k
                size_t idx = code - 258;
150
2.79k
                if (idx > table_size) {
151
191
                    throw std::runtime_error("LZWDecoder: bad code received");
152
2.60k
                } else if (idx == table_size) {
153
                    // The encoder would have just created this entry, so the first character of
154
                    // this entry would have been the same as the first character of the last entry.
155
94
                    next_c = getFirstChar(last_code);
156
2.51k
                } else {
157
2.51k
                    next_c = getFirstChar(code);
158
2.51k
                }
159
2.79k
            }
160
971k
            unsigned int new_idx = 258 + table_size;
161
971k
            util::no_ci_rt_error_if(new_idx == 4096, "LZWDecoder: table full");
162
971k
            addToTable(next_c);
163
971k
            unsigned int change_idx = new_idx + code_change_delta;
164
971k
            if (change_idx == 511 || change_idx == 1023 || change_idx == 2047) {
165
1.37k
                ++code_size;
166
1.37k
            }
167
971k
        }
168
169
983k
        if (code < 256) {
170
981k
            auto ch = static_cast<unsigned char>(code);
171
981k
            next()->write(&ch, 1);
172
981k
        } else {
173
2.66k
            unsigned int idx = code - 258;
174
2.66k
            if (idx >= table.size()) {
175
45
                throw std::runtime_error("Pl_LZWDecoder::handleCode: table overflow");
176
45
            }
177
2.61k
            Buffer& b = table.at(idx);
178
2.61k
            next()->write(b.getBuffer(), b.getSize());
179
2.61k
        }
180
983k
    }
181
182
995k
    last_code = code;
183
995k
}