blob: 0595985467ffa6fd6638772e2e8a618a38acf0c1 [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-Pehrsonbb5cb142011-09-22 12:41:58 -05004 * Last changed in libpng 1.5.6 [(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);
John Bowlerf3f7e142011-09-09 07:32:37 -050090 if ((uval & 0x80000000) == 0) /* non-negative */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -060091 return uval;
Andreas Dilger47a0c421997-05-16 02:46:07 -050092
John Bowlerf3f7e142011-09-09 07:32:37 -050093 uval = (uval ^ 0xffffffff) + 1; /* 2's complement: -x = ~x+1 */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -060094 return -(png_int_32)uval;
Andreas Dilger47a0c421997-05-16 02:46:07 -050095}
Andreas Dilger47a0c421997-05-16 02:46:07 -050096
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -050097/* Grab an unsigned 16-bit integer from a buffer in big-endian format. */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -060098png_uint_16 (PNGAPI
99png_get_uint_16)(png_const_bytep buf)
Guy Schalnat0d580581995-07-20 02:43:20 -0500100{
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600101 /* ANSI-C requires an int value to accomodate at least 16 bits so this
102 * works and allows the compiler not to worry about possible narrowing
103 * on 32 bit systems. (Pre-ANSI systems did not make integers smaller
104 * than 16 bits either.)
105 */
106 unsigned int val =
107 ((unsigned int)(*buf) << 8) +
108 ((unsigned int)(*(buf + 1)));
Guy Schalnat0d580581995-07-20 02:43:20 -0500109
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600110 return (png_uint_16)val;
Guy Schalnat0d580581995-07-20 02:43:20 -0500111}
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600112
113#endif /* PNG_READ_INT_FUNCTIONS_SUPPORTED */
114
115/* Read and check the PNG file signature */
116void /* PRIVATE */
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-Pehrsonbb5cb142011-09-22 12:41:58 -0500168 png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(buf+4);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500169
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500170 png_debug2(0, "Reading %lx chunk, length = %lu",
171 (unsigned long)png_ptr->chunk_name, (unsigned long)length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500172
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600173 /* Reset the crc and run it over the chunk name. */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500174 png_reset_crc(png_ptr);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500175 png_calculate_crc(png_ptr, buf + 4, 4);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500176
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600177 /* Check to see if chunk name is valid. */
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500178 png_check_chunk_name(png_ptr, png_ptr->chunk_name);
179
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500180#ifdef PNG_IO_STATE_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500181 png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_DATA;
182#endif
183
184 return length;
185}
186
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500187/* Read data, and (optionally) run it through the CRC. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500188void /* PRIVATE */
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-Pehrsonbb5cb142011-09-22 12:41:58 -0500221 if (PNG_CHUNK_ANCILLIARY(png_ptr->chunk_name) ?
222 !(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) :
223 (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600224 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600225 png_chunk_warning(png_ptr, "CRC error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600226 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600227
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600228 else
229 {
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500230 png_chunk_benign_error(png_ptr, "CRC error");
231 return (0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600232 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600233
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600234 return (1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600235 }
236
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600237 return (0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500238}
239
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600240/* 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 -0500241 * the data it has read thus far.
242 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500243int /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600244png_crc_error(png_structp png_ptr)
245{
246 png_byte crc_bytes[4];
247 png_uint_32 crc;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500248 int need_crc = 1;
249
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500250 if (PNG_CHUNK_ANCILLIARY(png_ptr->chunk_name))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500251 {
252 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
253 (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
254 need_crc = 0;
255 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600256
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500257 else /* critical */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500258 {
259 if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
260 need_crc = 0;
261 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600262
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500263#ifdef PNG_IO_STATE_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500264 png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_CRC;
265#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500266
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600267 /* The chunk CRC must be serialized in a single I/O call. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600268 png_read_data(png_ptr, crc_bytes, 4);
269
Andreas Dilger47a0c421997-05-16 02:46:07 -0500270 if (need_crc)
271 {
272 crc = png_get_uint_32(crc_bytes);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600273 return ((int)(crc != png_ptr->crc));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500274 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600275
Andreas Dilger47a0c421997-05-16 02:46:07 -0500276 else
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600277 return (0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600278}
279
Glenn Randers-Pehrson5975f312011-04-01 13:15:36 -0500280#ifdef PNG_READ_COMPRESSED_TEXT_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600281static png_size_t
282png_inflate(png_structp png_ptr, png_bytep data, png_size_t size,
283 png_bytep output, png_size_t output_size)
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600284{
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600285 png_size_t count = 0;
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600286
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600287 /* zlib can't necessarily handle more than 65535 bytes at once (i.e. it can't
288 * even necessarily handle 65536 bytes) because the type uInt is "16 bits or
289 * more". Consequently it is necessary to chunk the input to zlib. This
290 * code uses ZLIB_IO_MAX, from pngpriv.h, as the maximum (the maximum value
291 * that can be stored in a uInt.) It is possible to set ZLIB_IO_MAX to a
292 * lower value in pngpriv.h and this may sometimes have a performance
293 * advantage, because it forces access of the input data to be separated from
294 * at least some of the use by some period of time.
295 */
296 png_ptr->zstream.next_in = data;
297 /* avail_in is set below from 'size' */
298 png_ptr->zstream.avail_in = 0;
299
300 while (1)
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600301 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600302 int ret, avail;
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600303
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -0500304 /* The setting of 'avail_in' used to be outside the loop; by setting it
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600305 * inside it is possible to chunk the input to zlib and simply rely on
306 * zlib to advance the 'next_in' pointer. This allows arbitrary amounts o
307 * data to be passed through zlib at the unavoidable cost of requiring a
308 * window save (memcpy of up to 32768 output bytes) every ZLIB_IO_MAX
309 * input bytes.
310 */
311 if (png_ptr->zstream.avail_in == 0 && size > 0)
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600312 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600313 if (size <= ZLIB_IO_MAX)
Glenn Randers-Pehrson72cda2d2010-03-06 08:18:03 -0600314 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600315 /* The value is less than ZLIB_IO_MAX so the cast is safe: */
316 png_ptr->zstream.avail_in = (uInt)size;
317 size = 0;
Glenn Randers-Pehrson72cda2d2010-03-06 08:18:03 -0600318 }
Glenn Randers-Pehrson33893092010-10-23 13:20:18 -0500319
Glenn Randers-Pehrson72cda2d2010-03-06 08:18:03 -0600320 else
321 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600322 png_ptr->zstream.avail_in = ZLIB_IO_MAX;
323 size -= ZLIB_IO_MAX;
Glenn Randers-Pehrson72cda2d2010-03-06 08:18:03 -0600324 }
Glenn Randers-Pehrson9d51afc2010-02-12 20:12:56 -0600325 }
326
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600327 /* Reset the output buffer each time round - we empty it
328 * after every inflate call.
329 */
330 png_ptr->zstream.next_out = png_ptr->zbuf;
331 png_ptr->zstream.avail_out = png_ptr->zbuf_size;
332
333 ret = inflate(&png_ptr->zstream, Z_NO_FLUSH);
334 avail = png_ptr->zbuf_size - png_ptr->zstream.avail_out;
335
336 /* First copy/count any new output - but only if we didn't
337 * get an error code.
338 */
339 if ((ret == Z_OK || ret == Z_STREAM_END) && avail > 0)
340 {
341 png_size_t space = avail; /* > 0, see above */
342
343 if (output != 0 && output_size > count)
344 {
345 png_size_t copy = output_size - count;
346
347 if (space < copy)
348 copy = space;
349
350 png_memcpy(output + count, png_ptr->zbuf, copy);
351 }
352 count += space;
353 }
354
355 if (ret == Z_OK)
356 continue;
357
358 /* Termination conditions - always reset the zstream, it
359 * must be left in inflateInit state.
360 */
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600361 png_ptr->zstream.avail_in = 0;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600362 inflateReset(&png_ptr->zstream);
363
364 if (ret == Z_STREAM_END)
365 return count; /* NOTE: may be zero. */
366
367 /* Now handle the error codes - the API always returns 0
368 * and the error message is dumped into the uncompressed
369 * buffer if available.
370 */
John Bowler88b77cc2011-05-05 06:49:55 -0500371# ifdef PNG_WARNINGS_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600372 {
John Bowler88b77cc2011-05-05 06:49:55 -0500373 png_const_charp msg;
374
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600375 if (png_ptr->zstream.msg != 0)
376 msg = png_ptr->zstream.msg;
377
John Bowler88b77cc2011-05-05 06:49:55 -0500378 else switch (ret)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600379 {
John Bowler88b77cc2011-05-05 06:49:55 -0500380 case Z_BUF_ERROR:
381 msg = "Buffer error in compressed datastream";
382 break;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600383
John Bowler88b77cc2011-05-05 06:49:55 -0500384 case Z_DATA_ERROR:
385 msg = "Data error in compressed datastream";
386 break;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600387
John Bowler88b77cc2011-05-05 06:49:55 -0500388 default:
389 msg = "Incomplete compressed datastream";
390 break;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600391 }
392
John Bowler88b77cc2011-05-05 06:49:55 -0500393 png_chunk_warning(png_ptr, msg);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600394 }
John Bowler88b77cc2011-05-05 06:49:55 -0500395# endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600396
397 /* 0 means an error - notice that this code simply ignores
398 * zero length compressed chunks as a result.
399 */
400 return 0;
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600401 }
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600402}
403
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600404/*
405 * Decompress trailing data in a chunk. The assumption is that chunkdata
406 * points at an allocated area holding the contents of a chunk with a
407 * trailing compressed part. What we get back is an allocated area
408 * holding the original prefix part and an uncompressed version of the
409 * trailing part (the malloc area passed in is freed).
410 */
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500411void /* PRIVATE */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500412png_decompress_chunk(png_structp png_ptr, int comp_type,
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600413 png_size_t chunklength,
414 png_size_t prefix_size, png_size_t *newlength)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600415{
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600416 /* The caller should guarantee this */
417 if (prefix_size > chunklength)
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600418 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600419 /* The recovery is to delete the chunk. */
420 png_warning(png_ptr, "invalid chunklength");
421 prefix_size = 0; /* To delete everything */
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600422 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600423
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600424 else if (comp_type == PNG_COMPRESSION_TYPE_BASE)
Glenn Randers-Pehrson9b0956f2010-02-12 11:17:22 -0600425 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600426 png_size_t expanded_size = png_inflate(png_ptr,
427 (png_bytep)(png_ptr->chunkdata + prefix_size),
428 chunklength - prefix_size,
Glenn Randers-Pehrsonb75b2412011-04-16 19:35:05 -0500429 0, /* output */
430 0); /* output size */
Glenn Randers-Pehrson9b0956f2010-02-12 11:17:22 -0600431
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600432 /* Now check the limits on this chunk - if the limit fails the
433 * compressed data will be removed, the prefix will remain.
434 */
435#ifdef PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED
436 if (png_ptr->user_chunk_malloc_max &&
437 (prefix_size + expanded_size >= png_ptr->user_chunk_malloc_max - 1))
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600438#else
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600439# ifdef PNG_USER_CHUNK_MALLOC_MAX
440 if ((PNG_USER_CHUNK_MALLOC_MAX > 0) &&
441 prefix_size + expanded_size >= PNG_USER_CHUNK_MALLOC_MAX - 1)
442# endif
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600443#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600444 png_warning(png_ptr, "Exceeded size limit while expanding chunk");
445
446 /* If the size is zero either there was an error and a message
447 * has already been output (warning) or the size really is zero
448 * and we have nothing to do - the code will exit through the
449 * error case below.
450 */
451#if defined(PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED) || \
452 defined(PNG_USER_CHUNK_MALLOC_MAX)
453 else if (expanded_size > 0)
454#else
455 if (expanded_size > 0)
456#endif
457 {
458 /* Success (maybe) - really uncompress the chunk. */
459 png_size_t new_size = 0;
John Bowlera5bcab72011-07-14 23:02:11 -0500460 png_charp text = (png_charp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600461 prefix_size + expanded_size + 1);
462
463 if (text != NULL)
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600464 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600465 png_memcpy(text, png_ptr->chunkdata, prefix_size);
466 new_size = png_inflate(png_ptr,
467 (png_bytep)(png_ptr->chunkdata + prefix_size),
468 chunklength - prefix_size,
469 (png_bytep)(text + prefix_size), expanded_size);
470 text[prefix_size + expanded_size] = 0; /* just in case */
471
472 if (new_size == expanded_size)
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600473 {
474 png_free(png_ptr, png_ptr->chunkdata);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600475 png_ptr->chunkdata = text;
476 *newlength = prefix_size + expanded_size;
477 return; /* The success return! */
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600478 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600479
480 png_warning(png_ptr, "png_inflate logic error");
481 png_free(png_ptr, text);
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600482 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600483
484 else
485 png_warning(png_ptr, "Not enough memory to decompress chunk");
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600486 }
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600487 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600488
Glenn Randers-Pehrson9b0956f2010-02-12 11:17:22 -0600489 else /* if (comp_type != PNG_COMPRESSION_TYPE_BASE) */
490 {
John Bowler88b77cc2011-05-05 06:49:55 -0500491 PNG_WARNING_PARAMETERS(p)
492 png_warning_parameter_signed(p, 1, PNG_NUMBER_FORMAT_d, comp_type);
493 png_formatted_warning(png_ptr, p, "Unknown zTXt compression type @1");
Glenn Randers-Pehrson9b0956f2010-02-12 11:17:22 -0600494
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600495 /* The recovery is to simply drop the data. */
Glenn Randers-Pehrson9b0956f2010-02-12 11:17:22 -0600496 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600497
498 /* Generic error return - leave the prefix, delete the compressed
499 * data, reallocate the chunkdata to remove the potentially large
500 * amount of compressed data.
501 */
502 {
John Bowlera5bcab72011-07-14 23:02:11 -0500503 png_charp text = (png_charp)png_malloc_warn(png_ptr, prefix_size + 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600504
505 if (text != NULL)
506 {
507 if (prefix_size > 0)
508 png_memcpy(text, png_ptr->chunkdata, prefix_size);
509
510 png_free(png_ptr, png_ptr->chunkdata);
511 png_ptr->chunkdata = text;
512
513 /* This is an extra zero in the 'uncompressed' part. */
514 *(png_ptr->chunkdata + prefix_size) = 0x00;
515 }
516 /* Ignore a malloc error here - it is safe. */
517 }
518
519 *newlength = prefix_size;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600520}
Glenn Randers-Pehrson5975f312011-04-01 13:15:36 -0500521#endif /* PNG_READ_COMPRESSED_TEXT_SUPPORTED */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600522
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500523/* Read and check the IDHR chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500524void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600525png_handle_IHDR(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500526{
527 png_byte buf[13];
528 png_uint_32 width, height;
529 int bit_depth, color_type, compression_type, filter_type;
530 int interlace_type;
531
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500532 png_debug(1, "in png_handle_IHDR");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500533
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600534 if (png_ptr->mode & PNG_HAVE_IHDR)
Guy Schalnate5a37791996-06-05 15:50:50 -0500535 png_error(png_ptr, "Out of place IHDR");
536
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500537 /* Check the length */
Guy Schalnat0d580581995-07-20 02:43:20 -0500538 if (length != 13)
Guy Schalnat6d764711995-12-19 03:22:19 -0600539 png_error(png_ptr, "Invalid IHDR chunk");
Guy Schalnat0d580581995-07-20 02:43:20 -0500540
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600541 png_ptr->mode |= PNG_HAVE_IHDR;
542
Guy Schalnat0d580581995-07-20 02:43:20 -0500543 png_crc_read(png_ptr, buf, 13);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600544 png_crc_finish(png_ptr, 0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500545
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500546 width = png_get_uint_31(png_ptr, buf);
547 height = png_get_uint_31(png_ptr, buf + 4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500548 bit_depth = buf[8];
549 color_type = buf[9];
550 compression_type = buf[10];
551 filter_type = buf[11];
552 interlace_type = buf[12];
553
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500554 /* Set internal variables */
Guy Schalnat0d580581995-07-20 02:43:20 -0500555 png_ptr->width = width;
556 png_ptr->height = height;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600557 png_ptr->bit_depth = (png_byte)bit_depth;
558 png_ptr->interlaced = (png_byte)interlace_type;
559 png_ptr->color_type = (png_byte)color_type;
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500560#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600561 png_ptr->filter_type = (png_byte)filter_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500562#endif
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500563 png_ptr->compression_type = (png_byte)compression_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500564
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500565 /* Find number of channels */
Guy Schalnat0d580581995-07-20 02:43:20 -0500566 switch (png_ptr->color_type)
567 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600568 default: /* invalid, png_set_IHDR calls png_error */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500569 case PNG_COLOR_TYPE_GRAY:
570 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnat0d580581995-07-20 02:43:20 -0500571 png_ptr->channels = 1;
572 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500573
Andreas Dilger47a0c421997-05-16 02:46:07 -0500574 case PNG_COLOR_TYPE_RGB:
Guy Schalnat0d580581995-07-20 02:43:20 -0500575 png_ptr->channels = 3;
576 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500577
Andreas Dilger47a0c421997-05-16 02:46:07 -0500578 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnat0d580581995-07-20 02:43:20 -0500579 png_ptr->channels = 2;
580 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500581
Andreas Dilger47a0c421997-05-16 02:46:07 -0500582 case PNG_COLOR_TYPE_RGB_ALPHA:
Guy Schalnat0d580581995-07-20 02:43:20 -0500583 png_ptr->channels = 4;
584 break;
585 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600586
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500587 /* Set up other useful info */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600588 png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth *
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600589 png_ptr->channels);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500590 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width);
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500591 png_debug1(3, "bit_depth = %d", png_ptr->bit_depth);
592 png_debug1(3, "channels = %d", png_ptr->channels);
Glenn Randers-Pehrsonb764c602011-01-14 21:18:37 -0600593 png_debug1(3, "rowbytes = %lu", (unsigned long)png_ptr->rowbytes);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500594 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600595 color_type, interlace_type, compression_type, filter_type);
Guy Schalnat0d580581995-07-20 02:43:20 -0500596}
597
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500598/* Read and check the palette */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500599void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600600png_handle_PLTE(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500601{
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600602 png_color palette[PNG_MAX_PALETTE_LENGTH];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600603 int num, i;
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500604#ifdef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500605 png_colorp pal_ptr;
606#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500607
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500608 png_debug(1, "in png_handle_PLTE");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500609
Guy Schalnate5a37791996-06-05 15:50:50 -0500610 if (!(png_ptr->mode & PNG_HAVE_IHDR))
611 png_error(png_ptr, "Missing IHDR before PLTE");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500612
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600613 else if (png_ptr->mode & PNG_HAVE_IDAT)
614 {
615 png_warning(png_ptr, "Invalid PLTE after IDAT");
616 png_crc_finish(png_ptr, length);
617 return;
618 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500619
Guy Schalnate5a37791996-06-05 15:50:50 -0500620 else if (png_ptr->mode & PNG_HAVE_PLTE)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600621 png_error(png_ptr, "Duplicate PLTE chunk");
622
623 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500624
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500625 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
626 {
627 png_warning(png_ptr,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600628 "Ignoring PLTE chunk in grayscale PNG");
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500629 png_crc_finish(png_ptr, length);
630 return;
631 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600632
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -0500633#ifndef PNG_READ_OPT_PLTE_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500634 if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
635 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600636 png_crc_finish(png_ptr, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500637 return;
638 }
639#endif
640
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600641 if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3)
Guy Schalnate5a37791996-06-05 15:50:50 -0500642 {
643 if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
644 {
645 png_warning(png_ptr, "Invalid palette chunk");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600646 png_crc_finish(png_ptr, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500647 return;
648 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500649
Guy Schalnate5a37791996-06-05 15:50:50 -0500650 else
651 {
652 png_error(png_ptr, "Invalid palette chunk");
653 }
654 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500655
656 num = (int)length / 3;
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500657
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500658#ifdef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500659 for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)
660 {
661 png_byte buf[3];
662
663 png_crc_read(png_ptr, buf, 3);
664 pal_ptr->red = buf[0];
665 pal_ptr->green = buf[1];
666 pal_ptr->blue = buf[2];
667 }
668#else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600669 for (i = 0; i < num; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500670 {
671 png_byte buf[3];
672
673 png_crc_read(png_ptr, buf, 3);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500674 /* Don't depend upon png_color being any order */
Guy Schalnat0d580581995-07-20 02:43:20 -0500675 palette[i].red = buf[0];
676 palette[i].green = buf[1];
677 palette[i].blue = buf[2];
678 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500679#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600680
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600681 /* 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 -0500682 * whatever the normal CRC configuration tells us. However, if we
683 * have an RGB image, the PLTE can be considered ancillary, so
684 * we will act as though it is.
685 */
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -0500686#ifndef PNG_READ_OPT_PLTE_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600687 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600688#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600689 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500690 png_crc_finish(png_ptr, 0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600691 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600692
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -0500693#ifndef PNG_READ_OPT_PLTE_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600694 else if (png_crc_error(png_ptr)) /* Only if we have a CRC error */
695 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600696 /* If we don't want to use the data from an ancillary chunk,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600697 * we have two options: an error abort, or a warning and we
698 * ignore the data in this chunk (which should be OK, since
699 * it's considered ancillary for a RGB or RGBA image).
700 */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600701 if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE))
702 {
703 if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)
704 {
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500705 png_chunk_benign_error(png_ptr, "CRC error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600706 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600707
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600708 else
709 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600710 png_chunk_warning(png_ptr, "CRC error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600711 return;
712 }
713 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600714
Andreas Dilger47a0c421997-05-16 02:46:07 -0500715 /* Otherwise, we (optionally) emit a warning and use the chunk. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600716 else if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN))
717 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600718 png_chunk_warning(png_ptr, "CRC error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600719 }
720 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600721#endif
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500722
Andreas Dilger47a0c421997-05-16 02:46:07 -0500723 png_set_PLTE(png_ptr, info_ptr, palette, num);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500724
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500725#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500726 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
727 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600728 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500729 {
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500730 if (png_ptr->num_trans > (png_uint_16)num)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500731 {
732 png_warning(png_ptr, "Truncating incorrect tRNS chunk length");
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500733 png_ptr->num_trans = (png_uint_16)num;
734 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600735
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500736 if (info_ptr->num_trans > (png_uint_16)num)
737 {
738 png_warning(png_ptr, "Truncating incorrect info tRNS chunk length");
739 info_ptr->num_trans = (png_uint_16)num;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500740 }
741 }
742 }
743#endif
744
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600745}
Guy Schalnate5a37791996-06-05 15:50:50 -0500746
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500747void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600748png_handle_IEND(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
749{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500750 png_debug(1, "in png_handle_IEND");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500751
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600752 if (!(png_ptr->mode & PNG_HAVE_IHDR) || !(png_ptr->mode & PNG_HAVE_IDAT))
753 {
754 png_error(png_ptr, "No image in file");
755 }
756
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600757 png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600758
759 if (length != 0)
760 {
761 png_warning(png_ptr, "Incorrect IEND chunk length");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600762 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600763
Andreas Dilger47a0c421997-05-16 02:46:07 -0500764 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500765
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600766 PNG_UNUSED(info_ptr) /* Quiet compiler warnings about unused info_ptr */
Guy Schalnat0d580581995-07-20 02:43:20 -0500767}
768
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500769#ifdef PNG_READ_gAMA_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500770void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600771png_handle_gAMA(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500772{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600773 png_fixed_point igamma;
Guy Schalnat0d580581995-07-20 02:43:20 -0500774 png_byte buf[4];
775
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500776 png_debug(1, "in png_handle_gAMA");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500777
Guy Schalnate5a37791996-06-05 15:50:50 -0500778 if (!(png_ptr->mode & PNG_HAVE_IHDR))
779 png_error(png_ptr, "Missing IHDR before gAMA");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600780
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600781 else if (png_ptr->mode & PNG_HAVE_IDAT)
782 {
783 png_warning(png_ptr, "Invalid gAMA after IDAT");
784 png_crc_finish(png_ptr, length);
785 return;
786 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600787
Guy Schalnate5a37791996-06-05 15:50:50 -0500788 else if (png_ptr->mode & PNG_HAVE_PLTE)
789 /* Should be an error, but we can cope with it */
790 png_warning(png_ptr, "Out of place gAMA chunk");
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -0600791
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500792 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA)
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500793#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600794 && !(info_ptr->valid & PNG_INFO_sRGB)
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -0600795#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600796 )
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600797 {
798 png_warning(png_ptr, "Duplicate gAMA chunk");
799 png_crc_finish(png_ptr, length);
800 return;
801 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500802
Guy Schalnat0d580581995-07-20 02:43:20 -0500803 if (length != 4)
804 {
Guy Schalnat69b14481996-01-10 02:56:49 -0600805 png_warning(png_ptr, "Incorrect gAMA chunk length");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600806 png_crc_finish(png_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500807 return;
808 }
809
810 png_crc_read(png_ptr, buf, 4);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600811
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600812 if (png_crc_finish(png_ptr, 0))
813 return;
814
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600815 igamma = png_get_fixed_point(NULL, buf);
Glenn Randers-Pehrson33893092010-10-23 13:20:18 -0500816
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600817 /* Check for zero gamma or an error. */
818 if (igamma <= 0)
819 {
820 png_warning(png_ptr,
821 "Ignoring gAMA chunk with out of range gamma");
822
823 return;
824 }
825
826# ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600827 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB))
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600828 {
John Bowler75156122011-09-09 17:21:44 -0500829 if (PNG_OUT_OF_RANGE(igamma, 45500, 500))
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600830 {
John Bowler88b77cc2011-05-05 06:49:55 -0500831 PNG_WARNING_PARAMETERS(p)
832 png_warning_parameter_signed(p, 1, PNG_NUMBER_FORMAT_fixed, igamma);
833 png_formatted_warning(png_ptr, p,
834 "Ignoring incorrect gAMA value @1 when sRGB is also present");
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600835 return;
836 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600837 }
838# endif /* PNG_READ_sRGB_SUPPORTED */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600839
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600840# ifdef PNG_READ_GAMMA_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600841 /* Gamma correction on read is supported. */
842 png_ptr->gamma = igamma;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600843# endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600844 /* And set the 'info' structure members. */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600845 png_set_gAMA_fixed(png_ptr, info_ptr, igamma);
Guy Schalnat0d580581995-07-20 02:43:20 -0500846}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500847#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500848
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500849#ifdef PNG_READ_sBIT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500850void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600851png_handle_sBIT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500852{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500853 png_size_t truelen;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600854 png_byte buf[4];
Guy Schalnat69b14481996-01-10 02:56:49 -0600855
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500856 png_debug(1, "in png_handle_sBIT");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500857
Guy Schalnat69b14481996-01-10 02:56:49 -0600858 buf[0] = buf[1] = buf[2] = buf[3] = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -0500859
Guy Schalnate5a37791996-06-05 15:50:50 -0500860 if (!(png_ptr->mode & PNG_HAVE_IHDR))
861 png_error(png_ptr, "Missing IHDR before sBIT");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600862
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600863 else if (png_ptr->mode & PNG_HAVE_IDAT)
864 {
865 png_warning(png_ptr, "Invalid sBIT after IDAT");
866 png_crc_finish(png_ptr, length);
867 return;
868 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600869
Guy Schalnate5a37791996-06-05 15:50:50 -0500870 else if (png_ptr->mode & PNG_HAVE_PLTE)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600871 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500872 /* Should be an error, but we can cope with it */
873 png_warning(png_ptr, "Out of place sBIT chunk");
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600874 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600875
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500876 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600877 {
878 png_warning(png_ptr, "Duplicate sBIT chunk");
879 png_crc_finish(png_ptr, length);
880 return;
881 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500882
Guy Schalnat0d580581995-07-20 02:43:20 -0500883 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600884 truelen = 3;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600885
Guy Schalnat0d580581995-07-20 02:43:20 -0500886 else
Andreas Dilger47a0c421997-05-16 02:46:07 -0500887 truelen = (png_size_t)png_ptr->channels;
Guy Schalnat0d580581995-07-20 02:43:20 -0500888
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500889 if (length != truelen || length > 4)
Guy Schalnat0d580581995-07-20 02:43:20 -0500890 {
Guy Schalnat69b14481996-01-10 02:56:49 -0600891 png_warning(png_ptr, "Incorrect sBIT chunk length");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600892 png_crc_finish(png_ptr, length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600893 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500894 }
895
Andreas Dilger47a0c421997-05-16 02:46:07 -0500896 png_crc_read(png_ptr, buf, truelen);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600897
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600898 if (png_crc_finish(png_ptr, 0))
899 return;
900
Guy Schalnat0d580581995-07-20 02:43:20 -0500901 if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
902 {
Guy Schalnat6d764711995-12-19 03:22:19 -0600903 png_ptr->sig_bit.red = buf[0];
904 png_ptr->sig_bit.green = buf[1];
905 png_ptr->sig_bit.blue = buf[2];
906 png_ptr->sig_bit.alpha = buf[3];
Guy Schalnat0d580581995-07-20 02:43:20 -0500907 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600908
Guy Schalnat0d580581995-07-20 02:43:20 -0500909 else
910 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600911 png_ptr->sig_bit.gray = buf[0];
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600912 png_ptr->sig_bit.red = buf[0];
913 png_ptr->sig_bit.green = buf[0];
914 png_ptr->sig_bit.blue = buf[0];
Guy Schalnat6d764711995-12-19 03:22:19 -0600915 png_ptr->sig_bit.alpha = buf[1];
Guy Schalnat0d580581995-07-20 02:43:20 -0500916 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600917
Andreas Dilger47a0c421997-05-16 02:46:07 -0500918 png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
Guy Schalnat0d580581995-07-20 02:43:20 -0500919}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500920#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500921
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500922#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500923void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600924png_handle_cHRM(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500925{
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500926 png_byte buf[32];
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600927 png_fixed_point x_white, y_white, x_red, y_red, x_green, y_green, x_blue,
928 y_blue;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600929
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500930 png_debug(1, "in png_handle_cHRM");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500931
Guy Schalnate5a37791996-06-05 15:50:50 -0500932 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600933 png_error(png_ptr, "Missing IHDR before cHRM");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600934
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600935 else if (png_ptr->mode & PNG_HAVE_IDAT)
936 {
937 png_warning(png_ptr, "Invalid cHRM after IDAT");
938 png_crc_finish(png_ptr, length);
939 return;
940 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600941
Guy Schalnate5a37791996-06-05 15:50:50 -0500942 else if (png_ptr->mode & PNG_HAVE_PLTE)
943 /* Should be an error, but we can cope with it */
Glenn Randers-Pehrson93254f62011-10-26 08:36:21 -0500944 png_warning(png_ptr, "Out of place cHRM chunk");
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -0600945
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500946 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600947# ifdef PNG_READ_sRGB_SUPPORTED
948 && !(info_ptr->valid & PNG_INFO_sRGB)
949# endif
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -0600950 )
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600951 {
952 png_warning(png_ptr, "Duplicate cHRM chunk");
953 png_crc_finish(png_ptr, length);
954 return;
955 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500956
Guy Schalnat0d580581995-07-20 02:43:20 -0500957 if (length != 32)
958 {
Guy Schalnat69b14481996-01-10 02:56:49 -0600959 png_warning(png_ptr, "Incorrect cHRM chunk length");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600960 png_crc_finish(png_ptr, length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600961 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500962 }
963
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500964 png_crc_read(png_ptr, buf, 32);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600965
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500966 if (png_crc_finish(png_ptr, 0))
967 return;
968
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600969 x_white = png_get_fixed_point(NULL, buf);
970 y_white = png_get_fixed_point(NULL, buf + 4);
971 x_red = png_get_fixed_point(NULL, buf + 8);
972 y_red = png_get_fixed_point(NULL, buf + 12);
973 x_green = png_get_fixed_point(NULL, buf + 16);
974 y_green = png_get_fixed_point(NULL, buf + 20);
975 x_blue = png_get_fixed_point(NULL, buf + 24);
976 y_blue = png_get_fixed_point(NULL, buf + 28);
Glenn Randers-Pehrson33893092010-10-23 13:20:18 -0500977
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600978 if (x_white == PNG_FIXED_ERROR ||
979 y_white == PNG_FIXED_ERROR ||
980 x_red == PNG_FIXED_ERROR ||
981 y_red == PNG_FIXED_ERROR ||
982 x_green == PNG_FIXED_ERROR ||
983 y_green == PNG_FIXED_ERROR ||
984 x_blue == PNG_FIXED_ERROR ||
985 y_blue == PNG_FIXED_ERROR)
986 {
987 png_warning(png_ptr, "Ignoring cHRM chunk with negative chromaticities");
988 return;
989 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600990
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500991#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500992 if ((info_ptr != NULL) && (info_ptr->valid & PNG_INFO_sRGB))
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600993 {
994 if (PNG_OUT_OF_RANGE(x_white, 31270, 1000) ||
995 PNG_OUT_OF_RANGE(y_white, 32900, 1000) ||
John Bowler75156122011-09-09 17:21:44 -0500996 PNG_OUT_OF_RANGE(x_red, 64000, 1000) ||
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600997 PNG_OUT_OF_RANGE(y_red, 33000, 1000) ||
998 PNG_OUT_OF_RANGE(x_green, 30000, 1000) ||
John Bowler75156122011-09-09 17:21:44 -0500999 PNG_OUT_OF_RANGE(y_green, 60000, 1000) ||
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001000 PNG_OUT_OF_RANGE(x_blue, 15000, 1000) ||
1001 PNG_OUT_OF_RANGE(y_blue, 6000, 1000))
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001002 {
John Bowler88b77cc2011-05-05 06:49:55 -05001003 PNG_WARNING_PARAMETERS(p)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001004
John Bowler88b77cc2011-05-05 06:49:55 -05001005 png_warning_parameter_signed(p, 1, PNG_NUMBER_FORMAT_fixed, x_white);
1006 png_warning_parameter_signed(p, 2, PNG_NUMBER_FORMAT_fixed, y_white);
1007 png_warning_parameter_signed(p, 3, PNG_NUMBER_FORMAT_fixed, x_red);
1008 png_warning_parameter_signed(p, 4, PNG_NUMBER_FORMAT_fixed, y_red);
1009 png_warning_parameter_signed(p, 5, PNG_NUMBER_FORMAT_fixed, x_green);
1010 png_warning_parameter_signed(p, 6, PNG_NUMBER_FORMAT_fixed, y_green);
1011 png_warning_parameter_signed(p, 7, PNG_NUMBER_FORMAT_fixed, x_blue);
1012 png_warning_parameter_signed(p, 8, PNG_NUMBER_FORMAT_fixed, y_blue);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001013
John Bowler88b77cc2011-05-05 06:49:55 -05001014 png_formatted_warning(png_ptr, p,
1015 "Ignoring incorrect cHRM white(@1,@2) r(@3,@4)g(@5,@6)b(@7,@8) "
1016 "when sRGB is also present");
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001017 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001018 return;
1019 }
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001020#endif /* PNG_READ_sRGB_SUPPORTED */
1021
John Bowlercb0b2962011-05-12 21:48:29 -05001022#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1023 /* Store the _white values as default coefficients for the rgb to gray
John Bowler736f40f2011-08-25 16:19:44 -05001024 * operation if it is supported. Check if the transform is already set to
1025 * avoid destroying the transform values.
John Bowlercb0b2962011-05-12 21:48:29 -05001026 */
John Bowler736f40f2011-08-25 16:19:44 -05001027 if (!png_ptr->rgb_to_gray_coefficients_set)
John Bowlercb0b2962011-05-12 21:48:29 -05001028 {
John Bowler736f40f2011-08-25 16:19:44 -05001029 /* png_set_background has not been called and we haven't seen an sRGB
1030 * chunk yet. Find the XYZ of the three end points.
John Bowlercb0b2962011-05-12 21:48:29 -05001031 */
John Bowler736f40f2011-08-25 16:19:44 -05001032 png_XYZ XYZ;
1033 png_xy xy;
John Bowlercb0b2962011-05-12 21:48:29 -05001034
John Bowler736f40f2011-08-25 16:19:44 -05001035 xy.redx = x_red;
1036 xy.redy = y_red;
1037 xy.greenx = x_green;
1038 xy.greeny = y_green;
1039 xy.bluex = x_blue;
1040 xy.bluey = y_blue;
1041 xy.whitex = x_white;
1042 xy.whitey = y_white;
1043
1044 if (png_XYZ_from_xy_checked(png_ptr, &XYZ, xy))
1045 {
1046 /* The success case, because XYZ_from_xy normalises to a reference
1047 * white Y of 1.0 we just need to scale the numbers. This should
1048 * always work just fine. It is an internal error if this overflows.
1049 */
1050 {
1051 png_fixed_point r, g, b;
1052 if (png_muldiv(&r, XYZ.redY, 32768, PNG_FP_1) &&
1053 r >= 0 && r <= 32768 &&
1054 png_muldiv(&g, XYZ.greenY, 32768, PNG_FP_1) &&
1055 g >= 0 && g <= 32768 &&
1056 png_muldiv(&b, XYZ.blueY, 32768, PNG_FP_1) &&
1057 b >= 0 && b <= 32768 &&
1058 r+g+b <= 32769)
1059 {
1060 /* We allow 0 coefficients here. r+g+b may be 32769 if two or
1061 * all of the coefficients were rounded up. Handle this by
1062 * reducing the *largest* coefficient by 1; this matches the
1063 * approach used for the default coefficients in pngrtran.c
1064 */
1065 int add = 0;
1066
1067 if (r+g+b > 32768)
1068 add = -1;
1069 else if (r+g+b < 32768)
1070 add = 1;
1071
1072 if (add != 0)
1073 {
1074 if (g >= r && g >= b)
1075 g += add;
1076 else if (r >= g && r >= b)
1077 r += add;
1078 else
1079 b += add;
1080 }
1081
1082 /* Check for an internal error. */
1083 if (r+g+b != 32768)
1084 png_error(png_ptr,
1085 "internal error handling cHRM coefficients");
1086
1087 png_ptr->rgb_to_gray_red_coeff = (png_uint_16)r;
1088 png_ptr->rgb_to_gray_green_coeff = (png_uint_16)g;
1089 }
1090
1091 /* This is a png_error at present even though it could be ignored -
1092 * it should never happen, but it is important that if it does, the
1093 * bug is fixed.
1094 */
1095 else
1096 png_error(png_ptr, "internal error handling cHRM->XYZ");
1097 }
John Bowlercb0b2962011-05-12 21:48:29 -05001098 }
1099 }
1100#endif
1101
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001102 png_set_cHRM_fixed(png_ptr, info_ptr, x_white, y_white, x_red, y_red,
1103 x_green, y_green, x_blue, y_blue);
Guy Schalnat0d580581995-07-20 02:43:20 -05001104}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001105#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001106
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001107#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001108void /* PRIVATE */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001109png_handle_sRGB(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1110{
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001111 int intent;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001112 png_byte buf[1];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001113
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001114 png_debug(1, "in png_handle_sRGB");
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001115
1116 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1117 png_error(png_ptr, "Missing IHDR before sRGB");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001118
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001119 else if (png_ptr->mode & PNG_HAVE_IDAT)
1120 {
1121 png_warning(png_ptr, "Invalid sRGB after IDAT");
1122 png_crc_finish(png_ptr, length);
1123 return;
1124 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001125
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001126 else if (png_ptr->mode & PNG_HAVE_PLTE)
1127 /* Should be an error, but we can cope with it */
1128 png_warning(png_ptr, "Out of place sRGB chunk");
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06001129
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001130 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB))
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001131 {
1132 png_warning(png_ptr, "Duplicate sRGB chunk");
1133 png_crc_finish(png_ptr, length);
1134 return;
1135 }
1136
1137 if (length != 1)
1138 {
1139 png_warning(png_ptr, "Incorrect sRGB chunk length");
1140 png_crc_finish(png_ptr, length);
1141 return;
1142 }
1143
1144 png_crc_read(png_ptr, buf, 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001145
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001146 if (png_crc_finish(png_ptr, 0))
1147 return;
1148
1149 intent = buf[0];
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001150
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001151 /* Check for bad intent */
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001152 if (intent >= PNG_sRGB_INTENT_LAST)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001153 {
1154 png_warning(png_ptr, "Unknown sRGB intent");
1155 return;
1156 }
1157
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001158#if defined(PNG_READ_gAMA_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED)
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001159 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001160 {
John Bowler75156122011-09-09 17:21:44 -05001161 if (PNG_OUT_OF_RANGE(info_ptr->gamma, 45500, 500))
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06001162 {
John Bowler88b77cc2011-05-05 06:49:55 -05001163 PNG_WARNING_PARAMETERS(p)
1164
1165 png_warning_parameter_signed(p, 1, PNG_NUMBER_FORMAT_fixed,
1166 info_ptr->gamma);
1167
1168 png_formatted_warning(png_ptr, p,
1169 "Ignoring incorrect gAMA value @1 when sRGB is also present");
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06001170 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001171 }
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06001172#endif /* PNG_READ_gAMA_SUPPORTED */
1173
1174#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001175 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM))
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001176 if (PNG_OUT_OF_RANGE(info_ptr->x_white, 31270, 1000) ||
1177 PNG_OUT_OF_RANGE(info_ptr->y_white, 32900, 1000) ||
John Bowler75156122011-09-09 17:21:44 -05001178 PNG_OUT_OF_RANGE(info_ptr->x_red, 64000, 1000) ||
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001179 PNG_OUT_OF_RANGE(info_ptr->y_red, 33000, 1000) ||
1180 PNG_OUT_OF_RANGE(info_ptr->x_green, 30000, 1000) ||
John Bowler75156122011-09-09 17:21:44 -05001181 PNG_OUT_OF_RANGE(info_ptr->y_green, 60000, 1000) ||
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001182 PNG_OUT_OF_RANGE(info_ptr->x_blue, 15000, 1000) ||
1183 PNG_OUT_OF_RANGE(info_ptr->y_blue, 6000, 1000))
1184 {
1185 png_warning(png_ptr,
1186 "Ignoring incorrect cHRM value when sRGB is also present");
1187 }
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06001188#endif /* PNG_READ_cHRM_SUPPORTED */
1189
John Bowler736f40f2011-08-25 16:19:44 -05001190 /* This is recorded for use when handling the cHRM chunk above. An sRGB
1191 * chunk unconditionally overwrites the coefficients for grayscale conversion
1192 * too.
1193 */
1194 png_ptr->is_sRGB = 1;
1195
1196# ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1197 /* Don't overwrite user supplied values: */
1198 if (!png_ptr->rgb_to_gray_coefficients_set)
1199 {
1200 /* These numbers come from the sRGB specification (or, since one has to
1201 * pay much money to get a copy, the wikipedia sRGB page) the
1202 * chromaticity values quoted have been inverted to get the reverse
1203 * transformation from RGB to XYZ and the 'Y' coefficients scaled by
1204 * 32768 (then rounded).
1205 *
1206 * sRGB and ITU Rec-709 both truncate the values for the D65 white
1207 * point to four digits and, even though it actually stores five
1208 * digits, the PNG spec gives the truncated value.
1209 *
1210 * This means that when the chromaticities are converted back to XYZ
1211 * end points we end up with (6968,23435,2366), which, as described in
1212 * pngrtran.c, would overflow. If the five digit precision and up is
1213 * used we get, instead:
1214 *
1215 * 6968*R + 23435*G + 2365*B
1216 *
1217 * (Notice that this rounds the blue coefficient down, rather than the
1218 * choice used in pngrtran.c which is to round the green one down.)
1219 */
1220 png_ptr->rgb_to_gray_red_coeff = 6968; /* 0.212639005871510 */
1221 png_ptr->rgb_to_gray_green_coeff = 23434; /* 0.715168678767756 */
1222 /* png_ptr->rgb_to_gray_blue_coeff = 2366; 0.072192315360734 */
1223
1224 /* The following keeps the cHRM chunk from destroying the
1225 * coefficients again in the event that it follows the sRGB chunk.
1226 */
1227 png_ptr->rgb_to_gray_coefficients_set = 1;
1228 }
1229# endif
1230
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06001231 png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, intent);
1232}
1233#endif /* PNG_READ_sRGB_SUPPORTED */
1234
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001235#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001236void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001237png_handle_iCCP(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1238/* Note: this does not properly handle chunks that are > 64K under DOS */
1239{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001240 png_byte compression_type;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -06001241 png_bytep pC;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001242 png_charp profile;
1243 png_uint_32 skip = 0;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001244 png_uint_32 profile_size;
1245 png_alloc_size_t profile_length;
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001246 png_size_t slength, prefix_length, data_length;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001247
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001248 png_debug(1, "in png_handle_iCCP");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001249
1250 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1251 png_error(png_ptr, "Missing IHDR before iCCP");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001252
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001253 else if (png_ptr->mode & PNG_HAVE_IDAT)
1254 {
1255 png_warning(png_ptr, "Invalid iCCP after IDAT");
1256 png_crc_finish(png_ptr, length);
1257 return;
1258 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001259
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001260 else if (png_ptr->mode & PNG_HAVE_PLTE)
1261 /* Should be an error, but we can cope with it */
1262 png_warning(png_ptr, "Out of place iCCP chunk");
1263
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001264 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_iCCP))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001265 {
1266 png_warning(png_ptr, "Duplicate iCCP chunk");
1267 png_crc_finish(png_ptr, length);
1268 return;
1269 }
1270
1271#ifdef PNG_MAX_MALLOC_64K
1272 if (length > (png_uint_32)65535L)
1273 {
1274 png_warning(png_ptr, "iCCP chunk too large to fit in memory");
1275 skip = length - (png_uint_32)65535L;
1276 length = (png_uint_32)65535L;
1277 }
1278#endif
1279
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001280 png_free(png_ptr, png_ptr->chunkdata);
1281 png_ptr->chunkdata = (png_charp)png_malloc(png_ptr, length + 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001282 slength = (png_size_t)length;
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001283 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001284
1285 if (png_crc_finish(png_ptr, skip))
1286 {
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001287 png_free(png_ptr, png_ptr->chunkdata);
1288 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001289 return;
1290 }
1291
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001292 png_ptr->chunkdata[slength] = 0x00;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001293
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001294 for (profile = png_ptr->chunkdata; *profile; profile++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001295 /* Empty loop to find end of name */ ;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001296
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001297 ++profile;
1298
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001299 /* There should be at least one zero (the compression type byte)
1300 * following the separator, and we should be on it
1301 */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001302 if (profile >= png_ptr->chunkdata + slength - 1)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001303 {
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001304 png_free(png_ptr, png_ptr->chunkdata);
1305 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001306 png_warning(png_ptr, "Malformed iCCP chunk");
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -05001307 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001308 }
1309
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001310 /* Compression_type should always be zero */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001311 compression_type = *profile++;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001312
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001313 if (compression_type)
1314 {
1315 png_warning(png_ptr, "Ignoring nonzero compression type in iCCP chunk");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001316 compression_type = 0x00; /* Reset it to zero (libpng-1.0.6 through 1.0.8
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001317 wrote nonzero) */
1318 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001319
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001320 prefix_length = profile - png_ptr->chunkdata;
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -05001321 png_decompress_chunk(png_ptr, compression_type,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001322 slength, prefix_length, &data_length);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001323
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001324 profile_length = data_length - prefix_length;
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06001325
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001326 if (prefix_length > data_length || profile_length < 4)
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001327 {
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001328 png_free(png_ptr, png_ptr->chunkdata);
1329 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001330 png_warning(png_ptr, "Profile size field missing from iCCP chunk");
1331 return;
1332 }
1333
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06001334 /* Check the profile_size recorded in the first 32 bits of the ICC profile */
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001335 pC = (png_bytep)(png_ptr->chunkdata + prefix_length);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001336 profile_size = ((*(pC )) << 24) |
1337 ((*(pC + 1)) << 16) |
1338 ((*(pC + 2)) << 8) |
1339 ((*(pC + 3)) );
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001340
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001341 /* NOTE: the following guarantees that 'profile_length' fits into 32 bits,
1342 * because profile_size is a 32 bit value.
1343 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001344 if (profile_size < profile_length)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001345 profile_length = profile_size;
1346
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001347 /* And the following guarantees that profile_size == profile_length. */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001348 if (profile_size > profile_length)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001349 {
John Bowler88b77cc2011-05-05 06:49:55 -05001350 PNG_WARNING_PARAMETERS(p)
1351
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001352 png_free(png_ptr, png_ptr->chunkdata);
1353 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001354
John Bowler88b77cc2011-05-05 06:49:55 -05001355 png_warning_parameter_unsigned(p, 1, PNG_NUMBER_FORMAT_u, profile_size);
1356 png_warning_parameter_unsigned(p, 2, PNG_NUMBER_FORMAT_u, profile_length);
1357 png_formatted_warning(png_ptr, p,
1358 "Ignoring iCCP chunk with declared size = @1 and actual length = @2");
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001359 return;
1360 }
1361
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001362 png_set_iCCP(png_ptr, info_ptr, png_ptr->chunkdata,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001363 compression_type, (png_bytep)png_ptr->chunkdata + prefix_length,
1364 profile_size);
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001365 png_free(png_ptr, png_ptr->chunkdata);
1366 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001367}
1368#endif /* PNG_READ_iCCP_SUPPORTED */
1369
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001370#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001371void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001372png_handle_sPLT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1373/* Note: this does not properly handle chunks that are > 64K under DOS */
1374{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001375 png_bytep entry_start;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001376 png_sPLT_t new_palette;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001377 png_sPLT_entryp pp;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001378 png_uint_32 data_length;
1379 int entry_size, i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001380 png_uint_32 skip = 0;
1381 png_size_t slength;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001382 png_uint_32 dl;
1383 png_size_t max_dl;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001384
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001385 png_debug(1, "in png_handle_sPLT");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001386
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001387#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson7824a702009-06-13 10:05:05 -05001388
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001389 if (png_ptr->user_chunk_cache_max != 0)
1390 {
1391 if (png_ptr->user_chunk_cache_max == 1)
1392 {
1393 png_crc_finish(png_ptr, length);
1394 return;
1395 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001396
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001397 if (--png_ptr->user_chunk_cache_max == 1)
1398 {
1399 png_warning(png_ptr, "No space in chunk cache for sPLT");
1400 png_crc_finish(png_ptr, length);
1401 return;
1402 }
1403 }
1404#endif
1405
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001406 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1407 png_error(png_ptr, "Missing IHDR before sPLT");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001408
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001409 else if (png_ptr->mode & PNG_HAVE_IDAT)
1410 {
1411 png_warning(png_ptr, "Invalid sPLT after IDAT");
1412 png_crc_finish(png_ptr, length);
1413 return;
1414 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001415
1416#ifdef PNG_MAX_MALLOC_64K
1417 if (length > (png_uint_32)65535L)
1418 {
1419 png_warning(png_ptr, "sPLT chunk too large to fit in memory");
1420 skip = length - (png_uint_32)65535L;
1421 length = (png_uint_32)65535L;
1422 }
1423#endif
1424
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001425 png_free(png_ptr, png_ptr->chunkdata);
1426 png_ptr->chunkdata = (png_charp)png_malloc(png_ptr, length + 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001427
1428 /* WARNING: this may break if size_t is less than 32 bits; it is assumed
1429 * that the PNG_MAX_MALLOC_64K test is enabled in this case, but this is a
1430 * potential breakage point if the types in pngconf.h aren't exactly right.
1431 */
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001432 slength = (png_size_t)length;
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001433 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001434
1435 if (png_crc_finish(png_ptr, skip))
1436 {
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001437 png_free(png_ptr, png_ptr->chunkdata);
1438 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001439 return;
1440 }
1441
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001442 png_ptr->chunkdata[slength] = 0x00;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001443
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001444 for (entry_start = (png_bytep)png_ptr->chunkdata; *entry_start;
1445 entry_start++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001446 /* Empty loop to find end of name */ ;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001447
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001448 ++entry_start;
1449
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001450 /* A sample depth should follow the separator, and we should be on it */
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001451 if (entry_start > (png_bytep)png_ptr->chunkdata + slength - 2)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001452 {
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001453 png_free(png_ptr, png_ptr->chunkdata);
1454 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -05001455 png_warning(png_ptr, "malformed sPLT chunk");
1456 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001457 }
1458
1459 new_palette.depth = *entry_start++;
Glenn Randers-Pehrsona565f0e2010-03-06 08:24:45 -06001460 entry_size = (new_palette.depth == 8 ? 6 : 10);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001461 /* This must fit in a png_uint_32 because it is derived from the original
1462 * chunk data length (and use 'length', not 'slength' here for clarity -
1463 * they are guaranteed to be the same, see the tests above.)
1464 */
1465 data_length = length - (png_uint_32)(entry_start -
1466 (png_bytep)png_ptr->chunkdata);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001467
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001468 /* Integrity-check the data length */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001469 if (data_length % entry_size)
1470 {
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001471 png_free(png_ptr, png_ptr->chunkdata);
1472 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001473 png_warning(png_ptr, "sPLT chunk has bad length");
1474 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001475 }
1476
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001477 dl = (png_int_32)(data_length / entry_size);
1478 max_dl = PNG_SIZE_MAX / png_sizeof(png_sPLT_entry);
1479
1480 if (dl > max_dl)
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001481 {
1482 png_warning(png_ptr, "sPLT chunk too long");
1483 return;
1484 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001485
1486 new_palette.nentries = (png_int_32)(data_length / entry_size);
1487
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001488 new_palette.entries = (png_sPLT_entryp)png_malloc_warn(
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001489 png_ptr, new_palette.nentries * png_sizeof(png_sPLT_entry));
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001490
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001491 if (new_palette.entries == NULL)
1492 {
1493 png_warning(png_ptr, "sPLT chunk requires too much memory");
1494 return;
1495 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001496
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001497#ifdef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001498 for (i = 0; i < new_palette.nentries; i++)
1499 {
Glenn Randers-Pehrson90b878c2009-10-07 12:44:35 -05001500 pp = new_palette.entries + i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001501
1502 if (new_palette.depth == 8)
1503 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001504 pp->red = *entry_start++;
1505 pp->green = *entry_start++;
1506 pp->blue = *entry_start++;
1507 pp->alpha = *entry_start++;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001508 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001509
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001510 else
1511 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001512 pp->red = png_get_uint_16(entry_start); entry_start += 2;
1513 pp->green = png_get_uint_16(entry_start); entry_start += 2;
1514 pp->blue = png_get_uint_16(entry_start); entry_start += 2;
1515 pp->alpha = png_get_uint_16(entry_start); entry_start += 2;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001516 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001517
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001518 pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
1519 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001520#else
1521 pp = new_palette.entries;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001522
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001523 for (i = 0; i < new_palette.nentries; i++)
1524 {
1525
1526 if (new_palette.depth == 8)
1527 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001528 pp[i].red = *entry_start++;
1529 pp[i].green = *entry_start++;
1530 pp[i].blue = *entry_start++;
1531 pp[i].alpha = *entry_start++;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001532 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001533
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001534 else
1535 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001536 pp[i].red = png_get_uint_16(entry_start); entry_start += 2;
1537 pp[i].green = png_get_uint_16(entry_start); entry_start += 2;
1538 pp[i].blue = png_get_uint_16(entry_start); entry_start += 2;
1539 pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001540 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001541
Glenn Randers-Pehrsonf27592a2011-03-21 18:05:40 -05001542 pp[i].frequency = png_get_uint_16(entry_start); entry_start += 2;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001543 }
1544#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001545
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001546 /* Discard all chunk data except the name and stash that */
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001547 new_palette.name = png_ptr->chunkdata;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001548
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001549 png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001550
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001551 png_free(png_ptr, png_ptr->chunkdata);
1552 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001553 png_free(png_ptr, new_palette.entries);
1554}
1555#endif /* PNG_READ_sPLT_SUPPORTED */
1556
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001557#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001558void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001559png_handle_tRNS(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001560{
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001561 png_byte readbuf[PNG_MAX_PALETTE_LENGTH];
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06001562
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001563 png_debug(1, "in png_handle_tRNS");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001564
Guy Schalnate5a37791996-06-05 15:50:50 -05001565 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1566 png_error(png_ptr, "Missing IHDR before tRNS");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001567
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001568 else if (png_ptr->mode & PNG_HAVE_IDAT)
1569 {
1570 png_warning(png_ptr, "Invalid tRNS after IDAT");
1571 png_crc_finish(png_ptr, length);
1572 return;
1573 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001574
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001575 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001576 {
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001577 png_warning(png_ptr, "Duplicate tRNS chunk");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001578 png_crc_finish(png_ptr, length);
1579 return;
1580 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001581
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001582 if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
Guy Schalnat0d580581995-07-20 02:43:20 -05001583 {
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001584 png_byte buf[2];
Guy Schalnat0d580581995-07-20 02:43:20 -05001585
1586 if (length != 2)
1587 {
Guy Schalnat69b14481996-01-10 02:56:49 -06001588 png_warning(png_ptr, "Incorrect tRNS chunk length");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001589 png_crc_finish(png_ptr, length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001590 return;
1591 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001592
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001593 png_crc_read(png_ptr, buf, 2);
1594 png_ptr->num_trans = 1;
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -05001595 png_ptr->trans_color.gray = png_get_uint_16(buf);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001596 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001597
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001598 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
1599 {
1600 png_byte buf[6];
1601
1602 if (length != 6)
1603 {
1604 png_warning(png_ptr, "Incorrect tRNS chunk length");
1605 png_crc_finish(png_ptr, length);
1606 return;
1607 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001608
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001609 png_crc_read(png_ptr, buf, (png_size_t)length);
1610 png_ptr->num_trans = 1;
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -05001611 png_ptr->trans_color.red = png_get_uint_16(buf);
1612 png_ptr->trans_color.green = png_get_uint_16(buf + 2);
1613 png_ptr->trans_color.blue = png_get_uint_16(buf + 4);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001614 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001615
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001616 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1617 {
1618 if (!(png_ptr->mode & PNG_HAVE_PLTE))
1619 {
1620 /* Should be an error, but we can cope with it. */
1621 png_warning(png_ptr, "Missing PLTE before tRNS");
1622 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001623
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001624 if (length > (png_uint_32)png_ptr->num_palette ||
1625 length > PNG_MAX_PALETTE_LENGTH)
1626 {
1627 png_warning(png_ptr, "Incorrect tRNS chunk length");
1628 png_crc_finish(png_ptr, length);
1629 return;
1630 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001631
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001632 if (length == 0)
1633 {
1634 png_warning(png_ptr, "Zero length tRNS chunk");
1635 png_crc_finish(png_ptr, length);
1636 return;
1637 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001638
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001639 png_crc_read(png_ptr, readbuf, (png_size_t)length);
1640 png_ptr->num_trans = (png_uint_16)length;
1641 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001642
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001643 else
Guy Schalnate5a37791996-06-05 15:50:50 -05001644 {
1645 png_warning(png_ptr, "tRNS chunk not allowed with alpha channel");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001646 png_crc_finish(png_ptr, length);
Guy Schalnate5a37791996-06-05 15:50:50 -05001647 return;
1648 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001649
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001650 if (png_crc_finish(png_ptr, 0))
Glenn Randers-Pehrsona7dbcba2007-05-15 16:16:34 -05001651 {
1652 png_ptr->num_trans = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001653 return;
Glenn Randers-Pehrsona7dbcba2007-05-15 16:16:34 -05001654 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001655
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06001656 png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001657 &(png_ptr->trans_color));
Guy Schalnat0d580581995-07-20 02:43:20 -05001658}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001659#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001660
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001661#ifdef PNG_READ_bKGD_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001662void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001663png_handle_bKGD(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001664{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001665 png_size_t truelen;
Guy Schalnat0d580581995-07-20 02:43:20 -05001666 png_byte buf[6];
John Bowlercb0b2962011-05-12 21:48:29 -05001667 png_color_16 background;
Guy Schalnat0d580581995-07-20 02:43:20 -05001668
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001669 png_debug(1, "in png_handle_bKGD");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001670
Guy Schalnate5a37791996-06-05 15:50:50 -05001671 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1672 png_error(png_ptr, "Missing IHDR before bKGD");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001673
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001674 else if (png_ptr->mode & PNG_HAVE_IDAT)
1675 {
1676 png_warning(png_ptr, "Invalid bKGD after IDAT");
1677 png_crc_finish(png_ptr, length);
1678 return;
1679 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001680
Guy Schalnate5a37791996-06-05 15:50:50 -05001681 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001682 !(png_ptr->mode & PNG_HAVE_PLTE))
Guy Schalnate5a37791996-06-05 15:50:50 -05001683 {
1684 png_warning(png_ptr, "Missing PLTE before bKGD");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001685 png_crc_finish(png_ptr, length);
1686 return;
1687 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001688
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001689 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001690 {
1691 png_warning(png_ptr, "Duplicate bKGD chunk");
1692 png_crc_finish(png_ptr, length);
Guy Schalnate5a37791996-06-05 15:50:50 -05001693 return;
1694 }
1695
Guy Schalnat0d580581995-07-20 02:43:20 -05001696 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1697 truelen = 1;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001698
Guy Schalnat0d580581995-07-20 02:43:20 -05001699 else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
1700 truelen = 6;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001701
Guy Schalnat0d580581995-07-20 02:43:20 -05001702 else
1703 truelen = 2;
1704
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001705 if (length != truelen)
Guy Schalnat0d580581995-07-20 02:43:20 -05001706 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001707 png_warning(png_ptr, "Incorrect bKGD chunk length");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001708 png_crc_finish(png_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -05001709 return;
1710 }
1711
Andreas Dilger47a0c421997-05-16 02:46:07 -05001712 png_crc_read(png_ptr, buf, truelen);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001713
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001714 if (png_crc_finish(png_ptr, 0))
1715 return;
1716
Guy Schalnate5a37791996-06-05 15:50:50 -05001717 /* We convert the index value into RGB components so that we can allow
1718 * arbitrary RGB values for background when we have transparency, and
1719 * so it is easy to determine the RGB values of the background color
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001720 * from the info_ptr struct.
1721 */
Guy Schalnat0d580581995-07-20 02:43:20 -05001722 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Guy Schalnate5a37791996-06-05 15:50:50 -05001723 {
John Bowlercb0b2962011-05-12 21:48:29 -05001724 background.index = buf[0];
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001725
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001726 if (info_ptr && info_ptr->num_palette)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001727 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001728 if (buf[0] >= info_ptr->num_palette)
1729 {
1730 png_warning(png_ptr, "Incorrect bKGD chunk index value");
1731 return;
1732 }
1733
John Bowlercb0b2962011-05-12 21:48:29 -05001734 background.red = (png_uint_16)png_ptr->palette[buf[0]].red;
1735 background.green = (png_uint_16)png_ptr->palette[buf[0]].green;
1736 background.blue = (png_uint_16)png_ptr->palette[buf[0]].blue;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001737 }
John Bowlercb0b2962011-05-12 21:48:29 -05001738
1739 else
1740 background.red = background.green = background.blue = 0;
1741
1742 background.gray = 0;
Guy Schalnate5a37791996-06-05 15:50:50 -05001743 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001744
Andreas Dilger47a0c421997-05-16 02:46:07 -05001745 else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) /* GRAY */
Guy Schalnate5a37791996-06-05 15:50:50 -05001746 {
John Bowlercb0b2962011-05-12 21:48:29 -05001747 background.index = 0;
1748 background.red =
1749 background.green =
1750 background.blue =
1751 background.gray = png_get_uint_16(buf);
Guy Schalnate5a37791996-06-05 15:50:50 -05001752 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001753
Guy Schalnat0d580581995-07-20 02:43:20 -05001754 else
1755 {
John Bowlercb0b2962011-05-12 21:48:29 -05001756 background.index = 0;
1757 background.red = png_get_uint_16(buf);
1758 background.green = png_get_uint_16(buf + 2);
1759 background.blue = png_get_uint_16(buf + 4);
1760 background.gray = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -05001761 }
1762
John Bowlercb0b2962011-05-12 21:48:29 -05001763 png_set_bKGD(png_ptr, info_ptr, &background);
Guy Schalnat0d580581995-07-20 02:43:20 -05001764}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001765#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001766
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001767#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001768void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001769png_handle_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001770{
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001771 unsigned int num, i;
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001772 png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];
Guy Schalnat0d580581995-07-20 02:43:20 -05001773
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001774 png_debug(1, "in png_handle_hIST");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001775
Guy Schalnate5a37791996-06-05 15:50:50 -05001776 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1777 png_error(png_ptr, "Missing IHDR before hIST");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001778
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001779 else if (png_ptr->mode & PNG_HAVE_IDAT)
1780 {
1781 png_warning(png_ptr, "Invalid hIST after IDAT");
1782 png_crc_finish(png_ptr, length);
1783 return;
1784 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001785
Guy Schalnate5a37791996-06-05 15:50:50 -05001786 else if (!(png_ptr->mode & PNG_HAVE_PLTE))
1787 {
1788 png_warning(png_ptr, "Missing PLTE before hIST");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001789 png_crc_finish(png_ptr, length);
1790 return;
1791 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001792
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001793 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001794 {
1795 png_warning(png_ptr, "Duplicate hIST chunk");
1796 png_crc_finish(png_ptr, length);
Guy Schalnate5a37791996-06-05 15:50:50 -05001797 return;
1798 }
1799
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001800 num = length / 2 ;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001801
1802 if (num != (unsigned int)png_ptr->num_palette || num >
1803 (unsigned int)PNG_MAX_PALETTE_LENGTH)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001804 {
1805 png_warning(png_ptr, "Incorrect hIST chunk length");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001806 png_crc_finish(png_ptr, length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001807 return;
1808 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001809
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001810 for (i = 0; i < num; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001811 {
1812 png_byte buf[2];
1813
1814 png_crc_read(png_ptr, buf, 2);
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06001815 readbuf[i] = png_get_uint_16(buf);
Guy Schalnat0d580581995-07-20 02:43:20 -05001816 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001817
1818 if (png_crc_finish(png_ptr, 0))
1819 return;
1820
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06001821 png_set_hIST(png_ptr, info_ptr, readbuf);
Guy Schalnat0d580581995-07-20 02:43:20 -05001822}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001823#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001824
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001825#ifdef PNG_READ_pHYs_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001826void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001827png_handle_pHYs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001828{
1829 png_byte buf[9];
1830 png_uint_32 res_x, res_y;
1831 int unit_type;
1832
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001833 png_debug(1, "in png_handle_pHYs");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001834
Guy Schalnate5a37791996-06-05 15:50:50 -05001835 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001836 png_error(png_ptr, "Missing IHDR before pHYs");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001837
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001838 else if (png_ptr->mode & PNG_HAVE_IDAT)
1839 {
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001840 png_warning(png_ptr, "Invalid pHYs after IDAT");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001841 png_crc_finish(png_ptr, length);
1842 return;
1843 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001844
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001845 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001846 {
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001847 png_warning(png_ptr, "Duplicate pHYs chunk");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001848 png_crc_finish(png_ptr, length);
1849 return;
1850 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001851
Guy Schalnat0d580581995-07-20 02:43:20 -05001852 if (length != 9)
1853 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001854 png_warning(png_ptr, "Incorrect pHYs chunk length");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001855 png_crc_finish(png_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -05001856 return;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001857 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001858
1859 png_crc_read(png_ptr, buf, 9);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001860
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001861 if (png_crc_finish(png_ptr, 0))
1862 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001863
1864 res_x = png_get_uint_32(buf);
1865 res_y = png_get_uint_32(buf + 4);
1866 unit_type = buf[8];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001867 png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
Guy Schalnat0d580581995-07-20 02:43:20 -05001868}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001869#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001870
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001871#ifdef PNG_READ_oFFs_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001872void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001873png_handle_oFFs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001874{
1875 png_byte buf[9];
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001876 png_int_32 offset_x, offset_y;
Guy Schalnat0d580581995-07-20 02:43:20 -05001877 int unit_type;
1878
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001879 png_debug(1, "in png_handle_oFFs");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001880
Guy Schalnate5a37791996-06-05 15:50:50 -05001881 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1882 png_error(png_ptr, "Missing IHDR before oFFs");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001883
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001884 else if (png_ptr->mode & PNG_HAVE_IDAT)
1885 {
1886 png_warning(png_ptr, "Invalid oFFs after IDAT");
1887 png_crc_finish(png_ptr, length);
1888 return;
1889 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001890
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001891 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001892 {
1893 png_warning(png_ptr, "Duplicate oFFs chunk");
1894 png_crc_finish(png_ptr, length);
1895 return;
1896 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001897
Guy Schalnat0d580581995-07-20 02:43:20 -05001898 if (length != 9)
1899 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001900 png_warning(png_ptr, "Incorrect oFFs chunk length");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001901 png_crc_finish(png_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -05001902 return;
1903 }
1904
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001905 png_crc_read(png_ptr, buf, 9);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001906
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001907 if (png_crc_finish(png_ptr, 0))
1908 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001909
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001910 offset_x = png_get_int_32(buf);
1911 offset_y = png_get_int_32(buf + 4);
Guy Schalnat0d580581995-07-20 02:43:20 -05001912 unit_type = buf[8];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001913 png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
1914}
1915#endif
1916
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001917#ifdef PNG_READ_pCAL_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001918/* Read the pCAL chunk (described in the PNG Extensions document) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001919void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001920png_handle_pCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1921{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001922 png_int_32 X0, X1;
1923 png_byte type, nparams;
1924 png_charp buf, units, endptr;
1925 png_charpp params;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001926 png_size_t slength;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001927 int i;
1928
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001929 png_debug(1, "in png_handle_pCAL");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001930
1931 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1932 png_error(png_ptr, "Missing IHDR before pCAL");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001933
Andreas Dilger47a0c421997-05-16 02:46:07 -05001934 else if (png_ptr->mode & PNG_HAVE_IDAT)
1935 {
1936 png_warning(png_ptr, "Invalid pCAL after IDAT");
1937 png_crc_finish(png_ptr, length);
1938 return;
1939 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001940
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001941 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001942 {
1943 png_warning(png_ptr, "Duplicate pCAL chunk");
1944 png_crc_finish(png_ptr, length);
1945 return;
1946 }
1947
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001948 png_debug1(2, "Allocating and reading pCAL chunk data (%u bytes)",
1949 length + 1);
Glenn Randers-Pehrsond8d7b942008-07-21 10:34:34 -05001950 png_free(png_ptr, png_ptr->chunkdata);
1951 png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001952
Glenn Randers-Pehrsond8d7b942008-07-21 10:34:34 -05001953 if (png_ptr->chunkdata == NULL)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001954 {
1955 png_warning(png_ptr, "No memory for pCAL purpose");
1956 return;
1957 }
1958
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001959 slength = (png_size_t)length;
Glenn Randers-Pehrsond8d7b942008-07-21 10:34:34 -05001960 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001961
1962 if (png_crc_finish(png_ptr, 0))
1963 {
Glenn Randers-Pehrsond8d7b942008-07-21 10:34:34 -05001964 png_free(png_ptr, png_ptr->chunkdata);
1965 png_ptr->chunkdata = NULL;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001966 return;
1967 }
1968
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001969 png_ptr->chunkdata[slength] = 0x00; /* Null terminate the last string */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001970
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001971 png_debug(3, "Finding end of pCAL purpose string");
Glenn Randers-Pehrsond8d7b942008-07-21 10:34:34 -05001972 for (buf = png_ptr->chunkdata; *buf; buf++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001973 /* Empty loop */ ;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001974
Glenn Randers-Pehrsond8d7b942008-07-21 10:34:34 -05001975 endptr = png_ptr->chunkdata + slength;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001976
1977 /* We need to have at least 12 bytes after the purpose string
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001978 * in order to get the parameter information.
1979 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001980 if (endptr <= buf + 12)
1981 {
1982 png_warning(png_ptr, "Invalid pCAL data");
Glenn Randers-Pehrsond8d7b942008-07-21 10:34:34 -05001983 png_free(png_ptr, png_ptr->chunkdata);
1984 png_ptr->chunkdata = NULL;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001985 return;
1986 }
1987
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001988 png_debug(3, "Reading pCAL X0, X1, type, nparams, and units");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001989 X0 = png_get_int_32((png_bytep)buf+1);
1990 X1 = png_get_int_32((png_bytep)buf+5);
1991 type = buf[9];
1992 nparams = buf[10];
1993 units = buf + 11;
1994
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001995 png_debug(3, "Checking pCAL equation type and number of parameters");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001996 /* Check that we have the right number of parameters for known
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001997 * equation types.
1998 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001999 if ((type == PNG_EQUATION_LINEAR && nparams != 2) ||
2000 (type == PNG_EQUATION_BASE_E && nparams != 3) ||
2001 (type == PNG_EQUATION_ARBITRARY && nparams != 3) ||
2002 (type == PNG_EQUATION_HYPERBOLIC && nparams != 4))
2003 {
2004 png_warning(png_ptr, "Invalid pCAL parameters for equation type");
Glenn Randers-Pehrsond8d7b942008-07-21 10:34:34 -05002005 png_free(png_ptr, png_ptr->chunkdata);
2006 png_ptr->chunkdata = NULL;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002007 return;
2008 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002009
Andreas Dilger47a0c421997-05-16 02:46:07 -05002010 else if (type >= PNG_EQUATION_LAST)
2011 {
2012 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
2013 }
2014
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002015 for (buf = units; *buf; buf++)
Glenn Randers-Pehrsonf9f2fe01998-03-15 18:20:23 -06002016 /* Empty loop to move past the units string. */ ;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002017
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002018 png_debug(3, "Allocating pCAL parameters array");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002019
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05002020 params = (png_charpp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002021 (png_size_t)(nparams * png_sizeof(png_charp)));
2022
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002023 if (params == NULL)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002024 {
2025 png_free(png_ptr, png_ptr->chunkdata);
2026 png_ptr->chunkdata = NULL;
2027 png_warning(png_ptr, "No memory for pCAL params");
2028 return;
2029 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002030
2031 /* Get pointers to the start of each parameter string. */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002032 for (i = 0; i < (int)nparams; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002033 {
2034 buf++; /* Skip the null string terminator from previous parameter. */
2035
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002036 png_debug1(3, "Reading pCAL parameter %d", i);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002037
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002038 for (params[i] = buf; buf <= endptr && *buf != 0x00; buf++)
Glenn Randers-Pehrsonf9f2fe01998-03-15 18:20:23 -06002039 /* Empty loop to move past each parameter string */ ;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002040
2041 /* Make sure we haven't run out of data yet */
2042 if (buf > endptr)
2043 {
2044 png_warning(png_ptr, "Invalid pCAL data");
Glenn Randers-Pehrsond8d7b942008-07-21 10:34:34 -05002045 png_free(png_ptr, png_ptr->chunkdata);
2046 png_ptr->chunkdata = NULL;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002047 png_free(png_ptr, params);
2048 return;
2049 }
2050 }
2051
Glenn Randers-Pehrsond8d7b942008-07-21 10:34:34 -05002052 png_set_pCAL(png_ptr, info_ptr, png_ptr->chunkdata, X0, X1, type, nparams,
Andreas Dilger47a0c421997-05-16 02:46:07 -05002053 units, params);
2054
Glenn Randers-Pehrsond8d7b942008-07-21 10:34:34 -05002055 png_free(png_ptr, png_ptr->chunkdata);
2056 png_ptr->chunkdata = NULL;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002057 png_free(png_ptr, params);
Guy Schalnat0d580581995-07-20 02:43:20 -05002058}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002059#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002060
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002061#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002062/* Read the sCAL chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002063void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002064png_handle_sCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
2065{
John Bowler168a4332011-01-16 19:32:22 -06002066 png_size_t slength, i;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002067 int state;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002068
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002069 png_debug(1, "in png_handle_sCAL");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002070
2071 if (!(png_ptr->mode & PNG_HAVE_IHDR))
2072 png_error(png_ptr, "Missing IHDR before sCAL");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002073
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002074 else if (png_ptr->mode & PNG_HAVE_IDAT)
2075 {
2076 png_warning(png_ptr, "Invalid sCAL after IDAT");
2077 png_crc_finish(png_ptr, length);
2078 return;
2079 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002080
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002081 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002082 {
2083 png_warning(png_ptr, "Duplicate sCAL chunk");
2084 png_crc_finish(png_ptr, length);
2085 return;
2086 }
2087
Glenn Randers-Pehrson254a5132011-06-10 17:45:48 -05002088 /* Need unit type, width, \0, height: minimum 4 bytes */
2089 else if (length < 4)
2090 {
2091 png_warning(png_ptr, "sCAL chunk too short");
2092 png_crc_finish(png_ptr, length);
2093 return;
2094 }
2095
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002096 png_debug1(2, "Allocating and reading sCAL chunk data (%u bytes)",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002097 length + 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002098
Glenn Randers-Pehrsond8d7b942008-07-21 10:34:34 -05002099 png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002100
Glenn Randers-Pehrsond8d7b942008-07-21 10:34:34 -05002101 if (png_ptr->chunkdata == NULL)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002102 {
2103 png_warning(png_ptr, "Out of memory while processing sCAL chunk");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002104 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002105 return;
2106 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002107
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002108 slength = (png_size_t)length;
Glenn Randers-Pehrsond8d7b942008-07-21 10:34:34 -05002109 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002110 png_ptr->chunkdata[slength] = 0x00; /* Null terminate the last string */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002111
2112 if (png_crc_finish(png_ptr, 0))
2113 {
Glenn Randers-Pehrsond8d7b942008-07-21 10:34:34 -05002114 png_free(png_ptr, png_ptr->chunkdata);
2115 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002116 return;
2117 }
2118
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002119 /* Validate the unit. */
2120 if (png_ptr->chunkdata[0] != 1 && png_ptr->chunkdata[0] != 2)
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -05002121 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002122 png_warning(png_ptr, "Invalid sCAL ignored: invalid unit");
Glenn Randers-Pehrsonef3831a2010-06-22 13:03:32 -05002123 png_free(png_ptr, png_ptr->chunkdata);
2124 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05002125 return;
2126 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002127
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002128 /* Validate the ASCII numbers, need two ASCII numbers separated by
2129 * a '\0' and they need to fit exactly in the chunk data.
2130 */
Glenn Randers-Pehrson254a5132011-06-10 17:45:48 -05002131 i = 1;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002132 state = 0;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002133
John Bowler8d261262011-06-18 13:37:11 -05002134 if (!png_check_fp_number(png_ptr->chunkdata, slength, &state, &i) ||
John Bowler168a4332011-01-16 19:32:22 -06002135 i >= slength || png_ptr->chunkdata[i++] != 0)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002136 png_warning(png_ptr, "Invalid sCAL chunk ignored: bad width format");
2137
John Bowler8d261262011-06-18 13:37:11 -05002138 else if (!PNG_FP_IS_POSITIVE(state))
2139 png_warning(png_ptr, "Invalid sCAL chunk ignored: non-positive width");
2140
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002141 else
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -06002142 {
John Bowler168a4332011-01-16 19:32:22 -06002143 png_size_t heighti = i;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002144
Glenn Randers-Pehrson254a5132011-06-10 17:45:48 -05002145 state = 0;
John Bowler8d261262011-06-18 13:37:11 -05002146 if (!png_check_fp_number(png_ptr->chunkdata, slength, &state, &i) ||
John Bowler168a4332011-01-16 19:32:22 -06002147 i != slength)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002148 png_warning(png_ptr, "Invalid sCAL chunk ignored: bad height format");
2149
John Bowler8d261262011-06-18 13:37:11 -05002150 else if (!PNG_FP_IS_POSITIVE(state))
2151 png_warning(png_ptr,
2152 "Invalid sCAL chunk ignored: non-positive height");
2153
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002154 else
2155 /* This is the (only) success case. */
2156 png_set_sCAL_s(png_ptr, info_ptr, png_ptr->chunkdata[0],
2157 png_ptr->chunkdata+1, png_ptr->chunkdata+heighti);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002158 }
2159
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002160 /* Clean up - just free the temporarily allocated buffer. */
Glenn Randers-Pehrsond8d7b942008-07-21 10:34:34 -05002161 png_free(png_ptr, png_ptr->chunkdata);
2162 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002163}
2164#endif
2165
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002166#ifdef PNG_READ_tIME_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002167void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002168png_handle_tIME(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002169{
2170 png_byte buf[7];
2171 png_time mod_time;
2172
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002173 png_debug(1, "in png_handle_tIME");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002174
Guy Schalnate5a37791996-06-05 15:50:50 -05002175 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002176 png_error(png_ptr, "Out of place tIME chunk");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002177
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002178 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002179 {
2180 png_warning(png_ptr, "Duplicate tIME chunk");
2181 png_crc_finish(png_ptr, length);
2182 return;
2183 }
2184
2185 if (png_ptr->mode & PNG_HAVE_IDAT)
2186 png_ptr->mode |= PNG_AFTER_IDAT;
Guy Schalnate5a37791996-06-05 15:50:50 -05002187
Guy Schalnat0d580581995-07-20 02:43:20 -05002188 if (length != 7)
2189 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002190 png_warning(png_ptr, "Incorrect tIME chunk length");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002191 png_crc_finish(png_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -05002192 return;
2193 }
2194
2195 png_crc_read(png_ptr, buf, 7);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002196
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002197 if (png_crc_finish(png_ptr, 0))
2198 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05002199
2200 mod_time.second = buf[6];
2201 mod_time.minute = buf[5];
2202 mod_time.hour = buf[4];
2203 mod_time.day = buf[3];
2204 mod_time.month = buf[2];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002205 mod_time.year = png_get_uint_16(buf);
Guy Schalnat0d580581995-07-20 02:43:20 -05002206
Andreas Dilger47a0c421997-05-16 02:46:07 -05002207 png_set_tIME(png_ptr, info_ptr, &mod_time);
Guy Schalnat0d580581995-07-20 02:43:20 -05002208}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002209#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002210
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002211#ifdef PNG_READ_tEXt_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002212/* Note: this does not properly handle chunks that are > 64K under DOS */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002213void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002214png_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002215{
Andreas Dilger47a0c421997-05-16 02:46:07 -05002216 png_textp text_ptr;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002217 png_charp key;
Guy Schalnat6d764711995-12-19 03:22:19 -06002218 png_charp text;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002219 png_uint_32 skip = 0;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06002220 png_size_t slength;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002221 int ret;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002222
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002223 png_debug(1, "in png_handle_tEXt");
Guy Schalnat0d580581995-07-20 02:43:20 -05002224
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06002225#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002226 if (png_ptr->user_chunk_cache_max != 0)
2227 {
2228 if (png_ptr->user_chunk_cache_max == 1)
2229 {
2230 png_crc_finish(png_ptr, length);
2231 return;
2232 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002233
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002234 if (--png_ptr->user_chunk_cache_max == 1)
2235 {
2236 png_warning(png_ptr, "No space in chunk cache for tEXt");
2237 png_crc_finish(png_ptr, length);
2238 return;
2239 }
2240 }
2241#endif
2242
Guy Schalnate5a37791996-06-05 15:50:50 -05002243 if (!(png_ptr->mode & PNG_HAVE_IHDR))
2244 png_error(png_ptr, "Missing IHDR before tEXt");
2245
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002246 if (png_ptr->mode & PNG_HAVE_IDAT)
2247 png_ptr->mode |= PNG_AFTER_IDAT;
2248
Andreas Dilger47a0c421997-05-16 02:46:07 -05002249#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002250 if (length > (png_uint_32)65535L)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002251 {
2252 png_warning(png_ptr, "tEXt chunk too large to fit in memory");
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002253 skip = length - (png_uint_32)65535L;
2254 length = (png_uint_32)65535L;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002255 }
2256#endif
2257
Glenn Randers-Pehrson97a9b482008-10-25 20:03:28 -05002258 png_free(png_ptr, png_ptr->chunkdata);
2259
2260 png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002261
Glenn Randers-Pehrson97a9b482008-10-25 20:03:28 -05002262 if (png_ptr->chunkdata == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002263 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05002264 png_warning(png_ptr, "No memory to process text chunk");
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002265 return;
2266 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002267
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06002268 slength = (png_size_t)length;
Glenn Randers-Pehrson97a9b482008-10-25 20:03:28 -05002269 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002270
2271 if (png_crc_finish(png_ptr, skip))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002272 {
Glenn Randers-Pehrson97a9b482008-10-25 20:03:28 -05002273 png_free(png_ptr, png_ptr->chunkdata);
2274 png_ptr->chunkdata = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002275 return;
2276 }
2277
Glenn Randers-Pehrson97a9b482008-10-25 20:03:28 -05002278 key = png_ptr->chunkdata;
2279
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06002280 key[slength] = 0x00;
Guy Schalnat0d580581995-07-20 02:43:20 -05002281
2282 for (text = key; *text; text++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002283 /* Empty loop to find end of key */ ;
Guy Schalnat0d580581995-07-20 02:43:20 -05002284
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06002285 if (text != key + slength)
Guy Schalnat0d580581995-07-20 02:43:20 -05002286 text++;
2287
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002288 text_ptr = (png_textp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002289 png_sizeof(png_text));
2290
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002291 if (text_ptr == NULL)
2292 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002293 png_warning(png_ptr, "Not enough memory to process text chunk");
2294 png_free(png_ptr, png_ptr->chunkdata);
2295 png_ptr->chunkdata = NULL;
2296 return;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002297 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002298
Andreas Dilger47a0c421997-05-16 02:46:07 -05002299 text_ptr->compression = PNG_TEXT_COMPRESSION_NONE;
2300 text_ptr->key = key;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002301 text_ptr->lang = NULL;
2302 text_ptr->lang_key = NULL;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002303 text_ptr->itxt_length = 0;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002304 text_ptr->text = text;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002305 text_ptr->text_length = png_strlen(text);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002306
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002307 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002308
Glenn Randers-Pehrson97a9b482008-10-25 20:03:28 -05002309 png_free(png_ptr, png_ptr->chunkdata);
2310 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05002311 png_free(png_ptr, text_ptr);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002312
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002313 if (ret)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002314 png_warning(png_ptr, "Insufficient memory to process text chunk");
Guy Schalnat0d580581995-07-20 02:43:20 -05002315}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002316#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002317
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002318#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002319/* Note: this does not correctly handle chunks that are > 64K under DOS */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002320void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002321png_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002322{
Andreas Dilger47a0c421997-05-16 02:46:07 -05002323 png_textp text_ptr;
Guy Schalnat6d764711995-12-19 03:22:19 -06002324 png_charp text;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06002325 int comp_type;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002326 int ret;
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06002327 png_size_t slength, prefix_len, data_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002328
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002329 png_debug(1, "in png_handle_zTXt");
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002330
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06002331#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002332 if (png_ptr->user_chunk_cache_max != 0)
2333 {
2334 if (png_ptr->user_chunk_cache_max == 1)
2335 {
2336 png_crc_finish(png_ptr, length);
2337 return;
2338 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002339
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002340 if (--png_ptr->user_chunk_cache_max == 1)
2341 {
2342 png_warning(png_ptr, "No space in chunk cache for zTXt");
2343 png_crc_finish(png_ptr, length);
2344 return;
2345 }
2346 }
2347#endif
2348
Guy Schalnate5a37791996-06-05 15:50:50 -05002349 if (!(png_ptr->mode & PNG_HAVE_IHDR))
2350 png_error(png_ptr, "Missing IHDR before zTXt");
2351
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002352 if (png_ptr->mode & PNG_HAVE_IDAT)
2353 png_ptr->mode |= PNG_AFTER_IDAT;
2354
Andreas Dilger47a0c421997-05-16 02:46:07 -05002355#ifdef PNG_MAX_MALLOC_64K
2356 /* We will no doubt have problems with chunks even half this size, but
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002357 * there is no hard and fast rule to tell us where to stop.
2358 */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002359 if (length > (png_uint_32)65535L)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002360 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002361 png_warning(png_ptr, "zTXt chunk too large to fit in memory");
2362 png_crc_finish(png_ptr, length);
2363 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002364 }
2365#endif
2366
Glenn Randers-Pehrson97a9b482008-10-25 20:03:28 -05002367 png_free(png_ptr, png_ptr->chunkdata);
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002368 png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002369
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002370 if (png_ptr->chunkdata == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002371 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002372 png_warning(png_ptr, "Out of memory processing zTXt chunk");
2373 return;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002374 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002375
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002376 slength = (png_size_t)length;
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002377 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002378
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002379 if (png_crc_finish(png_ptr, 0))
2380 {
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002381 png_free(png_ptr, png_ptr->chunkdata);
2382 png_ptr->chunkdata = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002383 return;
2384 }
2385
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002386 png_ptr->chunkdata[slength] = 0x00;
Guy Schalnat0d580581995-07-20 02:43:20 -05002387
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002388 for (text = png_ptr->chunkdata; *text; text++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002389 /* Empty loop */ ;
Guy Schalnat0d580581995-07-20 02:43:20 -05002390
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002391 /* zTXt must have some text after the chunkdataword */
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002392 if (text >= png_ptr->chunkdata + slength - 2)
Guy Schalnat0d580581995-07-20 02:43:20 -05002393 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002394 png_warning(png_ptr, "Truncated zTXt chunk");
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002395 png_free(png_ptr, png_ptr->chunkdata);
2396 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002397 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05002398 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002399
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002400 else
Guy Schalnat0d580581995-07-20 02:43:20 -05002401 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002402 comp_type = *(++text);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002403
Glenn Randers-Pehrsonf05f8032000-12-23 14:27:39 -06002404 if (comp_type != PNG_TEXT_COMPRESSION_zTXt)
2405 {
2406 png_warning(png_ptr, "Unknown compression type in zTXt chunk");
2407 comp_type = PNG_TEXT_COMPRESSION_zTXt;
2408 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002409
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002410 text++; /* Skip the compression_method byte */
Guy Schalnat0d580581995-07-20 02:43:20 -05002411 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002412
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002413 prefix_len = text - png_ptr->chunkdata;
Guy Schalnat0d580581995-07-20 02:43:20 -05002414
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -05002415 png_decompress_chunk(png_ptr, comp_type,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002416 (png_size_t)length, prefix_len, &data_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002417
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002418 text_ptr = (png_textp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002419 png_sizeof(png_text));
2420
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002421 if (text_ptr == NULL)
2422 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002423 png_warning(png_ptr, "Not enough memory to process zTXt chunk");
2424 png_free(png_ptr, png_ptr->chunkdata);
2425 png_ptr->chunkdata = NULL;
2426 return;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002427 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002428
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05002429 text_ptr->compression = comp_type;
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002430 text_ptr->key = png_ptr->chunkdata;
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05002431 text_ptr->lang = NULL;
2432 text_ptr->lang_key = NULL;
2433 text_ptr->itxt_length = 0;
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002434 text_ptr->text = png_ptr->chunkdata + prefix_len;
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05002435 text_ptr->text_length = data_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002436
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002437 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002438
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05002439 png_free(png_ptr, text_ptr);
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002440 png_free(png_ptr, png_ptr->chunkdata);
2441 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002442
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002443 if (ret)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002444 png_error(png_ptr, "Insufficient memory to store zTXt chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002445}
2446#endif
2447
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002448#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002449/* Note: this does not correctly handle chunks that are > 64K under DOS */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002450void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002451png_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
2452{
2453 png_textp text_ptr;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002454 png_charp key, lang, text, lang_key;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06002455 int comp_flag;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002456 int comp_type = 0;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002457 int ret;
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06002458 png_size_t slength, prefix_len, data_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002459
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002460 png_debug(1, "in png_handle_iTXt");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002461
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06002462#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002463 if (png_ptr->user_chunk_cache_max != 0)
2464 {
2465 if (png_ptr->user_chunk_cache_max == 1)
2466 {
2467 png_crc_finish(png_ptr, length);
2468 return;
2469 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002470
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002471 if (--png_ptr->user_chunk_cache_max == 1)
2472 {
2473 png_warning(png_ptr, "No space in chunk cache for iTXt");
2474 png_crc_finish(png_ptr, length);
2475 return;
2476 }
2477 }
2478#endif
2479
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002480 if (!(png_ptr->mode & PNG_HAVE_IHDR))
2481 png_error(png_ptr, "Missing IHDR before iTXt");
2482
2483 if (png_ptr->mode & PNG_HAVE_IDAT)
2484 png_ptr->mode |= PNG_AFTER_IDAT;
2485
2486#ifdef PNG_MAX_MALLOC_64K
2487 /* We will no doubt have problems with chunks even half this size, but
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002488 * there is no hard and fast rule to tell us where to stop.
2489 */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002490 if (length > (png_uint_32)65535L)
2491 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002492 png_warning(png_ptr, "iTXt chunk too large to fit in memory");
2493 png_crc_finish(png_ptr, length);
2494 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002495 }
2496#endif
2497
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002498 png_free(png_ptr, png_ptr->chunkdata);
2499 png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002500
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002501 if (png_ptr->chunkdata == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002502 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002503 png_warning(png_ptr, "No memory to process iTXt chunk");
2504 return;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002505 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002506
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05002507 slength = (png_size_t)length;
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002508 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002509
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002510 if (png_crc_finish(png_ptr, 0))
2511 {
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002512 png_free(png_ptr, png_ptr->chunkdata);
2513 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002514 return;
2515 }
2516
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002517 png_ptr->chunkdata[slength] = 0x00;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002518
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002519 for (lang = png_ptr->chunkdata; *lang; lang++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002520 /* Empty loop */ ;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002521
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002522 lang++; /* Skip NUL separator */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002523
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002524 /* iTXt must have a language tag (possibly empty), two compression bytes,
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002525 * translated keyword (possibly empty), and possibly some text after the
2526 * keyword
2527 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002528
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002529 if (lang >= png_ptr->chunkdata + slength - 3)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002530 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002531 png_warning(png_ptr, "Truncated iTXt chunk");
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002532 png_free(png_ptr, png_ptr->chunkdata);
2533 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002534 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002535 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002536
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002537 else
2538 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002539 comp_flag = *lang++;
2540 comp_type = *lang++;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002541 }
2542
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002543 for (lang_key = lang; *lang_key; lang_key++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002544 /* Empty loop */ ;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002545
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002546 lang_key++; /* Skip NUL separator */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002547
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002548 if (lang_key >= png_ptr->chunkdata + slength)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002549 {
2550 png_warning(png_ptr, "Truncated iTXt chunk");
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002551 png_free(png_ptr, png_ptr->chunkdata);
2552 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002553 return;
2554 }
2555
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002556 for (text = lang_key; *text; text++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002557 /* Empty loop */ ;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002558
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002559 text++; /* Skip NUL separator */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002560
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002561 if (text >= png_ptr->chunkdata + slength)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002562 {
2563 png_warning(png_ptr, "Malformed iTXt chunk");
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002564 png_free(png_ptr, png_ptr->chunkdata);
2565 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002566 return;
2567 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002568
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002569 prefix_len = text - png_ptr->chunkdata;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002570
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002571 key=png_ptr->chunkdata;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002572
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002573 if (comp_flag)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002574 png_decompress_chunk(png_ptr, comp_type,
2575 (size_t)length, prefix_len, &data_len);
2576
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06002577 else
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002578 data_len = png_strlen(png_ptr->chunkdata + prefix_len);
2579
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002580 text_ptr = (png_textp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002581 png_sizeof(png_text));
2582
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002583 if (text_ptr == NULL)
2584 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002585 png_warning(png_ptr, "Not enough memory to process iTXt chunk");
2586 png_free(png_ptr, png_ptr->chunkdata);
2587 png_ptr->chunkdata = NULL;
2588 return;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002589 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002590
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05002591 text_ptr->compression = (int)comp_flag + 1;
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002592 text_ptr->lang_key = png_ptr->chunkdata + (lang_key - key);
2593 text_ptr->lang = png_ptr->chunkdata + (lang - key);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05002594 text_ptr->itxt_length = data_len;
2595 text_ptr->text_length = 0;
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002596 text_ptr->key = png_ptr->chunkdata;
2597 text_ptr->text = png_ptr->chunkdata + prefix_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002598
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002599 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002600
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05002601 png_free(png_ptr, text_ptr);
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05002602 png_free(png_ptr, png_ptr->chunkdata);
2603 png_ptr->chunkdata = NULL;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002604
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002605 if (ret)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002606 png_error(png_ptr, "Insufficient memory to store iTXt chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002607}
2608#endif
2609
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002610/* This function is called when we haven't found a handler for a
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002611 * chunk. If there isn't a problem with the chunk itself (ie bad
2612 * chunk name, CRC, or a critical chunk), the chunk is silently ignored
2613 * -- unless the PNG_FLAG_UNKNOWN_CHUNKS_SUPPORTED flag is on in which
2614 * case it will be saved away to be written out later.
2615 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002616void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002617png_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
2618{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002619 png_uint_32 skip = 0;
2620
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002621 png_debug(1, "in png_handle_unknown");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002622
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06002623#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002624 if (png_ptr->user_chunk_cache_max != 0)
2625 {
2626 if (png_ptr->user_chunk_cache_max == 1)
2627 {
2628 png_crc_finish(png_ptr, length);
2629 return;
2630 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002631
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002632 if (--png_ptr->user_chunk_cache_max == 1)
2633 {
2634 png_warning(png_ptr, "No space in chunk cache for unknown chunk");
2635 png_crc_finish(png_ptr, length);
2636 return;
2637 }
2638 }
2639#endif
2640
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002641 if (png_ptr->mode & PNG_HAVE_IDAT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002642 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002643 if (png_ptr->chunk_name != png_IDAT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002644 png_ptr->mode |= PNG_AFTER_IDAT;
2645 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002646
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002647 if (PNG_CHUNK_CRITICAL(png_ptr->chunk_name))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002648 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002649#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002650 if (png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name) !=
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002651 PNG_HANDLE_CHUNK_ALWAYS
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002652#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002653 && png_ptr->read_user_chunk_fn == NULL
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002654#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002655 )
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05002656#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002657 png_chunk_error(png_ptr, "unknown critical chunk");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002658 }
2659
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002660#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson7824a702009-06-13 10:05:05 -05002661 if ((png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS)
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002662#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
Glenn Randers-Pehrson7824a702009-06-13 10:05:05 -05002663 || (png_ptr->read_user_chunk_fn != NULL)
2664#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002665 )
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002666 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002667#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002668 if (length > 65535)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002669 {
2670 png_warning(png_ptr, "unknown chunk too large to fit in memory");
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002671 skip = length - 65535;
2672 length = 65535;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002673 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002674#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002675
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002676 /* TODO: this code is very close to the unknown handling in pngpread.c,
2677 * maybe it can be put into a common utility routine?
2678 * png_struct::unknown_chunk is just used as a temporary variable, along
2679 * with the data into which the chunk is read. These can be eliminated.
2680 */
2681 PNG_CSTRING_FROM_CHUNK(png_ptr->unknown_chunk.name, png_ptr->chunk_name);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002682 png_ptr->unknown_chunk.size = (png_size_t)length;
2683
2684 if (length == 0)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002685 png_ptr->unknown_chunk.data = NULL;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002686
2687 else
2688 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002689 png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr, length);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002690 png_crc_read(png_ptr, png_ptr->unknown_chunk.data, length);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002691 }
2692
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002693#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002694 if (png_ptr->read_user_chunk_fn != NULL)
2695 {
2696 /* Callback to user unknown chunk handler */
2697 int ret;
2698
2699 ret = (*(png_ptr->read_user_chunk_fn))
2700 (png_ptr, &png_ptr->unknown_chunk);
2701
2702 if (ret < 0)
2703 png_chunk_error(png_ptr, "error in user chunk");
2704
2705 if (ret == 0)
2706 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002707 if (PNG_CHUNK_CRITICAL(png_ptr->chunk_name))
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002708 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002709#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002710 if (png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name) !=
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002711 PNG_HANDLE_CHUNK_ALWAYS)
Glenn Randers-Pehrson7824a702009-06-13 10:05:05 -05002712#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002713 png_chunk_error(png_ptr, "unknown critical chunk");
2714 }
2715
2716 png_set_unknown_chunks(png_ptr, info_ptr,
2717 &png_ptr->unknown_chunk, 1);
2718 }
2719 }
2720
2721 else
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002722#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002723 png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1);
2724
2725 png_free(png_ptr, png_ptr->unknown_chunk.data);
2726 png_ptr->unknown_chunk.data = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002727 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002728
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002729 else
2730#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002731 skip = length;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002732
2733 png_crc_finish(png_ptr, skip);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002734
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -05002735#ifndef PNG_READ_USER_CHUNKS_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002736 PNG_UNUSED(info_ptr) /* Quiet compiler warnings about unused info_ptr */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002737#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002738}
2739
2740/* This function is called to verify that a chunk name is valid.
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002741 * This function can't have the "critical chunk check" incorporated
2742 * into it, since in the future we will need to be able to call user
2743 * functions to handle unknown critical chunks after we check that
2744 * the chunk name itself is valid.
2745 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002746
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002747/* Bit hacking: the test for an invalid byte in the 4 byte chunk name is:
2748 *
2749 * ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
2750 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002751
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002752void /* PRIVATE */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002753png_check_chunk_name(png_structp png_ptr, png_uint_32 chunk_name)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002754{
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002755 int i;
2756
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002757 png_debug(1, "in png_check_chunk_name");
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002758
2759 for (i=1; i<=4; ++i)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002760 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002761 int c = chunk_name & 0xff;
2762
2763 if (c < 65 || c > 122 || (c > 90 && c < 97))
2764 png_chunk_error(png_ptr, "invalid chunk type");
2765
2766 chunk_name >>= 8;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002767 }
2768}
2769
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -05002770/* Combines the row recently read in with the existing pixels in the
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002771 * row. This routine takes care of alpha and transparency if requested.
2772 * This routine also handles the two methods of progressive display
2773 * of interlaced images, depending on the mask value.
2774 * The mask value describes which pixels are to be combined with
2775 * the row. The pattern always repeats every 8 pixels, so just 8
2776 * bits are needed. A one indicates the pixel is to be combined,
2777 * a zero indicates the pixel is to be skipped. This is in addition
2778 * to any alpha or transparency value associated with the pixel. If
2779 * you want all pixels to be combined, pass 0xff (255) in mask.
2780 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002781
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002782void /* PRIVATE */
John Bowlerac8375d2011-10-06 22:27:16 -05002783png_combine_row(png_structp png_ptr, png_bytep dp, int display)
Guy Schalnat0d580581995-07-20 02:43:20 -05002784{
John Bowler4e68aa72011-10-11 16:01:33 -05002785 unsigned int pixel_depth = png_ptr->transformed_pixel_depth;
2786 png_const_bytep sp = png_ptr->row_buf + 1;
John Bowlerac8375d2011-10-06 22:27:16 -05002787 png_uint_32 row_width = png_ptr->width;
John Bowler4e68aa72011-10-11 16:01:33 -05002788 unsigned int pass = png_ptr->pass;
John Bowlerfb5b3ac2011-10-16 22:52:56 -05002789 png_bytep end_ptr = 0;
2790 png_byte end_byte = 0;
2791 unsigned int end_mask;
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002792
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002793 png_debug(1, "in png_combine_row");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002794
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002795 /* Added in 1.5.6: it should not be possible to enter this routine until at
2796 * least one row has been read from the PNG data and transformed.
2797 */
2798 if (pixel_depth == 0)
2799 png_error(png_ptr, "internal row logic error");
2800
2801 /* Added in 1.5.4: the pixel depth should match the information returned by
2802 * any call to png_read_update_info at this point. Do not continue if we got
John Bowler9994f252011-05-15 18:52:39 -05002803 * this wrong.
2804 */
2805 if (png_ptr->info_rowbytes != 0 && png_ptr->info_rowbytes !=
John Bowlerac8375d2011-10-06 22:27:16 -05002806 PNG_ROWBYTES(pixel_depth, row_width))
John Bowler9994f252011-05-15 18:52:39 -05002807 png_error(png_ptr, "internal row size calculation error");
2808
John Bowlerac8375d2011-10-06 22:27:16 -05002809 /* Don't expect this to ever happen: */
2810 if (row_width == 0)
2811 png_error(png_ptr, "internal row width error");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002812
John Bowlerfb5b3ac2011-10-16 22:52:56 -05002813 /* Preserve the last byte in cases where only part of it will be overwritten,
2814 * the multiply below may overflow, we don't care because ANSI-C guarantees
2815 * we get the low bits.
2816 */
2817 end_mask = (pixel_depth * row_width) & 7;
2818 if (end_mask != 0)
2819 {
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05002820 /* end_ptr == NULL is a flag to say do nothing */
John Bowlerfb5b3ac2011-10-16 22:52:56 -05002821 end_ptr = dp + PNG_ROWBYTES(pixel_depth, row_width) - 1;
2822 end_byte = *end_ptr;
2823# ifdef PNG_READ_PACKSWAP_SUPPORTED
2824 if (png_ptr->transformations & PNG_PACKSWAP) /* little-endian byte */
2825 end_mask = 0xff << end_mask;
2826
2827 else /* big-endian byte */
2828# endif
2829 end_mask = 0xff >> end_mask;
2830 /* end_mask is now the bits to *keep* from the destination row */
2831 }
2832
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05002833 /* For non-interlaced images this reduces to a png_memcpy(). A png_memcpy()
2834 * will also happen if interlacing isn't supported or if the application
2835 * does not call png_set_interlace_handling(). In the latter cases the
2836 * caller just gets a sequence of the unexpanded rows from each interlace
2837 * pass.
John Bowlerac8375d2011-10-06 22:27:16 -05002838 */
2839#ifdef PNG_READ_INTERLACING_SUPPORTED
2840 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE) &&
John Bowler4e68aa72011-10-11 16:01:33 -05002841 pass < 6 && (display == 0 ||
2842 /* The following copies everything for 'display' on passes 0, 2 and 4. */
2843 (display == 1 && (pass & 1) != 0)))
Guy Schalnat0d580581995-07-20 02:43:20 -05002844 {
John Bowler4e68aa72011-10-11 16:01:33 -05002845 /* Narrow images may have no bits in a pass; the caller should handle
2846 * this, but this test is cheap:
John Bowlerac8375d2011-10-06 22:27:16 -05002847 */
John Bowler4e68aa72011-10-11 16:01:33 -05002848 if (row_width <= PNG_PASS_START_COL(pass))
2849 return;
John Bowlerac8375d2011-10-06 22:27:16 -05002850
John Bowler4e68aa72011-10-11 16:01:33 -05002851 if (pixel_depth < 8)
Guy Schalnat0d580581995-07-20 02:43:20 -05002852 {
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05002853 /* For pixel depths up to 4-bpp the 8-pixel mask can be expanded to fit
John Bowler4e68aa72011-10-11 16:01:33 -05002854 * into 32 bits, then a single loop over the bytes using the four byte
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05002855 * values in the 32-bit mask can be used. For the 'display' option the
John Bowler4e68aa72011-10-11 16:01:33 -05002856 * expanded mask may also not require any masking within a byte. To
2857 * make this work the PACKSWAP option must be taken into account - it
2858 * simply requires the pixels to be reversed in each byte.
2859 *
2860 * The 'regular' case requires a mask for each of the first 6 passes,
2861 * the 'display' case does a copy for the even passes in the range
2862 * 0..6. This has already been handled in the tst above.
2863 *
2864 * The masks are arranged as four bytes with the first byte to use in
2865 * the lowest bits (little-endian) regardless of the order (PACKSWAP or
2866 * not) of the pixels in each byte.
2867 *
2868 * NOTE: the whole of this logic depends on the caller of this function
2869 * only calling it on rows appropriate to the pass. This function only
2870 * understands the 'x' logic, the 'y' logic is handled by the caller.
2871 *
2872 * The following defines allow generation of compile time constant bit
2873 * masks for each pixel depth and each possibility of swapped or not
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05002874 * swapped bytes. Pass 'p' is in the range 0..6; 'x', a pixel index,
2875 * is in the range 0..7; and the result is 1 if the pixel is to be
2876 * copied in the pass, 0 if not. 'S' is for the sparkle method, 'B'
2877 * for the block method.
2878 *
John Bowler92ef3132011-10-27 19:36:08 -05002879 * With some compilers a compile time expression of the general form:
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05002880 *
John Bowler92ef3132011-10-27 19:36:08 -05002881 * (shift >= 32) ? (a >> (shift-32)) : (b >> shift)
2882 *
2883 * Produces warnings with values of 'shift' in the range 33 to 63
2884 * because the right hand side of the ?: expression is evalulated by
2885 * the compiler even though it isn't used. Microsoft Visual C (various
2886 * versions) and the Intel C compiler are known to do this. To avoid
2887 * this the following macros are used in 1.5.6. This is a temporary
2888 * solution to avoid destablizing the code during the release process.
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05002889 */
John Bowler92ef3132011-10-27 19:36:08 -05002890# if PNG_USE_COMPILE_TIME_MASKS
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05002891# define PNG_LSR(x,s) ((x)>>((s) & 0x1f))
2892# define PNG_LSL(x,s) ((x)<<((s) & 0x1f))
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05002893# else
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05002894# define PNG_LSR(x,s) ((x)>>(s))
2895# define PNG_LSL(x,s) ((x)<<(s))
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05002896# endif
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05002897# define S_COPY(p,x) (((p)<4 ? PNG_LSR(0x80088822,(3-(p))*8+(7-(x))) :\
2898 PNG_LSR(0xaa55ff00,(7-(p))*8+(7-(x)))) & 1)
2899# define B_COPY(p,x) (((p)<4 ? PNG_LSR(0xff0fff33,(3-(p))*8+(7-(x))) :\
2900 PNG_LSR(0xff55ff00,(7-(p))*8+(7-(x)))) & 1)
Guy Schalnat0d580581995-07-20 02:43:20 -05002901
John Bowler4e68aa72011-10-11 16:01:33 -05002902 /* Return a mask for pass 'p' pixel 'x' at depth 'd'. The mask is
2903 * little endian - the first pixel is at bit 0 - however the extra
2904 * parameter 's' can be set to cause the mask position to be swapped
2905 * within each byte, to match the PNG format. This is done by XOR of
2906 * the shift with 7, 6 or 4 for bit depths 1, 2 and 4.
2907 */
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05002908# define PIXEL_MASK(p,x,d,s) \
2909 (PNG_LSL(((PNG_LSL(1U,(d)))-1),(((x)*(d))^((s)?8-(d):0))))
John Bowlerac8375d2011-10-06 22:27:16 -05002910
John Bowler4e68aa72011-10-11 16:01:33 -05002911 /* Hence generate the appropriate 'block' or 'sparkle' pixel copy mask.
2912 */
2913# define S_MASKx(p,x,d,s) (S_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
2914# define B_MASKx(p,x,d,s) (B_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
John Bowlerac8375d2011-10-06 22:27:16 -05002915
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05002916 /* Combine 8 of these to get the full mask. For the 1-bpp and 2-bpp
2917 * cases the result needs replicating, for the 4-bpp case the above
2918 * generates a full 32 bits.
John Bowler4e68aa72011-10-11 16:01:33 -05002919 */
2920# define MASK_EXPAND(m,d) ((m)*((d)==1?0x01010101:((d)==2?0x00010001:1)))
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002921
John Bowler4e68aa72011-10-11 16:01:33 -05002922# define S_MASK(p,d,s) MASK_EXPAND(S_MASKx(p,0,d,s) + S_MASKx(p,1,d,s) +\
2923 S_MASKx(p,2,d,s) + S_MASKx(p,3,d,s) + S_MASKx(p,4,d,s) +\
2924 S_MASKx(p,5,d,s) + S_MASKx(p,6,d,s) + S_MASKx(p,7,d,s), d)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002925
John Bowler4e68aa72011-10-11 16:01:33 -05002926# define B_MASK(p,d,s) MASK_EXPAND(B_MASKx(p,0,d,s) + B_MASKx(p,1,d,s) +\
2927 B_MASKx(p,2,d,s) + B_MASKx(p,3,d,s) + B_MASKx(p,4,d,s) +\
2928 B_MASKx(p,5,d,s) + B_MASKx(p,6,d,s) + B_MASKx(p,7,d,s), d)
John Bowlerac8375d2011-10-06 22:27:16 -05002929
John Bowler4e68aa72011-10-11 16:01:33 -05002930#if PNG_USE_COMPILE_TIME_MASKS
2931 /* Utility macros to construct all the masks for a depth/swap
2932 * combination. The 's' parameter says whether the format is PNG
2933 * (big endian bytes) or not. Only the three odd numbered passes are
2934 * required for the display/block algorithm.
2935 */
2936# define S_MASKS(d,s) { S_MASK(0,d,s), S_MASK(1,d,s), S_MASK(2,d,s),\
2937 S_MASK(3,d,s), S_MASK(4,d,s), S_MASK(5,d,s) }
John Bowlerac8375d2011-10-06 22:27:16 -05002938
John Bowler4e68aa72011-10-11 16:01:33 -05002939# define B_MASKS(d,s) { B_MASK(1,d,s), S_MASK(3,d,s), S_MASK(5,d,s) }
John Bowlerac8375d2011-10-06 22:27:16 -05002940
John Bowler4e68aa72011-10-11 16:01:33 -05002941# define DEPTH_INDEX(d) ((d)==1?0:((d)==2?1:2))
John Bowlerac8375d2011-10-06 22:27:16 -05002942
John Bowler4e68aa72011-10-11 16:01:33 -05002943 /* Hence the pre-compiled masks indexed by PACKSWAP (or not), depth and
2944 * then pass:
2945 */
2946 static PNG_CONST png_uint_32 row_mask[2/*PACKSWAP*/][3/*depth*/][6] = {
2947 /* Little-endian byte masks for PACKSWAP */
2948 { S_MASKS(1,0), S_MASKS(2,0), S_MASKS(4,0) },
2949 /* Normal (big-endian byte) masks - PNG format */
2950 { S_MASKS(1,1), S_MASKS(2,1), S_MASKS(4,1) }
2951 };
John Bowlerac8375d2011-10-06 22:27:16 -05002952
John Bowler4e68aa72011-10-11 16:01:33 -05002953 /* display_mask has only three entries for the odd passes, so index by
2954 * pass>>1.
2955 */
2956 static PNG_CONST png_uint_32 display_mask[2][3][3] = {
2957 /* Little-endian byte masks for PACKSWAP */
2958 { B_MASKS(1,0), B_MASKS(2,0), B_MASKS(4,0) },
2959 /* Normal (big-endian byte) masks - PNG format */
2960 { B_MASKS(1,1), B_MASKS(2,1), B_MASKS(4,1) }
2961 };
John Bowlerac8375d2011-10-06 22:27:16 -05002962
John Bowler4e68aa72011-10-11 16:01:33 -05002963# define MASK(pass,depth,display,png)\
2964 ((display)?display_mask[png][DEPTH_INDEX(depth)][pass>>1]:\
2965 row_mask[png][DEPTH_INDEX(depth)][pass])
John Bowlerac8375d2011-10-06 22:27:16 -05002966
John Bowler4e68aa72011-10-11 16:01:33 -05002967#else /* !PNG_USE_COMPILE_TIME_MASKS */
2968 /* This is the runtime alternative: it seems unlikely that this will
2969 * ever be either smaller or faster than the compile time approach.
2970 */
2971# define MASK(pass,depth,display,png)\
2972 ((display)?B_MASK(pass,depth,png):S_MASK(pass,depth,png))
2973#endif /* !PNG_USE_COMPILE_TIME_MASKS */
John Bowlerac8375d2011-10-06 22:27:16 -05002974
John Bowler4e68aa72011-10-11 16:01:33 -05002975 /* Use the appropriate mask to copy the required bits. In some cases
2976 * the byte mask will be 0 or 0xff, optimize these cases. row_width is
2977 * the number of pixels, but the code copies bytes, so it is necessary
2978 * to special case the end.
2979 */
2980 png_uint_32 pixels_per_byte = 8 / pixel_depth;
2981 png_uint_32 mask;
John Bowlerac8375d2011-10-06 22:27:16 -05002982
John Bowler4e68aa72011-10-11 16:01:33 -05002983# ifdef PNG_READ_PACKSWAP_SUPPORTED
2984 if (png_ptr->transformations & PNG_PACKSWAP)
2985 mask = MASK(pass, pixel_depth, display, 0);
John Bowlerac8375d2011-10-06 22:27:16 -05002986
2987 else
John Bowler4e68aa72011-10-11 16:01:33 -05002988# endif
2989 mask = MASK(pass, pixel_depth, display, 1);
2990
2991 for (;;)
2992 {
2993 png_uint_32 m;
2994
2995 /* It doesn't matter in the following if png_uint_32 has more than
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05002996 * 32 bits because the high bits always match those in m<<24; it is,
John Bowler4e68aa72011-10-11 16:01:33 -05002997 * however, essential to use OR here, not +, because of this.
2998 */
2999 m = mask;
3000 mask = (m >> 8) | (m << 24); /* rotate right to good compilers */
3001 m &= 0xff;
3002
3003 if (m != 0) /* something to copy */
John Bowlerac8375d2011-10-06 22:27:16 -05003004 {
John Bowler4e68aa72011-10-11 16:01:33 -05003005 if (m != 0xff)
3006 *dp = (png_byte)((*dp & ~m) | (*sp & m));
3007 else
3008 *dp = *sp;
John Bowlerac8375d2011-10-06 22:27:16 -05003009 }
John Bowler4e68aa72011-10-11 16:01:33 -05003010
3011 /* NOTE: this may overwrite the last byte with garbage if the image
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003012 * is not an exact number of bytes wide; libpng has always done
John Bowler4e68aa72011-10-11 16:01:33 -05003013 * this.
3014 */
3015 if (row_width <= pixels_per_byte)
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003016 break; /* May need to restore part of the last byte */
John Bowler4e68aa72011-10-11 16:01:33 -05003017
3018 row_width -= pixels_per_byte;
3019 ++dp;
3020 ++sp;
3021 }
3022 }
3023
3024 else /* pixel_depth >= 8 */
3025 {
3026 unsigned int bytes_to_copy, bytes_to_jump;
3027
3028 /* Validate the depth - it must be a multiple of 8 */
3029 if (pixel_depth & 7)
3030 png_error(png_ptr, "invalid user transform pixel depth");
3031
3032 pixel_depth >>= 3; /* now in bytes */
3033 row_width *= pixel_depth;
3034
3035 /* Regardless of pass number the Adam 7 interlace always results in a
3036 * fixed number of pixels to copy then to skip. There may be a
3037 * different number of pixels to skip at the start though.
3038 */
3039 {
3040 unsigned int offset = PNG_PASS_START_COL(pass) * pixel_depth;
3041
3042 row_width -= offset;
3043 dp += offset;
3044 sp += offset;
John Bowlerac8375d2011-10-06 22:27:16 -05003045 }
3046
John Bowler4e68aa72011-10-11 16:01:33 -05003047 /* Work out the bytes to copy. */
3048 if (display)
3049 {
3050 /* When doing the 'block' algorithm the pixel in the pass gets
3051 * replicated to adjacent pixels. This is why the even (0,2,4,6)
3052 * passes are skipped above - the entire expanded row is copied.
3053 */
3054 bytes_to_copy = (1<<((6-pass)>>1)) * pixel_depth;
3055
3056 /* But don't allow this number to exceed the actual row width. */
3057 if (bytes_to_copy > row_width)
3058 bytes_to_copy = row_width;
3059 }
3060
3061 else /* normal row; Adam7 only ever gives us one pixel to copy. */
3062 bytes_to_copy = pixel_depth;
3063
3064 /* In Adam7 there is a constant offset between where the pixels go. */
3065 bytes_to_jump = PNG_PASS_COL_OFFSET(pass) * pixel_depth;
3066
3067 /* And simply copy these bytes. Some optimization is possible here,
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003068 * depending on the value of 'bytes_to_copy'. Special case the low
John Bowler4e68aa72011-10-11 16:01:33 -05003069 * byte counts, which we know to be frequent.
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003070 *
3071 * Notice that these cases all 'return' rather than 'break' - this
3072 * avoids an unnecessary test on whether to restore the last byte
3073 * below.
John Bowler4e68aa72011-10-11 16:01:33 -05003074 */
3075 switch (bytes_to_copy)
3076 {
3077 case 1:
3078 for (;;)
3079 {
3080 *dp = *sp;
3081
3082 if (row_width <= bytes_to_jump)
3083 return;
3084
3085 dp += bytes_to_jump;
3086 sp += bytes_to_jump;
3087 row_width -= bytes_to_jump;
3088 }
3089
3090 case 2:
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003091 /* There is a possibility of a partial copy at the end here; this
John Bowler4e68aa72011-10-11 16:01:33 -05003092 * slows the code down somewhat.
3093 */
3094 do
3095 {
3096 dp[0] = sp[0], dp[1] = sp[1];
3097
3098 if (row_width <= bytes_to_jump)
3099 return;
3100
3101 sp += bytes_to_jump;
3102 dp += bytes_to_jump;
3103 row_width -= bytes_to_jump;
3104 }
3105 while (row_width > 1);
3106
3107 /* And there can only be one byte left at this point: */
3108 *dp = *sp;
3109 return;
3110
3111 case 3:
3112 /* This can only be the RGB case, so each copy is exactly one
3113 * pixel and it is not necessary to check for a partial copy.
3114 */
3115 for(;;)
3116 {
3117 dp[0] = sp[0], dp[1] = sp[1], dp[2] = sp[2];
3118
3119 if (row_width <= bytes_to_jump)
3120 return;
3121
3122 sp += bytes_to_jump;
3123 dp += bytes_to_jump;
3124 row_width -= bytes_to_jump;
3125 }
3126
3127 default:
3128#if PNG_ALIGN_TYPE != PNG_ALIGN_NONE
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003129 /* Check for double byte alignment and, if possible, use a
3130 * 16-bit copy. Don't attempt this for narrow images - ones that
John Bowler4e68aa72011-10-11 16:01:33 -05003131 * are less than an interlace panel wide. Don't attempt it for
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003132 * wide bytes-to-copy either - use the png_memcpy there.
John Bowler4e68aa72011-10-11 16:01:33 -05003133 */
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003134 if (bytes_to_copy < 16 /*else use png_memcpy*/ &&
John Bowler4e68aa72011-10-11 16:01:33 -05003135 png_isaligned(dp, png_uint_16) &&
3136 png_isaligned(sp, png_uint_16) &&
3137 bytes_to_copy % sizeof (png_uint_16) == 0 &&
3138 bytes_to_jump % sizeof (png_uint_16) == 0)
3139 {
3140 /* Everything is aligned for png_uint_16 copies, but try for
3141 * png_uint_32 first.
3142 */
3143 if (png_isaligned(dp, png_uint_32) &&
3144 png_isaligned(sp, png_uint_32) &&
3145 bytes_to_copy % sizeof (png_uint_32) == 0 &&
3146 bytes_to_jump % sizeof (png_uint_32) == 0)
3147 {
3148 png_uint_32p dp32 = (png_uint_32p)dp;
3149 png_const_uint_32p sp32 = (png_const_uint_32p)sp;
3150 unsigned int skip = (bytes_to_jump-bytes_to_copy) /
3151 sizeof (png_uint_32);
3152
3153 do
3154 {
3155 size_t c = bytes_to_copy;
3156 do
3157 {
3158 *dp32++ = *sp32++;
3159 c -= sizeof (png_uint_32);
3160 }
3161 while (c > 0);
3162
3163 if (row_width <= bytes_to_jump)
3164 return;
3165
3166 dp32 += skip;
3167 sp32 += skip;
3168 row_width -= bytes_to_jump;
3169 }
3170 while (bytes_to_copy <= row_width);
3171
3172 /* Get to here when the row_width truncates the final copy.
3173 * There will be 1-3 bytes left to copy, so don't try the
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003174 * 16-bit loop below.
John Bowler4e68aa72011-10-11 16:01:33 -05003175 */
3176 dp = (png_bytep)dp32;
3177 sp = (png_const_bytep)sp32;
3178 do
3179 *dp++ = *sp++;
3180 while (--row_width > 0);
3181 return;
3182 }
3183
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003184 /* Else do it in 16-bit quantities, but only if the size is
John Bowler4e68aa72011-10-11 16:01:33 -05003185 * not too large.
3186 */
3187 else
3188 {
3189 png_uint_16p dp16 = (png_uint_16p)dp;
3190 png_const_uint_16p sp16 = (png_const_uint_16p)sp;
3191 unsigned int skip = (bytes_to_jump-bytes_to_copy) /
3192 sizeof (png_uint_16);
3193
3194 do
3195 {
3196 size_t c = bytes_to_copy;
3197 do
3198 {
3199 *dp16++ = *sp16++;
3200 c -= sizeof (png_uint_16);
3201 }
3202 while (c > 0);
3203
3204 if (row_width <= bytes_to_jump)
3205 return;
3206
3207 dp16 += skip;
3208 sp16 += skip;
3209 row_width -= bytes_to_jump;
3210 }
3211 while (bytes_to_copy <= row_width);
3212
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003213 /* End of row - 1 byte left, bytes_to_copy > row_width: */
John Bowler4e68aa72011-10-11 16:01:33 -05003214 dp = (png_bytep)dp16;
3215 sp = (png_const_bytep)sp16;
3216 do
3217 *dp++ = *sp++;
3218 while (--row_width > 0);
3219 return;
3220 }
3221 }
3222#endif /* PNG_ALIGN_ code */
3223
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003224 /* The true default - use a png_memcpy: */
John Bowler4e68aa72011-10-11 16:01:33 -05003225 for (;;)
3226 {
3227 png_memcpy(dp, sp, bytes_to_copy);
3228
3229 if (row_width <= bytes_to_jump)
3230 return;
3231
3232 sp += bytes_to_jump;
3233 dp += bytes_to_jump;
3234 row_width -= bytes_to_jump;
3235 if (bytes_to_copy > row_width)
3236 bytes_to_copy = row_width;
3237 }
3238 }
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003239
3240 /* NOT REACHED*/
John Bowler4e68aa72011-10-11 16:01:33 -05003241 } /* pixel_depth >= 8 */
3242
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003243 /* Here if pixel_depth < 8 to check 'end_ptr' below. */
Guy Schalnat0d580581995-07-20 02:43:20 -05003244 }
John Bowler4e68aa72011-10-11 16:01:33 -05003245 else
John Bowlerac8375d2011-10-06 22:27:16 -05003246#endif
3247
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003248 /* If here then the switch above wasn't used so just png_memcpy the whole row
John Bowler4e68aa72011-10-11 16:01:33 -05003249 * from the temporary row buffer (notice that this overwrites the end of the
3250 * destination row if it is a partial byte.)
John Bowlerac8375d2011-10-06 22:27:16 -05003251 */
3252 png_memcpy(dp, sp, PNG_ROWBYTES(pixel_depth, row_width));
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003253
3254 /* Restore the overwritten bits from the last byte if necessary. */
3255 if (end_ptr != NULL)
3256 *end_ptr = (png_byte)((end_byte & end_mask) | (*end_ptr & ~end_mask));
Guy Schalnat0d580581995-07-20 02:43:20 -05003257}
3258
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06003259#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05003260void /* PRIVATE */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003261png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
3262 png_uint_32 transformations /* Because these may affect the byte layout */)
Guy Schalnat0d580581995-07-20 02:43:20 -05003263{
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003264 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
3265 /* Offset to next interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003266 static PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003267
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05003268 png_debug(1, "in png_do_read_interlace");
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003269 if (row != NULL && row_info != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -05003270 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003271 png_uint_32 final_width;
3272
3273 final_width = row_info->width * png_pass_inc[pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05003274
3275 switch (row_info->pixel_depth)
3276 {
3277 case 1:
3278 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003279 png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3);
3280 png_bytep dp = row + (png_size_t)((final_width - 1) >> 3);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003281 int sshift, dshift;
3282 int s_start, s_end, s_inc;
3283 int jstop = png_pass_inc[pass];
3284 png_byte v;
Guy Schalnat0d580581995-07-20 02:43:20 -05003285 png_uint_32 i;
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003286 int j;
Guy Schalnat0d580581995-07-20 02:43:20 -05003287
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003288#ifdef PNG_READ_PACKSWAP_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003289 if (transformations & PNG_PACKSWAP)
3290 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003291 sshift = (int)((row_info->width + 7) & 0x07);
3292 dshift = (int)((final_width + 7) & 0x07);
3293 s_start = 7;
3294 s_end = 0;
3295 s_inc = -1;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003296 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003297
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003298 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05003299#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003300 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003301 sshift = 7 - (int)((row_info->width + 7) & 0x07);
3302 dshift = 7 - (int)((final_width + 7) & 0x07);
3303 s_start = 0;
3304 s_end = 7;
3305 s_inc = 1;
3306 }
Glenn Randers-Pehrson5dd2b8e2004-11-24 07:50:16 -06003307
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003308 for (i = 0; i < row_info->width; i++)
3309 {
3310 v = (png_byte)((*sp >> sshift) & 0x01);
3311 for (j = 0; j < jstop; j++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003312 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003313 *dp &= (png_byte)((0x7f7f >> (7 - dshift)) & 0xff);
3314 *dp |= (png_byte)(v << dshift);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003315
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003316 if (dshift == s_end)
3317 {
3318 dshift = s_start;
3319 dp--;
3320 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003321
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003322 else
3323 dshift += s_inc;
3324 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003325
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003326 if (sshift == s_end)
3327 {
3328 sshift = s_start;
Guy Schalnat0d580581995-07-20 02:43:20 -05003329 sp--;
3330 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003331
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003332 else
3333 sshift += s_inc;
Guy Schalnat0d580581995-07-20 02:43:20 -05003334 }
3335 break;
3336 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003337
Guy Schalnat0d580581995-07-20 02:43:20 -05003338 case 2:
3339 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003340 png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);
3341 png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);
3342 int sshift, dshift;
3343 int s_start, s_end, s_inc;
3344 int jstop = png_pass_inc[pass];
Andreas Dilger47a0c421997-05-16 02:46:07 -05003345 png_uint_32 i;
Guy Schalnat0d580581995-07-20 02:43:20 -05003346
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003347#ifdef PNG_READ_PACKSWAP_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003348 if (transformations & PNG_PACKSWAP)
3349 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003350 sshift = (int)(((row_info->width + 3) & 0x03) << 1);
3351 dshift = (int)(((final_width + 3) & 0x03) << 1);
3352 s_start = 6;
3353 s_end = 0;
3354 s_inc = -2;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003355 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003356
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003357 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05003358#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003359 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003360 sshift = (int)((3 - ((row_info->width + 3) & 0x03)) << 1);
3361 dshift = (int)((3 - ((final_width + 3) & 0x03)) << 1);
3362 s_start = 0;
3363 s_end = 6;
3364 s_inc = 2;
3365 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05003366
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003367 for (i = 0; i < row_info->width; i++)
3368 {
3369 png_byte v;
3370 int j;
3371
3372 v = (png_byte)((*sp >> sshift) & 0x03);
3373 for (j = 0; j < jstop; j++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003374 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003375 *dp &= (png_byte)((0x3f3f >> (6 - dshift)) & 0xff);
3376 *dp |= (png_byte)(v << dshift);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003377
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003378 if (dshift == s_end)
3379 {
3380 dshift = s_start;
3381 dp--;
3382 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003383
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003384 else
3385 dshift += s_inc;
3386 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003387
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003388 if (sshift == s_end)
3389 {
3390 sshift = s_start;
Guy Schalnat0d580581995-07-20 02:43:20 -05003391 sp--;
3392 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003393
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003394 else
3395 sshift += s_inc;
Guy Schalnat0d580581995-07-20 02:43:20 -05003396 }
3397 break;
3398 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003399
Guy Schalnat0d580581995-07-20 02:43:20 -05003400 case 4:
3401 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003402 png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1);
3403 png_bytep dp = row + (png_size_t)((final_width - 1) >> 1);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003404 int sshift, dshift;
3405 int s_start, s_end, s_inc;
Guy Schalnat0d580581995-07-20 02:43:20 -05003406 png_uint_32 i;
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003407 int jstop = png_pass_inc[pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05003408
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003409#ifdef PNG_READ_PACKSWAP_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003410 if (transformations & PNG_PACKSWAP)
3411 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003412 sshift = (int)(((row_info->width + 1) & 0x01) << 2);
3413 dshift = (int)(((final_width + 1) & 0x01) << 2);
3414 s_start = 4;
3415 s_end = 0;
3416 s_inc = -4;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003417 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003418
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003419 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05003420#endif
3421 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003422 sshift = (int)((1 - ((row_info->width + 1) & 0x01)) << 2);
3423 dshift = (int)((1 - ((final_width + 1) & 0x01)) << 2);
3424 s_start = 0;
3425 s_end = 4;
3426 s_inc = 4;
3427 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05003428
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003429 for (i = 0; i < row_info->width; i++)
3430 {
Glenn Randers-Pehrson8db19982011-10-27 16:17:24 -05003431 png_byte v = (png_byte)((*sp >> sshift) & 0x0f);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003432 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003433
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003434 for (j = 0; j < jstop; j++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003435 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003436 *dp &= (png_byte)((0xf0f >> (4 - dshift)) & 0xff);
3437 *dp |= (png_byte)(v << dshift);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003438
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003439 if (dshift == s_end)
3440 {
3441 dshift = s_start;
3442 dp--;
3443 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003444
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003445 else
3446 dshift += s_inc;
3447 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003448
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003449 if (sshift == s_end)
3450 {
3451 sshift = s_start;
Guy Schalnat0d580581995-07-20 02:43:20 -05003452 sp--;
3453 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003454
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003455 else
3456 sshift += s_inc;
Guy Schalnat0d580581995-07-20 02:43:20 -05003457 }
3458 break;
3459 }
John Bowlerac8375d2011-10-06 22:27:16 -05003460
Guy Schalnat0d580581995-07-20 02:43:20 -05003461 default:
3462 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003463 png_size_t pixel_bytes = (row_info->pixel_depth >> 3);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003464
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06003465 png_bytep sp = row + (png_size_t)(row_info->width - 1)
3466 * pixel_bytes;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003467
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003468 png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05003469
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003470 int jstop = png_pass_inc[pass];
3471 png_uint_32 i;
3472
3473 for (i = 0; i < row_info->width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003474 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003475 png_byte v[8];
3476 int j;
3477
3478 png_memcpy(v, sp, pixel_bytes);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003479
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003480 for (j = 0; j < jstop; j++)
3481 {
3482 png_memcpy(dp, v, pixel_bytes);
3483 dp -= pixel_bytes;
3484 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003485
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003486 sp -= pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05003487 }
3488 break;
3489 }
3490 }
John Bowlerac8375d2011-10-06 22:27:16 -05003491
Guy Schalnat0d580581995-07-20 02:43:20 -05003492 row_info->width = final_width;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003493 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width);
Guy Schalnat0d580581995-07-20 02:43:20 -05003494 }
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -05003495#ifndef PNG_READ_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003496 PNG_UNUSED(transformations) /* Silence compiler warning */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05003497#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003498}
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06003499#endif /* PNG_READ_INTERLACING_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05003500
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003501/* 1.5.6: Changed to just take a png_row_info (not png_ptr) and to ignore bad
3502 * adaptive filter bytes.
3503 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05003504void /* PRIVATE */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003505png_read_filter_row(png_row_infop row_info, png_bytep row,
3506 png_const_bytep prev_row, int filter)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003507{
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003508 switch (filter)
3509 {
3510 case PNG_FILTER_VALUE_NONE:
3511 break;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003512
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003513 case PNG_FILTER_VALUE_SUB:
3514 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003515 png_size_t i;
3516 png_size_t istop = row_info->rowbytes;
3517 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003518 png_bytep rp = row + bpp;
3519 png_bytep lp = row;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003520
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003521 for (i = bpp; i < istop; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003522 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05003523 *rp = (png_byte)(((int)(*rp) + (int)(*lp++)) & 0xff);
3524 rp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003525 }
3526 break;
3527 }
3528 case PNG_FILTER_VALUE_UP:
3529 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003530 png_size_t i;
3531 png_size_t istop = row_info->rowbytes;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003532 png_bytep rp = row;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003533 png_const_bytep pp = prev_row;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003534
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003535 for (i = 0; i < istop; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003536 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05003537 *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
3538 rp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003539 }
3540 break;
3541 }
3542 case PNG_FILTER_VALUE_AVG:
3543 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003544 png_size_t i;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003545 png_bytep rp = row;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003546 png_const_bytep pp = prev_row;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003547 png_bytep lp = row;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003548 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
3549 png_size_t istop = row_info->rowbytes - bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003550
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003551 for (i = 0; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003552 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05003553 *rp = (png_byte)(((int)(*rp) +
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003554 ((int)(*pp++) / 2 )) & 0xff);
3555
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05003556 rp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003557 }
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003558
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05003559 for (i = 0; i < istop; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003560 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05003561 *rp = (png_byte)(((int)(*rp) +
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003562 (int)(*pp++ + *lp++) / 2 ) & 0xff);
3563
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05003564 rp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003565 }
3566 break;
3567 }
3568 case PNG_FILTER_VALUE_PAETH:
3569 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003570 png_size_t i;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003571 png_bytep rp = row;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003572 png_const_bytep pp = prev_row;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003573 png_bytep lp = row;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003574 png_const_bytep cp = prev_row;
3575 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
3576 png_size_t istop=row_info->rowbytes - bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003577
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003578 for (i = 0; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003579 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05003580 *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
3581 rp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003582 }
3583
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003584 for (i = 0; i < istop; i++) /* Use leftover rp,pp */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003585 {
3586 int a, b, c, pa, pb, pc, p;
3587
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003588 a = *lp++;
3589 b = *pp++;
3590 c = *cp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003591
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003592 p = b - c;
3593 pc = a - c;
3594
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003595#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003596 pa = abs(p);
3597 pb = abs(pc);
3598 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003599#else
Glenn Randers-Pehrsona565f0e2010-03-06 08:24:45 -06003600 pa = p < 0 ? -p : p;
3601 pb = pc < 0 ? -pc : pc;
3602 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003603#endif
3604
3605 /*
3606 if (pa <= pb && pa <= pc)
3607 p = a;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003608
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003609 else if (pb <= pc)
3610 p = b;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003611
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003612 else
3613 p = c;
3614 */
3615
Glenn Randers-Pehrsona565f0e2010-03-06 08:24:45 -06003616 p = (pa <= pb && pa <= pc) ? a : (pb <= pc) ? b : c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003617
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05003618 *rp = (png_byte)(((int)(*rp) + p) & 0xff);
3619 rp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003620 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003621 break;
3622 }
3623 default:
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003624 /* NOT REACHED */
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05003625 break;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003626 }
3627}
Guy Schalnat0d580581995-07-20 02:43:20 -05003628
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05003629#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05003630void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06003631png_read_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05003632{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003633#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003634 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003635
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003636 /* Start of interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003637 static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003638
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003639 /* Offset to next interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003640 static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003641
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003642 /* Start of interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003643 static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003644
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003645 /* Offset to next interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003646 static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003647#endif /* PNG_READ_INTERLACING_SUPPORTED */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003648
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05003649 png_debug(1, "in png_read_finish_row");
Guy Schalnat0d580581995-07-20 02:43:20 -05003650 png_ptr->row_number++;
3651 if (png_ptr->row_number < png_ptr->num_rows)
3652 return;
3653
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003654#ifdef PNG_READ_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05003655 if (png_ptr->interlaced)
3656 {
3657 png_ptr->row_number = 0;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003658
Glenn Randers-Pehrson435cf872011-10-05 16:23:53 -05003659 /* TO DO: don't do this if prev_row isn't needed (requires
3660 * read-ahead of the next row's filter byte.
3661 */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003662 png_memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
3663
Guy Schalnat0d580581995-07-20 02:43:20 -05003664 do
3665 {
3666 png_ptr->pass++;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003667
Guy Schalnat0d580581995-07-20 02:43:20 -05003668 if (png_ptr->pass >= 7)
3669 break;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003670
Guy Schalnat0d580581995-07-20 02:43:20 -05003671 png_ptr->iwidth = (png_ptr->width +
3672 png_pass_inc[png_ptr->pass] - 1 -
3673 png_pass_start[png_ptr->pass]) /
3674 png_pass_inc[png_ptr->pass];
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05003675
Guy Schalnat0d580581995-07-20 02:43:20 -05003676 if (!(png_ptr->transformations & PNG_INTERLACE))
3677 {
3678 png_ptr->num_rows = (png_ptr->height +
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003679 png_pass_yinc[png_ptr->pass] - 1 -
3680 png_pass_ystart[png_ptr->pass]) /
3681 png_pass_yinc[png_ptr->pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05003682 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003683
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -05003684 else /* if (png_ptr->transformations & PNG_INTERLACE) */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003685 break; /* libpng deinterlacing sees every row */
3686
3687 } while (png_ptr->num_rows == 0 || png_ptr->iwidth == 0);
Guy Schalnat0d580581995-07-20 02:43:20 -05003688
3689 if (png_ptr->pass < 7)
3690 return;
3691 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003692#endif /* PNG_READ_INTERLACING_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05003693
Guy Schalnate5a37791996-06-05 15:50:50 -05003694 if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
Guy Schalnat0d580581995-07-20 02:43:20 -05003695 {
3696 char extra;
3697 int ret;
3698
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003699 png_ptr->zstream.next_out = (Byte *)&extra;
3700 png_ptr->zstream.avail_out = (uInt)1;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003701
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003702 for (;;)
Guy Schalnat0d580581995-07-20 02:43:20 -05003703 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003704 if (!(png_ptr->zstream.avail_in))
Guy Schalnat0d580581995-07-20 02:43:20 -05003705 {
3706 while (!png_ptr->idat_size)
3707 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003708 png_crc_finish(png_ptr, 0);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003709 png_ptr->idat_size = png_read_chunk_header(png_ptr);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003710 if (png_ptr->chunk_name != png_IDAT)
Guy Schalnat6d764711995-12-19 03:22:19 -06003711 png_error(png_ptr, "Not enough image data");
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -06003712 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003713
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003714 png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
3715 png_ptr->zstream.next_in = png_ptr->zbuf;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003716
Guy Schalnat0d580581995-07-20 02:43:20 -05003717 if (png_ptr->zbuf_size > png_ptr->idat_size)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003718 png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003719
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003720 png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zstream.avail_in);
3721 png_ptr->idat_size -= png_ptr->zstream.avail_in;
Guy Schalnat0d580581995-07-20 02:43:20 -05003722 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003723
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003724 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003725
Guy Schalnat0d580581995-07-20 02:43:20 -05003726 if (ret == Z_STREAM_END)
3727 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003728 if (!(png_ptr->zstream.avail_out) || png_ptr->zstream.avail_in ||
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003729 png_ptr->idat_size)
Glenn Randers-Pehrson859665d2002-08-06 18:06:11 -05003730 png_warning(png_ptr, "Extra compressed data");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003731
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003732 png_ptr->mode |= PNG_AFTER_IDAT;
3733 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
Guy Schalnat0d580581995-07-20 02:43:20 -05003734 break;
3735 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003736
Guy Schalnat0d580581995-07-20 02:43:20 -05003737 if (ret != Z_OK)
Glenn Randers-Pehrsona565f0e2010-03-06 08:24:45 -06003738 png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003739 "Decompression Error");
Guy Schalnat0d580581995-07-20 02:43:20 -05003740
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003741 if (!(png_ptr->zstream.avail_out))
Glenn Randers-Pehrson859665d2002-08-06 18:06:11 -05003742 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05003743 png_warning(png_ptr, "Extra compressed data");
Glenn Randers-Pehrson859665d2002-08-06 18:06:11 -05003744 png_ptr->mode |= PNG_AFTER_IDAT;
3745 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
3746 break;
3747 }
Guy Schalnat0d580581995-07-20 02:43:20 -05003748
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06003749 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003750 png_ptr->zstream.avail_out = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -05003751 }
3752
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003753 if (png_ptr->idat_size || png_ptr->zstream.avail_in)
Glenn Randers-Pehrson859665d2002-08-06 18:06:11 -05003754 png_warning(png_ptr, "Extra compression data");
Guy Schalnat0d580581995-07-20 02:43:20 -05003755
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003756 inflateReset(&png_ptr->zstream);
Guy Schalnat0d580581995-07-20 02:43:20 -05003757
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003758 png_ptr->mode |= PNG_AFTER_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -05003759}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05003760#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05003761
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05003762void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06003763png_read_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05003764{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003765#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003766 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003767
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003768 /* Start of interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003769 static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003770
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003771 /* Offset to next interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003772 static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003773
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003774 /* Start of interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003775 static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003776
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003777 /* Offset to next interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003778 static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003779#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003780
Guy Schalnat0d580581995-07-20 02:43:20 -05003781 int max_pixel_depth;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05003782 png_size_t row_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05003783
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05003784 png_debug(1, "in png_read_start_row");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003785 png_ptr->zstream.avail_in = 0;
John Bowler4a12f4a2011-04-17 18:34:22 -05003786#ifdef PNG_READ_TRANSFORMS_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05003787 png_init_read_transformations(png_ptr);
John Bowler4a12f4a2011-04-17 18:34:22 -05003788#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003789#ifdef PNG_READ_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05003790 if (png_ptr->interlaced)
3791 {
3792 if (!(png_ptr->transformations & PNG_INTERLACE))
3793 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003794 png_pass_ystart[0]) / png_pass_yinc[0];
3795
Guy Schalnat0d580581995-07-20 02:43:20 -05003796 else
3797 png_ptr->num_rows = png_ptr->height;
3798
3799 png_ptr->iwidth = (png_ptr->width +
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003800 png_pass_inc[png_ptr->pass] - 1 -
3801 png_pass_start[png_ptr->pass]) /
3802 png_pass_inc[png_ptr->pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05003803 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003804
Guy Schalnat0d580581995-07-20 02:43:20 -05003805 else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003806#endif /* PNG_READ_INTERLACING_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05003807 {
3808 png_ptr->num_rows = png_ptr->height;
3809 png_ptr->iwidth = png_ptr->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05003810 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003811
Guy Schalnat0d580581995-07-20 02:43:20 -05003812 max_pixel_depth = png_ptr->pixel_depth;
3813
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003814#ifdef PNG_READ_PACK_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05003815 if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8)
Guy Schalnat0d580581995-07-20 02:43:20 -05003816 max_pixel_depth = 8;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05003817#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003818
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003819#ifdef PNG_READ_EXPAND_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -05003820 if (png_ptr->transformations & PNG_EXPAND)
Guy Schalnat0d580581995-07-20 02:43:20 -05003821 {
3822 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
3823 {
3824 if (png_ptr->num_trans)
3825 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003826
Guy Schalnat0d580581995-07-20 02:43:20 -05003827 else
3828 max_pixel_depth = 24;
3829 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003830
Guy Schalnat0d580581995-07-20 02:43:20 -05003831 else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
3832 {
3833 if (max_pixel_depth < 8)
3834 max_pixel_depth = 8;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003835
Guy Schalnat0d580581995-07-20 02:43:20 -05003836 if (png_ptr->num_trans)
3837 max_pixel_depth *= 2;
3838 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003839
Guy Schalnat0d580581995-07-20 02:43:20 -05003840 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
3841 {
3842 if (png_ptr->num_trans)
3843 {
3844 max_pixel_depth *= 4;
3845 max_pixel_depth /= 3;
3846 }
3847 }
3848 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05003849#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003850
John Bowler4d562962011-02-12 09:01:20 -06003851#ifdef PNG_READ_EXPAND_16_SUPPORTED
3852 if (png_ptr->transformations & PNG_EXPAND_16)
3853 {
3854# ifdef PNG_READ_EXPAND_SUPPORTED
3855 /* In fact it is an error if it isn't supported, but checking is
3856 * the safe way.
3857 */
3858 if (png_ptr->transformations & PNG_EXPAND)
3859 {
3860 if (png_ptr->bit_depth < 16)
3861 max_pixel_depth *= 2;
3862 }
3863 else
3864# endif
3865 png_ptr->transformations &= ~PNG_EXPAND_16;
3866 }
3867#endif
3868
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003869#ifdef PNG_READ_FILLER_SUPPORTED
Guy Schalnat51f0eb41995-09-26 05:22:39 -05003870 if (png_ptr->transformations & (PNG_FILLER))
Guy Schalnat0d580581995-07-20 02:43:20 -05003871 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05003872 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
3873 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003874
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05003875 else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003876 {
3877 if (max_pixel_depth <= 8)
3878 max_pixel_depth = 16;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003879
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003880 else
3881 max_pixel_depth = 32;
3882 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003883
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003884 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
3885 {
3886 if (max_pixel_depth <= 32)
3887 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003888
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003889 else
3890 max_pixel_depth = 64;
3891 }
Guy Schalnat0d580581995-07-20 02:43:20 -05003892 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05003893#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003894
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003895#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05003896 if (png_ptr->transformations & PNG_GRAY_TO_RGB)
3897 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003898 if (
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003899#ifdef PNG_READ_EXPAND_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003900 (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) ||
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003901#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003902#ifdef PNG_READ_FILLER_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003903 (png_ptr->transformations & (PNG_FILLER)) ||
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003904#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003905 png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
Guy Schalnat0d580581995-07-20 02:43:20 -05003906 {
3907 if (max_pixel_depth <= 16)
3908 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003909
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003910 else
Guy Schalnat0d580581995-07-20 02:43:20 -05003911 max_pixel_depth = 64;
3912 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003913
Guy Schalnat0d580581995-07-20 02:43:20 -05003914 else
3915 {
3916 if (max_pixel_depth <= 8)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003917 {
3918 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06003919 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003920
3921 else
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06003922 max_pixel_depth = 24;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003923 }
3924
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06003925 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
3926 max_pixel_depth = 64;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003927
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003928 else
Guy Schalnat0d580581995-07-20 02:43:20 -05003929 max_pixel_depth = 48;
3930 }
3931 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05003932#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003933
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05003934#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
3935defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003936 if (png_ptr->transformations & PNG_USER_TRANSFORM)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003937 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003938 int user_pixel_depth = png_ptr->user_transform_depth *
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05003939 png_ptr->user_transform_channels;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003940
3941 if (user_pixel_depth > max_pixel_depth)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003942 max_pixel_depth = user_pixel_depth;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003943 }
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05003944#endif
3945
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003946 /* This value is stored in png_struct and double checked in the row read
3947 * code.
3948 */
3949 png_ptr->maximum_pixel_depth = (png_byte)max_pixel_depth;
3950 png_ptr->transformed_pixel_depth = 0; /* calculated on demand */
3951
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003952 /* Align the width on the next larger 8 pixels. Mainly used
3953 * for interlacing
3954 */
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05003955 row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003956 /* Calculate the maximum bytes needed, adding a byte and a pixel
3957 * for safety's sake
3958 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003959 row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) +
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003960 1 + ((max_pixel_depth + 7) >> 3);
3961
Guy Schalnat0d580581995-07-20 02:43:20 -05003962#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05003963 if (row_bytes > (png_uint_32)65536L)
Guy Schalnate5a37791996-06-05 15:50:50 -05003964 png_error(png_ptr, "This image requires a row greater than 64KB");
Guy Schalnat0d580581995-07-20 02:43:20 -05003965#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003966
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06003967 if (row_bytes + 48 > png_ptr->old_big_row_buf_size)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003968 {
3969 png_free(png_ptr, png_ptr->big_row_buf);
Mans Rullgardc9e27d02011-10-17 15:25:03 -05003970 png_free(png_ptr, png_ptr->big_prev_row);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003971
Glenn Randers-Pehrson6917b512009-03-09 15:31:08 -05003972 if (png_ptr->interlaced)
Glenn Randers-Pehrsona515d302010-01-01 10:24:25 -06003973 png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr,
3974 row_bytes + 48);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003975
Glenn Randers-Pehrsona515d302010-01-01 10:24:25 -06003976 else
Mans Rullgardc9e27d02011-10-17 15:25:03 -05003977 png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003978
Mans Rullgardc9e27d02011-10-17 15:25:03 -05003979 png_ptr->big_prev_row = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06003980
3981#ifdef PNG_ALIGNED_MEMORY_SUPPORTED
3982 /* Use 16-byte aligned memory for row_buf with at least 16 bytes
Mans Rullgardc9e27d02011-10-17 15:25:03 -05003983 * of padding before and after row_buf; treat prev_row similarly.
John Bowlerac8375d2011-10-06 22:27:16 -05003984 * NOTE: the alignment is to the start of the pixels, one beyond the start
3985 * of the buffer, because of the filter byte. Prior to libpng 1.5.6 this
Mans Rullgardc9e27d02011-10-17 15:25:03 -05003986 * was incorrect; the filter byte was aligned, which had the exact
3987 * opposite effect of that intended.
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06003988 */
John Bowlerac8375d2011-10-06 22:27:16 -05003989 {
3990 png_bytep temp = png_ptr->big_row_buf + 32;
Glenn Randers-Pehrson8db19982011-10-27 16:17:24 -05003991 int extra = (int)((temp - (png_bytep)0) & 0x0f);
John Bowlerac8375d2011-10-06 22:27:16 -05003992 png_ptr->row_buf = temp - extra - 1/*filter byte*/;
Mans Rullgardc9e27d02011-10-17 15:25:03 -05003993
3994 temp = png_ptr->big_prev_row + 32;
Glenn Randers-Pehrson8db19982011-10-27 16:17:24 -05003995 extra = (int)((temp - (png_bytep)0) & 0x0f);
Mans Rullgardc9e27d02011-10-17 15:25:03 -05003996 png_ptr->prev_row = temp - extra - 1/*filter byte*/;
John Bowlerac8375d2011-10-06 22:27:16 -05003997 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003998
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06003999#else
John Bowlerac8375d2011-10-06 22:27:16 -05004000 /* Use 31 bytes of padding before and 17 bytes after row_buf. */
4001 png_ptr->row_buf = png_ptr->big_row_buf + 31;
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004002 png_ptr->prev_row = png_ptr->big_prev_row + 31;
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06004003#endif
4004 png_ptr->old_big_row_buf_size = row_bytes + 48;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004005 }
Guy Schalnat0d580581995-07-20 02:43:20 -05004006
4007#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004008 if (png_ptr->rowbytes > 65535)
Guy Schalnate5a37791996-06-05 15:50:50 -05004009 png_error(png_ptr, "This image requires a row greater than 64KB");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004010
Guy Schalnat0d580581995-07-20 02:43:20 -05004011#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004012 if (png_ptr->rowbytes > (PNG_SIZE_MAX - 1))
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05004013 png_error(png_ptr, "Row has too many bytes to allocate in memory");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004014
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05004015 png_memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05004016
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004017 png_debug1(3, "width = %u,", png_ptr->width);
4018 png_debug1(3, "height = %u,", png_ptr->height);
4019 png_debug1(3, "iwidth = %u,", png_ptr->iwidth);
4020 png_debug1(3, "num_rows = %u,", png_ptr->num_rows);
Glenn Randers-Pehrsonb764c602011-01-14 21:18:37 -06004021 png_debug1(3, "rowbytes = %lu,", (unsigned long)png_ptr->rowbytes);
4022 png_debug1(3, "irowbytes = %lu",
4023 (unsigned long)PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05004024
Guy Schalnate5a37791996-06-05 15:50:50 -05004025 png_ptr->flags |= PNG_FLAG_ROW_INIT;
Guy Schalnat0d580581995-07-20 02:43:20 -05004026}
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06004027#endif /* PNG_READ_SUPPORTED */