blob: 01d346682fbdfe2be4338abc26eb1826a5ac4e4b [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-Pehrson05670152014-03-08 12:39:52 -06004 * Last changed in libpng 1.6.11 [(PENDING RELEASE)]
Glenn Randers-Pehrson9eec1592014-02-09 12:49:39 -06005 * Copyright (c) 1998-2014 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 -060021png_uint_32 PNGAPI
John Bowler5d567862011-12-24 09:12:00 -060022png_get_uint_31(png_const_structrp png_ptr, png_const_bytep buf)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -060023{
24 png_uint_32 uval = png_get_uint_32(buf);
25
26 if (uval > PNG_UINT_31_MAX)
27 png_error(png_ptr, "PNG unsigned integer out of range");
28
29 return (uval);
30}
31
32#if defined(PNG_READ_gAMA_SUPPORTED) || defined(PNG_READ_cHRM_SUPPORTED)
33/* The following is a variation on the above for use with the fixed
34 * point values used for gAMA and cHRM. Instead of png_error it
35 * issues a warning and returns (-1) - an invalid value because both
36 * gAMA and cHRM use *unsigned* integers for fixed point values.
37 */
38#define PNG_FIXED_ERROR (-1)
39
40static png_fixed_point /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -060041png_get_fixed_point(png_structrp png_ptr, png_const_bytep buf)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -060042{
43 png_uint_32 uval = png_get_uint_32(buf);
44
45 if (uval <= PNG_UINT_31_MAX)
46 return (png_fixed_point)uval; /* known to be in range */
47
48 /* The caller can turn off the warning by passing NULL. */
49 if (png_ptr != NULL)
50 png_warning(png_ptr, "PNG fixed point integer out of range");
51
52 return PNG_FIXED_ERROR;
53}
54#endif
55
56#ifdef PNG_READ_INT_FUNCTIONS_SUPPORTED
57/* NOTE: the read macros will obscure these definitions, so that if
58 * PNG_USE_READ_MACROS is set the library will not use them internally,
59 * but the APIs will still be available externally.
60 *
61 * The parentheses around "PNGAPI function_name" in the following three
62 * functions are necessary because they allow the macros to co-exist with
63 * these (unused but exported) functions.
64 */
65
66/* Grab an unsigned 32-bit integer from a buffer in big-endian format. */
67png_uint_32 (PNGAPI
68png_get_uint_32)(png_const_bytep buf)
69{
70 png_uint_32 uval =
71 ((png_uint_32)(*(buf )) << 24) +
72 ((png_uint_32)(*(buf + 1)) << 16) +
73 ((png_uint_32)(*(buf + 2)) << 8) +
74 ((png_uint_32)(*(buf + 3)) ) ;
75
76 return uval;
Guy Schalnat0d580581995-07-20 02:43:20 -050077}
78
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -050079/* Grab a signed 32-bit integer from a buffer in big-endian format. The
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -060080 * data is stored in the PNG file in two's complement format and there
81 * is no guarantee that a 'png_int_32' is exactly 32 bits, therefore
82 * the following code does a two's complement to native conversion.
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050083 */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -060084png_int_32 (PNGAPI
85png_get_int_32)(png_const_bytep buf)
Andreas Dilger47a0c421997-05-16 02:46:07 -050086{
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -060087 png_uint_32 uval = png_get_uint_32(buf);
John Bowlerf3f7e142011-09-09 07:32:37 -050088 if ((uval & 0x80000000) == 0) /* non-negative */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -060089 return uval;
Andreas Dilger47a0c421997-05-16 02:46:07 -050090
John Bowlerf3f7e142011-09-09 07:32:37 -050091 uval = (uval ^ 0xffffffff) + 1; /* 2's complement: -x = ~x+1 */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -060092 return -(png_int_32)uval;
Andreas Dilger47a0c421997-05-16 02:46:07 -050093}
Andreas Dilger47a0c421997-05-16 02:46:07 -050094
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -050095/* Grab an unsigned 16-bit integer from a buffer in big-endian format. */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -060096png_uint_16 (PNGAPI
97png_get_uint_16)(png_const_bytep buf)
Guy Schalnat0d580581995-07-20 02:43:20 -050098{
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -060099 /* ANSI-C requires an int value to accomodate at least 16 bits so this
100 * works and allows the compiler not to worry about possible narrowing
101 * on 32 bit systems. (Pre-ANSI systems did not make integers smaller
102 * than 16 bits either.)
103 */
104 unsigned int val =
105 ((unsigned int)(*buf) << 8) +
106 ((unsigned int)(*(buf + 1)));
Guy Schalnat0d580581995-07-20 02:43:20 -0500107
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600108 return (png_uint_16)val;
Guy Schalnat0d580581995-07-20 02:43:20 -0500109}
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600110
111#endif /* PNG_READ_INT_FUNCTIONS_SUPPORTED */
112
113/* Read and check the PNG file signature */
114void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600115png_read_sig(png_structrp png_ptr, png_inforp info_ptr)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600116{
117 png_size_t num_checked, num_to_check;
118
119 /* Exit if the user application does not expect a signature. */
120 if (png_ptr->sig_bytes >= 8)
121 return;
122
123 num_checked = png_ptr->sig_bytes;
124 num_to_check = 8 - num_checked;
125
126#ifdef PNG_IO_STATE_SUPPORTED
127 png_ptr->io_state = PNG_IO_READING | PNG_IO_SIGNATURE;
128#endif
129
130 /* The signature must be serialized in a single I/O call. */
131 png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
132 png_ptr->sig_bytes = 8;
133
134 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
135 {
136 if (num_checked < 4 &&
137 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
138 png_error(png_ptr, "Not a PNG file");
139 else
140 png_error(png_ptr, "PNG file corrupted by ASCII conversion");
141 }
142 if (num_checked < 3)
143 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
144}
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -0600145
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500146/* Read the chunk header (length + type name).
147 * Put the type name into png_ptr->chunk_name, and return the length.
148 */
149png_uint_32 /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600150png_read_chunk_header(png_structrp png_ptr)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500151{
152 png_byte buf[8];
153 png_uint_32 length;
154
155#ifdef PNG_IO_STATE_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500156 png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_HDR;
157#endif
158
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600159 /* Read the length and the chunk name.
160 * This must be performed in a single I/O call.
161 */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500162 png_read_data(png_ptr, buf, 8);
163 length = png_get_uint_31(png_ptr, buf);
164
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600165 /* Put the chunk name into png_ptr->chunk_name. */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500166 png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(buf+4);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500167
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500168 png_debug2(0, "Reading %lx chunk, length = %lu",
169 (unsigned long)png_ptr->chunk_name, (unsigned long)length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500170
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600171 /* Reset the crc and run it over the chunk name. */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500172 png_reset_crc(png_ptr);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500173 png_calculate_crc(png_ptr, buf + 4, 4);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500174
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600175 /* Check to see if chunk name is valid. */
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500176 png_check_chunk_name(png_ptr, png_ptr->chunk_name);
177
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500178#ifdef PNG_IO_STATE_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500179 png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_DATA;
180#endif
181
182 return length;
183}
184
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500185/* Read data, and (optionally) run it through the CRC. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500186void /* PRIVATE */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600187png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500188{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500189 if (png_ptr == NULL)
190 return;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600191
Guy Schalnat6d764711995-12-19 03:22:19 -0600192 png_read_data(png_ptr, buf, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500193 png_calculate_crc(png_ptr, buf, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500194}
195
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600196/* Optionally skip data and then check the CRC. Depending on whether we
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600197 * are reading an ancillary or critical chunk, and how the program has set
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500198 * things up, we may calculate the CRC on the data and print a message.
199 * Returns '1' if there was a CRC error, '0' otherwise.
200 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500201int /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600202png_crc_finish(png_structrp png_ptr, png_uint_32 skip)
Guy Schalnat0d580581995-07-20 02:43:20 -0500203{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600204 /* The size of the local buffer for inflate is a good guess as to a
205 * reasonable size to use for buffering reads from the application.
206 */
207 while (skip > 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600208 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600209 png_uint_32 len;
210 png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600211
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600212 len = (sizeof tmpbuf);
213 if (len > skip)
214 len = skip;
215 skip -= len;
216
217 png_crc_read(png_ptr, tmpbuf, len);
Guy Schalnat0d580581995-07-20 02:43:20 -0500218 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600219
Andreas Dilger47a0c421997-05-16 02:46:07 -0500220 if (png_crc_error(png_ptr))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600221 {
Glenn Randers-Pehrson67a289f2013-04-19 19:03:34 -0500222 if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) ?
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500223 !(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) :
224 (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600225 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600226 png_chunk_warning(png_ptr, "CRC error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600227 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600228
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600229 else
Glenn Randers-Pehrsoneb657ae2014-02-22 21:59:40 -0600230 png_chunk_error(png_ptr, "CRC error");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600231
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600232 return (1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600233 }
234
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600235 return (0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500236}
237
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600238/* 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 -0500239 * the data it has read thus far.
240 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500241int /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600242png_crc_error(png_structrp png_ptr)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600243{
244 png_byte crc_bytes[4];
245 png_uint_32 crc;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500246 int need_crc = 1;
247
Glenn Randers-Pehrson67a289f2013-04-19 19:03:34 -0500248 if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500249 {
250 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
251 (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
252 need_crc = 0;
253 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600254
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500255 else /* critical */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500256 {
257 if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
258 need_crc = 0;
259 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600260
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500261#ifdef PNG_IO_STATE_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500262 png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_CRC;
263#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500264
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600265 /* The chunk CRC must be serialized in a single I/O call. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600266 png_read_data(png_ptr, crc_bytes, 4);
267
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -0600268 if (need_crc != 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500269 {
270 crc = png_get_uint_32(crc_bytes);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600271 return ((int)(crc != png_ptr->crc));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500272 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600273
Andreas Dilger47a0c421997-05-16 02:46:07 -0500274 else
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600275 return (0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600276}
277
Glenn Randers-Pehrson11321342013-11-18 20:12:24 -0600278#if defined(PNG_READ_iCCP_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) ||\
279 defined(PNG_READ_pCAL_SUPPORTED) || defined(PNG_READ_sCAL_SUPPORTED) ||\
280 defined(PNG_READ_sPLT_SUPPORTED) || defined(PNG_READ_tEXt_SUPPORTED) ||\
281 defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_SEQUENTIAL_READ_SUPPORTED)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600282/* Manage the read buffer; this simply reallocates the buffer if it is not small
283 * enough (or if it is not allocated). The routine returns a pointer to the
284 * buffer; if an error occurs and 'warn' is set the routine returns NULL, else
285 * it will call png_error (via png_malloc) on failure. (warn == 2 means
286 * 'silent').
287 */
288static png_bytep
289png_read_buffer(png_structrp png_ptr, png_alloc_size_t new_size, int warn)
John Bowler42a2b552012-03-05 20:57:40 -0600290{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600291 png_bytep buffer = png_ptr->read_buffer;
John Bowlerb5d00512012-03-09 09:15:18 -0600292
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600293 if (buffer != NULL && new_size > png_ptr->read_buffer_size)
John Bowlerb5d00512012-03-09 09:15:18 -0600294 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600295 png_ptr->read_buffer = NULL;
296 png_ptr->read_buffer = NULL;
297 png_ptr->read_buffer_size = 0;
298 png_free(png_ptr, buffer);
299 buffer = NULL;
300 }
John Bowlerb5d00512012-03-09 09:15:18 -0600301
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600302 if (buffer == NULL)
303 {
304 buffer = png_voidcast(png_bytep, png_malloc_base(png_ptr, new_size));
305
306 if (buffer != NULL)
John Bowlerb5d00512012-03-09 09:15:18 -0600307 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600308 png_ptr->read_buffer = buffer;
309 png_ptr->read_buffer_size = new_size;
John Bowlerb5d00512012-03-09 09:15:18 -0600310 }
John Bowlerb5d00512012-03-09 09:15:18 -0600311
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600312 else if (warn < 2) /* else silent */
John Bowlerb5d00512012-03-09 09:15:18 -0600313 {
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -0600314 if (warn != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600315 png_chunk_warning(png_ptr, "insufficient memory to read chunk");
Glenn Randers-Pehrson45625ec2014-02-22 23:09:27 -0600316
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600317 else
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600318 png_chunk_error(png_ptr, "insufficient memory to read chunk");
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600319 }
320 }
321
322 return buffer;
323}
Glenn Randers-Pehrson11321342013-11-18 20:12:24 -0600324#endif /* PNG_READ_iCCP|iTXt|pCAL|sCAL|sPLT|tEXt|zTXt|SEQUENTIAL_READ */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600325
326/* png_inflate_claim: claim the zstream for some nefarious purpose that involves
327 * decompression. Returns Z_OK on success, else a zlib error code. It checks
328 * the owner but, in final release builds, just issues a warning if some other
329 * chunk apparently owns the stream. Prior to release it does a png_error.
330 */
331static int
John Bowler0c7ac062013-05-07 21:59:05 -0500332png_inflate_claim(png_structrp png_ptr, png_uint_32 owner)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600333{
334 if (png_ptr->zowner != 0)
335 {
336 char msg[64];
337
338 PNG_STRING_FROM_CHUNK(msg, png_ptr->zowner);
339 /* So the message that results is "<chunk> using zstream"; this is an
340 * internal error, but is very useful for debugging. i18n requirements
341 * are minimal.
342 */
343 (void)png_safecat(msg, (sizeof msg), 4, " using zstream");
344# if PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC
345 png_chunk_warning(png_ptr, msg);
346 png_ptr->zowner = 0;
347# else
348 png_chunk_error(png_ptr, msg);
349# endif
350 }
351
352 /* Implementation note: unlike 'png_deflate_claim' this internal function
353 * does not take the size of the data as an argument. Some efficiency could
354 * be gained by using this when it is known *if* the zlib stream itself does
355 * not record the number; however, this is an illusion: the original writer
356 * of the PNG may have selected a lower window size, and we really must
357 * follow that because, for systems with with limited capabilities, we
358 * would otherwise reject the application's attempts to use a smaller window
359 * size (zlib doesn't have an interface to say "this or lower"!).
360 *
361 * inflateReset2 was added to zlib 1.2.4; before this the window could not be
362 * reset, therefore it is necessary to always allocate the maximum window
363 * size with earlier zlibs just in case later compressed chunks need it.
364 */
365 {
366 int ret; /* zlib return code */
John Bowler9afb90f2013-05-08 14:21:46 -0500367# if PNG_ZLIB_VERNUM >= 0x1240
John Bowler0c7ac062013-05-07 21:59:05 -0500368
Glenn Randers-Pehrson62c6fbb2013-05-07 23:16:06 -0500369# if defined(PNG_SET_OPTION_SUPPORTED) && \
370 defined(PNG_MAXIMUM_INFLATE_WINDOW)
John Bowler0c7ac062013-05-07 21:59:05 -0500371 int window_bits;
372
373 if (((png_ptr->options >> PNG_MAXIMUM_INFLATE_WINDOW) & 3) ==
374 PNG_OPTION_ON)
375 window_bits = 15;
376
377 else
378 window_bits = 0;
379# else
380# define window_bits 0
381# endif
382# endif
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600383
384 /* Set this for safety, just in case the previous owner left pointers to
385 * memory allocations.
386 */
387 png_ptr->zstream.next_in = NULL;
388 png_ptr->zstream.avail_in = 0;
389 png_ptr->zstream.next_out = NULL;
390 png_ptr->zstream.avail_out = 0;
391
392 if (png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED)
393 {
John Bowler9afb90f2013-05-08 14:21:46 -0500394# if PNG_ZLIB_VERNUM < 0x1240
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600395 ret = inflateReset(&png_ptr->zstream);
396# else
397 ret = inflateReset2(&png_ptr->zstream, window_bits);
398# endif
399 }
400
401 else
402 {
John Bowler9afb90f2013-05-08 14:21:46 -0500403# if PNG_ZLIB_VERNUM < 0x1240
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600404 ret = inflateInit(&png_ptr->zstream);
405# else
406 ret = inflateInit2(&png_ptr->zstream, window_bits);
407# endif
408
409 if (ret == Z_OK)
410 png_ptr->flags |= PNG_FLAG_ZSTREAM_INITIALIZED;
John Bowler42a2b552012-03-05 20:57:40 -0600411 }
412
413 if (ret == Z_OK)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600414 png_ptr->zowner = owner;
John Bowler42a2b552012-03-05 20:57:40 -0600415
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600416 else
417 png_zstream_error(png_ptr, ret);
418
419 return ret;
420 }
John Bowler0c7ac062013-05-07 21:59:05 -0500421
422# ifdef window_bits
423# undef window_bits
424# endif
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600425}
426
427#ifdef PNG_READ_COMPRESSED_TEXT_SUPPORTED
428/* png_inflate now returns zlib error codes including Z_OK and Z_STREAM_END to
429 * allow the caller to do multiple calls if required. If the 'finish' flag is
430 * set Z_FINISH will be passed to the final inflate() call and Z_STREAM_END must
431 * be returned or there has been a problem, otherwise Z_SYNC_FLUSH is used and
432 * Z_OK or Z_STREAM_END will be returned on success.
433 *
434 * The input and output sizes are updated to the actual amounts of data consumed
435 * or written, not the amount available (as in a z_stream). The data pointers
436 * are not changed, so the next input is (data+input_size) and the next
437 * available output is (output+output_size).
438 */
439static int
440png_inflate(png_structrp png_ptr, png_uint_32 owner, int finish,
441 /* INPUT: */ png_const_bytep input, png_uint_32p input_size_ptr,
442 /* OUTPUT: */ png_bytep output, png_alloc_size_t *output_size_ptr)
443{
444 if (png_ptr->zowner == owner) /* Else not claimed */
445 {
446 int ret;
447 png_alloc_size_t avail_out = *output_size_ptr;
448 png_uint_32 avail_in = *input_size_ptr;
449
450 /* zlib can't necessarily handle more than 65535 bytes at once (i.e. it
451 * can't even necessarily handle 65536 bytes) because the type uInt is
452 * "16 bits or more". Consequently it is necessary to chunk the input to
453 * zlib. This code uses ZLIB_IO_MAX, from pngpriv.h, as the maximum (the
454 * maximum value that can be stored in a uInt.) It is possible to set
455 * ZLIB_IO_MAX to a lower value in pngpriv.h and this may sometimes have
456 * a performance advantage, because it reduces the amount of data accessed
457 * at each step and that may give the OS more time to page it in.
John Bowlerb5d00512012-03-09 09:15:18 -0600458 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600459 png_ptr->zstream.next_in = PNGZ_INPUT_CAST(input);
460 /* avail_in and avail_out are set below from 'size' */
John Bowlerb5d00512012-03-09 09:15:18 -0600461 png_ptr->zstream.avail_in = 0;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600462 png_ptr->zstream.avail_out = 0;
John Bowlerb5d00512012-03-09 09:15:18 -0600463
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600464 /* Read directly into the output if it is available (this is set to
465 * a local buffer below if output is NULL).
John Bowlerb5d00512012-03-09 09:15:18 -0600466 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600467 if (output != NULL)
468 png_ptr->zstream.next_out = output;
469
470 do
John Bowlerb5d00512012-03-09 09:15:18 -0600471 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600472 uInt avail;
473 Byte local_buffer[PNG_INFLATE_BUF_SIZE];
John Bowlerb5d00512012-03-09 09:15:18 -0600474
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600475 /* zlib INPUT BUFFER */
476 /* The setting of 'avail_in' used to be outside the loop; by setting it
477 * inside it is possible to chunk the input to zlib and simply rely on
478 * zlib to advance the 'next_in' pointer. This allows arbitrary
479 * amounts of data to be passed through zlib at the unavoidable cost of
480 * requiring a window save (memcpy of up to 32768 output bytes)
481 * every ZLIB_IO_MAX input bytes.
482 */
483 avail_in += png_ptr->zstream.avail_in; /* not consumed last time */
John Bowlerb5d00512012-03-09 09:15:18 -0600484
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600485 avail = ZLIB_IO_MAX;
486
487 if (avail_in < avail)
488 avail = (uInt)avail_in; /* safe: < than ZLIB_IO_MAX */
489
490 avail_in -= avail;
491 png_ptr->zstream.avail_in = avail;
492
493 /* zlib OUTPUT BUFFER */
494 avail_out += png_ptr->zstream.avail_out; /* not written last time */
495
496 avail = ZLIB_IO_MAX; /* maximum zlib can process */
497
498 if (output == NULL)
John Bowlerb5d00512012-03-09 09:15:18 -0600499 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600500 /* Reset the output buffer each time round if output is NULL and
501 * make available the full buffer, up to 'remaining_space'
502 */
503 png_ptr->zstream.next_out = local_buffer;
504 if ((sizeof local_buffer) < avail)
505 avail = (sizeof local_buffer);
John Bowlerb5d00512012-03-09 09:15:18 -0600506 }
507
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600508 if (avail_out < avail)
509 avail = (uInt)avail_out; /* safe: < ZLIB_IO_MAX */
John Bowlerb5d00512012-03-09 09:15:18 -0600510
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600511 png_ptr->zstream.avail_out = avail;
512 avail_out -= avail;
513
514 /* zlib inflate call */
515 /* In fact 'avail_out' may be 0 at this point, that happens at the end
516 * of the read when the final LZ end code was not passed at the end of
517 * the previous chunk of input data. Tell zlib if we have reached the
518 * end of the output buffer.
519 */
520 ret = inflate(&png_ptr->zstream, avail_out > 0 ? Z_NO_FLUSH :
521 (finish ? Z_FINISH : Z_SYNC_FLUSH));
522 } while (ret == Z_OK);
523
524 /* For safety kill the local buffer pointer now */
525 if (output == NULL)
526 png_ptr->zstream.next_out = NULL;
527
528 /* Claw back the 'size' and 'remaining_space' byte counts. */
529 avail_in += png_ptr->zstream.avail_in;
530 avail_out += png_ptr->zstream.avail_out;
531
532 /* Update the input and output sizes; the updated values are the amount
533 * consumed or written, effectively the inverse of what zlib uses.
John Bowlerb5d00512012-03-09 09:15:18 -0600534 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600535 if (avail_out > 0)
536 *output_size_ptr -= avail_out;
537
538 if (avail_in > 0)
539 *input_size_ptr -= avail_in;
540
541 /* Ensure png_ptr->zstream.msg is set (even in the success case!) */
542 png_zstream_error(png_ptr, ret);
543 return ret;
544 }
545
546 else
547 {
548 /* This is a bad internal error. The recovery assigns to the zstream msg
549 * pointer, which is not owned by the caller, but this is safe; it's only
550 * used on errors!
551 */
552 png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed");
553 return Z_STREAM_ERROR;
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600554 }
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600555}
556
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600557/*
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600558 * Decompress trailing data in a chunk. The assumption is that read_buffer
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600559 * points at an allocated area holding the contents of a chunk with a
560 * trailing compressed part. What we get back is an allocated area
561 * holding the original prefix part and an uncompressed version of the
562 * trailing part (the malloc area passed in is freed).
563 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600564static int
565png_decompress_chunk(png_structrp png_ptr,
566 png_uint_32 chunklength, png_uint_32 prefix_size,
567 png_alloc_size_t *newlength /* must be initialized to the maximum! */,
568 int terminate /*add a '\0' to the end of the uncompressed data*/)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600569{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600570 /* TODO: implement different limits for different types of chunk.
571 *
572 * The caller supplies *newlength set to the maximum length of the
573 * uncompressed data, but this routine allocates space for the prefix and
574 * maybe a '\0' terminator too. We have to assume that 'prefix_size' is
575 * limited only by the maximum chunk size.
576 */
577 png_alloc_size_t limit = PNG_SIZE_MAX;
Glenn Randers-Pehrson3744f942012-08-10 19:22:53 -0500578
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600579# ifdef PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED
580 if (png_ptr->user_chunk_malloc_max > 0 &&
581 png_ptr->user_chunk_malloc_max < limit)
582 limit = png_ptr->user_chunk_malloc_max;
583# elif PNG_USER_CHUNK_MALLOC_MAX > 0
584 if (PNG_USER_CHUNK_MALLOC_MAX < limit)
585 limit = PNG_USER_CHUNK_MALLOC_MAX;
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -0600586# endif
John Bowlerb5d00512012-03-09 09:15:18 -0600587
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600588 if (limit >= prefix_size + (terminate != 0))
589 {
590 int ret;
591
592 limit -= prefix_size + (terminate != 0);
593
594 if (limit < *newlength)
595 *newlength = limit;
596
John Bowler0c7ac062013-05-07 21:59:05 -0500597 /* Now try to claim the stream. */
598 ret = png_inflate_claim(png_ptr, png_ptr->chunk_name);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600599
600 if (ret == Z_OK)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600601 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600602 png_uint_32 lzsize = chunklength - prefix_size;
John Bowlerb5d00512012-03-09 09:15:18 -0600603
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600604 ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/,
605 /* input: */ png_ptr->read_buffer + prefix_size, &lzsize,
606 /* output: */ NULL, newlength);
607
608 if (ret == Z_STREAM_END)
John Bowlerb5d00512012-03-09 09:15:18 -0600609 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600610 /* Use 'inflateReset' here, not 'inflateReset2' because this
611 * preserves the previously decided window size (otherwise it would
612 * be necessary to store the previous window size.) In practice
613 * this doesn't matter anyway, because png_inflate will call inflate
614 * with Z_FINISH in almost all cases, so the window will not be
615 * maintained.
616 */
617 if (inflateReset(&png_ptr->zstream) == Z_OK)
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600618 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600619 /* Because of the limit checks above we know that the new,
620 * expanded, size will fit in a size_t (let alone an
621 * png_alloc_size_t). Use png_malloc_base here to avoid an
622 * extra OOM message.
623 */
624 png_alloc_size_t new_size = *newlength;
625 png_alloc_size_t buffer_size = prefix_size + new_size +
626 (terminate != 0);
627 png_bytep text = png_voidcast(png_bytep, png_malloc_base(png_ptr,
628 buffer_size));
629
630 if (text != NULL)
631 {
632 ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/,
633 png_ptr->read_buffer + prefix_size, &lzsize,
634 text + prefix_size, newlength);
635
636 if (ret == Z_STREAM_END)
637 {
638 if (new_size == *newlength)
639 {
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -0600640 if (terminate != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600641 text[prefix_size + *newlength] = 0;
642
643 if (prefix_size > 0)
644 memcpy(text, png_ptr->read_buffer, prefix_size);
645
646 {
647 png_bytep old_ptr = png_ptr->read_buffer;
648
649 png_ptr->read_buffer = text;
650 png_ptr->read_buffer_size = buffer_size;
651 text = old_ptr; /* freed below */
652 }
653 }
654
655 else
656 {
657 /* The size changed on the second read, there can be no
658 * guarantee that anything is correct at this point.
659 * The 'msg' pointer has been set to "unexpected end of
660 * LZ stream", which is fine, but return an error code
661 * that the caller won't accept.
662 */
663 ret = PNG_UNEXPECTED_ZLIB_RETURN;
664 }
665 }
666
667 else if (ret == Z_OK)
668 ret = PNG_UNEXPECTED_ZLIB_RETURN; /* for safety */
669
670 /* Free the text pointer (this is the old read_buffer on
671 * success)
672 */
673 png_free(png_ptr, text);
674
675 /* This really is very benign, but it's still an error because
676 * the extra space may otherwise be used as a Trojan Horse.
677 */
678 if (ret == Z_STREAM_END &&
679 chunklength - prefix_size != lzsize)
680 png_chunk_benign_error(png_ptr, "extra compressed data");
681 }
682
683 else
684 {
685 /* Out of memory allocating the buffer */
686 ret = Z_MEM_ERROR;
687 png_zstream_error(png_ptr, Z_MEM_ERROR);
688 }
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600689 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600690
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600691 else
692 {
693 /* inflateReset failed, store the error message */
694 png_zstream_error(png_ptr, ret);
695
696 if (ret == Z_STREAM_END)
697 ret = PNG_UNEXPECTED_ZLIB_RETURN;
698 }
John Bowlerb5d00512012-03-09 09:15:18 -0600699 }
700
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600701 else if (ret == Z_OK)
702 ret = PNG_UNEXPECTED_ZLIB_RETURN;
703
704 /* Release the claimed stream */
705 png_ptr->zowner = 0;
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600706 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600707
708 else /* the claim failed */ if (ret == Z_STREAM_END) /* impossible! */
709 ret = PNG_UNEXPECTED_ZLIB_RETURN;
710
711 return ret;
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600712 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600713
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600714 else
John Bowlerb5d00512012-03-09 09:15:18 -0600715 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600716 /* Application/configuration limits exceeded */
717 png_zstream_error(png_ptr, Z_MEM_ERROR);
718 return Z_MEM_ERROR;
John Bowlerb5d00512012-03-09 09:15:18 -0600719 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600720}
Glenn Randers-Pehrson5975f312011-04-01 13:15:36 -0500721#endif /* PNG_READ_COMPRESSED_TEXT_SUPPORTED */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600722
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600723#ifdef PNG_READ_iCCP_SUPPORTED
724/* Perform a partial read and decompress, producing 'avail_out' bytes and
725 * reading from the current chunk as required.
726 */
727static int
728png_inflate_read(png_structrp png_ptr, png_bytep read_buffer, uInt read_size,
729 png_uint_32p chunk_bytes, png_bytep next_out, png_alloc_size_t *out_size,
730 int finish)
731{
732 if (png_ptr->zowner == png_ptr->chunk_name)
733 {
734 int ret;
735
736 /* next_in and avail_in must have been initialized by the caller. */
737 png_ptr->zstream.next_out = next_out;
738 png_ptr->zstream.avail_out = 0; /* set in the loop */
739
740 do
741 {
742 if (png_ptr->zstream.avail_in == 0)
743 {
744 if (read_size > *chunk_bytes)
745 read_size = (uInt)*chunk_bytes;
746 *chunk_bytes -= read_size;
747
748 if (read_size > 0)
749 png_crc_read(png_ptr, read_buffer, read_size);
750
751 png_ptr->zstream.next_in = read_buffer;
752 png_ptr->zstream.avail_in = read_size;
753 }
754
755 if (png_ptr->zstream.avail_out == 0)
756 {
757 uInt avail = ZLIB_IO_MAX;
758 if (avail > *out_size)
759 avail = (uInt)*out_size;
760 *out_size -= avail;
761
762 png_ptr->zstream.avail_out = avail;
763 }
764
765 /* Use Z_SYNC_FLUSH when there is no more chunk data to ensure that all
766 * the available output is produced; this allows reading of truncated
767 * streams.
768 */
769 ret = inflate(&png_ptr->zstream,
770 *chunk_bytes > 0 ? Z_NO_FLUSH : (finish ? Z_FINISH : Z_SYNC_FLUSH));
771 }
772 while (ret == Z_OK && (*out_size > 0 || png_ptr->zstream.avail_out > 0));
773
774 *out_size += png_ptr->zstream.avail_out;
775 png_ptr->zstream.avail_out = 0; /* Should not be required, but is safe */
776
777 /* Ensure the error message pointer is always set: */
778 png_zstream_error(png_ptr, ret);
779 return ret;
780 }
781
782 else
783 {
784 png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed");
785 return Z_STREAM_ERROR;
786 }
787}
788#endif
789
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500790/* Read and check the IDHR chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500791void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600792png_handle_IHDR(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500793{
794 png_byte buf[13];
795 png_uint_32 width, height;
796 int bit_depth, color_type, compression_type, filter_type;
797 int interlace_type;
798
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500799 png_debug(1, "in png_handle_IHDR");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500800
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600801 if (png_ptr->mode & PNG_HAVE_IHDR)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600802 png_chunk_error(png_ptr, "out of place");
Guy Schalnate5a37791996-06-05 15:50:50 -0500803
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500804 /* Check the length */
Guy Schalnat0d580581995-07-20 02:43:20 -0500805 if (length != 13)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600806 png_chunk_error(png_ptr, "invalid");
Guy Schalnat0d580581995-07-20 02:43:20 -0500807
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600808 png_ptr->mode |= PNG_HAVE_IHDR;
809
Guy Schalnat0d580581995-07-20 02:43:20 -0500810 png_crc_read(png_ptr, buf, 13);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600811 png_crc_finish(png_ptr, 0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500812
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500813 width = png_get_uint_31(png_ptr, buf);
814 height = png_get_uint_31(png_ptr, buf + 4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500815 bit_depth = buf[8];
816 color_type = buf[9];
817 compression_type = buf[10];
818 filter_type = buf[11];
819 interlace_type = buf[12];
820
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500821 /* Set internal variables */
Guy Schalnat0d580581995-07-20 02:43:20 -0500822 png_ptr->width = width;
823 png_ptr->height = height;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600824 png_ptr->bit_depth = (png_byte)bit_depth;
825 png_ptr->interlaced = (png_byte)interlace_type;
826 png_ptr->color_type = (png_byte)color_type;
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500827#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600828 png_ptr->filter_type = (png_byte)filter_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500829#endif
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500830 png_ptr->compression_type = (png_byte)compression_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500831
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500832 /* Find number of channels */
Guy Schalnat0d580581995-07-20 02:43:20 -0500833 switch (png_ptr->color_type)
834 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600835 default: /* invalid, png_set_IHDR calls png_error */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500836 case PNG_COLOR_TYPE_GRAY:
837 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnat0d580581995-07-20 02:43:20 -0500838 png_ptr->channels = 1;
839 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500840
Andreas Dilger47a0c421997-05-16 02:46:07 -0500841 case PNG_COLOR_TYPE_RGB:
Guy Schalnat0d580581995-07-20 02:43:20 -0500842 png_ptr->channels = 3;
843 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500844
Andreas Dilger47a0c421997-05-16 02:46:07 -0500845 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnat0d580581995-07-20 02:43:20 -0500846 png_ptr->channels = 2;
847 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500848
Andreas Dilger47a0c421997-05-16 02:46:07 -0500849 case PNG_COLOR_TYPE_RGB_ALPHA:
Guy Schalnat0d580581995-07-20 02:43:20 -0500850 png_ptr->channels = 4;
851 break;
852 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600853
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500854 /* Set up other useful info */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600855 png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth *
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600856 png_ptr->channels);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500857 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width);
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500858 png_debug1(3, "bit_depth = %d", png_ptr->bit_depth);
859 png_debug1(3, "channels = %d", png_ptr->channels);
Glenn Randers-Pehrsonb764c602011-01-14 21:18:37 -0600860 png_debug1(3, "rowbytes = %lu", (unsigned long)png_ptr->rowbytes);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500861 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600862 color_type, interlace_type, compression_type, filter_type);
Guy Schalnat0d580581995-07-20 02:43:20 -0500863}
864
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500865/* Read and check the palette */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500866void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600867png_handle_PLTE(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500868{
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600869 png_color palette[PNG_MAX_PALETTE_LENGTH];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600870 int num, i;
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500871#ifdef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500872 png_colorp pal_ptr;
873#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500874
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500875 png_debug(1, "in png_handle_PLTE");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500876
Guy Schalnate5a37791996-06-05 15:50:50 -0500877 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600878 png_chunk_error(png_ptr, "missing IHDR");
879
880 /* Moved to before the 'after IDAT' check below because otherwise duplicate
881 * PLTE chunks are potentially ignored (the spec says there shall not be more
882 * than one PLTE, the error is not treated as benign, so this check trumps
883 * the requirement that PLTE appears before IDAT.)
884 */
885 else if (png_ptr->mode & PNG_HAVE_PLTE)
886 png_chunk_error(png_ptr, "duplicate");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500887
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600888 else if (png_ptr->mode & PNG_HAVE_IDAT)
889 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600890 /* This is benign because the non-benign error happened before, when an
891 * IDAT was encountered in a color-mapped image with no PLTE.
892 */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600893 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600894 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600895 return;
896 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500897
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600898 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500899
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600900 if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR))
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500901 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500902 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600903 png_chunk_benign_error(png_ptr, "ignored in grayscale PNG");
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500904 return;
905 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600906
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -0500907#ifndef PNG_READ_OPT_PLTE_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500908 if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
909 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600910 png_crc_finish(png_ptr, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500911 return;
912 }
913#endif
914
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600915 if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3)
Guy Schalnate5a37791996-06-05 15:50:50 -0500916 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600917 png_crc_finish(png_ptr, length);
918
Guy Schalnate5a37791996-06-05 15:50:50 -0500919 if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600920 png_chunk_benign_error(png_ptr, "invalid");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500921
Guy Schalnate5a37791996-06-05 15:50:50 -0500922 else
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600923 png_chunk_error(png_ptr, "invalid");
924
925 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500926 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500927
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600928 /* The cast is safe because 'length' is less than 3*PNG_MAX_PALETTE_LENGTH */
Guy Schalnat0d580581995-07-20 02:43:20 -0500929 num = (int)length / 3;
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500930
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500931#ifdef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500932 for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)
933 {
934 png_byte buf[3];
935
936 png_crc_read(png_ptr, buf, 3);
937 pal_ptr->red = buf[0];
938 pal_ptr->green = buf[1];
939 pal_ptr->blue = buf[2];
940 }
941#else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600942 for (i = 0; i < num; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500943 {
944 png_byte buf[3];
945
946 png_crc_read(png_ptr, buf, 3);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500947 /* Don't depend upon png_color being any order */
Guy Schalnat0d580581995-07-20 02:43:20 -0500948 palette[i].red = buf[0];
949 palette[i].green = buf[1];
950 palette[i].blue = buf[2];
951 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500952#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600953
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600954 /* 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 -0500955 * whatever the normal CRC configuration tells us. However, if we
956 * have an RGB image, the PLTE can be considered ancillary, so
957 * we will act as though it is.
958 */
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -0500959#ifndef PNG_READ_OPT_PLTE_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600960 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600961#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600962 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500963 png_crc_finish(png_ptr, 0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600964 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600965
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -0500966#ifndef PNG_READ_OPT_PLTE_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600967 else if (png_crc_error(png_ptr)) /* Only if we have a CRC error */
968 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600969 /* If we don't want to use the data from an ancillary chunk,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600970 * we have two options: an error abort, or a warning and we
971 * ignore the data in this chunk (which should be OK, since
972 * it's considered ancillary for a RGB or RGBA image).
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600973 *
974 * IMPLEMENTATION NOTE: this is only here because png_crc_finish uses the
975 * chunk type to determine whether to check the ancillary or the critical
976 * flags.
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600977 */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600978 if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE))
979 {
980 if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)
Glenn Randers-Pehrson45625ec2014-02-22 23:09:27 -0600981 return;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600982
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600983 else
Glenn Randers-Pehrson45625ec2014-02-22 23:09:27 -0600984 png_chunk_error(png_ptr, "CRC error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600985 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600986
Andreas Dilger47a0c421997-05-16 02:46:07 -0500987 /* Otherwise, we (optionally) emit a warning and use the chunk. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600988 else if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN))
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600989 png_chunk_warning(png_ptr, "CRC error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600990 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600991#endif
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500992
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600993 /* TODO: png_set_PLTE has the side effect of setting png_ptr->palette to its
994 * own copy of the palette. This has the side effect that when png_start_row
995 * is called (this happens after any call to png_read_update_info) the
996 * info_ptr palette gets changed. This is extremely unexpected and
997 * confusing.
998 *
999 * Fix this by not sharing the palette in this way.
1000 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001001 png_set_PLTE(png_ptr, info_ptr, palette, num);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001002
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001003 /* The three chunks, bKGD, hIST and tRNS *must* appear after PLTE and before
1004 * IDAT. Prior to 1.6.0 this was not checked; instead the code merely
1005 * checked the apparent validity of a tRNS chunk inserted before PLTE on a
1006 * palette PNG. 1.6.0 attempts to rigorously follow the standard and
1007 * therefore does a benign error if the erroneous condition is detected *and*
1008 * cancels the tRNS if the benign error returns. The alternative is to
1009 * amend the standard since it would be rather hypocritical of the standards
1010 * maintainers to ignore it.
1011 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001012#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001013 if (png_ptr->num_trans > 0 ||
1014 (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS) != 0))
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001015 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001016 /* Cancel this because otherwise it would be used if the transforms
1017 * require it. Don't cancel the 'valid' flag because this would prevent
1018 * detection of duplicate chunks.
1019 */
1020 png_ptr->num_trans = 0;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001021
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001022 if (info_ptr != NULL)
1023 info_ptr->num_trans = 0;
1024
1025 png_chunk_benign_error(png_ptr, "tRNS must be after");
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001026 }
1027#endif
1028
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001029#ifdef PNG_READ_hIST_SUPPORTED
1030 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) != 0)
1031 png_chunk_benign_error(png_ptr, "hIST must be after");
1032#endif
1033
1034#ifdef PNG_READ_bKGD_SUPPORTED
1035 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) != 0)
1036 png_chunk_benign_error(png_ptr, "bKGD must be after");
1037#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001038}
Guy Schalnate5a37791996-06-05 15:50:50 -05001039
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001040void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001041png_handle_IEND(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001042{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001043 png_debug(1, "in png_handle_IEND");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001044
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001045 if (!(png_ptr->mode & PNG_HAVE_IHDR) || !(png_ptr->mode & PNG_HAVE_IDAT))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001046 png_chunk_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001047
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001048 png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001049
Andreas Dilger47a0c421997-05-16 02:46:07 -05001050 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001051
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001052 if (length != 0)
1053 png_chunk_benign_error(png_ptr, "invalid");
1054
1055 PNG_UNUSED(info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001056}
1057
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001058#ifdef PNG_READ_gAMA_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001059void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001060png_handle_gAMA(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001061{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001062 png_fixed_point igamma;
Guy Schalnat0d580581995-07-20 02:43:20 -05001063 png_byte buf[4];
1064
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001065 png_debug(1, "in png_handle_gAMA");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001066
Guy Schalnate5a37791996-06-05 15:50:50 -05001067 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001068 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001069
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001070 else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001071 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001072 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001073 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001074 return;
1075 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001076
Guy Schalnat0d580581995-07-20 02:43:20 -05001077 if (length != 4)
1078 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001079 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001080 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnat0d580581995-07-20 02:43:20 -05001081 return;
1082 }
1083
1084 png_crc_read(png_ptr, buf, 4);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001085
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001086 if (png_crc_finish(png_ptr, 0))
1087 return;
1088
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001089 igamma = png_get_fixed_point(NULL, buf);
Glenn Randers-Pehrson33893092010-10-23 13:20:18 -05001090
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001091 png_colorspace_set_gamma(png_ptr, &png_ptr->colorspace, igamma);
1092 png_colorspace_sync(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001093}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001094#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001095
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001096#ifdef PNG_READ_sBIT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001097void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001098png_handle_sBIT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001099{
John Bowler414d7b52014-02-06 11:32:57 -06001100 unsigned int truelen, i;
1101 png_byte sample_depth;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001102 png_byte buf[4];
Guy Schalnat69b14481996-01-10 02:56:49 -06001103
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001104 png_debug(1, "in png_handle_sBIT");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001105
Guy Schalnate5a37791996-06-05 15:50:50 -05001106 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001107 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001108
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001109 else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001110 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001111 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001112 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001113 return;
1114 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001115
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001116 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001117 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001118 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001119 png_chunk_benign_error(png_ptr, "duplicate");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001120 return;
1121 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001122
Guy Schalnat0d580581995-07-20 02:43:20 -05001123 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
John Bowler414d7b52014-02-06 11:32:57 -06001124 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001125 truelen = 3;
John Bowler414d7b52014-02-06 11:32:57 -06001126 sample_depth = 8;
1127 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001128
Guy Schalnat0d580581995-07-20 02:43:20 -05001129 else
John Bowler414d7b52014-02-06 11:32:57 -06001130 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001131 truelen = png_ptr->channels;
John Bowler414d7b52014-02-06 11:32:57 -06001132 sample_depth = png_ptr->bit_depth;
1133 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001134
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001135 if (length != truelen || length > 4)
Guy Schalnat0d580581995-07-20 02:43:20 -05001136 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001137 png_chunk_benign_error(png_ptr, "invalid");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001138 png_crc_finish(png_ptr, length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001139 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001140 }
1141
John Bowler414d7b52014-02-06 11:32:57 -06001142 buf[0] = buf[1] = buf[2] = buf[3] = sample_depth;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001143 png_crc_read(png_ptr, buf, truelen);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001144
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001145 if (png_crc_finish(png_ptr, 0))
1146 return;
1147
John Bowler414d7b52014-02-06 11:32:57 -06001148 for (i=0; i<truelen; ++i)
1149 if (buf[i] == 0 || buf[i] > sample_depth)
1150 {
1151 png_chunk_benign_error(png_ptr, "invalid");
1152 return;
1153 }
1154
Guy Schalnat0d580581995-07-20 02:43:20 -05001155 if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
1156 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001157 png_ptr->sig_bit.red = buf[0];
1158 png_ptr->sig_bit.green = buf[1];
1159 png_ptr->sig_bit.blue = buf[2];
1160 png_ptr->sig_bit.alpha = buf[3];
Guy Schalnat0d580581995-07-20 02:43:20 -05001161 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001162
Guy Schalnat0d580581995-07-20 02:43:20 -05001163 else
1164 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001165 png_ptr->sig_bit.gray = buf[0];
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001166 png_ptr->sig_bit.red = buf[0];
1167 png_ptr->sig_bit.green = buf[0];
1168 png_ptr->sig_bit.blue = buf[0];
Guy Schalnat6d764711995-12-19 03:22:19 -06001169 png_ptr->sig_bit.alpha = buf[1];
Guy Schalnat0d580581995-07-20 02:43:20 -05001170 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001171
Andreas Dilger47a0c421997-05-16 02:46:07 -05001172 png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
Guy Schalnat0d580581995-07-20 02:43:20 -05001173}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001174#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001175
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001176#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001177void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001178png_handle_cHRM(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001179{
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001180 png_byte buf[32];
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001181 png_xy xy;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -06001182
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001183 png_debug(1, "in png_handle_cHRM");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001184
Guy Schalnate5a37791996-06-05 15:50:50 -05001185 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001186 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001187
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001188 else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001189 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001190 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001191 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001192 return;
1193 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001194
Guy Schalnat0d580581995-07-20 02:43:20 -05001195 if (length != 32)
1196 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001197 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001198 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001199 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001200 }
1201
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001202 png_crc_read(png_ptr, buf, 32);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001203
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001204 if (png_crc_finish(png_ptr, 0))
1205 return;
1206
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001207 xy.whitex = png_get_fixed_point(NULL, buf);
1208 xy.whitey = png_get_fixed_point(NULL, buf + 4);
1209 xy.redx = png_get_fixed_point(NULL, buf + 8);
1210 xy.redy = png_get_fixed_point(NULL, buf + 12);
1211 xy.greenx = png_get_fixed_point(NULL, buf + 16);
1212 xy.greeny = png_get_fixed_point(NULL, buf + 20);
1213 xy.bluex = png_get_fixed_point(NULL, buf + 24);
1214 xy.bluey = png_get_fixed_point(NULL, buf + 28);
Glenn Randers-Pehrson33893092010-10-23 13:20:18 -05001215
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001216 if (xy.whitex == PNG_FIXED_ERROR ||
1217 xy.whitey == PNG_FIXED_ERROR ||
1218 xy.redx == PNG_FIXED_ERROR ||
1219 xy.redy == PNG_FIXED_ERROR ||
1220 xy.greenx == PNG_FIXED_ERROR ||
1221 xy.greeny == PNG_FIXED_ERROR ||
1222 xy.bluex == PNG_FIXED_ERROR ||
1223 xy.bluey == PNG_FIXED_ERROR)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001224 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001225 png_chunk_benign_error(png_ptr, "invalid values");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001226 return;
1227 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001228
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001229 /* If a colorspace error has already been output skip this chunk */
1230 if (png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID)
1231 return;
1232
1233 if (png_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001234 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001235 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1236 png_colorspace_sync(png_ptr, info_ptr);
1237 png_chunk_benign_error(png_ptr, "duplicate");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001238 return;
1239 }
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001240
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001241 png_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM;
1242 (void)png_colorspace_set_chromaticities(png_ptr, &png_ptr->colorspace, &xy,
1243 1/*prefer cHRM values*/);
1244 png_colorspace_sync(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001245}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001246#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001247
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001248#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001249void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001250png_handle_sRGB(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001251{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001252 png_byte intent;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001253
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001254 png_debug(1, "in png_handle_sRGB");
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001255
1256 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001257 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001258
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001259 else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE))
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001260 {
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001261 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001262 png_chunk_benign_error(png_ptr, "out of place");
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001263 return;
1264 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001265
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001266 if (length != 1)
1267 {
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001268 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001269 png_chunk_benign_error(png_ptr, "invalid");
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001270 return;
1271 }
1272
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001273 png_crc_read(png_ptr, &intent, 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001274
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001275 if (png_crc_finish(png_ptr, 0))
1276 return;
1277
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001278 /* If a colorspace error has already been output skip this chunk */
1279 if (png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID)
1280 return;
John Bowler88b77cc2011-05-05 06:49:55 -05001281
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001282 /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect
1283 * this.
1284 */
1285 if (png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT)
John Bowlerb11b31a2012-03-21 07:55:46 -05001286 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001287 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1288 png_colorspace_sync(png_ptr, info_ptr);
1289 png_chunk_benign_error(png_ptr, "too many profiles");
John Bowlerb11b31a2012-03-21 07:55:46 -05001290 return;
1291 }
John Bowler736f40f2011-08-25 16:19:44 -05001292
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001293 (void)png_colorspace_set_sRGB(png_ptr, &png_ptr->colorspace, intent);
1294 png_colorspace_sync(png_ptr, info_ptr);
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06001295}
1296#endif /* PNG_READ_sRGB_SUPPORTED */
1297
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001298#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001299void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001300png_handle_iCCP(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001301/* Note: this does not properly handle profiles that are > 64K under DOS */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001302{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001303 png_const_charp errmsg = NULL; /* error message output, or no error */
1304 int finished = 0; /* crc checked */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001305
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001306 png_debug(1, "in png_handle_iCCP");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001307
1308 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001309 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001310
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001311 else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001312 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001313 png_crc_finish(png_ptr, length);
1314 png_chunk_benign_error(png_ptr, "out of place");
1315 return;
1316 }
1317
1318 /* Consistent with all the above colorspace handling an obviously *invalid*
1319 * chunk is just ignored, so does not invalidate the color space. An
1320 * alternative is to set the 'invalid' flags at the start of this routine
1321 * and only clear them in they were not set before and all the tests pass.
1322 * The minimum 'deflate' stream is assumed to be just the 2 byte header and 4
1323 * byte checksum. The keyword must be one character and there is a
1324 * terminator (0) byte and the compression method.
1325 */
1326 if (length < 9)
1327 {
1328 png_crc_finish(png_ptr, length);
1329 png_chunk_benign_error(png_ptr, "too short");
1330 return;
1331 }
1332
1333 /* If a colorspace error has already been output skip this chunk */
1334 if (png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID)
1335 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001336 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001337 return;
1338 }
1339
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001340 /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect
1341 * this.
1342 */
1343 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) == 0)
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06001344 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001345 uInt read_length, keyword_length;
1346 char keyword[81];
1347
1348 /* Find the keyword; the keyword plus separator and compression method
1349 * bytes can be at most 81 characters long.
1350 */
1351 read_length = 81; /* maximum */
1352 if (read_length > length)
1353 read_length = (uInt)length;
1354
1355 png_crc_read(png_ptr, (png_bytep)keyword, read_length);
1356 length -= read_length;
1357
1358 keyword_length = 0;
1359 while (keyword_length < 80 && keyword_length < read_length &&
1360 keyword[keyword_length] != 0)
1361 ++keyword_length;
1362
1363 /* TODO: make the keyword checking common */
1364 if (keyword_length >= 1 && keyword_length <= 79)
1365 {
1366 /* We only understand '0' compression - deflate - so if we get a
1367 * different value we can't safely decode the chunk.
1368 */
1369 if (keyword_length+1 < read_length &&
1370 keyword[keyword_length+1] == PNG_COMPRESSION_TYPE_BASE)
1371 {
1372 read_length -= keyword_length+2;
1373
John Bowler0c7ac062013-05-07 21:59:05 -05001374 if (png_inflate_claim(png_ptr, png_iCCP) == Z_OK)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001375 {
1376 Byte profile_header[132];
1377 Byte local_buffer[PNG_INFLATE_BUF_SIZE];
1378 png_alloc_size_t size = (sizeof profile_header);
1379
1380 png_ptr->zstream.next_in = (Bytef*)keyword + (keyword_length+2);
1381 png_ptr->zstream.avail_in = read_length;
1382 (void)png_inflate_read(png_ptr, local_buffer,
1383 (sizeof local_buffer), &length, profile_header, &size,
1384 0/*finish: don't, because the output is too small*/);
1385
1386 if (size == 0)
1387 {
1388 /* We have the ICC profile header; do the basic header checks.
1389 */
1390 const png_uint_32 profile_length =
1391 png_get_uint_32(profile_header);
1392
1393 if (png_icc_check_length(png_ptr, &png_ptr->colorspace,
1394 keyword, profile_length))
1395 {
1396 /* The length is apparently ok, so we can check the 132
1397 * byte header.
1398 */
1399 if (png_icc_check_header(png_ptr, &png_ptr->colorspace,
1400 keyword, profile_length, profile_header,
1401 png_ptr->color_type))
1402 {
1403 /* Now read the tag table; a variable size buffer is
1404 * needed at this point, allocate one for the whole
1405 * profile. The header check has already validated
1406 * that none of these stuff will overflow.
1407 */
1408 const png_uint_32 tag_count = png_get_uint_32(
1409 profile_header+128);
1410 png_bytep profile = png_read_buffer(png_ptr,
1411 profile_length, 2/*silent*/);
1412
1413 if (profile != NULL)
1414 {
1415 memcpy(profile, profile_header,
1416 (sizeof profile_header));
1417
1418 size = 12 * tag_count;
1419
1420 (void)png_inflate_read(png_ptr, local_buffer,
1421 (sizeof local_buffer), &length,
1422 profile + (sizeof profile_header), &size, 0);
1423
Glenn Randers-Pehrson9c5a1ba2014-02-17 09:12:52 -06001424 /* Still expect a buffer error because we expect
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001425 * there to be some tag data!
1426 */
1427 if (size == 0)
1428 {
1429 if (png_icc_check_tag_table(png_ptr,
1430 &png_ptr->colorspace, keyword, profile_length,
1431 profile))
1432 {
1433 /* The profile has been validated for basic
1434 * security issues, so read the whole thing in.
1435 */
1436 size = profile_length - (sizeof profile_header)
1437 - 12 * tag_count;
1438
1439 (void)png_inflate_read(png_ptr, local_buffer,
1440 (sizeof local_buffer), &length,
1441 profile + (sizeof profile_header) +
1442 12 * tag_count, &size, 1/*finish*/);
1443
1444 if (length > 0 && !(png_ptr->flags &
1445 PNG_FLAG_BENIGN_ERRORS_WARN))
1446 errmsg = "extra compressed data";
1447
1448 /* But otherwise allow extra data: */
1449 else if (size == 0)
1450 {
1451 if (length > 0)
1452 {
1453 /* This can be handled completely, so
1454 * keep going.
1455 */
1456 png_chunk_warning(png_ptr,
1457 "extra compressed data");
1458 }
1459
1460 png_crc_finish(png_ptr, length);
1461 finished = 1;
1462
1463# ifdef PNG_sRGB_SUPPORTED
1464 /* Check for a match against sRGB */
1465 png_icc_set_sRGB(png_ptr,
1466 &png_ptr->colorspace, profile,
1467 png_ptr->zstream.adler);
1468# endif
1469
1470 /* Steal the profile for info_ptr. */
1471 if (info_ptr != NULL)
1472 {
1473 png_free_data(png_ptr, info_ptr,
1474 PNG_FREE_ICCP, 0);
1475
1476 info_ptr->iccp_name = png_voidcast(char*,
1477 png_malloc_base(png_ptr,
1478 keyword_length+1));
1479 if (info_ptr->iccp_name != NULL)
1480 {
1481 memcpy(info_ptr->iccp_name, keyword,
1482 keyword_length+1);
1483 info_ptr->iccp_proflen =
1484 profile_length;
1485 info_ptr->iccp_profile = profile;
1486 png_ptr->read_buffer = NULL; /*steal*/
1487 info_ptr->free_me |= PNG_FREE_ICCP;
1488 info_ptr->valid |= PNG_INFO_iCCP;
1489 }
1490
1491 else
1492 {
1493 png_ptr->colorspace.flags |=
1494 PNG_COLORSPACE_INVALID;
1495 errmsg = "out of memory";
1496 }
1497 }
1498
1499 /* else the profile remains in the read
1500 * buffer which gets reused for subsequent
1501 * chunks.
1502 */
1503
1504 if (info_ptr != NULL)
1505 png_colorspace_sync(png_ptr, info_ptr);
1506
1507 if (errmsg == NULL)
1508 {
1509 png_ptr->zowner = 0;
1510 return;
1511 }
1512 }
1513
1514 else if (size > 0)
1515 errmsg = "truncated";
1516
1517 else
1518 errmsg = png_ptr->zstream.msg;
1519 }
1520
1521 /* else png_icc_check_tag_table output an error */
1522 }
1523
1524 else /* profile truncated */
1525 errmsg = png_ptr->zstream.msg;
1526 }
1527
1528 else
1529 errmsg = "out of memory";
1530 }
1531
1532 /* else png_icc_check_header output an error */
1533 }
1534
1535 /* else png_icc_check_length output an error */
1536 }
1537
1538 else /* profile truncated */
1539 errmsg = png_ptr->zstream.msg;
1540
1541 /* Release the stream */
1542 png_ptr->zowner = 0;
1543 }
1544
1545 else /* png_inflate_claim failed */
1546 errmsg = png_ptr->zstream.msg;
1547 }
1548
1549 else
1550 errmsg = "bad compression method"; /* or missing */
1551 }
1552
1553 else
1554 errmsg = "bad keyword";
1555 }
1556
1557 else
1558 errmsg = "too many profiles";
1559
1560 /* Failure: the reason is in 'errmsg' */
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06001561 if (finished == 0)
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06001562 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06001563
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001564 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1565 png_colorspace_sync(png_ptr, info_ptr);
1566 if (errmsg != NULL) /* else already output */
1567 png_chunk_benign_error(png_ptr, errmsg);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001568}
1569#endif /* PNG_READ_iCCP_SUPPORTED */
1570
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001571#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001572void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001573png_handle_sPLT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001574/* Note: this does not properly handle chunks that are > 64K under DOS */
1575{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001576 png_bytep entry_start, buffer;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001577 png_sPLT_t new_palette;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001578 png_sPLT_entryp pp;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001579 png_uint_32 data_length;
1580 int entry_size, i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001581 png_uint_32 skip = 0;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001582 png_uint_32 dl;
1583 png_size_t max_dl;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001584
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001585 png_debug(1, "in png_handle_sPLT");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001586
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001587#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001588 if (png_ptr->user_chunk_cache_max != 0)
1589 {
1590 if (png_ptr->user_chunk_cache_max == 1)
1591 {
1592 png_crc_finish(png_ptr, length);
1593 return;
1594 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001595
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001596 if (--png_ptr->user_chunk_cache_max == 1)
1597 {
1598 png_warning(png_ptr, "No space in chunk cache for sPLT");
1599 png_crc_finish(png_ptr, length);
1600 return;
1601 }
1602 }
1603#endif
1604
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001605 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001606 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001607
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001608 else if (png_ptr->mode & PNG_HAVE_IDAT)
1609 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001610 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001611 png_chunk_benign_error(png_ptr, "out of place");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001612 return;
1613 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001614
1615#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001616 if (length > 65535U)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001617 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001618 png_crc_finish(png_ptr, length);
1619 png_chunk_benign_error(png_ptr, "too large to fit in memory");
1620 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001621 }
1622#endif
1623
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001624 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
1625 if (buffer == NULL)
1626 {
1627 png_crc_finish(png_ptr, length);
1628 png_chunk_benign_error(png_ptr, "out of memory");
1629 return;
1630 }
1631
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001632
1633 /* WARNING: this may break if size_t is less than 32 bits; it is assumed
1634 * that the PNG_MAX_MALLOC_64K test is enabled in this case, but this is a
1635 * potential breakage point if the types in pngconf.h aren't exactly right.
1636 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001637 png_crc_read(png_ptr, buffer, length);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001638
1639 if (png_crc_finish(png_ptr, skip))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001640 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001641
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001642 buffer[length] = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001643
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001644 for (entry_start = buffer; *entry_start; entry_start++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001645 /* Empty loop to find end of name */ ;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001646
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001647 ++entry_start;
1648
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001649 /* A sample depth should follow the separator, and we should be on it */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001650 if (entry_start > buffer + length - 2)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001651 {
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -05001652 png_warning(png_ptr, "malformed sPLT chunk");
1653 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001654 }
1655
1656 new_palette.depth = *entry_start++;
Glenn Randers-Pehrsona565f0e2010-03-06 08:24:45 -06001657 entry_size = (new_palette.depth == 8 ? 6 : 10);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001658 /* This must fit in a png_uint_32 because it is derived from the original
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001659 * chunk data length.
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001660 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001661 data_length = length - (png_uint_32)(entry_start - buffer);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001662
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001663 /* Integrity-check the data length */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001664 if (data_length % entry_size)
1665 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001666 png_warning(png_ptr, "sPLT chunk has bad length");
1667 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001668 }
1669
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001670 dl = (png_int_32)(data_length / entry_size);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001671 max_dl = PNG_SIZE_MAX / (sizeof (png_sPLT_entry));
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001672
1673 if (dl > max_dl)
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001674 {
1675 png_warning(png_ptr, "sPLT chunk too long");
1676 return;
1677 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001678
1679 new_palette.nentries = (png_int_32)(data_length / entry_size);
1680
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001681 new_palette.entries = (png_sPLT_entryp)png_malloc_warn(
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001682 png_ptr, new_palette.nentries * (sizeof (png_sPLT_entry)));
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001683
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001684 if (new_palette.entries == NULL)
1685 {
1686 png_warning(png_ptr, "sPLT chunk requires too much memory");
1687 return;
1688 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001689
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001690#ifdef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001691 for (i = 0; i < new_palette.nentries; i++)
1692 {
Glenn Randers-Pehrson90b878c2009-10-07 12:44:35 -05001693 pp = new_palette.entries + i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001694
1695 if (new_palette.depth == 8)
1696 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001697 pp->red = *entry_start++;
1698 pp->green = *entry_start++;
1699 pp->blue = *entry_start++;
1700 pp->alpha = *entry_start++;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001701 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001702
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001703 else
1704 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001705 pp->red = png_get_uint_16(entry_start); entry_start += 2;
1706 pp->green = png_get_uint_16(entry_start); entry_start += 2;
1707 pp->blue = png_get_uint_16(entry_start); entry_start += 2;
1708 pp->alpha = png_get_uint_16(entry_start); entry_start += 2;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001709 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001710
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001711 pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
1712 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001713#else
1714 pp = new_palette.entries;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001715
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001716 for (i = 0; i < new_palette.nentries; i++)
1717 {
1718
1719 if (new_palette.depth == 8)
1720 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001721 pp[i].red = *entry_start++;
1722 pp[i].green = *entry_start++;
1723 pp[i].blue = *entry_start++;
1724 pp[i].alpha = *entry_start++;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001725 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001726
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001727 else
1728 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001729 pp[i].red = png_get_uint_16(entry_start); entry_start += 2;
1730 pp[i].green = png_get_uint_16(entry_start); entry_start += 2;
1731 pp[i].blue = png_get_uint_16(entry_start); entry_start += 2;
1732 pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001733 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001734
Glenn Randers-Pehrsonf27592a2011-03-21 18:05:40 -05001735 pp[i].frequency = png_get_uint_16(entry_start); entry_start += 2;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001736 }
1737#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001738
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001739 /* Discard all chunk data except the name and stash that */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001740 new_palette.name = (png_charp)buffer;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001741
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001742 png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001743
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001744 png_free(png_ptr, new_palette.entries);
1745}
1746#endif /* PNG_READ_sPLT_SUPPORTED */
1747
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001748#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001749void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001750png_handle_tRNS(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001751{
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001752 png_byte readbuf[PNG_MAX_PALETTE_LENGTH];
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06001753
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001754 png_debug(1, "in png_handle_tRNS");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001755
Guy Schalnate5a37791996-06-05 15:50:50 -05001756 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001757 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001758
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001759 else if (png_ptr->mode & PNG_HAVE_IDAT)
1760 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001761 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001762 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001763 return;
1764 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001765
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001766 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001767 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001768 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001769 png_chunk_benign_error(png_ptr, "duplicate");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001770 return;
1771 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001772
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001773 if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
Guy Schalnat0d580581995-07-20 02:43:20 -05001774 {
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001775 png_byte buf[2];
Guy Schalnat0d580581995-07-20 02:43:20 -05001776
1777 if (length != 2)
1778 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001779 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001780 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001781 return;
1782 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001783
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001784 png_crc_read(png_ptr, buf, 2);
1785 png_ptr->num_trans = 1;
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -05001786 png_ptr->trans_color.gray = png_get_uint_16(buf);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001787 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001788
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001789 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
1790 {
1791 png_byte buf[6];
1792
1793 if (length != 6)
1794 {
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001795 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001796 png_chunk_benign_error(png_ptr, "invalid");
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001797 return;
1798 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001799
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001800 png_crc_read(png_ptr, buf, length);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001801 png_ptr->num_trans = 1;
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -05001802 png_ptr->trans_color.red = png_get_uint_16(buf);
1803 png_ptr->trans_color.green = png_get_uint_16(buf + 2);
1804 png_ptr->trans_color.blue = png_get_uint_16(buf + 4);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001805 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001806
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001807 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1808 {
1809 if (!(png_ptr->mode & PNG_HAVE_PLTE))
1810 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001811 /* TODO: is this actually an error in the ISO spec? */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001812 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001813 png_chunk_benign_error(png_ptr, "out of place");
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001814 return;
1815 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001816
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001817 if (length > png_ptr->num_palette || length > PNG_MAX_PALETTE_LENGTH ||
1818 length == 0)
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06001819 {
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06001820 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001821 png_chunk_benign_error(png_ptr, "invalid");
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06001822 return;
1823 }
1824
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001825 png_crc_read(png_ptr, readbuf, length);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001826 png_ptr->num_trans = (png_uint_16)length;
1827 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001828
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001829 else
Guy Schalnate5a37791996-06-05 15:50:50 -05001830 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001831 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001832 png_chunk_benign_error(png_ptr, "invalid with alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -05001833 return;
1834 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001835
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001836 if (png_crc_finish(png_ptr, 0))
Glenn Randers-Pehrsona7dbcba2007-05-15 16:16:34 -05001837 {
1838 png_ptr->num_trans = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001839 return;
Glenn Randers-Pehrsona7dbcba2007-05-15 16:16:34 -05001840 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001841
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001842 /* TODO: this is a horrible side effect in the palette case because the
1843 * png_struct ends up with a pointer to the tRNS buffer owned by the
1844 * png_info. Fix this.
1845 */
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06001846 png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001847 &(png_ptr->trans_color));
Guy Schalnat0d580581995-07-20 02:43:20 -05001848}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001849#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001850
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001851#ifdef PNG_READ_bKGD_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001852void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001853png_handle_bKGD(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001854{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001855 unsigned int truelen;
Guy Schalnat0d580581995-07-20 02:43:20 -05001856 png_byte buf[6];
John Bowlercb0b2962011-05-12 21:48:29 -05001857 png_color_16 background;
Guy Schalnat0d580581995-07-20 02:43:20 -05001858
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001859 png_debug(1, "in png_handle_bKGD");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001860
Guy Schalnate5a37791996-06-05 15:50:50 -05001861 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001862 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001863
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001864 else if ((png_ptr->mode & PNG_HAVE_IDAT) ||
1865 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
1866 !(png_ptr->mode & PNG_HAVE_PLTE)))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001867 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001868 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001869 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001870 return;
1871 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001872
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001873 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001874 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001875 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001876 png_chunk_benign_error(png_ptr, "duplicate");
Guy Schalnate5a37791996-06-05 15:50:50 -05001877 return;
1878 }
1879
Guy Schalnat0d580581995-07-20 02:43:20 -05001880 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1881 truelen = 1;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001882
Guy Schalnat0d580581995-07-20 02:43:20 -05001883 else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
1884 truelen = 6;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001885
Guy Schalnat0d580581995-07-20 02:43:20 -05001886 else
1887 truelen = 2;
1888
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001889 if (length != truelen)
Guy Schalnat0d580581995-07-20 02:43:20 -05001890 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001891 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001892 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnat0d580581995-07-20 02:43:20 -05001893 return;
1894 }
1895
Andreas Dilger47a0c421997-05-16 02:46:07 -05001896 png_crc_read(png_ptr, buf, truelen);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001897
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001898 if (png_crc_finish(png_ptr, 0))
1899 return;
1900
Guy Schalnate5a37791996-06-05 15:50:50 -05001901 /* We convert the index value into RGB components so that we can allow
1902 * arbitrary RGB values for background when we have transparency, and
1903 * so it is easy to determine the RGB values of the background color
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001904 * from the info_ptr struct.
1905 */
Guy Schalnat0d580581995-07-20 02:43:20 -05001906 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Guy Schalnate5a37791996-06-05 15:50:50 -05001907 {
John Bowlercb0b2962011-05-12 21:48:29 -05001908 background.index = buf[0];
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001909
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001910 if (info_ptr && info_ptr->num_palette)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001911 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001912 if (buf[0] >= info_ptr->num_palette)
1913 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001914 png_chunk_benign_error(png_ptr, "invalid index");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001915 return;
1916 }
1917
John Bowlercb0b2962011-05-12 21:48:29 -05001918 background.red = (png_uint_16)png_ptr->palette[buf[0]].red;
1919 background.green = (png_uint_16)png_ptr->palette[buf[0]].green;
1920 background.blue = (png_uint_16)png_ptr->palette[buf[0]].blue;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001921 }
John Bowlercb0b2962011-05-12 21:48:29 -05001922
1923 else
1924 background.red = background.green = background.blue = 0;
1925
1926 background.gray = 0;
Guy Schalnate5a37791996-06-05 15:50:50 -05001927 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001928
Andreas Dilger47a0c421997-05-16 02:46:07 -05001929 else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) /* GRAY */
Guy Schalnate5a37791996-06-05 15:50:50 -05001930 {
John Bowlercb0b2962011-05-12 21:48:29 -05001931 background.index = 0;
1932 background.red =
1933 background.green =
1934 background.blue =
1935 background.gray = png_get_uint_16(buf);
Guy Schalnate5a37791996-06-05 15:50:50 -05001936 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001937
Guy Schalnat0d580581995-07-20 02:43:20 -05001938 else
1939 {
John Bowlercb0b2962011-05-12 21:48:29 -05001940 background.index = 0;
1941 background.red = png_get_uint_16(buf);
1942 background.green = png_get_uint_16(buf + 2);
1943 background.blue = png_get_uint_16(buf + 4);
1944 background.gray = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -05001945 }
1946
John Bowlercb0b2962011-05-12 21:48:29 -05001947 png_set_bKGD(png_ptr, info_ptr, &background);
Guy Schalnat0d580581995-07-20 02:43:20 -05001948}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001949#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001950
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001951#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001952void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001953png_handle_hIST(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001954{
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001955 unsigned int num, i;
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001956 png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];
Guy Schalnat0d580581995-07-20 02:43:20 -05001957
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001958 png_debug(1, "in png_handle_hIST");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001959
Guy Schalnate5a37791996-06-05 15:50:50 -05001960 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001961 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001962
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001963 else if ((png_ptr->mode & PNG_HAVE_IDAT) || !(png_ptr->mode & PNG_HAVE_PLTE))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001964 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001965 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001966 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001967 return;
1968 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001969
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001970 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001971 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001972 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001973 png_chunk_benign_error(png_ptr, "duplicate");
Guy Schalnate5a37791996-06-05 15:50:50 -05001974 return;
1975 }
1976
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001977 num = length / 2 ;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001978
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001979 if (num != png_ptr->num_palette || num > PNG_MAX_PALETTE_LENGTH)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001980 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001981 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001982 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001983 return;
1984 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001985
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001986 for (i = 0; i < num; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001987 {
1988 png_byte buf[2];
1989
1990 png_crc_read(png_ptr, buf, 2);
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06001991 readbuf[i] = png_get_uint_16(buf);
Guy Schalnat0d580581995-07-20 02:43:20 -05001992 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001993
1994 if (png_crc_finish(png_ptr, 0))
1995 return;
1996
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06001997 png_set_hIST(png_ptr, info_ptr, readbuf);
Guy Schalnat0d580581995-07-20 02:43:20 -05001998}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001999#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002000
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002001#ifdef PNG_READ_pHYs_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002002void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002003png_handle_pHYs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002004{
2005 png_byte buf[9];
2006 png_uint_32 res_x, res_y;
2007 int unit_type;
2008
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002009 png_debug(1, "in png_handle_pHYs");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002010
Guy Schalnate5a37791996-06-05 15:50:50 -05002011 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002012 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002013
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002014 else if (png_ptr->mode & PNG_HAVE_IDAT)
2015 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002016 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002017 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002018 return;
2019 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002020
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002021 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002022 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002023 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002024 png_chunk_benign_error(png_ptr, "duplicate");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002025 return;
2026 }
Guy Schalnate5a37791996-06-05 15:50:50 -05002027
Guy Schalnat0d580581995-07-20 02:43:20 -05002028 if (length != 9)
2029 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002030 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002031 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnat0d580581995-07-20 02:43:20 -05002032 return;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002033 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002034
2035 png_crc_read(png_ptr, buf, 9);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002036
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002037 if (png_crc_finish(png_ptr, 0))
2038 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05002039
2040 res_x = png_get_uint_32(buf);
2041 res_y = png_get_uint_32(buf + 4);
2042 unit_type = buf[8];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002043 png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
Guy Schalnat0d580581995-07-20 02:43:20 -05002044}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002045#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002046
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002047#ifdef PNG_READ_oFFs_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002048void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002049png_handle_oFFs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002050{
2051 png_byte buf[9];
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002052 png_int_32 offset_x, offset_y;
Guy Schalnat0d580581995-07-20 02:43:20 -05002053 int unit_type;
2054
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002055 png_debug(1, "in png_handle_oFFs");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002056
Guy Schalnate5a37791996-06-05 15:50:50 -05002057 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002058 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002059
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002060 else if (png_ptr->mode & PNG_HAVE_IDAT)
2061 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002062 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002063 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002064 return;
2065 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002066
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002067 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002068 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002069 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002070 png_chunk_benign_error(png_ptr, "duplicate");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002071 return;
2072 }
Guy Schalnate5a37791996-06-05 15:50:50 -05002073
Guy Schalnat0d580581995-07-20 02:43:20 -05002074 if (length != 9)
2075 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002076 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002077 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnat0d580581995-07-20 02:43:20 -05002078 return;
2079 }
2080
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002081 png_crc_read(png_ptr, buf, 9);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002082
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002083 if (png_crc_finish(png_ptr, 0))
2084 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05002085
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002086 offset_x = png_get_int_32(buf);
2087 offset_y = png_get_int_32(buf + 4);
Guy Schalnat0d580581995-07-20 02:43:20 -05002088 unit_type = buf[8];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002089 png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
2090}
2091#endif
2092
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002093#ifdef PNG_READ_pCAL_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002094/* Read the pCAL chunk (described in the PNG Extensions document) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002095void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002096png_handle_pCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002097{
Andreas Dilger47a0c421997-05-16 02:46:07 -05002098 png_int_32 X0, X1;
2099 png_byte type, nparams;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002100 png_bytep buffer, buf, units, endptr;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002101 png_charpp params;
2102 int i;
2103
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002104 png_debug(1, "in png_handle_pCAL");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002105
2106 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002107 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002108
Andreas Dilger47a0c421997-05-16 02:46:07 -05002109 else if (png_ptr->mode & PNG_HAVE_IDAT)
2110 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002111 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002112 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002113 return;
2114 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002115
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002116 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL))
Andreas Dilger47a0c421997-05-16 02:46:07 -05002117 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002118 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002119 png_chunk_benign_error(png_ptr, "duplicate");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002120 return;
2121 }
2122
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002123 png_debug1(2, "Allocating and reading pCAL chunk data (%u bytes)",
2124 length + 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002125
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002126 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
2127
2128 if (buffer == NULL)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002129 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002130 png_crc_finish(png_ptr, length);
2131 png_chunk_benign_error(png_ptr, "out of memory");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002132 return;
2133 }
2134
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002135 png_crc_read(png_ptr, buffer, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002136
2137 if (png_crc_finish(png_ptr, 0))
Andreas Dilger47a0c421997-05-16 02:46:07 -05002138 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002139
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002140 buffer[length] = 0; /* Null terminate the last string */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002141
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002142 png_debug(3, "Finding end of pCAL purpose string");
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002143 for (buf = buffer; *buf; buf++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002144 /* Empty loop */ ;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002145
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002146 endptr = buffer + length;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002147
2148 /* We need to have at least 12 bytes after the purpose string
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002149 * in order to get the parameter information.
2150 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002151 if (endptr <= buf + 12)
2152 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002153 png_chunk_benign_error(png_ptr, "invalid");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002154 return;
2155 }
2156
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002157 png_debug(3, "Reading pCAL X0, X1, type, nparams, and units");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002158 X0 = png_get_int_32((png_bytep)buf+1);
2159 X1 = png_get_int_32((png_bytep)buf+5);
2160 type = buf[9];
2161 nparams = buf[10];
2162 units = buf + 11;
2163
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002164 png_debug(3, "Checking pCAL equation type and number of parameters");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002165 /* Check that we have the right number of parameters for known
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002166 * equation types.
2167 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002168 if ((type == PNG_EQUATION_LINEAR && nparams != 2) ||
2169 (type == PNG_EQUATION_BASE_E && nparams != 3) ||
2170 (type == PNG_EQUATION_ARBITRARY && nparams != 3) ||
2171 (type == PNG_EQUATION_HYPERBOLIC && nparams != 4))
2172 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002173 png_chunk_benign_error(png_ptr, "invalid parameter count");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002174 return;
2175 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002176
Andreas Dilger47a0c421997-05-16 02:46:07 -05002177 else if (type >= PNG_EQUATION_LAST)
2178 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002179 png_chunk_benign_error(png_ptr, "unrecognized equation type");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002180 }
2181
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002182 for (buf = units; *buf; buf++)
Glenn Randers-Pehrsonf9f2fe01998-03-15 18:20:23 -06002183 /* Empty loop to move past the units string. */ ;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002184
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002185 png_debug(3, "Allocating pCAL parameters array");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002186
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002187 params = png_voidcast(png_charpp, png_malloc_warn(png_ptr,
2188 nparams * (sizeof (png_charp))));
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002189
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002190 if (params == NULL)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002191 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002192 png_chunk_benign_error(png_ptr, "out of memory");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002193 return;
2194 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002195
2196 /* Get pointers to the start of each parameter string. */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002197 for (i = 0; i < nparams; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002198 {
2199 buf++; /* Skip the null string terminator from previous parameter. */
2200
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002201 png_debug1(3, "Reading pCAL parameter %d", i);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002202
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002203 for (params[i] = (png_charp)buf; buf <= endptr && *buf != 0; buf++)
Glenn Randers-Pehrsonf9f2fe01998-03-15 18:20:23 -06002204 /* Empty loop to move past each parameter string */ ;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002205
2206 /* Make sure we haven't run out of data yet */
2207 if (buf > endptr)
2208 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002209 png_free(png_ptr, params);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002210 png_chunk_benign_error(png_ptr, "invalid data");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002211 return;
2212 }
2213 }
2214
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002215 png_set_pCAL(png_ptr, info_ptr, (png_charp)buffer, X0, X1, type, nparams,
2216 (png_charp)units, params);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002217
Andreas Dilger47a0c421997-05-16 02:46:07 -05002218 png_free(png_ptr, params);
Guy Schalnat0d580581995-07-20 02:43:20 -05002219}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002220#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002221
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002222#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002223/* Read the sCAL chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002224void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002225png_handle_sCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002226{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002227 png_bytep buffer;
2228 png_size_t i;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002229 int state;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002230
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002231 png_debug(1, "in png_handle_sCAL");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002232
2233 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002234 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002235
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002236 else if (png_ptr->mode & PNG_HAVE_IDAT)
2237 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002238 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002239 png_chunk_benign_error(png_ptr, "out of place");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002240 return;
2241 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002242
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002243 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002244 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002245 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002246 png_chunk_benign_error(png_ptr, "duplicate");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002247 return;
2248 }
2249
Glenn Randers-Pehrson254a5132011-06-10 17:45:48 -05002250 /* Need unit type, width, \0, height: minimum 4 bytes */
2251 else if (length < 4)
2252 {
Glenn Randers-Pehrson254a5132011-06-10 17:45:48 -05002253 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002254 png_chunk_benign_error(png_ptr, "invalid");
Glenn Randers-Pehrson254a5132011-06-10 17:45:48 -05002255 return;
2256 }
2257
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002258 png_debug1(2, "Allocating and reading sCAL chunk data (%u bytes)",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002259 length + 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002260
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002261 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002262
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002263 if (buffer == NULL)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002264 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002265 png_chunk_benign_error(png_ptr, "out of memory");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002266 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002267 return;
2268 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002269
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002270 png_crc_read(png_ptr, buffer, length);
2271 buffer[length] = 0; /* Null terminate the last string */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002272
2273 if (png_crc_finish(png_ptr, 0))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002274 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002275
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002276 /* Validate the unit. */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002277 if (buffer[0] != 1 && buffer[0] != 2)
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -05002278 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002279 png_chunk_benign_error(png_ptr, "invalid unit");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05002280 return;
2281 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002282
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002283 /* Validate the ASCII numbers, need two ASCII numbers separated by
2284 * a '\0' and they need to fit exactly in the chunk data.
2285 */
Glenn Randers-Pehrson254a5132011-06-10 17:45:48 -05002286 i = 1;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002287 state = 0;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002288
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002289 if (!png_check_fp_number((png_const_charp)buffer, length, &state, &i) ||
2290 i >= length || buffer[i++] != 0)
2291 png_chunk_benign_error(png_ptr, "bad width format");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002292
John Bowler8d261262011-06-18 13:37:11 -05002293 else if (!PNG_FP_IS_POSITIVE(state))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002294 png_chunk_benign_error(png_ptr, "non-positive width");
John Bowler8d261262011-06-18 13:37:11 -05002295
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002296 else
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -06002297 {
John Bowler168a4332011-01-16 19:32:22 -06002298 png_size_t heighti = i;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002299
Glenn Randers-Pehrson254a5132011-06-10 17:45:48 -05002300 state = 0;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002301 if (!png_check_fp_number((png_const_charp)buffer, length, &state, &i) ||
2302 i != length)
2303 png_chunk_benign_error(png_ptr, "bad height format");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002304
John Bowler8d261262011-06-18 13:37:11 -05002305 else if (!PNG_FP_IS_POSITIVE(state))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002306 png_chunk_benign_error(png_ptr, "non-positive height");
John Bowler8d261262011-06-18 13:37:11 -05002307
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002308 else
2309 /* This is the (only) success case. */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002310 png_set_sCAL_s(png_ptr, info_ptr, buffer[0],
2311 (png_charp)buffer+1, (png_charp)buffer+heighti);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002312 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002313}
2314#endif
2315
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002316#ifdef PNG_READ_tIME_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002317void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002318png_handle_tIME(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002319{
2320 png_byte buf[7];
2321 png_time mod_time;
2322
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002323 png_debug(1, "in png_handle_tIME");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002324
Guy Schalnate5a37791996-06-05 15:50:50 -05002325 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002326 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002327
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002328 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002329 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002330 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002331 png_chunk_benign_error(png_ptr, "duplicate");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002332 return;
2333 }
2334
2335 if (png_ptr->mode & PNG_HAVE_IDAT)
2336 png_ptr->mode |= PNG_AFTER_IDAT;
Guy Schalnate5a37791996-06-05 15:50:50 -05002337
Guy Schalnat0d580581995-07-20 02:43:20 -05002338 if (length != 7)
2339 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002340 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002341 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnat0d580581995-07-20 02:43:20 -05002342 return;
2343 }
2344
2345 png_crc_read(png_ptr, buf, 7);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002346
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002347 if (png_crc_finish(png_ptr, 0))
2348 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05002349
2350 mod_time.second = buf[6];
2351 mod_time.minute = buf[5];
2352 mod_time.hour = buf[4];
2353 mod_time.day = buf[3];
2354 mod_time.month = buf[2];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002355 mod_time.year = png_get_uint_16(buf);
Guy Schalnat0d580581995-07-20 02:43:20 -05002356
Andreas Dilger47a0c421997-05-16 02:46:07 -05002357 png_set_tIME(png_ptr, info_ptr, &mod_time);
Guy Schalnat0d580581995-07-20 02:43:20 -05002358}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002359#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002360
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002361#ifdef PNG_READ_tEXt_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002362/* Note: this does not properly handle chunks that are > 64K under DOS */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002363void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002364png_handle_tEXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002365{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002366 png_text text_info;
2367 png_bytep buffer;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002368 png_charp key;
Guy Schalnat6d764711995-12-19 03:22:19 -06002369 png_charp text;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002370 png_uint_32 skip = 0;
2371
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002372 png_debug(1, "in png_handle_tEXt");
Guy Schalnat0d580581995-07-20 02:43:20 -05002373
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06002374#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002375 if (png_ptr->user_chunk_cache_max != 0)
2376 {
2377 if (png_ptr->user_chunk_cache_max == 1)
2378 {
2379 png_crc_finish(png_ptr, length);
2380 return;
2381 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002382
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002383 if (--png_ptr->user_chunk_cache_max == 1)
2384 {
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002385 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002386 png_chunk_benign_error(png_ptr, "no space in chunk cache");
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002387 return;
2388 }
2389 }
2390#endif
2391
Guy Schalnate5a37791996-06-05 15:50:50 -05002392 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002393 png_chunk_error(png_ptr, "missing IHDR");
Guy Schalnate5a37791996-06-05 15:50:50 -05002394
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002395 if (png_ptr->mode & PNG_HAVE_IDAT)
2396 png_ptr->mode |= PNG_AFTER_IDAT;
2397
Andreas Dilger47a0c421997-05-16 02:46:07 -05002398#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002399 if (length > 65535U)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002400 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002401 png_crc_finish(png_ptr, length);
2402 png_chunk_benign_error(png_ptr, "too large to fit in memory");
2403 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002404 }
2405#endif
2406
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002407 buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
Glenn Randers-Pehrson97a9b482008-10-25 20:03:28 -05002408
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002409 if (buffer == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002410 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002411 png_chunk_benign_error(png_ptr, "out of memory");
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002412 return;
2413 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002414
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002415 png_crc_read(png_ptr, buffer, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002416
2417 if (png_crc_finish(png_ptr, skip))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002418 return;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002419
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002420 key = (png_charp)buffer;
2421 key[length] = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -05002422
2423 for (text = key; *text; text++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002424 /* Empty loop to find end of key */ ;
Guy Schalnat0d580581995-07-20 02:43:20 -05002425
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002426 if (text != key + length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002427 text++;
2428
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002429 text_info.compression = PNG_TEXT_COMPRESSION_NONE;
2430 text_info.key = key;
2431 text_info.lang = NULL;
2432 text_info.lang_key = NULL;
2433 text_info.itxt_length = 0;
2434 text_info.text = text;
2435 text_info.text_length = strlen(text);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002436
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002437 if (png_set_text_2(png_ptr, info_ptr, &text_info, 1))
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002438 png_warning(png_ptr, "Insufficient memory to process text chunk");
Guy Schalnat0d580581995-07-20 02:43:20 -05002439}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002440#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002441
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002442#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002443/* Note: this does not correctly handle chunks that are > 64K under DOS */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002444void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002445png_handle_zTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002446{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002447 png_const_charp errmsg = NULL;
2448 png_bytep buffer;
2449 png_uint_32 keyword_length;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002450
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002451 png_debug(1, "in png_handle_zTXt");
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002452
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06002453#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002454 if (png_ptr->user_chunk_cache_max != 0)
2455 {
2456 if (png_ptr->user_chunk_cache_max == 1)
2457 {
2458 png_crc_finish(png_ptr, length);
2459 return;
2460 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002461
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002462 if (--png_ptr->user_chunk_cache_max == 1)
2463 {
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002464 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002465 png_chunk_benign_error(png_ptr, "no space in chunk cache");
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002466 return;
2467 }
2468 }
2469#endif
2470
Guy Schalnate5a37791996-06-05 15:50:50 -05002471 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002472 png_chunk_error(png_ptr, "missing IHDR");
Guy Schalnate5a37791996-06-05 15:50:50 -05002473
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002474 if (png_ptr->mode & PNG_HAVE_IDAT)
2475 png_ptr->mode |= PNG_AFTER_IDAT;
2476
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002477 buffer = png_read_buffer(png_ptr, length, 2/*silent*/);
2478
2479 if (buffer == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002480 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002481 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002482 png_chunk_benign_error(png_ptr, "out of memory");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002483 return;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002484 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002485
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002486 png_crc_read(png_ptr, buffer, length);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002487
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002488 if (png_crc_finish(png_ptr, 0))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002489 return;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002490
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002491 /* TODO: also check that the keyword contents match the spec! */
2492 for (keyword_length = 0;
2493 keyword_length < length && buffer[keyword_length] != 0;
2494 ++keyword_length)
2495 /* Empty loop to find end of name */ ;
Guy Schalnat0d580581995-07-20 02:43:20 -05002496
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002497 if (keyword_length > 79 || keyword_length < 1)
2498 errmsg = "bad keyword";
Guy Schalnat0d580581995-07-20 02:43:20 -05002499
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002500 /* zTXt must have some LZ data after the keyword, although it may expand to
2501 * zero bytes; we need a '\0' at the end of the keyword, the compression type
2502 * then the LZ data:
2503 */
2504 else if (keyword_length + 3 > length)
2505 errmsg = "truncated";
2506
2507 else if (buffer[keyword_length+1] != PNG_COMPRESSION_TYPE_BASE)
2508 errmsg = "unknown compression type";
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002509
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002510 else
Guy Schalnat0d580581995-07-20 02:43:20 -05002511 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002512 png_alloc_size_t uncompressed_length = PNG_SIZE_MAX;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002513
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002514 /* TODO: at present png_decompress_chunk imposes a single application
2515 * level memory limit, this should be split to different values for iCCP
2516 * and text chunks.
2517 */
2518 if (png_decompress_chunk(png_ptr, length, keyword_length+2,
2519 &uncompressed_length, 1/*terminate*/) == Z_STREAM_END)
2520 {
2521 png_text text;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002522
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002523 /* It worked; png_ptr->read_buffer now looks like a tEXt chunk except
2524 * for the extra compression type byte and the fact that it isn't
2525 * necessarily '\0' terminated.
2526 */
2527 buffer = png_ptr->read_buffer;
2528 buffer[uncompressed_length+(keyword_length+2)] = 0;
2529
2530 text.compression = PNG_TEXT_COMPRESSION_zTXt;
2531 text.key = (png_charp)buffer;
2532 text.text = (png_charp)(buffer + keyword_length+2);
2533 text.text_length = uncompressed_length;
2534 text.itxt_length = 0;
2535 text.lang = NULL;
2536 text.lang_key = NULL;
2537
2538 if (png_set_text_2(png_ptr, info_ptr, &text, 1))
2539 errmsg = "insufficient memory";
2540 }
2541
2542 else
2543 errmsg = png_ptr->zstream.msg;
John Bowlerb5d00512012-03-09 09:15:18 -06002544 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002545
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002546 if (errmsg != NULL)
2547 png_chunk_benign_error(png_ptr, errmsg);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002548}
2549#endif
2550
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002551#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002552/* Note: this does not correctly handle chunks that are > 64K under DOS */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002553void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002554png_handle_iTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002555{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002556 png_const_charp errmsg = NULL;
2557 png_bytep buffer;
2558 png_uint_32 prefix_length;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002559
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002560 png_debug(1, "in png_handle_iTXt");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002561
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06002562#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002563 if (png_ptr->user_chunk_cache_max != 0)
2564 {
2565 if (png_ptr->user_chunk_cache_max == 1)
2566 {
2567 png_crc_finish(png_ptr, length);
2568 return;
2569 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002570
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002571 if (--png_ptr->user_chunk_cache_max == 1)
2572 {
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002573 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002574 png_chunk_benign_error(png_ptr, "no space in chunk cache");
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002575 return;
2576 }
2577 }
2578#endif
2579
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002580 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002581 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002582
2583 if (png_ptr->mode & PNG_HAVE_IDAT)
2584 png_ptr->mode |= PNG_AFTER_IDAT;
2585
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002586 buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
2587
2588 if (buffer == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002589 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002590 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002591 png_chunk_benign_error(png_ptr, "out of memory");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002592 return;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002593 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002594
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002595 png_crc_read(png_ptr, buffer, length);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002596
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002597 if (png_crc_finish(png_ptr, 0))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002598 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002599
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002600 /* First the keyword. */
2601 for (prefix_length=0;
2602 prefix_length < length && buffer[prefix_length] != 0;
2603 ++prefix_length)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002604 /* Empty loop */ ;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002605
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002606 /* Perform a basic check on the keyword length here. */
2607 if (prefix_length > 79 || prefix_length < 1)
2608 errmsg = "bad keyword";
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002609
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002610 /* Expect keyword, compression flag, compression type, language, translated
2611 * keyword (both may be empty but are 0 terminated) then the text, which may
2612 * be empty.
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002613 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002614 else if (prefix_length + 5 > length)
2615 errmsg = "truncated";
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002616
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002617 else if (buffer[prefix_length+1] == 0 ||
2618 (buffer[prefix_length+1] == 1 &&
2619 buffer[prefix_length+2] == PNG_COMPRESSION_TYPE_BASE))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002620 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002621 int compressed = buffer[prefix_length+1] != 0;
2622 png_uint_32 language_offset, translated_keyword_offset;
2623 png_alloc_size_t uncompressed_length = 0;
2624
2625 /* Now the language tag */
2626 prefix_length += 3;
2627 language_offset = prefix_length;
2628
2629 for (; prefix_length < length && buffer[prefix_length] != 0;
2630 ++prefix_length)
2631 /* Empty loop */ ;
2632
2633 /* WARNING: the length may be invalid here, this is checked below. */
2634 translated_keyword_offset = ++prefix_length;
2635
2636 for (; prefix_length < length && buffer[prefix_length] != 0;
2637 ++prefix_length)
2638 /* Empty loop */ ;
2639
2640 /* prefix_length should now be at the trailing '\0' of the translated
2641 * keyword, but it may already be over the end. None of this arithmetic
2642 * can overflow because chunks are at most 2^31 bytes long, but on 16-bit
2643 * systems the available allocaton may overflow.
2644 */
2645 ++prefix_length;
2646
2647 if (!compressed && prefix_length <= length)
2648 uncompressed_length = length - prefix_length;
2649
2650 else if (compressed && prefix_length < length)
2651 {
2652 uncompressed_length = PNG_SIZE_MAX;
2653
2654 /* TODO: at present png_decompress_chunk imposes a single application
2655 * level memory limit, this should be split to different values for
2656 * iCCP and text chunks.
2657 */
2658 if (png_decompress_chunk(png_ptr, length, prefix_length,
2659 &uncompressed_length, 1/*terminate*/) == Z_STREAM_END)
2660 buffer = png_ptr->read_buffer;
2661
2662 else
2663 errmsg = png_ptr->zstream.msg;
2664 }
2665
2666 else
2667 errmsg = "truncated";
2668
2669 if (errmsg == NULL)
2670 {
2671 png_text text;
2672
2673 buffer[uncompressed_length+prefix_length] = 0;
2674
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06002675 if (compressed != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002676 text.compression = PNG_ITXT_COMPRESSION_NONE;
2677
2678 else
2679 text.compression = PNG_ITXT_COMPRESSION_zTXt;
2680
2681 text.key = (png_charp)buffer;
2682 text.lang = (png_charp)buffer + language_offset;
2683 text.lang_key = (png_charp)buffer + translated_keyword_offset;
2684 text.text = (png_charp)buffer + prefix_length;
2685 text.text_length = 0;
2686 text.itxt_length = uncompressed_length;
2687
2688 if (png_set_text_2(png_ptr, info_ptr, &text, 1))
2689 errmsg = "insufficient memory";
2690 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002691 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002692
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002693 else
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002694 errmsg = "bad compression info";
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002695
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002696 if (errmsg != NULL)
2697 png_chunk_benign_error(png_ptr, errmsg);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002698}
2699#endif
2700
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06002701#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002702/* Utility function for png_handle_unknown; set up png_ptr::unknown_chunk */
2703static int
2704png_cache_unknown_chunk(png_structrp png_ptr, png_uint_32 length)
2705{
2706 png_alloc_size_t limit = PNG_SIZE_MAX;
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06002707
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002708 if (png_ptr->unknown_chunk.data != NULL)
2709 {
2710 png_free(png_ptr, png_ptr->unknown_chunk.data);
2711 png_ptr->unknown_chunk.data = NULL;
2712 }
2713
2714# ifdef PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED
2715 if (png_ptr->user_chunk_malloc_max > 0 &&
2716 png_ptr->user_chunk_malloc_max < limit)
2717 limit = png_ptr->user_chunk_malloc_max;
2718
2719# elif PNG_USER_CHUNK_MALLOC_MAX > 0
2720 if (PNG_USER_CHUNK_MALLOC_MAX < limit)
2721 limit = PNG_USER_CHUNK_MALLOC_MAX;
2722# endif
2723
2724 if (length <= limit)
2725 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002726 PNG_CSTRING_FROM_CHUNK(png_ptr->unknown_chunk.name, png_ptr->chunk_name);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002727 /* The following is safe because of the PNG_SIZE_MAX init above */
2728 png_ptr->unknown_chunk.size = (png_size_t)length/*SAFE*/;
2729 /* 'mode' is a flag array, only the bottom four bits matter here */
2730 png_ptr->unknown_chunk.location = (png_byte)png_ptr->mode/*SAFE*/;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002731
2732 if (length == 0)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002733 png_ptr->unknown_chunk.data = NULL;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002734
2735 else
2736 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002737 /* Do a 'warn' here - it is handled below. */
2738 png_ptr->unknown_chunk.data = png_voidcast(png_bytep,
2739 png_malloc_warn(png_ptr, length));
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06002740 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002741 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002742
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002743 if (png_ptr->unknown_chunk.data == NULL && length > 0)
2744 {
2745 /* This is benign because we clean up correctly */
2746 png_crc_finish(png_ptr, length);
2747 png_chunk_benign_error(png_ptr, "unknown chunk exceeds memory limits");
2748 return 0;
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06002749 }
John Bowlere9567512012-08-15 22:53:00 -05002750
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06002751 else
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002752 {
2753 if (length > 0)
2754 png_crc_read(png_ptr, png_ptr->unknown_chunk.data, length);
2755 png_crc_finish(png_ptr, 0);
2756 return 1;
2757 }
2758}
2759#endif /* PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */
John Bowlere9567512012-08-15 22:53:00 -05002760
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002761/* Handle an unknown, or known but disabled, chunk */
2762void /* PRIVATE */
2763png_handle_unknown(png_structrp png_ptr, png_inforp info_ptr,
2764 png_uint_32 length, int keep)
2765{
2766 int handled = 0; /* the chunk was handled */
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06002767
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002768 png_debug(1, "in png_handle_unknown");
2769
Glenn Randers-Pehrsonb3721752013-09-30 13:56:44 -05002770#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002771 /* NOTE: this code is based on the code in libpng-1.4.12 except for fixing
2772 * the bug which meant that setting a non-default behavior for a specific
2773 * chunk would be ignored (the default was always used unless a user
2774 * callback was installed).
2775 *
2776 * 'keep' is the value from the png_chunk_unknown_handling, the setting for
2777 * this specific chunk_name, if PNG_HANDLE_AS_UNKNOWN_SUPPORTED, if not it
2778 * will always be PNG_HANDLE_CHUNK_AS_DEFAULT and it needs to be set here.
2779 * This is just an optimization to avoid multiple calls to the lookup
2780 * function.
2781 */
Glenn Randers-Pehrson873f16f2013-09-20 14:28:50 -05002782# ifndef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsonb3721752013-09-30 13:56:44 -05002783# ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
2784 keep = png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name);
2785# endif
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002786# endif
2787
2788 /* One of the following methods will read the chunk or skip it (at least one
2789 * of these is always defined because this is the only way to switch on
2790 * PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
2791 */
2792# ifdef PNG_READ_USER_CHUNKS_SUPPORTED
2793 /* The user callback takes precedence over the chunk keep value, but the
2794 * keep value is still required to validate a save of a critical chunk.
2795 */
2796 if (png_ptr->read_user_chunk_fn != NULL)
2797 {
2798 if (png_cache_unknown_chunk(png_ptr, length))
2799 {
2800 /* Callback to user unknown chunk handler */
2801 int ret = (*(png_ptr->read_user_chunk_fn))(png_ptr,
2802 &png_ptr->unknown_chunk);
2803
2804 /* ret is:
2805 * negative: An error occured, png_chunk_error will be called.
2806 * zero: The chunk was not handled, the chunk will be discarded
2807 * unless png_set_keep_unknown_chunks has been used to set
2808 * a 'keep' behavior for this particular chunk, in which
2809 * case that will be used. A critical chunk will cause an
2810 * error at this point unless it is to be saved.
2811 * positive: The chunk was handled, libpng will ignore/discard it.
2812 */
2813 if (ret < 0)
2814 png_chunk_error(png_ptr, "error in user chunk");
2815
2816 else if (ret == 0)
2817 {
2818 /* If the keep value is 'default' or 'never' override it, but
2819 * still error out on critical chunks unless the keep value is
2820 * 'always' While this is weird it is the behavior in 1.4.12.
2821 * A possible improvement would be to obey the value set for the
2822 * chunk, but this would be an API change that would probably
2823 * damage some applications.
2824 *
2825 * The png_app_warning below catches the case that matters, where
2826 * the application has not set specific save or ignore for this
2827 * chunk or global save or ignore.
2828 */
Glenn Randers-Pehrson4ea113b2013-03-02 16:03:45 -06002829 if (keep < PNG_HANDLE_CHUNK_IF_SAFE)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002830 {
2831# ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
2832 if (png_ptr->unknown_default < PNG_HANDLE_CHUNK_IF_SAFE)
Glenn Randers-Pehrson8c87dc82013-03-04 17:38:04 -06002833 {
2834 png_chunk_warning(png_ptr, "Saving unknown chunk:");
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002835 png_app_warning(png_ptr,
2836 "forcing save of an unhandled chunk;"
2837 " please call png_set_keep_unknown_chunks");
Glenn Randers-Pehrson8c87dc82013-03-04 17:38:04 -06002838 /* with keep = PNG_HANDLE_CHUNK_IF_SAFE */
2839 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002840# endif
2841 keep = PNG_HANDLE_CHUNK_IF_SAFE;
2842 }
2843 }
2844
2845 else /* chunk was handled */
2846 {
2847 handled = 1;
2848 /* Critical chunks can be safely discarded at this point. */
2849 keep = PNG_HANDLE_CHUNK_NEVER;
2850 }
2851 }
2852
2853 else
2854 keep = PNG_HANDLE_CHUNK_NEVER; /* insufficient memory */
2855 }
2856
2857 else
2858 /* Use the SAVE_UNKNOWN_CHUNKS code or skip the chunk */
2859# endif /* PNG_READ_USER_CHUNKS_SUPPORTED */
2860
2861# ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
2862 {
2863 /* keep is currently just the per-chunk setting, if there was no
2864 * setting change it to the global default now (not that this may
2865 * still be AS_DEFAULT) then obtain the cache of the chunk if required,
2866 * if not simply skip the chunk.
2867 */
2868 if (keep == PNG_HANDLE_CHUNK_AS_DEFAULT)
2869 keep = png_ptr->unknown_default;
2870
2871 if (keep == PNG_HANDLE_CHUNK_ALWAYS ||
2872 (keep == PNG_HANDLE_CHUNK_IF_SAFE &&
Glenn Randers-Pehrson67a289f2013-04-19 19:03:34 -05002873 PNG_CHUNK_ANCILLARY(png_ptr->chunk_name)))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002874 {
2875 if (!png_cache_unknown_chunk(png_ptr, length))
2876 keep = PNG_HANDLE_CHUNK_NEVER;
2877 }
2878
2879 else
2880 png_crc_finish(png_ptr, length);
2881 }
2882# else
2883# ifndef PNG_READ_USER_CHUNKS_SUPPORTED
2884# error no method to support READ_UNKNOWN_CHUNKS
2885# endif
2886
2887 {
2888 /* If here there is no read callback pointer set and no support is
2889 * compiled in to just save the unknown chunks, so simply skip this
2890 * chunk. If 'keep' is something other than AS_DEFAULT or NEVER then
2891 * the app has erroneously asked for unknown chunk saving when there
2892 * is no support.
2893 */
2894 if (keep > PNG_HANDLE_CHUNK_NEVER)
2895 png_app_error(png_ptr, "no unknown chunk support available");
2896
2897 png_crc_finish(png_ptr, length);
2898 }
Glenn Randers-Pehrsonb3721752013-09-30 13:56:44 -05002899# endif
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002900
2901# ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
2902 /* Now store the chunk in the chunk list if appropriate, and if the limits
2903 * permit it.
2904 */
2905 if (keep == PNG_HANDLE_CHUNK_ALWAYS ||
2906 (keep == PNG_HANDLE_CHUNK_IF_SAFE &&
Glenn Randers-Pehrson67a289f2013-04-19 19:03:34 -05002907 PNG_CHUNK_ANCILLARY(png_ptr->chunk_name)))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002908 {
2909# ifdef PNG_USER_LIMITS_SUPPORTED
2910 switch (png_ptr->user_chunk_cache_max)
2911 {
2912 case 2:
2913 png_ptr->user_chunk_cache_max = 1;
2914 png_chunk_benign_error(png_ptr, "no space in chunk cache");
2915 /* FALL THROUGH */
2916 case 1:
2917 /* NOTE: prior to 1.6.0 this case resulted in an unknown critical
2918 * chunk being skipped, now there will be a hard error below.
2919 */
2920 break;
2921
2922 default: /* not at limit */
2923 --(png_ptr->user_chunk_cache_max);
2924 /* FALL THROUGH */
2925 case 0: /* no limit */
2926# endif /* PNG_USER_LIMITS_SUPPORTED */
2927 /* Here when the limit isn't reached or when limits are compiled
2928 * out; store the chunk.
2929 */
2930 png_set_unknown_chunks(png_ptr, info_ptr,
2931 &png_ptr->unknown_chunk, 1);
2932 handled = 1;
2933# ifdef PNG_USER_LIMITS_SUPPORTED
2934 break;
2935 }
2936# endif
2937 }
Glenn Randers-Pehrsonb3721752013-09-30 13:56:44 -05002938# else /* no store support: the chunk must be handled by the user callback */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002939 PNG_UNUSED(info_ptr)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002940# endif
2941
2942 /* Regardless of the error handling below the cached data (if any) can be
2943 * freed now. Notice that the data is not freed if there is a png_error, but
2944 * it will be freed by destroy_read_struct.
2945 */
2946 if (png_ptr->unknown_chunk.data != NULL)
2947 png_free(png_ptr, png_ptr->unknown_chunk.data);
2948 png_ptr->unknown_chunk.data = NULL;
2949
2950#else /* !PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */
2951 /* There is no support to read an unknown chunk, so just skip it. */
2952 png_crc_finish(png_ptr, length);
2953 PNG_UNUSED(info_ptr)
2954 PNG_UNUSED(keep)
2955#endif /* !PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */
2956
2957 /* Check for unhandled critical chunks */
2958 if (!handled && PNG_CHUNK_CRITICAL(png_ptr->chunk_name))
2959 png_chunk_error(png_ptr, "unhandled critical chunk");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002960}
2961
2962/* This function is called to verify that a chunk name is valid.
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002963 * This function can't have the "critical chunk check" incorporated
2964 * into it, since in the future we will need to be able to call user
2965 * functions to handle unknown critical chunks after we check that
2966 * the chunk name itself is valid.
2967 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002968
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002969/* Bit hacking: the test for an invalid byte in the 4 byte chunk name is:
2970 *
2971 * ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
2972 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002973
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002974void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002975png_check_chunk_name(png_structrp png_ptr, png_uint_32 chunk_name)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002976{
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002977 int i;
2978
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002979 png_debug(1, "in png_check_chunk_name");
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002980
2981 for (i=1; i<=4; ++i)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002982 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002983 int c = chunk_name & 0xff;
2984
2985 if (c < 65 || c > 122 || (c > 90 && c < 97))
2986 png_chunk_error(png_ptr, "invalid chunk type");
2987
2988 chunk_name >>= 8;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002989 }
2990}
2991
John Bowler7875d532011-11-07 22:33:49 -06002992/* Combines the row recently read in with the existing pixels in the row. This
2993 * routine takes care of alpha and transparency if requested. This routine also
2994 * handles the two methods of progressive display of interlaced images,
2995 * depending on the 'display' value; if 'display' is true then the whole row
2996 * (dp) is filled from the start by replicating the available pixels. If
2997 * 'display' is false only those pixels present in the pass are filled in.
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002998 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002999void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06003000png_combine_row(png_const_structrp png_ptr, png_bytep dp, int display)
Guy Schalnat0d580581995-07-20 02:43:20 -05003001{
John Bowler4e68aa72011-10-11 16:01:33 -05003002 unsigned int pixel_depth = png_ptr->transformed_pixel_depth;
3003 png_const_bytep sp = png_ptr->row_buf + 1;
John Bowlerac8375d2011-10-06 22:27:16 -05003004 png_uint_32 row_width = png_ptr->width;
John Bowler4e68aa72011-10-11 16:01:33 -05003005 unsigned int pass = png_ptr->pass;
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003006 png_bytep end_ptr = 0;
3007 png_byte end_byte = 0;
3008 unsigned int end_mask;
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003009
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05003010 png_debug(1, "in png_combine_row");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003011
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003012 /* Added in 1.5.6: it should not be possible to enter this routine until at
3013 * least one row has been read from the PNG data and transformed.
3014 */
3015 if (pixel_depth == 0)
3016 png_error(png_ptr, "internal row logic error");
3017
3018 /* Added in 1.5.4: the pixel depth should match the information returned by
3019 * any call to png_read_update_info at this point. Do not continue if we got
John Bowler9994f252011-05-15 18:52:39 -05003020 * this wrong.
3021 */
3022 if (png_ptr->info_rowbytes != 0 && png_ptr->info_rowbytes !=
John Bowlerac8375d2011-10-06 22:27:16 -05003023 PNG_ROWBYTES(pixel_depth, row_width))
John Bowler9994f252011-05-15 18:52:39 -05003024 png_error(png_ptr, "internal row size calculation error");
3025
John Bowlerac8375d2011-10-06 22:27:16 -05003026 /* Don't expect this to ever happen: */
3027 if (row_width == 0)
3028 png_error(png_ptr, "internal row width error");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003029
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003030 /* Preserve the last byte in cases where only part of it will be overwritten,
3031 * the multiply below may overflow, we don't care because ANSI-C guarantees
3032 * we get the low bits.
3033 */
3034 end_mask = (pixel_depth * row_width) & 7;
3035 if (end_mask != 0)
3036 {
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003037 /* end_ptr == NULL is a flag to say do nothing */
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003038 end_ptr = dp + PNG_ROWBYTES(pixel_depth, row_width) - 1;
3039 end_byte = *end_ptr;
3040# ifdef PNG_READ_PACKSWAP_SUPPORTED
3041 if (png_ptr->transformations & PNG_PACKSWAP) /* little-endian byte */
3042 end_mask = 0xff << end_mask;
3043
3044 else /* big-endian byte */
3045# endif
3046 end_mask = 0xff >> end_mask;
3047 /* end_mask is now the bits to *keep* from the destination row */
3048 }
3049
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003050 /* For non-interlaced images this reduces to a memcpy(). A memcpy()
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003051 * will also happen if interlacing isn't supported or if the application
3052 * does not call png_set_interlace_handling(). In the latter cases the
3053 * caller just gets a sequence of the unexpanded rows from each interlace
3054 * pass.
John Bowlerac8375d2011-10-06 22:27:16 -05003055 */
3056#ifdef PNG_READ_INTERLACING_SUPPORTED
3057 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE) &&
John Bowler4e68aa72011-10-11 16:01:33 -05003058 pass < 6 && (display == 0 ||
3059 /* The following copies everything for 'display' on passes 0, 2 and 4. */
3060 (display == 1 && (pass & 1) != 0)))
Guy Schalnat0d580581995-07-20 02:43:20 -05003061 {
John Bowler4e68aa72011-10-11 16:01:33 -05003062 /* Narrow images may have no bits in a pass; the caller should handle
3063 * this, but this test is cheap:
John Bowlerac8375d2011-10-06 22:27:16 -05003064 */
John Bowler4e68aa72011-10-11 16:01:33 -05003065 if (row_width <= PNG_PASS_START_COL(pass))
3066 return;
John Bowlerac8375d2011-10-06 22:27:16 -05003067
John Bowler4e68aa72011-10-11 16:01:33 -05003068 if (pixel_depth < 8)
Guy Schalnat0d580581995-07-20 02:43:20 -05003069 {
John Bowler7875d532011-11-07 22:33:49 -06003070 /* For pixel depths up to 4 bpp the 8-pixel mask can be expanded to fit
John Bowler4e68aa72011-10-11 16:01:33 -05003071 * into 32 bits, then a single loop over the bytes using the four byte
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003072 * values in the 32-bit mask can be used. For the 'display' option the
John Bowler4e68aa72011-10-11 16:01:33 -05003073 * expanded mask may also not require any masking within a byte. To
3074 * make this work the PACKSWAP option must be taken into account - it
3075 * simply requires the pixels to be reversed in each byte.
3076 *
3077 * The 'regular' case requires a mask for each of the first 6 passes,
3078 * the 'display' case does a copy for the even passes in the range
John Bowler7875d532011-11-07 22:33:49 -06003079 * 0..6. This has already been handled in the test above.
John Bowler4e68aa72011-10-11 16:01:33 -05003080 *
3081 * The masks are arranged as four bytes with the first byte to use in
3082 * the lowest bits (little-endian) regardless of the order (PACKSWAP or
3083 * not) of the pixels in each byte.
3084 *
3085 * NOTE: the whole of this logic depends on the caller of this function
3086 * only calling it on rows appropriate to the pass. This function only
John Bowler7875d532011-11-07 22:33:49 -06003087 * understands the 'x' logic; the 'y' logic is handled by the caller.
John Bowler4e68aa72011-10-11 16:01:33 -05003088 *
3089 * The following defines allow generation of compile time constant bit
3090 * masks for each pixel depth and each possibility of swapped or not
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003091 * swapped bytes. Pass 'p' is in the range 0..6; 'x', a pixel index,
3092 * is in the range 0..7; and the result is 1 if the pixel is to be
3093 * copied in the pass, 0 if not. 'S' is for the sparkle method, 'B'
3094 * for the block method.
3095 *
John Bowler92ef3132011-10-27 19:36:08 -05003096 * With some compilers a compile time expression of the general form:
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05003097 *
John Bowler92ef3132011-10-27 19:36:08 -05003098 * (shift >= 32) ? (a >> (shift-32)) : (b >> shift)
3099 *
3100 * Produces warnings with values of 'shift' in the range 33 to 63
Glenn Randers-Pehrson18763662011-10-27 22:09:22 -05003101 * because the right hand side of the ?: expression is evaluated by
John Bowler92ef3132011-10-27 19:36:08 -05003102 * the compiler even though it isn't used. Microsoft Visual C (various
3103 * versions) and the Intel C compiler are known to do this. To avoid
3104 * this the following macros are used in 1.5.6. This is a temporary
John Bowler7875d532011-11-07 22:33:49 -06003105 * solution to avoid destabilizing the code during the release process.
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05003106 */
John Bowler92ef3132011-10-27 19:36:08 -05003107# if PNG_USE_COMPILE_TIME_MASKS
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05003108# define PNG_LSR(x,s) ((x)>>((s) & 0x1f))
3109# define PNG_LSL(x,s) ((x)<<((s) & 0x1f))
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003110# else
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05003111# define PNG_LSR(x,s) ((x)>>(s))
3112# define PNG_LSL(x,s) ((x)<<(s))
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003113# endif
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05003114# define S_COPY(p,x) (((p)<4 ? PNG_LSR(0x80088822,(3-(p))*8+(7-(x))) :\
3115 PNG_LSR(0xaa55ff00,(7-(p))*8+(7-(x)))) & 1)
3116# define B_COPY(p,x) (((p)<4 ? PNG_LSR(0xff0fff33,(3-(p))*8+(7-(x))) :\
3117 PNG_LSR(0xff55ff00,(7-(p))*8+(7-(x)))) & 1)
Guy Schalnat0d580581995-07-20 02:43:20 -05003118
John Bowler4e68aa72011-10-11 16:01:33 -05003119 /* Return a mask for pass 'p' pixel 'x' at depth 'd'. The mask is
3120 * little endian - the first pixel is at bit 0 - however the extra
3121 * parameter 's' can be set to cause the mask position to be swapped
3122 * within each byte, to match the PNG format. This is done by XOR of
3123 * the shift with 7, 6 or 4 for bit depths 1, 2 and 4.
3124 */
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05003125# define PIXEL_MASK(p,x,d,s) \
3126 (PNG_LSL(((PNG_LSL(1U,(d)))-1),(((x)*(d))^((s)?8-(d):0))))
John Bowlerac8375d2011-10-06 22:27:16 -05003127
John Bowler4e68aa72011-10-11 16:01:33 -05003128 /* Hence generate the appropriate 'block' or 'sparkle' pixel copy mask.
3129 */
3130# define S_MASKx(p,x,d,s) (S_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
3131# 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 -05003132
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003133 /* Combine 8 of these to get the full mask. For the 1-bpp and 2-bpp
3134 * cases the result needs replicating, for the 4-bpp case the above
3135 * generates a full 32 bits.
John Bowler4e68aa72011-10-11 16:01:33 -05003136 */
3137# define MASK_EXPAND(m,d) ((m)*((d)==1?0x01010101:((d)==2?0x00010001:1)))
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003138
John Bowler4e68aa72011-10-11 16:01:33 -05003139# define S_MASK(p,d,s) MASK_EXPAND(S_MASKx(p,0,d,s) + S_MASKx(p,1,d,s) +\
3140 S_MASKx(p,2,d,s) + S_MASKx(p,3,d,s) + S_MASKx(p,4,d,s) +\
3141 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 -06003142
John Bowler4e68aa72011-10-11 16:01:33 -05003143# define B_MASK(p,d,s) MASK_EXPAND(B_MASKx(p,0,d,s) + B_MASKx(p,1,d,s) +\
3144 B_MASKx(p,2,d,s) + B_MASKx(p,3,d,s) + B_MASKx(p,4,d,s) +\
3145 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 -05003146
John Bowler4e68aa72011-10-11 16:01:33 -05003147#if PNG_USE_COMPILE_TIME_MASKS
3148 /* Utility macros to construct all the masks for a depth/swap
3149 * combination. The 's' parameter says whether the format is PNG
John Bowler7875d532011-11-07 22:33:49 -06003150 * (big endian bytes) or not. Only the three odd-numbered passes are
John Bowler4e68aa72011-10-11 16:01:33 -05003151 * required for the display/block algorithm.
3152 */
3153# define S_MASKS(d,s) { S_MASK(0,d,s), S_MASK(1,d,s), S_MASK(2,d,s),\
3154 S_MASK(3,d,s), S_MASK(4,d,s), S_MASK(5,d,s) }
John Bowlerac8375d2011-10-06 22:27:16 -05003155
Glenn Randers-Pehrsonfa26eb12014-04-06 09:06:37 -05003156# define B_MASKS(d,s) { B_MASK(1,d,s), B_MASK(3,d,s), B_MASK(5,d,s) }
John Bowlerac8375d2011-10-06 22:27:16 -05003157
John Bowler4e68aa72011-10-11 16:01:33 -05003158# define DEPTH_INDEX(d) ((d)==1?0:((d)==2?1:2))
John Bowlerac8375d2011-10-06 22:27:16 -05003159
John Bowler4e68aa72011-10-11 16:01:33 -05003160 /* Hence the pre-compiled masks indexed by PACKSWAP (or not), depth and
3161 * then pass:
3162 */
John Bowler7875d532011-11-07 22:33:49 -06003163 static PNG_CONST png_uint_32 row_mask[2/*PACKSWAP*/][3/*depth*/][6] =
3164 {
John Bowler4e68aa72011-10-11 16:01:33 -05003165 /* Little-endian byte masks for PACKSWAP */
3166 { S_MASKS(1,0), S_MASKS(2,0), S_MASKS(4,0) },
3167 /* Normal (big-endian byte) masks - PNG format */
3168 { S_MASKS(1,1), S_MASKS(2,1), S_MASKS(4,1) }
3169 };
John Bowlerac8375d2011-10-06 22:27:16 -05003170
John Bowler4e68aa72011-10-11 16:01:33 -05003171 /* display_mask has only three entries for the odd passes, so index by
3172 * pass>>1.
3173 */
John Bowler7875d532011-11-07 22:33:49 -06003174 static PNG_CONST png_uint_32 display_mask[2][3][3] =
3175 {
John Bowler4e68aa72011-10-11 16:01:33 -05003176 /* Little-endian byte masks for PACKSWAP */
3177 { B_MASKS(1,0), B_MASKS(2,0), B_MASKS(4,0) },
3178 /* Normal (big-endian byte) masks - PNG format */
3179 { B_MASKS(1,1), B_MASKS(2,1), B_MASKS(4,1) }
3180 };
John Bowlerac8375d2011-10-06 22:27:16 -05003181
John Bowler4e68aa72011-10-11 16:01:33 -05003182# define MASK(pass,depth,display,png)\
3183 ((display)?display_mask[png][DEPTH_INDEX(depth)][pass>>1]:\
3184 row_mask[png][DEPTH_INDEX(depth)][pass])
John Bowlerac8375d2011-10-06 22:27:16 -05003185
John Bowler4e68aa72011-10-11 16:01:33 -05003186#else /* !PNG_USE_COMPILE_TIME_MASKS */
3187 /* This is the runtime alternative: it seems unlikely that this will
3188 * ever be either smaller or faster than the compile time approach.
3189 */
3190# define MASK(pass,depth,display,png)\
3191 ((display)?B_MASK(pass,depth,png):S_MASK(pass,depth,png))
3192#endif /* !PNG_USE_COMPILE_TIME_MASKS */
John Bowlerac8375d2011-10-06 22:27:16 -05003193
John Bowler4e68aa72011-10-11 16:01:33 -05003194 /* Use the appropriate mask to copy the required bits. In some cases
Glenn Randers-Pehrsonfa26eb12014-04-06 09:06:37 -05003195 * the byte mask will be 0 or 0xff; optimize these cases. row_width is
John Bowler4e68aa72011-10-11 16:01:33 -05003196 * the number of pixels, but the code copies bytes, so it is necessary
3197 * to special case the end.
3198 */
3199 png_uint_32 pixels_per_byte = 8 / pixel_depth;
3200 png_uint_32 mask;
John Bowlerac8375d2011-10-06 22:27:16 -05003201
John Bowler4e68aa72011-10-11 16:01:33 -05003202# ifdef PNG_READ_PACKSWAP_SUPPORTED
3203 if (png_ptr->transformations & PNG_PACKSWAP)
3204 mask = MASK(pass, pixel_depth, display, 0);
John Bowlerac8375d2011-10-06 22:27:16 -05003205
3206 else
John Bowler4e68aa72011-10-11 16:01:33 -05003207# endif
3208 mask = MASK(pass, pixel_depth, display, 1);
3209
3210 for (;;)
3211 {
3212 png_uint_32 m;
3213
3214 /* It doesn't matter in the following if png_uint_32 has more than
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003215 * 32 bits because the high bits always match those in m<<24; it is,
John Bowler4e68aa72011-10-11 16:01:33 -05003216 * however, essential to use OR here, not +, because of this.
3217 */
3218 m = mask;
3219 mask = (m >> 8) | (m << 24); /* rotate right to good compilers */
3220 m &= 0xff;
3221
3222 if (m != 0) /* something to copy */
John Bowlerac8375d2011-10-06 22:27:16 -05003223 {
John Bowler4e68aa72011-10-11 16:01:33 -05003224 if (m != 0xff)
3225 *dp = (png_byte)((*dp & ~m) | (*sp & m));
3226 else
3227 *dp = *sp;
John Bowlerac8375d2011-10-06 22:27:16 -05003228 }
John Bowler4e68aa72011-10-11 16:01:33 -05003229
3230 /* NOTE: this may overwrite the last byte with garbage if the image
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003231 * is not an exact number of bytes wide; libpng has always done
John Bowler4e68aa72011-10-11 16:01:33 -05003232 * this.
3233 */
3234 if (row_width <= pixels_per_byte)
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003235 break; /* May need to restore part of the last byte */
John Bowler4e68aa72011-10-11 16:01:33 -05003236
3237 row_width -= pixels_per_byte;
3238 ++dp;
3239 ++sp;
3240 }
3241 }
3242
3243 else /* pixel_depth >= 8 */
3244 {
3245 unsigned int bytes_to_copy, bytes_to_jump;
3246
3247 /* Validate the depth - it must be a multiple of 8 */
3248 if (pixel_depth & 7)
3249 png_error(png_ptr, "invalid user transform pixel depth");
3250
3251 pixel_depth >>= 3; /* now in bytes */
3252 row_width *= pixel_depth;
3253
3254 /* Regardless of pass number the Adam 7 interlace always results in a
3255 * fixed number of pixels to copy then to skip. There may be a
3256 * different number of pixels to skip at the start though.
3257 */
3258 {
3259 unsigned int offset = PNG_PASS_START_COL(pass) * pixel_depth;
3260
3261 row_width -= offset;
3262 dp += offset;
3263 sp += offset;
John Bowlerac8375d2011-10-06 22:27:16 -05003264 }
3265
John Bowler4e68aa72011-10-11 16:01:33 -05003266 /* Work out the bytes to copy. */
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06003267 if (display != 0)
John Bowler4e68aa72011-10-11 16:01:33 -05003268 {
3269 /* When doing the 'block' algorithm the pixel in the pass gets
3270 * replicated to adjacent pixels. This is why the even (0,2,4,6)
3271 * passes are skipped above - the entire expanded row is copied.
3272 */
3273 bytes_to_copy = (1<<((6-pass)>>1)) * pixel_depth;
3274
3275 /* But don't allow this number to exceed the actual row width. */
3276 if (bytes_to_copy > row_width)
3277 bytes_to_copy = row_width;
3278 }
3279
3280 else /* normal row; Adam7 only ever gives us one pixel to copy. */
3281 bytes_to_copy = pixel_depth;
3282
3283 /* In Adam7 there is a constant offset between where the pixels go. */
3284 bytes_to_jump = PNG_PASS_COL_OFFSET(pass) * pixel_depth;
3285
3286 /* And simply copy these bytes. Some optimization is possible here,
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003287 * depending on the value of 'bytes_to_copy'. Special case the low
John Bowler4e68aa72011-10-11 16:01:33 -05003288 * byte counts, which we know to be frequent.
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003289 *
3290 * Notice that these cases all 'return' rather than 'break' - this
3291 * avoids an unnecessary test on whether to restore the last byte
3292 * below.
John Bowler4e68aa72011-10-11 16:01:33 -05003293 */
3294 switch (bytes_to_copy)
3295 {
3296 case 1:
3297 for (;;)
3298 {
3299 *dp = *sp;
3300
3301 if (row_width <= bytes_to_jump)
3302 return;
3303
3304 dp += bytes_to_jump;
3305 sp += bytes_to_jump;
3306 row_width -= bytes_to_jump;
3307 }
3308
3309 case 2:
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003310 /* There is a possibility of a partial copy at the end here; this
John Bowler4e68aa72011-10-11 16:01:33 -05003311 * slows the code down somewhat.
3312 */
3313 do
3314 {
3315 dp[0] = sp[0], dp[1] = sp[1];
3316
3317 if (row_width <= bytes_to_jump)
3318 return;
3319
3320 sp += bytes_to_jump;
3321 dp += bytes_to_jump;
3322 row_width -= bytes_to_jump;
3323 }
3324 while (row_width > 1);
3325
3326 /* And there can only be one byte left at this point: */
3327 *dp = *sp;
3328 return;
3329
3330 case 3:
3331 /* This can only be the RGB case, so each copy is exactly one
3332 * pixel and it is not necessary to check for a partial copy.
3333 */
3334 for(;;)
3335 {
3336 dp[0] = sp[0], dp[1] = sp[1], dp[2] = sp[2];
3337
3338 if (row_width <= bytes_to_jump)
3339 return;
3340
3341 sp += bytes_to_jump;
3342 dp += bytes_to_jump;
3343 row_width -= bytes_to_jump;
3344 }
3345
3346 default:
3347#if PNG_ALIGN_TYPE != PNG_ALIGN_NONE
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003348 /* Check for double byte alignment and, if possible, use a
3349 * 16-bit copy. Don't attempt this for narrow images - ones that
John Bowler4e68aa72011-10-11 16:01:33 -05003350 * are less than an interlace panel wide. Don't attempt it for
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003351 * wide bytes_to_copy either - use the memcpy there.
John Bowler4e68aa72011-10-11 16:01:33 -05003352 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003353 if (bytes_to_copy < 16 /*else use memcpy*/ &&
John Bowler4e68aa72011-10-11 16:01:33 -05003354 png_isaligned(dp, png_uint_16) &&
3355 png_isaligned(sp, png_uint_16) &&
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003356 bytes_to_copy % (sizeof (png_uint_16)) == 0 &&
3357 bytes_to_jump % (sizeof (png_uint_16)) == 0)
John Bowler4e68aa72011-10-11 16:01:33 -05003358 {
3359 /* Everything is aligned for png_uint_16 copies, but try for
3360 * png_uint_32 first.
3361 */
3362 if (png_isaligned(dp, png_uint_32) &&
3363 png_isaligned(sp, png_uint_32) &&
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003364 bytes_to_copy % (sizeof (png_uint_32)) == 0 &&
3365 bytes_to_jump % (sizeof (png_uint_32)) == 0)
John Bowler4e68aa72011-10-11 16:01:33 -05003366 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003367 png_uint_32p dp32 = png_aligncast(png_uint_32p,dp);
3368 png_const_uint_32p sp32 = png_aligncastconst(
3369 png_const_uint_32p, sp);
John Bowler2286a7c2013-03-10 21:35:35 -05003370 size_t skip = (bytes_to_jump-bytes_to_copy) /
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003371 (sizeof (png_uint_32));
John Bowler4e68aa72011-10-11 16:01:33 -05003372
3373 do
3374 {
3375 size_t c = bytes_to_copy;
3376 do
3377 {
3378 *dp32++ = *sp32++;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003379 c -= (sizeof (png_uint_32));
John Bowler4e68aa72011-10-11 16:01:33 -05003380 }
3381 while (c > 0);
3382
3383 if (row_width <= bytes_to_jump)
3384 return;
3385
3386 dp32 += skip;
3387 sp32 += skip;
3388 row_width -= bytes_to_jump;
3389 }
3390 while (bytes_to_copy <= row_width);
3391
3392 /* Get to here when the row_width truncates the final copy.
3393 * There will be 1-3 bytes left to copy, so don't try the
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003394 * 16-bit loop below.
John Bowler4e68aa72011-10-11 16:01:33 -05003395 */
3396 dp = (png_bytep)dp32;
3397 sp = (png_const_bytep)sp32;
3398 do
3399 *dp++ = *sp++;
3400 while (--row_width > 0);
3401 return;
3402 }
3403
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003404 /* Else do it in 16-bit quantities, but only if the size is
John Bowler4e68aa72011-10-11 16:01:33 -05003405 * not too large.
3406 */
3407 else
3408 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003409 png_uint_16p dp16 = png_aligncast(png_uint_16p, dp);
3410 png_const_uint_16p sp16 = png_aligncastconst(
3411 png_const_uint_16p, sp);
John Bowlercaa3f292013-03-10 21:40:27 -05003412 size_t skip = (bytes_to_jump-bytes_to_copy) /
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003413 (sizeof (png_uint_16));
John Bowler4e68aa72011-10-11 16:01:33 -05003414
3415 do
3416 {
3417 size_t c = bytes_to_copy;
3418 do
3419 {
3420 *dp16++ = *sp16++;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003421 c -= (sizeof (png_uint_16));
John Bowler4e68aa72011-10-11 16:01:33 -05003422 }
3423 while (c > 0);
3424
3425 if (row_width <= bytes_to_jump)
3426 return;
3427
3428 dp16 += skip;
3429 sp16 += skip;
3430 row_width -= bytes_to_jump;
3431 }
3432 while (bytes_to_copy <= row_width);
3433
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003434 /* End of row - 1 byte left, bytes_to_copy > row_width: */
John Bowler4e68aa72011-10-11 16:01:33 -05003435 dp = (png_bytep)dp16;
3436 sp = (png_const_bytep)sp16;
3437 do
3438 *dp++ = *sp++;
3439 while (--row_width > 0);
3440 return;
3441 }
3442 }
3443#endif /* PNG_ALIGN_ code */
3444
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003445 /* The true default - use a memcpy: */
John Bowler4e68aa72011-10-11 16:01:33 -05003446 for (;;)
3447 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003448 memcpy(dp, sp, bytes_to_copy);
John Bowler4e68aa72011-10-11 16:01:33 -05003449
3450 if (row_width <= bytes_to_jump)
3451 return;
3452
3453 sp += bytes_to_jump;
3454 dp += bytes_to_jump;
3455 row_width -= bytes_to_jump;
3456 if (bytes_to_copy > row_width)
3457 bytes_to_copy = row_width;
3458 }
3459 }
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003460
3461 /* NOT REACHED*/
John Bowler4e68aa72011-10-11 16:01:33 -05003462 } /* pixel_depth >= 8 */
3463
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003464 /* Here if pixel_depth < 8 to check 'end_ptr' below. */
Guy Schalnat0d580581995-07-20 02:43:20 -05003465 }
John Bowler4e68aa72011-10-11 16:01:33 -05003466 else
Glenn Randers-Pehrson72855fb2014-04-13 21:27:25 -05003467#endif /* PNG_READ_INTERLACING_SUPPORTED */
John Bowlerac8375d2011-10-06 22:27:16 -05003468
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003469 /* If here then the switch above wasn't used so just memcpy the whole row
John Bowler4e68aa72011-10-11 16:01:33 -05003470 * from the temporary row buffer (notice that this overwrites the end of the
3471 * destination row if it is a partial byte.)
John Bowlerac8375d2011-10-06 22:27:16 -05003472 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003473 memcpy(dp, sp, PNG_ROWBYTES(pixel_depth, row_width));
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003474
3475 /* Restore the overwritten bits from the last byte if necessary. */
3476 if (end_ptr != NULL)
3477 *end_ptr = (png_byte)((end_byte & end_mask) | (*end_ptr & ~end_mask));
Guy Schalnat0d580581995-07-20 02:43:20 -05003478}
3479
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06003480#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05003481void /* PRIVATE */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003482png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
3483 png_uint_32 transformations /* Because these may affect the byte layout */)
Guy Schalnat0d580581995-07-20 02:43:20 -05003484{
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003485 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
3486 /* Offset to next interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003487 static PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003488
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05003489 png_debug(1, "in png_do_read_interlace");
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003490 if (row != NULL && row_info != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -05003491 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003492 png_uint_32 final_width;
3493
3494 final_width = row_info->width * png_pass_inc[pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05003495
3496 switch (row_info->pixel_depth)
3497 {
3498 case 1:
3499 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003500 png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3);
3501 png_bytep dp = row + (png_size_t)((final_width - 1) >> 3);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003502 int sshift, dshift;
3503 int s_start, s_end, s_inc;
3504 int jstop = png_pass_inc[pass];
3505 png_byte v;
Guy Schalnat0d580581995-07-20 02:43:20 -05003506 png_uint_32 i;
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003507 int j;
Guy Schalnat0d580581995-07-20 02:43:20 -05003508
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003509#ifdef PNG_READ_PACKSWAP_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003510 if (transformations & PNG_PACKSWAP)
3511 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003512 sshift = (int)((row_info->width + 7) & 0x07);
3513 dshift = (int)((final_width + 7) & 0x07);
3514 s_start = 7;
3515 s_end = 0;
3516 s_inc = -1;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003517 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003518
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003519 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05003520#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003521 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003522 sshift = 7 - (int)((row_info->width + 7) & 0x07);
3523 dshift = 7 - (int)((final_width + 7) & 0x07);
3524 s_start = 0;
3525 s_end = 7;
3526 s_inc = 1;
3527 }
Glenn Randers-Pehrson5dd2b8e2004-11-24 07:50:16 -06003528
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003529 for (i = 0; i < row_info->width; i++)
3530 {
3531 v = (png_byte)((*sp >> sshift) & 0x01);
3532 for (j = 0; j < jstop; j++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003533 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003534 unsigned int tmp = *dp & (0x7f7f >> (7 - dshift));
3535 tmp |= v << dshift;
3536 *dp = (png_byte)(tmp & 0xff);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003537
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003538 if (dshift == s_end)
3539 {
3540 dshift = s_start;
3541 dp--;
3542 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003543
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003544 else
3545 dshift += s_inc;
3546 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003547
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003548 if (sshift == s_end)
3549 {
3550 sshift = s_start;
Guy Schalnat0d580581995-07-20 02:43:20 -05003551 sp--;
3552 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003553
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003554 else
3555 sshift += s_inc;
Guy Schalnat0d580581995-07-20 02:43:20 -05003556 }
3557 break;
3558 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003559
Guy Schalnat0d580581995-07-20 02:43:20 -05003560 case 2:
3561 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003562 png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);
3563 png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);
3564 int sshift, dshift;
3565 int s_start, s_end, s_inc;
3566 int jstop = png_pass_inc[pass];
Andreas Dilger47a0c421997-05-16 02:46:07 -05003567 png_uint_32 i;
Guy Schalnat0d580581995-07-20 02:43:20 -05003568
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003569#ifdef PNG_READ_PACKSWAP_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003570 if (transformations & PNG_PACKSWAP)
3571 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003572 sshift = (int)(((row_info->width + 3) & 0x03) << 1);
3573 dshift = (int)(((final_width + 3) & 0x03) << 1);
3574 s_start = 6;
3575 s_end = 0;
3576 s_inc = -2;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003577 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003578
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003579 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05003580#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003581 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003582 sshift = (int)((3 - ((row_info->width + 3) & 0x03)) << 1);
3583 dshift = (int)((3 - ((final_width + 3) & 0x03)) << 1);
3584 s_start = 0;
3585 s_end = 6;
3586 s_inc = 2;
3587 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05003588
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003589 for (i = 0; i < row_info->width; i++)
3590 {
3591 png_byte v;
3592 int j;
3593
3594 v = (png_byte)((*sp >> sshift) & 0x03);
3595 for (j = 0; j < jstop; j++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003596 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003597 unsigned int tmp = *dp & (0x3f3f >> (6 - dshift));
3598 tmp |= v << dshift;
3599 *dp = (png_byte)(tmp & 0xff);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003600
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003601 if (dshift == s_end)
3602 {
3603 dshift = s_start;
3604 dp--;
3605 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003606
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003607 else
3608 dshift += s_inc;
3609 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003610
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003611 if (sshift == s_end)
3612 {
3613 sshift = s_start;
Guy Schalnat0d580581995-07-20 02:43:20 -05003614 sp--;
3615 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003616
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003617 else
3618 sshift += s_inc;
Guy Schalnat0d580581995-07-20 02:43:20 -05003619 }
3620 break;
3621 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003622
Guy Schalnat0d580581995-07-20 02:43:20 -05003623 case 4:
3624 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003625 png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1);
3626 png_bytep dp = row + (png_size_t)((final_width - 1) >> 1);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003627 int sshift, dshift;
3628 int s_start, s_end, s_inc;
Guy Schalnat0d580581995-07-20 02:43:20 -05003629 png_uint_32 i;
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003630 int jstop = png_pass_inc[pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05003631
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003632#ifdef PNG_READ_PACKSWAP_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003633 if (transformations & PNG_PACKSWAP)
3634 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003635 sshift = (int)(((row_info->width + 1) & 0x01) << 2);
3636 dshift = (int)(((final_width + 1) & 0x01) << 2);
3637 s_start = 4;
3638 s_end = 0;
3639 s_inc = -4;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003640 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003641
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003642 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05003643#endif
3644 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003645 sshift = (int)((1 - ((row_info->width + 1) & 0x01)) << 2);
3646 dshift = (int)((1 - ((final_width + 1) & 0x01)) << 2);
3647 s_start = 0;
3648 s_end = 4;
3649 s_inc = 4;
3650 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05003651
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003652 for (i = 0; i < row_info->width; i++)
3653 {
Glenn Randers-Pehrson8db19982011-10-27 16:17:24 -05003654 png_byte v = (png_byte)((*sp >> sshift) & 0x0f);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003655 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003656
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003657 for (j = 0; j < jstop; j++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003658 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003659 unsigned int tmp = *dp & (0xf0f >> (4 - dshift));
3660 tmp |= v << dshift;
3661 *dp = (png_byte)(tmp & 0xff);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003662
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003663 if (dshift == s_end)
3664 {
3665 dshift = s_start;
3666 dp--;
3667 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003668
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003669 else
3670 dshift += s_inc;
3671 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003672
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003673 if (sshift == s_end)
3674 {
3675 sshift = s_start;
Guy Schalnat0d580581995-07-20 02:43:20 -05003676 sp--;
3677 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003678
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003679 else
3680 sshift += s_inc;
Guy Schalnat0d580581995-07-20 02:43:20 -05003681 }
3682 break;
3683 }
John Bowlerac8375d2011-10-06 22:27:16 -05003684
Guy Schalnat0d580581995-07-20 02:43:20 -05003685 default:
3686 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003687 png_size_t pixel_bytes = (row_info->pixel_depth >> 3);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003688
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06003689 png_bytep sp = row + (png_size_t)(row_info->width - 1)
3690 * pixel_bytes;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003691
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003692 png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05003693
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003694 int jstop = png_pass_inc[pass];
3695 png_uint_32 i;
3696
3697 for (i = 0; i < row_info->width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003698 {
Glenn Randers-Pehrsondb56fa12013-07-15 10:09:54 -05003699 png_byte v[8]; /* SAFE; pixel_depth does not exceed 64 */
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003700 int j;
3701
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003702 memcpy(v, sp, pixel_bytes);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003703
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003704 for (j = 0; j < jstop; j++)
3705 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003706 memcpy(dp, v, pixel_bytes);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003707 dp -= pixel_bytes;
3708 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003709
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003710 sp -= pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05003711 }
3712 break;
3713 }
3714 }
John Bowlerac8375d2011-10-06 22:27:16 -05003715
Guy Schalnat0d580581995-07-20 02:43:20 -05003716 row_info->width = final_width;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003717 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width);
Guy Schalnat0d580581995-07-20 02:43:20 -05003718 }
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -05003719#ifndef PNG_READ_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003720 PNG_UNUSED(transformations) /* Silence compiler warning */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05003721#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003722}
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06003723#endif /* PNG_READ_INTERLACING_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05003724
Mans Rullgardd3a94802011-11-03 00:47:55 -05003725static void
3726png_read_filter_row_sub(png_row_infop row_info, png_bytep row,
3727 png_const_bytep prev_row)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003728{
Mans Rullgardd3a94802011-11-03 00:47:55 -05003729 png_size_t i;
3730 png_size_t istop = row_info->rowbytes;
3731 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
3732 png_bytep rp = row + bpp;
Mans Rullgardd3a94802011-11-03 00:47:55 -05003733
3734 PNG_UNUSED(prev_row)
3735
3736 for (i = bpp; i < istop; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003737 {
John Bowler7875d532011-11-07 22:33:49 -06003738 *rp = (png_byte)(((int)(*rp) + (int)(*(rp-bpp))) & 0xff);
Mans Rullgardd3a94802011-11-03 00:47:55 -05003739 rp++;
3740 }
3741}
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003742
Mans Rullgardd3a94802011-11-03 00:47:55 -05003743static void
3744png_read_filter_row_up(png_row_infop row_info, png_bytep row,
3745 png_const_bytep prev_row)
3746{
3747 png_size_t i;
3748 png_size_t istop = row_info->rowbytes;
3749 png_bytep rp = row;
3750 png_const_bytep pp = prev_row;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003751
Mans Rullgardd3a94802011-11-03 00:47:55 -05003752 for (i = 0; i < istop; i++)
3753 {
3754 *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
3755 rp++;
3756 }
3757}
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003758
Mans Rullgardd3a94802011-11-03 00:47:55 -05003759static void
3760png_read_filter_row_avg(png_row_infop row_info, png_bytep row,
3761 png_const_bytep prev_row)
3762{
3763 png_size_t i;
3764 png_bytep rp = row;
3765 png_const_bytep pp = prev_row;
Mans Rullgardd3a94802011-11-03 00:47:55 -05003766 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
3767 png_size_t istop = row_info->rowbytes - bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003768
Mans Rullgardd3a94802011-11-03 00:47:55 -05003769 for (i = 0; i < bpp; i++)
3770 {
3771 *rp = (png_byte)(((int)(*rp) +
3772 ((int)(*pp++) / 2 )) & 0xff);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003773
Mans Rullgardd3a94802011-11-03 00:47:55 -05003774 rp++;
3775 }
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003776
Mans Rullgardd3a94802011-11-03 00:47:55 -05003777 for (i = 0; i < istop; i++)
3778 {
3779 *rp = (png_byte)(((int)(*rp) +
John Bowler7875d532011-11-07 22:33:49 -06003780 (int)(*pp++ + *(rp-bpp)) / 2 ) & 0xff);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003781
Mans Rullgardd3a94802011-11-03 00:47:55 -05003782 rp++;
3783 }
3784}
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003785
Mans Rullgardd3a94802011-11-03 00:47:55 -05003786static void
John Bowlerfcc02632011-11-03 18:31:00 -05003787png_read_filter_row_paeth_1byte_pixel(png_row_infop row_info, png_bytep row,
Mans Rullgardd3a94802011-11-03 00:47:55 -05003788 png_const_bytep prev_row)
3789{
John Bowlerfcc02632011-11-03 18:31:00 -05003790 png_bytep rp_end = row + row_info->rowbytes;
3791 int a, c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003792
John Bowlerfcc02632011-11-03 18:31:00 -05003793 /* First pixel/byte */
3794 c = *prev_row++;
3795 a = *row + c;
3796 *row++ = (png_byte)a;
3797
3798 /* Remainder */
3799 while (row < rp_end)
Mans Rullgardd3a94802011-11-03 00:47:55 -05003800 {
John Bowlerfcc02632011-11-03 18:31:00 -05003801 int b, pa, pb, pc, p;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003802
John Bowlerfcc02632011-11-03 18:31:00 -05003803 a &= 0xff; /* From previous iteration or start */
3804 b = *prev_row++;
Mans Rullgardd3a94802011-11-03 00:47:55 -05003805
3806 p = b - c;
3807 pc = a - c;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003808
John Bowlerfcc02632011-11-03 18:31:00 -05003809# ifdef PNG_USE_ABS
3810 pa = abs(p);
3811 pb = abs(pc);
3812 pc = abs(p + pc);
3813# else
3814 pa = p < 0 ? -p : p;
3815 pb = pc < 0 ? -pc : pc;
3816 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
3817# endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003818
John Bowlerfcc02632011-11-03 18:31:00 -05003819 /* Find the best predictor, the least of pa, pb, pc favoring the earlier
3820 * ones in the case of a tie.
3821 */
3822 if (pb < pa) pa = pb, a = b;
3823 if (pc < pa) a = c;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003824
John Bowlerfcc02632011-11-03 18:31:00 -05003825 /* Calculate the current pixel in a, and move the previous row pixel to c
3826 * for the next time round the loop
3827 */
3828 c = b;
3829 a += *row;
3830 *row++ = (png_byte)a;
3831 }
3832}
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003833
John Bowlerfcc02632011-11-03 18:31:00 -05003834static void
3835png_read_filter_row_paeth_multibyte_pixel(png_row_infop row_info, png_bytep row,
3836 png_const_bytep prev_row)
3837{
3838 int bpp = (row_info->pixel_depth + 7) >> 3;
3839 png_bytep rp_end = row + bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003840
John Bowlerfcc02632011-11-03 18:31:00 -05003841 /* Process the first pixel in the row completely (this is the same as 'up'
3842 * because there is only one candidate predictor for the first row).
3843 */
3844 while (row < rp_end)
3845 {
3846 int a = *row + *prev_row++;
3847 *row++ = (png_byte)a;
3848 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003849
John Bowlerfcc02632011-11-03 18:31:00 -05003850 /* Remainder */
3851 rp_end += row_info->rowbytes - bpp;
3852
3853 while (row < rp_end)
3854 {
3855 int a, b, c, pa, pb, pc, p;
3856
3857 c = *(prev_row - bpp);
3858 a = *(row - bpp);
3859 b = *prev_row++;
3860
3861 p = b - c;
3862 pc = a - c;
3863
3864# ifdef PNG_USE_ABS
3865 pa = abs(p);
3866 pb = abs(pc);
3867 pc = abs(p + pc);
3868# else
3869 pa = p < 0 ? -p : p;
3870 pb = pc < 0 ? -pc : pc;
3871 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
3872# endif
3873
3874 if (pb < pa) pa = pb, a = b;
3875 if (pc < pa) a = c;
3876
John Bowlerfcc02632011-11-03 18:31:00 -05003877 a += *row;
3878 *row++ = (png_byte)a;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003879 }
3880}
Guy Schalnat0d580581995-07-20 02:43:20 -05003881
Mans Rullgardd3a94802011-11-03 00:47:55 -05003882static void
John Bowler5d567862011-12-24 09:12:00 -06003883png_init_filter_functions(png_structrp pp)
Glenn Randers-Pehrson32440202013-08-21 18:01:32 -05003884 /* This function is called once for every PNG image (except for PNG images
3885 * that only use PNG_FILTER_VALUE_NONE for all rows) to set the
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003886 * implementations required to reverse the filtering of PNG rows. Reversing
3887 * the filter is the first transformation performed on the row data. It is
3888 * performed in place, therefore an implementation can be selected based on
3889 * the image pixel format. If the implementation depends on image width then
Glenn Randers-Pehrson6e1c74b2013-04-22 11:06:26 -05003890 * take care to ensure that it works correctly if the image is interlaced -
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003891 * interlacing causes the actual row width to vary.
3892 */
Mans Rullgardd3a94802011-11-03 00:47:55 -05003893{
John Bowlerfcc02632011-11-03 18:31:00 -05003894 unsigned int bpp = (pp->pixel_depth + 7) >> 3;
3895
Mans Rullgardd3a94802011-11-03 00:47:55 -05003896 pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub;
3897 pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up;
3898 pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg;
John Bowlerfcc02632011-11-03 18:31:00 -05003899 if (bpp == 1)
3900 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
3901 png_read_filter_row_paeth_1byte_pixel;
3902 else
3903 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
3904 png_read_filter_row_paeth_multibyte_pixel;
Mans Rullgardd3a94802011-11-03 00:47:55 -05003905
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003906#ifdef PNG_FILTER_OPTIMIZATIONS
3907 /* To use this define PNG_FILTER_OPTIMIZATIONS as the name of a function to
3908 * call to install hardware optimizations for the above functions; simply
3909 * replace whatever elements of the pp->read_filter[] array with a hardware
3910 * specific (or, for that matter, generic) optimization.
3911 *
3912 * To see an example of this examine what configure.ac does when
3913 * --enable-arm-neon is specified on the command line.
3914 */
3915 PNG_FILTER_OPTIMIZATIONS(pp, bpp);
Mans Rullgardd3a94802011-11-03 00:47:55 -05003916#endif
3917}
3918
3919void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06003920png_read_filter_row(png_structrp pp, png_row_infop row_info, png_bytep row,
Mans Rullgardd3a94802011-11-03 00:47:55 -05003921 png_const_bytep prev_row, int filter)
3922{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003923 /* OPTIMIZATION: DO NOT MODIFY THIS FUNCTION, instead #define
3924 * PNG_FILTER_OPTIMIZATIONS to a function that overrides the generic
3925 * implementations. See png_init_filter_functions above.
3926 */
Mans Rullgardd3a94802011-11-03 00:47:55 -05003927 if (filter > PNG_FILTER_VALUE_NONE && filter < PNG_FILTER_VALUE_LAST)
Glenn Randers-Pehrson685d79e2013-08-20 21:15:31 -05003928 {
3929 if (pp->read_filter[0] == NULL)
3930 png_init_filter_functions(pp);
3931
Mans Rullgardd3a94802011-11-03 00:47:55 -05003932 pp->read_filter[filter-1](row_info, row, prev_row);
Glenn Randers-Pehrson685d79e2013-08-20 21:15:31 -05003933 }
Mans Rullgardd3a94802011-11-03 00:47:55 -05003934}
3935
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05003936#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05003937void /* PRIVATE */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003938png_read_IDAT_data(png_structrp png_ptr, png_bytep output,
3939 png_alloc_size_t avail_out)
3940{
3941 /* Loop reading IDATs and decompressing the result into output[avail_out] */
3942 png_ptr->zstream.next_out = output;
3943 png_ptr->zstream.avail_out = 0; /* safety: set below */
3944
3945 if (output == NULL)
3946 avail_out = 0;
3947
3948 do
3949 {
3950 int ret;
3951 png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];
3952
3953 if (png_ptr->zstream.avail_in == 0)
3954 {
3955 uInt avail_in;
3956 png_bytep buffer;
3957
3958 while (png_ptr->idat_size == 0)
3959 {
3960 png_crc_finish(png_ptr, 0);
3961
3962 png_ptr->idat_size = png_read_chunk_header(png_ptr);
3963 /* This is an error even in the 'check' case because the code just
3964 * consumed a non-IDAT header.
3965 */
3966 if (png_ptr->chunk_name != png_IDAT)
3967 png_error(png_ptr, "Not enough image data");
3968 }
3969
3970 avail_in = png_ptr->IDAT_read_size;
3971
3972 if (avail_in > png_ptr->idat_size)
3973 avail_in = (uInt)png_ptr->idat_size;
3974
3975 /* A PNG with a gradually increasing IDAT size will defeat this attempt
3976 * to minimize memory usage by causing lots of re-allocs, but
3977 * realistically doing IDAT_read_size re-allocs is not likely to be a
3978 * big problem.
3979 */
3980 buffer = png_read_buffer(png_ptr, avail_in, 0/*error*/);
3981
3982 png_crc_read(png_ptr, buffer, avail_in);
3983 png_ptr->idat_size -= avail_in;
3984
3985 png_ptr->zstream.next_in = buffer;
3986 png_ptr->zstream.avail_in = avail_in;
3987 }
3988
3989 /* And set up the output side. */
3990 if (output != NULL) /* standard read */
3991 {
3992 uInt out = ZLIB_IO_MAX;
3993
3994 if (out > avail_out)
3995 out = (uInt)avail_out;
3996
3997 avail_out -= out;
3998 png_ptr->zstream.avail_out = out;
3999 }
4000
John Bowler43c07e12013-04-07 21:33:30 -05004001 else /* after last row, checking for end */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004002 {
4003 png_ptr->zstream.next_out = tmpbuf;
4004 png_ptr->zstream.avail_out = (sizeof tmpbuf);
4005 }
4006
4007 /* Use NO_FLUSH; this gives zlib the maximum opportunity to optimize the
4008 * process. If the LZ stream is truncated the sequential reader will
4009 * terminally damage the stream, above, by reading the chunk header of the
4010 * following chunk (it then exits with png_error).
4011 *
4012 * TODO: deal more elegantly with truncated IDAT lists.
4013 */
4014 ret = inflate(&png_ptr->zstream, Z_NO_FLUSH);
4015
John Bowler43c07e12013-04-07 21:33:30 -05004016 /* Take the unconsumed output back. */
4017 if (output != NULL)
4018 avail_out += png_ptr->zstream.avail_out;
4019
4020 else /* avail_out counts the extra bytes */
4021 avail_out += (sizeof tmpbuf) - png_ptr->zstream.avail_out;
4022
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004023 png_ptr->zstream.avail_out = 0;
4024
4025 if (ret == Z_STREAM_END)
4026 {
4027 /* Do this for safety; we won't read any more into this row. */
4028 png_ptr->zstream.next_out = NULL;
4029
4030 png_ptr->mode |= PNG_AFTER_IDAT;
4031 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
4032
4033 if (png_ptr->zstream.avail_in > 0 || png_ptr->idat_size > 0)
4034 png_chunk_benign_error(png_ptr, "Extra compressed data");
4035 break;
4036 }
4037
4038 if (ret != Z_OK)
4039 {
4040 png_zstream_error(png_ptr, ret);
4041
4042 if (output != NULL)
4043 png_chunk_error(png_ptr, png_ptr->zstream.msg);
4044
4045 else /* checking */
4046 {
4047 png_chunk_benign_error(png_ptr, png_ptr->zstream.msg);
4048 return;
4049 }
4050 }
4051 } while (avail_out > 0);
4052
4053 if (avail_out > 0)
4054 {
4055 /* The stream ended before the image; this is the same as too few IDATs so
4056 * should be handled the same way.
4057 */
4058 if (output != NULL)
4059 png_error(png_ptr, "Not enough image data");
4060
John Bowler43c07e12013-04-07 21:33:30 -05004061 else /* the deflate stream contained extra data */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004062 png_chunk_benign_error(png_ptr, "Too much image data");
4063 }
4064}
4065
4066void /* PRIVATE */
4067png_read_finish_IDAT(png_structrp png_ptr)
4068{
4069 /* We don't need any more data and the stream should have ended, however the
4070 * LZ end code may actually not have been processed. In this case we must
4071 * read it otherwise stray unread IDAT data or, more likely, an IDAT chunk
4072 * may still remain to be consumed.
4073 */
4074 if (!(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED))
4075 {
4076 /* The NULL causes png_read_IDAT_data to swallow any remaining bytes in
4077 * the compressed stream, but the stream may be damaged too, so even after
4078 * this call we may need to terminate the zstream ownership.
4079 */
4080 png_read_IDAT_data(png_ptr, NULL, 0);
4081 png_ptr->zstream.next_out = NULL; /* safety */
4082
4083 /* Now clear everything out for safety; the following may not have been
4084 * done.
4085 */
4086 if (!(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED))
4087 {
4088 png_ptr->mode |= PNG_AFTER_IDAT;
4089 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
4090 }
4091 }
4092
4093 /* If the zstream has not been released do it now *and* terminate the reading
4094 * of the final IDAT chunk.
4095 */
4096 if (png_ptr->zowner == png_IDAT)
4097 {
4098 /* Always do this; the pointers otherwise point into the read buffer. */
4099 png_ptr->zstream.next_in = NULL;
4100 png_ptr->zstream.avail_in = 0;
4101
4102 /* Now we no longer own the zstream. */
4103 png_ptr->zowner = 0;
4104
4105 /* The slightly weird semantics of the sequential IDAT reading is that we
4106 * are always in or at the end of an IDAT chunk, so we always need to do a
4107 * crc_finish here. If idat_size is non-zero we also need to read the
4108 * spurious bytes at the end of the chunk now.
4109 */
4110 (void)png_crc_finish(png_ptr, png_ptr->idat_size);
4111 }
4112}
4113
4114void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06004115png_read_finish_row(png_structrp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05004116{
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004117 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004118
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004119 /* Start of interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004120 static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004121
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004122 /* Offset to next interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004123 static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004124
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004125 /* Start of interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004126 static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004127
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004128 /* Offset to next interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004129 static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004130
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05004131 png_debug(1, "in png_read_finish_row");
Guy Schalnat0d580581995-07-20 02:43:20 -05004132 png_ptr->row_number++;
4133 if (png_ptr->row_number < png_ptr->num_rows)
4134 return;
4135
4136 if (png_ptr->interlaced)
4137 {
4138 png_ptr->row_number = 0;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004139
Glenn Randers-Pehrson435cf872011-10-05 16:23:53 -05004140 /* TO DO: don't do this if prev_row isn't needed (requires
4141 * read-ahead of the next row's filter byte.
4142 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004143 memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004144
Guy Schalnat0d580581995-07-20 02:43:20 -05004145 do
4146 {
4147 png_ptr->pass++;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004148
Guy Schalnat0d580581995-07-20 02:43:20 -05004149 if (png_ptr->pass >= 7)
4150 break;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004151
Guy Schalnat0d580581995-07-20 02:43:20 -05004152 png_ptr->iwidth = (png_ptr->width +
4153 png_pass_inc[png_ptr->pass] - 1 -
4154 png_pass_start[png_ptr->pass]) /
4155 png_pass_inc[png_ptr->pass];
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05004156
Guy Schalnat0d580581995-07-20 02:43:20 -05004157 if (!(png_ptr->transformations & PNG_INTERLACE))
4158 {
4159 png_ptr->num_rows = (png_ptr->height +
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004160 png_pass_yinc[png_ptr->pass] - 1 -
4161 png_pass_ystart[png_ptr->pass]) /
4162 png_pass_yinc[png_ptr->pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05004163 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004164
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -05004165 else /* if (png_ptr->transformations & PNG_INTERLACE) */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004166 break; /* libpng deinterlacing sees every row */
4167
4168 } while (png_ptr->num_rows == 0 || png_ptr->iwidth == 0);
Guy Schalnat0d580581995-07-20 02:43:20 -05004169
4170 if (png_ptr->pass < 7)
4171 return;
4172 }
4173
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004174 /* Here after at the end of the last row of the last pass. */
4175 png_read_finish_IDAT(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05004176}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05004177#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05004178
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05004179void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06004180png_read_start_row(png_structrp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05004181{
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004182 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004183
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004184 /* Start of interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004185 static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004186
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004187 /* Offset to next interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004188 static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004189
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004190 /* Start of interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004191 static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004192
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004193 /* Offset to next interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004194 static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004195
Guy Schalnat0d580581995-07-20 02:43:20 -05004196 int max_pixel_depth;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05004197 png_size_t row_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05004198
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05004199 png_debug(1, "in png_read_start_row");
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004200
John Bowler4a12f4a2011-04-17 18:34:22 -05004201#ifdef PNG_READ_TRANSFORMS_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05004202 png_init_read_transformations(png_ptr);
John Bowler4a12f4a2011-04-17 18:34:22 -05004203#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05004204 if (png_ptr->interlaced)
4205 {
4206 if (!(png_ptr->transformations & PNG_INTERLACE))
4207 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004208 png_pass_ystart[0]) / png_pass_yinc[0];
4209
Guy Schalnat0d580581995-07-20 02:43:20 -05004210 else
4211 png_ptr->num_rows = png_ptr->height;
4212
4213 png_ptr->iwidth = (png_ptr->width +
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004214 png_pass_inc[png_ptr->pass] - 1 -
4215 png_pass_start[png_ptr->pass]) /
4216 png_pass_inc[png_ptr->pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05004217 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004218
Guy Schalnat0d580581995-07-20 02:43:20 -05004219 else
4220 {
4221 png_ptr->num_rows = png_ptr->height;
4222 png_ptr->iwidth = png_ptr->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05004223 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004224
Guy Schalnat0d580581995-07-20 02:43:20 -05004225 max_pixel_depth = png_ptr->pixel_depth;
4226
John Bowlere6fb6912011-11-08 21:35:16 -06004227 /* WARNING: * png_read_transform_info (pngrtran.c) performs a simpliar set of
4228 * calculations to calculate the final pixel depth, then
4229 * png_do_read_transforms actually does the transforms. This means that the
4230 * code which effectively calculates this value is actually repeated in three
4231 * separate places. They must all match. Innocent changes to the order of
4232 * transformations can and will break libpng in a way that causes memory
4233 * overwrites.
4234 *
4235 * TODO: fix this.
4236 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004237#ifdef PNG_READ_PACK_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05004238 if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8)
Guy Schalnat0d580581995-07-20 02:43:20 -05004239 max_pixel_depth = 8;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004240#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05004241
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004242#ifdef PNG_READ_EXPAND_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -05004243 if (png_ptr->transformations & PNG_EXPAND)
Guy Schalnat0d580581995-07-20 02:43:20 -05004244 {
4245 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
4246 {
4247 if (png_ptr->num_trans)
4248 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004249
Guy Schalnat0d580581995-07-20 02:43:20 -05004250 else
4251 max_pixel_depth = 24;
4252 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004253
Guy Schalnat0d580581995-07-20 02:43:20 -05004254 else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
4255 {
4256 if (max_pixel_depth < 8)
4257 max_pixel_depth = 8;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004258
Guy Schalnat0d580581995-07-20 02:43:20 -05004259 if (png_ptr->num_trans)
4260 max_pixel_depth *= 2;
4261 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004262
Guy Schalnat0d580581995-07-20 02:43:20 -05004263 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
4264 {
4265 if (png_ptr->num_trans)
4266 {
4267 max_pixel_depth *= 4;
4268 max_pixel_depth /= 3;
4269 }
4270 }
4271 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004272#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05004273
John Bowler4d562962011-02-12 09:01:20 -06004274#ifdef PNG_READ_EXPAND_16_SUPPORTED
4275 if (png_ptr->transformations & PNG_EXPAND_16)
4276 {
4277# ifdef PNG_READ_EXPAND_SUPPORTED
4278 /* In fact it is an error if it isn't supported, but checking is
4279 * the safe way.
4280 */
4281 if (png_ptr->transformations & PNG_EXPAND)
4282 {
4283 if (png_ptr->bit_depth < 16)
4284 max_pixel_depth *= 2;
4285 }
4286 else
4287# endif
4288 png_ptr->transformations &= ~PNG_EXPAND_16;
4289 }
4290#endif
4291
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004292#ifdef PNG_READ_FILLER_SUPPORTED
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004293 if (png_ptr->transformations & (PNG_FILLER))
Guy Schalnat0d580581995-07-20 02:43:20 -05004294 {
John Bowlere6fb6912011-11-08 21:35:16 -06004295 if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004296 {
4297 if (max_pixel_depth <= 8)
4298 max_pixel_depth = 16;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004299
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004300 else
4301 max_pixel_depth = 32;
4302 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004303
John Bowlere6fb6912011-11-08 21:35:16 -06004304 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB ||
4305 png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004306 {
4307 if (max_pixel_depth <= 32)
4308 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004309
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004310 else
4311 max_pixel_depth = 64;
4312 }
Guy Schalnat0d580581995-07-20 02:43:20 -05004313 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004314#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05004315
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004316#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05004317 if (png_ptr->transformations & PNG_GRAY_TO_RGB)
4318 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06004319 if (
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004320#ifdef PNG_READ_EXPAND_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004321 (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) ||
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06004322#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004323#ifdef PNG_READ_FILLER_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004324 (png_ptr->transformations & (PNG_FILLER)) ||
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06004325#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004326 png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
Guy Schalnat0d580581995-07-20 02:43:20 -05004327 {
4328 if (max_pixel_depth <= 16)
4329 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004330
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06004331 else
Guy Schalnat0d580581995-07-20 02:43:20 -05004332 max_pixel_depth = 64;
4333 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004334
Guy Schalnat0d580581995-07-20 02:43:20 -05004335 else
4336 {
4337 if (max_pixel_depth <= 8)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004338 {
4339 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06004340 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004341
4342 else
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06004343 max_pixel_depth = 24;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004344 }
4345
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06004346 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4347 max_pixel_depth = 64;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004348
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06004349 else
Guy Schalnat0d580581995-07-20 02:43:20 -05004350 max_pixel_depth = 48;
4351 }
4352 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004353#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05004354
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05004355#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
4356defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004357 if (png_ptr->transformations & PNG_USER_TRANSFORM)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004358 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004359 int user_pixel_depth = png_ptr->user_transform_depth *
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05004360 png_ptr->user_transform_channels;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004361
4362 if (user_pixel_depth > max_pixel_depth)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004363 max_pixel_depth = user_pixel_depth;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004364 }
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05004365#endif
4366
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004367 /* This value is stored in png_struct and double checked in the row read
4368 * code.
4369 */
4370 png_ptr->maximum_pixel_depth = (png_byte)max_pixel_depth;
4371 png_ptr->transformed_pixel_depth = 0; /* calculated on demand */
4372
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004373 /* Align the width on the next larger 8 pixels. Mainly used
4374 * for interlacing
4375 */
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05004376 row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004377 /* Calculate the maximum bytes needed, adding a byte and a pixel
4378 * for safety's sake
4379 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004380 row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) +
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004381 1 + ((max_pixel_depth + 7) >> 3);
4382
Guy Schalnat0d580581995-07-20 02:43:20 -05004383#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05004384 if (row_bytes > (png_uint_32)65536L)
Guy Schalnate5a37791996-06-05 15:50:50 -05004385 png_error(png_ptr, "This image requires a row greater than 64KB");
Guy Schalnat0d580581995-07-20 02:43:20 -05004386#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004387
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06004388 if (row_bytes + 48 > png_ptr->old_big_row_buf_size)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004389 {
4390 png_free(png_ptr, png_ptr->big_row_buf);
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004391 png_free(png_ptr, png_ptr->big_prev_row);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004392
Glenn Randers-Pehrson6917b512009-03-09 15:31:08 -05004393 if (png_ptr->interlaced)
Glenn Randers-Pehrsona515d302010-01-01 10:24:25 -06004394 png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr,
4395 row_bytes + 48);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004396
Glenn Randers-Pehrsona515d302010-01-01 10:24:25 -06004397 else
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004398 png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004399
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004400 png_ptr->big_prev_row = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06004401
4402#ifdef PNG_ALIGNED_MEMORY_SUPPORTED
4403 /* Use 16-byte aligned memory for row_buf with at least 16 bytes
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004404 * of padding before and after row_buf; treat prev_row similarly.
John Bowlerac8375d2011-10-06 22:27:16 -05004405 * NOTE: the alignment is to the start of the pixels, one beyond the start
4406 * of the buffer, because of the filter byte. Prior to libpng 1.5.6 this
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004407 * was incorrect; the filter byte was aligned, which had the exact
4408 * opposite effect of that intended.
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06004409 */
John Bowlerac8375d2011-10-06 22:27:16 -05004410 {
4411 png_bytep temp = png_ptr->big_row_buf + 32;
Glenn Randers-Pehrson8db19982011-10-27 16:17:24 -05004412 int extra = (int)((temp - (png_bytep)0) & 0x0f);
John Bowlerac8375d2011-10-06 22:27:16 -05004413 png_ptr->row_buf = temp - extra - 1/*filter byte*/;
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004414
4415 temp = png_ptr->big_prev_row + 32;
Glenn Randers-Pehrson8db19982011-10-27 16:17:24 -05004416 extra = (int)((temp - (png_bytep)0) & 0x0f);
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004417 png_ptr->prev_row = temp - extra - 1/*filter byte*/;
John Bowlerac8375d2011-10-06 22:27:16 -05004418 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004419
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06004420#else
John Bowlerac8375d2011-10-06 22:27:16 -05004421 /* Use 31 bytes of padding before and 17 bytes after row_buf. */
4422 png_ptr->row_buf = png_ptr->big_row_buf + 31;
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004423 png_ptr->prev_row = png_ptr->big_prev_row + 31;
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06004424#endif
4425 png_ptr->old_big_row_buf_size = row_bytes + 48;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004426 }
Guy Schalnat0d580581995-07-20 02:43:20 -05004427
4428#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004429 if (png_ptr->rowbytes > 65535)
Guy Schalnate5a37791996-06-05 15:50:50 -05004430 png_error(png_ptr, "This image requires a row greater than 64KB");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004431
Guy Schalnat0d580581995-07-20 02:43:20 -05004432#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004433 if (png_ptr->rowbytes > (PNG_SIZE_MAX - 1))
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05004434 png_error(png_ptr, "Row has too many bytes to allocate in memory");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004435
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004436 memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05004437
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004438 png_debug1(3, "width = %u,", png_ptr->width);
4439 png_debug1(3, "height = %u,", png_ptr->height);
4440 png_debug1(3, "iwidth = %u,", png_ptr->iwidth);
4441 png_debug1(3, "num_rows = %u,", png_ptr->num_rows);
Glenn Randers-Pehrsonb764c602011-01-14 21:18:37 -06004442 png_debug1(3, "rowbytes = %lu,", (unsigned long)png_ptr->rowbytes);
4443 png_debug1(3, "irowbytes = %lu",
4444 (unsigned long)PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05004445
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004446 /* The sequential reader needs a buffer for IDAT, but the progressive reader
4447 * does not, so free the read buffer now regardless; the sequential reader
4448 * reallocates it on demand.
4449 */
4450 if (png_ptr->read_buffer)
4451 {
4452 png_bytep buffer = png_ptr->read_buffer;
4453
4454 png_ptr->read_buffer_size = 0;
4455 png_ptr->read_buffer = NULL;
4456 png_free(png_ptr, buffer);
4457 }
4458
John Bowler0c7ac062013-05-07 21:59:05 -05004459 /* Finally claim the zstream for the inflate of the IDAT data, use the bits
4460 * value from the stream (note that this will result in a fatal error if the
4461 * IDAT stream has a bogus deflate header window_bits value, but this should
4462 * not be happening any longer!)
Glenn Randers-Pehrsondb67cba2013-05-07 14:31:35 -05004463 */
John Bowler0c7ac062013-05-07 21:59:05 -05004464 if (png_inflate_claim(png_ptr, png_IDAT) != Z_OK)
4465 png_error(png_ptr, png_ptr->zstream.msg);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004466
Guy Schalnate5a37791996-06-05 15:50:50 -05004467 png_ptr->flags |= PNG_FLAG_ROW_INIT;
Guy Schalnat0d580581995-07-20 02:43:20 -05004468}
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06004469#endif /* PNG_READ_SUPPORTED */