Coverage Report

Created: 2025-11-24 06:47

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