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