Line | Count | Source (jump to first uncovered line) |
1 | | /* pngrutil.c - utilities to read a PNG file |
2 | | * |
3 | | * Copyright (c) 2018-2025 Cosmin Truta |
4 | | * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson |
5 | | * Copyright (c) 1996-1997 Andreas Dilger |
6 | | * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc. |
7 | | * |
8 | | * This code is released under the libpng license. |
9 | | * For conditions of distribution and use, see the disclaimer |
10 | | * and license in png.h |
11 | | * |
12 | | * This file contains routines that are only called from within |
13 | | * libpng itself during the course of reading an image. |
14 | | */ |
15 | | |
16 | | #include "pngpriv.h" |
17 | | |
18 | | #ifdef PNG_READ_SUPPORTED |
19 | | |
20 | | /* The minimum 'zlib' stream is assumed to be just the 2 byte header, 5 bytes |
21 | | * minimum 'deflate' stream, and the 4 byte checksum. |
22 | | */ |
23 | 58.9k | #define LZ77Min (2U+5U+4U) |
24 | | |
25 | | #ifdef PNG_READ_INTERLACING_SUPPORTED |
26 | | /* Arrays to facilitate interlacing - use pass (0 - 6) as index. */ |
27 | | |
28 | | /* Start of interlace block */ |
29 | | static const png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; |
30 | | /* Offset to next interlace block */ |
31 | | static const png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; |
32 | | /* Start of interlace block in the y direction */ |
33 | | static const png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; |
34 | | /* Offset to next interlace block in the y direction */ |
35 | | static const png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; |
36 | | |
37 | | /* TODO: Move these arrays to a common utility module to avoid duplication. */ |
38 | | #endif |
39 | | |
40 | | png_uint_32 PNGAPI |
41 | | png_get_uint_31(png_const_structrp png_ptr, png_const_bytep buf) |
42 | 970k | { |
43 | 970k | png_uint_32 uval = png_get_uint_32(buf); |
44 | | |
45 | 970k | if (uval > PNG_UINT_31_MAX) |
46 | 1.42k | png_error(png_ptr, "PNG unsigned integer out of range"); |
47 | | |
48 | 969k | return uval; |
49 | 970k | } |
50 | | |
51 | | #ifdef PNG_READ_INT_FUNCTIONS_SUPPORTED |
52 | | /* NOTE: the read macros will obscure these definitions, so that if |
53 | | * PNG_USE_READ_MACROS is set the library will not use them internally, |
54 | | * but the APIs will still be available externally. |
55 | | * |
56 | | * The parentheses around "PNGAPI function_name" in the following three |
57 | | * functions are necessary because they allow the macros to co-exist with |
58 | | * these (unused but exported) functions. |
59 | | */ |
60 | | |
61 | | /* Grab an unsigned 32-bit integer from a buffer in big-endian format. */ |
62 | | png_uint_32 (PNGAPI |
63 | | png_get_uint_32)(png_const_bytep buf) |
64 | 0 | { |
65 | 0 | png_uint_32 uval = |
66 | 0 | ((png_uint_32)(*(buf )) << 24) + |
67 | 0 | ((png_uint_32)(*(buf + 1)) << 16) + |
68 | 0 | ((png_uint_32)(*(buf + 2)) << 8) + |
69 | 0 | ((png_uint_32)(*(buf + 3)) ) ; |
70 | |
|
71 | 0 | return uval; |
72 | 0 | } |
73 | | |
74 | | /* Grab a signed 32-bit integer from a buffer in big-endian format. The |
75 | | * data is stored in the PNG file in two's complement format and there |
76 | | * is no guarantee that a 'png_int_32' is exactly 32 bits, therefore |
77 | | * the following code does a two's complement to native conversion. |
78 | | */ |
79 | | png_int_32 (PNGAPI |
80 | | png_get_int_32)(png_const_bytep buf) |
81 | 0 | { |
82 | 0 | png_uint_32 uval = png_get_uint_32(buf); |
83 | 0 | if ((uval & 0x80000000) == 0) /* non-negative */ |
84 | 0 | return (png_int_32)uval; |
85 | | |
86 | 0 | uval = (uval ^ 0xffffffff) + 1; /* 2's complement: -x = ~x+1 */ |
87 | 0 | if ((uval & 0x80000000) == 0) /* no overflow */ |
88 | 0 | return -(png_int_32)uval; |
89 | | /* The following has to be safe; this function only gets called on PNG data |
90 | | * and if we get here that data is invalid. 0 is the most safe value and |
91 | | * if not then an attacker would surely just generate a PNG with 0 instead. |
92 | | */ |
93 | 0 | return 0; |
94 | 0 | } |
95 | | |
96 | | /* Grab an unsigned 16-bit integer from a buffer in big-endian format. */ |
97 | | png_uint_16 (PNGAPI |
98 | | png_get_uint_16)(png_const_bytep buf) |
99 | 0 | { |
100 | | /* ANSI-C requires an int value to accommodate at least 16 bits so this |
101 | | * works and allows the compiler not to worry about possible narrowing |
102 | | * on 32-bit systems. (Pre-ANSI systems did not make integers smaller |
103 | | * than 16 bits either.) |
104 | | */ |
105 | 0 | unsigned int val = |
106 | 0 | ((unsigned int)(*buf) << 8) + |
107 | 0 | ((unsigned int)(*(buf + 1))); |
108 | |
|
109 | 0 | return (png_uint_16)val; |
110 | 0 | } |
111 | | |
112 | | #endif /* READ_INT_FUNCTIONS */ |
113 | | |
114 | | /* Read and check the PNG file signature */ |
115 | | void /* PRIVATE */ |
116 | | png_read_sig(png_structrp png_ptr, png_inforp info_ptr) |
117 | 97.2k | { |
118 | 97.2k | size_t num_checked, num_to_check; |
119 | | |
120 | | /* Exit if the user application does not expect a signature. */ |
121 | 97.2k | if (png_ptr->sig_bytes >= 8) |
122 | 97.2k | return; |
123 | | |
124 | 0 | num_checked = png_ptr->sig_bytes; |
125 | 0 | num_to_check = 8 - num_checked; |
126 | |
|
127 | 0 | #ifdef PNG_IO_STATE_SUPPORTED |
128 | 0 | png_ptr->io_state = PNG_IO_READING | PNG_IO_SIGNATURE; |
129 | 0 | #endif |
130 | | |
131 | | /* The signature must be serialized in a single I/O call. */ |
132 | 0 | png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check); |
133 | 0 | png_ptr->sig_bytes = 8; |
134 | |
|
135 | 0 | if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check) != 0) |
136 | 0 | { |
137 | 0 | if (num_checked < 4 && |
138 | 0 | png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4) != 0) |
139 | 0 | png_error(png_ptr, "Not a PNG file"); |
140 | 0 | else |
141 | 0 | png_error(png_ptr, "PNG file corrupted by ASCII conversion"); |
142 | 0 | } |
143 | 0 | if (num_checked < 3) |
144 | 0 | png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE; |
145 | 0 | } |
146 | | |
147 | | /* This function is called to verify that a chunk name is valid. |
148 | | * Do this using the bit-whacking approach from contrib/tools/pngfix.c |
149 | | * |
150 | | * Copied from libpng 1.7. |
151 | | */ |
152 | | static int |
153 | | check_chunk_name(png_uint_32 name) |
154 | 813k | { |
155 | 813k | png_uint_32 t; |
156 | | |
157 | | /* Remove bit 5 from all but the reserved byte; this means |
158 | | * every 8-bit unit must be in the range 65-90 to be valid. |
159 | | * So bit 5 must be zero, bit 6 must be set and bit 7 zero. |
160 | | */ |
161 | 813k | name &= ~PNG_U32(32,32,0,32); |
162 | 813k | t = (name & ~0x1f1f1f1fU) ^ 0x40404040U; |
163 | | |
164 | | /* Subtract 65 for each 8-bit quantity, this must not |
165 | | * overflow and each byte must then be in the range 0-25. |
166 | | */ |
167 | 813k | name -= PNG_U32(65,65,65,65); |
168 | 813k | t |= name; |
169 | | |
170 | | /* Subtract 26, handling the overflow which should set the |
171 | | * top three bits of each byte. |
172 | | */ |
173 | 813k | name -= PNG_U32(25,25,25,26); |
174 | 813k | t |= ~name; |
175 | | |
176 | 813k | return (t & 0xe0e0e0e0U) == 0U; |
177 | 813k | } |
178 | | |
179 | | /* Read the chunk header (length + type name). |
180 | | * Put the type name into png_ptr->chunk_name, and return the length. |
181 | | */ |
182 | | png_uint_32 /* PRIVATE */ |
183 | | png_read_chunk_header(png_structrp png_ptr) |
184 | 830k | { |
185 | 830k | png_byte buf[8]; |
186 | 830k | png_uint_32 chunk_name, length; |
187 | | |
188 | 830k | #ifdef PNG_IO_STATE_SUPPORTED |
189 | 830k | png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_HDR; |
190 | 830k | #endif |
191 | | |
192 | | /* Read the length and the chunk name. png_struct::chunk_name is immediately |
193 | | * updated even if they are detectably wrong. This aids error message |
194 | | * handling by allowing png_chunk_error to be used. |
195 | | */ |
196 | 830k | png_read_data(png_ptr, buf, 8); |
197 | 830k | length = png_get_uint_31(png_ptr, buf); |
198 | 830k | png_ptr->chunk_name = chunk_name = PNG_CHUNK_FROM_STRING(buf+4); |
199 | | |
200 | | /* Reset the crc and run it over the chunk name. */ |
201 | 830k | png_reset_crc(png_ptr); |
202 | 830k | png_calculate_crc(png_ptr, buf + 4, 4); |
203 | | |
204 | 830k | png_debug2(0, "Reading chunk typeid = 0x%lx, length = %lu", |
205 | 830k | (unsigned long)png_ptr->chunk_name, (unsigned long)length); |
206 | | |
207 | | /* Sanity check the length (first by <= 0x80) and the chunk name. An error |
208 | | * here indicates a broken stream and libpng has no recovery from this. |
209 | | */ |
210 | 830k | if (buf[0] >= 0x80U) |
211 | 0 | png_chunk_error(png_ptr, "bad header (invalid length)"); |
212 | | |
213 | | /* Check to see if chunk name is valid. */ |
214 | 830k | if (!check_chunk_name(chunk_name)) |
215 | 4.34k | png_chunk_error(png_ptr, "bad header (invalid type)"); |
216 | | |
217 | 826k | #ifdef PNG_IO_STATE_SUPPORTED |
218 | 826k | png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_DATA; |
219 | 826k | #endif |
220 | | |
221 | 826k | return length; |
222 | 830k | } |
223 | | |
224 | | /* Read data, and (optionally) run it through the CRC. */ |
225 | | void /* PRIVATE */ |
226 | | png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length) |
227 | 734k | { |
228 | 734k | if (png_ptr == NULL) |
229 | 0 | return; |
230 | | |
231 | 734k | png_read_data(png_ptr, buf, length); |
232 | 734k | png_calculate_crc(png_ptr, buf, length); |
233 | 734k | } |
234 | | |
235 | | /* Compare the CRC stored in the PNG file with that calculated by libpng from |
236 | | * the data it has read thus far. |
237 | | */ |
238 | | static int |
239 | | png_crc_error(png_structrp png_ptr, int handle_as_ancillary) |
240 | 783k | { |
241 | 783k | png_byte crc_bytes[4]; |
242 | 783k | png_uint_32 crc; |
243 | 783k | int need_crc = 1; |
244 | | |
245 | | /* There are four flags two for ancillary and two for critical chunks. The |
246 | | * default setting of these flags is all zero. |
247 | | * |
248 | | * PNG_FLAG_CRC_ANCILLARY_USE |
249 | | * PNG_FLAG_CRC_ANCILLARY_NOWARN |
250 | | * USE+NOWARN: no CRC calculation (implemented here), else; |
251 | | * NOWARN: png_chunk_error on error (implemented in png_crc_finish) |
252 | | * else: png_chunk_warning on error (implemented in png_crc_finish) |
253 | | * This is the default. |
254 | | * |
255 | | * I.e. NOWARN without USE produces png_chunk_error. The default setting |
256 | | * where neither are set does the same thing. |
257 | | * |
258 | | * PNG_FLAG_CRC_CRITICAL_USE |
259 | | * PNG_FLAG_CRC_CRITICAL_IGNORE |
260 | | * IGNORE: no CRC calculation (implemented here), else; |
261 | | * USE: png_chunk_warning on error (implemented in png_crc_finish) |
262 | | * else: png_chunk_error on error (implemented in png_crc_finish) |
263 | | * This is the default. |
264 | | * |
265 | | * This arose because of original mis-implementation and has persisted for |
266 | | * compatibility reasons. |
267 | | * |
268 | | * TODO: the flag names are internal so maybe this can be changed to |
269 | | * something comprehensible. |
270 | | */ |
271 | 783k | if (handle_as_ancillary || PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) != 0) |
272 | 571k | { |
273 | 571k | if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) == |
274 | 571k | (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN)) |
275 | 0 | need_crc = 0; |
276 | 571k | } |
277 | | |
278 | 212k | else /* critical */ |
279 | 212k | { |
280 | 212k | if ((png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) != 0) |
281 | 0 | need_crc = 0; |
282 | 212k | } |
283 | | |
284 | 783k | #ifdef PNG_IO_STATE_SUPPORTED |
285 | 783k | png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_CRC; |
286 | 783k | #endif |
287 | | |
288 | | /* The chunk CRC must be serialized in a single I/O call. */ |
289 | 783k | png_read_data(png_ptr, crc_bytes, 4); |
290 | | |
291 | 783k | if (need_crc != 0) |
292 | 775k | { |
293 | 775k | crc = png_get_uint_32(crc_bytes); |
294 | 775k | return crc != png_ptr->crc; |
295 | 775k | } |
296 | | |
297 | 8.61k | else |
298 | 8.61k | return 0; |
299 | 783k | } |
300 | | |
301 | | /* Optionally skip data and then check the CRC. Depending on whether we |
302 | | * are reading an ancillary or critical chunk, and how the program has set |
303 | | * things up, we may calculate the CRC on the data and print a message. |
304 | | * Returns '1' if there was a CRC error, '0' otherwise. |
305 | | * |
306 | | * There is one public version which is used in most places and another which |
307 | | * takes the value for the 'critical' flag to check. This allows PLTE and IEND |
308 | | * handling code to ignore the CRC error and removes some confusing code |
309 | | * duplication. |
310 | | */ |
311 | | static int |
312 | | png_crc_finish_critical(png_structrp png_ptr, png_uint_32 skip, |
313 | | int handle_as_ancillary) |
314 | 795k | { |
315 | | /* The size of the local buffer for inflate is a good guess as to a |
316 | | * reasonable size to use for buffering reads from the application. |
317 | | */ |
318 | 861k | while (skip > 0) |
319 | 65.8k | { |
320 | 65.8k | png_uint_32 len; |
321 | 65.8k | png_byte tmpbuf[PNG_INFLATE_BUF_SIZE]; |
322 | | |
323 | 65.8k | len = (sizeof tmpbuf); |
324 | 65.8k | if (len > skip) |
325 | 58.2k | len = skip; |
326 | 65.8k | skip -= len; |
327 | | |
328 | 65.8k | png_crc_read(png_ptr, tmpbuf, len); |
329 | 65.8k | } |
330 | | |
331 | | /* If 'handle_as_ancillary' has been requested and this is a critical chunk |
332 | | * but PNG_FLAG_CRC_CRITICAL_IGNORE was set then png_read_crc did not, in |
333 | | * fact, calculate the CRC so the ANCILLARY settings should not be used |
334 | | * instead. |
335 | | */ |
336 | 795k | if (handle_as_ancillary && |
337 | 795k | (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) != 0) |
338 | 0 | handle_as_ancillary = 0; |
339 | | |
340 | | /* TODO: this might be more comprehensible if png_crc_error was inlined here. |
341 | | */ |
342 | 795k | if (png_crc_error(png_ptr, handle_as_ancillary) != 0) |
343 | 445k | { |
344 | | /* See above for the explanation of how the flags work. */ |
345 | 445k | if (handle_as_ancillary || PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) != 0 ? |
346 | 444k | (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) == 0 : |
347 | 445k | (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE) != 0) |
348 | 444k | png_chunk_warning(png_ptr, "CRC error"); |
349 | | |
350 | 1.25k | else |
351 | 1.25k | png_chunk_error(png_ptr, "CRC error"); |
352 | | |
353 | 444k | return 1; |
354 | 445k | } |
355 | | |
356 | 349k | return 0; |
357 | 795k | } |
358 | | |
359 | | int /* PRIVATE */ |
360 | | png_crc_finish(png_structrp png_ptr, png_uint_32 skip) |
361 | 746k | { |
362 | 746k | return png_crc_finish_critical(png_ptr, skip, 0/*critical handling*/); |
363 | 746k | } |
364 | | |
365 | | #if defined(PNG_READ_iCCP_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) ||\ |
366 | | defined(PNG_READ_pCAL_SUPPORTED) || defined(PNG_READ_sCAL_SUPPORTED) ||\ |
367 | | defined(PNG_READ_sPLT_SUPPORTED) || defined(PNG_READ_tEXt_SUPPORTED) ||\ |
368 | | defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_READ_eXIf_SUPPORTED) ||\ |
369 | | defined(PNG_SEQUENTIAL_READ_SUPPORTED) |
370 | | /* Manage the read buffer; this simply reallocates the buffer if it is not small |
371 | | * enough (or if it is not allocated). The routine returns a pointer to the |
372 | | * buffer; if an error occurs and 'warn' is set the routine returns NULL, else |
373 | | * it will call png_error on failure. |
374 | | */ |
375 | | static png_bytep |
376 | | png_read_buffer(png_structrp png_ptr, png_alloc_size_t new_size) |
377 | 214k | { |
378 | 214k | png_bytep buffer = png_ptr->read_buffer; |
379 | | |
380 | 214k | if (new_size > png_chunk_max(png_ptr)) return NULL; |
381 | | |
382 | 214k | if (buffer != NULL && new_size > png_ptr->read_buffer_size) |
383 | 5.70k | { |
384 | 5.70k | png_ptr->read_buffer = NULL; |
385 | 5.70k | png_ptr->read_buffer_size = 0; |
386 | 5.70k | png_free(png_ptr, buffer); |
387 | 5.70k | buffer = NULL; |
388 | 5.70k | } |
389 | | |
390 | 214k | if (buffer == NULL) |
391 | 69.7k | { |
392 | 69.7k | buffer = png_voidcast(png_bytep, png_malloc_base(png_ptr, new_size)); |
393 | | |
394 | 69.7k | if (buffer != NULL) |
395 | 69.7k | { |
396 | 69.7k | # ifndef PNG_NO_MEMZERO /* for detecting UIM bugs **only** */ |
397 | 69.7k | memset(buffer, 0, new_size); /* just in case */ |
398 | 69.7k | # endif |
399 | 69.7k | png_ptr->read_buffer = buffer; |
400 | 69.7k | png_ptr->read_buffer_size = new_size; |
401 | 69.7k | } |
402 | 69.7k | } |
403 | | |
404 | 214k | return buffer; |
405 | 214k | } |
406 | | #endif /* READ_iCCP|iTXt|pCAL|sCAL|sPLT|tEXt|zTXt|eXIf|SEQUENTIAL_READ */ |
407 | | |
408 | | /* png_inflate_claim: claim the zstream for some nefarious purpose that involves |
409 | | * decompression. Returns Z_OK on success, else a zlib error code. It checks |
410 | | * the owner but, in final release builds, just issues a warning if some other |
411 | | * chunk apparently owns the stream. Prior to release it does a png_error. |
412 | | */ |
413 | | static int |
414 | | png_inflate_claim(png_structrp png_ptr, png_uint_32 owner) |
415 | 112k | { |
416 | 112k | if (png_ptr->zowner != 0) |
417 | 0 | { |
418 | 0 | char msg[64]; |
419 | |
|
420 | 0 | PNG_STRING_FROM_CHUNK(msg, png_ptr->zowner); |
421 | | /* So the message that results is "<chunk> using zstream"; this is an |
422 | | * internal error, but is very useful for debugging. i18n requirements |
423 | | * are minimal. |
424 | | */ |
425 | 0 | (void)png_safecat(msg, (sizeof msg), 4, " using zstream"); |
426 | | #if PNG_RELEASE_BUILD |
427 | | png_chunk_warning(png_ptr, msg); |
428 | | png_ptr->zowner = 0; |
429 | | #else |
430 | 0 | png_chunk_error(png_ptr, msg); |
431 | 0 | #endif |
432 | 0 | } |
433 | | |
434 | | /* Implementation note: unlike 'png_deflate_claim' this internal function |
435 | | * does not take the size of the data as an argument. Some efficiency could |
436 | | * be gained by using this when it is known *if* the zlib stream itself does |
437 | | * not record the number; however, this is an illusion: the original writer |
438 | | * of the PNG may have selected a lower window size, and we really must |
439 | | * follow that because, for systems with with limited capabilities, we |
440 | | * would otherwise reject the application's attempts to use a smaller window |
441 | | * size (zlib doesn't have an interface to say "this or lower"!). |
442 | | * |
443 | | * inflateReset2 was added to zlib 1.2.4; before this the window could not be |
444 | | * reset, therefore it is necessary to always allocate the maximum window |
445 | | * size with earlier zlibs just in case later compressed chunks need it. |
446 | | */ |
447 | 112k | { |
448 | 112k | int ret; /* zlib return code */ |
449 | 112k | #if ZLIB_VERNUM >= 0x1240 |
450 | 112k | int window_bits = 0; |
451 | | |
452 | 112k | # if defined(PNG_SET_OPTION_SUPPORTED) && defined(PNG_MAXIMUM_INFLATE_WINDOW) |
453 | 112k | if (((png_ptr->options >> PNG_MAXIMUM_INFLATE_WINDOW) & 3) == |
454 | 112k | PNG_OPTION_ON) |
455 | 0 | { |
456 | 0 | window_bits = 15; |
457 | 0 | png_ptr->zstream_start = 0; /* fixed window size */ |
458 | 0 | } |
459 | | |
460 | 112k | else |
461 | 112k | { |
462 | 112k | png_ptr->zstream_start = 1; |
463 | 112k | } |
464 | 112k | # endif |
465 | | |
466 | 112k | #endif /* ZLIB_VERNUM >= 0x1240 */ |
467 | | |
468 | | /* Set this for safety, just in case the previous owner left pointers to |
469 | | * memory allocations. |
470 | | */ |
471 | 112k | png_ptr->zstream.next_in = NULL; |
472 | 112k | png_ptr->zstream.avail_in = 0; |
473 | 112k | png_ptr->zstream.next_out = NULL; |
474 | 112k | png_ptr->zstream.avail_out = 0; |
475 | | |
476 | 112k | if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0) |
477 | 49.1k | { |
478 | 49.1k | #if ZLIB_VERNUM >= 0x1240 |
479 | 49.1k | ret = inflateReset2(&png_ptr->zstream, window_bits); |
480 | | #else |
481 | | ret = inflateReset(&png_ptr->zstream); |
482 | | #endif |
483 | 49.1k | } |
484 | | |
485 | 63.6k | else |
486 | 63.6k | { |
487 | 63.6k | #if ZLIB_VERNUM >= 0x1240 |
488 | 63.6k | ret = inflateInit2(&png_ptr->zstream, window_bits); |
489 | | #else |
490 | | ret = inflateInit(&png_ptr->zstream); |
491 | | #endif |
492 | | |
493 | 63.6k | if (ret == Z_OK) |
494 | 63.6k | png_ptr->flags |= PNG_FLAG_ZSTREAM_INITIALIZED; |
495 | 63.6k | } |
496 | | |
497 | | #ifdef PNG_DISABLE_ADLER32_CHECK_SUPPORTED |
498 | | if (((png_ptr->options >> PNG_IGNORE_ADLER32) & 3) == PNG_OPTION_ON) |
499 | | /* Turn off validation of the ADLER32 checksum in IDAT chunks */ |
500 | | ret = inflateValidate(&png_ptr->zstream, 0); |
501 | | #endif |
502 | | |
503 | 112k | if (ret == Z_OK) |
504 | 112k | png_ptr->zowner = owner; |
505 | | |
506 | 0 | else |
507 | 0 | png_zstream_error(png_ptr, ret); |
508 | | |
509 | 112k | return ret; |
510 | 112k | } |
511 | | |
512 | | #ifdef window_bits |
513 | | # undef window_bits |
514 | | #endif |
515 | 112k | } |
516 | | |
517 | | #if ZLIB_VERNUM >= 0x1240 |
518 | | /* Handle the start of the inflate stream if we called inflateInit2(strm,0); |
519 | | * in this case some zlib versions skip validation of the CINFO field and, in |
520 | | * certain circumstances, libpng may end up displaying an invalid image, in |
521 | | * contrast to implementations that call zlib in the normal way (e.g. libpng |
522 | | * 1.5). |
523 | | */ |
524 | | int /* PRIVATE */ |
525 | | png_zlib_inflate(png_structrp png_ptr, int flush) |
526 | 1.38M | { |
527 | 1.38M | if (png_ptr->zstream_start && png_ptr->zstream.avail_in > 0) |
528 | 110k | { |
529 | 110k | if ((*png_ptr->zstream.next_in >> 4) > 7) |
530 | 641 | { |
531 | 641 | png_ptr->zstream.msg = "invalid window size (libpng)"; |
532 | 641 | return Z_DATA_ERROR; |
533 | 641 | } |
534 | | |
535 | 109k | png_ptr->zstream_start = 0; |
536 | 109k | } |
537 | | |
538 | 1.38M | return inflate(&png_ptr->zstream, flush); |
539 | 1.38M | } |
540 | | #endif /* Zlib >= 1.2.4 */ |
541 | | |
542 | | #ifdef PNG_READ_COMPRESSED_TEXT_SUPPORTED |
543 | | #if defined(PNG_READ_zTXt_SUPPORTED) || defined (PNG_READ_iTXt_SUPPORTED) |
544 | | /* png_inflate now returns zlib error codes including Z_OK and Z_STREAM_END to |
545 | | * allow the caller to do multiple calls if required. If the 'finish' flag is |
546 | | * set Z_FINISH will be passed to the final inflate() call and Z_STREAM_END must |
547 | | * be returned or there has been a problem, otherwise Z_SYNC_FLUSH is used and |
548 | | * Z_OK or Z_STREAM_END will be returned on success. |
549 | | * |
550 | | * The input and output sizes are updated to the actual amounts of data consumed |
551 | | * or written, not the amount available (as in a z_stream). The data pointers |
552 | | * are not changed, so the next input is (data+input_size) and the next |
553 | | * available output is (output+output_size). |
554 | | */ |
555 | | static int |
556 | | png_inflate(png_structrp png_ptr, png_uint_32 owner, int finish, |
557 | | /* INPUT: */ png_const_bytep input, png_uint_32p input_size_ptr, |
558 | | /* OUTPUT: */ png_bytep output, png_alloc_size_t *output_size_ptr) |
559 | 13.1k | { |
560 | 13.1k | if (png_ptr->zowner == owner) /* Else not claimed */ |
561 | 13.1k | { |
562 | 13.1k | int ret; |
563 | 13.1k | png_alloc_size_t avail_out = *output_size_ptr; |
564 | 13.1k | png_uint_32 avail_in = *input_size_ptr; |
565 | | |
566 | | /* zlib can't necessarily handle more than 65535 bytes at once (i.e. it |
567 | | * can't even necessarily handle 65536 bytes) because the type uInt is |
568 | | * "16 bits or more". Consequently it is necessary to chunk the input to |
569 | | * zlib. This code uses ZLIB_IO_MAX, from pngpriv.h, as the maximum (the |
570 | | * maximum value that can be stored in a uInt.) It is possible to set |
571 | | * ZLIB_IO_MAX to a lower value in pngpriv.h and this may sometimes have |
572 | | * a performance advantage, because it reduces the amount of data accessed |
573 | | * at each step and that may give the OS more time to page it in. |
574 | | */ |
575 | 13.1k | png_ptr->zstream.next_in = PNGZ_INPUT_CAST(input); |
576 | | /* avail_in and avail_out are set below from 'size' */ |
577 | 13.1k | png_ptr->zstream.avail_in = 0; |
578 | 13.1k | png_ptr->zstream.avail_out = 0; |
579 | | |
580 | | /* Read directly into the output if it is available (this is set to |
581 | | * a local buffer below if output is NULL). |
582 | | */ |
583 | 13.1k | if (output != NULL) |
584 | 5.57k | png_ptr->zstream.next_out = output; |
585 | | |
586 | 13.1k | do |
587 | 99.6k | { |
588 | 99.6k | uInt avail; |
589 | 99.6k | Byte local_buffer[PNG_INFLATE_BUF_SIZE]; |
590 | | |
591 | | /* zlib INPUT BUFFER */ |
592 | | /* The setting of 'avail_in' used to be outside the loop; by setting it |
593 | | * inside it is possible to chunk the input to zlib and simply rely on |
594 | | * zlib to advance the 'next_in' pointer. This allows arbitrary |
595 | | * amounts of data to be passed through zlib at the unavoidable cost of |
596 | | * requiring a window save (memcpy of up to 32768 output bytes) |
597 | | * every ZLIB_IO_MAX input bytes. |
598 | | */ |
599 | 99.6k | avail_in += png_ptr->zstream.avail_in; /* not consumed last time */ |
600 | | |
601 | 99.6k | avail = ZLIB_IO_MAX; |
602 | | |
603 | 99.6k | if (avail_in < avail) |
604 | 99.6k | avail = (uInt)avail_in; /* safe: < than ZLIB_IO_MAX */ |
605 | | |
606 | 99.6k | avail_in -= avail; |
607 | 99.6k | png_ptr->zstream.avail_in = avail; |
608 | | |
609 | | /* zlib OUTPUT BUFFER */ |
610 | 99.6k | avail_out += png_ptr->zstream.avail_out; /* not written last time */ |
611 | | |
612 | 99.6k | avail = ZLIB_IO_MAX; /* maximum zlib can process */ |
613 | | |
614 | 99.6k | if (output == NULL) |
615 | 94.1k | { |
616 | | /* Reset the output buffer each time round if output is NULL and |
617 | | * make available the full buffer, up to 'remaining_space' |
618 | | */ |
619 | 94.1k | png_ptr->zstream.next_out = local_buffer; |
620 | 94.1k | if ((sizeof local_buffer) < avail) |
621 | 94.1k | avail = (sizeof local_buffer); |
622 | 94.1k | } |
623 | | |
624 | 99.6k | if (avail_out < avail) |
625 | 5.57k | avail = (uInt)avail_out; /* safe: < ZLIB_IO_MAX */ |
626 | | |
627 | 99.6k | png_ptr->zstream.avail_out = avail; |
628 | 99.6k | avail_out -= avail; |
629 | | |
630 | | /* zlib inflate call */ |
631 | | /* In fact 'avail_out' may be 0 at this point, that happens at the end |
632 | | * of the read when the final LZ end code was not passed at the end of |
633 | | * the previous chunk of input data. Tell zlib if we have reached the |
634 | | * end of the output buffer. |
635 | | */ |
636 | 99.6k | ret = PNG_INFLATE(png_ptr, avail_out > 0 ? Z_NO_FLUSH : |
637 | 99.6k | (finish ? Z_FINISH : Z_SYNC_FLUSH)); |
638 | 99.6k | } while (ret == Z_OK); |
639 | | |
640 | | /* For safety kill the local buffer pointer now */ |
641 | 13.1k | if (output == NULL) |
642 | 7.55k | png_ptr->zstream.next_out = NULL; |
643 | | |
644 | | /* Claw back the 'size' and 'remaining_space' byte counts. */ |
645 | 13.1k | avail_in += png_ptr->zstream.avail_in; |
646 | 13.1k | avail_out += png_ptr->zstream.avail_out; |
647 | | |
648 | | /* Update the input and output sizes; the updated values are the amount |
649 | | * consumed or written, effectively the inverse of what zlib uses. |
650 | | */ |
651 | 13.1k | if (avail_out > 0) |
652 | 7.55k | *output_size_ptr -= avail_out; |
653 | | |
654 | 13.1k | if (avail_in > 0) |
655 | 5.11k | *input_size_ptr -= avail_in; |
656 | | |
657 | | /* Ensure png_ptr->zstream.msg is set (even in the success case!) */ |
658 | 13.1k | png_zstream_error(png_ptr, ret); |
659 | 13.1k | return ret; |
660 | 13.1k | } |
661 | | |
662 | 0 | else |
663 | 0 | { |
664 | | /* This is a bad internal error. The recovery assigns to the zstream msg |
665 | | * pointer, which is not owned by the caller, but this is safe; it's only |
666 | | * used on errors! |
667 | | */ |
668 | 0 | png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed"); |
669 | 0 | return Z_STREAM_ERROR; |
670 | 0 | } |
671 | 13.1k | } |
672 | | |
673 | | /* |
674 | | * Decompress trailing data in a chunk. The assumption is that read_buffer |
675 | | * points at an allocated area holding the contents of a chunk with a |
676 | | * trailing compressed part. What we get back is an allocated area |
677 | | * holding the original prefix part and an uncompressed version of the |
678 | | * trailing part (the malloc area passed in is freed). |
679 | | */ |
680 | | static int |
681 | | png_decompress_chunk(png_structrp png_ptr, |
682 | | png_uint_32 chunklength, png_uint_32 prefix_size, |
683 | | png_alloc_size_t *newlength /* must be initialized to the maximum! */, |
684 | | int terminate /*add a '\0' to the end of the uncompressed data*/) |
685 | 7.55k | { |
686 | | /* TODO: implement different limits for different types of chunk. |
687 | | * |
688 | | * The caller supplies *newlength set to the maximum length of the |
689 | | * uncompressed data, but this routine allocates space for the prefix and |
690 | | * maybe a '\0' terminator too. We have to assume that 'prefix_size' is |
691 | | * limited only by the maximum chunk size. |
692 | | */ |
693 | 7.55k | png_alloc_size_t limit = png_chunk_max(png_ptr); |
694 | | |
695 | 7.55k | if (limit >= prefix_size + (terminate != 0)) |
696 | 7.55k | { |
697 | 7.55k | int ret; |
698 | | |
699 | 7.55k | limit -= prefix_size + (terminate != 0); |
700 | | |
701 | 7.55k | if (limit < *newlength) |
702 | 7.55k | *newlength = limit; |
703 | | |
704 | | /* Now try to claim the stream. */ |
705 | 7.55k | ret = png_inflate_claim(png_ptr, png_ptr->chunk_name); |
706 | | |
707 | 7.55k | if (ret == Z_OK) |
708 | 7.55k | { |
709 | 7.55k | png_uint_32 lzsize = chunklength - prefix_size; |
710 | | |
711 | 7.55k | ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/, |
712 | 7.55k | /* input: */ png_ptr->read_buffer + prefix_size, &lzsize, |
713 | | /* output: */ NULL, newlength); |
714 | | |
715 | 7.55k | if (ret == Z_STREAM_END) |
716 | 5.57k | { |
717 | | /* Use 'inflateReset' here, not 'inflateReset2' because this |
718 | | * preserves the previously decided window size (otherwise it would |
719 | | * be necessary to store the previous window size.) In practice |
720 | | * this doesn't matter anyway, because png_inflate will call inflate |
721 | | * with Z_FINISH in almost all cases, so the window will not be |
722 | | * maintained. |
723 | | */ |
724 | 5.57k | if (inflateReset(&png_ptr->zstream) == Z_OK) |
725 | 5.57k | { |
726 | | /* Because of the limit checks above we know that the new, |
727 | | * expanded, size will fit in a size_t (let alone an |
728 | | * png_alloc_size_t). Use png_malloc_base here to avoid an |
729 | | * extra OOM message. |
730 | | */ |
731 | 5.57k | png_alloc_size_t new_size = *newlength; |
732 | 5.57k | png_alloc_size_t buffer_size = prefix_size + new_size + |
733 | 5.57k | (terminate != 0); |
734 | 5.57k | png_bytep text = png_voidcast(png_bytep, png_malloc_base(png_ptr, |
735 | 5.57k | buffer_size)); |
736 | | |
737 | 5.57k | if (text != NULL) |
738 | 5.57k | { |
739 | 5.57k | memset(text, 0, buffer_size); |
740 | | |
741 | 5.57k | ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/, |
742 | 5.57k | png_ptr->read_buffer + prefix_size, &lzsize, |
743 | 5.57k | text + prefix_size, newlength); |
744 | | |
745 | 5.57k | if (ret == Z_STREAM_END) |
746 | 5.57k | { |
747 | 5.57k | if (new_size == *newlength) |
748 | 5.57k | { |
749 | 5.57k | if (terminate != 0) |
750 | 5.57k | text[prefix_size + *newlength] = 0; |
751 | | |
752 | 5.57k | if (prefix_size > 0) |
753 | 5.57k | memcpy(text, png_ptr->read_buffer, prefix_size); |
754 | | |
755 | 5.57k | { |
756 | 5.57k | png_bytep old_ptr = png_ptr->read_buffer; |
757 | | |
758 | 5.57k | png_ptr->read_buffer = text; |
759 | 5.57k | png_ptr->read_buffer_size = buffer_size; |
760 | 5.57k | text = old_ptr; /* freed below */ |
761 | 5.57k | } |
762 | 5.57k | } |
763 | | |
764 | 0 | else |
765 | 0 | { |
766 | | /* The size changed on the second read, there can be no |
767 | | * guarantee that anything is correct at this point. |
768 | | * The 'msg' pointer has been set to "unexpected end of |
769 | | * LZ stream", which is fine, but return an error code |
770 | | * that the caller won't accept. |
771 | | */ |
772 | 0 | ret = PNG_UNEXPECTED_ZLIB_RETURN; |
773 | 0 | } |
774 | 5.57k | } |
775 | | |
776 | 0 | else if (ret == Z_OK) |
777 | 0 | ret = PNG_UNEXPECTED_ZLIB_RETURN; /* for safety */ |
778 | | |
779 | | /* Free the text pointer (this is the old read_buffer on |
780 | | * success) |
781 | | */ |
782 | 5.57k | png_free(png_ptr, text); |
783 | | |
784 | | /* This really is very benign, but it's still an error because |
785 | | * the extra space may otherwise be used as a Trojan Horse. |
786 | | */ |
787 | 5.57k | if (ret == Z_STREAM_END && |
788 | 5.57k | chunklength - prefix_size != lzsize) |
789 | 3.64k | png_chunk_benign_error(png_ptr, "extra compressed data"); |
790 | 5.57k | } |
791 | | |
792 | 0 | else |
793 | 0 | { |
794 | | /* Out of memory allocating the buffer */ |
795 | 0 | ret = Z_MEM_ERROR; |
796 | 0 | png_zstream_error(png_ptr, Z_MEM_ERROR); |
797 | 0 | } |
798 | 5.57k | } |
799 | | |
800 | 0 | else |
801 | 0 | { |
802 | | /* inflateReset failed, store the error message */ |
803 | 0 | png_zstream_error(png_ptr, ret); |
804 | 0 | ret = PNG_UNEXPECTED_ZLIB_RETURN; |
805 | 0 | } |
806 | 5.57k | } |
807 | | |
808 | 1.98k | else if (ret == Z_OK) |
809 | 0 | ret = PNG_UNEXPECTED_ZLIB_RETURN; |
810 | | |
811 | | /* Release the claimed stream */ |
812 | 7.55k | png_ptr->zowner = 0; |
813 | 7.55k | } |
814 | | |
815 | 0 | else /* the claim failed */ if (ret == Z_STREAM_END) /* impossible! */ |
816 | 0 | ret = PNG_UNEXPECTED_ZLIB_RETURN; |
817 | | |
818 | 7.55k | return ret; |
819 | 7.55k | } |
820 | | |
821 | 0 | else |
822 | 0 | { |
823 | | /* Application/configuration limits exceeded */ |
824 | 0 | png_zstream_error(png_ptr, Z_MEM_ERROR); |
825 | 0 | return Z_MEM_ERROR; |
826 | 0 | } |
827 | 7.55k | } |
828 | | #endif /* READ_zTXt || READ_iTXt */ |
829 | | #endif /* READ_COMPRESSED_TEXT */ |
830 | | |
831 | | #ifdef PNG_READ_iCCP_SUPPORTED |
832 | | /* Perform a partial read and decompress, producing 'avail_out' bytes and |
833 | | * reading from the current chunk as required. |
834 | | */ |
835 | | static int |
836 | | png_inflate_read(png_structrp png_ptr, png_bytep read_buffer, uInt read_size, |
837 | | png_uint_32p chunk_bytes, png_bytep next_out, png_alloc_size_t *out_size, |
838 | | int finish) |
839 | 91.0k | { |
840 | 91.0k | if (png_ptr->zowner == png_ptr->chunk_name) |
841 | 91.0k | { |
842 | 91.0k | int ret; |
843 | | |
844 | | /* next_in and avail_in must have been initialized by the caller. */ |
845 | 91.0k | png_ptr->zstream.next_out = next_out; |
846 | 91.0k | png_ptr->zstream.avail_out = 0; /* set in the loop */ |
847 | | |
848 | 91.0k | do |
849 | 117k | { |
850 | 117k | if (png_ptr->zstream.avail_in == 0) |
851 | 31.8k | { |
852 | 31.8k | if (read_size > *chunk_bytes) |
853 | 28.1k | read_size = (uInt)*chunk_bytes; |
854 | 31.8k | *chunk_bytes -= read_size; |
855 | | |
856 | 31.8k | if (read_size > 0) |
857 | 23.6k | png_crc_read(png_ptr, read_buffer, read_size); |
858 | | |
859 | 31.8k | png_ptr->zstream.next_in = read_buffer; |
860 | 31.8k | png_ptr->zstream.avail_in = read_size; |
861 | 31.8k | } |
862 | | |
863 | 117k | if (png_ptr->zstream.avail_out == 0) |
864 | 90.9k | { |
865 | 90.9k | uInt avail = ZLIB_IO_MAX; |
866 | 90.9k | if (avail > *out_size) |
867 | 90.9k | avail = (uInt)*out_size; |
868 | 90.9k | *out_size -= avail; |
869 | | |
870 | 90.9k | png_ptr->zstream.avail_out = avail; |
871 | 90.9k | } |
872 | | |
873 | | /* Use Z_SYNC_FLUSH when there is no more chunk data to ensure that all |
874 | | * the available output is produced; this allows reading of truncated |
875 | | * streams. |
876 | | */ |
877 | 117k | ret = PNG_INFLATE(png_ptr, *chunk_bytes > 0 ? |
878 | 117k | Z_NO_FLUSH : (finish ? Z_FINISH : Z_SYNC_FLUSH)); |
879 | 117k | } |
880 | 117k | while (ret == Z_OK && (*out_size > 0 || png_ptr->zstream.avail_out > 0)); |
881 | | |
882 | 91.0k | *out_size += png_ptr->zstream.avail_out; |
883 | 91.0k | png_ptr->zstream.avail_out = 0; /* Should not be required, but is safe */ |
884 | | |
885 | | /* Ensure the error message pointer is always set: */ |
886 | 91.0k | png_zstream_error(png_ptr, ret); |
887 | 91.0k | return ret; |
888 | 91.0k | } |
889 | | |
890 | 0 | else |
891 | 0 | { |
892 | 0 | png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed"); |
893 | 0 | return Z_STREAM_ERROR; |
894 | 0 | } |
895 | 91.0k | } |
896 | | #endif /* READ_iCCP */ |
897 | | |
898 | | /* CHUNK HANDLING */ |
899 | | /* Read and check the IDHR chunk */ |
900 | | static png_handle_result_code |
901 | | png_handle_IHDR(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
902 | 78.2k | { |
903 | 78.2k | png_byte buf[13]; |
904 | 78.2k | png_uint_32 width, height; |
905 | 78.2k | int bit_depth, color_type, compression_type, filter_type; |
906 | 78.2k | int interlace_type; |
907 | | |
908 | 78.2k | png_debug(1, "in png_handle_IHDR"); |
909 | | |
910 | | /* Length and position are checked by the caller. */ |
911 | | |
912 | 78.2k | png_ptr->mode |= PNG_HAVE_IHDR; |
913 | | |
914 | 78.2k | png_crc_read(png_ptr, buf, 13); |
915 | 78.2k | png_crc_finish(png_ptr, 0); |
916 | | |
917 | 78.2k | width = png_get_uint_31(png_ptr, buf); |
918 | 78.2k | height = png_get_uint_31(png_ptr, buf + 4); |
919 | 78.2k | bit_depth = buf[8]; |
920 | 78.2k | color_type = buf[9]; |
921 | 78.2k | compression_type = buf[10]; |
922 | 78.2k | filter_type = buf[11]; |
923 | 78.2k | interlace_type = buf[12]; |
924 | | |
925 | | /* Set internal variables */ |
926 | 78.2k | png_ptr->width = width; |
927 | 78.2k | png_ptr->height = height; |
928 | 78.2k | png_ptr->bit_depth = (png_byte)bit_depth; |
929 | 78.2k | png_ptr->interlaced = (png_byte)interlace_type; |
930 | 78.2k | png_ptr->color_type = (png_byte)color_type; |
931 | 78.2k | #ifdef PNG_MNG_FEATURES_SUPPORTED |
932 | 78.2k | png_ptr->filter_type = (png_byte)filter_type; |
933 | 78.2k | #endif |
934 | 78.2k | png_ptr->compression_type = (png_byte)compression_type; |
935 | | |
936 | | /* Find number of channels */ |
937 | 78.2k | switch (png_ptr->color_type) |
938 | 78.2k | { |
939 | 217 | default: /* invalid, png_set_IHDR calls png_error */ |
940 | 51.2k | case PNG_COLOR_TYPE_GRAY: |
941 | 58.1k | case PNG_COLOR_TYPE_PALETTE: |
942 | 58.1k | png_ptr->channels = 1; |
943 | 58.1k | break; |
944 | | |
945 | 9.69k | case PNG_COLOR_TYPE_RGB: |
946 | 9.69k | png_ptr->channels = 3; |
947 | 9.69k | break; |
948 | | |
949 | 2.42k | case PNG_COLOR_TYPE_GRAY_ALPHA: |
950 | 2.42k | png_ptr->channels = 2; |
951 | 2.42k | break; |
952 | | |
953 | 7.78k | case PNG_COLOR_TYPE_RGB_ALPHA: |
954 | 7.78k | png_ptr->channels = 4; |
955 | 7.78k | break; |
956 | 78.2k | } |
957 | | |
958 | | /* Set up other useful info */ |
959 | 78.0k | png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth * png_ptr->channels); |
960 | 78.0k | png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width); |
961 | 78.0k | png_debug1(3, "bit_depth = %d", png_ptr->bit_depth); |
962 | 78.0k | png_debug1(3, "channels = %d", png_ptr->channels); |
963 | 78.0k | png_debug1(3, "rowbytes = %lu", (unsigned long)png_ptr->rowbytes); |
964 | | |
965 | | /* Rely on png_set_IHDR to completely validate the data and call png_error if |
966 | | * it's wrong. |
967 | | */ |
968 | 78.0k | png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, |
969 | 78.0k | color_type, interlace_type, compression_type, filter_type); |
970 | | |
971 | 78.0k | return handled_ok; |
972 | 0 | PNG_UNUSED(length) |
973 | 0 | } |
974 | | |
975 | | /* Read and check the palette */ |
976 | | /* TODO: there are several obvious errors in this code when handling |
977 | | * out-of-place chunks and there is much over-complexity caused by trying to |
978 | | * patch up the problems. |
979 | | */ |
980 | | static png_handle_result_code |
981 | | png_handle_PLTE(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
982 | 11.0k | { |
983 | 11.0k | png_const_charp errmsg = NULL; |
984 | | |
985 | 11.0k | png_debug(1, "in png_handle_PLTE"); |
986 | | |
987 | | /* 1.6.47: consistency. This used to be especially treated as a critical |
988 | | * error even in an image which is not colour mapped, there isn't a good |
989 | | * justification for treating some errors here one way and others another so |
990 | | * everything uses the same logic. |
991 | | */ |
992 | 11.0k | if ((png_ptr->mode & PNG_HAVE_PLTE) != 0) |
993 | 2.47k | errmsg = "duplicate"; |
994 | | |
995 | 8.55k | else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0) |
996 | 1.58k | errmsg = "out of place"; |
997 | | |
998 | 6.96k | else if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0) |
999 | 1.86k | errmsg = "ignored in grayscale PNG"; |
1000 | | |
1001 | 5.10k | else if (length > 3*PNG_MAX_PALETTE_LENGTH || (length % 3) != 0) |
1002 | 610 | errmsg = "invalid"; |
1003 | | |
1004 | | /* This drops PLTE in favour of tRNS or bKGD because both of those chunks |
1005 | | * can have an effect on the rendering of the image whereas PLTE only matters |
1006 | | * in the case of an 8-bit display with a decoder which controls the palette. |
1007 | | * |
1008 | | * The alternative here is to ignore the error and store the palette anyway; |
1009 | | * destroying the tRNS will definately cause problems. |
1010 | | * |
1011 | | * NOTE: the case of PNG_COLOR_TYPE_PALETTE need not be considered because |
1012 | | * the png_handle_ routines for the three 'after PLTE' chunks tRNS, bKGD and |
1013 | | * hIST all check for a preceding PLTE in these cases. |
1014 | | */ |
1015 | 4.49k | else if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE && |
1016 | 4.49k | (png_has_chunk(png_ptr, tRNS) || png_has_chunk(png_ptr, bKGD))) |
1017 | 1.96k | errmsg = "out of place"; |
1018 | | |
1019 | 2.52k | else |
1020 | 2.52k | { |
1021 | | /* If the palette has 256 or fewer entries but is too large for the bit |
1022 | | * depth we don't issue an error to preserve the behavior of previous |
1023 | | * libpng versions. We silently truncate the unused extra palette entries |
1024 | | * here. |
1025 | | */ |
1026 | 2.52k | const unsigned max_palette_length = |
1027 | 2.52k | (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ? |
1028 | 2.16k | 1U << png_ptr->bit_depth : PNG_MAX_PALETTE_LENGTH; |
1029 | | |
1030 | | /* The cast is safe because 'length' is less than |
1031 | | * 3*PNG_MAX_PALETTE_LENGTH |
1032 | | */ |
1033 | 2.52k | const unsigned num = (length > 3U*max_palette_length) ? |
1034 | 2.49k | max_palette_length : (unsigned)length / 3U; |
1035 | | |
1036 | 2.52k | unsigned i, j; |
1037 | 2.52k | png_byte buf[3*PNG_MAX_PALETTE_LENGTH]; |
1038 | 2.52k | png_color palette[PNG_MAX_PALETTE_LENGTH]; |
1039 | | |
1040 | | /* Read the chunk into the buffer then read to the end of the chunk. */ |
1041 | 2.52k | png_crc_read(png_ptr, buf, num*3U); |
1042 | 2.52k | png_crc_finish_critical(png_ptr, length - 3U*num, |
1043 | | /* Handle as ancillary if PLTE is optional: */ |
1044 | 2.52k | png_ptr->color_type != PNG_COLOR_TYPE_PALETTE); |
1045 | | |
1046 | 40.1k | for (i = 0U, j = 0U; i < num; i++) |
1047 | 37.6k | { |
1048 | 37.6k | palette[i].red = buf[j++]; |
1049 | 37.6k | palette[i].green = buf[j++]; |
1050 | 37.6k | palette[i].blue = buf[j++]; |
1051 | 37.6k | } |
1052 | | |
1053 | | /* A valid PLTE chunk has been read */ |
1054 | 2.52k | png_ptr->mode |= PNG_HAVE_PLTE; |
1055 | | |
1056 | | /* TODO: png_set_PLTE has the side effect of setting png_ptr->palette to |
1057 | | * its own copy of the palette. This has the side effect that when |
1058 | | * png_start_row is called (this happens after any call to |
1059 | | * png_read_update_info) the info_ptr palette gets changed. This is |
1060 | | * extremely unexpected and confusing. |
1061 | | * |
1062 | | * REVIEW: there have been consistent bugs in the past about gamma and |
1063 | | * similar transforms to colour mapped images being useless because the |
1064 | | * modified palette cannot be accessed because of the above. |
1065 | | * |
1066 | | * CONSIDER: Fix this by not sharing the palette in this way. But does |
1067 | | * this completely fix the problem? |
1068 | | */ |
1069 | 2.52k | png_set_PLTE(png_ptr, info_ptr, palette, num); |
1070 | 2.52k | return handled_ok; |
1071 | 2.52k | } |
1072 | | |
1073 | | /* Here on error: errmsg is non NULL. */ |
1074 | 8.50k | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
1075 | 56 | { |
1076 | 56 | png_crc_finish(png_ptr, length); |
1077 | 56 | png_chunk_error(png_ptr, errmsg); |
1078 | 56 | } |
1079 | | |
1080 | 8.44k | else /* not critical to this image */ |
1081 | 8.44k | { |
1082 | 8.44k | png_crc_finish_critical(png_ptr, length, 1/*handle as ancillary*/); |
1083 | 8.44k | png_chunk_benign_error(png_ptr, errmsg); |
1084 | 8.44k | } |
1085 | | |
1086 | | /* Because PNG_UNUSED(errmsg) does not work if all the uses are compiled out |
1087 | | * (this does happen). |
1088 | | */ |
1089 | 8.44k | return errmsg != NULL ? handled_error : handled_error; |
1090 | 8.50k | } |
1091 | | |
1092 | | /* On read the IDAT chunk is always handled specially, even if marked for |
1093 | | * unknown handling (this is allowed), so: |
1094 | | */ |
1095 | | #define png_handle_IDAT NULL |
1096 | | |
1097 | | static png_handle_result_code |
1098 | | png_handle_IEND(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
1099 | 37.5k | { |
1100 | 37.5k | png_debug(1, "in png_handle_IEND"); |
1101 | | |
1102 | 37.5k | png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND); |
1103 | | |
1104 | 37.5k | if (length != 0) |
1105 | 3.10k | png_chunk_benign_error(png_ptr, "invalid"); |
1106 | | |
1107 | 37.5k | png_crc_finish_critical(png_ptr, length, 1/*handle as ancillary*/); |
1108 | | |
1109 | 37.5k | return handled_ok; |
1110 | 0 | PNG_UNUSED(info_ptr) |
1111 | 0 | } |
1112 | | |
1113 | | #ifdef PNG_READ_gAMA_SUPPORTED |
1114 | | static png_handle_result_code |
1115 | | png_handle_gAMA(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
1116 | 4.21k | { |
1117 | 4.21k | png_uint_32 ugamma; |
1118 | 4.21k | png_byte buf[4]; |
1119 | | |
1120 | 4.21k | png_debug(1, "in png_handle_gAMA"); |
1121 | | |
1122 | 4.21k | png_crc_read(png_ptr, buf, 4); |
1123 | | |
1124 | 4.21k | if (png_crc_finish(png_ptr, 0) != 0) |
1125 | 2.28k | return handled_error; |
1126 | | |
1127 | 1.93k | ugamma = png_get_uint_32(buf); |
1128 | | |
1129 | 1.93k | if (ugamma > PNG_UINT_31_MAX) |
1130 | 1.23k | { |
1131 | 1.23k | png_chunk_benign_error(png_ptr, "invalid"); |
1132 | 1.23k | return handled_error; |
1133 | 1.23k | } |
1134 | | |
1135 | 695 | png_set_gAMA_fixed(png_ptr, info_ptr, (png_fixed_point)/*SAFE*/ugamma); |
1136 | | |
1137 | 695 | #ifdef PNG_READ_GAMMA_SUPPORTED |
1138 | | /* PNGv3: chunk precedence for gamma is cICP, [iCCP], sRGB, gAMA. gAMA is |
1139 | | * at the end of the chain so simply check for an unset value. |
1140 | | */ |
1141 | 695 | if (png_ptr->chunk_gamma == 0) |
1142 | 634 | png_ptr->chunk_gamma = (png_fixed_point)/*SAFE*/ugamma; |
1143 | 695 | #endif /*READ_GAMMA*/ |
1144 | | |
1145 | 695 | return handled_ok; |
1146 | 0 | PNG_UNUSED(length) |
1147 | 0 | } |
1148 | | #else |
1149 | | # define png_handle_gAMA NULL |
1150 | | #endif |
1151 | | |
1152 | | #ifdef PNG_READ_sBIT_SUPPORTED |
1153 | | static png_handle_result_code /* PRIVATE */ |
1154 | | png_handle_sBIT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
1155 | 8.96k | { |
1156 | 8.96k | unsigned int truelen, i; |
1157 | 8.96k | png_byte sample_depth; |
1158 | 8.96k | png_byte buf[4]; |
1159 | | |
1160 | 8.96k | png_debug(1, "in png_handle_sBIT"); |
1161 | | |
1162 | 8.96k | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
1163 | 2.63k | { |
1164 | 2.63k | truelen = 3; |
1165 | 2.63k | sample_depth = 8; |
1166 | 2.63k | } |
1167 | | |
1168 | 6.32k | else |
1169 | 6.32k | { |
1170 | 6.32k | truelen = png_ptr->channels; |
1171 | 6.32k | sample_depth = png_ptr->bit_depth; |
1172 | 6.32k | } |
1173 | | |
1174 | 8.96k | if (length != truelen) |
1175 | 1.99k | { |
1176 | 1.99k | png_crc_finish(png_ptr, length); |
1177 | 1.99k | png_chunk_benign_error(png_ptr, "bad length"); |
1178 | 1.99k | return handled_error; |
1179 | 1.99k | } |
1180 | | |
1181 | 6.96k | buf[0] = buf[1] = buf[2] = buf[3] = sample_depth; |
1182 | 6.96k | png_crc_read(png_ptr, buf, truelen); |
1183 | | |
1184 | 6.96k | if (png_crc_finish(png_ptr, 0) != 0) |
1185 | 2.21k | return handled_error; |
1186 | | |
1187 | 12.7k | for (i=0; i<truelen; ++i) |
1188 | 12.6k | { |
1189 | 12.6k | if (buf[i] == 0 || buf[i] > sample_depth) |
1190 | 4.66k | { |
1191 | 4.66k | png_chunk_benign_error(png_ptr, "invalid"); |
1192 | 4.66k | return handled_error; |
1193 | 4.66k | } |
1194 | 12.6k | } |
1195 | | |
1196 | 90 | if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0) |
1197 | 10 | { |
1198 | 10 | png_ptr->sig_bit.red = buf[0]; |
1199 | 10 | png_ptr->sig_bit.green = buf[1]; |
1200 | 10 | png_ptr->sig_bit.blue = buf[2]; |
1201 | 10 | png_ptr->sig_bit.alpha = buf[3]; |
1202 | 10 | } |
1203 | | |
1204 | 80 | else /* grayscale */ |
1205 | 80 | { |
1206 | 80 | png_ptr->sig_bit.gray = buf[0]; |
1207 | 80 | png_ptr->sig_bit.red = buf[0]; |
1208 | 80 | png_ptr->sig_bit.green = buf[0]; |
1209 | 80 | png_ptr->sig_bit.blue = buf[0]; |
1210 | 80 | png_ptr->sig_bit.alpha = buf[1]; |
1211 | 80 | } |
1212 | | |
1213 | 90 | png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit)); |
1214 | 90 | return handled_ok; |
1215 | 4.75k | } |
1216 | | #else |
1217 | | # define png_handle_sBIT NULL |
1218 | | #endif |
1219 | | |
1220 | | #ifdef PNG_READ_cHRM_SUPPORTED |
1221 | | static png_int_32 |
1222 | | png_get_int_32_checked(png_const_bytep buf, int *error) |
1223 | 18.1k | { |
1224 | 18.1k | png_uint_32 uval = png_get_uint_32(buf); |
1225 | 18.1k | if ((uval & 0x80000000) == 0) /* non-negative */ |
1226 | 9.63k | return (png_int_32)uval; |
1227 | | |
1228 | 8.50k | uval = (uval ^ 0xffffffff) + 1; /* 2's complement: -x = ~x+1 */ |
1229 | 8.50k | if ((uval & 0x80000000) == 0) /* no overflow */ |
1230 | 5.79k | return -(png_int_32)uval; |
1231 | | |
1232 | | /* This version of png_get_int_32 has a way of returning the error to the |
1233 | | * caller, so: |
1234 | | */ |
1235 | 2.70k | *error = 1; |
1236 | 2.70k | return 0; /* Safe */ |
1237 | 8.50k | } |
1238 | | |
1239 | | static png_handle_result_code /* PRIVATE */ |
1240 | | png_handle_cHRM(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
1241 | 5.18k | { |
1242 | 5.18k | int error = 0; |
1243 | 5.18k | png_xy xy; |
1244 | 5.18k | png_byte buf[32]; |
1245 | | |
1246 | 5.18k | png_debug(1, "in png_handle_cHRM"); |
1247 | | |
1248 | 5.18k | png_crc_read(png_ptr, buf, 32); |
1249 | | |
1250 | 5.18k | if (png_crc_finish(png_ptr, 0) != 0) |
1251 | 2.85k | return handled_error; |
1252 | | |
1253 | 2.33k | xy.whitex = png_get_int_32_checked(buf + 0, &error); |
1254 | 2.33k | xy.whitey = png_get_int_32_checked(buf + 4, &error); |
1255 | 2.33k | xy.redx = png_get_int_32_checked(buf + 8, &error); |
1256 | 2.33k | xy.redy = png_get_int_32_checked(buf + 12, &error); |
1257 | 2.33k | xy.greenx = png_get_int_32_checked(buf + 16, &error); |
1258 | 2.33k | xy.greeny = png_get_int_32_checked(buf + 20, &error); |
1259 | 2.33k | xy.bluex = png_get_int_32_checked(buf + 24, &error); |
1260 | 2.33k | xy.bluey = png_get_int_32_checked(buf + 28, &error); |
1261 | | |
1262 | 2.33k | if (error) |
1263 | 1.72k | { |
1264 | 1.72k | png_chunk_benign_error(png_ptr, "invalid"); |
1265 | 1.72k | return handled_error; |
1266 | 1.72k | } |
1267 | | |
1268 | | /* png_set_cHRM may complain about some of the values but this doesn't matter |
1269 | | * because it was a cHRM and it did have vaguely (if, perhaps, ridiculous) |
1270 | | * values. Ridiculousity will be checked if the values are used later. |
1271 | | */ |
1272 | 609 | png_set_cHRM_fixed(png_ptr, info_ptr, xy.whitex, xy.whitey, xy.redx, xy.redy, |
1273 | 609 | xy.greenx, xy.greeny, xy.bluex, xy.bluey); |
1274 | | |
1275 | | /* We only use 'chromaticities' for RGB to gray */ |
1276 | 609 | # ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED |
1277 | | /* There is no need to check sRGB here, cICP is NYI and iCCP is not |
1278 | | * supported so just check mDCV. |
1279 | | */ |
1280 | 609 | if (!png_has_chunk(png_ptr, mDCV)) |
1281 | 544 | { |
1282 | 544 | png_ptr->chromaticities = xy; |
1283 | 544 | } |
1284 | 609 | # endif /* READ_RGB_TO_GRAY */ |
1285 | | |
1286 | 609 | return handled_ok; |
1287 | 0 | PNG_UNUSED(length) |
1288 | 0 | } |
1289 | | #else |
1290 | | # define png_handle_cHRM NULL |
1291 | | #endif |
1292 | | |
1293 | | #ifdef PNG_READ_sRGB_SUPPORTED |
1294 | | static png_handle_result_code /* PRIVATE */ |
1295 | | png_handle_sRGB(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
1296 | 3.45k | { |
1297 | 3.45k | png_byte intent; |
1298 | | |
1299 | 3.45k | png_debug(1, "in png_handle_sRGB"); |
1300 | | |
1301 | 3.45k | png_crc_read(png_ptr, &intent, 1); |
1302 | | |
1303 | 3.45k | if (png_crc_finish(png_ptr, 0) != 0) |
1304 | 1.85k | return handled_error; |
1305 | | |
1306 | | /* This checks the range of the "rendering intent" because it is specified in |
1307 | | * the PNG spec itself; the "reserved" values will result in the chunk not |
1308 | | * being accepted, just as they do with the various "reserved" values in |
1309 | | * IHDR. |
1310 | | */ |
1311 | 1.59k | if (intent > 3/*PNGv3 spec*/) |
1312 | 1.38k | { |
1313 | 1.38k | png_chunk_benign_error(png_ptr, "invalid"); |
1314 | 1.38k | return handled_error; |
1315 | 1.38k | } |
1316 | | |
1317 | 216 | png_set_sRGB(png_ptr, info_ptr, intent); |
1318 | | /* NOTE: png_struct::chromaticities is not set here because the RGB to gray |
1319 | | * coefficients are known without a need for the chromaticities. |
1320 | | */ |
1321 | | |
1322 | 216 | #ifdef PNG_READ_GAMMA_SUPPORTED |
1323 | | /* PNGv3: chunk precedence for gamma is cICP, [iCCP], sRGB, gAMA. iCCP is |
1324 | | * not supported by libpng so the only requirement is to check for cICP |
1325 | | * setting the gamma (this is NYI, but this check is safe.) |
1326 | | */ |
1327 | 216 | if (!png_has_chunk(png_ptr, cICP) || png_ptr->chunk_gamma == 0) |
1328 | 176 | png_ptr->chunk_gamma = PNG_GAMMA_sRGB_INVERSE; |
1329 | 216 | #endif /*READ_GAMMA*/ |
1330 | | |
1331 | 216 | return handled_ok; |
1332 | 0 | PNG_UNUSED(length) |
1333 | 0 | } |
1334 | | #else |
1335 | | # define png_handle_sRGB NULL |
1336 | | #endif /* READ_sRGB */ |
1337 | | |
1338 | | #ifdef PNG_READ_iCCP_SUPPORTED |
1339 | | static png_handle_result_code /* PRIVATE */ |
1340 | | png_handle_iCCP(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
1341 | | /* Note: this does not properly handle profiles that are > 64K under DOS */ |
1342 | 58.9k | { |
1343 | 58.9k | png_const_charp errmsg = NULL; /* error message output, or no error */ |
1344 | 58.9k | int finished = 0; /* crc checked */ |
1345 | | |
1346 | 58.9k | png_debug(1, "in png_handle_iCCP"); |
1347 | | |
1348 | | /* PNGv3: allow PNG files with both sRGB and iCCP because the PNG spec only |
1349 | | * ever said that there "should" be only one, not "shall" and the PNGv3 |
1350 | | * colour chunk precedence rules give a handling for this case anyway. |
1351 | | */ |
1352 | 58.9k | { |
1353 | 58.9k | uInt read_length, keyword_length; |
1354 | 58.9k | char keyword[81]; |
1355 | | |
1356 | | /* Find the keyword; the keyword plus separator and compression method |
1357 | | * bytes can be at most 81 characters long. |
1358 | | */ |
1359 | 58.9k | read_length = 81; /* maximum */ |
1360 | 58.9k | if (read_length > length) |
1361 | 3.25k | read_length = (uInt)/*SAFE*/length; |
1362 | | |
1363 | 58.9k | png_crc_read(png_ptr, (png_bytep)keyword, read_length); |
1364 | 58.9k | length -= read_length; |
1365 | | |
1366 | 58.9k | if (length < LZ77Min) |
1367 | 3.27k | { |
1368 | 3.27k | png_crc_finish(png_ptr, length); |
1369 | 3.27k | png_chunk_benign_error(png_ptr, "too short"); |
1370 | 3.27k | return handled_error; |
1371 | 3.27k | } |
1372 | | |
1373 | 55.6k | keyword_length = 0; |
1374 | 418k | while (keyword_length < 80 && keyword_length < read_length && |
1375 | 418k | keyword[keyword_length] != 0) |
1376 | 362k | ++keyword_length; |
1377 | | |
1378 | | /* TODO: make the keyword checking common */ |
1379 | 55.6k | if (keyword_length >= 1 && keyword_length <= 79) |
1380 | 54.7k | { |
1381 | | /* We only understand '0' compression - deflate - so if we get a |
1382 | | * different value we can't safely decode the chunk. |
1383 | | */ |
1384 | 54.7k | if (keyword_length+1 < read_length && |
1385 | 54.7k | keyword[keyword_length+1] == PNG_COMPRESSION_TYPE_BASE) |
1386 | 54.0k | { |
1387 | 54.0k | read_length -= keyword_length+2; |
1388 | | |
1389 | 54.0k | if (png_inflate_claim(png_ptr, png_iCCP) == Z_OK) |
1390 | 54.0k | { |
1391 | 54.0k | Byte profile_header[132]={0}; |
1392 | 54.0k | Byte local_buffer[PNG_INFLATE_BUF_SIZE]; |
1393 | 54.0k | png_alloc_size_t size = (sizeof profile_header); |
1394 | | |
1395 | 54.0k | png_ptr->zstream.next_in = (Bytef*)keyword + (keyword_length+2); |
1396 | 54.0k | png_ptr->zstream.avail_in = read_length; |
1397 | 54.0k | (void)png_inflate_read(png_ptr, local_buffer, |
1398 | 54.0k | (sizeof local_buffer), &length, profile_header, &size, |
1399 | 54.0k | 0/*finish: don't, because the output is too small*/); |
1400 | | |
1401 | 54.0k | if (size == 0) |
1402 | 36.1k | { |
1403 | | /* We have the ICC profile header; do the basic header checks. |
1404 | | */ |
1405 | 36.1k | png_uint_32 profile_length = png_get_uint_32(profile_header); |
1406 | | |
1407 | 36.1k | if (png_icc_check_length(png_ptr, keyword, profile_length) != |
1408 | 36.1k | 0) |
1409 | 34.4k | { |
1410 | | /* The length is apparently ok, so we can check the 132 |
1411 | | * byte header. |
1412 | | */ |
1413 | 34.4k | if (png_icc_check_header(png_ptr, keyword, profile_length, |
1414 | 34.4k | profile_header, png_ptr->color_type) != 0) |
1415 | 20.7k | { |
1416 | | /* Now read the tag table; a variable size buffer is |
1417 | | * needed at this point, allocate one for the whole |
1418 | | * profile. The header check has already validated |
1419 | | * that none of this stuff will overflow. |
1420 | | */ |
1421 | 20.7k | png_uint_32 tag_count = |
1422 | 20.7k | png_get_uint_32(profile_header + 128); |
1423 | 20.7k | png_bytep profile = png_read_buffer(png_ptr, |
1424 | 20.7k | profile_length); |
1425 | | |
1426 | 20.7k | if (profile != NULL) |
1427 | 20.7k | { |
1428 | 20.7k | memcpy(profile, profile_header, |
1429 | 20.7k | (sizeof profile_header)); |
1430 | | |
1431 | 20.7k | size = 12 * tag_count; |
1432 | | |
1433 | 20.7k | (void)png_inflate_read(png_ptr, local_buffer, |
1434 | 20.7k | (sizeof local_buffer), &length, |
1435 | 20.7k | profile + (sizeof profile_header), &size, 0); |
1436 | | |
1437 | | /* Still expect a buffer error because we expect |
1438 | | * there to be some tag data! |
1439 | | */ |
1440 | 20.7k | if (size == 0) |
1441 | 18.6k | { |
1442 | 18.6k | if (png_icc_check_tag_table(png_ptr, |
1443 | 18.6k | keyword, profile_length, profile) != 0) |
1444 | 16.2k | { |
1445 | | /* The profile has been validated for basic |
1446 | | * security issues, so read the whole thing in. |
1447 | | */ |
1448 | 16.2k | size = profile_length - (sizeof profile_header) |
1449 | 16.2k | - 12 * tag_count; |
1450 | | |
1451 | 16.2k | (void)png_inflate_read(png_ptr, local_buffer, |
1452 | 16.2k | (sizeof local_buffer), &length, |
1453 | 16.2k | profile + (sizeof profile_header) + |
1454 | 16.2k | 12 * tag_count, &size, 1/*finish*/); |
1455 | | |
1456 | 16.2k | if (length > 0 && !(png_ptr->flags & |
1457 | 5.81k | PNG_FLAG_BENIGN_ERRORS_WARN)) |
1458 | 0 | errmsg = "extra compressed data"; |
1459 | | |
1460 | | /* But otherwise allow extra data: */ |
1461 | 16.2k | else if (size == 0) |
1462 | 4.79k | { |
1463 | 4.79k | if (length > 0) |
1464 | 1.28k | { |
1465 | | /* This can be handled completely, so |
1466 | | * keep going. |
1467 | | */ |
1468 | 1.28k | png_chunk_warning(png_ptr, |
1469 | 1.28k | "extra compressed data"); |
1470 | 1.28k | } |
1471 | | |
1472 | 4.79k | png_crc_finish(png_ptr, length); |
1473 | 4.79k | finished = 1; |
1474 | | |
1475 | | /* Steal the profile for info_ptr. */ |
1476 | 4.79k | if (info_ptr != NULL) |
1477 | 4.60k | { |
1478 | 4.60k | png_free_data(png_ptr, info_ptr, |
1479 | 4.60k | PNG_FREE_ICCP, 0); |
1480 | | |
1481 | 4.60k | info_ptr->iccp_name = png_voidcast(char*, |
1482 | 4.60k | png_malloc_base(png_ptr, |
1483 | 4.60k | keyword_length+1)); |
1484 | 4.60k | if (info_ptr->iccp_name != NULL) |
1485 | 4.60k | { |
1486 | 4.60k | memcpy(info_ptr->iccp_name, keyword, |
1487 | 4.60k | keyword_length+1); |
1488 | 4.60k | info_ptr->iccp_proflen = |
1489 | 4.60k | profile_length; |
1490 | 4.60k | info_ptr->iccp_profile = profile; |
1491 | 4.60k | png_ptr->read_buffer = NULL; /*steal*/ |
1492 | 4.60k | info_ptr->free_me |= PNG_FREE_ICCP; |
1493 | 4.60k | info_ptr->valid |= PNG_INFO_iCCP; |
1494 | 4.60k | } |
1495 | | |
1496 | 0 | else |
1497 | 0 | errmsg = "out of memory"; |
1498 | 4.60k | } |
1499 | | |
1500 | | /* else the profile remains in the read |
1501 | | * buffer which gets reused for subsequent |
1502 | | * chunks. |
1503 | | */ |
1504 | | |
1505 | 4.79k | if (errmsg == NULL) |
1506 | 4.60k | { |
1507 | 4.60k | png_ptr->zowner = 0; |
1508 | 4.60k | return handled_ok; |
1509 | 4.60k | } |
1510 | 4.79k | } |
1511 | 11.6k | if (errmsg == NULL) |
1512 | 10.9k | errmsg = png_ptr->zstream.msg; |
1513 | 11.6k | } |
1514 | | /* else png_icc_check_tag_table output an error */ |
1515 | 18.6k | } |
1516 | 2.09k | else /* profile truncated */ |
1517 | 2.09k | errmsg = png_ptr->zstream.msg; |
1518 | 20.7k | } |
1519 | | |
1520 | 0 | else |
1521 | 0 | errmsg = "out of memory"; |
1522 | 20.7k | } |
1523 | | |
1524 | | /* else png_icc_check_header output an error */ |
1525 | 34.4k | } |
1526 | | |
1527 | | /* else png_icc_check_length output an error */ |
1528 | 36.1k | } |
1529 | | |
1530 | 17.8k | else /* profile truncated */ |
1531 | 17.8k | errmsg = png_ptr->zstream.msg; |
1532 | | |
1533 | | /* Release the stream */ |
1534 | 49.4k | png_ptr->zowner = 0; |
1535 | 49.4k | } |
1536 | | |
1537 | 0 | else /* png_inflate_claim failed */ |
1538 | 0 | errmsg = png_ptr->zstream.msg; |
1539 | 54.0k | } |
1540 | | |
1541 | 677 | else |
1542 | 677 | errmsg = "bad compression method"; /* or missing */ |
1543 | 54.7k | } |
1544 | | |
1545 | 988 | else |
1546 | 988 | errmsg = "bad keyword"; |
1547 | 55.6k | } |
1548 | | |
1549 | | /* Failure: the reason is in 'errmsg' */ |
1550 | 51.0k | if (finished == 0) |
1551 | 49.6k | png_crc_finish(png_ptr, length); |
1552 | | |
1553 | 51.0k | if (errmsg != NULL) /* else already output */ |
1554 | 28.7k | png_chunk_benign_error(png_ptr, errmsg); |
1555 | | |
1556 | 51.0k | return handled_error; |
1557 | 55.6k | } |
1558 | | #else |
1559 | | # define png_handle_iCCP NULL |
1560 | | #endif /* READ_iCCP */ |
1561 | | |
1562 | | #ifdef PNG_READ_sPLT_SUPPORTED |
1563 | | static png_handle_result_code /* PRIVATE */ |
1564 | | png_handle_sPLT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
1565 | | /* Note: this does not properly handle chunks that are > 64K under DOS */ |
1566 | 0 | { |
1567 | 0 | png_bytep entry_start, buffer; |
1568 | 0 | png_sPLT_t new_palette; |
1569 | 0 | png_sPLT_entryp pp; |
1570 | 0 | png_uint_32 data_length; |
1571 | 0 | int entry_size, i; |
1572 | 0 | png_uint_32 skip = 0; |
1573 | 0 | png_uint_32 dl; |
1574 | 0 | size_t max_dl; |
1575 | |
|
1576 | 0 | png_debug(1, "in png_handle_sPLT"); |
1577 | |
|
1578 | 0 | #ifdef PNG_USER_LIMITS_SUPPORTED |
1579 | 0 | if (png_ptr->user_chunk_cache_max != 0) |
1580 | 0 | { |
1581 | 0 | if (png_ptr->user_chunk_cache_max == 1) |
1582 | 0 | { |
1583 | 0 | png_crc_finish(png_ptr, length); |
1584 | 0 | return handled_error; |
1585 | 0 | } |
1586 | | |
1587 | 0 | if (--png_ptr->user_chunk_cache_max == 1) |
1588 | 0 | { |
1589 | 0 | png_warning(png_ptr, "No space in chunk cache for sPLT"); |
1590 | 0 | png_crc_finish(png_ptr, length); |
1591 | 0 | return handled_error; |
1592 | 0 | } |
1593 | 0 | } |
1594 | 0 | #endif |
1595 | | |
1596 | 0 | buffer = png_read_buffer(png_ptr, length+1); |
1597 | 0 | if (buffer == NULL) |
1598 | 0 | { |
1599 | 0 | png_crc_finish(png_ptr, length); |
1600 | 0 | png_chunk_benign_error(png_ptr, "out of memory"); |
1601 | 0 | return handled_error; |
1602 | 0 | } |
1603 | | |
1604 | | |
1605 | | /* WARNING: this may break if size_t is less than 32 bits; it is assumed |
1606 | | * that the PNG_MAX_MALLOC_64K test is enabled in this case, but this is a |
1607 | | * potential breakage point if the types in pngconf.h aren't exactly right. |
1608 | | */ |
1609 | 0 | png_crc_read(png_ptr, buffer, length); |
1610 | |
|
1611 | 0 | if (png_crc_finish(png_ptr, skip) != 0) |
1612 | 0 | return handled_error; |
1613 | | |
1614 | 0 | buffer[length] = 0; |
1615 | |
|
1616 | 0 | for (entry_start = buffer; *entry_start; entry_start++) |
1617 | 0 | /* Empty loop to find end of name */ ; |
1618 | |
|
1619 | 0 | ++entry_start; |
1620 | | |
1621 | | /* A sample depth should follow the separator, and we should be on it */ |
1622 | 0 | if (length < 2U || entry_start > buffer + (length - 2U)) |
1623 | 0 | { |
1624 | 0 | png_warning(png_ptr, "malformed sPLT chunk"); |
1625 | 0 | return handled_error; |
1626 | 0 | } |
1627 | | |
1628 | 0 | new_palette.depth = *entry_start++; |
1629 | 0 | entry_size = (new_palette.depth == 8 ? 6 : 10); |
1630 | | /* This must fit in a png_uint_32 because it is derived from the original |
1631 | | * chunk data length. |
1632 | | */ |
1633 | 0 | data_length = length - (png_uint_32)(entry_start - buffer); |
1634 | | |
1635 | | /* Integrity-check the data length */ |
1636 | 0 | if ((data_length % (unsigned int)entry_size) != 0) |
1637 | 0 | { |
1638 | 0 | png_warning(png_ptr, "sPLT chunk has bad length"); |
1639 | 0 | return handled_error; |
1640 | 0 | } |
1641 | | |
1642 | 0 | dl = (png_uint_32)(data_length / (unsigned int)entry_size); |
1643 | 0 | max_dl = PNG_SIZE_MAX / (sizeof (png_sPLT_entry)); |
1644 | |
|
1645 | 0 | if (dl > max_dl) |
1646 | 0 | { |
1647 | 0 | png_warning(png_ptr, "sPLT chunk too long"); |
1648 | 0 | return handled_error; |
1649 | 0 | } |
1650 | | |
1651 | 0 | new_palette.nentries = (png_int_32)(data_length / (unsigned int)entry_size); |
1652 | |
|
1653 | 0 | new_palette.entries = (png_sPLT_entryp)png_malloc_warn(png_ptr, |
1654 | 0 | (png_alloc_size_t) new_palette.nentries * (sizeof (png_sPLT_entry))); |
1655 | |
|
1656 | 0 | if (new_palette.entries == NULL) |
1657 | 0 | { |
1658 | 0 | png_warning(png_ptr, "sPLT chunk requires too much memory"); |
1659 | 0 | return handled_error; |
1660 | 0 | } |
1661 | | |
1662 | 0 | for (i = 0; i < new_palette.nentries; i++) |
1663 | 0 | { |
1664 | 0 | pp = new_palette.entries + i; |
1665 | |
|
1666 | 0 | if (new_palette.depth == 8) |
1667 | 0 | { |
1668 | 0 | pp->red = *entry_start++; |
1669 | 0 | pp->green = *entry_start++; |
1670 | 0 | pp->blue = *entry_start++; |
1671 | 0 | pp->alpha = *entry_start++; |
1672 | 0 | } |
1673 | | |
1674 | 0 | else |
1675 | 0 | { |
1676 | 0 | pp->red = png_get_uint_16(entry_start); entry_start += 2; |
1677 | 0 | pp->green = png_get_uint_16(entry_start); entry_start += 2; |
1678 | 0 | pp->blue = png_get_uint_16(entry_start); entry_start += 2; |
1679 | 0 | pp->alpha = png_get_uint_16(entry_start); entry_start += 2; |
1680 | 0 | } |
1681 | |
|
1682 | 0 | pp->frequency = png_get_uint_16(entry_start); entry_start += 2; |
1683 | 0 | } |
1684 | | |
1685 | | /* Discard all chunk data except the name and stash that */ |
1686 | 0 | new_palette.name = (png_charp)buffer; |
1687 | |
|
1688 | 0 | png_set_sPLT(png_ptr, info_ptr, &new_palette, 1); |
1689 | |
|
1690 | 0 | png_free(png_ptr, new_palette.entries); |
1691 | 0 | return handled_ok; |
1692 | 0 | } |
1693 | | #else |
1694 | | # define png_handle_sPLT NULL |
1695 | | #endif /* READ_sPLT */ |
1696 | | |
1697 | | #ifdef PNG_READ_tRNS_SUPPORTED |
1698 | | static png_handle_result_code /* PRIVATE */ |
1699 | | png_handle_tRNS(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
1700 | 20.8k | { |
1701 | 20.8k | png_byte readbuf[PNG_MAX_PALETTE_LENGTH]; |
1702 | | |
1703 | 20.8k | png_debug(1, "in png_handle_tRNS"); |
1704 | | |
1705 | 20.8k | if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) |
1706 | 5.70k | { |
1707 | 5.70k | png_byte buf[2]; |
1708 | | |
1709 | 5.70k | if (length != 2) |
1710 | 2.95k | { |
1711 | 2.95k | png_crc_finish(png_ptr, length); |
1712 | 2.95k | png_chunk_benign_error(png_ptr, "invalid"); |
1713 | 2.95k | return handled_error; |
1714 | 2.95k | } |
1715 | | |
1716 | 2.74k | png_crc_read(png_ptr, buf, 2); |
1717 | 2.74k | png_ptr->num_trans = 1; |
1718 | 2.74k | png_ptr->trans_color.gray = png_get_uint_16(buf); |
1719 | 2.74k | } |
1720 | | |
1721 | 15.1k | else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB) |
1722 | 5.00k | { |
1723 | 5.00k | png_byte buf[6]; |
1724 | | |
1725 | 5.00k | if (length != 6) |
1726 | 2.23k | { |
1727 | 2.23k | png_crc_finish(png_ptr, length); |
1728 | 2.23k | png_chunk_benign_error(png_ptr, "invalid"); |
1729 | 2.23k | return handled_error; |
1730 | 2.23k | } |
1731 | | |
1732 | 2.77k | png_crc_read(png_ptr, buf, length); |
1733 | 2.77k | png_ptr->num_trans = 1; |
1734 | 2.77k | png_ptr->trans_color.red = png_get_uint_16(buf); |
1735 | 2.77k | png_ptr->trans_color.green = png_get_uint_16(buf + 2); |
1736 | 2.77k | png_ptr->trans_color.blue = png_get_uint_16(buf + 4); |
1737 | 2.77k | } |
1738 | | |
1739 | 10.1k | else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
1740 | 8.02k | { |
1741 | 8.02k | if ((png_ptr->mode & PNG_HAVE_PLTE) == 0) |
1742 | 1.86k | { |
1743 | 1.86k | png_crc_finish(png_ptr, length); |
1744 | 1.86k | png_chunk_benign_error(png_ptr, "out of place"); |
1745 | 1.86k | return handled_error; |
1746 | 1.86k | } |
1747 | | |
1748 | 6.16k | if (length > (unsigned int) png_ptr->num_palette || |
1749 | 6.16k | length > (unsigned int) PNG_MAX_PALETTE_LENGTH || |
1750 | 6.16k | length == 0) |
1751 | 3.30k | { |
1752 | 3.30k | png_crc_finish(png_ptr, length); |
1753 | 3.30k | png_chunk_benign_error(png_ptr, "invalid"); |
1754 | 3.30k | return handled_error; |
1755 | 3.30k | } |
1756 | | |
1757 | 2.85k | png_crc_read(png_ptr, readbuf, length); |
1758 | 2.85k | png_ptr->num_trans = (png_uint_16)length; |
1759 | 2.85k | } |
1760 | | |
1761 | 2.13k | else |
1762 | 2.13k | { |
1763 | 2.13k | png_crc_finish(png_ptr, length); |
1764 | 2.13k | png_chunk_benign_error(png_ptr, "invalid with alpha channel"); |
1765 | 2.13k | return handled_error; |
1766 | 2.13k | } |
1767 | | |
1768 | 8.37k | if (png_crc_finish(png_ptr, 0) != 0) |
1769 | 6.64k | { |
1770 | 6.64k | png_ptr->num_trans = 0; |
1771 | 6.64k | return handled_error; |
1772 | 6.64k | } |
1773 | | |
1774 | | /* TODO: this is a horrible side effect in the palette case because the |
1775 | | * png_struct ends up with a pointer to the tRNS buffer owned by the |
1776 | | * png_info. Fix this. |
1777 | | */ |
1778 | 1.72k | png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans, |
1779 | 1.72k | &(png_ptr->trans_color)); |
1780 | 1.72k | return handled_ok; |
1781 | 8.37k | } |
1782 | | #else |
1783 | | # define png_handle_tRNS NULL |
1784 | | #endif |
1785 | | |
1786 | | #ifdef PNG_READ_bKGD_SUPPORTED |
1787 | | static png_handle_result_code /* PRIVATE */ |
1788 | | png_handle_bKGD(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
1789 | 18.1k | { |
1790 | 18.1k | unsigned int truelen; |
1791 | 18.1k | png_byte buf[6]; |
1792 | 18.1k | png_color_16 background; |
1793 | | |
1794 | 18.1k | png_debug(1, "in png_handle_bKGD"); |
1795 | | |
1796 | 18.1k | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
1797 | 6.07k | { |
1798 | 6.07k | if ((png_ptr->mode & PNG_HAVE_PLTE) == 0) |
1799 | 1.76k | { |
1800 | 1.76k | png_crc_finish(png_ptr, length); |
1801 | 1.76k | png_chunk_benign_error(png_ptr, "out of place"); |
1802 | 1.76k | return handled_error; |
1803 | 1.76k | } |
1804 | | |
1805 | 4.31k | truelen = 1; |
1806 | 4.31k | } |
1807 | | |
1808 | 12.0k | else if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0) |
1809 | 6.58k | truelen = 6; |
1810 | | |
1811 | 5.48k | else |
1812 | 5.48k | truelen = 2; |
1813 | | |
1814 | 16.3k | if (length != truelen) |
1815 | 3.02k | { |
1816 | 3.02k | png_crc_finish(png_ptr, length); |
1817 | 3.02k | png_chunk_benign_error(png_ptr, "invalid"); |
1818 | 3.02k | return handled_error; |
1819 | 3.02k | } |
1820 | | |
1821 | 13.3k | png_crc_read(png_ptr, buf, truelen); |
1822 | | |
1823 | 13.3k | if (png_crc_finish(png_ptr, 0) != 0) |
1824 | 2.67k | return handled_error; |
1825 | | |
1826 | | /* We convert the index value into RGB components so that we can allow |
1827 | | * arbitrary RGB values for background when we have transparency, and |
1828 | | * so it is easy to determine the RGB values of the background color |
1829 | | * from the info_ptr struct. |
1830 | | */ |
1831 | 10.6k | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
1832 | 2.34k | { |
1833 | 2.34k | background.index = buf[0]; |
1834 | | |
1835 | 2.34k | if (info_ptr != NULL && info_ptr->num_palette != 0) |
1836 | 2.34k | { |
1837 | 2.34k | if (buf[0] >= info_ptr->num_palette) |
1838 | 2.29k | { |
1839 | 2.29k | png_chunk_benign_error(png_ptr, "invalid index"); |
1840 | 2.29k | return handled_error; |
1841 | 2.29k | } |
1842 | | |
1843 | 47 | background.red = (png_uint_16)png_ptr->palette[buf[0]].red; |
1844 | 47 | background.green = (png_uint_16)png_ptr->palette[buf[0]].green; |
1845 | 47 | background.blue = (png_uint_16)png_ptr->palette[buf[0]].blue; |
1846 | 47 | } |
1847 | | |
1848 | 0 | else |
1849 | 0 | background.red = background.green = background.blue = 0; |
1850 | | |
1851 | 47 | background.gray = 0; |
1852 | 47 | } |
1853 | | |
1854 | 8.33k | else if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0) /* GRAY */ |
1855 | 3.37k | { |
1856 | 3.37k | if (png_ptr->bit_depth <= 8) |
1857 | 3.33k | { |
1858 | 3.33k | if (buf[0] != 0 || buf[1] >= (unsigned int)(1 << png_ptr->bit_depth)) |
1859 | 3.30k | { |
1860 | 3.30k | png_chunk_benign_error(png_ptr, "invalid gray level"); |
1861 | 3.30k | return handled_error; |
1862 | 3.30k | } |
1863 | 3.33k | } |
1864 | | |
1865 | 74 | background.index = 0; |
1866 | 74 | background.red = |
1867 | 74 | background.green = |
1868 | 74 | background.blue = |
1869 | 74 | background.gray = png_get_uint_16(buf); |
1870 | 74 | } |
1871 | | |
1872 | 4.95k | else |
1873 | 4.95k | { |
1874 | 4.95k | if (png_ptr->bit_depth <= 8) |
1875 | 4.85k | { |
1876 | 4.85k | if (buf[0] != 0 || buf[2] != 0 || buf[4] != 0) |
1877 | 4.70k | { |
1878 | 4.70k | png_chunk_benign_error(png_ptr, "invalid color"); |
1879 | 4.70k | return handled_error; |
1880 | 4.70k | } |
1881 | 4.85k | } |
1882 | | |
1883 | 254 | background.index = 0; |
1884 | 254 | background.red = png_get_uint_16(buf); |
1885 | 254 | background.green = png_get_uint_16(buf + 2); |
1886 | 254 | background.blue = png_get_uint_16(buf + 4); |
1887 | 254 | background.gray = 0; |
1888 | 254 | } |
1889 | | |
1890 | 375 | png_set_bKGD(png_ptr, info_ptr, &background); |
1891 | 375 | return handled_ok; |
1892 | 10.6k | } |
1893 | | #else |
1894 | | # define png_handle_bKGD NULL |
1895 | | #endif |
1896 | | |
1897 | | #ifdef PNG_READ_cICP_SUPPORTED |
1898 | | static png_handle_result_code /* PRIVATE */ |
1899 | | png_handle_cICP(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
1900 | 1.00k | { |
1901 | 1.00k | png_byte buf[4]; |
1902 | | |
1903 | 1.00k | png_debug(1, "in png_handle_cICP"); |
1904 | | |
1905 | 1.00k | png_crc_read(png_ptr, buf, 4); |
1906 | | |
1907 | 1.00k | if (png_crc_finish(png_ptr, 0) != 0) |
1908 | 954 | return handled_error; |
1909 | | |
1910 | 50 | png_set_cICP(png_ptr, info_ptr, buf[0], buf[1], buf[2], buf[3]); |
1911 | | |
1912 | | /* We only use 'chromaticities' for RGB to gray */ |
1913 | 50 | # ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED |
1914 | 50 | if (!png_has_chunk(png_ptr, mDCV)) |
1915 | 15 | { |
1916 | | /* TODO: png_ptr->chromaticities = chromaticities; */ |
1917 | 15 | } |
1918 | 50 | # endif /* READ_RGB_TO_GRAY */ |
1919 | | |
1920 | 50 | #ifdef PNG_READ_GAMMA_SUPPORTED |
1921 | | /* PNGv3: chunk precedence for gamma is cICP, [iCCP], sRGB, gAMA. cICP is |
1922 | | * at the head so simply set the gamma if it can be determined. If not |
1923 | | * chunk_gamma remains unchanged; sRGB and gAMA handling check it for |
1924 | | * being zero. |
1925 | | */ |
1926 | | /* TODO: set png_struct::chunk_gamma when possible */ |
1927 | 50 | #endif /*READ_GAMMA*/ |
1928 | | |
1929 | 50 | return handled_ok; |
1930 | 0 | PNG_UNUSED(length) |
1931 | 0 | } |
1932 | | #else |
1933 | | # define png_handle_cICP NULL |
1934 | | #endif |
1935 | | |
1936 | | #ifdef PNG_READ_cLLI_SUPPORTED |
1937 | | static png_handle_result_code /* PRIVATE */ |
1938 | | png_handle_cLLI(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
1939 | 857 | { |
1940 | 857 | png_byte buf[8]; |
1941 | | |
1942 | 857 | png_debug(1, "in png_handle_cLLI"); |
1943 | | |
1944 | 857 | png_crc_read(png_ptr, buf, 8); |
1945 | | |
1946 | 857 | if (png_crc_finish(png_ptr, 0) != 0) |
1947 | 792 | return handled_error; |
1948 | | |
1949 | | /* The error checking happens here, this puts it in just one place: */ |
1950 | 65 | png_set_cLLI_fixed(png_ptr, info_ptr, png_get_uint_32(buf), |
1951 | 65 | png_get_uint_32(buf+4)); |
1952 | 65 | return handled_ok; |
1953 | 0 | PNG_UNUSED(length) |
1954 | 0 | } |
1955 | | #else |
1956 | | # define png_handle_cLLI NULL |
1957 | | #endif |
1958 | | |
1959 | | #ifdef PNG_READ_mDCV_SUPPORTED |
1960 | | static png_handle_result_code /* PRIVATE */ |
1961 | | png_handle_mDCV(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
1962 | 1.07k | { |
1963 | 1.07k | png_xy chromaticities; |
1964 | 1.07k | png_byte buf[24]; |
1965 | | |
1966 | 1.07k | png_debug(1, "in png_handle_mDCV"); |
1967 | | |
1968 | 1.07k | png_crc_read(png_ptr, buf, 24); |
1969 | | |
1970 | 1.07k | if (png_crc_finish(png_ptr, 0) != 0) |
1971 | 961 | return handled_error; |
1972 | | |
1973 | | /* The error checking happens here, this puts it in just one place. The |
1974 | | * odd /50000 scaling factor makes it more difficult but the (x.y) values are |
1975 | | * only two bytes so a <<1 is safe. |
1976 | | * |
1977 | | * WARNING: the PNG specification defines the cHRM chunk to **start** with |
1978 | | * the white point (x,y). The W3C PNG v3 specification puts the white point |
1979 | | * **after* R,G,B. The x,y values in mDCV are also scaled by 50,000 and |
1980 | | * stored in just two bytes, whereas those in cHRM are scaled by 100,000 and |
1981 | | * stored in four bytes. This is very, very confusing. These APIs remove |
1982 | | * the confusion by copying the existing, well established, API. |
1983 | | */ |
1984 | 115 | chromaticities.redx = png_get_uint_16(buf+ 0U) << 1; /* red x */ |
1985 | 115 | chromaticities.redy = png_get_uint_16(buf+ 2U) << 1; /* red y */ |
1986 | 115 | chromaticities.greenx = png_get_uint_16(buf+ 4U) << 1; /* green x */ |
1987 | 115 | chromaticities.greeny = png_get_uint_16(buf+ 6U) << 1; /* green y */ |
1988 | 115 | chromaticities.bluex = png_get_uint_16(buf+ 8U) << 1; /* blue x */ |
1989 | 115 | chromaticities.bluey = png_get_uint_16(buf+10U) << 1; /* blue y */ |
1990 | 115 | chromaticities.whitex = png_get_uint_16(buf+12U) << 1; /* white x */ |
1991 | 115 | chromaticities.whitey = png_get_uint_16(buf+14U) << 1; /* white y */ |
1992 | | |
1993 | 115 | png_set_mDCV_fixed(png_ptr, info_ptr, |
1994 | 115 | chromaticities.whitex, chromaticities.whitey, |
1995 | 115 | chromaticities.redx, chromaticities.redy, |
1996 | 115 | chromaticities.greenx, chromaticities.greeny, |
1997 | 115 | chromaticities.bluex, chromaticities.bluey, |
1998 | 115 | png_get_uint_32(buf+16U), /* peak luminance */ |
1999 | 115 | png_get_uint_32(buf+20U));/* minimum perceivable luminance */ |
2000 | | |
2001 | | /* We only use 'chromaticities' for RGB to gray */ |
2002 | 115 | # ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED |
2003 | 115 | png_ptr->chromaticities = chromaticities; |
2004 | 115 | # endif /* READ_RGB_TO_GRAY */ |
2005 | | |
2006 | 115 | return handled_ok; |
2007 | 0 | PNG_UNUSED(length) |
2008 | 0 | } |
2009 | | #else |
2010 | | # define png_handle_mDCV NULL |
2011 | | #endif |
2012 | | |
2013 | | #ifdef PNG_READ_eXIf_SUPPORTED |
2014 | | static png_handle_result_code /* PRIVATE */ |
2015 | | png_handle_eXIf(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
2016 | 5.13k | { |
2017 | 5.13k | png_bytep buffer = NULL; |
2018 | | |
2019 | 5.13k | png_debug(1, "in png_handle_eXIf"); |
2020 | | |
2021 | 5.13k | buffer = png_read_buffer(png_ptr, length); |
2022 | | |
2023 | 5.13k | if (buffer == NULL) |
2024 | 0 | { |
2025 | 0 | png_crc_finish(png_ptr, length); |
2026 | 0 | png_chunk_benign_error(png_ptr, "out of memory"); |
2027 | 0 | return handled_error; |
2028 | 0 | } |
2029 | | |
2030 | 5.13k | png_crc_read(png_ptr, buffer, length); |
2031 | | |
2032 | 5.13k | if (png_crc_finish(png_ptr, 0) != 0) |
2033 | 4.05k | return handled_error; |
2034 | | |
2035 | | /* PNGv3: the code used to check the byte order mark at the start for MM or |
2036 | | * II, however PNGv3 states that the the first 4 bytes should be checked. |
2037 | | * The caller ensures that there are four bytes available. |
2038 | | */ |
2039 | 1.08k | { |
2040 | 1.08k | png_uint_32 header = png_get_uint_32(buffer); |
2041 | | |
2042 | | /* These numbers are copied from the PNGv3 spec: */ |
2043 | 1.08k | if (header != 0x49492A00 && header != 0x4D4D002A) |
2044 | 512 | { |
2045 | 512 | png_chunk_benign_error(png_ptr, "invalid"); |
2046 | 512 | return handled_error; |
2047 | 512 | } |
2048 | 1.08k | } |
2049 | | |
2050 | 568 | png_set_eXIf_1(png_ptr, info_ptr, length, buffer); |
2051 | 568 | return handled_ok; |
2052 | 1.08k | } |
2053 | | #else |
2054 | | # define png_handle_eXIf NULL |
2055 | | #endif |
2056 | | |
2057 | | #ifdef PNG_READ_hIST_SUPPORTED |
2058 | | static png_handle_result_code /* PRIVATE */ |
2059 | | png_handle_hIST(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
2060 | 0 | { |
2061 | 0 | unsigned int num, i; |
2062 | 0 | png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH]; |
2063 | |
|
2064 | 0 | png_debug(1, "in png_handle_hIST"); |
2065 | | |
2066 | | /* This cast is safe because the chunk definition limits the length to a |
2067 | | * maximum of 1024 bytes. |
2068 | | * |
2069 | | * TODO: maybe use png_uint_32 anyway, not unsigned int, to reduce the |
2070 | | * casts. |
2071 | | */ |
2072 | 0 | num = (unsigned int)length / 2 ; |
2073 | |
|
2074 | 0 | if (length != num * 2 || |
2075 | 0 | num != (unsigned int)png_ptr->num_palette || |
2076 | 0 | num > (unsigned int)PNG_MAX_PALETTE_LENGTH) |
2077 | 0 | { |
2078 | 0 | png_crc_finish(png_ptr, length); |
2079 | 0 | png_chunk_benign_error(png_ptr, "invalid"); |
2080 | 0 | return handled_error; |
2081 | 0 | } |
2082 | | |
2083 | 0 | for (i = 0; i < num; i++) |
2084 | 0 | { |
2085 | 0 | png_byte buf[2]; |
2086 | |
|
2087 | 0 | png_crc_read(png_ptr, buf, 2); |
2088 | 0 | readbuf[i] = png_get_uint_16(buf); |
2089 | 0 | } |
2090 | |
|
2091 | 0 | if (png_crc_finish(png_ptr, 0) != 0) |
2092 | 0 | return handled_error; |
2093 | | |
2094 | 0 | png_set_hIST(png_ptr, info_ptr, readbuf); |
2095 | 0 | return handled_ok; |
2096 | 0 | } |
2097 | | #else |
2098 | | # define png_handle_hIST NULL |
2099 | | #endif |
2100 | | |
2101 | | #ifdef PNG_READ_pHYs_SUPPORTED |
2102 | | static png_handle_result_code /* PRIVATE */ |
2103 | | png_handle_pHYs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
2104 | 2.87k | { |
2105 | 2.87k | png_byte buf[9]; |
2106 | 2.87k | png_uint_32 res_x, res_y; |
2107 | 2.87k | int unit_type; |
2108 | | |
2109 | 2.87k | png_debug(1, "in png_handle_pHYs"); |
2110 | | |
2111 | 2.87k | png_crc_read(png_ptr, buf, 9); |
2112 | | |
2113 | 2.87k | if (png_crc_finish(png_ptr, 0) != 0) |
2114 | 2.65k | return handled_error; |
2115 | | |
2116 | 216 | res_x = png_get_uint_32(buf); |
2117 | 216 | res_y = png_get_uint_32(buf + 4); |
2118 | 216 | unit_type = buf[8]; |
2119 | 216 | png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type); |
2120 | 216 | return handled_ok; |
2121 | 0 | PNG_UNUSED(length) |
2122 | 0 | } |
2123 | | #else |
2124 | | # define png_handle_pHYs NULL |
2125 | | #endif |
2126 | | |
2127 | | #ifdef PNG_READ_oFFs_SUPPORTED |
2128 | | static png_handle_result_code /* PRIVATE */ |
2129 | | png_handle_oFFs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
2130 | 2.21k | { |
2131 | 2.21k | png_byte buf[9]; |
2132 | 2.21k | png_int_32 offset_x, offset_y; |
2133 | 2.21k | int unit_type; |
2134 | | |
2135 | 2.21k | png_debug(1, "in png_handle_oFFs"); |
2136 | | |
2137 | 2.21k | png_crc_read(png_ptr, buf, 9); |
2138 | | |
2139 | 2.21k | if (png_crc_finish(png_ptr, 0) != 0) |
2140 | 2.09k | return handled_error; |
2141 | | |
2142 | 125 | offset_x = png_get_int_32(buf); |
2143 | 125 | offset_y = png_get_int_32(buf + 4); |
2144 | 125 | unit_type = buf[8]; |
2145 | 125 | png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type); |
2146 | 125 | return handled_ok; |
2147 | 0 | PNG_UNUSED(length) |
2148 | 0 | } |
2149 | | #else |
2150 | | # define png_handle_oFFs NULL |
2151 | | #endif |
2152 | | |
2153 | | #ifdef PNG_READ_pCAL_SUPPORTED |
2154 | | /* Read the pCAL chunk (described in the PNG Extensions document) */ |
2155 | | static png_handle_result_code /* PRIVATE */ |
2156 | | png_handle_pCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
2157 | 0 | { |
2158 | 0 | png_int_32 X0, X1; |
2159 | 0 | png_byte type, nparams; |
2160 | 0 | png_bytep buffer, buf, units, endptr; |
2161 | 0 | png_charpp params; |
2162 | 0 | int i; |
2163 | |
|
2164 | 0 | png_debug(1, "in png_handle_pCAL"); |
2165 | 0 | png_debug1(2, "Allocating and reading pCAL chunk data (%u bytes)", |
2166 | 0 | length + 1); |
2167 | |
|
2168 | 0 | buffer = png_read_buffer(png_ptr, length+1); |
2169 | |
|
2170 | 0 | if (buffer == NULL) |
2171 | 0 | { |
2172 | 0 | png_crc_finish(png_ptr, length); |
2173 | 0 | png_chunk_benign_error(png_ptr, "out of memory"); |
2174 | 0 | return handled_error; |
2175 | 0 | } |
2176 | | |
2177 | 0 | png_crc_read(png_ptr, buffer, length); |
2178 | |
|
2179 | 0 | if (png_crc_finish(png_ptr, 0) != 0) |
2180 | 0 | return handled_error; |
2181 | | |
2182 | 0 | buffer[length] = 0; /* Null terminate the last string */ |
2183 | |
|
2184 | 0 | png_debug(3, "Finding end of pCAL purpose string"); |
2185 | 0 | for (buf = buffer; *buf; buf++) |
2186 | 0 | /* Empty loop */ ; |
2187 | |
|
2188 | 0 | endptr = buffer + length; |
2189 | | |
2190 | | /* We need to have at least 12 bytes after the purpose string |
2191 | | * in order to get the parameter information. |
2192 | | */ |
2193 | 0 | if (endptr - buf <= 12) |
2194 | 0 | { |
2195 | 0 | png_chunk_benign_error(png_ptr, "invalid"); |
2196 | 0 | return handled_error; |
2197 | 0 | } |
2198 | | |
2199 | 0 | png_debug(3, "Reading pCAL X0, X1, type, nparams, and units"); |
2200 | 0 | X0 = png_get_int_32((png_bytep)buf+1); |
2201 | 0 | X1 = png_get_int_32((png_bytep)buf+5); |
2202 | 0 | type = buf[9]; |
2203 | 0 | nparams = buf[10]; |
2204 | 0 | units = buf + 11; |
2205 | |
|
2206 | 0 | png_debug(3, "Checking pCAL equation type and number of parameters"); |
2207 | | /* Check that we have the right number of parameters for known |
2208 | | * equation types. |
2209 | | */ |
2210 | 0 | if ((type == PNG_EQUATION_LINEAR && nparams != 2) || |
2211 | 0 | (type == PNG_EQUATION_BASE_E && nparams != 3) || |
2212 | 0 | (type == PNG_EQUATION_ARBITRARY && nparams != 3) || |
2213 | 0 | (type == PNG_EQUATION_HYPERBOLIC && nparams != 4)) |
2214 | 0 | { |
2215 | 0 | png_chunk_benign_error(png_ptr, "invalid parameter count"); |
2216 | 0 | return handled_error; |
2217 | 0 | } |
2218 | | |
2219 | 0 | else if (type >= PNG_EQUATION_LAST) |
2220 | 0 | { |
2221 | 0 | png_chunk_benign_error(png_ptr, "unrecognized equation type"); |
2222 | 0 | } |
2223 | | |
2224 | 0 | for (buf = units; *buf; buf++) |
2225 | 0 | /* Empty loop to move past the units string. */ ; |
2226 | |
|
2227 | 0 | png_debug(3, "Allocating pCAL parameters array"); |
2228 | |
|
2229 | 0 | params = png_voidcast(png_charpp, png_malloc_warn(png_ptr, |
2230 | 0 | nparams * (sizeof (png_charp)))); |
2231 | |
|
2232 | 0 | if (params == NULL) |
2233 | 0 | { |
2234 | 0 | png_chunk_benign_error(png_ptr, "out of memory"); |
2235 | 0 | return handled_error; |
2236 | 0 | } |
2237 | | |
2238 | | /* Get pointers to the start of each parameter string. */ |
2239 | 0 | for (i = 0; i < nparams; i++) |
2240 | 0 | { |
2241 | 0 | buf++; /* Skip the null string terminator from previous parameter. */ |
2242 | |
|
2243 | 0 | png_debug1(3, "Reading pCAL parameter %d", i); |
2244 | |
|
2245 | 0 | for (params[i] = (png_charp)buf; buf <= endptr && *buf != 0; buf++) |
2246 | 0 | /* Empty loop to move past each parameter string */ ; |
2247 | | |
2248 | | /* Make sure we haven't run out of data yet */ |
2249 | 0 | if (buf > endptr) |
2250 | 0 | { |
2251 | 0 | png_free(png_ptr, params); |
2252 | 0 | png_chunk_benign_error(png_ptr, "invalid data"); |
2253 | 0 | return handled_error; |
2254 | 0 | } |
2255 | 0 | } |
2256 | | |
2257 | 0 | png_set_pCAL(png_ptr, info_ptr, (png_charp)buffer, X0, X1, type, nparams, |
2258 | 0 | (png_charp)units, params); |
2259 | | |
2260 | | /* TODO: BUG: png_set_pCAL calls png_chunk_report which, in this case, calls |
2261 | | * png_benign_error and that can error out. |
2262 | | * |
2263 | | * png_read_buffer needs to be allocated with space for both nparams and the |
2264 | | * parameter strings. Not hard to do. |
2265 | | */ |
2266 | 0 | png_free(png_ptr, params); |
2267 | 0 | return handled_ok; |
2268 | 0 | } |
2269 | | #else |
2270 | | # define png_handle_pCAL NULL |
2271 | | #endif |
2272 | | |
2273 | | #ifdef PNG_READ_sCAL_SUPPORTED |
2274 | | /* Read the sCAL chunk */ |
2275 | | static png_handle_result_code /* PRIVATE */ |
2276 | | png_handle_sCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
2277 | 0 | { |
2278 | 0 | png_bytep buffer; |
2279 | 0 | size_t i; |
2280 | 0 | int state; |
2281 | |
|
2282 | 0 | png_debug(1, "in png_handle_sCAL"); |
2283 | 0 | png_debug1(2, "Allocating and reading sCAL chunk data (%u bytes)", |
2284 | 0 | length + 1); |
2285 | |
|
2286 | 0 | buffer = png_read_buffer(png_ptr, length+1); |
2287 | |
|
2288 | 0 | if (buffer == NULL) |
2289 | 0 | { |
2290 | 0 | png_crc_finish(png_ptr, length); |
2291 | 0 | png_chunk_benign_error(png_ptr, "out of memory"); |
2292 | 0 | return handled_error; |
2293 | 0 | } |
2294 | | |
2295 | 0 | png_crc_read(png_ptr, buffer, length); |
2296 | 0 | buffer[length] = 0; /* Null terminate the last string */ |
2297 | |
|
2298 | 0 | if (png_crc_finish(png_ptr, 0) != 0) |
2299 | 0 | return handled_error; |
2300 | | |
2301 | | /* Validate the unit. */ |
2302 | 0 | if (buffer[0] != 1 && buffer[0] != 2) |
2303 | 0 | { |
2304 | 0 | png_chunk_benign_error(png_ptr, "invalid unit"); |
2305 | 0 | return handled_error; |
2306 | 0 | } |
2307 | | |
2308 | | /* Validate the ASCII numbers, need two ASCII numbers separated by |
2309 | | * a '\0' and they need to fit exactly in the chunk data. |
2310 | | */ |
2311 | 0 | i = 1; |
2312 | 0 | state = 0; |
2313 | |
|
2314 | 0 | if (png_check_fp_number((png_const_charp)buffer, length, &state, &i) == 0 || |
2315 | 0 | i >= length || buffer[i++] != 0) |
2316 | 0 | png_chunk_benign_error(png_ptr, "bad width format"); |
2317 | | |
2318 | 0 | else if (PNG_FP_IS_POSITIVE(state) == 0) |
2319 | 0 | png_chunk_benign_error(png_ptr, "non-positive width"); |
2320 | | |
2321 | 0 | else |
2322 | 0 | { |
2323 | 0 | size_t heighti = i; |
2324 | |
|
2325 | 0 | state = 0; |
2326 | 0 | if (png_check_fp_number((png_const_charp)buffer, length, |
2327 | 0 | &state, &i) == 0 || i != length) |
2328 | 0 | png_chunk_benign_error(png_ptr, "bad height format"); |
2329 | | |
2330 | 0 | else if (PNG_FP_IS_POSITIVE(state) == 0) |
2331 | 0 | png_chunk_benign_error(png_ptr, "non-positive height"); |
2332 | | |
2333 | 0 | else |
2334 | 0 | { |
2335 | | /* This is the (only) success case. */ |
2336 | 0 | png_set_sCAL_s(png_ptr, info_ptr, buffer[0], |
2337 | 0 | (png_charp)buffer+1, (png_charp)buffer+heighti); |
2338 | 0 | return handled_ok; |
2339 | 0 | } |
2340 | 0 | } |
2341 | | |
2342 | 0 | return handled_error; |
2343 | 0 | } |
2344 | | #else |
2345 | | # define png_handle_sCAL NULL |
2346 | | #endif |
2347 | | |
2348 | | #ifdef PNG_READ_tIME_SUPPORTED |
2349 | | static png_handle_result_code /* PRIVATE */ |
2350 | | png_handle_tIME(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
2351 | 2.66k | { |
2352 | 2.66k | png_byte buf[7]; |
2353 | 2.66k | png_time mod_time; |
2354 | | |
2355 | 2.66k | png_debug(1, "in png_handle_tIME"); |
2356 | | |
2357 | | /* TODO: what is this doing here? It should be happened in pngread.c and |
2358 | | * pngpread.c, although it could be moved to png_handle_chunk below and |
2359 | | * thereby avoid some code duplication. |
2360 | | */ |
2361 | 2.66k | if ((png_ptr->mode & PNG_HAVE_IDAT) != 0) |
2362 | 938 | png_ptr->mode |= PNG_AFTER_IDAT; |
2363 | | |
2364 | 2.66k | png_crc_read(png_ptr, buf, 7); |
2365 | | |
2366 | 2.66k | if (png_crc_finish(png_ptr, 0) != 0) |
2367 | 2.41k | return handled_error; |
2368 | | |
2369 | 252 | mod_time.second = buf[6]; |
2370 | 252 | mod_time.minute = buf[5]; |
2371 | 252 | mod_time.hour = buf[4]; |
2372 | 252 | mod_time.day = buf[3]; |
2373 | 252 | mod_time.month = buf[2]; |
2374 | 252 | mod_time.year = png_get_uint_16(buf); |
2375 | | |
2376 | 252 | png_set_tIME(png_ptr, info_ptr, &mod_time); |
2377 | 252 | return handled_ok; |
2378 | 0 | PNG_UNUSED(length) |
2379 | 0 | } |
2380 | | #else |
2381 | | # define png_handle_tIME NULL |
2382 | | #endif |
2383 | | |
2384 | | #ifdef PNG_READ_tEXt_SUPPORTED |
2385 | | /* Note: this does not properly handle chunks that are > 64K under DOS */ |
2386 | | static png_handle_result_code /* PRIVATE */ |
2387 | | png_handle_tEXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
2388 | 46.0k | { |
2389 | 46.0k | png_text text_info; |
2390 | 46.0k | png_bytep buffer; |
2391 | 46.0k | png_charp key; |
2392 | 46.0k | png_charp text; |
2393 | 46.0k | png_uint_32 skip = 0; |
2394 | | |
2395 | 46.0k | png_debug(1, "in png_handle_tEXt"); |
2396 | | |
2397 | 46.0k | #ifdef PNG_USER_LIMITS_SUPPORTED |
2398 | 46.0k | if (png_ptr->user_chunk_cache_max != 0) |
2399 | 46.0k | { |
2400 | 46.0k | if (png_ptr->user_chunk_cache_max == 1) |
2401 | 0 | { |
2402 | 0 | png_crc_finish(png_ptr, length); |
2403 | 0 | return handled_error; |
2404 | 0 | } |
2405 | | |
2406 | 46.0k | if (--png_ptr->user_chunk_cache_max == 1) |
2407 | 0 | { |
2408 | 0 | png_crc_finish(png_ptr, length); |
2409 | 0 | png_chunk_benign_error(png_ptr, "no space in chunk cache"); |
2410 | 0 | return handled_error; |
2411 | 0 | } |
2412 | 46.0k | } |
2413 | 46.0k | #endif |
2414 | | |
2415 | | /* TODO: this doesn't work and shouldn't be necessary. */ |
2416 | 46.0k | if ((png_ptr->mode & PNG_HAVE_IDAT) != 0) |
2417 | 28.9k | png_ptr->mode |= PNG_AFTER_IDAT; |
2418 | | |
2419 | 46.0k | buffer = png_read_buffer(png_ptr, length+1); |
2420 | | |
2421 | 46.0k | if (buffer == NULL) |
2422 | 87 | { |
2423 | 87 | png_crc_finish(png_ptr, length); |
2424 | 87 | png_chunk_benign_error(png_ptr, "out of memory"); |
2425 | 87 | return handled_error; |
2426 | 87 | } |
2427 | | |
2428 | 45.9k | png_crc_read(png_ptr, buffer, length); |
2429 | | |
2430 | 45.9k | if (png_crc_finish(png_ptr, skip) != 0) |
2431 | 3.12k | return handled_error; |
2432 | | |
2433 | 42.8k | key = (png_charp)buffer; |
2434 | 42.8k | key[length] = 0; |
2435 | | |
2436 | 146k | for (text = key; *text; text++) |
2437 | 103k | /* Empty loop to find end of key */ ; |
2438 | | |
2439 | 42.8k | if (text != key + length) |
2440 | 8.86k | text++; |
2441 | | |
2442 | 42.8k | text_info.compression = PNG_TEXT_COMPRESSION_NONE; |
2443 | 42.8k | text_info.key = key; |
2444 | 42.8k | text_info.lang = NULL; |
2445 | 42.8k | text_info.lang_key = NULL; |
2446 | 42.8k | text_info.itxt_length = 0; |
2447 | 42.8k | text_info.text = text; |
2448 | 42.8k | text_info.text_length = strlen(text); |
2449 | | |
2450 | 42.8k | if (png_set_text_2(png_ptr, info_ptr, &text_info, 1) == 0) |
2451 | 42.4k | return handled_ok; |
2452 | | |
2453 | 444 | png_chunk_benign_error(png_ptr, "out of memory"); |
2454 | 444 | return handled_error; |
2455 | 42.8k | } |
2456 | | #else |
2457 | | # define png_handle_tEXt NULL |
2458 | | #endif |
2459 | | |
2460 | | #ifdef PNG_READ_zTXt_SUPPORTED |
2461 | | /* Note: this does not correctly handle chunks that are > 64K under DOS */ |
2462 | | static png_handle_result_code /* PRIVATE */ |
2463 | | png_handle_zTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
2464 | 14.5k | { |
2465 | 14.5k | png_const_charp errmsg = NULL; |
2466 | 14.5k | png_bytep buffer; |
2467 | 14.5k | png_uint_32 keyword_length; |
2468 | | |
2469 | 14.5k | png_debug(1, "in png_handle_zTXt"); |
2470 | | |
2471 | 14.5k | #ifdef PNG_USER_LIMITS_SUPPORTED |
2472 | 14.5k | if (png_ptr->user_chunk_cache_max != 0) |
2473 | 14.5k | { |
2474 | 14.5k | if (png_ptr->user_chunk_cache_max == 1) |
2475 | 0 | { |
2476 | 0 | png_crc_finish(png_ptr, length); |
2477 | 0 | return handled_error; |
2478 | 0 | } |
2479 | | |
2480 | 14.5k | if (--png_ptr->user_chunk_cache_max == 1) |
2481 | 0 | { |
2482 | 0 | png_crc_finish(png_ptr, length); |
2483 | 0 | png_chunk_benign_error(png_ptr, "no space in chunk cache"); |
2484 | 0 | return handled_error; |
2485 | 0 | } |
2486 | 14.5k | } |
2487 | 14.5k | #endif |
2488 | | |
2489 | | /* TODO: should not be necessary. */ |
2490 | 14.5k | if ((png_ptr->mode & PNG_HAVE_IDAT) != 0) |
2491 | 1.91k | png_ptr->mode |= PNG_AFTER_IDAT; |
2492 | | |
2493 | | /* Note, "length" is sufficient here; we won't be adding |
2494 | | * a null terminator later. The limit check in png_handle_chunk should be |
2495 | | * sufficient. |
2496 | | */ |
2497 | 14.5k | buffer = png_read_buffer(png_ptr, length); |
2498 | | |
2499 | 14.5k | if (buffer == NULL) |
2500 | 0 | { |
2501 | 0 | png_crc_finish(png_ptr, length); |
2502 | 0 | png_chunk_benign_error(png_ptr, "out of memory"); |
2503 | 0 | return handled_error; |
2504 | 0 | } |
2505 | | |
2506 | 14.5k | png_crc_read(png_ptr, buffer, length); |
2507 | | |
2508 | 14.5k | if (png_crc_finish(png_ptr, 0) != 0) |
2509 | 4.51k | return handled_error; |
2510 | | |
2511 | | /* TODO: also check that the keyword contents match the spec! */ |
2512 | 10.0k | for (keyword_length = 0; |
2513 | 88.8k | keyword_length < length && buffer[keyword_length] != 0; |
2514 | 78.8k | ++keyword_length) |
2515 | 78.8k | /* Empty loop to find end of name */ ; |
2516 | | |
2517 | 10.0k | if (keyword_length > 79 || keyword_length < 1) |
2518 | 1.40k | errmsg = "bad keyword"; |
2519 | | |
2520 | | /* zTXt must have some LZ data after the keyword, although it may expand to |
2521 | | * zero bytes; we need a '\0' at the end of the keyword, the compression type |
2522 | | * then the LZ data: |
2523 | | */ |
2524 | 8.60k | else if (keyword_length + 3 > length) |
2525 | 63 | errmsg = "truncated"; |
2526 | | |
2527 | 8.54k | else if (buffer[keyword_length+1] != PNG_COMPRESSION_TYPE_BASE) |
2528 | 305 | errmsg = "unknown compression type"; |
2529 | | |
2530 | 8.24k | else |
2531 | 8.24k | { |
2532 | 8.24k | png_alloc_size_t uncompressed_length = PNG_SIZE_MAX; |
2533 | | |
2534 | | /* TODO: at present png_decompress_chunk imposes a single application |
2535 | | * level memory limit, this should be split to different values for iCCP |
2536 | | * and text chunks. |
2537 | | */ |
2538 | 8.24k | if (png_decompress_chunk(png_ptr, length, keyword_length+2, |
2539 | 8.24k | &uncompressed_length, 1/*terminate*/) == Z_STREAM_END) |
2540 | 5.57k | { |
2541 | 5.57k | png_text text; |
2542 | | |
2543 | 5.57k | if (png_ptr->read_buffer == NULL) |
2544 | 0 | errmsg="Read failure in png_handle_zTXt"; |
2545 | 5.57k | else |
2546 | 5.57k | { |
2547 | | /* It worked; png_ptr->read_buffer now looks like a tEXt chunk |
2548 | | * except for the extra compression type byte and the fact that |
2549 | | * it isn't necessarily '\0' terminated. |
2550 | | */ |
2551 | 5.57k | buffer = png_ptr->read_buffer; |
2552 | 5.57k | buffer[uncompressed_length+(keyword_length+2)] = 0; |
2553 | | |
2554 | 5.57k | text.compression = PNG_TEXT_COMPRESSION_zTXt; |
2555 | 5.57k | text.key = (png_charp)buffer; |
2556 | 5.57k | text.text = (png_charp)(buffer + keyword_length+2); |
2557 | 5.57k | text.text_length = uncompressed_length; |
2558 | 5.57k | text.itxt_length = 0; |
2559 | 5.57k | text.lang = NULL; |
2560 | 5.57k | text.lang_key = NULL; |
2561 | | |
2562 | 5.57k | if (png_set_text_2(png_ptr, info_ptr, &text, 1) == 0) |
2563 | 5.57k | return handled_ok; |
2564 | | |
2565 | 0 | errmsg = "out of memory"; |
2566 | 0 | } |
2567 | 5.57k | } |
2568 | | |
2569 | 2.67k | else |
2570 | 2.67k | errmsg = png_ptr->zstream.msg; |
2571 | 8.24k | } |
2572 | | |
2573 | 4.44k | png_chunk_benign_error(png_ptr, errmsg); |
2574 | 4.44k | return handled_error; |
2575 | 10.0k | } |
2576 | | #else |
2577 | | # define png_handle_zTXt NULL |
2578 | | #endif |
2579 | | |
2580 | | #ifdef PNG_READ_iTXt_SUPPORTED |
2581 | | /* Note: this does not correctly handle chunks that are > 64K under DOS */ |
2582 | | static png_handle_result_code /* PRIVATE */ |
2583 | | png_handle_iTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
2584 | 0 | { |
2585 | 0 | png_const_charp errmsg = NULL; |
2586 | 0 | png_bytep buffer; |
2587 | 0 | png_uint_32 prefix_length; |
2588 | |
|
2589 | 0 | png_debug(1, "in png_handle_iTXt"); |
2590 | |
|
2591 | 0 | #ifdef PNG_USER_LIMITS_SUPPORTED |
2592 | 0 | if (png_ptr->user_chunk_cache_max != 0) |
2593 | 0 | { |
2594 | 0 | if (png_ptr->user_chunk_cache_max == 1) |
2595 | 0 | { |
2596 | 0 | png_crc_finish(png_ptr, length); |
2597 | 0 | return handled_error; |
2598 | 0 | } |
2599 | | |
2600 | 0 | if (--png_ptr->user_chunk_cache_max == 1) |
2601 | 0 | { |
2602 | 0 | png_crc_finish(png_ptr, length); |
2603 | 0 | png_chunk_benign_error(png_ptr, "no space in chunk cache"); |
2604 | 0 | return handled_error; |
2605 | 0 | } |
2606 | 0 | } |
2607 | 0 | #endif |
2608 | | |
2609 | | /* TODO: should not be necessary. */ |
2610 | 0 | if ((png_ptr->mode & PNG_HAVE_IDAT) != 0) |
2611 | 0 | png_ptr->mode |= PNG_AFTER_IDAT; |
2612 | |
|
2613 | 0 | buffer = png_read_buffer(png_ptr, length+1); |
2614 | |
|
2615 | 0 | if (buffer == NULL) |
2616 | 0 | { |
2617 | 0 | png_crc_finish(png_ptr, length); |
2618 | 0 | png_chunk_benign_error(png_ptr, "out of memory"); |
2619 | 0 | return handled_error; |
2620 | 0 | } |
2621 | | |
2622 | 0 | png_crc_read(png_ptr, buffer, length); |
2623 | |
|
2624 | 0 | if (png_crc_finish(png_ptr, 0) != 0) |
2625 | 0 | return handled_error; |
2626 | | |
2627 | | /* First the keyword. */ |
2628 | 0 | for (prefix_length=0; |
2629 | 0 | prefix_length < length && buffer[prefix_length] != 0; |
2630 | 0 | ++prefix_length) |
2631 | 0 | /* Empty loop */ ; |
2632 | | |
2633 | | /* Perform a basic check on the keyword length here. */ |
2634 | 0 | if (prefix_length > 79 || prefix_length < 1) |
2635 | 0 | errmsg = "bad keyword"; |
2636 | | |
2637 | | /* Expect keyword, compression flag, compression type, language, translated |
2638 | | * keyword (both may be empty but are 0 terminated) then the text, which may |
2639 | | * be empty. |
2640 | | */ |
2641 | 0 | else if (prefix_length + 5 > length) |
2642 | 0 | errmsg = "truncated"; |
2643 | | |
2644 | 0 | else if (buffer[prefix_length+1] == 0 || |
2645 | 0 | (buffer[prefix_length+1] == 1 && |
2646 | 0 | buffer[prefix_length+2] == PNG_COMPRESSION_TYPE_BASE)) |
2647 | 0 | { |
2648 | 0 | int compressed = buffer[prefix_length+1] != 0; |
2649 | 0 | png_uint_32 language_offset, translated_keyword_offset; |
2650 | 0 | png_alloc_size_t uncompressed_length = 0; |
2651 | | |
2652 | | /* Now the language tag */ |
2653 | 0 | prefix_length += 3; |
2654 | 0 | language_offset = prefix_length; |
2655 | |
|
2656 | 0 | for (; prefix_length < length && buffer[prefix_length] != 0; |
2657 | 0 | ++prefix_length) |
2658 | 0 | /* Empty loop */ ; |
2659 | | |
2660 | | /* WARNING: the length may be invalid here, this is checked below. */ |
2661 | 0 | translated_keyword_offset = ++prefix_length; |
2662 | |
|
2663 | 0 | for (; prefix_length < length && buffer[prefix_length] != 0; |
2664 | 0 | ++prefix_length) |
2665 | 0 | /* Empty loop */ ; |
2666 | | |
2667 | | /* prefix_length should now be at the trailing '\0' of the translated |
2668 | | * keyword, but it may already be over the end. None of this arithmetic |
2669 | | * can overflow because chunks are at most 2^31 bytes long, but on 16-bit |
2670 | | * systems the available allocation may overflow. |
2671 | | */ |
2672 | 0 | ++prefix_length; |
2673 | |
|
2674 | 0 | if (compressed == 0 && prefix_length <= length) |
2675 | 0 | uncompressed_length = length - prefix_length; |
2676 | | |
2677 | 0 | else if (compressed != 0 && prefix_length < length) |
2678 | 0 | { |
2679 | 0 | uncompressed_length = PNG_SIZE_MAX; |
2680 | | |
2681 | | /* TODO: at present png_decompress_chunk imposes a single application |
2682 | | * level memory limit, this should be split to different values for |
2683 | | * iCCP and text chunks. |
2684 | | */ |
2685 | 0 | if (png_decompress_chunk(png_ptr, length, prefix_length, |
2686 | 0 | &uncompressed_length, 1/*terminate*/) == Z_STREAM_END) |
2687 | 0 | buffer = png_ptr->read_buffer; |
2688 | | |
2689 | 0 | else |
2690 | 0 | errmsg = png_ptr->zstream.msg; |
2691 | 0 | } |
2692 | | |
2693 | 0 | else |
2694 | 0 | errmsg = "truncated"; |
2695 | |
|
2696 | 0 | if (errmsg == NULL) |
2697 | 0 | { |
2698 | 0 | png_text text; |
2699 | |
|
2700 | 0 | buffer[uncompressed_length+prefix_length] = 0; |
2701 | |
|
2702 | 0 | if (compressed == 0) |
2703 | 0 | text.compression = PNG_ITXT_COMPRESSION_NONE; |
2704 | | |
2705 | 0 | else |
2706 | 0 | text.compression = PNG_ITXT_COMPRESSION_zTXt; |
2707 | |
|
2708 | 0 | text.key = (png_charp)buffer; |
2709 | 0 | text.lang = (png_charp)buffer + language_offset; |
2710 | 0 | text.lang_key = (png_charp)buffer + translated_keyword_offset; |
2711 | 0 | text.text = (png_charp)buffer + prefix_length; |
2712 | 0 | text.text_length = 0; |
2713 | 0 | text.itxt_length = uncompressed_length; |
2714 | |
|
2715 | 0 | if (png_set_text_2(png_ptr, info_ptr, &text, 1) == 0) |
2716 | 0 | return handled_ok; |
2717 | | |
2718 | 0 | errmsg = "out of memory"; |
2719 | 0 | } |
2720 | 0 | } |
2721 | | |
2722 | 0 | else |
2723 | 0 | errmsg = "bad compression info"; |
2724 | | |
2725 | 0 | if (errmsg != NULL) |
2726 | 0 | png_chunk_benign_error(png_ptr, errmsg); |
2727 | 0 | return handled_error; |
2728 | 0 | } |
2729 | | #else |
2730 | | # define png_handle_iTXt NULL |
2731 | | #endif |
2732 | | |
2733 | | #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED |
2734 | | /* Utility function for png_handle_unknown; set up png_ptr::unknown_chunk */ |
2735 | | static int |
2736 | | png_cache_unknown_chunk(png_structrp png_ptr, png_uint_32 length) |
2737 | 315k | { |
2738 | 315k | const png_alloc_size_t limit = png_chunk_max(png_ptr); |
2739 | | |
2740 | 315k | if (png_ptr->unknown_chunk.data != NULL) |
2741 | 0 | { |
2742 | 0 | png_free(png_ptr, png_ptr->unknown_chunk.data); |
2743 | 0 | png_ptr->unknown_chunk.data = NULL; |
2744 | 0 | } |
2745 | | |
2746 | 315k | if (length <= limit) |
2747 | 313k | { |
2748 | 313k | PNG_CSTRING_FROM_CHUNK(png_ptr->unknown_chunk.name, png_ptr->chunk_name); |
2749 | | /* The following is safe because of the PNG_SIZE_MAX init above */ |
2750 | 313k | png_ptr->unknown_chunk.size = (size_t)length/*SAFE*/; |
2751 | | /* 'mode' is a flag array, only the bottom four bits matter here */ |
2752 | 313k | png_ptr->unknown_chunk.location = (png_byte)png_ptr->mode/*SAFE*/; |
2753 | | |
2754 | 313k | if (length == 0) |
2755 | 54.7k | png_ptr->unknown_chunk.data = NULL; |
2756 | | |
2757 | 258k | else |
2758 | 258k | { |
2759 | | /* Do a 'warn' here - it is handled below. */ |
2760 | 258k | png_ptr->unknown_chunk.data = png_voidcast(png_bytep, |
2761 | 258k | png_malloc_warn(png_ptr, length)); |
2762 | 258k | } |
2763 | 313k | } |
2764 | | |
2765 | 315k | if (png_ptr->unknown_chunk.data == NULL && length > 0) |
2766 | 1.99k | { |
2767 | | /* This is benign because we clean up correctly */ |
2768 | 1.99k | png_crc_finish(png_ptr, length); |
2769 | 1.99k | png_chunk_benign_error(png_ptr, "unknown chunk exceeds memory limits"); |
2770 | 1.99k | return 0; |
2771 | 1.99k | } |
2772 | | |
2773 | 313k | else |
2774 | 313k | { |
2775 | 313k | if (length > 0) |
2776 | 258k | png_crc_read(png_ptr, png_ptr->unknown_chunk.data, length); |
2777 | 313k | png_crc_finish(png_ptr, 0); |
2778 | 313k | return 1; |
2779 | 313k | } |
2780 | 315k | } |
2781 | | #endif /* READ_UNKNOWN_CHUNKS */ |
2782 | | |
2783 | | /* Handle an unknown, or known but disabled, chunk */ |
2784 | | png_handle_result_code /*PRIVATE*/ |
2785 | | png_handle_unknown(png_structrp png_ptr, png_inforp info_ptr, |
2786 | | png_uint_32 length, int keep) |
2787 | 315k | { |
2788 | 315k | png_handle_result_code handled = handled_discarded; /* the default */ |
2789 | | |
2790 | 315k | png_debug(1, "in png_handle_unknown"); |
2791 | | |
2792 | 315k | #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED |
2793 | | /* NOTE: this code is based on the code in libpng-1.4.12 except for fixing |
2794 | | * the bug which meant that setting a non-default behavior for a specific |
2795 | | * chunk would be ignored (the default was always used unless a user |
2796 | | * callback was installed). |
2797 | | * |
2798 | | * 'keep' is the value from the png_chunk_unknown_handling, the setting for |
2799 | | * this specific chunk_name, if PNG_HANDLE_AS_UNKNOWN_SUPPORTED, if not it |
2800 | | * will always be PNG_HANDLE_CHUNK_AS_DEFAULT and it needs to be set here. |
2801 | | * This is just an optimization to avoid multiple calls to the lookup |
2802 | | * function. |
2803 | | */ |
2804 | | # ifndef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
2805 | | # ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED |
2806 | | keep = png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name); |
2807 | | # endif |
2808 | | # endif |
2809 | | |
2810 | | /* One of the following methods will read the chunk or skip it (at least one |
2811 | | * of these is always defined because this is the only way to switch on |
2812 | | * PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) |
2813 | | */ |
2814 | 315k | # ifdef PNG_READ_USER_CHUNKS_SUPPORTED |
2815 | | /* The user callback takes precedence over the chunk keep value, but the |
2816 | | * keep value is still required to validate a save of a critical chunk. |
2817 | | */ |
2818 | 315k | if (png_ptr->read_user_chunk_fn != NULL) |
2819 | 315k | { |
2820 | 315k | if (png_cache_unknown_chunk(png_ptr, length) != 0) |
2821 | 309k | { |
2822 | | /* Callback to user unknown chunk handler */ |
2823 | 309k | int ret = (*(png_ptr->read_user_chunk_fn))(png_ptr, |
2824 | 309k | &png_ptr->unknown_chunk); |
2825 | | |
2826 | | /* ret is: |
2827 | | * negative: An error occurred; png_chunk_error will be called. |
2828 | | * zero: The chunk was not handled, the chunk will be discarded |
2829 | | * unless png_set_keep_unknown_chunks has been used to set |
2830 | | * a 'keep' behavior for this particular chunk, in which |
2831 | | * case that will be used. A critical chunk will cause an |
2832 | | * error at this point unless it is to be saved. |
2833 | | * positive: The chunk was handled, libpng will ignore/discard it. |
2834 | | */ |
2835 | 309k | if (ret < 0) /* handled_error */ |
2836 | 206 | png_chunk_error(png_ptr, "error in user chunk"); |
2837 | | |
2838 | 309k | else if (ret == 0) |
2839 | 84.5k | { |
2840 | | /* If the keep value is 'default' or 'never' override it, but |
2841 | | * still error out on critical chunks unless the keep value is |
2842 | | * 'always' While this is weird it is the behavior in 1.4.12. |
2843 | | * A possible improvement would be to obey the value set for the |
2844 | | * chunk, but this would be an API change that would probably |
2845 | | * damage some applications. |
2846 | | * |
2847 | | * The png_app_warning below catches the case that matters, where |
2848 | | * the application has not set specific save or ignore for this |
2849 | | * chunk or global save or ignore. |
2850 | | */ |
2851 | 84.5k | if (keep < PNG_HANDLE_CHUNK_IF_SAFE) |
2852 | 83.1k | { |
2853 | 83.1k | # ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED |
2854 | 83.1k | if (png_ptr->unknown_default < PNG_HANDLE_CHUNK_IF_SAFE) |
2855 | 0 | { |
2856 | 0 | png_chunk_warning(png_ptr, "Saving unknown chunk:"); |
2857 | 0 | png_app_warning(png_ptr, |
2858 | 0 | "forcing save of an unhandled chunk;" |
2859 | 0 | " please call png_set_keep_unknown_chunks"); |
2860 | | /* with keep = PNG_HANDLE_CHUNK_IF_SAFE */ |
2861 | 0 | } |
2862 | 83.1k | # endif |
2863 | 83.1k | keep = PNG_HANDLE_CHUNK_IF_SAFE; |
2864 | 83.1k | } |
2865 | 84.5k | } |
2866 | | |
2867 | 224k | else /* chunk was handled */ |
2868 | 224k | { |
2869 | 224k | handled = handled_ok; |
2870 | | /* Critical chunks can be safely discarded at this point. */ |
2871 | 224k | keep = PNG_HANDLE_CHUNK_NEVER; |
2872 | 224k | } |
2873 | 309k | } |
2874 | | |
2875 | 6.14k | else |
2876 | 6.14k | keep = PNG_HANDLE_CHUNK_NEVER; /* insufficient memory */ |
2877 | 315k | } |
2878 | | |
2879 | 0 | else |
2880 | | /* Use the SAVE_UNKNOWN_CHUNKS code or skip the chunk */ |
2881 | 0 | # endif /* READ_USER_CHUNKS */ |
2882 | | |
2883 | 0 | # ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED |
2884 | 0 | { |
2885 | | /* keep is currently just the per-chunk setting, if there was no |
2886 | | * setting change it to the global default now (not that this may |
2887 | | * still be AS_DEFAULT) then obtain the cache of the chunk if required, |
2888 | | * if not simply skip the chunk. |
2889 | | */ |
2890 | 0 | if (keep == PNG_HANDLE_CHUNK_AS_DEFAULT) |
2891 | 0 | keep = png_ptr->unknown_default; |
2892 | |
|
2893 | 0 | if (keep == PNG_HANDLE_CHUNK_ALWAYS || |
2894 | 0 | (keep == PNG_HANDLE_CHUNK_IF_SAFE && |
2895 | 0 | PNG_CHUNK_ANCILLARY(png_ptr->chunk_name))) |
2896 | 0 | { |
2897 | 0 | if (png_cache_unknown_chunk(png_ptr, length) == 0) |
2898 | 0 | keep = PNG_HANDLE_CHUNK_NEVER; |
2899 | 0 | } |
2900 | | |
2901 | 0 | else |
2902 | 0 | png_crc_finish(png_ptr, length); |
2903 | 0 | } |
2904 | | # else |
2905 | | # ifndef PNG_READ_USER_CHUNKS_SUPPORTED |
2906 | | # error no method to support READ_UNKNOWN_CHUNKS |
2907 | | # endif |
2908 | | |
2909 | | { |
2910 | | /* If here there is no read callback pointer set and no support is |
2911 | | * compiled in to just save the unknown chunks, so simply skip this |
2912 | | * chunk. If 'keep' is something other than AS_DEFAULT or NEVER then |
2913 | | * the app has erroneously asked for unknown chunk saving when there |
2914 | | * is no support. |
2915 | | */ |
2916 | | if (keep > PNG_HANDLE_CHUNK_NEVER) |
2917 | | png_app_error(png_ptr, "no unknown chunk support available"); |
2918 | | |
2919 | | png_crc_finish(png_ptr, length); |
2920 | | } |
2921 | | # endif |
2922 | | |
2923 | 315k | # ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED |
2924 | | /* Now store the chunk in the chunk list if appropriate, and if the limits |
2925 | | * permit it. |
2926 | | */ |
2927 | 315k | if (keep == PNG_HANDLE_CHUNK_ALWAYS || |
2928 | 315k | (keep == PNG_HANDLE_CHUNK_IF_SAFE && |
2929 | 309k | PNG_CHUNK_ANCILLARY(png_ptr->chunk_name))) |
2930 | 84.5k | { |
2931 | 84.5k | # ifdef PNG_USER_LIMITS_SUPPORTED |
2932 | 84.5k | switch (png_ptr->user_chunk_cache_max) |
2933 | 84.5k | { |
2934 | 0 | case 2: |
2935 | 0 | png_ptr->user_chunk_cache_max = 1; |
2936 | 0 | png_chunk_benign_error(png_ptr, "no space in chunk cache"); |
2937 | | /* FALLTHROUGH */ |
2938 | 0 | case 1: |
2939 | | /* NOTE: prior to 1.6.0 this case resulted in an unknown critical |
2940 | | * chunk being skipped, now there will be a hard error below. |
2941 | | */ |
2942 | 0 | break; |
2943 | | |
2944 | 84.5k | default: /* not at limit */ |
2945 | 84.5k | --(png_ptr->user_chunk_cache_max); |
2946 | | /* FALLTHROUGH */ |
2947 | 84.5k | case 0: /* no limit */ |
2948 | 84.5k | # endif /* USER_LIMITS */ |
2949 | | /* Here when the limit isn't reached or when limits are compiled |
2950 | | * out; store the chunk. |
2951 | | */ |
2952 | 84.5k | png_set_unknown_chunks(png_ptr, info_ptr, |
2953 | 84.5k | &png_ptr->unknown_chunk, 1); |
2954 | 84.5k | handled = handled_saved; |
2955 | 84.5k | # ifdef PNG_USER_LIMITS_SUPPORTED |
2956 | 84.5k | break; |
2957 | 84.5k | } |
2958 | 84.5k | # endif |
2959 | 84.5k | } |
2960 | | # else /* no store support: the chunk must be handled by the user callback */ |
2961 | | PNG_UNUSED(info_ptr) |
2962 | | # endif |
2963 | | |
2964 | | /* Regardless of the error handling below the cached data (if any) can be |
2965 | | * freed now. Notice that the data is not freed if there is a png_error, but |
2966 | | * it will be freed by destroy_read_struct. |
2967 | | */ |
2968 | 312k | if (png_ptr->unknown_chunk.data != NULL) |
2969 | 252k | png_free(png_ptr, png_ptr->unknown_chunk.data); |
2970 | 312k | png_ptr->unknown_chunk.data = NULL; |
2971 | | |
2972 | | #else /* !PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */ |
2973 | | /* There is no support to read an unknown chunk, so just skip it. */ |
2974 | | png_crc_finish(png_ptr, length); |
2975 | | PNG_UNUSED(info_ptr) |
2976 | | PNG_UNUSED(keep) |
2977 | | #endif /* !READ_UNKNOWN_CHUNKS */ |
2978 | | |
2979 | | /* Check for unhandled critical chunks */ |
2980 | 312k | if (handled < handled_saved && PNG_CHUNK_CRITICAL(png_ptr->chunk_name)) |
2981 | 37 | png_chunk_error(png_ptr, "unhandled critical chunk"); |
2982 | | |
2983 | 312k | return handled; |
2984 | 312k | } |
2985 | | |
2986 | | /* APNG handling: the minimal implementation of APNG handling in libpng 1.6 |
2987 | | * requires that those significant applications which already handle APNG not |
2988 | | * get hosed. To do this ensure the code here will have to ensure than APNG |
2989 | | * data by default (at least in 1.6) gets stored in the unknown chunk list. |
2990 | | * Maybe this can be relaxed in a few years but at present it's just the only |
2991 | | * safe way. |
2992 | | * |
2993 | | * ATM just cause unknown handling for all three chunks: |
2994 | | */ |
2995 | | #define png_handle_acTL NULL |
2996 | | #define png_handle_fcTL NULL |
2997 | | #define png_handle_fdAT NULL |
2998 | | |
2999 | | /* |
3000 | | * 1.6.47: This is the new table driven interface to all the chunk handling. |
3001 | | * |
3002 | | * The table describes the PNG standard rules for **reading** known chunks - |
3003 | | * every chunk which has an entry in PNG_KNOWN_CHUNKS. The table contains an |
3004 | | * entry for each PNG_INDEX_cHNK describing the rules. |
3005 | | * |
3006 | | * In this initial version the only information in the entry is the |
3007 | | * png_handle_cHNK function for the chunk in question. When chunk support is |
3008 | | * compiled out the entry will be NULL. |
3009 | | */ |
3010 | | static const struct |
3011 | | { |
3012 | | png_handle_result_code (*handler)( |
3013 | | png_structrp, png_inforp, png_uint_32 length); |
3014 | | /* A chunk-specific 'handler', NULL if the chunk is not supported in this |
3015 | | * build. |
3016 | | */ |
3017 | | |
3018 | | /* Crushing these values helps on modern 32-bit architectures because the |
3019 | | * pointer and the following bit fields both end up requiring 32 bits. |
3020 | | * Typically this will halve the table size. On 64-bit architectures the |
3021 | | * table entries will typically be 8 bytes. |
3022 | | */ |
3023 | | png_uint_32 max_length :12; /* Length min, max in bytes */ |
3024 | | png_uint_32 min_length :8; |
3025 | | /* Length errors on critical chunks have special handling to preserve the |
3026 | | * existing behaviour in libpng 1.6. Anciallary chunks are checked below |
3027 | | * and produce a 'benign' error. |
3028 | | */ |
3029 | | png_uint_32 pos_before :4; /* PNG_HAVE_ values chunk must precede */ |
3030 | | png_uint_32 pos_after :4; /* PNG_HAVE_ values chunk must follow */ |
3031 | | /* NOTE: PLTE, tRNS and bKGD require special handling which depends on |
3032 | | * the colour type of the base image. |
3033 | | */ |
3034 | | png_uint_32 multiple :1; /* Multiple occurences permitted */ |
3035 | | /* This is enabled for PLTE because PLTE may, in practice, be optional */ |
3036 | | } |
3037 | | read_chunks[PNG_INDEX_unknown] = |
3038 | | { |
3039 | | /* Definitions as above but done indirectly by #define so that |
3040 | | * PNG_KNOWN_CHUNKS can be used safely to build the table in order. |
3041 | | * |
3042 | | * Each CDcHNK definition lists the values for the parameters **after** |
3043 | | * the first, 'handler', function. 'handler' is NULL when the chunk has no |
3044 | | * compiled in support. |
3045 | | */ |
3046 | 153k | # define NoCheck 0x801U /* Do not check the maximum length */ |
3047 | 19.7k | # define Limit 0x802U /* Limit to png_chunk_max bytes */ |
3048 | | # define LKMin 3U+LZ77Min /* Minimum length of keyword+LZ77 */ |
3049 | | |
3050 | | #define hIHDR PNG_HAVE_IHDR |
3051 | | #define hPLTE PNG_HAVE_PLTE |
3052 | | #define hIDAT PNG_HAVE_IDAT |
3053 | | /* For the two chunks, tRNS and bKGD which can occur in PNGs without a PLTE |
3054 | | * but must occur after the PLTE use this and put the check in the handler |
3055 | | * routine for colour mapped images were PLTE is required. Also put a check |
3056 | | * in PLTE for other image types to drop the PLTE if tRNS or bKGD have been |
3057 | | * seen. |
3058 | | */ |
3059 | | #define hCOL (PNG_HAVE_PLTE|PNG_HAVE_IDAT) |
3060 | | /* Used for the decoding chunks which must be before PLTE. */ |
3061 | | #define aIDAT PNG_AFTER_IDAT |
3062 | | |
3063 | | /* Chunks from W3C PNG v3: */ |
3064 | | /* cHNK max_len, min, before, after, multiple */ |
3065 | | # define CDIHDR 13U, 13U, hIHDR, 0, 0 |
3066 | | # define CDPLTE NoCheck, 0U, 0, hIHDR, 1 |
3067 | | /* PLTE errors are only critical for colour-map images, consequently the |
3068 | | * hander does all the checks. |
3069 | | */ |
3070 | | # define CDIDAT NoCheck, 0U, aIDAT, hIHDR, 1 |
3071 | | # define CDIEND NoCheck, 0U, 0, aIDAT, 0 |
3072 | | /* Historically data was allowed in IEND */ |
3073 | | # define CDtRNS 256U, 0U, hIDAT, hIHDR, 0 |
3074 | | # define CDcHRM 32U, 32U, hCOL, hIHDR, 0 |
3075 | | # define CDgAMA 4U, 4U, hCOL, hIHDR, 0 |
3076 | | # define CDiCCP NoCheck, LKMin, hCOL, hIHDR, 0 |
3077 | | # define CDsBIT 4U, 1U, hCOL, hIHDR, 0 |
3078 | | # define CDsRGB 1U, 1U, hCOL, hIHDR, 0 |
3079 | | # define CDcICP 4U, 4U, hCOL, hIHDR, 0 |
3080 | | # define CDmDCV 24U, 24U, hCOL, hIHDR, 0 |
3081 | | # define CDeXIf Limit, 4U, 0, hIHDR, 0 |
3082 | | # define CDcLLI 8U, 8U, hCOL, hIHDR, 0 |
3083 | | # define CDtEXt NoCheck, 2U, 0, hIHDR, 1 |
3084 | | /* Allocates 'length+1'; checked in the handler */ |
3085 | | # define CDzTXt Limit, LKMin, 0, hIHDR, 1 |
3086 | | # define CDiTXt NoCheck, 6U, 0, hIHDR, 1 |
3087 | | /* Allocates 'length+1'; checked in the handler */ |
3088 | | # define CDbKGD 6U, 1U, hIDAT, hIHDR, 0 |
3089 | | # define CDhIST 1024U, 0U, hPLTE, hIHDR, 0 |
3090 | | # define CDpHYs 9U, 9U, hIDAT, hIHDR, 0 |
3091 | | # define CDsPLT NoCheck, 3U, hIDAT, hIHDR, 1 |
3092 | | /* Allocates 'length+1'; checked in the handler */ |
3093 | | # define CDtIME 7U, 7U, 0, hIHDR, 0 |
3094 | | # define CDacTL 8U, 8U, hIDAT, hIHDR, 0 |
3095 | | # define CDfcTL 25U, 26U, 0, hIHDR, 1 |
3096 | | # define CDfdAT Limit, 4U, hIDAT, hIHDR, 1 |
3097 | | /* Supported chunks from PNG extensions 1.5.0, NYI so limit */ |
3098 | | # define CDoFFs 9U, 9U, hIDAT, hIHDR, 0 |
3099 | | # define CDpCAL NoCheck, 14U, hIDAT, hIHDR, 0 |
3100 | | /* Allocates 'length+1'; checked in the handler */ |
3101 | | # define CDsCAL Limit, 4U, hIDAT, hIHDR, 0 |
3102 | | /* Allocates 'length+1'; checked in the handler */ |
3103 | | |
3104 | | # define PNG_CHUNK(cHNK, index) { png_handle_ ## cHNK, CD ## cHNK }, |
3105 | | PNG_KNOWN_CHUNKS |
3106 | | # undef PNG_CHUNK |
3107 | | }; |
3108 | | |
3109 | | |
3110 | | static png_index |
3111 | | png_chunk_index_from_name(png_uint_32 chunk_name) |
3112 | 494k | { |
3113 | | /* For chunk png_cHNK return PNG_INDEX_cHNK. Return PNG_INDEX_unknown if |
3114 | | * chunk_name is not known. Notice that in a particular build "known" does |
3115 | | * not necessarily mean "supported", although the inverse applies. |
3116 | | */ |
3117 | 494k | switch (chunk_name) |
3118 | 494k | { |
3119 | 0 | # define PNG_CHUNK(cHNK, index)\ |
3120 | 360k | case png_ ## cHNK: return PNG_INDEX_ ## cHNK; /* == index */ |
3121 | | |
3122 | 0 | PNG_KNOWN_CHUNKS |
3123 | | |
3124 | 0 | # undef PNG_CHUNK |
3125 | | |
3126 | 134k | default: return PNG_INDEX_unknown; |
3127 | 494k | } |
3128 | 494k | } |
3129 | | |
3130 | | png_handle_result_code /*PRIVATE*/ |
3131 | | png_handle_chunk(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
3132 | 494k | { |
3133 | | /* CSE: these things don't change, these autos are just to save typing and |
3134 | | * make the code more clear. |
3135 | | */ |
3136 | 494k | const png_uint_32 chunk_name = png_ptr->chunk_name; |
3137 | 494k | const png_index chunk_index = png_chunk_index_from_name(chunk_name); |
3138 | | |
3139 | 494k | png_handle_result_code handled = handled_error; |
3140 | 494k | png_const_charp errmsg = NULL; |
3141 | | |
3142 | | /* Is this a known chunk? If not there are no checks performed here; |
3143 | | * png_handle_unknown does the correct checks. This means that the values |
3144 | | * for known but unsupported chunks in the above table are not used here |
3145 | | * however the chunks_seen fields in png_struct are still set. |
3146 | | */ |
3147 | 494k | if (chunk_index == PNG_INDEX_unknown || |
3148 | 494k | read_chunks[chunk_index].handler == NULL) |
3149 | 139k | { |
3150 | 139k | handled = png_handle_unknown( |
3151 | 139k | png_ptr, info_ptr, length, PNG_HANDLE_CHUNK_AS_DEFAULT); |
3152 | 139k | } |
3153 | | |
3154 | | /* First check the position. The first check is historical; the stream must |
3155 | | * start with IHDR and anything else causes libpng to give up immediately. |
3156 | | */ |
3157 | 354k | else if (chunk_index != PNG_INDEX_IHDR && |
3158 | 354k | (png_ptr->mode & PNG_HAVE_IHDR) == 0) |
3159 | 83 | png_chunk_error(png_ptr, "missing IHDR"); /* NORETURN */ |
3160 | | |
3161 | | /* Before all the pos_before chunks, after all the pos_after chunks. */ |
3162 | 354k | else if (((png_ptr->mode & read_chunks[chunk_index].pos_before) != 0) || |
3163 | 354k | ((png_ptr->mode & read_chunks[chunk_index].pos_after) != |
3164 | 351k | read_chunks[chunk_index].pos_after)) |
3165 | 3.70k | { |
3166 | 3.70k | errmsg = "out of place"; |
3167 | 3.70k | } |
3168 | | |
3169 | | /* Now check for duplicates: duplicated critical chunks also produce a |
3170 | | * full error. |
3171 | | */ |
3172 | 351k | else if (read_chunks[chunk_index].multiple == 0 && |
3173 | 351k | png_file_has_chunk(png_ptr, chunk_index)) |
3174 | 3.27k | { |
3175 | 3.27k | errmsg = "duplicate"; |
3176 | 3.27k | } |
3177 | | |
3178 | 347k | else if (length < read_chunks[chunk_index].min_length) |
3179 | 22.7k | errmsg = "too short"; |
3180 | 325k | else |
3181 | 325k | { |
3182 | | /* NOTE: apart from IHDR the critical chunks (PLTE, IDAT and IEND) are set |
3183 | | * up above not to do any length checks. |
3184 | | * |
3185 | | * The png_chunk_max check ensures that the variable length chunks are |
3186 | | * always checked at this point for being within the system allocation |
3187 | | * limits. |
3188 | | */ |
3189 | 325k | unsigned max_length = read_chunks[chunk_index].max_length; |
3190 | | |
3191 | 325k | switch (max_length) |
3192 | 325k | { |
3193 | 19.7k | case Limit: |
3194 | | /* png_read_chunk_header has already png_error'ed chunks with a |
3195 | | * length exceeding the 31-bit PNG limit, so just check the memory |
3196 | | * limit: |
3197 | | */ |
3198 | 19.7k | if (length <= png_chunk_max(png_ptr)) |
3199 | 19.6k | goto MeetsLimit; |
3200 | | |
3201 | 96 | errmsg = "length exceeds libpng limit"; |
3202 | 96 | break; |
3203 | | |
3204 | 151k | default: |
3205 | 151k | if (length <= max_length) |
3206 | 149k | goto MeetsLimit; |
3207 | | |
3208 | 1.97k | errmsg = "too long"; |
3209 | 1.97k | break; |
3210 | | |
3211 | 153k | case NoCheck: |
3212 | 322k | MeetsLimit: |
3213 | 322k | handled = read_chunks[chunk_index].handler( |
3214 | 322k | png_ptr, info_ptr, length); |
3215 | 322k | break; |
3216 | 325k | } |
3217 | 325k | } |
3218 | | |
3219 | | /* If there was an error or the chunk was simply skipped it is not counted as |
3220 | | * 'seen'. |
3221 | | */ |
3222 | 479k | if (errmsg != NULL) |
3223 | 31.8k | { |
3224 | 31.8k | if (PNG_CHUNK_CRITICAL(chunk_name)) /* stop immediately */ |
3225 | 326 | png_chunk_error(png_ptr, errmsg); |
3226 | 31.4k | else /* ancillary chunk */ |
3227 | 31.4k | { |
3228 | | /* The chunk data is skipped: */ |
3229 | 31.4k | png_crc_finish(png_ptr, length); |
3230 | 31.4k | png_chunk_benign_error(png_ptr, errmsg); |
3231 | 31.4k | } |
3232 | 31.8k | } |
3233 | | |
3234 | 447k | else if (handled >= handled_saved) |
3235 | 305k | { |
3236 | 305k | if (chunk_index != PNG_INDEX_unknown) |
3237 | 179k | png_file_add_chunk(png_ptr, chunk_index); |
3238 | 305k | } |
3239 | | |
3240 | 478k | return handled; |
3241 | 479k | } |
3242 | | |
3243 | | /* Combines the row recently read in with the existing pixels in the row. This |
3244 | | * routine takes care of alpha and transparency if requested. This routine also |
3245 | | * handles the two methods of progressive display of interlaced images, |
3246 | | * depending on the 'display' value; if 'display' is true then the whole row |
3247 | | * (dp) is filled from the start by replicating the available pixels. If |
3248 | | * 'display' is false only those pixels present in the pass are filled in. |
3249 | | */ |
3250 | | void /* PRIVATE */ |
3251 | | png_combine_row(png_const_structrp png_ptr, png_bytep dp, int display) |
3252 | 1.00M | { |
3253 | 1.00M | unsigned int pixel_depth = png_ptr->transformed_pixel_depth; |
3254 | 1.00M | png_const_bytep sp = png_ptr->row_buf + 1; |
3255 | 1.00M | png_alloc_size_t row_width = png_ptr->width; |
3256 | 1.00M | unsigned int pass = png_ptr->pass; |
3257 | 1.00M | png_bytep end_ptr = 0; |
3258 | 1.00M | png_byte end_byte = 0; |
3259 | 1.00M | unsigned int end_mask; |
3260 | | |
3261 | 1.00M | png_debug(1, "in png_combine_row"); |
3262 | | |
3263 | | /* Added in 1.5.6: it should not be possible to enter this routine until at |
3264 | | * least one row has been read from the PNG data and transformed. |
3265 | | */ |
3266 | 1.00M | if (pixel_depth == 0) |
3267 | 0 | png_error(png_ptr, "internal row logic error"); |
3268 | | |
3269 | | /* Added in 1.5.4: the pixel depth should match the information returned by |
3270 | | * any call to png_read_update_info at this point. Do not continue if we got |
3271 | | * this wrong. |
3272 | | */ |
3273 | 1.00M | if (png_ptr->info_rowbytes != 0 && png_ptr->info_rowbytes != |
3274 | 1.00M | PNG_ROWBYTES(pixel_depth, row_width)) |
3275 | 0 | png_error(png_ptr, "internal row size calculation error"); |
3276 | | |
3277 | | /* Don't expect this to ever happen: */ |
3278 | 1.00M | if (row_width == 0) |
3279 | 0 | png_error(png_ptr, "internal row width error"); |
3280 | | |
3281 | | /* Preserve the last byte in cases where only part of it will be overwritten, |
3282 | | * the multiply below may overflow, we don't care because ANSI-C guarantees |
3283 | | * we get the low bits. |
3284 | | */ |
3285 | 1.00M | end_mask = (pixel_depth * row_width) & 7; |
3286 | 1.00M | if (end_mask != 0) |
3287 | 0 | { |
3288 | | /* end_ptr == NULL is a flag to say do nothing */ |
3289 | 0 | end_ptr = dp + PNG_ROWBYTES(pixel_depth, row_width) - 1; |
3290 | 0 | end_byte = *end_ptr; |
3291 | 0 | # ifdef PNG_READ_PACKSWAP_SUPPORTED |
3292 | 0 | if ((png_ptr->transformations & PNG_PACKSWAP) != 0) |
3293 | | /* little-endian byte */ |
3294 | 0 | end_mask = (unsigned int)(0xff << end_mask); |
3295 | | |
3296 | 0 | else /* big-endian byte */ |
3297 | 0 | # endif |
3298 | 0 | end_mask = 0xff >> end_mask; |
3299 | | /* end_mask is now the bits to *keep* from the destination row */ |
3300 | 0 | } |
3301 | | |
3302 | | /* For non-interlaced images this reduces to a memcpy(). A memcpy() |
3303 | | * will also happen if interlacing isn't supported or if the application |
3304 | | * does not call png_set_interlace_handling(). In the latter cases the |
3305 | | * caller just gets a sequence of the unexpanded rows from each interlace |
3306 | | * pass. |
3307 | | */ |
3308 | 1.00M | #ifdef PNG_READ_INTERLACING_SUPPORTED |
3309 | 1.00M | if (png_ptr->interlaced != 0 && |
3310 | 1.00M | (png_ptr->transformations & PNG_INTERLACE) != 0 && |
3311 | 1.00M | pass < 6 && (display == 0 || |
3312 | | /* The following copies everything for 'display' on passes 0, 2 and 4. */ |
3313 | 431k | (display == 1 && (pass & 1) != 0))) |
3314 | 431k | { |
3315 | | /* Narrow images may have no bits in a pass; the caller should handle |
3316 | | * this, but this test is cheap: |
3317 | | */ |
3318 | 431k | if (row_width <= PNG_PASS_START_COL(pass)) |
3319 | 0 | return; |
3320 | | |
3321 | 431k | if (pixel_depth < 8) |
3322 | 0 | { |
3323 | | /* For pixel depths up to 4 bpp the 8-pixel mask can be expanded to fit |
3324 | | * into 32 bits, then a single loop over the bytes using the four byte |
3325 | | * values in the 32-bit mask can be used. For the 'display' option the |
3326 | | * expanded mask may also not require any masking within a byte. To |
3327 | | * make this work the PACKSWAP option must be taken into account - it |
3328 | | * simply requires the pixels to be reversed in each byte. |
3329 | | * |
3330 | | * The 'regular' case requires a mask for each of the first 6 passes, |
3331 | | * the 'display' case does a copy for the even passes in the range |
3332 | | * 0..6. This has already been handled in the test above. |
3333 | | * |
3334 | | * The masks are arranged as four bytes with the first byte to use in |
3335 | | * the lowest bits (little-endian) regardless of the order (PACKSWAP or |
3336 | | * not) of the pixels in each byte. |
3337 | | * |
3338 | | * NOTE: the whole of this logic depends on the caller of this function |
3339 | | * only calling it on rows appropriate to the pass. This function only |
3340 | | * understands the 'x' logic; the 'y' logic is handled by the caller. |
3341 | | * |
3342 | | * The following defines allow generation of compile time constant bit |
3343 | | * masks for each pixel depth and each possibility of swapped or not |
3344 | | * swapped bytes. Pass 'p' is in the range 0..6; 'x', a pixel index, |
3345 | | * is in the range 0..7; and the result is 1 if the pixel is to be |
3346 | | * copied in the pass, 0 if not. 'S' is for the sparkle method, 'B' |
3347 | | * for the block method. |
3348 | | * |
3349 | | * With some compilers a compile time expression of the general form: |
3350 | | * |
3351 | | * (shift >= 32) ? (a >> (shift-32)) : (b >> shift) |
3352 | | * |
3353 | | * Produces warnings with values of 'shift' in the range 33 to 63 |
3354 | | * because the right hand side of the ?: expression is evaluated by |
3355 | | * the compiler even though it isn't used. Microsoft Visual C (various |
3356 | | * versions) and the Intel C compiler are known to do this. To avoid |
3357 | | * this the following macros are used in 1.5.6. This is a temporary |
3358 | | * solution to avoid destabilizing the code during the release process. |
3359 | | */ |
3360 | 0 | # if PNG_USE_COMPILE_TIME_MASKS |
3361 | 0 | # define PNG_LSR(x,s) ((x)>>((s) & 0x1f)) |
3362 | 0 | # define PNG_LSL(x,s) ((x)<<((s) & 0x1f)) |
3363 | | # else |
3364 | | # define PNG_LSR(x,s) ((x)>>(s)) |
3365 | | # define PNG_LSL(x,s) ((x)<<(s)) |
3366 | | # endif |
3367 | 0 | # define S_COPY(p,x) (((p)<4 ? PNG_LSR(0x80088822,(3-(p))*8+(7-(x))) :\ |
3368 | 0 | PNG_LSR(0xaa55ff00,(7-(p))*8+(7-(x)))) & 1) |
3369 | 0 | # define B_COPY(p,x) (((p)<4 ? PNG_LSR(0xff0fff33,(3-(p))*8+(7-(x))) :\ |
3370 | 0 | PNG_LSR(0xff55ff00,(7-(p))*8+(7-(x)))) & 1) |
3371 | | |
3372 | | /* Return a mask for pass 'p' pixel 'x' at depth 'd'. The mask is |
3373 | | * little endian - the first pixel is at bit 0 - however the extra |
3374 | | * parameter 's' can be set to cause the mask position to be swapped |
3375 | | * within each byte, to match the PNG format. This is done by XOR of |
3376 | | * the shift with 7, 6 or 4 for bit depths 1, 2 and 4. |
3377 | | */ |
3378 | 0 | # define PIXEL_MASK(p,x,d,s) \ |
3379 | 0 | (PNG_LSL(((PNG_LSL(1U,(d)))-1),(((x)*(d))^((s)?8-(d):0)))) |
3380 | | |
3381 | | /* Hence generate the appropriate 'block' or 'sparkle' pixel copy mask. |
3382 | | */ |
3383 | 0 | # define S_MASKx(p,x,d,s) (S_COPY(p,x)?PIXEL_MASK(p,x,d,s):0) |
3384 | 0 | # define B_MASKx(p,x,d,s) (B_COPY(p,x)?PIXEL_MASK(p,x,d,s):0) |
3385 | | |
3386 | | /* Combine 8 of these to get the full mask. For the 1-bpp and 2-bpp |
3387 | | * cases the result needs replicating, for the 4-bpp case the above |
3388 | | * generates a full 32 bits. |
3389 | | */ |
3390 | 0 | # define MASK_EXPAND(m,d) ((m)*((d)==1?0x01010101:((d)==2?0x00010001:1))) |
3391 | |
|
3392 | 0 | # define S_MASK(p,d,s) MASK_EXPAND(S_MASKx(p,0,d,s) + S_MASKx(p,1,d,s) +\ |
3393 | 0 | S_MASKx(p,2,d,s) + S_MASKx(p,3,d,s) + S_MASKx(p,4,d,s) +\ |
3394 | 0 | S_MASKx(p,5,d,s) + S_MASKx(p,6,d,s) + S_MASKx(p,7,d,s), d) |
3395 | |
|
3396 | 0 | # define B_MASK(p,d,s) MASK_EXPAND(B_MASKx(p,0,d,s) + B_MASKx(p,1,d,s) +\ |
3397 | 0 | B_MASKx(p,2,d,s) + B_MASKx(p,3,d,s) + B_MASKx(p,4,d,s) +\ |
3398 | 0 | B_MASKx(p,5,d,s) + B_MASKx(p,6,d,s) + B_MASKx(p,7,d,s), d) |
3399 | |
|
3400 | 0 | #if PNG_USE_COMPILE_TIME_MASKS |
3401 | | /* Utility macros to construct all the masks for a depth/swap |
3402 | | * combination. The 's' parameter says whether the format is PNG |
3403 | | * (big endian bytes) or not. Only the three odd-numbered passes are |
3404 | | * required for the display/block algorithm. |
3405 | | */ |
3406 | 0 | # define S_MASKS(d,s) { S_MASK(0,d,s), S_MASK(1,d,s), S_MASK(2,d,s),\ |
3407 | 0 | S_MASK(3,d,s), S_MASK(4,d,s), S_MASK(5,d,s) } |
3408 | |
|
3409 | 0 | # define B_MASKS(d,s) { B_MASK(1,d,s), B_MASK(3,d,s), B_MASK(5,d,s) } |
3410 | |
|
3411 | 0 | # define DEPTH_INDEX(d) ((d)==1?0:((d)==2?1:2)) |
3412 | | |
3413 | | /* Hence the pre-compiled masks indexed by PACKSWAP (or not), depth and |
3414 | | * then pass: |
3415 | | */ |
3416 | 0 | static const png_uint_32 row_mask[2/*PACKSWAP*/][3/*depth*/][6] = |
3417 | 0 | { |
3418 | | /* Little-endian byte masks for PACKSWAP */ |
3419 | 0 | { S_MASKS(1,0), S_MASKS(2,0), S_MASKS(4,0) }, |
3420 | | /* Normal (big-endian byte) masks - PNG format */ |
3421 | 0 | { S_MASKS(1,1), S_MASKS(2,1), S_MASKS(4,1) } |
3422 | 0 | }; |
3423 | | |
3424 | | /* display_mask has only three entries for the odd passes, so index by |
3425 | | * pass>>1. |
3426 | | */ |
3427 | 0 | static const png_uint_32 display_mask[2][3][3] = |
3428 | 0 | { |
3429 | | /* Little-endian byte masks for PACKSWAP */ |
3430 | 0 | { B_MASKS(1,0), B_MASKS(2,0), B_MASKS(4,0) }, |
3431 | | /* Normal (big-endian byte) masks - PNG format */ |
3432 | 0 | { B_MASKS(1,1), B_MASKS(2,1), B_MASKS(4,1) } |
3433 | 0 | }; |
3434 | |
|
3435 | 0 | # define MASK(pass,depth,display,png)\ |
3436 | 0 | ((display)?display_mask[png][DEPTH_INDEX(depth)][pass>>1]:\ |
3437 | 0 | row_mask[png][DEPTH_INDEX(depth)][pass]) |
3438 | |
|
3439 | | #else /* !PNG_USE_COMPILE_TIME_MASKS */ |
3440 | | /* This is the runtime alternative: it seems unlikely that this will |
3441 | | * ever be either smaller or faster than the compile time approach. |
3442 | | */ |
3443 | | # define MASK(pass,depth,display,png)\ |
3444 | | ((display)?B_MASK(pass,depth,png):S_MASK(pass,depth,png)) |
3445 | | #endif /* !USE_COMPILE_TIME_MASKS */ |
3446 | | |
3447 | | /* Use the appropriate mask to copy the required bits. In some cases |
3448 | | * the byte mask will be 0 or 0xff; optimize these cases. row_width is |
3449 | | * the number of pixels, but the code copies bytes, so it is necessary |
3450 | | * to special case the end. |
3451 | | */ |
3452 | 0 | png_uint_32 pixels_per_byte = 8 / pixel_depth; |
3453 | 0 | png_uint_32 mask; |
3454 | |
|
3455 | 0 | # ifdef PNG_READ_PACKSWAP_SUPPORTED |
3456 | 0 | if ((png_ptr->transformations & PNG_PACKSWAP) != 0) |
3457 | 0 | mask = MASK(pass, pixel_depth, display, 0); |
3458 | | |
3459 | 0 | else |
3460 | 0 | # endif |
3461 | 0 | mask = MASK(pass, pixel_depth, display, 1); |
3462 | |
|
3463 | 0 | for (;;) |
3464 | 0 | { |
3465 | 0 | png_uint_32 m; |
3466 | | |
3467 | | /* It doesn't matter in the following if png_uint_32 has more than |
3468 | | * 32 bits because the high bits always match those in m<<24; it is, |
3469 | | * however, essential to use OR here, not +, because of this. |
3470 | | */ |
3471 | 0 | m = mask; |
3472 | 0 | mask = (m >> 8) | (m << 24); /* rotate right to good compilers */ |
3473 | 0 | m &= 0xff; |
3474 | |
|
3475 | 0 | if (m != 0) /* something to copy */ |
3476 | 0 | { |
3477 | 0 | if (m != 0xff) |
3478 | 0 | *dp = (png_byte)((*dp & ~m) | (*sp & m)); |
3479 | 0 | else |
3480 | 0 | *dp = *sp; |
3481 | 0 | } |
3482 | | |
3483 | | /* NOTE: this may overwrite the last byte with garbage if the image |
3484 | | * is not an exact number of bytes wide; libpng has always done |
3485 | | * this. |
3486 | | */ |
3487 | 0 | if (row_width <= pixels_per_byte) |
3488 | 0 | break; /* May need to restore part of the last byte */ |
3489 | | |
3490 | 0 | row_width -= pixels_per_byte; |
3491 | 0 | ++dp; |
3492 | 0 | ++sp; |
3493 | 0 | } |
3494 | 0 | } |
3495 | | |
3496 | 431k | else /* pixel_depth >= 8 */ |
3497 | 431k | { |
3498 | 431k | unsigned int bytes_to_copy, bytes_to_jump; |
3499 | | |
3500 | | /* Validate the depth - it must be a multiple of 8 */ |
3501 | 431k | if (pixel_depth & 7) |
3502 | 0 | png_error(png_ptr, "invalid user transform pixel depth"); |
3503 | | |
3504 | 431k | pixel_depth >>= 3; /* now in bytes */ |
3505 | 431k | row_width *= pixel_depth; |
3506 | | |
3507 | | /* Regardless of pass number the Adam 7 interlace always results in a |
3508 | | * fixed number of pixels to copy then to skip. There may be a |
3509 | | * different number of pixels to skip at the start though. |
3510 | | */ |
3511 | 431k | { |
3512 | 431k | unsigned int offset = PNG_PASS_START_COL(pass) * pixel_depth; |
3513 | | |
3514 | 431k | row_width -= offset; |
3515 | 431k | dp += offset; |
3516 | 431k | sp += offset; |
3517 | 431k | } |
3518 | | |
3519 | | /* Work out the bytes to copy. */ |
3520 | 431k | if (display != 0) |
3521 | 0 | { |
3522 | | /* When doing the 'block' algorithm the pixel in the pass gets |
3523 | | * replicated to adjacent pixels. This is why the even (0,2,4,6) |
3524 | | * passes are skipped above - the entire expanded row is copied. |
3525 | | */ |
3526 | 0 | bytes_to_copy = (1<<((6-pass)>>1)) * pixel_depth; |
3527 | | |
3528 | | /* But don't allow this number to exceed the actual row width. */ |
3529 | 0 | if (bytes_to_copy > row_width) |
3530 | 0 | bytes_to_copy = (unsigned int)/*SAFE*/row_width; |
3531 | 0 | } |
3532 | | |
3533 | 431k | else /* normal row; Adam7 only ever gives us one pixel to copy. */ |
3534 | 431k | bytes_to_copy = pixel_depth; |
3535 | | |
3536 | | /* In Adam7 there is a constant offset between where the pixels go. */ |
3537 | 431k | bytes_to_jump = PNG_PASS_COL_OFFSET(pass) * pixel_depth; |
3538 | | |
3539 | | /* And simply copy these bytes. Some optimization is possible here, |
3540 | | * depending on the value of 'bytes_to_copy'. Special case the low |
3541 | | * byte counts, which we know to be frequent. |
3542 | | * |
3543 | | * Notice that these cases all 'return' rather than 'break' - this |
3544 | | * avoids an unnecessary test on whether to restore the last byte |
3545 | | * below. |
3546 | | */ |
3547 | 431k | switch (bytes_to_copy) |
3548 | 431k | { |
3549 | 120k | case 1: |
3550 | 120k | for (;;) |
3551 | 4.13M | { |
3552 | 4.13M | *dp = *sp; |
3553 | | |
3554 | 4.13M | if (row_width <= bytes_to_jump) |
3555 | 120k | return; |
3556 | | |
3557 | 4.01M | dp += bytes_to_jump; |
3558 | 4.01M | sp += bytes_to_jump; |
3559 | 4.01M | row_width -= bytes_to_jump; |
3560 | 4.01M | } |
3561 | | |
3562 | 181k | case 2: |
3563 | | /* There is a possibility of a partial copy at the end here; this |
3564 | | * slows the code down somewhat. |
3565 | | */ |
3566 | 181k | do |
3567 | 2.09M | { |
3568 | 2.09M | dp[0] = sp[0]; dp[1] = sp[1]; |
3569 | | |
3570 | 2.09M | if (row_width <= bytes_to_jump) |
3571 | 181k | return; |
3572 | | |
3573 | 1.91M | sp += bytes_to_jump; |
3574 | 1.91M | dp += bytes_to_jump; |
3575 | 1.91M | row_width -= bytes_to_jump; |
3576 | 1.91M | } |
3577 | 1.91M | while (row_width > 1); |
3578 | | |
3579 | | /* And there can only be one byte left at this point: */ |
3580 | 0 | *dp = *sp; |
3581 | 0 | return; |
3582 | | |
3583 | 82.0k | case 3: |
3584 | | /* This can only be the RGB case, so each copy is exactly one |
3585 | | * pixel and it is not necessary to check for a partial copy. |
3586 | | */ |
3587 | 82.0k | for (;;) |
3588 | 2.20M | { |
3589 | 2.20M | dp[0] = sp[0]; dp[1] = sp[1]; dp[2] = sp[2]; |
3590 | | |
3591 | 2.20M | if (row_width <= bytes_to_jump) |
3592 | 82.0k | return; |
3593 | | |
3594 | 2.11M | sp += bytes_to_jump; |
3595 | 2.11M | dp += bytes_to_jump; |
3596 | 2.11M | row_width -= bytes_to_jump; |
3597 | 2.11M | } |
3598 | | |
3599 | 48.2k | default: |
3600 | 48.2k | #if PNG_ALIGN_TYPE != PNG_ALIGN_NONE |
3601 | | /* Check for double byte alignment and, if possible, use a |
3602 | | * 16-bit copy. Don't attempt this for narrow images - ones that |
3603 | | * are less than an interlace panel wide. Don't attempt it for |
3604 | | * wide bytes_to_copy either - use the memcpy there. |
3605 | | */ |
3606 | 48.2k | if (bytes_to_copy < 16 /*else use memcpy*/ && |
3607 | 48.2k | png_isaligned(dp, png_uint_16) && |
3608 | 48.2k | png_isaligned(sp, png_uint_16) && |
3609 | 48.2k | bytes_to_copy % (sizeof (png_uint_16)) == 0 && |
3610 | 48.2k | bytes_to_jump % (sizeof (png_uint_16)) == 0) |
3611 | 48.2k | { |
3612 | | /* Everything is aligned for png_uint_16 copies, but try for |
3613 | | * png_uint_32 first. |
3614 | | */ |
3615 | 48.2k | if (png_isaligned(dp, png_uint_32) && |
3616 | 48.2k | png_isaligned(sp, png_uint_32) && |
3617 | 48.2k | bytes_to_copy % (sizeof (png_uint_32)) == 0 && |
3618 | 48.2k | bytes_to_jump % (sizeof (png_uint_32)) == 0) |
3619 | 31.5k | { |
3620 | 31.5k | png_uint_32p dp32 = png_aligncast(png_uint_32p,dp); |
3621 | 31.5k | png_const_uint_32p sp32 = png_aligncastconst( |
3622 | 31.5k | png_const_uint_32p, sp); |
3623 | 31.5k | size_t skip = (bytes_to_jump-bytes_to_copy) / |
3624 | 31.5k | (sizeof (png_uint_32)); |
3625 | | |
3626 | 31.5k | do |
3627 | 170k | { |
3628 | 170k | size_t c = bytes_to_copy; |
3629 | 170k | do |
3630 | 236k | { |
3631 | 236k | *dp32++ = *sp32++; |
3632 | 236k | c -= (sizeof (png_uint_32)); |
3633 | 236k | } |
3634 | 236k | while (c > 0); |
3635 | | |
3636 | 170k | if (row_width <= bytes_to_jump) |
3637 | 31.5k | return; |
3638 | | |
3639 | 139k | dp32 += skip; |
3640 | 139k | sp32 += skip; |
3641 | 139k | row_width -= bytes_to_jump; |
3642 | 139k | } |
3643 | 139k | while (bytes_to_copy <= row_width); |
3644 | | |
3645 | | /* Get to here when the row_width truncates the final copy. |
3646 | | * There will be 1-3 bytes left to copy, so don't try the |
3647 | | * 16-bit loop below. |
3648 | | */ |
3649 | 0 | dp = (png_bytep)dp32; |
3650 | 0 | sp = (png_const_bytep)sp32; |
3651 | 0 | do |
3652 | 0 | *dp++ = *sp++; |
3653 | 0 | while (--row_width > 0); |
3654 | 0 | return; |
3655 | 31.5k | } |
3656 | | |
3657 | | /* Else do it in 16-bit quantities, but only if the size is |
3658 | | * not too large. |
3659 | | */ |
3660 | 16.7k | else |
3661 | 16.7k | { |
3662 | 16.7k | png_uint_16p dp16 = png_aligncast(png_uint_16p, dp); |
3663 | 16.7k | png_const_uint_16p sp16 = png_aligncastconst( |
3664 | 16.7k | png_const_uint_16p, sp); |
3665 | 16.7k | size_t skip = (bytes_to_jump-bytes_to_copy) / |
3666 | 16.7k | (sizeof (png_uint_16)); |
3667 | | |
3668 | 16.7k | do |
3669 | 94.3k | { |
3670 | 94.3k | size_t c = bytes_to_copy; |
3671 | 94.3k | do |
3672 | 283k | { |
3673 | 283k | *dp16++ = *sp16++; |
3674 | 283k | c -= (sizeof (png_uint_16)); |
3675 | 283k | } |
3676 | 283k | while (c > 0); |
3677 | | |
3678 | 94.3k | if (row_width <= bytes_to_jump) |
3679 | 16.7k | return; |
3680 | | |
3681 | 77.5k | dp16 += skip; |
3682 | 77.5k | sp16 += skip; |
3683 | 77.5k | row_width -= bytes_to_jump; |
3684 | 77.5k | } |
3685 | 77.5k | while (bytes_to_copy <= row_width); |
3686 | | |
3687 | | /* End of row - 1 byte left, bytes_to_copy > row_width: */ |
3688 | 0 | dp = (png_bytep)dp16; |
3689 | 0 | sp = (png_const_bytep)sp16; |
3690 | 0 | do |
3691 | 0 | *dp++ = *sp++; |
3692 | 0 | while (--row_width > 0); |
3693 | 0 | return; |
3694 | 16.7k | } |
3695 | 48.2k | } |
3696 | 0 | #endif /* ALIGN_TYPE code */ |
3697 | | |
3698 | | /* The true default - use a memcpy: */ |
3699 | 0 | for (;;) |
3700 | 0 | { |
3701 | 0 | memcpy(dp, sp, bytes_to_copy); |
3702 | |
|
3703 | 0 | if (row_width <= bytes_to_jump) |
3704 | 0 | return; |
3705 | | |
3706 | 0 | sp += bytes_to_jump; |
3707 | 0 | dp += bytes_to_jump; |
3708 | 0 | row_width -= bytes_to_jump; |
3709 | 0 | if (bytes_to_copy > row_width) |
3710 | 0 | bytes_to_copy = (unsigned int)/*SAFE*/row_width; |
3711 | 0 | } |
3712 | 431k | } |
3713 | | |
3714 | | /* NOT REACHED*/ |
3715 | 431k | } /* pixel_depth >= 8 */ |
3716 | | |
3717 | | /* Here if pixel_depth < 8 to check 'end_ptr' below. */ |
3718 | 431k | } |
3719 | 574k | else |
3720 | 574k | #endif /* READ_INTERLACING */ |
3721 | | |
3722 | | /* If here then the switch above wasn't used so just memcpy the whole row |
3723 | | * from the temporary row buffer (notice that this overwrites the end of the |
3724 | | * destination row if it is a partial byte.) |
3725 | | */ |
3726 | 574k | memcpy(dp, sp, PNG_ROWBYTES(pixel_depth, row_width)); |
3727 | | |
3728 | | /* Restore the overwritten bits from the last byte if necessary. */ |
3729 | 574k | if (end_ptr != NULL) |
3730 | 0 | *end_ptr = (png_byte)((end_byte & end_mask) | (*end_ptr & ~end_mask)); |
3731 | 574k | } |
3732 | | |
3733 | | #ifdef PNG_READ_INTERLACING_SUPPORTED |
3734 | | void /* PRIVATE */ |
3735 | | png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass, |
3736 | | png_uint_32 transformations /* Because these may affect the byte layout */) |
3737 | 431k | { |
3738 | 431k | png_debug(1, "in png_do_read_interlace"); |
3739 | 431k | if (row != NULL && row_info != NULL) |
3740 | 431k | { |
3741 | 431k | png_uint_32 final_width; |
3742 | | |
3743 | 431k | final_width = row_info->width * png_pass_inc[pass]; |
3744 | | |
3745 | 431k | switch (row_info->pixel_depth) |
3746 | 431k | { |
3747 | 0 | case 1: |
3748 | 0 | { |
3749 | 0 | png_bytep sp = row + (size_t)((row_info->width - 1) >> 3); |
3750 | 0 | png_bytep dp = row + (size_t)((final_width - 1) >> 3); |
3751 | 0 | unsigned int sshift, dshift; |
3752 | 0 | unsigned int s_start, s_end; |
3753 | 0 | int s_inc; |
3754 | 0 | int jstop = (int)png_pass_inc[pass]; |
3755 | 0 | png_byte v; |
3756 | 0 | png_uint_32 i; |
3757 | 0 | int j; |
3758 | |
|
3759 | 0 | #ifdef PNG_READ_PACKSWAP_SUPPORTED |
3760 | 0 | if ((transformations & PNG_PACKSWAP) != 0) |
3761 | 0 | { |
3762 | 0 | sshift = ((row_info->width + 7) & 0x07); |
3763 | 0 | dshift = ((final_width + 7) & 0x07); |
3764 | 0 | s_start = 7; |
3765 | 0 | s_end = 0; |
3766 | 0 | s_inc = -1; |
3767 | 0 | } |
3768 | | |
3769 | 0 | else |
3770 | 0 | #endif |
3771 | 0 | { |
3772 | 0 | sshift = 7 - ((row_info->width + 7) & 0x07); |
3773 | 0 | dshift = 7 - ((final_width + 7) & 0x07); |
3774 | 0 | s_start = 0; |
3775 | 0 | s_end = 7; |
3776 | 0 | s_inc = 1; |
3777 | 0 | } |
3778 | |
|
3779 | 0 | for (i = 0; i < row_info->width; i++) |
3780 | 0 | { |
3781 | 0 | v = (png_byte)((*sp >> sshift) & 0x01); |
3782 | 0 | for (j = 0; j < jstop; j++) |
3783 | 0 | { |
3784 | 0 | unsigned int tmp = *dp & (0x7f7f >> (7 - dshift)); |
3785 | 0 | tmp |= (unsigned int)(v << dshift); |
3786 | 0 | *dp = (png_byte)(tmp & 0xff); |
3787 | |
|
3788 | 0 | if (dshift == s_end) |
3789 | 0 | { |
3790 | 0 | dshift = s_start; |
3791 | 0 | dp--; |
3792 | 0 | } |
3793 | | |
3794 | 0 | else |
3795 | 0 | dshift = (unsigned int)((int)dshift + s_inc); |
3796 | 0 | } |
3797 | |
|
3798 | 0 | if (sshift == s_end) |
3799 | 0 | { |
3800 | 0 | sshift = s_start; |
3801 | 0 | sp--; |
3802 | 0 | } |
3803 | | |
3804 | 0 | else |
3805 | 0 | sshift = (unsigned int)((int)sshift + s_inc); |
3806 | 0 | } |
3807 | 0 | break; |
3808 | 0 | } |
3809 | | |
3810 | 0 | case 2: |
3811 | 0 | { |
3812 | 0 | png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2); |
3813 | 0 | png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2); |
3814 | 0 | unsigned int sshift, dshift; |
3815 | 0 | unsigned int s_start, s_end; |
3816 | 0 | int s_inc; |
3817 | 0 | int jstop = (int)png_pass_inc[pass]; |
3818 | 0 | png_uint_32 i; |
3819 | |
|
3820 | 0 | #ifdef PNG_READ_PACKSWAP_SUPPORTED |
3821 | 0 | if ((transformations & PNG_PACKSWAP) != 0) |
3822 | 0 | { |
3823 | 0 | sshift = (((row_info->width + 3) & 0x03) << 1); |
3824 | 0 | dshift = (((final_width + 3) & 0x03) << 1); |
3825 | 0 | s_start = 6; |
3826 | 0 | s_end = 0; |
3827 | 0 | s_inc = -2; |
3828 | 0 | } |
3829 | | |
3830 | 0 | else |
3831 | 0 | #endif |
3832 | 0 | { |
3833 | 0 | sshift = ((3 - ((row_info->width + 3) & 0x03)) << 1); |
3834 | 0 | dshift = ((3 - ((final_width + 3) & 0x03)) << 1); |
3835 | 0 | s_start = 0; |
3836 | 0 | s_end = 6; |
3837 | 0 | s_inc = 2; |
3838 | 0 | } |
3839 | |
|
3840 | 0 | for (i = 0; i < row_info->width; i++) |
3841 | 0 | { |
3842 | 0 | png_byte v; |
3843 | 0 | int j; |
3844 | |
|
3845 | 0 | v = (png_byte)((*sp >> sshift) & 0x03); |
3846 | 0 | for (j = 0; j < jstop; j++) |
3847 | 0 | { |
3848 | 0 | unsigned int tmp = *dp & (0x3f3f >> (6 - dshift)); |
3849 | 0 | tmp |= (unsigned int)(v << dshift); |
3850 | 0 | *dp = (png_byte)(tmp & 0xff); |
3851 | |
|
3852 | 0 | if (dshift == s_end) |
3853 | 0 | { |
3854 | 0 | dshift = s_start; |
3855 | 0 | dp--; |
3856 | 0 | } |
3857 | | |
3858 | 0 | else |
3859 | 0 | dshift = (unsigned int)((int)dshift + s_inc); |
3860 | 0 | } |
3861 | |
|
3862 | 0 | if (sshift == s_end) |
3863 | 0 | { |
3864 | 0 | sshift = s_start; |
3865 | 0 | sp--; |
3866 | 0 | } |
3867 | | |
3868 | 0 | else |
3869 | 0 | sshift = (unsigned int)((int)sshift + s_inc); |
3870 | 0 | } |
3871 | 0 | break; |
3872 | 0 | } |
3873 | | |
3874 | 0 | case 4: |
3875 | 0 | { |
3876 | 0 | png_bytep sp = row + (size_t)((row_info->width - 1) >> 1); |
3877 | 0 | png_bytep dp = row + (size_t)((final_width - 1) >> 1); |
3878 | 0 | unsigned int sshift, dshift; |
3879 | 0 | unsigned int s_start, s_end; |
3880 | 0 | int s_inc; |
3881 | 0 | png_uint_32 i; |
3882 | 0 | int jstop = (int)png_pass_inc[pass]; |
3883 | |
|
3884 | 0 | #ifdef PNG_READ_PACKSWAP_SUPPORTED |
3885 | 0 | if ((transformations & PNG_PACKSWAP) != 0) |
3886 | 0 | { |
3887 | 0 | sshift = (((row_info->width + 1) & 0x01) << 2); |
3888 | 0 | dshift = (((final_width + 1) & 0x01) << 2); |
3889 | 0 | s_start = 4; |
3890 | 0 | s_end = 0; |
3891 | 0 | s_inc = -4; |
3892 | 0 | } |
3893 | | |
3894 | 0 | else |
3895 | 0 | #endif |
3896 | 0 | { |
3897 | 0 | sshift = ((1 - ((row_info->width + 1) & 0x01)) << 2); |
3898 | 0 | dshift = ((1 - ((final_width + 1) & 0x01)) << 2); |
3899 | 0 | s_start = 0; |
3900 | 0 | s_end = 4; |
3901 | 0 | s_inc = 4; |
3902 | 0 | } |
3903 | |
|
3904 | 0 | for (i = 0; i < row_info->width; i++) |
3905 | 0 | { |
3906 | 0 | png_byte v = (png_byte)((*sp >> sshift) & 0x0f); |
3907 | 0 | int j; |
3908 | |
|
3909 | 0 | for (j = 0; j < jstop; j++) |
3910 | 0 | { |
3911 | 0 | unsigned int tmp = *dp & (0xf0f >> (4 - dshift)); |
3912 | 0 | tmp |= (unsigned int)(v << dshift); |
3913 | 0 | *dp = (png_byte)(tmp & 0xff); |
3914 | |
|
3915 | 0 | if (dshift == s_end) |
3916 | 0 | { |
3917 | 0 | dshift = s_start; |
3918 | 0 | dp--; |
3919 | 0 | } |
3920 | | |
3921 | 0 | else |
3922 | 0 | dshift = (unsigned int)((int)dshift + s_inc); |
3923 | 0 | } |
3924 | |
|
3925 | 0 | if (sshift == s_end) |
3926 | 0 | { |
3927 | 0 | sshift = s_start; |
3928 | 0 | sp--; |
3929 | 0 | } |
3930 | | |
3931 | 0 | else |
3932 | 0 | sshift = (unsigned int)((int)sshift + s_inc); |
3933 | 0 | } |
3934 | 0 | break; |
3935 | 0 | } |
3936 | | |
3937 | 431k | default: |
3938 | 431k | { |
3939 | 431k | size_t pixel_bytes = (row_info->pixel_depth >> 3); |
3940 | | |
3941 | 431k | png_bytep sp = row + (size_t)(row_info->width - 1) |
3942 | 431k | * pixel_bytes; |
3943 | | |
3944 | 431k | png_bytep dp = row + (size_t)(final_width - 1) * pixel_bytes; |
3945 | | |
3946 | 431k | int jstop = (int)png_pass_inc[pass]; |
3947 | 431k | png_uint_32 i; |
3948 | | |
3949 | 9.12M | for (i = 0; i < row_info->width; i++) |
3950 | 8.69M | { |
3951 | 8.69M | png_byte v[8]; /* SAFE; pixel_depth does not exceed 64 */ |
3952 | 8.69M | int j; |
3953 | | |
3954 | 8.69M | memcpy(v, sp, pixel_bytes); |
3955 | | |
3956 | 39.8M | for (j = 0; j < jstop; j++) |
3957 | 31.1M | { |
3958 | 31.1M | memcpy(dp, v, pixel_bytes); |
3959 | 31.1M | dp -= pixel_bytes; |
3960 | 31.1M | } |
3961 | | |
3962 | 8.69M | sp -= pixel_bytes; |
3963 | 8.69M | } |
3964 | 431k | break; |
3965 | 0 | } |
3966 | 431k | } |
3967 | | |
3968 | 431k | row_info->width = final_width; |
3969 | 431k | row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width); |
3970 | 431k | } |
3971 | | #ifndef PNG_READ_PACKSWAP_SUPPORTED |
3972 | | PNG_UNUSED(transformations) /* Silence compiler warning */ |
3973 | | #endif |
3974 | 431k | } |
3975 | | #endif /* READ_INTERLACING */ |
3976 | | |
3977 | | static void |
3978 | | png_read_filter_row_sub(png_row_infop row_info, png_bytep row, |
3979 | | png_const_bytep prev_row) |
3980 | 69.3k | { |
3981 | 69.3k | size_t i; |
3982 | 69.3k | size_t istop = row_info->rowbytes; |
3983 | 69.3k | unsigned int bpp = (row_info->pixel_depth + 7) >> 3; |
3984 | 69.3k | png_bytep rp = row + bpp; |
3985 | | |
3986 | 69.3k | PNG_UNUSED(prev_row) |
3987 | | |
3988 | 7.31M | for (i = bpp; i < istop; i++) |
3989 | 7.24M | { |
3990 | 7.24M | *rp = (png_byte)(((int)(*rp) + (int)(*(rp-bpp))) & 0xff); |
3991 | 7.24M | rp++; |
3992 | 7.24M | } |
3993 | 69.3k | } |
3994 | | |
3995 | | static void |
3996 | | png_read_filter_row_up(png_row_infop row_info, png_bytep row, |
3997 | | png_const_bytep prev_row) |
3998 | 85.7k | { |
3999 | 85.7k | size_t i; |
4000 | 85.7k | size_t istop = row_info->rowbytes; |
4001 | 85.7k | png_bytep rp = row; |
4002 | 85.7k | png_const_bytep pp = prev_row; |
4003 | | |
4004 | 23.8M | for (i = 0; i < istop; i++) |
4005 | 23.7M | { |
4006 | 23.7M | *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff); |
4007 | 23.7M | rp++; |
4008 | 23.7M | } |
4009 | 85.7k | } |
4010 | | |
4011 | | static void |
4012 | | png_read_filter_row_avg(png_row_infop row_info, png_bytep row, |
4013 | | png_const_bytep prev_row) |
4014 | 101k | { |
4015 | 101k | size_t i; |
4016 | 101k | png_bytep rp = row; |
4017 | 101k | png_const_bytep pp = prev_row; |
4018 | 101k | unsigned int bpp = (row_info->pixel_depth + 7) >> 3; |
4019 | 101k | size_t istop = row_info->rowbytes - bpp; |
4020 | | |
4021 | 234k | for (i = 0; i < bpp; i++) |
4022 | 133k | { |
4023 | 133k | *rp = (png_byte)(((int)(*rp) + |
4024 | 133k | ((int)(*pp++) / 2 )) & 0xff); |
4025 | | |
4026 | 133k | rp++; |
4027 | 133k | } |
4028 | | |
4029 | 3.21M | for (i = 0; i < istop; i++) |
4030 | 3.11M | { |
4031 | 3.11M | *rp = (png_byte)(((int)(*rp) + |
4032 | 3.11M | (int)(*pp++ + *(rp-bpp)) / 2 ) & 0xff); |
4033 | | |
4034 | 3.11M | rp++; |
4035 | 3.11M | } |
4036 | 101k | } |
4037 | | |
4038 | | static void |
4039 | | png_read_filter_row_paeth_1byte_pixel(png_row_infop row_info, png_bytep row, |
4040 | | png_const_bytep prev_row) |
4041 | 48.6k | { |
4042 | 48.6k | png_bytep rp_end = row + row_info->rowbytes; |
4043 | 48.6k | int a, c; |
4044 | | |
4045 | | /* First pixel/byte */ |
4046 | 48.6k | c = *prev_row++; |
4047 | 48.6k | a = *row + c; |
4048 | 48.6k | *row++ = (png_byte)a; |
4049 | | |
4050 | | /* Remainder */ |
4051 | 638k | while (row < rp_end) |
4052 | 589k | { |
4053 | 589k | int b, pa, pb, pc, p; |
4054 | | |
4055 | 589k | a &= 0xff; /* From previous iteration or start */ |
4056 | 589k | b = *prev_row++; |
4057 | | |
4058 | 589k | p = b - c; |
4059 | 589k | pc = a - c; |
4060 | | |
4061 | | #ifdef PNG_USE_ABS |
4062 | | pa = abs(p); |
4063 | | pb = abs(pc); |
4064 | | pc = abs(p + pc); |
4065 | | #else |
4066 | 589k | pa = p < 0 ? -p : p; |
4067 | 589k | pb = pc < 0 ? -pc : pc; |
4068 | 589k | pc = (p + pc) < 0 ? -(p + pc) : p + pc; |
4069 | 589k | #endif |
4070 | | |
4071 | | /* Find the best predictor, the least of pa, pb, pc favoring the earlier |
4072 | | * ones in the case of a tie. |
4073 | | */ |
4074 | 589k | if (pb < pa) |
4075 | 88.4k | { |
4076 | 88.4k | pa = pb; a = b; |
4077 | 88.4k | } |
4078 | 589k | if (pc < pa) a = c; |
4079 | | |
4080 | | /* Calculate the current pixel in a, and move the previous row pixel to c |
4081 | | * for the next time round the loop |
4082 | | */ |
4083 | 589k | c = b; |
4084 | 589k | a += *row; |
4085 | 589k | *row++ = (png_byte)a; |
4086 | 589k | } |
4087 | 48.6k | } |
4088 | | |
4089 | | static void |
4090 | | png_read_filter_row_paeth_multibyte_pixel(png_row_infop row_info, png_bytep row, |
4091 | | png_const_bytep prev_row) |
4092 | 37.2k | { |
4093 | 37.2k | unsigned int bpp = (row_info->pixel_depth + 7) >> 3; |
4094 | 37.2k | png_bytep rp_end = row + bpp; |
4095 | | |
4096 | | /* Process the first pixel in the row completely (this is the same as 'up' |
4097 | | * because there is only one candidate predictor for the first row). |
4098 | | */ |
4099 | 148k | while (row < rp_end) |
4100 | 111k | { |
4101 | 111k | int a = *row + *prev_row++; |
4102 | 111k | *row++ = (png_byte)a; |
4103 | 111k | } |
4104 | | |
4105 | | /* Remainder */ |
4106 | 37.2k | rp_end = rp_end + (row_info->rowbytes - bpp); |
4107 | | |
4108 | 2.84M | while (row < rp_end) |
4109 | 2.80M | { |
4110 | 2.80M | int a, b, c, pa, pb, pc, p; |
4111 | | |
4112 | 2.80M | c = *(prev_row - bpp); |
4113 | 2.80M | a = *(row - bpp); |
4114 | 2.80M | b = *prev_row++; |
4115 | | |
4116 | 2.80M | p = b - c; |
4117 | 2.80M | pc = a - c; |
4118 | | |
4119 | | #ifdef PNG_USE_ABS |
4120 | | pa = abs(p); |
4121 | | pb = abs(pc); |
4122 | | pc = abs(p + pc); |
4123 | | #else |
4124 | 2.80M | pa = p < 0 ? -p : p; |
4125 | 2.80M | pb = pc < 0 ? -pc : pc; |
4126 | 2.80M | pc = (p + pc) < 0 ? -(p + pc) : p + pc; |
4127 | 2.80M | #endif |
4128 | | |
4129 | 2.80M | if (pb < pa) |
4130 | 488k | { |
4131 | 488k | pa = pb; a = b; |
4132 | 488k | } |
4133 | 2.80M | if (pc < pa) a = c; |
4134 | | |
4135 | 2.80M | a += *row; |
4136 | 2.80M | *row++ = (png_byte)a; |
4137 | 2.80M | } |
4138 | 37.2k | } |
4139 | | |
4140 | | static void |
4141 | | png_init_filter_functions(png_structrp pp) |
4142 | | /* This function is called once for every PNG image (except for PNG images |
4143 | | * that only use PNG_FILTER_VALUE_NONE for all rows) to set the |
4144 | | * implementations required to reverse the filtering of PNG rows. Reversing |
4145 | | * the filter is the first transformation performed on the row data. It is |
4146 | | * performed in place, therefore an implementation can be selected based on |
4147 | | * the image pixel format. If the implementation depends on image width then |
4148 | | * take care to ensure that it works correctly if the image is interlaced - |
4149 | | * interlacing causes the actual row width to vary. |
4150 | | */ |
4151 | 8.76k | { |
4152 | 8.76k | unsigned int bpp = (pp->pixel_depth + 7) >> 3; |
4153 | | |
4154 | 8.76k | pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub; |
4155 | 8.76k | pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up; |
4156 | 8.76k | pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg; |
4157 | 8.76k | if (bpp == 1) |
4158 | 3.20k | pp->read_filter[PNG_FILTER_VALUE_PAETH-1] = |
4159 | 3.20k | png_read_filter_row_paeth_1byte_pixel; |
4160 | 5.55k | else |
4161 | 5.55k | pp->read_filter[PNG_FILTER_VALUE_PAETH-1] = |
4162 | 5.55k | png_read_filter_row_paeth_multibyte_pixel; |
4163 | | |
4164 | 8.76k | #ifdef PNG_FILTER_OPTIMIZATIONS |
4165 | | /* To use this define PNG_FILTER_OPTIMIZATIONS as the name of a function to |
4166 | | * call to install hardware optimizations for the above functions; simply |
4167 | | * replace whatever elements of the pp->read_filter[] array with a hardware |
4168 | | * specific (or, for that matter, generic) optimization. |
4169 | | * |
4170 | | * To see an example of this examine what configure.ac does when |
4171 | | * --enable-arm-neon is specified on the command line. |
4172 | | */ |
4173 | 8.76k | PNG_FILTER_OPTIMIZATIONS(pp, bpp); |
4174 | 8.76k | #endif |
4175 | 8.76k | } |
4176 | | |
4177 | | void /* PRIVATE */ |
4178 | | png_read_filter_row(png_structrp pp, png_row_infop row_info, png_bytep row, |
4179 | | png_const_bytep prev_row, int filter) |
4180 | 442k | { |
4181 | | /* OPTIMIZATION: DO NOT MODIFY THIS FUNCTION, instead #define |
4182 | | * PNG_FILTER_OPTIMIZATIONS to a function that overrides the generic |
4183 | | * implementations. See png_init_filter_functions above. |
4184 | | */ |
4185 | 442k | if (filter > PNG_FILTER_VALUE_NONE && filter < PNG_FILTER_VALUE_LAST) |
4186 | 442k | { |
4187 | 442k | if (pp->read_filter[0] == NULL) |
4188 | 8.76k | png_init_filter_functions(pp); |
4189 | | |
4190 | 442k | pp->read_filter[filter-1](row_info, row, prev_row); |
4191 | 442k | } |
4192 | 442k | } |
4193 | | |
4194 | | #ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
4195 | | void /* PRIVATE */ |
4196 | | png_read_IDAT_data(png_structrp png_ptr, png_bytep output, |
4197 | | png_alloc_size_t avail_out) |
4198 | 1.04M | { |
4199 | | /* Loop reading IDATs and decompressing the result into output[avail_out] */ |
4200 | 1.04M | png_ptr->zstream.next_out = output; |
4201 | 1.04M | png_ptr->zstream.avail_out = 0; /* safety: set below */ |
4202 | | |
4203 | 1.04M | if (output == NULL) |
4204 | 28.4k | avail_out = 0; |
4205 | | |
4206 | 1.04M | do |
4207 | 1.17M | { |
4208 | 1.17M | int ret; |
4209 | 1.17M | png_byte tmpbuf[PNG_INFLATE_BUF_SIZE]; |
4210 | | |
4211 | 1.17M | if (png_ptr->zstream.avail_in == 0) |
4212 | 133k | { |
4213 | 133k | uInt avail_in; |
4214 | 133k | png_bytep buffer; |
4215 | | |
4216 | 217k | while (png_ptr->idat_size == 0) |
4217 | 84.3k | { |
4218 | 84.3k | png_crc_finish(png_ptr, 0); |
4219 | | |
4220 | 84.3k | png_ptr->idat_size = png_read_chunk_header(png_ptr); |
4221 | | /* This is an error even in the 'check' case because the code just |
4222 | | * consumed a non-IDAT header. |
4223 | | */ |
4224 | 84.3k | if (png_ptr->chunk_name != png_IDAT) |
4225 | 155 | png_error(png_ptr, "Not enough image data"); |
4226 | 84.3k | } |
4227 | | |
4228 | 133k | avail_in = png_ptr->IDAT_read_size; |
4229 | | |
4230 | 133k | if (avail_in > png_chunk_max(png_ptr)) |
4231 | 0 | avail_in = (uInt)/*SAFE*/png_chunk_max(png_ptr); |
4232 | | |
4233 | 133k | if (avail_in > png_ptr->idat_size) |
4234 | 127k | avail_in = (uInt)png_ptr->idat_size; |
4235 | | |
4236 | | /* A PNG with a gradually increasing IDAT size will defeat this attempt |
4237 | | * to minimize memory usage by causing lots of re-allocs, but |
4238 | | * realistically doing IDAT_read_size re-allocs is not likely to be a |
4239 | | * big problem. |
4240 | | * |
4241 | | * An error here corresponds to the system being out of memory. |
4242 | | */ |
4243 | 133k | buffer = png_read_buffer(png_ptr, avail_in); |
4244 | | |
4245 | 133k | if (buffer == NULL) |
4246 | 0 | png_chunk_error(png_ptr, "out of memory"); |
4247 | | |
4248 | 133k | png_crc_read(png_ptr, buffer, avail_in); |
4249 | 133k | png_ptr->idat_size -= avail_in; |
4250 | | |
4251 | 133k | png_ptr->zstream.next_in = buffer; |
4252 | 133k | png_ptr->zstream.avail_in = avail_in; |
4253 | 133k | } |
4254 | | |
4255 | | /* And set up the output side. */ |
4256 | 1.17M | if (output != NULL) /* standard read */ |
4257 | 1.07M | { |
4258 | 1.07M | uInt out = ZLIB_IO_MAX; |
4259 | | |
4260 | 1.07M | if (out > avail_out) |
4261 | 1.07M | out = (uInt)avail_out; |
4262 | | |
4263 | 1.07M | avail_out -= out; |
4264 | 1.07M | png_ptr->zstream.avail_out = out; |
4265 | 1.07M | } |
4266 | | |
4267 | 95.8k | else /* after last row, checking for end */ |
4268 | 95.8k | { |
4269 | 95.8k | png_ptr->zstream.next_out = tmpbuf; |
4270 | 95.8k | png_ptr->zstream.avail_out = (sizeof tmpbuf); |
4271 | 95.8k | } |
4272 | | |
4273 | | /* Use NO_FLUSH; this gives zlib the maximum opportunity to optimize the |
4274 | | * process. If the LZ stream is truncated the sequential reader will |
4275 | | * terminally damage the stream, above, by reading the chunk header of the |
4276 | | * following chunk (it then exits with png_error). |
4277 | | * |
4278 | | * TODO: deal more elegantly with truncated IDAT lists. |
4279 | | */ |
4280 | 1.17M | ret = PNG_INFLATE(png_ptr, Z_NO_FLUSH); |
4281 | | |
4282 | | /* Take the unconsumed output back. */ |
4283 | 1.17M | if (output != NULL) |
4284 | 1.07M | avail_out += png_ptr->zstream.avail_out; |
4285 | | |
4286 | 95.8k | else /* avail_out counts the extra bytes */ |
4287 | 95.8k | avail_out += (sizeof tmpbuf) - png_ptr->zstream.avail_out; |
4288 | | |
4289 | 1.17M | png_ptr->zstream.avail_out = 0; |
4290 | | |
4291 | 1.17M | if (ret == Z_STREAM_END) |
4292 | 14.5k | { |
4293 | | /* Do this for safety; we won't read any more into this row. */ |
4294 | 14.5k | png_ptr->zstream.next_out = NULL; |
4295 | | |
4296 | 14.5k | png_ptr->mode |= PNG_AFTER_IDAT; |
4297 | 14.5k | png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED; |
4298 | | |
4299 | 14.5k | if (png_ptr->zstream.avail_in > 0 || png_ptr->idat_size > 0) |
4300 | 156 | png_chunk_benign_error(png_ptr, "Extra compressed data"); |
4301 | 14.5k | break; |
4302 | 14.5k | } |
4303 | | |
4304 | 1.15M | if (ret != Z_OK) |
4305 | 27.9k | { |
4306 | 27.9k | png_zstream_error(png_ptr, ret); |
4307 | | |
4308 | 27.9k | if (output != NULL) |
4309 | 3.00k | png_chunk_error(png_ptr, png_ptr->zstream.msg); |
4310 | | |
4311 | 24.9k | else /* checking */ |
4312 | 24.9k | { |
4313 | 24.9k | png_chunk_benign_error(png_ptr, png_ptr->zstream.msg); |
4314 | 24.9k | return; |
4315 | 24.9k | } |
4316 | 27.9k | } |
4317 | 1.15M | } while (avail_out > 0); |
4318 | | |
4319 | 1.01M | if (avail_out > 0) |
4320 | 1.97k | { |
4321 | | /* The stream ended before the image; this is the same as too few IDATs so |
4322 | | * should be handled the same way. |
4323 | | */ |
4324 | 1.97k | if (output != NULL) |
4325 | 23 | png_error(png_ptr, "Not enough image data"); |
4326 | | |
4327 | 1.95k | else /* the deflate stream contained extra data */ |
4328 | 1.95k | png_chunk_benign_error(png_ptr, "Too much image data"); |
4329 | 1.97k | } |
4330 | 1.01M | } |
4331 | | |
4332 | | void /* PRIVATE */ |
4333 | | png_read_finish_IDAT(png_structrp png_ptr) |
4334 | 80.0k | { |
4335 | | /* We don't need any more data and the stream should have ended, however the |
4336 | | * LZ end code may actually not have been processed. In this case we must |
4337 | | * read it otherwise stray unread IDAT data or, more likely, an IDAT chunk |
4338 | | * may still remain to be consumed. |
4339 | | */ |
4340 | 80.0k | if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0) |
4341 | 28.4k | { |
4342 | | /* The NULL causes png_read_IDAT_data to swallow any remaining bytes in |
4343 | | * the compressed stream, but the stream may be damaged too, so even after |
4344 | | * this call we may need to terminate the zstream ownership. |
4345 | | */ |
4346 | 28.4k | png_read_IDAT_data(png_ptr, NULL, 0); |
4347 | 28.4k | png_ptr->zstream.next_out = NULL; /* safety */ |
4348 | | |
4349 | | /* Now clear everything out for safety; the following may not have been |
4350 | | * done. |
4351 | | */ |
4352 | 28.4k | if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0) |
4353 | 25.2k | { |
4354 | 25.2k | png_ptr->mode |= PNG_AFTER_IDAT; |
4355 | 25.2k | png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED; |
4356 | 25.2k | } |
4357 | 28.4k | } |
4358 | | |
4359 | | /* If the zstream has not been released do it now *and* terminate the reading |
4360 | | * of the final IDAT chunk. |
4361 | | */ |
4362 | 80.0k | if (png_ptr->zowner == png_IDAT) |
4363 | 39.7k | { |
4364 | | /* Always do this; the pointers otherwise point into the read buffer. */ |
4365 | 39.7k | png_ptr->zstream.next_in = NULL; |
4366 | 39.7k | png_ptr->zstream.avail_in = 0; |
4367 | | |
4368 | | /* Now we no longer own the zstream. */ |
4369 | 39.7k | png_ptr->zowner = 0; |
4370 | | |
4371 | | /* The slightly weird semantics of the sequential IDAT reading is that we |
4372 | | * are always in or at the end of an IDAT chunk, so we always need to do a |
4373 | | * crc_finish here. If idat_size is non-zero we also need to read the |
4374 | | * spurious bytes at the end of the chunk now. |
4375 | | */ |
4376 | 39.7k | (void)png_crc_finish(png_ptr, png_ptr->idat_size); |
4377 | 39.7k | } |
4378 | 80.0k | } |
4379 | | |
4380 | | void /* PRIVATE */ |
4381 | | png_read_finish_row(png_structrp png_ptr) |
4382 | 3.29M | { |
4383 | 3.29M | png_debug(1, "in png_read_finish_row"); |
4384 | 3.29M | png_ptr->row_number++; |
4385 | 3.29M | if (png_ptr->row_number < png_ptr->num_rows) |
4386 | 3.10M | return; |
4387 | | |
4388 | 187k | if (png_ptr->interlaced != 0) |
4389 | 169k | { |
4390 | 169k | png_ptr->row_number = 0; |
4391 | | |
4392 | | /* TO DO: don't do this if prev_row isn't needed (requires |
4393 | | * read-ahead of the next row's filter byte. |
4394 | | */ |
4395 | 169k | memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1); |
4396 | | |
4397 | 169k | do |
4398 | 169k | { |
4399 | 169k | png_ptr->pass++; |
4400 | | |
4401 | 169k | if (png_ptr->pass >= 7) |
4402 | 23.2k | break; |
4403 | | |
4404 | 146k | png_ptr->iwidth = (png_ptr->width + |
4405 | 146k | png_pass_inc[png_ptr->pass] - 1 - |
4406 | 146k | png_pass_start[png_ptr->pass]) / |
4407 | 146k | png_pass_inc[png_ptr->pass]; |
4408 | | |
4409 | 146k | if ((png_ptr->transformations & PNG_INTERLACE) == 0) |
4410 | 0 | { |
4411 | 0 | png_ptr->num_rows = (png_ptr->height + |
4412 | 0 | png_pass_yinc[png_ptr->pass] - 1 - |
4413 | 0 | png_pass_ystart[png_ptr->pass]) / |
4414 | 0 | png_pass_yinc[png_ptr->pass]; |
4415 | 0 | } |
4416 | | |
4417 | 146k | else /* if (png_ptr->transformations & PNG_INTERLACE) */ |
4418 | 146k | break; /* libpng deinterlacing sees every row */ |
4419 | | |
4420 | 146k | } while (png_ptr->num_rows == 0 || png_ptr->iwidth == 0); |
4421 | | |
4422 | 169k | if (png_ptr->pass < 7) |
4423 | 146k | return; |
4424 | 169k | } |
4425 | | |
4426 | | /* Here after at the end of the last row of the last pass. */ |
4427 | 40.9k | png_read_finish_IDAT(png_ptr); |
4428 | 40.9k | } |
4429 | | #endif /* SEQUENTIAL_READ */ |
4430 | | |
4431 | | void /* PRIVATE */ |
4432 | | png_read_start_row(png_structrp png_ptr) |
4433 | 51.2k | { |
4434 | 51.2k | unsigned int max_pixel_depth; |
4435 | 51.2k | size_t row_bytes; |
4436 | | |
4437 | 51.2k | png_debug(1, "in png_read_start_row"); |
4438 | | |
4439 | 51.2k | #ifdef PNG_READ_TRANSFORMS_SUPPORTED |
4440 | 51.2k | png_init_read_transformations(png_ptr); |
4441 | 51.2k | #endif |
4442 | 51.2k | if (png_ptr->interlaced != 0) |
4443 | 27.8k | { |
4444 | 27.8k | if ((png_ptr->transformations & PNG_INTERLACE) == 0) |
4445 | 0 | png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 - |
4446 | 0 | png_pass_ystart[0]) / png_pass_yinc[0]; |
4447 | | |
4448 | 27.8k | else |
4449 | 27.8k | png_ptr->num_rows = png_ptr->height; |
4450 | | |
4451 | 27.8k | png_ptr->iwidth = (png_ptr->width + |
4452 | 27.8k | png_pass_inc[png_ptr->pass] - 1 - |
4453 | 27.8k | png_pass_start[png_ptr->pass]) / |
4454 | 27.8k | png_pass_inc[png_ptr->pass]; |
4455 | 27.8k | } |
4456 | | |
4457 | 23.4k | else |
4458 | 23.4k | { |
4459 | 23.4k | png_ptr->num_rows = png_ptr->height; |
4460 | 23.4k | png_ptr->iwidth = png_ptr->width; |
4461 | 23.4k | } |
4462 | | |
4463 | 51.2k | max_pixel_depth = (unsigned int)png_ptr->pixel_depth; |
4464 | | |
4465 | | /* WARNING: * png_read_transform_info (pngrtran.c) performs a simpler set of |
4466 | | * calculations to calculate the final pixel depth, then |
4467 | | * png_do_read_transforms actually does the transforms. This means that the |
4468 | | * code which effectively calculates this value is actually repeated in three |
4469 | | * separate places. They must all match. Innocent changes to the order of |
4470 | | * transformations can and will break libpng in a way that causes memory |
4471 | | * overwrites. |
4472 | | * |
4473 | | * TODO: fix this. |
4474 | | */ |
4475 | 51.2k | #ifdef PNG_READ_PACK_SUPPORTED |
4476 | 51.2k | if ((png_ptr->transformations & PNG_PACK) != 0 && png_ptr->bit_depth < 8) |
4477 | 32.0k | max_pixel_depth = 8; |
4478 | 51.2k | #endif |
4479 | | |
4480 | 51.2k | #ifdef PNG_READ_EXPAND_SUPPORTED |
4481 | 51.2k | if ((png_ptr->transformations & PNG_EXPAND) != 0) |
4482 | 0 | { |
4483 | 0 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
4484 | 0 | { |
4485 | 0 | if (png_ptr->num_trans != 0) |
4486 | 0 | max_pixel_depth = 32; |
4487 | | |
4488 | 0 | else |
4489 | 0 | max_pixel_depth = 24; |
4490 | 0 | } |
4491 | | |
4492 | 0 | else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) |
4493 | 0 | { |
4494 | 0 | if (max_pixel_depth < 8) |
4495 | 0 | max_pixel_depth = 8; |
4496 | |
|
4497 | 0 | if (png_ptr->num_trans != 0) |
4498 | 0 | max_pixel_depth *= 2; |
4499 | 0 | } |
4500 | | |
4501 | 0 | else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB) |
4502 | 0 | { |
4503 | 0 | if (png_ptr->num_trans != 0) |
4504 | 0 | { |
4505 | 0 | max_pixel_depth *= 4; |
4506 | 0 | max_pixel_depth /= 3; |
4507 | 0 | } |
4508 | 0 | } |
4509 | 0 | } |
4510 | 51.2k | #endif |
4511 | | |
4512 | 51.2k | #ifdef PNG_READ_EXPAND_16_SUPPORTED |
4513 | 51.2k | if ((png_ptr->transformations & PNG_EXPAND_16) != 0) |
4514 | 0 | { |
4515 | 0 | # ifdef PNG_READ_EXPAND_SUPPORTED |
4516 | | /* In fact it is an error if it isn't supported, but checking is |
4517 | | * the safe way. |
4518 | | */ |
4519 | 0 | if ((png_ptr->transformations & PNG_EXPAND) != 0) |
4520 | 0 | { |
4521 | 0 | if (png_ptr->bit_depth < 16) |
4522 | 0 | max_pixel_depth *= 2; |
4523 | 0 | } |
4524 | 0 | else |
4525 | 0 | # endif |
4526 | 0 | png_ptr->transformations &= ~PNG_EXPAND_16; |
4527 | 0 | } |
4528 | 51.2k | #endif |
4529 | | |
4530 | 51.2k | #ifdef PNG_READ_FILLER_SUPPORTED |
4531 | 51.2k | if ((png_ptr->transformations & (PNG_FILLER)) != 0) |
4532 | 0 | { |
4533 | 0 | if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) |
4534 | 0 | { |
4535 | 0 | if (max_pixel_depth <= 8) |
4536 | 0 | max_pixel_depth = 16; |
4537 | | |
4538 | 0 | else |
4539 | 0 | max_pixel_depth = 32; |
4540 | 0 | } |
4541 | | |
4542 | 0 | else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB || |
4543 | 0 | png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
4544 | 0 | { |
4545 | 0 | if (max_pixel_depth <= 32) |
4546 | 0 | max_pixel_depth = 32; |
4547 | | |
4548 | 0 | else |
4549 | 0 | max_pixel_depth = 64; |
4550 | 0 | } |
4551 | 0 | } |
4552 | 51.2k | #endif |
4553 | | |
4554 | 51.2k | #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED |
4555 | 51.2k | if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0) |
4556 | 0 | { |
4557 | 0 | if ( |
4558 | 0 | #ifdef PNG_READ_EXPAND_SUPPORTED |
4559 | 0 | (png_ptr->num_trans != 0 && |
4560 | 0 | (png_ptr->transformations & PNG_EXPAND) != 0) || |
4561 | 0 | #endif |
4562 | 0 | #ifdef PNG_READ_FILLER_SUPPORTED |
4563 | 0 | (png_ptr->transformations & (PNG_FILLER)) != 0 || |
4564 | 0 | #endif |
4565 | 0 | png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
4566 | 0 | { |
4567 | 0 | if (max_pixel_depth <= 16) |
4568 | 0 | max_pixel_depth = 32; |
4569 | | |
4570 | 0 | else |
4571 | 0 | max_pixel_depth = 64; |
4572 | 0 | } |
4573 | | |
4574 | 0 | else |
4575 | 0 | { |
4576 | 0 | if (max_pixel_depth <= 8) |
4577 | 0 | { |
4578 | 0 | if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
4579 | 0 | max_pixel_depth = 32; |
4580 | | |
4581 | 0 | else |
4582 | 0 | max_pixel_depth = 24; |
4583 | 0 | } |
4584 | | |
4585 | 0 | else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
4586 | 0 | max_pixel_depth = 64; |
4587 | | |
4588 | 0 | else |
4589 | 0 | max_pixel_depth = 48; |
4590 | 0 | } |
4591 | 0 | } |
4592 | 51.2k | #endif |
4593 | | |
4594 | 51.2k | #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \ |
4595 | 51.2k | defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) |
4596 | 51.2k | if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0) |
4597 | 0 | { |
4598 | 0 | unsigned int user_pixel_depth = png_ptr->user_transform_depth * |
4599 | 0 | png_ptr->user_transform_channels; |
4600 | |
|
4601 | 0 | if (user_pixel_depth > max_pixel_depth) |
4602 | 0 | max_pixel_depth = user_pixel_depth; |
4603 | 0 | } |
4604 | 51.2k | #endif |
4605 | | |
4606 | | /* This value is stored in png_struct and double checked in the row read |
4607 | | * code. |
4608 | | */ |
4609 | 51.2k | png_ptr->maximum_pixel_depth = (png_byte)max_pixel_depth; |
4610 | 51.2k | png_ptr->transformed_pixel_depth = 0; /* calculated on demand */ |
4611 | | |
4612 | | /* Align the width on the next larger 8 pixels. Mainly used |
4613 | | * for interlacing |
4614 | | */ |
4615 | 51.2k | row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7)); |
4616 | | /* Calculate the maximum bytes needed, adding a byte and a pixel |
4617 | | * for safety's sake |
4618 | | */ |
4619 | 51.2k | row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) + |
4620 | 51.2k | 1 + ((max_pixel_depth + 7) >> 3U); |
4621 | | |
4622 | | #ifdef PNG_MAX_MALLOC_64K |
4623 | | if (row_bytes > (png_uint_32)65536L) |
4624 | | png_error(png_ptr, "This image requires a row greater than 64KB"); |
4625 | | #endif |
4626 | | |
4627 | 51.2k | if (row_bytes + 48 > png_ptr->old_big_row_buf_size) |
4628 | 51.2k | { |
4629 | 51.2k | png_free(png_ptr, png_ptr->big_row_buf); |
4630 | 51.2k | png_free(png_ptr, png_ptr->big_prev_row); |
4631 | | |
4632 | 51.2k | if (png_ptr->interlaced != 0) |
4633 | 27.8k | png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr, |
4634 | 27.8k | row_bytes + 48); |
4635 | | |
4636 | 23.4k | else |
4637 | 23.4k | png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes + 48); |
4638 | | |
4639 | 51.2k | png_ptr->big_prev_row = (png_bytep)png_malloc(png_ptr, row_bytes + 48); |
4640 | | |
4641 | 51.2k | #ifdef PNG_ALIGNED_MEMORY_SUPPORTED |
4642 | | /* Use 16-byte aligned memory for row_buf with at least 16 bytes |
4643 | | * of padding before and after row_buf; treat prev_row similarly. |
4644 | | * NOTE: the alignment is to the start of the pixels, one beyond the start |
4645 | | * of the buffer, because of the filter byte. Prior to libpng 1.5.6 this |
4646 | | * was incorrect; the filter byte was aligned, which had the exact |
4647 | | * opposite effect of that intended. |
4648 | | */ |
4649 | 51.2k | { |
4650 | 51.2k | png_bytep temp = png_ptr->big_row_buf + 32; |
4651 | 51.2k | size_t extra = (size_t)temp & 0x0f; |
4652 | 51.2k | png_ptr->row_buf = temp - extra - 1/*filter byte*/; |
4653 | | |
4654 | 51.2k | temp = png_ptr->big_prev_row + 32; |
4655 | 51.2k | extra = (size_t)temp & 0x0f; |
4656 | 51.2k | png_ptr->prev_row = temp - extra - 1/*filter byte*/; |
4657 | 51.2k | } |
4658 | | #else |
4659 | | /* Use 31 bytes of padding before and 17 bytes after row_buf. */ |
4660 | | png_ptr->row_buf = png_ptr->big_row_buf + 31; |
4661 | | png_ptr->prev_row = png_ptr->big_prev_row + 31; |
4662 | | #endif |
4663 | 51.2k | png_ptr->old_big_row_buf_size = row_bytes + 48; |
4664 | 51.2k | } |
4665 | | |
4666 | | #ifdef PNG_MAX_MALLOC_64K |
4667 | | if (png_ptr->rowbytes > 65535) |
4668 | | png_error(png_ptr, "This image requires a row greater than 64KB"); |
4669 | | |
4670 | | #endif |
4671 | 51.2k | if (png_ptr->rowbytes > (PNG_SIZE_MAX - 1)) |
4672 | 0 | png_error(png_ptr, "Row has too many bytes to allocate in memory"); |
4673 | | |
4674 | 51.2k | memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1); |
4675 | | |
4676 | 51.2k | png_debug1(3, "width = %u,", png_ptr->width); |
4677 | 51.2k | png_debug1(3, "height = %u,", png_ptr->height); |
4678 | 51.2k | png_debug1(3, "iwidth = %u,", png_ptr->iwidth); |
4679 | 51.2k | png_debug1(3, "num_rows = %u,", png_ptr->num_rows); |
4680 | 51.2k | png_debug1(3, "rowbytes = %lu,", (unsigned long)png_ptr->rowbytes); |
4681 | 51.2k | png_debug1(3, "irowbytes = %lu", |
4682 | 51.2k | (unsigned long)PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1); |
4683 | | |
4684 | | /* The sequential reader needs a buffer for IDAT, but the progressive reader |
4685 | | * does not, so free the read buffer now regardless; the sequential reader |
4686 | | * reallocates it on demand. |
4687 | | */ |
4688 | 51.2k | if (png_ptr->read_buffer != NULL) |
4689 | 2.13k | { |
4690 | 2.13k | png_bytep buffer = png_ptr->read_buffer; |
4691 | | |
4692 | 2.13k | png_ptr->read_buffer_size = 0; |
4693 | 2.13k | png_ptr->read_buffer = NULL; |
4694 | 2.13k | png_free(png_ptr, buffer); |
4695 | 2.13k | } |
4696 | | |
4697 | | /* Finally claim the zstream for the inflate of the IDAT data, use the bits |
4698 | | * value from the stream (note that this will result in a fatal error if the |
4699 | | * IDAT stream has a bogus deflate header window_bits value, but this should |
4700 | | * not be happening any longer!) |
4701 | | */ |
4702 | 51.2k | if (png_inflate_claim(png_ptr, png_IDAT) != Z_OK) |
4703 | 0 | png_error(png_ptr, png_ptr->zstream.msg); |
4704 | | |
4705 | 51.2k | png_ptr->flags |= PNG_FLAG_ROW_INIT; |
4706 | 51.2k | } |
4707 | | #endif /* READ */ |