blob: e66075786e1138273640bf87e0ae90754bdf43e7 [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-Pehrson5b9a28f2012-11-25 12:46:24 -0600199 * are reading an ancillary or critical chunk, and how the program has set
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500200 * 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
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -0500214 len = (sizeof tmpbuf);
John Bowlerb5d00512012-03-09 09:15:18 -0600215 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
Glenn Randers-Pehrson5b9a28f2012-11-25 12:46:24 -0600285 * buffer; if an error occurs and 'warn' is set the routine returns NULL, else
John Bowlerb5d00512012-03-09 09:15:18 -0600286 * 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 {
Glenn Randers-Pehrson4430b912012-08-09 22:24:04 -0500315#ifdef PNG_WARNINGS_SUPPORTED
316 if (warn)
317 png_chunk_warning(png_ptr, "insufficient memory to read chunk");
318 else
319#endif
320 {
321#ifdef PNG_ERROR_TEXT_SUPPORTED
322 png_chunk_error(png_ptr, "insufficient memory to read chunk");
323#endif
324 }
John Bowlerb5d00512012-03-09 09:15:18 -0600325 }
326 }
327
328 return buffer;
329}
330
331/* png_inflate_claim: claim the zstream for some nefarious purpose that involves
332 * decompression. Returns Z_OK on success, else a zlib error code. It checks
333 * the owner but, in final release builds, just issues a warning if some other
334 * chunk apparently owns the stream. Prior to release it does a png_error.
335 */
336static int
John Bowler90669192012-03-09 22:03:13 -0600337png_inflate_claim(png_structrp png_ptr, png_uint_32 owner, int window_bits)
John Bowlerb5d00512012-03-09 09:15:18 -0600338{
339 if (png_ptr->zowner != 0)
340 {
341 char msg[64];
342
343 PNG_STRING_FROM_CHUNK(msg, png_ptr->zowner);
344 /* So the message that results is "<chunk> using zstream"; this is an
345 * internal error, but is very useful for debugging. i18n requirements
346 * are minimal.
347 */
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -0500348 (void)png_safecat(msg, (sizeof msg), 4, " using zstream");
John Bowler921648a2012-03-28 23:36:12 -0500349# if PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC
John Bowlerb5d00512012-03-09 09:15:18 -0600350 png_chunk_warning(png_ptr, msg);
351 png_ptr->zowner = 0;
352# else
353 png_chunk_error(png_ptr, msg);
354# endif
355 }
356
John Bowler42a2b552012-03-05 20:57:40 -0600357 /* Implementation note: unlike 'png_deflate_claim' this internal function
358 * does not take the size of the data as an argument. Some efficiency could
359 * be gained by using this when it is known *if* the zlib stream itself does
Glenn Randers-Pehrson5b9a28f2012-11-25 12:46:24 -0600360 * not record the number; however, this is an illusion: the original writer
Glenn Randers-Pehrson58512802013-01-10 12:05:59 -0600361 * of the PNG may have selected a lower window size, and we really must
362 * follow that because, for systems with with limited capabilities, we
Glenn Randers-Pehrson5b9a28f2012-11-25 12:46:24 -0600363 * would otherwise reject the application's attempts to use a smaller window
364 * size (zlib doesn't have an interface to say "this or lower"!).
John Bowlerb5d00512012-03-09 09:15:18 -0600365 *
366 * inflateReset2 was added to zlib 1.2.4; before this the window could not be
367 * reset, therefore it is necessary to always allocate the maximum window
368 * size with earlier zlibs just in case later compressed chunks need it.
John Bowler42a2b552012-03-05 20:57:40 -0600369 */
John Bowler42a2b552012-03-05 20:57:40 -0600370 {
371 int ret; /* zlib return code */
372
John Bowlerb5d00512012-03-09 09:15:18 -0600373 /* Set this for safety, just in case the previous owner left pointers to
374 * memory allocations.
375 */
376 png_ptr->zstream.next_in = NULL;
377 png_ptr->zstream.avail_in = 0;
378 png_ptr->zstream.next_out = NULL;
379 png_ptr->zstream.avail_out = 0;
John Bowler42a2b552012-03-05 20:57:40 -0600380
381 if (png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED)
John Bowlerb5d00512012-03-09 09:15:18 -0600382 {
383# if ZLIB_VERNUM < 0x1240
John Bowler90669192012-03-09 22:03:13 -0600384 PNG_UNUSED(window_bits)
John Bowlerb5d00512012-03-09 09:15:18 -0600385 ret = inflateReset(&png_ptr->zstream);
386# else
John Bowler90669192012-03-09 22:03:13 -0600387 ret = inflateReset2(&png_ptr->zstream, window_bits);
John Bowlerb5d00512012-03-09 09:15:18 -0600388# endif
389 }
John Bowler42a2b552012-03-05 20:57:40 -0600390
391 else
392 {
John Bowlerb5d00512012-03-09 09:15:18 -0600393# if ZLIB_VERNUM < 0x1240
394 ret = inflateInit(&png_ptr->zstream);
395# else
John Bowler90669192012-03-09 22:03:13 -0600396 ret = inflateInit2(&png_ptr->zstream, window_bits);
John Bowlerb5d00512012-03-09 09:15:18 -0600397# endif
John Bowler42a2b552012-03-05 20:57:40 -0600398
399 if (ret == Z_OK)
400 png_ptr->flags |= PNG_FLAG_ZSTREAM_INITIALIZED;
401 }
402
403 if (ret == Z_OK)
John Bowlerb5d00512012-03-09 09:15:18 -0600404 png_ptr->zowner = owner;
John Bowler42a2b552012-03-05 20:57:40 -0600405
406 else
John Bowlerb5d00512012-03-09 09:15:18 -0600407 png_zstream_error(png_ptr, ret);
John Bowler42a2b552012-03-05 20:57:40 -0600408
John Bowlerb5d00512012-03-09 09:15:18 -0600409 return ret;
410 }
411}
412
John Bowlerb11b31a2012-03-21 07:55:46 -0500413#ifdef PNG_READ_COMPRESSED_TEXT_SUPPORTED
John Bowlerb5d00512012-03-09 09:15:18 -0600414/* png_inflate now returns zlib error codes including Z_OK and Z_STREAM_END to
415 * allow the caller to do multiple calls if required. If the 'finish' flag is
416 * set Z_FINISH will be passed to the final inflate() call and Z_STREAM_END must
417 * be returned or there has been a problem, otherwise Z_SYNC_FLUSH is used and
418 * Z_OK or Z_STREAM_END will be returned on success.
419 *
420 * The input and output sizes are updated to the actual amounts of data consumed
421 * or written, not the amount available (as in a z_stream). The data pointers
422 * are not changed, so the next input is (data+input_size) and the next
423 * available output is (output+output_size).
424 */
425static int
426png_inflate(png_structrp png_ptr, png_uint_32 owner, int finish,
427 /* INPUT: */ png_const_bytep input, png_uint_32p input_size_ptr,
428 /* OUTPUT: */ png_bytep output, png_alloc_size_t *output_size_ptr)
429{
430 if (png_ptr->zowner == owner) /* Else not claimed */
431 {
432 int ret;
433 png_alloc_size_t avail_out = *output_size_ptr;
434 png_uint_32 avail_in = *input_size_ptr;
435
436 /* zlib can't necessarily handle more than 65535 bytes at once (i.e. it
437 * can't even necessarily handle 65536 bytes) because the type uInt is
438 * "16 bits or more". Consequently it is necessary to chunk the input to
439 * zlib. This code uses ZLIB_IO_MAX, from pngpriv.h, as the maximum (the
440 * maximum value that can be stored in a uInt.) It is possible to set
441 * ZLIB_IO_MAX to a lower value in pngpriv.h and this may sometimes have
442 * a performance advantage, because it reduces the amount of data accessed
443 * at each step and that may give the OS more time to page it in.
444 */
445 png_ptr->zstream.next_in = PNGZ_INPUT_CAST(input);
446 /* avail_in and avail_out are set below from 'size' */
447 png_ptr->zstream.avail_in = 0;
448 png_ptr->zstream.avail_out = 0;
449
450 /* Read directly into the output if it is available (this is set to
451 * a local buffer below if output is NULL).
452 */
453 if (output != NULL)
454 png_ptr->zstream.next_out = output;
455
456 do
457 {
458 uInt avail;
459 Byte local_buffer[PNG_INFLATE_BUF_SIZE];
460
461 /* zlib INPUT BUFFER */
462 /* The setting of 'avail_in' used to be outside the loop; by setting it
463 * inside it is possible to chunk the input to zlib and simply rely on
464 * zlib to advance the 'next_in' pointer. This allows arbitrary
465 * amounts of data to be passed through zlib at the unavoidable cost of
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -0500466 * requiring a window save (memcpy of up to 32768 output bytes)
John Bowlerb5d00512012-03-09 09:15:18 -0600467 * every ZLIB_IO_MAX input bytes.
468 */
469 avail_in += png_ptr->zstream.avail_in; /* not consumed last time */
470
471 avail = ZLIB_IO_MAX;
472
473 if (avail_in < avail)
474 avail = (uInt)avail_in; /* safe: < than ZLIB_IO_MAX */
475
476 avail_in -= avail;
477 png_ptr->zstream.avail_in = avail;
478
479 /* zlib OUTPUT BUFFER */
480 avail_out += png_ptr->zstream.avail_out; /* not written last time */
481
482 avail = ZLIB_IO_MAX; /* maximum zlib can process */
483
484 if (output == NULL)
485 {
486 /* Reset the output buffer each time round if output is NULL and
487 * make available the full buffer, up to 'remaining_space'
488 */
489 png_ptr->zstream.next_out = local_buffer;
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -0500490 if ((sizeof local_buffer) < avail)
491 avail = (sizeof local_buffer);
John Bowlerb5d00512012-03-09 09:15:18 -0600492 }
493
494 if (avail_out < avail)
495 avail = (uInt)avail_out; /* safe: < ZLIB_IO_MAX */
496
497 png_ptr->zstream.avail_out = avail;
498 avail_out -= avail;
499
500 /* zlib inflate call */
501 /* In fact 'avail_out' may be 0 at this point, that happens at the end
502 * of the read when the final LZ end code was not passed at the end of
503 * the previous chunk of input data. Tell zlib if we have reached the
504 * end of the output buffer.
505 */
506 ret = inflate(&png_ptr->zstream, avail_out > 0 ? Z_NO_FLUSH :
507 (finish ? Z_FINISH : Z_SYNC_FLUSH));
508 } while (ret == Z_OK);
509
510 /* For safety kill the local buffer pointer now */
511 if (output == NULL)
512 png_ptr->zstream.next_out = NULL;
513
514 /* Claw back the 'size' and 'remaining_space' byte counts. */
515 avail_in += png_ptr->zstream.avail_in;
516 avail_out += png_ptr->zstream.avail_out;
517
518 /* Update the input and output sizes; the updated values are the amount
519 * consumed or written, effectively the inverse of what zlib uses.
520 */
521 if (avail_out > 0)
522 *output_size_ptr -= avail_out;
523
524 if (avail_in > 0)
525 *input_size_ptr -= avail_in;
526
527 /* Ensure png_ptr->zstream.msg is set (even in the success case!) */
528 png_zstream_error(png_ptr, ret);
529 return ret;
John Bowler42a2b552012-03-05 20:57:40 -0600530 }
531
532 else
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600533 {
John Bowlerb5d00512012-03-09 09:15:18 -0600534 /* This is a bad internal error. The recovery assigns to the zstream msg
535 * pointer, which is not owned by the caller, but this is safe; it's only
536 * used on errors!
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600537 */
John Bowlerb5d00512012-03-09 09:15:18 -0600538 png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed");
539 return Z_STREAM_ERROR;
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600540 }
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600541}
542
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600543/*
John Bowlerb5d00512012-03-09 09:15:18 -0600544 * Decompress trailing data in a chunk. The assumption is that read_buffer
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600545 * points at an allocated area holding the contents of a chunk with a
546 * trailing compressed part. What we get back is an allocated area
547 * holding the original prefix part and an uncompressed version of the
548 * trailing part (the malloc area passed in is freed).
549 */
John Bowlerb5d00512012-03-09 09:15:18 -0600550static int
551png_decompress_chunk(png_structrp png_ptr,
552 png_uint_32 chunklength, png_uint_32 prefix_size,
John Bowler0ae4f7b2012-03-03 21:10:26 -0600553 png_alloc_size_t *newlength /* must be initialized to the maximum! */,
554 int terminate /*add a '\0' to the end of the uncompressed data*/)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600555{
John Bowler0ae4f7b2012-03-03 21:10:26 -0600556 /* TODO: implement different limits for different types of chunk.
557 *
558 * The caller supplies *newlength set to the maximum length of the
559 * uncompressed data, but this routine allocates space for the prefix and
560 * maybe a '\0' terminator too. We have to assume that 'prefix_size' is
561 * limited only by the maximum chunk size.
562 */
563 png_alloc_size_t limit = PNG_SIZE_MAX;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600564
John Bowler0ae4f7b2012-03-03 21:10:26 -0600565# ifdef PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED
566 if (png_ptr->user_chunk_malloc_max > 0 &&
567 png_ptr->user_chunk_malloc_max < limit)
568 limit = png_ptr->user_chunk_malloc_max;
569# elif PNG_USER_CHUNK_MALLOC_MAX > 0
570 if (PNG_USER_CHUNK_MALLOC_MAX < limit)
571 limit = PNG_USER_CHUNK_MALLOC_MAX;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600572# endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600573
John Bowler0ae4f7b2012-03-03 21:10:26 -0600574 if (limit >= prefix_size + (terminate != 0))
575 {
John Bowlerb5d00512012-03-09 09:15:18 -0600576 int ret;
Glenn Randers-Pehrson3744f942012-08-10 19:22:53 -0500577
John Bowler0ae4f7b2012-03-03 21:10:26 -0600578 limit -= prefix_size + (terminate != 0);
579
580 if (limit < *newlength)
581 *newlength = limit;
582
John Bowler90669192012-03-09 22:03:13 -0600583 /* Now try to claim the stream; the 'warn' setting causes zlib to be told
584 * to use the maximum window size during inflate; this hides errors in the
585 * deflate header window bits value which is used if '0' is passed. In
586 * fact this only has an effect with zlib versions 1.2.4 and later - see
587 * the comments in png_inflate_claim above.
588 */
589 ret = png_inflate_claim(png_ptr, png_ptr->chunk_name,
590 png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN ? 15 : 0);
John Bowlerb5d00512012-03-09 09:15:18 -0600591
592 if (ret == Z_OK)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600593 {
John Bowlerb5d00512012-03-09 09:15:18 -0600594 png_uint_32 lzsize = chunklength - prefix_size;
595
596 ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/,
597 /* input: */ png_ptr->read_buffer + prefix_size, &lzsize,
598 /* output: */ NULL, newlength);
Glenn Randers-Pehrson3744f942012-08-10 19:22:53 -0500599
John Bowlerb5d00512012-03-09 09:15:18 -0600600 if (ret == Z_STREAM_END)
601 {
John Bowler90669192012-03-09 22:03:13 -0600602 /* Use 'inflateReset' here, not 'inflateReset2' because this
603 * preserves the previously decided window size (otherwise it would
604 * be necessary to store the previous window size.) In practice
605 * this doesn't matter anyway, because png_inflate will call inflate
606 * with Z_FINISH in almost all cases, so the window will not be
607 * maintained.
608 */
John Bowlerb5d00512012-03-09 09:15:18 -0600609 if (inflateReset(&png_ptr->zstream) == Z_OK)
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600610 {
John Bowler0ae4f7b2012-03-03 21:10:26 -0600611 /* Because of the limit checks above we know that the new,
612 * expanded, size will fit in a size_t (let alone an
613 * png_alloc_size_t). Use png_malloc_base here to avoid an
614 * extra OOM message.
615 */
616 png_alloc_size_t new_size = *newlength;
John Bowlerb5d00512012-03-09 09:15:18 -0600617 png_alloc_size_t buffer_size = prefix_size + new_size +
618 (terminate != 0);
619 png_bytep text = png_voidcast(png_bytep, png_malloc_base(png_ptr,
620 buffer_size));
John Bowler0ae4f7b2012-03-03 21:10:26 -0600621
John Bowlerb5d00512012-03-09 09:15:18 -0600622 if (text != NULL)
John Bowler0ae4f7b2012-03-03 21:10:26 -0600623 {
John Bowlerb5d00512012-03-09 09:15:18 -0600624 ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/,
625 png_ptr->read_buffer + prefix_size, &lzsize,
626 text + prefix_size, newlength);
627
628 if (ret == Z_STREAM_END)
629 {
John Bowler0ae4f7b2012-03-03 21:10:26 -0600630 if (new_size == *newlength)
631 {
632 if (terminate)
633 text[prefix_size + *newlength] = 0;
634
635 if (prefix_size > 0)
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -0500636 memcpy(text, png_ptr->read_buffer, prefix_size);
John Bowler0ae4f7b2012-03-03 21:10:26 -0600637
John Bowlerb5d00512012-03-09 09:15:18 -0600638 {
639 png_bytep old_ptr = png_ptr->read_buffer;
640
641 png_ptr->read_buffer = text;
642 png_ptr->read_buffer_size = buffer_size;
643 text = old_ptr; /* freed below */
644 }
John Bowler0ae4f7b2012-03-03 21:10:26 -0600645 }
646
John Bowlerb5d00512012-03-09 09:15:18 -0600647 else
648 {
649 /* The size changed on the second read, there can be no
650 * guarantee that anything is correct at this point.
651 * The 'msg' pointer has been set to "unexpected end of
652 * LZ stream", which is fine, but return an error code
653 * that the caller won't accept.
654 */
655 ret = PNG_UNEXPECTED_ZLIB_RETURN;
656 }
657 }
John Bowler0ae4f7b2012-03-03 21:10:26 -0600658
John Bowlerb5d00512012-03-09 09:15:18 -0600659 else if (ret == Z_OK)
660 ret = PNG_UNEXPECTED_ZLIB_RETURN; /* for safety */
John Bowler0ae4f7b2012-03-03 21:10:26 -0600661
John Bowlerb5d00512012-03-09 09:15:18 -0600662 /* Free the text pointer (this is the old read_buffer on
663 * success)
664 */
665 png_free(png_ptr, text);
666
667 /* This really is very benign, but it's still an error because
668 * the extra space may otherwise be used as a Trojan Horse.
669 */
670 if (ret == Z_STREAM_END &&
671 chunklength - prefix_size != lzsize)
672 png_chunk_benign_error(png_ptr, "extra compressed data");
673 }
674
675 else
676 {
677 /* Out of memory allocating the buffer */
678 ret = Z_MEM_ERROR;
679 png_zstream_error(png_ptr, Z_MEM_ERROR);
John Bowler0ae4f7b2012-03-03 21:10:26 -0600680 }
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600681 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600682
John Bowlerb5d00512012-03-09 09:15:18 -0600683 else
684 {
685 /* inflateReset failed, store the error message */
686 png_zstream_error(png_ptr, ret);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600687
John Bowlerb5d00512012-03-09 09:15:18 -0600688 if (ret == Z_STREAM_END)
689 ret = PNG_UNEXPECTED_ZLIB_RETURN;
690 }
691 }
692
693 else if (ret == Z_OK)
694 ret = PNG_UNEXPECTED_ZLIB_RETURN;
695
696 /* Release the claimed stream */
697 png_ptr->zowner = 0;
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600698 }
John Bowlerb5d00512012-03-09 09:15:18 -0600699
700 else /* the claim failed */ if (ret == Z_STREAM_END) /* impossible! */
701 ret = PNG_UNEXPECTED_ZLIB_RETURN;
702
703 return ret;
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600704 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600705
John Bowlerb5d00512012-03-09 09:15:18 -0600706 else
707 {
708 /* Application/configuration limits exceeded */
709 png_zstream_error(png_ptr, Z_MEM_ERROR);
710 return Z_MEM_ERROR;
711 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600712}
Glenn Randers-Pehrson5975f312011-04-01 13:15:36 -0500713#endif /* PNG_READ_COMPRESSED_TEXT_SUPPORTED */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600714
John Bowlerb11b31a2012-03-21 07:55:46 -0500715#ifdef PNG_READ_iCCP_SUPPORTED
716/* Perform a partial read and decompress, producing 'avail_out' bytes and
717 * reading from the current chunk as required.
718 */
719static int
720png_inflate_read(png_structrp png_ptr, png_bytep read_buffer, uInt read_size,
721 png_uint_32p chunk_bytes, png_bytep next_out, png_alloc_size_t *out_size,
722 int finish)
723{
724 if (png_ptr->zowner == png_ptr->chunk_name)
725 {
726 int ret;
727
728 /* next_in and avail_in must have been initialized by the caller. */
729 png_ptr->zstream.next_out = next_out;
730 png_ptr->zstream.avail_out = 0; /* set in the loop */
731
732 do
733 {
734 if (png_ptr->zstream.avail_in == 0)
735 {
736 if (read_size > *chunk_bytes)
737 read_size = (uInt)*chunk_bytes;
738 *chunk_bytes -= read_size;
739
740 if (read_size > 0)
741 png_crc_read(png_ptr, read_buffer, read_size);
742
743 png_ptr->zstream.next_in = read_buffer;
744 png_ptr->zstream.avail_in = read_size;
745 }
746
747 if (png_ptr->zstream.avail_out == 0)
748 {
749 uInt avail = ZLIB_IO_MAX;
750 if (avail > *out_size)
751 avail = (uInt)*out_size;
752 *out_size -= avail;
753
754 png_ptr->zstream.avail_out = avail;
755 }
756
757 /* Use Z_SYNC_FLUSH when there is no more chunk data to ensure that all
758 * the available output is produced; this allows reading of truncated
759 * streams.
760 */
761 ret = inflate(&png_ptr->zstream,
762 *chunk_bytes > 0 ? Z_NO_FLUSH : (finish ? Z_FINISH : Z_SYNC_FLUSH));
763 }
764 while (ret == Z_OK && (*out_size > 0 || png_ptr->zstream.avail_out > 0));
765
766 *out_size += png_ptr->zstream.avail_out;
767 png_ptr->zstream.avail_out = 0; /* Should not be required, but is safe */
768
769 /* Ensure the error message pointer is always set: */
770 png_zstream_error(png_ptr, ret);
771 return ret;
772 }
773
774 else
775 {
776 png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed");
777 return Z_STREAM_ERROR;
778 }
779}
780#endif
781
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500782/* Read and check the IDHR chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500783void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600784png_handle_IHDR(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500785{
786 png_byte buf[13];
787 png_uint_32 width, height;
788 int bit_depth, color_type, compression_type, filter_type;
789 int interlace_type;
790
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500791 png_debug(1, "in png_handle_IHDR");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500792
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600793 if (png_ptr->mode & PNG_HAVE_IHDR)
John Bowlerb11b31a2012-03-21 07:55:46 -0500794 png_chunk_error(png_ptr, "out of place");
Guy Schalnate5a37791996-06-05 15:50:50 -0500795
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500796 /* Check the length */
Guy Schalnat0d580581995-07-20 02:43:20 -0500797 if (length != 13)
John Bowlerb11b31a2012-03-21 07:55:46 -0500798 png_chunk_error(png_ptr, "invalid");
Guy Schalnat0d580581995-07-20 02:43:20 -0500799
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600800 png_ptr->mode |= PNG_HAVE_IHDR;
801
Guy Schalnat0d580581995-07-20 02:43:20 -0500802 png_crc_read(png_ptr, buf, 13);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600803 png_crc_finish(png_ptr, 0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500804
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500805 width = png_get_uint_31(png_ptr, buf);
806 height = png_get_uint_31(png_ptr, buf + 4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500807 bit_depth = buf[8];
808 color_type = buf[9];
809 compression_type = buf[10];
810 filter_type = buf[11];
811 interlace_type = buf[12];
812
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500813 /* Set internal variables */
Guy Schalnat0d580581995-07-20 02:43:20 -0500814 png_ptr->width = width;
815 png_ptr->height = height;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600816 png_ptr->bit_depth = (png_byte)bit_depth;
817 png_ptr->interlaced = (png_byte)interlace_type;
818 png_ptr->color_type = (png_byte)color_type;
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500819#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600820 png_ptr->filter_type = (png_byte)filter_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500821#endif
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500822 png_ptr->compression_type = (png_byte)compression_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500823
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500824 /* Find number of channels */
Guy Schalnat0d580581995-07-20 02:43:20 -0500825 switch (png_ptr->color_type)
826 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600827 default: /* invalid, png_set_IHDR calls png_error */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500828 case PNG_COLOR_TYPE_GRAY:
829 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnat0d580581995-07-20 02:43:20 -0500830 png_ptr->channels = 1;
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:
Guy Schalnat0d580581995-07-20 02:43:20 -0500834 png_ptr->channels = 3;
835 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500836
Andreas Dilger47a0c421997-05-16 02:46:07 -0500837 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnat0d580581995-07-20 02:43:20 -0500838 png_ptr->channels = 2;
839 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500840
Andreas Dilger47a0c421997-05-16 02:46:07 -0500841 case PNG_COLOR_TYPE_RGB_ALPHA:
Guy Schalnat0d580581995-07-20 02:43:20 -0500842 png_ptr->channels = 4;
843 break;
844 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600845
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500846 /* Set up other useful info */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600847 png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth *
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600848 png_ptr->channels);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500849 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width);
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500850 png_debug1(3, "bit_depth = %d", png_ptr->bit_depth);
851 png_debug1(3, "channels = %d", png_ptr->channels);
Glenn Randers-Pehrsonb764c602011-01-14 21:18:37 -0600852 png_debug1(3, "rowbytes = %lu", (unsigned long)png_ptr->rowbytes);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500853 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600854 color_type, interlace_type, compression_type, filter_type);
Guy Schalnat0d580581995-07-20 02:43:20 -0500855}
856
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500857/* Read and check the palette */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500858void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600859png_handle_PLTE(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500860{
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600861 png_color palette[PNG_MAX_PALETTE_LENGTH];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600862 int num, i;
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500863#ifdef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500864 png_colorp pal_ptr;
865#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500866
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500867 png_debug(1, "in png_handle_PLTE");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500868
Guy Schalnate5a37791996-06-05 15:50:50 -0500869 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -0500870 png_chunk_error(png_ptr, "missing IHDR");
871
872 /* Moved to before the 'after IDAT' check below because otherwise duplicate
873 * PLTE chunks are potentially ignored (the spec says there shall not be more
874 * than one PLTE, the error is not treated as benign, so this check trumps
875 * the requirement that PLTE appears before IDAT.)
876 */
877 else if (png_ptr->mode & PNG_HAVE_PLTE)
878 png_chunk_error(png_ptr, "duplicate");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500879
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600880 else if (png_ptr->mode & PNG_HAVE_IDAT)
881 {
John Bowlerb11b31a2012-03-21 07:55:46 -0500882 /* This is benign because the non-benign error happened before, when an
883 * IDAT was encountered in a color-mapped image with no PLTE.
884 */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600885 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -0500886 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600887 return;
888 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500889
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600890 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500891
John Bowlerb11b31a2012-03-21 07:55:46 -0500892 if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR))
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500893 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500894 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -0500895 png_chunk_benign_error(png_ptr, "ignored in grayscale PNG");
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500896 return;
897 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600898
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -0500899#ifndef PNG_READ_OPT_PLTE_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500900 if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
901 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600902 png_crc_finish(png_ptr, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500903 return;
904 }
905#endif
906
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600907 if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3)
Guy Schalnate5a37791996-06-05 15:50:50 -0500908 {
John Bowlerb11b31a2012-03-21 07:55:46 -0500909 png_crc_finish(png_ptr, length);
910
Guy Schalnate5a37791996-06-05 15:50:50 -0500911 if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
John Bowlerb11b31a2012-03-21 07:55:46 -0500912 png_chunk_benign_error(png_ptr, "invalid");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500913
Guy Schalnate5a37791996-06-05 15:50:50 -0500914 else
John Bowlerb11b31a2012-03-21 07:55:46 -0500915 png_chunk_error(png_ptr, "invalid");
916
917 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500918 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500919
John Bowlerb11b31a2012-03-21 07:55:46 -0500920 /* The cast is safe because 'length' is less than 3*PNG_MAX_PALETTE_LENGTH */
Guy Schalnat0d580581995-07-20 02:43:20 -0500921 num = (int)length / 3;
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500922
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500923#ifdef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500924 for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)
925 {
926 png_byte buf[3];
927
928 png_crc_read(png_ptr, buf, 3);
929 pal_ptr->red = buf[0];
930 pal_ptr->green = buf[1];
931 pal_ptr->blue = buf[2];
932 }
933#else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600934 for (i = 0; i < num; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500935 {
936 png_byte buf[3];
937
938 png_crc_read(png_ptr, buf, 3);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500939 /* Don't depend upon png_color being any order */
Guy Schalnat0d580581995-07-20 02:43:20 -0500940 palette[i].red = buf[0];
941 palette[i].green = buf[1];
942 palette[i].blue = buf[2];
943 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500944#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600945
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600946 /* 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 -0500947 * whatever the normal CRC configuration tells us. However, if we
948 * have an RGB image, the PLTE can be considered ancillary, so
949 * we will act as though it is.
950 */
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -0500951#ifndef PNG_READ_OPT_PLTE_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600952 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600953#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600954 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500955 png_crc_finish(png_ptr, 0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600956 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600957
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -0500958#ifndef PNG_READ_OPT_PLTE_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600959 else if (png_crc_error(png_ptr)) /* Only if we have a CRC error */
960 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600961 /* If we don't want to use the data from an ancillary chunk,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600962 * we have two options: an error abort, or a warning and we
963 * ignore the data in this chunk (which should be OK, since
964 * it's considered ancillary for a RGB or RGBA image).
John Bowlerb11b31a2012-03-21 07:55:46 -0500965 *
966 * IMPLEMENTATION NOTE: this is only here because png_crc_finish uses the
967 * chunk type to determine whether to check the ancillary or the critical
968 * flags.
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600969 */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600970 if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE))
971 {
972 if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)
973 {
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500974 png_chunk_benign_error(png_ptr, "CRC error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600975 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600976
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600977 else
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 return;
981 }
982 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600983
Andreas Dilger47a0c421997-05-16 02:46:07 -0500984 /* Otherwise, we (optionally) emit a warning and use the chunk. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600985 else if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN))
986 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600987 png_chunk_warning(png_ptr, "CRC error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600988 }
989 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600990#endif
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500991
John Bowlerb11b31a2012-03-21 07:55:46 -0500992 /* TODO: png_set_PLTE has the side effect of setting png_ptr->palette to its
993 * own copy of the palette. This has the side effect that when png_start_row
994 * is called (this happens after any call to png_read_update_info) the
995 * info_ptr palette gets changed. This is extremely unexpected and
996 * confusing.
997 *
998 * Fix this by not sharing the palette in this way.
999 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001000 png_set_PLTE(png_ptr, info_ptr, palette, num);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001001
John Bowlerb11b31a2012-03-21 07:55:46 -05001002 /* The three chunks, bKGD, hIST and tRNS *must* appear after PLTE and before
1003 * IDAT. Prior to 1.6.0 this was not checked; instead the code merely
1004 * checked the apparent validity of a tRNS chunk inserted before PLTE on a
1005 * palette PNG. 1.6.0 attempts to rigorously follow the standard and
1006 * therefore does a benign error if the erroneous condition is detected *and*
1007 * cancels the tRNS if the benign error returns. The alternative is to
1008 * amend the standard since it would be rather hypocritical of the standards
1009 * maintainers to ignore it.
1010 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001011#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrson3744f942012-08-10 19:22:53 -05001012 if (png_ptr->num_trans > 0 ||
John Bowlerb11b31a2012-03-21 07:55:46 -05001013 (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS) != 0))
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001014 {
John Bowlerb11b31a2012-03-21 07:55:46 -05001015 /* Cancel this because otherwise it would be used if the transforms
1016 * require it. Don't cancel the 'valid' flag because this would prevent
1017 * detection of duplicate chunks.
1018 */
1019 png_ptr->num_trans = 0;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001020
John Bowlerb11b31a2012-03-21 07:55:46 -05001021 if (info_ptr != NULL)
1022 info_ptr->num_trans = 0;
1023
1024 png_chunk_benign_error(png_ptr, "tRNS must be after");
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001025 }
1026#endif
1027
John Bowlerb11b31a2012-03-21 07:55:46 -05001028#ifdef PNG_READ_hIST_SUPPORTED
1029 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) != 0)
1030 png_chunk_benign_error(png_ptr, "hIST must be after");
1031#endif
1032
1033#ifdef PNG_READ_bKGD_SUPPORTED
1034 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) != 0)
1035 png_chunk_benign_error(png_ptr, "bKGD must be after");
1036#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001037}
Guy Schalnate5a37791996-06-05 15:50:50 -05001038
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001039void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001040png_handle_IEND(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001041{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001042 png_debug(1, "in png_handle_IEND");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001043
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001044 if (!(png_ptr->mode & PNG_HAVE_IHDR) || !(png_ptr->mode & PNG_HAVE_IDAT))
John Bowlerb11b31a2012-03-21 07:55:46 -05001045 png_chunk_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001046
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001047 png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001048
Andreas Dilger47a0c421997-05-16 02:46:07 -05001049 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001050
John Bowlerb11b31a2012-03-21 07:55:46 -05001051 if (length != 0)
1052 png_chunk_benign_error(png_ptr, "invalid");
1053
1054 PNG_UNUSED(info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001055}
1056
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001057#ifdef PNG_READ_gAMA_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001058void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001059png_handle_gAMA(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001060{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001061 png_fixed_point igamma;
Guy Schalnat0d580581995-07-20 02:43:20 -05001062 png_byte buf[4];
1063
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001064 png_debug(1, "in png_handle_gAMA");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001065
Guy Schalnate5a37791996-06-05 15:50:50 -05001066 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05001067 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001068
John Bowlerb11b31a2012-03-21 07:55:46 -05001069 else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001070 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001071 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001072 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001073 return;
1074 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001075
Guy Schalnat0d580581995-07-20 02:43:20 -05001076 if (length != 4)
1077 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001078 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001079 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnat0d580581995-07-20 02:43:20 -05001080 return;
1081 }
1082
1083 png_crc_read(png_ptr, buf, 4);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001084
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001085 if (png_crc_finish(png_ptr, 0))
1086 return;
1087
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001088 igamma = png_get_fixed_point(NULL, buf);
Glenn Randers-Pehrson33893092010-10-23 13:20:18 -05001089
John Bowlerf8dfd122012-10-25 19:30:02 -05001090 png_colorspace_set_gamma(png_ptr, &png_ptr->colorspace, igamma);
John Bowlerb11b31a2012-03-21 07:55:46 -05001091 png_colorspace_sync(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001092}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001093#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001094
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001095#ifdef PNG_READ_sBIT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001096void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001097png_handle_sBIT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001098{
John Bowler0ae4f7b2012-03-03 21:10:26 -06001099 unsigned int truelen;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001100 png_byte buf[4];
Guy Schalnat69b14481996-01-10 02:56:49 -06001101
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001102 png_debug(1, "in png_handle_sBIT");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001103
Guy Schalnat69b14481996-01-10 02:56:49 -06001104 buf[0] = buf[1] = buf[2] = buf[3] = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -05001105
Guy Schalnate5a37791996-06-05 15:50:50 -05001106 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05001107 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001108
John Bowlerb11b31a2012-03-21 07:55:46 -05001109 else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001110 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001111 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001112 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001113 return;
1114 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001115
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001116 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001117 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001118 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001119 png_chunk_benign_error(png_ptr, "duplicate");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001120 return;
1121 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001122
Guy Schalnat0d580581995-07-20 02:43:20 -05001123 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001124 truelen = 3;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001125
Guy Schalnat0d580581995-07-20 02:43:20 -05001126 else
John Bowler0ae4f7b2012-03-03 21:10:26 -06001127 truelen = png_ptr->channels;
Guy Schalnat0d580581995-07-20 02:43:20 -05001128
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001129 if (length != truelen || length > 4)
Guy Schalnat0d580581995-07-20 02:43:20 -05001130 {
John Bowlerb11b31a2012-03-21 07:55:46 -05001131 png_chunk_benign_error(png_ptr, "invalid");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001132 png_crc_finish(png_ptr, length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001133 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001134 }
1135
Andreas Dilger47a0c421997-05-16 02:46:07 -05001136 png_crc_read(png_ptr, buf, truelen);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001137
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001138 if (png_crc_finish(png_ptr, 0))
1139 return;
1140
Guy Schalnat0d580581995-07-20 02:43:20 -05001141 if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
1142 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001143 png_ptr->sig_bit.red = buf[0];
1144 png_ptr->sig_bit.green = buf[1];
1145 png_ptr->sig_bit.blue = buf[2];
1146 png_ptr->sig_bit.alpha = buf[3];
Guy Schalnat0d580581995-07-20 02:43:20 -05001147 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001148
Guy Schalnat0d580581995-07-20 02:43:20 -05001149 else
1150 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001151 png_ptr->sig_bit.gray = buf[0];
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001152 png_ptr->sig_bit.red = buf[0];
1153 png_ptr->sig_bit.green = buf[0];
1154 png_ptr->sig_bit.blue = buf[0];
Guy Schalnat6d764711995-12-19 03:22:19 -06001155 png_ptr->sig_bit.alpha = buf[1];
Guy Schalnat0d580581995-07-20 02:43:20 -05001156 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001157
Andreas Dilger47a0c421997-05-16 02:46:07 -05001158 png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
Guy Schalnat0d580581995-07-20 02:43:20 -05001159}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001160#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001161
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001162#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001163void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001164png_handle_cHRM(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001165{
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001166 png_byte buf[32];
John Bowlerb11b31a2012-03-21 07:55:46 -05001167 png_xy xy;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -06001168
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001169 png_debug(1, "in png_handle_cHRM");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001170
Guy Schalnate5a37791996-06-05 15:50:50 -05001171 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05001172 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001173
John Bowlerb11b31a2012-03-21 07:55:46 -05001174 else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001175 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001176 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001177 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001178 return;
1179 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001180
Guy Schalnat0d580581995-07-20 02:43:20 -05001181 if (length != 32)
1182 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001183 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001184 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001185 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001186 }
1187
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001188 png_crc_read(png_ptr, buf, 32);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001189
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001190 if (png_crc_finish(png_ptr, 0))
1191 return;
1192
John Bowlerb11b31a2012-03-21 07:55:46 -05001193 xy.whitex = png_get_fixed_point(NULL, buf);
1194 xy.whitey = png_get_fixed_point(NULL, buf + 4);
1195 xy.redx = png_get_fixed_point(NULL, buf + 8);
1196 xy.redy = png_get_fixed_point(NULL, buf + 12);
1197 xy.greenx = png_get_fixed_point(NULL, buf + 16);
1198 xy.greeny = png_get_fixed_point(NULL, buf + 20);
1199 xy.bluex = png_get_fixed_point(NULL, buf + 24);
1200 xy.bluey = png_get_fixed_point(NULL, buf + 28);
Glenn Randers-Pehrson33893092010-10-23 13:20:18 -05001201
John Bowlerb11b31a2012-03-21 07:55:46 -05001202 if (xy.whitex == PNG_FIXED_ERROR ||
1203 xy.whitey == PNG_FIXED_ERROR ||
1204 xy.redx == PNG_FIXED_ERROR ||
1205 xy.redy == PNG_FIXED_ERROR ||
1206 xy.greenx == PNG_FIXED_ERROR ||
1207 xy.greeny == PNG_FIXED_ERROR ||
1208 xy.bluex == PNG_FIXED_ERROR ||
1209 xy.bluey == PNG_FIXED_ERROR)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001210 {
John Bowlerb11b31a2012-03-21 07:55:46 -05001211 png_chunk_benign_error(png_ptr, "invalid values");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001212 return;
1213 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001214
John Bowlerb11b31a2012-03-21 07:55:46 -05001215 /* If a colorspace error has already been output skip this chunk */
1216 if (png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID)
1217 return;
1218
1219 if (png_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001220 {
John Bowlerb11b31a2012-03-21 07:55:46 -05001221 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1222 png_colorspace_sync(png_ptr, info_ptr);
1223 png_chunk_benign_error(png_ptr, "duplicate");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001224 return;
1225 }
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001226
John Bowlerb11b31a2012-03-21 07:55:46 -05001227 png_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM;
1228 (void)png_colorspace_set_chromaticities(png_ptr, &png_ptr->colorspace, &xy,
1229 1/*prefer cHRM values*/);
1230 png_colorspace_sync(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001231}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001232#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001233
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001234#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001235void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001236png_handle_sRGB(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001237{
John Bowlerb11b31a2012-03-21 07:55:46 -05001238 png_byte intent;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001239
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001240 png_debug(1, "in png_handle_sRGB");
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001241
1242 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05001243 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001244
John Bowlerb11b31a2012-03-21 07:55:46 -05001245 else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE))
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001246 {
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001247 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001248 png_chunk_benign_error(png_ptr, "out of place");
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001249 return;
1250 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001251
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001252 if (length != 1)
1253 {
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001254 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001255 png_chunk_benign_error(png_ptr, "invalid");
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001256 return;
1257 }
1258
John Bowlerb11b31a2012-03-21 07:55:46 -05001259 png_crc_read(png_ptr, &intent, 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001260
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001261 if (png_crc_finish(png_ptr, 0))
1262 return;
1263
John Bowlerb11b31a2012-03-21 07:55:46 -05001264 /* If a colorspace error has already been output skip this chunk */
1265 if (png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID)
1266 return;
John Bowler88b77cc2011-05-05 06:49:55 -05001267
John Bowlerb11b31a2012-03-21 07:55:46 -05001268 /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect
1269 * this.
John Bowler736f40f2011-08-25 16:19:44 -05001270 */
John Bowlerb11b31a2012-03-21 07:55:46 -05001271 if (png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT)
1272 {
1273 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1274 png_colorspace_sync(png_ptr, info_ptr);
1275 png_chunk_benign_error(png_ptr, "too many profiles");
1276 return;
1277 }
John Bowler736f40f2011-08-25 16:19:44 -05001278
John Bowler520803d2012-11-01 08:12:47 -05001279 (void)png_colorspace_set_sRGB(png_ptr, &png_ptr->colorspace, intent);
John Bowlerb11b31a2012-03-21 07:55:46 -05001280 png_colorspace_sync(png_ptr, info_ptr);
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06001281}
1282#endif /* PNG_READ_sRGB_SUPPORTED */
1283
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001284#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001285void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001286png_handle_iCCP(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
John Bowleraa816c42012-03-16 07:39:49 -05001287/* Note: this does not properly handle profiles that are > 64K under DOS */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001288{
John Bowler921648a2012-03-28 23:36:12 -05001289 png_const_charp errmsg = NULL; /* error message output, or no error */
1290 int finished = 0; /* crc checked */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001291
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001292 png_debug(1, "in png_handle_iCCP");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001293
1294 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05001295 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001296
John Bowlerb11b31a2012-03-21 07:55:46 -05001297 else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001298 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001299 png_crc_finish(png_ptr, length);
John Bowleraa816c42012-03-16 07:39:49 -05001300 png_chunk_benign_error(png_ptr, "out of place");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001301 return;
1302 }
1303
John Bowlerb11b31a2012-03-21 07:55:46 -05001304 /* Consistent with all the above colorspace handling an obviously *invalid*
1305 * chunk is just ignored, so does not invalidate the color space. An
1306 * alternative is to set the 'invalid' flags at the start of this routine
1307 * and only clear them in they were not set before and all the tests pass.
1308 * The minimum 'deflate' stream is assumed to be just the 2 byte header and 4
1309 * byte checksum. The keyword must be one character and there is a
1310 * terminator (0) byte and the compression method.
1311 */
1312 if (length < 9)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001313 {
John Bowler0ae4f7b2012-03-03 21:10:26 -06001314 png_crc_finish(png_ptr, length);
John Bowleraa816c42012-03-16 07:39:49 -05001315 png_chunk_benign_error(png_ptr, "too short");
John Bowler0ae4f7b2012-03-03 21:10:26 -06001316 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001317 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001318
John Bowlerb11b31a2012-03-21 07:55:46 -05001319 /* If a colorspace error has already been output skip this chunk */
1320 if (png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID)
John Bowler0ae4f7b2012-03-03 21:10:26 -06001321 {
John Bowlerb11b31a2012-03-21 07:55:46 -05001322 png_crc_finish(png_ptr, length);
John Bowler0ae4f7b2012-03-03 21:10:26 -06001323 return;
1324 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001325
John Bowlerb11b31a2012-03-21 07:55:46 -05001326 /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect
1327 * this.
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001328 */
John Bowlerb11b31a2012-03-21 07:55:46 -05001329 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) == 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001330 {
John Bowlerb11b31a2012-03-21 07:55:46 -05001331 uInt read_length, keyword_length;
1332 char keyword[81];
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001333
John Bowlerb11b31a2012-03-21 07:55:46 -05001334 /* Find the keyword; the keyword plus separator and compression method
1335 * bytes can be at most 81 characters long.
John Bowler0ae4f7b2012-03-03 21:10:26 -06001336 */
John Bowlerb11b31a2012-03-21 07:55:46 -05001337 read_length = 81; /* maximum */
1338 if (read_length > length)
1339 read_length = (uInt)length;
John Bowler0ae4f7b2012-03-03 21:10:26 -06001340
John Bowlerb11b31a2012-03-21 07:55:46 -05001341 png_crc_read(png_ptr, (png_bytep)keyword, read_length);
1342 length -= read_length;
John Bowler0ae4f7b2012-03-03 21:10:26 -06001343
John Bowlerb11b31a2012-03-21 07:55:46 -05001344 keyword_length = 0;
1345 while (keyword_length < 80 && keyword_length < read_length &&
1346 keyword[keyword_length] != 0)
1347 ++keyword_length;
1348
1349 /* TODO: make the keyword checking common */
1350 if (keyword_length >= 1 && keyword_length <= 79)
John Bowler0ae4f7b2012-03-03 21:10:26 -06001351 {
John Bowlerb11b31a2012-03-21 07:55:46 -05001352 /* We only understand '0' compression - deflate - so if we get a
1353 * different value we can't safely decode the chunk.
John Bowler0ae4f7b2012-03-03 21:10:26 -06001354 */
John Bowlerb11b31a2012-03-21 07:55:46 -05001355 if (keyword_length+1 < read_length &&
1356 keyword[keyword_length+1] == PNG_COMPRESSION_TYPE_BASE)
1357 {
1358 read_length -= keyword_length+2;
John Bowler0ae4f7b2012-03-03 21:10:26 -06001359
John Bowlerb11b31a2012-03-21 07:55:46 -05001360 if (png_inflate_claim(png_ptr, png_iCCP,
1361 png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN ? 15 : 0) == Z_OK)
1362 {
John Bowlerb11b31a2012-03-21 07:55:46 -05001363 Byte profile_header[132];
1364 Byte local_buffer[PNG_INFLATE_BUF_SIZE];
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05001365 png_alloc_size_t size = (sizeof profile_header);
John Bowlerb11b31a2012-03-21 07:55:46 -05001366
1367 png_ptr->zstream.next_in = (Bytef*)keyword + (keyword_length+2);
1368 png_ptr->zstream.avail_in = read_length;
John Bowler921648a2012-03-28 23:36:12 -05001369 (void)png_inflate_read(png_ptr, local_buffer,
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05001370 (sizeof local_buffer), &length, profile_header, &size,
John Bowlerb11b31a2012-03-21 07:55:46 -05001371 0/*finish: don't, because the output is too small*/);
1372
1373 if (size == 0)
1374 {
1375 /* We have the ICC profile header; do the basic header checks.
1376 */
1377 const png_uint_32 profile_length =
1378 png_get_uint_32(profile_header);
1379
John Bowlerb11b31a2012-03-21 07:55:46 -05001380 if (png_icc_check_length(png_ptr, &png_ptr->colorspace,
1381 keyword, profile_length))
1382 {
1383 /* The length is apparently ok, so we can check the 132
1384 * byte header.
1385 */
1386 if (png_icc_check_header(png_ptr, &png_ptr->colorspace,
John Bowler14d0ca62012-08-25 16:21:46 -05001387 keyword, profile_length, profile_header,
1388 png_ptr->color_type))
John Bowlerb11b31a2012-03-21 07:55:46 -05001389 {
1390 /* Now read the tag table; a variable size buffer is
1391 * needed at this point, allocate one for the whole
1392 * profile. The header check has already validated
1393 * that none of these stuff will overflow.
1394 */
1395 const png_uint_32 tag_count = png_get_uint_32(
1396 profile_header+128);
1397 png_bytep profile = png_read_buffer(png_ptr,
1398 profile_length, 2/*silent*/);
1399
1400 if (profile != NULL)
1401 {
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -05001402 memcpy(profile, profile_header,
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05001403 (sizeof profile_header));
John Bowlerb11b31a2012-03-21 07:55:46 -05001404
John Bowler13a87d92012-03-28 09:51:43 -05001405 size = 12 * tag_count;
Glenn Randers-Pehrson8fbd60d2012-03-21 09:18:15 -05001406
John Bowler921648a2012-03-28 23:36:12 -05001407 (void)png_inflate_read(png_ptr, local_buffer,
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05001408 (sizeof local_buffer), &length,
John Bowlerb11b31a2012-03-21 07:55:46 -05001409 profile + (sizeof profile_header), &size, 0);
1410
John Bowler921648a2012-03-28 23:36:12 -05001411 /* Still expect a a buffer error because we expect
1412 * there to be some tag data!
1413 */
John Bowlerb11b31a2012-03-21 07:55:46 -05001414 if (size == 0)
1415 {
1416 if (png_icc_check_tag_table(png_ptr,
1417 &png_ptr->colorspace, keyword, profile_length,
1418 profile))
1419 {
1420 /* The profile has been validated for basic
1421 * security issues, so read the whole thing in.
1422 */
1423 size = profile_length - (sizeof profile_header)
1424 - 12 * tag_count;
Glenn Randers-Pehrson8fbd60d2012-03-21 09:18:15 -05001425
John Bowler921648a2012-03-28 23:36:12 -05001426 (void)png_inflate_read(png_ptr, local_buffer,
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05001427 (sizeof local_buffer), &length,
John Bowlerb11b31a2012-03-21 07:55:46 -05001428 profile + (sizeof profile_header) +
1429 12 * tag_count, &size, 1/*finish*/);
1430
John Bowler921648a2012-03-28 23:36:12 -05001431 if (length > 0 && !(png_ptr->flags &
1432 PNG_FLAG_BENIGN_ERRORS_WARN))
1433 errmsg = "extra compressed data";
1434
1435 /* But otherwise allow extra data: */
1436 else if (size == 0)
John Bowlerb11b31a2012-03-21 07:55:46 -05001437 {
John Bowler921648a2012-03-28 23:36:12 -05001438 if (length > 0)
1439 {
1440 /* This can be handled completely, so
1441 * keep going.
1442 */
John Bowlerb11b31a2012-03-21 07:55:46 -05001443 png_chunk_warning(png_ptr,
1444 "extra compressed data");
John Bowler921648a2012-03-28 23:36:12 -05001445 }
John Bowlerb11b31a2012-03-21 07:55:46 -05001446
John Bowler921648a2012-03-28 23:36:12 -05001447 png_crc_finish(png_ptr, length);
1448 finished = 1;
1449
John Bowler520803d2012-11-01 08:12:47 -05001450# ifdef PNG_sRGB_SUPPORTED
1451 /* Check for a match against sRGB */
1452 png_icc_set_sRGB(png_ptr,
1453 &png_ptr->colorspace, profile,
1454 png_ptr->zstream.adler);
1455# endif
John Bowlerb11b31a2012-03-21 07:55:46 -05001456
Glenn Randers-Pehrson8fbd60d2012-03-21 09:18:15 -05001457 /* Steal the profile for info_ptr. */
John Bowlerf8dfd122012-10-25 19:30:02 -05001458 if (info_ptr != NULL)
John Bowlerb11b31a2012-03-21 07:55:46 -05001459 {
1460 png_free_data(png_ptr, info_ptr,
1461 PNG_FREE_ICCP, 0);
1462
1463 info_ptr->iccp_name = png_voidcast(char*,
1464 png_malloc_base(png_ptr,
1465 keyword_length+1));
John Bowler921648a2012-03-28 23:36:12 -05001466 if (info_ptr->iccp_name != NULL)
1467 {
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -05001468 memcpy(info_ptr->iccp_name, keyword,
John Bowler921648a2012-03-28 23:36:12 -05001469 keyword_length+1);
1470 info_ptr->iccp_proflen =
1471 profile_length;
1472 info_ptr->iccp_profile = profile;
1473 png_ptr->read_buffer = NULL; /*steal*/
1474 info_ptr->free_me |= PNG_FREE_ICCP;
1475 info_ptr->valid |= PNG_INFO_iCCP;
1476 }
1477
1478 else
John Bowlerb11b31a2012-03-21 07:55:46 -05001479 {
1480 png_ptr->colorspace.flags |=
1481 PNG_COLORSPACE_INVALID;
John Bowler921648a2012-03-28 23:36:12 -05001482 errmsg = "out of memory";
John Bowlerb11b31a2012-03-21 07:55:46 -05001483 }
John Bowlerb11b31a2012-03-21 07:55:46 -05001484 }
1485
John Bowler921648a2012-03-28 23:36:12 -05001486 /* else the profile remains in the read
1487 * buffer which gets reused for subsequent
1488 * chunks.
1489 */
1490
1491 if (info_ptr != NULL)
1492 png_colorspace_sync(png_ptr, info_ptr);
1493
John Bowlerf8dfd122012-10-25 19:30:02 -05001494 if (errmsg == NULL)
John Bowler921648a2012-03-28 23:36:12 -05001495 {
1496 png_ptr->zowner = 0;
1497 return;
1498 }
John Bowlerb11b31a2012-03-21 07:55:46 -05001499 }
1500
John Bowler921648a2012-03-28 23:36:12 -05001501 else if (size > 0)
John Bowlerb11b31a2012-03-21 07:55:46 -05001502 errmsg = "truncated";
John Bowler921648a2012-03-28 23:36:12 -05001503
1504 else
1505 errmsg = png_ptr->zstream.msg;
John Bowlerb11b31a2012-03-21 07:55:46 -05001506 }
1507
1508 /* else png_icc_check_tag_table output an error */
1509 }
1510
1511 else /* profile truncated */
1512 errmsg = png_ptr->zstream.msg;
1513 }
1514
1515 else
1516 errmsg = "out of memory";
1517 }
1518
1519 /* else png_icc_check_header output an error */
1520 }
1521
1522 /* else png_icc_check_length output an error */
1523 }
1524
1525 else /* profile truncated */
1526 errmsg = png_ptr->zstream.msg;
1527
1528 /* Release the stream */
1529 png_ptr->zowner = 0;
1530 }
1531
1532 else /* png_inflate_claim failed */
1533 errmsg = png_ptr->zstream.msg;
1534 }
John Bowler0ae4f7b2012-03-03 21:10:26 -06001535
1536 else
John Bowlerb11b31a2012-03-21 07:55:46 -05001537 errmsg = "bad compression method"; /* or missing */
John Bowler0ae4f7b2012-03-03 21:10:26 -06001538 }
John Bowler0ae4f7b2012-03-03 21:10:26 -06001539
John Bowlerb5d00512012-03-09 09:15:18 -06001540 else
John Bowlerb11b31a2012-03-21 07:55:46 -05001541 errmsg = "bad keyword";
John Bowlerb5d00512012-03-09 09:15:18 -06001542 }
Glenn Randers-Pehrson3744f942012-08-10 19:22:53 -05001543
John Bowlerb11b31a2012-03-21 07:55:46 -05001544 else
1545 errmsg = "too many profiles";
John Bowler0ae4f7b2012-03-03 21:10:26 -06001546
John Bowlerb11b31a2012-03-21 07:55:46 -05001547 /* Failure: the reason is in 'errmsg' */
John Bowler921648a2012-03-28 23:36:12 -05001548 if (!finished)
1549 png_crc_finish(png_ptr, length);
1550
John Bowlerb11b31a2012-03-21 07:55:46 -05001551 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1552 png_colorspace_sync(png_ptr, info_ptr);
1553 if (errmsg != NULL) /* else already output */
1554 png_chunk_benign_error(png_ptr, errmsg);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001555}
1556#endif /* PNG_READ_iCCP_SUPPORTED */
1557
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001558#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001559void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001560png_handle_sPLT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001561/* Note: this does not properly handle chunks that are > 64K under DOS */
1562{
John Bowlerb5d00512012-03-09 09:15:18 -06001563 png_bytep entry_start, buffer;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001564 png_sPLT_t new_palette;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001565 png_sPLT_entryp pp;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001566 png_uint_32 data_length;
1567 int entry_size, i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001568 png_uint_32 skip = 0;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001569 png_uint_32 dl;
1570 png_size_t max_dl;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001571
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001572 png_debug(1, "in png_handle_sPLT");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001573
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001574#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001575 if (png_ptr->user_chunk_cache_max != 0)
1576 {
1577 if (png_ptr->user_chunk_cache_max == 1)
1578 {
1579 png_crc_finish(png_ptr, length);
1580 return;
1581 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001582
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001583 if (--png_ptr->user_chunk_cache_max == 1)
1584 {
1585 png_warning(png_ptr, "No space in chunk cache for sPLT");
1586 png_crc_finish(png_ptr, length);
1587 return;
1588 }
1589 }
1590#endif
1591
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001592 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05001593 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001594
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001595 else if (png_ptr->mode & PNG_HAVE_IDAT)
1596 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001597 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001598 png_chunk_benign_error(png_ptr, "out of place");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001599 return;
1600 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001601
1602#ifdef PNG_MAX_MALLOC_64K
John Bowlerb11b31a2012-03-21 07:55:46 -05001603 if (length > 65535U)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001604 {
John Bowlerb11b31a2012-03-21 07:55:46 -05001605 png_crc_finish(png_ptr, length);
1606 png_chunk_benign_error(png_ptr, "too large to fit in memory");
1607 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001608 }
1609#endif
1610
John Bowlerb11b31a2012-03-21 07:55:46 -05001611 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
1612 if (buffer == NULL)
1613 {
1614 png_crc_finish(png_ptr, length);
1615 png_chunk_benign_error(png_ptr, "out of memory");
1616 return;
1617 }
1618
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001619
1620 /* WARNING: this may break if size_t is less than 32 bits; it is assumed
1621 * that the PNG_MAX_MALLOC_64K test is enabled in this case, but this is a
1622 * potential breakage point if the types in pngconf.h aren't exactly right.
1623 */
John Bowlerb5d00512012-03-09 09:15:18 -06001624 png_crc_read(png_ptr, buffer, length);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001625
1626 if (png_crc_finish(png_ptr, skip))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001627 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001628
John Bowlerb5d00512012-03-09 09:15:18 -06001629 buffer[length] = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001630
John Bowlerb5d00512012-03-09 09:15:18 -06001631 for (entry_start = buffer; *entry_start; entry_start++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001632 /* Empty loop to find end of name */ ;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001633
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001634 ++entry_start;
1635
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001636 /* A sample depth should follow the separator, and we should be on it */
John Bowlerb5d00512012-03-09 09:15:18 -06001637 if (entry_start > buffer + length - 2)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001638 {
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -05001639 png_warning(png_ptr, "malformed sPLT chunk");
1640 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001641 }
1642
1643 new_palette.depth = *entry_start++;
Glenn Randers-Pehrsona565f0e2010-03-06 08:24:45 -06001644 entry_size = (new_palette.depth == 8 ? 6 : 10);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001645 /* This must fit in a png_uint_32 because it is derived from the original
John Bowlerb5d00512012-03-09 09:15:18 -06001646 * chunk data length.
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001647 */
John Bowlerb5d00512012-03-09 09:15:18 -06001648 data_length = length - (png_uint_32)(entry_start - buffer);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001649
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001650 /* Integrity-check the data length */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001651 if (data_length % entry_size)
1652 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001653 png_warning(png_ptr, "sPLT chunk has bad length");
1654 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001655 }
1656
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001657 dl = (png_int_32)(data_length / entry_size);
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05001658 max_dl = PNG_SIZE_MAX / (sizeof (png_sPLT_entry));
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001659
1660 if (dl > max_dl)
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001661 {
1662 png_warning(png_ptr, "sPLT chunk too long");
1663 return;
1664 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001665
1666 new_palette.nentries = (png_int_32)(data_length / entry_size);
1667
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001668 new_palette.entries = (png_sPLT_entryp)png_malloc_warn(
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05001669 png_ptr, new_palette.nentries * (sizeof (png_sPLT_entry)));
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001670
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001671 if (new_palette.entries == NULL)
1672 {
1673 png_warning(png_ptr, "sPLT chunk requires too much memory");
1674 return;
1675 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001676
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001677#ifdef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001678 for (i = 0; i < new_palette.nentries; i++)
1679 {
Glenn Randers-Pehrson90b878c2009-10-07 12:44:35 -05001680 pp = new_palette.entries + i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001681
1682 if (new_palette.depth == 8)
1683 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001684 pp->red = *entry_start++;
1685 pp->green = *entry_start++;
1686 pp->blue = *entry_start++;
1687 pp->alpha = *entry_start++;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001688 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001689
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001690 else
1691 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001692 pp->red = png_get_uint_16(entry_start); entry_start += 2;
1693 pp->green = png_get_uint_16(entry_start); entry_start += 2;
1694 pp->blue = png_get_uint_16(entry_start); entry_start += 2;
1695 pp->alpha = png_get_uint_16(entry_start); entry_start += 2;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001696 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001697
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001698 pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
1699 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001700#else
1701 pp = new_palette.entries;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001702
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001703 for (i = 0; i < new_palette.nentries; i++)
1704 {
1705
1706 if (new_palette.depth == 8)
1707 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001708 pp[i].red = *entry_start++;
1709 pp[i].green = *entry_start++;
1710 pp[i].blue = *entry_start++;
1711 pp[i].alpha = *entry_start++;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001712 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001713
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001714 else
1715 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001716 pp[i].red = png_get_uint_16(entry_start); entry_start += 2;
1717 pp[i].green = png_get_uint_16(entry_start); entry_start += 2;
1718 pp[i].blue = png_get_uint_16(entry_start); entry_start += 2;
1719 pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001720 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001721
Glenn Randers-Pehrsonf27592a2011-03-21 18:05:40 -05001722 pp[i].frequency = png_get_uint_16(entry_start); entry_start += 2;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001723 }
1724#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001725
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001726 /* Discard all chunk data except the name and stash that */
John Bowlerb5d00512012-03-09 09:15:18 -06001727 new_palette.name = (png_charp)buffer;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001728
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001729 png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001730
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001731 png_free(png_ptr, new_palette.entries);
1732}
1733#endif /* PNG_READ_sPLT_SUPPORTED */
1734
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001735#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001736void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001737png_handle_tRNS(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001738{
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001739 png_byte readbuf[PNG_MAX_PALETTE_LENGTH];
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06001740
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001741 png_debug(1, "in png_handle_tRNS");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001742
Guy Schalnate5a37791996-06-05 15:50:50 -05001743 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05001744 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001745
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001746 else if (png_ptr->mode & PNG_HAVE_IDAT)
1747 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001748 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001749 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001750 return;
1751 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001752
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001753 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001754 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001755 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001756 png_chunk_benign_error(png_ptr, "duplicate");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001757 return;
1758 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001759
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001760 if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
Guy Schalnat0d580581995-07-20 02:43:20 -05001761 {
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001762 png_byte buf[2];
Guy Schalnat0d580581995-07-20 02:43:20 -05001763
1764 if (length != 2)
1765 {
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, "invalid");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001768 return;
1769 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001770
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001771 png_crc_read(png_ptr, buf, 2);
1772 png_ptr->num_trans = 1;
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -05001773 png_ptr->trans_color.gray = png_get_uint_16(buf);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001774 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001775
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001776 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
1777 {
1778 png_byte buf[6];
1779
1780 if (length != 6)
1781 {
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001782 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001783 png_chunk_benign_error(png_ptr, "invalid");
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001784 return;
1785 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001786
John Bowler0ae4f7b2012-03-03 21:10:26 -06001787 png_crc_read(png_ptr, buf, length);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001788 png_ptr->num_trans = 1;
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -05001789 png_ptr->trans_color.red = png_get_uint_16(buf);
1790 png_ptr->trans_color.green = png_get_uint_16(buf + 2);
1791 png_ptr->trans_color.blue = png_get_uint_16(buf + 4);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001792 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001793
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001794 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1795 {
1796 if (!(png_ptr->mode & PNG_HAVE_PLTE))
1797 {
John Bowlerb11b31a2012-03-21 07:55:46 -05001798 /* TODO: is this actually an error in the ISO spec? */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001799 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001800 png_chunk_benign_error(png_ptr, "out of place");
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001801 return;
1802 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001803
John Bowlerb11b31a2012-03-21 07:55:46 -05001804 if (length > png_ptr->num_palette || length > PNG_MAX_PALETTE_LENGTH ||
1805 length == 0)
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001806 {
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001807 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001808 png_chunk_benign_error(png_ptr, "invalid");
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001809 return;
1810 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001811
John Bowler0ae4f7b2012-03-03 21:10:26 -06001812 png_crc_read(png_ptr, readbuf, length);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001813 png_ptr->num_trans = (png_uint_16)length;
1814 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001815
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001816 else
Guy Schalnate5a37791996-06-05 15:50:50 -05001817 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001818 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001819 png_chunk_benign_error(png_ptr, "invalid with alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -05001820 return;
1821 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001822
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001823 if (png_crc_finish(png_ptr, 0))
Glenn Randers-Pehrsona7dbcba2007-05-15 16:16:34 -05001824 {
1825 png_ptr->num_trans = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001826 return;
Glenn Randers-Pehrsona7dbcba2007-05-15 16:16:34 -05001827 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001828
John Bowlerb11b31a2012-03-21 07:55:46 -05001829 /* TODO: this is a horrible side effect in the palette case because the
1830 * png_struct ends up with a pointer to the tRNS buffer owned by the
1831 * png_info. Fix this.
1832 */
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06001833 png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001834 &(png_ptr->trans_color));
Guy Schalnat0d580581995-07-20 02:43:20 -05001835}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001836#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001837
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001838#ifdef PNG_READ_bKGD_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001839void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001840png_handle_bKGD(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001841{
John Bowler0ae4f7b2012-03-03 21:10:26 -06001842 unsigned int truelen;
Guy Schalnat0d580581995-07-20 02:43:20 -05001843 png_byte buf[6];
John Bowlercb0b2962011-05-12 21:48:29 -05001844 png_color_16 background;
Guy Schalnat0d580581995-07-20 02:43:20 -05001845
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001846 png_debug(1, "in png_handle_bKGD");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001847
Guy Schalnate5a37791996-06-05 15:50:50 -05001848 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05001849 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001850
John Bowlerb11b31a2012-03-21 07:55:46 -05001851 else if ((png_ptr->mode & PNG_HAVE_IDAT) ||
1852 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
1853 !(png_ptr->mode & PNG_HAVE_PLTE)))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001854 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001855 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001856 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001857 return;
1858 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001859
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001860 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001861 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001862 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001863 png_chunk_benign_error(png_ptr, "duplicate");
Guy Schalnate5a37791996-06-05 15:50:50 -05001864 return;
1865 }
1866
Guy Schalnat0d580581995-07-20 02:43:20 -05001867 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1868 truelen = 1;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001869
Guy Schalnat0d580581995-07-20 02:43:20 -05001870 else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
1871 truelen = 6;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001872
Guy Schalnat0d580581995-07-20 02:43:20 -05001873 else
1874 truelen = 2;
1875
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001876 if (length != truelen)
Guy Schalnat0d580581995-07-20 02:43:20 -05001877 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001878 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001879 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnat0d580581995-07-20 02:43:20 -05001880 return;
1881 }
1882
Andreas Dilger47a0c421997-05-16 02:46:07 -05001883 png_crc_read(png_ptr, buf, truelen);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001884
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001885 if (png_crc_finish(png_ptr, 0))
1886 return;
1887
Guy Schalnate5a37791996-06-05 15:50:50 -05001888 /* We convert the index value into RGB components so that we can allow
1889 * arbitrary RGB values for background when we have transparency, and
1890 * so it is easy to determine the RGB values of the background color
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001891 * from the info_ptr struct.
1892 */
Guy Schalnat0d580581995-07-20 02:43:20 -05001893 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Guy Schalnate5a37791996-06-05 15:50:50 -05001894 {
John Bowlercb0b2962011-05-12 21:48:29 -05001895 background.index = buf[0];
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001896
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001897 if (info_ptr && info_ptr->num_palette)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001898 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001899 if (buf[0] >= info_ptr->num_palette)
1900 {
John Bowlerb11b31a2012-03-21 07:55:46 -05001901 png_chunk_benign_error(png_ptr, "invalid index");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001902 return;
1903 }
1904
John Bowlercb0b2962011-05-12 21:48:29 -05001905 background.red = (png_uint_16)png_ptr->palette[buf[0]].red;
1906 background.green = (png_uint_16)png_ptr->palette[buf[0]].green;
1907 background.blue = (png_uint_16)png_ptr->palette[buf[0]].blue;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001908 }
John Bowlercb0b2962011-05-12 21:48:29 -05001909
1910 else
1911 background.red = background.green = background.blue = 0;
1912
1913 background.gray = 0;
Guy Schalnate5a37791996-06-05 15:50:50 -05001914 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001915
Andreas Dilger47a0c421997-05-16 02:46:07 -05001916 else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) /* GRAY */
Guy Schalnate5a37791996-06-05 15:50:50 -05001917 {
John Bowlercb0b2962011-05-12 21:48:29 -05001918 background.index = 0;
1919 background.red =
1920 background.green =
1921 background.blue =
1922 background.gray = png_get_uint_16(buf);
Guy Schalnate5a37791996-06-05 15:50:50 -05001923 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001924
Guy Schalnat0d580581995-07-20 02:43:20 -05001925 else
1926 {
John Bowlercb0b2962011-05-12 21:48:29 -05001927 background.index = 0;
1928 background.red = png_get_uint_16(buf);
1929 background.green = png_get_uint_16(buf + 2);
1930 background.blue = png_get_uint_16(buf + 4);
1931 background.gray = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -05001932 }
1933
John Bowlercb0b2962011-05-12 21:48:29 -05001934 png_set_bKGD(png_ptr, info_ptr, &background);
Guy Schalnat0d580581995-07-20 02:43:20 -05001935}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001936#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001937
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001938#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001939void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001940png_handle_hIST(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001941{
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001942 unsigned int num, i;
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001943 png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];
Guy Schalnat0d580581995-07-20 02:43:20 -05001944
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001945 png_debug(1, "in png_handle_hIST");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001946
Guy Schalnate5a37791996-06-05 15:50:50 -05001947 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05001948 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001949
John Bowlerb11b31a2012-03-21 07:55:46 -05001950 else if ((png_ptr->mode & PNG_HAVE_IDAT) || !(png_ptr->mode & PNG_HAVE_PLTE))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001951 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001952 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001953 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001954 return;
1955 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001956
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001957 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001958 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001959 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001960 png_chunk_benign_error(png_ptr, "duplicate");
Guy Schalnate5a37791996-06-05 15:50:50 -05001961 return;
1962 }
1963
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001964 num = length / 2 ;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001965
John Bowlerb11b31a2012-03-21 07:55:46 -05001966 if (num != png_ptr->num_palette || num > PNG_MAX_PALETTE_LENGTH)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001967 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001968 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05001969 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001970 return;
1971 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001972
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001973 for (i = 0; i < num; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001974 {
1975 png_byte buf[2];
1976
1977 png_crc_read(png_ptr, buf, 2);
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06001978 readbuf[i] = png_get_uint_16(buf);
Guy Schalnat0d580581995-07-20 02:43:20 -05001979 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001980
1981 if (png_crc_finish(png_ptr, 0))
1982 return;
1983
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06001984 png_set_hIST(png_ptr, info_ptr, readbuf);
Guy Schalnat0d580581995-07-20 02:43:20 -05001985}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001986#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001987
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001988#ifdef PNG_READ_pHYs_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001989void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001990png_handle_pHYs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001991{
1992 png_byte buf[9];
1993 png_uint_32 res_x, res_y;
1994 int unit_type;
1995
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001996 png_debug(1, "in png_handle_pHYs");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001997
Guy Schalnate5a37791996-06-05 15:50:50 -05001998 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05001999 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002000
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002001 else if (png_ptr->mode & PNG_HAVE_IDAT)
2002 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002003 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002004 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002005 return;
2006 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002007
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002008 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002009 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002010 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002011 png_chunk_benign_error(png_ptr, "duplicate");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002012 return;
2013 }
Guy Schalnate5a37791996-06-05 15:50:50 -05002014
Guy Schalnat0d580581995-07-20 02:43:20 -05002015 if (length != 9)
2016 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002017 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002018 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnat0d580581995-07-20 02:43:20 -05002019 return;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002020 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002021
2022 png_crc_read(png_ptr, buf, 9);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002023
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002024 if (png_crc_finish(png_ptr, 0))
2025 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05002026
2027 res_x = png_get_uint_32(buf);
2028 res_y = png_get_uint_32(buf + 4);
2029 unit_type = buf[8];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002030 png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
Guy Schalnat0d580581995-07-20 02:43:20 -05002031}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002032#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002033
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002034#ifdef PNG_READ_oFFs_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002035void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002036png_handle_oFFs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002037{
2038 png_byte buf[9];
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002039 png_int_32 offset_x, offset_y;
Guy Schalnat0d580581995-07-20 02:43:20 -05002040 int unit_type;
2041
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002042 png_debug(1, "in png_handle_oFFs");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002043
Guy Schalnate5a37791996-06-05 15:50:50 -05002044 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05002045 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002046
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002047 else if (png_ptr->mode & PNG_HAVE_IDAT)
2048 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002049 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002050 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002051 return;
2052 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002053
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002054 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002055 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002056 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002057 png_chunk_benign_error(png_ptr, "duplicate");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002058 return;
2059 }
Guy Schalnate5a37791996-06-05 15:50:50 -05002060
Guy Schalnat0d580581995-07-20 02:43:20 -05002061 if (length != 9)
2062 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002063 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002064 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnat0d580581995-07-20 02:43:20 -05002065 return;
2066 }
2067
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002068 png_crc_read(png_ptr, buf, 9);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002069
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002070 if (png_crc_finish(png_ptr, 0))
2071 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05002072
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002073 offset_x = png_get_int_32(buf);
2074 offset_y = png_get_int_32(buf + 4);
Guy Schalnat0d580581995-07-20 02:43:20 -05002075 unit_type = buf[8];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002076 png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
2077}
2078#endif
2079
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002080#ifdef PNG_READ_pCAL_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002081/* Read the pCAL chunk (described in the PNG Extensions document) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002082void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002083png_handle_pCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002084{
Andreas Dilger47a0c421997-05-16 02:46:07 -05002085 png_int_32 X0, X1;
2086 png_byte type, nparams;
John Bowlerb5d00512012-03-09 09:15:18 -06002087 png_bytep buffer, buf, units, endptr;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002088 png_charpp params;
2089 int i;
2090
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002091 png_debug(1, "in png_handle_pCAL");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002092
2093 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05002094 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002095
Andreas Dilger47a0c421997-05-16 02:46:07 -05002096 else if (png_ptr->mode & PNG_HAVE_IDAT)
2097 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002098 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002099 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002100 return;
2101 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002102
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002103 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL))
Andreas Dilger47a0c421997-05-16 02:46:07 -05002104 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002105 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002106 png_chunk_benign_error(png_ptr, "duplicate");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002107 return;
2108 }
2109
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002110 png_debug1(2, "Allocating and reading pCAL chunk data (%u bytes)",
2111 length + 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002112
John Bowlerb11b31a2012-03-21 07:55:46 -05002113 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
John Bowlerb5d00512012-03-09 09:15:18 -06002114
2115 if (buffer == NULL)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002116 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002117 png_crc_finish(png_ptr, length);
2118 png_chunk_benign_error(png_ptr, "out of memory");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002119 return;
2120 }
2121
John Bowlerb5d00512012-03-09 09:15:18 -06002122 png_crc_read(png_ptr, buffer, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002123
2124 if (png_crc_finish(png_ptr, 0))
Andreas Dilger47a0c421997-05-16 02:46:07 -05002125 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002126
John Bowlerb5d00512012-03-09 09:15:18 -06002127 buffer[length] = 0; /* Null terminate the last string */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002128
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002129 png_debug(3, "Finding end of pCAL purpose string");
John Bowlerb5d00512012-03-09 09:15:18 -06002130 for (buf = buffer; *buf; buf++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002131 /* Empty loop */ ;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002132
John Bowlerb5d00512012-03-09 09:15:18 -06002133 endptr = buffer + length;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002134
2135 /* We need to have at least 12 bytes after the purpose string
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002136 * in order to get the parameter information.
2137 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002138 if (endptr <= buf + 12)
2139 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002140 png_chunk_benign_error(png_ptr, "invalid");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002141 return;
2142 }
2143
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002144 png_debug(3, "Reading pCAL X0, X1, type, nparams, and units");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002145 X0 = png_get_int_32((png_bytep)buf+1);
2146 X1 = png_get_int_32((png_bytep)buf+5);
2147 type = buf[9];
2148 nparams = buf[10];
2149 units = buf + 11;
2150
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002151 png_debug(3, "Checking pCAL equation type and number of parameters");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002152 /* Check that we have the right number of parameters for known
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002153 * equation types.
2154 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002155 if ((type == PNG_EQUATION_LINEAR && nparams != 2) ||
2156 (type == PNG_EQUATION_BASE_E && nparams != 3) ||
2157 (type == PNG_EQUATION_ARBITRARY && nparams != 3) ||
2158 (type == PNG_EQUATION_HYPERBOLIC && nparams != 4))
2159 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002160 png_chunk_benign_error(png_ptr, "invalid parameter count");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002161 return;
2162 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002163
Andreas Dilger47a0c421997-05-16 02:46:07 -05002164 else if (type >= PNG_EQUATION_LAST)
2165 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002166 png_chunk_benign_error(png_ptr, "unrecognized equation type");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002167 }
2168
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002169 for (buf = units; *buf; buf++)
Glenn Randers-Pehrsonf9f2fe01998-03-15 18:20:23 -06002170 /* Empty loop to move past the units string. */ ;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002171
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002172 png_debug(3, "Allocating pCAL parameters array");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002173
John Bowlerb5d00512012-03-09 09:15:18 -06002174 params = png_voidcast(png_charpp, png_malloc_warn(png_ptr,
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05002175 nparams * (sizeof (png_charp))));
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002176
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002177 if (params == NULL)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002178 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002179 png_chunk_benign_error(png_ptr, "out of memory");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002180 return;
2181 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002182
2183 /* Get pointers to the start of each parameter string. */
John Bowlerb5d00512012-03-09 09:15:18 -06002184 for (i = 0; i < nparams; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002185 {
2186 buf++; /* Skip the null string terminator from previous parameter. */
2187
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002188 png_debug1(3, "Reading pCAL parameter %d", i);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002189
John Bowlerb5d00512012-03-09 09:15:18 -06002190 for (params[i] = (png_charp)buf; buf <= endptr && *buf != 0; buf++)
Glenn Randers-Pehrsonf9f2fe01998-03-15 18:20:23 -06002191 /* Empty loop to move past each parameter string */ ;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002192
2193 /* Make sure we haven't run out of data yet */
2194 if (buf > endptr)
2195 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002196 png_free(png_ptr, params);
John Bowlerb11b31a2012-03-21 07:55:46 -05002197 png_chunk_benign_error(png_ptr, "invalid data");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002198 return;
2199 }
2200 }
2201
John Bowlerb5d00512012-03-09 09:15:18 -06002202 png_set_pCAL(png_ptr, info_ptr, (png_charp)buffer, X0, X1, type, nparams,
2203 (png_charp)units, params);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002204
Andreas Dilger47a0c421997-05-16 02:46:07 -05002205 png_free(png_ptr, params);
Guy Schalnat0d580581995-07-20 02:43:20 -05002206}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002207#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002208
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002209#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002210/* Read the sCAL chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002211void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002212png_handle_sCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002213{
John Bowlerb5d00512012-03-09 09:15:18 -06002214 png_bytep buffer;
2215 png_size_t i;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002216 int state;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002217
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002218 png_debug(1, "in png_handle_sCAL");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002219
2220 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05002221 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002222
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002223 else if (png_ptr->mode & PNG_HAVE_IDAT)
2224 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002225 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002226 png_chunk_benign_error(png_ptr, "out of place");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002227 return;
2228 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002229
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002230 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002231 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002232 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002233 png_chunk_benign_error(png_ptr, "duplicate");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002234 return;
2235 }
2236
Glenn Randers-Pehrson254a5132011-06-10 17:45:48 -05002237 /* Need unit type, width, \0, height: minimum 4 bytes */
2238 else if (length < 4)
2239 {
Glenn Randers-Pehrson254a5132011-06-10 17:45:48 -05002240 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002241 png_chunk_benign_error(png_ptr, "invalid");
Glenn Randers-Pehrson254a5132011-06-10 17:45:48 -05002242 return;
2243 }
2244
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002245 png_debug1(2, "Allocating and reading sCAL chunk data (%u bytes)",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002246 length + 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002247
John Bowlerb11b31a2012-03-21 07:55:46 -05002248 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002249
John Bowlerb5d00512012-03-09 09:15:18 -06002250 if (buffer == NULL)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002251 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002252 png_chunk_benign_error(png_ptr, "out of memory");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002253 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002254 return;
2255 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002256
John Bowlerb5d00512012-03-09 09:15:18 -06002257 png_crc_read(png_ptr, buffer, length);
2258 buffer[length] = 0; /* Null terminate the last string */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002259
2260 if (png_crc_finish(png_ptr, 0))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002261 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002262
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002263 /* Validate the unit. */
John Bowlerb5d00512012-03-09 09:15:18 -06002264 if (buffer[0] != 1 && buffer[0] != 2)
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -05002265 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002266 png_chunk_benign_error(png_ptr, "invalid unit");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05002267 return;
2268 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002269
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002270 /* Validate the ASCII numbers, need two ASCII numbers separated by
2271 * a '\0' and they need to fit exactly in the chunk data.
2272 */
Glenn Randers-Pehrson254a5132011-06-10 17:45:48 -05002273 i = 1;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002274 state = 0;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002275
John Bowlerb5d00512012-03-09 09:15:18 -06002276 if (!png_check_fp_number((png_const_charp)buffer, length, &state, &i) ||
2277 i >= length || buffer[i++] != 0)
John Bowlerb11b31a2012-03-21 07:55:46 -05002278 png_chunk_benign_error(png_ptr, "bad width format");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002279
John Bowler8d261262011-06-18 13:37:11 -05002280 else if (!PNG_FP_IS_POSITIVE(state))
John Bowlerb11b31a2012-03-21 07:55:46 -05002281 png_chunk_benign_error(png_ptr, "non-positive width");
John Bowler8d261262011-06-18 13:37:11 -05002282
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002283 else
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -06002284 {
John Bowler168a4332011-01-16 19:32:22 -06002285 png_size_t heighti = i;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002286
Glenn Randers-Pehrson254a5132011-06-10 17:45:48 -05002287 state = 0;
John Bowlerb5d00512012-03-09 09:15:18 -06002288 if (!png_check_fp_number((png_const_charp)buffer, length, &state, &i) ||
2289 i != length)
John Bowlerb11b31a2012-03-21 07:55:46 -05002290 png_chunk_benign_error(png_ptr, "bad height format");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002291
John Bowler8d261262011-06-18 13:37:11 -05002292 else if (!PNG_FP_IS_POSITIVE(state))
John Bowlerb11b31a2012-03-21 07:55:46 -05002293 png_chunk_benign_error(png_ptr, "non-positive height");
John Bowler8d261262011-06-18 13:37:11 -05002294
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002295 else
2296 /* This is the (only) success case. */
John Bowlerb5d00512012-03-09 09:15:18 -06002297 png_set_sCAL_s(png_ptr, info_ptr, buffer[0],
2298 (png_charp)buffer+1, (png_charp)buffer+heighti);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002299 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002300}
2301#endif
2302
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002303#ifdef PNG_READ_tIME_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002304void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002305png_handle_tIME(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002306{
2307 png_byte buf[7];
2308 png_time mod_time;
2309
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002310 png_debug(1, "in png_handle_tIME");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002311
Guy Schalnate5a37791996-06-05 15:50:50 -05002312 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05002313 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002314
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002315 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002316 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002317 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002318 png_chunk_benign_error(png_ptr, "duplicate");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002319 return;
2320 }
2321
2322 if (png_ptr->mode & PNG_HAVE_IDAT)
2323 png_ptr->mode |= PNG_AFTER_IDAT;
Guy Schalnate5a37791996-06-05 15:50:50 -05002324
Guy Schalnat0d580581995-07-20 02:43:20 -05002325 if (length != 7)
2326 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002327 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002328 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnat0d580581995-07-20 02:43:20 -05002329 return;
2330 }
2331
2332 png_crc_read(png_ptr, buf, 7);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002333
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002334 if (png_crc_finish(png_ptr, 0))
2335 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05002336
2337 mod_time.second = buf[6];
2338 mod_time.minute = buf[5];
2339 mod_time.hour = buf[4];
2340 mod_time.day = buf[3];
2341 mod_time.month = buf[2];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002342 mod_time.year = png_get_uint_16(buf);
Guy Schalnat0d580581995-07-20 02:43:20 -05002343
Andreas Dilger47a0c421997-05-16 02:46:07 -05002344 png_set_tIME(png_ptr, info_ptr, &mod_time);
Guy Schalnat0d580581995-07-20 02:43:20 -05002345}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002346#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002347
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002348#ifdef PNG_READ_tEXt_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002349/* Note: this does not properly handle chunks that are > 64K under DOS */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002350void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002351png_handle_tEXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002352{
John Bowlerb5d00512012-03-09 09:15:18 -06002353 png_text text_info;
2354 png_bytep buffer;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002355 png_charp key;
Guy Schalnat6d764711995-12-19 03:22:19 -06002356 png_charp text;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002357 png_uint_32 skip = 0;
2358
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002359 png_debug(1, "in png_handle_tEXt");
Guy Schalnat0d580581995-07-20 02:43:20 -05002360
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06002361#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002362 if (png_ptr->user_chunk_cache_max != 0)
2363 {
2364 if (png_ptr->user_chunk_cache_max == 1)
2365 {
2366 png_crc_finish(png_ptr, length);
2367 return;
2368 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002369
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002370 if (--png_ptr->user_chunk_cache_max == 1)
2371 {
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002372 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002373 png_chunk_benign_error(png_ptr, "no space in chunk cache");
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002374 return;
2375 }
2376 }
2377#endif
2378
Guy Schalnate5a37791996-06-05 15:50:50 -05002379 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05002380 png_chunk_error(png_ptr, "missing IHDR");
Guy Schalnate5a37791996-06-05 15:50:50 -05002381
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002382 if (png_ptr->mode & PNG_HAVE_IDAT)
2383 png_ptr->mode |= PNG_AFTER_IDAT;
2384
Andreas Dilger47a0c421997-05-16 02:46:07 -05002385#ifdef PNG_MAX_MALLOC_64K
John Bowlerb11b31a2012-03-21 07:55:46 -05002386 if (length > 65535U)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002387 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002388 png_crc_finish(png_ptr, length);
2389 png_chunk_benign_error(png_ptr, "too large to fit in memory");
2390 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002391 }
2392#endif
2393
John Bowlerb5d00512012-03-09 09:15:18 -06002394 buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
Glenn Randers-Pehrson97a9b482008-10-25 20:03:28 -05002395
John Bowlerb5d00512012-03-09 09:15:18 -06002396 if (buffer == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002397 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002398 png_chunk_benign_error(png_ptr, "out of memory");
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002399 return;
2400 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002401
John Bowlerb5d00512012-03-09 09:15:18 -06002402 png_crc_read(png_ptr, buffer, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002403
2404 if (png_crc_finish(png_ptr, skip))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002405 return;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002406
John Bowlerb5d00512012-03-09 09:15:18 -06002407 key = (png_charp)buffer;
2408 key[length] = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -05002409
2410 for (text = key; *text; text++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002411 /* Empty loop to find end of key */ ;
Guy Schalnat0d580581995-07-20 02:43:20 -05002412
John Bowler0ae4f7b2012-03-03 21:10:26 -06002413 if (text != key + length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002414 text++;
2415
John Bowlerb5d00512012-03-09 09:15:18 -06002416 text_info.compression = PNG_TEXT_COMPRESSION_NONE;
2417 text_info.key = key;
2418 text_info.lang = NULL;
2419 text_info.lang_key = NULL;
2420 text_info.itxt_length = 0;
2421 text_info.text = text;
Glenn Randers-Pehrsonece07cf2012-08-10 18:08:02 -05002422 text_info.text_length = strlen(text);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002423
John Bowlerb5d00512012-03-09 09:15:18 -06002424 if (png_set_text_2(png_ptr, info_ptr, &text_info, 1))
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002425 png_warning(png_ptr, "Insufficient memory to process text chunk");
Guy Schalnat0d580581995-07-20 02:43:20 -05002426}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002427#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002428
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002429#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002430/* Note: this does not correctly handle chunks that are > 64K under DOS */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002431void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002432png_handle_zTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002433{
John Bowler0ae4f7b2012-03-03 21:10:26 -06002434 png_const_charp errmsg = NULL;
John Bowlerb5d00512012-03-09 09:15:18 -06002435 png_bytep buffer;
2436 png_uint_32 keyword_length;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002437
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002438 png_debug(1, "in png_handle_zTXt");
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002439
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06002440#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002441 if (png_ptr->user_chunk_cache_max != 0)
2442 {
2443 if (png_ptr->user_chunk_cache_max == 1)
2444 {
2445 png_crc_finish(png_ptr, length);
2446 return;
2447 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002448
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002449 if (--png_ptr->user_chunk_cache_max == 1)
2450 {
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002451 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002452 png_chunk_benign_error(png_ptr, "no space in chunk cache");
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002453 return;
2454 }
2455 }
2456#endif
2457
Guy Schalnate5a37791996-06-05 15:50:50 -05002458 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05002459 png_chunk_error(png_ptr, "missing IHDR");
Guy Schalnate5a37791996-06-05 15:50:50 -05002460
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002461 if (png_ptr->mode & PNG_HAVE_IDAT)
2462 png_ptr->mode |= PNG_AFTER_IDAT;
2463
John Bowlerb11b31a2012-03-21 07:55:46 -05002464 buffer = png_read_buffer(png_ptr, length, 2/*silent*/);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002465
John Bowlerb5d00512012-03-09 09:15:18 -06002466 if (buffer == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002467 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002468 png_crc_finish(png_ptr, length);
2469 png_chunk_benign_error(png_ptr, "out of memory");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002470 return;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002471 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002472
John Bowlerb5d00512012-03-09 09:15:18 -06002473 png_crc_read(png_ptr, buffer, length);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002474
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002475 if (png_crc_finish(png_ptr, 0))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002476 return;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002477
John Bowler0ae4f7b2012-03-03 21:10:26 -06002478 /* TODO: also check that the keyword contents match the spec! */
2479 for (keyword_length = 0;
John Bowlerb5d00512012-03-09 09:15:18 -06002480 keyword_length < length && buffer[keyword_length] != 0;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002481 ++keyword_length)
2482 /* Empty loop to find end of name */ ;
Guy Schalnat0d580581995-07-20 02:43:20 -05002483
John Bowler0ae4f7b2012-03-03 21:10:26 -06002484 if (keyword_length > 79 || keyword_length < 1)
2485 errmsg = "bad keyword";
Guy Schalnat0d580581995-07-20 02:43:20 -05002486
John Bowler0ae4f7b2012-03-03 21:10:26 -06002487 /* zTXt must have some LZ data after the keyword, although it may expand to
2488 * zero bytes; we need a '\0' at the end of the keyword, the compression type
2489 * then the LZ data:
2490 */
2491 else if (keyword_length + 3 > length)
2492 errmsg = "truncated";
2493
John Bowlerb5d00512012-03-09 09:15:18 -06002494 else if (buffer[keyword_length+1] != PNG_COMPRESSION_TYPE_BASE)
John Bowler0ae4f7b2012-03-03 21:10:26 -06002495 errmsg = "unknown compression type";
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002496
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002497 else
Guy Schalnat0d580581995-07-20 02:43:20 -05002498 {
John Bowler0ae4f7b2012-03-03 21:10:26 -06002499 png_alloc_size_t uncompressed_length = PNG_SIZE_MAX;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002500
John Bowler0ae4f7b2012-03-03 21:10:26 -06002501 /* TODO: at present png_decompress_chunk imposes a single application
2502 * level memory limit, this should be split to different values for iCCP
2503 * and text chunks.
2504 */
John Bowlerb5d00512012-03-09 09:15:18 -06002505 if (png_decompress_chunk(png_ptr, length, keyword_length+2,
2506 &uncompressed_length, 1/*terminate*/) == Z_STREAM_END)
John Bowler0ae4f7b2012-03-03 21:10:26 -06002507 {
2508 png_text text;
2509
John Bowlerb5d00512012-03-09 09:15:18 -06002510 /* It worked; png_ptr->read_buffer now looks like a tEXt chunk except
2511 * for the extra compression type byte and the fact that it isn't
John Bowler0ae4f7b2012-03-03 21:10:26 -06002512 * necessarily '\0' terminated.
2513 */
John Bowlerb5d00512012-03-09 09:15:18 -06002514 buffer = png_ptr->read_buffer;
2515 buffer[uncompressed_length+(keyword_length+2)] = 0;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002516
2517 text.compression = PNG_TEXT_COMPRESSION_zTXt;
John Bowlerb5d00512012-03-09 09:15:18 -06002518 text.key = (png_charp)buffer;
2519 text.text = (png_charp)(buffer + keyword_length+2);
John Bowler0ae4f7b2012-03-03 21:10:26 -06002520 text.text_length = uncompressed_length;
2521 text.itxt_length = 0;
2522 text.lang = NULL;
2523 text.lang_key = NULL;
2524
2525 if (png_set_text_2(png_ptr, info_ptr, &text, 1))
John Bowlerb11b31a2012-03-21 07:55:46 -05002526 errmsg = "insufficient memory";
John Bowler0ae4f7b2012-03-03 21:10:26 -06002527 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002528
John Bowlerb5d00512012-03-09 09:15:18 -06002529 else
2530 errmsg = png_ptr->zstream.msg;
2531 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002532
John Bowler0ae4f7b2012-03-03 21:10:26 -06002533 if (errmsg != NULL)
2534 png_chunk_benign_error(png_ptr, errmsg);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002535}
2536#endif
2537
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002538#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002539/* Note: this does not correctly handle chunks that are > 64K under DOS */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002540void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002541png_handle_iTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002542{
John Bowler0ae4f7b2012-03-03 21:10:26 -06002543 png_const_charp errmsg = NULL;
John Bowlerb5d00512012-03-09 09:15:18 -06002544 png_bytep buffer;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002545 png_uint_32 prefix_length;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002546
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002547 png_debug(1, "in png_handle_iTXt");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002548
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06002549#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002550 if (png_ptr->user_chunk_cache_max != 0)
2551 {
2552 if (png_ptr->user_chunk_cache_max == 1)
2553 {
2554 png_crc_finish(png_ptr, length);
2555 return;
2556 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002557
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002558 if (--png_ptr->user_chunk_cache_max == 1)
2559 {
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002560 png_crc_finish(png_ptr, length);
John Bowlerb11b31a2012-03-21 07:55:46 -05002561 png_chunk_benign_error(png_ptr, "no space in chunk cache");
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002562 return;
2563 }
2564 }
2565#endif
2566
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002567 if (!(png_ptr->mode & PNG_HAVE_IHDR))
John Bowlerb11b31a2012-03-21 07:55:46 -05002568 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002569
2570 if (png_ptr->mode & PNG_HAVE_IDAT)
2571 png_ptr->mode |= PNG_AFTER_IDAT;
2572
John Bowlerb5d00512012-03-09 09:15:18 -06002573 buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002574
John Bowlerb5d00512012-03-09 09:15:18 -06002575 if (buffer == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002576 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002577 png_crc_finish(png_ptr, length);
2578 png_chunk_benign_error(png_ptr, "out of memory");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002579 return;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002580 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002581
John Bowlerb5d00512012-03-09 09:15:18 -06002582 png_crc_read(png_ptr, buffer, length);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002583
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002584 if (png_crc_finish(png_ptr, 0))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002585 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002586
John Bowler0ae4f7b2012-03-03 21:10:26 -06002587 /* First the keyword. */
2588 for (prefix_length=0;
John Bowlerb5d00512012-03-09 09:15:18 -06002589 prefix_length < length && buffer[prefix_length] != 0;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002590 ++prefix_length)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002591 /* Empty loop */ ;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002592
John Bowler0ae4f7b2012-03-03 21:10:26 -06002593 /* Perform a basic check on the keyword length here. */
2594 if (prefix_length > 79 || prefix_length < 1)
2595 errmsg = "bad keyword";
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002596
John Bowler0ae4f7b2012-03-03 21:10:26 -06002597 /* Expect keyword, compression flag, compression type, language, translated
2598 * keyword (both may be empty but are 0 terminated) then the text, which may
2599 * be empty.
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002600 */
John Bowler0ae4f7b2012-03-03 21:10:26 -06002601 else if (prefix_length + 5 > length)
2602 errmsg = "truncated";
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002603
John Bowlerb5d00512012-03-09 09:15:18 -06002604 else if (buffer[prefix_length+1] == 0 ||
2605 (buffer[prefix_length+1] == 1 &&
2606 buffer[prefix_length+2] == PNG_COMPRESSION_TYPE_BASE))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002607 {
John Bowlerb5d00512012-03-09 09:15:18 -06002608 int compressed = buffer[prefix_length+1] != 0;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002609 png_uint_32 language_offset, translated_keyword_offset;
2610 png_alloc_size_t uncompressed_length = 0;
2611
2612 /* Now the language tag */
2613 prefix_length += 3;
2614 language_offset = prefix_length;
2615
John Bowlerb5d00512012-03-09 09:15:18 -06002616 for (; prefix_length < length && buffer[prefix_length] != 0;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002617 ++prefix_length)
2618 /* Empty loop */ ;
2619
2620 /* WARNING: the length may be invalid here, this is checked below. */
2621 translated_keyword_offset = ++prefix_length;
2622
John Bowlerb5d00512012-03-09 09:15:18 -06002623 for (; prefix_length < length && buffer[prefix_length] != 0;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002624 ++prefix_length)
2625 /* Empty loop */ ;
2626
2627 /* prefix_length should now be at the trailing '\0' of the translated
2628 * keyword, but it may already be over the end. None of this arithmetic
2629 * can overflow because chunks are at most 2^31 bytes long, but on 16-bit
2630 * systems the available allocaton may overflow.
2631 */
2632 ++prefix_length;
2633
2634 if (!compressed && prefix_length <= length)
2635 uncompressed_length = length - prefix_length;
2636
2637 else if (compressed && prefix_length < length)
2638 {
2639 uncompressed_length = PNG_SIZE_MAX;
2640
2641 /* TODO: at present png_decompress_chunk imposes a single application
2642 * level memory limit, this should be split to different values for
2643 * iCCP and text chunks.
2644 */
John Bowlerb5d00512012-03-09 09:15:18 -06002645 if (png_decompress_chunk(png_ptr, length, prefix_length,
2646 &uncompressed_length, 1/*terminate*/) == Z_STREAM_END)
2647 buffer = png_ptr->read_buffer;
2648
2649 else
2650 errmsg = png_ptr->zstream.msg;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002651 }
2652
2653 else
2654 errmsg = "truncated";
2655
2656 if (errmsg == NULL)
2657 {
2658 png_text text;
2659
John Bowlerb5d00512012-03-09 09:15:18 -06002660 buffer[uncompressed_length+prefix_length] = 0;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002661
2662 if (compressed)
2663 text.compression = PNG_ITXT_COMPRESSION_NONE;
2664
2665 else
2666 text.compression = PNG_ITXT_COMPRESSION_zTXt;
2667
John Bowlerb5d00512012-03-09 09:15:18 -06002668 text.key = (png_charp)buffer;
2669 text.lang = (png_charp)buffer + language_offset;
2670 text.lang_key = (png_charp)buffer + translated_keyword_offset;
2671 text.text = (png_charp)buffer + prefix_length;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002672 text.text_length = 0;
2673 text.itxt_length = uncompressed_length;
2674
2675 if (png_set_text_2(png_ptr, info_ptr, &text, 1))
2676 errmsg = "insufficient memory";
2677 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002678 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002679
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002680 else
John Bowler0ae4f7b2012-03-03 21:10:26 -06002681 errmsg = "bad compression info";
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002682
John Bowler0ae4f7b2012-03-03 21:10:26 -06002683 if (errmsg != NULL)
2684 png_chunk_benign_error(png_ptr, errmsg);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002685}
2686#endif
2687
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002688#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
John Bowlere9567512012-08-15 22:53:00 -05002689/* Utility function for png_handle_unknown; set up png_ptr::unknown_chunk */
2690static int
2691png_cache_unknown_chunk(png_structrp png_ptr, png_uint_32 length)
2692{
2693 png_alloc_size_t limit = PNG_SIZE_MAX;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002694
John Bowlere9567512012-08-15 22:53:00 -05002695 if (png_ptr->unknown_chunk.data != NULL)
2696 {
2697 png_free(png_ptr, png_ptr->unknown_chunk.data);
2698 png_ptr->unknown_chunk.data = NULL;
2699 }
2700
2701# ifdef PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED
2702 if (png_ptr->user_chunk_malloc_max > 0 &&
2703 png_ptr->user_chunk_malloc_max < limit)
2704 limit = png_ptr->user_chunk_malloc_max;
2705
2706# elif PNG_USER_CHUNK_MALLOC_MAX > 0
2707 if (PNG_USER_CHUNK_MALLOC_MAX < limit)
2708 limit = PNG_USER_CHUNK_MALLOC_MAX;
2709# endif
2710
2711 if (length <= limit)
2712 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002713 PNG_CSTRING_FROM_CHUNK(png_ptr->unknown_chunk.name, png_ptr->chunk_name);
John Bowlere9567512012-08-15 22:53:00 -05002714 /* The following is safe because of the PNG_SIZE_MAX init above */
2715 png_ptr->unknown_chunk.size = (png_size_t)length/*SAFE*/;
2716 /* 'mode' is a flag array, only the bottom four bits matter here */
2717 png_ptr->unknown_chunk.location = (png_byte)png_ptr->mode/*SAFE*/;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002718
2719 if (length == 0)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002720 png_ptr->unknown_chunk.data = NULL;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002721
2722 else
2723 {
John Bowlere9567512012-08-15 22:53:00 -05002724 /* Do a 'warn' here - it is handled below. */
2725 png_ptr->unknown_chunk.data = png_voidcast(png_bytep,
2726 png_malloc_warn(png_ptr, length));
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002727 }
John Bowlere9567512012-08-15 22:53:00 -05002728 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002729
John Bowlere9567512012-08-15 22:53:00 -05002730 if (png_ptr->unknown_chunk.data == NULL && length > 0)
2731 {
2732 /* This is benign because we clean up correctly */
2733 png_crc_finish(png_ptr, length);
2734 png_chunk_benign_error(png_ptr, "unknown chunk exceeds memory limits");
2735 return 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002736 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002737
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002738 else
John Bowlere9567512012-08-15 22:53:00 -05002739 {
2740 if (length > 0)
2741 png_crc_read(png_ptr, png_ptr->unknown_chunk.data, length);
2742 png_crc_finish(png_ptr, 0);
2743 return 1;
2744 }
2745}
2746#endif /* PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002747
John Bowlere9567512012-08-15 22:53:00 -05002748/* Handle an unknown, or known but disabled, chunk */
2749void /* PRIVATE */
2750png_handle_unknown(png_structrp png_ptr, png_inforp info_ptr,
2751 png_uint_32 length, int keep)
2752{
2753 int handled = 0; /* the chunk was handled */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002754
John Bowlere9567512012-08-15 22:53:00 -05002755 png_debug(1, "in png_handle_unknown");
2756
2757#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
2758 /* NOTE: this code is based on the code in libpng-1.4.12 except for fixing
2759 * the bug which meant that setting a non-default behavior for a specific
2760 * chunk would be ignored (the default was always used unless a user
2761 * callback was installed).
2762 *
2763 * 'keep' is the value from the png_chunk_unknown_handling, the setting for
2764 * this specific chunk_name, if PNG_HANDLE_AS_UNKNOWN_SUPPORTED, if not it
2765 * will always be PNG_HANDLE_CHUNK_AS_DEFAULT and it needs to be set here.
2766 * This is just an optimization to avoid multiple calls to the lookup
2767 * function.
2768 */
2769# ifndef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
John Bowler4a6c6df2012-08-16 16:12:13 -05002770# ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
2771 keep = png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name);
2772# endif
John Bowlere9567512012-08-15 22:53:00 -05002773# endif
2774
2775 /* One of the following methods will read the chunk or skip it (at least one
2776 * of these is always defined because this is the only way to switch on
2777 * PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
2778 */
2779# ifdef PNG_READ_USER_CHUNKS_SUPPORTED
2780 /* The user callback takes precedence over the chunk keep value, but the
2781 * keep value is still required to validate a save of a critical chunk.
2782 */
2783 if (png_ptr->read_user_chunk_fn != NULL)
2784 {
2785 if (png_cache_unknown_chunk(png_ptr, length))
2786 {
2787 /* Callback to user unknown chunk handler */
2788 int ret = (*(png_ptr->read_user_chunk_fn))(png_ptr,
2789 &png_ptr->unknown_chunk);
2790
2791 /* ret is:
2792 * negative: An error occured, png_chunk_error will be called.
2793 * zero: The chunk was not handled, the chunk will be discarded
2794 * unless png_set_keep_unknown_chunks has been used to set
2795 * a 'keep' behavior for this particular chunk, in which
2796 * case that will be used. A critical chunk will cause an
2797 * error at this point unless it is to be saved.
2798 * positive: The chunk was handled, libpng will ignore/discard it.
2799 */
2800 if (ret < 0)
2801 png_chunk_error(png_ptr, "error in user chunk");
2802
2803 else if (ret == 0)
2804 {
2805 /* If the keep value is 'default' or 'never' override it, but
2806 * still error out on critical chunks unless the keep value is
2807 * 'always' While this is weird it is the behavior in 1.4.12. A
2808 * possible improvement would be to obey the value set for the
2809 * chunk, but this would be an API change that would probably
2810 * damage some applications.
2811 *
2812 * The png_app_warning below catches the case that matters, where
2813 * the application has neither set specific save for this chunk
2814 * or global save.
2815 */
2816 if (keep < PNG_HANDLE_CHUNK_IF_SAFE)
2817 {
2818# ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
2819 if (png_ptr->unknown_default < PNG_HANDLE_CHUNK_IF_SAFE)
2820 png_app_warning(png_ptr,
2821 "forcing save of an unhandled chunk; please call png_set_keep_unknown_chunks");
2822# endif
2823 keep = PNG_HANDLE_CHUNK_IF_SAFE;
2824 }
2825 }
2826
2827 else /* chunk was handled */
2828 {
2829 handled = 1;
2830 /* Critical chunks can be safely discarded at this point. */
2831 keep = PNG_HANDLE_CHUNK_NEVER;
2832 }
2833 }
2834
2835 else
2836 keep = PNG_HANDLE_CHUNK_NEVER; /* insufficient memory */
2837 }
2838
John Bowlerd0eef282012-08-17 15:30:29 -05002839 else
2840 /* Use the SAVE_UNKNOWN_CHUNKS code or skip the chunk */
John Bowlere9567512012-08-15 22:53:00 -05002841# endif /* PNG_READ_USER_CHUNKS_SUPPORTED */
2842
2843# ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
2844 {
2845 /* keep is currently just the per-chunk setting, if there was no
2846 * setting change it to the global default now (not that this may
2847 * still be AS_DEFAULT) then obtain the cache of the chunk if required,
2848 * if not simply skip the chunk.
2849 */
2850 if (keep == PNG_HANDLE_CHUNK_AS_DEFAULT)
2851 keep = png_ptr->unknown_default;
2852
2853 if (keep == PNG_HANDLE_CHUNK_ALWAYS ||
2854 (keep == PNG_HANDLE_CHUNK_IF_SAFE &&
2855 PNG_CHUNK_ANCILLIARY(png_ptr->chunk_name)))
2856 {
2857 if (!png_cache_unknown_chunk(png_ptr, length))
2858 keep = PNG_HANDLE_CHUNK_NEVER;
2859 }
2860
2861 else
2862 png_crc_finish(png_ptr, length);
2863 }
2864# else
2865# ifndef PNG_READ_USER_CHUNKS_SUPPORTED
2866# error no method to support READ_UNKNOWN_CHUNKS
2867# endif
John Bowlerd0eef282012-08-17 15:30:29 -05002868
2869 {
2870 /* If here there is no read callback pointer set and no support is
2871 * compiled in to just save the unknown chunks, so simply skip this
2872 * chunk. If 'keep' is something other than AS_DEFAULT or NEVER then
2873 * the app has erroneously asked for unknown chunk saving when there
2874 * is no support.
2875 */
2876 if (keep > PNG_HANDLE_CHUNK_NEVER)
2877 png_app_error(png_ptr, "no unknown chunk support available");
2878
2879 png_crc_finish(png_ptr, length);
2880 }
John Bowlere9567512012-08-15 22:53:00 -05002881# endif
2882
2883# ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
2884 /* Now store the chunk in the chunk list if appropriate, and if the limits
2885 * permit it.
2886 */
2887 if (keep == PNG_HANDLE_CHUNK_ALWAYS ||
2888 (keep == PNG_HANDLE_CHUNK_IF_SAFE &&
2889 PNG_CHUNK_ANCILLIARY(png_ptr->chunk_name)))
2890 {
2891# ifdef PNG_USER_LIMITS_SUPPORTED
2892 switch (png_ptr->user_chunk_cache_max)
2893 {
2894 case 2:
2895 png_ptr->user_chunk_cache_max = 1;
2896 png_chunk_benign_error(png_ptr, "no space in chunk cache");
2897 /* FALL THROUGH */
2898 case 1:
2899 /* NOTE: prior to 1.6.0 this case resulted in an unknown critical
2900 * chunk being skipped, now there will be a hard error below.
2901 */
2902 break;
2903
2904 default: /* not at limit */
2905 --(png_ptr->user_chunk_cache_max);
2906 /* FALL THROUGH */
2907 case 0: /* no limit */
2908# endif /* PNG_USER_LIMITS_SUPPORTED */
2909 /* Here when the limit isn't reached or when limits are compiled
2910 * out; store the chunk.
2911 */
2912 png_set_unknown_chunks(png_ptr, info_ptr,
2913 &png_ptr->unknown_chunk, 1);
2914 handled = 1;
2915# ifdef PNG_USER_LIMITS_SUPPORTED
2916 break;
2917 }
2918# endif
2919 }
2920# else /* no store support! */
2921 PNG_UNUSED(info_ptr)
2922# error untested code (reading unknown chunks with no store support)
2923# endif
2924
2925 /* Regardless of the error handling below the cached data (if any) can be
2926 * freed now. Notice that the data is not freed if there is a png_error, but
2927 * it will be freed by destroy_read_struct.
2928 */
2929 if (png_ptr->unknown_chunk.data != NULL)
2930 png_free(png_ptr, png_ptr->unknown_chunk.data);
2931 png_ptr->unknown_chunk.data = NULL;
2932
2933#else /* !PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */
2934 /* There is no support to read an unknown chunk, so just skip it. */
2935 png_crc_finish(png_ptr, length);
2936 PNG_UNUSED(info_ptr)
2937 PNG_UNUSED(keep)
2938#endif /* !PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */
2939
2940 /* Check for unhandled critical chunks */
2941 if (!handled && PNG_CHUNK_CRITICAL(png_ptr->chunk_name))
2942 png_chunk_error(png_ptr, "unhandled critical chunk");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002943}
2944
2945/* This function is called to verify that a chunk name is valid.
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002946 * This function can't have the "critical chunk check" incorporated
2947 * into it, since in the future we will need to be able to call user
2948 * functions to handle unknown critical chunks after we check that
2949 * the chunk name itself is valid.
2950 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002951
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002952/* Bit hacking: the test for an invalid byte in the 4 byte chunk name is:
2953 *
2954 * ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
2955 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002956
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002957void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002958png_check_chunk_name(png_structrp png_ptr, png_uint_32 chunk_name)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002959{
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002960 int i;
2961
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002962 png_debug(1, "in png_check_chunk_name");
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002963
2964 for (i=1; i<=4; ++i)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002965 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002966 int c = chunk_name & 0xff;
2967
2968 if (c < 65 || c > 122 || (c > 90 && c < 97))
2969 png_chunk_error(png_ptr, "invalid chunk type");
2970
2971 chunk_name >>= 8;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002972 }
2973}
2974
John Bowler7875d532011-11-07 22:33:49 -06002975/* Combines the row recently read in with the existing pixels in the row. This
2976 * routine takes care of alpha and transparency if requested. This routine also
2977 * handles the two methods of progressive display of interlaced images,
2978 * depending on the 'display' value; if 'display' is true then the whole row
2979 * (dp) is filled from the start by replicating the available pixels. If
2980 * 'display' is false only those pixels present in the pass are filled in.
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002981 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002982void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002983png_combine_row(png_const_structrp png_ptr, png_bytep dp, int display)
Guy Schalnat0d580581995-07-20 02:43:20 -05002984{
John Bowler4e68aa72011-10-11 16:01:33 -05002985 unsigned int pixel_depth = png_ptr->transformed_pixel_depth;
2986 png_const_bytep sp = png_ptr->row_buf + 1;
John Bowlerac8375d2011-10-06 22:27:16 -05002987 png_uint_32 row_width = png_ptr->width;
John Bowler4e68aa72011-10-11 16:01:33 -05002988 unsigned int pass = png_ptr->pass;
John Bowlerfb5b3ac2011-10-16 22:52:56 -05002989 png_bytep end_ptr = 0;
2990 png_byte end_byte = 0;
2991 unsigned int end_mask;
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002992
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002993 png_debug(1, "in png_combine_row");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002994
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002995 /* Added in 1.5.6: it should not be possible to enter this routine until at
2996 * least one row has been read from the PNG data and transformed.
2997 */
2998 if (pixel_depth == 0)
2999 png_error(png_ptr, "internal row logic error");
3000
3001 /* Added in 1.5.4: the pixel depth should match the information returned by
3002 * any call to png_read_update_info at this point. Do not continue if we got
John Bowler9994f252011-05-15 18:52:39 -05003003 * this wrong.
3004 */
3005 if (png_ptr->info_rowbytes != 0 && png_ptr->info_rowbytes !=
John Bowlerac8375d2011-10-06 22:27:16 -05003006 PNG_ROWBYTES(pixel_depth, row_width))
John Bowler9994f252011-05-15 18:52:39 -05003007 png_error(png_ptr, "internal row size calculation error");
3008
John Bowlerac8375d2011-10-06 22:27:16 -05003009 /* Don't expect this to ever happen: */
3010 if (row_width == 0)
3011 png_error(png_ptr, "internal row width error");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003012
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003013 /* Preserve the last byte in cases where only part of it will be overwritten,
3014 * the multiply below may overflow, we don't care because ANSI-C guarantees
3015 * we get the low bits.
3016 */
3017 end_mask = (pixel_depth * row_width) & 7;
3018 if (end_mask != 0)
3019 {
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003020 /* end_ptr == NULL is a flag to say do nothing */
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003021 end_ptr = dp + PNG_ROWBYTES(pixel_depth, row_width) - 1;
3022 end_byte = *end_ptr;
3023# ifdef PNG_READ_PACKSWAP_SUPPORTED
3024 if (png_ptr->transformations & PNG_PACKSWAP) /* little-endian byte */
3025 end_mask = 0xff << end_mask;
3026
3027 else /* big-endian byte */
3028# endif
3029 end_mask = 0xff >> end_mask;
3030 /* end_mask is now the bits to *keep* from the destination row */
3031 }
3032
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -05003033 /* For non-interlaced images this reduces to a memcpy(). A memcpy()
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003034 * will also happen if interlacing isn't supported or if the application
3035 * does not call png_set_interlace_handling(). In the latter cases the
3036 * caller just gets a sequence of the unexpanded rows from each interlace
3037 * pass.
John Bowlerac8375d2011-10-06 22:27:16 -05003038 */
3039#ifdef PNG_READ_INTERLACING_SUPPORTED
3040 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE) &&
John Bowler4e68aa72011-10-11 16:01:33 -05003041 pass < 6 && (display == 0 ||
3042 /* The following copies everything for 'display' on passes 0, 2 and 4. */
3043 (display == 1 && (pass & 1) != 0)))
Guy Schalnat0d580581995-07-20 02:43:20 -05003044 {
John Bowler4e68aa72011-10-11 16:01:33 -05003045 /* Narrow images may have no bits in a pass; the caller should handle
3046 * this, but this test is cheap:
John Bowlerac8375d2011-10-06 22:27:16 -05003047 */
John Bowler4e68aa72011-10-11 16:01:33 -05003048 if (row_width <= PNG_PASS_START_COL(pass))
3049 return;
John Bowlerac8375d2011-10-06 22:27:16 -05003050
John Bowler4e68aa72011-10-11 16:01:33 -05003051 if (pixel_depth < 8)
Guy Schalnat0d580581995-07-20 02:43:20 -05003052 {
John Bowler7875d532011-11-07 22:33:49 -06003053 /* For pixel depths up to 4 bpp the 8-pixel mask can be expanded to fit
John Bowler4e68aa72011-10-11 16:01:33 -05003054 * into 32 bits, then a single loop over the bytes using the four byte
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003055 * values in the 32-bit mask can be used. For the 'display' option the
John Bowler4e68aa72011-10-11 16:01:33 -05003056 * expanded mask may also not require any masking within a byte. To
3057 * make this work the PACKSWAP option must be taken into account - it
3058 * simply requires the pixels to be reversed in each byte.
3059 *
3060 * The 'regular' case requires a mask for each of the first 6 passes,
3061 * the 'display' case does a copy for the even passes in the range
John Bowler7875d532011-11-07 22:33:49 -06003062 * 0..6. This has already been handled in the test above.
John Bowler4e68aa72011-10-11 16:01:33 -05003063 *
3064 * The masks are arranged as four bytes with the first byte to use in
3065 * the lowest bits (little-endian) regardless of the order (PACKSWAP or
3066 * not) of the pixels in each byte.
3067 *
3068 * NOTE: the whole of this logic depends on the caller of this function
3069 * only calling it on rows appropriate to the pass. This function only
John Bowler7875d532011-11-07 22:33:49 -06003070 * understands the 'x' logic; the 'y' logic is handled by the caller.
John Bowler4e68aa72011-10-11 16:01:33 -05003071 *
3072 * The following defines allow generation of compile time constant bit
3073 * masks for each pixel depth and each possibility of swapped or not
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003074 * swapped bytes. Pass 'p' is in the range 0..6; 'x', a pixel index,
3075 * is in the range 0..7; and the result is 1 if the pixel is to be
3076 * copied in the pass, 0 if not. 'S' is for the sparkle method, 'B'
3077 * for the block method.
3078 *
John Bowler92ef3132011-10-27 19:36:08 -05003079 * With some compilers a compile time expression of the general form:
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05003080 *
John Bowler92ef3132011-10-27 19:36:08 -05003081 * (shift >= 32) ? (a >> (shift-32)) : (b >> shift)
3082 *
3083 * Produces warnings with values of 'shift' in the range 33 to 63
Glenn Randers-Pehrson18763662011-10-27 22:09:22 -05003084 * because the right hand side of the ?: expression is evaluated by
John Bowler92ef3132011-10-27 19:36:08 -05003085 * the compiler even though it isn't used. Microsoft Visual C (various
3086 * versions) and the Intel C compiler are known to do this. To avoid
3087 * this the following macros are used in 1.5.6. This is a temporary
John Bowler7875d532011-11-07 22:33:49 -06003088 * solution to avoid destabilizing the code during the release process.
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05003089 */
John Bowler92ef3132011-10-27 19:36:08 -05003090# if PNG_USE_COMPILE_TIME_MASKS
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05003091# define PNG_LSR(x,s) ((x)>>((s) & 0x1f))
3092# define PNG_LSL(x,s) ((x)<<((s) & 0x1f))
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003093# else
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05003094# define PNG_LSR(x,s) ((x)>>(s))
3095# define PNG_LSL(x,s) ((x)<<(s))
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003096# endif
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05003097# define S_COPY(p,x) (((p)<4 ? PNG_LSR(0x80088822,(3-(p))*8+(7-(x))) :\
3098 PNG_LSR(0xaa55ff00,(7-(p))*8+(7-(x)))) & 1)
3099# define B_COPY(p,x) (((p)<4 ? PNG_LSR(0xff0fff33,(3-(p))*8+(7-(x))) :\
3100 PNG_LSR(0xff55ff00,(7-(p))*8+(7-(x)))) & 1)
Guy Schalnat0d580581995-07-20 02:43:20 -05003101
John Bowler4e68aa72011-10-11 16:01:33 -05003102 /* Return a mask for pass 'p' pixel 'x' at depth 'd'. The mask is
3103 * little endian - the first pixel is at bit 0 - however the extra
3104 * parameter 's' can be set to cause the mask position to be swapped
3105 * within each byte, to match the PNG format. This is done by XOR of
3106 * the shift with 7, 6 or 4 for bit depths 1, 2 and 4.
3107 */
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05003108# define PIXEL_MASK(p,x,d,s) \
3109 (PNG_LSL(((PNG_LSL(1U,(d)))-1),(((x)*(d))^((s)?8-(d):0))))
John Bowlerac8375d2011-10-06 22:27:16 -05003110
John Bowler4e68aa72011-10-11 16:01:33 -05003111 /* Hence generate the appropriate 'block' or 'sparkle' pixel copy mask.
3112 */
3113# define S_MASKx(p,x,d,s) (S_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
3114# 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 -05003115
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003116 /* Combine 8 of these to get the full mask. For the 1-bpp and 2-bpp
3117 * cases the result needs replicating, for the 4-bpp case the above
3118 * generates a full 32 bits.
John Bowler4e68aa72011-10-11 16:01:33 -05003119 */
3120# define MASK_EXPAND(m,d) ((m)*((d)==1?0x01010101:((d)==2?0x00010001:1)))
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003121
John Bowler4e68aa72011-10-11 16:01:33 -05003122# define S_MASK(p,d,s) MASK_EXPAND(S_MASKx(p,0,d,s) + S_MASKx(p,1,d,s) +\
3123 S_MASKx(p,2,d,s) + S_MASKx(p,3,d,s) + S_MASKx(p,4,d,s) +\
3124 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 -06003125
John Bowler4e68aa72011-10-11 16:01:33 -05003126# define B_MASK(p,d,s) MASK_EXPAND(B_MASKx(p,0,d,s) + B_MASKx(p,1,d,s) +\
3127 B_MASKx(p,2,d,s) + B_MASKx(p,3,d,s) + B_MASKx(p,4,d,s) +\
3128 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 -05003129
John Bowler4e68aa72011-10-11 16:01:33 -05003130#if PNG_USE_COMPILE_TIME_MASKS
3131 /* Utility macros to construct all the masks for a depth/swap
3132 * combination. The 's' parameter says whether the format is PNG
John Bowler7875d532011-11-07 22:33:49 -06003133 * (big endian bytes) or not. Only the three odd-numbered passes are
John Bowler4e68aa72011-10-11 16:01:33 -05003134 * required for the display/block algorithm.
3135 */
3136# define S_MASKS(d,s) { S_MASK(0,d,s), S_MASK(1,d,s), S_MASK(2,d,s),\
3137 S_MASK(3,d,s), S_MASK(4,d,s), S_MASK(5,d,s) }
John Bowlerac8375d2011-10-06 22:27:16 -05003138
John Bowler4e68aa72011-10-11 16:01:33 -05003139# 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 -05003140
John Bowler4e68aa72011-10-11 16:01:33 -05003141# define DEPTH_INDEX(d) ((d)==1?0:((d)==2?1:2))
John Bowlerac8375d2011-10-06 22:27:16 -05003142
John Bowler4e68aa72011-10-11 16:01:33 -05003143 /* Hence the pre-compiled masks indexed by PACKSWAP (or not), depth and
3144 * then pass:
3145 */
John Bowler7875d532011-11-07 22:33:49 -06003146 static PNG_CONST png_uint_32 row_mask[2/*PACKSWAP*/][3/*depth*/][6] =
3147 {
John Bowler4e68aa72011-10-11 16:01:33 -05003148 /* Little-endian byte masks for PACKSWAP */
3149 { S_MASKS(1,0), S_MASKS(2,0), S_MASKS(4,0) },
3150 /* Normal (big-endian byte) masks - PNG format */
3151 { S_MASKS(1,1), S_MASKS(2,1), S_MASKS(4,1) }
3152 };
John Bowlerac8375d2011-10-06 22:27:16 -05003153
John Bowler4e68aa72011-10-11 16:01:33 -05003154 /* display_mask has only three entries for the odd passes, so index by
3155 * pass>>1.
3156 */
John Bowler7875d532011-11-07 22:33:49 -06003157 static PNG_CONST png_uint_32 display_mask[2][3][3] =
3158 {
John Bowler4e68aa72011-10-11 16:01:33 -05003159 /* Little-endian byte masks for PACKSWAP */
3160 { B_MASKS(1,0), B_MASKS(2,0), B_MASKS(4,0) },
3161 /* Normal (big-endian byte) masks - PNG format */
3162 { B_MASKS(1,1), B_MASKS(2,1), B_MASKS(4,1) }
3163 };
John Bowlerac8375d2011-10-06 22:27:16 -05003164
John Bowler4e68aa72011-10-11 16:01:33 -05003165# define MASK(pass,depth,display,png)\
3166 ((display)?display_mask[png][DEPTH_INDEX(depth)][pass>>1]:\
3167 row_mask[png][DEPTH_INDEX(depth)][pass])
John Bowlerac8375d2011-10-06 22:27:16 -05003168
John Bowler4e68aa72011-10-11 16:01:33 -05003169#else /* !PNG_USE_COMPILE_TIME_MASKS */
3170 /* This is the runtime alternative: it seems unlikely that this will
3171 * ever be either smaller or faster than the compile time approach.
3172 */
3173# define MASK(pass,depth,display,png)\
3174 ((display)?B_MASK(pass,depth,png):S_MASK(pass,depth,png))
3175#endif /* !PNG_USE_COMPILE_TIME_MASKS */
John Bowlerac8375d2011-10-06 22:27:16 -05003176
John Bowler4e68aa72011-10-11 16:01:33 -05003177 /* Use the appropriate mask to copy the required bits. In some cases
3178 * the byte mask will be 0 or 0xff, optimize these cases. row_width is
3179 * the number of pixels, but the code copies bytes, so it is necessary
3180 * to special case the end.
3181 */
3182 png_uint_32 pixels_per_byte = 8 / pixel_depth;
3183 png_uint_32 mask;
John Bowlerac8375d2011-10-06 22:27:16 -05003184
John Bowler4e68aa72011-10-11 16:01:33 -05003185# ifdef PNG_READ_PACKSWAP_SUPPORTED
3186 if (png_ptr->transformations & PNG_PACKSWAP)
3187 mask = MASK(pass, pixel_depth, display, 0);
John Bowlerac8375d2011-10-06 22:27:16 -05003188
3189 else
John Bowler4e68aa72011-10-11 16:01:33 -05003190# endif
3191 mask = MASK(pass, pixel_depth, display, 1);
3192
3193 for (;;)
3194 {
3195 png_uint_32 m;
3196
3197 /* It doesn't matter in the following if png_uint_32 has more than
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003198 * 32 bits because the high bits always match those in m<<24; it is,
John Bowler4e68aa72011-10-11 16:01:33 -05003199 * however, essential to use OR here, not +, because of this.
3200 */
3201 m = mask;
3202 mask = (m >> 8) | (m << 24); /* rotate right to good compilers */
3203 m &= 0xff;
3204
3205 if (m != 0) /* something to copy */
John Bowlerac8375d2011-10-06 22:27:16 -05003206 {
John Bowler4e68aa72011-10-11 16:01:33 -05003207 if (m != 0xff)
3208 *dp = (png_byte)((*dp & ~m) | (*sp & m));
3209 else
3210 *dp = *sp;
John Bowlerac8375d2011-10-06 22:27:16 -05003211 }
John Bowler4e68aa72011-10-11 16:01:33 -05003212
3213 /* NOTE: this may overwrite the last byte with garbage if the image
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003214 * is not an exact number of bytes wide; libpng has always done
John Bowler4e68aa72011-10-11 16:01:33 -05003215 * this.
3216 */
3217 if (row_width <= pixels_per_byte)
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003218 break; /* May need to restore part of the last byte */
John Bowler4e68aa72011-10-11 16:01:33 -05003219
3220 row_width -= pixels_per_byte;
3221 ++dp;
3222 ++sp;
3223 }
3224 }
3225
3226 else /* pixel_depth >= 8 */
3227 {
3228 unsigned int bytes_to_copy, bytes_to_jump;
3229
3230 /* Validate the depth - it must be a multiple of 8 */
3231 if (pixel_depth & 7)
3232 png_error(png_ptr, "invalid user transform pixel depth");
3233
3234 pixel_depth >>= 3; /* now in bytes */
3235 row_width *= pixel_depth;
3236
3237 /* Regardless of pass number the Adam 7 interlace always results in a
3238 * fixed number of pixels to copy then to skip. There may be a
3239 * different number of pixels to skip at the start though.
3240 */
3241 {
3242 unsigned int offset = PNG_PASS_START_COL(pass) * pixel_depth;
3243
3244 row_width -= offset;
3245 dp += offset;
3246 sp += offset;
John Bowlerac8375d2011-10-06 22:27:16 -05003247 }
3248
John Bowler4e68aa72011-10-11 16:01:33 -05003249 /* Work out the bytes to copy. */
3250 if (display)
3251 {
3252 /* When doing the 'block' algorithm the pixel in the pass gets
3253 * replicated to adjacent pixels. This is why the even (0,2,4,6)
3254 * passes are skipped above - the entire expanded row is copied.
3255 */
3256 bytes_to_copy = (1<<((6-pass)>>1)) * pixel_depth;
3257
3258 /* But don't allow this number to exceed the actual row width. */
3259 if (bytes_to_copy > row_width)
3260 bytes_to_copy = row_width;
3261 }
3262
3263 else /* normal row; Adam7 only ever gives us one pixel to copy. */
3264 bytes_to_copy = pixel_depth;
3265
3266 /* In Adam7 there is a constant offset between where the pixels go. */
3267 bytes_to_jump = PNG_PASS_COL_OFFSET(pass) * pixel_depth;
3268
3269 /* And simply copy these bytes. Some optimization is possible here,
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003270 * depending on the value of 'bytes_to_copy'. Special case the low
John Bowler4e68aa72011-10-11 16:01:33 -05003271 * byte counts, which we know to be frequent.
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003272 *
3273 * Notice that these cases all 'return' rather than 'break' - this
3274 * avoids an unnecessary test on whether to restore the last byte
3275 * below.
John Bowler4e68aa72011-10-11 16:01:33 -05003276 */
3277 switch (bytes_to_copy)
3278 {
3279 case 1:
3280 for (;;)
3281 {
3282 *dp = *sp;
3283
3284 if (row_width <= bytes_to_jump)
3285 return;
3286
3287 dp += bytes_to_jump;
3288 sp += bytes_to_jump;
3289 row_width -= bytes_to_jump;
3290 }
3291
3292 case 2:
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003293 /* There is a possibility of a partial copy at the end here; this
John Bowler4e68aa72011-10-11 16:01:33 -05003294 * slows the code down somewhat.
3295 */
3296 do
3297 {
3298 dp[0] = sp[0], dp[1] = sp[1];
3299
3300 if (row_width <= bytes_to_jump)
3301 return;
3302
3303 sp += bytes_to_jump;
3304 dp += bytes_to_jump;
3305 row_width -= bytes_to_jump;
3306 }
3307 while (row_width > 1);
3308
3309 /* And there can only be one byte left at this point: */
3310 *dp = *sp;
3311 return;
3312
3313 case 3:
3314 /* This can only be the RGB case, so each copy is exactly one
3315 * pixel and it is not necessary to check for a partial copy.
3316 */
3317 for(;;)
3318 {
3319 dp[0] = sp[0], dp[1] = sp[1], dp[2] = sp[2];
3320
3321 if (row_width <= bytes_to_jump)
3322 return;
3323
3324 sp += bytes_to_jump;
3325 dp += bytes_to_jump;
3326 row_width -= bytes_to_jump;
3327 }
3328
3329 default:
3330#if PNG_ALIGN_TYPE != PNG_ALIGN_NONE
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003331 /* Check for double byte alignment and, if possible, use a
3332 * 16-bit copy. Don't attempt this for narrow images - ones that
John Bowler4e68aa72011-10-11 16:01:33 -05003333 * are less than an interlace panel wide. Don't attempt it for
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -05003334 * wide bytes_to_copy either - use the memcpy there.
John Bowler4e68aa72011-10-11 16:01:33 -05003335 */
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -05003336 if (bytes_to_copy < 16 /*else use memcpy*/ &&
John Bowler4e68aa72011-10-11 16:01:33 -05003337 png_isaligned(dp, png_uint_16) &&
3338 png_isaligned(sp, png_uint_16) &&
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05003339 bytes_to_copy % (sizeof (png_uint_16)) == 0 &&
3340 bytes_to_jump % (sizeof (png_uint_16)) == 0)
John Bowler4e68aa72011-10-11 16:01:33 -05003341 {
3342 /* Everything is aligned for png_uint_16 copies, but try for
3343 * png_uint_32 first.
3344 */
3345 if (png_isaligned(dp, png_uint_32) &&
3346 png_isaligned(sp, png_uint_32) &&
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05003347 bytes_to_copy % (sizeof (png_uint_32)) == 0 &&
3348 bytes_to_jump % (sizeof (png_uint_32)) == 0)
John Bowler4e68aa72011-10-11 16:01:33 -05003349 {
John Bowlerb4541692012-04-30 06:31:54 -05003350 png_uint_32p dp32 = png_aligncast(png_uint_32p,dp);
3351 png_const_uint_32p sp32 = png_aligncastconst(
3352 png_const_uint_32p, sp);
John Bowler4e68aa72011-10-11 16:01:33 -05003353 unsigned int skip = (bytes_to_jump-bytes_to_copy) /
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05003354 (sizeof (png_uint_32));
John Bowler4e68aa72011-10-11 16:01:33 -05003355
3356 do
3357 {
3358 size_t c = bytes_to_copy;
3359 do
3360 {
3361 *dp32++ = *sp32++;
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05003362 c -= (sizeof (png_uint_32));
John Bowler4e68aa72011-10-11 16:01:33 -05003363 }
3364 while (c > 0);
3365
3366 if (row_width <= bytes_to_jump)
3367 return;
3368
3369 dp32 += skip;
3370 sp32 += skip;
3371 row_width -= bytes_to_jump;
3372 }
3373 while (bytes_to_copy <= row_width);
3374
3375 /* Get to here when the row_width truncates the final copy.
3376 * There will be 1-3 bytes left to copy, so don't try the
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003377 * 16-bit loop below.
John Bowler4e68aa72011-10-11 16:01:33 -05003378 */
3379 dp = (png_bytep)dp32;
3380 sp = (png_const_bytep)sp32;
3381 do
3382 *dp++ = *sp++;
3383 while (--row_width > 0);
3384 return;
3385 }
3386
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003387 /* Else do it in 16-bit quantities, but only if the size is
John Bowler4e68aa72011-10-11 16:01:33 -05003388 * not too large.
3389 */
3390 else
3391 {
John Bowlerb4541692012-04-30 06:31:54 -05003392 png_uint_16p dp16 = png_aligncast(png_uint_16p, dp);
3393 png_const_uint_16p sp16 = png_aligncastconst(
3394 png_const_uint_16p, sp);
John Bowler4e68aa72011-10-11 16:01:33 -05003395 unsigned int skip = (bytes_to_jump-bytes_to_copy) /
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05003396 (sizeof (png_uint_16));
John Bowler4e68aa72011-10-11 16:01:33 -05003397
3398 do
3399 {
3400 size_t c = bytes_to_copy;
3401 do
3402 {
3403 *dp16++ = *sp16++;
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05003404 c -= (sizeof (png_uint_16));
John Bowler4e68aa72011-10-11 16:01:33 -05003405 }
3406 while (c > 0);
3407
3408 if (row_width <= bytes_to_jump)
3409 return;
3410
3411 dp16 += skip;
3412 sp16 += skip;
3413 row_width -= bytes_to_jump;
3414 }
3415 while (bytes_to_copy <= row_width);
3416
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003417 /* End of row - 1 byte left, bytes_to_copy > row_width: */
John Bowler4e68aa72011-10-11 16:01:33 -05003418 dp = (png_bytep)dp16;
3419 sp = (png_const_bytep)sp16;
3420 do
3421 *dp++ = *sp++;
3422 while (--row_width > 0);
3423 return;
3424 }
3425 }
3426#endif /* PNG_ALIGN_ code */
3427
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -05003428 /* The true default - use a memcpy: */
John Bowler4e68aa72011-10-11 16:01:33 -05003429 for (;;)
3430 {
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -05003431 memcpy(dp, sp, bytes_to_copy);
John Bowler4e68aa72011-10-11 16:01:33 -05003432
3433 if (row_width <= bytes_to_jump)
3434 return;
3435
3436 sp += bytes_to_jump;
3437 dp += bytes_to_jump;
3438 row_width -= bytes_to_jump;
3439 if (bytes_to_copy > row_width)
3440 bytes_to_copy = row_width;
3441 }
3442 }
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003443
3444 /* NOT REACHED*/
John Bowler4e68aa72011-10-11 16:01:33 -05003445 } /* pixel_depth >= 8 */
3446
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003447 /* Here if pixel_depth < 8 to check 'end_ptr' below. */
Guy Schalnat0d580581995-07-20 02:43:20 -05003448 }
John Bowler4e68aa72011-10-11 16:01:33 -05003449 else
John Bowlerac8375d2011-10-06 22:27:16 -05003450#endif
3451
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -05003452 /* If here then the switch above wasn't used so just memcpy the whole row
John Bowler4e68aa72011-10-11 16:01:33 -05003453 * from the temporary row buffer (notice that this overwrites the end of the
3454 * destination row if it is a partial byte.)
John Bowlerac8375d2011-10-06 22:27:16 -05003455 */
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -05003456 memcpy(dp, sp, PNG_ROWBYTES(pixel_depth, row_width));
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003457
3458 /* Restore the overwritten bits from the last byte if necessary. */
3459 if (end_ptr != NULL)
3460 *end_ptr = (png_byte)((end_byte & end_mask) | (*end_ptr & ~end_mask));
Guy Schalnat0d580581995-07-20 02:43:20 -05003461}
3462
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06003463#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05003464void /* PRIVATE */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003465png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
3466 png_uint_32 transformations /* Because these may affect the byte layout */)
Guy Schalnat0d580581995-07-20 02:43:20 -05003467{
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003468 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
3469 /* Offset to next interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003470 static PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003471
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05003472 png_debug(1, "in png_do_read_interlace");
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003473 if (row != NULL && row_info != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -05003474 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003475 png_uint_32 final_width;
3476
3477 final_width = row_info->width * png_pass_inc[pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05003478
3479 switch (row_info->pixel_depth)
3480 {
3481 case 1:
3482 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003483 png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3);
3484 png_bytep dp = row + (png_size_t)((final_width - 1) >> 3);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003485 int sshift, dshift;
3486 int s_start, s_end, s_inc;
3487 int jstop = png_pass_inc[pass];
3488 png_byte v;
Guy Schalnat0d580581995-07-20 02:43:20 -05003489 png_uint_32 i;
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003490 int j;
Guy Schalnat0d580581995-07-20 02:43:20 -05003491
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003492#ifdef PNG_READ_PACKSWAP_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003493 if (transformations & PNG_PACKSWAP)
3494 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003495 sshift = (int)((row_info->width + 7) & 0x07);
3496 dshift = (int)((final_width + 7) & 0x07);
3497 s_start = 7;
3498 s_end = 0;
3499 s_inc = -1;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003500 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003501
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003502 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05003503#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003504 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003505 sshift = 7 - (int)((row_info->width + 7) & 0x07);
3506 dshift = 7 - (int)((final_width + 7) & 0x07);
3507 s_start = 0;
3508 s_end = 7;
3509 s_inc = 1;
3510 }
Glenn Randers-Pehrson5dd2b8e2004-11-24 07:50:16 -06003511
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003512 for (i = 0; i < row_info->width; i++)
3513 {
3514 v = (png_byte)((*sp >> sshift) & 0x01);
3515 for (j = 0; j < jstop; j++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003516 {
John Bowler8fb6c6a2012-01-25 07:47:44 -06003517 unsigned int tmp = *dp & (0x7f7f >> (7 - dshift));
3518 tmp |= v << dshift;
3519 *dp = (png_byte)(tmp & 0xff);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003520
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003521 if (dshift == s_end)
3522 {
3523 dshift = s_start;
3524 dp--;
3525 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003526
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003527 else
3528 dshift += s_inc;
3529 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003530
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003531 if (sshift == s_end)
3532 {
3533 sshift = s_start;
Guy Schalnat0d580581995-07-20 02:43:20 -05003534 sp--;
3535 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003536
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003537 else
3538 sshift += s_inc;
Guy Schalnat0d580581995-07-20 02:43:20 -05003539 }
3540 break;
3541 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003542
Guy Schalnat0d580581995-07-20 02:43:20 -05003543 case 2:
3544 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003545 png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);
3546 png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);
3547 int sshift, dshift;
3548 int s_start, s_end, s_inc;
3549 int jstop = png_pass_inc[pass];
Andreas Dilger47a0c421997-05-16 02:46:07 -05003550 png_uint_32 i;
Guy Schalnat0d580581995-07-20 02:43:20 -05003551
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003552#ifdef PNG_READ_PACKSWAP_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003553 if (transformations & PNG_PACKSWAP)
3554 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003555 sshift = (int)(((row_info->width + 3) & 0x03) << 1);
3556 dshift = (int)(((final_width + 3) & 0x03) << 1);
3557 s_start = 6;
3558 s_end = 0;
3559 s_inc = -2;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003560 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003561
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003562 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05003563#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003564 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003565 sshift = (int)((3 - ((row_info->width + 3) & 0x03)) << 1);
3566 dshift = (int)((3 - ((final_width + 3) & 0x03)) << 1);
3567 s_start = 0;
3568 s_end = 6;
3569 s_inc = 2;
3570 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05003571
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003572 for (i = 0; i < row_info->width; i++)
3573 {
3574 png_byte v;
3575 int j;
3576
3577 v = (png_byte)((*sp >> sshift) & 0x03);
3578 for (j = 0; j < jstop; j++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003579 {
John Bowler8fb6c6a2012-01-25 07:47:44 -06003580 unsigned int tmp = *dp & (0x3f3f >> (6 - dshift));
3581 tmp |= v << dshift;
3582 *dp = (png_byte)(tmp & 0xff);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003583
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003584 if (dshift == s_end)
3585 {
3586 dshift = s_start;
3587 dp--;
3588 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003589
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003590 else
3591 dshift += s_inc;
3592 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003593
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003594 if (sshift == s_end)
3595 {
3596 sshift = s_start;
Guy Schalnat0d580581995-07-20 02:43:20 -05003597 sp--;
3598 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003599
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003600 else
3601 sshift += s_inc;
Guy Schalnat0d580581995-07-20 02:43:20 -05003602 }
3603 break;
3604 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003605
Guy Schalnat0d580581995-07-20 02:43:20 -05003606 case 4:
3607 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003608 png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1);
3609 png_bytep dp = row + (png_size_t)((final_width - 1) >> 1);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003610 int sshift, dshift;
3611 int s_start, s_end, s_inc;
Guy Schalnat0d580581995-07-20 02:43:20 -05003612 png_uint_32 i;
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003613 int jstop = png_pass_inc[pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05003614
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003615#ifdef PNG_READ_PACKSWAP_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003616 if (transformations & PNG_PACKSWAP)
3617 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003618 sshift = (int)(((row_info->width + 1) & 0x01) << 2);
3619 dshift = (int)(((final_width + 1) & 0x01) << 2);
3620 s_start = 4;
3621 s_end = 0;
3622 s_inc = -4;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003623 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003624
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003625 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05003626#endif
3627 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003628 sshift = (int)((1 - ((row_info->width + 1) & 0x01)) << 2);
3629 dshift = (int)((1 - ((final_width + 1) & 0x01)) << 2);
3630 s_start = 0;
3631 s_end = 4;
3632 s_inc = 4;
3633 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05003634
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003635 for (i = 0; i < row_info->width; i++)
3636 {
Glenn Randers-Pehrson8db19982011-10-27 16:17:24 -05003637 png_byte v = (png_byte)((*sp >> sshift) & 0x0f);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003638 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003639
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003640 for (j = 0; j < jstop; j++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003641 {
John Bowler8fb6c6a2012-01-25 07:47:44 -06003642 unsigned int tmp = *dp & (0xf0f >> (4 - dshift));
3643 tmp |= v << dshift;
3644 *dp = (png_byte)(tmp & 0xff);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003645
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003646 if (dshift == s_end)
3647 {
3648 dshift = s_start;
3649 dp--;
3650 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003651
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003652 else
3653 dshift += s_inc;
3654 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003655
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003656 if (sshift == s_end)
3657 {
3658 sshift = s_start;
Guy Schalnat0d580581995-07-20 02:43:20 -05003659 sp--;
3660 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003661
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003662 else
3663 sshift += s_inc;
Guy Schalnat0d580581995-07-20 02:43:20 -05003664 }
3665 break;
3666 }
John Bowlerac8375d2011-10-06 22:27:16 -05003667
Guy Schalnat0d580581995-07-20 02:43:20 -05003668 default:
3669 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003670 png_size_t pixel_bytes = (row_info->pixel_depth >> 3);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003671
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06003672 png_bytep sp = row + (png_size_t)(row_info->width - 1)
3673 * pixel_bytes;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003674
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003675 png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05003676
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003677 int jstop = png_pass_inc[pass];
3678 png_uint_32 i;
3679
3680 for (i = 0; i < row_info->width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003681 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003682 png_byte v[8];
3683 int j;
3684
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -05003685 memcpy(v, sp, pixel_bytes);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003686
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003687 for (j = 0; j < jstop; j++)
3688 {
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -05003689 memcpy(dp, v, pixel_bytes);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003690 dp -= pixel_bytes;
3691 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003692
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003693 sp -= pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05003694 }
3695 break;
3696 }
3697 }
John Bowlerac8375d2011-10-06 22:27:16 -05003698
Guy Schalnat0d580581995-07-20 02:43:20 -05003699 row_info->width = final_width;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003700 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width);
Guy Schalnat0d580581995-07-20 02:43:20 -05003701 }
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -05003702#ifndef PNG_READ_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003703 PNG_UNUSED(transformations) /* Silence compiler warning */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05003704#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003705}
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06003706#endif /* PNG_READ_INTERLACING_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05003707
Mans Rullgardd3a94802011-11-03 00:47:55 -05003708static void
3709png_read_filter_row_sub(png_row_infop row_info, png_bytep row,
3710 png_const_bytep prev_row)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003711{
Mans Rullgardd3a94802011-11-03 00:47:55 -05003712 png_size_t i;
3713 png_size_t istop = row_info->rowbytes;
3714 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
3715 png_bytep rp = row + bpp;
Mans Rullgardd3a94802011-11-03 00:47:55 -05003716
3717 PNG_UNUSED(prev_row)
3718
3719 for (i = bpp; i < istop; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003720 {
John Bowler7875d532011-11-07 22:33:49 -06003721 *rp = (png_byte)(((int)(*rp) + (int)(*(rp-bpp))) & 0xff);
Mans Rullgardd3a94802011-11-03 00:47:55 -05003722 rp++;
3723 }
3724}
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003725
Mans Rullgardd3a94802011-11-03 00:47:55 -05003726static void
3727png_read_filter_row_up(png_row_infop row_info, png_bytep row,
3728 png_const_bytep prev_row)
3729{
3730 png_size_t i;
3731 png_size_t istop = row_info->rowbytes;
3732 png_bytep rp = row;
3733 png_const_bytep pp = prev_row;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003734
Mans Rullgardd3a94802011-11-03 00:47:55 -05003735 for (i = 0; i < istop; i++)
3736 {
3737 *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
3738 rp++;
3739 }
3740}
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003741
Mans Rullgardd3a94802011-11-03 00:47:55 -05003742static void
3743png_read_filter_row_avg(png_row_infop row_info, png_bytep row,
3744 png_const_bytep prev_row)
3745{
3746 png_size_t i;
3747 png_bytep rp = row;
3748 png_const_bytep pp = prev_row;
Mans Rullgardd3a94802011-11-03 00:47:55 -05003749 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
3750 png_size_t istop = row_info->rowbytes - bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003751
Mans Rullgardd3a94802011-11-03 00:47:55 -05003752 for (i = 0; i < bpp; i++)
3753 {
3754 *rp = (png_byte)(((int)(*rp) +
3755 ((int)(*pp++) / 2 )) & 0xff);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003756
Mans Rullgardd3a94802011-11-03 00:47:55 -05003757 rp++;
3758 }
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003759
Mans Rullgardd3a94802011-11-03 00:47:55 -05003760 for (i = 0; i < istop; i++)
3761 {
3762 *rp = (png_byte)(((int)(*rp) +
John Bowler7875d532011-11-07 22:33:49 -06003763 (int)(*pp++ + *(rp-bpp)) / 2 ) & 0xff);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003764
Mans Rullgardd3a94802011-11-03 00:47:55 -05003765 rp++;
3766 }
3767}
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003768
Mans Rullgardd3a94802011-11-03 00:47:55 -05003769static void
John Bowlerfcc02632011-11-03 18:31:00 -05003770png_read_filter_row_paeth_1byte_pixel(png_row_infop row_info, png_bytep row,
Mans Rullgardd3a94802011-11-03 00:47:55 -05003771 png_const_bytep prev_row)
3772{
John Bowlerfcc02632011-11-03 18:31:00 -05003773 png_bytep rp_end = row + row_info->rowbytes;
3774 int a, c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003775
John Bowlerfcc02632011-11-03 18:31:00 -05003776 /* First pixel/byte */
3777 c = *prev_row++;
3778 a = *row + c;
3779 *row++ = (png_byte)a;
3780
3781 /* Remainder */
3782 while (row < rp_end)
Mans Rullgardd3a94802011-11-03 00:47:55 -05003783 {
John Bowlerfcc02632011-11-03 18:31:00 -05003784 int b, pa, pb, pc, p;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003785
John Bowlerfcc02632011-11-03 18:31:00 -05003786 a &= 0xff; /* From previous iteration or start */
3787 b = *prev_row++;
Mans Rullgardd3a94802011-11-03 00:47:55 -05003788
3789 p = b - c;
3790 pc = a - c;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003791
John Bowlerfcc02632011-11-03 18:31:00 -05003792# ifdef PNG_USE_ABS
3793 pa = abs(p);
3794 pb = abs(pc);
3795 pc = abs(p + pc);
3796# else
3797 pa = p < 0 ? -p : p;
3798 pb = pc < 0 ? -pc : pc;
3799 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
3800# endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003801
John Bowlerfcc02632011-11-03 18:31:00 -05003802 /* Find the best predictor, the least of pa, pb, pc favoring the earlier
3803 * ones in the case of a tie.
3804 */
3805 if (pb < pa) pa = pb, a = b;
3806 if (pc < pa) a = c;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003807
John Bowlerfcc02632011-11-03 18:31:00 -05003808 /* Calculate the current pixel in a, and move the previous row pixel to c
3809 * for the next time round the loop
3810 */
3811 c = b;
3812 a += *row;
3813 *row++ = (png_byte)a;
3814 }
3815}
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003816
John Bowlerfcc02632011-11-03 18:31:00 -05003817static void
3818png_read_filter_row_paeth_multibyte_pixel(png_row_infop row_info, png_bytep row,
3819 png_const_bytep prev_row)
3820{
3821 int bpp = (row_info->pixel_depth + 7) >> 3;
3822 png_bytep rp_end = row + bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003823
John Bowlerfcc02632011-11-03 18:31:00 -05003824 /* Process the first pixel in the row completely (this is the same as 'up'
3825 * because there is only one candidate predictor for the first row).
3826 */
3827 while (row < rp_end)
3828 {
3829 int a = *row + *prev_row++;
3830 *row++ = (png_byte)a;
3831 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003832
John Bowlerfcc02632011-11-03 18:31:00 -05003833 /* Remainder */
3834 rp_end += row_info->rowbytes - bpp;
3835
3836 while (row < rp_end)
3837 {
3838 int a, b, c, pa, pb, pc, p;
3839
3840 c = *(prev_row - bpp);
3841 a = *(row - bpp);
3842 b = *prev_row++;
3843
3844 p = b - c;
3845 pc = a - c;
3846
3847# ifdef PNG_USE_ABS
3848 pa = abs(p);
3849 pb = abs(pc);
3850 pc = abs(p + pc);
3851# else
3852 pa = p < 0 ? -p : p;
3853 pb = pc < 0 ? -pc : pc;
3854 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
3855# endif
3856
3857 if (pb < pa) pa = pb, a = b;
3858 if (pc < pa) a = c;
3859
3860 c = b;
3861 a += *row;
3862 *row++ = (png_byte)a;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003863 }
3864}
Guy Schalnat0d580581995-07-20 02:43:20 -05003865
Mans Rullgardd3a94802011-11-03 00:47:55 -05003866static void
John Bowler5d567862011-12-24 09:12:00 -06003867png_init_filter_functions(png_structrp pp)
John Bowler6cf32b42012-12-15 07:55:54 -06003868 /* This function is called once for every PNG image to set the
3869 * implementations required to reverse the filtering of PNG rows. Reversing
3870 * the filter is the first transformation performed on the row data. It is
3871 * performed in place, therefore an implementation can be selected based on
3872 * the image pixel format. If the implementation depends on image width then
3873 * take care to ensure that it works corretly if the image is interlaced -
3874 * interlacing causes the actual row width to vary.
3875 */
Mans Rullgardd3a94802011-11-03 00:47:55 -05003876{
John Bowlerfcc02632011-11-03 18:31:00 -05003877 unsigned int bpp = (pp->pixel_depth + 7) >> 3;
3878
Mans Rullgardd3a94802011-11-03 00:47:55 -05003879 pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub;
3880 pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up;
3881 pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg;
John Bowlerfcc02632011-11-03 18:31:00 -05003882 if (bpp == 1)
3883 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
3884 png_read_filter_row_paeth_1byte_pixel;
3885 else
3886 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
3887 png_read_filter_row_paeth_multibyte_pixel;
Mans Rullgardd3a94802011-11-03 00:47:55 -05003888
John Bowlerf758d6c2012-12-13 11:26:50 -06003889#ifdef PNG_FILTER_OPTIMIZATIONS
3890 /* To use this define PNG_FILTER_OPTIMIZATIONS as the name of a function to
3891 * call to install hardware optimizations for the above functions; simply
3892 * replace whatever elements of the pp->read_filter[] array with a hardware
3893 * specific (or, for that matter, generic) optimization.
3894 *
3895 * To see an example of this examine what configure.ac does when
3896 * --enable-arm-neon is specified on the command line.
3897 */
3898 PNG_FILTER_OPTIMIZATIONS(pp, bpp);
Mans Rullgardd3a94802011-11-03 00:47:55 -05003899#endif
3900}
3901
3902void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06003903png_read_filter_row(png_structrp pp, png_row_infop row_info, png_bytep row,
Mans Rullgardd3a94802011-11-03 00:47:55 -05003904 png_const_bytep prev_row, int filter)
3905{
John Bowler6cf32b42012-12-15 07:55:54 -06003906 /* OPTIMIZATION: DO NOT MODIFY THIS FUNCTION, instead #define
3907 * PNG_FILTER_OPTIMIZATIONS to a function that overrides the generic
3908 * implementations. See png_init_filter_functions above.
3909 */
Mans Rullgardd3a94802011-11-03 00:47:55 -05003910 if (pp->read_filter[0] == NULL)
3911 png_init_filter_functions(pp);
3912 if (filter > PNG_FILTER_VALUE_NONE && filter < PNG_FILTER_VALUE_LAST)
3913 pp->read_filter[filter-1](row_info, row, prev_row);
3914}
3915
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05003916#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05003917void /* PRIVATE */
John Bowlerb5d00512012-03-09 09:15:18 -06003918png_read_IDAT_data(png_structrp png_ptr, png_bytep output,
3919 png_alloc_size_t avail_out)
3920{
3921 /* Loop reading IDATs and decompressing the result into output[avail_out] */
3922 png_ptr->zstream.next_out = output;
3923 png_ptr->zstream.avail_out = 0; /* safety: set below */
3924
3925 if (output == NULL)
3926 avail_out = 0;
3927
3928 do
3929 {
3930 int ret;
3931 png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];
3932
3933 if (png_ptr->zstream.avail_in == 0)
3934 {
3935 uInt avail_in;
3936 png_bytep buffer;
3937
3938 while (png_ptr->idat_size == 0)
3939 {
3940 png_crc_finish(png_ptr, 0);
3941
3942 png_ptr->idat_size = png_read_chunk_header(png_ptr);
3943 /* This is an error even in the 'check' case because the code just
3944 * consumed a non-IDAT header.
3945 */
3946 if (png_ptr->chunk_name != png_IDAT)
3947 png_error(png_ptr, "Not enough image data");
3948 }
3949
3950 avail_in = png_ptr->IDAT_read_size;
3951
3952 if (avail_in > png_ptr->idat_size)
3953 avail_in = (uInt)png_ptr->idat_size;
3954
3955 /* A PNG with a gradually increasing IDAT size will defeat this attempt
3956 * to minimize memory usage by causing lots of re-allocs, but
3957 * realistically doing IDAT_read_size re-allocs is not likely to be a
3958 * big problem.
3959 */
3960 buffer = png_read_buffer(png_ptr, avail_in, 0/*error*/);
3961
3962 png_crc_read(png_ptr, buffer, avail_in);
3963 png_ptr->idat_size -= avail_in;
3964
3965 png_ptr->zstream.next_in = buffer;
3966 png_ptr->zstream.avail_in = avail_in;
3967 }
3968
3969 /* And set up the output side. */
3970 if (output != NULL) /* standard read */
3971 {
3972 uInt out = ZLIB_IO_MAX;
3973
3974 if (out > avail_out)
3975 out = (uInt)avail_out;
3976
3977 avail_out -= out;
3978 png_ptr->zstream.avail_out = out;
3979 }
3980
3981 else /* check for end */
3982 {
3983 png_ptr->zstream.next_out = tmpbuf;
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05003984 png_ptr->zstream.avail_out = (sizeof tmpbuf);
John Bowlerb5d00512012-03-09 09:15:18 -06003985 }
3986
3987 /* Use NO_FLUSH; this gives zlib the maximum opportunity to optimize the
John Bowler90669192012-03-09 22:03:13 -06003988 * process. If the LZ stream is truncated the sequential reader will
3989 * terminally damage the stream, above, by reading the chunk header of the
3990 * following chunk (it then exits with png_error).
3991 *
3992 * TODO: deal more elegantly with truncated IDAT lists.
John Bowlerb5d00512012-03-09 09:15:18 -06003993 */
3994 ret = inflate(&png_ptr->zstream, Z_NO_FLUSH);
3995
3996 /* Take the unconsumed output back (so, in the 'check' case this just
3997 * counts up).
3998 */
3999 avail_out += png_ptr->zstream.avail_out;
4000 png_ptr->zstream.avail_out = 0;
4001
4002 if (ret == Z_STREAM_END)
4003 {
4004 /* Do this for safety; we won't read any more into this row. */
4005 png_ptr->zstream.next_out = NULL;
4006
4007 png_ptr->mode |= PNG_AFTER_IDAT;
4008 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
4009
4010 if (png_ptr->zstream.avail_in > 0 || png_ptr->idat_size > 0)
4011 png_chunk_benign_error(png_ptr, "Extra compressed data");
4012 break;
4013 }
4014
4015 if (ret != Z_OK)
4016 {
4017 png_zstream_error(png_ptr, ret);
4018
4019 if (output != NULL)
4020 png_chunk_error(png_ptr, png_ptr->zstream.msg);
4021
4022 else /* checking */
4023 {
4024 png_chunk_benign_error(png_ptr, png_ptr->zstream.msg);
4025 return;
4026 }
4027 }
4028 } while (avail_out > 0);
4029
4030 if (avail_out > 0)
4031 {
4032 /* The stream ended before the image; this is the same as too few IDATs so
4033 * should be handled the same way.
4034 */
4035 if (output != NULL)
4036 png_error(png_ptr, "Not enough image data");
4037
4038 else /* checking */
4039 png_chunk_benign_error(png_ptr, "Too much image data");
4040 }
4041}
4042
4043void /* PRIVATE */
4044png_read_finish_IDAT(png_structrp png_ptr)
4045{
4046 /* We don't need any more data and the stream should have ended, however the
4047 * LZ end code may actually not have been processed. In this case we must
4048 * read it otherwise stray unread IDAT data or, more likely, an IDAT chunk
4049 * may still remain to be consumed.
4050 */
4051 if (!(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED))
4052 {
4053 /* The NULL causes png_read_IDAT_data to swallow any remaining bytes in
4054 * the compressed stream, but the stream may be damaged too, so even after
4055 * this call we may need to terminate the zstream ownership.
4056 */
4057 png_read_IDAT_data(png_ptr, NULL, 0);
4058 png_ptr->zstream.next_out = NULL; /* safety */
4059
4060 /* Now clear everything out for safety; the following may not have been
4061 * done.
4062 */
4063 if (!(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED))
4064 {
4065 png_ptr->mode |= PNG_AFTER_IDAT;
4066 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
4067 }
4068 }
4069
4070 /* If the zstream has not been released do it now *and* terminate the reading
4071 * of the final IDAT chunk.
4072 */
4073 if (png_ptr->zowner == png_IDAT)
4074 {
4075 /* Always do this; the pointers otherwise point into the read buffer. */
4076 png_ptr->zstream.next_in = NULL;
4077 png_ptr->zstream.avail_in = 0;
4078
4079 /* Now we no longer own the zstream. */
4080 png_ptr->zowner = 0;
4081
4082 /* The slightly weird semantics of the sequential IDAT reading is that we
4083 * are always in or at the end of an IDAT chunk, so we always need to do a
4084 * crc_finish here. If idat_size is non-zero we also need to read the
4085 * spurious bytes at the end of the chunk now.
4086 */
4087 (void)png_crc_finish(png_ptr, png_ptr->idat_size);
4088 }
4089}
4090
4091void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06004092png_read_finish_row(png_structrp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05004093{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004094#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004095 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004096
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004097 /* Start of interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004098 static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004099
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004100 /* Offset to next interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004101 static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004102
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004103 /* Start of interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004104 static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004105
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004106 /* Offset to next interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004107 static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004108#endif /* PNG_READ_INTERLACING_SUPPORTED */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004109
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05004110 png_debug(1, "in png_read_finish_row");
Guy Schalnat0d580581995-07-20 02:43:20 -05004111 png_ptr->row_number++;
4112 if (png_ptr->row_number < png_ptr->num_rows)
4113 return;
4114
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004115#ifdef PNG_READ_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05004116 if (png_ptr->interlaced)
4117 {
4118 png_ptr->row_number = 0;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004119
Glenn Randers-Pehrson435cf872011-10-05 16:23:53 -05004120 /* TO DO: don't do this if prev_row isn't needed (requires
4121 * read-ahead of the next row's filter byte.
4122 */
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -05004123 memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004124
Guy Schalnat0d580581995-07-20 02:43:20 -05004125 do
4126 {
4127 png_ptr->pass++;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004128
Guy Schalnat0d580581995-07-20 02:43:20 -05004129 if (png_ptr->pass >= 7)
4130 break;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004131
Guy Schalnat0d580581995-07-20 02:43:20 -05004132 png_ptr->iwidth = (png_ptr->width +
4133 png_pass_inc[png_ptr->pass] - 1 -
4134 png_pass_start[png_ptr->pass]) /
4135 png_pass_inc[png_ptr->pass];
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05004136
Guy Schalnat0d580581995-07-20 02:43:20 -05004137 if (!(png_ptr->transformations & PNG_INTERLACE))
4138 {
4139 png_ptr->num_rows = (png_ptr->height +
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004140 png_pass_yinc[png_ptr->pass] - 1 -
4141 png_pass_ystart[png_ptr->pass]) /
4142 png_pass_yinc[png_ptr->pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05004143 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004144
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -05004145 else /* if (png_ptr->transformations & PNG_INTERLACE) */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004146 break; /* libpng deinterlacing sees every row */
4147
4148 } while (png_ptr->num_rows == 0 || png_ptr->iwidth == 0);
Guy Schalnat0d580581995-07-20 02:43:20 -05004149
4150 if (png_ptr->pass < 7)
4151 return;
4152 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004153#endif /* PNG_READ_INTERLACING_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05004154
John Bowler42a2b552012-03-05 20:57:40 -06004155 /* Here after at the end of the last row of the last pass. */
John Bowlerb5d00512012-03-09 09:15:18 -06004156 png_read_finish_IDAT(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05004157}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05004158#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05004159
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05004160void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06004161png_read_start_row(png_structrp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05004162{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004163#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004164 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004165
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004166 /* Start of interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004167 static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004168
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004169 /* Offset to next interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004170 static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004171
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004172 /* Start of interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004173 static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004174
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004175 /* Offset to next interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004176 static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004177#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004178
Guy Schalnat0d580581995-07-20 02:43:20 -05004179 int max_pixel_depth;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05004180 png_size_t row_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05004181
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05004182 png_debug(1, "in png_read_start_row");
John Bowler42a2b552012-03-05 20:57:40 -06004183
John Bowler4a12f4a2011-04-17 18:34:22 -05004184#ifdef PNG_READ_TRANSFORMS_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05004185 png_init_read_transformations(png_ptr);
John Bowler4a12f4a2011-04-17 18:34:22 -05004186#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004187#ifdef PNG_READ_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05004188 if (png_ptr->interlaced)
4189 {
4190 if (!(png_ptr->transformations & PNG_INTERLACE))
4191 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004192 png_pass_ystart[0]) / png_pass_yinc[0];
4193
Guy Schalnat0d580581995-07-20 02:43:20 -05004194 else
4195 png_ptr->num_rows = png_ptr->height;
4196
4197 png_ptr->iwidth = (png_ptr->width +
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004198 png_pass_inc[png_ptr->pass] - 1 -
4199 png_pass_start[png_ptr->pass]) /
4200 png_pass_inc[png_ptr->pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05004201 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004202
Guy Schalnat0d580581995-07-20 02:43:20 -05004203 else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004204#endif /* PNG_READ_INTERLACING_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05004205 {
4206 png_ptr->num_rows = png_ptr->height;
4207 png_ptr->iwidth = png_ptr->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05004208 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004209
Guy Schalnat0d580581995-07-20 02:43:20 -05004210 max_pixel_depth = png_ptr->pixel_depth;
4211
John Bowlere6fb6912011-11-08 21:35:16 -06004212 /* WARNING: * png_read_transform_info (pngrtran.c) performs a simpliar set of
4213 * calculations to calculate the final pixel depth, then
4214 * png_do_read_transforms actually does the transforms. This means that the
4215 * code which effectively calculates this value is actually repeated in three
4216 * separate places. They must all match. Innocent changes to the order of
4217 * transformations can and will break libpng in a way that causes memory
4218 * overwrites.
4219 *
4220 * TODO: fix this.
4221 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004222#ifdef PNG_READ_PACK_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05004223 if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8)
Guy Schalnat0d580581995-07-20 02:43:20 -05004224 max_pixel_depth = 8;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004225#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05004226
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004227#ifdef PNG_READ_EXPAND_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -05004228 if (png_ptr->transformations & PNG_EXPAND)
Guy Schalnat0d580581995-07-20 02:43:20 -05004229 {
4230 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
4231 {
4232 if (png_ptr->num_trans)
4233 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004234
Guy Schalnat0d580581995-07-20 02:43:20 -05004235 else
4236 max_pixel_depth = 24;
4237 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004238
Guy Schalnat0d580581995-07-20 02:43:20 -05004239 else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
4240 {
4241 if (max_pixel_depth < 8)
4242 max_pixel_depth = 8;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004243
Guy Schalnat0d580581995-07-20 02:43:20 -05004244 if (png_ptr->num_trans)
4245 max_pixel_depth *= 2;
4246 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004247
Guy Schalnat0d580581995-07-20 02:43:20 -05004248 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
4249 {
4250 if (png_ptr->num_trans)
4251 {
4252 max_pixel_depth *= 4;
4253 max_pixel_depth /= 3;
4254 }
4255 }
4256 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004257#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05004258
John Bowler4d562962011-02-12 09:01:20 -06004259#ifdef PNG_READ_EXPAND_16_SUPPORTED
4260 if (png_ptr->transformations & PNG_EXPAND_16)
4261 {
4262# ifdef PNG_READ_EXPAND_SUPPORTED
4263 /* In fact it is an error if it isn't supported, but checking is
4264 * the safe way.
4265 */
4266 if (png_ptr->transformations & PNG_EXPAND)
4267 {
4268 if (png_ptr->bit_depth < 16)
4269 max_pixel_depth *= 2;
4270 }
4271 else
4272# endif
4273 png_ptr->transformations &= ~PNG_EXPAND_16;
4274 }
4275#endif
4276
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004277#ifdef PNG_READ_FILLER_SUPPORTED
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004278 if (png_ptr->transformations & (PNG_FILLER))
Guy Schalnat0d580581995-07-20 02:43:20 -05004279 {
John Bowlere6fb6912011-11-08 21:35:16 -06004280 if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004281 {
4282 if (max_pixel_depth <= 8)
4283 max_pixel_depth = 16;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004284
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004285 else
4286 max_pixel_depth = 32;
4287 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004288
John Bowlere6fb6912011-11-08 21:35:16 -06004289 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB ||
4290 png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004291 {
4292 if (max_pixel_depth <= 32)
4293 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004294
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004295 else
4296 max_pixel_depth = 64;
4297 }
Guy Schalnat0d580581995-07-20 02:43:20 -05004298 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004299#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05004300
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004301#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05004302 if (png_ptr->transformations & PNG_GRAY_TO_RGB)
4303 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06004304 if (
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004305#ifdef PNG_READ_EXPAND_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004306 (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) ||
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06004307#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004308#ifdef PNG_READ_FILLER_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004309 (png_ptr->transformations & (PNG_FILLER)) ||
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06004310#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004311 png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
Guy Schalnat0d580581995-07-20 02:43:20 -05004312 {
4313 if (max_pixel_depth <= 16)
4314 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004315
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06004316 else
Guy Schalnat0d580581995-07-20 02:43:20 -05004317 max_pixel_depth = 64;
4318 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004319
Guy Schalnat0d580581995-07-20 02:43:20 -05004320 else
4321 {
4322 if (max_pixel_depth <= 8)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004323 {
4324 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06004325 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004326
4327 else
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06004328 max_pixel_depth = 24;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004329 }
4330
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06004331 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4332 max_pixel_depth = 64;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004333
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06004334 else
Guy Schalnat0d580581995-07-20 02:43:20 -05004335 max_pixel_depth = 48;
4336 }
4337 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004338#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05004339
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05004340#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
4341defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004342 if (png_ptr->transformations & PNG_USER_TRANSFORM)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004343 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004344 int user_pixel_depth = png_ptr->user_transform_depth *
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05004345 png_ptr->user_transform_channels;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004346
4347 if (user_pixel_depth > max_pixel_depth)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004348 max_pixel_depth = user_pixel_depth;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004349 }
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05004350#endif
4351
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004352 /* This value is stored in png_struct and double checked in the row read
4353 * code.
4354 */
4355 png_ptr->maximum_pixel_depth = (png_byte)max_pixel_depth;
4356 png_ptr->transformed_pixel_depth = 0; /* calculated on demand */
4357
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004358 /* Align the width on the next larger 8 pixels. Mainly used
4359 * for interlacing
4360 */
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05004361 row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004362 /* Calculate the maximum bytes needed, adding a byte and a pixel
4363 * for safety's sake
4364 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004365 row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) +
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004366 1 + ((max_pixel_depth + 7) >> 3);
4367
Guy Schalnat0d580581995-07-20 02:43:20 -05004368#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05004369 if (row_bytes > (png_uint_32)65536L)
Guy Schalnate5a37791996-06-05 15:50:50 -05004370 png_error(png_ptr, "This image requires a row greater than 64KB");
Guy Schalnat0d580581995-07-20 02:43:20 -05004371#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004372
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06004373 if (row_bytes + 48 > png_ptr->old_big_row_buf_size)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004374 {
4375 png_free(png_ptr, png_ptr->big_row_buf);
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004376 png_free(png_ptr, png_ptr->big_prev_row);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004377
Glenn Randers-Pehrson6917b512009-03-09 15:31:08 -05004378 if (png_ptr->interlaced)
Glenn Randers-Pehrsona515d302010-01-01 10:24:25 -06004379 png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr,
4380 row_bytes + 48);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004381
Glenn Randers-Pehrsona515d302010-01-01 10:24:25 -06004382 else
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004383 png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004384
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004385 png_ptr->big_prev_row = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06004386
4387#ifdef PNG_ALIGNED_MEMORY_SUPPORTED
4388 /* Use 16-byte aligned memory for row_buf with at least 16 bytes
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004389 * of padding before and after row_buf; treat prev_row similarly.
John Bowlerac8375d2011-10-06 22:27:16 -05004390 * NOTE: the alignment is to the start of the pixels, one beyond the start
4391 * of the buffer, because of the filter byte. Prior to libpng 1.5.6 this
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004392 * was incorrect; the filter byte was aligned, which had the exact
4393 * opposite effect of that intended.
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06004394 */
John Bowlerac8375d2011-10-06 22:27:16 -05004395 {
4396 png_bytep temp = png_ptr->big_row_buf + 32;
Glenn Randers-Pehrson8db19982011-10-27 16:17:24 -05004397 int extra = (int)((temp - (png_bytep)0) & 0x0f);
John Bowlerac8375d2011-10-06 22:27:16 -05004398 png_ptr->row_buf = temp - extra - 1/*filter byte*/;
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004399
4400 temp = png_ptr->big_prev_row + 32;
Glenn Randers-Pehrson8db19982011-10-27 16:17:24 -05004401 extra = (int)((temp - (png_bytep)0) & 0x0f);
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004402 png_ptr->prev_row = temp - extra - 1/*filter byte*/;
John Bowlerac8375d2011-10-06 22:27:16 -05004403 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004404
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06004405#else
John Bowlerac8375d2011-10-06 22:27:16 -05004406 /* Use 31 bytes of padding before and 17 bytes after row_buf. */
4407 png_ptr->row_buf = png_ptr->big_row_buf + 31;
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004408 png_ptr->prev_row = png_ptr->big_prev_row + 31;
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06004409#endif
4410 png_ptr->old_big_row_buf_size = row_bytes + 48;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004411 }
Guy Schalnat0d580581995-07-20 02:43:20 -05004412
4413#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004414 if (png_ptr->rowbytes > 65535)
Guy Schalnate5a37791996-06-05 15:50:50 -05004415 png_error(png_ptr, "This image requires a row greater than 64KB");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004416
Guy Schalnat0d580581995-07-20 02:43:20 -05004417#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004418 if (png_ptr->rowbytes > (PNG_SIZE_MAX - 1))
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05004419 png_error(png_ptr, "Row has too many bytes to allocate in memory");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004420
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -05004421 memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05004422
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004423 png_debug1(3, "width = %u,", png_ptr->width);
4424 png_debug1(3, "height = %u,", png_ptr->height);
4425 png_debug1(3, "iwidth = %u,", png_ptr->iwidth);
4426 png_debug1(3, "num_rows = %u,", png_ptr->num_rows);
Glenn Randers-Pehrsonb764c602011-01-14 21:18:37 -06004427 png_debug1(3, "rowbytes = %lu,", (unsigned long)png_ptr->rowbytes);
4428 png_debug1(3, "irowbytes = %lu",
4429 (unsigned long)PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05004430
John Bowlerb5d00512012-03-09 09:15:18 -06004431 /* The sequential reader needs a buffer for IDAT, but the progressive reader
4432 * does not, so free the read buffer now regardless; the sequential reader
4433 * reallocates it on demand.
4434 */
4435 if (png_ptr->read_buffer)
4436 {
4437 png_bytep buffer = png_ptr->read_buffer;
4438
4439 png_ptr->read_buffer_size = 0;
4440 png_ptr->read_buffer = NULL;
4441 png_free(png_ptr, buffer);
4442 }
4443
John Bowler90669192012-03-09 22:03:13 -06004444 /* Finally claim the zstream for the inflate of the IDAT data, use the bits
4445 * value from the stream (note that this will result in a fatal error if the
4446 * IDAT stream has a bogus deflate header window_bits value, but this should
4447 * not be happening any longer!)
4448 */
4449 if (png_inflate_claim(png_ptr, png_IDAT, 0) != Z_OK)
John Bowlerb5d00512012-03-09 09:15:18 -06004450 png_error(png_ptr, png_ptr->zstream.msg);
John Bowler42a2b552012-03-05 20:57:40 -06004451
Guy Schalnate5a37791996-06-05 15:50:50 -05004452 png_ptr->flags |= PNG_FLAG_ROW_INIT;
Guy Schalnat0d580581995-07-20 02:43:20 -05004453}
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06004454#endif /* PNG_READ_SUPPORTED */