2 /* pngrutil.c - utilities to read a PNG file
4 * Last changed in libpng 1.6.2 [April 25, 2013]
5 * Copyright (c) 1998-2013 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
21 #define png_strtod(p,a,b) strtod(a,b)
24 png_get_uint_31(png_const_structrp png_ptr
, png_const_bytep buf
)
26 png_uint_32 uval
= png_get_uint_32(buf
);
28 if (uval
> PNG_UINT_31_MAX
)
29 png_error(png_ptr
, "PNG unsigned integer out of range");
34 #if defined(PNG_READ_gAMA_SUPPORTED) || defined(PNG_READ_cHRM_SUPPORTED)
35 /* The following is a variation on the above for use with the fixed
36 * point values used for gAMA and cHRM. Instead of png_error it
37 * issues a warning and returns (-1) - an invalid value because both
38 * gAMA and cHRM use *unsigned* integers for fixed point values.
40 #define PNG_FIXED_ERROR (-1)
42 static png_fixed_point
/* PRIVATE */
43 png_get_fixed_point(png_structrp png_ptr
, png_const_bytep buf
)
45 png_uint_32 uval
= png_get_uint_32(buf
);
47 if (uval
<= PNG_UINT_31_MAX
)
48 return (png_fixed_point
)uval
; /* known to be in range */
50 /* The caller can turn off the warning by passing NULL. */
52 png_warning(png_ptr
, "PNG fixed point integer out of range");
54 return PNG_FIXED_ERROR
;
58 #ifdef PNG_READ_INT_FUNCTIONS_SUPPORTED
59 /* NOTE: the read macros will obscure these definitions, so that if
60 * PNG_USE_READ_MACROS is set the library will not use them internally,
61 * but the APIs will still be available externally.
63 * The parentheses around "PNGAPI function_name" in the following three
64 * functions are necessary because they allow the macros to co-exist with
65 * these (unused but exported) functions.
68 /* Grab an unsigned 32-bit integer from a buffer in big-endian format. */
70 png_get_uint_32
)(png_const_bytep buf
)
73 ((png_uint_32
)(*(buf
)) << 24) +
74 ((png_uint_32
)(*(buf
+ 1)) << 16) +
75 ((png_uint_32
)(*(buf
+ 2)) << 8) +
76 ((png_uint_32
)(*(buf
+ 3)) ) ;
81 /* Grab a signed 32-bit integer from a buffer in big-endian format. The
82 * data is stored in the PNG file in two's complement format and there
83 * is no guarantee that a 'png_int_32' is exactly 32 bits, therefore
84 * the following code does a two's complement to native conversion.
87 png_get_int_32
)(png_const_bytep buf
)
89 png_uint_32 uval
= png_get_uint_32(buf
);
90 if ((uval
& 0x80000000) == 0) /* non-negative */
93 uval
= (uval
^ 0xffffffff) + 1; /* 2's complement: -x = ~x+1 */
94 return -(png_int_32
)uval
;
97 /* Grab an unsigned 16-bit integer from a buffer in big-endian format. */
99 png_get_uint_16
)(png_const_bytep buf
)
101 /* ANSI-C requires an int value to accomodate at least 16 bits so this
102 * works and allows the compiler not to worry about possible narrowing
103 * on 32 bit systems. (Pre-ANSI systems did not make integers smaller
104 * than 16 bits either.)
107 ((unsigned int)(*buf
) << 8) +
108 ((unsigned int)(*(buf
+ 1)));
110 return (png_uint_16
)val
;
113 #endif /* PNG_READ_INT_FUNCTIONS_SUPPORTED */
115 /* Read and check the PNG file signature */
117 png_read_sig(png_structrp png_ptr
, png_inforp info_ptr
)
119 png_size_t num_checked
, num_to_check
;
121 /* Exit if the user application does not expect a signature. */
122 if (png_ptr
->sig_bytes
>= 8)
125 num_checked
= png_ptr
->sig_bytes
;
126 num_to_check
= 8 - num_checked
;
128 #ifdef PNG_IO_STATE_SUPPORTED
129 png_ptr
->io_state
= PNG_IO_READING
| PNG_IO_SIGNATURE
;
132 /* The signature must be serialized in a single I/O call. */
133 png_read_data(png_ptr
, &(info_ptr
->signature
[num_checked
]), num_to_check
);
134 png_ptr
->sig_bytes
= 8;
136 if (png_sig_cmp(info_ptr
->signature
, num_checked
, num_to_check
))
138 if (num_checked
< 4 &&
139 png_sig_cmp(info_ptr
->signature
, num_checked
, num_to_check
- 4))
140 png_error(png_ptr
, "Not a PNG file");
142 png_error(png_ptr
, "PNG file corrupted by ASCII conversion");
145 png_ptr
->mode
|= PNG_HAVE_PNG_SIGNATURE
;
148 /* Read the chunk header (length + type name).
149 * Put the type name into png_ptr->chunk_name, and return the length.
151 png_uint_32
/* PRIVATE */
152 png_read_chunk_header(png_structrp png_ptr
)
157 #ifdef PNG_IO_STATE_SUPPORTED
158 png_ptr
->io_state
= PNG_IO_READING
| PNG_IO_CHUNK_HDR
;
161 /* Read the length and the chunk name.
162 * This must be performed in a single I/O call.
164 png_read_data(png_ptr
, buf
, 8);
165 length
= png_get_uint_31(png_ptr
, buf
);
167 /* Put the chunk name into png_ptr->chunk_name. */
168 png_ptr
->chunk_name
= PNG_CHUNK_FROM_STRING(buf
+4);
170 png_debug2(0, "Reading %lx chunk, length = %lu",
171 (unsigned long)png_ptr
->chunk_name
, (unsigned long)length
);
173 /* Reset the crc and run it over the chunk name. */
174 png_reset_crc(png_ptr
);
175 png_calculate_crc(png_ptr
, buf
+ 4, 4);
177 /* Check to see if chunk name is valid. */
178 png_check_chunk_name(png_ptr
, png_ptr
->chunk_name
);
180 #ifdef PNG_IO_STATE_SUPPORTED
181 png_ptr
->io_state
= PNG_IO_READING
| PNG_IO_CHUNK_DATA
;
187 /* Read data, and (optionally) run it through the CRC. */
189 png_crc_read(png_structrp png_ptr
, png_bytep buf
, png_uint_32 length
)
194 png_read_data(png_ptr
, buf
, length
);
195 png_calculate_crc(png_ptr
, buf
, length
);
198 /* Optionally skip data and then check the CRC. Depending on whether we
199 * are reading an ancillary or critical chunk, and how the program has set
200 * things up, we may calculate the CRC on the data and print a message.
201 * Returns '1' if there was a CRC error, '0' otherwise.
204 png_crc_finish(png_structrp png_ptr
, png_uint_32 skip
)
206 /* The size of the local buffer for inflate is a good guess as to a
207 * reasonable size to use for buffering reads from the application.
212 png_byte tmpbuf
[PNG_INFLATE_BUF_SIZE
];
214 len
= (sizeof tmpbuf
);
219 png_crc_read(png_ptr
, tmpbuf
, len
);
222 if (png_crc_error(png_ptr
))
224 if (PNG_CHUNK_ANCILLARY(png_ptr
->chunk_name
) ?
225 !(png_ptr
->flags
& PNG_FLAG_CRC_ANCILLARY_NOWARN
) :
226 (png_ptr
->flags
& PNG_FLAG_CRC_CRITICAL_USE
))
228 png_chunk_warning(png_ptr
, "CRC error");
233 png_chunk_benign_error(png_ptr
, "CRC error");
243 /* Compare the CRC stored in the PNG file with that calculated by libpng from
244 * the data it has read thus far.
247 png_crc_error(png_structrp png_ptr
)
249 png_byte crc_bytes
[4];
253 if (PNG_CHUNK_ANCILLARY(png_ptr
->chunk_name
))
255 if ((png_ptr
->flags
& PNG_FLAG_CRC_ANCILLARY_MASK
) ==
256 (PNG_FLAG_CRC_ANCILLARY_USE
| PNG_FLAG_CRC_ANCILLARY_NOWARN
))
262 if (png_ptr
->flags
& PNG_FLAG_CRC_CRITICAL_IGNORE
)
266 #ifdef PNG_IO_STATE_SUPPORTED
267 png_ptr
->io_state
= PNG_IO_READING
| PNG_IO_CHUNK_CRC
;
270 /* The chunk CRC must be serialized in a single I/O call. */
271 png_read_data(png_ptr
, crc_bytes
, 4);
275 crc
= png_get_uint_32(crc_bytes
);
276 return ((int)(crc
!= png_ptr
->crc
));
283 /* Manage the read buffer; this simply reallocates the buffer if it is not small
284 * enough (or if it is not allocated). The routine returns a pointer to the
285 * buffer; if an error occurs and 'warn' is set the routine returns NULL, else
286 * it will call png_error (via png_malloc) on failure. (warn == 2 means
290 png_read_buffer(png_structrp png_ptr
, png_alloc_size_t new_size
, int warn
)
292 png_bytep buffer
= png_ptr
->read_buffer
;
294 if (buffer
!= NULL
&& new_size
> png_ptr
->read_buffer_size
)
296 png_ptr
->read_buffer
= NULL
;
297 png_ptr
->read_buffer
= NULL
;
298 png_ptr
->read_buffer_size
= 0;
299 png_free(png_ptr
, buffer
);
305 buffer
= png_voidcast(png_bytep
, png_malloc_base(png_ptr
, new_size
));
309 png_ptr
->read_buffer
= buffer
;
310 png_ptr
->read_buffer_size
= new_size
;
313 else if (warn
< 2) /* else silent */
315 #ifdef PNG_WARNINGS_SUPPORTED
317 png_chunk_warning(png_ptr
, "insufficient memory to read chunk");
321 #ifdef PNG_ERROR_TEXT_SUPPORTED
322 png_chunk_error(png_ptr
, "insufficient memory to read chunk");
331 /* png_inflate_claim: claim the zstream for some nefarious purpose that involves
332 * decompression. Returns Z_OK on success, else a zlib error code. It checks
333 * the owner but, in final release builds, just issues a warning if some other
334 * chunk apparently owns the stream. Prior to release it does a png_error.
337 png_inflate_claim(png_structrp png_ptr
, png_uint_32 owner
, int window_bits
)
339 if (png_ptr
->zowner
!= 0)
343 PNG_STRING_FROM_CHUNK(msg
, png_ptr
->zowner
);
344 /* So the message that results is "<chunk> using zstream"; this is an
345 * internal error, but is very useful for debugging. i18n requirements
348 (void)png_safecat(msg
, (sizeof msg
), 4, " using zstream");
349 # if PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC
350 png_chunk_warning(png_ptr
, msg
);
353 png_chunk_error(png_ptr
, msg
);
357 /* Implementation note: unlike 'png_deflate_claim' this internal function
358 * does not take the size of the data as an argument. Some efficiency could
359 * be gained by using this when it is known *if* the zlib stream itself does
360 * not record the number; however, this is an illusion: the original writer
361 * of the PNG may have selected a lower window size, and we really must
362 * follow that because, for systems with with limited capabilities, we
363 * would otherwise reject the application's attempts to use a smaller window
364 * size (zlib doesn't have an interface to say "this or lower"!).
366 * inflateReset2 was added to zlib 1.2.4; before this the window could not be
367 * reset, therefore it is necessary to always allocate the maximum window
368 * size with earlier zlibs just in case later compressed chunks need it.
371 int ret
; /* zlib return code */
373 /* Set this for safety, just in case the previous owner left pointers to
374 * memory allocations.
376 png_ptr
->zstream
.next_in
= NULL
;
377 png_ptr
->zstream
.avail_in
= 0;
378 png_ptr
->zstream
.next_out
= NULL
;
379 png_ptr
->zstream
.avail_out
= 0;
381 if (png_ptr
->flags
& PNG_FLAG_ZSTREAM_INITIALIZED
)
383 # if ZLIB_VERNUM < 0x1240
384 PNG_UNUSED(window_bits
)
385 ret
= inflateReset(&png_ptr
->zstream
);
387 ret
= inflateReset2(&png_ptr
->zstream
, window_bits
);
393 # if ZLIB_VERNUM < 0x1240
394 ret
= inflateInit(&png_ptr
->zstream
);
396 ret
= inflateInit2(&png_ptr
->zstream
, window_bits
);
400 png_ptr
->flags
|= PNG_FLAG_ZSTREAM_INITIALIZED
;
404 png_ptr
->zowner
= owner
;
407 png_zstream_error(png_ptr
, ret
);
413 #ifdef PNG_READ_COMPRESSED_TEXT_SUPPORTED
414 /* png_inflate now returns zlib error codes including Z_OK and Z_STREAM_END to
415 * allow the caller to do multiple calls if required. If the 'finish' flag is
416 * set Z_FINISH will be passed to the final inflate() call and Z_STREAM_END must
417 * be returned or there has been a problem, otherwise Z_SYNC_FLUSH is used and
418 * Z_OK or Z_STREAM_END will be returned on success.
420 * The input and output sizes are updated to the actual amounts of data consumed
421 * or written, not the amount available (as in a z_stream). The data pointers
422 * are not changed, so the next input is (data+input_size) and the next
423 * available output is (output+output_size).
426 png_inflate(png_structrp png_ptr
, png_uint_32 owner
, int finish
,
427 /* INPUT: */ png_const_bytep input
, png_uint_32p input_size_ptr
,
428 /* OUTPUT: */ png_bytep output
, png_alloc_size_t
*output_size_ptr
)
430 if (png_ptr
->zowner
== owner
) /* Else not claimed */
433 png_alloc_size_t avail_out
= *output_size_ptr
;
434 png_uint_32 avail_in
= *input_size_ptr
;
436 /* zlib can't necessarily handle more than 65535 bytes at once (i.e. it
437 * can't even necessarily handle 65536 bytes) because the type uInt is
438 * "16 bits or more". Consequently it is necessary to chunk the input to
439 * zlib. This code uses ZLIB_IO_MAX, from pngpriv.h, as the maximum (the
440 * maximum value that can be stored in a uInt.) It is possible to set
441 * ZLIB_IO_MAX to a lower value in pngpriv.h and this may sometimes have
442 * a performance advantage, because it reduces the amount of data accessed
443 * at each step and that may give the OS more time to page it in.
445 png_ptr
->zstream
.next_in
= PNGZ_INPUT_CAST(input
);
446 /* avail_in and avail_out are set below from 'size' */
447 png_ptr
->zstream
.avail_in
= 0;
448 png_ptr
->zstream
.avail_out
= 0;
450 /* Read directly into the output if it is available (this is set to
451 * a local buffer below if output is NULL).
454 png_ptr
->zstream
.next_out
= output
;
459 Byte local_buffer
[PNG_INFLATE_BUF_SIZE
];
461 /* zlib INPUT BUFFER */
462 /* The setting of 'avail_in' used to be outside the loop; by setting it
463 * inside it is possible to chunk the input to zlib and simply rely on
464 * zlib to advance the 'next_in' pointer. This allows arbitrary
465 * amounts of data to be passed through zlib at the unavoidable cost of
466 * requiring a window save (memcpy of up to 32768 output bytes)
467 * every ZLIB_IO_MAX input bytes.
469 avail_in
+= png_ptr
->zstream
.avail_in
; /* not consumed last time */
473 if (avail_in
< avail
)
474 avail
= (uInt
)avail_in
; /* safe: < than ZLIB_IO_MAX */
477 png_ptr
->zstream
.avail_in
= avail
;
479 /* zlib OUTPUT BUFFER */
480 avail_out
+= png_ptr
->zstream
.avail_out
; /* not written last time */
482 avail
= ZLIB_IO_MAX
; /* maximum zlib can process */
486 /* Reset the output buffer each time round if output is NULL and
487 * make available the full buffer, up to 'remaining_space'
489 png_ptr
->zstream
.next_out
= local_buffer
;
490 if ((sizeof local_buffer
) < avail
)
491 avail
= (sizeof local_buffer
);
494 if (avail_out
< avail
)
495 avail
= (uInt
)avail_out
; /* safe: < ZLIB_IO_MAX */
497 png_ptr
->zstream
.avail_out
= avail
;
500 /* zlib inflate call */
501 /* In fact 'avail_out' may be 0 at this point, that happens at the end
502 * of the read when the final LZ end code was not passed at the end of
503 * the previous chunk of input data. Tell zlib if we have reached the
504 * end of the output buffer.
506 ret
= inflate(&png_ptr
->zstream
, avail_out
> 0 ? Z_NO_FLUSH
:
507 (finish
? Z_FINISH
: Z_SYNC_FLUSH
));
508 } while (ret
== Z_OK
);
510 /* For safety kill the local buffer pointer now */
512 png_ptr
->zstream
.next_out
= NULL
;
514 /* Claw back the 'size' and 'remaining_space' byte counts. */
515 avail_in
+= png_ptr
->zstream
.avail_in
;
516 avail_out
+= png_ptr
->zstream
.avail_out
;
518 /* Update the input and output sizes; the updated values are the amount
519 * consumed or written, effectively the inverse of what zlib uses.
522 *output_size_ptr
-= avail_out
;
525 *input_size_ptr
-= avail_in
;
527 /* Ensure png_ptr->zstream.msg is set (even in the success case!) */
528 png_zstream_error(png_ptr
, ret
);
534 /* This is a bad internal error. The recovery assigns to the zstream msg
535 * pointer, which is not owned by the caller, but this is safe; it's only
538 png_ptr
->zstream
.msg
= PNGZ_MSG_CAST("zstream unclaimed");
539 return Z_STREAM_ERROR
;
544 * Decompress trailing data in a chunk. The assumption is that read_buffer
545 * points at an allocated area holding the contents of a chunk with a
546 * trailing compressed part. What we get back is an allocated area
547 * holding the original prefix part and an uncompressed version of the
548 * trailing part (the malloc area passed in is freed).
551 png_decompress_chunk(png_structrp png_ptr
,
552 png_uint_32 chunklength
, png_uint_32 prefix_size
,
553 png_alloc_size_t
*newlength
/* must be initialized to the maximum! */,
554 int terminate
/*add a '\0' to the end of the uncompressed data*/)
556 /* TODO: implement different limits for different types of chunk.
558 * The caller supplies *newlength set to the maximum length of the
559 * uncompressed data, but this routine allocates space for the prefix and
560 * maybe a '\0' terminator too. We have to assume that 'prefix_size' is
561 * limited only by the maximum chunk size.
563 png_alloc_size_t limit
= PNG_SIZE_MAX
;
565 # ifdef PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED
566 if (png_ptr
->user_chunk_malloc_max
> 0 &&
567 png_ptr
->user_chunk_malloc_max
< limit
)
568 limit
= png_ptr
->user_chunk_malloc_max
;
569 # elif PNG_USER_CHUNK_MALLOC_MAX > 0
570 if (PNG_USER_CHUNK_MALLOC_MAX
< limit
)
571 limit
= PNG_USER_CHUNK_MALLOC_MAX
;
574 if (limit
>= prefix_size
+ (terminate
!= 0))
578 limit
-= prefix_size
+ (terminate
!= 0);
580 if (limit
< *newlength
)
583 /* Now try to claim the stream; the 'warn' setting causes zlib to be told
584 * to use the maximum window size during inflate; this hides errors in the
585 * deflate header window bits value which is used if '0' is passed. In
586 * fact this only has an effect with zlib versions 1.2.4 and later - see
587 * the comments in png_inflate_claim above.
589 ret
= png_inflate_claim(png_ptr
, png_ptr
->chunk_name
,
590 png_ptr
->flags
& PNG_FLAG_BENIGN_ERRORS_WARN
? 15 : 0);
594 png_uint_32 lzsize
= chunklength
- prefix_size
;
596 ret
= png_inflate(png_ptr
, png_ptr
->chunk_name
, 1/*finish*/,
597 /* input: */ png_ptr
->read_buffer
+ prefix_size
, &lzsize
,
598 /* output: */ NULL
, newlength
);
600 if (ret
== Z_STREAM_END
)
602 /* Use 'inflateReset' here, not 'inflateReset2' because this
603 * preserves the previously decided window size (otherwise it would
604 * be necessary to store the previous window size.) In practice
605 * this doesn't matter anyway, because png_inflate will call inflate
606 * with Z_FINISH in almost all cases, so the window will not be
609 if (inflateReset(&png_ptr
->zstream
) == Z_OK
)
611 /* Because of the limit checks above we know that the new,
612 * expanded, size will fit in a size_t (let alone an
613 * png_alloc_size_t). Use png_malloc_base here to avoid an
616 png_alloc_size_t new_size
= *newlength
;
617 png_alloc_size_t buffer_size
= prefix_size
+ new_size
+
619 png_bytep text
= png_voidcast(png_bytep
, png_malloc_base(png_ptr
,
624 ret
= png_inflate(png_ptr
, png_ptr
->chunk_name
, 1/*finish*/,
625 png_ptr
->read_buffer
+ prefix_size
, &lzsize
,
626 text
+ prefix_size
, newlength
);
628 if (ret
== Z_STREAM_END
)
630 if (new_size
== *newlength
)
633 text
[prefix_size
+ *newlength
] = 0;
636 memcpy(text
, png_ptr
->read_buffer
, prefix_size
);
639 png_bytep old_ptr
= png_ptr
->read_buffer
;
641 png_ptr
->read_buffer
= text
;
642 png_ptr
->read_buffer_size
= buffer_size
;
643 text
= old_ptr
; /* freed below */
649 /* The size changed on the second read, there can be no
650 * guarantee that anything is correct at this point.
651 * The 'msg' pointer has been set to "unexpected end of
652 * LZ stream", which is fine, but return an error code
653 * that the caller won't accept.
655 ret
= PNG_UNEXPECTED_ZLIB_RETURN
;
659 else if (ret
== Z_OK
)
660 ret
= PNG_UNEXPECTED_ZLIB_RETURN
; /* for safety */
662 /* Free the text pointer (this is the old read_buffer on
665 png_free(png_ptr
, text
);
667 /* This really is very benign, but it's still an error because
668 * the extra space may otherwise be used as a Trojan Horse.
670 if (ret
== Z_STREAM_END
&&
671 chunklength
- prefix_size
!= lzsize
)
672 png_chunk_benign_error(png_ptr
, "extra compressed data");
677 /* Out of memory allocating the buffer */
679 png_zstream_error(png_ptr
, Z_MEM_ERROR
);
685 /* inflateReset failed, store the error message */
686 png_zstream_error(png_ptr
, ret
);
688 if (ret
== Z_STREAM_END
)
689 ret
= PNG_UNEXPECTED_ZLIB_RETURN
;
693 else if (ret
== Z_OK
)
694 ret
= PNG_UNEXPECTED_ZLIB_RETURN
;
696 /* Release the claimed stream */
700 else /* the claim failed */ if (ret
== Z_STREAM_END
) /* impossible! */
701 ret
= PNG_UNEXPECTED_ZLIB_RETURN
;
708 /* Application/configuration limits exceeded */
709 png_zstream_error(png_ptr
, Z_MEM_ERROR
);
713 #endif /* PNG_READ_COMPRESSED_TEXT_SUPPORTED */
715 #ifdef PNG_READ_iCCP_SUPPORTED
716 /* Perform a partial read and decompress, producing 'avail_out' bytes and
717 * reading from the current chunk as required.
720 png_inflate_read(png_structrp png_ptr
, png_bytep read_buffer
, uInt read_size
,
721 png_uint_32p chunk_bytes
, png_bytep next_out
, png_alloc_size_t
*out_size
,
724 if (png_ptr
->zowner
== png_ptr
->chunk_name
)
728 /* next_in and avail_in must have been initialized by the caller. */
729 png_ptr
->zstream
.next_out
= next_out
;
730 png_ptr
->zstream
.avail_out
= 0; /* set in the loop */
734 if (png_ptr
->zstream
.avail_in
== 0)
736 if (read_size
> *chunk_bytes
)
737 read_size
= (uInt
)*chunk_bytes
;
738 *chunk_bytes
-= read_size
;
741 png_crc_read(png_ptr
, read_buffer
, read_size
);
743 png_ptr
->zstream
.next_in
= read_buffer
;
744 png_ptr
->zstream
.avail_in
= read_size
;
747 if (png_ptr
->zstream
.avail_out
== 0)
749 uInt avail
= ZLIB_IO_MAX
;
750 if (avail
> *out_size
)
751 avail
= (uInt
)*out_size
;
754 png_ptr
->zstream
.avail_out
= avail
;
757 /* Use Z_SYNC_FLUSH when there is no more chunk data to ensure that all
758 * the available output is produced; this allows reading of truncated
761 ret
= inflate(&png_ptr
->zstream
,
762 *chunk_bytes
> 0 ? Z_NO_FLUSH
: (finish
? Z_FINISH
: Z_SYNC_FLUSH
));
764 while (ret
== Z_OK
&& (*out_size
> 0 || png_ptr
->zstream
.avail_out
> 0));
766 *out_size
+= png_ptr
->zstream
.avail_out
;
767 png_ptr
->zstream
.avail_out
= 0; /* Should not be required, but is safe */
769 /* Ensure the error message pointer is always set: */
770 png_zstream_error(png_ptr
, ret
);
776 png_ptr
->zstream
.msg
= PNGZ_MSG_CAST("zstream unclaimed");
777 return Z_STREAM_ERROR
;
782 /* Read and check the IDHR chunk */
784 png_handle_IHDR(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
787 png_uint_32 width
, height
;
788 int bit_depth
, color_type
, compression_type
, filter_type
;
791 png_debug(1, "in png_handle_IHDR");
793 if (png_ptr
->mode
& PNG_HAVE_IHDR
)
794 png_chunk_error(png_ptr
, "out of place");
796 /* Check the length */
798 png_chunk_error(png_ptr
, "invalid");
800 png_ptr
->mode
|= PNG_HAVE_IHDR
;
802 png_crc_read(png_ptr
, buf
, 13);
803 png_crc_finish(png_ptr
, 0);
805 width
= png_get_uint_31(png_ptr
, buf
);
806 height
= png_get_uint_31(png_ptr
, buf
+ 4);
809 compression_type
= buf
[10];
810 filter_type
= buf
[11];
811 interlace_type
= buf
[12];
813 /* Set internal variables */
814 png_ptr
->width
= width
;
815 png_ptr
->height
= height
;
816 png_ptr
->bit_depth
= (png_byte
)bit_depth
;
817 png_ptr
->interlaced
= (png_byte
)interlace_type
;
818 png_ptr
->color_type
= (png_byte
)color_type
;
819 #ifdef PNG_MNG_FEATURES_SUPPORTED
820 png_ptr
->filter_type
= (png_byte
)filter_type
;
822 png_ptr
->compression_type
= (png_byte
)compression_type
;
824 /* Find number of channels */
825 switch (png_ptr
->color_type
)
827 default: /* invalid, png_set_IHDR calls png_error */
828 case PNG_COLOR_TYPE_GRAY
:
829 case PNG_COLOR_TYPE_PALETTE
:
830 png_ptr
->channels
= 1;
833 case PNG_COLOR_TYPE_RGB
:
834 png_ptr
->channels
= 3;
837 case PNG_COLOR_TYPE_GRAY_ALPHA
:
838 png_ptr
->channels
= 2;
841 case PNG_COLOR_TYPE_RGB_ALPHA
:
842 png_ptr
->channels
= 4;
846 /* Set up other useful info */
847 png_ptr
->pixel_depth
= (png_byte
)(png_ptr
->bit_depth
*
849 png_ptr
->rowbytes
= PNG_ROWBYTES(png_ptr
->pixel_depth
, png_ptr
->width
);
850 png_debug1(3, "bit_depth = %d", png_ptr
->bit_depth
);
851 png_debug1(3, "channels = %d", png_ptr
->channels
);
852 png_debug1(3, "rowbytes = %lu", (unsigned long)png_ptr
->rowbytes
);
853 png_set_IHDR(png_ptr
, info_ptr
, width
, height
, bit_depth
,
854 color_type
, interlace_type
, compression_type
, filter_type
);
857 /* Read and check the palette */
859 png_handle_PLTE(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
861 png_color palette
[PNG_MAX_PALETTE_LENGTH
];
863 #ifdef PNG_POINTER_INDEXING_SUPPORTED
867 png_debug(1, "in png_handle_PLTE");
869 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
870 png_chunk_error(png_ptr
, "missing IHDR");
872 /* Moved to before the 'after IDAT' check below because otherwise duplicate
873 * PLTE chunks are potentially ignored (the spec says there shall not be more
874 * than one PLTE, the error is not treated as benign, so this check trumps
875 * the requirement that PLTE appears before IDAT.)
877 else if (png_ptr
->mode
& PNG_HAVE_PLTE
)
878 png_chunk_error(png_ptr
, "duplicate");
880 else if (png_ptr
->mode
& PNG_HAVE_IDAT
)
882 /* This is benign because the non-benign error happened before, when an
883 * IDAT was encountered in a color-mapped image with no PLTE.
885 png_crc_finish(png_ptr
, length
);
886 png_chunk_benign_error(png_ptr
, "out of place");
890 png_ptr
->mode
|= PNG_HAVE_PLTE
;
892 if (!(png_ptr
->color_type
& PNG_COLOR_MASK_COLOR
))
894 png_crc_finish(png_ptr
, length
);
895 png_chunk_benign_error(png_ptr
, "ignored in grayscale PNG");
899 #ifndef PNG_READ_OPT_PLTE_SUPPORTED
900 if (png_ptr
->color_type
!= PNG_COLOR_TYPE_PALETTE
)
902 png_crc_finish(png_ptr
, length
);
907 if (length
> 3*PNG_MAX_PALETTE_LENGTH
|| length
% 3)
909 png_crc_finish(png_ptr
, length
);
911 if (png_ptr
->color_type
!= PNG_COLOR_TYPE_PALETTE
)
912 png_chunk_benign_error(png_ptr
, "invalid");
915 png_chunk_error(png_ptr
, "invalid");
920 /* The cast is safe because 'length' is less than 3*PNG_MAX_PALETTE_LENGTH */
921 num
= (int)length
/ 3;
923 #ifdef PNG_POINTER_INDEXING_SUPPORTED
924 for (i
= 0, pal_ptr
= palette
; i
< num
; i
++, pal_ptr
++)
928 png_crc_read(png_ptr
, buf
, 3);
929 pal_ptr
->red
= buf
[0];
930 pal_ptr
->green
= buf
[1];
931 pal_ptr
->blue
= buf
[2];
934 for (i
= 0; i
< num
; i
++)
938 png_crc_read(png_ptr
, buf
, 3);
939 /* Don't depend upon png_color being any order */
940 palette
[i
].red
= buf
[0];
941 palette
[i
].green
= buf
[1];
942 palette
[i
].blue
= buf
[2];
946 /* If we actually need the PLTE chunk (ie for a paletted image), we do
947 * whatever the normal CRC configuration tells us. However, if we
948 * have an RGB image, the PLTE can be considered ancillary, so
949 * we will act as though it is.
951 #ifndef PNG_READ_OPT_PLTE_SUPPORTED
952 if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
955 png_crc_finish(png_ptr
, 0);
958 #ifndef PNG_READ_OPT_PLTE_SUPPORTED
959 else if (png_crc_error(png_ptr
)) /* Only if we have a CRC error */
961 /* If we don't want to use the data from an ancillary chunk,
962 * we have two options: an error abort, or a warning and we
963 * ignore the data in this chunk (which should be OK, since
964 * it's considered ancillary for a RGB or RGBA image).
966 * IMPLEMENTATION NOTE: this is only here because png_crc_finish uses the
967 * chunk type to determine whether to check the ancillary or the critical
970 if (!(png_ptr
->flags
& PNG_FLAG_CRC_ANCILLARY_USE
))
972 if (png_ptr
->flags
& PNG_FLAG_CRC_ANCILLARY_NOWARN
)
974 png_chunk_benign_error(png_ptr
, "CRC error");
979 png_chunk_warning(png_ptr
, "CRC error");
984 /* Otherwise, we (optionally) emit a warning and use the chunk. */
985 else if (!(png_ptr
->flags
& PNG_FLAG_CRC_ANCILLARY_NOWARN
))
987 png_chunk_warning(png_ptr
, "CRC error");
992 /* TODO: png_set_PLTE has the side effect of setting png_ptr->palette to its
993 * own copy of the palette. This has the side effect that when png_start_row
994 * is called (this happens after any call to png_read_update_info) the
995 * info_ptr palette gets changed. This is extremely unexpected and
998 * Fix this by not sharing the palette in this way.
1000 png_set_PLTE(png_ptr
, info_ptr
, palette
, num
);
1002 /* The three chunks, bKGD, hIST and tRNS *must* appear after PLTE and before
1003 * IDAT. Prior to 1.6.0 this was not checked; instead the code merely
1004 * checked the apparent validity of a tRNS chunk inserted before PLTE on a
1005 * palette PNG. 1.6.0 attempts to rigorously follow the standard and
1006 * therefore does a benign error if the erroneous condition is detected *and*
1007 * cancels the tRNS if the benign error returns. The alternative is to
1008 * amend the standard since it would be rather hypocritical of the standards
1009 * maintainers to ignore it.
1011 #ifdef PNG_READ_tRNS_SUPPORTED
1012 if (png_ptr
->num_trans
> 0 ||
1013 (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_tRNS
) != 0))
1015 /* Cancel this because otherwise it would be used if the transforms
1016 * require it. Don't cancel the 'valid' flag because this would prevent
1017 * detection of duplicate chunks.
1019 png_ptr
->num_trans
= 0;
1021 if (info_ptr
!= NULL
)
1022 info_ptr
->num_trans
= 0;
1024 png_chunk_benign_error(png_ptr
, "tRNS must be after");
1028 #ifdef PNG_READ_hIST_SUPPORTED
1029 if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_hIST
) != 0)
1030 png_chunk_benign_error(png_ptr
, "hIST must be after");
1033 #ifdef PNG_READ_bKGD_SUPPORTED
1034 if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_bKGD
) != 0)
1035 png_chunk_benign_error(png_ptr
, "bKGD must be after");
1040 png_handle_IEND(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
1042 png_debug(1, "in png_handle_IEND");
1044 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
) || !(png_ptr
->mode
& PNG_HAVE_IDAT
))
1045 png_chunk_error(png_ptr
, "out of place");
1047 png_ptr
->mode
|= (PNG_AFTER_IDAT
| PNG_HAVE_IEND
);
1049 png_crc_finish(png_ptr
, length
);
1052 png_chunk_benign_error(png_ptr
, "invalid");
1054 PNG_UNUSED(info_ptr
)
1057 #ifdef PNG_READ_gAMA_SUPPORTED
1059 png_handle_gAMA(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
1061 png_fixed_point igamma
;
1064 png_debug(1, "in png_handle_gAMA");
1066 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
1067 png_chunk_error(png_ptr
, "missing IHDR");
1069 else if (png_ptr
->mode
& (PNG_HAVE_IDAT
|PNG_HAVE_PLTE
))
1071 png_crc_finish(png_ptr
, length
);
1072 png_chunk_benign_error(png_ptr
, "out of place");
1078 png_crc_finish(png_ptr
, length
);
1079 png_chunk_benign_error(png_ptr
, "invalid");
1083 png_crc_read(png_ptr
, buf
, 4);
1085 if (png_crc_finish(png_ptr
, 0))
1088 igamma
= png_get_fixed_point(NULL
, buf
);
1090 png_colorspace_set_gamma(png_ptr
, &png_ptr
->colorspace
, igamma
);
1091 png_colorspace_sync(png_ptr
, info_ptr
);
1095 #ifdef PNG_READ_sBIT_SUPPORTED
1097 png_handle_sBIT(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
1099 unsigned int truelen
;
1102 png_debug(1, "in png_handle_sBIT");
1104 buf
[0] = buf
[1] = buf
[2] = buf
[3] = 0;
1106 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
1107 png_chunk_error(png_ptr
, "missing IHDR");
1109 else if (png_ptr
->mode
& (PNG_HAVE_IDAT
|PNG_HAVE_PLTE
))
1111 png_crc_finish(png_ptr
, length
);
1112 png_chunk_benign_error(png_ptr
, "out of place");
1116 if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_sBIT
))
1118 png_crc_finish(png_ptr
, length
);
1119 png_chunk_benign_error(png_ptr
, "duplicate");
1123 if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
1127 truelen
= png_ptr
->channels
;
1129 if (length
!= truelen
|| length
> 4)
1131 png_chunk_benign_error(png_ptr
, "invalid");
1132 png_crc_finish(png_ptr
, length
);
1136 png_crc_read(png_ptr
, buf
, truelen
);
1138 if (png_crc_finish(png_ptr
, 0))
1141 if (png_ptr
->color_type
& PNG_COLOR_MASK_COLOR
)
1143 png_ptr
->sig_bit
.red
= buf
[0];
1144 png_ptr
->sig_bit
.green
= buf
[1];
1145 png_ptr
->sig_bit
.blue
= buf
[2];
1146 png_ptr
->sig_bit
.alpha
= buf
[3];
1151 png_ptr
->sig_bit
.gray
= buf
[0];
1152 png_ptr
->sig_bit
.red
= buf
[0];
1153 png_ptr
->sig_bit
.green
= buf
[0];
1154 png_ptr
->sig_bit
.blue
= buf
[0];
1155 png_ptr
->sig_bit
.alpha
= buf
[1];
1158 png_set_sBIT(png_ptr
, info_ptr
, &(png_ptr
->sig_bit
));
1162 #ifdef PNG_READ_cHRM_SUPPORTED
1164 png_handle_cHRM(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
1169 png_debug(1, "in png_handle_cHRM");
1171 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
1172 png_chunk_error(png_ptr
, "missing IHDR");
1174 else if (png_ptr
->mode
& (PNG_HAVE_IDAT
|PNG_HAVE_PLTE
))
1176 png_crc_finish(png_ptr
, length
);
1177 png_chunk_benign_error(png_ptr
, "out of place");
1183 png_crc_finish(png_ptr
, length
);
1184 png_chunk_benign_error(png_ptr
, "invalid");
1188 png_crc_read(png_ptr
, buf
, 32);
1190 if (png_crc_finish(png_ptr
, 0))
1193 xy
.whitex
= png_get_fixed_point(NULL
, buf
);
1194 xy
.whitey
= png_get_fixed_point(NULL
, buf
+ 4);
1195 xy
.redx
= png_get_fixed_point(NULL
, buf
+ 8);
1196 xy
.redy
= png_get_fixed_point(NULL
, buf
+ 12);
1197 xy
.greenx
= png_get_fixed_point(NULL
, buf
+ 16);
1198 xy
.greeny
= png_get_fixed_point(NULL
, buf
+ 20);
1199 xy
.bluex
= png_get_fixed_point(NULL
, buf
+ 24);
1200 xy
.bluey
= png_get_fixed_point(NULL
, buf
+ 28);
1202 if (xy
.whitex
== PNG_FIXED_ERROR
||
1203 xy
.whitey
== PNG_FIXED_ERROR
||
1204 xy
.redx
== PNG_FIXED_ERROR
||
1205 xy
.redy
== PNG_FIXED_ERROR
||
1206 xy
.greenx
== PNG_FIXED_ERROR
||
1207 xy
.greeny
== PNG_FIXED_ERROR
||
1208 xy
.bluex
== PNG_FIXED_ERROR
||
1209 xy
.bluey
== PNG_FIXED_ERROR
)
1211 png_chunk_benign_error(png_ptr
, "invalid values");
1215 /* If a colorspace error has already been output skip this chunk */
1216 if (png_ptr
->colorspace
.flags
& PNG_COLORSPACE_INVALID
)
1219 if (png_ptr
->colorspace
.flags
& PNG_COLORSPACE_FROM_cHRM
)
1221 png_ptr
->colorspace
.flags
|= PNG_COLORSPACE_INVALID
;
1222 png_colorspace_sync(png_ptr
, info_ptr
);
1223 png_chunk_benign_error(png_ptr
, "duplicate");
1227 png_ptr
->colorspace
.flags
|= PNG_COLORSPACE_FROM_cHRM
;
1228 (void)png_colorspace_set_chromaticities(png_ptr
, &png_ptr
->colorspace
, &xy
,
1229 1/*prefer cHRM values*/);
1230 png_colorspace_sync(png_ptr
, info_ptr
);
1234 #ifdef PNG_READ_sRGB_SUPPORTED
1236 png_handle_sRGB(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
1240 png_debug(1, "in png_handle_sRGB");
1242 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
1243 png_chunk_error(png_ptr
, "missing IHDR");
1245 else if (png_ptr
->mode
& (PNG_HAVE_IDAT
|PNG_HAVE_PLTE
))
1247 png_crc_finish(png_ptr
, length
);
1248 png_chunk_benign_error(png_ptr
, "out of place");
1254 png_crc_finish(png_ptr
, length
);
1255 png_chunk_benign_error(png_ptr
, "invalid");
1259 png_crc_read(png_ptr
, &intent
, 1);
1261 if (png_crc_finish(png_ptr
, 0))
1264 /* If a colorspace error has already been output skip this chunk */
1265 if (png_ptr
->colorspace
.flags
& PNG_COLORSPACE_INVALID
)
1268 /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect
1271 if (png_ptr
->colorspace
.flags
& PNG_COLORSPACE_HAVE_INTENT
)
1273 png_ptr
->colorspace
.flags
|= PNG_COLORSPACE_INVALID
;
1274 png_colorspace_sync(png_ptr
, info_ptr
);
1275 png_chunk_benign_error(png_ptr
, "too many profiles");
1279 (void)png_colorspace_set_sRGB(png_ptr
, &png_ptr
->colorspace
, intent
);
1280 png_colorspace_sync(png_ptr
, info_ptr
);
1282 #endif /* PNG_READ_sRGB_SUPPORTED */
1284 #ifdef PNG_READ_iCCP_SUPPORTED
1286 png_handle_iCCP(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
1287 /* Note: this does not properly handle profiles that are > 64K under DOS */
1289 png_const_charp errmsg
= NULL
; /* error message output, or no error */
1290 int finished
= 0; /* crc checked */
1292 png_debug(1, "in png_handle_iCCP");
1294 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
1295 png_chunk_error(png_ptr
, "missing IHDR");
1297 else if (png_ptr
->mode
& (PNG_HAVE_IDAT
|PNG_HAVE_PLTE
))
1299 png_crc_finish(png_ptr
, length
);
1300 png_chunk_benign_error(png_ptr
, "out of place");
1304 /* Consistent with all the above colorspace handling an obviously *invalid*
1305 * chunk is just ignored, so does not invalidate the color space. An
1306 * alternative is to set the 'invalid' flags at the start of this routine
1307 * and only clear them in they were not set before and all the tests pass.
1308 * The minimum 'deflate' stream is assumed to be just the 2 byte header and 4
1309 * byte checksum. The keyword must be one character and there is a
1310 * terminator (0) byte and the compression method.
1314 png_crc_finish(png_ptr
, length
);
1315 png_chunk_benign_error(png_ptr
, "too short");
1319 /* If a colorspace error has already been output skip this chunk */
1320 if (png_ptr
->colorspace
.flags
& PNG_COLORSPACE_INVALID
)
1322 png_crc_finish(png_ptr
, length
);
1326 /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect
1329 if ((png_ptr
->colorspace
.flags
& PNG_COLORSPACE_HAVE_INTENT
) == 0)
1331 uInt read_length
, keyword_length
;
1334 /* Find the keyword; the keyword plus separator and compression method
1335 * bytes can be at most 81 characters long.
1337 read_length
= 81; /* maximum */
1338 if (read_length
> length
)
1339 read_length
= (uInt
)length
;
1341 png_crc_read(png_ptr
, (png_bytep
)keyword
, read_length
);
1342 length
-= read_length
;
1345 while (keyword_length
< 80 && keyword_length
< read_length
&&
1346 keyword
[keyword_length
] != 0)
1349 /* TODO: make the keyword checking common */
1350 if (keyword_length
>= 1 && keyword_length
<= 79)
1352 /* We only understand '0' compression - deflate - so if we get a
1353 * different value we can't safely decode the chunk.
1355 if (keyword_length
+1 < read_length
&&
1356 keyword
[keyword_length
+1] == PNG_COMPRESSION_TYPE_BASE
)
1358 read_length
-= keyword_length
+2;
1360 if (png_inflate_claim(png_ptr
, png_iCCP
,
1361 png_ptr
->flags
& PNG_FLAG_BENIGN_ERRORS_WARN
? 15 : 0) == Z_OK
)
1363 Byte profile_header
[132];
1364 Byte local_buffer
[PNG_INFLATE_BUF_SIZE
];
1365 png_alloc_size_t size
= (sizeof profile_header
);
1367 png_ptr
->zstream
.next_in
= (Bytef
*)keyword
+ (keyword_length
+2);
1368 png_ptr
->zstream
.avail_in
= read_length
;
1369 (void)png_inflate_read(png_ptr
, local_buffer
,
1370 (sizeof local_buffer
), &length
, profile_header
, &size
,
1371 0/*finish: don't, because the output is too small*/);
1375 /* We have the ICC profile header; do the basic header checks.
1377 const png_uint_32 profile_length
=
1378 png_get_uint_32(profile_header
);
1380 if (png_icc_check_length(png_ptr
, &png_ptr
->colorspace
,
1381 keyword
, profile_length
))
1383 /* The length is apparently ok, so we can check the 132
1386 if (png_icc_check_header(png_ptr
, &png_ptr
->colorspace
,
1387 keyword
, profile_length
, profile_header
,
1388 png_ptr
->color_type
))
1390 /* Now read the tag table; a variable size buffer is
1391 * needed at this point, allocate one for the whole
1392 * profile. The header check has already validated
1393 * that none of these stuff will overflow.
1395 const png_uint_32 tag_count
= png_get_uint_32(
1396 profile_header
+128);
1397 png_bytep profile
= png_read_buffer(png_ptr
,
1398 profile_length
, 2/*silent*/);
1400 if (profile
!= NULL
)
1402 memcpy(profile
, profile_header
,
1403 (sizeof profile_header
));
1405 size
= 12 * tag_count
;
1407 (void)png_inflate_read(png_ptr
, local_buffer
,
1408 (sizeof local_buffer
), &length
,
1409 profile
+ (sizeof profile_header
), &size
, 0);
1411 /* Still expect a a buffer error because we expect
1412 * there to be some tag data!
1416 if (png_icc_check_tag_table(png_ptr
,
1417 &png_ptr
->colorspace
, keyword
, profile_length
,
1420 /* The profile has been validated for basic
1421 * security issues, so read the whole thing in.
1423 size
= profile_length
- (sizeof profile_header
)
1426 (void)png_inflate_read(png_ptr
, local_buffer
,
1427 (sizeof local_buffer
), &length
,
1428 profile
+ (sizeof profile_header
) +
1429 12 * tag_count
, &size
, 1/*finish*/);
1431 if (length
> 0 && !(png_ptr
->flags
&
1432 PNG_FLAG_BENIGN_ERRORS_WARN
))
1433 errmsg
= "extra compressed data";
1435 /* But otherwise allow extra data: */
1440 /* This can be handled completely, so
1443 png_chunk_warning(png_ptr
,
1444 "extra compressed data");
1447 png_crc_finish(png_ptr
, length
);
1450 # ifdef PNG_sRGB_SUPPORTED
1451 /* Check for a match against sRGB */
1452 png_icc_set_sRGB(png_ptr
,
1453 &png_ptr
->colorspace
, profile
,
1454 png_ptr
->zstream
.adler
);
1457 /* Steal the profile for info_ptr. */
1458 if (info_ptr
!= NULL
)
1460 png_free_data(png_ptr
, info_ptr
,
1463 info_ptr
->iccp_name
= png_voidcast(char*,
1464 png_malloc_base(png_ptr
,
1466 if (info_ptr
->iccp_name
!= NULL
)
1468 memcpy(info_ptr
->iccp_name
, keyword
,
1470 info_ptr
->iccp_proflen
=
1472 info_ptr
->iccp_profile
= profile
;
1473 png_ptr
->read_buffer
= NULL
; /*steal*/
1474 info_ptr
->free_me
|= PNG_FREE_ICCP
;
1475 info_ptr
->valid
|= PNG_INFO_iCCP
;
1480 png_ptr
->colorspace
.flags
|=
1481 PNG_COLORSPACE_INVALID
;
1482 errmsg
= "out of memory";
1486 /* else the profile remains in the read
1487 * buffer which gets reused for subsequent
1491 if (info_ptr
!= NULL
)
1492 png_colorspace_sync(png_ptr
, info_ptr
);
1496 png_ptr
->zowner
= 0;
1502 errmsg
= "truncated";
1505 errmsg
= png_ptr
->zstream
.msg
;
1508 /* else png_icc_check_tag_table output an error */
1511 else /* profile truncated */
1512 errmsg
= png_ptr
->zstream
.msg
;
1516 errmsg
= "out of memory";
1519 /* else png_icc_check_header output an error */
1522 /* else png_icc_check_length output an error */
1525 else /* profile truncated */
1526 errmsg
= png_ptr
->zstream
.msg
;
1528 /* Release the stream */
1529 png_ptr
->zowner
= 0;
1532 else /* png_inflate_claim failed */
1533 errmsg
= png_ptr
->zstream
.msg
;
1537 errmsg
= "bad compression method"; /* or missing */
1541 errmsg
= "bad keyword";
1545 errmsg
= "too many profiles";
1547 /* Failure: the reason is in 'errmsg' */
1549 png_crc_finish(png_ptr
, length
);
1551 png_ptr
->colorspace
.flags
|= PNG_COLORSPACE_INVALID
;
1552 png_colorspace_sync(png_ptr
, info_ptr
);
1553 if (errmsg
!= NULL
) /* else already output */
1554 png_chunk_benign_error(png_ptr
, errmsg
);
1556 #endif /* PNG_READ_iCCP_SUPPORTED */
1558 #ifdef PNG_READ_sPLT_SUPPORTED
1560 png_handle_sPLT(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
1561 /* Note: this does not properly handle chunks that are > 64K under DOS */
1563 png_bytep entry_start
, buffer
;
1564 png_sPLT_t new_palette
;
1566 png_uint_32 data_length
;
1568 png_uint_32 skip
= 0;
1572 png_debug(1, "in png_handle_sPLT");
1574 #ifdef PNG_USER_LIMITS_SUPPORTED
1575 if (png_ptr
->user_chunk_cache_max
!= 0)
1577 if (png_ptr
->user_chunk_cache_max
== 1)
1579 png_crc_finish(png_ptr
, length
);
1583 if (--png_ptr
->user_chunk_cache_max
== 1)
1585 png_warning(png_ptr
, "No space in chunk cache for sPLT");
1586 png_crc_finish(png_ptr
, length
);
1592 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
1593 png_chunk_error(png_ptr
, "missing IHDR");
1595 else if (png_ptr
->mode
& PNG_HAVE_IDAT
)
1597 png_crc_finish(png_ptr
, length
);
1598 png_chunk_benign_error(png_ptr
, "out of place");
1602 #ifdef PNG_MAX_MALLOC_64K
1603 if (length
> 65535U)
1605 png_crc_finish(png_ptr
, length
);
1606 png_chunk_benign_error(png_ptr
, "too large to fit in memory");
1611 buffer
= png_read_buffer(png_ptr
, length
+1, 2/*silent*/);
1614 png_crc_finish(png_ptr
, length
);
1615 png_chunk_benign_error(png_ptr
, "out of memory");
1620 /* WARNING: this may break if size_t is less than 32 bits; it is assumed
1621 * that the PNG_MAX_MALLOC_64K test is enabled in this case, but this is a
1622 * potential breakage point if the types in pngconf.h aren't exactly right.
1624 png_crc_read(png_ptr
, buffer
, length
);
1626 if (png_crc_finish(png_ptr
, skip
))
1631 for (entry_start
= buffer
; *entry_start
; entry_start
++)
1632 /* Empty loop to find end of name */ ;
1636 /* A sample depth should follow the separator, and we should be on it */
1637 if (entry_start
> buffer
+ length
- 2)
1639 png_warning(png_ptr
, "malformed sPLT chunk");
1643 new_palette
.depth
= *entry_start
++;
1644 entry_size
= (new_palette
.depth
== 8 ? 6 : 10);
1645 /* This must fit in a png_uint_32 because it is derived from the original
1646 * chunk data length.
1648 data_length
= length
- (png_uint_32
)(entry_start
- buffer
);
1650 /* Integrity-check the data length */
1651 if (data_length
% entry_size
)
1653 png_warning(png_ptr
, "sPLT chunk has bad length");
1657 dl
= (png_int_32
)(data_length
/ entry_size
);
1658 max_dl
= PNG_SIZE_MAX
/ (sizeof (png_sPLT_entry
));
1662 png_warning(png_ptr
, "sPLT chunk too long");
1666 new_palette
.nentries
= (png_int_32
)(data_length
/ entry_size
);
1668 new_palette
.entries
= (png_sPLT_entryp
)png_malloc_warn(
1669 png_ptr
, new_palette
.nentries
* (sizeof (png_sPLT_entry
)));
1671 if (new_palette
.entries
== NULL
)
1673 png_warning(png_ptr
, "sPLT chunk requires too much memory");
1677 #ifdef PNG_POINTER_INDEXING_SUPPORTED
1678 for (i
= 0; i
< new_palette
.nentries
; i
++)
1680 pp
= new_palette
.entries
+ i
;
1682 if (new_palette
.depth
== 8)
1684 pp
->red
= *entry_start
++;
1685 pp
->green
= *entry_start
++;
1686 pp
->blue
= *entry_start
++;
1687 pp
->alpha
= *entry_start
++;
1692 pp
->red
= png_get_uint_16(entry_start
); entry_start
+= 2;
1693 pp
->green
= png_get_uint_16(entry_start
); entry_start
+= 2;
1694 pp
->blue
= png_get_uint_16(entry_start
); entry_start
+= 2;
1695 pp
->alpha
= png_get_uint_16(entry_start
); entry_start
+= 2;
1698 pp
->frequency
= png_get_uint_16(entry_start
); entry_start
+= 2;
1701 pp
= new_palette
.entries
;
1703 for (i
= 0; i
< new_palette
.nentries
; i
++)
1706 if (new_palette
.depth
== 8)
1708 pp
[i
].red
= *entry_start
++;
1709 pp
[i
].green
= *entry_start
++;
1710 pp
[i
].blue
= *entry_start
++;
1711 pp
[i
].alpha
= *entry_start
++;
1716 pp
[i
].red
= png_get_uint_16(entry_start
); entry_start
+= 2;
1717 pp
[i
].green
= png_get_uint_16(entry_start
); entry_start
+= 2;
1718 pp
[i
].blue
= png_get_uint_16(entry_start
); entry_start
+= 2;
1719 pp
[i
].alpha
= png_get_uint_16(entry_start
); entry_start
+= 2;
1722 pp
[i
].frequency
= png_get_uint_16(entry_start
); entry_start
+= 2;
1726 /* Discard all chunk data except the name and stash that */
1727 new_palette
.name
= (png_charp
)buffer
;
1729 png_set_sPLT(png_ptr
, info_ptr
, &new_palette
, 1);
1731 png_free(png_ptr
, new_palette
.entries
);
1733 #endif /* PNG_READ_sPLT_SUPPORTED */
1735 #ifdef PNG_READ_tRNS_SUPPORTED
1737 png_handle_tRNS(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
1739 png_byte readbuf
[PNG_MAX_PALETTE_LENGTH
];
1741 png_debug(1, "in png_handle_tRNS");
1743 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
1744 png_chunk_error(png_ptr
, "missing IHDR");
1746 else if (png_ptr
->mode
& PNG_HAVE_IDAT
)
1748 png_crc_finish(png_ptr
, length
);
1749 png_chunk_benign_error(png_ptr
, "out of place");
1753 else if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_tRNS
))
1755 png_crc_finish(png_ptr
, length
);
1756 png_chunk_benign_error(png_ptr
, "duplicate");
1760 if (png_ptr
->color_type
== PNG_COLOR_TYPE_GRAY
)
1766 png_crc_finish(png_ptr
, length
);
1767 png_chunk_benign_error(png_ptr
, "invalid");
1771 png_crc_read(png_ptr
, buf
, 2);
1772 png_ptr
->num_trans
= 1;
1773 png_ptr
->trans_color
.gray
= png_get_uint_16(buf
);
1776 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_RGB
)
1782 png_crc_finish(png_ptr
, length
);
1783 png_chunk_benign_error(png_ptr
, "invalid");
1787 png_crc_read(png_ptr
, buf
, length
);
1788 png_ptr
->num_trans
= 1;
1789 png_ptr
->trans_color
.red
= png_get_uint_16(buf
);
1790 png_ptr
->trans_color
.green
= png_get_uint_16(buf
+ 2);
1791 png_ptr
->trans_color
.blue
= png_get_uint_16(buf
+ 4);
1794 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
1796 if (!(png_ptr
->mode
& PNG_HAVE_PLTE
))
1798 /* TODO: is this actually an error in the ISO spec? */
1799 png_crc_finish(png_ptr
, length
);
1800 png_chunk_benign_error(png_ptr
, "out of place");
1804 if (length
> png_ptr
->num_palette
|| length
> PNG_MAX_PALETTE_LENGTH
||
1807 png_crc_finish(png_ptr
, length
);
1808 png_chunk_benign_error(png_ptr
, "invalid");
1812 png_crc_read(png_ptr
, readbuf
, length
);
1813 png_ptr
->num_trans
= (png_uint_16
)length
;
1818 png_crc_finish(png_ptr
, length
);
1819 png_chunk_benign_error(png_ptr
, "invalid with alpha channel");
1823 if (png_crc_finish(png_ptr
, 0))
1825 png_ptr
->num_trans
= 0;
1829 /* TODO: this is a horrible side effect in the palette case because the
1830 * png_struct ends up with a pointer to the tRNS buffer owned by the
1831 * png_info. Fix this.
1833 png_set_tRNS(png_ptr
, info_ptr
, readbuf
, png_ptr
->num_trans
,
1834 &(png_ptr
->trans_color
));
1838 #ifdef PNG_READ_bKGD_SUPPORTED
1840 png_handle_bKGD(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
1842 unsigned int truelen
;
1844 png_color_16 background
;
1846 png_debug(1, "in png_handle_bKGD");
1848 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
1849 png_chunk_error(png_ptr
, "missing IHDR");
1851 else if ((png_ptr
->mode
& PNG_HAVE_IDAT
) ||
1852 (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
&&
1853 !(png_ptr
->mode
& PNG_HAVE_PLTE
)))
1855 png_crc_finish(png_ptr
, length
);
1856 png_chunk_benign_error(png_ptr
, "out of place");
1860 else if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_bKGD
))
1862 png_crc_finish(png_ptr
, length
);
1863 png_chunk_benign_error(png_ptr
, "duplicate");
1867 if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
1870 else if (png_ptr
->color_type
& PNG_COLOR_MASK_COLOR
)
1876 if (length
!= truelen
)
1878 png_crc_finish(png_ptr
, length
);
1879 png_chunk_benign_error(png_ptr
, "invalid");
1883 png_crc_read(png_ptr
, buf
, truelen
);
1885 if (png_crc_finish(png_ptr
, 0))
1888 /* We convert the index value into RGB components so that we can allow
1889 * arbitrary RGB values for background when we have transparency, and
1890 * so it is easy to determine the RGB values of the background color
1891 * from the info_ptr struct.
1893 if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
1895 background
.index
= buf
[0];
1897 if (info_ptr
&& info_ptr
->num_palette
)
1899 if (buf
[0] >= info_ptr
->num_palette
)
1901 png_chunk_benign_error(png_ptr
, "invalid index");
1905 background
.red
= (png_uint_16
)png_ptr
->palette
[buf
[0]].red
;
1906 background
.green
= (png_uint_16
)png_ptr
->palette
[buf
[0]].green
;
1907 background
.blue
= (png_uint_16
)png_ptr
->palette
[buf
[0]].blue
;
1911 background
.red
= background
.green
= background
.blue
= 0;
1913 background
.gray
= 0;
1916 else if (!(png_ptr
->color_type
& PNG_COLOR_MASK_COLOR
)) /* GRAY */
1918 background
.index
= 0;
1922 background
.gray
= png_get_uint_16(buf
);
1927 background
.index
= 0;
1928 background
.red
= png_get_uint_16(buf
);
1929 background
.green
= png_get_uint_16(buf
+ 2);
1930 background
.blue
= png_get_uint_16(buf
+ 4);
1931 background
.gray
= 0;
1934 png_set_bKGD(png_ptr
, info_ptr
, &background
);
1938 #ifdef PNG_READ_hIST_SUPPORTED
1940 png_handle_hIST(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
1942 unsigned int num
, i
;
1943 png_uint_16 readbuf
[PNG_MAX_PALETTE_LENGTH
];
1945 png_debug(1, "in png_handle_hIST");
1947 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
1948 png_chunk_error(png_ptr
, "missing IHDR");
1950 else if ((png_ptr
->mode
& PNG_HAVE_IDAT
) || !(png_ptr
->mode
& PNG_HAVE_PLTE
))
1952 png_crc_finish(png_ptr
, length
);
1953 png_chunk_benign_error(png_ptr
, "out of place");
1957 else if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_hIST
))
1959 png_crc_finish(png_ptr
, length
);
1960 png_chunk_benign_error(png_ptr
, "duplicate");
1966 if (num
!= png_ptr
->num_palette
|| num
> PNG_MAX_PALETTE_LENGTH
)
1968 png_crc_finish(png_ptr
, length
);
1969 png_chunk_benign_error(png_ptr
, "invalid");
1973 for (i
= 0; i
< num
; i
++)
1977 png_crc_read(png_ptr
, buf
, 2);
1978 readbuf
[i
] = png_get_uint_16(buf
);
1981 if (png_crc_finish(png_ptr
, 0))
1984 png_set_hIST(png_ptr
, info_ptr
, readbuf
);
1988 #ifdef PNG_READ_pHYs_SUPPORTED
1990 png_handle_pHYs(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
1993 png_uint_32 res_x
, res_y
;
1996 png_debug(1, "in png_handle_pHYs");
1998 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
1999 png_chunk_error(png_ptr
, "missing IHDR");
2001 else if (png_ptr
->mode
& PNG_HAVE_IDAT
)
2003 png_crc_finish(png_ptr
, length
);
2004 png_chunk_benign_error(png_ptr
, "out of place");
2008 else if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_pHYs
))
2010 png_crc_finish(png_ptr
, length
);
2011 png_chunk_benign_error(png_ptr
, "duplicate");
2017 png_crc_finish(png_ptr
, length
);
2018 png_chunk_benign_error(png_ptr
, "invalid");
2022 png_crc_read(png_ptr
, buf
, 9);
2024 if (png_crc_finish(png_ptr
, 0))
2027 res_x
= png_get_uint_32(buf
);
2028 res_y
= png_get_uint_32(buf
+ 4);
2030 png_set_pHYs(png_ptr
, info_ptr
, res_x
, res_y
, unit_type
);
2034 #ifdef PNG_READ_oFFs_SUPPORTED
2036 png_handle_oFFs(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
2039 png_int_32 offset_x
, offset_y
;
2042 png_debug(1, "in png_handle_oFFs");
2044 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
2045 png_chunk_error(png_ptr
, "missing IHDR");
2047 else if (png_ptr
->mode
& PNG_HAVE_IDAT
)
2049 png_crc_finish(png_ptr
, length
);
2050 png_chunk_benign_error(png_ptr
, "out of place");
2054 else if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_oFFs
))
2056 png_crc_finish(png_ptr
, length
);
2057 png_chunk_benign_error(png_ptr
, "duplicate");
2063 png_crc_finish(png_ptr
, length
);
2064 png_chunk_benign_error(png_ptr
, "invalid");
2068 png_crc_read(png_ptr
, buf
, 9);
2070 if (png_crc_finish(png_ptr
, 0))
2073 offset_x
= png_get_int_32(buf
);
2074 offset_y
= png_get_int_32(buf
+ 4);
2076 png_set_oFFs(png_ptr
, info_ptr
, offset_x
, offset_y
, unit_type
);
2080 #ifdef PNG_READ_pCAL_SUPPORTED
2081 /* Read the pCAL chunk (described in the PNG Extensions document) */
2083 png_handle_pCAL(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
2086 png_byte type
, nparams
;
2087 png_bytep buffer
, buf
, units
, endptr
;
2091 png_debug(1, "in png_handle_pCAL");
2093 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
2094 png_chunk_error(png_ptr
, "missing IHDR");
2096 else if (png_ptr
->mode
& PNG_HAVE_IDAT
)
2098 png_crc_finish(png_ptr
, length
);
2099 png_chunk_benign_error(png_ptr
, "out of place");
2103 else if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_pCAL
))
2105 png_crc_finish(png_ptr
, length
);
2106 png_chunk_benign_error(png_ptr
, "duplicate");
2110 png_debug1(2, "Allocating and reading pCAL chunk data (%u bytes)",
2113 buffer
= png_read_buffer(png_ptr
, length
+1, 2/*silent*/);
2117 png_crc_finish(png_ptr
, length
);
2118 png_chunk_benign_error(png_ptr
, "out of memory");
2122 png_crc_read(png_ptr
, buffer
, length
);
2124 if (png_crc_finish(png_ptr
, 0))
2127 buffer
[length
] = 0; /* Null terminate the last string */
2129 png_debug(3, "Finding end of pCAL purpose string");
2130 for (buf
= buffer
; *buf
; buf
++)
2133 endptr
= buffer
+ length
;
2135 /* We need to have at least 12 bytes after the purpose string
2136 * in order to get the parameter information.
2138 if (endptr
<= buf
+ 12)
2140 png_chunk_benign_error(png_ptr
, "invalid");
2144 png_debug(3, "Reading pCAL X0, X1, type, nparams, and units");
2145 X0
= png_get_int_32((png_bytep
)buf
+1);
2146 X1
= png_get_int_32((png_bytep
)buf
+5);
2151 png_debug(3, "Checking pCAL equation type and number of parameters");
2152 /* Check that we have the right number of parameters for known
2155 if ((type
== PNG_EQUATION_LINEAR
&& nparams
!= 2) ||
2156 (type
== PNG_EQUATION_BASE_E
&& nparams
!= 3) ||
2157 (type
== PNG_EQUATION_ARBITRARY
&& nparams
!= 3) ||
2158 (type
== PNG_EQUATION_HYPERBOLIC
&& nparams
!= 4))
2160 png_chunk_benign_error(png_ptr
, "invalid parameter count");
2164 else if (type
>= PNG_EQUATION_LAST
)
2166 png_chunk_benign_error(png_ptr
, "unrecognized equation type");
2169 for (buf
= units
; *buf
; buf
++)
2170 /* Empty loop to move past the units string. */ ;
2172 png_debug(3, "Allocating pCAL parameters array");
2174 params
= png_voidcast(png_charpp
, png_malloc_warn(png_ptr
,
2175 nparams
* (sizeof (png_charp
))));
2179 png_chunk_benign_error(png_ptr
, "out of memory");
2183 /* Get pointers to the start of each parameter string. */
2184 for (i
= 0; i
< nparams
; i
++)
2186 buf
++; /* Skip the null string terminator from previous parameter. */
2188 png_debug1(3, "Reading pCAL parameter %d", i
);
2190 for (params
[i
] = (png_charp
)buf
; buf
<= endptr
&& *buf
!= 0; buf
++)
2191 /* Empty loop to move past each parameter string */ ;
2193 /* Make sure we haven't run out of data yet */
2196 png_free(png_ptr
, params
);
2197 png_chunk_benign_error(png_ptr
, "invalid data");
2202 png_set_pCAL(png_ptr
, info_ptr
, (png_charp
)buffer
, X0
, X1
, type
, nparams
,
2203 (png_charp
)units
, params
);
2205 png_free(png_ptr
, params
);
2209 #ifdef PNG_READ_sCAL_SUPPORTED
2210 /* Read the sCAL chunk */
2212 png_handle_sCAL(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
2218 png_debug(1, "in png_handle_sCAL");
2220 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
2221 png_chunk_error(png_ptr
, "missing IHDR");
2223 else if (png_ptr
->mode
& PNG_HAVE_IDAT
)
2225 png_crc_finish(png_ptr
, length
);
2226 png_chunk_benign_error(png_ptr
, "out of place");
2230 else if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_sCAL
))
2232 png_crc_finish(png_ptr
, length
);
2233 png_chunk_benign_error(png_ptr
, "duplicate");
2237 /* Need unit type, width, \0, height: minimum 4 bytes */
2238 else if (length
< 4)
2240 png_crc_finish(png_ptr
, length
);
2241 png_chunk_benign_error(png_ptr
, "invalid");
2245 png_debug1(2, "Allocating and reading sCAL chunk data (%u bytes)",
2248 buffer
= png_read_buffer(png_ptr
, length
+1, 2/*silent*/);
2252 png_chunk_benign_error(png_ptr
, "out of memory");
2253 png_crc_finish(png_ptr
, length
);
2257 png_crc_read(png_ptr
, buffer
, length
);
2258 buffer
[length
] = 0; /* Null terminate the last string */
2260 if (png_crc_finish(png_ptr
, 0))
2263 /* Validate the unit. */
2264 if (buffer
[0] != 1 && buffer
[0] != 2)
2266 png_chunk_benign_error(png_ptr
, "invalid unit");
2270 /* Validate the ASCII numbers, need two ASCII numbers separated by
2271 * a '\0' and they need to fit exactly in the chunk data.
2276 if (!png_check_fp_number((png_const_charp
)buffer
, length
, &state
, &i
) ||
2277 i
>= length
|| buffer
[i
++] != 0)
2278 png_chunk_benign_error(png_ptr
, "bad width format");
2280 else if (!PNG_FP_IS_POSITIVE(state
))
2281 png_chunk_benign_error(png_ptr
, "non-positive width");
2285 png_size_t heighti
= i
;
2288 if (!png_check_fp_number((png_const_charp
)buffer
, length
, &state
, &i
) ||
2290 png_chunk_benign_error(png_ptr
, "bad height format");
2292 else if (!PNG_FP_IS_POSITIVE(state
))
2293 png_chunk_benign_error(png_ptr
, "non-positive height");
2296 /* This is the (only) success case. */
2297 png_set_sCAL_s(png_ptr
, info_ptr
, buffer
[0],
2298 (png_charp
)buffer
+1, (png_charp
)buffer
+heighti
);
2303 #ifdef PNG_READ_tIME_SUPPORTED
2305 png_handle_tIME(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
2310 png_debug(1, "in png_handle_tIME");
2312 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
2313 png_chunk_error(png_ptr
, "missing IHDR");
2315 else if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_tIME
))
2317 png_crc_finish(png_ptr
, length
);
2318 png_chunk_benign_error(png_ptr
, "duplicate");
2322 if (png_ptr
->mode
& PNG_HAVE_IDAT
)
2323 png_ptr
->mode
|= PNG_AFTER_IDAT
;
2327 png_crc_finish(png_ptr
, length
);
2328 png_chunk_benign_error(png_ptr
, "invalid");
2332 png_crc_read(png_ptr
, buf
, 7);
2334 if (png_crc_finish(png_ptr
, 0))
2337 mod_time
.second
= buf
[6];
2338 mod_time
.minute
= buf
[5];
2339 mod_time
.hour
= buf
[4];
2340 mod_time
.day
= buf
[3];
2341 mod_time
.month
= buf
[2];
2342 mod_time
.year
= png_get_uint_16(buf
);
2344 png_set_tIME(png_ptr
, info_ptr
, &mod_time
);
2348 #ifdef PNG_READ_tEXt_SUPPORTED
2349 /* Note: this does not properly handle chunks that are > 64K under DOS */
2351 png_handle_tEXt(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
2357 png_uint_32 skip
= 0;
2359 png_debug(1, "in png_handle_tEXt");
2361 #ifdef PNG_USER_LIMITS_SUPPORTED
2362 if (png_ptr
->user_chunk_cache_max
!= 0)
2364 if (png_ptr
->user_chunk_cache_max
== 1)
2366 png_crc_finish(png_ptr
, length
);
2370 if (--png_ptr
->user_chunk_cache_max
== 1)
2372 png_crc_finish(png_ptr
, length
);
2373 png_chunk_benign_error(png_ptr
, "no space in chunk cache");
2379 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
2380 png_chunk_error(png_ptr
, "missing IHDR");
2382 if (png_ptr
->mode
& PNG_HAVE_IDAT
)
2383 png_ptr
->mode
|= PNG_AFTER_IDAT
;
2385 #ifdef PNG_MAX_MALLOC_64K
2386 if (length
> 65535U)
2388 png_crc_finish(png_ptr
, length
);
2389 png_chunk_benign_error(png_ptr
, "too large to fit in memory");
2394 buffer
= png_read_buffer(png_ptr
, length
+1, 1/*warn*/);
2398 png_chunk_benign_error(png_ptr
, "out of memory");
2402 png_crc_read(png_ptr
, buffer
, length
);
2404 if (png_crc_finish(png_ptr
, skip
))
2407 key
= (png_charp
)buffer
;
2410 for (text
= key
; *text
; text
++)
2411 /* Empty loop to find end of key */ ;
2413 if (text
!= key
+ length
)
2416 text_info
.compression
= PNG_TEXT_COMPRESSION_NONE
;
2417 text_info
.key
= key
;
2418 text_info
.lang
= NULL
;
2419 text_info
.lang_key
= NULL
;
2420 text_info
.itxt_length
= 0;
2421 text_info
.text
= text
;
2422 text_info
.text_length
= strlen(text
);
2424 if (png_set_text_2(png_ptr
, info_ptr
, &text_info
, 1))
2425 png_warning(png_ptr
, "Insufficient memory to process text chunk");
2429 #ifdef PNG_READ_zTXt_SUPPORTED
2430 /* Note: this does not correctly handle chunks that are > 64K under DOS */
2432 png_handle_zTXt(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
2434 png_const_charp errmsg
= NULL
;
2436 png_uint_32 keyword_length
;
2438 png_debug(1, "in png_handle_zTXt");
2440 #ifdef PNG_USER_LIMITS_SUPPORTED
2441 if (png_ptr
->user_chunk_cache_max
!= 0)
2443 if (png_ptr
->user_chunk_cache_max
== 1)
2445 png_crc_finish(png_ptr
, length
);
2449 if (--png_ptr
->user_chunk_cache_max
== 1)
2451 png_crc_finish(png_ptr
, length
);
2452 png_chunk_benign_error(png_ptr
, "no space in chunk cache");
2458 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
2459 png_chunk_error(png_ptr
, "missing IHDR");
2461 if (png_ptr
->mode
& PNG_HAVE_IDAT
)
2462 png_ptr
->mode
|= PNG_AFTER_IDAT
;
2464 buffer
= png_read_buffer(png_ptr
, length
, 2/*silent*/);
2468 png_crc_finish(png_ptr
, length
);
2469 png_chunk_benign_error(png_ptr
, "out of memory");
2473 png_crc_read(png_ptr
, buffer
, length
);
2475 if (png_crc_finish(png_ptr
, 0))
2478 /* TODO: also check that the keyword contents match the spec! */
2479 for (keyword_length
= 0;
2480 keyword_length
< length
&& buffer
[keyword_length
] != 0;
2482 /* Empty loop to find end of name */ ;
2484 if (keyword_length
> 79 || keyword_length
< 1)
2485 errmsg
= "bad keyword";
2487 /* zTXt must have some LZ data after the keyword, although it may expand to
2488 * zero bytes; we need a '\0' at the end of the keyword, the compression type
2491 else if (keyword_length
+ 3 > length
)
2492 errmsg
= "truncated";
2494 else if (buffer
[keyword_length
+1] != PNG_COMPRESSION_TYPE_BASE
)
2495 errmsg
= "unknown compression type";
2499 png_alloc_size_t uncompressed_length
= PNG_SIZE_MAX
;
2501 /* TODO: at present png_decompress_chunk imposes a single application
2502 * level memory limit, this should be split to different values for iCCP
2505 if (png_decompress_chunk(png_ptr
, length
, keyword_length
+2,
2506 &uncompressed_length
, 1/*terminate*/) == Z_STREAM_END
)
2510 /* It worked; png_ptr->read_buffer now looks like a tEXt chunk except
2511 * for the extra compression type byte and the fact that it isn't
2512 * necessarily '\0' terminated.
2514 buffer
= png_ptr
->read_buffer
;
2515 buffer
[uncompressed_length
+(keyword_length
+2)] = 0;
2517 text
.compression
= PNG_TEXT_COMPRESSION_zTXt
;
2518 text
.key
= (png_charp
)buffer
;
2519 text
.text
= (png_charp
)(buffer
+ keyword_length
+2);
2520 text
.text_length
= uncompressed_length
;
2521 text
.itxt_length
= 0;
2523 text
.lang_key
= NULL
;
2525 if (png_set_text_2(png_ptr
, info_ptr
, &text
, 1))
2526 errmsg
= "insufficient memory";
2530 errmsg
= png_ptr
->zstream
.msg
;
2534 png_chunk_benign_error(png_ptr
, errmsg
);
2538 #ifdef PNG_READ_iTXt_SUPPORTED
2539 /* Note: this does not correctly handle chunks that are > 64K under DOS */
2541 png_handle_iTXt(png_structrp png_ptr
, png_inforp info_ptr
, png_uint_32 length
)
2543 png_const_charp errmsg
= NULL
;
2545 png_uint_32 prefix_length
;
2547 png_debug(1, "in png_handle_iTXt");
2549 #ifdef PNG_USER_LIMITS_SUPPORTED
2550 if (png_ptr
->user_chunk_cache_max
!= 0)
2552 if (png_ptr
->user_chunk_cache_max
== 1)
2554 png_crc_finish(png_ptr
, length
);
2558 if (--png_ptr
->user_chunk_cache_max
== 1)
2560 png_crc_finish(png_ptr
, length
);
2561 png_chunk_benign_error(png_ptr
, "no space in chunk cache");
2567 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
2568 png_chunk_error(png_ptr
, "missing IHDR");
2570 if (png_ptr
->mode
& PNG_HAVE_IDAT
)
2571 png_ptr
->mode
|= PNG_AFTER_IDAT
;
2573 buffer
= png_read_buffer(png_ptr
, length
+1, 1/*warn*/);
2577 png_crc_finish(png_ptr
, length
);
2578 png_chunk_benign_error(png_ptr
, "out of memory");
2582 png_crc_read(png_ptr
, buffer
, length
);
2584 if (png_crc_finish(png_ptr
, 0))
2587 /* First the keyword. */
2588 for (prefix_length
=0;
2589 prefix_length
< length
&& buffer
[prefix_length
] != 0;
2593 /* Perform a basic check on the keyword length here. */
2594 if (prefix_length
> 79 || prefix_length
< 1)
2595 errmsg
= "bad keyword";
2597 /* Expect keyword, compression flag, compression type, language, translated
2598 * keyword (both may be empty but are 0 terminated) then the text, which may
2601 else if (prefix_length
+ 5 > length
)
2602 errmsg
= "truncated";
2604 else if (buffer
[prefix_length
+1] == 0 ||
2605 (buffer
[prefix_length
+1] == 1 &&
2606 buffer
[prefix_length
+2] == PNG_COMPRESSION_TYPE_BASE
))
2608 int compressed
= buffer
[prefix_length
+1] != 0;
2609 png_uint_32 language_offset
, translated_keyword_offset
;
2610 png_alloc_size_t uncompressed_length
= 0;
2612 /* Now the language tag */
2614 language_offset
= prefix_length
;
2616 for (; prefix_length
< length
&& buffer
[prefix_length
] != 0;
2620 /* WARNING: the length may be invalid here, this is checked below. */
2621 translated_keyword_offset
= ++prefix_length
;
2623 for (; prefix_length
< length
&& buffer
[prefix_length
] != 0;
2627 /* prefix_length should now be at the trailing '\0' of the translated
2628 * keyword, but it may already be over the end. None of this arithmetic
2629 * can overflow because chunks are at most 2^31 bytes long, but on 16-bit
2630 * systems the available allocaton may overflow.
2634 if (!compressed
&& prefix_length
<= length
)
2635 uncompressed_length
= length
- prefix_length
;
2637 else if (compressed
&& prefix_length
< length
)
2639 uncompressed_length
= PNG_SIZE_MAX
;
2641 /* TODO: at present png_decompress_chunk imposes a single application
2642 * level memory limit, this should be split to different values for
2643 * iCCP and text chunks.
2645 if (png_decompress_chunk(png_ptr
, length
, prefix_length
,
2646 &uncompressed_length
, 1/*terminate*/) == Z_STREAM_END
)
2647 buffer
= png_ptr
->read_buffer
;
2650 errmsg
= png_ptr
->zstream
.msg
;
2654 errmsg
= "truncated";
2660 buffer
[uncompressed_length
+prefix_length
] = 0;
2663 text
.compression
= PNG_ITXT_COMPRESSION_NONE
;
2666 text
.compression
= PNG_ITXT_COMPRESSION_zTXt
;
2668 text
.key
= (png_charp
)buffer
;
2669 text
.lang
= (png_charp
)buffer
+ language_offset
;
2670 text
.lang_key
= (png_charp
)buffer
+ translated_keyword_offset
;
2671 text
.text
= (png_charp
)buffer
+ prefix_length
;
2672 text
.text_length
= 0;
2673 text
.itxt_length
= uncompressed_length
;
2675 if (png_set_text_2(png_ptr
, info_ptr
, &text
, 1))
2676 errmsg
= "insufficient memory";
2681 errmsg
= "bad compression info";
2684 png_chunk_benign_error(png_ptr
, errmsg
);
2688 #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
2689 /* Utility function for png_handle_unknown; set up png_ptr::unknown_chunk */
2691 png_cache_unknown_chunk(png_structrp png_ptr
, png_uint_32 length
)
2693 png_alloc_size_t limit
= PNG_SIZE_MAX
;
2695 if (png_ptr
->unknown_chunk
.data
!= NULL
)
2697 png_free(png_ptr
, png_ptr
->unknown_chunk
.data
);
2698 png_ptr
->unknown_chunk
.data
= NULL
;
2701 # ifdef PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED
2702 if (png_ptr
->user_chunk_malloc_max
> 0 &&
2703 png_ptr
->user_chunk_malloc_max
< limit
)
2704 limit
= png_ptr
->user_chunk_malloc_max
;
2706 # elif PNG_USER_CHUNK_MALLOC_MAX > 0
2707 if (PNG_USER_CHUNK_MALLOC_MAX
< limit
)
2708 limit
= PNG_USER_CHUNK_MALLOC_MAX
;
2711 if (length
<= limit
)
2713 PNG_CSTRING_FROM_CHUNK(png_ptr
->unknown_chunk
.name
, png_ptr
->chunk_name
);
2714 /* The following is safe because of the PNG_SIZE_MAX init above */
2715 png_ptr
->unknown_chunk
.size
= (png_size_t
)length
/*SAFE*/;
2716 /* 'mode' is a flag array, only the bottom four bits matter here */
2717 png_ptr
->unknown_chunk
.location
= (png_byte
)png_ptr
->mode
/*SAFE*/;
2720 png_ptr
->unknown_chunk
.data
= NULL
;
2724 /* Do a 'warn' here - it is handled below. */
2725 png_ptr
->unknown_chunk
.data
= png_voidcast(png_bytep
,
2726 png_malloc_warn(png_ptr
, length
));
2730 if (png_ptr
->unknown_chunk
.data
== NULL
&& length
> 0)
2732 /* This is benign because we clean up correctly */
2733 png_crc_finish(png_ptr
, length
);
2734 png_chunk_benign_error(png_ptr
, "unknown chunk exceeds memory limits");
2741 png_crc_read(png_ptr
, png_ptr
->unknown_chunk
.data
, length
);
2742 png_crc_finish(png_ptr
, 0);
2746 #endif /* PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */
2748 /* Handle an unknown, or known but disabled, chunk */
2750 png_handle_unknown(png_structrp png_ptr
, png_inforp info_ptr
,
2751 png_uint_32 length
, int keep
)
2753 int handled
= 0; /* the chunk was handled */
2755 png_debug(1, "in png_handle_unknown");
2757 #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
2758 /* NOTE: this code is based on the code in libpng-1.4.12 except for fixing
2759 * the bug which meant that setting a non-default behavior for a specific
2760 * chunk would be ignored (the default was always used unless a user
2761 * callback was installed).
2763 * 'keep' is the value from the png_chunk_unknown_handling, the setting for
2764 * this specific chunk_name, if PNG_HANDLE_AS_UNKNOWN_SUPPORTED, if not it
2765 * will always be PNG_HANDLE_CHUNK_AS_DEFAULT and it needs to be set here.
2766 * This is just an optimization to avoid multiple calls to the lookup
2769 # ifndef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
2770 # ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
2771 keep
= png_chunk_unknown_handling(png_ptr
, png_ptr
->chunk_name
);
2775 /* One of the following methods will read the chunk or skip it (at least one
2776 * of these is always defined because this is the only way to switch on
2777 * PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
2779 # ifdef PNG_READ_USER_CHUNKS_SUPPORTED
2780 /* The user callback takes precedence over the chunk keep value, but the
2781 * keep value is still required to validate a save of a critical chunk.
2783 if (png_ptr
->read_user_chunk_fn
!= NULL
)
2785 if (png_cache_unknown_chunk(png_ptr
, length
))
2787 /* Callback to user unknown chunk handler */
2788 int ret
= (*(png_ptr
->read_user_chunk_fn
))(png_ptr
,
2789 &png_ptr
->unknown_chunk
);
2792 * negative: An error occured, png_chunk_error will be called.
2793 * zero: The chunk was not handled, the chunk will be discarded
2794 * unless png_set_keep_unknown_chunks has been used to set
2795 * a 'keep' behavior for this particular chunk, in which
2796 * case that will be used. A critical chunk will cause an
2797 * error at this point unless it is to be saved.
2798 * positive: The chunk was handled, libpng will ignore/discard it.
2801 png_chunk_error(png_ptr
, "error in user chunk");
2805 /* If the keep value is 'default' or 'never' override it, but
2806 * still error out on critical chunks unless the keep value is
2807 * 'always' While this is weird it is the behavior in 1.4.12.
2808 * A possible improvement would be to obey the value set for the
2809 * chunk, but this would be an API change that would probably
2810 * damage some applications.
2812 * The png_app_warning below catches the case that matters, where
2813 * the application has not set specific save or ignore for this
2814 * chunk or global save or ignore.
2816 if (keep
< PNG_HANDLE_CHUNK_IF_SAFE
)
2818 # ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
2819 if (png_ptr
->unknown_default
< PNG_HANDLE_CHUNK_IF_SAFE
)
2821 png_chunk_warning(png_ptr
, "Saving unknown chunk:");
2822 png_app_warning(png_ptr
,
2823 "forcing save of an unhandled chunk;"
2824 " please call png_set_keep_unknown_chunks");
2825 /* with keep = PNG_HANDLE_CHUNK_IF_SAFE */
2828 keep
= PNG_HANDLE_CHUNK_IF_SAFE
;
2832 else /* chunk was handled */
2835 /* Critical chunks can be safely discarded at this point. */
2836 keep
= PNG_HANDLE_CHUNK_NEVER
;
2841 keep
= PNG_HANDLE_CHUNK_NEVER
; /* insufficient memory */
2845 /* Use the SAVE_UNKNOWN_CHUNKS code or skip the chunk */
2846 # endif /* PNG_READ_USER_CHUNKS_SUPPORTED */
2848 # ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
2850 /* keep is currently just the per-chunk setting, if there was no
2851 * setting change it to the global default now (not that this may
2852 * still be AS_DEFAULT) then obtain the cache of the chunk if required,
2853 * if not simply skip the chunk.
2855 if (keep
== PNG_HANDLE_CHUNK_AS_DEFAULT
)
2856 keep
= png_ptr
->unknown_default
;
2858 if (keep
== PNG_HANDLE_CHUNK_ALWAYS
||
2859 (keep
== PNG_HANDLE_CHUNK_IF_SAFE
&&
2860 PNG_CHUNK_ANCILLARY(png_ptr
->chunk_name
)))
2862 if (!png_cache_unknown_chunk(png_ptr
, length
))
2863 keep
= PNG_HANDLE_CHUNK_NEVER
;
2867 png_crc_finish(png_ptr
, length
);
2870 # ifndef PNG_READ_USER_CHUNKS_SUPPORTED
2871 # error no method to support READ_UNKNOWN_CHUNKS
2875 /* If here there is no read callback pointer set and no support is
2876 * compiled in to just save the unknown chunks, so simply skip this
2877 * chunk. If 'keep' is something other than AS_DEFAULT or NEVER then
2878 * the app has erroneously asked for unknown chunk saving when there
2881 if (keep
> PNG_HANDLE_CHUNK_NEVER
)
2882 png_app_error(png_ptr
, "no unknown chunk support available");
2884 png_crc_finish(png_ptr
, length
);
2888 # ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
2889 /* Now store the chunk in the chunk list if appropriate, and if the limits
2892 if (keep
== PNG_HANDLE_CHUNK_ALWAYS
||
2893 (keep
== PNG_HANDLE_CHUNK_IF_SAFE
&&
2894 PNG_CHUNK_ANCILLARY(png_ptr
->chunk_name
)))
2896 # ifdef PNG_USER_LIMITS_SUPPORTED
2897 switch (png_ptr
->user_chunk_cache_max
)
2900 png_ptr
->user_chunk_cache_max
= 1;
2901 png_chunk_benign_error(png_ptr
, "no space in chunk cache");
2904 /* NOTE: prior to 1.6.0 this case resulted in an unknown critical
2905 * chunk being skipped, now there will be a hard error below.
2909 default: /* not at limit */
2910 --(png_ptr
->user_chunk_cache_max
);
2912 case 0: /* no limit */
2913 # endif /* PNG_USER_LIMITS_SUPPORTED */
2914 /* Here when the limit isn't reached or when limits are compiled
2915 * out; store the chunk.
2917 png_set_unknown_chunks(png_ptr
, info_ptr
,
2918 &png_ptr
->unknown_chunk
, 1);
2920 # ifdef PNG_USER_LIMITS_SUPPORTED
2925 # else /* no store support! */
2926 PNG_UNUSED(info_ptr
)
2927 # error untested code (reading unknown chunks with no store support)
2930 /* Regardless of the error handling below the cached data (if any) can be
2931 * freed now. Notice that the data is not freed if there is a png_error, but
2932 * it will be freed by destroy_read_struct.
2934 if (png_ptr
->unknown_chunk
.data
!= NULL
)
2935 png_free(png_ptr
, png_ptr
->unknown_chunk
.data
);
2936 png_ptr
->unknown_chunk
.data
= NULL
;
2938 #else /* !PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */
2939 /* There is no support to read an unknown chunk, so just skip it. */
2940 png_crc_finish(png_ptr
, length
);
2941 PNG_UNUSED(info_ptr
)
2943 #endif /* !PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */
2945 /* Check for unhandled critical chunks */
2946 if (!handled
&& PNG_CHUNK_CRITICAL(png_ptr
->chunk_name
))
2947 png_chunk_error(png_ptr
, "unhandled critical chunk");
2950 /* This function is called to verify that a chunk name is valid.
2951 * This function can't have the "critical chunk check" incorporated
2952 * into it, since in the future we will need to be able to call user
2953 * functions to handle unknown critical chunks after we check that
2954 * the chunk name itself is valid.
2957 /* Bit hacking: the test for an invalid byte in the 4 byte chunk name is:
2959 * ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
2963 png_check_chunk_name(png_structrp png_ptr
, png_uint_32 chunk_name
)
2967 png_debug(1, "in png_check_chunk_name");
2969 for (i
=1; i
<=4; ++i
)
2971 int c
= chunk_name
& 0xff;
2973 if (c
< 65 || c
> 122 || (c
> 90 && c
< 97))
2974 png_chunk_error(png_ptr
, "invalid chunk type");
2980 /* Combines the row recently read in with the existing pixels in the row. This
2981 * routine takes care of alpha and transparency if requested. This routine also
2982 * handles the two methods of progressive display of interlaced images,
2983 * depending on the 'display' value; if 'display' is true then the whole row
2984 * (dp) is filled from the start by replicating the available pixels. If
2985 * 'display' is false only those pixels present in the pass are filled in.
2988 png_combine_row(png_const_structrp png_ptr
, png_bytep dp
, int display
)
2990 unsigned int pixel_depth
= png_ptr
->transformed_pixel_depth
;
2991 png_const_bytep sp
= png_ptr
->row_buf
+ 1;
2992 png_uint_32 row_width
= png_ptr
->width
;
2993 unsigned int pass
= png_ptr
->pass
;
2994 png_bytep end_ptr
= 0;
2995 png_byte end_byte
= 0;
2996 unsigned int end_mask
;
2998 png_debug(1, "in png_combine_row");
3000 /* Added in 1.5.6: it should not be possible to enter this routine until at
3001 * least one row has been read from the PNG data and transformed.
3003 if (pixel_depth
== 0)
3004 png_error(png_ptr
, "internal row logic error");
3006 /* Added in 1.5.4: the pixel depth should match the information returned by
3007 * any call to png_read_update_info at this point. Do not continue if we got
3010 if (png_ptr
->info_rowbytes
!= 0 && png_ptr
->info_rowbytes
!=
3011 PNG_ROWBYTES(pixel_depth
, row_width
))
3012 png_error(png_ptr
, "internal row size calculation error");
3014 /* Don't expect this to ever happen: */
3016 png_error(png_ptr
, "internal row width error");
3018 /* Preserve the last byte in cases where only part of it will be overwritten,
3019 * the multiply below may overflow, we don't care because ANSI-C guarantees
3020 * we get the low bits.
3022 end_mask
= (pixel_depth
* row_width
) & 7;
3025 /* end_ptr == NULL is a flag to say do nothing */
3026 end_ptr
= dp
+ PNG_ROWBYTES(pixel_depth
, row_width
) - 1;
3027 end_byte
= *end_ptr
;
3028 # ifdef PNG_READ_PACKSWAP_SUPPORTED
3029 if (png_ptr
->transformations
& PNG_PACKSWAP
) /* little-endian byte */
3030 end_mask
= 0xff << end_mask
;
3032 else /* big-endian byte */
3034 end_mask
= 0xff >> end_mask
;
3035 /* end_mask is now the bits to *keep* from the destination row */
3038 /* For non-interlaced images this reduces to a memcpy(). A memcpy()
3039 * will also happen if interlacing isn't supported or if the application
3040 * does not call png_set_interlace_handling(). In the latter cases the
3041 * caller just gets a sequence of the unexpanded rows from each interlace
3044 #ifdef PNG_READ_INTERLACING_SUPPORTED
3045 if (png_ptr
->interlaced
&& (png_ptr
->transformations
& PNG_INTERLACE
) &&
3046 pass
< 6 && (display
== 0 ||
3047 /* The following copies everything for 'display' on passes 0, 2 and 4. */
3048 (display
== 1 && (pass
& 1) != 0)))
3050 /* Narrow images may have no bits in a pass; the caller should handle
3051 * this, but this test is cheap:
3053 if (row_width
<= PNG_PASS_START_COL(pass
))
3056 if (pixel_depth
< 8)
3058 /* For pixel depths up to 4 bpp the 8-pixel mask can be expanded to fit
3059 * into 32 bits, then a single loop over the bytes using the four byte
3060 * values in the 32-bit mask can be used. For the 'display' option the
3061 * expanded mask may also not require any masking within a byte. To
3062 * make this work the PACKSWAP option must be taken into account - it
3063 * simply requires the pixels to be reversed in each byte.
3065 * The 'regular' case requires a mask for each of the first 6 passes,
3066 * the 'display' case does a copy for the even passes in the range
3067 * 0..6. This has already been handled in the test above.
3069 * The masks are arranged as four bytes with the first byte to use in
3070 * the lowest bits (little-endian) regardless of the order (PACKSWAP or
3071 * not) of the pixels in each byte.
3073 * NOTE: the whole of this logic depends on the caller of this function
3074 * only calling it on rows appropriate to the pass. This function only
3075 * understands the 'x' logic; the 'y' logic is handled by the caller.
3077 * The following defines allow generation of compile time constant bit
3078 * masks for each pixel depth and each possibility of swapped or not
3079 * swapped bytes. Pass 'p' is in the range 0..6; 'x', a pixel index,
3080 * is in the range 0..7; and the result is 1 if the pixel is to be
3081 * copied in the pass, 0 if not. 'S' is for the sparkle method, 'B'
3082 * for the block method.
3084 * With some compilers a compile time expression of the general form:
3086 * (shift >= 32) ? (a >> (shift-32)) : (b >> shift)
3088 * Produces warnings with values of 'shift' in the range 33 to 63
3089 * because the right hand side of the ?: expression is evaluated by
3090 * the compiler even though it isn't used. Microsoft Visual C (various
3091 * versions) and the Intel C compiler are known to do this. To avoid
3092 * this the following macros are used in 1.5.6. This is a temporary
3093 * solution to avoid destabilizing the code during the release process.
3095 # if PNG_USE_COMPILE_TIME_MASKS
3096 # define PNG_LSR(x,s) ((x)>>((s) & 0x1f))
3097 # define PNG_LSL(x,s) ((x)<<((s) & 0x1f))
3099 # define PNG_LSR(x,s) ((x)>>(s))
3100 # define PNG_LSL(x,s) ((x)<<(s))
3102 # define S_COPY(p,x) (((p)<4 ? PNG_LSR(0x80088822,(3-(p))*8+(7-(x))) :\
3103 PNG_LSR(0xaa55ff00,(7-(p))*8+(7-(x)))) & 1)
3104 # define B_COPY(p,x) (((p)<4 ? PNG_LSR(0xff0fff33,(3-(p))*8+(7-(x))) :\
3105 PNG_LSR(0xff55ff00,(7-(p))*8+(7-(x)))) & 1)
3107 /* Return a mask for pass 'p' pixel 'x' at depth 'd'. The mask is
3108 * little endian - the first pixel is at bit 0 - however the extra
3109 * parameter 's' can be set to cause the mask position to be swapped
3110 * within each byte, to match the PNG format. This is done by XOR of
3111 * the shift with 7, 6 or 4 for bit depths 1, 2 and 4.
3113 # define PIXEL_MASK(p,x,d,s) \
3114 (PNG_LSL(((PNG_LSL(1U,(d)))-1),(((x)*(d))^((s)?8-(d):0))))
3116 /* Hence generate the appropriate 'block' or 'sparkle' pixel copy mask.
3118 # define S_MASKx(p,x,d,s) (S_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
3119 # define B_MASKx(p,x,d,s) (B_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
3121 /* Combine 8 of these to get the full mask. For the 1-bpp and 2-bpp
3122 * cases the result needs replicating, for the 4-bpp case the above
3123 * generates a full 32 bits.
3125 # define MASK_EXPAND(m,d) ((m)*((d)==1?0x01010101:((d)==2?0x00010001:1)))
3127 # define S_MASK(p,d,s) MASK_EXPAND(S_MASKx(p,0,d,s) + S_MASKx(p,1,d,s) +\
3128 S_MASKx(p,2,d,s) + S_MASKx(p,3,d,s) + S_MASKx(p,4,d,s) +\
3129 S_MASKx(p,5,d,s) + S_MASKx(p,6,d,s) + S_MASKx(p,7,d,s), d)
3131 # define B_MASK(p,d,s) MASK_EXPAND(B_MASKx(p,0,d,s) + B_MASKx(p,1,d,s) +\
3132 B_MASKx(p,2,d,s) + B_MASKx(p,3,d,s) + B_MASKx(p,4,d,s) +\
3133 B_MASKx(p,5,d,s) + B_MASKx(p,6,d,s) + B_MASKx(p,7,d,s), d)
3135 #if PNG_USE_COMPILE_TIME_MASKS
3136 /* Utility macros to construct all the masks for a depth/swap
3137 * combination. The 's' parameter says whether the format is PNG
3138 * (big endian bytes) or not. Only the three odd-numbered passes are
3139 * required for the display/block algorithm.
3141 # define S_MASKS(d,s) { S_MASK(0,d,s), S_MASK(1,d,s), S_MASK(2,d,s),\
3142 S_MASK(3,d,s), S_MASK(4,d,s), S_MASK(5,d,s) }
3144 # define B_MASKS(d,s) { B_MASK(1,d,s), S_MASK(3,d,s), S_MASK(5,d,s) }
3146 # define DEPTH_INDEX(d) ((d)==1?0:((d)==2?1:2))
3148 /* Hence the pre-compiled masks indexed by PACKSWAP (or not), depth and
3151 static PNG_CONST png_uint_32 row_mask
[2/*PACKSWAP*/][3/*depth*/][6] =
3153 /* Little-endian byte masks for PACKSWAP */
3154 { S_MASKS(1,0), S_MASKS(2,0), S_MASKS(4,0) },
3155 /* Normal (big-endian byte) masks - PNG format */
3156 { S_MASKS(1,1), S_MASKS(2,1), S_MASKS(4,1) }
3159 /* display_mask has only three entries for the odd passes, so index by
3162 static PNG_CONST png_uint_32 display_mask
[2][3][3] =
3164 /* Little-endian byte masks for PACKSWAP */
3165 { B_MASKS(1,0), B_MASKS(2,0), B_MASKS(4,0) },
3166 /* Normal (big-endian byte) masks - PNG format */
3167 { B_MASKS(1,1), B_MASKS(2,1), B_MASKS(4,1) }
3170 # define MASK(pass,depth,display,png)\
3171 ((display)?display_mask[png][DEPTH_INDEX(depth)][pass>>1]:\
3172 row_mask[png][DEPTH_INDEX(depth)][pass])
3174 #else /* !PNG_USE_COMPILE_TIME_MASKS */
3175 /* This is the runtime alternative: it seems unlikely that this will
3176 * ever be either smaller or faster than the compile time approach.
3178 # define MASK(pass,depth,display,png)\
3179 ((display)?B_MASK(pass,depth,png):S_MASK(pass,depth,png))
3180 #endif /* !PNG_USE_COMPILE_TIME_MASKS */
3182 /* Use the appropriate mask to copy the required bits. In some cases
3183 * the byte mask will be 0 or 0xff, optimize these cases. row_width is
3184 * the number of pixels, but the code copies bytes, so it is necessary
3185 * to special case the end.
3187 png_uint_32 pixels_per_byte
= 8 / pixel_depth
;
3190 # ifdef PNG_READ_PACKSWAP_SUPPORTED
3191 if (png_ptr
->transformations
& PNG_PACKSWAP
)
3192 mask
= MASK(pass
, pixel_depth
, display
, 0);
3196 mask
= MASK(pass
, pixel_depth
, display
, 1);
3202 /* It doesn't matter in the following if png_uint_32 has more than
3203 * 32 bits because the high bits always match those in m<<24; it is,
3204 * however, essential to use OR here, not +, because of this.
3207 mask
= (m
>> 8) | (m
<< 24); /* rotate right to good compilers */
3210 if (m
!= 0) /* something to copy */
3213 *dp
= (png_byte
)((*dp
& ~m
) | (*sp
& m
));
3218 /* NOTE: this may overwrite the last byte with garbage if the image
3219 * is not an exact number of bytes wide; libpng has always done
3222 if (row_width
<= pixels_per_byte
)
3223 break; /* May need to restore part of the last byte */
3225 row_width
-= pixels_per_byte
;
3231 else /* pixel_depth >= 8 */
3233 unsigned int bytes_to_copy
, bytes_to_jump
;
3235 /* Validate the depth - it must be a multiple of 8 */
3236 if (pixel_depth
& 7)
3237 png_error(png_ptr
, "invalid user transform pixel depth");
3239 pixel_depth
>>= 3; /* now in bytes */
3240 row_width
*= pixel_depth
;
3242 /* Regardless of pass number the Adam 7 interlace always results in a
3243 * fixed number of pixels to copy then to skip. There may be a
3244 * different number of pixels to skip at the start though.
3247 unsigned int offset
= PNG_PASS_START_COL(pass
) * pixel_depth
;
3249 row_width
-= offset
;
3254 /* Work out the bytes to copy. */
3257 /* When doing the 'block' algorithm the pixel in the pass gets
3258 * replicated to adjacent pixels. This is why the even (0,2,4,6)
3259 * passes are skipped above - the entire expanded row is copied.
3261 bytes_to_copy
= (1<<((6-pass
)>>1)) * pixel_depth
;
3263 /* But don't allow this number to exceed the actual row width. */
3264 if (bytes_to_copy
> row_width
)
3265 bytes_to_copy
= row_width
;
3268 else /* normal row; Adam7 only ever gives us one pixel to copy. */
3269 bytes_to_copy
= pixel_depth
;
3271 /* In Adam7 there is a constant offset between where the pixels go. */
3272 bytes_to_jump
= PNG_PASS_COL_OFFSET(pass
) * pixel_depth
;
3274 /* And simply copy these bytes. Some optimization is possible here,
3275 * depending on the value of 'bytes_to_copy'. Special case the low
3276 * byte counts, which we know to be frequent.
3278 * Notice that these cases all 'return' rather than 'break' - this
3279 * avoids an unnecessary test on whether to restore the last byte
3282 switch (bytes_to_copy
)
3289 if (row_width
<= bytes_to_jump
)
3292 dp
+= bytes_to_jump
;
3293 sp
+= bytes_to_jump
;
3294 row_width
-= bytes_to_jump
;
3298 /* There is a possibility of a partial copy at the end here; this
3299 * slows the code down somewhat.
3303 dp
[0] = sp
[0], dp
[1] = sp
[1];
3305 if (row_width
<= bytes_to_jump
)
3308 sp
+= bytes_to_jump
;
3309 dp
+= bytes_to_jump
;
3310 row_width
-= bytes_to_jump
;
3312 while (row_width
> 1);
3314 /* And there can only be one byte left at this point: */
3319 /* This can only be the RGB case, so each copy is exactly one
3320 * pixel and it is not necessary to check for a partial copy.
3324 dp
[0] = sp
[0], dp
[1] = sp
[1], dp
[2] = sp
[2];
3326 if (row_width
<= bytes_to_jump
)
3329 sp
+= bytes_to_jump
;
3330 dp
+= bytes_to_jump
;
3331 row_width
-= bytes_to_jump
;
3335 #if PNG_ALIGN_TYPE != PNG_ALIGN_NONE
3336 /* Check for double byte alignment and, if possible, use a
3337 * 16-bit copy. Don't attempt this for narrow images - ones that
3338 * are less than an interlace panel wide. Don't attempt it for
3339 * wide bytes_to_copy either - use the memcpy there.
3341 if (bytes_to_copy
< 16 /*else use memcpy*/ &&
3342 png_isaligned(dp
, png_uint_16
) &&
3343 png_isaligned(sp
, png_uint_16
) &&
3344 bytes_to_copy
% (sizeof (png_uint_16
)) == 0 &&
3345 bytes_to_jump
% (sizeof (png_uint_16
)) == 0)
3347 /* Everything is aligned for png_uint_16 copies, but try for
3348 * png_uint_32 first.
3350 if (png_isaligned(dp
, png_uint_32
) &&
3351 png_isaligned(sp
, png_uint_32
) &&
3352 bytes_to_copy
% (sizeof (png_uint_32
)) == 0 &&
3353 bytes_to_jump
% (sizeof (png_uint_32
)) == 0)
3355 png_uint_32p dp32
= png_aligncast(png_uint_32p
,dp
);
3356 png_const_uint_32p sp32
= png_aligncastconst(
3357 png_const_uint_32p
, sp
);
3358 size_t skip
= (bytes_to_jump
-bytes_to_copy
) /
3359 (sizeof (png_uint_32
));
3363 size_t c
= bytes_to_copy
;
3367 c
-= (sizeof (png_uint_32
));
3371 if (row_width
<= bytes_to_jump
)
3376 row_width
-= bytes_to_jump
;
3378 while (bytes_to_copy
<= row_width
);
3380 /* Get to here when the row_width truncates the final copy.
3381 * There will be 1-3 bytes left to copy, so don't try the
3382 * 16-bit loop below.
3384 dp
= (png_bytep
)dp32
;
3385 sp
= (png_const_bytep
)sp32
;
3388 while (--row_width
> 0);
3392 /* Else do it in 16-bit quantities, but only if the size is
3397 png_uint_16p dp16
= png_aligncast(png_uint_16p
, dp
);
3398 png_const_uint_16p sp16
= png_aligncastconst(
3399 png_const_uint_16p
, sp
);
3400 size_t skip
= (bytes_to_jump
-bytes_to_copy
) /
3401 (sizeof (png_uint_16
));
3405 size_t c
= bytes_to_copy
;
3409 c
-= (sizeof (png_uint_16
));
3413 if (row_width
<= bytes_to_jump
)
3418 row_width
-= bytes_to_jump
;
3420 while (bytes_to_copy
<= row_width
);
3422 /* End of row - 1 byte left, bytes_to_copy > row_width: */
3423 dp
= (png_bytep
)dp16
;
3424 sp
= (png_const_bytep
)sp16
;
3427 while (--row_width
> 0);
3431 #endif /* PNG_ALIGN_ code */
3433 /* The true default - use a memcpy: */
3436 memcpy(dp
, sp
, bytes_to_copy
);
3438 if (row_width
<= bytes_to_jump
)
3441 sp
+= bytes_to_jump
;
3442 dp
+= bytes_to_jump
;
3443 row_width
-= bytes_to_jump
;
3444 if (bytes_to_copy
> row_width
)
3445 bytes_to_copy
= row_width
;
3450 } /* pixel_depth >= 8 */
3452 /* Here if pixel_depth < 8 to check 'end_ptr' below. */
3457 /* If here then the switch above wasn't used so just memcpy the whole row
3458 * from the temporary row buffer (notice that this overwrites the end of the
3459 * destination row if it is a partial byte.)
3461 memcpy(dp
, sp
, PNG_ROWBYTES(pixel_depth
, row_width
));
3463 /* Restore the overwritten bits from the last byte if necessary. */
3464 if (end_ptr
!= NULL
)
3465 *end_ptr
= (png_byte
)((end_byte
& end_mask
) | (*end_ptr
& ~end_mask
));
3468 #ifdef PNG_READ_INTERLACING_SUPPORTED
3470 png_do_read_interlace(png_row_infop row_info
, png_bytep row
, int pass
,
3471 png_uint_32 transformations
/* Because these may affect the byte layout */)
3473 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
3474 /* Offset to next interlace block */
3475 static PNG_CONST
int png_pass_inc
[7] = {8, 8, 4, 4, 2, 2, 1};
3477 png_debug(1, "in png_do_read_interlace");
3478 if (row
!= NULL
&& row_info
!= NULL
)
3480 png_uint_32 final_width
;
3482 final_width
= row_info
->width
* png_pass_inc
[pass
];
3484 switch (row_info
->pixel_depth
)
3488 png_bytep sp
= row
+ (png_size_t
)((row_info
->width
- 1) >> 3);
3489 png_bytep dp
= row
+ (png_size_t
)((final_width
- 1) >> 3);
3491 int s_start
, s_end
, s_inc
;
3492 int jstop
= png_pass_inc
[pass
];
3497 #ifdef PNG_READ_PACKSWAP_SUPPORTED
3498 if (transformations
& PNG_PACKSWAP
)
3500 sshift
= (int)((row_info
->width
+ 7) & 0x07);
3501 dshift
= (int)((final_width
+ 7) & 0x07);
3510 sshift
= 7 - (int)((row_info
->width
+ 7) & 0x07);
3511 dshift
= 7 - (int)((final_width
+ 7) & 0x07);
3517 for (i
= 0; i
< row_info
->width
; i
++)
3519 v
= (png_byte
)((*sp
>> sshift
) & 0x01);
3520 for (j
= 0; j
< jstop
; j
++)
3522 unsigned int tmp
= *dp
& (0x7f7f >> (7 - dshift
));
3524 *dp
= (png_byte
)(tmp
& 0xff);
3526 if (dshift
== s_end
)
3536 if (sshift
== s_end
)
3550 png_bytep sp
= row
+ (png_uint_32
)((row_info
->width
- 1) >> 2);
3551 png_bytep dp
= row
+ (png_uint_32
)((final_width
- 1) >> 2);
3553 int s_start
, s_end
, s_inc
;
3554 int jstop
= png_pass_inc
[pass
];
3557 #ifdef PNG_READ_PACKSWAP_SUPPORTED
3558 if (transformations
& PNG_PACKSWAP
)
3560 sshift
= (int)(((row_info
->width
+ 3) & 0x03) << 1);
3561 dshift
= (int)(((final_width
+ 3) & 0x03) << 1);
3570 sshift
= (int)((3 - ((row_info
->width
+ 3) & 0x03)) << 1);
3571 dshift
= (int)((3 - ((final_width
+ 3) & 0x03)) << 1);
3577 for (i
= 0; i
< row_info
->width
; i
++)
3582 v
= (png_byte
)((*sp
>> sshift
) & 0x03);
3583 for (j
= 0; j
< jstop
; j
++)
3585 unsigned int tmp
= *dp
& (0x3f3f >> (6 - dshift
));
3587 *dp
= (png_byte
)(tmp
& 0xff);
3589 if (dshift
== s_end
)
3599 if (sshift
== s_end
)
3613 png_bytep sp
= row
+ (png_size_t
)((row_info
->width
- 1) >> 1);
3614 png_bytep dp
= row
+ (png_size_t
)((final_width
- 1) >> 1);
3616 int s_start
, s_end
, s_inc
;
3618 int jstop
= png_pass_inc
[pass
];
3620 #ifdef PNG_READ_PACKSWAP_SUPPORTED
3621 if (transformations
& PNG_PACKSWAP
)
3623 sshift
= (int)(((row_info
->width
+ 1) & 0x01) << 2);
3624 dshift
= (int)(((final_width
+ 1) & 0x01) << 2);
3633 sshift
= (int)((1 - ((row_info
->width
+ 1) & 0x01)) << 2);
3634 dshift
= (int)((1 - ((final_width
+ 1) & 0x01)) << 2);
3640 for (i
= 0; i
< row_info
->width
; i
++)
3642 png_byte v
= (png_byte
)((*sp
>> sshift
) & 0x0f);
3645 for (j
= 0; j
< jstop
; j
++)
3647 unsigned int tmp
= *dp
& (0xf0f >> (4 - dshift
));
3649 *dp
= (png_byte
)(tmp
& 0xff);
3651 if (dshift
== s_end
)
3661 if (sshift
== s_end
)
3675 png_size_t pixel_bytes
= (row_info
->pixel_depth
>> 3);
3677 png_bytep sp
= row
+ (png_size_t
)(row_info
->width
- 1)
3680 png_bytep dp
= row
+ (png_size_t
)(final_width
- 1) * pixel_bytes
;
3682 int jstop
= png_pass_inc
[pass
];
3685 for (i
= 0; i
< row_info
->width
; i
++)
3690 memcpy(v
, sp
, pixel_bytes
);
3692 for (j
= 0; j
< jstop
; j
++)
3694 memcpy(dp
, v
, pixel_bytes
);
3704 row_info
->width
= final_width
;
3705 row_info
->rowbytes
= PNG_ROWBYTES(row_info
->pixel_depth
, final_width
);
3707 #ifndef PNG_READ_PACKSWAP_SUPPORTED
3708 PNG_UNUSED(transformations
) /* Silence compiler warning */
3711 #endif /* PNG_READ_INTERLACING_SUPPORTED */
3714 png_read_filter_row_sub(png_row_infop row_info
, png_bytep row
,
3715 png_const_bytep prev_row
)
3718 png_size_t istop
= row_info
->rowbytes
;
3719 unsigned int bpp
= (row_info
->pixel_depth
+ 7) >> 3;
3720 png_bytep rp
= row
+ bpp
;
3722 PNG_UNUSED(prev_row
)
3724 for (i
= bpp
; i
< istop
; i
++)
3726 *rp
= (png_byte
)(((int)(*rp
) + (int)(*(rp
-bpp
))) & 0xff);
3732 png_read_filter_row_up(png_row_infop row_info
, png_bytep row
,
3733 png_const_bytep prev_row
)
3736 png_size_t istop
= row_info
->rowbytes
;
3738 png_const_bytep pp
= prev_row
;
3740 for (i
= 0; i
< istop
; i
++)
3742 *rp
= (png_byte
)(((int)(*rp
) + (int)(*pp
++)) & 0xff);
3748 png_read_filter_row_avg(png_row_infop row_info
, png_bytep row
,
3749 png_const_bytep prev_row
)
3753 png_const_bytep pp
= prev_row
;
3754 unsigned int bpp
= (row_info
->pixel_depth
+ 7) >> 3;
3755 png_size_t istop
= row_info
->rowbytes
- bpp
;
3757 for (i
= 0; i
< bpp
; i
++)
3759 *rp
= (png_byte
)(((int)(*rp
) +
3760 ((int)(*pp
++) / 2 )) & 0xff);
3765 for (i
= 0; i
< istop
; i
++)
3767 *rp
= (png_byte
)(((int)(*rp
) +
3768 (int)(*pp
++ + *(rp
-bpp
)) / 2 ) & 0xff);
3775 png_read_filter_row_paeth_1byte_pixel(png_row_infop row_info
, png_bytep row
,
3776 png_const_bytep prev_row
)
3778 png_bytep rp_end
= row
+ row_info
->rowbytes
;
3781 /* First pixel/byte */
3784 *row
++ = (png_byte
)a
;
3787 while (row
< rp_end
)
3789 int b
, pa
, pb
, pc
, p
;
3791 a
&= 0xff; /* From previous iteration or start */
3802 pa
= p
< 0 ? -p
: p
;
3803 pb
= pc
< 0 ? -pc
: pc
;
3804 pc
= (p
+ pc
) < 0 ? -(p
+ pc
) : p
+ pc
;
3807 /* Find the best predictor, the least of pa, pb, pc favoring the earlier
3808 * ones in the case of a tie.
3810 if (pb
< pa
) pa
= pb
, a
= b
;
3813 /* Calculate the current pixel in a, and move the previous row pixel to c
3814 * for the next time round the loop
3818 *row
++ = (png_byte
)a
;
3823 png_read_filter_row_paeth_multibyte_pixel(png_row_infop row_info
, png_bytep row
,
3824 png_const_bytep prev_row
)
3826 int bpp
= (row_info
->pixel_depth
+ 7) >> 3;
3827 png_bytep rp_end
= row
+ bpp
;
3829 /* Process the first pixel in the row completely (this is the same as 'up'
3830 * because there is only one candidate predictor for the first row).
3832 while (row
< rp_end
)
3834 int a
= *row
+ *prev_row
++;
3835 *row
++ = (png_byte
)a
;
3839 rp_end
+= row_info
->rowbytes
- bpp
;
3841 while (row
< rp_end
)
3843 int a
, b
, c
, pa
, pb
, pc
, p
;
3845 c
= *(prev_row
- bpp
);
3857 pa
= p
< 0 ? -p
: p
;
3858 pb
= pc
< 0 ? -pc
: pc
;
3859 pc
= (p
+ pc
) < 0 ? -(p
+ pc
) : p
+ pc
;
3862 if (pb
< pa
) pa
= pb
, a
= b
;
3867 *row
++ = (png_byte
)a
;
3872 png_init_filter_functions(png_structrp pp
)
3873 /* This function is called once for every PNG image to set the
3874 * implementations required to reverse the filtering of PNG rows. Reversing
3875 * the filter is the first transformation performed on the row data. It is
3876 * performed in place, therefore an implementation can be selected based on
3877 * the image pixel format. If the implementation depends on image width then
3878 * take care to ensure that it works correctly if the image is interlaced -
3879 * interlacing causes the actual row width to vary.
3882 unsigned int bpp
= (pp
->pixel_depth
+ 7) >> 3;
3884 pp
->read_filter
[PNG_FILTER_VALUE_SUB
-1] = png_read_filter_row_sub
;
3885 pp
->read_filter
[PNG_FILTER_VALUE_UP
-1] = png_read_filter_row_up
;
3886 pp
->read_filter
[PNG_FILTER_VALUE_AVG
-1] = png_read_filter_row_avg
;
3888 pp
->read_filter
[PNG_FILTER_VALUE_PAETH
-1] =
3889 png_read_filter_row_paeth_1byte_pixel
;
3891 pp
->read_filter
[PNG_FILTER_VALUE_PAETH
-1] =
3892 png_read_filter_row_paeth_multibyte_pixel
;
3894 #ifdef PNG_FILTER_OPTIMIZATIONS
3895 /* To use this define PNG_FILTER_OPTIMIZATIONS as the name of a function to
3896 * call to install hardware optimizations for the above functions; simply
3897 * replace whatever elements of the pp->read_filter[] array with a hardware
3898 * specific (or, for that matter, generic) optimization.
3900 * To see an example of this examine what configure.ac does when
3901 * --enable-arm-neon is specified on the command line.
3903 PNG_FILTER_OPTIMIZATIONS(pp
, bpp
);
3908 png_read_filter_row(png_structrp pp
, png_row_infop row_info
, png_bytep row
,
3909 png_const_bytep prev_row
, int filter
)
3911 /* OPTIMIZATION: DO NOT MODIFY THIS FUNCTION, instead #define
3912 * PNG_FILTER_OPTIMIZATIONS to a function that overrides the generic
3913 * implementations. See png_init_filter_functions above.
3915 if (pp
->read_filter
[0] == NULL
)
3916 png_init_filter_functions(pp
);
3917 if (filter
> PNG_FILTER_VALUE_NONE
&& filter
< PNG_FILTER_VALUE_LAST
)
3918 pp
->read_filter
[filter
-1](row_info
, row
, prev_row
);
3921 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
3923 png_read_IDAT_data(png_structrp png_ptr
, png_bytep output
,
3924 png_alloc_size_t avail_out
)
3926 /* Loop reading IDATs and decompressing the result into output[avail_out] */
3927 png_ptr
->zstream
.next_out
= output
;
3928 png_ptr
->zstream
.avail_out
= 0; /* safety: set below */
3936 png_byte tmpbuf
[PNG_INFLATE_BUF_SIZE
];
3938 if (png_ptr
->zstream
.avail_in
== 0)
3943 while (png_ptr
->idat_size
== 0)
3945 png_crc_finish(png_ptr
, 0);
3947 png_ptr
->idat_size
= png_read_chunk_header(png_ptr
);
3948 /* This is an error even in the 'check' case because the code just
3949 * consumed a non-IDAT header.
3951 if (png_ptr
->chunk_name
!= png_IDAT
)
3952 png_error(png_ptr
, "Not enough image data");
3955 avail_in
= png_ptr
->IDAT_read_size
;
3957 if (avail_in
> png_ptr
->idat_size
)
3958 avail_in
= (uInt
)png_ptr
->idat_size
;
3960 /* A PNG with a gradually increasing IDAT size will defeat this attempt
3961 * to minimize memory usage by causing lots of re-allocs, but
3962 * realistically doing IDAT_read_size re-allocs is not likely to be a
3965 buffer
= png_read_buffer(png_ptr
, avail_in
, 0/*error*/);
3967 png_crc_read(png_ptr
, buffer
, avail_in
);
3968 png_ptr
->idat_size
-= avail_in
;
3970 png_ptr
->zstream
.next_in
= buffer
;
3971 png_ptr
->zstream
.avail_in
= avail_in
;
3974 /* And set up the output side. */
3975 if (output
!= NULL
) /* standard read */
3977 uInt out
= ZLIB_IO_MAX
;
3979 if (out
> avail_out
)
3980 out
= (uInt
)avail_out
;
3983 png_ptr
->zstream
.avail_out
= out
;
3986 else /* after last row, checking for end */
3988 png_ptr
->zstream
.next_out
= tmpbuf
;
3989 png_ptr
->zstream
.avail_out
= (sizeof tmpbuf
);
3992 /* Use NO_FLUSH; this gives zlib the maximum opportunity to optimize the
3993 * process. If the LZ stream is truncated the sequential reader will
3994 * terminally damage the stream, above, by reading the chunk header of the
3995 * following chunk (it then exits with png_error).
3997 * TODO: deal more elegantly with truncated IDAT lists.
3999 ret
= inflate(&png_ptr
->zstream
, Z_NO_FLUSH
);
4001 /* Take the unconsumed output back. */
4003 avail_out
+= png_ptr
->zstream
.avail_out
;
4005 else /* avail_out counts the extra bytes */
4006 avail_out
+= (sizeof tmpbuf
) - png_ptr
->zstream
.avail_out
;
4008 png_ptr
->zstream
.avail_out
= 0;
4010 if (ret
== Z_STREAM_END
)
4012 /* Do this for safety; we won't read any more into this row. */
4013 png_ptr
->zstream
.next_out
= NULL
;
4015 png_ptr
->mode
|= PNG_AFTER_IDAT
;
4016 png_ptr
->flags
|= PNG_FLAG_ZSTREAM_ENDED
;
4018 if (png_ptr
->zstream
.avail_in
> 0 || png_ptr
->idat_size
> 0)
4019 png_chunk_benign_error(png_ptr
, "Extra compressed data");
4025 png_zstream_error(png_ptr
, ret
);
4028 png_chunk_error(png_ptr
, png_ptr
->zstream
.msg
);
4032 png_chunk_benign_error(png_ptr
, png_ptr
->zstream
.msg
);
4036 } while (avail_out
> 0);
4040 /* The stream ended before the image; this is the same as too few IDATs so
4041 * should be handled the same way.
4044 png_error(png_ptr
, "Not enough image data");
4046 else /* the deflate stream contained extra data */
4047 png_chunk_benign_error(png_ptr
, "Too much image data");
4052 png_read_finish_IDAT(png_structrp png_ptr
)
4054 /* We don't need any more data and the stream should have ended, however the
4055 * LZ end code may actually not have been processed. In this case we must
4056 * read it otherwise stray unread IDAT data or, more likely, an IDAT chunk
4057 * may still remain to be consumed.
4059 if (!(png_ptr
->flags
& PNG_FLAG_ZSTREAM_ENDED
))
4061 /* The NULL causes png_read_IDAT_data to swallow any remaining bytes in
4062 * the compressed stream, but the stream may be damaged too, so even after
4063 * this call we may need to terminate the zstream ownership.
4065 png_read_IDAT_data(png_ptr
, NULL
, 0);
4066 png_ptr
->zstream
.next_out
= NULL
; /* safety */
4068 /* Now clear everything out for safety; the following may not have been
4071 if (!(png_ptr
->flags
& PNG_FLAG_ZSTREAM_ENDED
))
4073 png_ptr
->mode
|= PNG_AFTER_IDAT
;
4074 png_ptr
->flags
|= PNG_FLAG_ZSTREAM_ENDED
;
4078 /* If the zstream has not been released do it now *and* terminate the reading
4079 * of the final IDAT chunk.
4081 if (png_ptr
->zowner
== png_IDAT
)
4083 /* Always do this; the pointers otherwise point into the read buffer. */
4084 png_ptr
->zstream
.next_in
= NULL
;
4085 png_ptr
->zstream
.avail_in
= 0;
4087 /* Now we no longer own the zstream. */
4088 png_ptr
->zowner
= 0;
4090 /* The slightly weird semantics of the sequential IDAT reading is that we
4091 * are always in or at the end of an IDAT chunk, so we always need to do a
4092 * crc_finish here. If idat_size is non-zero we also need to read the
4093 * spurious bytes at the end of the chunk now.
4095 (void)png_crc_finish(png_ptr
, png_ptr
->idat_size
);
4100 png_read_finish_row(png_structrp png_ptr
)
4102 #ifdef PNG_READ_INTERLACING_SUPPORTED
4103 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
4105 /* Start of interlace block */
4106 static PNG_CONST png_byte png_pass_start
[7] = {0, 4, 0, 2, 0, 1, 0};
4108 /* Offset to next interlace block */
4109 static PNG_CONST png_byte png_pass_inc
[7] = {8, 8, 4, 4, 2, 2, 1};
4111 /* Start of interlace block in the y direction */
4112 static PNG_CONST png_byte png_pass_ystart
[7] = {0, 0, 4, 0, 2, 0, 1};
4114 /* Offset to next interlace block in the y direction */
4115 static PNG_CONST png_byte png_pass_yinc
[7] = {8, 8, 8, 4, 4, 2, 2};
4116 #endif /* PNG_READ_INTERLACING_SUPPORTED */
4118 png_debug(1, "in png_read_finish_row");
4119 png_ptr
->row_number
++;
4120 if (png_ptr
->row_number
< png_ptr
->num_rows
)
4123 #ifdef PNG_READ_INTERLACING_SUPPORTED
4124 if (png_ptr
->interlaced
)
4126 png_ptr
->row_number
= 0;
4128 /* TO DO: don't do this if prev_row isn't needed (requires
4129 * read-ahead of the next row's filter byte.
4131 memset(png_ptr
->prev_row
, 0, png_ptr
->rowbytes
+ 1);
4137 if (png_ptr
->pass
>= 7)
4140 png_ptr
->iwidth
= (png_ptr
->width
+
4141 png_pass_inc
[png_ptr
->pass
] - 1 -
4142 png_pass_start
[png_ptr
->pass
]) /
4143 png_pass_inc
[png_ptr
->pass
];
4145 if (!(png_ptr
->transformations
& PNG_INTERLACE
))
4147 png_ptr
->num_rows
= (png_ptr
->height
+
4148 png_pass_yinc
[png_ptr
->pass
] - 1 -
4149 png_pass_ystart
[png_ptr
->pass
]) /
4150 png_pass_yinc
[png_ptr
->pass
];
4153 else /* if (png_ptr->transformations & PNG_INTERLACE) */
4154 break; /* libpng deinterlacing sees every row */
4156 } while (png_ptr
->num_rows
== 0 || png_ptr
->iwidth
== 0);
4158 if (png_ptr
->pass
< 7)
4161 #endif /* PNG_READ_INTERLACING_SUPPORTED */
4163 /* Here after at the end of the last row of the last pass. */
4164 png_read_finish_IDAT(png_ptr
);
4166 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
4169 png_read_start_row(png_structrp png_ptr
)
4171 #ifdef PNG_READ_INTERLACING_SUPPORTED
4172 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
4174 /* Start of interlace block */
4175 static PNG_CONST png_byte png_pass_start
[7] = {0, 4, 0, 2, 0, 1, 0};
4177 /* Offset to next interlace block */
4178 static PNG_CONST png_byte png_pass_inc
[7] = {8, 8, 4, 4, 2, 2, 1};
4180 /* Start of interlace block in the y direction */
4181 static PNG_CONST png_byte png_pass_ystart
[7] = {0, 0, 4, 0, 2, 0, 1};
4183 /* Offset to next interlace block in the y direction */
4184 static PNG_CONST png_byte png_pass_yinc
[7] = {8, 8, 8, 4, 4, 2, 2};
4187 int max_pixel_depth
;
4188 png_size_t row_bytes
;
4190 png_debug(1, "in png_read_start_row");
4192 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
4193 png_init_read_transformations(png_ptr
);
4195 #ifdef PNG_READ_INTERLACING_SUPPORTED
4196 if (png_ptr
->interlaced
)
4198 if (!(png_ptr
->transformations
& PNG_INTERLACE
))
4199 png_ptr
->num_rows
= (png_ptr
->height
+ png_pass_yinc
[0] - 1 -
4200 png_pass_ystart
[0]) / png_pass_yinc
[0];
4203 png_ptr
->num_rows
= png_ptr
->height
;
4205 png_ptr
->iwidth
= (png_ptr
->width
+
4206 png_pass_inc
[png_ptr
->pass
] - 1 -
4207 png_pass_start
[png_ptr
->pass
]) /
4208 png_pass_inc
[png_ptr
->pass
];
4212 #endif /* PNG_READ_INTERLACING_SUPPORTED */
4214 png_ptr
->num_rows
= png_ptr
->height
;
4215 png_ptr
->iwidth
= png_ptr
->width
;
4218 max_pixel_depth
= png_ptr
->pixel_depth
;
4220 /* WARNING: * png_read_transform_info (pngrtran.c) performs a simpliar set of
4221 * calculations to calculate the final pixel depth, then
4222 * png_do_read_transforms actually does the transforms. This means that the
4223 * code which effectively calculates this value is actually repeated in three
4224 * separate places. They must all match. Innocent changes to the order of
4225 * transformations can and will break libpng in a way that causes memory
4230 #ifdef PNG_READ_PACK_SUPPORTED
4231 if ((png_ptr
->transformations
& PNG_PACK
) && png_ptr
->bit_depth
< 8)
4232 max_pixel_depth
= 8;
4235 #ifdef PNG_READ_EXPAND_SUPPORTED
4236 if (png_ptr
->transformations
& PNG_EXPAND
)
4238 if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
4240 if (png_ptr
->num_trans
)
4241 max_pixel_depth
= 32;
4244 max_pixel_depth
= 24;
4247 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_GRAY
)
4249 if (max_pixel_depth
< 8)
4250 max_pixel_depth
= 8;
4252 if (png_ptr
->num_trans
)
4253 max_pixel_depth
*= 2;
4256 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_RGB
)
4258 if (png_ptr
->num_trans
)
4260 max_pixel_depth
*= 4;
4261 max_pixel_depth
/= 3;
4267 #ifdef PNG_READ_EXPAND_16_SUPPORTED
4268 if (png_ptr
->transformations
& PNG_EXPAND_16
)
4270 # ifdef PNG_READ_EXPAND_SUPPORTED
4271 /* In fact it is an error if it isn't supported, but checking is
4274 if (png_ptr
->transformations
& PNG_EXPAND
)
4276 if (png_ptr
->bit_depth
< 16)
4277 max_pixel_depth
*= 2;
4281 png_ptr
->transformations
&= ~PNG_EXPAND_16
;
4285 #ifdef PNG_READ_FILLER_SUPPORTED
4286 if (png_ptr
->transformations
& (PNG_FILLER
))
4288 if (png_ptr
->color_type
== PNG_COLOR_TYPE_GRAY
)
4290 if (max_pixel_depth
<= 8)
4291 max_pixel_depth
= 16;
4294 max_pixel_depth
= 32;
4297 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_RGB
||
4298 png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
4300 if (max_pixel_depth
<= 32)
4301 max_pixel_depth
= 32;
4304 max_pixel_depth
= 64;
4309 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
4310 if (png_ptr
->transformations
& PNG_GRAY_TO_RGB
)
4313 #ifdef PNG_READ_EXPAND_SUPPORTED
4314 (png_ptr
->num_trans
&& (png_ptr
->transformations
& PNG_EXPAND
)) ||
4316 #ifdef PNG_READ_FILLER_SUPPORTED
4317 (png_ptr
->transformations
& (PNG_FILLER
)) ||
4319 png_ptr
->color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
4321 if (max_pixel_depth
<= 16)
4322 max_pixel_depth
= 32;
4325 max_pixel_depth
= 64;
4330 if (max_pixel_depth
<= 8)
4332 if (png_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
4333 max_pixel_depth
= 32;
4336 max_pixel_depth
= 24;
4339 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
4340 max_pixel_depth
= 64;
4343 max_pixel_depth
= 48;
4348 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
4349 defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
4350 if (png_ptr
->transformations
& PNG_USER_TRANSFORM
)
4352 int user_pixel_depth
= png_ptr
->user_transform_depth
*
4353 png_ptr
->user_transform_channels
;
4355 if (user_pixel_depth
> max_pixel_depth
)
4356 max_pixel_depth
= user_pixel_depth
;
4360 /* This value is stored in png_struct and double checked in the row read
4363 png_ptr
->maximum_pixel_depth
= (png_byte
)max_pixel_depth
;
4364 png_ptr
->transformed_pixel_depth
= 0; /* calculated on demand */
4366 /* Align the width on the next larger 8 pixels. Mainly used
4369 row_bytes
= ((png_ptr
->width
+ 7) & ~((png_uint_32
)7));
4370 /* Calculate the maximum bytes needed, adding a byte and a pixel
4373 row_bytes
= PNG_ROWBYTES(max_pixel_depth
, row_bytes
) +
4374 1 + ((max_pixel_depth
+ 7) >> 3);
4376 #ifdef PNG_MAX_MALLOC_64K
4377 if (row_bytes
> (png_uint_32
)65536L)
4378 png_error(png_ptr
, "This image requires a row greater than 64KB");
4381 if (row_bytes
+ 48 > png_ptr
->old_big_row_buf_size
)
4383 png_free(png_ptr
, png_ptr
->big_row_buf
);
4384 png_free(png_ptr
, png_ptr
->big_prev_row
);
4386 if (png_ptr
->interlaced
)
4387 png_ptr
->big_row_buf
= (png_bytep
)png_calloc(png_ptr
,
4391 png_ptr
->big_row_buf
= (png_bytep
)png_malloc(png_ptr
, row_bytes
+ 48);
4393 png_ptr
->big_prev_row
= (png_bytep
)png_malloc(png_ptr
, row_bytes
+ 48);
4395 #ifdef PNG_ALIGNED_MEMORY_SUPPORTED
4396 /* Use 16-byte aligned memory for row_buf with at least 16 bytes
4397 * of padding before and after row_buf; treat prev_row similarly.
4398 * NOTE: the alignment is to the start of the pixels, one beyond the start
4399 * of the buffer, because of the filter byte. Prior to libpng 1.5.6 this
4400 * was incorrect; the filter byte was aligned, which had the exact
4401 * opposite effect of that intended.
4404 png_bytep temp
= png_ptr
->big_row_buf
+ 32;
4405 int extra
= (int)((temp
- (png_bytep
)0) & 0x0f);
4406 png_ptr
->row_buf
= temp
- extra
- 1/*filter byte*/;
4408 temp
= png_ptr
->big_prev_row
+ 32;
4409 extra
= (int)((temp
- (png_bytep
)0) & 0x0f);
4410 png_ptr
->prev_row
= temp
- extra
- 1/*filter byte*/;
4414 /* Use 31 bytes of padding before and 17 bytes after row_buf. */
4415 png_ptr
->row_buf
= png_ptr
->big_row_buf
+ 31;
4416 png_ptr
->prev_row
= png_ptr
->big_prev_row
+ 31;
4418 png_ptr
->old_big_row_buf_size
= row_bytes
+ 48;
4421 #ifdef PNG_MAX_MALLOC_64K
4422 if (png_ptr
->rowbytes
> 65535)
4423 png_error(png_ptr
, "This image requires a row greater than 64KB");
4426 if (png_ptr
->rowbytes
> (PNG_SIZE_MAX
- 1))
4427 png_error(png_ptr
, "Row has too many bytes to allocate in memory");
4429 memset(png_ptr
->prev_row
, 0, png_ptr
->rowbytes
+ 1);
4431 png_debug1(3, "width = %u,", png_ptr
->width
);
4432 png_debug1(3, "height = %u,", png_ptr
->height
);
4433 png_debug1(3, "iwidth = %u,", png_ptr
->iwidth
);
4434 png_debug1(3, "num_rows = %u,", png_ptr
->num_rows
);
4435 png_debug1(3, "rowbytes = %lu,", (unsigned long)png_ptr
->rowbytes
);
4436 png_debug1(3, "irowbytes = %lu",
4437 (unsigned long)PNG_ROWBYTES(png_ptr
->pixel_depth
, png_ptr
->iwidth
) + 1);
4439 /* The sequential reader needs a buffer for IDAT, but the progressive reader
4440 * does not, so free the read buffer now regardless; the sequential reader
4441 * reallocates it on demand.
4443 if (png_ptr
->read_buffer
)
4445 png_bytep buffer
= png_ptr
->read_buffer
;
4447 png_ptr
->read_buffer_size
= 0;
4448 png_ptr
->read_buffer
= NULL
;
4449 png_free(png_ptr
, buffer
);
4452 /* Finally claim the zstream for the inflate of the IDAT data, use the bits
4453 * value from the stream (note that this will result in a fatal error if the
4454 * IDAT stream has a bogus deflate header window_bits value, but this should
4455 * not be happening any longer!)
4457 if (png_inflate_claim(png_ptr
, png_IDAT
, 0) != Z_OK
)
4458 png_error(png_ptr
, png_ptr
->zstream
.msg
);
4460 png_ptr
->flags
|= PNG_FLAG_ROW_INIT
;
4462 #endif /* PNG_READ_SUPPORTED */