blob: d5fa08c397d1413e8de83ddafc0f0839de5e71f8 [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 *
Cosmin Trutaa8738932018-07-28 18:47:21 -04004 * Copyright (c) 2018 Cosmin Truta
Glenn Randers-Pehrson84e6e352018-01-07 17:43:09 -06005 * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson
Cosmin Trutaa8738932018-07-28 18:47:21 -04006 * Copyright (c) 1996-1997 Andreas Dilger
7 * 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);
Glenn Randers-Pehrsona8242fe2015-08-17 20:46:27 -050088 if ((uval & 0x80000000) == 0) /* non-negative */
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -050089 return (png_int_32)uval;
Andreas Dilger47a0c421997-05-16 02:46:07 -050090
Glenn Randers-Pehrsona8242fe2015-08-17 20:46:27 -050091 uval = (uval ^ 0xffffffff) + 1; /* 2's complement: -x = ~x+1 */
John Bowler2d62f742015-08-19 12:56:48 -050092 if ((uval & 0x80000000) == 0) /* no overflow */
Glenn Randers-Pehrsonee079482016-07-18 08:40:26 -050093 return -(png_int_32)uval;
John Bowler2d62f742015-08-19 12:56:48 -050094 /* The following has to be safe; this function only gets called on PNG data
95 * and if we get here that data is invalid. 0 is the most safe value and
96 * if not then an attacker would surely just generate a PNG with 0 instead.
97 */
98 return 0;
Andreas Dilger47a0c421997-05-16 02:46:07 -050099}
Andreas Dilger47a0c421997-05-16 02:46:07 -0500100
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500101/* Grab an unsigned 16-bit integer from a buffer in big-endian format. */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600102png_uint_16 (PNGAPI
103png_get_uint_16)(png_const_bytep buf)
Guy Schalnat0d580581995-07-20 02:43:20 -0500104{
Unknownf23b41d2017-11-03 00:52:06 -0400105 /* ANSI-C requires an int value to accommodate at least 16 bits so this
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600106 * works and allows the compiler not to worry about possible narrowing
Glenn Randers-Pehrson8b83ff32015-08-13 20:57:18 -0500107 * on 32-bit systems. (Pre-ANSI systems did not make integers smaller
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600108 * than 16 bits either.)
109 */
110 unsigned int val =
111 ((unsigned int)(*buf) << 8) +
112 ((unsigned int)(*(buf + 1)));
Guy Schalnat0d580581995-07-20 02:43:20 -0500113
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600114 return (png_uint_16)val;
Guy Schalnat0d580581995-07-20 02:43:20 -0500115}
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600116
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -0600117#endif /* READ_INT_FUNCTIONS */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600118
119/* Read and check the PNG file signature */
120void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600121png_read_sig(png_structrp png_ptr, png_inforp info_ptr)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600122{
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -0400123 size_t num_checked, num_to_check;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600124
125 /* Exit if the user application does not expect a signature. */
126 if (png_ptr->sig_bytes >= 8)
127 return;
128
129 num_checked = png_ptr->sig_bytes;
130 num_to_check = 8 - num_checked;
131
132#ifdef PNG_IO_STATE_SUPPORTED
133 png_ptr->io_state = PNG_IO_READING | PNG_IO_SIGNATURE;
134#endif
135
136 /* The signature must be serialized in a single I/O call. */
137 png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
138 png_ptr->sig_bytes = 8;
139
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500140 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check) != 0)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600141 {
142 if (num_checked < 4 &&
143 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
144 png_error(png_ptr, "Not a PNG file");
145 else
146 png_error(png_ptr, "PNG file corrupted by ASCII conversion");
147 }
148 if (num_checked < 3)
149 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
150}
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -0600151
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500152/* Read the chunk header (length + type name).
153 * Put the type name into png_ptr->chunk_name, and return the length.
154 */
155png_uint_32 /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600156png_read_chunk_header(png_structrp png_ptr)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500157{
158 png_byte buf[8];
159 png_uint_32 length;
160
161#ifdef PNG_IO_STATE_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500162 png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_HDR;
163#endif
164
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600165 /* Read the length and the chunk name.
166 * This must be performed in a single I/O call.
167 */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500168 png_read_data(png_ptr, buf, 8);
169 length = png_get_uint_31(png_ptr, buf);
170
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600171 /* Put the chunk name into png_ptr->chunk_name. */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500172 png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(buf+4);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500173
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500174 png_debug2(0, "Reading %lx chunk, length = %lu",
175 (unsigned long)png_ptr->chunk_name, (unsigned long)length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500176
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600177 /* Reset the crc and run it over the chunk name. */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500178 png_reset_crc(png_ptr);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500179 png_calculate_crc(png_ptr, buf + 4, 4);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500180
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600181 /* Check to see if chunk name is valid. */
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500182 png_check_chunk_name(png_ptr, png_ptr->chunk_name);
183
Glenn Randers-Pehrson347538e2017-08-02 19:21:19 -0500184 /* Check for too-large chunk length */
Glenn Randers-Pehrsonfcd1bb92017-08-05 15:08:40 -0500185 png_check_chunk_length(png_ptr, length);
Glenn Randers-Pehrson347538e2017-08-02 19:21:19 -0500186
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500187#ifdef PNG_IO_STATE_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500188 png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_DATA;
189#endif
190
191 return length;
192}
193
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500194/* Read data, and (optionally) run it through the CRC. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500195void /* PRIVATE */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600196png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500197{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500198 if (png_ptr == NULL)
199 return;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600200
Guy Schalnat6d764711995-12-19 03:22:19 -0600201 png_read_data(png_ptr, buf, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500202 png_calculate_crc(png_ptr, buf, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500203}
204
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600205/* Optionally skip data and then check the CRC. Depending on whether we
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600206 * are reading an ancillary or critical chunk, and how the program has set
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500207 * things up, we may calculate the CRC on the data and print a message.
208 * Returns '1' if there was a CRC error, '0' otherwise.
209 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500210int /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600211png_crc_finish(png_structrp png_ptr, png_uint_32 skip)
Guy Schalnat0d580581995-07-20 02:43:20 -0500212{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600213 /* The size of the local buffer for inflate is a good guess as to a
214 * reasonable size to use for buffering reads from the application.
215 */
216 while (skip > 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600217 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600218 png_uint_32 len;
219 png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600220
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600221 len = (sizeof tmpbuf);
222 if (len > skip)
223 len = skip;
224 skip -= len;
225
226 png_crc_read(png_ptr, tmpbuf, len);
Guy Schalnat0d580581995-07-20 02:43:20 -0500227 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600228
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500229 if (png_crc_error(png_ptr) != 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600230 {
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500231 if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) != 0 ?
Glenn Randers-Pehrsonb963fee2014-11-01 13:18:36 -0500232 (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) == 0 :
233 (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE) != 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600234 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600235 png_chunk_warning(png_ptr, "CRC error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600236 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600237
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600238 else
Glenn Randers-Pehrsoneb657ae2014-02-22 21:59:40 -0600239 png_chunk_error(png_ptr, "CRC error");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600240
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600241 return (1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600242 }
243
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600244 return (0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500245}
246
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600247/* 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 -0500248 * the data it has read thus far.
249 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500250int /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600251png_crc_error(png_structrp png_ptr)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600252{
253 png_byte crc_bytes[4];
254 png_uint_32 crc;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500255 int need_crc = 1;
256
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500257 if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) != 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500258 {
259 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
260 (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
261 need_crc = 0;
262 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600263
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500264 else /* critical */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500265 {
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500266 if ((png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) != 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500267 need_crc = 0;
268 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600269
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500270#ifdef PNG_IO_STATE_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500271 png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_CRC;
272#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500273
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600274 /* The chunk CRC must be serialized in a single I/O call. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600275 png_read_data(png_ptr, crc_bytes, 4);
276
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -0600277 if (need_crc != 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500278 {
279 crc = png_get_uint_32(crc_bytes);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600280 return ((int)(crc != png_ptr->crc));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500281 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600282
Andreas Dilger47a0c421997-05-16 02:46:07 -0500283 else
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600284 return (0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600285}
286
Glenn Randers-Pehrson11321342013-11-18 20:12:24 -0600287#if defined(PNG_READ_iCCP_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) ||\
288 defined(PNG_READ_pCAL_SUPPORTED) || defined(PNG_READ_sCAL_SUPPORTED) ||\
289 defined(PNG_READ_sPLT_SUPPORTED) || defined(PNG_READ_tEXt_SUPPORTED) ||\
290 defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_SEQUENTIAL_READ_SUPPORTED)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600291/* Manage the read buffer; this simply reallocates the buffer if it is not small
292 * enough (or if it is not allocated). The routine returns a pointer to the
293 * buffer; if an error occurs and 'warn' is set the routine returns NULL, else
294 * it will call png_error (via png_malloc) on failure. (warn == 2 means
295 * 'silent').
296 */
297static png_bytep
298png_read_buffer(png_structrp png_ptr, png_alloc_size_t new_size, int warn)
John Bowler42a2b552012-03-05 20:57:40 -0600299{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600300 png_bytep buffer = png_ptr->read_buffer;
John Bowlerb5d00512012-03-09 09:15:18 -0600301
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600302 if (buffer != NULL && new_size > png_ptr->read_buffer_size)
John Bowlerb5d00512012-03-09 09:15:18 -0600303 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600304 png_ptr->read_buffer = NULL;
305 png_ptr->read_buffer = NULL;
306 png_ptr->read_buffer_size = 0;
307 png_free(png_ptr, buffer);
308 buffer = NULL;
309 }
John Bowlerb5d00512012-03-09 09:15:18 -0600310
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600311 if (buffer == NULL)
312 {
Glenn Randers-Pehrson935676c2016-08-12 06:58:15 -0500313 buffer = png_voidcast(png_bytep, png_malloc_base(png_ptr, new_size));
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600314
315 if (buffer != NULL)
John Bowlerb5d00512012-03-09 09:15:18 -0600316 {
Glenn Randers-Pehrson397c7ee2017-09-19 18:52:21 -0500317 memset(buffer, 0, new_size); /* just in case */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600318 png_ptr->read_buffer = buffer;
319 png_ptr->read_buffer_size = new_size;
John Bowlerb5d00512012-03-09 09:15:18 -0600320 }
John Bowlerb5d00512012-03-09 09:15:18 -0600321
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600322 else if (warn < 2) /* else silent */
John Bowlerb5d00512012-03-09 09:15:18 -0600323 {
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -0600324 if (warn != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600325 png_chunk_warning(png_ptr, "insufficient memory to read chunk");
Glenn Randers-Pehrson45625ec2014-02-22 23:09:27 -0600326
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600327 else
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600328 png_chunk_error(png_ptr, "insufficient memory to read chunk");
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600329 }
330 }
331
332 return buffer;
333}
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -0600334#endif /* READ_iCCP|iTXt|pCAL|sCAL|sPLT|tEXt|zTXt|SEQUENTIAL_READ */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600335
336/* png_inflate_claim: claim the zstream for some nefarious purpose that involves
337 * decompression. Returns Z_OK on success, else a zlib error code. It checks
338 * the owner but, in final release builds, just issues a warning if some other
339 * chunk apparently owns the stream. Prior to release it does a png_error.
340 */
341static int
John Bowler0c7ac062013-05-07 21:59:05 -0500342png_inflate_claim(png_structrp png_ptr, png_uint_32 owner)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600343{
344 if (png_ptr->zowner != 0)
345 {
346 char msg[64];
347
348 PNG_STRING_FROM_CHUNK(msg, png_ptr->zowner);
349 /* So the message that results is "<chunk> using zstream"; this is an
350 * internal error, but is very useful for debugging. i18n requirements
351 * are minimal.
352 */
353 (void)png_safecat(msg, (sizeof msg), 4, " using zstream");
John Bowler8ee821e2015-05-06 20:03:14 -0500354#if PNG_RELEASE_BUILD
Glenn Randers-Pehrson664bd632014-08-26 19:55:58 -0500355 png_chunk_warning(png_ptr, msg);
356 png_ptr->zowner = 0;
357#else
358 png_chunk_error(png_ptr, msg);
359#endif
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600360 }
361
362 /* Implementation note: unlike 'png_deflate_claim' this internal function
363 * does not take the size of the data as an argument. Some efficiency could
364 * be gained by using this when it is known *if* the zlib stream itself does
365 * not record the number; however, this is an illusion: the original writer
366 * of the PNG may have selected a lower window size, and we really must
367 * follow that because, for systems with with limited capabilities, we
368 * would otherwise reject the application's attempts to use a smaller window
369 * size (zlib doesn't have an interface to say "this or lower"!).
370 *
371 * inflateReset2 was added to zlib 1.2.4; before this the window could not be
372 * reset, therefore it is necessary to always allocate the maximum window
373 * size with earlier zlibs just in case later compressed chunks need it.
374 */
375 {
376 int ret; /* zlib return code */
Glenn Randers-Pehrson78357162016-09-19 16:46:54 -0500377#if ZLIB_VERNUM >= 0x1240
Glenn Randers-Pehrson89ea0812016-09-25 17:42:15 -0500378 int window_bits = 0;
John Bowler0c7ac062013-05-07 21:59:05 -0500379
Glenn Randers-Pehrson664bd632014-08-26 19:55:58 -0500380# if defined(PNG_SET_OPTION_SUPPORTED) && defined(PNG_MAXIMUM_INFLATE_WINDOW)
Glenn Randers-Pehrson664bd632014-08-26 19:55:58 -0500381 if (((png_ptr->options >> PNG_MAXIMUM_INFLATE_WINDOW) & 3) ==
382 PNG_OPTION_ON)
John Bowler28a1cdf2015-11-27 23:57:39 -0800383 {
Glenn Randers-Pehrson664bd632014-08-26 19:55:58 -0500384 window_bits = 15;
John Bowler28a1cdf2015-11-27 23:57:39 -0800385 png_ptr->zstream_start = 0; /* fixed window size */
386 }
John Bowler0c7ac062013-05-07 21:59:05 -0500387
Glenn Randers-Pehrson664bd632014-08-26 19:55:58 -0500388 else
John Bowler28a1cdf2015-11-27 23:57:39 -0800389 {
John Bowler28a1cdf2015-11-27 23:57:39 -0800390 png_ptr->zstream_start = 1;
391 }
Glenn Randers-Pehrson664bd632014-08-26 19:55:58 -0500392# endif
Glenn Randers-Pehrson89ea0812016-09-25 17:42:15 -0500393
394#endif /* ZLIB_VERNUM >= 0x1240 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600395
396 /* Set this for safety, just in case the previous owner left pointers to
397 * memory allocations.
398 */
399 png_ptr->zstream.next_in = NULL;
400 png_ptr->zstream.avail_in = 0;
401 png_ptr->zstream.next_out = NULL;
402 png_ptr->zstream.avail_out = 0;
403
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500404 if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600405 {
Glenn Randers-Pehrson89ea0812016-09-25 17:42:15 -0500406#if ZLIB_VERNUM >= 0x1240
Glenn Randers-Pehrson664bd632014-08-26 19:55:58 -0500407 ret = inflateReset2(&png_ptr->zstream, window_bits);
Glenn Randers-Pehrson89ea0812016-09-25 17:42:15 -0500408#else
409 ret = inflateReset(&png_ptr->zstream);
Glenn Randers-Pehrson664bd632014-08-26 19:55:58 -0500410#endif
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600411 }
412
413 else
414 {
Glenn Randers-Pehrson89ea0812016-09-25 17:42:15 -0500415#if ZLIB_VERNUM >= 0x1240
Glenn Randers-Pehrson664bd632014-08-26 19:55:58 -0500416 ret = inflateInit2(&png_ptr->zstream, window_bits);
Glenn Randers-Pehrson89ea0812016-09-25 17:42:15 -0500417#else
418 ret = inflateInit(&png_ptr->zstream);
Glenn Randers-Pehrson664bd632014-08-26 19:55:58 -0500419#endif
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600420
421 if (ret == Z_OK)
422 png_ptr->flags |= PNG_FLAG_ZSTREAM_INITIALIZED;
John Bowler42a2b552012-03-05 20:57:40 -0600423 }
424
Glenn Randers-Pehrsona7029a52017-03-01 06:52:16 -0600425#if ZLIB_VERNUM >= 0x1290 && \
Glenn Randers-Pehrsondbb5fce2016-12-24 16:12:20 -0600426 defined(PNG_SET_OPTION_SUPPORTED) && defined(PNG_IGNORE_ADLER32)
427 if (((png_ptr->options >> PNG_IGNORE_ADLER32) & 3) == PNG_OPTION_ON)
428 /* Turn off validation of the ADLER32 checksum in IDAT chunks */
Glenn Randers-Pehrsona1068992016-10-07 13:33:50 -0500429 ret = inflateValidate(&png_ptr->zstream, 0);
Glenn Randers-Pehrson89ea0812016-09-25 17:42:15 -0500430#endif
431
John Bowler42a2b552012-03-05 20:57:40 -0600432 if (ret == Z_OK)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600433 png_ptr->zowner = owner;
John Bowler42a2b552012-03-05 20:57:40 -0600434
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600435 else
436 png_zstream_error(png_ptr, ret);
437
438 return ret;
439 }
John Bowler0c7ac062013-05-07 21:59:05 -0500440
Glenn Randers-Pehrson664bd632014-08-26 19:55:58 -0500441#ifdef window_bits
442# undef window_bits
443#endif
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600444}
445
Glenn Randers-Pehrson78357162016-09-19 16:46:54 -0500446#if ZLIB_VERNUM >= 0x1240
John Bowler28a1cdf2015-11-27 23:57:39 -0800447/* Handle the start of the inflate stream if we called inflateInit2(strm,0);
448 * in this case some zlib versions skip validation of the CINFO field and, in
449 * certain circumstances, libpng may end up displaying an invalid image, in
450 * contrast to implementations that call zlib in the normal way (e.g. libpng
451 * 1.5).
452 */
453int /* PRIVATE */
454png_zlib_inflate(png_structrp png_ptr, int flush)
455{
456 if (png_ptr->zstream_start && png_ptr->zstream.avail_in > 0)
457 {
458 if ((*png_ptr->zstream.next_in >> 4) > 7)
459 {
460 png_ptr->zstream.msg = "invalid window size (libpng)";
461 return Z_DATA_ERROR;
462 }
463
464 png_ptr->zstream_start = 0;
465 }
466
467 return inflate(&png_ptr->zstream, flush);
468}
469#endif /* Zlib >= 1.2.4 */
470
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600471#ifdef PNG_READ_COMPRESSED_TEXT_SUPPORTED
Glenn Randers-Pehrson8ff2ed22016-08-11 20:13:33 -0500472#if defined(PNG_READ_zTXt_SUPPORTED) || defined (PNG_READ_iTXt_SUPPORTED)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600473/* png_inflate now returns zlib error codes including Z_OK and Z_STREAM_END to
474 * allow the caller to do multiple calls if required. If the 'finish' flag is
475 * set Z_FINISH will be passed to the final inflate() call and Z_STREAM_END must
476 * be returned or there has been a problem, otherwise Z_SYNC_FLUSH is used and
477 * Z_OK or Z_STREAM_END will be returned on success.
478 *
479 * The input and output sizes are updated to the actual amounts of data consumed
480 * or written, not the amount available (as in a z_stream). The data pointers
481 * are not changed, so the next input is (data+input_size) and the next
482 * available output is (output+output_size).
483 */
484static int
485png_inflate(png_structrp png_ptr, png_uint_32 owner, int finish,
486 /* INPUT: */ png_const_bytep input, png_uint_32p input_size_ptr,
487 /* OUTPUT: */ png_bytep output, png_alloc_size_t *output_size_ptr)
488{
489 if (png_ptr->zowner == owner) /* Else not claimed */
490 {
491 int ret;
492 png_alloc_size_t avail_out = *output_size_ptr;
493 png_uint_32 avail_in = *input_size_ptr;
494
495 /* zlib can't necessarily handle more than 65535 bytes at once (i.e. it
496 * can't even necessarily handle 65536 bytes) because the type uInt is
497 * "16 bits or more". Consequently it is necessary to chunk the input to
498 * zlib. This code uses ZLIB_IO_MAX, from pngpriv.h, as the maximum (the
499 * maximum value that can be stored in a uInt.) It is possible to set
500 * ZLIB_IO_MAX to a lower value in pngpriv.h and this may sometimes have
501 * a performance advantage, because it reduces the amount of data accessed
502 * at each step and that may give the OS more time to page it in.
John Bowlerb5d00512012-03-09 09:15:18 -0600503 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600504 png_ptr->zstream.next_in = PNGZ_INPUT_CAST(input);
505 /* avail_in and avail_out are set below from 'size' */
John Bowlerb5d00512012-03-09 09:15:18 -0600506 png_ptr->zstream.avail_in = 0;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600507 png_ptr->zstream.avail_out = 0;
John Bowlerb5d00512012-03-09 09:15:18 -0600508
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600509 /* Read directly into the output if it is available (this is set to
510 * a local buffer below if output is NULL).
John Bowlerb5d00512012-03-09 09:15:18 -0600511 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600512 if (output != NULL)
513 png_ptr->zstream.next_out = output;
514
515 do
John Bowlerb5d00512012-03-09 09:15:18 -0600516 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600517 uInt avail;
518 Byte local_buffer[PNG_INFLATE_BUF_SIZE];
John Bowlerb5d00512012-03-09 09:15:18 -0600519
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600520 /* zlib INPUT BUFFER */
521 /* The setting of 'avail_in' used to be outside the loop; by setting it
522 * inside it is possible to chunk the input to zlib and simply rely on
523 * zlib to advance the 'next_in' pointer. This allows arbitrary
524 * amounts of data to be passed through zlib at the unavoidable cost of
525 * requiring a window save (memcpy of up to 32768 output bytes)
526 * every ZLIB_IO_MAX input bytes.
527 */
528 avail_in += png_ptr->zstream.avail_in; /* not consumed last time */
John Bowlerb5d00512012-03-09 09:15:18 -0600529
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600530 avail = ZLIB_IO_MAX;
531
532 if (avail_in < avail)
533 avail = (uInt)avail_in; /* safe: < than ZLIB_IO_MAX */
534
535 avail_in -= avail;
536 png_ptr->zstream.avail_in = avail;
537
538 /* zlib OUTPUT BUFFER */
539 avail_out += png_ptr->zstream.avail_out; /* not written last time */
540
541 avail = ZLIB_IO_MAX; /* maximum zlib can process */
542
543 if (output == NULL)
John Bowlerb5d00512012-03-09 09:15:18 -0600544 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600545 /* Reset the output buffer each time round if output is NULL and
546 * make available the full buffer, up to 'remaining_space'
547 */
548 png_ptr->zstream.next_out = local_buffer;
549 if ((sizeof local_buffer) < avail)
550 avail = (sizeof local_buffer);
John Bowlerb5d00512012-03-09 09:15:18 -0600551 }
552
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600553 if (avail_out < avail)
554 avail = (uInt)avail_out; /* safe: < ZLIB_IO_MAX */
John Bowlerb5d00512012-03-09 09:15:18 -0600555
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600556 png_ptr->zstream.avail_out = avail;
557 avail_out -= avail;
558
559 /* zlib inflate call */
560 /* In fact 'avail_out' may be 0 at this point, that happens at the end
561 * of the read when the final LZ end code was not passed at the end of
562 * the previous chunk of input data. Tell zlib if we have reached the
563 * end of the output buffer.
564 */
John Bowler28a1cdf2015-11-27 23:57:39 -0800565 ret = PNG_INFLATE(png_ptr, avail_out > 0 ? Z_NO_FLUSH :
Glenn Randers-Pehrson664bd632014-08-26 19:55:58 -0500566 (finish ? Z_FINISH : Z_SYNC_FLUSH));
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600567 } while (ret == Z_OK);
568
569 /* For safety kill the local buffer pointer now */
570 if (output == NULL)
571 png_ptr->zstream.next_out = NULL;
572
573 /* Claw back the 'size' and 'remaining_space' byte counts. */
574 avail_in += png_ptr->zstream.avail_in;
575 avail_out += png_ptr->zstream.avail_out;
576
577 /* Update the input and output sizes; the updated values are the amount
578 * consumed or written, effectively the inverse of what zlib uses.
John Bowlerb5d00512012-03-09 09:15:18 -0600579 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600580 if (avail_out > 0)
581 *output_size_ptr -= avail_out;
582
583 if (avail_in > 0)
584 *input_size_ptr -= avail_in;
585
586 /* Ensure png_ptr->zstream.msg is set (even in the success case!) */
587 png_zstream_error(png_ptr, ret);
588 return ret;
589 }
590
591 else
592 {
593 /* This is a bad internal error. The recovery assigns to the zstream msg
594 * pointer, which is not owned by the caller, but this is safe; it's only
595 * used on errors!
596 */
597 png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed");
598 return Z_STREAM_ERROR;
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600599 }
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600600}
601
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600602/*
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600603 * Decompress trailing data in a chunk. The assumption is that read_buffer
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600604 * points at an allocated area holding the contents of a chunk with a
605 * trailing compressed part. What we get back is an allocated area
606 * holding the original prefix part and an uncompressed version of the
607 * trailing part (the malloc area passed in is freed).
608 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600609static int
610png_decompress_chunk(png_structrp png_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500611 png_uint_32 chunklength, png_uint_32 prefix_size,
612 png_alloc_size_t *newlength /* must be initialized to the maximum! */,
613 int terminate /*add a '\0' to the end of the uncompressed data*/)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600614{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600615 /* TODO: implement different limits for different types of chunk.
616 *
617 * The caller supplies *newlength set to the maximum length of the
618 * uncompressed data, but this routine allocates space for the prefix and
619 * maybe a '\0' terminator too. We have to assume that 'prefix_size' is
620 * limited only by the maximum chunk size.
621 */
622 png_alloc_size_t limit = PNG_SIZE_MAX;
Glenn Randers-Pehrson3744f942012-08-10 19:22:53 -0500623
Glenn Randers-Pehrsond3445892015-03-27 08:58:32 -0500624# ifdef PNG_SET_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson664bd632014-08-26 19:55:58 -0500625 if (png_ptr->user_chunk_malloc_max > 0 &&
626 png_ptr->user_chunk_malloc_max < limit)
627 limit = png_ptr->user_chunk_malloc_max;
628# elif PNG_USER_CHUNK_MALLOC_MAX > 0
629 if (PNG_USER_CHUNK_MALLOC_MAX < limit)
630 limit = PNG_USER_CHUNK_MALLOC_MAX;
631# endif
John Bowlerb5d00512012-03-09 09:15:18 -0600632
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600633 if (limit >= prefix_size + (terminate != 0))
634 {
635 int ret;
636
637 limit -= prefix_size + (terminate != 0);
638
639 if (limit < *newlength)
640 *newlength = limit;
641
John Bowler0c7ac062013-05-07 21:59:05 -0500642 /* Now try to claim the stream. */
643 ret = png_inflate_claim(png_ptr, png_ptr->chunk_name);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600644
645 if (ret == Z_OK)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600646 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600647 png_uint_32 lzsize = chunklength - prefix_size;
John Bowlerb5d00512012-03-09 09:15:18 -0600648
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600649 ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500650 /* input: */ png_ptr->read_buffer + prefix_size, &lzsize,
651 /* output: */ NULL, newlength);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600652
653 if (ret == Z_STREAM_END)
John Bowlerb5d00512012-03-09 09:15:18 -0600654 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600655 /* Use 'inflateReset' here, not 'inflateReset2' because this
656 * preserves the previously decided window size (otherwise it would
657 * be necessary to store the previous window size.) In practice
658 * this doesn't matter anyway, because png_inflate will call inflate
659 * with Z_FINISH in almost all cases, so the window will not be
660 * maintained.
661 */
662 if (inflateReset(&png_ptr->zstream) == Z_OK)
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600663 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600664 /* Because of the limit checks above we know that the new,
665 * expanded, size will fit in a size_t (let alone an
666 * png_alloc_size_t). Use png_malloc_base here to avoid an
667 * extra OOM message.
668 */
669 png_alloc_size_t new_size = *newlength;
670 png_alloc_size_t buffer_size = prefix_size + new_size +
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500671 (terminate != 0);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600672 png_bytep text = png_voidcast(png_bytep, png_malloc_base(png_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500673 buffer_size));
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600674
675 if (text != NULL)
676 {
Glenn Randers-Pehrson0165bad2017-09-22 15:21:36 -0500677 memset(text, 0, buffer_size);
678
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600679 ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500680 png_ptr->read_buffer + prefix_size, &lzsize,
681 text + prefix_size, newlength);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600682
683 if (ret == Z_STREAM_END)
684 {
685 if (new_size == *newlength)
686 {
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -0600687 if (terminate != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600688 text[prefix_size + *newlength] = 0;
689
690 if (prefix_size > 0)
691 memcpy(text, png_ptr->read_buffer, prefix_size);
692
693 {
694 png_bytep old_ptr = png_ptr->read_buffer;
695
696 png_ptr->read_buffer = text;
697 png_ptr->read_buffer_size = buffer_size;
698 text = old_ptr; /* freed below */
699 }
700 }
701
702 else
703 {
704 /* The size changed on the second read, there can be no
705 * guarantee that anything is correct at this point.
706 * The 'msg' pointer has been set to "unexpected end of
707 * LZ stream", which is fine, but return an error code
708 * that the caller won't accept.
709 */
710 ret = PNG_UNEXPECTED_ZLIB_RETURN;
711 }
712 }
713
714 else if (ret == Z_OK)
715 ret = PNG_UNEXPECTED_ZLIB_RETURN; /* for safety */
716
717 /* Free the text pointer (this is the old read_buffer on
718 * success)
719 */
720 png_free(png_ptr, text);
721
722 /* This really is very benign, but it's still an error because
723 * the extra space may otherwise be used as a Trojan Horse.
724 */
725 if (ret == Z_STREAM_END &&
Glenn Randers-Pehrsondbb5fce2016-12-24 16:12:20 -0600726 chunklength - prefix_size != lzsize)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600727 png_chunk_benign_error(png_ptr, "extra compressed data");
728 }
729
730 else
731 {
732 /* Out of memory allocating the buffer */
733 ret = Z_MEM_ERROR;
734 png_zstream_error(png_ptr, Z_MEM_ERROR);
735 }
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600736 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600737
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600738 else
739 {
740 /* inflateReset failed, store the error message */
741 png_zstream_error(png_ptr, ret);
Glenn Randers-Pehrson5efa4832017-09-20 15:53:38 -0500742 ret = PNG_UNEXPECTED_ZLIB_RETURN;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600743 }
John Bowlerb5d00512012-03-09 09:15:18 -0600744 }
745
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600746 else if (ret == Z_OK)
747 ret = PNG_UNEXPECTED_ZLIB_RETURN;
748
749 /* Release the claimed stream */
750 png_ptr->zowner = 0;
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600751 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600752
753 else /* the claim failed */ if (ret == Z_STREAM_END) /* impossible! */
754 ret = PNG_UNEXPECTED_ZLIB_RETURN;
755
756 return ret;
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -0600757 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600758
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600759 else
John Bowlerb5d00512012-03-09 09:15:18 -0600760 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600761 /* Application/configuration limits exceeded */
762 png_zstream_error(png_ptr, Z_MEM_ERROR);
763 return Z_MEM_ERROR;
John Bowlerb5d00512012-03-09 09:15:18 -0600764 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600765}
Glenn Randers-Pehrson8d091a62016-08-01 17:50:42 -0500766#endif /* READ_zTXt || READ_iTXt */
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -0600767#endif /* READ_COMPRESSED_TEXT */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600768
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600769#ifdef PNG_READ_iCCP_SUPPORTED
770/* Perform a partial read and decompress, producing 'avail_out' bytes and
771 * reading from the current chunk as required.
772 */
773static int
774png_inflate_read(png_structrp png_ptr, png_bytep read_buffer, uInt read_size,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500775 png_uint_32p chunk_bytes, png_bytep next_out, png_alloc_size_t *out_size,
776 int finish)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600777{
778 if (png_ptr->zowner == png_ptr->chunk_name)
779 {
780 int ret;
781
782 /* next_in and avail_in must have been initialized by the caller. */
783 png_ptr->zstream.next_out = next_out;
784 png_ptr->zstream.avail_out = 0; /* set in the loop */
785
786 do
787 {
788 if (png_ptr->zstream.avail_in == 0)
789 {
790 if (read_size > *chunk_bytes)
791 read_size = (uInt)*chunk_bytes;
792 *chunk_bytes -= read_size;
793
794 if (read_size > 0)
795 png_crc_read(png_ptr, read_buffer, read_size);
796
797 png_ptr->zstream.next_in = read_buffer;
798 png_ptr->zstream.avail_in = read_size;
799 }
800
801 if (png_ptr->zstream.avail_out == 0)
802 {
803 uInt avail = ZLIB_IO_MAX;
804 if (avail > *out_size)
805 avail = (uInt)*out_size;
806 *out_size -= avail;
807
808 png_ptr->zstream.avail_out = avail;
809 }
810
811 /* Use Z_SYNC_FLUSH when there is no more chunk data to ensure that all
812 * the available output is produced; this allows reading of truncated
813 * streams.
814 */
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500815 ret = PNG_INFLATE(png_ptr, *chunk_bytes > 0 ?
816 Z_NO_FLUSH : (finish ? Z_FINISH : Z_SYNC_FLUSH));
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600817 }
818 while (ret == Z_OK && (*out_size > 0 || png_ptr->zstream.avail_out > 0));
819
820 *out_size += png_ptr->zstream.avail_out;
821 png_ptr->zstream.avail_out = 0; /* Should not be required, but is safe */
822
823 /* Ensure the error message pointer is always set: */
824 png_zstream_error(png_ptr, ret);
825 return ret;
826 }
827
828 else
829 {
830 png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed");
831 return Z_STREAM_ERROR;
832 }
833}
Glenn Randers-Pehrsondbb5fce2016-12-24 16:12:20 -0600834#endif /* READ_iCCP */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600835
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500836/* Read and check the IDHR chunk */
Glenn Randers-Pehrsonf43b5e32014-12-20 19:10:52 -0600837
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500838void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600839png_handle_IHDR(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500840{
841 png_byte buf[13];
842 png_uint_32 width, height;
843 int bit_depth, color_type, compression_type, filter_type;
844 int interlace_type;
845
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500846 png_debug(1, "in png_handle_IHDR");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500847
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500848 if ((png_ptr->mode & PNG_HAVE_IHDR) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600849 png_chunk_error(png_ptr, "out of place");
Guy Schalnate5a37791996-06-05 15:50:50 -0500850
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500851 /* Check the length */
Guy Schalnat0d580581995-07-20 02:43:20 -0500852 if (length != 13)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600853 png_chunk_error(png_ptr, "invalid");
Guy Schalnat0d580581995-07-20 02:43:20 -0500854
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600855 png_ptr->mode |= PNG_HAVE_IHDR;
856
Guy Schalnat0d580581995-07-20 02:43:20 -0500857 png_crc_read(png_ptr, buf, 13);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600858 png_crc_finish(png_ptr, 0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500859
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500860 width = png_get_uint_31(png_ptr, buf);
861 height = png_get_uint_31(png_ptr, buf + 4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500862 bit_depth = buf[8];
863 color_type = buf[9];
864 compression_type = buf[10];
865 filter_type = buf[11];
866 interlace_type = buf[12];
867
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500868 /* Set internal variables */
Guy Schalnat0d580581995-07-20 02:43:20 -0500869 png_ptr->width = width;
870 png_ptr->height = height;
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -0500871 png_ptr->bit_depth = (png_byte)bit_depth;
872 png_ptr->interlaced = (png_byte)interlace_type;
873 png_ptr->color_type = (png_byte)color_type;
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500874#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -0500875 png_ptr->filter_type = (png_byte)filter_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500876#endif
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -0500877 png_ptr->compression_type = (png_byte)compression_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500878
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500879 /* Find number of channels */
Guy Schalnat0d580581995-07-20 02:43:20 -0500880 switch (png_ptr->color_type)
881 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600882 default: /* invalid, png_set_IHDR calls png_error */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500883 case PNG_COLOR_TYPE_GRAY:
884 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnat0d580581995-07-20 02:43:20 -0500885 png_ptr->channels = 1;
886 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500887
Andreas Dilger47a0c421997-05-16 02:46:07 -0500888 case PNG_COLOR_TYPE_RGB:
Guy Schalnat0d580581995-07-20 02:43:20 -0500889 png_ptr->channels = 3;
890 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500891
Andreas Dilger47a0c421997-05-16 02:46:07 -0500892 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnat0d580581995-07-20 02:43:20 -0500893 png_ptr->channels = 2;
894 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500895
Andreas Dilger47a0c421997-05-16 02:46:07 -0500896 case PNG_COLOR_TYPE_RGB_ALPHA:
Guy Schalnat0d580581995-07-20 02:43:20 -0500897 png_ptr->channels = 4;
898 break;
899 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600900
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500901 /* Set up other useful info */
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -0500902 png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth * png_ptr->channels);
Glenn Randers-Pehrson06ee3842014-12-21 13:51:44 -0600903 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width);
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500904 png_debug1(3, "bit_depth = %d", png_ptr->bit_depth);
905 png_debug1(3, "channels = %d", png_ptr->channels);
Glenn Randers-Pehrson06ee3842014-12-21 13:51:44 -0600906 png_debug1(3, "rowbytes = %lu", (unsigned long)png_ptr->rowbytes);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500907 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600908 color_type, interlace_type, compression_type, filter_type);
Guy Schalnat0d580581995-07-20 02:43:20 -0500909}
910
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500911/* Read and check the palette */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500912void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600913png_handle_PLTE(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500914{
Glenn Randers-Pehrson2f8b82e2017-10-29 10:30:42 -0500915 png_color palette[PNG_MAX_PALETTE_LENGTH];
Glenn Randers-Pehrson1bef8e92015-10-30 11:34:37 -0500916 int max_palette_length, num, i;
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500917#ifdef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500918 png_colorp pal_ptr;
919#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500920
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500921 png_debug(1, "in png_handle_PLTE");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500922
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500923 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600924 png_chunk_error(png_ptr, "missing IHDR");
925
926 /* Moved to before the 'after IDAT' check below because otherwise duplicate
927 * PLTE chunks are potentially ignored (the spec says there shall not be more
928 * than one PLTE, the error is not treated as benign, so this check trumps
929 * the requirement that PLTE appears before IDAT.)
930 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500931 else if ((png_ptr->mode & PNG_HAVE_PLTE) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600932 png_chunk_error(png_ptr, "duplicate");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500933
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500934 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600935 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600936 /* This is benign because the non-benign error happened before, when an
937 * IDAT was encountered in a color-mapped image with no PLTE.
938 */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600939 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600940 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600941 return;
942 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500943
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600944 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500945
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500946 if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500947 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500948 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600949 png_chunk_benign_error(png_ptr, "ignored in grayscale PNG");
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500950 return;
951 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -0600952
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -0500953#ifndef PNG_READ_OPT_PLTE_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500954 if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
955 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600956 png_crc_finish(png_ptr, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500957 return;
958 }
959#endif
960
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600961 if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3)
Guy Schalnate5a37791996-06-05 15:50:50 -0500962 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600963 png_crc_finish(png_ptr, length);
964
Guy Schalnate5a37791996-06-05 15:50:50 -0500965 if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600966 png_chunk_benign_error(png_ptr, "invalid");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500967
Guy Schalnate5a37791996-06-05 15:50:50 -0500968 else
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600969 png_chunk_error(png_ptr, "invalid");
970
971 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500972 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500973
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600974 /* The cast is safe because 'length' is less than 3*PNG_MAX_PALETTE_LENGTH */
Guy Schalnat0d580581995-07-20 02:43:20 -0500975 num = (int)length / 3;
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500976
Glenn Randers-Pehrson83f4c732015-11-05 11:18:44 -0600977 /* If the palette has 256 or fewer entries but is too large for the bit
978 * depth, we don't issue an error, to preserve the behavior of previous
979 * libpng versions. We silently truncate the unused extra palette entries
980 * here.
Glenn Randers-Pehrson1bef8e92015-10-30 11:34:37 -0500981 */
Glenn Randers-Pehrson83f4c732015-11-05 11:18:44 -0600982 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
983 max_palette_length = (1 << png_ptr->bit_depth);
984 else
985 max_palette_length = PNG_MAX_PALETTE_LENGTH;
986
Glenn Randers-Pehrson1bef8e92015-10-30 11:34:37 -0500987 if (num > max_palette_length)
Glenn Randers-Pehrson83f4c732015-11-05 11:18:44 -0600988 num = max_palette_length;
Glenn Randers-Pehrson1bef8e92015-10-30 11:34:37 -0500989
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500990#ifdef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500991 for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)
992 {
993 png_byte buf[3];
994
995 png_crc_read(png_ptr, buf, 3);
996 pal_ptr->red = buf[0];
997 pal_ptr->green = buf[1];
998 pal_ptr->blue = buf[2];
999 }
1000#else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001001 for (i = 0; i < num; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001002 {
1003 png_byte buf[3];
1004
1005 png_crc_read(png_ptr, buf, 3);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001006 /* Don't depend upon png_color being any order */
Guy Schalnat0d580581995-07-20 02:43:20 -05001007 palette[i].red = buf[0];
1008 palette[i].green = buf[1];
1009 palette[i].blue = buf[2];
1010 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001011#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001012
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001013 /* 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 -05001014 * whatever the normal CRC configuration tells us. However, if we
1015 * have an RGB image, the PLTE can be considered ancillary, so
1016 * we will act as though it is.
1017 */
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -05001018#ifndef PNG_READ_OPT_PLTE_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001019 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001020#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001021 {
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -05001022 png_crc_finish(png_ptr, (png_uint_32) (length - (unsigned int)num * 3));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001023 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001024
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -05001025#ifndef PNG_READ_OPT_PLTE_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001026 else if (png_crc_error(png_ptr) != 0) /* Only if we have a CRC error */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001027 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001028 /* If we don't want to use the data from an ancillary chunk,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001029 * we have two options: an error abort, or a warning and we
1030 * ignore the data in this chunk (which should be OK, since
1031 * it's considered ancillary for a RGB or RGBA image).
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001032 *
1033 * IMPLEMENTATION NOTE: this is only here because png_crc_finish uses the
1034 * chunk type to determine whether to check the ancillary or the critical
1035 * flags.
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001036 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001037 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001038 {
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001039 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) != 0)
Glenn Randers-Pehrson45625ec2014-02-22 23:09:27 -06001040 return;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001041
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001042 else
Glenn Randers-Pehrson45625ec2014-02-22 23:09:27 -06001043 png_chunk_error(png_ptr, "CRC error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001044 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001045
Andreas Dilger47a0c421997-05-16 02:46:07 -05001046 /* Otherwise, we (optionally) emit a warning and use the chunk. */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001047 else if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) == 0)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001048 png_chunk_warning(png_ptr, "CRC error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001049 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001050#endif
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -05001051
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001052 /* TODO: png_set_PLTE has the side effect of setting png_ptr->palette to its
1053 * own copy of the palette. This has the side effect that when png_start_row
1054 * is called (this happens after any call to png_read_update_info) the
1055 * info_ptr palette gets changed. This is extremely unexpected and
1056 * confusing.
1057 *
1058 * Fix this by not sharing the palette in this way.
1059 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001060 png_set_PLTE(png_ptr, info_ptr, palette, num);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001061
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001062 /* The three chunks, bKGD, hIST and tRNS *must* appear after PLTE and before
1063 * IDAT. Prior to 1.6.0 this was not checked; instead the code merely
1064 * checked the apparent validity of a tRNS chunk inserted before PLTE on a
1065 * palette PNG. 1.6.0 attempts to rigorously follow the standard and
1066 * therefore does a benign error if the erroneous condition is detected *and*
1067 * cancels the tRNS if the benign error returns. The alternative is to
1068 * amend the standard since it would be rather hypocritical of the standards
1069 * maintainers to ignore it.
1070 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001071#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001072 if (png_ptr->num_trans > 0 ||
Glenn Randers-Pehrson664bd632014-08-26 19:55:58 -05001073 (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS) != 0))
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001074 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001075 /* Cancel this because otherwise it would be used if the transforms
1076 * require it. Don't cancel the 'valid' flag because this would prevent
1077 * detection of duplicate chunks.
1078 */
1079 png_ptr->num_trans = 0;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001080
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001081 if (info_ptr != NULL)
1082 info_ptr->num_trans = 0;
1083
1084 png_chunk_benign_error(png_ptr, "tRNS must be after");
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001085 }
1086#endif
1087
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001088#ifdef PNG_READ_hIST_SUPPORTED
1089 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) != 0)
1090 png_chunk_benign_error(png_ptr, "hIST must be after");
1091#endif
1092
1093#ifdef PNG_READ_bKGD_SUPPORTED
1094 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) != 0)
1095 png_chunk_benign_error(png_ptr, "bKGD must be after");
1096#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001097}
Guy Schalnate5a37791996-06-05 15:50:50 -05001098
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001099void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001100png_handle_IEND(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001101{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001102 png_debug(1, "in png_handle_IEND");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001103
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001104 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0 ||
1105 (png_ptr->mode & PNG_HAVE_IDAT) == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001106 png_chunk_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001107
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001108 png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001109
Andreas Dilger47a0c421997-05-16 02:46:07 -05001110 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001111
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001112 if (length != 0)
1113 png_chunk_benign_error(png_ptr, "invalid");
1114
1115 PNG_UNUSED(info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001116}
1117
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001118#ifdef PNG_READ_gAMA_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001119void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001120png_handle_gAMA(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001121{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001122 png_fixed_point igamma;
Guy Schalnat0d580581995-07-20 02:43:20 -05001123 png_byte buf[4];
1124
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001125 png_debug(1, "in png_handle_gAMA");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001126
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001127 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001128 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001129
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001130 else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001131 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001132 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001133 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001134 return;
1135 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001136
Guy Schalnat0d580581995-07-20 02:43:20 -05001137 if (length != 4)
1138 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001139 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001140 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnat0d580581995-07-20 02:43:20 -05001141 return;
1142 }
1143
1144 png_crc_read(png_ptr, buf, 4);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001145
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001146 if (png_crc_finish(png_ptr, 0) != 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001147 return;
1148
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001149 igamma = png_get_fixed_point(NULL, buf);
Glenn Randers-Pehrson33893092010-10-23 13:20:18 -05001150
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001151 png_colorspace_set_gamma(png_ptr, &png_ptr->colorspace, igamma);
1152 png_colorspace_sync(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001153}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001154#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001155
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001156#ifdef PNG_READ_sBIT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001157void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001158png_handle_sBIT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001159{
John Bowler414d7b52014-02-06 11:32:57 -06001160 unsigned int truelen, i;
1161 png_byte sample_depth;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001162 png_byte buf[4];
Guy Schalnat69b14481996-01-10 02:56:49 -06001163
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001164 png_debug(1, "in png_handle_sBIT");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001165
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001166 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001167 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001168
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001169 else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001170 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001171 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001172 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001173 return;
1174 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001175
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001176 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT) != 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001177 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001178 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001179 png_chunk_benign_error(png_ptr, "duplicate");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001180 return;
1181 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001182
Guy Schalnat0d580581995-07-20 02:43:20 -05001183 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
John Bowler414d7b52014-02-06 11:32:57 -06001184 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001185 truelen = 3;
John Bowler414d7b52014-02-06 11:32:57 -06001186 sample_depth = 8;
1187 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001188
Guy Schalnat0d580581995-07-20 02:43:20 -05001189 else
John Bowler414d7b52014-02-06 11:32:57 -06001190 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001191 truelen = png_ptr->channels;
John Bowler414d7b52014-02-06 11:32:57 -06001192 sample_depth = png_ptr->bit_depth;
1193 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001194
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001195 if (length != truelen || length > 4)
Guy Schalnat0d580581995-07-20 02:43:20 -05001196 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001197 png_chunk_benign_error(png_ptr, "invalid");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001198 png_crc_finish(png_ptr, length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001199 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001200 }
1201
John Bowler414d7b52014-02-06 11:32:57 -06001202 buf[0] = buf[1] = buf[2] = buf[3] = sample_depth;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001203 png_crc_read(png_ptr, buf, truelen);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001204
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001205 if (png_crc_finish(png_ptr, 0) != 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001206 return;
1207
John Bowler414d7b52014-02-06 11:32:57 -06001208 for (i=0; i<truelen; ++i)
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05001209 {
John Bowler414d7b52014-02-06 11:32:57 -06001210 if (buf[i] == 0 || buf[i] > sample_depth)
1211 {
1212 png_chunk_benign_error(png_ptr, "invalid");
1213 return;
1214 }
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05001215 }
John Bowler414d7b52014-02-06 11:32:57 -06001216
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001217 if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
Guy Schalnat0d580581995-07-20 02:43:20 -05001218 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001219 png_ptr->sig_bit.red = buf[0];
1220 png_ptr->sig_bit.green = buf[1];
1221 png_ptr->sig_bit.blue = buf[2];
1222 png_ptr->sig_bit.alpha = buf[3];
Guy Schalnat0d580581995-07-20 02:43:20 -05001223 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001224
Guy Schalnat0d580581995-07-20 02:43:20 -05001225 else
1226 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001227 png_ptr->sig_bit.gray = buf[0];
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001228 png_ptr->sig_bit.red = buf[0];
1229 png_ptr->sig_bit.green = buf[0];
1230 png_ptr->sig_bit.blue = buf[0];
Guy Schalnat6d764711995-12-19 03:22:19 -06001231 png_ptr->sig_bit.alpha = buf[1];
Guy Schalnat0d580581995-07-20 02:43:20 -05001232 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001233
Andreas Dilger47a0c421997-05-16 02:46:07 -05001234 png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
Guy Schalnat0d580581995-07-20 02:43:20 -05001235}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001236#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001237
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001238#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001239void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001240png_handle_cHRM(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001241{
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001242 png_byte buf[32];
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001243 png_xy xy;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -06001244
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001245 png_debug(1, "in png_handle_cHRM");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001246
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001247 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001248 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001249
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001250 else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001251 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001252 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001253 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001254 return;
1255 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001256
Guy Schalnat0d580581995-07-20 02:43:20 -05001257 if (length != 32)
1258 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001259 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001260 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001261 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001262 }
1263
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001264 png_crc_read(png_ptr, buf, 32);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001265
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001266 if (png_crc_finish(png_ptr, 0) != 0)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001267 return;
1268
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001269 xy.whitex = png_get_fixed_point(NULL, buf);
1270 xy.whitey = png_get_fixed_point(NULL, buf + 4);
1271 xy.redx = png_get_fixed_point(NULL, buf + 8);
1272 xy.redy = png_get_fixed_point(NULL, buf + 12);
1273 xy.greenx = png_get_fixed_point(NULL, buf + 16);
1274 xy.greeny = png_get_fixed_point(NULL, buf + 20);
1275 xy.bluex = png_get_fixed_point(NULL, buf + 24);
1276 xy.bluey = png_get_fixed_point(NULL, buf + 28);
Glenn Randers-Pehrson33893092010-10-23 13:20:18 -05001277
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001278 if (xy.whitex == PNG_FIXED_ERROR ||
1279 xy.whitey == PNG_FIXED_ERROR ||
1280 xy.redx == PNG_FIXED_ERROR ||
1281 xy.redy == PNG_FIXED_ERROR ||
1282 xy.greenx == PNG_FIXED_ERROR ||
1283 xy.greeny == PNG_FIXED_ERROR ||
1284 xy.bluex == PNG_FIXED_ERROR ||
1285 xy.bluey == PNG_FIXED_ERROR)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001286 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001287 png_chunk_benign_error(png_ptr, "invalid values");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001288 return;
1289 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001290
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001291 /* If a colorspace error has already been output skip this chunk */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001292 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001293 return;
1294
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001295 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM) != 0)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001296 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001297 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1298 png_colorspace_sync(png_ptr, info_ptr);
1299 png_chunk_benign_error(png_ptr, "duplicate");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001300 return;
1301 }
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001302
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001303 png_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM;
1304 (void)png_colorspace_set_chromaticities(png_ptr, &png_ptr->colorspace, &xy,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001305 1/*prefer cHRM values*/);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001306 png_colorspace_sync(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001307}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001308#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001309
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001310#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001311void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001312png_handle_sRGB(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001313{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001314 png_byte intent;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001315
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001316 png_debug(1, "in png_handle_sRGB");
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001317
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001318 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001319 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001320
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001321 else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001322 {
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001323 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001324 png_chunk_benign_error(png_ptr, "out of place");
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001325 return;
1326 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001327
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001328 if (length != 1)
1329 {
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001330 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001331 png_chunk_benign_error(png_ptr, "invalid");
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001332 return;
1333 }
1334
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001335 png_crc_read(png_ptr, &intent, 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001336
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001337 if (png_crc_finish(png_ptr, 0) != 0)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001338 return;
1339
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001340 /* If a colorspace error has already been output skip this chunk */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001341 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001342 return;
John Bowler88b77cc2011-05-05 06:49:55 -05001343
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001344 /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect
1345 * this.
1346 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001347 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) != 0)
John Bowlerb11b31a2012-03-21 07:55:46 -05001348 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001349 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1350 png_colorspace_sync(png_ptr, info_ptr);
1351 png_chunk_benign_error(png_ptr, "too many profiles");
John Bowlerb11b31a2012-03-21 07:55:46 -05001352 return;
1353 }
John Bowler736f40f2011-08-25 16:19:44 -05001354
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001355 (void)png_colorspace_set_sRGB(png_ptr, &png_ptr->colorspace, intent);
1356 png_colorspace_sync(png_ptr, info_ptr);
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06001357}
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06001358#endif /* READ_sRGB */
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06001359
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001360#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001361void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001362png_handle_iCCP(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001363/* Note: this does not properly handle profiles that are > 64K under DOS */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001364{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001365 png_const_charp errmsg = NULL; /* error message output, or no error */
1366 int finished = 0; /* crc checked */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001367
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001368 png_debug(1, "in png_handle_iCCP");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001369
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001370 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001371 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001372
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001373 else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001374 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001375 png_crc_finish(png_ptr, length);
1376 png_chunk_benign_error(png_ptr, "out of place");
1377 return;
1378 }
1379
1380 /* Consistent with all the above colorspace handling an obviously *invalid*
1381 * chunk is just ignored, so does not invalidate the color space. An
1382 * alternative is to set the 'invalid' flags at the start of this routine
1383 * and only clear them in they were not set before and all the tests pass.
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001384 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001385
Glenn Randers-Pehrsonc82ae402017-08-06 08:37:48 -05001386 /* The keyword must be at least one character and there is a
1387 * terminator (0) byte and the compression method byte, and the
1388 * 'zlib' datastream is at least 11 bytes.
1389 */
1390 if (length < 14)
1391 {
1392 png_crc_finish(png_ptr, length);
1393 png_chunk_benign_error(png_ptr, "too short");
1394 return;
1395 }
1396
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001397 /* If a colorspace error has already been output skip this chunk */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001398 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001399 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001400 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001401 return;
1402 }
1403
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001404 /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect
1405 * this.
1406 */
1407 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) == 0)
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06001408 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001409 uInt read_length, keyword_length;
1410 char keyword[81];
1411
1412 /* Find the keyword; the keyword plus separator and compression method
1413 * bytes can be at most 81 characters long.
1414 */
1415 read_length = 81; /* maximum */
1416 if (read_length > length)
1417 read_length = (uInt)length;
1418
1419 png_crc_read(png_ptr, (png_bytep)keyword, read_length);
1420 length -= read_length;
1421
Glenn Randers-Pehrsonc82ae402017-08-06 08:37:48 -05001422 /* The minimum 'zlib' stream is assumed to be just the 2 byte header,
1423 * 5 bytes minimum 'deflate' stream, and the 4 byte checksum.
1424 */
1425 if (length < 11)
1426 {
1427 png_crc_finish(png_ptr, length);
1428 png_chunk_benign_error(png_ptr, "too short");
1429 return;
1430 }
Glenn Randers-Pehrson099558d2017-08-06 08:25:56 -05001431
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001432 keyword_length = 0;
1433 while (keyword_length < 80 && keyword_length < read_length &&
1434 keyword[keyword_length] != 0)
1435 ++keyword_length;
1436
1437 /* TODO: make the keyword checking common */
1438 if (keyword_length >= 1 && keyword_length <= 79)
1439 {
1440 /* We only understand '0' compression - deflate - so if we get a
1441 * different value we can't safely decode the chunk.
1442 */
1443 if (keyword_length+1 < read_length &&
1444 keyword[keyword_length+1] == PNG_COMPRESSION_TYPE_BASE)
1445 {
1446 read_length -= keyword_length+2;
1447
John Bowler0c7ac062013-05-07 21:59:05 -05001448 if (png_inflate_claim(png_ptr, png_iCCP) == Z_OK)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001449 {
Glenn Randers-Pehrsonc5c778b2017-08-05 20:15:52 -05001450 Byte profile_header[132]={0};
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001451 Byte local_buffer[PNG_INFLATE_BUF_SIZE];
1452 png_alloc_size_t size = (sizeof profile_header);
1453
1454 png_ptr->zstream.next_in = (Bytef*)keyword + (keyword_length+2);
1455 png_ptr->zstream.avail_in = read_length;
1456 (void)png_inflate_read(png_ptr, local_buffer,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001457 (sizeof local_buffer), &length, profile_header, &size,
1458 0/*finish: don't, because the output is too small*/);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001459
1460 if (size == 0)
1461 {
1462 /* We have the ICC profile header; do the basic header checks.
1463 */
Cosmin Trutaceb32772018-08-18 22:47:16 -04001464 png_uint_32 profile_length = png_get_uint_32(profile_header);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001465
1466 if (png_icc_check_length(png_ptr, &png_ptr->colorspace,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001467 keyword, profile_length) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001468 {
1469 /* The length is apparently ok, so we can check the 132
1470 * byte header.
1471 */
1472 if (png_icc_check_header(png_ptr, &png_ptr->colorspace,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001473 keyword, profile_length, profile_header,
1474 png_ptr->color_type) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001475 {
1476 /* Now read the tag table; a variable size buffer is
1477 * needed at this point, allocate one for the whole
1478 * profile. The header check has already validated
Glenn Randers-Pehrson397c7ee2017-09-19 18:52:21 -05001479 * that none of this stuff will overflow.
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001480 */
Cosmin Trutaceb32772018-08-18 22:47:16 -04001481 png_uint_32 tag_count =
1482 png_get_uint_32(profile_header + 128);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001483 png_bytep profile = png_read_buffer(png_ptr,
Glenn Randers-Pehrson935676c2016-08-12 06:58:15 -05001484 profile_length, 2/*silent*/);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001485
1486 if (profile != NULL)
1487 {
1488 memcpy(profile, profile_header,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001489 (sizeof profile_header));
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001490
1491 size = 12 * tag_count;
1492
1493 (void)png_inflate_read(png_ptr, local_buffer,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001494 (sizeof local_buffer), &length,
1495 profile + (sizeof profile_header), &size, 0);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001496
Glenn Randers-Pehrson9c5a1ba2014-02-17 09:12:52 -06001497 /* Still expect a buffer error because we expect
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001498 * there to be some tag data!
1499 */
1500 if (size == 0)
1501 {
1502 if (png_icc_check_tag_table(png_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001503 &png_ptr->colorspace, keyword, profile_length,
1504 profile) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001505 {
1506 /* The profile has been validated for basic
1507 * security issues, so read the whole thing in.
1508 */
1509 size = profile_length - (sizeof profile_header)
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001510 - 12 * tag_count;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001511
1512 (void)png_inflate_read(png_ptr, local_buffer,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001513 (sizeof local_buffer), &length,
1514 profile + (sizeof profile_header) +
1515 12 * tag_count, &size, 1/*finish*/);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001516
1517 if (length > 0 && !(png_ptr->flags &
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001518 PNG_FLAG_BENIGN_ERRORS_WARN))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001519 errmsg = "extra compressed data";
1520
1521 /* But otherwise allow extra data: */
1522 else if (size == 0)
1523 {
1524 if (length > 0)
1525 {
1526 /* This can be handled completely, so
1527 * keep going.
1528 */
1529 png_chunk_warning(png_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001530 "extra compressed data");
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001531 }
1532
1533 png_crc_finish(png_ptr, length);
1534 finished = 1;
1535
Glenn Randers-Pehrson80495122016-07-03 10:28:54 -05001536# if defined(PNG_sRGB_SUPPORTED) && PNG_sRGB_PROFILE_CHECKS >= 0
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05001537 /* Check for a match against sRGB */
1538 png_icc_set_sRGB(png_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001539 &png_ptr->colorspace, profile,
1540 png_ptr->zstream.adler);
Glenn Randers-Pehrson80495122016-07-03 10:28:54 -05001541# endif
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001542
1543 /* Steal the profile for info_ptr. */
1544 if (info_ptr != NULL)
1545 {
1546 png_free_data(png_ptr, info_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001547 PNG_FREE_ICCP, 0);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001548
1549 info_ptr->iccp_name = png_voidcast(char*,
Glenn Randers-Pehrson935676c2016-08-12 06:58:15 -05001550 png_malloc_base(png_ptr,
1551 keyword_length+1));
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001552 if (info_ptr->iccp_name != NULL)
1553 {
1554 memcpy(info_ptr->iccp_name, keyword,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001555 keyword_length+1);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001556 info_ptr->iccp_proflen =
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001557 profile_length;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001558 info_ptr->iccp_profile = profile;
1559 png_ptr->read_buffer = NULL; /*steal*/
1560 info_ptr->free_me |= PNG_FREE_ICCP;
1561 info_ptr->valid |= PNG_INFO_iCCP;
1562 }
1563
1564 else
1565 {
1566 png_ptr->colorspace.flags |=
1567 PNG_COLORSPACE_INVALID;
1568 errmsg = "out of memory";
1569 }
1570 }
1571
1572 /* else the profile remains in the read
1573 * buffer which gets reused for subsequent
1574 * chunks.
1575 */
1576
1577 if (info_ptr != NULL)
1578 png_colorspace_sync(png_ptr, info_ptr);
1579
1580 if (errmsg == NULL)
1581 {
1582 png_ptr->zowner = 0;
1583 return;
1584 }
1585 }
Glenn Randers-Pehrson3d2d0b52017-09-23 21:22:18 -05001586 if (errmsg == NULL)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001587 errmsg = png_ptr->zstream.msg;
1588 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001589 /* else png_icc_check_tag_table output an error */
1590 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001591 else /* profile truncated */
1592 errmsg = png_ptr->zstream.msg;
1593 }
1594
1595 else
1596 errmsg = "out of memory";
1597 }
1598
1599 /* else png_icc_check_header output an error */
1600 }
1601
1602 /* else png_icc_check_length output an error */
1603 }
1604
1605 else /* profile truncated */
1606 errmsg = png_ptr->zstream.msg;
1607
1608 /* Release the stream */
1609 png_ptr->zowner = 0;
1610 }
1611
1612 else /* png_inflate_claim failed */
1613 errmsg = png_ptr->zstream.msg;
1614 }
1615
1616 else
1617 errmsg = "bad compression method"; /* or missing */
1618 }
1619
1620 else
1621 errmsg = "bad keyword";
1622 }
1623
1624 else
1625 errmsg = "too many profiles";
1626
1627 /* Failure: the reason is in 'errmsg' */
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06001628 if (finished == 0)
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06001629 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06001630
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001631 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1632 png_colorspace_sync(png_ptr, info_ptr);
1633 if (errmsg != NULL) /* else already output */
1634 png_chunk_benign_error(png_ptr, errmsg);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001635}
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06001636#endif /* READ_iCCP */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001637
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001638#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001639void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001640png_handle_sPLT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001641/* Note: this does not properly handle chunks that are > 64K under DOS */
1642{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001643 png_bytep entry_start, buffer;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001644 png_sPLT_t new_palette;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001645 png_sPLT_entryp pp;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001646 png_uint_32 data_length;
1647 int entry_size, i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001648 png_uint_32 skip = 0;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001649 png_uint_32 dl;
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04001650 size_t max_dl;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001651
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001652 png_debug(1, "in png_handle_sPLT");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001653
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001654#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001655 if (png_ptr->user_chunk_cache_max != 0)
1656 {
1657 if (png_ptr->user_chunk_cache_max == 1)
1658 {
1659 png_crc_finish(png_ptr, length);
1660 return;
1661 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001662
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001663 if (--png_ptr->user_chunk_cache_max == 1)
1664 {
1665 png_warning(png_ptr, "No space in chunk cache for sPLT");
1666 png_crc_finish(png_ptr, length);
1667 return;
1668 }
1669 }
1670#endif
1671
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001672 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001673 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001674
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001675 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001676 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001677 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001678 png_chunk_benign_error(png_ptr, "out of place");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001679 return;
1680 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001681
1682#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001683 if (length > 65535U)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001684 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001685 png_crc_finish(png_ptr, length);
1686 png_chunk_benign_error(png_ptr, "too large to fit in memory");
1687 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001688 }
1689#endif
1690
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001691 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
1692 if (buffer == NULL)
1693 {
1694 png_crc_finish(png_ptr, length);
1695 png_chunk_benign_error(png_ptr, "out of memory");
1696 return;
1697 }
1698
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001699
1700 /* WARNING: this may break if size_t is less than 32 bits; it is assumed
1701 * that the PNG_MAX_MALLOC_64K test is enabled in this case, but this is a
1702 * potential breakage point if the types in pngconf.h aren't exactly right.
1703 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001704 png_crc_read(png_ptr, buffer, length);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001705
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001706 if (png_crc_finish(png_ptr, skip) != 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001707 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001708
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001709 buffer[length] = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001710
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001711 for (entry_start = buffer; *entry_start; entry_start++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001712 /* Empty loop to find end of name */ ;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001713
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001714 ++entry_start;
1715
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001716 /* A sample depth should follow the separator, and we should be on it */
Glenn Randers-Pehrson8ba160b2015-11-21 14:31:59 -06001717 if (length < 2U || entry_start > buffer + (length - 2U))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001718 {
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -05001719 png_warning(png_ptr, "malformed sPLT chunk");
1720 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001721 }
1722
1723 new_palette.depth = *entry_start++;
Glenn Randers-Pehrsona565f0e2010-03-06 08:24:45 -06001724 entry_size = (new_palette.depth == 8 ? 6 : 10);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001725 /* This must fit in a png_uint_32 because it is derived from the original
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001726 * chunk data length.
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001727 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001728 data_length = length - (png_uint_32)(entry_start - buffer);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001729
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001730 /* Integrity-check the data length */
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -05001731 if ((data_length % (unsigned int)entry_size) != 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001732 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001733 png_warning(png_ptr, "sPLT chunk has bad length");
1734 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001735 }
1736
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -05001737 dl = (png_uint_32)(data_length / (unsigned int)entry_size);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001738 max_dl = PNG_SIZE_MAX / (sizeof (png_sPLT_entry));
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001739
1740 if (dl > max_dl)
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001741 {
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05001742 png_warning(png_ptr, "sPLT chunk too long");
1743 return;
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001744 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001745
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -05001746 new_palette.nentries = (png_int_32)(data_length / (unsigned int)entry_size);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001747
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -05001748 new_palette.entries = (png_sPLT_entryp)png_malloc_warn(png_ptr,
1749 (png_alloc_size_t) new_palette.nentries * (sizeof (png_sPLT_entry)));
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001750
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001751 if (new_palette.entries == NULL)
1752 {
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05001753 png_warning(png_ptr, "sPLT chunk requires too much memory");
1754 return;
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001755 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001756
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001757#ifdef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001758 for (i = 0; i < new_palette.nentries; i++)
1759 {
Glenn Randers-Pehrson90b878c2009-10-07 12:44:35 -05001760 pp = new_palette.entries + i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001761
1762 if (new_palette.depth == 8)
1763 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001764 pp->red = *entry_start++;
1765 pp->green = *entry_start++;
1766 pp->blue = *entry_start++;
1767 pp->alpha = *entry_start++;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001768 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001769
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001770 else
1771 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001772 pp->red = png_get_uint_16(entry_start); entry_start += 2;
1773 pp->green = png_get_uint_16(entry_start); entry_start += 2;
1774 pp->blue = png_get_uint_16(entry_start); entry_start += 2;
1775 pp->alpha = png_get_uint_16(entry_start); entry_start += 2;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001776 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001777
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001778 pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
1779 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001780#else
1781 pp = new_palette.entries;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001782
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001783 for (i = 0; i < new_palette.nentries; i++)
1784 {
1785
1786 if (new_palette.depth == 8)
1787 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001788 pp[i].red = *entry_start++;
1789 pp[i].green = *entry_start++;
1790 pp[i].blue = *entry_start++;
1791 pp[i].alpha = *entry_start++;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001792 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001793
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001794 else
1795 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001796 pp[i].red = png_get_uint_16(entry_start); entry_start += 2;
1797 pp[i].green = png_get_uint_16(entry_start); entry_start += 2;
1798 pp[i].blue = png_get_uint_16(entry_start); entry_start += 2;
1799 pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001800 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001801
Glenn Randers-Pehrsonf27592a2011-03-21 18:05:40 -05001802 pp[i].frequency = png_get_uint_16(entry_start); entry_start += 2;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001803 }
1804#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001805
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001806 /* Discard all chunk data except the name and stash that */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001807 new_palette.name = (png_charp)buffer;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001808
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001809 png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001810
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001811 png_free(png_ptr, new_palette.entries);
1812}
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06001813#endif /* READ_sPLT */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001814
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001815#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001816void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001817png_handle_tRNS(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001818{
Glenn Randers-Pehrson2f8b82e2017-10-29 10:30:42 -05001819 png_byte readbuf[PNG_MAX_PALETTE_LENGTH];
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06001820
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001821 png_debug(1, "in png_handle_tRNS");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001822
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001823 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001824 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001825
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001826 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001827 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001828 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001829 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001830 return;
1831 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001832
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001833 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS) != 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001834 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001835 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001836 png_chunk_benign_error(png_ptr, "duplicate");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001837 return;
1838 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001839
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001840 if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
Guy Schalnat0d580581995-07-20 02:43:20 -05001841 {
Glenn Randers-Pehrson2f8b82e2017-10-29 10:30:42 -05001842 png_byte buf[2];
Guy Schalnat0d580581995-07-20 02:43:20 -05001843
1844 if (length != 2)
1845 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001846 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001847 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001848 return;
1849 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001850
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001851 png_crc_read(png_ptr, buf, 2);
1852 png_ptr->num_trans = 1;
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -05001853 png_ptr->trans_color.gray = png_get_uint_16(buf);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001854 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001855
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001856 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
1857 {
Glenn Randers-Pehrson2f8b82e2017-10-29 10:30:42 -05001858 png_byte buf[6];
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001859
1860 if (length != 6)
1861 {
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001862 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001863 png_chunk_benign_error(png_ptr, "invalid");
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001864 return;
1865 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001866
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001867 png_crc_read(png_ptr, buf, length);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001868 png_ptr->num_trans = 1;
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -05001869 png_ptr->trans_color.red = png_get_uint_16(buf);
1870 png_ptr->trans_color.green = png_get_uint_16(buf + 2);
1871 png_ptr->trans_color.blue = png_get_uint_16(buf + 4);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001872 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001873
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001874 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1875 {
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001876 if ((png_ptr->mode & PNG_HAVE_PLTE) == 0)
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001877 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001878 /* TODO: is this actually an error in the ISO spec? */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001879 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001880 png_chunk_benign_error(png_ptr, "out of place");
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001881 return;
1882 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001883
Glenn Randers-Pehrsone485a092015-07-29 07:29:17 -05001884 if (length > (unsigned int) png_ptr->num_palette ||
Glenn Randers-Pehrsonc861dc82015-04-01 12:06:01 -05001885 length > (unsigned int) PNG_MAX_PALETTE_LENGTH ||
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001886 length == 0)
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06001887 {
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06001888 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001889 png_chunk_benign_error(png_ptr, "invalid");
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06001890 return;
1891 }
1892
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001893 png_crc_read(png_ptr, readbuf, length);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001894 png_ptr->num_trans = (png_uint_16)length;
1895 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001896
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001897 else
Guy Schalnate5a37791996-06-05 15:50:50 -05001898 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001899 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001900 png_chunk_benign_error(png_ptr, "invalid with alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -05001901 return;
1902 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001903
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001904 if (png_crc_finish(png_ptr, 0) != 0)
Glenn Randers-Pehrsona7dbcba2007-05-15 16:16:34 -05001905 {
1906 png_ptr->num_trans = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001907 return;
Glenn Randers-Pehrsona7dbcba2007-05-15 16:16:34 -05001908 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001909
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001910 /* TODO: this is a horrible side effect in the palette case because the
1911 * png_struct ends up with a pointer to the tRNS buffer owned by the
1912 * png_info. Fix this.
1913 */
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06001914 png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001915 &(png_ptr->trans_color));
Guy Schalnat0d580581995-07-20 02:43:20 -05001916}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001917#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001918
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001919#ifdef PNG_READ_bKGD_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001920void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001921png_handle_bKGD(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05001922{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001923 unsigned int truelen;
Guy Schalnat0d580581995-07-20 02:43:20 -05001924 png_byte buf[6];
John Bowlercb0b2962011-05-12 21:48:29 -05001925 png_color_16 background;
Guy Schalnat0d580581995-07-20 02:43:20 -05001926
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001927 png_debug(1, "in png_handle_bKGD");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001928
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001929 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001930 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001931
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001932 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0 ||
1933 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
1934 (png_ptr->mode & PNG_HAVE_PLTE) == 0))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001935 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001936 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001937 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001938 return;
1939 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001940
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001941 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) != 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001942 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001943 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001944 png_chunk_benign_error(png_ptr, "duplicate");
Guy Schalnate5a37791996-06-05 15:50:50 -05001945 return;
1946 }
1947
Guy Schalnat0d580581995-07-20 02:43:20 -05001948 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1949 truelen = 1;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001950
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001951 else if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
Guy Schalnat0d580581995-07-20 02:43:20 -05001952 truelen = 6;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001953
Guy Schalnat0d580581995-07-20 02:43:20 -05001954 else
1955 truelen = 2;
1956
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001957 if (length != truelen)
Guy Schalnat0d580581995-07-20 02:43:20 -05001958 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001959 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001960 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnat0d580581995-07-20 02:43:20 -05001961 return;
1962 }
1963
Andreas Dilger47a0c421997-05-16 02:46:07 -05001964 png_crc_read(png_ptr, buf, truelen);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001965
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001966 if (png_crc_finish(png_ptr, 0) != 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001967 return;
1968
Guy Schalnate5a37791996-06-05 15:50:50 -05001969 /* We convert the index value into RGB components so that we can allow
1970 * arbitrary RGB values for background when we have transparency, and
1971 * so it is easy to determine the RGB values of the background color
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001972 * from the info_ptr struct.
1973 */
Guy Schalnat0d580581995-07-20 02:43:20 -05001974 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Guy Schalnate5a37791996-06-05 15:50:50 -05001975 {
John Bowlercb0b2962011-05-12 21:48:29 -05001976 background.index = buf[0];
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001977
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001978 if (info_ptr != NULL && info_ptr->num_palette != 0)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001979 {
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001980 if (buf[0] >= info_ptr->num_palette)
1981 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001982 png_chunk_benign_error(png_ptr, "invalid index");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001983 return;
1984 }
1985
John Bowlercb0b2962011-05-12 21:48:29 -05001986 background.red = (png_uint_16)png_ptr->palette[buf[0]].red;
1987 background.green = (png_uint_16)png_ptr->palette[buf[0]].green;
1988 background.blue = (png_uint_16)png_ptr->palette[buf[0]].blue;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001989 }
John Bowlercb0b2962011-05-12 21:48:29 -05001990
1991 else
1992 background.red = background.green = background.blue = 0;
1993
1994 background.gray = 0;
Guy Schalnate5a37791996-06-05 15:50:50 -05001995 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06001996
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001997 else if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0) /* GRAY */
Guy Schalnate5a37791996-06-05 15:50:50 -05001998 {
Glenn Randers-Pehrson84e6e352018-01-07 17:43:09 -06001999 if (png_ptr->bit_depth <= 8)
2000 {
2001 if (buf[0] != 0 || buf[1] >= (unsigned int)(1 << png_ptr->bit_depth))
2002 {
2003 png_chunk_benign_error(png_ptr, "invalid gray level");
2004 return;
2005 }
2006 }
2007
John Bowlercb0b2962011-05-12 21:48:29 -05002008 background.index = 0;
2009 background.red =
2010 background.green =
2011 background.blue =
2012 background.gray = png_get_uint_16(buf);
Guy Schalnate5a37791996-06-05 15:50:50 -05002013 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002014
Guy Schalnat0d580581995-07-20 02:43:20 -05002015 else
2016 {
Glenn Randers-Pehrson84e6e352018-01-07 17:43:09 -06002017 if (png_ptr->bit_depth <= 8)
2018 {
2019 if (buf[0] != 0 || buf[2] != 0 || buf[4] != 0)
2020 {
2021 png_chunk_benign_error(png_ptr, "invalid color");
2022 return;
2023 }
2024 }
2025
John Bowlercb0b2962011-05-12 21:48:29 -05002026 background.index = 0;
2027 background.red = png_get_uint_16(buf);
2028 background.green = png_get_uint_16(buf + 2);
2029 background.blue = png_get_uint_16(buf + 4);
2030 background.gray = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -05002031 }
2032
John Bowlercb0b2962011-05-12 21:48:29 -05002033 png_set_bKGD(png_ptr, info_ptr, &background);
Guy Schalnat0d580581995-07-20 02:43:20 -05002034}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002035#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002036
Glenn Randers-Pehrson68cb0aa2017-07-13 11:19:53 -05002037#ifdef PNG_READ_eXIf_SUPPORTED
2038void /* PRIVATE */
2039png_handle_eXIf(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2040{
2041 unsigned int i;
Glenn Randers-Pehrson71a56182017-08-01 21:42:16 -05002042
Glenn Randers-Pehrson68cb0aa2017-07-13 11:19:53 -05002043 png_debug(1, "in png_handle_eXIf");
2044
2045 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2046 png_chunk_error(png_ptr, "missing IHDR");
2047
Glenn Randers-Pehrsoncf713fb2017-08-06 10:24:04 -05002048 if (length < 2)
2049 {
2050 png_crc_finish(png_ptr, length);
2051 png_chunk_benign_error(png_ptr, "too short");
2052 return;
2053 }
2054
Glenn Randers-Pehrson4ab78af2017-07-30 19:36:25 -05002055 else if (info_ptr == NULL || (info_ptr->valid & PNG_INFO_eXIf) != 0)
Glenn Randers-Pehrson68cb0aa2017-07-13 11:19:53 -05002056 {
2057 png_crc_finish(png_ptr, length);
2058 png_chunk_benign_error(png_ptr, "duplicate");
2059 return;
2060 }
2061
Glenn Randers-Pehrson1ebe4f72017-08-05 19:58:41 -05002062 info_ptr->free_me |= PNG_FREE_EXIF;
2063
Glenn Randers-Pehrsonfd6b8722017-08-02 06:35:15 -05002064 info_ptr->eXIf_buf = png_voidcast(png_bytep,
Glenn Randers-Pehrson68cb0aa2017-07-13 11:19:53 -05002065 png_malloc_warn(png_ptr, length));
2066
Glenn Randers-Pehrsonfd6b8722017-08-02 06:35:15 -05002067 if (info_ptr->eXIf_buf == NULL)
Glenn Randers-Pehrson71a56182017-08-01 21:42:16 -05002068 {
2069 png_crc_finish(png_ptr, length);
2070 png_chunk_benign_error(png_ptr, "out of memory");
2071 return;
2072 }
2073
Glenn Randers-Pehrson68cb0aa2017-07-13 11:19:53 -05002074 for (i = 0; i < length; i++)
2075 {
2076 png_byte buf[1];
2077 png_crc_read(png_ptr, buf, 1);
Glenn Randers-Pehrsonfd6b8722017-08-02 06:35:15 -05002078 info_ptr->eXIf_buf[i] = buf[0];
Glenn Randers-Pehrson33787342017-08-06 10:35:23 -05002079 if (i == 1 && buf[0] != 'M' && buf[0] != 'I'
Glenn Randers-Pehrsoncf713fb2017-08-06 10:24:04 -05002080 && info_ptr->eXIf_buf[0] != buf[0])
2081 {
2082 png_crc_finish(png_ptr, length);
2083 png_chunk_benign_error(png_ptr, "incorrect byte-order specifier");
Glenn Randers-Pehrsonc362a8c2017-08-07 06:31:58 -05002084 png_free(png_ptr, info_ptr->eXIf_buf);
2085 info_ptr->eXIf_buf = NULL;
Glenn Randers-Pehrsoncf713fb2017-08-06 10:24:04 -05002086 return;
2087 }
Glenn Randers-Pehrson68cb0aa2017-07-13 11:19:53 -05002088 }
2089
2090 if (png_crc_finish(png_ptr, 0) != 0)
2091 return;
2092
Glenn Randers-Pehrsond930d362017-08-03 10:29:10 -05002093 png_set_eXIf_1(png_ptr, info_ptr, length, info_ptr->eXIf_buf);
Glenn Randers-Pehrson4ab78af2017-07-30 19:36:25 -05002094
Glenn Randers-Pehrsonfd6b8722017-08-02 06:35:15 -05002095 png_free(png_ptr, info_ptr->eXIf_buf);
2096 info_ptr->eXIf_buf = NULL;
Glenn Randers-Pehrson68cb0aa2017-07-13 11:19:53 -05002097}
2098#endif
2099
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002100#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002101void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002102png_handle_hIST(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002103{
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05002104 unsigned int num, i;
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05002105 png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];
Guy Schalnat0d580581995-07-20 02:43:20 -05002106
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002107 png_debug(1, "in png_handle_hIST");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002108
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002109 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002110 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002111
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002112 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0 ||
2113 (png_ptr->mode & PNG_HAVE_PLTE) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002114 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002115 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002116 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002117 return;
2118 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002119
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002120 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) != 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002121 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002122 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002123 png_chunk_benign_error(png_ptr, "duplicate");
Guy Schalnate5a37791996-06-05 15:50:50 -05002124 return;
2125 }
2126
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05002127 num = length / 2 ;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002128
Glenn Randers-Pehrsone485a092015-07-29 07:29:17 -05002129 if (num != (unsigned int) png_ptr->num_palette ||
2130 num > (unsigned int) PNG_MAX_PALETTE_LENGTH)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002131 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002132 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002133 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002134 return;
2135 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002136
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002137 for (i = 0; i < num; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05002138 {
2139 png_byte buf[2];
2140
2141 png_crc_read(png_ptr, buf, 2);
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06002142 readbuf[i] = png_get_uint_16(buf);
Guy Schalnat0d580581995-07-20 02:43:20 -05002143 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002144
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002145 if (png_crc_finish(png_ptr, 0) != 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002146 return;
2147
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06002148 png_set_hIST(png_ptr, info_ptr, readbuf);
Guy Schalnat0d580581995-07-20 02:43:20 -05002149}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002150#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002151
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002152#ifdef PNG_READ_pHYs_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002153void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002154png_handle_pHYs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002155{
2156 png_byte buf[9];
2157 png_uint_32 res_x, res_y;
2158 int unit_type;
2159
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002160 png_debug(1, "in png_handle_pHYs");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002161
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002162 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002163 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002164
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002165 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002166 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002167 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002168 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002169 return;
2170 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002171
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002172 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs) != 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002173 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002174 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002175 png_chunk_benign_error(png_ptr, "duplicate");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002176 return;
2177 }
Guy Schalnate5a37791996-06-05 15:50:50 -05002178
Guy Schalnat0d580581995-07-20 02:43:20 -05002179 if (length != 9)
2180 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002181 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002182 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnat0d580581995-07-20 02:43:20 -05002183 return;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002184 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002185
2186 png_crc_read(png_ptr, buf, 9);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002187
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002188 if (png_crc_finish(png_ptr, 0) != 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002189 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05002190
2191 res_x = png_get_uint_32(buf);
2192 res_y = png_get_uint_32(buf + 4);
2193 unit_type = buf[8];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002194 png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
Guy Schalnat0d580581995-07-20 02:43:20 -05002195}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002196#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002197
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002198#ifdef PNG_READ_oFFs_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002199void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002200png_handle_oFFs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002201{
2202 png_byte buf[9];
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002203 png_int_32 offset_x, offset_y;
Guy Schalnat0d580581995-07-20 02:43:20 -05002204 int unit_type;
2205
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002206 png_debug(1, "in png_handle_oFFs");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002207
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002208 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002209 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002210
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002211 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002212 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002213 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002214 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002215 return;
2216 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002217
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002218 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs) != 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002219 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002220 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002221 png_chunk_benign_error(png_ptr, "duplicate");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002222 return;
2223 }
Guy Schalnate5a37791996-06-05 15:50:50 -05002224
Guy Schalnat0d580581995-07-20 02:43:20 -05002225 if (length != 9)
2226 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002227 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002228 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnat0d580581995-07-20 02:43:20 -05002229 return;
2230 }
2231
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002232 png_crc_read(png_ptr, buf, 9);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002233
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002234 if (png_crc_finish(png_ptr, 0) != 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002235 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05002236
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002237 offset_x = png_get_int_32(buf);
2238 offset_y = png_get_int_32(buf + 4);
Guy Schalnat0d580581995-07-20 02:43:20 -05002239 unit_type = buf[8];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002240 png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
2241}
2242#endif
2243
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002244#ifdef PNG_READ_pCAL_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002245/* Read the pCAL chunk (described in the PNG Extensions document) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002246void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002247png_handle_pCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002248{
Andreas Dilger47a0c421997-05-16 02:46:07 -05002249 png_int_32 X0, X1;
2250 png_byte type, nparams;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002251 png_bytep buffer, buf, units, endptr;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002252 png_charpp params;
2253 int i;
2254
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002255 png_debug(1, "in png_handle_pCAL");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002256
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002257 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002258 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002259
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002260 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002261 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002262 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002263 png_chunk_benign_error(png_ptr, "out of place");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002264 return;
2265 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002266
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002267 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL) != 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002268 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002269 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002270 png_chunk_benign_error(png_ptr, "duplicate");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002271 return;
2272 }
2273
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002274 png_debug1(2, "Allocating and reading pCAL chunk data (%u bytes)",
2275 length + 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002276
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002277 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
2278
2279 if (buffer == NULL)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002280 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002281 png_crc_finish(png_ptr, length);
2282 png_chunk_benign_error(png_ptr, "out of memory");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002283 return;
2284 }
2285
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002286 png_crc_read(png_ptr, buffer, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002287
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002288 if (png_crc_finish(png_ptr, 0) != 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002289 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002290
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002291 buffer[length] = 0; /* Null terminate the last string */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002292
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002293 png_debug(3, "Finding end of pCAL purpose string");
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002294 for (buf = buffer; *buf; buf++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002295 /* Empty loop */ ;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002296
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002297 endptr = buffer + length;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002298
2299 /* We need to have at least 12 bytes after the purpose string
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002300 * in order to get the parameter information.
2301 */
Glenn Randers-Pehrson27f08ac2015-11-22 22:59:47 -06002302 if (endptr - buf <= 12)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002303 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002304 png_chunk_benign_error(png_ptr, "invalid");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002305 return;
2306 }
2307
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002308 png_debug(3, "Reading pCAL X0, X1, type, nparams, and units");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002309 X0 = png_get_int_32((png_bytep)buf+1);
2310 X1 = png_get_int_32((png_bytep)buf+5);
2311 type = buf[9];
2312 nparams = buf[10];
2313 units = buf + 11;
2314
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002315 png_debug(3, "Checking pCAL equation type and number of parameters");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002316 /* Check that we have the right number of parameters for known
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002317 * equation types.
2318 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002319 if ((type == PNG_EQUATION_LINEAR && nparams != 2) ||
2320 (type == PNG_EQUATION_BASE_E && nparams != 3) ||
2321 (type == PNG_EQUATION_ARBITRARY && nparams != 3) ||
2322 (type == PNG_EQUATION_HYPERBOLIC && nparams != 4))
2323 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002324 png_chunk_benign_error(png_ptr, "invalid parameter count");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002325 return;
2326 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002327
Andreas Dilger47a0c421997-05-16 02:46:07 -05002328 else if (type >= PNG_EQUATION_LAST)
2329 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002330 png_chunk_benign_error(png_ptr, "unrecognized equation type");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002331 }
2332
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002333 for (buf = units; *buf; buf++)
Glenn Randers-Pehrsonf9f2fe01998-03-15 18:20:23 -06002334 /* Empty loop to move past the units string. */ ;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002335
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002336 png_debug(3, "Allocating pCAL parameters array");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002337
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002338 params = png_voidcast(png_charpp, png_malloc_warn(png_ptr,
2339 nparams * (sizeof (png_charp))));
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002340
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002341 if (params == NULL)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002342 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002343 png_chunk_benign_error(png_ptr, "out of memory");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002344 return;
2345 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002346
2347 /* Get pointers to the start of each parameter string. */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002348 for (i = 0; i < nparams; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002349 {
2350 buf++; /* Skip the null string terminator from previous parameter. */
2351
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002352 png_debug1(3, "Reading pCAL parameter %d", i);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002353
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002354 for (params[i] = (png_charp)buf; buf <= endptr && *buf != 0; buf++)
Glenn Randers-Pehrsonf9f2fe01998-03-15 18:20:23 -06002355 /* Empty loop to move past each parameter string */ ;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002356
2357 /* Make sure we haven't run out of data yet */
2358 if (buf > endptr)
2359 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002360 png_free(png_ptr, params);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002361 png_chunk_benign_error(png_ptr, "invalid data");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002362 return;
2363 }
2364 }
2365
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002366 png_set_pCAL(png_ptr, info_ptr, (png_charp)buffer, X0, X1, type, nparams,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05002367 (png_charp)units, params);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002368
Andreas Dilger47a0c421997-05-16 02:46:07 -05002369 png_free(png_ptr, params);
Guy Schalnat0d580581995-07-20 02:43:20 -05002370}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002371#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002372
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002373#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002374/* Read the sCAL chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002375void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002376png_handle_sCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002377{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002378 png_bytep buffer;
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04002379 size_t i;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002380 int state;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002381
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002382 png_debug(1, "in png_handle_sCAL");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002383
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002384 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002385 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002386
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002387 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002388 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002389 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002390 png_chunk_benign_error(png_ptr, "out of place");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002391 return;
2392 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002393
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002394 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL) != 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002395 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002396 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002397 png_chunk_benign_error(png_ptr, "duplicate");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002398 return;
2399 }
2400
Glenn Randers-Pehrson254a5132011-06-10 17:45:48 -05002401 /* Need unit type, width, \0, height: minimum 4 bytes */
2402 else if (length < 4)
2403 {
Glenn Randers-Pehrson254a5132011-06-10 17:45:48 -05002404 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002405 png_chunk_benign_error(png_ptr, "invalid");
Glenn Randers-Pehrson254a5132011-06-10 17:45:48 -05002406 return;
2407 }
2408
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002409 png_debug1(2, "Allocating and reading sCAL chunk data (%u bytes)",
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05002410 length + 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002411
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002412 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002413
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002414 if (buffer == NULL)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002415 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002416 png_chunk_benign_error(png_ptr, "out of memory");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002417 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002418 return;
2419 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002420
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002421 png_crc_read(png_ptr, buffer, length);
2422 buffer[length] = 0; /* Null terminate the last string */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002423
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002424 if (png_crc_finish(png_ptr, 0) != 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002425 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002426
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002427 /* Validate the unit. */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002428 if (buffer[0] != 1 && buffer[0] != 2)
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -05002429 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002430 png_chunk_benign_error(png_ptr, "invalid unit");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05002431 return;
2432 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002433
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002434 /* Validate the ASCII numbers, need two ASCII numbers separated by
2435 * a '\0' and they need to fit exactly in the chunk data.
2436 */
Glenn Randers-Pehrson254a5132011-06-10 17:45:48 -05002437 i = 1;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002438 state = 0;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002439
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002440 if (png_check_fp_number((png_const_charp)buffer, length, &state, &i) == 0 ||
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002441 i >= length || buffer[i++] != 0)
2442 png_chunk_benign_error(png_ptr, "bad width format");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002443
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002444 else if (PNG_FP_IS_POSITIVE(state) == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002445 png_chunk_benign_error(png_ptr, "non-positive width");
John Bowler8d261262011-06-18 13:37:11 -05002446
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002447 else
Glenn Randers-Pehrson20788d32011-01-06 09:01:04 -06002448 {
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04002449 size_t heighti = i;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002450
Glenn Randers-Pehrson254a5132011-06-10 17:45:48 -05002451 state = 0;
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002452 if (png_check_fp_number((png_const_charp)buffer, length,
2453 &state, &i) == 0 || i != length)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002454 png_chunk_benign_error(png_ptr, "bad height format");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002455
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002456 else if (PNG_FP_IS_POSITIVE(state) == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002457 png_chunk_benign_error(png_ptr, "non-positive height");
John Bowler8d261262011-06-18 13:37:11 -05002458
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002459 else
2460 /* This is the (only) success case. */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002461 png_set_sCAL_s(png_ptr, info_ptr, buffer[0],
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05002462 (png_charp)buffer+1, (png_charp)buffer+heighti);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002463 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002464}
2465#endif
2466
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002467#ifdef PNG_READ_tIME_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002468void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002469png_handle_tIME(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002470{
2471 png_byte buf[7];
2472 png_time mod_time;
2473
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002474 png_debug(1, "in png_handle_tIME");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002475
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002476 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002477 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002478
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002479 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME) != 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002480 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002481 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002482 png_chunk_benign_error(png_ptr, "duplicate");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002483 return;
2484 }
2485
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002486 if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002487 png_ptr->mode |= PNG_AFTER_IDAT;
Guy Schalnate5a37791996-06-05 15:50:50 -05002488
Guy Schalnat0d580581995-07-20 02:43:20 -05002489 if (length != 7)
2490 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002491 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002492 png_chunk_benign_error(png_ptr, "invalid");
Guy Schalnat0d580581995-07-20 02:43:20 -05002493 return;
2494 }
2495
2496 png_crc_read(png_ptr, buf, 7);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002497
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002498 if (png_crc_finish(png_ptr, 0) != 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002499 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05002500
2501 mod_time.second = buf[6];
2502 mod_time.minute = buf[5];
2503 mod_time.hour = buf[4];
2504 mod_time.day = buf[3];
2505 mod_time.month = buf[2];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002506 mod_time.year = png_get_uint_16(buf);
Guy Schalnat0d580581995-07-20 02:43:20 -05002507
Andreas Dilger47a0c421997-05-16 02:46:07 -05002508 png_set_tIME(png_ptr, info_ptr, &mod_time);
Guy Schalnat0d580581995-07-20 02:43:20 -05002509}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002510#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002511
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002512#ifdef PNG_READ_tEXt_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002513/* Note: this does not properly handle chunks that are > 64K under DOS */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002514void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002515png_handle_tEXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002516{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002517 png_text text_info;
2518 png_bytep buffer;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002519 png_charp key;
Guy Schalnat6d764711995-12-19 03:22:19 -06002520 png_charp text;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002521 png_uint_32 skip = 0;
2522
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002523 png_debug(1, "in png_handle_tEXt");
Guy Schalnat0d580581995-07-20 02:43:20 -05002524
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06002525#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002526 if (png_ptr->user_chunk_cache_max != 0)
2527 {
2528 if (png_ptr->user_chunk_cache_max == 1)
2529 {
2530 png_crc_finish(png_ptr, length);
2531 return;
2532 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002533
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002534 if (--png_ptr->user_chunk_cache_max == 1)
2535 {
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002536 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002537 png_chunk_benign_error(png_ptr, "no space in chunk cache");
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002538 return;
2539 }
2540 }
2541#endif
2542
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002543 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002544 png_chunk_error(png_ptr, "missing IHDR");
Guy Schalnate5a37791996-06-05 15:50:50 -05002545
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002546 if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002547 png_ptr->mode |= PNG_AFTER_IDAT;
2548
Andreas Dilger47a0c421997-05-16 02:46:07 -05002549#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002550 if (length > 65535U)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002551 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002552 png_crc_finish(png_ptr, length);
2553 png_chunk_benign_error(png_ptr, "too large to fit in memory");
2554 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002555 }
2556#endif
2557
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002558 buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
Glenn Randers-Pehrson97a9b482008-10-25 20:03:28 -05002559
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002560 if (buffer == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002561 {
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -05002562 png_chunk_benign_error(png_ptr, "out of memory");
2563 return;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002564 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002565
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002566 png_crc_read(png_ptr, buffer, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002567
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002568 if (png_crc_finish(png_ptr, skip) != 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002569 return;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002570
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002571 key = (png_charp)buffer;
2572 key[length] = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -05002573
2574 for (text = key; *text; text++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002575 /* Empty loop to find end of key */ ;
Guy Schalnat0d580581995-07-20 02:43:20 -05002576
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002577 if (text != key + length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002578 text++;
2579
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002580 text_info.compression = PNG_TEXT_COMPRESSION_NONE;
2581 text_info.key = key;
2582 text_info.lang = NULL;
2583 text_info.lang_key = NULL;
2584 text_info.itxt_length = 0;
2585 text_info.text = text;
2586 text_info.text_length = strlen(text);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002587
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002588 if (png_set_text_2(png_ptr, info_ptr, &text_info, 1) != 0)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002589 png_warning(png_ptr, "Insufficient memory to process text chunk");
Guy Schalnat0d580581995-07-20 02:43:20 -05002590}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002591#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002592
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002593#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002594/* Note: this does not correctly handle chunks that are > 64K under DOS */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002595void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002596png_handle_zTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -05002597{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002598 png_const_charp errmsg = NULL;
2599 png_bytep buffer;
2600 png_uint_32 keyword_length;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002601
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002602 png_debug(1, "in png_handle_zTXt");
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002603
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06002604#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002605 if (png_ptr->user_chunk_cache_max != 0)
2606 {
2607 if (png_ptr->user_chunk_cache_max == 1)
2608 {
2609 png_crc_finish(png_ptr, length);
2610 return;
2611 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002612
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002613 if (--png_ptr->user_chunk_cache_max == 1)
2614 {
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002615 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002616 png_chunk_benign_error(png_ptr, "no space in chunk cache");
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002617 return;
2618 }
2619 }
2620#endif
2621
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002622 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002623 png_chunk_error(png_ptr, "missing IHDR");
Guy Schalnate5a37791996-06-05 15:50:50 -05002624
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002625 if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002626 png_ptr->mode |= PNG_AFTER_IDAT;
2627
Glenn Randers-Pehrson2eff8ef2017-04-22 15:30:52 -05002628 /* Note, "length" is sufficient here; we won't be adding
2629 * a null terminator later.
2630 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002631 buffer = png_read_buffer(png_ptr, length, 2/*silent*/);
2632
2633 if (buffer == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002634 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002635 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002636 png_chunk_benign_error(png_ptr, "out of memory");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002637 return;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002638 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002639
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002640 png_crc_read(png_ptr, buffer, length);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002641
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002642 if (png_crc_finish(png_ptr, 0) != 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002643 return;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002644
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002645 /* TODO: also check that the keyword contents match the spec! */
2646 for (keyword_length = 0;
2647 keyword_length < length && buffer[keyword_length] != 0;
2648 ++keyword_length)
2649 /* Empty loop to find end of name */ ;
Guy Schalnat0d580581995-07-20 02:43:20 -05002650
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002651 if (keyword_length > 79 || keyword_length < 1)
2652 errmsg = "bad keyword";
Guy Schalnat0d580581995-07-20 02:43:20 -05002653
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002654 /* zTXt must have some LZ data after the keyword, although it may expand to
2655 * zero bytes; we need a '\0' at the end of the keyword, the compression type
2656 * then the LZ data:
2657 */
2658 else if (keyword_length + 3 > length)
2659 errmsg = "truncated";
2660
2661 else if (buffer[keyword_length+1] != PNG_COMPRESSION_TYPE_BASE)
2662 errmsg = "unknown compression type";
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002663
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002664 else
Guy Schalnat0d580581995-07-20 02:43:20 -05002665 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002666 png_alloc_size_t uncompressed_length = PNG_SIZE_MAX;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002667
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002668 /* TODO: at present png_decompress_chunk imposes a single application
2669 * level memory limit, this should be split to different values for iCCP
2670 * and text chunks.
2671 */
2672 if (png_decompress_chunk(png_ptr, length, keyword_length+2,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05002673 &uncompressed_length, 1/*terminate*/) == Z_STREAM_END)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002674 {
2675 png_text text;
John Bowler0ae4f7b2012-03-03 21:10:26 -06002676
Glenn Randers-Pehrson39d84f42017-08-05 20:51:23 -05002677 if (png_ptr->read_buffer == NULL)
2678 errmsg="Read failure in png_handle_zTXt";
2679 else
2680 {
2681 /* It worked; png_ptr->read_buffer now looks like a tEXt chunk
2682 * except for the extra compression type byte and the fact that
2683 * it isn't necessarily '\0' terminated.
2684 */
2685 buffer = png_ptr->read_buffer;
2686 buffer[uncompressed_length+(keyword_length+2)] = 0;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002687
Glenn Randers-Pehrson39d84f42017-08-05 20:51:23 -05002688 text.compression = PNG_TEXT_COMPRESSION_zTXt;
2689 text.key = (png_charp)buffer;
2690 text.text = (png_charp)(buffer + keyword_length+2);
2691 text.text_length = uncompressed_length;
2692 text.itxt_length = 0;
2693 text.lang = NULL;
2694 text.lang_key = NULL;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002695
Glenn Randers-Pehrson39d84f42017-08-05 20:51:23 -05002696 if (png_set_text_2(png_ptr, info_ptr, &text, 1) != 0)
2697 errmsg = "insufficient memory";
2698 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002699 }
2700
2701 else
2702 errmsg = png_ptr->zstream.msg;
John Bowlerb5d00512012-03-09 09:15:18 -06002703 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002704
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002705 if (errmsg != NULL)
2706 png_chunk_benign_error(png_ptr, errmsg);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002707}
2708#endif
2709
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002710#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002711/* Note: this does not correctly handle chunks that are > 64K under DOS */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002712void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002713png_handle_iTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002714{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002715 png_const_charp errmsg = NULL;
2716 png_bytep buffer;
2717 png_uint_32 prefix_length;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002718
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002719 png_debug(1, "in png_handle_iTXt");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002720
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06002721#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002722 if (png_ptr->user_chunk_cache_max != 0)
2723 {
2724 if (png_ptr->user_chunk_cache_max == 1)
2725 {
2726 png_crc_finish(png_ptr, length);
2727 return;
2728 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002729
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002730 if (--png_ptr->user_chunk_cache_max == 1)
2731 {
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002732 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002733 png_chunk_benign_error(png_ptr, "no space in chunk cache");
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05002734 return;
2735 }
2736 }
2737#endif
2738
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002739 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002740 png_chunk_error(png_ptr, "missing IHDR");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002741
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002742 if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002743 png_ptr->mode |= PNG_AFTER_IDAT;
2744
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002745 buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
2746
2747 if (buffer == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002748 {
John Bowlerb11b31a2012-03-21 07:55:46 -05002749 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002750 png_chunk_benign_error(png_ptr, "out of memory");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002751 return;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05002752 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002753
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002754 png_crc_read(png_ptr, buffer, length);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002755
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002756 if (png_crc_finish(png_ptr, 0) != 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002757 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002758
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002759 /* First the keyword. */
2760 for (prefix_length=0;
2761 prefix_length < length && buffer[prefix_length] != 0;
2762 ++prefix_length)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002763 /* Empty loop */ ;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002764
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002765 /* Perform a basic check on the keyword length here. */
2766 if (prefix_length > 79 || prefix_length < 1)
2767 errmsg = "bad keyword";
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002768
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002769 /* Expect keyword, compression flag, compression type, language, translated
2770 * keyword (both may be empty but are 0 terminated) then the text, which may
2771 * be empty.
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002772 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002773 else if (prefix_length + 5 > length)
2774 errmsg = "truncated";
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002775
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002776 else if (buffer[prefix_length+1] == 0 ||
2777 (buffer[prefix_length+1] == 1 &&
2778 buffer[prefix_length+2] == PNG_COMPRESSION_TYPE_BASE))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002779 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002780 int compressed = buffer[prefix_length+1] != 0;
2781 png_uint_32 language_offset, translated_keyword_offset;
2782 png_alloc_size_t uncompressed_length = 0;
2783
2784 /* Now the language tag */
2785 prefix_length += 3;
2786 language_offset = prefix_length;
2787
2788 for (; prefix_length < length && buffer[prefix_length] != 0;
2789 ++prefix_length)
2790 /* Empty loop */ ;
2791
2792 /* WARNING: the length may be invalid here, this is checked below. */
2793 translated_keyword_offset = ++prefix_length;
2794
2795 for (; prefix_length < length && buffer[prefix_length] != 0;
2796 ++prefix_length)
2797 /* Empty loop */ ;
2798
2799 /* prefix_length should now be at the trailing '\0' of the translated
2800 * keyword, but it may already be over the end. None of this arithmetic
2801 * can overflow because chunks are at most 2^31 bytes long, but on 16-bit
Glenn Randers-Pehrson3efbeca2014-07-21 16:57:30 -05002802 * systems the available allocation may overflow.
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002803 */
2804 ++prefix_length;
2805
Glenn Randers-Pehrsonebba0742014-10-25 12:22:39 -05002806 if (compressed == 0 && prefix_length <= length)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002807 uncompressed_length = length - prefix_length;
2808
Glenn Randers-Pehrsonebba0742014-10-25 12:22:39 -05002809 else if (compressed != 0 && prefix_length < length)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002810 {
2811 uncompressed_length = PNG_SIZE_MAX;
2812
2813 /* TODO: at present png_decompress_chunk imposes a single application
2814 * level memory limit, this should be split to different values for
2815 * iCCP and text chunks.
2816 */
2817 if (png_decompress_chunk(png_ptr, length, prefix_length,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05002818 &uncompressed_length, 1/*terminate*/) == Z_STREAM_END)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002819 buffer = png_ptr->read_buffer;
2820
2821 else
2822 errmsg = png_ptr->zstream.msg;
2823 }
2824
2825 else
2826 errmsg = "truncated";
2827
2828 if (errmsg == NULL)
2829 {
2830 png_text text;
2831
2832 buffer[uncompressed_length+prefix_length] = 0;
2833
Glenn Randers-Pehrson48e6fad2014-09-27 09:37:50 -05002834 if (compressed == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002835 text.compression = PNG_ITXT_COMPRESSION_NONE;
2836
2837 else
2838 text.compression = PNG_ITXT_COMPRESSION_zTXt;
2839
2840 text.key = (png_charp)buffer;
2841 text.lang = (png_charp)buffer + language_offset;
2842 text.lang_key = (png_charp)buffer + translated_keyword_offset;
2843 text.text = (png_charp)buffer + prefix_length;
2844 text.text_length = 0;
2845 text.itxt_length = uncompressed_length;
2846
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002847 if (png_set_text_2(png_ptr, info_ptr, &text, 1) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002848 errmsg = "insufficient memory";
2849 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002850 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002851
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002852 else
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002853 errmsg = "bad compression info";
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002854
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002855 if (errmsg != NULL)
2856 png_chunk_benign_error(png_ptr, errmsg);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002857}
2858#endif
2859
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06002860#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002861/* Utility function for png_handle_unknown; set up png_ptr::unknown_chunk */
2862static int
2863png_cache_unknown_chunk(png_structrp png_ptr, png_uint_32 length)
2864{
2865 png_alloc_size_t limit = PNG_SIZE_MAX;
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06002866
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002867 if (png_ptr->unknown_chunk.data != NULL)
2868 {
2869 png_free(png_ptr, png_ptr->unknown_chunk.data);
2870 png_ptr->unknown_chunk.data = NULL;
2871 }
2872
Glenn Randers-Pehrsond3445892015-03-27 08:58:32 -05002873# ifdef PNG_SET_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05002874 if (png_ptr->user_chunk_malloc_max > 0 &&
2875 png_ptr->user_chunk_malloc_max < limit)
2876 limit = png_ptr->user_chunk_malloc_max;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002877
2878# elif PNG_USER_CHUNK_MALLOC_MAX > 0
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05002879 if (PNG_USER_CHUNK_MALLOC_MAX < limit)
2880 limit = PNG_USER_CHUNK_MALLOC_MAX;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002881# endif
2882
2883 if (length <= limit)
2884 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002885 PNG_CSTRING_FROM_CHUNK(png_ptr->unknown_chunk.name, png_ptr->chunk_name);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002886 /* The following is safe because of the PNG_SIZE_MAX init above */
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04002887 png_ptr->unknown_chunk.size = (size_t)length/*SAFE*/;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002888 /* 'mode' is a flag array, only the bottom four bits matter here */
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05002889 png_ptr->unknown_chunk.location = (png_byte)png_ptr->mode/*SAFE*/;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002890
2891 if (length == 0)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002892 png_ptr->unknown_chunk.data = NULL;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06002893
2894 else
2895 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002896 /* Do a 'warn' here - it is handled below. */
2897 png_ptr->unknown_chunk.data = png_voidcast(png_bytep,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05002898 png_malloc_warn(png_ptr, length));
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06002899 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002900 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002901
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002902 if (png_ptr->unknown_chunk.data == NULL && length > 0)
2903 {
2904 /* This is benign because we clean up correctly */
2905 png_crc_finish(png_ptr, length);
2906 png_chunk_benign_error(png_ptr, "unknown chunk exceeds memory limits");
2907 return 0;
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06002908 }
John Bowlere9567512012-08-15 22:53:00 -05002909
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06002910 else
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002911 {
2912 if (length > 0)
2913 png_crc_read(png_ptr, png_ptr->unknown_chunk.data, length);
2914 png_crc_finish(png_ptr, 0);
2915 return 1;
2916 }
2917}
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06002918#endif /* READ_UNKNOWN_CHUNKS */
John Bowlere9567512012-08-15 22:53:00 -05002919
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002920/* Handle an unknown, or known but disabled, chunk */
2921void /* PRIVATE */
2922png_handle_unknown(png_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05002923 png_uint_32 length, int keep)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002924{
2925 int handled = 0; /* the chunk was handled */
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06002926
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002927 png_debug(1, "in png_handle_unknown");
2928
Glenn Randers-Pehrsonb3721752013-09-30 13:56:44 -05002929#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002930 /* NOTE: this code is based on the code in libpng-1.4.12 except for fixing
2931 * the bug which meant that setting a non-default behavior for a specific
2932 * chunk would be ignored (the default was always used unless a user
2933 * callback was installed).
2934 *
2935 * 'keep' is the value from the png_chunk_unknown_handling, the setting for
2936 * this specific chunk_name, if PNG_HANDLE_AS_UNKNOWN_SUPPORTED, if not it
2937 * will always be PNG_HANDLE_CHUNK_AS_DEFAULT and it needs to be set here.
2938 * This is just an optimization to avoid multiple calls to the lookup
2939 * function.
2940 */
Glenn Randers-Pehrson873f16f2013-09-20 14:28:50 -05002941# ifndef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsonb3721752013-09-30 13:56:44 -05002942# ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05002943 keep = png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name);
Glenn Randers-Pehrsonb3721752013-09-30 13:56:44 -05002944# endif
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002945# endif
2946
2947 /* One of the following methods will read the chunk or skip it (at least one
2948 * of these is always defined because this is the only way to switch on
2949 * PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
2950 */
2951# ifdef PNG_READ_USER_CHUNKS_SUPPORTED
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05002952 /* The user callback takes precedence over the chunk keep value, but the
2953 * keep value is still required to validate a save of a critical chunk.
2954 */
2955 if (png_ptr->read_user_chunk_fn != NULL)
2956 {
2957 if (png_cache_unknown_chunk(png_ptr, length) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002958 {
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05002959 /* Callback to user unknown chunk handler */
2960 int ret = (*(png_ptr->read_user_chunk_fn))(png_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05002961 &png_ptr->unknown_chunk);
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05002962
2963 /* ret is:
2964 * negative: An error occurred; png_chunk_error will be called.
2965 * zero: The chunk was not handled, the chunk will be discarded
2966 * unless png_set_keep_unknown_chunks has been used to set
2967 * a 'keep' behavior for this particular chunk, in which
2968 * case that will be used. A critical chunk will cause an
2969 * error at this point unless it is to be saved.
2970 * positive: The chunk was handled, libpng will ignore/discard it.
2971 */
2972 if (ret < 0)
2973 png_chunk_error(png_ptr, "error in user chunk");
2974
2975 else if (ret == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002976 {
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05002977 /* If the keep value is 'default' or 'never' override it, but
2978 * still error out on critical chunks unless the keep value is
2979 * 'always' While this is weird it is the behavior in 1.4.12.
2980 * A possible improvement would be to obey the value set for the
2981 * chunk, but this would be an API change that would probably
2982 * damage some applications.
2983 *
2984 * The png_app_warning below catches the case that matters, where
2985 * the application has not set specific save or ignore for this
2986 * chunk or global save or ignore.
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002987 */
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05002988 if (keep < PNG_HANDLE_CHUNK_IF_SAFE)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002989 {
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05002990# ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
2991 if (png_ptr->unknown_default < PNG_HANDLE_CHUNK_IF_SAFE)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002992 {
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05002993 png_chunk_warning(png_ptr, "Saving unknown chunk:");
2994 png_app_warning(png_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05002995 "forcing save of an unhandled chunk;"
2996 " please call png_set_keep_unknown_chunks");
2997 /* with keep = PNG_HANDLE_CHUNK_IF_SAFE */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002998 }
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05002999# endif
3000 keep = PNG_HANDLE_CHUNK_IF_SAFE;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003001 }
3002 }
3003
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05003004 else /* chunk was handled */
3005 {
3006 handled = 1;
3007 /* Critical chunks can be safely discarded at this point. */
3008 keep = PNG_HANDLE_CHUNK_NEVER;
3009 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003010 }
3011
3012 else
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05003013 keep = PNG_HANDLE_CHUNK_NEVER; /* insufficient memory */
3014 }
3015
3016 else
3017 /* Use the SAVE_UNKNOWN_CHUNKS code or skip the chunk */
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06003018# endif /* READ_USER_CHUNKS */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003019
3020# ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05003021 {
3022 /* keep is currently just the per-chunk setting, if there was no
3023 * setting change it to the global default now (not that this may
3024 * still be AS_DEFAULT) then obtain the cache of the chunk if required,
3025 * if not simply skip the chunk.
3026 */
3027 if (keep == PNG_HANDLE_CHUNK_AS_DEFAULT)
3028 keep = png_ptr->unknown_default;
3029
3030 if (keep == PNG_HANDLE_CHUNK_ALWAYS ||
3031 (keep == PNG_HANDLE_CHUNK_IF_SAFE &&
3032 PNG_CHUNK_ANCILLARY(png_ptr->chunk_name)))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003033 {
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05003034 if (png_cache_unknown_chunk(png_ptr, length) == 0)
3035 keep = PNG_HANDLE_CHUNK_NEVER;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003036 }
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05003037
3038 else
3039 png_crc_finish(png_ptr, length);
3040 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003041# else
3042# ifndef PNG_READ_USER_CHUNKS_SUPPORTED
3043# error no method to support READ_UNKNOWN_CHUNKS
3044# endif
3045
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05003046 {
3047 /* If here there is no read callback pointer set and no support is
3048 * compiled in to just save the unknown chunks, so simply skip this
3049 * chunk. If 'keep' is something other than AS_DEFAULT or NEVER then
3050 * the app has erroneously asked for unknown chunk saving when there
3051 * is no support.
3052 */
3053 if (keep > PNG_HANDLE_CHUNK_NEVER)
3054 png_app_error(png_ptr, "no unknown chunk support available");
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003055
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05003056 png_crc_finish(png_ptr, length);
3057 }
Glenn Randers-Pehrsonb3721752013-09-30 13:56:44 -05003058# endif
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003059
3060# ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05003061 /* Now store the chunk in the chunk list if appropriate, and if the limits
3062 * permit it.
3063 */
3064 if (keep == PNG_HANDLE_CHUNK_ALWAYS ||
3065 (keep == PNG_HANDLE_CHUNK_IF_SAFE &&
3066 PNG_CHUNK_ANCILLARY(png_ptr->chunk_name)))
3067 {
3068# ifdef PNG_USER_LIMITS_SUPPORTED
3069 switch (png_ptr->user_chunk_cache_max)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003070 {
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05003071 case 2:
3072 png_ptr->user_chunk_cache_max = 1;
3073 png_chunk_benign_error(png_ptr, "no space in chunk cache");
John Bowler72d07d32017-07-11 07:50:35 -05003074 /* FALLTHROUGH */
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05003075 case 1:
3076 /* NOTE: prior to 1.6.0 this case resulted in an unknown critical
3077 * chunk being skipped, now there will be a hard error below.
3078 */
3079 break;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003080
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05003081 default: /* not at limit */
3082 --(png_ptr->user_chunk_cache_max);
John Bowler72d07d32017-07-11 07:50:35 -05003083 /* FALLTHROUGH */
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05003084 case 0: /* no limit */
3085# endif /* USER_LIMITS */
3086 /* Here when the limit isn't reached or when limits are compiled
3087 * out; store the chunk.
3088 */
3089 png_set_unknown_chunks(png_ptr, info_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05003090 &png_ptr->unknown_chunk, 1);
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05003091 handled = 1;
3092# ifdef PNG_USER_LIMITS_SUPPORTED
3093 break;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003094 }
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05003095# endif
3096 }
Glenn Randers-Pehrsonb3721752013-09-30 13:56:44 -05003097# else /* no store support: the chunk must be handled by the user callback */
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05003098 PNG_UNUSED(info_ptr)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003099# endif
3100
3101 /* Regardless of the error handling below the cached data (if any) can be
3102 * freed now. Notice that the data is not freed if there is a png_error, but
3103 * it will be freed by destroy_read_struct.
3104 */
3105 if (png_ptr->unknown_chunk.data != NULL)
3106 png_free(png_ptr, png_ptr->unknown_chunk.data);
3107 png_ptr->unknown_chunk.data = NULL;
3108
3109#else /* !PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */
3110 /* There is no support to read an unknown chunk, so just skip it. */
3111 png_crc_finish(png_ptr, length);
3112 PNG_UNUSED(info_ptr)
3113 PNG_UNUSED(keep)
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06003114#endif /* !READ_UNKNOWN_CHUNKS */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003115
3116 /* Check for unhandled critical chunks */
Glenn Randers-Pehrsonebba0742014-10-25 12:22:39 -05003117 if (handled == 0 && PNG_CHUNK_CRITICAL(png_ptr->chunk_name))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003118 png_chunk_error(png_ptr, "unhandled critical chunk");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003119}
3120
3121/* This function is called to verify that a chunk name is valid.
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003122 * This function can't have the "critical chunk check" incorporated
3123 * into it, since in the future we will need to be able to call user
3124 * functions to handle unknown critical chunks after we check that
3125 * the chunk name itself is valid.
3126 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05003127
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003128/* Bit hacking: the test for an invalid byte in the 4 byte chunk name is:
3129 *
3130 * ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
3131 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05003132
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05003133void /* PRIVATE */
Cosmin Trutaceb32772018-08-18 22:47:16 -04003134png_check_chunk_name(png_const_structrp png_ptr, png_uint_32 chunk_name)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003135{
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003136 int i;
Glenn Randers-Pehrson13bc0b62017-08-05 15:33:24 -05003137 png_uint_32 cn=chunk_name;
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003138
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05003139 png_debug(1, "in png_check_chunk_name");
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003140
3141 for (i=1; i<=4; ++i)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003142 {
Glenn Randers-Pehrson13bc0b62017-08-05 15:33:24 -05003143 int c = cn & 0xff;
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003144
3145 if (c < 65 || c > 122 || (c > 90 && c < 97))
3146 png_chunk_error(png_ptr, "invalid chunk type");
3147
Glenn Randers-Pehrson13bc0b62017-08-05 15:33:24 -05003148 cn >>= 8;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003149 }
3150}
3151
Glenn Randers-Pehrson2dca1562017-08-04 14:09:27 -05003152void /* PRIVATE */
Cosmin Trutaceb32772018-08-18 22:47:16 -04003153png_check_chunk_length(png_const_structrp png_ptr, png_uint_32 length)
Glenn Randers-Pehrson2dca1562017-08-04 14:09:27 -05003154{
3155 png_alloc_size_t limit = PNG_UINT_31_MAX;
3156
Glenn Randers-Pehrson2dca1562017-08-04 14:09:27 -05003157# ifdef PNG_SET_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrsoneb2f42a2017-08-31 11:14:23 -05003158 if (png_ptr->user_chunk_malloc_max > 0 &&
3159 png_ptr->user_chunk_malloc_max < limit)
3160 limit = png_ptr->user_chunk_malloc_max;
Glenn Randers-Pehrson2dca1562017-08-04 14:09:27 -05003161# elif PNG_USER_CHUNK_MALLOC_MAX > 0
Glenn Randers-Pehrsoneb2f42a2017-08-31 11:14:23 -05003162 if (PNG_USER_CHUNK_MALLOC_MAX < limit)
3163 limit = PNG_USER_CHUNK_MALLOC_MAX;
Glenn Randers-Pehrson2dca1562017-08-04 14:09:27 -05003164# endif
Glenn Randers-Pehrsoneb2f42a2017-08-31 11:14:23 -05003165 if (png_ptr->chunk_name == png_IDAT)
Glenn Randers-Pehrson2dca1562017-08-04 14:09:27 -05003166 {
Glenn Randers-Pehrsoneb2f42a2017-08-31 11:14:23 -05003167 png_alloc_size_t idat_limit = PNG_UINT_31_MAX;
Glenn Randers-Pehrson2dca1562017-08-04 14:09:27 -05003168 size_t row_factor =
Cosmin Truta8a057662018-06-17 22:56:29 -04003169 (size_t)png_ptr->width
3170 * (size_t)png_ptr->channels
3171 * (png_ptr->bit_depth > 8? 2: 1)
3172 + 1
3173 + (png_ptr->interlaced? 6: 0);
Glenn Randers-Pehrson2dca1562017-08-04 14:09:27 -05003174 if (png_ptr->height > PNG_UINT_32_MAX/row_factor)
Cosmin Truta8a057662018-06-17 22:56:29 -04003175 idat_limit = PNG_UINT_31_MAX;
Glenn Randers-Pehrson2dca1562017-08-04 14:09:27 -05003176 else
Glenn Randers-Pehrsoneb2f42a2017-08-31 11:14:23 -05003177 idat_limit = png_ptr->height * row_factor;
3178 row_factor = row_factor > 32566? 32566 : row_factor;
3179 idat_limit += 6 + 5*(idat_limit/row_factor+1); /* zlib+deflate overhead */
3180 idat_limit=idat_limit < PNG_UINT_31_MAX? idat_limit : PNG_UINT_31_MAX;
3181 limit = limit < idat_limit? idat_limit : limit;
Glenn Randers-Pehrson2dca1562017-08-04 14:09:27 -05003182 }
3183
3184 if (length > limit)
3185 {
3186 png_debug2(0," length = %lu, limit = %lu",
3187 (unsigned long)length,(unsigned long)limit);
3188 png_chunk_error(png_ptr, "chunk data is too large");
3189 }
3190}
3191
John Bowler7875d532011-11-07 22:33:49 -06003192/* Combines the row recently read in with the existing pixels in the row. This
3193 * routine takes care of alpha and transparency if requested. This routine also
3194 * handles the two methods of progressive display of interlaced images,
3195 * depending on the 'display' value; if 'display' is true then the whole row
3196 * (dp) is filled from the start by replicating the available pixels. If
3197 * 'display' is false only those pixels present in the pass are filled in.
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003198 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05003199void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06003200png_combine_row(png_const_structrp png_ptr, png_bytep dp, int display)
Guy Schalnat0d580581995-07-20 02:43:20 -05003201{
John Bowler4e68aa72011-10-11 16:01:33 -05003202 unsigned int pixel_depth = png_ptr->transformed_pixel_depth;
3203 png_const_bytep sp = png_ptr->row_buf + 1;
John Bowlerdc294202014-12-21 17:11:33 -06003204 png_alloc_size_t row_width = png_ptr->width;
John Bowler4e68aa72011-10-11 16:01:33 -05003205 unsigned int pass = png_ptr->pass;
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003206 png_bytep end_ptr = 0;
3207 png_byte end_byte = 0;
3208 unsigned int end_mask;
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003209
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05003210 png_debug(1, "in png_combine_row");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003211
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003212 /* Added in 1.5.6: it should not be possible to enter this routine until at
3213 * least one row has been read from the PNG data and transformed.
3214 */
3215 if (pixel_depth == 0)
3216 png_error(png_ptr, "internal row logic error");
3217
3218 /* Added in 1.5.4: the pixel depth should match the information returned by
3219 * any call to png_read_update_info at this point. Do not continue if we got
John Bowler9994f252011-05-15 18:52:39 -05003220 * this wrong.
3221 */
3222 if (png_ptr->info_rowbytes != 0 && png_ptr->info_rowbytes !=
John Bowlerac8375d2011-10-06 22:27:16 -05003223 PNG_ROWBYTES(pixel_depth, row_width))
John Bowler9994f252011-05-15 18:52:39 -05003224 png_error(png_ptr, "internal row size calculation error");
3225
John Bowlerac8375d2011-10-06 22:27:16 -05003226 /* Don't expect this to ever happen: */
3227 if (row_width == 0)
3228 png_error(png_ptr, "internal row width error");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003229
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003230 /* Preserve the last byte in cases where only part of it will be overwritten,
3231 * the multiply below may overflow, we don't care because ANSI-C guarantees
3232 * we get the low bits.
3233 */
3234 end_mask = (pixel_depth * row_width) & 7;
3235 if (end_mask != 0)
3236 {
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003237 /* end_ptr == NULL is a flag to say do nothing */
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003238 end_ptr = dp + PNG_ROWBYTES(pixel_depth, row_width) - 1;
3239 end_byte = *end_ptr;
3240# ifdef PNG_READ_PACKSWAP_SUPPORTED
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05003241 if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
3242 /* little-endian byte */
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -05003243 end_mask = (unsigned int)(0xff << end_mask);
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003244
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05003245 else /* big-endian byte */
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003246# endif
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05003247 end_mask = 0xff >> end_mask;
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003248 /* end_mask is now the bits to *keep* from the destination row */
3249 }
3250
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003251 /* For non-interlaced images this reduces to a memcpy(). A memcpy()
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003252 * will also happen if interlacing isn't supported or if the application
3253 * does not call png_set_interlace_handling(). In the latter cases the
3254 * caller just gets a sequence of the unexpanded rows from each interlace
3255 * pass.
John Bowlerac8375d2011-10-06 22:27:16 -05003256 */
3257#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05003258 if (png_ptr->interlaced != 0 &&
3259 (png_ptr->transformations & PNG_INTERLACE) != 0 &&
3260 pass < 6 && (display == 0 ||
3261 /* The following copies everything for 'display' on passes 0, 2 and 4. */
3262 (display == 1 && (pass & 1) != 0)))
Guy Schalnat0d580581995-07-20 02:43:20 -05003263 {
John Bowler4e68aa72011-10-11 16:01:33 -05003264 /* Narrow images may have no bits in a pass; the caller should handle
3265 * this, but this test is cheap:
John Bowlerac8375d2011-10-06 22:27:16 -05003266 */
John Bowler4e68aa72011-10-11 16:01:33 -05003267 if (row_width <= PNG_PASS_START_COL(pass))
3268 return;
John Bowlerac8375d2011-10-06 22:27:16 -05003269
John Bowler4e68aa72011-10-11 16:01:33 -05003270 if (pixel_depth < 8)
Guy Schalnat0d580581995-07-20 02:43:20 -05003271 {
John Bowler7875d532011-11-07 22:33:49 -06003272 /* For pixel depths up to 4 bpp the 8-pixel mask can be expanded to fit
John Bowler4e68aa72011-10-11 16:01:33 -05003273 * into 32 bits, then a single loop over the bytes using the four byte
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003274 * values in the 32-bit mask can be used. For the 'display' option the
John Bowler4e68aa72011-10-11 16:01:33 -05003275 * expanded mask may also not require any masking within a byte. To
3276 * make this work the PACKSWAP option must be taken into account - it
3277 * simply requires the pixels to be reversed in each byte.
3278 *
3279 * The 'regular' case requires a mask for each of the first 6 passes,
3280 * the 'display' case does a copy for the even passes in the range
John Bowler7875d532011-11-07 22:33:49 -06003281 * 0..6. This has already been handled in the test above.
John Bowler4e68aa72011-10-11 16:01:33 -05003282 *
3283 * The masks are arranged as four bytes with the first byte to use in
3284 * the lowest bits (little-endian) regardless of the order (PACKSWAP or
3285 * not) of the pixels in each byte.
3286 *
3287 * NOTE: the whole of this logic depends on the caller of this function
3288 * only calling it on rows appropriate to the pass. This function only
John Bowler7875d532011-11-07 22:33:49 -06003289 * understands the 'x' logic; the 'y' logic is handled by the caller.
John Bowler4e68aa72011-10-11 16:01:33 -05003290 *
3291 * The following defines allow generation of compile time constant bit
3292 * masks for each pixel depth and each possibility of swapped or not
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003293 * swapped bytes. Pass 'p' is in the range 0..6; 'x', a pixel index,
3294 * is in the range 0..7; and the result is 1 if the pixel is to be
3295 * copied in the pass, 0 if not. 'S' is for the sparkle method, 'B'
3296 * for the block method.
3297 *
John Bowler92ef3132011-10-27 19:36:08 -05003298 * With some compilers a compile time expression of the general form:
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05003299 *
John Bowler92ef3132011-10-27 19:36:08 -05003300 * (shift >= 32) ? (a >> (shift-32)) : (b >> shift)
3301 *
3302 * Produces warnings with values of 'shift' in the range 33 to 63
Glenn Randers-Pehrson18763662011-10-27 22:09:22 -05003303 * because the right hand side of the ?: expression is evaluated by
John Bowler92ef3132011-10-27 19:36:08 -05003304 * the compiler even though it isn't used. Microsoft Visual C (various
3305 * versions) and the Intel C compiler are known to do this. To avoid
3306 * this the following macros are used in 1.5.6. This is a temporary
John Bowler7875d532011-11-07 22:33:49 -06003307 * solution to avoid destabilizing the code during the release process.
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05003308 */
John Bowler92ef3132011-10-27 19:36:08 -05003309# if PNG_USE_COMPILE_TIME_MASKS
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05003310# define PNG_LSR(x,s) ((x)>>((s) & 0x1f))
3311# define PNG_LSL(x,s) ((x)<<((s) & 0x1f))
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003312# else
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05003313# define PNG_LSR(x,s) ((x)>>(s))
3314# define PNG_LSL(x,s) ((x)<<(s))
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003315# endif
Glenn Randers-Pehrsona8242fe2015-08-17 20:46:27 -05003316# define S_COPY(p,x) (((p)<4 ? PNG_LSR(0x80088822,(3-(p))*8+(7-(x))) :\
3317 PNG_LSR(0xaa55ff00,(7-(p))*8+(7-(x)))) & 1)
3318# define B_COPY(p,x) (((p)<4 ? PNG_LSR(0xff0fff33,(3-(p))*8+(7-(x))) :\
3319 PNG_LSR(0xff55ff00,(7-(p))*8+(7-(x)))) & 1)
Guy Schalnat0d580581995-07-20 02:43:20 -05003320
John Bowler4e68aa72011-10-11 16:01:33 -05003321 /* Return a mask for pass 'p' pixel 'x' at depth 'd'. The mask is
3322 * little endian - the first pixel is at bit 0 - however the extra
3323 * parameter 's' can be set to cause the mask position to be swapped
3324 * within each byte, to match the PNG format. This is done by XOR of
3325 * the shift with 7, 6 or 4 for bit depths 1, 2 and 4.
3326 */
Glenn Randers-Pehrsoncb756992011-10-27 16:59:03 -05003327# define PIXEL_MASK(p,x,d,s) \
3328 (PNG_LSL(((PNG_LSL(1U,(d)))-1),(((x)*(d))^((s)?8-(d):0))))
John Bowlerac8375d2011-10-06 22:27:16 -05003329
John Bowler4e68aa72011-10-11 16:01:33 -05003330 /* Hence generate the appropriate 'block' or 'sparkle' pixel copy mask.
3331 */
3332# define S_MASKx(p,x,d,s) (S_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
3333# 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 -05003334
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003335 /* Combine 8 of these to get the full mask. For the 1-bpp and 2-bpp
3336 * cases the result needs replicating, for the 4-bpp case the above
3337 * generates a full 32 bits.
John Bowler4e68aa72011-10-11 16:01:33 -05003338 */
Glenn Randers-Pehrsona8242fe2015-08-17 20:46:27 -05003339# define MASK_EXPAND(m,d) ((m)*((d)==1?0x01010101:((d)==2?0x00010001:1)))
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003340
John Bowler4e68aa72011-10-11 16:01:33 -05003341# define S_MASK(p,d,s) MASK_EXPAND(S_MASKx(p,0,d,s) + S_MASKx(p,1,d,s) +\
3342 S_MASKx(p,2,d,s) + S_MASKx(p,3,d,s) + S_MASKx(p,4,d,s) +\
3343 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 -06003344
John Bowler4e68aa72011-10-11 16:01:33 -05003345# define B_MASK(p,d,s) MASK_EXPAND(B_MASKx(p,0,d,s) + B_MASKx(p,1,d,s) +\
3346 B_MASKx(p,2,d,s) + B_MASKx(p,3,d,s) + B_MASKx(p,4,d,s) +\
3347 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 -05003348
John Bowler4e68aa72011-10-11 16:01:33 -05003349#if PNG_USE_COMPILE_TIME_MASKS
3350 /* Utility macros to construct all the masks for a depth/swap
3351 * combination. The 's' parameter says whether the format is PNG
John Bowler7875d532011-11-07 22:33:49 -06003352 * (big endian bytes) or not. Only the three odd-numbered passes are
John Bowler4e68aa72011-10-11 16:01:33 -05003353 * required for the display/block algorithm.
3354 */
3355# define S_MASKS(d,s) { S_MASK(0,d,s), S_MASK(1,d,s), S_MASK(2,d,s),\
3356 S_MASK(3,d,s), S_MASK(4,d,s), S_MASK(5,d,s) }
John Bowlerac8375d2011-10-06 22:27:16 -05003357
Glenn Randers-Pehrsonfa26eb12014-04-06 09:06:37 -05003358# 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 -05003359
John Bowler4e68aa72011-10-11 16:01:33 -05003360# define DEPTH_INDEX(d) ((d)==1?0:((d)==2?1:2))
John Bowlerac8375d2011-10-06 22:27:16 -05003361
John Bowler4e68aa72011-10-11 16:01:33 -05003362 /* Hence the pre-compiled masks indexed by PACKSWAP (or not), depth and
3363 * then pass:
3364 */
Cosmin Truta1ef88822018-08-18 21:01:02 -04003365 static const png_uint_32 row_mask[2/*PACKSWAP*/][3/*depth*/][6] =
John Bowler7875d532011-11-07 22:33:49 -06003366 {
John Bowler4e68aa72011-10-11 16:01:33 -05003367 /* Little-endian byte masks for PACKSWAP */
3368 { S_MASKS(1,0), S_MASKS(2,0), S_MASKS(4,0) },
3369 /* Normal (big-endian byte) masks - PNG format */
3370 { S_MASKS(1,1), S_MASKS(2,1), S_MASKS(4,1) }
3371 };
John Bowlerac8375d2011-10-06 22:27:16 -05003372
John Bowler4e68aa72011-10-11 16:01:33 -05003373 /* display_mask has only three entries for the odd passes, so index by
3374 * pass>>1.
3375 */
Cosmin Truta1ef88822018-08-18 21:01:02 -04003376 static const png_uint_32 display_mask[2][3][3] =
John Bowler7875d532011-11-07 22:33:49 -06003377 {
John Bowler4e68aa72011-10-11 16:01:33 -05003378 /* Little-endian byte masks for PACKSWAP */
3379 { B_MASKS(1,0), B_MASKS(2,0), B_MASKS(4,0) },
3380 /* Normal (big-endian byte) masks - PNG format */
3381 { B_MASKS(1,1), B_MASKS(2,1), B_MASKS(4,1) }
3382 };
John Bowlerac8375d2011-10-06 22:27:16 -05003383
John Bowler4e68aa72011-10-11 16:01:33 -05003384# define MASK(pass,depth,display,png)\
3385 ((display)?display_mask[png][DEPTH_INDEX(depth)][pass>>1]:\
3386 row_mask[png][DEPTH_INDEX(depth)][pass])
John Bowlerac8375d2011-10-06 22:27:16 -05003387
John Bowler4e68aa72011-10-11 16:01:33 -05003388#else /* !PNG_USE_COMPILE_TIME_MASKS */
3389 /* This is the runtime alternative: it seems unlikely that this will
3390 * ever be either smaller or faster than the compile time approach.
3391 */
3392# define MASK(pass,depth,display,png)\
3393 ((display)?B_MASK(pass,depth,png):S_MASK(pass,depth,png))
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06003394#endif /* !USE_COMPILE_TIME_MASKS */
John Bowlerac8375d2011-10-06 22:27:16 -05003395
John Bowler4e68aa72011-10-11 16:01:33 -05003396 /* Use the appropriate mask to copy the required bits. In some cases
Glenn Randers-Pehrsonfa26eb12014-04-06 09:06:37 -05003397 * the byte mask will be 0 or 0xff; optimize these cases. row_width is
John Bowler4e68aa72011-10-11 16:01:33 -05003398 * the number of pixels, but the code copies bytes, so it is necessary
3399 * to special case the end.
3400 */
3401 png_uint_32 pixels_per_byte = 8 / pixel_depth;
3402 png_uint_32 mask;
John Bowlerac8375d2011-10-06 22:27:16 -05003403
John Bowler4e68aa72011-10-11 16:01:33 -05003404# ifdef PNG_READ_PACKSWAP_SUPPORTED
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05003405 if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
3406 mask = MASK(pass, pixel_depth, display, 0);
John Bowlerac8375d2011-10-06 22:27:16 -05003407
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05003408 else
John Bowler4e68aa72011-10-11 16:01:33 -05003409# endif
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05003410 mask = MASK(pass, pixel_depth, display, 1);
John Bowler4e68aa72011-10-11 16:01:33 -05003411
3412 for (;;)
3413 {
3414 png_uint_32 m;
3415
3416 /* It doesn't matter in the following if png_uint_32 has more than
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003417 * 32 bits because the high bits always match those in m<<24; it is,
John Bowler4e68aa72011-10-11 16:01:33 -05003418 * however, essential to use OR here, not +, because of this.
3419 */
3420 m = mask;
3421 mask = (m >> 8) | (m << 24); /* rotate right to good compilers */
3422 m &= 0xff;
3423
3424 if (m != 0) /* something to copy */
John Bowlerac8375d2011-10-06 22:27:16 -05003425 {
John Bowler4e68aa72011-10-11 16:01:33 -05003426 if (m != 0xff)
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05003427 *dp = (png_byte)((*dp & ~m) | (*sp & m));
John Bowler4e68aa72011-10-11 16:01:33 -05003428 else
3429 *dp = *sp;
John Bowlerac8375d2011-10-06 22:27:16 -05003430 }
John Bowler4e68aa72011-10-11 16:01:33 -05003431
3432 /* NOTE: this may overwrite the last byte with garbage if the image
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003433 * is not an exact number of bytes wide; libpng has always done
John Bowler4e68aa72011-10-11 16:01:33 -05003434 * this.
3435 */
3436 if (row_width <= pixels_per_byte)
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003437 break; /* May need to restore part of the last byte */
John Bowler4e68aa72011-10-11 16:01:33 -05003438
3439 row_width -= pixels_per_byte;
3440 ++dp;
3441 ++sp;
3442 }
3443 }
3444
3445 else /* pixel_depth >= 8 */
3446 {
3447 unsigned int bytes_to_copy, bytes_to_jump;
3448
3449 /* Validate the depth - it must be a multiple of 8 */
3450 if (pixel_depth & 7)
3451 png_error(png_ptr, "invalid user transform pixel depth");
3452
3453 pixel_depth >>= 3; /* now in bytes */
3454 row_width *= pixel_depth;
3455
3456 /* Regardless of pass number the Adam 7 interlace always results in a
3457 * fixed number of pixels to copy then to skip. There may be a
3458 * different number of pixels to skip at the start though.
3459 */
3460 {
3461 unsigned int offset = PNG_PASS_START_COL(pass) * pixel_depth;
3462
3463 row_width -= offset;
3464 dp += offset;
3465 sp += offset;
John Bowlerac8375d2011-10-06 22:27:16 -05003466 }
3467
John Bowler4e68aa72011-10-11 16:01:33 -05003468 /* Work out the bytes to copy. */
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06003469 if (display != 0)
John Bowler4e68aa72011-10-11 16:01:33 -05003470 {
3471 /* When doing the 'block' algorithm the pixel in the pass gets
3472 * replicated to adjacent pixels. This is why the even (0,2,4,6)
3473 * passes are skipped above - the entire expanded row is copied.
3474 */
3475 bytes_to_copy = (1<<((6-pass)>>1)) * pixel_depth;
3476
3477 /* But don't allow this number to exceed the actual row width. */
3478 if (bytes_to_copy > row_width)
John Bowlerdc294202014-12-21 17:11:33 -06003479 bytes_to_copy = (unsigned int)/*SAFE*/row_width;
John Bowler4e68aa72011-10-11 16:01:33 -05003480 }
3481
3482 else /* normal row; Adam7 only ever gives us one pixel to copy. */
3483 bytes_to_copy = pixel_depth;
3484
3485 /* In Adam7 there is a constant offset between where the pixels go. */
3486 bytes_to_jump = PNG_PASS_COL_OFFSET(pass) * pixel_depth;
3487
3488 /* And simply copy these bytes. Some optimization is possible here,
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003489 * depending on the value of 'bytes_to_copy'. Special case the low
John Bowler4e68aa72011-10-11 16:01:33 -05003490 * byte counts, which we know to be frequent.
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003491 *
3492 * Notice that these cases all 'return' rather than 'break' - this
3493 * avoids an unnecessary test on whether to restore the last byte
3494 * below.
John Bowler4e68aa72011-10-11 16:01:33 -05003495 */
3496 switch (bytes_to_copy)
3497 {
3498 case 1:
3499 for (;;)
3500 {
3501 *dp = *sp;
3502
3503 if (row_width <= bytes_to_jump)
3504 return;
3505
3506 dp += bytes_to_jump;
3507 sp += bytes_to_jump;
3508 row_width -= bytes_to_jump;
3509 }
3510
3511 case 2:
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003512 /* There is a possibility of a partial copy at the end here; this
John Bowler4e68aa72011-10-11 16:01:33 -05003513 * slows the code down somewhat.
3514 */
3515 do
3516 {
Viktor Szakats8c50acb2017-03-29 23:54:40 +00003517 dp[0] = sp[0]; dp[1] = sp[1];
John Bowler4e68aa72011-10-11 16:01:33 -05003518
3519 if (row_width <= bytes_to_jump)
3520 return;
3521
3522 sp += bytes_to_jump;
3523 dp += bytes_to_jump;
3524 row_width -= bytes_to_jump;
3525 }
3526 while (row_width > 1);
3527
3528 /* And there can only be one byte left at this point: */
3529 *dp = *sp;
3530 return;
3531
3532 case 3:
3533 /* This can only be the RGB case, so each copy is exactly one
3534 * pixel and it is not necessary to check for a partial copy.
3535 */
Glenn Randers-Pehrson48e6fad2014-09-27 09:37:50 -05003536 for (;;)
John Bowler4e68aa72011-10-11 16:01:33 -05003537 {
Viktor Szakats8c50acb2017-03-29 23:54:40 +00003538 dp[0] = sp[0]; dp[1] = sp[1]; dp[2] = sp[2];
John Bowler4e68aa72011-10-11 16:01:33 -05003539
3540 if (row_width <= bytes_to_jump)
3541 return;
3542
3543 sp += bytes_to_jump;
3544 dp += bytes_to_jump;
3545 row_width -= bytes_to_jump;
3546 }
3547
3548 default:
3549#if PNG_ALIGN_TYPE != PNG_ALIGN_NONE
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003550 /* Check for double byte alignment and, if possible, use a
3551 * 16-bit copy. Don't attempt this for narrow images - ones that
John Bowler4e68aa72011-10-11 16:01:33 -05003552 * are less than an interlace panel wide. Don't attempt it for
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003553 * wide bytes_to_copy either - use the memcpy there.
John Bowler4e68aa72011-10-11 16:01:33 -05003554 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003555 if (bytes_to_copy < 16 /*else use memcpy*/ &&
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05003556 png_isaligned(dp, png_uint_16) &&
3557 png_isaligned(sp, png_uint_16) &&
3558 bytes_to_copy % (sizeof (png_uint_16)) == 0 &&
3559 bytes_to_jump % (sizeof (png_uint_16)) == 0)
John Bowler4e68aa72011-10-11 16:01:33 -05003560 {
3561 /* Everything is aligned for png_uint_16 copies, but try for
3562 * png_uint_32 first.
3563 */
Glenn Randers-Pehrson639b4862016-07-15 17:22:10 -05003564 if (png_isaligned(dp, png_uint_32) &&
3565 png_isaligned(sp, png_uint_32) &&
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05003566 bytes_to_copy % (sizeof (png_uint_32)) == 0 &&
3567 bytes_to_jump % (sizeof (png_uint_32)) == 0)
John Bowler4e68aa72011-10-11 16:01:33 -05003568 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003569 png_uint_32p dp32 = png_aligncast(png_uint_32p,dp);
3570 png_const_uint_32p sp32 = png_aligncastconst(
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05003571 png_const_uint_32p, sp);
John Bowler2286a7c2013-03-10 21:35:35 -05003572 size_t skip = (bytes_to_jump-bytes_to_copy) /
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05003573 (sizeof (png_uint_32));
John Bowler4e68aa72011-10-11 16:01:33 -05003574
3575 do
3576 {
3577 size_t c = bytes_to_copy;
3578 do
3579 {
3580 *dp32++ = *sp32++;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003581 c -= (sizeof (png_uint_32));
John Bowler4e68aa72011-10-11 16:01:33 -05003582 }
3583 while (c > 0);
3584
3585 if (row_width <= bytes_to_jump)
3586 return;
3587
3588 dp32 += skip;
3589 sp32 += skip;
3590 row_width -= bytes_to_jump;
3591 }
3592 while (bytes_to_copy <= row_width);
3593
3594 /* Get to here when the row_width truncates the final copy.
3595 * There will be 1-3 bytes left to copy, so don't try the
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003596 * 16-bit loop below.
John Bowler4e68aa72011-10-11 16:01:33 -05003597 */
3598 dp = (png_bytep)dp32;
3599 sp = (png_const_bytep)sp32;
3600 do
3601 *dp++ = *sp++;
3602 while (--row_width > 0);
3603 return;
3604 }
3605
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003606 /* Else do it in 16-bit quantities, but only if the size is
John Bowler4e68aa72011-10-11 16:01:33 -05003607 * not too large.
3608 */
3609 else
3610 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003611 png_uint_16p dp16 = png_aligncast(png_uint_16p, dp);
3612 png_const_uint_16p sp16 = png_aligncastconst(
3613 png_const_uint_16p, sp);
John Bowlercaa3f292013-03-10 21:40:27 -05003614 size_t skip = (bytes_to_jump-bytes_to_copy) /
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003615 (sizeof (png_uint_16));
John Bowler4e68aa72011-10-11 16:01:33 -05003616
3617 do
3618 {
3619 size_t c = bytes_to_copy;
3620 do
3621 {
3622 *dp16++ = *sp16++;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003623 c -= (sizeof (png_uint_16));
John Bowler4e68aa72011-10-11 16:01:33 -05003624 }
3625 while (c > 0);
3626
3627 if (row_width <= bytes_to_jump)
3628 return;
3629
3630 dp16 += skip;
3631 sp16 += skip;
3632 row_width -= bytes_to_jump;
3633 }
3634 while (bytes_to_copy <= row_width);
3635
Glenn Randers-Pehrsonef02d562011-10-27 12:05:58 -05003636 /* End of row - 1 byte left, bytes_to_copy > row_width: */
John Bowler4e68aa72011-10-11 16:01:33 -05003637 dp = (png_bytep)dp16;
3638 sp = (png_const_bytep)sp16;
3639 do
3640 *dp++ = *sp++;
3641 while (--row_width > 0);
3642 return;
3643 }
3644 }
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06003645#endif /* ALIGN_TYPE code */
John Bowler4e68aa72011-10-11 16:01:33 -05003646
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003647 /* The true default - use a memcpy: */
John Bowler4e68aa72011-10-11 16:01:33 -05003648 for (;;)
3649 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003650 memcpy(dp, sp, bytes_to_copy);
John Bowler4e68aa72011-10-11 16:01:33 -05003651
3652 if (row_width <= bytes_to_jump)
3653 return;
3654
3655 sp += bytes_to_jump;
3656 dp += bytes_to_jump;
3657 row_width -= bytes_to_jump;
3658 if (bytes_to_copy > row_width)
John Bowlerdc294202014-12-21 17:11:33 -06003659 bytes_to_copy = (unsigned int)/*SAFE*/row_width;
John Bowler4e68aa72011-10-11 16:01:33 -05003660 }
3661 }
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003662
3663 /* NOT REACHED*/
John Bowler4e68aa72011-10-11 16:01:33 -05003664 } /* pixel_depth >= 8 */
3665
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003666 /* Here if pixel_depth < 8 to check 'end_ptr' below. */
Guy Schalnat0d580581995-07-20 02:43:20 -05003667 }
John Bowler4e68aa72011-10-11 16:01:33 -05003668 else
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06003669#endif /* READ_INTERLACING */
John Bowlerac8375d2011-10-06 22:27:16 -05003670
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003671 /* If here then the switch above wasn't used so just memcpy the whole row
John Bowler4e68aa72011-10-11 16:01:33 -05003672 * from the temporary row buffer (notice that this overwrites the end of the
3673 * destination row if it is a partial byte.)
John Bowlerac8375d2011-10-06 22:27:16 -05003674 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003675 memcpy(dp, sp, PNG_ROWBYTES(pixel_depth, row_width));
John Bowlerfb5b3ac2011-10-16 22:52:56 -05003676
3677 /* Restore the overwritten bits from the last byte if necessary. */
3678 if (end_ptr != NULL)
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05003679 *end_ptr = (png_byte)((end_byte & end_mask) | (*end_ptr & ~end_mask));
Guy Schalnat0d580581995-07-20 02:43:20 -05003680}
3681
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06003682#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05003683void /* PRIVATE */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003684png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05003685 png_uint_32 transformations /* Because these may affect the byte layout */)
Guy Schalnat0d580581995-07-20 02:43:20 -05003686{
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003687 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
3688 /* Offset to next interlace block */
Cosmin Truta1ef88822018-08-18 21:01:02 -04003689 static const unsigned int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003690
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05003691 png_debug(1, "in png_do_read_interlace");
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003692 if (row != NULL && row_info != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -05003693 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003694 png_uint_32 final_width;
3695
3696 final_width = row_info->width * png_pass_inc[pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05003697
3698 switch (row_info->pixel_depth)
3699 {
3700 case 1:
3701 {
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04003702 png_bytep sp = row + (size_t)((row_info->width - 1) >> 3);
3703 png_bytep dp = row + (size_t)((final_width - 1) >> 3);
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -05003704 unsigned int sshift, dshift;
3705 unsigned int s_start, s_end;
3706 int s_inc;
3707 int jstop = (int)png_pass_inc[pass];
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003708 png_byte v;
Guy Schalnat0d580581995-07-20 02:43:20 -05003709 png_uint_32 i;
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003710 int j;
Guy Schalnat0d580581995-07-20 02:43:20 -05003711
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003712#ifdef PNG_READ_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05003713 if ((transformations & PNG_PACKSWAP) != 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05003714 {
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -05003715 sshift = ((row_info->width + 7) & 0x07);
3716 dshift = ((final_width + 7) & 0x07);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003717 s_start = 7;
3718 s_end = 0;
3719 s_inc = -1;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003720 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003721
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003722 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05003723#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003724 {
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -05003725 sshift = 7 - ((row_info->width + 7) & 0x07);
3726 dshift = 7 - ((final_width + 7) & 0x07);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003727 s_start = 0;
3728 s_end = 7;
3729 s_inc = 1;
3730 }
Glenn Randers-Pehrson5dd2b8e2004-11-24 07:50:16 -06003731
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003732 for (i = 0; i < row_info->width; i++)
3733 {
3734 v = (png_byte)((*sp >> sshift) & 0x01);
3735 for (j = 0; j < jstop; j++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003736 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003737 unsigned int tmp = *dp & (0x7f7f >> (7 - dshift));
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -05003738 tmp |= (unsigned int)(v << dshift);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003739 *dp = (png_byte)(tmp & 0xff);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003740
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003741 if (dshift == s_end)
3742 {
3743 dshift = s_start;
3744 dp--;
3745 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003746
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003747 else
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -05003748 dshift = (unsigned int)((int)dshift + s_inc);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003749 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003750
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003751 if (sshift == s_end)
3752 {
3753 sshift = s_start;
Guy Schalnat0d580581995-07-20 02:43:20 -05003754 sp--;
3755 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003756
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003757 else
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -05003758 sshift = (unsigned int)((int)sshift + s_inc);
Guy Schalnat0d580581995-07-20 02:43:20 -05003759 }
3760 break;
3761 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003762
Guy Schalnat0d580581995-07-20 02:43:20 -05003763 case 2:
3764 {
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003765 png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);
3766 png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -05003767 unsigned int sshift, dshift;
3768 unsigned int s_start, s_end;
3769 int s_inc;
3770 int jstop = (int)png_pass_inc[pass];
Andreas Dilger47a0c421997-05-16 02:46:07 -05003771 png_uint_32 i;
Guy Schalnat0d580581995-07-20 02:43:20 -05003772
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003773#ifdef PNG_READ_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05003774 if ((transformations & PNG_PACKSWAP) != 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05003775 {
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -05003776 sshift = (((row_info->width + 3) & 0x03) << 1);
3777 dshift = (((final_width + 3) & 0x03) << 1);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003778 s_start = 6;
3779 s_end = 0;
3780 s_inc = -2;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003781 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003782
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003783 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05003784#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003785 {
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -05003786 sshift = ((3 - ((row_info->width + 3) & 0x03)) << 1);
3787 dshift = ((3 - ((final_width + 3) & 0x03)) << 1);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003788 s_start = 0;
3789 s_end = 6;
3790 s_inc = 2;
3791 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05003792
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003793 for (i = 0; i < row_info->width; i++)
3794 {
3795 png_byte v;
3796 int j;
3797
3798 v = (png_byte)((*sp >> sshift) & 0x03);
3799 for (j = 0; j < jstop; j++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003800 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003801 unsigned int tmp = *dp & (0x3f3f >> (6 - dshift));
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -05003802 tmp |= (unsigned int)(v << dshift);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003803 *dp = (png_byte)(tmp & 0xff);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003804
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003805 if (dshift == s_end)
3806 {
3807 dshift = s_start;
3808 dp--;
3809 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003810
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003811 else
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -05003812 dshift = (unsigned int)((int)dshift + s_inc);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003813 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003814
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003815 if (sshift == s_end)
3816 {
3817 sshift = s_start;
Guy Schalnat0d580581995-07-20 02:43:20 -05003818 sp--;
3819 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003820
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003821 else
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -05003822 sshift = (unsigned int)((int)sshift + s_inc);
Guy Schalnat0d580581995-07-20 02:43:20 -05003823 }
3824 break;
3825 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003826
Guy Schalnat0d580581995-07-20 02:43:20 -05003827 case 4:
3828 {
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04003829 png_bytep sp = row + (size_t)((row_info->width - 1) >> 1);
3830 png_bytep dp = row + (size_t)((final_width - 1) >> 1);
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -05003831 unsigned int sshift, dshift;
3832 unsigned int s_start, s_end;
3833 int s_inc;
Guy Schalnat0d580581995-07-20 02:43:20 -05003834 png_uint_32 i;
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -05003835 int jstop = (int)png_pass_inc[pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05003836
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003837#ifdef PNG_READ_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05003838 if ((transformations & PNG_PACKSWAP) != 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05003839 {
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -05003840 sshift = (((row_info->width + 1) & 0x01) << 2);
3841 dshift = (((final_width + 1) & 0x01) << 2);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003842 s_start = 4;
3843 s_end = 0;
3844 s_inc = -4;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003845 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003846
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003847 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05003848#endif
3849 {
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -05003850 sshift = ((1 - ((row_info->width + 1) & 0x01)) << 2);
3851 dshift = ((1 - ((final_width + 1) & 0x01)) << 2);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003852 s_start = 0;
3853 s_end = 4;
3854 s_inc = 4;
3855 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05003856
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003857 for (i = 0; i < row_info->width; i++)
3858 {
Glenn Randers-Pehrson8db19982011-10-27 16:17:24 -05003859 png_byte v = (png_byte)((*sp >> sshift) & 0x0f);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003860 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003861
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003862 for (j = 0; j < jstop; j++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003863 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003864 unsigned int tmp = *dp & (0xf0f >> (4 - dshift));
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -05003865 tmp |= (unsigned int)(v << dshift);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003866 *dp = (png_byte)(tmp & 0xff);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003867
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003868 if (dshift == s_end)
3869 {
3870 dshift = s_start;
3871 dp--;
3872 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003873
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003874 else
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -05003875 dshift = (unsigned int)((int)dshift + s_inc);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003876 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003877
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003878 if (sshift == s_end)
3879 {
3880 sshift = s_start;
Guy Schalnat0d580581995-07-20 02:43:20 -05003881 sp--;
3882 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003883
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003884 else
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -05003885 sshift = (unsigned int)((int)sshift + s_inc);
Guy Schalnat0d580581995-07-20 02:43:20 -05003886 }
3887 break;
3888 }
John Bowlerac8375d2011-10-06 22:27:16 -05003889
Guy Schalnat0d580581995-07-20 02:43:20 -05003890 default:
3891 {
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04003892 size_t pixel_bytes = (row_info->pixel_depth >> 3);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003893
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04003894 png_bytep sp = row + (size_t)(row_info->width - 1)
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06003895 * pixel_bytes;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003896
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04003897 png_bytep dp = row + (size_t)(final_width - 1) * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05003898
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -05003899 int jstop = (int)png_pass_inc[pass];
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003900 png_uint_32 i;
3901
3902 for (i = 0; i < row_info->width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003903 {
Glenn Randers-Pehrsondb56fa12013-07-15 10:09:54 -05003904 png_byte v[8]; /* SAFE; pixel_depth does not exceed 64 */
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003905 int j;
3906
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003907 memcpy(v, sp, pixel_bytes);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003908
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003909 for (j = 0; j < jstop; j++)
3910 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003911 memcpy(dp, v, pixel_bytes);
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003912 dp -= pixel_bytes;
3913 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003914
Glenn Randers-Pehrsonc3dda6d2004-11-27 18:22:29 -06003915 sp -= pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05003916 }
3917 break;
3918 }
3919 }
John Bowlerac8375d2011-10-06 22:27:16 -05003920
Guy Schalnat0d580581995-07-20 02:43:20 -05003921 row_info->width = final_width;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003922 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width);
Guy Schalnat0d580581995-07-20 02:43:20 -05003923 }
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -05003924#ifndef PNG_READ_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003925 PNG_UNUSED(transformations) /* Silence compiler warning */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05003926#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003927}
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06003928#endif /* READ_INTERLACING */
Guy Schalnat0d580581995-07-20 02:43:20 -05003929
Mans Rullgardd3a94802011-11-03 00:47:55 -05003930static void
3931png_read_filter_row_sub(png_row_infop row_info, png_bytep row,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05003932 png_const_bytep prev_row)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003933{
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04003934 size_t i;
3935 size_t istop = row_info->rowbytes;
Mans Rullgardd3a94802011-11-03 00:47:55 -05003936 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
3937 png_bytep rp = row + bpp;
Mans Rullgardd3a94802011-11-03 00:47:55 -05003938
3939 PNG_UNUSED(prev_row)
3940
3941 for (i = bpp; i < istop; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003942 {
John Bowler7875d532011-11-07 22:33:49 -06003943 *rp = (png_byte)(((int)(*rp) + (int)(*(rp-bpp))) & 0xff);
Mans Rullgardd3a94802011-11-03 00:47:55 -05003944 rp++;
3945 }
3946}
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003947
Mans Rullgardd3a94802011-11-03 00:47:55 -05003948static void
3949png_read_filter_row_up(png_row_infop row_info, png_bytep row,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05003950 png_const_bytep prev_row)
Mans Rullgardd3a94802011-11-03 00:47:55 -05003951{
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04003952 size_t i;
3953 size_t istop = row_info->rowbytes;
Mans Rullgardd3a94802011-11-03 00:47:55 -05003954 png_bytep rp = row;
3955 png_const_bytep pp = prev_row;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003956
Mans Rullgardd3a94802011-11-03 00:47:55 -05003957 for (i = 0; i < istop; i++)
3958 {
3959 *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
3960 rp++;
3961 }
3962}
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003963
Mans Rullgardd3a94802011-11-03 00:47:55 -05003964static void
3965png_read_filter_row_avg(png_row_infop row_info, png_bytep row,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05003966 png_const_bytep prev_row)
Mans Rullgardd3a94802011-11-03 00:47:55 -05003967{
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04003968 size_t i;
Mans Rullgardd3a94802011-11-03 00:47:55 -05003969 png_bytep rp = row;
3970 png_const_bytep pp = prev_row;
Mans Rullgardd3a94802011-11-03 00:47:55 -05003971 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04003972 size_t istop = row_info->rowbytes - bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003973
Mans Rullgardd3a94802011-11-03 00:47:55 -05003974 for (i = 0; i < bpp; i++)
3975 {
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05003976 *rp = (png_byte)(((int)(*rp) +
3977 ((int)(*pp++) / 2 )) & 0xff);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003978
Mans Rullgardd3a94802011-11-03 00:47:55 -05003979 rp++;
3980 }
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003981
Mans Rullgardd3a94802011-11-03 00:47:55 -05003982 for (i = 0; i < istop; i++)
3983 {
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05003984 *rp = (png_byte)(((int)(*rp) +
3985 (int)(*pp++ + *(rp-bpp)) / 2 ) & 0xff);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06003986
Mans Rullgardd3a94802011-11-03 00:47:55 -05003987 rp++;
3988 }
3989}
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003990
Mans Rullgardd3a94802011-11-03 00:47:55 -05003991static void
John Bowlerfcc02632011-11-03 18:31:00 -05003992png_read_filter_row_paeth_1byte_pixel(png_row_infop row_info, png_bytep row,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05003993 png_const_bytep prev_row)
Mans Rullgardd3a94802011-11-03 00:47:55 -05003994{
John Bowlerfcc02632011-11-03 18:31:00 -05003995 png_bytep rp_end = row + row_info->rowbytes;
3996 int a, c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003997
John Bowlerfcc02632011-11-03 18:31:00 -05003998 /* First pixel/byte */
3999 c = *prev_row++;
4000 a = *row + c;
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05004001 *row++ = (png_byte)a;
John Bowlerfcc02632011-11-03 18:31:00 -05004002
4003 /* Remainder */
4004 while (row < rp_end)
Mans Rullgardd3a94802011-11-03 00:47:55 -05004005 {
John Bowlerfcc02632011-11-03 18:31:00 -05004006 int b, pa, pb, pc, p;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004007
John Bowlerfcc02632011-11-03 18:31:00 -05004008 a &= 0xff; /* From previous iteration or start */
4009 b = *prev_row++;
Mans Rullgardd3a94802011-11-03 00:47:55 -05004010
4011 p = b - c;
4012 pc = a - c;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05004013
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05004014#ifdef PNG_USE_ABS
4015 pa = abs(p);
4016 pb = abs(pc);
4017 pc = abs(p + pc);
4018#else
4019 pa = p < 0 ? -p : p;
4020 pb = pc < 0 ? -pc : pc;
4021 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
4022#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004023
John Bowlerfcc02632011-11-03 18:31:00 -05004024 /* Find the best predictor, the least of pa, pb, pc favoring the earlier
4025 * ones in the case of a tie.
4026 */
Viktor Szakats8c50acb2017-03-29 23:54:40 +00004027 if (pb < pa)
4028 {
4029 pa = pb; a = b;
4030 }
John Bowlerfcc02632011-11-03 18:31:00 -05004031 if (pc < pa) a = c;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004032
John Bowlerfcc02632011-11-03 18:31:00 -05004033 /* Calculate the current pixel in a, and move the previous row pixel to c
4034 * for the next time round the loop
4035 */
4036 c = b;
4037 a += *row;
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05004038 *row++ = (png_byte)a;
John Bowlerfcc02632011-11-03 18:31:00 -05004039 }
4040}
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004041
John Bowlerfcc02632011-11-03 18:31:00 -05004042static void
4043png_read_filter_row_paeth_multibyte_pixel(png_row_infop row_info, png_bytep row,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05004044 png_const_bytep prev_row)
John Bowlerfcc02632011-11-03 18:31:00 -05004045{
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -05004046 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
John Bowlerfcc02632011-11-03 18:31:00 -05004047 png_bytep rp_end = row + bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004048
John Bowlerfcc02632011-11-03 18:31:00 -05004049 /* Process the first pixel in the row completely (this is the same as 'up'
4050 * because there is only one candidate predictor for the first row).
4051 */
4052 while (row < rp_end)
4053 {
4054 int a = *row + *prev_row++;
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05004055 *row++ = (png_byte)a;
John Bowlerfcc02632011-11-03 18:31:00 -05004056 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004057
John Bowlerfcc02632011-11-03 18:31:00 -05004058 /* Remainder */
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -05004059 rp_end = rp_end + (row_info->rowbytes - bpp);
John Bowlerfcc02632011-11-03 18:31:00 -05004060
4061 while (row < rp_end)
4062 {
4063 int a, b, c, pa, pb, pc, p;
4064
4065 c = *(prev_row - bpp);
4066 a = *(row - bpp);
4067 b = *prev_row++;
4068
4069 p = b - c;
4070 pc = a - c;
4071
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05004072#ifdef PNG_USE_ABS
4073 pa = abs(p);
4074 pb = abs(pc);
4075 pc = abs(p + pc);
4076#else
4077 pa = p < 0 ? -p : p;
4078 pb = pc < 0 ? -pc : pc;
4079 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
4080#endif
John Bowlerfcc02632011-11-03 18:31:00 -05004081
Viktor Szakats8c50acb2017-03-29 23:54:40 +00004082 if (pb < pa)
4083 {
4084 pa = pb; a = b;
4085 }
John Bowlerfcc02632011-11-03 18:31:00 -05004086 if (pc < pa) a = c;
4087
John Bowlerfcc02632011-11-03 18:31:00 -05004088 a += *row;
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05004089 *row++ = (png_byte)a;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004090 }
4091}
Guy Schalnat0d580581995-07-20 02:43:20 -05004092
Mans Rullgardd3a94802011-11-03 00:47:55 -05004093static void
John Bowler5d567862011-12-24 09:12:00 -06004094png_init_filter_functions(png_structrp pp)
Glenn Randers-Pehrson32440202013-08-21 18:01:32 -05004095 /* This function is called once for every PNG image (except for PNG images
4096 * that only use PNG_FILTER_VALUE_NONE for all rows) to set the
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004097 * implementations required to reverse the filtering of PNG rows. Reversing
4098 * the filter is the first transformation performed on the row data. It is
4099 * performed in place, therefore an implementation can be selected based on
4100 * the image pixel format. If the implementation depends on image width then
Glenn Randers-Pehrson6e1c74b2013-04-22 11:06:26 -05004101 * take care to ensure that it works correctly if the image is interlaced -
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004102 * interlacing causes the actual row width to vary.
4103 */
Mans Rullgardd3a94802011-11-03 00:47:55 -05004104{
John Bowlerfcc02632011-11-03 18:31:00 -05004105 unsigned int bpp = (pp->pixel_depth + 7) >> 3;
4106
Mans Rullgardd3a94802011-11-03 00:47:55 -05004107 pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub;
4108 pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up;
4109 pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg;
John Bowlerfcc02632011-11-03 18:31:00 -05004110 if (bpp == 1)
4111 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
4112 png_read_filter_row_paeth_1byte_pixel;
4113 else
4114 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
4115 png_read_filter_row_paeth_multibyte_pixel;
Mans Rullgardd3a94802011-11-03 00:47:55 -05004116
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004117#ifdef PNG_FILTER_OPTIMIZATIONS
4118 /* To use this define PNG_FILTER_OPTIMIZATIONS as the name of a function to
4119 * call to install hardware optimizations for the above functions; simply
4120 * replace whatever elements of the pp->read_filter[] array with a hardware
4121 * specific (or, for that matter, generic) optimization.
4122 *
4123 * To see an example of this examine what configure.ac does when
4124 * --enable-arm-neon is specified on the command line.
4125 */
4126 PNG_FILTER_OPTIMIZATIONS(pp, bpp);
Mans Rullgardd3a94802011-11-03 00:47:55 -05004127#endif
4128}
4129
4130void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06004131png_read_filter_row(png_structrp pp, png_row_infop row_info, png_bytep row,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05004132 png_const_bytep prev_row, int filter)
Mans Rullgardd3a94802011-11-03 00:47:55 -05004133{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004134 /* OPTIMIZATION: DO NOT MODIFY THIS FUNCTION, instead #define
4135 * PNG_FILTER_OPTIMIZATIONS to a function that overrides the generic
4136 * implementations. See png_init_filter_functions above.
4137 */
Mans Rullgardd3a94802011-11-03 00:47:55 -05004138 if (filter > PNG_FILTER_VALUE_NONE && filter < PNG_FILTER_VALUE_LAST)
Glenn Randers-Pehrson685d79e2013-08-20 21:15:31 -05004139 {
4140 if (pp->read_filter[0] == NULL)
4141 png_init_filter_functions(pp);
4142
Mans Rullgardd3a94802011-11-03 00:47:55 -05004143 pp->read_filter[filter-1](row_info, row, prev_row);
Glenn Randers-Pehrson685d79e2013-08-20 21:15:31 -05004144 }
Mans Rullgardd3a94802011-11-03 00:47:55 -05004145}
4146
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05004147#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05004148void /* PRIVATE */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004149png_read_IDAT_data(png_structrp png_ptr, png_bytep output,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05004150 png_alloc_size_t avail_out)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004151{
4152 /* Loop reading IDATs and decompressing the result into output[avail_out] */
4153 png_ptr->zstream.next_out = output;
4154 png_ptr->zstream.avail_out = 0; /* safety: set below */
4155
4156 if (output == NULL)
4157 avail_out = 0;
4158
4159 do
4160 {
4161 int ret;
4162 png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];
4163
4164 if (png_ptr->zstream.avail_in == 0)
4165 {
4166 uInt avail_in;
4167 png_bytep buffer;
4168
4169 while (png_ptr->idat_size == 0)
4170 {
4171 png_crc_finish(png_ptr, 0);
4172
4173 png_ptr->idat_size = png_read_chunk_header(png_ptr);
4174 /* This is an error even in the 'check' case because the code just
4175 * consumed a non-IDAT header.
4176 */
4177 if (png_ptr->chunk_name != png_IDAT)
4178 png_error(png_ptr, "Not enough image data");
4179 }
4180
4181 avail_in = png_ptr->IDAT_read_size;
4182
4183 if (avail_in > png_ptr->idat_size)
4184 avail_in = (uInt)png_ptr->idat_size;
4185
4186 /* A PNG with a gradually increasing IDAT size will defeat this attempt
4187 * to minimize memory usage by causing lots of re-allocs, but
4188 * realistically doing IDAT_read_size re-allocs is not likely to be a
4189 * big problem.
4190 */
4191 buffer = png_read_buffer(png_ptr, avail_in, 0/*error*/);
4192
4193 png_crc_read(png_ptr, buffer, avail_in);
4194 png_ptr->idat_size -= avail_in;
4195
4196 png_ptr->zstream.next_in = buffer;
4197 png_ptr->zstream.avail_in = avail_in;
4198 }
4199
4200 /* And set up the output side. */
4201 if (output != NULL) /* standard read */
4202 {
4203 uInt out = ZLIB_IO_MAX;
4204
4205 if (out > avail_out)
4206 out = (uInt)avail_out;
4207
4208 avail_out -= out;
4209 png_ptr->zstream.avail_out = out;
4210 }
4211
John Bowler43c07e12013-04-07 21:33:30 -05004212 else /* after last row, checking for end */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004213 {
4214 png_ptr->zstream.next_out = tmpbuf;
4215 png_ptr->zstream.avail_out = (sizeof tmpbuf);
4216 }
4217
4218 /* Use NO_FLUSH; this gives zlib the maximum opportunity to optimize the
4219 * process. If the LZ stream is truncated the sequential reader will
4220 * terminally damage the stream, above, by reading the chunk header of the
4221 * following chunk (it then exits with png_error).
4222 *
4223 * TODO: deal more elegantly with truncated IDAT lists.
4224 */
John Bowler28a1cdf2015-11-27 23:57:39 -08004225 ret = PNG_INFLATE(png_ptr, Z_NO_FLUSH);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004226
John Bowler43c07e12013-04-07 21:33:30 -05004227 /* Take the unconsumed output back. */
4228 if (output != NULL)
4229 avail_out += png_ptr->zstream.avail_out;
4230
4231 else /* avail_out counts the extra bytes */
4232 avail_out += (sizeof tmpbuf) - png_ptr->zstream.avail_out;
4233
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004234 png_ptr->zstream.avail_out = 0;
4235
4236 if (ret == Z_STREAM_END)
4237 {
4238 /* Do this for safety; we won't read any more into this row. */
4239 png_ptr->zstream.next_out = NULL;
4240
4241 png_ptr->mode |= PNG_AFTER_IDAT;
4242 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
4243
4244 if (png_ptr->zstream.avail_in > 0 || png_ptr->idat_size > 0)
4245 png_chunk_benign_error(png_ptr, "Extra compressed data");
4246 break;
4247 }
4248
4249 if (ret != Z_OK)
4250 {
4251 png_zstream_error(png_ptr, ret);
4252
4253 if (output != NULL)
Glenn Randers-Pehrson8187ba12016-12-26 18:15:02 -06004254 png_chunk_error(png_ptr, png_ptr->zstream.msg);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004255
4256 else /* checking */
4257 {
4258 png_chunk_benign_error(png_ptr, png_ptr->zstream.msg);
4259 return;
4260 }
4261 }
4262 } while (avail_out > 0);
4263
4264 if (avail_out > 0)
4265 {
4266 /* The stream ended before the image; this is the same as too few IDATs so
4267 * should be handled the same way.
4268 */
4269 if (output != NULL)
4270 png_error(png_ptr, "Not enough image data");
4271
John Bowler43c07e12013-04-07 21:33:30 -05004272 else /* the deflate stream contained extra data */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004273 png_chunk_benign_error(png_ptr, "Too much image data");
4274 }
4275}
4276
4277void /* PRIVATE */
4278png_read_finish_IDAT(png_structrp png_ptr)
4279{
4280 /* We don't need any more data and the stream should have ended, however the
4281 * LZ end code may actually not have been processed. In this case we must
4282 * read it otherwise stray unread IDAT data or, more likely, an IDAT chunk
4283 * may still remain to be consumed.
4284 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004285 if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004286 {
4287 /* The NULL causes png_read_IDAT_data to swallow any remaining bytes in
4288 * the compressed stream, but the stream may be damaged too, so even after
4289 * this call we may need to terminate the zstream ownership.
4290 */
4291 png_read_IDAT_data(png_ptr, NULL, 0);
4292 png_ptr->zstream.next_out = NULL; /* safety */
4293
4294 /* Now clear everything out for safety; the following may not have been
4295 * done.
4296 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004297 if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004298 {
4299 png_ptr->mode |= PNG_AFTER_IDAT;
4300 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
4301 }
4302 }
4303
4304 /* If the zstream has not been released do it now *and* terminate the reading
4305 * of the final IDAT chunk.
4306 */
4307 if (png_ptr->zowner == png_IDAT)
4308 {
4309 /* Always do this; the pointers otherwise point into the read buffer. */
4310 png_ptr->zstream.next_in = NULL;
4311 png_ptr->zstream.avail_in = 0;
4312
4313 /* Now we no longer own the zstream. */
4314 png_ptr->zowner = 0;
4315
4316 /* The slightly weird semantics of the sequential IDAT reading is that we
4317 * are always in or at the end of an IDAT chunk, so we always need to do a
4318 * crc_finish here. If idat_size is non-zero we also need to read the
4319 * spurious bytes at the end of the chunk now.
4320 */
4321 (void)png_crc_finish(png_ptr, png_ptr->idat_size);
4322 }
4323}
4324
4325void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06004326png_read_finish_row(png_structrp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05004327{
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004328 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004329
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004330 /* Start of interlace block */
Cosmin Truta1ef88822018-08-18 21:01:02 -04004331 static const png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004332
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004333 /* Offset to next interlace block */
Cosmin Truta1ef88822018-08-18 21:01:02 -04004334 static const png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004335
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004336 /* Start of interlace block in the y direction */
Cosmin Truta1ef88822018-08-18 21:01:02 -04004337 static const png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004338
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004339 /* Offset to next interlace block in the y direction */
Cosmin Truta1ef88822018-08-18 21:01:02 -04004340 static const png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004341
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05004342 png_debug(1, "in png_read_finish_row");
Guy Schalnat0d580581995-07-20 02:43:20 -05004343 png_ptr->row_number++;
4344 if (png_ptr->row_number < png_ptr->num_rows)
4345 return;
4346
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004347 if (png_ptr->interlaced != 0)
Guy Schalnat0d580581995-07-20 02:43:20 -05004348 {
4349 png_ptr->row_number = 0;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004350
Glenn Randers-Pehrson435cf872011-10-05 16:23:53 -05004351 /* TO DO: don't do this if prev_row isn't needed (requires
4352 * read-ahead of the next row's filter byte.
4353 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004354 memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004355
Guy Schalnat0d580581995-07-20 02:43:20 -05004356 do
4357 {
4358 png_ptr->pass++;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004359
Guy Schalnat0d580581995-07-20 02:43:20 -05004360 if (png_ptr->pass >= 7)
4361 break;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004362
Guy Schalnat0d580581995-07-20 02:43:20 -05004363 png_ptr->iwidth = (png_ptr->width +
4364 png_pass_inc[png_ptr->pass] - 1 -
4365 png_pass_start[png_ptr->pass]) /
4366 png_pass_inc[png_ptr->pass];
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05004367
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004368 if ((png_ptr->transformations & PNG_INTERLACE) == 0)
Guy Schalnat0d580581995-07-20 02:43:20 -05004369 {
4370 png_ptr->num_rows = (png_ptr->height +
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004371 png_pass_yinc[png_ptr->pass] - 1 -
4372 png_pass_ystart[png_ptr->pass]) /
4373 png_pass_yinc[png_ptr->pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05004374 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004375
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -05004376 else /* if (png_ptr->transformations & PNG_INTERLACE) */
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004377 break; /* libpng deinterlacing sees every row */
4378
4379 } while (png_ptr->num_rows == 0 || png_ptr->iwidth == 0);
Guy Schalnat0d580581995-07-20 02:43:20 -05004380
4381 if (png_ptr->pass < 7)
4382 return;
4383 }
4384
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004385 /* Here after at the end of the last row of the last pass. */
4386 png_read_finish_IDAT(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05004387}
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06004388#endif /* SEQUENTIAL_READ */
Guy Schalnat0d580581995-07-20 02:43:20 -05004389
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05004390void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06004391png_read_start_row(png_structrp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05004392{
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004393 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004394
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004395 /* Start of interlace block */
Cosmin Truta1ef88822018-08-18 21:01:02 -04004396 static const png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004397
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004398 /* Offset to next interlace block */
Cosmin Truta1ef88822018-08-18 21:01:02 -04004399 static const png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004400
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004401 /* Start of interlace block in the y direction */
Cosmin Truta1ef88822018-08-18 21:01:02 -04004402 static const png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004403
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004404 /* Offset to next interlace block in the y direction */
Cosmin Truta1ef88822018-08-18 21:01:02 -04004405 static const png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06004406
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -05004407 unsigned int max_pixel_depth;
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04004408 size_t row_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05004409
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05004410 png_debug(1, "in png_read_start_row");
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004411
John Bowler4a12f4a2011-04-17 18:34:22 -05004412#ifdef PNG_READ_TRANSFORMS_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05004413 png_init_read_transformations(png_ptr);
John Bowler4a12f4a2011-04-17 18:34:22 -05004414#endif
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004415 if (png_ptr->interlaced != 0)
Guy Schalnat0d580581995-07-20 02:43:20 -05004416 {
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004417 if ((png_ptr->transformations & PNG_INTERLACE) == 0)
Guy Schalnat0d580581995-07-20 02:43:20 -05004418 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004419 png_pass_ystart[0]) / png_pass_yinc[0];
4420
Guy Schalnat0d580581995-07-20 02:43:20 -05004421 else
4422 png_ptr->num_rows = png_ptr->height;
4423
4424 png_ptr->iwidth = (png_ptr->width +
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004425 png_pass_inc[png_ptr->pass] - 1 -
4426 png_pass_start[png_ptr->pass]) /
4427 png_pass_inc[png_ptr->pass];
Guy Schalnat0d580581995-07-20 02:43:20 -05004428 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004429
Guy Schalnat0d580581995-07-20 02:43:20 -05004430 else
4431 {
4432 png_ptr->num_rows = png_ptr->height;
4433 png_ptr->iwidth = png_ptr->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05004434 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004435
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -05004436 max_pixel_depth = (unsigned int)png_ptr->pixel_depth;
Guy Schalnat0d580581995-07-20 02:43:20 -05004437
Glenn Randers-Pehrsonf43b5e32014-12-20 19:10:52 -06004438 /* WARNING: * png_read_transform_info (pngrtran.c) performs a simpler set of
John Bowlere6fb6912011-11-08 21:35:16 -06004439 * calculations to calculate the final pixel depth, then
4440 * png_do_read_transforms actually does the transforms. This means that the
4441 * code which effectively calculates this value is actually repeated in three
4442 * separate places. They must all match. Innocent changes to the order of
4443 * transformations can and will break libpng in a way that causes memory
4444 * overwrites.
4445 *
4446 * TODO: fix this.
4447 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004448#ifdef PNG_READ_PACK_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004449 if ((png_ptr->transformations & PNG_PACK) != 0 && png_ptr->bit_depth < 8)
Guy Schalnat0d580581995-07-20 02:43:20 -05004450 max_pixel_depth = 8;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004451#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05004452
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004453#ifdef PNG_READ_EXPAND_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004454 if ((png_ptr->transformations & PNG_EXPAND) != 0)
Guy Schalnat0d580581995-07-20 02:43:20 -05004455 {
4456 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
4457 {
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004458 if (png_ptr->num_trans != 0)
Guy Schalnat0d580581995-07-20 02:43:20 -05004459 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004460
Guy Schalnat0d580581995-07-20 02:43:20 -05004461 else
4462 max_pixel_depth = 24;
4463 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004464
Guy Schalnat0d580581995-07-20 02:43:20 -05004465 else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
4466 {
4467 if (max_pixel_depth < 8)
4468 max_pixel_depth = 8;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004469
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004470 if (png_ptr->num_trans != 0)
Guy Schalnat0d580581995-07-20 02:43:20 -05004471 max_pixel_depth *= 2;
4472 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004473
Guy Schalnat0d580581995-07-20 02:43:20 -05004474 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
4475 {
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004476 if (png_ptr->num_trans != 0)
Guy Schalnat0d580581995-07-20 02:43:20 -05004477 {
4478 max_pixel_depth *= 4;
4479 max_pixel_depth /= 3;
4480 }
4481 }
4482 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004483#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05004484
John Bowler4d562962011-02-12 09:01:20 -06004485#ifdef PNG_READ_EXPAND_16_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004486 if ((png_ptr->transformations & PNG_EXPAND_16) != 0)
John Bowler4d562962011-02-12 09:01:20 -06004487 {
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05004488# ifdef PNG_READ_EXPAND_SUPPORTED
4489 /* In fact it is an error if it isn't supported, but checking is
4490 * the safe way.
4491 */
4492 if ((png_ptr->transformations & PNG_EXPAND) != 0)
4493 {
4494 if (png_ptr->bit_depth < 16)
4495 max_pixel_depth *= 2;
4496 }
4497 else
4498# endif
4499 png_ptr->transformations &= ~PNG_EXPAND_16;
John Bowler4d562962011-02-12 09:01:20 -06004500 }
4501#endif
4502
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004503#ifdef PNG_READ_FILLER_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004504 if ((png_ptr->transformations & (PNG_FILLER)) != 0)
Guy Schalnat0d580581995-07-20 02:43:20 -05004505 {
John Bowlere6fb6912011-11-08 21:35:16 -06004506 if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004507 {
4508 if (max_pixel_depth <= 8)
4509 max_pixel_depth = 16;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004510
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004511 else
4512 max_pixel_depth = 32;
4513 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004514
John Bowlere6fb6912011-11-08 21:35:16 -06004515 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB ||
4516 png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004517 {
4518 if (max_pixel_depth <= 32)
4519 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004520
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004521 else
4522 max_pixel_depth = 64;
4523 }
Guy Schalnat0d580581995-07-20 02:43:20 -05004524 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004525#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05004526
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004527#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004528 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0)
Guy Schalnat0d580581995-07-20 02:43:20 -05004529 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06004530 if (
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004531#ifdef PNG_READ_EXPAND_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004532 (png_ptr->num_trans != 0 &&
4533 (png_ptr->transformations & PNG_EXPAND) != 0) ||
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06004534#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004535#ifdef PNG_READ_FILLER_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004536 (png_ptr->transformations & (PNG_FILLER)) != 0 ||
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06004537#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004538 png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
Guy Schalnat0d580581995-07-20 02:43:20 -05004539 {
4540 if (max_pixel_depth <= 16)
4541 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004542
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06004543 else
Guy Schalnat0d580581995-07-20 02:43:20 -05004544 max_pixel_depth = 64;
4545 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004546
Guy Schalnat0d580581995-07-20 02:43:20 -05004547 else
4548 {
4549 if (max_pixel_depth <= 8)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004550 {
4551 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06004552 max_pixel_depth = 32;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004553
4554 else
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06004555 max_pixel_depth = 24;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004556 }
4557
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06004558 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4559 max_pixel_depth = 64;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004560
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06004561 else
Guy Schalnat0d580581995-07-20 02:43:20 -05004562 max_pixel_depth = 48;
4563 }
4564 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004565#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05004566
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05004567#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
4568defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004569 if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0)
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004570 {
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -05004571 unsigned int user_pixel_depth = png_ptr->user_transform_depth *
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05004572 png_ptr->user_transform_channels;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004573
4574 if (user_pixel_depth > max_pixel_depth)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004575 max_pixel_depth = user_pixel_depth;
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004576 }
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05004577#endif
4578
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004579 /* This value is stored in png_struct and double checked in the row read
4580 * code.
4581 */
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05004582 png_ptr->maximum_pixel_depth = (png_byte)max_pixel_depth;
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004583 png_ptr->transformed_pixel_depth = 0; /* calculated on demand */
4584
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004585 /* Align the width on the next larger 8 pixels. Mainly used
4586 * for interlacing
4587 */
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05004588 row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004589 /* Calculate the maximum bytes needed, adding a byte and a pixel
4590 * for safety's sake
4591 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004592 row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) +
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -05004593 1 + ((max_pixel_depth + 7) >> 3U);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004594
Guy Schalnat0d580581995-07-20 02:43:20 -05004595#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05004596 if (row_bytes > (png_uint_32)65536L)
Guy Schalnate5a37791996-06-05 15:50:50 -05004597 png_error(png_ptr, "This image requires a row greater than 64KB");
Guy Schalnat0d580581995-07-20 02:43:20 -05004598#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004599
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06004600 if (row_bytes + 48 > png_ptr->old_big_row_buf_size)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004601 {
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -05004602 png_free(png_ptr, png_ptr->big_row_buf);
4603 png_free(png_ptr, png_ptr->big_prev_row);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004604
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -05004605 if (png_ptr->interlaced != 0)
4606 png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr,
4607 row_bytes + 48);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004608
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -05004609 else
4610 png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004611
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -05004612 png_ptr->big_prev_row = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06004613
4614#ifdef PNG_ALIGNED_MEMORY_SUPPORTED
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -05004615 /* Use 16-byte aligned memory for row_buf with at least 16 bytes
4616 * of padding before and after row_buf; treat prev_row similarly.
4617 * NOTE: the alignment is to the start of the pixels, one beyond the start
4618 * of the buffer, because of the filter byte. Prior to libpng 1.5.6 this
4619 * was incorrect; the filter byte was aligned, which had the exact
4620 * opposite effect of that intended.
4621 */
4622 {
4623 png_bytep temp = png_ptr->big_row_buf + 32;
4624 int extra = (int)((temp - (png_bytep)0) & 0x0f);
4625 png_ptr->row_buf = temp - extra - 1/*filter byte*/;
Mans Rullgardc9e27d02011-10-17 15:25:03 -05004626
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -05004627 temp = png_ptr->big_prev_row + 32;
4628 extra = (int)((temp - (png_bytep)0) & 0x0f);
4629 png_ptr->prev_row = temp - extra - 1/*filter byte*/;
4630 }
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004631
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06004632#else
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -05004633 /* Use 31 bytes of padding before and 17 bytes after row_buf. */
4634 png_ptr->row_buf = png_ptr->big_row_buf + 31;
4635 png_ptr->prev_row = png_ptr->big_prev_row + 31;
Glenn Randers-Pehrsoneddc5af2009-11-20 21:15:06 -06004636#endif
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -05004637 png_ptr->old_big_row_buf_size = row_bytes + 48;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004638 }
Guy Schalnat0d580581995-07-20 02:43:20 -05004639
4640#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004641 if (png_ptr->rowbytes > 65535)
Guy Schalnate5a37791996-06-05 15:50:50 -05004642 png_error(png_ptr, "This image requires a row greater than 64KB");
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004643
Guy Schalnat0d580581995-07-20 02:43:20 -05004644#endif
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004645 if (png_ptr->rowbytes > (PNG_SIZE_MAX - 1))
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05004646 png_error(png_ptr, "Row has too many bytes to allocate in memory");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004647
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004648 memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05004649
Glenn Randers-Pehrson5c92dec2011-01-07 18:28:47 -06004650 png_debug1(3, "width = %u,", png_ptr->width);
4651 png_debug1(3, "height = %u,", png_ptr->height);
4652 png_debug1(3, "iwidth = %u,", png_ptr->iwidth);
4653 png_debug1(3, "num_rows = %u,", png_ptr->num_rows);
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05004654 png_debug1(3, "rowbytes = %lu,", (unsigned long)png_ptr->rowbytes);
4655 png_debug1(3, "irowbytes = %lu",
Glenn Randers-Pehrsonb764c602011-01-14 21:18:37 -06004656 (unsigned long)PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05004657
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004658 /* The sequential reader needs a buffer for IDAT, but the progressive reader
4659 * does not, so free the read buffer now regardless; the sequential reader
4660 * reallocates it on demand.
4661 */
Glenn Randers-Pehrsona1312f72016-10-16 14:34:40 -05004662 if (png_ptr->read_buffer != NULL)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004663 {
4664 png_bytep buffer = png_ptr->read_buffer;
4665
4666 png_ptr->read_buffer_size = 0;
4667 png_ptr->read_buffer = NULL;
4668 png_free(png_ptr, buffer);
4669 }
4670
John Bowler0c7ac062013-05-07 21:59:05 -05004671 /* Finally claim the zstream for the inflate of the IDAT data, use the bits
4672 * value from the stream (note that this will result in a fatal error if the
4673 * IDAT stream has a bogus deflate header window_bits value, but this should
4674 * not be happening any longer!)
Glenn Randers-Pehrsondb67cba2013-05-07 14:31:35 -05004675 */
John Bowler0c7ac062013-05-07 21:59:05 -05004676 if (png_inflate_claim(png_ptr, png_IDAT) != Z_OK)
4677 png_error(png_ptr, png_ptr->zstream.msg);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004678
Guy Schalnate5a37791996-06-05 15:50:50 -05004679 png_ptr->flags |= PNG_FLAG_ROW_INIT;
Guy Schalnat0d580581995-07-20 02:43:20 -05004680}
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06004681#endif /* READ */