blob: e5feb18da43061af29c1a69ef03d1d07c8f02011 [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-Pehrson5c92dec2011-01-07 18:28:47 -0600304 /* The setting of 'avail_in' used to be outside the loop, by setting it
305 * 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 */
944 png_warning(png_ptr, "Missing PLTE before cHRM");
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;
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002789
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002790 png_debug(1, "in png_combine_row");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002791
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002792 /* Added in 1.5.6: it should not be possible to enter this routine until at
2793 * least one row has been read from the PNG data and transformed.
2794 */
2795 if (pixel_depth == 0)
2796 png_error(png_ptr, "internal row logic error");
2797
2798 /* Added in 1.5.4: the pixel depth should match the information returned by
2799 * any call to png_read_update_info at this point. Do not continue if we got
John Bowler9994f252011-05-15 18:52:39 -05002800 * this wrong.
2801 */
2802 if (png_ptr->info_rowbytes != 0 && png_ptr->info_rowbytes !=
John Bowlerac8375d2011-10-06 22:27:16 -05002803 PNG_ROWBYTES(pixel_depth, row_width))
John Bowler9994f252011-05-15 18:52:39 -05002804 png_error(png_ptr, "internal row size calculation error");
2805
John Bowlerac8375d2011-10-06 22:27:16 -05002806 /* Don't expect this to ever happen: */
2807 if (row_width == 0)
2808 png_error(png_ptr, "internal row width error");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002809
John Bowlerac8375d2011-10-06 22:27:16 -05002810 /* This reduces to a memcpy for non-interlaced images and for the case where
2811 * interlacing isn't supported or isn't done (in that case the caller gets a
2812 * sequence of interlace pass rows.)
2813 */
2814#ifdef PNG_READ_INTERLACING_SUPPORTED
2815 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE) &&
John Bowler4e68aa72011-10-11 16:01:33 -05002816 pass < 6 && (display == 0 ||
2817 /* The following copies everything for 'display' on passes 0, 2 and 4. */
2818 (display == 1 && (pass & 1) != 0)))
Guy Schalnat0d580581995-07-20 02:43:20 -05002819 {
John Bowler4e68aa72011-10-11 16:01:33 -05002820 /* Narrow images may have no bits in a pass; the caller should handle
2821 * this, but this test is cheap:
John Bowlerac8375d2011-10-06 22:27:16 -05002822 */
John Bowler4e68aa72011-10-11 16:01:33 -05002823 if (row_width <= PNG_PASS_START_COL(pass))
2824 return;
John Bowlerac8375d2011-10-06 22:27:16 -05002825
John Bowler4e68aa72011-10-11 16:01:33 -05002826 if (pixel_depth < 8)
Guy Schalnat0d580581995-07-20 02:43:20 -05002827 {
John Bowler4e68aa72011-10-11 16:01:33 -05002828 /* For pixel depths up to 4bpp the 8-pixel mask can be expanded to fit
2829 * into 32 bits, then a single loop over the bytes using the four byte
2830 * values in the 32 bit mask can be used. For the 'display' option the
2831 * expanded mask may also not require any masking within a byte. To
2832 * make this work the PACKSWAP option must be taken into account - it
2833 * simply requires the pixels to be reversed in each byte.
2834 *
2835 * The 'regular' case requires a mask for each of the first 6 passes,
2836 * the 'display' case does a copy for the even passes in the range
2837 * 0..6. This has already been handled in the tst above.
2838 *
2839 * The masks are arranged as four bytes with the first byte to use in
2840 * the lowest bits (little-endian) regardless of the order (PACKSWAP or
2841 * not) of the pixels in each byte.
2842 *
2843 * NOTE: the whole of this logic depends on the caller of this function
2844 * only calling it on rows appropriate to the pass. This function only
2845 * understands the 'x' logic, the 'y' logic is handled by the caller.
2846 *
2847 * The following defines allow generation of compile time constant bit
2848 * masks for each pixel depth and each possibility of swapped or not
2849 * swapped bytes. Pass is in the range 0..6, 'x', a pixel index, is in
2850 * the range 0..7, the result is 1 if the pixel is to be copied in the
2851 * pass, 0 if not. 'S' is for the sparkle method, 'B' for the block
2852 * method.
2853 */
2854# define S_COPY(p,x) (((p)<4 ? 0x80088822 >> ((3-(p))*8+(7-(x))) :\
2855 0xaa55ff00 >> ((7-(p))*8+(7-(x)))) & 1)
2856# define B_COPY(p,x) (((p)<4 ? 0xff0fff33 >> ((3-(p))*8+(7-(x))) :\
2857 0xff55ff00 >> ((7-(p))*8+(7-(x)))) & 1)
Guy Schalnat0d580581995-07-20 02:43:20 -05002858
John Bowler4e68aa72011-10-11 16:01:33 -05002859 /* Return a mask for pass 'p' pixel 'x' at depth 'd'. The mask is
2860 * little endian - the first pixel is at bit 0 - however the extra
2861 * parameter 's' can be set to cause the mask position to be swapped
2862 * within each byte, to match the PNG format. This is done by XOR of
2863 * the shift with 7, 6 or 4 for bit depths 1, 2 and 4.
2864 */
2865# define PIXEL_MASK(p,x,d,s) (((1U<<(d))-1)<<(((x)*(d))^((s)?8-(d):0)))
John Bowlerac8375d2011-10-06 22:27:16 -05002866
John Bowler4e68aa72011-10-11 16:01:33 -05002867 /* Hence generate the appropriate 'block' or 'sparkle' pixel copy mask.
2868 */
2869# define S_MASKx(p,x,d,s) (S_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
2870# 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 -05002871
John Bowler4e68aa72011-10-11 16:01:33 -05002872 /* Combine 8 of these to get the full mask. For the 1 and 2 bpp cases
2873 * the result needs replicating, for the 4bpp case the above generates
2874 * a full 32 bits.
2875 */
2876# define MASK_EXPAND(m,d) ((m)*((d)==1?0x01010101:((d)==2?0x00010001:1)))
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06002877
John Bowler4e68aa72011-10-11 16:01:33 -05002878# define S_MASK(p,d,s) MASK_EXPAND(S_MASKx(p,0,d,s) + S_MASKx(p,1,d,s) +\
2879 S_MASKx(p,2,d,s) + S_MASKx(p,3,d,s) + S_MASKx(p,4,d,s) +\
2880 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 -06002881
John Bowler4e68aa72011-10-11 16:01:33 -05002882# define B_MASK(p,d,s) MASK_EXPAND(B_MASKx(p,0,d,s) + B_MASKx(p,1,d,s) +\
2883 B_MASKx(p,2,d,s) + B_MASKx(p,3,d,s) + B_MASKx(p,4,d,s) +\
2884 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 -05002885
John Bowler4e68aa72011-10-11 16:01:33 -05002886#if PNG_USE_COMPILE_TIME_MASKS
2887 /* Utility macros to construct all the masks for a depth/swap
2888 * combination. The 's' parameter says whether the format is PNG
2889 * (big endian bytes) or not. Only the three odd numbered passes are
2890 * required for the display/block algorithm.
2891 */
2892# define S_MASKS(d,s) { S_MASK(0,d,s), S_MASK(1,d,s), S_MASK(2,d,s),\
2893 S_MASK(3,d,s), S_MASK(4,d,s), S_MASK(5,d,s) }
John Bowlerac8375d2011-10-06 22:27:16 -05002894
John Bowler4e68aa72011-10-11 16:01:33 -05002895# 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 -05002896
John Bowler4e68aa72011-10-11 16:01:33 -05002897# define DEPTH_INDEX(d) ((d)==1?0:((d)==2?1:2))
John Bowlerac8375d2011-10-06 22:27:16 -05002898
John Bowler4e68aa72011-10-11 16:01:33 -05002899 /* Hence the pre-compiled masks indexed by PACKSWAP (or not), depth and
2900 * then pass:
2901 */
2902 static PNG_CONST png_uint_32 row_mask[2/*PACKSWAP*/][3/*depth*/][6] = {
2903 /* Little-endian byte masks for PACKSWAP */
2904 { S_MASKS(1,0), S_MASKS(2,0), S_MASKS(4,0) },
2905 /* Normal (big-endian byte) masks - PNG format */
2906 { S_MASKS(1,1), S_MASKS(2,1), S_MASKS(4,1) }
2907 };
John Bowlerac8375d2011-10-06 22:27:16 -05002908
John Bowler4e68aa72011-10-11 16:01:33 -05002909 /* display_mask has only three entries for the odd passes, so index by
2910 * pass>>1.
2911 */
2912 static PNG_CONST png_uint_32 display_mask[2][3][3] = {
2913 /* Little-endian byte masks for PACKSWAP */
2914 { B_MASKS(1,0), B_MASKS(2,0), B_MASKS(4,0) },
2915 /* Normal (big-endian byte) masks - PNG format */
2916 { B_MASKS(1,1), B_MASKS(2,1), B_MASKS(4,1) }
2917 };
John Bowlerac8375d2011-10-06 22:27:16 -05002918
John Bowler4e68aa72011-10-11 16:01:33 -05002919# define MASK(pass,depth,display,png)\
2920 ((display)?display_mask[png][DEPTH_INDEX(depth)][pass>>1]:\
2921 row_mask[png][DEPTH_INDEX(depth)][pass])
John Bowlerac8375d2011-10-06 22:27:16 -05002922
John Bowler4e68aa72011-10-11 16:01:33 -05002923#else /* !PNG_USE_COMPILE_TIME_MASKS */
2924 /* This is the runtime alternative: it seems unlikely that this will
2925 * ever be either smaller or faster than the compile time approach.
2926 */
2927# define MASK(pass,depth,display,png)\
2928 ((display)?B_MASK(pass,depth,png):S_MASK(pass,depth,png))
2929#endif /* !PNG_USE_COMPILE_TIME_MASKS */
John Bowlerac8375d2011-10-06 22:27:16 -05002930
John Bowler4e68aa72011-10-11 16:01:33 -05002931 /* Use the appropriate mask to copy the required bits. In some cases
2932 * the byte mask will be 0 or 0xff, optimize these cases. row_width is
2933 * the number of pixels, but the code copies bytes, so it is necessary
2934 * to special case the end.
2935 */
2936 png_uint_32 pixels_per_byte = 8 / pixel_depth;
2937 png_uint_32 mask;
John Bowlerac8375d2011-10-06 22:27:16 -05002938
John Bowler4e68aa72011-10-11 16:01:33 -05002939# ifdef PNG_READ_PACKSWAP_SUPPORTED
2940 if (png_ptr->transformations & PNG_PACKSWAP)
2941 mask = MASK(pass, pixel_depth, display, 0);
John Bowlerac8375d2011-10-06 22:27:16 -05002942
2943 else
John Bowler4e68aa72011-10-11 16:01:33 -05002944# endif
2945 mask = MASK(pass, pixel_depth, display, 1);
2946
2947 for (;;)
2948 {
2949 png_uint_32 m;
2950
2951 /* It doesn't matter in the following if png_uint_32 has more than
2952 * 32 bits because the high bits always match those in m<<24, it is,
2953 * however, essential to use OR here, not +, because of this.
2954 */
2955 m = mask;
2956 mask = (m >> 8) | (m << 24); /* rotate right to good compilers */
2957 m &= 0xff;
2958
2959 if (m != 0) /* something to copy */
John Bowlerac8375d2011-10-06 22:27:16 -05002960 {
John Bowler4e68aa72011-10-11 16:01:33 -05002961 if (m != 0xff)
2962 *dp = (png_byte)((*dp & ~m) | (*sp & m));
2963 else
2964 *dp = *sp;
John Bowlerac8375d2011-10-06 22:27:16 -05002965 }
John Bowler4e68aa72011-10-11 16:01:33 -05002966
2967 /* NOTE: this may overwrite the last byte with garbage if the image
2968 * is not an exact number of bytes wide, libpng has always done
2969 * this.
2970 */
2971 if (row_width <= pixels_per_byte)
2972 return;
2973
2974 row_width -= pixels_per_byte;
2975 ++dp;
2976 ++sp;
2977 }
2978 }
2979
2980 else /* pixel_depth >= 8 */
2981 {
2982 unsigned int bytes_to_copy, bytes_to_jump;
2983
2984 /* Validate the depth - it must be a multiple of 8 */
2985 if (pixel_depth & 7)
2986 png_error(png_ptr, "invalid user transform pixel depth");
2987
2988 pixel_depth >>= 3; /* now in bytes */
2989 row_width *= pixel_depth;
2990
2991 /* Regardless of pass number the Adam 7 interlace always results in a
2992 * fixed number of pixels to copy then to skip. There may be a
2993 * different number of pixels to skip at the start though.
2994 */
2995 {
2996 unsigned int offset = PNG_PASS_START_COL(pass) * pixel_depth;
2997
2998 row_width -= offset;
2999 dp += offset;
3000 sp += offset;
John Bowlerac8375d2011-10-06 22:27:16 -05003001 }
3002
John Bowler4e68aa72011-10-11 16:01:33 -05003003 /* Work out the bytes to copy. */
3004 if (display)
3005 {
3006 /* When doing the 'block' algorithm the pixel in the pass gets
3007 * replicated to adjacent pixels. This is why the even (0,2,4,6)
3008 * passes are skipped above - the entire expanded row is copied.
3009 */
3010 bytes_to_copy = (1<<((6-pass)>>1)) * pixel_depth;
3011
3012 /* But don't allow this number to exceed the actual row width. */
3013 if (bytes_to_copy > row_width)
3014 bytes_to_copy = row_width;
3015 }
3016
3017 else /* normal row; Adam7 only ever gives us one pixel to copy. */
3018 bytes_to_copy = pixel_depth;
3019
3020 /* In Adam7 there is a constant offset between where the pixels go. */
3021 bytes_to_jump = PNG_PASS_COL_OFFSET(pass) * pixel_depth;
3022
3023 /* And simply copy these bytes. Some optimization is possible here,
3024 * depending on the value of 'bytes_to_copy'. Speical case the low
3025 * byte counts, which we know to be frequent.
3026 */
3027 switch (bytes_to_copy)
3028 {
3029 case 1:
3030 for (;;)
3031 {
3032 *dp = *sp;
3033
3034 if (row_width <= bytes_to_jump)
3035 return;
3036
3037 dp += bytes_to_jump;
3038 sp += bytes_to_jump;
3039 row_width -= bytes_to_jump;
3040 }
3041
3042 case 2:
3043 /* There is a possibility of a partial copy at the end here, this
3044 * slows the code down somewhat.
3045 */
3046 do
3047 {
3048 dp[0] = sp[0], dp[1] = sp[1];
3049
3050 if (row_width <= bytes_to_jump)
3051 return;
3052
3053 sp += bytes_to_jump;
3054 dp += bytes_to_jump;
3055 row_width -= bytes_to_jump;
3056 }
3057 while (row_width > 1);
3058
3059 /* And there can only be one byte left at this point: */
3060 *dp = *sp;
3061 return;
3062
3063 case 3:
3064 /* This can only be the RGB case, so each copy is exactly one
3065 * pixel and it is not necessary to check for a partial copy.
3066 */
3067 for(;;)
3068 {
3069 dp[0] = sp[0], dp[1] = sp[1], dp[2] = sp[2];
3070
3071 if (row_width <= bytes_to_jump)
3072 return;
3073
3074 sp += bytes_to_jump;
3075 dp += bytes_to_jump;
3076 row_width -= bytes_to_jump;
3077 }
3078
3079 default:
3080#if PNG_ALIGN_TYPE != PNG_ALIGN_NONE
3081 /* Check for double byte alignment and, if possible, use a 16
3082 * bit copy. Don't attempt this for narrow images - ones that
3083 * are less than an interlace panel wide. Don't attempt it for
3084 * wide bytes-to-copy either - use the memcpy there.
3085 */
3086 if (bytes_to_copy < 16 /*else use memcpy*/ &&
3087 png_isaligned(dp, png_uint_16) &&
3088 png_isaligned(sp, png_uint_16) &&
3089 bytes_to_copy % sizeof (png_uint_16) == 0 &&
3090 bytes_to_jump % sizeof (png_uint_16) == 0)
3091 {
3092 /* Everything is aligned for png_uint_16 copies, but try for
3093 * png_uint_32 first.
3094 */
3095 if (png_isaligned(dp, png_uint_32) &&
3096 png_isaligned(sp, png_uint_32) &&
3097 bytes_to_copy % sizeof (png_uint_32) == 0 &&
3098 bytes_to_jump % sizeof (png_uint_32) == 0)
3099 {
3100 png_uint_32p dp32 = (png_uint_32p)dp;
3101 png_const_uint_32p sp32 = (png_const_uint_32p)sp;
3102 unsigned int skip = (bytes_to_jump-bytes_to_copy) /
3103 sizeof (png_uint_32);
3104
3105 do
3106 {
3107 size_t c = bytes_to_copy;
3108 do
3109 {
3110 *dp32++ = *sp32++;
3111 c -= sizeof (png_uint_32);
3112 }
3113 while (c > 0);
3114
3115 if (row_width <= bytes_to_jump)
3116 return;
3117
3118 dp32 += skip;
3119 sp32 += skip;
3120 row_width -= bytes_to_jump;
3121 }
3122 while (bytes_to_copy <= row_width);
3123
3124 /* Get to here when the row_width truncates the final copy.
3125 * There will be 1-3 bytes left to copy, so don't try the
3126 * 16bit loop below.
3127 */
3128 dp = (png_bytep)dp32;
3129 sp = (png_const_bytep)sp32;
3130 do
3131 *dp++ = *sp++;
3132 while (--row_width > 0);
3133 return;
3134 }
3135
3136 /* Else do it in 16 bit quantities, but only if the size is
3137 * not too large.
3138 */
3139 else
3140 {
3141 png_uint_16p dp16 = (png_uint_16p)dp;
3142 png_const_uint_16p sp16 = (png_const_uint_16p)sp;
3143 unsigned int skip = (bytes_to_jump-bytes_to_copy) /
3144 sizeof (png_uint_16);
3145
3146 do
3147 {
3148 size_t c = bytes_to_copy;
3149 do
3150 {
3151 *dp16++ = *sp16++;
3152 c -= sizeof (png_uint_16);
3153 }
3154 while (c > 0);
3155
3156 if (row_width <= bytes_to_jump)
3157 return;
3158
3159 dp16 += skip;
3160 sp16 += skip;
3161 row_width -= bytes_to_jump;
3162 }
3163 while (bytes_to_copy <= row_width);
3164
3165 /* End of row - 1 byte left, bytes_to_copy>row_width: */
3166 dp = (png_bytep)dp16;
3167 sp = (png_const_bytep)sp16;
3168 do
3169 *dp++ = *sp++;
3170 while (--row_width > 0);
3171 return;
3172 }
3173 }
3174#endif /* PNG_ALIGN_ code */
3175
3176 /* The true default - use a memcpy: */
3177 for (;;)
3178 {
3179 png_memcpy(dp, sp, bytes_to_copy);
3180
3181 if (row_width <= bytes_to_jump)
3182 return;
3183
3184 sp += bytes_to_jump;
3185 dp += bytes_to_jump;
3186 row_width -= bytes_to_jump;
3187 if (bytes_to_copy > row_width)
3188 bytes_to_copy = row_width;
3189 }
3190 }
3191 } /* pixel_depth >= 8 */
3192
3193 /* NOT REACHED*/
Guy Schalnat0d580581995-07-20 02:43:20 -05003194 }
John Bowler4e68aa72011-10-11 16:01:33 -05003195 else
John Bowlerac8375d2011-10-06 22:27:16 -05003196#endif
3197
3198 /* If here then the switch above wasn't used so just memcpy the whole row
John Bowler4e68aa72011-10-11 16:01:33 -05003199 * from the temporary row buffer (notice that this overwrites the end of the
3200 * destination row if it is a partial byte.)
John Bowlerac8375d2011-10-06 22:27:16 -05003201 */
3202 png_memcpy(dp, sp, PNG_ROWBYTES(pixel_depth, row_width));
Guy Schalnat0d580581995-07-20 02:43:20 -05003203}
3204
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06003205#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05003206void /* PRIVATE */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003207png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
3208 png_uint_32 transformations /* Because these may affect the byte layout */)
Guy Schalnat0d580581995-07-20 02:43:20 -05003209{
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003210 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
3211 /* Offset to next interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003212 static PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003213
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05003214 png_debug(1, "in png_do_read_interlace");
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003215 if (row != NULL && row_info != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -05003216 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003217 png_uint_32 final_width;
3218
3219 final_width = row_info->width * png_pass_inc[pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05003220
3221 switch (row_info->pixel_depth)
3222 {
3223 case 1:
3224 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003225 png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3);
3226 png_bytep dp = row + (png_size_t)((final_width - 1) >> 3);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003227 int sshift, dshift;
3228 int s_start, s_end, s_inc;
3229 int jstop = png_pass_inc[pass];
3230 png_byte v;
Guy Schalnat0d580581995-07-20 02:43:20 -05003231 png_uint_32 i;
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003232 int j;
Guy Schalnat0d580581995-07-20 02:43:20 -05003233
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003234#ifdef PNG_READ_PACKSWAP_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003235 if (transformations & PNG_PACKSWAP)
3236 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003237 sshift = (int)((row_info->width + 7) & 0x07);
3238 dshift = (int)((final_width + 7) & 0x07);
3239 s_start = 7;
3240 s_end = 0;
3241 s_inc = -1;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003242 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003243
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003244 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05003245#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003246 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003247 sshift = 7 - (int)((row_info->width + 7) & 0x07);
3248 dshift = 7 - (int)((final_width + 7) & 0x07);
3249 s_start = 0;
3250 s_end = 7;
3251 s_inc = 1;
3252 }
Glenn Randers-Pehrson5dd2b8e2004-11-24 07:50:16 -06003253
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003254 for (i = 0; i < row_info->width; i++)
3255 {
3256 v = (png_byte)((*sp >> sshift) & 0x01);
3257 for (j = 0; j < jstop; j++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003258 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003259 *dp &= (png_byte)((0x7f7f >> (7 - dshift)) & 0xff);
3260 *dp |= (png_byte)(v << dshift);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003261
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003262 if (dshift == s_end)
3263 {
3264 dshift = s_start;
3265 dp--;
3266 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003267
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003268 else
3269 dshift += s_inc;
3270 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003271
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003272 if (sshift == s_end)
3273 {
3274 sshift = s_start;
Guy Schalnat0d580581995-07-20 02:43:20 -05003275 sp--;
3276 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003277
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003278 else
3279 sshift += s_inc;
Guy Schalnat0d580581995-07-20 02:43:20 -05003280 }
3281 break;
3282 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003283
Guy Schalnat0d580581995-07-20 02:43:20 -05003284 case 2:
3285 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003286 png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);
3287 png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);
3288 int sshift, dshift;
3289 int s_start, s_end, s_inc;
3290 int jstop = png_pass_inc[pass];
Andreas Dilger47a0c421997-05-16 02:46:07 -05003291 png_uint_32 i;
Guy Schalnat0d580581995-07-20 02:43:20 -05003292
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003293#ifdef PNG_READ_PACKSWAP_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003294 if (transformations & PNG_PACKSWAP)
3295 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003296 sshift = (int)(((row_info->width + 3) & 0x03) << 1);
3297 dshift = (int)(((final_width + 3) & 0x03) << 1);
3298 s_start = 6;
3299 s_end = 0;
3300 s_inc = -2;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003301 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003302
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003303 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05003304#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003305 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003306 sshift = (int)((3 - ((row_info->width + 3) & 0x03)) << 1);
3307 dshift = (int)((3 - ((final_width + 3) & 0x03)) << 1);
3308 s_start = 0;
3309 s_end = 6;
3310 s_inc = 2;
3311 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05003312
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003313 for (i = 0; i < row_info->width; i++)
3314 {
3315 png_byte v;
3316 int j;
3317
3318 v = (png_byte)((*sp >> sshift) & 0x03);
3319 for (j = 0; j < jstop; j++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003320 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003321 *dp &= (png_byte)((0x3f3f >> (6 - dshift)) & 0xff);
3322 *dp |= (png_byte)(v << dshift);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003323
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003324 if (dshift == s_end)
3325 {
3326 dshift = s_start;
3327 dp--;
3328 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003329
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003330 else
3331 dshift += s_inc;
3332 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003333
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003334 if (sshift == s_end)
3335 {
3336 sshift = s_start;
Guy Schalnat0d580581995-07-20 02:43:20 -05003337 sp--;
3338 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003339
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003340 else
3341 sshift += s_inc;
Guy Schalnat0d580581995-07-20 02:43:20 -05003342 }
3343 break;
3344 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003345
Guy Schalnat0d580581995-07-20 02:43:20 -05003346 case 4:
3347 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003348 png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1);
3349 png_bytep dp = row + (png_size_t)((final_width - 1) >> 1);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003350 int sshift, dshift;
3351 int s_start, s_end, s_inc;
Guy Schalnat0d580581995-07-20 02:43:20 -05003352 png_uint_32 i;
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003353 int jstop = png_pass_inc[pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05003354
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003355#ifdef PNG_READ_PACKSWAP_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003356 if (transformations & PNG_PACKSWAP)
3357 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003358 sshift = (int)(((row_info->width + 1) & 0x01) << 2);
3359 dshift = (int)(((final_width + 1) & 0x01) << 2);
3360 s_start = 4;
3361 s_end = 0;
3362 s_inc = -4;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003363 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003364
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003365 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05003366#endif
3367 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003368 sshift = (int)((1 - ((row_info->width + 1) & 0x01)) << 2);
3369 dshift = (int)((1 - ((final_width + 1) & 0x01)) << 2);
3370 s_start = 0;
3371 s_end = 4;
3372 s_inc = 4;
3373 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05003374
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003375 for (i = 0; i < row_info->width; i++)
3376 {
3377 png_byte v = (png_byte)((*sp >> sshift) & 0xf);
3378 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003379
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003380 for (j = 0; j < jstop; j++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003381 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003382 *dp &= (png_byte)((0xf0f >> (4 - dshift)) & 0xff);
3383 *dp |= (png_byte)(v << dshift);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003384
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003385 if (dshift == s_end)
3386 {
3387 dshift = s_start;
3388 dp--;
3389 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003390
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003391 else
3392 dshift += s_inc;
3393 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003394
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003395 if (sshift == s_end)
3396 {
3397 sshift = s_start;
Guy Schalnat0d580581995-07-20 02:43:20 -05003398 sp--;
3399 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003400
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003401 else
3402 sshift += s_inc;
Guy Schalnat0d580581995-07-20 02:43:20 -05003403 }
3404 break;
3405 }
John Bowlerac8375d2011-10-06 22:27:16 -05003406
Guy Schalnat0d580581995-07-20 02:43:20 -05003407 default:
3408 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003409 png_size_t pixel_bytes = (row_info->pixel_depth >> 3);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003410
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06003411 png_bytep sp = row + (png_size_t)(row_info->width - 1)
3412 * pixel_bytes;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003413
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003414 png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05003415
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003416 int jstop = png_pass_inc[pass];
3417 png_uint_32 i;
3418
3419 for (i = 0; i < row_info->width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003420 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003421 png_byte v[8];
3422 int j;
3423
3424 png_memcpy(v, sp, pixel_bytes);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003425
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003426 for (j = 0; j < jstop; j++)
3427 {
3428 png_memcpy(dp, v, pixel_bytes);
3429 dp -= pixel_bytes;
3430 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003431
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003432 sp -= pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05003433 }
3434 break;
3435 }
3436 }
John Bowlerac8375d2011-10-06 22:27:16 -05003437
Guy Schalnat0d580581995-07-20 02:43:20 -05003438 row_info->width = final_width;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003439 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width);
Guy Schalnat0d580581995-07-20 02:43:20 -05003440 }
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -05003441#ifndef PNG_READ_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003442 PNG_UNUSED(transformations) /* Silence compiler warning */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05003443#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003444}
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06003445#endif /* PNG_READ_INTERLACING_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05003446
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003447/* 1.5.6: Changed to just take a png_row_info (not png_ptr) and to ignore bad
3448 * adaptive filter bytes.
3449 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05003450void /* PRIVATE */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003451png_read_filter_row(png_row_infop row_info, png_bytep row,
3452 png_const_bytep prev_row, int filter)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003453{
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003454 switch (filter)
3455 {
3456 case PNG_FILTER_VALUE_NONE:
3457 break;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003458
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003459 case PNG_FILTER_VALUE_SUB:
3460 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003461 png_size_t i;
3462 png_size_t istop = row_info->rowbytes;
3463 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003464 png_bytep rp = row + bpp;
3465 png_bytep lp = row;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003466
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003467 for (i = bpp; i < istop; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003468 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05003469 *rp = (png_byte)(((int)(*rp) + (int)(*lp++)) & 0xff);
3470 rp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003471 }
3472 break;
3473 }
3474 case PNG_FILTER_VALUE_UP:
3475 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003476 png_size_t i;
3477 png_size_t istop = row_info->rowbytes;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003478 png_bytep rp = row;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003479 png_const_bytep pp = prev_row;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003480
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003481 for (i = 0; i < istop; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003482 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05003483 *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
3484 rp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003485 }
3486 break;
3487 }
3488 case PNG_FILTER_VALUE_AVG:
3489 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003490 png_size_t i;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003491 png_bytep rp = row;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003492 png_const_bytep pp = prev_row;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003493 png_bytep lp = row;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003494 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
3495 png_size_t istop = row_info->rowbytes - bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003496
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003497 for (i = 0; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003498 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05003499 *rp = (png_byte)(((int)(*rp) +
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003500 ((int)(*pp++) / 2 )) & 0xff);
3501
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05003502 rp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003503 }
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003504
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05003505 for (i = 0; i < istop; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003506 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05003507 *rp = (png_byte)(((int)(*rp) +
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003508 (int)(*pp++ + *lp++) / 2 ) & 0xff);
3509
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05003510 rp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003511 }
3512 break;
3513 }
3514 case PNG_FILTER_VALUE_PAETH:
3515 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003516 png_size_t i;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003517 png_bytep rp = row;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003518 png_const_bytep pp = prev_row;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003519 png_bytep lp = row;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003520 png_const_bytep cp = prev_row;
3521 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
3522 png_size_t istop=row_info->rowbytes - bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003523
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003524 for (i = 0; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003525 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05003526 *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
3527 rp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003528 }
3529
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003530 for (i = 0; i < istop; i++) /* Use leftover rp,pp */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003531 {
3532 int a, b, c, pa, pb, pc, p;
3533
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003534 a = *lp++;
3535 b = *pp++;
3536 c = *cp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003537
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003538 p = b - c;
3539 pc = a - c;
3540
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003541#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003542 pa = abs(p);
3543 pb = abs(pc);
3544 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003545#else
Glenn Randers-Pehrsona565f0e2010-03-06 08:24:45 -06003546 pa = p < 0 ? -p : p;
3547 pb = pc < 0 ? -pc : pc;
3548 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003549#endif
3550
3551 /*
3552 if (pa <= pb && pa <= pc)
3553 p = a;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003554
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003555 else if (pb <= pc)
3556 p = b;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003557
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003558 else
3559 p = c;
3560 */
3561
Glenn Randers-Pehrsona565f0e2010-03-06 08:24:45 -06003562 p = (pa <= pb && pa <= pc) ? a : (pb <= pc) ? b : c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003563
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05003564 *rp = (png_byte)(((int)(*rp) + p) & 0xff);
3565 rp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003566 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003567 break;
3568 }
3569 default:
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003570 /* NOT REACHED */
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05003571 break;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003572 }
3573}
Guy Schalnat0d580581995-07-20 02:43:20 -05003574
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05003575#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05003576void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06003577png_read_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05003578{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003579#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003580 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003581
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003582 /* Start of interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003583 static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003584
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003585 /* Offset to next interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003586 static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003587
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003588 /* Start of interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003589 static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003590
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003591 /* Offset to next interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003592 static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003593#endif /* PNG_READ_INTERLACING_SUPPORTED */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003594
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05003595 png_debug(1, "in png_read_finish_row");
Guy Schalnat0d580581995-07-20 02:43:20 -05003596 png_ptr->row_number++;
3597 if (png_ptr->row_number < png_ptr->num_rows)
3598 return;
3599
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003600#ifdef PNG_READ_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05003601 if (png_ptr->interlaced)
3602 {
3603 png_ptr->row_number = 0;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003604
Glenn Randers-Pehrson435cf872011-10-05 16:23:53 -05003605 /* TO DO: don't do this if prev_row isn't needed (requires
3606 * read-ahead of the next row's filter byte.
3607 */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003608 png_memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
3609
Guy Schalnat0d580581995-07-20 02:43:20 -05003610 do
3611 {
3612 png_ptr->pass++;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003613
Guy Schalnat0d580581995-07-20 02:43:20 -05003614 if (png_ptr->pass >= 7)
3615 break;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003616
Guy Schalnat0d580581995-07-20 02:43:20 -05003617 png_ptr->iwidth = (png_ptr->width +
3618 png_pass_inc[png_ptr->pass] - 1 -
3619 png_pass_start[png_ptr->pass]) /
3620 png_pass_inc[png_ptr->pass];
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05003621
Guy Schalnat0d580581995-07-20 02:43:20 -05003622 if (!(png_ptr->transformations & PNG_INTERLACE))
3623 {
3624 png_ptr->num_rows = (png_ptr->height +
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003625 png_pass_yinc[png_ptr->pass] - 1 -
3626 png_pass_ystart[png_ptr->pass]) /
3627 png_pass_yinc[png_ptr->pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05003628 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003629
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -05003630 else /* if (png_ptr->transformations & PNG_INTERLACE) */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003631 break; /* libpng deinterlacing sees every row */
3632
3633 } while (png_ptr->num_rows == 0 || png_ptr->iwidth == 0);
Guy Schalnat0d580581995-07-20 02:43:20 -05003634
3635 if (png_ptr->pass < 7)
3636 return;
3637 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003638#endif /* PNG_READ_INTERLACING_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05003639
Guy Schalnate5a37791996-06-05 15:50:50 -05003640 if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
Guy Schalnat0d580581995-07-20 02:43:20 -05003641 {
3642 char extra;
3643 int ret;
3644
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003645 png_ptr->zstream.next_out = (Byte *)&extra;
3646 png_ptr->zstream.avail_out = (uInt)1;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003647
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003648 for (;;)
Guy Schalnat0d580581995-07-20 02:43:20 -05003649 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003650 if (!(png_ptr->zstream.avail_in))
Guy Schalnat0d580581995-07-20 02:43:20 -05003651 {
3652 while (!png_ptr->idat_size)
3653 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003654 png_crc_finish(png_ptr, 0);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003655 png_ptr->idat_size = png_read_chunk_header(png_ptr);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003656 if (png_ptr->chunk_name != png_IDAT)
Guy Schalnat6d764711995-12-19 03:22:19 -06003657 png_error(png_ptr, "Not enough image data");
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -06003658 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003659
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003660 png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
3661 png_ptr->zstream.next_in = png_ptr->zbuf;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003662
Guy Schalnat0d580581995-07-20 02:43:20 -05003663 if (png_ptr->zbuf_size > png_ptr->idat_size)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003664 png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003665
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003666 png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zstream.avail_in);
3667 png_ptr->idat_size -= png_ptr->zstream.avail_in;
Guy Schalnat0d580581995-07-20 02:43:20 -05003668 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003669
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003670 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003671
Guy Schalnat0d580581995-07-20 02:43:20 -05003672 if (ret == Z_STREAM_END)
3673 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003674 if (!(png_ptr->zstream.avail_out) || png_ptr->zstream.avail_in ||
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003675 png_ptr->idat_size)
Glenn Randers-Pehrson859665d2002-08-06 18:06:11 -05003676 png_warning(png_ptr, "Extra compressed data");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003677
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003678 png_ptr->mode |= PNG_AFTER_IDAT;
3679 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
Guy Schalnat0d580581995-07-20 02:43:20 -05003680 break;
3681 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003682
Guy Schalnat0d580581995-07-20 02:43:20 -05003683 if (ret != Z_OK)
Glenn Randers-Pehrsona565f0e2010-03-06 08:24:45 -06003684 png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003685 "Decompression Error");
Guy Schalnat0d580581995-07-20 02:43:20 -05003686
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003687 if (!(png_ptr->zstream.avail_out))
Glenn Randers-Pehrson859665d2002-08-06 18:06:11 -05003688 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05003689 png_warning(png_ptr, "Extra compressed data");
Glenn Randers-Pehrson859665d2002-08-06 18:06:11 -05003690 png_ptr->mode |= PNG_AFTER_IDAT;
3691 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
3692 break;
3693 }
Guy Schalnat0d580581995-07-20 02:43:20 -05003694
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06003695 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003696 png_ptr->zstream.avail_out = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -05003697 }
3698
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003699 if (png_ptr->idat_size || png_ptr->zstream.avail_in)
Glenn Randers-Pehrson859665d2002-08-06 18:06:11 -05003700 png_warning(png_ptr, "Extra compression data");
Guy Schalnat0d580581995-07-20 02:43:20 -05003701
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003702 inflateReset(&png_ptr->zstream);
Guy Schalnat0d580581995-07-20 02:43:20 -05003703
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003704 png_ptr->mode |= PNG_AFTER_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -05003705}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05003706#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05003707
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05003708void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06003709png_read_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05003710{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003711#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003712 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003713
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003714 /* Start of interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003715 static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003716
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003717 /* Offset to next interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003718 static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003719
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003720 /* Start of interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003721 static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003722
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003723 /* Offset to next interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003724 static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003725#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003726
Guy Schalnat0d580581995-07-20 02:43:20 -05003727 int max_pixel_depth;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05003728 png_size_t row_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05003729
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05003730 png_debug(1, "in png_read_start_row");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003731 png_ptr->zstream.avail_in = 0;
John Bowler4a12f4a2011-04-17 18:34:22 -05003732#ifdef PNG_READ_TRANSFORMS_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05003733 png_init_read_transformations(png_ptr);
John Bowler4a12f4a2011-04-17 18:34:22 -05003734#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003735#ifdef PNG_READ_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05003736 if (png_ptr->interlaced)
3737 {
3738 if (!(png_ptr->transformations & PNG_INTERLACE))
3739 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003740 png_pass_ystart[0]) / png_pass_yinc[0];
3741
Guy Schalnat0d580581995-07-20 02:43:20 -05003742 else
3743 png_ptr->num_rows = png_ptr->height;
3744
3745 png_ptr->iwidth = (png_ptr->width +
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003746 png_pass_inc[png_ptr->pass] - 1 -
3747 png_pass_start[png_ptr->pass]) /
3748 png_pass_inc[png_ptr->pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05003749 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003750
Guy Schalnat0d580581995-07-20 02:43:20 -05003751 else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003752#endif /* PNG_READ_INTERLACING_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05003753 {
3754 png_ptr->num_rows = png_ptr->height;
3755 png_ptr->iwidth = png_ptr->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05003756 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003757
Guy Schalnat0d580581995-07-20 02:43:20 -05003758 max_pixel_depth = png_ptr->pixel_depth;
3759
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003760#ifdef PNG_READ_PACK_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05003761 if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8)
Guy Schalnat0d580581995-07-20 02:43:20 -05003762 max_pixel_depth = 8;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05003763#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003764
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003765#ifdef PNG_READ_EXPAND_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -05003766 if (png_ptr->transformations & PNG_EXPAND)
Guy Schalnat0d580581995-07-20 02:43:20 -05003767 {
3768 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
3769 {
3770 if (png_ptr->num_trans)
3771 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003772
Guy Schalnat0d580581995-07-20 02:43:20 -05003773 else
3774 max_pixel_depth = 24;
3775 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003776
Guy Schalnat0d580581995-07-20 02:43:20 -05003777 else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
3778 {
3779 if (max_pixel_depth < 8)
3780 max_pixel_depth = 8;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003781
Guy Schalnat0d580581995-07-20 02:43:20 -05003782 if (png_ptr->num_trans)
3783 max_pixel_depth *= 2;
3784 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003785
Guy Schalnat0d580581995-07-20 02:43:20 -05003786 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
3787 {
3788 if (png_ptr->num_trans)
3789 {
3790 max_pixel_depth *= 4;
3791 max_pixel_depth /= 3;
3792 }
3793 }
3794 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05003795#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003796
John Bowler4d562962011-02-12 09:01:20 -06003797#ifdef PNG_READ_EXPAND_16_SUPPORTED
3798 if (png_ptr->transformations & PNG_EXPAND_16)
3799 {
3800# ifdef PNG_READ_EXPAND_SUPPORTED
3801 /* In fact it is an error if it isn't supported, but checking is
3802 * the safe way.
3803 */
3804 if (png_ptr->transformations & PNG_EXPAND)
3805 {
3806 if (png_ptr->bit_depth < 16)
3807 max_pixel_depth *= 2;
3808 }
3809 else
3810# endif
3811 png_ptr->transformations &= ~PNG_EXPAND_16;
3812 }
3813#endif
3814
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003815#ifdef PNG_READ_FILLER_SUPPORTED
Guy Schalnat51f0eb41995-09-26 05:22:39 -05003816 if (png_ptr->transformations & (PNG_FILLER))
Guy Schalnat0d580581995-07-20 02:43:20 -05003817 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05003818 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
3819 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003820
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05003821 else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003822 {
3823 if (max_pixel_depth <= 8)
3824 max_pixel_depth = 16;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003825
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003826 else
3827 max_pixel_depth = 32;
3828 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003829
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003830 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
3831 {
3832 if (max_pixel_depth <= 32)
3833 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003834
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003835 else
3836 max_pixel_depth = 64;
3837 }
Guy Schalnat0d580581995-07-20 02:43:20 -05003838 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05003839#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003840
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003841#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05003842 if (png_ptr->transformations & PNG_GRAY_TO_RGB)
3843 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003844 if (
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003845#ifdef PNG_READ_EXPAND_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003846 (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) ||
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003847#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003848#ifdef PNG_READ_FILLER_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003849 (png_ptr->transformations & (PNG_FILLER)) ||
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003850#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003851 png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
Guy Schalnat0d580581995-07-20 02:43:20 -05003852 {
3853 if (max_pixel_depth <= 16)
3854 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003855
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003856 else
Guy Schalnat0d580581995-07-20 02:43:20 -05003857 max_pixel_depth = 64;
3858 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003859
Guy Schalnat0d580581995-07-20 02:43:20 -05003860 else
3861 {
3862 if (max_pixel_depth <= 8)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003863 {
3864 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06003865 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003866
3867 else
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06003868 max_pixel_depth = 24;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003869 }
3870
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06003871 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
3872 max_pixel_depth = 64;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003873
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003874 else
Guy Schalnat0d580581995-07-20 02:43:20 -05003875 max_pixel_depth = 48;
3876 }
3877 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05003878#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003879
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05003880#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
3881defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003882 if (png_ptr->transformations & PNG_USER_TRANSFORM)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003883 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003884 int user_pixel_depth = png_ptr->user_transform_depth *
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05003885 png_ptr->user_transform_channels;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003886
3887 if (user_pixel_depth > max_pixel_depth)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003888 max_pixel_depth = user_pixel_depth;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003889 }
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05003890#endif
3891
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003892 /* This value is stored in png_struct and double checked in the row read
3893 * code.
3894 */
3895 png_ptr->maximum_pixel_depth = (png_byte)max_pixel_depth;
3896 png_ptr->transformed_pixel_depth = 0; /* calculated on demand */
3897
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003898 /* Align the width on the next larger 8 pixels. Mainly used
3899 * for interlacing
3900 */
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05003901 row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003902 /* Calculate the maximum bytes needed, adding a byte and a pixel
3903 * for safety's sake
3904 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003905 row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) +
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003906 1 + ((max_pixel_depth + 7) >> 3);
3907
Guy Schalnat0d580581995-07-20 02:43:20 -05003908#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05003909 if (row_bytes > (png_uint_32)65536L)
Guy Schalnate5a37791996-06-05 15:50:50 -05003910 png_error(png_ptr, "This image requires a row greater than 64KB");
Guy Schalnat0d580581995-07-20 02:43:20 -05003911#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003912
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06003913 if (row_bytes + 48 > png_ptr->old_big_row_buf_size)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003914 {
3915 png_free(png_ptr, png_ptr->big_row_buf);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003916
Glenn Randers-Pehrson6917b512009-03-09 15:31:08 -05003917 if (png_ptr->interlaced)
Glenn Randers-Pehrsona515d302010-01-01 10:24:25 -06003918 png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr,
3919 row_bytes + 48);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003920
Glenn Randers-Pehrsona515d302010-01-01 10:24:25 -06003921 else
3922 png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr,
3923 row_bytes + 48);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003924
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06003925 png_ptr->old_big_row_buf_size = row_bytes + 48;
3926
3927#ifdef PNG_ALIGNED_MEMORY_SUPPORTED
3928 /* Use 16-byte aligned memory for row_buf with at least 16 bytes
3929 * of padding before and after row_buf.
John Bowlerac8375d2011-10-06 22:27:16 -05003930 * NOTE: the alignment is to the start of the pixels, one beyond the start
3931 * of the buffer, because of the filter byte. Prior to libpng 1.5.6 this
3932 * was incorrect, the filter byte was aligned, which had the exact opposite
3933 * effect to that intended.
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06003934 */
John Bowlerac8375d2011-10-06 22:27:16 -05003935 {
3936 png_bytep temp = png_ptr->big_row_buf + 32;
3937 int extra = (int)((temp - (png_bytep)0) & 0xf);
3938 png_ptr->row_buf = temp - extra - 1/*filter byte*/;
3939 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003940
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06003941 png_ptr->old_big_row_buf_size = row_bytes + 48;
3942#else
John Bowlerac8375d2011-10-06 22:27:16 -05003943 /* Use 31 bytes of padding before and 17 bytes after row_buf. */
3944 png_ptr->row_buf = png_ptr->big_row_buf + 31;
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06003945#endif
3946 png_ptr->old_big_row_buf_size = row_bytes + 48;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003947 }
Guy Schalnat0d580581995-07-20 02:43:20 -05003948
3949#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003950 if (png_ptr->rowbytes > 65535)
Guy Schalnate5a37791996-06-05 15:50:50 -05003951 png_error(png_ptr, "This image requires a row greater than 64KB");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003952
Guy Schalnat0d580581995-07-20 02:43:20 -05003953#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003954 if (png_ptr->rowbytes > (PNG_SIZE_MAX - 1))
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05003955 png_error(png_ptr, "Row has too many bytes to allocate in memory");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003956
Glenn Randers-Pehrsona98aa482009-10-13 09:23:39 -05003957 if (png_ptr->rowbytes + 1 > png_ptr->old_prev_row_size)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003958 {
Glenn Randers-Pehrsona98aa482009-10-13 09:23:39 -05003959 png_free(png_ptr, png_ptr->prev_row);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003960
3961 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, png_ptr->rowbytes + 1);
3962
Glenn Randers-Pehrsona98aa482009-10-13 09:23:39 -05003963 png_ptr->old_prev_row_size = png_ptr->rowbytes + 1;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003964 }
Guy Schalnat0d580581995-07-20 02:43:20 -05003965
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05003966 png_memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05003967
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003968 png_debug1(3, "width = %u,", png_ptr->width);
3969 png_debug1(3, "height = %u,", png_ptr->height);
3970 png_debug1(3, "iwidth = %u,", png_ptr->iwidth);
3971 png_debug1(3, "num_rows = %u,", png_ptr->num_rows);
Glenn Randers-Pehrsonb764c602011-01-14 21:18:37 -06003972 png_debug1(3, "rowbytes = %lu,", (unsigned long)png_ptr->rowbytes);
3973 png_debug1(3, "irowbytes = %lu",
3974 (unsigned long)PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05003975
Guy Schalnate5a37791996-06-05 15:50:50 -05003976 png_ptr->flags |= PNG_FLAG_ROW_INIT;
Guy Schalnat0d580581995-07-20 02:43:20 -05003977}
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06003978#endif /* PNG_READ_SUPPORTED */