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; |
Glenn Randers-Pehrson | 8fbd60d | 2012-03-21 09:18:15 -0500 | [diff] [blame^] | 1439 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 1440 | ret = png_inflate_read(png_ptr, local_buffer, |
| 1441 | sizeof local_buffer, &length, |
| 1442 | profile + (sizeof profile_header), &size, 0); |
| 1443 | |
| 1444 | if (size == 0) |
| 1445 | { |
| 1446 | if (png_icc_check_tag_table(png_ptr, |
| 1447 | &png_ptr->colorspace, keyword, profile_length, |
| 1448 | profile)) |
| 1449 | { |
| 1450 | /* The profile has been validated for basic |
| 1451 | * security issues, so read the whole thing in. |
| 1452 | */ |
| 1453 | size = profile_length - (sizeof profile_header) |
| 1454 | - 12 * tag_count; |
Glenn Randers-Pehrson | 8fbd60d | 2012-03-21 09:18:15 -0500 | [diff] [blame^] | 1455 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 1456 | ret = png_inflate_read(png_ptr, local_buffer, |
| 1457 | sizeof local_buffer, &length, |
| 1458 | profile + (sizeof profile_header) + |
| 1459 | 12 * tag_count, &size, 1/*finish*/); |
| 1460 | |
| 1461 | if (size == 0) |
| 1462 | { |
Glenn Randers-Pehrson | 8fbd60d | 2012-03-21 09:18:15 -0500 | [diff] [blame^] | 1463 | int |
| 1464 | icheck; |
| 1465 | |
| 1466 | /* Known sRGB profiles: |
| 1467 | * 0: not a known sRGB profile |
| 1468 | * 1: HP-Microsoft sRGB v2 |
| 1469 | * 2: ICC sRGB v4 perceptual |
| 1470 | * 3: ICC sRGB v2 perceptual |
| 1471 | * no black-compensation |
| 1472 | */ |
| 1473 | |
| 1474 | png_uint_32 check_crc[4] = {0, |
| 1475 | 0xf29e526dUL, 0xbbef7812UL, |
| 1476 | 0x427ebb21UL}, |
| 1477 | |
| 1478 | check_len[4] = {0, 3144, 60960, |
| 1479 | 3052}; |
| 1480 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 1481 | if (ret != Z_STREAM_END) |
| 1482 | png_chunk_warning(png_ptr, |
| 1483 | png_ptr->zstream.msg); |
| 1484 | |
| 1485 | else if (length > 0) |
| 1486 | png_chunk_warning(png_ptr, |
| 1487 | "extra compressed data"); |
| 1488 | |
| 1489 | /* But, because we got the whole profile, |
| 1490 | * assume it is ok. |
| 1491 | */ |
| 1492 | png_ptr->zowner = 0; |
| 1493 | |
| 1494 | /* Yet the CRC should still be correct */ |
| 1495 | if (png_crc_finish(png_ptr, length)) |
| 1496 | return; |
| 1497 | |
Glenn Randers-Pehrson | 8fbd60d | 2012-03-21 09:18:15 -0500 | [diff] [blame^] | 1498 | /* See if it's a known sRGB profile. */ |
| 1499 | |
| 1500 | for (icheck=3; icheck > 0; icheck--) |
| 1501 | { |
| 1502 | if (profile_length == check_len[icheck]) |
| 1503 | { |
| 1504 | png_uint_32 profile_crc = crc32(0, |
| 1505 | profile, profile_length); |
| 1506 | |
| 1507 | if (profile_crc == |
| 1508 | check_crc[icheck]) |
| 1509 | { |
| 1510 | (void)png_colorspace_set_sRGB( |
| 1511 | png_ptr, |
| 1512 | &png_ptr->colorspace, |
| 1513 | PNG_sRGB_INTENT_PERCEPTUAL, |
| 1514 | 0); |
| 1515 | |
| 1516 | png_colorspace_sync(png_ptr, |
| 1517 | info_ptr); |
| 1518 | |
| 1519 | png_free(png_ptr, profile); |
| 1520 | |
| 1521 | return; |
| 1522 | } |
| 1523 | } |
| 1524 | } |
| 1525 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 1526 | /* Set the gAMA and cHRM information */ |
| 1527 | png_icc_set_gAMA_and_cHRM(png_ptr, |
| 1528 | &png_ptr->colorspace, keyword, profile, |
| 1529 | 0/*prefer explicit gAMA/cHRM*/); |
| 1530 | |
Glenn Randers-Pehrson | 8fbd60d | 2012-03-21 09:18:15 -0500 | [diff] [blame^] | 1531 | /* Steal the profile for info_ptr. */ |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 1532 | if (info_ptr != NULL) |
| 1533 | { |
| 1534 | png_free_data(png_ptr, info_ptr, |
| 1535 | PNG_FREE_ICCP, 0); |
| 1536 | |
| 1537 | info_ptr->iccp_name = png_voidcast(char*, |
| 1538 | png_malloc_base(png_ptr, |
| 1539 | keyword_length+1)); |
| 1540 | if (info_ptr->iccp_name == NULL) |
| 1541 | { |
| 1542 | png_ptr->colorspace.flags |= |
| 1543 | PNG_COLORSPACE_INVALID; |
| 1544 | png_colorspace_sync(png_ptr, |
| 1545 | info_ptr); |
| 1546 | png_chunk_benign_error(png_ptr, |
| 1547 | "out of memory"); |
| 1548 | return; |
| 1549 | } |
| 1550 | |
| 1551 | memcpy(info_ptr->iccp_name, keyword, |
| 1552 | keyword_length+1); |
| 1553 | info_ptr->iccp_proflen = profile_length; |
| 1554 | info_ptr->iccp_profile = profile; |
| 1555 | png_ptr->read_buffer = NULL; /*steal*/ |
| 1556 | info_ptr->free_me |= PNG_FREE_ICCP; |
| 1557 | info_ptr->valid |= PNG_INFO_iCCP; |
| 1558 | |
| 1559 | png_colorspace_sync(png_ptr, info_ptr); |
| 1560 | } |
| 1561 | |
| 1562 | return; |
| 1563 | } |
| 1564 | |
| 1565 | else |
| 1566 | errmsg = "truncated"; |
| 1567 | } |
| 1568 | |
| 1569 | /* else png_icc_check_tag_table output an error */ |
| 1570 | } |
| 1571 | |
| 1572 | else /* profile truncated */ |
| 1573 | errmsg = png_ptr->zstream.msg; |
| 1574 | } |
| 1575 | |
| 1576 | else |
| 1577 | errmsg = "out of memory"; |
| 1578 | } |
| 1579 | |
| 1580 | /* else png_icc_check_header output an error */ |
| 1581 | } |
| 1582 | |
| 1583 | /* else png_icc_check_length output an error */ |
| 1584 | } |
| 1585 | |
| 1586 | else /* profile truncated */ |
| 1587 | errmsg = png_ptr->zstream.msg; |
| 1588 | |
| 1589 | /* Release the stream */ |
| 1590 | png_ptr->zowner = 0; |
| 1591 | } |
| 1592 | |
| 1593 | else /* png_inflate_claim failed */ |
| 1594 | errmsg = png_ptr->zstream.msg; |
| 1595 | } |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 1596 | |
| 1597 | else |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 1598 | errmsg = "bad compression method"; /* or missing */ |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 1599 | } |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 1600 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 1601 | else |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 1602 | errmsg = "bad keyword"; |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 1603 | } |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 1604 | |
| 1605 | else |
| 1606 | errmsg = "too many profiles"; |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 1607 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 1608 | /* Failure: the reason is in 'errmsg' */ |
| 1609 | png_crc_finish(png_ptr, length); |
| 1610 | png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID; |
| 1611 | png_colorspace_sync(png_ptr, info_ptr); |
| 1612 | if (errmsg != NULL) /* else already output */ |
| 1613 | png_chunk_benign_error(png_ptr, errmsg); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1614 | } |
| 1615 | #endif /* PNG_READ_iCCP_SUPPORTED */ |
| 1616 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1617 | #ifdef PNG_READ_sPLT_SUPPORTED |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1618 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 1619 | 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] | 1620 | /* Note: this does not properly handle chunks that are > 64K under DOS */ |
| 1621 | { |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 1622 | png_bytep entry_start, buffer; |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 1623 | png_sPLT_t new_palette; |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 1624 | png_sPLT_entryp pp; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1625 | png_uint_32 data_length; |
| 1626 | int entry_size, i; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1627 | png_uint_32 skip = 0; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1628 | png_uint_32 dl; |
| 1629 | png_size_t max_dl; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1630 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1631 | png_debug(1, "in png_handle_sPLT"); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1632 | |
Glenn Randers-Pehrson | e3f3c4e | 2010-02-07 18:08:50 -0600 | [diff] [blame] | 1633 | #ifdef PNG_USER_LIMITS_SUPPORTED |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 1634 | if (png_ptr->user_chunk_cache_max != 0) |
| 1635 | { |
| 1636 | if (png_ptr->user_chunk_cache_max == 1) |
| 1637 | { |
| 1638 | png_crc_finish(png_ptr, length); |
| 1639 | return; |
| 1640 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1641 | |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 1642 | if (--png_ptr->user_chunk_cache_max == 1) |
| 1643 | { |
| 1644 | png_warning(png_ptr, "No space in chunk cache for sPLT"); |
| 1645 | png_crc_finish(png_ptr, length); |
| 1646 | return; |
| 1647 | } |
| 1648 | } |
| 1649 | #endif |
| 1650 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1651 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 1652 | png_chunk_error(png_ptr, "missing IHDR"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1653 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1654 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 1655 | { |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1656 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 1657 | png_chunk_benign_error(png_ptr, "out of place"); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1658 | return; |
| 1659 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1660 | |
| 1661 | #ifdef PNG_MAX_MALLOC_64K |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 1662 | if (length > 65535U) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1663 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 1664 | png_crc_finish(png_ptr, length); |
| 1665 | png_chunk_benign_error(png_ptr, "too large to fit in memory"); |
| 1666 | return; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1667 | } |
| 1668 | #endif |
| 1669 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 1670 | buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/); |
| 1671 | if (buffer == NULL) |
| 1672 | { |
| 1673 | png_crc_finish(png_ptr, length); |
| 1674 | png_chunk_benign_error(png_ptr, "out of memory"); |
| 1675 | return; |
| 1676 | } |
| 1677 | |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1678 | |
| 1679 | /* WARNING: this may break if size_t is less than 32 bits; it is assumed |
| 1680 | * that the PNG_MAX_MALLOC_64K test is enabled in this case, but this is a |
| 1681 | * potential breakage point if the types in pngconf.h aren't exactly right. |
| 1682 | */ |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 1683 | png_crc_read(png_ptr, buffer, length); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1684 | |
| 1685 | if (png_crc_finish(png_ptr, skip)) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1686 | return; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1687 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 1688 | buffer[length] = 0; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1689 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 1690 | for (entry_start = buffer; *entry_start; entry_start++) |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 1691 | /* Empty loop to find end of name */ ; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1692 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1693 | ++entry_start; |
| 1694 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 1695 | /* 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] | 1696 | if (entry_start > buffer + length - 2) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1697 | { |
Glenn Randers-Pehrson | 4accabb | 2000-04-14 14:20:47 -0500 | [diff] [blame] | 1698 | png_warning(png_ptr, "malformed sPLT chunk"); |
| 1699 | return; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1700 | } |
| 1701 | |
| 1702 | new_palette.depth = *entry_start++; |
Glenn Randers-Pehrson | a565f0e | 2010-03-06 08:24:45 -0600 | [diff] [blame] | 1703 | entry_size = (new_palette.depth == 8 ? 6 : 10); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1704 | /* 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] | 1705 | * chunk data length. |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1706 | */ |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 1707 | data_length = length - (png_uint_32)(entry_start - buffer); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1708 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 1709 | /* Integrity-check the data length */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1710 | if (data_length % entry_size) |
| 1711 | { |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 1712 | png_warning(png_ptr, "sPLT chunk has bad length"); |
| 1713 | return; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1714 | } |
| 1715 | |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1716 | dl = (png_int_32)(data_length / entry_size); |
| 1717 | max_dl = PNG_SIZE_MAX / png_sizeof(png_sPLT_entry); |
| 1718 | |
| 1719 | if (dl > max_dl) |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 1720 | { |
| 1721 | png_warning(png_ptr, "sPLT chunk too long"); |
| 1722 | return; |
| 1723 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1724 | |
| 1725 | new_palette.nentries = (png_int_32)(data_length / entry_size); |
| 1726 | |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 1727 | new_palette.entries = (png_sPLT_entryp)png_malloc_warn( |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1728 | png_ptr, new_palette.nentries * png_sizeof(png_sPLT_entry)); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1729 | |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 1730 | if (new_palette.entries == NULL) |
| 1731 | { |
| 1732 | png_warning(png_ptr, "sPLT chunk requires too much memory"); |
| 1733 | return; |
| 1734 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1735 | |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 1736 | #ifdef PNG_POINTER_INDEXING_SUPPORTED |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1737 | for (i = 0; i < new_palette.nentries; i++) |
| 1738 | { |
Glenn Randers-Pehrson | 90b878c | 2009-10-07 12:44:35 -0500 | [diff] [blame] | 1739 | pp = new_palette.entries + i; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1740 | |
| 1741 | if (new_palette.depth == 8) |
| 1742 | { |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1743 | pp->red = *entry_start++; |
| 1744 | pp->green = *entry_start++; |
| 1745 | pp->blue = *entry_start++; |
| 1746 | pp->alpha = *entry_start++; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1747 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1748 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1749 | else |
| 1750 | { |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1751 | pp->red = png_get_uint_16(entry_start); entry_start += 2; |
| 1752 | pp->green = png_get_uint_16(entry_start); entry_start += 2; |
| 1753 | pp->blue = png_get_uint_16(entry_start); entry_start += 2; |
| 1754 | pp->alpha = png_get_uint_16(entry_start); entry_start += 2; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1755 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1756 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1757 | pp->frequency = png_get_uint_16(entry_start); entry_start += 2; |
| 1758 | } |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 1759 | #else |
| 1760 | pp = new_palette.entries; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1761 | |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 1762 | for (i = 0; i < new_palette.nentries; i++) |
| 1763 | { |
| 1764 | |
| 1765 | if (new_palette.depth == 8) |
| 1766 | { |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1767 | pp[i].red = *entry_start++; |
| 1768 | pp[i].green = *entry_start++; |
| 1769 | pp[i].blue = *entry_start++; |
| 1770 | pp[i].alpha = *entry_start++; |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 1771 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1772 | |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 1773 | else |
| 1774 | { |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1775 | pp[i].red = png_get_uint_16(entry_start); entry_start += 2; |
| 1776 | pp[i].green = png_get_uint_16(entry_start); entry_start += 2; |
| 1777 | pp[i].blue = png_get_uint_16(entry_start); entry_start += 2; |
| 1778 | 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] | 1779 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1780 | |
Glenn Randers-Pehrson | f27592a | 2011-03-21 18:05:40 -0500 | [diff] [blame] | 1781 | 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] | 1782 | } |
| 1783 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1784 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 1785 | /* Discard all chunk data except the name and stash that */ |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 1786 | new_palette.name = (png_charp)buffer; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1787 | |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 1788 | png_set_sPLT(png_ptr, info_ptr, &new_palette, 1); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1789 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1790 | png_free(png_ptr, new_palette.entries); |
| 1791 | } |
| 1792 | #endif /* PNG_READ_sPLT_SUPPORTED */ |
| 1793 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1794 | #ifdef PNG_READ_tRNS_SUPPORTED |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1795 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 1796 | 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] | 1797 | { |
Glenn Randers-Pehrson | d1e8c86 | 2002-06-20 06:54:34 -0500 | [diff] [blame] | 1798 | png_byte readbuf[PNG_MAX_PALETTE_LENGTH]; |
Glenn Randers-Pehrson | 76e5fd6 | 2000-12-28 07:50:05 -0600 | [diff] [blame] | 1799 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1800 | png_debug(1, "in png_handle_tRNS"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1801 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1802 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 1803 | png_chunk_error(png_ptr, "missing IHDR"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1804 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1805 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 1806 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1807 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 1808 | png_chunk_benign_error(png_ptr, "out of place"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1809 | return; |
| 1810 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1811 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1812 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1813 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1814 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 1815 | png_chunk_benign_error(png_ptr, "duplicate"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1816 | return; |
| 1817 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1818 | |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1819 | if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1820 | { |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1821 | png_byte buf[2]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1822 | |
| 1823 | if (length != 2) |
| 1824 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1825 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 1826 | png_chunk_benign_error(png_ptr, "invalid"); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1827 | return; |
| 1828 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1829 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1830 | png_crc_read(png_ptr, buf, 2); |
| 1831 | png_ptr->num_trans = 1; |
Glenn Randers-Pehrson | 56f6396 | 2008-10-06 10:16:17 -0500 | [diff] [blame] | 1832 | png_ptr->trans_color.gray = png_get_uint_16(buf); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1833 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1834 | |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1835 | else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB) |
| 1836 | { |
| 1837 | png_byte buf[6]; |
| 1838 | |
| 1839 | if (length != 6) |
| 1840 | { |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1841 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 1842 | png_chunk_benign_error(png_ptr, "invalid"); |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1843 | return; |
| 1844 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1845 | |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 1846 | png_crc_read(png_ptr, buf, length); |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1847 | png_ptr->num_trans = 1; |
Glenn Randers-Pehrson | 56f6396 | 2008-10-06 10:16:17 -0500 | [diff] [blame] | 1848 | png_ptr->trans_color.red = png_get_uint_16(buf); |
| 1849 | png_ptr->trans_color.green = png_get_uint_16(buf + 2); |
| 1850 | png_ptr->trans_color.blue = png_get_uint_16(buf + 4); |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1851 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1852 | |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1853 | else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 1854 | { |
| 1855 | if (!(png_ptr->mode & PNG_HAVE_PLTE)) |
| 1856 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 1857 | /* TODO: is this actually an error in the ISO spec? */ |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1858 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 1859 | png_chunk_benign_error(png_ptr, "out of place"); |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1860 | return; |
| 1861 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1862 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 1863 | if (length > png_ptr->num_palette || length > PNG_MAX_PALETTE_LENGTH || |
| 1864 | length == 0) |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1865 | { |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [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, "invalid"); |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1868 | return; |
| 1869 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1870 | |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 1871 | png_crc_read(png_ptr, readbuf, length); |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1872 | png_ptr->num_trans = (png_uint_16)length; |
| 1873 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1874 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1875 | else |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1876 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1877 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 1878 | png_chunk_benign_error(png_ptr, "invalid with alpha channel"); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1879 | return; |
| 1880 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1881 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1882 | if (png_crc_finish(png_ptr, 0)) |
Glenn Randers-Pehrson | a7dbcba | 2007-05-15 16:16:34 -0500 | [diff] [blame] | 1883 | { |
| 1884 | png_ptr->num_trans = 0; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1885 | return; |
Glenn Randers-Pehrson | a7dbcba | 2007-05-15 16:16:34 -0500 | [diff] [blame] | 1886 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1887 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 1888 | /* TODO: this is a horrible side effect in the palette case because the |
| 1889 | * png_struct ends up with a pointer to the tRNS buffer owned by the |
| 1890 | * png_info. Fix this. |
| 1891 | */ |
Glenn Randers-Pehrson | 76e5fd6 | 2000-12-28 07:50:05 -0600 | [diff] [blame] | 1892 | 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] | 1893 | &(png_ptr->trans_color)); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1894 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1895 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1896 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1897 | #ifdef PNG_READ_bKGD_SUPPORTED |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1898 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 1899 | 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] | 1900 | { |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 1901 | unsigned int truelen; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1902 | png_byte buf[6]; |
John Bowler | cb0b296 | 2011-05-12 21:48:29 -0500 | [diff] [blame] | 1903 | png_color_16 background; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1904 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1905 | png_debug(1, "in png_handle_bKGD"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1906 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1907 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 1908 | png_chunk_error(png_ptr, "missing IHDR"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1909 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 1910 | else if ((png_ptr->mode & PNG_HAVE_IDAT) || |
| 1911 | (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && |
| 1912 | !(png_ptr->mode & PNG_HAVE_PLTE))) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1913 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1914 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 1915 | png_chunk_benign_error(png_ptr, "out of place"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1916 | return; |
| 1917 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1918 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1919 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1920 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1921 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 1922 | png_chunk_benign_error(png_ptr, "duplicate"); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1923 | return; |
| 1924 | } |
| 1925 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1926 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 1927 | truelen = 1; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1928 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1929 | else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) |
| 1930 | truelen = 6; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1931 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1932 | else |
| 1933 | truelen = 2; |
| 1934 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1935 | if (length != truelen) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1936 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1937 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 1938 | png_chunk_benign_error(png_ptr, "invalid"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1939 | return; |
| 1940 | } |
| 1941 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1942 | png_crc_read(png_ptr, buf, truelen); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1943 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1944 | if (png_crc_finish(png_ptr, 0)) |
| 1945 | return; |
| 1946 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1947 | /* We convert the index value into RGB components so that we can allow |
| 1948 | * arbitrary RGB values for background when we have transparency, and |
| 1949 | * 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] | 1950 | * from the info_ptr struct. |
| 1951 | */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1952 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1953 | { |
John Bowler | cb0b296 | 2011-05-12 21:48:29 -0500 | [diff] [blame] | 1954 | background.index = buf[0]; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1955 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1956 | if (info_ptr && info_ptr->num_palette) |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1957 | { |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1958 | if (buf[0] >= info_ptr->num_palette) |
| 1959 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 1960 | png_chunk_benign_error(png_ptr, "invalid index"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1961 | return; |
| 1962 | } |
| 1963 | |
John Bowler | cb0b296 | 2011-05-12 21:48:29 -0500 | [diff] [blame] | 1964 | background.red = (png_uint_16)png_ptr->palette[buf[0]].red; |
| 1965 | background.green = (png_uint_16)png_ptr->palette[buf[0]].green; |
| 1966 | background.blue = (png_uint_16)png_ptr->palette[buf[0]].blue; |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1967 | } |
John Bowler | cb0b296 | 2011-05-12 21:48:29 -0500 | [diff] [blame] | 1968 | |
| 1969 | else |
| 1970 | background.red = background.green = background.blue = 0; |
| 1971 | |
| 1972 | background.gray = 0; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1973 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1974 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1975 | else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) /* GRAY */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1976 | { |
John Bowler | cb0b296 | 2011-05-12 21:48:29 -0500 | [diff] [blame] | 1977 | background.index = 0; |
| 1978 | background.red = |
| 1979 | background.green = |
| 1980 | background.blue = |
| 1981 | background.gray = png_get_uint_16(buf); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1982 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 1983 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1984 | else |
| 1985 | { |
John Bowler | cb0b296 | 2011-05-12 21:48:29 -0500 | [diff] [blame] | 1986 | background.index = 0; |
| 1987 | background.red = png_get_uint_16(buf); |
| 1988 | background.green = png_get_uint_16(buf + 2); |
| 1989 | background.blue = png_get_uint_16(buf + 4); |
| 1990 | background.gray = 0; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1991 | } |
| 1992 | |
John Bowler | cb0b296 | 2011-05-12 21:48:29 -0500 | [diff] [blame] | 1993 | png_set_bKGD(png_ptr, info_ptr, &background); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1994 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1995 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1996 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1997 | #ifdef PNG_READ_hIST_SUPPORTED |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1998 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 1999 | 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] | 2000 | { |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 2001 | unsigned int num, i; |
Glenn Randers-Pehrson | d1e8c86 | 2002-06-20 06:54:34 -0500 | [diff] [blame] | 2002 | png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2003 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2004 | png_debug(1, "in png_handle_hIST"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2005 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2006 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2007 | png_chunk_error(png_ptr, "missing IHDR"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2008 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2009 | 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] | 2010 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2011 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2012 | png_chunk_benign_error(png_ptr, "out of place"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2013 | return; |
| 2014 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2015 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2016 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2017 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2018 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2019 | png_chunk_benign_error(png_ptr, "duplicate"); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2020 | return; |
| 2021 | } |
| 2022 | |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 2023 | num = length / 2 ; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2024 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2025 | if (num != png_ptr->num_palette || num > PNG_MAX_PALETTE_LENGTH) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2026 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2027 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2028 | png_chunk_benign_error(png_ptr, "invalid"); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2029 | return; |
| 2030 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2031 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2032 | for (i = 0; i < num; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2033 | { |
| 2034 | png_byte buf[2]; |
| 2035 | |
| 2036 | png_crc_read(png_ptr, buf, 2); |
Glenn Randers-Pehrson | 76e5fd6 | 2000-12-28 07:50:05 -0600 | [diff] [blame] | 2037 | readbuf[i] = png_get_uint_16(buf); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2038 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2039 | |
| 2040 | if (png_crc_finish(png_ptr, 0)) |
| 2041 | return; |
| 2042 | |
Glenn Randers-Pehrson | 76e5fd6 | 2000-12-28 07:50:05 -0600 | [diff] [blame] | 2043 | png_set_hIST(png_ptr, info_ptr, readbuf); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2044 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2045 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2046 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 2047 | #ifdef PNG_READ_pHYs_SUPPORTED |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2048 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 2049 | 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] | 2050 | { |
| 2051 | png_byte buf[9]; |
| 2052 | png_uint_32 res_x, res_y; |
| 2053 | int unit_type; |
| 2054 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2055 | png_debug(1, "in png_handle_pHYs"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2056 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2057 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2058 | png_chunk_error(png_ptr, "missing IHDR"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2059 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2060 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 2061 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2062 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2063 | png_chunk_benign_error(png_ptr, "out of place"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2064 | return; |
| 2065 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2066 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2067 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2068 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2069 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2070 | png_chunk_benign_error(png_ptr, "duplicate"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2071 | return; |
| 2072 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2073 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2074 | if (length != 9) |
| 2075 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2076 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2077 | png_chunk_benign_error(png_ptr, "invalid"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2078 | return; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2079 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2080 | |
| 2081 | png_crc_read(png_ptr, buf, 9); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2082 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2083 | if (png_crc_finish(png_ptr, 0)) |
| 2084 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2085 | |
| 2086 | res_x = png_get_uint_32(buf); |
| 2087 | res_y = png_get_uint_32(buf + 4); |
| 2088 | unit_type = buf[8]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2089 | 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] | 2090 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2091 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2092 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 2093 | #ifdef PNG_READ_oFFs_SUPPORTED |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2094 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 2095 | 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] | 2096 | { |
| 2097 | png_byte buf[9]; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2098 | png_int_32 offset_x, offset_y; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2099 | int unit_type; |
| 2100 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2101 | png_debug(1, "in png_handle_oFFs"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2102 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2103 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2104 | png_chunk_error(png_ptr, "missing IHDR"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2105 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2106 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 2107 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2108 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2109 | png_chunk_benign_error(png_ptr, "out of place"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2110 | return; |
| 2111 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2112 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2113 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2114 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2115 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2116 | png_chunk_benign_error(png_ptr, "duplicate"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2117 | return; |
| 2118 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2119 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2120 | if (length != 9) |
| 2121 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2122 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2123 | png_chunk_benign_error(png_ptr, "invalid"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2124 | return; |
| 2125 | } |
| 2126 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2127 | png_crc_read(png_ptr, buf, 9); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2128 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2129 | if (png_crc_finish(png_ptr, 0)) |
| 2130 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2131 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2132 | offset_x = png_get_int_32(buf); |
| 2133 | offset_y = png_get_int_32(buf + 4); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2134 | unit_type = buf[8]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2135 | png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type); |
| 2136 | } |
| 2137 | #endif |
| 2138 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 2139 | #ifdef PNG_READ_pCAL_SUPPORTED |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 2140 | /* Read the pCAL chunk (described in the PNG Extensions document) */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2141 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 2142 | 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] | 2143 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2144 | png_int_32 X0, X1; |
| 2145 | png_byte type, nparams; |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2146 | png_bytep buffer, buf, units, endptr; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2147 | png_charpp params; |
| 2148 | int i; |
| 2149 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2150 | png_debug(1, "in png_handle_pCAL"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2151 | |
| 2152 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2153 | png_chunk_error(png_ptr, "missing IHDR"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2154 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2155 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 2156 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2157 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2158 | png_chunk_benign_error(png_ptr, "out of place"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2159 | return; |
| 2160 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2161 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2162 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2163 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2164 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2165 | png_chunk_benign_error(png_ptr, "duplicate"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2166 | return; |
| 2167 | } |
| 2168 | |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2169 | png_debug1(2, "Allocating and reading pCAL chunk data (%u bytes)", |
| 2170 | length + 1); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2171 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2172 | buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/); |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2173 | |
| 2174 | if (buffer == NULL) |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2175 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2176 | png_crc_finish(png_ptr, length); |
| 2177 | png_chunk_benign_error(png_ptr, "out of memory"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2178 | return; |
| 2179 | } |
| 2180 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2181 | png_crc_read(png_ptr, buffer, length); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2182 | |
| 2183 | if (png_crc_finish(png_ptr, 0)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2184 | return; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2185 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2186 | buffer[length] = 0; /* Null terminate the last string */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2187 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2188 | png_debug(3, "Finding end of pCAL purpose string"); |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2189 | for (buf = buffer; *buf; buf++) |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 2190 | /* Empty loop */ ; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2191 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2192 | endptr = buffer + length; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2193 | |
| 2194 | /* 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] | 2195 | * in order to get the parameter information. |
| 2196 | */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2197 | if (endptr <= buf + 12) |
| 2198 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2199 | png_chunk_benign_error(png_ptr, "invalid"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2200 | return; |
| 2201 | } |
| 2202 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2203 | png_debug(3, "Reading pCAL X0, X1, type, nparams, and units"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2204 | X0 = png_get_int_32((png_bytep)buf+1); |
| 2205 | X1 = png_get_int_32((png_bytep)buf+5); |
| 2206 | type = buf[9]; |
| 2207 | nparams = buf[10]; |
| 2208 | units = buf + 11; |
| 2209 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2210 | png_debug(3, "Checking pCAL equation type and number of parameters"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2211 | /* Check that we have the right number of parameters for known |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2212 | * equation types. |
| 2213 | */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2214 | if ((type == PNG_EQUATION_LINEAR && nparams != 2) || |
| 2215 | (type == PNG_EQUATION_BASE_E && nparams != 3) || |
| 2216 | (type == PNG_EQUATION_ARBITRARY && nparams != 3) || |
| 2217 | (type == PNG_EQUATION_HYPERBOLIC && nparams != 4)) |
| 2218 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2219 | png_chunk_benign_error(png_ptr, "invalid parameter count"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2220 | return; |
| 2221 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2222 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2223 | else if (type >= PNG_EQUATION_LAST) |
| 2224 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2225 | png_chunk_benign_error(png_ptr, "unrecognized equation type"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2226 | } |
| 2227 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2228 | for (buf = units; *buf; buf++) |
Glenn Randers-Pehrson | f9f2fe0 | 1998-03-15 18:20:23 -0600 | [diff] [blame] | 2229 | /* Empty loop to move past the units string. */ ; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2230 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2231 | png_debug(3, "Allocating pCAL parameters array"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2232 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2233 | params = png_voidcast(png_charpp, png_malloc_warn(png_ptr, |
| 2234 | nparams * png_sizeof(png_charp))); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2235 | |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 2236 | if (params == NULL) |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2237 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2238 | png_chunk_benign_error(png_ptr, "out of memory"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2239 | return; |
| 2240 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2241 | |
| 2242 | /* Get pointers to the start of each parameter string. */ |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2243 | for (i = 0; i < nparams; i++) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2244 | { |
| 2245 | buf++; /* Skip the null string terminator from previous parameter. */ |
| 2246 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2247 | png_debug1(3, "Reading pCAL parameter %d", i); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2248 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2249 | for (params[i] = (png_charp)buf; buf <= endptr && *buf != 0; buf++) |
Glenn Randers-Pehrson | f9f2fe0 | 1998-03-15 18:20:23 -0600 | [diff] [blame] | 2250 | /* Empty loop to move past each parameter string */ ; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2251 | |
| 2252 | /* Make sure we haven't run out of data yet */ |
| 2253 | if (buf > endptr) |
| 2254 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2255 | png_free(png_ptr, params); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2256 | png_chunk_benign_error(png_ptr, "invalid data"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2257 | return; |
| 2258 | } |
| 2259 | } |
| 2260 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2261 | png_set_pCAL(png_ptr, info_ptr, (png_charp)buffer, X0, X1, type, nparams, |
| 2262 | (png_charp)units, params); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2263 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2264 | png_free(png_ptr, params); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2265 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2266 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2267 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 2268 | #ifdef PNG_READ_sCAL_SUPPORTED |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 2269 | /* Read the sCAL chunk */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2270 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 2271 | 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] | 2272 | { |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2273 | png_bytep buffer; |
| 2274 | png_size_t i; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2275 | int state; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2276 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2277 | png_debug(1, "in png_handle_sCAL"); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2278 | |
| 2279 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2280 | png_chunk_error(png_ptr, "missing IHDR"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2281 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2282 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 2283 | { |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2284 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2285 | png_chunk_benign_error(png_ptr, "out of place"); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2286 | return; |
| 2287 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2288 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2289 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL)) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2290 | { |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2291 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2292 | png_chunk_benign_error(png_ptr, "duplicate"); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2293 | return; |
| 2294 | } |
| 2295 | |
Glenn Randers-Pehrson | 254a513 | 2011-06-10 17:45:48 -0500 | [diff] [blame] | 2296 | /* Need unit type, width, \0, height: minimum 4 bytes */ |
| 2297 | else if (length < 4) |
| 2298 | { |
Glenn Randers-Pehrson | 254a513 | 2011-06-10 17:45:48 -0500 | [diff] [blame] | 2299 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2300 | png_chunk_benign_error(png_ptr, "invalid"); |
Glenn Randers-Pehrson | 254a513 | 2011-06-10 17:45:48 -0500 | [diff] [blame] | 2301 | return; |
| 2302 | } |
| 2303 | |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2304 | png_debug1(2, "Allocating and reading sCAL chunk data (%u bytes)", |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2305 | length + 1); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2306 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2307 | buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2308 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2309 | if (buffer == NULL) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2310 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2311 | png_chunk_benign_error(png_ptr, "out of memory"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2312 | png_crc_finish(png_ptr, length); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2313 | return; |
| 2314 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2315 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2316 | png_crc_read(png_ptr, buffer, length); |
| 2317 | buffer[length] = 0; /* Null terminate the last string */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2318 | |
| 2319 | if (png_crc_finish(png_ptr, 0)) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2320 | return; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2321 | |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2322 | /* Validate the unit. */ |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2323 | if (buffer[0] != 1 && buffer[0] != 2) |
Glenn Randers-Pehrson | 4accabb | 2000-04-14 14:20:47 -0500 | [diff] [blame] | 2324 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2325 | png_chunk_benign_error(png_ptr, "invalid unit"); |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 2326 | return; |
| 2327 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2328 | |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2329 | /* Validate the ASCII numbers, need two ASCII numbers separated by |
| 2330 | * a '\0' and they need to fit exactly in the chunk data. |
| 2331 | */ |
Glenn Randers-Pehrson | 254a513 | 2011-06-10 17:45:48 -0500 | [diff] [blame] | 2332 | i = 1; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2333 | state = 0; |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 2334 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2335 | if (!png_check_fp_number((png_const_charp)buffer, length, &state, &i) || |
| 2336 | i >= length || buffer[i++] != 0) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2337 | png_chunk_benign_error(png_ptr, "bad width format"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2338 | |
John Bowler | 8d26126 | 2011-06-18 13:37:11 -0500 | [diff] [blame] | 2339 | else if (!PNG_FP_IS_POSITIVE(state)) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2340 | png_chunk_benign_error(png_ptr, "non-positive width"); |
John Bowler | 8d26126 | 2011-06-18 13:37:11 -0500 | [diff] [blame] | 2341 | |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2342 | else |
Glenn Randers-Pehrson | 20788d3 | 2011-01-06 09:01:04 -0600 | [diff] [blame] | 2343 | { |
John Bowler | 168a433 | 2011-01-16 19:32:22 -0600 | [diff] [blame] | 2344 | png_size_t heighti = i; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2345 | |
Glenn Randers-Pehrson | 254a513 | 2011-06-10 17:45:48 -0500 | [diff] [blame] | 2346 | state = 0; |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2347 | if (!png_check_fp_number((png_const_charp)buffer, length, &state, &i) || |
| 2348 | i != length) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2349 | png_chunk_benign_error(png_ptr, "bad height format"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2350 | |
John Bowler | 8d26126 | 2011-06-18 13:37:11 -0500 | [diff] [blame] | 2351 | else if (!PNG_FP_IS_POSITIVE(state)) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2352 | png_chunk_benign_error(png_ptr, "non-positive height"); |
John Bowler | 8d26126 | 2011-06-18 13:37:11 -0500 | [diff] [blame] | 2353 | |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2354 | else |
| 2355 | /* This is the (only) success case. */ |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2356 | png_set_sCAL_s(png_ptr, info_ptr, buffer[0], |
| 2357 | (png_charp)buffer+1, (png_charp)buffer+heighti); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2358 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2359 | } |
| 2360 | #endif |
| 2361 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 2362 | #ifdef PNG_READ_tIME_SUPPORTED |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2363 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 2364 | 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] | 2365 | { |
| 2366 | png_byte buf[7]; |
| 2367 | png_time mod_time; |
| 2368 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2369 | png_debug(1, "in png_handle_tIME"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2370 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2371 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2372 | png_chunk_error(png_ptr, "missing IHDR"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2373 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2374 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2375 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2376 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2377 | png_chunk_benign_error(png_ptr, "duplicate"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2378 | return; |
| 2379 | } |
| 2380 | |
| 2381 | if (png_ptr->mode & PNG_HAVE_IDAT) |
| 2382 | png_ptr->mode |= PNG_AFTER_IDAT; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2383 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2384 | if (length != 7) |
| 2385 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2386 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2387 | png_chunk_benign_error(png_ptr, "invalid"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2388 | return; |
| 2389 | } |
| 2390 | |
| 2391 | png_crc_read(png_ptr, buf, 7); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2392 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2393 | if (png_crc_finish(png_ptr, 0)) |
| 2394 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2395 | |
| 2396 | mod_time.second = buf[6]; |
| 2397 | mod_time.minute = buf[5]; |
| 2398 | mod_time.hour = buf[4]; |
| 2399 | mod_time.day = buf[3]; |
| 2400 | mod_time.month = buf[2]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2401 | mod_time.year = png_get_uint_16(buf); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2402 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2403 | png_set_tIME(png_ptr, info_ptr, &mod_time); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2404 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2405 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2406 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 2407 | #ifdef PNG_READ_tEXt_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2408 | /* 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] | 2409 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 2410 | 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] | 2411 | { |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2412 | png_text text_info; |
| 2413 | png_bytep buffer; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2414 | png_charp key; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 2415 | png_charp text; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2416 | png_uint_32 skip = 0; |
| 2417 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2418 | png_debug(1, "in png_handle_tEXt"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2419 | |
Glenn Randers-Pehrson | e3f3c4e | 2010-02-07 18:08:50 -0600 | [diff] [blame] | 2420 | #ifdef PNG_USER_LIMITS_SUPPORTED |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 2421 | if (png_ptr->user_chunk_cache_max != 0) |
| 2422 | { |
| 2423 | if (png_ptr->user_chunk_cache_max == 1) |
| 2424 | { |
| 2425 | png_crc_finish(png_ptr, length); |
| 2426 | return; |
| 2427 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2428 | |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 2429 | if (--png_ptr->user_chunk_cache_max == 1) |
| 2430 | { |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 2431 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2432 | png_chunk_benign_error(png_ptr, "no space in chunk cache"); |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 2433 | return; |
| 2434 | } |
| 2435 | } |
| 2436 | #endif |
| 2437 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2438 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2439 | png_chunk_error(png_ptr, "missing IHDR"); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2440 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2441 | if (png_ptr->mode & PNG_HAVE_IDAT) |
| 2442 | png_ptr->mode |= PNG_AFTER_IDAT; |
| 2443 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2444 | #ifdef PNG_MAX_MALLOC_64K |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2445 | if (length > 65535U) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2446 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2447 | png_crc_finish(png_ptr, length); |
| 2448 | png_chunk_benign_error(png_ptr, "too large to fit in memory"); |
| 2449 | return; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2450 | } |
| 2451 | #endif |
| 2452 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2453 | buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/); |
Glenn Randers-Pehrson | 97a9b48 | 2008-10-25 20:03:28 -0500 | [diff] [blame] | 2454 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2455 | if (buffer == NULL) |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 2456 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2457 | png_chunk_benign_error(png_ptr, "out of memory"); |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 2458 | return; |
| 2459 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2460 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2461 | png_crc_read(png_ptr, buffer, length); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2462 | |
| 2463 | if (png_crc_finish(png_ptr, skip)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2464 | return; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2465 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2466 | key = (png_charp)buffer; |
| 2467 | key[length] = 0; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2468 | |
| 2469 | for (text = key; *text; text++) |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 2470 | /* Empty loop to find end of key */ ; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2471 | |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2472 | if (text != key + length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2473 | text++; |
| 2474 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2475 | text_info.compression = PNG_TEXT_COMPRESSION_NONE; |
| 2476 | text_info.key = key; |
| 2477 | text_info.lang = NULL; |
| 2478 | text_info.lang_key = NULL; |
| 2479 | text_info.itxt_length = 0; |
| 2480 | text_info.text = text; |
| 2481 | text_info.text_length = png_strlen(text); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2482 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2483 | 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] | 2484 | png_warning(png_ptr, "Insufficient memory to process text chunk"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2485 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2486 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2487 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 2488 | #ifdef PNG_READ_zTXt_SUPPORTED |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 2489 | /* 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] | 2490 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 2491 | 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] | 2492 | { |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2493 | png_const_charp errmsg = NULL; |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2494 | png_bytep buffer; |
| 2495 | png_uint_32 keyword_length; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2496 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2497 | png_debug(1, "in png_handle_zTXt"); |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 2498 | |
Glenn Randers-Pehrson | e3f3c4e | 2010-02-07 18:08:50 -0600 | [diff] [blame] | 2499 | #ifdef PNG_USER_LIMITS_SUPPORTED |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 2500 | if (png_ptr->user_chunk_cache_max != 0) |
| 2501 | { |
| 2502 | if (png_ptr->user_chunk_cache_max == 1) |
| 2503 | { |
| 2504 | png_crc_finish(png_ptr, length); |
| 2505 | return; |
| 2506 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2507 | |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 2508 | if (--png_ptr->user_chunk_cache_max == 1) |
| 2509 | { |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 2510 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2511 | png_chunk_benign_error(png_ptr, "no space in chunk cache"); |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 2512 | return; |
| 2513 | } |
| 2514 | } |
| 2515 | #endif |
| 2516 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2517 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2518 | png_chunk_error(png_ptr, "missing IHDR"); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2519 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2520 | if (png_ptr->mode & PNG_HAVE_IDAT) |
| 2521 | png_ptr->mode |= PNG_AFTER_IDAT; |
| 2522 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2523 | buffer = png_read_buffer(png_ptr, length, 2/*silent*/); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2524 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2525 | if (buffer == NULL) |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 2526 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2527 | png_crc_finish(png_ptr, length); |
| 2528 | png_chunk_benign_error(png_ptr, "out of memory"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2529 | return; |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 2530 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2531 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2532 | png_crc_read(png_ptr, buffer, length); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2533 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2534 | if (png_crc_finish(png_ptr, 0)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2535 | return; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2536 | |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2537 | /* TODO: also check that the keyword contents match the spec! */ |
| 2538 | for (keyword_length = 0; |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2539 | keyword_length < length && buffer[keyword_length] != 0; |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2540 | ++keyword_length) |
| 2541 | /* Empty loop to find end of name */ ; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2542 | |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2543 | if (keyword_length > 79 || keyword_length < 1) |
| 2544 | errmsg = "bad keyword"; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2545 | |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2546 | /* zTXt must have some LZ data after the keyword, although it may expand to |
| 2547 | * zero bytes; we need a '\0' at the end of the keyword, the compression type |
| 2548 | * then the LZ data: |
| 2549 | */ |
| 2550 | else if (keyword_length + 3 > length) |
| 2551 | errmsg = "truncated"; |
| 2552 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2553 | else if (buffer[keyword_length+1] != PNG_COMPRESSION_TYPE_BASE) |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2554 | errmsg = "unknown compression type"; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2555 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2556 | else |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2557 | { |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2558 | png_alloc_size_t uncompressed_length = PNG_SIZE_MAX; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2559 | |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2560 | /* TODO: at present png_decompress_chunk imposes a single application |
| 2561 | * level memory limit, this should be split to different values for iCCP |
| 2562 | * and text chunks. |
| 2563 | */ |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2564 | if (png_decompress_chunk(png_ptr, length, keyword_length+2, |
| 2565 | &uncompressed_length, 1/*terminate*/) == Z_STREAM_END) |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2566 | { |
| 2567 | png_text text; |
| 2568 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2569 | /* It worked; png_ptr->read_buffer now looks like a tEXt chunk except |
| 2570 | * 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] | 2571 | * necessarily '\0' terminated. |
| 2572 | */ |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2573 | buffer = png_ptr->read_buffer; |
| 2574 | buffer[uncompressed_length+(keyword_length+2)] = 0; |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2575 | |
| 2576 | text.compression = PNG_TEXT_COMPRESSION_zTXt; |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2577 | text.key = (png_charp)buffer; |
| 2578 | text.text = (png_charp)(buffer + keyword_length+2); |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2579 | text.text_length = uncompressed_length; |
| 2580 | text.itxt_length = 0; |
| 2581 | text.lang = NULL; |
| 2582 | text.lang_key = NULL; |
| 2583 | |
| 2584 | if (png_set_text_2(png_ptr, info_ptr, &text, 1)) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2585 | errmsg = "insufficient memory"; |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2586 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2587 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2588 | else |
| 2589 | errmsg = png_ptr->zstream.msg; |
| 2590 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2591 | |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2592 | if (errmsg != NULL) |
| 2593 | png_chunk_benign_error(png_ptr, errmsg); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2594 | } |
| 2595 | #endif |
| 2596 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 2597 | #ifdef PNG_READ_iTXt_SUPPORTED |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 2598 | /* 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] | 2599 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 2600 | 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] | 2601 | { |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2602 | png_const_charp errmsg = NULL; |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2603 | png_bytep buffer; |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2604 | png_uint_32 prefix_length; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2605 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2606 | png_debug(1, "in png_handle_iTXt"); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2607 | |
Glenn Randers-Pehrson | e3f3c4e | 2010-02-07 18:08:50 -0600 | [diff] [blame] | 2608 | #ifdef PNG_USER_LIMITS_SUPPORTED |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 2609 | if (png_ptr->user_chunk_cache_max != 0) |
| 2610 | { |
| 2611 | if (png_ptr->user_chunk_cache_max == 1) |
| 2612 | { |
| 2613 | png_crc_finish(png_ptr, length); |
| 2614 | return; |
| 2615 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2616 | |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 2617 | if (--png_ptr->user_chunk_cache_max == 1) |
| 2618 | { |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 2619 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2620 | png_chunk_benign_error(png_ptr, "no space in chunk cache"); |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 2621 | return; |
| 2622 | } |
| 2623 | } |
| 2624 | #endif |
| 2625 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2626 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2627 | png_chunk_error(png_ptr, "missing IHDR"); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2628 | |
| 2629 | if (png_ptr->mode & PNG_HAVE_IDAT) |
| 2630 | png_ptr->mode |= PNG_AFTER_IDAT; |
| 2631 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2632 | buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2633 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2634 | if (buffer == NULL) |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 2635 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2636 | png_crc_finish(png_ptr, length); |
| 2637 | png_chunk_benign_error(png_ptr, "out of memory"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2638 | return; |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 2639 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2640 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2641 | png_crc_read(png_ptr, buffer, length); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2642 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2643 | if (png_crc_finish(png_ptr, 0)) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2644 | return; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2645 | |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2646 | /* First the keyword. */ |
| 2647 | for (prefix_length=0; |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2648 | prefix_length < length && buffer[prefix_length] != 0; |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2649 | ++prefix_length) |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 2650 | /* Empty loop */ ; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2651 | |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2652 | /* Perform a basic check on the keyword length here. */ |
| 2653 | if (prefix_length > 79 || prefix_length < 1) |
| 2654 | errmsg = "bad keyword"; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2655 | |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2656 | /* Expect keyword, compression flag, compression type, language, translated |
| 2657 | * keyword (both may be empty but are 0 terminated) then the text, which may |
| 2658 | * be empty. |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 2659 | */ |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2660 | else if (prefix_length + 5 > length) |
| 2661 | errmsg = "truncated"; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2662 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2663 | else if (buffer[prefix_length+1] == 0 || |
| 2664 | (buffer[prefix_length+1] == 1 && |
| 2665 | buffer[prefix_length+2] == PNG_COMPRESSION_TYPE_BASE)) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2666 | { |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2667 | int compressed = buffer[prefix_length+1] != 0; |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2668 | png_uint_32 language_offset, translated_keyword_offset; |
| 2669 | png_alloc_size_t uncompressed_length = 0; |
| 2670 | |
| 2671 | /* Now the language tag */ |
| 2672 | prefix_length += 3; |
| 2673 | language_offset = prefix_length; |
| 2674 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2675 | for (; prefix_length < length && buffer[prefix_length] != 0; |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2676 | ++prefix_length) |
| 2677 | /* Empty loop */ ; |
| 2678 | |
| 2679 | /* WARNING: the length may be invalid here, this is checked below. */ |
| 2680 | translated_keyword_offset = ++prefix_length; |
| 2681 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2682 | for (; prefix_length < length && buffer[prefix_length] != 0; |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2683 | ++prefix_length) |
| 2684 | /* Empty loop */ ; |
| 2685 | |
| 2686 | /* prefix_length should now be at the trailing '\0' of the translated |
| 2687 | * keyword, but it may already be over the end. None of this arithmetic |
| 2688 | * can overflow because chunks are at most 2^31 bytes long, but on 16-bit |
| 2689 | * systems the available allocaton may overflow. |
| 2690 | */ |
| 2691 | ++prefix_length; |
| 2692 | |
| 2693 | if (!compressed && prefix_length <= length) |
| 2694 | uncompressed_length = length - prefix_length; |
| 2695 | |
| 2696 | else if (compressed && prefix_length < length) |
| 2697 | { |
| 2698 | uncompressed_length = PNG_SIZE_MAX; |
| 2699 | |
| 2700 | /* TODO: at present png_decompress_chunk imposes a single application |
| 2701 | * level memory limit, this should be split to different values for |
| 2702 | * iCCP and text chunks. |
| 2703 | */ |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2704 | if (png_decompress_chunk(png_ptr, length, prefix_length, |
| 2705 | &uncompressed_length, 1/*terminate*/) == Z_STREAM_END) |
| 2706 | buffer = png_ptr->read_buffer; |
| 2707 | |
| 2708 | else |
| 2709 | errmsg = png_ptr->zstream.msg; |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2710 | } |
| 2711 | |
| 2712 | else |
| 2713 | errmsg = "truncated"; |
| 2714 | |
| 2715 | if (errmsg == NULL) |
| 2716 | { |
| 2717 | png_text text; |
| 2718 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2719 | buffer[uncompressed_length+prefix_length] = 0; |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2720 | |
| 2721 | if (compressed) |
| 2722 | text.compression = PNG_ITXT_COMPRESSION_NONE; |
| 2723 | |
| 2724 | else |
| 2725 | text.compression = PNG_ITXT_COMPRESSION_zTXt; |
| 2726 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 2727 | text.key = (png_charp)buffer; |
| 2728 | text.lang = (png_charp)buffer + language_offset; |
| 2729 | text.lang_key = (png_charp)buffer + translated_keyword_offset; |
| 2730 | text.text = (png_charp)buffer + prefix_length; |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2731 | text.text_length = 0; |
| 2732 | text.itxt_length = uncompressed_length; |
| 2733 | |
| 2734 | if (png_set_text_2(png_ptr, info_ptr, &text, 1)) |
| 2735 | errmsg = "insufficient memory"; |
| 2736 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2737 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2738 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2739 | else |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2740 | errmsg = "bad compression info"; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2741 | |
John Bowler | 0ae4f7b | 2012-03-03 21:10:26 -0600 | [diff] [blame] | 2742 | if (errmsg != NULL) |
| 2743 | png_chunk_benign_error(png_ptr, errmsg); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2744 | } |
| 2745 | #endif |
| 2746 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2747 | /* 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] | 2748 | * chunk. If there isn't a problem with the chunk itself (ie bad |
| 2749 | * chunk name, CRC, or a critical chunk), the chunk is silently ignored |
| 2750 | * -- unless the PNG_FLAG_UNKNOWN_CHUNKS_SUPPORTED flag is on in which |
| 2751 | * case it will be saved away to be written out later. |
| 2752 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2753 | void /* PRIVATE */ |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2754 | png_handle_unknown(png_structrp png_ptr, png_inforp info_ptr, |
| 2755 | png_uint_32 length) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2756 | { |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2757 | png_uint_32 skip = 0; |
| 2758 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2759 | png_debug(1, "in png_handle_unknown"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2760 | |
Glenn Randers-Pehrson | e3f3c4e | 2010-02-07 18:08:50 -0600 | [diff] [blame] | 2761 | #ifdef PNG_USER_LIMITS_SUPPORTED |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 2762 | if (png_ptr->user_chunk_cache_max != 0) |
| 2763 | { |
| 2764 | if (png_ptr->user_chunk_cache_max == 1) |
| 2765 | { |
| 2766 | png_crc_finish(png_ptr, length); |
| 2767 | return; |
| 2768 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2769 | |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 2770 | if (--png_ptr->user_chunk_cache_max == 1) |
| 2771 | { |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 2772 | png_crc_finish(png_ptr, length); |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2773 | png_chunk_benign_error(png_ptr, "no space in chunk cache"); |
Glenn Randers-Pehrson | 800d1e9 | 2008-08-20 17:25:21 -0500 | [diff] [blame] | 2774 | return; |
| 2775 | } |
| 2776 | } |
| 2777 | #endif |
| 2778 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2779 | if (png_ptr->mode & PNG_HAVE_IDAT) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2780 | { |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 2781 | if (png_ptr->chunk_name != png_IDAT) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2782 | png_ptr->mode |= PNG_AFTER_IDAT; |
| 2783 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2784 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 2785 | if (PNG_CHUNK_CRITICAL(png_ptr->chunk_name)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2786 | { |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 2787 | #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 2788 | if (png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name) != |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2789 | PNG_HANDLE_CHUNK_ALWAYS |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 2790 | #ifdef PNG_READ_USER_CHUNKS_SUPPORTED |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2791 | && png_ptr->read_user_chunk_fn == NULL |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2792 | #endif |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2793 | ) |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 2794 | #endif |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2795 | png_chunk_error(png_ptr, "unknown critical chunk"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2796 | } |
| 2797 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 2798 | #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED |
Glenn Randers-Pehrson | 7824a70 | 2009-06-13 10:05:05 -0500 | [diff] [blame] | 2799 | if ((png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS) |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 2800 | #ifdef PNG_READ_USER_CHUNKS_SUPPORTED |
Glenn Randers-Pehrson | 7824a70 | 2009-06-13 10:05:05 -0500 | [diff] [blame] | 2801 | || (png_ptr->read_user_chunk_fn != NULL) |
| 2802 | #endif |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2803 | ) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2804 | { |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2805 | #ifdef PNG_MAX_MALLOC_64K |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 2806 | if (length > 65535) |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2807 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2808 | png_crc_finish(png_ptr, length); |
| 2809 | png_chunk_benign_error(png_ptr, |
| 2810 | "unknown chunk too large to fit in memory"); |
| 2811 | return; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2812 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2813 | #endif |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2814 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 2815 | /* TODO: this code is very close to the unknown handling in pngpread.c, |
| 2816 | * maybe it can be put into a common utility routine? |
| 2817 | * png_struct::unknown_chunk is just used as a temporary variable, along |
| 2818 | * with the data into which the chunk is read. These can be eliminated. |
| 2819 | */ |
| 2820 | 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] | 2821 | png_ptr->unknown_chunk.size = (png_size_t)length; |
| 2822 | |
| 2823 | if (length == 0) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2824 | png_ptr->unknown_chunk.data = NULL; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2825 | |
| 2826 | else |
| 2827 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2828 | 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] | 2829 | png_crc_read(png_ptr, png_ptr->unknown_chunk.data, length); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2830 | } |
| 2831 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 2832 | #ifdef PNG_READ_USER_CHUNKS_SUPPORTED |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2833 | if (png_ptr->read_user_chunk_fn != NULL) |
| 2834 | { |
| 2835 | /* Callback to user unknown chunk handler */ |
| 2836 | int ret; |
| 2837 | |
| 2838 | ret = (*(png_ptr->read_user_chunk_fn)) |
| 2839 | (png_ptr, &png_ptr->unknown_chunk); |
| 2840 | |
| 2841 | if (ret < 0) |
| 2842 | png_chunk_error(png_ptr, "error in user chunk"); |
| 2843 | |
| 2844 | if (ret == 0) |
| 2845 | { |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 2846 | if (PNG_CHUNK_CRITICAL(png_ptr->chunk_name)) |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2847 | { |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 2848 | #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 2849 | if (png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name) != |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2850 | PNG_HANDLE_CHUNK_ALWAYS) |
Glenn Randers-Pehrson | 7824a70 | 2009-06-13 10:05:05 -0500 | [diff] [blame] | 2851 | #endif |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2852 | png_chunk_error(png_ptr, "unknown critical chunk"); |
| 2853 | } |
| 2854 | |
| 2855 | png_set_unknown_chunks(png_ptr, info_ptr, |
| 2856 | &png_ptr->unknown_chunk, 1); |
| 2857 | } |
| 2858 | } |
| 2859 | |
| 2860 | else |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2861 | #endif |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2862 | png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1); |
| 2863 | |
| 2864 | png_free(png_ptr, png_ptr->unknown_chunk.data); |
| 2865 | png_ptr->unknown_chunk.data = NULL; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2866 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2867 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2868 | else |
| 2869 | #endif |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2870 | skip = length; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2871 | |
| 2872 | png_crc_finish(png_ptr, skip); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2873 | |
Glenn Randers-Pehrson | b2aca21 | 2009-09-23 11:32:37 -0500 | [diff] [blame] | 2874 | #ifndef PNG_READ_USER_CHUNKS_SUPPORTED |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2875 | PNG_UNUSED(info_ptr) /* Quiet compiler warnings about unused info_ptr */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2876 | #endif |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2877 | } |
| 2878 | |
| 2879 | /* 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] | 2880 | * This function can't have the "critical chunk check" incorporated |
| 2881 | * into it, since in the future we will need to be able to call user |
| 2882 | * functions to handle unknown critical chunks after we check that |
| 2883 | * the chunk name itself is valid. |
| 2884 | */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2885 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 2886 | /* Bit hacking: the test for an invalid byte in the 4 byte chunk name is: |
| 2887 | * |
| 2888 | * ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97)) |
| 2889 | */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2890 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2891 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 2892 | 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] | 2893 | { |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 2894 | int i; |
| 2895 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2896 | png_debug(1, "in png_check_chunk_name"); |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 2897 | |
| 2898 | for (i=1; i<=4; ++i) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2899 | { |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 2900 | int c = chunk_name & 0xff; |
| 2901 | |
| 2902 | if (c < 65 || c > 122 || (c > 90 && c < 97)) |
| 2903 | png_chunk_error(png_ptr, "invalid chunk type"); |
| 2904 | |
| 2905 | chunk_name >>= 8; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2906 | } |
| 2907 | } |
| 2908 | |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2909 | /* Combines the row recently read in with the existing pixels in the row. This |
| 2910 | * routine takes care of alpha and transparency if requested. This routine also |
| 2911 | * handles the two methods of progressive display of interlaced images, |
| 2912 | * depending on the 'display' value; if 'display' is true then the whole row |
| 2913 | * (dp) is filled from the start by replicating the available pixels. If |
| 2914 | * '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] | 2915 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2916 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 2917 | 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] | 2918 | { |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 2919 | unsigned int pixel_depth = png_ptr->transformed_pixel_depth; |
| 2920 | png_const_bytep sp = png_ptr->row_buf + 1; |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 2921 | png_uint_32 row_width = png_ptr->width; |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 2922 | unsigned int pass = png_ptr->pass; |
John Bowler | fb5b3ac | 2011-10-16 22:52:56 -0500 | [diff] [blame] | 2923 | png_bytep end_ptr = 0; |
| 2924 | png_byte end_byte = 0; |
| 2925 | unsigned int end_mask; |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 2926 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2927 | png_debug(1, "in png_combine_row"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2928 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 2929 | /* Added in 1.5.6: it should not be possible to enter this routine until at |
| 2930 | * least one row has been read from the PNG data and transformed. |
| 2931 | */ |
| 2932 | if (pixel_depth == 0) |
| 2933 | png_error(png_ptr, "internal row logic error"); |
| 2934 | |
| 2935 | /* Added in 1.5.4: the pixel depth should match the information returned by |
| 2936 | * 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] | 2937 | * this wrong. |
| 2938 | */ |
| 2939 | if (png_ptr->info_rowbytes != 0 && png_ptr->info_rowbytes != |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 2940 | PNG_ROWBYTES(pixel_depth, row_width)) |
John Bowler | 9994f25 | 2011-05-15 18:52:39 -0500 | [diff] [blame] | 2941 | png_error(png_ptr, "internal row size calculation error"); |
| 2942 | |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 2943 | /* Don't expect this to ever happen: */ |
| 2944 | if (row_width == 0) |
| 2945 | png_error(png_ptr, "internal row width error"); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 2946 | |
John Bowler | fb5b3ac | 2011-10-16 22:52:56 -0500 | [diff] [blame] | 2947 | /* Preserve the last byte in cases where only part of it will be overwritten, |
| 2948 | * the multiply below may overflow, we don't care because ANSI-C guarantees |
| 2949 | * we get the low bits. |
| 2950 | */ |
| 2951 | end_mask = (pixel_depth * row_width) & 7; |
| 2952 | if (end_mask != 0) |
| 2953 | { |
Glenn Randers-Pehrson | ef02d56 | 2011-10-27 12:05:58 -0500 | [diff] [blame] | 2954 | /* end_ptr == NULL is a flag to say do nothing */ |
John Bowler | fb5b3ac | 2011-10-16 22:52:56 -0500 | [diff] [blame] | 2955 | end_ptr = dp + PNG_ROWBYTES(pixel_depth, row_width) - 1; |
| 2956 | end_byte = *end_ptr; |
| 2957 | # ifdef PNG_READ_PACKSWAP_SUPPORTED |
| 2958 | if (png_ptr->transformations & PNG_PACKSWAP) /* little-endian byte */ |
| 2959 | end_mask = 0xff << end_mask; |
| 2960 | |
| 2961 | else /* big-endian byte */ |
| 2962 | # endif |
| 2963 | end_mask = 0xff >> end_mask; |
| 2964 | /* end_mask is now the bits to *keep* from the destination row */ |
| 2965 | } |
| 2966 | |
Glenn Randers-Pehrson | ef02d56 | 2011-10-27 12:05:58 -0500 | [diff] [blame] | 2967 | /* For non-interlaced images this reduces to a png_memcpy(). A png_memcpy() |
| 2968 | * will also happen if interlacing isn't supported or if the application |
| 2969 | * does not call png_set_interlace_handling(). In the latter cases the |
| 2970 | * caller just gets a sequence of the unexpanded rows from each interlace |
| 2971 | * pass. |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 2972 | */ |
| 2973 | #ifdef PNG_READ_INTERLACING_SUPPORTED |
| 2974 | if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE) && |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 2975 | pass < 6 && (display == 0 || |
| 2976 | /* The following copies everything for 'display' on passes 0, 2 and 4. */ |
| 2977 | (display == 1 && (pass & 1) != 0))) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2978 | { |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 2979 | /* Narrow images may have no bits in a pass; the caller should handle |
| 2980 | * this, but this test is cheap: |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 2981 | */ |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 2982 | if (row_width <= PNG_PASS_START_COL(pass)) |
| 2983 | return; |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 2984 | |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 2985 | if (pixel_depth < 8) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2986 | { |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2987 | /* 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] | 2988 | * 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] | 2989 | * 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] | 2990 | * expanded mask may also not require any masking within a byte. To |
| 2991 | * make this work the PACKSWAP option must be taken into account - it |
| 2992 | * simply requires the pixels to be reversed in each byte. |
| 2993 | * |
| 2994 | * The 'regular' case requires a mask for each of the first 6 passes, |
| 2995 | * 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] | 2996 | * 0..6. This has already been handled in the test above. |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 2997 | * |
| 2998 | * The masks are arranged as four bytes with the first byte to use in |
| 2999 | * the lowest bits (little-endian) regardless of the order (PACKSWAP or |
| 3000 | * not) of the pixels in each byte. |
| 3001 | * |
| 3002 | * NOTE: the whole of this logic depends on the caller of this function |
| 3003 | * only calling it on rows appropriate to the pass. This function only |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3004 | * understands the 'x' logic; the 'y' logic is handled by the caller. |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3005 | * |
| 3006 | * The following defines allow generation of compile time constant bit |
| 3007 | * 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] | 3008 | * swapped bytes. Pass 'p' is in the range 0..6; 'x', a pixel index, |
| 3009 | * is in the range 0..7; and the result is 1 if the pixel is to be |
| 3010 | * copied in the pass, 0 if not. 'S' is for the sparkle method, 'B' |
| 3011 | * for the block method. |
| 3012 | * |
John Bowler | 92ef313 | 2011-10-27 19:36:08 -0500 | [diff] [blame] | 3013 | * With some compilers a compile time expression of the general form: |
Glenn Randers-Pehrson | cb75699 | 2011-10-27 16:59:03 -0500 | [diff] [blame] | 3014 | * |
John Bowler | 92ef313 | 2011-10-27 19:36:08 -0500 | [diff] [blame] | 3015 | * (shift >= 32) ? (a >> (shift-32)) : (b >> shift) |
| 3016 | * |
| 3017 | * 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] | 3018 | * because the right hand side of the ?: expression is evaluated by |
John Bowler | 92ef313 | 2011-10-27 19:36:08 -0500 | [diff] [blame] | 3019 | * the compiler even though it isn't used. Microsoft Visual C (various |
| 3020 | * versions) and the Intel C compiler are known to do this. To avoid |
| 3021 | * 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] | 3022 | * solution to avoid destabilizing the code during the release process. |
Glenn Randers-Pehrson | cb75699 | 2011-10-27 16:59:03 -0500 | [diff] [blame] | 3023 | */ |
John Bowler | 92ef313 | 2011-10-27 19:36:08 -0500 | [diff] [blame] | 3024 | # if PNG_USE_COMPILE_TIME_MASKS |
Glenn Randers-Pehrson | cb75699 | 2011-10-27 16:59:03 -0500 | [diff] [blame] | 3025 | # define PNG_LSR(x,s) ((x)>>((s) & 0x1f)) |
| 3026 | # define PNG_LSL(x,s) ((x)<<((s) & 0x1f)) |
Glenn Randers-Pehrson | ef02d56 | 2011-10-27 12:05:58 -0500 | [diff] [blame] | 3027 | # else |
Glenn Randers-Pehrson | cb75699 | 2011-10-27 16:59:03 -0500 | [diff] [blame] | 3028 | # define PNG_LSR(x,s) ((x)>>(s)) |
| 3029 | # define PNG_LSL(x,s) ((x)<<(s)) |
Glenn Randers-Pehrson | ef02d56 | 2011-10-27 12:05:58 -0500 | [diff] [blame] | 3030 | # endif |
Glenn Randers-Pehrson | cb75699 | 2011-10-27 16:59:03 -0500 | [diff] [blame] | 3031 | # define S_COPY(p,x) (((p)<4 ? PNG_LSR(0x80088822,(3-(p))*8+(7-(x))) :\ |
| 3032 | PNG_LSR(0xaa55ff00,(7-(p))*8+(7-(x)))) & 1) |
| 3033 | # define B_COPY(p,x) (((p)<4 ? PNG_LSR(0xff0fff33,(3-(p))*8+(7-(x))) :\ |
| 3034 | PNG_LSR(0xff55ff00,(7-(p))*8+(7-(x)))) & 1) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3035 | |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3036 | /* Return a mask for pass 'p' pixel 'x' at depth 'd'. The mask is |
| 3037 | * little endian - the first pixel is at bit 0 - however the extra |
| 3038 | * parameter 's' can be set to cause the mask position to be swapped |
| 3039 | * within each byte, to match the PNG format. This is done by XOR of |
| 3040 | * the shift with 7, 6 or 4 for bit depths 1, 2 and 4. |
| 3041 | */ |
Glenn Randers-Pehrson | cb75699 | 2011-10-27 16:59:03 -0500 | [diff] [blame] | 3042 | # define PIXEL_MASK(p,x,d,s) \ |
| 3043 | (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] | 3044 | |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3045 | /* Hence generate the appropriate 'block' or 'sparkle' pixel copy mask. |
| 3046 | */ |
| 3047 | # define S_MASKx(p,x,d,s) (S_COPY(p,x)?PIXEL_MASK(p,x,d,s):0) |
| 3048 | # 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] | 3049 | |
Glenn Randers-Pehrson | ef02d56 | 2011-10-27 12:05:58 -0500 | [diff] [blame] | 3050 | /* Combine 8 of these to get the full mask. For the 1-bpp and 2-bpp |
| 3051 | * cases the result needs replicating, for the 4-bpp case the above |
| 3052 | * generates a full 32 bits. |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3053 | */ |
| 3054 | # 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] | 3055 | |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3056 | # define S_MASK(p,d,s) MASK_EXPAND(S_MASKx(p,0,d,s) + S_MASKx(p,1,d,s) +\ |
| 3057 | S_MASKx(p,2,d,s) + S_MASKx(p,3,d,s) + S_MASKx(p,4,d,s) +\ |
| 3058 | 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] | 3059 | |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3060 | # define B_MASK(p,d,s) MASK_EXPAND(B_MASKx(p,0,d,s) + B_MASKx(p,1,d,s) +\ |
| 3061 | B_MASKx(p,2,d,s) + B_MASKx(p,3,d,s) + B_MASKx(p,4,d,s) +\ |
| 3062 | 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] | 3063 | |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3064 | #if PNG_USE_COMPILE_TIME_MASKS |
| 3065 | /* Utility macros to construct all the masks for a depth/swap |
| 3066 | * combination. The 's' parameter says whether the format is PNG |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3067 | * (big endian bytes) or not. Only the three odd-numbered passes are |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3068 | * required for the display/block algorithm. |
| 3069 | */ |
| 3070 | # define S_MASKS(d,s) { S_MASK(0,d,s), S_MASK(1,d,s), S_MASK(2,d,s),\ |
| 3071 | 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] | 3072 | |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3073 | # 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] | 3074 | |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3075 | # define DEPTH_INDEX(d) ((d)==1?0:((d)==2?1:2)) |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 3076 | |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3077 | /* Hence the pre-compiled masks indexed by PACKSWAP (or not), depth and |
| 3078 | * then pass: |
| 3079 | */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3080 | static PNG_CONST png_uint_32 row_mask[2/*PACKSWAP*/][3/*depth*/][6] = |
| 3081 | { |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3082 | /* Little-endian byte masks for PACKSWAP */ |
| 3083 | { S_MASKS(1,0), S_MASKS(2,0), S_MASKS(4,0) }, |
| 3084 | /* Normal (big-endian byte) masks - PNG format */ |
| 3085 | { S_MASKS(1,1), S_MASKS(2,1), S_MASKS(4,1) } |
| 3086 | }; |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 3087 | |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3088 | /* display_mask has only three entries for the odd passes, so index by |
| 3089 | * pass>>1. |
| 3090 | */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3091 | static PNG_CONST png_uint_32 display_mask[2][3][3] = |
| 3092 | { |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3093 | /* Little-endian byte masks for PACKSWAP */ |
| 3094 | { B_MASKS(1,0), B_MASKS(2,0), B_MASKS(4,0) }, |
| 3095 | /* Normal (big-endian byte) masks - PNG format */ |
| 3096 | { B_MASKS(1,1), B_MASKS(2,1), B_MASKS(4,1) } |
| 3097 | }; |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 3098 | |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3099 | # define MASK(pass,depth,display,png)\ |
| 3100 | ((display)?display_mask[png][DEPTH_INDEX(depth)][pass>>1]:\ |
| 3101 | row_mask[png][DEPTH_INDEX(depth)][pass]) |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 3102 | |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3103 | #else /* !PNG_USE_COMPILE_TIME_MASKS */ |
| 3104 | /* This is the runtime alternative: it seems unlikely that this will |
| 3105 | * ever be either smaller or faster than the compile time approach. |
| 3106 | */ |
| 3107 | # define MASK(pass,depth,display,png)\ |
| 3108 | ((display)?B_MASK(pass,depth,png):S_MASK(pass,depth,png)) |
| 3109 | #endif /* !PNG_USE_COMPILE_TIME_MASKS */ |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 3110 | |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3111 | /* Use the appropriate mask to copy the required bits. In some cases |
| 3112 | * the byte mask will be 0 or 0xff, optimize these cases. row_width is |
| 3113 | * the number of pixels, but the code copies bytes, so it is necessary |
| 3114 | * to special case the end. |
| 3115 | */ |
| 3116 | png_uint_32 pixels_per_byte = 8 / pixel_depth; |
| 3117 | png_uint_32 mask; |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 3118 | |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3119 | # ifdef PNG_READ_PACKSWAP_SUPPORTED |
| 3120 | if (png_ptr->transformations & PNG_PACKSWAP) |
| 3121 | mask = MASK(pass, pixel_depth, display, 0); |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 3122 | |
| 3123 | else |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3124 | # endif |
| 3125 | mask = MASK(pass, pixel_depth, display, 1); |
| 3126 | |
| 3127 | for (;;) |
| 3128 | { |
| 3129 | png_uint_32 m; |
| 3130 | |
| 3131 | /* 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] | 3132 | * 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] | 3133 | * however, essential to use OR here, not +, because of this. |
| 3134 | */ |
| 3135 | m = mask; |
| 3136 | mask = (m >> 8) | (m << 24); /* rotate right to good compilers */ |
| 3137 | m &= 0xff; |
| 3138 | |
| 3139 | if (m != 0) /* something to copy */ |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 3140 | { |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3141 | if (m != 0xff) |
| 3142 | *dp = (png_byte)((*dp & ~m) | (*sp & m)); |
| 3143 | else |
| 3144 | *dp = *sp; |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 3145 | } |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3146 | |
| 3147 | /* 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] | 3148 | * is not an exact number of bytes wide; libpng has always done |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3149 | * this. |
| 3150 | */ |
| 3151 | if (row_width <= pixels_per_byte) |
John Bowler | fb5b3ac | 2011-10-16 22:52:56 -0500 | [diff] [blame] | 3152 | break; /* May need to restore part of the last byte */ |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3153 | |
| 3154 | row_width -= pixels_per_byte; |
| 3155 | ++dp; |
| 3156 | ++sp; |
| 3157 | } |
| 3158 | } |
| 3159 | |
| 3160 | else /* pixel_depth >= 8 */ |
| 3161 | { |
| 3162 | unsigned int bytes_to_copy, bytes_to_jump; |
| 3163 | |
| 3164 | /* Validate the depth - it must be a multiple of 8 */ |
| 3165 | if (pixel_depth & 7) |
| 3166 | png_error(png_ptr, "invalid user transform pixel depth"); |
| 3167 | |
| 3168 | pixel_depth >>= 3; /* now in bytes */ |
| 3169 | row_width *= pixel_depth; |
| 3170 | |
| 3171 | /* Regardless of pass number the Adam 7 interlace always results in a |
| 3172 | * fixed number of pixels to copy then to skip. There may be a |
| 3173 | * different number of pixels to skip at the start though. |
| 3174 | */ |
| 3175 | { |
| 3176 | unsigned int offset = PNG_PASS_START_COL(pass) * pixel_depth; |
| 3177 | |
| 3178 | row_width -= offset; |
| 3179 | dp += offset; |
| 3180 | sp += offset; |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 3181 | } |
| 3182 | |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3183 | /* Work out the bytes to copy. */ |
| 3184 | if (display) |
| 3185 | { |
| 3186 | /* When doing the 'block' algorithm the pixel in the pass gets |
| 3187 | * replicated to adjacent pixels. This is why the even (0,2,4,6) |
| 3188 | * passes are skipped above - the entire expanded row is copied. |
| 3189 | */ |
| 3190 | bytes_to_copy = (1<<((6-pass)>>1)) * pixel_depth; |
| 3191 | |
| 3192 | /* But don't allow this number to exceed the actual row width. */ |
| 3193 | if (bytes_to_copy > row_width) |
| 3194 | bytes_to_copy = row_width; |
| 3195 | } |
| 3196 | |
| 3197 | else /* normal row; Adam7 only ever gives us one pixel to copy. */ |
| 3198 | bytes_to_copy = pixel_depth; |
| 3199 | |
| 3200 | /* In Adam7 there is a constant offset between where the pixels go. */ |
| 3201 | bytes_to_jump = PNG_PASS_COL_OFFSET(pass) * pixel_depth; |
| 3202 | |
| 3203 | /* And simply copy these bytes. Some optimization is possible here, |
Glenn Randers-Pehrson | ef02d56 | 2011-10-27 12:05:58 -0500 | [diff] [blame] | 3204 | * depending on the value of 'bytes_to_copy'. Special case the low |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3205 | * byte counts, which we know to be frequent. |
John Bowler | fb5b3ac | 2011-10-16 22:52:56 -0500 | [diff] [blame] | 3206 | * |
| 3207 | * Notice that these cases all 'return' rather than 'break' - this |
| 3208 | * avoids an unnecessary test on whether to restore the last byte |
| 3209 | * below. |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3210 | */ |
| 3211 | switch (bytes_to_copy) |
| 3212 | { |
| 3213 | case 1: |
| 3214 | for (;;) |
| 3215 | { |
| 3216 | *dp = *sp; |
| 3217 | |
| 3218 | if (row_width <= bytes_to_jump) |
| 3219 | return; |
| 3220 | |
| 3221 | dp += bytes_to_jump; |
| 3222 | sp += bytes_to_jump; |
| 3223 | row_width -= bytes_to_jump; |
| 3224 | } |
| 3225 | |
| 3226 | case 2: |
Glenn Randers-Pehrson | ef02d56 | 2011-10-27 12:05:58 -0500 | [diff] [blame] | 3227 | /* 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] | 3228 | * slows the code down somewhat. |
| 3229 | */ |
| 3230 | do |
| 3231 | { |
| 3232 | dp[0] = sp[0], dp[1] = sp[1]; |
| 3233 | |
| 3234 | if (row_width <= bytes_to_jump) |
| 3235 | return; |
| 3236 | |
| 3237 | sp += bytes_to_jump; |
| 3238 | dp += bytes_to_jump; |
| 3239 | row_width -= bytes_to_jump; |
| 3240 | } |
| 3241 | while (row_width > 1); |
| 3242 | |
| 3243 | /* And there can only be one byte left at this point: */ |
| 3244 | *dp = *sp; |
| 3245 | return; |
| 3246 | |
| 3247 | case 3: |
| 3248 | /* This can only be the RGB case, so each copy is exactly one |
| 3249 | * pixel and it is not necessary to check for a partial copy. |
| 3250 | */ |
| 3251 | for(;;) |
| 3252 | { |
| 3253 | dp[0] = sp[0], dp[1] = sp[1], dp[2] = sp[2]; |
| 3254 | |
| 3255 | if (row_width <= bytes_to_jump) |
| 3256 | return; |
| 3257 | |
| 3258 | sp += bytes_to_jump; |
| 3259 | dp += bytes_to_jump; |
| 3260 | row_width -= bytes_to_jump; |
| 3261 | } |
| 3262 | |
| 3263 | default: |
| 3264 | #if PNG_ALIGN_TYPE != PNG_ALIGN_NONE |
Glenn Randers-Pehrson | ef02d56 | 2011-10-27 12:05:58 -0500 | [diff] [blame] | 3265 | /* Check for double byte alignment and, if possible, use a |
| 3266 | * 16-bit copy. Don't attempt this for narrow images - ones that |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3267 | * are less than an interlace panel wide. Don't attempt it for |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3268 | * wide bytes_to_copy either - use the png_memcpy there. |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3269 | */ |
Glenn Randers-Pehrson | ef02d56 | 2011-10-27 12:05:58 -0500 | [diff] [blame] | 3270 | if (bytes_to_copy < 16 /*else use png_memcpy*/ && |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3271 | png_isaligned(dp, png_uint_16) && |
| 3272 | png_isaligned(sp, png_uint_16) && |
| 3273 | bytes_to_copy % sizeof (png_uint_16) == 0 && |
| 3274 | bytes_to_jump % sizeof (png_uint_16) == 0) |
| 3275 | { |
| 3276 | /* Everything is aligned for png_uint_16 copies, but try for |
| 3277 | * png_uint_32 first. |
| 3278 | */ |
| 3279 | if (png_isaligned(dp, png_uint_32) && |
| 3280 | png_isaligned(sp, png_uint_32) && |
| 3281 | bytes_to_copy % sizeof (png_uint_32) == 0 && |
| 3282 | bytes_to_jump % sizeof (png_uint_32) == 0) |
| 3283 | { |
| 3284 | png_uint_32p dp32 = (png_uint_32p)dp; |
| 3285 | png_const_uint_32p sp32 = (png_const_uint_32p)sp; |
| 3286 | unsigned int skip = (bytes_to_jump-bytes_to_copy) / |
| 3287 | sizeof (png_uint_32); |
| 3288 | |
| 3289 | do |
| 3290 | { |
| 3291 | size_t c = bytes_to_copy; |
| 3292 | do |
| 3293 | { |
| 3294 | *dp32++ = *sp32++; |
| 3295 | c -= sizeof (png_uint_32); |
| 3296 | } |
| 3297 | while (c > 0); |
| 3298 | |
| 3299 | if (row_width <= bytes_to_jump) |
| 3300 | return; |
| 3301 | |
| 3302 | dp32 += skip; |
| 3303 | sp32 += skip; |
| 3304 | row_width -= bytes_to_jump; |
| 3305 | } |
| 3306 | while (bytes_to_copy <= row_width); |
| 3307 | |
| 3308 | /* Get to here when the row_width truncates the final copy. |
| 3309 | * 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] | 3310 | * 16-bit loop below. |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3311 | */ |
| 3312 | dp = (png_bytep)dp32; |
| 3313 | sp = (png_const_bytep)sp32; |
| 3314 | do |
| 3315 | *dp++ = *sp++; |
| 3316 | while (--row_width > 0); |
| 3317 | return; |
| 3318 | } |
| 3319 | |
Glenn Randers-Pehrson | ef02d56 | 2011-10-27 12:05:58 -0500 | [diff] [blame] | 3320 | /* 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] | 3321 | * not too large. |
| 3322 | */ |
| 3323 | else |
| 3324 | { |
| 3325 | png_uint_16p dp16 = (png_uint_16p)dp; |
| 3326 | png_const_uint_16p sp16 = (png_const_uint_16p)sp; |
| 3327 | unsigned int skip = (bytes_to_jump-bytes_to_copy) / |
| 3328 | sizeof (png_uint_16); |
| 3329 | |
| 3330 | do |
| 3331 | { |
| 3332 | size_t c = bytes_to_copy; |
| 3333 | do |
| 3334 | { |
| 3335 | *dp16++ = *sp16++; |
| 3336 | c -= sizeof (png_uint_16); |
| 3337 | } |
| 3338 | while (c > 0); |
| 3339 | |
| 3340 | if (row_width <= bytes_to_jump) |
| 3341 | return; |
| 3342 | |
| 3343 | dp16 += skip; |
| 3344 | sp16 += skip; |
| 3345 | row_width -= bytes_to_jump; |
| 3346 | } |
| 3347 | while (bytes_to_copy <= row_width); |
| 3348 | |
Glenn Randers-Pehrson | ef02d56 | 2011-10-27 12:05:58 -0500 | [diff] [blame] | 3349 | /* End of row - 1 byte left, bytes_to_copy > row_width: */ |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3350 | dp = (png_bytep)dp16; |
| 3351 | sp = (png_const_bytep)sp16; |
| 3352 | do |
| 3353 | *dp++ = *sp++; |
| 3354 | while (--row_width > 0); |
| 3355 | return; |
| 3356 | } |
| 3357 | } |
| 3358 | #endif /* PNG_ALIGN_ code */ |
| 3359 | |
Glenn Randers-Pehrson | ef02d56 | 2011-10-27 12:05:58 -0500 | [diff] [blame] | 3360 | /* The true default - use a png_memcpy: */ |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3361 | for (;;) |
| 3362 | { |
| 3363 | png_memcpy(dp, sp, bytes_to_copy); |
| 3364 | |
| 3365 | if (row_width <= bytes_to_jump) |
| 3366 | return; |
| 3367 | |
| 3368 | sp += bytes_to_jump; |
| 3369 | dp += bytes_to_jump; |
| 3370 | row_width -= bytes_to_jump; |
| 3371 | if (bytes_to_copy > row_width) |
| 3372 | bytes_to_copy = row_width; |
| 3373 | } |
| 3374 | } |
John Bowler | fb5b3ac | 2011-10-16 22:52:56 -0500 | [diff] [blame] | 3375 | |
| 3376 | /* NOT REACHED*/ |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3377 | } /* pixel_depth >= 8 */ |
| 3378 | |
John Bowler | fb5b3ac | 2011-10-16 22:52:56 -0500 | [diff] [blame] | 3379 | /* Here if pixel_depth < 8 to check 'end_ptr' below. */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3380 | } |
John Bowler | 4e68aa7 | 2011-10-11 16:01:33 -0500 | [diff] [blame] | 3381 | else |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 3382 | #endif |
| 3383 | |
Glenn Randers-Pehrson | ef02d56 | 2011-10-27 12:05:58 -0500 | [diff] [blame] | 3384 | /* 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] | 3385 | * from the temporary row buffer (notice that this overwrites the end of the |
| 3386 | * destination row if it is a partial byte.) |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 3387 | */ |
| 3388 | png_memcpy(dp, sp, PNG_ROWBYTES(pixel_depth, row_width)); |
John Bowler | fb5b3ac | 2011-10-16 22:52:56 -0500 | [diff] [blame] | 3389 | |
| 3390 | /* Restore the overwritten bits from the last byte if necessary. */ |
| 3391 | if (end_ptr != NULL) |
| 3392 | *end_ptr = (png_byte)((end_byte & end_mask) | (*end_ptr & ~end_mask)); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3393 | } |
| 3394 | |
Glenn Randers-Pehrson | 231e687 | 2001-01-12 15:13:06 -0600 | [diff] [blame] | 3395 | #ifdef PNG_READ_INTERLACING_SUPPORTED |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 3396 | void /* PRIVATE */ |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 3397 | png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass, |
| 3398 | png_uint_32 transformations /* Because these may affect the byte layout */) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3399 | { |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 3400 | /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ |
| 3401 | /* Offset to next interlace block */ |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 3402 | 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] | 3403 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 3404 | png_debug(1, "in png_do_read_interlace"); |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3405 | if (row != NULL && row_info != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3406 | { |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3407 | png_uint_32 final_width; |
| 3408 | |
| 3409 | final_width = row_info->width * png_pass_inc[pass]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3410 | |
| 3411 | switch (row_info->pixel_depth) |
| 3412 | { |
| 3413 | case 1: |
| 3414 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 3415 | png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3); |
| 3416 | png_bytep dp = row + (png_size_t)((final_width - 1) >> 3); |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3417 | int sshift, dshift; |
| 3418 | int s_start, s_end, s_inc; |
| 3419 | int jstop = png_pass_inc[pass]; |
| 3420 | png_byte v; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3421 | png_uint_32 i; |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3422 | int j; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3423 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 3424 | #ifdef PNG_READ_PACKSWAP_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3425 | if (transformations & PNG_PACKSWAP) |
| 3426 | { |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3427 | sshift = (int)((row_info->width + 7) & 0x07); |
| 3428 | dshift = (int)((final_width + 7) & 0x07); |
| 3429 | s_start = 7; |
| 3430 | s_end = 0; |
| 3431 | s_inc = -1; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3432 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3433 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3434 | else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3435 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3436 | { |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3437 | sshift = 7 - (int)((row_info->width + 7) & 0x07); |
| 3438 | dshift = 7 - (int)((final_width + 7) & 0x07); |
| 3439 | s_start = 0; |
| 3440 | s_end = 7; |
| 3441 | s_inc = 1; |
| 3442 | } |
Glenn Randers-Pehrson | 5dd2b8e | 2004-11-24 07:50:16 -0600 | [diff] [blame] | 3443 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3444 | for (i = 0; i < row_info->width; i++) |
| 3445 | { |
| 3446 | v = (png_byte)((*sp >> sshift) & 0x01); |
| 3447 | for (j = 0; j < jstop; j++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3448 | { |
John Bowler | 8fb6c6a | 2012-01-25 07:47:44 -0600 | [diff] [blame] | 3449 | unsigned int tmp = *dp & (0x7f7f >> (7 - dshift)); |
| 3450 | tmp |= v << dshift; |
| 3451 | *dp = (png_byte)(tmp & 0xff); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3452 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3453 | if (dshift == s_end) |
| 3454 | { |
| 3455 | dshift = s_start; |
| 3456 | dp--; |
| 3457 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3458 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3459 | else |
| 3460 | dshift += s_inc; |
| 3461 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3462 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3463 | if (sshift == s_end) |
| 3464 | { |
| 3465 | sshift = s_start; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3466 | sp--; |
| 3467 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3468 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3469 | else |
| 3470 | sshift += s_inc; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3471 | } |
| 3472 | break; |
| 3473 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3474 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3475 | case 2: |
| 3476 | { |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3477 | png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2); |
| 3478 | png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2); |
| 3479 | int sshift, dshift; |
| 3480 | int s_start, s_end, s_inc; |
| 3481 | int jstop = png_pass_inc[pass]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3482 | png_uint_32 i; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3483 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 3484 | #ifdef PNG_READ_PACKSWAP_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3485 | if (transformations & PNG_PACKSWAP) |
| 3486 | { |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3487 | sshift = (int)(((row_info->width + 3) & 0x03) << 1); |
| 3488 | dshift = (int)(((final_width + 3) & 0x03) << 1); |
| 3489 | s_start = 6; |
| 3490 | s_end = 0; |
| 3491 | s_inc = -2; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3492 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3493 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3494 | else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3495 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3496 | { |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3497 | sshift = (int)((3 - ((row_info->width + 3) & 0x03)) << 1); |
| 3498 | dshift = (int)((3 - ((final_width + 3) & 0x03)) << 1); |
| 3499 | s_start = 0; |
| 3500 | s_end = 6; |
| 3501 | s_inc = 2; |
| 3502 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3503 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3504 | for (i = 0; i < row_info->width; i++) |
| 3505 | { |
| 3506 | png_byte v; |
| 3507 | int j; |
| 3508 | |
| 3509 | v = (png_byte)((*sp >> sshift) & 0x03); |
| 3510 | for (j = 0; j < jstop; j++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3511 | { |
John Bowler | 8fb6c6a | 2012-01-25 07:47:44 -0600 | [diff] [blame] | 3512 | unsigned int tmp = *dp & (0x3f3f >> (6 - dshift)); |
| 3513 | tmp |= v << dshift; |
| 3514 | *dp = (png_byte)(tmp & 0xff); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3515 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3516 | if (dshift == s_end) |
| 3517 | { |
| 3518 | dshift = s_start; |
| 3519 | dp--; |
| 3520 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3521 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3522 | else |
| 3523 | dshift += s_inc; |
| 3524 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3525 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3526 | if (sshift == s_end) |
| 3527 | { |
| 3528 | sshift = s_start; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3529 | sp--; |
| 3530 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3531 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3532 | else |
| 3533 | sshift += s_inc; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3534 | } |
| 3535 | break; |
| 3536 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3537 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3538 | case 4: |
| 3539 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 3540 | png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1); |
| 3541 | png_bytep dp = row + (png_size_t)((final_width - 1) >> 1); |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3542 | int sshift, dshift; |
| 3543 | int s_start, s_end, s_inc; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3544 | png_uint_32 i; |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3545 | int jstop = png_pass_inc[pass]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3546 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 3547 | #ifdef PNG_READ_PACKSWAP_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3548 | if (transformations & PNG_PACKSWAP) |
| 3549 | { |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3550 | sshift = (int)(((row_info->width + 1) & 0x01) << 2); |
| 3551 | dshift = (int)(((final_width + 1) & 0x01) << 2); |
| 3552 | s_start = 4; |
| 3553 | s_end = 0; |
| 3554 | s_inc = -4; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3555 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3556 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3557 | else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3558 | #endif |
| 3559 | { |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3560 | sshift = (int)((1 - ((row_info->width + 1) & 0x01)) << 2); |
| 3561 | dshift = (int)((1 - ((final_width + 1) & 0x01)) << 2); |
| 3562 | s_start = 0; |
| 3563 | s_end = 4; |
| 3564 | s_inc = 4; |
| 3565 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3566 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3567 | for (i = 0; i < row_info->width; i++) |
| 3568 | { |
Glenn Randers-Pehrson | 8db1998 | 2011-10-27 16:17:24 -0500 | [diff] [blame] | 3569 | png_byte v = (png_byte)((*sp >> sshift) & 0x0f); |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3570 | int j; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3571 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3572 | for (j = 0; j < jstop; j++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3573 | { |
John Bowler | 8fb6c6a | 2012-01-25 07:47:44 -0600 | [diff] [blame] | 3574 | unsigned int tmp = *dp & (0xf0f >> (4 - dshift)); |
| 3575 | tmp |= v << dshift; |
| 3576 | *dp = (png_byte)(tmp & 0xff); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3577 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3578 | if (dshift == s_end) |
| 3579 | { |
| 3580 | dshift = s_start; |
| 3581 | dp--; |
| 3582 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3583 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3584 | else |
| 3585 | dshift += s_inc; |
| 3586 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3587 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3588 | if (sshift == s_end) |
| 3589 | { |
| 3590 | sshift = s_start; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3591 | sp--; |
| 3592 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3593 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3594 | else |
| 3595 | sshift += s_inc; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3596 | } |
| 3597 | break; |
| 3598 | } |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 3599 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3600 | default: |
| 3601 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 3602 | png_size_t pixel_bytes = (row_info->pixel_depth >> 3); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3603 | |
Glenn Randers-Pehrson | e3f3c4e | 2010-02-07 18:08:50 -0600 | [diff] [blame] | 3604 | png_bytep sp = row + (png_size_t)(row_info->width - 1) |
| 3605 | * pixel_bytes; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3606 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3607 | png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3608 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3609 | int jstop = png_pass_inc[pass]; |
| 3610 | png_uint_32 i; |
| 3611 | |
| 3612 | for (i = 0; i < row_info->width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3613 | { |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3614 | png_byte v[8]; |
| 3615 | int j; |
| 3616 | |
| 3617 | png_memcpy(v, sp, pixel_bytes); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3618 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3619 | for (j = 0; j < jstop; j++) |
| 3620 | { |
| 3621 | png_memcpy(dp, v, pixel_bytes); |
| 3622 | dp -= pixel_bytes; |
| 3623 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3624 | |
Glenn Randers-Pehrson | c3dda6d | 2004-11-27 18:22:29 -0600 | [diff] [blame] | 3625 | sp -= pixel_bytes; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3626 | } |
| 3627 | break; |
| 3628 | } |
| 3629 | } |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 3630 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3631 | row_info->width = final_width; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 3632 | row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3633 | } |
Glenn Randers-Pehrson | b2aca21 | 2009-09-23 11:32:37 -0500 | [diff] [blame] | 3634 | #ifndef PNG_READ_PACKSWAP_SUPPORTED |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3635 | PNG_UNUSED(transformations) /* Silence compiler warning */ |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 3636 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3637 | } |
Glenn Randers-Pehrson | 231e687 | 2001-01-12 15:13:06 -0600 | [diff] [blame] | 3638 | #endif /* PNG_READ_INTERLACING_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3639 | |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3640 | static void |
| 3641 | png_read_filter_row_sub(png_row_infop row_info, png_bytep row, |
| 3642 | png_const_bytep prev_row) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3643 | { |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3644 | png_size_t i; |
| 3645 | png_size_t istop = row_info->rowbytes; |
| 3646 | unsigned int bpp = (row_info->pixel_depth + 7) >> 3; |
| 3647 | png_bytep rp = row + bpp; |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3648 | |
| 3649 | PNG_UNUSED(prev_row) |
| 3650 | |
| 3651 | for (i = bpp; i < istop; i++) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3652 | { |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3653 | *rp = (png_byte)(((int)(*rp) + (int)(*(rp-bpp))) & 0xff); |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3654 | rp++; |
| 3655 | } |
| 3656 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3657 | |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3658 | static void |
| 3659 | png_read_filter_row_up(png_row_infop row_info, png_bytep row, |
| 3660 | png_const_bytep prev_row) |
| 3661 | { |
| 3662 | png_size_t i; |
| 3663 | png_size_t istop = row_info->rowbytes; |
| 3664 | png_bytep rp = row; |
| 3665 | png_const_bytep pp = prev_row; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3666 | |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3667 | for (i = 0; i < istop; i++) |
| 3668 | { |
| 3669 | *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff); |
| 3670 | rp++; |
| 3671 | } |
| 3672 | } |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3673 | |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3674 | static void |
| 3675 | png_read_filter_row_avg(png_row_infop row_info, png_bytep row, |
| 3676 | png_const_bytep prev_row) |
| 3677 | { |
| 3678 | png_size_t i; |
| 3679 | png_bytep rp = row; |
| 3680 | png_const_bytep pp = prev_row; |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3681 | unsigned int bpp = (row_info->pixel_depth + 7) >> 3; |
| 3682 | png_size_t istop = row_info->rowbytes - bpp; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3683 | |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3684 | for (i = 0; i < bpp; i++) |
| 3685 | { |
| 3686 | *rp = (png_byte)(((int)(*rp) + |
| 3687 | ((int)(*pp++) / 2 )) & 0xff); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3688 | |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3689 | rp++; |
| 3690 | } |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 3691 | |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3692 | for (i = 0; i < istop; i++) |
| 3693 | { |
| 3694 | *rp = (png_byte)(((int)(*rp) + |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3695 | (int)(*pp++ + *(rp-bpp)) / 2 ) & 0xff); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3696 | |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3697 | rp++; |
| 3698 | } |
| 3699 | } |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3700 | |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3701 | static void |
John Bowler | fcc0263 | 2011-11-03 18:31:00 -0500 | [diff] [blame] | 3702 | 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] | 3703 | png_const_bytep prev_row) |
| 3704 | { |
John Bowler | fcc0263 | 2011-11-03 18:31:00 -0500 | [diff] [blame] | 3705 | png_bytep rp_end = row + row_info->rowbytes; |
| 3706 | int a, c; |
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 | /* First pixel/byte */ |
| 3709 | c = *prev_row++; |
| 3710 | a = *row + c; |
| 3711 | *row++ = (png_byte)a; |
| 3712 | |
| 3713 | /* Remainder */ |
| 3714 | while (row < rp_end) |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3715 | { |
John Bowler | fcc0263 | 2011-11-03 18:31:00 -0500 | [diff] [blame] | 3716 | int b, pa, pb, pc, p; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3717 | |
John Bowler | fcc0263 | 2011-11-03 18:31:00 -0500 | [diff] [blame] | 3718 | a &= 0xff; /* From previous iteration or start */ |
| 3719 | b = *prev_row++; |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3720 | |
| 3721 | p = b - c; |
| 3722 | pc = a - c; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 3723 | |
John Bowler | fcc0263 | 2011-11-03 18:31:00 -0500 | [diff] [blame] | 3724 | # ifdef PNG_USE_ABS |
| 3725 | pa = abs(p); |
| 3726 | pb = abs(pc); |
| 3727 | pc = abs(p + pc); |
| 3728 | # else |
| 3729 | pa = p < 0 ? -p : p; |
| 3730 | pb = pc < 0 ? -pc : pc; |
| 3731 | pc = (p + pc) < 0 ? -(p + pc) : p + pc; |
| 3732 | # endif |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3733 | |
John Bowler | fcc0263 | 2011-11-03 18:31:00 -0500 | [diff] [blame] | 3734 | /* Find the best predictor, the least of pa, pb, pc favoring the earlier |
| 3735 | * ones in the case of a tie. |
| 3736 | */ |
| 3737 | if (pb < pa) pa = pb, a = b; |
| 3738 | if (pc < pa) a = c; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3739 | |
John Bowler | fcc0263 | 2011-11-03 18:31:00 -0500 | [diff] [blame] | 3740 | /* Calculate the current pixel in a, and move the previous row pixel to c |
| 3741 | * for the next time round the loop |
| 3742 | */ |
| 3743 | c = b; |
| 3744 | a += *row; |
| 3745 | *row++ = (png_byte)a; |
| 3746 | } |
| 3747 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 3748 | |
John Bowler | fcc0263 | 2011-11-03 18:31:00 -0500 | [diff] [blame] | 3749 | static void |
| 3750 | png_read_filter_row_paeth_multibyte_pixel(png_row_infop row_info, png_bytep row, |
| 3751 | png_const_bytep prev_row) |
| 3752 | { |
| 3753 | int bpp = (row_info->pixel_depth + 7) >> 3; |
| 3754 | png_bytep rp_end = row + bpp; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3755 | |
John Bowler | fcc0263 | 2011-11-03 18:31:00 -0500 | [diff] [blame] | 3756 | /* Process the first pixel in the row completely (this is the same as 'up' |
| 3757 | * because there is only one candidate predictor for the first row). |
| 3758 | */ |
| 3759 | while (row < rp_end) |
| 3760 | { |
| 3761 | int a = *row + *prev_row++; |
| 3762 | *row++ = (png_byte)a; |
| 3763 | } |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3764 | |
John Bowler | fcc0263 | 2011-11-03 18:31:00 -0500 | [diff] [blame] | 3765 | /* Remainder */ |
| 3766 | rp_end += row_info->rowbytes - bpp; |
| 3767 | |
| 3768 | while (row < rp_end) |
| 3769 | { |
| 3770 | int a, b, c, pa, pb, pc, p; |
| 3771 | |
| 3772 | c = *(prev_row - bpp); |
| 3773 | a = *(row - bpp); |
| 3774 | b = *prev_row++; |
| 3775 | |
| 3776 | p = b - c; |
| 3777 | pc = a - c; |
| 3778 | |
| 3779 | # ifdef PNG_USE_ABS |
| 3780 | pa = abs(p); |
| 3781 | pb = abs(pc); |
| 3782 | pc = abs(p + pc); |
| 3783 | # else |
| 3784 | pa = p < 0 ? -p : p; |
| 3785 | pb = pc < 0 ? -pc : pc; |
| 3786 | pc = (p + pc) < 0 ? -(p + pc) : p + pc; |
| 3787 | # endif |
| 3788 | |
| 3789 | if (pb < pa) pa = pb, a = b; |
| 3790 | if (pc < pa) a = c; |
| 3791 | |
| 3792 | c = b; |
| 3793 | a += *row; |
| 3794 | *row++ = (png_byte)a; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3795 | } |
| 3796 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3797 | |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3798 | #ifdef PNG_ARM_NEON |
Mans Rullgard | e7acc6a | 2011-11-16 13:48:18 -0600 | [diff] [blame] | 3799 | |
| 3800 | #ifdef __linux__ |
| 3801 | #include <stdio.h> |
| 3802 | #include <elf.h> |
| 3803 | #include <asm/hwcap.h> |
| 3804 | |
| 3805 | static int png_have_hwcap(unsigned cap) |
| 3806 | { |
| 3807 | FILE *f = fopen("/proc/self/auxv", "r"); |
| 3808 | Elf32_auxv_t aux; |
| 3809 | int have_cap = 0; |
| 3810 | |
| 3811 | if (!f) |
| 3812 | return 0; |
| 3813 | |
| 3814 | while (fread(&aux, sizeof(aux), 1, f) > 0) |
| 3815 | { |
| 3816 | if (aux.a_type == AT_HWCAP && |
| 3817 | aux.a_un.a_val & cap) |
| 3818 | { |
| 3819 | have_cap = 1; |
| 3820 | break; |
| 3821 | } |
| 3822 | } |
| 3823 | |
| 3824 | fclose(f); |
| 3825 | |
| 3826 | return have_cap; |
| 3827 | } |
| 3828 | #endif /* __linux__ */ |
| 3829 | |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3830 | static void |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 3831 | png_init_filter_functions_neon(png_structrp pp, unsigned int bpp) |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3832 | { |
Mans Rullgard | e7acc6a | 2011-11-16 13:48:18 -0600 | [diff] [blame] | 3833 | #ifdef __linux__ |
| 3834 | if (!png_have_hwcap(HWCAP_NEON)) |
| 3835 | return; |
| 3836 | #endif |
| 3837 | |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3838 | pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up_neon; |
| 3839 | |
Mans Rullgard | e7acc6a | 2011-11-16 13:48:18 -0600 | [diff] [blame] | 3840 | if (bpp == 3) |
| 3841 | { |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3842 | pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub3_neon; |
| 3843 | 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] | 3844 | pp->read_filter[PNG_FILTER_VALUE_PAETH-1] = |
Mans Rullgard | e7acc6a | 2011-11-16 13:48:18 -0600 | [diff] [blame] | 3845 | png_read_filter_row_paeth3_neon; |
| 3846 | } |
| 3847 | |
| 3848 | else if (bpp == 4) |
| 3849 | { |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3850 | pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub4_neon; |
| 3851 | 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] | 3852 | pp->read_filter[PNG_FILTER_VALUE_PAETH-1] = |
| 3853 | png_read_filter_row_paeth4_neon; |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3854 | } |
| 3855 | } |
Mans Rullgard | e7acc6a | 2011-11-16 13:48:18 -0600 | [diff] [blame] | 3856 | #endif /* PNG_ARM_NEON */ |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3857 | |
| 3858 | static void |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 3859 | png_init_filter_functions(png_structrp pp) |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3860 | { |
John Bowler | fcc0263 | 2011-11-03 18:31:00 -0500 | [diff] [blame] | 3861 | unsigned int bpp = (pp->pixel_depth + 7) >> 3; |
| 3862 | |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3863 | pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub; |
| 3864 | pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up; |
| 3865 | 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] | 3866 | if (bpp == 1) |
| 3867 | pp->read_filter[PNG_FILTER_VALUE_PAETH-1] = |
| 3868 | png_read_filter_row_paeth_1byte_pixel; |
| 3869 | else |
| 3870 | pp->read_filter[PNG_FILTER_VALUE_PAETH-1] = |
| 3871 | png_read_filter_row_paeth_multibyte_pixel; |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3872 | |
| 3873 | #ifdef PNG_ARM_NEON |
John Bowler | fcc0263 | 2011-11-03 18:31:00 -0500 | [diff] [blame] | 3874 | png_init_filter_functions_neon(pp, bpp); |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 3875 | #endif |
| 3876 | } |
| 3877 | |
| 3878 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 3879 | 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] | 3880 | png_const_bytep prev_row, int filter) |
| 3881 | { |
| 3882 | if (pp->read_filter[0] == NULL) |
| 3883 | png_init_filter_functions(pp); |
| 3884 | if (filter > PNG_FILTER_VALUE_NONE && filter < PNG_FILTER_VALUE_LAST) |
| 3885 | pp->read_filter[filter-1](row_info, row, prev_row); |
| 3886 | } |
| 3887 | |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 3888 | #ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 3889 | void /* PRIVATE */ |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 3890 | png_read_IDAT_data(png_structrp png_ptr, png_bytep output, |
| 3891 | png_alloc_size_t avail_out) |
| 3892 | { |
| 3893 | /* Loop reading IDATs and decompressing the result into output[avail_out] */ |
| 3894 | png_ptr->zstream.next_out = output; |
| 3895 | png_ptr->zstream.avail_out = 0; /* safety: set below */ |
| 3896 | |
| 3897 | if (output == NULL) |
| 3898 | avail_out = 0; |
| 3899 | |
| 3900 | do |
| 3901 | { |
| 3902 | int ret; |
| 3903 | png_byte tmpbuf[PNG_INFLATE_BUF_SIZE]; |
| 3904 | |
| 3905 | if (png_ptr->zstream.avail_in == 0) |
| 3906 | { |
| 3907 | uInt avail_in; |
| 3908 | png_bytep buffer; |
| 3909 | |
| 3910 | while (png_ptr->idat_size == 0) |
| 3911 | { |
| 3912 | png_crc_finish(png_ptr, 0); |
| 3913 | |
| 3914 | png_ptr->idat_size = png_read_chunk_header(png_ptr); |
| 3915 | /* This is an error even in the 'check' case because the code just |
| 3916 | * consumed a non-IDAT header. |
| 3917 | */ |
| 3918 | if (png_ptr->chunk_name != png_IDAT) |
| 3919 | png_error(png_ptr, "Not enough image data"); |
| 3920 | } |
| 3921 | |
| 3922 | avail_in = png_ptr->IDAT_read_size; |
| 3923 | |
| 3924 | if (avail_in > png_ptr->idat_size) |
| 3925 | avail_in = (uInt)png_ptr->idat_size; |
| 3926 | |
| 3927 | /* A PNG with a gradually increasing IDAT size will defeat this attempt |
| 3928 | * to minimize memory usage by causing lots of re-allocs, but |
| 3929 | * realistically doing IDAT_read_size re-allocs is not likely to be a |
| 3930 | * big problem. |
| 3931 | */ |
| 3932 | buffer = png_read_buffer(png_ptr, avail_in, 0/*error*/); |
| 3933 | |
| 3934 | png_crc_read(png_ptr, buffer, avail_in); |
| 3935 | png_ptr->idat_size -= avail_in; |
| 3936 | |
| 3937 | png_ptr->zstream.next_in = buffer; |
| 3938 | png_ptr->zstream.avail_in = avail_in; |
| 3939 | } |
| 3940 | |
| 3941 | /* And set up the output side. */ |
| 3942 | if (output != NULL) /* standard read */ |
| 3943 | { |
| 3944 | uInt out = ZLIB_IO_MAX; |
| 3945 | |
| 3946 | if (out > avail_out) |
| 3947 | out = (uInt)avail_out; |
| 3948 | |
| 3949 | avail_out -= out; |
| 3950 | png_ptr->zstream.avail_out = out; |
| 3951 | } |
| 3952 | |
| 3953 | else /* check for end */ |
| 3954 | { |
| 3955 | png_ptr->zstream.next_out = tmpbuf; |
| 3956 | png_ptr->zstream.avail_out = sizeof tmpbuf; |
| 3957 | } |
| 3958 | |
| 3959 | /* Use NO_FLUSH; this gives zlib the maximum opportunity to optimize the |
John Bowler | 9066919 | 2012-03-09 22:03:13 -0600 | [diff] [blame] | 3960 | * process. If the LZ stream is truncated the sequential reader will |
| 3961 | * terminally damage the stream, above, by reading the chunk header of the |
| 3962 | * following chunk (it then exits with png_error). |
| 3963 | * |
| 3964 | * TODO: deal more elegantly with truncated IDAT lists. |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 3965 | */ |
| 3966 | ret = inflate(&png_ptr->zstream, Z_NO_FLUSH); |
| 3967 | |
| 3968 | /* Take the unconsumed output back (so, in the 'check' case this just |
| 3969 | * counts up). |
| 3970 | */ |
| 3971 | avail_out += png_ptr->zstream.avail_out; |
| 3972 | png_ptr->zstream.avail_out = 0; |
| 3973 | |
| 3974 | if (ret == Z_STREAM_END) |
| 3975 | { |
| 3976 | /* Do this for safety; we won't read any more into this row. */ |
| 3977 | png_ptr->zstream.next_out = NULL; |
| 3978 | |
| 3979 | png_ptr->mode |= PNG_AFTER_IDAT; |
| 3980 | png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED; |
| 3981 | |
| 3982 | if (png_ptr->zstream.avail_in > 0 || png_ptr->idat_size > 0) |
| 3983 | png_chunk_benign_error(png_ptr, "Extra compressed data"); |
| 3984 | break; |
| 3985 | } |
| 3986 | |
| 3987 | if (ret != Z_OK) |
| 3988 | { |
| 3989 | png_zstream_error(png_ptr, ret); |
| 3990 | |
| 3991 | if (output != NULL) |
| 3992 | png_chunk_error(png_ptr, png_ptr->zstream.msg); |
| 3993 | |
| 3994 | else /* checking */ |
| 3995 | { |
| 3996 | png_chunk_benign_error(png_ptr, png_ptr->zstream.msg); |
| 3997 | return; |
| 3998 | } |
| 3999 | } |
| 4000 | } while (avail_out > 0); |
| 4001 | |
| 4002 | if (avail_out > 0) |
| 4003 | { |
| 4004 | /* The stream ended before the image; this is the same as too few IDATs so |
| 4005 | * should be handled the same way. |
| 4006 | */ |
| 4007 | if (output != NULL) |
| 4008 | png_error(png_ptr, "Not enough image data"); |
| 4009 | |
| 4010 | else /* checking */ |
| 4011 | png_chunk_benign_error(png_ptr, "Too much image data"); |
| 4012 | } |
| 4013 | } |
| 4014 | |
| 4015 | void /* PRIVATE */ |
| 4016 | png_read_finish_IDAT(png_structrp png_ptr) |
| 4017 | { |
| 4018 | /* We don't need any more data and the stream should have ended, however the |
| 4019 | * LZ end code may actually not have been processed. In this case we must |
| 4020 | * read it otherwise stray unread IDAT data or, more likely, an IDAT chunk |
| 4021 | * may still remain to be consumed. |
| 4022 | */ |
| 4023 | if (!(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED)) |
| 4024 | { |
| 4025 | /* The NULL causes png_read_IDAT_data to swallow any remaining bytes in |
| 4026 | * the compressed stream, but the stream may be damaged too, so even after |
| 4027 | * this call we may need to terminate the zstream ownership. |
| 4028 | */ |
| 4029 | png_read_IDAT_data(png_ptr, NULL, 0); |
| 4030 | png_ptr->zstream.next_out = NULL; /* safety */ |
| 4031 | |
| 4032 | /* Now clear everything out for safety; the following may not have been |
| 4033 | * done. |
| 4034 | */ |
| 4035 | if (!(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED)) |
| 4036 | { |
| 4037 | png_ptr->mode |= PNG_AFTER_IDAT; |
| 4038 | png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED; |
| 4039 | } |
| 4040 | } |
| 4041 | |
| 4042 | /* If the zstream has not been released do it now *and* terminate the reading |
| 4043 | * of the final IDAT chunk. |
| 4044 | */ |
| 4045 | if (png_ptr->zowner == png_IDAT) |
| 4046 | { |
| 4047 | /* Always do this; the pointers otherwise point into the read buffer. */ |
| 4048 | png_ptr->zstream.next_in = NULL; |
| 4049 | png_ptr->zstream.avail_in = 0; |
| 4050 | |
| 4051 | /* Now we no longer own the zstream. */ |
| 4052 | png_ptr->zowner = 0; |
| 4053 | |
| 4054 | /* The slightly weird semantics of the sequential IDAT reading is that we |
| 4055 | * are always in or at the end of an IDAT chunk, so we always need to do a |
| 4056 | * crc_finish here. If idat_size is non-zero we also need to read the |
| 4057 | * spurious bytes at the end of the chunk now. |
| 4058 | */ |
| 4059 | (void)png_crc_finish(png_ptr, png_ptr->idat_size); |
| 4060 | } |
| 4061 | } |
| 4062 | |
| 4063 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 4064 | png_read_finish_row(png_structrp png_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4065 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 4066 | #ifdef PNG_READ_INTERLACING_SUPPORTED |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 4067 | /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 4068 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 4069 | /* Start of interlace block */ |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 4070 | 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] | 4071 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 4072 | /* Offset to next interlace block */ |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 4073 | 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] | 4074 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 4075 | /* Start of interlace block in the y direction */ |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 4076 | 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] | 4077 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 4078 | /* Offset to next interlace block in the y direction */ |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 4079 | 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] | 4080 | #endif /* PNG_READ_INTERLACING_SUPPORTED */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 4081 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 4082 | png_debug(1, "in png_read_finish_row"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4083 | png_ptr->row_number++; |
| 4084 | if (png_ptr->row_number < png_ptr->num_rows) |
| 4085 | return; |
| 4086 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 4087 | #ifdef PNG_READ_INTERLACING_SUPPORTED |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4088 | if (png_ptr->interlaced) |
| 4089 | { |
| 4090 | png_ptr->row_number = 0; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4091 | |
Glenn Randers-Pehrson | 435cf87 | 2011-10-05 16:23:53 -0500 | [diff] [blame] | 4092 | /* TO DO: don't do this if prev_row isn't needed (requires |
| 4093 | * read-ahead of the next row's filter byte. |
| 4094 | */ |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4095 | png_memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1); |
| 4096 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4097 | do |
| 4098 | { |
| 4099 | png_ptr->pass++; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4100 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4101 | if (png_ptr->pass >= 7) |
| 4102 | break; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4103 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4104 | png_ptr->iwidth = (png_ptr->width + |
| 4105 | png_pass_inc[png_ptr->pass] - 1 - |
| 4106 | png_pass_start[png_ptr->pass]) / |
| 4107 | png_pass_inc[png_ptr->pass]; |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 4108 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4109 | if (!(png_ptr->transformations & PNG_INTERLACE)) |
| 4110 | { |
| 4111 | png_ptr->num_rows = (png_ptr->height + |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4112 | png_pass_yinc[png_ptr->pass] - 1 - |
| 4113 | png_pass_ystart[png_ptr->pass]) / |
| 4114 | png_pass_yinc[png_ptr->pass]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4115 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4116 | |
Glenn Randers-Pehrson | 345bc27 | 1998-06-14 14:43:31 -0500 | [diff] [blame] | 4117 | else /* if (png_ptr->transformations & PNG_INTERLACE) */ |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4118 | break; /* libpng deinterlacing sees every row */ |
| 4119 | |
| 4120 | } while (png_ptr->num_rows == 0 || png_ptr->iwidth == 0); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4121 | |
| 4122 | if (png_ptr->pass < 7) |
| 4123 | return; |
| 4124 | } |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 4125 | #endif /* PNG_READ_INTERLACING_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4126 | |
John Bowler | 42a2b55 | 2012-03-05 20:57:40 -0600 | [diff] [blame] | 4127 | /* 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] | 4128 | png_read_finish_IDAT(png_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4129 | } |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 4130 | #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4131 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 4132 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 4133 | png_read_start_row(png_structrp png_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4134 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 4135 | #ifdef PNG_READ_INTERLACING_SUPPORTED |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 4136 | /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 4137 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 4138 | /* Start of interlace block */ |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 4139 | 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] | 4140 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 4141 | /* Offset to next interlace block */ |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 4142 | 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] | 4143 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 4144 | /* Start of interlace block in the y direction */ |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 4145 | 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] | 4146 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 4147 | /* Offset to next interlace block in the y direction */ |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 4148 | 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] | 4149 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 4150 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4151 | int max_pixel_depth; |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 4152 | png_size_t row_bytes; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4153 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 4154 | png_debug(1, "in png_read_start_row"); |
John Bowler | 42a2b55 | 2012-03-05 20:57:40 -0600 | [diff] [blame] | 4155 | |
John Bowler | 4a12f4a | 2011-04-17 18:34:22 -0500 | [diff] [blame] | 4156 | #ifdef PNG_READ_TRANSFORMS_SUPPORTED |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4157 | png_init_read_transformations(png_ptr); |
John Bowler | 4a12f4a | 2011-04-17 18:34:22 -0500 | [diff] [blame] | 4158 | #endif |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 4159 | #ifdef PNG_READ_INTERLACING_SUPPORTED |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4160 | if (png_ptr->interlaced) |
| 4161 | { |
| 4162 | if (!(png_ptr->transformations & PNG_INTERLACE)) |
| 4163 | 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] | 4164 | png_pass_ystart[0]) / png_pass_yinc[0]; |
| 4165 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4166 | else |
| 4167 | png_ptr->num_rows = png_ptr->height; |
| 4168 | |
| 4169 | png_ptr->iwidth = (png_ptr->width + |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4170 | png_pass_inc[png_ptr->pass] - 1 - |
| 4171 | png_pass_start[png_ptr->pass]) / |
| 4172 | png_pass_inc[png_ptr->pass]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4173 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4174 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4175 | else |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 4176 | #endif /* PNG_READ_INTERLACING_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4177 | { |
| 4178 | png_ptr->num_rows = png_ptr->height; |
| 4179 | png_ptr->iwidth = png_ptr->width; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4180 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4181 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4182 | max_pixel_depth = png_ptr->pixel_depth; |
| 4183 | |
John Bowler | e6fb691 | 2011-11-08 21:35:16 -0600 | [diff] [blame] | 4184 | /* WARNING: * png_read_transform_info (pngrtran.c) performs a simpliar set of |
| 4185 | * calculations to calculate the final pixel depth, then |
| 4186 | * png_do_read_transforms actually does the transforms. This means that the |
| 4187 | * code which effectively calculates this value is actually repeated in three |
| 4188 | * separate places. They must all match. Innocent changes to the order of |
| 4189 | * transformations can and will break libpng in a way that causes memory |
| 4190 | * overwrites. |
| 4191 | * |
| 4192 | * TODO: fix this. |
| 4193 | */ |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 4194 | #ifdef PNG_READ_PACK_SUPPORTED |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4195 | if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4196 | max_pixel_depth = 8; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 4197 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4198 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 4199 | #ifdef PNG_READ_EXPAND_SUPPORTED |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 4200 | if (png_ptr->transformations & PNG_EXPAND) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4201 | { |
| 4202 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 4203 | { |
| 4204 | if (png_ptr->num_trans) |
| 4205 | max_pixel_depth = 32; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4206 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4207 | else |
| 4208 | max_pixel_depth = 24; |
| 4209 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4210 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4211 | else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) |
| 4212 | { |
| 4213 | if (max_pixel_depth < 8) |
| 4214 | max_pixel_depth = 8; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4215 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4216 | if (png_ptr->num_trans) |
| 4217 | max_pixel_depth *= 2; |
| 4218 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4219 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4220 | else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB) |
| 4221 | { |
| 4222 | if (png_ptr->num_trans) |
| 4223 | { |
| 4224 | max_pixel_depth *= 4; |
| 4225 | max_pixel_depth /= 3; |
| 4226 | } |
| 4227 | } |
| 4228 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 4229 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4230 | |
John Bowler | 4d56296 | 2011-02-12 09:01:20 -0600 | [diff] [blame] | 4231 | #ifdef PNG_READ_EXPAND_16_SUPPORTED |
| 4232 | if (png_ptr->transformations & PNG_EXPAND_16) |
| 4233 | { |
| 4234 | # ifdef PNG_READ_EXPAND_SUPPORTED |
| 4235 | /* In fact it is an error if it isn't supported, but checking is |
| 4236 | * the safe way. |
| 4237 | */ |
| 4238 | if (png_ptr->transformations & PNG_EXPAND) |
| 4239 | { |
| 4240 | if (png_ptr->bit_depth < 16) |
| 4241 | max_pixel_depth *= 2; |
| 4242 | } |
| 4243 | else |
| 4244 | # endif |
| 4245 | png_ptr->transformations &= ~PNG_EXPAND_16; |
| 4246 | } |
| 4247 | #endif |
| 4248 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 4249 | #ifdef PNG_READ_FILLER_SUPPORTED |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 4250 | if (png_ptr->transformations & (PNG_FILLER)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4251 | { |
John Bowler | e6fb691 | 2011-11-08 21:35:16 -0600 | [diff] [blame] | 4252 | if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 4253 | { |
| 4254 | if (max_pixel_depth <= 8) |
| 4255 | max_pixel_depth = 16; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4256 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 4257 | else |
| 4258 | max_pixel_depth = 32; |
| 4259 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4260 | |
John Bowler | e6fb691 | 2011-11-08 21:35:16 -0600 | [diff] [blame] | 4261 | else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB || |
| 4262 | png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 4263 | { |
| 4264 | if (max_pixel_depth <= 32) |
| 4265 | max_pixel_depth = 32; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4266 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 4267 | else |
| 4268 | max_pixel_depth = 64; |
| 4269 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4270 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 4271 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4272 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 4273 | #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4274 | if (png_ptr->transformations & PNG_GRAY_TO_RGB) |
| 4275 | { |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 4276 | if ( |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 4277 | #ifdef PNG_READ_EXPAND_SUPPORTED |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4278 | (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) || |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 4279 | #endif |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 4280 | #ifdef PNG_READ_FILLER_SUPPORTED |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4281 | (png_ptr->transformations & (PNG_FILLER)) || |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 4282 | #endif |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4283 | png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4284 | { |
| 4285 | if (max_pixel_depth <= 16) |
| 4286 | max_pixel_depth = 32; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4287 | |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 4288 | else |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4289 | max_pixel_depth = 64; |
| 4290 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4291 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4292 | else |
| 4293 | { |
| 4294 | if (max_pixel_depth <= 8) |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4295 | { |
| 4296 | if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 4297 | max_pixel_depth = 32; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4298 | |
| 4299 | else |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 4300 | max_pixel_depth = 24; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4301 | } |
| 4302 | |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 4303 | else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
| 4304 | max_pixel_depth = 64; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4305 | |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 4306 | else |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4307 | max_pixel_depth = 48; |
| 4308 | } |
| 4309 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 4310 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4311 | |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 4312 | #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \ |
| 4313 | defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 4314 | if (png_ptr->transformations & PNG_USER_TRANSFORM) |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4315 | { |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 4316 | int user_pixel_depth = png_ptr->user_transform_depth * |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 4317 | png_ptr->user_transform_channels; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4318 | |
| 4319 | if (user_pixel_depth > max_pixel_depth) |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 4320 | max_pixel_depth = user_pixel_depth; |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4321 | } |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 4322 | #endif |
| 4323 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 4324 | /* This value is stored in png_struct and double checked in the row read |
| 4325 | * code. |
| 4326 | */ |
| 4327 | png_ptr->maximum_pixel_depth = (png_byte)max_pixel_depth; |
| 4328 | png_ptr->transformed_pixel_depth = 0; /* calculated on demand */ |
| 4329 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 4330 | /* Align the width on the next larger 8 pixels. Mainly used |
| 4331 | * for interlacing |
| 4332 | */ |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 4333 | row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7)); |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 4334 | /* Calculate the maximum bytes needed, adding a byte and a pixel |
| 4335 | * for safety's sake |
| 4336 | */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 4337 | row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) + |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4338 | 1 + ((max_pixel_depth + 7) >> 3); |
| 4339 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4340 | #ifdef PNG_MAX_MALLOC_64K |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 4341 | if (row_bytes > (png_uint_32)65536L) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 4342 | png_error(png_ptr, "This image requires a row greater than 64KB"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4343 | #endif |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 4344 | |
Glenn Randers-Pehrson | eddc5af | 2009-11-20 21:15:06 -0600 | [diff] [blame] | 4345 | if (row_bytes + 48 > png_ptr->old_big_row_buf_size) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 4346 | { |
| 4347 | png_free(png_ptr, png_ptr->big_row_buf); |
Mans Rullgard | c9e27d0 | 2011-10-17 15:25:03 -0500 | [diff] [blame] | 4348 | png_free(png_ptr, png_ptr->big_prev_row); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4349 | |
Glenn Randers-Pehrson | 6917b51 | 2009-03-09 15:31:08 -0500 | [diff] [blame] | 4350 | if (png_ptr->interlaced) |
Glenn Randers-Pehrson | a515d30 | 2010-01-01 10:24:25 -0600 | [diff] [blame] | 4351 | png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr, |
| 4352 | row_bytes + 48); |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4353 | |
Glenn Randers-Pehrson | a515d30 | 2010-01-01 10:24:25 -0600 | [diff] [blame] | 4354 | else |
Mans Rullgard | c9e27d0 | 2011-10-17 15:25:03 -0500 | [diff] [blame] | 4355 | 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] | 4356 | |
Mans Rullgard | c9e27d0 | 2011-10-17 15:25:03 -0500 | [diff] [blame] | 4357 | 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] | 4358 | |
| 4359 | #ifdef PNG_ALIGNED_MEMORY_SUPPORTED |
| 4360 | /* 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] | 4361 | * of padding before and after row_buf; treat prev_row similarly. |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 4362 | * NOTE: the alignment is to the start of the pixels, one beyond the start |
| 4363 | * 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] | 4364 | * was incorrect; the filter byte was aligned, which had the exact |
| 4365 | * opposite effect of that intended. |
Glenn Randers-Pehrson | eddc5af | 2009-11-20 21:15:06 -0600 | [diff] [blame] | 4366 | */ |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 4367 | { |
| 4368 | png_bytep temp = png_ptr->big_row_buf + 32; |
Glenn Randers-Pehrson | 8db1998 | 2011-10-27 16:17:24 -0500 | [diff] [blame] | 4369 | int extra = (int)((temp - (png_bytep)0) & 0x0f); |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 4370 | png_ptr->row_buf = temp - extra - 1/*filter byte*/; |
Mans Rullgard | c9e27d0 | 2011-10-17 15:25:03 -0500 | [diff] [blame] | 4371 | |
| 4372 | temp = png_ptr->big_prev_row + 32; |
Glenn Randers-Pehrson | 8db1998 | 2011-10-27 16:17:24 -0500 | [diff] [blame] | 4373 | extra = (int)((temp - (png_bytep)0) & 0x0f); |
Mans Rullgard | c9e27d0 | 2011-10-17 15:25:03 -0500 | [diff] [blame] | 4374 | png_ptr->prev_row = temp - extra - 1/*filter byte*/; |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 4375 | } |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4376 | |
Glenn Randers-Pehrson | eddc5af | 2009-11-20 21:15:06 -0600 | [diff] [blame] | 4377 | #else |
John Bowler | ac8375d | 2011-10-06 22:27:16 -0500 | [diff] [blame] | 4378 | /* Use 31 bytes of padding before and 17 bytes after row_buf. */ |
| 4379 | png_ptr->row_buf = png_ptr->big_row_buf + 31; |
Mans Rullgard | c9e27d0 | 2011-10-17 15:25:03 -0500 | [diff] [blame] | 4380 | png_ptr->prev_row = png_ptr->big_prev_row + 31; |
Glenn Randers-Pehrson | eddc5af | 2009-11-20 21:15:06 -0600 | [diff] [blame] | 4381 | #endif |
| 4382 | png_ptr->old_big_row_buf_size = row_bytes + 48; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 4383 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4384 | |
| 4385 | #ifdef PNG_MAX_MALLOC_64K |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4386 | if (png_ptr->rowbytes > 65535) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 4387 | 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] | 4388 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4389 | #endif |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4390 | if (png_ptr->rowbytes > (PNG_SIZE_MAX - 1)) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 4391 | 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] | 4392 | |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 4393 | png_memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 4394 | |
Glenn Randers-Pehrson | 5c92dec | 2011-01-07 18:28:47 -0600 | [diff] [blame] | 4395 | png_debug1(3, "width = %u,", png_ptr->width); |
| 4396 | png_debug1(3, "height = %u,", png_ptr->height); |
| 4397 | png_debug1(3, "iwidth = %u,", png_ptr->iwidth); |
| 4398 | png_debug1(3, "num_rows = %u,", png_ptr->num_rows); |
Glenn Randers-Pehrson | b764c60 | 2011-01-14 21:18:37 -0600 | [diff] [blame] | 4399 | png_debug1(3, "rowbytes = %lu,", (unsigned long)png_ptr->rowbytes); |
| 4400 | png_debug1(3, "irowbytes = %lu", |
| 4401 | (unsigned long)PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4402 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 4403 | /* The sequential reader needs a buffer for IDAT, but the progressive reader |
| 4404 | * does not, so free the read buffer now regardless; the sequential reader |
| 4405 | * reallocates it on demand. |
| 4406 | */ |
| 4407 | if (png_ptr->read_buffer) |
| 4408 | { |
| 4409 | png_bytep buffer = png_ptr->read_buffer; |
| 4410 | |
| 4411 | png_ptr->read_buffer_size = 0; |
| 4412 | png_ptr->read_buffer = NULL; |
| 4413 | png_free(png_ptr, buffer); |
| 4414 | } |
| 4415 | |
John Bowler | 9066919 | 2012-03-09 22:03:13 -0600 | [diff] [blame] | 4416 | /* Finally claim the zstream for the inflate of the IDAT data, use the bits |
| 4417 | * value from the stream (note that this will result in a fatal error if the |
| 4418 | * IDAT stream has a bogus deflate header window_bits value, but this should |
| 4419 | * not be happening any longer!) |
| 4420 | */ |
| 4421 | if (png_inflate_claim(png_ptr, png_IDAT, 0) != Z_OK) |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 4422 | png_error(png_ptr, png_ptr->zstream.msg); |
John Bowler | 42a2b55 | 2012-03-05 20:57:40 -0600 | [diff] [blame] | 4423 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 4424 | png_ptr->flags |= PNG_FLAG_ROW_INIT; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4425 | } |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 4426 | #endif /* PNG_READ_SUPPORTED */ |