blob: 2befd4add2cafec136cd14c3c54190c63c61592b [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-Pehrson67ee8ce2011-12-22 08:21:00 -06004 * Last changed in libpng 1.6.0 [(PENDING RELEASE)]
Glenn Randers-Pehrson1531bd62012-01-01 14:45:04 -06005 * Copyright (c) 1998-2012 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 -060021#define png_strtod(p,a,b) strtod(a,b)
22
23png_uint_32 PNGAPI
John Bowler5d567862011-12-24 09:12:00 -060024png_get_uint_31(png_const_structrp png_ptr, png_const_bytep buf)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -060025{
26 png_uint_32 uval = png_get_uint_32(buf);
27
28 if (uval > PNG_UINT_31_MAX)
29 png_error(png_ptr, "PNG unsigned integer out of range");
30
31 return (uval);
32}
33
34#if defined(PNG_READ_gAMA_SUPPORTED) || defined(PNG_READ_cHRM_SUPPORTED)
35/* The following is a variation on the above for use with the fixed
36 * point values used for gAMA and cHRM. Instead of png_error it
37 * issues a warning and returns (-1) - an invalid value because both
38 * gAMA and cHRM use *unsigned* integers for fixed point values.
39 */
40#define PNG_FIXED_ERROR (-1)
41
42static png_fixed_point /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -060043png_get_fixed_point(png_structrp png_ptr, png_const_bytep buf)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -060044{
45 png_uint_32 uval = png_get_uint_32(buf);
46
47 if (uval <= PNG_UINT_31_MAX)
48 return (png_fixed_point)uval; /* known to be in range */
49
50 /* The caller can turn off the warning by passing NULL. */
51 if (png_ptr != NULL)
52 png_warning(png_ptr, "PNG fixed point integer out of range");
53
54 return PNG_FIXED_ERROR;
55}
56#endif
57
58#ifdef PNG_READ_INT_FUNCTIONS_SUPPORTED
59/* NOTE: the read macros will obscure these definitions, so that if
60 * PNG_USE_READ_MACROS is set the library will not use them internally,
61 * but the APIs will still be available externally.
62 *
63 * The parentheses around "PNGAPI function_name" in the following three
64 * functions are necessary because they allow the macros to co-exist with
65 * these (unused but exported) functions.
66 */
67
68/* Grab an unsigned 32-bit integer from a buffer in big-endian format. */
69png_uint_32 (PNGAPI
70png_get_uint_32)(png_const_bytep buf)
71{
72 png_uint_32 uval =
73 ((png_uint_32)(*(buf )) << 24) +
74 ((png_uint_32)(*(buf + 1)) << 16) +
75 ((png_uint_32)(*(buf + 2)) << 8) +
76 ((png_uint_32)(*(buf + 3)) ) ;
77
78 return uval;
Guy Schalnat0d580581995-07-20 02:43:20 -050079}
80
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -050081/* Grab a signed 32-bit integer from a buffer in big-endian format. The
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -060082 * data is stored in the PNG file in two's complement format and there
83 * is no guarantee that a 'png_int_32' is exactly 32 bits, therefore
84 * the following code does a two's complement to native conversion.
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050085 */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -060086png_int_32 (PNGAPI
87png_get_int_32)(png_const_bytep buf)
Andreas Dilger47a0c421997-05-16 02:46:07 -050088{
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -060089 png_uint_32 uval = png_get_uint_32(buf);
John Bowlerf3f7e142011-09-09 07:32:37 -050090 if ((uval & 0x80000000) == 0) /* non-negative */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -060091 return uval;
Andreas Dilger47a0c421997-05-16 02:46:07 -050092
John Bowlerf3f7e142011-09-09 07:32:37 -050093 uval = (uval ^ 0xffffffff) + 1; /* 2's complement: -x = ~x+1 */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -060094 return -(png_int_32)uval;
Andreas Dilger47a0c421997-05-16 02:46:07 -050095}
Andreas Dilger47a0c421997-05-16 02:46:07 -050096
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -050097/* Grab an unsigned 16-bit integer from a buffer in big-endian format. */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -060098png_uint_16 (PNGAPI
99png_get_uint_16)(png_const_bytep buf)
Guy Schalnat0d580581995-07-20 02:43:20 -0500100{
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600101 /* ANSI-C requires an int value to accomodate at least 16 bits so this
102 * works and allows the compiler not to worry about possible narrowing
103 * on 32 bit systems. (Pre-ANSI systems did not make integers smaller
104 * than 16 bits either.)
105 */
106 unsigned int val =
107 ((unsigned int)(*buf) << 8) +
108 ((unsigned int)(*(buf + 1)));
Guy Schalnat0d580581995-07-20 02:43:20 -0500109
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600110 return (png_uint_16)val;
Guy Schalnat0d580581995-07-20 02:43:20 -0500111}
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600112
113#endif /* PNG_READ_INT_FUNCTIONS_SUPPORTED */
114
115/* Read and check the PNG file signature */
116void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600117png_read_sig(png_structrp png_ptr, png_inforp info_ptr)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600118{
119 png_size_t num_checked, num_to_check;
120
121 /* Exit if the user application does not expect a signature. */
122 if (png_ptr->sig_bytes >= 8)
123 return;
124
125 num_checked = png_ptr->sig_bytes;
126 num_to_check = 8 - num_checked;
127
128#ifdef PNG_IO_STATE_SUPPORTED
129 png_ptr->io_state = PNG_IO_READING | PNG_IO_SIGNATURE;
130#endif
131
132 /* The signature must be serialized in a single I/O call. */
133 png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
134 png_ptr->sig_bytes = 8;
135
136 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
137 {
138 if (num_checked < 4 &&
139 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
140 png_error(png_ptr, "Not a PNG file");
141 else
142 png_error(png_ptr, "PNG file corrupted by ASCII conversion");
143 }
144 if (num_checked < 3)
145 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
146}
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -0600147
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500148/* Read the chunk header (length + type name).
149 * Put the type name into png_ptr->chunk_name, and return the length.
150 */
151png_uint_32 /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600152png_read_chunk_header(png_structrp png_ptr)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500153{
154 png_byte buf[8];
155 png_uint_32 length;
156
157#ifdef PNG_IO_STATE_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500158 png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_HDR;
159#endif
160
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600161 /* Read the length and the chunk name.
162 * This must be performed in a single I/O call.
163 */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500164 png_read_data(png_ptr, buf, 8);
165 length = png_get_uint_31(png_ptr, buf);
166
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600167 /* Put the chunk name into png_ptr->chunk_name. */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500168 png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(buf+4);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500169
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500170 png_debug2(0, "Reading %lx chunk, length = %lu",
171 (unsigned long)png_ptr->chunk_name, (unsigned long)length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500172
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600173 /* Reset the crc and run it over the chunk name. */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500174 png_reset_crc(png_ptr);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500175 png_calculate_crc(png_ptr, buf + 4, 4);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500176
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600177 /* Check to see if chunk name is valid. */
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500178 png_check_chunk_name(png_ptr, png_ptr->chunk_name);
179
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500180#ifdef PNG_IO_STATE_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500181 png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_DATA;
182#endif
183
184 return length;
185}
186
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500187/* Read data, and (optionally) run it through the CRC. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500188void /* PRIVATE */
John Bowler0ae4f7b2012-03-03 21:10:26 -0600189png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500190{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500191 if (png_ptr == NULL)
192 return;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600193
Guy Schalnat6d764711995-12-19 03:22:19 -0600194 png_read_data(png_ptr, buf, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500195 png_calculate_crc(png_ptr, buf, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500196}
197
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600198/* Optionally skip data and then check the CRC. Depending on whether we
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500199 * are reading a ancillary or critical chunk, and how the program has set
200 * things up, we may calculate the CRC on the data and print a message.
201 * Returns '1' if there was a CRC error, '0' otherwise.
202 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500203int /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600204png_crc_finish(png_structrp png_ptr, png_uint_32 skip)
Guy Schalnat0d580581995-07-20 02:43:20 -0500205{
John Bowlerb5d00512012-03-09 09:15:18 -0600206 /* The size of the local buffer for inflate is a good guess as to a
207 * reasonable size to use for buffering reads from the application.
208 */
209 while (skip > 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600210 {
John Bowlerb5d00512012-03-09 09:15:18 -0600211 png_uint_32 len;
212 png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600213
John Bowlerb5d00512012-03-09 09:15:18 -0600214 len = sizeof tmpbuf;
215 if (len > skip)
216 len = skip;
217 skip -= len;
218
219 png_crc_read(png_ptr, tmpbuf, len);
Guy Schalnat0d580581995-07-20 02:43:20 -0500220 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600221
Andreas Dilger47a0c421997-05-16 02:46:07 -0500222 if (png_crc_error(png_ptr))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600223 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500224 if (PNG_CHUNK_ANCILLIARY(png_ptr->chunk_name) ?
225 !(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) :
226 (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600227 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600228 png_chunk_warning(png_ptr, "CRC error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600229 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600230
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600231 else
232 {
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500233 png_chunk_benign_error(png_ptr, "CRC error");
234 return (0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600235 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600236
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600237 return (1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600238 }
239
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600240 return (0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500241}
242
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600243/* 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 -0500244 * the data it has read thus far.
245 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500246int /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600247png_crc_error(png_structrp png_ptr)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600248{
249 png_byte crc_bytes[4];
250 png_uint_32 crc;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500251 int need_crc = 1;
252
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500253 if (PNG_CHUNK_ANCILLIARY(png_ptr->chunk_name))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500254 {
255 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
256 (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
257 need_crc = 0;
258 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600259
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500260 else /* critical */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500261 {
262 if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
263 need_crc = 0;
264 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600265
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500266#ifdef PNG_IO_STATE_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500267 png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_CRC;
268#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500269
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600270 /* The chunk CRC must be serialized in a single I/O call. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600271 png_read_data(png_ptr, crc_bytes, 4);
272
Andreas Dilger47a0c421997-05-16 02:46:07 -0500273 if (need_crc)
274 {
275 crc = png_get_uint_32(crc_bytes);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600276 return ((int)(crc != png_ptr->crc));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500277 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600278
Andreas Dilger47a0c421997-05-16 02:46:07 -0500279 else
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600280 return (0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600281}
282
John Bowlerb5d00512012-03-09 09:15:18 -0600283/* Manage the read buffer; this simply reallocates the buffer if it is not small
284 * enough (or if it is not allocated). The routine returns a pointer to the
285 * buffer, if an error occurs and 'warn' is set the routine returns NULL, else
286 * it will call png_error (via png_malloc) on failure. (warn == 2 means
287 * 'silent').
John Bowler42a2b552012-03-05 20:57:40 -0600288 */
John Bowlerb5d00512012-03-09 09:15:18 -0600289static png_bytep
290png_read_buffer(png_structrp png_ptr, png_alloc_size_t new_size, int warn)
John Bowler42a2b552012-03-05 20:57:40 -0600291{
John Bowlerb5d00512012-03-09 09:15:18 -0600292 png_bytep buffer = png_ptr->read_buffer;
293
294 if (buffer != NULL && new_size > png_ptr->read_buffer_size)
295 {
296 png_ptr->read_buffer = NULL;
297 png_ptr->read_buffer = NULL;
298 png_ptr->read_buffer_size = 0;
299 png_free(png_ptr, buffer);
300 buffer = NULL;
301 }
302
303 if (buffer == NULL)
304 {
305 buffer = png_voidcast(png_bytep, png_malloc_base(png_ptr, new_size));
306
307 if (buffer != NULL)
308 {
309 png_ptr->read_buffer = buffer;
310 png_ptr->read_buffer_size = new_size;
311 }
312
313 else if (warn < 2) /* else silent */
314 {
315 (warn ? png_chunk_warning : png_chunk_error)(png_ptr,
316 "insufficient memory to read chunk");
317 }
318 }
319
320 return buffer;
321}
322
323/* png_inflate_claim: claim the zstream for some nefarious purpose that involves
324 * decompression. Returns Z_OK on success, else a zlib error code. It checks
325 * the owner but, in final release builds, just issues a warning if some other
326 * chunk apparently owns the stream. Prior to release it does a png_error.
327 */
328static int
329png_inflate_claim(png_structrp png_ptr, png_uint_32 owner)
330{
331 if (png_ptr->zowner != 0)
332 {
333 char msg[64];
334
335 PNG_STRING_FROM_CHUNK(msg, png_ptr->zowner);
336 /* So the message that results is "<chunk> using zstream"; this is an
337 * internal error, but is very useful for debugging. i18n requirements
338 * are minimal.
339 */
340 (void)png_safecat(msg, sizeof msg, 4, " using zstream");
341# if PNG_LIBPNG_BUILD_BASE_TYPE == PNG_LIBPNG_BUILD_STABLE
342 png_chunk_warning(png_ptr, msg);
343 png_ptr->zowner = 0;
344# else
345 png_chunk_error(png_ptr, msg);
346# endif
347 }
348
John Bowler42a2b552012-03-05 20:57:40 -0600349 /* Implementation note: unlike 'png_deflate_claim' this internal function
350 * does not take the size of the data as an argument. Some efficiency could
351 * be gained by using this when it is known *if* the zlib stream itself does
352 * not record the number, however this is a chimera: the original writer of
353 * the PNG may have selected a lower window size, and we really must follow
354 * that because, for systems with with limited capabilities, we would
355 * otherwise reject the applications attempts to use a smaller window size.
356 * (zlib doesn't have an interface to say "this or lower"!)
John Bowlerb5d00512012-03-09 09:15:18 -0600357 *
358 * inflateReset2 was added to zlib 1.2.4; before this the window could not be
359 * reset, therefore it is necessary to always allocate the maximum window
360 * size with earlier zlibs just in case later compressed chunks need it.
John Bowler42a2b552012-03-05 20:57:40 -0600361 */
John Bowler42a2b552012-03-05 20:57:40 -0600362 {
363 int ret; /* zlib return code */
364
John Bowlerb5d00512012-03-09 09:15:18 -0600365 /* Set this for safety, just in case the previous owner left pointers to
366 * memory allocations.
367 */
368 png_ptr->zstream.next_in = NULL;
369 png_ptr->zstream.avail_in = 0;
370 png_ptr->zstream.next_out = NULL;
371 png_ptr->zstream.avail_out = 0;
John Bowler42a2b552012-03-05 20:57:40 -0600372
373 if (png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED)
John Bowlerb5d00512012-03-09 09:15:18 -0600374 {
375# if ZLIB_VERNUM < 0x1240
376 ret = inflateReset(&png_ptr->zstream);
377# else
378 ret = inflateReset2(&png_ptr->zstream, 0/*use stream windowBits*/);
379# endif
380 }
John Bowler42a2b552012-03-05 20:57:40 -0600381
382 else
383 {
John Bowlerb5d00512012-03-09 09:15:18 -0600384# if ZLIB_VERNUM < 0x1240
385 ret = inflateInit(&png_ptr->zstream);
386# else
387 ret = inflateInit2(&png_ptr->zstream, 0/*use stream windowBits*/);
388# endif
John Bowler42a2b552012-03-05 20:57:40 -0600389
390 if (ret == Z_OK)
391 png_ptr->flags |= PNG_FLAG_ZSTREAM_INITIALIZED;
392 }
393
394 if (ret == Z_OK)
John Bowlerb5d00512012-03-09 09:15:18 -0600395 png_ptr->zowner = owner;
John Bowler42a2b552012-03-05 20:57:40 -0600396
397 else
John Bowlerb5d00512012-03-09 09:15:18 -0600398 png_zstream_error(png_ptr, ret);
John Bowler42a2b552012-03-05 20:57:40 -0600399
John Bowlerb5d00512012-03-09 09:15:18 -0600400 return ret;
401 }
402}
403
404/* png_inflate now returns zlib error codes including Z_OK and Z_STREAM_END to
405 * allow the caller to do multiple calls if required. If the 'finish' flag is
406 * set Z_FINISH will be passed to the final inflate() call and Z_STREAM_END must
407 * be returned or there has been a problem, otherwise Z_SYNC_FLUSH is used and
408 * Z_OK or Z_STREAM_END will be returned on success.
409 *
410 * The input and output sizes are updated to the actual amounts of data consumed
411 * or written, not the amount available (as in a z_stream). The data pointers
412 * are not changed, so the next input is (data+input_size) and the next
413 * available output is (output+output_size).
414 */
415static int
416png_inflate(png_structrp png_ptr, png_uint_32 owner, int finish,
417 /* INPUT: */ png_const_bytep input, png_uint_32p input_size_ptr,
418 /* OUTPUT: */ png_bytep output, png_alloc_size_t *output_size_ptr)
419{
420 if (png_ptr->zowner == owner) /* Else not claimed */
421 {
422 int ret;
423 png_alloc_size_t avail_out = *output_size_ptr;
424 png_uint_32 avail_in = *input_size_ptr;
425
426 /* zlib can't necessarily handle more than 65535 bytes at once (i.e. it
427 * can't even necessarily handle 65536 bytes) because the type uInt is
428 * "16 bits or more". Consequently it is necessary to chunk the input to
429 * zlib. This code uses ZLIB_IO_MAX, from pngpriv.h, as the maximum (the
430 * maximum value that can be stored in a uInt.) It is possible to set
431 * ZLIB_IO_MAX to a lower value in pngpriv.h and this may sometimes have
432 * a performance advantage, because it reduces the amount of data accessed
433 * at each step and that may give the OS more time to page it in.
434 */
435 png_ptr->zstream.next_in = PNGZ_INPUT_CAST(input);
436 /* avail_in and avail_out are set below from 'size' */
437 png_ptr->zstream.avail_in = 0;
438 png_ptr->zstream.avail_out = 0;
439
440 /* Read directly into the output if it is available (this is set to
441 * a local buffer below if output is NULL).
442 */
443 if (output != NULL)
444 png_ptr->zstream.next_out = output;
445
446 do
447 {
448 uInt avail;
449 Byte local_buffer[PNG_INFLATE_BUF_SIZE];
450
451 /* zlib INPUT BUFFER */
452 /* The setting of 'avail_in' used to be outside the loop; by setting it
453 * inside it is possible to chunk the input to zlib and simply rely on
454 * zlib to advance the 'next_in' pointer. This allows arbitrary
455 * amounts of data to be passed through zlib at the unavoidable cost of
456 * requiring a window save (png_memcpy of up to 32768 output bytes)
457 * every ZLIB_IO_MAX input bytes.
458 */
459 avail_in += png_ptr->zstream.avail_in; /* not consumed last time */
460
461 avail = ZLIB_IO_MAX;
462
463 if (avail_in < avail)
464 avail = (uInt)avail_in; /* safe: < than ZLIB_IO_MAX */
465
466 avail_in -= avail;
467 png_ptr->zstream.avail_in = avail;
468
469 /* zlib OUTPUT BUFFER */
470 avail_out += png_ptr->zstream.avail_out; /* not written last time */
471
472 avail = ZLIB_IO_MAX; /* maximum zlib can process */
473
474 if (output == NULL)
475 {
476 /* Reset the output buffer each time round if output is NULL and
477 * make available the full buffer, up to 'remaining_space'
478 */
479 png_ptr->zstream.next_out = local_buffer;
480 if (sizeof local_buffer < avail)
481 avail = sizeof local_buffer;
482 }
483
484 if (avail_out < avail)
485 avail = (uInt)avail_out; /* safe: < ZLIB_IO_MAX */
486
487 png_ptr->zstream.avail_out = avail;
488 avail_out -= avail;
489
490 /* zlib inflate call */
491 /* In fact 'avail_out' may be 0 at this point, that happens at the end
492 * of the read when the final LZ end code was not passed at the end of
493 * the previous chunk of input data. Tell zlib if we have reached the
494 * end of the output buffer.
495 */
496 ret = inflate(&png_ptr->zstream, avail_out > 0 ? Z_NO_FLUSH :
497 (finish ? Z_FINISH : Z_SYNC_FLUSH));
498 } while (ret == Z_OK);
499
500 /* For safety kill the local buffer pointer now */
501 if (output == NULL)
502 png_ptr->zstream.next_out = NULL;
503
504 /* Claw back the 'size' and 'remaining_space' byte counts. */
505 avail_in += png_ptr->zstream.avail_in;
506 avail_out += png_ptr->zstream.avail_out;
507
508 /* Update the input and output sizes; the updated values are the amount
509 * consumed or written, effectively the inverse of what zlib uses.
510 */
511 if (avail_out > 0)
512 *output_size_ptr -= avail_out;
513
514 if (avail_in > 0)
515 *input_size_ptr -= avail_in;
516
517 /* Ensure png_ptr->zstream.msg is set (even in the success case!) */
518 png_zstream_error(png_ptr, ret);
519 return ret;
John Bowler42a2b552012-03-05 20:57:40 -0600520 }
521
522 else
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600523 {
John Bowlerb5d00512012-03-09 09:15:18 -0600524 /* This is a bad internal error. The recovery assigns to the zstream msg
525 * pointer, which is not owned by the caller, but this is safe; it's only
526 * used on errors!
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600527 */
John Bowlerb5d00512012-03-09 09:15:18 -0600528 png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed");
529 return Z_STREAM_ERROR;
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600530 }
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600531}
532
John Bowlerb5d00512012-03-09 09:15:18 -0600533#ifdef PNG_READ_COMPRESSED_TEXT_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600534/*
John Bowlerb5d00512012-03-09 09:15:18 -0600535 * Decompress trailing data in a chunk. The assumption is that read_buffer
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600536 * points at an allocated area holding the contents of a chunk with a
537 * trailing compressed part. What we get back is an allocated area
538 * holding the original prefix part and an uncompressed version of the
539 * trailing part (the malloc area passed in is freed).
540 */
John Bowlerb5d00512012-03-09 09:15:18 -0600541static int
542png_decompress_chunk(png_structrp png_ptr,
543 png_uint_32 chunklength, png_uint_32 prefix_size,
John Bowler0ae4f7b2012-03-03 21:10:26 -0600544 png_alloc_size_t *newlength /* must be initialized to the maximum! */,
545 int terminate /*add a '\0' to the end of the uncompressed data*/)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600546{
John Bowler0ae4f7b2012-03-03 21:10:26 -0600547 /* TODO: implement different limits for different types of chunk.
548 *
549 * The caller supplies *newlength set to the maximum length of the
550 * uncompressed data, but this routine allocates space for the prefix and
551 * maybe a '\0' terminator too. We have to assume that 'prefix_size' is
552 * limited only by the maximum chunk size.
553 */
554 png_alloc_size_t limit = PNG_SIZE_MAX;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600555
John Bowler0ae4f7b2012-03-03 21:10:26 -0600556# ifdef PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED
557 if (png_ptr->user_chunk_malloc_max > 0 &&
558 png_ptr->user_chunk_malloc_max < limit)
559 limit = png_ptr->user_chunk_malloc_max;
560# elif PNG_USER_CHUNK_MALLOC_MAX > 0
561 if (PNG_USER_CHUNK_MALLOC_MAX < limit)
562 limit = PNG_USER_CHUNK_MALLOC_MAX;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600563# endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600564
John Bowler0ae4f7b2012-03-03 21:10:26 -0600565 if (limit >= prefix_size + (terminate != 0))
566 {
John Bowlerb5d00512012-03-09 09:15:18 -0600567 int ret;
568
John Bowler0ae4f7b2012-03-03 21:10:26 -0600569 limit -= prefix_size + (terminate != 0);
570
571 if (limit < *newlength)
572 *newlength = limit;
573
John Bowlerb5d00512012-03-09 09:15:18 -0600574 /* Now try to claim the stream */
575 ret = png_inflate_claim(png_ptr, png_ptr->chunk_name);
576
577 if (ret == Z_OK)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600578 {
John Bowlerb5d00512012-03-09 09:15:18 -0600579 png_uint_32 lzsize = chunklength - prefix_size;
580
581 ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/,
582 /* input: */ png_ptr->read_buffer + prefix_size, &lzsize,
583 /* output: */ NULL, newlength);
584
585 if (ret == Z_STREAM_END)
586 {
587#if 1
588 if (inflateReset(&png_ptr->zstream) == Z_OK)
589#else
590 if (inflateReset2(&png_ptr->zstream, 0/*from stream*/) == Z_OK)
591#endif
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600592 {
John Bowler0ae4f7b2012-03-03 21:10:26 -0600593 /* Because of the limit checks above we know that the new,
594 * expanded, size will fit in a size_t (let alone an
595 * png_alloc_size_t). Use png_malloc_base here to avoid an
596 * extra OOM message.
597 */
598 png_alloc_size_t new_size = *newlength;
John Bowlerb5d00512012-03-09 09:15:18 -0600599 png_alloc_size_t buffer_size = prefix_size + new_size +
600 (terminate != 0);
601 png_bytep text = png_voidcast(png_bytep, png_malloc_base(png_ptr,
602 buffer_size));
John Bowler0ae4f7b2012-03-03 21:10:26 -0600603
John Bowlerb5d00512012-03-09 09:15:18 -0600604 if (text != NULL)
John Bowler0ae4f7b2012-03-03 21:10:26 -0600605 {
John Bowlerb5d00512012-03-09 09:15:18 -0600606 ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/,
607 png_ptr->read_buffer + prefix_size, &lzsize,
608 text + prefix_size, newlength);
609
610 if (ret == Z_STREAM_END)
611 {
John Bowler0ae4f7b2012-03-03 21:10:26 -0600612 if (new_size == *newlength)
613 {
614 if (terminate)
615 text[prefix_size + *newlength] = 0;
616
617 if (prefix_size > 0)
John Bowlerb5d00512012-03-09 09:15:18 -0600618 png_memcpy(text, png_ptr->read_buffer, prefix_size);
John Bowler0ae4f7b2012-03-03 21:10:26 -0600619
John Bowlerb5d00512012-03-09 09:15:18 -0600620 {
621 png_bytep old_ptr = png_ptr->read_buffer;
622
623 png_ptr->read_buffer = text;
624 png_ptr->read_buffer_size = buffer_size;
625 text = old_ptr; /* freed below */
626 }
John Bowler0ae4f7b2012-03-03 21:10:26 -0600627 }
628
John Bowlerb5d00512012-03-09 09:15:18 -0600629 else
630 {
631 /* The size changed on the second read, there can be no
632 * guarantee that anything is correct at this point.
633 * The 'msg' pointer has been set to "unexpected end of
634 * LZ stream", which is fine, but return an error code
635 * that the caller won't accept.
636 */
637 ret = PNG_UNEXPECTED_ZLIB_RETURN;
638 }
639 }
John Bowler0ae4f7b2012-03-03 21:10:26 -0600640
John Bowlerb5d00512012-03-09 09:15:18 -0600641 else if (ret == Z_OK)
642 ret = PNG_UNEXPECTED_ZLIB_RETURN; /* for safety */
John Bowler0ae4f7b2012-03-03 21:10:26 -0600643
John Bowlerb5d00512012-03-09 09:15:18 -0600644 /* Free the text pointer (this is the old read_buffer on
645 * success)
646 */
647 png_free(png_ptr, text);
648
649 /* This really is very benign, but it's still an error because
650 * the extra space may otherwise be used as a Trojan Horse.
651 */
652 if (ret == Z_STREAM_END &&
653 chunklength - prefix_size != lzsize)
654 png_chunk_benign_error(png_ptr, "extra compressed data");
655 }
656
657 else
658 {
659 /* Out of memory allocating the buffer */
660 ret = Z_MEM_ERROR;
661 png_zstream_error(png_ptr, Z_MEM_ERROR);
John Bowler0ae4f7b2012-03-03 21:10:26 -0600662 }
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600663 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600664
John Bowlerb5d00512012-03-09 09:15:18 -0600665 else
666 {
667 /* inflateReset failed, store the error message */
668 png_zstream_error(png_ptr, ret);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600669
John Bowlerb5d00512012-03-09 09:15:18 -0600670 if (ret == Z_STREAM_END)
671 ret = PNG_UNEXPECTED_ZLIB_RETURN;
672 }
673 }
674
675 else if (ret == Z_OK)
676 ret = PNG_UNEXPECTED_ZLIB_RETURN;
677
678 /* Release the claimed stream */
679 png_ptr->zowner = 0;
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600680 }
John Bowlerb5d00512012-03-09 09:15:18 -0600681
682 else /* the claim failed */ if (ret == Z_STREAM_END) /* impossible! */
683 ret = PNG_UNEXPECTED_ZLIB_RETURN;
684
685 return ret;
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600686 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600687
John Bowlerb5d00512012-03-09 09:15:18 -0600688 else
689 {
690 /* Application/configuration limits exceeded */
691 png_zstream_error(png_ptr, Z_MEM_ERROR);
692 return Z_MEM_ERROR;
693 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600694}
Glenn Randers-Pehrson5975f312011-04-01 13:15:36 -0500695#endif /* PNG_READ_COMPRESSED_TEXT_SUPPORTED */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600696
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500697/* Read and check the IDHR chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500698void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600699png_handle_IHDR(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500700{
701 png_byte buf[13];
702 png_uint_32 width, height;
703 int bit_depth, color_type, compression_type, filter_type;
704 int interlace_type;
705
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500706 png_debug(1, "in png_handle_IHDR");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500707
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600708 if (png_ptr->mode & PNG_HAVE_IHDR)
Guy Schalnate5a37791996-06-05 15:50:50 -0500709 png_error(png_ptr, "Out of place IHDR");
710
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500711 /* Check the length */
Guy Schalnat0d580581995-07-20 02:43:20 -0500712 if (length != 13)
Guy Schalnat6d764711995-12-19 03:22:19 -0600713 png_error(png_ptr, "Invalid IHDR chunk");
Guy Schalnat0d580581995-07-20 02:43:20 -0500714
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600715 png_ptr->mode |= PNG_HAVE_IHDR;
716
Guy Schalnat0d580581995-07-20 02:43:20 -0500717 png_crc_read(png_ptr, buf, 13);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600718 png_crc_finish(png_ptr, 0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500719
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500720 width = png_get_uint_31(png_ptr, buf);
721 height = png_get_uint_31(png_ptr, buf + 4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500722 bit_depth = buf[8];
723 color_type = buf[9];
724 compression_type = buf[10];
725 filter_type = buf[11];
726 interlace_type = buf[12];
727
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500728 /* Set internal variables */
Guy Schalnat0d580581995-07-20 02:43:20 -0500729 png_ptr->width = width;
730 png_ptr->height = height;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600731 png_ptr->bit_depth = (png_byte)bit_depth;
732 png_ptr->interlaced = (png_byte)interlace_type;
733 png_ptr->color_type = (png_byte)color_type;
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500734#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600735 png_ptr->filter_type = (png_byte)filter_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500736#endif
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500737 png_ptr->compression_type = (png_byte)compression_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500738
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500739 /* Find number of channels */
Guy Schalnat0d580581995-07-20 02:43:20 -0500740 switch (png_ptr->color_type)
741 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600742 default: /* invalid, png_set_IHDR calls png_error */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500743 case PNG_COLOR_TYPE_GRAY:
744 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnat0d580581995-07-20 02:43:20 -0500745 png_ptr->channels = 1;
746 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500747
Andreas Dilger47a0c421997-05-16 02:46:07 -0500748 case PNG_COLOR_TYPE_RGB:
Guy Schalnat0d580581995-07-20 02:43:20 -0500749 png_ptr->channels = 3;
750 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500751
Andreas Dilger47a0c421997-05-16 02:46:07 -0500752 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnat0d580581995-07-20 02:43:20 -0500753 png_ptr->channels = 2;
754 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500755
Andreas Dilger47a0c421997-05-16 02:46:07 -0500756 case PNG_COLOR_TYPE_RGB_ALPHA:
Guy Schalnat0d580581995-07-20 02:43:20 -0500757 png_ptr->channels = 4;
758 break;
759 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600760
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500761 /* Set up other useful info */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600762 png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth *
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600763 png_ptr->channels);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500764 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width);
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500765 png_debug1(3, "bit_depth = %d", png_ptr->bit_depth);
766 png_debug1(3, "channels = %d", png_ptr->channels);
Glenn Randers-Pehrsonb764c602011-01-14 21:18:37 -0600767 png_debug1(3, "rowbytes = %lu", (unsigned long)png_ptr->rowbytes);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500768 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600769 color_type, interlace_type, compression_type, filter_type);
Guy Schalnat0d580581995-07-20 02:43:20 -0500770}
771
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500772/* Read and check the palette */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500773void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600774png_handle_PLTE(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500775{
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600776 png_color palette[PNG_MAX_PALETTE_LENGTH];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600777 int num, i;
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500778#ifdef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500779 png_colorp pal_ptr;
780#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500781
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500782 png_debug(1, "in png_handle_PLTE");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500783
Guy Schalnate5a37791996-06-05 15:50:50 -0500784 if (!(png_ptr->mode & PNG_HAVE_IHDR))
785 png_error(png_ptr, "Missing IHDR before PLTE");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500786
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600787 else if (png_ptr->mode & PNG_HAVE_IDAT)
788 {
789 png_warning(png_ptr, "Invalid PLTE after IDAT");
790 png_crc_finish(png_ptr, length);
791 return;
792 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500793
Guy Schalnate5a37791996-06-05 15:50:50 -0500794 else if (png_ptr->mode & PNG_HAVE_PLTE)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600795 png_error(png_ptr, "Duplicate PLTE chunk");
796
797 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500798
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500799 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
800 {
801 png_warning(png_ptr,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600802 "Ignoring PLTE chunk in grayscale PNG");
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500803 png_crc_finish(png_ptr, length);
804 return;
805 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600806
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -0500807#ifndef PNG_READ_OPT_PLTE_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500808 if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
809 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600810 png_crc_finish(png_ptr, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500811 return;
812 }
813#endif
814
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600815 if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3)
Guy Schalnate5a37791996-06-05 15:50:50 -0500816 {
817 if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
818 {
819 png_warning(png_ptr, "Invalid palette chunk");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600820 png_crc_finish(png_ptr, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500821 return;
822 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500823
Guy Schalnate5a37791996-06-05 15:50:50 -0500824 else
825 {
826 png_error(png_ptr, "Invalid palette chunk");
827 }
828 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500829
830 num = (int)length / 3;
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500831
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500832#ifdef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500833 for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)
834 {
835 png_byte buf[3];
836
837 png_crc_read(png_ptr, buf, 3);
838 pal_ptr->red = buf[0];
839 pal_ptr->green = buf[1];
840 pal_ptr->blue = buf[2];
841 }
842#else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600843 for (i = 0; i < num; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500844 {
845 png_byte buf[3];
846
847 png_crc_read(png_ptr, buf, 3);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500848 /* Don't depend upon png_color being any order */
Guy Schalnat0d580581995-07-20 02:43:20 -0500849 palette[i].red = buf[0];
850 palette[i].green = buf[1];
851 palette[i].blue = buf[2];
852 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500853#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600854
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600855 /* 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 -0500856 * whatever the normal CRC configuration tells us. However, if we
857 * have an RGB image, the PLTE can be considered ancillary, so
858 * we will act as though it is.
859 */
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -0500860#ifndef PNG_READ_OPT_PLTE_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600861 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600862#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600863 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500864 png_crc_finish(png_ptr, 0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600865 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600866
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -0500867#ifndef PNG_READ_OPT_PLTE_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600868 else if (png_crc_error(png_ptr)) /* Only if we have a CRC error */
869 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600870 /* If we don't want to use the data from an ancillary chunk,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600871 * we have two options: an error abort, or a warning and we
872 * ignore the data in this chunk (which should be OK, since
873 * it's considered ancillary for a RGB or RGBA image).
874 */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600875 if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE))
876 {
877 if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)
878 {
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500879 png_chunk_benign_error(png_ptr, "CRC error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600880 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600881
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600882 else
883 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600884 png_chunk_warning(png_ptr, "CRC error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600885 return;
886 }
887 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600888
Andreas Dilger47a0c421997-05-16 02:46:07 -0500889 /* Otherwise, we (optionally) emit a warning and use the chunk. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600890 else if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN))
891 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600892 png_chunk_warning(png_ptr, "CRC error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600893 }
894 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600895#endif
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500896
Andreas Dilger47a0c421997-05-16 02:46:07 -0500897 png_set_PLTE(png_ptr, info_ptr, palette, num);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500898
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500899#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500900 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
901 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600902 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500903 {
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500904 if (png_ptr->num_trans > (png_uint_16)num)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500905 {
906 png_warning(png_ptr, "Truncating incorrect tRNS chunk length");
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500907 png_ptr->num_trans = (png_uint_16)num;
908 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600909
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500910 if (info_ptr->num_trans > (png_uint_16)num)
911 {
912 png_warning(png_ptr, "Truncating incorrect info tRNS chunk length");
913 info_ptr->num_trans = (png_uint_16)num;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500914 }
915 }
916 }
917#endif
918
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600919}
Guy Schalnate5a37791996-06-05 15:50:50 -0500920
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500921void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600922png_handle_IEND(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600923{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500924 png_debug(1, "in png_handle_IEND");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500925
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600926 if (!(png_ptr->mode & PNG_HAVE_IHDR) || !(png_ptr->mode & PNG_HAVE_IDAT))
927 {
928 png_error(png_ptr, "No image in file");
929 }
930
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600931 png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600932
933 if (length != 0)
934 {
935 png_warning(png_ptr, "Incorrect IEND chunk length");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600936 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600937
Andreas Dilger47a0c421997-05-16 02:46:07 -0500938 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500939
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600940 PNG_UNUSED(info_ptr) /* Quiet compiler warnings about unused info_ptr */
Guy Schalnat0d580581995-07-20 02:43:20 -0500941}
942
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500943#ifdef PNG_READ_gAMA_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500944void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600945png_handle_gAMA(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500946{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600947 png_fixed_point igamma;
Guy Schalnat0d580581995-07-20 02:43:20 -0500948 png_byte buf[4];
949
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500950 png_debug(1, "in png_handle_gAMA");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500951
Guy Schalnate5a37791996-06-05 15:50:50 -0500952 if (!(png_ptr->mode & PNG_HAVE_IHDR))
953 png_error(png_ptr, "Missing IHDR before gAMA");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600954
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600955 else if (png_ptr->mode & PNG_HAVE_IDAT)
956 {
957 png_warning(png_ptr, "Invalid gAMA after IDAT");
958 png_crc_finish(png_ptr, length);
959 return;
960 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600961
Guy Schalnate5a37791996-06-05 15:50:50 -0500962 else if (png_ptr->mode & PNG_HAVE_PLTE)
963 /* Should be an error, but we can cope with it */
964 png_warning(png_ptr, "Out of place gAMA chunk");
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -0600965
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500966 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA)
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500967#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600968 && !(info_ptr->valid & PNG_INFO_sRGB)
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -0600969#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600970 )
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600971 {
972 png_warning(png_ptr, "Duplicate gAMA chunk");
973 png_crc_finish(png_ptr, length);
974 return;
975 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500976
Guy Schalnat0d580581995-07-20 02:43:20 -0500977 if (length != 4)
978 {
Guy Schalnat69b14481996-01-10 02:56:49 -0600979 png_warning(png_ptr, "Incorrect gAMA chunk length");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600980 png_crc_finish(png_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500981 return;
982 }
983
984 png_crc_read(png_ptr, buf, 4);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600985
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600986 if (png_crc_finish(png_ptr, 0))
987 return;
988
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600989 igamma = png_get_fixed_point(NULL, buf);
Glenn Randers-Pehrson33893092010-10-23 13:20:18 -0500990
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600991 /* Check for zero gamma or an error. */
992 if (igamma <= 0)
993 {
994 png_warning(png_ptr,
995 "Ignoring gAMA chunk with out of range gamma");
996
997 return;
998 }
999
1000# ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001001 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB))
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001002 {
John Bowler75156122011-09-09 17:21:44 -05001003 if (PNG_OUT_OF_RANGE(igamma, 45500, 500))
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001004 {
John Bowler88b77cc2011-05-05 06:49:55 -05001005 PNG_WARNING_PARAMETERS(p)
1006 png_warning_parameter_signed(p, 1, PNG_NUMBER_FORMAT_fixed, igamma);
1007 png_formatted_warning(png_ptr, p,
1008 "Ignoring incorrect gAMA value @1 when sRGB is also present");
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001009 return;
1010 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001011 }
1012# endif /* PNG_READ_sRGB_SUPPORTED */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001013
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001014# ifdef PNG_READ_GAMMA_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001015 /* Gamma correction on read is supported. */
1016 png_ptr->gamma = igamma;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001017# endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001018 /* And set the 'info' structure members. */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001019 png_set_gAMA_fixed(png_ptr, info_ptr, igamma);
Guy Schalnat0d580581995-07-20 02:43:20 -05001020}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001021#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001022
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001023#ifdef PNG_READ_sBIT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001024void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001025png_handle_sBIT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001026{
John Bowler0ae4f7b2012-03-03 21:10:26 -06001027 unsigned int truelen;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001028 png_byte buf[4];
Guy Schalnat69b14481996-01-10 02:56:49 -06001029
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001030 png_debug(1, "in png_handle_sBIT");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001031
Guy Schalnat69b14481996-01-10 02:56:49 -06001032 buf[0] = buf[1] = buf[2] = buf[3] = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -05001033
Guy Schalnate5a37791996-06-05 15:50:50 -05001034 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1035 png_error(png_ptr, "Missing IHDR before sBIT");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001036
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001037 else if (png_ptr->mode & PNG_HAVE_IDAT)
1038 {
1039 png_warning(png_ptr, "Invalid sBIT after IDAT");
1040 png_crc_finish(png_ptr, length);
1041 return;
1042 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001043
Guy Schalnate5a37791996-06-05 15:50:50 -05001044 else if (png_ptr->mode & PNG_HAVE_PLTE)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001045 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001046 /* Should be an error, but we can cope with it */
1047 png_warning(png_ptr, "Out of place sBIT chunk");
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001048 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001049
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001050 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001051 {
1052 png_warning(png_ptr, "Duplicate sBIT chunk");
1053 png_crc_finish(png_ptr, length);
1054 return;
1055 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001056
Guy Schalnat0d580581995-07-20 02:43:20 -05001057 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001058 truelen = 3;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001059
Guy Schalnat0d580581995-07-20 02:43:20 -05001060 else
John Bowler0ae4f7b2012-03-03 21:10:26 -06001061 truelen = png_ptr->channels;
Guy Schalnat0d580581995-07-20 02:43:20 -05001062
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001063 if (length != truelen || length > 4)
Guy Schalnat0d580581995-07-20 02:43:20 -05001064 {
Guy Schalnat69b14481996-01-10 02:56:49 -06001065 png_warning(png_ptr, "Incorrect sBIT chunk length");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001066 png_crc_finish(png_ptr, length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001067 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001068 }
1069
Andreas Dilger47a0c421997-05-16 02:46:07 -05001070 png_crc_read(png_ptr, buf, truelen);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001071
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001072 if (png_crc_finish(png_ptr, 0))
1073 return;
1074
Guy Schalnat0d580581995-07-20 02:43:20 -05001075 if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
1076 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001077 png_ptr->sig_bit.red = buf[0];
1078 png_ptr->sig_bit.green = buf[1];
1079 png_ptr->sig_bit.blue = buf[2];
1080 png_ptr->sig_bit.alpha = buf[3];
Guy Schalnat0d580581995-07-20 02:43:20 -05001081 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001082
Guy Schalnat0d580581995-07-20 02:43:20 -05001083 else
1084 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001085 png_ptr->sig_bit.gray = buf[0];
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001086 png_ptr->sig_bit.red = buf[0];
1087 png_ptr->sig_bit.green = buf[0];
1088 png_ptr->sig_bit.blue = buf[0];
Guy Schalnat6d764711995-12-19 03:22:19 -06001089 png_ptr->sig_bit.alpha = buf[1];
Guy Schalnat0d580581995-07-20 02:43:20 -05001090 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001091
Andreas Dilger47a0c421997-05-16 02:46:07 -05001092 png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
Guy Schalnat0d580581995-07-20 02:43:20 -05001093}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001094#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001095
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001096#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001097void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001098png_handle_cHRM(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001099{
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001100 png_byte buf[32];
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001101 png_fixed_point x_white, y_white, x_red, y_red, x_green, y_green, x_blue,
1102 y_blue;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -06001103
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001104 png_debug(1, "in png_handle_cHRM");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001105
Guy Schalnate5a37791996-06-05 15:50:50 -05001106 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001107 png_error(png_ptr, "Missing IHDR before cHRM");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001108
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001109 else if (png_ptr->mode & PNG_HAVE_IDAT)
1110 {
1111 png_warning(png_ptr, "Invalid cHRM after IDAT");
1112 png_crc_finish(png_ptr, length);
1113 return;
1114 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001115
Guy Schalnate5a37791996-06-05 15:50:50 -05001116 else if (png_ptr->mode & PNG_HAVE_PLTE)
1117 /* Should be an error, but we can cope with it */
Glenn Randers-Pehrson93254f62011-10-26 08:36:21 -05001118 png_warning(png_ptr, "Out of place cHRM chunk");
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06001119
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001120 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001121# ifdef PNG_READ_sRGB_SUPPORTED
1122 && !(info_ptr->valid & PNG_INFO_sRGB)
1123# endif
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06001124 )
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001125 {
1126 png_warning(png_ptr, "Duplicate cHRM chunk");
1127 png_crc_finish(png_ptr, length);
1128 return;
1129 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001130
Guy Schalnat0d580581995-07-20 02:43:20 -05001131 if (length != 32)
1132 {
Guy Schalnat69b14481996-01-10 02:56:49 -06001133 png_warning(png_ptr, "Incorrect cHRM chunk length");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001134 png_crc_finish(png_ptr, length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001135 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001136 }
1137
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001138 png_crc_read(png_ptr, buf, 32);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001139
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001140 if (png_crc_finish(png_ptr, 0))
1141 return;
1142
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001143 x_white = png_get_fixed_point(NULL, buf);
1144 y_white = png_get_fixed_point(NULL, buf + 4);
1145 x_red = png_get_fixed_point(NULL, buf + 8);
1146 y_red = png_get_fixed_point(NULL, buf + 12);
1147 x_green = png_get_fixed_point(NULL, buf + 16);
1148 y_green = png_get_fixed_point(NULL, buf + 20);
1149 x_blue = png_get_fixed_point(NULL, buf + 24);
1150 y_blue = png_get_fixed_point(NULL, buf + 28);
Glenn Randers-Pehrson33893092010-10-23 13:20:18 -05001151
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001152 if (x_white == PNG_FIXED_ERROR ||
1153 y_white == PNG_FIXED_ERROR ||
1154 x_red == PNG_FIXED_ERROR ||
1155 y_red == PNG_FIXED_ERROR ||
1156 x_green == PNG_FIXED_ERROR ||
1157 y_green == PNG_FIXED_ERROR ||
1158 x_blue == PNG_FIXED_ERROR ||
1159 y_blue == PNG_FIXED_ERROR)
1160 {
1161 png_warning(png_ptr, "Ignoring cHRM chunk with negative chromaticities");
1162 return;
1163 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001164
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001165#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001166 if ((info_ptr != NULL) && (info_ptr->valid & PNG_INFO_sRGB))
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001167 {
1168 if (PNG_OUT_OF_RANGE(x_white, 31270, 1000) ||
1169 PNG_OUT_OF_RANGE(y_white, 32900, 1000) ||
John Bowler75156122011-09-09 17:21:44 -05001170 PNG_OUT_OF_RANGE(x_red, 64000, 1000) ||
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001171 PNG_OUT_OF_RANGE(y_red, 33000, 1000) ||
1172 PNG_OUT_OF_RANGE(x_green, 30000, 1000) ||
John Bowler75156122011-09-09 17:21:44 -05001173 PNG_OUT_OF_RANGE(y_green, 60000, 1000) ||
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001174 PNG_OUT_OF_RANGE(x_blue, 15000, 1000) ||
1175 PNG_OUT_OF_RANGE(y_blue, 6000, 1000))
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001176 {
John Bowler88b77cc2011-05-05 06:49:55 -05001177 PNG_WARNING_PARAMETERS(p)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001178
John Bowler88b77cc2011-05-05 06:49:55 -05001179 png_warning_parameter_signed(p, 1, PNG_NUMBER_FORMAT_fixed, x_white);
1180 png_warning_parameter_signed(p, 2, PNG_NUMBER_FORMAT_fixed, y_white);
1181 png_warning_parameter_signed(p, 3, PNG_NUMBER_FORMAT_fixed, x_red);
1182 png_warning_parameter_signed(p, 4, PNG_NUMBER_FORMAT_fixed, y_red);
1183 png_warning_parameter_signed(p, 5, PNG_NUMBER_FORMAT_fixed, x_green);
1184 png_warning_parameter_signed(p, 6, PNG_NUMBER_FORMAT_fixed, y_green);
1185 png_warning_parameter_signed(p, 7, PNG_NUMBER_FORMAT_fixed, x_blue);
1186 png_warning_parameter_signed(p, 8, PNG_NUMBER_FORMAT_fixed, y_blue);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001187
John Bowler88b77cc2011-05-05 06:49:55 -05001188 png_formatted_warning(png_ptr, p,
1189 "Ignoring incorrect cHRM white(@1,@2) r(@3,@4)g(@5,@6)b(@7,@8) "
1190 "when sRGB is also present");
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001191 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001192 return;
1193 }
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001194#endif /* PNG_READ_sRGB_SUPPORTED */
1195
John Bowlercb0b2962011-05-12 21:48:29 -05001196#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1197 /* Store the _white values as default coefficients for the rgb to gray
John Bowler736f40f2011-08-25 16:19:44 -05001198 * operation if it is supported. Check if the transform is already set to
1199 * avoid destroying the transform values.
John Bowlercb0b2962011-05-12 21:48:29 -05001200 */
John Bowler736f40f2011-08-25 16:19:44 -05001201 if (!png_ptr->rgb_to_gray_coefficients_set)
John Bowlercb0b2962011-05-12 21:48:29 -05001202 {
John Bowler736f40f2011-08-25 16:19:44 -05001203 /* png_set_background has not been called and we haven't seen an sRGB
1204 * chunk yet. Find the XYZ of the three end points.
John Bowlercb0b2962011-05-12 21:48:29 -05001205 */
John Bowler736f40f2011-08-25 16:19:44 -05001206 png_XYZ XYZ;
1207 png_xy xy;
John Bowlercb0b2962011-05-12 21:48:29 -05001208
John Bowler736f40f2011-08-25 16:19:44 -05001209 xy.redx = x_red;
1210 xy.redy = y_red;
1211 xy.greenx = x_green;
1212 xy.greeny = y_green;
1213 xy.bluex = x_blue;
1214 xy.bluey = y_blue;
1215 xy.whitex = x_white;
1216 xy.whitey = y_white;
1217
1218 if (png_XYZ_from_xy_checked(png_ptr, &XYZ, xy))
1219 {
1220 /* The success case, because XYZ_from_xy normalises to a reference
1221 * white Y of 1.0 we just need to scale the numbers. This should
1222 * always work just fine. It is an internal error if this overflows.
1223 */
1224 {
1225 png_fixed_point r, g, b;
1226 if (png_muldiv(&r, XYZ.redY, 32768, PNG_FP_1) &&
1227 r >= 0 && r <= 32768 &&
1228 png_muldiv(&g, XYZ.greenY, 32768, PNG_FP_1) &&
1229 g >= 0 && g <= 32768 &&
1230 png_muldiv(&b, XYZ.blueY, 32768, PNG_FP_1) &&
1231 b >= 0 && b <= 32768 &&
1232 r+g+b <= 32769)
1233 {
1234 /* We allow 0 coefficients here. r+g+b may be 32769 if two or
1235 * all of the coefficients were rounded up. Handle this by
1236 * reducing the *largest* coefficient by 1; this matches the
1237 * approach used for the default coefficients in pngrtran.c
1238 */
1239 int add = 0;
1240
1241 if (r+g+b > 32768)
1242 add = -1;
1243 else if (r+g+b < 32768)
1244 add = 1;
1245
1246 if (add != 0)
1247 {
1248 if (g >= r && g >= b)
1249 g += add;
1250 else if (r >= g && r >= b)
1251 r += add;
1252 else
1253 b += add;
1254 }
1255
1256 /* Check for an internal error. */
1257 if (r+g+b != 32768)
1258 png_error(png_ptr,
1259 "internal error handling cHRM coefficients");
1260
1261 png_ptr->rgb_to_gray_red_coeff = (png_uint_16)r;
1262 png_ptr->rgb_to_gray_green_coeff = (png_uint_16)g;
1263 }
1264
1265 /* This is a png_error at present even though it could be ignored -
1266 * it should never happen, but it is important that if it does, the
1267 * bug is fixed.
1268 */
1269 else
1270 png_error(png_ptr, "internal error handling cHRM->XYZ");
1271 }
John Bowlercb0b2962011-05-12 21:48:29 -05001272 }
1273 }
1274#endif
1275
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001276 png_set_cHRM_fixed(png_ptr, info_ptr, x_white, y_white, x_red, y_red,
1277 x_green, y_green, x_blue, y_blue);
Guy Schalnat0d580581995-07-20 02:43:20 -05001278}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001279#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001280
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001281#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001282void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001283png_handle_sRGB(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001284{
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001285 int intent;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001286 png_byte buf[1];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001287
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001288 png_debug(1, "in png_handle_sRGB");
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001289
1290 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1291 png_error(png_ptr, "Missing IHDR before sRGB");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001292
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001293 else if (png_ptr->mode & PNG_HAVE_IDAT)
1294 {
1295 png_warning(png_ptr, "Invalid sRGB after IDAT");
1296 png_crc_finish(png_ptr, length);
1297 return;
1298 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001299
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001300 else if (png_ptr->mode & PNG_HAVE_PLTE)
1301 /* Should be an error, but we can cope with it */
1302 png_warning(png_ptr, "Out of place sRGB chunk");
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06001303
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001304 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB))
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001305 {
1306 png_warning(png_ptr, "Duplicate sRGB chunk");
1307 png_crc_finish(png_ptr, length);
1308 return;
1309 }
1310
1311 if (length != 1)
1312 {
1313 png_warning(png_ptr, "Incorrect sRGB chunk length");
1314 png_crc_finish(png_ptr, length);
1315 return;
1316 }
1317
1318 png_crc_read(png_ptr, buf, 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001319
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001320 if (png_crc_finish(png_ptr, 0))
1321 return;
1322
1323 intent = buf[0];
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001324
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001325 /* Check for bad intent */
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001326 if (intent >= PNG_sRGB_INTENT_LAST)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001327 {
1328 png_warning(png_ptr, "Unknown sRGB intent");
1329 return;
1330 }
1331
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001332#if defined(PNG_READ_gAMA_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED)
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001333 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001334 {
John Bowler75156122011-09-09 17:21:44 -05001335 if (PNG_OUT_OF_RANGE(info_ptr->gamma, 45500, 500))
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06001336 {
John Bowler88b77cc2011-05-05 06:49:55 -05001337 PNG_WARNING_PARAMETERS(p)
1338
1339 png_warning_parameter_signed(p, 1, PNG_NUMBER_FORMAT_fixed,
1340 info_ptr->gamma);
1341
1342 png_formatted_warning(png_ptr, p,
1343 "Ignoring incorrect gAMA value @1 when sRGB is also present");
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06001344 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001345 }
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06001346#endif /* PNG_READ_gAMA_SUPPORTED */
1347
1348#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001349 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM))
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001350 if (PNG_OUT_OF_RANGE(info_ptr->x_white, 31270, 1000) ||
1351 PNG_OUT_OF_RANGE(info_ptr->y_white, 32900, 1000) ||
John Bowler75156122011-09-09 17:21:44 -05001352 PNG_OUT_OF_RANGE(info_ptr->x_red, 64000, 1000) ||
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001353 PNG_OUT_OF_RANGE(info_ptr->y_red, 33000, 1000) ||
1354 PNG_OUT_OF_RANGE(info_ptr->x_green, 30000, 1000) ||
John Bowler75156122011-09-09 17:21:44 -05001355 PNG_OUT_OF_RANGE(info_ptr->y_green, 60000, 1000) ||
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001356 PNG_OUT_OF_RANGE(info_ptr->x_blue, 15000, 1000) ||
1357 PNG_OUT_OF_RANGE(info_ptr->y_blue, 6000, 1000))
1358 {
1359 png_warning(png_ptr,
1360 "Ignoring incorrect cHRM value when sRGB is also present");
1361 }
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06001362#endif /* PNG_READ_cHRM_SUPPORTED */
1363
John Bowler736f40f2011-08-25 16:19:44 -05001364 /* This is recorded for use when handling the cHRM chunk above. An sRGB
1365 * chunk unconditionally overwrites the coefficients for grayscale conversion
1366 * too.
1367 */
1368 png_ptr->is_sRGB = 1;
1369
1370# ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1371 /* Don't overwrite user supplied values: */
1372 if (!png_ptr->rgb_to_gray_coefficients_set)
1373 {
1374 /* These numbers come from the sRGB specification (or, since one has to
1375 * pay much money to get a copy, the wikipedia sRGB page) the
1376 * chromaticity values quoted have been inverted to get the reverse
1377 * transformation from RGB to XYZ and the 'Y' coefficients scaled by
1378 * 32768 (then rounded).
1379 *
1380 * sRGB and ITU Rec-709 both truncate the values for the D65 white
1381 * point to four digits and, even though it actually stores five
1382 * digits, the PNG spec gives the truncated value.
1383 *
1384 * This means that when the chromaticities are converted back to XYZ
1385 * end points we end up with (6968,23435,2366), which, as described in
1386 * pngrtran.c, would overflow. If the five digit precision and up is
1387 * used we get, instead:
1388 *
1389 * 6968*R + 23435*G + 2365*B
1390 *
1391 * (Notice that this rounds the blue coefficient down, rather than the
1392 * choice used in pngrtran.c which is to round the green one down.)
1393 */
1394 png_ptr->rgb_to_gray_red_coeff = 6968; /* 0.212639005871510 */
1395 png_ptr->rgb_to_gray_green_coeff = 23434; /* 0.715168678767756 */
1396 /* png_ptr->rgb_to_gray_blue_coeff = 2366; 0.072192315360734 */
1397
1398 /* The following keeps the cHRM chunk from destroying the
1399 * coefficients again in the event that it follows the sRGB chunk.
1400 */
1401 png_ptr->rgb_to_gray_coefficients_set = 1;
1402 }
1403# endif
1404
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06001405 png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, intent);
1406}
1407#endif /* PNG_READ_sRGB_SUPPORTED */
1408
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001409#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001410void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001411png_handle_iCCP(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001412/* Note: this does not properly handle chunks that are > 64K under DOS */
1413{
John Bowler0ae4f7b2012-03-03 21:10:26 -06001414 png_uint_32 keyword_length;
1415 png_const_charp error_message = NULL;
John Bowlerb5d00512012-03-09 09:15:18 -06001416 png_bytep buffer;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001417
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001418 png_debug(1, "in png_handle_iCCP");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001419
1420 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1421 png_error(png_ptr, "Missing IHDR before iCCP");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001422
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001423 else if (png_ptr->mode & PNG_HAVE_IDAT)
1424 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001425 png_crc_finish(png_ptr, length);
John Bowler0ae4f7b2012-03-03 21:10:26 -06001426 png_chunk_benign_error(png_ptr, "Invalid iCCP after IDAT");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001427 return;
1428 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001429
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001430 else if (png_ptr->mode & PNG_HAVE_PLTE)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001431 {
John Bowler0ae4f7b2012-03-03 21:10:26 -06001432 /* Ignore out-of-place iCCP chunks because other implementations may
1433 * *have* to do so, so it is misleading if libpng handles them.
1434 */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001435 png_crc_finish(png_ptr, length);
John Bowler0ae4f7b2012-03-03 21:10:26 -06001436 png_chunk_benign_error(png_ptr, "Out of place iCCP chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001437 return;
1438 }
1439
Glenn Randers-Pehrson6038b802012-03-08 10:40:07 -06001440 if ((png_ptr->mode & PNG_HAVE_iCCP) || (info_ptr != NULL &&
1441 (info_ptr->valid & (PNG_INFO_iCCP|PNG_INFO_sRGB))))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001442 {
John Bowler0ae4f7b2012-03-03 21:10:26 -06001443 png_crc_finish(png_ptr, length);
1444 png_chunk_benign_error(png_ptr, "Duplicate color profile");
1445 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001446 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001447
Glenn Randers-Pehrson6038b802012-03-08 10:40:07 -06001448 png_ptr->mode |= PNG_HAVE_iCCP;
1449
John Bowler0ae4f7b2012-03-03 21:10:26 -06001450 /* TODO: read the chunk in pieces, validating it as we go. */
John Bowlerb5d00512012-03-09 09:15:18 -06001451 buffer = png_read_buffer(png_ptr, length, 0/*!warn*/);
1452
1453 png_crc_read(png_ptr, buffer, length);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001454
John Bowler0ae4f7b2012-03-03 21:10:26 -06001455 if (png_crc_finish(png_ptr, 0))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001456 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001457
John Bowler0ae4f7b2012-03-03 21:10:26 -06001458 /* TODO: also check that the keyword contents match the spec! */
1459 for (keyword_length = 0;
John Bowlerb5d00512012-03-09 09:15:18 -06001460 keyword_length < length && buffer[keyword_length] != 0;
John Bowler0ae4f7b2012-03-03 21:10:26 -06001461 ++keyword_length)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001462 /* Empty loop to find end of name */ ;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001463
John Bowler0ae4f7b2012-03-03 21:10:26 -06001464 if (keyword_length > 79 || keyword_length < 1)
1465 {
John Bowler0ae4f7b2012-03-03 21:10:26 -06001466 png_chunk_benign_error(png_ptr, "Bad iCCP keyword");
1467 return;
1468 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001469
John Bowler0ae4f7b2012-03-03 21:10:26 -06001470 /* There should be at least one zero (the compression type byte) following
1471 * the separator followed by some LZ data; check for at least one byte.
1472 * Since a chunk length is no more than 2^31 the addition below is safe.
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001473 */
John Bowler0ae4f7b2012-03-03 21:10:26 -06001474 if (keyword_length + 3 >= length)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001475 {
John Bowler0ae4f7b2012-03-03 21:10:26 -06001476 png_chunk_benign_error(png_ptr, "iCCP chunk too short");
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -05001477 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001478 }
1479
John Bowler0ae4f7b2012-03-03 21:10:26 -06001480 /* We only understand '0' compression - deflate - so if we get a different
1481 * value we can't safely decode the chunk.
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001482 */
John Bowlerb5d00512012-03-09 09:15:18 -06001483 if (buffer[keyword_length+1] != PNG_COMPRESSION_TYPE_BASE)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001484 {
John Bowler0ae4f7b2012-03-03 21:10:26 -06001485 png_chunk_benign_error(png_ptr, "Bad compression type in iCCP chunk");
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001486 return;
1487 }
1488
John Bowler0ae4f7b2012-03-03 21:10:26 -06001489 {
1490 png_alloc_size_t uncompressed_length;
1491
1492 /* TODO: this is slightly broken, an ICC profile can be up to 2^32-1 bytes
1493 * in length, but it is stored here with the keyword and the compression
1494 * type prefixed to it. This means that on 32-bit systems the code can't
1495 * accept, or express, the maximum length.
1496 */
1497 if (PNG_SIZE_MAX > 0xffffffffU/*ICC profile maximum*/)
1498 uncompressed_length = 0xffffffffU;
1499
1500 else
1501 uncompressed_length = PNG_SIZE_MAX;
1502
1503 /* TODO: at present png_decompress_chunk imposes a single application
1504 * level memory limit, this should be split to different values for iCCP
1505 * and text chunks.
1506 */
John Bowlerb5d00512012-03-09 09:15:18 -06001507 if (png_decompress_chunk(png_ptr, length, keyword_length+2,
1508 &uncompressed_length, 0/*do not terminate*/) == Z_STREAM_END)
John Bowler0ae4f7b2012-03-03 21:10:26 -06001509 {
John Bowlerb5d00512012-03-09 09:15:18 -06001510 /* It worked; png_ptr->read_buffer now contains the full uncompressed
1511 * ICC profile. The cast below is safe because of the checks above,
1512 * the final uncompressed length can be at most 2^32-1
John Bowler0ae4f7b2012-03-03 21:10:26 -06001513 */
1514 png_uint_32 profile_length = (png_uint_32)uncompressed_length;
John Bowlerb5d00512012-03-09 09:15:18 -06001515 png_bytep profile = png_ptr->read_buffer + (keyword_length+2);
John Bowler0ae4f7b2012-03-03 21:10:26 -06001516
1517 /* Now perform some basic checks.
1518 *
1519 * TODO: do a lot more checks, the format is pretty simple, at least
1520 * check that the whole tag table is there.
1521 */
1522 if (profile_length > 132 && png_get_uint_32(profile) == profile_length)
John Bowlerb5d00512012-03-09 09:15:18 -06001523 png_set_iCCP(png_ptr, info_ptr, (png_charp)png_ptr->read_buffer,
1524 PNG_COMPRESSION_TYPE_BASE, profile, profile_length);
John Bowler0ae4f7b2012-03-03 21:10:26 -06001525
1526 else
1527 error_message = "Malformed ICC profile";
1528 }
John Bowler0ae4f7b2012-03-03 21:10:26 -06001529
John Bowlerb5d00512012-03-09 09:15:18 -06001530 else
1531 error_message = png_ptr->zstream.msg;
1532 }
John Bowler0ae4f7b2012-03-03 21:10:26 -06001533
1534 if (error_message != NULL)
1535 png_chunk_benign_error(png_ptr, error_message);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001536}
1537#endif /* PNG_READ_iCCP_SUPPORTED */
1538
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001539#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001540void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001541png_handle_sPLT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001542/* Note: this does not properly handle chunks that are > 64K under DOS */
1543{
John Bowlerb5d00512012-03-09 09:15:18 -06001544 png_bytep entry_start, buffer;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001545 png_sPLT_t new_palette;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001546 png_sPLT_entryp pp;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001547 png_uint_32 data_length;
1548 int entry_size, i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001549 png_uint_32 skip = 0;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001550 png_uint_32 dl;
1551 png_size_t max_dl;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001552
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001553 png_debug(1, "in png_handle_sPLT");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001554
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001555#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson7824a702009-06-13 10:05:05 -05001556
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001557 if (png_ptr->user_chunk_cache_max != 0)
1558 {
1559 if (png_ptr->user_chunk_cache_max == 1)
1560 {
1561 png_crc_finish(png_ptr, length);
1562 return;
1563 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001564
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001565 if (--png_ptr->user_chunk_cache_max == 1)
1566 {
1567 png_warning(png_ptr, "No space in chunk cache for sPLT");
1568 png_crc_finish(png_ptr, length);
1569 return;
1570 }
1571 }
1572#endif
1573
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001574 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1575 png_error(png_ptr, "Missing IHDR before sPLT");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001576
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001577 else if (png_ptr->mode & PNG_HAVE_IDAT)
1578 {
1579 png_warning(png_ptr, "Invalid sPLT after IDAT");
1580 png_crc_finish(png_ptr, length);
1581 return;
1582 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001583
1584#ifdef PNG_MAX_MALLOC_64K
1585 if (length > (png_uint_32)65535L)
1586 {
1587 png_warning(png_ptr, "sPLT chunk too large to fit in memory");
1588 skip = length - (png_uint_32)65535L;
1589 length = (png_uint_32)65535L;
1590 }
1591#endif
1592
John Bowlerb5d00512012-03-09 09:15:18 -06001593 buffer = png_read_buffer(png_ptr, length+1, 0/*!warn*/);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001594
1595 /* WARNING: this may break if size_t is less than 32 bits; it is assumed
1596 * that the PNG_MAX_MALLOC_64K test is enabled in this case, but this is a
1597 * potential breakage point if the types in pngconf.h aren't exactly right.
1598 */
John Bowlerb5d00512012-03-09 09:15:18 -06001599 png_crc_read(png_ptr, buffer, length);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001600
1601 if (png_crc_finish(png_ptr, skip))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001602 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001603
John Bowlerb5d00512012-03-09 09:15:18 -06001604 buffer[length] = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001605
John Bowlerb5d00512012-03-09 09:15:18 -06001606 for (entry_start = buffer; *entry_start; entry_start++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001607 /* Empty loop to find end of name */ ;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001608
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001609 ++entry_start;
1610
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001611 /* A sample depth should follow the separator, and we should be on it */
John Bowlerb5d00512012-03-09 09:15:18 -06001612 if (entry_start > buffer + length - 2)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001613 {
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -05001614 png_warning(png_ptr, "malformed sPLT chunk");
1615 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001616 }
1617
1618 new_palette.depth = *entry_start++;
Glenn Randers-Pehrsona565f0e2010-03-06 08:24:45 -06001619 entry_size = (new_palette.depth == 8 ? 6 : 10);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001620 /* This must fit in a png_uint_32 because it is derived from the original
John Bowlerb5d00512012-03-09 09:15:18 -06001621 * chunk data length.
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001622 */
John Bowlerb5d00512012-03-09 09:15:18 -06001623 data_length = length - (png_uint_32)(entry_start - buffer);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001624
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001625 /* Integrity-check the data length */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001626 if (data_length % entry_size)
1627 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001628 png_warning(png_ptr, "sPLT chunk has bad length");
1629 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001630 }
1631
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001632 dl = (png_int_32)(data_length / entry_size);
1633 max_dl = PNG_SIZE_MAX / png_sizeof(png_sPLT_entry);
1634
1635 if (dl > max_dl)
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001636 {
1637 png_warning(png_ptr, "sPLT chunk too long");
1638 return;
1639 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001640
1641 new_palette.nentries = (png_int_32)(data_length / entry_size);
1642
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001643 new_palette.entries = (png_sPLT_entryp)png_malloc_warn(
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001644 png_ptr, new_palette.nentries * png_sizeof(png_sPLT_entry));
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001645
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001646 if (new_palette.entries == NULL)
1647 {
1648 png_warning(png_ptr, "sPLT chunk requires too much memory");
1649 return;
1650 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001651
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001652#ifdef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001653 for (i = 0; i < new_palette.nentries; i++)
1654 {
Glenn Randers-Pehrson90b878c2009-10-07 12:44:35 -05001655 pp = new_palette.entries + i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001656
1657 if (new_palette.depth == 8)
1658 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001659 pp->red = *entry_start++;
1660 pp->green = *entry_start++;
1661 pp->blue = *entry_start++;
1662 pp->alpha = *entry_start++;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001663 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001664
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001665 else
1666 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001667 pp->red = png_get_uint_16(entry_start); entry_start += 2;
1668 pp->green = png_get_uint_16(entry_start); entry_start += 2;
1669 pp->blue = png_get_uint_16(entry_start); entry_start += 2;
1670 pp->alpha = png_get_uint_16(entry_start); entry_start += 2;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001671 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001672
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001673 pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
1674 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001675#else
1676 pp = new_palette.entries;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001677
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001678 for (i = 0; i < new_palette.nentries; i++)
1679 {
1680
1681 if (new_palette.depth == 8)
1682 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001683 pp[i].red = *entry_start++;
1684 pp[i].green = *entry_start++;
1685 pp[i].blue = *entry_start++;
1686 pp[i].alpha = *entry_start++;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001687 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001688
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001689 else
1690 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001691 pp[i].red = png_get_uint_16(entry_start); entry_start += 2;
1692 pp[i].green = png_get_uint_16(entry_start); entry_start += 2;
1693 pp[i].blue = png_get_uint_16(entry_start); entry_start += 2;
1694 pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001695 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001696
Glenn Randers-Pehrsonf27592a2011-03-21 18:05:40 -05001697 pp[i].frequency = png_get_uint_16(entry_start); entry_start += 2;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001698 }
1699#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001700
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001701 /* Discard all chunk data except the name and stash that */
John Bowlerb5d00512012-03-09 09:15:18 -06001702 new_palette.name = (png_charp)buffer;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001703
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001704 png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001705
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001706 png_free(png_ptr, new_palette.entries);
1707}
1708#endif /* PNG_READ_sPLT_SUPPORTED */
1709
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001710#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001711void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001712png_handle_tRNS(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001713{
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001714 png_byte readbuf[PNG_MAX_PALETTE_LENGTH];
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06001715
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001716 png_debug(1, "in png_handle_tRNS");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001717
Guy Schalnate5a37791996-06-05 15:50:50 -05001718 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1719 png_error(png_ptr, "Missing IHDR before tRNS");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001720
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001721 else if (png_ptr->mode & PNG_HAVE_IDAT)
1722 {
1723 png_warning(png_ptr, "Invalid tRNS after IDAT");
1724 png_crc_finish(png_ptr, length);
1725 return;
1726 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001727
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001728 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001729 {
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001730 png_warning(png_ptr, "Duplicate tRNS chunk");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001731 png_crc_finish(png_ptr, length);
1732 return;
1733 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001734
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001735 if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
Guy Schalnat0d580581995-07-20 02:43:20 -05001736 {
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001737 png_byte buf[2];
Guy Schalnat0d580581995-07-20 02:43:20 -05001738
1739 if (length != 2)
1740 {
Guy Schalnat69b14481996-01-10 02:56:49 -06001741 png_warning(png_ptr, "Incorrect tRNS chunk length");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001742 png_crc_finish(png_ptr, length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001743 return;
1744 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001745
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001746 png_crc_read(png_ptr, buf, 2);
1747 png_ptr->num_trans = 1;
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -05001748 png_ptr->trans_color.gray = png_get_uint_16(buf);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001749 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001750
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001751 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
1752 {
1753 png_byte buf[6];
1754
1755 if (length != 6)
1756 {
1757 png_warning(png_ptr, "Incorrect tRNS chunk length");
1758 png_crc_finish(png_ptr, length);
1759 return;
1760 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001761
John Bowler0ae4f7b2012-03-03 21:10:26 -06001762 png_crc_read(png_ptr, buf, length);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001763 png_ptr->num_trans = 1;
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -05001764 png_ptr->trans_color.red = png_get_uint_16(buf);
1765 png_ptr->trans_color.green = png_get_uint_16(buf + 2);
1766 png_ptr->trans_color.blue = png_get_uint_16(buf + 4);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001767 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001768
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001769 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1770 {
1771 if (!(png_ptr->mode & PNG_HAVE_PLTE))
1772 {
1773 /* Should be an error, but we can cope with it. */
1774 png_warning(png_ptr, "Missing PLTE before tRNS");
1775 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001776
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001777 if (length > (png_uint_32)png_ptr->num_palette ||
1778 length > PNG_MAX_PALETTE_LENGTH)
1779 {
1780 png_warning(png_ptr, "Incorrect tRNS chunk length");
1781 png_crc_finish(png_ptr, length);
1782 return;
1783 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001784
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001785 if (length == 0)
1786 {
1787 png_warning(png_ptr, "Zero length tRNS chunk");
1788 png_crc_finish(png_ptr, length);
1789 return;
1790 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001791
John Bowler0ae4f7b2012-03-03 21:10:26 -06001792 png_crc_read(png_ptr, readbuf, length);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001793 png_ptr->num_trans = (png_uint_16)length;
1794 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001795
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001796 else
Guy Schalnate5a37791996-06-05 15:50:50 -05001797 {
1798 png_warning(png_ptr, "tRNS chunk not allowed with alpha channel");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001799 png_crc_finish(png_ptr, length);
Guy Schalnate5a37791996-06-05 15:50:50 -05001800 return;
1801 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001802
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001803 if (png_crc_finish(png_ptr, 0))
Glenn Randers-Pehrsona7dbcba2007-05-15 16:16:34 -05001804 {
1805 png_ptr->num_trans = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001806 return;
Glenn Randers-Pehrsona7dbcba2007-05-15 16:16:34 -05001807 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001808
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06001809 png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001810 &(png_ptr->trans_color));
Guy Schalnat0d580581995-07-20 02:43:20 -05001811}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001812#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001813
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001814#ifdef PNG_READ_bKGD_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001815void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001816png_handle_bKGD(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001817{
John Bowler0ae4f7b2012-03-03 21:10:26 -06001818 unsigned int truelen;
Guy Schalnat0d580581995-07-20 02:43:20 -05001819 png_byte buf[6];
John Bowlercb0b2962011-05-12 21:48:29 -05001820 png_color_16 background;
Guy Schalnat0d580581995-07-20 02:43:20 -05001821
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001822 png_debug(1, "in png_handle_bKGD");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001823
Guy Schalnate5a37791996-06-05 15:50:50 -05001824 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1825 png_error(png_ptr, "Missing IHDR before bKGD");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001826
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001827 else if (png_ptr->mode & PNG_HAVE_IDAT)
1828 {
1829 png_warning(png_ptr, "Invalid bKGD after IDAT");
1830 png_crc_finish(png_ptr, length);
1831 return;
1832 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001833
Guy Schalnate5a37791996-06-05 15:50:50 -05001834 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001835 !(png_ptr->mode & PNG_HAVE_PLTE))
Guy Schalnate5a37791996-06-05 15:50:50 -05001836 {
1837 png_warning(png_ptr, "Missing PLTE before bKGD");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001838 png_crc_finish(png_ptr, length);
1839 return;
1840 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001841
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001842 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001843 {
1844 png_warning(png_ptr, "Duplicate bKGD chunk");
1845 png_crc_finish(png_ptr, length);
Guy Schalnate5a37791996-06-05 15:50:50 -05001846 return;
1847 }
1848
Guy Schalnat0d580581995-07-20 02:43:20 -05001849 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1850 truelen = 1;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001851
Guy Schalnat0d580581995-07-20 02:43:20 -05001852 else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
1853 truelen = 6;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001854
Guy Schalnat0d580581995-07-20 02:43:20 -05001855 else
1856 truelen = 2;
1857
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001858 if (length != truelen)
Guy Schalnat0d580581995-07-20 02:43:20 -05001859 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001860 png_warning(png_ptr, "Incorrect bKGD chunk length");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001861 png_crc_finish(png_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -05001862 return;
1863 }
1864
Andreas Dilger47a0c421997-05-16 02:46:07 -05001865 png_crc_read(png_ptr, buf, truelen);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001866
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001867 if (png_crc_finish(png_ptr, 0))
1868 return;
1869
Guy Schalnate5a37791996-06-05 15:50:50 -05001870 /* We convert the index value into RGB components so that we can allow
1871 * arbitrary RGB values for background when we have transparency, and
1872 * so it is easy to determine the RGB values of the background color
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001873 * from the info_ptr struct.
1874 */
Guy Schalnat0d580581995-07-20 02:43:20 -05001875 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Guy Schalnate5a37791996-06-05 15:50:50 -05001876 {
John Bowlercb0b2962011-05-12 21:48:29 -05001877 background.index = buf[0];
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001878
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001879 if (info_ptr && info_ptr->num_palette)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001880 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001881 if (buf[0] >= info_ptr->num_palette)
1882 {
1883 png_warning(png_ptr, "Incorrect bKGD chunk index value");
1884 return;
1885 }
1886
John Bowlercb0b2962011-05-12 21:48:29 -05001887 background.red = (png_uint_16)png_ptr->palette[buf[0]].red;
1888 background.green = (png_uint_16)png_ptr->palette[buf[0]].green;
1889 background.blue = (png_uint_16)png_ptr->palette[buf[0]].blue;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001890 }
John Bowlercb0b2962011-05-12 21:48:29 -05001891
1892 else
1893 background.red = background.green = background.blue = 0;
1894
1895 background.gray = 0;
Guy Schalnate5a37791996-06-05 15:50:50 -05001896 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001897
Andreas Dilger47a0c421997-05-16 02:46:07 -05001898 else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) /* GRAY */
Guy Schalnate5a37791996-06-05 15:50:50 -05001899 {
John Bowlercb0b2962011-05-12 21:48:29 -05001900 background.index = 0;
1901 background.red =
1902 background.green =
1903 background.blue =
1904 background.gray = png_get_uint_16(buf);
Guy Schalnate5a37791996-06-05 15:50:50 -05001905 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001906
Guy Schalnat0d580581995-07-20 02:43:20 -05001907 else
1908 {
John Bowlercb0b2962011-05-12 21:48:29 -05001909 background.index = 0;
1910 background.red = png_get_uint_16(buf);
1911 background.green = png_get_uint_16(buf + 2);
1912 background.blue = png_get_uint_16(buf + 4);
1913 background.gray = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -05001914 }
1915
John Bowlercb0b2962011-05-12 21:48:29 -05001916 png_set_bKGD(png_ptr, info_ptr, &background);
Guy Schalnat0d580581995-07-20 02:43:20 -05001917}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001918#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001919
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001920#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001921void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001922png_handle_hIST(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001923{
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001924 unsigned int num, i;
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001925 png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];
Guy Schalnat0d580581995-07-20 02:43:20 -05001926
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001927 png_debug(1, "in png_handle_hIST");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001928
Guy Schalnate5a37791996-06-05 15:50:50 -05001929 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1930 png_error(png_ptr, "Missing IHDR before hIST");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001931
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001932 else if (png_ptr->mode & PNG_HAVE_IDAT)
1933 {
1934 png_warning(png_ptr, "Invalid hIST after IDAT");
1935 png_crc_finish(png_ptr, length);
1936 return;
1937 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001938
Guy Schalnate5a37791996-06-05 15:50:50 -05001939 else if (!(png_ptr->mode & PNG_HAVE_PLTE))
1940 {
1941 png_warning(png_ptr, "Missing PLTE before hIST");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001942 png_crc_finish(png_ptr, length);
1943 return;
1944 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001945
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001946 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001947 {
1948 png_warning(png_ptr, "Duplicate hIST chunk");
1949 png_crc_finish(png_ptr, length);
Guy Schalnate5a37791996-06-05 15:50:50 -05001950 return;
1951 }
1952
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001953 num = length / 2 ;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001954
1955 if (num != (unsigned int)png_ptr->num_palette || num >
1956 (unsigned int)PNG_MAX_PALETTE_LENGTH)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001957 {
1958 png_warning(png_ptr, "Incorrect hIST chunk length");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001959 png_crc_finish(png_ptr, length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001960 return;
1961 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001962
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001963 for (i = 0; i < num; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001964 {
1965 png_byte buf[2];
1966
1967 png_crc_read(png_ptr, buf, 2);
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06001968 readbuf[i] = png_get_uint_16(buf);
Guy Schalnat0d580581995-07-20 02:43:20 -05001969 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001970
1971 if (png_crc_finish(png_ptr, 0))
1972 return;
1973
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06001974 png_set_hIST(png_ptr, info_ptr, readbuf);
Guy Schalnat0d580581995-07-20 02:43:20 -05001975}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001976#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001977
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001978#ifdef PNG_READ_pHYs_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001979void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001980png_handle_pHYs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001981{
1982 png_byte buf[9];
1983 png_uint_32 res_x, res_y;
1984 int unit_type;
1985
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001986 png_debug(1, "in png_handle_pHYs");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001987
Guy Schalnate5a37791996-06-05 15:50:50 -05001988 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001989 png_error(png_ptr, "Missing IHDR before pHYs");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001990
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001991 else if (png_ptr->mode & PNG_HAVE_IDAT)
1992 {
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001993 png_warning(png_ptr, "Invalid pHYs after IDAT");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001994 png_crc_finish(png_ptr, length);
1995 return;
1996 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001997
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001998 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001999 {
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05002000 png_warning(png_ptr, "Duplicate pHYs chunk");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002001 png_crc_finish(png_ptr, length);
2002 return;
2003 }
Guy Schalnate5a37791996-06-05 15:50:50 -05002004
Guy Schalnat0d580581995-07-20 02:43:20 -05002005 if (length != 9)
2006 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002007 png_warning(png_ptr, "Incorrect pHYs chunk length");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002008 png_crc_finish(png_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -05002009 return;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002010 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002011
2012 png_crc_read(png_ptr, buf, 9);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002013
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002014 if (png_crc_finish(png_ptr, 0))
2015 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05002016
2017 res_x = png_get_uint_32(buf);
2018 res_y = png_get_uint_32(buf + 4);
2019 unit_type = buf[8];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002020 png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
Guy Schalnat0d580581995-07-20 02:43:20 -05002021}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002022#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002023
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002024#ifdef PNG_READ_oFFs_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002025void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002026png_handle_oFFs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002027{
2028 png_byte buf[9];
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002029 png_int_32 offset_x, offset_y;
Guy Schalnat0d580581995-07-20 02:43:20 -05002030 int unit_type;
2031
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002032 png_debug(1, "in png_handle_oFFs");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002033
Guy Schalnate5a37791996-06-05 15:50:50 -05002034 if (!(png_ptr->mode & PNG_HAVE_IHDR))
2035 png_error(png_ptr, "Missing IHDR before oFFs");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002036
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002037 else if (png_ptr->mode & PNG_HAVE_IDAT)
2038 {
2039 png_warning(png_ptr, "Invalid oFFs after IDAT");
2040 png_crc_finish(png_ptr, length);
2041 return;
2042 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002043
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002044 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002045 {
2046 png_warning(png_ptr, "Duplicate oFFs chunk");
2047 png_crc_finish(png_ptr, length);
2048 return;
2049 }
Guy Schalnate5a37791996-06-05 15:50:50 -05002050
Guy Schalnat0d580581995-07-20 02:43:20 -05002051 if (length != 9)
2052 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002053 png_warning(png_ptr, "Incorrect oFFs chunk length");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002054 png_crc_finish(png_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -05002055 return;
2056 }
2057
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002058 png_crc_read(png_ptr, buf, 9);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002059
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002060 if (png_crc_finish(png_ptr, 0))
2061 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05002062
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002063 offset_x = png_get_int_32(buf);
2064 offset_y = png_get_int_32(buf + 4);
Guy Schalnat0d580581995-07-20 02:43:20 -05002065 unit_type = buf[8];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002066 png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
2067}
2068#endif
2069
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002070#ifdef PNG_READ_pCAL_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002071/* Read the pCAL chunk (described in the PNG Extensions document) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002072void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002073png_handle_pCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002074{
Andreas Dilger47a0c421997-05-16 02:46:07 -05002075 png_int_32 X0, X1;
2076 png_byte type, nparams;
John Bowlerb5d00512012-03-09 09:15:18 -06002077 png_bytep buffer, buf, units, endptr;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002078 png_charpp params;
2079 int i;
2080
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002081 png_debug(1, "in png_handle_pCAL");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002082
2083 if (!(png_ptr->mode & PNG_HAVE_IHDR))
2084 png_error(png_ptr, "Missing IHDR before pCAL");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002085
Andreas Dilger47a0c421997-05-16 02:46:07 -05002086 else if (png_ptr->mode & PNG_HAVE_IDAT)
2087 {
2088 png_warning(png_ptr, "Invalid pCAL after IDAT");
2089 png_crc_finish(png_ptr, length);
2090 return;
2091 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002092
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002093 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL))
Andreas Dilger47a0c421997-05-16 02:46:07 -05002094 {
2095 png_warning(png_ptr, "Duplicate pCAL chunk");
2096 png_crc_finish(png_ptr, length);
2097 return;
2098 }
2099
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002100 png_debug1(2, "Allocating and reading pCAL chunk data (%u bytes)",
2101 length + 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002102
John Bowlerb5d00512012-03-09 09:15:18 -06002103 buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
2104
2105 if (buffer == NULL)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002106 {
2107 png_warning(png_ptr, "No memory for pCAL purpose");
2108 return;
2109 }
2110
John Bowlerb5d00512012-03-09 09:15:18 -06002111 png_crc_read(png_ptr, buffer, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002112
2113 if (png_crc_finish(png_ptr, 0))
Andreas Dilger47a0c421997-05-16 02:46:07 -05002114 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002115
John Bowlerb5d00512012-03-09 09:15:18 -06002116 buffer[length] = 0; /* Null terminate the last string */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002117
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002118 png_debug(3, "Finding end of pCAL purpose string");
John Bowlerb5d00512012-03-09 09:15:18 -06002119 for (buf = buffer; *buf; buf++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002120 /* Empty loop */ ;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002121
John Bowlerb5d00512012-03-09 09:15:18 -06002122 endptr = buffer + length;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002123
2124 /* We need to have at least 12 bytes after the purpose string
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002125 * in order to get the parameter information.
2126 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002127 if (endptr <= buf + 12)
2128 {
2129 png_warning(png_ptr, "Invalid pCAL data");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002130 return;
2131 }
2132
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002133 png_debug(3, "Reading pCAL X0, X1, type, nparams, and units");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002134 X0 = png_get_int_32((png_bytep)buf+1);
2135 X1 = png_get_int_32((png_bytep)buf+5);
2136 type = buf[9];
2137 nparams = buf[10];
2138 units = buf + 11;
2139
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002140 png_debug(3, "Checking pCAL equation type and number of parameters");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002141 /* Check that we have the right number of parameters for known
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002142 * equation types.
2143 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002144 if ((type == PNG_EQUATION_LINEAR && nparams != 2) ||
2145 (type == PNG_EQUATION_BASE_E && nparams != 3) ||
2146 (type == PNG_EQUATION_ARBITRARY && nparams != 3) ||
2147 (type == PNG_EQUATION_HYPERBOLIC && nparams != 4))
2148 {
2149 png_warning(png_ptr, "Invalid pCAL parameters for equation type");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002150 return;
2151 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002152
Andreas Dilger47a0c421997-05-16 02:46:07 -05002153 else if (type >= PNG_EQUATION_LAST)
2154 {
2155 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
2156 }
2157
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002158 for (buf = units; *buf; buf++)
Glenn Randers-Pehrsonf9f2fe01998-03-15 18:20:23 -06002159 /* Empty loop to move past the units string. */ ;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002160
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002161 png_debug(3, "Allocating pCAL parameters array");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002162
John Bowlerb5d00512012-03-09 09:15:18 -06002163 params = png_voidcast(png_charpp, png_malloc_warn(png_ptr,
2164 nparams * png_sizeof(png_charp)));
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002165
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002166 if (params == NULL)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002167 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002168 png_warning(png_ptr, "No memory for pCAL params");
2169 return;
2170 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002171
2172 /* Get pointers to the start of each parameter string. */
John Bowlerb5d00512012-03-09 09:15:18 -06002173 for (i = 0; i < nparams; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002174 {
2175 buf++; /* Skip the null string terminator from previous parameter. */
2176
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002177 png_debug1(3, "Reading pCAL parameter %d", i);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002178
John Bowlerb5d00512012-03-09 09:15:18 -06002179 for (params[i] = (png_charp)buf; buf <= endptr && *buf != 0; buf++)
Glenn Randers-Pehrsonf9f2fe01998-03-15 18:20:23 -06002180 /* Empty loop to move past each parameter string */ ;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002181
2182 /* Make sure we haven't run out of data yet */
2183 if (buf > endptr)
2184 {
2185 png_warning(png_ptr, "Invalid pCAL data");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002186 png_free(png_ptr, params);
2187 return;
2188 }
2189 }
2190
John Bowlerb5d00512012-03-09 09:15:18 -06002191 png_set_pCAL(png_ptr, info_ptr, (png_charp)buffer, X0, X1, type, nparams,
2192 (png_charp)units, params);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002193
Andreas Dilger47a0c421997-05-16 02:46:07 -05002194 png_free(png_ptr, params);
Guy Schalnat0d580581995-07-20 02:43:20 -05002195}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002196#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002197
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002198#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002199/* Read the sCAL chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002200void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002201png_handle_sCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002202{
John Bowlerb5d00512012-03-09 09:15:18 -06002203 png_bytep buffer;
2204 png_size_t i;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002205 int state;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002206
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002207 png_debug(1, "in png_handle_sCAL");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002208
2209 if (!(png_ptr->mode & PNG_HAVE_IHDR))
2210 png_error(png_ptr, "Missing IHDR before sCAL");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002211
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002212 else if (png_ptr->mode & PNG_HAVE_IDAT)
2213 {
2214 png_warning(png_ptr, "Invalid sCAL after IDAT");
2215 png_crc_finish(png_ptr, length);
2216 return;
2217 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002218
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002219 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002220 {
2221 png_warning(png_ptr, "Duplicate sCAL chunk");
2222 png_crc_finish(png_ptr, length);
2223 return;
2224 }
2225
Glenn Randers-Pehrson254a5132011-06-10 17:45:48 -05002226 /* Need unit type, width, \0, height: minimum 4 bytes */
2227 else if (length < 4)
2228 {
2229 png_warning(png_ptr, "sCAL chunk too short");
2230 png_crc_finish(png_ptr, length);
2231 return;
2232 }
2233
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002234 png_debug1(2, "Allocating and reading sCAL chunk data (%u bytes)",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002235 length + 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002236
John Bowlerb5d00512012-03-09 09:15:18 -06002237 buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002238
John Bowlerb5d00512012-03-09 09:15:18 -06002239 if (buffer == NULL)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002240 {
2241 png_warning(png_ptr, "Out of memory while processing sCAL chunk");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002242 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002243 return;
2244 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002245
John Bowlerb5d00512012-03-09 09:15:18 -06002246 png_crc_read(png_ptr, buffer, length);
2247 buffer[length] = 0; /* Null terminate the last string */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002248
2249 if (png_crc_finish(png_ptr, 0))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002250 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002251
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002252 /* Validate the unit. */
John Bowlerb5d00512012-03-09 09:15:18 -06002253 if (buffer[0] != 1 && buffer[0] != 2)
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -05002254 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002255 png_warning(png_ptr, "Invalid sCAL ignored: invalid unit");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05002256 return;
2257 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002258
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002259 /* Validate the ASCII numbers, need two ASCII numbers separated by
2260 * a '\0' and they need to fit exactly in the chunk data.
2261 */
Glenn Randers-Pehrson254a5132011-06-10 17:45:48 -05002262 i = 1;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002263 state = 0;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002264
John Bowlerb5d00512012-03-09 09:15:18 -06002265 if (!png_check_fp_number((png_const_charp)buffer, length, &state, &i) ||
2266 i >= length || buffer[i++] != 0)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002267 png_warning(png_ptr, "Invalid sCAL chunk ignored: bad width format");
2268
John Bowler8d261262011-06-18 13:37:11 -05002269 else if (!PNG_FP_IS_POSITIVE(state))
2270 png_warning(png_ptr, "Invalid sCAL chunk ignored: non-positive width");
2271
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002272 else
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -06002273 {
John Bowler168a4332011-01-16 19:32:22 -06002274 png_size_t heighti = i;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002275
Glenn Randers-Pehrson254a5132011-06-10 17:45:48 -05002276 state = 0;
John Bowlerb5d00512012-03-09 09:15:18 -06002277 if (!png_check_fp_number((png_const_charp)buffer, length, &state, &i) ||
2278 i != length)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002279 png_warning(png_ptr, "Invalid sCAL chunk ignored: bad height format");
2280
John Bowler8d261262011-06-18 13:37:11 -05002281 else if (!PNG_FP_IS_POSITIVE(state))
2282 png_warning(png_ptr,
2283 "Invalid sCAL chunk ignored: non-positive height");
2284
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002285 else
2286 /* This is the (only) success case. */
John Bowlerb5d00512012-03-09 09:15:18 -06002287 png_set_sCAL_s(png_ptr, info_ptr, buffer[0],
2288 (png_charp)buffer+1, (png_charp)buffer+heighti);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002289 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002290}
2291#endif
2292
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002293#ifdef PNG_READ_tIME_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002294void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002295png_handle_tIME(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002296{
2297 png_byte buf[7];
2298 png_time mod_time;
2299
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002300 png_debug(1, "in png_handle_tIME");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002301
Guy Schalnate5a37791996-06-05 15:50:50 -05002302 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002303 png_error(png_ptr, "Out of place tIME chunk");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002304
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002305 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002306 {
2307 png_warning(png_ptr, "Duplicate tIME chunk");
2308 png_crc_finish(png_ptr, length);
2309 return;
2310 }
2311
2312 if (png_ptr->mode & PNG_HAVE_IDAT)
2313 png_ptr->mode |= PNG_AFTER_IDAT;
Guy Schalnate5a37791996-06-05 15:50:50 -05002314
Guy Schalnat0d580581995-07-20 02:43:20 -05002315 if (length != 7)
2316 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002317 png_warning(png_ptr, "Incorrect tIME chunk length");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002318 png_crc_finish(png_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -05002319 return;
2320 }
2321
2322 png_crc_read(png_ptr, buf, 7);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002323
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002324 if (png_crc_finish(png_ptr, 0))
2325 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05002326
2327 mod_time.second = buf[6];
2328 mod_time.minute = buf[5];
2329 mod_time.hour = buf[4];
2330 mod_time.day = buf[3];
2331 mod_time.month = buf[2];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002332 mod_time.year = png_get_uint_16(buf);
Guy Schalnat0d580581995-07-20 02:43:20 -05002333
Andreas Dilger47a0c421997-05-16 02:46:07 -05002334 png_set_tIME(png_ptr, info_ptr, &mod_time);
Guy Schalnat0d580581995-07-20 02:43:20 -05002335}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002336#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002337
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002338#ifdef PNG_READ_tEXt_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002339/* Note: this does not properly handle chunks that are > 64K under DOS */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002340void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002341png_handle_tEXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002342{
John Bowlerb5d00512012-03-09 09:15:18 -06002343 png_text text_info;
2344 png_bytep buffer;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002345 png_charp key;
Guy Schalnat6d764711995-12-19 03:22:19 -06002346 png_charp text;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002347 png_uint_32 skip = 0;
2348
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002349 png_debug(1, "in png_handle_tEXt");
Guy Schalnat0d580581995-07-20 02:43:20 -05002350
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06002351#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002352 if (png_ptr->user_chunk_cache_max != 0)
2353 {
2354 if (png_ptr->user_chunk_cache_max == 1)
2355 {
2356 png_crc_finish(png_ptr, length);
2357 return;
2358 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002359
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002360 if (--png_ptr->user_chunk_cache_max == 1)
2361 {
2362 png_warning(png_ptr, "No space in chunk cache for tEXt");
2363 png_crc_finish(png_ptr, length);
2364 return;
2365 }
2366 }
2367#endif
2368
Guy Schalnate5a37791996-06-05 15:50:50 -05002369 if (!(png_ptr->mode & PNG_HAVE_IHDR))
2370 png_error(png_ptr, "Missing IHDR before tEXt");
2371
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002372 if (png_ptr->mode & PNG_HAVE_IDAT)
2373 png_ptr->mode |= PNG_AFTER_IDAT;
2374
Andreas Dilger47a0c421997-05-16 02:46:07 -05002375#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002376 if (length > (png_uint_32)65535L)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002377 {
2378 png_warning(png_ptr, "tEXt chunk too large to fit in memory");
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002379 skip = length - (png_uint_32)65535L;
2380 length = (png_uint_32)65535L;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002381 }
2382#endif
2383
John Bowlerb5d00512012-03-09 09:15:18 -06002384 buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
Glenn Randers-Pehrson97a9b482008-10-25 20:03:28 -05002385
John Bowlerb5d00512012-03-09 09:15:18 -06002386 if (buffer == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002387 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05002388 png_warning(png_ptr, "No memory to process text chunk");
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002389 return;
2390 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002391
John Bowlerb5d00512012-03-09 09:15:18 -06002392 png_crc_read(png_ptr, buffer, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002393
2394 if (png_crc_finish(png_ptr, skip))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002395 return;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002396
John Bowlerb5d00512012-03-09 09:15:18 -06002397 key = (png_charp)buffer;
2398 key[length] = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -05002399
2400 for (text = key; *text; text++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002401 /* Empty loop to find end of key */ ;
Guy Schalnat0d580581995-07-20 02:43:20 -05002402
John Bowler0ae4f7b2012-03-03 21:10:26 -06002403 if (text != key + length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002404 text++;
2405
John Bowlerb5d00512012-03-09 09:15:18 -06002406 text_info.compression = PNG_TEXT_COMPRESSION_NONE;
2407 text_info.key = key;
2408 text_info.lang = NULL;
2409 text_info.lang_key = NULL;
2410 text_info.itxt_length = 0;
2411 text_info.text = text;
2412 text_info.text_length = png_strlen(text);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002413
John Bowlerb5d00512012-03-09 09:15:18 -06002414 if (png_set_text_2(png_ptr, info_ptr, &text_info, 1))
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002415 png_warning(png_ptr, "Insufficient memory to process text chunk");
Guy Schalnat0d580581995-07-20 02:43:20 -05002416}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002417#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002418
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002419#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002420/* Note: this does not correctly handle chunks that are > 64K under DOS */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002421void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002422png_handle_zTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002423{
John Bowler0ae4f7b2012-03-03 21:10:26 -06002424 png_const_charp errmsg = NULL;
John Bowlerb5d00512012-03-09 09:15:18 -06002425 png_bytep buffer;
2426 png_uint_32 keyword_length;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002427
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002428 png_debug(1, "in png_handle_zTXt");
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002429
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06002430#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002431 if (png_ptr->user_chunk_cache_max != 0)
2432 {
2433 if (png_ptr->user_chunk_cache_max == 1)
2434 {
2435 png_crc_finish(png_ptr, length);
2436 return;
2437 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002438
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002439 if (--png_ptr->user_chunk_cache_max == 1)
2440 {
2441 png_warning(png_ptr, "No space in chunk cache for zTXt");
2442 png_crc_finish(png_ptr, length);
2443 return;
2444 }
2445 }
2446#endif
2447
Guy Schalnate5a37791996-06-05 15:50:50 -05002448 if (!(png_ptr->mode & PNG_HAVE_IHDR))
2449 png_error(png_ptr, "Missing IHDR before zTXt");
2450
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002451 if (png_ptr->mode & PNG_HAVE_IDAT)
2452 png_ptr->mode |= PNG_AFTER_IDAT;
2453
John Bowlerb5d00512012-03-09 09:15:18 -06002454 buffer = png_read_buffer(png_ptr, length, 1/*warn*/);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002455
John Bowlerb5d00512012-03-09 09:15:18 -06002456 if (buffer == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002457 {
John Bowler0ae4f7b2012-03-03 21:10:26 -06002458 png_chunk_benign_error(png_ptr, "insufficient memory");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002459 return;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002460 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002461
John Bowlerb5d00512012-03-09 09:15:18 -06002462 png_crc_read(png_ptr, buffer, length);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002463
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002464 if (png_crc_finish(png_ptr, 0))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002465 return;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002466
John Bowler0ae4f7b2012-03-03 21:10:26 -06002467 /* TODO: also check that the keyword contents match the spec! */
2468 for (keyword_length = 0;
John Bowlerb5d00512012-03-09 09:15:18 -06002469 keyword_length < length && buffer[keyword_length] != 0;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002470 ++keyword_length)
2471 /* Empty loop to find end of name */ ;
Guy Schalnat0d580581995-07-20 02:43:20 -05002472
John Bowler0ae4f7b2012-03-03 21:10:26 -06002473 if (keyword_length > 79 || keyword_length < 1)
2474 errmsg = "bad keyword";
Guy Schalnat0d580581995-07-20 02:43:20 -05002475
John Bowler0ae4f7b2012-03-03 21:10:26 -06002476 /* zTXt must have some LZ data after the keyword, although it may expand to
2477 * zero bytes; we need a '\0' at the end of the keyword, the compression type
2478 * then the LZ data:
2479 */
2480 else if (keyword_length + 3 > length)
2481 errmsg = "truncated";
2482
John Bowlerb5d00512012-03-09 09:15:18 -06002483 else if (buffer[keyword_length+1] != PNG_COMPRESSION_TYPE_BASE)
John Bowler0ae4f7b2012-03-03 21:10:26 -06002484 errmsg = "unknown compression type";
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002485
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002486 else
Guy Schalnat0d580581995-07-20 02:43:20 -05002487 {
John Bowler0ae4f7b2012-03-03 21:10:26 -06002488 png_alloc_size_t uncompressed_length = PNG_SIZE_MAX;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002489
John Bowler0ae4f7b2012-03-03 21:10:26 -06002490 /* TODO: at present png_decompress_chunk imposes a single application
2491 * level memory limit, this should be split to different values for iCCP
2492 * and text chunks.
2493 */
John Bowlerb5d00512012-03-09 09:15:18 -06002494 if (png_decompress_chunk(png_ptr, length, keyword_length+2,
2495 &uncompressed_length, 1/*terminate*/) == Z_STREAM_END)
John Bowler0ae4f7b2012-03-03 21:10:26 -06002496 {
2497 png_text text;
2498
John Bowlerb5d00512012-03-09 09:15:18 -06002499 /* It worked; png_ptr->read_buffer now looks like a tEXt chunk except
2500 * for the extra compression type byte and the fact that it isn't
John Bowler0ae4f7b2012-03-03 21:10:26 -06002501 * necessarily '\0' terminated.
2502 */
John Bowlerb5d00512012-03-09 09:15:18 -06002503 buffer = png_ptr->read_buffer;
2504 buffer[uncompressed_length+(keyword_length+2)] = 0;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002505
2506 text.compression = PNG_TEXT_COMPRESSION_zTXt;
John Bowlerb5d00512012-03-09 09:15:18 -06002507 text.key = (png_charp)buffer;
2508 text.text = (png_charp)(buffer + keyword_length+2);
John Bowler0ae4f7b2012-03-03 21:10:26 -06002509 text.text_length = uncompressed_length;
2510 text.itxt_length = 0;
2511 text.lang = NULL;
2512 text.lang_key = NULL;
2513
2514 if (png_set_text_2(png_ptr, info_ptr, &text, 1))
2515 errmsg = "insufficient memory to store zTXt chunk";
2516 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002517
John Bowlerb5d00512012-03-09 09:15:18 -06002518 else
2519 errmsg = png_ptr->zstream.msg;
2520 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002521
John Bowler0ae4f7b2012-03-03 21:10:26 -06002522 if (errmsg != NULL)
2523 png_chunk_benign_error(png_ptr, errmsg);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002524}
2525#endif
2526
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002527#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002528/* Note: this does not correctly handle chunks that are > 64K under DOS */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002529void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002530png_handle_iTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002531{
John Bowler0ae4f7b2012-03-03 21:10:26 -06002532 png_const_charp errmsg = NULL;
John Bowlerb5d00512012-03-09 09:15:18 -06002533 png_bytep buffer;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002534 png_uint_32 prefix_length;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002535
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002536 png_debug(1, "in png_handle_iTXt");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002537
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06002538#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002539 if (png_ptr->user_chunk_cache_max != 0)
2540 {
2541 if (png_ptr->user_chunk_cache_max == 1)
2542 {
2543 png_crc_finish(png_ptr, length);
2544 return;
2545 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002546
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002547 if (--png_ptr->user_chunk_cache_max == 1)
2548 {
2549 png_warning(png_ptr, "No space in chunk cache for iTXt");
2550 png_crc_finish(png_ptr, length);
2551 return;
2552 }
2553 }
2554#endif
2555
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002556 if (!(png_ptr->mode & PNG_HAVE_IHDR))
2557 png_error(png_ptr, "Missing IHDR before iTXt");
2558
2559 if (png_ptr->mode & PNG_HAVE_IDAT)
2560 png_ptr->mode |= PNG_AFTER_IDAT;
2561
John Bowlerb5d00512012-03-09 09:15:18 -06002562 buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002563
John Bowlerb5d00512012-03-09 09:15:18 -06002564 if (buffer == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002565 {
John Bowler0ae4f7b2012-03-03 21:10:26 -06002566 png_chunk_benign_error(png_ptr, "insufficient memory");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002567 return;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002568 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002569
John Bowlerb5d00512012-03-09 09:15:18 -06002570 png_crc_read(png_ptr, buffer, length);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002571
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002572 if (png_crc_finish(png_ptr, 0))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002573 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002574
John Bowler0ae4f7b2012-03-03 21:10:26 -06002575 /* First the keyword. */
2576 for (prefix_length=0;
John Bowlerb5d00512012-03-09 09:15:18 -06002577 prefix_length < length && buffer[prefix_length] != 0;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002578 ++prefix_length)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002579 /* Empty loop */ ;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002580
John Bowler0ae4f7b2012-03-03 21:10:26 -06002581 /* Perform a basic check on the keyword length here. */
2582 if (prefix_length > 79 || prefix_length < 1)
2583 errmsg = "bad keyword";
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002584
John Bowler0ae4f7b2012-03-03 21:10:26 -06002585 /* Expect keyword, compression flag, compression type, language, translated
2586 * keyword (both may be empty but are 0 terminated) then the text, which may
2587 * be empty.
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002588 */
John Bowler0ae4f7b2012-03-03 21:10:26 -06002589 else if (prefix_length + 5 > length)
2590 errmsg = "truncated";
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002591
John Bowlerb5d00512012-03-09 09:15:18 -06002592 else if (buffer[prefix_length+1] == 0 ||
2593 (buffer[prefix_length+1] == 1 &&
2594 buffer[prefix_length+2] == PNG_COMPRESSION_TYPE_BASE))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002595 {
John Bowlerb5d00512012-03-09 09:15:18 -06002596 int compressed = buffer[prefix_length+1] != 0;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002597 png_uint_32 language_offset, translated_keyword_offset;
2598 png_alloc_size_t uncompressed_length = 0;
2599
2600 /* Now the language tag */
2601 prefix_length += 3;
2602 language_offset = prefix_length;
2603
John Bowlerb5d00512012-03-09 09:15:18 -06002604 for (; prefix_length < length && buffer[prefix_length] != 0;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002605 ++prefix_length)
2606 /* Empty loop */ ;
2607
2608 /* WARNING: the length may be invalid here, this is checked below. */
2609 translated_keyword_offset = ++prefix_length;
2610
John Bowlerb5d00512012-03-09 09:15:18 -06002611 for (; prefix_length < length && buffer[prefix_length] != 0;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002612 ++prefix_length)
2613 /* Empty loop */ ;
2614
2615 /* prefix_length should now be at the trailing '\0' of the translated
2616 * keyword, but it may already be over the end. None of this arithmetic
2617 * can overflow because chunks are at most 2^31 bytes long, but on 16-bit
2618 * systems the available allocaton may overflow.
2619 */
2620 ++prefix_length;
2621
2622 if (!compressed && prefix_length <= length)
2623 uncompressed_length = length - prefix_length;
2624
2625 else if (compressed && prefix_length < length)
2626 {
2627 uncompressed_length = PNG_SIZE_MAX;
2628
2629 /* TODO: at present png_decompress_chunk imposes a single application
2630 * level memory limit, this should be split to different values for
2631 * iCCP and text chunks.
2632 */
John Bowlerb5d00512012-03-09 09:15:18 -06002633 if (png_decompress_chunk(png_ptr, length, prefix_length,
2634 &uncompressed_length, 1/*terminate*/) == Z_STREAM_END)
2635 buffer = png_ptr->read_buffer;
2636
2637 else
2638 errmsg = png_ptr->zstream.msg;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002639 }
2640
2641 else
2642 errmsg = "truncated";
2643
2644 if (errmsg == NULL)
2645 {
2646 png_text text;
2647
John Bowlerb5d00512012-03-09 09:15:18 -06002648 buffer[uncompressed_length+prefix_length] = 0;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002649
2650 if (compressed)
2651 text.compression = PNG_ITXT_COMPRESSION_NONE;
2652
2653 else
2654 text.compression = PNG_ITXT_COMPRESSION_zTXt;
2655
John Bowlerb5d00512012-03-09 09:15:18 -06002656 text.key = (png_charp)buffer;
2657 text.lang = (png_charp)buffer + language_offset;
2658 text.lang_key = (png_charp)buffer + translated_keyword_offset;
2659 text.text = (png_charp)buffer + prefix_length;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002660 text.text_length = 0;
2661 text.itxt_length = uncompressed_length;
2662
2663 if (png_set_text_2(png_ptr, info_ptr, &text, 1))
2664 errmsg = "insufficient memory";
2665 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002666 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002667
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002668 else
John Bowler0ae4f7b2012-03-03 21:10:26 -06002669 errmsg = "bad compression info";
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002670
John Bowler0ae4f7b2012-03-03 21:10:26 -06002671 if (errmsg != NULL)
2672 png_chunk_benign_error(png_ptr, errmsg);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002673}
2674#endif
2675
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002676/* This function is called when we haven't found a handler for a
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002677 * chunk. If there isn't a problem with the chunk itself (ie bad
2678 * chunk name, CRC, or a critical chunk), the chunk is silently ignored
2679 * -- unless the PNG_FLAG_UNKNOWN_CHUNKS_SUPPORTED flag is on in which
2680 * case it will be saved away to be written out later.
2681 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002682void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002683png_handle_unknown(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002684{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002685 png_uint_32 skip = 0;
2686
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002687 png_debug(1, "in png_handle_unknown");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002688
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06002689#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002690 if (png_ptr->user_chunk_cache_max != 0)
2691 {
2692 if (png_ptr->user_chunk_cache_max == 1)
2693 {
2694 png_crc_finish(png_ptr, length);
2695 return;
2696 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002697
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002698 if (--png_ptr->user_chunk_cache_max == 1)
2699 {
2700 png_warning(png_ptr, "No space in chunk cache for unknown chunk");
2701 png_crc_finish(png_ptr, length);
2702 return;
2703 }
2704 }
2705#endif
2706
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002707 if (png_ptr->mode & PNG_HAVE_IDAT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002708 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002709 if (png_ptr->chunk_name != png_IDAT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002710 png_ptr->mode |= PNG_AFTER_IDAT;
2711 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002712
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002713 if (PNG_CHUNK_CRITICAL(png_ptr->chunk_name))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002714 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002715#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002716 if (png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name) !=
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002717 PNG_HANDLE_CHUNK_ALWAYS
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002718#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002719 && png_ptr->read_user_chunk_fn == NULL
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002720#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002721 )
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05002722#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002723 png_chunk_error(png_ptr, "unknown critical chunk");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002724 }
2725
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002726#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson7824a702009-06-13 10:05:05 -05002727 if ((png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS)
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002728#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
Glenn Randers-Pehrson7824a702009-06-13 10:05:05 -05002729 || (png_ptr->read_user_chunk_fn != NULL)
2730#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002731 )
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002732 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002733#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002734 if (length > 65535)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002735 {
2736 png_warning(png_ptr, "unknown chunk too large to fit in memory");
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002737 skip = length - 65535;
2738 length = 65535;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002739 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002740#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002741
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002742 /* TODO: this code is very close to the unknown handling in pngpread.c,
2743 * maybe it can be put into a common utility routine?
2744 * png_struct::unknown_chunk is just used as a temporary variable, along
2745 * with the data into which the chunk is read. These can be eliminated.
2746 */
2747 PNG_CSTRING_FROM_CHUNK(png_ptr->unknown_chunk.name, png_ptr->chunk_name);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002748 png_ptr->unknown_chunk.size = (png_size_t)length;
2749
2750 if (length == 0)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002751 png_ptr->unknown_chunk.data = NULL;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002752
2753 else
2754 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002755 png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr, length);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002756 png_crc_read(png_ptr, png_ptr->unknown_chunk.data, length);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002757 }
2758
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002759#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002760 if (png_ptr->read_user_chunk_fn != NULL)
2761 {
2762 /* Callback to user unknown chunk handler */
2763 int ret;
2764
2765 ret = (*(png_ptr->read_user_chunk_fn))
2766 (png_ptr, &png_ptr->unknown_chunk);
2767
2768 if (ret < 0)
2769 png_chunk_error(png_ptr, "error in user chunk");
2770
2771 if (ret == 0)
2772 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002773 if (PNG_CHUNK_CRITICAL(png_ptr->chunk_name))
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002774 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002775#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002776 if (png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name) !=
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002777 PNG_HANDLE_CHUNK_ALWAYS)
Glenn Randers-Pehrson7824a702009-06-13 10:05:05 -05002778#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002779 png_chunk_error(png_ptr, "unknown critical chunk");
2780 }
2781
2782 png_set_unknown_chunks(png_ptr, info_ptr,
2783 &png_ptr->unknown_chunk, 1);
2784 }
2785 }
2786
2787 else
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002788#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002789 png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1);
2790
2791 png_free(png_ptr, png_ptr->unknown_chunk.data);
2792 png_ptr->unknown_chunk.data = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002793 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002794
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002795 else
2796#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002797 skip = length;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002798
2799 png_crc_finish(png_ptr, skip);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002800
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -05002801#ifndef PNG_READ_USER_CHUNKS_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002802 PNG_UNUSED(info_ptr) /* Quiet compiler warnings about unused info_ptr */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002803#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002804}
2805
2806/* This function is called to verify that a chunk name is valid.
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002807 * This function can't have the "critical chunk check" incorporated
2808 * into it, since in the future we will need to be able to call user
2809 * functions to handle unknown critical chunks after we check that
2810 * the chunk name itself is valid.
2811 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002812
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002813/* Bit hacking: the test for an invalid byte in the 4 byte chunk name is:
2814 *
2815 * ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
2816 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002817
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002818void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002819png_check_chunk_name(png_structrp png_ptr, png_uint_32 chunk_name)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002820{
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002821 int i;
2822
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002823 png_debug(1, "in png_check_chunk_name");
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002824
2825 for (i=1; i<=4; ++i)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002826 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002827 int c = chunk_name & 0xff;
2828
2829 if (c < 65 || c > 122 || (c > 90 && c < 97))
2830 png_chunk_error(png_ptr, "invalid chunk type");
2831
2832 chunk_name >>= 8;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002833 }
2834}
2835
John Bowler7875d532011-11-07 22:33:49 -06002836/* Combines the row recently read in with the existing pixels in the row. This
2837 * routine takes care of alpha and transparency if requested. This routine also
2838 * handles the two methods of progressive display of interlaced images,
2839 * depending on the 'display' value; if 'display' is true then the whole row
2840 * (dp) is filled from the start by replicating the available pixels. If
2841 * 'display' is false only those pixels present in the pass are filled in.
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002842 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002843void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002844png_combine_row(png_const_structrp png_ptr, png_bytep dp, int display)
Guy Schalnat0d580581995-07-20 02:43:20 -05002845{
John Bowler4e68aa72011-10-11 16:01:33 -05002846 unsigned int pixel_depth = png_ptr->transformed_pixel_depth;
2847 png_const_bytep sp = png_ptr->row_buf + 1;
John Bowlerac8375d2011-10-06 22:27:16 -05002848 png_uint_32 row_width = png_ptr->width;
John Bowler4e68aa72011-10-11 16:01:33 -05002849 unsigned int pass = png_ptr->pass;
John Bowlerfb5b3ac2011-10-16 22:52:56 -05002850 png_bytep end_ptr = 0;
2851 png_byte end_byte = 0;
2852 unsigned int end_mask;
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002853
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002854 png_debug(1, "in png_combine_row");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002855
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002856 /* Added in 1.5.6: it should not be possible to enter this routine until at
2857 * least one row has been read from the PNG data and transformed.
2858 */
2859 if (pixel_depth == 0)
2860 png_error(png_ptr, "internal row logic error");
2861
2862 /* Added in 1.5.4: the pixel depth should match the information returned by
2863 * any call to png_read_update_info at this point. Do not continue if we got
John Bowler9994f252011-05-15 18:52:39 -05002864 * this wrong.
2865 */
2866 if (png_ptr->info_rowbytes != 0 && png_ptr->info_rowbytes !=
John Bowlerac8375d2011-10-06 22:27:16 -05002867 PNG_ROWBYTES(pixel_depth, row_width))
John Bowler9994f252011-05-15 18:52:39 -05002868 png_error(png_ptr, "internal row size calculation error");
2869
John Bowlerac8375d2011-10-06 22:27:16 -05002870 /* Don't expect this to ever happen: */
2871 if (row_width == 0)
2872 png_error(png_ptr, "internal row width error");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002873
John Bowlerfb5b3ac2011-10-16 22:52:56 -05002874 /* Preserve the last byte in cases where only part of it will be overwritten,
2875 * the multiply below may overflow, we don't care because ANSI-C guarantees
2876 * we get the low bits.
2877 */
2878 end_mask = (pixel_depth * row_width) & 7;
2879 if (end_mask != 0)
2880 {
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05002881 /* end_ptr == NULL is a flag to say do nothing */
John Bowlerfb5b3ac2011-10-16 22:52:56 -05002882 end_ptr = dp + PNG_ROWBYTES(pixel_depth, row_width) - 1;
2883 end_byte = *end_ptr;
2884# ifdef PNG_READ_PACKSWAP_SUPPORTED
2885 if (png_ptr->transformations & PNG_PACKSWAP) /* little-endian byte */
2886 end_mask = 0xff << end_mask;
2887
2888 else /* big-endian byte */
2889# endif
2890 end_mask = 0xff >> end_mask;
2891 /* end_mask is now the bits to *keep* from the destination row */
2892 }
2893
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05002894 /* For non-interlaced images this reduces to a png_memcpy(). A png_memcpy()
2895 * will also happen if interlacing isn't supported or if the application
2896 * does not call png_set_interlace_handling(). In the latter cases the
2897 * caller just gets a sequence of the unexpanded rows from each interlace
2898 * pass.
John Bowlerac8375d2011-10-06 22:27:16 -05002899 */
2900#ifdef PNG_READ_INTERLACING_SUPPORTED
2901 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE) &&
John Bowler4e68aa72011-10-11 16:01:33 -05002902 pass < 6 && (display == 0 ||
2903 /* The following copies everything for 'display' on passes 0, 2 and 4. */
2904 (display == 1 && (pass & 1) != 0)))
Guy Schalnat0d580581995-07-20 02:43:20 -05002905 {
John Bowler4e68aa72011-10-11 16:01:33 -05002906 /* Narrow images may have no bits in a pass; the caller should handle
2907 * this, but this test is cheap:
John Bowlerac8375d2011-10-06 22:27:16 -05002908 */
John Bowler4e68aa72011-10-11 16:01:33 -05002909 if (row_width <= PNG_PASS_START_COL(pass))
2910 return;
John Bowlerac8375d2011-10-06 22:27:16 -05002911
John Bowler4e68aa72011-10-11 16:01:33 -05002912 if (pixel_depth < 8)
Guy Schalnat0d580581995-07-20 02:43:20 -05002913 {
John Bowler7875d532011-11-07 22:33:49 -06002914 /* For pixel depths up to 4 bpp the 8-pixel mask can be expanded to fit
John Bowler4e68aa72011-10-11 16:01:33 -05002915 * into 32 bits, then a single loop over the bytes using the four byte
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05002916 * values in the 32-bit mask can be used. For the 'display' option the
John Bowler4e68aa72011-10-11 16:01:33 -05002917 * expanded mask may also not require any masking within a byte. To
2918 * make this work the PACKSWAP option must be taken into account - it
2919 * simply requires the pixels to be reversed in each byte.
2920 *
2921 * The 'regular' case requires a mask for each of the first 6 passes,
2922 * the 'display' case does a copy for the even passes in the range
John Bowler7875d532011-11-07 22:33:49 -06002923 * 0..6. This has already been handled in the test above.
John Bowler4e68aa72011-10-11 16:01:33 -05002924 *
2925 * The masks are arranged as four bytes with the first byte to use in
2926 * the lowest bits (little-endian) regardless of the order (PACKSWAP or
2927 * not) of the pixels in each byte.
2928 *
2929 * NOTE: the whole of this logic depends on the caller of this function
2930 * only calling it on rows appropriate to the pass. This function only
John Bowler7875d532011-11-07 22:33:49 -06002931 * understands the 'x' logic; the 'y' logic is handled by the caller.
John Bowler4e68aa72011-10-11 16:01:33 -05002932 *
2933 * The following defines allow generation of compile time constant bit
2934 * masks for each pixel depth and each possibility of swapped or not
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05002935 * swapped bytes. Pass 'p' is in the range 0..6; 'x', a pixel index,
2936 * is in the range 0..7; and the result is 1 if the pixel is to be
2937 * copied in the pass, 0 if not. 'S' is for the sparkle method, 'B'
2938 * for the block method.
2939 *
John Bowler92ef3132011-10-27 19:36:08 -05002940 * With some compilers a compile time expression of the general form:
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05002941 *
John Bowler92ef3132011-10-27 19:36:08 -05002942 * (shift >= 32) ? (a >> (shift-32)) : (b >> shift)
2943 *
2944 * Produces warnings with values of 'shift' in the range 33 to 63
Glenn Randers-Pehrson18763662011-10-27 22:09:22 -05002945 * because the right hand side of the ?: expression is evaluated by
John Bowler92ef3132011-10-27 19:36:08 -05002946 * the compiler even though it isn't used. Microsoft Visual C (various
2947 * versions) and the Intel C compiler are known to do this. To avoid
2948 * this the following macros are used in 1.5.6. This is a temporary
John Bowler7875d532011-11-07 22:33:49 -06002949 * solution to avoid destabilizing the code during the release process.
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05002950 */
John Bowler92ef3132011-10-27 19:36:08 -05002951# if PNG_USE_COMPILE_TIME_MASKS
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05002952# define PNG_LSR(x,s) ((x)>>((s) & 0x1f))
2953# define PNG_LSL(x,s) ((x)<<((s) & 0x1f))
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05002954# else
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05002955# define PNG_LSR(x,s) ((x)>>(s))
2956# define PNG_LSL(x,s) ((x)<<(s))
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05002957# endif
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05002958# define S_COPY(p,x) (((p)<4 ? PNG_LSR(0x80088822,(3-(p))*8+(7-(x))) :\
2959 PNG_LSR(0xaa55ff00,(7-(p))*8+(7-(x)))) & 1)
2960# define B_COPY(p,x) (((p)<4 ? PNG_LSR(0xff0fff33,(3-(p))*8+(7-(x))) :\
2961 PNG_LSR(0xff55ff00,(7-(p))*8+(7-(x)))) & 1)
Guy Schalnat0d580581995-07-20 02:43:20 -05002962
John Bowler4e68aa72011-10-11 16:01:33 -05002963 /* Return a mask for pass 'p' pixel 'x' at depth 'd'. The mask is
2964 * little endian - the first pixel is at bit 0 - however the extra
2965 * parameter 's' can be set to cause the mask position to be swapped
2966 * within each byte, to match the PNG format. This is done by XOR of
2967 * the shift with 7, 6 or 4 for bit depths 1, 2 and 4.
2968 */
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05002969# define PIXEL_MASK(p,x,d,s) \
2970 (PNG_LSL(((PNG_LSL(1U,(d)))-1),(((x)*(d))^((s)?8-(d):0))))
John Bowlerac8375d2011-10-06 22:27:16 -05002971
John Bowler4e68aa72011-10-11 16:01:33 -05002972 /* Hence generate the appropriate 'block' or 'sparkle' pixel copy mask.
2973 */
2974# define S_MASKx(p,x,d,s) (S_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
2975# define B_MASKx(p,x,d,s) (B_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
John Bowlerac8375d2011-10-06 22:27:16 -05002976
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05002977 /* Combine 8 of these to get the full mask. For the 1-bpp and 2-bpp
2978 * cases the result needs replicating, for the 4-bpp case the above
2979 * generates a full 32 bits.
John Bowler4e68aa72011-10-11 16:01:33 -05002980 */
2981# define MASK_EXPAND(m,d) ((m)*((d)==1?0x01010101:((d)==2?0x00010001:1)))
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002982
John Bowler4e68aa72011-10-11 16:01:33 -05002983# define S_MASK(p,d,s) MASK_EXPAND(S_MASKx(p,0,d,s) + S_MASKx(p,1,d,s) +\
2984 S_MASKx(p,2,d,s) + S_MASKx(p,3,d,s) + S_MASKx(p,4,d,s) +\
2985 S_MASKx(p,5,d,s) + S_MASKx(p,6,d,s) + S_MASKx(p,7,d,s), d)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002986
John Bowler4e68aa72011-10-11 16:01:33 -05002987# define B_MASK(p,d,s) MASK_EXPAND(B_MASKx(p,0,d,s) + B_MASKx(p,1,d,s) +\
2988 B_MASKx(p,2,d,s) + B_MASKx(p,3,d,s) + B_MASKx(p,4,d,s) +\
2989 B_MASKx(p,5,d,s) + B_MASKx(p,6,d,s) + B_MASKx(p,7,d,s), d)
John Bowlerac8375d2011-10-06 22:27:16 -05002990
John Bowler4e68aa72011-10-11 16:01:33 -05002991#if PNG_USE_COMPILE_TIME_MASKS
2992 /* Utility macros to construct all the masks for a depth/swap
2993 * combination. The 's' parameter says whether the format is PNG
John Bowler7875d532011-11-07 22:33:49 -06002994 * (big endian bytes) or not. Only the three odd-numbered passes are
John Bowler4e68aa72011-10-11 16:01:33 -05002995 * required for the display/block algorithm.
2996 */
2997# define S_MASKS(d,s) { S_MASK(0,d,s), S_MASK(1,d,s), S_MASK(2,d,s),\
2998 S_MASK(3,d,s), S_MASK(4,d,s), S_MASK(5,d,s) }
John Bowlerac8375d2011-10-06 22:27:16 -05002999
John Bowler4e68aa72011-10-11 16:01:33 -05003000# 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 -05003001
John Bowler4e68aa72011-10-11 16:01:33 -05003002# define DEPTH_INDEX(d) ((d)==1?0:((d)==2?1:2))
John Bowlerac8375d2011-10-06 22:27:16 -05003003
John Bowler4e68aa72011-10-11 16:01:33 -05003004 /* Hence the pre-compiled masks indexed by PACKSWAP (or not), depth and
3005 * then pass:
3006 */
John Bowler7875d532011-11-07 22:33:49 -06003007 static PNG_CONST png_uint_32 row_mask[2/*PACKSWAP*/][3/*depth*/][6] =
3008 {
John Bowler4e68aa72011-10-11 16:01:33 -05003009 /* Little-endian byte masks for PACKSWAP */
3010 { S_MASKS(1,0), S_MASKS(2,0), S_MASKS(4,0) },
3011 /* Normal (big-endian byte) masks - PNG format */
3012 { S_MASKS(1,1), S_MASKS(2,1), S_MASKS(4,1) }
3013 };
John Bowlerac8375d2011-10-06 22:27:16 -05003014
John Bowler4e68aa72011-10-11 16:01:33 -05003015 /* display_mask has only three entries for the odd passes, so index by
3016 * pass>>1.
3017 */
John Bowler7875d532011-11-07 22:33:49 -06003018 static PNG_CONST png_uint_32 display_mask[2][3][3] =
3019 {
John Bowler4e68aa72011-10-11 16:01:33 -05003020 /* Little-endian byte masks for PACKSWAP */
3021 { B_MASKS(1,0), B_MASKS(2,0), B_MASKS(4,0) },
3022 /* Normal (big-endian byte) masks - PNG format */
3023 { B_MASKS(1,1), B_MASKS(2,1), B_MASKS(4,1) }
3024 };
John Bowlerac8375d2011-10-06 22:27:16 -05003025
John Bowler4e68aa72011-10-11 16:01:33 -05003026# define MASK(pass,depth,display,png)\
3027 ((display)?display_mask[png][DEPTH_INDEX(depth)][pass>>1]:\
3028 row_mask[png][DEPTH_INDEX(depth)][pass])
John Bowlerac8375d2011-10-06 22:27:16 -05003029
John Bowler4e68aa72011-10-11 16:01:33 -05003030#else /* !PNG_USE_COMPILE_TIME_MASKS */
3031 /* This is the runtime alternative: it seems unlikely that this will
3032 * ever be either smaller or faster than the compile time approach.
3033 */
3034# define MASK(pass,depth,display,png)\
3035 ((display)?B_MASK(pass,depth,png):S_MASK(pass,depth,png))
3036#endif /* !PNG_USE_COMPILE_TIME_MASKS */
John Bowlerac8375d2011-10-06 22:27:16 -05003037
John Bowler4e68aa72011-10-11 16:01:33 -05003038 /* Use the appropriate mask to copy the required bits. In some cases
3039 * the byte mask will be 0 or 0xff, optimize these cases. row_width is
3040 * the number of pixels, but the code copies bytes, so it is necessary
3041 * to special case the end.
3042 */
3043 png_uint_32 pixels_per_byte = 8 / pixel_depth;
3044 png_uint_32 mask;
John Bowlerac8375d2011-10-06 22:27:16 -05003045
John Bowler4e68aa72011-10-11 16:01:33 -05003046# ifdef PNG_READ_PACKSWAP_SUPPORTED
3047 if (png_ptr->transformations & PNG_PACKSWAP)
3048 mask = MASK(pass, pixel_depth, display, 0);
John Bowlerac8375d2011-10-06 22:27:16 -05003049
3050 else
John Bowler4e68aa72011-10-11 16:01:33 -05003051# endif
3052 mask = MASK(pass, pixel_depth, display, 1);
3053
3054 for (;;)
3055 {
3056 png_uint_32 m;
3057
3058 /* It doesn't matter in the following if png_uint_32 has more than
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003059 * 32 bits because the high bits always match those in m<<24; it is,
John Bowler4e68aa72011-10-11 16:01:33 -05003060 * however, essential to use OR here, not +, because of this.
3061 */
3062 m = mask;
3063 mask = (m >> 8) | (m << 24); /* rotate right to good compilers */
3064 m &= 0xff;
3065
3066 if (m != 0) /* something to copy */
John Bowlerac8375d2011-10-06 22:27:16 -05003067 {
John Bowler4e68aa72011-10-11 16:01:33 -05003068 if (m != 0xff)
3069 *dp = (png_byte)((*dp & ~m) | (*sp & m));
3070 else
3071 *dp = *sp;
John Bowlerac8375d2011-10-06 22:27:16 -05003072 }
John Bowler4e68aa72011-10-11 16:01:33 -05003073
3074 /* NOTE: this may overwrite the last byte with garbage if the image
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003075 * is not an exact number of bytes wide; libpng has always done
John Bowler4e68aa72011-10-11 16:01:33 -05003076 * this.
3077 */
3078 if (row_width <= pixels_per_byte)
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003079 break; /* May need to restore part of the last byte */
John Bowler4e68aa72011-10-11 16:01:33 -05003080
3081 row_width -= pixels_per_byte;
3082 ++dp;
3083 ++sp;
3084 }
3085 }
3086
3087 else /* pixel_depth >= 8 */
3088 {
3089 unsigned int bytes_to_copy, bytes_to_jump;
3090
3091 /* Validate the depth - it must be a multiple of 8 */
3092 if (pixel_depth & 7)
3093 png_error(png_ptr, "invalid user transform pixel depth");
3094
3095 pixel_depth >>= 3; /* now in bytes */
3096 row_width *= pixel_depth;
3097
3098 /* Regardless of pass number the Adam 7 interlace always results in a
3099 * fixed number of pixels to copy then to skip. There may be a
3100 * different number of pixels to skip at the start though.
3101 */
3102 {
3103 unsigned int offset = PNG_PASS_START_COL(pass) * pixel_depth;
3104
3105 row_width -= offset;
3106 dp += offset;
3107 sp += offset;
John Bowlerac8375d2011-10-06 22:27:16 -05003108 }
3109
John Bowler4e68aa72011-10-11 16:01:33 -05003110 /* Work out the bytes to copy. */
3111 if (display)
3112 {
3113 /* When doing the 'block' algorithm the pixel in the pass gets
3114 * replicated to adjacent pixels. This is why the even (0,2,4,6)
3115 * passes are skipped above - the entire expanded row is copied.
3116 */
3117 bytes_to_copy = (1<<((6-pass)>>1)) * pixel_depth;
3118
3119 /* But don't allow this number to exceed the actual row width. */
3120 if (bytes_to_copy > row_width)
3121 bytes_to_copy = row_width;
3122 }
3123
3124 else /* normal row; Adam7 only ever gives us one pixel to copy. */
3125 bytes_to_copy = pixel_depth;
3126
3127 /* In Adam7 there is a constant offset between where the pixels go. */
3128 bytes_to_jump = PNG_PASS_COL_OFFSET(pass) * pixel_depth;
3129
3130 /* And simply copy these bytes. Some optimization is possible here,
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003131 * depending on the value of 'bytes_to_copy'. Special case the low
John Bowler4e68aa72011-10-11 16:01:33 -05003132 * byte counts, which we know to be frequent.
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003133 *
3134 * Notice that these cases all 'return' rather than 'break' - this
3135 * avoids an unnecessary test on whether to restore the last byte
3136 * below.
John Bowler4e68aa72011-10-11 16:01:33 -05003137 */
3138 switch (bytes_to_copy)
3139 {
3140 case 1:
3141 for (;;)
3142 {
3143 *dp = *sp;
3144
3145 if (row_width <= bytes_to_jump)
3146 return;
3147
3148 dp += bytes_to_jump;
3149 sp += bytes_to_jump;
3150 row_width -= bytes_to_jump;
3151 }
3152
3153 case 2:
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003154 /* There is a possibility of a partial copy at the end here; this
John Bowler4e68aa72011-10-11 16:01:33 -05003155 * slows the code down somewhat.
3156 */
3157 do
3158 {
3159 dp[0] = sp[0], dp[1] = sp[1];
3160
3161 if (row_width <= bytes_to_jump)
3162 return;
3163
3164 sp += bytes_to_jump;
3165 dp += bytes_to_jump;
3166 row_width -= bytes_to_jump;
3167 }
3168 while (row_width > 1);
3169
3170 /* And there can only be one byte left at this point: */
3171 *dp = *sp;
3172 return;
3173
3174 case 3:
3175 /* This can only be the RGB case, so each copy is exactly one
3176 * pixel and it is not necessary to check for a partial copy.
3177 */
3178 for(;;)
3179 {
3180 dp[0] = sp[0], dp[1] = sp[1], dp[2] = sp[2];
3181
3182 if (row_width <= bytes_to_jump)
3183 return;
3184
3185 sp += bytes_to_jump;
3186 dp += bytes_to_jump;
3187 row_width -= bytes_to_jump;
3188 }
3189
3190 default:
3191#if PNG_ALIGN_TYPE != PNG_ALIGN_NONE
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003192 /* Check for double byte alignment and, if possible, use a
3193 * 16-bit copy. Don't attempt this for narrow images - ones that
John Bowler4e68aa72011-10-11 16:01:33 -05003194 * are less than an interlace panel wide. Don't attempt it for
John Bowler7875d532011-11-07 22:33:49 -06003195 * wide bytes_to_copy either - use the png_memcpy there.
John Bowler4e68aa72011-10-11 16:01:33 -05003196 */
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003197 if (bytes_to_copy < 16 /*else use png_memcpy*/ &&
John Bowler4e68aa72011-10-11 16:01:33 -05003198 png_isaligned(dp, png_uint_16) &&
3199 png_isaligned(sp, png_uint_16) &&
3200 bytes_to_copy % sizeof (png_uint_16) == 0 &&
3201 bytes_to_jump % sizeof (png_uint_16) == 0)
3202 {
3203 /* Everything is aligned for png_uint_16 copies, but try for
3204 * png_uint_32 first.
3205 */
3206 if (png_isaligned(dp, png_uint_32) &&
3207 png_isaligned(sp, png_uint_32) &&
3208 bytes_to_copy % sizeof (png_uint_32) == 0 &&
3209 bytes_to_jump % sizeof (png_uint_32) == 0)
3210 {
3211 png_uint_32p dp32 = (png_uint_32p)dp;
3212 png_const_uint_32p sp32 = (png_const_uint_32p)sp;
3213 unsigned int skip = (bytes_to_jump-bytes_to_copy) /
3214 sizeof (png_uint_32);
3215
3216 do
3217 {
3218 size_t c = bytes_to_copy;
3219 do
3220 {
3221 *dp32++ = *sp32++;
3222 c -= sizeof (png_uint_32);
3223 }
3224 while (c > 0);
3225
3226 if (row_width <= bytes_to_jump)
3227 return;
3228
3229 dp32 += skip;
3230 sp32 += skip;
3231 row_width -= bytes_to_jump;
3232 }
3233 while (bytes_to_copy <= row_width);
3234
3235 /* Get to here when the row_width truncates the final copy.
3236 * There will be 1-3 bytes left to copy, so don't try the
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003237 * 16-bit loop below.
John Bowler4e68aa72011-10-11 16:01:33 -05003238 */
3239 dp = (png_bytep)dp32;
3240 sp = (png_const_bytep)sp32;
3241 do
3242 *dp++ = *sp++;
3243 while (--row_width > 0);
3244 return;
3245 }
3246
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003247 /* Else do it in 16-bit quantities, but only if the size is
John Bowler4e68aa72011-10-11 16:01:33 -05003248 * not too large.
3249 */
3250 else
3251 {
3252 png_uint_16p dp16 = (png_uint_16p)dp;
3253 png_const_uint_16p sp16 = (png_const_uint_16p)sp;
3254 unsigned int skip = (bytes_to_jump-bytes_to_copy) /
3255 sizeof (png_uint_16);
3256
3257 do
3258 {
3259 size_t c = bytes_to_copy;
3260 do
3261 {
3262 *dp16++ = *sp16++;
3263 c -= sizeof (png_uint_16);
3264 }
3265 while (c > 0);
3266
3267 if (row_width <= bytes_to_jump)
3268 return;
3269
3270 dp16 += skip;
3271 sp16 += skip;
3272 row_width -= bytes_to_jump;
3273 }
3274 while (bytes_to_copy <= row_width);
3275
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003276 /* End of row - 1 byte left, bytes_to_copy > row_width: */
John Bowler4e68aa72011-10-11 16:01:33 -05003277 dp = (png_bytep)dp16;
3278 sp = (png_const_bytep)sp16;
3279 do
3280 *dp++ = *sp++;
3281 while (--row_width > 0);
3282 return;
3283 }
3284 }
3285#endif /* PNG_ALIGN_ code */
3286
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003287 /* The true default - use a png_memcpy: */
John Bowler4e68aa72011-10-11 16:01:33 -05003288 for (;;)
3289 {
3290 png_memcpy(dp, sp, bytes_to_copy);
3291
3292 if (row_width <= bytes_to_jump)
3293 return;
3294
3295 sp += bytes_to_jump;
3296 dp += bytes_to_jump;
3297 row_width -= bytes_to_jump;
3298 if (bytes_to_copy > row_width)
3299 bytes_to_copy = row_width;
3300 }
3301 }
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003302
3303 /* NOT REACHED*/
John Bowler4e68aa72011-10-11 16:01:33 -05003304 } /* pixel_depth >= 8 */
3305
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003306 /* Here if pixel_depth < 8 to check 'end_ptr' below. */
Guy Schalnat0d580581995-07-20 02:43:20 -05003307 }
John Bowler4e68aa72011-10-11 16:01:33 -05003308 else
John Bowlerac8375d2011-10-06 22:27:16 -05003309#endif
3310
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003311 /* If here then the switch above wasn't used so just png_memcpy the whole row
John Bowler4e68aa72011-10-11 16:01:33 -05003312 * from the temporary row buffer (notice that this overwrites the end of the
3313 * destination row if it is a partial byte.)
John Bowlerac8375d2011-10-06 22:27:16 -05003314 */
3315 png_memcpy(dp, sp, PNG_ROWBYTES(pixel_depth, row_width));
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003316
3317 /* Restore the overwritten bits from the last byte if necessary. */
3318 if (end_ptr != NULL)
3319 *end_ptr = (png_byte)((end_byte & end_mask) | (*end_ptr & ~end_mask));
Guy Schalnat0d580581995-07-20 02:43:20 -05003320}
3321
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06003322#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05003323void /* PRIVATE */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003324png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
3325 png_uint_32 transformations /* Because these may affect the byte layout */)
Guy Schalnat0d580581995-07-20 02:43:20 -05003326{
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003327 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
3328 /* Offset to next interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003329 static PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003330
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05003331 png_debug(1, "in png_do_read_interlace");
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003332 if (row != NULL && row_info != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -05003333 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003334 png_uint_32 final_width;
3335
3336 final_width = row_info->width * png_pass_inc[pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05003337
3338 switch (row_info->pixel_depth)
3339 {
3340 case 1:
3341 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003342 png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3);
3343 png_bytep dp = row + (png_size_t)((final_width - 1) >> 3);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003344 int sshift, dshift;
3345 int s_start, s_end, s_inc;
3346 int jstop = png_pass_inc[pass];
3347 png_byte v;
Guy Schalnat0d580581995-07-20 02:43:20 -05003348 png_uint_32 i;
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003349 int j;
Guy Schalnat0d580581995-07-20 02:43:20 -05003350
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003351#ifdef PNG_READ_PACKSWAP_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003352 if (transformations & PNG_PACKSWAP)
3353 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003354 sshift = (int)((row_info->width + 7) & 0x07);
3355 dshift = (int)((final_width + 7) & 0x07);
3356 s_start = 7;
3357 s_end = 0;
3358 s_inc = -1;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003359 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003360
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003361 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05003362#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003363 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003364 sshift = 7 - (int)((row_info->width + 7) & 0x07);
3365 dshift = 7 - (int)((final_width + 7) & 0x07);
3366 s_start = 0;
3367 s_end = 7;
3368 s_inc = 1;
3369 }
Glenn Randers-Pehrson5dd2b8e2004-11-24 07:50:16 -06003370
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003371 for (i = 0; i < row_info->width; i++)
3372 {
3373 v = (png_byte)((*sp >> sshift) & 0x01);
3374 for (j = 0; j < jstop; j++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003375 {
John Bowler8fb6c6a2012-01-25 07:47:44 -06003376 unsigned int tmp = *dp & (0x7f7f >> (7 - dshift));
3377 tmp |= v << dshift;
3378 *dp = (png_byte)(tmp & 0xff);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003379
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003380 if (dshift == s_end)
3381 {
3382 dshift = s_start;
3383 dp--;
3384 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003385
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003386 else
3387 dshift += s_inc;
3388 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003389
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003390 if (sshift == s_end)
3391 {
3392 sshift = s_start;
Guy Schalnat0d580581995-07-20 02:43:20 -05003393 sp--;
3394 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003395
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003396 else
3397 sshift += s_inc;
Guy Schalnat0d580581995-07-20 02:43:20 -05003398 }
3399 break;
3400 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003401
Guy Schalnat0d580581995-07-20 02:43:20 -05003402 case 2:
3403 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003404 png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);
3405 png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);
3406 int sshift, dshift;
3407 int s_start, s_end, s_inc;
3408 int jstop = png_pass_inc[pass];
Andreas Dilger47a0c421997-05-16 02:46:07 -05003409 png_uint_32 i;
Guy Schalnat0d580581995-07-20 02:43:20 -05003410
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003411#ifdef PNG_READ_PACKSWAP_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003412 if (transformations & PNG_PACKSWAP)
3413 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003414 sshift = (int)(((row_info->width + 3) & 0x03) << 1);
3415 dshift = (int)(((final_width + 3) & 0x03) << 1);
3416 s_start = 6;
3417 s_end = 0;
3418 s_inc = -2;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003419 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003420
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003421 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05003422#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003423 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003424 sshift = (int)((3 - ((row_info->width + 3) & 0x03)) << 1);
3425 dshift = (int)((3 - ((final_width + 3) & 0x03)) << 1);
3426 s_start = 0;
3427 s_end = 6;
3428 s_inc = 2;
3429 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05003430
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003431 for (i = 0; i < row_info->width; i++)
3432 {
3433 png_byte v;
3434 int j;
3435
3436 v = (png_byte)((*sp >> sshift) & 0x03);
3437 for (j = 0; j < jstop; j++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003438 {
John Bowler8fb6c6a2012-01-25 07:47:44 -06003439 unsigned int tmp = *dp & (0x3f3f >> (6 - dshift));
3440 tmp |= v << dshift;
3441 *dp = (png_byte)(tmp & 0xff);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003442
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003443 if (dshift == s_end)
3444 {
3445 dshift = s_start;
3446 dp--;
3447 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003448
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003449 else
3450 dshift += s_inc;
3451 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003452
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003453 if (sshift == s_end)
3454 {
3455 sshift = s_start;
Guy Schalnat0d580581995-07-20 02:43:20 -05003456 sp--;
3457 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003458
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003459 else
3460 sshift += s_inc;
Guy Schalnat0d580581995-07-20 02:43:20 -05003461 }
3462 break;
3463 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003464
Guy Schalnat0d580581995-07-20 02:43:20 -05003465 case 4:
3466 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003467 png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1);
3468 png_bytep dp = row + (png_size_t)((final_width - 1) >> 1);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003469 int sshift, dshift;
3470 int s_start, s_end, s_inc;
Guy Schalnat0d580581995-07-20 02:43:20 -05003471 png_uint_32 i;
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003472 int jstop = png_pass_inc[pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05003473
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003474#ifdef PNG_READ_PACKSWAP_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003475 if (transformations & PNG_PACKSWAP)
3476 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003477 sshift = (int)(((row_info->width + 1) & 0x01) << 2);
3478 dshift = (int)(((final_width + 1) & 0x01) << 2);
3479 s_start = 4;
3480 s_end = 0;
3481 s_inc = -4;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003482 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003483
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003484 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05003485#endif
3486 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003487 sshift = (int)((1 - ((row_info->width + 1) & 0x01)) << 2);
3488 dshift = (int)((1 - ((final_width + 1) & 0x01)) << 2);
3489 s_start = 0;
3490 s_end = 4;
3491 s_inc = 4;
3492 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05003493
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003494 for (i = 0; i < row_info->width; i++)
3495 {
Glenn Randers-Pehrson8db19982011-10-27 16:17:24 -05003496 png_byte v = (png_byte)((*sp >> sshift) & 0x0f);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003497 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003498
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003499 for (j = 0; j < jstop; j++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003500 {
John Bowler8fb6c6a2012-01-25 07:47:44 -06003501 unsigned int tmp = *dp & (0xf0f >> (4 - dshift));
3502 tmp |= v << dshift;
3503 *dp = (png_byte)(tmp & 0xff);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003504
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003505 if (dshift == s_end)
3506 {
3507 dshift = s_start;
3508 dp--;
3509 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003510
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003511 else
3512 dshift += s_inc;
3513 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003514
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003515 if (sshift == s_end)
3516 {
3517 sshift = s_start;
Guy Schalnat0d580581995-07-20 02:43:20 -05003518 sp--;
3519 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003520
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003521 else
3522 sshift += s_inc;
Guy Schalnat0d580581995-07-20 02:43:20 -05003523 }
3524 break;
3525 }
John Bowlerac8375d2011-10-06 22:27:16 -05003526
Guy Schalnat0d580581995-07-20 02:43:20 -05003527 default:
3528 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003529 png_size_t pixel_bytes = (row_info->pixel_depth >> 3);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003530
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06003531 png_bytep sp = row + (png_size_t)(row_info->width - 1)
3532 * pixel_bytes;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003533
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003534 png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05003535
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003536 int jstop = png_pass_inc[pass];
3537 png_uint_32 i;
3538
3539 for (i = 0; i < row_info->width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003540 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003541 png_byte v[8];
3542 int j;
3543
3544 png_memcpy(v, sp, pixel_bytes);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003545
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003546 for (j = 0; j < jstop; j++)
3547 {
3548 png_memcpy(dp, v, pixel_bytes);
3549 dp -= pixel_bytes;
3550 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003551
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003552 sp -= pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05003553 }
3554 break;
3555 }
3556 }
John Bowlerac8375d2011-10-06 22:27:16 -05003557
Guy Schalnat0d580581995-07-20 02:43:20 -05003558 row_info->width = final_width;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003559 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width);
Guy Schalnat0d580581995-07-20 02:43:20 -05003560 }
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -05003561#ifndef PNG_READ_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003562 PNG_UNUSED(transformations) /* Silence compiler warning */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05003563#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003564}
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06003565#endif /* PNG_READ_INTERLACING_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05003566
Mans Rullgardd3a94802011-11-03 00:47:55 -05003567static void
3568png_read_filter_row_sub(png_row_infop row_info, png_bytep row,
3569 png_const_bytep prev_row)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003570{
Mans Rullgardd3a94802011-11-03 00:47:55 -05003571 png_size_t i;
3572 png_size_t istop = row_info->rowbytes;
3573 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
3574 png_bytep rp = row + bpp;
Mans Rullgardd3a94802011-11-03 00:47:55 -05003575
3576 PNG_UNUSED(prev_row)
3577
3578 for (i = bpp; i < istop; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003579 {
John Bowler7875d532011-11-07 22:33:49 -06003580 *rp = (png_byte)(((int)(*rp) + (int)(*(rp-bpp))) & 0xff);
Mans Rullgardd3a94802011-11-03 00:47:55 -05003581 rp++;
3582 }
3583}
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003584
Mans Rullgardd3a94802011-11-03 00:47:55 -05003585static void
3586png_read_filter_row_up(png_row_infop row_info, png_bytep row,
3587 png_const_bytep prev_row)
3588{
3589 png_size_t i;
3590 png_size_t istop = row_info->rowbytes;
3591 png_bytep rp = row;
3592 png_const_bytep pp = prev_row;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003593
Mans Rullgardd3a94802011-11-03 00:47:55 -05003594 for (i = 0; i < istop; i++)
3595 {
3596 *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
3597 rp++;
3598 }
3599}
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003600
Mans Rullgardd3a94802011-11-03 00:47:55 -05003601static void
3602png_read_filter_row_avg(png_row_infop row_info, png_bytep row,
3603 png_const_bytep prev_row)
3604{
3605 png_size_t i;
3606 png_bytep rp = row;
3607 png_const_bytep pp = prev_row;
Mans Rullgardd3a94802011-11-03 00:47:55 -05003608 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
3609 png_size_t istop = row_info->rowbytes - bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003610
Mans Rullgardd3a94802011-11-03 00:47:55 -05003611 for (i = 0; i < bpp; i++)
3612 {
3613 *rp = (png_byte)(((int)(*rp) +
3614 ((int)(*pp++) / 2 )) & 0xff);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003615
Mans Rullgardd3a94802011-11-03 00:47:55 -05003616 rp++;
3617 }
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003618
Mans Rullgardd3a94802011-11-03 00:47:55 -05003619 for (i = 0; i < istop; i++)
3620 {
3621 *rp = (png_byte)(((int)(*rp) +
John Bowler7875d532011-11-07 22:33:49 -06003622 (int)(*pp++ + *(rp-bpp)) / 2 ) & 0xff);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003623
Mans Rullgardd3a94802011-11-03 00:47:55 -05003624 rp++;
3625 }
3626}
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003627
Mans Rullgardd3a94802011-11-03 00:47:55 -05003628static void
John Bowlerfcc02632011-11-03 18:31:00 -05003629png_read_filter_row_paeth_1byte_pixel(png_row_infop row_info, png_bytep row,
Mans Rullgardd3a94802011-11-03 00:47:55 -05003630 png_const_bytep prev_row)
3631{
John Bowlerfcc02632011-11-03 18:31:00 -05003632 png_bytep rp_end = row + row_info->rowbytes;
3633 int a, c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003634
John Bowlerfcc02632011-11-03 18:31:00 -05003635 /* First pixel/byte */
3636 c = *prev_row++;
3637 a = *row + c;
3638 *row++ = (png_byte)a;
3639
3640 /* Remainder */
3641 while (row < rp_end)
Mans Rullgardd3a94802011-11-03 00:47:55 -05003642 {
John Bowlerfcc02632011-11-03 18:31:00 -05003643 int b, pa, pb, pc, p;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003644
John Bowlerfcc02632011-11-03 18:31:00 -05003645 a &= 0xff; /* From previous iteration or start */
3646 b = *prev_row++;
Mans Rullgardd3a94802011-11-03 00:47:55 -05003647
3648 p = b - c;
3649 pc = a - c;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003650
John Bowlerfcc02632011-11-03 18:31:00 -05003651# ifdef PNG_USE_ABS
3652 pa = abs(p);
3653 pb = abs(pc);
3654 pc = abs(p + pc);
3655# else
3656 pa = p < 0 ? -p : p;
3657 pb = pc < 0 ? -pc : pc;
3658 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
3659# endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003660
John Bowlerfcc02632011-11-03 18:31:00 -05003661 /* Find the best predictor, the least of pa, pb, pc favoring the earlier
3662 * ones in the case of a tie.
3663 */
3664 if (pb < pa) pa = pb, a = b;
3665 if (pc < pa) a = c;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003666
John Bowlerfcc02632011-11-03 18:31:00 -05003667 /* Calculate the current pixel in a, and move the previous row pixel to c
3668 * for the next time round the loop
3669 */
3670 c = b;
3671 a += *row;
3672 *row++ = (png_byte)a;
3673 }
3674}
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003675
John Bowlerfcc02632011-11-03 18:31:00 -05003676static void
3677png_read_filter_row_paeth_multibyte_pixel(png_row_infop row_info, png_bytep row,
3678 png_const_bytep prev_row)
3679{
3680 int bpp = (row_info->pixel_depth + 7) >> 3;
3681 png_bytep rp_end = row + bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003682
John Bowlerfcc02632011-11-03 18:31:00 -05003683 /* Process the first pixel in the row completely (this is the same as 'up'
3684 * because there is only one candidate predictor for the first row).
3685 */
3686 while (row < rp_end)
3687 {
3688 int a = *row + *prev_row++;
3689 *row++ = (png_byte)a;
3690 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003691
John Bowlerfcc02632011-11-03 18:31:00 -05003692 /* Remainder */
3693 rp_end += row_info->rowbytes - bpp;
3694
3695 while (row < rp_end)
3696 {
3697 int a, b, c, pa, pb, pc, p;
3698
3699 c = *(prev_row - bpp);
3700 a = *(row - bpp);
3701 b = *prev_row++;
3702
3703 p = b - c;
3704 pc = a - c;
3705
3706# ifdef PNG_USE_ABS
3707 pa = abs(p);
3708 pb = abs(pc);
3709 pc = abs(p + pc);
3710# else
3711 pa = p < 0 ? -p : p;
3712 pb = pc < 0 ? -pc : pc;
3713 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
3714# endif
3715
3716 if (pb < pa) pa = pb, a = b;
3717 if (pc < pa) a = c;
3718
3719 c = b;
3720 a += *row;
3721 *row++ = (png_byte)a;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003722 }
3723}
Guy Schalnat0d580581995-07-20 02:43:20 -05003724
Mans Rullgardd3a94802011-11-03 00:47:55 -05003725#ifdef PNG_ARM_NEON
Mans Rullgarde7acc6a2011-11-16 13:48:18 -06003726
3727#ifdef __linux__
3728#include <stdio.h>
3729#include <elf.h>
3730#include <asm/hwcap.h>
3731
3732static int png_have_hwcap(unsigned cap)
3733{
3734 FILE *f = fopen("/proc/self/auxv", "r");
3735 Elf32_auxv_t aux;
3736 int have_cap = 0;
3737
3738 if (!f)
3739 return 0;
3740
3741 while (fread(&aux, sizeof(aux), 1, f) > 0)
3742 {
3743 if (aux.a_type == AT_HWCAP &&
3744 aux.a_un.a_val & cap)
3745 {
3746 have_cap = 1;
3747 break;
3748 }
3749 }
3750
3751 fclose(f);
3752
3753 return have_cap;
3754}
3755#endif /* __linux__ */
3756
Mans Rullgardd3a94802011-11-03 00:47:55 -05003757static void
John Bowler5d567862011-12-24 09:12:00 -06003758png_init_filter_functions_neon(png_structrp pp, unsigned int bpp)
Mans Rullgardd3a94802011-11-03 00:47:55 -05003759{
Mans Rullgarde7acc6a2011-11-16 13:48:18 -06003760#ifdef __linux__
3761 if (!png_have_hwcap(HWCAP_NEON))
3762 return;
3763#endif
3764
Mans Rullgardd3a94802011-11-03 00:47:55 -05003765 pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up_neon;
3766
Mans Rullgarde7acc6a2011-11-16 13:48:18 -06003767 if (bpp == 3)
3768 {
Mans Rullgardd3a94802011-11-03 00:47:55 -05003769 pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub3_neon;
3770 pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg3_neon;
John Bowler6f237b62012-03-02 13:13:15 -06003771 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
Mans Rullgarde7acc6a2011-11-16 13:48:18 -06003772 png_read_filter_row_paeth3_neon;
3773 }
3774
3775 else if (bpp == 4)
3776 {
Mans Rullgardd3a94802011-11-03 00:47:55 -05003777 pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub4_neon;
3778 pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg4_neon;
Mans Rullgarde7acc6a2011-11-16 13:48:18 -06003779 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
3780 png_read_filter_row_paeth4_neon;
Mans Rullgardd3a94802011-11-03 00:47:55 -05003781 }
3782}
Mans Rullgarde7acc6a2011-11-16 13:48:18 -06003783#endif /* PNG_ARM_NEON */
Mans Rullgardd3a94802011-11-03 00:47:55 -05003784
3785static void
John Bowler5d567862011-12-24 09:12:00 -06003786png_init_filter_functions(png_structrp pp)
Mans Rullgardd3a94802011-11-03 00:47:55 -05003787{
John Bowlerfcc02632011-11-03 18:31:00 -05003788 unsigned int bpp = (pp->pixel_depth + 7) >> 3;
3789
Mans Rullgardd3a94802011-11-03 00:47:55 -05003790 pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub;
3791 pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up;
3792 pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg;
John Bowlerfcc02632011-11-03 18:31:00 -05003793 if (bpp == 1)
3794 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
3795 png_read_filter_row_paeth_1byte_pixel;
3796 else
3797 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
3798 png_read_filter_row_paeth_multibyte_pixel;
Mans Rullgardd3a94802011-11-03 00:47:55 -05003799
3800#ifdef PNG_ARM_NEON
John Bowlerfcc02632011-11-03 18:31:00 -05003801 png_init_filter_functions_neon(pp, bpp);
Mans Rullgardd3a94802011-11-03 00:47:55 -05003802#endif
3803}
3804
3805void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06003806png_read_filter_row(png_structrp pp, png_row_infop row_info, png_bytep row,
Mans Rullgardd3a94802011-11-03 00:47:55 -05003807 png_const_bytep prev_row, int filter)
3808{
3809 if (pp->read_filter[0] == NULL)
3810 png_init_filter_functions(pp);
3811 if (filter > PNG_FILTER_VALUE_NONE && filter < PNG_FILTER_VALUE_LAST)
3812 pp->read_filter[filter-1](row_info, row, prev_row);
3813}
3814
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05003815#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05003816void /* PRIVATE */
John Bowlerb5d00512012-03-09 09:15:18 -06003817png_read_IDAT_data(png_structrp png_ptr, png_bytep output,
3818 png_alloc_size_t avail_out)
3819{
3820 /* Loop reading IDATs and decompressing the result into output[avail_out] */
3821 png_ptr->zstream.next_out = output;
3822 png_ptr->zstream.avail_out = 0; /* safety: set below */
3823
3824 if (output == NULL)
3825 avail_out = 0;
3826
3827 do
3828 {
3829 int ret;
3830 png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];
3831
3832 if (png_ptr->zstream.avail_in == 0)
3833 {
3834 uInt avail_in;
3835 png_bytep buffer;
3836
3837 while (png_ptr->idat_size == 0)
3838 {
3839 png_crc_finish(png_ptr, 0);
3840
3841 png_ptr->idat_size = png_read_chunk_header(png_ptr);
3842 /* This is an error even in the 'check' case because the code just
3843 * consumed a non-IDAT header.
3844 */
3845 if (png_ptr->chunk_name != png_IDAT)
3846 png_error(png_ptr, "Not enough image data");
3847 }
3848
3849 avail_in = png_ptr->IDAT_read_size;
3850
3851 if (avail_in > png_ptr->idat_size)
3852 avail_in = (uInt)png_ptr->idat_size;
3853
3854 /* A PNG with a gradually increasing IDAT size will defeat this attempt
3855 * to minimize memory usage by causing lots of re-allocs, but
3856 * realistically doing IDAT_read_size re-allocs is not likely to be a
3857 * big problem.
3858 */
3859 buffer = png_read_buffer(png_ptr, avail_in, 0/*error*/);
3860
3861 png_crc_read(png_ptr, buffer, avail_in);
3862 png_ptr->idat_size -= avail_in;
3863
3864 png_ptr->zstream.next_in = buffer;
3865 png_ptr->zstream.avail_in = avail_in;
3866 }
3867
3868 /* And set up the output side. */
3869 if (output != NULL) /* standard read */
3870 {
3871 uInt out = ZLIB_IO_MAX;
3872
3873 if (out > avail_out)
3874 out = (uInt)avail_out;
3875
3876 avail_out -= out;
3877 png_ptr->zstream.avail_out = out;
3878 }
3879
3880 else /* check for end */
3881 {
3882 png_ptr->zstream.next_out = tmpbuf;
3883 png_ptr->zstream.avail_out = sizeof tmpbuf;
3884 }
3885
3886 /* Use NO_FLUSH; this gives zlib the maximum opportunity to optimize the
3887 * process.
3888 */
3889 ret = inflate(&png_ptr->zstream, Z_NO_FLUSH);
3890
3891 /* Take the unconsumed output back (so, in the 'check' case this just
3892 * counts up).
3893 */
3894 avail_out += png_ptr->zstream.avail_out;
3895 png_ptr->zstream.avail_out = 0;
3896
3897 if (ret == Z_STREAM_END)
3898 {
3899 /* Do this for safety; we won't read any more into this row. */
3900 png_ptr->zstream.next_out = NULL;
3901
3902 png_ptr->mode |= PNG_AFTER_IDAT;
3903 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
3904
3905 if (png_ptr->zstream.avail_in > 0 || png_ptr->idat_size > 0)
3906 png_chunk_benign_error(png_ptr, "Extra compressed data");
3907 break;
3908 }
3909
3910 if (ret != Z_OK)
3911 {
3912 png_zstream_error(png_ptr, ret);
3913
3914 if (output != NULL)
3915 png_chunk_error(png_ptr, png_ptr->zstream.msg);
3916
3917 else /* checking */
3918 {
3919 png_chunk_benign_error(png_ptr, png_ptr->zstream.msg);
3920 return;
3921 }
3922 }
3923 } while (avail_out > 0);
3924
3925 if (avail_out > 0)
3926 {
3927 /* The stream ended before the image; this is the same as too few IDATs so
3928 * should be handled the same way.
3929 */
3930 if (output != NULL)
3931 png_error(png_ptr, "Not enough image data");
3932
3933 else /* checking */
3934 png_chunk_benign_error(png_ptr, "Too much image data");
3935 }
3936}
3937
3938void /* PRIVATE */
3939png_read_finish_IDAT(png_structrp png_ptr)
3940{
3941 /* We don't need any more data and the stream should have ended, however the
3942 * LZ end code may actually not have been processed. In this case we must
3943 * read it otherwise stray unread IDAT data or, more likely, an IDAT chunk
3944 * may still remain to be consumed.
3945 */
3946 if (!(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED))
3947 {
3948 /* The NULL causes png_read_IDAT_data to swallow any remaining bytes in
3949 * the compressed stream, but the stream may be damaged too, so even after
3950 * this call we may need to terminate the zstream ownership.
3951 */
3952 png_read_IDAT_data(png_ptr, NULL, 0);
3953 png_ptr->zstream.next_out = NULL; /* safety */
3954
3955 /* Now clear everything out for safety; the following may not have been
3956 * done.
3957 */
3958 if (!(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED))
3959 {
3960 png_ptr->mode |= PNG_AFTER_IDAT;
3961 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
3962 }
3963 }
3964
3965 /* If the zstream has not been released do it now *and* terminate the reading
3966 * of the final IDAT chunk.
3967 */
3968 if (png_ptr->zowner == png_IDAT)
3969 {
3970 /* Always do this; the pointers otherwise point into the read buffer. */
3971 png_ptr->zstream.next_in = NULL;
3972 png_ptr->zstream.avail_in = 0;
3973
3974 /* Now we no longer own the zstream. */
3975 png_ptr->zowner = 0;
3976
3977 /* The slightly weird semantics of the sequential IDAT reading is that we
3978 * are always in or at the end of an IDAT chunk, so we always need to do a
3979 * crc_finish here. If idat_size is non-zero we also need to read the
3980 * spurious bytes at the end of the chunk now.
3981 */
3982 (void)png_crc_finish(png_ptr, png_ptr->idat_size);
3983 }
3984}
3985
3986void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06003987png_read_finish_row(png_structrp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05003988{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003989#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003990 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003991
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003992 /* Start of interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003993 static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003994
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003995 /* Offset to next interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003996 static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003997
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003998 /* Start of interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003999 static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004000
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004001 /* Offset to next interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004002 static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004003#endif /* PNG_READ_INTERLACING_SUPPORTED */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004004
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05004005 png_debug(1, "in png_read_finish_row");
Guy Schalnat0d580581995-07-20 02:43:20 -05004006 png_ptr->row_number++;
4007 if (png_ptr->row_number < png_ptr->num_rows)
4008 return;
4009
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004010#ifdef PNG_READ_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05004011 if (png_ptr->interlaced)
4012 {
4013 png_ptr->row_number = 0;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004014
Glenn Randers-Pehrson435cf872011-10-05 16:23:53 -05004015 /* TO DO: don't do this if prev_row isn't needed (requires
4016 * read-ahead of the next row's filter byte.
4017 */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004018 png_memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
4019
Guy Schalnat0d580581995-07-20 02:43:20 -05004020 do
4021 {
4022 png_ptr->pass++;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004023
Guy Schalnat0d580581995-07-20 02:43:20 -05004024 if (png_ptr->pass >= 7)
4025 break;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004026
Guy Schalnat0d580581995-07-20 02:43:20 -05004027 png_ptr->iwidth = (png_ptr->width +
4028 png_pass_inc[png_ptr->pass] - 1 -
4029 png_pass_start[png_ptr->pass]) /
4030 png_pass_inc[png_ptr->pass];
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05004031
Guy Schalnat0d580581995-07-20 02:43:20 -05004032 if (!(png_ptr->transformations & PNG_INTERLACE))
4033 {
4034 png_ptr->num_rows = (png_ptr->height +
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004035 png_pass_yinc[png_ptr->pass] - 1 -
4036 png_pass_ystart[png_ptr->pass]) /
4037 png_pass_yinc[png_ptr->pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05004038 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004039
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -05004040 else /* if (png_ptr->transformations & PNG_INTERLACE) */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004041 break; /* libpng deinterlacing sees every row */
4042
4043 } while (png_ptr->num_rows == 0 || png_ptr->iwidth == 0);
Guy Schalnat0d580581995-07-20 02:43:20 -05004044
4045 if (png_ptr->pass < 7)
4046 return;
4047 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004048#endif /* PNG_READ_INTERLACING_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05004049
John Bowler42a2b552012-03-05 20:57:40 -06004050 /* Here after at the end of the last row of the last pass. */
John Bowlerb5d00512012-03-09 09:15:18 -06004051 png_read_finish_IDAT(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05004052}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05004053#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05004054
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05004055void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06004056png_read_start_row(png_structrp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05004057{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004058#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004059 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004060
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004061 /* Start of interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004062 static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004063
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004064 /* Offset to next interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004065 static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004066
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004067 /* Start of interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004068 static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004069
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004070 /* Offset to next interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004071 static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004072#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004073
Guy Schalnat0d580581995-07-20 02:43:20 -05004074 int max_pixel_depth;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05004075 png_size_t row_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05004076
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05004077 png_debug(1, "in png_read_start_row");
John Bowler42a2b552012-03-05 20:57:40 -06004078
John Bowler4a12f4a2011-04-17 18:34:22 -05004079#ifdef PNG_READ_TRANSFORMS_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05004080 png_init_read_transformations(png_ptr);
John Bowler4a12f4a2011-04-17 18:34:22 -05004081#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004082#ifdef PNG_READ_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05004083 if (png_ptr->interlaced)
4084 {
4085 if (!(png_ptr->transformations & PNG_INTERLACE))
4086 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004087 png_pass_ystart[0]) / png_pass_yinc[0];
4088
Guy Schalnat0d580581995-07-20 02:43:20 -05004089 else
4090 png_ptr->num_rows = png_ptr->height;
4091
4092 png_ptr->iwidth = (png_ptr->width +
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004093 png_pass_inc[png_ptr->pass] - 1 -
4094 png_pass_start[png_ptr->pass]) /
4095 png_pass_inc[png_ptr->pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05004096 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004097
Guy Schalnat0d580581995-07-20 02:43:20 -05004098 else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004099#endif /* PNG_READ_INTERLACING_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05004100 {
4101 png_ptr->num_rows = png_ptr->height;
4102 png_ptr->iwidth = png_ptr->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05004103 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004104
Guy Schalnat0d580581995-07-20 02:43:20 -05004105 max_pixel_depth = png_ptr->pixel_depth;
4106
John Bowlere6fb6912011-11-08 21:35:16 -06004107 /* WARNING: * png_read_transform_info (pngrtran.c) performs a simpliar set of
4108 * calculations to calculate the final pixel depth, then
4109 * png_do_read_transforms actually does the transforms. This means that the
4110 * code which effectively calculates this value is actually repeated in three
4111 * separate places. They must all match. Innocent changes to the order of
4112 * transformations can and will break libpng in a way that causes memory
4113 * overwrites.
4114 *
4115 * TODO: fix this.
4116 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004117#ifdef PNG_READ_PACK_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05004118 if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8)
Guy Schalnat0d580581995-07-20 02:43:20 -05004119 max_pixel_depth = 8;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004120#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05004121
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004122#ifdef PNG_READ_EXPAND_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -05004123 if (png_ptr->transformations & PNG_EXPAND)
Guy Schalnat0d580581995-07-20 02:43:20 -05004124 {
4125 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
4126 {
4127 if (png_ptr->num_trans)
4128 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004129
Guy Schalnat0d580581995-07-20 02:43:20 -05004130 else
4131 max_pixel_depth = 24;
4132 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004133
Guy Schalnat0d580581995-07-20 02:43:20 -05004134 else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
4135 {
4136 if (max_pixel_depth < 8)
4137 max_pixel_depth = 8;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004138
Guy Schalnat0d580581995-07-20 02:43:20 -05004139 if (png_ptr->num_trans)
4140 max_pixel_depth *= 2;
4141 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004142
Guy Schalnat0d580581995-07-20 02:43:20 -05004143 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
4144 {
4145 if (png_ptr->num_trans)
4146 {
4147 max_pixel_depth *= 4;
4148 max_pixel_depth /= 3;
4149 }
4150 }
4151 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004152#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05004153
John Bowler4d562962011-02-12 09:01:20 -06004154#ifdef PNG_READ_EXPAND_16_SUPPORTED
4155 if (png_ptr->transformations & PNG_EXPAND_16)
4156 {
4157# ifdef PNG_READ_EXPAND_SUPPORTED
4158 /* In fact it is an error if it isn't supported, but checking is
4159 * the safe way.
4160 */
4161 if (png_ptr->transformations & PNG_EXPAND)
4162 {
4163 if (png_ptr->bit_depth < 16)
4164 max_pixel_depth *= 2;
4165 }
4166 else
4167# endif
4168 png_ptr->transformations &= ~PNG_EXPAND_16;
4169 }
4170#endif
4171
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004172#ifdef PNG_READ_FILLER_SUPPORTED
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004173 if (png_ptr->transformations & (PNG_FILLER))
Guy Schalnat0d580581995-07-20 02:43:20 -05004174 {
John Bowlere6fb6912011-11-08 21:35:16 -06004175 if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004176 {
4177 if (max_pixel_depth <= 8)
4178 max_pixel_depth = 16;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004179
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004180 else
4181 max_pixel_depth = 32;
4182 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004183
John Bowlere6fb6912011-11-08 21:35:16 -06004184 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB ||
4185 png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004186 {
4187 if (max_pixel_depth <= 32)
4188 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004189
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004190 else
4191 max_pixel_depth = 64;
4192 }
Guy Schalnat0d580581995-07-20 02:43:20 -05004193 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004194#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05004195
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004196#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05004197 if (png_ptr->transformations & PNG_GRAY_TO_RGB)
4198 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06004199 if (
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004200#ifdef PNG_READ_EXPAND_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004201 (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) ||
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06004202#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004203#ifdef PNG_READ_FILLER_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004204 (png_ptr->transformations & (PNG_FILLER)) ||
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06004205#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004206 png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
Guy Schalnat0d580581995-07-20 02:43:20 -05004207 {
4208 if (max_pixel_depth <= 16)
4209 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004210
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06004211 else
Guy Schalnat0d580581995-07-20 02:43:20 -05004212 max_pixel_depth = 64;
4213 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004214
Guy Schalnat0d580581995-07-20 02:43:20 -05004215 else
4216 {
4217 if (max_pixel_depth <= 8)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004218 {
4219 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06004220 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004221
4222 else
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06004223 max_pixel_depth = 24;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004224 }
4225
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06004226 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4227 max_pixel_depth = 64;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004228
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06004229 else
Guy Schalnat0d580581995-07-20 02:43:20 -05004230 max_pixel_depth = 48;
4231 }
4232 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004233#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05004234
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05004235#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
4236defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004237 if (png_ptr->transformations & PNG_USER_TRANSFORM)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004238 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004239 int user_pixel_depth = png_ptr->user_transform_depth *
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05004240 png_ptr->user_transform_channels;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004241
4242 if (user_pixel_depth > max_pixel_depth)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004243 max_pixel_depth = user_pixel_depth;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004244 }
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05004245#endif
4246
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004247 /* This value is stored in png_struct and double checked in the row read
4248 * code.
4249 */
4250 png_ptr->maximum_pixel_depth = (png_byte)max_pixel_depth;
4251 png_ptr->transformed_pixel_depth = 0; /* calculated on demand */
4252
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004253 /* Align the width on the next larger 8 pixels. Mainly used
4254 * for interlacing
4255 */
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05004256 row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004257 /* Calculate the maximum bytes needed, adding a byte and a pixel
4258 * for safety's sake
4259 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004260 row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) +
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004261 1 + ((max_pixel_depth + 7) >> 3);
4262
Guy Schalnat0d580581995-07-20 02:43:20 -05004263#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05004264 if (row_bytes > (png_uint_32)65536L)
Guy Schalnate5a37791996-06-05 15:50:50 -05004265 png_error(png_ptr, "This image requires a row greater than 64KB");
Guy Schalnat0d580581995-07-20 02:43:20 -05004266#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004267
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06004268 if (row_bytes + 48 > png_ptr->old_big_row_buf_size)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004269 {
4270 png_free(png_ptr, png_ptr->big_row_buf);
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004271 png_free(png_ptr, png_ptr->big_prev_row);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004272
Glenn Randers-Pehrson6917b512009-03-09 15:31:08 -05004273 if (png_ptr->interlaced)
Glenn Randers-Pehrsona515d302010-01-01 10:24:25 -06004274 png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr,
4275 row_bytes + 48);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004276
Glenn Randers-Pehrsona515d302010-01-01 10:24:25 -06004277 else
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004278 png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004279
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004280 png_ptr->big_prev_row = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06004281
4282#ifdef PNG_ALIGNED_MEMORY_SUPPORTED
4283 /* Use 16-byte aligned memory for row_buf with at least 16 bytes
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004284 * of padding before and after row_buf; treat prev_row similarly.
John Bowlerac8375d2011-10-06 22:27:16 -05004285 * NOTE: the alignment is to the start of the pixels, one beyond the start
4286 * of the buffer, because of the filter byte. Prior to libpng 1.5.6 this
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004287 * was incorrect; the filter byte was aligned, which had the exact
4288 * opposite effect of that intended.
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06004289 */
John Bowlerac8375d2011-10-06 22:27:16 -05004290 {
4291 png_bytep temp = png_ptr->big_row_buf + 32;
Glenn Randers-Pehrson8db19982011-10-27 16:17:24 -05004292 int extra = (int)((temp - (png_bytep)0) & 0x0f);
John Bowlerac8375d2011-10-06 22:27:16 -05004293 png_ptr->row_buf = temp - extra - 1/*filter byte*/;
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004294
4295 temp = png_ptr->big_prev_row + 32;
Glenn Randers-Pehrson8db19982011-10-27 16:17:24 -05004296 extra = (int)((temp - (png_bytep)0) & 0x0f);
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004297 png_ptr->prev_row = temp - extra - 1/*filter byte*/;
John Bowlerac8375d2011-10-06 22:27:16 -05004298 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004299
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06004300#else
John Bowlerac8375d2011-10-06 22:27:16 -05004301 /* Use 31 bytes of padding before and 17 bytes after row_buf. */
4302 png_ptr->row_buf = png_ptr->big_row_buf + 31;
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004303 png_ptr->prev_row = png_ptr->big_prev_row + 31;
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06004304#endif
4305 png_ptr->old_big_row_buf_size = row_bytes + 48;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004306 }
Guy Schalnat0d580581995-07-20 02:43:20 -05004307
4308#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004309 if (png_ptr->rowbytes > 65535)
Guy Schalnate5a37791996-06-05 15:50:50 -05004310 png_error(png_ptr, "This image requires a row greater than 64KB");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004311
Guy Schalnat0d580581995-07-20 02:43:20 -05004312#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004313 if (png_ptr->rowbytes > (PNG_SIZE_MAX - 1))
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05004314 png_error(png_ptr, "Row has too many bytes to allocate in memory");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004315
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05004316 png_memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05004317
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004318 png_debug1(3, "width = %u,", png_ptr->width);
4319 png_debug1(3, "height = %u,", png_ptr->height);
4320 png_debug1(3, "iwidth = %u,", png_ptr->iwidth);
4321 png_debug1(3, "num_rows = %u,", png_ptr->num_rows);
Glenn Randers-Pehrsonb764c602011-01-14 21:18:37 -06004322 png_debug1(3, "rowbytes = %lu,", (unsigned long)png_ptr->rowbytes);
4323 png_debug1(3, "irowbytes = %lu",
4324 (unsigned long)PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05004325
John Bowlerb5d00512012-03-09 09:15:18 -06004326 /* The sequential reader needs a buffer for IDAT, but the progressive reader
4327 * does not, so free the read buffer now regardless; the sequential reader
4328 * reallocates it on demand.
4329 */
4330 if (png_ptr->read_buffer)
4331 {
4332 png_bytep buffer = png_ptr->read_buffer;
4333
4334 png_ptr->read_buffer_size = 0;
4335 png_ptr->read_buffer = NULL;
4336 png_free(png_ptr, buffer);
4337 }
4338
John Bowler42a2b552012-03-05 20:57:40 -06004339 /* Finally claim the zstream for the inflate of the IDAT data. */
John Bowlerb5d00512012-03-09 09:15:18 -06004340 if (png_inflate_claim(png_ptr, png_IDAT) != Z_OK)
4341 png_error(png_ptr, png_ptr->zstream.msg);
John Bowler42a2b552012-03-05 20:57:40 -06004342
Guy Schalnate5a37791996-06-05 15:50:50 -05004343 png_ptr->flags |= PNG_FLAG_ROW_INIT;
Guy Schalnat0d580581995-07-20 02:43:20 -05004344}
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06004345#endif /* PNG_READ_SUPPORTED */