Backed out changeset 06f41c22f3a6 (bug 1888460) for causing linux xpcshell failures...
[gecko.git] / media / libpng / pngrutil.c
blob7372a98219ebf72c61831a920f74ab80ab58c069
2 /* pngrutil.c - utilities to read a PNG file
4 * Copyright (c) 2018-2024 Cosmin Truta
5 * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson
6 * Copyright (c) 1996-1997 Andreas Dilger
7 * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.
9 * This code is released under the libpng license.
10 * For conditions of distribution and use, see the disclaimer
11 * and license in png.h
13 * This file contains routines that are only called from within
14 * libpng itself during the course of reading an image.
17 #include "pngpriv.h"
19 #ifdef PNG_READ_SUPPORTED
21 png_uint_32 PNGAPI
22 png_get_uint_31(png_const_structrp png_ptr, png_const_bytep buf)
24 png_uint_32 uval = png_get_uint_32(buf);
26 if (uval > PNG_UINT_31_MAX)
27 png_error(png_ptr, "PNG unsigned integer out of range");
29 return uval;
32 #if defined(PNG_READ_gAMA_SUPPORTED) || defined(PNG_READ_cHRM_SUPPORTED)
33 /* The following is a variation on the above for use with the fixed
34 * point values used for gAMA and cHRM. Instead of png_error it
35 * issues a warning and returns (-1) - an invalid value because both
36 * gAMA and cHRM use *unsigned* integers for fixed point values.
38 #define PNG_FIXED_ERROR (-1)
40 static png_fixed_point /* PRIVATE */
41 png_get_fixed_point(png_structrp png_ptr, png_const_bytep buf)
43 png_uint_32 uval = png_get_uint_32(buf);
45 if (uval <= PNG_UINT_31_MAX)
46 return (png_fixed_point)uval; /* known to be in range */
48 /* The caller can turn off the warning by passing NULL. */
49 if (png_ptr != NULL)
50 png_warning(png_ptr, "PNG fixed point integer out of range");
52 return PNG_FIXED_ERROR;
54 #endif
56 #ifdef PNG_READ_INT_FUNCTIONS_SUPPORTED
57 /* NOTE: the read macros will obscure these definitions, so that if
58 * PNG_USE_READ_MACROS is set the library will not use them internally,
59 * but the APIs will still be available externally.
61 * The parentheses around "PNGAPI function_name" in the following three
62 * functions are necessary because they allow the macros to co-exist with
63 * these (unused but exported) functions.
66 /* Grab an unsigned 32-bit integer from a buffer in big-endian format. */
67 png_uint_32 (PNGAPI
68 png_get_uint_32)(png_const_bytep buf)
70 png_uint_32 uval =
71 ((png_uint_32)(*(buf )) << 24) +
72 ((png_uint_32)(*(buf + 1)) << 16) +
73 ((png_uint_32)(*(buf + 2)) << 8) +
74 ((png_uint_32)(*(buf + 3)) ) ;
76 return uval;
79 /* Grab a signed 32-bit integer from a buffer in big-endian format. The
80 * data is stored in the PNG file in two's complement format and there
81 * is no guarantee that a 'png_int_32' is exactly 32 bits, therefore
82 * the following code does a two's complement to native conversion.
84 png_int_32 (PNGAPI
85 png_get_int_32)(png_const_bytep buf)
87 png_uint_32 uval = png_get_uint_32(buf);
88 if ((uval & 0x80000000) == 0) /* non-negative */
89 return (png_int_32)uval;
91 uval = (uval ^ 0xffffffff) + 1; /* 2's complement: -x = ~x+1 */
92 if ((uval & 0x80000000) == 0) /* no overflow */
93 return -(png_int_32)uval;
94 /* The following has to be safe; this function only gets called on PNG data
95 * and if we get here that data is invalid. 0 is the most safe value and
96 * if not then an attacker would surely just generate a PNG with 0 instead.
98 return 0;
101 /* Grab an unsigned 16-bit integer from a buffer in big-endian format. */
102 png_uint_16 (PNGAPI
103 png_get_uint_16)(png_const_bytep buf)
105 /* ANSI-C requires an int value to accommodate at least 16 bits so this
106 * works and allows the compiler not to worry about possible narrowing
107 * on 32-bit systems. (Pre-ANSI systems did not make integers smaller
108 * than 16 bits either.)
110 unsigned int val =
111 ((unsigned int)(*buf) << 8) +
112 ((unsigned int)(*(buf + 1)));
114 return (png_uint_16)val;
117 #endif /* READ_INT_FUNCTIONS */
119 /* Read and check the PNG file signature */
120 void /* PRIVATE */
121 png_read_sig(png_structrp png_ptr, png_inforp info_ptr)
123 size_t num_checked, num_to_check;
125 /* Exit if the user application does not expect a signature. */
126 if (png_ptr->sig_bytes >= 8)
127 return;
129 num_checked = png_ptr->sig_bytes;
130 num_to_check = 8 - num_checked;
132 #ifdef PNG_IO_STATE_SUPPORTED
133 png_ptr->io_state = PNG_IO_READING | PNG_IO_SIGNATURE;
134 #endif
136 /* The signature must be serialized in a single I/O call. */
137 png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
138 png_ptr->sig_bytes = 8;
140 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check) != 0)
142 if (num_checked < 4 &&
143 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4) != 0)
144 png_error(png_ptr, "Not a PNG file");
145 else
146 png_error(png_ptr, "PNG file corrupted by ASCII conversion");
148 if (num_checked < 3)
149 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
152 /* Read the chunk header (length + type name).
153 * Put the type name into png_ptr->chunk_name, and return the length.
155 png_uint_32 /* PRIVATE */
156 png_read_chunk_header(png_structrp png_ptr)
158 png_byte buf[8];
159 png_uint_32 length;
161 #ifdef PNG_IO_STATE_SUPPORTED
162 png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_HDR;
163 #endif
165 /* Read the length and the chunk name.
166 * This must be performed in a single I/O call.
168 png_read_data(png_ptr, buf, 8);
169 length = png_get_uint_31(png_ptr, buf);
171 /* Put the chunk name into png_ptr->chunk_name. */
172 png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(buf+4);
174 png_debug2(0, "Reading chunk typeid = 0x%lx, length = %lu",
175 (unsigned long)png_ptr->chunk_name, (unsigned long)length);
177 /* Reset the crc and run it over the chunk name. */
178 png_reset_crc(png_ptr);
179 png_calculate_crc(png_ptr, buf + 4, 4);
181 /* Check to see if chunk name is valid. */
182 png_check_chunk_name(png_ptr, png_ptr->chunk_name);
184 /* Check for too-large chunk length */
185 png_check_chunk_length(png_ptr, length);
187 #ifdef PNG_IO_STATE_SUPPORTED
188 png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_DATA;
189 #endif
191 return length;
194 /* Read data, and (optionally) run it through the CRC. */
195 void /* PRIVATE */
196 png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length)
198 if (png_ptr == NULL)
199 return;
201 png_read_data(png_ptr, buf, length);
202 png_calculate_crc(png_ptr, buf, length);
205 /* Optionally skip data and then check the CRC. Depending on whether we
206 * are reading an ancillary or critical chunk, and how the program has set
207 * things up, we may calculate the CRC on the data and print a message.
208 * Returns '1' if there was a CRC error, '0' otherwise.
210 int /* PRIVATE */
211 png_crc_finish(png_structrp png_ptr, png_uint_32 skip)
213 /* The size of the local buffer for inflate is a good guess as to a
214 * reasonable size to use for buffering reads from the application.
216 while (skip > 0)
218 png_uint_32 len;
219 png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];
221 len = (sizeof tmpbuf);
222 if (len > skip)
223 len = skip;
224 skip -= len;
226 png_crc_read(png_ptr, tmpbuf, len);
229 if (png_crc_error(png_ptr) != 0)
231 if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) != 0 ?
232 (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) == 0 :
233 (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE) != 0)
235 png_chunk_warning(png_ptr, "CRC error");
238 else
239 png_chunk_error(png_ptr, "CRC error");
241 return 1;
244 return 0;
247 /* Compare the CRC stored in the PNG file with that calculated by libpng from
248 * the data it has read thus far.
250 int /* PRIVATE */
251 png_crc_error(png_structrp png_ptr)
253 png_byte crc_bytes[4];
254 png_uint_32 crc;
255 int need_crc = 1;
257 if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) != 0)
259 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
260 (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
261 need_crc = 0;
264 else /* critical */
266 if ((png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) != 0)
267 need_crc = 0;
270 #ifdef PNG_IO_STATE_SUPPORTED
271 png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_CRC;
272 #endif
274 /* The chunk CRC must be serialized in a single I/O call. */
275 png_read_data(png_ptr, crc_bytes, 4);
277 if (need_crc != 0)
279 crc = png_get_uint_32(crc_bytes);
280 return crc != png_ptr->crc;
283 else
284 return 0;
287 #if defined(PNG_READ_iCCP_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) ||\
288 defined(PNG_READ_pCAL_SUPPORTED) || defined(PNG_READ_sCAL_SUPPORTED) ||\
289 defined(PNG_READ_sPLT_SUPPORTED) || defined(PNG_READ_tEXt_SUPPORTED) ||\
290 defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_SEQUENTIAL_READ_SUPPORTED)
291 /* Manage the read buffer; this simply reallocates the buffer if it is not small
292 * enough (or if it is not allocated). The routine returns a pointer to the
293 * buffer; if an error occurs and 'warn' is set the routine returns NULL, else
294 * it will call png_error (via png_malloc) on failure. (warn == 2 means
295 * 'silent').
297 static png_bytep
298 png_read_buffer(png_structrp png_ptr, png_alloc_size_t new_size, int warn)
300 png_bytep buffer = png_ptr->read_buffer;
302 if (buffer != NULL && new_size > png_ptr->read_buffer_size)
304 png_ptr->read_buffer = NULL;
305 png_ptr->read_buffer_size = 0;
306 png_free(png_ptr, buffer);
307 buffer = NULL;
310 if (buffer == NULL)
312 buffer = png_voidcast(png_bytep, png_malloc_base(png_ptr, new_size));
314 if (buffer != NULL)
316 memset(buffer, 0, new_size); /* just in case */
317 png_ptr->read_buffer = buffer;
318 png_ptr->read_buffer_size = new_size;
321 else if (warn < 2) /* else silent */
323 if (warn != 0)
324 png_chunk_warning(png_ptr, "insufficient memory to read chunk");
326 else
327 png_chunk_error(png_ptr, "insufficient memory to read chunk");
331 return buffer;
333 #endif /* READ_iCCP|iTXt|pCAL|sCAL|sPLT|tEXt|zTXt|SEQUENTIAL_READ */
335 /* png_inflate_claim: claim the zstream for some nefarious purpose that involves
336 * decompression. Returns Z_OK on success, else a zlib error code. It checks
337 * the owner but, in final release builds, just issues a warning if some other
338 * chunk apparently owns the stream. Prior to release it does a png_error.
340 static int
341 png_inflate_claim(png_structrp png_ptr, png_uint_32 owner)
343 if (png_ptr->zowner != 0)
345 char msg[64];
347 PNG_STRING_FROM_CHUNK(msg, png_ptr->zowner);
348 /* So the message that results is "<chunk> using zstream"; this is an
349 * internal error, but is very useful for debugging. i18n requirements
350 * are minimal.
352 (void)png_safecat(msg, (sizeof msg), 4, " using zstream");
353 #if PNG_RELEASE_BUILD
354 png_chunk_warning(png_ptr, msg);
355 png_ptr->zowner = 0;
356 #else
357 png_chunk_error(png_ptr, msg);
358 #endif
361 /* Implementation note: unlike 'png_deflate_claim' this internal function
362 * does not take the size of the data as an argument. Some efficiency could
363 * be gained by using this when it is known *if* the zlib stream itself does
364 * not record the number; however, this is an illusion: the original writer
365 * of the PNG may have selected a lower window size, and we really must
366 * follow that because, for systems with with limited capabilities, we
367 * would otherwise reject the application's attempts to use a smaller window
368 * size (zlib doesn't have an interface to say "this or lower"!).
370 * inflateReset2 was added to zlib 1.2.4; before this the window could not be
371 * reset, therefore it is necessary to always allocate the maximum window
372 * size with earlier zlibs just in case later compressed chunks need it.
375 int ret; /* zlib return code */
376 #if ZLIB_VERNUM >= 0x1240
377 int window_bits = 0;
379 # if defined(PNG_SET_OPTION_SUPPORTED) && defined(PNG_MAXIMUM_INFLATE_WINDOW)
380 if (((png_ptr->options >> PNG_MAXIMUM_INFLATE_WINDOW) & 3) ==
381 PNG_OPTION_ON)
383 window_bits = 15;
384 png_ptr->zstream_start = 0; /* fixed window size */
387 else
389 png_ptr->zstream_start = 1;
391 # endif
393 #endif /* ZLIB_VERNUM >= 0x1240 */
395 /* Set this for safety, just in case the previous owner left pointers to
396 * memory allocations.
398 png_ptr->zstream.next_in = NULL;
399 png_ptr->zstream.avail_in = 0;
400 png_ptr->zstream.next_out = NULL;
401 png_ptr->zstream.avail_out = 0;
403 if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0)
405 #if ZLIB_VERNUM >= 0x1240
406 ret = inflateReset2(&png_ptr->zstream, window_bits);
407 #else
408 ret = inflateReset(&png_ptr->zstream);
409 #endif
412 else
414 #if ZLIB_VERNUM >= 0x1240
415 ret = inflateInit2(&png_ptr->zstream, window_bits);
416 #else
417 ret = inflateInit(&png_ptr->zstream);
418 #endif
420 if (ret == Z_OK)
421 png_ptr->flags |= PNG_FLAG_ZSTREAM_INITIALIZED;
424 #ifdef PNG_DISABLE_ADLER32_CHECK_SUPPORTED
425 if (((png_ptr->options >> PNG_IGNORE_ADLER32) & 3) == PNG_OPTION_ON)
426 /* Turn off validation of the ADLER32 checksum in IDAT chunks */
427 ret = inflateValidate(&png_ptr->zstream, 0);
428 #endif
430 if (ret == Z_OK)
431 png_ptr->zowner = owner;
433 else
434 png_zstream_error(png_ptr, ret);
436 return ret;
439 #ifdef window_bits
440 # undef window_bits
441 #endif
444 #if ZLIB_VERNUM >= 0x1240
445 /* Handle the start of the inflate stream if we called inflateInit2(strm,0);
446 * in this case some zlib versions skip validation of the CINFO field and, in
447 * certain circumstances, libpng may end up displaying an invalid image, in
448 * contrast to implementations that call zlib in the normal way (e.g. libpng
449 * 1.5).
451 int /* PRIVATE */
452 png_zlib_inflate(png_structrp png_ptr, int flush)
454 if (png_ptr->zstream_start && png_ptr->zstream.avail_in > 0)
456 if ((*png_ptr->zstream.next_in >> 4) > 7)
458 png_ptr->zstream.msg = "invalid window size (libpng)";
459 return Z_DATA_ERROR;
462 png_ptr->zstream_start = 0;
465 return inflate(&png_ptr->zstream, flush);
467 #endif /* Zlib >= 1.2.4 */
469 #ifdef PNG_READ_COMPRESSED_TEXT_SUPPORTED
470 #if defined(PNG_READ_zTXt_SUPPORTED) || defined (PNG_READ_iTXt_SUPPORTED)
471 /* png_inflate now returns zlib error codes including Z_OK and Z_STREAM_END to
472 * allow the caller to do multiple calls if required. If the 'finish' flag is
473 * set Z_FINISH will be passed to the final inflate() call and Z_STREAM_END must
474 * be returned or there has been a problem, otherwise Z_SYNC_FLUSH is used and
475 * Z_OK or Z_STREAM_END will be returned on success.
477 * The input and output sizes are updated to the actual amounts of data consumed
478 * or written, not the amount available (as in a z_stream). The data pointers
479 * are not changed, so the next input is (data+input_size) and the next
480 * available output is (output+output_size).
482 static int
483 png_inflate(png_structrp png_ptr, png_uint_32 owner, int finish,
484 /* INPUT: */ png_const_bytep input, png_uint_32p input_size_ptr,
485 /* OUTPUT: */ png_bytep output, png_alloc_size_t *output_size_ptr)
487 if (png_ptr->zowner == owner) /* Else not claimed */
489 int ret;
490 png_alloc_size_t avail_out = *output_size_ptr;
491 png_uint_32 avail_in = *input_size_ptr;
493 /* zlib can't necessarily handle more than 65535 bytes at once (i.e. it
494 * can't even necessarily handle 65536 bytes) because the type uInt is
495 * "16 bits or more". Consequently it is necessary to chunk the input to
496 * zlib. This code uses ZLIB_IO_MAX, from pngpriv.h, as the maximum (the
497 * maximum value that can be stored in a uInt.) It is possible to set
498 * ZLIB_IO_MAX to a lower value in pngpriv.h and this may sometimes have
499 * a performance advantage, because it reduces the amount of data accessed
500 * at each step and that may give the OS more time to page it in.
502 png_ptr->zstream.next_in = PNGZ_INPUT_CAST(input);
503 /* avail_in and avail_out are set below from 'size' */
504 png_ptr->zstream.avail_in = 0;
505 png_ptr->zstream.avail_out = 0;
507 /* Read directly into the output if it is available (this is set to
508 * a local buffer below if output is NULL).
510 if (output != NULL)
511 png_ptr->zstream.next_out = output;
515 uInt avail;
516 Byte local_buffer[PNG_INFLATE_BUF_SIZE];
518 /* zlib INPUT BUFFER */
519 /* The setting of 'avail_in' used to be outside the loop; by setting it
520 * inside it is possible to chunk the input to zlib and simply rely on
521 * zlib to advance the 'next_in' pointer. This allows arbitrary
522 * amounts of data to be passed through zlib at the unavoidable cost of
523 * requiring a window save (memcpy of up to 32768 output bytes)
524 * every ZLIB_IO_MAX input bytes.
526 avail_in += png_ptr->zstream.avail_in; /* not consumed last time */
528 avail = ZLIB_IO_MAX;
530 if (avail_in < avail)
531 avail = (uInt)avail_in; /* safe: < than ZLIB_IO_MAX */
533 avail_in -= avail;
534 png_ptr->zstream.avail_in = avail;
536 /* zlib OUTPUT BUFFER */
537 avail_out += png_ptr->zstream.avail_out; /* not written last time */
539 avail = ZLIB_IO_MAX; /* maximum zlib can process */
541 if (output == NULL)
543 /* Reset the output buffer each time round if output is NULL and
544 * make available the full buffer, up to 'remaining_space'
546 png_ptr->zstream.next_out = local_buffer;
547 if ((sizeof local_buffer) < avail)
548 avail = (sizeof local_buffer);
551 if (avail_out < avail)
552 avail = (uInt)avail_out; /* safe: < ZLIB_IO_MAX */
554 png_ptr->zstream.avail_out = avail;
555 avail_out -= avail;
557 /* zlib inflate call */
558 /* In fact 'avail_out' may be 0 at this point, that happens at the end
559 * of the read when the final LZ end code was not passed at the end of
560 * the previous chunk of input data. Tell zlib if we have reached the
561 * end of the output buffer.
563 ret = PNG_INFLATE(png_ptr, avail_out > 0 ? Z_NO_FLUSH :
564 (finish ? Z_FINISH : Z_SYNC_FLUSH));
565 } while (ret == Z_OK);
567 /* For safety kill the local buffer pointer now */
568 if (output == NULL)
569 png_ptr->zstream.next_out = NULL;
571 /* Claw back the 'size' and 'remaining_space' byte counts. */
572 avail_in += png_ptr->zstream.avail_in;
573 avail_out += png_ptr->zstream.avail_out;
575 /* Update the input and output sizes; the updated values are the amount
576 * consumed or written, effectively the inverse of what zlib uses.
578 if (avail_out > 0)
579 *output_size_ptr -= avail_out;
581 if (avail_in > 0)
582 *input_size_ptr -= avail_in;
584 /* Ensure png_ptr->zstream.msg is set (even in the success case!) */
585 png_zstream_error(png_ptr, ret);
586 return ret;
589 else
591 /* This is a bad internal error. The recovery assigns to the zstream msg
592 * pointer, which is not owned by the caller, but this is safe; it's only
593 * used on errors!
595 png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed");
596 return Z_STREAM_ERROR;
601 * Decompress trailing data in a chunk. The assumption is that read_buffer
602 * points at an allocated area holding the contents of a chunk with a
603 * trailing compressed part. What we get back is an allocated area
604 * holding the original prefix part and an uncompressed version of the
605 * trailing part (the malloc area passed in is freed).
607 static int
608 png_decompress_chunk(png_structrp png_ptr,
609 png_uint_32 chunklength, png_uint_32 prefix_size,
610 png_alloc_size_t *newlength /* must be initialized to the maximum! */,
611 int terminate /*add a '\0' to the end of the uncompressed data*/)
613 /* TODO: implement different limits for different types of chunk.
615 * The caller supplies *newlength set to the maximum length of the
616 * uncompressed data, but this routine allocates space for the prefix and
617 * maybe a '\0' terminator too. We have to assume that 'prefix_size' is
618 * limited only by the maximum chunk size.
620 png_alloc_size_t limit = PNG_SIZE_MAX;
622 # ifdef PNG_SET_USER_LIMITS_SUPPORTED
623 if (png_ptr->user_chunk_malloc_max > 0 &&
624 png_ptr->user_chunk_malloc_max < limit)
625 limit = png_ptr->user_chunk_malloc_max;
626 # elif PNG_USER_CHUNK_MALLOC_MAX > 0
627 if (PNG_USER_CHUNK_MALLOC_MAX < limit)
628 limit = PNG_USER_CHUNK_MALLOC_MAX;
629 # endif
631 if (limit >= prefix_size + (terminate != 0))
633 int ret;
635 limit -= prefix_size + (terminate != 0);
637 if (limit < *newlength)
638 *newlength = limit;
640 /* Now try to claim the stream. */
641 ret = png_inflate_claim(png_ptr, png_ptr->chunk_name);
643 if (ret == Z_OK)
645 png_uint_32 lzsize = chunklength - prefix_size;
647 ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/,
648 /* input: */ png_ptr->read_buffer + prefix_size, &lzsize,
649 /* output: */ NULL, newlength);
651 if (ret == Z_STREAM_END)
653 /* Use 'inflateReset' here, not 'inflateReset2' because this
654 * preserves the previously decided window size (otherwise it would
655 * be necessary to store the previous window size.) In practice
656 * this doesn't matter anyway, because png_inflate will call inflate
657 * with Z_FINISH in almost all cases, so the window will not be
658 * maintained.
660 if (inflateReset(&png_ptr->zstream) == Z_OK)
662 /* Because of the limit checks above we know that the new,
663 * expanded, size will fit in a size_t (let alone an
664 * png_alloc_size_t). Use png_malloc_base here to avoid an
665 * extra OOM message.
667 png_alloc_size_t new_size = *newlength;
668 png_alloc_size_t buffer_size = prefix_size + new_size +
669 (terminate != 0);
670 png_bytep text = png_voidcast(png_bytep, png_malloc_base(png_ptr,
671 buffer_size));
673 if (text != NULL)
675 memset(text, 0, buffer_size);
677 ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/,
678 png_ptr->read_buffer + prefix_size, &lzsize,
679 text + prefix_size, newlength);
681 if (ret == Z_STREAM_END)
683 if (new_size == *newlength)
685 if (terminate != 0)
686 text[prefix_size + *newlength] = 0;
688 if (prefix_size > 0)
689 memcpy(text, png_ptr->read_buffer, prefix_size);
692 png_bytep old_ptr = png_ptr->read_buffer;
694 png_ptr->read_buffer = text;
695 png_ptr->read_buffer_size = buffer_size;
696 text = old_ptr; /* freed below */
700 else
702 /* The size changed on the second read, there can be no
703 * guarantee that anything is correct at this point.
704 * The 'msg' pointer has been set to "unexpected end of
705 * LZ stream", which is fine, but return an error code
706 * that the caller won't accept.
708 ret = PNG_UNEXPECTED_ZLIB_RETURN;
712 else if (ret == Z_OK)
713 ret = PNG_UNEXPECTED_ZLIB_RETURN; /* for safety */
715 /* Free the text pointer (this is the old read_buffer on
716 * success)
718 png_free(png_ptr, text);
720 /* This really is very benign, but it's still an error because
721 * the extra space may otherwise be used as a Trojan Horse.
723 if (ret == Z_STREAM_END &&
724 chunklength - prefix_size != lzsize)
725 png_chunk_benign_error(png_ptr, "extra compressed data");
728 else
730 /* Out of memory allocating the buffer */
731 ret = Z_MEM_ERROR;
732 png_zstream_error(png_ptr, Z_MEM_ERROR);
736 else
738 /* inflateReset failed, store the error message */
739 png_zstream_error(png_ptr, ret);
740 ret = PNG_UNEXPECTED_ZLIB_RETURN;
744 else if (ret == Z_OK)
745 ret = PNG_UNEXPECTED_ZLIB_RETURN;
747 /* Release the claimed stream */
748 png_ptr->zowner = 0;
751 else /* the claim failed */ if (ret == Z_STREAM_END) /* impossible! */
752 ret = PNG_UNEXPECTED_ZLIB_RETURN;
754 return ret;
757 else
759 /* Application/configuration limits exceeded */
760 png_zstream_error(png_ptr, Z_MEM_ERROR);
761 return Z_MEM_ERROR;
764 #endif /* READ_zTXt || READ_iTXt */
765 #endif /* READ_COMPRESSED_TEXT */
767 #ifdef PNG_READ_iCCP_SUPPORTED
768 /* Perform a partial read and decompress, producing 'avail_out' bytes and
769 * reading from the current chunk as required.
771 static int
772 png_inflate_read(png_structrp png_ptr, png_bytep read_buffer, uInt read_size,
773 png_uint_32p chunk_bytes, png_bytep next_out, png_alloc_size_t *out_size,
774 int finish)
776 if (png_ptr->zowner == png_ptr->chunk_name)
778 int ret;
780 /* next_in and avail_in must have been initialized by the caller. */
781 png_ptr->zstream.next_out = next_out;
782 png_ptr->zstream.avail_out = 0; /* set in the loop */
786 if (png_ptr->zstream.avail_in == 0)
788 if (read_size > *chunk_bytes)
789 read_size = (uInt)*chunk_bytes;
790 *chunk_bytes -= read_size;
792 if (read_size > 0)
793 png_crc_read(png_ptr, read_buffer, read_size);
795 png_ptr->zstream.next_in = read_buffer;
796 png_ptr->zstream.avail_in = read_size;
799 if (png_ptr->zstream.avail_out == 0)
801 uInt avail = ZLIB_IO_MAX;
802 if (avail > *out_size)
803 avail = (uInt)*out_size;
804 *out_size -= avail;
806 png_ptr->zstream.avail_out = avail;
809 /* Use Z_SYNC_FLUSH when there is no more chunk data to ensure that all
810 * the available output is produced; this allows reading of truncated
811 * streams.
813 ret = PNG_INFLATE(png_ptr, *chunk_bytes > 0 ?
814 Z_NO_FLUSH : (finish ? Z_FINISH : Z_SYNC_FLUSH));
816 while (ret == Z_OK && (*out_size > 0 || png_ptr->zstream.avail_out > 0));
818 *out_size += png_ptr->zstream.avail_out;
819 png_ptr->zstream.avail_out = 0; /* Should not be required, but is safe */
821 /* Ensure the error message pointer is always set: */
822 png_zstream_error(png_ptr, ret);
823 return ret;
826 else
828 png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed");
829 return Z_STREAM_ERROR;
832 #endif /* READ_iCCP */
834 /* Read and check the IDHR chunk */
836 void /* PRIVATE */
837 png_handle_IHDR(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
839 png_byte buf[13];
840 png_uint_32 width, height;
841 int bit_depth, color_type, compression_type, filter_type;
842 int interlace_type;
844 png_debug(1, "in png_handle_IHDR");
846 if ((png_ptr->mode & PNG_HAVE_IHDR) != 0)
847 png_chunk_error(png_ptr, "out of place");
849 /* Check the length */
850 if (length != 13)
851 png_chunk_error(png_ptr, "invalid");
853 png_ptr->mode |= PNG_HAVE_IHDR;
855 png_crc_read(png_ptr, buf, 13);
856 png_crc_finish(png_ptr, 0);
858 width = png_get_uint_31(png_ptr, buf);
859 height = png_get_uint_31(png_ptr, buf + 4);
860 bit_depth = buf[8];
861 color_type = buf[9];
862 compression_type = buf[10];
863 filter_type = buf[11];
864 interlace_type = buf[12];
866 #ifdef PNG_READ_APNG_SUPPORTED
867 png_ptr->first_frame_width = width;
868 png_ptr->first_frame_height = height;
869 #endif
871 /* Set internal variables */
872 png_ptr->width = width;
873 png_ptr->height = height;
874 png_ptr->bit_depth = (png_byte)bit_depth;
875 png_ptr->interlaced = (png_byte)interlace_type;
876 png_ptr->color_type = (png_byte)color_type;
877 #ifdef PNG_MNG_FEATURES_SUPPORTED
878 png_ptr->filter_type = (png_byte)filter_type;
879 #endif
880 png_ptr->compression_type = (png_byte)compression_type;
882 /* Find number of channels */
883 switch (png_ptr->color_type)
885 default: /* invalid, png_set_IHDR calls png_error */
886 case PNG_COLOR_TYPE_GRAY:
887 case PNG_COLOR_TYPE_PALETTE:
888 png_ptr->channels = 1;
889 break;
891 case PNG_COLOR_TYPE_RGB:
892 png_ptr->channels = 3;
893 break;
895 case PNG_COLOR_TYPE_GRAY_ALPHA:
896 png_ptr->channels = 2;
897 break;
899 case PNG_COLOR_TYPE_RGB_ALPHA:
900 png_ptr->channels = 4;
901 break;
904 /* Set up other useful info */
905 png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth * png_ptr->channels);
906 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width);
907 png_debug1(3, "bit_depth = %d", png_ptr->bit_depth);
908 png_debug1(3, "channels = %d", png_ptr->channels);
909 png_debug1(3, "rowbytes = %lu", (unsigned long)png_ptr->rowbytes);
910 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
911 color_type, interlace_type, compression_type, filter_type);
914 /* Read and check the palette */
915 void /* PRIVATE */
916 png_handle_PLTE(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
918 png_color palette[PNG_MAX_PALETTE_LENGTH];
919 int max_palette_length, num, i;
920 #ifdef PNG_POINTER_INDEXING_SUPPORTED
921 png_colorp pal_ptr;
922 #endif
924 png_debug(1, "in png_handle_PLTE");
926 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
927 png_chunk_error(png_ptr, "missing IHDR");
929 /* Moved to before the 'after IDAT' check below because otherwise duplicate
930 * PLTE chunks are potentially ignored (the spec says there shall not be more
931 * than one PLTE, the error is not treated as benign, so this check trumps
932 * the requirement that PLTE appears before IDAT.)
934 else if ((png_ptr->mode & PNG_HAVE_PLTE) != 0)
935 png_chunk_error(png_ptr, "duplicate");
937 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
939 /* This is benign because the non-benign error happened before, when an
940 * IDAT was encountered in a color-mapped image with no PLTE.
942 png_crc_finish(png_ptr, length);
943 png_chunk_benign_error(png_ptr, "out of place");
944 return;
947 png_ptr->mode |= PNG_HAVE_PLTE;
949 if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0)
951 png_crc_finish(png_ptr, length);
952 png_chunk_benign_error(png_ptr, "ignored in grayscale PNG");
953 return;
956 #ifndef PNG_READ_OPT_PLTE_SUPPORTED
957 if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
959 png_crc_finish(png_ptr, length);
960 return;
962 #endif
964 if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3)
966 png_crc_finish(png_ptr, length);
968 if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
969 png_chunk_benign_error(png_ptr, "invalid");
971 else
972 png_chunk_error(png_ptr, "invalid");
974 return;
977 /* The cast is safe because 'length' is less than 3*PNG_MAX_PALETTE_LENGTH */
978 num = (int)length / 3;
980 /* If the palette has 256 or fewer entries but is too large for the bit
981 * depth, we don't issue an error, to preserve the behavior of previous
982 * libpng versions. We silently truncate the unused extra palette entries
983 * here.
985 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
986 max_palette_length = (1 << png_ptr->bit_depth);
987 else
988 max_palette_length = PNG_MAX_PALETTE_LENGTH;
990 if (num > max_palette_length)
991 num = max_palette_length;
993 #ifdef PNG_POINTER_INDEXING_SUPPORTED
994 for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)
996 png_byte buf[3];
998 png_crc_read(png_ptr, buf, 3);
999 pal_ptr->red = buf[0];
1000 pal_ptr->green = buf[1];
1001 pal_ptr->blue = buf[2];
1003 #else
1004 for (i = 0; i < num; i++)
1006 png_byte buf[3];
1008 png_crc_read(png_ptr, buf, 3);
1009 /* Don't depend upon png_color being any order */
1010 palette[i].red = buf[0];
1011 palette[i].green = buf[1];
1012 palette[i].blue = buf[2];
1014 #endif
1016 /* If we actually need the PLTE chunk (ie for a paletted image), we do
1017 * whatever the normal CRC configuration tells us. However, if we
1018 * have an RGB image, the PLTE can be considered ancillary, so
1019 * we will act as though it is.
1021 #ifndef PNG_READ_OPT_PLTE_SUPPORTED
1022 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1023 #endif
1025 png_crc_finish(png_ptr, (png_uint_32) (length - (unsigned int)num * 3));
1028 #ifndef PNG_READ_OPT_PLTE_SUPPORTED
1029 else if (png_crc_error(png_ptr) != 0) /* Only if we have a CRC error */
1031 /* If we don't want to use the data from an ancillary chunk,
1032 * we have two options: an error abort, or a warning and we
1033 * ignore the data in this chunk (which should be OK, since
1034 * it's considered ancillary for a RGB or RGBA image).
1036 * IMPLEMENTATION NOTE: this is only here because png_crc_finish uses the
1037 * chunk type to determine whether to check the ancillary or the critical
1038 * flags.
1040 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE) == 0)
1042 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) != 0)
1043 return;
1045 else
1046 png_chunk_error(png_ptr, "CRC error");
1049 /* Otherwise, we (optionally) emit a warning and use the chunk. */
1050 else if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) == 0)
1051 png_chunk_warning(png_ptr, "CRC error");
1053 #endif
1055 /* TODO: png_set_PLTE has the side effect of setting png_ptr->palette to its
1056 * own copy of the palette. This has the side effect that when png_start_row
1057 * is called (this happens after any call to png_read_update_info) the
1058 * info_ptr palette gets changed. This is extremely unexpected and
1059 * confusing.
1061 * Fix this by not sharing the palette in this way.
1063 png_set_PLTE(png_ptr, info_ptr, palette, num);
1065 /* The three chunks, bKGD, hIST and tRNS *must* appear after PLTE and before
1066 * IDAT. Prior to 1.6.0 this was not checked; instead the code merely
1067 * checked the apparent validity of a tRNS chunk inserted before PLTE on a
1068 * palette PNG. 1.6.0 attempts to rigorously follow the standard and
1069 * therefore does a benign error if the erroneous condition is detected *and*
1070 * cancels the tRNS if the benign error returns. The alternative is to
1071 * amend the standard since it would be rather hypocritical of the standards
1072 * maintainers to ignore it.
1074 #ifdef PNG_READ_tRNS_SUPPORTED
1075 if (png_ptr->num_trans > 0 ||
1076 (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS) != 0))
1078 /* Cancel this because otherwise it would be used if the transforms
1079 * require it. Don't cancel the 'valid' flag because this would prevent
1080 * detection of duplicate chunks.
1082 png_ptr->num_trans = 0;
1084 if (info_ptr != NULL)
1085 info_ptr->num_trans = 0;
1087 png_chunk_benign_error(png_ptr, "tRNS must be after");
1089 #endif
1091 #ifdef PNG_READ_hIST_SUPPORTED
1092 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) != 0)
1093 png_chunk_benign_error(png_ptr, "hIST must be after");
1094 #endif
1096 #ifdef PNG_READ_bKGD_SUPPORTED
1097 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) != 0)
1098 png_chunk_benign_error(png_ptr, "bKGD must be after");
1099 #endif
1102 void /* PRIVATE */
1103 png_handle_IEND(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1105 png_debug(1, "in png_handle_IEND");
1107 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0 ||
1108 (png_ptr->mode & PNG_HAVE_IDAT) == 0)
1109 png_chunk_error(png_ptr, "out of place");
1111 png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND);
1113 png_crc_finish(png_ptr, length);
1115 if (length != 0)
1116 png_chunk_benign_error(png_ptr, "invalid");
1118 PNG_UNUSED(info_ptr)
1121 #ifdef PNG_READ_gAMA_SUPPORTED
1122 void /* PRIVATE */
1123 png_handle_gAMA(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1125 png_fixed_point igamma;
1126 png_byte buf[4];
1128 png_debug(1, "in png_handle_gAMA");
1130 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1131 png_chunk_error(png_ptr, "missing IHDR");
1133 else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
1135 png_crc_finish(png_ptr, length);
1136 png_chunk_benign_error(png_ptr, "out of place");
1137 return;
1140 if (length != 4)
1142 png_crc_finish(png_ptr, length);
1143 png_chunk_benign_error(png_ptr, "invalid");
1144 return;
1147 png_crc_read(png_ptr, buf, 4);
1149 if (png_crc_finish(png_ptr, 0) != 0)
1150 return;
1152 igamma = png_get_fixed_point(NULL, buf);
1154 png_colorspace_set_gamma(png_ptr, &png_ptr->colorspace, igamma);
1155 png_colorspace_sync(png_ptr, info_ptr);
1157 #endif
1159 #ifdef PNG_READ_sBIT_SUPPORTED
1160 void /* PRIVATE */
1161 png_handle_sBIT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1163 unsigned int truelen, i;
1164 png_byte sample_depth;
1165 png_byte buf[4];
1167 png_debug(1, "in png_handle_sBIT");
1169 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1170 png_chunk_error(png_ptr, "missing IHDR");
1172 else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
1174 png_crc_finish(png_ptr, length);
1175 png_chunk_benign_error(png_ptr, "out of place");
1176 return;
1179 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT) != 0)
1181 png_crc_finish(png_ptr, length);
1182 png_chunk_benign_error(png_ptr, "duplicate");
1183 return;
1186 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1188 truelen = 3;
1189 sample_depth = 8;
1192 else
1194 truelen = png_ptr->channels;
1195 sample_depth = png_ptr->bit_depth;
1198 if (length != truelen || length > 4)
1200 png_chunk_benign_error(png_ptr, "invalid");
1201 png_crc_finish(png_ptr, length);
1202 return;
1205 buf[0] = buf[1] = buf[2] = buf[3] = sample_depth;
1206 png_crc_read(png_ptr, buf, truelen);
1208 if (png_crc_finish(png_ptr, 0) != 0)
1209 return;
1211 for (i=0; i<truelen; ++i)
1213 if (buf[i] == 0 || buf[i] > sample_depth)
1215 png_chunk_benign_error(png_ptr, "invalid");
1216 return;
1220 if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
1222 png_ptr->sig_bit.red = buf[0];
1223 png_ptr->sig_bit.green = buf[1];
1224 png_ptr->sig_bit.blue = buf[2];
1225 png_ptr->sig_bit.alpha = buf[3];
1228 else
1230 png_ptr->sig_bit.gray = buf[0];
1231 png_ptr->sig_bit.red = buf[0];
1232 png_ptr->sig_bit.green = buf[0];
1233 png_ptr->sig_bit.blue = buf[0];
1234 png_ptr->sig_bit.alpha = buf[1];
1237 png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
1239 #endif
1241 #ifdef PNG_READ_cHRM_SUPPORTED
1242 void /* PRIVATE */
1243 png_handle_cHRM(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1245 png_byte buf[32];
1246 png_xy xy;
1248 png_debug(1, "in png_handle_cHRM");
1250 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1251 png_chunk_error(png_ptr, "missing IHDR");
1253 else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
1255 png_crc_finish(png_ptr, length);
1256 png_chunk_benign_error(png_ptr, "out of place");
1257 return;
1260 if (length != 32)
1262 png_crc_finish(png_ptr, length);
1263 png_chunk_benign_error(png_ptr, "invalid");
1264 return;
1267 png_crc_read(png_ptr, buf, 32);
1269 if (png_crc_finish(png_ptr, 0) != 0)
1270 return;
1272 xy.whitex = png_get_fixed_point(NULL, buf);
1273 xy.whitey = png_get_fixed_point(NULL, buf + 4);
1274 xy.redx = png_get_fixed_point(NULL, buf + 8);
1275 xy.redy = png_get_fixed_point(NULL, buf + 12);
1276 xy.greenx = png_get_fixed_point(NULL, buf + 16);
1277 xy.greeny = png_get_fixed_point(NULL, buf + 20);
1278 xy.bluex = png_get_fixed_point(NULL, buf + 24);
1279 xy.bluey = png_get_fixed_point(NULL, buf + 28);
1281 if (xy.whitex == PNG_FIXED_ERROR ||
1282 xy.whitey == PNG_FIXED_ERROR ||
1283 xy.redx == PNG_FIXED_ERROR ||
1284 xy.redy == PNG_FIXED_ERROR ||
1285 xy.greenx == PNG_FIXED_ERROR ||
1286 xy.greeny == PNG_FIXED_ERROR ||
1287 xy.bluex == PNG_FIXED_ERROR ||
1288 xy.bluey == PNG_FIXED_ERROR)
1290 png_chunk_benign_error(png_ptr, "invalid values");
1291 return;
1294 /* If a colorspace error has already been output skip this chunk */
1295 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0)
1296 return;
1298 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM) != 0)
1300 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1301 png_colorspace_sync(png_ptr, info_ptr);
1302 png_chunk_benign_error(png_ptr, "duplicate");
1303 return;
1306 png_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM;
1307 (void)png_colorspace_set_chromaticities(png_ptr, &png_ptr->colorspace, &xy,
1308 1/*prefer cHRM values*/);
1309 png_colorspace_sync(png_ptr, info_ptr);
1311 #endif
1313 #ifdef PNG_READ_sRGB_SUPPORTED
1314 void /* PRIVATE */
1315 png_handle_sRGB(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1317 png_byte intent;
1319 png_debug(1, "in png_handle_sRGB");
1321 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1322 png_chunk_error(png_ptr, "missing IHDR");
1324 else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
1326 png_crc_finish(png_ptr, length);
1327 png_chunk_benign_error(png_ptr, "out of place");
1328 return;
1331 if (length != 1)
1333 png_crc_finish(png_ptr, length);
1334 png_chunk_benign_error(png_ptr, "invalid");
1335 return;
1338 png_crc_read(png_ptr, &intent, 1);
1340 if (png_crc_finish(png_ptr, 0) != 0)
1341 return;
1343 /* If a colorspace error has already been output skip this chunk */
1344 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0)
1345 return;
1347 /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect
1348 * this.
1350 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) != 0)
1352 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1353 png_colorspace_sync(png_ptr, info_ptr);
1354 png_chunk_benign_error(png_ptr, "too many profiles");
1355 return;
1358 (void)png_colorspace_set_sRGB(png_ptr, &png_ptr->colorspace, intent);
1359 png_colorspace_sync(png_ptr, info_ptr);
1361 #endif /* READ_sRGB */
1363 #ifdef PNG_READ_iCCP_SUPPORTED
1364 void /* PRIVATE */
1365 png_handle_iCCP(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1366 /* Note: this does not properly handle profiles that are > 64K under DOS */
1368 png_const_charp errmsg = NULL; /* error message output, or no error */
1369 int finished = 0; /* crc checked */
1371 png_debug(1, "in png_handle_iCCP");
1373 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1374 png_chunk_error(png_ptr, "missing IHDR");
1376 else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
1378 png_crc_finish(png_ptr, length);
1379 png_chunk_benign_error(png_ptr, "out of place");
1380 return;
1383 /* Consistent with all the above colorspace handling an obviously *invalid*
1384 * chunk is just ignored, so does not invalidate the color space. An
1385 * alternative is to set the 'invalid' flags at the start of this routine
1386 * and only clear them in they were not set before and all the tests pass.
1389 /* The keyword must be at least one character and there is a
1390 * terminator (0) byte and the compression method byte, and the
1391 * 'zlib' datastream is at least 11 bytes.
1393 if (length < 14)
1395 png_crc_finish(png_ptr, length);
1396 png_chunk_benign_error(png_ptr, "too short");
1397 return;
1400 /* If a colorspace error has already been output skip this chunk */
1401 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0)
1403 png_crc_finish(png_ptr, length);
1404 return;
1407 /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect
1408 * this.
1410 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) == 0)
1412 uInt read_length, keyword_length;
1413 char keyword[81];
1415 /* Find the keyword; the keyword plus separator and compression method
1416 * bytes can be at most 81 characters long.
1418 read_length = 81; /* maximum */
1419 if (read_length > length)
1420 read_length = (uInt)length;
1422 png_crc_read(png_ptr, (png_bytep)keyword, read_length);
1423 length -= read_length;
1425 /* The minimum 'zlib' stream is assumed to be just the 2 byte header,
1426 * 5 bytes minimum 'deflate' stream, and the 4 byte checksum.
1428 if (length < 11)
1430 png_crc_finish(png_ptr, length);
1431 png_chunk_benign_error(png_ptr, "too short");
1432 return;
1435 keyword_length = 0;
1436 while (keyword_length < 80 && keyword_length < read_length &&
1437 keyword[keyword_length] != 0)
1438 ++keyword_length;
1440 /* TODO: make the keyword checking common */
1441 if (keyword_length >= 1 && keyword_length <= 79)
1443 /* We only understand '0' compression - deflate - so if we get a
1444 * different value we can't safely decode the chunk.
1446 if (keyword_length+1 < read_length &&
1447 keyword[keyword_length+1] == PNG_COMPRESSION_TYPE_BASE)
1449 read_length -= keyword_length+2;
1451 if (png_inflate_claim(png_ptr, png_iCCP) == Z_OK)
1453 Byte profile_header[132]={0};
1454 Byte local_buffer[PNG_INFLATE_BUF_SIZE];
1455 png_alloc_size_t size = (sizeof profile_header);
1457 png_ptr->zstream.next_in = (Bytef*)keyword + (keyword_length+2);
1458 png_ptr->zstream.avail_in = read_length;
1459 (void)png_inflate_read(png_ptr, local_buffer,
1460 (sizeof local_buffer), &length, profile_header, &size,
1461 0/*finish: don't, because the output is too small*/);
1463 if (size == 0)
1465 /* We have the ICC profile header; do the basic header checks.
1467 png_uint_32 profile_length = png_get_uint_32(profile_header);
1469 if (png_icc_check_length(png_ptr, &png_ptr->colorspace,
1470 keyword, profile_length) != 0)
1472 /* The length is apparently ok, so we can check the 132
1473 * byte header.
1475 if (png_icc_check_header(png_ptr, &png_ptr->colorspace,
1476 keyword, profile_length, profile_header,
1477 png_ptr->color_type) != 0)
1479 /* Now read the tag table; a variable size buffer is
1480 * needed at this point, allocate one for the whole
1481 * profile. The header check has already validated
1482 * that none of this stuff will overflow.
1484 png_uint_32 tag_count =
1485 png_get_uint_32(profile_header + 128);
1486 png_bytep profile = png_read_buffer(png_ptr,
1487 profile_length, 2/*silent*/);
1489 if (profile != NULL)
1491 memcpy(profile, profile_header,
1492 (sizeof profile_header));
1494 size = 12 * tag_count;
1496 (void)png_inflate_read(png_ptr, local_buffer,
1497 (sizeof local_buffer), &length,
1498 profile + (sizeof profile_header), &size, 0);
1500 /* Still expect a buffer error because we expect
1501 * there to be some tag data!
1503 if (size == 0)
1505 if (png_icc_check_tag_table(png_ptr,
1506 &png_ptr->colorspace, keyword, profile_length,
1507 profile) != 0)
1509 /* The profile has been validated for basic
1510 * security issues, so read the whole thing in.
1512 size = profile_length - (sizeof profile_header)
1513 - 12 * tag_count;
1515 (void)png_inflate_read(png_ptr, local_buffer,
1516 (sizeof local_buffer), &length,
1517 profile + (sizeof profile_header) +
1518 12 * tag_count, &size, 1/*finish*/);
1520 if (length > 0 && !(png_ptr->flags &
1521 PNG_FLAG_BENIGN_ERRORS_WARN))
1522 errmsg = "extra compressed data";
1524 /* But otherwise allow extra data: */
1525 else if (size == 0)
1527 if (length > 0)
1529 /* This can be handled completely, so
1530 * keep going.
1532 png_chunk_warning(png_ptr,
1533 "extra compressed data");
1536 png_crc_finish(png_ptr, length);
1537 finished = 1;
1539 # if defined(PNG_sRGB_SUPPORTED) && PNG_sRGB_PROFILE_CHECKS >= 0
1540 /* Check for a match against sRGB */
1541 png_icc_set_sRGB(png_ptr,
1542 &png_ptr->colorspace, profile,
1543 png_ptr->zstream.adler);
1544 # endif
1546 /* Steal the profile for info_ptr. */
1547 if (info_ptr != NULL)
1549 png_free_data(png_ptr, info_ptr,
1550 PNG_FREE_ICCP, 0);
1552 info_ptr->iccp_name = png_voidcast(char*,
1553 png_malloc_base(png_ptr,
1554 keyword_length+1));
1555 if (info_ptr->iccp_name != NULL)
1557 memcpy(info_ptr->iccp_name, keyword,
1558 keyword_length+1);
1559 info_ptr->iccp_proflen =
1560 profile_length;
1561 info_ptr->iccp_profile = profile;
1562 png_ptr->read_buffer = NULL; /*steal*/
1563 info_ptr->free_me |= PNG_FREE_ICCP;
1564 info_ptr->valid |= PNG_INFO_iCCP;
1567 else
1569 png_ptr->colorspace.flags |=
1570 PNG_COLORSPACE_INVALID;
1571 errmsg = "out of memory";
1575 /* else the profile remains in the read
1576 * buffer which gets reused for subsequent
1577 * chunks.
1580 if (info_ptr != NULL)
1581 png_colorspace_sync(png_ptr, info_ptr);
1583 if (errmsg == NULL)
1585 png_ptr->zowner = 0;
1586 return;
1589 if (errmsg == NULL)
1590 errmsg = png_ptr->zstream.msg;
1592 /* else png_icc_check_tag_table output an error */
1594 else /* profile truncated */
1595 errmsg = png_ptr->zstream.msg;
1598 else
1599 errmsg = "out of memory";
1602 /* else png_icc_check_header output an error */
1605 /* else png_icc_check_length output an error */
1608 else /* profile truncated */
1609 errmsg = png_ptr->zstream.msg;
1611 /* Release the stream */
1612 png_ptr->zowner = 0;
1615 else /* png_inflate_claim failed */
1616 errmsg = png_ptr->zstream.msg;
1619 else
1620 errmsg = "bad compression method"; /* or missing */
1623 else
1624 errmsg = "bad keyword";
1627 else
1628 errmsg = "too many profiles";
1630 /* Failure: the reason is in 'errmsg' */
1631 if (finished == 0)
1632 png_crc_finish(png_ptr, length);
1634 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1635 png_colorspace_sync(png_ptr, info_ptr);
1636 if (errmsg != NULL) /* else already output */
1637 png_chunk_benign_error(png_ptr, errmsg);
1639 #endif /* READ_iCCP */
1641 #ifdef PNG_READ_sPLT_SUPPORTED
1642 void /* PRIVATE */
1643 png_handle_sPLT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1644 /* Note: this does not properly handle chunks that are > 64K under DOS */
1646 png_bytep entry_start, buffer;
1647 png_sPLT_t new_palette;
1648 png_sPLT_entryp pp;
1649 png_uint_32 data_length;
1650 int entry_size, i;
1651 png_uint_32 skip = 0;
1652 png_uint_32 dl;
1653 size_t max_dl;
1655 png_debug(1, "in png_handle_sPLT");
1657 #ifdef PNG_USER_LIMITS_SUPPORTED
1658 if (png_ptr->user_chunk_cache_max != 0)
1660 if (png_ptr->user_chunk_cache_max == 1)
1662 png_crc_finish(png_ptr, length);
1663 return;
1666 if (--png_ptr->user_chunk_cache_max == 1)
1668 png_warning(png_ptr, "No space in chunk cache for sPLT");
1669 png_crc_finish(png_ptr, length);
1670 return;
1673 #endif
1675 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1676 png_chunk_error(png_ptr, "missing IHDR");
1678 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
1680 png_crc_finish(png_ptr, length);
1681 png_chunk_benign_error(png_ptr, "out of place");
1682 return;
1685 #ifdef PNG_MAX_MALLOC_64K
1686 if (length > 65535U)
1688 png_crc_finish(png_ptr, length);
1689 png_chunk_benign_error(png_ptr, "too large to fit in memory");
1690 return;
1692 #endif
1694 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
1695 if (buffer == NULL)
1697 png_crc_finish(png_ptr, length);
1698 png_chunk_benign_error(png_ptr, "out of memory");
1699 return;
1703 /* WARNING: this may break if size_t is less than 32 bits; it is assumed
1704 * that the PNG_MAX_MALLOC_64K test is enabled in this case, but this is a
1705 * potential breakage point if the types in pngconf.h aren't exactly right.
1707 png_crc_read(png_ptr, buffer, length);
1709 if (png_crc_finish(png_ptr, skip) != 0)
1710 return;
1712 buffer[length] = 0;
1714 for (entry_start = buffer; *entry_start; entry_start++)
1715 /* Empty loop to find end of name */ ;
1717 ++entry_start;
1719 /* A sample depth should follow the separator, and we should be on it */
1720 if (length < 2U || entry_start > buffer + (length - 2U))
1722 png_warning(png_ptr, "malformed sPLT chunk");
1723 return;
1726 new_palette.depth = *entry_start++;
1727 entry_size = (new_palette.depth == 8 ? 6 : 10);
1728 /* This must fit in a png_uint_32 because it is derived from the original
1729 * chunk data length.
1731 data_length = length - (png_uint_32)(entry_start - buffer);
1733 /* Integrity-check the data length */
1734 if ((data_length % (unsigned int)entry_size) != 0)
1736 png_warning(png_ptr, "sPLT chunk has bad length");
1737 return;
1740 dl = (png_uint_32)(data_length / (unsigned int)entry_size);
1741 max_dl = PNG_SIZE_MAX / (sizeof (png_sPLT_entry));
1743 if (dl > max_dl)
1745 png_warning(png_ptr, "sPLT chunk too long");
1746 return;
1749 new_palette.nentries = (png_int_32)(data_length / (unsigned int)entry_size);
1751 new_palette.entries = (png_sPLT_entryp)png_malloc_warn(png_ptr,
1752 (png_alloc_size_t) new_palette.nentries * (sizeof (png_sPLT_entry)));
1754 if (new_palette.entries == NULL)
1756 png_warning(png_ptr, "sPLT chunk requires too much memory");
1757 return;
1760 #ifdef PNG_POINTER_INDEXING_SUPPORTED
1761 for (i = 0; i < new_palette.nentries; i++)
1763 pp = new_palette.entries + i;
1765 if (new_palette.depth == 8)
1767 pp->red = *entry_start++;
1768 pp->green = *entry_start++;
1769 pp->blue = *entry_start++;
1770 pp->alpha = *entry_start++;
1773 else
1775 pp->red = png_get_uint_16(entry_start); entry_start += 2;
1776 pp->green = png_get_uint_16(entry_start); entry_start += 2;
1777 pp->blue = png_get_uint_16(entry_start); entry_start += 2;
1778 pp->alpha = png_get_uint_16(entry_start); entry_start += 2;
1781 pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
1783 #else
1784 pp = new_palette.entries;
1786 for (i = 0; i < new_palette.nentries; i++)
1789 if (new_palette.depth == 8)
1791 pp[i].red = *entry_start++;
1792 pp[i].green = *entry_start++;
1793 pp[i].blue = *entry_start++;
1794 pp[i].alpha = *entry_start++;
1797 else
1799 pp[i].red = png_get_uint_16(entry_start); entry_start += 2;
1800 pp[i].green = png_get_uint_16(entry_start); entry_start += 2;
1801 pp[i].blue = png_get_uint_16(entry_start); entry_start += 2;
1802 pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2;
1805 pp[i].frequency = png_get_uint_16(entry_start); entry_start += 2;
1807 #endif
1809 /* Discard all chunk data except the name and stash that */
1810 new_palette.name = (png_charp)buffer;
1812 png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);
1814 png_free(png_ptr, new_palette.entries);
1816 #endif /* READ_sPLT */
1818 #ifdef PNG_READ_tRNS_SUPPORTED
1819 void /* PRIVATE */
1820 png_handle_tRNS(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1822 png_byte readbuf[PNG_MAX_PALETTE_LENGTH];
1824 png_debug(1, "in png_handle_tRNS");
1826 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1827 png_chunk_error(png_ptr, "missing IHDR");
1829 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
1831 png_crc_finish(png_ptr, length);
1832 png_chunk_benign_error(png_ptr, "out of place");
1833 return;
1836 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS) != 0)
1838 png_crc_finish(png_ptr, length);
1839 png_chunk_benign_error(png_ptr, "duplicate");
1840 return;
1843 if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
1845 png_byte buf[2];
1847 if (length != 2)
1849 png_crc_finish(png_ptr, length);
1850 png_chunk_benign_error(png_ptr, "invalid");
1851 return;
1854 png_crc_read(png_ptr, buf, 2);
1855 png_ptr->num_trans = 1;
1856 png_ptr->trans_color.gray = png_get_uint_16(buf);
1859 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
1861 png_byte buf[6];
1863 if (length != 6)
1865 png_crc_finish(png_ptr, length);
1866 png_chunk_benign_error(png_ptr, "invalid");
1867 return;
1870 png_crc_read(png_ptr, buf, length);
1871 png_ptr->num_trans = 1;
1872 png_ptr->trans_color.red = png_get_uint_16(buf);
1873 png_ptr->trans_color.green = png_get_uint_16(buf + 2);
1874 png_ptr->trans_color.blue = png_get_uint_16(buf + 4);
1877 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1879 if ((png_ptr->mode & PNG_HAVE_PLTE) == 0)
1881 /* TODO: is this actually an error in the ISO spec? */
1882 png_crc_finish(png_ptr, length);
1883 png_chunk_benign_error(png_ptr, "out of place");
1884 return;
1887 if (length > (unsigned int) png_ptr->num_palette ||
1888 length > (unsigned int) PNG_MAX_PALETTE_LENGTH ||
1889 length == 0)
1891 png_crc_finish(png_ptr, length);
1892 png_chunk_benign_error(png_ptr, "invalid");
1893 return;
1896 png_crc_read(png_ptr, readbuf, length);
1897 png_ptr->num_trans = (png_uint_16)length;
1900 else
1902 png_crc_finish(png_ptr, length);
1903 png_chunk_benign_error(png_ptr, "invalid with alpha channel");
1904 return;
1907 if (png_crc_finish(png_ptr, 0) != 0)
1909 png_ptr->num_trans = 0;
1910 return;
1913 /* TODO: this is a horrible side effect in the palette case because the
1914 * png_struct ends up with a pointer to the tRNS buffer owned by the
1915 * png_info. Fix this.
1917 png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,
1918 &(png_ptr->trans_color));
1920 #endif
1922 #ifdef PNG_READ_bKGD_SUPPORTED
1923 void /* PRIVATE */
1924 png_handle_bKGD(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1926 unsigned int truelen;
1927 png_byte buf[6];
1928 png_color_16 background;
1930 png_debug(1, "in png_handle_bKGD");
1932 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1933 png_chunk_error(png_ptr, "missing IHDR");
1935 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0 ||
1936 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
1937 (png_ptr->mode & PNG_HAVE_PLTE) == 0))
1939 png_crc_finish(png_ptr, length);
1940 png_chunk_benign_error(png_ptr, "out of place");
1941 return;
1944 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) != 0)
1946 png_crc_finish(png_ptr, length);
1947 png_chunk_benign_error(png_ptr, "duplicate");
1948 return;
1951 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1952 truelen = 1;
1954 else if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
1955 truelen = 6;
1957 else
1958 truelen = 2;
1960 if (length != truelen)
1962 png_crc_finish(png_ptr, length);
1963 png_chunk_benign_error(png_ptr, "invalid");
1964 return;
1967 png_crc_read(png_ptr, buf, truelen);
1969 if (png_crc_finish(png_ptr, 0) != 0)
1970 return;
1972 /* We convert the index value into RGB components so that we can allow
1973 * arbitrary RGB values for background when we have transparency, and
1974 * so it is easy to determine the RGB values of the background color
1975 * from the info_ptr struct.
1977 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1979 background.index = buf[0];
1981 if (info_ptr != NULL && info_ptr->num_palette != 0)
1983 if (buf[0] >= info_ptr->num_palette)
1985 png_chunk_benign_error(png_ptr, "invalid index");
1986 return;
1989 background.red = (png_uint_16)png_ptr->palette[buf[0]].red;
1990 background.green = (png_uint_16)png_ptr->palette[buf[0]].green;
1991 background.blue = (png_uint_16)png_ptr->palette[buf[0]].blue;
1994 else
1995 background.red = background.green = background.blue = 0;
1997 background.gray = 0;
2000 else if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0) /* GRAY */
2002 if (png_ptr->bit_depth <= 8)
2004 if (buf[0] != 0 || buf[1] >= (unsigned int)(1 << png_ptr->bit_depth))
2006 png_chunk_benign_error(png_ptr, "invalid gray level");
2007 return;
2011 background.index = 0;
2012 background.red =
2013 background.green =
2014 background.blue =
2015 background.gray = png_get_uint_16(buf);
2018 else
2020 if (png_ptr->bit_depth <= 8)
2022 if (buf[0] != 0 || buf[2] != 0 || buf[4] != 0)
2024 png_chunk_benign_error(png_ptr, "invalid color");
2025 return;
2029 background.index = 0;
2030 background.red = png_get_uint_16(buf);
2031 background.green = png_get_uint_16(buf + 2);
2032 background.blue = png_get_uint_16(buf + 4);
2033 background.gray = 0;
2036 png_set_bKGD(png_ptr, info_ptr, &background);
2038 #endif
2040 #ifdef PNG_READ_eXIf_SUPPORTED
2041 void /* PRIVATE */
2042 png_handle_eXIf(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2044 unsigned int i;
2046 png_debug(1, "in png_handle_eXIf");
2048 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2049 png_chunk_error(png_ptr, "missing IHDR");
2051 if (length < 2)
2053 png_crc_finish(png_ptr, length);
2054 png_chunk_benign_error(png_ptr, "too short");
2055 return;
2058 else if (info_ptr == NULL || (info_ptr->valid & PNG_INFO_eXIf) != 0)
2060 png_crc_finish(png_ptr, length);
2061 png_chunk_benign_error(png_ptr, "duplicate");
2062 return;
2065 info_ptr->free_me |= PNG_FREE_EXIF;
2067 info_ptr->eXIf_buf = png_voidcast(png_bytep,
2068 png_malloc_warn(png_ptr, length));
2070 if (info_ptr->eXIf_buf == NULL)
2072 png_crc_finish(png_ptr, length);
2073 png_chunk_benign_error(png_ptr, "out of memory");
2074 return;
2077 for (i = 0; i < length; i++)
2079 png_byte buf[1];
2080 png_crc_read(png_ptr, buf, 1);
2081 info_ptr->eXIf_buf[i] = buf[0];
2082 if (i == 1)
2084 if ((buf[0] != 'M' && buf[0] != 'I') ||
2085 (info_ptr->eXIf_buf[0] != buf[0]))
2087 png_crc_finish(png_ptr, length - 2);
2088 png_chunk_benign_error(png_ptr, "incorrect byte-order specifier");
2089 png_free(png_ptr, info_ptr->eXIf_buf);
2090 info_ptr->eXIf_buf = NULL;
2091 return;
2096 if (png_crc_finish(png_ptr, 0) == 0)
2097 png_set_eXIf_1(png_ptr, info_ptr, length, info_ptr->eXIf_buf);
2099 png_free(png_ptr, info_ptr->eXIf_buf);
2100 info_ptr->eXIf_buf = NULL;
2102 #endif
2104 #ifdef PNG_READ_hIST_SUPPORTED
2105 void /* PRIVATE */
2106 png_handle_hIST(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2108 unsigned int num, i;
2109 png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];
2111 png_debug(1, "in png_handle_hIST");
2113 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2114 png_chunk_error(png_ptr, "missing IHDR");
2116 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0 ||
2117 (png_ptr->mode & PNG_HAVE_PLTE) == 0)
2119 png_crc_finish(png_ptr, length);
2120 png_chunk_benign_error(png_ptr, "out of place");
2121 return;
2124 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) != 0)
2126 png_crc_finish(png_ptr, length);
2127 png_chunk_benign_error(png_ptr, "duplicate");
2128 return;
2131 num = length / 2 ;
2133 if (length != num * 2 ||
2134 num != (unsigned int)png_ptr->num_palette ||
2135 num > (unsigned int)PNG_MAX_PALETTE_LENGTH)
2137 png_crc_finish(png_ptr, length);
2138 png_chunk_benign_error(png_ptr, "invalid");
2139 return;
2142 for (i = 0; i < num; i++)
2144 png_byte buf[2];
2146 png_crc_read(png_ptr, buf, 2);
2147 readbuf[i] = png_get_uint_16(buf);
2150 if (png_crc_finish(png_ptr, 0) != 0)
2151 return;
2153 png_set_hIST(png_ptr, info_ptr, readbuf);
2155 #endif
2157 #ifdef PNG_READ_pHYs_SUPPORTED
2158 void /* PRIVATE */
2159 png_handle_pHYs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2161 png_byte buf[9];
2162 png_uint_32 res_x, res_y;
2163 int unit_type;
2165 png_debug(1, "in png_handle_pHYs");
2167 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2168 png_chunk_error(png_ptr, "missing IHDR");
2170 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2172 png_crc_finish(png_ptr, length);
2173 png_chunk_benign_error(png_ptr, "out of place");
2174 return;
2177 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs) != 0)
2179 png_crc_finish(png_ptr, length);
2180 png_chunk_benign_error(png_ptr, "duplicate");
2181 return;
2184 if (length != 9)
2186 png_crc_finish(png_ptr, length);
2187 png_chunk_benign_error(png_ptr, "invalid");
2188 return;
2191 png_crc_read(png_ptr, buf, 9);
2193 if (png_crc_finish(png_ptr, 0) != 0)
2194 return;
2196 res_x = png_get_uint_32(buf);
2197 res_y = png_get_uint_32(buf + 4);
2198 unit_type = buf[8];
2199 png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
2201 #endif
2203 #ifdef PNG_READ_oFFs_SUPPORTED
2204 void /* PRIVATE */
2205 png_handle_oFFs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2207 png_byte buf[9];
2208 png_int_32 offset_x, offset_y;
2209 int unit_type;
2211 png_debug(1, "in png_handle_oFFs");
2213 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2214 png_chunk_error(png_ptr, "missing IHDR");
2216 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2218 png_crc_finish(png_ptr, length);
2219 png_chunk_benign_error(png_ptr, "out of place");
2220 return;
2223 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs) != 0)
2225 png_crc_finish(png_ptr, length);
2226 png_chunk_benign_error(png_ptr, "duplicate");
2227 return;
2230 if (length != 9)
2232 png_crc_finish(png_ptr, length);
2233 png_chunk_benign_error(png_ptr, "invalid");
2234 return;
2237 png_crc_read(png_ptr, buf, 9);
2239 if (png_crc_finish(png_ptr, 0) != 0)
2240 return;
2242 offset_x = png_get_int_32(buf);
2243 offset_y = png_get_int_32(buf + 4);
2244 unit_type = buf[8];
2245 png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
2247 #endif
2249 #ifdef PNG_READ_pCAL_SUPPORTED
2250 /* Read the pCAL chunk (described in the PNG Extensions document) */
2251 void /* PRIVATE */
2252 png_handle_pCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2254 png_int_32 X0, X1;
2255 png_byte type, nparams;
2256 png_bytep buffer, buf, units, endptr;
2257 png_charpp params;
2258 int i;
2260 png_debug(1, "in png_handle_pCAL");
2262 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2263 png_chunk_error(png_ptr, "missing IHDR");
2265 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2267 png_crc_finish(png_ptr, length);
2268 png_chunk_benign_error(png_ptr, "out of place");
2269 return;
2272 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL) != 0)
2274 png_crc_finish(png_ptr, length);
2275 png_chunk_benign_error(png_ptr, "duplicate");
2276 return;
2279 png_debug1(2, "Allocating and reading pCAL chunk data (%u bytes)",
2280 length + 1);
2282 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
2284 if (buffer == NULL)
2286 png_crc_finish(png_ptr, length);
2287 png_chunk_benign_error(png_ptr, "out of memory");
2288 return;
2291 png_crc_read(png_ptr, buffer, length);
2293 if (png_crc_finish(png_ptr, 0) != 0)
2294 return;
2296 buffer[length] = 0; /* Null terminate the last string */
2298 png_debug(3, "Finding end of pCAL purpose string");
2299 for (buf = buffer; *buf; buf++)
2300 /* Empty loop */ ;
2302 endptr = buffer + length;
2304 /* We need to have at least 12 bytes after the purpose string
2305 * in order to get the parameter information.
2307 if (endptr - buf <= 12)
2309 png_chunk_benign_error(png_ptr, "invalid");
2310 return;
2313 png_debug(3, "Reading pCAL X0, X1, type, nparams, and units");
2314 X0 = png_get_int_32((png_bytep)buf+1);
2315 X1 = png_get_int_32((png_bytep)buf+5);
2316 type = buf[9];
2317 nparams = buf[10];
2318 units = buf + 11;
2320 png_debug(3, "Checking pCAL equation type and number of parameters");
2321 /* Check that we have the right number of parameters for known
2322 * equation types.
2324 if ((type == PNG_EQUATION_LINEAR && nparams != 2) ||
2325 (type == PNG_EQUATION_BASE_E && nparams != 3) ||
2326 (type == PNG_EQUATION_ARBITRARY && nparams != 3) ||
2327 (type == PNG_EQUATION_HYPERBOLIC && nparams != 4))
2329 png_chunk_benign_error(png_ptr, "invalid parameter count");
2330 return;
2333 else if (type >= PNG_EQUATION_LAST)
2335 png_chunk_benign_error(png_ptr, "unrecognized equation type");
2338 for (buf = units; *buf; buf++)
2339 /* Empty loop to move past the units string. */ ;
2341 png_debug(3, "Allocating pCAL parameters array");
2343 params = png_voidcast(png_charpp, png_malloc_warn(png_ptr,
2344 nparams * (sizeof (png_charp))));
2346 if (params == NULL)
2348 png_chunk_benign_error(png_ptr, "out of memory");
2349 return;
2352 /* Get pointers to the start of each parameter string. */
2353 for (i = 0; i < nparams; i++)
2355 buf++; /* Skip the null string terminator from previous parameter. */
2357 png_debug1(3, "Reading pCAL parameter %d", i);
2359 for (params[i] = (png_charp)buf; buf <= endptr && *buf != 0; buf++)
2360 /* Empty loop to move past each parameter string */ ;
2362 /* Make sure we haven't run out of data yet */
2363 if (buf > endptr)
2365 png_free(png_ptr, params);
2366 png_chunk_benign_error(png_ptr, "invalid data");
2367 return;
2371 png_set_pCAL(png_ptr, info_ptr, (png_charp)buffer, X0, X1, type, nparams,
2372 (png_charp)units, params);
2374 png_free(png_ptr, params);
2376 #endif
2378 #ifdef PNG_READ_sCAL_SUPPORTED
2379 /* Read the sCAL chunk */
2380 void /* PRIVATE */
2381 png_handle_sCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2383 png_bytep buffer;
2384 size_t i;
2385 int state;
2387 png_debug(1, "in png_handle_sCAL");
2389 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2390 png_chunk_error(png_ptr, "missing IHDR");
2392 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2394 png_crc_finish(png_ptr, length);
2395 png_chunk_benign_error(png_ptr, "out of place");
2396 return;
2399 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL) != 0)
2401 png_crc_finish(png_ptr, length);
2402 png_chunk_benign_error(png_ptr, "duplicate");
2403 return;
2406 /* Need unit type, width, \0, height: minimum 4 bytes */
2407 else if (length < 4)
2409 png_crc_finish(png_ptr, length);
2410 png_chunk_benign_error(png_ptr, "invalid");
2411 return;
2414 png_debug1(2, "Allocating and reading sCAL chunk data (%u bytes)",
2415 length + 1);
2417 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
2419 if (buffer == NULL)
2421 png_chunk_benign_error(png_ptr, "out of memory");
2422 png_crc_finish(png_ptr, length);
2423 return;
2426 png_crc_read(png_ptr, buffer, length);
2427 buffer[length] = 0; /* Null terminate the last string */
2429 if (png_crc_finish(png_ptr, 0) != 0)
2430 return;
2432 /* Validate the unit. */
2433 if (buffer[0] != 1 && buffer[0] != 2)
2435 png_chunk_benign_error(png_ptr, "invalid unit");
2436 return;
2439 /* Validate the ASCII numbers, need two ASCII numbers separated by
2440 * a '\0' and they need to fit exactly in the chunk data.
2442 i = 1;
2443 state = 0;
2445 if (png_check_fp_number((png_const_charp)buffer, length, &state, &i) == 0 ||
2446 i >= length || buffer[i++] != 0)
2447 png_chunk_benign_error(png_ptr, "bad width format");
2449 else if (PNG_FP_IS_POSITIVE(state) == 0)
2450 png_chunk_benign_error(png_ptr, "non-positive width");
2452 else
2454 size_t heighti = i;
2456 state = 0;
2457 if (png_check_fp_number((png_const_charp)buffer, length,
2458 &state, &i) == 0 || i != length)
2459 png_chunk_benign_error(png_ptr, "bad height format");
2461 else if (PNG_FP_IS_POSITIVE(state) == 0)
2462 png_chunk_benign_error(png_ptr, "non-positive height");
2464 else
2465 /* This is the (only) success case. */
2466 png_set_sCAL_s(png_ptr, info_ptr, buffer[0],
2467 (png_charp)buffer+1, (png_charp)buffer+heighti);
2470 #endif
2472 #ifdef PNG_READ_tIME_SUPPORTED
2473 void /* PRIVATE */
2474 png_handle_tIME(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2476 png_byte buf[7];
2477 png_time mod_time;
2479 png_debug(1, "in png_handle_tIME");
2481 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2482 png_chunk_error(png_ptr, "missing IHDR");
2484 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME) != 0)
2486 png_crc_finish(png_ptr, length);
2487 png_chunk_benign_error(png_ptr, "duplicate");
2488 return;
2491 if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2492 png_ptr->mode |= PNG_AFTER_IDAT;
2494 if (length != 7)
2496 png_crc_finish(png_ptr, length);
2497 png_chunk_benign_error(png_ptr, "invalid");
2498 return;
2501 png_crc_read(png_ptr, buf, 7);
2503 if (png_crc_finish(png_ptr, 0) != 0)
2504 return;
2506 mod_time.second = buf[6];
2507 mod_time.minute = buf[5];
2508 mod_time.hour = buf[4];
2509 mod_time.day = buf[3];
2510 mod_time.month = buf[2];
2511 mod_time.year = png_get_uint_16(buf);
2513 png_set_tIME(png_ptr, info_ptr, &mod_time);
2515 #endif
2517 #ifdef PNG_READ_tEXt_SUPPORTED
2518 /* Note: this does not properly handle chunks that are > 64K under DOS */
2519 void /* PRIVATE */
2520 png_handle_tEXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2522 png_text text_info;
2523 png_bytep buffer;
2524 png_charp key;
2525 png_charp text;
2526 png_uint_32 skip = 0;
2528 png_debug(1, "in png_handle_tEXt");
2530 #ifdef PNG_USER_LIMITS_SUPPORTED
2531 if (png_ptr->user_chunk_cache_max != 0)
2533 if (png_ptr->user_chunk_cache_max == 1)
2535 png_crc_finish(png_ptr, length);
2536 return;
2539 if (--png_ptr->user_chunk_cache_max == 1)
2541 png_crc_finish(png_ptr, length);
2542 png_chunk_benign_error(png_ptr, "no space in chunk cache");
2543 return;
2546 #endif
2548 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2549 png_chunk_error(png_ptr, "missing IHDR");
2551 if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2552 png_ptr->mode |= PNG_AFTER_IDAT;
2554 #ifdef PNG_MAX_MALLOC_64K
2555 if (length > 65535U)
2557 png_crc_finish(png_ptr, length);
2558 png_chunk_benign_error(png_ptr, "too large to fit in memory");
2559 return;
2561 #endif
2563 buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
2565 if (buffer == NULL)
2567 png_chunk_benign_error(png_ptr, "out of memory");
2568 return;
2571 png_crc_read(png_ptr, buffer, length);
2573 if (png_crc_finish(png_ptr, skip) != 0)
2574 return;
2576 key = (png_charp)buffer;
2577 key[length] = 0;
2579 for (text = key; *text; text++)
2580 /* Empty loop to find end of key */ ;
2582 if (text != key + length)
2583 text++;
2585 text_info.compression = PNG_TEXT_COMPRESSION_NONE;
2586 text_info.key = key;
2587 text_info.lang = NULL;
2588 text_info.lang_key = NULL;
2589 text_info.itxt_length = 0;
2590 text_info.text = text;
2591 text_info.text_length = strlen(text);
2593 if (png_set_text_2(png_ptr, info_ptr, &text_info, 1) != 0)
2594 png_warning(png_ptr, "Insufficient memory to process text chunk");
2596 #endif
2598 #ifdef PNG_READ_zTXt_SUPPORTED
2599 /* Note: this does not correctly handle chunks that are > 64K under DOS */
2600 void /* PRIVATE */
2601 png_handle_zTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2603 png_const_charp errmsg = NULL;
2604 png_bytep buffer;
2605 png_uint_32 keyword_length;
2607 png_debug(1, "in png_handle_zTXt");
2609 #ifdef PNG_USER_LIMITS_SUPPORTED
2610 if (png_ptr->user_chunk_cache_max != 0)
2612 if (png_ptr->user_chunk_cache_max == 1)
2614 png_crc_finish(png_ptr, length);
2615 return;
2618 if (--png_ptr->user_chunk_cache_max == 1)
2620 png_crc_finish(png_ptr, length);
2621 png_chunk_benign_error(png_ptr, "no space in chunk cache");
2622 return;
2625 #endif
2627 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2628 png_chunk_error(png_ptr, "missing IHDR");
2630 if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2631 png_ptr->mode |= PNG_AFTER_IDAT;
2633 /* Note, "length" is sufficient here; we won't be adding
2634 * a null terminator later.
2636 buffer = png_read_buffer(png_ptr, length, 2/*silent*/);
2638 if (buffer == NULL)
2640 png_crc_finish(png_ptr, length);
2641 png_chunk_benign_error(png_ptr, "out of memory");
2642 return;
2645 png_crc_read(png_ptr, buffer, length);
2647 if (png_crc_finish(png_ptr, 0) != 0)
2648 return;
2650 /* TODO: also check that the keyword contents match the spec! */
2651 for (keyword_length = 0;
2652 keyword_length < length && buffer[keyword_length] != 0;
2653 ++keyword_length)
2654 /* Empty loop to find end of name */ ;
2656 if (keyword_length > 79 || keyword_length < 1)
2657 errmsg = "bad keyword";
2659 /* zTXt must have some LZ data after the keyword, although it may expand to
2660 * zero bytes; we need a '\0' at the end of the keyword, the compression type
2661 * then the LZ data:
2663 else if (keyword_length + 3 > length)
2664 errmsg = "truncated";
2666 else if (buffer[keyword_length+1] != PNG_COMPRESSION_TYPE_BASE)
2667 errmsg = "unknown compression type";
2669 else
2671 png_alloc_size_t uncompressed_length = PNG_SIZE_MAX;
2673 /* TODO: at present png_decompress_chunk imposes a single application
2674 * level memory limit, this should be split to different values for iCCP
2675 * and text chunks.
2677 if (png_decompress_chunk(png_ptr, length, keyword_length+2,
2678 &uncompressed_length, 1/*terminate*/) == Z_STREAM_END)
2680 png_text text;
2682 if (png_ptr->read_buffer == NULL)
2683 errmsg="Read failure in png_handle_zTXt";
2684 else
2686 /* It worked; png_ptr->read_buffer now looks like a tEXt chunk
2687 * except for the extra compression type byte and the fact that
2688 * it isn't necessarily '\0' terminated.
2690 buffer = png_ptr->read_buffer;
2691 buffer[uncompressed_length+(keyword_length+2)] = 0;
2693 text.compression = PNG_TEXT_COMPRESSION_zTXt;
2694 text.key = (png_charp)buffer;
2695 text.text = (png_charp)(buffer + keyword_length+2);
2696 text.text_length = uncompressed_length;
2697 text.itxt_length = 0;
2698 text.lang = NULL;
2699 text.lang_key = NULL;
2701 if (png_set_text_2(png_ptr, info_ptr, &text, 1) != 0)
2702 errmsg = "insufficient memory";
2706 else
2707 errmsg = png_ptr->zstream.msg;
2710 if (errmsg != NULL)
2711 png_chunk_benign_error(png_ptr, errmsg);
2713 #endif
2715 #ifdef PNG_READ_iTXt_SUPPORTED
2716 /* Note: this does not correctly handle chunks that are > 64K under DOS */
2717 void /* PRIVATE */
2718 png_handle_iTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2720 png_const_charp errmsg = NULL;
2721 png_bytep buffer;
2722 png_uint_32 prefix_length;
2724 png_debug(1, "in png_handle_iTXt");
2726 #ifdef PNG_USER_LIMITS_SUPPORTED
2727 if (png_ptr->user_chunk_cache_max != 0)
2729 if (png_ptr->user_chunk_cache_max == 1)
2731 png_crc_finish(png_ptr, length);
2732 return;
2735 if (--png_ptr->user_chunk_cache_max == 1)
2737 png_crc_finish(png_ptr, length);
2738 png_chunk_benign_error(png_ptr, "no space in chunk cache");
2739 return;
2742 #endif
2744 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2745 png_chunk_error(png_ptr, "missing IHDR");
2747 if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2748 png_ptr->mode |= PNG_AFTER_IDAT;
2750 buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
2752 if (buffer == NULL)
2754 png_crc_finish(png_ptr, length);
2755 png_chunk_benign_error(png_ptr, "out of memory");
2756 return;
2759 png_crc_read(png_ptr, buffer, length);
2761 if (png_crc_finish(png_ptr, 0) != 0)
2762 return;
2764 /* First the keyword. */
2765 for (prefix_length=0;
2766 prefix_length < length && buffer[prefix_length] != 0;
2767 ++prefix_length)
2768 /* Empty loop */ ;
2770 /* Perform a basic check on the keyword length here. */
2771 if (prefix_length > 79 || prefix_length < 1)
2772 errmsg = "bad keyword";
2774 /* Expect keyword, compression flag, compression type, language, translated
2775 * keyword (both may be empty but are 0 terminated) then the text, which may
2776 * be empty.
2778 else if (prefix_length + 5 > length)
2779 errmsg = "truncated";
2781 else if (buffer[prefix_length+1] == 0 ||
2782 (buffer[prefix_length+1] == 1 &&
2783 buffer[prefix_length+2] == PNG_COMPRESSION_TYPE_BASE))
2785 int compressed = buffer[prefix_length+1] != 0;
2786 png_uint_32 language_offset, translated_keyword_offset;
2787 png_alloc_size_t uncompressed_length = 0;
2789 /* Now the language tag */
2790 prefix_length += 3;
2791 language_offset = prefix_length;
2793 for (; prefix_length < length && buffer[prefix_length] != 0;
2794 ++prefix_length)
2795 /* Empty loop */ ;
2797 /* WARNING: the length may be invalid here, this is checked below. */
2798 translated_keyword_offset = ++prefix_length;
2800 for (; prefix_length < length && buffer[prefix_length] != 0;
2801 ++prefix_length)
2802 /* Empty loop */ ;
2804 /* prefix_length should now be at the trailing '\0' of the translated
2805 * keyword, but it may already be over the end. None of this arithmetic
2806 * can overflow because chunks are at most 2^31 bytes long, but on 16-bit
2807 * systems the available allocation may overflow.
2809 ++prefix_length;
2811 if (compressed == 0 && prefix_length <= length)
2812 uncompressed_length = length - prefix_length;
2814 else if (compressed != 0 && prefix_length < length)
2816 uncompressed_length = PNG_SIZE_MAX;
2818 /* TODO: at present png_decompress_chunk imposes a single application
2819 * level memory limit, this should be split to different values for
2820 * iCCP and text chunks.
2822 if (png_decompress_chunk(png_ptr, length, prefix_length,
2823 &uncompressed_length, 1/*terminate*/) == Z_STREAM_END)
2824 buffer = png_ptr->read_buffer;
2826 else
2827 errmsg = png_ptr->zstream.msg;
2830 else
2831 errmsg = "truncated";
2833 if (errmsg == NULL)
2835 png_text text;
2837 buffer[uncompressed_length+prefix_length] = 0;
2839 if (compressed == 0)
2840 text.compression = PNG_ITXT_COMPRESSION_NONE;
2842 else
2843 text.compression = PNG_ITXT_COMPRESSION_zTXt;
2845 text.key = (png_charp)buffer;
2846 text.lang = (png_charp)buffer + language_offset;
2847 text.lang_key = (png_charp)buffer + translated_keyword_offset;
2848 text.text = (png_charp)buffer + prefix_length;
2849 text.text_length = 0;
2850 text.itxt_length = uncompressed_length;
2852 if (png_set_text_2(png_ptr, info_ptr, &text, 1) != 0)
2853 errmsg = "insufficient memory";
2857 else
2858 errmsg = "bad compression info";
2860 if (errmsg != NULL)
2861 png_chunk_benign_error(png_ptr, errmsg);
2863 #endif
2865 #ifdef PNG_READ_APNG_SUPPORTED
2866 void /* PRIVATE */
2867 png_handle_acTL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
2869 png_byte data[8];
2870 png_uint_32 num_frames;
2871 png_uint_32 num_plays;
2872 png_uint_32 didSet;
2874 png_debug(1, "in png_handle_acTL");
2876 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2878 png_error(png_ptr, "Missing IHDR before acTL");
2880 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2882 png_warning(png_ptr, "Invalid acTL after IDAT skipped");
2883 png_crc_finish(png_ptr, length);
2884 return;
2886 else if ((png_ptr->mode & PNG_HAVE_acTL) != 0)
2888 png_warning(png_ptr, "Duplicate acTL skipped");
2889 png_crc_finish(png_ptr, length);
2890 return;
2892 else if (length != 8)
2894 png_warning(png_ptr, "acTL with invalid length skipped");
2895 png_crc_finish(png_ptr, length);
2896 return;
2899 png_crc_read(png_ptr, data, 8);
2900 png_crc_finish(png_ptr, 0);
2902 num_frames = png_get_uint_31(png_ptr, data);
2903 num_plays = png_get_uint_31(png_ptr, data + 4);
2905 /* the set function will do error checking on num_frames */
2906 didSet = png_set_acTL(png_ptr, info_ptr, num_frames, num_plays);
2907 if (didSet != 0)
2908 png_ptr->mode |= PNG_HAVE_acTL;
2911 void /* PRIVATE */
2912 png_handle_fcTL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
2914 png_byte data[22];
2915 png_uint_32 width;
2916 png_uint_32 height;
2917 png_uint_32 x_offset;
2918 png_uint_32 y_offset;
2919 png_uint_16 delay_num;
2920 png_uint_16 delay_den;
2921 png_byte dispose_op;
2922 png_byte blend_op;
2924 png_debug(1, "in png_handle_fcTL");
2926 png_ensure_sequence_number(png_ptr, length);
2928 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2930 png_error(png_ptr, "Missing IHDR before fcTL");
2932 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2934 /* for any frames other then the first this message may be misleading,
2935 * but correct. PNG_HAVE_IDAT is unset before the frame head is read
2936 * i can't think of a better message */
2937 png_warning(png_ptr, "Invalid fcTL after IDAT skipped");
2938 png_crc_finish(png_ptr, length-4);
2939 return;
2941 else if ((png_ptr->mode & PNG_HAVE_fcTL) != 0)
2943 png_warning(png_ptr, "Duplicate fcTL within one frame skipped");
2944 png_crc_finish(png_ptr, length-4);
2945 return;
2947 else if (length != 26)
2949 png_warning(png_ptr, "fcTL with invalid length skipped");
2950 png_crc_finish(png_ptr, length-4);
2951 return;
2954 png_crc_read(png_ptr, data, 22);
2955 png_crc_finish(png_ptr, 0);
2957 width = png_get_uint_31(png_ptr, data);
2958 height = png_get_uint_31(png_ptr, data + 4);
2959 x_offset = png_get_uint_31(png_ptr, data + 8);
2960 y_offset = png_get_uint_31(png_ptr, data + 12);
2961 delay_num = png_get_uint_16(data + 16);
2962 delay_den = png_get_uint_16(data + 18);
2963 dispose_op = data[20];
2964 blend_op = data[21];
2966 if (png_ptr->num_frames_read == 0 && (x_offset != 0 || y_offset != 0))
2968 png_warning(png_ptr, "fcTL for the first frame must have zero offset");
2969 return;
2972 if (info_ptr != NULL)
2974 if (png_ptr->num_frames_read == 0 &&
2975 (width != info_ptr->width || height != info_ptr->height))
2977 png_warning(png_ptr, "size in first frame's fcTL must match "
2978 "the size in IHDR");
2979 return;
2982 /* The set function will do more error checking */
2983 png_set_next_frame_fcTL(png_ptr, info_ptr, width, height,
2984 x_offset, y_offset, delay_num, delay_den,
2985 dispose_op, blend_op);
2987 png_read_reinit(png_ptr, info_ptr);
2989 png_ptr->mode |= PNG_HAVE_fcTL;
2993 void /* PRIVATE */
2994 png_have_info(png_structp png_ptr, png_infop info_ptr)
2996 if ((info_ptr->valid & PNG_INFO_acTL) != 0 &&
2997 (info_ptr->valid & PNG_INFO_fcTL) == 0)
2999 png_ptr->apng_flags |= PNG_FIRST_FRAME_HIDDEN;
3000 info_ptr->num_frames++;
3004 void /* PRIVATE */
3005 png_handle_fdAT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
3007 png_ensure_sequence_number(png_ptr, length);
3009 /* This function is only called from png_read_end(), png_read_info(),
3010 * and png_push_read_chunk() which means that:
3011 * - the user doesn't want to read this frame
3012 * - or this is an out-of-place fdAT
3013 * in either case it is safe to ignore the chunk with a warning */
3014 png_warning(png_ptr, "ignoring fdAT chunk");
3015 png_crc_finish(png_ptr, length - 4);
3016 PNG_UNUSED(info_ptr)
3019 void /* PRIVATE */
3020 png_ensure_sequence_number(png_structp png_ptr, png_uint_32 length)
3022 png_byte data[4];
3023 png_uint_32 sequence_number;
3025 if (length < 4)
3026 png_error(png_ptr, "invalid fcTL or fdAT chunk found");
3028 png_crc_read(png_ptr, data, 4);
3029 sequence_number = png_get_uint_31(png_ptr, data);
3031 if (sequence_number != png_ptr->next_seq_num)
3032 png_error(png_ptr, "fcTL or fdAT chunk with out-of-order sequence "
3033 "number found");
3035 png_ptr->next_seq_num++;
3037 #endif /* READ_APNG */
3039 #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
3040 /* Utility function for png_handle_unknown; set up png_ptr::unknown_chunk */
3041 static int
3042 png_cache_unknown_chunk(png_structrp png_ptr, png_uint_32 length)
3044 png_alloc_size_t limit = PNG_SIZE_MAX;
3046 if (png_ptr->unknown_chunk.data != NULL)
3048 png_free(png_ptr, png_ptr->unknown_chunk.data);
3049 png_ptr->unknown_chunk.data = NULL;
3052 # ifdef PNG_SET_USER_LIMITS_SUPPORTED
3053 if (png_ptr->user_chunk_malloc_max > 0 &&
3054 png_ptr->user_chunk_malloc_max < limit)
3055 limit = png_ptr->user_chunk_malloc_max;
3057 # elif PNG_USER_CHUNK_MALLOC_MAX > 0
3058 if (PNG_USER_CHUNK_MALLOC_MAX < limit)
3059 limit = PNG_USER_CHUNK_MALLOC_MAX;
3060 # endif
3062 if (length <= limit)
3064 PNG_CSTRING_FROM_CHUNK(png_ptr->unknown_chunk.name, png_ptr->chunk_name);
3065 /* The following is safe because of the PNG_SIZE_MAX init above */
3066 png_ptr->unknown_chunk.size = (size_t)length/*SAFE*/;
3067 /* 'mode' is a flag array, only the bottom four bits matter here */
3068 png_ptr->unknown_chunk.location = (png_byte)png_ptr->mode/*SAFE*/;
3070 if (length == 0)
3071 png_ptr->unknown_chunk.data = NULL;
3073 else
3075 /* Do a 'warn' here - it is handled below. */
3076 png_ptr->unknown_chunk.data = png_voidcast(png_bytep,
3077 png_malloc_warn(png_ptr, length));
3081 if (png_ptr->unknown_chunk.data == NULL && length > 0)
3083 /* This is benign because we clean up correctly */
3084 png_crc_finish(png_ptr, length);
3085 png_chunk_benign_error(png_ptr, "unknown chunk exceeds memory limits");
3086 return 0;
3089 else
3091 if (length > 0)
3092 png_crc_read(png_ptr, png_ptr->unknown_chunk.data, length);
3093 png_crc_finish(png_ptr, 0);
3094 return 1;
3097 #endif /* READ_UNKNOWN_CHUNKS */
3099 /* Handle an unknown, or known but disabled, chunk */
3100 void /* PRIVATE */
3101 png_handle_unknown(png_structrp png_ptr, png_inforp info_ptr,
3102 png_uint_32 length, int keep)
3104 int handled = 0; /* the chunk was handled */
3106 png_debug(1, "in png_handle_unknown");
3108 #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
3109 /* NOTE: this code is based on the code in libpng-1.4.12 except for fixing
3110 * the bug which meant that setting a non-default behavior for a specific
3111 * chunk would be ignored (the default was always used unless a user
3112 * callback was installed).
3114 * 'keep' is the value from the png_chunk_unknown_handling, the setting for
3115 * this specific chunk_name, if PNG_HANDLE_AS_UNKNOWN_SUPPORTED, if not it
3116 * will always be PNG_HANDLE_CHUNK_AS_DEFAULT and it needs to be set here.
3117 * This is just an optimization to avoid multiple calls to the lookup
3118 * function.
3120 # ifndef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
3121 # ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
3122 keep = png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name);
3123 # endif
3124 # endif
3126 /* One of the following methods will read the chunk or skip it (at least one
3127 * of these is always defined because this is the only way to switch on
3128 * PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
3130 # ifdef PNG_READ_USER_CHUNKS_SUPPORTED
3131 /* The user callback takes precedence over the chunk keep value, but the
3132 * keep value is still required to validate a save of a critical chunk.
3134 if (png_ptr->read_user_chunk_fn != NULL)
3136 if (png_cache_unknown_chunk(png_ptr, length) != 0)
3138 /* Callback to user unknown chunk handler */
3139 int ret = (*(png_ptr->read_user_chunk_fn))(png_ptr,
3140 &png_ptr->unknown_chunk);
3142 /* ret is:
3143 * negative: An error occurred; png_chunk_error will be called.
3144 * zero: The chunk was not handled, the chunk will be discarded
3145 * unless png_set_keep_unknown_chunks has been used to set
3146 * a 'keep' behavior for this particular chunk, in which
3147 * case that will be used. A critical chunk will cause an
3148 * error at this point unless it is to be saved.
3149 * positive: The chunk was handled, libpng will ignore/discard it.
3151 if (ret < 0)
3152 png_chunk_error(png_ptr, "error in user chunk");
3154 else if (ret == 0)
3156 /* If the keep value is 'default' or 'never' override it, but
3157 * still error out on critical chunks unless the keep value is
3158 * 'always' While this is weird it is the behavior in 1.4.12.
3159 * A possible improvement would be to obey the value set for the
3160 * chunk, but this would be an API change that would probably
3161 * damage some applications.
3163 * The png_app_warning below catches the case that matters, where
3164 * the application has not set specific save or ignore for this
3165 * chunk or global save or ignore.
3167 if (keep < PNG_HANDLE_CHUNK_IF_SAFE)
3169 # ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
3170 if (png_ptr->unknown_default < PNG_HANDLE_CHUNK_IF_SAFE)
3172 png_chunk_warning(png_ptr, "Saving unknown chunk:");
3173 png_app_warning(png_ptr,
3174 "forcing save of an unhandled chunk;"
3175 " please call png_set_keep_unknown_chunks");
3176 /* with keep = PNG_HANDLE_CHUNK_IF_SAFE */
3178 # endif
3179 keep = PNG_HANDLE_CHUNK_IF_SAFE;
3183 else /* chunk was handled */
3185 handled = 1;
3186 /* Critical chunks can be safely discarded at this point. */
3187 keep = PNG_HANDLE_CHUNK_NEVER;
3191 else
3192 keep = PNG_HANDLE_CHUNK_NEVER; /* insufficient memory */
3195 else
3196 /* Use the SAVE_UNKNOWN_CHUNKS code or skip the chunk */
3197 # endif /* READ_USER_CHUNKS */
3199 # ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
3201 /* keep is currently just the per-chunk setting, if there was no
3202 * setting change it to the global default now (not that this may
3203 * still be AS_DEFAULT) then obtain the cache of the chunk if required,
3204 * if not simply skip the chunk.
3206 if (keep == PNG_HANDLE_CHUNK_AS_DEFAULT)
3207 keep = png_ptr->unknown_default;
3209 if (keep == PNG_HANDLE_CHUNK_ALWAYS ||
3210 (keep == PNG_HANDLE_CHUNK_IF_SAFE &&
3211 PNG_CHUNK_ANCILLARY(png_ptr->chunk_name)))
3213 if (png_cache_unknown_chunk(png_ptr, length) == 0)
3214 keep = PNG_HANDLE_CHUNK_NEVER;
3217 else
3218 png_crc_finish(png_ptr, length);
3220 # else
3221 # ifndef PNG_READ_USER_CHUNKS_SUPPORTED
3222 # error no method to support READ_UNKNOWN_CHUNKS
3223 # endif
3226 /* If here there is no read callback pointer set and no support is
3227 * compiled in to just save the unknown chunks, so simply skip this
3228 * chunk. If 'keep' is something other than AS_DEFAULT or NEVER then
3229 * the app has erroneously asked for unknown chunk saving when there
3230 * is no support.
3232 if (keep > PNG_HANDLE_CHUNK_NEVER)
3233 png_app_error(png_ptr, "no unknown chunk support available");
3235 png_crc_finish(png_ptr, length);
3237 # endif
3239 # ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
3240 /* Now store the chunk in the chunk list if appropriate, and if the limits
3241 * permit it.
3243 if (keep == PNG_HANDLE_CHUNK_ALWAYS ||
3244 (keep == PNG_HANDLE_CHUNK_IF_SAFE &&
3245 PNG_CHUNK_ANCILLARY(png_ptr->chunk_name)))
3247 # ifdef PNG_USER_LIMITS_SUPPORTED
3248 switch (png_ptr->user_chunk_cache_max)
3250 case 2:
3251 png_ptr->user_chunk_cache_max = 1;
3252 png_chunk_benign_error(png_ptr, "no space in chunk cache");
3253 /* FALLTHROUGH */
3254 case 1:
3255 /* NOTE: prior to 1.6.0 this case resulted in an unknown critical
3256 * chunk being skipped, now there will be a hard error below.
3258 break;
3260 default: /* not at limit */
3261 --(png_ptr->user_chunk_cache_max);
3262 /* FALLTHROUGH */
3263 case 0: /* no limit */
3264 # endif /* USER_LIMITS */
3265 /* Here when the limit isn't reached or when limits are compiled
3266 * out; store the chunk.
3268 png_set_unknown_chunks(png_ptr, info_ptr,
3269 &png_ptr->unknown_chunk, 1);
3270 handled = 1;
3271 # ifdef PNG_USER_LIMITS_SUPPORTED
3272 break;
3274 # endif
3276 # else /* no store support: the chunk must be handled by the user callback */
3277 PNG_UNUSED(info_ptr)
3278 # endif
3280 /* Regardless of the error handling below the cached data (if any) can be
3281 * freed now. Notice that the data is not freed if there is a png_error, but
3282 * it will be freed by destroy_read_struct.
3284 if (png_ptr->unknown_chunk.data != NULL)
3285 png_free(png_ptr, png_ptr->unknown_chunk.data);
3286 png_ptr->unknown_chunk.data = NULL;
3288 #else /* !PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */
3289 /* There is no support to read an unknown chunk, so just skip it. */
3290 png_crc_finish(png_ptr, length);
3291 PNG_UNUSED(info_ptr)
3292 PNG_UNUSED(keep)
3293 #endif /* !READ_UNKNOWN_CHUNKS */
3295 /* Check for unhandled critical chunks */
3296 if (handled == 0 && PNG_CHUNK_CRITICAL(png_ptr->chunk_name))
3297 png_chunk_error(png_ptr, "unhandled critical chunk");
3300 /* This function is called to verify that a chunk name is valid.
3301 * This function can't have the "critical chunk check" incorporated
3302 * into it, since in the future we will need to be able to call user
3303 * functions to handle unknown critical chunks after we check that
3304 * the chunk name itself is valid.
3307 /* Bit hacking: the test for an invalid byte in the 4 byte chunk name is:
3309 * ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
3312 void /* PRIVATE */
3313 png_check_chunk_name(png_const_structrp png_ptr, png_uint_32 chunk_name)
3315 int i;
3316 png_uint_32 cn=chunk_name;
3318 png_debug(1, "in png_check_chunk_name");
3320 for (i=1; i<=4; ++i)
3322 int c = cn & 0xff;
3324 if (c < 65 || c > 122 || (c > 90 && c < 97))
3325 png_chunk_error(png_ptr, "invalid chunk type");
3327 cn >>= 8;
3331 void /* PRIVATE */
3332 png_check_chunk_length(png_const_structrp png_ptr, png_uint_32 length)
3334 png_alloc_size_t limit = PNG_UINT_31_MAX;
3336 # ifdef PNG_SET_USER_LIMITS_SUPPORTED
3337 if (png_ptr->user_chunk_malloc_max > 0 &&
3338 png_ptr->user_chunk_malloc_max < limit)
3339 limit = png_ptr->user_chunk_malloc_max;
3340 # elif PNG_USER_CHUNK_MALLOC_MAX > 0
3341 if (PNG_USER_CHUNK_MALLOC_MAX < limit)
3342 limit = PNG_USER_CHUNK_MALLOC_MAX;
3343 # endif
3344 #ifdef PNG_READ_APNG_SUPPORTED
3345 if (png_ptr->chunk_name == png_IDAT || png_ptr->chunk_name == png_fdAT)
3346 #else
3347 if (png_ptr->chunk_name == png_IDAT)
3348 #endif
3350 png_alloc_size_t idat_limit = PNG_UINT_31_MAX;
3351 size_t row_factor =
3352 (size_t)png_ptr->width
3353 * (size_t)png_ptr->channels
3354 * (png_ptr->bit_depth > 8? 2: 1)
3356 + (png_ptr->interlaced? 6: 0);
3357 if (png_ptr->height > PNG_UINT_32_MAX/row_factor)
3358 idat_limit = PNG_UINT_31_MAX;
3359 else
3360 idat_limit = png_ptr->height * row_factor;
3361 row_factor = row_factor > 32566? 32566 : row_factor;
3362 idat_limit += 6 + 5*(idat_limit/row_factor+1); /* zlib+deflate overhead */
3363 idat_limit=idat_limit < PNG_UINT_31_MAX? idat_limit : PNG_UINT_31_MAX;
3364 limit = limit < idat_limit? idat_limit : limit;
3367 if (length > limit)
3369 png_debug2(0," length = %lu, limit = %lu",
3370 (unsigned long)length,(unsigned long)limit);
3371 png_benign_error(png_ptr, "chunk data is too large");
3375 /* Combines the row recently read in with the existing pixels in the row. This
3376 * routine takes care of alpha and transparency if requested. This routine also
3377 * handles the two methods of progressive display of interlaced images,
3378 * depending on the 'display' value; if 'display' is true then the whole row
3379 * (dp) is filled from the start by replicating the available pixels. If
3380 * 'display' is false only those pixels present in the pass are filled in.
3382 void /* PRIVATE */
3383 png_combine_row(png_const_structrp png_ptr, png_bytep dp, int display)
3385 unsigned int pixel_depth = png_ptr->transformed_pixel_depth;
3386 png_const_bytep sp = png_ptr->row_buf + 1;
3387 png_alloc_size_t row_width = png_ptr->width;
3388 unsigned int pass = png_ptr->pass;
3389 png_bytep end_ptr = 0;
3390 png_byte end_byte = 0;
3391 unsigned int end_mask;
3393 png_debug(1, "in png_combine_row");
3395 /* Added in 1.5.6: it should not be possible to enter this routine until at
3396 * least one row has been read from the PNG data and transformed.
3398 if (pixel_depth == 0)
3399 png_error(png_ptr, "internal row logic error");
3401 /* Added in 1.5.4: the pixel depth should match the information returned by
3402 * any call to png_read_update_info at this point. Do not continue if we got
3403 * this wrong.
3405 if (png_ptr->info_rowbytes != 0 && png_ptr->info_rowbytes !=
3406 PNG_ROWBYTES(pixel_depth, row_width))
3407 png_error(png_ptr, "internal row size calculation error");
3409 /* Don't expect this to ever happen: */
3410 if (row_width == 0)
3411 png_error(png_ptr, "internal row width error");
3413 /* Preserve the last byte in cases where only part of it will be overwritten,
3414 * the multiply below may overflow, we don't care because ANSI-C guarantees
3415 * we get the low bits.
3417 end_mask = (pixel_depth * row_width) & 7;
3418 if (end_mask != 0)
3420 /* end_ptr == NULL is a flag to say do nothing */
3421 end_ptr = dp + PNG_ROWBYTES(pixel_depth, row_width) - 1;
3422 end_byte = *end_ptr;
3423 # ifdef PNG_READ_PACKSWAP_SUPPORTED
3424 if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
3425 /* little-endian byte */
3426 end_mask = (unsigned int)(0xff << end_mask);
3428 else /* big-endian byte */
3429 # endif
3430 end_mask = 0xff >> end_mask;
3431 /* end_mask is now the bits to *keep* from the destination row */
3434 /* For non-interlaced images this reduces to a memcpy(). A memcpy()
3435 * will also happen if interlacing isn't supported or if the application
3436 * does not call png_set_interlace_handling(). In the latter cases the
3437 * caller just gets a sequence of the unexpanded rows from each interlace
3438 * pass.
3440 #ifdef PNG_READ_INTERLACING_SUPPORTED
3441 if (png_ptr->interlaced != 0 &&
3442 (png_ptr->transformations & PNG_INTERLACE) != 0 &&
3443 pass < 6 && (display == 0 ||
3444 /* The following copies everything for 'display' on passes 0, 2 and 4. */
3445 (display == 1 && (pass & 1) != 0)))
3447 /* Narrow images may have no bits in a pass; the caller should handle
3448 * this, but this test is cheap:
3450 if (row_width <= PNG_PASS_START_COL(pass))
3451 return;
3453 if (pixel_depth < 8)
3455 /* For pixel depths up to 4 bpp the 8-pixel mask can be expanded to fit
3456 * into 32 bits, then a single loop over the bytes using the four byte
3457 * values in the 32-bit mask can be used. For the 'display' option the
3458 * expanded mask may also not require any masking within a byte. To
3459 * make this work the PACKSWAP option must be taken into account - it
3460 * simply requires the pixels to be reversed in each byte.
3462 * The 'regular' case requires a mask for each of the first 6 passes,
3463 * the 'display' case does a copy for the even passes in the range
3464 * 0..6. This has already been handled in the test above.
3466 * The masks are arranged as four bytes with the first byte to use in
3467 * the lowest bits (little-endian) regardless of the order (PACKSWAP or
3468 * not) of the pixels in each byte.
3470 * NOTE: the whole of this logic depends on the caller of this function
3471 * only calling it on rows appropriate to the pass. This function only
3472 * understands the 'x' logic; the 'y' logic is handled by the caller.
3474 * The following defines allow generation of compile time constant bit
3475 * masks for each pixel depth and each possibility of swapped or not
3476 * swapped bytes. Pass 'p' is in the range 0..6; 'x', a pixel index,
3477 * is in the range 0..7; and the result is 1 if the pixel is to be
3478 * copied in the pass, 0 if not. 'S' is for the sparkle method, 'B'
3479 * for the block method.
3481 * With some compilers a compile time expression of the general form:
3483 * (shift >= 32) ? (a >> (shift-32)) : (b >> shift)
3485 * Produces warnings with values of 'shift' in the range 33 to 63
3486 * because the right hand side of the ?: expression is evaluated by
3487 * the compiler even though it isn't used. Microsoft Visual C (various
3488 * versions) and the Intel C compiler are known to do this. To avoid
3489 * this the following macros are used in 1.5.6. This is a temporary
3490 * solution to avoid destabilizing the code during the release process.
3492 # if PNG_USE_COMPILE_TIME_MASKS
3493 # define PNG_LSR(x,s) ((x)>>((s) & 0x1f))
3494 # define PNG_LSL(x,s) ((x)<<((s) & 0x1f))
3495 # else
3496 # define PNG_LSR(x,s) ((x)>>(s))
3497 # define PNG_LSL(x,s) ((x)<<(s))
3498 # endif
3499 # define S_COPY(p,x) (((p)<4 ? PNG_LSR(0x80088822,(3-(p))*8+(7-(x))) :\
3500 PNG_LSR(0xaa55ff00,(7-(p))*8+(7-(x)))) & 1)
3501 # define B_COPY(p,x) (((p)<4 ? PNG_LSR(0xff0fff33,(3-(p))*8+(7-(x))) :\
3502 PNG_LSR(0xff55ff00,(7-(p))*8+(7-(x)))) & 1)
3504 /* Return a mask for pass 'p' pixel 'x' at depth 'd'. The mask is
3505 * little endian - the first pixel is at bit 0 - however the extra
3506 * parameter 's' can be set to cause the mask position to be swapped
3507 * within each byte, to match the PNG format. This is done by XOR of
3508 * the shift with 7, 6 or 4 for bit depths 1, 2 and 4.
3510 # define PIXEL_MASK(p,x,d,s) \
3511 (PNG_LSL(((PNG_LSL(1U,(d)))-1),(((x)*(d))^((s)?8-(d):0))))
3513 /* Hence generate the appropriate 'block' or 'sparkle' pixel copy mask.
3515 # define S_MASKx(p,x,d,s) (S_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
3516 # define B_MASKx(p,x,d,s) (B_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
3518 /* Combine 8 of these to get the full mask. For the 1-bpp and 2-bpp
3519 * cases the result needs replicating, for the 4-bpp case the above
3520 * generates a full 32 bits.
3522 # define MASK_EXPAND(m,d) ((m)*((d)==1?0x01010101:((d)==2?0x00010001:1)))
3524 # define S_MASK(p,d,s) MASK_EXPAND(S_MASKx(p,0,d,s) + S_MASKx(p,1,d,s) +\
3525 S_MASKx(p,2,d,s) + S_MASKx(p,3,d,s) + S_MASKx(p,4,d,s) +\
3526 S_MASKx(p,5,d,s) + S_MASKx(p,6,d,s) + S_MASKx(p,7,d,s), d)
3528 # define B_MASK(p,d,s) MASK_EXPAND(B_MASKx(p,0,d,s) + B_MASKx(p,1,d,s) +\
3529 B_MASKx(p,2,d,s) + B_MASKx(p,3,d,s) + B_MASKx(p,4,d,s) +\
3530 B_MASKx(p,5,d,s) + B_MASKx(p,6,d,s) + B_MASKx(p,7,d,s), d)
3532 #if PNG_USE_COMPILE_TIME_MASKS
3533 /* Utility macros to construct all the masks for a depth/swap
3534 * combination. The 's' parameter says whether the format is PNG
3535 * (big endian bytes) or not. Only the three odd-numbered passes are
3536 * required for the display/block algorithm.
3538 # define S_MASKS(d,s) { S_MASK(0,d,s), S_MASK(1,d,s), S_MASK(2,d,s),\
3539 S_MASK(3,d,s), S_MASK(4,d,s), S_MASK(5,d,s) }
3541 # define B_MASKS(d,s) { B_MASK(1,d,s), B_MASK(3,d,s), B_MASK(5,d,s) }
3543 # define DEPTH_INDEX(d) ((d)==1?0:((d)==2?1:2))
3545 /* Hence the pre-compiled masks indexed by PACKSWAP (or not), depth and
3546 * then pass:
3548 static const png_uint_32 row_mask[2/*PACKSWAP*/][3/*depth*/][6] =
3550 /* Little-endian byte masks for PACKSWAP */
3551 { S_MASKS(1,0), S_MASKS(2,0), S_MASKS(4,0) },
3552 /* Normal (big-endian byte) masks - PNG format */
3553 { S_MASKS(1,1), S_MASKS(2,1), S_MASKS(4,1) }
3556 /* display_mask has only three entries for the odd passes, so index by
3557 * pass>>1.
3559 static const png_uint_32 display_mask[2][3][3] =
3561 /* Little-endian byte masks for PACKSWAP */
3562 { B_MASKS(1,0), B_MASKS(2,0), B_MASKS(4,0) },
3563 /* Normal (big-endian byte) masks - PNG format */
3564 { B_MASKS(1,1), B_MASKS(2,1), B_MASKS(4,1) }
3567 # define MASK(pass,depth,display,png)\
3568 ((display)?display_mask[png][DEPTH_INDEX(depth)][pass>>1]:\
3569 row_mask[png][DEPTH_INDEX(depth)][pass])
3571 #else /* !PNG_USE_COMPILE_TIME_MASKS */
3572 /* This is the runtime alternative: it seems unlikely that this will
3573 * ever be either smaller or faster than the compile time approach.
3575 # define MASK(pass,depth,display,png)\
3576 ((display)?B_MASK(pass,depth,png):S_MASK(pass,depth,png))
3577 #endif /* !USE_COMPILE_TIME_MASKS */
3579 /* Use the appropriate mask to copy the required bits. In some cases
3580 * the byte mask will be 0 or 0xff; optimize these cases. row_width is
3581 * the number of pixels, but the code copies bytes, so it is necessary
3582 * to special case the end.
3584 png_uint_32 pixels_per_byte = 8 / pixel_depth;
3585 png_uint_32 mask;
3587 # ifdef PNG_READ_PACKSWAP_SUPPORTED
3588 if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
3589 mask = MASK(pass, pixel_depth, display, 0);
3591 else
3592 # endif
3593 mask = MASK(pass, pixel_depth, display, 1);
3595 for (;;)
3597 png_uint_32 m;
3599 /* It doesn't matter in the following if png_uint_32 has more than
3600 * 32 bits because the high bits always match those in m<<24; it is,
3601 * however, essential to use OR here, not +, because of this.
3603 m = mask;
3604 mask = (m >> 8) | (m << 24); /* rotate right to good compilers */
3605 m &= 0xff;
3607 if (m != 0) /* something to copy */
3609 if (m != 0xff)
3610 *dp = (png_byte)((*dp & ~m) | (*sp & m));
3611 else
3612 *dp = *sp;
3615 /* NOTE: this may overwrite the last byte with garbage if the image
3616 * is not an exact number of bytes wide; libpng has always done
3617 * this.
3619 if (row_width <= pixels_per_byte)
3620 break; /* May need to restore part of the last byte */
3622 row_width -= pixels_per_byte;
3623 ++dp;
3624 ++sp;
3628 else /* pixel_depth >= 8 */
3630 unsigned int bytes_to_copy, bytes_to_jump;
3632 /* Validate the depth - it must be a multiple of 8 */
3633 if (pixel_depth & 7)
3634 png_error(png_ptr, "invalid user transform pixel depth");
3636 pixel_depth >>= 3; /* now in bytes */
3637 row_width *= pixel_depth;
3639 /* Regardless of pass number the Adam 7 interlace always results in a
3640 * fixed number of pixels to copy then to skip. There may be a
3641 * different number of pixels to skip at the start though.
3644 unsigned int offset = PNG_PASS_START_COL(pass) * pixel_depth;
3646 row_width -= offset;
3647 dp += offset;
3648 sp += offset;
3651 /* Work out the bytes to copy. */
3652 if (display != 0)
3654 /* When doing the 'block' algorithm the pixel in the pass gets
3655 * replicated to adjacent pixels. This is why the even (0,2,4,6)
3656 * passes are skipped above - the entire expanded row is copied.
3658 bytes_to_copy = (1<<((6-pass)>>1)) * pixel_depth;
3660 /* But don't allow this number to exceed the actual row width. */
3661 if (bytes_to_copy > row_width)
3662 bytes_to_copy = (unsigned int)/*SAFE*/row_width;
3665 else /* normal row; Adam7 only ever gives us one pixel to copy. */
3666 bytes_to_copy = pixel_depth;
3668 /* In Adam7 there is a constant offset between where the pixels go. */
3669 bytes_to_jump = PNG_PASS_COL_OFFSET(pass) * pixel_depth;
3671 /* And simply copy these bytes. Some optimization is possible here,
3672 * depending on the value of 'bytes_to_copy'. Special case the low
3673 * byte counts, which we know to be frequent.
3675 * Notice that these cases all 'return' rather than 'break' - this
3676 * avoids an unnecessary test on whether to restore the last byte
3677 * below.
3679 switch (bytes_to_copy)
3681 case 1:
3682 for (;;)
3684 *dp = *sp;
3686 if (row_width <= bytes_to_jump)
3687 return;
3689 dp += bytes_to_jump;
3690 sp += bytes_to_jump;
3691 row_width -= bytes_to_jump;
3694 case 2:
3695 /* There is a possibility of a partial copy at the end here; this
3696 * slows the code down somewhat.
3700 dp[0] = sp[0]; dp[1] = sp[1];
3702 if (row_width <= bytes_to_jump)
3703 return;
3705 sp += bytes_to_jump;
3706 dp += bytes_to_jump;
3707 row_width -= bytes_to_jump;
3709 while (row_width > 1);
3711 /* And there can only be one byte left at this point: */
3712 *dp = *sp;
3713 return;
3715 case 3:
3716 /* This can only be the RGB case, so each copy is exactly one
3717 * pixel and it is not necessary to check for a partial copy.
3719 for (;;)
3721 dp[0] = sp[0]; dp[1] = sp[1]; dp[2] = sp[2];
3723 if (row_width <= bytes_to_jump)
3724 return;
3726 sp += bytes_to_jump;
3727 dp += bytes_to_jump;
3728 row_width -= bytes_to_jump;
3731 default:
3732 #if PNG_ALIGN_TYPE != PNG_ALIGN_NONE
3733 /* Check for double byte alignment and, if possible, use a
3734 * 16-bit copy. Don't attempt this for narrow images - ones that
3735 * are less than an interlace panel wide. Don't attempt it for
3736 * wide bytes_to_copy either - use the memcpy there.
3738 if (bytes_to_copy < 16 /*else use memcpy*/ &&
3739 png_isaligned(dp, png_uint_16) &&
3740 png_isaligned(sp, png_uint_16) &&
3741 bytes_to_copy % (sizeof (png_uint_16)) == 0 &&
3742 bytes_to_jump % (sizeof (png_uint_16)) == 0)
3744 /* Everything is aligned for png_uint_16 copies, but try for
3745 * png_uint_32 first.
3747 if (png_isaligned(dp, png_uint_32) &&
3748 png_isaligned(sp, png_uint_32) &&
3749 bytes_to_copy % (sizeof (png_uint_32)) == 0 &&
3750 bytes_to_jump % (sizeof (png_uint_32)) == 0)
3752 png_uint_32p dp32 = png_aligncast(png_uint_32p,dp);
3753 png_const_uint_32p sp32 = png_aligncastconst(
3754 png_const_uint_32p, sp);
3755 size_t skip = (bytes_to_jump-bytes_to_copy) /
3756 (sizeof (png_uint_32));
3760 size_t c = bytes_to_copy;
3763 *dp32++ = *sp32++;
3764 c -= (sizeof (png_uint_32));
3766 while (c > 0);
3768 if (row_width <= bytes_to_jump)
3769 return;
3771 dp32 += skip;
3772 sp32 += skip;
3773 row_width -= bytes_to_jump;
3775 while (bytes_to_copy <= row_width);
3777 /* Get to here when the row_width truncates the final copy.
3778 * There will be 1-3 bytes left to copy, so don't try the
3779 * 16-bit loop below.
3781 dp = (png_bytep)dp32;
3782 sp = (png_const_bytep)sp32;
3784 *dp++ = *sp++;
3785 while (--row_width > 0);
3786 return;
3789 /* Else do it in 16-bit quantities, but only if the size is
3790 * not too large.
3792 else
3794 png_uint_16p dp16 = png_aligncast(png_uint_16p, dp);
3795 png_const_uint_16p sp16 = png_aligncastconst(
3796 png_const_uint_16p, sp);
3797 size_t skip = (bytes_to_jump-bytes_to_copy) /
3798 (sizeof (png_uint_16));
3802 size_t c = bytes_to_copy;
3805 *dp16++ = *sp16++;
3806 c -= (sizeof (png_uint_16));
3808 while (c > 0);
3810 if (row_width <= bytes_to_jump)
3811 return;
3813 dp16 += skip;
3814 sp16 += skip;
3815 row_width -= bytes_to_jump;
3817 while (bytes_to_copy <= row_width);
3819 /* End of row - 1 byte left, bytes_to_copy > row_width: */
3820 dp = (png_bytep)dp16;
3821 sp = (png_const_bytep)sp16;
3823 *dp++ = *sp++;
3824 while (--row_width > 0);
3825 return;
3828 #endif /* ALIGN_TYPE code */
3830 /* The true default - use a memcpy: */
3831 for (;;)
3833 memcpy(dp, sp, bytes_to_copy);
3835 if (row_width <= bytes_to_jump)
3836 return;
3838 sp += bytes_to_jump;
3839 dp += bytes_to_jump;
3840 row_width -= bytes_to_jump;
3841 if (bytes_to_copy > row_width)
3842 bytes_to_copy = (unsigned int)/*SAFE*/row_width;
3846 /* NOT REACHED*/
3847 } /* pixel_depth >= 8 */
3849 /* Here if pixel_depth < 8 to check 'end_ptr' below. */
3851 else
3852 #endif /* READ_INTERLACING */
3854 /* If here then the switch above wasn't used so just memcpy the whole row
3855 * from the temporary row buffer (notice that this overwrites the end of the
3856 * destination row if it is a partial byte.)
3858 memcpy(dp, sp, PNG_ROWBYTES(pixel_depth, row_width));
3860 /* Restore the overwritten bits from the last byte if necessary. */
3861 if (end_ptr != NULL)
3862 *end_ptr = (png_byte)((end_byte & end_mask) | (*end_ptr & ~end_mask));
3865 #ifdef PNG_READ_INTERLACING_SUPPORTED
3866 void /* PRIVATE */
3867 png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
3868 png_uint_32 transformations /* Because these may affect the byte layout */)
3870 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
3871 /* Offset to next interlace block */
3872 static const unsigned int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
3874 png_debug(1, "in png_do_read_interlace");
3875 if (row != NULL && row_info != NULL)
3877 png_uint_32 final_width;
3879 final_width = row_info->width * png_pass_inc[pass];
3881 switch (row_info->pixel_depth)
3883 case 1:
3885 png_bytep sp = row + (size_t)((row_info->width - 1) >> 3);
3886 png_bytep dp = row + (size_t)((final_width - 1) >> 3);
3887 unsigned int sshift, dshift;
3888 unsigned int s_start, s_end;
3889 int s_inc;
3890 int jstop = (int)png_pass_inc[pass];
3891 png_byte v;
3892 png_uint_32 i;
3893 int j;
3895 #ifdef PNG_READ_PACKSWAP_SUPPORTED
3896 if ((transformations & PNG_PACKSWAP) != 0)
3898 sshift = ((row_info->width + 7) & 0x07);
3899 dshift = ((final_width + 7) & 0x07);
3900 s_start = 7;
3901 s_end = 0;
3902 s_inc = -1;
3905 else
3906 #endif
3908 sshift = 7 - ((row_info->width + 7) & 0x07);
3909 dshift = 7 - ((final_width + 7) & 0x07);
3910 s_start = 0;
3911 s_end = 7;
3912 s_inc = 1;
3915 for (i = 0; i < row_info->width; i++)
3917 v = (png_byte)((*sp >> sshift) & 0x01);
3918 for (j = 0; j < jstop; j++)
3920 unsigned int tmp = *dp & (0x7f7f >> (7 - dshift));
3921 tmp |= (unsigned int)(v << dshift);
3922 *dp = (png_byte)(tmp & 0xff);
3924 if (dshift == s_end)
3926 dshift = s_start;
3927 dp--;
3930 else
3931 dshift = (unsigned int)((int)dshift + s_inc);
3934 if (sshift == s_end)
3936 sshift = s_start;
3937 sp--;
3940 else
3941 sshift = (unsigned int)((int)sshift + s_inc);
3943 break;
3946 case 2:
3948 png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);
3949 png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);
3950 unsigned int sshift, dshift;
3951 unsigned int s_start, s_end;
3952 int s_inc;
3953 int jstop = (int)png_pass_inc[pass];
3954 png_uint_32 i;
3956 #ifdef PNG_READ_PACKSWAP_SUPPORTED
3957 if ((transformations & PNG_PACKSWAP) != 0)
3959 sshift = (((row_info->width + 3) & 0x03) << 1);
3960 dshift = (((final_width + 3) & 0x03) << 1);
3961 s_start = 6;
3962 s_end = 0;
3963 s_inc = -2;
3966 else
3967 #endif
3969 sshift = ((3 - ((row_info->width + 3) & 0x03)) << 1);
3970 dshift = ((3 - ((final_width + 3) & 0x03)) << 1);
3971 s_start = 0;
3972 s_end = 6;
3973 s_inc = 2;
3976 for (i = 0; i < row_info->width; i++)
3978 png_byte v;
3979 int j;
3981 v = (png_byte)((*sp >> sshift) & 0x03);
3982 for (j = 0; j < jstop; j++)
3984 unsigned int tmp = *dp & (0x3f3f >> (6 - dshift));
3985 tmp |= (unsigned int)(v << dshift);
3986 *dp = (png_byte)(tmp & 0xff);
3988 if (dshift == s_end)
3990 dshift = s_start;
3991 dp--;
3994 else
3995 dshift = (unsigned int)((int)dshift + s_inc);
3998 if (sshift == s_end)
4000 sshift = s_start;
4001 sp--;
4004 else
4005 sshift = (unsigned int)((int)sshift + s_inc);
4007 break;
4010 case 4:
4012 png_bytep sp = row + (size_t)((row_info->width - 1) >> 1);
4013 png_bytep dp = row + (size_t)((final_width - 1) >> 1);
4014 unsigned int sshift, dshift;
4015 unsigned int s_start, s_end;
4016 int s_inc;
4017 png_uint_32 i;
4018 int jstop = (int)png_pass_inc[pass];
4020 #ifdef PNG_READ_PACKSWAP_SUPPORTED
4021 if ((transformations & PNG_PACKSWAP) != 0)
4023 sshift = (((row_info->width + 1) & 0x01) << 2);
4024 dshift = (((final_width + 1) & 0x01) << 2);
4025 s_start = 4;
4026 s_end = 0;
4027 s_inc = -4;
4030 else
4031 #endif
4033 sshift = ((1 - ((row_info->width + 1) & 0x01)) << 2);
4034 dshift = ((1 - ((final_width + 1) & 0x01)) << 2);
4035 s_start = 0;
4036 s_end = 4;
4037 s_inc = 4;
4040 for (i = 0; i < row_info->width; i++)
4042 png_byte v = (png_byte)((*sp >> sshift) & 0x0f);
4043 int j;
4045 for (j = 0; j < jstop; j++)
4047 unsigned int tmp = *dp & (0xf0f >> (4 - dshift));
4048 tmp |= (unsigned int)(v << dshift);
4049 *dp = (png_byte)(tmp & 0xff);
4051 if (dshift == s_end)
4053 dshift = s_start;
4054 dp--;
4057 else
4058 dshift = (unsigned int)((int)dshift + s_inc);
4061 if (sshift == s_end)
4063 sshift = s_start;
4064 sp--;
4067 else
4068 sshift = (unsigned int)((int)sshift + s_inc);
4070 break;
4073 default:
4075 size_t pixel_bytes = (row_info->pixel_depth >> 3);
4077 png_bytep sp = row + (size_t)(row_info->width - 1)
4078 * pixel_bytes;
4080 png_bytep dp = row + (size_t)(final_width - 1) * pixel_bytes;
4082 int jstop = (int)png_pass_inc[pass];
4083 png_uint_32 i;
4085 for (i = 0; i < row_info->width; i++)
4087 png_byte v[8]; /* SAFE; pixel_depth does not exceed 64 */
4088 int j;
4090 memcpy(v, sp, pixel_bytes);
4092 for (j = 0; j < jstop; j++)
4094 memcpy(dp, v, pixel_bytes);
4095 dp -= pixel_bytes;
4098 sp -= pixel_bytes;
4100 break;
4104 row_info->width = final_width;
4105 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width);
4107 #ifndef PNG_READ_PACKSWAP_SUPPORTED
4108 PNG_UNUSED(transformations) /* Silence compiler warning */
4109 #endif
4111 #endif /* READ_INTERLACING */
4113 static void
4114 png_read_filter_row_sub(png_row_infop row_info, png_bytep row,
4115 png_const_bytep prev_row)
4117 size_t i;
4118 size_t istop = row_info->rowbytes;
4119 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
4120 png_bytep rp = row + bpp;
4122 PNG_UNUSED(prev_row)
4124 for (i = bpp; i < istop; i++)
4126 *rp = (png_byte)(((int)(*rp) + (int)(*(rp-bpp))) & 0xff);
4127 rp++;
4131 static void
4132 png_read_filter_row_up(png_row_infop row_info, png_bytep row,
4133 png_const_bytep prev_row)
4135 size_t i;
4136 size_t istop = row_info->rowbytes;
4137 png_bytep rp = row;
4138 png_const_bytep pp = prev_row;
4140 for (i = 0; i < istop; i++)
4142 *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
4143 rp++;
4147 static void
4148 png_read_filter_row_avg(png_row_infop row_info, png_bytep row,
4149 png_const_bytep prev_row)
4151 size_t i;
4152 png_bytep rp = row;
4153 png_const_bytep pp = prev_row;
4154 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
4155 size_t istop = row_info->rowbytes - bpp;
4157 for (i = 0; i < bpp; i++)
4159 *rp = (png_byte)(((int)(*rp) +
4160 ((int)(*pp++) / 2 )) & 0xff);
4162 rp++;
4165 for (i = 0; i < istop; i++)
4167 *rp = (png_byte)(((int)(*rp) +
4168 (int)(*pp++ + *(rp-bpp)) / 2 ) & 0xff);
4170 rp++;
4174 static void
4175 png_read_filter_row_paeth_1byte_pixel(png_row_infop row_info, png_bytep row,
4176 png_const_bytep prev_row)
4178 png_bytep rp_end = row + row_info->rowbytes;
4179 int a, c;
4181 /* First pixel/byte */
4182 c = *prev_row++;
4183 a = *row + c;
4184 *row++ = (png_byte)a;
4186 /* Remainder */
4187 while (row < rp_end)
4189 int b, pa, pb, pc, p;
4191 a &= 0xff; /* From previous iteration or start */
4192 b = *prev_row++;
4194 p = b - c;
4195 pc = a - c;
4197 #ifdef PNG_USE_ABS
4198 pa = abs(p);
4199 pb = abs(pc);
4200 pc = abs(p + pc);
4201 #else
4202 pa = p < 0 ? -p : p;
4203 pb = pc < 0 ? -pc : pc;
4204 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
4205 #endif
4207 /* Find the best predictor, the least of pa, pb, pc favoring the earlier
4208 * ones in the case of a tie.
4210 if (pb < pa)
4212 pa = pb; a = b;
4214 if (pc < pa) a = c;
4216 /* Calculate the current pixel in a, and move the previous row pixel to c
4217 * for the next time round the loop
4219 c = b;
4220 a += *row;
4221 *row++ = (png_byte)a;
4225 static void
4226 png_read_filter_row_paeth_multibyte_pixel(png_row_infop row_info, png_bytep row,
4227 png_const_bytep prev_row)
4229 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
4230 png_bytep rp_end = row + bpp;
4232 /* Process the first pixel in the row completely (this is the same as 'up'
4233 * because there is only one candidate predictor for the first row).
4235 while (row < rp_end)
4237 int a = *row + *prev_row++;
4238 *row++ = (png_byte)a;
4241 /* Remainder */
4242 rp_end = rp_end + (row_info->rowbytes - bpp);
4244 while (row < rp_end)
4246 int a, b, c, pa, pb, pc, p;
4248 c = *(prev_row - bpp);
4249 a = *(row - bpp);
4250 b = *prev_row++;
4252 p = b - c;
4253 pc = a - c;
4255 #ifdef PNG_USE_ABS
4256 pa = abs(p);
4257 pb = abs(pc);
4258 pc = abs(p + pc);
4259 #else
4260 pa = p < 0 ? -p : p;
4261 pb = pc < 0 ? -pc : pc;
4262 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
4263 #endif
4265 if (pb < pa)
4267 pa = pb; a = b;
4269 if (pc < pa) a = c;
4271 a += *row;
4272 *row++ = (png_byte)a;
4276 static void
4277 png_init_filter_functions(png_structrp pp)
4278 /* This function is called once for every PNG image (except for PNG images
4279 * that only use PNG_FILTER_VALUE_NONE for all rows) to set the
4280 * implementations required to reverse the filtering of PNG rows. Reversing
4281 * the filter is the first transformation performed on the row data. It is
4282 * performed in place, therefore an implementation can be selected based on
4283 * the image pixel format. If the implementation depends on image width then
4284 * take care to ensure that it works correctly if the image is interlaced -
4285 * interlacing causes the actual row width to vary.
4288 unsigned int bpp = (pp->pixel_depth + 7) >> 3;
4290 pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub;
4291 pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up;
4292 pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg;
4293 if (bpp == 1)
4294 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
4295 png_read_filter_row_paeth_1byte_pixel;
4296 else
4297 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
4298 png_read_filter_row_paeth_multibyte_pixel;
4300 #ifdef PNG_FILTER_OPTIMIZATIONS
4301 /* To use this define PNG_FILTER_OPTIMIZATIONS as the name of a function to
4302 * call to install hardware optimizations for the above functions; simply
4303 * replace whatever elements of the pp->read_filter[] array with a hardware
4304 * specific (or, for that matter, generic) optimization.
4306 * To see an example of this examine what configure.ac does when
4307 * --enable-arm-neon is specified on the command line.
4309 PNG_FILTER_OPTIMIZATIONS(pp, bpp);
4310 #endif
4313 void /* PRIVATE */
4314 png_read_filter_row(png_structrp pp, png_row_infop row_info, png_bytep row,
4315 png_const_bytep prev_row, int filter)
4317 /* OPTIMIZATION: DO NOT MODIFY THIS FUNCTION, instead #define
4318 * PNG_FILTER_OPTIMIZATIONS to a function that overrides the generic
4319 * implementations. See png_init_filter_functions above.
4321 if (filter > PNG_FILTER_VALUE_NONE && filter < PNG_FILTER_VALUE_LAST)
4323 if (pp->read_filter[0] == NULL)
4324 png_init_filter_functions(pp);
4326 pp->read_filter[filter-1](row_info, row, prev_row);
4330 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
4331 void /* PRIVATE */
4332 png_read_IDAT_data(png_structrp png_ptr, png_bytep output,
4333 png_alloc_size_t avail_out)
4335 /* Loop reading IDATs and decompressing the result into output[avail_out] */
4336 png_ptr->zstream.next_out = output;
4337 png_ptr->zstream.avail_out = 0; /* safety: set below */
4339 if (output == NULL)
4340 avail_out = 0;
4344 int ret;
4345 png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];
4347 if (png_ptr->zstream.avail_in == 0)
4349 uInt avail_in;
4350 png_bytep buffer;
4352 #ifdef PNG_READ_APNG_SUPPORTED
4353 png_uint_32 bytes_to_skip = 0;
4355 while (png_ptr->idat_size == 0 || bytes_to_skip != 0)
4357 png_crc_finish(png_ptr, bytes_to_skip);
4358 bytes_to_skip = 0;
4360 png_ptr->idat_size = png_read_chunk_header(png_ptr);
4361 if (png_ptr->num_frames_read == 0)
4363 if (png_ptr->chunk_name != png_IDAT)
4364 png_error(png_ptr, "Not enough image data");
4366 else
4368 if (png_ptr->chunk_name == png_IEND)
4369 png_error(png_ptr, "Not enough image data");
4370 if (png_ptr->chunk_name != png_fdAT)
4372 png_warning(png_ptr, "Skipped (ignored) a chunk "
4373 "between APNG chunks");
4374 bytes_to_skip = png_ptr->idat_size;
4375 continue;
4378 png_ensure_sequence_number(png_ptr, png_ptr->idat_size);
4380 png_ptr->idat_size -= 4;
4383 #else
4384 while (png_ptr->idat_size == 0)
4386 png_crc_finish(png_ptr, 0);
4388 png_ptr->idat_size = png_read_chunk_header(png_ptr);
4389 /* This is an error even in the 'check' case because the code just
4390 * consumed a non-IDAT header.
4392 if (png_ptr->chunk_name != png_IDAT)
4393 png_error(png_ptr, "Not enough image data");
4395 #endif /* READ_APNG */
4397 avail_in = png_ptr->IDAT_read_size;
4399 if (avail_in > png_ptr->idat_size)
4400 avail_in = (uInt)png_ptr->idat_size;
4402 /* A PNG with a gradually increasing IDAT size will defeat this attempt
4403 * to minimize memory usage by causing lots of re-allocs, but
4404 * realistically doing IDAT_read_size re-allocs is not likely to be a
4405 * big problem.
4407 buffer = png_read_buffer(png_ptr, avail_in, 0/*error*/);
4409 png_crc_read(png_ptr, buffer, avail_in);
4410 png_ptr->idat_size -= avail_in;
4412 png_ptr->zstream.next_in = buffer;
4413 png_ptr->zstream.avail_in = avail_in;
4416 /* And set up the output side. */
4417 if (output != NULL) /* standard read */
4419 uInt out = ZLIB_IO_MAX;
4421 if (out > avail_out)
4422 out = (uInt)avail_out;
4424 avail_out -= out;
4425 png_ptr->zstream.avail_out = out;
4428 else /* after last row, checking for end */
4430 png_ptr->zstream.next_out = tmpbuf;
4431 png_ptr->zstream.avail_out = (sizeof tmpbuf);
4434 /* Use NO_FLUSH; this gives zlib the maximum opportunity to optimize the
4435 * process. If the LZ stream is truncated the sequential reader will
4436 * terminally damage the stream, above, by reading the chunk header of the
4437 * following chunk (it then exits with png_error).
4439 * TODO: deal more elegantly with truncated IDAT lists.
4441 ret = PNG_INFLATE(png_ptr, Z_NO_FLUSH);
4443 /* Take the unconsumed output back. */
4444 if (output != NULL)
4445 avail_out += png_ptr->zstream.avail_out;
4447 else /* avail_out counts the extra bytes */
4448 avail_out += (sizeof tmpbuf) - png_ptr->zstream.avail_out;
4450 png_ptr->zstream.avail_out = 0;
4452 if (ret == Z_STREAM_END)
4454 /* Do this for safety; we won't read any more into this row. */
4455 png_ptr->zstream.next_out = NULL;
4457 png_ptr->mode |= PNG_AFTER_IDAT;
4458 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
4459 #ifdef PNG_READ_APNG_SUPPORTED
4460 png_ptr->num_frames_read++;
4461 #endif
4463 if (png_ptr->zstream.avail_in > 0 || png_ptr->idat_size > 0)
4464 png_chunk_benign_error(png_ptr, "Extra compressed data");
4465 break;
4468 if (ret != Z_OK)
4470 png_zstream_error(png_ptr, ret);
4472 if (output != NULL)
4473 png_chunk_error(png_ptr, png_ptr->zstream.msg);
4475 else /* checking */
4477 png_chunk_benign_error(png_ptr, png_ptr->zstream.msg);
4478 return;
4481 } while (avail_out > 0);
4483 if (avail_out > 0)
4485 /* The stream ended before the image; this is the same as too few IDATs so
4486 * should be handled the same way.
4488 if (output != NULL)
4489 png_error(png_ptr, "Not enough image data");
4491 else /* the deflate stream contained extra data */
4492 png_chunk_benign_error(png_ptr, "Too much image data");
4496 void /* PRIVATE */
4497 png_read_finish_IDAT(png_structrp png_ptr)
4499 /* We don't need any more data and the stream should have ended, however the
4500 * LZ end code may actually not have been processed. In this case we must
4501 * read it otherwise stray unread IDAT data or, more likely, an IDAT chunk
4502 * may still remain to be consumed.
4504 if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
4506 /* The NULL causes png_read_IDAT_data to swallow any remaining bytes in
4507 * the compressed stream, but the stream may be damaged too, so even after
4508 * this call we may need to terminate the zstream ownership.
4510 png_read_IDAT_data(png_ptr, NULL, 0);
4511 png_ptr->zstream.next_out = NULL; /* safety */
4513 /* Now clear everything out for safety; the following may not have been
4514 * done.
4516 if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
4518 png_ptr->mode |= PNG_AFTER_IDAT;
4519 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
4523 /* If the zstream has not been released do it now *and* terminate the reading
4524 * of the final IDAT chunk.
4526 if (png_ptr->zowner == png_IDAT)
4528 /* Always do this; the pointers otherwise point into the read buffer. */
4529 png_ptr->zstream.next_in = NULL;
4530 png_ptr->zstream.avail_in = 0;
4532 /* Now we no longer own the zstream. */
4533 png_ptr->zowner = 0;
4535 /* The slightly weird semantics of the sequential IDAT reading is that we
4536 * are always in or at the end of an IDAT chunk, so we always need to do a
4537 * crc_finish here. If idat_size is non-zero we also need to read the
4538 * spurious bytes at the end of the chunk now.
4540 (void)png_crc_finish(png_ptr, png_ptr->idat_size);
4544 void /* PRIVATE */
4545 png_read_finish_row(png_structrp png_ptr)
4547 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
4549 /* Start of interlace block */
4550 static const png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
4552 /* Offset to next interlace block */
4553 static const png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
4555 /* Start of interlace block in the y direction */
4556 static const png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
4558 /* Offset to next interlace block in the y direction */
4559 static const png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
4561 png_debug(1, "in png_read_finish_row");
4562 png_ptr->row_number++;
4563 if (png_ptr->row_number < png_ptr->num_rows)
4564 return;
4566 if (png_ptr->interlaced != 0)
4568 png_ptr->row_number = 0;
4570 /* TO DO: don't do this if prev_row isn't needed (requires
4571 * read-ahead of the next row's filter byte.
4573 memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
4577 png_ptr->pass++;
4579 if (png_ptr->pass >= 7)
4580 break;
4582 png_ptr->iwidth = (png_ptr->width +
4583 png_pass_inc[png_ptr->pass] - 1 -
4584 png_pass_start[png_ptr->pass]) /
4585 png_pass_inc[png_ptr->pass];
4587 if ((png_ptr->transformations & PNG_INTERLACE) == 0)
4589 png_ptr->num_rows = (png_ptr->height +
4590 png_pass_yinc[png_ptr->pass] - 1 -
4591 png_pass_ystart[png_ptr->pass]) /
4592 png_pass_yinc[png_ptr->pass];
4595 else /* if (png_ptr->transformations & PNG_INTERLACE) */
4596 break; /* libpng deinterlacing sees every row */
4598 } while (png_ptr->num_rows == 0 || png_ptr->iwidth == 0);
4600 if (png_ptr->pass < 7)
4601 return;
4604 /* Here after at the end of the last row of the last pass. */
4605 png_read_finish_IDAT(png_ptr);
4607 #endif /* SEQUENTIAL_READ */
4609 void /* PRIVATE */
4610 png_read_start_row(png_structrp png_ptr)
4612 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
4614 /* Start of interlace block */
4615 static const png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
4617 /* Offset to next interlace block */
4618 static const png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
4620 /* Start of interlace block in the y direction */
4621 static const png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
4623 /* Offset to next interlace block in the y direction */
4624 static const png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
4626 unsigned int max_pixel_depth;
4627 size_t row_bytes;
4629 png_debug(1, "in png_read_start_row");
4631 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
4632 png_init_read_transformations(png_ptr);
4633 #endif
4634 if (png_ptr->interlaced != 0)
4636 if ((png_ptr->transformations & PNG_INTERLACE) == 0)
4637 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
4638 png_pass_ystart[0]) / png_pass_yinc[0];
4640 else
4641 png_ptr->num_rows = png_ptr->height;
4643 png_ptr->iwidth = (png_ptr->width +
4644 png_pass_inc[png_ptr->pass] - 1 -
4645 png_pass_start[png_ptr->pass]) /
4646 png_pass_inc[png_ptr->pass];
4649 else
4651 png_ptr->num_rows = png_ptr->height;
4652 png_ptr->iwidth = png_ptr->width;
4655 max_pixel_depth = (unsigned int)png_ptr->pixel_depth;
4657 /* WARNING: * png_read_transform_info (pngrtran.c) performs a simpler set of
4658 * calculations to calculate the final pixel depth, then
4659 * png_do_read_transforms actually does the transforms. This means that the
4660 * code which effectively calculates this value is actually repeated in three
4661 * separate places. They must all match. Innocent changes to the order of
4662 * transformations can and will break libpng in a way that causes memory
4663 * overwrites.
4665 * TODO: fix this.
4667 #ifdef PNG_READ_PACK_SUPPORTED
4668 if ((png_ptr->transformations & PNG_PACK) != 0 && png_ptr->bit_depth < 8)
4669 max_pixel_depth = 8;
4670 #endif
4672 #ifdef PNG_READ_EXPAND_SUPPORTED
4673 if ((png_ptr->transformations & PNG_EXPAND) != 0)
4675 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
4677 if (png_ptr->num_trans != 0)
4678 max_pixel_depth = 32;
4680 else
4681 max_pixel_depth = 24;
4684 else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
4686 if (max_pixel_depth < 8)
4687 max_pixel_depth = 8;
4689 if (png_ptr->num_trans != 0)
4690 max_pixel_depth *= 2;
4693 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
4695 if (png_ptr->num_trans != 0)
4697 max_pixel_depth *= 4;
4698 max_pixel_depth /= 3;
4702 #endif
4704 #ifdef PNG_READ_EXPAND_16_SUPPORTED
4705 if ((png_ptr->transformations & PNG_EXPAND_16) != 0)
4707 # ifdef PNG_READ_EXPAND_SUPPORTED
4708 /* In fact it is an error if it isn't supported, but checking is
4709 * the safe way.
4711 if ((png_ptr->transformations & PNG_EXPAND) != 0)
4713 if (png_ptr->bit_depth < 16)
4714 max_pixel_depth *= 2;
4716 else
4717 # endif
4718 png_ptr->transformations &= ~PNG_EXPAND_16;
4720 #endif
4722 #ifdef PNG_READ_FILLER_SUPPORTED
4723 if ((png_ptr->transformations & (PNG_FILLER)) != 0)
4725 if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
4727 if (max_pixel_depth <= 8)
4728 max_pixel_depth = 16;
4730 else
4731 max_pixel_depth = 32;
4734 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB ||
4735 png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
4737 if (max_pixel_depth <= 32)
4738 max_pixel_depth = 32;
4740 else
4741 max_pixel_depth = 64;
4744 #endif
4746 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
4747 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0)
4749 if (
4750 #ifdef PNG_READ_EXPAND_SUPPORTED
4751 (png_ptr->num_trans != 0 &&
4752 (png_ptr->transformations & PNG_EXPAND) != 0) ||
4753 #endif
4754 #ifdef PNG_READ_FILLER_SUPPORTED
4755 (png_ptr->transformations & (PNG_FILLER)) != 0 ||
4756 #endif
4757 png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
4759 if (max_pixel_depth <= 16)
4760 max_pixel_depth = 32;
4762 else
4763 max_pixel_depth = 64;
4766 else
4768 if (max_pixel_depth <= 8)
4770 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4771 max_pixel_depth = 32;
4773 else
4774 max_pixel_depth = 24;
4777 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4778 max_pixel_depth = 64;
4780 else
4781 max_pixel_depth = 48;
4784 #endif
4786 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
4787 defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
4788 if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0)
4790 unsigned int user_pixel_depth = png_ptr->user_transform_depth *
4791 png_ptr->user_transform_channels;
4793 if (user_pixel_depth > max_pixel_depth)
4794 max_pixel_depth = user_pixel_depth;
4796 #endif
4798 /* This value is stored in png_struct and double checked in the row read
4799 * code.
4801 png_ptr->maximum_pixel_depth = (png_byte)max_pixel_depth;
4802 png_ptr->transformed_pixel_depth = 0; /* calculated on demand */
4804 /* Align the width on the next larger 8 pixels. Mainly used
4805 * for interlacing
4807 row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
4808 /* Calculate the maximum bytes needed, adding a byte and a pixel
4809 * for safety's sake
4811 row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) +
4812 1 + ((max_pixel_depth + 7) >> 3U);
4814 #ifdef PNG_MAX_MALLOC_64K
4815 if (row_bytes > (png_uint_32)65536L)
4816 png_error(png_ptr, "This image requires a row greater than 64KB");
4817 #endif
4819 if (row_bytes + 48 > png_ptr->old_big_row_buf_size)
4821 png_free(png_ptr, png_ptr->big_row_buf);
4822 png_free(png_ptr, png_ptr->big_prev_row);
4824 if (png_ptr->interlaced != 0)
4825 png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr,
4826 row_bytes + 48);
4828 else
4829 png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
4831 png_ptr->big_prev_row = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
4833 #ifdef PNG_ALIGNED_MEMORY_SUPPORTED
4834 /* Use 16-byte aligned memory for row_buf with at least 16 bytes
4835 * of padding before and after row_buf; treat prev_row similarly.
4836 * NOTE: the alignment is to the start of the pixels, one beyond the start
4837 * of the buffer, because of the filter byte. Prior to libpng 1.5.6 this
4838 * was incorrect; the filter byte was aligned, which had the exact
4839 * opposite effect of that intended.
4842 png_bytep temp = png_ptr->big_row_buf + 32;
4843 size_t extra = (size_t)temp & 0x0f;
4844 png_ptr->row_buf = temp - extra - 1/*filter byte*/;
4846 temp = png_ptr->big_prev_row + 32;
4847 extra = (size_t)temp & 0x0f;
4848 png_ptr->prev_row = temp - extra - 1/*filter byte*/;
4850 #else
4851 /* Use 31 bytes of padding before and 17 bytes after row_buf. */
4852 png_ptr->row_buf = png_ptr->big_row_buf + 31;
4853 png_ptr->prev_row = png_ptr->big_prev_row + 31;
4854 #endif
4855 png_ptr->old_big_row_buf_size = row_bytes + 48;
4858 #ifdef PNG_MAX_MALLOC_64K
4859 if (png_ptr->rowbytes > 65535)
4860 png_error(png_ptr, "This image requires a row greater than 64KB");
4862 #endif
4863 if (png_ptr->rowbytes > (PNG_SIZE_MAX - 1))
4864 png_error(png_ptr, "Row has too many bytes to allocate in memory");
4866 memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
4868 png_debug1(3, "width = %u,", png_ptr->width);
4869 png_debug1(3, "height = %u,", png_ptr->height);
4870 png_debug1(3, "iwidth = %u,", png_ptr->iwidth);
4871 png_debug1(3, "num_rows = %u,", png_ptr->num_rows);
4872 png_debug1(3, "rowbytes = %lu,", (unsigned long)png_ptr->rowbytes);
4873 png_debug1(3, "irowbytes = %lu",
4874 (unsigned long)PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1);
4876 /* The sequential reader needs a buffer for IDAT, but the progressive reader
4877 * does not, so free the read buffer now regardless; the sequential reader
4878 * reallocates it on demand.
4880 if (png_ptr->read_buffer != NULL)
4882 png_bytep buffer = png_ptr->read_buffer;
4884 png_ptr->read_buffer_size = 0;
4885 png_ptr->read_buffer = NULL;
4886 png_free(png_ptr, buffer);
4889 /* Finally claim the zstream for the inflate of the IDAT data, use the bits
4890 * value from the stream (note that this will result in a fatal error if the
4891 * IDAT stream has a bogus deflate header window_bits value, but this should
4892 * not be happening any longer!)
4894 if (png_inflate_claim(png_ptr, png_IDAT) != Z_OK)
4895 png_error(png_ptr, png_ptr->zstream.msg);
4897 png_ptr->flags |= PNG_FLAG_ROW_INIT;
4900 #ifdef PNG_READ_APNG_SUPPORTED
4901 /* This function is to be called after the main IDAT set has been read and
4902 * before a new IDAT is read. It resets some parts of png_ptr
4903 * to make them usable by the read functions again */
4904 void /* PRIVATE */
4905 png_read_reset(png_structp png_ptr)
4907 png_ptr->mode &= ~PNG_HAVE_IDAT;
4908 png_ptr->mode &= ~PNG_AFTER_IDAT;
4909 png_ptr->row_number = 0;
4910 png_ptr->pass = 0;
4913 void /* PRIVATE */
4914 png_read_reinit(png_structp png_ptr, png_infop info_ptr)
4916 png_ptr->width = info_ptr->next_frame_width;
4917 png_ptr->height = info_ptr->next_frame_height;
4918 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth,png_ptr->width);
4919 png_ptr->info_rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth,
4920 png_ptr->width);
4921 if (png_ptr->prev_row != NULL)
4922 memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
4925 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
4926 /* same as png_read_reset() but for the progressive reader */
4927 void /* PRIVATE */
4928 png_progressive_read_reset(png_structp png_ptr)
4930 #ifdef PNG_READ_INTERLACING_SUPPORTED
4931 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
4933 /* Start of interlace block */
4934 static PNG_CONST png_byte png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
4936 /* Offset to next interlace block */
4937 static PNG_CONST png_byte png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
4939 /* Start of interlace block in the y direction */
4940 static PNG_CONST png_byte png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
4942 /* Offset to next interlace block in the y direction */
4943 static PNG_CONST png_byte png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
4945 if (png_ptr->interlaced != 0)
4947 if ((png_ptr->transformations & PNG_INTERLACE) == 0)
4948 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
4949 png_pass_ystart[0]) / png_pass_yinc[0];
4950 else
4951 png_ptr->num_rows = png_ptr->height;
4953 png_ptr->iwidth = (png_ptr->width +
4954 png_pass_inc[png_ptr->pass] - 1 -
4955 png_pass_start[png_ptr->pass]) /
4956 png_pass_inc[png_ptr->pass];
4958 else
4959 #endif /* READ_INTERLACING */
4961 png_ptr->num_rows = png_ptr->height;
4962 png_ptr->iwidth = png_ptr->width;
4964 png_ptr->flags &= ~PNG_FLAG_ZSTREAM_ENDED;
4965 if (inflateReset(&(png_ptr->zstream)) != Z_OK)
4966 png_error(png_ptr, "inflateReset failed");
4967 png_ptr->zstream.avail_in = 0;
4968 png_ptr->zstream.next_in = 0;
4969 png_ptr->zstream.next_out = png_ptr->row_buf;
4970 png_ptr->zstream.avail_out = (uInt)PNG_ROWBYTES(png_ptr->pixel_depth,
4971 png_ptr->iwidth) + 1;
4973 #endif /* PROGRESSIVE_READ */
4974 #endif /* READ_APNG */
4975 #endif /* READ */