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