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 | 67ee8ce | 2011-12-22 08:21:00 -0600 | [diff] [blame] | 4 | * Last changed in libpng 1.6.0 [(PENDING RELEASE)] |
Glenn Randers-Pehrson | 1531bd6 | 2012-01-01 14:45:04 -0600 | [diff] [blame] | 5 | * Copyright (c) 1998-2012 Glenn Randers-Pehrson |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 6 | * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) |
| 7 | * (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] | 8 | * |
Glenn Randers-Pehrson | bfbf865 | 2009-06-26 21:46:52 -0500 | [diff] [blame] | 9 | * This code is released under the libpng license. |
Glenn Randers-Pehrson | c332bbc | 2009-06-25 13:43:50 -0500 | [diff] [blame] | 10 | * For conditions of distribution and use, see the disclaimer |
Glenn Randers-Pehrson | 037023b | 2009-06-24 10:27:36 -0500 | [diff] [blame] | 11 | * and license in png.h |
Glenn Randers-Pehrson | 3e61d79 | 2009-06-24 09:31:28 -0500 | [diff] [blame] | 12 | * |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 13 | * This file contains routines that are only called from within |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 14 | * libpng itself during the course of reading an image. |
| 15 | */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 16 | |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 17 | #include "pngpriv.h" |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 18 | |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 19 | #ifdef PNG_READ_SUPPORTED |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 20 | |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 21 | #define png_strtod(p,a,b) strtod(a,b) |
| 22 | |
| 23 | png_uint_32 PNGAPI |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 24 | png_get_uint_31(png_const_structrp png_ptr, png_const_bytep buf) |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 25 | { |
| 26 | png_uint_32 uval = png_get_uint_32(buf); |
| 27 | |
| 28 | if (uval > PNG_UINT_31_MAX) |
| 29 | png_error(png_ptr, "PNG unsigned integer out of range"); |
| 30 | |
| 31 | return (uval); |
| 32 | } |
| 33 | |
| 34 | #if defined(PNG_READ_gAMA_SUPPORTED) || defined(PNG_READ_cHRM_SUPPORTED) |
| 35 | /* The following is a variation on the above for use with the fixed |
| 36 | * point values used for gAMA and cHRM. Instead of png_error it |
| 37 | * issues a warning and returns (-1) - an invalid value because both |
| 38 | * gAMA and cHRM use *unsigned* integers for fixed point values. |
| 39 | */ |
| 40 | #define PNG_FIXED_ERROR (-1) |
| 41 | |
| 42 | static png_fixed_point /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 43 | png_get_fixed_point(png_structrp png_ptr, png_const_bytep buf) |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 44 | { |
| 45 | png_uint_32 uval = png_get_uint_32(buf); |
| 46 | |
| 47 | if (uval <= PNG_UINT_31_MAX) |
| 48 | return (png_fixed_point)uval; /* known to be in range */ |
| 49 | |
| 50 | /* The caller can turn off the warning by passing NULL. */ |
| 51 | if (png_ptr != NULL) |
| 52 | png_warning(png_ptr, "PNG fixed point integer out of range"); |
| 53 | |
| 54 | return PNG_FIXED_ERROR; |
| 55 | } |
| 56 | #endif |
| 57 | |
| 58 | #ifdef PNG_READ_INT_FUNCTIONS_SUPPORTED |
| 59 | /* NOTE: the read macros will obscure these definitions, so that if |
| 60 | * PNG_USE_READ_MACROS is set the library will not use them internally, |
| 61 | * but the APIs will still be available externally. |
| 62 | * |
| 63 | * The parentheses around "PNGAPI function_name" in the following three |
| 64 | * functions are necessary because they allow the macros to co-exist with |
| 65 | * these (unused but exported) functions. |
| 66 | */ |
| 67 | |
| 68 | /* Grab an unsigned 32-bit integer from a buffer in big-endian format. */ |
| 69 | png_uint_32 (PNGAPI |
| 70 | png_get_uint_32)(png_const_bytep buf) |
| 71 | { |
| 72 | png_uint_32 uval = |
| 73 | ((png_uint_32)(*(buf )) << 24) + |
| 74 | ((png_uint_32)(*(buf + 1)) << 16) + |
| 75 | ((png_uint_32)(*(buf + 2)) << 8) + |
| 76 | ((png_uint_32)(*(buf + 3)) ) ; |
| 77 | |
| 78 | return uval; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 79 | } |
| 80 | |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 81 | /* Grab a signed 32-bit integer from a buffer in big-endian format. The |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 82 | * data is stored in the PNG file in two's complement format and there |
| 83 | * is no guarantee that a 'png_int_32' is exactly 32 bits, therefore |
| 84 | * the following code does a two's complement to native conversion. |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 85 | */ |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 86 | png_int_32 (PNGAPI |
| 87 | png_get_int_32)(png_const_bytep buf) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 88 | { |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 89 | png_uint_32 uval = png_get_uint_32(buf); |
John Bowler | f3f7e14 | 2011-09-09 07:32:37 -0500 | [diff] [blame] | 90 | if ((uval & 0x80000000) == 0) /* non-negative */ |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 91 | return uval; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 92 | |
John Bowler | f3f7e14 | 2011-09-09 07:32:37 -0500 | [diff] [blame] | 93 | uval = (uval ^ 0xffffffff) + 1; /* 2's complement: -x = ~x+1 */ |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 94 | return -(png_int_32)uval; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 95 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 96 | |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 97 | /* Grab an unsigned 16-bit integer from a buffer in big-endian format. */ |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 98 | png_uint_16 (PNGAPI |
| 99 | png_get_uint_16)(png_const_bytep buf) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 100 | { |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 101 | /* ANSI-C requires an int value to accomodate at least 16 bits so this |
| 102 | * works and allows the compiler not to worry about possible narrowing |
| 103 | * on 32 bit systems. (Pre-ANSI systems did not make integers smaller |
| 104 | * than 16 bits either.) |
| 105 | */ |
| 106 | unsigned int val = |
| 107 | ((unsigned int)(*buf) << 8) + |
| 108 | ((unsigned int)(*(buf + 1))); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 109 | |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 110 | return (png_uint_16)val; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 111 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 112 | |
| 113 | #endif /* PNG_READ_INT_FUNCTIONS_SUPPORTED */ |
| 114 | |
| 115 | /* Read and check the PNG file signature */ |
| 116 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 117 | png_read_sig(png_structrp png_ptr, png_inforp info_ptr) |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 118 | { |
| 119 | png_size_t num_checked, num_to_check; |
| 120 | |
| 121 | /* Exit if the user application does not expect a signature. */ |
| 122 | if (png_ptr->sig_bytes >= 8) |
| 123 | return; |
| 124 | |
| 125 | num_checked = png_ptr->sig_bytes; |
| 126 | num_to_check = 8 - num_checked; |
| 127 | |
| 128 | #ifdef PNG_IO_STATE_SUPPORTED |
| 129 | png_ptr->io_state = PNG_IO_READING | PNG_IO_SIGNATURE; |
| 130 | #endif |
| 131 | |
| 132 | /* The signature must be serialized in a single I/O call. */ |
| 133 | png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check); |
| 134 | png_ptr->sig_bytes = 8; |
| 135 | |
| 136 | if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check)) |
| 137 | { |
| 138 | if (num_checked < 4 && |
| 139 | png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4)) |
| 140 | png_error(png_ptr, "Not a PNG file"); |
| 141 | else |
| 142 | png_error(png_ptr, "PNG file corrupted by ASCII conversion"); |
| 143 | } |
| 144 | if (num_checked < 3) |
| 145 | png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE; |
| 146 | } |
Glenn Randers-Pehrson | a581556 | 2010-11-20 21:48:29 -0600 | [diff] [blame] | 147 | |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 148 | /* Read the chunk header (length + type name). |
| 149 | * Put the type name into png_ptr->chunk_name, and return the length. |
| 150 | */ |
| 151 | png_uint_32 /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 152 | png_read_chunk_header(png_structrp png_ptr) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 153 | { |
| 154 | png_byte buf[8]; |
| 155 | png_uint_32 length; |
| 156 | |
| 157 | #ifdef PNG_IO_STATE_SUPPORTED |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 158 | png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_HDR; |
| 159 | #endif |
| 160 | |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 161 | /* Read the length and the chunk name. |
| 162 | * This must be performed in a single I/O call. |
| 163 | */ |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 164 | png_read_data(png_ptr, buf, 8); |
| 165 | length = png_get_uint_31(png_ptr, buf); |
| 166 | |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 167 | /* Put the chunk name into png_ptr->chunk_name. */ |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 168 | png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(buf+4); |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 169 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 170 | png_debug2(0, "Reading %lx chunk, length = %lu", |
| 171 | (unsigned long)png_ptr->chunk_name, (unsigned long)length); |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 172 | |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 173 | /* Reset the crc and run it over the chunk name. */ |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 174 | png_reset_crc(png_ptr); |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 175 | png_calculate_crc(png_ptr, buf + 4, 4); |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 176 | |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 177 | /* Check to see if chunk name is valid. */ |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 178 | png_check_chunk_name(png_ptr, png_ptr->chunk_name); |
| 179 | |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 180 | #ifdef PNG_IO_STATE_SUPPORTED |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 181 | png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_DATA; |
| 182 | #endif |
| 183 | |
| 184 | return length; |
| 185 | } |
| 186 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 187 | /* Read data, and (optionally) run it through the CRC. */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 188 | void /* PRIVATE */ |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 189 | png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 190 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 191 | if (png_ptr == NULL) |
| 192 | return; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 193 | |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 194 | png_read_data(png_ptr, buf, length); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 195 | png_calculate_crc(png_ptr, buf, length); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 196 | } |
| 197 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 198 | /* 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] | 199 | * are reading a ancillary or critical chunk, and how the program has set |
| 200 | * things up, we may calculate the CRC on the data and print a message. |
| 201 | * Returns '1' if there was a CRC error, '0' otherwise. |
| 202 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 203 | int /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 204 | png_crc_finish(png_structrp png_ptr, png_uint_32 skip) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 205 | { |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 206 | /* The size of the local buffer for inflate is a good guess as to a |
| 207 | * reasonable size to use for buffering reads from the application. |
| 208 | */ |
| 209 | while (skip > 0) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 210 | { |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 211 | png_uint_32 len; |
| 212 | png_byte tmpbuf[PNG_INFLATE_BUF_SIZE]; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 213 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 214 | len = sizeof tmpbuf; |
| 215 | if (len > skip) |
| 216 | len = skip; |
| 217 | skip -= len; |
| 218 | |
| 219 | png_crc_read(png_ptr, tmpbuf, len); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 220 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 221 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 222 | if (png_crc_error(png_ptr)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 223 | { |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 224 | if (PNG_CHUNK_ANCILLIARY(png_ptr->chunk_name) ? |
| 225 | !(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) : |
| 226 | (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 227 | { |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 228 | png_chunk_warning(png_ptr, "CRC error"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 229 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 230 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 231 | else |
| 232 | { |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 233 | png_chunk_benign_error(png_ptr, "CRC error"); |
| 234 | return (0); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 235 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 236 | |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 237 | return (1); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 238 | } |
| 239 | |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 240 | return (0); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 241 | } |
| 242 | |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 243 | /* 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] | 244 | * the data it has read thus far. |
| 245 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 246 | int /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 247 | png_crc_error(png_structrp png_ptr) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 248 | { |
| 249 | png_byte crc_bytes[4]; |
| 250 | png_uint_32 crc; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 251 | int need_crc = 1; |
| 252 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 253 | if (PNG_CHUNK_ANCILLIARY(png_ptr->chunk_name)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 254 | { |
| 255 | if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) == |
| 256 | (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN)) |
| 257 | need_crc = 0; |
| 258 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 259 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 260 | else /* critical */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 261 | { |
| 262 | if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) |
| 263 | need_crc = 0; |
| 264 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 265 | |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 266 | #ifdef PNG_IO_STATE_SUPPORTED |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 267 | png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_CRC; |
| 268 | #endif |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 269 | |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 270 | /* The chunk CRC must be serialized in a single I/O call. */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 271 | png_read_data(png_ptr, crc_bytes, 4); |
| 272 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 273 | if (need_crc) |
| 274 | { |
| 275 | crc = png_get_uint_32(crc_bytes); |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 276 | return ((int)(crc != png_ptr->crc)); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 277 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 278 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 279 | else |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 280 | return (0); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 281 | } |
| 282 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 283 | /* Manage the read buffer; this simply reallocates the buffer if it is not small |
| 284 | * enough (or if it is not allocated). The routine returns a pointer to the |
| 285 | * buffer, if an error occurs and 'warn' is set the routine returns NULL, else |
| 286 | * it will call png_error (via png_malloc) on failure. (warn == 2 means |
| 287 | * 'silent'). |
John Bowler | 42a2b55 | 2012-03-05 20:57:40 -0600 | [diff] [blame] | 288 | */ |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 289 | static png_bytep |
| 290 | png_read_buffer(png_structrp png_ptr, png_alloc_size_t new_size, int warn) |
John Bowler | 42a2b55 | 2012-03-05 20:57:40 -0600 | [diff] [blame] | 291 | { |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 292 | png_bytep buffer = png_ptr->read_buffer; |
| 293 | |
| 294 | if (buffer != NULL && new_size > png_ptr->read_buffer_size) |
| 295 | { |
| 296 | png_ptr->read_buffer = NULL; |
| 297 | png_ptr->read_buffer = NULL; |
| 298 | png_ptr->read_buffer_size = 0; |
| 299 | png_free(png_ptr, buffer); |
| 300 | buffer = NULL; |
| 301 | } |
| 302 | |
| 303 | if (buffer == NULL) |
| 304 | { |
| 305 | buffer = png_voidcast(png_bytep, png_malloc_base(png_ptr, new_size)); |
| 306 | |
| 307 | if (buffer != NULL) |
| 308 | { |
| 309 | png_ptr->read_buffer = buffer; |
| 310 | png_ptr->read_buffer_size = new_size; |
| 311 | } |
| 312 | |
| 313 | else if (warn < 2) /* else silent */ |
| 314 | { |
| 315 | (warn ? png_chunk_warning : png_chunk_error)(png_ptr, |
| 316 | "insufficient memory to read chunk"); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | return buffer; |
| 321 | } |
| 322 | |
| 323 | /* png_inflate_claim: claim the zstream for some nefarious purpose that involves |
| 324 | * decompression. Returns Z_OK on success, else a zlib error code. It checks |
| 325 | * the owner but, in final release builds, just issues a warning if some other |
| 326 | * chunk apparently owns the stream. Prior to release it does a png_error. |
| 327 | */ |
| 328 | static int |
John Bowler | 9066919 | 2012-03-09 22:03:13 -0600 | [diff] [blame] | 329 | png_inflate_claim(png_structrp png_ptr, png_uint_32 owner, int window_bits) |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 330 | { |
| 331 | if (png_ptr->zowner != 0) |
| 332 | { |
| 333 | char msg[64]; |
| 334 | |
| 335 | PNG_STRING_FROM_CHUNK(msg, png_ptr->zowner); |
| 336 | /* So the message that results is "<chunk> using zstream"; this is an |
| 337 | * internal error, but is very useful for debugging. i18n requirements |
| 338 | * are minimal. |
| 339 | */ |
| 340 | (void)png_safecat(msg, sizeof msg, 4, " using zstream"); |
| 341 | # if PNG_LIBPNG_BUILD_BASE_TYPE == PNG_LIBPNG_BUILD_STABLE |
| 342 | png_chunk_warning(png_ptr, msg); |
| 343 | png_ptr->zowner = 0; |
| 344 | # else |
| 345 | png_chunk_error(png_ptr, msg); |
| 346 | # endif |
| 347 | } |
| 348 | |
John Bowler | 42a2b55 | 2012-03-05 20:57:40 -0600 | [diff] [blame] | 349 | /* Implementation note: unlike 'png_deflate_claim' this internal function |
| 350 | * does not take the size of the data as an argument. Some efficiency could |
| 351 | * be gained by using this when it is known *if* the zlib stream itself does |
| 352 | * not record the number, however this is a chimera: the original writer of |
| 353 | * the PNG may have selected a lower window size, and we really must follow |
| 354 | * that because, for systems with with limited capabilities, we would |
| 355 | * otherwise reject the applications attempts to use a smaller window size. |
| 356 | * (zlib doesn't have an interface to say "this or lower"!) |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 357 | * |
| 358 | * inflateReset2 was added to zlib 1.2.4; before this the window could not be |
| 359 | * reset, therefore it is necessary to always allocate the maximum window |
| 360 | * size with earlier zlibs just in case later compressed chunks need it. |
John Bowler | 42a2b55 | 2012-03-05 20:57:40 -0600 | [diff] [blame] | 361 | */ |
John Bowler | 42a2b55 | 2012-03-05 20:57:40 -0600 | [diff] [blame] | 362 | { |
| 363 | int ret; /* zlib return code */ |
| 364 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 365 | /* Set this for safety, just in case the previous owner left pointers to |
| 366 | * memory allocations. |
| 367 | */ |
| 368 | png_ptr->zstream.next_in = NULL; |
| 369 | png_ptr->zstream.avail_in = 0; |
| 370 | png_ptr->zstream.next_out = NULL; |
| 371 | png_ptr->zstream.avail_out = 0; |
John Bowler | 42a2b55 | 2012-03-05 20:57:40 -0600 | [diff] [blame] | 372 | |
| 373 | if (png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 374 | { |
| 375 | # if ZLIB_VERNUM < 0x1240 |
John Bowler | 9066919 | 2012-03-09 22:03:13 -0600 | [diff] [blame] | 376 | PNG_UNUSED(window_bits) |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 377 | ret = inflateReset(&png_ptr->zstream); |
| 378 | # else |
John Bowler | 9066919 | 2012-03-09 22:03:13 -0600 | [diff] [blame] | 379 | ret = inflateReset2(&png_ptr->zstream, window_bits); |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 380 | # endif |
| 381 | } |
John Bowler | 42a2b55 | 2012-03-05 20:57:40 -0600 | [diff] [blame] | 382 | |
| 383 | else |
| 384 | { |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 385 | # if ZLIB_VERNUM < 0x1240 |
| 386 | ret = inflateInit(&png_ptr->zstream); |
| 387 | # else |
John Bowler | 9066919 | 2012-03-09 22:03:13 -0600 | [diff] [blame] | 388 | ret = inflateInit2(&png_ptr->zstream, window_bits); |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 389 | # endif |
John Bowler | 42a2b55 | 2012-03-05 20:57:40 -0600 | [diff] [blame] | 390 | |
| 391 | if (ret == Z_OK) |
| 392 | png_ptr->flags |= PNG_FLAG_ZSTREAM_INITIALIZED; |
| 393 | } |
| 394 | |
| 395 | if (ret == Z_OK) |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 396 | png_ptr->zowner = owner; |
John Bowler | 42a2b55 | 2012-03-05 20:57:40 -0600 | [diff] [blame] | 397 | |
| 398 | else |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 399 | png_zstream_error(png_ptr, ret); |
John Bowler | 42a2b55 | 2012-03-05 20:57:40 -0600 | [diff] [blame] | 400 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 401 | return ret; |
| 402 | } |
| 403 | } |
| 404 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 405 | #ifdef PNG_READ_COMPRESSED_TEXT_SUPPORTED |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 406 | /* png_inflate now returns zlib error codes including Z_OK and Z_STREAM_END to |
| 407 | * allow the caller to do multiple calls if required. If the 'finish' flag is |
| 408 | * set Z_FINISH will be passed to the final inflate() call and Z_STREAM_END must |
| 409 | * be returned or there has been a problem, otherwise Z_SYNC_FLUSH is used and |
| 410 | * Z_OK or Z_STREAM_END will be returned on success. |
| 411 | * |
| 412 | * The input and output sizes are updated to the actual amounts of data consumed |
| 413 | * or written, not the amount available (as in a z_stream). The data pointers |
| 414 | * are not changed, so the next input is (data+input_size) and the next |
| 415 | * available output is (output+output_size). |
| 416 | */ |
| 417 | static int |
| 418 | png_inflate(png_structrp png_ptr, png_uint_32 owner, int finish, |
| 419 | /* INPUT: */ png_const_bytep input, png_uint_32p input_size_ptr, |
| 420 | /* OUTPUT: */ png_bytep output, png_alloc_size_t *output_size_ptr) |
| 421 | { |
| 422 | if (png_ptr->zowner == owner) /* Else not claimed */ |
| 423 | { |
| 424 | int ret; |
| 425 | png_alloc_size_t avail_out = *output_size_ptr; |
| 426 | png_uint_32 avail_in = *input_size_ptr; |
| 427 | |
| 428 | /* zlib can't necessarily handle more than 65535 bytes at once (i.e. it |
| 429 | * can't even necessarily handle 65536 bytes) because the type uInt is |
| 430 | * "16 bits or more". Consequently it is necessary to chunk the input to |
| 431 | * zlib. This code uses ZLIB_IO_MAX, from pngpriv.h, as the maximum (the |
| 432 | * maximum value that can be stored in a uInt.) It is possible to set |
| 433 | * ZLIB_IO_MAX to a lower value in pngpriv.h and this may sometimes have |
| 434 | * a performance advantage, because it reduces the amount of data accessed |
| 435 | * at each step and that may give the OS more time to page it in. |
| 436 | */ |
| 437 | png_ptr->zstream.next_in = PNGZ_INPUT_CAST(input); |
| 438 | /* avail_in and avail_out are set below from 'size' */ |
| 439 | png_ptr->zstream.avail_in = 0; |
| 440 | png_ptr->zstream.avail_out = 0; |
| 441 | |
| 442 | /* Read directly into the output if it is available (this is set to |
| 443 | * a local buffer below if output is NULL). |
| 444 | */ |
| 445 | if (output != NULL) |
| 446 | png_ptr->zstream.next_out = output; |
| 447 | |
| 448 | do |
| 449 | { |
| 450 | uInt avail; |
| 451 | Byte local_buffer[PNG_INFLATE_BUF_SIZE]; |
| 452 | |
| 453 | /* zlib INPUT BUFFER */ |
| 454 | /* The setting of 'avail_in' used to be outside the loop; by setting it |
| 455 | * inside it is possible to chunk the input to zlib and simply rely on |
| 456 | * zlib to advance the 'next_in' pointer. This allows arbitrary |
| 457 | * amounts of data to be passed through zlib at the unavoidable cost of |
| 458 | * requiring a window save (png_memcpy of up to 32768 output bytes) |
| 459 | * every ZLIB_IO_MAX input bytes. |
| 460 | */ |
| 461 | avail_in += png_ptr->zstream.avail_in; /* not consumed last time */ |
| 462 | |
| 463 | avail = ZLIB_IO_MAX; |
| 464 | |
| 465 | if (avail_in < avail) |
| 466 | avail = (uInt)avail_in; /* safe: < than ZLIB_IO_MAX */ |
| 467 | |
| 468 | avail_in -= avail; |
| 469 | png_ptr->zstream.avail_in = avail; |
| 470 | |
| 471 | /* zlib OUTPUT BUFFER */ |
| 472 | avail_out += png_ptr->zstream.avail_out; /* not written last time */ |
| 473 | |
| 474 | avail = ZLIB_IO_MAX; /* maximum zlib can process */ |
| 475 | |
| 476 | if (output == NULL) |
| 477 | { |
| 478 | /* Reset the output buffer each time round if output is NULL and |
| 479 | * make available the full buffer, up to 'remaining_space' |
| 480 | */ |
| 481 | png_ptr->zstream.next_out = local_buffer; |
| 482 | if (sizeof local_buffer < avail) |
| 483 | avail = sizeof local_buffer; |
| 484 | } |
| 485 | |
| 486 | if (avail_out < avail) |
| 487 | avail = (uInt)avail_out; /* safe: < ZLIB_IO_MAX */ |
| 488 | |
| 489 | png_ptr->zstream.avail_out = avail; |
| 490 | avail_out -= avail; |
| 491 | |
| 492 | /* zlib inflate call */ |
| 493 | /* In fact 'avail_out' may be 0 at this point, that happens at the end |
| 494 | * of the read when the final LZ end code was not passed at the end of |
| 495 | * the previous chunk of input data. Tell zlib if we have reached the |
| 496 | * end of the output buffer. |
| 497 | */ |
| 498 | ret = inflate(&png_ptr->zstream, avail_out > 0 ? Z_NO_FLUSH : |
| 499 | (finish ? Z_FINISH : Z_SYNC_FLUSH)); |
| 500 | } while (ret == Z_OK); |
| 501 | |
| 502 | /* For safety kill the local buffer pointer now */ |
| 503 | if (output == NULL) |
| 504 | png_ptr->zstream.next_out = NULL; |
| 505 | |
| 506 | /* Claw back the 'size' and 'remaining_space' byte counts. */ |
| 507 | avail_in += png_ptr->zstream.avail_in; |
| 508 | avail_out += png_ptr->zstream.avail_out; |
| 509 | |
| 510 | /* Update the input and output sizes; the updated values are the amount |
| 511 | * consumed or written, effectively the inverse of what zlib uses. |
| 512 | */ |
| 513 | if (avail_out > 0) |
| 514 | *output_size_ptr -= avail_out; |
| 515 | |
| 516 | if (avail_in > 0) |
| 517 | *input_size_ptr -= avail_in; |
| 518 | |
| 519 | /* Ensure png_ptr->zstream.msg is set (even in the success case!) */ |
| 520 | png_zstream_error(png_ptr, ret); |
| 521 | return ret; |
John Bowler | 42a2b55 | 2012-03-05 20:57:40 -0600 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | else |
Glenn Randers-Pehrson | e3f3c4e | 2010-02-07 18:08:50 -0600 | [diff] [blame] | 525 | { |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 526 | /* This is a bad internal error. The recovery assigns to the zstream msg |
| 527 | * pointer, which is not owned by the caller, but this is safe; it's only |
| 528 | * used on errors! |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 529 | */ |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 530 | png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed"); |
| 531 | return Z_STREAM_ERROR; |
Glenn Randers-Pehrson | e3f3c4e | 2010-02-07 18:08:50 -0600 | [diff] [blame] | 532 | } |
Glenn Randers-Pehrson | e3f3c4e | 2010-02-07 18:08:50 -0600 | [diff] [blame] | 533 | } |
| 534 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 535 | /* |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 536 | * Decompress trailing data in a chunk. The assumption is that read_buffer |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 537 | * points at an allocated area holding the contents of a chunk with a |
| 538 | * trailing compressed part. What we get back is an allocated area |
| 539 | * holding the original prefix part and an uncompressed version of the |
| 540 | * trailing part (the malloc area passed in is freed). |
| 541 | */ |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 542 | static int |
| 543 | png_decompress_chunk(png_structrp png_ptr, |
| 544 | png_uint_32 chunklength, png_uint_32 prefix_size, |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 545 | png_alloc_size_t *newlength /* must be initialized to the maximum! */, |
| 546 | int terminate /*add a '\0' to the end of the uncompressed data*/) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 547 | { |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 548 | /* TODO: implement different limits for different types of chunk. |
| 549 | * |
| 550 | * The caller supplies *newlength set to the maximum length of the |
| 551 | * uncompressed data, but this routine allocates space for the prefix and |
| 552 | * maybe a '\0' terminator too. We have to assume that 'prefix_size' is |
| 553 | * limited only by the maximum chunk size. |
| 554 | */ |
| 555 | png_alloc_size_t limit = PNG_SIZE_MAX; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 556 | |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 557 | # ifdef PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED |
| 558 | if (png_ptr->user_chunk_malloc_max > 0 && |
| 559 | png_ptr->user_chunk_malloc_max < limit) |
| 560 | limit = png_ptr->user_chunk_malloc_max; |
| 561 | # elif PNG_USER_CHUNK_MALLOC_MAX > 0 |
| 562 | if (PNG_USER_CHUNK_MALLOC_MAX < limit) |
| 563 | limit = PNG_USER_CHUNK_MALLOC_MAX; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 564 | # endif |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 565 | |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 566 | if (limit >= prefix_size + (terminate != 0)) |
| 567 | { |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 568 | int ret; |
| 569 | |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 570 | limit -= prefix_size + (terminate != 0); |
| 571 | |
| 572 | if (limit < *newlength) |
| 573 | *newlength = limit; |
| 574 | |
John Bowler | 9066919 | 2012-03-09 22:03:13 -0600 | [diff] [blame] | 575 | /* Now try to claim the stream; the 'warn' setting causes zlib to be told |
| 576 | * to use the maximum window size during inflate; this hides errors in the |
| 577 | * deflate header window bits value which is used if '0' is passed. In |
| 578 | * fact this only has an effect with zlib versions 1.2.4 and later - see |
| 579 | * the comments in png_inflate_claim above. |
| 580 | */ |
| 581 | ret = png_inflate_claim(png_ptr, png_ptr->chunk_name, |
| 582 | png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN ? 15 : 0); |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 583 | |
| 584 | if (ret == Z_OK) |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 585 | { |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 586 | png_uint_32 lzsize = chunklength - prefix_size; |
| 587 | |
| 588 | ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/, |
| 589 | /* input: */ png_ptr->read_buffer + prefix_size, &lzsize, |
| 590 | /* output: */ NULL, newlength); |
| 591 | |
| 592 | if (ret == Z_STREAM_END) |
| 593 | { |
John Bowler | 9066919 | 2012-03-09 22:03:13 -0600 | [diff] [blame] | 594 | /* Use 'inflateReset' here, not 'inflateReset2' because this |
| 595 | * preserves the previously decided window size (otherwise it would |
| 596 | * be necessary to store the previous window size.) In practice |
| 597 | * this doesn't matter anyway, because png_inflate will call inflate |
| 598 | * with Z_FINISH in almost all cases, so the window will not be |
| 599 | * maintained. |
| 600 | */ |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 601 | if (inflateReset(&png_ptr->zstream) == Z_OK) |
Glenn Randers-Pehrson | 20788d3 | 2011-01-06 09:01:04 -0600 | [diff] [blame] | 602 | { |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 603 | /* Because of the limit checks above we know that the new, |
| 604 | * expanded, size will fit in a size_t (let alone an |
| 605 | * png_alloc_size_t). Use png_malloc_base here to avoid an |
| 606 | * extra OOM message. |
| 607 | */ |
| 608 | png_alloc_size_t new_size = *newlength; |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 609 | png_alloc_size_t buffer_size = prefix_size + new_size + |
| 610 | (terminate != 0); |
| 611 | png_bytep text = png_voidcast(png_bytep, png_malloc_base(png_ptr, |
| 612 | buffer_size)); |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 613 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 614 | if (text != NULL) |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 615 | { |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 616 | ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/, |
| 617 | png_ptr->read_buffer + prefix_size, &lzsize, |
| 618 | text + prefix_size, newlength); |
| 619 | |
| 620 | if (ret == Z_STREAM_END) |
| 621 | { |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 622 | if (new_size == *newlength) |
| 623 | { |
| 624 | if (terminate) |
| 625 | text[prefix_size + *newlength] = 0; |
| 626 | |
| 627 | if (prefix_size > 0) |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 628 | png_memcpy(text, png_ptr->read_buffer, prefix_size); |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 629 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 630 | { |
| 631 | png_bytep old_ptr = png_ptr->read_buffer; |
| 632 | |
| 633 | png_ptr->read_buffer = text; |
| 634 | png_ptr->read_buffer_size = buffer_size; |
| 635 | text = old_ptr; /* freed below */ |
| 636 | } |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 637 | } |
| 638 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 639 | else |
| 640 | { |
| 641 | /* The size changed on the second read, there can be no |
| 642 | * guarantee that anything is correct at this point. |
| 643 | * The 'msg' pointer has been set to "unexpected end of |
| 644 | * LZ stream", which is fine, but return an error code |
| 645 | * that the caller won't accept. |
| 646 | */ |
| 647 | ret = PNG_UNEXPECTED_ZLIB_RETURN; |
| 648 | } |
| 649 | } |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 650 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 651 | else if (ret == Z_OK) |
| 652 | ret = PNG_UNEXPECTED_ZLIB_RETURN; /* for safety */ |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 653 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 654 | /* Free the text pointer (this is the old read_buffer on |
| 655 | * success) |
| 656 | */ |
| 657 | png_free(png_ptr, text); |
| 658 | |
| 659 | /* This really is very benign, but it's still an error because |
| 660 | * the extra space may otherwise be used as a Trojan Horse. |
| 661 | */ |
| 662 | if (ret == Z_STREAM_END && |
| 663 | chunklength - prefix_size != lzsize) |
| 664 | png_chunk_benign_error(png_ptr, "extra compressed data"); |
| 665 | } |
| 666 | |
| 667 | else |
| 668 | { |
| 669 | /* Out of memory allocating the buffer */ |
| 670 | ret = Z_MEM_ERROR; |
| 671 | png_zstream_error(png_ptr, Z_MEM_ERROR); |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 672 | } |
Glenn Randers-Pehrson | 20788d3 | 2011-01-06 09:01:04 -0600 | [diff] [blame] | 673 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 674 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 675 | else |
| 676 | { |
| 677 | /* inflateReset failed, store the error message */ |
| 678 | png_zstream_error(png_ptr, ret); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 679 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 680 | if (ret == Z_STREAM_END) |
| 681 | ret = PNG_UNEXPECTED_ZLIB_RETURN; |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | else if (ret == Z_OK) |
| 686 | ret = PNG_UNEXPECTED_ZLIB_RETURN; |
| 687 | |
| 688 | /* Release the claimed stream */ |
| 689 | png_ptr->zowner = 0; |
Glenn Randers-Pehrson | 20788d3 | 2011-01-06 09:01:04 -0600 | [diff] [blame] | 690 | } |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 691 | |
| 692 | else /* the claim failed */ if (ret == Z_STREAM_END) /* impossible! */ |
| 693 | ret = PNG_UNEXPECTED_ZLIB_RETURN; |
| 694 | |
| 695 | return ret; |
Glenn Randers-Pehrson | 20788d3 | 2011-01-06 09:01:04 -0600 | [diff] [blame] | 696 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 697 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 698 | else |
| 699 | { |
| 700 | /* Application/configuration limits exceeded */ |
| 701 | png_zstream_error(png_ptr, Z_MEM_ERROR); |
| 702 | return Z_MEM_ERROR; |
| 703 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 704 | } |
Glenn Randers-Pehrson | 5975f31 | 2011-04-01 13:15:36 -0500 | [diff] [blame] | 705 | #endif /* PNG_READ_COMPRESSED_TEXT_SUPPORTED */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 706 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 707 | #ifdef PNG_READ_iCCP_SUPPORTED |
| 708 | /* Perform a partial read and decompress, producing 'avail_out' bytes and |
| 709 | * reading from the current chunk as required. |
| 710 | */ |
| 711 | static int |
| 712 | png_inflate_read(png_structrp png_ptr, png_bytep read_buffer, uInt read_size, |
| 713 | png_uint_32p chunk_bytes, png_bytep next_out, png_alloc_size_t *out_size, |
| 714 | int finish) |
| 715 | { |
| 716 | if (png_ptr->zowner == png_ptr->chunk_name) |
| 717 | { |
| 718 | int ret; |
| 719 | |
| 720 | /* next_in and avail_in must have been initialized by the caller. */ |
| 721 | png_ptr->zstream.next_out = next_out; |
| 722 | png_ptr->zstream.avail_out = 0; /* set in the loop */ |
| 723 | |
| 724 | do |
| 725 | { |
| 726 | if (png_ptr->zstream.avail_in == 0) |
| 727 | { |
| 728 | if (read_size > *chunk_bytes) |
| 729 | read_size = (uInt)*chunk_bytes; |
| 730 | *chunk_bytes -= read_size; |
| 731 | |
| 732 | if (read_size > 0) |
| 733 | png_crc_read(png_ptr, read_buffer, read_size); |
| 734 | |
| 735 | png_ptr->zstream.next_in = read_buffer; |
| 736 | png_ptr->zstream.avail_in = read_size; |
| 737 | } |
| 738 | |
| 739 | if (png_ptr->zstream.avail_out == 0) |
| 740 | { |
| 741 | uInt avail = ZLIB_IO_MAX; |
| 742 | if (avail > *out_size) |
| 743 | avail = (uInt)*out_size; |
| 744 | *out_size -= avail; |
| 745 | |
| 746 | png_ptr->zstream.avail_out = avail; |
| 747 | } |
| 748 | |
| 749 | /* Use Z_SYNC_FLUSH when there is no more chunk data to ensure that all |
| 750 | * the available output is produced; this allows reading of truncated |
| 751 | * streams. |
| 752 | */ |
| 753 | ret = inflate(&png_ptr->zstream, |
| 754 | *chunk_bytes > 0 ? Z_NO_FLUSH : (finish ? Z_FINISH : Z_SYNC_FLUSH)); |
| 755 | } |
| 756 | while (ret == Z_OK && (*out_size > 0 || png_ptr->zstream.avail_out > 0)); |
| 757 | |
| 758 | *out_size += png_ptr->zstream.avail_out; |
| 759 | png_ptr->zstream.avail_out = 0; /* Should not be required, but is safe */ |
| 760 | |
| 761 | /* Ensure the error message pointer is always set: */ |
| 762 | png_zstream_error(png_ptr, ret); |
| 763 | return ret; |
| 764 | } |
| 765 | |
| 766 | else |
| 767 | { |
| 768 | png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed"); |
| 769 | return Z_STREAM_ERROR; |
| 770 | } |
| 771 | } |
| 772 | #endif |
| 773 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 774 | /* Read and check the IDHR chunk */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 775 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 776 | png_handle_IHDR(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 777 | { |
| 778 | png_byte buf[13]; |
| 779 | png_uint_32 width, height; |
| 780 | int bit_depth, color_type, compression_type, filter_type; |
| 781 | int interlace_type; |
| 782 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 783 | png_debug(1, "in png_handle_IHDR"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 784 | |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 785 | if (png_ptr->mode & PNG_HAVE_IHDR) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 786 | png_chunk_error(png_ptr, "out of place"); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 787 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 788 | /* Check the length */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 789 | if (length != 13) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 790 | png_chunk_error(png_ptr, "invalid"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 791 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 792 | png_ptr->mode |= PNG_HAVE_IHDR; |
| 793 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 794 | png_crc_read(png_ptr, buf, 13); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 795 | png_crc_finish(png_ptr, 0); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 796 | |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 797 | width = png_get_uint_31(png_ptr, buf); |
| 798 | height = png_get_uint_31(png_ptr, buf + 4); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 799 | bit_depth = buf[8]; |
| 800 | color_type = buf[9]; |
| 801 | compression_type = buf[10]; |
| 802 | filter_type = buf[11]; |
| 803 | interlace_type = buf[12]; |
| 804 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 805 | /* Set internal variables */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 806 | png_ptr->width = width; |
| 807 | png_ptr->height = height; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 808 | png_ptr->bit_depth = (png_byte)bit_depth; |
| 809 | png_ptr->interlaced = (png_byte)interlace_type; |
| 810 | png_ptr->color_type = (png_byte)color_type; |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 811 | #ifdef PNG_MNG_FEATURES_SUPPORTED |
Glenn Randers-Pehrson | 2ad31ae | 2000-12-15 08:54:42 -0600 | [diff] [blame] | 812 | png_ptr->filter_type = (png_byte)filter_type; |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 813 | #endif |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 814 | png_ptr->compression_type = (png_byte)compression_type; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 815 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 816 | /* Find number of channels */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 817 | switch (png_ptr->color_type) |
| 818 | { |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 819 | default: /* invalid, png_set_IHDR calls png_error */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 820 | case PNG_COLOR_TYPE_GRAY: |
| 821 | case PNG_COLOR_TYPE_PALETTE: |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 822 | png_ptr->channels = 1; |
| 823 | break; |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 824 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 825 | case PNG_COLOR_TYPE_RGB: |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 826 | png_ptr->channels = 3; |
| 827 | break; |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 828 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 829 | case PNG_COLOR_TYPE_GRAY_ALPHA: |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 830 | png_ptr->channels = 2; |
| 831 | break; |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 832 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 833 | case PNG_COLOR_TYPE_RGB_ALPHA: |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 834 | png_ptr->channels = 4; |
| 835 | break; |
| 836 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 837 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 838 | /* Set up other useful info */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 839 | png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth * |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 840 | png_ptr->channels); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 841 | 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] | 842 | png_debug1(3, "bit_depth = %d", png_ptr->bit_depth); |
| 843 | png_debug1(3, "channels = %d", png_ptr->channels); |
Glenn Randers-Pehrson | b764c60 | 2011-01-14 21:18:37 -0600 | [diff] [blame] | 844 | png_debug1(3, "rowbytes = %lu", (unsigned long)png_ptr->rowbytes); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 845 | png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 846 | color_type, interlace_type, compression_type, filter_type); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 847 | } |
| 848 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 849 | /* Read and check the palette */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 850 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 851 | png_handle_PLTE(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 852 | { |
Glenn Randers-Pehrson | 76e5fd6 | 2000-12-28 07:50:05 -0600 | [diff] [blame] | 853 | png_color palette[PNG_MAX_PALETTE_LENGTH]; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 854 | int num, i; |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 855 | #ifdef PNG_POINTER_INDEXING_SUPPORTED |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 856 | png_colorp pal_ptr; |
| 857 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 858 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 859 | png_debug(1, "in png_handle_PLTE"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 860 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 861 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 862 | png_chunk_error(png_ptr, "missing IHDR"); |
| 863 | |
| 864 | /* Moved to before the 'after IDAT' check below because otherwise duplicate |
| 865 | * PLTE chunks are potentially ignored (the spec says there shall not be more |
| 866 | * than one PLTE, the error is not treated as benign, so this check trumps |
| 867 | * the requirement that PLTE appears before IDAT.) |
| 868 | */ |
| 869 | else if (png_ptr->mode & PNG_HAVE_PLTE) |
| 870 | png_chunk_error(png_ptr, "duplicate"); |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 871 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 872 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 873 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 874 | /* This is benign because the non-benign error happened before, when an |
| 875 | * IDAT was encountered in a color-mapped image with no PLTE. |
| 876 | */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 877 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 878 | png_chunk_benign_error(png_ptr, "out of place"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 879 | return; |
| 880 | } |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 881 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 882 | png_ptr->mode |= PNG_HAVE_PLTE; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 883 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 884 | if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 885 | { |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 886 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 887 | png_chunk_benign_error(png_ptr, "ignored in grayscale PNG"); |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 888 | return; |
| 889 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 890 | |
Glenn Randers-Pehrson | b2aca21 | 2009-09-23 11:32:37 -0500 | [diff] [blame] | 891 | #ifndef PNG_READ_OPT_PLTE_SUPPORTED |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 892 | if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE) |
| 893 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 894 | png_crc_finish(png_ptr, length); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 895 | return; |
| 896 | } |
| 897 | #endif |
| 898 | |
Glenn Randers-Pehrson | 76e5fd6 | 2000-12-28 07:50:05 -0600 | [diff] [blame] | 899 | if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 900 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 901 | png_crc_finish(png_ptr, length); |
| 902 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 903 | if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 904 | png_chunk_benign_error(png_ptr, "invalid"); |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 905 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 906 | else |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 907 | png_chunk_error(png_ptr, "invalid"); |
| 908 | |
| 909 | return; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 910 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 911 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 912 | /* The cast is safe because 'length' is less than 3*PNG_MAX_PALETTE_LENGTH */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 913 | num = (int)length / 3; |
Glenn Randers-Pehrson | 38e6e77 | 2000-04-09 19:06:13 -0500 | [diff] [blame] | 914 | |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 915 | #ifdef PNG_POINTER_INDEXING_SUPPORTED |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 916 | for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++) |
| 917 | { |
| 918 | png_byte buf[3]; |
| 919 | |
| 920 | png_crc_read(png_ptr, buf, 3); |
| 921 | pal_ptr->red = buf[0]; |
| 922 | pal_ptr->green = buf[1]; |
| 923 | pal_ptr->blue = buf[2]; |
| 924 | } |
| 925 | #else |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 926 | for (i = 0; i < num; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 927 | { |
| 928 | png_byte buf[3]; |
| 929 | |
| 930 | png_crc_read(png_ptr, buf, 3); |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 931 | /* Don't depend upon png_color being any order */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 932 | palette[i].red = buf[0]; |
| 933 | palette[i].green = buf[1]; |
| 934 | palette[i].blue = buf[2]; |
| 935 | } |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 936 | #endif |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 937 | |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 938 | /* 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] | 939 | * whatever the normal CRC configuration tells us. However, if we |
| 940 | * have an RGB image, the PLTE can be considered ancillary, so |
| 941 | * we will act as though it is. |
| 942 | */ |
Glenn Randers-Pehrson | b2aca21 | 2009-09-23 11:32:37 -0500 | [diff] [blame] | 943 | #ifndef PNG_READ_OPT_PLTE_SUPPORTED |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 944 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 945 | #endif |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 946 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 947 | png_crc_finish(png_ptr, 0); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 948 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 949 | |
Glenn Randers-Pehrson | b2aca21 | 2009-09-23 11:32:37 -0500 | [diff] [blame] | 950 | #ifndef PNG_READ_OPT_PLTE_SUPPORTED |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 951 | else if (png_crc_error(png_ptr)) /* Only if we have a CRC error */ |
| 952 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 953 | /* If we don't want to use the data from an ancillary chunk, |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 954 | * we have two options: an error abort, or a warning and we |
| 955 | * ignore the data in this chunk (which should be OK, since |
| 956 | * it's considered ancillary for a RGB or RGBA image). |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 957 | * |
| 958 | * IMPLEMENTATION NOTE: this is only here because png_crc_finish uses the |
| 959 | * chunk type to determine whether to check the ancillary or the critical |
| 960 | * flags. |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 961 | */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 962 | if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE)) |
| 963 | { |
| 964 | if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) |
| 965 | { |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 966 | png_chunk_benign_error(png_ptr, "CRC error"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 967 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 968 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 969 | else |
| 970 | { |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 971 | png_chunk_warning(png_ptr, "CRC error"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 972 | return; |
| 973 | } |
| 974 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 975 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 976 | /* Otherwise, we (optionally) emit a warning and use the chunk. */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 977 | else if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)) |
| 978 | { |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 979 | png_chunk_warning(png_ptr, "CRC error"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 980 | } |
| 981 | } |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 982 | #endif |
Glenn Randers-Pehrson | 38e6e77 | 2000-04-09 19:06:13 -0500 | [diff] [blame] | 983 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 984 | /* TODO: png_set_PLTE has the side effect of setting png_ptr->palette to its |
| 985 | * own copy of the palette. This has the side effect that when png_start_row |
| 986 | * is called (this happens after any call to png_read_update_info) the |
| 987 | * info_ptr palette gets changed. This is extremely unexpected and |
| 988 | * confusing. |
| 989 | * |
| 990 | * Fix this by not sharing the palette in this way. |
| 991 | */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 992 | png_set_PLTE(png_ptr, info_ptr, palette, num); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 993 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 994 | /* The three chunks, bKGD, hIST and tRNS *must* appear after PLTE and before |
| 995 | * IDAT. Prior to 1.6.0 this was not checked; instead the code merely |
| 996 | * checked the apparent validity of a tRNS chunk inserted before PLTE on a |
| 997 | * palette PNG. 1.6.0 attempts to rigorously follow the standard and |
| 998 | * therefore does a benign error if the erroneous condition is detected *and* |
| 999 | * cancels the tRNS if the benign error returns. The alternative is to |
| 1000 | * amend the standard since it would be rather hypocritical of the standards |
| 1001 | * maintainers to ignore it. |
| 1002 | */ |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1003 | #ifdef PNG_READ_tRNS_SUPPORTED |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1004 | if (png_ptr->num_trans > 0 || |
| 1005 | (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS) != 0)) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1006 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1007 | /* Cancel this because otherwise it would be used if the transforms |
| 1008 | * require it. Don't cancel the 'valid' flag because this would prevent |
| 1009 | * detection of duplicate chunks. |
| 1010 | */ |
| 1011 | png_ptr->num_trans = 0; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1012 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1013 | if (info_ptr != NULL) |
| 1014 | info_ptr->num_trans = 0; |
| 1015 | |
| 1016 | png_chunk_benign_error(png_ptr, "tRNS must be after"); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1017 | } |
| 1018 | #endif |
| 1019 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1020 | #ifdef PNG_READ_hIST_SUPPORTED |
| 1021 | if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) != 0) |
| 1022 | png_chunk_benign_error(png_ptr, "hIST must be after"); |
| 1023 | #endif |
| 1024 | |
| 1025 | #ifdef PNG_READ_bKGD_SUPPORTED |
| 1026 | if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) != 0) |
| 1027 | png_chunk_benign_error(png_ptr, "bKGD must be after"); |
| 1028 | #endif |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1029 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1030 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1031 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 1032 | png_handle_IEND(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1033 | { |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1034 | png_debug(1, "in png_handle_IEND"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1035 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1036 | if (!(png_ptr->mode & PNG_HAVE_IHDR) || !(png_ptr->mode & PNG_HAVE_IDAT)) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1037 | png_chunk_error(png_ptr, "out of place"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1038 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1039 | png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1040 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1041 | png_crc_finish(png_ptr, length); |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1042 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1043 | if (length != 0) |
| 1044 | png_chunk_benign_error(png_ptr, "invalid"); |
| 1045 | |
| 1046 | PNG_UNUSED(info_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1047 | } |
| 1048 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1049 | #ifdef PNG_READ_gAMA_SUPPORTED |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1050 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 1051 | png_handle_gAMA(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1052 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1053 | png_fixed_point igamma; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1054 | png_byte buf[4]; |
| 1055 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1056 | png_debug(1, "in png_handle_gAMA"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1057 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1058 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1059 | png_chunk_error(png_ptr, "missing IHDR"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1060 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1061 | else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1062 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1063 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1064 | png_chunk_benign_error(png_ptr, "out of place"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1065 | return; |
| 1066 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1067 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1068 | if (length != 4) |
| 1069 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1070 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1071 | png_chunk_benign_error(png_ptr, "invalid"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1072 | return; |
| 1073 | } |
| 1074 | |
| 1075 | png_crc_read(png_ptr, buf, 4); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1076 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1077 | if (png_crc_finish(png_ptr, 0)) |
| 1078 | return; |
| 1079 | |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1080 | igamma = png_get_fixed_point(NULL, buf); |
Glenn Randers-Pehrson | 3389309 | 2010-10-23 13:20:18 -0500 | [diff] [blame] | 1081 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1082 | /* The gAMA value is unsigned (and is a power law correction, so 0 is |
| 1083 | * meaningless.) |
| 1084 | */ |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1085 | if (igamma <= 0) |
| 1086 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1087 | png_chunk_benign_error(png_ptr, "out of range"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1088 | return; |
| 1089 | } |
| 1090 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1091 | /* If a colorspace error has already been output skip this chunk */ |
| 1092 | if (png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) |
| 1093 | return; |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 1094 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1095 | if (png_ptr->colorspace.flags & PNG_COLORSPACE_FROM_gAMA) |
| 1096 | { |
| 1097 | png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID; |
| 1098 | png_colorspace_sync(png_ptr, info_ptr); |
| 1099 | |
| 1100 | png_chunk_benign_error(png_ptr, "duplicate"); |
| 1101 | return; |
| 1102 | } |
| 1103 | |
| 1104 | png_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_gAMA; |
| 1105 | (void)png_colorspace_set_gamma(png_ptr, &png_ptr->colorspace, igamma, |
| 1106 | 1/*prefer gAMA values*/); |
| 1107 | png_colorspace_sync(png_ptr, info_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1108 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1109 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1110 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1111 | #ifdef PNG_READ_sBIT_SUPPORTED |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1112 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 1113 | png_handle_sBIT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1114 | { |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 1115 | unsigned int truelen; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1116 | png_byte buf[4]; |
Guy Schalnat | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 1117 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1118 | png_debug(1, "in png_handle_sBIT"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1119 | |
Guy Schalnat | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 1120 | buf[0] = buf[1] = buf[2] = buf[3] = 0; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1121 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1122 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1123 | png_chunk_error(png_ptr, "missing IHDR"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1124 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1125 | else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1126 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1127 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1128 | png_chunk_benign_error(png_ptr, "out of place"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1129 | return; |
| 1130 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1131 | |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1132 | if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1133 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1134 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1135 | png_chunk_benign_error(png_ptr, "duplicate"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1136 | return; |
| 1137 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1138 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1139 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1140 | truelen = 3; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1141 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1142 | else |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 1143 | truelen = png_ptr->channels; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1144 | |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 1145 | if (length != truelen || length > 4) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1146 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1147 | png_chunk_benign_error(png_ptr, "invalid"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1148 | png_crc_finish(png_ptr, length); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1149 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1150 | } |
| 1151 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1152 | png_crc_read(png_ptr, buf, truelen); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1153 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1154 | if (png_crc_finish(png_ptr, 0)) |
| 1155 | return; |
| 1156 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1157 | if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) |
| 1158 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1159 | png_ptr->sig_bit.red = buf[0]; |
| 1160 | png_ptr->sig_bit.green = buf[1]; |
| 1161 | png_ptr->sig_bit.blue = buf[2]; |
| 1162 | png_ptr->sig_bit.alpha = buf[3]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1163 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1164 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1165 | else |
| 1166 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1167 | png_ptr->sig_bit.gray = buf[0]; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1168 | png_ptr->sig_bit.red = buf[0]; |
| 1169 | png_ptr->sig_bit.green = buf[0]; |
| 1170 | png_ptr->sig_bit.blue = buf[0]; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1171 | png_ptr->sig_bit.alpha = buf[1]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1172 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1173 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1174 | png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit)); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1175 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1176 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1177 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1178 | #ifdef PNG_READ_cHRM_SUPPORTED |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1179 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 1180 | png_handle_cHRM(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1181 | { |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 1182 | png_byte buf[32]; |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1183 | png_xy xy; |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 1184 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1185 | png_debug(1, "in png_handle_cHRM"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1186 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1187 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1188 | png_chunk_error(png_ptr, "missing IHDR"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1189 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1190 | else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1191 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1192 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1193 | png_chunk_benign_error(png_ptr, "out of place"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1194 | return; |
| 1195 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1196 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1197 | if (length != 32) |
| 1198 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1199 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1200 | png_chunk_benign_error(png_ptr, "invalid"); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1201 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1202 | } |
| 1203 | |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 1204 | png_crc_read(png_ptr, buf, 32); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1205 | |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 1206 | if (png_crc_finish(png_ptr, 0)) |
| 1207 | return; |
| 1208 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1209 | xy.whitex = png_get_fixed_point(NULL, buf); |
| 1210 | xy.whitey = png_get_fixed_point(NULL, buf + 4); |
| 1211 | xy.redx = png_get_fixed_point(NULL, buf + 8); |
| 1212 | xy.redy = png_get_fixed_point(NULL, buf + 12); |
| 1213 | xy.greenx = png_get_fixed_point(NULL, buf + 16); |
| 1214 | xy.greeny = png_get_fixed_point(NULL, buf + 20); |
| 1215 | xy.bluex = png_get_fixed_point(NULL, buf + 24); |
| 1216 | xy.bluey = png_get_fixed_point(NULL, buf + 28); |
Glenn Randers-Pehrson | 3389309 | 2010-10-23 13:20:18 -0500 | [diff] [blame] | 1217 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1218 | if (xy.whitex == PNG_FIXED_ERROR || |
| 1219 | xy.whitey == PNG_FIXED_ERROR || |
| 1220 | xy.redx == PNG_FIXED_ERROR || |
| 1221 | xy.redy == PNG_FIXED_ERROR || |
| 1222 | xy.greenx == PNG_FIXED_ERROR || |
| 1223 | xy.greeny == PNG_FIXED_ERROR || |
| 1224 | xy.bluex == PNG_FIXED_ERROR || |
| 1225 | xy.bluey == PNG_FIXED_ERROR) |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1226 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1227 | png_chunk_benign_error(png_ptr, "invalid values"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1228 | return; |
| 1229 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1230 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1231 | /* If a colorspace error has already been output skip this chunk */ |
| 1232 | if (png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) |
| 1233 | return; |
| 1234 | |
| 1235 | if (png_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM) |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1236 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1237 | png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID; |
| 1238 | png_colorspace_sync(png_ptr, info_ptr); |
| 1239 | png_chunk_benign_error(png_ptr, "duplicate"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1240 | return; |
| 1241 | } |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 1242 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1243 | png_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM; |
| 1244 | (void)png_colorspace_set_chromaticities(png_ptr, &png_ptr->colorspace, &xy, |
| 1245 | 1/*prefer cHRM values*/); |
| 1246 | png_colorspace_sync(png_ptr, info_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1247 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1248 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1249 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1250 | #ifdef PNG_READ_sRGB_SUPPORTED |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1251 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 1252 | png_handle_sRGB(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 1253 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1254 | png_byte intent; |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 1255 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1256 | png_debug(1, "in png_handle_sRGB"); |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 1257 | |
| 1258 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1259 | png_chunk_error(png_ptr, "missing IHDR"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1260 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1261 | else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 1262 | { |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 1263 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1264 | png_chunk_benign_error(png_ptr, "out of place"); |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 1265 | return; |
| 1266 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1267 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 1268 | if (length != 1) |
| 1269 | { |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 1270 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1271 | png_chunk_benign_error(png_ptr, "invalid"); |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 1272 | return; |
| 1273 | } |
| 1274 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1275 | png_crc_read(png_ptr, &intent, 1); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1276 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 1277 | if (png_crc_finish(png_ptr, 0)) |
| 1278 | return; |
| 1279 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 1280 | /* Check for bad intent */ |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1281 | if (intent >= PNG_sRGB_INTENT_LAST) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 1282 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1283 | png_chunk_benign_error(png_ptr, "Unknown sRGB intent"); |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 1284 | return; |
| 1285 | } |
| 1286 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1287 | /* If a colorspace error has already been output skip this chunk */ |
| 1288 | if (png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) |
| 1289 | return; |
John Bowler | 88b77cc | 2011-05-05 06:49:55 -0500 | [diff] [blame] | 1290 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1291 | /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect |
| 1292 | * this. |
John Bowler | 736f40f | 2011-08-25 16:19:44 -0500 | [diff] [blame] | 1293 | */ |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1294 | if (png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) |
| 1295 | { |
| 1296 | png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID; |
| 1297 | png_colorspace_sync(png_ptr, info_ptr); |
| 1298 | png_chunk_benign_error(png_ptr, "too many profiles"); |
| 1299 | return; |
| 1300 | } |
John Bowler | 736f40f | 2011-08-25 16:19:44 -0500 | [diff] [blame] | 1301 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1302 | /* Do not override gAMA or cHRM from the PNG file; just check they match. |
| 1303 | * This is because we write a cHRM which corresponds to D65, however there is |
| 1304 | * an issue with CMMs that assume a D50 environment that requires adaptation |
| 1305 | * of the white point. This way at least it is possible to supply an |
| 1306 | * adapated value (so long as it is within the tolerance limits for a match |
| 1307 | * against the D65 chromaticities.) |
| 1308 | * |
| 1309 | * TODO: get expert opinions on this issue |
| 1310 | */ |
| 1311 | (void)png_colorspace_set_sRGB(png_ptr, &png_ptr->colorspace, intent, 0); |
| 1312 | png_colorspace_sync(png_ptr, info_ptr); |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 1313 | } |
| 1314 | #endif /* PNG_READ_sRGB_SUPPORTED */ |
| 1315 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1316 | #ifdef PNG_READ_iCCP_SUPPORTED |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1317 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 1318 | png_handle_iCCP(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
John Bowler | aa816c4 | 2012-03-16 07:39:49 -0500 | [diff] [blame] | 1319 | /* Note: this does not properly handle profiles that are > 64K under DOS */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1320 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1321 | png_const_charp errmsg; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1322 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1323 | png_debug(1, "in png_handle_iCCP"); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1324 | |
| 1325 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1326 | png_chunk_error(png_ptr, "missing IHDR"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1327 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1328 | else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1329 | { |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1330 | png_crc_finish(png_ptr, length); |
John Bowler | aa816c4 | 2012-03-16 07:39:49 -0500 | [diff] [blame] | 1331 | png_chunk_benign_error(png_ptr, "out of place"); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1332 | return; |
| 1333 | } |
| 1334 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1335 | /* Consistent with all the above colorspace handling an obviously *invalid* |
| 1336 | * chunk is just ignored, so does not invalidate the color space. An |
| 1337 | * alternative is to set the 'invalid' flags at the start of this routine |
| 1338 | * and only clear them in they were not set before and all the tests pass. |
| 1339 | * The minimum 'deflate' stream is assumed to be just the 2 byte header and 4 |
| 1340 | * byte checksum. The keyword must be one character and there is a |
| 1341 | * terminator (0) byte and the compression method. |
| 1342 | */ |
| 1343 | if (length < 9) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1344 | { |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 1345 | png_crc_finish(png_ptr, length); |
John Bowler | aa816c4 | 2012-03-16 07:39:49 -0500 | [diff] [blame] | 1346 | png_chunk_benign_error(png_ptr, "too short"); |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 1347 | return; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1348 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1349 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1350 | /* If a colorspace error has already been output skip this chunk */ |
| 1351 | if (png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 1352 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1353 | png_crc_finish(png_ptr, length); |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 1354 | return; |
| 1355 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1356 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1357 | /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect |
| 1358 | * this. |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 1359 | */ |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1360 | if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) == 0) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1361 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1362 | uInt read_length, keyword_length; |
| 1363 | char keyword[81]; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1364 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1365 | /* Find the keyword; the keyword plus separator and compression method |
| 1366 | * bytes can be at most 81 characters long. |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 1367 | */ |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1368 | read_length = 81; /* maximum */ |
| 1369 | if (read_length > length) |
| 1370 | read_length = (uInt)length; |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 1371 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1372 | png_crc_read(png_ptr, (png_bytep)keyword, read_length); |
| 1373 | length -= read_length; |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 1374 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1375 | keyword_length = 0; |
| 1376 | while (keyword_length < 80 && keyword_length < read_length && |
| 1377 | keyword[keyword_length] != 0) |
| 1378 | ++keyword_length; |
| 1379 | |
| 1380 | /* TODO: make the keyword checking common */ |
| 1381 | if (keyword_length >= 1 && keyword_length <= 79) |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 1382 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1383 | /* We only understand '0' compression - deflate - so if we get a |
| 1384 | * different value we can't safely decode the chunk. |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 1385 | */ |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1386 | if (keyword_length+1 < read_length && |
| 1387 | keyword[keyword_length+1] == PNG_COMPRESSION_TYPE_BASE) |
| 1388 | { |
| 1389 | read_length -= keyword_length+2; |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 1390 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1391 | if (png_inflate_claim(png_ptr, png_iCCP, |
| 1392 | png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN ? 15 : 0) == Z_OK) |
| 1393 | { |
| 1394 | int ret; |
| 1395 | Byte profile_header[132]; |
| 1396 | Byte local_buffer[PNG_INFLATE_BUF_SIZE]; |
| 1397 | png_alloc_size_t size = sizeof profile_header; |
| 1398 | |
| 1399 | png_ptr->zstream.next_in = (Bytef*)keyword + (keyword_length+2); |
| 1400 | png_ptr->zstream.avail_in = read_length; |
| 1401 | ret = png_inflate_read(png_ptr, local_buffer, |
| 1402 | sizeof local_buffer, &length, profile_header, &size, |
| 1403 | 0/*finish: don't, because the output is too small*/); |
| 1404 | |
| 1405 | if (size == 0) |
| 1406 | { |
| 1407 | /* We have the ICC profile header; do the basic header checks. |
| 1408 | */ |
| 1409 | const png_uint_32 profile_length = |
| 1410 | png_get_uint_32(profile_header); |
| 1411 | |
| 1412 | errmsg = NULL; /* flag to say error message output */ |
| 1413 | |
| 1414 | if (png_icc_check_length(png_ptr, &png_ptr->colorspace, |
| 1415 | keyword, profile_length)) |
| 1416 | { |
| 1417 | /* The length is apparently ok, so we can check the 132 |
| 1418 | * byte header. |
| 1419 | */ |
| 1420 | if (png_icc_check_header(png_ptr, &png_ptr->colorspace, |
| 1421 | keyword, profile_length, profile_header)) |
| 1422 | { |
| 1423 | /* Now read the tag table; a variable size buffer is |
| 1424 | * needed at this point, allocate one for the whole |
| 1425 | * profile. The header check has already validated |
| 1426 | * that none of these stuff will overflow. |
| 1427 | */ |
| 1428 | const png_uint_32 tag_count = png_get_uint_32( |
| 1429 | profile_header+128); |
| 1430 | png_bytep profile = png_read_buffer(png_ptr, |
| 1431 | profile_length, 2/*silent*/); |
| 1432 | |
| 1433 | if (profile != NULL) |
| 1434 | { |
| 1435 | memcpy(profile, profile_header, |
| 1436 | sizeof profile_header); |
| 1437 | |
| 1438 | size = (sizeof profile_header) + 12 * tag_count; |
| 1439 | ret = png_inflate_read(png_ptr, local_buffer, |
| 1440 | sizeof local_buffer, &length, |
| 1441 | profile + (sizeof profile_header), &size, 0); |
| 1442 | |
| 1443 | if (size == 0) |
| 1444 | { |
| 1445 | if (png_icc_check_tag_table(png_ptr, |
| 1446 | &png_ptr->colorspace, keyword, profile_length, |
| 1447 | profile)) |
| 1448 | { |
| 1449 | /* The profile has been validated for basic |
| 1450 | * security issues, so read the whole thing in. |
| 1451 | */ |
| 1452 | size = profile_length - (sizeof profile_header) |
| 1453 | - 12 * tag_count; |
| 1454 | ret = png_inflate_read(png_ptr, local_buffer, |
| 1455 | sizeof local_buffer, &length, |
| 1456 | profile + (sizeof profile_header) + |
| 1457 | 12 * tag_count, &size, 1/*finish*/); |
| 1458 | |
| 1459 | if (size == 0) |
| 1460 | { |
| 1461 | if (ret != Z_STREAM_END) |
| 1462 | png_chunk_warning(png_ptr, |
| 1463 | png_ptr->zstream.msg); |
| 1464 | |
| 1465 | else if (length > 0) |
| 1466 | png_chunk_warning(png_ptr, |
| 1467 | "extra compressed data"); |
| 1468 | |
| 1469 | /* But, because we got the whole profile, |
| 1470 | * assume it is ok. |
| 1471 | */ |
| 1472 | png_ptr->zowner = 0; |
| 1473 | |
| 1474 | /* Yet the CRC should still be correct */ |
| 1475 | if (png_crc_finish(png_ptr, length)) |
| 1476 | return; |
| 1477 | |
| 1478 | /* Set the gAMA and cHRM information */ |
| 1479 | png_icc_set_gAMA_and_cHRM(png_ptr, |
| 1480 | &png_ptr->colorspace, keyword, profile, |
| 1481 | 0/*prefer explicit gAMA/cHRM*/); |
| 1482 | |
| 1483 | /* And steal the profile for info_ptr. */ |
| 1484 | if (info_ptr != NULL) |
| 1485 | { |
| 1486 | png_free_data(png_ptr, info_ptr, |
| 1487 | PNG_FREE_ICCP, 0); |
| 1488 | |
| 1489 | info_ptr->iccp_name = png_voidcast(char*, |
| 1490 | png_malloc_base(png_ptr, |
| 1491 | keyword_length+1)); |
| 1492 | if (info_ptr->iccp_name == NULL) |
| 1493 | { |
| 1494 | png_ptr->colorspace.flags |= |
| 1495 | PNG_COLORSPACE_INVALID; |
| 1496 | png_colorspace_sync(png_ptr, |
| 1497 | info_ptr); |
| 1498 | png_chunk_benign_error(png_ptr, |
| 1499 | "out of memory"); |
| 1500 | return; |
| 1501 | } |
| 1502 | |
| 1503 | memcpy(info_ptr->iccp_name, keyword, |
| 1504 | keyword_length+1); |
| 1505 | info_ptr->iccp_proflen = profile_length; |
| 1506 | info_ptr->iccp_profile = profile; |
| 1507 | png_ptr->read_buffer = NULL; /*steal*/ |
| 1508 | info_ptr->free_me |= PNG_FREE_ICCP; |
| 1509 | info_ptr->valid |= PNG_INFO_iCCP; |
| 1510 | |
| 1511 | png_colorspace_sync(png_ptr, info_ptr); |
| 1512 | } |
| 1513 | |
| 1514 | return; |
| 1515 | } |
| 1516 | |
| 1517 | else |
| 1518 | errmsg = "truncated"; |
| 1519 | } |
| 1520 | |
| 1521 | /* else png_icc_check_tag_table output an error */ |
| 1522 | } |
| 1523 | |
| 1524 | else /* profile truncated */ |
| 1525 | errmsg = png_ptr->zstream.msg; |
| 1526 | } |
| 1527 | |
| 1528 | else |
| 1529 | errmsg = "out of memory"; |
| 1530 | } |
| 1531 | |
| 1532 | /* else png_icc_check_header output an error */ |
| 1533 | } |
| 1534 | |
| 1535 | /* else png_icc_check_length output an error */ |
| 1536 | } |
| 1537 | |
| 1538 | else /* profile truncated */ |
| 1539 | errmsg = png_ptr->zstream.msg; |
| 1540 | |
| 1541 | /* Release the stream */ |
| 1542 | png_ptr->zowner = 0; |
| 1543 | } |
| 1544 | |
| 1545 | else /* png_inflate_claim failed */ |
| 1546 | errmsg = png_ptr->zstream.msg; |
| 1547 | } |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 1548 | |
| 1549 | else |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1550 | errmsg = "bad compression method"; /* or missing */ |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 1551 | } |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 1552 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 1553 | else |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1554 | errmsg = "bad keyword"; |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 1555 | } |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1556 | |
| 1557 | else |
| 1558 | errmsg = "too many profiles"; |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 1559 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1560 | /* Failure: the reason is in 'errmsg' */ |
| 1561 | png_crc_finish(png_ptr, length); |
| 1562 | png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID; |
| 1563 | png_colorspace_sync(png_ptr, info_ptr); |
| 1564 | if (errmsg != NULL) /* else already output */ |
| 1565 | png_chunk_benign_error(png_ptr, errmsg); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1566 | } |
| 1567 | #endif /* PNG_READ_iCCP_SUPPORTED */ |
| 1568 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1569 | #ifdef PNG_READ_sPLT_SUPPORTED |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1570 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 1571 | png_handle_sPLT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1572 | /* Note: this does not properly handle chunks that are > 64K under DOS */ |
| 1573 | { |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 1574 | png_bytep entry_start, buffer; |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 1575 | png_sPLT_t new_palette; |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 1576 | png_sPLT_entryp pp; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1577 | png_uint_32 data_length; |
| 1578 | int entry_size, i; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1579 | png_uint_32 skip = 0; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1580 | png_uint_32 dl; |
| 1581 | png_size_t max_dl; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1582 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1583 | png_debug(1, "in png_handle_sPLT"); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1584 | |
Glenn Randers-Pehrson | e3f3c4e | 2010-02-07 18:08:50 -0600 | [diff] [blame] | 1585 | #ifdef PNG_USER_LIMITS_SUPPORTED |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 1586 | if (png_ptr->user_chunk_cache_max != 0) |
| 1587 | { |
| 1588 | if (png_ptr->user_chunk_cache_max == 1) |
| 1589 | { |
| 1590 | png_crc_finish(png_ptr, length); |
| 1591 | return; |
| 1592 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1593 | |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 1594 | if (--png_ptr->user_chunk_cache_max == 1) |
| 1595 | { |
| 1596 | png_warning(png_ptr, "No space in chunk cache for sPLT"); |
| 1597 | png_crc_finish(png_ptr, length); |
| 1598 | return; |
| 1599 | } |
| 1600 | } |
| 1601 | #endif |
| 1602 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1603 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1604 | png_chunk_error(png_ptr, "missing IHDR"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1605 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1606 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 1607 | { |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1608 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1609 | png_chunk_benign_error(png_ptr, "out of place"); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1610 | return; |
| 1611 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1612 | |
| 1613 | #ifdef PNG_MAX_MALLOC_64K |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1614 | if (length > 65535U) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1615 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1616 | png_crc_finish(png_ptr, length); |
| 1617 | png_chunk_benign_error(png_ptr, "too large to fit in memory"); |
| 1618 | return; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1619 | } |
| 1620 | #endif |
| 1621 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1622 | buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/); |
| 1623 | if (buffer == NULL) |
| 1624 | { |
| 1625 | png_crc_finish(png_ptr, length); |
| 1626 | png_chunk_benign_error(png_ptr, "out of memory"); |
| 1627 | return; |
| 1628 | } |
| 1629 | |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1630 | |
| 1631 | /* WARNING: this may break if size_t is less than 32 bits; it is assumed |
| 1632 | * that the PNG_MAX_MALLOC_64K test is enabled in this case, but this is a |
| 1633 | * potential breakage point if the types in pngconf.h aren't exactly right. |
| 1634 | */ |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 1635 | png_crc_read(png_ptr, buffer, length); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1636 | |
| 1637 | if (png_crc_finish(png_ptr, skip)) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1638 | return; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1639 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 1640 | buffer[length] = 0; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1641 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 1642 | for (entry_start = buffer; *entry_start; entry_start++) |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 1643 | /* Empty loop to find end of name */ ; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1644 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1645 | ++entry_start; |
| 1646 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 1647 | /* A sample depth should follow the separator, and we should be on it */ |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 1648 | if (entry_start > buffer + length - 2) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1649 | { |
Glenn Randers-Pehrson | 4accabb | 2000-04-14 14:20:47 -0500 | [diff] [blame] | 1650 | png_warning(png_ptr, "malformed sPLT chunk"); |
| 1651 | return; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1652 | } |
| 1653 | |
| 1654 | new_palette.depth = *entry_start++; |
Glenn Randers-Pehrson | a565f0e | 2010-03-06 08:24:45 -0600 | [diff] [blame] | 1655 | entry_size = (new_palette.depth == 8 ? 6 : 10); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1656 | /* This must fit in a png_uint_32 because it is derived from the original |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 1657 | * chunk data length. |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1658 | */ |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 1659 | data_length = length - (png_uint_32)(entry_start - buffer); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1660 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 1661 | /* Integrity-check the data length */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1662 | if (data_length % entry_size) |
| 1663 | { |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 1664 | png_warning(png_ptr, "sPLT chunk has bad length"); |
| 1665 | return; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1666 | } |
| 1667 | |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1668 | dl = (png_int_32)(data_length / entry_size); |
| 1669 | max_dl = PNG_SIZE_MAX / png_sizeof(png_sPLT_entry); |
| 1670 | |
| 1671 | if (dl > max_dl) |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 1672 | { |
| 1673 | png_warning(png_ptr, "sPLT chunk too long"); |
| 1674 | return; |
| 1675 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1676 | |
| 1677 | new_palette.nentries = (png_int_32)(data_length / entry_size); |
| 1678 | |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 1679 | new_palette.entries = (png_sPLT_entryp)png_malloc_warn( |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1680 | png_ptr, new_palette.nentries * png_sizeof(png_sPLT_entry)); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1681 | |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 1682 | if (new_palette.entries == NULL) |
| 1683 | { |
| 1684 | png_warning(png_ptr, "sPLT chunk requires too much memory"); |
| 1685 | return; |
| 1686 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1687 | |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 1688 | #ifdef PNG_POINTER_INDEXING_SUPPORTED |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1689 | for (i = 0; i < new_palette.nentries; i++) |
| 1690 | { |
Glenn Randers-Pehrson | 90b878c | 2009-10-07 12:44:35 -0500 | [diff] [blame] | 1691 | pp = new_palette.entries + i; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1692 | |
| 1693 | if (new_palette.depth == 8) |
| 1694 | { |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1695 | pp->red = *entry_start++; |
| 1696 | pp->green = *entry_start++; |
| 1697 | pp->blue = *entry_start++; |
| 1698 | pp->alpha = *entry_start++; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1699 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1700 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1701 | else |
| 1702 | { |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1703 | pp->red = png_get_uint_16(entry_start); entry_start += 2; |
| 1704 | pp->green = png_get_uint_16(entry_start); entry_start += 2; |
| 1705 | pp->blue = png_get_uint_16(entry_start); entry_start += 2; |
| 1706 | pp->alpha = png_get_uint_16(entry_start); entry_start += 2; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1707 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1708 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1709 | pp->frequency = png_get_uint_16(entry_start); entry_start += 2; |
| 1710 | } |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 1711 | #else |
| 1712 | pp = new_palette.entries; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1713 | |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 1714 | for (i = 0; i < new_palette.nentries; i++) |
| 1715 | { |
| 1716 | |
| 1717 | if (new_palette.depth == 8) |
| 1718 | { |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1719 | pp[i].red = *entry_start++; |
| 1720 | pp[i].green = *entry_start++; |
| 1721 | pp[i].blue = *entry_start++; |
| 1722 | pp[i].alpha = *entry_start++; |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 1723 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1724 | |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 1725 | else |
| 1726 | { |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1727 | pp[i].red = png_get_uint_16(entry_start); entry_start += 2; |
| 1728 | pp[i].green = png_get_uint_16(entry_start); entry_start += 2; |
| 1729 | pp[i].blue = png_get_uint_16(entry_start); entry_start += 2; |
| 1730 | pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2; |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 1731 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1732 | |
Glenn Randers-Pehrson | f27592a | 2011-03-21 18:05:40 -0500 | [diff] [blame] | 1733 | pp[i].frequency = png_get_uint_16(entry_start); entry_start += 2; |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 1734 | } |
| 1735 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1736 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 1737 | /* Discard all chunk data except the name and stash that */ |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 1738 | new_palette.name = (png_charp)buffer; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1739 | |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 1740 | png_set_sPLT(png_ptr, info_ptr, &new_palette, 1); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1741 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1742 | png_free(png_ptr, new_palette.entries); |
| 1743 | } |
| 1744 | #endif /* PNG_READ_sPLT_SUPPORTED */ |
| 1745 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1746 | #ifdef PNG_READ_tRNS_SUPPORTED |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1747 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 1748 | png_handle_tRNS(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1749 | { |
Glenn Randers-Pehrson | d1e8c86 | 2002-06-20 06:54:34 -0500 | [diff] [blame] | 1750 | png_byte readbuf[PNG_MAX_PALETTE_LENGTH]; |
Glenn Randers-Pehrson | 76e5fd6 | 2000-12-28 07:50:05 -0600 | [diff] [blame] | 1751 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1752 | png_debug(1, "in png_handle_tRNS"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1753 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1754 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1755 | png_chunk_error(png_ptr, "missing IHDR"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1756 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1757 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 1758 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1759 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1760 | png_chunk_benign_error(png_ptr, "out of place"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1761 | return; |
| 1762 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1763 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1764 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1765 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1766 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1767 | png_chunk_benign_error(png_ptr, "duplicate"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1768 | return; |
| 1769 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1770 | |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1771 | if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1772 | { |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1773 | png_byte buf[2]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1774 | |
| 1775 | if (length != 2) |
| 1776 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1777 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1778 | png_chunk_benign_error(png_ptr, "invalid"); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1779 | return; |
| 1780 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1781 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1782 | png_crc_read(png_ptr, buf, 2); |
| 1783 | png_ptr->num_trans = 1; |
Glenn Randers-Pehrson | 56f6396 | 2008-10-06 10:16:17 -0500 | [diff] [blame] | 1784 | png_ptr->trans_color.gray = png_get_uint_16(buf); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1785 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1786 | |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1787 | else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB) |
| 1788 | { |
| 1789 | png_byte buf[6]; |
| 1790 | |
| 1791 | if (length != 6) |
| 1792 | { |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1793 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1794 | png_chunk_benign_error(png_ptr, "invalid"); |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1795 | return; |
| 1796 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1797 | |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 1798 | png_crc_read(png_ptr, buf, length); |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1799 | png_ptr->num_trans = 1; |
Glenn Randers-Pehrson | 56f6396 | 2008-10-06 10:16:17 -0500 | [diff] [blame] | 1800 | png_ptr->trans_color.red = png_get_uint_16(buf); |
| 1801 | png_ptr->trans_color.green = png_get_uint_16(buf + 2); |
| 1802 | png_ptr->trans_color.blue = png_get_uint_16(buf + 4); |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1803 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1804 | |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1805 | else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 1806 | { |
| 1807 | if (!(png_ptr->mode & PNG_HAVE_PLTE)) |
| 1808 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1809 | /* TODO: is this actually an error in the ISO spec? */ |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1810 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1811 | png_chunk_benign_error(png_ptr, "out of place"); |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1812 | return; |
| 1813 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1814 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1815 | if (length > png_ptr->num_palette || length > PNG_MAX_PALETTE_LENGTH || |
| 1816 | length == 0) |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1817 | { |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1818 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1819 | png_chunk_benign_error(png_ptr, "invalid"); |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1820 | return; |
| 1821 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1822 | |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 1823 | png_crc_read(png_ptr, readbuf, length); |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1824 | png_ptr->num_trans = (png_uint_16)length; |
| 1825 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1826 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1827 | else |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1828 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1829 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1830 | png_chunk_benign_error(png_ptr, "invalid with alpha channel"); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1831 | return; |
| 1832 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1833 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1834 | if (png_crc_finish(png_ptr, 0)) |
Glenn Randers-Pehrson | a7dbcba | 2007-05-15 16:16:34 -0500 | [diff] [blame] | 1835 | { |
| 1836 | png_ptr->num_trans = 0; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1837 | return; |
Glenn Randers-Pehrson | a7dbcba | 2007-05-15 16:16:34 -0500 | [diff] [blame] | 1838 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1839 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1840 | /* TODO: this is a horrible side effect in the palette case because the |
| 1841 | * png_struct ends up with a pointer to the tRNS buffer owned by the |
| 1842 | * png_info. Fix this. |
| 1843 | */ |
Glenn Randers-Pehrson | 76e5fd6 | 2000-12-28 07:50:05 -0600 | [diff] [blame] | 1844 | png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans, |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1845 | &(png_ptr->trans_color)); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1846 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1847 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1848 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1849 | #ifdef PNG_READ_bKGD_SUPPORTED |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1850 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 1851 | png_handle_bKGD(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1852 | { |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 1853 | unsigned int truelen; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1854 | png_byte buf[6]; |
John Bowler | cb0b296 | 2011-05-12 21:48:29 -0500 | [diff] [blame] | 1855 | png_color_16 background; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1856 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1857 | png_debug(1, "in png_handle_bKGD"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1858 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1859 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1860 | png_chunk_error(png_ptr, "missing IHDR"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1861 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1862 | else if ((png_ptr->mode & PNG_HAVE_IDAT) || |
| 1863 | (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && |
| 1864 | !(png_ptr->mode & PNG_HAVE_PLTE))) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1865 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1866 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1867 | png_chunk_benign_error(png_ptr, "out of place"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1868 | return; |
| 1869 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1870 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1871 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1872 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1873 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1874 | png_chunk_benign_error(png_ptr, "duplicate"); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1875 | return; |
| 1876 | } |
| 1877 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1878 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 1879 | truelen = 1; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1880 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1881 | else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) |
| 1882 | truelen = 6; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1883 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1884 | else |
| 1885 | truelen = 2; |
| 1886 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1887 | if (length != truelen) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1888 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1889 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1890 | png_chunk_benign_error(png_ptr, "invalid"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1891 | return; |
| 1892 | } |
| 1893 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1894 | png_crc_read(png_ptr, buf, truelen); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1895 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1896 | if (png_crc_finish(png_ptr, 0)) |
| 1897 | return; |
| 1898 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1899 | /* We convert the index value into RGB components so that we can allow |
| 1900 | * arbitrary RGB values for background when we have transparency, and |
| 1901 | * so it is easy to determine the RGB values of the background color |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1902 | * from the info_ptr struct. |
| 1903 | */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1904 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1905 | { |
John Bowler | cb0b296 | 2011-05-12 21:48:29 -0500 | [diff] [blame] | 1906 | background.index = buf[0]; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1907 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1908 | if (info_ptr && info_ptr->num_palette) |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1909 | { |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1910 | if (buf[0] >= info_ptr->num_palette) |
| 1911 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1912 | png_chunk_benign_error(png_ptr, "invalid index"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1913 | return; |
| 1914 | } |
| 1915 | |
John Bowler | cb0b296 | 2011-05-12 21:48:29 -0500 | [diff] [blame] | 1916 | background.red = (png_uint_16)png_ptr->palette[buf[0]].red; |
| 1917 | background.green = (png_uint_16)png_ptr->palette[buf[0]].green; |
| 1918 | background.blue = (png_uint_16)png_ptr->palette[buf[0]].blue; |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1919 | } |
John Bowler | cb0b296 | 2011-05-12 21:48:29 -0500 | [diff] [blame] | 1920 | |
| 1921 | else |
| 1922 | background.red = background.green = background.blue = 0; |
| 1923 | |
| 1924 | background.gray = 0; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1925 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1926 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1927 | else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) /* GRAY */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1928 | { |
John Bowler | cb0b296 | 2011-05-12 21:48:29 -0500 | [diff] [blame] | 1929 | background.index = 0; |
| 1930 | background.red = |
| 1931 | background.green = |
| 1932 | background.blue = |
| 1933 | background.gray = png_get_uint_16(buf); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1934 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1935 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1936 | else |
| 1937 | { |
John Bowler | cb0b296 | 2011-05-12 21:48:29 -0500 | [diff] [blame] | 1938 | background.index = 0; |
| 1939 | background.red = png_get_uint_16(buf); |
| 1940 | background.green = png_get_uint_16(buf + 2); |
| 1941 | background.blue = png_get_uint_16(buf + 4); |
| 1942 | background.gray = 0; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1943 | } |
| 1944 | |
John Bowler | cb0b296 | 2011-05-12 21:48:29 -0500 | [diff] [blame] | 1945 | png_set_bKGD(png_ptr, info_ptr, &background); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1946 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1947 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1948 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1949 | #ifdef PNG_READ_hIST_SUPPORTED |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1950 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 1951 | png_handle_hIST(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1952 | { |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 1953 | unsigned int num, i; |
Glenn Randers-Pehrson | d1e8c86 | 2002-06-20 06:54:34 -0500 | [diff] [blame] | 1954 | png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1955 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1956 | png_debug(1, "in png_handle_hIST"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1957 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1958 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1959 | png_chunk_error(png_ptr, "missing IHDR"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1960 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1961 | else if ((png_ptr->mode & PNG_HAVE_IDAT) || !(png_ptr->mode & PNG_HAVE_PLTE)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1962 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1963 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1964 | png_chunk_benign_error(png_ptr, "out of place"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1965 | return; |
| 1966 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1967 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1968 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1969 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1970 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1971 | png_chunk_benign_error(png_ptr, "duplicate"); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1972 | return; |
| 1973 | } |
| 1974 | |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 1975 | num = length / 2 ; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1976 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1977 | if (num != png_ptr->num_palette || num > PNG_MAX_PALETTE_LENGTH) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1978 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1979 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 1980 | png_chunk_benign_error(png_ptr, "invalid"); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1981 | return; |
| 1982 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1983 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1984 | for (i = 0; i < num; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1985 | { |
| 1986 | png_byte buf[2]; |
| 1987 | |
| 1988 | png_crc_read(png_ptr, buf, 2); |
Glenn Randers-Pehrson | 76e5fd6 | 2000-12-28 07:50:05 -0600 | [diff] [blame] | 1989 | readbuf[i] = png_get_uint_16(buf); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1990 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1991 | |
| 1992 | if (png_crc_finish(png_ptr, 0)) |
| 1993 | return; |
| 1994 | |
Glenn Randers-Pehrson | 76e5fd6 | 2000-12-28 07:50:05 -0600 | [diff] [blame] | 1995 | png_set_hIST(png_ptr, info_ptr, readbuf); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1996 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1997 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1998 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1999 | #ifdef PNG_READ_pHYs_SUPPORTED |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2000 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 2001 | png_handle_pHYs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2002 | { |
| 2003 | png_byte buf[9]; |
| 2004 | png_uint_32 res_x, res_y; |
| 2005 | int unit_type; |
| 2006 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2007 | png_debug(1, "in png_handle_pHYs"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2008 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2009 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2010 | png_chunk_error(png_ptr, "missing IHDR"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2011 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2012 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 2013 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2014 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2015 | png_chunk_benign_error(png_ptr, "out of place"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2016 | return; |
| 2017 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2018 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2019 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2020 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2021 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2022 | png_chunk_benign_error(png_ptr, "duplicate"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2023 | return; |
| 2024 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2025 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2026 | if (length != 9) |
| 2027 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2028 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2029 | png_chunk_benign_error(png_ptr, "invalid"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2030 | return; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2031 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2032 | |
| 2033 | png_crc_read(png_ptr, buf, 9); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2034 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2035 | if (png_crc_finish(png_ptr, 0)) |
| 2036 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2037 | |
| 2038 | res_x = png_get_uint_32(buf); |
| 2039 | res_y = png_get_uint_32(buf + 4); |
| 2040 | unit_type = buf[8]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2041 | 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] | 2042 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2043 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2044 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 2045 | #ifdef PNG_READ_oFFs_SUPPORTED |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2046 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 2047 | png_handle_oFFs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2048 | { |
| 2049 | png_byte buf[9]; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2050 | png_int_32 offset_x, offset_y; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2051 | int unit_type; |
| 2052 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2053 | png_debug(1, "in png_handle_oFFs"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2054 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2055 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2056 | png_chunk_error(png_ptr, "missing IHDR"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2057 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2058 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 2059 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2060 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2061 | png_chunk_benign_error(png_ptr, "out of place"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2062 | return; |
| 2063 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2064 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2065 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2066 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2067 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2068 | png_chunk_benign_error(png_ptr, "duplicate"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2069 | return; |
| 2070 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2071 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2072 | if (length != 9) |
| 2073 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2074 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2075 | png_chunk_benign_error(png_ptr, "invalid"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2076 | return; |
| 2077 | } |
| 2078 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2079 | png_crc_read(png_ptr, buf, 9); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2080 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2081 | if (png_crc_finish(png_ptr, 0)) |
| 2082 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2083 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2084 | offset_x = png_get_int_32(buf); |
| 2085 | offset_y = png_get_int_32(buf + 4); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2086 | unit_type = buf[8]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2087 | png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type); |
| 2088 | } |
| 2089 | #endif |
| 2090 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 2091 | #ifdef PNG_READ_pCAL_SUPPORTED |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 2092 | /* Read the pCAL chunk (described in the PNG Extensions document) */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2093 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 2094 | png_handle_pCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2095 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2096 | png_int_32 X0, X1; |
| 2097 | png_byte type, nparams; |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2098 | png_bytep buffer, buf, units, endptr; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2099 | png_charpp params; |
| 2100 | int i; |
| 2101 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2102 | png_debug(1, "in png_handle_pCAL"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2103 | |
| 2104 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2105 | png_chunk_error(png_ptr, "missing IHDR"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2106 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2107 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 2108 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2109 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2110 | png_chunk_benign_error(png_ptr, "out of place"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2111 | return; |
| 2112 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2113 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2114 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2115 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2116 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2117 | png_chunk_benign_error(png_ptr, "duplicate"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2118 | return; |
| 2119 | } |
| 2120 | |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2121 | png_debug1(2, "Allocating and reading pCAL chunk data (%u bytes)", |
| 2122 | length + 1); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2123 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2124 | buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/); |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2125 | |
| 2126 | if (buffer == NULL) |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2127 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2128 | png_crc_finish(png_ptr, length); |
| 2129 | png_chunk_benign_error(png_ptr, "out of memory"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2130 | return; |
| 2131 | } |
| 2132 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2133 | png_crc_read(png_ptr, buffer, length); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2134 | |
| 2135 | if (png_crc_finish(png_ptr, 0)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2136 | return; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2137 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2138 | buffer[length] = 0; /* Null terminate the last string */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2139 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2140 | png_debug(3, "Finding end of pCAL purpose string"); |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2141 | for (buf = buffer; *buf; buf++) |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 2142 | /* Empty loop */ ; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2143 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2144 | endptr = buffer + length; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2145 | |
| 2146 | /* We need to have at least 12 bytes after the purpose string |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2147 | * in order to get the parameter information. |
| 2148 | */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2149 | if (endptr <= buf + 12) |
| 2150 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2151 | png_chunk_benign_error(png_ptr, "invalid"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2152 | return; |
| 2153 | } |
| 2154 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2155 | png_debug(3, "Reading pCAL X0, X1, type, nparams, and units"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2156 | X0 = png_get_int_32((png_bytep)buf+1); |
| 2157 | X1 = png_get_int_32((png_bytep)buf+5); |
| 2158 | type = buf[9]; |
| 2159 | nparams = buf[10]; |
| 2160 | units = buf + 11; |
| 2161 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2162 | png_debug(3, "Checking pCAL equation type and number of parameters"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2163 | /* Check that we have the right number of parameters for known |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2164 | * equation types. |
| 2165 | */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2166 | if ((type == PNG_EQUATION_LINEAR && nparams != 2) || |
| 2167 | (type == PNG_EQUATION_BASE_E && nparams != 3) || |
| 2168 | (type == PNG_EQUATION_ARBITRARY && nparams != 3) || |
| 2169 | (type == PNG_EQUATION_HYPERBOLIC && nparams != 4)) |
| 2170 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2171 | png_chunk_benign_error(png_ptr, "invalid parameter count"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2172 | return; |
| 2173 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2174 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2175 | else if (type >= PNG_EQUATION_LAST) |
| 2176 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2177 | png_chunk_benign_error(png_ptr, "unrecognized equation type"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2178 | } |
| 2179 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2180 | for (buf = units; *buf; buf++) |
Glenn Randers-Pehrson | f9f2fe0 | 1998-03-15 18:20:23 -0600 | [diff] [blame] | 2181 | /* Empty loop to move past the units string. */ ; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2182 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2183 | png_debug(3, "Allocating pCAL parameters array"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2184 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2185 | params = png_voidcast(png_charpp, png_malloc_warn(png_ptr, |
| 2186 | nparams * png_sizeof(png_charp))); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2187 | |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 2188 | if (params == NULL) |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2189 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2190 | png_chunk_benign_error(png_ptr, "out of memory"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2191 | return; |
| 2192 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2193 | |
| 2194 | /* Get pointers to the start of each parameter string. */ |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2195 | for (i = 0; i < nparams; i++) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2196 | { |
| 2197 | buf++; /* Skip the null string terminator from previous parameter. */ |
| 2198 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2199 | png_debug1(3, "Reading pCAL parameter %d", i); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2200 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2201 | for (params[i] = (png_charp)buf; buf <= endptr && *buf != 0; buf++) |
Glenn Randers-Pehrson | f9f2fe0 | 1998-03-15 18:20:23 -0600 | [diff] [blame] | 2202 | /* Empty loop to move past each parameter string */ ; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2203 | |
| 2204 | /* Make sure we haven't run out of data yet */ |
| 2205 | if (buf > endptr) |
| 2206 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2207 | png_free(png_ptr, params); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2208 | png_chunk_benign_error(png_ptr, "invalid data"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2209 | return; |
| 2210 | } |
| 2211 | } |
| 2212 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2213 | png_set_pCAL(png_ptr, info_ptr, (png_charp)buffer, X0, X1, type, nparams, |
| 2214 | (png_charp)units, params); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2215 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2216 | png_free(png_ptr, params); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2217 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2218 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2219 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 2220 | #ifdef PNG_READ_sCAL_SUPPORTED |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 2221 | /* Read the sCAL chunk */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2222 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 2223 | png_handle_sCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2224 | { |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2225 | png_bytep buffer; |
| 2226 | png_size_t i; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2227 | int state; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2228 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2229 | png_debug(1, "in png_handle_sCAL"); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2230 | |
| 2231 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2232 | png_chunk_error(png_ptr, "missing IHDR"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2233 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2234 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 2235 | { |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2236 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2237 | png_chunk_benign_error(png_ptr, "out of place"); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2238 | return; |
| 2239 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2240 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2241 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL)) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2242 | { |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2243 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2244 | png_chunk_benign_error(png_ptr, "duplicate"); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2245 | return; |
| 2246 | } |
| 2247 | |
Glenn Randers-Pehrson | 254a513 | 2011-06-10 17:45:48 -0500 | [diff] [blame] | 2248 | /* Need unit type, width, \0, height: minimum 4 bytes */ |
| 2249 | else if (length < 4) |
| 2250 | { |
Glenn Randers-Pehrson | 254a513 | 2011-06-10 17:45:48 -0500 | [diff] [blame] | 2251 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2252 | png_chunk_benign_error(png_ptr, "invalid"); |
Glenn Randers-Pehrson | 254a513 | 2011-06-10 17:45:48 -0500 | [diff] [blame] | 2253 | return; |
| 2254 | } |
| 2255 | |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2256 | png_debug1(2, "Allocating and reading sCAL chunk data (%u bytes)", |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2257 | length + 1); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2258 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2259 | buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2260 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2261 | if (buffer == NULL) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2262 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2263 | png_chunk_benign_error(png_ptr, "out of memory"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2264 | png_crc_finish(png_ptr, length); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2265 | return; |
| 2266 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2267 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2268 | png_crc_read(png_ptr, buffer, length); |
| 2269 | buffer[length] = 0; /* Null terminate the last string */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2270 | |
| 2271 | if (png_crc_finish(png_ptr, 0)) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2272 | return; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2273 | |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2274 | /* Validate the unit. */ |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2275 | if (buffer[0] != 1 && buffer[0] != 2) |
Glenn Randers-Pehrson | 4accabb | 2000-04-14 14:20:47 -0500 | [diff] [blame] | 2276 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2277 | png_chunk_benign_error(png_ptr, "invalid unit"); |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 2278 | return; |
| 2279 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2280 | |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2281 | /* Validate the ASCII numbers, need two ASCII numbers separated by |
| 2282 | * a '\0' and they need to fit exactly in the chunk data. |
| 2283 | */ |
Glenn Randers-Pehrson | 254a513 | 2011-06-10 17:45:48 -0500 | [diff] [blame] | 2284 | i = 1; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2285 | state = 0; |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 2286 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2287 | if (!png_check_fp_number((png_const_charp)buffer, length, &state, &i) || |
| 2288 | i >= length || buffer[i++] != 0) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2289 | png_chunk_benign_error(png_ptr, "bad width format"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2290 | |
John Bowler | 8d26126 | 2011-06-18 13:37:11 -0500 | [diff] [blame] | 2291 | else if (!PNG_FP_IS_POSITIVE(state)) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2292 | png_chunk_benign_error(png_ptr, "non-positive width"); |
John Bowler | 8d26126 | 2011-06-18 13:37:11 -0500 | [diff] [blame] | 2293 | |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2294 | else |
Glenn Randers-Pehrson | 20788d3 | 2011-01-06 09:01:04 -0600 | [diff] [blame] | 2295 | { |
John Bowler | 168a433 | 2011-01-16 19:32:22 -0600 | [diff] [blame] | 2296 | png_size_t heighti = i; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2297 | |
Glenn Randers-Pehrson | 254a513 | 2011-06-10 17:45:48 -0500 | [diff] [blame] | 2298 | state = 0; |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2299 | if (!png_check_fp_number((png_const_charp)buffer, length, &state, &i) || |
| 2300 | i != length) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2301 | png_chunk_benign_error(png_ptr, "bad height format"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2302 | |
John Bowler | 8d26126 | 2011-06-18 13:37:11 -0500 | [diff] [blame] | 2303 | else if (!PNG_FP_IS_POSITIVE(state)) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2304 | png_chunk_benign_error(png_ptr, "non-positive height"); |
John Bowler | 8d26126 | 2011-06-18 13:37:11 -0500 | [diff] [blame] | 2305 | |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2306 | else |
| 2307 | /* This is the (only) success case. */ |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2308 | png_set_sCAL_s(png_ptr, info_ptr, buffer[0], |
| 2309 | (png_charp)buffer+1, (png_charp)buffer+heighti); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2310 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2311 | } |
| 2312 | #endif |
| 2313 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 2314 | #ifdef PNG_READ_tIME_SUPPORTED |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2315 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 2316 | png_handle_tIME(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2317 | { |
| 2318 | png_byte buf[7]; |
| 2319 | png_time mod_time; |
| 2320 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2321 | png_debug(1, "in png_handle_tIME"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2322 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2323 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2324 | png_chunk_error(png_ptr, "missing IHDR"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2325 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2326 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2327 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2328 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2329 | png_chunk_benign_error(png_ptr, "duplicate"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2330 | return; |
| 2331 | } |
| 2332 | |
| 2333 | if (png_ptr->mode & PNG_HAVE_IDAT) |
| 2334 | png_ptr->mode |= PNG_AFTER_IDAT; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2335 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2336 | if (length != 7) |
| 2337 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2338 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2339 | png_chunk_benign_error(png_ptr, "invalid"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2340 | return; |
| 2341 | } |
| 2342 | |
| 2343 | png_crc_read(png_ptr, buf, 7); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2344 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2345 | if (png_crc_finish(png_ptr, 0)) |
| 2346 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2347 | |
| 2348 | mod_time.second = buf[6]; |
| 2349 | mod_time.minute = buf[5]; |
| 2350 | mod_time.hour = buf[4]; |
| 2351 | mod_time.day = buf[3]; |
| 2352 | mod_time.month = buf[2]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2353 | mod_time.year = png_get_uint_16(buf); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2354 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2355 | png_set_tIME(png_ptr, info_ptr, &mod_time); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2356 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2357 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2358 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 2359 | #ifdef PNG_READ_tEXt_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2360 | /* 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] | 2361 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 2362 | png_handle_tEXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2363 | { |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2364 | png_text text_info; |
| 2365 | png_bytep buffer; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2366 | png_charp key; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 2367 | png_charp text; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2368 | png_uint_32 skip = 0; |
| 2369 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2370 | png_debug(1, "in png_handle_tEXt"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2371 | |
Glenn Randers-Pehrson | e3f3c4e | 2010-02-07 18:08:50 -0600 | [diff] [blame] | 2372 | #ifdef PNG_USER_LIMITS_SUPPORTED |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 2373 | if (png_ptr->user_chunk_cache_max != 0) |
| 2374 | { |
| 2375 | if (png_ptr->user_chunk_cache_max == 1) |
| 2376 | { |
| 2377 | png_crc_finish(png_ptr, length); |
| 2378 | return; |
| 2379 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2380 | |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 2381 | if (--png_ptr->user_chunk_cache_max == 1) |
| 2382 | { |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 2383 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2384 | png_chunk_benign_error(png_ptr, "no space in chunk cache"); |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 2385 | return; |
| 2386 | } |
| 2387 | } |
| 2388 | #endif |
| 2389 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2390 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2391 | png_chunk_error(png_ptr, "missing IHDR"); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2392 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2393 | if (png_ptr->mode & PNG_HAVE_IDAT) |
| 2394 | png_ptr->mode |= PNG_AFTER_IDAT; |
| 2395 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2396 | #ifdef PNG_MAX_MALLOC_64K |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2397 | if (length > 65535U) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2398 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2399 | png_crc_finish(png_ptr, length); |
| 2400 | png_chunk_benign_error(png_ptr, "too large to fit in memory"); |
| 2401 | return; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2402 | } |
| 2403 | #endif |
| 2404 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2405 | buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/); |
Glenn Randers-Pehrson | 97a9b48 | 2008-10-25 20:03:28 -0500 | [diff] [blame] | 2406 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2407 | if (buffer == NULL) |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 2408 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2409 | png_chunk_benign_error(png_ptr, "out of memory"); |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 2410 | return; |
| 2411 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2412 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2413 | png_crc_read(png_ptr, buffer, length); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2414 | |
| 2415 | if (png_crc_finish(png_ptr, skip)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2416 | return; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2417 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2418 | key = (png_charp)buffer; |
| 2419 | key[length] = 0; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2420 | |
| 2421 | for (text = key; *text; text++) |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 2422 | /* Empty loop to find end of key */ ; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2423 | |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2424 | if (text != key + length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2425 | text++; |
| 2426 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2427 | text_info.compression = PNG_TEXT_COMPRESSION_NONE; |
| 2428 | text_info.key = key; |
| 2429 | text_info.lang = NULL; |
| 2430 | text_info.lang_key = NULL; |
| 2431 | text_info.itxt_length = 0; |
| 2432 | text_info.text = text; |
| 2433 | text_info.text_length = png_strlen(text); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2434 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2435 | if (png_set_text_2(png_ptr, info_ptr, &text_info, 1)) |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2436 | png_warning(png_ptr, "Insufficient memory to process text chunk"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2437 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2438 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2439 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 2440 | #ifdef PNG_READ_zTXt_SUPPORTED |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 2441 | /* 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] | 2442 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 2443 | png_handle_zTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2444 | { |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2445 | png_const_charp errmsg = NULL; |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2446 | png_bytep buffer; |
| 2447 | png_uint_32 keyword_length; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2448 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2449 | png_debug(1, "in png_handle_zTXt"); |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 2450 | |
Glenn Randers-Pehrson | e3f3c4e | 2010-02-07 18:08:50 -0600 | [diff] [blame] | 2451 | #ifdef PNG_USER_LIMITS_SUPPORTED |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 2452 | if (png_ptr->user_chunk_cache_max != 0) |
| 2453 | { |
| 2454 | if (png_ptr->user_chunk_cache_max == 1) |
| 2455 | { |
| 2456 | png_crc_finish(png_ptr, length); |
| 2457 | return; |
| 2458 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2459 | |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 2460 | if (--png_ptr->user_chunk_cache_max == 1) |
| 2461 | { |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 2462 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2463 | png_chunk_benign_error(png_ptr, "no space in chunk cache"); |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 2464 | return; |
| 2465 | } |
| 2466 | } |
| 2467 | #endif |
| 2468 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2469 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2470 | png_chunk_error(png_ptr, "missing IHDR"); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2471 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2472 | if (png_ptr->mode & PNG_HAVE_IDAT) |
| 2473 | png_ptr->mode |= PNG_AFTER_IDAT; |
| 2474 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2475 | buffer = png_read_buffer(png_ptr, length, 2/*silent*/); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2476 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2477 | if (buffer == NULL) |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 2478 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2479 | png_crc_finish(png_ptr, length); |
| 2480 | png_chunk_benign_error(png_ptr, "out of memory"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2481 | return; |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 2482 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2483 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2484 | png_crc_read(png_ptr, buffer, length); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2485 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2486 | if (png_crc_finish(png_ptr, 0)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2487 | return; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2488 | |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2489 | /* TODO: also check that the keyword contents match the spec! */ |
| 2490 | for (keyword_length = 0; |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2491 | keyword_length < length && buffer[keyword_length] != 0; |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2492 | ++keyword_length) |
| 2493 | /* Empty loop to find end of name */ ; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2494 | |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2495 | if (keyword_length > 79 || keyword_length < 1) |
| 2496 | errmsg = "bad keyword"; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2497 | |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2498 | /* zTXt must have some LZ data after the keyword, although it may expand to |
| 2499 | * zero bytes; we need a '\0' at the end of the keyword, the compression type |
| 2500 | * then the LZ data: |
| 2501 | */ |
| 2502 | else if (keyword_length + 3 > length) |
| 2503 | errmsg = "truncated"; |
| 2504 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2505 | else if (buffer[keyword_length+1] != PNG_COMPRESSION_TYPE_BASE) |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2506 | errmsg = "unknown compression type"; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2507 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2508 | else |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2509 | { |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2510 | png_alloc_size_t uncompressed_length = PNG_SIZE_MAX; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2511 | |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2512 | /* TODO: at present png_decompress_chunk imposes a single application |
| 2513 | * level memory limit, this should be split to different values for iCCP |
| 2514 | * and text chunks. |
| 2515 | */ |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2516 | if (png_decompress_chunk(png_ptr, length, keyword_length+2, |
| 2517 | &uncompressed_length, 1/*terminate*/) == Z_STREAM_END) |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2518 | { |
| 2519 | png_text text; |
| 2520 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2521 | /* It worked; png_ptr->read_buffer now looks like a tEXt chunk except |
| 2522 | * for the extra compression type byte and the fact that it isn't |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2523 | * necessarily '\0' terminated. |
| 2524 | */ |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2525 | buffer = png_ptr->read_buffer; |
| 2526 | buffer[uncompressed_length+(keyword_length+2)] = 0; |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2527 | |
| 2528 | text.compression = PNG_TEXT_COMPRESSION_zTXt; |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2529 | text.key = (png_charp)buffer; |
| 2530 | text.text = (png_charp)(buffer + keyword_length+2); |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2531 | text.text_length = uncompressed_length; |
| 2532 | text.itxt_length = 0; |
| 2533 | text.lang = NULL; |
| 2534 | text.lang_key = NULL; |
| 2535 | |
| 2536 | if (png_set_text_2(png_ptr, info_ptr, &text, 1)) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2537 | errmsg = "insufficient memory"; |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2538 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2539 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2540 | else |
| 2541 | errmsg = png_ptr->zstream.msg; |
| 2542 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2543 | |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2544 | if (errmsg != NULL) |
| 2545 | png_chunk_benign_error(png_ptr, errmsg); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2546 | } |
| 2547 | #endif |
| 2548 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 2549 | #ifdef PNG_READ_iTXt_SUPPORTED |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 2550 | /* 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] | 2551 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 2552 | png_handle_iTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2553 | { |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2554 | png_const_charp errmsg = NULL; |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2555 | png_bytep buffer; |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2556 | png_uint_32 prefix_length; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2557 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2558 | png_debug(1, "in png_handle_iTXt"); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2559 | |
Glenn Randers-Pehrson | e3f3c4e | 2010-02-07 18:08:50 -0600 | [diff] [blame] | 2560 | #ifdef PNG_USER_LIMITS_SUPPORTED |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 2561 | if (png_ptr->user_chunk_cache_max != 0) |
| 2562 | { |
| 2563 | if (png_ptr->user_chunk_cache_max == 1) |
| 2564 | { |
| 2565 | png_crc_finish(png_ptr, length); |
| 2566 | return; |
| 2567 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2568 | |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 2569 | if (--png_ptr->user_chunk_cache_max == 1) |
| 2570 | { |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 2571 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2572 | png_chunk_benign_error(png_ptr, "no space in chunk cache"); |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 2573 | return; |
| 2574 | } |
| 2575 | } |
| 2576 | #endif |
| 2577 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2578 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2579 | png_chunk_error(png_ptr, "missing IHDR"); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2580 | |
| 2581 | if (png_ptr->mode & PNG_HAVE_IDAT) |
| 2582 | png_ptr->mode |= PNG_AFTER_IDAT; |
| 2583 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2584 | buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2585 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2586 | if (buffer == NULL) |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 2587 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2588 | png_crc_finish(png_ptr, length); |
| 2589 | png_chunk_benign_error(png_ptr, "out of memory"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2590 | return; |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 2591 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2592 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2593 | png_crc_read(png_ptr, buffer, length); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2594 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2595 | if (png_crc_finish(png_ptr, 0)) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2596 | return; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2597 | |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2598 | /* First the keyword. */ |
| 2599 | for (prefix_length=0; |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2600 | prefix_length < length && buffer[prefix_length] != 0; |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2601 | ++prefix_length) |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 2602 | /* Empty loop */ ; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2603 | |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2604 | /* Perform a basic check on the keyword length here. */ |
| 2605 | if (prefix_length > 79 || prefix_length < 1) |
| 2606 | errmsg = "bad keyword"; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2607 | |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2608 | /* Expect keyword, compression flag, compression type, language, translated |
| 2609 | * keyword (both may be empty but are 0 terminated) then the text, which may |
| 2610 | * be empty. |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 2611 | */ |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2612 | else if (prefix_length + 5 > length) |
| 2613 | errmsg = "truncated"; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2614 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2615 | else if (buffer[prefix_length+1] == 0 || |
| 2616 | (buffer[prefix_length+1] == 1 && |
| 2617 | buffer[prefix_length+2] == PNG_COMPRESSION_TYPE_BASE)) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2618 | { |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2619 | int compressed = buffer[prefix_length+1] != 0; |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2620 | png_uint_32 language_offset, translated_keyword_offset; |
| 2621 | png_alloc_size_t uncompressed_length = 0; |
| 2622 | |
| 2623 | /* Now the language tag */ |
| 2624 | prefix_length += 3; |
| 2625 | language_offset = prefix_length; |
| 2626 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2627 | for (; prefix_length < length && buffer[prefix_length] != 0; |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2628 | ++prefix_length) |
| 2629 | /* Empty loop */ ; |
| 2630 | |
| 2631 | /* WARNING: the length may be invalid here, this is checked below. */ |
| 2632 | translated_keyword_offset = ++prefix_length; |
| 2633 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2634 | for (; prefix_length < length && buffer[prefix_length] != 0; |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2635 | ++prefix_length) |
| 2636 | /* Empty loop */ ; |
| 2637 | |
| 2638 | /* prefix_length should now be at the trailing '\0' of the translated |
| 2639 | * keyword, but it may already be over the end. None of this arithmetic |
| 2640 | * can overflow because chunks are at most 2^31 bytes long, but on 16-bit |
| 2641 | * systems the available allocaton may overflow. |
| 2642 | */ |
| 2643 | ++prefix_length; |
| 2644 | |
| 2645 | if (!compressed && prefix_length <= length) |
| 2646 | uncompressed_length = length - prefix_length; |
| 2647 | |
| 2648 | else if (compressed && prefix_length < length) |
| 2649 | { |
| 2650 | uncompressed_length = PNG_SIZE_MAX; |
| 2651 | |
| 2652 | /* TODO: at present png_decompress_chunk imposes a single application |
| 2653 | * level memory limit, this should be split to different values for |
| 2654 | * iCCP and text chunks. |
| 2655 | */ |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2656 | if (png_decompress_chunk(png_ptr, length, prefix_length, |
| 2657 | &uncompressed_length, 1/*terminate*/) == Z_STREAM_END) |
| 2658 | buffer = png_ptr->read_buffer; |
| 2659 | |
| 2660 | else |
| 2661 | errmsg = png_ptr->zstream.msg; |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2662 | } |
| 2663 | |
| 2664 | else |
| 2665 | errmsg = "truncated"; |
| 2666 | |
| 2667 | if (errmsg == NULL) |
| 2668 | { |
| 2669 | png_text text; |
| 2670 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2671 | buffer[uncompressed_length+prefix_length] = 0; |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2672 | |
| 2673 | if (compressed) |
| 2674 | text.compression = PNG_ITXT_COMPRESSION_NONE; |
| 2675 | |
| 2676 | else |
| 2677 | text.compression = PNG_ITXT_COMPRESSION_zTXt; |
| 2678 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2679 | text.key = (png_charp)buffer; |
| 2680 | text.lang = (png_charp)buffer + language_offset; |
| 2681 | text.lang_key = (png_charp)buffer + translated_keyword_offset; |
| 2682 | text.text = (png_charp)buffer + prefix_length; |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2683 | text.text_length = 0; |
| 2684 | text.itxt_length = uncompressed_length; |
| 2685 | |
| 2686 | if (png_set_text_2(png_ptr, info_ptr, &text, 1)) |
| 2687 | errmsg = "insufficient memory"; |
| 2688 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2689 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2690 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2691 | else |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2692 | errmsg = "bad compression info"; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2693 | |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2694 | if (errmsg != NULL) |
| 2695 | png_chunk_benign_error(png_ptr, errmsg); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2696 | } |
| 2697 | #endif |
| 2698 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2699 | /* This function is called when we haven't found a handler for a |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2700 | * chunk. If there isn't a problem with the chunk itself (ie bad |
| 2701 | * chunk name, CRC, or a critical chunk), the chunk is silently ignored |
| 2702 | * -- unless the PNG_FLAG_UNKNOWN_CHUNKS_SUPPORTED flag is on in which |
| 2703 | * case it will be saved away to be written out later. |
| 2704 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2705 | void /* PRIVATE */ |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2706 | png_handle_unknown(png_structrp png_ptr, png_inforp info_ptr, |
| 2707 | png_uint_32 length) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2708 | { |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2709 | png_uint_32 skip = 0; |
| 2710 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2711 | png_debug(1, "in png_handle_unknown"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2712 | |
Glenn Randers-Pehrson | e3f3c4e | 2010-02-07 18:08:50 -0600 | [diff] [blame] | 2713 | #ifdef PNG_USER_LIMITS_SUPPORTED |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 2714 | if (png_ptr->user_chunk_cache_max != 0) |
| 2715 | { |
| 2716 | if (png_ptr->user_chunk_cache_max == 1) |
| 2717 | { |
| 2718 | png_crc_finish(png_ptr, length); |
| 2719 | return; |
| 2720 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2721 | |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 2722 | if (--png_ptr->user_chunk_cache_max == 1) |
| 2723 | { |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 2724 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2725 | png_chunk_benign_error(png_ptr, "no space in chunk cache"); |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 2726 | return; |
| 2727 | } |
| 2728 | } |
| 2729 | #endif |
| 2730 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2731 | if (png_ptr->mode & PNG_HAVE_IDAT) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2732 | { |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 2733 | if (png_ptr->chunk_name != png_IDAT) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2734 | png_ptr->mode |= PNG_AFTER_IDAT; |
| 2735 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2736 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 2737 | if (PNG_CHUNK_CRITICAL(png_ptr->chunk_name)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2738 | { |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 2739 | #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 2740 | if (png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name) != |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2741 | PNG_HANDLE_CHUNK_ALWAYS |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 2742 | #ifdef PNG_READ_USER_CHUNKS_SUPPORTED |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2743 | && png_ptr->read_user_chunk_fn == NULL |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2744 | #endif |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2745 | ) |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 2746 | #endif |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2747 | png_chunk_error(png_ptr, "unknown critical chunk"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2748 | } |
| 2749 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 2750 | #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED |
Glenn Randers-Pehrson | 7824a70 | 2009-06-13 10:05:05 -0500 | [diff] [blame] | 2751 | if ((png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS) |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 2752 | #ifdef PNG_READ_USER_CHUNKS_SUPPORTED |
Glenn Randers-Pehrson | 7824a70 | 2009-06-13 10:05:05 -0500 | [diff] [blame] | 2753 | || (png_ptr->read_user_chunk_fn != NULL) |
| 2754 | #endif |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2755 | ) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2756 | { |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2757 | #ifdef PNG_MAX_MALLOC_64K |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 2758 | if (length > 65535) |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2759 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame^] | 2760 | png_crc_finish(png_ptr, length); |
| 2761 | png_chunk_benign_error(png_ptr, |
| 2762 | "unknown chunk too large to fit in memory"); |
| 2763 | return; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2764 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2765 | #endif |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2766 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 2767 | /* TODO: this code is very close to the unknown handling in pngpread.c, |
| 2768 | * maybe it can be put into a common utility routine? |
| 2769 | * png_struct::unknown_chunk is just used as a temporary variable, along |
| 2770 | * with the data into which the chunk is read. These can be eliminated. |
| 2771 | */ |
| 2772 | PNG_CSTRING_FROM_CHUNK(png_ptr->unknown_chunk.name, png_ptr->chunk_name); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2773 | png_ptr->unknown_chunk.size = (png_size_t)length; |
| 2774 | |
| 2775 | if (length == 0) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2776 | png_ptr->unknown_chunk.data = NULL; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2777 | |
| 2778 | else |
| 2779 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2780 | png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr, length); |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 2781 | png_crc_read(png_ptr, png_ptr->unknown_chunk.data, length); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2782 | } |
| 2783 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 2784 | #ifdef PNG_READ_USER_CHUNKS_SUPPORTED |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2785 | if (png_ptr->read_user_chunk_fn != NULL) |
| 2786 | { |
| 2787 | /* Callback to user unknown chunk handler */ |
| 2788 | int ret; |
| 2789 | |
| 2790 | ret = (*(png_ptr->read_user_chunk_fn)) |
| 2791 | (png_ptr, &png_ptr->unknown_chunk); |
| 2792 | |
| 2793 | if (ret < 0) |
| 2794 | png_chunk_error(png_ptr, "error in user chunk"); |
| 2795 | |
| 2796 | if (ret == 0) |
| 2797 | { |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 2798 | if (PNG_CHUNK_CRITICAL(png_ptr->chunk_name)) |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2799 | { |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 2800 | #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 2801 | if (png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name) != |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2802 | PNG_HANDLE_CHUNK_ALWAYS) |
Glenn Randers-Pehrson | 7824a70 | 2009-06-13 10:05:05 -0500 | [diff] [blame] | 2803 | #endif |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2804 | png_chunk_error(png_ptr, "unknown critical chunk"); |
| 2805 | } |
| 2806 | |
| 2807 | png_set_unknown_chunks(png_ptr, info_ptr, |
| 2808 | &png_ptr->unknown_chunk, 1); |
| 2809 | } |
| 2810 | } |
| 2811 | |
| 2812 | else |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2813 | #endif |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2814 | png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1); |
| 2815 | |
| 2816 | png_free(png_ptr, png_ptr->unknown_chunk.data); |
| 2817 | png_ptr->unknown_chunk.data = NULL; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2818 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2819 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2820 | else |
| 2821 | #endif |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2822 | skip = length; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2823 | |
| 2824 | png_crc_finish(png_ptr, skip); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2825 | |
Glenn Randers-Pehrson | b2aca21 | 2009-09-23 11:32:37 -0500 | [diff] [blame] | 2826 | #ifndef PNG_READ_USER_CHUNKS_SUPPORTED |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2827 | PNG_UNUSED(info_ptr) /* Quiet compiler warnings about unused info_ptr */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2828 | #endif |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2829 | } |
| 2830 | |
| 2831 | /* This function is called to verify that a chunk name is valid. |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2832 | * This function can't have the "critical chunk check" incorporated |
| 2833 | * into it, since in the future we will need to be able to call user |
| 2834 | * functions to handle unknown critical chunks after we check that |
| 2835 | * the chunk name itself is valid. |
| 2836 | */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2837 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 2838 | /* Bit hacking: the test for an invalid byte in the 4 byte chunk name is: |
| 2839 | * |
| 2840 | * ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97)) |
| 2841 | */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2842 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2843 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 2844 | png_check_chunk_name(png_structrp png_ptr, png_uint_32 chunk_name) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2845 | { |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 2846 | int i; |
| 2847 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2848 | png_debug(1, "in png_check_chunk_name"); |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 2849 | |
| 2850 | for (i=1; i<=4; ++i) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2851 | { |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 2852 | int c = chunk_name & 0xff; |
| 2853 | |
| 2854 | if (c < 65 || c > 122 || (c > 90 && c < 97)) |
| 2855 | png_chunk_error(png_ptr, "invalid chunk type"); |
| 2856 | |
| 2857 | chunk_name >>= 8; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2858 | } |
| 2859 | } |
| 2860 | |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2861 | /* Combines the row recently read in with the existing pixels in the row. This |
| 2862 | * routine takes care of alpha and transparency if requested. This routine also |
| 2863 | * handles the two methods of progressive display of interlaced images, |
| 2864 | * depending on the 'display' value; if 'display' is true then the whole row |
| 2865 | * (dp) is filled from the start by replicating the available pixels. If |
| 2866 | * 'display' is false only those pixels present in the pass are filled in. |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2867 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2868 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 2869 | png_combine_row(png_const_structrp png_ptr, png_bytep dp, int display) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2870 | { |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 2871 | unsigned int pixel_depth = png_ptr->transformed_pixel_depth; |
| 2872 | png_const_bytep sp = png_ptr->row_buf + 1; |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 2873 | png_uint_32 row_width = png_ptr->width; |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 2874 | unsigned int pass = png_ptr->pass; |
John Bowler | fb5b3ac | 2011-10-16 22:52:56 -0500 | [diff] [blame] | 2875 | png_bytep end_ptr = 0; |
| 2876 | png_byte end_byte = 0; |
| 2877 | unsigned int end_mask; |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 2878 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2879 | png_debug(1, "in png_combine_row"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2880 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 2881 | /* Added in 1.5.6: it should not be possible to enter this routine until at |
| 2882 | * least one row has been read from the PNG data and transformed. |
| 2883 | */ |
| 2884 | if (pixel_depth == 0) |
| 2885 | png_error(png_ptr, "internal row logic error"); |
| 2886 | |
| 2887 | /* Added in 1.5.4: the pixel depth should match the information returned by |
| 2888 | * any call to png_read_update_info at this point. Do not continue if we got |
John Bowler | 9994f25 | 2011-05-15 18:52:39 -0500 | [diff] [blame] | 2889 | * this wrong. |
| 2890 | */ |
| 2891 | if (png_ptr->info_rowbytes != 0 && png_ptr->info_rowbytes != |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 2892 | PNG_ROWBYTES(pixel_depth, row_width)) |
John Bowler | 9994f25 | 2011-05-15 18:52:39 -0500 | [diff] [blame] | 2893 | png_error(png_ptr, "internal row size calculation error"); |
| 2894 | |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 2895 | /* Don't expect this to ever happen: */ |
| 2896 | if (row_width == 0) |
| 2897 | png_error(png_ptr, "internal row width error"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2898 | |
John Bowler | fb5b3ac | 2011-10-16 22:52:56 -0500 | [diff] [blame] | 2899 | /* Preserve the last byte in cases where only part of it will be overwritten, |
| 2900 | * the multiply below may overflow, we don't care because ANSI-C guarantees |
| 2901 | * we get the low bits. |
| 2902 | */ |
| 2903 | end_mask = (pixel_depth * row_width) & 7; |
| 2904 | if (end_mask != 0) |
| 2905 | { |
Glenn Randers-Pehrson | ef02d56 | 2011-10-27 12:05:58 -0500 | [diff] [blame] | 2906 | /* end_ptr == NULL is a flag to say do nothing */ |
John Bowler | fb5b3ac | 2011-10-16 22:52:56 -0500 | [diff] [blame] | 2907 | end_ptr = dp + PNG_ROWBYTES(pixel_depth, row_width) - 1; |
| 2908 | end_byte = *end_ptr; |
| 2909 | # ifdef PNG_READ_PACKSWAP_SUPPORTED |
| 2910 | if (png_ptr->transformations & PNG_PACKSWAP) /* little-endian byte */ |
| 2911 | end_mask = 0xff << end_mask; |
| 2912 | |
| 2913 | else /* big-endian byte */ |
| 2914 | # endif |
| 2915 | end_mask = 0xff >> end_mask; |
| 2916 | /* end_mask is now the bits to *keep* from the destination row */ |
| 2917 | } |
| 2918 | |
Glenn Randers-Pehrson | ef02d56 | 2011-10-27 12:05:58 -0500 | [diff] [blame] | 2919 | /* For non-interlaced images this reduces to a png_memcpy(). A png_memcpy() |
| 2920 | * will also happen if interlacing isn't supported or if the application |
| 2921 | * does not call png_set_interlace_handling(). In the latter cases the |
| 2922 | * caller just gets a sequence of the unexpanded rows from each interlace |
| 2923 | * pass. |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 2924 | */ |
| 2925 | #ifdef PNG_READ_INTERLACING_SUPPORTED |
| 2926 | if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE) && |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 2927 | pass < 6 && (display == 0 || |
| 2928 | /* The following copies everything for 'display' on passes 0, 2 and 4. */ |
| 2929 | (display == 1 && (pass & 1) != 0))) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2930 | { |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 2931 | /* Narrow images may have no bits in a pass; the caller should handle |
| 2932 | * this, but this test is cheap: |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 2933 | */ |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 2934 | if (row_width <= PNG_PASS_START_COL(pass)) |
| 2935 | return; |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 2936 | |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 2937 | if (pixel_depth < 8) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2938 | { |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2939 | /* For pixel depths up to 4 bpp the 8-pixel mask can be expanded to fit |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 2940 | * into 32 bits, then a single loop over the bytes using the four byte |
Glenn Randers-Pehrson | ef02d56 | 2011-10-27 12:05:58 -0500 | [diff] [blame] | 2941 | * values in the 32-bit mask can be used. For the 'display' option the |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 2942 | * expanded mask may also not require any masking within a byte. To |
| 2943 | * make this work the PACKSWAP option must be taken into account - it |
| 2944 | * simply requires the pixels to be reversed in each byte. |
| 2945 | * |
| 2946 | * The 'regular' case requires a mask for each of the first 6 passes, |
| 2947 | * the 'display' case does a copy for the even passes in the range |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2948 | * 0..6. This has already been handled in the test above. |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 2949 | * |
| 2950 | * The masks are arranged as four bytes with the first byte to use in |
| 2951 | * the lowest bits (little-endian) regardless of the order (PACKSWAP or |
| 2952 | * not) of the pixels in each byte. |
| 2953 | * |
| 2954 | * NOTE: the whole of this logic depends on the caller of this function |
| 2955 | * only calling it on rows appropriate to the pass. This function only |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2956 | * understands the 'x' logic; the 'y' logic is handled by the caller. |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 2957 | * |
| 2958 | * The following defines allow generation of compile time constant bit |
| 2959 | * masks for each pixel depth and each possibility of swapped or not |
Glenn Randers-Pehrson | ef02d56 | 2011-10-27 12:05:58 -0500 | [diff] [blame] | 2960 | * swapped bytes. Pass 'p' is in the range 0..6; 'x', a pixel index, |
| 2961 | * is in the range 0..7; and the result is 1 if the pixel is to be |
| 2962 | * copied in the pass, 0 if not. 'S' is for the sparkle method, 'B' |
| 2963 | * for the block method. |
| 2964 | * |
John Bowler | 92ef313 | 2011-10-27 19:36:08 -0500 | [diff] [blame] | 2965 | * With some compilers a compile time expression of the general form: |
Glenn Randers-Pehrson | cb75699 | 2011-10-27 16:59:03 -0500 | [diff] [blame] | 2966 | * |
John Bowler | 92ef313 | 2011-10-27 19:36:08 -0500 | [diff] [blame] | 2967 | * (shift >= 32) ? (a >> (shift-32)) : (b >> shift) |
| 2968 | * |
| 2969 | * Produces warnings with values of 'shift' in the range 33 to 63 |
Glenn Randers-Pehrson | 1876366 | 2011-10-27 22:09:22 -0500 | [diff] [blame] | 2970 | * because the right hand side of the ?: expression is evaluated by |
John Bowler | 92ef313 | 2011-10-27 19:36:08 -0500 | [diff] [blame] | 2971 | * the compiler even though it isn't used. Microsoft Visual C (various |
| 2972 | * versions) and the Intel C compiler are known to do this. To avoid |
| 2973 | * this the following macros are used in 1.5.6. This is a temporary |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2974 | * solution to avoid destabilizing the code during the release process. |
Glenn Randers-Pehrson | cb75699 | 2011-10-27 16:59:03 -0500 | [diff] [blame] | 2975 | */ |
John Bowler | 92ef313 | 2011-10-27 19:36:08 -0500 | [diff] [blame] | 2976 | # if PNG_USE_COMPILE_TIME_MASKS |
Glenn Randers-Pehrson | cb75699 | 2011-10-27 16:59:03 -0500 | [diff] [blame] | 2977 | # define PNG_LSR(x,s) ((x)>>((s) & 0x1f)) |
| 2978 | # define PNG_LSL(x,s) ((x)<<((s) & 0x1f)) |
Glenn Randers-Pehrson | ef02d56 | 2011-10-27 12:05:58 -0500 | [diff] [blame] | 2979 | # else |
Glenn Randers-Pehrson | cb75699 | 2011-10-27 16:59:03 -0500 | [diff] [blame] | 2980 | # define PNG_LSR(x,s) ((x)>>(s)) |
| 2981 | # define PNG_LSL(x,s) ((x)<<(s)) |
Glenn Randers-Pehrson | ef02d56 | 2011-10-27 12:05:58 -0500 | [diff] [blame] | 2982 | # endif |
Glenn Randers-Pehrson | cb75699 | 2011-10-27 16:59:03 -0500 | [diff] [blame] | 2983 | # define S_COPY(p,x) (((p)<4 ? PNG_LSR(0x80088822,(3-(p))*8+(7-(x))) :\ |
| 2984 | PNG_LSR(0xaa55ff00,(7-(p))*8+(7-(x)))) & 1) |
| 2985 | # define B_COPY(p,x) (((p)<4 ? PNG_LSR(0xff0fff33,(3-(p))*8+(7-(x))) :\ |
| 2986 | PNG_LSR(0xff55ff00,(7-(p))*8+(7-(x)))) & 1) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2987 | |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 2988 | /* Return a mask for pass 'p' pixel 'x' at depth 'd'. The mask is |
| 2989 | * little endian - the first pixel is at bit 0 - however the extra |
| 2990 | * parameter 's' can be set to cause the mask position to be swapped |
| 2991 | * within each byte, to match the PNG format. This is done by XOR of |
| 2992 | * the shift with 7, 6 or 4 for bit depths 1, 2 and 4. |
| 2993 | */ |
Glenn Randers-Pehrson | cb75699 | 2011-10-27 16:59:03 -0500 | [diff] [blame] | 2994 | # define PIXEL_MASK(p,x,d,s) \ |
| 2995 | (PNG_LSL(((PNG_LSL(1U,(d)))-1),(((x)*(d))^((s)?8-(d):0)))) |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 2996 | |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 2997 | /* Hence generate the appropriate 'block' or 'sparkle' pixel copy mask. |
| 2998 | */ |
| 2999 | # define S_MASKx(p,x,d,s) (S_COPY(p,x)?PIXEL_MASK(p,x,d,s):0) |
| 3000 | # define B_MASKx(p,x,d,s) (B_COPY(p,x)?PIXEL_MASK(p,x,d,s):0) |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 3001 | |
Glenn Randers-Pehrson | ef02d56 | 2011-10-27 12:05:58 -0500 | [diff] [blame] | 3002 | /* Combine 8 of these to get the full mask. For the 1-bpp and 2-bpp |
| 3003 | * cases the result needs replicating, for the 4-bpp case the above |
| 3004 | * generates a full 32 bits. |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3005 | */ |
| 3006 | # define MASK_EXPAND(m,d) ((m)*((d)==1?0x01010101:((d)==2?0x00010001:1))) |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3007 | |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3008 | # define S_MASK(p,d,s) MASK_EXPAND(S_MASKx(p,0,d,s) + S_MASKx(p,1,d,s) +\ |
| 3009 | S_MASKx(p,2,d,s) + S_MASKx(p,3,d,s) + S_MASKx(p,4,d,s) +\ |
| 3010 | S_MASKx(p,5,d,s) + S_MASKx(p,6,d,s) + S_MASKx(p,7,d,s), d) |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3011 | |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3012 | # define B_MASK(p,d,s) MASK_EXPAND(B_MASKx(p,0,d,s) + B_MASKx(p,1,d,s) +\ |
| 3013 | B_MASKx(p,2,d,s) + B_MASKx(p,3,d,s) + B_MASKx(p,4,d,s) +\ |
| 3014 | B_MASKx(p,5,d,s) + B_MASKx(p,6,d,s) + B_MASKx(p,7,d,s), d) |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 3015 | |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3016 | #if PNG_USE_COMPILE_TIME_MASKS |
| 3017 | /* Utility macros to construct all the masks for a depth/swap |
| 3018 | * combination. The 's' parameter says whether the format is PNG |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3019 | * (big endian bytes) or not. Only the three odd-numbered passes are |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3020 | * required for the display/block algorithm. |
| 3021 | */ |
| 3022 | # define S_MASKS(d,s) { S_MASK(0,d,s), S_MASK(1,d,s), S_MASK(2,d,s),\ |
| 3023 | S_MASK(3,d,s), S_MASK(4,d,s), S_MASK(5,d,s) } |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 3024 | |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3025 | # define B_MASKS(d,s) { B_MASK(1,d,s), S_MASK(3,d,s), S_MASK(5,d,s) } |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 3026 | |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3027 | # define DEPTH_INDEX(d) ((d)==1?0:((d)==2?1:2)) |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 3028 | |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3029 | /* Hence the pre-compiled masks indexed by PACKSWAP (or not), depth and |
| 3030 | * then pass: |
| 3031 | */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3032 | static PNG_CONST png_uint_32 row_mask[2/*PACKSWAP*/][3/*depth*/][6] = |
| 3033 | { |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3034 | /* Little-endian byte masks for PACKSWAP */ |
| 3035 | { S_MASKS(1,0), S_MASKS(2,0), S_MASKS(4,0) }, |
| 3036 | /* Normal (big-endian byte) masks - PNG format */ |
| 3037 | { S_MASKS(1,1), S_MASKS(2,1), S_MASKS(4,1) } |
| 3038 | }; |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 3039 | |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3040 | /* display_mask has only three entries for the odd passes, so index by |
| 3041 | * pass>>1. |
| 3042 | */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3043 | static PNG_CONST png_uint_32 display_mask[2][3][3] = |
| 3044 | { |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3045 | /* Little-endian byte masks for PACKSWAP */ |
| 3046 | { B_MASKS(1,0), B_MASKS(2,0), B_MASKS(4,0) }, |
| 3047 | /* Normal (big-endian byte) masks - PNG format */ |
| 3048 | { B_MASKS(1,1), B_MASKS(2,1), B_MASKS(4,1) } |
| 3049 | }; |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 3050 | |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3051 | # define MASK(pass,depth,display,png)\ |
| 3052 | ((display)?display_mask[png][DEPTH_INDEX(depth)][pass>>1]:\ |
| 3053 | row_mask[png][DEPTH_INDEX(depth)][pass]) |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 3054 | |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3055 | #else /* !PNG_USE_COMPILE_TIME_MASKS */ |
| 3056 | /* This is the runtime alternative: it seems unlikely that this will |
| 3057 | * ever be either smaller or faster than the compile time approach. |
| 3058 | */ |
| 3059 | # define MASK(pass,depth,display,png)\ |
| 3060 | ((display)?B_MASK(pass,depth,png):S_MASK(pass,depth,png)) |
| 3061 | #endif /* !PNG_USE_COMPILE_TIME_MASKS */ |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 3062 | |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3063 | /* Use the appropriate mask to copy the required bits. In some cases |
| 3064 | * the byte mask will be 0 or 0xff, optimize these cases. row_width is |
| 3065 | * the number of pixels, but the code copies bytes, so it is necessary |
| 3066 | * to special case the end. |
| 3067 | */ |
| 3068 | png_uint_32 pixels_per_byte = 8 / pixel_depth; |
| 3069 | png_uint_32 mask; |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 3070 | |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3071 | # ifdef PNG_READ_PACKSWAP_SUPPORTED |
| 3072 | if (png_ptr->transformations & PNG_PACKSWAP) |
| 3073 | mask = MASK(pass, pixel_depth, display, 0); |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 3074 | |
| 3075 | else |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3076 | # endif |
| 3077 | mask = MASK(pass, pixel_depth, display, 1); |
| 3078 | |
| 3079 | for (;;) |
| 3080 | { |
| 3081 | png_uint_32 m; |
| 3082 | |
| 3083 | /* It doesn't matter in the following if png_uint_32 has more than |
Glenn Randers-Pehrson | ef02d56 | 2011-10-27 12:05:58 -0500 | [diff] [blame] | 3084 | * 32 bits because the high bits always match those in m<<24; it is, |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3085 | * however, essential to use OR here, not +, because of this. |
| 3086 | */ |
| 3087 | m = mask; |
| 3088 | mask = (m >> 8) | (m << 24); /* rotate right to good compilers */ |
| 3089 | m &= 0xff; |
| 3090 | |
| 3091 | if (m != 0) /* something to copy */ |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 3092 | { |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3093 | if (m != 0xff) |
| 3094 | *dp = (png_byte)((*dp & ~m) | (*sp & m)); |
| 3095 | else |
| 3096 | *dp = *sp; |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 3097 | } |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3098 | |
| 3099 | /* NOTE: this may overwrite the last byte with garbage if the image |
Glenn Randers-Pehrson | ef02d56 | 2011-10-27 12:05:58 -0500 | [diff] [blame] | 3100 | * is not an exact number of bytes wide; libpng has always done |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3101 | * this. |
| 3102 | */ |
| 3103 | if (row_width <= pixels_per_byte) |
John Bowler | fb5b3ac | 2011-10-16 22:52:56 -0500 | [diff] [blame] | 3104 | break; /* May need to restore part of the last byte */ |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3105 | |
| 3106 | row_width -= pixels_per_byte; |
| 3107 | ++dp; |
| 3108 | ++sp; |
| 3109 | } |
| 3110 | } |
| 3111 | |
| 3112 | else /* pixel_depth >= 8 */ |
| 3113 | { |
| 3114 | unsigned int bytes_to_copy, bytes_to_jump; |
| 3115 | |
| 3116 | /* Validate the depth - it must be a multiple of 8 */ |
| 3117 | if (pixel_depth & 7) |
| 3118 | png_error(png_ptr, "invalid user transform pixel depth"); |
| 3119 | |
| 3120 | pixel_depth >>= 3; /* now in bytes */ |
| 3121 | row_width *= pixel_depth; |
| 3122 | |
| 3123 | /* Regardless of pass number the Adam 7 interlace always results in a |
| 3124 | * fixed number of pixels to copy then to skip. There may be a |
| 3125 | * different number of pixels to skip at the start though. |
| 3126 | */ |
| 3127 | { |
| 3128 | unsigned int offset = PNG_PASS_START_COL(pass) * pixel_depth; |
| 3129 | |
| 3130 | row_width -= offset; |
| 3131 | dp += offset; |
| 3132 | sp += offset; |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 3133 | } |
| 3134 | |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3135 | /* Work out the bytes to copy. */ |
| 3136 | if (display) |
| 3137 | { |
| 3138 | /* When doing the 'block' algorithm the pixel in the pass gets |
| 3139 | * replicated to adjacent pixels. This is why the even (0,2,4,6) |
| 3140 | * passes are skipped above - the entire expanded row is copied. |
| 3141 | */ |
| 3142 | bytes_to_copy = (1<<((6-pass)>>1)) * pixel_depth; |
| 3143 | |
| 3144 | /* But don't allow this number to exceed the actual row width. */ |
| 3145 | if (bytes_to_copy > row_width) |
| 3146 | bytes_to_copy = row_width; |
| 3147 | } |
| 3148 | |
| 3149 | else /* normal row; Adam7 only ever gives us one pixel to copy. */ |
| 3150 | bytes_to_copy = pixel_depth; |
| 3151 | |
| 3152 | /* In Adam7 there is a constant offset between where the pixels go. */ |
| 3153 | bytes_to_jump = PNG_PASS_COL_OFFSET(pass) * pixel_depth; |
| 3154 | |
| 3155 | /* And simply copy these bytes. Some optimization is possible here, |
Glenn Randers-Pehrson | ef02d56 | 2011-10-27 12:05:58 -0500 | [diff] [blame] | 3156 | * depending on the value of 'bytes_to_copy'. Special case the low |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3157 | * byte counts, which we know to be frequent. |
John Bowler | fb5b3ac | 2011-10-16 22:52:56 -0500 | [diff] [blame] | 3158 | * |
| 3159 | * Notice that these cases all 'return' rather than 'break' - this |
| 3160 | * avoids an unnecessary test on whether to restore the last byte |
| 3161 | * below. |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3162 | */ |
| 3163 | switch (bytes_to_copy) |
| 3164 | { |
| 3165 | case 1: |
| 3166 | for (;;) |
| 3167 | { |
| 3168 | *dp = *sp; |
| 3169 | |
| 3170 | if (row_width <= bytes_to_jump) |
| 3171 | return; |
| 3172 | |
| 3173 | dp += bytes_to_jump; |
| 3174 | sp += bytes_to_jump; |
| 3175 | row_width -= bytes_to_jump; |
| 3176 | } |
| 3177 | |
| 3178 | case 2: |
Glenn Randers-Pehrson | ef02d56 | 2011-10-27 12:05:58 -0500 | [diff] [blame] | 3179 | /* There is a possibility of a partial copy at the end here; this |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3180 | * slows the code down somewhat. |
| 3181 | */ |
| 3182 | do |
| 3183 | { |
| 3184 | dp[0] = sp[0], dp[1] = sp[1]; |
| 3185 | |
| 3186 | if (row_width <= bytes_to_jump) |
| 3187 | return; |
| 3188 | |
| 3189 | sp += bytes_to_jump; |
| 3190 | dp += bytes_to_jump; |
| 3191 | row_width -= bytes_to_jump; |
| 3192 | } |
| 3193 | while (row_width > 1); |
| 3194 | |
| 3195 | /* And there can only be one byte left at this point: */ |
| 3196 | *dp = *sp; |
| 3197 | return; |
| 3198 | |
| 3199 | case 3: |
| 3200 | /* This can only be the RGB case, so each copy is exactly one |
| 3201 | * pixel and it is not necessary to check for a partial copy. |
| 3202 | */ |
| 3203 | for(;;) |
| 3204 | { |
| 3205 | dp[0] = sp[0], dp[1] = sp[1], dp[2] = sp[2]; |
| 3206 | |
| 3207 | if (row_width <= bytes_to_jump) |
| 3208 | return; |
| 3209 | |
| 3210 | sp += bytes_to_jump; |
| 3211 | dp += bytes_to_jump; |
| 3212 | row_width -= bytes_to_jump; |
| 3213 | } |
| 3214 | |
| 3215 | default: |
| 3216 | #if PNG_ALIGN_TYPE != PNG_ALIGN_NONE |
Glenn Randers-Pehrson | ef02d56 | 2011-10-27 12:05:58 -0500 | [diff] [blame] | 3217 | /* Check for double byte alignment and, if possible, use a |
| 3218 | * 16-bit copy. Don't attempt this for narrow images - ones that |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3219 | * are less than an interlace panel wide. Don't attempt it for |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3220 | * wide bytes_to_copy either - use the png_memcpy there. |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3221 | */ |
Glenn Randers-Pehrson | ef02d56 | 2011-10-27 12:05:58 -0500 | [diff] [blame] | 3222 | if (bytes_to_copy < 16 /*else use png_memcpy*/ && |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3223 | png_isaligned(dp, png_uint_16) && |
| 3224 | png_isaligned(sp, png_uint_16) && |
| 3225 | bytes_to_copy % sizeof (png_uint_16) == 0 && |
| 3226 | bytes_to_jump % sizeof (png_uint_16) == 0) |
| 3227 | { |
| 3228 | /* Everything is aligned for png_uint_16 copies, but try for |
| 3229 | * png_uint_32 first. |
| 3230 | */ |
| 3231 | if (png_isaligned(dp, png_uint_32) && |
| 3232 | png_isaligned(sp, png_uint_32) && |
| 3233 | bytes_to_copy % sizeof (png_uint_32) == 0 && |
| 3234 | bytes_to_jump % sizeof (png_uint_32) == 0) |
| 3235 | { |
| 3236 | png_uint_32p dp32 = (png_uint_32p)dp; |
| 3237 | png_const_uint_32p sp32 = (png_const_uint_32p)sp; |
| 3238 | unsigned int skip = (bytes_to_jump-bytes_to_copy) / |
| 3239 | sizeof (png_uint_32); |
| 3240 | |
| 3241 | do |
| 3242 | { |
| 3243 | size_t c = bytes_to_copy; |
| 3244 | do |
| 3245 | { |
| 3246 | *dp32++ = *sp32++; |
| 3247 | c -= sizeof (png_uint_32); |
| 3248 | } |
| 3249 | while (c > 0); |
| 3250 | |
| 3251 | if (row_width <= bytes_to_jump) |
| 3252 | return; |
| 3253 | |
| 3254 | dp32 += skip; |
| 3255 | sp32 += skip; |
| 3256 | row_width -= bytes_to_jump; |
| 3257 | } |
| 3258 | while (bytes_to_copy <= row_width); |
| 3259 | |
| 3260 | /* Get to here when the row_width truncates the final copy. |
| 3261 | * There will be 1-3 bytes left to copy, so don't try the |
Glenn Randers-Pehrson | ef02d56 | 2011-10-27 12:05:58 -0500 | [diff] [blame] | 3262 | * 16-bit loop below. |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3263 | */ |
| 3264 | dp = (png_bytep)dp32; |
| 3265 | sp = (png_const_bytep)sp32; |
| 3266 | do |
| 3267 | *dp++ = *sp++; |
| 3268 | while (--row_width > 0); |
| 3269 | return; |
| 3270 | } |
| 3271 | |
Glenn Randers-Pehrson | ef02d56 | 2011-10-27 12:05:58 -0500 | [diff] [blame] | 3272 | /* Else do it in 16-bit quantities, but only if the size is |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3273 | * not too large. |
| 3274 | */ |
| 3275 | else |
| 3276 | { |
| 3277 | png_uint_16p dp16 = (png_uint_16p)dp; |
| 3278 | png_const_uint_16p sp16 = (png_const_uint_16p)sp; |
| 3279 | unsigned int skip = (bytes_to_jump-bytes_to_copy) / |
| 3280 | sizeof (png_uint_16); |
| 3281 | |
| 3282 | do |
| 3283 | { |
| 3284 | size_t c = bytes_to_copy; |
| 3285 | do |
| 3286 | { |
| 3287 | *dp16++ = *sp16++; |
| 3288 | c -= sizeof (png_uint_16); |
| 3289 | } |
| 3290 | while (c > 0); |
| 3291 | |
| 3292 | if (row_width <= bytes_to_jump) |
| 3293 | return; |
| 3294 | |
| 3295 | dp16 += skip; |
| 3296 | sp16 += skip; |
| 3297 | row_width -= bytes_to_jump; |
| 3298 | } |
| 3299 | while (bytes_to_copy <= row_width); |
| 3300 | |
Glenn Randers-Pehrson | ef02d56 | 2011-10-27 12:05:58 -0500 | [diff] [blame] | 3301 | /* End of row - 1 byte left, bytes_to_copy > row_width: */ |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3302 | dp = (png_bytep)dp16; |
| 3303 | sp = (png_const_bytep)sp16; |
| 3304 | do |
| 3305 | *dp++ = *sp++; |
| 3306 | while (--row_width > 0); |
| 3307 | return; |
| 3308 | } |
| 3309 | } |
| 3310 | #endif /* PNG_ALIGN_ code */ |
| 3311 | |
Glenn Randers-Pehrson | ef02d56 | 2011-10-27 12:05:58 -0500 | [diff] [blame] | 3312 | /* The true default - use a png_memcpy: */ |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3313 | for (;;) |
| 3314 | { |
| 3315 | png_memcpy(dp, sp, bytes_to_copy); |
| 3316 | |
| 3317 | if (row_width <= bytes_to_jump) |
| 3318 | return; |
| 3319 | |
| 3320 | sp += bytes_to_jump; |
| 3321 | dp += bytes_to_jump; |
| 3322 | row_width -= bytes_to_jump; |
| 3323 | if (bytes_to_copy > row_width) |
| 3324 | bytes_to_copy = row_width; |
| 3325 | } |
| 3326 | } |
John Bowler | fb5b3ac | 2011-10-16 22:52:56 -0500 | [diff] [blame] | 3327 | |
| 3328 | /* NOT REACHED*/ |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3329 | } /* pixel_depth >= 8 */ |
| 3330 | |
John Bowler | fb5b3ac | 2011-10-16 22:52:56 -0500 | [diff] [blame] | 3331 | /* Here if pixel_depth < 8 to check 'end_ptr' below. */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3332 | } |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3333 | else |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 3334 | #endif |
| 3335 | |
Glenn Randers-Pehrson | ef02d56 | 2011-10-27 12:05:58 -0500 | [diff] [blame] | 3336 | /* If here then the switch above wasn't used so just png_memcpy the whole row |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3337 | * from the temporary row buffer (notice that this overwrites the end of the |
| 3338 | * destination row if it is a partial byte.) |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 3339 | */ |
| 3340 | png_memcpy(dp, sp, PNG_ROWBYTES(pixel_depth, row_width)); |
John Bowler | fb5b3ac | 2011-10-16 22:52:56 -0500 | [diff] [blame] | 3341 | |
| 3342 | /* Restore the overwritten bits from the last byte if necessary. */ |
| 3343 | if (end_ptr != NULL) |
| 3344 | *end_ptr = (png_byte)((end_byte & end_mask) | (*end_ptr & ~end_mask)); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3345 | } |
| 3346 | |
Glenn Randers-Pehrson | 231e687 | 2001-01-12 15:13:06 -0600 | [diff] [blame] | 3347 | #ifdef PNG_READ_INTERLACING_SUPPORTED |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 3348 | void /* PRIVATE */ |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 3349 | png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass, |
| 3350 | png_uint_32 transformations /* Because these may affect the byte layout */) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3351 | { |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 3352 | /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ |
| 3353 | /* Offset to next interlace block */ |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 3354 | static 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] | 3355 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 3356 | png_debug(1, "in png_do_read_interlace"); |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3357 | if (row != NULL && row_info != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3358 | { |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3359 | png_uint_32 final_width; |
| 3360 | |
| 3361 | final_width = row_info->width * png_pass_inc[pass]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3362 | |
| 3363 | switch (row_info->pixel_depth) |
| 3364 | { |
| 3365 | case 1: |
| 3366 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 3367 | png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3); |
| 3368 | png_bytep dp = row + (png_size_t)((final_width - 1) >> 3); |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3369 | int sshift, dshift; |
| 3370 | int s_start, s_end, s_inc; |
| 3371 | int jstop = png_pass_inc[pass]; |
| 3372 | png_byte v; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3373 | png_uint_32 i; |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3374 | int j; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3375 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 3376 | #ifdef PNG_READ_PACKSWAP_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3377 | if (transformations & PNG_PACKSWAP) |
| 3378 | { |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3379 | sshift = (int)((row_info->width + 7) & 0x07); |
| 3380 | dshift = (int)((final_width + 7) & 0x07); |
| 3381 | s_start = 7; |
| 3382 | s_end = 0; |
| 3383 | s_inc = -1; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3384 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3385 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3386 | else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3387 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3388 | { |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3389 | sshift = 7 - (int)((row_info->width + 7) & 0x07); |
| 3390 | dshift = 7 - (int)((final_width + 7) & 0x07); |
| 3391 | s_start = 0; |
| 3392 | s_end = 7; |
| 3393 | s_inc = 1; |
| 3394 | } |
Glenn Randers-Pehrson | 5dd2b8e | 2004-11-24 07:50:16 -0600 | [diff] [blame] | 3395 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3396 | for (i = 0; i < row_info->width; i++) |
| 3397 | { |
| 3398 | v = (png_byte)((*sp >> sshift) & 0x01); |
| 3399 | for (j = 0; j < jstop; j++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3400 | { |
John Bowler | 8fb6c6a | 2012-01-25 07:47:44 -0600 | [diff] [blame] | 3401 | unsigned int tmp = *dp & (0x7f7f >> (7 - dshift)); |
| 3402 | tmp |= v << dshift; |
| 3403 | *dp = (png_byte)(tmp & 0xff); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3404 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3405 | if (dshift == s_end) |
| 3406 | { |
| 3407 | dshift = s_start; |
| 3408 | dp--; |
| 3409 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3410 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3411 | else |
| 3412 | dshift += s_inc; |
| 3413 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3414 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3415 | if (sshift == s_end) |
| 3416 | { |
| 3417 | sshift = s_start; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3418 | sp--; |
| 3419 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3420 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3421 | else |
| 3422 | sshift += s_inc; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3423 | } |
| 3424 | break; |
| 3425 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3426 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3427 | case 2: |
| 3428 | { |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3429 | png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2); |
| 3430 | png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2); |
| 3431 | int sshift, dshift; |
| 3432 | int s_start, s_end, s_inc; |
| 3433 | int jstop = png_pass_inc[pass]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3434 | png_uint_32 i; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3435 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 3436 | #ifdef PNG_READ_PACKSWAP_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3437 | if (transformations & PNG_PACKSWAP) |
| 3438 | { |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3439 | sshift = (int)(((row_info->width + 3) & 0x03) << 1); |
| 3440 | dshift = (int)(((final_width + 3) & 0x03) << 1); |
| 3441 | s_start = 6; |
| 3442 | s_end = 0; |
| 3443 | s_inc = -2; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3444 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3445 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3446 | else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3447 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3448 | { |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3449 | sshift = (int)((3 - ((row_info->width + 3) & 0x03)) << 1); |
| 3450 | dshift = (int)((3 - ((final_width + 3) & 0x03)) << 1); |
| 3451 | s_start = 0; |
| 3452 | s_end = 6; |
| 3453 | s_inc = 2; |
| 3454 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3455 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3456 | for (i = 0; i < row_info->width; i++) |
| 3457 | { |
| 3458 | png_byte v; |
| 3459 | int j; |
| 3460 | |
| 3461 | v = (png_byte)((*sp >> sshift) & 0x03); |
| 3462 | for (j = 0; j < jstop; j++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3463 | { |
John Bowler | 8fb6c6a | 2012-01-25 07:47:44 -0600 | [diff] [blame] | 3464 | unsigned int tmp = *dp & (0x3f3f >> (6 - dshift)); |
| 3465 | tmp |= v << dshift; |
| 3466 | *dp = (png_byte)(tmp & 0xff); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3467 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3468 | if (dshift == s_end) |
| 3469 | { |
| 3470 | dshift = s_start; |
| 3471 | dp--; |
| 3472 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3473 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3474 | else |
| 3475 | dshift += s_inc; |
| 3476 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3477 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3478 | if (sshift == s_end) |
| 3479 | { |
| 3480 | sshift = s_start; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3481 | sp--; |
| 3482 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3483 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3484 | else |
| 3485 | sshift += s_inc; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3486 | } |
| 3487 | break; |
| 3488 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3489 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3490 | case 4: |
| 3491 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 3492 | png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1); |
| 3493 | png_bytep dp = row + (png_size_t)((final_width - 1) >> 1); |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3494 | int sshift, dshift; |
| 3495 | int s_start, s_end, s_inc; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3496 | png_uint_32 i; |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3497 | int jstop = png_pass_inc[pass]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3498 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 3499 | #ifdef PNG_READ_PACKSWAP_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3500 | if (transformations & PNG_PACKSWAP) |
| 3501 | { |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3502 | sshift = (int)(((row_info->width + 1) & 0x01) << 2); |
| 3503 | dshift = (int)(((final_width + 1) & 0x01) << 2); |
| 3504 | s_start = 4; |
| 3505 | s_end = 0; |
| 3506 | s_inc = -4; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3507 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3508 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3509 | else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3510 | #endif |
| 3511 | { |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3512 | sshift = (int)((1 - ((row_info->width + 1) & 0x01)) << 2); |
| 3513 | dshift = (int)((1 - ((final_width + 1) & 0x01)) << 2); |
| 3514 | s_start = 0; |
| 3515 | s_end = 4; |
| 3516 | s_inc = 4; |
| 3517 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3518 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3519 | for (i = 0; i < row_info->width; i++) |
| 3520 | { |
Glenn Randers-Pehrson | 8db1998 | 2011-10-27 16:17:24 -0500 | [diff] [blame] | 3521 | png_byte v = (png_byte)((*sp >> sshift) & 0x0f); |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3522 | int j; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3523 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3524 | for (j = 0; j < jstop; j++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3525 | { |
John Bowler | 8fb6c6a | 2012-01-25 07:47:44 -0600 | [diff] [blame] | 3526 | unsigned int tmp = *dp & (0xf0f >> (4 - dshift)); |
| 3527 | tmp |= v << dshift; |
| 3528 | *dp = (png_byte)(tmp & 0xff); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3529 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3530 | if (dshift == s_end) |
| 3531 | { |
| 3532 | dshift = s_start; |
| 3533 | dp--; |
| 3534 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3535 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3536 | else |
| 3537 | dshift += s_inc; |
| 3538 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3539 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3540 | if (sshift == s_end) |
| 3541 | { |
| 3542 | sshift = s_start; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3543 | sp--; |
| 3544 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3545 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3546 | else |
| 3547 | sshift += s_inc; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3548 | } |
| 3549 | break; |
| 3550 | } |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 3551 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3552 | default: |
| 3553 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 3554 | png_size_t pixel_bytes = (row_info->pixel_depth >> 3); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3555 | |
Glenn Randers-Pehrson | e3f3c4e | 2010-02-07 18:08:50 -0600 | [diff] [blame] | 3556 | png_bytep sp = row + (png_size_t)(row_info->width - 1) |
| 3557 | * pixel_bytes; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3558 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3559 | png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3560 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3561 | int jstop = png_pass_inc[pass]; |
| 3562 | png_uint_32 i; |
| 3563 | |
| 3564 | for (i = 0; i < row_info->width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3565 | { |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3566 | png_byte v[8]; |
| 3567 | int j; |
| 3568 | |
| 3569 | png_memcpy(v, sp, pixel_bytes); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3570 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3571 | for (j = 0; j < jstop; j++) |
| 3572 | { |
| 3573 | png_memcpy(dp, v, pixel_bytes); |
| 3574 | dp -= pixel_bytes; |
| 3575 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3576 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3577 | sp -= pixel_bytes; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3578 | } |
| 3579 | break; |
| 3580 | } |
| 3581 | } |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 3582 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3583 | row_info->width = final_width; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 3584 | row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3585 | } |
Glenn Randers-Pehrson | b2aca21 | 2009-09-23 11:32:37 -0500 | [diff] [blame] | 3586 | #ifndef PNG_READ_PACKSWAP_SUPPORTED |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3587 | PNG_UNUSED(transformations) /* Silence compiler warning */ |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 3588 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3589 | } |
Glenn Randers-Pehrson | 231e687 | 2001-01-12 15:13:06 -0600 | [diff] [blame] | 3590 | #endif /* PNG_READ_INTERLACING_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3591 | |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3592 | static void |
| 3593 | png_read_filter_row_sub(png_row_infop row_info, png_bytep row, |
| 3594 | png_const_bytep prev_row) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3595 | { |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3596 | png_size_t i; |
| 3597 | png_size_t istop = row_info->rowbytes; |
| 3598 | unsigned int bpp = (row_info->pixel_depth + 7) >> 3; |
| 3599 | png_bytep rp = row + bpp; |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3600 | |
| 3601 | PNG_UNUSED(prev_row) |
| 3602 | |
| 3603 | for (i = bpp; i < istop; i++) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3604 | { |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3605 | *rp = (png_byte)(((int)(*rp) + (int)(*(rp-bpp))) & 0xff); |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3606 | rp++; |
| 3607 | } |
| 3608 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3609 | |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3610 | static void |
| 3611 | png_read_filter_row_up(png_row_infop row_info, png_bytep row, |
| 3612 | png_const_bytep prev_row) |
| 3613 | { |
| 3614 | png_size_t i; |
| 3615 | png_size_t istop = row_info->rowbytes; |
| 3616 | png_bytep rp = row; |
| 3617 | png_const_bytep pp = prev_row; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3618 | |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3619 | for (i = 0; i < istop; i++) |
| 3620 | { |
| 3621 | *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff); |
| 3622 | rp++; |
| 3623 | } |
| 3624 | } |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3625 | |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3626 | static void |
| 3627 | png_read_filter_row_avg(png_row_infop row_info, png_bytep row, |
| 3628 | png_const_bytep prev_row) |
| 3629 | { |
| 3630 | png_size_t i; |
| 3631 | png_bytep rp = row; |
| 3632 | png_const_bytep pp = prev_row; |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3633 | unsigned int bpp = (row_info->pixel_depth + 7) >> 3; |
| 3634 | png_size_t istop = row_info->rowbytes - bpp; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3635 | |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3636 | for (i = 0; i < bpp; i++) |
| 3637 | { |
| 3638 | *rp = (png_byte)(((int)(*rp) + |
| 3639 | ((int)(*pp++) / 2 )) & 0xff); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3640 | |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3641 | rp++; |
| 3642 | } |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 3643 | |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3644 | for (i = 0; i < istop; i++) |
| 3645 | { |
| 3646 | *rp = (png_byte)(((int)(*rp) + |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3647 | (int)(*pp++ + *(rp-bpp)) / 2 ) & 0xff); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3648 | |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3649 | rp++; |
| 3650 | } |
| 3651 | } |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3652 | |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3653 | static void |
John Bowler | fcc0263 | 2011-11-03 18:31:00 -0500 | [diff] [blame] | 3654 | png_read_filter_row_paeth_1byte_pixel(png_row_infop row_info, png_bytep row, |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3655 | png_const_bytep prev_row) |
| 3656 | { |
John Bowler | fcc0263 | 2011-11-03 18:31:00 -0500 | [diff] [blame] | 3657 | png_bytep rp_end = row + row_info->rowbytes; |
| 3658 | int a, c; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3659 | |
John Bowler | fcc0263 | 2011-11-03 18:31:00 -0500 | [diff] [blame] | 3660 | /* First pixel/byte */ |
| 3661 | c = *prev_row++; |
| 3662 | a = *row + c; |
| 3663 | *row++ = (png_byte)a; |
| 3664 | |
| 3665 | /* Remainder */ |
| 3666 | while (row < rp_end) |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3667 | { |
John Bowler | fcc0263 | 2011-11-03 18:31:00 -0500 | [diff] [blame] | 3668 | int b, pa, pb, pc, p; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3669 | |
John Bowler | fcc0263 | 2011-11-03 18:31:00 -0500 | [diff] [blame] | 3670 | a &= 0xff; /* From previous iteration or start */ |
| 3671 | b = *prev_row++; |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3672 | |
| 3673 | p = b - c; |
| 3674 | pc = a - c; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 3675 | |
John Bowler | fcc0263 | 2011-11-03 18:31:00 -0500 | [diff] [blame] | 3676 | # ifdef PNG_USE_ABS |
| 3677 | pa = abs(p); |
| 3678 | pb = abs(pc); |
| 3679 | pc = abs(p + pc); |
| 3680 | # else |
| 3681 | pa = p < 0 ? -p : p; |
| 3682 | pb = pc < 0 ? -pc : pc; |
| 3683 | pc = (p + pc) < 0 ? -(p + pc) : p + pc; |
| 3684 | # endif |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3685 | |
John Bowler | fcc0263 | 2011-11-03 18:31:00 -0500 | [diff] [blame] | 3686 | /* Find the best predictor, the least of pa, pb, pc favoring the earlier |
| 3687 | * ones in the case of a tie. |
| 3688 | */ |
| 3689 | if (pb < pa) pa = pb, a = b; |
| 3690 | if (pc < pa) a = c; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3691 | |
John Bowler | fcc0263 | 2011-11-03 18:31:00 -0500 | [diff] [blame] | 3692 | /* Calculate the current pixel in a, and move the previous row pixel to c |
| 3693 | * for the next time round the loop |
| 3694 | */ |
| 3695 | c = b; |
| 3696 | a += *row; |
| 3697 | *row++ = (png_byte)a; |
| 3698 | } |
| 3699 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3700 | |
John Bowler | fcc0263 | 2011-11-03 18:31:00 -0500 | [diff] [blame] | 3701 | static void |
| 3702 | png_read_filter_row_paeth_multibyte_pixel(png_row_infop row_info, png_bytep row, |
| 3703 | png_const_bytep prev_row) |
| 3704 | { |
| 3705 | int bpp = (row_info->pixel_depth + 7) >> 3; |
| 3706 | png_bytep rp_end = row + bpp; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3707 | |
John Bowler | fcc0263 | 2011-11-03 18:31:00 -0500 | [diff] [blame] | 3708 | /* Process the first pixel in the row completely (this is the same as 'up' |
| 3709 | * because there is only one candidate predictor for the first row). |
| 3710 | */ |
| 3711 | while (row < rp_end) |
| 3712 | { |
| 3713 | int a = *row + *prev_row++; |
| 3714 | *row++ = (png_byte)a; |
| 3715 | } |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3716 | |
John Bowler | fcc0263 | 2011-11-03 18:31:00 -0500 | [diff] [blame] | 3717 | /* Remainder */ |
| 3718 | rp_end += row_info->rowbytes - bpp; |
| 3719 | |
| 3720 | while (row < rp_end) |
| 3721 | { |
| 3722 | int a, b, c, pa, pb, pc, p; |
| 3723 | |
| 3724 | c = *(prev_row - bpp); |
| 3725 | a = *(row - bpp); |
| 3726 | b = *prev_row++; |
| 3727 | |
| 3728 | p = b - c; |
| 3729 | pc = a - c; |
| 3730 | |
| 3731 | # ifdef PNG_USE_ABS |
| 3732 | pa = abs(p); |
| 3733 | pb = abs(pc); |
| 3734 | pc = abs(p + pc); |
| 3735 | # else |
| 3736 | pa = p < 0 ? -p : p; |
| 3737 | pb = pc < 0 ? -pc : pc; |
| 3738 | pc = (p + pc) < 0 ? -(p + pc) : p + pc; |
| 3739 | # endif |
| 3740 | |
| 3741 | if (pb < pa) pa = pb, a = b; |
| 3742 | if (pc < pa) a = c; |
| 3743 | |
| 3744 | c = b; |
| 3745 | a += *row; |
| 3746 | *row++ = (png_byte)a; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3747 | } |
| 3748 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3749 | |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3750 | #ifdef PNG_ARM_NEON |
Mans Rullgard | e7acc6a | 2011-11-16 13:48:18 -0600 | [diff] [blame] | 3751 | |
| 3752 | #ifdef __linux__ |
| 3753 | #include <stdio.h> |
| 3754 | #include <elf.h> |
| 3755 | #include <asm/hwcap.h> |
| 3756 | |
| 3757 | static int png_have_hwcap(unsigned cap) |
| 3758 | { |
| 3759 | FILE *f = fopen("/proc/self/auxv", "r"); |
| 3760 | Elf32_auxv_t aux; |
| 3761 | int have_cap = 0; |
| 3762 | |
| 3763 | if (!f) |
| 3764 | return 0; |
| 3765 | |
| 3766 | while (fread(&aux, sizeof(aux), 1, f) > 0) |
| 3767 | { |
| 3768 | if (aux.a_type == AT_HWCAP && |
| 3769 | aux.a_un.a_val & cap) |
| 3770 | { |
| 3771 | have_cap = 1; |
| 3772 | break; |
| 3773 | } |
| 3774 | } |
| 3775 | |
| 3776 | fclose(f); |
| 3777 | |
| 3778 | return have_cap; |
| 3779 | } |
| 3780 | #endif /* __linux__ */ |
| 3781 | |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3782 | static void |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 3783 | png_init_filter_functions_neon(png_structrp pp, unsigned int bpp) |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3784 | { |
Mans Rullgard | e7acc6a | 2011-11-16 13:48:18 -0600 | [diff] [blame] | 3785 | #ifdef __linux__ |
| 3786 | if (!png_have_hwcap(HWCAP_NEON)) |
| 3787 | return; |
| 3788 | #endif |
| 3789 | |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3790 | pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up_neon; |
| 3791 | |
Mans Rullgard | e7acc6a | 2011-11-16 13:48:18 -0600 | [diff] [blame] | 3792 | if (bpp == 3) |
| 3793 | { |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3794 | pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub3_neon; |
| 3795 | pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg3_neon; |
John Bowler | 6f237b6 | 2012-03-02 13:13:15 -0600 | [diff] [blame] | 3796 | pp->read_filter[PNG_FILTER_VALUE_PAETH-1] = |
Mans Rullgard | e7acc6a | 2011-11-16 13:48:18 -0600 | [diff] [blame] | 3797 | png_read_filter_row_paeth3_neon; |
| 3798 | } |
| 3799 | |
| 3800 | else if (bpp == 4) |
| 3801 | { |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3802 | pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub4_neon; |
| 3803 | pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg4_neon; |
Mans Rullgard | e7acc6a | 2011-11-16 13:48:18 -0600 | [diff] [blame] | 3804 | pp->read_filter[PNG_FILTER_VALUE_PAETH-1] = |
| 3805 | png_read_filter_row_paeth4_neon; |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3806 | } |
| 3807 | } |
Mans Rullgard | e7acc6a | 2011-11-16 13:48:18 -0600 | [diff] [blame] | 3808 | #endif /* PNG_ARM_NEON */ |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3809 | |
| 3810 | static void |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 3811 | png_init_filter_functions(png_structrp pp) |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3812 | { |
John Bowler | fcc0263 | 2011-11-03 18:31:00 -0500 | [diff] [blame] | 3813 | unsigned int bpp = (pp->pixel_depth + 7) >> 3; |
| 3814 | |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3815 | pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub; |
| 3816 | pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up; |
| 3817 | pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg; |
John Bowler | fcc0263 | 2011-11-03 18:31:00 -0500 | [diff] [blame] | 3818 | if (bpp == 1) |
| 3819 | pp->read_filter[PNG_FILTER_VALUE_PAETH-1] = |
| 3820 | png_read_filter_row_paeth_1byte_pixel; |
| 3821 | else |
| 3822 | pp->read_filter[PNG_FILTER_VALUE_PAETH-1] = |
| 3823 | png_read_filter_row_paeth_multibyte_pixel; |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3824 | |
| 3825 | #ifdef PNG_ARM_NEON |
John Bowler | fcc0263 | 2011-11-03 18:31:00 -0500 | [diff] [blame] | 3826 | png_init_filter_functions_neon(pp, bpp); |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3827 | #endif |
| 3828 | } |
| 3829 | |
| 3830 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 3831 | png_read_filter_row(png_structrp pp, png_row_infop row_info, png_bytep row, |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3832 | png_const_bytep prev_row, int filter) |
| 3833 | { |
| 3834 | if (pp->read_filter[0] == NULL) |
| 3835 | png_init_filter_functions(pp); |
| 3836 | if (filter > PNG_FILTER_VALUE_NONE && filter < PNG_FILTER_VALUE_LAST) |
| 3837 | pp->read_filter[filter-1](row_info, row, prev_row); |
| 3838 | } |
| 3839 | |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 3840 | #ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 3841 | void /* PRIVATE */ |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 3842 | png_read_IDAT_data(png_structrp png_ptr, png_bytep output, |
| 3843 | png_alloc_size_t avail_out) |
| 3844 | { |
| 3845 | /* Loop reading IDATs and decompressing the result into output[avail_out] */ |
| 3846 | png_ptr->zstream.next_out = output; |
| 3847 | png_ptr->zstream.avail_out = 0; /* safety: set below */ |
| 3848 | |
| 3849 | if (output == NULL) |
| 3850 | avail_out = 0; |
| 3851 | |
| 3852 | do |
| 3853 | { |
| 3854 | int ret; |
| 3855 | png_byte tmpbuf[PNG_INFLATE_BUF_SIZE]; |
| 3856 | |
| 3857 | if (png_ptr->zstream.avail_in == 0) |
| 3858 | { |
| 3859 | uInt avail_in; |
| 3860 | png_bytep buffer; |
| 3861 | |
| 3862 | while (png_ptr->idat_size == 0) |
| 3863 | { |
| 3864 | png_crc_finish(png_ptr, 0); |
| 3865 | |
| 3866 | png_ptr->idat_size = png_read_chunk_header(png_ptr); |
| 3867 | /* This is an error even in the 'check' case because the code just |
| 3868 | * consumed a non-IDAT header. |
| 3869 | */ |
| 3870 | if (png_ptr->chunk_name != png_IDAT) |
| 3871 | png_error(png_ptr, "Not enough image data"); |
| 3872 | } |
| 3873 | |
| 3874 | avail_in = png_ptr->IDAT_read_size; |
| 3875 | |
| 3876 | if (avail_in > png_ptr->idat_size) |
| 3877 | avail_in = (uInt)png_ptr->idat_size; |
| 3878 | |
| 3879 | /* A PNG with a gradually increasing IDAT size will defeat this attempt |
| 3880 | * to minimize memory usage by causing lots of re-allocs, but |
| 3881 | * realistically doing IDAT_read_size re-allocs is not likely to be a |
| 3882 | * big problem. |
| 3883 | */ |
| 3884 | buffer = png_read_buffer(png_ptr, avail_in, 0/*error*/); |
| 3885 | |
| 3886 | png_crc_read(png_ptr, buffer, avail_in); |
| 3887 | png_ptr->idat_size -= avail_in; |
| 3888 | |
| 3889 | png_ptr->zstream.next_in = buffer; |
| 3890 | png_ptr->zstream.avail_in = avail_in; |
| 3891 | } |
| 3892 | |
| 3893 | /* And set up the output side. */ |
| 3894 | if (output != NULL) /* standard read */ |
| 3895 | { |
| 3896 | uInt out = ZLIB_IO_MAX; |
| 3897 | |
| 3898 | if (out > avail_out) |
| 3899 | out = (uInt)avail_out; |
| 3900 | |
| 3901 | avail_out -= out; |
| 3902 | png_ptr->zstream.avail_out = out; |
| 3903 | } |
| 3904 | |
| 3905 | else /* check for end */ |
| 3906 | { |
| 3907 | png_ptr->zstream.next_out = tmpbuf; |
| 3908 | png_ptr->zstream.avail_out = sizeof tmpbuf; |
| 3909 | } |
| 3910 | |
| 3911 | /* Use NO_FLUSH; this gives zlib the maximum opportunity to optimize the |
John Bowler | 9066919 | 2012-03-09 22:03:13 -0600 | [diff] [blame] | 3912 | * process. If the LZ stream is truncated the sequential reader will |
| 3913 | * terminally damage the stream, above, by reading the chunk header of the |
| 3914 | * following chunk (it then exits with png_error). |
| 3915 | * |
| 3916 | * TODO: deal more elegantly with truncated IDAT lists. |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 3917 | */ |
| 3918 | ret = inflate(&png_ptr->zstream, Z_NO_FLUSH); |
| 3919 | |
| 3920 | /* Take the unconsumed output back (so, in the 'check' case this just |
| 3921 | * counts up). |
| 3922 | */ |
| 3923 | avail_out += png_ptr->zstream.avail_out; |
| 3924 | png_ptr->zstream.avail_out = 0; |
| 3925 | |
| 3926 | if (ret == Z_STREAM_END) |
| 3927 | { |
| 3928 | /* Do this for safety; we won't read any more into this row. */ |
| 3929 | png_ptr->zstream.next_out = NULL; |
| 3930 | |
| 3931 | png_ptr->mode |= PNG_AFTER_IDAT; |
| 3932 | png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED; |
| 3933 | |
| 3934 | if (png_ptr->zstream.avail_in > 0 || png_ptr->idat_size > 0) |
| 3935 | png_chunk_benign_error(png_ptr, "Extra compressed data"); |
| 3936 | break; |
| 3937 | } |
| 3938 | |
| 3939 | if (ret != Z_OK) |
| 3940 | { |
| 3941 | png_zstream_error(png_ptr, ret); |
| 3942 | |
| 3943 | if (output != NULL) |
| 3944 | png_chunk_error(png_ptr, png_ptr->zstream.msg); |
| 3945 | |
| 3946 | else /* checking */ |
| 3947 | { |
| 3948 | png_chunk_benign_error(png_ptr, png_ptr->zstream.msg); |
| 3949 | return; |
| 3950 | } |
| 3951 | } |
| 3952 | } while (avail_out > 0); |
| 3953 | |
| 3954 | if (avail_out > 0) |
| 3955 | { |
| 3956 | /* The stream ended before the image; this is the same as too few IDATs so |
| 3957 | * should be handled the same way. |
| 3958 | */ |
| 3959 | if (output != NULL) |
| 3960 | png_error(png_ptr, "Not enough image data"); |
| 3961 | |
| 3962 | else /* checking */ |
| 3963 | png_chunk_benign_error(png_ptr, "Too much image data"); |
| 3964 | } |
| 3965 | } |
| 3966 | |
| 3967 | void /* PRIVATE */ |
| 3968 | png_read_finish_IDAT(png_structrp png_ptr) |
| 3969 | { |
| 3970 | /* We don't need any more data and the stream should have ended, however the |
| 3971 | * LZ end code may actually not have been processed. In this case we must |
| 3972 | * read it otherwise stray unread IDAT data or, more likely, an IDAT chunk |
| 3973 | * may still remain to be consumed. |
| 3974 | */ |
| 3975 | if (!(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED)) |
| 3976 | { |
| 3977 | /* The NULL causes png_read_IDAT_data to swallow any remaining bytes in |
| 3978 | * the compressed stream, but the stream may be damaged too, so even after |
| 3979 | * this call we may need to terminate the zstream ownership. |
| 3980 | */ |
| 3981 | png_read_IDAT_data(png_ptr, NULL, 0); |
| 3982 | png_ptr->zstream.next_out = NULL; /* safety */ |
| 3983 | |
| 3984 | /* Now clear everything out for safety; the following may not have been |
| 3985 | * done. |
| 3986 | */ |
| 3987 | if (!(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED)) |
| 3988 | { |
| 3989 | png_ptr->mode |= PNG_AFTER_IDAT; |
| 3990 | png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED; |
| 3991 | } |
| 3992 | } |
| 3993 | |
| 3994 | /* If the zstream has not been released do it now *and* terminate the reading |
| 3995 | * of the final IDAT chunk. |
| 3996 | */ |
| 3997 | if (png_ptr->zowner == png_IDAT) |
| 3998 | { |
| 3999 | /* Always do this; the pointers otherwise point into the read buffer. */ |
| 4000 | png_ptr->zstream.next_in = NULL; |
| 4001 | png_ptr->zstream.avail_in = 0; |
| 4002 | |
| 4003 | /* Now we no longer own the zstream. */ |
| 4004 | png_ptr->zowner = 0; |
| 4005 | |
| 4006 | /* The slightly weird semantics of the sequential IDAT reading is that we |
| 4007 | * are always in or at the end of an IDAT chunk, so we always need to do a |
| 4008 | * crc_finish here. If idat_size is non-zero we also need to read the |
| 4009 | * spurious bytes at the end of the chunk now. |
| 4010 | */ |
| 4011 | (void)png_crc_finish(png_ptr, png_ptr->idat_size); |
| 4012 | } |
| 4013 | } |
| 4014 | |
| 4015 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 4016 | png_read_finish_row(png_structrp png_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4017 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 4018 | #ifdef PNG_READ_INTERLACING_SUPPORTED |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 4019 | /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 4020 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 4021 | /* Start of interlace block */ |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 4022 | static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 4023 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 4024 | /* Offset to next interlace block */ |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 4025 | static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 4026 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 4027 | /* Start of interlace block in the y direction */ |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 4028 | static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 4029 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 4030 | /* Offset to next interlace block in the y direction */ |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 4031 | static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 4032 | #endif /* PNG_READ_INTERLACING_SUPPORTED */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 4033 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 4034 | png_debug(1, "in png_read_finish_row"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4035 | png_ptr->row_number++; |
| 4036 | if (png_ptr->row_number < png_ptr->num_rows) |
| 4037 | return; |
| 4038 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 4039 | #ifdef PNG_READ_INTERLACING_SUPPORTED |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4040 | if (png_ptr->interlaced) |
| 4041 | { |
| 4042 | png_ptr->row_number = 0; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4043 | |
Glenn Randers-Pehrson | 435cf87 | 2011-10-05 16:23:53 -0500 | [diff] [blame] | 4044 | /* TO DO: don't do this if prev_row isn't needed (requires |
| 4045 | * read-ahead of the next row's filter byte. |
| 4046 | */ |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4047 | png_memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1); |
| 4048 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4049 | do |
| 4050 | { |
| 4051 | png_ptr->pass++; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4052 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4053 | if (png_ptr->pass >= 7) |
| 4054 | break; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4055 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4056 | png_ptr->iwidth = (png_ptr->width + |
| 4057 | png_pass_inc[png_ptr->pass] - 1 - |
| 4058 | png_pass_start[png_ptr->pass]) / |
| 4059 | png_pass_inc[png_ptr->pass]; |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 4060 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4061 | if (!(png_ptr->transformations & PNG_INTERLACE)) |
| 4062 | { |
| 4063 | png_ptr->num_rows = (png_ptr->height + |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4064 | png_pass_yinc[png_ptr->pass] - 1 - |
| 4065 | png_pass_ystart[png_ptr->pass]) / |
| 4066 | png_pass_yinc[png_ptr->pass]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4067 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4068 | |
Glenn Randers-Pehrson | 345bc27 | 1998-06-14 14:43:31 -0500 | [diff] [blame] | 4069 | else /* if (png_ptr->transformations & PNG_INTERLACE) */ |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4070 | break; /* libpng deinterlacing sees every row */ |
| 4071 | |
| 4072 | } while (png_ptr->num_rows == 0 || png_ptr->iwidth == 0); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4073 | |
| 4074 | if (png_ptr->pass < 7) |
| 4075 | return; |
| 4076 | } |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 4077 | #endif /* PNG_READ_INTERLACING_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4078 | |
John Bowler | 42a2b55 | 2012-03-05 20:57:40 -0600 | [diff] [blame] | 4079 | /* Here after at the end of the last row of the last pass. */ |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 4080 | png_read_finish_IDAT(png_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4081 | } |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 4082 | #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4083 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 4084 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 4085 | png_read_start_row(png_structrp png_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4086 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 4087 | #ifdef PNG_READ_INTERLACING_SUPPORTED |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 4088 | /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 4089 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 4090 | /* Start of interlace block */ |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 4091 | static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 4092 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 4093 | /* Offset to next interlace block */ |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 4094 | static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 4095 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 4096 | /* Start of interlace block in the y direction */ |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 4097 | static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 4098 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 4099 | /* Offset to next interlace block in the y direction */ |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 4100 | static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 4101 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 4102 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4103 | int max_pixel_depth; |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 4104 | png_size_t row_bytes; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4105 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 4106 | png_debug(1, "in png_read_start_row"); |
John Bowler | 42a2b55 | 2012-03-05 20:57:40 -0600 | [diff] [blame] | 4107 | |
John Bowler | 4a12f4a | 2011-04-17 18:34:22 -0500 | [diff] [blame] | 4108 | #ifdef PNG_READ_TRANSFORMS_SUPPORTED |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4109 | png_init_read_transformations(png_ptr); |
John Bowler | 4a12f4a | 2011-04-17 18:34:22 -0500 | [diff] [blame] | 4110 | #endif |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 4111 | #ifdef PNG_READ_INTERLACING_SUPPORTED |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4112 | if (png_ptr->interlaced) |
| 4113 | { |
| 4114 | if (!(png_ptr->transformations & PNG_INTERLACE)) |
| 4115 | png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 - |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4116 | png_pass_ystart[0]) / png_pass_yinc[0]; |
| 4117 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4118 | else |
| 4119 | png_ptr->num_rows = png_ptr->height; |
| 4120 | |
| 4121 | png_ptr->iwidth = (png_ptr->width + |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4122 | png_pass_inc[png_ptr->pass] - 1 - |
| 4123 | png_pass_start[png_ptr->pass]) / |
| 4124 | png_pass_inc[png_ptr->pass]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4125 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4126 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4127 | else |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 4128 | #endif /* PNG_READ_INTERLACING_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4129 | { |
| 4130 | png_ptr->num_rows = png_ptr->height; |
| 4131 | png_ptr->iwidth = png_ptr->width; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4132 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4133 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4134 | max_pixel_depth = png_ptr->pixel_depth; |
| 4135 | |
John Bowler | e6fb691 | 2011-11-08 21:35:16 -0600 | [diff] [blame] | 4136 | /* WARNING: * png_read_transform_info (pngrtran.c) performs a simpliar set of |
| 4137 | * calculations to calculate the final pixel depth, then |
| 4138 | * png_do_read_transforms actually does the transforms. This means that the |
| 4139 | * code which effectively calculates this value is actually repeated in three |
| 4140 | * separate places. They must all match. Innocent changes to the order of |
| 4141 | * transformations can and will break libpng in a way that causes memory |
| 4142 | * overwrites. |
| 4143 | * |
| 4144 | * TODO: fix this. |
| 4145 | */ |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 4146 | #ifdef PNG_READ_PACK_SUPPORTED |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4147 | if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4148 | max_pixel_depth = 8; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 4149 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4150 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 4151 | #ifdef PNG_READ_EXPAND_SUPPORTED |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 4152 | if (png_ptr->transformations & PNG_EXPAND) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4153 | { |
| 4154 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 4155 | { |
| 4156 | if (png_ptr->num_trans) |
| 4157 | max_pixel_depth = 32; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4158 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4159 | else |
| 4160 | max_pixel_depth = 24; |
| 4161 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4162 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4163 | else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) |
| 4164 | { |
| 4165 | if (max_pixel_depth < 8) |
| 4166 | max_pixel_depth = 8; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4167 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4168 | if (png_ptr->num_trans) |
| 4169 | max_pixel_depth *= 2; |
| 4170 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4171 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4172 | else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB) |
| 4173 | { |
| 4174 | if (png_ptr->num_trans) |
| 4175 | { |
| 4176 | max_pixel_depth *= 4; |
| 4177 | max_pixel_depth /= 3; |
| 4178 | } |
| 4179 | } |
| 4180 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 4181 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4182 | |
John Bowler | 4d56296 | 2011-02-12 09:01:20 -0600 | [diff] [blame] | 4183 | #ifdef PNG_READ_EXPAND_16_SUPPORTED |
| 4184 | if (png_ptr->transformations & PNG_EXPAND_16) |
| 4185 | { |
| 4186 | # ifdef PNG_READ_EXPAND_SUPPORTED |
| 4187 | /* In fact it is an error if it isn't supported, but checking is |
| 4188 | * the safe way. |
| 4189 | */ |
| 4190 | if (png_ptr->transformations & PNG_EXPAND) |
| 4191 | { |
| 4192 | if (png_ptr->bit_depth < 16) |
| 4193 | max_pixel_depth *= 2; |
| 4194 | } |
| 4195 | else |
| 4196 | # endif |
| 4197 | png_ptr->transformations &= ~PNG_EXPAND_16; |
| 4198 | } |
| 4199 | #endif |
| 4200 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 4201 | #ifdef PNG_READ_FILLER_SUPPORTED |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 4202 | if (png_ptr->transformations & (PNG_FILLER)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4203 | { |
John Bowler | e6fb691 | 2011-11-08 21:35:16 -0600 | [diff] [blame] | 4204 | if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 4205 | { |
| 4206 | if (max_pixel_depth <= 8) |
| 4207 | max_pixel_depth = 16; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4208 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 4209 | else |
| 4210 | max_pixel_depth = 32; |
| 4211 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4212 | |
John Bowler | e6fb691 | 2011-11-08 21:35:16 -0600 | [diff] [blame] | 4213 | else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB || |
| 4214 | png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 4215 | { |
| 4216 | if (max_pixel_depth <= 32) |
| 4217 | max_pixel_depth = 32; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4218 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 4219 | else |
| 4220 | max_pixel_depth = 64; |
| 4221 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4222 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 4223 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4224 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 4225 | #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4226 | if (png_ptr->transformations & PNG_GRAY_TO_RGB) |
| 4227 | { |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 4228 | if ( |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 4229 | #ifdef PNG_READ_EXPAND_SUPPORTED |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4230 | (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) || |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 4231 | #endif |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 4232 | #ifdef PNG_READ_FILLER_SUPPORTED |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4233 | (png_ptr->transformations & (PNG_FILLER)) || |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 4234 | #endif |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4235 | png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4236 | { |
| 4237 | if (max_pixel_depth <= 16) |
| 4238 | max_pixel_depth = 32; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4239 | |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 4240 | else |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4241 | max_pixel_depth = 64; |
| 4242 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4243 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4244 | else |
| 4245 | { |
| 4246 | if (max_pixel_depth <= 8) |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4247 | { |
| 4248 | if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 4249 | max_pixel_depth = 32; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4250 | |
| 4251 | else |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 4252 | max_pixel_depth = 24; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4253 | } |
| 4254 | |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 4255 | else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
| 4256 | max_pixel_depth = 64; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4257 | |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 4258 | else |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4259 | max_pixel_depth = 48; |
| 4260 | } |
| 4261 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 4262 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4263 | |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 4264 | #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \ |
| 4265 | defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 4266 | if (png_ptr->transformations & PNG_USER_TRANSFORM) |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4267 | { |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 4268 | int user_pixel_depth = png_ptr->user_transform_depth * |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 4269 | png_ptr->user_transform_channels; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4270 | |
| 4271 | if (user_pixel_depth > max_pixel_depth) |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 4272 | max_pixel_depth = user_pixel_depth; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4273 | } |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 4274 | #endif |
| 4275 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 4276 | /* This value is stored in png_struct and double checked in the row read |
| 4277 | * code. |
| 4278 | */ |
| 4279 | png_ptr->maximum_pixel_depth = (png_byte)max_pixel_depth; |
| 4280 | png_ptr->transformed_pixel_depth = 0; /* calculated on demand */ |
| 4281 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 4282 | /* Align the width on the next larger 8 pixels. Mainly used |
| 4283 | * for interlacing |
| 4284 | */ |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 4285 | row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7)); |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 4286 | /* Calculate the maximum bytes needed, adding a byte and a pixel |
| 4287 | * for safety's sake |
| 4288 | */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 4289 | row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) + |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4290 | 1 + ((max_pixel_depth + 7) >> 3); |
| 4291 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4292 | #ifdef PNG_MAX_MALLOC_64K |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 4293 | if (row_bytes > (png_uint_32)65536L) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 4294 | png_error(png_ptr, "This image requires a row greater than 64KB"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4295 | #endif |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 4296 | |
Glenn Randers-Pehrson | eddc5af | 2009-11-20 21:15:06 -0600 | [diff] [blame] | 4297 | if (row_bytes + 48 > png_ptr->old_big_row_buf_size) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 4298 | { |
| 4299 | png_free(png_ptr, png_ptr->big_row_buf); |
Mans Rullgard | c9e27d0 | 2011-10-17 15:25:03 -0500 | [diff] [blame] | 4300 | png_free(png_ptr, png_ptr->big_prev_row); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4301 | |
Glenn Randers-Pehrson | 6917b51 | 2009-03-09 15:31:08 -0500 | [diff] [blame] | 4302 | if (png_ptr->interlaced) |
Glenn Randers-Pehrson | a515d30 | 2010-01-01 10:24:25 -0600 | [diff] [blame] | 4303 | png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr, |
| 4304 | row_bytes + 48); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4305 | |
Glenn Randers-Pehrson | a515d30 | 2010-01-01 10:24:25 -0600 | [diff] [blame] | 4306 | else |
Mans Rullgard | c9e27d0 | 2011-10-17 15:25:03 -0500 | [diff] [blame] | 4307 | png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes + 48); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4308 | |
Mans Rullgard | c9e27d0 | 2011-10-17 15:25:03 -0500 | [diff] [blame] | 4309 | png_ptr->big_prev_row = (png_bytep)png_malloc(png_ptr, row_bytes + 48); |
Glenn Randers-Pehrson | eddc5af | 2009-11-20 21:15:06 -0600 | [diff] [blame] | 4310 | |
| 4311 | #ifdef PNG_ALIGNED_MEMORY_SUPPORTED |
| 4312 | /* Use 16-byte aligned memory for row_buf with at least 16 bytes |
Mans Rullgard | c9e27d0 | 2011-10-17 15:25:03 -0500 | [diff] [blame] | 4313 | * of padding before and after row_buf; treat prev_row similarly. |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 4314 | * NOTE: the alignment is to the start of the pixels, one beyond the start |
| 4315 | * of the buffer, because of the filter byte. Prior to libpng 1.5.6 this |
Mans Rullgard | c9e27d0 | 2011-10-17 15:25:03 -0500 | [diff] [blame] | 4316 | * was incorrect; the filter byte was aligned, which had the exact |
| 4317 | * opposite effect of that intended. |
Glenn Randers-Pehrson | eddc5af | 2009-11-20 21:15:06 -0600 | [diff] [blame] | 4318 | */ |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 4319 | { |
| 4320 | png_bytep temp = png_ptr->big_row_buf + 32; |
Glenn Randers-Pehrson | 8db1998 | 2011-10-27 16:17:24 -0500 | [diff] [blame] | 4321 | int extra = (int)((temp - (png_bytep)0) & 0x0f); |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 4322 | png_ptr->row_buf = temp - extra - 1/*filter byte*/; |
Mans Rullgard | c9e27d0 | 2011-10-17 15:25:03 -0500 | [diff] [blame] | 4323 | |
| 4324 | temp = png_ptr->big_prev_row + 32; |
Glenn Randers-Pehrson | 8db1998 | 2011-10-27 16:17:24 -0500 | [diff] [blame] | 4325 | extra = (int)((temp - (png_bytep)0) & 0x0f); |
Mans Rullgard | c9e27d0 | 2011-10-17 15:25:03 -0500 | [diff] [blame] | 4326 | png_ptr->prev_row = temp - extra - 1/*filter byte*/; |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 4327 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4328 | |
Glenn Randers-Pehrson | eddc5af | 2009-11-20 21:15:06 -0600 | [diff] [blame] | 4329 | #else |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 4330 | /* Use 31 bytes of padding before and 17 bytes after row_buf. */ |
| 4331 | png_ptr->row_buf = png_ptr->big_row_buf + 31; |
Mans Rullgard | c9e27d0 | 2011-10-17 15:25:03 -0500 | [diff] [blame] | 4332 | png_ptr->prev_row = png_ptr->big_prev_row + 31; |
Glenn Randers-Pehrson | eddc5af | 2009-11-20 21:15:06 -0600 | [diff] [blame] | 4333 | #endif |
| 4334 | png_ptr->old_big_row_buf_size = row_bytes + 48; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 4335 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4336 | |
| 4337 | #ifdef PNG_MAX_MALLOC_64K |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4338 | if (png_ptr->rowbytes > 65535) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 4339 | png_error(png_ptr, "This image requires a row greater than 64KB"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4340 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4341 | #endif |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4342 | if (png_ptr->rowbytes > (PNG_SIZE_MAX - 1)) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 4343 | 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] | 4344 | |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 4345 | png_memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 4346 | |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4347 | png_debug1(3, "width = %u,", png_ptr->width); |
| 4348 | png_debug1(3, "height = %u,", png_ptr->height); |
| 4349 | png_debug1(3, "iwidth = %u,", png_ptr->iwidth); |
| 4350 | png_debug1(3, "num_rows = %u,", png_ptr->num_rows); |
Glenn Randers-Pehrson | b764c60 | 2011-01-14 21:18:37 -0600 | [diff] [blame] | 4351 | png_debug1(3, "rowbytes = %lu,", (unsigned long)png_ptr->rowbytes); |
| 4352 | png_debug1(3, "irowbytes = %lu", |
| 4353 | (unsigned long)PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4354 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 4355 | /* The sequential reader needs a buffer for IDAT, but the progressive reader |
| 4356 | * does not, so free the read buffer now regardless; the sequential reader |
| 4357 | * reallocates it on demand. |
| 4358 | */ |
| 4359 | if (png_ptr->read_buffer) |
| 4360 | { |
| 4361 | png_bytep buffer = png_ptr->read_buffer; |
| 4362 | |
| 4363 | png_ptr->read_buffer_size = 0; |
| 4364 | png_ptr->read_buffer = NULL; |
| 4365 | png_free(png_ptr, buffer); |
| 4366 | } |
| 4367 | |
John Bowler | 9066919 | 2012-03-09 22:03:13 -0600 | [diff] [blame] | 4368 | /* Finally claim the zstream for the inflate of the IDAT data, use the bits |
| 4369 | * value from the stream (note that this will result in a fatal error if the |
| 4370 | * IDAT stream has a bogus deflate header window_bits value, but this should |
| 4371 | * not be happening any longer!) |
| 4372 | */ |
| 4373 | if (png_inflate_claim(png_ptr, png_IDAT, 0) != Z_OK) |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 4374 | png_error(png_ptr, png_ptr->zstream.msg); |
John Bowler | 42a2b55 | 2012-03-05 20:57:40 -0600 | [diff] [blame] | 4375 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 4376 | png_ptr->flags |= PNG_FLAG_ROW_INIT; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4377 | } |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 4378 | #endif /* PNG_READ_SUPPORTED */ |