blob: 8382444d81d0ca00f34ee83089d7b74efa0c43e4 [file] [log] [blame]
The Android Open Source Project893912b2009-03-03 19:30:05 -08001
2/* pngrutil.c - utilities to read a PNG file
3 *
Matt Sarett96be9082016-05-03 13:29:54 -04004 * Last changed in libpng 1.6.20 [December 3, 2015]
5 * Copyright (c) 1998-2015 Glenn Randers-Pehrson
The Android Open Source Project893912b2009-03-03 19:30:05 -08006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
8 *
Patrick Scotta0bb96c2009-07-22 11:50:02 -04009 * This code is released under the libpng license.
10 * For conditions of distribution and use, see the disclaimer
11 * and license in png.h
12 *
The Android Open Source Project893912b2009-03-03 19:30:05 -080013 * This file contains routines that are only called from within
14 * libpng itself during the course of reading an image.
15 */
16
Chris Craikb50c2172013-07-29 15:28:30 -070017#include "pngpriv.h"
18
Patrick Scott5f6bd842010-06-28 16:55:16 -040019#ifdef PNG_READ_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -080020
The Android Open Source Project893912b2009-03-03 19:30:05 -080021png_uint_32 PNGAPI
Chris Craikb50c2172013-07-29 15:28:30 -070022png_get_uint_31(png_const_structrp png_ptr, png_const_bytep buf)
The Android Open Source Project893912b2009-03-03 19:30:05 -080023{
Chris Craikb50c2172013-07-29 15:28:30 -070024 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);
The Android Open Source Project893912b2009-03-03 19:30:05 -080030}
Chris Craikb50c2172013-07-29 15:28:30 -070031
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 */
41png_get_fixed_point(png_structrp png_ptr, png_const_bytep buf)
42{
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
The Android Open Source Project893912b2009-03-03 19:30:05 -080066/* Grab an unsigned 32-bit integer from a buffer in big-endian format. */
Chris Craikb50c2172013-07-29 15:28:30 -070067png_uint_32 (PNGAPI
68png_get_uint_32)(png_const_bytep buf)
The Android Open Source Project893912b2009-03-03 19:30:05 -080069{
Chris Craikb50c2172013-07-29 15:28:30 -070070 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)) ) ;
The Android Open Source Project893912b2009-03-03 19:30:05 -080075
Chris Craikb50c2172013-07-29 15:28:30 -070076 return uval;
The Android Open Source Project893912b2009-03-03 19:30:05 -080077}
78
79/* Grab a signed 32-bit integer from a buffer in big-endian format. The
Chris Craikb50c2172013-07-29 15:28:30 -070080 * 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.
Patrick Scotta0bb96c2009-07-22 11:50:02 -040083 */
Chris Craikb50c2172013-07-29 15:28:30 -070084png_int_32 (PNGAPI
85png_get_int_32)(png_const_bytep buf)
The Android Open Source Project893912b2009-03-03 19:30:05 -080086{
Chris Craikb50c2172013-07-29 15:28:30 -070087 png_uint_32 uval = png_get_uint_32(buf);
88 if ((uval & 0x80000000) == 0) /* non-negative */
89 return uval;
The Android Open Source Project893912b2009-03-03 19:30:05 -080090
Chris Craikb50c2172013-07-29 15:28:30 -070091 uval = (uval ^ 0xffffffff) + 1; /* 2's complement: -x = ~x+1 */
Matt Sarett96be9082016-05-03 13:29:54 -040092 if ((uval & 0x80000000) == 0) /* no overflow */
93 return -(png_int_32)uval;
94 /* 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;
The Android Open Source Project893912b2009-03-03 19:30:05 -080099}
100
101/* Grab an unsigned 16-bit integer from a buffer in big-endian format. */
Chris Craikb50c2172013-07-29 15:28:30 -0700102png_uint_16 (PNGAPI
103png_get_uint_16)(png_const_bytep buf)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800104{
Chris Craikb50c2172013-07-29 15:28:30 -0700105 /* ANSI-C requires an int value to accomodate at least 16 bits so this
106 * works and allows the compiler not to worry about possible narrowing
Matt Sarett96be9082016-05-03 13:29:54 -0400107 * on 32-bit systems. (Pre-ANSI systems did not make integers smaller
Chris Craikb50c2172013-07-29 15:28:30 -0700108 * than 16 bits either.)
109 */
110 unsigned int val =
111 ((unsigned int)(*buf) << 8) +
112 ((unsigned int)(*(buf + 1)));
The Android Open Source Project893912b2009-03-03 19:30:05 -0800113
Chris Craikb50c2172013-07-29 15:28:30 -0700114 return (png_uint_16)val;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800115}
Chris Craikb50c2172013-07-29 15:28:30 -0700116
Matt Sarett96be9082016-05-03 13:29:54 -0400117#endif /* READ_INT_FUNCTIONS */
Chris Craikb50c2172013-07-29 15:28:30 -0700118
119/* Read and check the PNG file signature */
120void /* PRIVATE */
121png_read_sig(png_structrp png_ptr, png_inforp info_ptr)
122{
123 png_size_t num_checked, num_to_check;
124
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
Matt Sarett96be9082016-05-03 13:29:54 -0400140 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700141 {
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}
The Android Open Source Project893912b2009-03-03 19:30:05 -0800151
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700152/* 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 */
Chris Craikb50c2172013-07-29 15:28:30 -0700156png_read_chunk_header(png_structrp png_ptr)
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700157{
158 png_byte buf[8];
159 png_uint_32 length;
160
Chris Craikb50c2172013-07-29 15:28:30 -0700161#ifdef PNG_IO_STATE_SUPPORTED
162 png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_HDR;
163#endif
164
165 /* Read the length and the chunk name.
166 * This must be performed in a single I/O call.
167 */
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700168 png_read_data(png_ptr, buf, 8);
169 length = png_get_uint_31(png_ptr, buf);
170
Chris Craikb50c2172013-07-29 15:28:30 -0700171 /* Put the chunk name into png_ptr->chunk_name. */
172 png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(buf+4);
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700173
Chris Craikb50c2172013-07-29 15:28:30 -0700174 png_debug2(0, "Reading %lx chunk, length = %lu",
175 (unsigned long)png_ptr->chunk_name, (unsigned long)length);
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700176
Chris Craikb50c2172013-07-29 15:28:30 -0700177 /* Reset the crc and run it over the chunk name. */
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700178 png_reset_crc(png_ptr);
Chris Craikb50c2172013-07-29 15:28:30 -0700179 png_calculate_crc(png_ptr, buf + 4, 4);
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700180
Chris Craikb50c2172013-07-29 15:28:30 -0700181 /* Check to see if chunk name is valid. */
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700182 png_check_chunk_name(png_ptr, png_ptr->chunk_name);
183
Chris Craikb50c2172013-07-29 15:28:30 -0700184#ifdef PNG_IO_STATE_SUPPORTED
185 png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_DATA;
186#endif
187
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700188 return length;
189}
190
The Android Open Source Project893912b2009-03-03 19:30:05 -0800191/* Read data, and (optionally) run it through the CRC. */
192void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -0700193png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800194{
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400195 if (png_ptr == NULL)
196 return;
Chris Craikb50c2172013-07-29 15:28:30 -0700197
The Android Open Source Project893912b2009-03-03 19:30:05 -0800198 png_read_data(png_ptr, buf, length);
199 png_calculate_crc(png_ptr, buf, length);
200}
201
202/* Optionally skip data and then check the CRC. Depending on whether we
Chris Craikb50c2172013-07-29 15:28:30 -0700203 * are reading an ancillary or critical chunk, and how the program has set
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400204 * things up, we may calculate the CRC on the data and print a message.
205 * Returns '1' if there was a CRC error, '0' otherwise.
206 */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800207int /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -0700208png_crc_finish(png_structrp png_ptr, png_uint_32 skip)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800209{
Chris Craikb50c2172013-07-29 15:28:30 -0700210 /* The size of the local buffer for inflate is a good guess as to a
211 * reasonable size to use for buffering reads from the application.
212 */
213 while (skip > 0)
214 {
215 png_uint_32 len;
216 png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];
The Android Open Source Project893912b2009-03-03 19:30:05 -0800217
Chris Craikb50c2172013-07-29 15:28:30 -0700218 len = (sizeof tmpbuf);
219 if (len > skip)
220 len = skip;
221 skip -= len;
222
223 png_crc_read(png_ptr, tmpbuf, len);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800224 }
225
Matt Sarett96be9082016-05-03 13:29:54 -0400226 if (png_crc_error(png_ptr) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800227 {
Matt Sarett96be9082016-05-03 13:29:54 -0400228 if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) != 0 ?
229 (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) == 0 :
230 (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800231 {
232 png_chunk_warning(png_ptr, "CRC error");
233 }
Chris Craikb50c2172013-07-29 15:28:30 -0700234
The Android Open Source Project893912b2009-03-03 19:30:05 -0800235 else
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530236 png_chunk_error(png_ptr, "CRC error");
Chris Craikb50c2172013-07-29 15:28:30 -0700237
The Android Open Source Project893912b2009-03-03 19:30:05 -0800238 return (1);
239 }
240
241 return (0);
242}
243
Sireesh Tripurarib0277d02014-05-09 15:39:12 +0530244#ifdef PNG_INDEX_SUPPORTED
Matt Sarett96be9082016-05-03 13:29:54 -0400245/* If tile index is used to skip over data and decode a partial image
246 * the crc value may be incorrect.
247 * The crc will only be calculated for the partial data read,
248 * not the entire data, which will result in an incorrect crc value.
249 * This function treats a png_crc_error as a warning, as opposed to the
250 * original function png_crc_finish, which will treat it as an error.
251 */
Sireesh Tripurarib0277d02014-05-09 15:39:12 +0530252int /* PRIVATE */
253png_opt_crc_finish(png_structrp png_ptr, png_uint_32 skip)
254{
255 while (skip > 0)
256 {
257 png_uint_32 len;
258 png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];
259
260 len = (sizeof tmpbuf);
261 if (len > skip)
262 len = skip;
263 skip -= len;
264
265 png_crc_read(png_ptr, tmpbuf, len);
266 }
267
268 if (png_crc_error(png_ptr))
269 {
270 png_chunk_warning(png_ptr, "CRC error");
271 return (1);
272 }
273
274 return (0);
275}
276#endif
277
The Android Open Source Project893912b2009-03-03 19:30:05 -0800278/* Compare the CRC stored in the PNG file with that calculated by libpng from
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400279 * the data it has read thus far.
280 */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800281int /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -0700282png_crc_error(png_structrp png_ptr)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800283{
284 png_byte crc_bytes[4];
285 png_uint_32 crc;
286 int need_crc = 1;
287
Matt Sarett96be9082016-05-03 13:29:54 -0400288 if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800289 {
290 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
291 (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
292 need_crc = 0;
293 }
Chris Craikb50c2172013-07-29 15:28:30 -0700294
295 else /* critical */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800296 {
Matt Sarett96be9082016-05-03 13:29:54 -0400297 if ((png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800298 need_crc = 0;
299 }
300
Chris Craikb50c2172013-07-29 15:28:30 -0700301#ifdef PNG_IO_STATE_SUPPORTED
302 png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_CRC;
303#endif
304
305 /* The chunk CRC must be serialized in a single I/O call. */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800306 png_read_data(png_ptr, crc_bytes, 4);
307
Matt Sarett96be9082016-05-03 13:29:54 -0400308 if (need_crc != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800309 {
310 crc = png_get_uint_32(crc_bytes);
311 return ((int)(crc != png_ptr->crc));
312 }
Chris Craikb50c2172013-07-29 15:28:30 -0700313
The Android Open Source Project893912b2009-03-03 19:30:05 -0800314 else
315 return (0);
316}
317
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530318#if defined(PNG_READ_iCCP_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) ||\
319 defined(PNG_READ_pCAL_SUPPORTED) || defined(PNG_READ_sCAL_SUPPORTED) ||\
320 defined(PNG_READ_sPLT_SUPPORTED) || defined(PNG_READ_tEXt_SUPPORTED) ||\
321 defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_SEQUENTIAL_READ_SUPPORTED)
Chris Craikb50c2172013-07-29 15:28:30 -0700322/* Manage the read buffer; this simply reallocates the buffer if it is not small
323 * enough (or if it is not allocated). The routine returns a pointer to the
324 * buffer; if an error occurs and 'warn' is set the routine returns NULL, else
325 * it will call png_error (via png_malloc) on failure. (warn == 2 means
326 * 'silent').
327 */
328static png_bytep
329png_read_buffer(png_structrp png_ptr, png_alloc_size_t new_size, int warn)
Patrick Scott5f6bd842010-06-28 16:55:16 -0400330{
Chris Craikb50c2172013-07-29 15:28:30 -0700331 png_bytep buffer = png_ptr->read_buffer;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400332
Chris Craikb50c2172013-07-29 15:28:30 -0700333 if (buffer != NULL && new_size > png_ptr->read_buffer_size)
Patrick Scott5f6bd842010-06-28 16:55:16 -0400334 {
Chris Craikb50c2172013-07-29 15:28:30 -0700335 png_ptr->read_buffer = NULL;
336 png_ptr->read_buffer = NULL;
337 png_ptr->read_buffer_size = 0;
338 png_free(png_ptr, buffer);
339 buffer = NULL;
340 }
Patrick Scott5f6bd842010-06-28 16:55:16 -0400341
Chris Craikb50c2172013-07-29 15:28:30 -0700342 if (buffer == NULL)
343 {
344 buffer = png_voidcast(png_bytep, png_malloc_base(png_ptr, new_size));
Patrick Scott5f6bd842010-06-28 16:55:16 -0400345
Chris Craikb50c2172013-07-29 15:28:30 -0700346 if (buffer != NULL)
Patrick Scott5f6bd842010-06-28 16:55:16 -0400347 {
Chris Craikb50c2172013-07-29 15:28:30 -0700348 png_ptr->read_buffer = buffer;
349 png_ptr->read_buffer_size = new_size;
350 }
351
352 else if (warn < 2) /* else silent */
353 {
Matt Sarett96be9082016-05-03 13:29:54 -0400354 if (warn != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700355 png_chunk_warning(png_ptr, "insufficient memory to read chunk");
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530356
Chris Craikb50c2172013-07-29 15:28:30 -0700357 else
Chris Craikb50c2172013-07-29 15:28:30 -0700358 png_chunk_error(png_ptr, "insufficient memory to read chunk");
Chris Craikb50c2172013-07-29 15:28:30 -0700359 }
360 }
361
362 return buffer;
363}
Matt Sarett96be9082016-05-03 13:29:54 -0400364#endif /* READ_iCCP|iTXt|pCAL|sCAL|sPLT|tEXt|zTXt|SEQUENTIAL_READ */
Chris Craikb50c2172013-07-29 15:28:30 -0700365
366/* png_inflate_claim: claim the zstream for some nefarious purpose that involves
367 * decompression. Returns Z_OK on success, else a zlib error code. It checks
368 * the owner but, in final release builds, just issues a warning if some other
369 * chunk apparently owns the stream. Prior to release it does a png_error.
370 */
371static int
372png_inflate_claim(png_structrp png_ptr, png_uint_32 owner)
373{
374 if (png_ptr->zowner != 0)
375 {
376 char msg[64];
377
378 PNG_STRING_FROM_CHUNK(msg, png_ptr->zowner);
379 /* So the message that results is "<chunk> using zstream"; this is an
380 * internal error, but is very useful for debugging. i18n requirements
381 * are minimal.
382 */
383 (void)png_safecat(msg, (sizeof msg), 4, " using zstream");
Matt Sarett96be9082016-05-03 13:29:54 -0400384#if PNG_RELEASE_BUILD
385 png_chunk_warning(png_ptr, msg);
386 png_ptr->zowner = 0;
387#else
388 png_chunk_error(png_ptr, msg);
389#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700390 }
391
392 /* Implementation note: unlike 'png_deflate_claim' this internal function
393 * does not take the size of the data as an argument. Some efficiency could
394 * be gained by using this when it is known *if* the zlib stream itself does
395 * not record the number; however, this is an illusion: the original writer
396 * of the PNG may have selected a lower window size, and we really must
397 * follow that because, for systems with with limited capabilities, we
398 * would otherwise reject the application's attempts to use a smaller window
399 * size (zlib doesn't have an interface to say "this or lower"!).
400 *
401 * inflateReset2 was added to zlib 1.2.4; before this the window could not be
402 * reset, therefore it is necessary to always allocate the maximum window
403 * size with earlier zlibs just in case later compressed chunks need it.
404 */
405 {
406 int ret; /* zlib return code */
Matt Sarett96be9082016-05-03 13:29:54 -0400407#if PNG_ZLIB_VERNUM >= 0x1240
Chris Craikb50c2172013-07-29 15:28:30 -0700408
Matt Sarett96be9082016-05-03 13:29:54 -0400409# if defined(PNG_SET_OPTION_SUPPORTED) && defined(PNG_MAXIMUM_INFLATE_WINDOW)
410 int window_bits;
Chris Craikb50c2172013-07-29 15:28:30 -0700411
Matt Sarett96be9082016-05-03 13:29:54 -0400412 if (((png_ptr->options >> PNG_MAXIMUM_INFLATE_WINDOW) & 3) ==
413 PNG_OPTION_ON)
414 {
415 window_bits = 15;
416 png_ptr->zstream_start = 0; /* fixed window size */
417 }
Chris Craikb50c2172013-07-29 15:28:30 -0700418
Matt Sarett96be9082016-05-03 13:29:54 -0400419 else
420 {
421 window_bits = 0;
422 png_ptr->zstream_start = 1;
423 }
424# else
425# define window_bits 0
426# endif
427#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700428
429 /* Set this for safety, just in case the previous owner left pointers to
430 * memory allocations.
431 */
432 png_ptr->zstream.next_in = NULL;
433 png_ptr->zstream.avail_in = 0;
434 png_ptr->zstream.next_out = NULL;
435 png_ptr->zstream.avail_out = 0;
436
Matt Sarett96be9082016-05-03 13:29:54 -0400437 if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700438 {
Matt Sarett96be9082016-05-03 13:29:54 -0400439#if PNG_ZLIB_VERNUM < 0x1240
440 ret = inflateReset(&png_ptr->zstream);
441#else
442 ret = inflateReset2(&png_ptr->zstream, window_bits);
443#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700444 }
445
446 else
447 {
Matt Sarett96be9082016-05-03 13:29:54 -0400448#if PNG_ZLIB_VERNUM < 0x1240
449 ret = inflateInit(&png_ptr->zstream);
450#else
451 ret = inflateInit2(&png_ptr->zstream, window_bits);
452#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700453
454 if (ret == Z_OK)
455 png_ptr->flags |= PNG_FLAG_ZSTREAM_INITIALIZED;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400456 }
457
458 if (ret == Z_OK)
Chris Craikb50c2172013-07-29 15:28:30 -0700459 png_ptr->zowner = owner;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400460
Chris Craikb50c2172013-07-29 15:28:30 -0700461 else
462 png_zstream_error(png_ptr, ret);
463
464 return ret;
465 }
466
Matt Sarett96be9082016-05-03 13:29:54 -0400467#ifdef window_bits
468# undef window_bits
469#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700470}
471
Matt Sarett96be9082016-05-03 13:29:54 -0400472#if PNG_ZLIB_VERNUM >= 0x1240
473/* Handle the start of the inflate stream if we called inflateInit2(strm,0);
474 * in this case some zlib versions skip validation of the CINFO field and, in
475 * certain circumstances, libpng may end up displaying an invalid image, in
476 * contrast to implementations that call zlib in the normal way (e.g. libpng
477 * 1.5).
478 */
479int /* PRIVATE */
480png_zlib_inflate(png_structrp png_ptr, int flush)
481{
482 if (png_ptr->zstream_start && png_ptr->zstream.avail_in > 0)
483 {
484 if ((*png_ptr->zstream.next_in >> 4) > 7)
485 {
486 png_ptr->zstream.msg = "invalid window size (libpng)";
487 return Z_DATA_ERROR;
488 }
489
490 png_ptr->zstream_start = 0;
491 }
492
493 return inflate(&png_ptr->zstream, flush);
494}
495#endif /* Zlib >= 1.2.4 */
496
Chris Craikb50c2172013-07-29 15:28:30 -0700497#ifdef PNG_READ_COMPRESSED_TEXT_SUPPORTED
498/* png_inflate now returns zlib error codes including Z_OK and Z_STREAM_END to
499 * allow the caller to do multiple calls if required. If the 'finish' flag is
500 * set Z_FINISH will be passed to the final inflate() call and Z_STREAM_END must
501 * be returned or there has been a problem, otherwise Z_SYNC_FLUSH is used and
502 * Z_OK or Z_STREAM_END will be returned on success.
503 *
504 * The input and output sizes are updated to the actual amounts of data consumed
505 * or written, not the amount available (as in a z_stream). The data pointers
506 * are not changed, so the next input is (data+input_size) and the next
507 * available output is (output+output_size).
508 */
509static int
510png_inflate(png_structrp png_ptr, png_uint_32 owner, int finish,
511 /* INPUT: */ png_const_bytep input, png_uint_32p input_size_ptr,
512 /* OUTPUT: */ png_bytep output, png_alloc_size_t *output_size_ptr)
513{
514 if (png_ptr->zowner == owner) /* Else not claimed */
515 {
516 int ret;
517 png_alloc_size_t avail_out = *output_size_ptr;
518 png_uint_32 avail_in = *input_size_ptr;
519
520 /* zlib can't necessarily handle more than 65535 bytes at once (i.e. it
521 * can't even necessarily handle 65536 bytes) because the type uInt is
522 * "16 bits or more". Consequently it is necessary to chunk the input to
523 * zlib. This code uses ZLIB_IO_MAX, from pngpriv.h, as the maximum (the
524 * maximum value that can be stored in a uInt.) It is possible to set
525 * ZLIB_IO_MAX to a lower value in pngpriv.h and this may sometimes have
526 * a performance advantage, because it reduces the amount of data accessed
527 * at each step and that may give the OS more time to page it in.
Patrick Scott5f6bd842010-06-28 16:55:16 -0400528 */
Chris Craikb50c2172013-07-29 15:28:30 -0700529 png_ptr->zstream.next_in = PNGZ_INPUT_CAST(input);
530 /* avail_in and avail_out are set below from 'size' */
Patrick Scott5f6bd842010-06-28 16:55:16 -0400531 png_ptr->zstream.avail_in = 0;
Chris Craikb50c2172013-07-29 15:28:30 -0700532 png_ptr->zstream.avail_out = 0;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400533
Chris Craikb50c2172013-07-29 15:28:30 -0700534 /* Read directly into the output if it is available (this is set to
535 * a local buffer below if output is NULL).
Patrick Scott5f6bd842010-06-28 16:55:16 -0400536 */
Chris Craikb50c2172013-07-29 15:28:30 -0700537 if (output != NULL)
538 png_ptr->zstream.next_out = output;
539
540 do
Patrick Scott5f6bd842010-06-28 16:55:16 -0400541 {
Chris Craikb50c2172013-07-29 15:28:30 -0700542 uInt avail;
543 Byte local_buffer[PNG_INFLATE_BUF_SIZE];
544
545 /* zlib INPUT BUFFER */
546 /* The setting of 'avail_in' used to be outside the loop; by setting it
547 * inside it is possible to chunk the input to zlib and simply rely on
548 * zlib to advance the 'next_in' pointer. This allows arbitrary
549 * amounts of data to be passed through zlib at the unavoidable cost of
550 * requiring a window save (memcpy of up to 32768 output bytes)
551 * every ZLIB_IO_MAX input bytes.
552 */
553 avail_in += png_ptr->zstream.avail_in; /* not consumed last time */
554
555 avail = ZLIB_IO_MAX;
556
557 if (avail_in < avail)
558 avail = (uInt)avail_in; /* safe: < than ZLIB_IO_MAX */
559
560 avail_in -= avail;
561 png_ptr->zstream.avail_in = avail;
562
563 /* zlib OUTPUT BUFFER */
564 avail_out += png_ptr->zstream.avail_out; /* not written last time */
565
566 avail = ZLIB_IO_MAX; /* maximum zlib can process */
567
568 if (output == NULL)
Patrick Scott5f6bd842010-06-28 16:55:16 -0400569 {
Chris Craikb50c2172013-07-29 15:28:30 -0700570 /* Reset the output buffer each time round if output is NULL and
571 * make available the full buffer, up to 'remaining_space'
572 */
573 png_ptr->zstream.next_out = local_buffer;
574 if ((sizeof local_buffer) < avail)
575 avail = (sizeof local_buffer);
Patrick Scott5f6bd842010-06-28 16:55:16 -0400576 }
577
Chris Craikb50c2172013-07-29 15:28:30 -0700578 if (avail_out < avail)
579 avail = (uInt)avail_out; /* safe: < ZLIB_IO_MAX */
Patrick Scott5f6bd842010-06-28 16:55:16 -0400580
Chris Craikb50c2172013-07-29 15:28:30 -0700581 png_ptr->zstream.avail_out = avail;
582 avail_out -= avail;
583
584 /* zlib inflate call */
585 /* In fact 'avail_out' may be 0 at this point, that happens at the end
586 * of the read when the final LZ end code was not passed at the end of
587 * the previous chunk of input data. Tell zlib if we have reached the
588 * end of the output buffer.
589 */
Matt Sarett96be9082016-05-03 13:29:54 -0400590 ret = PNG_INFLATE(png_ptr, avail_out > 0 ? Z_NO_FLUSH :
591 (finish ? Z_FINISH : Z_SYNC_FLUSH));
Chris Craikb50c2172013-07-29 15:28:30 -0700592 } while (ret == Z_OK);
593
594 /* For safety kill the local buffer pointer now */
595 if (output == NULL)
596 png_ptr->zstream.next_out = NULL;
597
598 /* Claw back the 'size' and 'remaining_space' byte counts. */
599 avail_in += png_ptr->zstream.avail_in;
600 avail_out += png_ptr->zstream.avail_out;
601
602 /* Update the input and output sizes; the updated values are the amount
603 * consumed or written, effectively the inverse of what zlib uses.
Patrick Scott5f6bd842010-06-28 16:55:16 -0400604 */
Chris Craikb50c2172013-07-29 15:28:30 -0700605 if (avail_out > 0)
606 *output_size_ptr -= avail_out;
607
608 if (avail_in > 0)
609 *input_size_ptr -= avail_in;
610
611 /* Ensure png_ptr->zstream.msg is set (even in the success case!) */
612 png_zstream_error(png_ptr, ret);
613 return ret;
614 }
615
616 else
617 {
618 /* This is a bad internal error. The recovery assigns to the zstream msg
619 * pointer, which is not owned by the caller, but this is safe; it's only
620 * used on errors!
621 */
622 png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed");
623 return Z_STREAM_ERROR;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400624 }
625}
626
The Android Open Source Project893912b2009-03-03 19:30:05 -0800627/*
Chris Craikb50c2172013-07-29 15:28:30 -0700628 * Decompress trailing data in a chunk. The assumption is that read_buffer
The Android Open Source Project893912b2009-03-03 19:30:05 -0800629 * points at an allocated area holding the contents of a chunk with a
630 * trailing compressed part. What we get back is an allocated area
631 * holding the original prefix part and an uncompressed version of the
632 * trailing part (the malloc area passed in is freed).
633 */
Chris Craikb50c2172013-07-29 15:28:30 -0700634static int
635png_decompress_chunk(png_structrp png_ptr,
636 png_uint_32 chunklength, png_uint_32 prefix_size,
637 png_alloc_size_t *newlength /* must be initialized to the maximum! */,
638 int terminate /*add a '\0' to the end of the uncompressed data*/)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800639{
Chris Craikb50c2172013-07-29 15:28:30 -0700640 /* TODO: implement different limits for different types of chunk.
641 *
642 * The caller supplies *newlength set to the maximum length of the
643 * uncompressed data, but this routine allocates space for the prefix and
644 * maybe a '\0' terminator too. We have to assume that 'prefix_size' is
645 * limited only by the maximum chunk size.
646 */
647 png_alloc_size_t limit = PNG_SIZE_MAX;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400648
Matt Sarett96be9082016-05-03 13:29:54 -0400649# ifdef PNG_SET_USER_LIMITS_SUPPORTED
650 if (png_ptr->user_chunk_malloc_max > 0 &&
651 png_ptr->user_chunk_malloc_max < limit)
652 limit = png_ptr->user_chunk_malloc_max;
653# elif PNG_USER_CHUNK_MALLOC_MAX > 0
654 if (PNG_USER_CHUNK_MALLOC_MAX < limit)
655 limit = PNG_USER_CHUNK_MALLOC_MAX;
656# endif
Patrick Scott5f6bd842010-06-28 16:55:16 -0400657
Chris Craikb50c2172013-07-29 15:28:30 -0700658 if (limit >= prefix_size + (terminate != 0))
659 {
660 int ret;
661
662 limit -= prefix_size + (terminate != 0);
663
664 if (limit < *newlength)
665 *newlength = limit;
666
667 /* Now try to claim the stream. */
668 ret = png_inflate_claim(png_ptr, png_ptr->chunk_name);
669
670 if (ret == Z_OK)
Patrick Scott5f6bd842010-06-28 16:55:16 -0400671 {
Chris Craikb50c2172013-07-29 15:28:30 -0700672 png_uint_32 lzsize = chunklength - prefix_size;
Dave Burkeccee1212012-03-05 22:36:13 -0800673
Chris Craikb50c2172013-07-29 15:28:30 -0700674 ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/,
675 /* input: */ png_ptr->read_buffer + prefix_size, &lzsize,
676 /* output: */ NULL, newlength);
677
678 if (ret == Z_STREAM_END)
Dave Burkeccee1212012-03-05 22:36:13 -0800679 {
Chris Craikb50c2172013-07-29 15:28:30 -0700680 /* Use 'inflateReset' here, not 'inflateReset2' because this
681 * preserves the previously decided window size (otherwise it would
682 * be necessary to store the previous window size.) In practice
683 * this doesn't matter anyway, because png_inflate will call inflate
684 * with Z_FINISH in almost all cases, so the window will not be
685 * maintained.
686 */
687 if (inflateReset(&png_ptr->zstream) == Z_OK)
Patrick Scott5f6bd842010-06-28 16:55:16 -0400688 {
Chris Craikb50c2172013-07-29 15:28:30 -0700689 /* Because of the limit checks above we know that the new,
690 * expanded, size will fit in a size_t (let alone an
691 * png_alloc_size_t). Use png_malloc_base here to avoid an
692 * extra OOM message.
693 */
694 png_alloc_size_t new_size = *newlength;
695 png_alloc_size_t buffer_size = prefix_size + new_size +
696 (terminate != 0);
697 png_bytep text = png_voidcast(png_bytep, png_malloc_base(png_ptr,
698 buffer_size));
699
700 if (text != NULL)
701 {
702 ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/,
703 png_ptr->read_buffer + prefix_size, &lzsize,
704 text + prefix_size, newlength);
705
706 if (ret == Z_STREAM_END)
707 {
708 if (new_size == *newlength)
709 {
Matt Sarett96be9082016-05-03 13:29:54 -0400710 if (terminate != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700711 text[prefix_size + *newlength] = 0;
712
713 if (prefix_size > 0)
714 memcpy(text, png_ptr->read_buffer, prefix_size);
715
716 {
717 png_bytep old_ptr = png_ptr->read_buffer;
718
719 png_ptr->read_buffer = text;
720 png_ptr->read_buffer_size = buffer_size;
721 text = old_ptr; /* freed below */
722 }
723 }
724
725 else
726 {
727 /* The size changed on the second read, there can be no
728 * guarantee that anything is correct at this point.
729 * The 'msg' pointer has been set to "unexpected end of
730 * LZ stream", which is fine, but return an error code
731 * that the caller won't accept.
732 */
733 ret = PNG_UNEXPECTED_ZLIB_RETURN;
734 }
735 }
736
737 else if (ret == Z_OK)
738 ret = PNG_UNEXPECTED_ZLIB_RETURN; /* for safety */
739
740 /* Free the text pointer (this is the old read_buffer on
741 * success)
742 */
743 png_free(png_ptr, text);
744
745 /* This really is very benign, but it's still an error because
746 * the extra space may otherwise be used as a Trojan Horse.
747 */
748 if (ret == Z_STREAM_END &&
749 chunklength - prefix_size != lzsize)
750 png_chunk_benign_error(png_ptr, "extra compressed data");
751 }
752
753 else
754 {
755 /* Out of memory allocating the buffer */
756 ret = Z_MEM_ERROR;
757 png_zstream_error(png_ptr, Z_MEM_ERROR);
758 }
Patrick Scott5f6bd842010-06-28 16:55:16 -0400759 }
760
Chris Craikb50c2172013-07-29 15:28:30 -0700761 else
762 {
763 /* inflateReset failed, store the error message */
764 png_zstream_error(png_ptr, ret);
765
766 if (ret == Z_STREAM_END)
767 ret = PNG_UNEXPECTED_ZLIB_RETURN;
768 }
Patrick Scott5f6bd842010-06-28 16:55:16 -0400769 }
Chris Craikb50c2172013-07-29 15:28:30 -0700770
771 else if (ret == Z_OK)
772 ret = PNG_UNEXPECTED_ZLIB_RETURN;
773
774 /* Release the claimed stream */
775 png_ptr->zowner = 0;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400776 }
Chris Craikb50c2172013-07-29 15:28:30 -0700777
778 else /* the claim failed */ if (ret == Z_STREAM_END) /* impossible! */
779 ret = PNG_UNEXPECTED_ZLIB_RETURN;
780
781 return ret;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400782 }
783
Chris Craikb50c2172013-07-29 15:28:30 -0700784 else
The Android Open Source Project893912b2009-03-03 19:30:05 -0800785 {
Chris Craikb50c2172013-07-29 15:28:30 -0700786 /* Application/configuration limits exceeded */
787 png_zstream_error(png_ptr, Z_MEM_ERROR);
788 return Z_MEM_ERROR;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800789 }
Chris Craikb50c2172013-07-29 15:28:30 -0700790}
Matt Sarett96be9082016-05-03 13:29:54 -0400791#endif /* READ_COMPRESSED_TEXT */
Patrick Scott5f6bd842010-06-28 16:55:16 -0400792
Chris Craikb50c2172013-07-29 15:28:30 -0700793#ifdef PNG_READ_iCCP_SUPPORTED
794/* Perform a partial read and decompress, producing 'avail_out' bytes and
795 * reading from the current chunk as required.
796 */
797static int
798png_inflate_read(png_structrp png_ptr, png_bytep read_buffer, uInt read_size,
799 png_uint_32p chunk_bytes, png_bytep next_out, png_alloc_size_t *out_size,
800 int finish)
801{
802 if (png_ptr->zowner == png_ptr->chunk_name)
Patrick Scott5f6bd842010-06-28 16:55:16 -0400803 {
Chris Craikb50c2172013-07-29 15:28:30 -0700804 int ret;
805
806 /* next_in and avail_in must have been initialized by the caller. */
807 png_ptr->zstream.next_out = next_out;
808 png_ptr->zstream.avail_out = 0; /* set in the loop */
809
810 do
Patrick Scott5f6bd842010-06-28 16:55:16 -0400811 {
Chris Craikb50c2172013-07-29 15:28:30 -0700812 if (png_ptr->zstream.avail_in == 0)
813 {
814 if (read_size > *chunk_bytes)
815 read_size = (uInt)*chunk_bytes;
816 *chunk_bytes -= read_size;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400817
Chris Craikb50c2172013-07-29 15:28:30 -0700818 if (read_size > 0)
819 png_crc_read(png_ptr, read_buffer, read_size);
820
821 png_ptr->zstream.next_in = read_buffer;
822 png_ptr->zstream.avail_in = read_size;
823 }
824
825 if (png_ptr->zstream.avail_out == 0)
826 {
827 uInt avail = ZLIB_IO_MAX;
828 if (avail > *out_size)
829 avail = (uInt)*out_size;
830 *out_size -= avail;
831
832 png_ptr->zstream.avail_out = avail;
833 }
834
835 /* Use Z_SYNC_FLUSH when there is no more chunk data to ensure that all
836 * the available output is produced; this allows reading of truncated
837 * streams.
838 */
Matt Sarett96be9082016-05-03 13:29:54 -0400839 ret = PNG_INFLATE(png_ptr,
Chris Craikb50c2172013-07-29 15:28:30 -0700840 *chunk_bytes > 0 ? Z_NO_FLUSH : (finish ? Z_FINISH : Z_SYNC_FLUSH));
Patrick Scott5f6bd842010-06-28 16:55:16 -0400841 }
Chris Craikb50c2172013-07-29 15:28:30 -0700842 while (ret == Z_OK && (*out_size > 0 || png_ptr->zstream.avail_out > 0));
843
844 *out_size += png_ptr->zstream.avail_out;
845 png_ptr->zstream.avail_out = 0; /* Should not be required, but is safe */
846
847 /* Ensure the error message pointer is always set: */
848 png_zstream_error(png_ptr, ret);
849 return ret;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400850 }
851
Chris Craikb50c2172013-07-29 15:28:30 -0700852 else
853 {
854 png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed");
855 return Z_STREAM_ERROR;
856 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800857}
858#endif
859
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400860/* Read and check the IDHR chunk */
Matt Sarett96be9082016-05-03 13:29:54 -0400861
The Android Open Source Project893912b2009-03-03 19:30:05 -0800862void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -0700863png_handle_IHDR(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800864{
865 png_byte buf[13];
866 png_uint_32 width, height;
867 int bit_depth, color_type, compression_type, filter_type;
868 int interlace_type;
869
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700870 png_debug(1, "in png_handle_IHDR");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800871
Matt Sarett96be9082016-05-03 13:29:54 -0400872 if ((png_ptr->mode & PNG_HAVE_IHDR) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700873 png_chunk_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800874
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400875 /* Check the length */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800876 if (length != 13)
Chris Craikb50c2172013-07-29 15:28:30 -0700877 png_chunk_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800878
879 png_ptr->mode |= PNG_HAVE_IHDR;
880
881 png_crc_read(png_ptr, buf, 13);
882 png_crc_finish(png_ptr, 0);
883
884 width = png_get_uint_31(png_ptr, buf);
885 height = png_get_uint_31(png_ptr, buf + 4);
886 bit_depth = buf[8];
887 color_type = buf[9];
888 compression_type = buf[10];
889 filter_type = buf[11];
890 interlace_type = buf[12];
891
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400892 /* Set internal variables */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800893 png_ptr->width = width;
894 png_ptr->height = height;
895 png_ptr->bit_depth = (png_byte)bit_depth;
896 png_ptr->interlaced = (png_byte)interlace_type;
897 png_ptr->color_type = (png_byte)color_type;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400898#ifdef PNG_MNG_FEATURES_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800899 png_ptr->filter_type = (png_byte)filter_type;
900#endif
901 png_ptr->compression_type = (png_byte)compression_type;
902
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400903 /* Find number of channels */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800904 switch (png_ptr->color_type)
905 {
Chris Craikb50c2172013-07-29 15:28:30 -0700906 default: /* invalid, png_set_IHDR calls png_error */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800907 case PNG_COLOR_TYPE_GRAY:
908 case PNG_COLOR_TYPE_PALETTE:
909 png_ptr->channels = 1;
910 break;
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400911
The Android Open Source Project893912b2009-03-03 19:30:05 -0800912 case PNG_COLOR_TYPE_RGB:
913 png_ptr->channels = 3;
914 break;
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400915
The Android Open Source Project893912b2009-03-03 19:30:05 -0800916 case PNG_COLOR_TYPE_GRAY_ALPHA:
917 png_ptr->channels = 2;
918 break;
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400919
The Android Open Source Project893912b2009-03-03 19:30:05 -0800920 case PNG_COLOR_TYPE_RGB_ALPHA:
921 png_ptr->channels = 4;
922 break;
923 }
924
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400925 /* Set up other useful info */
Matt Sarett96be9082016-05-03 13:29:54 -0400926 png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth * png_ptr->channels);
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700927 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width);
928 png_debug1(3, "bit_depth = %d", png_ptr->bit_depth);
929 png_debug1(3, "channels = %d", png_ptr->channels);
Chris Craikb50c2172013-07-29 15:28:30 -0700930 png_debug1(3, "rowbytes = %lu", (unsigned long)png_ptr->rowbytes);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800931 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
Chris Craikb50c2172013-07-29 15:28:30 -0700932 color_type, interlace_type, compression_type, filter_type);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800933}
934
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400935/* Read and check the palette */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800936void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -0700937png_handle_PLTE(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800938{
939 png_color palette[PNG_MAX_PALETTE_LENGTH];
Matt Sarett96be9082016-05-03 13:29:54 -0400940 int max_palette_length, num, i;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400941#ifdef PNG_POINTER_INDEXING_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800942 png_colorp pal_ptr;
943#endif
944
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700945 png_debug(1, "in png_handle_PLTE");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800946
Matt Sarett96be9082016-05-03 13:29:54 -0400947 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700948 png_chunk_error(png_ptr, "missing IHDR");
949
950 /* Moved to before the 'after IDAT' check below because otherwise duplicate
951 * PLTE chunks are potentially ignored (the spec says there shall not be more
952 * than one PLTE, the error is not treated as benign, so this check trumps
953 * the requirement that PLTE appears before IDAT.)
954 */
Matt Sarett96be9082016-05-03 13:29:54 -0400955 else if ((png_ptr->mode & PNG_HAVE_PLTE) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700956 png_chunk_error(png_ptr, "duplicate");
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400957
Matt Sarett96be9082016-05-03 13:29:54 -0400958 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800959 {
Chris Craikb50c2172013-07-29 15:28:30 -0700960 /* This is benign because the non-benign error happened before, when an
961 * IDAT was encountered in a color-mapped image with no PLTE.
962 */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800963 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -0700964 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800965 return;
966 }
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400967
The Android Open Source Project893912b2009-03-03 19:30:05 -0800968 png_ptr->mode |= PNG_HAVE_PLTE;
969
Matt Sarett96be9082016-05-03 13:29:54 -0400970 if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800971 {
The Android Open Source Project893912b2009-03-03 19:30:05 -0800972 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -0700973 png_chunk_benign_error(png_ptr, "ignored in grayscale PNG");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800974 return;
975 }
Chris Craikb50c2172013-07-29 15:28:30 -0700976
Patrick Scott5f6bd842010-06-28 16:55:16 -0400977#ifndef PNG_READ_OPT_PLTE_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800978 if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
979 {
980 png_crc_finish(png_ptr, length);
981 return;
982 }
983#endif
984
985 if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3)
986 {
Chris Craikb50c2172013-07-29 15:28:30 -0700987 png_crc_finish(png_ptr, length);
988
The Android Open Source Project893912b2009-03-03 19:30:05 -0800989 if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
Chris Craikb50c2172013-07-29 15:28:30 -0700990 png_chunk_benign_error(png_ptr, "invalid");
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400991
The Android Open Source Project893912b2009-03-03 19:30:05 -0800992 else
Chris Craikb50c2172013-07-29 15:28:30 -0700993 png_chunk_error(png_ptr, "invalid");
994
995 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800996 }
997
Chris Craikb50c2172013-07-29 15:28:30 -0700998 /* The cast is safe because 'length' is less than 3*PNG_MAX_PALETTE_LENGTH */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800999 num = (int)length / 3;
1000
Matt Sarett96be9082016-05-03 13:29:54 -04001001 /* If the palette has 256 or fewer entries but is too large for the bit
1002 * depth, we don't issue an error, to preserve the behavior of previous
1003 * libpng versions. We silently truncate the unused extra palette entries
1004 * here.
1005 */
1006 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1007 max_palette_length = (1 << png_ptr->bit_depth);
1008 else
1009 max_palette_length = PNG_MAX_PALETTE_LENGTH;
1010
1011 if (num > max_palette_length)
1012 num = max_palette_length;
1013
Patrick Scott5f6bd842010-06-28 16:55:16 -04001014#ifdef PNG_POINTER_INDEXING_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001015 for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)
1016 {
1017 png_byte buf[3];
1018
1019 png_crc_read(png_ptr, buf, 3);
1020 pal_ptr->red = buf[0];
1021 pal_ptr->green = buf[1];
1022 pal_ptr->blue = buf[2];
1023 }
1024#else
1025 for (i = 0; i < num; i++)
1026 {
1027 png_byte buf[3];
1028
1029 png_crc_read(png_ptr, buf, 3);
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001030 /* Don't depend upon png_color being any order */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001031 palette[i].red = buf[0];
1032 palette[i].green = buf[1];
1033 palette[i].blue = buf[2];
1034 }
1035#endif
1036
Chris Craikb50c2172013-07-29 15:28:30 -07001037 /* If we actually need the PLTE chunk (ie for a paletted image), we do
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001038 * whatever the normal CRC configuration tells us. However, if we
1039 * have an RGB image, the PLTE can be considered ancillary, so
1040 * we will act as though it is.
1041 */
Patrick Scott5f6bd842010-06-28 16:55:16 -04001042#ifndef PNG_READ_OPT_PLTE_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001043 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1044#endif
1045 {
Matt Sarett96be9082016-05-03 13:29:54 -04001046 png_crc_finish(png_ptr, (int) length - num * 3);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001047 }
Chris Craikb50c2172013-07-29 15:28:30 -07001048
Patrick Scott5f6bd842010-06-28 16:55:16 -04001049#ifndef PNG_READ_OPT_PLTE_SUPPORTED
Matt Sarett96be9082016-05-03 13:29:54 -04001050 else if (png_crc_error(png_ptr) != 0) /* Only if we have a CRC error */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001051 {
1052 /* If we don't want to use the data from an ancillary chunk,
Chris Craikb50c2172013-07-29 15:28:30 -07001053 * we have two options: an error abort, or a warning and we
1054 * ignore the data in this chunk (which should be OK, since
1055 * it's considered ancillary for a RGB or RGBA image).
1056 *
1057 * IMPLEMENTATION NOTE: this is only here because png_crc_finish uses the
1058 * chunk type to determine whether to check the ancillary or the critical
1059 * flags.
1060 */
Matt Sarett96be9082016-05-03 13:29:54 -04001061 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE) == 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001062 {
Matt Sarett96be9082016-05-03 13:29:54 -04001063 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301064 return;
Chris Craikb50c2172013-07-29 15:28:30 -07001065
The Android Open Source Project893912b2009-03-03 19:30:05 -08001066 else
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301067 png_chunk_error(png_ptr, "CRC error");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001068 }
Chris Craikb50c2172013-07-29 15:28:30 -07001069
The Android Open Source Project893912b2009-03-03 19:30:05 -08001070 /* Otherwise, we (optionally) emit a warning and use the chunk. */
Matt Sarett96be9082016-05-03 13:29:54 -04001071 else if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) == 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001072 png_chunk_warning(png_ptr, "CRC error");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001073 }
1074#endif
1075
Chris Craikb50c2172013-07-29 15:28:30 -07001076 /* TODO: png_set_PLTE has the side effect of setting png_ptr->palette to its
1077 * own copy of the palette. This has the side effect that when png_start_row
1078 * is called (this happens after any call to png_read_update_info) the
1079 * info_ptr palette gets changed. This is extremely unexpected and
1080 * confusing.
1081 *
1082 * Fix this by not sharing the palette in this way.
1083 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001084 png_set_PLTE(png_ptr, info_ptr, palette, num);
1085
Chris Craikb50c2172013-07-29 15:28:30 -07001086 /* The three chunks, bKGD, hIST and tRNS *must* appear after PLTE and before
1087 * IDAT. Prior to 1.6.0 this was not checked; instead the code merely
1088 * checked the apparent validity of a tRNS chunk inserted before PLTE on a
1089 * palette PNG. 1.6.0 attempts to rigorously follow the standard and
1090 * therefore does a benign error if the erroneous condition is detected *and*
1091 * cancels the tRNS if the benign error returns. The alternative is to
1092 * amend the standard since it would be rather hypocritical of the standards
1093 * maintainers to ignore it.
1094 */
Patrick Scott5f6bd842010-06-28 16:55:16 -04001095#ifdef PNG_READ_tRNS_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001096 if (png_ptr->num_trans > 0 ||
Matt Sarett96be9082016-05-03 13:29:54 -04001097 (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS) != 0))
The Android Open Source Project893912b2009-03-03 19:30:05 -08001098 {
Chris Craikb50c2172013-07-29 15:28:30 -07001099 /* Cancel this because otherwise it would be used if the transforms
1100 * require it. Don't cancel the 'valid' flag because this would prevent
1101 * detection of duplicate chunks.
1102 */
1103 png_ptr->num_trans = 0;
1104
1105 if (info_ptr != NULL)
1106 info_ptr->num_trans = 0;
1107
1108 png_chunk_benign_error(png_ptr, "tRNS must be after");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001109 }
1110#endif
1111
Chris Craikb50c2172013-07-29 15:28:30 -07001112#ifdef PNG_READ_hIST_SUPPORTED
1113 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) != 0)
1114 png_chunk_benign_error(png_ptr, "hIST must be after");
1115#endif
1116
1117#ifdef PNG_READ_bKGD_SUPPORTED
1118 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) != 0)
1119 png_chunk_benign_error(png_ptr, "bKGD must be after");
1120#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -08001121}
1122
1123void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07001124png_handle_IEND(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001125{
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001126 png_debug(1, "in png_handle_IEND");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001127
Matt Sarett96be9082016-05-03 13:29:54 -04001128 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0 ||
1129 (png_ptr->mode & PNG_HAVE_IDAT) == 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001130 png_chunk_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001131
1132 png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND);
1133
The Android Open Source Project893912b2009-03-03 19:30:05 -08001134 png_crc_finish(png_ptr, length);
1135
Chris Craikb50c2172013-07-29 15:28:30 -07001136 if (length != 0)
1137 png_chunk_benign_error(png_ptr, "invalid");
1138
1139 PNG_UNUSED(info_ptr)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001140}
1141
Patrick Scott5f6bd842010-06-28 16:55:16 -04001142#ifdef PNG_READ_gAMA_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001143void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07001144png_handle_gAMA(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001145{
1146 png_fixed_point igamma;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001147 png_byte buf[4];
1148
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001149 png_debug(1, "in png_handle_gAMA");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001150
Matt Sarett96be9082016-05-03 13:29:54 -04001151 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001152 png_chunk_error(png_ptr, "missing IHDR");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001153
Matt Sarett96be9082016-05-03 13:29:54 -04001154 else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001155 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001156 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001157 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001158 return;
1159 }
1160
1161 if (length != 4)
1162 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001163 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001164 png_chunk_benign_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001165 return;
1166 }
1167
1168 png_crc_read(png_ptr, buf, 4);
Chris Craikb50c2172013-07-29 15:28:30 -07001169
Matt Sarett96be9082016-05-03 13:29:54 -04001170 if (png_crc_finish(png_ptr, 0) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001171 return;
1172
Chris Craikb50c2172013-07-29 15:28:30 -07001173 igamma = png_get_fixed_point(NULL, buf);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001174
Chris Craikb50c2172013-07-29 15:28:30 -07001175 png_colorspace_set_gamma(png_ptr, &png_ptr->colorspace, igamma);
1176 png_colorspace_sync(png_ptr, info_ptr);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001177}
1178#endif
1179
Patrick Scott5f6bd842010-06-28 16:55:16 -04001180#ifdef PNG_READ_sBIT_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001181void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07001182png_handle_sBIT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001183{
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301184 unsigned int truelen, i;
1185 png_byte sample_depth;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001186 png_byte buf[4];
1187
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001188 png_debug(1, "in png_handle_sBIT");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001189
Matt Sarett96be9082016-05-03 13:29:54 -04001190 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001191 png_chunk_error(png_ptr, "missing IHDR");
1192
Matt Sarett96be9082016-05-03 13:29:54 -04001193 else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001194 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001195 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001196 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001197 return;
1198 }
Chris Craikb50c2172013-07-29 15:28:30 -07001199
Matt Sarett96be9082016-05-03 13:29:54 -04001200 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001201 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001202 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001203 png_chunk_benign_error(png_ptr, "duplicate");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001204 return;
1205 }
1206
1207 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301208 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001209 truelen = 3;
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301210 sample_depth = 8;
1211 }
Chris Craikb50c2172013-07-29 15:28:30 -07001212
The Android Open Source Project893912b2009-03-03 19:30:05 -08001213 else
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301214 {
Chris Craikb50c2172013-07-29 15:28:30 -07001215 truelen = png_ptr->channels;
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301216 sample_depth = png_ptr->bit_depth;
1217 }
The Android Open Source Project893912b2009-03-03 19:30:05 -08001218
1219 if (length != truelen || length > 4)
1220 {
Chris Craikb50c2172013-07-29 15:28:30 -07001221 png_chunk_benign_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001222 png_crc_finish(png_ptr, length);
1223 return;
1224 }
1225
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301226 buf[0] = buf[1] = buf[2] = buf[3] = sample_depth;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001227 png_crc_read(png_ptr, buf, truelen);
Chris Craikb50c2172013-07-29 15:28:30 -07001228
Matt Sarett96be9082016-05-03 13:29:54 -04001229 if (png_crc_finish(png_ptr, 0) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001230 return;
1231
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301232 for (i=0; i<truelen; ++i)
Matt Sarett96be9082016-05-03 13:29:54 -04001233 {
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301234 if (buf[i] == 0 || buf[i] > sample_depth)
1235 {
1236 png_chunk_benign_error(png_ptr, "invalid");
1237 return;
1238 }
Matt Sarett96be9082016-05-03 13:29:54 -04001239 }
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301240
Matt Sarett96be9082016-05-03 13:29:54 -04001241 if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001242 {
1243 png_ptr->sig_bit.red = buf[0];
1244 png_ptr->sig_bit.green = buf[1];
1245 png_ptr->sig_bit.blue = buf[2];
1246 png_ptr->sig_bit.alpha = buf[3];
1247 }
Chris Craikb50c2172013-07-29 15:28:30 -07001248
The Android Open Source Project893912b2009-03-03 19:30:05 -08001249 else
1250 {
1251 png_ptr->sig_bit.gray = buf[0];
1252 png_ptr->sig_bit.red = buf[0];
1253 png_ptr->sig_bit.green = buf[0];
1254 png_ptr->sig_bit.blue = buf[0];
1255 png_ptr->sig_bit.alpha = buf[1];
1256 }
Chris Craikb50c2172013-07-29 15:28:30 -07001257
The Android Open Source Project893912b2009-03-03 19:30:05 -08001258 png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
1259}
1260#endif
1261
Patrick Scott5f6bd842010-06-28 16:55:16 -04001262#ifdef PNG_READ_cHRM_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001263void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07001264png_handle_cHRM(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001265{
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001266 png_byte buf[32];
Chris Craikb50c2172013-07-29 15:28:30 -07001267 png_xy xy;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001268
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001269 png_debug(1, "in png_handle_cHRM");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001270
Matt Sarett96be9082016-05-03 13:29:54 -04001271 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001272 png_chunk_error(png_ptr, "missing IHDR");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001273
Matt Sarett96be9082016-05-03 13:29:54 -04001274 else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001275 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001276 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001277 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001278 return;
1279 }
1280
1281 if (length != 32)
1282 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001283 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001284 png_chunk_benign_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001285 return;
1286 }
1287
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001288 png_crc_read(png_ptr, buf, 32);
Chris Craikb50c2172013-07-29 15:28:30 -07001289
Matt Sarett96be9082016-05-03 13:29:54 -04001290 if (png_crc_finish(png_ptr, 0) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001291 return;
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001292
Chris Craikb50c2172013-07-29 15:28:30 -07001293 xy.whitex = png_get_fixed_point(NULL, buf);
1294 xy.whitey = png_get_fixed_point(NULL, buf + 4);
1295 xy.redx = png_get_fixed_point(NULL, buf + 8);
1296 xy.redy = png_get_fixed_point(NULL, buf + 12);
1297 xy.greenx = png_get_fixed_point(NULL, buf + 16);
1298 xy.greeny = png_get_fixed_point(NULL, buf + 20);
1299 xy.bluex = png_get_fixed_point(NULL, buf + 24);
1300 xy.bluey = png_get_fixed_point(NULL, buf + 28);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001301
Chris Craikb50c2172013-07-29 15:28:30 -07001302 if (xy.whitex == PNG_FIXED_ERROR ||
1303 xy.whitey == PNG_FIXED_ERROR ||
1304 xy.redx == PNG_FIXED_ERROR ||
1305 xy.redy == PNG_FIXED_ERROR ||
1306 xy.greenx == PNG_FIXED_ERROR ||
1307 xy.greeny == PNG_FIXED_ERROR ||
1308 xy.bluex == PNG_FIXED_ERROR ||
1309 xy.bluey == PNG_FIXED_ERROR)
1310 {
1311 png_chunk_benign_error(png_ptr, "invalid values");
1312 return;
1313 }
The Android Open Source Project893912b2009-03-03 19:30:05 -08001314
Chris Craikb50c2172013-07-29 15:28:30 -07001315 /* If a colorspace error has already been output skip this chunk */
Matt Sarett96be9082016-05-03 13:29:54 -04001316 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001317 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001318
Matt Sarett96be9082016-05-03 13:29:54 -04001319 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001320 {
1321 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1322 png_colorspace_sync(png_ptr, info_ptr);
1323 png_chunk_benign_error(png_ptr, "duplicate");
1324 return;
1325 }
The Android Open Source Project893912b2009-03-03 19:30:05 -08001326
Chris Craikb50c2172013-07-29 15:28:30 -07001327 png_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM;
1328 (void)png_colorspace_set_chromaticities(png_ptr, &png_ptr->colorspace, &xy,
1329 1/*prefer cHRM values*/);
1330 png_colorspace_sync(png_ptr, info_ptr);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001331}
1332#endif
1333
Patrick Scott5f6bd842010-06-28 16:55:16 -04001334#ifdef PNG_READ_sRGB_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001335void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07001336png_handle_sRGB(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001337{
Chris Craikb50c2172013-07-29 15:28:30 -07001338 png_byte intent;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001339
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001340 png_debug(1, "in png_handle_sRGB");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001341
Matt Sarett96be9082016-05-03 13:29:54 -04001342 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001343 png_chunk_error(png_ptr, "missing IHDR");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001344
Matt Sarett96be9082016-05-03 13:29:54 -04001345 else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001346 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001347 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001348 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001349 return;
1350 }
1351
1352 if (length != 1)
1353 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001354 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001355 png_chunk_benign_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001356 return;
1357 }
1358
Chris Craikb50c2172013-07-29 15:28:30 -07001359 png_crc_read(png_ptr, &intent, 1);
1360
Matt Sarett96be9082016-05-03 13:29:54 -04001361 if (png_crc_finish(png_ptr, 0) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001362 return;
1363
Chris Craikb50c2172013-07-29 15:28:30 -07001364 /* If a colorspace error has already been output skip this chunk */
Matt Sarett96be9082016-05-03 13:29:54 -04001365 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001366 return;
1367
1368 /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect
1369 * this.
1370 */
Matt Sarett96be9082016-05-03 13:29:54 -04001371 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001372 {
Chris Craikb50c2172013-07-29 15:28:30 -07001373 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1374 png_colorspace_sync(png_ptr, info_ptr);
1375 png_chunk_benign_error(png_ptr, "too many profiles");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001376 return;
1377 }
1378
Chris Craikb50c2172013-07-29 15:28:30 -07001379 (void)png_colorspace_set_sRGB(png_ptr, &png_ptr->colorspace, intent);
1380 png_colorspace_sync(png_ptr, info_ptr);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001381}
Matt Sarett96be9082016-05-03 13:29:54 -04001382#endif /* READ_sRGB */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001383
Patrick Scott5f6bd842010-06-28 16:55:16 -04001384#ifdef PNG_READ_iCCP_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001385void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07001386png_handle_iCCP(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1387/* Note: this does not properly handle profiles that are > 64K under DOS */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001388{
Chris Craikb50c2172013-07-29 15:28:30 -07001389 png_const_charp errmsg = NULL; /* error message output, or no error */
1390 int finished = 0; /* crc checked */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001391
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001392 png_debug(1, "in png_handle_iCCP");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001393
Matt Sarett96be9082016-05-03 13:29:54 -04001394 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001395 png_chunk_error(png_ptr, "missing IHDR");
1396
Matt Sarett96be9082016-05-03 13:29:54 -04001397 else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001398 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001399 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001400 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001401 return;
1402 }
1403
Chris Craikb50c2172013-07-29 15:28:30 -07001404 /* Consistent with all the above colorspace handling an obviously *invalid*
1405 * chunk is just ignored, so does not invalidate the color space. An
1406 * alternative is to set the 'invalid' flags at the start of this routine
1407 * and only clear them in they were not set before and all the tests pass.
Matt Sarett96be9082016-05-03 13:29:54 -04001408 * The minimum 'deflate' stream is assumed to be just the 2 byte header and
1409 * 4 byte checksum. The keyword must be at least one character and there is
1410 * a terminator (0) byte and the compression method.
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001411 */
Chris Craikb50c2172013-07-29 15:28:30 -07001412 if (length < 9)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001413 {
Chris Craikb50c2172013-07-29 15:28:30 -07001414 png_crc_finish(png_ptr, length);
1415 png_chunk_benign_error(png_ptr, "too short");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001416 return;
1417 }
1418
Chris Craikb50c2172013-07-29 15:28:30 -07001419 /* If a colorspace error has already been output skip this chunk */
Matt Sarett96be9082016-05-03 13:29:54 -04001420 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001421 {
Chris Craikb50c2172013-07-29 15:28:30 -07001422 png_crc_finish(png_ptr, length);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001423 return;
1424 }
1425
Chris Craikb50c2172013-07-29 15:28:30 -07001426 /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect
1427 * this.
1428 */
1429 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) == 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001430 {
Chris Craikb50c2172013-07-29 15:28:30 -07001431 uInt read_length, keyword_length;
1432 char keyword[81];
1433
1434 /* Find the keyword; the keyword plus separator and compression method
1435 * bytes can be at most 81 characters long.
1436 */
1437 read_length = 81; /* maximum */
1438 if (read_length > length)
1439 read_length = (uInt)length;
1440
1441 png_crc_read(png_ptr, (png_bytep)keyword, read_length);
1442 length -= read_length;
1443
1444 keyword_length = 0;
1445 while (keyword_length < 80 && keyword_length < read_length &&
1446 keyword[keyword_length] != 0)
1447 ++keyword_length;
1448
1449 /* TODO: make the keyword checking common */
1450 if (keyword_length >= 1 && keyword_length <= 79)
1451 {
1452 /* We only understand '0' compression - deflate - so if we get a
1453 * different value we can't safely decode the chunk.
1454 */
1455 if (keyword_length+1 < read_length &&
1456 keyword[keyword_length+1] == PNG_COMPRESSION_TYPE_BASE)
1457 {
1458 read_length -= keyword_length+2;
1459
1460 if (png_inflate_claim(png_ptr, png_iCCP) == Z_OK)
1461 {
1462 Byte profile_header[132];
1463 Byte local_buffer[PNG_INFLATE_BUF_SIZE];
1464 png_alloc_size_t size = (sizeof profile_header);
1465
1466 png_ptr->zstream.next_in = (Bytef*)keyword + (keyword_length+2);
1467 png_ptr->zstream.avail_in = read_length;
1468 (void)png_inflate_read(png_ptr, local_buffer,
1469 (sizeof local_buffer), &length, profile_header, &size,
1470 0/*finish: don't, because the output is too small*/);
1471
1472 if (size == 0)
1473 {
1474 /* We have the ICC profile header; do the basic header checks.
1475 */
1476 const png_uint_32 profile_length =
1477 png_get_uint_32(profile_header);
1478
1479 if (png_icc_check_length(png_ptr, &png_ptr->colorspace,
Matt Sarett96be9082016-05-03 13:29:54 -04001480 keyword, profile_length) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001481 {
1482 /* The length is apparently ok, so we can check the 132
1483 * byte header.
1484 */
1485 if (png_icc_check_header(png_ptr, &png_ptr->colorspace,
1486 keyword, profile_length, profile_header,
Matt Sarett96be9082016-05-03 13:29:54 -04001487 png_ptr->color_type) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001488 {
1489 /* Now read the tag table; a variable size buffer is
1490 * needed at this point, allocate one for the whole
1491 * profile. The header check has already validated
1492 * that none of these stuff will overflow.
1493 */
1494 const png_uint_32 tag_count = png_get_uint_32(
1495 profile_header+128);
1496 png_bytep profile = png_read_buffer(png_ptr,
1497 profile_length, 2/*silent*/);
1498
1499 if (profile != NULL)
1500 {
1501 memcpy(profile, profile_header,
1502 (sizeof profile_header));
1503
1504 size = 12 * tag_count;
1505
1506 (void)png_inflate_read(png_ptr, local_buffer,
1507 (sizeof local_buffer), &length,
1508 profile + (sizeof profile_header), &size, 0);
1509
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301510 /* Still expect a buffer error because we expect
Chris Craikb50c2172013-07-29 15:28:30 -07001511 * there to be some tag data!
1512 */
1513 if (size == 0)
1514 {
1515 if (png_icc_check_tag_table(png_ptr,
1516 &png_ptr->colorspace, keyword, profile_length,
Matt Sarett96be9082016-05-03 13:29:54 -04001517 profile) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001518 {
1519 /* The profile has been validated for basic
1520 * security issues, so read the whole thing in.
1521 */
1522 size = profile_length - (sizeof profile_header)
1523 - 12 * tag_count;
1524
1525 (void)png_inflate_read(png_ptr, local_buffer,
1526 (sizeof local_buffer), &length,
1527 profile + (sizeof profile_header) +
1528 12 * tag_count, &size, 1/*finish*/);
1529
1530 if (length > 0 && !(png_ptr->flags &
1531 PNG_FLAG_BENIGN_ERRORS_WARN))
1532 errmsg = "extra compressed data";
1533
1534 /* But otherwise allow extra data: */
1535 else if (size == 0)
1536 {
1537 if (length > 0)
1538 {
1539 /* This can be handled completely, so
1540 * keep going.
1541 */
1542 png_chunk_warning(png_ptr,
1543 "extra compressed data");
1544 }
1545
1546 png_crc_finish(png_ptr, length);
1547 finished = 1;
1548
1549# ifdef PNG_sRGB_SUPPORTED
Matt Sarett96be9082016-05-03 13:29:54 -04001550 /* Check for a match against sRGB */
1551 png_icc_set_sRGB(png_ptr,
1552 &png_ptr->colorspace, profile,
1553 png_ptr->zstream.adler);
Chris Craikb50c2172013-07-29 15:28:30 -07001554# endif
1555
1556 /* Steal the profile for info_ptr. */
1557 if (info_ptr != NULL)
1558 {
1559 png_free_data(png_ptr, info_ptr,
1560 PNG_FREE_ICCP, 0);
1561
1562 info_ptr->iccp_name = png_voidcast(char*,
1563 png_malloc_base(png_ptr,
1564 keyword_length+1));
1565 if (info_ptr->iccp_name != NULL)
1566 {
1567 memcpy(info_ptr->iccp_name, keyword,
1568 keyword_length+1);
1569 info_ptr->iccp_proflen =
1570 profile_length;
1571 info_ptr->iccp_profile = profile;
1572 png_ptr->read_buffer = NULL; /*steal*/
1573 info_ptr->free_me |= PNG_FREE_ICCP;
1574 info_ptr->valid |= PNG_INFO_iCCP;
1575 }
1576
1577 else
1578 {
1579 png_ptr->colorspace.flags |=
1580 PNG_COLORSPACE_INVALID;
1581 errmsg = "out of memory";
1582 }
1583 }
1584
1585 /* else the profile remains in the read
1586 * buffer which gets reused for subsequent
1587 * chunks.
1588 */
1589
1590 if (info_ptr != NULL)
1591 png_colorspace_sync(png_ptr, info_ptr);
1592
1593 if (errmsg == NULL)
1594 {
1595 png_ptr->zowner = 0;
1596 return;
1597 }
1598 }
1599
1600 else if (size > 0)
1601 errmsg = "truncated";
1602
Matt Sarett96be9082016-05-03 13:29:54 -04001603#ifndef __COVERITY__
Chris Craikb50c2172013-07-29 15:28:30 -07001604 else
1605 errmsg = png_ptr->zstream.msg;
Matt Sarett96be9082016-05-03 13:29:54 -04001606#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001607 }
1608
1609 /* else png_icc_check_tag_table output an error */
1610 }
1611
1612 else /* profile truncated */
1613 errmsg = png_ptr->zstream.msg;
1614 }
1615
1616 else
1617 errmsg = "out of memory";
1618 }
1619
1620 /* else png_icc_check_header output an error */
1621 }
1622
1623 /* else png_icc_check_length output an error */
1624 }
1625
1626 else /* profile truncated */
1627 errmsg = png_ptr->zstream.msg;
1628
1629 /* Release the stream */
1630 png_ptr->zowner = 0;
1631 }
1632
1633 else /* png_inflate_claim failed */
1634 errmsg = png_ptr->zstream.msg;
1635 }
1636
1637 else
1638 errmsg = "bad compression method"; /* or missing */
1639 }
1640
1641 else
1642 errmsg = "bad keyword";
The Android Open Source Project893912b2009-03-03 19:30:05 -08001643 }
1644
Chris Craikb50c2172013-07-29 15:28:30 -07001645 else
1646 errmsg = "too many profiles";
1647
1648 /* Failure: the reason is in 'errmsg' */
Matt Sarett96be9082016-05-03 13:29:54 -04001649 if (finished == 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001650 png_crc_finish(png_ptr, length);
1651
1652 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1653 png_colorspace_sync(png_ptr, info_ptr);
1654 if (errmsg != NULL) /* else already output */
1655 png_chunk_benign_error(png_ptr, errmsg);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001656}
Matt Sarett96be9082016-05-03 13:29:54 -04001657#endif /* READ_iCCP */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001658
Patrick Scott5f6bd842010-06-28 16:55:16 -04001659#ifdef PNG_READ_sPLT_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001660void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07001661png_handle_sPLT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001662/* Note: this does not properly handle chunks that are > 64K under DOS */
1663{
Chris Craikb50c2172013-07-29 15:28:30 -07001664 png_bytep entry_start, buffer;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001665 png_sPLT_t new_palette;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001666 png_sPLT_entryp pp;
Chris Craikb50c2172013-07-29 15:28:30 -07001667 png_uint_32 data_length;
1668 int entry_size, i;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001669 png_uint_32 skip = 0;
Chris Craikb50c2172013-07-29 15:28:30 -07001670 png_uint_32 dl;
1671 png_size_t max_dl;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001672
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001673 png_debug(1, "in png_handle_sPLT");
1674
Patrick Scott5f6bd842010-06-28 16:55:16 -04001675#ifdef PNG_USER_LIMITS_SUPPORTED
Patrick Scott5f6bd842010-06-28 16:55:16 -04001676 if (png_ptr->user_chunk_cache_max != 0)
1677 {
1678 if (png_ptr->user_chunk_cache_max == 1)
1679 {
1680 png_crc_finish(png_ptr, length);
1681 return;
1682 }
Chris Craikb50c2172013-07-29 15:28:30 -07001683
Patrick Scott5f6bd842010-06-28 16:55:16 -04001684 if (--png_ptr->user_chunk_cache_max == 1)
1685 {
1686 png_warning(png_ptr, "No space in chunk cache for sPLT");
1687 png_crc_finish(png_ptr, length);
1688 return;
1689 }
1690 }
1691#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -08001692
Matt Sarett96be9082016-05-03 13:29:54 -04001693 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001694 png_chunk_error(png_ptr, "missing IHDR");
1695
Matt Sarett96be9082016-05-03 13:29:54 -04001696 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001697 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001698 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001699 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001700 return;
1701 }
1702
1703#ifdef PNG_MAX_MALLOC_64K
Chris Craikb50c2172013-07-29 15:28:30 -07001704 if (length > 65535U)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001705 {
Chris Craikb50c2172013-07-29 15:28:30 -07001706 png_crc_finish(png_ptr, length);
1707 png_chunk_benign_error(png_ptr, "too large to fit in memory");
1708 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001709 }
1710#endif
1711
Chris Craikb50c2172013-07-29 15:28:30 -07001712 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
1713 if (buffer == NULL)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001714 {
Chris Craikb50c2172013-07-29 15:28:30 -07001715 png_crc_finish(png_ptr, length);
1716 png_chunk_benign_error(png_ptr, "out of memory");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001717 return;
1718 }
1719
The Android Open Source Project893912b2009-03-03 19:30:05 -08001720
Chris Craikb50c2172013-07-29 15:28:30 -07001721 /* WARNING: this may break if size_t is less than 32 bits; it is assumed
1722 * that the PNG_MAX_MALLOC_64K test is enabled in this case, but this is a
1723 * potential breakage point if the types in pngconf.h aren't exactly right.
1724 */
1725 png_crc_read(png_ptr, buffer, length);
1726
Matt Sarett96be9082016-05-03 13:29:54 -04001727 if (png_crc_finish(png_ptr, skip) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001728 return;
1729
1730 buffer[length] = 0;
1731
1732 for (entry_start = buffer; *entry_start; entry_start++)
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001733 /* Empty loop to find end of name */ ;
Chris Craikb50c2172013-07-29 15:28:30 -07001734
The Android Open Source Project893912b2009-03-03 19:30:05 -08001735 ++entry_start;
1736
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001737 /* A sample depth should follow the separator, and we should be on it */
Matt Sarett96be9082016-05-03 13:29:54 -04001738 if (length < 2U || entry_start > buffer + (length - 2U))
The Android Open Source Project893912b2009-03-03 19:30:05 -08001739 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001740 png_warning(png_ptr, "malformed sPLT chunk");
1741 return;
1742 }
1743
1744 new_palette.depth = *entry_start++;
1745 entry_size = (new_palette.depth == 8 ? 6 : 10);
Chris Craikb50c2172013-07-29 15:28:30 -07001746 /* This must fit in a png_uint_32 because it is derived from the original
1747 * chunk data length.
1748 */
1749 data_length = length - (png_uint_32)(entry_start - buffer);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001750
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001751 /* Integrity-check the data length */
Matt Sarett96be9082016-05-03 13:29:54 -04001752 if ((data_length % entry_size) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001753 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001754 png_warning(png_ptr, "sPLT chunk has bad length");
1755 return;
1756 }
1757
Chris Craikb50c2172013-07-29 15:28:30 -07001758 dl = (png_int_32)(data_length / entry_size);
1759 max_dl = PNG_SIZE_MAX / (sizeof (png_sPLT_entry));
1760
1761 if (dl > max_dl)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001762 {
Matt Sarett96be9082016-05-03 13:29:54 -04001763 png_warning(png_ptr, "sPLT chunk too long");
1764 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001765 }
Chris Craikb50c2172013-07-29 15:28:30 -07001766
1767 new_palette.nentries = (png_int_32)(data_length / entry_size);
1768
The Android Open Source Project893912b2009-03-03 19:30:05 -08001769 new_palette.entries = (png_sPLT_entryp)png_malloc_warn(
Chris Craikb50c2172013-07-29 15:28:30 -07001770 png_ptr, new_palette.nentries * (sizeof (png_sPLT_entry)));
1771
The Android Open Source Project893912b2009-03-03 19:30:05 -08001772 if (new_palette.entries == NULL)
1773 {
Matt Sarett96be9082016-05-03 13:29:54 -04001774 png_warning(png_ptr, "sPLT chunk requires too much memory");
1775 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001776 }
1777
Patrick Scott5f6bd842010-06-28 16:55:16 -04001778#ifdef PNG_POINTER_INDEXING_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001779 for (i = 0; i < new_palette.nentries; i++)
1780 {
Patrick Scott5f6bd842010-06-28 16:55:16 -04001781 pp = new_palette.entries + i;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001782
1783 if (new_palette.depth == 8)
1784 {
Chris Craikb50c2172013-07-29 15:28:30 -07001785 pp->red = *entry_start++;
1786 pp->green = *entry_start++;
1787 pp->blue = *entry_start++;
1788 pp->alpha = *entry_start++;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001789 }
Chris Craikb50c2172013-07-29 15:28:30 -07001790
The Android Open Source Project893912b2009-03-03 19:30:05 -08001791 else
1792 {
Chris Craikb50c2172013-07-29 15:28:30 -07001793 pp->red = png_get_uint_16(entry_start); entry_start += 2;
1794 pp->green = png_get_uint_16(entry_start); entry_start += 2;
1795 pp->blue = png_get_uint_16(entry_start); entry_start += 2;
1796 pp->alpha = png_get_uint_16(entry_start); entry_start += 2;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001797 }
Chris Craikb50c2172013-07-29 15:28:30 -07001798
The Android Open Source Project893912b2009-03-03 19:30:05 -08001799 pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
1800 }
1801#else
1802 pp = new_palette.entries;
Chris Craikb50c2172013-07-29 15:28:30 -07001803
The Android Open Source Project893912b2009-03-03 19:30:05 -08001804 for (i = 0; i < new_palette.nentries; i++)
1805 {
1806
1807 if (new_palette.depth == 8)
1808 {
Chris Craikb50c2172013-07-29 15:28:30 -07001809 pp[i].red = *entry_start++;
1810 pp[i].green = *entry_start++;
1811 pp[i].blue = *entry_start++;
1812 pp[i].alpha = *entry_start++;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001813 }
Chris Craikb50c2172013-07-29 15:28:30 -07001814
The Android Open Source Project893912b2009-03-03 19:30:05 -08001815 else
1816 {
Chris Craikb50c2172013-07-29 15:28:30 -07001817 pp[i].red = png_get_uint_16(entry_start); entry_start += 2;
1818 pp[i].green = png_get_uint_16(entry_start); entry_start += 2;
1819 pp[i].blue = png_get_uint_16(entry_start); entry_start += 2;
1820 pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001821 }
Chris Craikb50c2172013-07-29 15:28:30 -07001822
1823 pp[i].frequency = png_get_uint_16(entry_start); entry_start += 2;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001824 }
1825#endif
1826
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001827 /* Discard all chunk data except the name and stash that */
Chris Craikb50c2172013-07-29 15:28:30 -07001828 new_palette.name = (png_charp)buffer;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001829
1830 png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);
1831
The Android Open Source Project893912b2009-03-03 19:30:05 -08001832 png_free(png_ptr, new_palette.entries);
1833}
Matt Sarett96be9082016-05-03 13:29:54 -04001834#endif /* READ_sPLT */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001835
Patrick Scott5f6bd842010-06-28 16:55:16 -04001836#ifdef PNG_READ_tRNS_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001837void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07001838png_handle_tRNS(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001839{
1840 png_byte readbuf[PNG_MAX_PALETTE_LENGTH];
1841
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001842 png_debug(1, "in png_handle_tRNS");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001843
Matt Sarett96be9082016-05-03 13:29:54 -04001844 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001845 png_chunk_error(png_ptr, "missing IHDR");
1846
Matt Sarett96be9082016-05-03 13:29:54 -04001847 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001848 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001849 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001850 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001851 return;
1852 }
Chris Craikb50c2172013-07-29 15:28:30 -07001853
Matt Sarett96be9082016-05-03 13:29:54 -04001854 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001855 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001856 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001857 png_chunk_benign_error(png_ptr, "duplicate");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001858 return;
1859 }
1860
1861 if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
1862 {
1863 png_byte buf[2];
1864
1865 if (length != 2)
1866 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001867 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001868 png_chunk_benign_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001869 return;
1870 }
1871
1872 png_crc_read(png_ptr, buf, 2);
1873 png_ptr->num_trans = 1;
Chris Craikb50c2172013-07-29 15:28:30 -07001874 png_ptr->trans_color.gray = png_get_uint_16(buf);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001875 }
Chris Craikb50c2172013-07-29 15:28:30 -07001876
The Android Open Source Project893912b2009-03-03 19:30:05 -08001877 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
1878 {
1879 png_byte buf[6];
1880
1881 if (length != 6)
1882 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001883 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001884 png_chunk_benign_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001885 return;
1886 }
Chris Craikb50c2172013-07-29 15:28:30 -07001887
1888 png_crc_read(png_ptr, buf, length);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001889 png_ptr->num_trans = 1;
Chris Craikb50c2172013-07-29 15:28:30 -07001890 png_ptr->trans_color.red = png_get_uint_16(buf);
1891 png_ptr->trans_color.green = png_get_uint_16(buf + 2);
1892 png_ptr->trans_color.blue = png_get_uint_16(buf + 4);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001893 }
Chris Craikb50c2172013-07-29 15:28:30 -07001894
The Android Open Source Project893912b2009-03-03 19:30:05 -08001895 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1896 {
Matt Sarett96be9082016-05-03 13:29:54 -04001897 if ((png_ptr->mode & PNG_HAVE_PLTE) == 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001898 {
Chris Craikb50c2172013-07-29 15:28:30 -07001899 /* TODO: is this actually an error in the ISO spec? */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001900 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001901 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001902 return;
1903 }
Chris Craikb50c2172013-07-29 15:28:30 -07001904
Matt Sarett96be9082016-05-03 13:29:54 -04001905 if (length > (unsigned int) png_ptr->num_palette ||
1906 length > (unsigned int) PNG_MAX_PALETTE_LENGTH ||
Chris Craikb50c2172013-07-29 15:28:30 -07001907 length == 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001908 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001909 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001910 png_chunk_benign_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001911 return;
1912 }
Chris Craikb50c2172013-07-29 15:28:30 -07001913
1914 png_crc_read(png_ptr, readbuf, length);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001915 png_ptr->num_trans = (png_uint_16)length;
1916 }
Chris Craikb50c2172013-07-29 15:28:30 -07001917
The Android Open Source Project893912b2009-03-03 19:30:05 -08001918 else
1919 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001920 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001921 png_chunk_benign_error(png_ptr, "invalid with alpha channel");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001922 return;
1923 }
1924
Matt Sarett96be9082016-05-03 13:29:54 -04001925 if (png_crc_finish(png_ptr, 0) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001926 {
1927 png_ptr->num_trans = 0;
1928 return;
1929 }
1930
Chris Craikb50c2172013-07-29 15:28:30 -07001931 /* TODO: this is a horrible side effect in the palette case because the
1932 * png_struct ends up with a pointer to the tRNS buffer owned by the
1933 * png_info. Fix this.
1934 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001935 png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,
Chris Craikb50c2172013-07-29 15:28:30 -07001936 &(png_ptr->trans_color));
The Android Open Source Project893912b2009-03-03 19:30:05 -08001937}
1938#endif
1939
Patrick Scott5f6bd842010-06-28 16:55:16 -04001940#ifdef PNG_READ_bKGD_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001941void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07001942png_handle_bKGD(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001943{
Chris Craikb50c2172013-07-29 15:28:30 -07001944 unsigned int truelen;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001945 png_byte buf[6];
Chris Craikb50c2172013-07-29 15:28:30 -07001946 png_color_16 background;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001947
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001948 png_debug(1, "in png_handle_bKGD");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001949
Matt Sarett96be9082016-05-03 13:29:54 -04001950 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001951 png_chunk_error(png_ptr, "missing IHDR");
1952
Matt Sarett96be9082016-05-03 13:29:54 -04001953 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0 ||
1954 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
1955 (png_ptr->mode & PNG_HAVE_PLTE) == 0))
The Android Open Source Project893912b2009-03-03 19:30:05 -08001956 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001957 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001958 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001959 return;
1960 }
Chris Craikb50c2172013-07-29 15:28:30 -07001961
Matt Sarett96be9082016-05-03 13:29:54 -04001962 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001963 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001964 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001965 png_chunk_benign_error(png_ptr, "duplicate");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001966 return;
1967 }
1968
1969 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1970 truelen = 1;
Chris Craikb50c2172013-07-29 15:28:30 -07001971
Matt Sarett96be9082016-05-03 13:29:54 -04001972 else if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001973 truelen = 6;
Chris Craikb50c2172013-07-29 15:28:30 -07001974
The Android Open Source Project893912b2009-03-03 19:30:05 -08001975 else
1976 truelen = 2;
1977
1978 if (length != truelen)
1979 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001980 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001981 png_chunk_benign_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001982 return;
1983 }
1984
1985 png_crc_read(png_ptr, buf, truelen);
Chris Craikb50c2172013-07-29 15:28:30 -07001986
Matt Sarett96be9082016-05-03 13:29:54 -04001987 if (png_crc_finish(png_ptr, 0) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001988 return;
1989
1990 /* We convert the index value into RGB components so that we can allow
1991 * arbitrary RGB values for background when we have transparency, and
1992 * so it is easy to determine the RGB values of the background color
Chris Craikb50c2172013-07-29 15:28:30 -07001993 * from the info_ptr struct.
1994 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001995 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1996 {
Chris Craikb50c2172013-07-29 15:28:30 -07001997 background.index = buf[0];
1998
Matt Sarett96be9082016-05-03 13:29:54 -04001999 if (info_ptr != NULL && info_ptr->num_palette != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002000 {
Chris Craikb50c2172013-07-29 15:28:30 -07002001 if (buf[0] >= info_ptr->num_palette)
2002 {
2003 png_chunk_benign_error(png_ptr, "invalid index");
2004 return;
2005 }
2006
2007 background.red = (png_uint_16)png_ptr->palette[buf[0]].red;
2008 background.green = (png_uint_16)png_ptr->palette[buf[0]].green;
2009 background.blue = (png_uint_16)png_ptr->palette[buf[0]].blue;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002010 }
Chris Craikb50c2172013-07-29 15:28:30 -07002011
2012 else
2013 background.red = background.green = background.blue = 0;
2014
2015 background.gray = 0;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002016 }
2017
Matt Sarett96be9082016-05-03 13:29:54 -04002018 else if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0) /* GRAY */
Chris Craikb50c2172013-07-29 15:28:30 -07002019 {
2020 background.index = 0;
2021 background.red =
2022 background.green =
2023 background.blue =
2024 background.gray = png_get_uint_16(buf);
2025 }
2026
2027 else
2028 {
2029 background.index = 0;
2030 background.red = png_get_uint_16(buf);
2031 background.green = png_get_uint_16(buf + 2);
2032 background.blue = png_get_uint_16(buf + 4);
2033 background.gray = 0;
2034 }
2035
2036 png_set_bKGD(png_ptr, info_ptr, &background);
The Android Open Source Project893912b2009-03-03 19:30:05 -08002037}
2038#endif
2039
Patrick Scott5f6bd842010-06-28 16:55:16 -04002040#ifdef PNG_READ_hIST_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08002041void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07002042png_handle_hIST(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002043{
2044 unsigned int num, i;
2045 png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];
2046
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002047 png_debug(1, "in png_handle_hIST");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002048
Matt Sarett96be9082016-05-03 13:29:54 -04002049 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
Chris Craikb50c2172013-07-29 15:28:30 -07002050 png_chunk_error(png_ptr, "missing IHDR");
2051
Matt Sarett96be9082016-05-03 13:29:54 -04002052 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0 ||
2053 (png_ptr->mode & PNG_HAVE_PLTE) == 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002054 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002055 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002056 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002057 return;
2058 }
Chris Craikb50c2172013-07-29 15:28:30 -07002059
Matt Sarett96be9082016-05-03 13:29:54 -04002060 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002061 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002062 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002063 png_chunk_benign_error(png_ptr, "duplicate");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002064 return;
2065 }
2066
2067 num = length / 2 ;
Chris Craikb50c2172013-07-29 15:28:30 -07002068
Matt Sarett96be9082016-05-03 13:29:54 -04002069 if (num != (unsigned int) png_ptr->num_palette ||
2070 num > (unsigned int) PNG_MAX_PALETTE_LENGTH)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002071 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002072 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002073 png_chunk_benign_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002074 return;
2075 }
2076
2077 for (i = 0; i < num; i++)
2078 {
2079 png_byte buf[2];
2080
2081 png_crc_read(png_ptr, buf, 2);
2082 readbuf[i] = png_get_uint_16(buf);
2083 }
2084
Matt Sarett96be9082016-05-03 13:29:54 -04002085 if (png_crc_finish(png_ptr, 0) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002086 return;
2087
2088 png_set_hIST(png_ptr, info_ptr, readbuf);
2089}
2090#endif
2091
Patrick Scott5f6bd842010-06-28 16:55:16 -04002092#ifdef PNG_READ_pHYs_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08002093void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07002094png_handle_pHYs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002095{
2096 png_byte buf[9];
2097 png_uint_32 res_x, res_y;
2098 int unit_type;
2099
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002100 png_debug(1, "in png_handle_pHYs");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002101
Matt Sarett96be9082016-05-03 13:29:54 -04002102 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
Chris Craikb50c2172013-07-29 15:28:30 -07002103 png_chunk_error(png_ptr, "missing IHDR");
2104
Matt Sarett96be9082016-05-03 13:29:54 -04002105 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002106 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002107 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002108 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002109 return;
2110 }
Chris Craikb50c2172013-07-29 15:28:30 -07002111
Matt Sarett96be9082016-05-03 13:29:54 -04002112 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002113 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002114 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002115 png_chunk_benign_error(png_ptr, "duplicate");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002116 return;
2117 }
2118
2119 if (length != 9)
2120 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002121 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002122 png_chunk_benign_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002123 return;
2124 }
2125
2126 png_crc_read(png_ptr, buf, 9);
Chris Craikb50c2172013-07-29 15:28:30 -07002127
Matt Sarett96be9082016-05-03 13:29:54 -04002128 if (png_crc_finish(png_ptr, 0) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002129 return;
2130
2131 res_x = png_get_uint_32(buf);
2132 res_y = png_get_uint_32(buf + 4);
2133 unit_type = buf[8];
2134 png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
2135}
2136#endif
2137
Patrick Scott5f6bd842010-06-28 16:55:16 -04002138#ifdef PNG_READ_oFFs_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08002139void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07002140png_handle_oFFs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002141{
2142 png_byte buf[9];
2143 png_int_32 offset_x, offset_y;
2144 int unit_type;
2145
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002146 png_debug(1, "in png_handle_oFFs");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002147
Matt Sarett96be9082016-05-03 13:29:54 -04002148 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
Chris Craikb50c2172013-07-29 15:28:30 -07002149 png_chunk_error(png_ptr, "missing IHDR");
2150
Matt Sarett96be9082016-05-03 13:29:54 -04002151 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002152 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002153 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002154 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002155 return;
2156 }
Chris Craikb50c2172013-07-29 15:28:30 -07002157
Matt Sarett96be9082016-05-03 13:29:54 -04002158 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002159 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002160 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002161 png_chunk_benign_error(png_ptr, "duplicate");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002162 return;
2163 }
2164
2165 if (length != 9)
2166 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002167 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002168 png_chunk_benign_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002169 return;
2170 }
2171
2172 png_crc_read(png_ptr, buf, 9);
Chris Craikb50c2172013-07-29 15:28:30 -07002173
Matt Sarett96be9082016-05-03 13:29:54 -04002174 if (png_crc_finish(png_ptr, 0) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002175 return;
2176
2177 offset_x = png_get_int_32(buf);
2178 offset_y = png_get_int_32(buf + 4);
2179 unit_type = buf[8];
2180 png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
2181}
2182#endif
2183
Patrick Scott5f6bd842010-06-28 16:55:16 -04002184#ifdef PNG_READ_pCAL_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -04002185/* Read the pCAL chunk (described in the PNG Extensions document) */
The Android Open Source Project893912b2009-03-03 19:30:05 -08002186void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07002187png_handle_pCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002188{
The Android Open Source Project893912b2009-03-03 19:30:05 -08002189 png_int_32 X0, X1;
2190 png_byte type, nparams;
Chris Craikb50c2172013-07-29 15:28:30 -07002191 png_bytep buffer, buf, units, endptr;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002192 png_charpp params;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002193 int i;
2194
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002195 png_debug(1, "in png_handle_pCAL");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002196
Matt Sarett96be9082016-05-03 13:29:54 -04002197 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
Chris Craikb50c2172013-07-29 15:28:30 -07002198 png_chunk_error(png_ptr, "missing IHDR");
2199
Matt Sarett96be9082016-05-03 13:29:54 -04002200 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002201 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002202 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002203 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002204 return;
2205 }
Chris Craikb50c2172013-07-29 15:28:30 -07002206
Matt Sarett96be9082016-05-03 13:29:54 -04002207 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002208 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002209 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002210 png_chunk_benign_error(png_ptr, "duplicate");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002211 return;
2212 }
2213
Chris Craikb50c2172013-07-29 15:28:30 -07002214 png_debug1(2, "Allocating and reading pCAL chunk data (%u bytes)",
2215 length + 1);
2216
2217 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
2218
2219 if (buffer == NULL)
2220 {
2221 png_crc_finish(png_ptr, length);
2222 png_chunk_benign_error(png_ptr, "out of memory");
2223 return;
2224 }
2225
2226 png_crc_read(png_ptr, buffer, length);
The Android Open Source Project893912b2009-03-03 19:30:05 -08002227
Matt Sarett96be9082016-05-03 13:29:54 -04002228 if (png_crc_finish(png_ptr, 0) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002229 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002230
Chris Craikb50c2172013-07-29 15:28:30 -07002231 buffer[length] = 0; /* Null terminate the last string */
The Android Open Source Project893912b2009-03-03 19:30:05 -08002232
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002233 png_debug(3, "Finding end of pCAL purpose string");
Chris Craikb50c2172013-07-29 15:28:30 -07002234 for (buf = buffer; *buf; buf++)
Patrick Scotta0bb96c2009-07-22 11:50:02 -04002235 /* Empty loop */ ;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002236
Chris Craikb50c2172013-07-29 15:28:30 -07002237 endptr = buffer + length;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002238
2239 /* We need to have at least 12 bytes after the purpose string
Chris Craikb50c2172013-07-29 15:28:30 -07002240 * in order to get the parameter information.
2241 */
Matt Sarett96be9082016-05-03 13:29:54 -04002242 if (endptr - buf <= 12)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002243 {
Chris Craikb50c2172013-07-29 15:28:30 -07002244 png_chunk_benign_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002245 return;
2246 }
2247
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002248 png_debug(3, "Reading pCAL X0, X1, type, nparams, and units");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002249 X0 = png_get_int_32((png_bytep)buf+1);
2250 X1 = png_get_int_32((png_bytep)buf+5);
2251 type = buf[9];
2252 nparams = buf[10];
2253 units = buf + 11;
2254
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002255 png_debug(3, "Checking pCAL equation type and number of parameters");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002256 /* Check that we have the right number of parameters for known
Chris Craikb50c2172013-07-29 15:28:30 -07002257 * equation types.
2258 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08002259 if ((type == PNG_EQUATION_LINEAR && nparams != 2) ||
2260 (type == PNG_EQUATION_BASE_E && nparams != 3) ||
2261 (type == PNG_EQUATION_ARBITRARY && nparams != 3) ||
2262 (type == PNG_EQUATION_HYPERBOLIC && nparams != 4))
2263 {
Chris Craikb50c2172013-07-29 15:28:30 -07002264 png_chunk_benign_error(png_ptr, "invalid parameter count");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002265 return;
2266 }
Chris Craikb50c2172013-07-29 15:28:30 -07002267
The Android Open Source Project893912b2009-03-03 19:30:05 -08002268 else if (type >= PNG_EQUATION_LAST)
2269 {
Chris Craikb50c2172013-07-29 15:28:30 -07002270 png_chunk_benign_error(png_ptr, "unrecognized equation type");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002271 }
2272
2273 for (buf = units; *buf; buf++)
2274 /* Empty loop to move past the units string. */ ;
2275
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002276 png_debug(3, "Allocating pCAL parameters array");
Chris Craikb50c2172013-07-29 15:28:30 -07002277
2278 params = png_voidcast(png_charpp, png_malloc_warn(png_ptr,
2279 nparams * (sizeof (png_charp))));
2280
The Android Open Source Project893912b2009-03-03 19:30:05 -08002281 if (params == NULL)
Chris Craikb50c2172013-07-29 15:28:30 -07002282 {
2283 png_chunk_benign_error(png_ptr, "out of memory");
2284 return;
2285 }
The Android Open Source Project893912b2009-03-03 19:30:05 -08002286
2287 /* Get pointers to the start of each parameter string. */
Chris Craikb50c2172013-07-29 15:28:30 -07002288 for (i = 0; i < nparams; i++)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002289 {
2290 buf++; /* Skip the null string terminator from previous parameter. */
2291
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002292 png_debug1(3, "Reading pCAL parameter %d", i);
Chris Craikb50c2172013-07-29 15:28:30 -07002293
2294 for (params[i] = (png_charp)buf; buf <= endptr && *buf != 0; buf++)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002295 /* Empty loop to move past each parameter string */ ;
2296
2297 /* Make sure we haven't run out of data yet */
2298 if (buf > endptr)
2299 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002300 png_free(png_ptr, params);
Chris Craikb50c2172013-07-29 15:28:30 -07002301 png_chunk_benign_error(png_ptr, "invalid data");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002302 return;
2303 }
2304 }
2305
Chris Craikb50c2172013-07-29 15:28:30 -07002306 png_set_pCAL(png_ptr, info_ptr, (png_charp)buffer, X0, X1, type, nparams,
2307 (png_charp)units, params);
The Android Open Source Project893912b2009-03-03 19:30:05 -08002308
The Android Open Source Project893912b2009-03-03 19:30:05 -08002309 png_free(png_ptr, params);
2310}
2311#endif
2312
Patrick Scott5f6bd842010-06-28 16:55:16 -04002313#ifdef PNG_READ_sCAL_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -04002314/* Read the sCAL chunk */
The Android Open Source Project893912b2009-03-03 19:30:05 -08002315void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07002316png_handle_sCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002317{
Chris Craikb50c2172013-07-29 15:28:30 -07002318 png_bytep buffer;
2319 png_size_t i;
2320 int state;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002321
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002322 png_debug(1, "in png_handle_sCAL");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002323
Matt Sarett96be9082016-05-03 13:29:54 -04002324 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
Chris Craikb50c2172013-07-29 15:28:30 -07002325 png_chunk_error(png_ptr, "missing IHDR");
2326
Matt Sarett96be9082016-05-03 13:29:54 -04002327 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002328 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002329 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002330 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002331 return;
2332 }
Chris Craikb50c2172013-07-29 15:28:30 -07002333
Matt Sarett96be9082016-05-03 13:29:54 -04002334 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002335 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002336 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002337 png_chunk_benign_error(png_ptr, "duplicate");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002338 return;
2339 }
2340
Eric Vannier66dce0d2011-07-20 17:03:29 -07002341 /* Need unit type, width, \0, height: minimum 4 bytes */
2342 else if (length < 4)
2343 {
Chris Craikb50c2172013-07-29 15:28:30 -07002344 png_crc_finish(png_ptr, length);
2345 png_chunk_benign_error(png_ptr, "invalid");
2346 return;
2347 }
2348
2349 png_debug1(2, "Allocating and reading sCAL chunk data (%u bytes)",
2350 length + 1);
2351
2352 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
2353
2354 if (buffer == NULL)
2355 {
2356 png_chunk_benign_error(png_ptr, "out of memory");
Eric Vannier66dce0d2011-07-20 17:03:29 -07002357 png_crc_finish(png_ptr, length);
2358 return;
2359 }
2360
Chris Craikb50c2172013-07-29 15:28:30 -07002361 png_crc_read(png_ptr, buffer, length);
2362 buffer[length] = 0; /* Null terminate the last string */
The Android Open Source Project893912b2009-03-03 19:30:05 -08002363
Matt Sarett96be9082016-05-03 13:29:54 -04002364 if (png_crc_finish(png_ptr, 0) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07002365 return;
2366
2367 /* Validate the unit. */
2368 if (buffer[0] != 1 && buffer[0] != 2)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002369 {
Chris Craikb50c2172013-07-29 15:28:30 -07002370 png_chunk_benign_error(png_ptr, "invalid unit");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002371 return;
2372 }
2373
Chris Craikb50c2172013-07-29 15:28:30 -07002374 /* Validate the ASCII numbers, need two ASCII numbers separated by
2375 * a '\0' and they need to fit exactly in the chunk data.
2376 */
2377 i = 1;
2378 state = 0;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002379
Matt Sarett96be9082016-05-03 13:29:54 -04002380 if (png_check_fp_number((png_const_charp)buffer, length, &state, &i) == 0 ||
Chris Craikb50c2172013-07-29 15:28:30 -07002381 i >= length || buffer[i++] != 0)
2382 png_chunk_benign_error(png_ptr, "bad width format");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002383
Matt Sarett96be9082016-05-03 13:29:54 -04002384 else if (PNG_FP_IS_POSITIVE(state) == 0)
Chris Craikb50c2172013-07-29 15:28:30 -07002385 png_chunk_benign_error(png_ptr, "non-positive width");
2386
2387 else
The Android Open Source Project893912b2009-03-03 19:30:05 -08002388 {
Chris Craikb50c2172013-07-29 15:28:30 -07002389 png_size_t heighti = i;
2390
2391 state = 0;
Matt Sarett96be9082016-05-03 13:29:54 -04002392 if (png_check_fp_number((png_const_charp)buffer, length,
2393 &state, &i) == 0 || i != length)
Chris Craikb50c2172013-07-29 15:28:30 -07002394 png_chunk_benign_error(png_ptr, "bad height format");
2395
Matt Sarett96be9082016-05-03 13:29:54 -04002396 else if (PNG_FP_IS_POSITIVE(state) == 0)
Chris Craikb50c2172013-07-29 15:28:30 -07002397 png_chunk_benign_error(png_ptr, "non-positive height");
2398
2399 else
2400 /* This is the (only) success case. */
2401 png_set_sCAL_s(png_ptr, info_ptr, buffer[0],
2402 (png_charp)buffer+1, (png_charp)buffer+heighti);
The Android Open Source Project893912b2009-03-03 19:30:05 -08002403 }
The Android Open Source Project893912b2009-03-03 19:30:05 -08002404}
2405#endif
2406
Patrick Scott5f6bd842010-06-28 16:55:16 -04002407#ifdef PNG_READ_tIME_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08002408void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07002409png_handle_tIME(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002410{
2411 png_byte buf[7];
2412 png_time mod_time;
2413
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002414 png_debug(1, "in png_handle_tIME");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002415
Matt Sarett96be9082016-05-03 13:29:54 -04002416 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
Chris Craikb50c2172013-07-29 15:28:30 -07002417 png_chunk_error(png_ptr, "missing IHDR");
2418
Matt Sarett96be9082016-05-03 13:29:54 -04002419 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002420 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002421 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002422 png_chunk_benign_error(png_ptr, "duplicate");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002423 return;
2424 }
2425
Matt Sarett96be9082016-05-03 13:29:54 -04002426 if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002427 png_ptr->mode |= PNG_AFTER_IDAT;
2428
2429 if (length != 7)
2430 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002431 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002432 png_chunk_benign_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002433 return;
2434 }
2435
2436 png_crc_read(png_ptr, buf, 7);
Chris Craikb50c2172013-07-29 15:28:30 -07002437
Matt Sarett96be9082016-05-03 13:29:54 -04002438 if (png_crc_finish(png_ptr, 0) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002439 return;
2440
2441 mod_time.second = buf[6];
2442 mod_time.minute = buf[5];
2443 mod_time.hour = buf[4];
2444 mod_time.day = buf[3];
2445 mod_time.month = buf[2];
2446 mod_time.year = png_get_uint_16(buf);
2447
2448 png_set_tIME(png_ptr, info_ptr, &mod_time);
2449}
2450#endif
2451
Patrick Scott5f6bd842010-06-28 16:55:16 -04002452#ifdef PNG_READ_tEXt_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08002453/* Note: this does not properly handle chunks that are > 64K under DOS */
2454void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07002455png_handle_tEXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002456{
Chris Craikb50c2172013-07-29 15:28:30 -07002457 png_text text_info;
2458 png_bytep buffer;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002459 png_charp key;
2460 png_charp text;
2461 png_uint_32 skip = 0;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002462
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002463 png_debug(1, "in png_handle_tEXt");
2464
Patrick Scott5f6bd842010-06-28 16:55:16 -04002465#ifdef PNG_USER_LIMITS_SUPPORTED
2466 if (png_ptr->user_chunk_cache_max != 0)
2467 {
2468 if (png_ptr->user_chunk_cache_max == 1)
2469 {
2470 png_crc_finish(png_ptr, length);
2471 return;
2472 }
Chris Craikb50c2172013-07-29 15:28:30 -07002473
Patrick Scott5f6bd842010-06-28 16:55:16 -04002474 if (--png_ptr->user_chunk_cache_max == 1)
2475 {
Patrick Scott5f6bd842010-06-28 16:55:16 -04002476 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002477 png_chunk_benign_error(png_ptr, "no space in chunk cache");
Patrick Scott5f6bd842010-06-28 16:55:16 -04002478 return;
2479 }
2480 }
2481#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -08002482
Matt Sarett96be9082016-05-03 13:29:54 -04002483 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
Chris Craikb50c2172013-07-29 15:28:30 -07002484 png_chunk_error(png_ptr, "missing IHDR");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002485
Matt Sarett96be9082016-05-03 13:29:54 -04002486 if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002487 png_ptr->mode |= PNG_AFTER_IDAT;
2488
2489#ifdef PNG_MAX_MALLOC_64K
Chris Craikb50c2172013-07-29 15:28:30 -07002490 if (length > 65535U)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002491 {
Chris Craikb50c2172013-07-29 15:28:30 -07002492 png_crc_finish(png_ptr, length);
2493 png_chunk_benign_error(png_ptr, "too large to fit in memory");
2494 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002495 }
2496#endif
2497
Chris Craikb50c2172013-07-29 15:28:30 -07002498 buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002499
Chris Craikb50c2172013-07-29 15:28:30 -07002500 if (buffer == NULL)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002501 {
Chris Craikb50c2172013-07-29 15:28:30 -07002502 png_chunk_benign_error(png_ptr, "out of memory");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002503 return;
2504 }
Chris Craikb50c2172013-07-29 15:28:30 -07002505
2506 png_crc_read(png_ptr, buffer, length);
The Android Open Source Project893912b2009-03-03 19:30:05 -08002507
Matt Sarett96be9082016-05-03 13:29:54 -04002508 if (png_crc_finish(png_ptr, skip) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002509 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002510
Chris Craikb50c2172013-07-29 15:28:30 -07002511 key = (png_charp)buffer;
2512 key[length] = 0;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002513
2514 for (text = key; *text; text++)
Patrick Scotta0bb96c2009-07-22 11:50:02 -04002515 /* Empty loop to find end of key */ ;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002516
Chris Craikb50c2172013-07-29 15:28:30 -07002517 if (text != key + length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002518 text++;
2519
Chris Craikb50c2172013-07-29 15:28:30 -07002520 text_info.compression = PNG_TEXT_COMPRESSION_NONE;
2521 text_info.key = key;
2522 text_info.lang = NULL;
2523 text_info.lang_key = NULL;
2524 text_info.itxt_length = 0;
2525 text_info.text = text;
2526 text_info.text_length = strlen(text);
The Android Open Source Project893912b2009-03-03 19:30:05 -08002527
Matt Sarett96be9082016-05-03 13:29:54 -04002528 if (png_set_text_2(png_ptr, info_ptr, &text_info, 1) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07002529 png_warning(png_ptr, "Insufficient memory to process text chunk");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002530}
2531#endif
2532
Patrick Scott5f6bd842010-06-28 16:55:16 -04002533#ifdef PNG_READ_zTXt_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -04002534/* Note: this does not correctly handle chunks that are > 64K under DOS */
The Android Open Source Project893912b2009-03-03 19:30:05 -08002535void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07002536png_handle_zTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002537{
Chris Craikb50c2172013-07-29 15:28:30 -07002538 png_const_charp errmsg = NULL;
2539 png_bytep buffer;
2540 png_uint_32 keyword_length;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002541
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002542 png_debug(1, "in png_handle_zTXt");
2543
Patrick Scott5f6bd842010-06-28 16:55:16 -04002544#ifdef PNG_USER_LIMITS_SUPPORTED
2545 if (png_ptr->user_chunk_cache_max != 0)
2546 {
2547 if (png_ptr->user_chunk_cache_max == 1)
2548 {
2549 png_crc_finish(png_ptr, length);
2550 return;
2551 }
Chris Craikb50c2172013-07-29 15:28:30 -07002552
Patrick Scott5f6bd842010-06-28 16:55:16 -04002553 if (--png_ptr->user_chunk_cache_max == 1)
2554 {
Patrick Scott5f6bd842010-06-28 16:55:16 -04002555 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002556 png_chunk_benign_error(png_ptr, "no space in chunk cache");
Patrick Scott5f6bd842010-06-28 16:55:16 -04002557 return;
2558 }
2559 }
2560#endif
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002561
Matt Sarett96be9082016-05-03 13:29:54 -04002562 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
Chris Craikb50c2172013-07-29 15:28:30 -07002563 png_chunk_error(png_ptr, "missing IHDR");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002564
Matt Sarett96be9082016-05-03 13:29:54 -04002565 if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002566 png_ptr->mode |= PNG_AFTER_IDAT;
2567
Chris Craikb50c2172013-07-29 15:28:30 -07002568 buffer = png_read_buffer(png_ptr, length, 2/*silent*/);
The Android Open Source Project893912b2009-03-03 19:30:05 -08002569
Chris Craikb50c2172013-07-29 15:28:30 -07002570 if (buffer == NULL)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002571 {
Chris Craikb50c2172013-07-29 15:28:30 -07002572 png_crc_finish(png_ptr, length);
2573 png_chunk_benign_error(png_ptr, "out of memory");
2574 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002575 }
Chris Craikb50c2172013-07-29 15:28:30 -07002576
2577 png_crc_read(png_ptr, buffer, length);
2578
Matt Sarett96be9082016-05-03 13:29:54 -04002579 if (png_crc_finish(png_ptr, 0) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002580 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002581
Chris Craikb50c2172013-07-29 15:28:30 -07002582 /* TODO: also check that the keyword contents match the spec! */
2583 for (keyword_length = 0;
2584 keyword_length < length && buffer[keyword_length] != 0;
2585 ++keyword_length)
2586 /* Empty loop to find end of name */ ;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002587
Chris Craikb50c2172013-07-29 15:28:30 -07002588 if (keyword_length > 79 || keyword_length < 1)
2589 errmsg = "bad keyword";
The Android Open Source Project893912b2009-03-03 19:30:05 -08002590
Chris Craikb50c2172013-07-29 15:28:30 -07002591 /* zTXt must have some LZ data after the keyword, although it may expand to
2592 * zero bytes; we need a '\0' at the end of the keyword, the compression type
2593 * then the LZ data:
2594 */
2595 else if (keyword_length + 3 > length)
2596 errmsg = "truncated";
2597
2598 else if (buffer[keyword_length+1] != PNG_COMPRESSION_TYPE_BASE)
2599 errmsg = "unknown compression type";
2600
The Android Open Source Project893912b2009-03-03 19:30:05 -08002601 else
2602 {
Chris Craikb50c2172013-07-29 15:28:30 -07002603 png_alloc_size_t uncompressed_length = PNG_SIZE_MAX;
2604
2605 /* TODO: at present png_decompress_chunk imposes a single application
2606 * level memory limit, this should be split to different values for iCCP
2607 * and text chunks.
2608 */
2609 if (png_decompress_chunk(png_ptr, length, keyword_length+2,
2610 &uncompressed_length, 1/*terminate*/) == Z_STREAM_END)
2611 {
2612 png_text text;
2613
2614 /* It worked; png_ptr->read_buffer now looks like a tEXt chunk except
2615 * for the extra compression type byte and the fact that it isn't
2616 * necessarily '\0' terminated.
2617 */
2618 buffer = png_ptr->read_buffer;
2619 buffer[uncompressed_length+(keyword_length+2)] = 0;
2620
2621 text.compression = PNG_TEXT_COMPRESSION_zTXt;
2622 text.key = (png_charp)buffer;
2623 text.text = (png_charp)(buffer + keyword_length+2);
2624 text.text_length = uncompressed_length;
2625 text.itxt_length = 0;
2626 text.lang = NULL;
2627 text.lang_key = NULL;
2628
Matt Sarett96be9082016-05-03 13:29:54 -04002629 if (png_set_text_2(png_ptr, info_ptr, &text, 1) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07002630 errmsg = "insufficient memory";
2631 }
2632
2633 else
2634 errmsg = png_ptr->zstream.msg;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002635 }
The Android Open Source Project893912b2009-03-03 19:30:05 -08002636
Chris Craikb50c2172013-07-29 15:28:30 -07002637 if (errmsg != NULL)
2638 png_chunk_benign_error(png_ptr, errmsg);
The Android Open Source Project893912b2009-03-03 19:30:05 -08002639}
2640#endif
2641
Patrick Scott5f6bd842010-06-28 16:55:16 -04002642#ifdef PNG_READ_iTXt_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -04002643/* Note: this does not correctly handle chunks that are > 64K under DOS */
The Android Open Source Project893912b2009-03-03 19:30:05 -08002644void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07002645png_handle_iTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002646{
Chris Craikb50c2172013-07-29 15:28:30 -07002647 png_const_charp errmsg = NULL;
2648 png_bytep buffer;
2649 png_uint_32 prefix_length;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002650
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002651 png_debug(1, "in png_handle_iTXt");
2652
Patrick Scott5f6bd842010-06-28 16:55:16 -04002653#ifdef PNG_USER_LIMITS_SUPPORTED
2654 if (png_ptr->user_chunk_cache_max != 0)
2655 {
2656 if (png_ptr->user_chunk_cache_max == 1)
2657 {
2658 png_crc_finish(png_ptr, length);
2659 return;
2660 }
Chris Craikb50c2172013-07-29 15:28:30 -07002661
Patrick Scott5f6bd842010-06-28 16:55:16 -04002662 if (--png_ptr->user_chunk_cache_max == 1)
2663 {
Patrick Scott5f6bd842010-06-28 16:55:16 -04002664 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002665 png_chunk_benign_error(png_ptr, "no space in chunk cache");
Patrick Scott5f6bd842010-06-28 16:55:16 -04002666 return;
2667 }
2668 }
2669#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -08002670
Matt Sarett96be9082016-05-03 13:29:54 -04002671 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
Chris Craikb50c2172013-07-29 15:28:30 -07002672 png_chunk_error(png_ptr, "missing IHDR");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002673
Matt Sarett96be9082016-05-03 13:29:54 -04002674 if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002675 png_ptr->mode |= PNG_AFTER_IDAT;
2676
Chris Craikb50c2172013-07-29 15:28:30 -07002677 buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
The Android Open Source Project893912b2009-03-03 19:30:05 -08002678
Chris Craikb50c2172013-07-29 15:28:30 -07002679 if (buffer == NULL)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002680 {
Chris Craikb50c2172013-07-29 15:28:30 -07002681 png_crc_finish(png_ptr, length);
2682 png_chunk_benign_error(png_ptr, "out of memory");
2683 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002684 }
Chris Craikb50c2172013-07-29 15:28:30 -07002685
2686 png_crc_read(png_ptr, buffer, length);
2687
Matt Sarett96be9082016-05-03 13:29:54 -04002688 if (png_crc_finish(png_ptr, 0) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002689 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002690
Chris Craikb50c2172013-07-29 15:28:30 -07002691 /* First the keyword. */
2692 for (prefix_length=0;
2693 prefix_length < length && buffer[prefix_length] != 0;
2694 ++prefix_length)
Patrick Scotta0bb96c2009-07-22 11:50:02 -04002695 /* Empty loop */ ;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002696
Chris Craikb50c2172013-07-29 15:28:30 -07002697 /* Perform a basic check on the keyword length here. */
2698 if (prefix_length > 79 || prefix_length < 1)
2699 errmsg = "bad keyword";
2700
2701 /* Expect keyword, compression flag, compression type, language, translated
2702 * keyword (both may be empty but are 0 terminated) then the text, which may
2703 * be empty.
Patrick Scotta0bb96c2009-07-22 11:50:02 -04002704 */
Chris Craikb50c2172013-07-29 15:28:30 -07002705 else if (prefix_length + 5 > length)
2706 errmsg = "truncated";
The Android Open Source Project893912b2009-03-03 19:30:05 -08002707
Chris Craikb50c2172013-07-29 15:28:30 -07002708 else if (buffer[prefix_length+1] == 0 ||
2709 (buffer[prefix_length+1] == 1 &&
2710 buffer[prefix_length+2] == PNG_COMPRESSION_TYPE_BASE))
The Android Open Source Project893912b2009-03-03 19:30:05 -08002711 {
Chris Craikb50c2172013-07-29 15:28:30 -07002712 int compressed = buffer[prefix_length+1] != 0;
2713 png_uint_32 language_offset, translated_keyword_offset;
2714 png_alloc_size_t uncompressed_length = 0;
2715
2716 /* Now the language tag */
2717 prefix_length += 3;
2718 language_offset = prefix_length;
2719
2720 for (; prefix_length < length && buffer[prefix_length] != 0;
2721 ++prefix_length)
2722 /* Empty loop */ ;
2723
2724 /* WARNING: the length may be invalid here, this is checked below. */
2725 translated_keyword_offset = ++prefix_length;
2726
2727 for (; prefix_length < length && buffer[prefix_length] != 0;
2728 ++prefix_length)
2729 /* Empty loop */ ;
2730
2731 /* prefix_length should now be at the trailing '\0' of the translated
2732 * keyword, but it may already be over the end. None of this arithmetic
2733 * can overflow because chunks are at most 2^31 bytes long, but on 16-bit
Matt Sarett96be9082016-05-03 13:29:54 -04002734 * systems the available allocation may overflow.
Chris Craikb50c2172013-07-29 15:28:30 -07002735 */
2736 ++prefix_length;
2737
Matt Sarett96be9082016-05-03 13:29:54 -04002738 if (compressed == 0 && prefix_length <= length)
Chris Craikb50c2172013-07-29 15:28:30 -07002739 uncompressed_length = length - prefix_length;
2740
Matt Sarett96be9082016-05-03 13:29:54 -04002741 else if (compressed != 0 && prefix_length < length)
Chris Craikb50c2172013-07-29 15:28:30 -07002742 {
2743 uncompressed_length = PNG_SIZE_MAX;
2744
2745 /* TODO: at present png_decompress_chunk imposes a single application
2746 * level memory limit, this should be split to different values for
2747 * iCCP and text chunks.
2748 */
2749 if (png_decompress_chunk(png_ptr, length, prefix_length,
2750 &uncompressed_length, 1/*terminate*/) == Z_STREAM_END)
2751 buffer = png_ptr->read_buffer;
2752
2753 else
2754 errmsg = png_ptr->zstream.msg;
2755 }
2756
2757 else
2758 errmsg = "truncated";
2759
2760 if (errmsg == NULL)
2761 {
2762 png_text text;
2763
2764 buffer[uncompressed_length+prefix_length] = 0;
2765
Matt Sarett96be9082016-05-03 13:29:54 -04002766 if (compressed == 0)
Chris Craikb50c2172013-07-29 15:28:30 -07002767 text.compression = PNG_ITXT_COMPRESSION_NONE;
2768
2769 else
2770 text.compression = PNG_ITXT_COMPRESSION_zTXt;
2771
2772 text.key = (png_charp)buffer;
2773 text.lang = (png_charp)buffer + language_offset;
2774 text.lang_key = (png_charp)buffer + translated_keyword_offset;
2775 text.text = (png_charp)buffer + prefix_length;
2776 text.text_length = 0;
2777 text.itxt_length = uncompressed_length;
2778
Matt Sarett96be9082016-05-03 13:29:54 -04002779 if (png_set_text_2(png_ptr, info_ptr, &text, 1) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07002780 errmsg = "insufficient memory";
2781 }
The Android Open Source Project893912b2009-03-03 19:30:05 -08002782 }
Chris Craikb50c2172013-07-29 15:28:30 -07002783
The Android Open Source Project893912b2009-03-03 19:30:05 -08002784 else
Chris Craikb50c2172013-07-29 15:28:30 -07002785 errmsg = "bad compression info";
The Android Open Source Project893912b2009-03-03 19:30:05 -08002786
Chris Craikb50c2172013-07-29 15:28:30 -07002787 if (errmsg != NULL)
2788 png_chunk_benign_error(png_ptr, errmsg);
The Android Open Source Project893912b2009-03-03 19:30:05 -08002789}
2790#endif
2791
Chris Craikb50c2172013-07-29 15:28:30 -07002792#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
2793/* Utility function for png_handle_unknown; set up png_ptr::unknown_chunk */
2794static int
2795png_cache_unknown_chunk(png_structrp png_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002796{
Chris Craikb50c2172013-07-29 15:28:30 -07002797 png_alloc_size_t limit = PNG_SIZE_MAX;
2798
2799 if (png_ptr->unknown_chunk.data != NULL)
2800 {
2801 png_free(png_ptr, png_ptr->unknown_chunk.data);
2802 png_ptr->unknown_chunk.data = NULL;
2803 }
2804
Matt Sarett96be9082016-05-03 13:29:54 -04002805# ifdef PNG_SET_USER_LIMITS_SUPPORTED
2806 if (png_ptr->user_chunk_malloc_max > 0 &&
2807 png_ptr->user_chunk_malloc_max < limit)
2808 limit = png_ptr->user_chunk_malloc_max;
Chris Craikb50c2172013-07-29 15:28:30 -07002809
2810# elif PNG_USER_CHUNK_MALLOC_MAX > 0
Matt Sarett96be9082016-05-03 13:29:54 -04002811 if (PNG_USER_CHUNK_MALLOC_MAX < limit)
2812 limit = PNG_USER_CHUNK_MALLOC_MAX;
Chris Craikb50c2172013-07-29 15:28:30 -07002813# endif
2814
2815 if (length <= limit)
2816 {
2817 PNG_CSTRING_FROM_CHUNK(png_ptr->unknown_chunk.name, png_ptr->chunk_name);
2818 /* The following is safe because of the PNG_SIZE_MAX init above */
2819 png_ptr->unknown_chunk.size = (png_size_t)length/*SAFE*/;
2820 /* 'mode' is a flag array, only the bottom four bits matter here */
2821 png_ptr->unknown_chunk.location = (png_byte)png_ptr->mode/*SAFE*/;
2822
2823 if (length == 0)
2824 png_ptr->unknown_chunk.data = NULL;
2825
2826 else
2827 {
2828 /* Do a 'warn' here - it is handled below. */
2829 png_ptr->unknown_chunk.data = png_voidcast(png_bytep,
2830 png_malloc_warn(png_ptr, length));
2831 }
2832 }
2833
2834 if (png_ptr->unknown_chunk.data == NULL && length > 0)
2835 {
2836 /* This is benign because we clean up correctly */
2837 png_crc_finish(png_ptr, length);
2838 png_chunk_benign_error(png_ptr, "unknown chunk exceeds memory limits");
2839 return 0;
2840 }
2841
2842 else
2843 {
2844 if (length > 0)
2845 png_crc_read(png_ptr, png_ptr->unknown_chunk.data, length);
2846 png_crc_finish(png_ptr, 0);
2847 return 1;
2848 }
2849}
Matt Sarett96be9082016-05-03 13:29:54 -04002850#endif /* READ_UNKNOWN_CHUNKS */
Chris Craikb50c2172013-07-29 15:28:30 -07002851
2852/* Handle an unknown, or known but disabled, chunk */
2853void /* PRIVATE */
2854png_handle_unknown(png_structrp png_ptr, png_inforp info_ptr,
2855 png_uint_32 length, int keep)
2856{
2857 int handled = 0; /* the chunk was handled */
The Android Open Source Project893912b2009-03-03 19:30:05 -08002858
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002859 png_debug(1, "in png_handle_unknown");
2860
Patrick Scott5f6bd842010-06-28 16:55:16 -04002861#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07002862 /* NOTE: this code is based on the code in libpng-1.4.12 except for fixing
2863 * the bug which meant that setting a non-default behavior for a specific
2864 * chunk would be ignored (the default was always used unless a user
2865 * callback was installed).
2866 *
2867 * 'keep' is the value from the png_chunk_unknown_handling, the setting for
2868 * this specific chunk_name, if PNG_HANDLE_AS_UNKNOWN_SUPPORTED, if not it
2869 * will always be PNG_HANDLE_CHUNK_AS_DEFAULT and it needs to be set here.
2870 * This is just an optimization to avoid multiple calls to the lookup
2871 * function.
2872 */
2873# ifndef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
2874# ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
Matt Sarett96be9082016-05-03 13:29:54 -04002875 keep = png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name);
Chris Craikb50c2172013-07-29 15:28:30 -07002876# endif
2877# endif
The Android Open Source Project893912b2009-03-03 19:30:05 -08002878
Chris Craikb50c2172013-07-29 15:28:30 -07002879 /* One of the following methods will read the chunk or skip it (at least one
2880 * of these is always defined because this is the only way to switch on
2881 * PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
2882 */
2883# ifdef PNG_READ_USER_CHUNKS_SUPPORTED
Matt Sarett96be9082016-05-03 13:29:54 -04002884 /* The user callback takes precedence over the chunk keep value, but the
2885 * keep value is still required to validate a save of a critical chunk.
2886 */
2887 if (png_ptr->read_user_chunk_fn != NULL)
2888 {
2889 if (png_cache_unknown_chunk(png_ptr, length) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07002890 {
Matt Sarett96be9082016-05-03 13:29:54 -04002891 /* Callback to user unknown chunk handler */
2892 int ret = (*(png_ptr->read_user_chunk_fn))(png_ptr,
2893 &png_ptr->unknown_chunk);
2894
2895 /* ret is:
2896 * negative: An error occurred; png_chunk_error will be called.
2897 * zero: The chunk was not handled, the chunk will be discarded
2898 * unless png_set_keep_unknown_chunks has been used to set
2899 * a 'keep' behavior for this particular chunk, in which
2900 * case that will be used. A critical chunk will cause an
2901 * error at this point unless it is to be saved.
2902 * positive: The chunk was handled, libpng will ignore/discard it.
2903 */
2904 if (ret < 0)
2905 png_chunk_error(png_ptr, "error in user chunk");
2906
2907 else if (ret == 0)
Chris Craikb50c2172013-07-29 15:28:30 -07002908 {
Matt Sarett96be9082016-05-03 13:29:54 -04002909 /* If the keep value is 'default' or 'never' override it, but
2910 * still error out on critical chunks unless the keep value is
2911 * 'always' While this is weird it is the behavior in 1.4.12.
2912 * A possible improvement would be to obey the value set for the
2913 * chunk, but this would be an API change that would probably
2914 * damage some applications.
2915 *
2916 * The png_app_warning below catches the case that matters, where
2917 * the application has not set specific save or ignore for this
2918 * chunk or global save or ignore.
Chris Craikb50c2172013-07-29 15:28:30 -07002919 */
Matt Sarett96be9082016-05-03 13:29:54 -04002920 if (keep < PNG_HANDLE_CHUNK_IF_SAFE)
Chris Craikb50c2172013-07-29 15:28:30 -07002921 {
Matt Sarett96be9082016-05-03 13:29:54 -04002922# ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
2923 if (png_ptr->unknown_default < PNG_HANDLE_CHUNK_IF_SAFE)
Chris Craikb50c2172013-07-29 15:28:30 -07002924 {
Matt Sarett96be9082016-05-03 13:29:54 -04002925 png_chunk_warning(png_ptr, "Saving unknown chunk:");
2926 png_app_warning(png_ptr,
2927 "forcing save of an unhandled chunk;"
2928 " please call png_set_keep_unknown_chunks");
2929 /* with keep = PNG_HANDLE_CHUNK_IF_SAFE */
Chris Craikb50c2172013-07-29 15:28:30 -07002930 }
Matt Sarett96be9082016-05-03 13:29:54 -04002931# endif
2932 keep = PNG_HANDLE_CHUNK_IF_SAFE;
Chris Craikb50c2172013-07-29 15:28:30 -07002933 }
2934 }
2935
Matt Sarett96be9082016-05-03 13:29:54 -04002936 else /* chunk was handled */
2937 {
2938 handled = 1;
2939 /* Critical chunks can be safely discarded at this point. */
2940 keep = PNG_HANDLE_CHUNK_NEVER;
2941 }
Chris Craikb50c2172013-07-29 15:28:30 -07002942 }
2943
2944 else
Matt Sarett96be9082016-05-03 13:29:54 -04002945 keep = PNG_HANDLE_CHUNK_NEVER; /* insufficient memory */
2946 }
2947
2948 else
2949 /* Use the SAVE_UNKNOWN_CHUNKS code or skip the chunk */
2950# endif /* READ_USER_CHUNKS */
Chris Craikb50c2172013-07-29 15:28:30 -07002951
2952# ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
Matt Sarett96be9082016-05-03 13:29:54 -04002953 {
2954 /* keep is currently just the per-chunk setting, if there was no
2955 * setting change it to the global default now (not that this may
2956 * still be AS_DEFAULT) then obtain the cache of the chunk if required,
2957 * if not simply skip the chunk.
2958 */
2959 if (keep == PNG_HANDLE_CHUNK_AS_DEFAULT)
2960 keep = png_ptr->unknown_default;
2961
2962 if (keep == PNG_HANDLE_CHUNK_ALWAYS ||
2963 (keep == PNG_HANDLE_CHUNK_IF_SAFE &&
2964 PNG_CHUNK_ANCILLARY(png_ptr->chunk_name)))
Chris Craikb50c2172013-07-29 15:28:30 -07002965 {
Matt Sarett96be9082016-05-03 13:29:54 -04002966 if (png_cache_unknown_chunk(png_ptr, length) == 0)
2967 keep = PNG_HANDLE_CHUNK_NEVER;
Chris Craikb50c2172013-07-29 15:28:30 -07002968 }
Matt Sarett96be9082016-05-03 13:29:54 -04002969
2970 else
2971 png_crc_finish(png_ptr, length);
2972 }
Chris Craikb50c2172013-07-29 15:28:30 -07002973# else
2974# ifndef PNG_READ_USER_CHUNKS_SUPPORTED
2975# error no method to support READ_UNKNOWN_CHUNKS
2976# endif
2977
Matt Sarett96be9082016-05-03 13:29:54 -04002978 {
2979 /* If here there is no read callback pointer set and no support is
2980 * compiled in to just save the unknown chunks, so simply skip this
2981 * chunk. If 'keep' is something other than AS_DEFAULT or NEVER then
2982 * the app has erroneously asked for unknown chunk saving when there
2983 * is no support.
2984 */
2985 if (keep > PNG_HANDLE_CHUNK_NEVER)
2986 png_app_error(png_ptr, "no unknown chunk support available");
Chris Craikb50c2172013-07-29 15:28:30 -07002987
Matt Sarett96be9082016-05-03 13:29:54 -04002988 png_crc_finish(png_ptr, length);
2989 }
Chris Craikb50c2172013-07-29 15:28:30 -07002990# endif
2991
2992# ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
Matt Sarett96be9082016-05-03 13:29:54 -04002993 /* Now store the chunk in the chunk list if appropriate, and if the limits
2994 * permit it.
2995 */
2996 if (keep == PNG_HANDLE_CHUNK_ALWAYS ||
2997 (keep == PNG_HANDLE_CHUNK_IF_SAFE &&
2998 PNG_CHUNK_ANCILLARY(png_ptr->chunk_name)))
2999 {
3000# ifdef PNG_USER_LIMITS_SUPPORTED
3001 switch (png_ptr->user_chunk_cache_max)
Chris Craikb50c2172013-07-29 15:28:30 -07003002 {
Matt Sarett96be9082016-05-03 13:29:54 -04003003 case 2:
3004 png_ptr->user_chunk_cache_max = 1;
3005 png_chunk_benign_error(png_ptr, "no space in chunk cache");
3006 /* FALL THROUGH */
3007 case 1:
3008 /* NOTE: prior to 1.6.0 this case resulted in an unknown critical
3009 * chunk being skipped, now there will be a hard error below.
3010 */
3011 break;
Chris Craikb50c2172013-07-29 15:28:30 -07003012
Matt Sarett96be9082016-05-03 13:29:54 -04003013 default: /* not at limit */
3014 --(png_ptr->user_chunk_cache_max);
3015 /* FALL THROUGH */
3016 case 0: /* no limit */
3017# endif /* USER_LIMITS */
3018 /* Here when the limit isn't reached or when limits are compiled
3019 * out; store the chunk.
3020 */
3021 png_set_unknown_chunks(png_ptr, info_ptr,
3022 &png_ptr->unknown_chunk, 1);
3023 handled = 1;
3024# ifdef PNG_USER_LIMITS_SUPPORTED
3025 break;
Chris Craikb50c2172013-07-29 15:28:30 -07003026 }
Matt Sarett96be9082016-05-03 13:29:54 -04003027# endif
3028 }
Sireesh Tripurarib478e662014-05-09 15:15:10 +05303029# else /* no store support: the chunk must be handled by the user callback */
Matt Sarett96be9082016-05-03 13:29:54 -04003030 PNG_UNUSED(info_ptr)
Chris Craikb50c2172013-07-29 15:28:30 -07003031# endif
3032
3033 /* Regardless of the error handling below the cached data (if any) can be
3034 * freed now. Notice that the data is not freed if there is a png_error, but
3035 * it will be freed by destroy_read_struct.
3036 */
3037 if (png_ptr->unknown_chunk.data != NULL)
3038 png_free(png_ptr, png_ptr->unknown_chunk.data);
3039 png_ptr->unknown_chunk.data = NULL;
3040
3041#else /* !PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */
3042 /* There is no support to read an unknown chunk, so just skip it. */
3043 png_crc_finish(png_ptr, length);
3044 PNG_UNUSED(info_ptr)
3045 PNG_UNUSED(keep)
Matt Sarett96be9082016-05-03 13:29:54 -04003046#endif /* !READ_UNKNOWN_CHUNKS */
Chris Craikb50c2172013-07-29 15:28:30 -07003047
3048 /* Check for unhandled critical chunks */
Matt Sarett96be9082016-05-03 13:29:54 -04003049 if (handled == 0 && PNG_CHUNK_CRITICAL(png_ptr->chunk_name))
Chris Craikb50c2172013-07-29 15:28:30 -07003050 png_chunk_error(png_ptr, "unhandled critical chunk");
The Android Open Source Project893912b2009-03-03 19:30:05 -08003051}
3052
3053/* This function is called to verify that a chunk name is valid.
Chris Craikb50c2172013-07-29 15:28:30 -07003054 * This function can't have the "critical chunk check" incorporated
3055 * into it, since in the future we will need to be able to call user
3056 * functions to handle unknown critical chunks after we check that
3057 * the chunk name itself is valid.
3058 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08003059
Chris Craikb50c2172013-07-29 15:28:30 -07003060/* Bit hacking: the test for an invalid byte in the 4 byte chunk name is:
3061 *
3062 * ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
3063 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08003064
3065void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07003066png_check_chunk_name(png_structrp png_ptr, png_uint_32 chunk_name)
The Android Open Source Project893912b2009-03-03 19:30:05 -08003067{
Chris Craikb50c2172013-07-29 15:28:30 -07003068 int i;
3069
The Android Open Source Project4215dd12009-03-09 11:52:12 -07003070 png_debug(1, "in png_check_chunk_name");
Chris Craikb50c2172013-07-29 15:28:30 -07003071
3072 for (i=1; i<=4; ++i)
The Android Open Source Project893912b2009-03-03 19:30:05 -08003073 {
Chris Craikb50c2172013-07-29 15:28:30 -07003074 int c = chunk_name & 0xff;
3075
3076 if (c < 65 || c > 122 || (c > 90 && c < 97))
3077 png_chunk_error(png_ptr, "invalid chunk type");
3078
3079 chunk_name >>= 8;
The Android Open Source Project893912b2009-03-03 19:30:05 -08003080 }
3081}
3082
Chris Craikb50c2172013-07-29 15:28:30 -07003083/* Combines the row recently read in with the existing pixels in the row. This
3084 * routine takes care of alpha and transparency if requested. This routine also
3085 * handles the two methods of progressive display of interlaced images,
3086 * depending on the 'display' value; if 'display' is true then the whole row
3087 * (dp) is filled from the start by replicating the available pixels. If
3088 * 'display' is false only those pixels present in the pass are filled in.
3089 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08003090void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07003091png_combine_row(png_const_structrp png_ptr, png_bytep dp, int display)
The Android Open Source Project893912b2009-03-03 19:30:05 -08003092{
Chris Craikb50c2172013-07-29 15:28:30 -07003093 unsigned int pixel_depth = png_ptr->transformed_pixel_depth;
3094 png_const_bytep sp = png_ptr->row_buf + 1;
Henrik Smiding82aab852015-02-20 13:01:11 +01003095 png_alloc_size_t row_width = png_ptr->width;
Chris Craikb50c2172013-07-29 15:28:30 -07003096 unsigned int pass = png_ptr->pass;
3097 png_bytep end_ptr = 0;
3098 png_byte end_byte = 0;
3099 unsigned int end_mask;
3100
The Android Open Source Project4215dd12009-03-09 11:52:12 -07003101 png_debug(1, "in png_combine_row");
Chris Craikb50c2172013-07-29 15:28:30 -07003102
3103 /* Added in 1.5.6: it should not be possible to enter this routine until at
3104 * least one row has been read from the PNG data and transformed.
3105 */
3106 if (pixel_depth == 0)
3107 png_error(png_ptr, "internal row logic error");
3108
3109 /* Added in 1.5.4: the pixel depth should match the information returned by
3110 * any call to png_read_update_info at this point. Do not continue if we got
3111 * this wrong.
3112 */
3113 if (png_ptr->info_rowbytes != 0 && png_ptr->info_rowbytes !=
3114 PNG_ROWBYTES(pixel_depth, row_width))
3115 png_error(png_ptr, "internal row size calculation error");
3116
3117 /* Don't expect this to ever happen: */
3118 if (row_width == 0)
3119 png_error(png_ptr, "internal row width error");
3120
3121 /* Preserve the last byte in cases where only part of it will be overwritten,
3122 * the multiply below may overflow, we don't care because ANSI-C guarantees
3123 * we get the low bits.
3124 */
3125 end_mask = (pixel_depth * row_width) & 7;
3126 if (end_mask != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08003127 {
Chris Craikb50c2172013-07-29 15:28:30 -07003128 /* end_ptr == NULL is a flag to say do nothing */
3129 end_ptr = dp + PNG_ROWBYTES(pixel_depth, row_width) - 1;
3130 end_byte = *end_ptr;
3131# ifdef PNG_READ_PACKSWAP_SUPPORTED
Matt Sarett96be9082016-05-03 13:29:54 -04003132 if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
3133 /* little-endian byte */
3134 end_mask = 0xff << end_mask;
Chris Craikb50c2172013-07-29 15:28:30 -07003135
Matt Sarett96be9082016-05-03 13:29:54 -04003136 else /* big-endian byte */
Chris Craikb50c2172013-07-29 15:28:30 -07003137# endif
Matt Sarett96be9082016-05-03 13:29:54 -04003138 end_mask = 0xff >> end_mask;
Chris Craikb50c2172013-07-29 15:28:30 -07003139 /* end_mask is now the bits to *keep* from the destination row */
The Android Open Source Project893912b2009-03-03 19:30:05 -08003140 }
Chris Craikb50c2172013-07-29 15:28:30 -07003141
3142 /* For non-interlaced images this reduces to a memcpy(). A memcpy()
3143 * will also happen if interlacing isn't supported or if the application
3144 * does not call png_set_interlace_handling(). In the latter cases the
3145 * caller just gets a sequence of the unexpanded rows from each interlace
3146 * pass.
3147 */
3148#ifdef PNG_READ_INTERLACING_SUPPORTED
Matt Sarett96be9082016-05-03 13:29:54 -04003149 if (png_ptr->interlaced != 0 &&
3150 (png_ptr->transformations & PNG_INTERLACE) != 0 &&
3151 pass < 6 && (display == 0 ||
3152 /* The following copies everything for 'display' on passes 0, 2 and 4. */
3153 (display == 1 && (pass & 1) != 0)))
The Android Open Source Project893912b2009-03-03 19:30:05 -08003154 {
Chris Craikb50c2172013-07-29 15:28:30 -07003155 /* Narrow images may have no bits in a pass; the caller should handle
3156 * this, but this test is cheap:
3157 */
3158 if (row_width <= PNG_PASS_START_COL(pass))
3159 return;
3160
3161 if (pixel_depth < 8)
The Android Open Source Project893912b2009-03-03 19:30:05 -08003162 {
Chris Craikb50c2172013-07-29 15:28:30 -07003163 /* For pixel depths up to 4 bpp the 8-pixel mask can be expanded to fit
3164 * into 32 bits, then a single loop over the bytes using the four byte
3165 * values in the 32-bit mask can be used. For the 'display' option the
3166 * expanded mask may also not require any masking within a byte. To
3167 * make this work the PACKSWAP option must be taken into account - it
3168 * simply requires the pixels to be reversed in each byte.
3169 *
3170 * The 'regular' case requires a mask for each of the first 6 passes,
3171 * the 'display' case does a copy for the even passes in the range
3172 * 0..6. This has already been handled in the test above.
3173 *
3174 * The masks are arranged as four bytes with the first byte to use in
3175 * the lowest bits (little-endian) regardless of the order (PACKSWAP or
3176 * not) of the pixels in each byte.
3177 *
3178 * NOTE: the whole of this logic depends on the caller of this function
3179 * only calling it on rows appropriate to the pass. This function only
3180 * understands the 'x' logic; the 'y' logic is handled by the caller.
3181 *
3182 * The following defines allow generation of compile time constant bit
3183 * masks for each pixel depth and each possibility of swapped or not
3184 * swapped bytes. Pass 'p' is in the range 0..6; 'x', a pixel index,
3185 * is in the range 0..7; and the result is 1 if the pixel is to be
3186 * copied in the pass, 0 if not. 'S' is for the sparkle method, 'B'
3187 * for the block method.
3188 *
3189 * With some compilers a compile time expression of the general form:
3190 *
3191 * (shift >= 32) ? (a >> (shift-32)) : (b >> shift)
3192 *
3193 * Produces warnings with values of 'shift' in the range 33 to 63
3194 * because the right hand side of the ?: expression is evaluated by
3195 * the compiler even though it isn't used. Microsoft Visual C (various
3196 * versions) and the Intel C compiler are known to do this. To avoid
3197 * this the following macros are used in 1.5.6. This is a temporary
3198 * solution to avoid destabilizing the code during the release process.
3199 */
3200# if PNG_USE_COMPILE_TIME_MASKS
3201# define PNG_LSR(x,s) ((x)>>((s) & 0x1f))
3202# define PNG_LSL(x,s) ((x)<<((s) & 0x1f))
3203# else
3204# define PNG_LSR(x,s) ((x)>>(s))
3205# define PNG_LSL(x,s) ((x)<<(s))
3206# endif
3207# define S_COPY(p,x) (((p)<4 ? PNG_LSR(0x80088822,(3-(p))*8+(7-(x))) :\
3208 PNG_LSR(0xaa55ff00,(7-(p))*8+(7-(x)))) & 1)
3209# define B_COPY(p,x) (((p)<4 ? PNG_LSR(0xff0fff33,(3-(p))*8+(7-(x))) :\
3210 PNG_LSR(0xff55ff00,(7-(p))*8+(7-(x)))) & 1)
The Android Open Source Project893912b2009-03-03 19:30:05 -08003211
Chris Craikb50c2172013-07-29 15:28:30 -07003212 /* Return a mask for pass 'p' pixel 'x' at depth 'd'. The mask is
3213 * little endian - the first pixel is at bit 0 - however the extra
3214 * parameter 's' can be set to cause the mask position to be swapped
3215 * within each byte, to match the PNG format. This is done by XOR of
3216 * the shift with 7, 6 or 4 for bit depths 1, 2 and 4.
3217 */
3218# define PIXEL_MASK(p,x,d,s) \
3219 (PNG_LSL(((PNG_LSL(1U,(d)))-1),(((x)*(d))^((s)?8-(d):0))))
3220
3221 /* Hence generate the appropriate 'block' or 'sparkle' pixel copy mask.
3222 */
3223# define S_MASKx(p,x,d,s) (S_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
3224# define B_MASKx(p,x,d,s) (B_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
3225
3226 /* Combine 8 of these to get the full mask. For the 1-bpp and 2-bpp
3227 * cases the result needs replicating, for the 4-bpp case the above
3228 * generates a full 32 bits.
3229 */
3230# define MASK_EXPAND(m,d) ((m)*((d)==1?0x01010101:((d)==2?0x00010001:1)))
3231
3232# define S_MASK(p,d,s) MASK_EXPAND(S_MASKx(p,0,d,s) + S_MASKx(p,1,d,s) +\
3233 S_MASKx(p,2,d,s) + S_MASKx(p,3,d,s) + S_MASKx(p,4,d,s) +\
3234 S_MASKx(p,5,d,s) + S_MASKx(p,6,d,s) + S_MASKx(p,7,d,s), d)
3235
3236# define B_MASK(p,d,s) MASK_EXPAND(B_MASKx(p,0,d,s) + B_MASKx(p,1,d,s) +\
3237 B_MASKx(p,2,d,s) + B_MASKx(p,3,d,s) + B_MASKx(p,4,d,s) +\
3238 B_MASKx(p,5,d,s) + B_MASKx(p,6,d,s) + B_MASKx(p,7,d,s), d)
3239
3240#if PNG_USE_COMPILE_TIME_MASKS
3241 /* Utility macros to construct all the masks for a depth/swap
3242 * combination. The 's' parameter says whether the format is PNG
3243 * (big endian bytes) or not. Only the three odd-numbered passes are
3244 * required for the display/block algorithm.
3245 */
3246# define S_MASKS(d,s) { S_MASK(0,d,s), S_MASK(1,d,s), S_MASK(2,d,s),\
3247 S_MASK(3,d,s), S_MASK(4,d,s), S_MASK(5,d,s) }
3248
Matt Sarett96be9082016-05-03 13:29:54 -04003249# define B_MASKS(d,s) { B_MASK(1,d,s), B_MASK(3,d,s), B_MASK(5,d,s) }
Chris Craikb50c2172013-07-29 15:28:30 -07003250
3251# define DEPTH_INDEX(d) ((d)==1?0:((d)==2?1:2))
3252
3253 /* Hence the pre-compiled masks indexed by PACKSWAP (or not), depth and
3254 * then pass:
3255 */
3256 static PNG_CONST png_uint_32 row_mask[2/*PACKSWAP*/][3/*depth*/][6] =
3257 {
3258 /* Little-endian byte masks for PACKSWAP */
3259 { S_MASKS(1,0), S_MASKS(2,0), S_MASKS(4,0) },
3260 /* Normal (big-endian byte) masks - PNG format */
3261 { S_MASKS(1,1), S_MASKS(2,1), S_MASKS(4,1) }
3262 };
3263
3264 /* display_mask has only three entries for the odd passes, so index by
3265 * pass>>1.
3266 */
3267 static PNG_CONST png_uint_32 display_mask[2][3][3] =
3268 {
3269 /* Little-endian byte masks for PACKSWAP */
3270 { B_MASKS(1,0), B_MASKS(2,0), B_MASKS(4,0) },
3271 /* Normal (big-endian byte) masks - PNG format */
3272 { B_MASKS(1,1), B_MASKS(2,1), B_MASKS(4,1) }
3273 };
3274
3275# define MASK(pass,depth,display,png)\
3276 ((display)?display_mask[png][DEPTH_INDEX(depth)][pass>>1]:\
3277 row_mask[png][DEPTH_INDEX(depth)][pass])
3278
3279#else /* !PNG_USE_COMPILE_TIME_MASKS */
3280 /* This is the runtime alternative: it seems unlikely that this will
3281 * ever be either smaller or faster than the compile time approach.
3282 */
3283# define MASK(pass,depth,display,png)\
3284 ((display)?B_MASK(pass,depth,png):S_MASK(pass,depth,png))
Matt Sarett96be9082016-05-03 13:29:54 -04003285#endif /* !USE_COMPILE_TIME_MASKS */
Chris Craikb50c2172013-07-29 15:28:30 -07003286
3287 /* Use the appropriate mask to copy the required bits. In some cases
Matt Sarett96be9082016-05-03 13:29:54 -04003288 * the byte mask will be 0 or 0xff; optimize these cases. row_width is
Chris Craikb50c2172013-07-29 15:28:30 -07003289 * the number of pixels, but the code copies bytes, so it is necessary
3290 * to special case the end.
3291 */
3292 png_uint_32 pixels_per_byte = 8 / pixel_depth;
3293 png_uint_32 mask;
3294
3295# ifdef PNG_READ_PACKSWAP_SUPPORTED
Matt Sarett96be9082016-05-03 13:29:54 -04003296 if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
3297 mask = MASK(pass, pixel_depth, display, 0);
Chris Craikb50c2172013-07-29 15:28:30 -07003298
Matt Sarett96be9082016-05-03 13:29:54 -04003299 else
Chris Craikb50c2172013-07-29 15:28:30 -07003300# endif
Matt Sarett96be9082016-05-03 13:29:54 -04003301 mask = MASK(pass, pixel_depth, display, 1);
The Android Open Source Project893912b2009-03-03 19:30:05 -08003302
Chris Craikb50c2172013-07-29 15:28:30 -07003303 for (;;)
The Android Open Source Project893912b2009-03-03 19:30:05 -08003304 {
Chris Craikb50c2172013-07-29 15:28:30 -07003305 png_uint_32 m;
The Android Open Source Project893912b2009-03-03 19:30:05 -08003306
Chris Craikb50c2172013-07-29 15:28:30 -07003307 /* It doesn't matter in the following if png_uint_32 has more than
3308 * 32 bits because the high bits always match those in m<<24; it is,
3309 * however, essential to use OR here, not +, because of this.
3310 */
3311 m = mask;
3312 mask = (m >> 8) | (m << 24); /* rotate right to good compilers */
3313 m &= 0xff;
3314
3315 if (m != 0) /* something to copy */
The Android Open Source Project893912b2009-03-03 19:30:05 -08003316 {
Chris Craikb50c2172013-07-29 15:28:30 -07003317 if (m != 0xff)
3318 *dp = (png_byte)((*dp & ~m) | (*sp & m));
The Android Open Source Project893912b2009-03-03 19:30:05 -08003319 else
Chris Craikb50c2172013-07-29 15:28:30 -07003320 *dp = *sp;
The Android Open Source Project893912b2009-03-03 19:30:05 -08003321 }
The Android Open Source Project893912b2009-03-03 19:30:05 -08003322
Chris Craikb50c2172013-07-29 15:28:30 -07003323 /* NOTE: this may overwrite the last byte with garbage if the image
3324 * is not an exact number of bytes wide; libpng has always done
3325 * this.
3326 */
3327 if (row_width <= pixels_per_byte)
3328 break; /* May need to restore part of the last byte */
The Android Open Source Project893912b2009-03-03 19:30:05 -08003329
Chris Craikb50c2172013-07-29 15:28:30 -07003330 row_width -= pixels_per_byte;
3331 ++dp;
3332 ++sp;
The Android Open Source Project893912b2009-03-03 19:30:05 -08003333 }
3334 }
Chris Craikb50c2172013-07-29 15:28:30 -07003335
3336 else /* pixel_depth >= 8 */
3337 {
3338 unsigned int bytes_to_copy, bytes_to_jump;
3339
3340 /* Validate the depth - it must be a multiple of 8 */
3341 if (pixel_depth & 7)
3342 png_error(png_ptr, "invalid user transform pixel depth");
3343
3344 pixel_depth >>= 3; /* now in bytes */
3345 row_width *= pixel_depth;
3346
3347 /* Regardless of pass number the Adam 7 interlace always results in a
3348 * fixed number of pixels to copy then to skip. There may be a
3349 * different number of pixels to skip at the start though.
3350 */
3351 {
3352 unsigned int offset = PNG_PASS_START_COL(pass) * pixel_depth;
3353
3354 row_width -= offset;
3355 dp += offset;
3356 sp += offset;
3357 }
3358
3359 /* Work out the bytes to copy. */
Matt Sarett96be9082016-05-03 13:29:54 -04003360 if (display != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07003361 {
3362 /* When doing the 'block' algorithm the pixel in the pass gets
3363 * replicated to adjacent pixels. This is why the even (0,2,4,6)
3364 * passes are skipped above - the entire expanded row is copied.
3365 */
3366 bytes_to_copy = (1<<((6-pass)>>1)) * pixel_depth;
3367
3368 /* But don't allow this number to exceed the actual row width. */
3369 if (bytes_to_copy > row_width)
Henrik Smiding82aab852015-02-20 13:01:11 +01003370 bytes_to_copy = (unsigned int)/*SAFE*/row_width;
Chris Craikb50c2172013-07-29 15:28:30 -07003371 }
3372
3373 else /* normal row; Adam7 only ever gives us one pixel to copy. */
3374 bytes_to_copy = pixel_depth;
3375
3376 /* In Adam7 there is a constant offset between where the pixels go. */
3377 bytes_to_jump = PNG_PASS_COL_OFFSET(pass) * pixel_depth;
3378
3379 /* And simply copy these bytes. Some optimization is possible here,
3380 * depending on the value of 'bytes_to_copy'. Special case the low
3381 * byte counts, which we know to be frequent.
3382 *
3383 * Notice that these cases all 'return' rather than 'break' - this
3384 * avoids an unnecessary test on whether to restore the last byte
3385 * below.
3386 */
3387 switch (bytes_to_copy)
3388 {
3389 case 1:
3390 for (;;)
3391 {
3392 *dp = *sp;
3393
3394 if (row_width <= bytes_to_jump)
3395 return;
3396
3397 dp += bytes_to_jump;
3398 sp += bytes_to_jump;
3399 row_width -= bytes_to_jump;
3400 }
3401
3402 case 2:
3403 /* There is a possibility of a partial copy at the end here; this
3404 * slows the code down somewhat.
3405 */
3406 do
3407 {
3408 dp[0] = sp[0], dp[1] = sp[1];
3409
3410 if (row_width <= bytes_to_jump)
3411 return;
3412
3413 sp += bytes_to_jump;
3414 dp += bytes_to_jump;
3415 row_width -= bytes_to_jump;
3416 }
3417 while (row_width > 1);
3418
3419 /* And there can only be one byte left at this point: */
3420 *dp = *sp;
3421 return;
3422
3423 case 3:
3424 /* This can only be the RGB case, so each copy is exactly one
3425 * pixel and it is not necessary to check for a partial copy.
3426 */
Matt Sarett96be9082016-05-03 13:29:54 -04003427 for (;;)
Chris Craikb50c2172013-07-29 15:28:30 -07003428 {
3429 dp[0] = sp[0], dp[1] = sp[1], dp[2] = sp[2];
3430
3431 if (row_width <= bytes_to_jump)
3432 return;
3433
3434 sp += bytes_to_jump;
3435 dp += bytes_to_jump;
3436 row_width -= bytes_to_jump;
3437 }
3438
3439 default:
3440#if PNG_ALIGN_TYPE != PNG_ALIGN_NONE
3441 /* Check for double byte alignment and, if possible, use a
3442 * 16-bit copy. Don't attempt this for narrow images - ones that
3443 * are less than an interlace panel wide. Don't attempt it for
3444 * wide bytes_to_copy either - use the memcpy there.
3445 */
3446 if (bytes_to_copy < 16 /*else use memcpy*/ &&
Matt Sarett96be9082016-05-03 13:29:54 -04003447 png_isaligned(dp, png_uint_16) &&
3448 png_isaligned(sp, png_uint_16) &&
3449 bytes_to_copy % (sizeof (png_uint_16)) == 0 &&
3450 bytes_to_jump % (sizeof (png_uint_16)) == 0)
Chris Craikb50c2172013-07-29 15:28:30 -07003451 {
3452 /* Everything is aligned for png_uint_16 copies, but try for
3453 * png_uint_32 first.
3454 */
Matt Sarett96be9082016-05-03 13:29:54 -04003455 if (png_isaligned(dp, png_uint_32) != 0 &&
3456 png_isaligned(sp, png_uint_32) != 0 &&
3457 bytes_to_copy % (sizeof (png_uint_32)) == 0 &&
3458 bytes_to_jump % (sizeof (png_uint_32)) == 0)
Chris Craikb50c2172013-07-29 15:28:30 -07003459 {
3460 png_uint_32p dp32 = png_aligncast(png_uint_32p,dp);
3461 png_const_uint_32p sp32 = png_aligncastconst(
Matt Sarett96be9082016-05-03 13:29:54 -04003462 png_const_uint_32p, sp);
Chris Craikb50c2172013-07-29 15:28:30 -07003463 size_t skip = (bytes_to_jump-bytes_to_copy) /
Matt Sarett96be9082016-05-03 13:29:54 -04003464 (sizeof (png_uint_32));
Chris Craikb50c2172013-07-29 15:28:30 -07003465
3466 do
3467 {
3468 size_t c = bytes_to_copy;
3469 do
3470 {
3471 *dp32++ = *sp32++;
3472 c -= (sizeof (png_uint_32));
3473 }
3474 while (c > 0);
3475
3476 if (row_width <= bytes_to_jump)
3477 return;
3478
3479 dp32 += skip;
3480 sp32 += skip;
3481 row_width -= bytes_to_jump;
3482 }
3483 while (bytes_to_copy <= row_width);
3484
3485 /* Get to here when the row_width truncates the final copy.
3486 * There will be 1-3 bytes left to copy, so don't try the
3487 * 16-bit loop below.
3488 */
3489 dp = (png_bytep)dp32;
3490 sp = (png_const_bytep)sp32;
3491 do
3492 *dp++ = *sp++;
3493 while (--row_width > 0);
3494 return;
3495 }
3496
3497 /* Else do it in 16-bit quantities, but only if the size is
3498 * not too large.
3499 */
3500 else
3501 {
3502 png_uint_16p dp16 = png_aligncast(png_uint_16p, dp);
3503 png_const_uint_16p sp16 = png_aligncastconst(
3504 png_const_uint_16p, sp);
3505 size_t skip = (bytes_to_jump-bytes_to_copy) /
3506 (sizeof (png_uint_16));
3507
3508 do
3509 {
3510 size_t c = bytes_to_copy;
3511 do
3512 {
3513 *dp16++ = *sp16++;
3514 c -= (sizeof (png_uint_16));
3515 }
3516 while (c > 0);
3517
3518 if (row_width <= bytes_to_jump)
3519 return;
3520
3521 dp16 += skip;
3522 sp16 += skip;
3523 row_width -= bytes_to_jump;
3524 }
3525 while (bytes_to_copy <= row_width);
3526
3527 /* End of row - 1 byte left, bytes_to_copy > row_width: */
3528 dp = (png_bytep)dp16;
3529 sp = (png_const_bytep)sp16;
3530 do
3531 *dp++ = *sp++;
3532 while (--row_width > 0);
3533 return;
3534 }
3535 }
Matt Sarett96be9082016-05-03 13:29:54 -04003536#endif /* ALIGN_TYPE code */
Chris Craikb50c2172013-07-29 15:28:30 -07003537
3538 /* The true default - use a memcpy: */
3539 for (;;)
3540 {
3541 memcpy(dp, sp, bytes_to_copy);
3542
3543 if (row_width <= bytes_to_jump)
3544 return;
3545
3546 sp += bytes_to_jump;
3547 dp += bytes_to_jump;
3548 row_width -= bytes_to_jump;
3549 if (bytes_to_copy > row_width)
Henrik Smiding82aab852015-02-20 13:01:11 +01003550 bytes_to_copy = (unsigned int)/*SAFE*/row_width;
Chris Craikb50c2172013-07-29 15:28:30 -07003551 }
3552 }
3553
3554 /* NOT REACHED*/
3555 } /* pixel_depth >= 8 */
3556
3557 /* Here if pixel_depth < 8 to check 'end_ptr' below. */
The Android Open Source Project893912b2009-03-03 19:30:05 -08003558 }
Chris Craikb50c2172013-07-29 15:28:30 -07003559 else
Matt Sarett96be9082016-05-03 13:29:54 -04003560#endif /* READ_INTERLACING */
Chris Craikb50c2172013-07-29 15:28:30 -07003561
3562 /* If here then the switch above wasn't used so just memcpy the whole row
3563 * from the temporary row buffer (notice that this overwrites the end of the
3564 * destination row if it is a partial byte.)
3565 */
3566 memcpy(dp, sp, PNG_ROWBYTES(pixel_depth, row_width));
3567
3568 /* Restore the overwritten bits from the last byte if necessary. */
3569 if (end_ptr != NULL)
3570 *end_ptr = (png_byte)((end_byte & end_mask) | (*end_ptr & ~end_mask));
The Android Open Source Project893912b2009-03-03 19:30:05 -08003571}
3572
3573#ifdef PNG_READ_INTERLACING_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08003574void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07003575png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
3576 png_uint_32 transformations /* Because these may affect the byte layout */)
The Android Open Source Project893912b2009-03-03 19:30:05 -08003577{
Patrick Scotta0bb96c2009-07-22 11:50:02 -04003578 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
3579 /* Offset to next interlace block */
Chris Craikb50c2172013-07-29 15:28:30 -07003580 static PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
The Android Open Source Project893912b2009-03-03 19:30:05 -08003581
The Android Open Source Project4215dd12009-03-09 11:52:12 -07003582 png_debug(1, "in png_do_read_interlace");
The Android Open Source Project893912b2009-03-03 19:30:05 -08003583 if (row != NULL && row_info != NULL)
3584 {
3585 png_uint_32 final_width;
3586
3587 final_width = row_info->width * png_pass_inc[pass];
3588
3589 switch (row_info->pixel_depth)
3590 {
3591 case 1:
3592 {
3593 png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3);
3594 png_bytep dp = row + (png_size_t)((final_width - 1) >> 3);
3595 int sshift, dshift;
3596 int s_start, s_end, s_inc;
3597 int jstop = png_pass_inc[pass];
3598 png_byte v;
3599 png_uint_32 i;
3600 int j;
3601
Patrick Scott5f6bd842010-06-28 16:55:16 -04003602#ifdef PNG_READ_PACKSWAP_SUPPORTED
Matt Sarett96be9082016-05-03 13:29:54 -04003603 if ((transformations & PNG_PACKSWAP) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08003604 {
3605 sshift = (int)((row_info->width + 7) & 0x07);
3606 dshift = (int)((final_width + 7) & 0x07);
3607 s_start = 7;
3608 s_end = 0;
3609 s_inc = -1;
3610 }
Chris Craikb50c2172013-07-29 15:28:30 -07003611
The Android Open Source Project893912b2009-03-03 19:30:05 -08003612 else
3613#endif
3614 {
3615 sshift = 7 - (int)((row_info->width + 7) & 0x07);
3616 dshift = 7 - (int)((final_width + 7) & 0x07);
3617 s_start = 0;
3618 s_end = 7;
3619 s_inc = 1;
3620 }
3621
3622 for (i = 0; i < row_info->width; i++)
3623 {
3624 v = (png_byte)((*sp >> sshift) & 0x01);
3625 for (j = 0; j < jstop; j++)
3626 {
Chris Craikb50c2172013-07-29 15:28:30 -07003627 unsigned int tmp = *dp & (0x7f7f >> (7 - dshift));
3628 tmp |= v << dshift;
3629 *dp = (png_byte)(tmp & 0xff);
3630
The Android Open Source Project893912b2009-03-03 19:30:05 -08003631 if (dshift == s_end)
3632 {
3633 dshift = s_start;
3634 dp--;
3635 }
Chris Craikb50c2172013-07-29 15:28:30 -07003636
The Android Open Source Project893912b2009-03-03 19:30:05 -08003637 else
3638 dshift += s_inc;
3639 }
Chris Craikb50c2172013-07-29 15:28:30 -07003640
The Android Open Source Project893912b2009-03-03 19:30:05 -08003641 if (sshift == s_end)
3642 {
3643 sshift = s_start;
3644 sp--;
3645 }
Chris Craikb50c2172013-07-29 15:28:30 -07003646
The Android Open Source Project893912b2009-03-03 19:30:05 -08003647 else
3648 sshift += s_inc;
3649 }
3650 break;
3651 }
Chris Craikb50c2172013-07-29 15:28:30 -07003652
The Android Open Source Project893912b2009-03-03 19:30:05 -08003653 case 2:
3654 {
3655 png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);
3656 png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);
3657 int sshift, dshift;
3658 int s_start, s_end, s_inc;
3659 int jstop = png_pass_inc[pass];
3660 png_uint_32 i;
3661
Patrick Scott5f6bd842010-06-28 16:55:16 -04003662#ifdef PNG_READ_PACKSWAP_SUPPORTED
Matt Sarett96be9082016-05-03 13:29:54 -04003663 if ((transformations & PNG_PACKSWAP) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08003664 {
3665 sshift = (int)(((row_info->width + 3) & 0x03) << 1);
3666 dshift = (int)(((final_width + 3) & 0x03) << 1);
3667 s_start = 6;
3668 s_end = 0;
3669 s_inc = -2;
3670 }
Chris Craikb50c2172013-07-29 15:28:30 -07003671
The Android Open Source Project893912b2009-03-03 19:30:05 -08003672 else
3673#endif
3674 {
3675 sshift = (int)((3 - ((row_info->width + 3) & 0x03)) << 1);
3676 dshift = (int)((3 - ((final_width + 3) & 0x03)) << 1);
3677 s_start = 0;
3678 s_end = 6;
3679 s_inc = 2;
3680 }
3681
3682 for (i = 0; i < row_info->width; i++)
3683 {
3684 png_byte v;
3685 int j;
3686
3687 v = (png_byte)((*sp >> sshift) & 0x03);
3688 for (j = 0; j < jstop; j++)
3689 {
Chris Craikb50c2172013-07-29 15:28:30 -07003690 unsigned int tmp = *dp & (0x3f3f >> (6 - dshift));
3691 tmp |= v << dshift;
3692 *dp = (png_byte)(tmp & 0xff);
3693
The Android Open Source Project893912b2009-03-03 19:30:05 -08003694 if (dshift == s_end)
3695 {
3696 dshift = s_start;
3697 dp--;
3698 }
Chris Craikb50c2172013-07-29 15:28:30 -07003699
The Android Open Source Project893912b2009-03-03 19:30:05 -08003700 else
3701 dshift += s_inc;
3702 }
Chris Craikb50c2172013-07-29 15:28:30 -07003703
The Android Open Source Project893912b2009-03-03 19:30:05 -08003704 if (sshift == s_end)
3705 {
3706 sshift = s_start;
3707 sp--;
3708 }
Chris Craikb50c2172013-07-29 15:28:30 -07003709
The Android Open Source Project893912b2009-03-03 19:30:05 -08003710 else
3711 sshift += s_inc;
3712 }
3713 break;
3714 }
Chris Craikb50c2172013-07-29 15:28:30 -07003715
The Android Open Source Project893912b2009-03-03 19:30:05 -08003716 case 4:
3717 {
3718 png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1);
3719 png_bytep dp = row + (png_size_t)((final_width - 1) >> 1);
3720 int sshift, dshift;
3721 int s_start, s_end, s_inc;
3722 png_uint_32 i;
3723 int jstop = png_pass_inc[pass];
3724
Patrick Scott5f6bd842010-06-28 16:55:16 -04003725#ifdef PNG_READ_PACKSWAP_SUPPORTED
Matt Sarett96be9082016-05-03 13:29:54 -04003726 if ((transformations & PNG_PACKSWAP) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08003727 {
3728 sshift = (int)(((row_info->width + 1) & 0x01) << 2);
3729 dshift = (int)(((final_width + 1) & 0x01) << 2);
3730 s_start = 4;
3731 s_end = 0;
3732 s_inc = -4;
3733 }
Chris Craikb50c2172013-07-29 15:28:30 -07003734
The Android Open Source Project893912b2009-03-03 19:30:05 -08003735 else
3736#endif
3737 {
3738 sshift = (int)((1 - ((row_info->width + 1) & 0x01)) << 2);
3739 dshift = (int)((1 - ((final_width + 1) & 0x01)) << 2);
3740 s_start = 0;
3741 s_end = 4;
3742 s_inc = 4;
3743 }
3744
3745 for (i = 0; i < row_info->width; i++)
3746 {
Chris Craikb50c2172013-07-29 15:28:30 -07003747 png_byte v = (png_byte)((*sp >> sshift) & 0x0f);
The Android Open Source Project893912b2009-03-03 19:30:05 -08003748 int j;
3749
3750 for (j = 0; j < jstop; j++)
3751 {
Chris Craikb50c2172013-07-29 15:28:30 -07003752 unsigned int tmp = *dp & (0xf0f >> (4 - dshift));
3753 tmp |= v << dshift;
3754 *dp = (png_byte)(tmp & 0xff);
3755
The Android Open Source Project893912b2009-03-03 19:30:05 -08003756 if (dshift == s_end)
3757 {
3758 dshift = s_start;
3759 dp--;
3760 }
Chris Craikb50c2172013-07-29 15:28:30 -07003761
The Android Open Source Project893912b2009-03-03 19:30:05 -08003762 else
3763 dshift += s_inc;
3764 }
Chris Craikb50c2172013-07-29 15:28:30 -07003765
The Android Open Source Project893912b2009-03-03 19:30:05 -08003766 if (sshift == s_end)
3767 {
3768 sshift = s_start;
3769 sp--;
3770 }
Chris Craikb50c2172013-07-29 15:28:30 -07003771
The Android Open Source Project893912b2009-03-03 19:30:05 -08003772 else
3773 sshift += s_inc;
3774 }
3775 break;
3776 }
Chris Craikb50c2172013-07-29 15:28:30 -07003777
The Android Open Source Project893912b2009-03-03 19:30:05 -08003778 default:
3779 {
3780 png_size_t pixel_bytes = (row_info->pixel_depth >> 3);
Chris Craikb50c2172013-07-29 15:28:30 -07003781
Patrick Scott5f6bd842010-06-28 16:55:16 -04003782 png_bytep sp = row + (png_size_t)(row_info->width - 1)
3783 * pixel_bytes;
Chris Craikb50c2172013-07-29 15:28:30 -07003784
The Android Open Source Project893912b2009-03-03 19:30:05 -08003785 png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes;
3786
3787 int jstop = png_pass_inc[pass];
3788 png_uint_32 i;
3789
3790 for (i = 0; i < row_info->width; i++)
3791 {
Chris Craikb50c2172013-07-29 15:28:30 -07003792 png_byte v[8]; /* SAFE; pixel_depth does not exceed 64 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08003793 int j;
3794
Chris Craikb50c2172013-07-29 15:28:30 -07003795 memcpy(v, sp, pixel_bytes);
3796
The Android Open Source Project893912b2009-03-03 19:30:05 -08003797 for (j = 0; j < jstop; j++)
3798 {
Chris Craikb50c2172013-07-29 15:28:30 -07003799 memcpy(dp, v, pixel_bytes);
The Android Open Source Project893912b2009-03-03 19:30:05 -08003800 dp -= pixel_bytes;
3801 }
Chris Craikb50c2172013-07-29 15:28:30 -07003802
The Android Open Source Project893912b2009-03-03 19:30:05 -08003803 sp -= pixel_bytes;
3804 }
3805 break;
3806 }
3807 }
Chris Craikb50c2172013-07-29 15:28:30 -07003808
The Android Open Source Project893912b2009-03-03 19:30:05 -08003809 row_info->width = final_width;
The Android Open Source Project4215dd12009-03-09 11:52:12 -07003810 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width);
The Android Open Source Project893912b2009-03-03 19:30:05 -08003811 }
Patrick Scott5f6bd842010-06-28 16:55:16 -04003812#ifndef PNG_READ_PACKSWAP_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07003813 PNG_UNUSED(transformations) /* Silence compiler warning */
The Android Open Source Project893912b2009-03-03 19:30:05 -08003814#endif
3815}
Matt Sarett96be9082016-05-03 13:29:54 -04003816#endif /* READ_INTERLACING */
The Android Open Source Project893912b2009-03-03 19:30:05 -08003817
Chris Craikb50c2172013-07-29 15:28:30 -07003818static void
3819png_read_filter_row_sub(png_row_infop row_info, png_bytep row,
3820 png_const_bytep prev_row)
The Android Open Source Project893912b2009-03-03 19:30:05 -08003821{
Chris Craikb50c2172013-07-29 15:28:30 -07003822 png_size_t i;
3823 png_size_t istop = row_info->rowbytes;
3824 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
3825 png_bytep rp = row + bpp;
3826
3827 PNG_UNUSED(prev_row)
3828
3829 for (i = bpp; i < istop; i++)
The Android Open Source Project893912b2009-03-03 19:30:05 -08003830 {
Chris Craikb50c2172013-07-29 15:28:30 -07003831 *rp = (png_byte)(((int)(*rp) + (int)(*(rp-bpp))) & 0xff);
3832 rp++;
The Android Open Source Project893912b2009-03-03 19:30:05 -08003833 }
3834}
3835
Chris Craikb50c2172013-07-29 15:28:30 -07003836static void
3837png_read_filter_row_up(png_row_infop row_info, png_bytep row,
3838 png_const_bytep prev_row)
Joseph Wen4ce0ee12010-08-20 10:42:22 +08003839{
Chris Craikb50c2172013-07-29 15:28:30 -07003840 png_size_t i;
3841 png_size_t istop = row_info->rowbytes;
3842 png_bytep rp = row;
3843 png_const_bytep pp = prev_row;
Joseph Wen4ce0ee12010-08-20 10:42:22 +08003844
Chris Craikb50c2172013-07-29 15:28:30 -07003845 for (i = 0; i < istop; i++)
3846 {
3847 *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
3848 rp++;
3849 }
Joseph Wen4ce0ee12010-08-20 10:42:22 +08003850}
Chris Craikb50c2172013-07-29 15:28:30 -07003851
3852static void
3853png_read_filter_row_avg(png_row_infop row_info, png_bytep row,
3854 png_const_bytep prev_row)
3855{
3856 png_size_t i;
3857 png_bytep rp = row;
3858 png_const_bytep pp = prev_row;
3859 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
3860 png_size_t istop = row_info->rowbytes - bpp;
3861
3862 for (i = 0; i < bpp; i++)
3863 {
3864 *rp = (png_byte)(((int)(*rp) +
3865 ((int)(*pp++) / 2 )) & 0xff);
3866
3867 rp++;
3868 }
3869
3870 for (i = 0; i < istop; i++)
3871 {
3872 *rp = (png_byte)(((int)(*rp) +
3873 (int)(*pp++ + *(rp-bpp)) / 2 ) & 0xff);
3874
3875 rp++;
3876 }
3877}
3878
3879static void
3880png_read_filter_row_paeth_1byte_pixel(png_row_infop row_info, png_bytep row,
3881 png_const_bytep prev_row)
3882{
3883 png_bytep rp_end = row + row_info->rowbytes;
3884 int a, c;
3885
3886 /* First pixel/byte */
3887 c = *prev_row++;
3888 a = *row + c;
3889 *row++ = (png_byte)a;
3890
3891 /* Remainder */
3892 while (row < rp_end)
3893 {
3894 int b, pa, pb, pc, p;
3895
3896 a &= 0xff; /* From previous iteration or start */
3897 b = *prev_row++;
3898
3899 p = b - c;
3900 pc = a - c;
3901
Matt Sarett96be9082016-05-03 13:29:54 -04003902#ifdef PNG_USE_ABS
3903 pa = abs(p);
3904 pb = abs(pc);
3905 pc = abs(p + pc);
3906#else
3907 pa = p < 0 ? -p : p;
3908 pb = pc < 0 ? -pc : pc;
3909 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
3910#endif
Chris Craikb50c2172013-07-29 15:28:30 -07003911
3912 /* Find the best predictor, the least of pa, pb, pc favoring the earlier
3913 * ones in the case of a tie.
3914 */
3915 if (pb < pa) pa = pb, a = b;
3916 if (pc < pa) a = c;
3917
3918 /* Calculate the current pixel in a, and move the previous row pixel to c
3919 * for the next time round the loop
3920 */
3921 c = b;
3922 a += *row;
3923 *row++ = (png_byte)a;
3924 }
3925}
3926
3927static void
3928png_read_filter_row_paeth_multibyte_pixel(png_row_infop row_info, png_bytep row,
3929 png_const_bytep prev_row)
3930{
3931 int bpp = (row_info->pixel_depth + 7) >> 3;
3932 png_bytep rp_end = row + bpp;
3933
3934 /* Process the first pixel in the row completely (this is the same as 'up'
3935 * because there is only one candidate predictor for the first row).
3936 */
3937 while (row < rp_end)
3938 {
3939 int a = *row + *prev_row++;
3940 *row++ = (png_byte)a;
3941 }
3942
3943 /* Remainder */
3944 rp_end += row_info->rowbytes - bpp;
3945
3946 while (row < rp_end)
3947 {
3948 int a, b, c, pa, pb, pc, p;
3949
3950 c = *(prev_row - bpp);
3951 a = *(row - bpp);
3952 b = *prev_row++;
3953
3954 p = b - c;
3955 pc = a - c;
3956
Matt Sarett96be9082016-05-03 13:29:54 -04003957#ifdef PNG_USE_ABS
3958 pa = abs(p);
3959 pb = abs(pc);
3960 pc = abs(p + pc);
3961#else
3962 pa = p < 0 ? -p : p;
3963 pb = pc < 0 ? -pc : pc;
3964 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
3965#endif
Chris Craikb50c2172013-07-29 15:28:30 -07003966
3967 if (pb < pa) pa = pb, a = b;
3968 if (pc < pa) a = c;
3969
Chris Craikb50c2172013-07-29 15:28:30 -07003970 a += *row;
3971 *row++ = (png_byte)a;
3972 }
3973}
3974
3975static void
3976png_init_filter_functions(png_structrp pp)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05303977 /* This function is called once for every PNG image (except for PNG images
3978 * that only use PNG_FILTER_VALUE_NONE for all rows) to set the
Chris Craikb50c2172013-07-29 15:28:30 -07003979 * implementations required to reverse the filtering of PNG rows. Reversing
3980 * the filter is the first transformation performed on the row data. It is
3981 * performed in place, therefore an implementation can be selected based on
3982 * the image pixel format. If the implementation depends on image width then
3983 * take care to ensure that it works correctly if the image is interlaced -
3984 * interlacing causes the actual row width to vary.
3985 */
3986{
3987 unsigned int bpp = (pp->pixel_depth + 7) >> 3;
3988
3989 pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub;
3990 pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up;
3991 pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg;
3992 if (bpp == 1)
3993 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
3994 png_read_filter_row_paeth_1byte_pixel;
3995 else
3996 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
3997 png_read_filter_row_paeth_multibyte_pixel;
3998
3999#ifdef PNG_FILTER_OPTIMIZATIONS
4000 /* To use this define PNG_FILTER_OPTIMIZATIONS as the name of a function to
4001 * call to install hardware optimizations for the above functions; simply
4002 * replace whatever elements of the pp->read_filter[] array with a hardware
4003 * specific (or, for that matter, generic) optimization.
4004 *
4005 * To see an example of this examine what configure.ac does when
4006 * --enable-arm-neon is specified on the command line.
4007 */
4008 PNG_FILTER_OPTIMIZATIONS(pp, bpp);
Joseph Wen4ce0ee12010-08-20 10:42:22 +08004009#endif
Chris Craikb50c2172013-07-29 15:28:30 -07004010}
4011
4012void /* PRIVATE */
4013png_read_filter_row(png_structrp pp, png_row_infop row_info, png_bytep row,
4014 png_const_bytep prev_row, int filter)
4015{
4016 /* OPTIMIZATION: DO NOT MODIFY THIS FUNCTION, instead #define
4017 * PNG_FILTER_OPTIMIZATIONS to a function that overrides the generic
4018 * implementations. See png_init_filter_functions above.
4019 */
Chris Craikb50c2172013-07-29 15:28:30 -07004020 if (filter > PNG_FILTER_VALUE_NONE && filter < PNG_FILTER_VALUE_LAST)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05304021 {
4022 if (pp->read_filter[0] == NULL)
4023 png_init_filter_functions(pp);
4024
Chris Craikb50c2172013-07-29 15:28:30 -07004025 pp->read_filter[filter-1](row_info, row, prev_row);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05304026 }
Chris Craikb50c2172013-07-29 15:28:30 -07004027}
Joseph Wen4ce0ee12010-08-20 10:42:22 +08004028
Patrick Scott5f6bd842010-06-28 16:55:16 -04004029#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08004030void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07004031png_read_IDAT_data(png_structrp png_ptr, png_bytep output,
4032 png_alloc_size_t avail_out)
4033{
4034 /* Loop reading IDATs and decompressing the result into output[avail_out] */
4035 png_ptr->zstream.next_out = output;
4036 png_ptr->zstream.avail_out = 0; /* safety: set below */
4037
4038 if (output == NULL)
4039 avail_out = 0;
4040
4041 do
4042 {
4043 int ret;
4044 png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];
4045
4046 if (png_ptr->zstream.avail_in == 0)
4047 {
4048 uInt avail_in;
4049 png_bytep buffer;
4050
4051 while (png_ptr->idat_size == 0)
4052 {
Sireesh Tripurarib0277d02014-05-09 15:39:12 +05304053#ifdef PNG_INDEX_SUPPORTED
4054 if (png_ptr->index) {
4055 png_opt_crc_finish(png_ptr, 0);
4056 png_ptr->index->stream_idat_position = png_ptr->total_data_read;
4057 } else
4058#endif
Chris Craikb50c2172013-07-29 15:28:30 -07004059 png_crc_finish(png_ptr, 0);
4060
4061 png_ptr->idat_size = png_read_chunk_header(png_ptr);
4062 /* This is an error even in the 'check' case because the code just
4063 * consumed a non-IDAT header.
4064 */
4065 if (png_ptr->chunk_name != png_IDAT)
4066 png_error(png_ptr, "Not enough image data");
4067 }
4068
4069 avail_in = png_ptr->IDAT_read_size;
4070
4071 if (avail_in > png_ptr->idat_size)
4072 avail_in = (uInt)png_ptr->idat_size;
4073
4074 /* A PNG with a gradually increasing IDAT size will defeat this attempt
4075 * to minimize memory usage by causing lots of re-allocs, but
4076 * realistically doing IDAT_read_size re-allocs is not likely to be a
4077 * big problem.
4078 */
4079 buffer = png_read_buffer(png_ptr, avail_in, 0/*error*/);
4080
4081 png_crc_read(png_ptr, buffer, avail_in);
4082 png_ptr->idat_size -= avail_in;
4083
4084 png_ptr->zstream.next_in = buffer;
4085 png_ptr->zstream.avail_in = avail_in;
4086 }
4087
4088 /* And set up the output side. */
4089 if (output != NULL) /* standard read */
4090 {
4091 uInt out = ZLIB_IO_MAX;
4092
4093 if (out > avail_out)
4094 out = (uInt)avail_out;
4095
4096 avail_out -= out;
4097 png_ptr->zstream.avail_out = out;
4098 }
4099
4100 else /* after last row, checking for end */
4101 {
4102 png_ptr->zstream.next_out = tmpbuf;
4103 png_ptr->zstream.avail_out = (sizeof tmpbuf);
4104 }
4105
4106 /* Use NO_FLUSH; this gives zlib the maximum opportunity to optimize the
4107 * process. If the LZ stream is truncated the sequential reader will
4108 * terminally damage the stream, above, by reading the chunk header of the
4109 * following chunk (it then exits with png_error).
4110 *
4111 * TODO: deal more elegantly with truncated IDAT lists.
4112 */
Matt Sarett96be9082016-05-03 13:29:54 -04004113 ret = PNG_INFLATE(png_ptr, Z_NO_FLUSH);
Chris Craikb50c2172013-07-29 15:28:30 -07004114
4115 /* Take the unconsumed output back. */
4116 if (output != NULL)
4117 avail_out += png_ptr->zstream.avail_out;
4118
4119 else /* avail_out counts the extra bytes */
4120 avail_out += (sizeof tmpbuf) - png_ptr->zstream.avail_out;
4121
4122 png_ptr->zstream.avail_out = 0;
4123
4124 if (ret == Z_STREAM_END)
4125 {
4126 /* Do this for safety; we won't read any more into this row. */
4127 png_ptr->zstream.next_out = NULL;
4128
4129 png_ptr->mode |= PNG_AFTER_IDAT;
4130 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
4131
4132 if (png_ptr->zstream.avail_in > 0 || png_ptr->idat_size > 0)
4133 png_chunk_benign_error(png_ptr, "Extra compressed data");
4134 break;
4135 }
4136
Matt Sarett96be9082016-05-03 13:29:54 -04004137 if (ret != Z_OK) {
Sireesh Tripurarib0277d02014-05-09 15:39:12 +05304138#ifdef PNG_INDEX_SUPPORTED
Matt Sarett96be9082016-05-03 13:29:54 -04004139 if (png_ptr->index) {
4140 if (png_ptr->row_number != png_ptr->height - 1) {
4141 png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg : "Decompression error");
4142 }
4143 } else
Sireesh Tripurarib0277d02014-05-09 15:39:12 +05304144#endif
Chris Craikb50c2172013-07-29 15:28:30 -07004145 {
4146 png_zstream_error(png_ptr, ret);
4147
4148 if (output != NULL)
4149 png_chunk_error(png_ptr, png_ptr->zstream.msg);
4150
4151 else /* checking */
4152 {
4153 png_chunk_benign_error(png_ptr, png_ptr->zstream.msg);
4154 return;
4155 }
4156 }
Matt Sarett96be9082016-05-03 13:29:54 -04004157 }
Chris Craikb50c2172013-07-29 15:28:30 -07004158 } while (avail_out > 0);
4159
4160 if (avail_out > 0)
4161 {
4162 /* The stream ended before the image; this is the same as too few IDATs so
4163 * should be handled the same way.
4164 */
4165 if (output != NULL)
4166 png_error(png_ptr, "Not enough image data");
4167
4168 else /* the deflate stream contained extra data */
4169 png_chunk_benign_error(png_ptr, "Too much image data");
4170 }
4171}
4172
4173void /* PRIVATE */
4174png_read_finish_IDAT(png_structrp png_ptr)
4175{
4176 /* We don't need any more data and the stream should have ended, however the
4177 * LZ end code may actually not have been processed. In this case we must
4178 * read it otherwise stray unread IDAT data or, more likely, an IDAT chunk
4179 * may still remain to be consumed.
4180 */
Matt Sarett96be9082016-05-03 13:29:54 -04004181 if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
Chris Craikb50c2172013-07-29 15:28:30 -07004182 {
4183 /* The NULL causes png_read_IDAT_data to swallow any remaining bytes in
4184 * the compressed stream, but the stream may be damaged too, so even after
4185 * this call we may need to terminate the zstream ownership.
4186 */
4187 png_read_IDAT_data(png_ptr, NULL, 0);
4188 png_ptr->zstream.next_out = NULL; /* safety */
4189
4190 /* Now clear everything out for safety; the following may not have been
4191 * done.
4192 */
Matt Sarett96be9082016-05-03 13:29:54 -04004193 if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
Chris Craikb50c2172013-07-29 15:28:30 -07004194 {
4195 png_ptr->mode |= PNG_AFTER_IDAT;
4196 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
4197 }
4198 }
4199
4200 /* If the zstream has not been released do it now *and* terminate the reading
4201 * of the final IDAT chunk.
4202 */
4203 if (png_ptr->zowner == png_IDAT)
4204 {
4205 /* Always do this; the pointers otherwise point into the read buffer. */
4206 png_ptr->zstream.next_in = NULL;
4207 png_ptr->zstream.avail_in = 0;
4208
4209 /* Now we no longer own the zstream. */
4210 png_ptr->zowner = 0;
4211
4212 /* The slightly weird semantics of the sequential IDAT reading is that we
4213 * are always in or at the end of an IDAT chunk, so we always need to do a
4214 * crc_finish here. If idat_size is non-zero we also need to read the
4215 * spurious bytes at the end of the chunk now.
4216 */
Matt Sarett96be9082016-05-03 13:29:54 -04004217#ifdef PNG_INDEX_SUPPORTED
4218 if (png_ptr->index)
4219 {
4220 (void)png_opt_crc_finish(png_ptr, png_ptr->idat_size);
4221 png_ptr->index->stream_idat_position = png_ptr->total_data_read;
4222 }
4223 else
4224#endif
Chris Craikb50c2172013-07-29 15:28:30 -07004225 (void)png_crc_finish(png_ptr, png_ptr->idat_size);
4226 }
4227}
4228
Sireesh Tripurarib0277d02014-05-09 15:39:12 +05304229#ifdef PNG_INDEX_SUPPORTED
4230void /* PRIVATE */
4231png_set_interlaced_pass(png_structp png_ptr, int pass)
4232{
4233 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
4234 /* Start of interlace block */
4235 PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
4236 /* Offset to next interlace block */
4237 PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
4238 /* Start of interlace block in the y direction */
4239 PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
4240 /* Offset to next interlace block in the y direction */
4241 PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
4242 png_ptr->pass = pass;
4243 png_ptr->iwidth = (png_ptr->width +
4244 png_pass_inc[png_ptr->pass] - 1 -
4245 png_pass_start[png_ptr->pass]) /
4246 png_pass_inc[png_ptr->pass];
4247}
4248#endif
4249
Chris Craikb50c2172013-07-29 15:28:30 -07004250void /* PRIVATE */
4251png_read_finish_row(png_structrp png_ptr)
The Android Open Source Project893912b2009-03-03 19:30:05 -08004252{
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004253 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
The Android Open Source Project893912b2009-03-03 19:30:05 -08004254
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004255 /* Start of interlace block */
Chris Craikb50c2172013-07-29 15:28:30 -07004256 static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
The Android Open Source Project893912b2009-03-03 19:30:05 -08004257
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004258 /* Offset to next interlace block */
Chris Craikb50c2172013-07-29 15:28:30 -07004259 static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
The Android Open Source Project893912b2009-03-03 19:30:05 -08004260
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004261 /* Start of interlace block in the y direction */
Chris Craikb50c2172013-07-29 15:28:30 -07004262 static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
The Android Open Source Project893912b2009-03-03 19:30:05 -08004263
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004264 /* Offset to next interlace block in the y direction */
Chris Craikb50c2172013-07-29 15:28:30 -07004265 static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
The Android Open Source Project893912b2009-03-03 19:30:05 -08004266
The Android Open Source Project4215dd12009-03-09 11:52:12 -07004267 png_debug(1, "in png_read_finish_row");
The Android Open Source Project893912b2009-03-03 19:30:05 -08004268 png_ptr->row_number++;
4269 if (png_ptr->row_number < png_ptr->num_rows)
4270 return;
4271
Matt Sarett96be9082016-05-03 13:29:54 -04004272 if (png_ptr->interlaced != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08004273 {
4274 png_ptr->row_number = 0;
Chris Craikb50c2172013-07-29 15:28:30 -07004275
4276 /* TO DO: don't do this if prev_row isn't needed (requires
4277 * read-ahead of the next row's filter byte.
4278 */
4279 memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
4280
The Android Open Source Project893912b2009-03-03 19:30:05 -08004281 do
4282 {
4283 png_ptr->pass++;
Chris Craikb50c2172013-07-29 15:28:30 -07004284
The Android Open Source Project893912b2009-03-03 19:30:05 -08004285 if (png_ptr->pass >= 7)
4286 break;
Chris Craikb50c2172013-07-29 15:28:30 -07004287
The Android Open Source Project893912b2009-03-03 19:30:05 -08004288 png_ptr->iwidth = (png_ptr->width +
4289 png_pass_inc[png_ptr->pass] - 1 -
4290 png_pass_start[png_ptr->pass]) /
4291 png_pass_inc[png_ptr->pass];
4292
Matt Sarett96be9082016-05-03 13:29:54 -04004293 if ((png_ptr->transformations & PNG_INTERLACE) == 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08004294 {
4295 png_ptr->num_rows = (png_ptr->height +
Chris Craikb50c2172013-07-29 15:28:30 -07004296 png_pass_yinc[png_ptr->pass] - 1 -
4297 png_pass_ystart[png_ptr->pass]) /
4298 png_pass_yinc[png_ptr->pass];
The Android Open Source Project893912b2009-03-03 19:30:05 -08004299 }
Chris Craikb50c2172013-07-29 15:28:30 -07004300
The Android Open Source Project893912b2009-03-03 19:30:05 -08004301 else /* if (png_ptr->transformations & PNG_INTERLACE) */
Chris Craikb50c2172013-07-29 15:28:30 -07004302 break; /* libpng deinterlacing sees every row */
4303
4304 } while (png_ptr->num_rows == 0 || png_ptr->iwidth == 0);
The Android Open Source Project893912b2009-03-03 19:30:05 -08004305
4306 if (png_ptr->pass < 7)
4307 return;
4308 }
The Android Open Source Project893912b2009-03-03 19:30:05 -08004309
Chris Craikb50c2172013-07-29 15:28:30 -07004310 /* Here after at the end of the last row of the last pass. */
4311 png_read_finish_IDAT(png_ptr);
The Android Open Source Project893912b2009-03-03 19:30:05 -08004312}
Matt Sarett96be9082016-05-03 13:29:54 -04004313#endif /* SEQUENTIAL_READ */
The Android Open Source Project893912b2009-03-03 19:30:05 -08004314
4315void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07004316png_read_start_row(png_structrp png_ptr)
The Android Open Source Project893912b2009-03-03 19:30:05 -08004317{
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004318 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
The Android Open Source Project893912b2009-03-03 19:30:05 -08004319
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004320 /* Start of interlace block */
Chris Craikb50c2172013-07-29 15:28:30 -07004321 static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
The Android Open Source Project893912b2009-03-03 19:30:05 -08004322
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004323 /* Offset to next interlace block */
Chris Craikb50c2172013-07-29 15:28:30 -07004324 static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
The Android Open Source Project893912b2009-03-03 19:30:05 -08004325
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004326 /* Start of interlace block in the y direction */
Chris Craikb50c2172013-07-29 15:28:30 -07004327 static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
The Android Open Source Project893912b2009-03-03 19:30:05 -08004328
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004329 /* Offset to next interlace block in the y direction */
Chris Craikb50c2172013-07-29 15:28:30 -07004330 static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
The Android Open Source Project893912b2009-03-03 19:30:05 -08004331
4332 int max_pixel_depth;
The Android Open Source Project4215dd12009-03-09 11:52:12 -07004333 png_size_t row_bytes;
The Android Open Source Project893912b2009-03-03 19:30:05 -08004334
The Android Open Source Project4215dd12009-03-09 11:52:12 -07004335 png_debug(1, "in png_read_start_row");
Chris Craikb50c2172013-07-29 15:28:30 -07004336
4337#ifdef PNG_READ_TRANSFORMS_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08004338 png_init_read_transformations(png_ptr);
Chris Craikb50c2172013-07-29 15:28:30 -07004339#endif
Matt Sarett96be9082016-05-03 13:29:54 -04004340 if (png_ptr->interlaced != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08004341 {
Matt Sarett96be9082016-05-03 13:29:54 -04004342 if ((png_ptr->transformations & PNG_INTERLACE) == 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08004343 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
Chris Craikb50c2172013-07-29 15:28:30 -07004344 png_pass_ystart[0]) / png_pass_yinc[0];
4345
The Android Open Source Project893912b2009-03-03 19:30:05 -08004346 else
4347 png_ptr->num_rows = png_ptr->height;
4348
4349 png_ptr->iwidth = (png_ptr->width +
Chris Craikb50c2172013-07-29 15:28:30 -07004350 png_pass_inc[png_ptr->pass] - 1 -
4351 png_pass_start[png_ptr->pass]) /
4352 png_pass_inc[png_ptr->pass];
The Android Open Source Project893912b2009-03-03 19:30:05 -08004353 }
Chris Craikb50c2172013-07-29 15:28:30 -07004354
The Android Open Source Project893912b2009-03-03 19:30:05 -08004355 else
The Android Open Source Project893912b2009-03-03 19:30:05 -08004356 {
4357 png_ptr->num_rows = png_ptr->height;
4358 png_ptr->iwidth = png_ptr->width;
The Android Open Source Project893912b2009-03-03 19:30:05 -08004359 }
Chris Craikb50c2172013-07-29 15:28:30 -07004360
The Android Open Source Project893912b2009-03-03 19:30:05 -08004361 max_pixel_depth = png_ptr->pixel_depth;
4362
Matt Sarett96be9082016-05-03 13:29:54 -04004363 /* WARNING: * png_read_transform_info (pngrtran.c) performs a simpler set of
Chris Craikb50c2172013-07-29 15:28:30 -07004364 * calculations to calculate the final pixel depth, then
4365 * png_do_read_transforms actually does the transforms. This means that the
4366 * code which effectively calculates this value is actually repeated in three
4367 * separate places. They must all match. Innocent changes to the order of
4368 * transformations can and will break libpng in a way that causes memory
4369 * overwrites.
4370 *
4371 * TODO: fix this.
4372 */
Patrick Scott5f6bd842010-06-28 16:55:16 -04004373#ifdef PNG_READ_PACK_SUPPORTED
Matt Sarett96be9082016-05-03 13:29:54 -04004374 if ((png_ptr->transformations & PNG_PACK) != 0 && png_ptr->bit_depth < 8)
The Android Open Source Project893912b2009-03-03 19:30:05 -08004375 max_pixel_depth = 8;
4376#endif
4377
Patrick Scott5f6bd842010-06-28 16:55:16 -04004378#ifdef PNG_READ_EXPAND_SUPPORTED
Matt Sarett96be9082016-05-03 13:29:54 -04004379 if ((png_ptr->transformations & PNG_EXPAND) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08004380 {
4381 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
4382 {
Matt Sarett96be9082016-05-03 13:29:54 -04004383 if (png_ptr->num_trans != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08004384 max_pixel_depth = 32;
Chris Craikb50c2172013-07-29 15:28:30 -07004385
The Android Open Source Project893912b2009-03-03 19:30:05 -08004386 else
4387 max_pixel_depth = 24;
4388 }
Chris Craikb50c2172013-07-29 15:28:30 -07004389
The Android Open Source Project893912b2009-03-03 19:30:05 -08004390 else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
4391 {
4392 if (max_pixel_depth < 8)
4393 max_pixel_depth = 8;
Chris Craikb50c2172013-07-29 15:28:30 -07004394
Matt Sarett96be9082016-05-03 13:29:54 -04004395 if (png_ptr->num_trans != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08004396 max_pixel_depth *= 2;
4397 }
Chris Craikb50c2172013-07-29 15:28:30 -07004398
The Android Open Source Project893912b2009-03-03 19:30:05 -08004399 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
4400 {
Matt Sarett96be9082016-05-03 13:29:54 -04004401 if (png_ptr->num_trans != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08004402 {
4403 max_pixel_depth *= 4;
4404 max_pixel_depth /= 3;
4405 }
4406 }
4407 }
4408#endif
4409
Chris Craikb50c2172013-07-29 15:28:30 -07004410#ifdef PNG_READ_EXPAND_16_SUPPORTED
Matt Sarett96be9082016-05-03 13:29:54 -04004411 if ((png_ptr->transformations & PNG_EXPAND_16) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07004412 {
Matt Sarett96be9082016-05-03 13:29:54 -04004413# ifdef PNG_READ_EXPAND_SUPPORTED
4414 /* In fact it is an error if it isn't supported, but checking is
4415 * the safe way.
4416 */
4417 if ((png_ptr->transformations & PNG_EXPAND) != 0)
4418 {
4419 if (png_ptr->bit_depth < 16)
4420 max_pixel_depth *= 2;
4421 }
4422 else
4423# endif
4424 png_ptr->transformations &= ~PNG_EXPAND_16;
Chris Craikb50c2172013-07-29 15:28:30 -07004425 }
4426#endif
4427
Patrick Scott5f6bd842010-06-28 16:55:16 -04004428#ifdef PNG_READ_FILLER_SUPPORTED
Matt Sarett96be9082016-05-03 13:29:54 -04004429 if ((png_ptr->transformations & (PNG_FILLER)) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08004430 {
Chris Craikb50c2172013-07-29 15:28:30 -07004431 if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
The Android Open Source Project893912b2009-03-03 19:30:05 -08004432 {
4433 if (max_pixel_depth <= 8)
4434 max_pixel_depth = 16;
Chris Craikb50c2172013-07-29 15:28:30 -07004435
The Android Open Source Project893912b2009-03-03 19:30:05 -08004436 else
4437 max_pixel_depth = 32;
4438 }
Chris Craikb50c2172013-07-29 15:28:30 -07004439
4440 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB ||
4441 png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
The Android Open Source Project893912b2009-03-03 19:30:05 -08004442 {
4443 if (max_pixel_depth <= 32)
4444 max_pixel_depth = 32;
Chris Craikb50c2172013-07-29 15:28:30 -07004445
The Android Open Source Project893912b2009-03-03 19:30:05 -08004446 else
4447 max_pixel_depth = 64;
4448 }
4449 }
4450#endif
4451
Patrick Scott5f6bd842010-06-28 16:55:16 -04004452#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Matt Sarett96be9082016-05-03 13:29:54 -04004453 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08004454 {
4455 if (
Patrick Scott5f6bd842010-06-28 16:55:16 -04004456#ifdef PNG_READ_EXPAND_SUPPORTED
Matt Sarett96be9082016-05-03 13:29:54 -04004457 (png_ptr->num_trans != 0 &&
4458 (png_ptr->transformations & PNG_EXPAND) != 0) ||
The Android Open Source Project893912b2009-03-03 19:30:05 -08004459#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04004460#ifdef PNG_READ_FILLER_SUPPORTED
Matt Sarett96be9082016-05-03 13:29:54 -04004461 (png_ptr->transformations & (PNG_FILLER)) != 0 ||
The Android Open Source Project893912b2009-03-03 19:30:05 -08004462#endif
Chris Craikb50c2172013-07-29 15:28:30 -07004463 png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
The Android Open Source Project893912b2009-03-03 19:30:05 -08004464 {
4465 if (max_pixel_depth <= 16)
4466 max_pixel_depth = 32;
Chris Craikb50c2172013-07-29 15:28:30 -07004467
The Android Open Source Project893912b2009-03-03 19:30:05 -08004468 else
4469 max_pixel_depth = 64;
4470 }
Chris Craikb50c2172013-07-29 15:28:30 -07004471
The Android Open Source Project893912b2009-03-03 19:30:05 -08004472 else
4473 {
4474 if (max_pixel_depth <= 8)
Chris Craikb50c2172013-07-29 15:28:30 -07004475 {
4476 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
The Android Open Source Project893912b2009-03-03 19:30:05 -08004477 max_pixel_depth = 32;
Chris Craikb50c2172013-07-29 15:28:30 -07004478
4479 else
The Android Open Source Project893912b2009-03-03 19:30:05 -08004480 max_pixel_depth = 24;
Chris Craikb50c2172013-07-29 15:28:30 -07004481 }
4482
The Android Open Source Project893912b2009-03-03 19:30:05 -08004483 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4484 max_pixel_depth = 64;
Chris Craikb50c2172013-07-29 15:28:30 -07004485
The Android Open Source Project893912b2009-03-03 19:30:05 -08004486 else
4487 max_pixel_depth = 48;
4488 }
4489 }
4490#endif
4491
4492#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
4493defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
Matt Sarett96be9082016-05-03 13:29:54 -04004494 if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07004495 {
4496 int user_pixel_depth = png_ptr->user_transform_depth *
The Android Open Source Project893912b2009-03-03 19:30:05 -08004497 png_ptr->user_transform_channels;
Chris Craikb50c2172013-07-29 15:28:30 -07004498
4499 if (user_pixel_depth > max_pixel_depth)
4500 max_pixel_depth = user_pixel_depth;
4501 }
The Android Open Source Project893912b2009-03-03 19:30:05 -08004502#endif
4503
Chris Craikb50c2172013-07-29 15:28:30 -07004504 /* This value is stored in png_struct and double checked in the row read
4505 * code.
4506 */
4507 png_ptr->maximum_pixel_depth = (png_byte)max_pixel_depth;
4508 png_ptr->transformed_pixel_depth = 0; /* calculated on demand */
4509
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004510 /* Align the width on the next larger 8 pixels. Mainly used
4511 * for interlacing
4512 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08004513 row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004514 /* Calculate the maximum bytes needed, adding a byte and a pixel
4515 * for safety's sake
4516 */
The Android Open Source Project4215dd12009-03-09 11:52:12 -07004517 row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) +
Chris Craikb50c2172013-07-29 15:28:30 -07004518 1 + ((max_pixel_depth + 7) >> 3);
4519
The Android Open Source Project893912b2009-03-03 19:30:05 -08004520#ifdef PNG_MAX_MALLOC_64K
4521 if (row_bytes > (png_uint_32)65536L)
4522 png_error(png_ptr, "This image requires a row greater than 64KB");
4523#endif
4524
Chris Craikb50c2172013-07-29 15:28:30 -07004525 if (row_bytes + 48 > png_ptr->old_big_row_buf_size)
The Android Open Source Project893912b2009-03-03 19:30:05 -08004526 {
The Android Open Source Project4215dd12009-03-09 11:52:12 -07004527 png_free(png_ptr, png_ptr->big_row_buf);
Chris Craikb50c2172013-07-29 15:28:30 -07004528 png_free(png_ptr, png_ptr->big_prev_row);
4529
Matt Sarett96be9082016-05-03 13:29:54 -04004530 if (png_ptr->interlaced != 0)
Patrick Scott5f6bd842010-06-28 16:55:16 -04004531 png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr,
Chris Craikb50c2172013-07-29 15:28:30 -07004532 row_bytes + 48);
Patrick Scott5f6bd842010-06-28 16:55:16 -04004533
Chris Craikb50c2172013-07-29 15:28:30 -07004534 else
4535 png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
4536
4537 png_ptr->big_prev_row = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
4538
4539#ifdef PNG_ALIGNED_MEMORY_SUPPORTED
4540 /* Use 16-byte aligned memory for row_buf with at least 16 bytes
4541 * of padding before and after row_buf; treat prev_row similarly.
4542 * NOTE: the alignment is to the start of the pixels, one beyond the start
4543 * of the buffer, because of the filter byte. Prior to libpng 1.5.6 this
4544 * was incorrect; the filter byte was aligned, which had the exact
4545 * opposite effect of that intended.
4546 */
4547 {
4548 png_bytep temp = png_ptr->big_row_buf + 32;
4549 int extra = (int)((temp - (png_bytep)0) & 0x0f);
4550 png_ptr->row_buf = temp - extra - 1/*filter byte*/;
4551
4552 temp = png_ptr->big_prev_row + 32;
4553 extra = (int)((temp - (png_bytep)0) & 0x0f);
4554 png_ptr->prev_row = temp - extra - 1/*filter byte*/;
4555 }
4556
4557#else
4558 /* Use 31 bytes of padding before and 17 bytes after row_buf. */
4559 png_ptr->row_buf = png_ptr->big_row_buf + 31;
4560 png_ptr->prev_row = png_ptr->big_prev_row + 31;
4561#endif
4562 png_ptr->old_big_row_buf_size = row_bytes + 48;
The Android Open Source Project893912b2009-03-03 19:30:05 -08004563 }
4564
4565#ifdef PNG_MAX_MALLOC_64K
Chris Craikb50c2172013-07-29 15:28:30 -07004566 if (png_ptr->rowbytes > 65535)
The Android Open Source Project893912b2009-03-03 19:30:05 -08004567 png_error(png_ptr, "This image requires a row greater than 64KB");
The Android Open Source Project893912b2009-03-03 19:30:05 -08004568
Chris Craikb50c2172013-07-29 15:28:30 -07004569#endif
4570 if (png_ptr->rowbytes > (PNG_SIZE_MAX - 1))
4571 png_error(png_ptr, "Row has too many bytes to allocate in memory");
4572
4573 memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
4574
4575 png_debug1(3, "width = %u,", png_ptr->width);
4576 png_debug1(3, "height = %u,", png_ptr->height);
4577 png_debug1(3, "iwidth = %u,", png_ptr->iwidth);
4578 png_debug1(3, "num_rows = %u,", png_ptr->num_rows);
4579 png_debug1(3, "rowbytes = %lu,", (unsigned long)png_ptr->rowbytes);
4580 png_debug1(3, "irowbytes = %lu",
4581 (unsigned long)PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1);
4582
4583 /* The sequential reader needs a buffer for IDAT, but the progressive reader
4584 * does not, so free the read buffer now regardless; the sequential reader
4585 * reallocates it on demand.
4586 */
Matt Sarett96be9082016-05-03 13:29:54 -04004587 if (png_ptr->read_buffer != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08004588 {
Chris Craikb50c2172013-07-29 15:28:30 -07004589 png_bytep buffer = png_ptr->read_buffer;
4590
4591 png_ptr->read_buffer_size = 0;
4592 png_ptr->read_buffer = NULL;
4593 png_free(png_ptr, buffer);
The Android Open Source Project893912b2009-03-03 19:30:05 -08004594 }
4595
Chris Craikb50c2172013-07-29 15:28:30 -07004596 /* Finally claim the zstream for the inflate of the IDAT data, use the bits
4597 * value from the stream (note that this will result in a fatal error if the
4598 * IDAT stream has a bogus deflate header window_bits value, but this should
4599 * not be happening any longer!)
4600 */
4601 if (png_inflate_claim(png_ptr, png_IDAT) != Z_OK)
4602 png_error(png_ptr, png_ptr->zstream.msg);
The Android Open Source Project893912b2009-03-03 19:30:05 -08004603
4604 png_ptr->flags |= PNG_FLAG_ROW_INIT;
4605}
Matt Sarett96be9082016-05-03 13:29:54 -04004606#endif /* READ */