/src/libjxl/lib/jxl/icc_codec.cc
Line | Count | Source |
1 | | // Copyright (c) the JPEG XL Project Authors. All rights reserved. |
2 | | // |
3 | | // Use of this source code is governed by a BSD-style |
4 | | // license that can be found in the LICENSE file. |
5 | | |
6 | | #include "lib/jxl/icc_codec.h" |
7 | | |
8 | | #include <jxl/memory_manager.h> |
9 | | |
10 | | #include <algorithm> |
11 | | #include <cstddef> |
12 | | #include <cstdint> |
13 | | |
14 | | #include "lib/jxl/base/common.h" |
15 | | #include "lib/jxl/base/status.h" |
16 | | #include "lib/jxl/dec_ans.h" |
17 | | #include "lib/jxl/dec_bit_reader.h" |
18 | | #include "lib/jxl/fields.h" |
19 | | #include "lib/jxl/icc_codec_common.h" |
20 | | #include "lib/jxl/padded_bytes.h" |
21 | | |
22 | | namespace jxl { |
23 | | namespace { |
24 | | |
25 | | // Shuffles or interleaves bytes, for example with width 2, turns "ABCDabcd" |
26 | | // into "AaBbCcDd". Transposes a matrix of ceil(size / width) columns and |
27 | | // width rows. There are size elements, size may be < width * height, if so the |
28 | | // last elements of the rightmost column are missing, the missing spots are |
29 | | // transposed along with the filled spots, and the result has the missing |
30 | | // elements at the end of the bottom row. The input is the input matrix in |
31 | | // scanline order but with missing elements skipped (which may occur in multiple |
32 | | // locations), the output is the result matrix in scanline order (with |
33 | | // no need to skip missing elements as they are past the end of the data). |
34 | | Status Shuffle(JxlMemoryManager* memory_manager, uint8_t* data, size_t size, |
35 | 12.2k | size_t width) { |
36 | 12.2k | size_t height = (size + width - 1) / width; // amount of rows of output |
37 | 12.2k | PaddedBytes result(memory_manager); |
38 | 12.2k | JXL_ASSIGN_OR_RETURN(result, |
39 | 12.2k | PaddedBytes::WithInitialSpace(memory_manager, size)); |
40 | | // i = output index, j input index |
41 | 12.2k | size_t s = 0; |
42 | 12.2k | size_t j = 0; |
43 | 2.10M | for (size_t i = 0; i < size; i++) { |
44 | 2.09M | result[i] = data[j]; |
45 | 2.09M | j += height; |
46 | 2.09M | if (j >= size) j = ++s; |
47 | 2.09M | } |
48 | | |
49 | 2.10M | for (size_t i = 0; i < size; i++) { |
50 | 2.09M | data[i] = result[i]; |
51 | 2.09M | } |
52 | 12.2k | return true; |
53 | 12.2k | } |
54 | | |
55 | | // Two base-128 varints at up to 10 bytes each. |
56 | | constexpr size_t kPreambleSize = 20; |
57 | | |
58 | | // Decodes a base-128 unsigned varint into *out, advancing *pos by the exact |
59 | | // number of bytes consumed. Returns an error if the input is truncated, if |
60 | | // the terminator (top-bit-clear byte) is not seen within 10 bytes, or if the |
61 | | // 10th byte encodes a value that does not fit in a uint64_t. |
62 | | Status DecodeVarInt(const uint8_t* input, size_t inputSize, size_t* pos, |
63 | 88.3k | uint64_t* out) { |
64 | 88.3k | uint64_t ret = 0; |
65 | | // 9 bytes cover bits 0..62; the 10th byte may only contribute bit 63. |
66 | 118k | for (size_t i = 0; i < 9; ++i) { |
67 | 118k | if (*pos >= inputSize) { |
68 | 0 | return JXL_FAILURE("DecodeVarInt: truncated input"); |
69 | 0 | } |
70 | 118k | const uint8_t byte = input[(*pos)++]; |
71 | 118k | ret |= static_cast<uint64_t>(byte & 0x7F) << (7 * i); |
72 | 118k | if ((byte & 0x80) == 0) { |
73 | 88.3k | *out = ret; |
74 | 88.3k | return true; |
75 | 88.3k | } |
76 | 118k | } |
77 | 0 | if (*pos >= inputSize) { |
78 | 0 | return JXL_FAILURE("DecodeVarInt: truncated input (10th byte)"); |
79 | 0 | } |
80 | 0 | const uint8_t byte = input[(*pos)++]; |
81 | 0 | if ((byte & 0x80) != 0) { |
82 | 0 | return JXL_FAILURE("DecodeVarInt: varint exceeds 10 bytes"); |
83 | 0 | } |
84 | 0 | if ((byte & 0x7E) != 0) { |
85 | 0 | return JXL_FAILURE("DecodeVarInt: value exceeds 2^64 - 1"); |
86 | 0 | } |
87 | 0 | ret |= static_cast<uint64_t>(byte & 0x01) << 63; |
88 | 0 | *out = ret; |
89 | 0 | return true; |
90 | 0 | } |
91 | | |
92 | | } // namespace |
93 | | |
94 | | // Mimics the beginning of UnpredictICC for quick validity check. |
95 | | // At least kPreambleSize bytes of data should be valid at invocation time. |
96 | 4.62k | Status CheckPreamble(const PaddedBytes& data, size_t enc_size) { |
97 | 4.62k | const uint8_t* enc = data.data(); |
98 | 4.62k | size_t size = data.size(); |
99 | 4.62k | size_t pos = 0; |
100 | 4.62k | uint64_t osize; |
101 | 4.62k | JXL_RETURN_IF_ERROR(DecodeVarInt(enc, size, &pos, &osize)); |
102 | 4.62k | JXL_RETURN_IF_ERROR(CheckIs32Bit(osize)); |
103 | 4.62k | uint64_t csize; |
104 | 4.62k | JXL_RETURN_IF_ERROR(DecodeVarInt(enc, size, &pos, &csize)); |
105 | 4.62k | JXL_RETURN_IF_ERROR(CheckIs32Bit(csize)); |
106 | 4.62k | JXL_RETURN_IF_ERROR(CheckOutOfBounds(pos, csize, size)); |
107 | | // We expect that UnpredictICC inflates input, not the other way round. |
108 | 4.62k | if (osize + 65536 < enc_size) return JXL_FAILURE("Malformed ICC"); |
109 | | |
110 | | // NB(eustas): 64 MiB ICC should be enough for everything!? |
111 | 4.62k | const size_t output_limit = 1 << 28; |
112 | 4.62k | if (output_limit && osize > output_limit) { |
113 | 0 | return JXL_FAILURE("Decoded ICC is too large"); |
114 | 0 | } |
115 | 4.62k | return true; |
116 | 4.62k | } |
117 | | |
118 | | // Decodes the result of PredictICC back to a valid ICC profile. |
119 | 4.60k | Status UnpredictICC(const uint8_t* enc, size_t size, PaddedBytes* result) { |
120 | 4.60k | if (!result->empty()) return JXL_FAILURE("result must be empty initially"); |
121 | 4.60k | JxlMemoryManager* memory_manager = result->memory_manager(); |
122 | 4.60k | size_t pos = 0; |
123 | 4.60k | uint64_t osize; |
124 | 4.60k | JXL_RETURN_IF_ERROR(DecodeVarInt(enc, size, &pos, &osize)); // Output size |
125 | 4.60k | JXL_RETURN_IF_ERROR(CheckIs32Bit(osize)); |
126 | 4.60k | uint64_t csize; |
127 | 4.60k | JXL_RETURN_IF_ERROR(DecodeVarInt(enc, size, &pos, &csize)); // Commands size |
128 | | // Every command is translated to at least one byte. |
129 | 4.60k | JXL_RETURN_IF_ERROR(CheckIs32Bit(csize)); |
130 | 4.60k | size_t cpos = pos; // pos in commands stream |
131 | 4.60k | JXL_RETURN_IF_ERROR(CheckOutOfBounds(pos, csize, size)); |
132 | 4.60k | size_t commands_end = cpos + csize; |
133 | 4.60k | pos = commands_end; // pos in data stream |
134 | | |
135 | | // Header |
136 | 4.60k | PaddedBytes header{memory_manager}; |
137 | 4.60k | JXL_RETURN_IF_ERROR(header.append(ICCInitialHeaderPrediction(osize))); |
138 | 593k | for (size_t i = 0; i <= kICCHeaderSize; i++) { |
139 | 593k | if (result->size() == osize) { |
140 | 3 | if (cpos != commands_end) return JXL_FAILURE("Not all commands used"); |
141 | 3 | if (pos != size) return JXL_FAILURE("Not all data used"); |
142 | 3 | return true; // Valid end |
143 | 3 | } |
144 | 593k | if (i == kICCHeaderSize) break; // Done |
145 | 589k | ICCPredictHeader(result->data(), result->size(), header.data(), i); |
146 | 589k | if (pos >= size) return JXL_FAILURE("Out of bounds"); |
147 | 589k | JXL_RETURN_IF_ERROR(result->push_back(enc[pos++] + header[i])); |
148 | 589k | } |
149 | 4.60k | if (cpos >= commands_end) return JXL_FAILURE("Out of bounds"); |
150 | | |
151 | | // Tag list |
152 | 4.60k | uint64_t numtags; |
153 | 4.60k | JXL_RETURN_IF_ERROR(DecodeVarInt(enc, commands_end, &cpos, &numtags)); |
154 | | |
155 | 4.60k | if (numtags != 0) { |
156 | 4.60k | numtags--; |
157 | 4.60k | JXL_RETURN_IF_ERROR(CheckIs32Bit(numtags)); |
158 | 4.60k | JXL_RETURN_IF_ERROR(AppendUint32(numtags, result)); |
159 | 4.60k | uint64_t prevtagstart = kICCHeaderSize + numtags * 12; |
160 | 4.60k | uint64_t prevtagsize = 0; |
161 | 43.0k | for (;;) { |
162 | 43.0k | if (result->size() > osize) return JXL_FAILURE("Invalid result size"); |
163 | 43.0k | if (cpos > commands_end) return JXL_FAILURE("Out of bounds"); |
164 | 43.0k | if (cpos == commands_end) break; // Valid end |
165 | 43.0k | uint8_t command = enc[cpos++]; |
166 | 43.0k | uint8_t tagcode = command & 63; |
167 | 43.0k | Tag tag; |
168 | 43.0k | if (tagcode == 0) { |
169 | 4.60k | break; |
170 | 38.3k | } else if (tagcode == kCommandTagUnknown) { |
171 | 2.98k | JXL_RETURN_IF_ERROR(CheckOutOfBounds(pos, 4, size)); |
172 | 2.98k | tag = DecodeKeyword(enc, size, pos); |
173 | 2.98k | pos += 4; |
174 | 35.4k | } else if (tagcode == kCommandTagTRC) { |
175 | 4.32k | tag = kRtrcTag; |
176 | 31.0k | } else if (tagcode == kCommandTagXYZ) { |
177 | 2.60k | tag = kRxyzTag; |
178 | 28.4k | } else { |
179 | 28.4k | if (tagcode - kCommandTagStringFirst >= kNumTagStrings) { |
180 | 0 | return JXL_FAILURE("Unknown tagcode"); |
181 | 0 | } |
182 | 28.4k | tag = *kTagStrings[tagcode - kCommandTagStringFirst]; |
183 | 28.4k | } |
184 | 38.3k | JXL_RETURN_IF_ERROR(AppendKeyword(tag, result)); |
185 | | |
186 | 38.3k | uint64_t tagstart; |
187 | 38.3k | uint64_t tagsize = prevtagsize; |
188 | 38.3k | if (tag == kRxyzTag || tag == kGxyzTag || tag == kBxyzTag || |
189 | 30.6k | tag == kKxyzTag || tag == kWtptTag || tag == kBkptTag || |
190 | 25.8k | tag == kLumiTag) { |
191 | 12.5k | tagsize = 20; |
192 | 12.5k | } |
193 | | |
194 | 38.3k | if (command & kFlagBitOffset) { |
195 | 14.0k | JXL_RETURN_IF_ERROR(DecodeVarInt(enc, commands_end, &cpos, &tagstart)); |
196 | 24.3k | } else { |
197 | 24.3k | JXL_RETURN_IF_ERROR(CheckIs32Bit(prevtagstart)); |
198 | 24.3k | tagstart = prevtagstart + prevtagsize; |
199 | 24.3k | } |
200 | 38.3k | JXL_RETURN_IF_ERROR(CheckIs32Bit(tagstart)); |
201 | 38.3k | JXL_RETURN_IF_ERROR(AppendUint32(tagstart, result)); |
202 | 38.3k | if (command & kFlagBitSize) { |
203 | 23.2k | JXL_RETURN_IF_ERROR(DecodeVarInt(enc, commands_end, &cpos, &tagsize)); |
204 | 23.2k | } |
205 | 38.3k | JXL_RETURN_IF_ERROR(CheckIs32Bit(tagsize)); |
206 | 38.3k | JXL_RETURN_IF_ERROR(AppendUint32(tagsize, result)); |
207 | 38.3k | prevtagstart = tagstart; |
208 | 38.3k | prevtagsize = tagsize; |
209 | | |
210 | 38.3k | if (tagcode == kCommandTagTRC) { |
211 | 4.32k | JXL_RETURN_IF_ERROR(AppendKeyword(kGtrcTag, result)); |
212 | 4.32k | JXL_RETURN_IF_ERROR(AppendUint32(tagstart, result)); |
213 | 4.32k | JXL_RETURN_IF_ERROR(AppendUint32(tagsize, result)); |
214 | 4.32k | JXL_RETURN_IF_ERROR(AppendKeyword(kBtrcTag, result)); |
215 | 4.32k | JXL_RETURN_IF_ERROR(AppendUint32(tagstart, result)); |
216 | 4.32k | JXL_RETURN_IF_ERROR(AppendUint32(tagsize, result)); |
217 | 4.32k | } |
218 | | |
219 | 38.3k | if (tagcode == kCommandTagXYZ) { |
220 | 2.60k | JXL_RETURN_IF_ERROR(CheckIs32Bit(tagstart + tagsize * 2)); |
221 | 2.60k | JXL_RETURN_IF_ERROR(AppendKeyword(kGxyzTag, result)); |
222 | 2.60k | JXL_RETURN_IF_ERROR(AppendUint32(tagstart + tagsize, result)); |
223 | 2.60k | JXL_RETURN_IF_ERROR(AppendUint32(tagsize, result)); |
224 | 2.60k | JXL_RETURN_IF_ERROR(AppendKeyword(kBxyzTag, result)); |
225 | 2.60k | JXL_RETURN_IF_ERROR(AppendUint32(tagstart + tagsize * 2, result)); |
226 | 2.60k | JXL_RETURN_IF_ERROR(AppendUint32(tagsize, result)); |
227 | 2.60k | } |
228 | 38.3k | } |
229 | 4.60k | } |
230 | | |
231 | | // Main Content |
232 | 72.4k | for (;;) { |
233 | 72.4k | if (result->size() > osize) return JXL_FAILURE("Invalid result size"); |
234 | 72.4k | if (cpos > commands_end) return JXL_FAILURE("Out of bounds"); |
235 | 72.4k | if (cpos == commands_end) break; // Valid end |
236 | 67.8k | uint8_t command = enc[cpos++]; |
237 | 67.8k | if (command == kCommandInsert) { |
238 | 15.7k | uint64_t num; |
239 | 15.7k | JXL_RETURN_IF_ERROR(DecodeVarInt(enc, commands_end, &cpos, &num)); |
240 | 15.7k | JXL_RETURN_IF_ERROR(CheckOutOfBounds(pos, num, size)); |
241 | 28.9M | for (size_t i = 0; i < num; i++) { |
242 | 28.9M | JXL_RETURN_IF_ERROR(result->push_back(enc[pos++])); |
243 | 28.9M | } |
244 | 52.1k | } else if (command == kCommandShuffle2 || command == kCommandShuffle4) { |
245 | 12.2k | uint64_t num; |
246 | 12.2k | JXL_RETURN_IF_ERROR(DecodeVarInt(enc, commands_end, &cpos, &num)); |
247 | 12.2k | JXL_RETURN_IF_ERROR(CheckOutOfBounds(pos, num, size)); |
248 | 12.2k | PaddedBytes shuffled(memory_manager); |
249 | 12.2k | JXL_ASSIGN_OR_RETURN(shuffled, |
250 | 12.2k | PaddedBytes::WithInitialSpace(memory_manager, num)); |
251 | 2.10M | for (size_t i = 0; i < num; i++) { |
252 | 2.09M | shuffled[i] = enc[pos + i]; |
253 | 2.09M | } |
254 | 12.2k | if (command == kCommandShuffle2) { |
255 | 12.2k | JXL_RETURN_IF_ERROR(Shuffle(memory_manager, shuffled.data(), num, 2)); |
256 | 12.2k | } else if (command == kCommandShuffle4) { |
257 | 2 | JXL_RETURN_IF_ERROR(Shuffle(memory_manager, shuffled.data(), num, 4)); |
258 | 2 | } |
259 | 2.10M | for (size_t i = 0; i < num; i++) { |
260 | 2.09M | JXL_RETURN_IF_ERROR(result->push_back(shuffled[i])); |
261 | 2.09M | pos++; |
262 | 2.09M | } |
263 | 39.9k | } else if (command == kCommandPredict) { |
264 | 0 | JXL_RETURN_IF_ERROR(CheckOutOfBounds(cpos, 2, commands_end)); |
265 | 0 | uint8_t flags = enc[cpos++]; |
266 | |
|
267 | 0 | size_t width = (flags & 3) + 1; |
268 | 0 | if (width == 3) return JXL_FAILURE("Invalid width"); |
269 | | |
270 | 0 | int order = (flags & 12) >> 2; |
271 | 0 | if (order == 3) return JXL_FAILURE("Invalid order"); |
272 | | |
273 | 0 | uint64_t stride = width; |
274 | 0 | if (flags & 16) { |
275 | 0 | JXL_RETURN_IF_ERROR(DecodeVarInt(enc, commands_end, &cpos, &stride)); |
276 | 0 | if (stride < width) { |
277 | 0 | return JXL_FAILURE("Invalid stride"); |
278 | 0 | } |
279 | 0 | } |
280 | | // If stride * 4 >= result->size(), return failure. The check |
281 | | // "size == 0 || ((size - 1) >> 2) < stride" corresponds to |
282 | | // "stride * 4 >= size", but does not suffer from integer overflow. |
283 | | // This check is more strict than necessary but follows the specification |
284 | | // and the encoder should ensure this is followed. |
285 | 0 | if (result->empty() || ((result->size() - 1u) >> 2u) < stride) { |
286 | 0 | return JXL_FAILURE("Invalid stride"); |
287 | 0 | } |
288 | | |
289 | 0 | uint64_t num; |
290 | 0 | JXL_RETURN_IF_ERROR(DecodeVarInt(enc, commands_end, &cpos, &num)); // in bytes |
291 | 0 | JXL_RETURN_IF_ERROR(CheckOutOfBounds(pos, num, size)); |
292 | | |
293 | 0 | PaddedBytes shuffled(memory_manager); |
294 | 0 | JXL_ASSIGN_OR_RETURN(shuffled, |
295 | 0 | PaddedBytes::WithInitialSpace(memory_manager, num)); |
296 | |
|
297 | 0 | for (size_t i = 0; i < num; i++) { |
298 | 0 | shuffled[i] = enc[pos + i]; |
299 | 0 | } |
300 | 0 | if (width > 1) { |
301 | 0 | JXL_RETURN_IF_ERROR( |
302 | 0 | Shuffle(memory_manager, shuffled.data(), num, width)); |
303 | 0 | } |
304 | | |
305 | 0 | size_t start = result->size(); |
306 | 0 | for (size_t i = 0; i < num; i++) { |
307 | 0 | uint8_t predicted = LinearPredictICCValue(result->data(), start, i, |
308 | 0 | stride, width, order); |
309 | 0 | JXL_RETURN_IF_ERROR(result->push_back(predicted + shuffled[i])); |
310 | 0 | } |
311 | 0 | pos += num; |
312 | 39.9k | } else if (command == kCommandXYZ) { |
313 | 17.7k | JXL_RETURN_IF_ERROR(AppendKeyword(kXyz_Tag, result)); |
314 | 88.9k | for (int i = 0; i < 4; i++) { |
315 | 71.1k | JXL_RETURN_IF_ERROR(result->push_back(0)); |
316 | 71.1k | } |
317 | 17.7k | JXL_RETURN_IF_ERROR(CheckOutOfBounds(pos, 12, size)); |
318 | 231k | for (size_t i = 0; i < 12; i++) { |
319 | 213k | JXL_RETURN_IF_ERROR(result->push_back(enc[pos++])); |
320 | 213k | } |
321 | 22.1k | } else if (command >= kCommandTypeStartFirst && |
322 | 22.1k | command < kCommandTypeStartFirst + kNumTypeStrings) { |
323 | 22.1k | JXL_RETURN_IF_ERROR(AppendKeyword( |
324 | 22.1k | *kTypeStrings[command - kCommandTypeStartFirst], result)); |
325 | 110k | for (size_t i = 0; i < 4; i++) { |
326 | 88.5k | JXL_RETURN_IF_ERROR(result->push_back(0)); |
327 | 88.5k | } |
328 | 22.1k | } else { |
329 | 1 | return JXL_FAILURE("Unknown command"); |
330 | 1 | } |
331 | 67.8k | } |
332 | | |
333 | 4.60k | if (pos != size) return JXL_FAILURE("Not all data used"); |
334 | 4.60k | if (result->size() != osize) return JXL_FAILURE("Invalid result size"); |
335 | | |
336 | 4.60k | return true; |
337 | 4.60k | } |
338 | | |
339 | 4.74k | Status ICCReader::Init(BitReader* reader) { |
340 | 4.74k | JXL_RETURN_IF_ERROR(CheckEOI(reader)); |
341 | 4.74k | JxlMemoryManager* memory_manager = decompressed_.memory_manager(); |
342 | 4.74k | used_bits_base_ = reader->TotalBitsConsumed(); |
343 | 4.74k | if (bits_to_skip_ == 0) { |
344 | 4.63k | enc_size_ = U64Coder::Read(reader); |
345 | 4.63k | if (enc_size_ > 268435456) { |
346 | | // Avoid too large memory allocation for invalid file. |
347 | 0 | return JXL_FAILURE("Too large encoded profile"); |
348 | 0 | } |
349 | 4.63k | JXL_RETURN_IF_ERROR(DecodeHistograms( |
350 | 4.63k | memory_manager, reader, kNumICCContexts, &code_, &context_map_)); |
351 | 9.26k | JXL_ASSIGN_OR_RETURN(ans_reader_, ANSSymbolReader::Create(&code_, reader)); |
352 | 9.26k | i_ = 0; |
353 | 9.26k | JXL_RETURN_IF_ERROR( |
354 | 9.26k | decompressed_.resize(std::min<size_t>(i_ + 0x400, enc_size_))); |
355 | 13.8k | for (; i_ < std::min<size_t>(2, enc_size_); i_++) { |
356 | 9.26k | decompressed_[i_] = ans_reader_.ReadHybridUint( |
357 | 9.26k | ICCANSContext(i_, i_ > 0 ? decompressed_[i_ - 1] : 0, |
358 | 9.26k | i_ > 1 ? decompressed_[i_ - 2] : 0), |
359 | 9.26k | reader, context_map_); |
360 | 9.26k | } |
361 | 4.63k | if (enc_size_ > kPreambleSize) { |
362 | 87.9k | for (; i_ < kPreambleSize; i_++) { |
363 | 83.3k | decompressed_[i_] = ans_reader_.ReadHybridUint( |
364 | 83.3k | ICCANSContext(i_, decompressed_[i_ - 1], decompressed_[i_ - 2]), |
365 | 83.3k | reader, context_map_); |
366 | 83.3k | } |
367 | 4.62k | JXL_RETURN_IF_ERROR(CheckEOI(reader)); |
368 | 4.62k | JXL_RETURN_IF_ERROR(CheckPreamble(decompressed_, enc_size_)); |
369 | 4.62k | } |
370 | 4.63k | bits_to_skip_ = reader->TotalBitsConsumed() - used_bits_base_; |
371 | 4.63k | } else { |
372 | 114 | reader->SkipBits(bits_to_skip_); |
373 | 114 | } |
374 | 4.74k | return true; |
375 | 4.74k | } |
376 | | |
377 | 4.74k | Status ICCReader::Process(BitReader* reader, PaddedBytes* icc) { |
378 | 4.74k | auto checkpoint = jxl::make_unique<ANSSymbolReader::Checkpoint>(); |
379 | 4.74k | size_t saved_i = 0; |
380 | 98.3k | auto save = [&]() { |
381 | 98.3k | ans_reader_.Save(checkpoint.get()); |
382 | 98.3k | bits_to_skip_ = reader->TotalBitsConsumed() - used_bits_base_; |
383 | 98.3k | saved_i = i_; |
384 | 98.3k | }; |
385 | 4.74k | save(); |
386 | 98.3k | auto check_and_restore = [&]() -> Status { |
387 | 98.3k | Status status = CheckEOI(reader); |
388 | 98.3k | if (!status) { |
389 | | // not enough bytes. |
390 | 138 | ans_reader_.Restore(*checkpoint); |
391 | 138 | i_ = saved_i; |
392 | 138 | return status; |
393 | 138 | } |
394 | 98.2k | return true; |
395 | 98.3k | }; |
396 | 49.3M | for (; i_ < enc_size_; i_++) { |
397 | 49.3M | if (i_ % ANSSymbolReader::kMaxCheckpointInterval == 0 && i_ > 0) { |
398 | 93.6k | JXL_RETURN_IF_ERROR(check_and_restore()); |
399 | 93.6k | save(); |
400 | 93.6k | if ((i_ > 0) && (((i_ & 0xFFFF) == 0))) { |
401 | 503 | float used_bytes = |
402 | 503 | (reader->TotalBitsConsumed() - used_bits_base_) / 8.0f; |
403 | 503 | if (i_ > used_bytes * 256) return JXL_FAILURE("Corrupted stream"); |
404 | 503 | } |
405 | 93.6k | JXL_RETURN_IF_ERROR( |
406 | 93.6k | decompressed_.resize(std::min<size_t>(i_ + 0x400, enc_size_))); |
407 | 93.6k | } |
408 | 49.3M | JXL_ENSURE(i_ >= 2); |
409 | 49.3M | decompressed_[i_] = ans_reader_.ReadHybridUint( |
410 | 49.3M | ICCANSContext(i_, decompressed_[i_ - 1], decompressed_[i_ - 2]), reader, |
411 | 49.3M | context_map_); |
412 | 49.3M | } |
413 | 4.71k | JXL_RETURN_IF_ERROR(check_and_restore()); |
414 | 4.60k | bits_to_skip_ = reader->TotalBitsConsumed() - used_bits_base_; |
415 | 4.60k | if (!ans_reader_.CheckANSFinalState()) { |
416 | 0 | return JXL_FAILURE("Corrupted ICC profile"); |
417 | 0 | } |
418 | | |
419 | 4.60k | icc->clear(); |
420 | 4.60k | return UnpredictICC(decompressed_.data(), decompressed_.size(), icc); |
421 | 4.60k | } |
422 | | |
423 | 107k | Status ICCReader::CheckEOI(BitReader* reader) { |
424 | 107k | if (reader->AllReadsWithinBounds()) return true; |
425 | 138 | return JXL_NOT_ENOUGH_BYTES("Not enough bytes for reading ICC profile"); |
426 | 107k | } |
427 | | |
428 | | } // namespace jxl |