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