/src/libjxl/lib/jxl/icc_codec.cc
Line | Count | Source (jump to first uncovered line) |
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 | 439k | size_t width) { |
36 | 439k | size_t height = (size + width - 1) / width; // amount of rows of output |
37 | 439k | PaddedBytes result(memory_manager); |
38 | 439k | JXL_ASSIGN_OR_RETURN(result, |
39 | 439k | PaddedBytes::WithInitialSpace(memory_manager, size)); |
40 | | // i = output index, j input index |
41 | 439k | size_t s = 0; |
42 | 439k | size_t j = 0; |
43 | 34.9M | for (size_t i = 0; i < size; i++) { |
44 | 34.5M | result[i] = data[j]; |
45 | 34.5M | j += height; |
46 | 34.5M | if (j >= size) j = ++s; |
47 | 34.5M | } |
48 | | |
49 | 34.9M | for (size_t i = 0; i < size; i++) { |
50 | 34.5M | data[i] = result[i]; |
51 | 34.5M | } |
52 | 439k | return true; |
53 | 439k | } |
54 | | |
55 | | // TODO(eustas): should be 20, or even 18, once DecodeVarInt is improved; |
56 | | // currently DecodeVarInt does not signal the errors, and marks |
57 | | // 11 bytes as used even if only 10 are used (and 9 is enough for |
58 | | // 63-bit values). |
59 | | constexpr const size_t kPreambleSize = 22; // enough for reading 2 VarInts |
60 | | |
61 | 14.2M | uint64_t DecodeVarInt(const uint8_t* input, size_t inputSize, size_t* pos) { |
62 | 14.2M | size_t i; |
63 | 14.2M | uint64_t ret = 0; |
64 | 60.8M | for (i = 0; *pos + i < inputSize && i < 10; ++i) { |
65 | 60.8M | ret |= static_cast<uint64_t>(input[*pos + i] & 127) |
66 | 60.8M | << static_cast<uint64_t>(7 * i); |
67 | | // If the next-byte flag is not set, stop |
68 | 60.8M | if ((input[*pos + i] & 128) == 0) break; |
69 | 60.8M | } |
70 | | // TODO(user): Return a decoding error if i == 10. |
71 | 14.2M | *pos += i + 1; |
72 | 14.2M | return ret; |
73 | 14.2M | } |
74 | | |
75 | | } // namespace |
76 | | |
77 | | // Mimics the beginning of UnpredictICC for quick validity check. |
78 | | // At least kPreambleSize bytes of data should be valid at invocation time. |
79 | 3.33k | Status CheckPreamble(const PaddedBytes& data, size_t enc_size) { |
80 | 3.33k | const uint8_t* enc = data.data(); |
81 | 3.33k | size_t size = data.size(); |
82 | 3.33k | size_t pos = 0; |
83 | 3.33k | uint64_t osize = DecodeVarInt(enc, size, &pos); |
84 | 3.33k | JXL_RETURN_IF_ERROR(CheckIs32Bit(osize)); |
85 | 3.24k | if (pos >= size) return JXL_FAILURE("Out of bounds"); |
86 | 3.24k | uint64_t csize = DecodeVarInt(enc, size, &pos); |
87 | 3.24k | JXL_RETURN_IF_ERROR(CheckIs32Bit(csize)); |
88 | 3.22k | JXL_RETURN_IF_ERROR(CheckOutOfBounds(pos, csize, size)); |
89 | | // We expect that UnpredictICC inflates input, not the other way round. |
90 | 3.17k | if (osize + 65536 < enc_size) return JXL_FAILURE("Malformed ICC"); |
91 | | |
92 | | // NB(eustas): 64 MiB ICC should be enough for everything!? |
93 | 3.13k | const size_t output_limit = 1 << 28; |
94 | 3.13k | if (output_limit && osize > output_limit) { |
95 | 21 | return JXL_FAILURE("Decoded ICC is too large"); |
96 | 21 | } |
97 | 3.11k | return true; |
98 | 3.13k | } |
99 | | |
100 | | // Decodes the result of PredictICC back to a valid ICC profile. |
101 | 7.06k | Status UnpredictICC(const uint8_t* enc, size_t size, PaddedBytes* result) { |
102 | 7.06k | if (!result->empty()) return JXL_FAILURE("result must be empty initially"); |
103 | 7.06k | JxlMemoryManager* memory_manager = result->memory_manager(); |
104 | 7.06k | size_t pos = 0; |
105 | | // TODO(lode): technically speaking we need to check that the entire varint |
106 | | // decoding never goes out of bounds, not just the first byte. This requires |
107 | | // a DecodeVarInt function that returns an error code. It is safe to use |
108 | | // DecodeVarInt with out of bounds values, it silently returns, but the |
109 | | // specification requires an error. Idem for all DecodeVarInt below. |
110 | 7.06k | if (pos >= size) return JXL_FAILURE("Out of bounds"); |
111 | 6.99k | uint64_t osize = DecodeVarInt(enc, size, &pos); // Output size |
112 | 6.99k | JXL_RETURN_IF_ERROR(CheckIs32Bit(osize)); |
113 | 6.88k | if (pos >= size) return JXL_FAILURE("Out of bounds"); |
114 | 6.79k | uint64_t csize = DecodeVarInt(enc, size, &pos); // Commands size |
115 | | // Every command is translated to at least on byte. |
116 | 6.79k | JXL_RETURN_IF_ERROR(CheckIs32Bit(csize)); |
117 | 6.77k | size_t cpos = pos; // pos in commands stream |
118 | 6.77k | JXL_RETURN_IF_ERROR(CheckOutOfBounds(pos, csize, size)); |
119 | 6.66k | size_t commands_end = cpos + csize; |
120 | 6.66k | pos = commands_end; // pos in data stream |
121 | | |
122 | | // Header |
123 | 6.66k | PaddedBytes header{memory_manager}; |
124 | 6.66k | JXL_RETURN_IF_ERROR(header.append(ICCInitialHeaderPrediction(osize))); |
125 | 759k | for (size_t i = 0; i <= kICCHeaderSize; i++) { |
126 | 759k | if (result->size() == osize) { |
127 | 831 | if (cpos != commands_end) return JXL_FAILURE("Not all commands used"); |
128 | 656 | if (pos != size) return JXL_FAILURE("Not all data used"); |
129 | 334 | return true; // Valid end |
130 | 656 | } |
131 | 758k | if (i == kICCHeaderSize) break; // Done |
132 | 752k | ICCPredictHeader(result->data(), result->size(), header.data(), i); |
133 | 752k | if (pos >= size) return JXL_FAILURE("Out of bounds"); |
134 | 752k | JXL_RETURN_IF_ERROR(result->push_back(enc[pos++] + header[i])); |
135 | 752k | } |
136 | 5.72k | if (cpos >= commands_end) return JXL_FAILURE("Out of bounds"); |
137 | | |
138 | | // Tag list |
139 | 5.70k | uint64_t numtags = DecodeVarInt(enc, size, &cpos); |
140 | | |
141 | 5.70k | if (numtags != 0) { |
142 | 4.68k | numtags--; |
143 | 4.68k | JXL_RETURN_IF_ERROR(CheckIs32Bit(numtags)); |
144 | 4.39k | JXL_RETURN_IF_ERROR(AppendUint32(numtags, result)); |
145 | 4.39k | uint64_t prevtagstart = kICCHeaderSize + numtags * 12; |
146 | 4.39k | uint64_t prevtagsize = 0; |
147 | 7.44M | for (;;) { |
148 | 7.44M | if (result->size() > osize) return JXL_FAILURE("Invalid result size"); |
149 | 7.44M | if (cpos > commands_end) return JXL_FAILURE("Out of bounds"); |
150 | 7.44M | if (cpos == commands_end) break; // Valid end |
151 | 7.44M | uint8_t command = enc[cpos++]; |
152 | 7.44M | uint8_t tagcode = command & 63; |
153 | 7.44M | Tag tag; |
154 | 7.44M | if (tagcode == 0) { |
155 | 3.84k | break; |
156 | 7.44M | } else if (tagcode == kCommandTagUnknown) { |
157 | 7.29M | JXL_RETURN_IF_ERROR(CheckOutOfBounds(pos, 4, size)); |
158 | 7.29M | tag = DecodeKeyword(enc, size, pos); |
159 | 7.29M | pos += 4; |
160 | 7.29M | } else if (tagcode == kCommandTagTRC) { |
161 | 75.4k | tag = kRtrcTag; |
162 | 75.4k | } else if (tagcode == kCommandTagXYZ) { |
163 | 14.0k | tag = kRxyzTag; |
164 | 59.7k | } else { |
165 | 59.7k | if (tagcode - kCommandTagStringFirst >= kNumTagStrings) { |
166 | 92 | return JXL_FAILURE("Unknown tagcode"); |
167 | 92 | } |
168 | 59.6k | tag = *kTagStrings[tagcode - kCommandTagStringFirst]; |
169 | 59.6k | } |
170 | 7.44M | JXL_RETURN_IF_ERROR(AppendKeyword(tag, result)); |
171 | | |
172 | 7.44M | uint64_t tagstart; |
173 | 7.44M | uint64_t tagsize = prevtagsize; |
174 | 7.44M | if (tag == kRxyzTag || tag == kGxyzTag || tag == kBxyzTag || |
175 | 7.44M | tag == kKxyzTag || tag == kWtptTag || tag == kBkptTag || |
176 | 7.44M | tag == kLumiTag) { |
177 | 42.1k | tagsize = 20; |
178 | 42.1k | } |
179 | | |
180 | 7.44M | if (command & kFlagBitOffset) { |
181 | 6.85M | if (cpos >= commands_end) return JXL_FAILURE("Out of bounds"); |
182 | 6.85M | tagstart = DecodeVarInt(enc, size, &cpos); |
183 | 6.85M | } else { |
184 | 592k | JXL_RETURN_IF_ERROR(CheckIs32Bit(prevtagstart)); |
185 | 592k | tagstart = prevtagstart + prevtagsize; |
186 | 592k | } |
187 | 7.44M | JXL_RETURN_IF_ERROR(CheckIs32Bit(tagstart)); |
188 | 7.44M | JXL_RETURN_IF_ERROR(AppendUint32(tagstart, result)); |
189 | 7.44M | if (command & kFlagBitSize) { |
190 | 6.19M | if (cpos >= commands_end) return JXL_FAILURE("Out of bounds"); |
191 | 6.19M | tagsize = DecodeVarInt(enc, size, &cpos); |
192 | 6.19M | } |
193 | 7.44M | JXL_RETURN_IF_ERROR(CheckIs32Bit(tagsize)); |
194 | 7.44M | JXL_RETURN_IF_ERROR(AppendUint32(tagsize, result)); |
195 | 7.44M | prevtagstart = tagstart; |
196 | 7.44M | prevtagsize = tagsize; |
197 | | |
198 | 7.44M | if (tagcode == kCommandTagTRC) { |
199 | 75.4k | JXL_RETURN_IF_ERROR(AppendKeyword(kGtrcTag, result)); |
200 | 75.4k | JXL_RETURN_IF_ERROR(AppendUint32(tagstart, result)); |
201 | 75.4k | JXL_RETURN_IF_ERROR(AppendUint32(tagsize, result)); |
202 | 75.4k | JXL_RETURN_IF_ERROR(AppendKeyword(kBtrcTag, result)); |
203 | 75.4k | JXL_RETURN_IF_ERROR(AppendUint32(tagstart, result)); |
204 | 75.4k | JXL_RETURN_IF_ERROR(AppendUint32(tagsize, result)); |
205 | 75.4k | } |
206 | | |
207 | 7.44M | if (tagcode == kCommandTagXYZ) { |
208 | 14.0k | JXL_RETURN_IF_ERROR(CheckIs32Bit(tagstart + tagsize * 2)); |
209 | 14.0k | JXL_RETURN_IF_ERROR(AppendKeyword(kGxyzTag, result)); |
210 | 14.0k | JXL_RETURN_IF_ERROR(AppendUint32(tagstart + tagsize, result)); |
211 | 14.0k | JXL_RETURN_IF_ERROR(AppendUint32(tagsize, result)); |
212 | 14.0k | JXL_RETURN_IF_ERROR(AppendKeyword(kBxyzTag, result)); |
213 | 14.0k | JXL_RETURN_IF_ERROR(AppendUint32(tagstart + tagsize * 2, result)); |
214 | 14.0k | JXL_RETURN_IF_ERROR(AppendUint32(tagsize, result)); |
215 | 14.0k | } |
216 | 7.44M | } |
217 | 4.39k | } |
218 | | |
219 | | // Main Content |
220 | 1.84M | for (;;) { |
221 | 1.84M | if (result->size() > osize) return JXL_FAILURE("Invalid result size"); |
222 | 1.84M | if (cpos > commands_end) return JXL_FAILURE("Out of bounds"); |
223 | 1.84M | if (cpos == commands_end) break; // Valid end |
224 | 1.83M | uint8_t command = enc[cpos++]; |
225 | 1.83M | if (command == kCommandInsert) { |
226 | 711k | if (cpos >= commands_end) return JXL_FAILURE("Out of bounds"); |
227 | 711k | uint64_t num = DecodeVarInt(enc, size, &cpos); |
228 | 711k | JXL_RETURN_IF_ERROR(CheckOutOfBounds(pos, num, size)); |
229 | 37.5M | for (size_t i = 0; i < num; i++) { |
230 | 36.8M | JXL_RETURN_IF_ERROR(result->push_back(enc[pos++])); |
231 | 36.8M | } |
232 | 1.12M | } else if (command == kCommandShuffle2 || command == kCommandShuffle4) { |
233 | 19.1k | if (cpos >= commands_end) return JXL_FAILURE("Out of bounds"); |
234 | 19.0k | uint64_t num = DecodeVarInt(enc, size, &cpos); |
235 | 19.0k | JXL_RETURN_IF_ERROR(CheckOutOfBounds(pos, num, size)); |
236 | 18.9k | PaddedBytes shuffled(memory_manager); |
237 | 18.9k | JXL_ASSIGN_OR_RETURN(shuffled, |
238 | 18.9k | PaddedBytes::WithInitialSpace(memory_manager, num)); |
239 | 3.95M | for (size_t i = 0; i < num; i++) { |
240 | 3.93M | shuffled[i] = enc[pos + i]; |
241 | 3.93M | } |
242 | 18.9k | if (command == kCommandShuffle2) { |
243 | 10.0k | JXL_RETURN_IF_ERROR(Shuffle(memory_manager, shuffled.data(), num, 2)); |
244 | 10.0k | } else if (command == kCommandShuffle4) { |
245 | 8.93k | JXL_RETURN_IF_ERROR(Shuffle(memory_manager, shuffled.data(), num, 4)); |
246 | 8.93k | } |
247 | 3.95M | for (size_t i = 0; i < num; i++) { |
248 | 3.93M | JXL_RETURN_IF_ERROR(result->push_back(shuffled[i])); |
249 | 3.93M | pos++; |
250 | 3.93M | } |
251 | 1.10M | } else if (command == kCommandPredict) { |
252 | 424k | JXL_RETURN_IF_ERROR(CheckOutOfBounds(cpos, 2, commands_end)); |
253 | 424k | uint8_t flags = enc[cpos++]; |
254 | | |
255 | 424k | size_t width = (flags & 3) + 1; |
256 | 424k | if (width == 3) return JXL_FAILURE("Invalid width"); |
257 | | |
258 | 424k | int order = (flags & 12) >> 2; |
259 | 424k | if (order == 3) return JXL_FAILURE("Invalid order"); |
260 | | |
261 | 424k | uint64_t stride = width; |
262 | 424k | if (flags & 16) { |
263 | 2.27k | if (cpos >= commands_end) return JXL_FAILURE("Out of bounds"); |
264 | 2.27k | stride = DecodeVarInt(enc, size, &cpos); |
265 | 2.27k | if (stride < width) { |
266 | 7 | return JXL_FAILURE("Invalid stride"); |
267 | 7 | } |
268 | 2.27k | } |
269 | | // If stride * 4 >= result->size(), return failure. The check |
270 | | // "size == 0 || ((size - 1) >> 2) < stride" corresponds to |
271 | | // "stride * 4 >= size", but does not suffer from integer overflow. |
272 | | // This check is more strict than necessary but follows the specification |
273 | | // and the encoder should ensure this is followed. |
274 | 424k | if (result->empty() || ((result->size() - 1u) >> 2u) < stride) { |
275 | 476 | return JXL_FAILURE("Invalid stride"); |
276 | 476 | } |
277 | | |
278 | 424k | if (cpos >= commands_end) return JXL_FAILURE("Out of bounds"); |
279 | 424k | uint64_t num = DecodeVarInt(enc, size, &cpos); // in bytes |
280 | 424k | JXL_RETURN_IF_ERROR(CheckOutOfBounds(pos, num, size)); |
281 | | |
282 | 423k | PaddedBytes shuffled(memory_manager); |
283 | 423k | JXL_ASSIGN_OR_RETURN(shuffled, |
284 | 423k | PaddedBytes::WithInitialSpace(memory_manager, num)); |
285 | | |
286 | 32.2M | for (size_t i = 0; i < num; i++) { |
287 | 31.8M | shuffled[i] = enc[pos + i]; |
288 | 31.8M | } |
289 | 423k | if (width > 1) { |
290 | 420k | JXL_RETURN_IF_ERROR( |
291 | 420k | Shuffle(memory_manager, shuffled.data(), num, width)); |
292 | 420k | } |
293 | | |
294 | 423k | size_t start = result->size(); |
295 | 32.2M | for (size_t i = 0; i < num; i++) { |
296 | 31.8M | uint8_t predicted = LinearPredictICCValue(result->data(), start, i, |
297 | 31.8M | stride, width, order); |
298 | 31.8M | JXL_RETURN_IF_ERROR(result->push_back(predicted + shuffled[i])); |
299 | 31.8M | } |
300 | 423k | pos += num; |
301 | 680k | } else if (command == kCommandXYZ) { |
302 | 9.06k | JXL_RETURN_IF_ERROR(AppendKeyword(kXyz_Tag, result)); |
303 | 45.3k | for (int i = 0; i < 4; i++) { |
304 | 36.2k | JXL_RETURN_IF_ERROR(result->push_back(0)); |
305 | 36.2k | } |
306 | 9.06k | JXL_RETURN_IF_ERROR(CheckOutOfBounds(pos, 12, size)); |
307 | 117k | for (size_t i = 0; i < 12; i++) { |
308 | 108k | JXL_RETURN_IF_ERROR(result->push_back(enc[pos++])); |
309 | 108k | } |
310 | 671k | } else if (command >= kCommandTypeStartFirst && |
311 | 671k | command < kCommandTypeStartFirst + kNumTypeStrings) { |
312 | 671k | JXL_RETURN_IF_ERROR(AppendKeyword( |
313 | 671k | *kTypeStrings[command - kCommandTypeStartFirst], result)); |
314 | 3.35M | for (size_t i = 0; i < 4; i++) { |
315 | 2.68M | JXL_RETURN_IF_ERROR(result->push_back(0)); |
316 | 2.68M | } |
317 | 671k | } else { |
318 | 283 | return JXL_FAILURE("Unknown command"); |
319 | 283 | } |
320 | 1.83M | } |
321 | | |
322 | 3.73k | if (pos != size) return JXL_FAILURE("Not all data used"); |
323 | 3.52k | if (result->size() != osize) return JXL_FAILURE("Invalid result size"); |
324 | | |
325 | 3.17k | return true; |
326 | 3.52k | } |
327 | | |
328 | 122k | Status ICCReader::Init(BitReader* reader) { |
329 | 122k | JXL_RETURN_IF_ERROR(CheckEOI(reader)); |
330 | 122k | JxlMemoryManager* memory_manager = decompressed_.memory_manager(); |
331 | 122k | used_bits_base_ = reader->TotalBitsConsumed(); |
332 | 122k | if (bits_to_skip_ == 0) { |
333 | 112k | enc_size_ = U64Coder::Read(reader); |
334 | 112k | if (enc_size_ > 268435456) { |
335 | | // Avoid too large memory allocation for invalid file. |
336 | 641 | return JXL_FAILURE("Too large encoded profile"); |
337 | 641 | } |
338 | 112k | JXL_RETURN_IF_ERROR(DecodeHistograms( |
339 | 112k | memory_manager, reader, kNumICCContexts, &code_, &context_map_)); |
340 | 133k | JXL_ASSIGN_OR_RETURN(ans_reader_, ANSSymbolReader::Create(&code_, reader)); |
341 | 133k | i_ = 0; |
342 | 133k | JXL_RETURN_IF_ERROR( |
343 | 133k | decompressed_.resize(std::min<size_t>(i_ + 0x400, enc_size_))); |
344 | 198k | for (; i_ < std::min<size_t>(2, enc_size_); i_++) { |
345 | 131k | decompressed_[i_] = ans_reader_.ReadHybridUint( |
346 | 131k | ICCANSContext(i_, i_ > 0 ? decompressed_[i_ - 1] : 0, |
347 | 131k | i_ > 1 ? decompressed_[i_ - 2] : 0), |
348 | 131k | reader, context_map_); |
349 | 131k | } |
350 | 66.7k | if (enc_size_ > kPreambleSize) { |
351 | 1.37M | for (; i_ < kPreambleSize; i_++) { |
352 | 1.31M | decompressed_[i_] = ans_reader_.ReadHybridUint( |
353 | 1.31M | ICCANSContext(i_, decompressed_[i_ - 1], decompressed_[i_ - 2]), |
354 | 1.31M | reader, context_map_); |
355 | 1.31M | } |
356 | 65.5k | JXL_RETURN_IF_ERROR(CheckEOI(reader)); |
357 | 3.33k | JXL_RETURN_IF_ERROR(CheckPreamble(decompressed_, enc_size_)); |
358 | 3.33k | } |
359 | 4.30k | bits_to_skip_ = reader->TotalBitsConsumed() - used_bits_base_; |
360 | 9.60k | } else { |
361 | 9.60k | reader->SkipBits(bits_to_skip_); |
362 | 9.60k | } |
363 | 13.9k | return true; |
364 | 122k | } |
365 | | |
366 | 12.7k | Status ICCReader::Process(BitReader* reader, PaddedBytes* icc) { |
367 | 12.7k | auto checkpoint = jxl::make_unique<ANSSymbolReader::Checkpoint>(); |
368 | 12.7k | size_t saved_i = 0; |
369 | 2.36M | auto save = [&]() { |
370 | 2.36M | ans_reader_.Save(checkpoint.get()); |
371 | 2.36M | bits_to_skip_ = reader->TotalBitsConsumed() - used_bits_base_; |
372 | 2.36M | saved_i = i_; |
373 | 2.36M | }; |
374 | 12.7k | save(); |
375 | 2.36M | auto check_and_restore = [&]() -> Status { |
376 | 2.36M | Status status = CheckEOI(reader); |
377 | 2.36M | if (!status) { |
378 | | // not enough bytes. |
379 | 9.91k | ans_reader_.Restore(*checkpoint); |
380 | 9.91k | i_ = saved_i; |
381 | 9.91k | return status; |
382 | 9.91k | } |
383 | 2.35M | return true; |
384 | 2.36M | }; |
385 | 1.20G | for (; i_ < enc_size_; i_++) { |
386 | 1.20G | if (i_ % ANSSymbolReader::kMaxCheckpointInterval == 0 && i_ > 0) { |
387 | 2.35M | JXL_RETURN_IF_ERROR(check_and_restore()); |
388 | 2.35M | save(); |
389 | 2.35M | if ((i_ > 0) && (((i_ & 0xFFFF) == 0))) { |
390 | 18.1k | float used_bytes = |
391 | 18.1k | (reader->TotalBitsConsumed() - used_bits_base_) / 8.0f; |
392 | 18.1k | if (i_ > used_bytes * 256) return JXL_FAILURE("Corrupted stream"); |
393 | 18.1k | } |
394 | 2.35M | JXL_RETURN_IF_ERROR( |
395 | 2.35M | decompressed_.resize(std::min<size_t>(i_ + 0x400, enc_size_))); |
396 | 2.35M | } |
397 | 1.20G | JXL_ENSURE(i_ >= 2); |
398 | 1.20G | decompressed_[i_] = ans_reader_.ReadHybridUint( |
399 | 1.20G | ICCANSContext(i_, decompressed_[i_ - 1], decompressed_[i_ - 2]), reader, |
400 | 1.20G | context_map_); |
401 | 1.20G | } |
402 | 6.00k | JXL_RETURN_IF_ERROR(check_and_restore()); |
403 | 2.75k | bits_to_skip_ = reader->TotalBitsConsumed() - used_bits_base_; |
404 | 2.75k | if (!ans_reader_.CheckANSFinalState()) { |
405 | 0 | return JXL_FAILURE("Corrupted ICC profile"); |
406 | 0 | } |
407 | | |
408 | 2.75k | icc->clear(); |
409 | 2.75k | return UnpredictICC(decompressed_.data(), decompressed_.size(), icc); |
410 | 2.75k | } |
411 | | |
412 | 2.55M | Status ICCReader::CheckEOI(BitReader* reader) { |
413 | 2.55M | if (reader->AllReadsWithinBounds()) return true; |
414 | 72.1k | return JXL_NOT_ENOUGH_BYTES("Not enough bytes for reading ICC profile"); |
415 | 2.55M | } |
416 | | |
417 | | } // namespace jxl |