Backed out 10 changesets (bug 1803810) for xpcshell failures on test_import_global...
[gecko.git] / media / libpng / pngrutil.c
blob51e2204000ec4422bb4913ded7ecf16d62474fda
2 /* pngrutil.c - utilities to read a PNG file
4 * Copyright (c) 2018-2022 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))
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 %lx chunk, 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 ((int)(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 #if ZLIB_VERNUM >= 0x1290 && \
425 defined(PNG_SET_OPTION_SUPPORTED) && defined(PNG_IGNORE_ADLER32)
426 if (((png_ptr->options >> PNG_IGNORE_ADLER32) & 3) == PNG_OPTION_ON)
427 /* Turn off validation of the ADLER32 checksum in IDAT chunks */
428 ret = inflateValidate(&png_ptr->zstream, 0);
429 #endif
431 if (ret == Z_OK)
432 png_ptr->zowner = owner;
434 else
435 png_zstream_error(png_ptr, ret);
437 return ret;
440 #ifdef window_bits
441 # undef window_bits
442 #endif
445 #if ZLIB_VERNUM >= 0x1240
446 /* Handle the start of the inflate stream if we called inflateInit2(strm,0);
447 * in this case some zlib versions skip validation of the CINFO field and, in
448 * certain circumstances, libpng may end up displaying an invalid image, in
449 * contrast to implementations that call zlib in the normal way (e.g. libpng
450 * 1.5).
452 int /* PRIVATE */
453 png_zlib_inflate(png_structrp png_ptr, int flush)
455 if (png_ptr->zstream_start && png_ptr->zstream.avail_in > 0)
457 if ((*png_ptr->zstream.next_in >> 4) > 7)
459 png_ptr->zstream.msg = "invalid window size (libpng)";
460 return Z_DATA_ERROR;
463 png_ptr->zstream_start = 0;
466 return inflate(&png_ptr->zstream, flush);
468 #endif /* Zlib >= 1.2.4 */
470 #ifdef PNG_READ_COMPRESSED_TEXT_SUPPORTED
471 #if defined(PNG_READ_zTXt_SUPPORTED) || defined (PNG_READ_iTXt_SUPPORTED)
472 /* png_inflate now returns zlib error codes including Z_OK and Z_STREAM_END to
473 * allow the caller to do multiple calls if required. If the 'finish' flag is
474 * set Z_FINISH will be passed to the final inflate() call and Z_STREAM_END must
475 * be returned or there has been a problem, otherwise Z_SYNC_FLUSH is used and
476 * Z_OK or Z_STREAM_END will be returned on success.
478 * The input and output sizes are updated to the actual amounts of data consumed
479 * or written, not the amount available (as in a z_stream). The data pointers
480 * are not changed, so the next input is (data+input_size) and the next
481 * available output is (output+output_size).
483 static int
484 png_inflate(png_structrp png_ptr, png_uint_32 owner, int finish,
485 /* INPUT: */ png_const_bytep input, png_uint_32p input_size_ptr,
486 /* OUTPUT: */ png_bytep output, png_alloc_size_t *output_size_ptr)
488 if (png_ptr->zowner == owner) /* Else not claimed */
490 int ret;
491 png_alloc_size_t avail_out = *output_size_ptr;
492 png_uint_32 avail_in = *input_size_ptr;
494 /* zlib can't necessarily handle more than 65535 bytes at once (i.e. it
495 * can't even necessarily handle 65536 bytes) because the type uInt is
496 * "16 bits or more". Consequently it is necessary to chunk the input to
497 * zlib. This code uses ZLIB_IO_MAX, from pngpriv.h, as the maximum (the
498 * maximum value that can be stored in a uInt.) It is possible to set
499 * ZLIB_IO_MAX to a lower value in pngpriv.h and this may sometimes have
500 * a performance advantage, because it reduces the amount of data accessed
501 * at each step and that may give the OS more time to page it in.
503 png_ptr->zstream.next_in = PNGZ_INPUT_CAST(input);
504 /* avail_in and avail_out are set below from 'size' */
505 png_ptr->zstream.avail_in = 0;
506 png_ptr->zstream.avail_out = 0;
508 /* Read directly into the output if it is available (this is set to
509 * a local buffer below if output is NULL).
511 if (output != NULL)
512 png_ptr->zstream.next_out = output;
516 uInt avail;
517 Byte local_buffer[PNG_INFLATE_BUF_SIZE];
519 /* zlib INPUT BUFFER */
520 /* The setting of 'avail_in' used to be outside the loop; by setting it
521 * inside it is possible to chunk the input to zlib and simply rely on
522 * zlib to advance the 'next_in' pointer. This allows arbitrary
523 * amounts of data to be passed through zlib at the unavoidable cost of
524 * requiring a window save (memcpy of up to 32768 output bytes)
525 * every ZLIB_IO_MAX input bytes.
527 avail_in += png_ptr->zstream.avail_in; /* not consumed last time */
529 avail = ZLIB_IO_MAX;
531 if (avail_in < avail)
532 avail = (uInt)avail_in; /* safe: < than ZLIB_IO_MAX */
534 avail_in -= avail;
535 png_ptr->zstream.avail_in = avail;
537 /* zlib OUTPUT BUFFER */
538 avail_out += png_ptr->zstream.avail_out; /* not written last time */
540 avail = ZLIB_IO_MAX; /* maximum zlib can process */
542 if (output == NULL)
544 /* Reset the output buffer each time round if output is NULL and
545 * make available the full buffer, up to 'remaining_space'
547 png_ptr->zstream.next_out = local_buffer;
548 if ((sizeof local_buffer) < avail)
549 avail = (sizeof local_buffer);
552 if (avail_out < avail)
553 avail = (uInt)avail_out; /* safe: < ZLIB_IO_MAX */
555 png_ptr->zstream.avail_out = avail;
556 avail_out -= avail;
558 /* zlib inflate call */
559 /* In fact 'avail_out' may be 0 at this point, that happens at the end
560 * of the read when the final LZ end code was not passed at the end of
561 * the previous chunk of input data. Tell zlib if we have reached the
562 * end of the output buffer.
564 ret = PNG_INFLATE(png_ptr, avail_out > 0 ? Z_NO_FLUSH :
565 (finish ? Z_FINISH : Z_SYNC_FLUSH));
566 } while (ret == Z_OK);
568 /* For safety kill the local buffer pointer now */
569 if (output == NULL)
570 png_ptr->zstream.next_out = NULL;
572 /* Claw back the 'size' and 'remaining_space' byte counts. */
573 avail_in += png_ptr->zstream.avail_in;
574 avail_out += png_ptr->zstream.avail_out;
576 /* Update the input and output sizes; the updated values are the amount
577 * consumed or written, effectively the inverse of what zlib uses.
579 if (avail_out > 0)
580 *output_size_ptr -= avail_out;
582 if (avail_in > 0)
583 *input_size_ptr -= avail_in;
585 /* Ensure png_ptr->zstream.msg is set (even in the success case!) */
586 png_zstream_error(png_ptr, ret);
587 return ret;
590 else
592 /* This is a bad internal error. The recovery assigns to the zstream msg
593 * pointer, which is not owned by the caller, but this is safe; it's only
594 * used on errors!
596 png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed");
597 return Z_STREAM_ERROR;
602 * Decompress trailing data in a chunk. The assumption is that read_buffer
603 * points at an allocated area holding the contents of a chunk with a
604 * trailing compressed part. What we get back is an allocated area
605 * holding the original prefix part and an uncompressed version of the
606 * trailing part (the malloc area passed in is freed).
608 static int
609 png_decompress_chunk(png_structrp png_ptr,
610 png_uint_32 chunklength, png_uint_32 prefix_size,
611 png_alloc_size_t *newlength /* must be initialized to the maximum! */,
612 int terminate /*add a '\0' to the end of the uncompressed data*/)
614 /* TODO: implement different limits for different types of chunk.
616 * The caller supplies *newlength set to the maximum length of the
617 * uncompressed data, but this routine allocates space for the prefix and
618 * maybe a '\0' terminator too. We have to assume that 'prefix_size' is
619 * limited only by the maximum chunk size.
621 png_alloc_size_t limit = PNG_SIZE_MAX;
623 # ifdef PNG_SET_USER_LIMITS_SUPPORTED
624 if (png_ptr->user_chunk_malloc_max > 0 &&
625 png_ptr->user_chunk_malloc_max < limit)
626 limit = png_ptr->user_chunk_malloc_max;
627 # elif PNG_USER_CHUNK_MALLOC_MAX > 0
628 if (PNG_USER_CHUNK_MALLOC_MAX < limit)
629 limit = PNG_USER_CHUNK_MALLOC_MAX;
630 # endif
632 if (limit >= prefix_size + (terminate != 0))
634 int ret;
636 limit -= prefix_size + (terminate != 0);
638 if (limit < *newlength)
639 *newlength = limit;
641 /* Now try to claim the stream. */
642 ret = png_inflate_claim(png_ptr, png_ptr->chunk_name);
644 if (ret == Z_OK)
646 png_uint_32 lzsize = chunklength - prefix_size;
648 ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/,
649 /* input: */ png_ptr->read_buffer + prefix_size, &lzsize,
650 /* output: */ NULL, newlength);
652 if (ret == Z_STREAM_END)
654 /* Use 'inflateReset' here, not 'inflateReset2' because this
655 * preserves the previously decided window size (otherwise it would
656 * be necessary to store the previous window size.) In practice
657 * this doesn't matter anyway, because png_inflate will call inflate
658 * with Z_FINISH in almost all cases, so the window will not be
659 * maintained.
661 if (inflateReset(&png_ptr->zstream) == Z_OK)
663 /* Because of the limit checks above we know that the new,
664 * expanded, size will fit in a size_t (let alone an
665 * png_alloc_size_t). Use png_malloc_base here to avoid an
666 * extra OOM message.
668 png_alloc_size_t new_size = *newlength;
669 png_alloc_size_t buffer_size = prefix_size + new_size +
670 (terminate != 0);
671 png_bytep text = png_voidcast(png_bytep, png_malloc_base(png_ptr,
672 buffer_size));
674 if (text != NULL)
676 memset(text, 0, buffer_size);
678 ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/,
679 png_ptr->read_buffer + prefix_size, &lzsize,
680 text + prefix_size, newlength);
682 if (ret == Z_STREAM_END)
684 if (new_size == *newlength)
686 if (terminate != 0)
687 text[prefix_size + *newlength] = 0;
689 if (prefix_size > 0)
690 memcpy(text, png_ptr->read_buffer, prefix_size);
693 png_bytep old_ptr = png_ptr->read_buffer;
695 png_ptr->read_buffer = text;
696 png_ptr->read_buffer_size = buffer_size;
697 text = old_ptr; /* freed below */
701 else
703 /* The size changed on the second read, there can be no
704 * guarantee that anything is correct at this point.
705 * The 'msg' pointer has been set to "unexpected end of
706 * LZ stream", which is fine, but return an error code
707 * that the caller won't accept.
709 ret = PNG_UNEXPECTED_ZLIB_RETURN;
713 else if (ret == Z_OK)
714 ret = PNG_UNEXPECTED_ZLIB_RETURN; /* for safety */
716 /* Free the text pointer (this is the old read_buffer on
717 * success)
719 png_free(png_ptr, text);
721 /* This really is very benign, but it's still an error because
722 * the extra space may otherwise be used as a Trojan Horse.
724 if (ret == Z_STREAM_END &&
725 chunklength - prefix_size != lzsize)
726 png_chunk_benign_error(png_ptr, "extra compressed data");
729 else
731 /* Out of memory allocating the buffer */
732 ret = Z_MEM_ERROR;
733 png_zstream_error(png_ptr, Z_MEM_ERROR);
737 else
739 /* inflateReset failed, store the error message */
740 png_zstream_error(png_ptr, ret);
741 ret = PNG_UNEXPECTED_ZLIB_RETURN;
745 else if (ret == Z_OK)
746 ret = PNG_UNEXPECTED_ZLIB_RETURN;
748 /* Release the claimed stream */
749 png_ptr->zowner = 0;
752 else /* the claim failed */ if (ret == Z_STREAM_END) /* impossible! */
753 ret = PNG_UNEXPECTED_ZLIB_RETURN;
755 return ret;
758 else
760 /* Application/configuration limits exceeded */
761 png_zstream_error(png_ptr, Z_MEM_ERROR);
762 return Z_MEM_ERROR;
765 #endif /* READ_zTXt || READ_iTXt */
766 #endif /* READ_COMPRESSED_TEXT */
768 #ifdef PNG_READ_iCCP_SUPPORTED
769 /* Perform a partial read and decompress, producing 'avail_out' bytes and
770 * reading from the current chunk as required.
772 static int
773 png_inflate_read(png_structrp png_ptr, png_bytep read_buffer, uInt read_size,
774 png_uint_32p chunk_bytes, png_bytep next_out, png_alloc_size_t *out_size,
775 int finish)
777 if (png_ptr->zowner == png_ptr->chunk_name)
779 int ret;
781 /* next_in and avail_in must have been initialized by the caller. */
782 png_ptr->zstream.next_out = next_out;
783 png_ptr->zstream.avail_out = 0; /* set in the loop */
787 if (png_ptr->zstream.avail_in == 0)
789 if (read_size > *chunk_bytes)
790 read_size = (uInt)*chunk_bytes;
791 *chunk_bytes -= read_size;
793 if (read_size > 0)
794 png_crc_read(png_ptr, read_buffer, read_size);
796 png_ptr->zstream.next_in = read_buffer;
797 png_ptr->zstream.avail_in = read_size;
800 if (png_ptr->zstream.avail_out == 0)
802 uInt avail = ZLIB_IO_MAX;
803 if (avail > *out_size)
804 avail = (uInt)*out_size;
805 *out_size -= avail;
807 png_ptr->zstream.avail_out = avail;
810 /* Use Z_SYNC_FLUSH when there is no more chunk data to ensure that all
811 * the available output is produced; this allows reading of truncated
812 * streams.
814 ret = PNG_INFLATE(png_ptr, *chunk_bytes > 0 ?
815 Z_NO_FLUSH : (finish ? Z_FINISH : Z_SYNC_FLUSH));
817 while (ret == Z_OK && (*out_size > 0 || png_ptr->zstream.avail_out > 0));
819 *out_size += png_ptr->zstream.avail_out;
820 png_ptr->zstream.avail_out = 0; /* Should not be required, but is safe */
822 /* Ensure the error message pointer is always set: */
823 png_zstream_error(png_ptr, ret);
824 return ret;
827 else
829 png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed");
830 return Z_STREAM_ERROR;
833 #endif /* READ_iCCP */
835 /* Read and check the IDHR chunk */
837 void /* PRIVATE */
838 png_handle_IHDR(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
840 png_byte buf[13];
841 png_uint_32 width, height;
842 int bit_depth, color_type, compression_type, filter_type;
843 int interlace_type;
845 png_debug(1, "in png_handle_IHDR");
847 if ((png_ptr->mode & PNG_HAVE_IHDR) != 0)
848 png_chunk_error(png_ptr, "out of place");
850 /* Check the length */
851 if (length != 13)
852 png_chunk_error(png_ptr, "invalid");
854 png_ptr->mode |= PNG_HAVE_IHDR;
856 png_crc_read(png_ptr, buf, 13);
857 png_crc_finish(png_ptr, 0);
859 width = png_get_uint_31(png_ptr, buf);
860 height = png_get_uint_31(png_ptr, buf + 4);
861 bit_depth = buf[8];
862 color_type = buf[9];
863 compression_type = buf[10];
864 filter_type = buf[11];
865 interlace_type = buf[12];
867 #ifdef PNG_READ_APNG_SUPPORTED
868 png_ptr->first_frame_width = width;
869 png_ptr->first_frame_height = height;
870 #endif
872 /* Set internal variables */
873 png_ptr->width = width;
874 png_ptr->height = height;
875 png_ptr->bit_depth = (png_byte)bit_depth;
876 png_ptr->interlaced = (png_byte)interlace_type;
877 png_ptr->color_type = (png_byte)color_type;
878 #ifdef PNG_MNG_FEATURES_SUPPORTED
879 png_ptr->filter_type = (png_byte)filter_type;
880 #endif
881 png_ptr->compression_type = (png_byte)compression_type;
883 /* Find number of channels */
884 switch (png_ptr->color_type)
886 default: /* invalid, png_set_IHDR calls png_error */
887 case PNG_COLOR_TYPE_GRAY:
888 case PNG_COLOR_TYPE_PALETTE:
889 png_ptr->channels = 1;
890 break;
892 case PNG_COLOR_TYPE_RGB:
893 png_ptr->channels = 3;
894 break;
896 case PNG_COLOR_TYPE_GRAY_ALPHA:
897 png_ptr->channels = 2;
898 break;
900 case PNG_COLOR_TYPE_RGB_ALPHA:
901 png_ptr->channels = 4;
902 break;
905 /* Set up other useful info */
906 png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth * png_ptr->channels);
907 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width);
908 png_debug1(3, "bit_depth = %d", png_ptr->bit_depth);
909 png_debug1(3, "channels = %d", png_ptr->channels);
910 png_debug1(3, "rowbytes = %lu", (unsigned long)png_ptr->rowbytes);
911 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
912 color_type, interlace_type, compression_type, filter_type);
915 /* Read and check the palette */
916 void /* PRIVATE */
917 png_handle_PLTE(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
919 png_color palette[PNG_MAX_PALETTE_LENGTH];
920 int max_palette_length, num, i;
921 #ifdef PNG_POINTER_INDEXING_SUPPORTED
922 png_colorp pal_ptr;
923 #endif
925 png_debug(1, "in png_handle_PLTE");
927 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
928 png_chunk_error(png_ptr, "missing IHDR");
930 /* Moved to before the 'after IDAT' check below because otherwise duplicate
931 * PLTE chunks are potentially ignored (the spec says there shall not be more
932 * than one PLTE, the error is not treated as benign, so this check trumps
933 * the requirement that PLTE appears before IDAT.)
935 else if ((png_ptr->mode & PNG_HAVE_PLTE) != 0)
936 png_chunk_error(png_ptr, "duplicate");
938 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
940 /* This is benign because the non-benign error happened before, when an
941 * IDAT was encountered in a color-mapped image with no PLTE.
943 png_crc_finish(png_ptr, length);
944 png_chunk_benign_error(png_ptr, "out of place");
945 return;
948 png_ptr->mode |= PNG_HAVE_PLTE;
950 if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0)
952 png_crc_finish(png_ptr, length);
953 png_chunk_benign_error(png_ptr, "ignored in grayscale PNG");
954 return;
957 #ifndef PNG_READ_OPT_PLTE_SUPPORTED
958 if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
960 png_crc_finish(png_ptr, length);
961 return;
963 #endif
965 if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3)
967 png_crc_finish(png_ptr, length);
969 if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
970 png_chunk_benign_error(png_ptr, "invalid");
972 else
973 png_chunk_error(png_ptr, "invalid");
975 return;
978 /* The cast is safe because 'length' is less than 3*PNG_MAX_PALETTE_LENGTH */
979 num = (int)length / 3;
981 /* If the palette has 256 or fewer entries but is too large for the bit
982 * depth, we don't issue an error, to preserve the behavior of previous
983 * libpng versions. We silently truncate the unused extra palette entries
984 * here.
986 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
987 max_palette_length = (1 << png_ptr->bit_depth);
988 else
989 max_palette_length = PNG_MAX_PALETTE_LENGTH;
991 if (num > max_palette_length)
992 num = max_palette_length;
994 #ifdef PNG_POINTER_INDEXING_SUPPORTED
995 for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)
997 png_byte buf[3];
999 png_crc_read(png_ptr, buf, 3);
1000 pal_ptr->red = buf[0];
1001 pal_ptr->green = buf[1];
1002 pal_ptr->blue = buf[2];
1004 #else
1005 for (i = 0; i < num; i++)
1007 png_byte buf[3];
1009 png_crc_read(png_ptr, buf, 3);
1010 /* Don't depend upon png_color being any order */
1011 palette[i].red = buf[0];
1012 palette[i].green = buf[1];
1013 palette[i].blue = buf[2];
1015 #endif
1017 /* If we actually need the PLTE chunk (ie for a paletted image), we do
1018 * whatever the normal CRC configuration tells us. However, if we
1019 * have an RGB image, the PLTE can be considered ancillary, so
1020 * we will act as though it is.
1022 #ifndef PNG_READ_OPT_PLTE_SUPPORTED
1023 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1024 #endif
1026 png_crc_finish(png_ptr, (png_uint_32) (length - (unsigned int)num * 3));
1029 #ifndef PNG_READ_OPT_PLTE_SUPPORTED
1030 else if (png_crc_error(png_ptr) != 0) /* Only if we have a CRC error */
1032 /* If we don't want to use the data from an ancillary chunk,
1033 * we have two options: an error abort, or a warning and we
1034 * ignore the data in this chunk (which should be OK, since
1035 * it's considered ancillary for a RGB or RGBA image).
1037 * IMPLEMENTATION NOTE: this is only here because png_crc_finish uses the
1038 * chunk type to determine whether to check the ancillary or the critical
1039 * flags.
1041 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE) == 0)
1043 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) != 0)
1044 return;
1046 else
1047 png_chunk_error(png_ptr, "CRC error");
1050 /* Otherwise, we (optionally) emit a warning and use the chunk. */
1051 else if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) == 0)
1052 png_chunk_warning(png_ptr, "CRC error");
1054 #endif
1056 /* TODO: png_set_PLTE has the side effect of setting png_ptr->palette to its
1057 * own copy of the palette. This has the side effect that when png_start_row
1058 * is called (this happens after any call to png_read_update_info) the
1059 * info_ptr palette gets changed. This is extremely unexpected and
1060 * confusing.
1062 * Fix this by not sharing the palette in this way.
1064 png_set_PLTE(png_ptr, info_ptr, palette, num);
1066 /* The three chunks, bKGD, hIST and tRNS *must* appear after PLTE and before
1067 * IDAT. Prior to 1.6.0 this was not checked; instead the code merely
1068 * checked the apparent validity of a tRNS chunk inserted before PLTE on a
1069 * palette PNG. 1.6.0 attempts to rigorously follow the standard and
1070 * therefore does a benign error if the erroneous condition is detected *and*
1071 * cancels the tRNS if the benign error returns. The alternative is to
1072 * amend the standard since it would be rather hypocritical of the standards
1073 * maintainers to ignore it.
1075 #ifdef PNG_READ_tRNS_SUPPORTED
1076 if (png_ptr->num_trans > 0 ||
1077 (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS) != 0))
1079 /* Cancel this because otherwise it would be used if the transforms
1080 * require it. Don't cancel the 'valid' flag because this would prevent
1081 * detection of duplicate chunks.
1083 png_ptr->num_trans = 0;
1085 if (info_ptr != NULL)
1086 info_ptr->num_trans = 0;
1088 png_chunk_benign_error(png_ptr, "tRNS must be after");
1090 #endif
1092 #ifdef PNG_READ_hIST_SUPPORTED
1093 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) != 0)
1094 png_chunk_benign_error(png_ptr, "hIST must be after");
1095 #endif
1097 #ifdef PNG_READ_bKGD_SUPPORTED
1098 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) != 0)
1099 png_chunk_benign_error(png_ptr, "bKGD must be after");
1100 #endif
1103 void /* PRIVATE */
1104 png_handle_IEND(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1106 png_debug(1, "in png_handle_IEND");
1108 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0 ||
1109 (png_ptr->mode & PNG_HAVE_IDAT) == 0)
1110 png_chunk_error(png_ptr, "out of place");
1112 png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND);
1114 png_crc_finish(png_ptr, length);
1116 if (length != 0)
1117 png_chunk_benign_error(png_ptr, "invalid");
1119 PNG_UNUSED(info_ptr)
1122 #ifdef PNG_READ_gAMA_SUPPORTED
1123 void /* PRIVATE */
1124 png_handle_gAMA(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1126 png_fixed_point igamma;
1127 png_byte buf[4];
1129 png_debug(1, "in png_handle_gAMA");
1131 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1132 png_chunk_error(png_ptr, "missing IHDR");
1134 else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
1136 png_crc_finish(png_ptr, length);
1137 png_chunk_benign_error(png_ptr, "out of place");
1138 return;
1141 if (length != 4)
1143 png_crc_finish(png_ptr, length);
1144 png_chunk_benign_error(png_ptr, "invalid");
1145 return;
1148 png_crc_read(png_ptr, buf, 4);
1150 if (png_crc_finish(png_ptr, 0) != 0)
1151 return;
1153 igamma = png_get_fixed_point(NULL, buf);
1155 png_colorspace_set_gamma(png_ptr, &png_ptr->colorspace, igamma);
1156 png_colorspace_sync(png_ptr, info_ptr);
1158 #endif
1160 #ifdef PNG_READ_sBIT_SUPPORTED
1161 void /* PRIVATE */
1162 png_handle_sBIT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1164 unsigned int truelen, i;
1165 png_byte sample_depth;
1166 png_byte buf[4];
1168 png_debug(1, "in png_handle_sBIT");
1170 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1171 png_chunk_error(png_ptr, "missing IHDR");
1173 else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
1175 png_crc_finish(png_ptr, length);
1176 png_chunk_benign_error(png_ptr, "out of place");
1177 return;
1180 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT) != 0)
1182 png_crc_finish(png_ptr, length);
1183 png_chunk_benign_error(png_ptr, "duplicate");
1184 return;
1187 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1189 truelen = 3;
1190 sample_depth = 8;
1193 else
1195 truelen = png_ptr->channels;
1196 sample_depth = png_ptr->bit_depth;
1199 if (length != truelen || length > 4)
1201 png_chunk_benign_error(png_ptr, "invalid");
1202 png_crc_finish(png_ptr, length);
1203 return;
1206 buf[0] = buf[1] = buf[2] = buf[3] = sample_depth;
1207 png_crc_read(png_ptr, buf, truelen);
1209 if (png_crc_finish(png_ptr, 0) != 0)
1210 return;
1212 for (i=0; i<truelen; ++i)
1214 if (buf[i] == 0 || buf[i] > sample_depth)
1216 png_chunk_benign_error(png_ptr, "invalid");
1217 return;
1221 if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
1223 png_ptr->sig_bit.red = buf[0];
1224 png_ptr->sig_bit.green = buf[1];
1225 png_ptr->sig_bit.blue = buf[2];
1226 png_ptr->sig_bit.alpha = buf[3];
1229 else
1231 png_ptr->sig_bit.gray = buf[0];
1232 png_ptr->sig_bit.red = buf[0];
1233 png_ptr->sig_bit.green = buf[0];
1234 png_ptr->sig_bit.blue = buf[0];
1235 png_ptr->sig_bit.alpha = buf[1];
1238 png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
1240 #endif
1242 #ifdef PNG_READ_cHRM_SUPPORTED
1243 void /* PRIVATE */
1244 png_handle_cHRM(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1246 png_byte buf[32];
1247 png_xy xy;
1249 png_debug(1, "in png_handle_cHRM");
1251 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1252 png_chunk_error(png_ptr, "missing IHDR");
1254 else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
1256 png_crc_finish(png_ptr, length);
1257 png_chunk_benign_error(png_ptr, "out of place");
1258 return;
1261 if (length != 32)
1263 png_crc_finish(png_ptr, length);
1264 png_chunk_benign_error(png_ptr, "invalid");
1265 return;
1268 png_crc_read(png_ptr, buf, 32);
1270 if (png_crc_finish(png_ptr, 0) != 0)
1271 return;
1273 xy.whitex = png_get_fixed_point(NULL, buf);
1274 xy.whitey = png_get_fixed_point(NULL, buf + 4);
1275 xy.redx = png_get_fixed_point(NULL, buf + 8);
1276 xy.redy = png_get_fixed_point(NULL, buf + 12);
1277 xy.greenx = png_get_fixed_point(NULL, buf + 16);
1278 xy.greeny = png_get_fixed_point(NULL, buf + 20);
1279 xy.bluex = png_get_fixed_point(NULL, buf + 24);
1280 xy.bluey = png_get_fixed_point(NULL, buf + 28);
1282 if (xy.whitex == PNG_FIXED_ERROR ||
1283 xy.whitey == PNG_FIXED_ERROR ||
1284 xy.redx == PNG_FIXED_ERROR ||
1285 xy.redy == PNG_FIXED_ERROR ||
1286 xy.greenx == PNG_FIXED_ERROR ||
1287 xy.greeny == PNG_FIXED_ERROR ||
1288 xy.bluex == PNG_FIXED_ERROR ||
1289 xy.bluey == PNG_FIXED_ERROR)
1291 png_chunk_benign_error(png_ptr, "invalid values");
1292 return;
1295 /* If a colorspace error has already been output skip this chunk */
1296 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0)
1297 return;
1299 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM) != 0)
1301 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1302 png_colorspace_sync(png_ptr, info_ptr);
1303 png_chunk_benign_error(png_ptr, "duplicate");
1304 return;
1307 png_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM;
1308 (void)png_colorspace_set_chromaticities(png_ptr, &png_ptr->colorspace, &xy,
1309 1/*prefer cHRM values*/);
1310 png_colorspace_sync(png_ptr, info_ptr);
1312 #endif
1314 #ifdef PNG_READ_sRGB_SUPPORTED
1315 void /* PRIVATE */
1316 png_handle_sRGB(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1318 png_byte intent;
1320 png_debug(1, "in png_handle_sRGB");
1322 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1323 png_chunk_error(png_ptr, "missing IHDR");
1325 else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
1327 png_crc_finish(png_ptr, length);
1328 png_chunk_benign_error(png_ptr, "out of place");
1329 return;
1332 if (length != 1)
1334 png_crc_finish(png_ptr, length);
1335 png_chunk_benign_error(png_ptr, "invalid");
1336 return;
1339 png_crc_read(png_ptr, &intent, 1);
1341 if (png_crc_finish(png_ptr, 0) != 0)
1342 return;
1344 /* If a colorspace error has already been output skip this chunk */
1345 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0)
1346 return;
1348 /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect
1349 * this.
1351 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) != 0)
1353 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1354 png_colorspace_sync(png_ptr, info_ptr);
1355 png_chunk_benign_error(png_ptr, "too many profiles");
1356 return;
1359 (void)png_colorspace_set_sRGB(png_ptr, &png_ptr->colorspace, intent);
1360 png_colorspace_sync(png_ptr, info_ptr);
1362 #endif /* READ_sRGB */
1364 #ifdef PNG_READ_iCCP_SUPPORTED
1365 void /* PRIVATE */
1366 png_handle_iCCP(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1367 /* Note: this does not properly handle profiles that are > 64K under DOS */
1369 png_const_charp errmsg = NULL; /* error message output, or no error */
1370 int finished = 0; /* crc checked */
1372 png_debug(1, "in png_handle_iCCP");
1374 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1375 png_chunk_error(png_ptr, "missing IHDR");
1377 else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
1379 png_crc_finish(png_ptr, length);
1380 png_chunk_benign_error(png_ptr, "out of place");
1381 return;
1384 /* Consistent with all the above colorspace handling an obviously *invalid*
1385 * chunk is just ignored, so does not invalidate the color space. An
1386 * alternative is to set the 'invalid' flags at the start of this routine
1387 * and only clear them in they were not set before and all the tests pass.
1390 /* The keyword must be at least one character and there is a
1391 * terminator (0) byte and the compression method byte, and the
1392 * 'zlib' datastream is at least 11 bytes.
1394 if (length < 14)
1396 png_crc_finish(png_ptr, length);
1397 png_chunk_benign_error(png_ptr, "too short");
1398 return;
1401 /* If a colorspace error has already been output skip this chunk */
1402 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0)
1404 png_crc_finish(png_ptr, length);
1405 return;
1408 /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect
1409 * this.
1411 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) == 0)
1413 uInt read_length, keyword_length;
1414 char keyword[81];
1416 /* Find the keyword; the keyword plus separator and compression method
1417 * bytes can be at most 81 characters long.
1419 read_length = 81; /* maximum */
1420 if (read_length > length)
1421 read_length = (uInt)length;
1423 png_crc_read(png_ptr, (png_bytep)keyword, read_length);
1424 length -= read_length;
1426 /* The minimum 'zlib' stream is assumed to be just the 2 byte header,
1427 * 5 bytes minimum 'deflate' stream, and the 4 byte checksum.
1429 if (length < 11)
1431 png_crc_finish(png_ptr, length);
1432 png_chunk_benign_error(png_ptr, "too short");
1433 return;
1436 keyword_length = 0;
1437 while (keyword_length < 80 && keyword_length < read_length &&
1438 keyword[keyword_length] != 0)
1439 ++keyword_length;
1441 /* TODO: make the keyword checking common */
1442 if (keyword_length >= 1 && keyword_length <= 79)
1444 /* We only understand '0' compression - deflate - so if we get a
1445 * different value we can't safely decode the chunk.
1447 if (keyword_length+1 < read_length &&
1448 keyword[keyword_length+1] == PNG_COMPRESSION_TYPE_BASE)
1450 read_length -= keyword_length+2;
1452 if (png_inflate_claim(png_ptr, png_iCCP) == Z_OK)
1454 Byte profile_header[132]={0};
1455 Byte local_buffer[PNG_INFLATE_BUF_SIZE];
1456 png_alloc_size_t size = (sizeof profile_header);
1458 png_ptr->zstream.next_in = (Bytef*)keyword + (keyword_length+2);
1459 png_ptr->zstream.avail_in = read_length;
1460 (void)png_inflate_read(png_ptr, local_buffer,
1461 (sizeof local_buffer), &length, profile_header, &size,
1462 0/*finish: don't, because the output is too small*/);
1464 if (size == 0)
1466 /* We have the ICC profile header; do the basic header checks.
1468 png_uint_32 profile_length = png_get_uint_32(profile_header);
1470 if (png_icc_check_length(png_ptr, &png_ptr->colorspace,
1471 keyword, profile_length) != 0)
1473 /* The length is apparently ok, so we can check the 132
1474 * byte header.
1476 if (png_icc_check_header(png_ptr, &png_ptr->colorspace,
1477 keyword, profile_length, profile_header,
1478 png_ptr->color_type) != 0)
1480 /* Now read the tag table; a variable size buffer is
1481 * needed at this point, allocate one for the whole
1482 * profile. The header check has already validated
1483 * that none of this stuff will overflow.
1485 png_uint_32 tag_count =
1486 png_get_uint_32(profile_header + 128);
1487 png_bytep profile = png_read_buffer(png_ptr,
1488 profile_length, 2/*silent*/);
1490 if (profile != NULL)
1492 memcpy(profile, profile_header,
1493 (sizeof profile_header));
1495 size = 12 * tag_count;
1497 (void)png_inflate_read(png_ptr, local_buffer,
1498 (sizeof local_buffer), &length,
1499 profile + (sizeof profile_header), &size, 0);
1501 /* Still expect a buffer error because we expect
1502 * there to be some tag data!
1504 if (size == 0)
1506 if (png_icc_check_tag_table(png_ptr,
1507 &png_ptr->colorspace, keyword, profile_length,
1508 profile) != 0)
1510 /* The profile has been validated for basic
1511 * security issues, so read the whole thing in.
1513 size = profile_length - (sizeof profile_header)
1514 - 12 * tag_count;
1516 (void)png_inflate_read(png_ptr, local_buffer,
1517 (sizeof local_buffer), &length,
1518 profile + (sizeof profile_header) +
1519 12 * tag_count, &size, 1/*finish*/);
1521 if (length > 0 && !(png_ptr->flags &
1522 PNG_FLAG_BENIGN_ERRORS_WARN))
1523 errmsg = "extra compressed data";
1525 /* But otherwise allow extra data: */
1526 else if (size == 0)
1528 if (length > 0)
1530 /* This can be handled completely, so
1531 * keep going.
1533 png_chunk_warning(png_ptr,
1534 "extra compressed data");
1537 png_crc_finish(png_ptr, length);
1538 finished = 1;
1540 # if defined(PNG_sRGB_SUPPORTED) && PNG_sRGB_PROFILE_CHECKS >= 0
1541 /* Check for a match against sRGB */
1542 png_icc_set_sRGB(png_ptr,
1543 &png_ptr->colorspace, profile,
1544 png_ptr->zstream.adler);
1545 # endif
1547 /* Steal the profile for info_ptr. */
1548 if (info_ptr != NULL)
1550 png_free_data(png_ptr, info_ptr,
1551 PNG_FREE_ICCP, 0);
1553 info_ptr->iccp_name = png_voidcast(char*,
1554 png_malloc_base(png_ptr,
1555 keyword_length+1));
1556 if (info_ptr->iccp_name != NULL)
1558 memcpy(info_ptr->iccp_name, keyword,
1559 keyword_length+1);
1560 info_ptr->iccp_proflen =
1561 profile_length;
1562 info_ptr->iccp_profile = profile;
1563 png_ptr->read_buffer = NULL; /*steal*/
1564 info_ptr->free_me |= PNG_FREE_ICCP;
1565 info_ptr->valid |= PNG_INFO_iCCP;
1568 else
1570 png_ptr->colorspace.flags |=
1571 PNG_COLORSPACE_INVALID;
1572 errmsg = "out of memory";
1576 /* else the profile remains in the read
1577 * buffer which gets reused for subsequent
1578 * chunks.
1581 if (info_ptr != NULL)
1582 png_colorspace_sync(png_ptr, info_ptr);
1584 if (errmsg == NULL)
1586 png_ptr->zowner = 0;
1587 return;
1590 if (errmsg == NULL)
1591 errmsg = png_ptr->zstream.msg;
1593 /* else png_icc_check_tag_table output an error */
1595 else /* profile truncated */
1596 errmsg = png_ptr->zstream.msg;
1599 else
1600 errmsg = "out of memory";
1603 /* else png_icc_check_header output an error */
1606 /* else png_icc_check_length output an error */
1609 else /* profile truncated */
1610 errmsg = png_ptr->zstream.msg;
1612 /* Release the stream */
1613 png_ptr->zowner = 0;
1616 else /* png_inflate_claim failed */
1617 errmsg = png_ptr->zstream.msg;
1620 else
1621 errmsg = "bad compression method"; /* or missing */
1624 else
1625 errmsg = "bad keyword";
1628 else
1629 errmsg = "too many profiles";
1631 /* Failure: the reason is in 'errmsg' */
1632 if (finished == 0)
1633 png_crc_finish(png_ptr, length);
1635 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1636 png_colorspace_sync(png_ptr, info_ptr);
1637 if (errmsg != NULL) /* else already output */
1638 png_chunk_benign_error(png_ptr, errmsg);
1640 #endif /* READ_iCCP */
1642 #ifdef PNG_READ_sPLT_SUPPORTED
1643 void /* PRIVATE */
1644 png_handle_sPLT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1645 /* Note: this does not properly handle chunks that are > 64K under DOS */
1647 png_bytep entry_start, buffer;
1648 png_sPLT_t new_palette;
1649 png_sPLT_entryp pp;
1650 png_uint_32 data_length;
1651 int entry_size, i;
1652 png_uint_32 skip = 0;
1653 png_uint_32 dl;
1654 size_t max_dl;
1656 png_debug(1, "in png_handle_sPLT");
1658 #ifdef PNG_USER_LIMITS_SUPPORTED
1659 if (png_ptr->user_chunk_cache_max != 0)
1661 if (png_ptr->user_chunk_cache_max == 1)
1663 png_crc_finish(png_ptr, length);
1664 return;
1667 if (--png_ptr->user_chunk_cache_max == 1)
1669 png_warning(png_ptr, "No space in chunk cache for sPLT");
1670 png_crc_finish(png_ptr, length);
1671 return;
1674 #endif
1676 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1677 png_chunk_error(png_ptr, "missing IHDR");
1679 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
1681 png_crc_finish(png_ptr, length);
1682 png_chunk_benign_error(png_ptr, "out of place");
1683 return;
1686 #ifdef PNG_MAX_MALLOC_64K
1687 if (length > 65535U)
1689 png_crc_finish(png_ptr, length);
1690 png_chunk_benign_error(png_ptr, "too large to fit in memory");
1691 return;
1693 #endif
1695 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
1696 if (buffer == NULL)
1698 png_crc_finish(png_ptr, length);
1699 png_chunk_benign_error(png_ptr, "out of memory");
1700 return;
1704 /* WARNING: this may break if size_t is less than 32 bits; it is assumed
1705 * that the PNG_MAX_MALLOC_64K test is enabled in this case, but this is a
1706 * potential breakage point if the types in pngconf.h aren't exactly right.
1708 png_crc_read(png_ptr, buffer, length);
1710 if (png_crc_finish(png_ptr, skip) != 0)
1711 return;
1713 buffer[length] = 0;
1715 for (entry_start = buffer; *entry_start; entry_start++)
1716 /* Empty loop to find end of name */ ;
1718 ++entry_start;
1720 /* A sample depth should follow the separator, and we should be on it */
1721 if (length < 2U || entry_start > buffer + (length - 2U))
1723 png_warning(png_ptr, "malformed sPLT chunk");
1724 return;
1727 new_palette.depth = *entry_start++;
1728 entry_size = (new_palette.depth == 8 ? 6 : 10);
1729 /* This must fit in a png_uint_32 because it is derived from the original
1730 * chunk data length.
1732 data_length = length - (png_uint_32)(entry_start - buffer);
1734 /* Integrity-check the data length */
1735 if ((data_length % (unsigned int)entry_size) != 0)
1737 png_warning(png_ptr, "sPLT chunk has bad length");
1738 return;
1741 dl = (png_uint_32)(data_length / (unsigned int)entry_size);
1742 max_dl = PNG_SIZE_MAX / (sizeof (png_sPLT_entry));
1744 if (dl > max_dl)
1746 png_warning(png_ptr, "sPLT chunk too long");
1747 return;
1750 new_palette.nentries = (png_int_32)(data_length / (unsigned int)entry_size);
1752 new_palette.entries = (png_sPLT_entryp)png_malloc_warn(png_ptr,
1753 (png_alloc_size_t) new_palette.nentries * (sizeof (png_sPLT_entry)));
1755 if (new_palette.entries == NULL)
1757 png_warning(png_ptr, "sPLT chunk requires too much memory");
1758 return;
1761 #ifdef PNG_POINTER_INDEXING_SUPPORTED
1762 for (i = 0; i < new_palette.nentries; i++)
1764 pp = new_palette.entries + i;
1766 if (new_palette.depth == 8)
1768 pp->red = *entry_start++;
1769 pp->green = *entry_start++;
1770 pp->blue = *entry_start++;
1771 pp->alpha = *entry_start++;
1774 else
1776 pp->red = png_get_uint_16(entry_start); entry_start += 2;
1777 pp->green = png_get_uint_16(entry_start); entry_start += 2;
1778 pp->blue = png_get_uint_16(entry_start); entry_start += 2;
1779 pp->alpha = png_get_uint_16(entry_start); entry_start += 2;
1782 pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
1784 #else
1785 pp = new_palette.entries;
1787 for (i = 0; i < new_palette.nentries; i++)
1790 if (new_palette.depth == 8)
1792 pp[i].red = *entry_start++;
1793 pp[i].green = *entry_start++;
1794 pp[i].blue = *entry_start++;
1795 pp[i].alpha = *entry_start++;
1798 else
1800 pp[i].red = png_get_uint_16(entry_start); entry_start += 2;
1801 pp[i].green = png_get_uint_16(entry_start); entry_start += 2;
1802 pp[i].blue = png_get_uint_16(entry_start); entry_start += 2;
1803 pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2;
1806 pp[i].frequency = png_get_uint_16(entry_start); entry_start += 2;
1808 #endif
1810 /* Discard all chunk data except the name and stash that */
1811 new_palette.name = (png_charp)buffer;
1813 png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);
1815 png_free(png_ptr, new_palette.entries);
1817 #endif /* READ_sPLT */
1819 #ifdef PNG_READ_tRNS_SUPPORTED
1820 void /* PRIVATE */
1821 png_handle_tRNS(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1823 png_byte readbuf[PNG_MAX_PALETTE_LENGTH];
1825 png_debug(1, "in png_handle_tRNS");
1827 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1828 png_chunk_error(png_ptr, "missing IHDR");
1830 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
1832 png_crc_finish(png_ptr, length);
1833 png_chunk_benign_error(png_ptr, "out of place");
1834 return;
1837 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS) != 0)
1839 png_crc_finish(png_ptr, length);
1840 png_chunk_benign_error(png_ptr, "duplicate");
1841 return;
1844 if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
1846 png_byte buf[2];
1848 if (length != 2)
1850 png_crc_finish(png_ptr, length);
1851 png_chunk_benign_error(png_ptr, "invalid");
1852 return;
1855 png_crc_read(png_ptr, buf, 2);
1856 png_ptr->num_trans = 1;
1857 png_ptr->trans_color.gray = png_get_uint_16(buf);
1860 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
1862 png_byte buf[6];
1864 if (length != 6)
1866 png_crc_finish(png_ptr, length);
1867 png_chunk_benign_error(png_ptr, "invalid");
1868 return;
1871 png_crc_read(png_ptr, buf, length);
1872 png_ptr->num_trans = 1;
1873 png_ptr->trans_color.red = png_get_uint_16(buf);
1874 png_ptr->trans_color.green = png_get_uint_16(buf + 2);
1875 png_ptr->trans_color.blue = png_get_uint_16(buf + 4);
1878 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1880 if ((png_ptr->mode & PNG_HAVE_PLTE) == 0)
1882 /* TODO: is this actually an error in the ISO spec? */
1883 png_crc_finish(png_ptr, length);
1884 png_chunk_benign_error(png_ptr, "out of place");
1885 return;
1888 if (length > (unsigned int) png_ptr->num_palette ||
1889 length > (unsigned int) PNG_MAX_PALETTE_LENGTH ||
1890 length == 0)
1892 png_crc_finish(png_ptr, length);
1893 png_chunk_benign_error(png_ptr, "invalid");
1894 return;
1897 png_crc_read(png_ptr, readbuf, length);
1898 png_ptr->num_trans = (png_uint_16)length;
1901 else
1903 png_crc_finish(png_ptr, length);
1904 png_chunk_benign_error(png_ptr, "invalid with alpha channel");
1905 return;
1908 if (png_crc_finish(png_ptr, 0) != 0)
1910 png_ptr->num_trans = 0;
1911 return;
1914 /* TODO: this is a horrible side effect in the palette case because the
1915 * png_struct ends up with a pointer to the tRNS buffer owned by the
1916 * png_info. Fix this.
1918 png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,
1919 &(png_ptr->trans_color));
1921 #endif
1923 #ifdef PNG_READ_bKGD_SUPPORTED
1924 void /* PRIVATE */
1925 png_handle_bKGD(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1927 unsigned int truelen;
1928 png_byte buf[6];
1929 png_color_16 background;
1931 png_debug(1, "in png_handle_bKGD");
1933 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1934 png_chunk_error(png_ptr, "missing IHDR");
1936 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0 ||
1937 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
1938 (png_ptr->mode & PNG_HAVE_PLTE) == 0))
1940 png_crc_finish(png_ptr, length);
1941 png_chunk_benign_error(png_ptr, "out of place");
1942 return;
1945 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) != 0)
1947 png_crc_finish(png_ptr, length);
1948 png_chunk_benign_error(png_ptr, "duplicate");
1949 return;
1952 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1953 truelen = 1;
1955 else if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
1956 truelen = 6;
1958 else
1959 truelen = 2;
1961 if (length != truelen)
1963 png_crc_finish(png_ptr, length);
1964 png_chunk_benign_error(png_ptr, "invalid");
1965 return;
1968 png_crc_read(png_ptr, buf, truelen);
1970 if (png_crc_finish(png_ptr, 0) != 0)
1971 return;
1973 /* We convert the index value into RGB components so that we can allow
1974 * arbitrary RGB values for background when we have transparency, and
1975 * so it is easy to determine the RGB values of the background color
1976 * from the info_ptr struct.
1978 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1980 background.index = buf[0];
1982 if (info_ptr != NULL && info_ptr->num_palette != 0)
1984 if (buf[0] >= info_ptr->num_palette)
1986 png_chunk_benign_error(png_ptr, "invalid index");
1987 return;
1990 background.red = (png_uint_16)png_ptr->palette[buf[0]].red;
1991 background.green = (png_uint_16)png_ptr->palette[buf[0]].green;
1992 background.blue = (png_uint_16)png_ptr->palette[buf[0]].blue;
1995 else
1996 background.red = background.green = background.blue = 0;
1998 background.gray = 0;
2001 else if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0) /* GRAY */
2003 if (png_ptr->bit_depth <= 8)
2005 if (buf[0] != 0 || buf[1] >= (unsigned int)(1 << png_ptr->bit_depth))
2007 png_chunk_benign_error(png_ptr, "invalid gray level");
2008 return;
2012 background.index = 0;
2013 background.red =
2014 background.green =
2015 background.blue =
2016 background.gray = png_get_uint_16(buf);
2019 else
2021 if (png_ptr->bit_depth <= 8)
2023 if (buf[0] != 0 || buf[2] != 0 || buf[4] != 0)
2025 png_chunk_benign_error(png_ptr, "invalid color");
2026 return;
2030 background.index = 0;
2031 background.red = png_get_uint_16(buf);
2032 background.green = png_get_uint_16(buf + 2);
2033 background.blue = png_get_uint_16(buf + 4);
2034 background.gray = 0;
2037 png_set_bKGD(png_ptr, info_ptr, &background);
2039 #endif
2041 #ifdef PNG_READ_eXIf_SUPPORTED
2042 void /* PRIVATE */
2043 png_handle_eXIf(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2045 unsigned int i;
2047 png_debug(1, "in png_handle_eXIf");
2049 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2050 png_chunk_error(png_ptr, "missing IHDR");
2052 if (length < 2)
2054 png_crc_finish(png_ptr, length);
2055 png_chunk_benign_error(png_ptr, "too short");
2056 return;
2059 else if (info_ptr == NULL || (info_ptr->valid & PNG_INFO_eXIf) != 0)
2061 png_crc_finish(png_ptr, length);
2062 png_chunk_benign_error(png_ptr, "duplicate");
2063 return;
2066 info_ptr->free_me |= PNG_FREE_EXIF;
2068 info_ptr->eXIf_buf = png_voidcast(png_bytep,
2069 png_malloc_warn(png_ptr, length));
2071 if (info_ptr->eXIf_buf == NULL)
2073 png_crc_finish(png_ptr, length);
2074 png_chunk_benign_error(png_ptr, "out of memory");
2075 return;
2078 for (i = 0; i < length; i++)
2080 png_byte buf[1];
2081 png_crc_read(png_ptr, buf, 1);
2082 info_ptr->eXIf_buf[i] = buf[0];
2083 if (i == 1)
2085 if ((buf[0] != 'M' && buf[0] != 'I') ||
2086 (info_ptr->eXIf_buf[0] != buf[0]))
2088 png_crc_finish(png_ptr, length - 2);
2089 png_chunk_benign_error(png_ptr, "incorrect byte-order specifier");
2090 png_free(png_ptr, info_ptr->eXIf_buf);
2091 info_ptr->eXIf_buf = NULL;
2092 return;
2097 if (png_crc_finish(png_ptr, 0) == 0)
2098 png_set_eXIf_1(png_ptr, info_ptr, length, info_ptr->eXIf_buf);
2100 png_free(png_ptr, info_ptr->eXIf_buf);
2101 info_ptr->eXIf_buf = NULL;
2103 #endif
2105 #ifdef PNG_READ_hIST_SUPPORTED
2106 void /* PRIVATE */
2107 png_handle_hIST(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2109 unsigned int num, i;
2110 png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];
2112 png_debug(1, "in png_handle_hIST");
2114 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2115 png_chunk_error(png_ptr, "missing IHDR");
2117 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0 ||
2118 (png_ptr->mode & PNG_HAVE_PLTE) == 0)
2120 png_crc_finish(png_ptr, length);
2121 png_chunk_benign_error(png_ptr, "out of place");
2122 return;
2125 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) != 0)
2127 png_crc_finish(png_ptr, length);
2128 png_chunk_benign_error(png_ptr, "duplicate");
2129 return;
2132 num = length / 2 ;
2134 if (length != num * 2 ||
2135 num != (unsigned int)png_ptr->num_palette ||
2136 num > (unsigned int)PNG_MAX_PALETTE_LENGTH)
2138 png_crc_finish(png_ptr, length);
2139 png_chunk_benign_error(png_ptr, "invalid");
2140 return;
2143 for (i = 0; i < num; i++)
2145 png_byte buf[2];
2147 png_crc_read(png_ptr, buf, 2);
2148 readbuf[i] = png_get_uint_16(buf);
2151 if (png_crc_finish(png_ptr, 0) != 0)
2152 return;
2154 png_set_hIST(png_ptr, info_ptr, readbuf);
2156 #endif
2158 #ifdef PNG_READ_pHYs_SUPPORTED
2159 void /* PRIVATE */
2160 png_handle_pHYs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2162 png_byte buf[9];
2163 png_uint_32 res_x, res_y;
2164 int unit_type;
2166 png_debug(1, "in png_handle_pHYs");
2168 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2169 png_chunk_error(png_ptr, "missing IHDR");
2171 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2173 png_crc_finish(png_ptr, length);
2174 png_chunk_benign_error(png_ptr, "out of place");
2175 return;
2178 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs) != 0)
2180 png_crc_finish(png_ptr, length);
2181 png_chunk_benign_error(png_ptr, "duplicate");
2182 return;
2185 if (length != 9)
2187 png_crc_finish(png_ptr, length);
2188 png_chunk_benign_error(png_ptr, "invalid");
2189 return;
2192 png_crc_read(png_ptr, buf, 9);
2194 if (png_crc_finish(png_ptr, 0) != 0)
2195 return;
2197 res_x = png_get_uint_32(buf);
2198 res_y = png_get_uint_32(buf + 4);
2199 unit_type = buf[8];
2200 png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
2202 #endif
2204 #ifdef PNG_READ_oFFs_SUPPORTED
2205 void /* PRIVATE */
2206 png_handle_oFFs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2208 png_byte buf[9];
2209 png_int_32 offset_x, offset_y;
2210 int unit_type;
2212 png_debug(1, "in png_handle_oFFs");
2214 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2215 png_chunk_error(png_ptr, "missing IHDR");
2217 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2219 png_crc_finish(png_ptr, length);
2220 png_chunk_benign_error(png_ptr, "out of place");
2221 return;
2224 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs) != 0)
2226 png_crc_finish(png_ptr, length);
2227 png_chunk_benign_error(png_ptr, "duplicate");
2228 return;
2231 if (length != 9)
2233 png_crc_finish(png_ptr, length);
2234 png_chunk_benign_error(png_ptr, "invalid");
2235 return;
2238 png_crc_read(png_ptr, buf, 9);
2240 if (png_crc_finish(png_ptr, 0) != 0)
2241 return;
2243 offset_x = png_get_int_32(buf);
2244 offset_y = png_get_int_32(buf + 4);
2245 unit_type = buf[8];
2246 png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
2248 #endif
2250 #ifdef PNG_READ_pCAL_SUPPORTED
2251 /* Read the pCAL chunk (described in the PNG Extensions document) */
2252 void /* PRIVATE */
2253 png_handle_pCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2255 png_int_32 X0, X1;
2256 png_byte type, nparams;
2257 png_bytep buffer, buf, units, endptr;
2258 png_charpp params;
2259 int i;
2261 png_debug(1, "in png_handle_pCAL");
2263 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2264 png_chunk_error(png_ptr, "missing IHDR");
2266 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2268 png_crc_finish(png_ptr, length);
2269 png_chunk_benign_error(png_ptr, "out of place");
2270 return;
2273 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL) != 0)
2275 png_crc_finish(png_ptr, length);
2276 png_chunk_benign_error(png_ptr, "duplicate");
2277 return;
2280 png_debug1(2, "Allocating and reading pCAL chunk data (%u bytes)",
2281 length + 1);
2283 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
2285 if (buffer == NULL)
2287 png_crc_finish(png_ptr, length);
2288 png_chunk_benign_error(png_ptr, "out of memory");
2289 return;
2292 png_crc_read(png_ptr, buffer, length);
2294 if (png_crc_finish(png_ptr, 0) != 0)
2295 return;
2297 buffer[length] = 0; /* Null terminate the last string */
2299 png_debug(3, "Finding end of pCAL purpose string");
2300 for (buf = buffer; *buf; buf++)
2301 /* Empty loop */ ;
2303 endptr = buffer + length;
2305 /* We need to have at least 12 bytes after the purpose string
2306 * in order to get the parameter information.
2308 if (endptr - buf <= 12)
2310 png_chunk_benign_error(png_ptr, "invalid");
2311 return;
2314 png_debug(3, "Reading pCAL X0, X1, type, nparams, and units");
2315 X0 = png_get_int_32((png_bytep)buf+1);
2316 X1 = png_get_int_32((png_bytep)buf+5);
2317 type = buf[9];
2318 nparams = buf[10];
2319 units = buf + 11;
2321 png_debug(3, "Checking pCAL equation type and number of parameters");
2322 /* Check that we have the right number of parameters for known
2323 * equation types.
2325 if ((type == PNG_EQUATION_LINEAR && nparams != 2) ||
2326 (type == PNG_EQUATION_BASE_E && nparams != 3) ||
2327 (type == PNG_EQUATION_ARBITRARY && nparams != 3) ||
2328 (type == PNG_EQUATION_HYPERBOLIC && nparams != 4))
2330 png_chunk_benign_error(png_ptr, "invalid parameter count");
2331 return;
2334 else if (type >= PNG_EQUATION_LAST)
2336 png_chunk_benign_error(png_ptr, "unrecognized equation type");
2339 for (buf = units; *buf; buf++)
2340 /* Empty loop to move past the units string. */ ;
2342 png_debug(3, "Allocating pCAL parameters array");
2344 params = png_voidcast(png_charpp, png_malloc_warn(png_ptr,
2345 nparams * (sizeof (png_charp))));
2347 if (params == NULL)
2349 png_chunk_benign_error(png_ptr, "out of memory");
2350 return;
2353 /* Get pointers to the start of each parameter string. */
2354 for (i = 0; i < nparams; i++)
2356 buf++; /* Skip the null string terminator from previous parameter. */
2358 png_debug1(3, "Reading pCAL parameter %d", i);
2360 for (params[i] = (png_charp)buf; buf <= endptr && *buf != 0; buf++)
2361 /* Empty loop to move past each parameter string */ ;
2363 /* Make sure we haven't run out of data yet */
2364 if (buf > endptr)
2366 png_free(png_ptr, params);
2367 png_chunk_benign_error(png_ptr, "invalid data");
2368 return;
2372 png_set_pCAL(png_ptr, info_ptr, (png_charp)buffer, X0, X1, type, nparams,
2373 (png_charp)units, params);
2375 png_free(png_ptr, params);
2377 #endif
2379 #ifdef PNG_READ_sCAL_SUPPORTED
2380 /* Read the sCAL chunk */
2381 void /* PRIVATE */
2382 png_handle_sCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2384 png_bytep buffer;
2385 size_t i;
2386 int state;
2388 png_debug(1, "in png_handle_sCAL");
2390 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2391 png_chunk_error(png_ptr, "missing IHDR");
2393 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2395 png_crc_finish(png_ptr, length);
2396 png_chunk_benign_error(png_ptr, "out of place");
2397 return;
2400 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL) != 0)
2402 png_crc_finish(png_ptr, length);
2403 png_chunk_benign_error(png_ptr, "duplicate");
2404 return;
2407 /* Need unit type, width, \0, height: minimum 4 bytes */
2408 else if (length < 4)
2410 png_crc_finish(png_ptr, length);
2411 png_chunk_benign_error(png_ptr, "invalid");
2412 return;
2415 png_debug1(2, "Allocating and reading sCAL chunk data (%u bytes)",
2416 length + 1);
2418 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
2420 if (buffer == NULL)
2422 png_chunk_benign_error(png_ptr, "out of memory");
2423 png_crc_finish(png_ptr, length);
2424 return;
2427 png_crc_read(png_ptr, buffer, length);
2428 buffer[length] = 0; /* Null terminate the last string */
2430 if (png_crc_finish(png_ptr, 0) != 0)
2431 return;
2433 /* Validate the unit. */
2434 if (buffer[0] != 1 && buffer[0] != 2)
2436 png_chunk_benign_error(png_ptr, "invalid unit");
2437 return;
2440 /* Validate the ASCII numbers, need two ASCII numbers separated by
2441 * a '\0' and they need to fit exactly in the chunk data.
2443 i = 1;
2444 state = 0;
2446 if (png_check_fp_number((png_const_charp)buffer, length, &state, &i) == 0 ||
2447 i >= length || buffer[i++] != 0)
2448 png_chunk_benign_error(png_ptr, "bad width format");
2450 else if (PNG_FP_IS_POSITIVE(state) == 0)
2451 png_chunk_benign_error(png_ptr, "non-positive width");
2453 else
2455 size_t heighti = i;
2457 state = 0;
2458 if (png_check_fp_number((png_const_charp)buffer, length,
2459 &state, &i) == 0 || i != length)
2460 png_chunk_benign_error(png_ptr, "bad height format");
2462 else if (PNG_FP_IS_POSITIVE(state) == 0)
2463 png_chunk_benign_error(png_ptr, "non-positive height");
2465 else
2466 /* This is the (only) success case. */
2467 png_set_sCAL_s(png_ptr, info_ptr, buffer[0],
2468 (png_charp)buffer+1, (png_charp)buffer+heighti);
2471 #endif
2473 #ifdef PNG_READ_tIME_SUPPORTED
2474 void /* PRIVATE */
2475 png_handle_tIME(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2477 png_byte buf[7];
2478 png_time mod_time;
2480 png_debug(1, "in png_handle_tIME");
2482 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2483 png_chunk_error(png_ptr, "missing IHDR");
2485 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME) != 0)
2487 png_crc_finish(png_ptr, length);
2488 png_chunk_benign_error(png_ptr, "duplicate");
2489 return;
2492 if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2493 png_ptr->mode |= PNG_AFTER_IDAT;
2495 if (length != 7)
2497 png_crc_finish(png_ptr, length);
2498 png_chunk_benign_error(png_ptr, "invalid");
2499 return;
2502 png_crc_read(png_ptr, buf, 7);
2504 if (png_crc_finish(png_ptr, 0) != 0)
2505 return;
2507 mod_time.second = buf[6];
2508 mod_time.minute = buf[5];
2509 mod_time.hour = buf[4];
2510 mod_time.day = buf[3];
2511 mod_time.month = buf[2];
2512 mod_time.year = png_get_uint_16(buf);
2514 png_set_tIME(png_ptr, info_ptr, &mod_time);
2516 #endif
2518 #ifdef PNG_READ_tEXt_SUPPORTED
2519 /* Note: this does not properly handle chunks that are > 64K under DOS */
2520 void /* PRIVATE */
2521 png_handle_tEXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2523 png_text text_info;
2524 png_bytep buffer;
2525 png_charp key;
2526 png_charp text;
2527 png_uint_32 skip = 0;
2529 png_debug(1, "in png_handle_tEXt");
2531 #ifdef PNG_USER_LIMITS_SUPPORTED
2532 if (png_ptr->user_chunk_cache_max != 0)
2534 if (png_ptr->user_chunk_cache_max == 1)
2536 png_crc_finish(png_ptr, length);
2537 return;
2540 if (--png_ptr->user_chunk_cache_max == 1)
2542 png_crc_finish(png_ptr, length);
2543 png_chunk_benign_error(png_ptr, "no space in chunk cache");
2544 return;
2547 #endif
2549 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2550 png_chunk_error(png_ptr, "missing IHDR");
2552 if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2553 png_ptr->mode |= PNG_AFTER_IDAT;
2555 #ifdef PNG_MAX_MALLOC_64K
2556 if (length > 65535U)
2558 png_crc_finish(png_ptr, length);
2559 png_chunk_benign_error(png_ptr, "too large to fit in memory");
2560 return;
2562 #endif
2564 buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
2566 if (buffer == NULL)
2568 png_chunk_benign_error(png_ptr, "out of memory");
2569 return;
2572 png_crc_read(png_ptr, buffer, length);
2574 if (png_crc_finish(png_ptr, skip) != 0)
2575 return;
2577 key = (png_charp)buffer;
2578 key[length] = 0;
2580 for (text = key; *text; text++)
2581 /* Empty loop to find end of key */ ;
2583 if (text != key + length)
2584 text++;
2586 text_info.compression = PNG_TEXT_COMPRESSION_NONE;
2587 text_info.key = key;
2588 text_info.lang = NULL;
2589 text_info.lang_key = NULL;
2590 text_info.itxt_length = 0;
2591 text_info.text = text;
2592 text_info.text_length = strlen(text);
2594 if (png_set_text_2(png_ptr, info_ptr, &text_info, 1) != 0)
2595 png_warning(png_ptr, "Insufficient memory to process text chunk");
2597 #endif
2599 #ifdef PNG_READ_zTXt_SUPPORTED
2600 /* Note: this does not correctly handle chunks that are > 64K under DOS */
2601 void /* PRIVATE */
2602 png_handle_zTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2604 png_const_charp errmsg = NULL;
2605 png_bytep buffer;
2606 png_uint_32 keyword_length;
2608 png_debug(1, "in png_handle_zTXt");
2610 #ifdef PNG_USER_LIMITS_SUPPORTED
2611 if (png_ptr->user_chunk_cache_max != 0)
2613 if (png_ptr->user_chunk_cache_max == 1)
2615 png_crc_finish(png_ptr, length);
2616 return;
2619 if (--png_ptr->user_chunk_cache_max == 1)
2621 png_crc_finish(png_ptr, length);
2622 png_chunk_benign_error(png_ptr, "no space in chunk cache");
2623 return;
2626 #endif
2628 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2629 png_chunk_error(png_ptr, "missing IHDR");
2631 if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2632 png_ptr->mode |= PNG_AFTER_IDAT;
2634 /* Note, "length" is sufficient here; we won't be adding
2635 * a null terminator later.
2637 buffer = png_read_buffer(png_ptr, length, 2/*silent*/);
2639 if (buffer == NULL)
2641 png_crc_finish(png_ptr, length);
2642 png_chunk_benign_error(png_ptr, "out of memory");
2643 return;
2646 png_crc_read(png_ptr, buffer, length);
2648 if (png_crc_finish(png_ptr, 0) != 0)
2649 return;
2651 /* TODO: also check that the keyword contents match the spec! */
2652 for (keyword_length = 0;
2653 keyword_length < length && buffer[keyword_length] != 0;
2654 ++keyword_length)
2655 /* Empty loop to find end of name */ ;
2657 if (keyword_length > 79 || keyword_length < 1)
2658 errmsg = "bad keyword";
2660 /* zTXt must have some LZ data after the keyword, although it may expand to
2661 * zero bytes; we need a '\0' at the end of the keyword, the compression type
2662 * then the LZ data:
2664 else if (keyword_length + 3 > length)
2665 errmsg = "truncated";
2667 else if (buffer[keyword_length+1] != PNG_COMPRESSION_TYPE_BASE)
2668 errmsg = "unknown compression type";
2670 else
2672 png_alloc_size_t uncompressed_length = PNG_SIZE_MAX;
2674 /* TODO: at present png_decompress_chunk imposes a single application
2675 * level memory limit, this should be split to different values for iCCP
2676 * and text chunks.
2678 if (png_decompress_chunk(png_ptr, length, keyword_length+2,
2679 &uncompressed_length, 1/*terminate*/) == Z_STREAM_END)
2681 png_text text;
2683 if (png_ptr->read_buffer == NULL)
2684 errmsg="Read failure in png_handle_zTXt";
2685 else
2687 /* It worked; png_ptr->read_buffer now looks like a tEXt chunk
2688 * except for the extra compression type byte and the fact that
2689 * it isn't necessarily '\0' terminated.
2691 buffer = png_ptr->read_buffer;
2692 buffer[uncompressed_length+(keyword_length+2)] = 0;
2694 text.compression = PNG_TEXT_COMPRESSION_zTXt;
2695 text.key = (png_charp)buffer;
2696 text.text = (png_charp)(buffer + keyword_length+2);
2697 text.text_length = uncompressed_length;
2698 text.itxt_length = 0;
2699 text.lang = NULL;
2700 text.lang_key = NULL;
2702 if (png_set_text_2(png_ptr, info_ptr, &text, 1) != 0)
2703 errmsg = "insufficient memory";
2707 else
2708 errmsg = png_ptr->zstream.msg;
2711 if (errmsg != NULL)
2712 png_chunk_benign_error(png_ptr, errmsg);
2714 #endif
2716 #ifdef PNG_READ_iTXt_SUPPORTED
2717 /* Note: this does not correctly handle chunks that are > 64K under DOS */
2718 void /* PRIVATE */
2719 png_handle_iTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2721 png_const_charp errmsg = NULL;
2722 png_bytep buffer;
2723 png_uint_32 prefix_length;
2725 png_debug(1, "in png_handle_iTXt");
2727 #ifdef PNG_USER_LIMITS_SUPPORTED
2728 if (png_ptr->user_chunk_cache_max != 0)
2730 if (png_ptr->user_chunk_cache_max == 1)
2732 png_crc_finish(png_ptr, length);
2733 return;
2736 if (--png_ptr->user_chunk_cache_max == 1)
2738 png_crc_finish(png_ptr, length);
2739 png_chunk_benign_error(png_ptr, "no space in chunk cache");
2740 return;
2743 #endif
2745 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2746 png_chunk_error(png_ptr, "missing IHDR");
2748 if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2749 png_ptr->mode |= PNG_AFTER_IDAT;
2751 buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
2753 if (buffer == NULL)
2755 png_crc_finish(png_ptr, length);
2756 png_chunk_benign_error(png_ptr, "out of memory");
2757 return;
2760 png_crc_read(png_ptr, buffer, length);
2762 if (png_crc_finish(png_ptr, 0) != 0)
2763 return;
2765 /* First the keyword. */
2766 for (prefix_length=0;
2767 prefix_length < length && buffer[prefix_length] != 0;
2768 ++prefix_length)
2769 /* Empty loop */ ;
2771 /* Perform a basic check on the keyword length here. */
2772 if (prefix_length > 79 || prefix_length < 1)
2773 errmsg = "bad keyword";
2775 /* Expect keyword, compression flag, compression type, language, translated
2776 * keyword (both may be empty but are 0 terminated) then the text, which may
2777 * be empty.
2779 else if (prefix_length + 5 > length)
2780 errmsg = "truncated";
2782 else if (buffer[prefix_length+1] == 0 ||
2783 (buffer[prefix_length+1] == 1 &&
2784 buffer[prefix_length+2] == PNG_COMPRESSION_TYPE_BASE))
2786 int compressed = buffer[prefix_length+1] != 0;
2787 png_uint_32 language_offset, translated_keyword_offset;
2788 png_alloc_size_t uncompressed_length = 0;
2790 /* Now the language tag */
2791 prefix_length += 3;
2792 language_offset = prefix_length;
2794 for (; prefix_length < length && buffer[prefix_length] != 0;
2795 ++prefix_length)
2796 /* Empty loop */ ;
2798 /* WARNING: the length may be invalid here, this is checked below. */
2799 translated_keyword_offset = ++prefix_length;
2801 for (; prefix_length < length && buffer[prefix_length] != 0;
2802 ++prefix_length)
2803 /* Empty loop */ ;
2805 /* prefix_length should now be at the trailing '\0' of the translated
2806 * keyword, but it may already be over the end. None of this arithmetic
2807 * can overflow because chunks are at most 2^31 bytes long, but on 16-bit
2808 * systems the available allocation may overflow.
2810 ++prefix_length;
2812 if (compressed == 0 && prefix_length <= length)
2813 uncompressed_length = length - prefix_length;
2815 else if (compressed != 0 && prefix_length < length)
2817 uncompressed_length = PNG_SIZE_MAX;
2819 /* TODO: at present png_decompress_chunk imposes a single application
2820 * level memory limit, this should be split to different values for
2821 * iCCP and text chunks.
2823 if (png_decompress_chunk(png_ptr, length, prefix_length,
2824 &uncompressed_length, 1/*terminate*/) == Z_STREAM_END)
2825 buffer = png_ptr->read_buffer;
2827 else
2828 errmsg = png_ptr->zstream.msg;
2831 else
2832 errmsg = "truncated";
2834 if (errmsg == NULL)
2836 png_text text;
2838 buffer[uncompressed_length+prefix_length] = 0;
2840 if (compressed == 0)
2841 text.compression = PNG_ITXT_COMPRESSION_NONE;
2843 else
2844 text.compression = PNG_ITXT_COMPRESSION_zTXt;
2846 text.key = (png_charp)buffer;
2847 text.lang = (png_charp)buffer + language_offset;
2848 text.lang_key = (png_charp)buffer + translated_keyword_offset;
2849 text.text = (png_charp)buffer + prefix_length;
2850 text.text_length = 0;
2851 text.itxt_length = uncompressed_length;
2853 if (png_set_text_2(png_ptr, info_ptr, &text, 1) != 0)
2854 errmsg = "insufficient memory";
2858 else
2859 errmsg = "bad compression info";
2861 if (errmsg != NULL)
2862 png_chunk_benign_error(png_ptr, errmsg);
2864 #endif
2866 #ifdef PNG_READ_APNG_SUPPORTED
2867 void /* PRIVATE */
2868 png_handle_acTL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
2870 png_byte data[8];
2871 png_uint_32 num_frames;
2872 png_uint_32 num_plays;
2873 png_uint_32 didSet;
2875 png_debug(1, "in png_handle_acTL");
2877 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2879 png_error(png_ptr, "Missing IHDR before acTL");
2881 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2883 png_warning(png_ptr, "Invalid acTL after IDAT skipped");
2884 png_crc_finish(png_ptr, length);
2885 return;
2887 else if ((png_ptr->mode & PNG_HAVE_acTL) != 0)
2889 png_warning(png_ptr, "Duplicate acTL skipped");
2890 png_crc_finish(png_ptr, length);
2891 return;
2893 else if (length != 8)
2895 png_warning(png_ptr, "acTL with invalid length skipped");
2896 png_crc_finish(png_ptr, length);
2897 return;
2900 png_crc_read(png_ptr, data, 8);
2901 png_crc_finish(png_ptr, 0);
2903 num_frames = png_get_uint_31(png_ptr, data);
2904 num_plays = png_get_uint_31(png_ptr, data + 4);
2906 /* the set function will do error checking on num_frames */
2907 didSet = png_set_acTL(png_ptr, info_ptr, num_frames, num_plays);
2908 if (didSet != 0)
2909 png_ptr->mode |= PNG_HAVE_acTL;
2912 void /* PRIVATE */
2913 png_handle_fcTL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
2915 png_byte data[22];
2916 png_uint_32 width;
2917 png_uint_32 height;
2918 png_uint_32 x_offset;
2919 png_uint_32 y_offset;
2920 png_uint_16 delay_num;
2921 png_uint_16 delay_den;
2922 png_byte dispose_op;
2923 png_byte blend_op;
2925 png_debug(1, "in png_handle_fcTL");
2927 png_ensure_sequence_number(png_ptr, length);
2929 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2931 png_error(png_ptr, "Missing IHDR before fcTL");
2933 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2935 /* for any frames other then the first this message may be misleading,
2936 * but correct. PNG_HAVE_IDAT is unset before the frame head is read
2937 * i can't think of a better message */
2938 png_warning(png_ptr, "Invalid fcTL after IDAT skipped");
2939 png_crc_finish(png_ptr, length-4);
2940 return;
2942 else if ((png_ptr->mode & PNG_HAVE_fcTL) != 0)
2944 png_warning(png_ptr, "Duplicate fcTL within one frame skipped");
2945 png_crc_finish(png_ptr, length-4);
2946 return;
2948 else if (length != 26)
2950 png_warning(png_ptr, "fcTL with invalid length skipped");
2951 png_crc_finish(png_ptr, length-4);
2952 return;
2955 png_crc_read(png_ptr, data, 22);
2956 png_crc_finish(png_ptr, 0);
2958 width = png_get_uint_31(png_ptr, data);
2959 height = png_get_uint_31(png_ptr, data + 4);
2960 x_offset = png_get_uint_31(png_ptr, data + 8);
2961 y_offset = png_get_uint_31(png_ptr, data + 12);
2962 delay_num = png_get_uint_16(data + 16);
2963 delay_den = png_get_uint_16(data + 18);
2964 dispose_op = data[20];
2965 blend_op = data[21];
2967 if (png_ptr->num_frames_read == 0 && (x_offset != 0 || y_offset != 0))
2969 png_warning(png_ptr, "fcTL for the first frame must have zero offset");
2970 return;
2973 if (info_ptr != NULL)
2975 if (png_ptr->num_frames_read == 0 &&
2976 (width != info_ptr->width || height != info_ptr->height))
2978 png_warning(png_ptr, "size in first frame's fcTL must match "
2979 "the size in IHDR");
2980 return;
2983 /* The set function will do more error checking */
2984 png_set_next_frame_fcTL(png_ptr, info_ptr, width, height,
2985 x_offset, y_offset, delay_num, delay_den,
2986 dispose_op, blend_op);
2988 png_read_reinit(png_ptr, info_ptr);
2990 png_ptr->mode |= PNG_HAVE_fcTL;
2994 void /* PRIVATE */
2995 png_have_info(png_structp png_ptr, png_infop info_ptr)
2997 if ((info_ptr->valid & PNG_INFO_acTL) != 0 &&
2998 (info_ptr->valid & PNG_INFO_fcTL) == 0)
3000 png_ptr->apng_flags |= PNG_FIRST_FRAME_HIDDEN;
3001 info_ptr->num_frames++;
3005 void /* PRIVATE */
3006 png_handle_fdAT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
3008 png_ensure_sequence_number(png_ptr, length);
3010 /* This function is only called from png_read_end(), png_read_info(),
3011 * and png_push_read_chunk() which means that:
3012 * - the user doesn't want to read this frame
3013 * - or this is an out-of-place fdAT
3014 * in either case it is safe to ignore the chunk with a warning */
3015 png_warning(png_ptr, "ignoring fdAT chunk");
3016 png_crc_finish(png_ptr, length - 4);
3017 PNG_UNUSED(info_ptr)
3020 void /* PRIVATE */
3021 png_ensure_sequence_number(png_structp png_ptr, png_uint_32 length)
3023 png_byte data[4];
3024 png_uint_32 sequence_number;
3026 if (length < 4)
3027 png_error(png_ptr, "invalid fcTL or fdAT chunk found");
3029 png_crc_read(png_ptr, data, 4);
3030 sequence_number = png_get_uint_31(png_ptr, data);
3032 if (sequence_number != png_ptr->next_seq_num)
3033 png_error(png_ptr, "fcTL or fdAT chunk with out-of-order sequence "
3034 "number found");
3036 png_ptr->next_seq_num++;
3038 #endif /* READ_APNG */
3040 #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
3041 /* Utility function for png_handle_unknown; set up png_ptr::unknown_chunk */
3042 static int
3043 png_cache_unknown_chunk(png_structrp png_ptr, png_uint_32 length)
3045 png_alloc_size_t limit = PNG_SIZE_MAX;
3047 if (png_ptr->unknown_chunk.data != NULL)
3049 png_free(png_ptr, png_ptr->unknown_chunk.data);
3050 png_ptr->unknown_chunk.data = NULL;
3053 # ifdef PNG_SET_USER_LIMITS_SUPPORTED
3054 if (png_ptr->user_chunk_malloc_max > 0 &&
3055 png_ptr->user_chunk_malloc_max < limit)
3056 limit = png_ptr->user_chunk_malloc_max;
3058 # elif PNG_USER_CHUNK_MALLOC_MAX > 0
3059 if (PNG_USER_CHUNK_MALLOC_MAX < limit)
3060 limit = PNG_USER_CHUNK_MALLOC_MAX;
3061 # endif
3063 if (length <= limit)
3065 PNG_CSTRING_FROM_CHUNK(png_ptr->unknown_chunk.name, png_ptr->chunk_name);
3066 /* The following is safe because of the PNG_SIZE_MAX init above */
3067 png_ptr->unknown_chunk.size = (size_t)length/*SAFE*/;
3068 /* 'mode' is a flag array, only the bottom four bits matter here */
3069 png_ptr->unknown_chunk.location = (png_byte)png_ptr->mode/*SAFE*/;
3071 if (length == 0)
3072 png_ptr->unknown_chunk.data = NULL;
3074 else
3076 /* Do a 'warn' here - it is handled below. */
3077 png_ptr->unknown_chunk.data = png_voidcast(png_bytep,
3078 png_malloc_warn(png_ptr, length));
3082 if (png_ptr->unknown_chunk.data == NULL && length > 0)
3084 /* This is benign because we clean up correctly */
3085 png_crc_finish(png_ptr, length);
3086 png_chunk_benign_error(png_ptr, "unknown chunk exceeds memory limits");
3087 return 0;
3090 else
3092 if (length > 0)
3093 png_crc_read(png_ptr, png_ptr->unknown_chunk.data, length);
3094 png_crc_finish(png_ptr, 0);
3095 return 1;
3098 #endif /* READ_UNKNOWN_CHUNKS */
3100 /* Handle an unknown, or known but disabled, chunk */
3101 void /* PRIVATE */
3102 png_handle_unknown(png_structrp png_ptr, png_inforp info_ptr,
3103 png_uint_32 length, int keep)
3105 int handled = 0; /* the chunk was handled */
3107 png_debug(1, "in png_handle_unknown");
3109 #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
3110 /* NOTE: this code is based on the code in libpng-1.4.12 except for fixing
3111 * the bug which meant that setting a non-default behavior for a specific
3112 * chunk would be ignored (the default was always used unless a user
3113 * callback was installed).
3115 * 'keep' is the value from the png_chunk_unknown_handling, the setting for
3116 * this specific chunk_name, if PNG_HANDLE_AS_UNKNOWN_SUPPORTED, if not it
3117 * will always be PNG_HANDLE_CHUNK_AS_DEFAULT and it needs to be set here.
3118 * This is just an optimization to avoid multiple calls to the lookup
3119 * function.
3121 # ifndef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
3122 # ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
3123 keep = png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name);
3124 # endif
3125 # endif
3127 /* One of the following methods will read the chunk or skip it (at least one
3128 * of these is always defined because this is the only way to switch on
3129 * PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
3131 # ifdef PNG_READ_USER_CHUNKS_SUPPORTED
3132 /* The user callback takes precedence over the chunk keep value, but the
3133 * keep value is still required to validate a save of a critical chunk.
3135 if (png_ptr->read_user_chunk_fn != NULL)
3137 if (png_cache_unknown_chunk(png_ptr, length) != 0)
3139 /* Callback to user unknown chunk handler */
3140 int ret = (*(png_ptr->read_user_chunk_fn))(png_ptr,
3141 &png_ptr->unknown_chunk);
3143 /* ret is:
3144 * negative: An error occurred; png_chunk_error will be called.
3145 * zero: The chunk was not handled, the chunk will be discarded
3146 * unless png_set_keep_unknown_chunks has been used to set
3147 * a 'keep' behavior for this particular chunk, in which
3148 * case that will be used. A critical chunk will cause an
3149 * error at this point unless it is to be saved.
3150 * positive: The chunk was handled, libpng will ignore/discard it.
3152 if (ret < 0)
3153 png_chunk_error(png_ptr, "error in user chunk");
3155 else if (ret == 0)
3157 /* If the keep value is 'default' or 'never' override it, but
3158 * still error out on critical chunks unless the keep value is
3159 * 'always' While this is weird it is the behavior in 1.4.12.
3160 * A possible improvement would be to obey the value set for the
3161 * chunk, but this would be an API change that would probably
3162 * damage some applications.
3164 * The png_app_warning below catches the case that matters, where
3165 * the application has not set specific save or ignore for this
3166 * chunk or global save or ignore.
3168 if (keep < PNG_HANDLE_CHUNK_IF_SAFE)
3170 # ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
3171 if (png_ptr->unknown_default < PNG_HANDLE_CHUNK_IF_SAFE)
3173 png_chunk_warning(png_ptr, "Saving unknown chunk:");
3174 png_app_warning(png_ptr,
3175 "forcing save of an unhandled chunk;"
3176 " please call png_set_keep_unknown_chunks");
3177 /* with keep = PNG_HANDLE_CHUNK_IF_SAFE */
3179 # endif
3180 keep = PNG_HANDLE_CHUNK_IF_SAFE;
3184 else /* chunk was handled */
3186 handled = 1;
3187 /* Critical chunks can be safely discarded at this point. */
3188 keep = PNG_HANDLE_CHUNK_NEVER;
3192 else
3193 keep = PNG_HANDLE_CHUNK_NEVER; /* insufficient memory */
3196 else
3197 /* Use the SAVE_UNKNOWN_CHUNKS code or skip the chunk */
3198 # endif /* READ_USER_CHUNKS */
3200 # ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
3202 /* keep is currently just the per-chunk setting, if there was no
3203 * setting change it to the global default now (not that this may
3204 * still be AS_DEFAULT) then obtain the cache of the chunk if required,
3205 * if not simply skip the chunk.
3207 if (keep == PNG_HANDLE_CHUNK_AS_DEFAULT)
3208 keep = png_ptr->unknown_default;
3210 if (keep == PNG_HANDLE_CHUNK_ALWAYS ||
3211 (keep == PNG_HANDLE_CHUNK_IF_SAFE &&
3212 PNG_CHUNK_ANCILLARY(png_ptr->chunk_name)))
3214 if (png_cache_unknown_chunk(png_ptr, length) == 0)
3215 keep = PNG_HANDLE_CHUNK_NEVER;
3218 else
3219 png_crc_finish(png_ptr, length);
3221 # else
3222 # ifndef PNG_READ_USER_CHUNKS_SUPPORTED
3223 # error no method to support READ_UNKNOWN_CHUNKS
3224 # endif
3227 /* If here there is no read callback pointer set and no support is
3228 * compiled in to just save the unknown chunks, so simply skip this
3229 * chunk. If 'keep' is something other than AS_DEFAULT or NEVER then
3230 * the app has erroneously asked for unknown chunk saving when there
3231 * is no support.
3233 if (keep > PNG_HANDLE_CHUNK_NEVER)
3234 png_app_error(png_ptr, "no unknown chunk support available");
3236 png_crc_finish(png_ptr, length);
3238 # endif
3240 # ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
3241 /* Now store the chunk in the chunk list if appropriate, and if the limits
3242 * permit it.
3244 if (keep == PNG_HANDLE_CHUNK_ALWAYS ||
3245 (keep == PNG_HANDLE_CHUNK_IF_SAFE &&
3246 PNG_CHUNK_ANCILLARY(png_ptr->chunk_name)))
3248 # ifdef PNG_USER_LIMITS_SUPPORTED
3249 switch (png_ptr->user_chunk_cache_max)
3251 case 2:
3252 png_ptr->user_chunk_cache_max = 1;
3253 png_chunk_benign_error(png_ptr, "no space in chunk cache");
3254 /* FALLTHROUGH */
3255 case 1:
3256 /* NOTE: prior to 1.6.0 this case resulted in an unknown critical
3257 * chunk being skipped, now there will be a hard error below.
3259 break;
3261 default: /* not at limit */
3262 --(png_ptr->user_chunk_cache_max);
3263 /* FALLTHROUGH */
3264 case 0: /* no limit */
3265 # endif /* USER_LIMITS */
3266 /* Here when the limit isn't reached or when limits are compiled
3267 * out; store the chunk.
3269 png_set_unknown_chunks(png_ptr, info_ptr,
3270 &png_ptr->unknown_chunk, 1);
3271 handled = 1;
3272 # ifdef PNG_USER_LIMITS_SUPPORTED
3273 break;
3275 # endif
3277 # else /* no store support: the chunk must be handled by the user callback */
3278 PNG_UNUSED(info_ptr)
3279 # endif
3281 /* Regardless of the error handling below the cached data (if any) can be
3282 * freed now. Notice that the data is not freed if there is a png_error, but
3283 * it will be freed by destroy_read_struct.
3285 if (png_ptr->unknown_chunk.data != NULL)
3286 png_free(png_ptr, png_ptr->unknown_chunk.data);
3287 png_ptr->unknown_chunk.data = NULL;
3289 #else /* !PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */
3290 /* There is no support to read an unknown chunk, so just skip it. */
3291 png_crc_finish(png_ptr, length);
3292 PNG_UNUSED(info_ptr)
3293 PNG_UNUSED(keep)
3294 #endif /* !READ_UNKNOWN_CHUNKS */
3296 /* Check for unhandled critical chunks */
3297 if (handled == 0 && PNG_CHUNK_CRITICAL(png_ptr->chunk_name))
3298 png_chunk_error(png_ptr, "unhandled critical chunk");
3301 /* This function is called to verify that a chunk name is valid.
3302 * This function can't have the "critical chunk check" incorporated
3303 * into it, since in the future we will need to be able to call user
3304 * functions to handle unknown critical chunks after we check that
3305 * the chunk name itself is valid.
3308 /* Bit hacking: the test for an invalid byte in the 4 byte chunk name is:
3310 * ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
3313 void /* PRIVATE */
3314 png_check_chunk_name(png_const_structrp png_ptr, png_uint_32 chunk_name)
3316 int i;
3317 png_uint_32 cn=chunk_name;
3319 png_debug(1, "in png_check_chunk_name");
3321 for (i=1; i<=4; ++i)
3323 int c = cn & 0xff;
3325 if (c < 65 || c > 122 || (c > 90 && c < 97))
3326 png_chunk_error(png_ptr, "invalid chunk type");
3328 cn >>= 8;
3332 void /* PRIVATE */
3333 png_check_chunk_length(png_const_structrp png_ptr, png_uint_32 length)
3335 png_alloc_size_t limit = PNG_UINT_31_MAX;
3337 # ifdef PNG_SET_USER_LIMITS_SUPPORTED
3338 if (png_ptr->user_chunk_malloc_max > 0 &&
3339 png_ptr->user_chunk_malloc_max < limit)
3340 limit = png_ptr->user_chunk_malloc_max;
3341 # elif PNG_USER_CHUNK_MALLOC_MAX > 0
3342 if (PNG_USER_CHUNK_MALLOC_MAX < limit)
3343 limit = PNG_USER_CHUNK_MALLOC_MAX;
3344 # endif
3345 #ifdef PNG_READ_APNG_SUPPORTED
3346 if (png_ptr->chunk_name == png_IDAT || png_ptr->chunk_name == png_fdAT)
3347 #else
3348 if (png_ptr->chunk_name == png_IDAT)
3349 #endif
3351 png_alloc_size_t idat_limit = PNG_UINT_31_MAX;
3352 size_t row_factor =
3353 (size_t)png_ptr->width
3354 * (size_t)png_ptr->channels
3355 * (png_ptr->bit_depth > 8? 2: 1)
3357 + (png_ptr->interlaced? 6: 0);
3358 if (png_ptr->height > PNG_UINT_32_MAX/row_factor)
3359 idat_limit = PNG_UINT_31_MAX;
3360 else
3361 idat_limit = png_ptr->height * row_factor;
3362 row_factor = row_factor > 32566? 32566 : row_factor;
3363 idat_limit += 6 + 5*(idat_limit/row_factor+1); /* zlib+deflate overhead */
3364 idat_limit=idat_limit < PNG_UINT_31_MAX? idat_limit : PNG_UINT_31_MAX;
3365 limit = limit < idat_limit? idat_limit : limit;
3368 if (length > limit)
3370 png_debug2(0," length = %lu, limit = %lu",
3371 (unsigned long)length,(unsigned long)limit);
3372 png_benign_error(png_ptr, "chunk data is too large");
3376 /* Combines the row recently read in with the existing pixels in the row. This
3377 * routine takes care of alpha and transparency if requested. This routine also
3378 * handles the two methods of progressive display of interlaced images,
3379 * depending on the 'display' value; if 'display' is true then the whole row
3380 * (dp) is filled from the start by replicating the available pixels. If
3381 * 'display' is false only those pixels present in the pass are filled in.
3383 void /* PRIVATE */
3384 png_combine_row(png_const_structrp png_ptr, png_bytep dp, int display)
3386 unsigned int pixel_depth = png_ptr->transformed_pixel_depth;
3387 png_const_bytep sp = png_ptr->row_buf + 1;
3388 png_alloc_size_t row_width = png_ptr->width;
3389 unsigned int pass = png_ptr->pass;
3390 png_bytep end_ptr = 0;
3391 png_byte end_byte = 0;
3392 unsigned int end_mask;
3394 png_debug(1, "in png_combine_row");
3396 /* Added in 1.5.6: it should not be possible to enter this routine until at
3397 * least one row has been read from the PNG data and transformed.
3399 if (pixel_depth == 0)
3400 png_error(png_ptr, "internal row logic error");
3402 /* Added in 1.5.4: the pixel depth should match the information returned by
3403 * any call to png_read_update_info at this point. Do not continue if we got
3404 * this wrong.
3406 if (png_ptr->info_rowbytes != 0 && png_ptr->info_rowbytes !=
3407 PNG_ROWBYTES(pixel_depth, row_width))
3408 png_error(png_ptr, "internal row size calculation error");
3410 /* Don't expect this to ever happen: */
3411 if (row_width == 0)
3412 png_error(png_ptr, "internal row width error");
3414 /* Preserve the last byte in cases where only part of it will be overwritten,
3415 * the multiply below may overflow, we don't care because ANSI-C guarantees
3416 * we get the low bits.
3418 end_mask = (pixel_depth * row_width) & 7;
3419 if (end_mask != 0)
3421 /* end_ptr == NULL is a flag to say do nothing */
3422 end_ptr = dp + PNG_ROWBYTES(pixel_depth, row_width) - 1;
3423 end_byte = *end_ptr;
3424 # ifdef PNG_READ_PACKSWAP_SUPPORTED
3425 if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
3426 /* little-endian byte */
3427 end_mask = (unsigned int)(0xff << end_mask);
3429 else /* big-endian byte */
3430 # endif
3431 end_mask = 0xff >> end_mask;
3432 /* end_mask is now the bits to *keep* from the destination row */
3435 /* For non-interlaced images this reduces to a memcpy(). A memcpy()
3436 * will also happen if interlacing isn't supported or if the application
3437 * does not call png_set_interlace_handling(). In the latter cases the
3438 * caller just gets a sequence of the unexpanded rows from each interlace
3439 * pass.
3441 #ifdef PNG_READ_INTERLACING_SUPPORTED
3442 if (png_ptr->interlaced != 0 &&
3443 (png_ptr->transformations & PNG_INTERLACE) != 0 &&
3444 pass < 6 && (display == 0 ||
3445 /* The following copies everything for 'display' on passes 0, 2 and 4. */
3446 (display == 1 && (pass & 1) != 0)))
3448 /* Narrow images may have no bits in a pass; the caller should handle
3449 * this, but this test is cheap:
3451 if (row_width <= PNG_PASS_START_COL(pass))
3452 return;
3454 if (pixel_depth < 8)
3456 /* For pixel depths up to 4 bpp the 8-pixel mask can be expanded to fit
3457 * into 32 bits, then a single loop over the bytes using the four byte
3458 * values in the 32-bit mask can be used. For the 'display' option the
3459 * expanded mask may also not require any masking within a byte. To
3460 * make this work the PACKSWAP option must be taken into account - it
3461 * simply requires the pixels to be reversed in each byte.
3463 * The 'regular' case requires a mask for each of the first 6 passes,
3464 * the 'display' case does a copy for the even passes in the range
3465 * 0..6. This has already been handled in the test above.
3467 * The masks are arranged as four bytes with the first byte to use in
3468 * the lowest bits (little-endian) regardless of the order (PACKSWAP or
3469 * not) of the pixels in each byte.
3471 * NOTE: the whole of this logic depends on the caller of this function
3472 * only calling it on rows appropriate to the pass. This function only
3473 * understands the 'x' logic; the 'y' logic is handled by the caller.
3475 * The following defines allow generation of compile time constant bit
3476 * masks for each pixel depth and each possibility of swapped or not
3477 * swapped bytes. Pass 'p' is in the range 0..6; 'x', a pixel index,
3478 * is in the range 0..7; and the result is 1 if the pixel is to be
3479 * copied in the pass, 0 if not. 'S' is for the sparkle method, 'B'
3480 * for the block method.
3482 * With some compilers a compile time expression of the general form:
3484 * (shift >= 32) ? (a >> (shift-32)) : (b >> shift)
3486 * Produces warnings with values of 'shift' in the range 33 to 63
3487 * because the right hand side of the ?: expression is evaluated by
3488 * the compiler even though it isn't used. Microsoft Visual C (various
3489 * versions) and the Intel C compiler are known to do this. To avoid
3490 * this the following macros are used in 1.5.6. This is a temporary
3491 * solution to avoid destabilizing the code during the release process.
3493 # if PNG_USE_COMPILE_TIME_MASKS
3494 # define PNG_LSR(x,s) ((x)>>((s) & 0x1f))
3495 # define PNG_LSL(x,s) ((x)<<((s) & 0x1f))
3496 # else
3497 # define PNG_LSR(x,s) ((x)>>(s))
3498 # define PNG_LSL(x,s) ((x)<<(s))
3499 # endif
3500 # define S_COPY(p,x) (((p)<4 ? PNG_LSR(0x80088822,(3-(p))*8+(7-(x))) :\
3501 PNG_LSR(0xaa55ff00,(7-(p))*8+(7-(x)))) & 1)
3502 # define B_COPY(p,x) (((p)<4 ? PNG_LSR(0xff0fff33,(3-(p))*8+(7-(x))) :\
3503 PNG_LSR(0xff55ff00,(7-(p))*8+(7-(x)))) & 1)
3505 /* Return a mask for pass 'p' pixel 'x' at depth 'd'. The mask is
3506 * little endian - the first pixel is at bit 0 - however the extra
3507 * parameter 's' can be set to cause the mask position to be swapped
3508 * within each byte, to match the PNG format. This is done by XOR of
3509 * the shift with 7, 6 or 4 for bit depths 1, 2 and 4.
3511 # define PIXEL_MASK(p,x,d,s) \
3512 (PNG_LSL(((PNG_LSL(1U,(d)))-1),(((x)*(d))^((s)?8-(d):0))))
3514 /* Hence generate the appropriate 'block' or 'sparkle' pixel copy mask.
3516 # define S_MASKx(p,x,d,s) (S_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
3517 # define B_MASKx(p,x,d,s) (B_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
3519 /* Combine 8 of these to get the full mask. For the 1-bpp and 2-bpp
3520 * cases the result needs replicating, for the 4-bpp case the above
3521 * generates a full 32 bits.
3523 # define MASK_EXPAND(m,d) ((m)*((d)==1?0x01010101:((d)==2?0x00010001:1)))
3525 # define S_MASK(p,d,s) MASK_EXPAND(S_MASKx(p,0,d,s) + S_MASKx(p,1,d,s) +\
3526 S_MASKx(p,2,d,s) + S_MASKx(p,3,d,s) + S_MASKx(p,4,d,s) +\
3527 S_MASKx(p,5,d,s) + S_MASKx(p,6,d,s) + S_MASKx(p,7,d,s), d)
3529 # define B_MASK(p,d,s) MASK_EXPAND(B_MASKx(p,0,d,s) + B_MASKx(p,1,d,s) +\
3530 B_MASKx(p,2,d,s) + B_MASKx(p,3,d,s) + B_MASKx(p,4,d,s) +\
3531 B_MASKx(p,5,d,s) + B_MASKx(p,6,d,s) + B_MASKx(p,7,d,s), d)
3533 #if PNG_USE_COMPILE_TIME_MASKS
3534 /* Utility macros to construct all the masks for a depth/swap
3535 * combination. The 's' parameter says whether the format is PNG
3536 * (big endian bytes) or not. Only the three odd-numbered passes are
3537 * required for the display/block algorithm.
3539 # define S_MASKS(d,s) { S_MASK(0,d,s), S_MASK(1,d,s), S_MASK(2,d,s),\
3540 S_MASK(3,d,s), S_MASK(4,d,s), S_MASK(5,d,s) }
3542 # define B_MASKS(d,s) { B_MASK(1,d,s), B_MASK(3,d,s), B_MASK(5,d,s) }
3544 # define DEPTH_INDEX(d) ((d)==1?0:((d)==2?1:2))
3546 /* Hence the pre-compiled masks indexed by PACKSWAP (or not), depth and
3547 * then pass:
3549 static const png_uint_32 row_mask[2/*PACKSWAP*/][3/*depth*/][6] =
3551 /* Little-endian byte masks for PACKSWAP */
3552 { S_MASKS(1,0), S_MASKS(2,0), S_MASKS(4,0) },
3553 /* Normal (big-endian byte) masks - PNG format */
3554 { S_MASKS(1,1), S_MASKS(2,1), S_MASKS(4,1) }
3557 /* display_mask has only three entries for the odd passes, so index by
3558 * pass>>1.
3560 static const png_uint_32 display_mask[2][3][3] =
3562 /* Little-endian byte masks for PACKSWAP */
3563 { B_MASKS(1,0), B_MASKS(2,0), B_MASKS(4,0) },
3564 /* Normal (big-endian byte) masks - PNG format */
3565 { B_MASKS(1,1), B_MASKS(2,1), B_MASKS(4,1) }
3568 # define MASK(pass,depth,display,png)\
3569 ((display)?display_mask[png][DEPTH_INDEX(depth)][pass>>1]:\
3570 row_mask[png][DEPTH_INDEX(depth)][pass])
3572 #else /* !PNG_USE_COMPILE_TIME_MASKS */
3573 /* This is the runtime alternative: it seems unlikely that this will
3574 * ever be either smaller or faster than the compile time approach.
3576 # define MASK(pass,depth,display,png)\
3577 ((display)?B_MASK(pass,depth,png):S_MASK(pass,depth,png))
3578 #endif /* !USE_COMPILE_TIME_MASKS */
3580 /* Use the appropriate mask to copy the required bits. In some cases
3581 * the byte mask will be 0 or 0xff; optimize these cases. row_width is
3582 * the number of pixels, but the code copies bytes, so it is necessary
3583 * to special case the end.
3585 png_uint_32 pixels_per_byte = 8 / pixel_depth;
3586 png_uint_32 mask;
3588 # ifdef PNG_READ_PACKSWAP_SUPPORTED
3589 if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
3590 mask = MASK(pass, pixel_depth, display, 0);
3592 else
3593 # endif
3594 mask = MASK(pass, pixel_depth, display, 1);
3596 for (;;)
3598 png_uint_32 m;
3600 /* It doesn't matter in the following if png_uint_32 has more than
3601 * 32 bits because the high bits always match those in m<<24; it is,
3602 * however, essential to use OR here, not +, because of this.
3604 m = mask;
3605 mask = (m >> 8) | (m << 24); /* rotate right to good compilers */
3606 m &= 0xff;
3608 if (m != 0) /* something to copy */
3610 if (m != 0xff)
3611 *dp = (png_byte)((*dp & ~m) | (*sp & m));
3612 else
3613 *dp = *sp;
3616 /* NOTE: this may overwrite the last byte with garbage if the image
3617 * is not an exact number of bytes wide; libpng has always done
3618 * this.
3620 if (row_width <= pixels_per_byte)
3621 break; /* May need to restore part of the last byte */
3623 row_width -= pixels_per_byte;
3624 ++dp;
3625 ++sp;
3629 else /* pixel_depth >= 8 */
3631 unsigned int bytes_to_copy, bytes_to_jump;
3633 /* Validate the depth - it must be a multiple of 8 */
3634 if (pixel_depth & 7)
3635 png_error(png_ptr, "invalid user transform pixel depth");
3637 pixel_depth >>= 3; /* now in bytes */
3638 row_width *= pixel_depth;
3640 /* Regardless of pass number the Adam 7 interlace always results in a
3641 * fixed number of pixels to copy then to skip. There may be a
3642 * different number of pixels to skip at the start though.
3645 unsigned int offset = PNG_PASS_START_COL(pass) * pixel_depth;
3647 row_width -= offset;
3648 dp += offset;
3649 sp += offset;
3652 /* Work out the bytes to copy. */
3653 if (display != 0)
3655 /* When doing the 'block' algorithm the pixel in the pass gets
3656 * replicated to adjacent pixels. This is why the even (0,2,4,6)
3657 * passes are skipped above - the entire expanded row is copied.
3659 bytes_to_copy = (1<<((6-pass)>>1)) * pixel_depth;
3661 /* But don't allow this number to exceed the actual row width. */
3662 if (bytes_to_copy > row_width)
3663 bytes_to_copy = (unsigned int)/*SAFE*/row_width;
3666 else /* normal row; Adam7 only ever gives us one pixel to copy. */
3667 bytes_to_copy = pixel_depth;
3669 /* In Adam7 there is a constant offset between where the pixels go. */
3670 bytes_to_jump = PNG_PASS_COL_OFFSET(pass) * pixel_depth;
3672 /* And simply copy these bytes. Some optimization is possible here,
3673 * depending on the value of 'bytes_to_copy'. Special case the low
3674 * byte counts, which we know to be frequent.
3676 * Notice that these cases all 'return' rather than 'break' - this
3677 * avoids an unnecessary test on whether to restore the last byte
3678 * below.
3680 switch (bytes_to_copy)
3682 case 1:
3683 for (;;)
3685 *dp = *sp;
3687 if (row_width <= bytes_to_jump)
3688 return;
3690 dp += bytes_to_jump;
3691 sp += bytes_to_jump;
3692 row_width -= bytes_to_jump;
3695 case 2:
3696 /* There is a possibility of a partial copy at the end here; this
3697 * slows the code down somewhat.
3701 dp[0] = sp[0]; dp[1] = sp[1];
3703 if (row_width <= bytes_to_jump)
3704 return;
3706 sp += bytes_to_jump;
3707 dp += bytes_to_jump;
3708 row_width -= bytes_to_jump;
3710 while (row_width > 1);
3712 /* And there can only be one byte left at this point: */
3713 *dp = *sp;
3714 return;
3716 case 3:
3717 /* This can only be the RGB case, so each copy is exactly one
3718 * pixel and it is not necessary to check for a partial copy.
3720 for (;;)
3722 dp[0] = sp[0]; dp[1] = sp[1]; dp[2] = sp[2];
3724 if (row_width <= bytes_to_jump)
3725 return;
3727 sp += bytes_to_jump;
3728 dp += bytes_to_jump;
3729 row_width -= bytes_to_jump;
3732 default:
3733 #if PNG_ALIGN_TYPE != PNG_ALIGN_NONE
3734 /* Check for double byte alignment and, if possible, use a
3735 * 16-bit copy. Don't attempt this for narrow images - ones that
3736 * are less than an interlace panel wide. Don't attempt it for
3737 * wide bytes_to_copy either - use the memcpy there.
3739 if (bytes_to_copy < 16 /*else use memcpy*/ &&
3740 png_isaligned(dp, png_uint_16) &&
3741 png_isaligned(sp, png_uint_16) &&
3742 bytes_to_copy % (sizeof (png_uint_16)) == 0 &&
3743 bytes_to_jump % (sizeof (png_uint_16)) == 0)
3745 /* Everything is aligned for png_uint_16 copies, but try for
3746 * png_uint_32 first.
3748 if (png_isaligned(dp, png_uint_32) &&
3749 png_isaligned(sp, png_uint_32) &&
3750 bytes_to_copy % (sizeof (png_uint_32)) == 0 &&
3751 bytes_to_jump % (sizeof (png_uint_32)) == 0)
3753 png_uint_32p dp32 = png_aligncast(png_uint_32p,dp);
3754 png_const_uint_32p sp32 = png_aligncastconst(
3755 png_const_uint_32p, sp);
3756 size_t skip = (bytes_to_jump-bytes_to_copy) /
3757 (sizeof (png_uint_32));
3761 size_t c = bytes_to_copy;
3764 *dp32++ = *sp32++;
3765 c -= (sizeof (png_uint_32));
3767 while (c > 0);
3769 if (row_width <= bytes_to_jump)
3770 return;
3772 dp32 += skip;
3773 sp32 += skip;
3774 row_width -= bytes_to_jump;
3776 while (bytes_to_copy <= row_width);
3778 /* Get to here when the row_width truncates the final copy.
3779 * There will be 1-3 bytes left to copy, so don't try the
3780 * 16-bit loop below.
3782 dp = (png_bytep)dp32;
3783 sp = (png_const_bytep)sp32;
3785 *dp++ = *sp++;
3786 while (--row_width > 0);
3787 return;
3790 /* Else do it in 16-bit quantities, but only if the size is
3791 * not too large.
3793 else
3795 png_uint_16p dp16 = png_aligncast(png_uint_16p, dp);
3796 png_const_uint_16p sp16 = png_aligncastconst(
3797 png_const_uint_16p, sp);
3798 size_t skip = (bytes_to_jump-bytes_to_copy) /
3799 (sizeof (png_uint_16));
3803 size_t c = bytes_to_copy;
3806 *dp16++ = *sp16++;
3807 c -= (sizeof (png_uint_16));
3809 while (c > 0);
3811 if (row_width <= bytes_to_jump)
3812 return;
3814 dp16 += skip;
3815 sp16 += skip;
3816 row_width -= bytes_to_jump;
3818 while (bytes_to_copy <= row_width);
3820 /* End of row - 1 byte left, bytes_to_copy > row_width: */
3821 dp = (png_bytep)dp16;
3822 sp = (png_const_bytep)sp16;
3824 *dp++ = *sp++;
3825 while (--row_width > 0);
3826 return;
3829 #endif /* ALIGN_TYPE code */
3831 /* The true default - use a memcpy: */
3832 for (;;)
3834 memcpy(dp, sp, bytes_to_copy);
3836 if (row_width <= bytes_to_jump)
3837 return;
3839 sp += bytes_to_jump;
3840 dp += bytes_to_jump;
3841 row_width -= bytes_to_jump;
3842 if (bytes_to_copy > row_width)
3843 bytes_to_copy = (unsigned int)/*SAFE*/row_width;
3847 /* NOT REACHED*/
3848 } /* pixel_depth >= 8 */
3850 /* Here if pixel_depth < 8 to check 'end_ptr' below. */
3852 else
3853 #endif /* READ_INTERLACING */
3855 /* If here then the switch above wasn't used so just memcpy the whole row
3856 * from the temporary row buffer (notice that this overwrites the end of the
3857 * destination row if it is a partial byte.)
3859 memcpy(dp, sp, PNG_ROWBYTES(pixel_depth, row_width));
3861 /* Restore the overwritten bits from the last byte if necessary. */
3862 if (end_ptr != NULL)
3863 *end_ptr = (png_byte)((end_byte & end_mask) | (*end_ptr & ~end_mask));
3866 #ifdef PNG_READ_INTERLACING_SUPPORTED
3867 void /* PRIVATE */
3868 png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
3869 png_uint_32 transformations /* Because these may affect the byte layout */)
3871 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
3872 /* Offset to next interlace block */
3873 static const unsigned int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
3875 png_debug(1, "in png_do_read_interlace");
3876 if (row != NULL && row_info != NULL)
3878 png_uint_32 final_width;
3880 final_width = row_info->width * png_pass_inc[pass];
3882 switch (row_info->pixel_depth)
3884 case 1:
3886 png_bytep sp = row + (size_t)((row_info->width - 1) >> 3);
3887 png_bytep dp = row + (size_t)((final_width - 1) >> 3);
3888 unsigned int sshift, dshift;
3889 unsigned int s_start, s_end;
3890 int s_inc;
3891 int jstop = (int)png_pass_inc[pass];
3892 png_byte v;
3893 png_uint_32 i;
3894 int j;
3896 #ifdef PNG_READ_PACKSWAP_SUPPORTED
3897 if ((transformations & PNG_PACKSWAP) != 0)
3899 sshift = ((row_info->width + 7) & 0x07);
3900 dshift = ((final_width + 7) & 0x07);
3901 s_start = 7;
3902 s_end = 0;
3903 s_inc = -1;
3906 else
3907 #endif
3909 sshift = 7 - ((row_info->width + 7) & 0x07);
3910 dshift = 7 - ((final_width + 7) & 0x07);
3911 s_start = 0;
3912 s_end = 7;
3913 s_inc = 1;
3916 for (i = 0; i < row_info->width; i++)
3918 v = (png_byte)((*sp >> sshift) & 0x01);
3919 for (j = 0; j < jstop; j++)
3921 unsigned int tmp = *dp & (0x7f7f >> (7 - dshift));
3922 tmp |= (unsigned int)(v << dshift);
3923 *dp = (png_byte)(tmp & 0xff);
3925 if (dshift == s_end)
3927 dshift = s_start;
3928 dp--;
3931 else
3932 dshift = (unsigned int)((int)dshift + s_inc);
3935 if (sshift == s_end)
3937 sshift = s_start;
3938 sp--;
3941 else
3942 sshift = (unsigned int)((int)sshift + s_inc);
3944 break;
3947 case 2:
3949 png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);
3950 png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);
3951 unsigned int sshift, dshift;
3952 unsigned int s_start, s_end;
3953 int s_inc;
3954 int jstop = (int)png_pass_inc[pass];
3955 png_uint_32 i;
3957 #ifdef PNG_READ_PACKSWAP_SUPPORTED
3958 if ((transformations & PNG_PACKSWAP) != 0)
3960 sshift = (((row_info->width + 3) & 0x03) << 1);
3961 dshift = (((final_width + 3) & 0x03) << 1);
3962 s_start = 6;
3963 s_end = 0;
3964 s_inc = -2;
3967 else
3968 #endif
3970 sshift = ((3 - ((row_info->width + 3) & 0x03)) << 1);
3971 dshift = ((3 - ((final_width + 3) & 0x03)) << 1);
3972 s_start = 0;
3973 s_end = 6;
3974 s_inc = 2;
3977 for (i = 0; i < row_info->width; i++)
3979 png_byte v;
3980 int j;
3982 v = (png_byte)((*sp >> sshift) & 0x03);
3983 for (j = 0; j < jstop; j++)
3985 unsigned int tmp = *dp & (0x3f3f >> (6 - dshift));
3986 tmp |= (unsigned int)(v << dshift);
3987 *dp = (png_byte)(tmp & 0xff);
3989 if (dshift == s_end)
3991 dshift = s_start;
3992 dp--;
3995 else
3996 dshift = (unsigned int)((int)dshift + s_inc);
3999 if (sshift == s_end)
4001 sshift = s_start;
4002 sp--;
4005 else
4006 sshift = (unsigned int)((int)sshift + s_inc);
4008 break;
4011 case 4:
4013 png_bytep sp = row + (size_t)((row_info->width - 1) >> 1);
4014 png_bytep dp = row + (size_t)((final_width - 1) >> 1);
4015 unsigned int sshift, dshift;
4016 unsigned int s_start, s_end;
4017 int s_inc;
4018 png_uint_32 i;
4019 int jstop = (int)png_pass_inc[pass];
4021 #ifdef PNG_READ_PACKSWAP_SUPPORTED
4022 if ((transformations & PNG_PACKSWAP) != 0)
4024 sshift = (((row_info->width + 1) & 0x01) << 2);
4025 dshift = (((final_width + 1) & 0x01) << 2);
4026 s_start = 4;
4027 s_end = 0;
4028 s_inc = -4;
4031 else
4032 #endif
4034 sshift = ((1 - ((row_info->width + 1) & 0x01)) << 2);
4035 dshift = ((1 - ((final_width + 1) & 0x01)) << 2);
4036 s_start = 0;
4037 s_end = 4;
4038 s_inc = 4;
4041 for (i = 0; i < row_info->width; i++)
4043 png_byte v = (png_byte)((*sp >> sshift) & 0x0f);
4044 int j;
4046 for (j = 0; j < jstop; j++)
4048 unsigned int tmp = *dp & (0xf0f >> (4 - dshift));
4049 tmp |= (unsigned int)(v << dshift);
4050 *dp = (png_byte)(tmp & 0xff);
4052 if (dshift == s_end)
4054 dshift = s_start;
4055 dp--;
4058 else
4059 dshift = (unsigned int)((int)dshift + s_inc);
4062 if (sshift == s_end)
4064 sshift = s_start;
4065 sp--;
4068 else
4069 sshift = (unsigned int)((int)sshift + s_inc);
4071 break;
4074 default:
4076 size_t pixel_bytes = (row_info->pixel_depth >> 3);
4078 png_bytep sp = row + (size_t)(row_info->width - 1)
4079 * pixel_bytes;
4081 png_bytep dp = row + (size_t)(final_width - 1) * pixel_bytes;
4083 int jstop = (int)png_pass_inc[pass];
4084 png_uint_32 i;
4086 for (i = 0; i < row_info->width; i++)
4088 png_byte v[8]; /* SAFE; pixel_depth does not exceed 64 */
4089 int j;
4091 memcpy(v, sp, pixel_bytes);
4093 for (j = 0; j < jstop; j++)
4095 memcpy(dp, v, pixel_bytes);
4096 dp -= pixel_bytes;
4099 sp -= pixel_bytes;
4101 break;
4105 row_info->width = final_width;
4106 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width);
4108 #ifndef PNG_READ_PACKSWAP_SUPPORTED
4109 PNG_UNUSED(transformations) /* Silence compiler warning */
4110 #endif
4112 #endif /* READ_INTERLACING */
4114 static void
4115 png_read_filter_row_sub(png_row_infop row_info, png_bytep row,
4116 png_const_bytep prev_row)
4118 size_t i;
4119 size_t istop = row_info->rowbytes;
4120 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
4121 png_bytep rp = row + bpp;
4123 PNG_UNUSED(prev_row)
4125 for (i = bpp; i < istop; i++)
4127 *rp = (png_byte)(((int)(*rp) + (int)(*(rp-bpp))) & 0xff);
4128 rp++;
4132 static void
4133 png_read_filter_row_up(png_row_infop row_info, png_bytep row,
4134 png_const_bytep prev_row)
4136 size_t i;
4137 size_t istop = row_info->rowbytes;
4138 png_bytep rp = row;
4139 png_const_bytep pp = prev_row;
4141 for (i = 0; i < istop; i++)
4143 *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
4144 rp++;
4148 static void
4149 png_read_filter_row_avg(png_row_infop row_info, png_bytep row,
4150 png_const_bytep prev_row)
4152 size_t i;
4153 png_bytep rp = row;
4154 png_const_bytep pp = prev_row;
4155 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
4156 size_t istop = row_info->rowbytes - bpp;
4158 for (i = 0; i < bpp; i++)
4160 *rp = (png_byte)(((int)(*rp) +
4161 ((int)(*pp++) / 2 )) & 0xff);
4163 rp++;
4166 for (i = 0; i < istop; i++)
4168 *rp = (png_byte)(((int)(*rp) +
4169 (int)(*pp++ + *(rp-bpp)) / 2 ) & 0xff);
4171 rp++;
4175 static void
4176 png_read_filter_row_paeth_1byte_pixel(png_row_infop row_info, png_bytep row,
4177 png_const_bytep prev_row)
4179 png_bytep rp_end = row + row_info->rowbytes;
4180 int a, c;
4182 /* First pixel/byte */
4183 c = *prev_row++;
4184 a = *row + c;
4185 *row++ = (png_byte)a;
4187 /* Remainder */
4188 while (row < rp_end)
4190 int b, pa, pb, pc, p;
4192 a &= 0xff; /* From previous iteration or start */
4193 b = *prev_row++;
4195 p = b - c;
4196 pc = a - c;
4198 #ifdef PNG_USE_ABS
4199 pa = abs(p);
4200 pb = abs(pc);
4201 pc = abs(p + pc);
4202 #else
4203 pa = p < 0 ? -p : p;
4204 pb = pc < 0 ? -pc : pc;
4205 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
4206 #endif
4208 /* Find the best predictor, the least of pa, pb, pc favoring the earlier
4209 * ones in the case of a tie.
4211 if (pb < pa)
4213 pa = pb; a = b;
4215 if (pc < pa) a = c;
4217 /* Calculate the current pixel in a, and move the previous row pixel to c
4218 * for the next time round the loop
4220 c = b;
4221 a += *row;
4222 *row++ = (png_byte)a;
4226 static void
4227 png_read_filter_row_paeth_multibyte_pixel(png_row_infop row_info, png_bytep row,
4228 png_const_bytep prev_row)
4230 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
4231 png_bytep rp_end = row + bpp;
4233 /* Process the first pixel in the row completely (this is the same as 'up'
4234 * because there is only one candidate predictor for the first row).
4236 while (row < rp_end)
4238 int a = *row + *prev_row++;
4239 *row++ = (png_byte)a;
4242 /* Remainder */
4243 rp_end = rp_end + (row_info->rowbytes - bpp);
4245 while (row < rp_end)
4247 int a, b, c, pa, pb, pc, p;
4249 c = *(prev_row - bpp);
4250 a = *(row - bpp);
4251 b = *prev_row++;
4253 p = b - c;
4254 pc = a - c;
4256 #ifdef PNG_USE_ABS
4257 pa = abs(p);
4258 pb = abs(pc);
4259 pc = abs(p + pc);
4260 #else
4261 pa = p < 0 ? -p : p;
4262 pb = pc < 0 ? -pc : pc;
4263 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
4264 #endif
4266 if (pb < pa)
4268 pa = pb; a = b;
4270 if (pc < pa) a = c;
4272 a += *row;
4273 *row++ = (png_byte)a;
4277 static void
4278 png_init_filter_functions(png_structrp pp)
4279 /* This function is called once for every PNG image (except for PNG images
4280 * that only use PNG_FILTER_VALUE_NONE for all rows) to set the
4281 * implementations required to reverse the filtering of PNG rows. Reversing
4282 * the filter is the first transformation performed on the row data. It is
4283 * performed in place, therefore an implementation can be selected based on
4284 * the image pixel format. If the implementation depends on image width then
4285 * take care to ensure that it works correctly if the image is interlaced -
4286 * interlacing causes the actual row width to vary.
4289 unsigned int bpp = (pp->pixel_depth + 7) >> 3;
4291 pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub;
4292 pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up;
4293 pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg;
4294 if (bpp == 1)
4295 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
4296 png_read_filter_row_paeth_1byte_pixel;
4297 else
4298 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
4299 png_read_filter_row_paeth_multibyte_pixel;
4301 #ifdef PNG_FILTER_OPTIMIZATIONS
4302 /* To use this define PNG_FILTER_OPTIMIZATIONS as the name of a function to
4303 * call to install hardware optimizations for the above functions; simply
4304 * replace whatever elements of the pp->read_filter[] array with a hardware
4305 * specific (or, for that matter, generic) optimization.
4307 * To see an example of this examine what configure.ac does when
4308 * --enable-arm-neon is specified on the command line.
4310 PNG_FILTER_OPTIMIZATIONS(pp, bpp);
4311 #endif
4314 void /* PRIVATE */
4315 png_read_filter_row(png_structrp pp, png_row_infop row_info, png_bytep row,
4316 png_const_bytep prev_row, int filter)
4318 /* OPTIMIZATION: DO NOT MODIFY THIS FUNCTION, instead #define
4319 * PNG_FILTER_OPTIMIZATIONS to a function that overrides the generic
4320 * implementations. See png_init_filter_functions above.
4322 if (filter > PNG_FILTER_VALUE_NONE && filter < PNG_FILTER_VALUE_LAST)
4324 if (pp->read_filter[0] == NULL)
4325 png_init_filter_functions(pp);
4327 pp->read_filter[filter-1](row_info, row, prev_row);
4331 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
4332 void /* PRIVATE */
4333 png_read_IDAT_data(png_structrp png_ptr, png_bytep output,
4334 png_alloc_size_t avail_out)
4336 /* Loop reading IDATs and decompressing the result into output[avail_out] */
4337 png_ptr->zstream.next_out = output;
4338 png_ptr->zstream.avail_out = 0; /* safety: set below */
4340 if (output == NULL)
4341 avail_out = 0;
4345 int ret;
4346 png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];
4348 if (png_ptr->zstream.avail_in == 0)
4350 uInt avail_in;
4351 png_bytep buffer;
4353 #ifdef PNG_READ_APNG_SUPPORTED
4354 png_uint_32 bytes_to_skip = 0;
4356 while (png_ptr->idat_size == 0 || bytes_to_skip != 0)
4358 png_crc_finish(png_ptr, bytes_to_skip);
4359 bytes_to_skip = 0;
4361 png_ptr->idat_size = png_read_chunk_header(png_ptr);
4362 if (png_ptr->num_frames_read == 0)
4364 if (png_ptr->chunk_name != png_IDAT)
4365 png_error(png_ptr, "Not enough image data");
4367 else
4369 if (png_ptr->chunk_name == png_IEND)
4370 png_error(png_ptr, "Not enough image data");
4371 if (png_ptr->chunk_name != png_fdAT)
4373 png_warning(png_ptr, "Skipped (ignored) a chunk "
4374 "between APNG chunks");
4375 bytes_to_skip = png_ptr->idat_size;
4376 continue;
4379 png_ensure_sequence_number(png_ptr, png_ptr->idat_size);
4381 png_ptr->idat_size -= 4;
4384 #else
4385 while (png_ptr->idat_size == 0)
4387 png_crc_finish(png_ptr, 0);
4389 png_ptr->idat_size = png_read_chunk_header(png_ptr);
4390 /* This is an error even in the 'check' case because the code just
4391 * consumed a non-IDAT header.
4393 if (png_ptr->chunk_name != png_IDAT)
4394 png_error(png_ptr, "Not enough image data");
4396 #endif /* READ_APNG */
4398 avail_in = png_ptr->IDAT_read_size;
4400 if (avail_in > png_ptr->idat_size)
4401 avail_in = (uInt)png_ptr->idat_size;
4403 /* A PNG with a gradually increasing IDAT size will defeat this attempt
4404 * to minimize memory usage by causing lots of re-allocs, but
4405 * realistically doing IDAT_read_size re-allocs is not likely to be a
4406 * big problem.
4408 buffer = png_read_buffer(png_ptr, avail_in, 0/*error*/);
4410 png_crc_read(png_ptr, buffer, avail_in);
4411 png_ptr->idat_size -= avail_in;
4413 png_ptr->zstream.next_in = buffer;
4414 png_ptr->zstream.avail_in = avail_in;
4417 /* And set up the output side. */
4418 if (output != NULL) /* standard read */
4420 uInt out = ZLIB_IO_MAX;
4422 if (out > avail_out)
4423 out = (uInt)avail_out;
4425 avail_out -= out;
4426 png_ptr->zstream.avail_out = out;
4429 else /* after last row, checking for end */
4431 png_ptr->zstream.next_out = tmpbuf;
4432 png_ptr->zstream.avail_out = (sizeof tmpbuf);
4435 /* Use NO_FLUSH; this gives zlib the maximum opportunity to optimize the
4436 * process. If the LZ stream is truncated the sequential reader will
4437 * terminally damage the stream, above, by reading the chunk header of the
4438 * following chunk (it then exits with png_error).
4440 * TODO: deal more elegantly with truncated IDAT lists.
4442 ret = PNG_INFLATE(png_ptr, Z_NO_FLUSH);
4444 /* Take the unconsumed output back. */
4445 if (output != NULL)
4446 avail_out += png_ptr->zstream.avail_out;
4448 else /* avail_out counts the extra bytes */
4449 avail_out += (sizeof tmpbuf) - png_ptr->zstream.avail_out;
4451 png_ptr->zstream.avail_out = 0;
4453 if (ret == Z_STREAM_END)
4455 /* Do this for safety; we won't read any more into this row. */
4456 png_ptr->zstream.next_out = NULL;
4458 png_ptr->mode |= PNG_AFTER_IDAT;
4459 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
4460 #ifdef PNG_READ_APNG_SUPPORTED
4461 png_ptr->num_frames_read++;
4462 #endif
4464 if (png_ptr->zstream.avail_in > 0 || png_ptr->idat_size > 0)
4465 png_chunk_benign_error(png_ptr, "Extra compressed data");
4466 break;
4469 if (ret != Z_OK)
4471 png_zstream_error(png_ptr, ret);
4473 if (output != NULL)
4474 png_chunk_error(png_ptr, png_ptr->zstream.msg);
4476 else /* checking */
4478 png_chunk_benign_error(png_ptr, png_ptr->zstream.msg);
4479 return;
4482 } while (avail_out > 0);
4484 if (avail_out > 0)
4486 /* The stream ended before the image; this is the same as too few IDATs so
4487 * should be handled the same way.
4489 if (output != NULL)
4490 png_error(png_ptr, "Not enough image data");
4492 else /* the deflate stream contained extra data */
4493 png_chunk_benign_error(png_ptr, "Too much image data");
4497 void /* PRIVATE */
4498 png_read_finish_IDAT(png_structrp png_ptr)
4500 /* We don't need any more data and the stream should have ended, however the
4501 * LZ end code may actually not have been processed. In this case we must
4502 * read it otherwise stray unread IDAT data or, more likely, an IDAT chunk
4503 * may still remain to be consumed.
4505 if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
4507 /* The NULL causes png_read_IDAT_data to swallow any remaining bytes in
4508 * the compressed stream, but the stream may be damaged too, so even after
4509 * this call we may need to terminate the zstream ownership.
4511 png_read_IDAT_data(png_ptr, NULL, 0);
4512 png_ptr->zstream.next_out = NULL; /* safety */
4514 /* Now clear everything out for safety; the following may not have been
4515 * done.
4517 if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
4519 png_ptr->mode |= PNG_AFTER_IDAT;
4520 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
4524 /* If the zstream has not been released do it now *and* terminate the reading
4525 * of the final IDAT chunk.
4527 if (png_ptr->zowner == png_IDAT)
4529 /* Always do this; the pointers otherwise point into the read buffer. */
4530 png_ptr->zstream.next_in = NULL;
4531 png_ptr->zstream.avail_in = 0;
4533 /* Now we no longer own the zstream. */
4534 png_ptr->zowner = 0;
4536 /* The slightly weird semantics of the sequential IDAT reading is that we
4537 * are always in or at the end of an IDAT chunk, so we always need to do a
4538 * crc_finish here. If idat_size is non-zero we also need to read the
4539 * spurious bytes at the end of the chunk now.
4541 (void)png_crc_finish(png_ptr, png_ptr->idat_size);
4545 void /* PRIVATE */
4546 png_read_finish_row(png_structrp png_ptr)
4548 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
4550 /* Start of interlace block */
4551 static const png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
4553 /* Offset to next interlace block */
4554 static const png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
4556 /* Start of interlace block in the y direction */
4557 static const png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
4559 /* Offset to next interlace block in the y direction */
4560 static const png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
4562 png_debug(1, "in png_read_finish_row");
4563 png_ptr->row_number++;
4564 if (png_ptr->row_number < png_ptr->num_rows)
4565 return;
4567 if (png_ptr->interlaced != 0)
4569 png_ptr->row_number = 0;
4571 /* TO DO: don't do this if prev_row isn't needed (requires
4572 * read-ahead of the next row's filter byte.
4574 memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
4578 png_ptr->pass++;
4580 if (png_ptr->pass >= 7)
4581 break;
4583 png_ptr->iwidth = (png_ptr->width +
4584 png_pass_inc[png_ptr->pass] - 1 -
4585 png_pass_start[png_ptr->pass]) /
4586 png_pass_inc[png_ptr->pass];
4588 if ((png_ptr->transformations & PNG_INTERLACE) == 0)
4590 png_ptr->num_rows = (png_ptr->height +
4591 png_pass_yinc[png_ptr->pass] - 1 -
4592 png_pass_ystart[png_ptr->pass]) /
4593 png_pass_yinc[png_ptr->pass];
4596 else /* if (png_ptr->transformations & PNG_INTERLACE) */
4597 break; /* libpng deinterlacing sees every row */
4599 } while (png_ptr->num_rows == 0 || png_ptr->iwidth == 0);
4601 if (png_ptr->pass < 7)
4602 return;
4605 /* Here after at the end of the last row of the last pass. */
4606 png_read_finish_IDAT(png_ptr);
4608 #endif /* SEQUENTIAL_READ */
4610 void /* PRIVATE */
4611 png_read_start_row(png_structrp png_ptr)
4613 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
4615 /* Start of interlace block */
4616 static const png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
4618 /* Offset to next interlace block */
4619 static const png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
4621 /* Start of interlace block in the y direction */
4622 static const png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
4624 /* Offset to next interlace block in the y direction */
4625 static const png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
4627 unsigned int max_pixel_depth;
4628 size_t row_bytes;
4630 png_debug(1, "in png_read_start_row");
4632 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
4633 png_init_read_transformations(png_ptr);
4634 #endif
4635 if (png_ptr->interlaced != 0)
4637 if ((png_ptr->transformations & PNG_INTERLACE) == 0)
4638 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
4639 png_pass_ystart[0]) / png_pass_yinc[0];
4641 else
4642 png_ptr->num_rows = png_ptr->height;
4644 png_ptr->iwidth = (png_ptr->width +
4645 png_pass_inc[png_ptr->pass] - 1 -
4646 png_pass_start[png_ptr->pass]) /
4647 png_pass_inc[png_ptr->pass];
4650 else
4652 png_ptr->num_rows = png_ptr->height;
4653 png_ptr->iwidth = png_ptr->width;
4656 max_pixel_depth = (unsigned int)png_ptr->pixel_depth;
4658 /* WARNING: * png_read_transform_info (pngrtran.c) performs a simpler set of
4659 * calculations to calculate the final pixel depth, then
4660 * png_do_read_transforms actually does the transforms. This means that the
4661 * code which effectively calculates this value is actually repeated in three
4662 * separate places. They must all match. Innocent changes to the order of
4663 * transformations can and will break libpng in a way that causes memory
4664 * overwrites.
4666 * TODO: fix this.
4668 #ifdef PNG_READ_PACK_SUPPORTED
4669 if ((png_ptr->transformations & PNG_PACK) != 0 && png_ptr->bit_depth < 8)
4670 max_pixel_depth = 8;
4671 #endif
4673 #ifdef PNG_READ_EXPAND_SUPPORTED
4674 if ((png_ptr->transformations & PNG_EXPAND) != 0)
4676 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
4678 if (png_ptr->num_trans != 0)
4679 max_pixel_depth = 32;
4681 else
4682 max_pixel_depth = 24;
4685 else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
4687 if (max_pixel_depth < 8)
4688 max_pixel_depth = 8;
4690 if (png_ptr->num_trans != 0)
4691 max_pixel_depth *= 2;
4694 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
4696 if (png_ptr->num_trans != 0)
4698 max_pixel_depth *= 4;
4699 max_pixel_depth /= 3;
4703 #endif
4705 #ifdef PNG_READ_EXPAND_16_SUPPORTED
4706 if ((png_ptr->transformations & PNG_EXPAND_16) != 0)
4708 # ifdef PNG_READ_EXPAND_SUPPORTED
4709 /* In fact it is an error if it isn't supported, but checking is
4710 * the safe way.
4712 if ((png_ptr->transformations & PNG_EXPAND) != 0)
4714 if (png_ptr->bit_depth < 16)
4715 max_pixel_depth *= 2;
4717 else
4718 # endif
4719 png_ptr->transformations &= ~PNG_EXPAND_16;
4721 #endif
4723 #ifdef PNG_READ_FILLER_SUPPORTED
4724 if ((png_ptr->transformations & (PNG_FILLER)) != 0)
4726 if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
4728 if (max_pixel_depth <= 8)
4729 max_pixel_depth = 16;
4731 else
4732 max_pixel_depth = 32;
4735 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB ||
4736 png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
4738 if (max_pixel_depth <= 32)
4739 max_pixel_depth = 32;
4741 else
4742 max_pixel_depth = 64;
4745 #endif
4747 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
4748 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0)
4750 if (
4751 #ifdef PNG_READ_EXPAND_SUPPORTED
4752 (png_ptr->num_trans != 0 &&
4753 (png_ptr->transformations & PNG_EXPAND) != 0) ||
4754 #endif
4755 #ifdef PNG_READ_FILLER_SUPPORTED
4756 (png_ptr->transformations & (PNG_FILLER)) != 0 ||
4757 #endif
4758 png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
4760 if (max_pixel_depth <= 16)
4761 max_pixel_depth = 32;
4763 else
4764 max_pixel_depth = 64;
4767 else
4769 if (max_pixel_depth <= 8)
4771 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4772 max_pixel_depth = 32;
4774 else
4775 max_pixel_depth = 24;
4778 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4779 max_pixel_depth = 64;
4781 else
4782 max_pixel_depth = 48;
4785 #endif
4787 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
4788 defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
4789 if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0)
4791 unsigned int user_pixel_depth = png_ptr->user_transform_depth *
4792 png_ptr->user_transform_channels;
4794 if (user_pixel_depth > max_pixel_depth)
4795 max_pixel_depth = user_pixel_depth;
4797 #endif
4799 /* This value is stored in png_struct and double checked in the row read
4800 * code.
4802 png_ptr->maximum_pixel_depth = (png_byte)max_pixel_depth;
4803 png_ptr->transformed_pixel_depth = 0; /* calculated on demand */
4805 /* Align the width on the next larger 8 pixels. Mainly used
4806 * for interlacing
4808 row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
4809 /* Calculate the maximum bytes needed, adding a byte and a pixel
4810 * for safety's sake
4812 row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) +
4813 1 + ((max_pixel_depth + 7) >> 3U);
4815 #ifdef PNG_MAX_MALLOC_64K
4816 if (row_bytes > (png_uint_32)65536L)
4817 png_error(png_ptr, "This image requires a row greater than 64KB");
4818 #endif
4820 if (row_bytes + 48 > png_ptr->old_big_row_buf_size)
4822 png_free(png_ptr, png_ptr->big_row_buf);
4823 png_free(png_ptr, png_ptr->big_prev_row);
4825 if (png_ptr->interlaced != 0)
4826 png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr,
4827 row_bytes + 48);
4829 else
4830 png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
4832 png_ptr->big_prev_row = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
4834 #ifdef PNG_ALIGNED_MEMORY_SUPPORTED
4835 /* Use 16-byte aligned memory for row_buf with at least 16 bytes
4836 * of padding before and after row_buf; treat prev_row similarly.
4837 * NOTE: the alignment is to the start of the pixels, one beyond the start
4838 * of the buffer, because of the filter byte. Prior to libpng 1.5.6 this
4839 * was incorrect; the filter byte was aligned, which had the exact
4840 * opposite effect of that intended.
4843 png_bytep temp = png_ptr->big_row_buf + 32;
4844 size_t extra = (size_t)temp & 0x0f;
4845 png_ptr->row_buf = temp - extra - 1/*filter byte*/;
4847 temp = png_ptr->big_prev_row + 32;
4848 extra = (size_t)temp & 0x0f;
4849 png_ptr->prev_row = temp - extra - 1/*filter byte*/;
4851 #else
4852 /* Use 31 bytes of padding before and 17 bytes after row_buf. */
4853 png_ptr->row_buf = png_ptr->big_row_buf + 31;
4854 png_ptr->prev_row = png_ptr->big_prev_row + 31;
4855 #endif
4856 png_ptr->old_big_row_buf_size = row_bytes + 48;
4859 #ifdef PNG_MAX_MALLOC_64K
4860 if (png_ptr->rowbytes > 65535)
4861 png_error(png_ptr, "This image requires a row greater than 64KB");
4863 #endif
4864 if (png_ptr->rowbytes > (PNG_SIZE_MAX - 1))
4865 png_error(png_ptr, "Row has too many bytes to allocate in memory");
4867 memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
4869 png_debug1(3, "width = %u,", png_ptr->width);
4870 png_debug1(3, "height = %u,", png_ptr->height);
4871 png_debug1(3, "iwidth = %u,", png_ptr->iwidth);
4872 png_debug1(3, "num_rows = %u,", png_ptr->num_rows);
4873 png_debug1(3, "rowbytes = %lu,", (unsigned long)png_ptr->rowbytes);
4874 png_debug1(3, "irowbytes = %lu",
4875 (unsigned long)PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1);
4877 /* The sequential reader needs a buffer for IDAT, but the progressive reader
4878 * does not, so free the read buffer now regardless; the sequential reader
4879 * reallocates it on demand.
4881 if (png_ptr->read_buffer != NULL)
4883 png_bytep buffer = png_ptr->read_buffer;
4885 png_ptr->read_buffer_size = 0;
4886 png_ptr->read_buffer = NULL;
4887 png_free(png_ptr, buffer);
4890 /* Finally claim the zstream for the inflate of the IDAT data, use the bits
4891 * value from the stream (note that this will result in a fatal error if the
4892 * IDAT stream has a bogus deflate header window_bits value, but this should
4893 * not be happening any longer!)
4895 if (png_inflate_claim(png_ptr, png_IDAT) != Z_OK)
4896 png_error(png_ptr, png_ptr->zstream.msg);
4898 png_ptr->flags |= PNG_FLAG_ROW_INIT;
4901 #ifdef PNG_READ_APNG_SUPPORTED
4902 /* This function is to be called after the main IDAT set has been read and
4903 * before a new IDAT is read. It resets some parts of png_ptr
4904 * to make them usable by the read functions again */
4905 void /* PRIVATE */
4906 png_read_reset(png_structp png_ptr)
4908 png_ptr->mode &= ~PNG_HAVE_IDAT;
4909 png_ptr->mode &= ~PNG_AFTER_IDAT;
4910 png_ptr->row_number = 0;
4911 png_ptr->pass = 0;
4914 void /* PRIVATE */
4915 png_read_reinit(png_structp png_ptr, png_infop info_ptr)
4917 png_ptr->width = info_ptr->next_frame_width;
4918 png_ptr->height = info_ptr->next_frame_height;
4919 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth,png_ptr->width);
4920 png_ptr->info_rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth,
4921 png_ptr->width);
4922 if (png_ptr->prev_row != NULL)
4923 memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
4926 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
4927 /* same as png_read_reset() but for the progressive reader */
4928 void /* PRIVATE */
4929 png_progressive_read_reset(png_structp png_ptr)
4931 #ifdef PNG_READ_INTERLACING_SUPPORTED
4932 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
4934 /* Start of interlace block */
4935 static PNG_CONST png_byte png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
4937 /* Offset to next interlace block */
4938 static PNG_CONST png_byte png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
4940 /* Start of interlace block in the y direction */
4941 static PNG_CONST png_byte png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
4943 /* Offset to next interlace block in the y direction */
4944 static PNG_CONST png_byte png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
4946 if (png_ptr->interlaced != 0)
4948 if ((png_ptr->transformations & PNG_INTERLACE) == 0)
4949 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
4950 png_pass_ystart[0]) / png_pass_yinc[0];
4951 else
4952 png_ptr->num_rows = png_ptr->height;
4954 png_ptr->iwidth = (png_ptr->width +
4955 png_pass_inc[png_ptr->pass] - 1 -
4956 png_pass_start[png_ptr->pass]) /
4957 png_pass_inc[png_ptr->pass];
4959 else
4960 #endif /* READ_INTERLACING */
4962 png_ptr->num_rows = png_ptr->height;
4963 png_ptr->iwidth = png_ptr->width;
4965 png_ptr->flags &= ~PNG_FLAG_ZSTREAM_ENDED;
4966 if (inflateReset(&(png_ptr->zstream)) != Z_OK)
4967 png_error(png_ptr, "inflateReset failed");
4968 png_ptr->zstream.avail_in = 0;
4969 png_ptr->zstream.next_in = 0;
4970 png_ptr->zstream.next_out = png_ptr->row_buf;
4971 png_ptr->zstream.avail_out = (uInt)PNG_ROWBYTES(png_ptr->pixel_depth,
4972 png_ptr->iwidth) + 1;
4974 #endif /* PROGRESSIVE_READ */
4975 #endif /* READ_APNG */
4976 #endif /* READ */