2 /* pngrutil.c - utilities to read a PNG file
4 * Last changed in libpng 1.6.29 [March 16, 2017]
5 * Copyright (c) 1998-2002,2004,2006-2017 Glenn Randers-Pehrson
6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 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.
19 #ifdef PNG_READ_SUPPORTED
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");
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. */
50 png_warning(png_ptr
, "PNG fixed point integer out of range");
52 return PNG_FIXED_ERROR
;
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. */
68 png_get_uint_32
)(png_const_bytep buf
)
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)) ) ;
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.
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.
101 /* Grab an unsigned 16-bit integer from a buffer in big-endian format. */
103 png_get_uint_16
)(png_const_bytep buf
)
105 /* ANSI-C requires an int value to accomodate 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.)
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 */
121 png_read_sig(png_structrp png_ptr
, png_inforp info_ptr
)
123 png_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)
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
;
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");
146 png_error(png_ptr
, "PNG file corrupted by ASCII conversion");
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
)
161 #ifdef PNG_IO_STATE_SUPPORTED
162 png_ptr
->io_state
= PNG_IO_READING
| PNG_IO_CHUNK_HDR
;
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 #ifdef PNG_IO_STATE_SUPPORTED
185 png_ptr
->io_state
= PNG_IO_READING
| PNG_IO_CHUNK_DATA
;
191 /* Read data, and (optionally) run it through the CRC. */
193 png_crc_read(png_structrp png_ptr
, png_bytep buf
, png_uint_32 length
)
198 png_read_data(png_ptr
, buf
, length
);
199 png_calculate_crc(png_ptr
, buf
, length
);
202 /* Optionally skip data and then check the CRC. Depending on whether we
203 * are reading an ancillary or critical chunk, and how the program has set
204 * things up, we may calculate the CRC on the data and print a message.
205 * Returns '1' if there was a CRC error, '0' otherwise.
208 png_crc_finish(png_structrp png_ptr
, png_uint_32 skip
)
210 /* The size of the local buffer for inflate is a good guess as to a
211 * reasonable size to use for buffering reads from the application.
216 png_byte tmpbuf
[PNG_INFLATE_BUF_SIZE
];
218 len
= (sizeof tmpbuf
);
223 png_crc_read(png_ptr
, tmpbuf
, len
);
226 if (png_crc_error(png_ptr
) != 0)
228 if (PNG_CHUNK_ANCILLARY(png_ptr
->chunk_name
) != 0 ?
229 (png_ptr
->flags
& PNG_FLAG_CRC_ANCILLARY_NOWARN
) == 0 :
230 (png_ptr
->flags
& PNG_FLAG_CRC_CRITICAL_USE
) != 0)
232 png_chunk_warning(png_ptr
, "CRC error");
236 png_chunk_error(png_ptr
, "CRC error");
244 /* Compare the CRC stored in the PNG file with that calculated by libpng from
245 * the data it has read thus far.
248 png_crc_error(png_structrp png_ptr
)
250 png_byte crc_bytes
[4];
254 if (PNG_CHUNK_ANCILLARY(png_ptr
->chunk_name
) != 0)
256 if ((png_ptr
->flags
& PNG_FLAG_CRC_ANCILLARY_MASK
) ==
257 (PNG_FLAG_CRC_ANCILLARY_USE
| PNG_FLAG_CRC_ANCILLARY_NOWARN
))
263 if ((png_ptr
->flags
& PNG_FLAG_CRC_CRITICAL_IGNORE
) != 0)
267 #ifdef PNG_IO_STATE_SUPPORTED
268 png_ptr
->io_state
= PNG_IO_READING
| PNG_IO_CHUNK_CRC
;
271 /* The chunk CRC must be serialized in a single I/O call. */
272 png_read_data(png_ptr
, crc_bytes
, 4);
276 crc
= png_get_uint_32(crc_bytes
);
277 return ((int)(crc
!= png_ptr
->crc
));
284 #if defined(PNG_READ_iCCP_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) ||\
285 defined(PNG_READ_pCAL_SUPPORTED) || defined(PNG_READ_sCAL_SUPPORTED) ||\
286 defined(PNG_READ_sPLT_SUPPORTED) || defined(PNG_READ_tEXt_SUPPORTED) ||\
287 defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_SEQUENTIAL_READ_SUPPORTED)
288 /* Manage the read buffer; this simply reallocates the buffer if it is not small
289 * enough (or if it is not allocated). The routine returns a pointer to the
290 * buffer; if an error occurs and 'warn' is set the routine returns NULL, else
291 * it will call png_error (via png_malloc) on failure. (warn == 2 means
295 png_read_buffer(png_structrp png_ptr
, png_alloc_size_t new_size
, int warn
)
297 png_bytep buffer
= png_ptr
->read_buffer
;
299 if (buffer
!= NULL
&& new_size
> png_ptr
->read_buffer_size
)
301 png_ptr
->read_buffer
= NULL
;
302 png_ptr
->read_buffer
= NULL
;
303 png_ptr
->read_buffer_size
= 0;
304 png_free(png_ptr
, buffer
);
310 buffer
= png_voidcast(png_bytep
, png_malloc_base(png_ptr
, new_size
));
314 png_ptr
->read_buffer
= buffer
;
315 png_ptr
->read_buffer_size
= new_size
;
318 else if (warn
< 2) /* else silent */
321 png_chunk_warning(png_ptr
, "insufficient memory to read chunk");
324 png_chunk_error(png_ptr
, "insufficient memory to read chunk");
330 #endif /* READ_iCCP|iTXt|pCAL|sCAL|sPLT|tEXt|zTXt|SEQUENTIAL_READ */
332 /* png_inflate_claim: claim the zstream for some nefarious purpose that involves
333 * decompression. Returns Z_OK on success, else a zlib error code. It checks
334 * the owner but, in final release builds, just issues a warning if some other
335 * chunk apparently owns the stream. Prior to release it does a png_error.
338 png_inflate_claim(png_structrp png_ptr
, png_uint_32 owner
)
340 if (png_ptr
->zowner
!= 0)
344 PNG_STRING_FROM_CHUNK(msg
, png_ptr
->zowner
);
345 /* So the message that results is "<chunk> using zstream"; this is an
346 * internal error, but is very useful for debugging. i18n requirements
349 (void)png_safecat(msg
, (sizeof msg
), 4, " using zstream");
350 #if PNG_RELEASE_BUILD
351 png_chunk_warning(png_ptr
, msg
);
354 png_chunk_error(png_ptr
, msg
);
358 /* Implementation note: unlike 'png_deflate_claim' this internal function
359 * does not take the size of the data as an argument. Some efficiency could
360 * be gained by using this when it is known *if* the zlib stream itself does
361 * not record the number; however, this is an illusion: the original writer
362 * of the PNG may have selected a lower window size, and we really must
363 * follow that because, for systems with with limited capabilities, we
364 * would otherwise reject the application's attempts to use a smaller window
365 * size (zlib doesn't have an interface to say "this or lower"!).
367 * inflateReset2 was added to zlib 1.2.4; before this the window could not be
368 * reset, therefore it is necessary to always allocate the maximum window
369 * size with earlier zlibs just in case later compressed chunks need it.
372 int ret
; /* zlib return code */
373 #if ZLIB_VERNUM >= 0x1240
376 # if defined(PNG_SET_OPTION_SUPPORTED) && defined(PNG_MAXIMUM_INFLATE_WINDOW)
377 if (((png_ptr
->options
>> PNG_MAXIMUM_INFLATE_WINDOW
) & 3) ==
381 png_ptr
->zstream_start
= 0; /* fixed window size */
386 png_ptr
->zstream_start
= 1;
390 #endif /* ZLIB_VERNUM >= 0x1240 */
392 /* Set this for safety, just in case the previous owner left pointers to
393 * memory allocations.
395 png_ptr
->zstream
.next_in
= NULL
;
396 png_ptr
->zstream
.avail_in
= 0;
397 png_ptr
->zstream
.next_out
= NULL
;
398 png_ptr
->zstream
.avail_out
= 0;
400 if ((png_ptr
->flags
& PNG_FLAG_ZSTREAM_INITIALIZED
) != 0)
402 #if ZLIB_VERNUM >= 0x1240
403 ret
= inflateReset2(&png_ptr
->zstream
, window_bits
);
405 ret
= inflateReset(&png_ptr
->zstream
);
411 #if ZLIB_VERNUM >= 0x1240
412 ret
= inflateInit2(&png_ptr
->zstream
, window_bits
);
414 ret
= inflateInit(&png_ptr
->zstream
);
418 png_ptr
->flags
|= PNG_FLAG_ZSTREAM_INITIALIZED
;
421 #if ZLIB_VERNUM >= 0x1290 && \
422 defined(PNG_SET_OPTION_SUPPORTED) && defined(PNG_IGNORE_ADLER32)
423 if (((png_ptr
->options
>> PNG_IGNORE_ADLER32
) & 3) == PNG_OPTION_ON
)
424 /* Turn off validation of the ADLER32 checksum in IDAT chunks */
425 ret
= inflateValidate(&png_ptr
->zstream
, 0);
429 png_ptr
->zowner
= owner
;
432 png_zstream_error(png_ptr
, ret
);
442 #if ZLIB_VERNUM >= 0x1240
443 /* Handle the start of the inflate stream if we called inflateInit2(strm,0);
444 * in this case some zlib versions skip validation of the CINFO field and, in
445 * certain circumstances, libpng may end up displaying an invalid image, in
446 * contrast to implementations that call zlib in the normal way (e.g. libpng
450 png_zlib_inflate(png_structrp png_ptr
, int flush
)
452 if (png_ptr
->zstream_start
&& png_ptr
->zstream
.avail_in
> 0)
454 if ((*png_ptr
->zstream
.next_in
>> 4) > 7)
456 png_ptr
->zstream
.msg
= "invalid window size (libpng)";
460 png_ptr
->zstream_start
= 0;
463 return inflate(&png_ptr
->zstream
, flush
);
465 #endif /* Zlib >= 1.2.4 */
467 #ifdef PNG_READ_COMPRESSED_TEXT_SUPPORTED
468 #if defined(PNG_READ_zTXt_SUPPORTED) || defined (PNG_READ_iTXt_SUPPORTED)
469 /* png_inflate now returns zlib error codes including Z_OK and Z_STREAM_END to
470 * allow the caller to do multiple calls if required. If the 'finish' flag is
471 * set Z_FINISH will be passed to the final inflate() call and Z_STREAM_END must
472 * be returned or there has been a problem, otherwise Z_SYNC_FLUSH is used and
473 * Z_OK or Z_STREAM_END will be returned on success.
475 * The input and output sizes are updated to the actual amounts of data consumed
476 * or written, not the amount available (as in a z_stream). The data pointers
477 * are not changed, so the next input is (data+input_size) and the next
478 * available output is (output+output_size).
481 png_inflate(png_structrp png_ptr
, png_uint_32 owner
, int finish
,
482 /* INPUT: */ png_const_bytep input
, png_uint_32p input_size_ptr
,
483 /* OUTPUT: */ png_bytep output
, png_alloc_size_t
*output_size_ptr
)
485 if (png_ptr
->zowner
== owner
) /* Else not claimed */
488 png_alloc_size_t avail_out
= *output_size_ptr
;
489 png_uint_32 avail_in
= *input_size_ptr
;
491 /* zlib can't necessarily handle more than 65535 bytes at once (i.e. it
492 * can't even necessarily handle 65536 bytes) because the type uInt is
493 * "16 bits or more". Consequently it is necessary to chunk the input to
494 * zlib. This code uses ZLIB_IO_MAX, from pngpriv.h, as the maximum (the
495 * maximum value that can be stored in a uInt.) It is possible to set
496 * ZLIB_IO_MAX to a lower value in pngpriv.h and this may sometimes have
497 * a performance advantage, because it reduces the amount of data accessed
498 * at each step and that may give the OS more time to page it in.
500 png_ptr
->zstream
.next_in
= PNGZ_INPUT_CAST(input
);
501 /* avail_in and avail_out are set below from 'size' */
502 png_ptr
->zstream
.avail_in
= 0;
503 png_ptr
->zstream
.avail_out
= 0;
505 /* Read directly into the output if it is available (this is set to
506 * a local buffer below if output is NULL).
509 png_ptr
->zstream
.next_out
= output
;
514 Byte local_buffer
[PNG_INFLATE_BUF_SIZE
];
516 /* zlib INPUT BUFFER */
517 /* The setting of 'avail_in' used to be outside the loop; by setting it
518 * inside it is possible to chunk the input to zlib and simply rely on
519 * zlib to advance the 'next_in' pointer. This allows arbitrary
520 * amounts of data to be passed through zlib at the unavoidable cost of
521 * requiring a window save (memcpy of up to 32768 output bytes)
522 * every ZLIB_IO_MAX input bytes.
524 avail_in
+= png_ptr
->zstream
.avail_in
; /* not consumed last time */
528 if (avail_in
< avail
)
529 avail
= (uInt
)avail_in
; /* safe: < than ZLIB_IO_MAX */
532 png_ptr
->zstream
.avail_in
= avail
;
534 /* zlib OUTPUT BUFFER */
535 avail_out
+= png_ptr
->zstream
.avail_out
; /* not written last time */
537 avail
= ZLIB_IO_MAX
; /* maximum zlib can process */
541 /* Reset the output buffer each time round if output is NULL and
542 * make available the full buffer, up to 'remaining_space'
544 png_ptr
->zstream
.next_out
= local_buffer
;
545 if ((sizeof local_buffer
) < avail
)
546 avail
= (sizeof local_buffer
);
549 if (avail_out
< avail
)
550 avail
= (uInt
)avail_out
; /* safe: < ZLIB_IO_MAX */
552 png_ptr
->zstream
.avail_out
= avail
;
555 /* zlib inflate call */
556 /* In fact 'avail_out' may be 0 at this point, that happens at the end
557 * of the read when the final LZ end code was not passed at the end of
558 * the previous chunk of input data. Tell zlib if we have reached the
559 * end of the output buffer.
561 ret
= PNG_INFLATE(png_ptr
, avail_out
> 0 ? Z_NO_FLUSH
:
562 (finish
? Z_FINISH
: Z_SYNC_FLUSH
));
563 } while (ret
== Z_OK
);
565 /* For safety kill the local buffer pointer now */
567 png_ptr
->zstream
.next_out
= NULL
;
569 /* Claw back the 'size' and 'remaining_space' byte counts. */
570 avail_in
+= png_ptr
->zstream
.avail_in
;
571 avail_out
+= png_ptr
->zstream
.avail_out
;
573 /* Update the input and output sizes; the updated values are the amount
574 * consumed or written, effectively the inverse of what zlib uses.
577 *output_size_ptr
-= avail_out
;
580 *input_size_ptr
-= avail_in
;
582 /* Ensure png_ptr->zstream.msg is set (even in the success case!) */
583 png_zstream_error(png_ptr
, ret
);
589 /* This is a bad internal error. The recovery assigns to the zstream msg
590 * pointer, which is not owned by the caller, but this is safe; it's only
593 png_ptr
->zstream
.msg
= PNGZ_MSG_CAST("zstream unclaimed");
594 return Z_STREAM_ERROR
;
599 * Decompress trailing data in a chunk. The assumption is that read_buffer
600 * points at an allocated area holding the contents of a chunk with a
601 * trailing compressed part. What we get back is an allocated area
602 * holding the original prefix part and an uncompressed version of the
603 * trailing part (the malloc area passed in is freed).
606 png_decompress_chunk(png_structrp png_ptr
,
607 png_uint_32 chunklength
, png_uint_32 prefix_size
,
608 png_alloc_size_t
*newlength
/* must be initialized to the maximum! */,
609 int terminate
/*add a '\0' to the end of the uncompressed data*/)
611 /* TODO: implement different limits for different types of chunk.
613 * The caller supplies *newlength set to the maximum length of the
614 * uncompressed data, but this routine allocates space for the prefix and
615 * maybe a '\0' terminator too. We have to assume that 'prefix_size' is
616 * limited only by the maximum chunk size.
618 png_alloc_size_t limit
= PNG_SIZE_MAX
;
620 # ifdef PNG_SET_USER_LIMITS_SUPPORTED
621 if (png_ptr
->user_chunk_malloc_max
> 0 &&
622 png_ptr
->user_chunk_malloc_max
< limit
)
623 limit
= png_ptr
->user_chunk_malloc_max
;
624 # elif PNG_USER_CHUNK_MALLOC_MAX > 0
625 if (PNG_USER_CHUNK_MALLOC_MAX
< limit
)
626 limit
= PNG_USER_CHUNK_MALLOC_MAX
;
629 if (limit
>= prefix_size
+ (terminate
!= 0))
633 limit
-= prefix_size
+ (terminate
!= 0);
635 if (limit
< *newlength
)
638 /* Now try to claim the stream. */
639 ret
= png_inflate_claim(png_ptr
, png_ptr
->chunk_name
);
643 png_uint_32 lzsize
= chunklength
- prefix_size
;
645 ret
= png_inflate(png_ptr
, png_ptr
->chunk_name
, 1/*finish*/,
646 /* input: */ png_ptr
->read_buffer
+ prefix_size
, &lzsize
,
647 /* output: */ NULL
, newlength
);
649 if (ret
== Z_STREAM_END
)
651 /* Use 'inflateReset' here, not 'inflateReset2' because this
652 * preserves the previously decided window size (otherwise it would
653 * be necessary to store the previous window size.) In practice
654 * this doesn't matter anyway, because png_inflate will call inflate
655 * with Z_FINISH in almost all cases, so the window will not be
658 if (inflateReset(&png_ptr
->zstream
) == Z_OK
)
660 /* Because of the limit checks above we know that the new,
661 * expanded, size will fit in a size_t (let alone an
662 * png_alloc_size_t). Use png_malloc_base here to avoid an
665 png_alloc_size_t new_size
= *newlength
;
666 png_alloc_size_t buffer_size
= prefix_size
+ new_size
+
668 png_bytep text
= png_voidcast(png_bytep
, png_malloc_base(png_ptr
,
673 ret
= png_inflate(png_ptr
, png_ptr
->chunk_name
, 1/*finish*/,
674 png_ptr
->read_buffer
+ prefix_size
, &lzsize
,
675 text
+ prefix_size
, newlength
);
677 if (ret
== Z_STREAM_END
)
679 if (new_size
== *newlength
)
682 text
[prefix_size
+ *newlength
] = 0;
685 memcpy(text
, png_ptr
->read_buffer
, prefix_size
);
688 png_bytep old_ptr
= png_ptr
->read_buffer
;
690 png_ptr
->read_buffer
= text
;
691 png_ptr
->read_buffer_size
= buffer_size
;
692 text
= old_ptr
; /* freed below */
698 /* The size changed on the second read, there can be no
699 * guarantee that anything is correct at this point.
700 * The 'msg' pointer has been set to "unexpected end of
701 * LZ stream", which is fine, but return an error code
702 * that the caller won't accept.
704 ret
= PNG_UNEXPECTED_ZLIB_RETURN
;
708 else if (ret
== Z_OK
)
709 ret
= PNG_UNEXPECTED_ZLIB_RETURN
; /* for safety */
711 /* Free the text pointer (this is the old read_buffer on
714 png_free(png_ptr
, text
);
716 /* This really is very benign, but it's still an error because
717 * the extra space may otherwise be used as a Trojan Horse.
719 if (ret
== Z_STREAM_END
&&
720 chunklength
- prefix_size
!= lzsize
)
721 png_chunk_benign_error(png_ptr
, "extra compressed data");
726 /* Out of memory allocating the buffer */
728 png_zstream_error(png_ptr
, Z_MEM_ERROR
);
734 /* inflateReset failed, store the error message */
735 png_zstream_error(png_ptr
, ret
);
737 if (ret
== Z_STREAM_END
)
738 ret
= PNG_UNEXPECTED_ZLIB_RETURN
;
742 else if (ret
== Z_OK
)
743 ret
= PNG_UNEXPECTED_ZLIB_RETURN
;
745 /* Release the claimed stream */
749 else /* the claim failed */ if (ret
== Z_STREAM_END
) /* impossible! */
750 ret
= PNG_UNEXPECTED_ZLIB_RETURN
;
757 /* Application/configuration limits exceeded */
758 png_zstream_error(png_ptr
, Z_MEM_ERROR
);
762 #endif /* READ_zTXt || READ_iTXt */
763 #endif /* READ_COMPRESSED_TEXT */
765 #ifdef PNG_READ_iCCP_SUPPORTED
766 /* Perform a partial read and decompress, producing 'avail_out' bytes and
767 * reading from the current chunk as required.
770 png_inflate_read(png_structrp png_ptr
, png_bytep read_buffer
, uInt read_size
,
771 png_uint_32p chunk_bytes
, png_bytep next_out
, png_alloc_size_t
*out_size
,
774 if (png_ptr
->zowner
== png_ptr
->chunk_name
)
778 /* next_in and avail_in must have been initialized by the caller. */
779 png_ptr
->zstream
.next_out
= next_out
;
780 png_ptr
->zstream
.avail_out
= 0; /* set in the loop */
784 if (png_ptr
->zstream
.avail_in
== 0)
786 if (read_size
> *chunk_bytes
)
787 read_size
= (uInt
)*chunk_bytes
;
788 *chunk_bytes
-= read_size
;
791 png_crc_read(png_ptr
, read_buffer
, read_size
);
793 png_ptr
->zstream
.next_in
= read_buffer
;
794 png_ptr
->zstream
.avail_in
= read_size
;
797 if (png_ptr
->zstream
.avail_out
== 0)
799 uInt avail
= ZLIB_IO_MAX
;
800 if (avail
> *out_size
)
801 avail
= (uInt
)*out_size
;
804 png_ptr
->zstream
.avail_out
= avail
;
807 /* Use Z_SYNC_FLUSH when there is no more chunk data to ensure that all
808 * the available output is produced; this allows reading of truncated
811 ret
= PNG_INFLATE(png_ptr
, *chunk_bytes
> 0 ?
812 Z_NO_FLUSH
: (finish
? Z_FINISH
: Z_SYNC_FLUSH
));
814 while (ret
== Z_OK
&& (*out_size
> 0 || png_ptr
->zstream
.avail_out
> 0));
816 *out_size
+= png_ptr
->zstream
.avail_out
;
817 png_ptr
->zstream
.avail_out
= 0; /* Should not be required, but is safe */
819 /* Ensure the error message pointer is always set: */
820 png_zstream_error(png_ptr
, ret
);
826 png_ptr
->zstream
.msg
= PNGZ_MSG_CAST("zstream unclaimed");
827 return Z_STREAM_ERROR
;
830 #endif /* READ_iCCP */
832 /* Read and check the IDHR chunk */
835 png_handle_IHDR(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
838 png_uint_32 width
, height
;
839 int bit_depth
, color_type
, compression_type
, filter_type
;
842 png_debug(1, "in png_handle_IHDR");
844 if ((png_ptr
->mode
& PNG_HAVE_IHDR
) != 0)
845 png_chunk_error(png_ptr
, "out of place");
847 /* Check the length */
849 png_chunk_error(png_ptr
, "invalid");
851 png_ptr
->mode
|= PNG_HAVE_IHDR
;
853 png_crc_read(png_ptr
, buf
, 13);
854 png_crc_finish(png_ptr
, 0);
856 width
= png_get_uint_31(png_ptr
, buf
);
857 height
= png_get_uint_31(png_ptr
, buf
+ 4);
860 compression_type
= buf
[10];
861 filter_type
= buf
[11];
862 interlace_type
= buf
[12];
864 /* Set internal variables */
865 png_ptr
->width
= width
;
866 png_ptr
->height
= height
;
867 png_ptr
->bit_depth
= (png_byte
)bit_depth
;
868 png_ptr
->interlaced
= (png_byte
)interlace_type
;
869 png_ptr
->color_type
= (png_byte
)color_type
;
870 #ifdef PNG_MNG_FEATURES_SUPPORTED
871 png_ptr
->filter_type
= (png_byte
)filter_type
;
873 png_ptr
->compression_type
= (png_byte
)compression_type
;
875 /* Find number of channels */
876 switch (png_ptr
->color_type
)
878 default: /* invalid, png_set_IHDR calls png_error */
879 case PNG_COLOR_TYPE_GRAY
:
880 case PNG_COLOR_TYPE_PALETTE
:
881 png_ptr
->channels
= 1;
884 case PNG_COLOR_TYPE_RGB
:
885 png_ptr
->channels
= 3;
888 case PNG_COLOR_TYPE_GRAY_ALPHA
:
889 png_ptr
->channels
= 2;
892 case PNG_COLOR_TYPE_RGB_ALPHA
:
893 png_ptr
->channels
= 4;
897 /* Set up other useful info */
898 png_ptr
->pixel_depth
= (png_byte
)(png_ptr
->bit_depth
* png_ptr
->channels
);
899 png_ptr
->rowbytes
= PNG_ROWBYTES(png_ptr
->pixel_depth
, png_ptr
->width
);
900 png_debug1(3, "bit_depth = %d", png_ptr
->bit_depth
);
901 png_debug1(3, "channels = %d", png_ptr
->channels
);
902 png_debug1(3, "rowbytes = %lu", (unsigned long)png_ptr
->rowbytes
);
903 png_set_IHDR(png_ptr
, info_ptr
, width
, height
, bit_depth
,
904 color_type
, interlace_type
, compression_type
, filter_type
);
907 /* Read and check the palette */
909 png_handle_PLTE(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
911 png_color palette
[PNG_MAX_PALETTE_LENGTH
];
912 int max_palette_length
, num
, i
;
913 #ifdef PNG_POINTER_INDEXING_SUPPORTED
917 png_debug(1, "in png_handle_PLTE");
919 if ((png_ptr
->mode
& PNG_HAVE_IHDR
) == 0)
920 png_chunk_error(png_ptr
, "missing IHDR");
922 /* Moved to before the 'after IDAT' check below because otherwise duplicate
923 * PLTE chunks are potentially ignored (the spec says there shall not be more
924 * than one PLTE, the error is not treated as benign, so this check trumps
925 * the requirement that PLTE appears before IDAT.)
927 else if ((png_ptr
->mode
& PNG_HAVE_PLTE
) != 0)
928 png_chunk_error(png_ptr
, "duplicate");
930 else if ((png_ptr
->mode
& PNG_HAVE_IDAT
) != 0)
932 /* This is benign because the non-benign error happened before, when an
933 * IDAT was encountered in a color-mapped image with no PLTE.
935 png_crc_finish(png_ptr
, length
);
936 png_chunk_benign_error(png_ptr
, "out of place");
940 png_ptr
->mode
|= PNG_HAVE_PLTE
;
942 if ((png_ptr
->color_type
& PNG_COLOR_MASK_COLOR
) == 0)
944 png_crc_finish(png_ptr
, length
);
945 png_chunk_benign_error(png_ptr
, "ignored in grayscale PNG");
949 #ifndef PNG_READ_OPT_PLTE_SUPPORTED
950 if (png_ptr
->color_type
!= PNG_COLOR_TYPE_PALETTE
)
952 png_crc_finish(png_ptr
, length
);
957 if (length
> 3*PNG_MAX_PALETTE_LENGTH
|| length
% 3)
959 png_crc_finish(png_ptr
, length
);
961 if (png_ptr
->color_type
!= PNG_COLOR_TYPE_PALETTE
)
962 png_chunk_benign_error(png_ptr
, "invalid");
965 png_chunk_error(png_ptr
, "invalid");
970 /* The cast is safe because 'length' is less than 3*PNG_MAX_PALETTE_LENGTH */
971 num
= (int)length
/ 3;
973 /* If the palette has 256 or fewer entries but is too large for the bit
974 * depth, we don't issue an error, to preserve the behavior of previous
975 * libpng versions. We silently truncate the unused extra palette entries
978 if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
979 max_palette_length
= (1 << png_ptr
->bit_depth
);
981 max_palette_length
= PNG_MAX_PALETTE_LENGTH
;
983 if (num
> max_palette_length
)
984 num
= max_palette_length
;
986 #ifdef PNG_POINTER_INDEXING_SUPPORTED
987 for (i
= 0, pal_ptr
= palette
; i
< num
; i
++, pal_ptr
++)
991 png_crc_read(png_ptr
, buf
, 3);
992 pal_ptr
->red
= buf
[0];
993 pal_ptr
->green
= buf
[1];
994 pal_ptr
->blue
= buf
[2];
997 for (i
= 0; i
< num
; i
++)
1001 png_crc_read(png_ptr
, buf
, 3);
1002 /* Don't depend upon png_color being any order */
1003 palette
[i
].red
= buf
[0];
1004 palette
[i
].green
= buf
[1];
1005 palette
[i
].blue
= buf
[2];
1009 /* If we actually need the PLTE chunk (ie for a paletted image), we do
1010 * whatever the normal CRC configuration tells us. However, if we
1011 * have an RGB image, the PLTE can be considered ancillary, so
1012 * we will act as though it is.
1014 #ifndef PNG_READ_OPT_PLTE_SUPPORTED
1015 if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
1018 png_crc_finish(png_ptr
, (png_uint_32
) (length
- (unsigned int)num
* 3));
1021 #ifndef PNG_READ_OPT_PLTE_SUPPORTED
1022 else if (png_crc_error(png_ptr
) != 0) /* Only if we have a CRC error */
1024 /* If we don't want to use the data from an ancillary chunk,
1025 * we have two options: an error abort, or a warning and we
1026 * ignore the data in this chunk (which should be OK, since
1027 * it's considered ancillary for a RGB or RGBA image).
1029 * IMPLEMENTATION NOTE: this is only here because png_crc_finish uses the
1030 * chunk type to determine whether to check the ancillary or the critical
1033 if ((png_ptr
->flags
& PNG_FLAG_CRC_ANCILLARY_USE
) == 0)
1035 if ((png_ptr
->flags
& PNG_FLAG_CRC_ANCILLARY_NOWARN
) != 0)
1039 png_chunk_error(png_ptr
, "CRC error");
1042 /* Otherwise, we (optionally) emit a warning and use the chunk. */
1043 else if ((png_ptr
->flags
& PNG_FLAG_CRC_ANCILLARY_NOWARN
) == 0)
1044 png_chunk_warning(png_ptr
, "CRC error");
1048 /* TODO: png_set_PLTE has the side effect of setting png_ptr->palette to its
1049 * own copy of the palette. This has the side effect that when png_start_row
1050 * is called (this happens after any call to png_read_update_info) the
1051 * info_ptr palette gets changed. This is extremely unexpected and
1054 * Fix this by not sharing the palette in this way.
1056 png_set_PLTE(png_ptr
, info_ptr
, palette
, num
);
1058 /* The three chunks, bKGD, hIST and tRNS *must* appear after PLTE and before
1059 * IDAT. Prior to 1.6.0 this was not checked; instead the code merely
1060 * checked the apparent validity of a tRNS chunk inserted before PLTE on a
1061 * palette PNG. 1.6.0 attempts to rigorously follow the standard and
1062 * therefore does a benign error if the erroneous condition is detected *and*
1063 * cancels the tRNS if the benign error returns. The alternative is to
1064 * amend the standard since it would be rather hypocritical of the standards
1065 * maintainers to ignore it.
1067 #ifdef PNG_READ_tRNS_SUPPORTED
1068 if (png_ptr
->num_trans
> 0 ||
1069 (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_tRNS
) != 0))
1071 /* Cancel this because otherwise it would be used if the transforms
1072 * require it. Don't cancel the 'valid' flag because this would prevent
1073 * detection of duplicate chunks.
1075 png_ptr
->num_trans
= 0;
1077 if (info_ptr
!= NULL
)
1078 info_ptr
->num_trans
= 0;
1080 png_chunk_benign_error(png_ptr
, "tRNS must be after");
1084 #ifdef PNG_READ_hIST_SUPPORTED
1085 if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_hIST
) != 0)
1086 png_chunk_benign_error(png_ptr
, "hIST must be after");
1089 #ifdef PNG_READ_bKGD_SUPPORTED
1090 if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_bKGD
) != 0)
1091 png_chunk_benign_error(png_ptr
, "bKGD must be after");
1096 png_handle_IEND(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
1098 png_debug(1, "in png_handle_IEND");
1100 if ((png_ptr
->mode
& PNG_HAVE_IHDR
) == 0 ||
1101 (png_ptr
->mode
& PNG_HAVE_IDAT
) == 0)
1102 png_chunk_error(png_ptr
, "out of place");
1104 png_ptr
->mode
|= (PNG_AFTER_IDAT
| PNG_HAVE_IEND
);
1106 png_crc_finish(png_ptr
, length
);
1109 png_chunk_benign_error(png_ptr
, "invalid");
1111 PNG_UNUSED(info_ptr
)
1114 #ifdef PNG_READ_gAMA_SUPPORTED
1116 png_handle_gAMA(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
1118 png_fixed_point igamma
;
1121 png_debug(1, "in png_handle_gAMA");
1123 if ((png_ptr
->mode
& PNG_HAVE_IHDR
) == 0)
1124 png_chunk_error(png_ptr
, "missing IHDR");
1126 else if ((png_ptr
->mode
& (PNG_HAVE_IDAT
|PNG_HAVE_PLTE
)) != 0)
1128 png_crc_finish(png_ptr
, length
);
1129 png_chunk_benign_error(png_ptr
, "out of place");
1135 png_crc_finish(png_ptr
, length
);
1136 png_chunk_benign_error(png_ptr
, "invalid");
1140 png_crc_read(png_ptr
, buf
, 4);
1142 if (png_crc_finish(png_ptr
, 0) != 0)
1145 igamma
= png_get_fixed_point(NULL
, buf
);
1147 png_colorspace_set_gamma(png_ptr
, &png_ptr
->colorspace
, igamma
);
1148 png_colorspace_sync(png_ptr
, info_ptr
);
1152 #ifdef PNG_READ_sBIT_SUPPORTED
1154 png_handle_sBIT(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
1156 unsigned int truelen
, i
;
1157 png_byte sample_depth
;
1160 png_debug(1, "in png_handle_sBIT");
1162 if ((png_ptr
->mode
& PNG_HAVE_IHDR
) == 0)
1163 png_chunk_error(png_ptr
, "missing IHDR");
1165 else if ((png_ptr
->mode
& (PNG_HAVE_IDAT
|PNG_HAVE_PLTE
)) != 0)
1167 png_crc_finish(png_ptr
, length
);
1168 png_chunk_benign_error(png_ptr
, "out of place");
1172 if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_sBIT
) != 0)
1174 png_crc_finish(png_ptr
, length
);
1175 png_chunk_benign_error(png_ptr
, "duplicate");
1179 if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
1187 truelen
= png_ptr
->channels
;
1188 sample_depth
= png_ptr
->bit_depth
;
1191 if (length
!= truelen
|| length
> 4)
1193 png_chunk_benign_error(png_ptr
, "invalid");
1194 png_crc_finish(png_ptr
, length
);
1198 buf
[0] = buf
[1] = buf
[2] = buf
[3] = sample_depth
;
1199 png_crc_read(png_ptr
, buf
, truelen
);
1201 if (png_crc_finish(png_ptr
, 0) != 0)
1204 for (i
=0; i
<truelen
; ++i
)
1206 if (buf
[i
] == 0 || buf
[i
] > sample_depth
)
1208 png_chunk_benign_error(png_ptr
, "invalid");
1213 if ((png_ptr
->color_type
& PNG_COLOR_MASK_COLOR
) != 0)
1215 png_ptr
->sig_bit
.red
= buf
[0];
1216 png_ptr
->sig_bit
.green
= buf
[1];
1217 png_ptr
->sig_bit
.blue
= buf
[2];
1218 png_ptr
->sig_bit
.alpha
= buf
[3];
1223 png_ptr
->sig_bit
.gray
= buf
[0];
1224 png_ptr
->sig_bit
.red
= buf
[0];
1225 png_ptr
->sig_bit
.green
= buf
[0];
1226 png_ptr
->sig_bit
.blue
= buf
[0];
1227 png_ptr
->sig_bit
.alpha
= buf
[1];
1230 png_set_sBIT(png_ptr
, info_ptr
, &(png_ptr
->sig_bit
));
1234 #ifdef PNG_READ_cHRM_SUPPORTED
1236 png_handle_cHRM(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
1241 png_debug(1, "in png_handle_cHRM");
1243 if ((png_ptr
->mode
& PNG_HAVE_IHDR
) == 0)
1244 png_chunk_error(png_ptr
, "missing IHDR");
1246 else if ((png_ptr
->mode
& (PNG_HAVE_IDAT
|PNG_HAVE_PLTE
)) != 0)
1248 png_crc_finish(png_ptr
, length
);
1249 png_chunk_benign_error(png_ptr
, "out of place");
1255 png_crc_finish(png_ptr
, length
);
1256 png_chunk_benign_error(png_ptr
, "invalid");
1260 png_crc_read(png_ptr
, buf
, 32);
1262 if (png_crc_finish(png_ptr
, 0) != 0)
1265 xy
.whitex
= png_get_fixed_point(NULL
, buf
);
1266 xy
.whitey
= png_get_fixed_point(NULL
, buf
+ 4);
1267 xy
.redx
= png_get_fixed_point(NULL
, buf
+ 8);
1268 xy
.redy
= png_get_fixed_point(NULL
, buf
+ 12);
1269 xy
.greenx
= png_get_fixed_point(NULL
, buf
+ 16);
1270 xy
.greeny
= png_get_fixed_point(NULL
, buf
+ 20);
1271 xy
.bluex
= png_get_fixed_point(NULL
, buf
+ 24);
1272 xy
.bluey
= png_get_fixed_point(NULL
, buf
+ 28);
1274 if (xy
.whitex
== PNG_FIXED_ERROR
||
1275 xy
.whitey
== PNG_FIXED_ERROR
||
1276 xy
.redx
== PNG_FIXED_ERROR
||
1277 xy
.redy
== PNG_FIXED_ERROR
||
1278 xy
.greenx
== PNG_FIXED_ERROR
||
1279 xy
.greeny
== PNG_FIXED_ERROR
||
1280 xy
.bluex
== PNG_FIXED_ERROR
||
1281 xy
.bluey
== PNG_FIXED_ERROR
)
1283 png_chunk_benign_error(png_ptr
, "invalid values");
1287 /* If a colorspace error has already been output skip this chunk */
1288 if ((png_ptr
->colorspace
.flags
& PNG_COLORSPACE_INVALID
) != 0)
1291 if ((png_ptr
->colorspace
.flags
& PNG_COLORSPACE_FROM_cHRM
) != 0)
1293 png_ptr
->colorspace
.flags
|= PNG_COLORSPACE_INVALID
;
1294 png_colorspace_sync(png_ptr
, info_ptr
);
1295 png_chunk_benign_error(png_ptr
, "duplicate");
1299 png_ptr
->colorspace
.flags
|= PNG_COLORSPACE_FROM_cHRM
;
1300 (void)png_colorspace_set_chromaticities(png_ptr
, &png_ptr
->colorspace
, &xy
,
1301 1/*prefer cHRM values*/);
1302 png_colorspace_sync(png_ptr
, info_ptr
);
1306 #ifdef PNG_READ_sRGB_SUPPORTED
1308 png_handle_sRGB(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
1312 png_debug(1, "in png_handle_sRGB");
1314 if ((png_ptr
->mode
& PNG_HAVE_IHDR
) == 0)
1315 png_chunk_error(png_ptr
, "missing IHDR");
1317 else if ((png_ptr
->mode
& (PNG_HAVE_IDAT
|PNG_HAVE_PLTE
)) != 0)
1319 png_crc_finish(png_ptr
, length
);
1320 png_chunk_benign_error(png_ptr
, "out of place");
1326 png_crc_finish(png_ptr
, length
);
1327 png_chunk_benign_error(png_ptr
, "invalid");
1331 png_crc_read(png_ptr
, &intent
, 1);
1333 if (png_crc_finish(png_ptr
, 0) != 0)
1336 /* If a colorspace error has already been output skip this chunk */
1337 if ((png_ptr
->colorspace
.flags
& PNG_COLORSPACE_INVALID
) != 0)
1340 /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect
1343 if ((png_ptr
->colorspace
.flags
& PNG_COLORSPACE_HAVE_INTENT
) != 0)
1345 png_ptr
->colorspace
.flags
|= PNG_COLORSPACE_INVALID
;
1346 png_colorspace_sync(png_ptr
, info_ptr
);
1347 png_chunk_benign_error(png_ptr
, "too many profiles");
1351 (void)png_colorspace_set_sRGB(png_ptr
, &png_ptr
->colorspace
, intent
);
1352 png_colorspace_sync(png_ptr
, info_ptr
);
1354 #endif /* READ_sRGB */
1356 #ifdef PNG_READ_iCCP_SUPPORTED
1358 png_handle_iCCP(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
1359 /* Note: this does not properly handle profiles that are > 64K under DOS */
1361 png_const_charp errmsg
= NULL
; /* error message output, or no error */
1362 int finished
= 0; /* crc checked */
1364 png_debug(1, "in png_handle_iCCP");
1366 if ((png_ptr
->mode
& PNG_HAVE_IHDR
) == 0)
1367 png_chunk_error(png_ptr
, "missing IHDR");
1369 else if ((png_ptr
->mode
& (PNG_HAVE_IDAT
|PNG_HAVE_PLTE
)) != 0)
1371 png_crc_finish(png_ptr
, length
);
1372 png_chunk_benign_error(png_ptr
, "out of place");
1376 /* Consistent with all the above colorspace handling an obviously *invalid*
1377 * chunk is just ignored, so does not invalidate the color space. An
1378 * alternative is to set the 'invalid' flags at the start of this routine
1379 * and only clear them in they were not set before and all the tests pass.
1380 * The minimum 'deflate' stream is assumed to be just the 2 byte header and
1381 * 4 byte checksum. The keyword must be at least one character and there is
1382 * a terminator (0) byte and the compression method.
1386 png_crc_finish(png_ptr
, length
);
1387 png_chunk_benign_error(png_ptr
, "too short");
1391 /* If a colorspace error has already been output skip this chunk */
1392 if ((png_ptr
->colorspace
.flags
& PNG_COLORSPACE_INVALID
) != 0)
1394 png_crc_finish(png_ptr
, length
);
1398 /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect
1401 if ((png_ptr
->colorspace
.flags
& PNG_COLORSPACE_HAVE_INTENT
) == 0)
1403 uInt read_length
, keyword_length
;
1406 /* Find the keyword; the keyword plus separator and compression method
1407 * bytes can be at most 81 characters long.
1409 read_length
= 81; /* maximum */
1410 if (read_length
> length
)
1411 read_length
= (uInt
)length
;
1413 png_crc_read(png_ptr
, (png_bytep
)keyword
, read_length
);
1414 length
-= read_length
;
1417 while (keyword_length
< 80 && keyword_length
< read_length
&&
1418 keyword
[keyword_length
] != 0)
1421 /* TODO: make the keyword checking common */
1422 if (keyword_length
>= 1 && keyword_length
<= 79)
1424 /* We only understand '0' compression - deflate - so if we get a
1425 * different value we can't safely decode the chunk.
1427 if (keyword_length
+1 < read_length
&&
1428 keyword
[keyword_length
+1] == PNG_COMPRESSION_TYPE_BASE
)
1430 read_length
-= keyword_length
+2;
1432 if (png_inflate_claim(png_ptr
, png_iCCP
) == Z_OK
)
1434 Byte profile_header
[132];
1435 Byte local_buffer
[PNG_INFLATE_BUF_SIZE
];
1436 png_alloc_size_t size
= (sizeof profile_header
);
1438 png_ptr
->zstream
.next_in
= (Bytef
*)keyword
+ (keyword_length
+2);
1439 png_ptr
->zstream
.avail_in
= read_length
;
1440 (void)png_inflate_read(png_ptr
, local_buffer
,
1441 (sizeof local_buffer
), &length
, profile_header
, &size
,
1442 0/*finish: don't, because the output is too small*/);
1446 /* We have the ICC profile header; do the basic header checks.
1448 const png_uint_32 profile_length
=
1449 png_get_uint_32(profile_header
);
1451 if (png_icc_check_length(png_ptr
, &png_ptr
->colorspace
,
1452 keyword
, profile_length
) != 0)
1454 /* The length is apparently ok, so we can check the 132
1457 if (png_icc_check_header(png_ptr
, &png_ptr
->colorspace
,
1458 keyword
, profile_length
, profile_header
,
1459 png_ptr
->color_type
) != 0)
1461 /* Now read the tag table; a variable size buffer is
1462 * needed at this point, allocate one for the whole
1463 * profile. The header check has already validated
1464 * that none of these stuff will overflow.
1466 const png_uint_32 tag_count
= png_get_uint_32(
1467 profile_header
+128);
1468 png_bytep profile
= png_read_buffer(png_ptr
,
1469 profile_length
, 2/*silent*/);
1471 if (profile
!= NULL
)
1473 memcpy(profile
, profile_header
,
1474 (sizeof profile_header
));
1476 size
= 12 * tag_count
;
1478 (void)png_inflate_read(png_ptr
, local_buffer
,
1479 (sizeof local_buffer
), &length
,
1480 profile
+ (sizeof profile_header
), &size
, 0);
1482 /* Still expect a buffer error because we expect
1483 * there to be some tag data!
1487 if (png_icc_check_tag_table(png_ptr
,
1488 &png_ptr
->colorspace
, keyword
, profile_length
,
1491 /* The profile has been validated for basic
1492 * security issues, so read the whole thing in.
1494 size
= profile_length
- (sizeof profile_header
)
1497 (void)png_inflate_read(png_ptr
, local_buffer
,
1498 (sizeof local_buffer
), &length
,
1499 profile
+ (sizeof profile_header
) +
1500 12 * tag_count
, &size
, 1/*finish*/);
1502 if (length
> 0 && !(png_ptr
->flags
&
1503 PNG_FLAG_BENIGN_ERRORS_WARN
))
1504 errmsg
= "extra compressed data";
1506 /* But otherwise allow extra data: */
1511 /* This can be handled completely, so
1514 png_chunk_warning(png_ptr
,
1515 "extra compressed data");
1518 png_crc_finish(png_ptr
, length
);
1521 # if defined(PNG_sRGB_SUPPORTED) && PNG_sRGB_PROFILE_CHECKS >= 0
1522 /* Check for a match against sRGB */
1523 png_icc_set_sRGB(png_ptr
,
1524 &png_ptr
->colorspace
, profile
,
1525 png_ptr
->zstream
.adler
);
1528 /* Steal the profile for info_ptr. */
1529 if (info_ptr
!= NULL
)
1531 png_free_data(png_ptr
, info_ptr
,
1534 info_ptr
->iccp_name
= png_voidcast(char*,
1535 png_malloc_base(png_ptr
,
1537 if (info_ptr
->iccp_name
!= NULL
)
1539 memcpy(info_ptr
->iccp_name
, keyword
,
1541 info_ptr
->iccp_proflen
=
1543 info_ptr
->iccp_profile
= profile
;
1544 png_ptr
->read_buffer
= NULL
; /*steal*/
1545 info_ptr
->free_me
|= PNG_FREE_ICCP
;
1546 info_ptr
->valid
|= PNG_INFO_iCCP
;
1551 png_ptr
->colorspace
.flags
|=
1552 PNG_COLORSPACE_INVALID
;
1553 errmsg
= "out of memory";
1557 /* else the profile remains in the read
1558 * buffer which gets reused for subsequent
1562 if (info_ptr
!= NULL
)
1563 png_colorspace_sync(png_ptr
, info_ptr
);
1567 png_ptr
->zowner
= 0;
1573 errmsg
= "truncated";
1575 #ifndef __COVERITY__
1577 errmsg
= png_ptr
->zstream
.msg
;
1581 /* else png_icc_check_tag_table output an error */
1584 else /* profile truncated */
1585 errmsg
= png_ptr
->zstream
.msg
;
1589 errmsg
= "out of memory";
1592 /* else png_icc_check_header output an error */
1595 /* else png_icc_check_length output an error */
1598 else /* profile truncated */
1599 errmsg
= png_ptr
->zstream
.msg
;
1601 /* Release the stream */
1602 png_ptr
->zowner
= 0;
1605 else /* png_inflate_claim failed */
1606 errmsg
= png_ptr
->zstream
.msg
;
1610 errmsg
= "bad compression method"; /* or missing */
1614 errmsg
= "bad keyword";
1618 errmsg
= "too many profiles";
1620 /* Failure: the reason is in 'errmsg' */
1622 png_crc_finish(png_ptr
, length
);
1624 png_ptr
->colorspace
.flags
|= PNG_COLORSPACE_INVALID
;
1625 png_colorspace_sync(png_ptr
, info_ptr
);
1626 if (errmsg
!= NULL
) /* else already output */
1627 png_chunk_benign_error(png_ptr
, errmsg
);
1629 #endif /* READ_iCCP */
1631 #ifdef PNG_READ_sPLT_SUPPORTED
1633 png_handle_sPLT(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
1634 /* Note: this does not properly handle chunks that are > 64K under DOS */
1636 png_bytep entry_start
, buffer
;
1637 png_sPLT_t new_palette
;
1639 png_uint_32 data_length
;
1641 png_uint_32 skip
= 0;
1645 png_debug(1, "in png_handle_sPLT");
1647 #ifdef PNG_USER_LIMITS_SUPPORTED
1648 if (png_ptr
->user_chunk_cache_max
!= 0)
1650 if (png_ptr
->user_chunk_cache_max
== 1)
1652 png_crc_finish(png_ptr
, length
);
1656 if (--png_ptr
->user_chunk_cache_max
== 1)
1658 png_warning(png_ptr
, "No space in chunk cache for sPLT");
1659 png_crc_finish(png_ptr
, length
);
1665 if ((png_ptr
->mode
& PNG_HAVE_IHDR
) == 0)
1666 png_chunk_error(png_ptr
, "missing IHDR");
1668 else if ((png_ptr
->mode
& PNG_HAVE_IDAT
) != 0)
1670 png_crc_finish(png_ptr
, length
);
1671 png_chunk_benign_error(png_ptr
, "out of place");
1675 #ifdef PNG_MAX_MALLOC_64K
1676 if (length
> 65535U)
1678 png_crc_finish(png_ptr
, length
);
1679 png_chunk_benign_error(png_ptr
, "too large to fit in memory");
1684 buffer
= png_read_buffer(png_ptr
, length
+1, 2/*silent*/);
1687 png_crc_finish(png_ptr
, length
);
1688 png_chunk_benign_error(png_ptr
, "out of memory");
1693 /* WARNING: this may break if size_t is less than 32 bits; it is assumed
1694 * that the PNG_MAX_MALLOC_64K test is enabled in this case, but this is a
1695 * potential breakage point if the types in pngconf.h aren't exactly right.
1697 png_crc_read(png_ptr
, buffer
, length
);
1699 if (png_crc_finish(png_ptr
, skip
) != 0)
1704 for (entry_start
= buffer
; *entry_start
; entry_start
++)
1705 /* Empty loop to find end of name */ ;
1709 /* A sample depth should follow the separator, and we should be on it */
1710 if (length
< 2U || entry_start
> buffer
+ (length
- 2U))
1712 png_warning(png_ptr
, "malformed sPLT chunk");
1716 new_palette
.depth
= *entry_start
++;
1717 entry_size
= (new_palette
.depth
== 8 ? 6 : 10);
1718 /* This must fit in a png_uint_32 because it is derived from the original
1719 * chunk data length.
1721 data_length
= length
- (png_uint_32
)(entry_start
- buffer
);
1723 /* Integrity-check the data length */
1724 if ((data_length
% (unsigned int)entry_size
) != 0)
1726 png_warning(png_ptr
, "sPLT chunk has bad length");
1730 dl
= (png_uint_32
)(data_length
/ (unsigned int)entry_size
);
1731 max_dl
= PNG_SIZE_MAX
/ (sizeof (png_sPLT_entry
));
1735 png_warning(png_ptr
, "sPLT chunk too long");
1739 new_palette
.nentries
= (png_int_32
)(data_length
/ (unsigned int)entry_size
);
1741 new_palette
.entries
= (png_sPLT_entryp
)png_malloc_warn(png_ptr
,
1742 (png_alloc_size_t
) new_palette
.nentries
* (sizeof (png_sPLT_entry
)));
1744 if (new_palette
.entries
== NULL
)
1746 png_warning(png_ptr
, "sPLT chunk requires too much memory");
1750 #ifdef PNG_POINTER_INDEXING_SUPPORTED
1751 for (i
= 0; i
< new_palette
.nentries
; i
++)
1753 pp
= new_palette
.entries
+ i
;
1755 if (new_palette
.depth
== 8)
1757 pp
->red
= *entry_start
++;
1758 pp
->green
= *entry_start
++;
1759 pp
->blue
= *entry_start
++;
1760 pp
->alpha
= *entry_start
++;
1765 pp
->red
= png_get_uint_16(entry_start
); entry_start
+= 2;
1766 pp
->green
= png_get_uint_16(entry_start
); entry_start
+= 2;
1767 pp
->blue
= png_get_uint_16(entry_start
); entry_start
+= 2;
1768 pp
->alpha
= png_get_uint_16(entry_start
); entry_start
+= 2;
1771 pp
->frequency
= png_get_uint_16(entry_start
); entry_start
+= 2;
1774 pp
= new_palette
.entries
;
1776 for (i
= 0; i
< new_palette
.nentries
; i
++)
1779 if (new_palette
.depth
== 8)
1781 pp
[i
].red
= *entry_start
++;
1782 pp
[i
].green
= *entry_start
++;
1783 pp
[i
].blue
= *entry_start
++;
1784 pp
[i
].alpha
= *entry_start
++;
1789 pp
[i
].red
= png_get_uint_16(entry_start
); entry_start
+= 2;
1790 pp
[i
].green
= png_get_uint_16(entry_start
); entry_start
+= 2;
1791 pp
[i
].blue
= png_get_uint_16(entry_start
); entry_start
+= 2;
1792 pp
[i
].alpha
= png_get_uint_16(entry_start
); entry_start
+= 2;
1795 pp
[i
].frequency
= png_get_uint_16(entry_start
); entry_start
+= 2;
1799 /* Discard all chunk data except the name and stash that */
1800 new_palette
.name
= (png_charp
)buffer
;
1802 png_set_sPLT(png_ptr
, info_ptr
, &new_palette
, 1);
1804 png_free(png_ptr
, new_palette
.entries
);
1806 #endif /* READ_sPLT */
1808 #ifdef PNG_READ_tRNS_SUPPORTED
1810 png_handle_tRNS(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
1812 png_byte readbuf
[PNG_MAX_PALETTE_LENGTH
];
1814 png_debug(1, "in png_handle_tRNS");
1816 if ((png_ptr
->mode
& PNG_HAVE_IHDR
) == 0)
1817 png_chunk_error(png_ptr
, "missing IHDR");
1819 else if ((png_ptr
->mode
& PNG_HAVE_IDAT
) != 0)
1821 png_crc_finish(png_ptr
, length
);
1822 png_chunk_benign_error(png_ptr
, "out of place");
1826 else if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_tRNS
) != 0)
1828 png_crc_finish(png_ptr
, length
);
1829 png_chunk_benign_error(png_ptr
, "duplicate");
1833 if (png_ptr
->color_type
== PNG_COLOR_TYPE_GRAY
)
1839 png_crc_finish(png_ptr
, length
);
1840 png_chunk_benign_error(png_ptr
, "invalid");
1844 png_crc_read(png_ptr
, buf
, 2);
1845 png_ptr
->num_trans
= 1;
1846 png_ptr
->trans_color
.gray
= png_get_uint_16(buf
);
1849 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_RGB
)
1855 png_crc_finish(png_ptr
, length
);
1856 png_chunk_benign_error(png_ptr
, "invalid");
1860 png_crc_read(png_ptr
, buf
, length
);
1861 png_ptr
->num_trans
= 1;
1862 png_ptr
->trans_color
.red
= png_get_uint_16(buf
);
1863 png_ptr
->trans_color
.green
= png_get_uint_16(buf
+ 2);
1864 png_ptr
->trans_color
.blue
= png_get_uint_16(buf
+ 4);
1867 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
1869 if ((png_ptr
->mode
& PNG_HAVE_PLTE
) == 0)
1871 /* TODO: is this actually an error in the ISO spec? */
1872 png_crc_finish(png_ptr
, length
);
1873 png_chunk_benign_error(png_ptr
, "out of place");
1877 if (length
> (unsigned int) png_ptr
->num_palette
||
1878 length
> (unsigned int) PNG_MAX_PALETTE_LENGTH
||
1881 png_crc_finish(png_ptr
, length
);
1882 png_chunk_benign_error(png_ptr
, "invalid");
1886 png_crc_read(png_ptr
, readbuf
, length
);
1887 png_ptr
->num_trans
= (png_uint_16
)length
;
1892 png_crc_finish(png_ptr
, length
);
1893 png_chunk_benign_error(png_ptr
, "invalid with alpha channel");
1897 if (png_crc_finish(png_ptr
, 0) != 0)
1899 png_ptr
->num_trans
= 0;
1903 /* TODO: this is a horrible side effect in the palette case because the
1904 * png_struct ends up with a pointer to the tRNS buffer owned by the
1905 * png_info. Fix this.
1907 png_set_tRNS(png_ptr
, info_ptr
, readbuf
, png_ptr
->num_trans
,
1908 &(png_ptr
->trans_color
));
1912 #ifdef PNG_READ_bKGD_SUPPORTED
1914 png_handle_bKGD(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
1916 unsigned int truelen
;
1918 png_color_16 background
;
1920 png_debug(1, "in png_handle_bKGD");
1922 if ((png_ptr
->mode
& PNG_HAVE_IHDR
) == 0)
1923 png_chunk_error(png_ptr
, "missing IHDR");
1925 else if ((png_ptr
->mode
& PNG_HAVE_IDAT
) != 0 ||
1926 (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
&&
1927 (png_ptr
->mode
& PNG_HAVE_PLTE
) == 0))
1929 png_crc_finish(png_ptr
, length
);
1930 png_chunk_benign_error(png_ptr
, "out of place");
1934 else if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_bKGD
) != 0)
1936 png_crc_finish(png_ptr
, length
);
1937 png_chunk_benign_error(png_ptr
, "duplicate");
1941 if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
1944 else if ((png_ptr
->color_type
& PNG_COLOR_MASK_COLOR
) != 0)
1950 if (length
!= truelen
)
1952 png_crc_finish(png_ptr
, length
);
1953 png_chunk_benign_error(png_ptr
, "invalid");
1957 png_crc_read(png_ptr
, buf
, truelen
);
1959 if (png_crc_finish(png_ptr
, 0) != 0)
1962 /* We convert the index value into RGB components so that we can allow
1963 * arbitrary RGB values for background when we have transparency, and
1964 * so it is easy to determine the RGB values of the background color
1965 * from the info_ptr struct.
1967 if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
1969 background
.index
= buf
[0];
1971 if (info_ptr
!= NULL
&& info_ptr
->num_palette
!= 0)
1973 if (buf
[0] >= info_ptr
->num_palette
)
1975 png_chunk_benign_error(png_ptr
, "invalid index");
1979 background
.red
= (png_uint_16
)png_ptr
->palette
[buf
[0]].red
;
1980 background
.green
= (png_uint_16
)png_ptr
->palette
[buf
[0]].green
;
1981 background
.blue
= (png_uint_16
)png_ptr
->palette
[buf
[0]].blue
;
1985 background
.red
= background
.green
= background
.blue
= 0;
1987 background
.gray
= 0;
1990 else if ((png_ptr
->color_type
& PNG_COLOR_MASK_COLOR
) == 0) /* GRAY */
1992 background
.index
= 0;
1996 background
.gray
= png_get_uint_16(buf
);
2001 background
.index
= 0;
2002 background
.red
= png_get_uint_16(buf
);
2003 background
.green
= png_get_uint_16(buf
+ 2);
2004 background
.blue
= png_get_uint_16(buf
+ 4);
2005 background
.gray
= 0;
2008 png_set_bKGD(png_ptr
, info_ptr
, &background
);
2012 #ifdef PNG_READ_hIST_SUPPORTED
2014 png_handle_hIST(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
2016 unsigned int num
, i
;
2017 png_uint_16 readbuf
[PNG_MAX_PALETTE_LENGTH
];
2019 png_debug(1, "in png_handle_hIST");
2021 if ((png_ptr
->mode
& PNG_HAVE_IHDR
) == 0)
2022 png_chunk_error(png_ptr
, "missing IHDR");
2024 else if ((png_ptr
->mode
& PNG_HAVE_IDAT
) != 0 ||
2025 (png_ptr
->mode
& PNG_HAVE_PLTE
) == 0)
2027 png_crc_finish(png_ptr
, length
);
2028 png_chunk_benign_error(png_ptr
, "out of place");
2032 else if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_hIST
) != 0)
2034 png_crc_finish(png_ptr
, length
);
2035 png_chunk_benign_error(png_ptr
, "duplicate");
2041 if (num
!= (unsigned int) png_ptr
->num_palette
||
2042 num
> (unsigned int) PNG_MAX_PALETTE_LENGTH
)
2044 png_crc_finish(png_ptr
, length
);
2045 png_chunk_benign_error(png_ptr
, "invalid");
2049 for (i
= 0; i
< num
; i
++)
2053 png_crc_read(png_ptr
, buf
, 2);
2054 readbuf
[i
] = png_get_uint_16(buf
);
2057 if (png_crc_finish(png_ptr
, 0) != 0)
2060 png_set_hIST(png_ptr
, info_ptr
, readbuf
);
2064 #ifdef PNG_READ_pHYs_SUPPORTED
2066 png_handle_pHYs(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
2069 png_uint_32 res_x
, res_y
;
2072 png_debug(1, "in png_handle_pHYs");
2074 if ((png_ptr
->mode
& PNG_HAVE_IHDR
) == 0)
2075 png_chunk_error(png_ptr
, "missing IHDR");
2077 else if ((png_ptr
->mode
& PNG_HAVE_IDAT
) != 0)
2079 png_crc_finish(png_ptr
, length
);
2080 png_chunk_benign_error(png_ptr
, "out of place");
2084 else if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_pHYs
) != 0)
2086 png_crc_finish(png_ptr
, length
);
2087 png_chunk_benign_error(png_ptr
, "duplicate");
2093 png_crc_finish(png_ptr
, length
);
2094 png_chunk_benign_error(png_ptr
, "invalid");
2098 png_crc_read(png_ptr
, buf
, 9);
2100 if (png_crc_finish(png_ptr
, 0) != 0)
2103 res_x
= png_get_uint_32(buf
);
2104 res_y
= png_get_uint_32(buf
+ 4);
2106 png_set_pHYs(png_ptr
, info_ptr
, res_x
, res_y
, unit_type
);
2110 #ifdef PNG_READ_oFFs_SUPPORTED
2112 png_handle_oFFs(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
2115 png_int_32 offset_x
, offset_y
;
2118 png_debug(1, "in png_handle_oFFs");
2120 if ((png_ptr
->mode
& PNG_HAVE_IHDR
) == 0)
2121 png_chunk_error(png_ptr
, "missing IHDR");
2123 else if ((png_ptr
->mode
& PNG_HAVE_IDAT
) != 0)
2125 png_crc_finish(png_ptr
, length
);
2126 png_chunk_benign_error(png_ptr
, "out of place");
2130 else if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_oFFs
) != 0)
2132 png_crc_finish(png_ptr
, length
);
2133 png_chunk_benign_error(png_ptr
, "duplicate");
2139 png_crc_finish(png_ptr
, length
);
2140 png_chunk_benign_error(png_ptr
, "invalid");
2144 png_crc_read(png_ptr
, buf
, 9);
2146 if (png_crc_finish(png_ptr
, 0) != 0)
2149 offset_x
= png_get_int_32(buf
);
2150 offset_y
= png_get_int_32(buf
+ 4);
2152 png_set_oFFs(png_ptr
, info_ptr
, offset_x
, offset_y
, unit_type
);
2156 #ifdef PNG_READ_pCAL_SUPPORTED
2157 /* Read the pCAL chunk (described in the PNG Extensions document) */
2159 png_handle_pCAL(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
2162 png_byte type
, nparams
;
2163 png_bytep buffer
, buf
, units
, endptr
;
2167 png_debug(1, "in png_handle_pCAL");
2169 if ((png_ptr
->mode
& PNG_HAVE_IHDR
) == 0)
2170 png_chunk_error(png_ptr
, "missing IHDR");
2172 else if ((png_ptr
->mode
& PNG_HAVE_IDAT
) != 0)
2174 png_crc_finish(png_ptr
, length
);
2175 png_chunk_benign_error(png_ptr
, "out of place");
2179 else if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_pCAL
) != 0)
2181 png_crc_finish(png_ptr
, length
);
2182 png_chunk_benign_error(png_ptr
, "duplicate");
2186 png_debug1(2, "Allocating and reading pCAL chunk data (%u bytes)",
2189 buffer
= png_read_buffer(png_ptr
, length
+1, 2/*silent*/);
2193 png_crc_finish(png_ptr
, length
);
2194 png_chunk_benign_error(png_ptr
, "out of memory");
2198 png_crc_read(png_ptr
, buffer
, length
);
2200 if (png_crc_finish(png_ptr
, 0) != 0)
2203 buffer
[length
] = 0; /* Null terminate the last string */
2205 png_debug(3, "Finding end of pCAL purpose string");
2206 for (buf
= buffer
; *buf
; buf
++)
2209 endptr
= buffer
+ length
;
2211 /* We need to have at least 12 bytes after the purpose string
2212 * in order to get the parameter information.
2214 if (endptr
- buf
<= 12)
2216 png_chunk_benign_error(png_ptr
, "invalid");
2220 png_debug(3, "Reading pCAL X0, X1, type, nparams, and units");
2221 X0
= png_get_int_32((png_bytep
)buf
+1);
2222 X1
= png_get_int_32((png_bytep
)buf
+5);
2227 png_debug(3, "Checking pCAL equation type and number of parameters");
2228 /* Check that we have the right number of parameters for known
2231 if ((type
== PNG_EQUATION_LINEAR
&& nparams
!= 2) ||
2232 (type
== PNG_EQUATION_BASE_E
&& nparams
!= 3) ||
2233 (type
== PNG_EQUATION_ARBITRARY
&& nparams
!= 3) ||
2234 (type
== PNG_EQUATION_HYPERBOLIC
&& nparams
!= 4))
2236 png_chunk_benign_error(png_ptr
, "invalid parameter count");
2240 else if (type
>= PNG_EQUATION_LAST
)
2242 png_chunk_benign_error(png_ptr
, "unrecognized equation type");
2245 for (buf
= units
; *buf
; buf
++)
2246 /* Empty loop to move past the units string. */ ;
2248 png_debug(3, "Allocating pCAL parameters array");
2250 params
= png_voidcast(png_charpp
, png_malloc_warn(png_ptr
,
2251 nparams
* (sizeof (png_charp
))));
2255 png_chunk_benign_error(png_ptr
, "out of memory");
2259 /* Get pointers to the start of each parameter string. */
2260 for (i
= 0; i
< nparams
; i
++)
2262 buf
++; /* Skip the null string terminator from previous parameter. */
2264 png_debug1(3, "Reading pCAL parameter %d", i
);
2266 for (params
[i
] = (png_charp
)buf
; buf
<= endptr
&& *buf
!= 0; buf
++)
2267 /* Empty loop to move past each parameter string */ ;
2269 /* Make sure we haven't run out of data yet */
2272 png_free(png_ptr
, params
);
2273 png_chunk_benign_error(png_ptr
, "invalid data");
2278 png_set_pCAL(png_ptr
, info_ptr
, (png_charp
)buffer
, X0
, X1
, type
, nparams
,
2279 (png_charp
)units
, params
);
2281 png_free(png_ptr
, params
);
2285 #ifdef PNG_READ_sCAL_SUPPORTED
2286 /* Read the sCAL chunk */
2288 png_handle_sCAL(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
2294 png_debug(1, "in png_handle_sCAL");
2296 if ((png_ptr
->mode
& PNG_HAVE_IHDR
) == 0)
2297 png_chunk_error(png_ptr
, "missing IHDR");
2299 else if ((png_ptr
->mode
& PNG_HAVE_IDAT
) != 0)
2301 png_crc_finish(png_ptr
, length
);
2302 png_chunk_benign_error(png_ptr
, "out of place");
2306 else if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_sCAL
) != 0)
2308 png_crc_finish(png_ptr
, length
);
2309 png_chunk_benign_error(png_ptr
, "duplicate");
2313 /* Need unit type, width, \0, height: minimum 4 bytes */
2314 else if (length
< 4)
2316 png_crc_finish(png_ptr
, length
);
2317 png_chunk_benign_error(png_ptr
, "invalid");
2321 png_debug1(2, "Allocating and reading sCAL chunk data (%u bytes)",
2324 buffer
= png_read_buffer(png_ptr
, length
+1, 2/*silent*/);
2328 png_chunk_benign_error(png_ptr
, "out of memory");
2329 png_crc_finish(png_ptr
, length
);
2333 png_crc_read(png_ptr
, buffer
, length
);
2334 buffer
[length
] = 0; /* Null terminate the last string */
2336 if (png_crc_finish(png_ptr
, 0) != 0)
2339 /* Validate the unit. */
2340 if (buffer
[0] != 1 && buffer
[0] != 2)
2342 png_chunk_benign_error(png_ptr
, "invalid unit");
2346 /* Validate the ASCII numbers, need two ASCII numbers separated by
2347 * a '\0' and they need to fit exactly in the chunk data.
2352 if (png_check_fp_number((png_const_charp
)buffer
, length
, &state
, &i
) == 0 ||
2353 i
>= length
|| buffer
[i
++] != 0)
2354 png_chunk_benign_error(png_ptr
, "bad width format");
2356 else if (PNG_FP_IS_POSITIVE(state
) == 0)
2357 png_chunk_benign_error(png_ptr
, "non-positive width");
2361 png_size_t heighti
= i
;
2364 if (png_check_fp_number((png_const_charp
)buffer
, length
,
2365 &state
, &i
) == 0 || i
!= length
)
2366 png_chunk_benign_error(png_ptr
, "bad height format");
2368 else if (PNG_FP_IS_POSITIVE(state
) == 0)
2369 png_chunk_benign_error(png_ptr
, "non-positive height");
2372 /* This is the (only) success case. */
2373 png_set_sCAL_s(png_ptr
, info_ptr
, buffer
[0],
2374 (png_charp
)buffer
+1, (png_charp
)buffer
+heighti
);
2379 #ifdef PNG_READ_tIME_SUPPORTED
2381 png_handle_tIME(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
2386 png_debug(1, "in png_handle_tIME");
2388 if ((png_ptr
->mode
& PNG_HAVE_IHDR
) == 0)
2389 png_chunk_error(png_ptr
, "missing IHDR");
2391 else if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_tIME
) != 0)
2393 png_crc_finish(png_ptr
, length
);
2394 png_chunk_benign_error(png_ptr
, "duplicate");
2398 if ((png_ptr
->mode
& PNG_HAVE_IDAT
) != 0)
2399 png_ptr
->mode
|= PNG_AFTER_IDAT
;
2403 png_crc_finish(png_ptr
, length
);
2404 png_chunk_benign_error(png_ptr
, "invalid");
2408 png_crc_read(png_ptr
, buf
, 7);
2410 if (png_crc_finish(png_ptr
, 0) != 0)
2413 mod_time
.second
= buf
[6];
2414 mod_time
.minute
= buf
[5];
2415 mod_time
.hour
= buf
[4];
2416 mod_time
.day
= buf
[3];
2417 mod_time
.month
= buf
[2];
2418 mod_time
.year
= png_get_uint_16(buf
);
2420 png_set_tIME(png_ptr
, info_ptr
, &mod_time
);
2424 #ifdef PNG_READ_tEXt_SUPPORTED
2425 /* Note: this does not properly handle chunks that are > 64K under DOS */
2427 png_handle_tEXt(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
2433 png_uint_32 skip
= 0;
2435 png_debug(1, "in png_handle_tEXt");
2437 #ifdef PNG_USER_LIMITS_SUPPORTED
2438 if (png_ptr
->user_chunk_cache_max
!= 0)
2440 if (png_ptr
->user_chunk_cache_max
== 1)
2442 png_crc_finish(png_ptr
, length
);
2446 if (--png_ptr
->user_chunk_cache_max
== 1)
2448 png_crc_finish(png_ptr
, length
);
2449 png_chunk_benign_error(png_ptr
, "no space in chunk cache");
2455 if ((png_ptr
->mode
& PNG_HAVE_IHDR
) == 0)
2456 png_chunk_error(png_ptr
, "missing IHDR");
2458 if ((png_ptr
->mode
& PNG_HAVE_IDAT
) != 0)
2459 png_ptr
->mode
|= PNG_AFTER_IDAT
;
2461 #ifdef PNG_MAX_MALLOC_64K
2462 if (length
> 65535U)
2464 png_crc_finish(png_ptr
, length
);
2465 png_chunk_benign_error(png_ptr
, "too large to fit in memory");
2470 buffer
= png_read_buffer(png_ptr
, length
+1, 1/*warn*/);
2474 png_chunk_benign_error(png_ptr
, "out of memory");
2478 png_crc_read(png_ptr
, buffer
, length
);
2480 if (png_crc_finish(png_ptr
, skip
) != 0)
2483 key
= (png_charp
)buffer
;
2486 for (text
= key
; *text
; text
++)
2487 /* Empty loop to find end of key */ ;
2489 if (text
!= key
+ length
)
2492 text_info
.compression
= PNG_TEXT_COMPRESSION_NONE
;
2493 text_info
.key
= key
;
2494 text_info
.lang
= NULL
;
2495 text_info
.lang_key
= NULL
;
2496 text_info
.itxt_length
= 0;
2497 text_info
.text
= text
;
2498 text_info
.text_length
= strlen(text
);
2500 if (png_set_text_2(png_ptr
, info_ptr
, &text_info
, 1) != 0)
2501 png_warning(png_ptr
, "Insufficient memory to process text chunk");
2505 #ifdef PNG_READ_zTXt_SUPPORTED
2506 /* Note: this does not correctly handle chunks that are > 64K under DOS */
2508 png_handle_zTXt(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
2510 png_const_charp errmsg
= NULL
;
2512 png_uint_32 keyword_length
;
2514 png_debug(1, "in png_handle_zTXt");
2516 #ifdef PNG_USER_LIMITS_SUPPORTED
2517 if (png_ptr
->user_chunk_cache_max
!= 0)
2519 if (png_ptr
->user_chunk_cache_max
== 1)
2521 png_crc_finish(png_ptr
, length
);
2525 if (--png_ptr
->user_chunk_cache_max
== 1)
2527 png_crc_finish(png_ptr
, length
);
2528 png_chunk_benign_error(png_ptr
, "no space in chunk cache");
2534 if ((png_ptr
->mode
& PNG_HAVE_IHDR
) == 0)
2535 png_chunk_error(png_ptr
, "missing IHDR");
2537 if ((png_ptr
->mode
& PNG_HAVE_IDAT
) != 0)
2538 png_ptr
->mode
|= PNG_AFTER_IDAT
;
2540 buffer
= png_read_buffer(png_ptr
, length
, 2/*silent*/);
2544 png_crc_finish(png_ptr
, length
);
2545 png_chunk_benign_error(png_ptr
, "out of memory");
2549 png_crc_read(png_ptr
, buffer
, length
);
2551 if (png_crc_finish(png_ptr
, 0) != 0)
2554 /* TODO: also check that the keyword contents match the spec! */
2555 for (keyword_length
= 0;
2556 keyword_length
< length
&& buffer
[keyword_length
] != 0;
2558 /* Empty loop to find end of name */ ;
2560 if (keyword_length
> 79 || keyword_length
< 1)
2561 errmsg
= "bad keyword";
2563 /* zTXt must have some LZ data after the keyword, although it may expand to
2564 * zero bytes; we need a '\0' at the end of the keyword, the compression type
2567 else if (keyword_length
+ 3 > length
)
2568 errmsg
= "truncated";
2570 else if (buffer
[keyword_length
+1] != PNG_COMPRESSION_TYPE_BASE
)
2571 errmsg
= "unknown compression type";
2575 png_alloc_size_t uncompressed_length
= PNG_SIZE_MAX
;
2577 /* TODO: at present png_decompress_chunk imposes a single application
2578 * level memory limit, this should be split to different values for iCCP
2581 if (png_decompress_chunk(png_ptr
, length
, keyword_length
+2,
2582 &uncompressed_length
, 1/*terminate*/) == Z_STREAM_END
)
2586 /* It worked; png_ptr->read_buffer now looks like a tEXt chunk except
2587 * for the extra compression type byte and the fact that it isn't
2588 * necessarily '\0' terminated.
2590 buffer
= png_ptr
->read_buffer
;
2591 buffer
[uncompressed_length
+(keyword_length
+2)] = 0;
2593 text
.compression
= PNG_TEXT_COMPRESSION_zTXt
;
2594 text
.key
= (png_charp
)buffer
;
2595 text
.text
= (png_charp
)(buffer
+ keyword_length
+2);
2596 text
.text_length
= uncompressed_length
;
2597 text
.itxt_length
= 0;
2599 text
.lang_key
= NULL
;
2601 if (png_set_text_2(png_ptr
, info_ptr
, &text
, 1) != 0)
2602 errmsg
= "insufficient memory";
2606 errmsg
= png_ptr
->zstream
.msg
;
2610 png_chunk_benign_error(png_ptr
, errmsg
);
2614 #ifdef PNG_READ_iTXt_SUPPORTED
2615 /* Note: this does not correctly handle chunks that are > 64K under DOS */
2617 png_handle_iTXt(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
2619 png_const_charp errmsg
= NULL
;
2621 png_uint_32 prefix_length
;
2623 png_debug(1, "in png_handle_iTXt");
2625 #ifdef PNG_USER_LIMITS_SUPPORTED
2626 if (png_ptr
->user_chunk_cache_max
!= 0)
2628 if (png_ptr
->user_chunk_cache_max
== 1)
2630 png_crc_finish(png_ptr
, length
);
2634 if (--png_ptr
->user_chunk_cache_max
== 1)
2636 png_crc_finish(png_ptr
, length
);
2637 png_chunk_benign_error(png_ptr
, "no space in chunk cache");
2643 if ((png_ptr
->mode
& PNG_HAVE_IHDR
) == 0)
2644 png_chunk_error(png_ptr
, "missing IHDR");
2646 if ((png_ptr
->mode
& PNG_HAVE_IDAT
) != 0)
2647 png_ptr
->mode
|= PNG_AFTER_IDAT
;
2649 buffer
= png_read_buffer(png_ptr
, length
+1, 1/*warn*/);
2653 png_crc_finish(png_ptr
, length
);
2654 png_chunk_benign_error(png_ptr
, "out of memory");
2658 png_crc_read(png_ptr
, buffer
, length
);
2660 if (png_crc_finish(png_ptr
, 0) != 0)
2663 /* First the keyword. */
2664 for (prefix_length
=0;
2665 prefix_length
< length
&& buffer
[prefix_length
] != 0;
2669 /* Perform a basic check on the keyword length here. */
2670 if (prefix_length
> 79 || prefix_length
< 1)
2671 errmsg
= "bad keyword";
2673 /* Expect keyword, compression flag, compression type, language, translated
2674 * keyword (both may be empty but are 0 terminated) then the text, which may
2677 else if (prefix_length
+ 5 > length
)
2678 errmsg
= "truncated";
2680 else if (buffer
[prefix_length
+1] == 0 ||
2681 (buffer
[prefix_length
+1] == 1 &&
2682 buffer
[prefix_length
+2] == PNG_COMPRESSION_TYPE_BASE
))
2684 int compressed
= buffer
[prefix_length
+1] != 0;
2685 png_uint_32 language_offset
, translated_keyword_offset
;
2686 png_alloc_size_t uncompressed_length
= 0;
2688 /* Now the language tag */
2690 language_offset
= prefix_length
;
2692 for (; prefix_length
< length
&& buffer
[prefix_length
] != 0;
2696 /* WARNING: the length may be invalid here, this is checked below. */
2697 translated_keyword_offset
= ++prefix_length
;
2699 for (; prefix_length
< length
&& buffer
[prefix_length
] != 0;
2703 /* prefix_length should now be at the trailing '\0' of the translated
2704 * keyword, but it may already be over the end. None of this arithmetic
2705 * can overflow because chunks are at most 2^31 bytes long, but on 16-bit
2706 * systems the available allocation may overflow.
2710 if (compressed
== 0 && prefix_length
<= length
)
2711 uncompressed_length
= length
- prefix_length
;
2713 else if (compressed
!= 0 && prefix_length
< length
)
2715 uncompressed_length
= PNG_SIZE_MAX
;
2717 /* TODO: at present png_decompress_chunk imposes a single application
2718 * level memory limit, this should be split to different values for
2719 * iCCP and text chunks.
2721 if (png_decompress_chunk(png_ptr
, length
, prefix_length
,
2722 &uncompressed_length
, 1/*terminate*/) == Z_STREAM_END
)
2723 buffer
= png_ptr
->read_buffer
;
2726 errmsg
= png_ptr
->zstream
.msg
;
2730 errmsg
= "truncated";
2736 buffer
[uncompressed_length
+prefix_length
] = 0;
2738 if (compressed
== 0)
2739 text
.compression
= PNG_ITXT_COMPRESSION_NONE
;
2742 text
.compression
= PNG_ITXT_COMPRESSION_zTXt
;
2744 text
.key
= (png_charp
)buffer
;
2745 text
.lang
= (png_charp
)buffer
+ language_offset
;
2746 text
.lang_key
= (png_charp
)buffer
+ translated_keyword_offset
;
2747 text
.text
= (png_charp
)buffer
+ prefix_length
;
2748 text
.text_length
= 0;
2749 text
.itxt_length
= uncompressed_length
;
2751 if (png_set_text_2(png_ptr
, info_ptr
, &text
, 1) != 0)
2752 errmsg
= "insufficient memory";
2757 errmsg
= "bad compression info";
2760 png_chunk_benign_error(png_ptr
, errmsg
);
2764 #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
2765 /* Utility function for png_handle_unknown; set up png_ptr::unknown_chunk */
2767 png_cache_unknown_chunk(png_structrp png_ptr
, png_uint_32 length
)
2769 png_alloc_size_t limit
= PNG_SIZE_MAX
;
2771 if (png_ptr
->unknown_chunk
.data
!= NULL
)
2773 png_free(png_ptr
, png_ptr
->unknown_chunk
.data
);
2774 png_ptr
->unknown_chunk
.data
= NULL
;
2777 # ifdef PNG_SET_USER_LIMITS_SUPPORTED
2778 if (png_ptr
->user_chunk_malloc_max
> 0 &&
2779 png_ptr
->user_chunk_malloc_max
< limit
)
2780 limit
= png_ptr
->user_chunk_malloc_max
;
2782 # elif PNG_USER_CHUNK_MALLOC_MAX > 0
2783 if (PNG_USER_CHUNK_MALLOC_MAX
< limit
)
2784 limit
= PNG_USER_CHUNK_MALLOC_MAX
;
2787 if (length
<= limit
)
2789 PNG_CSTRING_FROM_CHUNK(png_ptr
->unknown_chunk
.name
, png_ptr
->chunk_name
);
2790 /* The following is safe because of the PNG_SIZE_MAX init above */
2791 png_ptr
->unknown_chunk
.size
= (png_size_t
)length
/*SAFE*/;
2792 /* 'mode' is a flag array, only the bottom four bits matter here */
2793 png_ptr
->unknown_chunk
.location
= (png_byte
)png_ptr
->mode
/*SAFE*/;
2796 png_ptr
->unknown_chunk
.data
= NULL
;
2800 /* Do a 'warn' here - it is handled below. */
2801 png_ptr
->unknown_chunk
.data
= png_voidcast(png_bytep
,
2802 png_malloc_warn(png_ptr
, length
));
2806 if (png_ptr
->unknown_chunk
.data
== NULL
&& length
> 0)
2808 /* This is benign because we clean up correctly */
2809 png_crc_finish(png_ptr
, length
);
2810 png_chunk_benign_error(png_ptr
, "unknown chunk exceeds memory limits");
2817 png_crc_read(png_ptr
, png_ptr
->unknown_chunk
.data
, length
);
2818 png_crc_finish(png_ptr
, 0);
2822 #endif /* READ_UNKNOWN_CHUNKS */
2824 /* Handle an unknown, or known but disabled, chunk */
2826 png_handle_unknown(png_structrp png_ptr
, png_inforp info_ptr
,
2827 png_uint_32 length
, int keep
)
2829 int handled
= 0; /* the chunk was handled */
2831 png_debug(1, "in png_handle_unknown");
2833 #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
2834 /* NOTE: this code is based on the code in libpng-1.4.12 except for fixing
2835 * the bug which meant that setting a non-default behavior for a specific
2836 * chunk would be ignored (the default was always used unless a user
2837 * callback was installed).
2839 * 'keep' is the value from the png_chunk_unknown_handling, the setting for
2840 * this specific chunk_name, if PNG_HANDLE_AS_UNKNOWN_SUPPORTED, if not it
2841 * will always be PNG_HANDLE_CHUNK_AS_DEFAULT and it needs to be set here.
2842 * This is just an optimization to avoid multiple calls to the lookup
2845 # ifndef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
2846 # ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
2847 keep
= png_chunk_unknown_handling(png_ptr
, png_ptr
->chunk_name
);
2851 /* One of the following methods will read the chunk or skip it (at least one
2852 * of these is always defined because this is the only way to switch on
2853 * PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
2855 # ifdef PNG_READ_USER_CHUNKS_SUPPORTED
2856 /* The user callback takes precedence over the chunk keep value, but the
2857 * keep value is still required to validate a save of a critical chunk.
2859 if (png_ptr
->read_user_chunk_fn
!= NULL
)
2861 if (png_cache_unknown_chunk(png_ptr
, length
) != 0)
2863 /* Callback to user unknown chunk handler */
2864 int ret
= (*(png_ptr
->read_user_chunk_fn
))(png_ptr
,
2865 &png_ptr
->unknown_chunk
);
2868 * negative: An error occurred; png_chunk_error will be called.
2869 * zero: The chunk was not handled, the chunk will be discarded
2870 * unless png_set_keep_unknown_chunks has been used to set
2871 * a 'keep' behavior for this particular chunk, in which
2872 * case that will be used. A critical chunk will cause an
2873 * error at this point unless it is to be saved.
2874 * positive: The chunk was handled, libpng will ignore/discard it.
2877 png_chunk_error(png_ptr
, "error in user chunk");
2881 /* If the keep value is 'default' or 'never' override it, but
2882 * still error out on critical chunks unless the keep value is
2883 * 'always' While this is weird it is the behavior in 1.4.12.
2884 * A possible improvement would be to obey the value set for the
2885 * chunk, but this would be an API change that would probably
2886 * damage some applications.
2888 * The png_app_warning below catches the case that matters, where
2889 * the application has not set specific save or ignore for this
2890 * chunk or global save or ignore.
2892 if (keep
< PNG_HANDLE_CHUNK_IF_SAFE
)
2894 # ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
2895 if (png_ptr
->unknown_default
< PNG_HANDLE_CHUNK_IF_SAFE
)
2897 png_chunk_warning(png_ptr
, "Saving unknown chunk:");
2898 png_app_warning(png_ptr
,
2899 "forcing save of an unhandled chunk;"
2900 " please call png_set_keep_unknown_chunks");
2901 /* with keep = PNG_HANDLE_CHUNK_IF_SAFE */
2904 keep
= PNG_HANDLE_CHUNK_IF_SAFE
;
2908 else /* chunk was handled */
2911 /* Critical chunks can be safely discarded at this point. */
2912 keep
= PNG_HANDLE_CHUNK_NEVER
;
2917 keep
= PNG_HANDLE_CHUNK_NEVER
; /* insufficient memory */
2921 /* Use the SAVE_UNKNOWN_CHUNKS code or skip the chunk */
2922 # endif /* READ_USER_CHUNKS */
2924 # ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
2926 /* keep is currently just the per-chunk setting, if there was no
2927 * setting change it to the global default now (not that this may
2928 * still be AS_DEFAULT) then obtain the cache of the chunk if required,
2929 * if not simply skip the chunk.
2931 if (keep
== PNG_HANDLE_CHUNK_AS_DEFAULT
)
2932 keep
= png_ptr
->unknown_default
;
2934 if (keep
== PNG_HANDLE_CHUNK_ALWAYS
||
2935 (keep
== PNG_HANDLE_CHUNK_IF_SAFE
&&
2936 PNG_CHUNK_ANCILLARY(png_ptr
->chunk_name
)))
2938 if (png_cache_unknown_chunk(png_ptr
, length
) == 0)
2939 keep
= PNG_HANDLE_CHUNK_NEVER
;
2943 png_crc_finish(png_ptr
, length
);
2946 # ifndef PNG_READ_USER_CHUNKS_SUPPORTED
2947 # error no method to support READ_UNKNOWN_CHUNKS
2951 /* If here there is no read callback pointer set and no support is
2952 * compiled in to just save the unknown chunks, so simply skip this
2953 * chunk. If 'keep' is something other than AS_DEFAULT or NEVER then
2954 * the app has erroneously asked for unknown chunk saving when there
2957 if (keep
> PNG_HANDLE_CHUNK_NEVER
)
2958 png_app_error(png_ptr
, "no unknown chunk support available");
2960 png_crc_finish(png_ptr
, length
);
2964 # ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
2965 /* Now store the chunk in the chunk list if appropriate, and if the limits
2968 if (keep
== PNG_HANDLE_CHUNK_ALWAYS
||
2969 (keep
== PNG_HANDLE_CHUNK_IF_SAFE
&&
2970 PNG_CHUNK_ANCILLARY(png_ptr
->chunk_name
)))
2972 # ifdef PNG_USER_LIMITS_SUPPORTED
2973 switch (png_ptr
->user_chunk_cache_max
)
2976 png_ptr
->user_chunk_cache_max
= 1;
2977 png_chunk_benign_error(png_ptr
, "no space in chunk cache");
2980 /* NOTE: prior to 1.6.0 this case resulted in an unknown critical
2981 * chunk being skipped, now there will be a hard error below.
2985 default: /* not at limit */
2986 --(png_ptr
->user_chunk_cache_max
);
2988 case 0: /* no limit */
2989 # endif /* USER_LIMITS */
2990 /* Here when the limit isn't reached or when limits are compiled
2991 * out; store the chunk.
2993 png_set_unknown_chunks(png_ptr
, info_ptr
,
2994 &png_ptr
->unknown_chunk
, 1);
2996 # ifdef PNG_USER_LIMITS_SUPPORTED
3001 # else /* no store support: the chunk must be handled by the user callback */
3002 PNG_UNUSED(info_ptr
)
3005 /* Regardless of the error handling below the cached data (if any) can be
3006 * freed now. Notice that the data is not freed if there is a png_error, but
3007 * it will be freed by destroy_read_struct.
3009 if (png_ptr
->unknown_chunk
.data
!= NULL
)
3010 png_free(png_ptr
, png_ptr
->unknown_chunk
.data
);
3011 png_ptr
->unknown_chunk
.data
= NULL
;
3013 #else /* !PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */
3014 /* There is no support to read an unknown chunk, so just skip it. */
3015 png_crc_finish(png_ptr
, length
);
3016 PNG_UNUSED(info_ptr
)
3018 #endif /* !READ_UNKNOWN_CHUNKS */
3020 /* Check for unhandled critical chunks */
3021 if (handled
== 0 && PNG_CHUNK_CRITICAL(png_ptr
->chunk_name
))
3022 png_chunk_error(png_ptr
, "unhandled critical chunk");
3025 /* This function is called to verify that a chunk name is valid.
3026 * This function can't have the "critical chunk check" incorporated
3027 * into it, since in the future we will need to be able to call user
3028 * functions to handle unknown critical chunks after we check that
3029 * the chunk name itself is valid.
3032 /* Bit hacking: the test for an invalid byte in the 4 byte chunk name is:
3034 * ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
3038 png_check_chunk_name(png_structrp png_ptr
, png_uint_32 chunk_name
)
3042 png_debug(1, "in png_check_chunk_name");
3044 for (i
=1; i
<=4; ++i
)
3046 int c
= chunk_name
& 0xff;
3048 if (c
< 65 || c
> 122 || (c
> 90 && c
< 97))
3049 png_chunk_error(png_ptr
, "invalid chunk type");
3055 /* Combines the row recently read in with the existing pixels in the row. This
3056 * routine takes care of alpha and transparency if requested. This routine also
3057 * handles the two methods of progressive display of interlaced images,
3058 * depending on the 'display' value; if 'display' is true then the whole row
3059 * (dp) is filled from the start by replicating the available pixels. If
3060 * 'display' is false only those pixels present in the pass are filled in.
3063 png_combine_row(png_const_structrp png_ptr
, png_bytep dp
, int display
)
3065 unsigned int pixel_depth
= png_ptr
->transformed_pixel_depth
;
3066 png_const_bytep sp
= png_ptr
->row_buf
+ 1;
3067 png_alloc_size_t row_width
= png_ptr
->width
;
3068 unsigned int pass
= png_ptr
->pass
;
3069 png_bytep end_ptr
= 0;
3070 png_byte end_byte
= 0;
3071 unsigned int end_mask
;
3073 png_debug(1, "in png_combine_row");
3075 /* Added in 1.5.6: it should not be possible to enter this routine until at
3076 * least one row has been read from the PNG data and transformed.
3078 if (pixel_depth
== 0)
3079 png_error(png_ptr
, "internal row logic error");
3081 /* Added in 1.5.4: the pixel depth should match the information returned by
3082 * any call to png_read_update_info at this point. Do not continue if we got
3085 if (png_ptr
->info_rowbytes
!= 0 && png_ptr
->info_rowbytes
!=
3086 PNG_ROWBYTES(pixel_depth
, row_width
))
3087 png_error(png_ptr
, "internal row size calculation error");
3089 /* Don't expect this to ever happen: */
3091 png_error(png_ptr
, "internal row width error");
3093 /* Preserve the last byte in cases where only part of it will be overwritten,
3094 * the multiply below may overflow, we don't care because ANSI-C guarantees
3095 * we get the low bits.
3097 end_mask
= (pixel_depth
* row_width
) & 7;
3100 /* end_ptr == NULL is a flag to say do nothing */
3101 end_ptr
= dp
+ PNG_ROWBYTES(pixel_depth
, row_width
) - 1;
3102 end_byte
= *end_ptr
;
3103 # ifdef PNG_READ_PACKSWAP_SUPPORTED
3104 if ((png_ptr
->transformations
& PNG_PACKSWAP
) != 0)
3105 /* little-endian byte */
3106 end_mask
= (unsigned int)(0xff << end_mask
);
3108 else /* big-endian byte */
3110 end_mask
= 0xff >> end_mask
;
3111 /* end_mask is now the bits to *keep* from the destination row */
3114 /* For non-interlaced images this reduces to a memcpy(). A memcpy()
3115 * will also happen if interlacing isn't supported or if the application
3116 * does not call png_set_interlace_handling(). In the latter cases the
3117 * caller just gets a sequence of the unexpanded rows from each interlace
3120 #ifdef PNG_READ_INTERLACING_SUPPORTED
3121 if (png_ptr
->interlaced
!= 0 &&
3122 (png_ptr
->transformations
& PNG_INTERLACE
) != 0 &&
3123 pass
< 6 && (display
== 0 ||
3124 /* The following copies everything for 'display' on passes 0, 2 and 4. */
3125 (display
== 1 && (pass
& 1) != 0)))
3127 /* Narrow images may have no bits in a pass; the caller should handle
3128 * this, but this test is cheap:
3130 if (row_width
<= PNG_PASS_START_COL(pass
))
3133 if (pixel_depth
< 8)
3135 /* For pixel depths up to 4 bpp the 8-pixel mask can be expanded to fit
3136 * into 32 bits, then a single loop over the bytes using the four byte
3137 * values in the 32-bit mask can be used. For the 'display' option the
3138 * expanded mask may also not require any masking within a byte. To
3139 * make this work the PACKSWAP option must be taken into account - it
3140 * simply requires the pixels to be reversed in each byte.
3142 * The 'regular' case requires a mask for each of the first 6 passes,
3143 * the 'display' case does a copy for the even passes in the range
3144 * 0..6. This has already been handled in the test above.
3146 * The masks are arranged as four bytes with the first byte to use in
3147 * the lowest bits (little-endian) regardless of the order (PACKSWAP or
3148 * not) of the pixels in each byte.
3150 * NOTE: the whole of this logic depends on the caller of this function
3151 * only calling it on rows appropriate to the pass. This function only
3152 * understands the 'x' logic; the 'y' logic is handled by the caller.
3154 * The following defines allow generation of compile time constant bit
3155 * masks for each pixel depth and each possibility of swapped or not
3156 * swapped bytes. Pass 'p' is in the range 0..6; 'x', a pixel index,
3157 * is in the range 0..7; and the result is 1 if the pixel is to be
3158 * copied in the pass, 0 if not. 'S' is for the sparkle method, 'B'
3159 * for the block method.
3161 * With some compilers a compile time expression of the general form:
3163 * (shift >= 32) ? (a >> (shift-32)) : (b >> shift)
3165 * Produces warnings with values of 'shift' in the range 33 to 63
3166 * because the right hand side of the ?: expression is evaluated by
3167 * the compiler even though it isn't used. Microsoft Visual C (various
3168 * versions) and the Intel C compiler are known to do this. To avoid
3169 * this the following macros are used in 1.5.6. This is a temporary
3170 * solution to avoid destabilizing the code during the release process.
3172 # if PNG_USE_COMPILE_TIME_MASKS
3173 # define PNG_LSR(x,s) ((x)>>((s) & 0x1f))
3174 # define PNG_LSL(x,s) ((x)<<((s) & 0x1f))
3176 # define PNG_LSR(x,s) ((x)>>(s))
3177 # define PNG_LSL(x,s) ((x)<<(s))
3179 # define S_COPY(p,x) (((p)<4 ? PNG_LSR(0x80088822,(3-(p))*8+(7-(x))) :\
3180 PNG_LSR(0xaa55ff00,(7-(p))*8+(7-(x)))) & 1)
3181 # define B_COPY(p,x) (((p)<4 ? PNG_LSR(0xff0fff33,(3-(p))*8+(7-(x))) :\
3182 PNG_LSR(0xff55ff00,(7-(p))*8+(7-(x)))) & 1)
3184 /* Return a mask for pass 'p' pixel 'x' at depth 'd'. The mask is
3185 * little endian - the first pixel is at bit 0 - however the extra
3186 * parameter 's' can be set to cause the mask position to be swapped
3187 * within each byte, to match the PNG format. This is done by XOR of
3188 * the shift with 7, 6 or 4 for bit depths 1, 2 and 4.
3190 # define PIXEL_MASK(p,x,d,s) \
3191 (PNG_LSL(((PNG_LSL(1U,(d)))-1),(((x)*(d))^((s)?8-(d):0))))
3193 /* Hence generate the appropriate 'block' or 'sparkle' pixel copy mask.
3195 # define S_MASKx(p,x,d,s) (S_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
3196 # define B_MASKx(p,x,d,s) (B_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
3198 /* Combine 8 of these to get the full mask. For the 1-bpp and 2-bpp
3199 * cases the result needs replicating, for the 4-bpp case the above
3200 * generates a full 32 bits.
3202 # define MASK_EXPAND(m,d) ((m)*((d)==1?0x01010101:((d)==2?0x00010001:1)))
3204 # define S_MASK(p,d,s) MASK_EXPAND(S_MASKx(p,0,d,s) + S_MASKx(p,1,d,s) +\
3205 S_MASKx(p,2,d,s) + S_MASKx(p,3,d,s) + S_MASKx(p,4,d,s) +\
3206 S_MASKx(p,5,d,s) + S_MASKx(p,6,d,s) + S_MASKx(p,7,d,s), d)
3208 # define B_MASK(p,d,s) MASK_EXPAND(B_MASKx(p,0,d,s) + B_MASKx(p,1,d,s) +\
3209 B_MASKx(p,2,d,s) + B_MASKx(p,3,d,s) + B_MASKx(p,4,d,s) +\
3210 B_MASKx(p,5,d,s) + B_MASKx(p,6,d,s) + B_MASKx(p,7,d,s), d)
3212 #if PNG_USE_COMPILE_TIME_MASKS
3213 /* Utility macros to construct all the masks for a depth/swap
3214 * combination. The 's' parameter says whether the format is PNG
3215 * (big endian bytes) or not. Only the three odd-numbered passes are
3216 * required for the display/block algorithm.
3218 # define S_MASKS(d,s) { S_MASK(0,d,s), S_MASK(1,d,s), S_MASK(2,d,s),\
3219 S_MASK(3,d,s), S_MASK(4,d,s), S_MASK(5,d,s) }
3221 # define B_MASKS(d,s) { B_MASK(1,d,s), B_MASK(3,d,s), B_MASK(5,d,s) }
3223 # define DEPTH_INDEX(d) ((d)==1?0:((d)==2?1:2))
3225 /* Hence the pre-compiled masks indexed by PACKSWAP (or not), depth and
3228 static PNG_CONST png_uint_32 row_mask
[2/*PACKSWAP*/][3/*depth*/][6] =
3230 /* Little-endian byte masks for PACKSWAP */
3231 { S_MASKS(1,0), S_MASKS(2,0), S_MASKS(4,0) },
3232 /* Normal (big-endian byte) masks - PNG format */
3233 { S_MASKS(1,1), S_MASKS(2,1), S_MASKS(4,1) }
3236 /* display_mask has only three entries for the odd passes, so index by
3239 static PNG_CONST png_uint_32 display_mask
[2][3][3] =
3241 /* Little-endian byte masks for PACKSWAP */
3242 { B_MASKS(1,0), B_MASKS(2,0), B_MASKS(4,0) },
3243 /* Normal (big-endian byte) masks - PNG format */
3244 { B_MASKS(1,1), B_MASKS(2,1), B_MASKS(4,1) }
3247 # define MASK(pass,depth,display,png)\
3248 ((display)?display_mask[png][DEPTH_INDEX(depth)][pass>>1]:\
3249 row_mask[png][DEPTH_INDEX(depth)][pass])
3251 #else /* !PNG_USE_COMPILE_TIME_MASKS */
3252 /* This is the runtime alternative: it seems unlikely that this will
3253 * ever be either smaller or faster than the compile time approach.
3255 # define MASK(pass,depth,display,png)\
3256 ((display)?B_MASK(pass,depth,png):S_MASK(pass,depth,png))
3257 #endif /* !USE_COMPILE_TIME_MASKS */
3259 /* Use the appropriate mask to copy the required bits. In some cases
3260 * the byte mask will be 0 or 0xff; optimize these cases. row_width is
3261 * the number of pixels, but the code copies bytes, so it is necessary
3262 * to special case the end.
3264 png_uint_32 pixels_per_byte
= 8 / pixel_depth
;
3267 # ifdef PNG_READ_PACKSWAP_SUPPORTED
3268 if ((png_ptr
->transformations
& PNG_PACKSWAP
) != 0)
3269 mask
= MASK(pass
, pixel_depth
, display
, 0);
3273 mask
= MASK(pass
, pixel_depth
, display
, 1);
3279 /* It doesn't matter in the following if png_uint_32 has more than
3280 * 32 bits because the high bits always match those in m<<24; it is,
3281 * however, essential to use OR here, not +, because of this.
3284 mask
= (m
>> 8) | (m
<< 24); /* rotate right to good compilers */
3287 if (m
!= 0) /* something to copy */
3290 *dp
= (png_byte
)((*dp
& ~m
) | (*sp
& m
));
3295 /* NOTE: this may overwrite the last byte with garbage if the image
3296 * is not an exact number of bytes wide; libpng has always done
3299 if (row_width
<= pixels_per_byte
)
3300 break; /* May need to restore part of the last byte */
3302 row_width
-= pixels_per_byte
;
3308 else /* pixel_depth >= 8 */
3310 unsigned int bytes_to_copy
, bytes_to_jump
;
3312 /* Validate the depth - it must be a multiple of 8 */
3313 if (pixel_depth
& 7)
3314 png_error(png_ptr
, "invalid user transform pixel depth");
3316 pixel_depth
>>= 3; /* now in bytes */
3317 row_width
*= pixel_depth
;
3319 /* Regardless of pass number the Adam 7 interlace always results in a
3320 * fixed number of pixels to copy then to skip. There may be a
3321 * different number of pixels to skip at the start though.
3324 unsigned int offset
= PNG_PASS_START_COL(pass
) * pixel_depth
;
3326 row_width
-= offset
;
3331 /* Work out the bytes to copy. */
3334 /* When doing the 'block' algorithm the pixel in the pass gets
3335 * replicated to adjacent pixels. This is why the even (0,2,4,6)
3336 * passes are skipped above - the entire expanded row is copied.
3338 bytes_to_copy
= (1<<((6-pass
)>>1)) * pixel_depth
;
3340 /* But don't allow this number to exceed the actual row width. */
3341 if (bytes_to_copy
> row_width
)
3342 bytes_to_copy
= (unsigned int)/*SAFE*/row_width
;
3345 else /* normal row; Adam7 only ever gives us one pixel to copy. */
3346 bytes_to_copy
= pixel_depth
;
3348 /* In Adam7 there is a constant offset between where the pixels go. */
3349 bytes_to_jump
= PNG_PASS_COL_OFFSET(pass
) * pixel_depth
;
3351 /* And simply copy these bytes. Some optimization is possible here,
3352 * depending on the value of 'bytes_to_copy'. Special case the low
3353 * byte counts, which we know to be frequent.
3355 * Notice that these cases all 'return' rather than 'break' - this
3356 * avoids an unnecessary test on whether to restore the last byte
3359 switch (bytes_to_copy
)
3366 if (row_width
<= bytes_to_jump
)
3369 dp
+= bytes_to_jump
;
3370 sp
+= bytes_to_jump
;
3371 row_width
-= bytes_to_jump
;
3375 /* There is a possibility of a partial copy at the end here; this
3376 * slows the code down somewhat.
3380 dp
[0] = sp
[0], dp
[1] = sp
[1];
3382 if (row_width
<= bytes_to_jump
)
3385 sp
+= bytes_to_jump
;
3386 dp
+= bytes_to_jump
;
3387 row_width
-= bytes_to_jump
;
3389 while (row_width
> 1);
3391 /* And there can only be one byte left at this point: */
3396 /* This can only be the RGB case, so each copy is exactly one
3397 * pixel and it is not necessary to check for a partial copy.
3401 dp
[0] = sp
[0], dp
[1] = sp
[1], dp
[2] = sp
[2];
3403 if (row_width
<= bytes_to_jump
)
3406 sp
+= bytes_to_jump
;
3407 dp
+= bytes_to_jump
;
3408 row_width
-= bytes_to_jump
;
3412 #if PNG_ALIGN_TYPE != PNG_ALIGN_NONE
3413 /* Check for double byte alignment and, if possible, use a
3414 * 16-bit copy. Don't attempt this for narrow images - ones that
3415 * are less than an interlace panel wide. Don't attempt it for
3416 * wide bytes_to_copy either - use the memcpy there.
3418 if (bytes_to_copy
< 16 /*else use memcpy*/ &&
3419 png_isaligned(dp
, png_uint_16
) &&
3420 png_isaligned(sp
, png_uint_16
) &&
3421 bytes_to_copy
% (sizeof (png_uint_16
)) == 0 &&
3422 bytes_to_jump
% (sizeof (png_uint_16
)) == 0)
3424 /* Everything is aligned for png_uint_16 copies, but try for
3425 * png_uint_32 first.
3427 if (png_isaligned(dp
, png_uint_32
) &&
3428 png_isaligned(sp
, png_uint_32
) &&
3429 bytes_to_copy
% (sizeof (png_uint_32
)) == 0 &&
3430 bytes_to_jump
% (sizeof (png_uint_32
)) == 0)
3432 png_uint_32p dp32
= png_aligncast(png_uint_32p
,dp
);
3433 png_const_uint_32p sp32
= png_aligncastconst(
3434 png_const_uint_32p
, sp
);
3435 size_t skip
= (bytes_to_jump
-bytes_to_copy
) /
3436 (sizeof (png_uint_32
));
3440 size_t c
= bytes_to_copy
;
3444 c
-= (sizeof (png_uint_32
));
3448 if (row_width
<= bytes_to_jump
)
3453 row_width
-= bytes_to_jump
;
3455 while (bytes_to_copy
<= row_width
);
3457 /* Get to here when the row_width truncates the final copy.
3458 * There will be 1-3 bytes left to copy, so don't try the
3459 * 16-bit loop below.
3461 dp
= (png_bytep
)dp32
;
3462 sp
= (png_const_bytep
)sp32
;
3465 while (--row_width
> 0);
3469 /* Else do it in 16-bit quantities, but only if the size is
3474 png_uint_16p dp16
= png_aligncast(png_uint_16p
, dp
);
3475 png_const_uint_16p sp16
= png_aligncastconst(
3476 png_const_uint_16p
, sp
);
3477 size_t skip
= (bytes_to_jump
-bytes_to_copy
) /
3478 (sizeof (png_uint_16
));
3482 size_t c
= bytes_to_copy
;
3486 c
-= (sizeof (png_uint_16
));
3490 if (row_width
<= bytes_to_jump
)
3495 row_width
-= bytes_to_jump
;
3497 while (bytes_to_copy
<= row_width
);
3499 /* End of row - 1 byte left, bytes_to_copy > row_width: */
3500 dp
= (png_bytep
)dp16
;
3501 sp
= (png_const_bytep
)sp16
;
3504 while (--row_width
> 0);
3508 #endif /* ALIGN_TYPE code */
3510 /* The true default - use a memcpy: */
3513 memcpy(dp
, sp
, bytes_to_copy
);
3515 if (row_width
<= bytes_to_jump
)
3518 sp
+= bytes_to_jump
;
3519 dp
+= bytes_to_jump
;
3520 row_width
-= bytes_to_jump
;
3521 if (bytes_to_copy
> row_width
)
3522 bytes_to_copy
= (unsigned int)/*SAFE*/row_width
;
3527 } /* pixel_depth >= 8 */
3529 /* Here if pixel_depth < 8 to check 'end_ptr' below. */
3532 #endif /* READ_INTERLACING */
3534 /* If here then the switch above wasn't used so just memcpy the whole row
3535 * from the temporary row buffer (notice that this overwrites the end of the
3536 * destination row if it is a partial byte.)
3538 memcpy(dp
, sp
, PNG_ROWBYTES(pixel_depth
, row_width
));
3540 /* Restore the overwritten bits from the last byte if necessary. */
3541 if (end_ptr
!= NULL
)
3542 *end_ptr
= (png_byte
)((end_byte
& end_mask
) | (*end_ptr
& ~end_mask
));
3545 #ifdef PNG_READ_INTERLACING_SUPPORTED
3547 png_do_read_interlace(png_row_infop row_info
, png_bytep row
, int pass
,
3548 png_uint_32 transformations
/* Because these may affect the byte layout */)
3550 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
3551 /* Offset to next interlace block */
3552 static PNG_CONST
unsigned int png_pass_inc
[7] = {8, 8, 4, 4, 2, 2, 1};
3554 png_debug(1, "in png_do_read_interlace");
3555 if (row
!= NULL
&& row_info
!= NULL
)
3557 png_uint_32 final_width
;
3559 final_width
= row_info
->width
* png_pass_inc
[pass
];
3561 switch (row_info
->pixel_depth
)
3565 png_bytep sp
= row
+ (png_size_t
)((row_info
->width
- 1) >> 3);
3566 png_bytep dp
= row
+ (png_size_t
)((final_width
- 1) >> 3);
3567 unsigned int sshift
, dshift
;
3568 unsigned int s_start
, s_end
;
3570 int jstop
= (int)png_pass_inc
[pass
];
3575 #ifdef PNG_READ_PACKSWAP_SUPPORTED
3576 if ((transformations
& PNG_PACKSWAP
) != 0)
3578 sshift
= ((row_info
->width
+ 7) & 0x07);
3579 dshift
= ((final_width
+ 7) & 0x07);
3588 sshift
= 7 - ((row_info
->width
+ 7) & 0x07);
3589 dshift
= 7 - ((final_width
+ 7) & 0x07);
3595 for (i
= 0; i
< row_info
->width
; i
++)
3597 v
= (png_byte
)((*sp
>> sshift
) & 0x01);
3598 for (j
= 0; j
< jstop
; j
++)
3600 unsigned int tmp
= *dp
& (0x7f7f >> (7 - dshift
));
3601 tmp
|= (unsigned int)(v
<< dshift
);
3602 *dp
= (png_byte
)(tmp
& 0xff);
3604 if (dshift
== s_end
)
3611 dshift
= (unsigned int)((int)dshift
+ s_inc
);
3614 if (sshift
== s_end
)
3621 sshift
= (unsigned int)((int)sshift
+ s_inc
);
3628 png_bytep sp
= row
+ (png_uint_32
)((row_info
->width
- 1) >> 2);
3629 png_bytep dp
= row
+ (png_uint_32
)((final_width
- 1) >> 2);
3630 unsigned int sshift
, dshift
;
3631 unsigned int s_start
, s_end
;
3633 int jstop
= (int)png_pass_inc
[pass
];
3636 #ifdef PNG_READ_PACKSWAP_SUPPORTED
3637 if ((transformations
& PNG_PACKSWAP
) != 0)
3639 sshift
= (((row_info
->width
+ 3) & 0x03) << 1);
3640 dshift
= (((final_width
+ 3) & 0x03) << 1);
3649 sshift
= ((3 - ((row_info
->width
+ 3) & 0x03)) << 1);
3650 dshift
= ((3 - ((final_width
+ 3) & 0x03)) << 1);
3656 for (i
= 0; i
< row_info
->width
; i
++)
3661 v
= (png_byte
)((*sp
>> sshift
) & 0x03);
3662 for (j
= 0; j
< jstop
; j
++)
3664 unsigned int tmp
= *dp
& (0x3f3f >> (6 - dshift
));
3665 tmp
|= (unsigned int)(v
<< dshift
);
3666 *dp
= (png_byte
)(tmp
& 0xff);
3668 if (dshift
== s_end
)
3675 dshift
= (unsigned int)((int)dshift
+ s_inc
);
3678 if (sshift
== s_end
)
3685 sshift
= (unsigned int)((int)sshift
+ s_inc
);
3692 png_bytep sp
= row
+ (png_size_t
)((row_info
->width
- 1) >> 1);
3693 png_bytep dp
= row
+ (png_size_t
)((final_width
- 1) >> 1);
3694 unsigned int sshift
, dshift
;
3695 unsigned int s_start
, s_end
;
3698 int jstop
= (int)png_pass_inc
[pass
];
3700 #ifdef PNG_READ_PACKSWAP_SUPPORTED
3701 if ((transformations
& PNG_PACKSWAP
) != 0)
3703 sshift
= (((row_info
->width
+ 1) & 0x01) << 2);
3704 dshift
= (((final_width
+ 1) & 0x01) << 2);
3713 sshift
= ((1 - ((row_info
->width
+ 1) & 0x01)) << 2);
3714 dshift
= ((1 - ((final_width
+ 1) & 0x01)) << 2);
3720 for (i
= 0; i
< row_info
->width
; i
++)
3722 png_byte v
= (png_byte
)((*sp
>> sshift
) & 0x0f);
3725 for (j
= 0; j
< jstop
; j
++)
3727 unsigned int tmp
= *dp
& (0xf0f >> (4 - dshift
));
3728 tmp
|= (unsigned int)(v
<< dshift
);
3729 *dp
= (png_byte
)(tmp
& 0xff);
3731 if (dshift
== s_end
)
3738 dshift
= (unsigned int)((int)dshift
+ s_inc
);
3741 if (sshift
== s_end
)
3748 sshift
= (unsigned int)((int)sshift
+ s_inc
);
3755 png_size_t pixel_bytes
= (row_info
->pixel_depth
>> 3);
3757 png_bytep sp
= row
+ (png_size_t
)(row_info
->width
- 1)
3760 png_bytep dp
= row
+ (png_size_t
)(final_width
- 1) * pixel_bytes
;
3762 int jstop
= (int)png_pass_inc
[pass
];
3765 for (i
= 0; i
< row_info
->width
; i
++)
3767 png_byte v
[8]; /* SAFE; pixel_depth does not exceed 64 */
3770 memcpy(v
, sp
, pixel_bytes
);
3772 for (j
= 0; j
< jstop
; j
++)
3774 memcpy(dp
, v
, pixel_bytes
);
3784 row_info
->width
= final_width
;
3785 row_info
->rowbytes
= PNG_ROWBYTES(row_info
->pixel_depth
, final_width
);
3787 #ifndef PNG_READ_PACKSWAP_SUPPORTED
3788 PNG_UNUSED(transformations
) /* Silence compiler warning */
3791 #endif /* READ_INTERLACING */
3794 png_read_filter_row_sub(png_row_infop row_info
, png_bytep row
,
3795 png_const_bytep prev_row
)
3798 png_size_t istop
= row_info
->rowbytes
;
3799 unsigned int bpp
= (row_info
->pixel_depth
+ 7) >> 3;
3800 png_bytep rp
= row
+ bpp
;
3802 PNG_UNUSED(prev_row
)
3804 for (i
= bpp
; i
< istop
; i
++)
3806 *rp
= (png_byte
)(((int)(*rp
) + (int)(*(rp
-bpp
))) & 0xff);
3812 png_read_filter_row_up(png_row_infop row_info
, png_bytep row
,
3813 png_const_bytep prev_row
)
3816 png_size_t istop
= row_info
->rowbytes
;
3818 png_const_bytep pp
= prev_row
;
3820 for (i
= 0; i
< istop
; i
++)
3822 *rp
= (png_byte
)(((int)(*rp
) + (int)(*pp
++)) & 0xff);
3828 png_read_filter_row_avg(png_row_infop row_info
, png_bytep row
,
3829 png_const_bytep prev_row
)
3833 png_const_bytep pp
= prev_row
;
3834 unsigned int bpp
= (row_info
->pixel_depth
+ 7) >> 3;
3835 png_size_t istop
= row_info
->rowbytes
- bpp
;
3837 for (i
= 0; i
< bpp
; i
++)
3839 *rp
= (png_byte
)(((int)(*rp
) +
3840 ((int)(*pp
++) / 2 )) & 0xff);
3845 for (i
= 0; i
< istop
; i
++)
3847 *rp
= (png_byte
)(((int)(*rp
) +
3848 (int)(*pp
++ + *(rp
-bpp
)) / 2 ) & 0xff);
3855 png_read_filter_row_paeth_1byte_pixel(png_row_infop row_info
, png_bytep row
,
3856 png_const_bytep prev_row
)
3858 png_bytep rp_end
= row
+ row_info
->rowbytes
;
3861 /* First pixel/byte */
3864 *row
++ = (png_byte
)a
;
3867 while (row
< rp_end
)
3869 int b
, pa
, pb
, pc
, p
;
3871 a
&= 0xff; /* From previous iteration or start */
3882 pa
= p
< 0 ? -p
: p
;
3883 pb
= pc
< 0 ? -pc
: pc
;
3884 pc
= (p
+ pc
) < 0 ? -(p
+ pc
) : p
+ pc
;
3887 /* Find the best predictor, the least of pa, pb, pc favoring the earlier
3888 * ones in the case of a tie.
3890 if (pb
< pa
) pa
= pb
, a
= b
;
3893 /* Calculate the current pixel in a, and move the previous row pixel to c
3894 * for the next time round the loop
3898 *row
++ = (png_byte
)a
;
3903 png_read_filter_row_paeth_multibyte_pixel(png_row_infop row_info
, png_bytep row
,
3904 png_const_bytep prev_row
)
3906 unsigned int bpp
= (row_info
->pixel_depth
+ 7) >> 3;
3907 png_bytep rp_end
= row
+ bpp
;
3909 /* Process the first pixel in the row completely (this is the same as 'up'
3910 * because there is only one candidate predictor for the first row).
3912 while (row
< rp_end
)
3914 int a
= *row
+ *prev_row
++;
3915 *row
++ = (png_byte
)a
;
3919 rp_end
= rp_end
+ (row_info
->rowbytes
- bpp
);
3921 while (row
< rp_end
)
3923 int a
, b
, c
, pa
, pb
, pc
, p
;
3925 c
= *(prev_row
- bpp
);
3937 pa
= p
< 0 ? -p
: p
;
3938 pb
= pc
< 0 ? -pc
: pc
;
3939 pc
= (p
+ pc
) < 0 ? -(p
+ pc
) : p
+ pc
;
3942 if (pb
< pa
) pa
= pb
, a
= b
;
3946 *row
++ = (png_byte
)a
;
3951 png_init_filter_functions(png_structrp pp
)
3952 /* This function is called once for every PNG image (except for PNG images
3953 * that only use PNG_FILTER_VALUE_NONE for all rows) to set the
3954 * implementations required to reverse the filtering of PNG rows. Reversing
3955 * the filter is the first transformation performed on the row data. It is
3956 * performed in place, therefore an implementation can be selected based on
3957 * the image pixel format. If the implementation depends on image width then
3958 * take care to ensure that it works correctly if the image is interlaced -
3959 * interlacing causes the actual row width to vary.
3962 unsigned int bpp
= (pp
->pixel_depth
+ 7) >> 3;
3964 pp
->read_filter
[PNG_FILTER_VALUE_SUB
-1] = png_read_filter_row_sub
;
3965 pp
->read_filter
[PNG_FILTER_VALUE_UP
-1] = png_read_filter_row_up
;
3966 pp
->read_filter
[PNG_FILTER_VALUE_AVG
-1] = png_read_filter_row_avg
;
3968 pp
->read_filter
[PNG_FILTER_VALUE_PAETH
-1] =
3969 png_read_filter_row_paeth_1byte_pixel
;
3971 pp
->read_filter
[PNG_FILTER_VALUE_PAETH
-1] =
3972 png_read_filter_row_paeth_multibyte_pixel
;
3974 #ifdef PNG_FILTER_OPTIMIZATIONS
3975 /* To use this define PNG_FILTER_OPTIMIZATIONS as the name of a function to
3976 * call to install hardware optimizations for the above functions; simply
3977 * replace whatever elements of the pp->read_filter[] array with a hardware
3978 * specific (or, for that matter, generic) optimization.
3980 * To see an example of this examine what configure.ac does when
3981 * --enable-arm-neon is specified on the command line.
3983 PNG_FILTER_OPTIMIZATIONS(pp
, bpp
);
3988 png_read_filter_row(png_structrp pp
, png_row_infop row_info
, png_bytep row
,
3989 png_const_bytep prev_row
, int filter
)
3991 /* OPTIMIZATION: DO NOT MODIFY THIS FUNCTION, instead #define
3992 * PNG_FILTER_OPTIMIZATIONS to a function that overrides the generic
3993 * implementations. See png_init_filter_functions above.
3995 if (filter
> PNG_FILTER_VALUE_NONE
&& filter
< PNG_FILTER_VALUE_LAST
)
3997 if (pp
->read_filter
[0] == NULL
)
3998 png_init_filter_functions(pp
);
4000 pp
->read_filter
[filter
-1](row_info
, row
, prev_row
);
4004 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
4006 png_read_IDAT_data(png_structrp png_ptr
, png_bytep output
,
4007 png_alloc_size_t avail_out
)
4009 /* Loop reading IDATs and decompressing the result into output[avail_out] */
4010 png_ptr
->zstream
.next_out
= output
;
4011 png_ptr
->zstream
.avail_out
= 0; /* safety: set below */
4019 png_byte tmpbuf
[PNG_INFLATE_BUF_SIZE
];
4021 if (png_ptr
->zstream
.avail_in
== 0)
4026 while (png_ptr
->idat_size
== 0)
4028 png_crc_finish(png_ptr
, 0);
4030 png_ptr
->idat_size
= png_read_chunk_header(png_ptr
);
4031 /* This is an error even in the 'check' case because the code just
4032 * consumed a non-IDAT header.
4034 if (png_ptr
->chunk_name
!= png_IDAT
)
4035 png_error(png_ptr
, "Not enough image data");
4038 avail_in
= png_ptr
->IDAT_read_size
;
4040 if (avail_in
> png_ptr
->idat_size
)
4041 avail_in
= (uInt
)png_ptr
->idat_size
;
4043 /* A PNG with a gradually increasing IDAT size will defeat this attempt
4044 * to minimize memory usage by causing lots of re-allocs, but
4045 * realistically doing IDAT_read_size re-allocs is not likely to be a
4048 buffer
= png_read_buffer(png_ptr
, avail_in
, 0/*error*/);
4050 png_crc_read(png_ptr
, buffer
, avail_in
);
4051 png_ptr
->idat_size
-= avail_in
;
4053 png_ptr
->zstream
.next_in
= buffer
;
4054 png_ptr
->zstream
.avail_in
= avail_in
;
4057 /* And set up the output side. */
4058 if (output
!= NULL
) /* standard read */
4060 uInt out
= ZLIB_IO_MAX
;
4062 if (out
> avail_out
)
4063 out
= (uInt
)avail_out
;
4066 png_ptr
->zstream
.avail_out
= out
;
4069 else /* after last row, checking for end */
4071 png_ptr
->zstream
.next_out
= tmpbuf
;
4072 png_ptr
->zstream
.avail_out
= (sizeof tmpbuf
);
4075 /* Use NO_FLUSH; this gives zlib the maximum opportunity to optimize the
4076 * process. If the LZ stream is truncated the sequential reader will
4077 * terminally damage the stream, above, by reading the chunk header of the
4078 * following chunk (it then exits with png_error).
4080 * TODO: deal more elegantly with truncated IDAT lists.
4082 ret
= PNG_INFLATE(png_ptr
, Z_NO_FLUSH
);
4084 /* Take the unconsumed output back. */
4086 avail_out
+= png_ptr
->zstream
.avail_out
;
4088 else /* avail_out counts the extra bytes */
4089 avail_out
+= (sizeof tmpbuf
) - png_ptr
->zstream
.avail_out
;
4091 png_ptr
->zstream
.avail_out
= 0;
4093 if (ret
== Z_STREAM_END
)
4095 /* Do this for safety; we won't read any more into this row. */
4096 png_ptr
->zstream
.next_out
= NULL
;
4098 png_ptr
->mode
|= PNG_AFTER_IDAT
;
4099 png_ptr
->flags
|= PNG_FLAG_ZSTREAM_ENDED
;
4101 if (png_ptr
->zstream
.avail_in
> 0 || png_ptr
->idat_size
> 0)
4102 png_chunk_benign_error(png_ptr
, "Extra compressed data");
4108 png_zstream_error(png_ptr
, ret
);
4111 png_chunk_error(png_ptr
, png_ptr
->zstream
.msg
);
4115 png_chunk_benign_error(png_ptr
, png_ptr
->zstream
.msg
);
4119 } while (avail_out
> 0);
4123 /* The stream ended before the image; this is the same as too few IDATs so
4124 * should be handled the same way.
4127 png_error(png_ptr
, "Not enough image data");
4129 else /* the deflate stream contained extra data */
4130 png_chunk_benign_error(png_ptr
, "Too much image data");
4135 png_read_finish_IDAT(png_structrp png_ptr
)
4137 /* We don't need any more data and the stream should have ended, however the
4138 * LZ end code may actually not have been processed. In this case we must
4139 * read it otherwise stray unread IDAT data or, more likely, an IDAT chunk
4140 * may still remain to be consumed.
4142 if ((png_ptr
->flags
& PNG_FLAG_ZSTREAM_ENDED
) == 0)
4144 /* The NULL causes png_read_IDAT_data to swallow any remaining bytes in
4145 * the compressed stream, but the stream may be damaged too, so even after
4146 * this call we may need to terminate the zstream ownership.
4148 png_read_IDAT_data(png_ptr
, NULL
, 0);
4149 png_ptr
->zstream
.next_out
= NULL
; /* safety */
4151 /* Now clear everything out for safety; the following may not have been
4154 if ((png_ptr
->flags
& PNG_FLAG_ZSTREAM_ENDED
) == 0)
4156 png_ptr
->mode
|= PNG_AFTER_IDAT
;
4157 png_ptr
->flags
|= PNG_FLAG_ZSTREAM_ENDED
;
4161 /* If the zstream has not been released do it now *and* terminate the reading
4162 * of the final IDAT chunk.
4164 if (png_ptr
->zowner
== png_IDAT
)
4166 /* Always do this; the pointers otherwise point into the read buffer. */
4167 png_ptr
->zstream
.next_in
= NULL
;
4168 png_ptr
->zstream
.avail_in
= 0;
4170 /* Now we no longer own the zstream. */
4171 png_ptr
->zowner
= 0;
4173 /* The slightly weird semantics of the sequential IDAT reading is that we
4174 * are always in or at the end of an IDAT chunk, so we always need to do a
4175 * crc_finish here. If idat_size is non-zero we also need to read the
4176 * spurious bytes at the end of the chunk now.
4178 (void)png_crc_finish(png_ptr
, png_ptr
->idat_size
);
4183 png_read_finish_row(png_structrp png_ptr
)
4185 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
4187 /* Start of interlace block */
4188 static PNG_CONST png_byte png_pass_start
[7] = {0, 4, 0, 2, 0, 1, 0};
4190 /* Offset to next interlace block */
4191 static PNG_CONST png_byte png_pass_inc
[7] = {8, 8, 4, 4, 2, 2, 1};
4193 /* Start of interlace block in the y direction */
4194 static PNG_CONST png_byte png_pass_ystart
[7] = {0, 0, 4, 0, 2, 0, 1};
4196 /* Offset to next interlace block in the y direction */
4197 static PNG_CONST png_byte png_pass_yinc
[7] = {8, 8, 8, 4, 4, 2, 2};
4199 png_debug(1, "in png_read_finish_row");
4200 png_ptr
->row_number
++;
4201 if (png_ptr
->row_number
< png_ptr
->num_rows
)
4204 if (png_ptr
->interlaced
!= 0)
4206 png_ptr
->row_number
= 0;
4208 /* TO DO: don't do this if prev_row isn't needed (requires
4209 * read-ahead of the next row's filter byte.
4211 memset(png_ptr
->prev_row
, 0, png_ptr
->rowbytes
+ 1);
4217 if (png_ptr
->pass
>= 7)
4220 png_ptr
->iwidth
= (png_ptr
->width
+
4221 png_pass_inc
[png_ptr
->pass
] - 1 -
4222 png_pass_start
[png_ptr
->pass
]) /
4223 png_pass_inc
[png_ptr
->pass
];
4225 if ((png_ptr
->transformations
& PNG_INTERLACE
) == 0)
4227 png_ptr
->num_rows
= (png_ptr
->height
+
4228 png_pass_yinc
[png_ptr
->pass
] - 1 -
4229 png_pass_ystart
[png_ptr
->pass
]) /
4230 png_pass_yinc
[png_ptr
->pass
];
4233 else /* if (png_ptr->transformations & PNG_INTERLACE) */
4234 break; /* libpng deinterlacing sees every row */
4236 } while (png_ptr
->num_rows
== 0 || png_ptr
->iwidth
== 0);
4238 if (png_ptr
->pass
< 7)
4242 /* Here after at the end of the last row of the last pass. */
4243 png_read_finish_IDAT(png_ptr
);
4245 #endif /* SEQUENTIAL_READ */
4248 png_read_start_row(png_structrp png_ptr
)
4250 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
4252 /* Start of interlace block */
4253 static PNG_CONST png_byte png_pass_start
[7] = {0, 4, 0, 2, 0, 1, 0};
4255 /* Offset to next interlace block */
4256 static PNG_CONST png_byte png_pass_inc
[7] = {8, 8, 4, 4, 2, 2, 1};
4258 /* Start of interlace block in the y direction */
4259 static PNG_CONST png_byte png_pass_ystart
[7] = {0, 0, 4, 0, 2, 0, 1};
4261 /* Offset to next interlace block in the y direction */
4262 static PNG_CONST png_byte png_pass_yinc
[7] = {8, 8, 8, 4, 4, 2, 2};
4264 unsigned int max_pixel_depth
;
4265 png_size_t row_bytes
;
4267 png_debug(1, "in png_read_start_row");
4269 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
4270 png_init_read_transformations(png_ptr
);
4272 if (png_ptr
->interlaced
!= 0)
4274 if ((png_ptr
->transformations
& PNG_INTERLACE
) == 0)
4275 png_ptr
->num_rows
= (png_ptr
->height
+ png_pass_yinc
[0] - 1 -
4276 png_pass_ystart
[0]) / png_pass_yinc
[0];
4279 png_ptr
->num_rows
= png_ptr
->height
;
4281 png_ptr
->iwidth
= (png_ptr
->width
+
4282 png_pass_inc
[png_ptr
->pass
] - 1 -
4283 png_pass_start
[png_ptr
->pass
]) /
4284 png_pass_inc
[png_ptr
->pass
];
4289 png_ptr
->num_rows
= png_ptr
->height
;
4290 png_ptr
->iwidth
= png_ptr
->width
;
4293 max_pixel_depth
= (unsigned int)png_ptr
->pixel_depth
;
4295 /* WARNING: * png_read_transform_info (pngrtran.c) performs a simpler set of
4296 * calculations to calculate the final pixel depth, then
4297 * png_do_read_transforms actually does the transforms. This means that the
4298 * code which effectively calculates this value is actually repeated in three
4299 * separate places. They must all match. Innocent changes to the order of
4300 * transformations can and will break libpng in a way that causes memory
4305 #ifdef PNG_READ_PACK_SUPPORTED
4306 if ((png_ptr
->transformations
& PNG_PACK
) != 0 && png_ptr
->bit_depth
< 8)
4307 max_pixel_depth
= 8;
4310 #ifdef PNG_READ_EXPAND_SUPPORTED
4311 if ((png_ptr
->transformations
& PNG_EXPAND
) != 0)
4313 if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
4315 if (png_ptr
->num_trans
!= 0)
4316 max_pixel_depth
= 32;
4319 max_pixel_depth
= 24;
4322 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_GRAY
)
4324 if (max_pixel_depth
< 8)
4325 max_pixel_depth
= 8;
4327 if (png_ptr
->num_trans
!= 0)
4328 max_pixel_depth
*= 2;
4331 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_RGB
)
4333 if (png_ptr
->num_trans
!= 0)
4335 max_pixel_depth
*= 4;
4336 max_pixel_depth
/= 3;
4342 #ifdef PNG_READ_EXPAND_16_SUPPORTED
4343 if ((png_ptr
->transformations
& PNG_EXPAND_16
) != 0)
4345 # ifdef PNG_READ_EXPAND_SUPPORTED
4346 /* In fact it is an error if it isn't supported, but checking is
4349 if ((png_ptr
->transformations
& PNG_EXPAND
) != 0)
4351 if (png_ptr
->bit_depth
< 16)
4352 max_pixel_depth
*= 2;
4356 png_ptr
->transformations
&= ~PNG_EXPAND_16
;
4360 #ifdef PNG_READ_FILLER_SUPPORTED
4361 if ((png_ptr
->transformations
& (PNG_FILLER
)) != 0)
4363 if (png_ptr
->color_type
== PNG_COLOR_TYPE_GRAY
)
4365 if (max_pixel_depth
<= 8)
4366 max_pixel_depth
= 16;
4369 max_pixel_depth
= 32;
4372 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_RGB
||
4373 png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
4375 if (max_pixel_depth
<= 32)
4376 max_pixel_depth
= 32;
4379 max_pixel_depth
= 64;
4384 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
4385 if ((png_ptr
->transformations
& PNG_GRAY_TO_RGB
) != 0)
4388 #ifdef PNG_READ_EXPAND_SUPPORTED
4389 (png_ptr
->num_trans
!= 0 &&
4390 (png_ptr
->transformations
& PNG_EXPAND
) != 0) ||
4392 #ifdef PNG_READ_FILLER_SUPPORTED
4393 (png_ptr
->transformations
& (PNG_FILLER
)) != 0 ||
4395 png_ptr
->color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
4397 if (max_pixel_depth
<= 16)
4398 max_pixel_depth
= 32;
4401 max_pixel_depth
= 64;
4406 if (max_pixel_depth
<= 8)
4408 if (png_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
4409 max_pixel_depth
= 32;
4412 max_pixel_depth
= 24;
4415 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
4416 max_pixel_depth
= 64;
4419 max_pixel_depth
= 48;
4424 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
4425 defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
4426 if ((png_ptr
->transformations
& PNG_USER_TRANSFORM
) != 0)
4428 unsigned int user_pixel_depth
= png_ptr
->user_transform_depth
*
4429 png_ptr
->user_transform_channels
;
4431 if (user_pixel_depth
> max_pixel_depth
)
4432 max_pixel_depth
= user_pixel_depth
;
4436 /* This value is stored in png_struct and double checked in the row read
4439 png_ptr
->maximum_pixel_depth
= (png_byte
)max_pixel_depth
;
4440 png_ptr
->transformed_pixel_depth
= 0; /* calculated on demand */
4442 /* Align the width on the next larger 8 pixels. Mainly used
4445 row_bytes
= ((png_ptr
->width
+ 7) & ~((png_uint_32
)7));
4446 /* Calculate the maximum bytes needed, adding a byte and a pixel
4449 row_bytes
= PNG_ROWBYTES(max_pixel_depth
, row_bytes
) +
4450 1 + ((max_pixel_depth
+ 7) >> 3U);
4452 #ifdef PNG_MAX_MALLOC_64K
4453 if (row_bytes
> (png_uint_32
)65536L)
4454 png_error(png_ptr
, "This image requires a row greater than 64KB");
4457 if (row_bytes
+ 48 > png_ptr
->old_big_row_buf_size
)
4459 png_free(png_ptr
, png_ptr
->big_row_buf
);
4460 png_free(png_ptr
, png_ptr
->big_prev_row
);
4462 if (png_ptr
->interlaced
!= 0)
4463 png_ptr
->big_row_buf
= (png_bytep
)png_calloc(png_ptr
,
4467 png_ptr
->big_row_buf
= (png_bytep
)png_malloc(png_ptr
, row_bytes
+ 48);
4469 png_ptr
->big_prev_row
= (png_bytep
)png_malloc(png_ptr
, row_bytes
+ 48);
4471 #ifdef PNG_ALIGNED_MEMORY_SUPPORTED
4472 /* Use 16-byte aligned memory for row_buf with at least 16 bytes
4473 * of padding before and after row_buf; treat prev_row similarly.
4474 * NOTE: the alignment is to the start of the pixels, one beyond the start
4475 * of the buffer, because of the filter byte. Prior to libpng 1.5.6 this
4476 * was incorrect; the filter byte was aligned, which had the exact
4477 * opposite effect of that intended.
4480 png_bytep temp
= png_ptr
->big_row_buf
+ 32;
4481 int extra
= (int)((temp
- (png_bytep
)0) & 0x0f);
4482 png_ptr
->row_buf
= temp
- extra
- 1/*filter byte*/;
4484 temp
= png_ptr
->big_prev_row
+ 32;
4485 extra
= (int)((temp
- (png_bytep
)0) & 0x0f);
4486 png_ptr
->prev_row
= temp
- extra
- 1/*filter byte*/;
4490 /* Use 31 bytes of padding before and 17 bytes after row_buf. */
4491 png_ptr
->row_buf
= png_ptr
->big_row_buf
+ 31;
4492 png_ptr
->prev_row
= png_ptr
->big_prev_row
+ 31;
4494 png_ptr
->old_big_row_buf_size
= row_bytes
+ 48;
4497 #ifdef PNG_MAX_MALLOC_64K
4498 if (png_ptr
->rowbytes
> 65535)
4499 png_error(png_ptr
, "This image requires a row greater than 64KB");
4502 if (png_ptr
->rowbytes
> (PNG_SIZE_MAX
- 1))
4503 png_error(png_ptr
, "Row has too many bytes to allocate in memory");
4505 memset(png_ptr
->prev_row
, 0, png_ptr
->rowbytes
+ 1);
4507 png_debug1(3, "width = %u,", png_ptr
->width
);
4508 png_debug1(3, "height = %u,", png_ptr
->height
);
4509 png_debug1(3, "iwidth = %u,", png_ptr
->iwidth
);
4510 png_debug1(3, "num_rows = %u,", png_ptr
->num_rows
);
4511 png_debug1(3, "rowbytes = %lu,", (unsigned long)png_ptr
->rowbytes
);
4512 png_debug1(3, "irowbytes = %lu",
4513 (unsigned long)PNG_ROWBYTES(png_ptr
->pixel_depth
, png_ptr
->iwidth
) + 1);
4515 /* The sequential reader needs a buffer for IDAT, but the progressive reader
4516 * does not, so free the read buffer now regardless; the sequential reader
4517 * reallocates it on demand.
4519 if (png_ptr
->read_buffer
!= NULL
)
4521 png_bytep buffer
= png_ptr
->read_buffer
;
4523 png_ptr
->read_buffer_size
= 0;
4524 png_ptr
->read_buffer
= NULL
;
4525 png_free(png_ptr
, buffer
);
4528 /* Finally claim the zstream for the inflate of the IDAT data, use the bits
4529 * value from the stream (note that this will result in a fatal error if the
4530 * IDAT stream has a bogus deflate header window_bits value, but this should
4531 * not be happening any longer!)
4533 if (png_inflate_claim(png_ptr
, png_IDAT
) != Z_OK
)
4534 png_error(png_ptr
, png_ptr
->zstream
.msg
);
4536 png_ptr
->flags
|= PNG_FLAG_ROW_INIT
;