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