Glenn Randers-Pehrson | e647462 | 2006-03-04 16:50:47 -0600 | [diff] [blame] | 1 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2 | /* pngrutil.c - utilities to read a PNG file |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 3 | * |
Glenn Randers-Pehrson glennrp@comcast.net | 11a3c7b | 2009-05-15 20:33:24 -0500 | [diff] [blame] | 4 | * Last changed in libpng 1.4.0 [May 15, 2009] |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 5 | * For conditions of distribution and use, see copyright notice in png.h |
Glenn Randers-Pehrson | 79134c6 | 2009-02-14 10:32:18 -0600 | [diff] [blame] | 6 | * Copyright (c) 1998-2009 Glenn Randers-Pehrson |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 7 | * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) |
| 8 | * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 9 | * |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 10 | * This file contains routines that are only called from within |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 11 | * libpng itself during the course of reading an image. |
| 12 | */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 13 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 14 | #include "png.h" |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 15 | #if defined(PNG_READ_SUPPORTED) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 16 | #include "pngpriv.h" |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 17 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 18 | # define png_strtod(p,a,b) strtod(a,b) |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 19 | png_uint_32 PNGAPI |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 20 | png_get_uint_31(png_structp png_ptr, png_bytep buf) |
| 21 | { |
| 22 | png_uint_32 i = png_get_uint_32(buf); |
| 23 | if (i > PNG_UINT_31_MAX) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 24 | png_error(png_ptr, "PNG unsigned integer out of range"); |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 25 | return (i); |
| 26 | } |
Glenn Randers-Pehrson | eb58091 | 2008-07-30 14:47:09 -0500 | [diff] [blame] | 27 | #ifndef PNG_USE_READ_MACROS |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 28 | /* Grab an unsigned 32-bit integer from a buffer in big-endian format. */ |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 29 | png_uint_32 PNGAPI |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 30 | png_get_uint_32(png_bytep buf) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 31 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 32 | png_uint_32 i = ((png_uint_32)(*buf) << 24) + |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 33 | ((png_uint_32)(*(buf + 1)) << 16) + |
| 34 | ((png_uint_32)(*(buf + 2)) << 8) + |
| 35 | (png_uint_32)(*(buf + 3)); |
| 36 | |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 37 | return (i); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 38 | } |
| 39 | |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 40 | /* Grab a signed 32-bit integer from a buffer in big-endian format. The |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 41 | * data is stored in the PNG file in two's complement format, and it is |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame^] | 42 | * assumed that the machine format for signed integers is the same. |
| 43 | */ |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 44 | png_int_32 PNGAPI |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 45 | png_get_int_32(png_bytep buf) |
| 46 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 47 | png_int_32 i = ((png_int_32)(*buf) << 24) + |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 48 | ((png_int_32)(*(buf + 1)) << 16) + |
| 49 | ((png_int_32)(*(buf + 2)) << 8) + |
| 50 | (png_int_32)(*(buf + 3)); |
| 51 | |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 52 | return (i); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 53 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 54 | |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 55 | /* Grab an unsigned 16-bit integer from a buffer in big-endian format. */ |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 56 | png_uint_16 PNGAPI |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 57 | png_get_uint_16(png_bytep buf) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 58 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 59 | png_uint_16 i = (png_uint_16)(((png_uint_16)(*buf) << 8) + |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 60 | (png_uint_16)(*(buf + 1))); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 61 | |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 62 | return (i); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 63 | } |
Glenn Randers-Pehrson | eb58091 | 2008-07-30 14:47:09 -0500 | [diff] [blame] | 64 | #endif /* PNG_USE_READ_MACROS */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 65 | |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 66 | /* Read the chunk header (length + type name). |
| 67 | * Put the type name into png_ptr->chunk_name, and return the length. |
| 68 | */ |
| 69 | png_uint_32 /* PRIVATE */ |
| 70 | png_read_chunk_header(png_structp png_ptr) |
| 71 | { |
| 72 | png_byte buf[8]; |
| 73 | png_uint_32 length; |
| 74 | |
| 75 | #ifdef PNG_IO_STATE_SUPPORTED |
| 76 | /* Inform the I/O callback that the chunk header is being read. |
| 77 | * PNG_IO_CHUNK_HDR requires a single I/O call. |
| 78 | */ |
| 79 | png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_HDR; |
| 80 | #endif |
| 81 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame^] | 82 | /* Read the length and the chunk name */ |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 83 | png_read_data(png_ptr, buf, 8); |
| 84 | length = png_get_uint_31(png_ptr, buf); |
| 85 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame^] | 86 | /* Put the chunk name into png_ptr->chunk_name */ |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 87 | png_memcpy(png_ptr->chunk_name, buf + 4, 4); |
| 88 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 89 | png_debug2(0, "Reading %s chunk, length = %lu", |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 90 | png_ptr->chunk_name, length); |
| 91 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame^] | 92 | /* Reset the crc and run it over the chunk name */ |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 93 | png_reset_crc(png_ptr); |
| 94 | png_calculate_crc(png_ptr, png_ptr->chunk_name, 4); |
| 95 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame^] | 96 | /* Check to see if chunk name is valid */ |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 97 | png_check_chunk_name(png_ptr, png_ptr->chunk_name); |
| 98 | |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 99 | #ifdef PNG_IO_STATE_SUPPORTED |
| 100 | /* Inform the I/O callback that chunk data will (possibly) be read. |
| 101 | * PNG_IO_CHUNK_DATA does NOT require a specific number of I/O calls. |
| 102 | */ |
| 103 | png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_DATA; |
| 104 | #endif |
| 105 | |
| 106 | return length; |
| 107 | } |
| 108 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 109 | /* Read data, and (optionally) run it through the CRC. */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 110 | void /* PRIVATE */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 111 | png_crc_read(png_structp png_ptr, png_bytep buf, png_size_t length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 112 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame^] | 113 | if (png_ptr == NULL) |
| 114 | return; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 115 | png_read_data(png_ptr, buf, length); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 116 | png_calculate_crc(png_ptr, buf, length); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 117 | } |
| 118 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 119 | /* Optionally skip data and then check the CRC. Depending on whether we |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame^] | 120 | * are reading a ancillary or critical chunk, and how the program has set |
| 121 | * things up, we may calculate the CRC on the data and print a message. |
| 122 | * Returns '1' if there was a CRC error, '0' otherwise. |
| 123 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 124 | int /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 125 | png_crc_finish(png_structp png_ptr, png_uint_32 skip) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 126 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 127 | png_size_t i; |
| 128 | png_size_t istop = png_ptr->zbuf_size; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 129 | |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 130 | for (i = (png_size_t)skip; i > istop; i -= istop) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 131 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 132 | png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 133 | } |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 134 | if (i) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 135 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 136 | png_crc_read(png_ptr, png_ptr->zbuf, i); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 137 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 138 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 139 | if (png_crc_error(png_ptr)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 140 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 141 | if (((png_ptr->chunk_name[0] & 0x20) && /* Ancillary */ |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame^] | 142 | !(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)) || |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 143 | (!(png_ptr->chunk_name[0] & 0x20) && /* Critical */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 144 | (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE))) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 145 | { |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 146 | png_chunk_warning(png_ptr, "CRC error"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 147 | } |
| 148 | else |
| 149 | { |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 150 | png_chunk_benign_error(png_ptr, "CRC error"); |
| 151 | return (0); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 152 | } |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 153 | return (1); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 154 | } |
| 155 | |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 156 | return (0); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 157 | } |
| 158 | |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 159 | /* Compare the CRC stored in the PNG file with that calculated by libpng from |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame^] | 160 | * the data it has read thus far. |
| 161 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 162 | int /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 163 | png_crc_error(png_structp png_ptr) |
| 164 | { |
| 165 | png_byte crc_bytes[4]; |
| 166 | png_uint_32 crc; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 167 | int need_crc = 1; |
| 168 | |
| 169 | if (png_ptr->chunk_name[0] & 0x20) /* ancillary */ |
| 170 | { |
| 171 | if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) == |
| 172 | (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN)) |
| 173 | need_crc = 0; |
| 174 | } |
| 175 | else /* critical */ |
| 176 | { |
| 177 | if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) |
| 178 | need_crc = 0; |
| 179 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 180 | |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 181 | #ifdef PNG_IO_STATE_SUPPORTED |
| 182 | /* inform the I/O callback that the chunk CRC is being read */ |
| 183 | /* PNG_IO_CHUNK_CRC requires the I/O to be done at once */ |
| 184 | png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_CRC; |
| 185 | #endif |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 186 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 187 | png_read_data(png_ptr, crc_bytes, 4); |
| 188 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 189 | if (need_crc) |
| 190 | { |
| 191 | crc = png_get_uint_32(crc_bytes); |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 192 | return ((int)(crc != png_ptr->crc)); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 193 | } |
| 194 | else |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 195 | return (0); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 196 | } |
| 197 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 198 | #if defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) || \ |
Glenn Randers-Pehrson | 4accabb | 2000-04-14 14:20:47 -0500 | [diff] [blame] | 199 | defined(PNG_READ_iCCP_SUPPORTED) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 200 | /* |
| 201 | * Decompress trailing data in a chunk. The assumption is that chunkdata |
| 202 | * points at an allocated area holding the contents of a chunk with a |
| 203 | * trailing compressed part. What we get back is an allocated area |
| 204 | * holding the original prefix part and an uncompressed version of the |
| 205 | * trailing part (the malloc area passed in is freed). |
| 206 | */ |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 207 | void /* PRIVATE */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 208 | png_decompress_chunk(png_structp png_ptr, int comp_type, |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 209 | png_size_t chunklength, |
Glenn Randers-Pehrson | 68ea243 | 2000-04-01 21:10:05 -0600 | [diff] [blame] | 210 | png_size_t prefix_size, png_size_t *newlength) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 211 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 212 | static PNG_CONST char msg[] = "Error decoding compressed text"; |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 213 | png_charp text; |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 214 | png_size_t text_size; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 215 | |
Glenn Randers-Pehrson | 76e5fd6 | 2000-12-28 07:50:05 -0600 | [diff] [blame] | 216 | if (comp_type == PNG_COMPRESSION_TYPE_BASE) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 217 | { |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 218 | int ret = Z_OK; |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 219 | png_ptr->zstream.next_in = (png_bytep)(png_ptr->chunkdata + prefix_size); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 220 | png_ptr->zstream.avail_in = (uInt)(chunklength - prefix_size); |
| 221 | png_ptr->zstream.next_out = png_ptr->zbuf; |
| 222 | png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; |
| 223 | |
| 224 | text_size = 0; |
| 225 | text = NULL; |
| 226 | |
| 227 | while (png_ptr->zstream.avail_in) |
| 228 | { |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 229 | ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 230 | if (ret != Z_OK && ret != Z_STREAM_END) |
| 231 | { |
| 232 | if (png_ptr->zstream.msg != NULL) |
| 233 | png_warning(png_ptr, png_ptr->zstream.msg); |
| 234 | else |
| 235 | png_warning(png_ptr, msg); |
| 236 | inflateReset(&png_ptr->zstream); |
| 237 | png_ptr->zstream.avail_in = 0; |
| 238 | |
| 239 | if (text == NULL) |
| 240 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 241 | text_size = prefix_size + png_sizeof(msg) + 1; |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 242 | text = (png_charp)png_malloc_warn(png_ptr, text_size); |
| 243 | if (text == NULL) |
| 244 | { |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 245 | png_free(png_ptr, png_ptr->chunkdata); |
Glenn Randers-Pehrson | 72b6330 | 2008-07-22 13:59:07 -0500 | [diff] [blame] | 246 | png_ptr->chunkdata = NULL; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 247 | png_error(png_ptr, "Not enough memory to decompress chunk"); |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 248 | } |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 249 | png_memcpy(text, png_ptr->chunkdata, prefix_size); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 250 | } |
| 251 | |
Glenn Randers-Pehrson | 82ae383 | 2001-04-20 10:32:10 -0500 | [diff] [blame] | 252 | text[text_size - 1] = 0x00; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 253 | |
Glenn Randers-Pehrson | 82ae383 | 2001-04-20 10:32:10 -0500 | [diff] [blame] | 254 | /* Copy what we can of the error message into the text chunk */ |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 255 | text_size = (png_size_t)(chunklength - |
| 256 | (text - png_ptr->chunkdata) - 1); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 257 | if (text_size > png_sizeof(msg)) |
| 258 | text_size = png_sizeof(msg); |
| 259 | png_memcpy(text + prefix_size, msg, text_size); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 260 | break; |
| 261 | } |
| 262 | if (!png_ptr->zstream.avail_out || ret == Z_STREAM_END) |
| 263 | { |
| 264 | if (text == NULL) |
| 265 | { |
| 266 | text_size = prefix_size + |
| 267 | png_ptr->zbuf_size - png_ptr->zstream.avail_out; |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 268 | text = (png_charp)png_malloc_warn(png_ptr, text_size + 1); |
| 269 | if (text == NULL) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 270 | { |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 271 | png_free(png_ptr, png_ptr->chunkdata); |
Glenn Randers-Pehrson | 72b6330 | 2008-07-22 13:59:07 -0500 | [diff] [blame] | 272 | png_ptr->chunkdata = NULL; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 273 | png_error(png_ptr, |
| 274 | "Not enough memory to decompress chunk"); |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 275 | } |
Glenn Randers-Pehrson | 82ae383 | 2001-04-20 10:32:10 -0500 | [diff] [blame] | 276 | png_memcpy(text + prefix_size, png_ptr->zbuf, |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 277 | text_size - prefix_size); |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 278 | png_memcpy(text, png_ptr->chunkdata, prefix_size); |
Glenn Randers-Pehrson | 82ae383 | 2001-04-20 10:32:10 -0500 | [diff] [blame] | 279 | *(text + text_size) = 0x00; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 280 | } |
| 281 | else |
| 282 | { |
| 283 | png_charp tmp; |
| 284 | |
| 285 | tmp = text; |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 286 | text = (png_charp)png_malloc_warn(png_ptr, |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 287 | (png_size_t)(text_size + |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 288 | png_ptr->zbuf_size - png_ptr->zstream.avail_out + 1)); |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 289 | if (text == NULL) |
| 290 | { |
| 291 | png_free(png_ptr, tmp); |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 292 | png_free(png_ptr, png_ptr->chunkdata); |
Glenn Randers-Pehrson | 72b6330 | 2008-07-22 13:59:07 -0500 | [diff] [blame] | 293 | png_ptr->chunkdata = NULL; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 294 | png_error(png_ptr, |
| 295 | "Not enough memory to decompress chunk"); |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 296 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 297 | png_memcpy(text, tmp, text_size); |
| 298 | png_free(png_ptr, tmp); |
| 299 | png_memcpy(text + text_size, png_ptr->zbuf, |
| 300 | (png_ptr->zbuf_size - png_ptr->zstream.avail_out)); |
| 301 | text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out; |
| 302 | *(text + text_size) = 0x00; |
| 303 | } |
| 304 | if (ret == Z_STREAM_END) |
| 305 | break; |
| 306 | else |
| 307 | { |
| 308 | png_ptr->zstream.next_out = png_ptr->zbuf; |
| 309 | png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; |
| 310 | } |
| 311 | } |
| 312 | } |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 313 | if (ret != Z_STREAM_END) |
Glenn Randers-Pehrson | 1394480 | 2000-06-24 07:42:42 -0500 | [diff] [blame] | 314 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 315 | #if !defined(PNG_NO_STDIO) |
Glenn Randers-Pehrson | f46918d | 2006-06-02 05:31:20 -0500 | [diff] [blame] | 316 | char umsg[52]; |
Glenn Randers-Pehrson | 1394480 | 2000-06-24 07:42:42 -0500 | [diff] [blame] | 317 | |
Glenn Randers-Pehrson | 316f97a | 2000-07-08 13:19:41 -0500 | [diff] [blame] | 318 | if (ret == Z_BUF_ERROR) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 319 | png_snprintf(umsg, 52, |
| 320 | "Buffer error in compressed datastream in %s chunk", |
Glenn Randers-Pehrson | 316f97a | 2000-07-08 13:19:41 -0500 | [diff] [blame] | 321 | png_ptr->chunk_name); |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame^] | 322 | |
Glenn Randers-Pehrson | 316f97a | 2000-07-08 13:19:41 -0500 | [diff] [blame] | 323 | else if (ret == Z_DATA_ERROR) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 324 | png_snprintf(umsg, 52, |
| 325 | "Data error in compressed datastream in %s chunk", |
Glenn Randers-Pehrson | 316f97a | 2000-07-08 13:19:41 -0500 | [diff] [blame] | 326 | png_ptr->chunk_name); |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame^] | 327 | |
Glenn Randers-Pehrson | 316f97a | 2000-07-08 13:19:41 -0500 | [diff] [blame] | 328 | else |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 329 | png_snprintf(umsg, 52, |
| 330 | "Incomplete compressed datastream in %s chunk", |
Glenn Randers-Pehrson | 316f97a | 2000-07-08 13:19:41 -0500 | [diff] [blame] | 331 | png_ptr->chunk_name); |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame^] | 332 | |
Glenn Randers-Pehrson | 1394480 | 2000-06-24 07:42:42 -0500 | [diff] [blame] | 333 | png_warning(png_ptr, umsg); |
| 334 | #else |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 335 | png_warning(png_ptr, |
Glenn Randers-Pehrson | 1394480 | 2000-06-24 07:42:42 -0500 | [diff] [blame] | 336 | "Incomplete compressed datastream in chunk other than IDAT"); |
| 337 | #endif |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 338 | text_size = prefix_size; |
Glenn Randers-Pehrson | 231e687 | 2001-01-12 15:13:06 -0600 | [diff] [blame] | 339 | if (text == NULL) |
| 340 | { |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 341 | text = (png_charp)png_malloc_warn(png_ptr, text_size+1); |
| 342 | if (text == NULL) |
| 343 | { |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 344 | png_free(png_ptr, png_ptr->chunkdata); |
Glenn Randers-Pehrson | 72b6330 | 2008-07-22 13:59:07 -0500 | [diff] [blame] | 345 | png_ptr->chunkdata = NULL; |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 346 | png_error(png_ptr, "Not enough memory for text"); |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 347 | } |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 348 | png_memcpy(text, png_ptr->chunkdata, prefix_size); |
Glenn Randers-Pehrson | 231e687 | 2001-01-12 15:13:06 -0600 | [diff] [blame] | 349 | } |
Glenn Randers-Pehrson | 82ae383 | 2001-04-20 10:32:10 -0500 | [diff] [blame] | 350 | *(text + text_size) = 0x00; |
Glenn Randers-Pehrson | 1394480 | 2000-06-24 07:42:42 -0500 | [diff] [blame] | 351 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 352 | |
| 353 | inflateReset(&png_ptr->zstream); |
| 354 | png_ptr->zstream.avail_in = 0; |
| 355 | |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 356 | png_free(png_ptr, png_ptr->chunkdata); |
| 357 | png_ptr->chunkdata = text; |
Glenn Randers-Pehrson | 68ea243 | 2000-04-01 21:10:05 -0600 | [diff] [blame] | 358 | *newlength=text_size; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 359 | } |
Glenn Randers-Pehrson | 76e5fd6 | 2000-12-28 07:50:05 -0600 | [diff] [blame] | 360 | else /* if (comp_type != PNG_COMPRESSION_TYPE_BASE) */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 361 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 362 | #if !defined(PNG_NO_STDIO) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 363 | char umsg[50]; |
| 364 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 365 | png_snprintf(umsg, 50, "Unknown zTXt compression type %d", comp_type); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 366 | png_warning(png_ptr, umsg); |
| 367 | #else |
| 368 | png_warning(png_ptr, "Unknown zTXt compression type"); |
| 369 | #endif |
| 370 | |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 371 | *(png_ptr->chunkdata + prefix_size) = 0x00; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 372 | *newlength = prefix_size; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 373 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 374 | } |
| 375 | #endif |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 376 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame^] | 377 | /* Read and check the IDHR chunk */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 378 | void /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 379 | png_handle_IHDR(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 380 | { |
| 381 | png_byte buf[13]; |
| 382 | png_uint_32 width, height; |
| 383 | int bit_depth, color_type, compression_type, filter_type; |
| 384 | int interlace_type; |
| 385 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 386 | png_debug(1, "in png_handle_IHDR"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 387 | |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 388 | if (png_ptr->mode & PNG_HAVE_IHDR) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 389 | png_error(png_ptr, "Out of place IHDR"); |
| 390 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame^] | 391 | /* Check the length */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 392 | if (length != 13) |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 393 | png_error(png_ptr, "Invalid IHDR chunk"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 394 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 395 | png_ptr->mode |= PNG_HAVE_IHDR; |
| 396 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 397 | png_crc_read(png_ptr, buf, 13); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 398 | png_crc_finish(png_ptr, 0); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 399 | |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 400 | width = png_get_uint_31(png_ptr, buf); |
| 401 | height = png_get_uint_31(png_ptr, buf + 4); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 402 | bit_depth = buf[8]; |
| 403 | color_type = buf[9]; |
| 404 | compression_type = buf[10]; |
| 405 | filter_type = buf[11]; |
| 406 | interlace_type = buf[12]; |
| 407 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame^] | 408 | /* Set internal variables */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 409 | png_ptr->width = width; |
| 410 | png_ptr->height = height; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 411 | png_ptr->bit_depth = (png_byte)bit_depth; |
| 412 | png_ptr->interlaced = (png_byte)interlace_type; |
| 413 | png_ptr->color_type = (png_byte)color_type; |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 414 | #if defined(PNG_MNG_FEATURES_SUPPORTED) |
Glenn Randers-Pehrson | 2ad31ae | 2000-12-15 08:54:42 -0600 | [diff] [blame] | 415 | png_ptr->filter_type = (png_byte)filter_type; |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 416 | #endif |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 417 | png_ptr->compression_type = (png_byte)compression_type; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 418 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame^] | 419 | /* Find number of channels */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 420 | switch (png_ptr->color_type) |
| 421 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 422 | case PNG_COLOR_TYPE_GRAY: |
| 423 | case PNG_COLOR_TYPE_PALETTE: |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 424 | png_ptr->channels = 1; |
| 425 | break; |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame^] | 426 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 427 | case PNG_COLOR_TYPE_RGB: |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 428 | png_ptr->channels = 3; |
| 429 | break; |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame^] | 430 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 431 | case PNG_COLOR_TYPE_GRAY_ALPHA: |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 432 | png_ptr->channels = 2; |
| 433 | break; |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame^] | 434 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 435 | case PNG_COLOR_TYPE_RGB_ALPHA: |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 436 | png_ptr->channels = 4; |
| 437 | break; |
| 438 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 439 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame^] | 440 | /* Set up other useful info */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 441 | png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth * |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 442 | png_ptr->channels); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 443 | png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width); |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 444 | png_debug1(3, "bit_depth = %d", png_ptr->bit_depth); |
| 445 | png_debug1(3, "channels = %d", png_ptr->channels); |
| 446 | png_debug1(3, "rowbytes = %lu", png_ptr->rowbytes); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 447 | png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, |
| 448 | color_type, interlace_type, compression_type, filter_type); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 449 | } |
| 450 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame^] | 451 | /* Read and check the palette */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 452 | void /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 453 | png_handle_PLTE(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 454 | { |
Glenn Randers-Pehrson | 76e5fd6 | 2000-12-28 07:50:05 -0600 | [diff] [blame] | 455 | png_color palette[PNG_MAX_PALETTE_LENGTH]; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 456 | int num, i; |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 457 | #ifndef PNG_NO_POINTER_INDEXING |
| 458 | png_colorp pal_ptr; |
| 459 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 460 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 461 | png_debug(1, "in png_handle_PLTE"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 462 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 463 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 464 | png_error(png_ptr, "Missing IHDR before PLTE"); |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame^] | 465 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 466 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 467 | { |
| 468 | png_warning(png_ptr, "Invalid PLTE after IDAT"); |
| 469 | png_crc_finish(png_ptr, length); |
| 470 | return; |
| 471 | } |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame^] | 472 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 473 | else if (png_ptr->mode & PNG_HAVE_PLTE) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 474 | png_error(png_ptr, "Duplicate PLTE chunk"); |
| 475 | |
| 476 | png_ptr->mode |= PNG_HAVE_PLTE; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 477 | |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 478 | if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR)) |
| 479 | { |
| 480 | png_warning(png_ptr, |
| 481 | "Ignoring PLTE chunk in grayscale PNG"); |
| 482 | png_crc_finish(png_ptr, length); |
| 483 | return; |
| 484 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 485 | #if !defined(PNG_READ_OPT_PLTE_SUPPORTED) |
| 486 | if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE) |
| 487 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 488 | png_crc_finish(png_ptr, length); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 489 | return; |
| 490 | } |
| 491 | #endif |
| 492 | |
Glenn Randers-Pehrson | 76e5fd6 | 2000-12-28 07:50:05 -0600 | [diff] [blame] | 493 | if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 494 | { |
| 495 | if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE) |
| 496 | { |
| 497 | png_warning(png_ptr, "Invalid palette chunk"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 498 | png_crc_finish(png_ptr, length); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 499 | return; |
| 500 | } |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame^] | 501 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 502 | else |
| 503 | { |
| 504 | png_error(png_ptr, "Invalid palette chunk"); |
| 505 | } |
| 506 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 507 | |
| 508 | num = (int)length / 3; |
Glenn Randers-Pehrson | 38e6e77 | 2000-04-09 19:06:13 -0500 | [diff] [blame] | 509 | |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 510 | #ifndef PNG_NO_POINTER_INDEXING |
| 511 | for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++) |
| 512 | { |
| 513 | png_byte buf[3]; |
| 514 | |
| 515 | png_crc_read(png_ptr, buf, 3); |
| 516 | pal_ptr->red = buf[0]; |
| 517 | pal_ptr->green = buf[1]; |
| 518 | pal_ptr->blue = buf[2]; |
| 519 | } |
| 520 | #else |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 521 | for (i = 0; i < num; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 522 | { |
| 523 | png_byte buf[3]; |
| 524 | |
| 525 | png_crc_read(png_ptr, buf, 3); |
| 526 | /* don't depend upon png_color being any order */ |
| 527 | palette[i].red = buf[0]; |
| 528 | palette[i].green = buf[1]; |
| 529 | palette[i].blue = buf[2]; |
| 530 | } |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 531 | #endif |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 532 | |
| 533 | /* If we actually NEED the PLTE chunk (ie for a paletted image), we do |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame^] | 534 | * whatever the normal CRC configuration tells us. However, if we |
| 535 | * have an RGB image, the PLTE can be considered ancillary, so |
| 536 | * we will act as though it is. |
| 537 | */ |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 538 | #if !defined(PNG_READ_OPT_PLTE_SUPPORTED) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 539 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 540 | #endif |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 541 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 542 | png_crc_finish(png_ptr, 0); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 543 | } |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 544 | #if !defined(PNG_READ_OPT_PLTE_SUPPORTED) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 545 | else if (png_crc_error(png_ptr)) /* Only if we have a CRC error */ |
| 546 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 547 | /* If we don't want to use the data from an ancillary chunk, |
| 548 | we have two options: an error abort, or a warning and we |
| 549 | ignore the data in this chunk (which should be OK, since |
| 550 | it's considered ancillary for a RGB or RGBA image). */ |
| 551 | if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE)) |
| 552 | { |
| 553 | if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) |
| 554 | { |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 555 | png_chunk_benign_error(png_ptr, "CRC error"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 556 | } |
| 557 | else |
| 558 | { |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 559 | png_chunk_warning(png_ptr, "CRC error"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 560 | return; |
| 561 | } |
| 562 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 563 | /* Otherwise, we (optionally) emit a warning and use the chunk. */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 564 | else if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)) |
| 565 | { |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 566 | png_chunk_warning(png_ptr, "CRC error"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 567 | } |
| 568 | } |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 569 | #endif |
Glenn Randers-Pehrson | 38e6e77 | 2000-04-09 19:06:13 -0500 | [diff] [blame] | 570 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 571 | png_set_PLTE(png_ptr, info_ptr, palette, num); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 572 | |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 573 | #if defined(PNG_READ_tRNS_SUPPORTED) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 574 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 575 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 576 | if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS)) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 577 | { |
Glenn Randers-Pehrson | 38e6e77 | 2000-04-09 19:06:13 -0500 | [diff] [blame] | 578 | if (png_ptr->num_trans > (png_uint_16)num) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 579 | { |
| 580 | png_warning(png_ptr, "Truncating incorrect tRNS chunk length"); |
Glenn Randers-Pehrson | 38e6e77 | 2000-04-09 19:06:13 -0500 | [diff] [blame] | 581 | png_ptr->num_trans = (png_uint_16)num; |
| 582 | } |
| 583 | if (info_ptr->num_trans > (png_uint_16)num) |
| 584 | { |
| 585 | png_warning(png_ptr, "Truncating incorrect info tRNS chunk length"); |
| 586 | info_ptr->num_trans = (png_uint_16)num; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | } |
| 590 | #endif |
| 591 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 592 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 593 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 594 | void /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 595 | png_handle_IEND(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
| 596 | { |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 597 | png_debug(1, "in png_handle_IEND"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 598 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 599 | if (!(png_ptr->mode & PNG_HAVE_IHDR) || !(png_ptr->mode & PNG_HAVE_IDAT)) |
| 600 | { |
| 601 | png_error(png_ptr, "No image in file"); |
| 602 | } |
| 603 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 604 | png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 605 | |
| 606 | if (length != 0) |
| 607 | { |
| 608 | png_warning(png_ptr, "Incorrect IEND chunk length"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 609 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 610 | png_crc_finish(png_ptr, length); |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 611 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 612 | info_ptr = info_ptr; /* quiet compiler warnings about unused info_ptr */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 613 | } |
| 614 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 615 | #if defined(PNG_READ_gAMA_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 616 | void /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 617 | png_handle_gAMA(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 618 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 619 | png_fixed_point igamma; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 620 | #ifdef PNG_FLOATING_POINT_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 621 | float file_gamma; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 622 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 623 | png_byte buf[4]; |
| 624 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 625 | png_debug(1, "in png_handle_gAMA"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 626 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 627 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 628 | png_error(png_ptr, "Missing IHDR before gAMA"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 629 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 630 | { |
| 631 | png_warning(png_ptr, "Invalid gAMA after IDAT"); |
| 632 | png_crc_finish(png_ptr, length); |
| 633 | return; |
| 634 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 635 | else if (png_ptr->mode & PNG_HAVE_PLTE) |
| 636 | /* Should be an error, but we can cope with it */ |
| 637 | png_warning(png_ptr, "Out of place gAMA chunk"); |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 638 | |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 639 | if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA) |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 640 | #if defined(PNG_READ_sRGB_SUPPORTED) |
| 641 | && !(info_ptr->valid & PNG_INFO_sRGB) |
| 642 | #endif |
| 643 | ) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 644 | { |
| 645 | png_warning(png_ptr, "Duplicate gAMA chunk"); |
| 646 | png_crc_finish(png_ptr, length); |
| 647 | return; |
| 648 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 649 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 650 | if (length != 4) |
| 651 | { |
Guy Schalnat | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 652 | png_warning(png_ptr, "Incorrect gAMA chunk length"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 653 | png_crc_finish(png_ptr, length); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 654 | return; |
| 655 | } |
| 656 | |
| 657 | png_crc_read(png_ptr, buf, 4); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 658 | if (png_crc_finish(png_ptr, 0)) |
| 659 | return; |
| 660 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 661 | igamma = (png_fixed_point)png_get_uint_32(buf); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 662 | /* check for zero gamma */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 663 | if (igamma == 0) |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 664 | { |
| 665 | png_warning(png_ptr, |
| 666 | "Ignoring gAMA chunk with gamma=0"); |
| 667 | return; |
| 668 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 669 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 670 | #if defined(PNG_READ_sRGB_SUPPORTED) |
Glenn Randers-Pehrson | 170b70c | 2006-03-10 10:19:04 -0600 | [diff] [blame] | 671 | if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB)) |
Glenn Randers-Pehrson | d029a75 | 2004-08-09 21:50:32 -0500 | [diff] [blame] | 672 | if (PNG_OUT_OF_RANGE(igamma, 45500L, 500)) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 673 | { |
| 674 | png_warning(png_ptr, |
| 675 | "Ignoring incorrect gAMA value when sRGB is also present"); |
Glenn Randers-Pehrson | 316f97a | 2000-07-08 13:19:41 -0500 | [diff] [blame] | 676 | #ifndef PNG_NO_CONSOLE_IO |
Glenn Randers-Pehrson | 79134c6 | 2009-02-14 10:32:18 -0600 | [diff] [blame] | 677 | fprintf(stderr, "gamma = (%d/100000)", (int)igamma); |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 678 | #endif |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 679 | return; |
| 680 | } |
| 681 | #endif /* PNG_READ_sRGB_SUPPORTED */ |
| 682 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 683 | #ifdef PNG_FLOATING_POINT_SUPPORTED |
Glenn Randers-Pehrson | 397100e | 1998-03-07 19:45:37 -0600 | [diff] [blame] | 684 | file_gamma = (float)igamma / (float)100000.0; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 685 | # ifdef PNG_READ_GAMMA_SUPPORTED |
| 686 | png_ptr->gamma = file_gamma; |
| 687 | # endif |
| 688 | png_set_gAMA(png_ptr, info_ptr, file_gamma); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 689 | #endif |
| 690 | #ifdef PNG_FIXED_POINT_SUPPORTED |
| 691 | png_set_gAMA_fixed(png_ptr, info_ptr, igamma); |
| 692 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 693 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 694 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 695 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 696 | #if defined(PNG_READ_sBIT_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 697 | void /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 698 | png_handle_sBIT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 699 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 700 | png_size_t truelen; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 701 | png_byte buf[4]; |
Guy Schalnat | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 702 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 703 | png_debug(1, "in png_handle_sBIT"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 704 | |
Guy Schalnat | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 705 | buf[0] = buf[1] = buf[2] = buf[3] = 0; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 706 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 707 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 708 | png_error(png_ptr, "Missing IHDR before sBIT"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 709 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 710 | { |
| 711 | png_warning(png_ptr, "Invalid sBIT after IDAT"); |
| 712 | png_crc_finish(png_ptr, length); |
| 713 | return; |
| 714 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 715 | else if (png_ptr->mode & PNG_HAVE_PLTE) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 716 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 717 | /* Should be an error, but we can cope with it */ |
| 718 | png_warning(png_ptr, "Out of place sBIT chunk"); |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 719 | } |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 720 | if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 721 | { |
| 722 | png_warning(png_ptr, "Duplicate sBIT chunk"); |
| 723 | png_crc_finish(png_ptr, length); |
| 724 | return; |
| 725 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 726 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 727 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 728 | truelen = 3; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 729 | else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 730 | truelen = (png_size_t)png_ptr->channels; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 731 | |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 732 | if (length != truelen || length > 4) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 733 | { |
Guy Schalnat | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 734 | png_warning(png_ptr, "Incorrect sBIT chunk length"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 735 | png_crc_finish(png_ptr, length); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 736 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 737 | } |
| 738 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 739 | png_crc_read(png_ptr, buf, truelen); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 740 | if (png_crc_finish(png_ptr, 0)) |
| 741 | return; |
| 742 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 743 | if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) |
| 744 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 745 | png_ptr->sig_bit.red = buf[0]; |
| 746 | png_ptr->sig_bit.green = buf[1]; |
| 747 | png_ptr->sig_bit.blue = buf[2]; |
| 748 | png_ptr->sig_bit.alpha = buf[3]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 749 | } |
| 750 | else |
| 751 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 752 | png_ptr->sig_bit.gray = buf[0]; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 753 | png_ptr->sig_bit.red = buf[0]; |
| 754 | png_ptr->sig_bit.green = buf[0]; |
| 755 | png_ptr->sig_bit.blue = buf[0]; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 756 | png_ptr->sig_bit.alpha = buf[1]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 757 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 758 | png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit)); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 759 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 760 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 761 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 762 | #if defined(PNG_READ_cHRM_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 763 | void /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 764 | png_handle_cHRM(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 765 | { |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 766 | png_byte buf[32]; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 767 | #ifdef PNG_FLOATING_POINT_SUPPORTED |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 768 | float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 769 | #endif |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 770 | png_fixed_point int_x_white, int_y_white, int_x_red, int_y_red, int_x_green, |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 771 | int_y_green, int_x_blue, int_y_blue; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 772 | |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 773 | png_uint_32 uint_x, uint_y; |
| 774 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 775 | png_debug(1, "in png_handle_cHRM"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 776 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 777 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 778 | png_error(png_ptr, "Missing IHDR before cHRM"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 779 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 780 | { |
| 781 | png_warning(png_ptr, "Invalid cHRM after IDAT"); |
| 782 | png_crc_finish(png_ptr, length); |
| 783 | return; |
| 784 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 785 | else if (png_ptr->mode & PNG_HAVE_PLTE) |
| 786 | /* Should be an error, but we can cope with it */ |
| 787 | png_warning(png_ptr, "Missing PLTE before cHRM"); |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 788 | |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 789 | if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM) |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 790 | #if defined(PNG_READ_sRGB_SUPPORTED) |
| 791 | && !(info_ptr->valid & PNG_INFO_sRGB) |
| 792 | #endif |
| 793 | ) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 794 | { |
| 795 | png_warning(png_ptr, "Duplicate cHRM chunk"); |
| 796 | png_crc_finish(png_ptr, length); |
| 797 | return; |
| 798 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 799 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 800 | if (length != 32) |
| 801 | { |
Guy Schalnat | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 802 | png_warning(png_ptr, "Incorrect cHRM chunk length"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 803 | png_crc_finish(png_ptr, length); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 804 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 805 | } |
| 806 | |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 807 | png_crc_read(png_ptr, buf, 32); |
| 808 | if (png_crc_finish(png_ptr, 0)) |
| 809 | return; |
| 810 | |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 811 | uint_x = png_get_uint_32(buf); |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 812 | uint_y = png_get_uint_32(buf + 4); |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 813 | int_x_white = (png_fixed_point)uint_x; |
| 814 | int_y_white = (png_fixed_point)uint_y; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 815 | |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 816 | uint_x = png_get_uint_32(buf + 8); |
| 817 | uint_y = png_get_uint_32(buf + 12); |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 818 | int_x_red = (png_fixed_point)uint_x; |
| 819 | int_y_red = (png_fixed_point)uint_y; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 820 | |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 821 | uint_x = png_get_uint_32(buf + 16); |
| 822 | uint_y = png_get_uint_32(buf + 20); |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 823 | int_x_green = (png_fixed_point)uint_x; |
| 824 | int_y_green = (png_fixed_point)uint_y; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 825 | |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 826 | uint_x = png_get_uint_32(buf + 24); |
| 827 | uint_y = png_get_uint_32(buf + 28); |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 828 | int_x_blue = (png_fixed_point)uint_x; |
| 829 | int_y_blue = (png_fixed_point)uint_y; |
| 830 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 831 | #ifdef PNG_FLOATING_POINT_SUPPORTED |
| 832 | white_x = (float)int_x_white / (float)100000.0; |
| 833 | white_y = (float)int_y_white / (float)100000.0; |
| 834 | red_x = (float)int_x_red / (float)100000.0; |
| 835 | red_y = (float)int_y_red / (float)100000.0; |
| 836 | green_x = (float)int_x_green / (float)100000.0; |
| 837 | green_y = (float)int_y_green / (float)100000.0; |
| 838 | blue_x = (float)int_x_blue / (float)100000.0; |
| 839 | blue_y = (float)int_y_blue / (float)100000.0; |
| 840 | #endif |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 841 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 842 | #if defined(PNG_READ_sRGB_SUPPORTED) |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 843 | if ((info_ptr != NULL) && (info_ptr->valid & PNG_INFO_sRGB)) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 844 | { |
Glenn Randers-Pehrson | d029a75 | 2004-08-09 21:50:32 -0500 | [diff] [blame] | 845 | if (PNG_OUT_OF_RANGE(int_x_white, 31270, 1000) || |
| 846 | PNG_OUT_OF_RANGE(int_y_white, 32900, 1000) || |
| 847 | PNG_OUT_OF_RANGE(int_x_red, 64000L, 1000) || |
| 848 | PNG_OUT_OF_RANGE(int_y_red, 33000, 1000) || |
| 849 | PNG_OUT_OF_RANGE(int_x_green, 30000, 1000) || |
| 850 | PNG_OUT_OF_RANGE(int_y_green, 60000L, 1000) || |
| 851 | PNG_OUT_OF_RANGE(int_x_blue, 15000, 1000) || |
| 852 | PNG_OUT_OF_RANGE(int_y_blue, 6000, 1000)) |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 853 | { |
| 854 | png_warning(png_ptr, |
| 855 | "Ignoring incorrect cHRM value when sRGB is also present"); |
Glenn Randers-Pehrson | 316f97a | 2000-07-08 13:19:41 -0500 | [diff] [blame] | 856 | #ifndef PNG_NO_CONSOLE_IO |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 857 | #ifdef PNG_FLOATING_POINT_SUPPORTED |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 858 | fprintf(stderr, "wx=%f, wy=%f, rx=%f, ry=%f\n", |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 859 | white_x, white_y, red_x, red_y); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 860 | fprintf(stderr, "gx=%f, gy=%f, bx=%f, by=%f\n", |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 861 | green_x, green_y, blue_x, blue_y); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 862 | #else |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 863 | fprintf(stderr, "wx=%ld, wy=%ld, rx=%ld, ry=%ld\n", |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 864 | int_x_white, int_y_white, int_x_red, int_y_red); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 865 | fprintf(stderr, "gx=%ld, gy=%ld, bx=%ld, by=%ld\n", |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 866 | int_x_green, int_y_green, int_x_blue, int_y_blue); |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 867 | #endif |
Glenn Randers-Pehrson | 316f97a | 2000-07-08 13:19:41 -0500 | [diff] [blame] | 868 | #endif /* PNG_NO_CONSOLE_IO */ |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 869 | } |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 870 | return; |
| 871 | } |
| 872 | #endif /* PNG_READ_sRGB_SUPPORTED */ |
| 873 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 874 | #ifdef PNG_FLOATING_POINT_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 875 | png_set_cHRM(png_ptr, info_ptr, |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 876 | white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 877 | #endif |
| 878 | #ifdef PNG_FIXED_POINT_SUPPORTED |
| 879 | png_set_cHRM_fixed(png_ptr, info_ptr, |
| 880 | int_x_white, int_y_white, int_x_red, int_y_red, int_x_green, |
| 881 | int_y_green, int_x_blue, int_y_blue); |
| 882 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 883 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 884 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 885 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 886 | #if defined(PNG_READ_sRGB_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 887 | void /* PRIVATE */ |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 888 | png_handle_sRGB(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
| 889 | { |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 890 | int intent; |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 891 | png_byte buf[1]; |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 892 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 893 | png_debug(1, "in png_handle_sRGB"); |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 894 | |
| 895 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 896 | png_error(png_ptr, "Missing IHDR before sRGB"); |
| 897 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 898 | { |
| 899 | png_warning(png_ptr, "Invalid sRGB after IDAT"); |
| 900 | png_crc_finish(png_ptr, length); |
| 901 | return; |
| 902 | } |
| 903 | else if (png_ptr->mode & PNG_HAVE_PLTE) |
| 904 | /* Should be an error, but we can cope with it */ |
| 905 | png_warning(png_ptr, "Out of place sRGB chunk"); |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 906 | |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 907 | if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB)) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 908 | { |
| 909 | png_warning(png_ptr, "Duplicate sRGB chunk"); |
| 910 | png_crc_finish(png_ptr, length); |
| 911 | return; |
| 912 | } |
| 913 | |
| 914 | if (length != 1) |
| 915 | { |
| 916 | png_warning(png_ptr, "Incorrect sRGB chunk length"); |
| 917 | png_crc_finish(png_ptr, length); |
| 918 | return; |
| 919 | } |
| 920 | |
| 921 | png_crc_read(png_ptr, buf, 1); |
| 922 | if (png_crc_finish(png_ptr, 0)) |
| 923 | return; |
| 924 | |
| 925 | intent = buf[0]; |
| 926 | /* check for bad intent */ |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 927 | if (intent >= PNG_sRGB_INTENT_LAST) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 928 | { |
| 929 | png_warning(png_ptr, "Unknown sRGB intent"); |
| 930 | return; |
| 931 | } |
| 932 | |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 933 | #if defined(PNG_READ_gAMA_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED) |
Glenn Randers-Pehrson | 170b70c | 2006-03-10 10:19:04 -0600 | [diff] [blame] | 934 | if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA)) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 935 | { |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 936 | png_fixed_point igamma; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 937 | #ifdef PNG_FIXED_POINT_SUPPORTED |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 938 | igamma=info_ptr->int_gamma; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 939 | #else |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 940 | # ifdef PNG_FLOATING_POINT_SUPPORTED |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 941 | igamma=(png_fixed_point)(info_ptr->gamma * 100000.); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 942 | # endif |
| 943 | #endif |
Glenn Randers-Pehrson | d029a75 | 2004-08-09 21:50:32 -0500 | [diff] [blame] | 944 | if (PNG_OUT_OF_RANGE(igamma, 45500L, 500)) |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 945 | { |
| 946 | png_warning(png_ptr, |
| 947 | "Ignoring incorrect gAMA value when sRGB is also present"); |
Glenn Randers-Pehrson | 316f97a | 2000-07-08 13:19:41 -0500 | [diff] [blame] | 948 | #ifndef PNG_NO_CONSOLE_IO |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 949 | # ifdef PNG_FIXED_POINT_SUPPORTED |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 950 | fprintf(stderr, "incorrect gamma=(%d/100000)\n", |
| 951 | (int)png_ptr->int_gamma); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 952 | # else |
| 953 | # ifdef PNG_FLOATING_POINT_SUPPORTED |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 954 | fprintf(stderr, "incorrect gamma=%f\n", png_ptr->gamma); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 955 | # endif |
| 956 | # endif |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 957 | #endif |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 958 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 959 | } |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 960 | #endif /* PNG_READ_gAMA_SUPPORTED */ |
| 961 | |
| 962 | #ifdef PNG_READ_cHRM_SUPPORTED |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 963 | #ifdef PNG_FIXED_POINT_SUPPORTED |
Glenn Randers-Pehrson | 170b70c | 2006-03-10 10:19:04 -0600 | [diff] [blame] | 964 | if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM)) |
Glenn Randers-Pehrson | d029a75 | 2004-08-09 21:50:32 -0500 | [diff] [blame] | 965 | if (PNG_OUT_OF_RANGE(info_ptr->int_x_white, 31270, 1000) || |
| 966 | PNG_OUT_OF_RANGE(info_ptr->int_y_white, 32900, 1000) || |
| 967 | PNG_OUT_OF_RANGE(info_ptr->int_x_red, 64000L, 1000) || |
| 968 | PNG_OUT_OF_RANGE(info_ptr->int_y_red, 33000, 1000) || |
| 969 | PNG_OUT_OF_RANGE(info_ptr->int_x_green, 30000, 1000) || |
| 970 | PNG_OUT_OF_RANGE(info_ptr->int_y_green, 60000L, 1000) || |
| 971 | PNG_OUT_OF_RANGE(info_ptr->int_x_blue, 15000, 1000) || |
| 972 | PNG_OUT_OF_RANGE(info_ptr->int_y_blue, 6000, 1000)) |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 973 | { |
| 974 | png_warning(png_ptr, |
| 975 | "Ignoring incorrect cHRM value when sRGB is also present"); |
| 976 | } |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 977 | #endif /* PNG_FIXED_POINT_SUPPORTED */ |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 978 | #endif /* PNG_READ_cHRM_SUPPORTED */ |
| 979 | |
| 980 | png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, intent); |
| 981 | } |
| 982 | #endif /* PNG_READ_sRGB_SUPPORTED */ |
| 983 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 984 | #if defined(PNG_READ_iCCP_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 985 | void /* PRIVATE */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 986 | png_handle_iCCP(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
| 987 | /* Note: this does not properly handle chunks that are > 64K under DOS */ |
| 988 | { |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 989 | png_byte compression_type; |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 990 | png_bytep pC; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 991 | png_charp profile; |
| 992 | png_uint_32 skip = 0; |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 993 | png_uint_32 profile_size, profile_length; |
Glenn Randers-Pehrson | 68ea243 | 2000-04-01 21:10:05 -0600 | [diff] [blame] | 994 | png_size_t slength, prefix_length, data_length; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 995 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 996 | png_debug(1, "in png_handle_iCCP"); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 997 | |
| 998 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 999 | png_error(png_ptr, "Missing IHDR before iCCP"); |
| 1000 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 1001 | { |
| 1002 | png_warning(png_ptr, "Invalid iCCP after IDAT"); |
| 1003 | png_crc_finish(png_ptr, length); |
| 1004 | return; |
| 1005 | } |
| 1006 | else if (png_ptr->mode & PNG_HAVE_PLTE) |
| 1007 | /* Should be an error, but we can cope with it */ |
| 1008 | png_warning(png_ptr, "Out of place iCCP chunk"); |
| 1009 | |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1010 | if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_iCCP)) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1011 | { |
| 1012 | png_warning(png_ptr, "Duplicate iCCP chunk"); |
| 1013 | png_crc_finish(png_ptr, length); |
| 1014 | return; |
| 1015 | } |
| 1016 | |
| 1017 | #ifdef PNG_MAX_MALLOC_64K |
| 1018 | if (length > (png_uint_32)65535L) |
| 1019 | { |
| 1020 | png_warning(png_ptr, "iCCP chunk too large to fit in memory"); |
| 1021 | skip = length - (png_uint_32)65535L; |
| 1022 | length = (png_uint_32)65535L; |
| 1023 | } |
| 1024 | #endif |
| 1025 | |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 1026 | png_free(png_ptr, png_ptr->chunkdata); |
| 1027 | png_ptr->chunkdata = (png_charp)png_malloc(png_ptr, length + 1); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1028 | slength = (png_size_t)length; |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 1029 | png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1030 | |
| 1031 | if (png_crc_finish(png_ptr, skip)) |
| 1032 | { |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 1033 | png_free(png_ptr, png_ptr->chunkdata); |
| 1034 | png_ptr->chunkdata = NULL; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1035 | return; |
| 1036 | } |
| 1037 | |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 1038 | png_ptr->chunkdata[slength] = 0x00; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1039 | |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 1040 | for (profile = png_ptr->chunkdata; *profile; profile++) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1041 | /* empty loop to find end of name */ ; |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 1042 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1043 | ++profile; |
| 1044 | |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 1045 | /* there should be at least one zero (the compression type byte) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1046 | following the separator, and we should be on it */ |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 1047 | if ( profile >= png_ptr->chunkdata + slength - 1) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1048 | { |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 1049 | png_free(png_ptr, png_ptr->chunkdata); |
| 1050 | png_ptr->chunkdata = NULL; |
Glenn Randers-Pehrson | 5e5c1e1 | 2000-11-10 12:26:19 -0600 | [diff] [blame] | 1051 | png_warning(png_ptr, "Malformed iCCP chunk"); |
Glenn Randers-Pehrson | 4accabb | 2000-04-14 14:20:47 -0500 | [diff] [blame] | 1052 | return; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1053 | } |
| 1054 | |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 1055 | /* compression_type should always be zero */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1056 | compression_type = *profile++; |
Glenn Randers-Pehrson | 5e5c1e1 | 2000-11-10 12:26:19 -0600 | [diff] [blame] | 1057 | if (compression_type) |
| 1058 | { |
| 1059 | png_warning(png_ptr, "Ignoring nonzero compression type in iCCP chunk"); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1060 | compression_type = 0x00; /* Reset it to zero (libpng-1.0.6 through 1.0.8 |
Glenn Randers-Pehrson | 5e5c1e1 | 2000-11-10 12:26:19 -0600 | [diff] [blame] | 1061 | wrote nonzero) */ |
| 1062 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1063 | |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 1064 | prefix_length = profile - png_ptr->chunkdata; |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 1065 | png_decompress_chunk(png_ptr, compression_type, |
| 1066 | slength, prefix_length, &data_length); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1067 | |
Glenn Randers-Pehrson | 5e5c1e1 | 2000-11-10 12:26:19 -0600 | [diff] [blame] | 1068 | profile_length = data_length - prefix_length; |
Glenn Randers-Pehrson | 231e687 | 2001-01-12 15:13:06 -0600 | [diff] [blame] | 1069 | |
Glenn Randers-Pehrson | db3b88d | 2001-12-04 06:30:43 -0600 | [diff] [blame] | 1070 | if ( prefix_length > data_length || profile_length < 4) |
Glenn Randers-Pehrson | b182893 | 2001-06-23 08:03:17 -0500 | [diff] [blame] | 1071 | { |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 1072 | png_free(png_ptr, png_ptr->chunkdata); |
| 1073 | png_ptr->chunkdata = NULL; |
Glenn Randers-Pehrson | b182893 | 2001-06-23 08:03:17 -0500 | [diff] [blame] | 1074 | png_warning(png_ptr, "Profile size field missing from iCCP chunk"); |
| 1075 | return; |
| 1076 | } |
| 1077 | |
Glenn Randers-Pehrson | 231e687 | 2001-01-12 15:13:06 -0600 | [diff] [blame] | 1078 | /* Check the profile_size recorded in the first 32 bits of the ICC profile */ |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 1079 | pC = (png_bytep)(png_ptr->chunkdata + prefix_length); |
Glenn Randers-Pehrson | 72b6330 | 2008-07-22 13:59:07 -0500 | [diff] [blame] | 1080 | profile_size = ((*(pC ))<<24) | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1081 | ((*(pC + 1))<<16) | |
| 1082 | ((*(pC + 2))<< 8) | |
| 1083 | ((*(pC + 3)) ); |
Glenn Randers-Pehrson | 5e5c1e1 | 2000-11-10 12:26:19 -0600 | [diff] [blame] | 1084 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1085 | if (profile_size < profile_length) |
Glenn Randers-Pehrson | 5e5c1e1 | 2000-11-10 12:26:19 -0600 | [diff] [blame] | 1086 | profile_length = profile_size; |
| 1087 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1088 | if (profile_size > profile_length) |
Glenn Randers-Pehrson | 5e5c1e1 | 2000-11-10 12:26:19 -0600 | [diff] [blame] | 1089 | { |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 1090 | png_free(png_ptr, png_ptr->chunkdata); |
| 1091 | png_ptr->chunkdata = NULL; |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 1092 | png_warning(png_ptr, "Ignoring truncated iCCP profile"); |
Glenn Randers-Pehrson | 5e5c1e1 | 2000-11-10 12:26:19 -0600 | [diff] [blame] | 1093 | return; |
| 1094 | } |
| 1095 | |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 1096 | png_set_iCCP(png_ptr, info_ptr, png_ptr->chunkdata, |
| 1097 | compression_type, png_ptr->chunkdata + prefix_length, profile_length); |
| 1098 | png_free(png_ptr, png_ptr->chunkdata); |
| 1099 | png_ptr->chunkdata = NULL; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1100 | } |
| 1101 | #endif /* PNG_READ_iCCP_SUPPORTED */ |
| 1102 | |
| 1103 | #if defined(PNG_READ_sPLT_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1104 | void /* PRIVATE */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1105 | png_handle_sPLT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
| 1106 | /* Note: this does not properly handle chunks that are > 64K under DOS */ |
| 1107 | { |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1108 | png_bytep entry_start; |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 1109 | png_sPLT_t new_palette; |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 1110 | #ifdef PNG_NO_POINTER_INDEXING |
| 1111 | png_sPLT_entryp pp; |
| 1112 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1113 | int data_length, entry_size, i; |
| 1114 | png_uint_32 skip = 0; |
| 1115 | png_size_t slength; |
| 1116 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1117 | png_debug(1, "in png_handle_sPLT"); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1118 | |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 1119 | #ifdef PNG_SET_USER_LIMITS_SUPPORTED |
| 1120 | |
| 1121 | if (png_ptr->user_chunk_cache_max != 0) |
| 1122 | { |
| 1123 | if (png_ptr->user_chunk_cache_max == 1) |
| 1124 | { |
| 1125 | png_crc_finish(png_ptr, length); |
| 1126 | return; |
| 1127 | } |
| 1128 | if (--png_ptr->user_chunk_cache_max == 1) |
| 1129 | { |
| 1130 | png_warning(png_ptr, "No space in chunk cache for sPLT"); |
| 1131 | png_crc_finish(png_ptr, length); |
| 1132 | return; |
| 1133 | } |
| 1134 | } |
| 1135 | #endif |
| 1136 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1137 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 1138 | png_error(png_ptr, "Missing IHDR before sPLT"); |
| 1139 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 1140 | { |
| 1141 | png_warning(png_ptr, "Invalid sPLT after IDAT"); |
| 1142 | png_crc_finish(png_ptr, length); |
| 1143 | return; |
| 1144 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1145 | |
| 1146 | #ifdef PNG_MAX_MALLOC_64K |
| 1147 | if (length > (png_uint_32)65535L) |
| 1148 | { |
| 1149 | png_warning(png_ptr, "sPLT chunk too large to fit in memory"); |
| 1150 | skip = length - (png_uint_32)65535L; |
| 1151 | length = (png_uint_32)65535L; |
| 1152 | } |
| 1153 | #endif |
| 1154 | |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 1155 | png_free(png_ptr, png_ptr->chunkdata); |
| 1156 | png_ptr->chunkdata = (png_charp)png_malloc(png_ptr, length + 1); |
Glenn Randers-Pehrson | 82ae383 | 2001-04-20 10:32:10 -0500 | [diff] [blame] | 1157 | slength = (png_size_t)length; |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 1158 | png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1159 | |
| 1160 | if (png_crc_finish(png_ptr, skip)) |
| 1161 | { |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 1162 | png_free(png_ptr, png_ptr->chunkdata); |
| 1163 | png_ptr->chunkdata = NULL; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1164 | return; |
| 1165 | } |
| 1166 | |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 1167 | png_ptr->chunkdata[slength] = 0x00; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1168 | |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 1169 | for (entry_start = (png_bytep)png_ptr->chunkdata; *entry_start; entry_start++) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1170 | /* empty loop to find end of name */ ; |
| 1171 | ++entry_start; |
| 1172 | |
| 1173 | /* a sample depth should follow the separator, and we should be on it */ |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 1174 | if (entry_start > (png_bytep)png_ptr->chunkdata + slength - 2) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1175 | { |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 1176 | png_free(png_ptr, png_ptr->chunkdata); |
| 1177 | png_ptr->chunkdata = NULL; |
Glenn Randers-Pehrson | 4accabb | 2000-04-14 14:20:47 -0500 | [diff] [blame] | 1178 | png_warning(png_ptr, "malformed sPLT chunk"); |
| 1179 | return; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | new_palette.depth = *entry_start++; |
| 1183 | entry_size = (new_palette.depth == 8 ? 6 : 10); |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 1184 | data_length = (slength - (entry_start - (png_bytep)png_ptr->chunkdata)); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1185 | |
| 1186 | /* integrity-check the data length */ |
| 1187 | if (data_length % entry_size) |
| 1188 | { |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 1189 | png_free(png_ptr, png_ptr->chunkdata); |
| 1190 | png_ptr->chunkdata = NULL; |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 1191 | png_warning(png_ptr, "sPLT chunk has bad length"); |
| 1192 | return; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1193 | } |
| 1194 | |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 1195 | new_palette.nentries = (png_int_32) ( data_length / entry_size); |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 1196 | if ((png_uint_32) new_palette.nentries > |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1197 | (png_uint_32) (PNG_SIZE_MAX / png_sizeof(png_sPLT_entry))) |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 1198 | { |
| 1199 | png_warning(png_ptr, "sPLT chunk too long"); |
| 1200 | return; |
| 1201 | } |
| 1202 | new_palette.entries = (png_sPLT_entryp)png_malloc_warn( |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1203 | png_ptr, new_palette.nentries * png_sizeof(png_sPLT_entry)); |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 1204 | if (new_palette.entries == NULL) |
| 1205 | { |
| 1206 | png_warning(png_ptr, "sPLT chunk requires too much memory"); |
| 1207 | return; |
| 1208 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1209 | |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 1210 | #ifndef PNG_NO_POINTER_INDEXING |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1211 | for (i = 0; i < new_palette.nentries; i++) |
| 1212 | { |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 1213 | png_sPLT_entryp pp = new_palette.entries + i; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1214 | |
| 1215 | if (new_palette.depth == 8) |
| 1216 | { |
| 1217 | pp->red = *entry_start++; |
| 1218 | pp->green = *entry_start++; |
| 1219 | pp->blue = *entry_start++; |
| 1220 | pp->alpha = *entry_start++; |
| 1221 | } |
| 1222 | else |
| 1223 | { |
| 1224 | pp->red = png_get_uint_16(entry_start); entry_start += 2; |
| 1225 | pp->green = png_get_uint_16(entry_start); entry_start += 2; |
| 1226 | pp->blue = png_get_uint_16(entry_start); entry_start += 2; |
| 1227 | pp->alpha = png_get_uint_16(entry_start); entry_start += 2; |
| 1228 | } |
| 1229 | pp->frequency = png_get_uint_16(entry_start); entry_start += 2; |
| 1230 | } |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 1231 | #else |
| 1232 | pp = new_palette.entries; |
| 1233 | for (i = 0; i < new_palette.nentries; i++) |
| 1234 | { |
| 1235 | |
| 1236 | if (new_palette.depth == 8) |
| 1237 | { |
| 1238 | pp[i].red = *entry_start++; |
| 1239 | pp[i].green = *entry_start++; |
| 1240 | pp[i].blue = *entry_start++; |
| 1241 | pp[i].alpha = *entry_start++; |
| 1242 | } |
| 1243 | else |
| 1244 | { |
| 1245 | pp[i].red = png_get_uint_16(entry_start); entry_start += 2; |
| 1246 | pp[i].green = png_get_uint_16(entry_start); entry_start += 2; |
| 1247 | pp[i].blue = png_get_uint_16(entry_start); entry_start += 2; |
| 1248 | pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2; |
| 1249 | } |
| 1250 | pp->frequency = png_get_uint_16(entry_start); entry_start += 2; |
| 1251 | } |
| 1252 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1253 | |
| 1254 | /* discard all chunk data except the name and stash that */ |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 1255 | new_palette.name = png_ptr->chunkdata; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1256 | |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 1257 | png_set_sPLT(png_ptr, info_ptr, &new_palette, 1); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1258 | |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 1259 | png_free(png_ptr, png_ptr->chunkdata); |
| 1260 | png_ptr->chunkdata = NULL; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1261 | png_free(png_ptr, new_palette.entries); |
| 1262 | } |
| 1263 | #endif /* PNG_READ_sPLT_SUPPORTED */ |
| 1264 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1265 | #if defined(PNG_READ_tRNS_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1266 | void /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1267 | png_handle_tRNS(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1268 | { |
Glenn Randers-Pehrson | d1e8c86 | 2002-06-20 06:54:34 -0500 | [diff] [blame] | 1269 | png_byte readbuf[PNG_MAX_PALETTE_LENGTH]; |
Glenn Randers-Pehrson | 76e5fd6 | 2000-12-28 07:50:05 -0600 | [diff] [blame] | 1270 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1271 | png_debug(1, "in png_handle_tRNS"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1272 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1273 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 1274 | png_error(png_ptr, "Missing IHDR before tRNS"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1275 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 1276 | { |
| 1277 | png_warning(png_ptr, "Invalid tRNS after IDAT"); |
| 1278 | png_crc_finish(png_ptr, length); |
| 1279 | return; |
| 1280 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1281 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1282 | { |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1283 | png_warning(png_ptr, "Duplicate tRNS chunk"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1284 | png_crc_finish(png_ptr, length); |
| 1285 | return; |
| 1286 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1287 | |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1288 | if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1289 | { |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1290 | png_byte buf[2]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1291 | |
| 1292 | if (length != 2) |
| 1293 | { |
Guy Schalnat | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 1294 | png_warning(png_ptr, "Incorrect tRNS chunk length"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1295 | png_crc_finish(png_ptr, length); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1296 | return; |
| 1297 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1298 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1299 | png_crc_read(png_ptr, buf, 2); |
| 1300 | png_ptr->num_trans = 1; |
Glenn Randers-Pehrson | 56f6396 | 2008-10-06 10:16:17 -0500 | [diff] [blame] | 1301 | png_ptr->trans_color.gray = png_get_uint_16(buf); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1302 | } |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1303 | else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB) |
| 1304 | { |
| 1305 | png_byte buf[6]; |
| 1306 | |
| 1307 | if (length != 6) |
| 1308 | { |
| 1309 | png_warning(png_ptr, "Incorrect tRNS chunk length"); |
| 1310 | png_crc_finish(png_ptr, length); |
| 1311 | return; |
| 1312 | } |
| 1313 | png_crc_read(png_ptr, buf, (png_size_t)length); |
| 1314 | png_ptr->num_trans = 1; |
Glenn Randers-Pehrson | 56f6396 | 2008-10-06 10:16:17 -0500 | [diff] [blame] | 1315 | png_ptr->trans_color.red = png_get_uint_16(buf); |
| 1316 | png_ptr->trans_color.green = png_get_uint_16(buf + 2); |
| 1317 | png_ptr->trans_color.blue = png_get_uint_16(buf + 4); |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1318 | } |
| 1319 | else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 1320 | { |
| 1321 | if (!(png_ptr->mode & PNG_HAVE_PLTE)) |
| 1322 | { |
| 1323 | /* Should be an error, but we can cope with it. */ |
| 1324 | png_warning(png_ptr, "Missing PLTE before tRNS"); |
| 1325 | } |
| 1326 | if (length > (png_uint_32)png_ptr->num_palette || |
| 1327 | length > PNG_MAX_PALETTE_LENGTH) |
| 1328 | { |
| 1329 | png_warning(png_ptr, "Incorrect tRNS chunk length"); |
| 1330 | png_crc_finish(png_ptr, length); |
| 1331 | return; |
| 1332 | } |
| 1333 | if (length == 0) |
| 1334 | { |
| 1335 | png_warning(png_ptr, "Zero length tRNS chunk"); |
| 1336 | png_crc_finish(png_ptr, length); |
| 1337 | return; |
| 1338 | } |
| 1339 | png_crc_read(png_ptr, readbuf, (png_size_t)length); |
| 1340 | png_ptr->num_trans = (png_uint_16)length; |
| 1341 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1342 | else |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1343 | { |
| 1344 | png_warning(png_ptr, "tRNS chunk not allowed with alpha channel"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1345 | png_crc_finish(png_ptr, length); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1346 | return; |
| 1347 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1348 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1349 | if (png_crc_finish(png_ptr, 0)) |
Glenn Randers-Pehrson | a7dbcba | 2007-05-15 16:16:34 -0500 | [diff] [blame] | 1350 | { |
| 1351 | png_ptr->num_trans = 0; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1352 | return; |
Glenn Randers-Pehrson | a7dbcba | 2007-05-15 16:16:34 -0500 | [diff] [blame] | 1353 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1354 | |
Glenn Randers-Pehrson | 76e5fd6 | 2000-12-28 07:50:05 -0600 | [diff] [blame] | 1355 | png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans, |
Glenn Randers-Pehrson | 56f6396 | 2008-10-06 10:16:17 -0500 | [diff] [blame] | 1356 | &(png_ptr->trans_color)); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1357 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1358 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1359 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1360 | #if defined(PNG_READ_bKGD_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1361 | void /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1362 | png_handle_bKGD(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1363 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1364 | png_size_t truelen; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1365 | png_byte buf[6]; |
| 1366 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1367 | png_debug(1, "in png_handle_bKGD"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1368 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1369 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 1370 | png_error(png_ptr, "Missing IHDR before bKGD"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1371 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 1372 | { |
| 1373 | png_warning(png_ptr, "Invalid bKGD after IDAT"); |
| 1374 | png_crc_finish(png_ptr, length); |
| 1375 | return; |
| 1376 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1377 | else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && |
| 1378 | !(png_ptr->mode & PNG_HAVE_PLTE)) |
| 1379 | { |
| 1380 | png_warning(png_ptr, "Missing PLTE before bKGD"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1381 | png_crc_finish(png_ptr, length); |
| 1382 | return; |
| 1383 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1384 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1385 | { |
| 1386 | png_warning(png_ptr, "Duplicate bKGD chunk"); |
| 1387 | png_crc_finish(png_ptr, length); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1388 | return; |
| 1389 | } |
| 1390 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1391 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 1392 | truelen = 1; |
| 1393 | else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) |
| 1394 | truelen = 6; |
| 1395 | else |
| 1396 | truelen = 2; |
| 1397 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1398 | if (length != truelen) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1399 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1400 | png_warning(png_ptr, "Incorrect bKGD chunk length"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1401 | png_crc_finish(png_ptr, length); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1402 | return; |
| 1403 | } |
| 1404 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1405 | png_crc_read(png_ptr, buf, truelen); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1406 | if (png_crc_finish(png_ptr, 0)) |
| 1407 | return; |
| 1408 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1409 | /* We convert the index value into RGB components so that we can allow |
| 1410 | * arbitrary RGB values for background when we have transparency, and |
| 1411 | * so it is easy to determine the RGB values of the background color |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1412 | * from the info_ptr struct. */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1413 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1414 | { |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1415 | png_ptr->background.index = buf[0]; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1416 | if (info_ptr && info_ptr->num_palette) |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1417 | { |
Glenn Randers-Pehrson | 79134c6 | 2009-02-14 10:32:18 -0600 | [diff] [blame] | 1418 | if (buf[0] >= info_ptr->num_palette) |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1419 | { |
| 1420 | png_warning(png_ptr, "Incorrect bKGD chunk index value"); |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1421 | return; |
| 1422 | } |
| 1423 | png_ptr->background.red = |
| 1424 | (png_uint_16)png_ptr->palette[buf[0]].red; |
| 1425 | png_ptr->background.green = |
| 1426 | (png_uint_16)png_ptr->palette[buf[0]].green; |
| 1427 | png_ptr->background.blue = |
| 1428 | (png_uint_16)png_ptr->palette[buf[0]].blue; |
| 1429 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1430 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1431 | else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) /* GRAY */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1432 | { |
| 1433 | png_ptr->background.red = |
| 1434 | png_ptr->background.green = |
| 1435 | png_ptr->background.blue = |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1436 | png_ptr->background.gray = png_get_uint_16(buf); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1437 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1438 | else |
| 1439 | { |
| 1440 | png_ptr->background.red = png_get_uint_16(buf); |
| 1441 | png_ptr->background.green = png_get_uint_16(buf + 2); |
| 1442 | png_ptr->background.blue = png_get_uint_16(buf + 4); |
| 1443 | } |
| 1444 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1445 | png_set_bKGD(png_ptr, info_ptr, &(png_ptr->background)); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1446 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1447 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1448 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1449 | #if defined(PNG_READ_hIST_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1450 | void /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1451 | png_handle_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1452 | { |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 1453 | unsigned int num, i; |
Glenn Randers-Pehrson | d1e8c86 | 2002-06-20 06:54:34 -0500 | [diff] [blame] | 1454 | png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1455 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1456 | png_debug(1, "in png_handle_hIST"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1457 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1458 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 1459 | png_error(png_ptr, "Missing IHDR before hIST"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1460 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 1461 | { |
| 1462 | png_warning(png_ptr, "Invalid hIST after IDAT"); |
| 1463 | png_crc_finish(png_ptr, length); |
| 1464 | return; |
| 1465 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1466 | else if (!(png_ptr->mode & PNG_HAVE_PLTE)) |
| 1467 | { |
| 1468 | png_warning(png_ptr, "Missing PLTE before hIST"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1469 | png_crc_finish(png_ptr, length); |
| 1470 | return; |
| 1471 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1472 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1473 | { |
| 1474 | png_warning(png_ptr, "Duplicate hIST chunk"); |
| 1475 | png_crc_finish(png_ptr, length); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1476 | return; |
| 1477 | } |
| 1478 | |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 1479 | num = length / 2 ; |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1480 | if (num != (unsigned int) png_ptr->num_palette || num > |
| 1481 | (unsigned int) PNG_MAX_PALETTE_LENGTH) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1482 | { |
| 1483 | png_warning(png_ptr, "Incorrect hIST chunk length"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1484 | png_crc_finish(png_ptr, length); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1485 | return; |
| 1486 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1487 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1488 | for (i = 0; i < num; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1489 | { |
| 1490 | png_byte buf[2]; |
| 1491 | |
| 1492 | png_crc_read(png_ptr, buf, 2); |
Glenn Randers-Pehrson | 76e5fd6 | 2000-12-28 07:50:05 -0600 | [diff] [blame] | 1493 | readbuf[i] = png_get_uint_16(buf); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1494 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1495 | |
| 1496 | if (png_crc_finish(png_ptr, 0)) |
| 1497 | return; |
| 1498 | |
Glenn Randers-Pehrson | 76e5fd6 | 2000-12-28 07:50:05 -0600 | [diff] [blame] | 1499 | png_set_hIST(png_ptr, info_ptr, readbuf); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1500 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1501 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1502 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1503 | #if defined(PNG_READ_pHYs_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1504 | void /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1505 | png_handle_pHYs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1506 | { |
| 1507 | png_byte buf[9]; |
| 1508 | png_uint_32 res_x, res_y; |
| 1509 | int unit_type; |
| 1510 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1511 | png_debug(1, "in png_handle_pHYs"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1512 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1513 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 1514 | png_error(png_ptr, "Missing IHDR before pHYs"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1515 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 1516 | { |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 1517 | png_warning(png_ptr, "Invalid pHYs after IDAT"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1518 | png_crc_finish(png_ptr, length); |
| 1519 | return; |
| 1520 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1521 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1522 | { |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 1523 | png_warning(png_ptr, "Duplicate pHYs chunk"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1524 | png_crc_finish(png_ptr, length); |
| 1525 | return; |
| 1526 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1527 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1528 | if (length != 9) |
| 1529 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1530 | png_warning(png_ptr, "Incorrect pHYs chunk length"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1531 | png_crc_finish(png_ptr, length); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1532 | return; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1533 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1534 | |
| 1535 | png_crc_read(png_ptr, buf, 9); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1536 | if (png_crc_finish(png_ptr, 0)) |
| 1537 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1538 | |
| 1539 | res_x = png_get_uint_32(buf); |
| 1540 | res_y = png_get_uint_32(buf + 4); |
| 1541 | unit_type = buf[8]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1542 | png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1543 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1544 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1545 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1546 | #if defined(PNG_READ_oFFs_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1547 | void /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1548 | png_handle_oFFs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1549 | { |
| 1550 | png_byte buf[9]; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1551 | png_int_32 offset_x, offset_y; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1552 | int unit_type; |
| 1553 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1554 | png_debug(1, "in png_handle_oFFs"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1555 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1556 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 1557 | png_error(png_ptr, "Missing IHDR before oFFs"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1558 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 1559 | { |
| 1560 | png_warning(png_ptr, "Invalid oFFs after IDAT"); |
| 1561 | png_crc_finish(png_ptr, length); |
| 1562 | return; |
| 1563 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1564 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1565 | { |
| 1566 | png_warning(png_ptr, "Duplicate oFFs chunk"); |
| 1567 | png_crc_finish(png_ptr, length); |
| 1568 | return; |
| 1569 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1570 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1571 | if (length != 9) |
| 1572 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1573 | png_warning(png_ptr, "Incorrect oFFs chunk length"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1574 | png_crc_finish(png_ptr, length); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1575 | return; |
| 1576 | } |
| 1577 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1578 | png_crc_read(png_ptr, buf, 9); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1579 | if (png_crc_finish(png_ptr, 0)) |
| 1580 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1581 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1582 | offset_x = png_get_int_32(buf); |
| 1583 | offset_y = png_get_int_32(buf + 4); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1584 | unit_type = buf[8]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1585 | png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type); |
| 1586 | } |
| 1587 | #endif |
| 1588 | |
| 1589 | #if defined(PNG_READ_pCAL_SUPPORTED) |
Glenn Randers-Pehrson | ff9c947 | 2000-07-11 07:12:36 -0500 | [diff] [blame] | 1590 | /* read the pCAL chunk (described in the PNG Extensions document) */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1591 | void /* PRIVATE */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1592 | png_handle_pCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
| 1593 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1594 | png_int_32 X0, X1; |
| 1595 | png_byte type, nparams; |
| 1596 | png_charp buf, units, endptr; |
| 1597 | png_charpp params; |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1598 | png_size_t slength; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1599 | int i; |
| 1600 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1601 | png_debug(1, "in png_handle_pCAL"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1602 | |
| 1603 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 1604 | png_error(png_ptr, "Missing IHDR before pCAL"); |
| 1605 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 1606 | { |
| 1607 | png_warning(png_ptr, "Invalid pCAL after IDAT"); |
| 1608 | png_crc_finish(png_ptr, length); |
| 1609 | return; |
| 1610 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1611 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1612 | { |
| 1613 | png_warning(png_ptr, "Duplicate pCAL chunk"); |
| 1614 | png_crc_finish(png_ptr, length); |
| 1615 | return; |
| 1616 | } |
| 1617 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1618 | png_debug1(2, "Allocating and reading pCAL chunk data (%lu bytes)", |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1619 | length + 1); |
Glenn Randers-Pehrson | d8d7b94 | 2008-07-21 10:34:34 -0500 | [diff] [blame] | 1620 | png_free(png_ptr, png_ptr->chunkdata); |
| 1621 | png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1); |
| 1622 | if (png_ptr->chunkdata == NULL) |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 1623 | { |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 1624 | png_warning(png_ptr, "No memory for pCAL purpose"); |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 1625 | return; |
| 1626 | } |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1627 | slength = (png_size_t)length; |
Glenn Randers-Pehrson | d8d7b94 | 2008-07-21 10:34:34 -0500 | [diff] [blame] | 1628 | png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1629 | |
| 1630 | if (png_crc_finish(png_ptr, 0)) |
| 1631 | { |
Glenn Randers-Pehrson | d8d7b94 | 2008-07-21 10:34:34 -0500 | [diff] [blame] | 1632 | png_free(png_ptr, png_ptr->chunkdata); |
| 1633 | png_ptr->chunkdata = NULL; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1634 | return; |
| 1635 | } |
| 1636 | |
Glenn Randers-Pehrson | d8d7b94 | 2008-07-21 10:34:34 -0500 | [diff] [blame] | 1637 | png_ptr->chunkdata[slength] = 0x00; /* null terminate the last string */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1638 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1639 | png_debug(3, "Finding end of pCAL purpose string"); |
Glenn Randers-Pehrson | d8d7b94 | 2008-07-21 10:34:34 -0500 | [diff] [blame] | 1640 | for (buf = png_ptr->chunkdata; *buf; buf++) |
Glenn Randers-Pehrson | d12aa50 | 1998-03-13 07:39:39 -0600 | [diff] [blame] | 1641 | /* empty loop */ ; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1642 | |
Glenn Randers-Pehrson | d8d7b94 | 2008-07-21 10:34:34 -0500 | [diff] [blame] | 1643 | endptr = png_ptr->chunkdata + slength; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1644 | |
| 1645 | /* We need to have at least 12 bytes after the purpose string |
| 1646 | in order to get the parameter information. */ |
| 1647 | if (endptr <= buf + 12) |
| 1648 | { |
| 1649 | png_warning(png_ptr, "Invalid pCAL data"); |
Glenn Randers-Pehrson | d8d7b94 | 2008-07-21 10:34:34 -0500 | [diff] [blame] | 1650 | png_free(png_ptr, png_ptr->chunkdata); |
| 1651 | png_ptr->chunkdata = NULL; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1652 | return; |
| 1653 | } |
| 1654 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1655 | png_debug(3, "Reading pCAL X0, X1, type, nparams, and units"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1656 | X0 = png_get_int_32((png_bytep)buf+1); |
| 1657 | X1 = png_get_int_32((png_bytep)buf+5); |
| 1658 | type = buf[9]; |
| 1659 | nparams = buf[10]; |
| 1660 | units = buf + 11; |
| 1661 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1662 | png_debug(3, "Checking pCAL equation type and number of parameters"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1663 | /* Check that we have the right number of parameters for known |
| 1664 | equation types. */ |
| 1665 | if ((type == PNG_EQUATION_LINEAR && nparams != 2) || |
| 1666 | (type == PNG_EQUATION_BASE_E && nparams != 3) || |
| 1667 | (type == PNG_EQUATION_ARBITRARY && nparams != 3) || |
| 1668 | (type == PNG_EQUATION_HYPERBOLIC && nparams != 4)) |
| 1669 | { |
| 1670 | png_warning(png_ptr, "Invalid pCAL parameters for equation type"); |
Glenn Randers-Pehrson | d8d7b94 | 2008-07-21 10:34:34 -0500 | [diff] [blame] | 1671 | png_free(png_ptr, png_ptr->chunkdata); |
| 1672 | png_ptr->chunkdata = NULL; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1673 | return; |
| 1674 | } |
| 1675 | else if (type >= PNG_EQUATION_LAST) |
| 1676 | { |
| 1677 | png_warning(png_ptr, "Unrecognized equation type for pCAL chunk"); |
| 1678 | } |
| 1679 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1680 | for (buf = units; *buf; buf++) |
Glenn Randers-Pehrson | f9f2fe0 | 1998-03-15 18:20:23 -0600 | [diff] [blame] | 1681 | /* Empty loop to move past the units string. */ ; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1682 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1683 | png_debug(3, "Allocating pCAL parameters array"); |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 1684 | params = (png_charpp)png_malloc_warn(png_ptr, |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1685 | (png_size_t)(nparams * png_sizeof(png_charp))); |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 1686 | if (params == NULL) |
| 1687 | { |
Glenn Randers-Pehrson | d8d7b94 | 2008-07-21 10:34:34 -0500 | [diff] [blame] | 1688 | png_free(png_ptr, png_ptr->chunkdata); |
| 1689 | png_ptr->chunkdata = NULL; |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 1690 | png_warning(png_ptr, "No memory for pCAL params"); |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 1691 | return; |
| 1692 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1693 | |
| 1694 | /* Get pointers to the start of each parameter string. */ |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 1695 | for (i = 0; i < (int)nparams; i++) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1696 | { |
| 1697 | buf++; /* Skip the null string terminator from previous parameter. */ |
| 1698 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1699 | png_debug1(3, "Reading pCAL parameter %d", i); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1700 | for (params[i] = buf; buf <= endptr && *buf != 0x00; buf++) |
Glenn Randers-Pehrson | f9f2fe0 | 1998-03-15 18:20:23 -0600 | [diff] [blame] | 1701 | /* Empty loop to move past each parameter string */ ; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1702 | |
| 1703 | /* Make sure we haven't run out of data yet */ |
| 1704 | if (buf > endptr) |
| 1705 | { |
| 1706 | png_warning(png_ptr, "Invalid pCAL data"); |
Glenn Randers-Pehrson | d8d7b94 | 2008-07-21 10:34:34 -0500 | [diff] [blame] | 1707 | png_free(png_ptr, png_ptr->chunkdata); |
| 1708 | png_ptr->chunkdata = NULL; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1709 | png_free(png_ptr, params); |
| 1710 | return; |
| 1711 | } |
| 1712 | } |
| 1713 | |
Glenn Randers-Pehrson | d8d7b94 | 2008-07-21 10:34:34 -0500 | [diff] [blame] | 1714 | png_set_pCAL(png_ptr, info_ptr, png_ptr->chunkdata, X0, X1, type, nparams, |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1715 | units, params); |
| 1716 | |
Glenn Randers-Pehrson | d8d7b94 | 2008-07-21 10:34:34 -0500 | [diff] [blame] | 1717 | png_free(png_ptr, png_ptr->chunkdata); |
| 1718 | png_ptr->chunkdata = NULL; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1719 | png_free(png_ptr, params); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1720 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1721 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1722 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1723 | #if defined(PNG_READ_sCAL_SUPPORTED) |
| 1724 | /* read the sCAL chunk */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1725 | void /* PRIVATE */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1726 | png_handle_sCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
| 1727 | { |
Glenn Randers-Pehrson | d8d7b94 | 2008-07-21 10:34:34 -0500 | [diff] [blame] | 1728 | png_charp ep; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1729 | #ifdef PNG_FLOATING_POINT_SUPPORTED |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 1730 | double width, height; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1731 | png_charp vp; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1732 | #else |
| 1733 | #ifdef PNG_FIXED_POINT_SUPPORTED |
| 1734 | png_charp swidth, sheight; |
| 1735 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1736 | #endif |
| 1737 | png_size_t slength; |
| 1738 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1739 | png_debug(1, "in png_handle_sCAL"); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1740 | |
| 1741 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 1742 | png_error(png_ptr, "Missing IHDR before sCAL"); |
| 1743 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 1744 | { |
| 1745 | png_warning(png_ptr, "Invalid sCAL after IDAT"); |
| 1746 | png_crc_finish(png_ptr, length); |
| 1747 | return; |
| 1748 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1749 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL)) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1750 | { |
| 1751 | png_warning(png_ptr, "Duplicate sCAL chunk"); |
| 1752 | png_crc_finish(png_ptr, length); |
| 1753 | return; |
| 1754 | } |
| 1755 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1756 | png_debug1(2, "Allocating and reading sCAL chunk data (%lu bytes)", |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1757 | length + 1); |
Glenn Randers-Pehrson | d8d7b94 | 2008-07-21 10:34:34 -0500 | [diff] [blame] | 1758 | png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1); |
| 1759 | if (png_ptr->chunkdata == NULL) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1760 | { |
| 1761 | png_warning(png_ptr, "Out of memory while processing sCAL chunk"); |
| 1762 | return; |
| 1763 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1764 | slength = (png_size_t)length; |
Glenn Randers-Pehrson | d8d7b94 | 2008-07-21 10:34:34 -0500 | [diff] [blame] | 1765 | png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1766 | |
| 1767 | if (png_crc_finish(png_ptr, 0)) |
| 1768 | { |
Glenn Randers-Pehrson | d8d7b94 | 2008-07-21 10:34:34 -0500 | [diff] [blame] | 1769 | png_free(png_ptr, png_ptr->chunkdata); |
| 1770 | png_ptr->chunkdata = NULL; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1771 | return; |
| 1772 | } |
| 1773 | |
Glenn Randers-Pehrson | d8d7b94 | 2008-07-21 10:34:34 -0500 | [diff] [blame] | 1774 | png_ptr->chunkdata[slength] = 0x00; /* null terminate the last string */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1775 | |
Glenn Randers-Pehrson | d8d7b94 | 2008-07-21 10:34:34 -0500 | [diff] [blame] | 1776 | ep = png_ptr->chunkdata + 1; /* skip unit byte */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1777 | |
| 1778 | #ifdef PNG_FLOATING_POINT_SUPPORTED |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1779 | width = png_strtod(png_ptr, ep, &vp); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1780 | if (*vp) |
Glenn Randers-Pehrson | 4accabb | 2000-04-14 14:20:47 -0500 | [diff] [blame] | 1781 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1782 | png_warning(png_ptr, "malformed width string in sCAL chunk"); |
| 1783 | return; |
Glenn Randers-Pehrson | 4accabb | 2000-04-14 14:20:47 -0500 | [diff] [blame] | 1784 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1785 | #else |
| 1786 | #ifdef PNG_FIXED_POINT_SUPPORTED |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 1787 | swidth = (png_charp)png_malloc_warn(png_ptr, png_strlen(ep) + 1); |
| 1788 | if (swidth == NULL) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 1789 | { |
| 1790 | png_warning(png_ptr, "Out of memory while processing sCAL chunk width"); |
| 1791 | return; |
| 1792 | } |
| 1793 | png_memcpy(swidth, ep, png_strlen(ep)); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1794 | #endif |
| 1795 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1796 | |
Glenn Randers-Pehrson | d8d7b94 | 2008-07-21 10:34:34 -0500 | [diff] [blame] | 1797 | for (ep = png_ptr->chunkdata; *ep; ep++) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1798 | /* empty loop */ ; |
| 1799 | ep++; |
| 1800 | |
Glenn Randers-Pehrson | d8d7b94 | 2008-07-21 10:34:34 -0500 | [diff] [blame] | 1801 | if (png_ptr->chunkdata + slength < ep) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1802 | { |
| 1803 | png_warning(png_ptr, "Truncated sCAL chunk"); |
| 1804 | #if defined(PNG_FIXED_POINT_SUPPORTED) && \ |
| 1805 | !defined(PNG_FLOATING_POINT_SUPPORTED) |
| 1806 | png_free(png_ptr, swidth); |
| 1807 | #endif |
Glenn Randers-Pehrson | d8d7b94 | 2008-07-21 10:34:34 -0500 | [diff] [blame] | 1808 | png_free(png_ptr, png_ptr->chunkdata); |
| 1809 | png_ptr->chunkdata = NULL; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1810 | return; |
| 1811 | } |
| 1812 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1813 | #ifdef PNG_FLOATING_POINT_SUPPORTED |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1814 | height = png_strtod(png_ptr, ep, &vp); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1815 | if (*vp) |
Glenn Randers-Pehrson | 4accabb | 2000-04-14 14:20:47 -0500 | [diff] [blame] | 1816 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1817 | png_warning(png_ptr, "malformed height string in sCAL chunk"); |
| 1818 | return; |
Glenn Randers-Pehrson | 4accabb | 2000-04-14 14:20:47 -0500 | [diff] [blame] | 1819 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1820 | #else |
| 1821 | #ifdef PNG_FIXED_POINT_SUPPORTED |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 1822 | sheight = (png_charp)png_malloc_warn(png_ptr, png_strlen(ep) + 1); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1823 | if (sheight == NULL) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 1824 | { |
| 1825 | png_warning(png_ptr, "Out of memory while processing sCAL chunk height"); |
| 1826 | return; |
| 1827 | } |
| 1828 | png_memcpy(sheight, ep, png_strlen(ep)); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1829 | #endif |
| 1830 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1831 | |
Glenn Randers-Pehrson | d8d7b94 | 2008-07-21 10:34:34 -0500 | [diff] [blame] | 1832 | if (png_ptr->chunkdata + slength < ep |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1833 | #ifdef PNG_FLOATING_POINT_SUPPORTED |
| 1834 | || width <= 0. || height <= 0. |
| 1835 | #endif |
| 1836 | ) |
| 1837 | { |
| 1838 | png_warning(png_ptr, "Invalid sCAL data"); |
Glenn Randers-Pehrson | d8d7b94 | 2008-07-21 10:34:34 -0500 | [diff] [blame] | 1839 | png_free(png_ptr, png_ptr->chunkdata); |
| 1840 | png_ptr->chunkdata = NULL; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1841 | #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1842 | png_free(png_ptr, swidth); |
| 1843 | png_free(png_ptr, sheight); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1844 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1845 | return; |
| 1846 | } |
| 1847 | |
| 1848 | |
| 1849 | #ifdef PNG_FLOATING_POINT_SUPPORTED |
Glenn Randers-Pehrson | d8d7b94 | 2008-07-21 10:34:34 -0500 | [diff] [blame] | 1850 | png_set_sCAL(png_ptr, info_ptr, png_ptr->chunkdata[0], width, height); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1851 | #else |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1852 | #ifdef PNG_FIXED_POINT_SUPPORTED |
Glenn Randers-Pehrson | d8d7b94 | 2008-07-21 10:34:34 -0500 | [diff] [blame] | 1853 | png_set_sCAL_s(png_ptr, info_ptr, png_ptr->chunkdata[0], swidth, sheight); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1854 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1855 | #endif |
| 1856 | |
Glenn Randers-Pehrson | d8d7b94 | 2008-07-21 10:34:34 -0500 | [diff] [blame] | 1857 | png_free(png_ptr, png_ptr->chunkdata); |
| 1858 | png_ptr->chunkdata = NULL; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1859 | #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED) |
| 1860 | png_free(png_ptr, swidth); |
| 1861 | png_free(png_ptr, sheight); |
| 1862 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1863 | } |
| 1864 | #endif |
| 1865 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1866 | #if defined(PNG_READ_tIME_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1867 | void /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1868 | png_handle_tIME(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1869 | { |
| 1870 | png_byte buf[7]; |
| 1871 | png_time mod_time; |
| 1872 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1873 | png_debug(1, "in png_handle_tIME"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1874 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1875 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1876 | png_error(png_ptr, "Out of place tIME chunk"); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1877 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1878 | { |
| 1879 | png_warning(png_ptr, "Duplicate tIME chunk"); |
| 1880 | png_crc_finish(png_ptr, length); |
| 1881 | return; |
| 1882 | } |
| 1883 | |
| 1884 | if (png_ptr->mode & PNG_HAVE_IDAT) |
| 1885 | png_ptr->mode |= PNG_AFTER_IDAT; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1886 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1887 | if (length != 7) |
| 1888 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1889 | png_warning(png_ptr, "Incorrect tIME chunk length"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1890 | png_crc_finish(png_ptr, length); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1891 | return; |
| 1892 | } |
| 1893 | |
| 1894 | png_crc_read(png_ptr, buf, 7); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1895 | if (png_crc_finish(png_ptr, 0)) |
| 1896 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1897 | |
| 1898 | mod_time.second = buf[6]; |
| 1899 | mod_time.minute = buf[5]; |
| 1900 | mod_time.hour = buf[4]; |
| 1901 | mod_time.day = buf[3]; |
| 1902 | mod_time.month = buf[2]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1903 | mod_time.year = png_get_uint_16(buf); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1904 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1905 | png_set_tIME(png_ptr, info_ptr, &mod_time); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1906 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1907 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1908 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1909 | #if defined(PNG_READ_tEXt_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1910 | /* Note: this does not properly handle chunks that are > 64K under DOS */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1911 | void /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1912 | png_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1913 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1914 | png_textp text_ptr; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1915 | png_charp key; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1916 | png_charp text; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1917 | png_uint_32 skip = 0; |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1918 | png_size_t slength; |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 1919 | int ret; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1920 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1921 | png_debug(1, "in png_handle_tEXt"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1922 | |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 1923 | #ifdef PNG_SET_USER_LIMITS_SUPPORTED |
| 1924 | if (png_ptr->user_chunk_cache_max != 0) |
| 1925 | { |
| 1926 | if (png_ptr->user_chunk_cache_max == 1) |
| 1927 | { |
| 1928 | png_crc_finish(png_ptr, length); |
| 1929 | return; |
| 1930 | } |
| 1931 | if (--png_ptr->user_chunk_cache_max == 1) |
| 1932 | { |
| 1933 | png_warning(png_ptr, "No space in chunk cache for tEXt"); |
| 1934 | png_crc_finish(png_ptr, length); |
| 1935 | return; |
| 1936 | } |
| 1937 | } |
| 1938 | #endif |
| 1939 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1940 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 1941 | png_error(png_ptr, "Missing IHDR before tEXt"); |
| 1942 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1943 | if (png_ptr->mode & PNG_HAVE_IDAT) |
| 1944 | png_ptr->mode |= PNG_AFTER_IDAT; |
| 1945 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1946 | #ifdef PNG_MAX_MALLOC_64K |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 1947 | if (length > (png_uint_32)65535L) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1948 | { |
| 1949 | png_warning(png_ptr, "tEXt chunk too large to fit in memory"); |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 1950 | skip = length - (png_uint_32)65535L; |
| 1951 | length = (png_uint_32)65535L; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1952 | } |
| 1953 | #endif |
| 1954 | |
Glenn Randers-Pehrson | 97a9b48 | 2008-10-25 20:03:28 -0500 | [diff] [blame] | 1955 | png_free(png_ptr, png_ptr->chunkdata); |
| 1956 | |
| 1957 | png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1); |
| 1958 | if (png_ptr->chunkdata == NULL) |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 1959 | { |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 1960 | png_warning(png_ptr, "No memory to process text chunk"); |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 1961 | return; |
| 1962 | } |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1963 | slength = (png_size_t)length; |
Glenn Randers-Pehrson | 97a9b48 | 2008-10-25 20:03:28 -0500 | [diff] [blame] | 1964 | png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1965 | |
| 1966 | if (png_crc_finish(png_ptr, skip)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1967 | { |
Glenn Randers-Pehrson | 97a9b48 | 2008-10-25 20:03:28 -0500 | [diff] [blame] | 1968 | png_free(png_ptr, png_ptr->chunkdata); |
| 1969 | png_ptr->chunkdata = NULL; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1970 | return; |
| 1971 | } |
| 1972 | |
Glenn Randers-Pehrson | 97a9b48 | 2008-10-25 20:03:28 -0500 | [diff] [blame] | 1973 | key = png_ptr->chunkdata; |
| 1974 | |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1975 | key[slength] = 0x00; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1976 | |
| 1977 | for (text = key; *text; text++) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1978 | /* empty loop to find end of key */ ; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1979 | |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1980 | if (text != key + slength) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1981 | text++; |
| 1982 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1983 | text_ptr = (png_textp)png_malloc_warn(png_ptr, |
| 1984 | png_sizeof(png_text)); |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 1985 | if (text_ptr == NULL) |
| 1986 | { |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 1987 | png_warning(png_ptr, "Not enough memory to process text chunk"); |
Glenn Randers-Pehrson | 97a9b48 | 2008-10-25 20:03:28 -0500 | [diff] [blame] | 1988 | png_free(png_ptr, png_ptr->chunkdata); |
| 1989 | png_ptr->chunkdata = NULL; |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 1990 | return; |
| 1991 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1992 | text_ptr->compression = PNG_TEXT_COMPRESSION_NONE; |
| 1993 | text_ptr->key = key; |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1994 | #ifdef PNG_iTXt_SUPPORTED |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1995 | text_ptr->lang = NULL; |
| 1996 | text_ptr->lang_key = NULL; |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1997 | text_ptr->itxt_length = 0; |
| 1998 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1999 | text_ptr->text = text; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2000 | text_ptr->text_length = png_strlen(text); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2001 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2002 | ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2003 | |
Glenn Randers-Pehrson | 97a9b48 | 2008-10-25 20:03:28 -0500 | [diff] [blame] | 2004 | png_free(png_ptr, png_ptr->chunkdata); |
| 2005 | png_ptr->chunkdata = NULL; |
Glenn Randers-Pehrson | 82ae383 | 2001-04-20 10:32:10 -0500 | [diff] [blame] | 2006 | png_free(png_ptr, text_ptr); |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 2007 | if (ret) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 2008 | png_warning(png_ptr, "Insufficient memory to process text chunk"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2009 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2010 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2011 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2012 | #if defined(PNG_READ_zTXt_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2013 | /* note: this does not correctly handle chunks that are > 64K under DOS */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2014 | void /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2015 | png_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2016 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2017 | png_textp text_ptr; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 2018 | png_charp text; |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 2019 | int comp_type; |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 2020 | int ret; |
Glenn Randers-Pehrson | 68ea243 | 2000-04-01 21:10:05 -0600 | [diff] [blame] | 2021 | png_size_t slength, prefix_len, data_len; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2022 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2023 | png_debug(1, "in png_handle_zTXt"); |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 2024 | |
| 2025 | #ifdef PNG_SET_USER_LIMITS_SUPPORTED |
| 2026 | if (png_ptr->user_chunk_cache_max != 0) |
| 2027 | { |
| 2028 | if (png_ptr->user_chunk_cache_max == 1) |
| 2029 | { |
| 2030 | png_crc_finish(png_ptr, length); |
| 2031 | return; |
| 2032 | } |
| 2033 | if (--png_ptr->user_chunk_cache_max == 1) |
| 2034 | { |
| 2035 | png_warning(png_ptr, "No space in chunk cache for zTXt"); |
| 2036 | png_crc_finish(png_ptr, length); |
| 2037 | return; |
| 2038 | } |
| 2039 | } |
| 2040 | #endif |
| 2041 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2042 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 2043 | png_error(png_ptr, "Missing IHDR before zTXt"); |
| 2044 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2045 | if (png_ptr->mode & PNG_HAVE_IDAT) |
| 2046 | png_ptr->mode |= PNG_AFTER_IDAT; |
| 2047 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2048 | #ifdef PNG_MAX_MALLOC_64K |
| 2049 | /* We will no doubt have problems with chunks even half this size, but |
| 2050 | there is no hard and fast rule to tell us where to stop. */ |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 2051 | if (length > (png_uint_32)65535L) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2052 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2053 | png_warning(png_ptr, "zTXt chunk too large to fit in memory"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2054 | png_crc_finish(png_ptr, length); |
| 2055 | return; |
| 2056 | } |
| 2057 | #endif |
| 2058 | |
Glenn Randers-Pehrson | 97a9b48 | 2008-10-25 20:03:28 -0500 | [diff] [blame] | 2059 | png_free(png_ptr, png_ptr->chunkdata); |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 2060 | png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1); |
| 2061 | if (png_ptr->chunkdata == NULL) |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 2062 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2063 | png_warning(png_ptr, "Out of memory processing zTXt chunk"); |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 2064 | return; |
| 2065 | } |
| 2066 | slength = (png_size_t)length; |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 2067 | png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2068 | if (png_crc_finish(png_ptr, 0)) |
| 2069 | { |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 2070 | png_free(png_ptr, png_ptr->chunkdata); |
| 2071 | png_ptr->chunkdata = NULL; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2072 | return; |
| 2073 | } |
| 2074 | |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 2075 | png_ptr->chunkdata[slength] = 0x00; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2076 | |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 2077 | for (text = png_ptr->chunkdata; *text; text++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2078 | /* empty loop */ ; |
| 2079 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2080 | /* zTXt must have some text after the chunkdataword */ |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 2081 | if (text >= png_ptr->chunkdata + slength - 2) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2082 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2083 | png_warning(png_ptr, "Truncated zTXt chunk"); |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 2084 | png_free(png_ptr, png_ptr->chunkdata); |
| 2085 | png_ptr->chunkdata = NULL; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2086 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2087 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2088 | else |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2089 | { |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2090 | comp_type = *(++text); |
Glenn Randers-Pehrson | f05f803 | 2000-12-23 14:27:39 -0600 | [diff] [blame] | 2091 | if (comp_type != PNG_TEXT_COMPRESSION_zTXt) |
| 2092 | { |
| 2093 | png_warning(png_ptr, "Unknown compression type in zTXt chunk"); |
| 2094 | comp_type = PNG_TEXT_COMPRESSION_zTXt; |
| 2095 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2096 | text++; /* skip the compression_method byte */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2097 | } |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 2098 | prefix_len = text - png_ptr->chunkdata; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2099 | |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 2100 | png_decompress_chunk(png_ptr, comp_type, |
| 2101 | (png_size_t)length, prefix_len, &data_len); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2102 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2103 | text_ptr = (png_textp)png_malloc_warn(png_ptr, |
| 2104 | png_sizeof(png_text)); |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 2105 | if (text_ptr == NULL) |
| 2106 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2107 | png_warning(png_ptr, "Not enough memory to process zTXt chunk"); |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 2108 | png_free(png_ptr, png_ptr->chunkdata); |
| 2109 | png_ptr->chunkdata = NULL; |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 2110 | return; |
| 2111 | } |
Glenn Randers-Pehrson | 82ae383 | 2001-04-20 10:32:10 -0500 | [diff] [blame] | 2112 | text_ptr->compression = comp_type; |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 2113 | text_ptr->key = png_ptr->chunkdata; |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2114 | #ifdef PNG_iTXt_SUPPORTED |
Glenn Randers-Pehrson | 82ae383 | 2001-04-20 10:32:10 -0500 | [diff] [blame] | 2115 | text_ptr->lang = NULL; |
| 2116 | text_ptr->lang_key = NULL; |
| 2117 | text_ptr->itxt_length = 0; |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2118 | #endif |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 2119 | text_ptr->text = png_ptr->chunkdata + prefix_len; |
Glenn Randers-Pehrson | 82ae383 | 2001-04-20 10:32:10 -0500 | [diff] [blame] | 2120 | text_ptr->text_length = data_len; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2121 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2122 | ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2123 | |
Glenn Randers-Pehrson | 82ae383 | 2001-04-20 10:32:10 -0500 | [diff] [blame] | 2124 | png_free(png_ptr, text_ptr); |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 2125 | png_free(png_ptr, png_ptr->chunkdata); |
| 2126 | png_ptr->chunkdata = NULL; |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 2127 | if (ret) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 2128 | png_error(png_ptr, "Insufficient memory to store zTXt chunk"); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2129 | } |
| 2130 | #endif |
| 2131 | |
| 2132 | #if defined(PNG_READ_iTXt_SUPPORTED) |
| 2133 | /* note: this does not correctly handle chunks that are > 64K under DOS */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2134 | void /* PRIVATE */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2135 | png_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
| 2136 | { |
| 2137 | png_textp text_ptr; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2138 | png_charp key, lang, text, lang_key; |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 2139 | int comp_flag; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2140 | int comp_type = 0; |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 2141 | int ret; |
Glenn Randers-Pehrson | 68ea243 | 2000-04-01 21:10:05 -0600 | [diff] [blame] | 2142 | png_size_t slength, prefix_len, data_len; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2143 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2144 | png_debug(1, "in png_handle_iTXt"); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2145 | |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 2146 | #ifdef PNG_SET_USER_LIMITS_SUPPORTED |
| 2147 | if (png_ptr->user_chunk_cache_max != 0) |
| 2148 | { |
| 2149 | if (png_ptr->user_chunk_cache_max == 1) |
| 2150 | { |
| 2151 | png_crc_finish(png_ptr, length); |
| 2152 | return; |
| 2153 | } |
| 2154 | if (--png_ptr->user_chunk_cache_max == 1) |
| 2155 | { |
| 2156 | png_warning(png_ptr, "No space in chunk cache for iTXt"); |
| 2157 | png_crc_finish(png_ptr, length); |
| 2158 | return; |
| 2159 | } |
| 2160 | } |
| 2161 | #endif |
| 2162 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2163 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 2164 | png_error(png_ptr, "Missing IHDR before iTXt"); |
| 2165 | |
| 2166 | if (png_ptr->mode & PNG_HAVE_IDAT) |
| 2167 | png_ptr->mode |= PNG_AFTER_IDAT; |
| 2168 | |
| 2169 | #ifdef PNG_MAX_MALLOC_64K |
| 2170 | /* We will no doubt have problems with chunks even half this size, but |
| 2171 | there is no hard and fast rule to tell us where to stop. */ |
| 2172 | if (length > (png_uint_32)65535L) |
| 2173 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2174 | png_warning(png_ptr, "iTXt chunk too large to fit in memory"); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2175 | png_crc_finish(png_ptr, length); |
| 2176 | return; |
| 2177 | } |
| 2178 | #endif |
| 2179 | |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 2180 | png_free(png_ptr, png_ptr->chunkdata); |
| 2181 | png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1); |
| 2182 | if (png_ptr->chunkdata == NULL) |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 2183 | { |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 2184 | png_warning(png_ptr, "No memory to process iTXt chunk"); |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 2185 | return; |
| 2186 | } |
Glenn Randers-Pehrson | 82ae383 | 2001-04-20 10:32:10 -0500 | [diff] [blame] | 2187 | slength = (png_size_t)length; |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 2188 | png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2189 | if (png_crc_finish(png_ptr, 0)) |
| 2190 | { |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 2191 | png_free(png_ptr, png_ptr->chunkdata); |
| 2192 | png_ptr->chunkdata = NULL; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2193 | return; |
| 2194 | } |
| 2195 | |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 2196 | png_ptr->chunkdata[slength] = 0x00; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2197 | |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 2198 | for (lang = png_ptr->chunkdata; *lang; lang++) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2199 | /* empty loop */ ; |
| 2200 | lang++; /* skip NUL separator */ |
| 2201 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2202 | /* iTXt must have a language tag (possibly empty), two compression bytes, |
| 2203 | translated keyword (possibly empty), and possibly some text after the |
| 2204 | keyword */ |
| 2205 | |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 2206 | if (lang >= png_ptr->chunkdata + slength - 3) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2207 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2208 | png_warning(png_ptr, "Truncated iTXt chunk"); |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 2209 | png_free(png_ptr, png_ptr->chunkdata); |
| 2210 | png_ptr->chunkdata = NULL; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2211 | return; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2212 | } |
| 2213 | else |
| 2214 | { |
| 2215 | comp_flag = *lang++; |
| 2216 | comp_type = *lang++; |
| 2217 | } |
| 2218 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2219 | for (lang_key = lang; *lang_key; lang_key++) |
| 2220 | /* empty loop */ ; |
| 2221 | lang_key++; /* skip NUL separator */ |
| 2222 | |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 2223 | if (lang_key >= png_ptr->chunkdata + slength) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2224 | { |
| 2225 | png_warning(png_ptr, "Truncated iTXt chunk"); |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 2226 | png_free(png_ptr, png_ptr->chunkdata); |
| 2227 | png_ptr->chunkdata = NULL; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2228 | return; |
| 2229 | } |
| 2230 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2231 | for (text = lang_key; *text; text++) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2232 | /* empty loop */ ; |
| 2233 | text++; /* skip NUL separator */ |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 2234 | if (text >= png_ptr->chunkdata + slength) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2235 | { |
| 2236 | png_warning(png_ptr, "Malformed iTXt chunk"); |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 2237 | png_free(png_ptr, png_ptr->chunkdata); |
| 2238 | png_ptr->chunkdata = NULL; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2239 | return; |
| 2240 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2241 | |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 2242 | prefix_len = text - png_ptr->chunkdata; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2243 | |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 2244 | key=png_ptr->chunkdata; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2245 | if (comp_flag) |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 2246 | png_decompress_chunk(png_ptr, comp_type, |
| 2247 | (size_t)length, prefix_len, &data_len); |
Glenn Randers-Pehrson | 68ea243 | 2000-04-01 21:10:05 -0600 | [diff] [blame] | 2248 | else |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 2249 | data_len = png_strlen(png_ptr->chunkdata + prefix_len); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2250 | text_ptr = (png_textp)png_malloc_warn(png_ptr, |
| 2251 | png_sizeof(png_text)); |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 2252 | if (text_ptr == NULL) |
| 2253 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2254 | png_warning(png_ptr, "Not enough memory to process iTXt chunk"); |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 2255 | png_free(png_ptr, png_ptr->chunkdata); |
| 2256 | png_ptr->chunkdata = NULL; |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 2257 | return; |
| 2258 | } |
Glenn Randers-Pehrson | 82ae383 | 2001-04-20 10:32:10 -0500 | [diff] [blame] | 2259 | text_ptr->compression = (int)comp_flag + 1; |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 2260 | text_ptr->lang_key = png_ptr->chunkdata + (lang_key - key); |
| 2261 | text_ptr->lang = png_ptr->chunkdata + (lang - key); |
Glenn Randers-Pehrson | 82ae383 | 2001-04-20 10:32:10 -0500 | [diff] [blame] | 2262 | text_ptr->itxt_length = data_len; |
| 2263 | text_ptr->text_length = 0; |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 2264 | text_ptr->key = png_ptr->chunkdata; |
| 2265 | text_ptr->text = png_ptr->chunkdata + prefix_len; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2266 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2267 | ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2268 | |
Glenn Randers-Pehrson | 82ae383 | 2001-04-20 10:32:10 -0500 | [diff] [blame] | 2269 | png_free(png_ptr, text_ptr); |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 2270 | png_free(png_ptr, png_ptr->chunkdata); |
| 2271 | png_ptr->chunkdata = NULL; |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 2272 | if (ret) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 2273 | png_error(png_ptr, "Insufficient memory to store iTXt chunk"); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2274 | } |
| 2275 | #endif |
| 2276 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2277 | /* This function is called when we haven't found a handler for a |
| 2278 | chunk. If there isn't a problem with the chunk itself (ie bad |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2279 | chunk name, CRC, or a critical chunk), the chunk is silently ignored |
| 2280 | -- unless the PNG_FLAG_UNKNOWN_CHUNKS_SUPPORTED flag is on in which |
| 2281 | case it will be saved away to be written out later. */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2282 | void /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2283 | png_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
| 2284 | { |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2285 | png_uint_32 skip = 0; |
| 2286 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2287 | png_debug(1, "in png_handle_unknown"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2288 | |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 2289 | #ifdef PNG_SET_USER_LIMITS_SUPPORTED |
| 2290 | if (png_ptr->user_chunk_cache_max != 0) |
| 2291 | { |
| 2292 | if (png_ptr->user_chunk_cache_max == 1) |
| 2293 | { |
| 2294 | png_crc_finish(png_ptr, length); |
| 2295 | return; |
| 2296 | } |
| 2297 | if (--png_ptr->user_chunk_cache_max == 1) |
| 2298 | { |
| 2299 | png_warning(png_ptr, "No space in chunk cache for unknown chunk"); |
| 2300 | png_crc_finish(png_ptr, length); |
| 2301 | return; |
| 2302 | } |
| 2303 | } |
| 2304 | #endif |
| 2305 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2306 | if (png_ptr->mode & PNG_HAVE_IDAT) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2307 | { |
| 2308 | #ifdef PNG_USE_LOCAL_ARRAYS |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2309 | PNG_CONST PNG_IDAT; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2310 | #endif |
| 2311 | if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) /* not an IDAT */ |
| 2312 | png_ptr->mode |= PNG_AFTER_IDAT; |
| 2313 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2314 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2315 | if (!(png_ptr->chunk_name[0] & 0x20)) |
| 2316 | { |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 2317 | #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2318 | if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) != |
Glenn Randers-Pehrson | dff799e | 2004-08-07 21:42:49 -0500 | [diff] [blame] | 2319 | PNG_HANDLE_CHUNK_ALWAYS |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2320 | #if defined(PNG_READ_USER_CHUNKS_SUPPORTED) |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 2321 | && png_ptr->read_user_chunk_fn == NULL |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2322 | #endif |
| 2323 | ) |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 2324 | #endif |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2325 | png_chunk_error(png_ptr, "unknown critical chunk"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2326 | } |
| 2327 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2328 | #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 2329 | if ((png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS) || |
| 2330 | (png_ptr->read_user_chunk_fn != NULL)) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2331 | { |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2332 | #ifdef PNG_MAX_MALLOC_64K |
| 2333 | if (length > (png_uint_32)65535L) |
| 2334 | { |
| 2335 | png_warning(png_ptr, "unknown chunk too large to fit in memory"); |
| 2336 | skip = length - (png_uint_32)65535L; |
| 2337 | length = (png_uint_32)65535L; |
| 2338 | } |
| 2339 | #endif |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2340 | png_memcpy((png_charp)png_ptr->unknown_chunk.name, |
| 2341 | (png_charp)png_ptr->chunk_name, |
| 2342 | png_sizeof(png_ptr->unknown_chunk.name)); |
| 2343 | png_ptr->unknown_chunk.name[png_sizeof(png_ptr->unknown_chunk.name)-1] = '\0'; |
| 2344 | png_ptr->unknown_chunk.size = (png_size_t)length; |
| 2345 | if (length == 0) |
| 2346 | png_ptr->unknown_chunk.data = NULL; |
| 2347 | else |
| 2348 | { |
| 2349 | png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr, length); |
| 2350 | png_crc_read(png_ptr, (png_bytep)png_ptr->unknown_chunk.data, length); |
| 2351 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2352 | #if defined(PNG_READ_USER_CHUNKS_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2353 | if (png_ptr->read_user_chunk_fn != NULL) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2354 | { |
| 2355 | /* callback to user unknown chunk handler */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2356 | int ret; |
| 2357 | ret = (*(png_ptr->read_user_chunk_fn)) |
| 2358 | (png_ptr, &png_ptr->unknown_chunk); |
| 2359 | if (ret < 0) |
| 2360 | png_chunk_error(png_ptr, "error in user chunk"); |
| 2361 | if (ret == 0) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2362 | { |
| 2363 | if (!(png_ptr->chunk_name[0] & 0x20)) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2364 | if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) != |
Glenn Randers-Pehrson | dff799e | 2004-08-07 21:42:49 -0500 | [diff] [blame] | 2365 | PNG_HANDLE_CHUNK_ALWAYS) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2366 | png_chunk_error(png_ptr, "unknown critical chunk"); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2367 | png_set_unknown_chunks(png_ptr, info_ptr, |
| 2368 | &png_ptr->unknown_chunk, 1); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2369 | } |
| 2370 | } |
| 2371 | else |
| 2372 | #endif |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2373 | png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1); |
| 2374 | png_free(png_ptr, png_ptr->unknown_chunk.data); |
| 2375 | png_ptr->unknown_chunk.data = NULL; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2376 | } |
| 2377 | else |
| 2378 | #endif |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2379 | skip = length; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2380 | |
| 2381 | png_crc_finish(png_ptr, skip); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2382 | |
| 2383 | #if !defined(PNG_READ_USER_CHUNKS_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2384 | info_ptr = info_ptr; /* quiet compiler warnings about unused info_ptr */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2385 | #endif |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2386 | } |
| 2387 | |
| 2388 | /* This function is called to verify that a chunk name is valid. |
| 2389 | This function can't have the "critical chunk check" incorporated |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2390 | into it, since in the future we will need to be able to call user |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2391 | functions to handle unknown critical chunks after we check that |
| 2392 | the chunk name itself is valid. */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2393 | |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 2394 | #define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2395 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2396 | void /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2397 | png_check_chunk_name(png_structp png_ptr, png_bytep chunk_name) |
| 2398 | { |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2399 | png_debug(1, "in png_check_chunk_name"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2400 | if (isnonalpha(chunk_name[0]) || isnonalpha(chunk_name[1]) || |
Glenn Randers-Pehrson | eb58091 | 2008-07-30 14:47:09 -0500 | [diff] [blame] | 2401 | isnonalpha(chunk_name[2]) || isnonalpha(chunk_name[3])) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2402 | { |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 2403 | png_chunk_error(png_ptr, "invalid chunk type"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2404 | } |
| 2405 | } |
| 2406 | |
Glenn Randers-Pehrson | 345bc27 | 1998-06-14 14:43:31 -0500 | [diff] [blame] | 2407 | /* Combines the row recently read in with the existing pixels in the |
| 2408 | row. This routine takes care of alpha and transparency if requested. |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2409 | This routine also handles the two methods of progressive display |
| 2410 | of interlaced images, depending on the mask value. |
| 2411 | The mask value describes which pixels are to be combined with |
| 2412 | the row. The pattern always repeats every 8 pixels, so just 8 |
Glenn Randers-Pehrson | 345bc27 | 1998-06-14 14:43:31 -0500 | [diff] [blame] | 2413 | bits are needed. A one indicates the pixel is to be combined, |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2414 | a zero indicates the pixel is to be skipped. This is in addition |
| 2415 | to any alpha or transparency value associated with the pixel. If |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2416 | you want all pixels to be combined, pass 0xff (255) in mask. */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2417 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2418 | void /* PRIVATE */ |
Glenn Randers-Pehrson | 231e687 | 2001-01-12 15:13:06 -0600 | [diff] [blame] | 2419 | png_combine_row(png_structp png_ptr, png_bytep row, int mask) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2420 | { |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2421 | png_debug(1, "in png_combine_row"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2422 | if (mask == 0xff) |
| 2423 | { |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2424 | png_memcpy(row, png_ptr->row_buf + 1, |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 2425 | PNG_ROWBYTES(png_ptr->row_info.pixel_depth, png_ptr->width)); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2426 | } |
| 2427 | else |
| 2428 | { |
| 2429 | switch (png_ptr->row_info.pixel_depth) |
| 2430 | { |
| 2431 | case 1: |
| 2432 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2433 | png_bytep sp = png_ptr->row_buf + 1; |
| 2434 | png_bytep dp = row; |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2435 | int s_inc, s_start, s_end; |
| 2436 | int m = 0x80; |
| 2437 | int shift; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2438 | png_uint_32 i; |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2439 | png_uint_32 row_width = png_ptr->width; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2440 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2441 | #if defined(PNG_READ_PACKSWAP_SUPPORTED) |
| 2442 | if (png_ptr->transformations & PNG_PACKSWAP) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2443 | { |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2444 | s_start = 0; |
| 2445 | s_end = 7; |
| 2446 | s_inc = 1; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2447 | } |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2448 | else |
| 2449 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2450 | { |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2451 | s_start = 7; |
| 2452 | s_end = 0; |
| 2453 | s_inc = -1; |
| 2454 | } |
| 2455 | |
| 2456 | shift = s_start; |
| 2457 | |
| 2458 | for (i = 0; i < row_width; i++) |
| 2459 | { |
| 2460 | if (m & mask) |
| 2461 | { |
| 2462 | int value; |
| 2463 | |
| 2464 | value = (*sp >> shift) & 0x01; |
| 2465 | *dp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff); |
| 2466 | *dp |= (png_byte)(value << shift); |
| 2467 | } |
| 2468 | |
| 2469 | if (shift == s_end) |
| 2470 | { |
| 2471 | shift = s_start; |
| 2472 | sp++; |
| 2473 | dp++; |
| 2474 | } |
| 2475 | else |
| 2476 | shift += s_inc; |
| 2477 | |
| 2478 | if (m == 1) |
| 2479 | m = 0x80; |
| 2480 | else |
| 2481 | m >>= 1; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2482 | } |
| 2483 | break; |
| 2484 | } |
| 2485 | case 2: |
| 2486 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2487 | png_bytep sp = png_ptr->row_buf + 1; |
| 2488 | png_bytep dp = row; |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2489 | int s_start, s_end, s_inc; |
Glenn Randers-Pehrson | 5dd2b8e | 2004-11-24 07:50:16 -0600 | [diff] [blame] | 2490 | int m = 0x80; |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2491 | int shift; |
| 2492 | png_uint_32 i; |
| 2493 | png_uint_32 row_width = png_ptr->width; |
| 2494 | int value; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2495 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2496 | #if defined(PNG_READ_PACKSWAP_SUPPORTED) |
| 2497 | if (png_ptr->transformations & PNG_PACKSWAP) |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2498 | { |
| 2499 | s_start = 0; |
| 2500 | s_end = 6; |
| 2501 | s_inc = 2; |
| 2502 | } |
| 2503 | else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2504 | #endif |
| 2505 | { |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2506 | s_start = 6; |
| 2507 | s_end = 0; |
| 2508 | s_inc = -2; |
| 2509 | } |
| 2510 | |
| 2511 | shift = s_start; |
| 2512 | |
| 2513 | for (i = 0; i < row_width; i++) |
| 2514 | { |
| 2515 | if (m & mask) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2516 | { |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2517 | value = (*sp >> shift) & 0x03; |
| 2518 | *dp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff); |
| 2519 | *dp |= (png_byte)(value << shift); |
Glenn Randers-Pehrson | 5dd2b8e | 2004-11-24 07:50:16 -0600 | [diff] [blame] | 2520 | } |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2521 | |
| 2522 | if (shift == s_end) |
| 2523 | { |
| 2524 | shift = s_start; |
| 2525 | sp++; |
| 2526 | dp++; |
| 2527 | } |
| 2528 | else |
| 2529 | shift += s_inc; |
| 2530 | if (m == 1) |
| 2531 | m = 0x80; |
| 2532 | else |
| 2533 | m >>= 1; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2534 | } |
| 2535 | break; |
| 2536 | } |
| 2537 | case 4: |
| 2538 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2539 | png_bytep sp = png_ptr->row_buf + 1; |
| 2540 | png_bytep dp = row; |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2541 | int s_start, s_end, s_inc; |
Glenn Randers-Pehrson | 5dd2b8e | 2004-11-24 07:50:16 -0600 | [diff] [blame] | 2542 | int m = 0x80; |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2543 | int shift; |
| 2544 | png_uint_32 i; |
| 2545 | png_uint_32 row_width = png_ptr->width; |
| 2546 | int value; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2547 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2548 | #if defined(PNG_READ_PACKSWAP_SUPPORTED) |
| 2549 | if (png_ptr->transformations & PNG_PACKSWAP) |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2550 | { |
| 2551 | s_start = 0; |
| 2552 | s_end = 4; |
| 2553 | s_inc = 4; |
| 2554 | } |
| 2555 | else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2556 | #endif |
| 2557 | { |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2558 | s_start = 4; |
| 2559 | s_end = 0; |
| 2560 | s_inc = -4; |
| 2561 | } |
| 2562 | shift = s_start; |
| 2563 | |
| 2564 | for (i = 0; i < row_width; i++) |
| 2565 | { |
| 2566 | if (m & mask) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2567 | { |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2568 | value = (*sp >> shift) & 0xf; |
| 2569 | *dp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff); |
| 2570 | *dp |= (png_byte)(value << shift); |
Glenn Randers-Pehrson | 5dd2b8e | 2004-11-24 07:50:16 -0600 | [diff] [blame] | 2571 | } |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2572 | |
| 2573 | if (shift == s_end) |
| 2574 | { |
| 2575 | shift = s_start; |
| 2576 | sp++; |
| 2577 | dp++; |
| 2578 | } |
| 2579 | else |
| 2580 | shift += s_inc; |
| 2581 | if (m == 1) |
| 2582 | m = 0x80; |
| 2583 | else |
| 2584 | m >>= 1; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2585 | } |
| 2586 | break; |
| 2587 | } |
| 2588 | default: |
| 2589 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2590 | png_bytep sp = png_ptr->row_buf + 1; |
| 2591 | png_bytep dp = row; |
| 2592 | png_size_t pixel_bytes = (png_ptr->row_info.pixel_depth >> 3); |
| 2593 | png_uint_32 i; |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2594 | png_uint_32 row_width = png_ptr->width; |
| 2595 | png_byte m = 0x80; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2596 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2597 | |
| 2598 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2599 | { |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2600 | if (m & mask) |
| 2601 | { |
| 2602 | png_memcpy(dp, sp, pixel_bytes); |
| 2603 | } |
| 2604 | |
| 2605 | sp += pixel_bytes; |
| 2606 | dp += pixel_bytes; |
| 2607 | |
| 2608 | if (m == 1) |
| 2609 | m = 0x80; |
| 2610 | else |
| 2611 | m >>= 1; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2612 | } |
| 2613 | break; |
| 2614 | } |
| 2615 | } |
| 2616 | } |
| 2617 | } |
| 2618 | |
Glenn Randers-Pehrson | 231e687 | 2001-01-12 15:13:06 -0600 | [diff] [blame] | 2619 | #ifdef PNG_READ_INTERLACING_SUPPORTED |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 2620 | /* OLD pre-1.0.9 interface: |
| 2621 | void png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass, |
| 2622 | png_uint_32 transformations) |
| 2623 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2624 | void /* PRIVATE */ |
Glenn Randers-Pehrson | 231e687 | 2001-01-12 15:13:06 -0600 | [diff] [blame] | 2625 | png_do_read_interlace(png_structp png_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2626 | { |
Glenn Randers-Pehrson | 231e687 | 2001-01-12 15:13:06 -0600 | [diff] [blame] | 2627 | png_row_infop row_info = &(png_ptr->row_info); |
| 2628 | png_bytep row = png_ptr->row_buf + 1; |
| 2629 | int pass = png_ptr->pass; |
| 2630 | png_uint_32 transformations = png_ptr->transformations; |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 2631 | #ifdef PNG_USE_LOCAL_ARRAYS |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 2632 | /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */ |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 2633 | /* offset to next interlace block */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2634 | PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 2635 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2636 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2637 | png_debug(1, "in png_do_read_interlace"); |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2638 | if (row != NULL && row_info != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2639 | { |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2640 | png_uint_32 final_width; |
| 2641 | |
| 2642 | final_width = row_info->width * png_pass_inc[pass]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2643 | |
| 2644 | switch (row_info->pixel_depth) |
| 2645 | { |
| 2646 | case 1: |
| 2647 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2648 | png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3); |
| 2649 | png_bytep dp = row + (png_size_t)((final_width - 1) >> 3); |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2650 | int sshift, dshift; |
| 2651 | int s_start, s_end, s_inc; |
| 2652 | int jstop = png_pass_inc[pass]; |
| 2653 | png_byte v; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2654 | png_uint_32 i; |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2655 | int j; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2656 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2657 | #if defined(PNG_READ_PACKSWAP_SUPPORTED) |
| 2658 | if (transformations & PNG_PACKSWAP) |
| 2659 | { |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2660 | sshift = (int)((row_info->width + 7) & 0x07); |
| 2661 | dshift = (int)((final_width + 7) & 0x07); |
| 2662 | s_start = 7; |
| 2663 | s_end = 0; |
| 2664 | s_inc = -1; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2665 | } |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2666 | else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2667 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2668 | { |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2669 | sshift = 7 - (int)((row_info->width + 7) & 0x07); |
| 2670 | dshift = 7 - (int)((final_width + 7) & 0x07); |
| 2671 | s_start = 0; |
| 2672 | s_end = 7; |
| 2673 | s_inc = 1; |
| 2674 | } |
Glenn Randers-Pehrson | 5dd2b8e | 2004-11-24 07:50:16 -0600 | [diff] [blame] | 2675 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2676 | for (i = 0; i < row_info->width; i++) |
| 2677 | { |
| 2678 | v = (png_byte)((*sp >> sshift) & 0x01); |
| 2679 | for (j = 0; j < jstop; j++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2680 | { |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2681 | *dp &= (png_byte)((0x7f7f >> (7 - dshift)) & 0xff); |
| 2682 | *dp |= (png_byte)(v << dshift); |
| 2683 | if (dshift == s_end) |
| 2684 | { |
| 2685 | dshift = s_start; |
| 2686 | dp--; |
| 2687 | } |
| 2688 | else |
| 2689 | dshift += s_inc; |
| 2690 | } |
| 2691 | if (sshift == s_end) |
| 2692 | { |
| 2693 | sshift = s_start; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2694 | sp--; |
| 2695 | } |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2696 | else |
| 2697 | sshift += s_inc; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2698 | } |
| 2699 | break; |
| 2700 | } |
| 2701 | case 2: |
| 2702 | { |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2703 | png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2); |
| 2704 | png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2); |
| 2705 | int sshift, dshift; |
| 2706 | int s_start, s_end, s_inc; |
| 2707 | int jstop = png_pass_inc[pass]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2708 | png_uint_32 i; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2709 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2710 | #if defined(PNG_READ_PACKSWAP_SUPPORTED) |
| 2711 | if (transformations & PNG_PACKSWAP) |
| 2712 | { |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2713 | sshift = (int)(((row_info->width + 3) & 0x03) << 1); |
| 2714 | dshift = (int)(((final_width + 3) & 0x03) << 1); |
| 2715 | s_start = 6; |
| 2716 | s_end = 0; |
| 2717 | s_inc = -2; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2718 | } |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2719 | else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2720 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2721 | { |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2722 | sshift = (int)((3 - ((row_info->width + 3) & 0x03)) << 1); |
| 2723 | dshift = (int)((3 - ((final_width + 3) & 0x03)) << 1); |
| 2724 | s_start = 0; |
| 2725 | s_end = 6; |
| 2726 | s_inc = 2; |
| 2727 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2728 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2729 | for (i = 0; i < row_info->width; i++) |
| 2730 | { |
| 2731 | png_byte v; |
| 2732 | int j; |
| 2733 | |
| 2734 | v = (png_byte)((*sp >> sshift) & 0x03); |
| 2735 | for (j = 0; j < jstop; j++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2736 | { |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2737 | *dp &= (png_byte)((0x3f3f >> (6 - dshift)) & 0xff); |
| 2738 | *dp |= (png_byte)(v << dshift); |
| 2739 | if (dshift == s_end) |
| 2740 | { |
| 2741 | dshift = s_start; |
| 2742 | dp--; |
| 2743 | } |
| 2744 | else |
| 2745 | dshift += s_inc; |
| 2746 | } |
| 2747 | if (sshift == s_end) |
| 2748 | { |
| 2749 | sshift = s_start; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2750 | sp--; |
| 2751 | } |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2752 | else |
| 2753 | sshift += s_inc; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2754 | } |
| 2755 | break; |
| 2756 | } |
| 2757 | case 4: |
| 2758 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2759 | png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1); |
| 2760 | png_bytep dp = row + (png_size_t)((final_width - 1) >> 1); |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2761 | int sshift, dshift; |
| 2762 | int s_start, s_end, s_inc; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2763 | png_uint_32 i; |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2764 | int jstop = png_pass_inc[pass]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2765 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2766 | #if defined(PNG_READ_PACKSWAP_SUPPORTED) |
| 2767 | if (transformations & PNG_PACKSWAP) |
| 2768 | { |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2769 | sshift = (int)(((row_info->width + 1) & 0x01) << 2); |
| 2770 | dshift = (int)(((final_width + 1) & 0x01) << 2); |
| 2771 | s_start = 4; |
| 2772 | s_end = 0; |
| 2773 | s_inc = -4; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2774 | } |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2775 | else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2776 | #endif |
| 2777 | { |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2778 | sshift = (int)((1 - ((row_info->width + 1) & 0x01)) << 2); |
| 2779 | dshift = (int)((1 - ((final_width + 1) & 0x01)) << 2); |
| 2780 | s_start = 0; |
| 2781 | s_end = 4; |
| 2782 | s_inc = 4; |
| 2783 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2784 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2785 | for (i = 0; i < row_info->width; i++) |
| 2786 | { |
| 2787 | png_byte v = (png_byte)((*sp >> sshift) & 0xf); |
| 2788 | int j; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2789 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2790 | for (j = 0; j < jstop; j++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2791 | { |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2792 | *dp &= (png_byte)((0xf0f >> (4 - dshift)) & 0xff); |
| 2793 | *dp |= (png_byte)(v << dshift); |
| 2794 | if (dshift == s_end) |
| 2795 | { |
| 2796 | dshift = s_start; |
| 2797 | dp--; |
| 2798 | } |
| 2799 | else |
| 2800 | dshift += s_inc; |
| 2801 | } |
| 2802 | if (sshift == s_end) |
| 2803 | { |
| 2804 | sshift = s_start; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2805 | sp--; |
| 2806 | } |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2807 | else |
| 2808 | sshift += s_inc; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2809 | } |
| 2810 | break; |
| 2811 | } |
| 2812 | default: |
| 2813 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2814 | png_size_t pixel_bytes = (row_info->pixel_depth >> 3); |
| 2815 | png_bytep sp = row + (png_size_t)(row_info->width - 1) * pixel_bytes; |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2816 | png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2817 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2818 | int jstop = png_pass_inc[pass]; |
| 2819 | png_uint_32 i; |
| 2820 | |
| 2821 | for (i = 0; i < row_info->width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2822 | { |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 2823 | png_byte v[8]; |
| 2824 | int j; |
| 2825 | |
| 2826 | png_memcpy(v, sp, pixel_bytes); |
| 2827 | for (j = 0; j < jstop; j++) |
| 2828 | { |
| 2829 | png_memcpy(dp, v, pixel_bytes); |
| 2830 | dp -= pixel_bytes; |
| 2831 | } |
| 2832 | sp -= pixel_bytes; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2833 | } |
| 2834 | break; |
| 2835 | } |
| 2836 | } |
| 2837 | row_info->width = final_width; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2838 | row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2839 | } |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 2840 | #if !defined(PNG_READ_PACKSWAP_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2841 | transformations = transformations; /* silence compiler warning */ |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 2842 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2843 | } |
Glenn Randers-Pehrson | 231e687 | 2001-01-12 15:13:06 -0600 | [diff] [blame] | 2844 | #endif /* PNG_READ_INTERLACING_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2845 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2846 | void /* PRIVATE */ |
Glenn Randers-Pehrson | 231e687 | 2001-01-12 15:13:06 -0600 | [diff] [blame] | 2847 | png_read_filter_row(png_structp png_ptr, png_row_infop row_info, png_bytep row, |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2848 | png_bytep prev_row, int filter) |
| 2849 | { |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2850 | png_debug(1, "in png_read_filter_row"); |
| 2851 | png_debug2(2, "row = %lu, filter = %d", png_ptr->row_number, filter); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2852 | switch (filter) |
| 2853 | { |
| 2854 | case PNG_FILTER_VALUE_NONE: |
| 2855 | break; |
| 2856 | case PNG_FILTER_VALUE_SUB: |
| 2857 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2858 | png_uint_32 i; |
| 2859 | png_uint_32 istop = row_info->rowbytes; |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 2860 | png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2861 | png_bytep rp = row + bpp; |
| 2862 | png_bytep lp = row; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2863 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2864 | for (i = bpp; i < istop; i++) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2865 | { |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 2866 | *rp = (png_byte)(((int)(*rp) + (int)(*lp++)) & 0xff); |
| 2867 | rp++; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2868 | } |
| 2869 | break; |
| 2870 | } |
| 2871 | case PNG_FILTER_VALUE_UP: |
| 2872 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2873 | png_uint_32 i; |
| 2874 | png_uint_32 istop = row_info->rowbytes; |
| 2875 | png_bytep rp = row; |
| 2876 | png_bytep pp = prev_row; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2877 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2878 | for (i = 0; i < istop; i++) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2879 | { |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 2880 | *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff); |
| 2881 | rp++; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2882 | } |
| 2883 | break; |
| 2884 | } |
| 2885 | case PNG_FILTER_VALUE_AVG: |
| 2886 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2887 | png_uint_32 i; |
| 2888 | png_bytep rp = row; |
| 2889 | png_bytep pp = prev_row; |
| 2890 | png_bytep lp = row; |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 2891 | png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3; |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 2892 | png_uint_32 istop = row_info->rowbytes - bpp; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2893 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2894 | for (i = 0; i < bpp; i++) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2895 | { |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 2896 | *rp = (png_byte)(((int)(*rp) + |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 2897 | ((int)(*pp++) / 2 )) & 0xff); |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 2898 | rp++; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2899 | } |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2900 | |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 2901 | for (i = 0; i < istop; i++) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2902 | { |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 2903 | *rp = (png_byte)(((int)(*rp) + |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 2904 | (int)(*pp++ + *lp++) / 2 ) & 0xff); |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 2905 | rp++; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2906 | } |
| 2907 | break; |
| 2908 | } |
| 2909 | case PNG_FILTER_VALUE_PAETH: |
| 2910 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2911 | png_uint_32 i; |
| 2912 | png_bytep rp = row; |
| 2913 | png_bytep pp = prev_row; |
| 2914 | png_bytep lp = row; |
| 2915 | png_bytep cp = prev_row; |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 2916 | png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3; |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 2917 | png_uint_32 istop=row_info->rowbytes - bpp; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2918 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2919 | for (i = 0; i < bpp; i++) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2920 | { |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 2921 | *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff); |
| 2922 | rp++; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2923 | } |
| 2924 | |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 2925 | for (i = 0; i < istop; i++) /* use leftover rp,pp */ |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2926 | { |
| 2927 | int a, b, c, pa, pb, pc, p; |
| 2928 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2929 | a = *lp++; |
| 2930 | b = *pp++; |
| 2931 | c = *cp++; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2932 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2933 | p = b - c; |
| 2934 | pc = a - c; |
| 2935 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2936 | #ifdef PNG_USE_ABS |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2937 | pa = abs(p); |
| 2938 | pb = abs(pc); |
| 2939 | pc = abs(p + pc); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2940 | #else |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2941 | pa = p < 0 ? -p : p; |
| 2942 | pb = pc < 0 ? -pc : pc; |
| 2943 | pc = (p + pc) < 0 ? -(p + pc) : p + pc; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2944 | #endif |
| 2945 | |
| 2946 | /* |
| 2947 | if (pa <= pb && pa <= pc) |
| 2948 | p = a; |
| 2949 | else if (pb <= pc) |
| 2950 | p = b; |
| 2951 | else |
| 2952 | p = c; |
| 2953 | */ |
| 2954 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2955 | p = (pa <= pb && pa <= pc) ? a : (pb <= pc) ? b : c; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2956 | |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 2957 | *rp = (png_byte)(((int)(*rp) + p) & 0xff); |
| 2958 | rp++; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2959 | } |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2960 | break; |
| 2961 | } |
| 2962 | default: |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2963 | png_warning(png_ptr, "Ignoring bad adaptive filter type"); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2964 | *row = 0; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2965 | break; |
| 2966 | } |
| 2967 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2968 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2969 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 2970 | png_read_finish_row(png_structp png_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2971 | { |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 2972 | #ifdef PNG_USE_LOCAL_ARRAYS |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2973 | #ifdef PNG_READ_INTERLACING_SUPPORTED |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 2974 | /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2975 | |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 2976 | /* start of interlace block */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2977 | PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2978 | |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 2979 | /* offset to next interlace block */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2980 | PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2981 | |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 2982 | /* start of interlace block in the y direction */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2983 | PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2984 | |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 2985 | /* offset to next interlace block in the y direction */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2986 | PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; |
| 2987 | #endif /* PNG_READ_INTERLACING_SUPPORTED */ |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 2988 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2989 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2990 | png_debug(1, "in png_read_finish_row"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2991 | png_ptr->row_number++; |
| 2992 | if (png_ptr->row_number < png_ptr->num_rows) |
| 2993 | return; |
| 2994 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2995 | #ifdef PNG_READ_INTERLACING_SUPPORTED |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2996 | if (png_ptr->interlaced) |
| 2997 | { |
| 2998 | png_ptr->row_number = 0; |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 2999 | png_memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3000 | do |
| 3001 | { |
| 3002 | png_ptr->pass++; |
| 3003 | if (png_ptr->pass >= 7) |
| 3004 | break; |
| 3005 | png_ptr->iwidth = (png_ptr->width + |
| 3006 | png_pass_inc[png_ptr->pass] - 1 - |
| 3007 | png_pass_start[png_ptr->pass]) / |
| 3008 | png_pass_inc[png_ptr->pass]; |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 3009 | |
| 3010 | png_ptr->irowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, |
| 3011 | png_ptr->iwidth) + 1; |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 3012 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3013 | if (!(png_ptr->transformations & PNG_INTERLACE)) |
| 3014 | { |
| 3015 | png_ptr->num_rows = (png_ptr->height + |
| 3016 | png_pass_yinc[png_ptr->pass] - 1 - |
| 3017 | png_pass_ystart[png_ptr->pass]) / |
| 3018 | png_pass_yinc[png_ptr->pass]; |
| 3019 | if (!(png_ptr->num_rows)) |
| 3020 | continue; |
| 3021 | } |
Glenn Randers-Pehrson | 345bc27 | 1998-06-14 14:43:31 -0500 | [diff] [blame] | 3022 | else /* if (png_ptr->transformations & PNG_INTERLACE) */ |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3023 | break; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3024 | } while (png_ptr->iwidth == 0); |
| 3025 | |
| 3026 | if (png_ptr->pass < 7) |
| 3027 | return; |
| 3028 | } |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 3029 | #endif /* PNG_READ_INTERLACING_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3030 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 3031 | if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3032 | { |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 3033 | #ifdef PNG_USE_LOCAL_ARRAYS |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 3034 | PNG_CONST PNG_IDAT; |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 3035 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3036 | char extra; |
| 3037 | int ret; |
| 3038 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3039 | png_ptr->zstream.next_out = (Byte *)&extra; |
| 3040 | png_ptr->zstream.avail_out = (uInt)1; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 3041 | for (;;) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3042 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3043 | if (!(png_ptr->zstream.avail_in)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3044 | { |
| 3045 | while (!png_ptr->idat_size) |
| 3046 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3047 | png_byte chunk_length[4]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3048 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3049 | png_crc_finish(png_ptr, 0); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3050 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3051 | png_read_data(png_ptr, chunk_length, 4); |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 3052 | png_ptr->idat_size = png_get_uint_31(png_ptr, chunk_length); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3053 | png_reset_crc(png_ptr); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3054 | png_crc_read(png_ptr, png_ptr->chunk_name, 4); |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 3055 | if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 3056 | png_error(png_ptr, "Not enough image data"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3057 | |
| 3058 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3059 | png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size; |
| 3060 | png_ptr->zstream.next_in = png_ptr->zbuf; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3061 | if (png_ptr->zbuf_size > png_ptr->idat_size) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3062 | png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size; |
| 3063 | png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zstream.avail_in); |
| 3064 | png_ptr->idat_size -= png_ptr->zstream.avail_in; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3065 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3066 | ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3067 | if (ret == Z_STREAM_END) |
| 3068 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3069 | if (!(png_ptr->zstream.avail_out) || png_ptr->zstream.avail_in || |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3070 | png_ptr->idat_size) |
Glenn Randers-Pehrson | 859665d | 2002-08-06 18:06:11 -0500 | [diff] [blame] | 3071 | png_warning(png_ptr, "Extra compressed data"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3072 | png_ptr->mode |= PNG_AFTER_IDAT; |
| 3073 | png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3074 | break; |
| 3075 | } |
| 3076 | if (ret != Z_OK) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3077 | png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg : |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 3078 | "Decompression Error"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3079 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3080 | if (!(png_ptr->zstream.avail_out)) |
Glenn Randers-Pehrson | 859665d | 2002-08-06 18:06:11 -0500 | [diff] [blame] | 3081 | { |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 3082 | png_warning(png_ptr, "Extra compressed data"); |
Glenn Randers-Pehrson | 859665d | 2002-08-06 18:06:11 -0500 | [diff] [blame] | 3083 | png_ptr->mode |= PNG_AFTER_IDAT; |
| 3084 | png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; |
| 3085 | break; |
| 3086 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3087 | |
Glenn Randers-Pehrson | ea3bcd7 | 1998-03-07 14:33:00 -0600 | [diff] [blame] | 3088 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3089 | png_ptr->zstream.avail_out = 0; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3090 | } |
| 3091 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3092 | if (png_ptr->idat_size || png_ptr->zstream.avail_in) |
Glenn Randers-Pehrson | 859665d | 2002-08-06 18:06:11 -0500 | [diff] [blame] | 3093 | png_warning(png_ptr, "Extra compression data"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3094 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3095 | inflateReset(&png_ptr->zstream); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3096 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3097 | png_ptr->mode |= PNG_AFTER_IDAT; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3098 | } |
| 3099 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 3100 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 3101 | png_read_start_row(png_structp png_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3102 | { |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 3103 | #ifdef PNG_USE_LOCAL_ARRAYS |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 3104 | #ifdef PNG_READ_INTERLACING_SUPPORTED |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 3105 | /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 3106 | |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 3107 | /* start of interlace block */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 3108 | PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 3109 | |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 3110 | /* offset to next interlace block */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 3111 | PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 3112 | |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 3113 | /* start of interlace block in the y direction */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 3114 | PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 3115 | |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 3116 | /* offset to next interlace block in the y direction */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 3117 | PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; |
| 3118 | #endif |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 3119 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 3120 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3121 | int max_pixel_depth; |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 3122 | png_size_t row_bytes; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3123 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 3124 | png_debug(1, "in png_read_start_row"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3125 | png_ptr->zstream.avail_in = 0; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3126 | png_init_read_transformations(png_ptr); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 3127 | #ifdef PNG_READ_INTERLACING_SUPPORTED |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3128 | if (png_ptr->interlaced) |
| 3129 | { |
| 3130 | if (!(png_ptr->transformations & PNG_INTERLACE)) |
| 3131 | png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 - |
| 3132 | png_pass_ystart[0]) / png_pass_yinc[0]; |
| 3133 | else |
| 3134 | png_ptr->num_rows = png_ptr->height; |
| 3135 | |
| 3136 | png_ptr->iwidth = (png_ptr->width + |
| 3137 | png_pass_inc[png_ptr->pass] - 1 - |
| 3138 | png_pass_start[png_ptr->pass]) / |
| 3139 | png_pass_inc[png_ptr->pass]; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 3140 | |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 3141 | png_ptr->irowbytes = |
| 3142 | PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3143 | } |
| 3144 | else |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 3145 | #endif /* PNG_READ_INTERLACING_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3146 | { |
| 3147 | png_ptr->num_rows = png_ptr->height; |
| 3148 | png_ptr->iwidth = png_ptr->width; |
| 3149 | png_ptr->irowbytes = png_ptr->rowbytes + 1; |
| 3150 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3151 | max_pixel_depth = png_ptr->pixel_depth; |
| 3152 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3153 | #if defined(PNG_READ_PACK_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3154 | if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3155 | max_pixel_depth = 8; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3156 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3157 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 3158 | #if defined(PNG_READ_EXPAND_SUPPORTED) |
| 3159 | if (png_ptr->transformations & PNG_EXPAND) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3160 | { |
| 3161 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 3162 | { |
| 3163 | if (png_ptr->num_trans) |
| 3164 | max_pixel_depth = 32; |
| 3165 | else |
| 3166 | max_pixel_depth = 24; |
| 3167 | } |
| 3168 | else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) |
| 3169 | { |
| 3170 | if (max_pixel_depth < 8) |
| 3171 | max_pixel_depth = 8; |
| 3172 | if (png_ptr->num_trans) |
| 3173 | max_pixel_depth *= 2; |
| 3174 | } |
| 3175 | else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB) |
| 3176 | { |
| 3177 | if (png_ptr->num_trans) |
| 3178 | { |
| 3179 | max_pixel_depth *= 4; |
| 3180 | max_pixel_depth /= 3; |
| 3181 | } |
| 3182 | } |
| 3183 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3184 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3185 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3186 | #if defined(PNG_READ_FILLER_SUPPORTED) |
| 3187 | if (png_ptr->transformations & (PNG_FILLER)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3188 | { |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 3189 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 3190 | max_pixel_depth = 32; |
| 3191 | else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3192 | { |
| 3193 | if (max_pixel_depth <= 8) |
| 3194 | max_pixel_depth = 16; |
| 3195 | else |
| 3196 | max_pixel_depth = 32; |
| 3197 | } |
| 3198 | else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB) |
| 3199 | { |
| 3200 | if (max_pixel_depth <= 32) |
| 3201 | max_pixel_depth = 32; |
| 3202 | else |
| 3203 | max_pixel_depth = 64; |
| 3204 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3205 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3206 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3207 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3208 | #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3209 | if (png_ptr->transformations & PNG_GRAY_TO_RGB) |
| 3210 | { |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 3211 | if ( |
| 3212 | #if defined(PNG_READ_EXPAND_SUPPORTED) |
| 3213 | (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) || |
| 3214 | #endif |
| 3215 | #if defined(PNG_READ_FILLER_SUPPORTED) |
| 3216 | (png_ptr->transformations & (PNG_FILLER)) || |
| 3217 | #endif |
| 3218 | png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3219 | { |
| 3220 | if (max_pixel_depth <= 16) |
| 3221 | max_pixel_depth = 32; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 3222 | else |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3223 | max_pixel_depth = 64; |
| 3224 | } |
| 3225 | else |
| 3226 | { |
| 3227 | if (max_pixel_depth <= 8) |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 3228 | { |
| 3229 | if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
| 3230 | max_pixel_depth = 32; |
| 3231 | else |
| 3232 | max_pixel_depth = 24; |
| 3233 | } |
| 3234 | else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
| 3235 | max_pixel_depth = 64; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 3236 | else |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3237 | max_pixel_depth = 48; |
| 3238 | } |
| 3239 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3240 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3241 | |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 3242 | #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \ |
| 3243 | defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 3244 | if (png_ptr->transformations & PNG_USER_TRANSFORM) |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 3245 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 3246 | int user_pixel_depth = png_ptr->user_transform_depth* |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 3247 | png_ptr->user_transform_channels; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 3248 | if (user_pixel_depth > max_pixel_depth) |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 3249 | max_pixel_depth=user_pixel_depth; |
| 3250 | } |
| 3251 | #endif |
| 3252 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3253 | /* align the width on the next larger 8 pixels. Mainly used |
| 3254 | for interlacing */ |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 3255 | row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7)); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3256 | /* calculate the maximum bytes needed, adding a byte and a pixel |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3257 | for safety's sake */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 3258 | row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) + |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3259 | 1 + ((max_pixel_depth + 7) >> 3); |
| 3260 | #ifdef PNG_MAX_MALLOC_64K |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 3261 | if (row_bytes > (png_uint_32)65536L) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 3262 | png_error(png_ptr, "This image requires a row greater than 64KB"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3263 | #endif |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 3264 | |
| 3265 | if (row_bytes + 64 > png_ptr->old_big_row_buf_size) |
| 3266 | { |
| 3267 | png_free(png_ptr, png_ptr->big_row_buf); |
Glenn Randers-Pehrson | 6917b51 | 2009-03-09 15:31:08 -0500 | [diff] [blame] | 3268 | if (png_ptr->interlaced) |
Glenn Randers-Pehrson | 8fb550c | 2009-03-21 08:15:32 -0500 | [diff] [blame] | 3269 | png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr, row_bytes + 64); |
Glenn Randers-Pehrson | 6917b51 | 2009-03-09 15:31:08 -0500 | [diff] [blame] | 3270 | else |
Glenn Randers-Pehrson | 8fb550c | 2009-03-21 08:15:32 -0500 | [diff] [blame] | 3271 | png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes + 64); |
| 3272 | png_ptr->row_buf = png_ptr->big_row_buf + 32; |
| 3273 | png_ptr->old_big_row_buf_size = row_bytes + 64; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 3274 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3275 | |
| 3276 | #ifdef PNG_MAX_MALLOC_64K |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 3277 | if ((png_uint_32)png_ptr->rowbytes + 1 > (png_uint_32)65536L) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 3278 | png_error(png_ptr, "This image requires a row greater than 64KB"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3279 | #endif |
Glenn Randers-Pehrson | ebd7f9a | 2006-10-19 09:37:56 -0500 | [diff] [blame] | 3280 | if ((png_uint_32)png_ptr->rowbytes > (png_uint_32)(PNG_SIZE_MAX - 1)) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 3281 | png_error(png_ptr, "Row has too many bytes to allocate in memory"); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 3282 | |
| 3283 | if (png_ptr->rowbytes+1 > png_ptr->old_prev_row_size) |
| 3284 | { |
| 3285 | png_free(png_ptr, png_ptr->prev_row); |
| 3286 | png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)( |
| 3287 | png_ptr->rowbytes + 1)); |
| 3288 | png_ptr->old_prev_row_size = png_ptr->rowbytes+1; |
| 3289 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3290 | |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 3291 | png_memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3292 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 3293 | png_debug1(3, "width = %lu,", png_ptr->width); |
| 3294 | png_debug1(3, "height = %lu,", png_ptr->height); |
| 3295 | png_debug1(3, "iwidth = %lu,", png_ptr->iwidth); |
| 3296 | png_debug1(3, "num_rows = %lu,", png_ptr->num_rows); |
| 3297 | png_debug1(3, "rowbytes = %lu,", png_ptr->rowbytes); |
Glenn Randers-Pehrson | 79134c6 | 2009-02-14 10:32:18 -0600 | [diff] [blame] | 3298 | png_debug1(3, "irowbytes = %lu", png_ptr->irowbytes); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3299 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 3300 | png_ptr->flags |= PNG_FLAG_ROW_INIT; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3301 | } |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 3302 | #endif /* PNG_READ_SUPPORTED */ |