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