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