Coverage Report

Created: 2026-08-13 07:14

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