blob: 381d83076ff211c3e5a05196a9a0873035cc1b00 [file] [log] [blame]
The Android Open Source Project893912b2009-03-03 19:30:05 -08001
2/* pngrutil.c - utilities to read a PNG file
3 *
Sireesh Tripurarib478e662014-05-09 15:15:10 +05304 * Last changed in libpng 1.6.10 [March 6, 2014]
5 * Copyright (c) 1998-2014 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 */
92 return -(png_int_32)uval;
The Android Open Source Project893912b2009-03-03 19:30:05 -080093}
94
95/* Grab an unsigned 16-bit integer from a buffer in big-endian format. */
Chris Craikb50c2172013-07-29 15:28:30 -070096png_uint_16 (PNGAPI
97png_get_uint_16)(png_const_bytep buf)
The Android Open Source Project893912b2009-03-03 19:30:05 -080098{
Chris Craikb50c2172013-07-29 15:28:30 -070099 /* ANSI-C requires an int value to accomodate at least 16 bits so this
100 * works and allows the compiler not to worry about possible narrowing
101 * on 32 bit systems. (Pre-ANSI systems did not make integers smaller
102 * than 16 bits either.)
103 */
104 unsigned int val =
105 ((unsigned int)(*buf) << 8) +
106 ((unsigned int)(*(buf + 1)));
The Android Open Source Project893912b2009-03-03 19:30:05 -0800107
Chris Craikb50c2172013-07-29 15:28:30 -0700108 return (png_uint_16)val;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800109}
Chris Craikb50c2172013-07-29 15:28:30 -0700110
111#endif /* PNG_READ_INT_FUNCTIONS_SUPPORTED */
112
113/* Read and check the PNG file signature */
114void /* PRIVATE */
115png_read_sig(png_structrp png_ptr, png_inforp info_ptr)
116{
117 png_size_t num_checked, num_to_check;
118
119 /* Exit if the user application does not expect a signature. */
120 if (png_ptr->sig_bytes >= 8)
121 return;
122
123 num_checked = png_ptr->sig_bytes;
124 num_to_check = 8 - num_checked;
125
126#ifdef PNG_IO_STATE_SUPPORTED
127 png_ptr->io_state = PNG_IO_READING | PNG_IO_SIGNATURE;
128#endif
129
130 /* The signature must be serialized in a single I/O call. */
131 png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
132 png_ptr->sig_bytes = 8;
133
134 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
135 {
136 if (num_checked < 4 &&
137 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
138 png_error(png_ptr, "Not a PNG file");
139 else
140 png_error(png_ptr, "PNG file corrupted by ASCII conversion");
141 }
142 if (num_checked < 3)
143 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
144}
The Android Open Source Project893912b2009-03-03 19:30:05 -0800145
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700146/* Read the chunk header (length + type name).
147 * Put the type name into png_ptr->chunk_name, and return the length.
148 */
149png_uint_32 /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -0700150png_read_chunk_header(png_structrp png_ptr)
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700151{
152 png_byte buf[8];
153 png_uint_32 length;
154
Chris Craikb50c2172013-07-29 15:28:30 -0700155#ifdef PNG_IO_STATE_SUPPORTED
156 png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_HDR;
157#endif
158
159 /* Read the length and the chunk name.
160 * This must be performed in a single I/O call.
161 */
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700162 png_read_data(png_ptr, buf, 8);
163 length = png_get_uint_31(png_ptr, buf);
164
Chris Craikb50c2172013-07-29 15:28:30 -0700165 /* Put the chunk name into png_ptr->chunk_name. */
166 png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(buf+4);
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700167
Chris Craikb50c2172013-07-29 15:28:30 -0700168 png_debug2(0, "Reading %lx chunk, length = %lu",
169 (unsigned long)png_ptr->chunk_name, (unsigned long)length);
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700170
Chris Craikb50c2172013-07-29 15:28:30 -0700171 /* Reset the crc and run it over the chunk name. */
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700172 png_reset_crc(png_ptr);
Chris Craikb50c2172013-07-29 15:28:30 -0700173 png_calculate_crc(png_ptr, buf + 4, 4);
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700174
Chris Craikb50c2172013-07-29 15:28:30 -0700175 /* Check to see if chunk name is valid. */
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700176 png_check_chunk_name(png_ptr, png_ptr->chunk_name);
177
Chris Craikb50c2172013-07-29 15:28:30 -0700178#ifdef PNG_IO_STATE_SUPPORTED
179 png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_DATA;
180#endif
181
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700182 return length;
183}
184
The Android Open Source Project893912b2009-03-03 19:30:05 -0800185/* Read data, and (optionally) run it through the CRC. */
186void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -0700187png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800188{
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400189 if (png_ptr == NULL)
190 return;
Chris Craikb50c2172013-07-29 15:28:30 -0700191
The Android Open Source Project893912b2009-03-03 19:30:05 -0800192 png_read_data(png_ptr, buf, length);
193 png_calculate_crc(png_ptr, buf, length);
194}
195
196/* Optionally skip data and then check the CRC. Depending on whether we
Chris Craikb50c2172013-07-29 15:28:30 -0700197 * are reading an ancillary or critical chunk, and how the program has set
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400198 * things up, we may calculate the CRC on the data and print a message.
199 * Returns '1' if there was a CRC error, '0' otherwise.
200 */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800201int /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -0700202png_crc_finish(png_structrp png_ptr, png_uint_32 skip)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800203{
Chris Craikb50c2172013-07-29 15:28:30 -0700204 /* The size of the local buffer for inflate is a good guess as to a
205 * reasonable size to use for buffering reads from the application.
206 */
207 while (skip > 0)
208 {
209 png_uint_32 len;
210 png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];
The Android Open Source Project893912b2009-03-03 19:30:05 -0800211
Chris Craikb50c2172013-07-29 15:28:30 -0700212 len = (sizeof tmpbuf);
213 if (len > skip)
214 len = skip;
215 skip -= len;
216
217 png_crc_read(png_ptr, tmpbuf, len);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800218 }
219
220 if (png_crc_error(png_ptr))
221 {
Chris Craikb50c2172013-07-29 15:28:30 -0700222 if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) ?
223 !(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) :
224 (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE))
The Android Open Source Project893912b2009-03-03 19:30:05 -0800225 {
226 png_chunk_warning(png_ptr, "CRC error");
227 }
Chris Craikb50c2172013-07-29 15:28:30 -0700228
The Android Open Source Project893912b2009-03-03 19:30:05 -0800229 else
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530230 png_chunk_error(png_ptr, "CRC error");
Chris Craikb50c2172013-07-29 15:28:30 -0700231
The Android Open Source Project893912b2009-03-03 19:30:05 -0800232 return (1);
233 }
234
235 return (0);
236}
237
Sireesh Tripurarib0277d02014-05-09 15:39:12 +0530238#ifdef PNG_INDEX_SUPPORTED
239int /* PRIVATE */
240png_opt_crc_finish(png_structrp png_ptr, png_uint_32 skip)
241{
242 while (skip > 0)
243 {
244 png_uint_32 len;
245 png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];
246
247 len = (sizeof tmpbuf);
248 if (len > skip)
249 len = skip;
250 skip -= len;
251
252 png_crc_read(png_ptr, tmpbuf, len);
253 }
254
255 if (png_crc_error(png_ptr))
256 {
257 png_chunk_warning(png_ptr, "CRC error");
258 return (1);
259 }
260
261 return (0);
262}
263#endif
264
The Android Open Source Project893912b2009-03-03 19:30:05 -0800265/* Compare the CRC stored in the PNG file with that calculated by libpng from
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400266 * the data it has read thus far.
267 */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800268int /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -0700269png_crc_error(png_structrp png_ptr)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800270{
271 png_byte crc_bytes[4];
272 png_uint_32 crc;
273 int need_crc = 1;
274
Chris Craikb50c2172013-07-29 15:28:30 -0700275 if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name))
The Android Open Source Project893912b2009-03-03 19:30:05 -0800276 {
277 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
278 (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
279 need_crc = 0;
280 }
Chris Craikb50c2172013-07-29 15:28:30 -0700281
282 else /* critical */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800283 {
284 if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
285 need_crc = 0;
286 }
287
Chris Craikb50c2172013-07-29 15:28:30 -0700288#ifdef PNG_IO_STATE_SUPPORTED
289 png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_CRC;
290#endif
291
292 /* The chunk CRC must be serialized in a single I/O call. */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800293 png_read_data(png_ptr, crc_bytes, 4);
294
295 if (need_crc)
296 {
297 crc = png_get_uint_32(crc_bytes);
298 return ((int)(crc != png_ptr->crc));
299 }
Chris Craikb50c2172013-07-29 15:28:30 -0700300
The Android Open Source Project893912b2009-03-03 19:30:05 -0800301 else
302 return (0);
303}
304
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530305#if defined(PNG_READ_iCCP_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) ||\
306 defined(PNG_READ_pCAL_SUPPORTED) || defined(PNG_READ_sCAL_SUPPORTED) ||\
307 defined(PNG_READ_sPLT_SUPPORTED) || defined(PNG_READ_tEXt_SUPPORTED) ||\
308 defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_SEQUENTIAL_READ_SUPPORTED)
Chris Craikb50c2172013-07-29 15:28:30 -0700309/* Manage the read buffer; this simply reallocates the buffer if it is not small
310 * enough (or if it is not allocated). The routine returns a pointer to the
311 * buffer; if an error occurs and 'warn' is set the routine returns NULL, else
312 * it will call png_error (via png_malloc) on failure. (warn == 2 means
313 * 'silent').
314 */
315static png_bytep
316png_read_buffer(png_structrp png_ptr, png_alloc_size_t new_size, int warn)
Patrick Scott5f6bd842010-06-28 16:55:16 -0400317{
Chris Craikb50c2172013-07-29 15:28:30 -0700318 png_bytep buffer = png_ptr->read_buffer;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400319
Chris Craikb50c2172013-07-29 15:28:30 -0700320 if (buffer != NULL && new_size > png_ptr->read_buffer_size)
Patrick Scott5f6bd842010-06-28 16:55:16 -0400321 {
Chris Craikb50c2172013-07-29 15:28:30 -0700322 png_ptr->read_buffer = NULL;
323 png_ptr->read_buffer = NULL;
324 png_ptr->read_buffer_size = 0;
325 png_free(png_ptr, buffer);
326 buffer = NULL;
327 }
Patrick Scott5f6bd842010-06-28 16:55:16 -0400328
Chris Craikb50c2172013-07-29 15:28:30 -0700329 if (buffer == NULL)
330 {
331 buffer = png_voidcast(png_bytep, png_malloc_base(png_ptr, new_size));
Patrick Scott5f6bd842010-06-28 16:55:16 -0400332
Chris Craikb50c2172013-07-29 15:28:30 -0700333 if (buffer != NULL)
Patrick Scott5f6bd842010-06-28 16:55:16 -0400334 {
Chris Craikb50c2172013-07-29 15:28:30 -0700335 png_ptr->read_buffer = buffer;
336 png_ptr->read_buffer_size = new_size;
337 }
338
339 else if (warn < 2) /* else silent */
340 {
Chris Craikb50c2172013-07-29 15:28:30 -0700341 if (warn)
342 png_chunk_warning(png_ptr, "insufficient memory to read chunk");
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530343
Chris Craikb50c2172013-07-29 15:28:30 -0700344 else
Chris Craikb50c2172013-07-29 15:28:30 -0700345 png_chunk_error(png_ptr, "insufficient memory to read chunk");
Chris Craikb50c2172013-07-29 15:28:30 -0700346 }
347 }
348
349 return buffer;
350}
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530351#endif /* PNG_READ_iCCP|iTXt|pCAL|sCAL|sPLT|tEXt|zTXt|SEQUENTIAL_READ */
Chris Craikb50c2172013-07-29 15:28:30 -0700352
353/* png_inflate_claim: claim the zstream for some nefarious purpose that involves
354 * decompression. Returns Z_OK on success, else a zlib error code. It checks
355 * the owner but, in final release builds, just issues a warning if some other
356 * chunk apparently owns the stream. Prior to release it does a png_error.
357 */
358static int
359png_inflate_claim(png_structrp png_ptr, png_uint_32 owner)
360{
361 if (png_ptr->zowner != 0)
362 {
363 char msg[64];
364
365 PNG_STRING_FROM_CHUNK(msg, png_ptr->zowner);
366 /* So the message that results is "<chunk> using zstream"; this is an
367 * internal error, but is very useful for debugging. i18n requirements
368 * are minimal.
369 */
370 (void)png_safecat(msg, (sizeof msg), 4, " using zstream");
371# if PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC
372 png_chunk_warning(png_ptr, msg);
373 png_ptr->zowner = 0;
374# else
375 png_chunk_error(png_ptr, msg);
376# endif
377 }
378
379 /* Implementation note: unlike 'png_deflate_claim' this internal function
380 * does not take the size of the data as an argument. Some efficiency could
381 * be gained by using this when it is known *if* the zlib stream itself does
382 * not record the number; however, this is an illusion: the original writer
383 * of the PNG may have selected a lower window size, and we really must
384 * follow that because, for systems with with limited capabilities, we
385 * would otherwise reject the application's attempts to use a smaller window
386 * size (zlib doesn't have an interface to say "this or lower"!).
387 *
388 * inflateReset2 was added to zlib 1.2.4; before this the window could not be
389 * reset, therefore it is necessary to always allocate the maximum window
390 * size with earlier zlibs just in case later compressed chunks need it.
391 */
392 {
393 int ret; /* zlib return code */
394# if PNG_ZLIB_VERNUM >= 0x1240
395
396# if defined(PNG_SET_OPTION_SUPPORTED) && \
397 defined(PNG_MAXIMUM_INFLATE_WINDOW)
398 int window_bits;
399
400 if (((png_ptr->options >> PNG_MAXIMUM_INFLATE_WINDOW) & 3) ==
401 PNG_OPTION_ON)
402 window_bits = 15;
403
404 else
405 window_bits = 0;
406# else
407# define window_bits 0
408# endif
409# endif
410
411 /* Set this for safety, just in case the previous owner left pointers to
412 * memory allocations.
413 */
414 png_ptr->zstream.next_in = NULL;
415 png_ptr->zstream.avail_in = 0;
416 png_ptr->zstream.next_out = NULL;
417 png_ptr->zstream.avail_out = 0;
418
419 if (png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED)
420 {
421# if PNG_ZLIB_VERNUM < 0x1240
422 ret = inflateReset(&png_ptr->zstream);
423# else
424 ret = inflateReset2(&png_ptr->zstream, window_bits);
425# endif
426 }
427
428 else
429 {
430# if PNG_ZLIB_VERNUM < 0x1240
431 ret = inflateInit(&png_ptr->zstream);
432# else
433 ret = inflateInit2(&png_ptr->zstream, window_bits);
434# endif
435
436 if (ret == Z_OK)
437 png_ptr->flags |= PNG_FLAG_ZSTREAM_INITIALIZED;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400438 }
439
440 if (ret == Z_OK)
Chris Craikb50c2172013-07-29 15:28:30 -0700441 png_ptr->zowner = owner;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400442
Chris Craikb50c2172013-07-29 15:28:30 -0700443 else
444 png_zstream_error(png_ptr, ret);
445
446 return ret;
447 }
448
449# ifdef window_bits
450# undef window_bits
451# endif
452}
453
454#ifdef PNG_READ_COMPRESSED_TEXT_SUPPORTED
455/* png_inflate now returns zlib error codes including Z_OK and Z_STREAM_END to
456 * allow the caller to do multiple calls if required. If the 'finish' flag is
457 * set Z_FINISH will be passed to the final inflate() call and Z_STREAM_END must
458 * be returned or there has been a problem, otherwise Z_SYNC_FLUSH is used and
459 * Z_OK or Z_STREAM_END will be returned on success.
460 *
461 * The input and output sizes are updated to the actual amounts of data consumed
462 * or written, not the amount available (as in a z_stream). The data pointers
463 * are not changed, so the next input is (data+input_size) and the next
464 * available output is (output+output_size).
465 */
466static int
467png_inflate(png_structrp png_ptr, png_uint_32 owner, int finish,
468 /* INPUT: */ png_const_bytep input, png_uint_32p input_size_ptr,
469 /* OUTPUT: */ png_bytep output, png_alloc_size_t *output_size_ptr)
470{
471 if (png_ptr->zowner == owner) /* Else not claimed */
472 {
473 int ret;
474 png_alloc_size_t avail_out = *output_size_ptr;
475 png_uint_32 avail_in = *input_size_ptr;
476
477 /* zlib can't necessarily handle more than 65535 bytes at once (i.e. it
478 * can't even necessarily handle 65536 bytes) because the type uInt is
479 * "16 bits or more". Consequently it is necessary to chunk the input to
480 * zlib. This code uses ZLIB_IO_MAX, from pngpriv.h, as the maximum (the
481 * maximum value that can be stored in a uInt.) It is possible to set
482 * ZLIB_IO_MAX to a lower value in pngpriv.h and this may sometimes have
483 * a performance advantage, because it reduces the amount of data accessed
484 * at each step and that may give the OS more time to page it in.
Patrick Scott5f6bd842010-06-28 16:55:16 -0400485 */
Chris Craikb50c2172013-07-29 15:28:30 -0700486 png_ptr->zstream.next_in = PNGZ_INPUT_CAST(input);
487 /* avail_in and avail_out are set below from 'size' */
Patrick Scott5f6bd842010-06-28 16:55:16 -0400488 png_ptr->zstream.avail_in = 0;
Chris Craikb50c2172013-07-29 15:28:30 -0700489 png_ptr->zstream.avail_out = 0;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400490
Chris Craikb50c2172013-07-29 15:28:30 -0700491 /* Read directly into the output if it is available (this is set to
492 * a local buffer below if output is NULL).
Patrick Scott5f6bd842010-06-28 16:55:16 -0400493 */
Chris Craikb50c2172013-07-29 15:28:30 -0700494 if (output != NULL)
495 png_ptr->zstream.next_out = output;
496
497 do
Patrick Scott5f6bd842010-06-28 16:55:16 -0400498 {
Chris Craikb50c2172013-07-29 15:28:30 -0700499 uInt avail;
500 Byte local_buffer[PNG_INFLATE_BUF_SIZE];
501
502 /* zlib INPUT BUFFER */
503 /* The setting of 'avail_in' used to be outside the loop; by setting it
504 * inside it is possible to chunk the input to zlib and simply rely on
505 * zlib to advance the 'next_in' pointer. This allows arbitrary
506 * amounts of data to be passed through zlib at the unavoidable cost of
507 * requiring a window save (memcpy of up to 32768 output bytes)
508 * every ZLIB_IO_MAX input bytes.
509 */
510 avail_in += png_ptr->zstream.avail_in; /* not consumed last time */
511
512 avail = ZLIB_IO_MAX;
513
514 if (avail_in < avail)
515 avail = (uInt)avail_in; /* safe: < than ZLIB_IO_MAX */
516
517 avail_in -= avail;
518 png_ptr->zstream.avail_in = avail;
519
520 /* zlib OUTPUT BUFFER */
521 avail_out += png_ptr->zstream.avail_out; /* not written last time */
522
523 avail = ZLIB_IO_MAX; /* maximum zlib can process */
524
525 if (output == NULL)
Patrick Scott5f6bd842010-06-28 16:55:16 -0400526 {
Chris Craikb50c2172013-07-29 15:28:30 -0700527 /* Reset the output buffer each time round if output is NULL and
528 * make available the full buffer, up to 'remaining_space'
529 */
530 png_ptr->zstream.next_out = local_buffer;
531 if ((sizeof local_buffer) < avail)
532 avail = (sizeof local_buffer);
Patrick Scott5f6bd842010-06-28 16:55:16 -0400533 }
534
Chris Craikb50c2172013-07-29 15:28:30 -0700535 if (avail_out < avail)
536 avail = (uInt)avail_out; /* safe: < ZLIB_IO_MAX */
Patrick Scott5f6bd842010-06-28 16:55:16 -0400537
Chris Craikb50c2172013-07-29 15:28:30 -0700538 png_ptr->zstream.avail_out = avail;
539 avail_out -= avail;
540
541 /* zlib inflate call */
542 /* In fact 'avail_out' may be 0 at this point, that happens at the end
543 * of the read when the final LZ end code was not passed at the end of
544 * the previous chunk of input data. Tell zlib if we have reached the
545 * end of the output buffer.
546 */
547 ret = inflate(&png_ptr->zstream, avail_out > 0 ? Z_NO_FLUSH :
548 (finish ? Z_FINISH : Z_SYNC_FLUSH));
549 } while (ret == Z_OK);
550
551 /* For safety kill the local buffer pointer now */
552 if (output == NULL)
553 png_ptr->zstream.next_out = NULL;
554
555 /* Claw back the 'size' and 'remaining_space' byte counts. */
556 avail_in += png_ptr->zstream.avail_in;
557 avail_out += png_ptr->zstream.avail_out;
558
559 /* Update the input and output sizes; the updated values are the amount
560 * consumed or written, effectively the inverse of what zlib uses.
Patrick Scott5f6bd842010-06-28 16:55:16 -0400561 */
Chris Craikb50c2172013-07-29 15:28:30 -0700562 if (avail_out > 0)
563 *output_size_ptr -= avail_out;
564
565 if (avail_in > 0)
566 *input_size_ptr -= avail_in;
567
568 /* Ensure png_ptr->zstream.msg is set (even in the success case!) */
569 png_zstream_error(png_ptr, ret);
570 return ret;
571 }
572
573 else
574 {
575 /* This is a bad internal error. The recovery assigns to the zstream msg
576 * pointer, which is not owned by the caller, but this is safe; it's only
577 * used on errors!
578 */
579 png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed");
580 return Z_STREAM_ERROR;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400581 }
582}
583
The Android Open Source Project893912b2009-03-03 19:30:05 -0800584/*
Chris Craikb50c2172013-07-29 15:28:30 -0700585 * Decompress trailing data in a chunk. The assumption is that read_buffer
The Android Open Source Project893912b2009-03-03 19:30:05 -0800586 * points at an allocated area holding the contents of a chunk with a
587 * trailing compressed part. What we get back is an allocated area
588 * holding the original prefix part and an uncompressed version of the
589 * trailing part (the malloc area passed in is freed).
590 */
Chris Craikb50c2172013-07-29 15:28:30 -0700591static int
592png_decompress_chunk(png_structrp png_ptr,
593 png_uint_32 chunklength, png_uint_32 prefix_size,
594 png_alloc_size_t *newlength /* must be initialized to the maximum! */,
595 int terminate /*add a '\0' to the end of the uncompressed data*/)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800596{
Chris Craikb50c2172013-07-29 15:28:30 -0700597 /* TODO: implement different limits for different types of chunk.
598 *
599 * The caller supplies *newlength set to the maximum length of the
600 * uncompressed data, but this routine allocates space for the prefix and
601 * maybe a '\0' terminator too. We have to assume that 'prefix_size' is
602 * limited only by the maximum chunk size.
603 */
604 png_alloc_size_t limit = PNG_SIZE_MAX;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400605
Chris Craikb50c2172013-07-29 15:28:30 -0700606# ifdef PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED
607 if (png_ptr->user_chunk_malloc_max > 0 &&
608 png_ptr->user_chunk_malloc_max < limit)
609 limit = png_ptr->user_chunk_malloc_max;
610# elif PNG_USER_CHUNK_MALLOC_MAX > 0
611 if (PNG_USER_CHUNK_MALLOC_MAX < limit)
612 limit = PNG_USER_CHUNK_MALLOC_MAX;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400613# endif
Patrick Scott5f6bd842010-06-28 16:55:16 -0400614
Chris Craikb50c2172013-07-29 15:28:30 -0700615 if (limit >= prefix_size + (terminate != 0))
616 {
617 int ret;
618
619 limit -= prefix_size + (terminate != 0);
620
621 if (limit < *newlength)
622 *newlength = limit;
623
624 /* Now try to claim the stream. */
625 ret = png_inflate_claim(png_ptr, png_ptr->chunk_name);
626
627 if (ret == Z_OK)
Patrick Scott5f6bd842010-06-28 16:55:16 -0400628 {
Chris Craikb50c2172013-07-29 15:28:30 -0700629 png_uint_32 lzsize = chunklength - prefix_size;
Dave Burkeccee1212012-03-05 22:36:13 -0800630
Chris Craikb50c2172013-07-29 15:28:30 -0700631 ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/,
632 /* input: */ png_ptr->read_buffer + prefix_size, &lzsize,
633 /* output: */ NULL, newlength);
634
635 if (ret == Z_STREAM_END)
Dave Burkeccee1212012-03-05 22:36:13 -0800636 {
Chris Craikb50c2172013-07-29 15:28:30 -0700637 /* Use 'inflateReset' here, not 'inflateReset2' because this
638 * preserves the previously decided window size (otherwise it would
639 * be necessary to store the previous window size.) In practice
640 * this doesn't matter anyway, because png_inflate will call inflate
641 * with Z_FINISH in almost all cases, so the window will not be
642 * maintained.
643 */
644 if (inflateReset(&png_ptr->zstream) == Z_OK)
Patrick Scott5f6bd842010-06-28 16:55:16 -0400645 {
Chris Craikb50c2172013-07-29 15:28:30 -0700646 /* Because of the limit checks above we know that the new,
647 * expanded, size will fit in a size_t (let alone an
648 * png_alloc_size_t). Use png_malloc_base here to avoid an
649 * extra OOM message.
650 */
651 png_alloc_size_t new_size = *newlength;
652 png_alloc_size_t buffer_size = prefix_size + new_size +
653 (terminate != 0);
654 png_bytep text = png_voidcast(png_bytep, png_malloc_base(png_ptr,
655 buffer_size));
656
657 if (text != NULL)
658 {
659 ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/,
660 png_ptr->read_buffer + prefix_size, &lzsize,
661 text + prefix_size, newlength);
662
663 if (ret == Z_STREAM_END)
664 {
665 if (new_size == *newlength)
666 {
667 if (terminate)
668 text[prefix_size + *newlength] = 0;
669
670 if (prefix_size > 0)
671 memcpy(text, png_ptr->read_buffer, prefix_size);
672
673 {
674 png_bytep old_ptr = png_ptr->read_buffer;
675
676 png_ptr->read_buffer = text;
677 png_ptr->read_buffer_size = buffer_size;
678 text = old_ptr; /* freed below */
679 }
680 }
681
682 else
683 {
684 /* The size changed on the second read, there can be no
685 * guarantee that anything is correct at this point.
686 * The 'msg' pointer has been set to "unexpected end of
687 * LZ stream", which is fine, but return an error code
688 * that the caller won't accept.
689 */
690 ret = PNG_UNEXPECTED_ZLIB_RETURN;
691 }
692 }
693
694 else if (ret == Z_OK)
695 ret = PNG_UNEXPECTED_ZLIB_RETURN; /* for safety */
696
697 /* Free the text pointer (this is the old read_buffer on
698 * success)
699 */
700 png_free(png_ptr, text);
701
702 /* This really is very benign, but it's still an error because
703 * the extra space may otherwise be used as a Trojan Horse.
704 */
705 if (ret == Z_STREAM_END &&
706 chunklength - prefix_size != lzsize)
707 png_chunk_benign_error(png_ptr, "extra compressed data");
708 }
709
710 else
711 {
712 /* Out of memory allocating the buffer */
713 ret = Z_MEM_ERROR;
714 png_zstream_error(png_ptr, Z_MEM_ERROR);
715 }
Patrick Scott5f6bd842010-06-28 16:55:16 -0400716 }
717
Chris Craikb50c2172013-07-29 15:28:30 -0700718 else
719 {
720 /* inflateReset failed, store the error message */
721 png_zstream_error(png_ptr, ret);
722
723 if (ret == Z_STREAM_END)
724 ret = PNG_UNEXPECTED_ZLIB_RETURN;
725 }
Patrick Scott5f6bd842010-06-28 16:55:16 -0400726 }
Chris Craikb50c2172013-07-29 15:28:30 -0700727
728 else if (ret == Z_OK)
729 ret = PNG_UNEXPECTED_ZLIB_RETURN;
730
731 /* Release the claimed stream */
732 png_ptr->zowner = 0;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400733 }
Chris Craikb50c2172013-07-29 15:28:30 -0700734
735 else /* the claim failed */ if (ret == Z_STREAM_END) /* impossible! */
736 ret = PNG_UNEXPECTED_ZLIB_RETURN;
737
738 return ret;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400739 }
740
Chris Craikb50c2172013-07-29 15:28:30 -0700741 else
The Android Open Source Project893912b2009-03-03 19:30:05 -0800742 {
Chris Craikb50c2172013-07-29 15:28:30 -0700743 /* Application/configuration limits exceeded */
744 png_zstream_error(png_ptr, Z_MEM_ERROR);
745 return Z_MEM_ERROR;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800746 }
Chris Craikb50c2172013-07-29 15:28:30 -0700747}
748#endif /* PNG_READ_COMPRESSED_TEXT_SUPPORTED */
Patrick Scott5f6bd842010-06-28 16:55:16 -0400749
Chris Craikb50c2172013-07-29 15:28:30 -0700750#ifdef PNG_READ_iCCP_SUPPORTED
751/* Perform a partial read and decompress, producing 'avail_out' bytes and
752 * reading from the current chunk as required.
753 */
754static int
755png_inflate_read(png_structrp png_ptr, png_bytep read_buffer, uInt read_size,
756 png_uint_32p chunk_bytes, png_bytep next_out, png_alloc_size_t *out_size,
757 int finish)
758{
759 if (png_ptr->zowner == png_ptr->chunk_name)
Patrick Scott5f6bd842010-06-28 16:55:16 -0400760 {
Chris Craikb50c2172013-07-29 15:28:30 -0700761 int ret;
762
763 /* next_in and avail_in must have been initialized by the caller. */
764 png_ptr->zstream.next_out = next_out;
765 png_ptr->zstream.avail_out = 0; /* set in the loop */
766
767 do
Patrick Scott5f6bd842010-06-28 16:55:16 -0400768 {
Chris Craikb50c2172013-07-29 15:28:30 -0700769 if (png_ptr->zstream.avail_in == 0)
770 {
771 if (read_size > *chunk_bytes)
772 read_size = (uInt)*chunk_bytes;
773 *chunk_bytes -= read_size;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400774
Chris Craikb50c2172013-07-29 15:28:30 -0700775 if (read_size > 0)
776 png_crc_read(png_ptr, read_buffer, read_size);
777
778 png_ptr->zstream.next_in = read_buffer;
779 png_ptr->zstream.avail_in = read_size;
780 }
781
782 if (png_ptr->zstream.avail_out == 0)
783 {
784 uInt avail = ZLIB_IO_MAX;
785 if (avail > *out_size)
786 avail = (uInt)*out_size;
787 *out_size -= avail;
788
789 png_ptr->zstream.avail_out = avail;
790 }
791
792 /* Use Z_SYNC_FLUSH when there is no more chunk data to ensure that all
793 * the available output is produced; this allows reading of truncated
794 * streams.
795 */
796 ret = inflate(&png_ptr->zstream,
797 *chunk_bytes > 0 ? Z_NO_FLUSH : (finish ? Z_FINISH : Z_SYNC_FLUSH));
Patrick Scott5f6bd842010-06-28 16:55:16 -0400798 }
Chris Craikb50c2172013-07-29 15:28:30 -0700799 while (ret == Z_OK && (*out_size > 0 || png_ptr->zstream.avail_out > 0));
800
801 *out_size += png_ptr->zstream.avail_out;
802 png_ptr->zstream.avail_out = 0; /* Should not be required, but is safe */
803
804 /* Ensure the error message pointer is always set: */
805 png_zstream_error(png_ptr, ret);
806 return ret;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400807 }
808
Chris Craikb50c2172013-07-29 15:28:30 -0700809 else
810 {
811 png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed");
812 return Z_STREAM_ERROR;
813 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800814}
815#endif
816
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400817/* Read and check the IDHR chunk */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800818void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -0700819png_handle_IHDR(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800820{
821 png_byte buf[13];
822 png_uint_32 width, height;
823 int bit_depth, color_type, compression_type, filter_type;
824 int interlace_type;
825
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700826 png_debug(1, "in png_handle_IHDR");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800827
828 if (png_ptr->mode & PNG_HAVE_IHDR)
Chris Craikb50c2172013-07-29 15:28:30 -0700829 png_chunk_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800830
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400831 /* Check the length */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800832 if (length != 13)
Chris Craikb50c2172013-07-29 15:28:30 -0700833 png_chunk_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800834
835 png_ptr->mode |= PNG_HAVE_IHDR;
836
837 png_crc_read(png_ptr, buf, 13);
838 png_crc_finish(png_ptr, 0);
839
840 width = png_get_uint_31(png_ptr, buf);
841 height = png_get_uint_31(png_ptr, buf + 4);
842 bit_depth = buf[8];
843 color_type = buf[9];
844 compression_type = buf[10];
845 filter_type = buf[11];
846 interlace_type = buf[12];
847
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400848 /* Set internal variables */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800849 png_ptr->width = width;
850 png_ptr->height = height;
851 png_ptr->bit_depth = (png_byte)bit_depth;
852 png_ptr->interlaced = (png_byte)interlace_type;
853 png_ptr->color_type = (png_byte)color_type;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400854#ifdef PNG_MNG_FEATURES_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800855 png_ptr->filter_type = (png_byte)filter_type;
856#endif
857 png_ptr->compression_type = (png_byte)compression_type;
858
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400859 /* Find number of channels */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800860 switch (png_ptr->color_type)
861 {
Chris Craikb50c2172013-07-29 15:28:30 -0700862 default: /* invalid, png_set_IHDR calls png_error */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800863 case PNG_COLOR_TYPE_GRAY:
864 case PNG_COLOR_TYPE_PALETTE:
865 png_ptr->channels = 1;
866 break;
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400867
The Android Open Source Project893912b2009-03-03 19:30:05 -0800868 case PNG_COLOR_TYPE_RGB:
869 png_ptr->channels = 3;
870 break;
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400871
The Android Open Source Project893912b2009-03-03 19:30:05 -0800872 case PNG_COLOR_TYPE_GRAY_ALPHA:
873 png_ptr->channels = 2;
874 break;
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400875
The Android Open Source Project893912b2009-03-03 19:30:05 -0800876 case PNG_COLOR_TYPE_RGB_ALPHA:
877 png_ptr->channels = 4;
878 break;
879 }
880
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400881 /* Set up other useful info */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800882 png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth *
883 png_ptr->channels);
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700884 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width);
885 png_debug1(3, "bit_depth = %d", png_ptr->bit_depth);
886 png_debug1(3, "channels = %d", png_ptr->channels);
Chris Craikb50c2172013-07-29 15:28:30 -0700887 png_debug1(3, "rowbytes = %lu", (unsigned long)png_ptr->rowbytes);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800888 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
Chris Craikb50c2172013-07-29 15:28:30 -0700889 color_type, interlace_type, compression_type, filter_type);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800890}
891
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400892/* Read and check the palette */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800893void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -0700894png_handle_PLTE(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800895{
896 png_color palette[PNG_MAX_PALETTE_LENGTH];
897 int num, i;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400898#ifdef PNG_POINTER_INDEXING_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800899 png_colorp pal_ptr;
900#endif
901
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700902 png_debug(1, "in png_handle_PLTE");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800903
904 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Chris Craikb50c2172013-07-29 15:28:30 -0700905 png_chunk_error(png_ptr, "missing IHDR");
906
907 /* Moved to before the 'after IDAT' check below because otherwise duplicate
908 * PLTE chunks are potentially ignored (the spec says there shall not be more
909 * than one PLTE, the error is not treated as benign, so this check trumps
910 * the requirement that PLTE appears before IDAT.)
911 */
912 else if (png_ptr->mode & PNG_HAVE_PLTE)
913 png_chunk_error(png_ptr, "duplicate");
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400914
The Android Open Source Project893912b2009-03-03 19:30:05 -0800915 else if (png_ptr->mode & PNG_HAVE_IDAT)
916 {
Chris Craikb50c2172013-07-29 15:28:30 -0700917 /* This is benign because the non-benign error happened before, when an
918 * IDAT was encountered in a color-mapped image with no PLTE.
919 */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800920 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -0700921 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800922 return;
923 }
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400924
The Android Open Source Project893912b2009-03-03 19:30:05 -0800925 png_ptr->mode |= PNG_HAVE_PLTE;
926
Chris Craikb50c2172013-07-29 15:28:30 -0700927 if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR))
The Android Open Source Project893912b2009-03-03 19:30:05 -0800928 {
The Android Open Source Project893912b2009-03-03 19:30:05 -0800929 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -0700930 png_chunk_benign_error(png_ptr, "ignored in grayscale PNG");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800931 return;
932 }
Chris Craikb50c2172013-07-29 15:28:30 -0700933
Patrick Scott5f6bd842010-06-28 16:55:16 -0400934#ifndef PNG_READ_OPT_PLTE_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800935 if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
936 {
937 png_crc_finish(png_ptr, length);
938 return;
939 }
940#endif
941
942 if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3)
943 {
Chris Craikb50c2172013-07-29 15:28:30 -0700944 png_crc_finish(png_ptr, length);
945
The Android Open Source Project893912b2009-03-03 19:30:05 -0800946 if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
Chris Craikb50c2172013-07-29 15:28:30 -0700947 png_chunk_benign_error(png_ptr, "invalid");
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400948
The Android Open Source Project893912b2009-03-03 19:30:05 -0800949 else
Chris Craikb50c2172013-07-29 15:28:30 -0700950 png_chunk_error(png_ptr, "invalid");
951
952 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800953 }
954
Chris Craikb50c2172013-07-29 15:28:30 -0700955 /* The cast is safe because 'length' is less than 3*PNG_MAX_PALETTE_LENGTH */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800956 num = (int)length / 3;
957
Patrick Scott5f6bd842010-06-28 16:55:16 -0400958#ifdef PNG_POINTER_INDEXING_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800959 for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)
960 {
961 png_byte buf[3];
962
963 png_crc_read(png_ptr, buf, 3);
964 pal_ptr->red = buf[0];
965 pal_ptr->green = buf[1];
966 pal_ptr->blue = buf[2];
967 }
968#else
969 for (i = 0; i < num; i++)
970 {
971 png_byte buf[3];
972
973 png_crc_read(png_ptr, buf, 3);
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400974 /* Don't depend upon png_color being any order */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800975 palette[i].red = buf[0];
976 palette[i].green = buf[1];
977 palette[i].blue = buf[2];
978 }
979#endif
980
Chris Craikb50c2172013-07-29 15:28:30 -0700981 /* If we actually need the PLTE chunk (ie for a paletted image), we do
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400982 * whatever the normal CRC configuration tells us. However, if we
983 * have an RGB image, the PLTE can be considered ancillary, so
984 * we will act as though it is.
985 */
Patrick Scott5f6bd842010-06-28 16:55:16 -0400986#ifndef PNG_READ_OPT_PLTE_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800987 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
988#endif
989 {
990 png_crc_finish(png_ptr, 0);
991 }
Chris Craikb50c2172013-07-29 15:28:30 -0700992
Patrick Scott5f6bd842010-06-28 16:55:16 -0400993#ifndef PNG_READ_OPT_PLTE_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800994 else if (png_crc_error(png_ptr)) /* Only if we have a CRC error */
995 {
996 /* If we don't want to use the data from an ancillary chunk,
Chris Craikb50c2172013-07-29 15:28:30 -0700997 * we have two options: an error abort, or a warning and we
998 * ignore the data in this chunk (which should be OK, since
999 * it's considered ancillary for a RGB or RGBA image).
1000 *
1001 * IMPLEMENTATION NOTE: this is only here because png_crc_finish uses the
1002 * chunk type to determine whether to check the ancillary or the critical
1003 * flags.
1004 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001005 if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE))
1006 {
1007 if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301008 return;
Chris Craikb50c2172013-07-29 15:28:30 -07001009
The Android Open Source Project893912b2009-03-03 19:30:05 -08001010 else
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301011 png_chunk_error(png_ptr, "CRC error");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001012 }
Chris Craikb50c2172013-07-29 15:28:30 -07001013
The Android Open Source Project893912b2009-03-03 19:30:05 -08001014 /* Otherwise, we (optionally) emit a warning and use the chunk. */
1015 else if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN))
The Android Open Source Project893912b2009-03-03 19:30:05 -08001016 png_chunk_warning(png_ptr, "CRC error");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001017 }
1018#endif
1019
Chris Craikb50c2172013-07-29 15:28:30 -07001020 /* TODO: png_set_PLTE has the side effect of setting png_ptr->palette to its
1021 * own copy of the palette. This has the side effect that when png_start_row
1022 * is called (this happens after any call to png_read_update_info) the
1023 * info_ptr palette gets changed. This is extremely unexpected and
1024 * confusing.
1025 *
1026 * Fix this by not sharing the palette in this way.
1027 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001028 png_set_PLTE(png_ptr, info_ptr, palette, num);
1029
Chris Craikb50c2172013-07-29 15:28:30 -07001030 /* The three chunks, bKGD, hIST and tRNS *must* appear after PLTE and before
1031 * IDAT. Prior to 1.6.0 this was not checked; instead the code merely
1032 * checked the apparent validity of a tRNS chunk inserted before PLTE on a
1033 * palette PNG. 1.6.0 attempts to rigorously follow the standard and
1034 * therefore does a benign error if the erroneous condition is detected *and*
1035 * cancels the tRNS if the benign error returns. The alternative is to
1036 * amend the standard since it would be rather hypocritical of the standards
1037 * maintainers to ignore it.
1038 */
Patrick Scott5f6bd842010-06-28 16:55:16 -04001039#ifdef PNG_READ_tRNS_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001040 if (png_ptr->num_trans > 0 ||
1041 (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS) != 0))
The Android Open Source Project893912b2009-03-03 19:30:05 -08001042 {
Chris Craikb50c2172013-07-29 15:28:30 -07001043 /* Cancel this because otherwise it would be used if the transforms
1044 * require it. Don't cancel the 'valid' flag because this would prevent
1045 * detection of duplicate chunks.
1046 */
1047 png_ptr->num_trans = 0;
1048
1049 if (info_ptr != NULL)
1050 info_ptr->num_trans = 0;
1051
1052 png_chunk_benign_error(png_ptr, "tRNS must be after");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001053 }
1054#endif
1055
Chris Craikb50c2172013-07-29 15:28:30 -07001056#ifdef PNG_READ_hIST_SUPPORTED
1057 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) != 0)
1058 png_chunk_benign_error(png_ptr, "hIST must be after");
1059#endif
1060
1061#ifdef PNG_READ_bKGD_SUPPORTED
1062 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) != 0)
1063 png_chunk_benign_error(png_ptr, "bKGD must be after");
1064#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -08001065}
1066
1067void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07001068png_handle_IEND(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001069{
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001070 png_debug(1, "in png_handle_IEND");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001071
1072 if (!(png_ptr->mode & PNG_HAVE_IHDR) || !(png_ptr->mode & PNG_HAVE_IDAT))
Chris Craikb50c2172013-07-29 15:28:30 -07001073 png_chunk_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001074
1075 png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND);
1076
The Android Open Source Project893912b2009-03-03 19:30:05 -08001077 png_crc_finish(png_ptr, length);
1078
Chris Craikb50c2172013-07-29 15:28:30 -07001079 if (length != 0)
1080 png_chunk_benign_error(png_ptr, "invalid");
1081
1082 PNG_UNUSED(info_ptr)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001083}
1084
Patrick Scott5f6bd842010-06-28 16:55:16 -04001085#ifdef PNG_READ_gAMA_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001086void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07001087png_handle_gAMA(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001088{
1089 png_fixed_point igamma;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001090 png_byte buf[4];
1091
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001092 png_debug(1, "in png_handle_gAMA");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001093
1094 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Chris Craikb50c2172013-07-29 15:28:30 -07001095 png_chunk_error(png_ptr, "missing IHDR");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001096
Chris Craikb50c2172013-07-29 15:28:30 -07001097 else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE))
The Android Open Source Project893912b2009-03-03 19:30:05 -08001098 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001099 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001100 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001101 return;
1102 }
1103
1104 if (length != 4)
1105 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001106 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001107 png_chunk_benign_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001108 return;
1109 }
1110
1111 png_crc_read(png_ptr, buf, 4);
Chris Craikb50c2172013-07-29 15:28:30 -07001112
The Android Open Source Project893912b2009-03-03 19:30:05 -08001113 if (png_crc_finish(png_ptr, 0))
1114 return;
1115
Chris Craikb50c2172013-07-29 15:28:30 -07001116 igamma = png_get_fixed_point(NULL, buf);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001117
Chris Craikb50c2172013-07-29 15:28:30 -07001118 png_colorspace_set_gamma(png_ptr, &png_ptr->colorspace, igamma);
1119 png_colorspace_sync(png_ptr, info_ptr);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001120}
1121#endif
1122
Patrick Scott5f6bd842010-06-28 16:55:16 -04001123#ifdef PNG_READ_sBIT_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001124void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07001125png_handle_sBIT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001126{
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301127 unsigned int truelen, i;
1128 png_byte sample_depth;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001129 png_byte buf[4];
1130
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001131 png_debug(1, "in png_handle_sBIT");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001132
The Android Open Source Project893912b2009-03-03 19:30:05 -08001133 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Chris Craikb50c2172013-07-29 15:28:30 -07001134 png_chunk_error(png_ptr, "missing IHDR");
1135
1136 else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE))
The Android Open Source Project893912b2009-03-03 19:30:05 -08001137 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001138 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001139 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001140 return;
1141 }
Chris Craikb50c2172013-07-29 15:28:30 -07001142
The Android Open Source Project893912b2009-03-03 19:30:05 -08001143 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT))
1144 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001145 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001146 png_chunk_benign_error(png_ptr, "duplicate");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001147 return;
1148 }
1149
1150 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301151 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001152 truelen = 3;
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301153 sample_depth = 8;
1154 }
Chris Craikb50c2172013-07-29 15:28:30 -07001155
The Android Open Source Project893912b2009-03-03 19:30:05 -08001156 else
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301157 {
Chris Craikb50c2172013-07-29 15:28:30 -07001158 truelen = png_ptr->channels;
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301159 sample_depth = png_ptr->bit_depth;
1160 }
The Android Open Source Project893912b2009-03-03 19:30:05 -08001161
1162 if (length != truelen || length > 4)
1163 {
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 png_crc_finish(png_ptr, length);
1166 return;
1167 }
1168
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301169 buf[0] = buf[1] = buf[2] = buf[3] = sample_depth;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001170 png_crc_read(png_ptr, buf, truelen);
Chris Craikb50c2172013-07-29 15:28:30 -07001171
The Android Open Source Project893912b2009-03-03 19:30:05 -08001172 if (png_crc_finish(png_ptr, 0))
1173 return;
1174
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301175 for (i=0; i<truelen; ++i)
1176 if (buf[i] == 0 || buf[i] > sample_depth)
1177 {
1178 png_chunk_benign_error(png_ptr, "invalid");
1179 return;
1180 }
1181
The Android Open Source Project893912b2009-03-03 19:30:05 -08001182 if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
1183 {
1184 png_ptr->sig_bit.red = buf[0];
1185 png_ptr->sig_bit.green = buf[1];
1186 png_ptr->sig_bit.blue = buf[2];
1187 png_ptr->sig_bit.alpha = buf[3];
1188 }
Chris Craikb50c2172013-07-29 15:28:30 -07001189
The Android Open Source Project893912b2009-03-03 19:30:05 -08001190 else
1191 {
1192 png_ptr->sig_bit.gray = buf[0];
1193 png_ptr->sig_bit.red = buf[0];
1194 png_ptr->sig_bit.green = buf[0];
1195 png_ptr->sig_bit.blue = buf[0];
1196 png_ptr->sig_bit.alpha = buf[1];
1197 }
Chris Craikb50c2172013-07-29 15:28:30 -07001198
The Android Open Source Project893912b2009-03-03 19:30:05 -08001199 png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
1200}
1201#endif
1202
Patrick Scott5f6bd842010-06-28 16:55:16 -04001203#ifdef PNG_READ_cHRM_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001204void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07001205png_handle_cHRM(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001206{
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001207 png_byte buf[32];
Chris Craikb50c2172013-07-29 15:28:30 -07001208 png_xy xy;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001209
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001210 png_debug(1, "in png_handle_cHRM");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001211
1212 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Chris Craikb50c2172013-07-29 15:28:30 -07001213 png_chunk_error(png_ptr, "missing IHDR");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001214
Chris Craikb50c2172013-07-29 15:28:30 -07001215 else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE))
The Android Open Source Project893912b2009-03-03 19:30:05 -08001216 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001217 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001218 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001219 return;
1220 }
1221
1222 if (length != 32)
1223 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001224 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001225 png_chunk_benign_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001226 return;
1227 }
1228
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001229 png_crc_read(png_ptr, buf, 32);
Chris Craikb50c2172013-07-29 15:28:30 -07001230
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001231 if (png_crc_finish(png_ptr, 0))
The Android Open Source Project893912b2009-03-03 19:30:05 -08001232 return;
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001233
Chris Craikb50c2172013-07-29 15:28:30 -07001234 xy.whitex = png_get_fixed_point(NULL, buf);
1235 xy.whitey = png_get_fixed_point(NULL, buf + 4);
1236 xy.redx = png_get_fixed_point(NULL, buf + 8);
1237 xy.redy = png_get_fixed_point(NULL, buf + 12);
1238 xy.greenx = png_get_fixed_point(NULL, buf + 16);
1239 xy.greeny = png_get_fixed_point(NULL, buf + 20);
1240 xy.bluex = png_get_fixed_point(NULL, buf + 24);
1241 xy.bluey = png_get_fixed_point(NULL, buf + 28);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001242
Chris Craikb50c2172013-07-29 15:28:30 -07001243 if (xy.whitex == PNG_FIXED_ERROR ||
1244 xy.whitey == PNG_FIXED_ERROR ||
1245 xy.redx == PNG_FIXED_ERROR ||
1246 xy.redy == PNG_FIXED_ERROR ||
1247 xy.greenx == PNG_FIXED_ERROR ||
1248 xy.greeny == PNG_FIXED_ERROR ||
1249 xy.bluex == PNG_FIXED_ERROR ||
1250 xy.bluey == PNG_FIXED_ERROR)
1251 {
1252 png_chunk_benign_error(png_ptr, "invalid values");
1253 return;
1254 }
The Android Open Source Project893912b2009-03-03 19:30:05 -08001255
Chris Craikb50c2172013-07-29 15:28:30 -07001256 /* If a colorspace error has already been output skip this chunk */
1257 if (png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID)
1258 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001259
Chris Craikb50c2172013-07-29 15:28:30 -07001260 if (png_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM)
1261 {
1262 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1263 png_colorspace_sync(png_ptr, info_ptr);
1264 png_chunk_benign_error(png_ptr, "duplicate");
1265 return;
1266 }
The Android Open Source Project893912b2009-03-03 19:30:05 -08001267
Chris Craikb50c2172013-07-29 15:28:30 -07001268 png_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM;
1269 (void)png_colorspace_set_chromaticities(png_ptr, &png_ptr->colorspace, &xy,
1270 1/*prefer cHRM values*/);
1271 png_colorspace_sync(png_ptr, info_ptr);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001272}
1273#endif
1274
Patrick Scott5f6bd842010-06-28 16:55:16 -04001275#ifdef PNG_READ_sRGB_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001276void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07001277png_handle_sRGB(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001278{
Chris Craikb50c2172013-07-29 15:28:30 -07001279 png_byte intent;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001280
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001281 png_debug(1, "in png_handle_sRGB");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001282
1283 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Chris Craikb50c2172013-07-29 15:28:30 -07001284 png_chunk_error(png_ptr, "missing IHDR");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001285
Chris Craikb50c2172013-07-29 15:28:30 -07001286 else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE))
The Android Open Source Project893912b2009-03-03 19:30:05 -08001287 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001288 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001289 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001290 return;
1291 }
1292
1293 if (length != 1)
1294 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001295 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001296 png_chunk_benign_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001297 return;
1298 }
1299
Chris Craikb50c2172013-07-29 15:28:30 -07001300 png_crc_read(png_ptr, &intent, 1);
1301
The Android Open Source Project893912b2009-03-03 19:30:05 -08001302 if (png_crc_finish(png_ptr, 0))
1303 return;
1304
Chris Craikb50c2172013-07-29 15:28:30 -07001305 /* If a colorspace error has already been output skip this chunk */
1306 if (png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID)
1307 return;
1308
1309 /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect
1310 * this.
1311 */
1312 if (png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001313 {
Chris Craikb50c2172013-07-29 15:28:30 -07001314 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1315 png_colorspace_sync(png_ptr, info_ptr);
1316 png_chunk_benign_error(png_ptr, "too many profiles");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001317 return;
1318 }
1319
Chris Craikb50c2172013-07-29 15:28:30 -07001320 (void)png_colorspace_set_sRGB(png_ptr, &png_ptr->colorspace, intent);
1321 png_colorspace_sync(png_ptr, info_ptr);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001322}
1323#endif /* PNG_READ_sRGB_SUPPORTED */
1324
Patrick Scott5f6bd842010-06-28 16:55:16 -04001325#ifdef PNG_READ_iCCP_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001326void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07001327png_handle_iCCP(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1328/* Note: this does not properly handle profiles that are > 64K under DOS */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001329{
Chris Craikb50c2172013-07-29 15:28:30 -07001330 png_const_charp errmsg = NULL; /* error message output, or no error */
1331 int finished = 0; /* crc checked */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001332
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001333 png_debug(1, "in png_handle_iCCP");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001334
1335 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Chris Craikb50c2172013-07-29 15:28:30 -07001336 png_chunk_error(png_ptr, "missing IHDR");
1337
1338 else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE))
The Android Open Source Project893912b2009-03-03 19:30:05 -08001339 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001340 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001341 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001342 return;
1343 }
1344
Chris Craikb50c2172013-07-29 15:28:30 -07001345 /* Consistent with all the above colorspace handling an obviously *invalid*
1346 * chunk is just ignored, so does not invalidate the color space. An
1347 * alternative is to set the 'invalid' flags at the start of this routine
1348 * and only clear them in they were not set before and all the tests pass.
1349 * The minimum 'deflate' stream is assumed to be just the 2 byte header and 4
1350 * byte checksum. The keyword must be one character and there is a
1351 * terminator (0) byte and the compression method.
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001352 */
Chris Craikb50c2172013-07-29 15:28:30 -07001353 if (length < 9)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001354 {
Chris Craikb50c2172013-07-29 15:28:30 -07001355 png_crc_finish(png_ptr, length);
1356 png_chunk_benign_error(png_ptr, "too short");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001357 return;
1358 }
1359
Chris Craikb50c2172013-07-29 15:28:30 -07001360 /* If a colorspace error has already been output skip this chunk */
1361 if (png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001362 {
Chris Craikb50c2172013-07-29 15:28:30 -07001363 png_crc_finish(png_ptr, length);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001364 return;
1365 }
1366
Chris Craikb50c2172013-07-29 15:28:30 -07001367 /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect
1368 * this.
1369 */
1370 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) == 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001371 {
Chris Craikb50c2172013-07-29 15:28:30 -07001372 uInt read_length, keyword_length;
1373 char keyword[81];
1374
1375 /* Find the keyword; the keyword plus separator and compression method
1376 * bytes can be at most 81 characters long.
1377 */
1378 read_length = 81; /* maximum */
1379 if (read_length > length)
1380 read_length = (uInt)length;
1381
1382 png_crc_read(png_ptr, (png_bytep)keyword, read_length);
1383 length -= read_length;
1384
1385 keyword_length = 0;
1386 while (keyword_length < 80 && keyword_length < read_length &&
1387 keyword[keyword_length] != 0)
1388 ++keyword_length;
1389
1390 /* TODO: make the keyword checking common */
1391 if (keyword_length >= 1 && keyword_length <= 79)
1392 {
1393 /* We only understand '0' compression - deflate - so if we get a
1394 * different value we can't safely decode the chunk.
1395 */
1396 if (keyword_length+1 < read_length &&
1397 keyword[keyword_length+1] == PNG_COMPRESSION_TYPE_BASE)
1398 {
1399 read_length -= keyword_length+2;
1400
1401 if (png_inflate_claim(png_ptr, png_iCCP) == Z_OK)
1402 {
1403 Byte profile_header[132];
1404 Byte local_buffer[PNG_INFLATE_BUF_SIZE];
1405 png_alloc_size_t size = (sizeof profile_header);
1406
1407 png_ptr->zstream.next_in = (Bytef*)keyword + (keyword_length+2);
1408 png_ptr->zstream.avail_in = read_length;
1409 (void)png_inflate_read(png_ptr, local_buffer,
1410 (sizeof local_buffer), &length, profile_header, &size,
1411 0/*finish: don't, because the output is too small*/);
1412
1413 if (size == 0)
1414 {
1415 /* We have the ICC profile header; do the basic header checks.
1416 */
1417 const png_uint_32 profile_length =
1418 png_get_uint_32(profile_header);
1419
1420 if (png_icc_check_length(png_ptr, &png_ptr->colorspace,
1421 keyword, profile_length))
1422 {
1423 /* The length is apparently ok, so we can check the 132
1424 * byte header.
1425 */
1426 if (png_icc_check_header(png_ptr, &png_ptr->colorspace,
1427 keyword, profile_length, profile_header,
1428 png_ptr->color_type))
1429 {
1430 /* Now read the tag table; a variable size buffer is
1431 * needed at this point, allocate one for the whole
1432 * profile. The header check has already validated
1433 * that none of these stuff will overflow.
1434 */
1435 const png_uint_32 tag_count = png_get_uint_32(
1436 profile_header+128);
1437 png_bytep profile = png_read_buffer(png_ptr,
1438 profile_length, 2/*silent*/);
1439
1440 if (profile != NULL)
1441 {
1442 memcpy(profile, profile_header,
1443 (sizeof profile_header));
1444
1445 size = 12 * tag_count;
1446
1447 (void)png_inflate_read(png_ptr, local_buffer,
1448 (sizeof local_buffer), &length,
1449 profile + (sizeof profile_header), &size, 0);
1450
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301451 /* Still expect a buffer error because we expect
Chris Craikb50c2172013-07-29 15:28:30 -07001452 * there to be some tag data!
1453 */
1454 if (size == 0)
1455 {
1456 if (png_icc_check_tag_table(png_ptr,
1457 &png_ptr->colorspace, keyword, profile_length,
1458 profile))
1459 {
1460 /* The profile has been validated for basic
1461 * security issues, so read the whole thing in.
1462 */
1463 size = profile_length - (sizeof profile_header)
1464 - 12 * tag_count;
1465
1466 (void)png_inflate_read(png_ptr, local_buffer,
1467 (sizeof local_buffer), &length,
1468 profile + (sizeof profile_header) +
1469 12 * tag_count, &size, 1/*finish*/);
1470
1471 if (length > 0 && !(png_ptr->flags &
1472 PNG_FLAG_BENIGN_ERRORS_WARN))
1473 errmsg = "extra compressed data";
1474
1475 /* But otherwise allow extra data: */
1476 else if (size == 0)
1477 {
1478 if (length > 0)
1479 {
1480 /* This can be handled completely, so
1481 * keep going.
1482 */
1483 png_chunk_warning(png_ptr,
1484 "extra compressed data");
1485 }
1486
1487 png_crc_finish(png_ptr, length);
1488 finished = 1;
1489
1490# ifdef PNG_sRGB_SUPPORTED
1491 /* Check for a match against sRGB */
1492 png_icc_set_sRGB(png_ptr,
1493 &png_ptr->colorspace, profile,
1494 png_ptr->zstream.adler);
1495# endif
1496
1497 /* Steal the profile for info_ptr. */
1498 if (info_ptr != NULL)
1499 {
1500 png_free_data(png_ptr, info_ptr,
1501 PNG_FREE_ICCP, 0);
1502
1503 info_ptr->iccp_name = png_voidcast(char*,
1504 png_malloc_base(png_ptr,
1505 keyword_length+1));
1506 if (info_ptr->iccp_name != NULL)
1507 {
1508 memcpy(info_ptr->iccp_name, keyword,
1509 keyword_length+1);
1510 info_ptr->iccp_proflen =
1511 profile_length;
1512 info_ptr->iccp_profile = profile;
1513 png_ptr->read_buffer = NULL; /*steal*/
1514 info_ptr->free_me |= PNG_FREE_ICCP;
1515 info_ptr->valid |= PNG_INFO_iCCP;
1516 }
1517
1518 else
1519 {
1520 png_ptr->colorspace.flags |=
1521 PNG_COLORSPACE_INVALID;
1522 errmsg = "out of memory";
1523 }
1524 }
1525
1526 /* else the profile remains in the read
1527 * buffer which gets reused for subsequent
1528 * chunks.
1529 */
1530
1531 if (info_ptr != NULL)
1532 png_colorspace_sync(png_ptr, info_ptr);
1533
1534 if (errmsg == NULL)
1535 {
1536 png_ptr->zowner = 0;
1537 return;
1538 }
1539 }
1540
1541 else if (size > 0)
1542 errmsg = "truncated";
1543
1544 else
1545 errmsg = png_ptr->zstream.msg;
1546 }
1547
1548 /* else png_icc_check_tag_table output an error */
1549 }
1550
1551 else /* profile truncated */
1552 errmsg = png_ptr->zstream.msg;
1553 }
1554
1555 else
1556 errmsg = "out of memory";
1557 }
1558
1559 /* else png_icc_check_header output an error */
1560 }
1561
1562 /* else png_icc_check_length output an error */
1563 }
1564
1565 else /* profile truncated */
1566 errmsg = png_ptr->zstream.msg;
1567
1568 /* Release the stream */
1569 png_ptr->zowner = 0;
1570 }
1571
1572 else /* png_inflate_claim failed */
1573 errmsg = png_ptr->zstream.msg;
1574 }
1575
1576 else
1577 errmsg = "bad compression method"; /* or missing */
1578 }
1579
1580 else
1581 errmsg = "bad keyword";
The Android Open Source Project893912b2009-03-03 19:30:05 -08001582 }
1583
Chris Craikb50c2172013-07-29 15:28:30 -07001584 else
1585 errmsg = "too many profiles";
1586
1587 /* Failure: the reason is in 'errmsg' */
1588 if (!finished)
1589 png_crc_finish(png_ptr, length);
1590
1591 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1592 png_colorspace_sync(png_ptr, info_ptr);
1593 if (errmsg != NULL) /* else already output */
1594 png_chunk_benign_error(png_ptr, errmsg);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001595}
1596#endif /* PNG_READ_iCCP_SUPPORTED */
1597
Patrick Scott5f6bd842010-06-28 16:55:16 -04001598#ifdef PNG_READ_sPLT_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001599void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07001600png_handle_sPLT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001601/* Note: this does not properly handle chunks that are > 64K under DOS */
1602{
Chris Craikb50c2172013-07-29 15:28:30 -07001603 png_bytep entry_start, buffer;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001604 png_sPLT_t new_palette;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001605 png_sPLT_entryp pp;
Chris Craikb50c2172013-07-29 15:28:30 -07001606 png_uint_32 data_length;
1607 int entry_size, i;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001608 png_uint_32 skip = 0;
Chris Craikb50c2172013-07-29 15:28:30 -07001609 png_uint_32 dl;
1610 png_size_t max_dl;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001611
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001612 png_debug(1, "in png_handle_sPLT");
1613
Patrick Scott5f6bd842010-06-28 16:55:16 -04001614#ifdef PNG_USER_LIMITS_SUPPORTED
Patrick Scott5f6bd842010-06-28 16:55:16 -04001615 if (png_ptr->user_chunk_cache_max != 0)
1616 {
1617 if (png_ptr->user_chunk_cache_max == 1)
1618 {
1619 png_crc_finish(png_ptr, length);
1620 return;
1621 }
Chris Craikb50c2172013-07-29 15:28:30 -07001622
Patrick Scott5f6bd842010-06-28 16:55:16 -04001623 if (--png_ptr->user_chunk_cache_max == 1)
1624 {
1625 png_warning(png_ptr, "No space in chunk cache for sPLT");
1626 png_crc_finish(png_ptr, length);
1627 return;
1628 }
1629 }
1630#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -08001631
1632 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Chris Craikb50c2172013-07-29 15:28:30 -07001633 png_chunk_error(png_ptr, "missing IHDR");
1634
The Android Open Source Project893912b2009-03-03 19:30:05 -08001635 else if (png_ptr->mode & PNG_HAVE_IDAT)
1636 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001637 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001638 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001639 return;
1640 }
1641
1642#ifdef PNG_MAX_MALLOC_64K
Chris Craikb50c2172013-07-29 15:28:30 -07001643 if (length > 65535U)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001644 {
Chris Craikb50c2172013-07-29 15:28:30 -07001645 png_crc_finish(png_ptr, length);
1646 png_chunk_benign_error(png_ptr, "too large to fit in memory");
1647 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001648 }
1649#endif
1650
Chris Craikb50c2172013-07-29 15:28:30 -07001651 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
1652 if (buffer == NULL)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001653 {
Chris Craikb50c2172013-07-29 15:28:30 -07001654 png_crc_finish(png_ptr, length);
1655 png_chunk_benign_error(png_ptr, "out of memory");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001656 return;
1657 }
1658
The Android Open Source Project893912b2009-03-03 19:30:05 -08001659
Chris Craikb50c2172013-07-29 15:28:30 -07001660 /* WARNING: this may break if size_t is less than 32 bits; it is assumed
1661 * that the PNG_MAX_MALLOC_64K test is enabled in this case, but this is a
1662 * potential breakage point if the types in pngconf.h aren't exactly right.
1663 */
1664 png_crc_read(png_ptr, buffer, length);
1665
1666 if (png_crc_finish(png_ptr, skip))
1667 return;
1668
1669 buffer[length] = 0;
1670
1671 for (entry_start = buffer; *entry_start; entry_start++)
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001672 /* Empty loop to find end of name */ ;
Chris Craikb50c2172013-07-29 15:28:30 -07001673
The Android Open Source Project893912b2009-03-03 19:30:05 -08001674 ++entry_start;
1675
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001676 /* A sample depth should follow the separator, and we should be on it */
Chris Craikb50c2172013-07-29 15:28:30 -07001677 if (entry_start > buffer + length - 2)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001678 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001679 png_warning(png_ptr, "malformed sPLT chunk");
1680 return;
1681 }
1682
1683 new_palette.depth = *entry_start++;
1684 entry_size = (new_palette.depth == 8 ? 6 : 10);
Chris Craikb50c2172013-07-29 15:28:30 -07001685 /* This must fit in a png_uint_32 because it is derived from the original
1686 * chunk data length.
1687 */
1688 data_length = length - (png_uint_32)(entry_start - buffer);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001689
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001690 /* Integrity-check the data length */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001691 if (data_length % entry_size)
1692 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001693 png_warning(png_ptr, "sPLT chunk has bad length");
1694 return;
1695 }
1696
Chris Craikb50c2172013-07-29 15:28:30 -07001697 dl = (png_int_32)(data_length / entry_size);
1698 max_dl = PNG_SIZE_MAX / (sizeof (png_sPLT_entry));
1699
1700 if (dl > max_dl)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001701 {
1702 png_warning(png_ptr, "sPLT chunk too long");
1703 return;
1704 }
Chris Craikb50c2172013-07-29 15:28:30 -07001705
1706 new_palette.nentries = (png_int_32)(data_length / entry_size);
1707
The Android Open Source Project893912b2009-03-03 19:30:05 -08001708 new_palette.entries = (png_sPLT_entryp)png_malloc_warn(
Chris Craikb50c2172013-07-29 15:28:30 -07001709 png_ptr, new_palette.nentries * (sizeof (png_sPLT_entry)));
1710
The Android Open Source Project893912b2009-03-03 19:30:05 -08001711 if (new_palette.entries == NULL)
1712 {
1713 png_warning(png_ptr, "sPLT chunk requires too much memory");
1714 return;
1715 }
1716
Patrick Scott5f6bd842010-06-28 16:55:16 -04001717#ifdef PNG_POINTER_INDEXING_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001718 for (i = 0; i < new_palette.nentries; i++)
1719 {
Patrick Scott5f6bd842010-06-28 16:55:16 -04001720 pp = new_palette.entries + i;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001721
1722 if (new_palette.depth == 8)
1723 {
Chris Craikb50c2172013-07-29 15:28:30 -07001724 pp->red = *entry_start++;
1725 pp->green = *entry_start++;
1726 pp->blue = *entry_start++;
1727 pp->alpha = *entry_start++;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001728 }
Chris Craikb50c2172013-07-29 15:28:30 -07001729
The Android Open Source Project893912b2009-03-03 19:30:05 -08001730 else
1731 {
Chris Craikb50c2172013-07-29 15:28:30 -07001732 pp->red = png_get_uint_16(entry_start); entry_start += 2;
1733 pp->green = png_get_uint_16(entry_start); entry_start += 2;
1734 pp->blue = png_get_uint_16(entry_start); entry_start += 2;
1735 pp->alpha = png_get_uint_16(entry_start); entry_start += 2;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001736 }
Chris Craikb50c2172013-07-29 15:28:30 -07001737
The Android Open Source Project893912b2009-03-03 19:30:05 -08001738 pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
1739 }
1740#else
1741 pp = new_palette.entries;
Chris Craikb50c2172013-07-29 15:28:30 -07001742
The Android Open Source Project893912b2009-03-03 19:30:05 -08001743 for (i = 0; i < new_palette.nentries; i++)
1744 {
1745
1746 if (new_palette.depth == 8)
1747 {
Chris Craikb50c2172013-07-29 15:28:30 -07001748 pp[i].red = *entry_start++;
1749 pp[i].green = *entry_start++;
1750 pp[i].blue = *entry_start++;
1751 pp[i].alpha = *entry_start++;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001752 }
Chris Craikb50c2172013-07-29 15:28:30 -07001753
The Android Open Source Project893912b2009-03-03 19:30:05 -08001754 else
1755 {
Chris Craikb50c2172013-07-29 15:28:30 -07001756 pp[i].red = png_get_uint_16(entry_start); entry_start += 2;
1757 pp[i].green = png_get_uint_16(entry_start); entry_start += 2;
1758 pp[i].blue = png_get_uint_16(entry_start); entry_start += 2;
1759 pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001760 }
Chris Craikb50c2172013-07-29 15:28:30 -07001761
1762 pp[i].frequency = png_get_uint_16(entry_start); entry_start += 2;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001763 }
1764#endif
1765
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001766 /* Discard all chunk data except the name and stash that */
Chris Craikb50c2172013-07-29 15:28:30 -07001767 new_palette.name = (png_charp)buffer;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001768
1769 png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);
1770
The Android Open Source Project893912b2009-03-03 19:30:05 -08001771 png_free(png_ptr, new_palette.entries);
1772}
1773#endif /* PNG_READ_sPLT_SUPPORTED */
1774
Patrick Scott5f6bd842010-06-28 16:55:16 -04001775#ifdef PNG_READ_tRNS_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001776void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07001777png_handle_tRNS(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001778{
1779 png_byte readbuf[PNG_MAX_PALETTE_LENGTH];
1780
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001781 png_debug(1, "in png_handle_tRNS");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001782
1783 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Chris Craikb50c2172013-07-29 15:28:30 -07001784 png_chunk_error(png_ptr, "missing IHDR");
1785
The Android Open Source Project893912b2009-03-03 19:30:05 -08001786 else if (png_ptr->mode & PNG_HAVE_IDAT)
1787 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001788 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001789 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001790 return;
1791 }
Chris Craikb50c2172013-07-29 15:28:30 -07001792
The Android Open Source Project893912b2009-03-03 19:30:05 -08001793 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
1794 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001795 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001796 png_chunk_benign_error(png_ptr, "duplicate");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001797 return;
1798 }
1799
1800 if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
1801 {
1802 png_byte buf[2];
1803
1804 if (length != 2)
1805 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001806 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001807 png_chunk_benign_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001808 return;
1809 }
1810
1811 png_crc_read(png_ptr, buf, 2);
1812 png_ptr->num_trans = 1;
Chris Craikb50c2172013-07-29 15:28:30 -07001813 png_ptr->trans_color.gray = png_get_uint_16(buf);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001814 }
Chris Craikb50c2172013-07-29 15:28:30 -07001815
The Android Open Source Project893912b2009-03-03 19:30:05 -08001816 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
1817 {
1818 png_byte buf[6];
1819
1820 if (length != 6)
1821 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001822 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001823 png_chunk_benign_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001824 return;
1825 }
Chris Craikb50c2172013-07-29 15:28:30 -07001826
1827 png_crc_read(png_ptr, buf, length);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001828 png_ptr->num_trans = 1;
Chris Craikb50c2172013-07-29 15:28:30 -07001829 png_ptr->trans_color.red = png_get_uint_16(buf);
1830 png_ptr->trans_color.green = png_get_uint_16(buf + 2);
1831 png_ptr->trans_color.blue = png_get_uint_16(buf + 4);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001832 }
Chris Craikb50c2172013-07-29 15:28:30 -07001833
The Android Open Source Project893912b2009-03-03 19:30:05 -08001834 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1835 {
1836 if (!(png_ptr->mode & PNG_HAVE_PLTE))
1837 {
Chris Craikb50c2172013-07-29 15:28:30 -07001838 /* TODO: is this actually an error in the ISO spec? */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001839 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001840 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001841 return;
1842 }
Chris Craikb50c2172013-07-29 15:28:30 -07001843
1844 if (length > png_ptr->num_palette || length > PNG_MAX_PALETTE_LENGTH ||
1845 length == 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001846 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001847 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001848 png_chunk_benign_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001849 return;
1850 }
Chris Craikb50c2172013-07-29 15:28:30 -07001851
1852 png_crc_read(png_ptr, readbuf, length);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001853 png_ptr->num_trans = (png_uint_16)length;
1854 }
Chris Craikb50c2172013-07-29 15:28:30 -07001855
The Android Open Source Project893912b2009-03-03 19:30:05 -08001856 else
1857 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001858 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001859 png_chunk_benign_error(png_ptr, "invalid with alpha channel");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001860 return;
1861 }
1862
1863 if (png_crc_finish(png_ptr, 0))
1864 {
1865 png_ptr->num_trans = 0;
1866 return;
1867 }
1868
Chris Craikb50c2172013-07-29 15:28:30 -07001869 /* TODO: this is a horrible side effect in the palette case because the
1870 * png_struct ends up with a pointer to the tRNS buffer owned by the
1871 * png_info. Fix this.
1872 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001873 png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,
Chris Craikb50c2172013-07-29 15:28:30 -07001874 &(png_ptr->trans_color));
The Android Open Source Project893912b2009-03-03 19:30:05 -08001875}
1876#endif
1877
Patrick Scott5f6bd842010-06-28 16:55:16 -04001878#ifdef PNG_READ_bKGD_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001879void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07001880png_handle_bKGD(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001881{
Chris Craikb50c2172013-07-29 15:28:30 -07001882 unsigned int truelen;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001883 png_byte buf[6];
Chris Craikb50c2172013-07-29 15:28:30 -07001884 png_color_16 background;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001885
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001886 png_debug(1, "in png_handle_bKGD");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001887
1888 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Chris Craikb50c2172013-07-29 15:28:30 -07001889 png_chunk_error(png_ptr, "missing IHDR");
1890
1891 else if ((png_ptr->mode & PNG_HAVE_IDAT) ||
1892 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
1893 !(png_ptr->mode & PNG_HAVE_PLTE)))
The Android Open Source Project893912b2009-03-03 19:30:05 -08001894 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001895 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001896 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001897 return;
1898 }
Chris Craikb50c2172013-07-29 15:28:30 -07001899
The Android Open Source Project893912b2009-03-03 19:30:05 -08001900 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD))
1901 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001902 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001903 png_chunk_benign_error(png_ptr, "duplicate");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001904 return;
1905 }
1906
1907 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1908 truelen = 1;
Chris Craikb50c2172013-07-29 15:28:30 -07001909
The Android Open Source Project893912b2009-03-03 19:30:05 -08001910 else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
1911 truelen = 6;
Chris Craikb50c2172013-07-29 15:28:30 -07001912
The Android Open Source Project893912b2009-03-03 19:30:05 -08001913 else
1914 truelen = 2;
1915
1916 if (length != truelen)
1917 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001918 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001919 png_chunk_benign_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001920 return;
1921 }
1922
1923 png_crc_read(png_ptr, buf, truelen);
Chris Craikb50c2172013-07-29 15:28:30 -07001924
The Android Open Source Project893912b2009-03-03 19:30:05 -08001925 if (png_crc_finish(png_ptr, 0))
1926 return;
1927
1928 /* We convert the index value into RGB components so that we can allow
1929 * arbitrary RGB values for background when we have transparency, and
1930 * so it is easy to determine the RGB values of the background color
Chris Craikb50c2172013-07-29 15:28:30 -07001931 * from the info_ptr struct.
1932 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001933 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1934 {
Chris Craikb50c2172013-07-29 15:28:30 -07001935 background.index = buf[0];
1936
The Android Open Source Project893912b2009-03-03 19:30:05 -08001937 if (info_ptr && info_ptr->num_palette)
1938 {
Chris Craikb50c2172013-07-29 15:28:30 -07001939 if (buf[0] >= info_ptr->num_palette)
1940 {
1941 png_chunk_benign_error(png_ptr, "invalid index");
1942 return;
1943 }
1944
1945 background.red = (png_uint_16)png_ptr->palette[buf[0]].red;
1946 background.green = (png_uint_16)png_ptr->palette[buf[0]].green;
1947 background.blue = (png_uint_16)png_ptr->palette[buf[0]].blue;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001948 }
Chris Craikb50c2172013-07-29 15:28:30 -07001949
1950 else
1951 background.red = background.green = background.blue = 0;
1952
1953 background.gray = 0;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001954 }
1955
Chris Craikb50c2172013-07-29 15:28:30 -07001956 else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) /* GRAY */
1957 {
1958 background.index = 0;
1959 background.red =
1960 background.green =
1961 background.blue =
1962 background.gray = png_get_uint_16(buf);
1963 }
1964
1965 else
1966 {
1967 background.index = 0;
1968 background.red = png_get_uint_16(buf);
1969 background.green = png_get_uint_16(buf + 2);
1970 background.blue = png_get_uint_16(buf + 4);
1971 background.gray = 0;
1972 }
1973
1974 png_set_bKGD(png_ptr, info_ptr, &background);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001975}
1976#endif
1977
Patrick Scott5f6bd842010-06-28 16:55:16 -04001978#ifdef PNG_READ_hIST_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001979void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07001980png_handle_hIST(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001981{
1982 unsigned int num, i;
1983 png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];
1984
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001985 png_debug(1, "in png_handle_hIST");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001986
1987 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Chris Craikb50c2172013-07-29 15:28:30 -07001988 png_chunk_error(png_ptr, "missing IHDR");
1989
1990 else if ((png_ptr->mode & PNG_HAVE_IDAT) || !(png_ptr->mode & PNG_HAVE_PLTE))
The Android Open Source Project893912b2009-03-03 19:30:05 -08001991 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001992 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001993 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001994 return;
1995 }
Chris Craikb50c2172013-07-29 15:28:30 -07001996
The Android Open Source Project893912b2009-03-03 19:30:05 -08001997 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST))
1998 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001999 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002000 png_chunk_benign_error(png_ptr, "duplicate");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002001 return;
2002 }
2003
2004 num = length / 2 ;
Chris Craikb50c2172013-07-29 15:28:30 -07002005
2006 if (num != png_ptr->num_palette || num > PNG_MAX_PALETTE_LENGTH)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002007 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002008 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002009 png_chunk_benign_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002010 return;
2011 }
2012
2013 for (i = 0; i < num; i++)
2014 {
2015 png_byte buf[2];
2016
2017 png_crc_read(png_ptr, buf, 2);
2018 readbuf[i] = png_get_uint_16(buf);
2019 }
2020
2021 if (png_crc_finish(png_ptr, 0))
2022 return;
2023
2024 png_set_hIST(png_ptr, info_ptr, readbuf);
2025}
2026#endif
2027
Patrick Scott5f6bd842010-06-28 16:55:16 -04002028#ifdef PNG_READ_pHYs_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08002029void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07002030png_handle_pHYs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002031{
2032 png_byte buf[9];
2033 png_uint_32 res_x, res_y;
2034 int unit_type;
2035
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002036 png_debug(1, "in png_handle_pHYs");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002037
2038 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Chris Craikb50c2172013-07-29 15:28:30 -07002039 png_chunk_error(png_ptr, "missing IHDR");
2040
The Android Open Source Project893912b2009-03-03 19:30:05 -08002041 else if (png_ptr->mode & PNG_HAVE_IDAT)
2042 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002043 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002044 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002045 return;
2046 }
Chris Craikb50c2172013-07-29 15:28:30 -07002047
The Android Open Source Project893912b2009-03-03 19:30:05 -08002048 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
2049 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002050 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002051 png_chunk_benign_error(png_ptr, "duplicate");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002052 return;
2053 }
2054
2055 if (length != 9)
2056 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002057 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002058 png_chunk_benign_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002059 return;
2060 }
2061
2062 png_crc_read(png_ptr, buf, 9);
Chris Craikb50c2172013-07-29 15:28:30 -07002063
The Android Open Source Project893912b2009-03-03 19:30:05 -08002064 if (png_crc_finish(png_ptr, 0))
2065 return;
2066
2067 res_x = png_get_uint_32(buf);
2068 res_y = png_get_uint_32(buf + 4);
2069 unit_type = buf[8];
2070 png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
2071}
2072#endif
2073
Patrick Scott5f6bd842010-06-28 16:55:16 -04002074#ifdef PNG_READ_oFFs_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08002075void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07002076png_handle_oFFs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002077{
2078 png_byte buf[9];
2079 png_int_32 offset_x, offset_y;
2080 int unit_type;
2081
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002082 png_debug(1, "in png_handle_oFFs");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002083
2084 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Chris Craikb50c2172013-07-29 15:28:30 -07002085 png_chunk_error(png_ptr, "missing IHDR");
2086
The Android Open Source Project893912b2009-03-03 19:30:05 -08002087 else if (png_ptr->mode & PNG_HAVE_IDAT)
2088 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002089 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002090 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002091 return;
2092 }
Chris Craikb50c2172013-07-29 15:28:30 -07002093
The Android Open Source Project893912b2009-03-03 19:30:05 -08002094 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs))
2095 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002096 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002097 png_chunk_benign_error(png_ptr, "duplicate");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002098 return;
2099 }
2100
2101 if (length != 9)
2102 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002103 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002104 png_chunk_benign_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002105 return;
2106 }
2107
2108 png_crc_read(png_ptr, buf, 9);
Chris Craikb50c2172013-07-29 15:28:30 -07002109
The Android Open Source Project893912b2009-03-03 19:30:05 -08002110 if (png_crc_finish(png_ptr, 0))
2111 return;
2112
2113 offset_x = png_get_int_32(buf);
2114 offset_y = png_get_int_32(buf + 4);
2115 unit_type = buf[8];
2116 png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
2117}
2118#endif
2119
Patrick Scott5f6bd842010-06-28 16:55:16 -04002120#ifdef PNG_READ_pCAL_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -04002121/* Read the pCAL chunk (described in the PNG Extensions document) */
The Android Open Source Project893912b2009-03-03 19:30:05 -08002122void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07002123png_handle_pCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002124{
The Android Open Source Project893912b2009-03-03 19:30:05 -08002125 png_int_32 X0, X1;
2126 png_byte type, nparams;
Chris Craikb50c2172013-07-29 15:28:30 -07002127 png_bytep buffer, buf, units, endptr;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002128 png_charpp params;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002129 int i;
2130
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002131 png_debug(1, "in png_handle_pCAL");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002132
2133 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Chris Craikb50c2172013-07-29 15:28:30 -07002134 png_chunk_error(png_ptr, "missing IHDR");
2135
The Android Open Source Project893912b2009-03-03 19:30:05 -08002136 else if (png_ptr->mode & PNG_HAVE_IDAT)
2137 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002138 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002139 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002140 return;
2141 }
Chris Craikb50c2172013-07-29 15:28:30 -07002142
The Android Open Source Project893912b2009-03-03 19:30:05 -08002143 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL))
2144 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002145 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002146 png_chunk_benign_error(png_ptr, "duplicate");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002147 return;
2148 }
2149
Chris Craikb50c2172013-07-29 15:28:30 -07002150 png_debug1(2, "Allocating and reading pCAL chunk data (%u bytes)",
2151 length + 1);
2152
2153 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
2154
2155 if (buffer == NULL)
2156 {
2157 png_crc_finish(png_ptr, length);
2158 png_chunk_benign_error(png_ptr, "out of memory");
2159 return;
2160 }
2161
2162 png_crc_read(png_ptr, buffer, length);
The Android Open Source Project893912b2009-03-03 19:30:05 -08002163
2164 if (png_crc_finish(png_ptr, 0))
The Android Open Source Project893912b2009-03-03 19:30:05 -08002165 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002166
Chris Craikb50c2172013-07-29 15:28:30 -07002167 buffer[length] = 0; /* Null terminate the last string */
The Android Open Source Project893912b2009-03-03 19:30:05 -08002168
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002169 png_debug(3, "Finding end of pCAL purpose string");
Chris Craikb50c2172013-07-29 15:28:30 -07002170 for (buf = buffer; *buf; buf++)
Patrick Scotta0bb96c2009-07-22 11:50:02 -04002171 /* Empty loop */ ;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002172
Chris Craikb50c2172013-07-29 15:28:30 -07002173 endptr = buffer + length;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002174
2175 /* We need to have at least 12 bytes after the purpose string
Chris Craikb50c2172013-07-29 15:28:30 -07002176 * in order to get the parameter information.
2177 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08002178 if (endptr <= buf + 12)
2179 {
Chris Craikb50c2172013-07-29 15:28:30 -07002180 png_chunk_benign_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002181 return;
2182 }
2183
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002184 png_debug(3, "Reading pCAL X0, X1, type, nparams, and units");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002185 X0 = png_get_int_32((png_bytep)buf+1);
2186 X1 = png_get_int_32((png_bytep)buf+5);
2187 type = buf[9];
2188 nparams = buf[10];
2189 units = buf + 11;
2190
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002191 png_debug(3, "Checking pCAL equation type and number of parameters");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002192 /* Check that we have the right number of parameters for known
Chris Craikb50c2172013-07-29 15:28:30 -07002193 * equation types.
2194 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08002195 if ((type == PNG_EQUATION_LINEAR && nparams != 2) ||
2196 (type == PNG_EQUATION_BASE_E && nparams != 3) ||
2197 (type == PNG_EQUATION_ARBITRARY && nparams != 3) ||
2198 (type == PNG_EQUATION_HYPERBOLIC && nparams != 4))
2199 {
Chris Craikb50c2172013-07-29 15:28:30 -07002200 png_chunk_benign_error(png_ptr, "invalid parameter count");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002201 return;
2202 }
Chris Craikb50c2172013-07-29 15:28:30 -07002203
The Android Open Source Project893912b2009-03-03 19:30:05 -08002204 else if (type >= PNG_EQUATION_LAST)
2205 {
Chris Craikb50c2172013-07-29 15:28:30 -07002206 png_chunk_benign_error(png_ptr, "unrecognized equation type");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002207 }
2208
2209 for (buf = units; *buf; buf++)
2210 /* Empty loop to move past the units string. */ ;
2211
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002212 png_debug(3, "Allocating pCAL parameters array");
Chris Craikb50c2172013-07-29 15:28:30 -07002213
2214 params = png_voidcast(png_charpp, png_malloc_warn(png_ptr,
2215 nparams * (sizeof (png_charp))));
2216
The Android Open Source Project893912b2009-03-03 19:30:05 -08002217 if (params == NULL)
Chris Craikb50c2172013-07-29 15:28:30 -07002218 {
2219 png_chunk_benign_error(png_ptr, "out of memory");
2220 return;
2221 }
The Android Open Source Project893912b2009-03-03 19:30:05 -08002222
2223 /* Get pointers to the start of each parameter string. */
Chris Craikb50c2172013-07-29 15:28:30 -07002224 for (i = 0; i < nparams; i++)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002225 {
2226 buf++; /* Skip the null string terminator from previous parameter. */
2227
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002228 png_debug1(3, "Reading pCAL parameter %d", i);
Chris Craikb50c2172013-07-29 15:28:30 -07002229
2230 for (params[i] = (png_charp)buf; buf <= endptr && *buf != 0; buf++)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002231 /* Empty loop to move past each parameter string */ ;
2232
2233 /* Make sure we haven't run out of data yet */
2234 if (buf > endptr)
2235 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002236 png_free(png_ptr, params);
Chris Craikb50c2172013-07-29 15:28:30 -07002237 png_chunk_benign_error(png_ptr, "invalid data");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002238 return;
2239 }
2240 }
2241
Chris Craikb50c2172013-07-29 15:28:30 -07002242 png_set_pCAL(png_ptr, info_ptr, (png_charp)buffer, X0, X1, type, nparams,
2243 (png_charp)units, params);
The Android Open Source Project893912b2009-03-03 19:30:05 -08002244
The Android Open Source Project893912b2009-03-03 19:30:05 -08002245 png_free(png_ptr, params);
2246}
2247#endif
2248
Patrick Scott5f6bd842010-06-28 16:55:16 -04002249#ifdef PNG_READ_sCAL_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -04002250/* Read the sCAL chunk */
The Android Open Source Project893912b2009-03-03 19:30:05 -08002251void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07002252png_handle_sCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002253{
Chris Craikb50c2172013-07-29 15:28:30 -07002254 png_bytep buffer;
2255 png_size_t i;
2256 int state;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002257
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002258 png_debug(1, "in png_handle_sCAL");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002259
2260 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Chris Craikb50c2172013-07-29 15:28:30 -07002261 png_chunk_error(png_ptr, "missing IHDR");
2262
The Android Open Source Project893912b2009-03-03 19:30:05 -08002263 else if (png_ptr->mode & PNG_HAVE_IDAT)
2264 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002265 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002266 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002267 return;
2268 }
Chris Craikb50c2172013-07-29 15:28:30 -07002269
The Android Open Source Project893912b2009-03-03 19:30:05 -08002270 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL))
2271 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002272 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002273 png_chunk_benign_error(png_ptr, "duplicate");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002274 return;
2275 }
2276
Eric Vannier66dce0d2011-07-20 17:03:29 -07002277 /* Need unit type, width, \0, height: minimum 4 bytes */
2278 else if (length < 4)
2279 {
Chris Craikb50c2172013-07-29 15:28:30 -07002280 png_crc_finish(png_ptr, length);
2281 png_chunk_benign_error(png_ptr, "invalid");
2282 return;
2283 }
2284
2285 png_debug1(2, "Allocating and reading sCAL chunk data (%u bytes)",
2286 length + 1);
2287
2288 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
2289
2290 if (buffer == NULL)
2291 {
2292 png_chunk_benign_error(png_ptr, "out of memory");
Eric Vannier66dce0d2011-07-20 17:03:29 -07002293 png_crc_finish(png_ptr, length);
2294 return;
2295 }
2296
Chris Craikb50c2172013-07-29 15:28:30 -07002297 png_crc_read(png_ptr, buffer, length);
2298 buffer[length] = 0; /* Null terminate the last string */
The Android Open Source Project893912b2009-03-03 19:30:05 -08002299
2300 if (png_crc_finish(png_ptr, 0))
Chris Craikb50c2172013-07-29 15:28:30 -07002301 return;
2302
2303 /* Validate the unit. */
2304 if (buffer[0] != 1 && buffer[0] != 2)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002305 {
Chris Craikb50c2172013-07-29 15:28:30 -07002306 png_chunk_benign_error(png_ptr, "invalid unit");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002307 return;
2308 }
2309
Chris Craikb50c2172013-07-29 15:28:30 -07002310 /* Validate the ASCII numbers, need two ASCII numbers separated by
2311 * a '\0' and they need to fit exactly in the chunk data.
2312 */
2313 i = 1;
2314 state = 0;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002315
Chris Craikb50c2172013-07-29 15:28:30 -07002316 if (!png_check_fp_number((png_const_charp)buffer, length, &state, &i) ||
2317 i >= length || buffer[i++] != 0)
2318 png_chunk_benign_error(png_ptr, "bad width format");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002319
Chris Craikb50c2172013-07-29 15:28:30 -07002320 else if (!PNG_FP_IS_POSITIVE(state))
2321 png_chunk_benign_error(png_ptr, "non-positive width");
2322
2323 else
The Android Open Source Project893912b2009-03-03 19:30:05 -08002324 {
Chris Craikb50c2172013-07-29 15:28:30 -07002325 png_size_t heighti = i;
2326
2327 state = 0;
2328 if (!png_check_fp_number((png_const_charp)buffer, length, &state, &i) ||
2329 i != length)
2330 png_chunk_benign_error(png_ptr, "bad height format");
2331
2332 else if (!PNG_FP_IS_POSITIVE(state))
2333 png_chunk_benign_error(png_ptr, "non-positive height");
2334
2335 else
2336 /* This is the (only) success case. */
2337 png_set_sCAL_s(png_ptr, info_ptr, buffer[0],
2338 (png_charp)buffer+1, (png_charp)buffer+heighti);
The Android Open Source Project893912b2009-03-03 19:30:05 -08002339 }
The Android Open Source Project893912b2009-03-03 19:30:05 -08002340}
2341#endif
2342
Patrick Scott5f6bd842010-06-28 16:55:16 -04002343#ifdef PNG_READ_tIME_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08002344void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07002345png_handle_tIME(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002346{
2347 png_byte buf[7];
2348 png_time mod_time;
2349
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002350 png_debug(1, "in png_handle_tIME");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002351
2352 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Chris Craikb50c2172013-07-29 15:28:30 -07002353 png_chunk_error(png_ptr, "missing IHDR");
2354
The Android Open Source Project893912b2009-03-03 19:30:05 -08002355 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME))
2356 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002357 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002358 png_chunk_benign_error(png_ptr, "duplicate");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002359 return;
2360 }
2361
2362 if (png_ptr->mode & PNG_HAVE_IDAT)
2363 png_ptr->mode |= PNG_AFTER_IDAT;
2364
2365 if (length != 7)
2366 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002367 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002368 png_chunk_benign_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002369 return;
2370 }
2371
2372 png_crc_read(png_ptr, buf, 7);
Chris Craikb50c2172013-07-29 15:28:30 -07002373
The Android Open Source Project893912b2009-03-03 19:30:05 -08002374 if (png_crc_finish(png_ptr, 0))
2375 return;
2376
2377 mod_time.second = buf[6];
2378 mod_time.minute = buf[5];
2379 mod_time.hour = buf[4];
2380 mod_time.day = buf[3];
2381 mod_time.month = buf[2];
2382 mod_time.year = png_get_uint_16(buf);
2383
2384 png_set_tIME(png_ptr, info_ptr, &mod_time);
2385}
2386#endif
2387
Patrick Scott5f6bd842010-06-28 16:55:16 -04002388#ifdef PNG_READ_tEXt_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08002389/* Note: this does not properly handle chunks that are > 64K under DOS */
2390void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07002391png_handle_tEXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002392{
Chris Craikb50c2172013-07-29 15:28:30 -07002393 png_text text_info;
2394 png_bytep buffer;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002395 png_charp key;
2396 png_charp text;
2397 png_uint_32 skip = 0;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002398
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002399 png_debug(1, "in png_handle_tEXt");
2400
Patrick Scott5f6bd842010-06-28 16:55:16 -04002401#ifdef PNG_USER_LIMITS_SUPPORTED
2402 if (png_ptr->user_chunk_cache_max != 0)
2403 {
2404 if (png_ptr->user_chunk_cache_max == 1)
2405 {
2406 png_crc_finish(png_ptr, length);
2407 return;
2408 }
Chris Craikb50c2172013-07-29 15:28:30 -07002409
Patrick Scott5f6bd842010-06-28 16:55:16 -04002410 if (--png_ptr->user_chunk_cache_max == 1)
2411 {
Patrick Scott5f6bd842010-06-28 16:55:16 -04002412 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002413 png_chunk_benign_error(png_ptr, "no space in chunk cache");
Patrick Scott5f6bd842010-06-28 16:55:16 -04002414 return;
2415 }
2416 }
2417#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -08002418
2419 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Chris Craikb50c2172013-07-29 15:28:30 -07002420 png_chunk_error(png_ptr, "missing IHDR");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002421
2422 if (png_ptr->mode & PNG_HAVE_IDAT)
2423 png_ptr->mode |= PNG_AFTER_IDAT;
2424
2425#ifdef PNG_MAX_MALLOC_64K
Chris Craikb50c2172013-07-29 15:28:30 -07002426 if (length > 65535U)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002427 {
Chris Craikb50c2172013-07-29 15:28:30 -07002428 png_crc_finish(png_ptr, length);
2429 png_chunk_benign_error(png_ptr, "too large to fit in memory");
2430 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002431 }
2432#endif
2433
Chris Craikb50c2172013-07-29 15:28:30 -07002434 buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002435
Chris Craikb50c2172013-07-29 15:28:30 -07002436 if (buffer == NULL)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002437 {
Chris Craikb50c2172013-07-29 15:28:30 -07002438 png_chunk_benign_error(png_ptr, "out of memory");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002439 return;
2440 }
Chris Craikb50c2172013-07-29 15:28:30 -07002441
2442 png_crc_read(png_ptr, buffer, length);
The Android Open Source Project893912b2009-03-03 19:30:05 -08002443
2444 if (png_crc_finish(png_ptr, skip))
The Android Open Source Project893912b2009-03-03 19:30:05 -08002445 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002446
Chris Craikb50c2172013-07-29 15:28:30 -07002447 key = (png_charp)buffer;
2448 key[length] = 0;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002449
2450 for (text = key; *text; text++)
Patrick Scotta0bb96c2009-07-22 11:50:02 -04002451 /* Empty loop to find end of key */ ;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002452
Chris Craikb50c2172013-07-29 15:28:30 -07002453 if (text != key + length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002454 text++;
2455
Chris Craikb50c2172013-07-29 15:28:30 -07002456 text_info.compression = PNG_TEXT_COMPRESSION_NONE;
2457 text_info.key = key;
2458 text_info.lang = NULL;
2459 text_info.lang_key = NULL;
2460 text_info.itxt_length = 0;
2461 text_info.text = text;
2462 text_info.text_length = strlen(text);
The Android Open Source Project893912b2009-03-03 19:30:05 -08002463
Chris Craikb50c2172013-07-29 15:28:30 -07002464 if (png_set_text_2(png_ptr, info_ptr, &text_info, 1))
2465 png_warning(png_ptr, "Insufficient memory to process text chunk");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002466}
2467#endif
2468
Patrick Scott5f6bd842010-06-28 16:55:16 -04002469#ifdef PNG_READ_zTXt_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -04002470/* Note: this does not correctly handle chunks that are > 64K under DOS */
The Android Open Source Project893912b2009-03-03 19:30:05 -08002471void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07002472png_handle_zTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002473{
Chris Craikb50c2172013-07-29 15:28:30 -07002474 png_const_charp errmsg = NULL;
2475 png_bytep buffer;
2476 png_uint_32 keyword_length;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002477
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002478 png_debug(1, "in png_handle_zTXt");
2479
Patrick Scott5f6bd842010-06-28 16:55:16 -04002480#ifdef PNG_USER_LIMITS_SUPPORTED
2481 if (png_ptr->user_chunk_cache_max != 0)
2482 {
2483 if (png_ptr->user_chunk_cache_max == 1)
2484 {
2485 png_crc_finish(png_ptr, length);
2486 return;
2487 }
Chris Craikb50c2172013-07-29 15:28:30 -07002488
Patrick Scott5f6bd842010-06-28 16:55:16 -04002489 if (--png_ptr->user_chunk_cache_max == 1)
2490 {
Patrick Scott5f6bd842010-06-28 16:55:16 -04002491 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002492 png_chunk_benign_error(png_ptr, "no space in chunk cache");
Patrick Scott5f6bd842010-06-28 16:55:16 -04002493 return;
2494 }
2495 }
2496#endif
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002497
The Android Open Source Project893912b2009-03-03 19:30:05 -08002498 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Chris Craikb50c2172013-07-29 15:28:30 -07002499 png_chunk_error(png_ptr, "missing IHDR");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002500
2501 if (png_ptr->mode & PNG_HAVE_IDAT)
2502 png_ptr->mode |= PNG_AFTER_IDAT;
2503
Chris Craikb50c2172013-07-29 15:28:30 -07002504 buffer = png_read_buffer(png_ptr, length, 2/*silent*/);
The Android Open Source Project893912b2009-03-03 19:30:05 -08002505
Chris Craikb50c2172013-07-29 15:28:30 -07002506 if (buffer == NULL)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002507 {
Chris Craikb50c2172013-07-29 15:28:30 -07002508 png_crc_finish(png_ptr, length);
2509 png_chunk_benign_error(png_ptr, "out of memory");
2510 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002511 }
Chris Craikb50c2172013-07-29 15:28:30 -07002512
2513 png_crc_read(png_ptr, buffer, length);
2514
The Android Open Source Project893912b2009-03-03 19:30:05 -08002515 if (png_crc_finish(png_ptr, 0))
The Android Open Source Project893912b2009-03-03 19:30:05 -08002516 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002517
Chris Craikb50c2172013-07-29 15:28:30 -07002518 /* TODO: also check that the keyword contents match the spec! */
2519 for (keyword_length = 0;
2520 keyword_length < length && buffer[keyword_length] != 0;
2521 ++keyword_length)
2522 /* Empty loop to find end of name */ ;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002523
Chris Craikb50c2172013-07-29 15:28:30 -07002524 if (keyword_length > 79 || keyword_length < 1)
2525 errmsg = "bad keyword";
The Android Open Source Project893912b2009-03-03 19:30:05 -08002526
Chris Craikb50c2172013-07-29 15:28:30 -07002527 /* zTXt must have some LZ data after the keyword, although it may expand to
2528 * zero bytes; we need a '\0' at the end of the keyword, the compression type
2529 * then the LZ data:
2530 */
2531 else if (keyword_length + 3 > length)
2532 errmsg = "truncated";
2533
2534 else if (buffer[keyword_length+1] != PNG_COMPRESSION_TYPE_BASE)
2535 errmsg = "unknown compression type";
2536
The Android Open Source Project893912b2009-03-03 19:30:05 -08002537 else
2538 {
Chris Craikb50c2172013-07-29 15:28:30 -07002539 png_alloc_size_t uncompressed_length = PNG_SIZE_MAX;
2540
2541 /* TODO: at present png_decompress_chunk imposes a single application
2542 * level memory limit, this should be split to different values for iCCP
2543 * and text chunks.
2544 */
2545 if (png_decompress_chunk(png_ptr, length, keyword_length+2,
2546 &uncompressed_length, 1/*terminate*/) == Z_STREAM_END)
2547 {
2548 png_text text;
2549
2550 /* It worked; png_ptr->read_buffer now looks like a tEXt chunk except
2551 * for the extra compression type byte and the fact that it isn't
2552 * necessarily '\0' terminated.
2553 */
2554 buffer = png_ptr->read_buffer;
2555 buffer[uncompressed_length+(keyword_length+2)] = 0;
2556
2557 text.compression = PNG_TEXT_COMPRESSION_zTXt;
2558 text.key = (png_charp)buffer;
2559 text.text = (png_charp)(buffer + keyword_length+2);
2560 text.text_length = uncompressed_length;
2561 text.itxt_length = 0;
2562 text.lang = NULL;
2563 text.lang_key = NULL;
2564
2565 if (png_set_text_2(png_ptr, info_ptr, &text, 1))
2566 errmsg = "insufficient memory";
2567 }
2568
2569 else
2570 errmsg = png_ptr->zstream.msg;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002571 }
The Android Open Source Project893912b2009-03-03 19:30:05 -08002572
Chris Craikb50c2172013-07-29 15:28:30 -07002573 if (errmsg != NULL)
2574 png_chunk_benign_error(png_ptr, errmsg);
The Android Open Source Project893912b2009-03-03 19:30:05 -08002575}
2576#endif
2577
Patrick Scott5f6bd842010-06-28 16:55:16 -04002578#ifdef PNG_READ_iTXt_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -04002579/* Note: this does not correctly handle chunks that are > 64K under DOS */
The Android Open Source Project893912b2009-03-03 19:30:05 -08002580void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07002581png_handle_iTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002582{
Chris Craikb50c2172013-07-29 15:28:30 -07002583 png_const_charp errmsg = NULL;
2584 png_bytep buffer;
2585 png_uint_32 prefix_length;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002586
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002587 png_debug(1, "in png_handle_iTXt");
2588
Patrick Scott5f6bd842010-06-28 16:55:16 -04002589#ifdef PNG_USER_LIMITS_SUPPORTED
2590 if (png_ptr->user_chunk_cache_max != 0)
2591 {
2592 if (png_ptr->user_chunk_cache_max == 1)
2593 {
2594 png_crc_finish(png_ptr, length);
2595 return;
2596 }
Chris Craikb50c2172013-07-29 15:28:30 -07002597
Patrick Scott5f6bd842010-06-28 16:55:16 -04002598 if (--png_ptr->user_chunk_cache_max == 1)
2599 {
Patrick Scott5f6bd842010-06-28 16:55:16 -04002600 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002601 png_chunk_benign_error(png_ptr, "no space in chunk cache");
Patrick Scott5f6bd842010-06-28 16:55:16 -04002602 return;
2603 }
2604 }
2605#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -08002606
2607 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Chris Craikb50c2172013-07-29 15:28:30 -07002608 png_chunk_error(png_ptr, "missing IHDR");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002609
2610 if (png_ptr->mode & PNG_HAVE_IDAT)
2611 png_ptr->mode |= PNG_AFTER_IDAT;
2612
Chris Craikb50c2172013-07-29 15:28:30 -07002613 buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
The Android Open Source Project893912b2009-03-03 19:30:05 -08002614
Chris Craikb50c2172013-07-29 15:28:30 -07002615 if (buffer == NULL)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002616 {
Chris Craikb50c2172013-07-29 15:28:30 -07002617 png_crc_finish(png_ptr, length);
2618 png_chunk_benign_error(png_ptr, "out of memory");
2619 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002620 }
Chris Craikb50c2172013-07-29 15:28:30 -07002621
2622 png_crc_read(png_ptr, buffer, length);
2623
The Android Open Source Project893912b2009-03-03 19:30:05 -08002624 if (png_crc_finish(png_ptr, 0))
The Android Open Source Project893912b2009-03-03 19:30:05 -08002625 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002626
Chris Craikb50c2172013-07-29 15:28:30 -07002627 /* First the keyword. */
2628 for (prefix_length=0;
2629 prefix_length < length && buffer[prefix_length] != 0;
2630 ++prefix_length)
Patrick Scotta0bb96c2009-07-22 11:50:02 -04002631 /* Empty loop */ ;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002632
Chris Craikb50c2172013-07-29 15:28:30 -07002633 /* Perform a basic check on the keyword length here. */
2634 if (prefix_length > 79 || prefix_length < 1)
2635 errmsg = "bad keyword";
2636
2637 /* Expect keyword, compression flag, compression type, language, translated
2638 * keyword (both may be empty but are 0 terminated) then the text, which may
2639 * be empty.
Patrick Scotta0bb96c2009-07-22 11:50:02 -04002640 */
Chris Craikb50c2172013-07-29 15:28:30 -07002641 else if (prefix_length + 5 > length)
2642 errmsg = "truncated";
The Android Open Source Project893912b2009-03-03 19:30:05 -08002643
Chris Craikb50c2172013-07-29 15:28:30 -07002644 else if (buffer[prefix_length+1] == 0 ||
2645 (buffer[prefix_length+1] == 1 &&
2646 buffer[prefix_length+2] == PNG_COMPRESSION_TYPE_BASE))
The Android Open Source Project893912b2009-03-03 19:30:05 -08002647 {
Chris Craikb50c2172013-07-29 15:28:30 -07002648 int compressed = buffer[prefix_length+1] != 0;
2649 png_uint_32 language_offset, translated_keyword_offset;
2650 png_alloc_size_t uncompressed_length = 0;
2651
2652 /* Now the language tag */
2653 prefix_length += 3;
2654 language_offset = prefix_length;
2655
2656 for (; prefix_length < length && buffer[prefix_length] != 0;
2657 ++prefix_length)
2658 /* Empty loop */ ;
2659
2660 /* WARNING: the length may be invalid here, this is checked below. */
2661 translated_keyword_offset = ++prefix_length;
2662
2663 for (; prefix_length < length && buffer[prefix_length] != 0;
2664 ++prefix_length)
2665 /* Empty loop */ ;
2666
2667 /* prefix_length should now be at the trailing '\0' of the translated
2668 * keyword, but it may already be over the end. None of this arithmetic
2669 * can overflow because chunks are at most 2^31 bytes long, but on 16-bit
2670 * systems the available allocaton may overflow.
2671 */
2672 ++prefix_length;
2673
2674 if (!compressed && prefix_length <= length)
2675 uncompressed_length = length - prefix_length;
2676
2677 else if (compressed && prefix_length < length)
2678 {
2679 uncompressed_length = PNG_SIZE_MAX;
2680
2681 /* TODO: at present png_decompress_chunk imposes a single application
2682 * level memory limit, this should be split to different values for
2683 * iCCP and text chunks.
2684 */
2685 if (png_decompress_chunk(png_ptr, length, prefix_length,
2686 &uncompressed_length, 1/*terminate*/) == Z_STREAM_END)
2687 buffer = png_ptr->read_buffer;
2688
2689 else
2690 errmsg = png_ptr->zstream.msg;
2691 }
2692
2693 else
2694 errmsg = "truncated";
2695
2696 if (errmsg == NULL)
2697 {
2698 png_text text;
2699
2700 buffer[uncompressed_length+prefix_length] = 0;
2701
2702 if (compressed)
2703 text.compression = PNG_ITXT_COMPRESSION_NONE;
2704
2705 else
2706 text.compression = PNG_ITXT_COMPRESSION_zTXt;
2707
2708 text.key = (png_charp)buffer;
2709 text.lang = (png_charp)buffer + language_offset;
2710 text.lang_key = (png_charp)buffer + translated_keyword_offset;
2711 text.text = (png_charp)buffer + prefix_length;
2712 text.text_length = 0;
2713 text.itxt_length = uncompressed_length;
2714
2715 if (png_set_text_2(png_ptr, info_ptr, &text, 1))
2716 errmsg = "insufficient memory";
2717 }
The Android Open Source Project893912b2009-03-03 19:30:05 -08002718 }
Chris Craikb50c2172013-07-29 15:28:30 -07002719
The Android Open Source Project893912b2009-03-03 19:30:05 -08002720 else
Chris Craikb50c2172013-07-29 15:28:30 -07002721 errmsg = "bad compression info";
The Android Open Source Project893912b2009-03-03 19:30:05 -08002722
Chris Craikb50c2172013-07-29 15:28:30 -07002723 if (errmsg != NULL)
2724 png_chunk_benign_error(png_ptr, errmsg);
The Android Open Source Project893912b2009-03-03 19:30:05 -08002725}
2726#endif
2727
Chris Craikb50c2172013-07-29 15:28:30 -07002728#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
2729/* Utility function for png_handle_unknown; set up png_ptr::unknown_chunk */
2730static int
2731png_cache_unknown_chunk(png_structrp png_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002732{
Chris Craikb50c2172013-07-29 15:28:30 -07002733 png_alloc_size_t limit = PNG_SIZE_MAX;
2734
2735 if (png_ptr->unknown_chunk.data != NULL)
2736 {
2737 png_free(png_ptr, png_ptr->unknown_chunk.data);
2738 png_ptr->unknown_chunk.data = NULL;
2739 }
2740
2741# ifdef PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED
2742 if (png_ptr->user_chunk_malloc_max > 0 &&
2743 png_ptr->user_chunk_malloc_max < limit)
2744 limit = png_ptr->user_chunk_malloc_max;
2745
2746# elif PNG_USER_CHUNK_MALLOC_MAX > 0
2747 if (PNG_USER_CHUNK_MALLOC_MAX < limit)
2748 limit = PNG_USER_CHUNK_MALLOC_MAX;
2749# endif
2750
2751 if (length <= limit)
2752 {
2753 PNG_CSTRING_FROM_CHUNK(png_ptr->unknown_chunk.name, png_ptr->chunk_name);
2754 /* The following is safe because of the PNG_SIZE_MAX init above */
2755 png_ptr->unknown_chunk.size = (png_size_t)length/*SAFE*/;
2756 /* 'mode' is a flag array, only the bottom four bits matter here */
2757 png_ptr->unknown_chunk.location = (png_byte)png_ptr->mode/*SAFE*/;
2758
2759 if (length == 0)
2760 png_ptr->unknown_chunk.data = NULL;
2761
2762 else
2763 {
2764 /* Do a 'warn' here - it is handled below. */
2765 png_ptr->unknown_chunk.data = png_voidcast(png_bytep,
2766 png_malloc_warn(png_ptr, length));
2767 }
2768 }
2769
2770 if (png_ptr->unknown_chunk.data == NULL && length > 0)
2771 {
2772 /* This is benign because we clean up correctly */
2773 png_crc_finish(png_ptr, length);
2774 png_chunk_benign_error(png_ptr, "unknown chunk exceeds memory limits");
2775 return 0;
2776 }
2777
2778 else
2779 {
2780 if (length > 0)
2781 png_crc_read(png_ptr, png_ptr->unknown_chunk.data, length);
2782 png_crc_finish(png_ptr, 0);
2783 return 1;
2784 }
2785}
2786#endif /* PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */
2787
2788/* Handle an unknown, or known but disabled, chunk */
2789void /* PRIVATE */
2790png_handle_unknown(png_structrp png_ptr, png_inforp info_ptr,
2791 png_uint_32 length, int keep)
2792{
2793 int handled = 0; /* the chunk was handled */
The Android Open Source Project893912b2009-03-03 19:30:05 -08002794
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002795 png_debug(1, "in png_handle_unknown");
2796
Patrick Scott5f6bd842010-06-28 16:55:16 -04002797#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07002798 /* NOTE: this code is based on the code in libpng-1.4.12 except for fixing
2799 * the bug which meant that setting a non-default behavior for a specific
2800 * chunk would be ignored (the default was always used unless a user
2801 * callback was installed).
2802 *
2803 * 'keep' is the value from the png_chunk_unknown_handling, the setting for
2804 * this specific chunk_name, if PNG_HANDLE_AS_UNKNOWN_SUPPORTED, if not it
2805 * will always be PNG_HANDLE_CHUNK_AS_DEFAULT and it needs to be set here.
2806 * This is just an optimization to avoid multiple calls to the lookup
2807 * function.
2808 */
2809# ifndef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
2810# ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
2811 keep = png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name);
2812# endif
2813# endif
The Android Open Source Project893912b2009-03-03 19:30:05 -08002814
Chris Craikb50c2172013-07-29 15:28:30 -07002815 /* One of the following methods will read the chunk or skip it (at least one
2816 * of these is always defined because this is the only way to switch on
2817 * PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
2818 */
2819# ifdef PNG_READ_USER_CHUNKS_SUPPORTED
2820 /* The user callback takes precedence over the chunk keep value, but the
2821 * keep value is still required to validate a save of a critical chunk.
2822 */
2823 if (png_ptr->read_user_chunk_fn != NULL)
2824 {
2825 if (png_cache_unknown_chunk(png_ptr, length))
2826 {
2827 /* Callback to user unknown chunk handler */
2828 int ret = (*(png_ptr->read_user_chunk_fn))(png_ptr,
2829 &png_ptr->unknown_chunk);
The Android Open Source Project893912b2009-03-03 19:30:05 -08002830
Chris Craikb50c2172013-07-29 15:28:30 -07002831 /* ret is:
2832 * negative: An error occured, png_chunk_error will be called.
2833 * zero: The chunk was not handled, the chunk will be discarded
2834 * unless png_set_keep_unknown_chunks has been used to set
2835 * a 'keep' behavior for this particular chunk, in which
2836 * case that will be used. A critical chunk will cause an
2837 * error at this point unless it is to be saved.
2838 * positive: The chunk was handled, libpng will ignore/discard it.
2839 */
2840 if (ret < 0)
2841 png_chunk_error(png_ptr, "error in user chunk");
2842
2843 else if (ret == 0)
2844 {
2845 /* If the keep value is 'default' or 'never' override it, but
2846 * still error out on critical chunks unless the keep value is
2847 * 'always' While this is weird it is the behavior in 1.4.12.
2848 * A possible improvement would be to obey the value set for the
2849 * chunk, but this would be an API change that would probably
2850 * damage some applications.
2851 *
2852 * The png_app_warning below catches the case that matters, where
2853 * the application has not set specific save or ignore for this
2854 * chunk or global save or ignore.
2855 */
2856 if (keep < PNG_HANDLE_CHUNK_IF_SAFE)
2857 {
2858# ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
2859 if (png_ptr->unknown_default < PNG_HANDLE_CHUNK_IF_SAFE)
2860 {
2861 png_chunk_warning(png_ptr, "Saving unknown chunk:");
2862 png_app_warning(png_ptr,
2863 "forcing save of an unhandled chunk;"
2864 " please call png_set_keep_unknown_chunks");
2865 /* with keep = PNG_HANDLE_CHUNK_IF_SAFE */
2866 }
2867# endif
2868 keep = PNG_HANDLE_CHUNK_IF_SAFE;
2869 }
2870 }
2871
2872 else /* chunk was handled */
2873 {
2874 handled = 1;
2875 /* Critical chunks can be safely discarded at this point. */
2876 keep = PNG_HANDLE_CHUNK_NEVER;
2877 }
2878 }
2879
2880 else
2881 keep = PNG_HANDLE_CHUNK_NEVER; /* insufficient memory */
2882 }
2883
2884 else
2885 /* Use the SAVE_UNKNOWN_CHUNKS code or skip the chunk */
2886# endif /* PNG_READ_USER_CHUNKS_SUPPORTED */
2887
2888# ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
2889 {
2890 /* keep is currently just the per-chunk setting, if there was no
2891 * setting change it to the global default now (not that this may
2892 * still be AS_DEFAULT) then obtain the cache of the chunk if required,
2893 * if not simply skip the chunk.
2894 */
2895 if (keep == PNG_HANDLE_CHUNK_AS_DEFAULT)
2896 keep = png_ptr->unknown_default;
2897
2898 if (keep == PNG_HANDLE_CHUNK_ALWAYS ||
2899 (keep == PNG_HANDLE_CHUNK_IF_SAFE &&
2900 PNG_CHUNK_ANCILLARY(png_ptr->chunk_name)))
2901 {
2902 if (!png_cache_unknown_chunk(png_ptr, length))
2903 keep = PNG_HANDLE_CHUNK_NEVER;
2904 }
2905
2906 else
2907 png_crc_finish(png_ptr, length);
2908 }
2909# else
2910# ifndef PNG_READ_USER_CHUNKS_SUPPORTED
2911# error no method to support READ_UNKNOWN_CHUNKS
2912# endif
2913
2914 {
2915 /* If here there is no read callback pointer set and no support is
2916 * compiled in to just save the unknown chunks, so simply skip this
2917 * chunk. If 'keep' is something other than AS_DEFAULT or NEVER then
2918 * the app has erroneously asked for unknown chunk saving when there
2919 * is no support.
2920 */
2921 if (keep > PNG_HANDLE_CHUNK_NEVER)
2922 png_app_error(png_ptr, "no unknown chunk support available");
2923
2924 png_crc_finish(png_ptr, length);
2925 }
2926# endif
2927
2928# ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
2929 /* Now store the chunk in the chunk list if appropriate, and if the limits
2930 * permit it.
2931 */
2932 if (keep == PNG_HANDLE_CHUNK_ALWAYS ||
2933 (keep == PNG_HANDLE_CHUNK_IF_SAFE &&
2934 PNG_CHUNK_ANCILLARY(png_ptr->chunk_name)))
2935 {
2936# ifdef PNG_USER_LIMITS_SUPPORTED
2937 switch (png_ptr->user_chunk_cache_max)
2938 {
2939 case 2:
2940 png_ptr->user_chunk_cache_max = 1;
2941 png_chunk_benign_error(png_ptr, "no space in chunk cache");
2942 /* FALL THROUGH */
2943 case 1:
2944 /* NOTE: prior to 1.6.0 this case resulted in an unknown critical
2945 * chunk being skipped, now there will be a hard error below.
2946 */
2947 break;
2948
2949 default: /* not at limit */
2950 --(png_ptr->user_chunk_cache_max);
2951 /* FALL THROUGH */
2952 case 0: /* no limit */
2953# endif /* PNG_USER_LIMITS_SUPPORTED */
2954 /* Here when the limit isn't reached or when limits are compiled
2955 * out; store the chunk.
2956 */
2957 png_set_unknown_chunks(png_ptr, info_ptr,
2958 &png_ptr->unknown_chunk, 1);
2959 handled = 1;
2960# ifdef PNG_USER_LIMITS_SUPPORTED
2961 break;
2962 }
2963# endif
2964 }
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302965# else /* no store support: the chunk must be handled by the user callback */
Chris Craikb50c2172013-07-29 15:28:30 -07002966 PNG_UNUSED(info_ptr)
Chris Craikb50c2172013-07-29 15:28:30 -07002967# endif
2968
2969 /* Regardless of the error handling below the cached data (if any) can be
2970 * freed now. Notice that the data is not freed if there is a png_error, but
2971 * it will be freed by destroy_read_struct.
2972 */
2973 if (png_ptr->unknown_chunk.data != NULL)
2974 png_free(png_ptr, png_ptr->unknown_chunk.data);
2975 png_ptr->unknown_chunk.data = NULL;
2976
2977#else /* !PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */
2978 /* There is no support to read an unknown chunk, so just skip it. */
2979 png_crc_finish(png_ptr, length);
2980 PNG_UNUSED(info_ptr)
2981 PNG_UNUSED(keep)
2982#endif /* !PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */
2983
2984 /* Check for unhandled critical chunks */
2985 if (!handled && PNG_CHUNK_CRITICAL(png_ptr->chunk_name))
2986 png_chunk_error(png_ptr, "unhandled critical chunk");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002987}
2988
2989/* This function is called to verify that a chunk name is valid.
Chris Craikb50c2172013-07-29 15:28:30 -07002990 * This function can't have the "critical chunk check" incorporated
2991 * into it, since in the future we will need to be able to call user
2992 * functions to handle unknown critical chunks after we check that
2993 * the chunk name itself is valid.
2994 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08002995
Chris Craikb50c2172013-07-29 15:28:30 -07002996/* Bit hacking: the test for an invalid byte in the 4 byte chunk name is:
2997 *
2998 * ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
2999 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08003000
3001void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07003002png_check_chunk_name(png_structrp png_ptr, png_uint_32 chunk_name)
The Android Open Source Project893912b2009-03-03 19:30:05 -08003003{
Chris Craikb50c2172013-07-29 15:28:30 -07003004 int i;
3005
The Android Open Source Project4215dd12009-03-09 11:52:12 -07003006 png_debug(1, "in png_check_chunk_name");
Chris Craikb50c2172013-07-29 15:28:30 -07003007
3008 for (i=1; i<=4; ++i)
The Android Open Source Project893912b2009-03-03 19:30:05 -08003009 {
Chris Craikb50c2172013-07-29 15:28:30 -07003010 int c = chunk_name & 0xff;
3011
3012 if (c < 65 || c > 122 || (c > 90 && c < 97))
3013 png_chunk_error(png_ptr, "invalid chunk type");
3014
3015 chunk_name >>= 8;
The Android Open Source Project893912b2009-03-03 19:30:05 -08003016 }
3017}
3018
Chris Craikb50c2172013-07-29 15:28:30 -07003019/* Combines the row recently read in with the existing pixels in the row. This
3020 * routine takes care of alpha and transparency if requested. This routine also
3021 * handles the two methods of progressive display of interlaced images,
3022 * depending on the 'display' value; if 'display' is true then the whole row
3023 * (dp) is filled from the start by replicating the available pixels. If
3024 * 'display' is false only those pixels present in the pass are filled in.
3025 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08003026void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07003027png_combine_row(png_const_structrp png_ptr, png_bytep dp, int display)
The Android Open Source Project893912b2009-03-03 19:30:05 -08003028{
Chris Craikb50c2172013-07-29 15:28:30 -07003029 unsigned int pixel_depth = png_ptr->transformed_pixel_depth;
3030 png_const_bytep sp = png_ptr->row_buf + 1;
Henrik Smiding82aab852015-02-20 13:01:11 +01003031 png_alloc_size_t row_width = png_ptr->width;
Chris Craikb50c2172013-07-29 15:28:30 -07003032 unsigned int pass = png_ptr->pass;
3033 png_bytep end_ptr = 0;
3034 png_byte end_byte = 0;
3035 unsigned int end_mask;
3036
The Android Open Source Project4215dd12009-03-09 11:52:12 -07003037 png_debug(1, "in png_combine_row");
Chris Craikb50c2172013-07-29 15:28:30 -07003038
3039 /* Added in 1.5.6: it should not be possible to enter this routine until at
3040 * least one row has been read from the PNG data and transformed.
3041 */
3042 if (pixel_depth == 0)
3043 png_error(png_ptr, "internal row logic error");
3044
3045 /* Added in 1.5.4: the pixel depth should match the information returned by
3046 * any call to png_read_update_info at this point. Do not continue if we got
3047 * this wrong.
3048 */
3049 if (png_ptr->info_rowbytes != 0 && png_ptr->info_rowbytes !=
3050 PNG_ROWBYTES(pixel_depth, row_width))
3051 png_error(png_ptr, "internal row size calculation error");
3052
3053 /* Don't expect this to ever happen: */
3054 if (row_width == 0)
3055 png_error(png_ptr, "internal row width error");
3056
3057 /* Preserve the last byte in cases where only part of it will be overwritten,
3058 * the multiply below may overflow, we don't care because ANSI-C guarantees
3059 * we get the low bits.
3060 */
3061 end_mask = (pixel_depth * row_width) & 7;
3062 if (end_mask != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08003063 {
Chris Craikb50c2172013-07-29 15:28:30 -07003064 /* end_ptr == NULL is a flag to say do nothing */
3065 end_ptr = dp + PNG_ROWBYTES(pixel_depth, row_width) - 1;
3066 end_byte = *end_ptr;
3067# ifdef PNG_READ_PACKSWAP_SUPPORTED
3068 if (png_ptr->transformations & PNG_PACKSWAP) /* little-endian byte */
3069 end_mask = 0xff << end_mask;
3070
3071 else /* big-endian byte */
3072# endif
3073 end_mask = 0xff >> end_mask;
3074 /* end_mask is now the bits to *keep* from the destination row */
The Android Open Source Project893912b2009-03-03 19:30:05 -08003075 }
Chris Craikb50c2172013-07-29 15:28:30 -07003076
3077 /* For non-interlaced images this reduces to a memcpy(). A memcpy()
3078 * will also happen if interlacing isn't supported or if the application
3079 * does not call png_set_interlace_handling(). In the latter cases the
3080 * caller just gets a sequence of the unexpanded rows from each interlace
3081 * pass.
3082 */
3083#ifdef PNG_READ_INTERLACING_SUPPORTED
3084 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE) &&
3085 pass < 6 && (display == 0 ||
3086 /* The following copies everything for 'display' on passes 0, 2 and 4. */
3087 (display == 1 && (pass & 1) != 0)))
The Android Open Source Project893912b2009-03-03 19:30:05 -08003088 {
Chris Craikb50c2172013-07-29 15:28:30 -07003089 /* Narrow images may have no bits in a pass; the caller should handle
3090 * this, but this test is cheap:
3091 */
3092 if (row_width <= PNG_PASS_START_COL(pass))
3093 return;
3094
3095 if (pixel_depth < 8)
The Android Open Source Project893912b2009-03-03 19:30:05 -08003096 {
Chris Craikb50c2172013-07-29 15:28:30 -07003097 /* For pixel depths up to 4 bpp the 8-pixel mask can be expanded to fit
3098 * into 32 bits, then a single loop over the bytes using the four byte
3099 * values in the 32-bit mask can be used. For the 'display' option the
3100 * expanded mask may also not require any masking within a byte. To
3101 * make this work the PACKSWAP option must be taken into account - it
3102 * simply requires the pixels to be reversed in each byte.
3103 *
3104 * The 'regular' case requires a mask for each of the first 6 passes,
3105 * the 'display' case does a copy for the even passes in the range
3106 * 0..6. This has already been handled in the test above.
3107 *
3108 * The masks are arranged as four bytes with the first byte to use in
3109 * the lowest bits (little-endian) regardless of the order (PACKSWAP or
3110 * not) of the pixels in each byte.
3111 *
3112 * NOTE: the whole of this logic depends on the caller of this function
3113 * only calling it on rows appropriate to the pass. This function only
3114 * understands the 'x' logic; the 'y' logic is handled by the caller.
3115 *
3116 * The following defines allow generation of compile time constant bit
3117 * masks for each pixel depth and each possibility of swapped or not
3118 * swapped bytes. Pass 'p' is in the range 0..6; 'x', a pixel index,
3119 * is in the range 0..7; and the result is 1 if the pixel is to be
3120 * copied in the pass, 0 if not. 'S' is for the sparkle method, 'B'
3121 * for the block method.
3122 *
3123 * With some compilers a compile time expression of the general form:
3124 *
3125 * (shift >= 32) ? (a >> (shift-32)) : (b >> shift)
3126 *
3127 * Produces warnings with values of 'shift' in the range 33 to 63
3128 * because the right hand side of the ?: expression is evaluated by
3129 * the compiler even though it isn't used. Microsoft Visual C (various
3130 * versions) and the Intel C compiler are known to do this. To avoid
3131 * this the following macros are used in 1.5.6. This is a temporary
3132 * solution to avoid destabilizing the code during the release process.
3133 */
3134# if PNG_USE_COMPILE_TIME_MASKS
3135# define PNG_LSR(x,s) ((x)>>((s) & 0x1f))
3136# define PNG_LSL(x,s) ((x)<<((s) & 0x1f))
3137# else
3138# define PNG_LSR(x,s) ((x)>>(s))
3139# define PNG_LSL(x,s) ((x)<<(s))
3140# endif
3141# define S_COPY(p,x) (((p)<4 ? PNG_LSR(0x80088822,(3-(p))*8+(7-(x))) :\
3142 PNG_LSR(0xaa55ff00,(7-(p))*8+(7-(x)))) & 1)
3143# define B_COPY(p,x) (((p)<4 ? PNG_LSR(0xff0fff33,(3-(p))*8+(7-(x))) :\
3144 PNG_LSR(0xff55ff00,(7-(p))*8+(7-(x)))) & 1)
The Android Open Source Project893912b2009-03-03 19:30:05 -08003145
Chris Craikb50c2172013-07-29 15:28:30 -07003146 /* Return a mask for pass 'p' pixel 'x' at depth 'd'. The mask is
3147 * little endian - the first pixel is at bit 0 - however the extra
3148 * parameter 's' can be set to cause the mask position to be swapped
3149 * within each byte, to match the PNG format. This is done by XOR of
3150 * the shift with 7, 6 or 4 for bit depths 1, 2 and 4.
3151 */
3152# define PIXEL_MASK(p,x,d,s) \
3153 (PNG_LSL(((PNG_LSL(1U,(d)))-1),(((x)*(d))^((s)?8-(d):0))))
3154
3155 /* Hence generate the appropriate 'block' or 'sparkle' pixel copy mask.
3156 */
3157# define S_MASKx(p,x,d,s) (S_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
3158# define B_MASKx(p,x,d,s) (B_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
3159
3160 /* Combine 8 of these to get the full mask. For the 1-bpp and 2-bpp
3161 * cases the result needs replicating, for the 4-bpp case the above
3162 * generates a full 32 bits.
3163 */
3164# define MASK_EXPAND(m,d) ((m)*((d)==1?0x01010101:((d)==2?0x00010001:1)))
3165
3166# define S_MASK(p,d,s) MASK_EXPAND(S_MASKx(p,0,d,s) + S_MASKx(p,1,d,s) +\
3167 S_MASKx(p,2,d,s) + S_MASKx(p,3,d,s) + S_MASKx(p,4,d,s) +\
3168 S_MASKx(p,5,d,s) + S_MASKx(p,6,d,s) + S_MASKx(p,7,d,s), d)
3169
3170# define B_MASK(p,d,s) MASK_EXPAND(B_MASKx(p,0,d,s) + B_MASKx(p,1,d,s) +\
3171 B_MASKx(p,2,d,s) + B_MASKx(p,3,d,s) + B_MASKx(p,4,d,s) +\
3172 B_MASKx(p,5,d,s) + B_MASKx(p,6,d,s) + B_MASKx(p,7,d,s), d)
3173
3174#if PNG_USE_COMPILE_TIME_MASKS
3175 /* Utility macros to construct all the masks for a depth/swap
3176 * combination. The 's' parameter says whether the format is PNG
3177 * (big endian bytes) or not. Only the three odd-numbered passes are
3178 * required for the display/block algorithm.
3179 */
3180# define S_MASKS(d,s) { S_MASK(0,d,s), S_MASK(1,d,s), S_MASK(2,d,s),\
3181 S_MASK(3,d,s), S_MASK(4,d,s), S_MASK(5,d,s) }
3182
3183# define B_MASKS(d,s) { B_MASK(1,d,s), S_MASK(3,d,s), S_MASK(5,d,s) }
3184
3185# define DEPTH_INDEX(d) ((d)==1?0:((d)==2?1:2))
3186
3187 /* Hence the pre-compiled masks indexed by PACKSWAP (or not), depth and
3188 * then pass:
3189 */
3190 static PNG_CONST png_uint_32 row_mask[2/*PACKSWAP*/][3/*depth*/][6] =
3191 {
3192 /* Little-endian byte masks for PACKSWAP */
3193 { S_MASKS(1,0), S_MASKS(2,0), S_MASKS(4,0) },
3194 /* Normal (big-endian byte) masks - PNG format */
3195 { S_MASKS(1,1), S_MASKS(2,1), S_MASKS(4,1) }
3196 };
3197
3198 /* display_mask has only three entries for the odd passes, so index by
3199 * pass>>1.
3200 */
3201 static PNG_CONST png_uint_32 display_mask[2][3][3] =
3202 {
3203 /* Little-endian byte masks for PACKSWAP */
3204 { B_MASKS(1,0), B_MASKS(2,0), B_MASKS(4,0) },
3205 /* Normal (big-endian byte) masks - PNG format */
3206 { B_MASKS(1,1), B_MASKS(2,1), B_MASKS(4,1) }
3207 };
3208
3209# define MASK(pass,depth,display,png)\
3210 ((display)?display_mask[png][DEPTH_INDEX(depth)][pass>>1]:\
3211 row_mask[png][DEPTH_INDEX(depth)][pass])
3212
3213#else /* !PNG_USE_COMPILE_TIME_MASKS */
3214 /* This is the runtime alternative: it seems unlikely that this will
3215 * ever be either smaller or faster than the compile time approach.
3216 */
3217# define MASK(pass,depth,display,png)\
3218 ((display)?B_MASK(pass,depth,png):S_MASK(pass,depth,png))
3219#endif /* !PNG_USE_COMPILE_TIME_MASKS */
3220
3221 /* Use the appropriate mask to copy the required bits. In some cases
3222 * the byte mask will be 0 or 0xff, optimize these cases. row_width is
3223 * the number of pixels, but the code copies bytes, so it is necessary
3224 * to special case the end.
3225 */
3226 png_uint_32 pixels_per_byte = 8 / pixel_depth;
3227 png_uint_32 mask;
3228
3229# ifdef PNG_READ_PACKSWAP_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08003230 if (png_ptr->transformations & PNG_PACKSWAP)
Chris Craikb50c2172013-07-29 15:28:30 -07003231 mask = MASK(pass, pixel_depth, display, 0);
3232
The Android Open Source Project893912b2009-03-03 19:30:05 -08003233 else
Chris Craikb50c2172013-07-29 15:28:30 -07003234# endif
3235 mask = MASK(pass, pixel_depth, display, 1);
The Android Open Source Project893912b2009-03-03 19:30:05 -08003236
Chris Craikb50c2172013-07-29 15:28:30 -07003237 for (;;)
The Android Open Source Project893912b2009-03-03 19:30:05 -08003238 {
Chris Craikb50c2172013-07-29 15:28:30 -07003239 png_uint_32 m;
The Android Open Source Project893912b2009-03-03 19:30:05 -08003240
Chris Craikb50c2172013-07-29 15:28:30 -07003241 /* It doesn't matter in the following if png_uint_32 has more than
3242 * 32 bits because the high bits always match those in m<<24; it is,
3243 * however, essential to use OR here, not +, because of this.
3244 */
3245 m = mask;
3246 mask = (m >> 8) | (m << 24); /* rotate right to good compilers */
3247 m &= 0xff;
3248
3249 if (m != 0) /* something to copy */
The Android Open Source Project893912b2009-03-03 19:30:05 -08003250 {
Chris Craikb50c2172013-07-29 15:28:30 -07003251 if (m != 0xff)
3252 *dp = (png_byte)((*dp & ~m) | (*sp & m));
The Android Open Source Project893912b2009-03-03 19:30:05 -08003253 else
Chris Craikb50c2172013-07-29 15:28:30 -07003254 *dp = *sp;
The Android Open Source Project893912b2009-03-03 19:30:05 -08003255 }
The Android Open Source Project893912b2009-03-03 19:30:05 -08003256
Chris Craikb50c2172013-07-29 15:28:30 -07003257 /* NOTE: this may overwrite the last byte with garbage if the image
3258 * is not an exact number of bytes wide; libpng has always done
3259 * this.
3260 */
3261 if (row_width <= pixels_per_byte)
3262 break; /* May need to restore part of the last byte */
The Android Open Source Project893912b2009-03-03 19:30:05 -08003263
Chris Craikb50c2172013-07-29 15:28:30 -07003264 row_width -= pixels_per_byte;
3265 ++dp;
3266 ++sp;
The Android Open Source Project893912b2009-03-03 19:30:05 -08003267 }
3268 }
Chris Craikb50c2172013-07-29 15:28:30 -07003269
3270 else /* pixel_depth >= 8 */
3271 {
3272 unsigned int bytes_to_copy, bytes_to_jump;
3273
3274 /* Validate the depth - it must be a multiple of 8 */
3275 if (pixel_depth & 7)
3276 png_error(png_ptr, "invalid user transform pixel depth");
3277
3278 pixel_depth >>= 3; /* now in bytes */
3279 row_width *= pixel_depth;
3280
3281 /* Regardless of pass number the Adam 7 interlace always results in a
3282 * fixed number of pixels to copy then to skip. There may be a
3283 * different number of pixels to skip at the start though.
3284 */
3285 {
3286 unsigned int offset = PNG_PASS_START_COL(pass) * pixel_depth;
3287
3288 row_width -= offset;
3289 dp += offset;
3290 sp += offset;
3291 }
3292
3293 /* Work out the bytes to copy. */
3294 if (display)
3295 {
3296 /* When doing the 'block' algorithm the pixel in the pass gets
3297 * replicated to adjacent pixels. This is why the even (0,2,4,6)
3298 * passes are skipped above - the entire expanded row is copied.
3299 */
3300 bytes_to_copy = (1<<((6-pass)>>1)) * pixel_depth;
3301
3302 /* But don't allow this number to exceed the actual row width. */
3303 if (bytes_to_copy > row_width)
Henrik Smiding82aab852015-02-20 13:01:11 +01003304 bytes_to_copy = (unsigned int)/*SAFE*/row_width;
Chris Craikb50c2172013-07-29 15:28:30 -07003305 }
3306
3307 else /* normal row; Adam7 only ever gives us one pixel to copy. */
3308 bytes_to_copy = pixel_depth;
3309
3310 /* In Adam7 there is a constant offset between where the pixels go. */
3311 bytes_to_jump = PNG_PASS_COL_OFFSET(pass) * pixel_depth;
3312
3313 /* And simply copy these bytes. Some optimization is possible here,
3314 * depending on the value of 'bytes_to_copy'. Special case the low
3315 * byte counts, which we know to be frequent.
3316 *
3317 * Notice that these cases all 'return' rather than 'break' - this
3318 * avoids an unnecessary test on whether to restore the last byte
3319 * below.
3320 */
3321 switch (bytes_to_copy)
3322 {
3323 case 1:
3324 for (;;)
3325 {
3326 *dp = *sp;
3327
3328 if (row_width <= bytes_to_jump)
3329 return;
3330
3331 dp += bytes_to_jump;
3332 sp += bytes_to_jump;
3333 row_width -= bytes_to_jump;
3334 }
3335
3336 case 2:
3337 /* There is a possibility of a partial copy at the end here; this
3338 * slows the code down somewhat.
3339 */
3340 do
3341 {
3342 dp[0] = sp[0], dp[1] = sp[1];
3343
3344 if (row_width <= bytes_to_jump)
3345 return;
3346
3347 sp += bytes_to_jump;
3348 dp += bytes_to_jump;
3349 row_width -= bytes_to_jump;
3350 }
3351 while (row_width > 1);
3352
3353 /* And there can only be one byte left at this point: */
3354 *dp = *sp;
3355 return;
3356
3357 case 3:
3358 /* This can only be the RGB case, so each copy is exactly one
3359 * pixel and it is not necessary to check for a partial copy.
3360 */
3361 for(;;)
3362 {
3363 dp[0] = sp[0], dp[1] = sp[1], dp[2] = sp[2];
3364
3365 if (row_width <= bytes_to_jump)
3366 return;
3367
3368 sp += bytes_to_jump;
3369 dp += bytes_to_jump;
3370 row_width -= bytes_to_jump;
3371 }
3372
3373 default:
3374#if PNG_ALIGN_TYPE != PNG_ALIGN_NONE
3375 /* Check for double byte alignment and, if possible, use a
3376 * 16-bit copy. Don't attempt this for narrow images - ones that
3377 * are less than an interlace panel wide. Don't attempt it for
3378 * wide bytes_to_copy either - use the memcpy there.
3379 */
3380 if (bytes_to_copy < 16 /*else use memcpy*/ &&
3381 png_isaligned(dp, png_uint_16) &&
3382 png_isaligned(sp, png_uint_16) &&
3383 bytes_to_copy % (sizeof (png_uint_16)) == 0 &&
3384 bytes_to_jump % (sizeof (png_uint_16)) == 0)
3385 {
3386 /* Everything is aligned for png_uint_16 copies, but try for
3387 * png_uint_32 first.
3388 */
3389 if (png_isaligned(dp, png_uint_32) &&
3390 png_isaligned(sp, png_uint_32) &&
3391 bytes_to_copy % (sizeof (png_uint_32)) == 0 &&
3392 bytes_to_jump % (sizeof (png_uint_32)) == 0)
3393 {
3394 png_uint_32p dp32 = png_aligncast(png_uint_32p,dp);
3395 png_const_uint_32p sp32 = png_aligncastconst(
3396 png_const_uint_32p, sp);
3397 size_t skip = (bytes_to_jump-bytes_to_copy) /
3398 (sizeof (png_uint_32));
3399
3400 do
3401 {
3402 size_t c = bytes_to_copy;
3403 do
3404 {
3405 *dp32++ = *sp32++;
3406 c -= (sizeof (png_uint_32));
3407 }
3408 while (c > 0);
3409
3410 if (row_width <= bytes_to_jump)
3411 return;
3412
3413 dp32 += skip;
3414 sp32 += skip;
3415 row_width -= bytes_to_jump;
3416 }
3417 while (bytes_to_copy <= row_width);
3418
3419 /* Get to here when the row_width truncates the final copy.
3420 * There will be 1-3 bytes left to copy, so don't try the
3421 * 16-bit loop below.
3422 */
3423 dp = (png_bytep)dp32;
3424 sp = (png_const_bytep)sp32;
3425 do
3426 *dp++ = *sp++;
3427 while (--row_width > 0);
3428 return;
3429 }
3430
3431 /* Else do it in 16-bit quantities, but only if the size is
3432 * not too large.
3433 */
3434 else
3435 {
3436 png_uint_16p dp16 = png_aligncast(png_uint_16p, dp);
3437 png_const_uint_16p sp16 = png_aligncastconst(
3438 png_const_uint_16p, sp);
3439 size_t skip = (bytes_to_jump-bytes_to_copy) /
3440 (sizeof (png_uint_16));
3441
3442 do
3443 {
3444 size_t c = bytes_to_copy;
3445 do
3446 {
3447 *dp16++ = *sp16++;
3448 c -= (sizeof (png_uint_16));
3449 }
3450 while (c > 0);
3451
3452 if (row_width <= bytes_to_jump)
3453 return;
3454
3455 dp16 += skip;
3456 sp16 += skip;
3457 row_width -= bytes_to_jump;
3458 }
3459 while (bytes_to_copy <= row_width);
3460
3461 /* End of row - 1 byte left, bytes_to_copy > row_width: */
3462 dp = (png_bytep)dp16;
3463 sp = (png_const_bytep)sp16;
3464 do
3465 *dp++ = *sp++;
3466 while (--row_width > 0);
3467 return;
3468 }
3469 }
3470#endif /* PNG_ALIGN_ code */
3471
3472 /* The true default - use a memcpy: */
3473 for (;;)
3474 {
3475 memcpy(dp, sp, bytes_to_copy);
3476
3477 if (row_width <= bytes_to_jump)
3478 return;
3479
3480 sp += bytes_to_jump;
3481 dp += bytes_to_jump;
3482 row_width -= bytes_to_jump;
3483 if (bytes_to_copy > row_width)
Henrik Smiding82aab852015-02-20 13:01:11 +01003484 bytes_to_copy = (unsigned int)/*SAFE*/row_width;
Chris Craikb50c2172013-07-29 15:28:30 -07003485 }
3486 }
3487
3488 /* NOT REACHED*/
3489 } /* pixel_depth >= 8 */
3490
3491 /* Here if pixel_depth < 8 to check 'end_ptr' below. */
The Android Open Source Project893912b2009-03-03 19:30:05 -08003492 }
Chris Craikb50c2172013-07-29 15:28:30 -07003493 else
3494#endif
3495
3496 /* If here then the switch above wasn't used so just memcpy the whole row
3497 * from the temporary row buffer (notice that this overwrites the end of the
3498 * destination row if it is a partial byte.)
3499 */
3500 memcpy(dp, sp, PNG_ROWBYTES(pixel_depth, row_width));
3501
3502 /* Restore the overwritten bits from the last byte if necessary. */
3503 if (end_ptr != NULL)
3504 *end_ptr = (png_byte)((end_byte & end_mask) | (*end_ptr & ~end_mask));
The Android Open Source Project893912b2009-03-03 19:30:05 -08003505}
3506
3507#ifdef PNG_READ_INTERLACING_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08003508void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07003509png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
3510 png_uint_32 transformations /* Because these may affect the byte layout */)
The Android Open Source Project893912b2009-03-03 19:30:05 -08003511{
Patrick Scotta0bb96c2009-07-22 11:50:02 -04003512 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
3513 /* Offset to next interlace block */
Chris Craikb50c2172013-07-29 15:28:30 -07003514 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 -08003515
The Android Open Source Project4215dd12009-03-09 11:52:12 -07003516 png_debug(1, "in png_do_read_interlace");
The Android Open Source Project893912b2009-03-03 19:30:05 -08003517 if (row != NULL && row_info != NULL)
3518 {
3519 png_uint_32 final_width;
3520
3521 final_width = row_info->width * png_pass_inc[pass];
3522
3523 switch (row_info->pixel_depth)
3524 {
3525 case 1:
3526 {
3527 png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3);
3528 png_bytep dp = row + (png_size_t)((final_width - 1) >> 3);
3529 int sshift, dshift;
3530 int s_start, s_end, s_inc;
3531 int jstop = png_pass_inc[pass];
3532 png_byte v;
3533 png_uint_32 i;
3534 int j;
3535
Patrick Scott5f6bd842010-06-28 16:55:16 -04003536#ifdef PNG_READ_PACKSWAP_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08003537 if (transformations & PNG_PACKSWAP)
3538 {
3539 sshift = (int)((row_info->width + 7) & 0x07);
3540 dshift = (int)((final_width + 7) & 0x07);
3541 s_start = 7;
3542 s_end = 0;
3543 s_inc = -1;
3544 }
Chris Craikb50c2172013-07-29 15:28:30 -07003545
The Android Open Source Project893912b2009-03-03 19:30:05 -08003546 else
3547#endif
3548 {
3549 sshift = 7 - (int)((row_info->width + 7) & 0x07);
3550 dshift = 7 - (int)((final_width + 7) & 0x07);
3551 s_start = 0;
3552 s_end = 7;
3553 s_inc = 1;
3554 }
3555
3556 for (i = 0; i < row_info->width; i++)
3557 {
3558 v = (png_byte)((*sp >> sshift) & 0x01);
3559 for (j = 0; j < jstop; j++)
3560 {
Chris Craikb50c2172013-07-29 15:28:30 -07003561 unsigned int tmp = *dp & (0x7f7f >> (7 - dshift));
3562 tmp |= v << dshift;
3563 *dp = (png_byte)(tmp & 0xff);
3564
The Android Open Source Project893912b2009-03-03 19:30:05 -08003565 if (dshift == s_end)
3566 {
3567 dshift = s_start;
3568 dp--;
3569 }
Chris Craikb50c2172013-07-29 15:28:30 -07003570
The Android Open Source Project893912b2009-03-03 19:30:05 -08003571 else
3572 dshift += s_inc;
3573 }
Chris Craikb50c2172013-07-29 15:28:30 -07003574
The Android Open Source Project893912b2009-03-03 19:30:05 -08003575 if (sshift == s_end)
3576 {
3577 sshift = s_start;
3578 sp--;
3579 }
Chris Craikb50c2172013-07-29 15:28:30 -07003580
The Android Open Source Project893912b2009-03-03 19:30:05 -08003581 else
3582 sshift += s_inc;
3583 }
3584 break;
3585 }
Chris Craikb50c2172013-07-29 15:28:30 -07003586
The Android Open Source Project893912b2009-03-03 19:30:05 -08003587 case 2:
3588 {
3589 png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);
3590 png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);
3591 int sshift, dshift;
3592 int s_start, s_end, s_inc;
3593 int jstop = png_pass_inc[pass];
3594 png_uint_32 i;
3595
Patrick Scott5f6bd842010-06-28 16:55:16 -04003596#ifdef PNG_READ_PACKSWAP_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08003597 if (transformations & PNG_PACKSWAP)
3598 {
3599 sshift = (int)(((row_info->width + 3) & 0x03) << 1);
3600 dshift = (int)(((final_width + 3) & 0x03) << 1);
3601 s_start = 6;
3602 s_end = 0;
3603 s_inc = -2;
3604 }
Chris Craikb50c2172013-07-29 15:28:30 -07003605
The Android Open Source Project893912b2009-03-03 19:30:05 -08003606 else
3607#endif
3608 {
3609 sshift = (int)((3 - ((row_info->width + 3) & 0x03)) << 1);
3610 dshift = (int)((3 - ((final_width + 3) & 0x03)) << 1);
3611 s_start = 0;
3612 s_end = 6;
3613 s_inc = 2;
3614 }
3615
3616 for (i = 0; i < row_info->width; i++)
3617 {
3618 png_byte v;
3619 int j;
3620
3621 v = (png_byte)((*sp >> sshift) & 0x03);
3622 for (j = 0; j < jstop; j++)
3623 {
Chris Craikb50c2172013-07-29 15:28:30 -07003624 unsigned int tmp = *dp & (0x3f3f >> (6 - dshift));
3625 tmp |= v << dshift;
3626 *dp = (png_byte)(tmp & 0xff);
3627
The Android Open Source Project893912b2009-03-03 19:30:05 -08003628 if (dshift == s_end)
3629 {
3630 dshift = s_start;
3631 dp--;
3632 }
Chris Craikb50c2172013-07-29 15:28:30 -07003633
The Android Open Source Project893912b2009-03-03 19:30:05 -08003634 else
3635 dshift += s_inc;
3636 }
Chris Craikb50c2172013-07-29 15:28:30 -07003637
The Android Open Source Project893912b2009-03-03 19:30:05 -08003638 if (sshift == s_end)
3639 {
3640 sshift = s_start;
3641 sp--;
3642 }
Chris Craikb50c2172013-07-29 15:28:30 -07003643
The Android Open Source Project893912b2009-03-03 19:30:05 -08003644 else
3645 sshift += s_inc;
3646 }
3647 break;
3648 }
Chris Craikb50c2172013-07-29 15:28:30 -07003649
The Android Open Source Project893912b2009-03-03 19:30:05 -08003650 case 4:
3651 {
3652 png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1);
3653 png_bytep dp = row + (png_size_t)((final_width - 1) >> 1);
3654 int sshift, dshift;
3655 int s_start, s_end, s_inc;
3656 png_uint_32 i;
3657 int jstop = png_pass_inc[pass];
3658
Patrick Scott5f6bd842010-06-28 16:55:16 -04003659#ifdef PNG_READ_PACKSWAP_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08003660 if (transformations & PNG_PACKSWAP)
3661 {
3662 sshift = (int)(((row_info->width + 1) & 0x01) << 2);
3663 dshift = (int)(((final_width + 1) & 0x01) << 2);
3664 s_start = 4;
3665 s_end = 0;
3666 s_inc = -4;
3667 }
Chris Craikb50c2172013-07-29 15:28:30 -07003668
The Android Open Source Project893912b2009-03-03 19:30:05 -08003669 else
3670#endif
3671 {
3672 sshift = (int)((1 - ((row_info->width + 1) & 0x01)) << 2);
3673 dshift = (int)((1 - ((final_width + 1) & 0x01)) << 2);
3674 s_start = 0;
3675 s_end = 4;
3676 s_inc = 4;
3677 }
3678
3679 for (i = 0; i < row_info->width; i++)
3680 {
Chris Craikb50c2172013-07-29 15:28:30 -07003681 png_byte v = (png_byte)((*sp >> sshift) & 0x0f);
The Android Open Source Project893912b2009-03-03 19:30:05 -08003682 int j;
3683
3684 for (j = 0; j < jstop; j++)
3685 {
Chris Craikb50c2172013-07-29 15:28:30 -07003686 unsigned int tmp = *dp & (0xf0f >> (4 - dshift));
3687 tmp |= v << dshift;
3688 *dp = (png_byte)(tmp & 0xff);
3689
The Android Open Source Project893912b2009-03-03 19:30:05 -08003690 if (dshift == s_end)
3691 {
3692 dshift = s_start;
3693 dp--;
3694 }
Chris Craikb50c2172013-07-29 15:28:30 -07003695
The Android Open Source Project893912b2009-03-03 19:30:05 -08003696 else
3697 dshift += s_inc;
3698 }
Chris Craikb50c2172013-07-29 15:28:30 -07003699
The Android Open Source Project893912b2009-03-03 19:30:05 -08003700 if (sshift == s_end)
3701 {
3702 sshift = s_start;
3703 sp--;
3704 }
Chris Craikb50c2172013-07-29 15:28:30 -07003705
The Android Open Source Project893912b2009-03-03 19:30:05 -08003706 else
3707 sshift += s_inc;
3708 }
3709 break;
3710 }
Chris Craikb50c2172013-07-29 15:28:30 -07003711
The Android Open Source Project893912b2009-03-03 19:30:05 -08003712 default:
3713 {
3714 png_size_t pixel_bytes = (row_info->pixel_depth >> 3);
Chris Craikb50c2172013-07-29 15:28:30 -07003715
Patrick Scott5f6bd842010-06-28 16:55:16 -04003716 png_bytep sp = row + (png_size_t)(row_info->width - 1)
3717 * pixel_bytes;
Chris Craikb50c2172013-07-29 15:28:30 -07003718
The Android Open Source Project893912b2009-03-03 19:30:05 -08003719 png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes;
3720
3721 int jstop = png_pass_inc[pass];
3722 png_uint_32 i;
3723
3724 for (i = 0; i < row_info->width; i++)
3725 {
Chris Craikb50c2172013-07-29 15:28:30 -07003726 png_byte v[8]; /* SAFE; pixel_depth does not exceed 64 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08003727 int j;
3728
Chris Craikb50c2172013-07-29 15:28:30 -07003729 memcpy(v, sp, pixel_bytes);
3730
The Android Open Source Project893912b2009-03-03 19:30:05 -08003731 for (j = 0; j < jstop; j++)
3732 {
Chris Craikb50c2172013-07-29 15:28:30 -07003733 memcpy(dp, v, pixel_bytes);
The Android Open Source Project893912b2009-03-03 19:30:05 -08003734 dp -= pixel_bytes;
3735 }
Chris Craikb50c2172013-07-29 15:28:30 -07003736
The Android Open Source Project893912b2009-03-03 19:30:05 -08003737 sp -= pixel_bytes;
3738 }
3739 break;
3740 }
3741 }
Chris Craikb50c2172013-07-29 15:28:30 -07003742
The Android Open Source Project893912b2009-03-03 19:30:05 -08003743 row_info->width = final_width;
The Android Open Source Project4215dd12009-03-09 11:52:12 -07003744 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width);
The Android Open Source Project893912b2009-03-03 19:30:05 -08003745 }
Patrick Scott5f6bd842010-06-28 16:55:16 -04003746#ifndef PNG_READ_PACKSWAP_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07003747 PNG_UNUSED(transformations) /* Silence compiler warning */
The Android Open Source Project893912b2009-03-03 19:30:05 -08003748#endif
3749}
3750#endif /* PNG_READ_INTERLACING_SUPPORTED */
3751
Chris Craikb50c2172013-07-29 15:28:30 -07003752static void
3753png_read_filter_row_sub(png_row_infop row_info, png_bytep row,
3754 png_const_bytep prev_row)
The Android Open Source Project893912b2009-03-03 19:30:05 -08003755{
Chris Craikb50c2172013-07-29 15:28:30 -07003756 png_size_t i;
3757 png_size_t istop = row_info->rowbytes;
3758 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
3759 png_bytep rp = row + bpp;
3760
3761 PNG_UNUSED(prev_row)
3762
3763 for (i = bpp; i < istop; i++)
The Android Open Source Project893912b2009-03-03 19:30:05 -08003764 {
Chris Craikb50c2172013-07-29 15:28:30 -07003765 *rp = (png_byte)(((int)(*rp) + (int)(*(rp-bpp))) & 0xff);
3766 rp++;
The Android Open Source Project893912b2009-03-03 19:30:05 -08003767 }
3768}
3769
Chris Craikb50c2172013-07-29 15:28:30 -07003770static void
3771png_read_filter_row_up(png_row_infop row_info, png_bytep row,
3772 png_const_bytep prev_row)
Joseph Wen4ce0ee12010-08-20 10:42:22 +08003773{
Chris Craikb50c2172013-07-29 15:28:30 -07003774 png_size_t i;
3775 png_size_t istop = row_info->rowbytes;
3776 png_bytep rp = row;
3777 png_const_bytep pp = prev_row;
Joseph Wen4ce0ee12010-08-20 10:42:22 +08003778
Chris Craikb50c2172013-07-29 15:28:30 -07003779 for (i = 0; i < istop; i++)
3780 {
3781 *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
3782 rp++;
3783 }
Joseph Wen4ce0ee12010-08-20 10:42:22 +08003784}
Chris Craikb50c2172013-07-29 15:28:30 -07003785
3786static void
3787png_read_filter_row_avg(png_row_infop row_info, png_bytep row,
3788 png_const_bytep prev_row)
3789{
3790 png_size_t i;
3791 png_bytep rp = row;
3792 png_const_bytep pp = prev_row;
3793 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
3794 png_size_t istop = row_info->rowbytes - bpp;
3795
3796 for (i = 0; i < bpp; i++)
3797 {
3798 *rp = (png_byte)(((int)(*rp) +
3799 ((int)(*pp++) / 2 )) & 0xff);
3800
3801 rp++;
3802 }
3803
3804 for (i = 0; i < istop; i++)
3805 {
3806 *rp = (png_byte)(((int)(*rp) +
3807 (int)(*pp++ + *(rp-bpp)) / 2 ) & 0xff);
3808
3809 rp++;
3810 }
3811}
3812
3813static void
3814png_read_filter_row_paeth_1byte_pixel(png_row_infop row_info, png_bytep row,
3815 png_const_bytep prev_row)
3816{
3817 png_bytep rp_end = row + row_info->rowbytes;
3818 int a, c;
3819
3820 /* First pixel/byte */
3821 c = *prev_row++;
3822 a = *row + c;
3823 *row++ = (png_byte)a;
3824
3825 /* Remainder */
3826 while (row < rp_end)
3827 {
3828 int b, pa, pb, pc, p;
3829
3830 a &= 0xff; /* From previous iteration or start */
3831 b = *prev_row++;
3832
3833 p = b - c;
3834 pc = a - c;
3835
3836# ifdef PNG_USE_ABS
3837 pa = abs(p);
3838 pb = abs(pc);
3839 pc = abs(p + pc);
3840# else
3841 pa = p < 0 ? -p : p;
3842 pb = pc < 0 ? -pc : pc;
3843 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
3844# endif
3845
3846 /* Find the best predictor, the least of pa, pb, pc favoring the earlier
3847 * ones in the case of a tie.
3848 */
3849 if (pb < pa) pa = pb, a = b;
3850 if (pc < pa) a = c;
3851
3852 /* Calculate the current pixel in a, and move the previous row pixel to c
3853 * for the next time round the loop
3854 */
3855 c = b;
3856 a += *row;
3857 *row++ = (png_byte)a;
3858 }
3859}
3860
3861static void
3862png_read_filter_row_paeth_multibyte_pixel(png_row_infop row_info, png_bytep row,
3863 png_const_bytep prev_row)
3864{
3865 int bpp = (row_info->pixel_depth + 7) >> 3;
3866 png_bytep rp_end = row + bpp;
3867
3868 /* Process the first pixel in the row completely (this is the same as 'up'
3869 * because there is only one candidate predictor for the first row).
3870 */
3871 while (row < rp_end)
3872 {
3873 int a = *row + *prev_row++;
3874 *row++ = (png_byte)a;
3875 }
3876
3877 /* Remainder */
3878 rp_end += row_info->rowbytes - bpp;
3879
3880 while (row < rp_end)
3881 {
3882 int a, b, c, pa, pb, pc, p;
3883
3884 c = *(prev_row - bpp);
3885 a = *(row - bpp);
3886 b = *prev_row++;
3887
3888 p = b - c;
3889 pc = a - c;
3890
3891# ifdef PNG_USE_ABS
3892 pa = abs(p);
3893 pb = abs(pc);
3894 pc = abs(p + pc);
3895# else
3896 pa = p < 0 ? -p : p;
3897 pb = pc < 0 ? -pc : pc;
3898 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
3899# endif
3900
3901 if (pb < pa) pa = pb, a = b;
3902 if (pc < pa) a = c;
3903
Chris Craikb50c2172013-07-29 15:28:30 -07003904 a += *row;
3905 *row++ = (png_byte)a;
3906 }
3907}
3908
3909static void
3910png_init_filter_functions(png_structrp pp)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05303911 /* This function is called once for every PNG image (except for PNG images
3912 * that only use PNG_FILTER_VALUE_NONE for all rows) to set the
Chris Craikb50c2172013-07-29 15:28:30 -07003913 * implementations required to reverse the filtering of PNG rows. Reversing
3914 * the filter is the first transformation performed on the row data. It is
3915 * performed in place, therefore an implementation can be selected based on
3916 * the image pixel format. If the implementation depends on image width then
3917 * take care to ensure that it works correctly if the image is interlaced -
3918 * interlacing causes the actual row width to vary.
3919 */
3920{
3921 unsigned int bpp = (pp->pixel_depth + 7) >> 3;
3922
3923 pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub;
3924 pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up;
3925 pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg;
3926 if (bpp == 1)
3927 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
3928 png_read_filter_row_paeth_1byte_pixel;
3929 else
3930 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
3931 png_read_filter_row_paeth_multibyte_pixel;
3932
3933#ifdef PNG_FILTER_OPTIMIZATIONS
3934 /* To use this define PNG_FILTER_OPTIMIZATIONS as the name of a function to
3935 * call to install hardware optimizations for the above functions; simply
3936 * replace whatever elements of the pp->read_filter[] array with a hardware
3937 * specific (or, for that matter, generic) optimization.
3938 *
3939 * To see an example of this examine what configure.ac does when
3940 * --enable-arm-neon is specified on the command line.
3941 */
3942 PNG_FILTER_OPTIMIZATIONS(pp, bpp);
Joseph Wen4ce0ee12010-08-20 10:42:22 +08003943#endif
Chris Craikb50c2172013-07-29 15:28:30 -07003944}
3945
3946void /* PRIVATE */
3947png_read_filter_row(png_structrp pp, png_row_infop row_info, png_bytep row,
3948 png_const_bytep prev_row, int filter)
3949{
3950 /* OPTIMIZATION: DO NOT MODIFY THIS FUNCTION, instead #define
3951 * PNG_FILTER_OPTIMIZATIONS to a function that overrides the generic
3952 * implementations. See png_init_filter_functions above.
3953 */
Chris Craikb50c2172013-07-29 15:28:30 -07003954 if (filter > PNG_FILTER_VALUE_NONE && filter < PNG_FILTER_VALUE_LAST)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05303955 {
3956 if (pp->read_filter[0] == NULL)
3957 png_init_filter_functions(pp);
3958
Chris Craikb50c2172013-07-29 15:28:30 -07003959 pp->read_filter[filter-1](row_info, row, prev_row);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05303960 }
Chris Craikb50c2172013-07-29 15:28:30 -07003961}
Joseph Wen4ce0ee12010-08-20 10:42:22 +08003962
Patrick Scott5f6bd842010-06-28 16:55:16 -04003963#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08003964void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07003965png_read_IDAT_data(png_structrp png_ptr, png_bytep output,
3966 png_alloc_size_t avail_out)
3967{
3968 /* Loop reading IDATs and decompressing the result into output[avail_out] */
3969 png_ptr->zstream.next_out = output;
3970 png_ptr->zstream.avail_out = 0; /* safety: set below */
3971
3972 if (output == NULL)
3973 avail_out = 0;
3974
3975 do
3976 {
3977 int ret;
3978 png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];
3979
3980 if (png_ptr->zstream.avail_in == 0)
3981 {
3982 uInt avail_in;
3983 png_bytep buffer;
3984
3985 while (png_ptr->idat_size == 0)
3986 {
Sireesh Tripurarib0277d02014-05-09 15:39:12 +05303987#ifdef PNG_INDEX_SUPPORTED
3988 if (png_ptr->index) {
3989 png_opt_crc_finish(png_ptr, 0);
3990 png_ptr->index->stream_idat_position = png_ptr->total_data_read;
3991 } else
3992#endif
Chris Craikb50c2172013-07-29 15:28:30 -07003993 png_crc_finish(png_ptr, 0);
3994
3995 png_ptr->idat_size = png_read_chunk_header(png_ptr);
3996 /* This is an error even in the 'check' case because the code just
3997 * consumed a non-IDAT header.
3998 */
3999 if (png_ptr->chunk_name != png_IDAT)
4000 png_error(png_ptr, "Not enough image data");
4001 }
4002
4003 avail_in = png_ptr->IDAT_read_size;
4004
4005 if (avail_in > png_ptr->idat_size)
4006 avail_in = (uInt)png_ptr->idat_size;
4007
4008 /* A PNG with a gradually increasing IDAT size will defeat this attempt
4009 * to minimize memory usage by causing lots of re-allocs, but
4010 * realistically doing IDAT_read_size re-allocs is not likely to be a
4011 * big problem.
4012 */
4013 buffer = png_read_buffer(png_ptr, avail_in, 0/*error*/);
4014
4015 png_crc_read(png_ptr, buffer, avail_in);
4016 png_ptr->idat_size -= avail_in;
4017
4018 png_ptr->zstream.next_in = buffer;
4019 png_ptr->zstream.avail_in = avail_in;
4020 }
4021
4022 /* And set up the output side. */
4023 if (output != NULL) /* standard read */
4024 {
4025 uInt out = ZLIB_IO_MAX;
4026
4027 if (out > avail_out)
4028 out = (uInt)avail_out;
4029
4030 avail_out -= out;
4031 png_ptr->zstream.avail_out = out;
4032 }
4033
4034 else /* after last row, checking for end */
4035 {
4036 png_ptr->zstream.next_out = tmpbuf;
4037 png_ptr->zstream.avail_out = (sizeof tmpbuf);
4038 }
4039
4040 /* Use NO_FLUSH; this gives zlib the maximum opportunity to optimize the
4041 * process. If the LZ stream is truncated the sequential reader will
4042 * terminally damage the stream, above, by reading the chunk header of the
4043 * following chunk (it then exits with png_error).
4044 *
4045 * TODO: deal more elegantly with truncated IDAT lists.
4046 */
4047 ret = inflate(&png_ptr->zstream, Z_NO_FLUSH);
4048
4049 /* Take the unconsumed output back. */
4050 if (output != NULL)
4051 avail_out += png_ptr->zstream.avail_out;
4052
4053 else /* avail_out counts the extra bytes */
4054 avail_out += (sizeof tmpbuf) - png_ptr->zstream.avail_out;
4055
4056 png_ptr->zstream.avail_out = 0;
4057
4058 if (ret == Z_STREAM_END)
4059 {
4060 /* Do this for safety; we won't read any more into this row. */
4061 png_ptr->zstream.next_out = NULL;
4062
4063 png_ptr->mode |= PNG_AFTER_IDAT;
4064 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
4065
4066 if (png_ptr->zstream.avail_in > 0 || png_ptr->idat_size > 0)
4067 png_chunk_benign_error(png_ptr, "Extra compressed data");
4068 break;
4069 }
4070
4071 if (ret != Z_OK)
Sireesh Tripurarib0277d02014-05-09 15:39:12 +05304072#ifdef PNG_INDEX_SUPPORTED
4073 if (png_ptr->index && png_ptr->row_number != png_ptr->height - 1)
4074#endif
Chris Craikb50c2172013-07-29 15:28:30 -07004075 {
4076 png_zstream_error(png_ptr, ret);
4077
4078 if (output != NULL)
4079 png_chunk_error(png_ptr, png_ptr->zstream.msg);
4080
4081 else /* checking */
4082 {
4083 png_chunk_benign_error(png_ptr, png_ptr->zstream.msg);
4084 return;
4085 }
4086 }
4087 } while (avail_out > 0);
4088
4089 if (avail_out > 0)
4090 {
4091 /* The stream ended before the image; this is the same as too few IDATs so
4092 * should be handled the same way.
4093 */
4094 if (output != NULL)
4095 png_error(png_ptr, "Not enough image data");
4096
4097 else /* the deflate stream contained extra data */
4098 png_chunk_benign_error(png_ptr, "Too much image data");
4099 }
4100}
4101
4102void /* PRIVATE */
4103png_read_finish_IDAT(png_structrp png_ptr)
4104{
4105 /* We don't need any more data and the stream should have ended, however the
4106 * LZ end code may actually not have been processed. In this case we must
4107 * read it otherwise stray unread IDAT data or, more likely, an IDAT chunk
4108 * may still remain to be consumed.
4109 */
4110 if (!(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED))
4111 {
4112 /* The NULL causes png_read_IDAT_data to swallow any remaining bytes in
4113 * the compressed stream, but the stream may be damaged too, so even after
4114 * this call we may need to terminate the zstream ownership.
4115 */
4116 png_read_IDAT_data(png_ptr, NULL, 0);
4117 png_ptr->zstream.next_out = NULL; /* safety */
4118
4119 /* Now clear everything out for safety; the following may not have been
4120 * done.
4121 */
4122 if (!(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED))
4123 {
4124 png_ptr->mode |= PNG_AFTER_IDAT;
4125 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
4126 }
4127 }
4128
4129 /* If the zstream has not been released do it now *and* terminate the reading
4130 * of the final IDAT chunk.
4131 */
4132 if (png_ptr->zowner == png_IDAT)
4133 {
4134 /* Always do this; the pointers otherwise point into the read buffer. */
4135 png_ptr->zstream.next_in = NULL;
4136 png_ptr->zstream.avail_in = 0;
4137
4138 /* Now we no longer own the zstream. */
4139 png_ptr->zowner = 0;
4140
4141 /* The slightly weird semantics of the sequential IDAT reading is that we
4142 * are always in or at the end of an IDAT chunk, so we always need to do a
4143 * crc_finish here. If idat_size is non-zero we also need to read the
4144 * spurious bytes at the end of the chunk now.
4145 */
4146 (void)png_crc_finish(png_ptr, png_ptr->idat_size);
4147 }
4148}
4149
Sireesh Tripurarib0277d02014-05-09 15:39:12 +05304150#ifdef PNG_INDEX_SUPPORTED
4151void /* PRIVATE */
4152png_set_interlaced_pass(png_structp png_ptr, int pass)
4153{
4154 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
4155 /* Start of interlace block */
4156 PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
4157 /* Offset to next interlace block */
4158 PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
4159 /* Start of interlace block in the y direction */
4160 PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
4161 /* Offset to next interlace block in the y direction */
4162 PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
4163 png_ptr->pass = pass;
4164 png_ptr->iwidth = (png_ptr->width +
4165 png_pass_inc[png_ptr->pass] - 1 -
4166 png_pass_start[png_ptr->pass]) /
4167 png_pass_inc[png_ptr->pass];
4168}
4169#endif
4170
Chris Craikb50c2172013-07-29 15:28:30 -07004171void /* PRIVATE */
4172png_read_finish_row(png_structrp png_ptr)
The Android Open Source Project893912b2009-03-03 19:30:05 -08004173{
The Android Open Source Project893912b2009-03-03 19:30:05 -08004174#ifdef PNG_READ_INTERLACING_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004175 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
The Android Open Source Project893912b2009-03-03 19:30:05 -08004176
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004177 /* Start of interlace block */
Chris Craikb50c2172013-07-29 15:28:30 -07004178 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 -08004179
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004180 /* Offset to next interlace block */
Chris Craikb50c2172013-07-29 15:28:30 -07004181 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 -08004182
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004183 /* Start of interlace block in the y direction */
Chris Craikb50c2172013-07-29 15:28:30 -07004184 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 -08004185
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004186 /* Offset to next interlace block in the y direction */
Chris Craikb50c2172013-07-29 15:28:30 -07004187 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 -08004188#endif /* PNG_READ_INTERLACING_SUPPORTED */
The Android Open Source Project893912b2009-03-03 19:30:05 -08004189
The Android Open Source Project4215dd12009-03-09 11:52:12 -07004190 png_debug(1, "in png_read_finish_row");
The Android Open Source Project893912b2009-03-03 19:30:05 -08004191 png_ptr->row_number++;
4192 if (png_ptr->row_number < png_ptr->num_rows)
4193 return;
4194
4195#ifdef PNG_READ_INTERLACING_SUPPORTED
4196 if (png_ptr->interlaced)
4197 {
4198 png_ptr->row_number = 0;
Chris Craikb50c2172013-07-29 15:28:30 -07004199
4200 /* TO DO: don't do this if prev_row isn't needed (requires
4201 * read-ahead of the next row's filter byte.
4202 */
4203 memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
4204
The Android Open Source Project893912b2009-03-03 19:30:05 -08004205 do
4206 {
4207 png_ptr->pass++;
Chris Craikb50c2172013-07-29 15:28:30 -07004208
The Android Open Source Project893912b2009-03-03 19:30:05 -08004209 if (png_ptr->pass >= 7)
4210 break;
Chris Craikb50c2172013-07-29 15:28:30 -07004211
The Android Open Source Project893912b2009-03-03 19:30:05 -08004212 png_ptr->iwidth = (png_ptr->width +
4213 png_pass_inc[png_ptr->pass] - 1 -
4214 png_pass_start[png_ptr->pass]) /
4215 png_pass_inc[png_ptr->pass];
4216
The Android Open Source Project893912b2009-03-03 19:30:05 -08004217 if (!(png_ptr->transformations & PNG_INTERLACE))
4218 {
4219 png_ptr->num_rows = (png_ptr->height +
Chris Craikb50c2172013-07-29 15:28:30 -07004220 png_pass_yinc[png_ptr->pass] - 1 -
4221 png_pass_ystart[png_ptr->pass]) /
4222 png_pass_yinc[png_ptr->pass];
The Android Open Source Project893912b2009-03-03 19:30:05 -08004223 }
Chris Craikb50c2172013-07-29 15:28:30 -07004224
The Android Open Source Project893912b2009-03-03 19:30:05 -08004225 else /* if (png_ptr->transformations & PNG_INTERLACE) */
Chris Craikb50c2172013-07-29 15:28:30 -07004226 break; /* libpng deinterlacing sees every row */
4227
4228 } while (png_ptr->num_rows == 0 || png_ptr->iwidth == 0);
The Android Open Source Project893912b2009-03-03 19:30:05 -08004229
4230 if (png_ptr->pass < 7)
4231 return;
4232 }
4233#endif /* PNG_READ_INTERLACING_SUPPORTED */
4234
Chris Craikb50c2172013-07-29 15:28:30 -07004235 /* Here after at the end of the last row of the last pass. */
4236 png_read_finish_IDAT(png_ptr);
The Android Open Source Project893912b2009-03-03 19:30:05 -08004237}
Patrick Scott5f6bd842010-06-28 16:55:16 -04004238#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
The Android Open Source Project893912b2009-03-03 19:30:05 -08004239
4240void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07004241png_read_start_row(png_structrp png_ptr)
The Android Open Source Project893912b2009-03-03 19:30:05 -08004242{
The Android Open Source Project893912b2009-03-03 19:30:05 -08004243#ifdef PNG_READ_INTERLACING_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004244 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
The Android Open Source Project893912b2009-03-03 19:30:05 -08004245
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004246 /* Start of interlace block */
Chris Craikb50c2172013-07-29 15:28:30 -07004247 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 -08004248
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004249 /* Offset to next interlace block */
Chris Craikb50c2172013-07-29 15:28:30 -07004250 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 -08004251
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004252 /* Start of interlace block in the y direction */
Chris Craikb50c2172013-07-29 15:28:30 -07004253 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 -08004254
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004255 /* Offset to next interlace block in the y direction */
Chris Craikb50c2172013-07-29 15:28:30 -07004256 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 -08004257#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -08004258
4259 int max_pixel_depth;
The Android Open Source Project4215dd12009-03-09 11:52:12 -07004260 png_size_t row_bytes;
The Android Open Source Project893912b2009-03-03 19:30:05 -08004261
The Android Open Source Project4215dd12009-03-09 11:52:12 -07004262 png_debug(1, "in png_read_start_row");
Chris Craikb50c2172013-07-29 15:28:30 -07004263
4264#ifdef PNG_READ_TRANSFORMS_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08004265 png_init_read_transformations(png_ptr);
Chris Craikb50c2172013-07-29 15:28:30 -07004266#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -08004267#ifdef PNG_READ_INTERLACING_SUPPORTED
4268 if (png_ptr->interlaced)
4269 {
4270 if (!(png_ptr->transformations & PNG_INTERLACE))
4271 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
Chris Craikb50c2172013-07-29 15:28:30 -07004272 png_pass_ystart[0]) / png_pass_yinc[0];
4273
The Android Open Source Project893912b2009-03-03 19:30:05 -08004274 else
4275 png_ptr->num_rows = png_ptr->height;
4276
4277 png_ptr->iwidth = (png_ptr->width +
Chris Craikb50c2172013-07-29 15:28:30 -07004278 png_pass_inc[png_ptr->pass] - 1 -
4279 png_pass_start[png_ptr->pass]) /
4280 png_pass_inc[png_ptr->pass];
The Android Open Source Project893912b2009-03-03 19:30:05 -08004281 }
Chris Craikb50c2172013-07-29 15:28:30 -07004282
The Android Open Source Project893912b2009-03-03 19:30:05 -08004283 else
4284#endif /* PNG_READ_INTERLACING_SUPPORTED */
4285 {
4286 png_ptr->num_rows = png_ptr->height;
4287 png_ptr->iwidth = png_ptr->width;
The Android Open Source Project893912b2009-03-03 19:30:05 -08004288 }
Chris Craikb50c2172013-07-29 15:28:30 -07004289
The Android Open Source Project893912b2009-03-03 19:30:05 -08004290 max_pixel_depth = png_ptr->pixel_depth;
4291
Chris Craikb50c2172013-07-29 15:28:30 -07004292 /* WARNING: * png_read_transform_info (pngrtran.c) performs a simpliar set of
4293 * calculations to calculate the final pixel depth, then
4294 * png_do_read_transforms actually does the transforms. This means that the
4295 * code which effectively calculates this value is actually repeated in three
4296 * separate places. They must all match. Innocent changes to the order of
4297 * transformations can and will break libpng in a way that causes memory
4298 * overwrites.
4299 *
4300 * TODO: fix this.
4301 */
Patrick Scott5f6bd842010-06-28 16:55:16 -04004302#ifdef PNG_READ_PACK_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08004303 if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8)
4304 max_pixel_depth = 8;
4305#endif
4306
Patrick Scott5f6bd842010-06-28 16:55:16 -04004307#ifdef PNG_READ_EXPAND_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08004308 if (png_ptr->transformations & PNG_EXPAND)
4309 {
4310 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
4311 {
4312 if (png_ptr->num_trans)
4313 max_pixel_depth = 32;
Chris Craikb50c2172013-07-29 15:28:30 -07004314
The Android Open Source Project893912b2009-03-03 19:30:05 -08004315 else
4316 max_pixel_depth = 24;
4317 }
Chris Craikb50c2172013-07-29 15:28:30 -07004318
The Android Open Source Project893912b2009-03-03 19:30:05 -08004319 else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
4320 {
4321 if (max_pixel_depth < 8)
4322 max_pixel_depth = 8;
Chris Craikb50c2172013-07-29 15:28:30 -07004323
The Android Open Source Project893912b2009-03-03 19:30:05 -08004324 if (png_ptr->num_trans)
4325 max_pixel_depth *= 2;
4326 }
Chris Craikb50c2172013-07-29 15:28:30 -07004327
The Android Open Source Project893912b2009-03-03 19:30:05 -08004328 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
4329 {
4330 if (png_ptr->num_trans)
4331 {
4332 max_pixel_depth *= 4;
4333 max_pixel_depth /= 3;
4334 }
4335 }
4336 }
4337#endif
4338
Chris Craikb50c2172013-07-29 15:28:30 -07004339#ifdef PNG_READ_EXPAND_16_SUPPORTED
4340 if (png_ptr->transformations & PNG_EXPAND_16)
4341 {
4342# ifdef PNG_READ_EXPAND_SUPPORTED
4343 /* In fact it is an error if it isn't supported, but checking is
4344 * the safe way.
4345 */
4346 if (png_ptr->transformations & PNG_EXPAND)
4347 {
4348 if (png_ptr->bit_depth < 16)
4349 max_pixel_depth *= 2;
4350 }
4351 else
4352# endif
4353 png_ptr->transformations &= ~PNG_EXPAND_16;
4354 }
4355#endif
4356
Patrick Scott5f6bd842010-06-28 16:55:16 -04004357#ifdef PNG_READ_FILLER_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08004358 if (png_ptr->transformations & (PNG_FILLER))
4359 {
Chris Craikb50c2172013-07-29 15:28:30 -07004360 if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
The Android Open Source Project893912b2009-03-03 19:30:05 -08004361 {
4362 if (max_pixel_depth <= 8)
4363 max_pixel_depth = 16;
Chris Craikb50c2172013-07-29 15:28:30 -07004364
The Android Open Source Project893912b2009-03-03 19:30:05 -08004365 else
4366 max_pixel_depth = 32;
4367 }
Chris Craikb50c2172013-07-29 15:28:30 -07004368
4369 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB ||
4370 png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
The Android Open Source Project893912b2009-03-03 19:30:05 -08004371 {
4372 if (max_pixel_depth <= 32)
4373 max_pixel_depth = 32;
Chris Craikb50c2172013-07-29 15:28:30 -07004374
The Android Open Source Project893912b2009-03-03 19:30:05 -08004375 else
4376 max_pixel_depth = 64;
4377 }
4378 }
4379#endif
4380
Patrick Scott5f6bd842010-06-28 16:55:16 -04004381#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08004382 if (png_ptr->transformations & PNG_GRAY_TO_RGB)
4383 {
4384 if (
Patrick Scott5f6bd842010-06-28 16:55:16 -04004385#ifdef PNG_READ_EXPAND_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07004386 (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) ||
The Android Open Source Project893912b2009-03-03 19:30:05 -08004387#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04004388#ifdef PNG_READ_FILLER_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07004389 (png_ptr->transformations & (PNG_FILLER)) ||
The Android Open Source Project893912b2009-03-03 19:30:05 -08004390#endif
Chris Craikb50c2172013-07-29 15:28:30 -07004391 png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
The Android Open Source Project893912b2009-03-03 19:30:05 -08004392 {
4393 if (max_pixel_depth <= 16)
4394 max_pixel_depth = 32;
Chris Craikb50c2172013-07-29 15:28:30 -07004395
The Android Open Source Project893912b2009-03-03 19:30:05 -08004396 else
4397 max_pixel_depth = 64;
4398 }
Chris Craikb50c2172013-07-29 15:28:30 -07004399
The Android Open Source Project893912b2009-03-03 19:30:05 -08004400 else
4401 {
4402 if (max_pixel_depth <= 8)
Chris Craikb50c2172013-07-29 15:28:30 -07004403 {
4404 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
The Android Open Source Project893912b2009-03-03 19:30:05 -08004405 max_pixel_depth = 32;
Chris Craikb50c2172013-07-29 15:28:30 -07004406
4407 else
The Android Open Source Project893912b2009-03-03 19:30:05 -08004408 max_pixel_depth = 24;
Chris Craikb50c2172013-07-29 15:28:30 -07004409 }
4410
The Android Open Source Project893912b2009-03-03 19:30:05 -08004411 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4412 max_pixel_depth = 64;
Chris Craikb50c2172013-07-29 15:28:30 -07004413
The Android Open Source Project893912b2009-03-03 19:30:05 -08004414 else
4415 max_pixel_depth = 48;
4416 }
4417 }
4418#endif
4419
4420#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
4421defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
The Android Open Source Project4215dd12009-03-09 11:52:12 -07004422 if (png_ptr->transformations & PNG_USER_TRANSFORM)
Chris Craikb50c2172013-07-29 15:28:30 -07004423 {
4424 int user_pixel_depth = png_ptr->user_transform_depth *
The Android Open Source Project893912b2009-03-03 19:30:05 -08004425 png_ptr->user_transform_channels;
Chris Craikb50c2172013-07-29 15:28:30 -07004426
4427 if (user_pixel_depth > max_pixel_depth)
4428 max_pixel_depth = user_pixel_depth;
4429 }
The Android Open Source Project893912b2009-03-03 19:30:05 -08004430#endif
4431
Chris Craikb50c2172013-07-29 15:28:30 -07004432 /* This value is stored in png_struct and double checked in the row read
4433 * code.
4434 */
4435 png_ptr->maximum_pixel_depth = (png_byte)max_pixel_depth;
4436 png_ptr->transformed_pixel_depth = 0; /* calculated on demand */
4437
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004438 /* Align the width on the next larger 8 pixels. Mainly used
4439 * for interlacing
4440 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08004441 row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004442 /* Calculate the maximum bytes needed, adding a byte and a pixel
4443 * for safety's sake
4444 */
The Android Open Source Project4215dd12009-03-09 11:52:12 -07004445 row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) +
Chris Craikb50c2172013-07-29 15:28:30 -07004446 1 + ((max_pixel_depth + 7) >> 3);
4447
The Android Open Source Project893912b2009-03-03 19:30:05 -08004448#ifdef PNG_MAX_MALLOC_64K
4449 if (row_bytes > (png_uint_32)65536L)
4450 png_error(png_ptr, "This image requires a row greater than 64KB");
4451#endif
4452
Chris Craikb50c2172013-07-29 15:28:30 -07004453 if (row_bytes + 48 > png_ptr->old_big_row_buf_size)
The Android Open Source Project893912b2009-03-03 19:30:05 -08004454 {
The Android Open Source Project4215dd12009-03-09 11:52:12 -07004455 png_free(png_ptr, png_ptr->big_row_buf);
Chris Craikb50c2172013-07-29 15:28:30 -07004456 png_free(png_ptr, png_ptr->big_prev_row);
4457
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004458 if (png_ptr->interlaced)
Patrick Scott5f6bd842010-06-28 16:55:16 -04004459 png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr,
Chris Craikb50c2172013-07-29 15:28:30 -07004460 row_bytes + 48);
Patrick Scott5f6bd842010-06-28 16:55:16 -04004461
Chris Craikb50c2172013-07-29 15:28:30 -07004462 else
4463 png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
4464
4465 png_ptr->big_prev_row = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
4466
4467#ifdef PNG_ALIGNED_MEMORY_SUPPORTED
4468 /* Use 16-byte aligned memory for row_buf with at least 16 bytes
4469 * of padding before and after row_buf; treat prev_row similarly.
4470 * NOTE: the alignment is to the start of the pixels, one beyond the start
4471 * of the buffer, because of the filter byte. Prior to libpng 1.5.6 this
4472 * was incorrect; the filter byte was aligned, which had the exact
4473 * opposite effect of that intended.
4474 */
4475 {
4476 png_bytep temp = png_ptr->big_row_buf + 32;
4477 int extra = (int)((temp - (png_bytep)0) & 0x0f);
4478 png_ptr->row_buf = temp - extra - 1/*filter byte*/;
4479
4480 temp = png_ptr->big_prev_row + 32;
4481 extra = (int)((temp - (png_bytep)0) & 0x0f);
4482 png_ptr->prev_row = temp - extra - 1/*filter byte*/;
4483 }
4484
4485#else
4486 /* Use 31 bytes of padding before and 17 bytes after row_buf. */
4487 png_ptr->row_buf = png_ptr->big_row_buf + 31;
4488 png_ptr->prev_row = png_ptr->big_prev_row + 31;
4489#endif
4490 png_ptr->old_big_row_buf_size = row_bytes + 48;
The Android Open Source Project893912b2009-03-03 19:30:05 -08004491 }
4492
4493#ifdef PNG_MAX_MALLOC_64K
Chris Craikb50c2172013-07-29 15:28:30 -07004494 if (png_ptr->rowbytes > 65535)
The Android Open Source Project893912b2009-03-03 19:30:05 -08004495 png_error(png_ptr, "This image requires a row greater than 64KB");
The Android Open Source Project893912b2009-03-03 19:30:05 -08004496
Chris Craikb50c2172013-07-29 15:28:30 -07004497#endif
4498 if (png_ptr->rowbytes > (PNG_SIZE_MAX - 1))
4499 png_error(png_ptr, "Row has too many bytes to allocate in memory");
4500
4501 memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
4502
4503 png_debug1(3, "width = %u,", png_ptr->width);
4504 png_debug1(3, "height = %u,", png_ptr->height);
4505 png_debug1(3, "iwidth = %u,", png_ptr->iwidth);
4506 png_debug1(3, "num_rows = %u,", png_ptr->num_rows);
4507 png_debug1(3, "rowbytes = %lu,", (unsigned long)png_ptr->rowbytes);
4508 png_debug1(3, "irowbytes = %lu",
4509 (unsigned long)PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1);
4510
4511 /* The sequential reader needs a buffer for IDAT, but the progressive reader
4512 * does not, so free the read buffer now regardless; the sequential reader
4513 * reallocates it on demand.
4514 */
4515 if (png_ptr->read_buffer)
The Android Open Source Project893912b2009-03-03 19:30:05 -08004516 {
Chris Craikb50c2172013-07-29 15:28:30 -07004517 png_bytep buffer = png_ptr->read_buffer;
4518
4519 png_ptr->read_buffer_size = 0;
4520 png_ptr->read_buffer = NULL;
4521 png_free(png_ptr, buffer);
The Android Open Source Project893912b2009-03-03 19:30:05 -08004522 }
4523
Chris Craikb50c2172013-07-29 15:28:30 -07004524 /* Finally claim the zstream for the inflate of the IDAT data, use the bits
4525 * value from the stream (note that this will result in a fatal error if the
4526 * IDAT stream has a bogus deflate header window_bits value, but this should
4527 * not be happening any longer!)
4528 */
4529 if (png_inflate_claim(png_ptr, png_IDAT) != Z_OK)
4530 png_error(png_ptr, png_ptr->zstream.msg);
The Android Open Source Project893912b2009-03-03 19:30:05 -08004531
4532 png_ptr->flags |= PNG_FLAG_ROW_INIT;
4533}
4534#endif /* PNG_READ_SUPPORTED */