blob: 7dccb1920278367cb7ffa25c12fd2559d02253f0 [file] [log] [blame]
Glenn Randers-Pehrsone6474622006-03-04 16:50:47 -06001
Andreas Dilger47a0c421997-05-16 02:46:07 -05002/* pngrutil.c - utilities to read a PNG file
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003 *
Glenn Randers-Pehrson9eec1592014-02-09 12:49:39 -06004 * Last changed in libpng 1.6.10 [(PENDING RELEASE)]
5 * Copyright (c) 1998-2014 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05006 * (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-Pehrsonb6ce43d1998-01-01 07:13:13 -06008 *
Glenn Randers-Pehrsonbfbf8652009-06-26 21:46:52 -05009 * This code is released under the libpng license.
Glenn Randers-Pehrsonc332bbc2009-06-25 13:43:50 -050010 * For conditions of distribution and use, see the disclaimer
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050011 * and license in png.h
Glenn Randers-Pehrson3e61d792009-06-24 09:31:28 -050012 *
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -050013 * This file contains routines that are only called from within
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060014 * libpng itself during the course of reading an image.
15 */
Guy Schalnat0d580581995-07-20 02:43:20 -050016
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050017#include "pngpriv.h"
Guy Schalnat0d580581995-07-20 02:43:20 -050018
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -060019#ifdef PNG_READ_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -050020
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -060021png_uint_32 PNGAPI
John Bowler5d567862011-12-24 09:12:00 -060022png_get_uint_31(png_const_structrp png_ptr, png_const_bytep buf)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -060023{
24 png_uint_32 uval = png_get_uint_32(buf);
25
26 if (uval > PNG_UINT_31_MAX)
27 png_error(png_ptr, "PNG unsigned integer out of range");
28
29 return (uval);
30}
31
32#if defined(PNG_READ_gAMA_SUPPORTED) || defined(PNG_READ_cHRM_SUPPORTED)
33/* The following is a variation on the above for use with the fixed
34 * point values used for gAMA and cHRM. Instead of png_error it
35 * issues a warning and returns (-1) - an invalid value because both
36 * gAMA and cHRM use *unsigned* integers for fixed point values.
37 */
38#define PNG_FIXED_ERROR (-1)
39
40static png_fixed_point /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -060041png_get_fixed_point(png_structrp png_ptr, png_const_bytep buf)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -060042{
43 png_uint_32 uval = png_get_uint_32(buf);
44
45 if (uval <= PNG_UINT_31_MAX)
46 return (png_fixed_point)uval; /* known to be in range */
47
48 /* The caller can turn off the warning by passing NULL. */
49 if (png_ptr != NULL)
50 png_warning(png_ptr, "PNG fixed point integer out of range");
51
52 return PNG_FIXED_ERROR;
53}
54#endif
55
56#ifdef PNG_READ_INT_FUNCTIONS_SUPPORTED
57/* NOTE: the read macros will obscure these definitions, so that if
58 * PNG_USE_READ_MACROS is set the library will not use them internally,
59 * but the APIs will still be available externally.
60 *
61 * The parentheses around "PNGAPI function_name" in the following three
62 * functions are necessary because they allow the macros to co-exist with
63 * these (unused but exported) functions.
64 */
65
66/* Grab an unsigned 32-bit integer from a buffer in big-endian format. */
67png_uint_32 (PNGAPI
68png_get_uint_32)(png_const_bytep buf)
69{
70 png_uint_32 uval =
71 ((png_uint_32)(*(buf )) << 24) +
72 ((png_uint_32)(*(buf + 1)) << 16) +
73 ((png_uint_32)(*(buf + 2)) << 8) +
74 ((png_uint_32)(*(buf + 3)) ) ;
75
76 return uval;
Guy Schalnat0d580581995-07-20 02:43:20 -050077}
78
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -050079/* Grab a signed 32-bit integer from a buffer in big-endian format. The
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -060080 * data is stored in the PNG file in two's complement format and there
81 * is no guarantee that a 'png_int_32' is exactly 32 bits, therefore
82 * the following code does a two's complement to native conversion.
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050083 */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -060084png_int_32 (PNGAPI
85png_get_int_32)(png_const_bytep buf)
Andreas Dilger47a0c421997-05-16 02:46:07 -050086{
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -060087 png_uint_32 uval = png_get_uint_32(buf);
John Bowlerf3f7e142011-09-09 07:32:37 -050088 if ((uval & 0x80000000) == 0) /* non-negative */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -060089 return uval;
Andreas Dilger47a0c421997-05-16 02:46:07 -050090
John Bowlerf3f7e142011-09-09 07:32:37 -050091 uval = (uval ^ 0xffffffff) + 1; /* 2's complement: -x = ~x+1 */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -060092 return -(png_int_32)uval;
Andreas Dilger47a0c421997-05-16 02:46:07 -050093}
Andreas Dilger47a0c421997-05-16 02:46:07 -050094
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -050095/* Grab an unsigned 16-bit integer from a buffer in big-endian format. */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -060096png_uint_16 (PNGAPI
97png_get_uint_16)(png_const_bytep buf)
Guy Schalnat0d580581995-07-20 02:43:20 -050098{
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -060099 /* ANSI-C requires an int value to accomodate at least 16 bits so this
100 * works and allows the compiler not to worry about possible narrowing
101 * on 32 bit systems. (Pre-ANSI systems did not make integers smaller
102 * than 16 bits either.)
103 */
104 unsigned int val =
105 ((unsigned int)(*buf) << 8) +
106 ((unsigned int)(*(buf + 1)));
Guy Schalnat0d580581995-07-20 02:43:20 -0500107
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600108 return (png_uint_16)val;
Guy Schalnat0d580581995-07-20 02:43:20 -0500109}
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600110
111#endif /* PNG_READ_INT_FUNCTIONS_SUPPORTED */
112
113/* Read and check the PNG file signature */
114void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600115png_read_sig(png_structrp png_ptr, png_inforp info_ptr)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600116{
117 png_size_t num_checked, num_to_check;
118
119 /* Exit if the user application does not expect a signature. */
120 if (png_ptr->sig_bytes >= 8)
121 return;
122
123 num_checked = png_ptr->sig_bytes;
124 num_to_check = 8 - num_checked;
125
126#ifdef PNG_IO_STATE_SUPPORTED
127 png_ptr->io_state = PNG_IO_READING | PNG_IO_SIGNATURE;
128#endif
129
130 /* The signature must be serialized in a single I/O call. */
131 png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
132 png_ptr->sig_bytes = 8;
133
134 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
135 {
136 if (num_checked < 4 &&
137 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
138 png_error(png_ptr, "Not a PNG file");
139 else
140 png_error(png_ptr, "PNG file corrupted by ASCII conversion");
141 }
142 if (num_checked < 3)
143 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
144}
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -0600145
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500146/* Read the chunk header (length + type name).
147 * Put the type name into png_ptr->chunk_name, and return the length.
148 */
149png_uint_32 /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600150png_read_chunk_header(png_structrp png_ptr)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500151{
152 png_byte buf[8];
153 png_uint_32 length;
154
155#ifdef PNG_IO_STATE_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500156 png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_HDR;
157#endif
158
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600159 /* Read the length and the chunk name.
160 * This must be performed in a single I/O call.
161 */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500162 png_read_data(png_ptr, buf, 8);
163 length = png_get_uint_31(png_ptr, buf);
164
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600165 /* Put the chunk name into png_ptr->chunk_name. */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500166 png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(buf+4);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500167
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500168 png_debug2(0, "Reading %lx chunk, length = %lu",
169 (unsigned long)png_ptr->chunk_name, (unsigned long)length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500170
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600171 /* Reset the crc and run it over the chunk name. */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500172 png_reset_crc(png_ptr);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500173 png_calculate_crc(png_ptr, buf + 4, 4);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500174
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600175 /* Check to see if chunk name is valid. */
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500176 png_check_chunk_name(png_ptr, png_ptr->chunk_name);
177
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500178#ifdef PNG_IO_STATE_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500179 png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_DATA;
180#endif
181
182 return length;
183}
184
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500185/* Read data, and (optionally) run it through the CRC. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500186void /* PRIVATE */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600187png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500188{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500189 if (png_ptr == NULL)
190 return;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600191
Guy Schalnat6d764711995-12-19 03:22:19 -0600192 png_read_data(png_ptr, buf, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500193 png_calculate_crc(png_ptr, buf, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500194}
195
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600196/* Optionally skip data and then check the CRC. Depending on whether we
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600197 * are reading an ancillary or critical chunk, and how the program has set
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500198 * things up, we may calculate the CRC on the data and print a message.
199 * Returns '1' if there was a CRC error, '0' otherwise.
200 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500201int /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600202png_crc_finish(png_structrp png_ptr, png_uint_32 skip)
Guy Schalnat0d580581995-07-20 02:43:20 -0500203{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600204 /* The size of the local buffer for inflate is a good guess as to a
205 * reasonable size to use for buffering reads from the application.
206 */
207 while (skip > 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600208 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600209 png_uint_32 len;
210 png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600211
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600212 len = (sizeof tmpbuf);
213 if (len > skip)
214 len = skip;
215 skip -= len;
216
217 png_crc_read(png_ptr, tmpbuf, len);
Guy Schalnat0d580581995-07-20 02:43:20 -0500218 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600219
Andreas Dilger47a0c421997-05-16 02:46:07 -0500220 if (png_crc_error(png_ptr))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600221 {
Glenn Randers-Pehrson67a289f2013-04-19 19:03:34 -0500222 if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) ?
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500223 !(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) :
224 (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600225 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600226 png_chunk_warning(png_ptr, "CRC error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600227 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600228
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600229 else
230 {
Glenn Randers-Pehrsoneb657ae2014-02-22 21:59:40 -0600231 png_chunk_error(png_ptr, "CRC error");
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500232 return (0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600233 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600234
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600235 return (1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600236 }
237
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600238 return (0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500239}
240
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600241/* Compare the CRC stored in the PNG file with that calculated by libpng from
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500242 * the data it has read thus far.
243 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500244int /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600245png_crc_error(png_structrp png_ptr)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600246{
247 png_byte crc_bytes[4];
248 png_uint_32 crc;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500249 int need_crc = 1;
250
Glenn Randers-Pehrson67a289f2013-04-19 19:03:34 -0500251 if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500252 {
253 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
254 (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
255 need_crc = 0;
256 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600257
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500258 else /* critical */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500259 {
260 if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
261 need_crc = 0;
262 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600263
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500264#ifdef PNG_IO_STATE_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500265 png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_CRC;
266#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500267
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600268 /* The chunk CRC must be serialized in a single I/O call. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600269 png_read_data(png_ptr, crc_bytes, 4);
270
Andreas Dilger47a0c421997-05-16 02:46:07 -0500271 if (need_crc)
272 {
273 crc = png_get_uint_32(crc_bytes);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600274 return ((int)(crc != png_ptr->crc));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500275 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600276
Andreas Dilger47a0c421997-05-16 02:46:07 -0500277 else
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600278 return (0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600279}
280
Glenn Randers-Pehrson11321342013-11-18 20:12:24 -0600281#if defined(PNG_READ_iCCP_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) ||\
282 defined(PNG_READ_pCAL_SUPPORTED) || defined(PNG_READ_sCAL_SUPPORTED) ||\
283 defined(PNG_READ_sPLT_SUPPORTED) || defined(PNG_READ_tEXt_SUPPORTED) ||\
284 defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_SEQUENTIAL_READ_SUPPORTED)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600285/* Manage the read buffer; this simply reallocates the buffer if it is not small
286 * enough (or if it is not allocated). The routine returns a pointer to the
287 * buffer; if an error occurs and 'warn' is set the routine returns NULL, else
288 * it will call png_error (via png_malloc) on failure. (warn == 2 means
289 * 'silent').
290 */
291static png_bytep
292png_read_buffer(png_structrp png_ptr, png_alloc_size_t new_size, int warn)
John Bowler42a2b552012-03-05 20:57:40 -0600293{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600294 png_bytep buffer = png_ptr->read_buffer;
John Bowlerb5d00512012-03-09 09:15:18 -0600295
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600296 if (buffer != NULL && new_size > png_ptr->read_buffer_size)
John Bowlerb5d00512012-03-09 09:15:18 -0600297 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600298 png_ptr->read_buffer = NULL;
299 png_ptr->read_buffer = NULL;
300 png_ptr->read_buffer_size = 0;
301 png_free(png_ptr, buffer);
302 buffer = NULL;
303 }
John Bowlerb5d00512012-03-09 09:15:18 -0600304
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600305 if (buffer == NULL)
306 {
307 buffer = png_voidcast(png_bytep, png_malloc_base(png_ptr, new_size));
308
309 if (buffer != NULL)
John Bowlerb5d00512012-03-09 09:15:18 -0600310 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600311 png_ptr->read_buffer = buffer;
312 png_ptr->read_buffer_size = new_size;
John Bowlerb5d00512012-03-09 09:15:18 -0600313 }
John Bowlerb5d00512012-03-09 09:15:18 -0600314
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600315 else if (warn < 2) /* else silent */
John Bowlerb5d00512012-03-09 09:15:18 -0600316 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600317 if (warn)
318 png_chunk_warning(png_ptr, "insufficient memory to read chunk");
Glenn Randers-Pehrson45625ec2014-02-22 23:09:27 -0600319
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600320 else
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600321 png_chunk_error(png_ptr, "insufficient memory to read chunk");
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600322 }
323 }
324
325 return buffer;
326}
Glenn Randers-Pehrson11321342013-11-18 20:12:24 -0600327#endif /* PNG_READ_iCCP|iTXt|pCAL|sCAL|sPLT|tEXt|zTXt|SEQUENTIAL_READ */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600328
329/* png_inflate_claim: claim the zstream for some nefarious purpose that involves
330 * decompression. Returns Z_OK on success, else a zlib error code. It checks
331 * the owner but, in final release builds, just issues a warning if some other
332 * chunk apparently owns the stream. Prior to release it does a png_error.
333 */
334static int
John Bowler0c7ac062013-05-07 21:59:05 -0500335png_inflate_claim(png_structrp png_ptr, png_uint_32 owner)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600336{
337 if (png_ptr->zowner != 0)
338 {
339 char msg[64];
340
341 PNG_STRING_FROM_CHUNK(msg, png_ptr->zowner);
342 /* So the message that results is "<chunk> using zstream"; this is an
343 * internal error, but is very useful for debugging. i18n requirements
344 * are minimal.
345 */
346 (void)png_safecat(msg, (sizeof msg), 4, " using zstream");
347# if PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC
348 png_chunk_warning(png_ptr, msg);
349 png_ptr->zowner = 0;
350# else
351 png_chunk_error(png_ptr, msg);
352# endif
353 }
354
355 /* Implementation note: unlike 'png_deflate_claim' this internal function
356 * does not take the size of the data as an argument. Some efficiency could
357 * be gained by using this when it is known *if* the zlib stream itself does
358 * not record the number; however, this is an illusion: the original writer
359 * of the PNG may have selected a lower window size, and we really must
360 * follow that because, for systems with with limited capabilities, we
361 * would otherwise reject the application's attempts to use a smaller window
362 * size (zlib doesn't have an interface to say "this or lower"!).
363 *
364 * inflateReset2 was added to zlib 1.2.4; before this the window could not be
365 * reset, therefore it is necessary to always allocate the maximum window
366 * size with earlier zlibs just in case later compressed chunks need it.
367 */
368 {
369 int ret; /* zlib return code */
John Bowler9afb90f2013-05-08 14:21:46 -0500370# if PNG_ZLIB_VERNUM >= 0x1240
John Bowler0c7ac062013-05-07 21:59:05 -0500371
Glenn Randers-Pehrson62c6fbb2013-05-07 23:16:06 -0500372# if defined(PNG_SET_OPTION_SUPPORTED) && \
373 defined(PNG_MAXIMUM_INFLATE_WINDOW)
John Bowler0c7ac062013-05-07 21:59:05 -0500374 int window_bits;
375
376 if (((png_ptr->options >> PNG_MAXIMUM_INFLATE_WINDOW) & 3) ==
377 PNG_OPTION_ON)
378 window_bits = 15;
379
380 else
381 window_bits = 0;
382# else
383# define window_bits 0
384# endif
385# endif
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600386
387 /* Set this for safety, just in case the previous owner left pointers to
388 * memory allocations.
389 */
390 png_ptr->zstream.next_in = NULL;
391 png_ptr->zstream.avail_in = 0;
392 png_ptr->zstream.next_out = NULL;
393 png_ptr->zstream.avail_out = 0;
394
395 if (png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED)
396 {
John Bowler9afb90f2013-05-08 14:21:46 -0500397# if PNG_ZLIB_VERNUM < 0x1240
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600398 ret = inflateReset(&png_ptr->zstream);
399# else
400 ret = inflateReset2(&png_ptr->zstream, window_bits);
401# endif
402 }
403
404 else
405 {
John Bowler9afb90f2013-05-08 14:21:46 -0500406# if PNG_ZLIB_VERNUM < 0x1240
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600407 ret = inflateInit(&png_ptr->zstream);
408# else
409 ret = inflateInit2(&png_ptr->zstream, window_bits);
410# endif
411
412 if (ret == Z_OK)
413 png_ptr->flags |= PNG_FLAG_ZSTREAM_INITIALIZED;
John Bowler42a2b552012-03-05 20:57:40 -0600414 }
415
416 if (ret == Z_OK)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600417 png_ptr->zowner = owner;
John Bowler42a2b552012-03-05 20:57:40 -0600418
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600419 else
420 png_zstream_error(png_ptr, ret);
421
422 return ret;
423 }
John Bowler0c7ac062013-05-07 21:59:05 -0500424
425# ifdef window_bits
426# undef window_bits
427# endif
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600428}
429
430#ifdef PNG_READ_COMPRESSED_TEXT_SUPPORTED
431/* png_inflate now returns zlib error codes including Z_OK and Z_STREAM_END to
432 * allow the caller to do multiple calls if required. If the 'finish' flag is
433 * set Z_FINISH will be passed to the final inflate() call and Z_STREAM_END must
434 * be returned or there has been a problem, otherwise Z_SYNC_FLUSH is used and
435 * Z_OK or Z_STREAM_END will be returned on success.
436 *
437 * The input and output sizes are updated to the actual amounts of data consumed
438 * or written, not the amount available (as in a z_stream). The data pointers
439 * are not changed, so the next input is (data+input_size) and the next
440 * available output is (output+output_size).
441 */
442static int
443png_inflate(png_structrp png_ptr, png_uint_32 owner, int finish,
444 /* INPUT: */ png_const_bytep input, png_uint_32p input_size_ptr,
445 /* OUTPUT: */ png_bytep output, png_alloc_size_t *output_size_ptr)
446{
447 if (png_ptr->zowner == owner) /* Else not claimed */
448 {
449 int ret;
450 png_alloc_size_t avail_out = *output_size_ptr;
451 png_uint_32 avail_in = *input_size_ptr;
452
453 /* zlib can't necessarily handle more than 65535 bytes at once (i.e. it
454 * can't even necessarily handle 65536 bytes) because the type uInt is
455 * "16 bits or more". Consequently it is necessary to chunk the input to
456 * zlib. This code uses ZLIB_IO_MAX, from pngpriv.h, as the maximum (the
457 * maximum value that can be stored in a uInt.) It is possible to set
458 * ZLIB_IO_MAX to a lower value in pngpriv.h and this may sometimes have
459 * a performance advantage, because it reduces the amount of data accessed
460 * at each step and that may give the OS more time to page it in.
John Bowlerb5d00512012-03-09 09:15:18 -0600461 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600462 png_ptr->zstream.next_in = PNGZ_INPUT_CAST(input);
463 /* avail_in and avail_out are set below from 'size' */
John Bowlerb5d00512012-03-09 09:15:18 -0600464 png_ptr->zstream.avail_in = 0;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600465 png_ptr->zstream.avail_out = 0;
John Bowlerb5d00512012-03-09 09:15:18 -0600466
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600467 /* Read directly into the output if it is available (this is set to
468 * a local buffer below if output is NULL).
John Bowlerb5d00512012-03-09 09:15:18 -0600469 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600470 if (output != NULL)
471 png_ptr->zstream.next_out = output;
472
473 do
John Bowlerb5d00512012-03-09 09:15:18 -0600474 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600475 uInt avail;
476 Byte local_buffer[PNG_INFLATE_BUF_SIZE];
John Bowlerb5d00512012-03-09 09:15:18 -0600477
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600478 /* zlib INPUT BUFFER */
479 /* The setting of 'avail_in' used to be outside the loop; by setting it
480 * inside it is possible to chunk the input to zlib and simply rely on
481 * zlib to advance the 'next_in' pointer. This allows arbitrary
482 * amounts of data to be passed through zlib at the unavoidable cost of
483 * requiring a window save (memcpy of up to 32768 output bytes)
484 * every ZLIB_IO_MAX input bytes.
485 */
486 avail_in += png_ptr->zstream.avail_in; /* not consumed last time */
John Bowlerb5d00512012-03-09 09:15:18 -0600487
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600488 avail = ZLIB_IO_MAX;
489
490 if (avail_in < avail)
491 avail = (uInt)avail_in; /* safe: < than ZLIB_IO_MAX */
492
493 avail_in -= avail;
494 png_ptr->zstream.avail_in = avail;
495
496 /* zlib OUTPUT BUFFER */
497 avail_out += png_ptr->zstream.avail_out; /* not written last time */
498
499 avail = ZLIB_IO_MAX; /* maximum zlib can process */
500
501 if (output == NULL)
John Bowlerb5d00512012-03-09 09:15:18 -0600502 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600503 /* Reset the output buffer each time round if output is NULL and
504 * make available the full buffer, up to 'remaining_space'
505 */
506 png_ptr->zstream.next_out = local_buffer;
507 if ((sizeof local_buffer) < avail)
508 avail = (sizeof local_buffer);
John Bowlerb5d00512012-03-09 09:15:18 -0600509 }
510
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600511 if (avail_out < avail)
512 avail = (uInt)avail_out; /* safe: < ZLIB_IO_MAX */
John Bowlerb5d00512012-03-09 09:15:18 -0600513
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600514 png_ptr->zstream.avail_out = avail;
515 avail_out -= avail;
516
517 /* zlib inflate call */
518 /* In fact 'avail_out' may be 0 at this point, that happens at the end
519 * of the read when the final LZ end code was not passed at the end of
520 * the previous chunk of input data. Tell zlib if we have reached the
521 * end of the output buffer.
522 */
523 ret = inflate(&png_ptr->zstream, avail_out > 0 ? Z_NO_FLUSH :
524 (finish ? Z_FINISH : Z_SYNC_FLUSH));
525 } while (ret == Z_OK);
526
527 /* For safety kill the local buffer pointer now */
528 if (output == NULL)
529 png_ptr->zstream.next_out = NULL;
530
531 /* Claw back the 'size' and 'remaining_space' byte counts. */
532 avail_in += png_ptr->zstream.avail_in;
533 avail_out += png_ptr->zstream.avail_out;
534
535 /* Update the input and output sizes; the updated values are the amount
536 * consumed or written, effectively the inverse of what zlib uses.
John Bowlerb5d00512012-03-09 09:15:18 -0600537 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600538 if (avail_out > 0)
539 *output_size_ptr -= avail_out;
540
541 if (avail_in > 0)
542 *input_size_ptr -= avail_in;
543
544 /* Ensure png_ptr->zstream.msg is set (even in the success case!) */
545 png_zstream_error(png_ptr, ret);
546 return ret;
547 }
548
549 else
550 {
551 /* This is a bad internal error. The recovery assigns to the zstream msg
552 * pointer, which is not owned by the caller, but this is safe; it's only
553 * used on errors!
554 */
555 png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed");
556 return Z_STREAM_ERROR;
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600557 }
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600558}
559
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600560/*
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600561 * Decompress trailing data in a chunk. The assumption is that read_buffer
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600562 * points at an allocated area holding the contents of a chunk with a
563 * trailing compressed part. What we get back is an allocated area
564 * holding the original prefix part and an uncompressed version of the
565 * trailing part (the malloc area passed in is freed).
566 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600567static int
568png_decompress_chunk(png_structrp png_ptr,
569 png_uint_32 chunklength, png_uint_32 prefix_size,
570 png_alloc_size_t *newlength /* must be initialized to the maximum! */,
571 int terminate /*add a '\0' to the end of the uncompressed data*/)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600572{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600573 /* TODO: implement different limits for different types of chunk.
574 *
575 * The caller supplies *newlength set to the maximum length of the
576 * uncompressed data, but this routine allocates space for the prefix and
577 * maybe a '\0' terminator too. We have to assume that 'prefix_size' is
578 * limited only by the maximum chunk size.
579 */
580 png_alloc_size_t limit = PNG_SIZE_MAX;
Glenn Randers-Pehrson3744f942012-08-10 19:22:53 -0500581
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600582# ifdef PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED
583 if (png_ptr->user_chunk_malloc_max > 0 &&
584 png_ptr->user_chunk_malloc_max < limit)
585 limit = png_ptr->user_chunk_malloc_max;
586# elif PNG_USER_CHUNK_MALLOC_MAX > 0
587 if (PNG_USER_CHUNK_MALLOC_MAX < limit)
588 limit = PNG_USER_CHUNK_MALLOC_MAX;
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -0600589# endif
John Bowlerb5d00512012-03-09 09:15:18 -0600590
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600591 if (limit >= prefix_size + (terminate != 0))
592 {
593 int ret;
594
595 limit -= prefix_size + (terminate != 0);
596
597 if (limit < *newlength)
598 *newlength = limit;
599
John Bowler0c7ac062013-05-07 21:59:05 -0500600 /* Now try to claim the stream. */
601 ret = png_inflate_claim(png_ptr, png_ptr->chunk_name);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600602
603 if (ret == Z_OK)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600604 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600605 png_uint_32 lzsize = chunklength - prefix_size;
John Bowlerb5d00512012-03-09 09:15:18 -0600606
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600607 ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/,
608 /* input: */ png_ptr->read_buffer + prefix_size, &lzsize,
609 /* output: */ NULL, newlength);
610
611 if (ret == Z_STREAM_END)
John Bowlerb5d00512012-03-09 09:15:18 -0600612 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600613 /* Use 'inflateReset' here, not 'inflateReset2' because this
614 * preserves the previously decided window size (otherwise it would
615 * be necessary to store the previous window size.) In practice
616 * this doesn't matter anyway, because png_inflate will call inflate
617 * with Z_FINISH in almost all cases, so the window will not be
618 * maintained.
619 */
620 if (inflateReset(&png_ptr->zstream) == Z_OK)
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600621 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600622 /* Because of the limit checks above we know that the new,
623 * expanded, size will fit in a size_t (let alone an
624 * png_alloc_size_t). Use png_malloc_base here to avoid an
625 * extra OOM message.
626 */
627 png_alloc_size_t new_size = *newlength;
628 png_alloc_size_t buffer_size = prefix_size + new_size +
629 (terminate != 0);
630 png_bytep text = png_voidcast(png_bytep, png_malloc_base(png_ptr,
631 buffer_size));
632
633 if (text != NULL)
634 {
635 ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/,
636 png_ptr->read_buffer + prefix_size, &lzsize,
637 text + prefix_size, newlength);
638
639 if (ret == Z_STREAM_END)
640 {
641 if (new_size == *newlength)
642 {
643 if (terminate)
644 text[prefix_size + *newlength] = 0;
645
646 if (prefix_size > 0)
647 memcpy(text, png_ptr->read_buffer, prefix_size);
648
649 {
650 png_bytep old_ptr = png_ptr->read_buffer;
651
652 png_ptr->read_buffer = text;
653 png_ptr->read_buffer_size = buffer_size;
654 text = old_ptr; /* freed below */
655 }
656 }
657
658 else
659 {
660 /* The size changed on the second read, there can be no
661 * guarantee that anything is correct at this point.
662 * The 'msg' pointer has been set to "unexpected end of
663 * LZ stream", which is fine, but return an error code
664 * that the caller won't accept.
665 */
666 ret = PNG_UNEXPECTED_ZLIB_RETURN;
667 }
668 }
669
670 else if (ret == Z_OK)
671 ret = PNG_UNEXPECTED_ZLIB_RETURN; /* for safety */
672
673 /* Free the text pointer (this is the old read_buffer on
674 * success)
675 */
676 png_free(png_ptr, text);
677
678 /* This really is very benign, but it's still an error because
679 * the extra space may otherwise be used as a Trojan Horse.
680 */
681 if (ret == Z_STREAM_END &&
682 chunklength - prefix_size != lzsize)
683 png_chunk_benign_error(png_ptr, "extra compressed data");
684 }
685
686 else
687 {
688 /* Out of memory allocating the buffer */
689 ret = Z_MEM_ERROR;
690 png_zstream_error(png_ptr, Z_MEM_ERROR);
691 }
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600692 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600693
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600694 else
695 {
696 /* inflateReset failed, store the error message */
697 png_zstream_error(png_ptr, ret);
698
699 if (ret == Z_STREAM_END)
700 ret = PNG_UNEXPECTED_ZLIB_RETURN;
701 }
John Bowlerb5d00512012-03-09 09:15:18 -0600702 }
703
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600704 else if (ret == Z_OK)
705 ret = PNG_UNEXPECTED_ZLIB_RETURN;
706
707 /* Release the claimed stream */
708 png_ptr->zowner = 0;
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600709 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600710
711 else /* the claim failed */ if (ret == Z_STREAM_END) /* impossible! */
712 ret = PNG_UNEXPECTED_ZLIB_RETURN;
713
714 return ret;
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600715 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600716
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600717 else
John Bowlerb5d00512012-03-09 09:15:18 -0600718 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600719 /* Application/configuration limits exceeded */
720 png_zstream_error(png_ptr, Z_MEM_ERROR);
721 return Z_MEM_ERROR;
John Bowlerb5d00512012-03-09 09:15:18 -0600722 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600723}
Glenn Randers-Pehrson5975f312011-04-01 13:15:36 -0500724#endif /* PNG_READ_COMPRESSED_TEXT_SUPPORTED */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600725
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600726#ifdef PNG_READ_iCCP_SUPPORTED
727/* Perform a partial read and decompress, producing 'avail_out' bytes and
728 * reading from the current chunk as required.
729 */
730static int
731png_inflate_read(png_structrp png_ptr, png_bytep read_buffer, uInt read_size,
732 png_uint_32p chunk_bytes, png_bytep next_out, png_alloc_size_t *out_size,
733 int finish)
734{
735 if (png_ptr->zowner == png_ptr->chunk_name)
736 {
737 int ret;
738
739 /* next_in and avail_in must have been initialized by the caller. */
740 png_ptr->zstream.next_out = next_out;
741 png_ptr->zstream.avail_out = 0; /* set in the loop */
742
743 do
744 {
745 if (png_ptr->zstream.avail_in == 0)
746 {
747 if (read_size > *chunk_bytes)
748 read_size = (uInt)*chunk_bytes;
749 *chunk_bytes -= read_size;
750
751 if (read_size > 0)
752 png_crc_read(png_ptr, read_buffer, read_size);
753
754 png_ptr->zstream.next_in = read_buffer;
755 png_ptr->zstream.avail_in = read_size;
756 }
757
758 if (png_ptr->zstream.avail_out == 0)
759 {
760 uInt avail = ZLIB_IO_MAX;
761 if (avail > *out_size)
762 avail = (uInt)*out_size;
763 *out_size -= avail;
764
765 png_ptr->zstream.avail_out = avail;
766 }
767
768 /* Use Z_SYNC_FLUSH when there is no more chunk data to ensure that all
769 * the available output is produced; this allows reading of truncated
770 * streams.
771 */
772 ret = inflate(&png_ptr->zstream,
773 *chunk_bytes > 0 ? Z_NO_FLUSH : (finish ? Z_FINISH : Z_SYNC_FLUSH));
774 }
775 while (ret == Z_OK && (*out_size > 0 || png_ptr->zstream.avail_out > 0));
776
777 *out_size += png_ptr->zstream.avail_out;
778 png_ptr->zstream.avail_out = 0; /* Should not be required, but is safe */
779
780 /* Ensure the error message pointer is always set: */
781 png_zstream_error(png_ptr, ret);
782 return ret;
783 }
784
785 else
786 {
787 png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed");
788 return Z_STREAM_ERROR;
789 }
790}
791#endif
792
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500793/* Read and check the IDHR chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500794void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600795png_handle_IHDR(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500796{
797 png_byte buf[13];
798 png_uint_32 width, height;
799 int bit_depth, color_type, compression_type, filter_type;
800 int interlace_type;
801
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500802 png_debug(1, "in png_handle_IHDR");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500803
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600804 if (png_ptr->mode & PNG_HAVE_IHDR)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600805 png_chunk_error(png_ptr, "out of place");
Guy Schalnate5a37791996-06-05 15:50:50 -0500806
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500807 /* Check the length */
Guy Schalnat0d580581995-07-20 02:43:20 -0500808 if (length != 13)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600809 png_chunk_error(png_ptr, "invalid");
Guy Schalnat0d580581995-07-20 02:43:20 -0500810
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600811 png_ptr->mode |= PNG_HAVE_IHDR;
812
Guy Schalnat0d580581995-07-20 02:43:20 -0500813 png_crc_read(png_ptr, buf, 13);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600814 png_crc_finish(png_ptr, 0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500815
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500816 width = png_get_uint_31(png_ptr, buf);
817 height = png_get_uint_31(png_ptr, buf + 4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500818 bit_depth = buf[8];
819 color_type = buf[9];
820 compression_type = buf[10];
821 filter_type = buf[11];
822 interlace_type = buf[12];
823
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500824 /* Set internal variables */
Guy Schalnat0d580581995-07-20 02:43:20 -0500825 png_ptr->width = width;
826 png_ptr->height = height;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600827 png_ptr->bit_depth = (png_byte)bit_depth;
828 png_ptr->interlaced = (png_byte)interlace_type;
829 png_ptr->color_type = (png_byte)color_type;
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500830#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600831 png_ptr->filter_type = (png_byte)filter_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500832#endif
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500833 png_ptr->compression_type = (png_byte)compression_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500834
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500835 /* Find number of channels */
Guy Schalnat0d580581995-07-20 02:43:20 -0500836 switch (png_ptr->color_type)
837 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600838 default: /* invalid, png_set_IHDR calls png_error */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500839 case PNG_COLOR_TYPE_GRAY:
840 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnat0d580581995-07-20 02:43:20 -0500841 png_ptr->channels = 1;
842 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500843
Andreas Dilger47a0c421997-05-16 02:46:07 -0500844 case PNG_COLOR_TYPE_RGB:
Guy Schalnat0d580581995-07-20 02:43:20 -0500845 png_ptr->channels = 3;
846 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500847
Andreas Dilger47a0c421997-05-16 02:46:07 -0500848 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnat0d580581995-07-20 02:43:20 -0500849 png_ptr->channels = 2;
850 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500851
Andreas Dilger47a0c421997-05-16 02:46:07 -0500852 case PNG_COLOR_TYPE_RGB_ALPHA:
Guy Schalnat0d580581995-07-20 02:43:20 -0500853 png_ptr->channels = 4;
854 break;
855 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600856
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500857 /* Set up other useful info */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600858 png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth *
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600859 png_ptr->channels);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500860 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width);
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500861 png_debug1(3, "bit_depth = %d", png_ptr->bit_depth);
862 png_debug1(3, "channels = %d", png_ptr->channels);
Glenn Randers-Pehrsonb764c602011-01-14 21:18:37 -0600863 png_debug1(3, "rowbytes = %lu", (unsigned long)png_ptr->rowbytes);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500864 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600865 color_type, interlace_type, compression_type, filter_type);
Guy Schalnat0d580581995-07-20 02:43:20 -0500866}
867
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500868/* Read and check the palette */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500869void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600870png_handle_PLTE(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500871{
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600872 png_color palette[PNG_MAX_PALETTE_LENGTH];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600873 int num, i;
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500874#ifdef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500875 png_colorp pal_ptr;
876#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500877
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500878 png_debug(1, "in png_handle_PLTE");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500879
Guy Schalnate5a37791996-06-05 15:50:50 -0500880 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600881 png_chunk_error(png_ptr, "missing IHDR");
882
883 /* Moved to before the 'after IDAT' check below because otherwise duplicate
884 * PLTE chunks are potentially ignored (the spec says there shall not be more
885 * than one PLTE, the error is not treated as benign, so this check trumps
886 * the requirement that PLTE appears before IDAT.)
887 */
888 else if (png_ptr->mode & PNG_HAVE_PLTE)
889 png_chunk_error(png_ptr, "duplicate");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500890
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600891 else if (png_ptr->mode & PNG_HAVE_IDAT)
892 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600893 /* This is benign because the non-benign error happened before, when an
894 * IDAT was encountered in a color-mapped image with no PLTE.
895 */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600896 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600897 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600898 return;
899 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500900
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600901 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500902
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600903 if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR))
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500904 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500905 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600906 png_chunk_benign_error(png_ptr, "ignored in grayscale PNG");
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500907 return;
908 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600909
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -0500910#ifndef PNG_READ_OPT_PLTE_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500911 if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
912 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600913 png_crc_finish(png_ptr, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500914 return;
915 }
916#endif
917
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600918 if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3)
Guy Schalnate5a37791996-06-05 15:50:50 -0500919 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600920 png_crc_finish(png_ptr, length);
921
Guy Schalnate5a37791996-06-05 15:50:50 -0500922 if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600923 png_chunk_benign_error(png_ptr, "invalid");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500924
Guy Schalnate5a37791996-06-05 15:50:50 -0500925 else
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600926 png_chunk_error(png_ptr, "invalid");
927
928 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500929 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500930
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600931 /* The cast is safe because 'length' is less than 3*PNG_MAX_PALETTE_LENGTH */
Guy Schalnat0d580581995-07-20 02:43:20 -0500932 num = (int)length / 3;
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500933
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500934#ifdef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500935 for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)
936 {
937 png_byte buf[3];
938
939 png_crc_read(png_ptr, buf, 3);
940 pal_ptr->red = buf[0];
941 pal_ptr->green = buf[1];
942 pal_ptr->blue = buf[2];
943 }
944#else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600945 for (i = 0; i < num; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500946 {
947 png_byte buf[3];
948
949 png_crc_read(png_ptr, buf, 3);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500950 /* Don't depend upon png_color being any order */
Guy Schalnat0d580581995-07-20 02:43:20 -0500951 palette[i].red = buf[0];
952 palette[i].green = buf[1];
953 palette[i].blue = buf[2];
954 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500955#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600956
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600957 /* If we actually need the PLTE chunk (ie for a paletted image), we do
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500958 * whatever the normal CRC configuration tells us. However, if we
959 * have an RGB image, the PLTE can be considered ancillary, so
960 * we will act as though it is.
961 */
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -0500962#ifndef PNG_READ_OPT_PLTE_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600963 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600964#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600965 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500966 png_crc_finish(png_ptr, 0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600967 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600968
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -0500969#ifndef PNG_READ_OPT_PLTE_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600970 else if (png_crc_error(png_ptr)) /* Only if we have a CRC error */
971 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600972 /* If we don't want to use the data from an ancillary chunk,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600973 * we have two options: an error abort, or a warning and we
974 * ignore the data in this chunk (which should be OK, since
975 * it's considered ancillary for a RGB or RGBA image).
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600976 *
977 * IMPLEMENTATION NOTE: this is only here because png_crc_finish uses the
978 * chunk type to determine whether to check the ancillary or the critical
979 * flags.
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600980 */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600981 if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE))
982 {
983 if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)
Glenn Randers-Pehrson45625ec2014-02-22 23:09:27 -0600984 return;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600985
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600986 else
Glenn Randers-Pehrson45625ec2014-02-22 23:09:27 -0600987 png_chunk_error(png_ptr, "CRC error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600988 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600989
Andreas Dilger47a0c421997-05-16 02:46:07 -0500990 /* Otherwise, we (optionally) emit a warning and use the chunk. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600991 else if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN))
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600992 png_chunk_warning(png_ptr, "CRC error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600993 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600994#endif
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500995
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600996 /* TODO: png_set_PLTE has the side effect of setting png_ptr->palette to its
997 * own copy of the palette. This has the side effect that when png_start_row
998 * is called (this happens after any call to png_read_update_info) the
999 * info_ptr palette gets changed. This is extremely unexpected and
1000 * confusing.
1001 *
1002 * Fix this by not sharing the palette in this way.
1003 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001004 png_set_PLTE(png_ptr, info_ptr, palette, num);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001005
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001006 /* The three chunks, bKGD, hIST and tRNS *must* appear after PLTE and before
1007 * IDAT. Prior to 1.6.0 this was not checked; instead the code merely
1008 * checked the apparent validity of a tRNS chunk inserted before PLTE on a
1009 * palette PNG. 1.6.0 attempts to rigorously follow the standard and
1010 * therefore does a benign error if the erroneous condition is detected *and*
1011 * cancels the tRNS if the benign error returns. The alternative is to
1012 * amend the standard since it would be rather hypocritical of the standards
1013 * maintainers to ignore it.
1014 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001015#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001016 if (png_ptr->num_trans > 0 ||
1017 (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS) != 0))
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001018 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001019 /* Cancel this because otherwise it would be used if the transforms
1020 * require it. Don't cancel the 'valid' flag because this would prevent
1021 * detection of duplicate chunks.
1022 */
1023 png_ptr->num_trans = 0;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001024
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001025 if (info_ptr != NULL)
1026 info_ptr->num_trans = 0;
1027
1028 png_chunk_benign_error(png_ptr, "tRNS must be after");
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001029 }
1030#endif
1031
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001032#ifdef PNG_READ_hIST_SUPPORTED
1033 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) != 0)
1034 png_chunk_benign_error(png_ptr, "hIST must be after");
1035#endif
1036
1037#ifdef PNG_READ_bKGD_SUPPORTED
1038 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) != 0)
1039 png_chunk_benign_error(png_ptr, "bKGD must be after");
1040#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001041}
Guy Schalnate5a37791996-06-05 15:50:50 -05001042
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001043void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001044png_handle_IEND(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001045{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001046 png_debug(1, "in png_handle_IEND");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001047
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001048 if (!(png_ptr->mode & PNG_HAVE_IHDR) || !(png_ptr->mode & PNG_HAVE_IDAT))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001049 png_chunk_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001050
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001051 png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001052
Andreas Dilger47a0c421997-05-16 02:46:07 -05001053 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001054
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001055 if (length != 0)
1056 png_chunk_benign_error(png_ptr, "invalid");
1057
1058 PNG_UNUSED(info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001059}
1060
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001061#ifdef PNG_READ_gAMA_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001062void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001063png_handle_gAMA(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001064{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001065 png_fixed_point igamma;
Guy Schalnat0d580581995-07-20 02:43:20 -05001066 png_byte buf[4];
1067
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001068 png_debug(1, "in png_handle_gAMA");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001069
Guy Schalnate5a37791996-06-05 15:50:50 -05001070 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001071 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001072
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001073 else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001074 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001075 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001076 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001077 return;
1078 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001079
Guy Schalnat0d580581995-07-20 02:43:20 -05001080 if (length != 4)
1081 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001082 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001083 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnat0d580581995-07-20 02:43:20 -05001084 return;
1085 }
1086
1087 png_crc_read(png_ptr, buf, 4);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001088
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001089 if (png_crc_finish(png_ptr, 0))
1090 return;
1091
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001092 igamma = png_get_fixed_point(NULL, buf);
Glenn Randers-Pehrson33893092010-10-23 13:20:18 -05001093
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001094 png_colorspace_set_gamma(png_ptr, &png_ptr->colorspace, igamma);
1095 png_colorspace_sync(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001096}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001097#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001098
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001099#ifdef PNG_READ_sBIT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001100void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001101png_handle_sBIT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001102{
John Bowler414d7b52014-02-06 11:32:57 -06001103 unsigned int truelen, i;
1104 png_byte sample_depth;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001105 png_byte buf[4];
Guy Schalnat69b14481996-01-10 02:56:49 -06001106
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001107 png_debug(1, "in png_handle_sBIT");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001108
Guy Schalnate5a37791996-06-05 15:50:50 -05001109 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001110 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001111
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001112 else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001113 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001114 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001115 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001116 return;
1117 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001118
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001119 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001120 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001121 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001122 png_chunk_benign_error(png_ptr, "duplicate");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001123 return;
1124 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001125
Guy Schalnat0d580581995-07-20 02:43:20 -05001126 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
John Bowler414d7b52014-02-06 11:32:57 -06001127 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001128 truelen = 3;
John Bowler414d7b52014-02-06 11:32:57 -06001129 sample_depth = 8;
1130 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001131
Guy Schalnat0d580581995-07-20 02:43:20 -05001132 else
John Bowler414d7b52014-02-06 11:32:57 -06001133 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001134 truelen = png_ptr->channels;
John Bowler414d7b52014-02-06 11:32:57 -06001135 sample_depth = png_ptr->bit_depth;
1136 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001137
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001138 if (length != truelen || length > 4)
Guy Schalnat0d580581995-07-20 02:43:20 -05001139 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001140 png_chunk_benign_error(png_ptr, "invalid");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001141 png_crc_finish(png_ptr, length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001142 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001143 }
1144
John Bowler414d7b52014-02-06 11:32:57 -06001145 buf[0] = buf[1] = buf[2] = buf[3] = sample_depth;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001146 png_crc_read(png_ptr, buf, truelen);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001147
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001148 if (png_crc_finish(png_ptr, 0))
1149 return;
1150
John Bowler414d7b52014-02-06 11:32:57 -06001151 for (i=0; i<truelen; ++i)
1152 if (buf[i] == 0 || buf[i] > sample_depth)
1153 {
1154 png_chunk_benign_error(png_ptr, "invalid");
1155 return;
1156 }
1157
Guy Schalnat0d580581995-07-20 02:43:20 -05001158 if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
1159 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001160 png_ptr->sig_bit.red = buf[0];
1161 png_ptr->sig_bit.green = buf[1];
1162 png_ptr->sig_bit.blue = buf[2];
1163 png_ptr->sig_bit.alpha = buf[3];
Guy Schalnat0d580581995-07-20 02:43:20 -05001164 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001165
Guy Schalnat0d580581995-07-20 02:43:20 -05001166 else
1167 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001168 png_ptr->sig_bit.gray = buf[0];
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001169 png_ptr->sig_bit.red = buf[0];
1170 png_ptr->sig_bit.green = buf[0];
1171 png_ptr->sig_bit.blue = buf[0];
Guy Schalnat6d764711995-12-19 03:22:19 -06001172 png_ptr->sig_bit.alpha = buf[1];
Guy Schalnat0d580581995-07-20 02:43:20 -05001173 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001174
Andreas Dilger47a0c421997-05-16 02:46:07 -05001175 png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
Guy Schalnat0d580581995-07-20 02:43:20 -05001176}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001177#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001178
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001179#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001180void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001181png_handle_cHRM(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001182{
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001183 png_byte buf[32];
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001184 png_xy xy;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -06001185
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001186 png_debug(1, "in png_handle_cHRM");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001187
Guy Schalnate5a37791996-06-05 15:50:50 -05001188 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001189 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001190
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001191 else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001192 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001193 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001194 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001195 return;
1196 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001197
Guy Schalnat0d580581995-07-20 02:43:20 -05001198 if (length != 32)
1199 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001200 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001201 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001202 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001203 }
1204
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001205 png_crc_read(png_ptr, buf, 32);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001206
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001207 if (png_crc_finish(png_ptr, 0))
1208 return;
1209
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001210 xy.whitex = png_get_fixed_point(NULL, buf);
1211 xy.whitey = png_get_fixed_point(NULL, buf + 4);
1212 xy.redx = png_get_fixed_point(NULL, buf + 8);
1213 xy.redy = png_get_fixed_point(NULL, buf + 12);
1214 xy.greenx = png_get_fixed_point(NULL, buf + 16);
1215 xy.greeny = png_get_fixed_point(NULL, buf + 20);
1216 xy.bluex = png_get_fixed_point(NULL, buf + 24);
1217 xy.bluey = png_get_fixed_point(NULL, buf + 28);
Glenn Randers-Pehrson33893092010-10-23 13:20:18 -05001218
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001219 if (xy.whitex == PNG_FIXED_ERROR ||
1220 xy.whitey == PNG_FIXED_ERROR ||
1221 xy.redx == PNG_FIXED_ERROR ||
1222 xy.redy == PNG_FIXED_ERROR ||
1223 xy.greenx == PNG_FIXED_ERROR ||
1224 xy.greeny == PNG_FIXED_ERROR ||
1225 xy.bluex == PNG_FIXED_ERROR ||
1226 xy.bluey == PNG_FIXED_ERROR)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001227 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001228 png_chunk_benign_error(png_ptr, "invalid values");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001229 return;
1230 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001231
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001232 /* If a colorspace error has already been output skip this chunk */
1233 if (png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID)
1234 return;
1235
1236 if (png_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001237 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001238 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1239 png_colorspace_sync(png_ptr, info_ptr);
1240 png_chunk_benign_error(png_ptr, "duplicate");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001241 return;
1242 }
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001243
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001244 png_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM;
1245 (void)png_colorspace_set_chromaticities(png_ptr, &png_ptr->colorspace, &xy,
1246 1/*prefer cHRM values*/);
1247 png_colorspace_sync(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001248}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001249#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001250
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001251#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001252void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001253png_handle_sRGB(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001254{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001255 png_byte intent;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001256
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001257 png_debug(1, "in png_handle_sRGB");
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001258
1259 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001260 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001261
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001262 else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE))
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001263 {
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001264 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001265 png_chunk_benign_error(png_ptr, "out of place");
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001266 return;
1267 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001268
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001269 if (length != 1)
1270 {
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001271 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001272 png_chunk_benign_error(png_ptr, "invalid");
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001273 return;
1274 }
1275
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001276 png_crc_read(png_ptr, &intent, 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001277
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001278 if (png_crc_finish(png_ptr, 0))
1279 return;
1280
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001281 /* If a colorspace error has already been output skip this chunk */
1282 if (png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID)
1283 return;
John Bowler88b77cc2011-05-05 06:49:55 -05001284
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001285 /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect
1286 * this.
1287 */
1288 if (png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT)
John Bowlerb11b31a2012-03-21 07:55:46 -05001289 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001290 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1291 png_colorspace_sync(png_ptr, info_ptr);
1292 png_chunk_benign_error(png_ptr, "too many profiles");
John Bowlerb11b31a2012-03-21 07:55:46 -05001293 return;
1294 }
John Bowler736f40f2011-08-25 16:19:44 -05001295
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001296 (void)png_colorspace_set_sRGB(png_ptr, &png_ptr->colorspace, intent);
1297 png_colorspace_sync(png_ptr, info_ptr);
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06001298}
1299#endif /* PNG_READ_sRGB_SUPPORTED */
1300
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001301#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001302void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001303png_handle_iCCP(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001304/* Note: this does not properly handle profiles that are > 64K under DOS */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001305{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001306 png_const_charp errmsg = NULL; /* error message output, or no error */
1307 int finished = 0; /* crc checked */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001308
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001309 png_debug(1, "in png_handle_iCCP");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001310
1311 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001312 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001313
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001314 else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001315 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001316 png_crc_finish(png_ptr, length);
1317 png_chunk_benign_error(png_ptr, "out of place");
1318 return;
1319 }
1320
1321 /* Consistent with all the above colorspace handling an obviously *invalid*
1322 * chunk is just ignored, so does not invalidate the color space. An
1323 * alternative is to set the 'invalid' flags at the start of this routine
1324 * and only clear them in they were not set before and all the tests pass.
1325 * The minimum 'deflate' stream is assumed to be just the 2 byte header and 4
1326 * byte checksum. The keyword must be one character and there is a
1327 * terminator (0) byte and the compression method.
1328 */
1329 if (length < 9)
1330 {
1331 png_crc_finish(png_ptr, length);
1332 png_chunk_benign_error(png_ptr, "too short");
1333 return;
1334 }
1335
1336 /* If a colorspace error has already been output skip this chunk */
1337 if (png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID)
1338 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001339 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001340 return;
1341 }
1342
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001343 /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect
1344 * this.
1345 */
1346 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) == 0)
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06001347 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001348 uInt read_length, keyword_length;
1349 char keyword[81];
1350
1351 /* Find the keyword; the keyword plus separator and compression method
1352 * bytes can be at most 81 characters long.
1353 */
1354 read_length = 81; /* maximum */
1355 if (read_length > length)
1356 read_length = (uInt)length;
1357
1358 png_crc_read(png_ptr, (png_bytep)keyword, read_length);
1359 length -= read_length;
1360
1361 keyword_length = 0;
1362 while (keyword_length < 80 && keyword_length < read_length &&
1363 keyword[keyword_length] != 0)
1364 ++keyword_length;
1365
1366 /* TODO: make the keyword checking common */
1367 if (keyword_length >= 1 && keyword_length <= 79)
1368 {
1369 /* We only understand '0' compression - deflate - so if we get a
1370 * different value we can't safely decode the chunk.
1371 */
1372 if (keyword_length+1 < read_length &&
1373 keyword[keyword_length+1] == PNG_COMPRESSION_TYPE_BASE)
1374 {
1375 read_length -= keyword_length+2;
1376
John Bowler0c7ac062013-05-07 21:59:05 -05001377 if (png_inflate_claim(png_ptr, png_iCCP) == Z_OK)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001378 {
1379 Byte profile_header[132];
1380 Byte local_buffer[PNG_INFLATE_BUF_SIZE];
1381 png_alloc_size_t size = (sizeof profile_header);
1382
1383 png_ptr->zstream.next_in = (Bytef*)keyword + (keyword_length+2);
1384 png_ptr->zstream.avail_in = read_length;
1385 (void)png_inflate_read(png_ptr, local_buffer,
1386 (sizeof local_buffer), &length, profile_header, &size,
1387 0/*finish: don't, because the output is too small*/);
1388
1389 if (size == 0)
1390 {
1391 /* We have the ICC profile header; do the basic header checks.
1392 */
1393 const png_uint_32 profile_length =
1394 png_get_uint_32(profile_header);
1395
1396 if (png_icc_check_length(png_ptr, &png_ptr->colorspace,
1397 keyword, profile_length))
1398 {
1399 /* The length is apparently ok, so we can check the 132
1400 * byte header.
1401 */
1402 if (png_icc_check_header(png_ptr, &png_ptr->colorspace,
1403 keyword, profile_length, profile_header,
1404 png_ptr->color_type))
1405 {
1406 /* Now read the tag table; a variable size buffer is
1407 * needed at this point, allocate one for the whole
1408 * profile. The header check has already validated
1409 * that none of these stuff will overflow.
1410 */
1411 const png_uint_32 tag_count = png_get_uint_32(
1412 profile_header+128);
1413 png_bytep profile = png_read_buffer(png_ptr,
1414 profile_length, 2/*silent*/);
1415
1416 if (profile != NULL)
1417 {
1418 memcpy(profile, profile_header,
1419 (sizeof profile_header));
1420
1421 size = 12 * tag_count;
1422
1423 (void)png_inflate_read(png_ptr, local_buffer,
1424 (sizeof local_buffer), &length,
1425 profile + (sizeof profile_header), &size, 0);
1426
Glenn Randers-Pehrson9c5a1ba2014-02-17 09:12:52 -06001427 /* Still expect a buffer error because we expect
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001428 * there to be some tag data!
1429 */
1430 if (size == 0)
1431 {
1432 if (png_icc_check_tag_table(png_ptr,
1433 &png_ptr->colorspace, keyword, profile_length,
1434 profile))
1435 {
1436 /* The profile has been validated for basic
1437 * security issues, so read the whole thing in.
1438 */
1439 size = profile_length - (sizeof profile_header)
1440 - 12 * tag_count;
1441
1442 (void)png_inflate_read(png_ptr, local_buffer,
1443 (sizeof local_buffer), &length,
1444 profile + (sizeof profile_header) +
1445 12 * tag_count, &size, 1/*finish*/);
1446
1447 if (length > 0 && !(png_ptr->flags &
1448 PNG_FLAG_BENIGN_ERRORS_WARN))
1449 errmsg = "extra compressed data";
1450
1451 /* But otherwise allow extra data: */
1452 else if (size == 0)
1453 {
1454 if (length > 0)
1455 {
1456 /* This can be handled completely, so
1457 * keep going.
1458 */
1459 png_chunk_warning(png_ptr,
1460 "extra compressed data");
1461 }
1462
1463 png_crc_finish(png_ptr, length);
1464 finished = 1;
1465
1466# ifdef PNG_sRGB_SUPPORTED
1467 /* Check for a match against sRGB */
1468 png_icc_set_sRGB(png_ptr,
1469 &png_ptr->colorspace, profile,
1470 png_ptr->zstream.adler);
1471# endif
1472
1473 /* Steal the profile for info_ptr. */
1474 if (info_ptr != NULL)
1475 {
1476 png_free_data(png_ptr, info_ptr,
1477 PNG_FREE_ICCP, 0);
1478
1479 info_ptr->iccp_name = png_voidcast(char*,
1480 png_malloc_base(png_ptr,
1481 keyword_length+1));
1482 if (info_ptr->iccp_name != NULL)
1483 {
1484 memcpy(info_ptr->iccp_name, keyword,
1485 keyword_length+1);
1486 info_ptr->iccp_proflen =
1487 profile_length;
1488 info_ptr->iccp_profile = profile;
1489 png_ptr->read_buffer = NULL; /*steal*/
1490 info_ptr->free_me |= PNG_FREE_ICCP;
1491 info_ptr->valid |= PNG_INFO_iCCP;
1492 }
1493
1494 else
1495 {
1496 png_ptr->colorspace.flags |=
1497 PNG_COLORSPACE_INVALID;
1498 errmsg = "out of memory";
1499 }
1500 }
1501
1502 /* else the profile remains in the read
1503 * buffer which gets reused for subsequent
1504 * chunks.
1505 */
1506
1507 if (info_ptr != NULL)
1508 png_colorspace_sync(png_ptr, info_ptr);
1509
1510 if (errmsg == NULL)
1511 {
1512 png_ptr->zowner = 0;
1513 return;
1514 }
1515 }
1516
1517 else if (size > 0)
1518 errmsg = "truncated";
1519
1520 else
1521 errmsg = png_ptr->zstream.msg;
1522 }
1523
1524 /* else png_icc_check_tag_table output an error */
1525 }
1526
1527 else /* profile truncated */
1528 errmsg = png_ptr->zstream.msg;
1529 }
1530
1531 else
1532 errmsg = "out of memory";
1533 }
1534
1535 /* else png_icc_check_header output an error */
1536 }
1537
1538 /* else png_icc_check_length output an error */
1539 }
1540
1541 else /* profile truncated */
1542 errmsg = png_ptr->zstream.msg;
1543
1544 /* Release the stream */
1545 png_ptr->zowner = 0;
1546 }
1547
1548 else /* png_inflate_claim failed */
1549 errmsg = png_ptr->zstream.msg;
1550 }
1551
1552 else
1553 errmsg = "bad compression method"; /* or missing */
1554 }
1555
1556 else
1557 errmsg = "bad keyword";
1558 }
1559
1560 else
1561 errmsg = "too many profiles";
1562
1563 /* Failure: the reason is in 'errmsg' */
1564 if (!finished)
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06001565 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06001566
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001567 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1568 png_colorspace_sync(png_ptr, info_ptr);
1569 if (errmsg != NULL) /* else already output */
1570 png_chunk_benign_error(png_ptr, errmsg);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001571}
1572#endif /* PNG_READ_iCCP_SUPPORTED */
1573
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001574#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001575void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001576png_handle_sPLT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001577/* Note: this does not properly handle chunks that are > 64K under DOS */
1578{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001579 png_bytep entry_start, buffer;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001580 png_sPLT_t new_palette;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001581 png_sPLT_entryp pp;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001582 png_uint_32 data_length;
1583 int entry_size, i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001584 png_uint_32 skip = 0;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001585 png_uint_32 dl;
1586 png_size_t max_dl;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001587
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001588 png_debug(1, "in png_handle_sPLT");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001589
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001590#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001591 if (png_ptr->user_chunk_cache_max != 0)
1592 {
1593 if (png_ptr->user_chunk_cache_max == 1)
1594 {
1595 png_crc_finish(png_ptr, length);
1596 return;
1597 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001598
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001599 if (--png_ptr->user_chunk_cache_max == 1)
1600 {
1601 png_warning(png_ptr, "No space in chunk cache for sPLT");
1602 png_crc_finish(png_ptr, length);
1603 return;
1604 }
1605 }
1606#endif
1607
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001608 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001609 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001610
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001611 else if (png_ptr->mode & PNG_HAVE_IDAT)
1612 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001613 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001614 png_chunk_benign_error(png_ptr, "out of place");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001615 return;
1616 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001617
1618#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001619 if (length > 65535U)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001620 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001621 png_crc_finish(png_ptr, length);
1622 png_chunk_benign_error(png_ptr, "too large to fit in memory");
1623 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001624 }
1625#endif
1626
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001627 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
1628 if (buffer == NULL)
1629 {
1630 png_crc_finish(png_ptr, length);
1631 png_chunk_benign_error(png_ptr, "out of memory");
1632 return;
1633 }
1634
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001635
1636 /* WARNING: this may break if size_t is less than 32 bits; it is assumed
1637 * that the PNG_MAX_MALLOC_64K test is enabled in this case, but this is a
1638 * potential breakage point if the types in pngconf.h aren't exactly right.
1639 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001640 png_crc_read(png_ptr, buffer, length);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001641
1642 if (png_crc_finish(png_ptr, skip))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001643 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001644
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001645 buffer[length] = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001646
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001647 for (entry_start = buffer; *entry_start; entry_start++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001648 /* Empty loop to find end of name */ ;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001649
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001650 ++entry_start;
1651
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001652 /* A sample depth should follow the separator, and we should be on it */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001653 if (entry_start > buffer + length - 2)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001654 {
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -05001655 png_warning(png_ptr, "malformed sPLT chunk");
1656 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001657 }
1658
1659 new_palette.depth = *entry_start++;
Glenn Randers-Pehrsona565f0e2010-03-06 08:24:45 -06001660 entry_size = (new_palette.depth == 8 ? 6 : 10);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001661 /* This must fit in a png_uint_32 because it is derived from the original
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001662 * chunk data length.
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001663 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001664 data_length = length - (png_uint_32)(entry_start - buffer);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001665
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001666 /* Integrity-check the data length */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001667 if (data_length % entry_size)
1668 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001669 png_warning(png_ptr, "sPLT chunk has bad length");
1670 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001671 }
1672
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001673 dl = (png_int_32)(data_length / entry_size);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001674 max_dl = PNG_SIZE_MAX / (sizeof (png_sPLT_entry));
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001675
1676 if (dl > max_dl)
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001677 {
1678 png_warning(png_ptr, "sPLT chunk too long");
1679 return;
1680 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001681
1682 new_palette.nentries = (png_int_32)(data_length / entry_size);
1683
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001684 new_palette.entries = (png_sPLT_entryp)png_malloc_warn(
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001685 png_ptr, new_palette.nentries * (sizeof (png_sPLT_entry)));
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001686
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001687 if (new_palette.entries == NULL)
1688 {
1689 png_warning(png_ptr, "sPLT chunk requires too much memory");
1690 return;
1691 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001692
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001693#ifdef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001694 for (i = 0; i < new_palette.nentries; i++)
1695 {
Glenn Randers-Pehrson90b878c2009-10-07 12:44:35 -05001696 pp = new_palette.entries + i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001697
1698 if (new_palette.depth == 8)
1699 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001700 pp->red = *entry_start++;
1701 pp->green = *entry_start++;
1702 pp->blue = *entry_start++;
1703 pp->alpha = *entry_start++;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001704 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001705
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001706 else
1707 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001708 pp->red = png_get_uint_16(entry_start); entry_start += 2;
1709 pp->green = png_get_uint_16(entry_start); entry_start += 2;
1710 pp->blue = png_get_uint_16(entry_start); entry_start += 2;
1711 pp->alpha = png_get_uint_16(entry_start); entry_start += 2;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001712 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001713
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001714 pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
1715 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001716#else
1717 pp = new_palette.entries;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001718
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001719 for (i = 0; i < new_palette.nentries; i++)
1720 {
1721
1722 if (new_palette.depth == 8)
1723 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001724 pp[i].red = *entry_start++;
1725 pp[i].green = *entry_start++;
1726 pp[i].blue = *entry_start++;
1727 pp[i].alpha = *entry_start++;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001728 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001729
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001730 else
1731 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001732 pp[i].red = png_get_uint_16(entry_start); entry_start += 2;
1733 pp[i].green = png_get_uint_16(entry_start); entry_start += 2;
1734 pp[i].blue = png_get_uint_16(entry_start); entry_start += 2;
1735 pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001736 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001737
Glenn Randers-Pehrsonf27592a2011-03-21 18:05:40 -05001738 pp[i].frequency = png_get_uint_16(entry_start); entry_start += 2;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001739 }
1740#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001741
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001742 /* Discard all chunk data except the name and stash that */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001743 new_palette.name = (png_charp)buffer;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001744
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001745 png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001746
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001747 png_free(png_ptr, new_palette.entries);
1748}
1749#endif /* PNG_READ_sPLT_SUPPORTED */
1750
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001751#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001752void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001753png_handle_tRNS(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001754{
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001755 png_byte readbuf[PNG_MAX_PALETTE_LENGTH];
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06001756
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001757 png_debug(1, "in png_handle_tRNS");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001758
Guy Schalnate5a37791996-06-05 15:50:50 -05001759 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001760 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001761
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001762 else if (png_ptr->mode & PNG_HAVE_IDAT)
1763 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001764 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001765 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001766 return;
1767 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001768
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001769 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001770 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001771 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001772 png_chunk_benign_error(png_ptr, "duplicate");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001773 return;
1774 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001775
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001776 if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
Guy Schalnat0d580581995-07-20 02:43:20 -05001777 {
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001778 png_byte buf[2];
Guy Schalnat0d580581995-07-20 02:43:20 -05001779
1780 if (length != 2)
1781 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001782 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001783 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001784 return;
1785 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001786
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001787 png_crc_read(png_ptr, buf, 2);
1788 png_ptr->num_trans = 1;
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -05001789 png_ptr->trans_color.gray = png_get_uint_16(buf);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001790 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001791
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001792 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
1793 {
1794 png_byte buf[6];
1795
1796 if (length != 6)
1797 {
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001798 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001799 png_chunk_benign_error(png_ptr, "invalid");
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001800 return;
1801 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001802
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001803 png_crc_read(png_ptr, buf, length);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001804 png_ptr->num_trans = 1;
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -05001805 png_ptr->trans_color.red = png_get_uint_16(buf);
1806 png_ptr->trans_color.green = png_get_uint_16(buf + 2);
1807 png_ptr->trans_color.blue = png_get_uint_16(buf + 4);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001808 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001809
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001810 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1811 {
1812 if (!(png_ptr->mode & PNG_HAVE_PLTE))
1813 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001814 /* TODO: is this actually an error in the ISO spec? */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001815 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001816 png_chunk_benign_error(png_ptr, "out of place");
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001817 return;
1818 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001819
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001820 if (length > png_ptr->num_palette || length > PNG_MAX_PALETTE_LENGTH ||
1821 length == 0)
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06001822 {
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06001823 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001824 png_chunk_benign_error(png_ptr, "invalid");
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06001825 return;
1826 }
1827
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001828 png_crc_read(png_ptr, readbuf, length);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001829 png_ptr->num_trans = (png_uint_16)length;
1830 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001831
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001832 else
Guy Schalnate5a37791996-06-05 15:50:50 -05001833 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001834 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001835 png_chunk_benign_error(png_ptr, "invalid with alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -05001836 return;
1837 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001838
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001839 if (png_crc_finish(png_ptr, 0))
Glenn Randers-Pehrsona7dbcba2007-05-15 16:16:34 -05001840 {
1841 png_ptr->num_trans = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001842 return;
Glenn Randers-Pehrsona7dbcba2007-05-15 16:16:34 -05001843 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001844
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001845 /* TODO: this is a horrible side effect in the palette case because the
1846 * png_struct ends up with a pointer to the tRNS buffer owned by the
1847 * png_info. Fix this.
1848 */
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06001849 png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001850 &(png_ptr->trans_color));
Guy Schalnat0d580581995-07-20 02:43:20 -05001851}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001852#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001853
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001854#ifdef PNG_READ_bKGD_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001855void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001856png_handle_bKGD(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001857{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001858 unsigned int truelen;
Guy Schalnat0d580581995-07-20 02:43:20 -05001859 png_byte buf[6];
John Bowlercb0b2962011-05-12 21:48:29 -05001860 png_color_16 background;
Guy Schalnat0d580581995-07-20 02:43:20 -05001861
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001862 png_debug(1, "in png_handle_bKGD");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001863
Guy Schalnate5a37791996-06-05 15:50:50 -05001864 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001865 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001866
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001867 else if ((png_ptr->mode & PNG_HAVE_IDAT) ||
1868 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
1869 !(png_ptr->mode & PNG_HAVE_PLTE)))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001870 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001871 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001872 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001873 return;
1874 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001875
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001876 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001877 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001878 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001879 png_chunk_benign_error(png_ptr, "duplicate");
Guy Schalnate5a37791996-06-05 15:50:50 -05001880 return;
1881 }
1882
Guy Schalnat0d580581995-07-20 02:43:20 -05001883 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1884 truelen = 1;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001885
Guy Schalnat0d580581995-07-20 02:43:20 -05001886 else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
1887 truelen = 6;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001888
Guy Schalnat0d580581995-07-20 02:43:20 -05001889 else
1890 truelen = 2;
1891
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001892 if (length != truelen)
Guy Schalnat0d580581995-07-20 02:43:20 -05001893 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001894 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001895 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnat0d580581995-07-20 02:43:20 -05001896 return;
1897 }
1898
Andreas Dilger47a0c421997-05-16 02:46:07 -05001899 png_crc_read(png_ptr, buf, truelen);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001900
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001901 if (png_crc_finish(png_ptr, 0))
1902 return;
1903
Guy Schalnate5a37791996-06-05 15:50:50 -05001904 /* We convert the index value into RGB components so that we can allow
1905 * arbitrary RGB values for background when we have transparency, and
1906 * so it is easy to determine the RGB values of the background color
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001907 * from the info_ptr struct.
1908 */
Guy Schalnat0d580581995-07-20 02:43:20 -05001909 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Guy Schalnate5a37791996-06-05 15:50:50 -05001910 {
John Bowlercb0b2962011-05-12 21:48:29 -05001911 background.index = buf[0];
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001912
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001913 if (info_ptr && info_ptr->num_palette)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001914 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001915 if (buf[0] >= info_ptr->num_palette)
1916 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001917 png_chunk_benign_error(png_ptr, "invalid index");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001918 return;
1919 }
1920
John Bowlercb0b2962011-05-12 21:48:29 -05001921 background.red = (png_uint_16)png_ptr->palette[buf[0]].red;
1922 background.green = (png_uint_16)png_ptr->palette[buf[0]].green;
1923 background.blue = (png_uint_16)png_ptr->palette[buf[0]].blue;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001924 }
John Bowlercb0b2962011-05-12 21:48:29 -05001925
1926 else
1927 background.red = background.green = background.blue = 0;
1928
1929 background.gray = 0;
Guy Schalnate5a37791996-06-05 15:50:50 -05001930 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001931
Andreas Dilger47a0c421997-05-16 02:46:07 -05001932 else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) /* GRAY */
Guy Schalnate5a37791996-06-05 15:50:50 -05001933 {
John Bowlercb0b2962011-05-12 21:48:29 -05001934 background.index = 0;
1935 background.red =
1936 background.green =
1937 background.blue =
1938 background.gray = png_get_uint_16(buf);
Guy Schalnate5a37791996-06-05 15:50:50 -05001939 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001940
Guy Schalnat0d580581995-07-20 02:43:20 -05001941 else
1942 {
John Bowlercb0b2962011-05-12 21:48:29 -05001943 background.index = 0;
1944 background.red = png_get_uint_16(buf);
1945 background.green = png_get_uint_16(buf + 2);
1946 background.blue = png_get_uint_16(buf + 4);
1947 background.gray = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -05001948 }
1949
John Bowlercb0b2962011-05-12 21:48:29 -05001950 png_set_bKGD(png_ptr, info_ptr, &background);
Guy Schalnat0d580581995-07-20 02:43:20 -05001951}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001952#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001953
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001954#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001955void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001956png_handle_hIST(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001957{
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001958 unsigned int num, i;
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001959 png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];
Guy Schalnat0d580581995-07-20 02:43:20 -05001960
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001961 png_debug(1, "in png_handle_hIST");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001962
Guy Schalnate5a37791996-06-05 15:50:50 -05001963 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001964 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001965
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001966 else if ((png_ptr->mode & PNG_HAVE_IDAT) || !(png_ptr->mode & PNG_HAVE_PLTE))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001967 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001968 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001969 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001970 return;
1971 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001972
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001973 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001974 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001975 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001976 png_chunk_benign_error(png_ptr, "duplicate");
Guy Schalnate5a37791996-06-05 15:50:50 -05001977 return;
1978 }
1979
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001980 num = length / 2 ;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001981
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001982 if (num != png_ptr->num_palette || num > PNG_MAX_PALETTE_LENGTH)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001983 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001984 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001985 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001986 return;
1987 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001988
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001989 for (i = 0; i < num; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001990 {
1991 png_byte buf[2];
1992
1993 png_crc_read(png_ptr, buf, 2);
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06001994 readbuf[i] = png_get_uint_16(buf);
Guy Schalnat0d580581995-07-20 02:43:20 -05001995 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001996
1997 if (png_crc_finish(png_ptr, 0))
1998 return;
1999
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06002000 png_set_hIST(png_ptr, info_ptr, readbuf);
Guy Schalnat0d580581995-07-20 02:43:20 -05002001}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002002#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002003
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002004#ifdef PNG_READ_pHYs_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002005void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002006png_handle_pHYs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002007{
2008 png_byte buf[9];
2009 png_uint_32 res_x, res_y;
2010 int unit_type;
2011
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002012 png_debug(1, "in png_handle_pHYs");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002013
Guy Schalnate5a37791996-06-05 15:50:50 -05002014 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002015 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002016
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002017 else if (png_ptr->mode & PNG_HAVE_IDAT)
2018 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002019 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002020 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002021 return;
2022 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002023
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002024 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002025 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002026 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002027 png_chunk_benign_error(png_ptr, "duplicate");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002028 return;
2029 }
Guy Schalnate5a37791996-06-05 15:50:50 -05002030
Guy Schalnat0d580581995-07-20 02:43:20 -05002031 if (length != 9)
2032 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002033 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002034 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnat0d580581995-07-20 02:43:20 -05002035 return;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002036 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002037
2038 png_crc_read(png_ptr, buf, 9);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002039
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002040 if (png_crc_finish(png_ptr, 0))
2041 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05002042
2043 res_x = png_get_uint_32(buf);
2044 res_y = png_get_uint_32(buf + 4);
2045 unit_type = buf[8];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002046 png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
Guy Schalnat0d580581995-07-20 02:43:20 -05002047}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002048#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002049
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002050#ifdef PNG_READ_oFFs_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002051void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002052png_handle_oFFs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002053{
2054 png_byte buf[9];
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002055 png_int_32 offset_x, offset_y;
Guy Schalnat0d580581995-07-20 02:43:20 -05002056 int unit_type;
2057
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002058 png_debug(1, "in png_handle_oFFs");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002059
Guy Schalnate5a37791996-06-05 15:50:50 -05002060 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002061 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002062
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002063 else if (png_ptr->mode & PNG_HAVE_IDAT)
2064 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002065 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002066 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002067 return;
2068 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002069
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002070 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002071 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002072 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002073 png_chunk_benign_error(png_ptr, "duplicate");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002074 return;
2075 }
Guy Schalnate5a37791996-06-05 15:50:50 -05002076
Guy Schalnat0d580581995-07-20 02:43:20 -05002077 if (length != 9)
2078 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002079 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002080 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnat0d580581995-07-20 02:43:20 -05002081 return;
2082 }
2083
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002084 png_crc_read(png_ptr, buf, 9);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002085
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002086 if (png_crc_finish(png_ptr, 0))
2087 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05002088
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002089 offset_x = png_get_int_32(buf);
2090 offset_y = png_get_int_32(buf + 4);
Guy Schalnat0d580581995-07-20 02:43:20 -05002091 unit_type = buf[8];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002092 png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
2093}
2094#endif
2095
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002096#ifdef PNG_READ_pCAL_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002097/* Read the pCAL chunk (described in the PNG Extensions document) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002098void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002099png_handle_pCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002100{
Andreas Dilger47a0c421997-05-16 02:46:07 -05002101 png_int_32 X0, X1;
2102 png_byte type, nparams;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002103 png_bytep buffer, buf, units, endptr;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002104 png_charpp params;
2105 int i;
2106
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002107 png_debug(1, "in png_handle_pCAL");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002108
2109 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002110 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002111
Andreas Dilger47a0c421997-05-16 02:46:07 -05002112 else if (png_ptr->mode & PNG_HAVE_IDAT)
2113 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002114 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002115 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002116 return;
2117 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002118
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002119 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL))
Andreas Dilger47a0c421997-05-16 02:46:07 -05002120 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002121 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002122 png_chunk_benign_error(png_ptr, "duplicate");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002123 return;
2124 }
2125
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002126 png_debug1(2, "Allocating and reading pCAL chunk data (%u bytes)",
2127 length + 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002128
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002129 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
2130
2131 if (buffer == NULL)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002132 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002133 png_crc_finish(png_ptr, length);
2134 png_chunk_benign_error(png_ptr, "out of memory");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002135 return;
2136 }
2137
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002138 png_crc_read(png_ptr, buffer, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002139
2140 if (png_crc_finish(png_ptr, 0))
Andreas Dilger47a0c421997-05-16 02:46:07 -05002141 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002142
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002143 buffer[length] = 0; /* Null terminate the last string */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002144
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002145 png_debug(3, "Finding end of pCAL purpose string");
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002146 for (buf = buffer; *buf; buf++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002147 /* Empty loop */ ;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002148
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002149 endptr = buffer + length;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002150
2151 /* We need to have at least 12 bytes after the purpose string
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002152 * in order to get the parameter information.
2153 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002154 if (endptr <= buf + 12)
2155 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002156 png_chunk_benign_error(png_ptr, "invalid");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002157 return;
2158 }
2159
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002160 png_debug(3, "Reading pCAL X0, X1, type, nparams, and units");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002161 X0 = png_get_int_32((png_bytep)buf+1);
2162 X1 = png_get_int_32((png_bytep)buf+5);
2163 type = buf[9];
2164 nparams = buf[10];
2165 units = buf + 11;
2166
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002167 png_debug(3, "Checking pCAL equation type and number of parameters");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002168 /* Check that we have the right number of parameters for known
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002169 * equation types.
2170 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002171 if ((type == PNG_EQUATION_LINEAR && nparams != 2) ||
2172 (type == PNG_EQUATION_BASE_E && nparams != 3) ||
2173 (type == PNG_EQUATION_ARBITRARY && nparams != 3) ||
2174 (type == PNG_EQUATION_HYPERBOLIC && nparams != 4))
2175 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002176 png_chunk_benign_error(png_ptr, "invalid parameter count");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002177 return;
2178 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002179
Andreas Dilger47a0c421997-05-16 02:46:07 -05002180 else if (type >= PNG_EQUATION_LAST)
2181 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002182 png_chunk_benign_error(png_ptr, "unrecognized equation type");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002183 }
2184
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002185 for (buf = units; *buf; buf++)
Glenn Randers-Pehrsonf9f2fe01998-03-15 18:20:23 -06002186 /* Empty loop to move past the units string. */ ;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002187
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002188 png_debug(3, "Allocating pCAL parameters array");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002189
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002190 params = png_voidcast(png_charpp, png_malloc_warn(png_ptr,
2191 nparams * (sizeof (png_charp))));
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002192
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002193 if (params == NULL)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002194 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002195 png_chunk_benign_error(png_ptr, "out of memory");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002196 return;
2197 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002198
2199 /* Get pointers to the start of each parameter string. */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002200 for (i = 0; i < nparams; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002201 {
2202 buf++; /* Skip the null string terminator from previous parameter. */
2203
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002204 png_debug1(3, "Reading pCAL parameter %d", i);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002205
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002206 for (params[i] = (png_charp)buf; buf <= endptr && *buf != 0; buf++)
Glenn Randers-Pehrsonf9f2fe01998-03-15 18:20:23 -06002207 /* Empty loop to move past each parameter string */ ;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002208
2209 /* Make sure we haven't run out of data yet */
2210 if (buf > endptr)
2211 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002212 png_free(png_ptr, params);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002213 png_chunk_benign_error(png_ptr, "invalid data");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002214 return;
2215 }
2216 }
2217
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002218 png_set_pCAL(png_ptr, info_ptr, (png_charp)buffer, X0, X1, type, nparams,
2219 (png_charp)units, params);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002220
Andreas Dilger47a0c421997-05-16 02:46:07 -05002221 png_free(png_ptr, params);
Guy Schalnat0d580581995-07-20 02:43:20 -05002222}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002223#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002224
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002225#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002226/* Read the sCAL chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002227void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002228png_handle_sCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002229{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002230 png_bytep buffer;
2231 png_size_t i;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002232 int state;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002233
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002234 png_debug(1, "in png_handle_sCAL");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002235
2236 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002237 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002238
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002239 else if (png_ptr->mode & PNG_HAVE_IDAT)
2240 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002241 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002242 png_chunk_benign_error(png_ptr, "out of place");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002243 return;
2244 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002245
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002246 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002247 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002248 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002249 png_chunk_benign_error(png_ptr, "duplicate");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002250 return;
2251 }
2252
Glenn Randers-Pehrson254a5132011-06-10 17:45:48 -05002253 /* Need unit type, width, \0, height: minimum 4 bytes */
2254 else if (length < 4)
2255 {
Glenn Randers-Pehrson254a5132011-06-10 17:45:48 -05002256 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002257 png_chunk_benign_error(png_ptr, "invalid");
Glenn Randers-Pehrson254a5132011-06-10 17:45:48 -05002258 return;
2259 }
2260
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002261 png_debug1(2, "Allocating and reading sCAL chunk data (%u bytes)",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002262 length + 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002263
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002264 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002265
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002266 if (buffer == NULL)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002267 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002268 png_chunk_benign_error(png_ptr, "out of memory");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002269 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002270 return;
2271 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002272
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002273 png_crc_read(png_ptr, buffer, length);
2274 buffer[length] = 0; /* Null terminate the last string */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002275
2276 if (png_crc_finish(png_ptr, 0))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002277 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002278
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002279 /* Validate the unit. */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002280 if (buffer[0] != 1 && buffer[0] != 2)
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -05002281 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002282 png_chunk_benign_error(png_ptr, "invalid unit");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05002283 return;
2284 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002285
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002286 /* Validate the ASCII numbers, need two ASCII numbers separated by
2287 * a '\0' and they need to fit exactly in the chunk data.
2288 */
Glenn Randers-Pehrson254a5132011-06-10 17:45:48 -05002289 i = 1;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002290 state = 0;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002291
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002292 if (!png_check_fp_number((png_const_charp)buffer, length, &state, &i) ||
2293 i >= length || buffer[i++] != 0)
2294 png_chunk_benign_error(png_ptr, "bad width format");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002295
John Bowler8d261262011-06-18 13:37:11 -05002296 else if (!PNG_FP_IS_POSITIVE(state))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002297 png_chunk_benign_error(png_ptr, "non-positive width");
John Bowler8d261262011-06-18 13:37:11 -05002298
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002299 else
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -06002300 {
John Bowler168a4332011-01-16 19:32:22 -06002301 png_size_t heighti = i;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002302
Glenn Randers-Pehrson254a5132011-06-10 17:45:48 -05002303 state = 0;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002304 if (!png_check_fp_number((png_const_charp)buffer, length, &state, &i) ||
2305 i != length)
2306 png_chunk_benign_error(png_ptr, "bad height format");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002307
John Bowler8d261262011-06-18 13:37:11 -05002308 else if (!PNG_FP_IS_POSITIVE(state))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002309 png_chunk_benign_error(png_ptr, "non-positive height");
John Bowler8d261262011-06-18 13:37:11 -05002310
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002311 else
2312 /* This is the (only) success case. */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002313 png_set_sCAL_s(png_ptr, info_ptr, buffer[0],
2314 (png_charp)buffer+1, (png_charp)buffer+heighti);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002315 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002316}
2317#endif
2318
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002319#ifdef PNG_READ_tIME_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002320void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002321png_handle_tIME(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002322{
2323 png_byte buf[7];
2324 png_time mod_time;
2325
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002326 png_debug(1, "in png_handle_tIME");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002327
Guy Schalnate5a37791996-06-05 15:50:50 -05002328 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002329 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002330
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002331 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002332 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002333 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002334 png_chunk_benign_error(png_ptr, "duplicate");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002335 return;
2336 }
2337
2338 if (png_ptr->mode & PNG_HAVE_IDAT)
2339 png_ptr->mode |= PNG_AFTER_IDAT;
Guy Schalnate5a37791996-06-05 15:50:50 -05002340
Guy Schalnat0d580581995-07-20 02:43:20 -05002341 if (length != 7)
2342 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002343 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002344 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnat0d580581995-07-20 02:43:20 -05002345 return;
2346 }
2347
2348 png_crc_read(png_ptr, buf, 7);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002349
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002350 if (png_crc_finish(png_ptr, 0))
2351 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05002352
2353 mod_time.second = buf[6];
2354 mod_time.minute = buf[5];
2355 mod_time.hour = buf[4];
2356 mod_time.day = buf[3];
2357 mod_time.month = buf[2];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002358 mod_time.year = png_get_uint_16(buf);
Guy Schalnat0d580581995-07-20 02:43:20 -05002359
Andreas Dilger47a0c421997-05-16 02:46:07 -05002360 png_set_tIME(png_ptr, info_ptr, &mod_time);
Guy Schalnat0d580581995-07-20 02:43:20 -05002361}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002362#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002363
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002364#ifdef PNG_READ_tEXt_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002365/* Note: this does not properly handle chunks that are > 64K under DOS */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002366void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002367png_handle_tEXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002368{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002369 png_text text_info;
2370 png_bytep buffer;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002371 png_charp key;
Guy Schalnat6d764711995-12-19 03:22:19 -06002372 png_charp text;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002373 png_uint_32 skip = 0;
2374
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002375 png_debug(1, "in png_handle_tEXt");
Guy Schalnat0d580581995-07-20 02:43:20 -05002376
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06002377#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002378 if (png_ptr->user_chunk_cache_max != 0)
2379 {
2380 if (png_ptr->user_chunk_cache_max == 1)
2381 {
2382 png_crc_finish(png_ptr, length);
2383 return;
2384 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002385
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002386 if (--png_ptr->user_chunk_cache_max == 1)
2387 {
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002388 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002389 png_chunk_benign_error(png_ptr, "no space in chunk cache");
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002390 return;
2391 }
2392 }
2393#endif
2394
Guy Schalnate5a37791996-06-05 15:50:50 -05002395 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002396 png_chunk_error(png_ptr, "missing IHDR");
Guy Schalnate5a37791996-06-05 15:50:50 -05002397
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002398 if (png_ptr->mode & PNG_HAVE_IDAT)
2399 png_ptr->mode |= PNG_AFTER_IDAT;
2400
Andreas Dilger47a0c421997-05-16 02:46:07 -05002401#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002402 if (length > 65535U)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002403 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002404 png_crc_finish(png_ptr, length);
2405 png_chunk_benign_error(png_ptr, "too large to fit in memory");
2406 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002407 }
2408#endif
2409
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002410 buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
Glenn Randers-Pehrson97a9b482008-10-25 20:03:28 -05002411
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002412 if (buffer == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002413 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002414 png_chunk_benign_error(png_ptr, "out of memory");
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002415 return;
2416 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002417
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002418 png_crc_read(png_ptr, buffer, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002419
2420 if (png_crc_finish(png_ptr, skip))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002421 return;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002422
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002423 key = (png_charp)buffer;
2424 key[length] = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -05002425
2426 for (text = key; *text; text++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002427 /* Empty loop to find end of key */ ;
Guy Schalnat0d580581995-07-20 02:43:20 -05002428
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002429 if (text != key + length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002430 text++;
2431
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002432 text_info.compression = PNG_TEXT_COMPRESSION_NONE;
2433 text_info.key = key;
2434 text_info.lang = NULL;
2435 text_info.lang_key = NULL;
2436 text_info.itxt_length = 0;
2437 text_info.text = text;
2438 text_info.text_length = strlen(text);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002439
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002440 if (png_set_text_2(png_ptr, info_ptr, &text_info, 1))
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002441 png_warning(png_ptr, "Insufficient memory to process text chunk");
Guy Schalnat0d580581995-07-20 02:43:20 -05002442}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002443#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002444
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002445#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002446/* Note: this does not correctly handle chunks that are > 64K under DOS */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002447void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002448png_handle_zTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002449{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002450 png_const_charp errmsg = NULL;
2451 png_bytep buffer;
2452 png_uint_32 keyword_length;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002453
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002454 png_debug(1, "in png_handle_zTXt");
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002455
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06002456#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002457 if (png_ptr->user_chunk_cache_max != 0)
2458 {
2459 if (png_ptr->user_chunk_cache_max == 1)
2460 {
2461 png_crc_finish(png_ptr, length);
2462 return;
2463 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002464
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002465 if (--png_ptr->user_chunk_cache_max == 1)
2466 {
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002467 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002468 png_chunk_benign_error(png_ptr, "no space in chunk cache");
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002469 return;
2470 }
2471 }
2472#endif
2473
Guy Schalnate5a37791996-06-05 15:50:50 -05002474 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002475 png_chunk_error(png_ptr, "missing IHDR");
Guy Schalnate5a37791996-06-05 15:50:50 -05002476
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002477 if (png_ptr->mode & PNG_HAVE_IDAT)
2478 png_ptr->mode |= PNG_AFTER_IDAT;
2479
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002480 buffer = png_read_buffer(png_ptr, length, 2/*silent*/);
2481
2482 if (buffer == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002483 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002484 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002485 png_chunk_benign_error(png_ptr, "out of memory");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002486 return;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002487 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002488
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002489 png_crc_read(png_ptr, buffer, length);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002490
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002491 if (png_crc_finish(png_ptr, 0))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002492 return;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002493
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002494 /* TODO: also check that the keyword contents match the spec! */
2495 for (keyword_length = 0;
2496 keyword_length < length && buffer[keyword_length] != 0;
2497 ++keyword_length)
2498 /* Empty loop to find end of name */ ;
Guy Schalnat0d580581995-07-20 02:43:20 -05002499
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002500 if (keyword_length > 79 || keyword_length < 1)
2501 errmsg = "bad keyword";
Guy Schalnat0d580581995-07-20 02:43:20 -05002502
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002503 /* zTXt must have some LZ data after the keyword, although it may expand to
2504 * zero bytes; we need a '\0' at the end of the keyword, the compression type
2505 * then the LZ data:
2506 */
2507 else if (keyword_length + 3 > length)
2508 errmsg = "truncated";
2509
2510 else if (buffer[keyword_length+1] != PNG_COMPRESSION_TYPE_BASE)
2511 errmsg = "unknown compression type";
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002512
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002513 else
Guy Schalnat0d580581995-07-20 02:43:20 -05002514 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002515 png_alloc_size_t uncompressed_length = PNG_SIZE_MAX;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002516
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002517 /* TODO: at present png_decompress_chunk imposes a single application
2518 * level memory limit, this should be split to different values for iCCP
2519 * and text chunks.
2520 */
2521 if (png_decompress_chunk(png_ptr, length, keyword_length+2,
2522 &uncompressed_length, 1/*terminate*/) == Z_STREAM_END)
2523 {
2524 png_text text;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002525
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002526 /* It worked; png_ptr->read_buffer now looks like a tEXt chunk except
2527 * for the extra compression type byte and the fact that it isn't
2528 * necessarily '\0' terminated.
2529 */
2530 buffer = png_ptr->read_buffer;
2531 buffer[uncompressed_length+(keyword_length+2)] = 0;
2532
2533 text.compression = PNG_TEXT_COMPRESSION_zTXt;
2534 text.key = (png_charp)buffer;
2535 text.text = (png_charp)(buffer + keyword_length+2);
2536 text.text_length = uncompressed_length;
2537 text.itxt_length = 0;
2538 text.lang = NULL;
2539 text.lang_key = NULL;
2540
2541 if (png_set_text_2(png_ptr, info_ptr, &text, 1))
2542 errmsg = "insufficient memory";
2543 }
2544
2545 else
2546 errmsg = png_ptr->zstream.msg;
John Bowlerb5d00512012-03-09 09:15:18 -06002547 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002548
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002549 if (errmsg != NULL)
2550 png_chunk_benign_error(png_ptr, errmsg);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002551}
2552#endif
2553
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002554#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002555/* Note: this does not correctly handle chunks that are > 64K under DOS */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002556void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002557png_handle_iTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002558{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002559 png_const_charp errmsg = NULL;
2560 png_bytep buffer;
2561 png_uint_32 prefix_length;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002562
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002563 png_debug(1, "in png_handle_iTXt");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002564
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06002565#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002566 if (png_ptr->user_chunk_cache_max != 0)
2567 {
2568 if (png_ptr->user_chunk_cache_max == 1)
2569 {
2570 png_crc_finish(png_ptr, length);
2571 return;
2572 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002573
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002574 if (--png_ptr->user_chunk_cache_max == 1)
2575 {
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002576 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002577 png_chunk_benign_error(png_ptr, "no space in chunk cache");
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002578 return;
2579 }
2580 }
2581#endif
2582
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002583 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002584 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002585
2586 if (png_ptr->mode & PNG_HAVE_IDAT)
2587 png_ptr->mode |= PNG_AFTER_IDAT;
2588
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002589 buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
2590
2591 if (buffer == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002592 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002593 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002594 png_chunk_benign_error(png_ptr, "out of memory");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002595 return;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002596 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002597
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002598 png_crc_read(png_ptr, buffer, length);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002599
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002600 if (png_crc_finish(png_ptr, 0))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002601 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002602
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002603 /* First the keyword. */
2604 for (prefix_length=0;
2605 prefix_length < length && buffer[prefix_length] != 0;
2606 ++prefix_length)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002607 /* Empty loop */ ;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002608
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002609 /* Perform a basic check on the keyword length here. */
2610 if (prefix_length > 79 || prefix_length < 1)
2611 errmsg = "bad keyword";
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002612
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002613 /* Expect keyword, compression flag, compression type, language, translated
2614 * keyword (both may be empty but are 0 terminated) then the text, which may
2615 * be empty.
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002616 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002617 else if (prefix_length + 5 > length)
2618 errmsg = "truncated";
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002619
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002620 else if (buffer[prefix_length+1] == 0 ||
2621 (buffer[prefix_length+1] == 1 &&
2622 buffer[prefix_length+2] == PNG_COMPRESSION_TYPE_BASE))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002623 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002624 int compressed = buffer[prefix_length+1] != 0;
2625 png_uint_32 language_offset, translated_keyword_offset;
2626 png_alloc_size_t uncompressed_length = 0;
2627
2628 /* Now the language tag */
2629 prefix_length += 3;
2630 language_offset = prefix_length;
2631
2632 for (; prefix_length < length && buffer[prefix_length] != 0;
2633 ++prefix_length)
2634 /* Empty loop */ ;
2635
2636 /* WARNING: the length may be invalid here, this is checked below. */
2637 translated_keyword_offset = ++prefix_length;
2638
2639 for (; prefix_length < length && buffer[prefix_length] != 0;
2640 ++prefix_length)
2641 /* Empty loop */ ;
2642
2643 /* prefix_length should now be at the trailing '\0' of the translated
2644 * keyword, but it may already be over the end. None of this arithmetic
2645 * can overflow because chunks are at most 2^31 bytes long, but on 16-bit
2646 * systems the available allocaton may overflow.
2647 */
2648 ++prefix_length;
2649
2650 if (!compressed && prefix_length <= length)
2651 uncompressed_length = length - prefix_length;
2652
2653 else if (compressed && prefix_length < length)
2654 {
2655 uncompressed_length = PNG_SIZE_MAX;
2656
2657 /* TODO: at present png_decompress_chunk imposes a single application
2658 * level memory limit, this should be split to different values for
2659 * iCCP and text chunks.
2660 */
2661 if (png_decompress_chunk(png_ptr, length, prefix_length,
2662 &uncompressed_length, 1/*terminate*/) == Z_STREAM_END)
2663 buffer = png_ptr->read_buffer;
2664
2665 else
2666 errmsg = png_ptr->zstream.msg;
2667 }
2668
2669 else
2670 errmsg = "truncated";
2671
2672 if (errmsg == NULL)
2673 {
2674 png_text text;
2675
2676 buffer[uncompressed_length+prefix_length] = 0;
2677
2678 if (compressed)
2679 text.compression = PNG_ITXT_COMPRESSION_NONE;
2680
2681 else
2682 text.compression = PNG_ITXT_COMPRESSION_zTXt;
2683
2684 text.key = (png_charp)buffer;
2685 text.lang = (png_charp)buffer + language_offset;
2686 text.lang_key = (png_charp)buffer + translated_keyword_offset;
2687 text.text = (png_charp)buffer + prefix_length;
2688 text.text_length = 0;
2689 text.itxt_length = uncompressed_length;
2690
2691 if (png_set_text_2(png_ptr, info_ptr, &text, 1))
2692 errmsg = "insufficient memory";
2693 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002694 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002695
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002696 else
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002697 errmsg = "bad compression info";
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002698
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002699 if (errmsg != NULL)
2700 png_chunk_benign_error(png_ptr, errmsg);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002701}
2702#endif
2703
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06002704#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002705/* Utility function for png_handle_unknown; set up png_ptr::unknown_chunk */
2706static int
2707png_cache_unknown_chunk(png_structrp png_ptr, png_uint_32 length)
2708{
2709 png_alloc_size_t limit = PNG_SIZE_MAX;
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06002710
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002711 if (png_ptr->unknown_chunk.data != NULL)
2712 {
2713 png_free(png_ptr, png_ptr->unknown_chunk.data);
2714 png_ptr->unknown_chunk.data = NULL;
2715 }
2716
2717# ifdef PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED
2718 if (png_ptr->user_chunk_malloc_max > 0 &&
2719 png_ptr->user_chunk_malloc_max < limit)
2720 limit = png_ptr->user_chunk_malloc_max;
2721
2722# elif PNG_USER_CHUNK_MALLOC_MAX > 0
2723 if (PNG_USER_CHUNK_MALLOC_MAX < limit)
2724 limit = PNG_USER_CHUNK_MALLOC_MAX;
2725# endif
2726
2727 if (length <= limit)
2728 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002729 PNG_CSTRING_FROM_CHUNK(png_ptr->unknown_chunk.name, png_ptr->chunk_name);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002730 /* The following is safe because of the PNG_SIZE_MAX init above */
2731 png_ptr->unknown_chunk.size = (png_size_t)length/*SAFE*/;
2732 /* 'mode' is a flag array, only the bottom four bits matter here */
2733 png_ptr->unknown_chunk.location = (png_byte)png_ptr->mode/*SAFE*/;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002734
2735 if (length == 0)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002736 png_ptr->unknown_chunk.data = NULL;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002737
2738 else
2739 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002740 /* Do a 'warn' here - it is handled below. */
2741 png_ptr->unknown_chunk.data = png_voidcast(png_bytep,
2742 png_malloc_warn(png_ptr, length));
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06002743 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002744 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002745
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002746 if (png_ptr->unknown_chunk.data == NULL && length > 0)
2747 {
2748 /* This is benign because we clean up correctly */
2749 png_crc_finish(png_ptr, length);
2750 png_chunk_benign_error(png_ptr, "unknown chunk exceeds memory limits");
2751 return 0;
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06002752 }
John Bowlere9567512012-08-15 22:53:00 -05002753
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06002754 else
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002755 {
2756 if (length > 0)
2757 png_crc_read(png_ptr, png_ptr->unknown_chunk.data, length);
2758 png_crc_finish(png_ptr, 0);
2759 return 1;
2760 }
2761}
2762#endif /* PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */
John Bowlere9567512012-08-15 22:53:00 -05002763
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002764/* Handle an unknown, or known but disabled, chunk */
2765void /* PRIVATE */
2766png_handle_unknown(png_structrp png_ptr, png_inforp info_ptr,
2767 png_uint_32 length, int keep)
2768{
2769 int handled = 0; /* the chunk was handled */
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06002770
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002771 png_debug(1, "in png_handle_unknown");
2772
Glenn Randers-Pehrsonb3721752013-09-30 13:56:44 -05002773#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002774 /* NOTE: this code is based on the code in libpng-1.4.12 except for fixing
2775 * the bug which meant that setting a non-default behavior for a specific
2776 * chunk would be ignored (the default was always used unless a user
2777 * callback was installed).
2778 *
2779 * 'keep' is the value from the png_chunk_unknown_handling, the setting for
2780 * this specific chunk_name, if PNG_HANDLE_AS_UNKNOWN_SUPPORTED, if not it
2781 * will always be PNG_HANDLE_CHUNK_AS_DEFAULT and it needs to be set here.
2782 * This is just an optimization to avoid multiple calls to the lookup
2783 * function.
2784 */
Glenn Randers-Pehrson873f16f2013-09-20 14:28:50 -05002785# ifndef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsonb3721752013-09-30 13:56:44 -05002786# ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
2787 keep = png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name);
2788# endif
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002789# endif
2790
2791 /* One of the following methods will read the chunk or skip it (at least one
2792 * of these is always defined because this is the only way to switch on
2793 * PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
2794 */
2795# ifdef PNG_READ_USER_CHUNKS_SUPPORTED
2796 /* The user callback takes precedence over the chunk keep value, but the
2797 * keep value is still required to validate a save of a critical chunk.
2798 */
2799 if (png_ptr->read_user_chunk_fn != NULL)
2800 {
2801 if (png_cache_unknown_chunk(png_ptr, length))
2802 {
2803 /* Callback to user unknown chunk handler */
2804 int ret = (*(png_ptr->read_user_chunk_fn))(png_ptr,
2805 &png_ptr->unknown_chunk);
2806
2807 /* ret is:
2808 * negative: An error occured, png_chunk_error will be called.
2809 * zero: The chunk was not handled, the chunk will be discarded
2810 * unless png_set_keep_unknown_chunks has been used to set
2811 * a 'keep' behavior for this particular chunk, in which
2812 * case that will be used. A critical chunk will cause an
2813 * error at this point unless it is to be saved.
2814 * positive: The chunk was handled, libpng will ignore/discard it.
2815 */
2816 if (ret < 0)
2817 png_chunk_error(png_ptr, "error in user chunk");
2818
2819 else if (ret == 0)
2820 {
2821 /* If the keep value is 'default' or 'never' override it, but
2822 * still error out on critical chunks unless the keep value is
2823 * 'always' While this is weird it is the behavior in 1.4.12.
2824 * A possible improvement would be to obey the value set for the
2825 * chunk, but this would be an API change that would probably
2826 * damage some applications.
2827 *
2828 * The png_app_warning below catches the case that matters, where
2829 * the application has not set specific save or ignore for this
2830 * chunk or global save or ignore.
2831 */
Glenn Randers-Pehrson4ea113b2013-03-02 16:03:45 -06002832 if (keep < PNG_HANDLE_CHUNK_IF_SAFE)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002833 {
2834# ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
2835 if (png_ptr->unknown_default < PNG_HANDLE_CHUNK_IF_SAFE)
Glenn Randers-Pehrson8c87dc82013-03-04 17:38:04 -06002836 {
2837 png_chunk_warning(png_ptr, "Saving unknown chunk:");
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002838 png_app_warning(png_ptr,
2839 "forcing save of an unhandled chunk;"
2840 " please call png_set_keep_unknown_chunks");
Glenn Randers-Pehrson8c87dc82013-03-04 17:38:04 -06002841 /* with keep = PNG_HANDLE_CHUNK_IF_SAFE */
2842 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002843# endif
2844 keep = PNG_HANDLE_CHUNK_IF_SAFE;
2845 }
2846 }
2847
2848 else /* chunk was handled */
2849 {
2850 handled = 1;
2851 /* Critical chunks can be safely discarded at this point. */
2852 keep = PNG_HANDLE_CHUNK_NEVER;
2853 }
2854 }
2855
2856 else
2857 keep = PNG_HANDLE_CHUNK_NEVER; /* insufficient memory */
2858 }
2859
2860 else
2861 /* Use the SAVE_UNKNOWN_CHUNKS code or skip the chunk */
2862# endif /* PNG_READ_USER_CHUNKS_SUPPORTED */
2863
2864# ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
2865 {
2866 /* keep is currently just the per-chunk setting, if there was no
2867 * setting change it to the global default now (not that this may
2868 * still be AS_DEFAULT) then obtain the cache of the chunk if required,
2869 * if not simply skip the chunk.
2870 */
2871 if (keep == PNG_HANDLE_CHUNK_AS_DEFAULT)
2872 keep = png_ptr->unknown_default;
2873
2874 if (keep == PNG_HANDLE_CHUNK_ALWAYS ||
2875 (keep == PNG_HANDLE_CHUNK_IF_SAFE &&
Glenn Randers-Pehrson67a289f2013-04-19 19:03:34 -05002876 PNG_CHUNK_ANCILLARY(png_ptr->chunk_name)))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002877 {
2878 if (!png_cache_unknown_chunk(png_ptr, length))
2879 keep = PNG_HANDLE_CHUNK_NEVER;
2880 }
2881
2882 else
2883 png_crc_finish(png_ptr, length);
2884 }
2885# else
2886# ifndef PNG_READ_USER_CHUNKS_SUPPORTED
2887# error no method to support READ_UNKNOWN_CHUNKS
2888# endif
2889
2890 {
2891 /* If here there is no read callback pointer set and no support is
2892 * compiled in to just save the unknown chunks, so simply skip this
2893 * chunk. If 'keep' is something other than AS_DEFAULT or NEVER then
2894 * the app has erroneously asked for unknown chunk saving when there
2895 * is no support.
2896 */
2897 if (keep > PNG_HANDLE_CHUNK_NEVER)
2898 png_app_error(png_ptr, "no unknown chunk support available");
2899
2900 png_crc_finish(png_ptr, length);
2901 }
Glenn Randers-Pehrsonb3721752013-09-30 13:56:44 -05002902# endif
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002903
2904# ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
2905 /* Now store the chunk in the chunk list if appropriate, and if the limits
2906 * permit it.
2907 */
2908 if (keep == PNG_HANDLE_CHUNK_ALWAYS ||
2909 (keep == PNG_HANDLE_CHUNK_IF_SAFE &&
Glenn Randers-Pehrson67a289f2013-04-19 19:03:34 -05002910 PNG_CHUNK_ANCILLARY(png_ptr->chunk_name)))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002911 {
2912# ifdef PNG_USER_LIMITS_SUPPORTED
2913 switch (png_ptr->user_chunk_cache_max)
2914 {
2915 case 2:
2916 png_ptr->user_chunk_cache_max = 1;
2917 png_chunk_benign_error(png_ptr, "no space in chunk cache");
2918 /* FALL THROUGH */
2919 case 1:
2920 /* NOTE: prior to 1.6.0 this case resulted in an unknown critical
2921 * chunk being skipped, now there will be a hard error below.
2922 */
2923 break;
2924
2925 default: /* not at limit */
2926 --(png_ptr->user_chunk_cache_max);
2927 /* FALL THROUGH */
2928 case 0: /* no limit */
2929# endif /* PNG_USER_LIMITS_SUPPORTED */
2930 /* Here when the limit isn't reached or when limits are compiled
2931 * out; store the chunk.
2932 */
2933 png_set_unknown_chunks(png_ptr, info_ptr,
2934 &png_ptr->unknown_chunk, 1);
2935 handled = 1;
2936# ifdef PNG_USER_LIMITS_SUPPORTED
2937 break;
2938 }
2939# endif
2940 }
Glenn Randers-Pehrsonb3721752013-09-30 13:56:44 -05002941# else /* no store support: the chunk must be handled by the user callback */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002942 PNG_UNUSED(info_ptr)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002943# endif
2944
2945 /* Regardless of the error handling below the cached data (if any) can be
2946 * freed now. Notice that the data is not freed if there is a png_error, but
2947 * it will be freed by destroy_read_struct.
2948 */
2949 if (png_ptr->unknown_chunk.data != NULL)
2950 png_free(png_ptr, png_ptr->unknown_chunk.data);
2951 png_ptr->unknown_chunk.data = NULL;
2952
2953#else /* !PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */
2954 /* There is no support to read an unknown chunk, so just skip it. */
2955 png_crc_finish(png_ptr, length);
2956 PNG_UNUSED(info_ptr)
2957 PNG_UNUSED(keep)
2958#endif /* !PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */
2959
2960 /* Check for unhandled critical chunks */
2961 if (!handled && PNG_CHUNK_CRITICAL(png_ptr->chunk_name))
2962 png_chunk_error(png_ptr, "unhandled critical chunk");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002963}
2964
2965/* This function is called to verify that a chunk name is valid.
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002966 * This function can't have the "critical chunk check" incorporated
2967 * into it, since in the future we will need to be able to call user
2968 * functions to handle unknown critical chunks after we check that
2969 * the chunk name itself is valid.
2970 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002971
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002972/* Bit hacking: the test for an invalid byte in the 4 byte chunk name is:
2973 *
2974 * ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
2975 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002976
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002977void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002978png_check_chunk_name(png_structrp png_ptr, png_uint_32 chunk_name)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002979{
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002980 int i;
2981
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002982 png_debug(1, "in png_check_chunk_name");
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002983
2984 for (i=1; i<=4; ++i)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002985 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002986 int c = chunk_name & 0xff;
2987
2988 if (c < 65 || c > 122 || (c > 90 && c < 97))
2989 png_chunk_error(png_ptr, "invalid chunk type");
2990
2991 chunk_name >>= 8;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002992 }
2993}
2994
John Bowler7875d532011-11-07 22:33:49 -06002995/* Combines the row recently read in with the existing pixels in the row. This
2996 * routine takes care of alpha and transparency if requested. This routine also
2997 * handles the two methods of progressive display of interlaced images,
2998 * depending on the 'display' value; if 'display' is true then the whole row
2999 * (dp) is filled from the start by replicating the available pixels. If
3000 * 'display' is false only those pixels present in the pass are filled in.
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003001 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05003002void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06003003png_combine_row(png_const_structrp png_ptr, png_bytep dp, int display)
Guy Schalnat0d580581995-07-20 02:43:20 -05003004{
John Bowler4e68aa72011-10-11 16:01:33 -05003005 unsigned int pixel_depth = png_ptr->transformed_pixel_depth;
3006 png_const_bytep sp = png_ptr->row_buf + 1;
John Bowlerac8375d2011-10-06 22:27:16 -05003007 png_uint_32 row_width = png_ptr->width;
John Bowler4e68aa72011-10-11 16:01:33 -05003008 unsigned int pass = png_ptr->pass;
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003009 png_bytep end_ptr = 0;
3010 png_byte end_byte = 0;
3011 unsigned int end_mask;
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003012
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05003013 png_debug(1, "in png_combine_row");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003014
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003015 /* Added in 1.5.6: it should not be possible to enter this routine until at
3016 * least one row has been read from the PNG data and transformed.
3017 */
3018 if (pixel_depth == 0)
3019 png_error(png_ptr, "internal row logic error");
3020
3021 /* Added in 1.5.4: the pixel depth should match the information returned by
3022 * any call to png_read_update_info at this point. Do not continue if we got
John Bowler9994f252011-05-15 18:52:39 -05003023 * this wrong.
3024 */
3025 if (png_ptr->info_rowbytes != 0 && png_ptr->info_rowbytes !=
John Bowlerac8375d2011-10-06 22:27:16 -05003026 PNG_ROWBYTES(pixel_depth, row_width))
John Bowler9994f252011-05-15 18:52:39 -05003027 png_error(png_ptr, "internal row size calculation error");
3028
John Bowlerac8375d2011-10-06 22:27:16 -05003029 /* Don't expect this to ever happen: */
3030 if (row_width == 0)
3031 png_error(png_ptr, "internal row width error");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003032
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003033 /* Preserve the last byte in cases where only part of it will be overwritten,
3034 * the multiply below may overflow, we don't care because ANSI-C guarantees
3035 * we get the low bits.
3036 */
3037 end_mask = (pixel_depth * row_width) & 7;
3038 if (end_mask != 0)
3039 {
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003040 /* end_ptr == NULL is a flag to say do nothing */
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003041 end_ptr = dp + PNG_ROWBYTES(pixel_depth, row_width) - 1;
3042 end_byte = *end_ptr;
3043# ifdef PNG_READ_PACKSWAP_SUPPORTED
3044 if (png_ptr->transformations & PNG_PACKSWAP) /* little-endian byte */
3045 end_mask = 0xff << end_mask;
3046
3047 else /* big-endian byte */
3048# endif
3049 end_mask = 0xff >> end_mask;
3050 /* end_mask is now the bits to *keep* from the destination row */
3051 }
3052
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003053 /* For non-interlaced images this reduces to a memcpy(). A memcpy()
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003054 * will also happen if interlacing isn't supported or if the application
3055 * does not call png_set_interlace_handling(). In the latter cases the
3056 * caller just gets a sequence of the unexpanded rows from each interlace
3057 * pass.
John Bowlerac8375d2011-10-06 22:27:16 -05003058 */
3059#ifdef PNG_READ_INTERLACING_SUPPORTED
3060 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE) &&
John Bowler4e68aa72011-10-11 16:01:33 -05003061 pass < 6 && (display == 0 ||
3062 /* The following copies everything for 'display' on passes 0, 2 and 4. */
3063 (display == 1 && (pass & 1) != 0)))
Guy Schalnat0d580581995-07-20 02:43:20 -05003064 {
John Bowler4e68aa72011-10-11 16:01:33 -05003065 /* Narrow images may have no bits in a pass; the caller should handle
3066 * this, but this test is cheap:
John Bowlerac8375d2011-10-06 22:27:16 -05003067 */
John Bowler4e68aa72011-10-11 16:01:33 -05003068 if (row_width <= PNG_PASS_START_COL(pass))
3069 return;
John Bowlerac8375d2011-10-06 22:27:16 -05003070
John Bowler4e68aa72011-10-11 16:01:33 -05003071 if (pixel_depth < 8)
Guy Schalnat0d580581995-07-20 02:43:20 -05003072 {
John Bowler7875d532011-11-07 22:33:49 -06003073 /* For pixel depths up to 4 bpp the 8-pixel mask can be expanded to fit
John Bowler4e68aa72011-10-11 16:01:33 -05003074 * into 32 bits, then a single loop over the bytes using the four byte
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003075 * values in the 32-bit mask can be used. For the 'display' option the
John Bowler4e68aa72011-10-11 16:01:33 -05003076 * expanded mask may also not require any masking within a byte. To
3077 * make this work the PACKSWAP option must be taken into account - it
3078 * simply requires the pixels to be reversed in each byte.
3079 *
3080 * The 'regular' case requires a mask for each of the first 6 passes,
3081 * the 'display' case does a copy for the even passes in the range
John Bowler7875d532011-11-07 22:33:49 -06003082 * 0..6. This has already been handled in the test above.
John Bowler4e68aa72011-10-11 16:01:33 -05003083 *
3084 * The masks are arranged as four bytes with the first byte to use in
3085 * the lowest bits (little-endian) regardless of the order (PACKSWAP or
3086 * not) of the pixels in each byte.
3087 *
3088 * NOTE: the whole of this logic depends on the caller of this function
3089 * only calling it on rows appropriate to the pass. This function only
John Bowler7875d532011-11-07 22:33:49 -06003090 * understands the 'x' logic; the 'y' logic is handled by the caller.
John Bowler4e68aa72011-10-11 16:01:33 -05003091 *
3092 * The following defines allow generation of compile time constant bit
3093 * masks for each pixel depth and each possibility of swapped or not
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003094 * swapped bytes. Pass 'p' is in the range 0..6; 'x', a pixel index,
3095 * is in the range 0..7; and the result is 1 if the pixel is to be
3096 * copied in the pass, 0 if not. 'S' is for the sparkle method, 'B'
3097 * for the block method.
3098 *
John Bowler92ef3132011-10-27 19:36:08 -05003099 * With some compilers a compile time expression of the general form:
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05003100 *
John Bowler92ef3132011-10-27 19:36:08 -05003101 * (shift >= 32) ? (a >> (shift-32)) : (b >> shift)
3102 *
3103 * Produces warnings with values of 'shift' in the range 33 to 63
Glenn Randers-Pehrson18763662011-10-27 22:09:22 -05003104 * because the right hand side of the ?: expression is evaluated by
John Bowler92ef3132011-10-27 19:36:08 -05003105 * the compiler even though it isn't used. Microsoft Visual C (various
3106 * versions) and the Intel C compiler are known to do this. To avoid
3107 * this the following macros are used in 1.5.6. This is a temporary
John Bowler7875d532011-11-07 22:33:49 -06003108 * solution to avoid destabilizing the code during the release process.
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05003109 */
John Bowler92ef3132011-10-27 19:36:08 -05003110# if PNG_USE_COMPILE_TIME_MASKS
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05003111# define PNG_LSR(x,s) ((x)>>((s) & 0x1f))
3112# define PNG_LSL(x,s) ((x)<<((s) & 0x1f))
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003113# else
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05003114# define PNG_LSR(x,s) ((x)>>(s))
3115# define PNG_LSL(x,s) ((x)<<(s))
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003116# endif
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05003117# define S_COPY(p,x) (((p)<4 ? PNG_LSR(0x80088822,(3-(p))*8+(7-(x))) :\
3118 PNG_LSR(0xaa55ff00,(7-(p))*8+(7-(x)))) & 1)
3119# define B_COPY(p,x) (((p)<4 ? PNG_LSR(0xff0fff33,(3-(p))*8+(7-(x))) :\
3120 PNG_LSR(0xff55ff00,(7-(p))*8+(7-(x)))) & 1)
Guy Schalnat0d580581995-07-20 02:43:20 -05003121
John Bowler4e68aa72011-10-11 16:01:33 -05003122 /* Return a mask for pass 'p' pixel 'x' at depth 'd'. The mask is
3123 * little endian - the first pixel is at bit 0 - however the extra
3124 * parameter 's' can be set to cause the mask position to be swapped
3125 * within each byte, to match the PNG format. This is done by XOR of
3126 * the shift with 7, 6 or 4 for bit depths 1, 2 and 4.
3127 */
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05003128# define PIXEL_MASK(p,x,d,s) \
3129 (PNG_LSL(((PNG_LSL(1U,(d)))-1),(((x)*(d))^((s)?8-(d):0))))
John Bowlerac8375d2011-10-06 22:27:16 -05003130
John Bowler4e68aa72011-10-11 16:01:33 -05003131 /* Hence generate the appropriate 'block' or 'sparkle' pixel copy mask.
3132 */
3133# define S_MASKx(p,x,d,s) (S_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
3134# define B_MASKx(p,x,d,s) (B_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
John Bowlerac8375d2011-10-06 22:27:16 -05003135
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003136 /* Combine 8 of these to get the full mask. For the 1-bpp and 2-bpp
3137 * cases the result needs replicating, for the 4-bpp case the above
3138 * generates a full 32 bits.
John Bowler4e68aa72011-10-11 16:01:33 -05003139 */
3140# define MASK_EXPAND(m,d) ((m)*((d)==1?0x01010101:((d)==2?0x00010001:1)))
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003141
John Bowler4e68aa72011-10-11 16:01:33 -05003142# define S_MASK(p,d,s) MASK_EXPAND(S_MASKx(p,0,d,s) + S_MASKx(p,1,d,s) +\
3143 S_MASKx(p,2,d,s) + S_MASKx(p,3,d,s) + S_MASKx(p,4,d,s) +\
3144 S_MASKx(p,5,d,s) + S_MASKx(p,6,d,s) + S_MASKx(p,7,d,s), d)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003145
John Bowler4e68aa72011-10-11 16:01:33 -05003146# define B_MASK(p,d,s) MASK_EXPAND(B_MASKx(p,0,d,s) + B_MASKx(p,1,d,s) +\
3147 B_MASKx(p,2,d,s) + B_MASKx(p,3,d,s) + B_MASKx(p,4,d,s) +\
3148 B_MASKx(p,5,d,s) + B_MASKx(p,6,d,s) + B_MASKx(p,7,d,s), d)
John Bowlerac8375d2011-10-06 22:27:16 -05003149
John Bowler4e68aa72011-10-11 16:01:33 -05003150#if PNG_USE_COMPILE_TIME_MASKS
3151 /* Utility macros to construct all the masks for a depth/swap
3152 * combination. The 's' parameter says whether the format is PNG
John Bowler7875d532011-11-07 22:33:49 -06003153 * (big endian bytes) or not. Only the three odd-numbered passes are
John Bowler4e68aa72011-10-11 16:01:33 -05003154 * required for the display/block algorithm.
3155 */
3156# define S_MASKS(d,s) { S_MASK(0,d,s), S_MASK(1,d,s), S_MASK(2,d,s),\
3157 S_MASK(3,d,s), S_MASK(4,d,s), S_MASK(5,d,s) }
John Bowlerac8375d2011-10-06 22:27:16 -05003158
John Bowler4e68aa72011-10-11 16:01:33 -05003159# define B_MASKS(d,s) { B_MASK(1,d,s), S_MASK(3,d,s), S_MASK(5,d,s) }
John Bowlerac8375d2011-10-06 22:27:16 -05003160
John Bowler4e68aa72011-10-11 16:01:33 -05003161# define DEPTH_INDEX(d) ((d)==1?0:((d)==2?1:2))
John Bowlerac8375d2011-10-06 22:27:16 -05003162
John Bowler4e68aa72011-10-11 16:01:33 -05003163 /* Hence the pre-compiled masks indexed by PACKSWAP (or not), depth and
3164 * then pass:
3165 */
John Bowler7875d532011-11-07 22:33:49 -06003166 static PNG_CONST png_uint_32 row_mask[2/*PACKSWAP*/][3/*depth*/][6] =
3167 {
John Bowler4e68aa72011-10-11 16:01:33 -05003168 /* Little-endian byte masks for PACKSWAP */
3169 { S_MASKS(1,0), S_MASKS(2,0), S_MASKS(4,0) },
3170 /* Normal (big-endian byte) masks - PNG format */
3171 { S_MASKS(1,1), S_MASKS(2,1), S_MASKS(4,1) }
3172 };
John Bowlerac8375d2011-10-06 22:27:16 -05003173
John Bowler4e68aa72011-10-11 16:01:33 -05003174 /* display_mask has only three entries for the odd passes, so index by
3175 * pass>>1.
3176 */
John Bowler7875d532011-11-07 22:33:49 -06003177 static PNG_CONST png_uint_32 display_mask[2][3][3] =
3178 {
John Bowler4e68aa72011-10-11 16:01:33 -05003179 /* Little-endian byte masks for PACKSWAP */
3180 { B_MASKS(1,0), B_MASKS(2,0), B_MASKS(4,0) },
3181 /* Normal (big-endian byte) masks - PNG format */
3182 { B_MASKS(1,1), B_MASKS(2,1), B_MASKS(4,1) }
3183 };
John Bowlerac8375d2011-10-06 22:27:16 -05003184
John Bowler4e68aa72011-10-11 16:01:33 -05003185# define MASK(pass,depth,display,png)\
3186 ((display)?display_mask[png][DEPTH_INDEX(depth)][pass>>1]:\
3187 row_mask[png][DEPTH_INDEX(depth)][pass])
John Bowlerac8375d2011-10-06 22:27:16 -05003188
John Bowler4e68aa72011-10-11 16:01:33 -05003189#else /* !PNG_USE_COMPILE_TIME_MASKS */
3190 /* This is the runtime alternative: it seems unlikely that this will
3191 * ever be either smaller or faster than the compile time approach.
3192 */
3193# define MASK(pass,depth,display,png)\
3194 ((display)?B_MASK(pass,depth,png):S_MASK(pass,depth,png))
3195#endif /* !PNG_USE_COMPILE_TIME_MASKS */
John Bowlerac8375d2011-10-06 22:27:16 -05003196
John Bowler4e68aa72011-10-11 16:01:33 -05003197 /* Use the appropriate mask to copy the required bits. In some cases
3198 * the byte mask will be 0 or 0xff, optimize these cases. row_width is
3199 * the number of pixels, but the code copies bytes, so it is necessary
3200 * to special case the end.
3201 */
3202 png_uint_32 pixels_per_byte = 8 / pixel_depth;
3203 png_uint_32 mask;
John Bowlerac8375d2011-10-06 22:27:16 -05003204
John Bowler4e68aa72011-10-11 16:01:33 -05003205# ifdef PNG_READ_PACKSWAP_SUPPORTED
3206 if (png_ptr->transformations & PNG_PACKSWAP)
3207 mask = MASK(pass, pixel_depth, display, 0);
John Bowlerac8375d2011-10-06 22:27:16 -05003208
3209 else
John Bowler4e68aa72011-10-11 16:01:33 -05003210# endif
3211 mask = MASK(pass, pixel_depth, display, 1);
3212
3213 for (;;)
3214 {
3215 png_uint_32 m;
3216
3217 /* It doesn't matter in the following if png_uint_32 has more than
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003218 * 32 bits because the high bits always match those in m<<24; it is,
John Bowler4e68aa72011-10-11 16:01:33 -05003219 * however, essential to use OR here, not +, because of this.
3220 */
3221 m = mask;
3222 mask = (m >> 8) | (m << 24); /* rotate right to good compilers */
3223 m &= 0xff;
3224
3225 if (m != 0) /* something to copy */
John Bowlerac8375d2011-10-06 22:27:16 -05003226 {
John Bowler4e68aa72011-10-11 16:01:33 -05003227 if (m != 0xff)
3228 *dp = (png_byte)((*dp & ~m) | (*sp & m));
3229 else
3230 *dp = *sp;
John Bowlerac8375d2011-10-06 22:27:16 -05003231 }
John Bowler4e68aa72011-10-11 16:01:33 -05003232
3233 /* NOTE: this may overwrite the last byte with garbage if the image
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003234 * is not an exact number of bytes wide; libpng has always done
John Bowler4e68aa72011-10-11 16:01:33 -05003235 * this.
3236 */
3237 if (row_width <= pixels_per_byte)
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003238 break; /* May need to restore part of the last byte */
John Bowler4e68aa72011-10-11 16:01:33 -05003239
3240 row_width -= pixels_per_byte;
3241 ++dp;
3242 ++sp;
3243 }
3244 }
3245
3246 else /* pixel_depth >= 8 */
3247 {
3248 unsigned int bytes_to_copy, bytes_to_jump;
3249
3250 /* Validate the depth - it must be a multiple of 8 */
3251 if (pixel_depth & 7)
3252 png_error(png_ptr, "invalid user transform pixel depth");
3253
3254 pixel_depth >>= 3; /* now in bytes */
3255 row_width *= pixel_depth;
3256
3257 /* Regardless of pass number the Adam 7 interlace always results in a
3258 * fixed number of pixels to copy then to skip. There may be a
3259 * different number of pixels to skip at the start though.
3260 */
3261 {
3262 unsigned int offset = PNG_PASS_START_COL(pass) * pixel_depth;
3263
3264 row_width -= offset;
3265 dp += offset;
3266 sp += offset;
John Bowlerac8375d2011-10-06 22:27:16 -05003267 }
3268
John Bowler4e68aa72011-10-11 16:01:33 -05003269 /* Work out the bytes to copy. */
3270 if (display)
3271 {
3272 /* When doing the 'block' algorithm the pixel in the pass gets
3273 * replicated to adjacent pixels. This is why the even (0,2,4,6)
3274 * passes are skipped above - the entire expanded row is copied.
3275 */
3276 bytes_to_copy = (1<<((6-pass)>>1)) * pixel_depth;
3277
3278 /* But don't allow this number to exceed the actual row width. */
3279 if (bytes_to_copy > row_width)
3280 bytes_to_copy = row_width;
3281 }
3282
3283 else /* normal row; Adam7 only ever gives us one pixel to copy. */
3284 bytes_to_copy = pixel_depth;
3285
3286 /* In Adam7 there is a constant offset between where the pixels go. */
3287 bytes_to_jump = PNG_PASS_COL_OFFSET(pass) * pixel_depth;
3288
3289 /* And simply copy these bytes. Some optimization is possible here,
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003290 * depending on the value of 'bytes_to_copy'. Special case the low
John Bowler4e68aa72011-10-11 16:01:33 -05003291 * byte counts, which we know to be frequent.
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003292 *
3293 * Notice that these cases all 'return' rather than 'break' - this
3294 * avoids an unnecessary test on whether to restore the last byte
3295 * below.
John Bowler4e68aa72011-10-11 16:01:33 -05003296 */
3297 switch (bytes_to_copy)
3298 {
3299 case 1:
3300 for (;;)
3301 {
3302 *dp = *sp;
3303
3304 if (row_width <= bytes_to_jump)
3305 return;
3306
3307 dp += bytes_to_jump;
3308 sp += bytes_to_jump;
3309 row_width -= bytes_to_jump;
3310 }
3311
3312 case 2:
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003313 /* There is a possibility of a partial copy at the end here; this
John Bowler4e68aa72011-10-11 16:01:33 -05003314 * slows the code down somewhat.
3315 */
3316 do
3317 {
3318 dp[0] = sp[0], dp[1] = sp[1];
3319
3320 if (row_width <= bytes_to_jump)
3321 return;
3322
3323 sp += bytes_to_jump;
3324 dp += bytes_to_jump;
3325 row_width -= bytes_to_jump;
3326 }
3327 while (row_width > 1);
3328
3329 /* And there can only be one byte left at this point: */
3330 *dp = *sp;
3331 return;
3332
3333 case 3:
3334 /* This can only be the RGB case, so each copy is exactly one
3335 * pixel and it is not necessary to check for a partial copy.
3336 */
3337 for(;;)
3338 {
3339 dp[0] = sp[0], dp[1] = sp[1], dp[2] = sp[2];
3340
3341 if (row_width <= bytes_to_jump)
3342 return;
3343
3344 sp += bytes_to_jump;
3345 dp += bytes_to_jump;
3346 row_width -= bytes_to_jump;
3347 }
3348
3349 default:
3350#if PNG_ALIGN_TYPE != PNG_ALIGN_NONE
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003351 /* Check for double byte alignment and, if possible, use a
3352 * 16-bit copy. Don't attempt this for narrow images - ones that
John Bowler4e68aa72011-10-11 16:01:33 -05003353 * are less than an interlace panel wide. Don't attempt it for
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003354 * wide bytes_to_copy either - use the memcpy there.
John Bowler4e68aa72011-10-11 16:01:33 -05003355 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003356 if (bytes_to_copy < 16 /*else use memcpy*/ &&
John Bowler4e68aa72011-10-11 16:01:33 -05003357 png_isaligned(dp, png_uint_16) &&
3358 png_isaligned(sp, png_uint_16) &&
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003359 bytes_to_copy % (sizeof (png_uint_16)) == 0 &&
3360 bytes_to_jump % (sizeof (png_uint_16)) == 0)
John Bowler4e68aa72011-10-11 16:01:33 -05003361 {
3362 /* Everything is aligned for png_uint_16 copies, but try for
3363 * png_uint_32 first.
3364 */
3365 if (png_isaligned(dp, png_uint_32) &&
3366 png_isaligned(sp, png_uint_32) &&
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003367 bytes_to_copy % (sizeof (png_uint_32)) == 0 &&
3368 bytes_to_jump % (sizeof (png_uint_32)) == 0)
John Bowler4e68aa72011-10-11 16:01:33 -05003369 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003370 png_uint_32p dp32 = png_aligncast(png_uint_32p,dp);
3371 png_const_uint_32p sp32 = png_aligncastconst(
3372 png_const_uint_32p, sp);
John Bowler2286a7c2013-03-10 21:35:35 -05003373 size_t skip = (bytes_to_jump-bytes_to_copy) /
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003374 (sizeof (png_uint_32));
John Bowler4e68aa72011-10-11 16:01:33 -05003375
3376 do
3377 {
3378 size_t c = bytes_to_copy;
3379 do
3380 {
3381 *dp32++ = *sp32++;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003382 c -= (sizeof (png_uint_32));
John Bowler4e68aa72011-10-11 16:01:33 -05003383 }
3384 while (c > 0);
3385
3386 if (row_width <= bytes_to_jump)
3387 return;
3388
3389 dp32 += skip;
3390 sp32 += skip;
3391 row_width -= bytes_to_jump;
3392 }
3393 while (bytes_to_copy <= row_width);
3394
3395 /* Get to here when the row_width truncates the final copy.
3396 * There will be 1-3 bytes left to copy, so don't try the
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003397 * 16-bit loop below.
John Bowler4e68aa72011-10-11 16:01:33 -05003398 */
3399 dp = (png_bytep)dp32;
3400 sp = (png_const_bytep)sp32;
3401 do
3402 *dp++ = *sp++;
3403 while (--row_width > 0);
3404 return;
3405 }
3406
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003407 /* Else do it in 16-bit quantities, but only if the size is
John Bowler4e68aa72011-10-11 16:01:33 -05003408 * not too large.
3409 */
3410 else
3411 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003412 png_uint_16p dp16 = png_aligncast(png_uint_16p, dp);
3413 png_const_uint_16p sp16 = png_aligncastconst(
3414 png_const_uint_16p, sp);
John Bowlercaa3f292013-03-10 21:40:27 -05003415 size_t skip = (bytes_to_jump-bytes_to_copy) /
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003416 (sizeof (png_uint_16));
John Bowler4e68aa72011-10-11 16:01:33 -05003417
3418 do
3419 {
3420 size_t c = bytes_to_copy;
3421 do
3422 {
3423 *dp16++ = *sp16++;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003424 c -= (sizeof (png_uint_16));
John Bowler4e68aa72011-10-11 16:01:33 -05003425 }
3426 while (c > 0);
3427
3428 if (row_width <= bytes_to_jump)
3429 return;
3430
3431 dp16 += skip;
3432 sp16 += skip;
3433 row_width -= bytes_to_jump;
3434 }
3435 while (bytes_to_copy <= row_width);
3436
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003437 /* End of row - 1 byte left, bytes_to_copy > row_width: */
John Bowler4e68aa72011-10-11 16:01:33 -05003438 dp = (png_bytep)dp16;
3439 sp = (png_const_bytep)sp16;
3440 do
3441 *dp++ = *sp++;
3442 while (--row_width > 0);
3443 return;
3444 }
3445 }
3446#endif /* PNG_ALIGN_ code */
3447
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003448 /* The true default - use a memcpy: */
John Bowler4e68aa72011-10-11 16:01:33 -05003449 for (;;)
3450 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003451 memcpy(dp, sp, bytes_to_copy);
John Bowler4e68aa72011-10-11 16:01:33 -05003452
3453 if (row_width <= bytes_to_jump)
3454 return;
3455
3456 sp += bytes_to_jump;
3457 dp += bytes_to_jump;
3458 row_width -= bytes_to_jump;
3459 if (bytes_to_copy > row_width)
3460 bytes_to_copy = row_width;
3461 }
3462 }
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003463
3464 /* NOT REACHED*/
John Bowler4e68aa72011-10-11 16:01:33 -05003465 } /* pixel_depth >= 8 */
3466
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003467 /* Here if pixel_depth < 8 to check 'end_ptr' below. */
Guy Schalnat0d580581995-07-20 02:43:20 -05003468 }
John Bowler4e68aa72011-10-11 16:01:33 -05003469 else
John Bowlerac8375d2011-10-06 22:27:16 -05003470#endif
3471
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003472 /* If here then the switch above wasn't used so just memcpy the whole row
John Bowler4e68aa72011-10-11 16:01:33 -05003473 * from the temporary row buffer (notice that this overwrites the end of the
3474 * destination row if it is a partial byte.)
John Bowlerac8375d2011-10-06 22:27:16 -05003475 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003476 memcpy(dp, sp, PNG_ROWBYTES(pixel_depth, row_width));
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003477
3478 /* Restore the overwritten bits from the last byte if necessary. */
3479 if (end_ptr != NULL)
3480 *end_ptr = (png_byte)((end_byte & end_mask) | (*end_ptr & ~end_mask));
Guy Schalnat0d580581995-07-20 02:43:20 -05003481}
3482
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06003483#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05003484void /* PRIVATE */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003485png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
3486 png_uint_32 transformations /* Because these may affect the byte layout */)
Guy Schalnat0d580581995-07-20 02:43:20 -05003487{
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003488 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
3489 /* Offset to next interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003490 static PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003491
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05003492 png_debug(1, "in png_do_read_interlace");
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003493 if (row != NULL && row_info != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -05003494 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003495 png_uint_32 final_width;
3496
3497 final_width = row_info->width * png_pass_inc[pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05003498
3499 switch (row_info->pixel_depth)
3500 {
3501 case 1:
3502 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003503 png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3);
3504 png_bytep dp = row + (png_size_t)((final_width - 1) >> 3);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003505 int sshift, dshift;
3506 int s_start, s_end, s_inc;
3507 int jstop = png_pass_inc[pass];
3508 png_byte v;
Guy Schalnat0d580581995-07-20 02:43:20 -05003509 png_uint_32 i;
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003510 int j;
Guy Schalnat0d580581995-07-20 02:43:20 -05003511
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003512#ifdef PNG_READ_PACKSWAP_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003513 if (transformations & PNG_PACKSWAP)
3514 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003515 sshift = (int)((row_info->width + 7) & 0x07);
3516 dshift = (int)((final_width + 7) & 0x07);
3517 s_start = 7;
3518 s_end = 0;
3519 s_inc = -1;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003520 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003521
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003522 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05003523#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003524 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003525 sshift = 7 - (int)((row_info->width + 7) & 0x07);
3526 dshift = 7 - (int)((final_width + 7) & 0x07);
3527 s_start = 0;
3528 s_end = 7;
3529 s_inc = 1;
3530 }
Glenn Randers-Pehrson5dd2b8e2004-11-24 07:50:16 -06003531
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003532 for (i = 0; i < row_info->width; i++)
3533 {
3534 v = (png_byte)((*sp >> sshift) & 0x01);
3535 for (j = 0; j < jstop; j++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003536 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003537 unsigned int tmp = *dp & (0x7f7f >> (7 - dshift));
3538 tmp |= v << dshift;
3539 *dp = (png_byte)(tmp & 0xff);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003540
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003541 if (dshift == s_end)
3542 {
3543 dshift = s_start;
3544 dp--;
3545 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003546
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003547 else
3548 dshift += s_inc;
3549 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003550
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003551 if (sshift == s_end)
3552 {
3553 sshift = s_start;
Guy Schalnat0d580581995-07-20 02:43:20 -05003554 sp--;
3555 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003556
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003557 else
3558 sshift += s_inc;
Guy Schalnat0d580581995-07-20 02:43:20 -05003559 }
3560 break;
3561 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003562
Guy Schalnat0d580581995-07-20 02:43:20 -05003563 case 2:
3564 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003565 png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);
3566 png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);
3567 int sshift, dshift;
3568 int s_start, s_end, s_inc;
3569 int jstop = png_pass_inc[pass];
Andreas Dilger47a0c421997-05-16 02:46:07 -05003570 png_uint_32 i;
Guy Schalnat0d580581995-07-20 02:43:20 -05003571
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003572#ifdef PNG_READ_PACKSWAP_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003573 if (transformations & PNG_PACKSWAP)
3574 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003575 sshift = (int)(((row_info->width + 3) & 0x03) << 1);
3576 dshift = (int)(((final_width + 3) & 0x03) << 1);
3577 s_start = 6;
3578 s_end = 0;
3579 s_inc = -2;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003580 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003581
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003582 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05003583#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003584 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003585 sshift = (int)((3 - ((row_info->width + 3) & 0x03)) << 1);
3586 dshift = (int)((3 - ((final_width + 3) & 0x03)) << 1);
3587 s_start = 0;
3588 s_end = 6;
3589 s_inc = 2;
3590 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05003591
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003592 for (i = 0; i < row_info->width; i++)
3593 {
3594 png_byte v;
3595 int j;
3596
3597 v = (png_byte)((*sp >> sshift) & 0x03);
3598 for (j = 0; j < jstop; j++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003599 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003600 unsigned int tmp = *dp & (0x3f3f >> (6 - dshift));
3601 tmp |= v << dshift;
3602 *dp = (png_byte)(tmp & 0xff);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003603
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003604 if (dshift == s_end)
3605 {
3606 dshift = s_start;
3607 dp--;
3608 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003609
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003610 else
3611 dshift += s_inc;
3612 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003613
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003614 if (sshift == s_end)
3615 {
3616 sshift = s_start;
Guy Schalnat0d580581995-07-20 02:43:20 -05003617 sp--;
3618 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003619
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003620 else
3621 sshift += s_inc;
Guy Schalnat0d580581995-07-20 02:43:20 -05003622 }
3623 break;
3624 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003625
Guy Schalnat0d580581995-07-20 02:43:20 -05003626 case 4:
3627 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003628 png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1);
3629 png_bytep dp = row + (png_size_t)((final_width - 1) >> 1);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003630 int sshift, dshift;
3631 int s_start, s_end, s_inc;
Guy Schalnat0d580581995-07-20 02:43:20 -05003632 png_uint_32 i;
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003633 int jstop = png_pass_inc[pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05003634
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003635#ifdef PNG_READ_PACKSWAP_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003636 if (transformations & PNG_PACKSWAP)
3637 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003638 sshift = (int)(((row_info->width + 1) & 0x01) << 2);
3639 dshift = (int)(((final_width + 1) & 0x01) << 2);
3640 s_start = 4;
3641 s_end = 0;
3642 s_inc = -4;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003643 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003644
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003645 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05003646#endif
3647 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003648 sshift = (int)((1 - ((row_info->width + 1) & 0x01)) << 2);
3649 dshift = (int)((1 - ((final_width + 1) & 0x01)) << 2);
3650 s_start = 0;
3651 s_end = 4;
3652 s_inc = 4;
3653 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05003654
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003655 for (i = 0; i < row_info->width; i++)
3656 {
Glenn Randers-Pehrson8db19982011-10-27 16:17:24 -05003657 png_byte v = (png_byte)((*sp >> sshift) & 0x0f);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003658 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003659
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003660 for (j = 0; j < jstop; j++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003661 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003662 unsigned int tmp = *dp & (0xf0f >> (4 - dshift));
3663 tmp |= v << dshift;
3664 *dp = (png_byte)(tmp & 0xff);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003665
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003666 if (dshift == s_end)
3667 {
3668 dshift = s_start;
3669 dp--;
3670 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003671
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003672 else
3673 dshift += s_inc;
3674 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003675
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003676 if (sshift == s_end)
3677 {
3678 sshift = s_start;
Guy Schalnat0d580581995-07-20 02:43:20 -05003679 sp--;
3680 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003681
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003682 else
3683 sshift += s_inc;
Guy Schalnat0d580581995-07-20 02:43:20 -05003684 }
3685 break;
3686 }
John Bowlerac8375d2011-10-06 22:27:16 -05003687
Guy Schalnat0d580581995-07-20 02:43:20 -05003688 default:
3689 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003690 png_size_t pixel_bytes = (row_info->pixel_depth >> 3);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003691
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06003692 png_bytep sp = row + (png_size_t)(row_info->width - 1)
3693 * pixel_bytes;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003694
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003695 png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05003696
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003697 int jstop = png_pass_inc[pass];
3698 png_uint_32 i;
3699
3700 for (i = 0; i < row_info->width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003701 {
Glenn Randers-Pehrsondb56fa12013-07-15 10:09:54 -05003702 png_byte v[8]; /* SAFE; pixel_depth does not exceed 64 */
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003703 int j;
3704
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003705 memcpy(v, sp, pixel_bytes);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003706
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003707 for (j = 0; j < jstop; j++)
3708 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003709 memcpy(dp, v, pixel_bytes);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003710 dp -= pixel_bytes;
3711 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003712
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003713 sp -= pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05003714 }
3715 break;
3716 }
3717 }
John Bowlerac8375d2011-10-06 22:27:16 -05003718
Guy Schalnat0d580581995-07-20 02:43:20 -05003719 row_info->width = final_width;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003720 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width);
Guy Schalnat0d580581995-07-20 02:43:20 -05003721 }
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -05003722#ifndef PNG_READ_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003723 PNG_UNUSED(transformations) /* Silence compiler warning */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05003724#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003725}
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06003726#endif /* PNG_READ_INTERLACING_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05003727
Mans Rullgardd3a94802011-11-03 00:47:55 -05003728static void
3729png_read_filter_row_sub(png_row_infop row_info, png_bytep row,
3730 png_const_bytep prev_row)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003731{
Mans Rullgardd3a94802011-11-03 00:47:55 -05003732 png_size_t i;
3733 png_size_t istop = row_info->rowbytes;
3734 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
3735 png_bytep rp = row + bpp;
Mans Rullgardd3a94802011-11-03 00:47:55 -05003736
3737 PNG_UNUSED(prev_row)
3738
3739 for (i = bpp; i < istop; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003740 {
John Bowler7875d532011-11-07 22:33:49 -06003741 *rp = (png_byte)(((int)(*rp) + (int)(*(rp-bpp))) & 0xff);
Mans Rullgardd3a94802011-11-03 00:47:55 -05003742 rp++;
3743 }
3744}
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003745
Mans Rullgardd3a94802011-11-03 00:47:55 -05003746static void
3747png_read_filter_row_up(png_row_infop row_info, png_bytep row,
3748 png_const_bytep prev_row)
3749{
3750 png_size_t i;
3751 png_size_t istop = row_info->rowbytes;
3752 png_bytep rp = row;
3753 png_const_bytep pp = prev_row;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003754
Mans Rullgardd3a94802011-11-03 00:47:55 -05003755 for (i = 0; i < istop; i++)
3756 {
3757 *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
3758 rp++;
3759 }
3760}
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003761
Mans Rullgardd3a94802011-11-03 00:47:55 -05003762static void
3763png_read_filter_row_avg(png_row_infop row_info, png_bytep row,
3764 png_const_bytep prev_row)
3765{
3766 png_size_t i;
3767 png_bytep rp = row;
3768 png_const_bytep pp = prev_row;
Mans Rullgardd3a94802011-11-03 00:47:55 -05003769 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
3770 png_size_t istop = row_info->rowbytes - bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003771
Mans Rullgardd3a94802011-11-03 00:47:55 -05003772 for (i = 0; i < bpp; i++)
3773 {
3774 *rp = (png_byte)(((int)(*rp) +
3775 ((int)(*pp++) / 2 )) & 0xff);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003776
Mans Rullgardd3a94802011-11-03 00:47:55 -05003777 rp++;
3778 }
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003779
Mans Rullgardd3a94802011-11-03 00:47:55 -05003780 for (i = 0; i < istop; i++)
3781 {
3782 *rp = (png_byte)(((int)(*rp) +
John Bowler7875d532011-11-07 22:33:49 -06003783 (int)(*pp++ + *(rp-bpp)) / 2 ) & 0xff);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003784
Mans Rullgardd3a94802011-11-03 00:47:55 -05003785 rp++;
3786 }
3787}
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003788
Mans Rullgardd3a94802011-11-03 00:47:55 -05003789static void
John Bowlerfcc02632011-11-03 18:31:00 -05003790png_read_filter_row_paeth_1byte_pixel(png_row_infop row_info, png_bytep row,
Mans Rullgardd3a94802011-11-03 00:47:55 -05003791 png_const_bytep prev_row)
3792{
John Bowlerfcc02632011-11-03 18:31:00 -05003793 png_bytep rp_end = row + row_info->rowbytes;
3794 int a, c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003795
John Bowlerfcc02632011-11-03 18:31:00 -05003796 /* First pixel/byte */
3797 c = *prev_row++;
3798 a = *row + c;
3799 *row++ = (png_byte)a;
3800
3801 /* Remainder */
3802 while (row < rp_end)
Mans Rullgardd3a94802011-11-03 00:47:55 -05003803 {
John Bowlerfcc02632011-11-03 18:31:00 -05003804 int b, pa, pb, pc, p;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003805
John Bowlerfcc02632011-11-03 18:31:00 -05003806 a &= 0xff; /* From previous iteration or start */
3807 b = *prev_row++;
Mans Rullgardd3a94802011-11-03 00:47:55 -05003808
3809 p = b - c;
3810 pc = a - c;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003811
John Bowlerfcc02632011-11-03 18:31:00 -05003812# ifdef PNG_USE_ABS
3813 pa = abs(p);
3814 pb = abs(pc);
3815 pc = abs(p + pc);
3816# else
3817 pa = p < 0 ? -p : p;
3818 pb = pc < 0 ? -pc : pc;
3819 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
3820# endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003821
John Bowlerfcc02632011-11-03 18:31:00 -05003822 /* Find the best predictor, the least of pa, pb, pc favoring the earlier
3823 * ones in the case of a tie.
3824 */
3825 if (pb < pa) pa = pb, a = b;
3826 if (pc < pa) a = c;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003827
John Bowlerfcc02632011-11-03 18:31:00 -05003828 /* Calculate the current pixel in a, and move the previous row pixel to c
3829 * for the next time round the loop
3830 */
3831 c = b;
3832 a += *row;
3833 *row++ = (png_byte)a;
3834 }
3835}
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003836
John Bowlerfcc02632011-11-03 18:31:00 -05003837static void
3838png_read_filter_row_paeth_multibyte_pixel(png_row_infop row_info, png_bytep row,
3839 png_const_bytep prev_row)
3840{
3841 int bpp = (row_info->pixel_depth + 7) >> 3;
3842 png_bytep rp_end = row + bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003843
John Bowlerfcc02632011-11-03 18:31:00 -05003844 /* Process the first pixel in the row completely (this is the same as 'up'
3845 * because there is only one candidate predictor for the first row).
3846 */
3847 while (row < rp_end)
3848 {
3849 int a = *row + *prev_row++;
3850 *row++ = (png_byte)a;
3851 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003852
John Bowlerfcc02632011-11-03 18:31:00 -05003853 /* Remainder */
3854 rp_end += row_info->rowbytes - bpp;
3855
3856 while (row < rp_end)
3857 {
3858 int a, b, c, pa, pb, pc, p;
3859
3860 c = *(prev_row - bpp);
3861 a = *(row - bpp);
3862 b = *prev_row++;
3863
3864 p = b - c;
3865 pc = a - c;
3866
3867# ifdef PNG_USE_ABS
3868 pa = abs(p);
3869 pb = abs(pc);
3870 pc = abs(p + pc);
3871# else
3872 pa = p < 0 ? -p : p;
3873 pb = pc < 0 ? -pc : pc;
3874 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
3875# endif
3876
3877 if (pb < pa) pa = pb, a = b;
3878 if (pc < pa) a = c;
3879
John Bowlerfcc02632011-11-03 18:31:00 -05003880 a += *row;
3881 *row++ = (png_byte)a;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003882 }
3883}
Guy Schalnat0d580581995-07-20 02:43:20 -05003884
Mans Rullgardd3a94802011-11-03 00:47:55 -05003885static void
John Bowler5d567862011-12-24 09:12:00 -06003886png_init_filter_functions(png_structrp pp)
Glenn Randers-Pehrson32440202013-08-21 18:01:32 -05003887 /* This function is called once for every PNG image (except for PNG images
3888 * that only use PNG_FILTER_VALUE_NONE for all rows) to set the
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003889 * implementations required to reverse the filtering of PNG rows. Reversing
3890 * the filter is the first transformation performed on the row data. It is
3891 * performed in place, therefore an implementation can be selected based on
3892 * the image pixel format. If the implementation depends on image width then
Glenn Randers-Pehrson6e1c74b2013-04-22 11:06:26 -05003893 * take care to ensure that it works correctly if the image is interlaced -
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003894 * interlacing causes the actual row width to vary.
3895 */
Mans Rullgardd3a94802011-11-03 00:47:55 -05003896{
John Bowlerfcc02632011-11-03 18:31:00 -05003897 unsigned int bpp = (pp->pixel_depth + 7) >> 3;
3898
Mans Rullgardd3a94802011-11-03 00:47:55 -05003899 pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub;
3900 pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up;
3901 pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg;
John Bowlerfcc02632011-11-03 18:31:00 -05003902 if (bpp == 1)
3903 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
3904 png_read_filter_row_paeth_1byte_pixel;
3905 else
3906 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
3907 png_read_filter_row_paeth_multibyte_pixel;
Mans Rullgardd3a94802011-11-03 00:47:55 -05003908
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003909#ifdef PNG_FILTER_OPTIMIZATIONS
3910 /* To use this define PNG_FILTER_OPTIMIZATIONS as the name of a function to
3911 * call to install hardware optimizations for the above functions; simply
3912 * replace whatever elements of the pp->read_filter[] array with a hardware
3913 * specific (or, for that matter, generic) optimization.
3914 *
3915 * To see an example of this examine what configure.ac does when
3916 * --enable-arm-neon is specified on the command line.
3917 */
3918 PNG_FILTER_OPTIMIZATIONS(pp, bpp);
Mans Rullgardd3a94802011-11-03 00:47:55 -05003919#endif
3920}
3921
3922void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06003923png_read_filter_row(png_structrp pp, png_row_infop row_info, png_bytep row,
Mans Rullgardd3a94802011-11-03 00:47:55 -05003924 png_const_bytep prev_row, int filter)
3925{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003926 /* OPTIMIZATION: DO NOT MODIFY THIS FUNCTION, instead #define
3927 * PNG_FILTER_OPTIMIZATIONS to a function that overrides the generic
3928 * implementations. See png_init_filter_functions above.
3929 */
Mans Rullgardd3a94802011-11-03 00:47:55 -05003930 if (filter > PNG_FILTER_VALUE_NONE && filter < PNG_FILTER_VALUE_LAST)
Glenn Randers-Pehrson685d79e2013-08-20 21:15:31 -05003931 {
3932 if (pp->read_filter[0] == NULL)
3933 png_init_filter_functions(pp);
3934
Mans Rullgardd3a94802011-11-03 00:47:55 -05003935 pp->read_filter[filter-1](row_info, row, prev_row);
Glenn Randers-Pehrson685d79e2013-08-20 21:15:31 -05003936 }
Mans Rullgardd3a94802011-11-03 00:47:55 -05003937}
3938
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05003939#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05003940void /* PRIVATE */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003941png_read_IDAT_data(png_structrp png_ptr, png_bytep output,
3942 png_alloc_size_t avail_out)
3943{
3944 /* Loop reading IDATs and decompressing the result into output[avail_out] */
3945 png_ptr->zstream.next_out = output;
3946 png_ptr->zstream.avail_out = 0; /* safety: set below */
3947
3948 if (output == NULL)
3949 avail_out = 0;
3950
3951 do
3952 {
3953 int ret;
3954 png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];
3955
3956 if (png_ptr->zstream.avail_in == 0)
3957 {
3958 uInt avail_in;
3959 png_bytep buffer;
3960
3961 while (png_ptr->idat_size == 0)
3962 {
3963 png_crc_finish(png_ptr, 0);
3964
3965 png_ptr->idat_size = png_read_chunk_header(png_ptr);
3966 /* This is an error even in the 'check' case because the code just
3967 * consumed a non-IDAT header.
3968 */
3969 if (png_ptr->chunk_name != png_IDAT)
3970 png_error(png_ptr, "Not enough image data");
3971 }
3972
3973 avail_in = png_ptr->IDAT_read_size;
3974
3975 if (avail_in > png_ptr->idat_size)
3976 avail_in = (uInt)png_ptr->idat_size;
3977
3978 /* A PNG with a gradually increasing IDAT size will defeat this attempt
3979 * to minimize memory usage by causing lots of re-allocs, but
3980 * realistically doing IDAT_read_size re-allocs is not likely to be a
3981 * big problem.
3982 */
3983 buffer = png_read_buffer(png_ptr, avail_in, 0/*error*/);
3984
3985 png_crc_read(png_ptr, buffer, avail_in);
3986 png_ptr->idat_size -= avail_in;
3987
3988 png_ptr->zstream.next_in = buffer;
3989 png_ptr->zstream.avail_in = avail_in;
3990 }
3991
3992 /* And set up the output side. */
3993 if (output != NULL) /* standard read */
3994 {
3995 uInt out = ZLIB_IO_MAX;
3996
3997 if (out > avail_out)
3998 out = (uInt)avail_out;
3999
4000 avail_out -= out;
4001 png_ptr->zstream.avail_out = out;
4002 }
4003
John Bowler43c07e12013-04-07 21:33:30 -05004004 else /* after last row, checking for end */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004005 {
4006 png_ptr->zstream.next_out = tmpbuf;
4007 png_ptr->zstream.avail_out = (sizeof tmpbuf);
4008 }
4009
4010 /* Use NO_FLUSH; this gives zlib the maximum opportunity to optimize the
4011 * process. If the LZ stream is truncated the sequential reader will
4012 * terminally damage the stream, above, by reading the chunk header of the
4013 * following chunk (it then exits with png_error).
4014 *
4015 * TODO: deal more elegantly with truncated IDAT lists.
4016 */
4017 ret = inflate(&png_ptr->zstream, Z_NO_FLUSH);
4018
John Bowler43c07e12013-04-07 21:33:30 -05004019 /* Take the unconsumed output back. */
4020 if (output != NULL)
4021 avail_out += png_ptr->zstream.avail_out;
4022
4023 else /* avail_out counts the extra bytes */
4024 avail_out += (sizeof tmpbuf) - png_ptr->zstream.avail_out;
4025
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004026 png_ptr->zstream.avail_out = 0;
4027
4028 if (ret == Z_STREAM_END)
4029 {
4030 /* Do this for safety; we won't read any more into this row. */
4031 png_ptr->zstream.next_out = NULL;
4032
4033 png_ptr->mode |= PNG_AFTER_IDAT;
4034 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
4035
4036 if (png_ptr->zstream.avail_in > 0 || png_ptr->idat_size > 0)
4037 png_chunk_benign_error(png_ptr, "Extra compressed data");
4038 break;
4039 }
4040
4041 if (ret != Z_OK)
4042 {
4043 png_zstream_error(png_ptr, ret);
4044
4045 if (output != NULL)
4046 png_chunk_error(png_ptr, png_ptr->zstream.msg);
4047
4048 else /* checking */
4049 {
4050 png_chunk_benign_error(png_ptr, png_ptr->zstream.msg);
4051 return;
4052 }
4053 }
4054 } while (avail_out > 0);
4055
4056 if (avail_out > 0)
4057 {
4058 /* The stream ended before the image; this is the same as too few IDATs so
4059 * should be handled the same way.
4060 */
4061 if (output != NULL)
4062 png_error(png_ptr, "Not enough image data");
4063
John Bowler43c07e12013-04-07 21:33:30 -05004064 else /* the deflate stream contained extra data */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004065 png_chunk_benign_error(png_ptr, "Too much image data");
4066 }
4067}
4068
4069void /* PRIVATE */
4070png_read_finish_IDAT(png_structrp png_ptr)
4071{
4072 /* We don't need any more data and the stream should have ended, however the
4073 * LZ end code may actually not have been processed. In this case we must
4074 * read it otherwise stray unread IDAT data or, more likely, an IDAT chunk
4075 * may still remain to be consumed.
4076 */
4077 if (!(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED))
4078 {
4079 /* The NULL causes png_read_IDAT_data to swallow any remaining bytes in
4080 * the compressed stream, but the stream may be damaged too, so even after
4081 * this call we may need to terminate the zstream ownership.
4082 */
4083 png_read_IDAT_data(png_ptr, NULL, 0);
4084 png_ptr->zstream.next_out = NULL; /* safety */
4085
4086 /* Now clear everything out for safety; the following may not have been
4087 * done.
4088 */
4089 if (!(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED))
4090 {
4091 png_ptr->mode |= PNG_AFTER_IDAT;
4092 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
4093 }
4094 }
4095
4096 /* If the zstream has not been released do it now *and* terminate the reading
4097 * of the final IDAT chunk.
4098 */
4099 if (png_ptr->zowner == png_IDAT)
4100 {
4101 /* Always do this; the pointers otherwise point into the read buffer. */
4102 png_ptr->zstream.next_in = NULL;
4103 png_ptr->zstream.avail_in = 0;
4104
4105 /* Now we no longer own the zstream. */
4106 png_ptr->zowner = 0;
4107
4108 /* The slightly weird semantics of the sequential IDAT reading is that we
4109 * are always in or at the end of an IDAT chunk, so we always need to do a
4110 * crc_finish here. If idat_size is non-zero we also need to read the
4111 * spurious bytes at the end of the chunk now.
4112 */
4113 (void)png_crc_finish(png_ptr, png_ptr->idat_size);
4114 }
4115}
4116
4117void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06004118png_read_finish_row(png_structrp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05004119{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004120#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004121 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004122
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004123 /* Start of interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004124 static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004125
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004126 /* Offset to next interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004127 static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004128
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004129 /* Start of interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004130 static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004131
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004132 /* Offset to next interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004133 static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004134#endif /* PNG_READ_INTERLACING_SUPPORTED */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004135
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05004136 png_debug(1, "in png_read_finish_row");
Guy Schalnat0d580581995-07-20 02:43:20 -05004137 png_ptr->row_number++;
4138 if (png_ptr->row_number < png_ptr->num_rows)
4139 return;
4140
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004141#ifdef PNG_READ_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05004142 if (png_ptr->interlaced)
4143 {
4144 png_ptr->row_number = 0;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004145
Glenn Randers-Pehrson435cf872011-10-05 16:23:53 -05004146 /* TO DO: don't do this if prev_row isn't needed (requires
4147 * read-ahead of the next row's filter byte.
4148 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004149 memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004150
Guy Schalnat0d580581995-07-20 02:43:20 -05004151 do
4152 {
4153 png_ptr->pass++;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004154
Guy Schalnat0d580581995-07-20 02:43:20 -05004155 if (png_ptr->pass >= 7)
4156 break;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004157
Guy Schalnat0d580581995-07-20 02:43:20 -05004158 png_ptr->iwidth = (png_ptr->width +
4159 png_pass_inc[png_ptr->pass] - 1 -
4160 png_pass_start[png_ptr->pass]) /
4161 png_pass_inc[png_ptr->pass];
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05004162
Guy Schalnat0d580581995-07-20 02:43:20 -05004163 if (!(png_ptr->transformations & PNG_INTERLACE))
4164 {
4165 png_ptr->num_rows = (png_ptr->height +
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004166 png_pass_yinc[png_ptr->pass] - 1 -
4167 png_pass_ystart[png_ptr->pass]) /
4168 png_pass_yinc[png_ptr->pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05004169 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004170
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -05004171 else /* if (png_ptr->transformations & PNG_INTERLACE) */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004172 break; /* libpng deinterlacing sees every row */
4173
4174 } while (png_ptr->num_rows == 0 || png_ptr->iwidth == 0);
Guy Schalnat0d580581995-07-20 02:43:20 -05004175
4176 if (png_ptr->pass < 7)
4177 return;
4178 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004179#endif /* PNG_READ_INTERLACING_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05004180
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004181 /* Here after at the end of the last row of the last pass. */
4182 png_read_finish_IDAT(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05004183}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05004184#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05004185
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05004186void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06004187png_read_start_row(png_structrp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05004188{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004189#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004190 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004191
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004192 /* Start of interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004193 static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004194
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004195 /* Offset to next interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004196 static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004197
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004198 /* Start of interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004199 static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004200
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004201 /* Offset to next interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004202 static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004203#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004204
Guy Schalnat0d580581995-07-20 02:43:20 -05004205 int max_pixel_depth;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05004206 png_size_t row_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05004207
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05004208 png_debug(1, "in png_read_start_row");
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004209
John Bowler4a12f4a2011-04-17 18:34:22 -05004210#ifdef PNG_READ_TRANSFORMS_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05004211 png_init_read_transformations(png_ptr);
John Bowler4a12f4a2011-04-17 18:34:22 -05004212#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004213#ifdef PNG_READ_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05004214 if (png_ptr->interlaced)
4215 {
4216 if (!(png_ptr->transformations & PNG_INTERLACE))
4217 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004218 png_pass_ystart[0]) / png_pass_yinc[0];
4219
Guy Schalnat0d580581995-07-20 02:43:20 -05004220 else
4221 png_ptr->num_rows = png_ptr->height;
4222
4223 png_ptr->iwidth = (png_ptr->width +
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004224 png_pass_inc[png_ptr->pass] - 1 -
4225 png_pass_start[png_ptr->pass]) /
4226 png_pass_inc[png_ptr->pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05004227 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004228
Guy Schalnat0d580581995-07-20 02:43:20 -05004229 else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004230#endif /* PNG_READ_INTERLACING_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05004231 {
4232 png_ptr->num_rows = png_ptr->height;
4233 png_ptr->iwidth = png_ptr->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05004234 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004235
Guy Schalnat0d580581995-07-20 02:43:20 -05004236 max_pixel_depth = png_ptr->pixel_depth;
4237
John Bowlere6fb6912011-11-08 21:35:16 -06004238 /* WARNING: * png_read_transform_info (pngrtran.c) performs a simpliar set of
4239 * calculations to calculate the final pixel depth, then
4240 * png_do_read_transforms actually does the transforms. This means that the
4241 * code which effectively calculates this value is actually repeated in three
4242 * separate places. They must all match. Innocent changes to the order of
4243 * transformations can and will break libpng in a way that causes memory
4244 * overwrites.
4245 *
4246 * TODO: fix this.
4247 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004248#ifdef PNG_READ_PACK_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05004249 if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8)
Guy Schalnat0d580581995-07-20 02:43:20 -05004250 max_pixel_depth = 8;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004251#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05004252
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004253#ifdef PNG_READ_EXPAND_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -05004254 if (png_ptr->transformations & PNG_EXPAND)
Guy Schalnat0d580581995-07-20 02:43:20 -05004255 {
4256 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
4257 {
4258 if (png_ptr->num_trans)
4259 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004260
Guy Schalnat0d580581995-07-20 02:43:20 -05004261 else
4262 max_pixel_depth = 24;
4263 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004264
Guy Schalnat0d580581995-07-20 02:43:20 -05004265 else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
4266 {
4267 if (max_pixel_depth < 8)
4268 max_pixel_depth = 8;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004269
Guy Schalnat0d580581995-07-20 02:43:20 -05004270 if (png_ptr->num_trans)
4271 max_pixel_depth *= 2;
4272 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004273
Guy Schalnat0d580581995-07-20 02:43:20 -05004274 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
4275 {
4276 if (png_ptr->num_trans)
4277 {
4278 max_pixel_depth *= 4;
4279 max_pixel_depth /= 3;
4280 }
4281 }
4282 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004283#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05004284
John Bowler4d562962011-02-12 09:01:20 -06004285#ifdef PNG_READ_EXPAND_16_SUPPORTED
4286 if (png_ptr->transformations & PNG_EXPAND_16)
4287 {
4288# ifdef PNG_READ_EXPAND_SUPPORTED
4289 /* In fact it is an error if it isn't supported, but checking is
4290 * the safe way.
4291 */
4292 if (png_ptr->transformations & PNG_EXPAND)
4293 {
4294 if (png_ptr->bit_depth < 16)
4295 max_pixel_depth *= 2;
4296 }
4297 else
4298# endif
4299 png_ptr->transformations &= ~PNG_EXPAND_16;
4300 }
4301#endif
4302
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004303#ifdef PNG_READ_FILLER_SUPPORTED
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004304 if (png_ptr->transformations & (PNG_FILLER))
Guy Schalnat0d580581995-07-20 02:43:20 -05004305 {
John Bowlere6fb6912011-11-08 21:35:16 -06004306 if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004307 {
4308 if (max_pixel_depth <= 8)
4309 max_pixel_depth = 16;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004310
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004311 else
4312 max_pixel_depth = 32;
4313 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004314
John Bowlere6fb6912011-11-08 21:35:16 -06004315 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB ||
4316 png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004317 {
4318 if (max_pixel_depth <= 32)
4319 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004320
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004321 else
4322 max_pixel_depth = 64;
4323 }
Guy Schalnat0d580581995-07-20 02:43:20 -05004324 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004325#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05004326
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004327#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05004328 if (png_ptr->transformations & PNG_GRAY_TO_RGB)
4329 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06004330 if (
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004331#ifdef PNG_READ_EXPAND_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004332 (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) ||
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06004333#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004334#ifdef PNG_READ_FILLER_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004335 (png_ptr->transformations & (PNG_FILLER)) ||
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06004336#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004337 png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
Guy Schalnat0d580581995-07-20 02:43:20 -05004338 {
4339 if (max_pixel_depth <= 16)
4340 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004341
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06004342 else
Guy Schalnat0d580581995-07-20 02:43:20 -05004343 max_pixel_depth = 64;
4344 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004345
Guy Schalnat0d580581995-07-20 02:43:20 -05004346 else
4347 {
4348 if (max_pixel_depth <= 8)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004349 {
4350 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06004351 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004352
4353 else
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06004354 max_pixel_depth = 24;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004355 }
4356
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06004357 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4358 max_pixel_depth = 64;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004359
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06004360 else
Guy Schalnat0d580581995-07-20 02:43:20 -05004361 max_pixel_depth = 48;
4362 }
4363 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004364#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05004365
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05004366#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
4367defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004368 if (png_ptr->transformations & PNG_USER_TRANSFORM)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004369 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004370 int user_pixel_depth = png_ptr->user_transform_depth *
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05004371 png_ptr->user_transform_channels;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004372
4373 if (user_pixel_depth > max_pixel_depth)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004374 max_pixel_depth = user_pixel_depth;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004375 }
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05004376#endif
4377
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004378 /* This value is stored in png_struct and double checked in the row read
4379 * code.
4380 */
4381 png_ptr->maximum_pixel_depth = (png_byte)max_pixel_depth;
4382 png_ptr->transformed_pixel_depth = 0; /* calculated on demand */
4383
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004384 /* Align the width on the next larger 8 pixels. Mainly used
4385 * for interlacing
4386 */
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05004387 row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004388 /* Calculate the maximum bytes needed, adding a byte and a pixel
4389 * for safety's sake
4390 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004391 row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) +
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004392 1 + ((max_pixel_depth + 7) >> 3);
4393
Guy Schalnat0d580581995-07-20 02:43:20 -05004394#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05004395 if (row_bytes > (png_uint_32)65536L)
Guy Schalnate5a37791996-06-05 15:50:50 -05004396 png_error(png_ptr, "This image requires a row greater than 64KB");
Guy Schalnat0d580581995-07-20 02:43:20 -05004397#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004398
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06004399 if (row_bytes + 48 > png_ptr->old_big_row_buf_size)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004400 {
4401 png_free(png_ptr, png_ptr->big_row_buf);
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004402 png_free(png_ptr, png_ptr->big_prev_row);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004403
Glenn Randers-Pehrson6917b512009-03-09 15:31:08 -05004404 if (png_ptr->interlaced)
Glenn Randers-Pehrsona515d302010-01-01 10:24:25 -06004405 png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr,
4406 row_bytes + 48);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004407
Glenn Randers-Pehrsona515d302010-01-01 10:24:25 -06004408 else
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004409 png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004410
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004411 png_ptr->big_prev_row = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06004412
4413#ifdef PNG_ALIGNED_MEMORY_SUPPORTED
4414 /* Use 16-byte aligned memory for row_buf with at least 16 bytes
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004415 * of padding before and after row_buf; treat prev_row similarly.
John Bowlerac8375d2011-10-06 22:27:16 -05004416 * NOTE: the alignment is to the start of the pixels, one beyond the start
4417 * of the buffer, because of the filter byte. Prior to libpng 1.5.6 this
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004418 * was incorrect; the filter byte was aligned, which had the exact
4419 * opposite effect of that intended.
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06004420 */
John Bowlerac8375d2011-10-06 22:27:16 -05004421 {
4422 png_bytep temp = png_ptr->big_row_buf + 32;
Glenn Randers-Pehrson8db19982011-10-27 16:17:24 -05004423 int extra = (int)((temp - (png_bytep)0) & 0x0f);
John Bowlerac8375d2011-10-06 22:27:16 -05004424 png_ptr->row_buf = temp - extra - 1/*filter byte*/;
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004425
4426 temp = png_ptr->big_prev_row + 32;
Glenn Randers-Pehrson8db19982011-10-27 16:17:24 -05004427 extra = (int)((temp - (png_bytep)0) & 0x0f);
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004428 png_ptr->prev_row = temp - extra - 1/*filter byte*/;
John Bowlerac8375d2011-10-06 22:27:16 -05004429 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004430
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06004431#else
John Bowlerac8375d2011-10-06 22:27:16 -05004432 /* Use 31 bytes of padding before and 17 bytes after row_buf. */
4433 png_ptr->row_buf = png_ptr->big_row_buf + 31;
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004434 png_ptr->prev_row = png_ptr->big_prev_row + 31;
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06004435#endif
4436 png_ptr->old_big_row_buf_size = row_bytes + 48;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004437 }
Guy Schalnat0d580581995-07-20 02:43:20 -05004438
4439#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004440 if (png_ptr->rowbytes > 65535)
Guy Schalnate5a37791996-06-05 15:50:50 -05004441 png_error(png_ptr, "This image requires a row greater than 64KB");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004442
Guy Schalnat0d580581995-07-20 02:43:20 -05004443#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004444 if (png_ptr->rowbytes > (PNG_SIZE_MAX - 1))
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05004445 png_error(png_ptr, "Row has too many bytes to allocate in memory");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004446
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004447 memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05004448
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004449 png_debug1(3, "width = %u,", png_ptr->width);
4450 png_debug1(3, "height = %u,", png_ptr->height);
4451 png_debug1(3, "iwidth = %u,", png_ptr->iwidth);
4452 png_debug1(3, "num_rows = %u,", png_ptr->num_rows);
Glenn Randers-Pehrsonb764c602011-01-14 21:18:37 -06004453 png_debug1(3, "rowbytes = %lu,", (unsigned long)png_ptr->rowbytes);
4454 png_debug1(3, "irowbytes = %lu",
4455 (unsigned long)PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05004456
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004457 /* The sequential reader needs a buffer for IDAT, but the progressive reader
4458 * does not, so free the read buffer now regardless; the sequential reader
4459 * reallocates it on demand.
4460 */
4461 if (png_ptr->read_buffer)
4462 {
4463 png_bytep buffer = png_ptr->read_buffer;
4464
4465 png_ptr->read_buffer_size = 0;
4466 png_ptr->read_buffer = NULL;
4467 png_free(png_ptr, buffer);
4468 }
4469
John Bowler0c7ac062013-05-07 21:59:05 -05004470 /* Finally claim the zstream for the inflate of the IDAT data, use the bits
4471 * value from the stream (note that this will result in a fatal error if the
4472 * IDAT stream has a bogus deflate header window_bits value, but this should
4473 * not be happening any longer!)
Glenn Randers-Pehrsondb67cba2013-05-07 14:31:35 -05004474 */
John Bowler0c7ac062013-05-07 21:59:05 -05004475 if (png_inflate_claim(png_ptr, png_IDAT) != Z_OK)
4476 png_error(png_ptr, png_ptr->zstream.msg);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004477
Guy Schalnate5a37791996-06-05 15:50:50 -05004478 png_ptr->flags |= PNG_FLAG_ROW_INIT;
Guy Schalnat0d580581995-07-20 02:43:20 -05004479}
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06004480#endif /* PNG_READ_SUPPORTED */