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