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