blob: d52f1007b9c38327f9058f7ff85224662bcd0240 [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-Pehrson65a24d02011-04-01 20:41:53 -05004 * Last changed in libpng 1.5.3 [(PENDING RELEASE)]
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06005 * Copyright (c) 1998-2011 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
24png_get_uint_31(png_structp png_ptr, png_const_bytep buf)
25{
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 */
43png_get_fixed_point(png_structp png_ptr, png_const_bytep buf)
44{
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);
90 if ((uval & 0x80000000L) == 0) /* non-negative */
91 return uval;
Andreas Dilger47a0c421997-05-16 02:46:07 -050092
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -060093 uval = (uval ^ 0xffffffffL) + 1; /* 2's complement: -x = ~x+1 */
94 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 */
117png_read_sig(png_structp png_ptr, png_infop info_ptr)
118{
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 */
152png_read_chunk_header(png_structp png_ptr)
153{
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-Pehrsonbeb572e2006-08-19 13:59:24 -0500168 png_memcpy(png_ptr->chunk_name, buf + 4, 4);
169
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600170 png_debug2(0, "Reading %s chunk, length = %u",
171 png_ptr->chunk_name, 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);
175 png_calculate_crc(png_ptr, png_ptr->chunk_name, 4);
176
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 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500189png_crc_read(png_structp png_ptr, png_bytep buf, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500190{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500191 if (png_ptr == NULL)
192 return;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600193
Guy Schalnat6d764711995-12-19 03:22:19 -0600194 png_read_data(png_ptr, buf, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500195 png_calculate_crc(png_ptr, buf, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500196}
197
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600198/* Optionally skip data and then check the CRC. Depending on whether we
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500199 * are reading a ancillary or critical chunk, and how the program has set
200 * things up, we may calculate the CRC on the data and print a message.
201 * Returns '1' if there was a CRC error, '0' otherwise.
202 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500203int /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600204png_crc_finish(png_structp png_ptr, png_uint_32 skip)
Guy Schalnat0d580581995-07-20 02:43:20 -0500205{
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500206 png_size_t i;
207 png_size_t istop = png_ptr->zbuf_size;
Guy Schalnat0d580581995-07-20 02:43:20 -0500208
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500209 for (i = (png_size_t)skip; i > istop; i -= istop)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600210 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500211 png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500212 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600213
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500214 if (i)
Guy Schalnat0d580581995-07-20 02:43:20 -0500215 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500216 png_crc_read(png_ptr, png_ptr->zbuf, i);
Guy Schalnat0d580581995-07-20 02:43:20 -0500217 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600218
Andreas Dilger47a0c421997-05-16 02:46:07 -0500219 if (png_crc_error(png_ptr))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600220 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500221 if (((png_ptr->chunk_name[0] & 0x20) && /* Ancillary */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500222 !(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)) ||
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500223 (!(png_ptr->chunk_name[0] & 0x20) && /* Critical */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600224 (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE)))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600225 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600226 png_chunk_warning(png_ptr, "CRC error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600227 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600228
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600229 else
230 {
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500231 png_chunk_benign_error(png_ptr, "CRC error");
232 return (0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600233 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600234
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600235 return (1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600236 }
237
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600238 return (0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500239}
240
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600241/* Compare the CRC stored in the PNG file with that calculated by libpng from
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500242 * the data it has read thus far.
243 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500244int /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600245png_crc_error(png_structp png_ptr)
246{
247 png_byte crc_bytes[4];
248 png_uint_32 crc;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500249 int need_crc = 1;
250
251 if (png_ptr->chunk_name[0] & 0x20) /* ancillary */
252 {
253 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
254 (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
255 need_crc = 0;
256 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600257
Andreas Dilger47a0c421997-05-16 02:46:07 -0500258 else /* critical */
259 {
260 if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
261 need_crc = 0;
262 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600263
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500264#ifdef PNG_IO_STATE_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500265 png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_CRC;
266#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500267
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600268 /* The chunk CRC must be serialized in a single I/O call. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600269 png_read_data(png_ptr, crc_bytes, 4);
270
Andreas Dilger47a0c421997-05-16 02:46:07 -0500271 if (need_crc)
272 {
273 crc = png_get_uint_32(crc_bytes);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600274 return ((int)(crc != png_ptr->crc));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500275 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600276
Andreas Dilger47a0c421997-05-16 02:46:07 -0500277 else
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600278 return (0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600279}
280
Glenn Randers-Pehrson5975f312011-04-01 13:15:36 -0500281#ifdef PNG_READ_COMPRESSED_TEXT_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600282static png_size_t
283png_inflate(png_structp png_ptr, png_bytep data, png_size_t size,
284 png_bytep output, png_size_t output_size)
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600285{
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600286 png_size_t count = 0;
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600287
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600288 /* zlib can't necessarily handle more than 65535 bytes at once (i.e. it can't
289 * even necessarily handle 65536 bytes) because the type uInt is "16 bits or
290 * more". Consequently it is necessary to chunk the input to zlib. This
291 * code uses ZLIB_IO_MAX, from pngpriv.h, as the maximum (the maximum value
292 * that can be stored in a uInt.) It is possible to set ZLIB_IO_MAX to a
293 * lower value in pngpriv.h and this may sometimes have a performance
294 * advantage, because it forces access of the input data to be separated from
295 * at least some of the use by some period of time.
296 */
297 png_ptr->zstream.next_in = data;
298 /* avail_in is set below from 'size' */
299 png_ptr->zstream.avail_in = 0;
300
301 while (1)
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600302 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600303 int ret, avail;
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600304
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600305 /* The setting of 'avail_in' used to be outside the loop, by setting it
306 * inside it is possible to chunk the input to zlib and simply rely on
307 * zlib to advance the 'next_in' pointer. This allows arbitrary amounts o
308 * data to be passed through zlib at the unavoidable cost of requiring a
309 * window save (memcpy of up to 32768 output bytes) every ZLIB_IO_MAX
310 * input bytes.
311 */
312 if (png_ptr->zstream.avail_in == 0 && size > 0)
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600313 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600314 if (size <= ZLIB_IO_MAX)
Glenn Randers-Pehrson72cda2d2010-03-06 08:18:03 -0600315 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600316 /* The value is less than ZLIB_IO_MAX so the cast is safe: */
317 png_ptr->zstream.avail_in = (uInt)size;
318 size = 0;
Glenn Randers-Pehrson72cda2d2010-03-06 08:18:03 -0600319 }
Glenn Randers-Pehrson33893092010-10-23 13:20:18 -0500320
Glenn Randers-Pehrson72cda2d2010-03-06 08:18:03 -0600321 else
322 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600323 png_ptr->zstream.avail_in = ZLIB_IO_MAX;
324 size -= ZLIB_IO_MAX;
Glenn Randers-Pehrson72cda2d2010-03-06 08:18:03 -0600325 }
Glenn Randers-Pehrson9d51afc2010-02-12 20:12:56 -0600326 }
327
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600328 /* Reset the output buffer each time round - we empty it
329 * after every inflate call.
330 */
331 png_ptr->zstream.next_out = png_ptr->zbuf;
332 png_ptr->zstream.avail_out = png_ptr->zbuf_size;
333
334 ret = inflate(&png_ptr->zstream, Z_NO_FLUSH);
335 avail = png_ptr->zbuf_size - png_ptr->zstream.avail_out;
336
337 /* First copy/count any new output - but only if we didn't
338 * get an error code.
339 */
340 if ((ret == Z_OK || ret == Z_STREAM_END) && avail > 0)
341 {
342 png_size_t space = avail; /* > 0, see above */
343
344 if (output != 0 && output_size > count)
345 {
346 png_size_t copy = output_size - count;
347
348 if (space < copy)
349 copy = space;
350
351 png_memcpy(output + count, png_ptr->zbuf, copy);
352 }
353 count += space;
354 }
355
356 if (ret == Z_OK)
357 continue;
358
359 /* Termination conditions - always reset the zstream, it
360 * must be left in inflateInit state.
361 */
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600362 png_ptr->zstream.avail_in = 0;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600363 inflateReset(&png_ptr->zstream);
364
365 if (ret == Z_STREAM_END)
366 return count; /* NOTE: may be zero. */
367
368 /* Now handle the error codes - the API always returns 0
369 * and the error message is dumped into the uncompressed
370 * buffer if available.
371 */
John Bowler88b77cc2011-05-05 06:49:55 -0500372# ifdef PNG_WARNINGS_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600373 {
John Bowler88b77cc2011-05-05 06:49:55 -0500374 png_const_charp msg;
375
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600376 if (png_ptr->zstream.msg != 0)
377 msg = png_ptr->zstream.msg;
378
John Bowler88b77cc2011-05-05 06:49:55 -0500379 else switch (ret)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600380 {
John Bowler88b77cc2011-05-05 06:49:55 -0500381 case Z_BUF_ERROR:
382 msg = "Buffer error in compressed datastream";
383 break;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600384
John Bowler88b77cc2011-05-05 06:49:55 -0500385 case Z_DATA_ERROR:
386 msg = "Data error in compressed datastream";
387 break;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600388
John Bowler88b77cc2011-05-05 06:49:55 -0500389 default:
390 msg = "Incomplete compressed datastream";
391 break;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600392 }
393
John Bowler88b77cc2011-05-05 06:49:55 -0500394 png_chunk_warning(png_ptr, msg);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600395 }
John Bowler88b77cc2011-05-05 06:49:55 -0500396# endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600397
398 /* 0 means an error - notice that this code simply ignores
399 * zero length compressed chunks as a result.
400 */
401 return 0;
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600402 }
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600403}
404
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600405/*
406 * Decompress trailing data in a chunk. The assumption is that chunkdata
407 * points at an allocated area holding the contents of a chunk with a
408 * trailing compressed part. What we get back is an allocated area
409 * holding the original prefix part and an uncompressed version of the
410 * trailing part (the malloc area passed in is freed).
411 */
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500412void /* PRIVATE */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500413png_decompress_chunk(png_structp png_ptr, int comp_type,
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600414 png_size_t chunklength,
415 png_size_t prefix_size, png_size_t *newlength)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600416{
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600417 /* The caller should guarantee this */
418 if (prefix_size > chunklength)
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600419 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600420 /* The recovery is to delete the chunk. */
421 png_warning(png_ptr, "invalid chunklength");
422 prefix_size = 0; /* To delete everything */
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600423 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600424
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600425 else if (comp_type == PNG_COMPRESSION_TYPE_BASE)
Glenn Randers-Pehrson9b0956f2010-02-12 11:17:22 -0600426 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600427 png_size_t expanded_size = png_inflate(png_ptr,
428 (png_bytep)(png_ptr->chunkdata + prefix_size),
429 chunklength - prefix_size,
Glenn Randers-Pehrsonb75b2412011-04-16 19:35:05 -0500430 0, /* output */
431 0); /* output size */
Glenn Randers-Pehrson9b0956f2010-02-12 11:17:22 -0600432
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600433 /* Now check the limits on this chunk - if the limit fails the
434 * compressed data will be removed, the prefix will remain.
435 */
436#ifdef PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED
437 if (png_ptr->user_chunk_malloc_max &&
438 (prefix_size + expanded_size >= png_ptr->user_chunk_malloc_max - 1))
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600439#else
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600440# ifdef PNG_USER_CHUNK_MALLOC_MAX
441 if ((PNG_USER_CHUNK_MALLOC_MAX > 0) &&
442 prefix_size + expanded_size >= PNG_USER_CHUNK_MALLOC_MAX - 1)
443# endif
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600444#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600445 png_warning(png_ptr, "Exceeded size limit while expanding chunk");
446
447 /* If the size is zero either there was an error and a message
448 * has already been output (warning) or the size really is zero
449 * and we have nothing to do - the code will exit through the
450 * error case below.
451 */
452#if defined(PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED) || \
453 defined(PNG_USER_CHUNK_MALLOC_MAX)
454 else if (expanded_size > 0)
455#else
456 if (expanded_size > 0)
457#endif
458 {
459 /* Success (maybe) - really uncompress the chunk. */
460 png_size_t new_size = 0;
461 png_charp text = png_malloc_warn(png_ptr,
462 prefix_size + expanded_size + 1);
463
464 if (text != NULL)
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600465 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600466 png_memcpy(text, png_ptr->chunkdata, prefix_size);
467 new_size = png_inflate(png_ptr,
468 (png_bytep)(png_ptr->chunkdata + prefix_size),
469 chunklength - prefix_size,
470 (png_bytep)(text + prefix_size), expanded_size);
471 text[prefix_size + expanded_size] = 0; /* just in case */
472
473 if (new_size == expanded_size)
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600474 {
475 png_free(png_ptr, png_ptr->chunkdata);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600476 png_ptr->chunkdata = text;
477 *newlength = prefix_size + expanded_size;
478 return; /* The success return! */
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600479 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600480
481 png_warning(png_ptr, "png_inflate logic error");
482 png_free(png_ptr, text);
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600483 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600484
485 else
486 png_warning(png_ptr, "Not enough memory to decompress chunk");
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600487 }
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600488 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600489
Glenn Randers-Pehrson9b0956f2010-02-12 11:17:22 -0600490 else /* if (comp_type != PNG_COMPRESSION_TYPE_BASE) */
491 {
John Bowler88b77cc2011-05-05 06:49:55 -0500492 PNG_WARNING_PARAMETERS(p)
493 png_warning_parameter_signed(p, 1, PNG_NUMBER_FORMAT_d, comp_type);
494 png_formatted_warning(png_ptr, p, "Unknown zTXt compression type @1");
Glenn Randers-Pehrson9b0956f2010-02-12 11:17:22 -0600495
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600496 /* The recovery is to simply drop the data. */
Glenn Randers-Pehrson9b0956f2010-02-12 11:17:22 -0600497 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600498
499 /* Generic error return - leave the prefix, delete the compressed
500 * data, reallocate the chunkdata to remove the potentially large
501 * amount of compressed data.
502 */
503 {
504 png_charp text = png_malloc_warn(png_ptr, prefix_size + 1);
505
506 if (text != NULL)
507 {
508 if (prefix_size > 0)
509 png_memcpy(text, png_ptr->chunkdata, prefix_size);
510
511 png_free(png_ptr, png_ptr->chunkdata);
512 png_ptr->chunkdata = text;
513
514 /* This is an extra zero in the 'uncompressed' part. */
515 *(png_ptr->chunkdata + prefix_size) = 0x00;
516 }
517 /* Ignore a malloc error here - it is safe. */
518 }
519
520 *newlength = prefix_size;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600521}
Glenn Randers-Pehrson5975f312011-04-01 13:15:36 -0500522#endif /* PNG_READ_COMPRESSED_TEXT_SUPPORTED */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600523
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500524/* Read and check the IDHR chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500525void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600526png_handle_IHDR(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500527{
528 png_byte buf[13];
529 png_uint_32 width, height;
530 int bit_depth, color_type, compression_type, filter_type;
531 int interlace_type;
532
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500533 png_debug(1, "in png_handle_IHDR");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500534
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600535 if (png_ptr->mode & PNG_HAVE_IHDR)
Guy Schalnate5a37791996-06-05 15:50:50 -0500536 png_error(png_ptr, "Out of place IHDR");
537
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500538 /* Check the length */
Guy Schalnat0d580581995-07-20 02:43:20 -0500539 if (length != 13)
Guy Schalnat6d764711995-12-19 03:22:19 -0600540 png_error(png_ptr, "Invalid IHDR chunk");
Guy Schalnat0d580581995-07-20 02:43:20 -0500541
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600542 png_ptr->mode |= PNG_HAVE_IHDR;
543
Guy Schalnat0d580581995-07-20 02:43:20 -0500544 png_crc_read(png_ptr, buf, 13);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600545 png_crc_finish(png_ptr, 0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500546
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500547 width = png_get_uint_31(png_ptr, buf);
548 height = png_get_uint_31(png_ptr, buf + 4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500549 bit_depth = buf[8];
550 color_type = buf[9];
551 compression_type = buf[10];
552 filter_type = buf[11];
553 interlace_type = buf[12];
554
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500555 /* Set internal variables */
Guy Schalnat0d580581995-07-20 02:43:20 -0500556 png_ptr->width = width;
557 png_ptr->height = height;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600558 png_ptr->bit_depth = (png_byte)bit_depth;
559 png_ptr->interlaced = (png_byte)interlace_type;
560 png_ptr->color_type = (png_byte)color_type;
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500561#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600562 png_ptr->filter_type = (png_byte)filter_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500563#endif
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500564 png_ptr->compression_type = (png_byte)compression_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500565
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500566 /* Find number of channels */
Guy Schalnat0d580581995-07-20 02:43:20 -0500567 switch (png_ptr->color_type)
568 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600569 default: /* invalid, png_set_IHDR calls png_error */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500570 case PNG_COLOR_TYPE_GRAY:
571 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnat0d580581995-07-20 02:43:20 -0500572 png_ptr->channels = 1;
573 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500574
Andreas Dilger47a0c421997-05-16 02:46:07 -0500575 case PNG_COLOR_TYPE_RGB:
Guy Schalnat0d580581995-07-20 02:43:20 -0500576 png_ptr->channels = 3;
577 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500578
Andreas Dilger47a0c421997-05-16 02:46:07 -0500579 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnat0d580581995-07-20 02:43:20 -0500580 png_ptr->channels = 2;
581 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500582
Andreas Dilger47a0c421997-05-16 02:46:07 -0500583 case PNG_COLOR_TYPE_RGB_ALPHA:
Guy Schalnat0d580581995-07-20 02:43:20 -0500584 png_ptr->channels = 4;
585 break;
586 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600587
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500588 /* Set up other useful info */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600589 png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth *
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600590 png_ptr->channels);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500591 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width);
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500592 png_debug1(3, "bit_depth = %d", png_ptr->bit_depth);
593 png_debug1(3, "channels = %d", png_ptr->channels);
Glenn Randers-Pehrsonb764c602011-01-14 21:18:37 -0600594 png_debug1(3, "rowbytes = %lu", (unsigned long)png_ptr->rowbytes);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500595 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600596 color_type, interlace_type, compression_type, filter_type);
Guy Schalnat0d580581995-07-20 02:43:20 -0500597}
598
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500599/* Read and check the palette */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500600void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600601png_handle_PLTE(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500602{
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600603 png_color palette[PNG_MAX_PALETTE_LENGTH];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600604 int num, i;
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500605#ifdef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500606 png_colorp pal_ptr;
607#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500608
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500609 png_debug(1, "in png_handle_PLTE");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500610
Guy Schalnate5a37791996-06-05 15:50:50 -0500611 if (!(png_ptr->mode & PNG_HAVE_IHDR))
612 png_error(png_ptr, "Missing IHDR before PLTE");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500613
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600614 else if (png_ptr->mode & PNG_HAVE_IDAT)
615 {
616 png_warning(png_ptr, "Invalid PLTE after IDAT");
617 png_crc_finish(png_ptr, length);
618 return;
619 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500620
Guy Schalnate5a37791996-06-05 15:50:50 -0500621 else if (png_ptr->mode & PNG_HAVE_PLTE)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600622 png_error(png_ptr, "Duplicate PLTE chunk");
623
624 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500625
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500626 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
627 {
628 png_warning(png_ptr,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600629 "Ignoring PLTE chunk in grayscale PNG");
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500630 png_crc_finish(png_ptr, length);
631 return;
632 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600633
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -0500634#ifndef PNG_READ_OPT_PLTE_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500635 if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
636 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600637 png_crc_finish(png_ptr, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500638 return;
639 }
640#endif
641
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600642 if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3)
Guy Schalnate5a37791996-06-05 15:50:50 -0500643 {
644 if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
645 {
646 png_warning(png_ptr, "Invalid palette chunk");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600647 png_crc_finish(png_ptr, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500648 return;
649 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500650
Guy Schalnate5a37791996-06-05 15:50:50 -0500651 else
652 {
653 png_error(png_ptr, "Invalid palette chunk");
654 }
655 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500656
657 num = (int)length / 3;
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500658
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500659#ifdef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500660 for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)
661 {
662 png_byte buf[3];
663
664 png_crc_read(png_ptr, buf, 3);
665 pal_ptr->red = buf[0];
666 pal_ptr->green = buf[1];
667 pal_ptr->blue = buf[2];
668 }
669#else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600670 for (i = 0; i < num; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500671 {
672 png_byte buf[3];
673
674 png_crc_read(png_ptr, buf, 3);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500675 /* Don't depend upon png_color being any order */
Guy Schalnat0d580581995-07-20 02:43:20 -0500676 palette[i].red = buf[0];
677 palette[i].green = buf[1];
678 palette[i].blue = buf[2];
679 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500680#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600681
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600682 /* 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 -0500683 * whatever the normal CRC configuration tells us. However, if we
684 * have an RGB image, the PLTE can be considered ancillary, so
685 * we will act as though it is.
686 */
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -0500687#ifndef PNG_READ_OPT_PLTE_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600688 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600689#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600690 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500691 png_crc_finish(png_ptr, 0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600692 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600693
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -0500694#ifndef PNG_READ_OPT_PLTE_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600695 else if (png_crc_error(png_ptr)) /* Only if we have a CRC error */
696 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600697 /* If we don't want to use the data from an ancillary chunk,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600698 * we have two options: an error abort, or a warning and we
699 * ignore the data in this chunk (which should be OK, since
700 * it's considered ancillary for a RGB or RGBA image).
701 */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600702 if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE))
703 {
704 if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)
705 {
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500706 png_chunk_benign_error(png_ptr, "CRC error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600707 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600708
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600709 else
710 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600711 png_chunk_warning(png_ptr, "CRC error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600712 return;
713 }
714 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600715
Andreas Dilger47a0c421997-05-16 02:46:07 -0500716 /* Otherwise, we (optionally) emit a warning and use the chunk. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600717 else if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN))
718 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600719 png_chunk_warning(png_ptr, "CRC error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600720 }
721 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600722#endif
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500723
Andreas Dilger47a0c421997-05-16 02:46:07 -0500724 png_set_PLTE(png_ptr, info_ptr, palette, num);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500725
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500726#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500727 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
728 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600729 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500730 {
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500731 if (png_ptr->num_trans > (png_uint_16)num)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500732 {
733 png_warning(png_ptr, "Truncating incorrect tRNS chunk length");
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500734 png_ptr->num_trans = (png_uint_16)num;
735 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600736
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500737 if (info_ptr->num_trans > (png_uint_16)num)
738 {
739 png_warning(png_ptr, "Truncating incorrect info tRNS chunk length");
740 info_ptr->num_trans = (png_uint_16)num;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500741 }
742 }
743 }
744#endif
745
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600746}
Guy Schalnate5a37791996-06-05 15:50:50 -0500747
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500748void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600749png_handle_IEND(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
750{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500751 png_debug(1, "in png_handle_IEND");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500752
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600753 if (!(png_ptr->mode & PNG_HAVE_IHDR) || !(png_ptr->mode & PNG_HAVE_IDAT))
754 {
755 png_error(png_ptr, "No image in file");
756 }
757
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600758 png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600759
760 if (length != 0)
761 {
762 png_warning(png_ptr, "Incorrect IEND chunk length");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600763 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600764
Andreas Dilger47a0c421997-05-16 02:46:07 -0500765 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500766
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600767 PNG_UNUSED(info_ptr) /* Quiet compiler warnings about unused info_ptr */
Guy Schalnat0d580581995-07-20 02:43:20 -0500768}
769
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500770#ifdef PNG_READ_gAMA_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500771void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600772png_handle_gAMA(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500773{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600774 png_fixed_point igamma;
Guy Schalnat0d580581995-07-20 02:43:20 -0500775 png_byte buf[4];
776
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500777 png_debug(1, "in png_handle_gAMA");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500778
Guy Schalnate5a37791996-06-05 15:50:50 -0500779 if (!(png_ptr->mode & PNG_HAVE_IHDR))
780 png_error(png_ptr, "Missing IHDR before gAMA");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600781
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600782 else if (png_ptr->mode & PNG_HAVE_IDAT)
783 {
784 png_warning(png_ptr, "Invalid gAMA after IDAT");
785 png_crc_finish(png_ptr, length);
786 return;
787 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600788
Guy Schalnate5a37791996-06-05 15:50:50 -0500789 else if (png_ptr->mode & PNG_HAVE_PLTE)
790 /* Should be an error, but we can cope with it */
791 png_warning(png_ptr, "Out of place gAMA chunk");
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -0600792
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500793 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA)
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500794#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600795 && !(info_ptr->valid & PNG_INFO_sRGB)
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -0600796#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600797 )
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600798 {
799 png_warning(png_ptr, "Duplicate gAMA chunk");
800 png_crc_finish(png_ptr, length);
801 return;
802 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500803
Guy Schalnat0d580581995-07-20 02:43:20 -0500804 if (length != 4)
805 {
Guy Schalnat69b14481996-01-10 02:56:49 -0600806 png_warning(png_ptr, "Incorrect gAMA chunk length");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600807 png_crc_finish(png_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500808 return;
809 }
810
811 png_crc_read(png_ptr, buf, 4);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600812
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600813 if (png_crc_finish(png_ptr, 0))
814 return;
815
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600816 igamma = png_get_fixed_point(NULL, buf);
Glenn Randers-Pehrson33893092010-10-23 13:20:18 -0500817
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600818 /* Check for zero gamma or an error. */
819 if (igamma <= 0)
820 {
821 png_warning(png_ptr,
822 "Ignoring gAMA chunk with out of range gamma");
823
824 return;
825 }
826
827# ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600828 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB))
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600829 {
Glenn Randers-Pehrsond029a752004-08-09 21:50:32 -0500830 if (PNG_OUT_OF_RANGE(igamma, 45500L, 500))
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600831 {
John Bowler88b77cc2011-05-05 06:49:55 -0500832 PNG_WARNING_PARAMETERS(p)
833 png_warning_parameter_signed(p, 1, PNG_NUMBER_FORMAT_fixed, igamma);
834 png_formatted_warning(png_ptr, p,
835 "Ignoring incorrect gAMA value @1 when sRGB is also present");
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600836 return;
837 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600838 }
839# endif /* PNG_READ_sRGB_SUPPORTED */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600840
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600841# ifdef PNG_READ_GAMMA_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600842 /* Gamma correction on read is supported. */
843 png_ptr->gamma = igamma;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600844# endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600845 /* And set the 'info' structure members. */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600846 png_set_gAMA_fixed(png_ptr, info_ptr, igamma);
Guy Schalnat0d580581995-07-20 02:43:20 -0500847}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500848#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500849
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500850#ifdef PNG_READ_sBIT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500851void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600852png_handle_sBIT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500853{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500854 png_size_t truelen;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600855 png_byte buf[4];
Guy Schalnat69b14481996-01-10 02:56:49 -0600856
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500857 png_debug(1, "in png_handle_sBIT");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500858
Guy Schalnat69b14481996-01-10 02:56:49 -0600859 buf[0] = buf[1] = buf[2] = buf[3] = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -0500860
Guy Schalnate5a37791996-06-05 15:50:50 -0500861 if (!(png_ptr->mode & PNG_HAVE_IHDR))
862 png_error(png_ptr, "Missing IHDR before sBIT");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600863
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600864 else if (png_ptr->mode & PNG_HAVE_IDAT)
865 {
866 png_warning(png_ptr, "Invalid sBIT after IDAT");
867 png_crc_finish(png_ptr, length);
868 return;
869 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600870
Guy Schalnate5a37791996-06-05 15:50:50 -0500871 else if (png_ptr->mode & PNG_HAVE_PLTE)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600872 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500873 /* Should be an error, but we can cope with it */
874 png_warning(png_ptr, "Out of place sBIT chunk");
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600875 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600876
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500877 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600878 {
879 png_warning(png_ptr, "Duplicate sBIT chunk");
880 png_crc_finish(png_ptr, length);
881 return;
882 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500883
Guy Schalnat0d580581995-07-20 02:43:20 -0500884 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600885 truelen = 3;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600886
Guy Schalnat0d580581995-07-20 02:43:20 -0500887 else
Andreas Dilger47a0c421997-05-16 02:46:07 -0500888 truelen = (png_size_t)png_ptr->channels;
Guy Schalnat0d580581995-07-20 02:43:20 -0500889
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500890 if (length != truelen || length > 4)
Guy Schalnat0d580581995-07-20 02:43:20 -0500891 {
Guy Schalnat69b14481996-01-10 02:56:49 -0600892 png_warning(png_ptr, "Incorrect sBIT chunk length");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600893 png_crc_finish(png_ptr, length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600894 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500895 }
896
Andreas Dilger47a0c421997-05-16 02:46:07 -0500897 png_crc_read(png_ptr, buf, truelen);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600898
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600899 if (png_crc_finish(png_ptr, 0))
900 return;
901
Guy Schalnat0d580581995-07-20 02:43:20 -0500902 if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
903 {
Guy Schalnat6d764711995-12-19 03:22:19 -0600904 png_ptr->sig_bit.red = buf[0];
905 png_ptr->sig_bit.green = buf[1];
906 png_ptr->sig_bit.blue = buf[2];
907 png_ptr->sig_bit.alpha = buf[3];
Guy Schalnat0d580581995-07-20 02:43:20 -0500908 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600909
Guy Schalnat0d580581995-07-20 02:43:20 -0500910 else
911 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600912 png_ptr->sig_bit.gray = buf[0];
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600913 png_ptr->sig_bit.red = buf[0];
914 png_ptr->sig_bit.green = buf[0];
915 png_ptr->sig_bit.blue = buf[0];
Guy Schalnat6d764711995-12-19 03:22:19 -0600916 png_ptr->sig_bit.alpha = buf[1];
Guy Schalnat0d580581995-07-20 02:43:20 -0500917 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600918
Andreas Dilger47a0c421997-05-16 02:46:07 -0500919 png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
Guy Schalnat0d580581995-07-20 02:43:20 -0500920}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500921#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500922
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500923#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500924void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600925png_handle_cHRM(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500926{
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500927 png_byte buf[32];
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600928 png_fixed_point x_white, y_white, x_red, y_red, x_green, y_green, x_blue,
929 y_blue;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600930
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500931 png_debug(1, "in png_handle_cHRM");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500932
Guy Schalnate5a37791996-06-05 15:50:50 -0500933 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600934 png_error(png_ptr, "Missing IHDR before cHRM");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600935
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600936 else if (png_ptr->mode & PNG_HAVE_IDAT)
937 {
938 png_warning(png_ptr, "Invalid cHRM after IDAT");
939 png_crc_finish(png_ptr, length);
940 return;
941 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600942
Guy Schalnate5a37791996-06-05 15:50:50 -0500943 else if (png_ptr->mode & PNG_HAVE_PLTE)
944 /* Should be an error, but we can cope with it */
945 png_warning(png_ptr, "Missing PLTE before cHRM");
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -0600946
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500947 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600948# ifdef PNG_READ_sRGB_SUPPORTED
949 && !(info_ptr->valid & PNG_INFO_sRGB)
950# endif
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -0600951 )
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600952 {
953 png_warning(png_ptr, "Duplicate cHRM chunk");
954 png_crc_finish(png_ptr, length);
955 return;
956 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500957
Guy Schalnat0d580581995-07-20 02:43:20 -0500958 if (length != 32)
959 {
Guy Schalnat69b14481996-01-10 02:56:49 -0600960 png_warning(png_ptr, "Incorrect cHRM chunk length");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600961 png_crc_finish(png_ptr, length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600962 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500963 }
964
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500965 png_crc_read(png_ptr, buf, 32);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600966
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500967 if (png_crc_finish(png_ptr, 0))
968 return;
969
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600970 x_white = png_get_fixed_point(NULL, buf);
971 y_white = png_get_fixed_point(NULL, buf + 4);
972 x_red = png_get_fixed_point(NULL, buf + 8);
973 y_red = png_get_fixed_point(NULL, buf + 12);
974 x_green = png_get_fixed_point(NULL, buf + 16);
975 y_green = png_get_fixed_point(NULL, buf + 20);
976 x_blue = png_get_fixed_point(NULL, buf + 24);
977 y_blue = png_get_fixed_point(NULL, buf + 28);
Glenn Randers-Pehrson33893092010-10-23 13:20:18 -0500978
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600979 if (x_white == PNG_FIXED_ERROR ||
980 y_white == PNG_FIXED_ERROR ||
981 x_red == PNG_FIXED_ERROR ||
982 y_red == PNG_FIXED_ERROR ||
983 x_green == PNG_FIXED_ERROR ||
984 y_green == PNG_FIXED_ERROR ||
985 x_blue == PNG_FIXED_ERROR ||
986 y_blue == PNG_FIXED_ERROR)
987 {
988 png_warning(png_ptr, "Ignoring cHRM chunk with negative chromaticities");
989 return;
990 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600991
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500992#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500993 if ((info_ptr != NULL) && (info_ptr->valid & PNG_INFO_sRGB))
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600994 {
995 if (PNG_OUT_OF_RANGE(x_white, 31270, 1000) ||
996 PNG_OUT_OF_RANGE(y_white, 32900, 1000) ||
997 PNG_OUT_OF_RANGE(x_red, 64000L, 1000) ||
998 PNG_OUT_OF_RANGE(y_red, 33000, 1000) ||
999 PNG_OUT_OF_RANGE(x_green, 30000, 1000) ||
1000 PNG_OUT_OF_RANGE(y_green, 60000L, 1000) ||
1001 PNG_OUT_OF_RANGE(x_blue, 15000, 1000) ||
1002 PNG_OUT_OF_RANGE(y_blue, 6000, 1000))
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001003 {
John Bowler88b77cc2011-05-05 06:49:55 -05001004 PNG_WARNING_PARAMETERS(p)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001005
John Bowler88b77cc2011-05-05 06:49:55 -05001006 png_warning_parameter_signed(p, 1, PNG_NUMBER_FORMAT_fixed, x_white);
1007 png_warning_parameter_signed(p, 2, PNG_NUMBER_FORMAT_fixed, y_white);
1008 png_warning_parameter_signed(p, 3, PNG_NUMBER_FORMAT_fixed, x_red);
1009 png_warning_parameter_signed(p, 4, PNG_NUMBER_FORMAT_fixed, y_red);
1010 png_warning_parameter_signed(p, 5, PNG_NUMBER_FORMAT_fixed, x_green);
1011 png_warning_parameter_signed(p, 6, PNG_NUMBER_FORMAT_fixed, y_green);
1012 png_warning_parameter_signed(p, 7, PNG_NUMBER_FORMAT_fixed, x_blue);
1013 png_warning_parameter_signed(p, 8, PNG_NUMBER_FORMAT_fixed, y_blue);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001014
John Bowler88b77cc2011-05-05 06:49:55 -05001015 png_formatted_warning(png_ptr, p,
1016 "Ignoring incorrect cHRM white(@1,@2) r(@3,@4)g(@5,@6)b(@7,@8) "
1017 "when sRGB is also present");
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001018 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001019 return;
1020 }
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001021#endif /* PNG_READ_sRGB_SUPPORTED */
1022
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001023 png_set_cHRM_fixed(png_ptr, info_ptr, x_white, y_white, x_red, y_red,
1024 x_green, y_green, x_blue, y_blue);
Guy Schalnat0d580581995-07-20 02:43:20 -05001025}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001026#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001027
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001028#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001029void /* PRIVATE */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001030png_handle_sRGB(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1031{
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001032 int intent;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001033 png_byte buf[1];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001034
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001035 png_debug(1, "in png_handle_sRGB");
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001036
1037 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1038 png_error(png_ptr, "Missing IHDR before sRGB");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001039
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001040 else if (png_ptr->mode & PNG_HAVE_IDAT)
1041 {
1042 png_warning(png_ptr, "Invalid sRGB after IDAT");
1043 png_crc_finish(png_ptr, length);
1044 return;
1045 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001046
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001047 else if (png_ptr->mode & PNG_HAVE_PLTE)
1048 /* Should be an error, but we can cope with it */
1049 png_warning(png_ptr, "Out of place sRGB chunk");
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06001050
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001051 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB))
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001052 {
1053 png_warning(png_ptr, "Duplicate sRGB chunk");
1054 png_crc_finish(png_ptr, length);
1055 return;
1056 }
1057
1058 if (length != 1)
1059 {
1060 png_warning(png_ptr, "Incorrect sRGB chunk length");
1061 png_crc_finish(png_ptr, length);
1062 return;
1063 }
1064
1065 png_crc_read(png_ptr, buf, 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001066
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001067 if (png_crc_finish(png_ptr, 0))
1068 return;
1069
1070 intent = buf[0];
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001071
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001072 /* Check for bad intent */
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001073 if (intent >= PNG_sRGB_INTENT_LAST)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001074 {
1075 png_warning(png_ptr, "Unknown sRGB intent");
1076 return;
1077 }
1078
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001079#if defined(PNG_READ_gAMA_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED)
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001080 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001081 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001082 if (PNG_OUT_OF_RANGE(info_ptr->gamma, 45500L, 500))
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06001083 {
John Bowler88b77cc2011-05-05 06:49:55 -05001084 PNG_WARNING_PARAMETERS(p)
1085
1086 png_warning_parameter_signed(p, 1, PNG_NUMBER_FORMAT_fixed,
1087 info_ptr->gamma);
1088
1089 png_formatted_warning(png_ptr, p,
1090 "Ignoring incorrect gAMA value @1 when sRGB is also present");
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06001091 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001092 }
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06001093#endif /* PNG_READ_gAMA_SUPPORTED */
1094
1095#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001096 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM))
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001097 if (PNG_OUT_OF_RANGE(info_ptr->x_white, 31270, 1000) ||
1098 PNG_OUT_OF_RANGE(info_ptr->y_white, 32900, 1000) ||
1099 PNG_OUT_OF_RANGE(info_ptr->x_red, 64000L, 1000) ||
1100 PNG_OUT_OF_RANGE(info_ptr->y_red, 33000, 1000) ||
1101 PNG_OUT_OF_RANGE(info_ptr->x_green, 30000, 1000) ||
1102 PNG_OUT_OF_RANGE(info_ptr->y_green, 60000L, 1000) ||
1103 PNG_OUT_OF_RANGE(info_ptr->x_blue, 15000, 1000) ||
1104 PNG_OUT_OF_RANGE(info_ptr->y_blue, 6000, 1000))
1105 {
1106 png_warning(png_ptr,
1107 "Ignoring incorrect cHRM value when sRGB is also present");
1108 }
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06001109#endif /* PNG_READ_cHRM_SUPPORTED */
1110
1111 png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, intent);
1112}
1113#endif /* PNG_READ_sRGB_SUPPORTED */
1114
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001115#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001116void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001117png_handle_iCCP(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1118/* Note: this does not properly handle chunks that are > 64K under DOS */
1119{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001120 png_byte compression_type;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -06001121 png_bytep pC;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001122 png_charp profile;
1123 png_uint_32 skip = 0;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001124 png_uint_32 profile_size;
1125 png_alloc_size_t profile_length;
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001126 png_size_t slength, prefix_length, data_length;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001127
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001128 png_debug(1, "in png_handle_iCCP");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001129
1130 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1131 png_error(png_ptr, "Missing IHDR before iCCP");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001132
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001133 else if (png_ptr->mode & PNG_HAVE_IDAT)
1134 {
1135 png_warning(png_ptr, "Invalid iCCP after IDAT");
1136 png_crc_finish(png_ptr, length);
1137 return;
1138 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001139
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001140 else if (png_ptr->mode & PNG_HAVE_PLTE)
1141 /* Should be an error, but we can cope with it */
1142 png_warning(png_ptr, "Out of place iCCP chunk");
1143
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001144 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_iCCP))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001145 {
1146 png_warning(png_ptr, "Duplicate iCCP chunk");
1147 png_crc_finish(png_ptr, length);
1148 return;
1149 }
1150
1151#ifdef PNG_MAX_MALLOC_64K
1152 if (length > (png_uint_32)65535L)
1153 {
1154 png_warning(png_ptr, "iCCP chunk too large to fit in memory");
1155 skip = length - (png_uint_32)65535L;
1156 length = (png_uint_32)65535L;
1157 }
1158#endif
1159
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001160 png_free(png_ptr, png_ptr->chunkdata);
1161 png_ptr->chunkdata = (png_charp)png_malloc(png_ptr, length + 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001162 slength = (png_size_t)length;
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001163 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001164
1165 if (png_crc_finish(png_ptr, skip))
1166 {
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001167 png_free(png_ptr, png_ptr->chunkdata);
1168 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001169 return;
1170 }
1171
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001172 png_ptr->chunkdata[slength] = 0x00;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001173
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001174 for (profile = png_ptr->chunkdata; *profile; profile++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001175 /* Empty loop to find end of name */ ;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001176
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001177 ++profile;
1178
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001179 /* There should be at least one zero (the compression type byte)
1180 * following the separator, and we should be on it
1181 */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001182 if (profile >= png_ptr->chunkdata + slength - 1)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001183 {
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001184 png_free(png_ptr, png_ptr->chunkdata);
1185 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001186 png_warning(png_ptr, "Malformed iCCP chunk");
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -05001187 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001188 }
1189
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001190 /* Compression_type should always be zero */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001191 compression_type = *profile++;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001192
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001193 if (compression_type)
1194 {
1195 png_warning(png_ptr, "Ignoring nonzero compression type in iCCP chunk");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001196 compression_type = 0x00; /* Reset it to zero (libpng-1.0.6 through 1.0.8
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001197 wrote nonzero) */
1198 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001199
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001200 prefix_length = profile - png_ptr->chunkdata;
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -05001201 png_decompress_chunk(png_ptr, compression_type,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001202 slength, prefix_length, &data_length);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001203
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001204 profile_length = data_length - prefix_length;
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06001205
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001206 if (prefix_length > data_length || profile_length < 4)
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001207 {
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001208 png_free(png_ptr, png_ptr->chunkdata);
1209 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001210 png_warning(png_ptr, "Profile size field missing from iCCP chunk");
1211 return;
1212 }
1213
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06001214 /* Check the profile_size recorded in the first 32 bits of the ICC profile */
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001215 pC = (png_bytep)(png_ptr->chunkdata + prefix_length);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001216 profile_size = ((*(pC )) << 24) |
1217 ((*(pC + 1)) << 16) |
1218 ((*(pC + 2)) << 8) |
1219 ((*(pC + 3)) );
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001220
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001221 /* NOTE: the following guarantees that 'profile_length' fits into 32 bits,
1222 * because profile_size is a 32 bit value.
1223 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001224 if (profile_size < profile_length)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001225 profile_length = profile_size;
1226
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001227 /* And the following guarantees that profile_size == profile_length. */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001228 if (profile_size > profile_length)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001229 {
John Bowler88b77cc2011-05-05 06:49:55 -05001230 PNG_WARNING_PARAMETERS(p)
1231
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001232 png_free(png_ptr, png_ptr->chunkdata);
1233 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001234
John Bowler88b77cc2011-05-05 06:49:55 -05001235 png_warning_parameter_unsigned(p, 1, PNG_NUMBER_FORMAT_u, profile_size);
1236 png_warning_parameter_unsigned(p, 2, PNG_NUMBER_FORMAT_u, profile_length);
1237 png_formatted_warning(png_ptr, p,
1238 "Ignoring iCCP chunk with declared size = @1 and actual length = @2");
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001239 return;
1240 }
1241
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001242 png_set_iCCP(png_ptr, info_ptr, png_ptr->chunkdata,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001243 compression_type, (png_bytep)png_ptr->chunkdata + prefix_length,
1244 profile_size);
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001245 png_free(png_ptr, png_ptr->chunkdata);
1246 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001247}
1248#endif /* PNG_READ_iCCP_SUPPORTED */
1249
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001250#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001251void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001252png_handle_sPLT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1253/* Note: this does not properly handle chunks that are > 64K under DOS */
1254{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001255 png_bytep entry_start;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001256 png_sPLT_t new_palette;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001257 png_sPLT_entryp pp;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001258 png_uint_32 data_length;
1259 int entry_size, i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001260 png_uint_32 skip = 0;
1261 png_size_t slength;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001262 png_uint_32 dl;
1263 png_size_t max_dl;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001264
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001265 png_debug(1, "in png_handle_sPLT");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001266
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001267#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson7824a702009-06-13 10:05:05 -05001268
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001269 if (png_ptr->user_chunk_cache_max != 0)
1270 {
1271 if (png_ptr->user_chunk_cache_max == 1)
1272 {
1273 png_crc_finish(png_ptr, length);
1274 return;
1275 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001276
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001277 if (--png_ptr->user_chunk_cache_max == 1)
1278 {
1279 png_warning(png_ptr, "No space in chunk cache for sPLT");
1280 png_crc_finish(png_ptr, length);
1281 return;
1282 }
1283 }
1284#endif
1285
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001286 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1287 png_error(png_ptr, "Missing IHDR before sPLT");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001288
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001289 else if (png_ptr->mode & PNG_HAVE_IDAT)
1290 {
1291 png_warning(png_ptr, "Invalid sPLT after IDAT");
1292 png_crc_finish(png_ptr, length);
1293 return;
1294 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001295
1296#ifdef PNG_MAX_MALLOC_64K
1297 if (length > (png_uint_32)65535L)
1298 {
1299 png_warning(png_ptr, "sPLT chunk too large to fit in memory");
1300 skip = length - (png_uint_32)65535L;
1301 length = (png_uint_32)65535L;
1302 }
1303#endif
1304
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001305 png_free(png_ptr, png_ptr->chunkdata);
1306 png_ptr->chunkdata = (png_charp)png_malloc(png_ptr, length + 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001307
1308 /* WARNING: this may break if size_t is less than 32 bits; it is assumed
1309 * that the PNG_MAX_MALLOC_64K test is enabled in this case, but this is a
1310 * potential breakage point if the types in pngconf.h aren't exactly right.
1311 */
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001312 slength = (png_size_t)length;
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001313 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001314
1315 if (png_crc_finish(png_ptr, skip))
1316 {
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001317 png_free(png_ptr, png_ptr->chunkdata);
1318 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001319 return;
1320 }
1321
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001322 png_ptr->chunkdata[slength] = 0x00;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001323
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001324 for (entry_start = (png_bytep)png_ptr->chunkdata; *entry_start;
1325 entry_start++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001326 /* Empty loop to find end of name */ ;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001327
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001328 ++entry_start;
1329
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001330 /* A sample depth should follow the separator, and we should be on it */
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001331 if (entry_start > (png_bytep)png_ptr->chunkdata + slength - 2)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001332 {
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001333 png_free(png_ptr, png_ptr->chunkdata);
1334 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -05001335 png_warning(png_ptr, "malformed sPLT chunk");
1336 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001337 }
1338
1339 new_palette.depth = *entry_start++;
Glenn Randers-Pehrsona565f0e2010-03-06 08:24:45 -06001340 entry_size = (new_palette.depth == 8 ? 6 : 10);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001341 /* This must fit in a png_uint_32 because it is derived from the original
1342 * chunk data length (and use 'length', not 'slength' here for clarity -
1343 * they are guaranteed to be the same, see the tests above.)
1344 */
1345 data_length = length - (png_uint_32)(entry_start -
1346 (png_bytep)png_ptr->chunkdata);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001347
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001348 /* Integrity-check the data length */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001349 if (data_length % entry_size)
1350 {
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001351 png_free(png_ptr, png_ptr->chunkdata);
1352 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001353 png_warning(png_ptr, "sPLT chunk has bad length");
1354 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001355 }
1356
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001357 dl = (png_int_32)(data_length / entry_size);
1358 max_dl = PNG_SIZE_MAX / png_sizeof(png_sPLT_entry);
1359
1360 if (dl > max_dl)
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001361 {
1362 png_warning(png_ptr, "sPLT chunk too long");
1363 return;
1364 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001365
1366 new_palette.nentries = (png_int_32)(data_length / entry_size);
1367
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001368 new_palette.entries = (png_sPLT_entryp)png_malloc_warn(
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001369 png_ptr, new_palette.nentries * png_sizeof(png_sPLT_entry));
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001370
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001371 if (new_palette.entries == NULL)
1372 {
1373 png_warning(png_ptr, "sPLT chunk requires too much memory");
1374 return;
1375 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001376
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001377#ifdef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001378 for (i = 0; i < new_palette.nentries; i++)
1379 {
Glenn Randers-Pehrson90b878c2009-10-07 12:44:35 -05001380 pp = new_palette.entries + i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001381
1382 if (new_palette.depth == 8)
1383 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001384 pp->red = *entry_start++;
1385 pp->green = *entry_start++;
1386 pp->blue = *entry_start++;
1387 pp->alpha = *entry_start++;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001388 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001389
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001390 else
1391 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001392 pp->red = png_get_uint_16(entry_start); entry_start += 2;
1393 pp->green = png_get_uint_16(entry_start); entry_start += 2;
1394 pp->blue = png_get_uint_16(entry_start); entry_start += 2;
1395 pp->alpha = png_get_uint_16(entry_start); entry_start += 2;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001396 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001397
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001398 pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
1399 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001400#else
1401 pp = new_palette.entries;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001402
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001403 for (i = 0; i < new_palette.nentries; i++)
1404 {
1405
1406 if (new_palette.depth == 8)
1407 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001408 pp[i].red = *entry_start++;
1409 pp[i].green = *entry_start++;
1410 pp[i].blue = *entry_start++;
1411 pp[i].alpha = *entry_start++;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001412 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001413
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001414 else
1415 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001416 pp[i].red = png_get_uint_16(entry_start); entry_start += 2;
1417 pp[i].green = png_get_uint_16(entry_start); entry_start += 2;
1418 pp[i].blue = png_get_uint_16(entry_start); entry_start += 2;
1419 pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001420 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001421
Glenn Randers-Pehrsonf27592a2011-03-21 18:05:40 -05001422 pp[i].frequency = png_get_uint_16(entry_start); entry_start += 2;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001423 }
1424#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001425
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001426 /* Discard all chunk data except the name and stash that */
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001427 new_palette.name = png_ptr->chunkdata;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001428
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001429 png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001430
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001431 png_free(png_ptr, png_ptr->chunkdata);
1432 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001433 png_free(png_ptr, new_palette.entries);
1434}
1435#endif /* PNG_READ_sPLT_SUPPORTED */
1436
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001437#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001438void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001439png_handle_tRNS(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001440{
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001441 png_byte readbuf[PNG_MAX_PALETTE_LENGTH];
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06001442
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001443 png_debug(1, "in png_handle_tRNS");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001444
Guy Schalnate5a37791996-06-05 15:50:50 -05001445 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1446 png_error(png_ptr, "Missing IHDR before tRNS");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001447
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001448 else if (png_ptr->mode & PNG_HAVE_IDAT)
1449 {
1450 png_warning(png_ptr, "Invalid tRNS after IDAT");
1451 png_crc_finish(png_ptr, length);
1452 return;
1453 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001454
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001455 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001456 {
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001457 png_warning(png_ptr, "Duplicate tRNS chunk");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001458 png_crc_finish(png_ptr, length);
1459 return;
1460 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001461
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001462 if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
Guy Schalnat0d580581995-07-20 02:43:20 -05001463 {
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001464 png_byte buf[2];
Guy Schalnat0d580581995-07-20 02:43:20 -05001465
1466 if (length != 2)
1467 {
Guy Schalnat69b14481996-01-10 02:56:49 -06001468 png_warning(png_ptr, "Incorrect tRNS chunk length");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001469 png_crc_finish(png_ptr, length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001470 return;
1471 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001472
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001473 png_crc_read(png_ptr, buf, 2);
1474 png_ptr->num_trans = 1;
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -05001475 png_ptr->trans_color.gray = png_get_uint_16(buf);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001476 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001477
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001478 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
1479 {
1480 png_byte buf[6];
1481
1482 if (length != 6)
1483 {
1484 png_warning(png_ptr, "Incorrect tRNS chunk length");
1485 png_crc_finish(png_ptr, length);
1486 return;
1487 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001488
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001489 png_crc_read(png_ptr, buf, (png_size_t)length);
1490 png_ptr->num_trans = 1;
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -05001491 png_ptr->trans_color.red = png_get_uint_16(buf);
1492 png_ptr->trans_color.green = png_get_uint_16(buf + 2);
1493 png_ptr->trans_color.blue = png_get_uint_16(buf + 4);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001494 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001495
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001496 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1497 {
1498 if (!(png_ptr->mode & PNG_HAVE_PLTE))
1499 {
1500 /* Should be an error, but we can cope with it. */
1501 png_warning(png_ptr, "Missing PLTE before tRNS");
1502 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001503
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001504 if (length > (png_uint_32)png_ptr->num_palette ||
1505 length > PNG_MAX_PALETTE_LENGTH)
1506 {
1507 png_warning(png_ptr, "Incorrect tRNS chunk length");
1508 png_crc_finish(png_ptr, length);
1509 return;
1510 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001511
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001512 if (length == 0)
1513 {
1514 png_warning(png_ptr, "Zero length tRNS chunk");
1515 png_crc_finish(png_ptr, length);
1516 return;
1517 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001518
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001519 png_crc_read(png_ptr, readbuf, (png_size_t)length);
1520 png_ptr->num_trans = (png_uint_16)length;
1521 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001522
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001523 else
Guy Schalnate5a37791996-06-05 15:50:50 -05001524 {
1525 png_warning(png_ptr, "tRNS chunk not allowed with alpha channel");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001526 png_crc_finish(png_ptr, length);
Guy Schalnate5a37791996-06-05 15:50:50 -05001527 return;
1528 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001529
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001530 if (png_crc_finish(png_ptr, 0))
Glenn Randers-Pehrsona7dbcba2007-05-15 16:16:34 -05001531 {
1532 png_ptr->num_trans = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001533 return;
Glenn Randers-Pehrsona7dbcba2007-05-15 16:16:34 -05001534 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001535
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06001536 png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001537 &(png_ptr->trans_color));
Guy Schalnat0d580581995-07-20 02:43:20 -05001538}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001539#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001540
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001541#ifdef PNG_READ_bKGD_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001542void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001543png_handle_bKGD(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001544{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001545 png_size_t truelen;
Guy Schalnat0d580581995-07-20 02:43:20 -05001546 png_byte buf[6];
1547
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001548 png_debug(1, "in png_handle_bKGD");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001549
Guy Schalnate5a37791996-06-05 15:50:50 -05001550 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1551 png_error(png_ptr, "Missing IHDR before bKGD");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001552
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001553 else if (png_ptr->mode & PNG_HAVE_IDAT)
1554 {
1555 png_warning(png_ptr, "Invalid bKGD after IDAT");
1556 png_crc_finish(png_ptr, length);
1557 return;
1558 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001559
Guy Schalnate5a37791996-06-05 15:50:50 -05001560 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001561 !(png_ptr->mode & PNG_HAVE_PLTE))
Guy Schalnate5a37791996-06-05 15:50:50 -05001562 {
1563 png_warning(png_ptr, "Missing PLTE before bKGD");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001564 png_crc_finish(png_ptr, length);
1565 return;
1566 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001567
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001568 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001569 {
1570 png_warning(png_ptr, "Duplicate bKGD chunk");
1571 png_crc_finish(png_ptr, length);
Guy Schalnate5a37791996-06-05 15:50:50 -05001572 return;
1573 }
1574
Guy Schalnat0d580581995-07-20 02:43:20 -05001575 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1576 truelen = 1;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001577
Guy Schalnat0d580581995-07-20 02:43:20 -05001578 else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
1579 truelen = 6;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001580
Guy Schalnat0d580581995-07-20 02:43:20 -05001581 else
1582 truelen = 2;
1583
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001584 if (length != truelen)
Guy Schalnat0d580581995-07-20 02:43:20 -05001585 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001586 png_warning(png_ptr, "Incorrect bKGD chunk length");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001587 png_crc_finish(png_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -05001588 return;
1589 }
1590
Andreas Dilger47a0c421997-05-16 02:46:07 -05001591 png_crc_read(png_ptr, buf, truelen);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001592
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001593 if (png_crc_finish(png_ptr, 0))
1594 return;
1595
Guy Schalnate5a37791996-06-05 15:50:50 -05001596 /* We convert the index value into RGB components so that we can allow
1597 * arbitrary RGB values for background when we have transparency, and
1598 * so it is easy to determine the RGB values of the background color
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001599 * from the info_ptr struct.
1600 */
Guy Schalnat0d580581995-07-20 02:43:20 -05001601 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Guy Schalnate5a37791996-06-05 15:50:50 -05001602 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001603 png_ptr->background.index = buf[0];
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001604
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001605 if (info_ptr && info_ptr->num_palette)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001606 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001607 if (buf[0] >= info_ptr->num_palette)
1608 {
1609 png_warning(png_ptr, "Incorrect bKGD chunk index value");
1610 return;
1611 }
1612
1613 png_ptr->background.red =
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001614 (png_uint_16)png_ptr->palette[buf[0]].red;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001615
1616 png_ptr->background.green =
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001617 (png_uint_16)png_ptr->palette[buf[0]].green;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001618
1619 png_ptr->background.blue =
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001620 (png_uint_16)png_ptr->palette[buf[0]].blue;
1621 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001622 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001623
Andreas Dilger47a0c421997-05-16 02:46:07 -05001624 else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) /* GRAY */
Guy Schalnate5a37791996-06-05 15:50:50 -05001625 {
1626 png_ptr->background.red =
1627 png_ptr->background.green =
1628 png_ptr->background.blue =
Guy Schalnat0d580581995-07-20 02:43:20 -05001629 png_ptr->background.gray = png_get_uint_16(buf);
Guy Schalnate5a37791996-06-05 15:50:50 -05001630 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001631
Guy Schalnat0d580581995-07-20 02:43:20 -05001632 else
1633 {
1634 png_ptr->background.red = png_get_uint_16(buf);
1635 png_ptr->background.green = png_get_uint_16(buf + 2);
1636 png_ptr->background.blue = png_get_uint_16(buf + 4);
1637 }
1638
Andreas Dilger47a0c421997-05-16 02:46:07 -05001639 png_set_bKGD(png_ptr, info_ptr, &(png_ptr->background));
Guy Schalnat0d580581995-07-20 02:43:20 -05001640}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001641#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001642
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001643#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001644void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001645png_handle_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001646{
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001647 unsigned int num, i;
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001648 png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];
Guy Schalnat0d580581995-07-20 02:43:20 -05001649
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001650 png_debug(1, "in png_handle_hIST");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001651
Guy Schalnate5a37791996-06-05 15:50:50 -05001652 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1653 png_error(png_ptr, "Missing IHDR before hIST");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001654
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001655 else if (png_ptr->mode & PNG_HAVE_IDAT)
1656 {
1657 png_warning(png_ptr, "Invalid hIST after IDAT");
1658 png_crc_finish(png_ptr, length);
1659 return;
1660 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001661
Guy Schalnate5a37791996-06-05 15:50:50 -05001662 else if (!(png_ptr->mode & PNG_HAVE_PLTE))
1663 {
1664 png_warning(png_ptr, "Missing PLTE before hIST");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001665 png_crc_finish(png_ptr, length);
1666 return;
1667 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001668
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001669 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001670 {
1671 png_warning(png_ptr, "Duplicate hIST chunk");
1672 png_crc_finish(png_ptr, length);
Guy Schalnate5a37791996-06-05 15:50:50 -05001673 return;
1674 }
1675
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001676 num = length / 2 ;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001677
1678 if (num != (unsigned int)png_ptr->num_palette || num >
1679 (unsigned int)PNG_MAX_PALETTE_LENGTH)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001680 {
1681 png_warning(png_ptr, "Incorrect hIST chunk length");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001682 png_crc_finish(png_ptr, length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001683 return;
1684 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001685
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001686 for (i = 0; i < num; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001687 {
1688 png_byte buf[2];
1689
1690 png_crc_read(png_ptr, buf, 2);
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06001691 readbuf[i] = png_get_uint_16(buf);
Guy Schalnat0d580581995-07-20 02:43:20 -05001692 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001693
1694 if (png_crc_finish(png_ptr, 0))
1695 return;
1696
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06001697 png_set_hIST(png_ptr, info_ptr, readbuf);
Guy Schalnat0d580581995-07-20 02:43:20 -05001698}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001699#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001700
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001701#ifdef PNG_READ_pHYs_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001702void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001703png_handle_pHYs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001704{
1705 png_byte buf[9];
1706 png_uint_32 res_x, res_y;
1707 int unit_type;
1708
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001709 png_debug(1, "in png_handle_pHYs");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001710
Guy Schalnate5a37791996-06-05 15:50:50 -05001711 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001712 png_error(png_ptr, "Missing IHDR before pHYs");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001713
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001714 else if (png_ptr->mode & PNG_HAVE_IDAT)
1715 {
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001716 png_warning(png_ptr, "Invalid pHYs after IDAT");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001717 png_crc_finish(png_ptr, length);
1718 return;
1719 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001720
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001721 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001722 {
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001723 png_warning(png_ptr, "Duplicate pHYs chunk");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001724 png_crc_finish(png_ptr, length);
1725 return;
1726 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001727
Guy Schalnat0d580581995-07-20 02:43:20 -05001728 if (length != 9)
1729 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001730 png_warning(png_ptr, "Incorrect pHYs chunk length");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001731 png_crc_finish(png_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -05001732 return;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001733 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001734
1735 png_crc_read(png_ptr, buf, 9);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001736
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001737 if (png_crc_finish(png_ptr, 0))
1738 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001739
1740 res_x = png_get_uint_32(buf);
1741 res_y = png_get_uint_32(buf + 4);
1742 unit_type = buf[8];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001743 png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
Guy Schalnat0d580581995-07-20 02:43:20 -05001744}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001745#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001746
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001747#ifdef PNG_READ_oFFs_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001748void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001749png_handle_oFFs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001750{
1751 png_byte buf[9];
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001752 png_int_32 offset_x, offset_y;
Guy Schalnat0d580581995-07-20 02:43:20 -05001753 int unit_type;
1754
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001755 png_debug(1, "in png_handle_oFFs");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001756
Guy Schalnate5a37791996-06-05 15:50:50 -05001757 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1758 png_error(png_ptr, "Missing IHDR before oFFs");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001759
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001760 else if (png_ptr->mode & PNG_HAVE_IDAT)
1761 {
1762 png_warning(png_ptr, "Invalid oFFs after IDAT");
1763 png_crc_finish(png_ptr, length);
1764 return;
1765 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001766
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001767 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001768 {
1769 png_warning(png_ptr, "Duplicate oFFs chunk");
1770 png_crc_finish(png_ptr, length);
1771 return;
1772 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001773
Guy Schalnat0d580581995-07-20 02:43:20 -05001774 if (length != 9)
1775 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001776 png_warning(png_ptr, "Incorrect oFFs chunk length");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001777 png_crc_finish(png_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -05001778 return;
1779 }
1780
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001781 png_crc_read(png_ptr, buf, 9);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001782
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001783 if (png_crc_finish(png_ptr, 0))
1784 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001785
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001786 offset_x = png_get_int_32(buf);
1787 offset_y = png_get_int_32(buf + 4);
Guy Schalnat0d580581995-07-20 02:43:20 -05001788 unit_type = buf[8];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001789 png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
1790}
1791#endif
1792
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001793#ifdef PNG_READ_pCAL_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001794/* Read the pCAL chunk (described in the PNG Extensions document) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001795void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001796png_handle_pCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1797{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001798 png_int_32 X0, X1;
1799 png_byte type, nparams;
1800 png_charp buf, units, endptr;
1801 png_charpp params;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001802 png_size_t slength;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001803 int i;
1804
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001805 png_debug(1, "in png_handle_pCAL");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001806
1807 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1808 png_error(png_ptr, "Missing IHDR before pCAL");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001809
Andreas Dilger47a0c421997-05-16 02:46:07 -05001810 else if (png_ptr->mode & PNG_HAVE_IDAT)
1811 {
1812 png_warning(png_ptr, "Invalid pCAL after IDAT");
1813 png_crc_finish(png_ptr, length);
1814 return;
1815 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001816
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001817 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001818 {
1819 png_warning(png_ptr, "Duplicate pCAL chunk");
1820 png_crc_finish(png_ptr, length);
1821 return;
1822 }
1823
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001824 png_debug1(2, "Allocating and reading pCAL chunk data (%u bytes)",
1825 length + 1);
Glenn Randers-Pehrsond8d7b942008-07-21 10:34:34 -05001826 png_free(png_ptr, png_ptr->chunkdata);
1827 png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001828
Glenn Randers-Pehrsond8d7b942008-07-21 10:34:34 -05001829 if (png_ptr->chunkdata == NULL)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001830 {
1831 png_warning(png_ptr, "No memory for pCAL purpose");
1832 return;
1833 }
1834
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001835 slength = (png_size_t)length;
Glenn Randers-Pehrsond8d7b942008-07-21 10:34:34 -05001836 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001837
1838 if (png_crc_finish(png_ptr, 0))
1839 {
Glenn Randers-Pehrsond8d7b942008-07-21 10:34:34 -05001840 png_free(png_ptr, png_ptr->chunkdata);
1841 png_ptr->chunkdata = NULL;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001842 return;
1843 }
1844
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001845 png_ptr->chunkdata[slength] = 0x00; /* Null terminate the last string */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001846
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001847 png_debug(3, "Finding end of pCAL purpose string");
Glenn Randers-Pehrsond8d7b942008-07-21 10:34:34 -05001848 for (buf = png_ptr->chunkdata; *buf; buf++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001849 /* Empty loop */ ;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001850
Glenn Randers-Pehrsond8d7b942008-07-21 10:34:34 -05001851 endptr = png_ptr->chunkdata + slength;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001852
1853 /* We need to have at least 12 bytes after the purpose string
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001854 * in order to get the parameter information.
1855 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001856 if (endptr <= buf + 12)
1857 {
1858 png_warning(png_ptr, "Invalid pCAL data");
Glenn Randers-Pehrsond8d7b942008-07-21 10:34:34 -05001859 png_free(png_ptr, png_ptr->chunkdata);
1860 png_ptr->chunkdata = NULL;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001861 return;
1862 }
1863
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001864 png_debug(3, "Reading pCAL X0, X1, type, nparams, and units");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001865 X0 = png_get_int_32((png_bytep)buf+1);
1866 X1 = png_get_int_32((png_bytep)buf+5);
1867 type = buf[9];
1868 nparams = buf[10];
1869 units = buf + 11;
1870
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001871 png_debug(3, "Checking pCAL equation type and number of parameters");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001872 /* Check that we have the right number of parameters for known
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001873 * equation types.
1874 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001875 if ((type == PNG_EQUATION_LINEAR && nparams != 2) ||
1876 (type == PNG_EQUATION_BASE_E && nparams != 3) ||
1877 (type == PNG_EQUATION_ARBITRARY && nparams != 3) ||
1878 (type == PNG_EQUATION_HYPERBOLIC && nparams != 4))
1879 {
1880 png_warning(png_ptr, "Invalid pCAL parameters for equation type");
Glenn Randers-Pehrsond8d7b942008-07-21 10:34:34 -05001881 png_free(png_ptr, png_ptr->chunkdata);
1882 png_ptr->chunkdata = NULL;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001883 return;
1884 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001885
Andreas Dilger47a0c421997-05-16 02:46:07 -05001886 else if (type >= PNG_EQUATION_LAST)
1887 {
1888 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1889 }
1890
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001891 for (buf = units; *buf; buf++)
Glenn Randers-Pehrsonf9f2fe01998-03-15 18:20:23 -06001892 /* Empty loop to move past the units string. */ ;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001893
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001894 png_debug(3, "Allocating pCAL parameters array");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001895
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001896 params = (png_charpp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001897 (png_size_t)(nparams * png_sizeof(png_charp)));
1898
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001899 if (params == NULL)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001900 {
1901 png_free(png_ptr, png_ptr->chunkdata);
1902 png_ptr->chunkdata = NULL;
1903 png_warning(png_ptr, "No memory for pCAL params");
1904 return;
1905 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001906
1907 /* Get pointers to the start of each parameter string. */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001908 for (i = 0; i < (int)nparams; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001909 {
1910 buf++; /* Skip the null string terminator from previous parameter. */
1911
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001912 png_debug1(3, "Reading pCAL parameter %d", i);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001913
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001914 for (params[i] = buf; buf <= endptr && *buf != 0x00; buf++)
Glenn Randers-Pehrsonf9f2fe01998-03-15 18:20:23 -06001915 /* Empty loop to move past each parameter string */ ;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001916
1917 /* Make sure we haven't run out of data yet */
1918 if (buf > endptr)
1919 {
1920 png_warning(png_ptr, "Invalid pCAL data");
Glenn Randers-Pehrsond8d7b942008-07-21 10:34:34 -05001921 png_free(png_ptr, png_ptr->chunkdata);
1922 png_ptr->chunkdata = NULL;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001923 png_free(png_ptr, params);
1924 return;
1925 }
1926 }
1927
Glenn Randers-Pehrsond8d7b942008-07-21 10:34:34 -05001928 png_set_pCAL(png_ptr, info_ptr, png_ptr->chunkdata, X0, X1, type, nparams,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001929 units, params);
1930
Glenn Randers-Pehrsond8d7b942008-07-21 10:34:34 -05001931 png_free(png_ptr, png_ptr->chunkdata);
1932 png_ptr->chunkdata = NULL;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001933 png_free(png_ptr, params);
Guy Schalnat0d580581995-07-20 02:43:20 -05001934}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001935#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001936
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001937#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001938/* Read the sCAL chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001939void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001940png_handle_sCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1941{
John Bowler168a4332011-01-16 19:32:22 -06001942 png_size_t slength, i;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001943 int state;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001944
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001945 png_debug(1, "in png_handle_sCAL");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001946
1947 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1948 png_error(png_ptr, "Missing IHDR before sCAL");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001949
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001950 else if (png_ptr->mode & PNG_HAVE_IDAT)
1951 {
1952 png_warning(png_ptr, "Invalid sCAL after IDAT");
1953 png_crc_finish(png_ptr, length);
1954 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_sCAL))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001958 {
1959 png_warning(png_ptr, "Duplicate sCAL chunk");
1960 png_crc_finish(png_ptr, length);
1961 return;
1962 }
1963
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001964 png_debug1(2, "Allocating and reading sCAL chunk data (%u bytes)",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001965 length + 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001966
Glenn Randers-Pehrsond8d7b942008-07-21 10:34:34 -05001967 png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001968
Glenn Randers-Pehrsond8d7b942008-07-21 10:34:34 -05001969 if (png_ptr->chunkdata == NULL)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001970 {
1971 png_warning(png_ptr, "Out of memory while processing sCAL chunk");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001972 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001973 return;
1974 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001975
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001976 slength = (png_size_t)length;
Glenn Randers-Pehrsond8d7b942008-07-21 10:34:34 -05001977 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001978 png_ptr->chunkdata[slength] = 0x00; /* Null terminate the last string */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001979
1980 if (png_crc_finish(png_ptr, 0))
1981 {
Glenn Randers-Pehrsond8d7b942008-07-21 10:34:34 -05001982 png_free(png_ptr, png_ptr->chunkdata);
1983 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001984 return;
1985 }
1986
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001987 /* Validate the unit. */
1988 if (png_ptr->chunkdata[0] != 1 && png_ptr->chunkdata[0] != 2)
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -05001989 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001990 png_warning(png_ptr, "Invalid sCAL ignored: invalid unit");
Glenn Randers-Pehrsonef3831a2010-06-22 13:03:32 -05001991 png_free(png_ptr, png_ptr->chunkdata);
1992 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001993 return;
1994 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001995
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001996 /* Validate the ASCII numbers, need two ASCII numbers separated by
1997 * a '\0' and they need to fit exactly in the chunk data.
1998 */
John Bowler168a4332011-01-16 19:32:22 -06001999 i = 0;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002000 state = 0;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002001
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002002 if (png_ptr->chunkdata[1] == 45 /* negative width */ ||
John Bowler168a4332011-01-16 19:32:22 -06002003 !png_check_fp_number(png_ptr->chunkdata, slength, &state, &i) ||
2004 i >= slength || png_ptr->chunkdata[i++] != 0)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002005 png_warning(png_ptr, "Invalid sCAL chunk ignored: bad width format");
2006
2007 else
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -06002008 {
John Bowler168a4332011-01-16 19:32:22 -06002009 png_size_t heighti = i;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002010
John Bowler168a4332011-01-16 19:32:22 -06002011 if (png_ptr->chunkdata[i] == 45 /* negative height */ ||
2012 !png_check_fp_number(png_ptr->chunkdata, slength, &state, &i) ||
2013 i != slength)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002014 png_warning(png_ptr, "Invalid sCAL chunk ignored: bad height format");
2015
2016 else
2017 /* This is the (only) success case. */
2018 png_set_sCAL_s(png_ptr, info_ptr, png_ptr->chunkdata[0],
2019 png_ptr->chunkdata+1, png_ptr->chunkdata+heighti);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002020 }
2021
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002022 /* Clean up - just free the temporarily allocated buffer. */
Glenn Randers-Pehrsond8d7b942008-07-21 10:34:34 -05002023 png_free(png_ptr, png_ptr->chunkdata);
2024 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002025}
2026#endif
2027
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002028#ifdef PNG_READ_tIME_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002029void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002030png_handle_tIME(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002031{
2032 png_byte buf[7];
2033 png_time mod_time;
2034
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002035 png_debug(1, "in png_handle_tIME");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002036
Guy Schalnate5a37791996-06-05 15:50:50 -05002037 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002038 png_error(png_ptr, "Out of place tIME chunk");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002039
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002040 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002041 {
2042 png_warning(png_ptr, "Duplicate tIME chunk");
2043 png_crc_finish(png_ptr, length);
2044 return;
2045 }
2046
2047 if (png_ptr->mode & PNG_HAVE_IDAT)
2048 png_ptr->mode |= PNG_AFTER_IDAT;
Guy Schalnate5a37791996-06-05 15:50:50 -05002049
Guy Schalnat0d580581995-07-20 02:43:20 -05002050 if (length != 7)
2051 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002052 png_warning(png_ptr, "Incorrect tIME chunk length");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002053 png_crc_finish(png_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -05002054 return;
2055 }
2056
2057 png_crc_read(png_ptr, buf, 7);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002058
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002059 if (png_crc_finish(png_ptr, 0))
2060 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05002061
2062 mod_time.second = buf[6];
2063 mod_time.minute = buf[5];
2064 mod_time.hour = buf[4];
2065 mod_time.day = buf[3];
2066 mod_time.month = buf[2];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002067 mod_time.year = png_get_uint_16(buf);
Guy Schalnat0d580581995-07-20 02:43:20 -05002068
Andreas Dilger47a0c421997-05-16 02:46:07 -05002069 png_set_tIME(png_ptr, info_ptr, &mod_time);
Guy Schalnat0d580581995-07-20 02:43:20 -05002070}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002071#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002072
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002073#ifdef PNG_READ_tEXt_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002074/* Note: this does not properly handle chunks that are > 64K under DOS */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002075void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002076png_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002077{
Andreas Dilger47a0c421997-05-16 02:46:07 -05002078 png_textp text_ptr;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002079 png_charp key;
Guy Schalnat6d764711995-12-19 03:22:19 -06002080 png_charp text;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002081 png_uint_32 skip = 0;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06002082 png_size_t slength;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002083 int ret;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002084
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002085 png_debug(1, "in png_handle_tEXt");
Guy Schalnat0d580581995-07-20 02:43:20 -05002086
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06002087#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002088 if (png_ptr->user_chunk_cache_max != 0)
2089 {
2090 if (png_ptr->user_chunk_cache_max == 1)
2091 {
2092 png_crc_finish(png_ptr, length);
2093 return;
2094 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002095
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002096 if (--png_ptr->user_chunk_cache_max == 1)
2097 {
2098 png_warning(png_ptr, "No space in chunk cache for tEXt");
2099 png_crc_finish(png_ptr, length);
2100 return;
2101 }
2102 }
2103#endif
2104
Guy Schalnate5a37791996-06-05 15:50:50 -05002105 if (!(png_ptr->mode & PNG_HAVE_IHDR))
2106 png_error(png_ptr, "Missing IHDR before tEXt");
2107
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002108 if (png_ptr->mode & PNG_HAVE_IDAT)
2109 png_ptr->mode |= PNG_AFTER_IDAT;
2110
Andreas Dilger47a0c421997-05-16 02:46:07 -05002111#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002112 if (length > (png_uint_32)65535L)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002113 {
2114 png_warning(png_ptr, "tEXt chunk too large to fit in memory");
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002115 skip = length - (png_uint_32)65535L;
2116 length = (png_uint_32)65535L;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002117 }
2118#endif
2119
Glenn Randers-Pehrson97a9b482008-10-25 20:03:28 -05002120 png_free(png_ptr, png_ptr->chunkdata);
2121
2122 png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002123
Glenn Randers-Pehrson97a9b482008-10-25 20:03:28 -05002124 if (png_ptr->chunkdata == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002125 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05002126 png_warning(png_ptr, "No memory to process text chunk");
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002127 return;
2128 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002129
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06002130 slength = (png_size_t)length;
Glenn Randers-Pehrson97a9b482008-10-25 20:03:28 -05002131 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002132
2133 if (png_crc_finish(png_ptr, skip))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002134 {
Glenn Randers-Pehrson97a9b482008-10-25 20:03:28 -05002135 png_free(png_ptr, png_ptr->chunkdata);
2136 png_ptr->chunkdata = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002137 return;
2138 }
2139
Glenn Randers-Pehrson97a9b482008-10-25 20:03:28 -05002140 key = png_ptr->chunkdata;
2141
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06002142 key[slength] = 0x00;
Guy Schalnat0d580581995-07-20 02:43:20 -05002143
2144 for (text = key; *text; text++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002145 /* Empty loop to find end of key */ ;
Guy Schalnat0d580581995-07-20 02:43:20 -05002146
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06002147 if (text != key + slength)
Guy Schalnat0d580581995-07-20 02:43:20 -05002148 text++;
2149
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002150 text_ptr = (png_textp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002151 png_sizeof(png_text));
2152
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002153 if (text_ptr == NULL)
2154 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002155 png_warning(png_ptr, "Not enough memory to process text chunk");
2156 png_free(png_ptr, png_ptr->chunkdata);
2157 png_ptr->chunkdata = NULL;
2158 return;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002159 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002160
Andreas Dilger47a0c421997-05-16 02:46:07 -05002161 text_ptr->compression = PNG_TEXT_COMPRESSION_NONE;
2162 text_ptr->key = key;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002163 text_ptr->lang = NULL;
2164 text_ptr->lang_key = NULL;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002165 text_ptr->itxt_length = 0;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002166 text_ptr->text = text;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002167 text_ptr->text_length = png_strlen(text);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002168
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002169 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002170
Glenn Randers-Pehrson97a9b482008-10-25 20:03:28 -05002171 png_free(png_ptr, png_ptr->chunkdata);
2172 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05002173 png_free(png_ptr, text_ptr);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002174
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002175 if (ret)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002176 png_warning(png_ptr, "Insufficient memory to process text chunk");
Guy Schalnat0d580581995-07-20 02:43:20 -05002177}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002178#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002179
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002180#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002181/* Note: this does not correctly handle chunks that are > 64K under DOS */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002182void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002183png_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002184{
Andreas Dilger47a0c421997-05-16 02:46:07 -05002185 png_textp text_ptr;
Guy Schalnat6d764711995-12-19 03:22:19 -06002186 png_charp text;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06002187 int comp_type;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002188 int ret;
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06002189 png_size_t slength, prefix_len, data_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002190
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002191 png_debug(1, "in png_handle_zTXt");
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002192
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06002193#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002194 if (png_ptr->user_chunk_cache_max != 0)
2195 {
2196 if (png_ptr->user_chunk_cache_max == 1)
2197 {
2198 png_crc_finish(png_ptr, length);
2199 return;
2200 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002201
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002202 if (--png_ptr->user_chunk_cache_max == 1)
2203 {
2204 png_warning(png_ptr, "No space in chunk cache for zTXt");
2205 png_crc_finish(png_ptr, length);
2206 return;
2207 }
2208 }
2209#endif
2210
Guy Schalnate5a37791996-06-05 15:50:50 -05002211 if (!(png_ptr->mode & PNG_HAVE_IHDR))
2212 png_error(png_ptr, "Missing IHDR before zTXt");
2213
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002214 if (png_ptr->mode & PNG_HAVE_IDAT)
2215 png_ptr->mode |= PNG_AFTER_IDAT;
2216
Andreas Dilger47a0c421997-05-16 02:46:07 -05002217#ifdef PNG_MAX_MALLOC_64K
2218 /* We will no doubt have problems with chunks even half this size, but
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002219 * there is no hard and fast rule to tell us where to stop.
2220 */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002221 if (length > (png_uint_32)65535L)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002222 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002223 png_warning(png_ptr, "zTXt chunk too large to fit in memory");
2224 png_crc_finish(png_ptr, length);
2225 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002226 }
2227#endif
2228
Glenn Randers-Pehrson97a9b482008-10-25 20:03:28 -05002229 png_free(png_ptr, png_ptr->chunkdata);
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002230 png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002231
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002232 if (png_ptr->chunkdata == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002233 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002234 png_warning(png_ptr, "Out of memory processing zTXt chunk");
2235 return;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002236 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002237
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002238 slength = (png_size_t)length;
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002239 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002240
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002241 if (png_crc_finish(png_ptr, 0))
2242 {
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002243 png_free(png_ptr, png_ptr->chunkdata);
2244 png_ptr->chunkdata = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002245 return;
2246 }
2247
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002248 png_ptr->chunkdata[slength] = 0x00;
Guy Schalnat0d580581995-07-20 02:43:20 -05002249
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002250 for (text = png_ptr->chunkdata; *text; text++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002251 /* Empty loop */ ;
Guy Schalnat0d580581995-07-20 02:43:20 -05002252
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002253 /* zTXt must have some text after the chunkdataword */
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002254 if (text >= png_ptr->chunkdata + slength - 2)
Guy Schalnat0d580581995-07-20 02:43:20 -05002255 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002256 png_warning(png_ptr, "Truncated zTXt chunk");
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002257 png_free(png_ptr, png_ptr->chunkdata);
2258 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002259 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05002260 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002261
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002262 else
Guy Schalnat0d580581995-07-20 02:43:20 -05002263 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002264 comp_type = *(++text);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002265
Glenn Randers-Pehrsonf05f8032000-12-23 14:27:39 -06002266 if (comp_type != PNG_TEXT_COMPRESSION_zTXt)
2267 {
2268 png_warning(png_ptr, "Unknown compression type in zTXt chunk");
2269 comp_type = PNG_TEXT_COMPRESSION_zTXt;
2270 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002271
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002272 text++; /* Skip the compression_method byte */
Guy Schalnat0d580581995-07-20 02:43:20 -05002273 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002274
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002275 prefix_len = text - png_ptr->chunkdata;
Guy Schalnat0d580581995-07-20 02:43:20 -05002276
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -05002277 png_decompress_chunk(png_ptr, comp_type,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002278 (png_size_t)length, prefix_len, &data_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002279
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002280 text_ptr = (png_textp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002281 png_sizeof(png_text));
2282
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002283 if (text_ptr == NULL)
2284 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002285 png_warning(png_ptr, "Not enough memory to process zTXt chunk");
2286 png_free(png_ptr, png_ptr->chunkdata);
2287 png_ptr->chunkdata = NULL;
2288 return;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002289 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002290
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05002291 text_ptr->compression = comp_type;
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002292 text_ptr->key = png_ptr->chunkdata;
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05002293 text_ptr->lang = NULL;
2294 text_ptr->lang_key = NULL;
2295 text_ptr->itxt_length = 0;
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002296 text_ptr->text = png_ptr->chunkdata + prefix_len;
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05002297 text_ptr->text_length = data_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002298
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002299 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002300
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05002301 png_free(png_ptr, text_ptr);
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002302 png_free(png_ptr, png_ptr->chunkdata);
2303 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002304
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002305 if (ret)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002306 png_error(png_ptr, "Insufficient memory to store zTXt chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002307}
2308#endif
2309
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002310#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002311/* Note: this does not correctly handle chunks that are > 64K under DOS */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002312void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002313png_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
2314{
2315 png_textp text_ptr;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002316 png_charp key, lang, text, lang_key;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06002317 int comp_flag;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002318 int comp_type = 0;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002319 int ret;
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06002320 png_size_t slength, prefix_len, data_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002321
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002322 png_debug(1, "in png_handle_iTXt");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002323
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06002324#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002325 if (png_ptr->user_chunk_cache_max != 0)
2326 {
2327 if (png_ptr->user_chunk_cache_max == 1)
2328 {
2329 png_crc_finish(png_ptr, length);
2330 return;
2331 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002332
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002333 if (--png_ptr->user_chunk_cache_max == 1)
2334 {
2335 png_warning(png_ptr, "No space in chunk cache for iTXt");
2336 png_crc_finish(png_ptr, length);
2337 return;
2338 }
2339 }
2340#endif
2341
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002342 if (!(png_ptr->mode & PNG_HAVE_IHDR))
2343 png_error(png_ptr, "Missing IHDR before iTXt");
2344
2345 if (png_ptr->mode & PNG_HAVE_IDAT)
2346 png_ptr->mode |= PNG_AFTER_IDAT;
2347
2348#ifdef PNG_MAX_MALLOC_64K
2349 /* We will no doubt have problems with chunks even half this size, but
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002350 * there is no hard and fast rule to tell us where to stop.
2351 */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002352 if (length > (png_uint_32)65535L)
2353 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002354 png_warning(png_ptr, "iTXt chunk too large to fit in memory");
2355 png_crc_finish(png_ptr, length);
2356 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002357 }
2358#endif
2359
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002360 png_free(png_ptr, png_ptr->chunkdata);
2361 png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002362
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002363 if (png_ptr->chunkdata == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002364 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002365 png_warning(png_ptr, "No memory to process iTXt chunk");
2366 return;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002367 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002368
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05002369 slength = (png_size_t)length;
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002370 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002371
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002372 if (png_crc_finish(png_ptr, 0))
2373 {
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002374 png_free(png_ptr, png_ptr->chunkdata);
2375 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002376 return;
2377 }
2378
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002379 png_ptr->chunkdata[slength] = 0x00;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002380
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002381 for (lang = png_ptr->chunkdata; *lang; lang++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002382 /* Empty loop */ ;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002383
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002384 lang++; /* Skip NUL separator */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002385
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002386 /* iTXt must have a language tag (possibly empty), two compression bytes,
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002387 * translated keyword (possibly empty), and possibly some text after the
2388 * keyword
2389 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002390
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002391 if (lang >= png_ptr->chunkdata + slength - 3)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002392 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002393 png_warning(png_ptr, "Truncated iTXt chunk");
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002394 png_free(png_ptr, png_ptr->chunkdata);
2395 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002396 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002397 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002398
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002399 else
2400 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002401 comp_flag = *lang++;
2402 comp_type = *lang++;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002403 }
2404
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002405 for (lang_key = lang; *lang_key; lang_key++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002406 /* Empty loop */ ;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002407
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002408 lang_key++; /* Skip NUL separator */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002409
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002410 if (lang_key >= png_ptr->chunkdata + slength)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002411 {
2412 png_warning(png_ptr, "Truncated iTXt chunk");
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002413 png_free(png_ptr, png_ptr->chunkdata);
2414 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002415 return;
2416 }
2417
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002418 for (text = lang_key; *text; text++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002419 /* Empty loop */ ;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002420
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002421 text++; /* Skip NUL separator */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002422
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002423 if (text >= png_ptr->chunkdata + slength)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002424 {
2425 png_warning(png_ptr, "Malformed iTXt chunk");
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002426 png_free(png_ptr, png_ptr->chunkdata);
2427 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002428 return;
2429 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002430
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002431 prefix_len = text - png_ptr->chunkdata;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002432
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002433 key=png_ptr->chunkdata;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002434
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002435 if (comp_flag)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002436 png_decompress_chunk(png_ptr, comp_type,
2437 (size_t)length, prefix_len, &data_len);
2438
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06002439 else
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002440 data_len = png_strlen(png_ptr->chunkdata + prefix_len);
2441
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002442 text_ptr = (png_textp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002443 png_sizeof(png_text));
2444
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002445 if (text_ptr == NULL)
2446 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002447 png_warning(png_ptr, "Not enough memory to process iTXt chunk");
2448 png_free(png_ptr, png_ptr->chunkdata);
2449 png_ptr->chunkdata = NULL;
2450 return;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002451 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002452
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05002453 text_ptr->compression = (int)comp_flag + 1;
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002454 text_ptr->lang_key = png_ptr->chunkdata + (lang_key - key);
2455 text_ptr->lang = png_ptr->chunkdata + (lang - key);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05002456 text_ptr->itxt_length = data_len;
2457 text_ptr->text_length = 0;
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002458 text_ptr->key = png_ptr->chunkdata;
2459 text_ptr->text = png_ptr->chunkdata + prefix_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002460
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002461 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002462
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05002463 png_free(png_ptr, text_ptr);
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002464 png_free(png_ptr, png_ptr->chunkdata);
2465 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002466
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002467 if (ret)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002468 png_error(png_ptr, "Insufficient memory to store iTXt chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002469}
2470#endif
2471
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002472/* This function is called when we haven't found a handler for a
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002473 * chunk. If there isn't a problem with the chunk itself (ie bad
2474 * chunk name, CRC, or a critical chunk), the chunk is silently ignored
2475 * -- unless the PNG_FLAG_UNKNOWN_CHUNKS_SUPPORTED flag is on in which
2476 * case it will be saved away to be written out later.
2477 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002478void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002479png_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
2480{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002481 png_uint_32 skip = 0;
2482
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002483 png_debug(1, "in png_handle_unknown");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002484
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06002485#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002486 if (png_ptr->user_chunk_cache_max != 0)
2487 {
2488 if (png_ptr->user_chunk_cache_max == 1)
2489 {
2490 png_crc_finish(png_ptr, length);
2491 return;
2492 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002493
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002494 if (--png_ptr->user_chunk_cache_max == 1)
2495 {
2496 png_warning(png_ptr, "No space in chunk cache for unknown chunk");
2497 png_crc_finish(png_ptr, length);
2498 return;
2499 }
2500 }
2501#endif
2502
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002503 if (png_ptr->mode & PNG_HAVE_IDAT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002504 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002505 PNG_IDAT;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002506
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002507 if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) /* Not an IDAT */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002508 png_ptr->mode |= PNG_AFTER_IDAT;
2509 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002510
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002511 if (!(png_ptr->chunk_name[0] & 0x20))
2512 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002513#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002514 if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002515 PNG_HANDLE_CHUNK_ALWAYS
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002516#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002517 && png_ptr->read_user_chunk_fn == NULL
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002518#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002519 )
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05002520#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002521 png_chunk_error(png_ptr, "unknown critical chunk");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002522 }
2523
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002524#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson7824a702009-06-13 10:05:05 -05002525 if ((png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS)
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002526#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
Glenn Randers-Pehrson7824a702009-06-13 10:05:05 -05002527 || (png_ptr->read_user_chunk_fn != NULL)
2528#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002529 )
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002530 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002531#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002532 if (length > (png_uint_32)65535L)
2533 {
2534 png_warning(png_ptr, "unknown chunk too large to fit in memory");
2535 skip = length - (png_uint_32)65535L;
2536 length = (png_uint_32)65535L;
2537 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002538#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002539
2540 png_memcpy((png_charp)png_ptr->unknown_chunk.name,
2541 (png_charp)png_ptr->chunk_name,
2542 png_sizeof(png_ptr->unknown_chunk.name));
2543
2544 png_ptr->unknown_chunk.name[png_sizeof(png_ptr->unknown_chunk.name)-1]
2545 = '\0';
2546
2547 png_ptr->unknown_chunk.size = (png_size_t)length;
2548
2549 if (length == 0)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002550 png_ptr->unknown_chunk.data = NULL;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002551
2552 else
2553 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002554 png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr, length);
2555 png_crc_read(png_ptr, (png_bytep)png_ptr->unknown_chunk.data, length);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002556 }
2557
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002558#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002559 if (png_ptr->read_user_chunk_fn != NULL)
2560 {
2561 /* Callback to user unknown chunk handler */
2562 int ret;
2563
2564 ret = (*(png_ptr->read_user_chunk_fn))
2565 (png_ptr, &png_ptr->unknown_chunk);
2566
2567 if (ret < 0)
2568 png_chunk_error(png_ptr, "error in user chunk");
2569
2570 if (ret == 0)
2571 {
2572 if (!(png_ptr->chunk_name[0] & 0x20))
2573 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002574#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002575 if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
2576 PNG_HANDLE_CHUNK_ALWAYS)
Glenn Randers-Pehrson7824a702009-06-13 10:05:05 -05002577#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002578 png_chunk_error(png_ptr, "unknown critical chunk");
2579 }
2580
2581 png_set_unknown_chunks(png_ptr, info_ptr,
2582 &png_ptr->unknown_chunk, 1);
2583 }
2584 }
2585
2586 else
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002587#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002588 png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1);
2589
2590 png_free(png_ptr, png_ptr->unknown_chunk.data);
2591 png_ptr->unknown_chunk.data = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002592 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002593
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002594 else
2595#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002596 skip = length;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002597
2598 png_crc_finish(png_ptr, skip);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002599
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -05002600#ifndef PNG_READ_USER_CHUNKS_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002601 PNG_UNUSED(info_ptr) /* Quiet compiler warnings about unused info_ptr */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002602#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002603}
2604
2605/* This function is called to verify that a chunk name is valid.
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002606 * This function can't have the "critical chunk check" incorporated
2607 * into it, since in the future we will need to be able to call user
2608 * functions to handle unknown critical chunks after we check that
2609 * the chunk name itself is valid.
2610 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002611
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05002612#define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
Andreas Dilger47a0c421997-05-16 02:46:07 -05002613
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002614void /* PRIVATE */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002615png_check_chunk_name(png_structp png_ptr, png_const_bytep chunk_name)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002616{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002617 png_debug(1, "in png_check_chunk_name");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002618 if (isnonalpha(chunk_name[0]) || isnonalpha(chunk_name[1]) ||
Glenn Randers-Pehrsoneb580912008-07-30 14:47:09 -05002619 isnonalpha(chunk_name[2]) || isnonalpha(chunk_name[3]))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002620 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06002621 png_chunk_error(png_ptr, "invalid chunk type");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002622 }
2623}
2624
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -05002625/* Combines the row recently read in with the existing pixels in the
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002626 * row. This routine takes care of alpha and transparency if requested.
2627 * This routine also handles the two methods of progressive display
2628 * of interlaced images, depending on the mask value.
2629 * The mask value describes which pixels are to be combined with
2630 * the row. The pattern always repeats every 8 pixels, so just 8
2631 * bits are needed. A one indicates the pixel is to be combined,
2632 * a zero indicates the pixel is to be skipped. This is in addition
2633 * to any alpha or transparency value associated with the pixel. If
2634 * you want all pixels to be combined, pass 0xff (255) in mask.
2635 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002636
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002637void /* PRIVATE */
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06002638png_combine_row(png_structp png_ptr, png_bytep row, int mask)
Guy Schalnat0d580581995-07-20 02:43:20 -05002639{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002640 png_debug(1, "in png_combine_row");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002641
Guy Schalnat0d580581995-07-20 02:43:20 -05002642 if (mask == 0xff)
2643 {
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002644 png_memcpy(row, png_ptr->row_buf + 1,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002645 PNG_ROWBYTES(png_ptr->row_info.pixel_depth, png_ptr->width));
Guy Schalnat0d580581995-07-20 02:43:20 -05002646 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002647
Guy Schalnat0d580581995-07-20 02:43:20 -05002648 else
2649 {
2650 switch (png_ptr->row_info.pixel_depth)
2651 {
2652 case 1:
2653 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002654 png_bytep sp = png_ptr->row_buf + 1;
2655 png_bytep dp = row;
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002656 int s_inc, s_start, s_end;
2657 int m = 0x80;
2658 int shift;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002659 png_uint_32 i;
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002660 png_uint_32 row_width = png_ptr->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002661
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002662#ifdef PNG_READ_PACKSWAP_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002663 if (png_ptr->transformations & PNG_PACKSWAP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002664 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002665 s_start = 0;
2666 s_end = 7;
2667 s_inc = 1;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002668 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002669
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002670 else
2671#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002672 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002673 s_start = 7;
2674 s_end = 0;
2675 s_inc = -1;
2676 }
2677
2678 shift = s_start;
2679
2680 for (i = 0; i < row_width; i++)
2681 {
2682 if (m & mask)
2683 {
2684 int value;
2685
2686 value = (*sp >> shift) & 0x01;
2687 *dp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
2688 *dp |= (png_byte)(value << shift);
2689 }
2690
2691 if (shift == s_end)
2692 {
2693 shift = s_start;
2694 sp++;
2695 dp++;
2696 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002697
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002698 else
2699 shift += s_inc;
2700
2701 if (m == 1)
2702 m = 0x80;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002703
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002704 else
2705 m >>= 1;
Guy Schalnat0d580581995-07-20 02:43:20 -05002706 }
2707 break;
2708 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002709
Guy Schalnat0d580581995-07-20 02:43:20 -05002710 case 2:
2711 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002712 png_bytep sp = png_ptr->row_buf + 1;
2713 png_bytep dp = row;
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002714 int s_start, s_end, s_inc;
Glenn Randers-Pehrson5dd2b8e2004-11-24 07:50:16 -06002715 int m = 0x80;
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002716 int shift;
2717 png_uint_32 i;
2718 png_uint_32 row_width = png_ptr->width;
2719 int value;
Guy Schalnat0d580581995-07-20 02:43:20 -05002720
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002721#ifdef PNG_READ_PACKSWAP_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002722 if (png_ptr->transformations & PNG_PACKSWAP)
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002723 {
2724 s_start = 0;
2725 s_end = 6;
2726 s_inc = 2;
2727 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002728
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002729 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05002730#endif
2731 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002732 s_start = 6;
2733 s_end = 0;
2734 s_inc = -2;
2735 }
2736
2737 shift = s_start;
2738
2739 for (i = 0; i < row_width; i++)
2740 {
2741 if (m & mask)
Guy Schalnat0d580581995-07-20 02:43:20 -05002742 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002743 value = (*sp >> shift) & 0x03;
2744 *dp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
2745 *dp |= (png_byte)(value << shift);
Glenn Randers-Pehrson5dd2b8e2004-11-24 07:50:16 -06002746 }
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002747
2748 if (shift == s_end)
2749 {
2750 shift = s_start;
2751 sp++;
2752 dp++;
2753 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002754
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002755 else
2756 shift += s_inc;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002757
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002758 if (m == 1)
2759 m = 0x80;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002760
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002761 else
2762 m >>= 1;
Guy Schalnat0d580581995-07-20 02:43:20 -05002763 }
2764 break;
2765 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002766
Guy Schalnat0d580581995-07-20 02:43:20 -05002767 case 4:
2768 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002769 png_bytep sp = png_ptr->row_buf + 1;
2770 png_bytep dp = row;
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002771 int s_start, s_end, s_inc;
Glenn Randers-Pehrson5dd2b8e2004-11-24 07:50:16 -06002772 int m = 0x80;
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002773 int shift;
2774 png_uint_32 i;
2775 png_uint_32 row_width = png_ptr->width;
2776 int value;
Guy Schalnat0d580581995-07-20 02:43:20 -05002777
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002778#ifdef PNG_READ_PACKSWAP_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002779 if (png_ptr->transformations & PNG_PACKSWAP)
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002780 {
2781 s_start = 0;
2782 s_end = 4;
2783 s_inc = 4;
2784 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002785
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002786 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05002787#endif
2788 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002789 s_start = 4;
2790 s_end = 0;
2791 s_inc = -4;
2792 }
2793 shift = s_start;
2794
2795 for (i = 0; i < row_width; i++)
2796 {
2797 if (m & mask)
Guy Schalnat0d580581995-07-20 02:43:20 -05002798 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002799 value = (*sp >> shift) & 0xf;
2800 *dp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
2801 *dp |= (png_byte)(value << shift);
Glenn Randers-Pehrson5dd2b8e2004-11-24 07:50:16 -06002802 }
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002803
2804 if (shift == s_end)
2805 {
2806 shift = s_start;
2807 sp++;
2808 dp++;
2809 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002810
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002811 else
2812 shift += s_inc;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002813
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002814 if (m == 1)
2815 m = 0x80;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002816
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002817 else
2818 m >>= 1;
Guy Schalnat0d580581995-07-20 02:43:20 -05002819 }
2820 break;
2821 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002822
Guy Schalnat0d580581995-07-20 02:43:20 -05002823 default:
2824 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002825 png_bytep sp = png_ptr->row_buf + 1;
2826 png_bytep dp = row;
2827 png_size_t pixel_bytes = (png_ptr->row_info.pixel_depth >> 3);
2828 png_uint_32 i;
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002829 png_uint_32 row_width = png_ptr->width;
2830 png_byte m = 0x80;
Guy Schalnat0d580581995-07-20 02:43:20 -05002831
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002832 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05002833 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002834 if (m & mask)
2835 {
2836 png_memcpy(dp, sp, pixel_bytes);
2837 }
2838
2839 sp += pixel_bytes;
2840 dp += pixel_bytes;
2841
2842 if (m == 1)
2843 m = 0x80;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002844
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002845 else
2846 m >>= 1;
Guy Schalnat0d580581995-07-20 02:43:20 -05002847 }
2848 break;
2849 }
2850 }
2851 }
2852}
2853
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06002854#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002855void /* PRIVATE */
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06002856png_do_read_interlace(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05002857{
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06002858 png_row_infop row_info = &(png_ptr->row_info);
2859 png_bytep row = png_ptr->row_buf + 1;
2860 int pass = png_ptr->pass;
2861 png_uint_32 transformations = png_ptr->transformations;
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002862 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
2863 /* Offset to next interlace block */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002864 PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002865
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002866 png_debug(1, "in png_do_read_interlace");
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002867 if (row != NULL && row_info != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -05002868 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002869 png_uint_32 final_width;
2870
2871 final_width = row_info->width * png_pass_inc[pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05002872
2873 switch (row_info->pixel_depth)
2874 {
2875 case 1:
2876 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002877 png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3);
2878 png_bytep dp = row + (png_size_t)((final_width - 1) >> 3);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002879 int sshift, dshift;
2880 int s_start, s_end, s_inc;
2881 int jstop = png_pass_inc[pass];
2882 png_byte v;
Guy Schalnat0d580581995-07-20 02:43:20 -05002883 png_uint_32 i;
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002884 int j;
Guy Schalnat0d580581995-07-20 02:43:20 -05002885
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002886#ifdef PNG_READ_PACKSWAP_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002887 if (transformations & PNG_PACKSWAP)
2888 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002889 sshift = (int)((row_info->width + 7) & 0x07);
2890 dshift = (int)((final_width + 7) & 0x07);
2891 s_start = 7;
2892 s_end = 0;
2893 s_inc = -1;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002894 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002895
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002896 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05002897#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002898 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002899 sshift = 7 - (int)((row_info->width + 7) & 0x07);
2900 dshift = 7 - (int)((final_width + 7) & 0x07);
2901 s_start = 0;
2902 s_end = 7;
2903 s_inc = 1;
2904 }
Glenn Randers-Pehrson5dd2b8e2004-11-24 07:50:16 -06002905
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002906 for (i = 0; i < row_info->width; i++)
2907 {
2908 v = (png_byte)((*sp >> sshift) & 0x01);
2909 for (j = 0; j < jstop; j++)
Guy Schalnat0d580581995-07-20 02:43:20 -05002910 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002911 *dp &= (png_byte)((0x7f7f >> (7 - dshift)) & 0xff);
2912 *dp |= (png_byte)(v << dshift);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002913
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002914 if (dshift == s_end)
2915 {
2916 dshift = s_start;
2917 dp--;
2918 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002919
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002920 else
2921 dshift += s_inc;
2922 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002923
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002924 if (sshift == s_end)
2925 {
2926 sshift = s_start;
Guy Schalnat0d580581995-07-20 02:43:20 -05002927 sp--;
2928 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002929
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002930 else
2931 sshift += s_inc;
Guy Schalnat0d580581995-07-20 02:43:20 -05002932 }
2933 break;
2934 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002935
Guy Schalnat0d580581995-07-20 02:43:20 -05002936 case 2:
2937 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002938 png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);
2939 png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);
2940 int sshift, dshift;
2941 int s_start, s_end, s_inc;
2942 int jstop = png_pass_inc[pass];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002943 png_uint_32 i;
Guy Schalnat0d580581995-07-20 02:43:20 -05002944
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002945#ifdef PNG_READ_PACKSWAP_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002946 if (transformations & PNG_PACKSWAP)
2947 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002948 sshift = (int)(((row_info->width + 3) & 0x03) << 1);
2949 dshift = (int)(((final_width + 3) & 0x03) << 1);
2950 s_start = 6;
2951 s_end = 0;
2952 s_inc = -2;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002953 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002954
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002955 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05002956#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002957 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002958 sshift = (int)((3 - ((row_info->width + 3) & 0x03)) << 1);
2959 dshift = (int)((3 - ((final_width + 3) & 0x03)) << 1);
2960 s_start = 0;
2961 s_end = 6;
2962 s_inc = 2;
2963 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002964
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002965 for (i = 0; i < row_info->width; i++)
2966 {
2967 png_byte v;
2968 int j;
2969
2970 v = (png_byte)((*sp >> sshift) & 0x03);
2971 for (j = 0; j < jstop; j++)
Guy Schalnat0d580581995-07-20 02:43:20 -05002972 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002973 *dp &= (png_byte)((0x3f3f >> (6 - dshift)) & 0xff);
2974 *dp |= (png_byte)(v << dshift);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002975
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002976 if (dshift == s_end)
2977 {
2978 dshift = s_start;
2979 dp--;
2980 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002981
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002982 else
2983 dshift += s_inc;
2984 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002985
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002986 if (sshift == s_end)
2987 {
2988 sshift = s_start;
Guy Schalnat0d580581995-07-20 02:43:20 -05002989 sp--;
2990 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002991
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002992 else
2993 sshift += s_inc;
Guy Schalnat0d580581995-07-20 02:43:20 -05002994 }
2995 break;
2996 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002997
Guy Schalnat0d580581995-07-20 02:43:20 -05002998 case 4:
2999 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003000 png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1);
3001 png_bytep dp = row + (png_size_t)((final_width - 1) >> 1);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003002 int sshift, dshift;
3003 int s_start, s_end, s_inc;
Guy Schalnat0d580581995-07-20 02:43:20 -05003004 png_uint_32 i;
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003005 int jstop = png_pass_inc[pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05003006
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003007#ifdef PNG_READ_PACKSWAP_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003008 if (transformations & PNG_PACKSWAP)
3009 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003010 sshift = (int)(((row_info->width + 1) & 0x01) << 2);
3011 dshift = (int)(((final_width + 1) & 0x01) << 2);
3012 s_start = 4;
3013 s_end = 0;
3014 s_inc = -4;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003015 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003016
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003017 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05003018#endif
3019 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003020 sshift = (int)((1 - ((row_info->width + 1) & 0x01)) << 2);
3021 dshift = (int)((1 - ((final_width + 1) & 0x01)) << 2);
3022 s_start = 0;
3023 s_end = 4;
3024 s_inc = 4;
3025 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05003026
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003027 for (i = 0; i < row_info->width; i++)
3028 {
3029 png_byte v = (png_byte)((*sp >> sshift) & 0xf);
3030 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003031
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003032 for (j = 0; j < jstop; j++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003033 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003034 *dp &= (png_byte)((0xf0f >> (4 - dshift)) & 0xff);
3035 *dp |= (png_byte)(v << dshift);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003036
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003037 if (dshift == s_end)
3038 {
3039 dshift = s_start;
3040 dp--;
3041 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003042
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003043 else
3044 dshift += s_inc;
3045 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003046
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003047 if (sshift == s_end)
3048 {
3049 sshift = s_start;
Guy Schalnat0d580581995-07-20 02:43:20 -05003050 sp--;
3051 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003052
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003053 else
3054 sshift += s_inc;
Guy Schalnat0d580581995-07-20 02:43:20 -05003055 }
3056 break;
3057 }
3058 default:
3059 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003060 png_size_t pixel_bytes = (row_info->pixel_depth >> 3);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003061
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06003062 png_bytep sp = row + (png_size_t)(row_info->width - 1)
3063 * pixel_bytes;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003064
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003065 png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05003066
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003067 int jstop = png_pass_inc[pass];
3068 png_uint_32 i;
3069
3070 for (i = 0; i < row_info->width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003071 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003072 png_byte v[8];
3073 int j;
3074
3075 png_memcpy(v, sp, pixel_bytes);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003076
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003077 for (j = 0; j < jstop; j++)
3078 {
3079 png_memcpy(dp, v, pixel_bytes);
3080 dp -= pixel_bytes;
3081 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003082
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003083 sp -= pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05003084 }
3085 break;
3086 }
3087 }
3088 row_info->width = final_width;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003089 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width);
Guy Schalnat0d580581995-07-20 02:43:20 -05003090 }
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -05003091#ifndef PNG_READ_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003092 PNG_UNUSED(transformations) /* Silence compiler warning */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05003093#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003094}
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06003095#endif /* PNG_READ_INTERLACING_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05003096
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05003097void /* PRIVATE */
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06003098png_read_filter_row(png_structp png_ptr, png_row_infop row_info, png_bytep row,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003099 png_const_bytep prev_row, int filter)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003100{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05003101 png_debug(1, "in png_read_filter_row");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003102 png_debug2(2, "row = %u, filter = %d", png_ptr->row_number, filter);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003103 switch (filter)
3104 {
3105 case PNG_FILTER_VALUE_NONE:
3106 break;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003107
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003108 case PNG_FILTER_VALUE_SUB:
3109 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003110 png_size_t i;
3111 png_size_t istop = row_info->rowbytes;
3112 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003113 png_bytep rp = row + bpp;
3114 png_bytep lp = row;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003115
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003116 for (i = bpp; i < istop; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003117 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05003118 *rp = (png_byte)(((int)(*rp) + (int)(*lp++)) & 0xff);
3119 rp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003120 }
3121 break;
3122 }
3123 case PNG_FILTER_VALUE_UP:
3124 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003125 png_size_t i;
3126 png_size_t istop = row_info->rowbytes;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003127 png_bytep rp = row;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003128 png_const_bytep pp = prev_row;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003129
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003130 for (i = 0; i < istop; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003131 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05003132 *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
3133 rp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003134 }
3135 break;
3136 }
3137 case PNG_FILTER_VALUE_AVG:
3138 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003139 png_size_t i;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003140 png_bytep rp = row;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003141 png_const_bytep pp = prev_row;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003142 png_bytep lp = row;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003143 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
3144 png_size_t istop = row_info->rowbytes - bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003145
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003146 for (i = 0; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003147 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05003148 *rp = (png_byte)(((int)(*rp) +
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003149 ((int)(*pp++) / 2 )) & 0xff);
3150
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05003151 rp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003152 }
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003153
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05003154 for (i = 0; i < istop; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003155 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05003156 *rp = (png_byte)(((int)(*rp) +
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003157 (int)(*pp++ + *lp++) / 2 ) & 0xff);
3158
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05003159 rp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003160 }
3161 break;
3162 }
3163 case PNG_FILTER_VALUE_PAETH:
3164 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003165 png_size_t i;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003166 png_bytep rp = row;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003167 png_const_bytep pp = prev_row;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003168 png_bytep lp = row;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003169 png_const_bytep cp = prev_row;
3170 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
3171 png_size_t istop=row_info->rowbytes - bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003172
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003173 for (i = 0; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003174 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05003175 *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
3176 rp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003177 }
3178
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003179 for (i = 0; i < istop; i++) /* Use leftover rp,pp */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003180 {
3181 int a, b, c, pa, pb, pc, p;
3182
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003183 a = *lp++;
3184 b = *pp++;
3185 c = *cp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003186
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003187 p = b - c;
3188 pc = a - c;
3189
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003190#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003191 pa = abs(p);
3192 pb = abs(pc);
3193 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003194#else
Glenn Randers-Pehrsona565f0e2010-03-06 08:24:45 -06003195 pa = p < 0 ? -p : p;
3196 pb = pc < 0 ? -pc : pc;
3197 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003198#endif
3199
3200 /*
3201 if (pa <= pb && pa <= pc)
3202 p = a;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003203
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003204 else if (pb <= pc)
3205 p = b;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003206
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003207 else
3208 p = c;
3209 */
3210
Glenn Randers-Pehrsona565f0e2010-03-06 08:24:45 -06003211 p = (pa <= pb && pa <= pc) ? a : (pb <= pc) ? b : c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003212
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05003213 *rp = (png_byte)(((int)(*rp) + p) & 0xff);
3214 rp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003215 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003216 break;
3217 }
3218 default:
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003219 png_error(png_ptr, "Ignoring bad adaptive filter type");
3220 /*NOT REACHED */
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05003221 break;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003222 }
3223}
Guy Schalnat0d580581995-07-20 02:43:20 -05003224
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05003225#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05003226void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06003227png_read_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05003228{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003229#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003230 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003231
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003232 /* Start of interlace block */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003233 PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003234
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003235 /* Offset to next interlace block */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003236 PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003237
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003238 /* Start of interlace block in the y direction */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003239 PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003240
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003241 /* Offset to next interlace block in the y direction */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003242 PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
3243#endif /* PNG_READ_INTERLACING_SUPPORTED */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003244
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05003245 png_debug(1, "in png_read_finish_row");
Guy Schalnat0d580581995-07-20 02:43:20 -05003246 png_ptr->row_number++;
3247 if (png_ptr->row_number < png_ptr->num_rows)
3248 return;
3249
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003250#ifdef PNG_READ_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05003251 if (png_ptr->interlaced)
3252 {
3253 png_ptr->row_number = 0;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003254
3255 png_memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
3256
Guy Schalnat0d580581995-07-20 02:43:20 -05003257 do
3258 {
3259 png_ptr->pass++;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003260
Guy Schalnat0d580581995-07-20 02:43:20 -05003261 if (png_ptr->pass >= 7)
3262 break;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003263
Guy Schalnat0d580581995-07-20 02:43:20 -05003264 png_ptr->iwidth = (png_ptr->width +
3265 png_pass_inc[png_ptr->pass] - 1 -
3266 png_pass_start[png_ptr->pass]) /
3267 png_pass_inc[png_ptr->pass];
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05003268
Guy Schalnat0d580581995-07-20 02:43:20 -05003269 if (!(png_ptr->transformations & PNG_INTERLACE))
3270 {
3271 png_ptr->num_rows = (png_ptr->height +
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003272 png_pass_yinc[png_ptr->pass] - 1 -
3273 png_pass_ystart[png_ptr->pass]) /
3274 png_pass_yinc[png_ptr->pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05003275 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003276
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -05003277 else /* if (png_ptr->transformations & PNG_INTERLACE) */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003278 break; /* libpng deinterlacing sees every row */
3279
3280 } while (png_ptr->num_rows == 0 || png_ptr->iwidth == 0);
Guy Schalnat0d580581995-07-20 02:43:20 -05003281
3282 if (png_ptr->pass < 7)
3283 return;
3284 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003285#endif /* PNG_READ_INTERLACING_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05003286
Guy Schalnate5a37791996-06-05 15:50:50 -05003287 if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
Guy Schalnat0d580581995-07-20 02:43:20 -05003288 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003289 PNG_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -05003290 char extra;
3291 int ret;
3292
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003293 png_ptr->zstream.next_out = (Byte *)&extra;
3294 png_ptr->zstream.avail_out = (uInt)1;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003295
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003296 for (;;)
Guy Schalnat0d580581995-07-20 02:43:20 -05003297 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003298 if (!(png_ptr->zstream.avail_in))
Guy Schalnat0d580581995-07-20 02:43:20 -05003299 {
3300 while (!png_ptr->idat_size)
3301 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003302 png_crc_finish(png_ptr, 0);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003303 png_ptr->idat_size = png_read_chunk_header(png_ptr);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05003304 if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
Guy Schalnat6d764711995-12-19 03:22:19 -06003305 png_error(png_ptr, "Not enough image data");
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -06003306 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003307
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003308 png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
3309 png_ptr->zstream.next_in = png_ptr->zbuf;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003310
Guy Schalnat0d580581995-07-20 02:43:20 -05003311 if (png_ptr->zbuf_size > png_ptr->idat_size)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003312 png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003313
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003314 png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zstream.avail_in);
3315 png_ptr->idat_size -= png_ptr->zstream.avail_in;
Guy Schalnat0d580581995-07-20 02:43:20 -05003316 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003317
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003318 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003319
Guy Schalnat0d580581995-07-20 02:43:20 -05003320 if (ret == Z_STREAM_END)
3321 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003322 if (!(png_ptr->zstream.avail_out) || png_ptr->zstream.avail_in ||
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003323 png_ptr->idat_size)
Glenn Randers-Pehrson859665d2002-08-06 18:06:11 -05003324 png_warning(png_ptr, "Extra compressed data");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003325
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003326 png_ptr->mode |= PNG_AFTER_IDAT;
3327 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
Guy Schalnat0d580581995-07-20 02:43:20 -05003328 break;
3329 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003330
Guy Schalnat0d580581995-07-20 02:43:20 -05003331 if (ret != Z_OK)
Glenn Randers-Pehrsona565f0e2010-03-06 08:24:45 -06003332 png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003333 "Decompression Error");
Guy Schalnat0d580581995-07-20 02:43:20 -05003334
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003335 if (!(png_ptr->zstream.avail_out))
Glenn Randers-Pehrson859665d2002-08-06 18:06:11 -05003336 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05003337 png_warning(png_ptr, "Extra compressed data");
Glenn Randers-Pehrson859665d2002-08-06 18:06:11 -05003338 png_ptr->mode |= PNG_AFTER_IDAT;
3339 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
3340 break;
3341 }
Guy Schalnat0d580581995-07-20 02:43:20 -05003342
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06003343 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003344 png_ptr->zstream.avail_out = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -05003345 }
3346
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003347 if (png_ptr->idat_size || png_ptr->zstream.avail_in)
Glenn Randers-Pehrson859665d2002-08-06 18:06:11 -05003348 png_warning(png_ptr, "Extra compression data");
Guy Schalnat0d580581995-07-20 02:43:20 -05003349
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003350 inflateReset(&png_ptr->zstream);
Guy Schalnat0d580581995-07-20 02:43:20 -05003351
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003352 png_ptr->mode |= PNG_AFTER_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -05003353}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05003354#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05003355
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05003356void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06003357png_read_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05003358{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003359#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003360 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003361
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003362 /* Start of interlace block */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003363 PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003364
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003365 /* Offset to next interlace block */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003366 PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003367
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003368 /* Start of interlace block in the y direction */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003369 PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003370
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003371 /* Offset to next interlace block in the y direction */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003372 PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
3373#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003374
Guy Schalnat0d580581995-07-20 02:43:20 -05003375 int max_pixel_depth;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05003376 png_size_t row_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05003377
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05003378 png_debug(1, "in png_read_start_row");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003379 png_ptr->zstream.avail_in = 0;
John Bowler4a12f4a2011-04-17 18:34:22 -05003380#ifdef PNG_READ_TRANSFORMS_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05003381 png_init_read_transformations(png_ptr);
John Bowler4a12f4a2011-04-17 18:34:22 -05003382#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003383#ifdef PNG_READ_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05003384 if (png_ptr->interlaced)
3385 {
3386 if (!(png_ptr->transformations & PNG_INTERLACE))
3387 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003388 png_pass_ystart[0]) / png_pass_yinc[0];
3389
Guy Schalnat0d580581995-07-20 02:43:20 -05003390 else
3391 png_ptr->num_rows = png_ptr->height;
3392
3393 png_ptr->iwidth = (png_ptr->width +
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003394 png_pass_inc[png_ptr->pass] - 1 -
3395 png_pass_start[png_ptr->pass]) /
3396 png_pass_inc[png_ptr->pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05003397 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003398
Guy Schalnat0d580581995-07-20 02:43:20 -05003399 else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003400#endif /* PNG_READ_INTERLACING_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05003401 {
3402 png_ptr->num_rows = png_ptr->height;
3403 png_ptr->iwidth = png_ptr->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05003404 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003405
Guy Schalnat0d580581995-07-20 02:43:20 -05003406 max_pixel_depth = png_ptr->pixel_depth;
3407
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003408#ifdef PNG_READ_PACK_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05003409 if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8)
Guy Schalnat0d580581995-07-20 02:43:20 -05003410 max_pixel_depth = 8;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05003411#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003412
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003413#ifdef PNG_READ_EXPAND_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -05003414 if (png_ptr->transformations & PNG_EXPAND)
Guy Schalnat0d580581995-07-20 02:43:20 -05003415 {
3416 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
3417 {
3418 if (png_ptr->num_trans)
3419 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003420
Guy Schalnat0d580581995-07-20 02:43:20 -05003421 else
3422 max_pixel_depth = 24;
3423 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003424
Guy Schalnat0d580581995-07-20 02:43:20 -05003425 else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
3426 {
3427 if (max_pixel_depth < 8)
3428 max_pixel_depth = 8;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003429
Guy Schalnat0d580581995-07-20 02:43:20 -05003430 if (png_ptr->num_trans)
3431 max_pixel_depth *= 2;
3432 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003433
Guy Schalnat0d580581995-07-20 02:43:20 -05003434 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
3435 {
3436 if (png_ptr->num_trans)
3437 {
3438 max_pixel_depth *= 4;
3439 max_pixel_depth /= 3;
3440 }
3441 }
3442 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05003443#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003444
John Bowler4d562962011-02-12 09:01:20 -06003445#ifdef PNG_READ_EXPAND_16_SUPPORTED
3446 if (png_ptr->transformations & PNG_EXPAND_16)
3447 {
3448# ifdef PNG_READ_EXPAND_SUPPORTED
3449 /* In fact it is an error if it isn't supported, but checking is
3450 * the safe way.
3451 */
3452 if (png_ptr->transformations & PNG_EXPAND)
3453 {
3454 if (png_ptr->bit_depth < 16)
3455 max_pixel_depth *= 2;
3456 }
3457 else
3458# endif
3459 png_ptr->transformations &= ~PNG_EXPAND_16;
3460 }
3461#endif
3462
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003463#ifdef PNG_READ_FILLER_SUPPORTED
Guy Schalnat51f0eb41995-09-26 05:22:39 -05003464 if (png_ptr->transformations & (PNG_FILLER))
Guy Schalnat0d580581995-07-20 02:43:20 -05003465 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05003466 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
3467 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003468
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05003469 else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003470 {
3471 if (max_pixel_depth <= 8)
3472 max_pixel_depth = 16;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003473
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003474 else
3475 max_pixel_depth = 32;
3476 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003477
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003478 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
3479 {
3480 if (max_pixel_depth <= 32)
3481 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003482
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003483 else
3484 max_pixel_depth = 64;
3485 }
Guy Schalnat0d580581995-07-20 02:43:20 -05003486 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05003487#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003488
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003489#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05003490 if (png_ptr->transformations & PNG_GRAY_TO_RGB)
3491 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003492 if (
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003493#ifdef PNG_READ_EXPAND_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003494 (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) ||
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003495#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003496#ifdef PNG_READ_FILLER_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003497 (png_ptr->transformations & (PNG_FILLER)) ||
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003498#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003499 png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
Guy Schalnat0d580581995-07-20 02:43:20 -05003500 {
3501 if (max_pixel_depth <= 16)
3502 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003503
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003504 else
Guy Schalnat0d580581995-07-20 02:43:20 -05003505 max_pixel_depth = 64;
3506 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003507
Guy Schalnat0d580581995-07-20 02:43:20 -05003508 else
3509 {
3510 if (max_pixel_depth <= 8)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003511 {
3512 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06003513 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003514
3515 else
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06003516 max_pixel_depth = 24;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003517 }
3518
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06003519 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
3520 max_pixel_depth = 64;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003521
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003522 else
Guy Schalnat0d580581995-07-20 02:43:20 -05003523 max_pixel_depth = 48;
3524 }
3525 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05003526#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003527
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05003528#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
3529defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003530 if (png_ptr->transformations & PNG_USER_TRANSFORM)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003531 {
3532 int user_pixel_depth = png_ptr->user_transform_depth*
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05003533 png_ptr->user_transform_channels;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003534
3535 if (user_pixel_depth > max_pixel_depth)
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05003536 max_pixel_depth=user_pixel_depth;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003537 }
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05003538#endif
3539
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003540 /* Align the width on the next larger 8 pixels. Mainly used
3541 * for interlacing
3542 */
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05003543 row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003544 /* Calculate the maximum bytes needed, adding a byte and a pixel
3545 * for safety's sake
3546 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003547 row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) +
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003548 1 + ((max_pixel_depth + 7) >> 3);
3549
Guy Schalnat0d580581995-07-20 02:43:20 -05003550#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05003551 if (row_bytes > (png_uint_32)65536L)
Guy Schalnate5a37791996-06-05 15:50:50 -05003552 png_error(png_ptr, "This image requires a row greater than 64KB");
Guy Schalnat0d580581995-07-20 02:43:20 -05003553#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003554
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06003555 if (row_bytes + 48 > png_ptr->old_big_row_buf_size)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003556 {
3557 png_free(png_ptr, png_ptr->big_row_buf);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003558
Glenn Randers-Pehrson6917b512009-03-09 15:31:08 -05003559 if (png_ptr->interlaced)
Glenn Randers-Pehrsona515d302010-01-01 10:24:25 -06003560 png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr,
3561 row_bytes + 48);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003562
Glenn Randers-Pehrsona515d302010-01-01 10:24:25 -06003563 else
3564 png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr,
3565 row_bytes + 48);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003566
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06003567 png_ptr->old_big_row_buf_size = row_bytes + 48;
3568
3569#ifdef PNG_ALIGNED_MEMORY_SUPPORTED
3570 /* Use 16-byte aligned memory for row_buf with at least 16 bytes
3571 * of padding before and after row_buf.
3572 */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003573 png_ptr->row_buf = png_ptr->big_row_buf + 32 -
3574 (((png_alloc_size_t)png_ptr->big_row_buf + 15) & 0x0F);
3575
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06003576 png_ptr->old_big_row_buf_size = row_bytes + 48;
3577#else
3578 /* Use 32 bytes of padding before and 16 bytes after row_buf. */
Glenn Randers-Pehrson8fb550c2009-03-21 08:15:32 -05003579 png_ptr->row_buf = png_ptr->big_row_buf + 32;
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06003580#endif
3581 png_ptr->old_big_row_buf_size = row_bytes + 48;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003582 }
Guy Schalnat0d580581995-07-20 02:43:20 -05003583
3584#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003585 if (png_ptr->rowbytes > 65535)
Guy Schalnate5a37791996-06-05 15:50:50 -05003586 png_error(png_ptr, "This image requires a row greater than 64KB");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003587
Guy Schalnat0d580581995-07-20 02:43:20 -05003588#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003589 if (png_ptr->rowbytes > (PNG_SIZE_MAX - 1))
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05003590 png_error(png_ptr, "Row has too many bytes to allocate in memory");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003591
Glenn Randers-Pehrsona98aa482009-10-13 09:23:39 -05003592 if (png_ptr->rowbytes + 1 > png_ptr->old_prev_row_size)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003593 {
Glenn Randers-Pehrsona98aa482009-10-13 09:23:39 -05003594 png_free(png_ptr, png_ptr->prev_row);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003595
3596 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, png_ptr->rowbytes + 1);
3597
Glenn Randers-Pehrsona98aa482009-10-13 09:23:39 -05003598 png_ptr->old_prev_row_size = png_ptr->rowbytes + 1;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003599 }
Guy Schalnat0d580581995-07-20 02:43:20 -05003600
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05003601 png_memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05003602
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003603 png_debug1(3, "width = %u,", png_ptr->width);
3604 png_debug1(3, "height = %u,", png_ptr->height);
3605 png_debug1(3, "iwidth = %u,", png_ptr->iwidth);
3606 png_debug1(3, "num_rows = %u,", png_ptr->num_rows);
Glenn Randers-Pehrsonb764c602011-01-14 21:18:37 -06003607 png_debug1(3, "rowbytes = %lu,", (unsigned long)png_ptr->rowbytes);
3608 png_debug1(3, "irowbytes = %lu",
3609 (unsigned long)PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05003610
Guy Schalnate5a37791996-06-05 15:50:50 -05003611 png_ptr->flags |= PNG_FLAG_ROW_INIT;
Guy Schalnat0d580581995-07-20 02:43:20 -05003612}
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06003613#endif /* PNG_READ_SUPPORTED */