blob: 4d87d2a0387661810a4e999a20e06076b59ef73a [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
John Bowler90669192012-03-09 22:03:13 -0600329png_inflate_claim(png_structrp png_ptr, png_uint_32 owner, int window_bits)
John Bowlerb5d00512012-03-09 09:15:18 -0600330{
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
John Bowler90669192012-03-09 22:03:13 -0600376 PNG_UNUSED(window_bits)
John Bowlerb5d00512012-03-09 09:15:18 -0600377 ret = inflateReset(&png_ptr->zstream);
378# else
John Bowler90669192012-03-09 22:03:13 -0600379 ret = inflateReset2(&png_ptr->zstream, window_bits);
John Bowlerb5d00512012-03-09 09:15:18 -0600380# endif
381 }
John Bowler42a2b552012-03-05 20:57:40 -0600382
383 else
384 {
John Bowlerb5d00512012-03-09 09:15:18 -0600385# if ZLIB_VERNUM < 0x1240
386 ret = inflateInit(&png_ptr->zstream);
387# else
John Bowler90669192012-03-09 22:03:13 -0600388 ret = inflateInit2(&png_ptr->zstream, window_bits);
John Bowlerb5d00512012-03-09 09:15:18 -0600389# endif
John Bowler42a2b552012-03-05 20:57:40 -0600390
391 if (ret == Z_OK)
392 png_ptr->flags |= PNG_FLAG_ZSTREAM_INITIALIZED;
393 }
394
395 if (ret == Z_OK)
John Bowlerb5d00512012-03-09 09:15:18 -0600396 png_ptr->zowner = owner;
John Bowler42a2b552012-03-05 20:57:40 -0600397
398 else
John Bowlerb5d00512012-03-09 09:15:18 -0600399 png_zstream_error(png_ptr, ret);
John Bowler42a2b552012-03-05 20:57:40 -0600400
John Bowlerb5d00512012-03-09 09:15:18 -0600401 return ret;
402 }
403}
404
John Bowlerb11b31a2012-03-21 07:55:46 -0500405#ifdef PNG_READ_COMPRESSED_TEXT_SUPPORTED
John Bowlerb5d00512012-03-09 09:15:18 -0600406/* png_inflate now returns zlib error codes including Z_OK and Z_STREAM_END to
407 * allow the caller to do multiple calls if required. If the 'finish' flag is
408 * set Z_FINISH will be passed to the final inflate() call and Z_STREAM_END must
409 * be returned or there has been a problem, otherwise Z_SYNC_FLUSH is used and
410 * Z_OK or Z_STREAM_END will be returned on success.
411 *
412 * The input and output sizes are updated to the actual amounts of data consumed
413 * or written, not the amount available (as in a z_stream). The data pointers
414 * are not changed, so the next input is (data+input_size) and the next
415 * available output is (output+output_size).
416 */
417static int
418png_inflate(png_structrp png_ptr, png_uint_32 owner, int finish,
419 /* INPUT: */ png_const_bytep input, png_uint_32p input_size_ptr,
420 /* OUTPUT: */ png_bytep output, png_alloc_size_t *output_size_ptr)
421{
422 if (png_ptr->zowner == owner) /* Else not claimed */
423 {
424 int ret;
425 png_alloc_size_t avail_out = *output_size_ptr;
426 png_uint_32 avail_in = *input_size_ptr;
427
428 /* zlib can't necessarily handle more than 65535 bytes at once (i.e. it
429 * can't even necessarily handle 65536 bytes) because the type uInt is
430 * "16 bits or more". Consequently it is necessary to chunk the input to
431 * zlib. This code uses ZLIB_IO_MAX, from pngpriv.h, as the maximum (the
432 * maximum value that can be stored in a uInt.) It is possible to set
433 * ZLIB_IO_MAX to a lower value in pngpriv.h and this may sometimes have
434 * a performance advantage, because it reduces the amount of data accessed
435 * at each step and that may give the OS more time to page it in.
436 */
437 png_ptr->zstream.next_in = PNGZ_INPUT_CAST(input);
438 /* avail_in and avail_out are set below from 'size' */
439 png_ptr->zstream.avail_in = 0;
440 png_ptr->zstream.avail_out = 0;
441
442 /* Read directly into the output if it is available (this is set to
443 * a local buffer below if output is NULL).
444 */
445 if (output != NULL)
446 png_ptr->zstream.next_out = output;
447
448 do
449 {
450 uInt avail;
451 Byte local_buffer[PNG_INFLATE_BUF_SIZE];
452
453 /* zlib INPUT BUFFER */
454 /* The setting of 'avail_in' used to be outside the loop; by setting it
455 * inside it is possible to chunk the input to zlib and simply rely on
456 * zlib to advance the 'next_in' pointer. This allows arbitrary
457 * amounts of data to be passed through zlib at the unavoidable cost of
458 * requiring a window save (png_memcpy of up to 32768 output bytes)
459 * every ZLIB_IO_MAX input bytes.
460 */
461 avail_in += png_ptr->zstream.avail_in; /* not consumed last time */
462
463 avail = ZLIB_IO_MAX;
464
465 if (avail_in < avail)
466 avail = (uInt)avail_in; /* safe: < than ZLIB_IO_MAX */
467
468 avail_in -= avail;
469 png_ptr->zstream.avail_in = avail;
470
471 /* zlib OUTPUT BUFFER */
472 avail_out += png_ptr->zstream.avail_out; /* not written last time */
473
474 avail = ZLIB_IO_MAX; /* maximum zlib can process */
475
476 if (output == NULL)
477 {
478 /* Reset the output buffer each time round if output is NULL and
479 * make available the full buffer, up to 'remaining_space'
480 */
481 png_ptr->zstream.next_out = local_buffer;
482 if (sizeof local_buffer < avail)
483 avail = sizeof local_buffer;
484 }
485
486 if (avail_out < avail)
487 avail = (uInt)avail_out; /* safe: < ZLIB_IO_MAX */
488
489 png_ptr->zstream.avail_out = avail;
490 avail_out -= avail;
491
492 /* zlib inflate call */
493 /* In fact 'avail_out' may be 0 at this point, that happens at the end
494 * of the read when the final LZ end code was not passed at the end of
495 * the previous chunk of input data. Tell zlib if we have reached the
496 * end of the output buffer.
497 */
498 ret = inflate(&png_ptr->zstream, avail_out > 0 ? Z_NO_FLUSH :
499 (finish ? Z_FINISH : Z_SYNC_FLUSH));
500 } while (ret == Z_OK);
501
502 /* For safety kill the local buffer pointer now */
503 if (output == NULL)
504 png_ptr->zstream.next_out = NULL;
505
506 /* Claw back the 'size' and 'remaining_space' byte counts. */
507 avail_in += png_ptr->zstream.avail_in;
508 avail_out += png_ptr->zstream.avail_out;
509
510 /* Update the input and output sizes; the updated values are the amount
511 * consumed or written, effectively the inverse of what zlib uses.
512 */
513 if (avail_out > 0)
514 *output_size_ptr -= avail_out;
515
516 if (avail_in > 0)
517 *input_size_ptr -= avail_in;
518
519 /* Ensure png_ptr->zstream.msg is set (even in the success case!) */
520 png_zstream_error(png_ptr, ret);
521 return ret;
John Bowler42a2b552012-03-05 20:57:40 -0600522 }
523
524 else
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600525 {
John Bowlerb5d00512012-03-09 09:15:18 -0600526 /* This is a bad internal error. The recovery assigns to the zstream msg
527 * pointer, which is not owned by the caller, but this is safe; it's only
528 * used on errors!
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600529 */
John Bowlerb5d00512012-03-09 09:15:18 -0600530 png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed");
531 return Z_STREAM_ERROR;
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600532 }
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600533}
534
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600535/*
John Bowlerb5d00512012-03-09 09:15:18 -0600536 * Decompress trailing data in a chunk. The assumption is that read_buffer
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600537 * points at an allocated area holding the contents of a chunk with a
538 * trailing compressed part. What we get back is an allocated area
539 * holding the original prefix part and an uncompressed version of the
540 * trailing part (the malloc area passed in is freed).
541 */
John Bowlerb5d00512012-03-09 09:15:18 -0600542static int
543png_decompress_chunk(png_structrp png_ptr,
544 png_uint_32 chunklength, png_uint_32 prefix_size,
John Bowler0ae4f7b2012-03-03 21:10:26 -0600545 png_alloc_size_t *newlength /* must be initialized to the maximum! */,
546 int terminate /*add a '\0' to the end of the uncompressed data*/)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600547{
John Bowler0ae4f7b2012-03-03 21:10:26 -0600548 /* TODO: implement different limits for different types of chunk.
549 *
550 * The caller supplies *newlength set to the maximum length of the
551 * uncompressed data, but this routine allocates space for the prefix and
552 * maybe a '\0' terminator too. We have to assume that 'prefix_size' is
553 * limited only by the maximum chunk size.
554 */
555 png_alloc_size_t limit = PNG_SIZE_MAX;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600556
John Bowler0ae4f7b2012-03-03 21:10:26 -0600557# ifdef PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED
558 if (png_ptr->user_chunk_malloc_max > 0 &&
559 png_ptr->user_chunk_malloc_max < limit)
560 limit = png_ptr->user_chunk_malloc_max;
561# elif PNG_USER_CHUNK_MALLOC_MAX > 0
562 if (PNG_USER_CHUNK_MALLOC_MAX < limit)
563 limit = PNG_USER_CHUNK_MALLOC_MAX;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600564# endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600565
John Bowler0ae4f7b2012-03-03 21:10:26 -0600566 if (limit >= prefix_size + (terminate != 0))
567 {
John Bowlerb5d00512012-03-09 09:15:18 -0600568 int ret;
569
John Bowler0ae4f7b2012-03-03 21:10:26 -0600570 limit -= prefix_size + (terminate != 0);
571
572 if (limit < *newlength)
573 *newlength = limit;
574
John Bowler90669192012-03-09 22:03:13 -0600575 /* Now try to claim the stream; the 'warn' setting causes zlib to be told
576 * to use the maximum window size during inflate; this hides errors in the
577 * deflate header window bits value which is used if '0' is passed. In
578 * fact this only has an effect with zlib versions 1.2.4 and later - see
579 * the comments in png_inflate_claim above.
580 */
581 ret = png_inflate_claim(png_ptr, png_ptr->chunk_name,
582 png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN ? 15 : 0);
John Bowlerb5d00512012-03-09 09:15:18 -0600583
584 if (ret == Z_OK)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600585 {
John Bowlerb5d00512012-03-09 09:15:18 -0600586 png_uint_32 lzsize = chunklength - prefix_size;
587
588 ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/,
589 /* input: */ png_ptr->read_buffer + prefix_size, &lzsize,
590 /* output: */ NULL, newlength);
591
592 if (ret == Z_STREAM_END)
593 {
John Bowler90669192012-03-09 22:03:13 -0600594 /* Use 'inflateReset' here, not 'inflateReset2' because this
595 * preserves the previously decided window size (otherwise it would
596 * be necessary to store the previous window size.) In practice
597 * this doesn't matter anyway, because png_inflate will call inflate
598 * with Z_FINISH in almost all cases, so the window will not be
599 * maintained.
600 */
John Bowlerb5d00512012-03-09 09:15:18 -0600601 if (inflateReset(&png_ptr->zstream) == Z_OK)
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600602 {
John Bowler0ae4f7b2012-03-03 21:10:26 -0600603 /* Because of the limit checks above we know that the new,
604 * expanded, size will fit in a size_t (let alone an
605 * png_alloc_size_t). Use png_malloc_base here to avoid an
606 * extra OOM message.
607 */
608 png_alloc_size_t new_size = *newlength;
John Bowlerb5d00512012-03-09 09:15:18 -0600609 png_alloc_size_t buffer_size = prefix_size + new_size +
610 (terminate != 0);
611 png_bytep text = png_voidcast(png_bytep, png_malloc_base(png_ptr,
612 buffer_size));
John Bowler0ae4f7b2012-03-03 21:10:26 -0600613
John Bowlerb5d00512012-03-09 09:15:18 -0600614 if (text != NULL)
John Bowler0ae4f7b2012-03-03 21:10:26 -0600615 {
John Bowlerb5d00512012-03-09 09:15:18 -0600616 ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/,
617 png_ptr->read_buffer + prefix_size, &lzsize,
618 text + prefix_size, newlength);
619
620 if (ret == Z_STREAM_END)
621 {
John Bowler0ae4f7b2012-03-03 21:10:26 -0600622 if (new_size == *newlength)
623 {
624 if (terminate)
625 text[prefix_size + *newlength] = 0;
626
627 if (prefix_size > 0)
John Bowlerb5d00512012-03-09 09:15:18 -0600628 png_memcpy(text, png_ptr->read_buffer, prefix_size);
John Bowler0ae4f7b2012-03-03 21:10:26 -0600629
John Bowlerb5d00512012-03-09 09:15:18 -0600630 {
631 png_bytep old_ptr = png_ptr->read_buffer;
632
633 png_ptr->read_buffer = text;
634 png_ptr->read_buffer_size = buffer_size;
635 text = old_ptr; /* freed below */
636 }
John Bowler0ae4f7b2012-03-03 21:10:26 -0600637 }
638
John Bowlerb5d00512012-03-09 09:15:18 -0600639 else
640 {
641 /* The size changed on the second read, there can be no
642 * guarantee that anything is correct at this point.
643 * The 'msg' pointer has been set to "unexpected end of
644 * LZ stream", which is fine, but return an error code
645 * that the caller won't accept.
646 */
647 ret = PNG_UNEXPECTED_ZLIB_RETURN;
648 }
649 }
John Bowler0ae4f7b2012-03-03 21:10:26 -0600650
John Bowlerb5d00512012-03-09 09:15:18 -0600651 else if (ret == Z_OK)
652 ret = PNG_UNEXPECTED_ZLIB_RETURN; /* for safety */
John Bowler0ae4f7b2012-03-03 21:10:26 -0600653
John Bowlerb5d00512012-03-09 09:15:18 -0600654 /* Free the text pointer (this is the old read_buffer on
655 * success)
656 */
657 png_free(png_ptr, text);
658
659 /* This really is very benign, but it's still an error because
660 * the extra space may otherwise be used as a Trojan Horse.
661 */
662 if (ret == Z_STREAM_END &&
663 chunklength - prefix_size != lzsize)
664 png_chunk_benign_error(png_ptr, "extra compressed data");
665 }
666
667 else
668 {
669 /* Out of memory allocating the buffer */
670 ret = Z_MEM_ERROR;
671 png_zstream_error(png_ptr, Z_MEM_ERROR);
John Bowler0ae4f7b2012-03-03 21:10:26 -0600672 }
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600673 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600674
John Bowlerb5d00512012-03-09 09:15:18 -0600675 else
676 {
677 /* inflateReset failed, store the error message */
678 png_zstream_error(png_ptr, ret);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600679
John Bowlerb5d00512012-03-09 09:15:18 -0600680 if (ret == Z_STREAM_END)
681 ret = PNG_UNEXPECTED_ZLIB_RETURN;
682 }
683 }
684
685 else if (ret == Z_OK)
686 ret = PNG_UNEXPECTED_ZLIB_RETURN;
687
688 /* Release the claimed stream */
689 png_ptr->zowner = 0;
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600690 }
John Bowlerb5d00512012-03-09 09:15:18 -0600691
692 else /* the claim failed */ if (ret == Z_STREAM_END) /* impossible! */
693 ret = PNG_UNEXPECTED_ZLIB_RETURN;
694
695 return ret;
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600696 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600697
John Bowlerb5d00512012-03-09 09:15:18 -0600698 else
699 {
700 /* Application/configuration limits exceeded */
701 png_zstream_error(png_ptr, Z_MEM_ERROR);
702 return Z_MEM_ERROR;
703 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600704}
Glenn Randers-Pehrson5975f312011-04-01 13:15:36 -0500705#endif /* PNG_READ_COMPRESSED_TEXT_SUPPORTED */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600706
John Bowlerb11b31a2012-03-21 07:55:46 -0500707#ifdef PNG_READ_iCCP_SUPPORTED
708/* Perform a partial read and decompress, producing 'avail_out' bytes and
709 * reading from the current chunk as required.
710 */
711static int
712png_inflate_read(png_structrp png_ptr, png_bytep read_buffer, uInt read_size,
713 png_uint_32p chunk_bytes, png_bytep next_out, png_alloc_size_t *out_size,
714 int finish)
715{
716 if (png_ptr->zowner == png_ptr->chunk_name)
717 {
718 int ret;
719
720 /* next_in and avail_in must have been initialized by the caller. */
721 png_ptr->zstream.next_out = next_out;
722 png_ptr->zstream.avail_out = 0; /* set in the loop */
723
724 do
725 {
726 if (png_ptr->zstream.avail_in == 0)
727 {
728 if (read_size > *chunk_bytes)
729 read_size = (uInt)*chunk_bytes;
730 *chunk_bytes -= read_size;
731
732 if (read_size > 0)
733 png_crc_read(png_ptr, read_buffer, read_size);
734
735 png_ptr->zstream.next_in = read_buffer;
736 png_ptr->zstream.avail_in = read_size;
737 }
738
739 if (png_ptr->zstream.avail_out == 0)
740 {
741 uInt avail = ZLIB_IO_MAX;
742 if (avail > *out_size)
743 avail = (uInt)*out_size;
744 *out_size -= avail;
745
746 png_ptr->zstream.avail_out = avail;
747 }
748
749 /* Use Z_SYNC_FLUSH when there is no more chunk data to ensure that all
750 * the available output is produced; this allows reading of truncated
751 * streams.
752 */
753 ret = inflate(&png_ptr->zstream,
754 *chunk_bytes > 0 ? Z_NO_FLUSH : (finish ? Z_FINISH : Z_SYNC_FLUSH));
755 }
756 while (ret == Z_OK && (*out_size > 0 || png_ptr->zstream.avail_out > 0));
757
758 *out_size += png_ptr->zstream.avail_out;
759 png_ptr->zstream.avail_out = 0; /* Should not be required, but is safe */
760
761 /* Ensure the error message pointer is always set: */
762 png_zstream_error(png_ptr, ret);
763 return ret;
764 }
765
766 else
767 {
768 png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed");
769 return Z_STREAM_ERROR;
770 }
771}
772#endif
773
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500774/* Read and check the IDHR chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500775void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600776png_handle_IHDR(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500777{
778 png_byte buf[13];
779 png_uint_32 width, height;
780 int bit_depth, color_type, compression_type, filter_type;
781 int interlace_type;
782
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500783 png_debug(1, "in png_handle_IHDR");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500784
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600785 if (png_ptr->mode & PNG_HAVE_IHDR)
John Bowlerb11b31a2012-03-21 07:55:46 -0500786 png_chunk_error(png_ptr, "out of place");
Guy Schalnate5a37791996-06-05 15:50:50 -0500787
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500788 /* Check the length */
Guy Schalnat0d580581995-07-20 02:43:20 -0500789 if (length != 13)
John Bowlerb11b31a2012-03-21 07:55:46 -0500790 png_chunk_error(png_ptr, "invalid");
Guy Schalnat0d580581995-07-20 02:43:20 -0500791
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600792 png_ptr->mode |= PNG_HAVE_IHDR;
793
Guy Schalnat0d580581995-07-20 02:43:20 -0500794 png_crc_read(png_ptr, buf, 13);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600795 png_crc_finish(png_ptr, 0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500796
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500797 width = png_get_uint_31(png_ptr, buf);
798 height = png_get_uint_31(png_ptr, buf + 4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500799 bit_depth = buf[8];
800 color_type = buf[9];
801 compression_type = buf[10];
802 filter_type = buf[11];
803 interlace_type = buf[12];
804
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500805 /* Set internal variables */
Guy Schalnat0d580581995-07-20 02:43:20 -0500806 png_ptr->width = width;
807 png_ptr->height = height;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600808 png_ptr->bit_depth = (png_byte)bit_depth;
809 png_ptr->interlaced = (png_byte)interlace_type;
810 png_ptr->color_type = (png_byte)color_type;
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500811#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600812 png_ptr->filter_type = (png_byte)filter_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500813#endif
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500814 png_ptr->compression_type = (png_byte)compression_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500815
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500816 /* Find number of channels */
Guy Schalnat0d580581995-07-20 02:43:20 -0500817 switch (png_ptr->color_type)
818 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600819 default: /* invalid, png_set_IHDR calls png_error */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500820 case PNG_COLOR_TYPE_GRAY:
821 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnat0d580581995-07-20 02:43:20 -0500822 png_ptr->channels = 1;
823 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500824
Andreas Dilger47a0c421997-05-16 02:46:07 -0500825 case PNG_COLOR_TYPE_RGB:
Guy Schalnat0d580581995-07-20 02:43:20 -0500826 png_ptr->channels = 3;
827 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500828
Andreas Dilger47a0c421997-05-16 02:46:07 -0500829 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnat0d580581995-07-20 02:43:20 -0500830 png_ptr->channels = 2;
831 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500832
Andreas Dilger47a0c421997-05-16 02:46:07 -0500833 case PNG_COLOR_TYPE_RGB_ALPHA:
Guy Schalnat0d580581995-07-20 02:43:20 -0500834 png_ptr->channels = 4;
835 break;
836 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600837
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500838 /* Set up other useful info */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600839 png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth *
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600840 png_ptr->channels);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500841 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width);
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500842 png_debug1(3, "bit_depth = %d", png_ptr->bit_depth);
843 png_debug1(3, "channels = %d", png_ptr->channels);
Glenn Randers-Pehrsonb764c602011-01-14 21:18:37 -0600844 png_debug1(3, "rowbytes = %lu", (unsigned long)png_ptr->rowbytes);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500845 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600846 color_type, interlace_type, compression_type, filter_type);
Guy Schalnat0d580581995-07-20 02:43:20 -0500847}
848
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500849/* Read and check the palette */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500850void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600851png_handle_PLTE(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500852{
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600853 png_color palette[PNG_MAX_PALETTE_LENGTH];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600854 int num, i;
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500855#ifdef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500856 png_colorp pal_ptr;
857#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500858
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500859 png_debug(1, "in png_handle_PLTE");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500860
Guy Schalnate5a37791996-06-05 15:50:50 -0500861 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -0500862 png_chunk_error(png_ptr, "missing IHDR");
863
864 /* Moved to before the 'after IDAT' check below because otherwise duplicate
865 * PLTE chunks are potentially ignored (the spec says there shall not be more
866 * than one PLTE, the error is not treated as benign, so this check trumps
867 * the requirement that PLTE appears before IDAT.)
868 */
869 else if (png_ptr->mode & PNG_HAVE_PLTE)
870 png_chunk_error(png_ptr, "duplicate");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500871
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600872 else if (png_ptr->mode & PNG_HAVE_IDAT)
873 {
John Bowlerb11b31a2012-03-21 07:55:46 -0500874 /* This is benign because the non-benign error happened before, when an
875 * IDAT was encountered in a color-mapped image with no PLTE.
876 */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600877 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -0500878 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600879 return;
880 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500881
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600882 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500883
John Bowlerb11b31a2012-03-21 07:55:46 -0500884 if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR))
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500885 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500886 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -0500887 png_chunk_benign_error(png_ptr, "ignored in grayscale PNG");
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500888 return;
889 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600890
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -0500891#ifndef PNG_READ_OPT_PLTE_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500892 if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
893 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600894 png_crc_finish(png_ptr, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500895 return;
896 }
897#endif
898
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600899 if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3)
Guy Schalnate5a37791996-06-05 15:50:50 -0500900 {
John Bowlerb11b31a2012-03-21 07:55:46 -0500901 png_crc_finish(png_ptr, length);
902
Guy Schalnate5a37791996-06-05 15:50:50 -0500903 if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
John Bowlerb11b31a2012-03-21 07:55:46 -0500904 png_chunk_benign_error(png_ptr, "invalid");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500905
Guy Schalnate5a37791996-06-05 15:50:50 -0500906 else
John Bowlerb11b31a2012-03-21 07:55:46 -0500907 png_chunk_error(png_ptr, "invalid");
908
909 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500910 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500911
John Bowlerb11b31a2012-03-21 07:55:46 -0500912 /* The cast is safe because 'length' is less than 3*PNG_MAX_PALETTE_LENGTH */
Guy Schalnat0d580581995-07-20 02:43:20 -0500913 num = (int)length / 3;
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500914
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500915#ifdef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500916 for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)
917 {
918 png_byte buf[3];
919
920 png_crc_read(png_ptr, buf, 3);
921 pal_ptr->red = buf[0];
922 pal_ptr->green = buf[1];
923 pal_ptr->blue = buf[2];
924 }
925#else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600926 for (i = 0; i < num; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500927 {
928 png_byte buf[3];
929
930 png_crc_read(png_ptr, buf, 3);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500931 /* Don't depend upon png_color being any order */
Guy Schalnat0d580581995-07-20 02:43:20 -0500932 palette[i].red = buf[0];
933 palette[i].green = buf[1];
934 palette[i].blue = buf[2];
935 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500936#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600937
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600938 /* 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 -0500939 * whatever the normal CRC configuration tells us. However, if we
940 * have an RGB image, the PLTE can be considered ancillary, so
941 * we will act as though it is.
942 */
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -0500943#ifndef PNG_READ_OPT_PLTE_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600944 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600945#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600946 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500947 png_crc_finish(png_ptr, 0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600948 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600949
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -0500950#ifndef PNG_READ_OPT_PLTE_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600951 else if (png_crc_error(png_ptr)) /* Only if we have a CRC error */
952 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600953 /* If we don't want to use the data from an ancillary chunk,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600954 * we have two options: an error abort, or a warning and we
955 * ignore the data in this chunk (which should be OK, since
956 * it's considered ancillary for a RGB or RGBA image).
John Bowlerb11b31a2012-03-21 07:55:46 -0500957 *
958 * IMPLEMENTATION NOTE: this is only here because png_crc_finish uses the
959 * chunk type to determine whether to check the ancillary or the critical
960 * flags.
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600961 */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600962 if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE))
963 {
964 if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)
965 {
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500966 png_chunk_benign_error(png_ptr, "CRC error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600967 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600968
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600969 else
970 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600971 png_chunk_warning(png_ptr, "CRC error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600972 return;
973 }
974 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600975
Andreas Dilger47a0c421997-05-16 02:46:07 -0500976 /* Otherwise, we (optionally) emit a warning and use the chunk. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600977 else if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN))
978 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600979 png_chunk_warning(png_ptr, "CRC error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600980 }
981 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600982#endif
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500983
John Bowlerb11b31a2012-03-21 07:55:46 -0500984 /* TODO: png_set_PLTE has the side effect of setting png_ptr->palette to its
985 * own copy of the palette. This has the side effect that when png_start_row
986 * is called (this happens after any call to png_read_update_info) the
987 * info_ptr palette gets changed. This is extremely unexpected and
988 * confusing.
989 *
990 * Fix this by not sharing the palette in this way.
991 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500992 png_set_PLTE(png_ptr, info_ptr, palette, num);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500993
John Bowlerb11b31a2012-03-21 07:55:46 -0500994 /* The three chunks, bKGD, hIST and tRNS *must* appear after PLTE and before
995 * IDAT. Prior to 1.6.0 this was not checked; instead the code merely
996 * checked the apparent validity of a tRNS chunk inserted before PLTE on a
997 * palette PNG. 1.6.0 attempts to rigorously follow the standard and
998 * therefore does a benign error if the erroneous condition is detected *and*
999 * cancels the tRNS if the benign error returns. The alternative is to
1000 * amend the standard since it would be rather hypocritical of the standards
1001 * maintainers to ignore it.
1002 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001003#ifdef PNG_READ_tRNS_SUPPORTED
John Bowlerb11b31a2012-03-21 07:55:46 -05001004 if (png_ptr->num_trans > 0 ||
1005 (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS) != 0))
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001006 {
John Bowlerb11b31a2012-03-21 07:55:46 -05001007 /* Cancel this because otherwise it would be used if the transforms
1008 * require it. Don't cancel the 'valid' flag because this would prevent
1009 * detection of duplicate chunks.
1010 */
1011 png_ptr->num_trans = 0;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001012
John Bowlerb11b31a2012-03-21 07:55:46 -05001013 if (info_ptr != NULL)
1014 info_ptr->num_trans = 0;
1015
1016 png_chunk_benign_error(png_ptr, "tRNS must be after");
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001017 }
1018#endif
1019
John Bowlerb11b31a2012-03-21 07:55:46 -05001020#ifdef PNG_READ_hIST_SUPPORTED
1021 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) != 0)
1022 png_chunk_benign_error(png_ptr, "hIST must be after");
1023#endif
1024
1025#ifdef PNG_READ_bKGD_SUPPORTED
1026 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) != 0)
1027 png_chunk_benign_error(png_ptr, "bKGD must be after");
1028#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001029}
Guy Schalnate5a37791996-06-05 15:50:50 -05001030
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001031void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001032png_handle_IEND(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001033{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001034 png_debug(1, "in png_handle_IEND");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001035
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001036 if (!(png_ptr->mode & PNG_HAVE_IHDR) || !(png_ptr->mode & PNG_HAVE_IDAT))
John Bowlerb11b31a2012-03-21 07:55:46 -05001037 png_chunk_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001038
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001039 png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001040
Andreas Dilger47a0c421997-05-16 02:46:07 -05001041 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001042
John Bowlerb11b31a2012-03-21 07:55:46 -05001043 if (length != 0)
1044 png_chunk_benign_error(png_ptr, "invalid");
1045
1046 PNG_UNUSED(info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001047}
1048
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001049#ifdef PNG_READ_gAMA_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001050void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001051png_handle_gAMA(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001052{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001053 png_fixed_point igamma;
Guy Schalnat0d580581995-07-20 02:43:20 -05001054 png_byte buf[4];
1055
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001056 png_debug(1, "in png_handle_gAMA");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001057
Guy Schalnate5a37791996-06-05 15:50:50 -05001058 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05001059 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001060
John Bowlerb11b31a2012-03-21 07:55:46 -05001061 else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001062 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001063 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001064 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001065 return;
1066 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001067
Guy Schalnat0d580581995-07-20 02:43:20 -05001068 if (length != 4)
1069 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001070 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001071 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnat0d580581995-07-20 02:43:20 -05001072 return;
1073 }
1074
1075 png_crc_read(png_ptr, buf, 4);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001076
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001077 if (png_crc_finish(png_ptr, 0))
1078 return;
1079
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001080 igamma = png_get_fixed_point(NULL, buf);
Glenn Randers-Pehrson33893092010-10-23 13:20:18 -05001081
John Bowlerb11b31a2012-03-21 07:55:46 -05001082 /* The gAMA value is unsigned (and is a power law correction, so 0 is
1083 * meaningless.)
1084 */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001085 if (igamma <= 0)
1086 {
John Bowlerb11b31a2012-03-21 07:55:46 -05001087 png_chunk_benign_error(png_ptr, "out of range");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001088 return;
1089 }
1090
John Bowlerb11b31a2012-03-21 07:55:46 -05001091 /* If a colorspace error has already been output skip this chunk */
1092 if (png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID)
1093 return;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001094
John Bowlerb11b31a2012-03-21 07:55:46 -05001095 if (png_ptr->colorspace.flags & PNG_COLORSPACE_FROM_gAMA)
1096 {
1097 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1098 png_colorspace_sync(png_ptr, info_ptr);
1099
1100 png_chunk_benign_error(png_ptr, "duplicate");
1101 return;
1102 }
1103
1104 png_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_gAMA;
1105 (void)png_colorspace_set_gamma(png_ptr, &png_ptr->colorspace, igamma,
1106 1/*prefer gAMA values*/);
1107 png_colorspace_sync(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001108}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001109#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001110
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001111#ifdef PNG_READ_sBIT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001112void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001113png_handle_sBIT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001114{
John Bowler0ae4f7b2012-03-03 21:10:26 -06001115 unsigned int truelen;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001116 png_byte buf[4];
Guy Schalnat69b14481996-01-10 02:56:49 -06001117
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001118 png_debug(1, "in png_handle_sBIT");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001119
Guy Schalnat69b14481996-01-10 02:56:49 -06001120 buf[0] = buf[1] = buf[2] = buf[3] = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -05001121
Guy Schalnate5a37791996-06-05 15:50:50 -05001122 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05001123 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001124
John Bowlerb11b31a2012-03-21 07:55:46 -05001125 else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001126 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001127 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001128 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001129 return;
1130 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001131
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001132 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001133 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001134 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001135 png_chunk_benign_error(png_ptr, "duplicate");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001136 return;
1137 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001138
Guy Schalnat0d580581995-07-20 02:43:20 -05001139 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001140 truelen = 3;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001141
Guy Schalnat0d580581995-07-20 02:43:20 -05001142 else
John Bowler0ae4f7b2012-03-03 21:10:26 -06001143 truelen = png_ptr->channels;
Guy Schalnat0d580581995-07-20 02:43:20 -05001144
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001145 if (length != truelen || length > 4)
Guy Schalnat0d580581995-07-20 02:43:20 -05001146 {
John Bowlerb11b31a2012-03-21 07:55:46 -05001147 png_chunk_benign_error(png_ptr, "invalid");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001148 png_crc_finish(png_ptr, length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001149 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001150 }
1151
Andreas Dilger47a0c421997-05-16 02:46:07 -05001152 png_crc_read(png_ptr, buf, truelen);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001153
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001154 if (png_crc_finish(png_ptr, 0))
1155 return;
1156
Guy Schalnat0d580581995-07-20 02:43:20 -05001157 if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
1158 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001159 png_ptr->sig_bit.red = buf[0];
1160 png_ptr->sig_bit.green = buf[1];
1161 png_ptr->sig_bit.blue = buf[2];
1162 png_ptr->sig_bit.alpha = buf[3];
Guy Schalnat0d580581995-07-20 02:43:20 -05001163 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001164
Guy Schalnat0d580581995-07-20 02:43:20 -05001165 else
1166 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001167 png_ptr->sig_bit.gray = buf[0];
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001168 png_ptr->sig_bit.red = buf[0];
1169 png_ptr->sig_bit.green = buf[0];
1170 png_ptr->sig_bit.blue = buf[0];
Guy Schalnat6d764711995-12-19 03:22:19 -06001171 png_ptr->sig_bit.alpha = buf[1];
Guy Schalnat0d580581995-07-20 02:43:20 -05001172 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001173
Andreas Dilger47a0c421997-05-16 02:46:07 -05001174 png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
Guy Schalnat0d580581995-07-20 02:43:20 -05001175}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001176#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001177
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001178#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001179void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001180png_handle_cHRM(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001181{
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001182 png_byte buf[32];
John Bowlerb11b31a2012-03-21 07:55:46 -05001183 png_xy xy;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -06001184
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001185 png_debug(1, "in png_handle_cHRM");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001186
Guy Schalnate5a37791996-06-05 15:50:50 -05001187 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05001188 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001189
John Bowlerb11b31a2012-03-21 07:55:46 -05001190 else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001191 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001192 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001193 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001194 return;
1195 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001196
Guy Schalnat0d580581995-07-20 02:43:20 -05001197 if (length != 32)
1198 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001199 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001200 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001201 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001202 }
1203
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001204 png_crc_read(png_ptr, buf, 32);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001205
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001206 if (png_crc_finish(png_ptr, 0))
1207 return;
1208
John Bowlerb11b31a2012-03-21 07:55:46 -05001209 xy.whitex = png_get_fixed_point(NULL, buf);
1210 xy.whitey = png_get_fixed_point(NULL, buf + 4);
1211 xy.redx = png_get_fixed_point(NULL, buf + 8);
1212 xy.redy = png_get_fixed_point(NULL, buf + 12);
1213 xy.greenx = png_get_fixed_point(NULL, buf + 16);
1214 xy.greeny = png_get_fixed_point(NULL, buf + 20);
1215 xy.bluex = png_get_fixed_point(NULL, buf + 24);
1216 xy.bluey = png_get_fixed_point(NULL, buf + 28);
Glenn Randers-Pehrson33893092010-10-23 13:20:18 -05001217
John Bowlerb11b31a2012-03-21 07:55:46 -05001218 if (xy.whitex == PNG_FIXED_ERROR ||
1219 xy.whitey == PNG_FIXED_ERROR ||
1220 xy.redx == PNG_FIXED_ERROR ||
1221 xy.redy == PNG_FIXED_ERROR ||
1222 xy.greenx == PNG_FIXED_ERROR ||
1223 xy.greeny == PNG_FIXED_ERROR ||
1224 xy.bluex == PNG_FIXED_ERROR ||
1225 xy.bluey == PNG_FIXED_ERROR)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001226 {
John Bowlerb11b31a2012-03-21 07:55:46 -05001227 png_chunk_benign_error(png_ptr, "invalid values");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001228 return;
1229 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001230
John Bowlerb11b31a2012-03-21 07:55:46 -05001231 /* If a colorspace error has already been output skip this chunk */
1232 if (png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID)
1233 return;
1234
1235 if (png_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001236 {
John Bowlerb11b31a2012-03-21 07:55:46 -05001237 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1238 png_colorspace_sync(png_ptr, info_ptr);
1239 png_chunk_benign_error(png_ptr, "duplicate");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001240 return;
1241 }
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001242
John Bowlerb11b31a2012-03-21 07:55:46 -05001243 png_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM;
1244 (void)png_colorspace_set_chromaticities(png_ptr, &png_ptr->colorspace, &xy,
1245 1/*prefer cHRM values*/);
1246 png_colorspace_sync(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001247}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001248#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001249
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001250#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001251void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001252png_handle_sRGB(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001253{
John Bowlerb11b31a2012-03-21 07:55:46 -05001254 png_byte intent;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001255
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001256 png_debug(1, "in png_handle_sRGB");
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001257
1258 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05001259 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001260
John Bowlerb11b31a2012-03-21 07:55:46 -05001261 else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE))
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001262 {
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001263 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001264 png_chunk_benign_error(png_ptr, "out of place");
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001265 return;
1266 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001267
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001268 if (length != 1)
1269 {
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001270 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001271 png_chunk_benign_error(png_ptr, "invalid");
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001272 return;
1273 }
1274
John Bowlerb11b31a2012-03-21 07:55:46 -05001275 png_crc_read(png_ptr, &intent, 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001276
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001277 if (png_crc_finish(png_ptr, 0))
1278 return;
1279
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001280 /* Check for bad intent */
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001281 if (intent >= PNG_sRGB_INTENT_LAST)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001282 {
John Bowlerb11b31a2012-03-21 07:55:46 -05001283 png_chunk_benign_error(png_ptr, "Unknown sRGB intent");
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001284 return;
1285 }
1286
John Bowlerb11b31a2012-03-21 07:55:46 -05001287 /* If a colorspace error has already been output skip this chunk */
1288 if (png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID)
1289 return;
John Bowler88b77cc2011-05-05 06:49:55 -05001290
John Bowlerb11b31a2012-03-21 07:55:46 -05001291 /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect
1292 * this.
John Bowler736f40f2011-08-25 16:19:44 -05001293 */
John Bowlerb11b31a2012-03-21 07:55:46 -05001294 if (png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT)
1295 {
1296 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1297 png_colorspace_sync(png_ptr, info_ptr);
1298 png_chunk_benign_error(png_ptr, "too many profiles");
1299 return;
1300 }
John Bowler736f40f2011-08-25 16:19:44 -05001301
John Bowlerb11b31a2012-03-21 07:55:46 -05001302 /* Do not override gAMA or cHRM from the PNG file; just check they match.
1303 * This is because we write a cHRM which corresponds to D65, however there is
1304 * an issue with CMMs that assume a D50 environment that requires adaptation
1305 * of the white point. This way at least it is possible to supply an
1306 * adapated value (so long as it is within the tolerance limits for a match
1307 * against the D65 chromaticities.)
1308 *
1309 * TODO: get expert opinions on this issue
1310 */
1311 (void)png_colorspace_set_sRGB(png_ptr, &png_ptr->colorspace, intent, 0);
1312 png_colorspace_sync(png_ptr, info_ptr);
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06001313}
1314#endif /* PNG_READ_sRGB_SUPPORTED */
1315
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001316#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001317void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001318png_handle_iCCP(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
John Bowleraa816c42012-03-16 07:39:49 -05001319/* Note: this does not properly handle profiles that are > 64K under DOS */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001320{
John Bowlerb11b31a2012-03-21 07:55:46 -05001321 png_const_charp errmsg;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001322
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001323 png_debug(1, "in png_handle_iCCP");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001324
1325 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05001326 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001327
John Bowlerb11b31a2012-03-21 07:55:46 -05001328 else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001329 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001330 png_crc_finish(png_ptr, length);
John Bowleraa816c42012-03-16 07:39:49 -05001331 png_chunk_benign_error(png_ptr, "out of place");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001332 return;
1333 }
1334
John Bowlerb11b31a2012-03-21 07:55:46 -05001335 /* Consistent with all the above colorspace handling an obviously *invalid*
1336 * chunk is just ignored, so does not invalidate the color space. An
1337 * alternative is to set the 'invalid' flags at the start of this routine
1338 * and only clear them in they were not set before and all the tests pass.
1339 * The minimum 'deflate' stream is assumed to be just the 2 byte header and 4
1340 * byte checksum. The keyword must be one character and there is a
1341 * terminator (0) byte and the compression method.
1342 */
1343 if (length < 9)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001344 {
John Bowler0ae4f7b2012-03-03 21:10:26 -06001345 png_crc_finish(png_ptr, length);
John Bowleraa816c42012-03-16 07:39:49 -05001346 png_chunk_benign_error(png_ptr, "too short");
John Bowler0ae4f7b2012-03-03 21:10:26 -06001347 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001348 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001349
John Bowlerb11b31a2012-03-21 07:55:46 -05001350 /* If a colorspace error has already been output skip this chunk */
1351 if (png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID)
John Bowler0ae4f7b2012-03-03 21:10:26 -06001352 {
John Bowlerb11b31a2012-03-21 07:55:46 -05001353 png_crc_finish(png_ptr, length);
John Bowler0ae4f7b2012-03-03 21:10:26 -06001354 return;
1355 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001356
John Bowlerb11b31a2012-03-21 07:55:46 -05001357 /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect
1358 * this.
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001359 */
John Bowlerb11b31a2012-03-21 07:55:46 -05001360 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) == 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001361 {
John Bowlerb11b31a2012-03-21 07:55:46 -05001362 uInt read_length, keyword_length;
1363 char keyword[81];
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001364
John Bowlerb11b31a2012-03-21 07:55:46 -05001365 /* Find the keyword; the keyword plus separator and compression method
1366 * bytes can be at most 81 characters long.
John Bowler0ae4f7b2012-03-03 21:10:26 -06001367 */
John Bowlerb11b31a2012-03-21 07:55:46 -05001368 read_length = 81; /* maximum */
1369 if (read_length > length)
1370 read_length = (uInt)length;
John Bowler0ae4f7b2012-03-03 21:10:26 -06001371
John Bowlerb11b31a2012-03-21 07:55:46 -05001372 png_crc_read(png_ptr, (png_bytep)keyword, read_length);
1373 length -= read_length;
John Bowler0ae4f7b2012-03-03 21:10:26 -06001374
John Bowlerb11b31a2012-03-21 07:55:46 -05001375 keyword_length = 0;
1376 while (keyword_length < 80 && keyword_length < read_length &&
1377 keyword[keyword_length] != 0)
1378 ++keyword_length;
1379
1380 /* TODO: make the keyword checking common */
1381 if (keyword_length >= 1 && keyword_length <= 79)
John Bowler0ae4f7b2012-03-03 21:10:26 -06001382 {
John Bowlerb11b31a2012-03-21 07:55:46 -05001383 /* We only understand '0' compression - deflate - so if we get a
1384 * different value we can't safely decode the chunk.
John Bowler0ae4f7b2012-03-03 21:10:26 -06001385 */
John Bowlerb11b31a2012-03-21 07:55:46 -05001386 if (keyword_length+1 < read_length &&
1387 keyword[keyword_length+1] == PNG_COMPRESSION_TYPE_BASE)
1388 {
1389 read_length -= keyword_length+2;
John Bowler0ae4f7b2012-03-03 21:10:26 -06001390
John Bowlerb11b31a2012-03-21 07:55:46 -05001391 if (png_inflate_claim(png_ptr, png_iCCP,
1392 png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN ? 15 : 0) == Z_OK)
1393 {
1394 int ret;
1395 Byte profile_header[132];
1396 Byte local_buffer[PNG_INFLATE_BUF_SIZE];
1397 png_alloc_size_t size = sizeof profile_header;
1398
1399 png_ptr->zstream.next_in = (Bytef*)keyword + (keyword_length+2);
1400 png_ptr->zstream.avail_in = read_length;
1401 ret = png_inflate_read(png_ptr, local_buffer,
1402 sizeof local_buffer, &length, profile_header, &size,
1403 0/*finish: don't, because the output is too small*/);
1404
1405 if (size == 0)
1406 {
1407 /* We have the ICC profile header; do the basic header checks.
1408 */
1409 const png_uint_32 profile_length =
1410 png_get_uint_32(profile_header);
1411
1412 errmsg = NULL; /* flag to say error message output */
1413
1414 if (png_icc_check_length(png_ptr, &png_ptr->colorspace,
1415 keyword, profile_length))
1416 {
1417 /* The length is apparently ok, so we can check the 132
1418 * byte header.
1419 */
1420 if (png_icc_check_header(png_ptr, &png_ptr->colorspace,
1421 keyword, profile_length, profile_header))
1422 {
1423 /* Now read the tag table; a variable size buffer is
1424 * needed at this point, allocate one for the whole
1425 * profile. The header check has already validated
1426 * that none of these stuff will overflow.
1427 */
1428 const png_uint_32 tag_count = png_get_uint_32(
1429 profile_header+128);
1430 png_bytep profile = png_read_buffer(png_ptr,
1431 profile_length, 2/*silent*/);
1432
1433 if (profile != NULL)
1434 {
1435 memcpy(profile, profile_header,
1436 sizeof profile_header);
1437
1438 size = (sizeof profile_header) + 12 * tag_count;
Glenn Randers-Pehrson8fbd60d2012-03-21 09:18:15 -05001439
John Bowlerb11b31a2012-03-21 07:55:46 -05001440 ret = png_inflate_read(png_ptr, local_buffer,
1441 sizeof local_buffer, &length,
1442 profile + (sizeof profile_header), &size, 0);
1443
1444 if (size == 0)
1445 {
1446 if (png_icc_check_tag_table(png_ptr,
1447 &png_ptr->colorspace, keyword, profile_length,
1448 profile))
1449 {
1450 /* The profile has been validated for basic
1451 * security issues, so read the whole thing in.
1452 */
1453 size = profile_length - (sizeof profile_header)
1454 - 12 * tag_count;
Glenn Randers-Pehrson8fbd60d2012-03-21 09:18:15 -05001455
John Bowlerb11b31a2012-03-21 07:55:46 -05001456 ret = png_inflate_read(png_ptr, local_buffer,
1457 sizeof local_buffer, &length,
1458 profile + (sizeof profile_header) +
1459 12 * tag_count, &size, 1/*finish*/);
1460
1461 if (size == 0)
1462 {
Glenn Randers-Pehrson8fbd60d2012-03-21 09:18:15 -05001463 int
1464 icheck;
1465
1466 /* Known sRGB profiles:
1467 * 0: not a known sRGB profile
1468 * 1: HP-Microsoft sRGB v2
1469 * 2: ICC sRGB v4 perceptual
1470 * 3: ICC sRGB v2 perceptual
1471 * no black-compensation
1472 */
1473
1474 png_uint_32 check_crc[4] = {0,
1475 0xf29e526dUL, 0xbbef7812UL,
1476 0x427ebb21UL},
1477
1478 check_len[4] = {0, 3144, 60960,
1479 3052};
1480
John Bowlerb11b31a2012-03-21 07:55:46 -05001481 if (ret != Z_STREAM_END)
1482 png_chunk_warning(png_ptr,
1483 png_ptr->zstream.msg);
1484
1485 else if (length > 0)
1486 png_chunk_warning(png_ptr,
1487 "extra compressed data");
1488
1489 /* But, because we got the whole profile,
1490 * assume it is ok.
1491 */
1492 png_ptr->zowner = 0;
1493
1494 /* Yet the CRC should still be correct */
1495 if (png_crc_finish(png_ptr, length))
1496 return;
1497
Glenn Randers-Pehrson8fbd60d2012-03-21 09:18:15 -05001498 /* See if it's a known sRGB profile. */
1499
1500 for (icheck=3; icheck > 0; icheck--)
1501 {
1502 if (profile_length == check_len[icheck])
1503 {
1504 png_uint_32 profile_crc = crc32(0,
1505 profile, profile_length);
1506
1507 if (profile_crc ==
1508 check_crc[icheck])
1509 {
1510 (void)png_colorspace_set_sRGB(
1511 png_ptr,
1512 &png_ptr->colorspace,
1513 PNG_sRGB_INTENT_PERCEPTUAL,
1514 0);
1515
1516 png_colorspace_sync(png_ptr,
1517 info_ptr);
1518
1519 png_free(png_ptr, profile);
1520
1521 return;
1522 }
1523 }
1524 }
1525
John Bowlerb11b31a2012-03-21 07:55:46 -05001526 /* Set the gAMA and cHRM information */
1527 png_icc_set_gAMA_and_cHRM(png_ptr,
1528 &png_ptr->colorspace, keyword, profile,
1529 0/*prefer explicit gAMA/cHRM*/);
1530
Glenn Randers-Pehrson8fbd60d2012-03-21 09:18:15 -05001531 /* Steal the profile for info_ptr. */
John Bowlerb11b31a2012-03-21 07:55:46 -05001532 if (info_ptr != NULL)
1533 {
1534 png_free_data(png_ptr, info_ptr,
1535 PNG_FREE_ICCP, 0);
1536
1537 info_ptr->iccp_name = png_voidcast(char*,
1538 png_malloc_base(png_ptr,
1539 keyword_length+1));
1540 if (info_ptr->iccp_name == NULL)
1541 {
1542 png_ptr->colorspace.flags |=
1543 PNG_COLORSPACE_INVALID;
1544 png_colorspace_sync(png_ptr,
1545 info_ptr);
1546 png_chunk_benign_error(png_ptr,
1547 "out of memory");
1548 return;
1549 }
1550
1551 memcpy(info_ptr->iccp_name, keyword,
1552 keyword_length+1);
1553 info_ptr->iccp_proflen = profile_length;
1554 info_ptr->iccp_profile = profile;
1555 png_ptr->read_buffer = NULL; /*steal*/
1556 info_ptr->free_me |= PNG_FREE_ICCP;
1557 info_ptr->valid |= PNG_INFO_iCCP;
1558
1559 png_colorspace_sync(png_ptr, info_ptr);
1560 }
1561
1562 return;
1563 }
1564
1565 else
1566 errmsg = "truncated";
1567 }
1568
1569 /* else png_icc_check_tag_table output an error */
1570 }
1571
1572 else /* profile truncated */
1573 errmsg = png_ptr->zstream.msg;
1574 }
1575
1576 else
1577 errmsg = "out of memory";
1578 }
1579
1580 /* else png_icc_check_header output an error */
1581 }
1582
1583 /* else png_icc_check_length output an error */
1584 }
1585
1586 else /* profile truncated */
1587 errmsg = png_ptr->zstream.msg;
1588
1589 /* Release the stream */
1590 png_ptr->zowner = 0;
1591 }
1592
1593 else /* png_inflate_claim failed */
1594 errmsg = png_ptr->zstream.msg;
1595 }
John Bowler0ae4f7b2012-03-03 21:10:26 -06001596
1597 else
John Bowlerb11b31a2012-03-21 07:55:46 -05001598 errmsg = "bad compression method"; /* or missing */
John Bowler0ae4f7b2012-03-03 21:10:26 -06001599 }
John Bowler0ae4f7b2012-03-03 21:10:26 -06001600
John Bowlerb5d00512012-03-09 09:15:18 -06001601 else
John Bowlerb11b31a2012-03-21 07:55:46 -05001602 errmsg = "bad keyword";
John Bowlerb5d00512012-03-09 09:15:18 -06001603 }
John Bowlerb11b31a2012-03-21 07:55:46 -05001604
1605 else
1606 errmsg = "too many profiles";
John Bowler0ae4f7b2012-03-03 21:10:26 -06001607
John Bowlerb11b31a2012-03-21 07:55:46 -05001608 /* Failure: the reason is in 'errmsg' */
1609 png_crc_finish(png_ptr, length);
1610 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1611 png_colorspace_sync(png_ptr, info_ptr);
1612 if (errmsg != NULL) /* else already output */
1613 png_chunk_benign_error(png_ptr, errmsg);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001614}
1615#endif /* PNG_READ_iCCP_SUPPORTED */
1616
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001617#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001618void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001619png_handle_sPLT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001620/* Note: this does not properly handle chunks that are > 64K under DOS */
1621{
John Bowlerb5d00512012-03-09 09:15:18 -06001622 png_bytep entry_start, buffer;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001623 png_sPLT_t new_palette;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001624 png_sPLT_entryp pp;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001625 png_uint_32 data_length;
1626 int entry_size, i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001627 png_uint_32 skip = 0;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001628 png_uint_32 dl;
1629 png_size_t max_dl;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001630
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001631 png_debug(1, "in png_handle_sPLT");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001632
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001633#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001634 if (png_ptr->user_chunk_cache_max != 0)
1635 {
1636 if (png_ptr->user_chunk_cache_max == 1)
1637 {
1638 png_crc_finish(png_ptr, length);
1639 return;
1640 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001641
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001642 if (--png_ptr->user_chunk_cache_max == 1)
1643 {
1644 png_warning(png_ptr, "No space in chunk cache for sPLT");
1645 png_crc_finish(png_ptr, length);
1646 return;
1647 }
1648 }
1649#endif
1650
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001651 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05001652 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001653
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001654 else if (png_ptr->mode & PNG_HAVE_IDAT)
1655 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001656 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001657 png_chunk_benign_error(png_ptr, "out of place");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001658 return;
1659 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001660
1661#ifdef PNG_MAX_MALLOC_64K
John Bowlerb11b31a2012-03-21 07:55:46 -05001662 if (length > 65535U)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001663 {
John Bowlerb11b31a2012-03-21 07:55:46 -05001664 png_crc_finish(png_ptr, length);
1665 png_chunk_benign_error(png_ptr, "too large to fit in memory");
1666 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001667 }
1668#endif
1669
John Bowlerb11b31a2012-03-21 07:55:46 -05001670 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
1671 if (buffer == NULL)
1672 {
1673 png_crc_finish(png_ptr, length);
1674 png_chunk_benign_error(png_ptr, "out of memory");
1675 return;
1676 }
1677
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001678
1679 /* WARNING: this may break if size_t is less than 32 bits; it is assumed
1680 * that the PNG_MAX_MALLOC_64K test is enabled in this case, but this is a
1681 * potential breakage point if the types in pngconf.h aren't exactly right.
1682 */
John Bowlerb5d00512012-03-09 09:15:18 -06001683 png_crc_read(png_ptr, buffer, length);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001684
1685 if (png_crc_finish(png_ptr, skip))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001686 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001687
John Bowlerb5d00512012-03-09 09:15:18 -06001688 buffer[length] = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001689
John Bowlerb5d00512012-03-09 09:15:18 -06001690 for (entry_start = buffer; *entry_start; entry_start++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001691 /* Empty loop to find end of name */ ;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001692
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001693 ++entry_start;
1694
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001695 /* A sample depth should follow the separator, and we should be on it */
John Bowlerb5d00512012-03-09 09:15:18 -06001696 if (entry_start > buffer + length - 2)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001697 {
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -05001698 png_warning(png_ptr, "malformed sPLT chunk");
1699 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001700 }
1701
1702 new_palette.depth = *entry_start++;
Glenn Randers-Pehrsona565f0e2010-03-06 08:24:45 -06001703 entry_size = (new_palette.depth == 8 ? 6 : 10);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001704 /* This must fit in a png_uint_32 because it is derived from the original
John Bowlerb5d00512012-03-09 09:15:18 -06001705 * chunk data length.
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001706 */
John Bowlerb5d00512012-03-09 09:15:18 -06001707 data_length = length - (png_uint_32)(entry_start - buffer);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001708
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001709 /* Integrity-check the data length */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001710 if (data_length % entry_size)
1711 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001712 png_warning(png_ptr, "sPLT chunk has bad length");
1713 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001714 }
1715
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001716 dl = (png_int_32)(data_length / entry_size);
1717 max_dl = PNG_SIZE_MAX / png_sizeof(png_sPLT_entry);
1718
1719 if (dl > max_dl)
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001720 {
1721 png_warning(png_ptr, "sPLT chunk too long");
1722 return;
1723 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001724
1725 new_palette.nentries = (png_int_32)(data_length / entry_size);
1726
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001727 new_palette.entries = (png_sPLT_entryp)png_malloc_warn(
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001728 png_ptr, new_palette.nentries * png_sizeof(png_sPLT_entry));
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001729
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001730 if (new_palette.entries == NULL)
1731 {
1732 png_warning(png_ptr, "sPLT chunk requires too much memory");
1733 return;
1734 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001735
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001736#ifdef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001737 for (i = 0; i < new_palette.nentries; i++)
1738 {
Glenn Randers-Pehrson90b878c2009-10-07 12:44:35 -05001739 pp = new_palette.entries + i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001740
1741 if (new_palette.depth == 8)
1742 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001743 pp->red = *entry_start++;
1744 pp->green = *entry_start++;
1745 pp->blue = *entry_start++;
1746 pp->alpha = *entry_start++;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001747 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001748
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001749 else
1750 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001751 pp->red = png_get_uint_16(entry_start); entry_start += 2;
1752 pp->green = png_get_uint_16(entry_start); entry_start += 2;
1753 pp->blue = png_get_uint_16(entry_start); entry_start += 2;
1754 pp->alpha = png_get_uint_16(entry_start); entry_start += 2;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001755 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001756
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001757 pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
1758 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001759#else
1760 pp = new_palette.entries;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001761
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001762 for (i = 0; i < new_palette.nentries; i++)
1763 {
1764
1765 if (new_palette.depth == 8)
1766 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001767 pp[i].red = *entry_start++;
1768 pp[i].green = *entry_start++;
1769 pp[i].blue = *entry_start++;
1770 pp[i].alpha = *entry_start++;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001771 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001772
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001773 else
1774 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001775 pp[i].red = png_get_uint_16(entry_start); entry_start += 2;
1776 pp[i].green = png_get_uint_16(entry_start); entry_start += 2;
1777 pp[i].blue = png_get_uint_16(entry_start); entry_start += 2;
1778 pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001779 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001780
Glenn Randers-Pehrsonf27592a2011-03-21 18:05:40 -05001781 pp[i].frequency = png_get_uint_16(entry_start); entry_start += 2;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001782 }
1783#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001784
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001785 /* Discard all chunk data except the name and stash that */
John Bowlerb5d00512012-03-09 09:15:18 -06001786 new_palette.name = (png_charp)buffer;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001787
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001788 png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001789
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001790 png_free(png_ptr, new_palette.entries);
1791}
1792#endif /* PNG_READ_sPLT_SUPPORTED */
1793
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001794#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001795void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001796png_handle_tRNS(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001797{
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001798 png_byte readbuf[PNG_MAX_PALETTE_LENGTH];
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06001799
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001800 png_debug(1, "in png_handle_tRNS");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001801
Guy Schalnate5a37791996-06-05 15:50:50 -05001802 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05001803 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001804
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001805 else if (png_ptr->mode & PNG_HAVE_IDAT)
1806 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001807 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001808 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001809 return;
1810 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001811
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001812 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001813 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001814 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001815 png_chunk_benign_error(png_ptr, "duplicate");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001816 return;
1817 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001818
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001819 if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
Guy Schalnat0d580581995-07-20 02:43:20 -05001820 {
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001821 png_byte buf[2];
Guy Schalnat0d580581995-07-20 02:43:20 -05001822
1823 if (length != 2)
1824 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001825 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001826 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001827 return;
1828 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001829
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001830 png_crc_read(png_ptr, buf, 2);
1831 png_ptr->num_trans = 1;
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -05001832 png_ptr->trans_color.gray = png_get_uint_16(buf);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001833 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001834
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001835 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
1836 {
1837 png_byte buf[6];
1838
1839 if (length != 6)
1840 {
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001841 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001842 png_chunk_benign_error(png_ptr, "invalid");
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001843 return;
1844 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001845
John Bowler0ae4f7b2012-03-03 21:10:26 -06001846 png_crc_read(png_ptr, buf, length);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001847 png_ptr->num_trans = 1;
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -05001848 png_ptr->trans_color.red = png_get_uint_16(buf);
1849 png_ptr->trans_color.green = png_get_uint_16(buf + 2);
1850 png_ptr->trans_color.blue = png_get_uint_16(buf + 4);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001851 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001852
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001853 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1854 {
1855 if (!(png_ptr->mode & PNG_HAVE_PLTE))
1856 {
John Bowlerb11b31a2012-03-21 07:55:46 -05001857 /* TODO: is this actually an error in the ISO spec? */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001858 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001859 png_chunk_benign_error(png_ptr, "out of place");
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001860 return;
1861 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001862
John Bowlerb11b31a2012-03-21 07:55:46 -05001863 if (length > png_ptr->num_palette || length > PNG_MAX_PALETTE_LENGTH ||
1864 length == 0)
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001865 {
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001866 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001867 png_chunk_benign_error(png_ptr, "invalid");
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001868 return;
1869 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001870
John Bowler0ae4f7b2012-03-03 21:10:26 -06001871 png_crc_read(png_ptr, readbuf, length);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001872 png_ptr->num_trans = (png_uint_16)length;
1873 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001874
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001875 else
Guy Schalnate5a37791996-06-05 15:50:50 -05001876 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001877 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001878 png_chunk_benign_error(png_ptr, "invalid with alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -05001879 return;
1880 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001881
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001882 if (png_crc_finish(png_ptr, 0))
Glenn Randers-Pehrsona7dbcba2007-05-15 16:16:34 -05001883 {
1884 png_ptr->num_trans = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001885 return;
Glenn Randers-Pehrsona7dbcba2007-05-15 16:16:34 -05001886 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001887
John Bowlerb11b31a2012-03-21 07:55:46 -05001888 /* TODO: this is a horrible side effect in the palette case because the
1889 * png_struct ends up with a pointer to the tRNS buffer owned by the
1890 * png_info. Fix this.
1891 */
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06001892 png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001893 &(png_ptr->trans_color));
Guy Schalnat0d580581995-07-20 02:43:20 -05001894}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001895#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001896
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001897#ifdef PNG_READ_bKGD_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001898void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001899png_handle_bKGD(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001900{
John Bowler0ae4f7b2012-03-03 21:10:26 -06001901 unsigned int truelen;
Guy Schalnat0d580581995-07-20 02:43:20 -05001902 png_byte buf[6];
John Bowlercb0b2962011-05-12 21:48:29 -05001903 png_color_16 background;
Guy Schalnat0d580581995-07-20 02:43:20 -05001904
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001905 png_debug(1, "in png_handle_bKGD");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001906
Guy Schalnate5a37791996-06-05 15:50:50 -05001907 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05001908 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001909
John Bowlerb11b31a2012-03-21 07:55:46 -05001910 else if ((png_ptr->mode & PNG_HAVE_IDAT) ||
1911 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
1912 !(png_ptr->mode & PNG_HAVE_PLTE)))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001913 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001914 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001915 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001916 return;
1917 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001918
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001919 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001920 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001921 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001922 png_chunk_benign_error(png_ptr, "duplicate");
Guy Schalnate5a37791996-06-05 15:50:50 -05001923 return;
1924 }
1925
Guy Schalnat0d580581995-07-20 02:43:20 -05001926 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1927 truelen = 1;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001928
Guy Schalnat0d580581995-07-20 02:43:20 -05001929 else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
1930 truelen = 6;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001931
Guy Schalnat0d580581995-07-20 02:43:20 -05001932 else
1933 truelen = 2;
1934
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001935 if (length != truelen)
Guy Schalnat0d580581995-07-20 02:43:20 -05001936 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001937 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001938 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnat0d580581995-07-20 02:43:20 -05001939 return;
1940 }
1941
Andreas Dilger47a0c421997-05-16 02:46:07 -05001942 png_crc_read(png_ptr, buf, truelen);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001943
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001944 if (png_crc_finish(png_ptr, 0))
1945 return;
1946
Guy Schalnate5a37791996-06-05 15:50:50 -05001947 /* We convert the index value into RGB components so that we can allow
1948 * arbitrary RGB values for background when we have transparency, and
1949 * so it is easy to determine the RGB values of the background color
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001950 * from the info_ptr struct.
1951 */
Guy Schalnat0d580581995-07-20 02:43:20 -05001952 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Guy Schalnate5a37791996-06-05 15:50:50 -05001953 {
John Bowlercb0b2962011-05-12 21:48:29 -05001954 background.index = buf[0];
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001955
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001956 if (info_ptr && info_ptr->num_palette)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001957 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001958 if (buf[0] >= info_ptr->num_palette)
1959 {
John Bowlerb11b31a2012-03-21 07:55:46 -05001960 png_chunk_benign_error(png_ptr, "invalid index");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001961 return;
1962 }
1963
John Bowlercb0b2962011-05-12 21:48:29 -05001964 background.red = (png_uint_16)png_ptr->palette[buf[0]].red;
1965 background.green = (png_uint_16)png_ptr->palette[buf[0]].green;
1966 background.blue = (png_uint_16)png_ptr->palette[buf[0]].blue;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001967 }
John Bowlercb0b2962011-05-12 21:48:29 -05001968
1969 else
1970 background.red = background.green = background.blue = 0;
1971
1972 background.gray = 0;
Guy Schalnate5a37791996-06-05 15:50:50 -05001973 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001974
Andreas Dilger47a0c421997-05-16 02:46:07 -05001975 else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) /* GRAY */
Guy Schalnate5a37791996-06-05 15:50:50 -05001976 {
John Bowlercb0b2962011-05-12 21:48:29 -05001977 background.index = 0;
1978 background.red =
1979 background.green =
1980 background.blue =
1981 background.gray = png_get_uint_16(buf);
Guy Schalnate5a37791996-06-05 15:50:50 -05001982 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001983
Guy Schalnat0d580581995-07-20 02:43:20 -05001984 else
1985 {
John Bowlercb0b2962011-05-12 21:48:29 -05001986 background.index = 0;
1987 background.red = png_get_uint_16(buf);
1988 background.green = png_get_uint_16(buf + 2);
1989 background.blue = png_get_uint_16(buf + 4);
1990 background.gray = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -05001991 }
1992
John Bowlercb0b2962011-05-12 21:48:29 -05001993 png_set_bKGD(png_ptr, info_ptr, &background);
Guy Schalnat0d580581995-07-20 02:43:20 -05001994}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001995#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001996
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001997#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001998void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001999png_handle_hIST(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002000{
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05002001 unsigned int num, i;
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05002002 png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];
Guy Schalnat0d580581995-07-20 02:43:20 -05002003
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002004 png_debug(1, "in png_handle_hIST");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002005
Guy Schalnate5a37791996-06-05 15:50:50 -05002006 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05002007 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002008
John Bowlerb11b31a2012-03-21 07:55:46 -05002009 else if ((png_ptr->mode & PNG_HAVE_IDAT) || !(png_ptr->mode & PNG_HAVE_PLTE))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002010 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002011 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002012 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002013 return;
2014 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002015
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002016 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002017 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002018 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002019 png_chunk_benign_error(png_ptr, "duplicate");
Guy Schalnate5a37791996-06-05 15:50:50 -05002020 return;
2021 }
2022
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05002023 num = length / 2 ;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002024
John Bowlerb11b31a2012-03-21 07:55:46 -05002025 if (num != png_ptr->num_palette || num > PNG_MAX_PALETTE_LENGTH)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002026 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002027 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002028 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002029 return;
2030 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002031
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002032 for (i = 0; i < num; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05002033 {
2034 png_byte buf[2];
2035
2036 png_crc_read(png_ptr, buf, 2);
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06002037 readbuf[i] = png_get_uint_16(buf);
Guy Schalnat0d580581995-07-20 02:43:20 -05002038 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002039
2040 if (png_crc_finish(png_ptr, 0))
2041 return;
2042
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06002043 png_set_hIST(png_ptr, info_ptr, readbuf);
Guy Schalnat0d580581995-07-20 02:43:20 -05002044}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002045#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002046
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002047#ifdef PNG_READ_pHYs_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002048void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002049png_handle_pHYs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002050{
2051 png_byte buf[9];
2052 png_uint_32 res_x, res_y;
2053 int unit_type;
2054
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002055 png_debug(1, "in png_handle_pHYs");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002056
Guy Schalnate5a37791996-06-05 15:50:50 -05002057 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05002058 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002059
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002060 else if (png_ptr->mode & PNG_HAVE_IDAT)
2061 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002062 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002063 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002064 return;
2065 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002066
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002067 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002068 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002069 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002070 png_chunk_benign_error(png_ptr, "duplicate");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002071 return;
2072 }
Guy Schalnate5a37791996-06-05 15:50:50 -05002073
Guy Schalnat0d580581995-07-20 02:43:20 -05002074 if (length != 9)
2075 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002076 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002077 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnat0d580581995-07-20 02:43:20 -05002078 return;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002079 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002080
2081 png_crc_read(png_ptr, buf, 9);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002082
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002083 if (png_crc_finish(png_ptr, 0))
2084 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05002085
2086 res_x = png_get_uint_32(buf);
2087 res_y = png_get_uint_32(buf + 4);
2088 unit_type = buf[8];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002089 png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
Guy Schalnat0d580581995-07-20 02:43:20 -05002090}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002091#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002092
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002093#ifdef PNG_READ_oFFs_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002094void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002095png_handle_oFFs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002096{
2097 png_byte buf[9];
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002098 png_int_32 offset_x, offset_y;
Guy Schalnat0d580581995-07-20 02:43:20 -05002099 int unit_type;
2100
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002101 png_debug(1, "in png_handle_oFFs");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002102
Guy Schalnate5a37791996-06-05 15:50:50 -05002103 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05002104 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002105
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002106 else if (png_ptr->mode & PNG_HAVE_IDAT)
2107 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002108 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002109 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002110 return;
2111 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002112
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002113 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002114 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002115 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002116 png_chunk_benign_error(png_ptr, "duplicate");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002117 return;
2118 }
Guy Schalnate5a37791996-06-05 15:50:50 -05002119
Guy Schalnat0d580581995-07-20 02:43:20 -05002120 if (length != 9)
2121 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002122 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002123 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnat0d580581995-07-20 02:43:20 -05002124 return;
2125 }
2126
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002127 png_crc_read(png_ptr, buf, 9);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002128
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002129 if (png_crc_finish(png_ptr, 0))
2130 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05002131
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002132 offset_x = png_get_int_32(buf);
2133 offset_y = png_get_int_32(buf + 4);
Guy Schalnat0d580581995-07-20 02:43:20 -05002134 unit_type = buf[8];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002135 png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
2136}
2137#endif
2138
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002139#ifdef PNG_READ_pCAL_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002140/* Read the pCAL chunk (described in the PNG Extensions document) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002141void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002142png_handle_pCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002143{
Andreas Dilger47a0c421997-05-16 02:46:07 -05002144 png_int_32 X0, X1;
2145 png_byte type, nparams;
John Bowlerb5d00512012-03-09 09:15:18 -06002146 png_bytep buffer, buf, units, endptr;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002147 png_charpp params;
2148 int i;
2149
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002150 png_debug(1, "in png_handle_pCAL");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002151
2152 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05002153 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002154
Andreas Dilger47a0c421997-05-16 02:46:07 -05002155 else if (png_ptr->mode & PNG_HAVE_IDAT)
2156 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002157 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002158 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002159 return;
2160 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002161
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002162 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL))
Andreas Dilger47a0c421997-05-16 02:46:07 -05002163 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002164 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002165 png_chunk_benign_error(png_ptr, "duplicate");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002166 return;
2167 }
2168
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002169 png_debug1(2, "Allocating and reading pCAL chunk data (%u bytes)",
2170 length + 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002171
John Bowlerb11b31a2012-03-21 07:55:46 -05002172 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
John Bowlerb5d00512012-03-09 09:15:18 -06002173
2174 if (buffer == NULL)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002175 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002176 png_crc_finish(png_ptr, length);
2177 png_chunk_benign_error(png_ptr, "out of memory");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002178 return;
2179 }
2180
John Bowlerb5d00512012-03-09 09:15:18 -06002181 png_crc_read(png_ptr, buffer, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002182
2183 if (png_crc_finish(png_ptr, 0))
Andreas Dilger47a0c421997-05-16 02:46:07 -05002184 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002185
John Bowlerb5d00512012-03-09 09:15:18 -06002186 buffer[length] = 0; /* Null terminate the last string */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002187
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002188 png_debug(3, "Finding end of pCAL purpose string");
John Bowlerb5d00512012-03-09 09:15:18 -06002189 for (buf = buffer; *buf; buf++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002190 /* Empty loop */ ;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002191
John Bowlerb5d00512012-03-09 09:15:18 -06002192 endptr = buffer + length;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002193
2194 /* We need to have at least 12 bytes after the purpose string
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002195 * in order to get the parameter information.
2196 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002197 if (endptr <= buf + 12)
2198 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002199 png_chunk_benign_error(png_ptr, "invalid");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002200 return;
2201 }
2202
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002203 png_debug(3, "Reading pCAL X0, X1, type, nparams, and units");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002204 X0 = png_get_int_32((png_bytep)buf+1);
2205 X1 = png_get_int_32((png_bytep)buf+5);
2206 type = buf[9];
2207 nparams = buf[10];
2208 units = buf + 11;
2209
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002210 png_debug(3, "Checking pCAL equation type and number of parameters");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002211 /* Check that we have the right number of parameters for known
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002212 * equation types.
2213 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002214 if ((type == PNG_EQUATION_LINEAR && nparams != 2) ||
2215 (type == PNG_EQUATION_BASE_E && nparams != 3) ||
2216 (type == PNG_EQUATION_ARBITRARY && nparams != 3) ||
2217 (type == PNG_EQUATION_HYPERBOLIC && nparams != 4))
2218 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002219 png_chunk_benign_error(png_ptr, "invalid parameter count");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002220 return;
2221 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002222
Andreas Dilger47a0c421997-05-16 02:46:07 -05002223 else if (type >= PNG_EQUATION_LAST)
2224 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002225 png_chunk_benign_error(png_ptr, "unrecognized equation type");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002226 }
2227
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002228 for (buf = units; *buf; buf++)
Glenn Randers-Pehrsonf9f2fe01998-03-15 18:20:23 -06002229 /* Empty loop to move past the units string. */ ;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002230
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002231 png_debug(3, "Allocating pCAL parameters array");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002232
John Bowlerb5d00512012-03-09 09:15:18 -06002233 params = png_voidcast(png_charpp, png_malloc_warn(png_ptr,
2234 nparams * png_sizeof(png_charp)));
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002235
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002236 if (params == NULL)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002237 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002238 png_chunk_benign_error(png_ptr, "out of memory");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002239 return;
2240 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002241
2242 /* Get pointers to the start of each parameter string. */
John Bowlerb5d00512012-03-09 09:15:18 -06002243 for (i = 0; i < nparams; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002244 {
2245 buf++; /* Skip the null string terminator from previous parameter. */
2246
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002247 png_debug1(3, "Reading pCAL parameter %d", i);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002248
John Bowlerb5d00512012-03-09 09:15:18 -06002249 for (params[i] = (png_charp)buf; buf <= endptr && *buf != 0; buf++)
Glenn Randers-Pehrsonf9f2fe01998-03-15 18:20:23 -06002250 /* Empty loop to move past each parameter string */ ;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002251
2252 /* Make sure we haven't run out of data yet */
2253 if (buf > endptr)
2254 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002255 png_free(png_ptr, params);
John Bowlerb11b31a2012-03-21 07:55:46 -05002256 png_chunk_benign_error(png_ptr, "invalid data");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002257 return;
2258 }
2259 }
2260
John Bowlerb5d00512012-03-09 09:15:18 -06002261 png_set_pCAL(png_ptr, info_ptr, (png_charp)buffer, X0, X1, type, nparams,
2262 (png_charp)units, params);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002263
Andreas Dilger47a0c421997-05-16 02:46:07 -05002264 png_free(png_ptr, params);
Guy Schalnat0d580581995-07-20 02:43:20 -05002265}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002266#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002267
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002268#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002269/* Read the sCAL chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002270void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002271png_handle_sCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002272{
John Bowlerb5d00512012-03-09 09:15:18 -06002273 png_bytep buffer;
2274 png_size_t i;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002275 int state;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002276
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002277 png_debug(1, "in png_handle_sCAL");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002278
2279 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05002280 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002281
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002282 else if (png_ptr->mode & PNG_HAVE_IDAT)
2283 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002284 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002285 png_chunk_benign_error(png_ptr, "out of place");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002286 return;
2287 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002288
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002289 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002290 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002291 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002292 png_chunk_benign_error(png_ptr, "duplicate");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002293 return;
2294 }
2295
Glenn Randers-Pehrson254a5132011-06-10 17:45:48 -05002296 /* Need unit type, width, \0, height: minimum 4 bytes */
2297 else if (length < 4)
2298 {
Glenn Randers-Pehrson254a5132011-06-10 17:45:48 -05002299 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002300 png_chunk_benign_error(png_ptr, "invalid");
Glenn Randers-Pehrson254a5132011-06-10 17:45:48 -05002301 return;
2302 }
2303
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002304 png_debug1(2, "Allocating and reading sCAL chunk data (%u bytes)",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002305 length + 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002306
John Bowlerb11b31a2012-03-21 07:55:46 -05002307 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002308
John Bowlerb5d00512012-03-09 09:15:18 -06002309 if (buffer == NULL)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002310 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002311 png_chunk_benign_error(png_ptr, "out of memory");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002312 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002313 return;
2314 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002315
John Bowlerb5d00512012-03-09 09:15:18 -06002316 png_crc_read(png_ptr, buffer, length);
2317 buffer[length] = 0; /* Null terminate the last string */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002318
2319 if (png_crc_finish(png_ptr, 0))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002320 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002321
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002322 /* Validate the unit. */
John Bowlerb5d00512012-03-09 09:15:18 -06002323 if (buffer[0] != 1 && buffer[0] != 2)
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -05002324 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002325 png_chunk_benign_error(png_ptr, "invalid unit");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05002326 return;
2327 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002328
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002329 /* Validate the ASCII numbers, need two ASCII numbers separated by
2330 * a '\0' and they need to fit exactly in the chunk data.
2331 */
Glenn Randers-Pehrson254a5132011-06-10 17:45:48 -05002332 i = 1;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002333 state = 0;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002334
John Bowlerb5d00512012-03-09 09:15:18 -06002335 if (!png_check_fp_number((png_const_charp)buffer, length, &state, &i) ||
2336 i >= length || buffer[i++] != 0)
John Bowlerb11b31a2012-03-21 07:55:46 -05002337 png_chunk_benign_error(png_ptr, "bad width format");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002338
John Bowler8d261262011-06-18 13:37:11 -05002339 else if (!PNG_FP_IS_POSITIVE(state))
John Bowlerb11b31a2012-03-21 07:55:46 -05002340 png_chunk_benign_error(png_ptr, "non-positive width");
John Bowler8d261262011-06-18 13:37:11 -05002341
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002342 else
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -06002343 {
John Bowler168a4332011-01-16 19:32:22 -06002344 png_size_t heighti = i;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002345
Glenn Randers-Pehrson254a5132011-06-10 17:45:48 -05002346 state = 0;
John Bowlerb5d00512012-03-09 09:15:18 -06002347 if (!png_check_fp_number((png_const_charp)buffer, length, &state, &i) ||
2348 i != length)
John Bowlerb11b31a2012-03-21 07:55:46 -05002349 png_chunk_benign_error(png_ptr, "bad height format");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002350
John Bowler8d261262011-06-18 13:37:11 -05002351 else if (!PNG_FP_IS_POSITIVE(state))
John Bowlerb11b31a2012-03-21 07:55:46 -05002352 png_chunk_benign_error(png_ptr, "non-positive height");
John Bowler8d261262011-06-18 13:37:11 -05002353
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002354 else
2355 /* This is the (only) success case. */
John Bowlerb5d00512012-03-09 09:15:18 -06002356 png_set_sCAL_s(png_ptr, info_ptr, buffer[0],
2357 (png_charp)buffer+1, (png_charp)buffer+heighti);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002358 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002359}
2360#endif
2361
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002362#ifdef PNG_READ_tIME_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002363void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002364png_handle_tIME(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002365{
2366 png_byte buf[7];
2367 png_time mod_time;
2368
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002369 png_debug(1, "in png_handle_tIME");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002370
Guy Schalnate5a37791996-06-05 15:50:50 -05002371 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05002372 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002373
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002374 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002375 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002376 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002377 png_chunk_benign_error(png_ptr, "duplicate");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002378 return;
2379 }
2380
2381 if (png_ptr->mode & PNG_HAVE_IDAT)
2382 png_ptr->mode |= PNG_AFTER_IDAT;
Guy Schalnate5a37791996-06-05 15:50:50 -05002383
Guy Schalnat0d580581995-07-20 02:43:20 -05002384 if (length != 7)
2385 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002386 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002387 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnat0d580581995-07-20 02:43:20 -05002388 return;
2389 }
2390
2391 png_crc_read(png_ptr, buf, 7);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002392
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002393 if (png_crc_finish(png_ptr, 0))
2394 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05002395
2396 mod_time.second = buf[6];
2397 mod_time.minute = buf[5];
2398 mod_time.hour = buf[4];
2399 mod_time.day = buf[3];
2400 mod_time.month = buf[2];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002401 mod_time.year = png_get_uint_16(buf);
Guy Schalnat0d580581995-07-20 02:43:20 -05002402
Andreas Dilger47a0c421997-05-16 02:46:07 -05002403 png_set_tIME(png_ptr, info_ptr, &mod_time);
Guy Schalnat0d580581995-07-20 02:43:20 -05002404}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002405#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002406
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002407#ifdef PNG_READ_tEXt_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002408/* Note: this does not properly handle chunks that are > 64K under DOS */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002409void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002410png_handle_tEXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002411{
John Bowlerb5d00512012-03-09 09:15:18 -06002412 png_text text_info;
2413 png_bytep buffer;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002414 png_charp key;
Guy Schalnat6d764711995-12-19 03:22:19 -06002415 png_charp text;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002416 png_uint_32 skip = 0;
2417
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002418 png_debug(1, "in png_handle_tEXt");
Guy Schalnat0d580581995-07-20 02:43:20 -05002419
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06002420#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002421 if (png_ptr->user_chunk_cache_max != 0)
2422 {
2423 if (png_ptr->user_chunk_cache_max == 1)
2424 {
2425 png_crc_finish(png_ptr, length);
2426 return;
2427 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002428
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002429 if (--png_ptr->user_chunk_cache_max == 1)
2430 {
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002431 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002432 png_chunk_benign_error(png_ptr, "no space in chunk cache");
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002433 return;
2434 }
2435 }
2436#endif
2437
Guy Schalnate5a37791996-06-05 15:50:50 -05002438 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05002439 png_chunk_error(png_ptr, "missing IHDR");
Guy Schalnate5a37791996-06-05 15:50:50 -05002440
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002441 if (png_ptr->mode & PNG_HAVE_IDAT)
2442 png_ptr->mode |= PNG_AFTER_IDAT;
2443
Andreas Dilger47a0c421997-05-16 02:46:07 -05002444#ifdef PNG_MAX_MALLOC_64K
John Bowlerb11b31a2012-03-21 07:55:46 -05002445 if (length > 65535U)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002446 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002447 png_crc_finish(png_ptr, length);
2448 png_chunk_benign_error(png_ptr, "too large to fit in memory");
2449 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002450 }
2451#endif
2452
John Bowlerb5d00512012-03-09 09:15:18 -06002453 buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
Glenn Randers-Pehrson97a9b482008-10-25 20:03:28 -05002454
John Bowlerb5d00512012-03-09 09:15:18 -06002455 if (buffer == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002456 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002457 png_chunk_benign_error(png_ptr, "out of memory");
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002458 return;
2459 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002460
John Bowlerb5d00512012-03-09 09:15:18 -06002461 png_crc_read(png_ptr, buffer, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002462
2463 if (png_crc_finish(png_ptr, skip))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002464 return;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002465
John Bowlerb5d00512012-03-09 09:15:18 -06002466 key = (png_charp)buffer;
2467 key[length] = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -05002468
2469 for (text = key; *text; text++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002470 /* Empty loop to find end of key */ ;
Guy Schalnat0d580581995-07-20 02:43:20 -05002471
John Bowler0ae4f7b2012-03-03 21:10:26 -06002472 if (text != key + length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002473 text++;
2474
John Bowlerb5d00512012-03-09 09:15:18 -06002475 text_info.compression = PNG_TEXT_COMPRESSION_NONE;
2476 text_info.key = key;
2477 text_info.lang = NULL;
2478 text_info.lang_key = NULL;
2479 text_info.itxt_length = 0;
2480 text_info.text = text;
2481 text_info.text_length = png_strlen(text);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002482
John Bowlerb5d00512012-03-09 09:15:18 -06002483 if (png_set_text_2(png_ptr, info_ptr, &text_info, 1))
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002484 png_warning(png_ptr, "Insufficient memory to process text chunk");
Guy Schalnat0d580581995-07-20 02:43:20 -05002485}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002486#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002487
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002488#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002489/* Note: this does not correctly handle chunks that are > 64K under DOS */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002490void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002491png_handle_zTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002492{
John Bowler0ae4f7b2012-03-03 21:10:26 -06002493 png_const_charp errmsg = NULL;
John Bowlerb5d00512012-03-09 09:15:18 -06002494 png_bytep buffer;
2495 png_uint_32 keyword_length;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002496
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002497 png_debug(1, "in png_handle_zTXt");
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002498
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06002499#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002500 if (png_ptr->user_chunk_cache_max != 0)
2501 {
2502 if (png_ptr->user_chunk_cache_max == 1)
2503 {
2504 png_crc_finish(png_ptr, length);
2505 return;
2506 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002507
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002508 if (--png_ptr->user_chunk_cache_max == 1)
2509 {
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002510 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002511 png_chunk_benign_error(png_ptr, "no space in chunk cache");
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002512 return;
2513 }
2514 }
2515#endif
2516
Guy Schalnate5a37791996-06-05 15:50:50 -05002517 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05002518 png_chunk_error(png_ptr, "missing IHDR");
Guy Schalnate5a37791996-06-05 15:50:50 -05002519
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002520 if (png_ptr->mode & PNG_HAVE_IDAT)
2521 png_ptr->mode |= PNG_AFTER_IDAT;
2522
John Bowlerb11b31a2012-03-21 07:55:46 -05002523 buffer = png_read_buffer(png_ptr, length, 2/*silent*/);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002524
John Bowlerb5d00512012-03-09 09:15:18 -06002525 if (buffer == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002526 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002527 png_crc_finish(png_ptr, length);
2528 png_chunk_benign_error(png_ptr, "out of memory");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002529 return;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002530 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002531
John Bowlerb5d00512012-03-09 09:15:18 -06002532 png_crc_read(png_ptr, buffer, length);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002533
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002534 if (png_crc_finish(png_ptr, 0))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002535 return;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002536
John Bowler0ae4f7b2012-03-03 21:10:26 -06002537 /* TODO: also check that the keyword contents match the spec! */
2538 for (keyword_length = 0;
John Bowlerb5d00512012-03-09 09:15:18 -06002539 keyword_length < length && buffer[keyword_length] != 0;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002540 ++keyword_length)
2541 /* Empty loop to find end of name */ ;
Guy Schalnat0d580581995-07-20 02:43:20 -05002542
John Bowler0ae4f7b2012-03-03 21:10:26 -06002543 if (keyword_length > 79 || keyword_length < 1)
2544 errmsg = "bad keyword";
Guy Schalnat0d580581995-07-20 02:43:20 -05002545
John Bowler0ae4f7b2012-03-03 21:10:26 -06002546 /* zTXt must have some LZ data after the keyword, although it may expand to
2547 * zero bytes; we need a '\0' at the end of the keyword, the compression type
2548 * then the LZ data:
2549 */
2550 else if (keyword_length + 3 > length)
2551 errmsg = "truncated";
2552
John Bowlerb5d00512012-03-09 09:15:18 -06002553 else if (buffer[keyword_length+1] != PNG_COMPRESSION_TYPE_BASE)
John Bowler0ae4f7b2012-03-03 21:10:26 -06002554 errmsg = "unknown compression type";
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002555
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002556 else
Guy Schalnat0d580581995-07-20 02:43:20 -05002557 {
John Bowler0ae4f7b2012-03-03 21:10:26 -06002558 png_alloc_size_t uncompressed_length = PNG_SIZE_MAX;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002559
John Bowler0ae4f7b2012-03-03 21:10:26 -06002560 /* TODO: at present png_decompress_chunk imposes a single application
2561 * level memory limit, this should be split to different values for iCCP
2562 * and text chunks.
2563 */
John Bowlerb5d00512012-03-09 09:15:18 -06002564 if (png_decompress_chunk(png_ptr, length, keyword_length+2,
2565 &uncompressed_length, 1/*terminate*/) == Z_STREAM_END)
John Bowler0ae4f7b2012-03-03 21:10:26 -06002566 {
2567 png_text text;
2568
John Bowlerb5d00512012-03-09 09:15:18 -06002569 /* It worked; png_ptr->read_buffer now looks like a tEXt chunk except
2570 * for the extra compression type byte and the fact that it isn't
John Bowler0ae4f7b2012-03-03 21:10:26 -06002571 * necessarily '\0' terminated.
2572 */
John Bowlerb5d00512012-03-09 09:15:18 -06002573 buffer = png_ptr->read_buffer;
2574 buffer[uncompressed_length+(keyword_length+2)] = 0;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002575
2576 text.compression = PNG_TEXT_COMPRESSION_zTXt;
John Bowlerb5d00512012-03-09 09:15:18 -06002577 text.key = (png_charp)buffer;
2578 text.text = (png_charp)(buffer + keyword_length+2);
John Bowler0ae4f7b2012-03-03 21:10:26 -06002579 text.text_length = uncompressed_length;
2580 text.itxt_length = 0;
2581 text.lang = NULL;
2582 text.lang_key = NULL;
2583
2584 if (png_set_text_2(png_ptr, info_ptr, &text, 1))
John Bowlerb11b31a2012-03-21 07:55:46 -05002585 errmsg = "insufficient memory";
John Bowler0ae4f7b2012-03-03 21:10:26 -06002586 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002587
John Bowlerb5d00512012-03-09 09:15:18 -06002588 else
2589 errmsg = png_ptr->zstream.msg;
2590 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002591
John Bowler0ae4f7b2012-03-03 21:10:26 -06002592 if (errmsg != NULL)
2593 png_chunk_benign_error(png_ptr, errmsg);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002594}
2595#endif
2596
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002597#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002598/* Note: this does not correctly handle chunks that are > 64K under DOS */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002599void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002600png_handle_iTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002601{
John Bowler0ae4f7b2012-03-03 21:10:26 -06002602 png_const_charp errmsg = NULL;
John Bowlerb5d00512012-03-09 09:15:18 -06002603 png_bytep buffer;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002604 png_uint_32 prefix_length;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002605
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002606 png_debug(1, "in png_handle_iTXt");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002607
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06002608#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002609 if (png_ptr->user_chunk_cache_max != 0)
2610 {
2611 if (png_ptr->user_chunk_cache_max == 1)
2612 {
2613 png_crc_finish(png_ptr, length);
2614 return;
2615 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002616
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002617 if (--png_ptr->user_chunk_cache_max == 1)
2618 {
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002619 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002620 png_chunk_benign_error(png_ptr, "no space in chunk cache");
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002621 return;
2622 }
2623 }
2624#endif
2625
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002626 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05002627 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002628
2629 if (png_ptr->mode & PNG_HAVE_IDAT)
2630 png_ptr->mode |= PNG_AFTER_IDAT;
2631
John Bowlerb5d00512012-03-09 09:15:18 -06002632 buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002633
John Bowlerb5d00512012-03-09 09:15:18 -06002634 if (buffer == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002635 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002636 png_crc_finish(png_ptr, length);
2637 png_chunk_benign_error(png_ptr, "out of memory");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002638 return;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002639 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002640
John Bowlerb5d00512012-03-09 09:15:18 -06002641 png_crc_read(png_ptr, buffer, length);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002642
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002643 if (png_crc_finish(png_ptr, 0))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002644 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002645
John Bowler0ae4f7b2012-03-03 21:10:26 -06002646 /* First the keyword. */
2647 for (prefix_length=0;
John Bowlerb5d00512012-03-09 09:15:18 -06002648 prefix_length < length && buffer[prefix_length] != 0;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002649 ++prefix_length)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002650 /* Empty loop */ ;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002651
John Bowler0ae4f7b2012-03-03 21:10:26 -06002652 /* Perform a basic check on the keyword length here. */
2653 if (prefix_length > 79 || prefix_length < 1)
2654 errmsg = "bad keyword";
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002655
John Bowler0ae4f7b2012-03-03 21:10:26 -06002656 /* Expect keyword, compression flag, compression type, language, translated
2657 * keyword (both may be empty but are 0 terminated) then the text, which may
2658 * be empty.
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002659 */
John Bowler0ae4f7b2012-03-03 21:10:26 -06002660 else if (prefix_length + 5 > length)
2661 errmsg = "truncated";
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002662
John Bowlerb5d00512012-03-09 09:15:18 -06002663 else if (buffer[prefix_length+1] == 0 ||
2664 (buffer[prefix_length+1] == 1 &&
2665 buffer[prefix_length+2] == PNG_COMPRESSION_TYPE_BASE))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002666 {
John Bowlerb5d00512012-03-09 09:15:18 -06002667 int compressed = buffer[prefix_length+1] != 0;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002668 png_uint_32 language_offset, translated_keyword_offset;
2669 png_alloc_size_t uncompressed_length = 0;
2670
2671 /* Now the language tag */
2672 prefix_length += 3;
2673 language_offset = prefix_length;
2674
John Bowlerb5d00512012-03-09 09:15:18 -06002675 for (; prefix_length < length && buffer[prefix_length] != 0;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002676 ++prefix_length)
2677 /* Empty loop */ ;
2678
2679 /* WARNING: the length may be invalid here, this is checked below. */
2680 translated_keyword_offset = ++prefix_length;
2681
John Bowlerb5d00512012-03-09 09:15:18 -06002682 for (; prefix_length < length && buffer[prefix_length] != 0;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002683 ++prefix_length)
2684 /* Empty loop */ ;
2685
2686 /* prefix_length should now be at the trailing '\0' of the translated
2687 * keyword, but it may already be over the end. None of this arithmetic
2688 * can overflow because chunks are at most 2^31 bytes long, but on 16-bit
2689 * systems the available allocaton may overflow.
2690 */
2691 ++prefix_length;
2692
2693 if (!compressed && prefix_length <= length)
2694 uncompressed_length = length - prefix_length;
2695
2696 else if (compressed && prefix_length < length)
2697 {
2698 uncompressed_length = PNG_SIZE_MAX;
2699
2700 /* TODO: at present png_decompress_chunk imposes a single application
2701 * level memory limit, this should be split to different values for
2702 * iCCP and text chunks.
2703 */
John Bowlerb5d00512012-03-09 09:15:18 -06002704 if (png_decompress_chunk(png_ptr, length, prefix_length,
2705 &uncompressed_length, 1/*terminate*/) == Z_STREAM_END)
2706 buffer = png_ptr->read_buffer;
2707
2708 else
2709 errmsg = png_ptr->zstream.msg;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002710 }
2711
2712 else
2713 errmsg = "truncated";
2714
2715 if (errmsg == NULL)
2716 {
2717 png_text text;
2718
John Bowlerb5d00512012-03-09 09:15:18 -06002719 buffer[uncompressed_length+prefix_length] = 0;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002720
2721 if (compressed)
2722 text.compression = PNG_ITXT_COMPRESSION_NONE;
2723
2724 else
2725 text.compression = PNG_ITXT_COMPRESSION_zTXt;
2726
John Bowlerb5d00512012-03-09 09:15:18 -06002727 text.key = (png_charp)buffer;
2728 text.lang = (png_charp)buffer + language_offset;
2729 text.lang_key = (png_charp)buffer + translated_keyword_offset;
2730 text.text = (png_charp)buffer + prefix_length;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002731 text.text_length = 0;
2732 text.itxt_length = uncompressed_length;
2733
2734 if (png_set_text_2(png_ptr, info_ptr, &text, 1))
2735 errmsg = "insufficient memory";
2736 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002737 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002738
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002739 else
John Bowler0ae4f7b2012-03-03 21:10:26 -06002740 errmsg = "bad compression info";
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002741
John Bowler0ae4f7b2012-03-03 21:10:26 -06002742 if (errmsg != NULL)
2743 png_chunk_benign_error(png_ptr, errmsg);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002744}
2745#endif
2746
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002747/* This function is called when we haven't found a handler for a
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002748 * chunk. If there isn't a problem with the chunk itself (ie bad
2749 * chunk name, CRC, or a critical chunk), the chunk is silently ignored
2750 * -- unless the PNG_FLAG_UNKNOWN_CHUNKS_SUPPORTED flag is on in which
2751 * case it will be saved away to be written out later.
2752 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002753void /* PRIVATE */
John Bowlerb11b31a2012-03-21 07:55:46 -05002754png_handle_unknown(png_structrp png_ptr, png_inforp info_ptr,
2755 png_uint_32 length)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002756{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002757 png_uint_32 skip = 0;
2758
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002759 png_debug(1, "in png_handle_unknown");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002760
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06002761#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002762 if (png_ptr->user_chunk_cache_max != 0)
2763 {
2764 if (png_ptr->user_chunk_cache_max == 1)
2765 {
2766 png_crc_finish(png_ptr, length);
2767 return;
2768 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002769
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002770 if (--png_ptr->user_chunk_cache_max == 1)
2771 {
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002772 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002773 png_chunk_benign_error(png_ptr, "no space in chunk cache");
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002774 return;
2775 }
2776 }
2777#endif
2778
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002779 if (png_ptr->mode & PNG_HAVE_IDAT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002780 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002781 if (png_ptr->chunk_name != png_IDAT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002782 png_ptr->mode |= PNG_AFTER_IDAT;
2783 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002784
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002785 if (PNG_CHUNK_CRITICAL(png_ptr->chunk_name))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002786 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002787#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002788 if (png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name) !=
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002789 PNG_HANDLE_CHUNK_ALWAYS
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002790#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002791 && png_ptr->read_user_chunk_fn == NULL
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002792#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002793 )
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05002794#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002795 png_chunk_error(png_ptr, "unknown critical chunk");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002796 }
2797
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002798#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson7824a702009-06-13 10:05:05 -05002799 if ((png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS)
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002800#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
Glenn Randers-Pehrson7824a702009-06-13 10:05:05 -05002801 || (png_ptr->read_user_chunk_fn != NULL)
2802#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002803 )
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002804 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002805#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002806 if (length > 65535)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002807 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002808 png_crc_finish(png_ptr, length);
2809 png_chunk_benign_error(png_ptr,
2810 "unknown chunk too large to fit in memory");
2811 return;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002812 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002813#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002814
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002815 /* TODO: this code is very close to the unknown handling in pngpread.c,
2816 * maybe it can be put into a common utility routine?
2817 * png_struct::unknown_chunk is just used as a temporary variable, along
2818 * with the data into which the chunk is read. These can be eliminated.
2819 */
2820 PNG_CSTRING_FROM_CHUNK(png_ptr->unknown_chunk.name, png_ptr->chunk_name);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002821 png_ptr->unknown_chunk.size = (png_size_t)length;
2822
2823 if (length == 0)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002824 png_ptr->unknown_chunk.data = NULL;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002825
2826 else
2827 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002828 png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr, length);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002829 png_crc_read(png_ptr, png_ptr->unknown_chunk.data, length);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002830 }
2831
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002832#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002833 if (png_ptr->read_user_chunk_fn != NULL)
2834 {
2835 /* Callback to user unknown chunk handler */
2836 int ret;
2837
2838 ret = (*(png_ptr->read_user_chunk_fn))
2839 (png_ptr, &png_ptr->unknown_chunk);
2840
2841 if (ret < 0)
2842 png_chunk_error(png_ptr, "error in user chunk");
2843
2844 if (ret == 0)
2845 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002846 if (PNG_CHUNK_CRITICAL(png_ptr->chunk_name))
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002847 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002848#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002849 if (png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name) !=
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002850 PNG_HANDLE_CHUNK_ALWAYS)
Glenn Randers-Pehrson7824a702009-06-13 10:05:05 -05002851#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002852 png_chunk_error(png_ptr, "unknown critical chunk");
2853 }
2854
2855 png_set_unknown_chunks(png_ptr, info_ptr,
2856 &png_ptr->unknown_chunk, 1);
2857 }
2858 }
2859
2860 else
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002861#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002862 png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1);
2863
2864 png_free(png_ptr, png_ptr->unknown_chunk.data);
2865 png_ptr->unknown_chunk.data = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002866 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002867
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002868 else
2869#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002870 skip = length;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002871
2872 png_crc_finish(png_ptr, skip);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002873
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -05002874#ifndef PNG_READ_USER_CHUNKS_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002875 PNG_UNUSED(info_ptr) /* Quiet compiler warnings about unused info_ptr */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002876#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002877}
2878
2879/* This function is called to verify that a chunk name is valid.
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002880 * This function can't have the "critical chunk check" incorporated
2881 * into it, since in the future we will need to be able to call user
2882 * functions to handle unknown critical chunks after we check that
2883 * the chunk name itself is valid.
2884 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002885
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002886/* Bit hacking: the test for an invalid byte in the 4 byte chunk name is:
2887 *
2888 * ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
2889 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002890
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002891void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002892png_check_chunk_name(png_structrp png_ptr, png_uint_32 chunk_name)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002893{
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002894 int i;
2895
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002896 png_debug(1, "in png_check_chunk_name");
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002897
2898 for (i=1; i<=4; ++i)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002899 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002900 int c = chunk_name & 0xff;
2901
2902 if (c < 65 || c > 122 || (c > 90 && c < 97))
2903 png_chunk_error(png_ptr, "invalid chunk type");
2904
2905 chunk_name >>= 8;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002906 }
2907}
2908
John Bowler7875d532011-11-07 22:33:49 -06002909/* Combines the row recently read in with the existing pixels in the row. This
2910 * routine takes care of alpha and transparency if requested. This routine also
2911 * handles the two methods of progressive display of interlaced images,
2912 * depending on the 'display' value; if 'display' is true then the whole row
2913 * (dp) is filled from the start by replicating the available pixels. If
2914 * 'display' is false only those pixels present in the pass are filled in.
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002915 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002916void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002917png_combine_row(png_const_structrp png_ptr, png_bytep dp, int display)
Guy Schalnat0d580581995-07-20 02:43:20 -05002918{
John Bowler4e68aa72011-10-11 16:01:33 -05002919 unsigned int pixel_depth = png_ptr->transformed_pixel_depth;
2920 png_const_bytep sp = png_ptr->row_buf + 1;
John Bowlerac8375d2011-10-06 22:27:16 -05002921 png_uint_32 row_width = png_ptr->width;
John Bowler4e68aa72011-10-11 16:01:33 -05002922 unsigned int pass = png_ptr->pass;
John Bowlerfb5b3ac2011-10-16 22:52:56 -05002923 png_bytep end_ptr = 0;
2924 png_byte end_byte = 0;
2925 unsigned int end_mask;
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002926
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002927 png_debug(1, "in png_combine_row");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002928
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002929 /* Added in 1.5.6: it should not be possible to enter this routine until at
2930 * least one row has been read from the PNG data and transformed.
2931 */
2932 if (pixel_depth == 0)
2933 png_error(png_ptr, "internal row logic error");
2934
2935 /* Added in 1.5.4: the pixel depth should match the information returned by
2936 * any call to png_read_update_info at this point. Do not continue if we got
John Bowler9994f252011-05-15 18:52:39 -05002937 * this wrong.
2938 */
2939 if (png_ptr->info_rowbytes != 0 && png_ptr->info_rowbytes !=
John Bowlerac8375d2011-10-06 22:27:16 -05002940 PNG_ROWBYTES(pixel_depth, row_width))
John Bowler9994f252011-05-15 18:52:39 -05002941 png_error(png_ptr, "internal row size calculation error");
2942
John Bowlerac8375d2011-10-06 22:27:16 -05002943 /* Don't expect this to ever happen: */
2944 if (row_width == 0)
2945 png_error(png_ptr, "internal row width error");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002946
John Bowlerfb5b3ac2011-10-16 22:52:56 -05002947 /* Preserve the last byte in cases where only part of it will be overwritten,
2948 * the multiply below may overflow, we don't care because ANSI-C guarantees
2949 * we get the low bits.
2950 */
2951 end_mask = (pixel_depth * row_width) & 7;
2952 if (end_mask != 0)
2953 {
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05002954 /* end_ptr == NULL is a flag to say do nothing */
John Bowlerfb5b3ac2011-10-16 22:52:56 -05002955 end_ptr = dp + PNG_ROWBYTES(pixel_depth, row_width) - 1;
2956 end_byte = *end_ptr;
2957# ifdef PNG_READ_PACKSWAP_SUPPORTED
2958 if (png_ptr->transformations & PNG_PACKSWAP) /* little-endian byte */
2959 end_mask = 0xff << end_mask;
2960
2961 else /* big-endian byte */
2962# endif
2963 end_mask = 0xff >> end_mask;
2964 /* end_mask is now the bits to *keep* from the destination row */
2965 }
2966
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05002967 /* For non-interlaced images this reduces to a png_memcpy(). A png_memcpy()
2968 * will also happen if interlacing isn't supported or if the application
2969 * does not call png_set_interlace_handling(). In the latter cases the
2970 * caller just gets a sequence of the unexpanded rows from each interlace
2971 * pass.
John Bowlerac8375d2011-10-06 22:27:16 -05002972 */
2973#ifdef PNG_READ_INTERLACING_SUPPORTED
2974 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE) &&
John Bowler4e68aa72011-10-11 16:01:33 -05002975 pass < 6 && (display == 0 ||
2976 /* The following copies everything for 'display' on passes 0, 2 and 4. */
2977 (display == 1 && (pass & 1) != 0)))
Guy Schalnat0d580581995-07-20 02:43:20 -05002978 {
John Bowler4e68aa72011-10-11 16:01:33 -05002979 /* Narrow images may have no bits in a pass; the caller should handle
2980 * this, but this test is cheap:
John Bowlerac8375d2011-10-06 22:27:16 -05002981 */
John Bowler4e68aa72011-10-11 16:01:33 -05002982 if (row_width <= PNG_PASS_START_COL(pass))
2983 return;
John Bowlerac8375d2011-10-06 22:27:16 -05002984
John Bowler4e68aa72011-10-11 16:01:33 -05002985 if (pixel_depth < 8)
Guy Schalnat0d580581995-07-20 02:43:20 -05002986 {
John Bowler7875d532011-11-07 22:33:49 -06002987 /* For pixel depths up to 4 bpp the 8-pixel mask can be expanded to fit
John Bowler4e68aa72011-10-11 16:01:33 -05002988 * into 32 bits, then a single loop over the bytes using the four byte
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05002989 * values in the 32-bit mask can be used. For the 'display' option the
John Bowler4e68aa72011-10-11 16:01:33 -05002990 * expanded mask may also not require any masking within a byte. To
2991 * make this work the PACKSWAP option must be taken into account - it
2992 * simply requires the pixels to be reversed in each byte.
2993 *
2994 * The 'regular' case requires a mask for each of the first 6 passes,
2995 * the 'display' case does a copy for the even passes in the range
John Bowler7875d532011-11-07 22:33:49 -06002996 * 0..6. This has already been handled in the test above.
John Bowler4e68aa72011-10-11 16:01:33 -05002997 *
2998 * The masks are arranged as four bytes with the first byte to use in
2999 * the lowest bits (little-endian) regardless of the order (PACKSWAP or
3000 * not) of the pixels in each byte.
3001 *
3002 * NOTE: the whole of this logic depends on the caller of this function
3003 * only calling it on rows appropriate to the pass. This function only
John Bowler7875d532011-11-07 22:33:49 -06003004 * understands the 'x' logic; the 'y' logic is handled by the caller.
John Bowler4e68aa72011-10-11 16:01:33 -05003005 *
3006 * The following defines allow generation of compile time constant bit
3007 * masks for each pixel depth and each possibility of swapped or not
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003008 * swapped bytes. Pass 'p' is in the range 0..6; 'x', a pixel index,
3009 * is in the range 0..7; and the result is 1 if the pixel is to be
3010 * copied in the pass, 0 if not. 'S' is for the sparkle method, 'B'
3011 * for the block method.
3012 *
John Bowler92ef3132011-10-27 19:36:08 -05003013 * With some compilers a compile time expression of the general form:
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05003014 *
John Bowler92ef3132011-10-27 19:36:08 -05003015 * (shift >= 32) ? (a >> (shift-32)) : (b >> shift)
3016 *
3017 * Produces warnings with values of 'shift' in the range 33 to 63
Glenn Randers-Pehrson18763662011-10-27 22:09:22 -05003018 * because the right hand side of the ?: expression is evaluated by
John Bowler92ef3132011-10-27 19:36:08 -05003019 * the compiler even though it isn't used. Microsoft Visual C (various
3020 * versions) and the Intel C compiler are known to do this. To avoid
3021 * this the following macros are used in 1.5.6. This is a temporary
John Bowler7875d532011-11-07 22:33:49 -06003022 * solution to avoid destabilizing the code during the release process.
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05003023 */
John Bowler92ef3132011-10-27 19:36:08 -05003024# if PNG_USE_COMPILE_TIME_MASKS
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05003025# define PNG_LSR(x,s) ((x)>>((s) & 0x1f))
3026# define PNG_LSL(x,s) ((x)<<((s) & 0x1f))
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003027# else
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05003028# define PNG_LSR(x,s) ((x)>>(s))
3029# define PNG_LSL(x,s) ((x)<<(s))
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003030# endif
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05003031# define S_COPY(p,x) (((p)<4 ? PNG_LSR(0x80088822,(3-(p))*8+(7-(x))) :\
3032 PNG_LSR(0xaa55ff00,(7-(p))*8+(7-(x)))) & 1)
3033# define B_COPY(p,x) (((p)<4 ? PNG_LSR(0xff0fff33,(3-(p))*8+(7-(x))) :\
3034 PNG_LSR(0xff55ff00,(7-(p))*8+(7-(x)))) & 1)
Guy Schalnat0d580581995-07-20 02:43:20 -05003035
John Bowler4e68aa72011-10-11 16:01:33 -05003036 /* Return a mask for pass 'p' pixel 'x' at depth 'd'. The mask is
3037 * little endian - the first pixel is at bit 0 - however the extra
3038 * parameter 's' can be set to cause the mask position to be swapped
3039 * within each byte, to match the PNG format. This is done by XOR of
3040 * the shift with 7, 6 or 4 for bit depths 1, 2 and 4.
3041 */
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05003042# define PIXEL_MASK(p,x,d,s) \
3043 (PNG_LSL(((PNG_LSL(1U,(d)))-1),(((x)*(d))^((s)?8-(d):0))))
John Bowlerac8375d2011-10-06 22:27:16 -05003044
John Bowler4e68aa72011-10-11 16:01:33 -05003045 /* Hence generate the appropriate 'block' or 'sparkle' pixel copy mask.
3046 */
3047# define S_MASKx(p,x,d,s) (S_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
3048# 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 -05003049
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003050 /* Combine 8 of these to get the full mask. For the 1-bpp and 2-bpp
3051 * cases the result needs replicating, for the 4-bpp case the above
3052 * generates a full 32 bits.
John Bowler4e68aa72011-10-11 16:01:33 -05003053 */
3054# define MASK_EXPAND(m,d) ((m)*((d)==1?0x01010101:((d)==2?0x00010001:1)))
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003055
John Bowler4e68aa72011-10-11 16:01:33 -05003056# define S_MASK(p,d,s) MASK_EXPAND(S_MASKx(p,0,d,s) + S_MASKx(p,1,d,s) +\
3057 S_MASKx(p,2,d,s) + S_MASKx(p,3,d,s) + S_MASKx(p,4,d,s) +\
3058 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 -06003059
John Bowler4e68aa72011-10-11 16:01:33 -05003060# define B_MASK(p,d,s) MASK_EXPAND(B_MASKx(p,0,d,s) + B_MASKx(p,1,d,s) +\
3061 B_MASKx(p,2,d,s) + B_MASKx(p,3,d,s) + B_MASKx(p,4,d,s) +\
3062 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 -05003063
John Bowler4e68aa72011-10-11 16:01:33 -05003064#if PNG_USE_COMPILE_TIME_MASKS
3065 /* Utility macros to construct all the masks for a depth/swap
3066 * combination. The 's' parameter says whether the format is PNG
John Bowler7875d532011-11-07 22:33:49 -06003067 * (big endian bytes) or not. Only the three odd-numbered passes are
John Bowler4e68aa72011-10-11 16:01:33 -05003068 * required for the display/block algorithm.
3069 */
3070# define S_MASKS(d,s) { S_MASK(0,d,s), S_MASK(1,d,s), S_MASK(2,d,s),\
3071 S_MASK(3,d,s), S_MASK(4,d,s), S_MASK(5,d,s) }
John Bowlerac8375d2011-10-06 22:27:16 -05003072
John Bowler4e68aa72011-10-11 16:01:33 -05003073# 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 -05003074
John Bowler4e68aa72011-10-11 16:01:33 -05003075# define DEPTH_INDEX(d) ((d)==1?0:((d)==2?1:2))
John Bowlerac8375d2011-10-06 22:27:16 -05003076
John Bowler4e68aa72011-10-11 16:01:33 -05003077 /* Hence the pre-compiled masks indexed by PACKSWAP (or not), depth and
3078 * then pass:
3079 */
John Bowler7875d532011-11-07 22:33:49 -06003080 static PNG_CONST png_uint_32 row_mask[2/*PACKSWAP*/][3/*depth*/][6] =
3081 {
John Bowler4e68aa72011-10-11 16:01:33 -05003082 /* Little-endian byte masks for PACKSWAP */
3083 { S_MASKS(1,0), S_MASKS(2,0), S_MASKS(4,0) },
3084 /* Normal (big-endian byte) masks - PNG format */
3085 { S_MASKS(1,1), S_MASKS(2,1), S_MASKS(4,1) }
3086 };
John Bowlerac8375d2011-10-06 22:27:16 -05003087
John Bowler4e68aa72011-10-11 16:01:33 -05003088 /* display_mask has only three entries for the odd passes, so index by
3089 * pass>>1.
3090 */
John Bowler7875d532011-11-07 22:33:49 -06003091 static PNG_CONST png_uint_32 display_mask[2][3][3] =
3092 {
John Bowler4e68aa72011-10-11 16:01:33 -05003093 /* Little-endian byte masks for PACKSWAP */
3094 { B_MASKS(1,0), B_MASKS(2,0), B_MASKS(4,0) },
3095 /* Normal (big-endian byte) masks - PNG format */
3096 { B_MASKS(1,1), B_MASKS(2,1), B_MASKS(4,1) }
3097 };
John Bowlerac8375d2011-10-06 22:27:16 -05003098
John Bowler4e68aa72011-10-11 16:01:33 -05003099# define MASK(pass,depth,display,png)\
3100 ((display)?display_mask[png][DEPTH_INDEX(depth)][pass>>1]:\
3101 row_mask[png][DEPTH_INDEX(depth)][pass])
John Bowlerac8375d2011-10-06 22:27:16 -05003102
John Bowler4e68aa72011-10-11 16:01:33 -05003103#else /* !PNG_USE_COMPILE_TIME_MASKS */
3104 /* This is the runtime alternative: it seems unlikely that this will
3105 * ever be either smaller or faster than the compile time approach.
3106 */
3107# define MASK(pass,depth,display,png)\
3108 ((display)?B_MASK(pass,depth,png):S_MASK(pass,depth,png))
3109#endif /* !PNG_USE_COMPILE_TIME_MASKS */
John Bowlerac8375d2011-10-06 22:27:16 -05003110
John Bowler4e68aa72011-10-11 16:01:33 -05003111 /* Use the appropriate mask to copy the required bits. In some cases
3112 * the byte mask will be 0 or 0xff, optimize these cases. row_width is
3113 * the number of pixels, but the code copies bytes, so it is necessary
3114 * to special case the end.
3115 */
3116 png_uint_32 pixels_per_byte = 8 / pixel_depth;
3117 png_uint_32 mask;
John Bowlerac8375d2011-10-06 22:27:16 -05003118
John Bowler4e68aa72011-10-11 16:01:33 -05003119# ifdef PNG_READ_PACKSWAP_SUPPORTED
3120 if (png_ptr->transformations & PNG_PACKSWAP)
3121 mask = MASK(pass, pixel_depth, display, 0);
John Bowlerac8375d2011-10-06 22:27:16 -05003122
3123 else
John Bowler4e68aa72011-10-11 16:01:33 -05003124# endif
3125 mask = MASK(pass, pixel_depth, display, 1);
3126
3127 for (;;)
3128 {
3129 png_uint_32 m;
3130
3131 /* It doesn't matter in the following if png_uint_32 has more than
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003132 * 32 bits because the high bits always match those in m<<24; it is,
John Bowler4e68aa72011-10-11 16:01:33 -05003133 * however, essential to use OR here, not +, because of this.
3134 */
3135 m = mask;
3136 mask = (m >> 8) | (m << 24); /* rotate right to good compilers */
3137 m &= 0xff;
3138
3139 if (m != 0) /* something to copy */
John Bowlerac8375d2011-10-06 22:27:16 -05003140 {
John Bowler4e68aa72011-10-11 16:01:33 -05003141 if (m != 0xff)
3142 *dp = (png_byte)((*dp & ~m) | (*sp & m));
3143 else
3144 *dp = *sp;
John Bowlerac8375d2011-10-06 22:27:16 -05003145 }
John Bowler4e68aa72011-10-11 16:01:33 -05003146
3147 /* NOTE: this may overwrite the last byte with garbage if the image
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003148 * is not an exact number of bytes wide; libpng has always done
John Bowler4e68aa72011-10-11 16:01:33 -05003149 * this.
3150 */
3151 if (row_width <= pixels_per_byte)
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003152 break; /* May need to restore part of the last byte */
John Bowler4e68aa72011-10-11 16:01:33 -05003153
3154 row_width -= pixels_per_byte;
3155 ++dp;
3156 ++sp;
3157 }
3158 }
3159
3160 else /* pixel_depth >= 8 */
3161 {
3162 unsigned int bytes_to_copy, bytes_to_jump;
3163
3164 /* Validate the depth - it must be a multiple of 8 */
3165 if (pixel_depth & 7)
3166 png_error(png_ptr, "invalid user transform pixel depth");
3167
3168 pixel_depth >>= 3; /* now in bytes */
3169 row_width *= pixel_depth;
3170
3171 /* Regardless of pass number the Adam 7 interlace always results in a
3172 * fixed number of pixels to copy then to skip. There may be a
3173 * different number of pixels to skip at the start though.
3174 */
3175 {
3176 unsigned int offset = PNG_PASS_START_COL(pass) * pixel_depth;
3177
3178 row_width -= offset;
3179 dp += offset;
3180 sp += offset;
John Bowlerac8375d2011-10-06 22:27:16 -05003181 }
3182
John Bowler4e68aa72011-10-11 16:01:33 -05003183 /* Work out the bytes to copy. */
3184 if (display)
3185 {
3186 /* When doing the 'block' algorithm the pixel in the pass gets
3187 * replicated to adjacent pixels. This is why the even (0,2,4,6)
3188 * passes are skipped above - the entire expanded row is copied.
3189 */
3190 bytes_to_copy = (1<<((6-pass)>>1)) * pixel_depth;
3191
3192 /* But don't allow this number to exceed the actual row width. */
3193 if (bytes_to_copy > row_width)
3194 bytes_to_copy = row_width;
3195 }
3196
3197 else /* normal row; Adam7 only ever gives us one pixel to copy. */
3198 bytes_to_copy = pixel_depth;
3199
3200 /* In Adam7 there is a constant offset between where the pixels go. */
3201 bytes_to_jump = PNG_PASS_COL_OFFSET(pass) * pixel_depth;
3202
3203 /* And simply copy these bytes. Some optimization is possible here,
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003204 * depending on the value of 'bytes_to_copy'. Special case the low
John Bowler4e68aa72011-10-11 16:01:33 -05003205 * byte counts, which we know to be frequent.
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003206 *
3207 * Notice that these cases all 'return' rather than 'break' - this
3208 * avoids an unnecessary test on whether to restore the last byte
3209 * below.
John Bowler4e68aa72011-10-11 16:01:33 -05003210 */
3211 switch (bytes_to_copy)
3212 {
3213 case 1:
3214 for (;;)
3215 {
3216 *dp = *sp;
3217
3218 if (row_width <= bytes_to_jump)
3219 return;
3220
3221 dp += bytes_to_jump;
3222 sp += bytes_to_jump;
3223 row_width -= bytes_to_jump;
3224 }
3225
3226 case 2:
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003227 /* There is a possibility of a partial copy at the end here; this
John Bowler4e68aa72011-10-11 16:01:33 -05003228 * slows the code down somewhat.
3229 */
3230 do
3231 {
3232 dp[0] = sp[0], dp[1] = sp[1];
3233
3234 if (row_width <= bytes_to_jump)
3235 return;
3236
3237 sp += bytes_to_jump;
3238 dp += bytes_to_jump;
3239 row_width -= bytes_to_jump;
3240 }
3241 while (row_width > 1);
3242
3243 /* And there can only be one byte left at this point: */
3244 *dp = *sp;
3245 return;
3246
3247 case 3:
3248 /* This can only be the RGB case, so each copy is exactly one
3249 * pixel and it is not necessary to check for a partial copy.
3250 */
3251 for(;;)
3252 {
3253 dp[0] = sp[0], dp[1] = sp[1], dp[2] = sp[2];
3254
3255 if (row_width <= bytes_to_jump)
3256 return;
3257
3258 sp += bytes_to_jump;
3259 dp += bytes_to_jump;
3260 row_width -= bytes_to_jump;
3261 }
3262
3263 default:
3264#if PNG_ALIGN_TYPE != PNG_ALIGN_NONE
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003265 /* Check for double byte alignment and, if possible, use a
3266 * 16-bit copy. Don't attempt this for narrow images - ones that
John Bowler4e68aa72011-10-11 16:01:33 -05003267 * are less than an interlace panel wide. Don't attempt it for
John Bowler7875d532011-11-07 22:33:49 -06003268 * wide bytes_to_copy either - use the png_memcpy there.
John Bowler4e68aa72011-10-11 16:01:33 -05003269 */
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003270 if (bytes_to_copy < 16 /*else use png_memcpy*/ &&
John Bowler4e68aa72011-10-11 16:01:33 -05003271 png_isaligned(dp, png_uint_16) &&
3272 png_isaligned(sp, png_uint_16) &&
3273 bytes_to_copy % sizeof (png_uint_16) == 0 &&
3274 bytes_to_jump % sizeof (png_uint_16) == 0)
3275 {
3276 /* Everything is aligned for png_uint_16 copies, but try for
3277 * png_uint_32 first.
3278 */
3279 if (png_isaligned(dp, png_uint_32) &&
3280 png_isaligned(sp, png_uint_32) &&
3281 bytes_to_copy % sizeof (png_uint_32) == 0 &&
3282 bytes_to_jump % sizeof (png_uint_32) == 0)
3283 {
3284 png_uint_32p dp32 = (png_uint_32p)dp;
3285 png_const_uint_32p sp32 = (png_const_uint_32p)sp;
3286 unsigned int skip = (bytes_to_jump-bytes_to_copy) /
3287 sizeof (png_uint_32);
3288
3289 do
3290 {
3291 size_t c = bytes_to_copy;
3292 do
3293 {
3294 *dp32++ = *sp32++;
3295 c -= sizeof (png_uint_32);
3296 }
3297 while (c > 0);
3298
3299 if (row_width <= bytes_to_jump)
3300 return;
3301
3302 dp32 += skip;
3303 sp32 += skip;
3304 row_width -= bytes_to_jump;
3305 }
3306 while (bytes_to_copy <= row_width);
3307
3308 /* Get to here when the row_width truncates the final copy.
3309 * There will be 1-3 bytes left to copy, so don't try the
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003310 * 16-bit loop below.
John Bowler4e68aa72011-10-11 16:01:33 -05003311 */
3312 dp = (png_bytep)dp32;
3313 sp = (png_const_bytep)sp32;
3314 do
3315 *dp++ = *sp++;
3316 while (--row_width > 0);
3317 return;
3318 }
3319
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003320 /* Else do it in 16-bit quantities, but only if the size is
John Bowler4e68aa72011-10-11 16:01:33 -05003321 * not too large.
3322 */
3323 else
3324 {
3325 png_uint_16p dp16 = (png_uint_16p)dp;
3326 png_const_uint_16p sp16 = (png_const_uint_16p)sp;
3327 unsigned int skip = (bytes_to_jump-bytes_to_copy) /
3328 sizeof (png_uint_16);
3329
3330 do
3331 {
3332 size_t c = bytes_to_copy;
3333 do
3334 {
3335 *dp16++ = *sp16++;
3336 c -= sizeof (png_uint_16);
3337 }
3338 while (c > 0);
3339
3340 if (row_width <= bytes_to_jump)
3341 return;
3342
3343 dp16 += skip;
3344 sp16 += skip;
3345 row_width -= bytes_to_jump;
3346 }
3347 while (bytes_to_copy <= row_width);
3348
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003349 /* End of row - 1 byte left, bytes_to_copy > row_width: */
John Bowler4e68aa72011-10-11 16:01:33 -05003350 dp = (png_bytep)dp16;
3351 sp = (png_const_bytep)sp16;
3352 do
3353 *dp++ = *sp++;
3354 while (--row_width > 0);
3355 return;
3356 }
3357 }
3358#endif /* PNG_ALIGN_ code */
3359
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003360 /* The true default - use a png_memcpy: */
John Bowler4e68aa72011-10-11 16:01:33 -05003361 for (;;)
3362 {
3363 png_memcpy(dp, sp, bytes_to_copy);
3364
3365 if (row_width <= bytes_to_jump)
3366 return;
3367
3368 sp += bytes_to_jump;
3369 dp += bytes_to_jump;
3370 row_width -= bytes_to_jump;
3371 if (bytes_to_copy > row_width)
3372 bytes_to_copy = row_width;
3373 }
3374 }
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003375
3376 /* NOT REACHED*/
John Bowler4e68aa72011-10-11 16:01:33 -05003377 } /* pixel_depth >= 8 */
3378
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003379 /* Here if pixel_depth < 8 to check 'end_ptr' below. */
Guy Schalnat0d580581995-07-20 02:43:20 -05003380 }
John Bowler4e68aa72011-10-11 16:01:33 -05003381 else
John Bowlerac8375d2011-10-06 22:27:16 -05003382#endif
3383
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003384 /* If here then the switch above wasn't used so just png_memcpy the whole row
John Bowler4e68aa72011-10-11 16:01:33 -05003385 * from the temporary row buffer (notice that this overwrites the end of the
3386 * destination row if it is a partial byte.)
John Bowlerac8375d2011-10-06 22:27:16 -05003387 */
3388 png_memcpy(dp, sp, PNG_ROWBYTES(pixel_depth, row_width));
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003389
3390 /* Restore the overwritten bits from the last byte if necessary. */
3391 if (end_ptr != NULL)
3392 *end_ptr = (png_byte)((end_byte & end_mask) | (*end_ptr & ~end_mask));
Guy Schalnat0d580581995-07-20 02:43:20 -05003393}
3394
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06003395#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05003396void /* PRIVATE */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003397png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
3398 png_uint_32 transformations /* Because these may affect the byte layout */)
Guy Schalnat0d580581995-07-20 02:43:20 -05003399{
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003400 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
3401 /* Offset to next interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003402 static PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003403
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05003404 png_debug(1, "in png_do_read_interlace");
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003405 if (row != NULL && row_info != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -05003406 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003407 png_uint_32 final_width;
3408
3409 final_width = row_info->width * png_pass_inc[pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05003410
3411 switch (row_info->pixel_depth)
3412 {
3413 case 1:
3414 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003415 png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3);
3416 png_bytep dp = row + (png_size_t)((final_width - 1) >> 3);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003417 int sshift, dshift;
3418 int s_start, s_end, s_inc;
3419 int jstop = png_pass_inc[pass];
3420 png_byte v;
Guy Schalnat0d580581995-07-20 02:43:20 -05003421 png_uint_32 i;
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003422 int j;
Guy Schalnat0d580581995-07-20 02:43:20 -05003423
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003424#ifdef PNG_READ_PACKSWAP_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003425 if (transformations & PNG_PACKSWAP)
3426 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003427 sshift = (int)((row_info->width + 7) & 0x07);
3428 dshift = (int)((final_width + 7) & 0x07);
3429 s_start = 7;
3430 s_end = 0;
3431 s_inc = -1;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003432 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003433
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003434 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05003435#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003436 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003437 sshift = 7 - (int)((row_info->width + 7) & 0x07);
3438 dshift = 7 - (int)((final_width + 7) & 0x07);
3439 s_start = 0;
3440 s_end = 7;
3441 s_inc = 1;
3442 }
Glenn Randers-Pehrson5dd2b8e2004-11-24 07:50:16 -06003443
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003444 for (i = 0; i < row_info->width; i++)
3445 {
3446 v = (png_byte)((*sp >> sshift) & 0x01);
3447 for (j = 0; j < jstop; j++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003448 {
John Bowler8fb6c6a2012-01-25 07:47:44 -06003449 unsigned int tmp = *dp & (0x7f7f >> (7 - dshift));
3450 tmp |= v << dshift;
3451 *dp = (png_byte)(tmp & 0xff);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003452
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003453 if (dshift == s_end)
3454 {
3455 dshift = s_start;
3456 dp--;
3457 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003458
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003459 else
3460 dshift += s_inc;
3461 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003462
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003463 if (sshift == s_end)
3464 {
3465 sshift = s_start;
Guy Schalnat0d580581995-07-20 02:43:20 -05003466 sp--;
3467 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003468
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003469 else
3470 sshift += s_inc;
Guy Schalnat0d580581995-07-20 02:43:20 -05003471 }
3472 break;
3473 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003474
Guy Schalnat0d580581995-07-20 02:43:20 -05003475 case 2:
3476 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003477 png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);
3478 png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);
3479 int sshift, dshift;
3480 int s_start, s_end, s_inc;
3481 int jstop = png_pass_inc[pass];
Andreas Dilger47a0c421997-05-16 02:46:07 -05003482 png_uint_32 i;
Guy Schalnat0d580581995-07-20 02:43:20 -05003483
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003484#ifdef PNG_READ_PACKSWAP_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003485 if (transformations & PNG_PACKSWAP)
3486 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003487 sshift = (int)(((row_info->width + 3) & 0x03) << 1);
3488 dshift = (int)(((final_width + 3) & 0x03) << 1);
3489 s_start = 6;
3490 s_end = 0;
3491 s_inc = -2;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003492 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003493
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003494 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05003495#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003496 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003497 sshift = (int)((3 - ((row_info->width + 3) & 0x03)) << 1);
3498 dshift = (int)((3 - ((final_width + 3) & 0x03)) << 1);
3499 s_start = 0;
3500 s_end = 6;
3501 s_inc = 2;
3502 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05003503
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003504 for (i = 0; i < row_info->width; i++)
3505 {
3506 png_byte v;
3507 int j;
3508
3509 v = (png_byte)((*sp >> sshift) & 0x03);
3510 for (j = 0; j < jstop; j++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003511 {
John Bowler8fb6c6a2012-01-25 07:47:44 -06003512 unsigned int tmp = *dp & (0x3f3f >> (6 - dshift));
3513 tmp |= v << dshift;
3514 *dp = (png_byte)(tmp & 0xff);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003515
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003516 if (dshift == s_end)
3517 {
3518 dshift = s_start;
3519 dp--;
3520 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003521
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003522 else
3523 dshift += s_inc;
3524 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003525
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003526 if (sshift == s_end)
3527 {
3528 sshift = s_start;
Guy Schalnat0d580581995-07-20 02:43:20 -05003529 sp--;
3530 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003531
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003532 else
3533 sshift += s_inc;
Guy Schalnat0d580581995-07-20 02:43:20 -05003534 }
3535 break;
3536 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003537
Guy Schalnat0d580581995-07-20 02:43:20 -05003538 case 4:
3539 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003540 png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1);
3541 png_bytep dp = row + (png_size_t)((final_width - 1) >> 1);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003542 int sshift, dshift;
3543 int s_start, s_end, s_inc;
Guy Schalnat0d580581995-07-20 02:43:20 -05003544 png_uint_32 i;
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003545 int jstop = png_pass_inc[pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05003546
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003547#ifdef PNG_READ_PACKSWAP_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003548 if (transformations & PNG_PACKSWAP)
3549 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003550 sshift = (int)(((row_info->width + 1) & 0x01) << 2);
3551 dshift = (int)(((final_width + 1) & 0x01) << 2);
3552 s_start = 4;
3553 s_end = 0;
3554 s_inc = -4;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003555 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003556
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003557 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05003558#endif
3559 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003560 sshift = (int)((1 - ((row_info->width + 1) & 0x01)) << 2);
3561 dshift = (int)((1 - ((final_width + 1) & 0x01)) << 2);
3562 s_start = 0;
3563 s_end = 4;
3564 s_inc = 4;
3565 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05003566
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003567 for (i = 0; i < row_info->width; i++)
3568 {
Glenn Randers-Pehrson8db19982011-10-27 16:17:24 -05003569 png_byte v = (png_byte)((*sp >> sshift) & 0x0f);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003570 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003571
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003572 for (j = 0; j < jstop; j++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003573 {
John Bowler8fb6c6a2012-01-25 07:47:44 -06003574 unsigned int tmp = *dp & (0xf0f >> (4 - dshift));
3575 tmp |= v << dshift;
3576 *dp = (png_byte)(tmp & 0xff);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003577
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003578 if (dshift == s_end)
3579 {
3580 dshift = s_start;
3581 dp--;
3582 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003583
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003584 else
3585 dshift += s_inc;
3586 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003587
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003588 if (sshift == s_end)
3589 {
3590 sshift = s_start;
Guy Schalnat0d580581995-07-20 02:43:20 -05003591 sp--;
3592 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003593
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003594 else
3595 sshift += s_inc;
Guy Schalnat0d580581995-07-20 02:43:20 -05003596 }
3597 break;
3598 }
John Bowlerac8375d2011-10-06 22:27:16 -05003599
Guy Schalnat0d580581995-07-20 02:43:20 -05003600 default:
3601 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003602 png_size_t pixel_bytes = (row_info->pixel_depth >> 3);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003603
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06003604 png_bytep sp = row + (png_size_t)(row_info->width - 1)
3605 * pixel_bytes;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003606
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003607 png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05003608
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003609 int jstop = png_pass_inc[pass];
3610 png_uint_32 i;
3611
3612 for (i = 0; i < row_info->width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003613 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003614 png_byte v[8];
3615 int j;
3616
3617 png_memcpy(v, sp, pixel_bytes);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003618
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003619 for (j = 0; j < jstop; j++)
3620 {
3621 png_memcpy(dp, v, pixel_bytes);
3622 dp -= pixel_bytes;
3623 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003624
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003625 sp -= pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05003626 }
3627 break;
3628 }
3629 }
John Bowlerac8375d2011-10-06 22:27:16 -05003630
Guy Schalnat0d580581995-07-20 02:43:20 -05003631 row_info->width = final_width;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003632 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width);
Guy Schalnat0d580581995-07-20 02:43:20 -05003633 }
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -05003634#ifndef PNG_READ_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003635 PNG_UNUSED(transformations) /* Silence compiler warning */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05003636#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003637}
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06003638#endif /* PNG_READ_INTERLACING_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05003639
Mans Rullgardd3a94802011-11-03 00:47:55 -05003640static void
3641png_read_filter_row_sub(png_row_infop row_info, png_bytep row,
3642 png_const_bytep prev_row)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003643{
Mans Rullgardd3a94802011-11-03 00:47:55 -05003644 png_size_t i;
3645 png_size_t istop = row_info->rowbytes;
3646 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
3647 png_bytep rp = row + bpp;
Mans Rullgardd3a94802011-11-03 00:47:55 -05003648
3649 PNG_UNUSED(prev_row)
3650
3651 for (i = bpp; i < istop; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003652 {
John Bowler7875d532011-11-07 22:33:49 -06003653 *rp = (png_byte)(((int)(*rp) + (int)(*(rp-bpp))) & 0xff);
Mans Rullgardd3a94802011-11-03 00:47:55 -05003654 rp++;
3655 }
3656}
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003657
Mans Rullgardd3a94802011-11-03 00:47:55 -05003658static void
3659png_read_filter_row_up(png_row_infop row_info, png_bytep row,
3660 png_const_bytep prev_row)
3661{
3662 png_size_t i;
3663 png_size_t istop = row_info->rowbytes;
3664 png_bytep rp = row;
3665 png_const_bytep pp = prev_row;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003666
Mans Rullgardd3a94802011-11-03 00:47:55 -05003667 for (i = 0; i < istop; i++)
3668 {
3669 *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
3670 rp++;
3671 }
3672}
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003673
Mans Rullgardd3a94802011-11-03 00:47:55 -05003674static void
3675png_read_filter_row_avg(png_row_infop row_info, png_bytep row,
3676 png_const_bytep prev_row)
3677{
3678 png_size_t i;
3679 png_bytep rp = row;
3680 png_const_bytep pp = prev_row;
Mans Rullgardd3a94802011-11-03 00:47:55 -05003681 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
3682 png_size_t istop = row_info->rowbytes - bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003683
Mans Rullgardd3a94802011-11-03 00:47:55 -05003684 for (i = 0; i < bpp; i++)
3685 {
3686 *rp = (png_byte)(((int)(*rp) +
3687 ((int)(*pp++) / 2 )) & 0xff);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003688
Mans Rullgardd3a94802011-11-03 00:47:55 -05003689 rp++;
3690 }
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003691
Mans Rullgardd3a94802011-11-03 00:47:55 -05003692 for (i = 0; i < istop; i++)
3693 {
3694 *rp = (png_byte)(((int)(*rp) +
John Bowler7875d532011-11-07 22:33:49 -06003695 (int)(*pp++ + *(rp-bpp)) / 2 ) & 0xff);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003696
Mans Rullgardd3a94802011-11-03 00:47:55 -05003697 rp++;
3698 }
3699}
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003700
Mans Rullgardd3a94802011-11-03 00:47:55 -05003701static void
John Bowlerfcc02632011-11-03 18:31:00 -05003702png_read_filter_row_paeth_1byte_pixel(png_row_infop row_info, png_bytep row,
Mans Rullgardd3a94802011-11-03 00:47:55 -05003703 png_const_bytep prev_row)
3704{
John Bowlerfcc02632011-11-03 18:31:00 -05003705 png_bytep rp_end = row + row_info->rowbytes;
3706 int a, c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003707
John Bowlerfcc02632011-11-03 18:31:00 -05003708 /* First pixel/byte */
3709 c = *prev_row++;
3710 a = *row + c;
3711 *row++ = (png_byte)a;
3712
3713 /* Remainder */
3714 while (row < rp_end)
Mans Rullgardd3a94802011-11-03 00:47:55 -05003715 {
John Bowlerfcc02632011-11-03 18:31:00 -05003716 int b, pa, pb, pc, p;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003717
John Bowlerfcc02632011-11-03 18:31:00 -05003718 a &= 0xff; /* From previous iteration or start */
3719 b = *prev_row++;
Mans Rullgardd3a94802011-11-03 00:47:55 -05003720
3721 p = b - c;
3722 pc = a - c;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003723
John Bowlerfcc02632011-11-03 18:31:00 -05003724# ifdef PNG_USE_ABS
3725 pa = abs(p);
3726 pb = abs(pc);
3727 pc = abs(p + pc);
3728# else
3729 pa = p < 0 ? -p : p;
3730 pb = pc < 0 ? -pc : pc;
3731 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
3732# endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003733
John Bowlerfcc02632011-11-03 18:31:00 -05003734 /* Find the best predictor, the least of pa, pb, pc favoring the earlier
3735 * ones in the case of a tie.
3736 */
3737 if (pb < pa) pa = pb, a = b;
3738 if (pc < pa) a = c;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003739
John Bowlerfcc02632011-11-03 18:31:00 -05003740 /* Calculate the current pixel in a, and move the previous row pixel to c
3741 * for the next time round the loop
3742 */
3743 c = b;
3744 a += *row;
3745 *row++ = (png_byte)a;
3746 }
3747}
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003748
John Bowlerfcc02632011-11-03 18:31:00 -05003749static void
3750png_read_filter_row_paeth_multibyte_pixel(png_row_infop row_info, png_bytep row,
3751 png_const_bytep prev_row)
3752{
3753 int bpp = (row_info->pixel_depth + 7) >> 3;
3754 png_bytep rp_end = row + bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003755
John Bowlerfcc02632011-11-03 18:31:00 -05003756 /* Process the first pixel in the row completely (this is the same as 'up'
3757 * because there is only one candidate predictor for the first row).
3758 */
3759 while (row < rp_end)
3760 {
3761 int a = *row + *prev_row++;
3762 *row++ = (png_byte)a;
3763 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003764
John Bowlerfcc02632011-11-03 18:31:00 -05003765 /* Remainder */
3766 rp_end += row_info->rowbytes - bpp;
3767
3768 while (row < rp_end)
3769 {
3770 int a, b, c, pa, pb, pc, p;
3771
3772 c = *(prev_row - bpp);
3773 a = *(row - bpp);
3774 b = *prev_row++;
3775
3776 p = b - c;
3777 pc = a - c;
3778
3779# ifdef PNG_USE_ABS
3780 pa = abs(p);
3781 pb = abs(pc);
3782 pc = abs(p + pc);
3783# else
3784 pa = p < 0 ? -p : p;
3785 pb = pc < 0 ? -pc : pc;
3786 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
3787# endif
3788
3789 if (pb < pa) pa = pb, a = b;
3790 if (pc < pa) a = c;
3791
3792 c = b;
3793 a += *row;
3794 *row++ = (png_byte)a;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003795 }
3796}
Guy Schalnat0d580581995-07-20 02:43:20 -05003797
Mans Rullgardd3a94802011-11-03 00:47:55 -05003798#ifdef PNG_ARM_NEON
Mans Rullgarde7acc6a2011-11-16 13:48:18 -06003799
3800#ifdef __linux__
3801#include <stdio.h>
3802#include <elf.h>
3803#include <asm/hwcap.h>
3804
3805static int png_have_hwcap(unsigned cap)
3806{
3807 FILE *f = fopen("/proc/self/auxv", "r");
3808 Elf32_auxv_t aux;
3809 int have_cap = 0;
3810
3811 if (!f)
3812 return 0;
3813
3814 while (fread(&aux, sizeof(aux), 1, f) > 0)
3815 {
3816 if (aux.a_type == AT_HWCAP &&
3817 aux.a_un.a_val & cap)
3818 {
3819 have_cap = 1;
3820 break;
3821 }
3822 }
3823
3824 fclose(f);
3825
3826 return have_cap;
3827}
3828#endif /* __linux__ */
3829
Mans Rullgardd3a94802011-11-03 00:47:55 -05003830static void
John Bowler5d567862011-12-24 09:12:00 -06003831png_init_filter_functions_neon(png_structrp pp, unsigned int bpp)
Mans Rullgardd3a94802011-11-03 00:47:55 -05003832{
Mans Rullgarde7acc6a2011-11-16 13:48:18 -06003833#ifdef __linux__
3834 if (!png_have_hwcap(HWCAP_NEON))
3835 return;
3836#endif
3837
Mans Rullgardd3a94802011-11-03 00:47:55 -05003838 pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up_neon;
3839
Mans Rullgarde7acc6a2011-11-16 13:48:18 -06003840 if (bpp == 3)
3841 {
Mans Rullgardd3a94802011-11-03 00:47:55 -05003842 pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub3_neon;
3843 pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg3_neon;
John Bowler6f237b62012-03-02 13:13:15 -06003844 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
Mans Rullgarde7acc6a2011-11-16 13:48:18 -06003845 png_read_filter_row_paeth3_neon;
3846 }
3847
3848 else if (bpp == 4)
3849 {
Mans Rullgardd3a94802011-11-03 00:47:55 -05003850 pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub4_neon;
3851 pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg4_neon;
Mans Rullgarde7acc6a2011-11-16 13:48:18 -06003852 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
3853 png_read_filter_row_paeth4_neon;
Mans Rullgardd3a94802011-11-03 00:47:55 -05003854 }
3855}
Mans Rullgarde7acc6a2011-11-16 13:48:18 -06003856#endif /* PNG_ARM_NEON */
Mans Rullgardd3a94802011-11-03 00:47:55 -05003857
3858static void
John Bowler5d567862011-12-24 09:12:00 -06003859png_init_filter_functions(png_structrp pp)
Mans Rullgardd3a94802011-11-03 00:47:55 -05003860{
John Bowlerfcc02632011-11-03 18:31:00 -05003861 unsigned int bpp = (pp->pixel_depth + 7) >> 3;
3862
Mans Rullgardd3a94802011-11-03 00:47:55 -05003863 pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub;
3864 pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up;
3865 pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg;
John Bowlerfcc02632011-11-03 18:31:00 -05003866 if (bpp == 1)
3867 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
3868 png_read_filter_row_paeth_1byte_pixel;
3869 else
3870 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
3871 png_read_filter_row_paeth_multibyte_pixel;
Mans Rullgardd3a94802011-11-03 00:47:55 -05003872
3873#ifdef PNG_ARM_NEON
John Bowlerfcc02632011-11-03 18:31:00 -05003874 png_init_filter_functions_neon(pp, bpp);
Mans Rullgardd3a94802011-11-03 00:47:55 -05003875#endif
3876}
3877
3878void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06003879png_read_filter_row(png_structrp pp, png_row_infop row_info, png_bytep row,
Mans Rullgardd3a94802011-11-03 00:47:55 -05003880 png_const_bytep prev_row, int filter)
3881{
3882 if (pp->read_filter[0] == NULL)
3883 png_init_filter_functions(pp);
3884 if (filter > PNG_FILTER_VALUE_NONE && filter < PNG_FILTER_VALUE_LAST)
3885 pp->read_filter[filter-1](row_info, row, prev_row);
3886}
3887
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05003888#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05003889void /* PRIVATE */
John Bowlerb5d00512012-03-09 09:15:18 -06003890png_read_IDAT_data(png_structrp png_ptr, png_bytep output,
3891 png_alloc_size_t avail_out)
3892{
3893 /* Loop reading IDATs and decompressing the result into output[avail_out] */
3894 png_ptr->zstream.next_out = output;
3895 png_ptr->zstream.avail_out = 0; /* safety: set below */
3896
3897 if (output == NULL)
3898 avail_out = 0;
3899
3900 do
3901 {
3902 int ret;
3903 png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];
3904
3905 if (png_ptr->zstream.avail_in == 0)
3906 {
3907 uInt avail_in;
3908 png_bytep buffer;
3909
3910 while (png_ptr->idat_size == 0)
3911 {
3912 png_crc_finish(png_ptr, 0);
3913
3914 png_ptr->idat_size = png_read_chunk_header(png_ptr);
3915 /* This is an error even in the 'check' case because the code just
3916 * consumed a non-IDAT header.
3917 */
3918 if (png_ptr->chunk_name != png_IDAT)
3919 png_error(png_ptr, "Not enough image data");
3920 }
3921
3922 avail_in = png_ptr->IDAT_read_size;
3923
3924 if (avail_in > png_ptr->idat_size)
3925 avail_in = (uInt)png_ptr->idat_size;
3926
3927 /* A PNG with a gradually increasing IDAT size will defeat this attempt
3928 * to minimize memory usage by causing lots of re-allocs, but
3929 * realistically doing IDAT_read_size re-allocs is not likely to be a
3930 * big problem.
3931 */
3932 buffer = png_read_buffer(png_ptr, avail_in, 0/*error*/);
3933
3934 png_crc_read(png_ptr, buffer, avail_in);
3935 png_ptr->idat_size -= avail_in;
3936
3937 png_ptr->zstream.next_in = buffer;
3938 png_ptr->zstream.avail_in = avail_in;
3939 }
3940
3941 /* And set up the output side. */
3942 if (output != NULL) /* standard read */
3943 {
3944 uInt out = ZLIB_IO_MAX;
3945
3946 if (out > avail_out)
3947 out = (uInt)avail_out;
3948
3949 avail_out -= out;
3950 png_ptr->zstream.avail_out = out;
3951 }
3952
3953 else /* check for end */
3954 {
3955 png_ptr->zstream.next_out = tmpbuf;
3956 png_ptr->zstream.avail_out = sizeof tmpbuf;
3957 }
3958
3959 /* Use NO_FLUSH; this gives zlib the maximum opportunity to optimize the
John Bowler90669192012-03-09 22:03:13 -06003960 * process. If the LZ stream is truncated the sequential reader will
3961 * terminally damage the stream, above, by reading the chunk header of the
3962 * following chunk (it then exits with png_error).
3963 *
3964 * TODO: deal more elegantly with truncated IDAT lists.
John Bowlerb5d00512012-03-09 09:15:18 -06003965 */
3966 ret = inflate(&png_ptr->zstream, Z_NO_FLUSH);
3967
3968 /* Take the unconsumed output back (so, in the 'check' case this just
3969 * counts up).
3970 */
3971 avail_out += png_ptr->zstream.avail_out;
3972 png_ptr->zstream.avail_out = 0;
3973
3974 if (ret == Z_STREAM_END)
3975 {
3976 /* Do this for safety; we won't read any more into this row. */
3977 png_ptr->zstream.next_out = NULL;
3978
3979 png_ptr->mode |= PNG_AFTER_IDAT;
3980 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
3981
3982 if (png_ptr->zstream.avail_in > 0 || png_ptr->idat_size > 0)
3983 png_chunk_benign_error(png_ptr, "Extra compressed data");
3984 break;
3985 }
3986
3987 if (ret != Z_OK)
3988 {
3989 png_zstream_error(png_ptr, ret);
3990
3991 if (output != NULL)
3992 png_chunk_error(png_ptr, png_ptr->zstream.msg);
3993
3994 else /* checking */
3995 {
3996 png_chunk_benign_error(png_ptr, png_ptr->zstream.msg);
3997 return;
3998 }
3999 }
4000 } while (avail_out > 0);
4001
4002 if (avail_out > 0)
4003 {
4004 /* The stream ended before the image; this is the same as too few IDATs so
4005 * should be handled the same way.
4006 */
4007 if (output != NULL)
4008 png_error(png_ptr, "Not enough image data");
4009
4010 else /* checking */
4011 png_chunk_benign_error(png_ptr, "Too much image data");
4012 }
4013}
4014
4015void /* PRIVATE */
4016png_read_finish_IDAT(png_structrp png_ptr)
4017{
4018 /* We don't need any more data and the stream should have ended, however the
4019 * LZ end code may actually not have been processed. In this case we must
4020 * read it otherwise stray unread IDAT data or, more likely, an IDAT chunk
4021 * may still remain to be consumed.
4022 */
4023 if (!(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED))
4024 {
4025 /* The NULL causes png_read_IDAT_data to swallow any remaining bytes in
4026 * the compressed stream, but the stream may be damaged too, so even after
4027 * this call we may need to terminate the zstream ownership.
4028 */
4029 png_read_IDAT_data(png_ptr, NULL, 0);
4030 png_ptr->zstream.next_out = NULL; /* safety */
4031
4032 /* Now clear everything out for safety; the following may not have been
4033 * done.
4034 */
4035 if (!(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED))
4036 {
4037 png_ptr->mode |= PNG_AFTER_IDAT;
4038 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
4039 }
4040 }
4041
4042 /* If the zstream has not been released do it now *and* terminate the reading
4043 * of the final IDAT chunk.
4044 */
4045 if (png_ptr->zowner == png_IDAT)
4046 {
4047 /* Always do this; the pointers otherwise point into the read buffer. */
4048 png_ptr->zstream.next_in = NULL;
4049 png_ptr->zstream.avail_in = 0;
4050
4051 /* Now we no longer own the zstream. */
4052 png_ptr->zowner = 0;
4053
4054 /* The slightly weird semantics of the sequential IDAT reading is that we
4055 * are always in or at the end of an IDAT chunk, so we always need to do a
4056 * crc_finish here. If idat_size is non-zero we also need to read the
4057 * spurious bytes at the end of the chunk now.
4058 */
4059 (void)png_crc_finish(png_ptr, png_ptr->idat_size);
4060 }
4061}
4062
4063void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06004064png_read_finish_row(png_structrp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05004065{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004066#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004067 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004068
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004069 /* Start of interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004070 static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004071
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004072 /* Offset to next interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004073 static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004074
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004075 /* Start of interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004076 static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004077
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004078 /* Offset to next interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004079 static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004080#endif /* PNG_READ_INTERLACING_SUPPORTED */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004081
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05004082 png_debug(1, "in png_read_finish_row");
Guy Schalnat0d580581995-07-20 02:43:20 -05004083 png_ptr->row_number++;
4084 if (png_ptr->row_number < png_ptr->num_rows)
4085 return;
4086
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004087#ifdef PNG_READ_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05004088 if (png_ptr->interlaced)
4089 {
4090 png_ptr->row_number = 0;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004091
Glenn Randers-Pehrson435cf872011-10-05 16:23:53 -05004092 /* TO DO: don't do this if prev_row isn't needed (requires
4093 * read-ahead of the next row's filter byte.
4094 */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004095 png_memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
4096
Guy Schalnat0d580581995-07-20 02:43:20 -05004097 do
4098 {
4099 png_ptr->pass++;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004100
Guy Schalnat0d580581995-07-20 02:43:20 -05004101 if (png_ptr->pass >= 7)
4102 break;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004103
Guy Schalnat0d580581995-07-20 02:43:20 -05004104 png_ptr->iwidth = (png_ptr->width +
4105 png_pass_inc[png_ptr->pass] - 1 -
4106 png_pass_start[png_ptr->pass]) /
4107 png_pass_inc[png_ptr->pass];
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05004108
Guy Schalnat0d580581995-07-20 02:43:20 -05004109 if (!(png_ptr->transformations & PNG_INTERLACE))
4110 {
4111 png_ptr->num_rows = (png_ptr->height +
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004112 png_pass_yinc[png_ptr->pass] - 1 -
4113 png_pass_ystart[png_ptr->pass]) /
4114 png_pass_yinc[png_ptr->pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05004115 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004116
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -05004117 else /* if (png_ptr->transformations & PNG_INTERLACE) */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004118 break; /* libpng deinterlacing sees every row */
4119
4120 } while (png_ptr->num_rows == 0 || png_ptr->iwidth == 0);
Guy Schalnat0d580581995-07-20 02:43:20 -05004121
4122 if (png_ptr->pass < 7)
4123 return;
4124 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004125#endif /* PNG_READ_INTERLACING_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05004126
John Bowler42a2b552012-03-05 20:57:40 -06004127 /* Here after at the end of the last row of the last pass. */
John Bowlerb5d00512012-03-09 09:15:18 -06004128 png_read_finish_IDAT(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05004129}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05004130#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05004131
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05004132void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06004133png_read_start_row(png_structrp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05004134{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004135#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004136 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004137
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004138 /* Start of interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004139 static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004140
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004141 /* Offset to next interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004142 static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004143
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004144 /* Start of interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004145 static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004146
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004147 /* Offset to next interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004148 static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004149#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004150
Guy Schalnat0d580581995-07-20 02:43:20 -05004151 int max_pixel_depth;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05004152 png_size_t row_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05004153
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05004154 png_debug(1, "in png_read_start_row");
John Bowler42a2b552012-03-05 20:57:40 -06004155
John Bowler4a12f4a2011-04-17 18:34:22 -05004156#ifdef PNG_READ_TRANSFORMS_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05004157 png_init_read_transformations(png_ptr);
John Bowler4a12f4a2011-04-17 18:34:22 -05004158#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004159#ifdef PNG_READ_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05004160 if (png_ptr->interlaced)
4161 {
4162 if (!(png_ptr->transformations & PNG_INTERLACE))
4163 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004164 png_pass_ystart[0]) / png_pass_yinc[0];
4165
Guy Schalnat0d580581995-07-20 02:43:20 -05004166 else
4167 png_ptr->num_rows = png_ptr->height;
4168
4169 png_ptr->iwidth = (png_ptr->width +
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004170 png_pass_inc[png_ptr->pass] - 1 -
4171 png_pass_start[png_ptr->pass]) /
4172 png_pass_inc[png_ptr->pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05004173 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004174
Guy Schalnat0d580581995-07-20 02:43:20 -05004175 else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004176#endif /* PNG_READ_INTERLACING_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05004177 {
4178 png_ptr->num_rows = png_ptr->height;
4179 png_ptr->iwidth = png_ptr->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05004180 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004181
Guy Schalnat0d580581995-07-20 02:43:20 -05004182 max_pixel_depth = png_ptr->pixel_depth;
4183
John Bowlere6fb6912011-11-08 21:35:16 -06004184 /* WARNING: * png_read_transform_info (pngrtran.c) performs a simpliar set of
4185 * calculations to calculate the final pixel depth, then
4186 * png_do_read_transforms actually does the transforms. This means that the
4187 * code which effectively calculates this value is actually repeated in three
4188 * separate places. They must all match. Innocent changes to the order of
4189 * transformations can and will break libpng in a way that causes memory
4190 * overwrites.
4191 *
4192 * TODO: fix this.
4193 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004194#ifdef PNG_READ_PACK_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05004195 if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8)
Guy Schalnat0d580581995-07-20 02:43:20 -05004196 max_pixel_depth = 8;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004197#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05004198
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004199#ifdef PNG_READ_EXPAND_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -05004200 if (png_ptr->transformations & PNG_EXPAND)
Guy Schalnat0d580581995-07-20 02:43:20 -05004201 {
4202 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
4203 {
4204 if (png_ptr->num_trans)
4205 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004206
Guy Schalnat0d580581995-07-20 02:43:20 -05004207 else
4208 max_pixel_depth = 24;
4209 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004210
Guy Schalnat0d580581995-07-20 02:43:20 -05004211 else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
4212 {
4213 if (max_pixel_depth < 8)
4214 max_pixel_depth = 8;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004215
Guy Schalnat0d580581995-07-20 02:43:20 -05004216 if (png_ptr->num_trans)
4217 max_pixel_depth *= 2;
4218 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004219
Guy Schalnat0d580581995-07-20 02:43:20 -05004220 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
4221 {
4222 if (png_ptr->num_trans)
4223 {
4224 max_pixel_depth *= 4;
4225 max_pixel_depth /= 3;
4226 }
4227 }
4228 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004229#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05004230
John Bowler4d562962011-02-12 09:01:20 -06004231#ifdef PNG_READ_EXPAND_16_SUPPORTED
4232 if (png_ptr->transformations & PNG_EXPAND_16)
4233 {
4234# ifdef PNG_READ_EXPAND_SUPPORTED
4235 /* In fact it is an error if it isn't supported, but checking is
4236 * the safe way.
4237 */
4238 if (png_ptr->transformations & PNG_EXPAND)
4239 {
4240 if (png_ptr->bit_depth < 16)
4241 max_pixel_depth *= 2;
4242 }
4243 else
4244# endif
4245 png_ptr->transformations &= ~PNG_EXPAND_16;
4246 }
4247#endif
4248
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004249#ifdef PNG_READ_FILLER_SUPPORTED
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004250 if (png_ptr->transformations & (PNG_FILLER))
Guy Schalnat0d580581995-07-20 02:43:20 -05004251 {
John Bowlere6fb6912011-11-08 21:35:16 -06004252 if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004253 {
4254 if (max_pixel_depth <= 8)
4255 max_pixel_depth = 16;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004256
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004257 else
4258 max_pixel_depth = 32;
4259 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004260
John Bowlere6fb6912011-11-08 21:35:16 -06004261 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB ||
4262 png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004263 {
4264 if (max_pixel_depth <= 32)
4265 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004266
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004267 else
4268 max_pixel_depth = 64;
4269 }
Guy Schalnat0d580581995-07-20 02:43:20 -05004270 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004271#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05004272
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004273#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05004274 if (png_ptr->transformations & PNG_GRAY_TO_RGB)
4275 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06004276 if (
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004277#ifdef PNG_READ_EXPAND_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004278 (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) ||
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06004279#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004280#ifdef PNG_READ_FILLER_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004281 (png_ptr->transformations & (PNG_FILLER)) ||
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06004282#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004283 png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
Guy Schalnat0d580581995-07-20 02:43:20 -05004284 {
4285 if (max_pixel_depth <= 16)
4286 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004287
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06004288 else
Guy Schalnat0d580581995-07-20 02:43:20 -05004289 max_pixel_depth = 64;
4290 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004291
Guy Schalnat0d580581995-07-20 02:43:20 -05004292 else
4293 {
4294 if (max_pixel_depth <= 8)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004295 {
4296 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06004297 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004298
4299 else
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06004300 max_pixel_depth = 24;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004301 }
4302
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06004303 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4304 max_pixel_depth = 64;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004305
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06004306 else
Guy Schalnat0d580581995-07-20 02:43:20 -05004307 max_pixel_depth = 48;
4308 }
4309 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004310#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05004311
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05004312#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
4313defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004314 if (png_ptr->transformations & PNG_USER_TRANSFORM)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004315 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004316 int user_pixel_depth = png_ptr->user_transform_depth *
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05004317 png_ptr->user_transform_channels;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004318
4319 if (user_pixel_depth > max_pixel_depth)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004320 max_pixel_depth = user_pixel_depth;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004321 }
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05004322#endif
4323
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004324 /* This value is stored in png_struct and double checked in the row read
4325 * code.
4326 */
4327 png_ptr->maximum_pixel_depth = (png_byte)max_pixel_depth;
4328 png_ptr->transformed_pixel_depth = 0; /* calculated on demand */
4329
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004330 /* Align the width on the next larger 8 pixels. Mainly used
4331 * for interlacing
4332 */
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05004333 row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004334 /* Calculate the maximum bytes needed, adding a byte and a pixel
4335 * for safety's sake
4336 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004337 row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) +
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004338 1 + ((max_pixel_depth + 7) >> 3);
4339
Guy Schalnat0d580581995-07-20 02:43:20 -05004340#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05004341 if (row_bytes > (png_uint_32)65536L)
Guy Schalnate5a37791996-06-05 15:50:50 -05004342 png_error(png_ptr, "This image requires a row greater than 64KB");
Guy Schalnat0d580581995-07-20 02:43:20 -05004343#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004344
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06004345 if (row_bytes + 48 > png_ptr->old_big_row_buf_size)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004346 {
4347 png_free(png_ptr, png_ptr->big_row_buf);
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004348 png_free(png_ptr, png_ptr->big_prev_row);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004349
Glenn Randers-Pehrson6917b512009-03-09 15:31:08 -05004350 if (png_ptr->interlaced)
Glenn Randers-Pehrsona515d302010-01-01 10:24:25 -06004351 png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr,
4352 row_bytes + 48);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004353
Glenn Randers-Pehrsona515d302010-01-01 10:24:25 -06004354 else
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004355 png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004356
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004357 png_ptr->big_prev_row = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06004358
4359#ifdef PNG_ALIGNED_MEMORY_SUPPORTED
4360 /* Use 16-byte aligned memory for row_buf with at least 16 bytes
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004361 * of padding before and after row_buf; treat prev_row similarly.
John Bowlerac8375d2011-10-06 22:27:16 -05004362 * NOTE: the alignment is to the start of the pixels, one beyond the start
4363 * of the buffer, because of the filter byte. Prior to libpng 1.5.6 this
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004364 * was incorrect; the filter byte was aligned, which had the exact
4365 * opposite effect of that intended.
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06004366 */
John Bowlerac8375d2011-10-06 22:27:16 -05004367 {
4368 png_bytep temp = png_ptr->big_row_buf + 32;
Glenn Randers-Pehrson8db19982011-10-27 16:17:24 -05004369 int extra = (int)((temp - (png_bytep)0) & 0x0f);
John Bowlerac8375d2011-10-06 22:27:16 -05004370 png_ptr->row_buf = temp - extra - 1/*filter byte*/;
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004371
4372 temp = png_ptr->big_prev_row + 32;
Glenn Randers-Pehrson8db19982011-10-27 16:17:24 -05004373 extra = (int)((temp - (png_bytep)0) & 0x0f);
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004374 png_ptr->prev_row = temp - extra - 1/*filter byte*/;
John Bowlerac8375d2011-10-06 22:27:16 -05004375 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004376
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06004377#else
John Bowlerac8375d2011-10-06 22:27:16 -05004378 /* Use 31 bytes of padding before and 17 bytes after row_buf. */
4379 png_ptr->row_buf = png_ptr->big_row_buf + 31;
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004380 png_ptr->prev_row = png_ptr->big_prev_row + 31;
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06004381#endif
4382 png_ptr->old_big_row_buf_size = row_bytes + 48;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004383 }
Guy Schalnat0d580581995-07-20 02:43:20 -05004384
4385#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004386 if (png_ptr->rowbytes > 65535)
Guy Schalnate5a37791996-06-05 15:50:50 -05004387 png_error(png_ptr, "This image requires a row greater than 64KB");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004388
Guy Schalnat0d580581995-07-20 02:43:20 -05004389#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004390 if (png_ptr->rowbytes > (PNG_SIZE_MAX - 1))
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05004391 png_error(png_ptr, "Row has too many bytes to allocate in memory");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004392
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05004393 png_memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05004394
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004395 png_debug1(3, "width = %u,", png_ptr->width);
4396 png_debug1(3, "height = %u,", png_ptr->height);
4397 png_debug1(3, "iwidth = %u,", png_ptr->iwidth);
4398 png_debug1(3, "num_rows = %u,", png_ptr->num_rows);
Glenn Randers-Pehrsonb764c602011-01-14 21:18:37 -06004399 png_debug1(3, "rowbytes = %lu,", (unsigned long)png_ptr->rowbytes);
4400 png_debug1(3, "irowbytes = %lu",
4401 (unsigned long)PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05004402
John Bowlerb5d00512012-03-09 09:15:18 -06004403 /* The sequential reader needs a buffer for IDAT, but the progressive reader
4404 * does not, so free the read buffer now regardless; the sequential reader
4405 * reallocates it on demand.
4406 */
4407 if (png_ptr->read_buffer)
4408 {
4409 png_bytep buffer = png_ptr->read_buffer;
4410
4411 png_ptr->read_buffer_size = 0;
4412 png_ptr->read_buffer = NULL;
4413 png_free(png_ptr, buffer);
4414 }
4415
John Bowler90669192012-03-09 22:03:13 -06004416 /* Finally claim the zstream for the inflate of the IDAT data, use the bits
4417 * value from the stream (note that this will result in a fatal error if the
4418 * IDAT stream has a bogus deflate header window_bits value, but this should
4419 * not be happening any longer!)
4420 */
4421 if (png_inflate_claim(png_ptr, png_IDAT, 0) != Z_OK)
John Bowlerb5d00512012-03-09 09:15:18 -06004422 png_error(png_ptr, png_ptr->zstream.msg);
John Bowler42a2b552012-03-05 20:57:40 -06004423
Guy Schalnate5a37791996-06-05 15:50:50 -05004424 png_ptr->flags |= PNG_FLAG_ROW_INIT;
Guy Schalnat0d580581995-07-20 02:43:20 -05004425}
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06004426#endif /* PNG_READ_SUPPORTED */