blob: 38a553bc65df8be84e39b61db3b16ad518b5e2bc [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;
1439 ret = png_inflate_read(png_ptr, local_buffer,
1440 sizeof local_buffer, &length,
1441 profile + (sizeof profile_header), &size, 0);
1442
1443 if (size == 0)
1444 {
1445 if (png_icc_check_tag_table(png_ptr,
1446 &png_ptr->colorspace, keyword, profile_length,
1447 profile))
1448 {
1449 /* The profile has been validated for basic
1450 * security issues, so read the whole thing in.
1451 */
1452 size = profile_length - (sizeof profile_header)
1453 - 12 * tag_count;
1454 ret = png_inflate_read(png_ptr, local_buffer,
1455 sizeof local_buffer, &length,
1456 profile + (sizeof profile_header) +
1457 12 * tag_count, &size, 1/*finish*/);
1458
1459 if (size == 0)
1460 {
1461 if (ret != Z_STREAM_END)
1462 png_chunk_warning(png_ptr,
1463 png_ptr->zstream.msg);
1464
1465 else if (length > 0)
1466 png_chunk_warning(png_ptr,
1467 "extra compressed data");
1468
1469 /* But, because we got the whole profile,
1470 * assume it is ok.
1471 */
1472 png_ptr->zowner = 0;
1473
1474 /* Yet the CRC should still be correct */
1475 if (png_crc_finish(png_ptr, length))
1476 return;
1477
1478 /* Set the gAMA and cHRM information */
1479 png_icc_set_gAMA_and_cHRM(png_ptr,
1480 &png_ptr->colorspace, keyword, profile,
1481 0/*prefer explicit gAMA/cHRM*/);
1482
1483 /* And steal the profile for info_ptr. */
1484 if (info_ptr != NULL)
1485 {
1486 png_free_data(png_ptr, info_ptr,
1487 PNG_FREE_ICCP, 0);
1488
1489 info_ptr->iccp_name = png_voidcast(char*,
1490 png_malloc_base(png_ptr,
1491 keyword_length+1));
1492 if (info_ptr->iccp_name == NULL)
1493 {
1494 png_ptr->colorspace.flags |=
1495 PNG_COLORSPACE_INVALID;
1496 png_colorspace_sync(png_ptr,
1497 info_ptr);
1498 png_chunk_benign_error(png_ptr,
1499 "out of memory");
1500 return;
1501 }
1502
1503 memcpy(info_ptr->iccp_name, keyword,
1504 keyword_length+1);
1505 info_ptr->iccp_proflen = profile_length;
1506 info_ptr->iccp_profile = profile;
1507 png_ptr->read_buffer = NULL; /*steal*/
1508 info_ptr->free_me |= PNG_FREE_ICCP;
1509 info_ptr->valid |= PNG_INFO_iCCP;
1510
1511 png_colorspace_sync(png_ptr, info_ptr);
1512 }
1513
1514 return;
1515 }
1516
1517 else
1518 errmsg = "truncated";
1519 }
1520
1521 /* else png_icc_check_tag_table output an error */
1522 }
1523
1524 else /* profile truncated */
1525 errmsg = png_ptr->zstream.msg;
1526 }
1527
1528 else
1529 errmsg = "out of memory";
1530 }
1531
1532 /* else png_icc_check_header output an error */
1533 }
1534
1535 /* else png_icc_check_length output an error */
1536 }
1537
1538 else /* profile truncated */
1539 errmsg = png_ptr->zstream.msg;
1540
1541 /* Release the stream */
1542 png_ptr->zowner = 0;
1543 }
1544
1545 else /* png_inflate_claim failed */
1546 errmsg = png_ptr->zstream.msg;
1547 }
John Bowler0ae4f7b2012-03-03 21:10:26 -06001548
1549 else
John Bowlerb11b31a2012-03-21 07:55:46 -05001550 errmsg = "bad compression method"; /* or missing */
John Bowler0ae4f7b2012-03-03 21:10:26 -06001551 }
John Bowler0ae4f7b2012-03-03 21:10:26 -06001552
John Bowlerb5d00512012-03-09 09:15:18 -06001553 else
John Bowlerb11b31a2012-03-21 07:55:46 -05001554 errmsg = "bad keyword";
John Bowlerb5d00512012-03-09 09:15:18 -06001555 }
John Bowlerb11b31a2012-03-21 07:55:46 -05001556
1557 else
1558 errmsg = "too many profiles";
John Bowler0ae4f7b2012-03-03 21:10:26 -06001559
John Bowlerb11b31a2012-03-21 07:55:46 -05001560 /* Failure: the reason is in 'errmsg' */
1561 png_crc_finish(png_ptr, length);
1562 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1563 png_colorspace_sync(png_ptr, info_ptr);
1564 if (errmsg != NULL) /* else already output */
1565 png_chunk_benign_error(png_ptr, errmsg);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001566}
1567#endif /* PNG_READ_iCCP_SUPPORTED */
1568
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001569#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001570void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001571png_handle_sPLT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001572/* Note: this does not properly handle chunks that are > 64K under DOS */
1573{
John Bowlerb5d00512012-03-09 09:15:18 -06001574 png_bytep entry_start, buffer;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001575 png_sPLT_t new_palette;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001576 png_sPLT_entryp pp;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001577 png_uint_32 data_length;
1578 int entry_size, i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001579 png_uint_32 skip = 0;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001580 png_uint_32 dl;
1581 png_size_t max_dl;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001582
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001583 png_debug(1, "in png_handle_sPLT");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001584
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001585#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001586 if (png_ptr->user_chunk_cache_max != 0)
1587 {
1588 if (png_ptr->user_chunk_cache_max == 1)
1589 {
1590 png_crc_finish(png_ptr, length);
1591 return;
1592 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001593
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001594 if (--png_ptr->user_chunk_cache_max == 1)
1595 {
1596 png_warning(png_ptr, "No space in chunk cache for sPLT");
1597 png_crc_finish(png_ptr, length);
1598 return;
1599 }
1600 }
1601#endif
1602
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001603 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05001604 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001605
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001606 else if (png_ptr->mode & PNG_HAVE_IDAT)
1607 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001608 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001609 png_chunk_benign_error(png_ptr, "out of place");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001610 return;
1611 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001612
1613#ifdef PNG_MAX_MALLOC_64K
John Bowlerb11b31a2012-03-21 07:55:46 -05001614 if (length > 65535U)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001615 {
John Bowlerb11b31a2012-03-21 07:55:46 -05001616 png_crc_finish(png_ptr, length);
1617 png_chunk_benign_error(png_ptr, "too large to fit in memory");
1618 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001619 }
1620#endif
1621
John Bowlerb11b31a2012-03-21 07:55:46 -05001622 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
1623 if (buffer == NULL)
1624 {
1625 png_crc_finish(png_ptr, length);
1626 png_chunk_benign_error(png_ptr, "out of memory");
1627 return;
1628 }
1629
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001630
1631 /* WARNING: this may break if size_t is less than 32 bits; it is assumed
1632 * that the PNG_MAX_MALLOC_64K test is enabled in this case, but this is a
1633 * potential breakage point if the types in pngconf.h aren't exactly right.
1634 */
John Bowlerb5d00512012-03-09 09:15:18 -06001635 png_crc_read(png_ptr, buffer, length);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001636
1637 if (png_crc_finish(png_ptr, skip))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001638 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001639
John Bowlerb5d00512012-03-09 09:15:18 -06001640 buffer[length] = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001641
John Bowlerb5d00512012-03-09 09:15:18 -06001642 for (entry_start = buffer; *entry_start; entry_start++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001643 /* Empty loop to find end of name */ ;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001644
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001645 ++entry_start;
1646
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001647 /* A sample depth should follow the separator, and we should be on it */
John Bowlerb5d00512012-03-09 09:15:18 -06001648 if (entry_start > buffer + length - 2)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001649 {
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -05001650 png_warning(png_ptr, "malformed sPLT chunk");
1651 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001652 }
1653
1654 new_palette.depth = *entry_start++;
Glenn Randers-Pehrsona565f0e2010-03-06 08:24:45 -06001655 entry_size = (new_palette.depth == 8 ? 6 : 10);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001656 /* This must fit in a png_uint_32 because it is derived from the original
John Bowlerb5d00512012-03-09 09:15:18 -06001657 * chunk data length.
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001658 */
John Bowlerb5d00512012-03-09 09:15:18 -06001659 data_length = length - (png_uint_32)(entry_start - buffer);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001660
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001661 /* Integrity-check the data length */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001662 if (data_length % entry_size)
1663 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001664 png_warning(png_ptr, "sPLT chunk has bad length");
1665 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001666 }
1667
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001668 dl = (png_int_32)(data_length / entry_size);
1669 max_dl = PNG_SIZE_MAX / png_sizeof(png_sPLT_entry);
1670
1671 if (dl > max_dl)
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001672 {
1673 png_warning(png_ptr, "sPLT chunk too long");
1674 return;
1675 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001676
1677 new_palette.nentries = (png_int_32)(data_length / entry_size);
1678
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001679 new_palette.entries = (png_sPLT_entryp)png_malloc_warn(
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001680 png_ptr, new_palette.nentries * png_sizeof(png_sPLT_entry));
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001681
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001682 if (new_palette.entries == NULL)
1683 {
1684 png_warning(png_ptr, "sPLT chunk requires too much memory");
1685 return;
1686 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001687
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001688#ifdef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001689 for (i = 0; i < new_palette.nentries; i++)
1690 {
Glenn Randers-Pehrson90b878c2009-10-07 12:44:35 -05001691 pp = new_palette.entries + i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001692
1693 if (new_palette.depth == 8)
1694 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001695 pp->red = *entry_start++;
1696 pp->green = *entry_start++;
1697 pp->blue = *entry_start++;
1698 pp->alpha = *entry_start++;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001699 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001700
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001701 else
1702 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001703 pp->red = png_get_uint_16(entry_start); entry_start += 2;
1704 pp->green = png_get_uint_16(entry_start); entry_start += 2;
1705 pp->blue = png_get_uint_16(entry_start); entry_start += 2;
1706 pp->alpha = png_get_uint_16(entry_start); entry_start += 2;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001707 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001708
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001709 pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
1710 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001711#else
1712 pp = new_palette.entries;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001713
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001714 for (i = 0; i < new_palette.nentries; i++)
1715 {
1716
1717 if (new_palette.depth == 8)
1718 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001719 pp[i].red = *entry_start++;
1720 pp[i].green = *entry_start++;
1721 pp[i].blue = *entry_start++;
1722 pp[i].alpha = *entry_start++;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001723 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001724
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001725 else
1726 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001727 pp[i].red = png_get_uint_16(entry_start); entry_start += 2;
1728 pp[i].green = png_get_uint_16(entry_start); entry_start += 2;
1729 pp[i].blue = png_get_uint_16(entry_start); entry_start += 2;
1730 pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001731 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001732
Glenn Randers-Pehrsonf27592a2011-03-21 18:05:40 -05001733 pp[i].frequency = png_get_uint_16(entry_start); entry_start += 2;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001734 }
1735#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001736
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001737 /* Discard all chunk data except the name and stash that */
John Bowlerb5d00512012-03-09 09:15:18 -06001738 new_palette.name = (png_charp)buffer;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001739
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001740 png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001741
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001742 png_free(png_ptr, new_palette.entries);
1743}
1744#endif /* PNG_READ_sPLT_SUPPORTED */
1745
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001746#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001747void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001748png_handle_tRNS(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001749{
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001750 png_byte readbuf[PNG_MAX_PALETTE_LENGTH];
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06001751
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001752 png_debug(1, "in png_handle_tRNS");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001753
Guy Schalnate5a37791996-06-05 15:50:50 -05001754 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05001755 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001756
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001757 else if (png_ptr->mode & PNG_HAVE_IDAT)
1758 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001759 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001760 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001761 return;
1762 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001763
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001764 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001765 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001766 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001767 png_chunk_benign_error(png_ptr, "duplicate");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001768 return;
1769 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001770
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001771 if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
Guy Schalnat0d580581995-07-20 02:43:20 -05001772 {
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001773 png_byte buf[2];
Guy Schalnat0d580581995-07-20 02:43:20 -05001774
1775 if (length != 2)
1776 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001777 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001778 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001779 return;
1780 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001781
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001782 png_crc_read(png_ptr, buf, 2);
1783 png_ptr->num_trans = 1;
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -05001784 png_ptr->trans_color.gray = png_get_uint_16(buf);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001785 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001786
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001787 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
1788 {
1789 png_byte buf[6];
1790
1791 if (length != 6)
1792 {
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001793 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001794 png_chunk_benign_error(png_ptr, "invalid");
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001795 return;
1796 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001797
John Bowler0ae4f7b2012-03-03 21:10:26 -06001798 png_crc_read(png_ptr, buf, length);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001799 png_ptr->num_trans = 1;
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -05001800 png_ptr->trans_color.red = png_get_uint_16(buf);
1801 png_ptr->trans_color.green = png_get_uint_16(buf + 2);
1802 png_ptr->trans_color.blue = png_get_uint_16(buf + 4);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001803 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001804
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001805 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1806 {
1807 if (!(png_ptr->mode & PNG_HAVE_PLTE))
1808 {
John Bowlerb11b31a2012-03-21 07:55:46 -05001809 /* TODO: is this actually an error in the ISO spec? */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001810 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001811 png_chunk_benign_error(png_ptr, "out of place");
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001812 return;
1813 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001814
John Bowlerb11b31a2012-03-21 07:55:46 -05001815 if (length > png_ptr->num_palette || length > PNG_MAX_PALETTE_LENGTH ||
1816 length == 0)
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001817 {
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001818 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001819 png_chunk_benign_error(png_ptr, "invalid");
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001820 return;
1821 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001822
John Bowler0ae4f7b2012-03-03 21:10:26 -06001823 png_crc_read(png_ptr, readbuf, length);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001824 png_ptr->num_trans = (png_uint_16)length;
1825 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001826
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001827 else
Guy Schalnate5a37791996-06-05 15:50:50 -05001828 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001829 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001830 png_chunk_benign_error(png_ptr, "invalid with alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -05001831 return;
1832 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001833
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001834 if (png_crc_finish(png_ptr, 0))
Glenn Randers-Pehrsona7dbcba2007-05-15 16:16:34 -05001835 {
1836 png_ptr->num_trans = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001837 return;
Glenn Randers-Pehrsona7dbcba2007-05-15 16:16:34 -05001838 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001839
John Bowlerb11b31a2012-03-21 07:55:46 -05001840 /* TODO: this is a horrible side effect in the palette case because the
1841 * png_struct ends up with a pointer to the tRNS buffer owned by the
1842 * png_info. Fix this.
1843 */
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06001844 png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001845 &(png_ptr->trans_color));
Guy Schalnat0d580581995-07-20 02:43:20 -05001846}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001847#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001848
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001849#ifdef PNG_READ_bKGD_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001850void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001851png_handle_bKGD(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001852{
John Bowler0ae4f7b2012-03-03 21:10:26 -06001853 unsigned int truelen;
Guy Schalnat0d580581995-07-20 02:43:20 -05001854 png_byte buf[6];
John Bowlercb0b2962011-05-12 21:48:29 -05001855 png_color_16 background;
Guy Schalnat0d580581995-07-20 02:43:20 -05001856
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001857 png_debug(1, "in png_handle_bKGD");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001858
Guy Schalnate5a37791996-06-05 15:50:50 -05001859 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05001860 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001861
John Bowlerb11b31a2012-03-21 07:55:46 -05001862 else if ((png_ptr->mode & PNG_HAVE_IDAT) ||
1863 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
1864 !(png_ptr->mode & PNG_HAVE_PLTE)))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001865 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001866 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001867 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001868 return;
1869 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001870
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001871 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001872 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001873 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001874 png_chunk_benign_error(png_ptr, "duplicate");
Guy Schalnate5a37791996-06-05 15:50:50 -05001875 return;
1876 }
1877
Guy Schalnat0d580581995-07-20 02:43:20 -05001878 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1879 truelen = 1;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001880
Guy Schalnat0d580581995-07-20 02:43:20 -05001881 else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
1882 truelen = 6;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001883
Guy Schalnat0d580581995-07-20 02:43:20 -05001884 else
1885 truelen = 2;
1886
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001887 if (length != truelen)
Guy Schalnat0d580581995-07-20 02:43:20 -05001888 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001889 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001890 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnat0d580581995-07-20 02:43:20 -05001891 return;
1892 }
1893
Andreas Dilger47a0c421997-05-16 02:46:07 -05001894 png_crc_read(png_ptr, buf, truelen);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001895
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001896 if (png_crc_finish(png_ptr, 0))
1897 return;
1898
Guy Schalnate5a37791996-06-05 15:50:50 -05001899 /* We convert the index value into RGB components so that we can allow
1900 * arbitrary RGB values for background when we have transparency, and
1901 * so it is easy to determine the RGB values of the background color
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001902 * from the info_ptr struct.
1903 */
Guy Schalnat0d580581995-07-20 02:43:20 -05001904 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Guy Schalnate5a37791996-06-05 15:50:50 -05001905 {
John Bowlercb0b2962011-05-12 21:48:29 -05001906 background.index = buf[0];
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001907
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001908 if (info_ptr && info_ptr->num_palette)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001909 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001910 if (buf[0] >= info_ptr->num_palette)
1911 {
John Bowlerb11b31a2012-03-21 07:55:46 -05001912 png_chunk_benign_error(png_ptr, "invalid index");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001913 return;
1914 }
1915
John Bowlercb0b2962011-05-12 21:48:29 -05001916 background.red = (png_uint_16)png_ptr->palette[buf[0]].red;
1917 background.green = (png_uint_16)png_ptr->palette[buf[0]].green;
1918 background.blue = (png_uint_16)png_ptr->palette[buf[0]].blue;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001919 }
John Bowlercb0b2962011-05-12 21:48:29 -05001920
1921 else
1922 background.red = background.green = background.blue = 0;
1923
1924 background.gray = 0;
Guy Schalnate5a37791996-06-05 15:50:50 -05001925 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001926
Andreas Dilger47a0c421997-05-16 02:46:07 -05001927 else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) /* GRAY */
Guy Schalnate5a37791996-06-05 15:50:50 -05001928 {
John Bowlercb0b2962011-05-12 21:48:29 -05001929 background.index = 0;
1930 background.red =
1931 background.green =
1932 background.blue =
1933 background.gray = png_get_uint_16(buf);
Guy Schalnate5a37791996-06-05 15:50:50 -05001934 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001935
Guy Schalnat0d580581995-07-20 02:43:20 -05001936 else
1937 {
John Bowlercb0b2962011-05-12 21:48:29 -05001938 background.index = 0;
1939 background.red = png_get_uint_16(buf);
1940 background.green = png_get_uint_16(buf + 2);
1941 background.blue = png_get_uint_16(buf + 4);
1942 background.gray = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -05001943 }
1944
John Bowlercb0b2962011-05-12 21:48:29 -05001945 png_set_bKGD(png_ptr, info_ptr, &background);
Guy Schalnat0d580581995-07-20 02:43:20 -05001946}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001947#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001948
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001949#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001950void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001951png_handle_hIST(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001952{
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001953 unsigned int num, i;
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001954 png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];
Guy Schalnat0d580581995-07-20 02:43:20 -05001955
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001956 png_debug(1, "in png_handle_hIST");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001957
Guy Schalnate5a37791996-06-05 15:50:50 -05001958 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05001959 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001960
John Bowlerb11b31a2012-03-21 07:55:46 -05001961 else if ((png_ptr->mode & PNG_HAVE_IDAT) || !(png_ptr->mode & PNG_HAVE_PLTE))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001962 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001963 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001964 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001965 return;
1966 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001967
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001968 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001969 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001970 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001971 png_chunk_benign_error(png_ptr, "duplicate");
Guy Schalnate5a37791996-06-05 15:50:50 -05001972 return;
1973 }
1974
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001975 num = length / 2 ;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001976
John Bowlerb11b31a2012-03-21 07:55:46 -05001977 if (num != png_ptr->num_palette || num > PNG_MAX_PALETTE_LENGTH)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001978 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001979 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001980 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001981 return;
1982 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001983
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001984 for (i = 0; i < num; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001985 {
1986 png_byte buf[2];
1987
1988 png_crc_read(png_ptr, buf, 2);
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06001989 readbuf[i] = png_get_uint_16(buf);
Guy Schalnat0d580581995-07-20 02:43:20 -05001990 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001991
1992 if (png_crc_finish(png_ptr, 0))
1993 return;
1994
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06001995 png_set_hIST(png_ptr, info_ptr, readbuf);
Guy Schalnat0d580581995-07-20 02:43:20 -05001996}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001997#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001998
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001999#ifdef PNG_READ_pHYs_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002000void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002001png_handle_pHYs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002002{
2003 png_byte buf[9];
2004 png_uint_32 res_x, res_y;
2005 int unit_type;
2006
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002007 png_debug(1, "in png_handle_pHYs");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002008
Guy Schalnate5a37791996-06-05 15:50:50 -05002009 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05002010 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002011
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002012 else if (png_ptr->mode & PNG_HAVE_IDAT)
2013 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002014 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002015 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002016 return;
2017 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002018
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002019 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002020 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002021 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002022 png_chunk_benign_error(png_ptr, "duplicate");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002023 return;
2024 }
Guy Schalnate5a37791996-06-05 15:50:50 -05002025
Guy Schalnat0d580581995-07-20 02:43:20 -05002026 if (length != 9)
2027 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002028 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002029 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnat0d580581995-07-20 02:43:20 -05002030 return;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002031 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002032
2033 png_crc_read(png_ptr, buf, 9);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002034
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002035 if (png_crc_finish(png_ptr, 0))
2036 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05002037
2038 res_x = png_get_uint_32(buf);
2039 res_y = png_get_uint_32(buf + 4);
2040 unit_type = buf[8];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002041 png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
Guy Schalnat0d580581995-07-20 02:43:20 -05002042}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002043#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002044
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002045#ifdef PNG_READ_oFFs_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002046void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002047png_handle_oFFs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002048{
2049 png_byte buf[9];
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002050 png_int_32 offset_x, offset_y;
Guy Schalnat0d580581995-07-20 02:43:20 -05002051 int unit_type;
2052
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002053 png_debug(1, "in png_handle_oFFs");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002054
Guy Schalnate5a37791996-06-05 15:50:50 -05002055 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05002056 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002057
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002058 else if (png_ptr->mode & PNG_HAVE_IDAT)
2059 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002060 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002061 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002062 return;
2063 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002064
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002065 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002066 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002067 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002068 png_chunk_benign_error(png_ptr, "duplicate");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002069 return;
2070 }
Guy Schalnate5a37791996-06-05 15:50:50 -05002071
Guy Schalnat0d580581995-07-20 02:43:20 -05002072 if (length != 9)
2073 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002074 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002075 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnat0d580581995-07-20 02:43:20 -05002076 return;
2077 }
2078
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002079 png_crc_read(png_ptr, buf, 9);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002080
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002081 if (png_crc_finish(png_ptr, 0))
2082 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05002083
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002084 offset_x = png_get_int_32(buf);
2085 offset_y = png_get_int_32(buf + 4);
Guy Schalnat0d580581995-07-20 02:43:20 -05002086 unit_type = buf[8];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002087 png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
2088}
2089#endif
2090
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002091#ifdef PNG_READ_pCAL_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002092/* Read the pCAL chunk (described in the PNG Extensions document) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002093void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002094png_handle_pCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002095{
Andreas Dilger47a0c421997-05-16 02:46:07 -05002096 png_int_32 X0, X1;
2097 png_byte type, nparams;
John Bowlerb5d00512012-03-09 09:15:18 -06002098 png_bytep buffer, buf, units, endptr;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002099 png_charpp params;
2100 int i;
2101
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002102 png_debug(1, "in png_handle_pCAL");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002103
2104 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05002105 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002106
Andreas Dilger47a0c421997-05-16 02:46:07 -05002107 else if (png_ptr->mode & PNG_HAVE_IDAT)
2108 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002109 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002110 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002111 return;
2112 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002113
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002114 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL))
Andreas Dilger47a0c421997-05-16 02:46:07 -05002115 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002116 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002117 png_chunk_benign_error(png_ptr, "duplicate");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002118 return;
2119 }
2120
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002121 png_debug1(2, "Allocating and reading pCAL chunk data (%u bytes)",
2122 length + 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002123
John Bowlerb11b31a2012-03-21 07:55:46 -05002124 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
John Bowlerb5d00512012-03-09 09:15:18 -06002125
2126 if (buffer == NULL)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002127 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002128 png_crc_finish(png_ptr, length);
2129 png_chunk_benign_error(png_ptr, "out of memory");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002130 return;
2131 }
2132
John Bowlerb5d00512012-03-09 09:15:18 -06002133 png_crc_read(png_ptr, buffer, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002134
2135 if (png_crc_finish(png_ptr, 0))
Andreas Dilger47a0c421997-05-16 02:46:07 -05002136 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002137
John Bowlerb5d00512012-03-09 09:15:18 -06002138 buffer[length] = 0; /* Null terminate the last string */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002139
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002140 png_debug(3, "Finding end of pCAL purpose string");
John Bowlerb5d00512012-03-09 09:15:18 -06002141 for (buf = buffer; *buf; buf++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002142 /* Empty loop */ ;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002143
John Bowlerb5d00512012-03-09 09:15:18 -06002144 endptr = buffer + length;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002145
2146 /* We need to have at least 12 bytes after the purpose string
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002147 * in order to get the parameter information.
2148 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002149 if (endptr <= buf + 12)
2150 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002151 png_chunk_benign_error(png_ptr, "invalid");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002152 return;
2153 }
2154
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002155 png_debug(3, "Reading pCAL X0, X1, type, nparams, and units");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002156 X0 = png_get_int_32((png_bytep)buf+1);
2157 X1 = png_get_int_32((png_bytep)buf+5);
2158 type = buf[9];
2159 nparams = buf[10];
2160 units = buf + 11;
2161
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002162 png_debug(3, "Checking pCAL equation type and number of parameters");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002163 /* Check that we have the right number of parameters for known
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002164 * equation types.
2165 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002166 if ((type == PNG_EQUATION_LINEAR && nparams != 2) ||
2167 (type == PNG_EQUATION_BASE_E && nparams != 3) ||
2168 (type == PNG_EQUATION_ARBITRARY && nparams != 3) ||
2169 (type == PNG_EQUATION_HYPERBOLIC && nparams != 4))
2170 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002171 png_chunk_benign_error(png_ptr, "invalid parameter count");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002172 return;
2173 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002174
Andreas Dilger47a0c421997-05-16 02:46:07 -05002175 else if (type >= PNG_EQUATION_LAST)
2176 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002177 png_chunk_benign_error(png_ptr, "unrecognized equation type");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002178 }
2179
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002180 for (buf = units; *buf; buf++)
Glenn Randers-Pehrsonf9f2fe01998-03-15 18:20:23 -06002181 /* Empty loop to move past the units string. */ ;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002182
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002183 png_debug(3, "Allocating pCAL parameters array");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002184
John Bowlerb5d00512012-03-09 09:15:18 -06002185 params = png_voidcast(png_charpp, png_malloc_warn(png_ptr,
2186 nparams * png_sizeof(png_charp)));
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002187
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002188 if (params == NULL)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002189 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002190 png_chunk_benign_error(png_ptr, "out of memory");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002191 return;
2192 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002193
2194 /* Get pointers to the start of each parameter string. */
John Bowlerb5d00512012-03-09 09:15:18 -06002195 for (i = 0; i < nparams; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002196 {
2197 buf++; /* Skip the null string terminator from previous parameter. */
2198
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002199 png_debug1(3, "Reading pCAL parameter %d", i);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002200
John Bowlerb5d00512012-03-09 09:15:18 -06002201 for (params[i] = (png_charp)buf; buf <= endptr && *buf != 0; buf++)
Glenn Randers-Pehrsonf9f2fe01998-03-15 18:20:23 -06002202 /* Empty loop to move past each parameter string */ ;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002203
2204 /* Make sure we haven't run out of data yet */
2205 if (buf > endptr)
2206 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002207 png_free(png_ptr, params);
John Bowlerb11b31a2012-03-21 07:55:46 -05002208 png_chunk_benign_error(png_ptr, "invalid data");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002209 return;
2210 }
2211 }
2212
John Bowlerb5d00512012-03-09 09:15:18 -06002213 png_set_pCAL(png_ptr, info_ptr, (png_charp)buffer, X0, X1, type, nparams,
2214 (png_charp)units, params);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002215
Andreas Dilger47a0c421997-05-16 02:46:07 -05002216 png_free(png_ptr, params);
Guy Schalnat0d580581995-07-20 02:43:20 -05002217}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002218#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002219
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002220#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002221/* Read the sCAL chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002222void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002223png_handle_sCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002224{
John Bowlerb5d00512012-03-09 09:15:18 -06002225 png_bytep buffer;
2226 png_size_t i;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002227 int state;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002228
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002229 png_debug(1, "in png_handle_sCAL");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002230
2231 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05002232 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002233
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002234 else if (png_ptr->mode & PNG_HAVE_IDAT)
2235 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002236 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002237 png_chunk_benign_error(png_ptr, "out of place");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002238 return;
2239 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002240
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002241 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002242 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002243 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002244 png_chunk_benign_error(png_ptr, "duplicate");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002245 return;
2246 }
2247
Glenn Randers-Pehrson254a5132011-06-10 17:45:48 -05002248 /* Need unit type, width, \0, height: minimum 4 bytes */
2249 else if (length < 4)
2250 {
Glenn Randers-Pehrson254a5132011-06-10 17:45:48 -05002251 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002252 png_chunk_benign_error(png_ptr, "invalid");
Glenn Randers-Pehrson254a5132011-06-10 17:45:48 -05002253 return;
2254 }
2255
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002256 png_debug1(2, "Allocating and reading sCAL chunk data (%u bytes)",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002257 length + 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002258
John Bowlerb11b31a2012-03-21 07:55:46 -05002259 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002260
John Bowlerb5d00512012-03-09 09:15:18 -06002261 if (buffer == NULL)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002262 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002263 png_chunk_benign_error(png_ptr, "out of memory");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002264 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002265 return;
2266 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002267
John Bowlerb5d00512012-03-09 09:15:18 -06002268 png_crc_read(png_ptr, buffer, length);
2269 buffer[length] = 0; /* Null terminate the last string */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002270
2271 if (png_crc_finish(png_ptr, 0))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002272 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002273
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002274 /* Validate the unit. */
John Bowlerb5d00512012-03-09 09:15:18 -06002275 if (buffer[0] != 1 && buffer[0] != 2)
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -05002276 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002277 png_chunk_benign_error(png_ptr, "invalid unit");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05002278 return;
2279 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002280
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002281 /* Validate the ASCII numbers, need two ASCII numbers separated by
2282 * a '\0' and they need to fit exactly in the chunk data.
2283 */
Glenn Randers-Pehrson254a5132011-06-10 17:45:48 -05002284 i = 1;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002285 state = 0;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002286
John Bowlerb5d00512012-03-09 09:15:18 -06002287 if (!png_check_fp_number((png_const_charp)buffer, length, &state, &i) ||
2288 i >= length || buffer[i++] != 0)
John Bowlerb11b31a2012-03-21 07:55:46 -05002289 png_chunk_benign_error(png_ptr, "bad width format");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002290
John Bowler8d261262011-06-18 13:37:11 -05002291 else if (!PNG_FP_IS_POSITIVE(state))
John Bowlerb11b31a2012-03-21 07:55:46 -05002292 png_chunk_benign_error(png_ptr, "non-positive width");
John Bowler8d261262011-06-18 13:37:11 -05002293
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002294 else
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -06002295 {
John Bowler168a4332011-01-16 19:32:22 -06002296 png_size_t heighti = i;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002297
Glenn Randers-Pehrson254a5132011-06-10 17:45:48 -05002298 state = 0;
John Bowlerb5d00512012-03-09 09:15:18 -06002299 if (!png_check_fp_number((png_const_charp)buffer, length, &state, &i) ||
2300 i != length)
John Bowlerb11b31a2012-03-21 07:55:46 -05002301 png_chunk_benign_error(png_ptr, "bad height format");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002302
John Bowler8d261262011-06-18 13:37:11 -05002303 else if (!PNG_FP_IS_POSITIVE(state))
John Bowlerb11b31a2012-03-21 07:55:46 -05002304 png_chunk_benign_error(png_ptr, "non-positive height");
John Bowler8d261262011-06-18 13:37:11 -05002305
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002306 else
2307 /* This is the (only) success case. */
John Bowlerb5d00512012-03-09 09:15:18 -06002308 png_set_sCAL_s(png_ptr, info_ptr, buffer[0],
2309 (png_charp)buffer+1, (png_charp)buffer+heighti);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002310 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002311}
2312#endif
2313
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002314#ifdef PNG_READ_tIME_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002315void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002316png_handle_tIME(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002317{
2318 png_byte buf[7];
2319 png_time mod_time;
2320
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002321 png_debug(1, "in png_handle_tIME");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002322
Guy Schalnate5a37791996-06-05 15:50:50 -05002323 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05002324 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002325
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002326 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002327 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002328 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002329 png_chunk_benign_error(png_ptr, "duplicate");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002330 return;
2331 }
2332
2333 if (png_ptr->mode & PNG_HAVE_IDAT)
2334 png_ptr->mode |= PNG_AFTER_IDAT;
Guy Schalnate5a37791996-06-05 15:50:50 -05002335
Guy Schalnat0d580581995-07-20 02:43:20 -05002336 if (length != 7)
2337 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002338 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002339 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnat0d580581995-07-20 02:43:20 -05002340 return;
2341 }
2342
2343 png_crc_read(png_ptr, buf, 7);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002344
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002345 if (png_crc_finish(png_ptr, 0))
2346 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05002347
2348 mod_time.second = buf[6];
2349 mod_time.minute = buf[5];
2350 mod_time.hour = buf[4];
2351 mod_time.day = buf[3];
2352 mod_time.month = buf[2];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002353 mod_time.year = png_get_uint_16(buf);
Guy Schalnat0d580581995-07-20 02:43:20 -05002354
Andreas Dilger47a0c421997-05-16 02:46:07 -05002355 png_set_tIME(png_ptr, info_ptr, &mod_time);
Guy Schalnat0d580581995-07-20 02:43:20 -05002356}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002357#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002358
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002359#ifdef PNG_READ_tEXt_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002360/* Note: this does not properly handle chunks that are > 64K under DOS */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002361void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002362png_handle_tEXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002363{
John Bowlerb5d00512012-03-09 09:15:18 -06002364 png_text text_info;
2365 png_bytep buffer;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002366 png_charp key;
Guy Schalnat6d764711995-12-19 03:22:19 -06002367 png_charp text;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002368 png_uint_32 skip = 0;
2369
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002370 png_debug(1, "in png_handle_tEXt");
Guy Schalnat0d580581995-07-20 02:43:20 -05002371
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06002372#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002373 if (png_ptr->user_chunk_cache_max != 0)
2374 {
2375 if (png_ptr->user_chunk_cache_max == 1)
2376 {
2377 png_crc_finish(png_ptr, length);
2378 return;
2379 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002380
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002381 if (--png_ptr->user_chunk_cache_max == 1)
2382 {
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002383 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002384 png_chunk_benign_error(png_ptr, "no space in chunk cache");
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002385 return;
2386 }
2387 }
2388#endif
2389
Guy Schalnate5a37791996-06-05 15:50:50 -05002390 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05002391 png_chunk_error(png_ptr, "missing IHDR");
Guy Schalnate5a37791996-06-05 15:50:50 -05002392
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002393 if (png_ptr->mode & PNG_HAVE_IDAT)
2394 png_ptr->mode |= PNG_AFTER_IDAT;
2395
Andreas Dilger47a0c421997-05-16 02:46:07 -05002396#ifdef PNG_MAX_MALLOC_64K
John Bowlerb11b31a2012-03-21 07:55:46 -05002397 if (length > 65535U)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002398 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002399 png_crc_finish(png_ptr, length);
2400 png_chunk_benign_error(png_ptr, "too large to fit in memory");
2401 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002402 }
2403#endif
2404
John Bowlerb5d00512012-03-09 09:15:18 -06002405 buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
Glenn Randers-Pehrson97a9b482008-10-25 20:03:28 -05002406
John Bowlerb5d00512012-03-09 09:15:18 -06002407 if (buffer == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002408 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002409 png_chunk_benign_error(png_ptr, "out of memory");
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002410 return;
2411 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002412
John Bowlerb5d00512012-03-09 09:15:18 -06002413 png_crc_read(png_ptr, buffer, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002414
2415 if (png_crc_finish(png_ptr, skip))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002416 return;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002417
John Bowlerb5d00512012-03-09 09:15:18 -06002418 key = (png_charp)buffer;
2419 key[length] = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -05002420
2421 for (text = key; *text; text++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002422 /* Empty loop to find end of key */ ;
Guy Schalnat0d580581995-07-20 02:43:20 -05002423
John Bowler0ae4f7b2012-03-03 21:10:26 -06002424 if (text != key + length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002425 text++;
2426
John Bowlerb5d00512012-03-09 09:15:18 -06002427 text_info.compression = PNG_TEXT_COMPRESSION_NONE;
2428 text_info.key = key;
2429 text_info.lang = NULL;
2430 text_info.lang_key = NULL;
2431 text_info.itxt_length = 0;
2432 text_info.text = text;
2433 text_info.text_length = png_strlen(text);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002434
John Bowlerb5d00512012-03-09 09:15:18 -06002435 if (png_set_text_2(png_ptr, info_ptr, &text_info, 1))
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002436 png_warning(png_ptr, "Insufficient memory to process text chunk");
Guy Schalnat0d580581995-07-20 02:43:20 -05002437}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002438#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002439
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002440#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002441/* Note: this does not correctly handle chunks that are > 64K under DOS */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002442void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002443png_handle_zTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002444{
John Bowler0ae4f7b2012-03-03 21:10:26 -06002445 png_const_charp errmsg = NULL;
John Bowlerb5d00512012-03-09 09:15:18 -06002446 png_bytep buffer;
2447 png_uint_32 keyword_length;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002448
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002449 png_debug(1, "in png_handle_zTXt");
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002450
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06002451#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002452 if (png_ptr->user_chunk_cache_max != 0)
2453 {
2454 if (png_ptr->user_chunk_cache_max == 1)
2455 {
2456 png_crc_finish(png_ptr, length);
2457 return;
2458 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002459
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002460 if (--png_ptr->user_chunk_cache_max == 1)
2461 {
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002462 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002463 png_chunk_benign_error(png_ptr, "no space in chunk cache");
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002464 return;
2465 }
2466 }
2467#endif
2468
Guy Schalnate5a37791996-06-05 15:50:50 -05002469 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05002470 png_chunk_error(png_ptr, "missing IHDR");
Guy Schalnate5a37791996-06-05 15:50:50 -05002471
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002472 if (png_ptr->mode & PNG_HAVE_IDAT)
2473 png_ptr->mode |= PNG_AFTER_IDAT;
2474
John Bowlerb11b31a2012-03-21 07:55:46 -05002475 buffer = png_read_buffer(png_ptr, length, 2/*silent*/);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002476
John Bowlerb5d00512012-03-09 09:15:18 -06002477 if (buffer == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002478 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002479 png_crc_finish(png_ptr, length);
2480 png_chunk_benign_error(png_ptr, "out of memory");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002481 return;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002482 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002483
John Bowlerb5d00512012-03-09 09:15:18 -06002484 png_crc_read(png_ptr, buffer, length);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002485
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002486 if (png_crc_finish(png_ptr, 0))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002487 return;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002488
John Bowler0ae4f7b2012-03-03 21:10:26 -06002489 /* TODO: also check that the keyword contents match the spec! */
2490 for (keyword_length = 0;
John Bowlerb5d00512012-03-09 09:15:18 -06002491 keyword_length < length && buffer[keyword_length] != 0;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002492 ++keyword_length)
2493 /* Empty loop to find end of name */ ;
Guy Schalnat0d580581995-07-20 02:43:20 -05002494
John Bowler0ae4f7b2012-03-03 21:10:26 -06002495 if (keyword_length > 79 || keyword_length < 1)
2496 errmsg = "bad keyword";
Guy Schalnat0d580581995-07-20 02:43:20 -05002497
John Bowler0ae4f7b2012-03-03 21:10:26 -06002498 /* zTXt must have some LZ data after the keyword, although it may expand to
2499 * zero bytes; we need a '\0' at the end of the keyword, the compression type
2500 * then the LZ data:
2501 */
2502 else if (keyword_length + 3 > length)
2503 errmsg = "truncated";
2504
John Bowlerb5d00512012-03-09 09:15:18 -06002505 else if (buffer[keyword_length+1] != PNG_COMPRESSION_TYPE_BASE)
John Bowler0ae4f7b2012-03-03 21:10:26 -06002506 errmsg = "unknown compression type";
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002507
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002508 else
Guy Schalnat0d580581995-07-20 02:43:20 -05002509 {
John Bowler0ae4f7b2012-03-03 21:10:26 -06002510 png_alloc_size_t uncompressed_length = PNG_SIZE_MAX;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002511
John Bowler0ae4f7b2012-03-03 21:10:26 -06002512 /* TODO: at present png_decompress_chunk imposes a single application
2513 * level memory limit, this should be split to different values for iCCP
2514 * and text chunks.
2515 */
John Bowlerb5d00512012-03-09 09:15:18 -06002516 if (png_decompress_chunk(png_ptr, length, keyword_length+2,
2517 &uncompressed_length, 1/*terminate*/) == Z_STREAM_END)
John Bowler0ae4f7b2012-03-03 21:10:26 -06002518 {
2519 png_text text;
2520
John Bowlerb5d00512012-03-09 09:15:18 -06002521 /* It worked; png_ptr->read_buffer now looks like a tEXt chunk except
2522 * for the extra compression type byte and the fact that it isn't
John Bowler0ae4f7b2012-03-03 21:10:26 -06002523 * necessarily '\0' terminated.
2524 */
John Bowlerb5d00512012-03-09 09:15:18 -06002525 buffer = png_ptr->read_buffer;
2526 buffer[uncompressed_length+(keyword_length+2)] = 0;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002527
2528 text.compression = PNG_TEXT_COMPRESSION_zTXt;
John Bowlerb5d00512012-03-09 09:15:18 -06002529 text.key = (png_charp)buffer;
2530 text.text = (png_charp)(buffer + keyword_length+2);
John Bowler0ae4f7b2012-03-03 21:10:26 -06002531 text.text_length = uncompressed_length;
2532 text.itxt_length = 0;
2533 text.lang = NULL;
2534 text.lang_key = NULL;
2535
2536 if (png_set_text_2(png_ptr, info_ptr, &text, 1))
John Bowlerb11b31a2012-03-21 07:55:46 -05002537 errmsg = "insufficient memory";
John Bowler0ae4f7b2012-03-03 21:10:26 -06002538 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002539
John Bowlerb5d00512012-03-09 09:15:18 -06002540 else
2541 errmsg = png_ptr->zstream.msg;
2542 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002543
John Bowler0ae4f7b2012-03-03 21:10:26 -06002544 if (errmsg != NULL)
2545 png_chunk_benign_error(png_ptr, errmsg);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002546}
2547#endif
2548
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002549#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002550/* Note: this does not correctly handle chunks that are > 64K under DOS */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002551void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002552png_handle_iTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002553{
John Bowler0ae4f7b2012-03-03 21:10:26 -06002554 png_const_charp errmsg = NULL;
John Bowlerb5d00512012-03-09 09:15:18 -06002555 png_bytep buffer;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002556 png_uint_32 prefix_length;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002557
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002558 png_debug(1, "in png_handle_iTXt");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002559
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06002560#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002561 if (png_ptr->user_chunk_cache_max != 0)
2562 {
2563 if (png_ptr->user_chunk_cache_max == 1)
2564 {
2565 png_crc_finish(png_ptr, length);
2566 return;
2567 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002568
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002569 if (--png_ptr->user_chunk_cache_max == 1)
2570 {
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002571 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002572 png_chunk_benign_error(png_ptr, "no space in chunk cache");
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002573 return;
2574 }
2575 }
2576#endif
2577
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002578 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05002579 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002580
2581 if (png_ptr->mode & PNG_HAVE_IDAT)
2582 png_ptr->mode |= PNG_AFTER_IDAT;
2583
John Bowlerb5d00512012-03-09 09:15:18 -06002584 buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002585
John Bowlerb5d00512012-03-09 09:15:18 -06002586 if (buffer == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002587 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002588 png_crc_finish(png_ptr, length);
2589 png_chunk_benign_error(png_ptr, "out of memory");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002590 return;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002591 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002592
John Bowlerb5d00512012-03-09 09:15:18 -06002593 png_crc_read(png_ptr, buffer, length);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002594
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002595 if (png_crc_finish(png_ptr, 0))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002596 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002597
John Bowler0ae4f7b2012-03-03 21:10:26 -06002598 /* First the keyword. */
2599 for (prefix_length=0;
John Bowlerb5d00512012-03-09 09:15:18 -06002600 prefix_length < length && buffer[prefix_length] != 0;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002601 ++prefix_length)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002602 /* Empty loop */ ;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002603
John Bowler0ae4f7b2012-03-03 21:10:26 -06002604 /* Perform a basic check on the keyword length here. */
2605 if (prefix_length > 79 || prefix_length < 1)
2606 errmsg = "bad keyword";
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002607
John Bowler0ae4f7b2012-03-03 21:10:26 -06002608 /* Expect keyword, compression flag, compression type, language, translated
2609 * keyword (both may be empty but are 0 terminated) then the text, which may
2610 * be empty.
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002611 */
John Bowler0ae4f7b2012-03-03 21:10:26 -06002612 else if (prefix_length + 5 > length)
2613 errmsg = "truncated";
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002614
John Bowlerb5d00512012-03-09 09:15:18 -06002615 else if (buffer[prefix_length+1] == 0 ||
2616 (buffer[prefix_length+1] == 1 &&
2617 buffer[prefix_length+2] == PNG_COMPRESSION_TYPE_BASE))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002618 {
John Bowlerb5d00512012-03-09 09:15:18 -06002619 int compressed = buffer[prefix_length+1] != 0;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002620 png_uint_32 language_offset, translated_keyword_offset;
2621 png_alloc_size_t uncompressed_length = 0;
2622
2623 /* Now the language tag */
2624 prefix_length += 3;
2625 language_offset = prefix_length;
2626
John Bowlerb5d00512012-03-09 09:15:18 -06002627 for (; prefix_length < length && buffer[prefix_length] != 0;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002628 ++prefix_length)
2629 /* Empty loop */ ;
2630
2631 /* WARNING: the length may be invalid here, this is checked below. */
2632 translated_keyword_offset = ++prefix_length;
2633
John Bowlerb5d00512012-03-09 09:15:18 -06002634 for (; prefix_length < length && buffer[prefix_length] != 0;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002635 ++prefix_length)
2636 /* Empty loop */ ;
2637
2638 /* prefix_length should now be at the trailing '\0' of the translated
2639 * keyword, but it may already be over the end. None of this arithmetic
2640 * can overflow because chunks are at most 2^31 bytes long, but on 16-bit
2641 * systems the available allocaton may overflow.
2642 */
2643 ++prefix_length;
2644
2645 if (!compressed && prefix_length <= length)
2646 uncompressed_length = length - prefix_length;
2647
2648 else if (compressed && prefix_length < length)
2649 {
2650 uncompressed_length = PNG_SIZE_MAX;
2651
2652 /* TODO: at present png_decompress_chunk imposes a single application
2653 * level memory limit, this should be split to different values for
2654 * iCCP and text chunks.
2655 */
John Bowlerb5d00512012-03-09 09:15:18 -06002656 if (png_decompress_chunk(png_ptr, length, prefix_length,
2657 &uncompressed_length, 1/*terminate*/) == Z_STREAM_END)
2658 buffer = png_ptr->read_buffer;
2659
2660 else
2661 errmsg = png_ptr->zstream.msg;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002662 }
2663
2664 else
2665 errmsg = "truncated";
2666
2667 if (errmsg == NULL)
2668 {
2669 png_text text;
2670
John Bowlerb5d00512012-03-09 09:15:18 -06002671 buffer[uncompressed_length+prefix_length] = 0;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002672
2673 if (compressed)
2674 text.compression = PNG_ITXT_COMPRESSION_NONE;
2675
2676 else
2677 text.compression = PNG_ITXT_COMPRESSION_zTXt;
2678
John Bowlerb5d00512012-03-09 09:15:18 -06002679 text.key = (png_charp)buffer;
2680 text.lang = (png_charp)buffer + language_offset;
2681 text.lang_key = (png_charp)buffer + translated_keyword_offset;
2682 text.text = (png_charp)buffer + prefix_length;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002683 text.text_length = 0;
2684 text.itxt_length = uncompressed_length;
2685
2686 if (png_set_text_2(png_ptr, info_ptr, &text, 1))
2687 errmsg = "insufficient memory";
2688 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002689 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002690
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002691 else
John Bowler0ae4f7b2012-03-03 21:10:26 -06002692 errmsg = "bad compression info";
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002693
John Bowler0ae4f7b2012-03-03 21:10:26 -06002694 if (errmsg != NULL)
2695 png_chunk_benign_error(png_ptr, errmsg);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002696}
2697#endif
2698
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002699/* This function is called when we haven't found a handler for a
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002700 * chunk. If there isn't a problem with the chunk itself (ie bad
2701 * chunk name, CRC, or a critical chunk), the chunk is silently ignored
2702 * -- unless the PNG_FLAG_UNKNOWN_CHUNKS_SUPPORTED flag is on in which
2703 * case it will be saved away to be written out later.
2704 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002705void /* PRIVATE */
John Bowlerb11b31a2012-03-21 07:55:46 -05002706png_handle_unknown(png_structrp png_ptr, png_inforp info_ptr,
2707 png_uint_32 length)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002708{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002709 png_uint_32 skip = 0;
2710
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002711 png_debug(1, "in png_handle_unknown");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002712
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06002713#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002714 if (png_ptr->user_chunk_cache_max != 0)
2715 {
2716 if (png_ptr->user_chunk_cache_max == 1)
2717 {
2718 png_crc_finish(png_ptr, length);
2719 return;
2720 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002721
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002722 if (--png_ptr->user_chunk_cache_max == 1)
2723 {
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002724 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002725 png_chunk_benign_error(png_ptr, "no space in chunk cache");
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002726 return;
2727 }
2728 }
2729#endif
2730
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002731 if (png_ptr->mode & PNG_HAVE_IDAT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002732 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002733 if (png_ptr->chunk_name != png_IDAT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002734 png_ptr->mode |= PNG_AFTER_IDAT;
2735 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002736
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002737 if (PNG_CHUNK_CRITICAL(png_ptr->chunk_name))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002738 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002739#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002740 if (png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name) !=
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002741 PNG_HANDLE_CHUNK_ALWAYS
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002742#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002743 && png_ptr->read_user_chunk_fn == NULL
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002744#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002745 )
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05002746#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002747 png_chunk_error(png_ptr, "unknown critical chunk");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002748 }
2749
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002750#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson7824a702009-06-13 10:05:05 -05002751 if ((png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS)
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002752#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
Glenn Randers-Pehrson7824a702009-06-13 10:05:05 -05002753 || (png_ptr->read_user_chunk_fn != NULL)
2754#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002755 )
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002756 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002757#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002758 if (length > 65535)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002759 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002760 png_crc_finish(png_ptr, length);
2761 png_chunk_benign_error(png_ptr,
2762 "unknown chunk too large to fit in memory");
2763 return;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002764 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002765#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002766
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002767 /* TODO: this code is very close to the unknown handling in pngpread.c,
2768 * maybe it can be put into a common utility routine?
2769 * png_struct::unknown_chunk is just used as a temporary variable, along
2770 * with the data into which the chunk is read. These can be eliminated.
2771 */
2772 PNG_CSTRING_FROM_CHUNK(png_ptr->unknown_chunk.name, png_ptr->chunk_name);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002773 png_ptr->unknown_chunk.size = (png_size_t)length;
2774
2775 if (length == 0)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002776 png_ptr->unknown_chunk.data = NULL;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002777
2778 else
2779 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002780 png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr, length);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002781 png_crc_read(png_ptr, png_ptr->unknown_chunk.data, length);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002782 }
2783
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002784#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002785 if (png_ptr->read_user_chunk_fn != NULL)
2786 {
2787 /* Callback to user unknown chunk handler */
2788 int ret;
2789
2790 ret = (*(png_ptr->read_user_chunk_fn))
2791 (png_ptr, &png_ptr->unknown_chunk);
2792
2793 if (ret < 0)
2794 png_chunk_error(png_ptr, "error in user chunk");
2795
2796 if (ret == 0)
2797 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002798 if (PNG_CHUNK_CRITICAL(png_ptr->chunk_name))
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002799 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002800#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002801 if (png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name) !=
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002802 PNG_HANDLE_CHUNK_ALWAYS)
Glenn Randers-Pehrson7824a702009-06-13 10:05:05 -05002803#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002804 png_chunk_error(png_ptr, "unknown critical chunk");
2805 }
2806
2807 png_set_unknown_chunks(png_ptr, info_ptr,
2808 &png_ptr->unknown_chunk, 1);
2809 }
2810 }
2811
2812 else
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002813#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002814 png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1);
2815
2816 png_free(png_ptr, png_ptr->unknown_chunk.data);
2817 png_ptr->unknown_chunk.data = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002818 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002819
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002820 else
2821#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002822 skip = length;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002823
2824 png_crc_finish(png_ptr, skip);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002825
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -05002826#ifndef PNG_READ_USER_CHUNKS_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002827 PNG_UNUSED(info_ptr) /* Quiet compiler warnings about unused info_ptr */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002828#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002829}
2830
2831/* This function is called to verify that a chunk name is valid.
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002832 * This function can't have the "critical chunk check" incorporated
2833 * into it, since in the future we will need to be able to call user
2834 * functions to handle unknown critical chunks after we check that
2835 * the chunk name itself is valid.
2836 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002837
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002838/* Bit hacking: the test for an invalid byte in the 4 byte chunk name is:
2839 *
2840 * ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
2841 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002842
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002843void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002844png_check_chunk_name(png_structrp png_ptr, png_uint_32 chunk_name)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002845{
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002846 int i;
2847
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002848 png_debug(1, "in png_check_chunk_name");
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002849
2850 for (i=1; i<=4; ++i)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002851 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002852 int c = chunk_name & 0xff;
2853
2854 if (c < 65 || c > 122 || (c > 90 && c < 97))
2855 png_chunk_error(png_ptr, "invalid chunk type");
2856
2857 chunk_name >>= 8;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002858 }
2859}
2860
John Bowler7875d532011-11-07 22:33:49 -06002861/* Combines the row recently read in with the existing pixels in the row. This
2862 * routine takes care of alpha and transparency if requested. This routine also
2863 * handles the two methods of progressive display of interlaced images,
2864 * depending on the 'display' value; if 'display' is true then the whole row
2865 * (dp) is filled from the start by replicating the available pixels. If
2866 * 'display' is false only those pixels present in the pass are filled in.
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002867 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002868void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002869png_combine_row(png_const_structrp png_ptr, png_bytep dp, int display)
Guy Schalnat0d580581995-07-20 02:43:20 -05002870{
John Bowler4e68aa72011-10-11 16:01:33 -05002871 unsigned int pixel_depth = png_ptr->transformed_pixel_depth;
2872 png_const_bytep sp = png_ptr->row_buf + 1;
John Bowlerac8375d2011-10-06 22:27:16 -05002873 png_uint_32 row_width = png_ptr->width;
John Bowler4e68aa72011-10-11 16:01:33 -05002874 unsigned int pass = png_ptr->pass;
John Bowlerfb5b3ac2011-10-16 22:52:56 -05002875 png_bytep end_ptr = 0;
2876 png_byte end_byte = 0;
2877 unsigned int end_mask;
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002878
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002879 png_debug(1, "in png_combine_row");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002880
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002881 /* Added in 1.5.6: it should not be possible to enter this routine until at
2882 * least one row has been read from the PNG data and transformed.
2883 */
2884 if (pixel_depth == 0)
2885 png_error(png_ptr, "internal row logic error");
2886
2887 /* Added in 1.5.4: the pixel depth should match the information returned by
2888 * any call to png_read_update_info at this point. Do not continue if we got
John Bowler9994f252011-05-15 18:52:39 -05002889 * this wrong.
2890 */
2891 if (png_ptr->info_rowbytes != 0 && png_ptr->info_rowbytes !=
John Bowlerac8375d2011-10-06 22:27:16 -05002892 PNG_ROWBYTES(pixel_depth, row_width))
John Bowler9994f252011-05-15 18:52:39 -05002893 png_error(png_ptr, "internal row size calculation error");
2894
John Bowlerac8375d2011-10-06 22:27:16 -05002895 /* Don't expect this to ever happen: */
2896 if (row_width == 0)
2897 png_error(png_ptr, "internal row width error");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002898
John Bowlerfb5b3ac2011-10-16 22:52:56 -05002899 /* Preserve the last byte in cases where only part of it will be overwritten,
2900 * the multiply below may overflow, we don't care because ANSI-C guarantees
2901 * we get the low bits.
2902 */
2903 end_mask = (pixel_depth * row_width) & 7;
2904 if (end_mask != 0)
2905 {
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05002906 /* end_ptr == NULL is a flag to say do nothing */
John Bowlerfb5b3ac2011-10-16 22:52:56 -05002907 end_ptr = dp + PNG_ROWBYTES(pixel_depth, row_width) - 1;
2908 end_byte = *end_ptr;
2909# ifdef PNG_READ_PACKSWAP_SUPPORTED
2910 if (png_ptr->transformations & PNG_PACKSWAP) /* little-endian byte */
2911 end_mask = 0xff << end_mask;
2912
2913 else /* big-endian byte */
2914# endif
2915 end_mask = 0xff >> end_mask;
2916 /* end_mask is now the bits to *keep* from the destination row */
2917 }
2918
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05002919 /* For non-interlaced images this reduces to a png_memcpy(). A png_memcpy()
2920 * will also happen if interlacing isn't supported or if the application
2921 * does not call png_set_interlace_handling(). In the latter cases the
2922 * caller just gets a sequence of the unexpanded rows from each interlace
2923 * pass.
John Bowlerac8375d2011-10-06 22:27:16 -05002924 */
2925#ifdef PNG_READ_INTERLACING_SUPPORTED
2926 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE) &&
John Bowler4e68aa72011-10-11 16:01:33 -05002927 pass < 6 && (display == 0 ||
2928 /* The following copies everything for 'display' on passes 0, 2 and 4. */
2929 (display == 1 && (pass & 1) != 0)))
Guy Schalnat0d580581995-07-20 02:43:20 -05002930 {
John Bowler4e68aa72011-10-11 16:01:33 -05002931 /* Narrow images may have no bits in a pass; the caller should handle
2932 * this, but this test is cheap:
John Bowlerac8375d2011-10-06 22:27:16 -05002933 */
John Bowler4e68aa72011-10-11 16:01:33 -05002934 if (row_width <= PNG_PASS_START_COL(pass))
2935 return;
John Bowlerac8375d2011-10-06 22:27:16 -05002936
John Bowler4e68aa72011-10-11 16:01:33 -05002937 if (pixel_depth < 8)
Guy Schalnat0d580581995-07-20 02:43:20 -05002938 {
John Bowler7875d532011-11-07 22:33:49 -06002939 /* For pixel depths up to 4 bpp the 8-pixel mask can be expanded to fit
John Bowler4e68aa72011-10-11 16:01:33 -05002940 * into 32 bits, then a single loop over the bytes using the four byte
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05002941 * values in the 32-bit mask can be used. For the 'display' option the
John Bowler4e68aa72011-10-11 16:01:33 -05002942 * expanded mask may also not require any masking within a byte. To
2943 * make this work the PACKSWAP option must be taken into account - it
2944 * simply requires the pixels to be reversed in each byte.
2945 *
2946 * The 'regular' case requires a mask for each of the first 6 passes,
2947 * the 'display' case does a copy for the even passes in the range
John Bowler7875d532011-11-07 22:33:49 -06002948 * 0..6. This has already been handled in the test above.
John Bowler4e68aa72011-10-11 16:01:33 -05002949 *
2950 * The masks are arranged as four bytes with the first byte to use in
2951 * the lowest bits (little-endian) regardless of the order (PACKSWAP or
2952 * not) of the pixels in each byte.
2953 *
2954 * NOTE: the whole of this logic depends on the caller of this function
2955 * only calling it on rows appropriate to the pass. This function only
John Bowler7875d532011-11-07 22:33:49 -06002956 * understands the 'x' logic; the 'y' logic is handled by the caller.
John Bowler4e68aa72011-10-11 16:01:33 -05002957 *
2958 * The following defines allow generation of compile time constant bit
2959 * masks for each pixel depth and each possibility of swapped or not
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05002960 * swapped bytes. Pass 'p' is in the range 0..6; 'x', a pixel index,
2961 * is in the range 0..7; and the result is 1 if the pixel is to be
2962 * copied in the pass, 0 if not. 'S' is for the sparkle method, 'B'
2963 * for the block method.
2964 *
John Bowler92ef3132011-10-27 19:36:08 -05002965 * With some compilers a compile time expression of the general form:
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05002966 *
John Bowler92ef3132011-10-27 19:36:08 -05002967 * (shift >= 32) ? (a >> (shift-32)) : (b >> shift)
2968 *
2969 * Produces warnings with values of 'shift' in the range 33 to 63
Glenn Randers-Pehrson18763662011-10-27 22:09:22 -05002970 * because the right hand side of the ?: expression is evaluated by
John Bowler92ef3132011-10-27 19:36:08 -05002971 * the compiler even though it isn't used. Microsoft Visual C (various
2972 * versions) and the Intel C compiler are known to do this. To avoid
2973 * this the following macros are used in 1.5.6. This is a temporary
John Bowler7875d532011-11-07 22:33:49 -06002974 * solution to avoid destabilizing the code during the release process.
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05002975 */
John Bowler92ef3132011-10-27 19:36:08 -05002976# if PNG_USE_COMPILE_TIME_MASKS
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05002977# define PNG_LSR(x,s) ((x)>>((s) & 0x1f))
2978# define PNG_LSL(x,s) ((x)<<((s) & 0x1f))
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05002979# else
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05002980# define PNG_LSR(x,s) ((x)>>(s))
2981# define PNG_LSL(x,s) ((x)<<(s))
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05002982# endif
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05002983# define S_COPY(p,x) (((p)<4 ? PNG_LSR(0x80088822,(3-(p))*8+(7-(x))) :\
2984 PNG_LSR(0xaa55ff00,(7-(p))*8+(7-(x)))) & 1)
2985# define B_COPY(p,x) (((p)<4 ? PNG_LSR(0xff0fff33,(3-(p))*8+(7-(x))) :\
2986 PNG_LSR(0xff55ff00,(7-(p))*8+(7-(x)))) & 1)
Guy Schalnat0d580581995-07-20 02:43:20 -05002987
John Bowler4e68aa72011-10-11 16:01:33 -05002988 /* Return a mask for pass 'p' pixel 'x' at depth 'd'. The mask is
2989 * little endian - the first pixel is at bit 0 - however the extra
2990 * parameter 's' can be set to cause the mask position to be swapped
2991 * within each byte, to match the PNG format. This is done by XOR of
2992 * the shift with 7, 6 or 4 for bit depths 1, 2 and 4.
2993 */
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05002994# define PIXEL_MASK(p,x,d,s) \
2995 (PNG_LSL(((PNG_LSL(1U,(d)))-1),(((x)*(d))^((s)?8-(d):0))))
John Bowlerac8375d2011-10-06 22:27:16 -05002996
John Bowler4e68aa72011-10-11 16:01:33 -05002997 /* Hence generate the appropriate 'block' or 'sparkle' pixel copy mask.
2998 */
2999# define S_MASKx(p,x,d,s) (S_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
3000# 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 -05003001
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003002 /* Combine 8 of these to get the full mask. For the 1-bpp and 2-bpp
3003 * cases the result needs replicating, for the 4-bpp case the above
3004 * generates a full 32 bits.
John Bowler4e68aa72011-10-11 16:01:33 -05003005 */
3006# define MASK_EXPAND(m,d) ((m)*((d)==1?0x01010101:((d)==2?0x00010001:1)))
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003007
John Bowler4e68aa72011-10-11 16:01:33 -05003008# define S_MASK(p,d,s) MASK_EXPAND(S_MASKx(p,0,d,s) + S_MASKx(p,1,d,s) +\
3009 S_MASKx(p,2,d,s) + S_MASKx(p,3,d,s) + S_MASKx(p,4,d,s) +\
3010 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 -06003011
John Bowler4e68aa72011-10-11 16:01:33 -05003012# define B_MASK(p,d,s) MASK_EXPAND(B_MASKx(p,0,d,s) + B_MASKx(p,1,d,s) +\
3013 B_MASKx(p,2,d,s) + B_MASKx(p,3,d,s) + B_MASKx(p,4,d,s) +\
3014 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 -05003015
John Bowler4e68aa72011-10-11 16:01:33 -05003016#if PNG_USE_COMPILE_TIME_MASKS
3017 /* Utility macros to construct all the masks for a depth/swap
3018 * combination. The 's' parameter says whether the format is PNG
John Bowler7875d532011-11-07 22:33:49 -06003019 * (big endian bytes) or not. Only the three odd-numbered passes are
John Bowler4e68aa72011-10-11 16:01:33 -05003020 * required for the display/block algorithm.
3021 */
3022# define S_MASKS(d,s) { S_MASK(0,d,s), S_MASK(1,d,s), S_MASK(2,d,s),\
3023 S_MASK(3,d,s), S_MASK(4,d,s), S_MASK(5,d,s) }
John Bowlerac8375d2011-10-06 22:27:16 -05003024
John Bowler4e68aa72011-10-11 16:01:33 -05003025# 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 -05003026
John Bowler4e68aa72011-10-11 16:01:33 -05003027# define DEPTH_INDEX(d) ((d)==1?0:((d)==2?1:2))
John Bowlerac8375d2011-10-06 22:27:16 -05003028
John Bowler4e68aa72011-10-11 16:01:33 -05003029 /* Hence the pre-compiled masks indexed by PACKSWAP (or not), depth and
3030 * then pass:
3031 */
John Bowler7875d532011-11-07 22:33:49 -06003032 static PNG_CONST png_uint_32 row_mask[2/*PACKSWAP*/][3/*depth*/][6] =
3033 {
John Bowler4e68aa72011-10-11 16:01:33 -05003034 /* Little-endian byte masks for PACKSWAP */
3035 { S_MASKS(1,0), S_MASKS(2,0), S_MASKS(4,0) },
3036 /* Normal (big-endian byte) masks - PNG format */
3037 { S_MASKS(1,1), S_MASKS(2,1), S_MASKS(4,1) }
3038 };
John Bowlerac8375d2011-10-06 22:27:16 -05003039
John Bowler4e68aa72011-10-11 16:01:33 -05003040 /* display_mask has only three entries for the odd passes, so index by
3041 * pass>>1.
3042 */
John Bowler7875d532011-11-07 22:33:49 -06003043 static PNG_CONST png_uint_32 display_mask[2][3][3] =
3044 {
John Bowler4e68aa72011-10-11 16:01:33 -05003045 /* Little-endian byte masks for PACKSWAP */
3046 { B_MASKS(1,0), B_MASKS(2,0), B_MASKS(4,0) },
3047 /* Normal (big-endian byte) masks - PNG format */
3048 { B_MASKS(1,1), B_MASKS(2,1), B_MASKS(4,1) }
3049 };
John Bowlerac8375d2011-10-06 22:27:16 -05003050
John Bowler4e68aa72011-10-11 16:01:33 -05003051# define MASK(pass,depth,display,png)\
3052 ((display)?display_mask[png][DEPTH_INDEX(depth)][pass>>1]:\
3053 row_mask[png][DEPTH_INDEX(depth)][pass])
John Bowlerac8375d2011-10-06 22:27:16 -05003054
John Bowler4e68aa72011-10-11 16:01:33 -05003055#else /* !PNG_USE_COMPILE_TIME_MASKS */
3056 /* This is the runtime alternative: it seems unlikely that this will
3057 * ever be either smaller or faster than the compile time approach.
3058 */
3059# define MASK(pass,depth,display,png)\
3060 ((display)?B_MASK(pass,depth,png):S_MASK(pass,depth,png))
3061#endif /* !PNG_USE_COMPILE_TIME_MASKS */
John Bowlerac8375d2011-10-06 22:27:16 -05003062
John Bowler4e68aa72011-10-11 16:01:33 -05003063 /* Use the appropriate mask to copy the required bits. In some cases
3064 * the byte mask will be 0 or 0xff, optimize these cases. row_width is
3065 * the number of pixels, but the code copies bytes, so it is necessary
3066 * to special case the end.
3067 */
3068 png_uint_32 pixels_per_byte = 8 / pixel_depth;
3069 png_uint_32 mask;
John Bowlerac8375d2011-10-06 22:27:16 -05003070
John Bowler4e68aa72011-10-11 16:01:33 -05003071# ifdef PNG_READ_PACKSWAP_SUPPORTED
3072 if (png_ptr->transformations & PNG_PACKSWAP)
3073 mask = MASK(pass, pixel_depth, display, 0);
John Bowlerac8375d2011-10-06 22:27:16 -05003074
3075 else
John Bowler4e68aa72011-10-11 16:01:33 -05003076# endif
3077 mask = MASK(pass, pixel_depth, display, 1);
3078
3079 for (;;)
3080 {
3081 png_uint_32 m;
3082
3083 /* It doesn't matter in the following if png_uint_32 has more than
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003084 * 32 bits because the high bits always match those in m<<24; it is,
John Bowler4e68aa72011-10-11 16:01:33 -05003085 * however, essential to use OR here, not +, because of this.
3086 */
3087 m = mask;
3088 mask = (m >> 8) | (m << 24); /* rotate right to good compilers */
3089 m &= 0xff;
3090
3091 if (m != 0) /* something to copy */
John Bowlerac8375d2011-10-06 22:27:16 -05003092 {
John Bowler4e68aa72011-10-11 16:01:33 -05003093 if (m != 0xff)
3094 *dp = (png_byte)((*dp & ~m) | (*sp & m));
3095 else
3096 *dp = *sp;
John Bowlerac8375d2011-10-06 22:27:16 -05003097 }
John Bowler4e68aa72011-10-11 16:01:33 -05003098
3099 /* NOTE: this may overwrite the last byte with garbage if the image
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003100 * is not an exact number of bytes wide; libpng has always done
John Bowler4e68aa72011-10-11 16:01:33 -05003101 * this.
3102 */
3103 if (row_width <= pixels_per_byte)
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003104 break; /* May need to restore part of the last byte */
John Bowler4e68aa72011-10-11 16:01:33 -05003105
3106 row_width -= pixels_per_byte;
3107 ++dp;
3108 ++sp;
3109 }
3110 }
3111
3112 else /* pixel_depth >= 8 */
3113 {
3114 unsigned int bytes_to_copy, bytes_to_jump;
3115
3116 /* Validate the depth - it must be a multiple of 8 */
3117 if (pixel_depth & 7)
3118 png_error(png_ptr, "invalid user transform pixel depth");
3119
3120 pixel_depth >>= 3; /* now in bytes */
3121 row_width *= pixel_depth;
3122
3123 /* Regardless of pass number the Adam 7 interlace always results in a
3124 * fixed number of pixels to copy then to skip. There may be a
3125 * different number of pixels to skip at the start though.
3126 */
3127 {
3128 unsigned int offset = PNG_PASS_START_COL(pass) * pixel_depth;
3129
3130 row_width -= offset;
3131 dp += offset;
3132 sp += offset;
John Bowlerac8375d2011-10-06 22:27:16 -05003133 }
3134
John Bowler4e68aa72011-10-11 16:01:33 -05003135 /* Work out the bytes to copy. */
3136 if (display)
3137 {
3138 /* When doing the 'block' algorithm the pixel in the pass gets
3139 * replicated to adjacent pixels. This is why the even (0,2,4,6)
3140 * passes are skipped above - the entire expanded row is copied.
3141 */
3142 bytes_to_copy = (1<<((6-pass)>>1)) * pixel_depth;
3143
3144 /* But don't allow this number to exceed the actual row width. */
3145 if (bytes_to_copy > row_width)
3146 bytes_to_copy = row_width;
3147 }
3148
3149 else /* normal row; Adam7 only ever gives us one pixel to copy. */
3150 bytes_to_copy = pixel_depth;
3151
3152 /* In Adam7 there is a constant offset between where the pixels go. */
3153 bytes_to_jump = PNG_PASS_COL_OFFSET(pass) * pixel_depth;
3154
3155 /* And simply copy these bytes. Some optimization is possible here,
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003156 * depending on the value of 'bytes_to_copy'. Special case the low
John Bowler4e68aa72011-10-11 16:01:33 -05003157 * byte counts, which we know to be frequent.
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003158 *
3159 * Notice that these cases all 'return' rather than 'break' - this
3160 * avoids an unnecessary test on whether to restore the last byte
3161 * below.
John Bowler4e68aa72011-10-11 16:01:33 -05003162 */
3163 switch (bytes_to_copy)
3164 {
3165 case 1:
3166 for (;;)
3167 {
3168 *dp = *sp;
3169
3170 if (row_width <= bytes_to_jump)
3171 return;
3172
3173 dp += bytes_to_jump;
3174 sp += bytes_to_jump;
3175 row_width -= bytes_to_jump;
3176 }
3177
3178 case 2:
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003179 /* There is a possibility of a partial copy at the end here; this
John Bowler4e68aa72011-10-11 16:01:33 -05003180 * slows the code down somewhat.
3181 */
3182 do
3183 {
3184 dp[0] = sp[0], dp[1] = sp[1];
3185
3186 if (row_width <= bytes_to_jump)
3187 return;
3188
3189 sp += bytes_to_jump;
3190 dp += bytes_to_jump;
3191 row_width -= bytes_to_jump;
3192 }
3193 while (row_width > 1);
3194
3195 /* And there can only be one byte left at this point: */
3196 *dp = *sp;
3197 return;
3198
3199 case 3:
3200 /* This can only be the RGB case, so each copy is exactly one
3201 * pixel and it is not necessary to check for a partial copy.
3202 */
3203 for(;;)
3204 {
3205 dp[0] = sp[0], dp[1] = sp[1], dp[2] = sp[2];
3206
3207 if (row_width <= bytes_to_jump)
3208 return;
3209
3210 sp += bytes_to_jump;
3211 dp += bytes_to_jump;
3212 row_width -= bytes_to_jump;
3213 }
3214
3215 default:
3216#if PNG_ALIGN_TYPE != PNG_ALIGN_NONE
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003217 /* Check for double byte alignment and, if possible, use a
3218 * 16-bit copy. Don't attempt this for narrow images - ones that
John Bowler4e68aa72011-10-11 16:01:33 -05003219 * are less than an interlace panel wide. Don't attempt it for
John Bowler7875d532011-11-07 22:33:49 -06003220 * wide bytes_to_copy either - use the png_memcpy there.
John Bowler4e68aa72011-10-11 16:01:33 -05003221 */
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003222 if (bytes_to_copy < 16 /*else use png_memcpy*/ &&
John Bowler4e68aa72011-10-11 16:01:33 -05003223 png_isaligned(dp, png_uint_16) &&
3224 png_isaligned(sp, png_uint_16) &&
3225 bytes_to_copy % sizeof (png_uint_16) == 0 &&
3226 bytes_to_jump % sizeof (png_uint_16) == 0)
3227 {
3228 /* Everything is aligned for png_uint_16 copies, but try for
3229 * png_uint_32 first.
3230 */
3231 if (png_isaligned(dp, png_uint_32) &&
3232 png_isaligned(sp, png_uint_32) &&
3233 bytes_to_copy % sizeof (png_uint_32) == 0 &&
3234 bytes_to_jump % sizeof (png_uint_32) == 0)
3235 {
3236 png_uint_32p dp32 = (png_uint_32p)dp;
3237 png_const_uint_32p sp32 = (png_const_uint_32p)sp;
3238 unsigned int skip = (bytes_to_jump-bytes_to_copy) /
3239 sizeof (png_uint_32);
3240
3241 do
3242 {
3243 size_t c = bytes_to_copy;
3244 do
3245 {
3246 *dp32++ = *sp32++;
3247 c -= sizeof (png_uint_32);
3248 }
3249 while (c > 0);
3250
3251 if (row_width <= bytes_to_jump)
3252 return;
3253
3254 dp32 += skip;
3255 sp32 += skip;
3256 row_width -= bytes_to_jump;
3257 }
3258 while (bytes_to_copy <= row_width);
3259
3260 /* Get to here when the row_width truncates the final copy.
3261 * There will be 1-3 bytes left to copy, so don't try the
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003262 * 16-bit loop below.
John Bowler4e68aa72011-10-11 16:01:33 -05003263 */
3264 dp = (png_bytep)dp32;
3265 sp = (png_const_bytep)sp32;
3266 do
3267 *dp++ = *sp++;
3268 while (--row_width > 0);
3269 return;
3270 }
3271
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003272 /* Else do it in 16-bit quantities, but only if the size is
John Bowler4e68aa72011-10-11 16:01:33 -05003273 * not too large.
3274 */
3275 else
3276 {
3277 png_uint_16p dp16 = (png_uint_16p)dp;
3278 png_const_uint_16p sp16 = (png_const_uint_16p)sp;
3279 unsigned int skip = (bytes_to_jump-bytes_to_copy) /
3280 sizeof (png_uint_16);
3281
3282 do
3283 {
3284 size_t c = bytes_to_copy;
3285 do
3286 {
3287 *dp16++ = *sp16++;
3288 c -= sizeof (png_uint_16);
3289 }
3290 while (c > 0);
3291
3292 if (row_width <= bytes_to_jump)
3293 return;
3294
3295 dp16 += skip;
3296 sp16 += skip;
3297 row_width -= bytes_to_jump;
3298 }
3299 while (bytes_to_copy <= row_width);
3300
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003301 /* End of row - 1 byte left, bytes_to_copy > row_width: */
John Bowler4e68aa72011-10-11 16:01:33 -05003302 dp = (png_bytep)dp16;
3303 sp = (png_const_bytep)sp16;
3304 do
3305 *dp++ = *sp++;
3306 while (--row_width > 0);
3307 return;
3308 }
3309 }
3310#endif /* PNG_ALIGN_ code */
3311
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003312 /* The true default - use a png_memcpy: */
John Bowler4e68aa72011-10-11 16:01:33 -05003313 for (;;)
3314 {
3315 png_memcpy(dp, sp, bytes_to_copy);
3316
3317 if (row_width <= bytes_to_jump)
3318 return;
3319
3320 sp += bytes_to_jump;
3321 dp += bytes_to_jump;
3322 row_width -= bytes_to_jump;
3323 if (bytes_to_copy > row_width)
3324 bytes_to_copy = row_width;
3325 }
3326 }
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003327
3328 /* NOT REACHED*/
John Bowler4e68aa72011-10-11 16:01:33 -05003329 } /* pixel_depth >= 8 */
3330
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003331 /* Here if pixel_depth < 8 to check 'end_ptr' below. */
Guy Schalnat0d580581995-07-20 02:43:20 -05003332 }
John Bowler4e68aa72011-10-11 16:01:33 -05003333 else
John Bowlerac8375d2011-10-06 22:27:16 -05003334#endif
3335
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003336 /* If here then the switch above wasn't used so just png_memcpy the whole row
John Bowler4e68aa72011-10-11 16:01:33 -05003337 * from the temporary row buffer (notice that this overwrites the end of the
3338 * destination row if it is a partial byte.)
John Bowlerac8375d2011-10-06 22:27:16 -05003339 */
3340 png_memcpy(dp, sp, PNG_ROWBYTES(pixel_depth, row_width));
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003341
3342 /* Restore the overwritten bits from the last byte if necessary. */
3343 if (end_ptr != NULL)
3344 *end_ptr = (png_byte)((end_byte & end_mask) | (*end_ptr & ~end_mask));
Guy Schalnat0d580581995-07-20 02:43:20 -05003345}
3346
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06003347#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05003348void /* PRIVATE */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003349png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
3350 png_uint_32 transformations /* Because these may affect the byte layout */)
Guy Schalnat0d580581995-07-20 02:43:20 -05003351{
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003352 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
3353 /* Offset to next interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003354 static PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003355
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05003356 png_debug(1, "in png_do_read_interlace");
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003357 if (row != NULL && row_info != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -05003358 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003359 png_uint_32 final_width;
3360
3361 final_width = row_info->width * png_pass_inc[pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05003362
3363 switch (row_info->pixel_depth)
3364 {
3365 case 1:
3366 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003367 png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3);
3368 png_bytep dp = row + (png_size_t)((final_width - 1) >> 3);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003369 int sshift, dshift;
3370 int s_start, s_end, s_inc;
3371 int jstop = png_pass_inc[pass];
3372 png_byte v;
Guy Schalnat0d580581995-07-20 02:43:20 -05003373 png_uint_32 i;
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003374 int j;
Guy Schalnat0d580581995-07-20 02:43:20 -05003375
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003376#ifdef PNG_READ_PACKSWAP_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003377 if (transformations & PNG_PACKSWAP)
3378 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003379 sshift = (int)((row_info->width + 7) & 0x07);
3380 dshift = (int)((final_width + 7) & 0x07);
3381 s_start = 7;
3382 s_end = 0;
3383 s_inc = -1;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003384 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003385
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003386 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05003387#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003388 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003389 sshift = 7 - (int)((row_info->width + 7) & 0x07);
3390 dshift = 7 - (int)((final_width + 7) & 0x07);
3391 s_start = 0;
3392 s_end = 7;
3393 s_inc = 1;
3394 }
Glenn Randers-Pehrson5dd2b8e2004-11-24 07:50:16 -06003395
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003396 for (i = 0; i < row_info->width; i++)
3397 {
3398 v = (png_byte)((*sp >> sshift) & 0x01);
3399 for (j = 0; j < jstop; j++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003400 {
John Bowler8fb6c6a2012-01-25 07:47:44 -06003401 unsigned int tmp = *dp & (0x7f7f >> (7 - dshift));
3402 tmp |= v << dshift;
3403 *dp = (png_byte)(tmp & 0xff);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003404
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003405 if (dshift == s_end)
3406 {
3407 dshift = s_start;
3408 dp--;
3409 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003410
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003411 else
3412 dshift += s_inc;
3413 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003414
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003415 if (sshift == s_end)
3416 {
3417 sshift = s_start;
Guy Schalnat0d580581995-07-20 02:43:20 -05003418 sp--;
3419 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003420
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003421 else
3422 sshift += s_inc;
Guy Schalnat0d580581995-07-20 02:43:20 -05003423 }
3424 break;
3425 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003426
Guy Schalnat0d580581995-07-20 02:43:20 -05003427 case 2:
3428 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003429 png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);
3430 png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);
3431 int sshift, dshift;
3432 int s_start, s_end, s_inc;
3433 int jstop = png_pass_inc[pass];
Andreas Dilger47a0c421997-05-16 02:46:07 -05003434 png_uint_32 i;
Guy Schalnat0d580581995-07-20 02:43:20 -05003435
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003436#ifdef PNG_READ_PACKSWAP_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003437 if (transformations & PNG_PACKSWAP)
3438 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003439 sshift = (int)(((row_info->width + 3) & 0x03) << 1);
3440 dshift = (int)(((final_width + 3) & 0x03) << 1);
3441 s_start = 6;
3442 s_end = 0;
3443 s_inc = -2;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003444 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003445
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003446 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05003447#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003448 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003449 sshift = (int)((3 - ((row_info->width + 3) & 0x03)) << 1);
3450 dshift = (int)((3 - ((final_width + 3) & 0x03)) << 1);
3451 s_start = 0;
3452 s_end = 6;
3453 s_inc = 2;
3454 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05003455
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003456 for (i = 0; i < row_info->width; i++)
3457 {
3458 png_byte v;
3459 int j;
3460
3461 v = (png_byte)((*sp >> sshift) & 0x03);
3462 for (j = 0; j < jstop; j++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003463 {
John Bowler8fb6c6a2012-01-25 07:47:44 -06003464 unsigned int tmp = *dp & (0x3f3f >> (6 - dshift));
3465 tmp |= v << dshift;
3466 *dp = (png_byte)(tmp & 0xff);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003467
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003468 if (dshift == s_end)
3469 {
3470 dshift = s_start;
3471 dp--;
3472 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003473
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003474 else
3475 dshift += s_inc;
3476 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003477
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003478 if (sshift == s_end)
3479 {
3480 sshift = s_start;
Guy Schalnat0d580581995-07-20 02:43:20 -05003481 sp--;
3482 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003483
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003484 else
3485 sshift += s_inc;
Guy Schalnat0d580581995-07-20 02:43:20 -05003486 }
3487 break;
3488 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003489
Guy Schalnat0d580581995-07-20 02:43:20 -05003490 case 4:
3491 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003492 png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1);
3493 png_bytep dp = row + (png_size_t)((final_width - 1) >> 1);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003494 int sshift, dshift;
3495 int s_start, s_end, s_inc;
Guy Schalnat0d580581995-07-20 02:43:20 -05003496 png_uint_32 i;
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003497 int jstop = png_pass_inc[pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05003498
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003499#ifdef PNG_READ_PACKSWAP_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003500 if (transformations & PNG_PACKSWAP)
3501 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003502 sshift = (int)(((row_info->width + 1) & 0x01) << 2);
3503 dshift = (int)(((final_width + 1) & 0x01) << 2);
3504 s_start = 4;
3505 s_end = 0;
3506 s_inc = -4;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003507 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003508
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003509 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05003510#endif
3511 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003512 sshift = (int)((1 - ((row_info->width + 1) & 0x01)) << 2);
3513 dshift = (int)((1 - ((final_width + 1) & 0x01)) << 2);
3514 s_start = 0;
3515 s_end = 4;
3516 s_inc = 4;
3517 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05003518
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003519 for (i = 0; i < row_info->width; i++)
3520 {
Glenn Randers-Pehrson8db19982011-10-27 16:17:24 -05003521 png_byte v = (png_byte)((*sp >> sshift) & 0x0f);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003522 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003523
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003524 for (j = 0; j < jstop; j++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003525 {
John Bowler8fb6c6a2012-01-25 07:47:44 -06003526 unsigned int tmp = *dp & (0xf0f >> (4 - dshift));
3527 tmp |= v << dshift;
3528 *dp = (png_byte)(tmp & 0xff);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003529
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003530 if (dshift == s_end)
3531 {
3532 dshift = s_start;
3533 dp--;
3534 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003535
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003536 else
3537 dshift += s_inc;
3538 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003539
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003540 if (sshift == s_end)
3541 {
3542 sshift = s_start;
Guy Schalnat0d580581995-07-20 02:43:20 -05003543 sp--;
3544 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003545
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003546 else
3547 sshift += s_inc;
Guy Schalnat0d580581995-07-20 02:43:20 -05003548 }
3549 break;
3550 }
John Bowlerac8375d2011-10-06 22:27:16 -05003551
Guy Schalnat0d580581995-07-20 02:43:20 -05003552 default:
3553 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003554 png_size_t pixel_bytes = (row_info->pixel_depth >> 3);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003555
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06003556 png_bytep sp = row + (png_size_t)(row_info->width - 1)
3557 * pixel_bytes;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003558
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003559 png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05003560
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003561 int jstop = png_pass_inc[pass];
3562 png_uint_32 i;
3563
3564 for (i = 0; i < row_info->width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003565 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003566 png_byte v[8];
3567 int j;
3568
3569 png_memcpy(v, sp, pixel_bytes);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003570
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003571 for (j = 0; j < jstop; j++)
3572 {
3573 png_memcpy(dp, v, pixel_bytes);
3574 dp -= pixel_bytes;
3575 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003576
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003577 sp -= pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05003578 }
3579 break;
3580 }
3581 }
John Bowlerac8375d2011-10-06 22:27:16 -05003582
Guy Schalnat0d580581995-07-20 02:43:20 -05003583 row_info->width = final_width;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003584 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width);
Guy Schalnat0d580581995-07-20 02:43:20 -05003585 }
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -05003586#ifndef PNG_READ_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003587 PNG_UNUSED(transformations) /* Silence compiler warning */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05003588#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003589}
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06003590#endif /* PNG_READ_INTERLACING_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05003591
Mans Rullgardd3a94802011-11-03 00:47:55 -05003592static void
3593png_read_filter_row_sub(png_row_infop row_info, png_bytep row,
3594 png_const_bytep prev_row)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003595{
Mans Rullgardd3a94802011-11-03 00:47:55 -05003596 png_size_t i;
3597 png_size_t istop = row_info->rowbytes;
3598 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
3599 png_bytep rp = row + bpp;
Mans Rullgardd3a94802011-11-03 00:47:55 -05003600
3601 PNG_UNUSED(prev_row)
3602
3603 for (i = bpp; i < istop; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003604 {
John Bowler7875d532011-11-07 22:33:49 -06003605 *rp = (png_byte)(((int)(*rp) + (int)(*(rp-bpp))) & 0xff);
Mans Rullgardd3a94802011-11-03 00:47:55 -05003606 rp++;
3607 }
3608}
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003609
Mans Rullgardd3a94802011-11-03 00:47:55 -05003610static void
3611png_read_filter_row_up(png_row_infop row_info, png_bytep row,
3612 png_const_bytep prev_row)
3613{
3614 png_size_t i;
3615 png_size_t istop = row_info->rowbytes;
3616 png_bytep rp = row;
3617 png_const_bytep pp = prev_row;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003618
Mans Rullgardd3a94802011-11-03 00:47:55 -05003619 for (i = 0; i < istop; i++)
3620 {
3621 *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
3622 rp++;
3623 }
3624}
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003625
Mans Rullgardd3a94802011-11-03 00:47:55 -05003626static void
3627png_read_filter_row_avg(png_row_infop row_info, png_bytep row,
3628 png_const_bytep prev_row)
3629{
3630 png_size_t i;
3631 png_bytep rp = row;
3632 png_const_bytep pp = prev_row;
Mans Rullgardd3a94802011-11-03 00:47:55 -05003633 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
3634 png_size_t istop = row_info->rowbytes - bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003635
Mans Rullgardd3a94802011-11-03 00:47:55 -05003636 for (i = 0; i < bpp; i++)
3637 {
3638 *rp = (png_byte)(((int)(*rp) +
3639 ((int)(*pp++) / 2 )) & 0xff);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003640
Mans Rullgardd3a94802011-11-03 00:47:55 -05003641 rp++;
3642 }
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003643
Mans Rullgardd3a94802011-11-03 00:47:55 -05003644 for (i = 0; i < istop; i++)
3645 {
3646 *rp = (png_byte)(((int)(*rp) +
John Bowler7875d532011-11-07 22:33:49 -06003647 (int)(*pp++ + *(rp-bpp)) / 2 ) & 0xff);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003648
Mans Rullgardd3a94802011-11-03 00:47:55 -05003649 rp++;
3650 }
3651}
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003652
Mans Rullgardd3a94802011-11-03 00:47:55 -05003653static void
John Bowlerfcc02632011-11-03 18:31:00 -05003654png_read_filter_row_paeth_1byte_pixel(png_row_infop row_info, png_bytep row,
Mans Rullgardd3a94802011-11-03 00:47:55 -05003655 png_const_bytep prev_row)
3656{
John Bowlerfcc02632011-11-03 18:31:00 -05003657 png_bytep rp_end = row + row_info->rowbytes;
3658 int a, c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003659
John Bowlerfcc02632011-11-03 18:31:00 -05003660 /* First pixel/byte */
3661 c = *prev_row++;
3662 a = *row + c;
3663 *row++ = (png_byte)a;
3664
3665 /* Remainder */
3666 while (row < rp_end)
Mans Rullgardd3a94802011-11-03 00:47:55 -05003667 {
John Bowlerfcc02632011-11-03 18:31:00 -05003668 int b, pa, pb, pc, p;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003669
John Bowlerfcc02632011-11-03 18:31:00 -05003670 a &= 0xff; /* From previous iteration or start */
3671 b = *prev_row++;
Mans Rullgardd3a94802011-11-03 00:47:55 -05003672
3673 p = b - c;
3674 pc = a - c;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003675
John Bowlerfcc02632011-11-03 18:31:00 -05003676# ifdef PNG_USE_ABS
3677 pa = abs(p);
3678 pb = abs(pc);
3679 pc = abs(p + pc);
3680# else
3681 pa = p < 0 ? -p : p;
3682 pb = pc < 0 ? -pc : pc;
3683 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
3684# endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003685
John Bowlerfcc02632011-11-03 18:31:00 -05003686 /* Find the best predictor, the least of pa, pb, pc favoring the earlier
3687 * ones in the case of a tie.
3688 */
3689 if (pb < pa) pa = pb, a = b;
3690 if (pc < pa) a = c;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003691
John Bowlerfcc02632011-11-03 18:31:00 -05003692 /* Calculate the current pixel in a, and move the previous row pixel to c
3693 * for the next time round the loop
3694 */
3695 c = b;
3696 a += *row;
3697 *row++ = (png_byte)a;
3698 }
3699}
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003700
John Bowlerfcc02632011-11-03 18:31:00 -05003701static void
3702png_read_filter_row_paeth_multibyte_pixel(png_row_infop row_info, png_bytep row,
3703 png_const_bytep prev_row)
3704{
3705 int bpp = (row_info->pixel_depth + 7) >> 3;
3706 png_bytep rp_end = row + bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003707
John Bowlerfcc02632011-11-03 18:31:00 -05003708 /* Process the first pixel in the row completely (this is the same as 'up'
3709 * because there is only one candidate predictor for the first row).
3710 */
3711 while (row < rp_end)
3712 {
3713 int a = *row + *prev_row++;
3714 *row++ = (png_byte)a;
3715 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003716
John Bowlerfcc02632011-11-03 18:31:00 -05003717 /* Remainder */
3718 rp_end += row_info->rowbytes - bpp;
3719
3720 while (row < rp_end)
3721 {
3722 int a, b, c, pa, pb, pc, p;
3723
3724 c = *(prev_row - bpp);
3725 a = *(row - bpp);
3726 b = *prev_row++;
3727
3728 p = b - c;
3729 pc = a - c;
3730
3731# ifdef PNG_USE_ABS
3732 pa = abs(p);
3733 pb = abs(pc);
3734 pc = abs(p + pc);
3735# else
3736 pa = p < 0 ? -p : p;
3737 pb = pc < 0 ? -pc : pc;
3738 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
3739# endif
3740
3741 if (pb < pa) pa = pb, a = b;
3742 if (pc < pa) a = c;
3743
3744 c = b;
3745 a += *row;
3746 *row++ = (png_byte)a;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003747 }
3748}
Guy Schalnat0d580581995-07-20 02:43:20 -05003749
Mans Rullgardd3a94802011-11-03 00:47:55 -05003750#ifdef PNG_ARM_NEON
Mans Rullgarde7acc6a2011-11-16 13:48:18 -06003751
3752#ifdef __linux__
3753#include <stdio.h>
3754#include <elf.h>
3755#include <asm/hwcap.h>
3756
3757static int png_have_hwcap(unsigned cap)
3758{
3759 FILE *f = fopen("/proc/self/auxv", "r");
3760 Elf32_auxv_t aux;
3761 int have_cap = 0;
3762
3763 if (!f)
3764 return 0;
3765
3766 while (fread(&aux, sizeof(aux), 1, f) > 0)
3767 {
3768 if (aux.a_type == AT_HWCAP &&
3769 aux.a_un.a_val & cap)
3770 {
3771 have_cap = 1;
3772 break;
3773 }
3774 }
3775
3776 fclose(f);
3777
3778 return have_cap;
3779}
3780#endif /* __linux__ */
3781
Mans Rullgardd3a94802011-11-03 00:47:55 -05003782static void
John Bowler5d567862011-12-24 09:12:00 -06003783png_init_filter_functions_neon(png_structrp pp, unsigned int bpp)
Mans Rullgardd3a94802011-11-03 00:47:55 -05003784{
Mans Rullgarde7acc6a2011-11-16 13:48:18 -06003785#ifdef __linux__
3786 if (!png_have_hwcap(HWCAP_NEON))
3787 return;
3788#endif
3789
Mans Rullgardd3a94802011-11-03 00:47:55 -05003790 pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up_neon;
3791
Mans Rullgarde7acc6a2011-11-16 13:48:18 -06003792 if (bpp == 3)
3793 {
Mans Rullgardd3a94802011-11-03 00:47:55 -05003794 pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub3_neon;
3795 pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg3_neon;
John Bowler6f237b62012-03-02 13:13:15 -06003796 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
Mans Rullgarde7acc6a2011-11-16 13:48:18 -06003797 png_read_filter_row_paeth3_neon;
3798 }
3799
3800 else if (bpp == 4)
3801 {
Mans Rullgardd3a94802011-11-03 00:47:55 -05003802 pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub4_neon;
3803 pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg4_neon;
Mans Rullgarde7acc6a2011-11-16 13:48:18 -06003804 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
3805 png_read_filter_row_paeth4_neon;
Mans Rullgardd3a94802011-11-03 00:47:55 -05003806 }
3807}
Mans Rullgarde7acc6a2011-11-16 13:48:18 -06003808#endif /* PNG_ARM_NEON */
Mans Rullgardd3a94802011-11-03 00:47:55 -05003809
3810static void
John Bowler5d567862011-12-24 09:12:00 -06003811png_init_filter_functions(png_structrp pp)
Mans Rullgardd3a94802011-11-03 00:47:55 -05003812{
John Bowlerfcc02632011-11-03 18:31:00 -05003813 unsigned int bpp = (pp->pixel_depth + 7) >> 3;
3814
Mans Rullgardd3a94802011-11-03 00:47:55 -05003815 pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub;
3816 pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up;
3817 pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg;
John Bowlerfcc02632011-11-03 18:31:00 -05003818 if (bpp == 1)
3819 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
3820 png_read_filter_row_paeth_1byte_pixel;
3821 else
3822 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
3823 png_read_filter_row_paeth_multibyte_pixel;
Mans Rullgardd3a94802011-11-03 00:47:55 -05003824
3825#ifdef PNG_ARM_NEON
John Bowlerfcc02632011-11-03 18:31:00 -05003826 png_init_filter_functions_neon(pp, bpp);
Mans Rullgardd3a94802011-11-03 00:47:55 -05003827#endif
3828}
3829
3830void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06003831png_read_filter_row(png_structrp pp, png_row_infop row_info, png_bytep row,
Mans Rullgardd3a94802011-11-03 00:47:55 -05003832 png_const_bytep prev_row, int filter)
3833{
3834 if (pp->read_filter[0] == NULL)
3835 png_init_filter_functions(pp);
3836 if (filter > PNG_FILTER_VALUE_NONE && filter < PNG_FILTER_VALUE_LAST)
3837 pp->read_filter[filter-1](row_info, row, prev_row);
3838}
3839
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05003840#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05003841void /* PRIVATE */
John Bowlerb5d00512012-03-09 09:15:18 -06003842png_read_IDAT_data(png_structrp png_ptr, png_bytep output,
3843 png_alloc_size_t avail_out)
3844{
3845 /* Loop reading IDATs and decompressing the result into output[avail_out] */
3846 png_ptr->zstream.next_out = output;
3847 png_ptr->zstream.avail_out = 0; /* safety: set below */
3848
3849 if (output == NULL)
3850 avail_out = 0;
3851
3852 do
3853 {
3854 int ret;
3855 png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];
3856
3857 if (png_ptr->zstream.avail_in == 0)
3858 {
3859 uInt avail_in;
3860 png_bytep buffer;
3861
3862 while (png_ptr->idat_size == 0)
3863 {
3864 png_crc_finish(png_ptr, 0);
3865
3866 png_ptr->idat_size = png_read_chunk_header(png_ptr);
3867 /* This is an error even in the 'check' case because the code just
3868 * consumed a non-IDAT header.
3869 */
3870 if (png_ptr->chunk_name != png_IDAT)
3871 png_error(png_ptr, "Not enough image data");
3872 }
3873
3874 avail_in = png_ptr->IDAT_read_size;
3875
3876 if (avail_in > png_ptr->idat_size)
3877 avail_in = (uInt)png_ptr->idat_size;
3878
3879 /* A PNG with a gradually increasing IDAT size will defeat this attempt
3880 * to minimize memory usage by causing lots of re-allocs, but
3881 * realistically doing IDAT_read_size re-allocs is not likely to be a
3882 * big problem.
3883 */
3884 buffer = png_read_buffer(png_ptr, avail_in, 0/*error*/);
3885
3886 png_crc_read(png_ptr, buffer, avail_in);
3887 png_ptr->idat_size -= avail_in;
3888
3889 png_ptr->zstream.next_in = buffer;
3890 png_ptr->zstream.avail_in = avail_in;
3891 }
3892
3893 /* And set up the output side. */
3894 if (output != NULL) /* standard read */
3895 {
3896 uInt out = ZLIB_IO_MAX;
3897
3898 if (out > avail_out)
3899 out = (uInt)avail_out;
3900
3901 avail_out -= out;
3902 png_ptr->zstream.avail_out = out;
3903 }
3904
3905 else /* check for end */
3906 {
3907 png_ptr->zstream.next_out = tmpbuf;
3908 png_ptr->zstream.avail_out = sizeof tmpbuf;
3909 }
3910
3911 /* Use NO_FLUSH; this gives zlib the maximum opportunity to optimize the
John Bowler90669192012-03-09 22:03:13 -06003912 * process. If the LZ stream is truncated the sequential reader will
3913 * terminally damage the stream, above, by reading the chunk header of the
3914 * following chunk (it then exits with png_error).
3915 *
3916 * TODO: deal more elegantly with truncated IDAT lists.
John Bowlerb5d00512012-03-09 09:15:18 -06003917 */
3918 ret = inflate(&png_ptr->zstream, Z_NO_FLUSH);
3919
3920 /* Take the unconsumed output back (so, in the 'check' case this just
3921 * counts up).
3922 */
3923 avail_out += png_ptr->zstream.avail_out;
3924 png_ptr->zstream.avail_out = 0;
3925
3926 if (ret == Z_STREAM_END)
3927 {
3928 /* Do this for safety; we won't read any more into this row. */
3929 png_ptr->zstream.next_out = NULL;
3930
3931 png_ptr->mode |= PNG_AFTER_IDAT;
3932 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
3933
3934 if (png_ptr->zstream.avail_in > 0 || png_ptr->idat_size > 0)
3935 png_chunk_benign_error(png_ptr, "Extra compressed data");
3936 break;
3937 }
3938
3939 if (ret != Z_OK)
3940 {
3941 png_zstream_error(png_ptr, ret);
3942
3943 if (output != NULL)
3944 png_chunk_error(png_ptr, png_ptr->zstream.msg);
3945
3946 else /* checking */
3947 {
3948 png_chunk_benign_error(png_ptr, png_ptr->zstream.msg);
3949 return;
3950 }
3951 }
3952 } while (avail_out > 0);
3953
3954 if (avail_out > 0)
3955 {
3956 /* The stream ended before the image; this is the same as too few IDATs so
3957 * should be handled the same way.
3958 */
3959 if (output != NULL)
3960 png_error(png_ptr, "Not enough image data");
3961
3962 else /* checking */
3963 png_chunk_benign_error(png_ptr, "Too much image data");
3964 }
3965}
3966
3967void /* PRIVATE */
3968png_read_finish_IDAT(png_structrp png_ptr)
3969{
3970 /* We don't need any more data and the stream should have ended, however the
3971 * LZ end code may actually not have been processed. In this case we must
3972 * read it otherwise stray unread IDAT data or, more likely, an IDAT chunk
3973 * may still remain to be consumed.
3974 */
3975 if (!(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED))
3976 {
3977 /* The NULL causes png_read_IDAT_data to swallow any remaining bytes in
3978 * the compressed stream, but the stream may be damaged too, so even after
3979 * this call we may need to terminate the zstream ownership.
3980 */
3981 png_read_IDAT_data(png_ptr, NULL, 0);
3982 png_ptr->zstream.next_out = NULL; /* safety */
3983
3984 /* Now clear everything out for safety; the following may not have been
3985 * done.
3986 */
3987 if (!(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED))
3988 {
3989 png_ptr->mode |= PNG_AFTER_IDAT;
3990 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
3991 }
3992 }
3993
3994 /* If the zstream has not been released do it now *and* terminate the reading
3995 * of the final IDAT chunk.
3996 */
3997 if (png_ptr->zowner == png_IDAT)
3998 {
3999 /* Always do this; the pointers otherwise point into the read buffer. */
4000 png_ptr->zstream.next_in = NULL;
4001 png_ptr->zstream.avail_in = 0;
4002
4003 /* Now we no longer own the zstream. */
4004 png_ptr->zowner = 0;
4005
4006 /* The slightly weird semantics of the sequential IDAT reading is that we
4007 * are always in or at the end of an IDAT chunk, so we always need to do a
4008 * crc_finish here. If idat_size is non-zero we also need to read the
4009 * spurious bytes at the end of the chunk now.
4010 */
4011 (void)png_crc_finish(png_ptr, png_ptr->idat_size);
4012 }
4013}
4014
4015void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06004016png_read_finish_row(png_structrp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05004017{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004018#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004019 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004020
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004021 /* Start of interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004022 static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004023
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004024 /* Offset to next interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004025 static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004026
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004027 /* Start of interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004028 static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004029
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004030 /* Offset to next interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004031 static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004032#endif /* PNG_READ_INTERLACING_SUPPORTED */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004033
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05004034 png_debug(1, "in png_read_finish_row");
Guy Schalnat0d580581995-07-20 02:43:20 -05004035 png_ptr->row_number++;
4036 if (png_ptr->row_number < png_ptr->num_rows)
4037 return;
4038
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004039#ifdef PNG_READ_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05004040 if (png_ptr->interlaced)
4041 {
4042 png_ptr->row_number = 0;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004043
Glenn Randers-Pehrson435cf872011-10-05 16:23:53 -05004044 /* TO DO: don't do this if prev_row isn't needed (requires
4045 * read-ahead of the next row's filter byte.
4046 */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004047 png_memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
4048
Guy Schalnat0d580581995-07-20 02:43:20 -05004049 do
4050 {
4051 png_ptr->pass++;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004052
Guy Schalnat0d580581995-07-20 02:43:20 -05004053 if (png_ptr->pass >= 7)
4054 break;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004055
Guy Schalnat0d580581995-07-20 02:43:20 -05004056 png_ptr->iwidth = (png_ptr->width +
4057 png_pass_inc[png_ptr->pass] - 1 -
4058 png_pass_start[png_ptr->pass]) /
4059 png_pass_inc[png_ptr->pass];
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05004060
Guy Schalnat0d580581995-07-20 02:43:20 -05004061 if (!(png_ptr->transformations & PNG_INTERLACE))
4062 {
4063 png_ptr->num_rows = (png_ptr->height +
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004064 png_pass_yinc[png_ptr->pass] - 1 -
4065 png_pass_ystart[png_ptr->pass]) /
4066 png_pass_yinc[png_ptr->pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05004067 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004068
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -05004069 else /* if (png_ptr->transformations & PNG_INTERLACE) */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004070 break; /* libpng deinterlacing sees every row */
4071
4072 } while (png_ptr->num_rows == 0 || png_ptr->iwidth == 0);
Guy Schalnat0d580581995-07-20 02:43:20 -05004073
4074 if (png_ptr->pass < 7)
4075 return;
4076 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004077#endif /* PNG_READ_INTERLACING_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05004078
John Bowler42a2b552012-03-05 20:57:40 -06004079 /* Here after at the end of the last row of the last pass. */
John Bowlerb5d00512012-03-09 09:15:18 -06004080 png_read_finish_IDAT(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05004081}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05004082#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05004083
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05004084void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06004085png_read_start_row(png_structrp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05004086{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004087#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004088 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004089
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004090 /* Start of interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004091 static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004092
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004093 /* Offset to next interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004094 static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004095
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004096 /* Start of interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004097 static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004098
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004099 /* Offset to next interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004100 static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004101#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004102
Guy Schalnat0d580581995-07-20 02:43:20 -05004103 int max_pixel_depth;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05004104 png_size_t row_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05004105
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05004106 png_debug(1, "in png_read_start_row");
John Bowler42a2b552012-03-05 20:57:40 -06004107
John Bowler4a12f4a2011-04-17 18:34:22 -05004108#ifdef PNG_READ_TRANSFORMS_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05004109 png_init_read_transformations(png_ptr);
John Bowler4a12f4a2011-04-17 18:34:22 -05004110#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004111#ifdef PNG_READ_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05004112 if (png_ptr->interlaced)
4113 {
4114 if (!(png_ptr->transformations & PNG_INTERLACE))
4115 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004116 png_pass_ystart[0]) / png_pass_yinc[0];
4117
Guy Schalnat0d580581995-07-20 02:43:20 -05004118 else
4119 png_ptr->num_rows = png_ptr->height;
4120
4121 png_ptr->iwidth = (png_ptr->width +
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004122 png_pass_inc[png_ptr->pass] - 1 -
4123 png_pass_start[png_ptr->pass]) /
4124 png_pass_inc[png_ptr->pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05004125 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004126
Guy Schalnat0d580581995-07-20 02:43:20 -05004127 else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004128#endif /* PNG_READ_INTERLACING_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05004129 {
4130 png_ptr->num_rows = png_ptr->height;
4131 png_ptr->iwidth = png_ptr->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05004132 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004133
Guy Schalnat0d580581995-07-20 02:43:20 -05004134 max_pixel_depth = png_ptr->pixel_depth;
4135
John Bowlere6fb6912011-11-08 21:35:16 -06004136 /* WARNING: * png_read_transform_info (pngrtran.c) performs a simpliar set of
4137 * calculations to calculate the final pixel depth, then
4138 * png_do_read_transforms actually does the transforms. This means that the
4139 * code which effectively calculates this value is actually repeated in three
4140 * separate places. They must all match. Innocent changes to the order of
4141 * transformations can and will break libpng in a way that causes memory
4142 * overwrites.
4143 *
4144 * TODO: fix this.
4145 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004146#ifdef PNG_READ_PACK_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05004147 if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8)
Guy Schalnat0d580581995-07-20 02:43:20 -05004148 max_pixel_depth = 8;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004149#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05004150
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004151#ifdef PNG_READ_EXPAND_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -05004152 if (png_ptr->transformations & PNG_EXPAND)
Guy Schalnat0d580581995-07-20 02:43:20 -05004153 {
4154 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
4155 {
4156 if (png_ptr->num_trans)
4157 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004158
Guy Schalnat0d580581995-07-20 02:43:20 -05004159 else
4160 max_pixel_depth = 24;
4161 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004162
Guy Schalnat0d580581995-07-20 02:43:20 -05004163 else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
4164 {
4165 if (max_pixel_depth < 8)
4166 max_pixel_depth = 8;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004167
Guy Schalnat0d580581995-07-20 02:43:20 -05004168 if (png_ptr->num_trans)
4169 max_pixel_depth *= 2;
4170 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004171
Guy Schalnat0d580581995-07-20 02:43:20 -05004172 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
4173 {
4174 if (png_ptr->num_trans)
4175 {
4176 max_pixel_depth *= 4;
4177 max_pixel_depth /= 3;
4178 }
4179 }
4180 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004181#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05004182
John Bowler4d562962011-02-12 09:01:20 -06004183#ifdef PNG_READ_EXPAND_16_SUPPORTED
4184 if (png_ptr->transformations & PNG_EXPAND_16)
4185 {
4186# ifdef PNG_READ_EXPAND_SUPPORTED
4187 /* In fact it is an error if it isn't supported, but checking is
4188 * the safe way.
4189 */
4190 if (png_ptr->transformations & PNG_EXPAND)
4191 {
4192 if (png_ptr->bit_depth < 16)
4193 max_pixel_depth *= 2;
4194 }
4195 else
4196# endif
4197 png_ptr->transformations &= ~PNG_EXPAND_16;
4198 }
4199#endif
4200
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004201#ifdef PNG_READ_FILLER_SUPPORTED
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004202 if (png_ptr->transformations & (PNG_FILLER))
Guy Schalnat0d580581995-07-20 02:43:20 -05004203 {
John Bowlere6fb6912011-11-08 21:35:16 -06004204 if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004205 {
4206 if (max_pixel_depth <= 8)
4207 max_pixel_depth = 16;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004208
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004209 else
4210 max_pixel_depth = 32;
4211 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004212
John Bowlere6fb6912011-11-08 21:35:16 -06004213 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB ||
4214 png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004215 {
4216 if (max_pixel_depth <= 32)
4217 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004218
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004219 else
4220 max_pixel_depth = 64;
4221 }
Guy Schalnat0d580581995-07-20 02:43:20 -05004222 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004223#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05004224
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004225#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05004226 if (png_ptr->transformations & PNG_GRAY_TO_RGB)
4227 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06004228 if (
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004229#ifdef PNG_READ_EXPAND_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004230 (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) ||
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06004231#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004232#ifdef PNG_READ_FILLER_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004233 (png_ptr->transformations & (PNG_FILLER)) ||
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06004234#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004235 png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
Guy Schalnat0d580581995-07-20 02:43:20 -05004236 {
4237 if (max_pixel_depth <= 16)
4238 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004239
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06004240 else
Guy Schalnat0d580581995-07-20 02:43:20 -05004241 max_pixel_depth = 64;
4242 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004243
Guy Schalnat0d580581995-07-20 02:43:20 -05004244 else
4245 {
4246 if (max_pixel_depth <= 8)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004247 {
4248 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06004249 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004250
4251 else
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06004252 max_pixel_depth = 24;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004253 }
4254
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06004255 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4256 max_pixel_depth = 64;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004257
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06004258 else
Guy Schalnat0d580581995-07-20 02:43:20 -05004259 max_pixel_depth = 48;
4260 }
4261 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004262#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05004263
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05004264#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
4265defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004266 if (png_ptr->transformations & PNG_USER_TRANSFORM)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004267 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004268 int user_pixel_depth = png_ptr->user_transform_depth *
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05004269 png_ptr->user_transform_channels;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004270
4271 if (user_pixel_depth > max_pixel_depth)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004272 max_pixel_depth = user_pixel_depth;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004273 }
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05004274#endif
4275
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004276 /* This value is stored in png_struct and double checked in the row read
4277 * code.
4278 */
4279 png_ptr->maximum_pixel_depth = (png_byte)max_pixel_depth;
4280 png_ptr->transformed_pixel_depth = 0; /* calculated on demand */
4281
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004282 /* Align the width on the next larger 8 pixels. Mainly used
4283 * for interlacing
4284 */
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05004285 row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004286 /* Calculate the maximum bytes needed, adding a byte and a pixel
4287 * for safety's sake
4288 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004289 row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) +
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004290 1 + ((max_pixel_depth + 7) >> 3);
4291
Guy Schalnat0d580581995-07-20 02:43:20 -05004292#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05004293 if (row_bytes > (png_uint_32)65536L)
Guy Schalnate5a37791996-06-05 15:50:50 -05004294 png_error(png_ptr, "This image requires a row greater than 64KB");
Guy Schalnat0d580581995-07-20 02:43:20 -05004295#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004296
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06004297 if (row_bytes + 48 > png_ptr->old_big_row_buf_size)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004298 {
4299 png_free(png_ptr, png_ptr->big_row_buf);
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004300 png_free(png_ptr, png_ptr->big_prev_row);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004301
Glenn Randers-Pehrson6917b512009-03-09 15:31:08 -05004302 if (png_ptr->interlaced)
Glenn Randers-Pehrsona515d302010-01-01 10:24:25 -06004303 png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr,
4304 row_bytes + 48);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004305
Glenn Randers-Pehrsona515d302010-01-01 10:24:25 -06004306 else
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004307 png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004308
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004309 png_ptr->big_prev_row = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06004310
4311#ifdef PNG_ALIGNED_MEMORY_SUPPORTED
4312 /* Use 16-byte aligned memory for row_buf with at least 16 bytes
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004313 * of padding before and after row_buf; treat prev_row similarly.
John Bowlerac8375d2011-10-06 22:27:16 -05004314 * NOTE: the alignment is to the start of the pixels, one beyond the start
4315 * of the buffer, because of the filter byte. Prior to libpng 1.5.6 this
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004316 * was incorrect; the filter byte was aligned, which had the exact
4317 * opposite effect of that intended.
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06004318 */
John Bowlerac8375d2011-10-06 22:27:16 -05004319 {
4320 png_bytep temp = png_ptr->big_row_buf + 32;
Glenn Randers-Pehrson8db19982011-10-27 16:17:24 -05004321 int extra = (int)((temp - (png_bytep)0) & 0x0f);
John Bowlerac8375d2011-10-06 22:27:16 -05004322 png_ptr->row_buf = temp - extra - 1/*filter byte*/;
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004323
4324 temp = png_ptr->big_prev_row + 32;
Glenn Randers-Pehrson8db19982011-10-27 16:17:24 -05004325 extra = (int)((temp - (png_bytep)0) & 0x0f);
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004326 png_ptr->prev_row = temp - extra - 1/*filter byte*/;
John Bowlerac8375d2011-10-06 22:27:16 -05004327 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004328
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06004329#else
John Bowlerac8375d2011-10-06 22:27:16 -05004330 /* Use 31 bytes of padding before and 17 bytes after row_buf. */
4331 png_ptr->row_buf = png_ptr->big_row_buf + 31;
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004332 png_ptr->prev_row = png_ptr->big_prev_row + 31;
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06004333#endif
4334 png_ptr->old_big_row_buf_size = row_bytes + 48;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004335 }
Guy Schalnat0d580581995-07-20 02:43:20 -05004336
4337#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004338 if (png_ptr->rowbytes > 65535)
Guy Schalnate5a37791996-06-05 15:50:50 -05004339 png_error(png_ptr, "This image requires a row greater than 64KB");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004340
Guy Schalnat0d580581995-07-20 02:43:20 -05004341#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004342 if (png_ptr->rowbytes > (PNG_SIZE_MAX - 1))
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05004343 png_error(png_ptr, "Row has too many bytes to allocate in memory");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004344
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05004345 png_memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05004346
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004347 png_debug1(3, "width = %u,", png_ptr->width);
4348 png_debug1(3, "height = %u,", png_ptr->height);
4349 png_debug1(3, "iwidth = %u,", png_ptr->iwidth);
4350 png_debug1(3, "num_rows = %u,", png_ptr->num_rows);
Glenn Randers-Pehrsonb764c602011-01-14 21:18:37 -06004351 png_debug1(3, "rowbytes = %lu,", (unsigned long)png_ptr->rowbytes);
4352 png_debug1(3, "irowbytes = %lu",
4353 (unsigned long)PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05004354
John Bowlerb5d00512012-03-09 09:15:18 -06004355 /* The sequential reader needs a buffer for IDAT, but the progressive reader
4356 * does not, so free the read buffer now regardless; the sequential reader
4357 * reallocates it on demand.
4358 */
4359 if (png_ptr->read_buffer)
4360 {
4361 png_bytep buffer = png_ptr->read_buffer;
4362
4363 png_ptr->read_buffer_size = 0;
4364 png_ptr->read_buffer = NULL;
4365 png_free(png_ptr, buffer);
4366 }
4367
John Bowler90669192012-03-09 22:03:13 -06004368 /* Finally claim the zstream for the inflate of the IDAT data, use the bits
4369 * value from the stream (note that this will result in a fatal error if the
4370 * IDAT stream has a bogus deflate header window_bits value, but this should
4371 * not be happening any longer!)
4372 */
4373 if (png_inflate_claim(png_ptr, png_IDAT, 0) != Z_OK)
John Bowlerb5d00512012-03-09 09:15:18 -06004374 png_error(png_ptr, png_ptr->zstream.msg);
John Bowler42a2b552012-03-05 20:57:40 -06004375
Guy Schalnate5a37791996-06-05 15:50:50 -05004376 png_ptr->flags |= PNG_FLAG_ROW_INIT;
Guy Schalnat0d580581995-07-20 02:43:20 -05004377}
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06004378#endif /* PNG_READ_SUPPORTED */