blob: dac1a57a7929d06ab5935ec9fb50c5f797cff23a [file] [log] [blame]
The Android Open Source Project893912b2009-03-03 19:30:05 -08001
2/* pngrutil.c - utilities to read a PNG file
3 *
Chris Craikb50c2172013-07-29 15:28:30 -07004 * Last changed in libpng 1.6.3 [July 18, 2013]
5 * Copyright (c) 1998-2013 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
230 {
Chris Craikb50c2172013-07-29 15:28:30 -0700231 png_chunk_benign_error(png_ptr, "CRC error");
232 return (0);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800233 }
Chris Craikb50c2172013-07-29 15:28:30 -0700234
The Android Open Source Project893912b2009-03-03 19:30:05 -0800235 return (1);
236 }
237
238 return (0);
239}
240
John Reck90bf0d22013-08-14 10:37:42 -0700241#ifdef PNG_INDEX_SUPPORTED
242int /* PRIVATE */
243png_opt_crc_finish(png_structrp png_ptr, png_uint_32 skip)
244{
245 while (skip > 0)
246 {
247 png_uint_32 len;
248 png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];
249
250 len = (sizeof tmpbuf);
251 if (len > skip)
252 len = skip;
253 skip -= len;
254
255 png_crc_read(png_ptr, tmpbuf, len);
256 }
257
258 if (png_crc_error(png_ptr))
259 {
260 png_chunk_warning(png_ptr, "CRC error");
261 return (1);
262 }
263
264 return (0);
265}
266#endif
267
The Android Open Source Project893912b2009-03-03 19:30:05 -0800268/* Compare the CRC stored in the PNG file with that calculated by libpng from
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400269 * the data it has read thus far.
270 */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800271int /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -0700272png_crc_error(png_structrp png_ptr)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800273{
274 png_byte crc_bytes[4];
275 png_uint_32 crc;
276 int need_crc = 1;
277
Chris Craikb50c2172013-07-29 15:28:30 -0700278 if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name))
The Android Open Source Project893912b2009-03-03 19:30:05 -0800279 {
280 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
281 (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
282 need_crc = 0;
283 }
Chris Craikb50c2172013-07-29 15:28:30 -0700284
285 else /* critical */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800286 {
287 if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
288 need_crc = 0;
289 }
290
Chris Craikb50c2172013-07-29 15:28:30 -0700291#ifdef PNG_IO_STATE_SUPPORTED
292 png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_CRC;
293#endif
294
295 /* The chunk CRC must be serialized in a single I/O call. */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800296 png_read_data(png_ptr, crc_bytes, 4);
297
298 if (need_crc)
299 {
300 crc = png_get_uint_32(crc_bytes);
301 return ((int)(crc != png_ptr->crc));
302 }
Chris Craikb50c2172013-07-29 15:28:30 -0700303
The Android Open Source Project893912b2009-03-03 19:30:05 -0800304 else
305 return (0);
306}
307
Chris Craikb50c2172013-07-29 15:28:30 -0700308/* Manage the read buffer; this simply reallocates the buffer if it is not small
309 * enough (or if it is not allocated). The routine returns a pointer to the
310 * buffer; if an error occurs and 'warn' is set the routine returns NULL, else
311 * it will call png_error (via png_malloc) on failure. (warn == 2 means
312 * 'silent').
313 */
314static png_bytep
315png_read_buffer(png_structrp png_ptr, png_alloc_size_t new_size, int warn)
Patrick Scott5f6bd842010-06-28 16:55:16 -0400316{
Chris Craikb50c2172013-07-29 15:28:30 -0700317 png_bytep buffer = png_ptr->read_buffer;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400318
Chris Craikb50c2172013-07-29 15:28:30 -0700319 if (buffer != NULL && new_size > png_ptr->read_buffer_size)
Patrick Scott5f6bd842010-06-28 16:55:16 -0400320 {
Chris Craikb50c2172013-07-29 15:28:30 -0700321 png_ptr->read_buffer = NULL;
322 png_ptr->read_buffer = NULL;
323 png_ptr->read_buffer_size = 0;
324 png_free(png_ptr, buffer);
325 buffer = NULL;
326 }
Patrick Scott5f6bd842010-06-28 16:55:16 -0400327
Chris Craikb50c2172013-07-29 15:28:30 -0700328 if (buffer == NULL)
329 {
330 buffer = png_voidcast(png_bytep, png_malloc_base(png_ptr, new_size));
Patrick Scott5f6bd842010-06-28 16:55:16 -0400331
Chris Craikb50c2172013-07-29 15:28:30 -0700332 if (buffer != NULL)
Patrick Scott5f6bd842010-06-28 16:55:16 -0400333 {
Chris Craikb50c2172013-07-29 15:28:30 -0700334 png_ptr->read_buffer = buffer;
335 png_ptr->read_buffer_size = new_size;
336 }
337
338 else if (warn < 2) /* else silent */
339 {
340#ifdef PNG_WARNINGS_SUPPORTED
341 if (warn)
342 png_chunk_warning(png_ptr, "insufficient memory to read chunk");
343 else
344#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -0400345 {
Chris Craikb50c2172013-07-29 15:28:30 -0700346#ifdef PNG_ERROR_TEXT_SUPPORTED
347 png_chunk_error(png_ptr, "insufficient memory to read chunk");
348#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -0400349 }
Chris Craikb50c2172013-07-29 15:28:30 -0700350 }
351 }
352
353 return buffer;
354}
355
356/* png_inflate_claim: claim the zstream for some nefarious purpose that involves
357 * decompression. Returns Z_OK on success, else a zlib error code. It checks
358 * the owner but, in final release builds, just issues a warning if some other
359 * chunk apparently owns the stream. Prior to release it does a png_error.
360 */
361static int
362png_inflate_claim(png_structrp png_ptr, png_uint_32 owner)
363{
364 if (png_ptr->zowner != 0)
365 {
366 char msg[64];
367
368 PNG_STRING_FROM_CHUNK(msg, png_ptr->zowner);
369 /* So the message that results is "<chunk> using zstream"; this is an
370 * internal error, but is very useful for debugging. i18n requirements
371 * are minimal.
372 */
373 (void)png_safecat(msg, (sizeof msg), 4, " using zstream");
374# if PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC
375 png_chunk_warning(png_ptr, msg);
376 png_ptr->zowner = 0;
377# else
378 png_chunk_error(png_ptr, msg);
379# endif
380 }
381
382 /* Implementation note: unlike 'png_deflate_claim' this internal function
383 * does not take the size of the data as an argument. Some efficiency could
384 * be gained by using this when it is known *if* the zlib stream itself does
385 * not record the number; however, this is an illusion: the original writer
386 * of the PNG may have selected a lower window size, and we really must
387 * follow that because, for systems with with limited capabilities, we
388 * would otherwise reject the application's attempts to use a smaller window
389 * size (zlib doesn't have an interface to say "this or lower"!).
390 *
391 * inflateReset2 was added to zlib 1.2.4; before this the window could not be
392 * reset, therefore it is necessary to always allocate the maximum window
393 * size with earlier zlibs just in case later compressed chunks need it.
394 */
395 {
396 int ret; /* zlib return code */
397# if PNG_ZLIB_VERNUM >= 0x1240
398
399# if defined(PNG_SET_OPTION_SUPPORTED) && \
400 defined(PNG_MAXIMUM_INFLATE_WINDOW)
401 int window_bits;
402
403 if (((png_ptr->options >> PNG_MAXIMUM_INFLATE_WINDOW) & 3) ==
404 PNG_OPTION_ON)
405 window_bits = 15;
406
407 else
408 window_bits = 0;
409# else
410# define window_bits 0
411# endif
412# endif
413
414 /* Set this for safety, just in case the previous owner left pointers to
415 * memory allocations.
416 */
417 png_ptr->zstream.next_in = NULL;
418 png_ptr->zstream.avail_in = 0;
419 png_ptr->zstream.next_out = NULL;
420 png_ptr->zstream.avail_out = 0;
421
422 if (png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED)
423 {
424# if PNG_ZLIB_VERNUM < 0x1240
425 ret = inflateReset(&png_ptr->zstream);
426# else
427 ret = inflateReset2(&png_ptr->zstream, window_bits);
428# endif
429 }
430
431 else
432 {
433# if PNG_ZLIB_VERNUM < 0x1240
434 ret = inflateInit(&png_ptr->zstream);
435# else
436 ret = inflateInit2(&png_ptr->zstream, window_bits);
437# endif
438
439 if (ret == Z_OK)
440 png_ptr->flags |= PNG_FLAG_ZSTREAM_INITIALIZED;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400441 }
442
443 if (ret == Z_OK)
Chris Craikb50c2172013-07-29 15:28:30 -0700444 png_ptr->zowner = owner;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400445
Chris Craikb50c2172013-07-29 15:28:30 -0700446 else
447 png_zstream_error(png_ptr, ret);
448
449 return ret;
450 }
451
452# ifdef window_bits
453# undef window_bits
454# endif
455}
456
457#ifdef PNG_READ_COMPRESSED_TEXT_SUPPORTED
458/* png_inflate now returns zlib error codes including Z_OK and Z_STREAM_END to
459 * allow the caller to do multiple calls if required. If the 'finish' flag is
460 * set Z_FINISH will be passed to the final inflate() call and Z_STREAM_END must
461 * be returned or there has been a problem, otherwise Z_SYNC_FLUSH is used and
462 * Z_OK or Z_STREAM_END will be returned on success.
463 *
464 * The input and output sizes are updated to the actual amounts of data consumed
465 * or written, not the amount available (as in a z_stream). The data pointers
466 * are not changed, so the next input is (data+input_size) and the next
467 * available output is (output+output_size).
468 */
469static int
470png_inflate(png_structrp png_ptr, png_uint_32 owner, int finish,
471 /* INPUT: */ png_const_bytep input, png_uint_32p input_size_ptr,
472 /* OUTPUT: */ png_bytep output, png_alloc_size_t *output_size_ptr)
473{
474 if (png_ptr->zowner == owner) /* Else not claimed */
475 {
476 int ret;
477 png_alloc_size_t avail_out = *output_size_ptr;
478 png_uint_32 avail_in = *input_size_ptr;
479
480 /* zlib can't necessarily handle more than 65535 bytes at once (i.e. it
481 * can't even necessarily handle 65536 bytes) because the type uInt is
482 * "16 bits or more". Consequently it is necessary to chunk the input to
483 * zlib. This code uses ZLIB_IO_MAX, from pngpriv.h, as the maximum (the
484 * maximum value that can be stored in a uInt.) It is possible to set
485 * ZLIB_IO_MAX to a lower value in pngpriv.h and this may sometimes have
486 * a performance advantage, because it reduces the amount of data accessed
487 * at each step and that may give the OS more time to page it in.
Patrick Scott5f6bd842010-06-28 16:55:16 -0400488 */
Chris Craikb50c2172013-07-29 15:28:30 -0700489 png_ptr->zstream.next_in = PNGZ_INPUT_CAST(input);
490 /* avail_in and avail_out are set below from 'size' */
Patrick Scott5f6bd842010-06-28 16:55:16 -0400491 png_ptr->zstream.avail_in = 0;
Chris Craikb50c2172013-07-29 15:28:30 -0700492 png_ptr->zstream.avail_out = 0;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400493
Chris Craikb50c2172013-07-29 15:28:30 -0700494 /* Read directly into the output if it is available (this is set to
495 * a local buffer below if output is NULL).
Patrick Scott5f6bd842010-06-28 16:55:16 -0400496 */
Chris Craikb50c2172013-07-29 15:28:30 -0700497 if (output != NULL)
498 png_ptr->zstream.next_out = output;
499
500 do
Patrick Scott5f6bd842010-06-28 16:55:16 -0400501 {
Chris Craikb50c2172013-07-29 15:28:30 -0700502 uInt avail;
503 Byte local_buffer[PNG_INFLATE_BUF_SIZE];
504
505 /* zlib INPUT BUFFER */
506 /* The setting of 'avail_in' used to be outside the loop; by setting it
507 * inside it is possible to chunk the input to zlib and simply rely on
508 * zlib to advance the 'next_in' pointer. This allows arbitrary
509 * amounts of data to be passed through zlib at the unavoidable cost of
510 * requiring a window save (memcpy of up to 32768 output bytes)
511 * every ZLIB_IO_MAX input bytes.
512 */
513 avail_in += png_ptr->zstream.avail_in; /* not consumed last time */
514
515 avail = ZLIB_IO_MAX;
516
517 if (avail_in < avail)
518 avail = (uInt)avail_in; /* safe: < than ZLIB_IO_MAX */
519
520 avail_in -= avail;
521 png_ptr->zstream.avail_in = avail;
522
523 /* zlib OUTPUT BUFFER */
524 avail_out += png_ptr->zstream.avail_out; /* not written last time */
525
526 avail = ZLIB_IO_MAX; /* maximum zlib can process */
527
528 if (output == NULL)
Patrick Scott5f6bd842010-06-28 16:55:16 -0400529 {
Chris Craikb50c2172013-07-29 15:28:30 -0700530 /* Reset the output buffer each time round if output is NULL and
531 * make available the full buffer, up to 'remaining_space'
532 */
533 png_ptr->zstream.next_out = local_buffer;
534 if ((sizeof local_buffer) < avail)
535 avail = (sizeof local_buffer);
Patrick Scott5f6bd842010-06-28 16:55:16 -0400536 }
537
Chris Craikb50c2172013-07-29 15:28:30 -0700538 if (avail_out < avail)
539 avail = (uInt)avail_out; /* safe: < ZLIB_IO_MAX */
Patrick Scott5f6bd842010-06-28 16:55:16 -0400540
Chris Craikb50c2172013-07-29 15:28:30 -0700541 png_ptr->zstream.avail_out = avail;
542 avail_out -= avail;
543
544 /* zlib inflate call */
545 /* In fact 'avail_out' may be 0 at this point, that happens at the end
546 * of the read when the final LZ end code was not passed at the end of
547 * the previous chunk of input data. Tell zlib if we have reached the
548 * end of the output buffer.
549 */
550 ret = inflate(&png_ptr->zstream, avail_out > 0 ? Z_NO_FLUSH :
551 (finish ? Z_FINISH : Z_SYNC_FLUSH));
552 } while (ret == Z_OK);
553
554 /* For safety kill the local buffer pointer now */
555 if (output == NULL)
556 png_ptr->zstream.next_out = NULL;
557
558 /* Claw back the 'size' and 'remaining_space' byte counts. */
559 avail_in += png_ptr->zstream.avail_in;
560 avail_out += png_ptr->zstream.avail_out;
561
562 /* Update the input and output sizes; the updated values are the amount
563 * consumed or written, effectively the inverse of what zlib uses.
Patrick Scott5f6bd842010-06-28 16:55:16 -0400564 */
Chris Craikb50c2172013-07-29 15:28:30 -0700565 if (avail_out > 0)
566 *output_size_ptr -= avail_out;
567
568 if (avail_in > 0)
569 *input_size_ptr -= avail_in;
570
571 /* Ensure png_ptr->zstream.msg is set (even in the success case!) */
572 png_zstream_error(png_ptr, ret);
573 return ret;
574 }
575
576 else
577 {
578 /* This is a bad internal error. The recovery assigns to the zstream msg
579 * pointer, which is not owned by the caller, but this is safe; it's only
580 * used on errors!
581 */
582 png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed");
583 return Z_STREAM_ERROR;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400584 }
585}
586
The Android Open Source Project893912b2009-03-03 19:30:05 -0800587/*
Chris Craikb50c2172013-07-29 15:28:30 -0700588 * Decompress trailing data in a chunk. The assumption is that read_buffer
The Android Open Source Project893912b2009-03-03 19:30:05 -0800589 * points at an allocated area holding the contents of a chunk with a
590 * trailing compressed part. What we get back is an allocated area
591 * holding the original prefix part and an uncompressed version of the
592 * trailing part (the malloc area passed in is freed).
593 */
Chris Craikb50c2172013-07-29 15:28:30 -0700594static int
595png_decompress_chunk(png_structrp png_ptr,
596 png_uint_32 chunklength, png_uint_32 prefix_size,
597 png_alloc_size_t *newlength /* must be initialized to the maximum! */,
598 int terminate /*add a '\0' to the end of the uncompressed data*/)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800599{
Chris Craikb50c2172013-07-29 15:28:30 -0700600 /* TODO: implement different limits for different types of chunk.
601 *
602 * The caller supplies *newlength set to the maximum length of the
603 * uncompressed data, but this routine allocates space for the prefix and
604 * maybe a '\0' terminator too. We have to assume that 'prefix_size' is
605 * limited only by the maximum chunk size.
606 */
607 png_alloc_size_t limit = PNG_SIZE_MAX;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400608
Chris Craikb50c2172013-07-29 15:28:30 -0700609# ifdef PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED
610 if (png_ptr->user_chunk_malloc_max > 0 &&
611 png_ptr->user_chunk_malloc_max < limit)
612 limit = png_ptr->user_chunk_malloc_max;
613# elif PNG_USER_CHUNK_MALLOC_MAX > 0
614 if (PNG_USER_CHUNK_MALLOC_MAX < limit)
615 limit = PNG_USER_CHUNK_MALLOC_MAX;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400616# endif
Patrick Scott5f6bd842010-06-28 16:55:16 -0400617
Chris Craikb50c2172013-07-29 15:28:30 -0700618 if (limit >= prefix_size + (terminate != 0))
619 {
620 int ret;
621
622 limit -= prefix_size + (terminate != 0);
623
624 if (limit < *newlength)
625 *newlength = limit;
626
627 /* Now try to claim the stream. */
628 ret = png_inflate_claim(png_ptr, png_ptr->chunk_name);
629
630 if (ret == Z_OK)
Patrick Scott5f6bd842010-06-28 16:55:16 -0400631 {
Chris Craikb50c2172013-07-29 15:28:30 -0700632 png_uint_32 lzsize = chunklength - prefix_size;
Dave Burkeccee1212012-03-05 22:36:13 -0800633
Chris Craikb50c2172013-07-29 15:28:30 -0700634 ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/,
635 /* input: */ png_ptr->read_buffer + prefix_size, &lzsize,
636 /* output: */ NULL, newlength);
637
638 if (ret == Z_STREAM_END)
Dave Burkeccee1212012-03-05 22:36:13 -0800639 {
Chris Craikb50c2172013-07-29 15:28:30 -0700640 /* Use 'inflateReset' here, not 'inflateReset2' because this
641 * preserves the previously decided window size (otherwise it would
642 * be necessary to store the previous window size.) In practice
643 * this doesn't matter anyway, because png_inflate will call inflate
644 * with Z_FINISH in almost all cases, so the window will not be
645 * maintained.
646 */
647 if (inflateReset(&png_ptr->zstream) == Z_OK)
Patrick Scott5f6bd842010-06-28 16:55:16 -0400648 {
Chris Craikb50c2172013-07-29 15:28:30 -0700649 /* Because of the limit checks above we know that the new,
650 * expanded, size will fit in a size_t (let alone an
651 * png_alloc_size_t). Use png_malloc_base here to avoid an
652 * extra OOM message.
653 */
654 png_alloc_size_t new_size = *newlength;
655 png_alloc_size_t buffer_size = prefix_size + new_size +
656 (terminate != 0);
657 png_bytep text = png_voidcast(png_bytep, png_malloc_base(png_ptr,
658 buffer_size));
659
660 if (text != NULL)
661 {
662 ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/,
663 png_ptr->read_buffer + prefix_size, &lzsize,
664 text + prefix_size, newlength);
665
666 if (ret == Z_STREAM_END)
667 {
668 if (new_size == *newlength)
669 {
670 if (terminate)
671 text[prefix_size + *newlength] = 0;
672
673 if (prefix_size > 0)
674 memcpy(text, png_ptr->read_buffer, prefix_size);
675
676 {
677 png_bytep old_ptr = png_ptr->read_buffer;
678
679 png_ptr->read_buffer = text;
680 png_ptr->read_buffer_size = buffer_size;
681 text = old_ptr; /* freed below */
682 }
683 }
684
685 else
686 {
687 /* The size changed on the second read, there can be no
688 * guarantee that anything is correct at this point.
689 * The 'msg' pointer has been set to "unexpected end of
690 * LZ stream", which is fine, but return an error code
691 * that the caller won't accept.
692 */
693 ret = PNG_UNEXPECTED_ZLIB_RETURN;
694 }
695 }
696
697 else if (ret == Z_OK)
698 ret = PNG_UNEXPECTED_ZLIB_RETURN; /* for safety */
699
700 /* Free the text pointer (this is the old read_buffer on
701 * success)
702 */
703 png_free(png_ptr, text);
704
705 /* This really is very benign, but it's still an error because
706 * the extra space may otherwise be used as a Trojan Horse.
707 */
708 if (ret == Z_STREAM_END &&
709 chunklength - prefix_size != lzsize)
710 png_chunk_benign_error(png_ptr, "extra compressed data");
711 }
712
713 else
714 {
715 /* Out of memory allocating the buffer */
716 ret = Z_MEM_ERROR;
717 png_zstream_error(png_ptr, Z_MEM_ERROR);
718 }
Patrick Scott5f6bd842010-06-28 16:55:16 -0400719 }
720
Chris Craikb50c2172013-07-29 15:28:30 -0700721 else
722 {
723 /* inflateReset failed, store the error message */
724 png_zstream_error(png_ptr, ret);
725
726 if (ret == Z_STREAM_END)
727 ret = PNG_UNEXPECTED_ZLIB_RETURN;
728 }
Patrick Scott5f6bd842010-06-28 16:55:16 -0400729 }
Chris Craikb50c2172013-07-29 15:28:30 -0700730
731 else if (ret == Z_OK)
732 ret = PNG_UNEXPECTED_ZLIB_RETURN;
733
734 /* Release the claimed stream */
735 png_ptr->zowner = 0;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400736 }
Chris Craikb50c2172013-07-29 15:28:30 -0700737
738 else /* the claim failed */ if (ret == Z_STREAM_END) /* impossible! */
739 ret = PNG_UNEXPECTED_ZLIB_RETURN;
740
741 return ret;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400742 }
743
Chris Craikb50c2172013-07-29 15:28:30 -0700744 else
The Android Open Source Project893912b2009-03-03 19:30:05 -0800745 {
Chris Craikb50c2172013-07-29 15:28:30 -0700746 /* Application/configuration limits exceeded */
747 png_zstream_error(png_ptr, Z_MEM_ERROR);
748 return Z_MEM_ERROR;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800749 }
Chris Craikb50c2172013-07-29 15:28:30 -0700750}
751#endif /* PNG_READ_COMPRESSED_TEXT_SUPPORTED */
Patrick Scott5f6bd842010-06-28 16:55:16 -0400752
Chris Craikb50c2172013-07-29 15:28:30 -0700753#ifdef PNG_READ_iCCP_SUPPORTED
754/* Perform a partial read and decompress, producing 'avail_out' bytes and
755 * reading from the current chunk as required.
756 */
757static int
758png_inflate_read(png_structrp png_ptr, png_bytep read_buffer, uInt read_size,
759 png_uint_32p chunk_bytes, png_bytep next_out, png_alloc_size_t *out_size,
760 int finish)
761{
762 if (png_ptr->zowner == png_ptr->chunk_name)
Patrick Scott5f6bd842010-06-28 16:55:16 -0400763 {
Chris Craikb50c2172013-07-29 15:28:30 -0700764 int ret;
765
766 /* next_in and avail_in must have been initialized by the caller. */
767 png_ptr->zstream.next_out = next_out;
768 png_ptr->zstream.avail_out = 0; /* set in the loop */
769
770 do
Patrick Scott5f6bd842010-06-28 16:55:16 -0400771 {
Chris Craikb50c2172013-07-29 15:28:30 -0700772 if (png_ptr->zstream.avail_in == 0)
773 {
774 if (read_size > *chunk_bytes)
775 read_size = (uInt)*chunk_bytes;
776 *chunk_bytes -= read_size;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400777
Chris Craikb50c2172013-07-29 15:28:30 -0700778 if (read_size > 0)
779 png_crc_read(png_ptr, read_buffer, read_size);
780
781 png_ptr->zstream.next_in = read_buffer;
782 png_ptr->zstream.avail_in = read_size;
783 }
784
785 if (png_ptr->zstream.avail_out == 0)
786 {
787 uInt avail = ZLIB_IO_MAX;
788 if (avail > *out_size)
789 avail = (uInt)*out_size;
790 *out_size -= avail;
791
792 png_ptr->zstream.avail_out = avail;
793 }
794
795 /* Use Z_SYNC_FLUSH when there is no more chunk data to ensure that all
796 * the available output is produced; this allows reading of truncated
797 * streams.
798 */
799 ret = inflate(&png_ptr->zstream,
800 *chunk_bytes > 0 ? Z_NO_FLUSH : (finish ? Z_FINISH : Z_SYNC_FLUSH));
Patrick Scott5f6bd842010-06-28 16:55:16 -0400801 }
Chris Craikb50c2172013-07-29 15:28:30 -0700802 while (ret == Z_OK && (*out_size > 0 || png_ptr->zstream.avail_out > 0));
803
804 *out_size += png_ptr->zstream.avail_out;
805 png_ptr->zstream.avail_out = 0; /* Should not be required, but is safe */
806
807 /* Ensure the error message pointer is always set: */
808 png_zstream_error(png_ptr, ret);
809 return ret;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400810 }
811
Chris Craikb50c2172013-07-29 15:28:30 -0700812 else
813 {
814 png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed");
815 return Z_STREAM_ERROR;
816 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800817}
818#endif
819
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400820/* Read and check the IDHR chunk */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800821void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -0700822png_handle_IHDR(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800823{
824 png_byte buf[13];
825 png_uint_32 width, height;
826 int bit_depth, color_type, compression_type, filter_type;
827 int interlace_type;
828
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700829 png_debug(1, "in png_handle_IHDR");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800830
831 if (png_ptr->mode & PNG_HAVE_IHDR)
Chris Craikb50c2172013-07-29 15:28:30 -0700832 png_chunk_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800833
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400834 /* Check the length */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800835 if (length != 13)
Chris Craikb50c2172013-07-29 15:28:30 -0700836 png_chunk_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800837
838 png_ptr->mode |= PNG_HAVE_IHDR;
839
840 png_crc_read(png_ptr, buf, 13);
841 png_crc_finish(png_ptr, 0);
842
843 width = png_get_uint_31(png_ptr, buf);
844 height = png_get_uint_31(png_ptr, buf + 4);
845 bit_depth = buf[8];
846 color_type = buf[9];
847 compression_type = buf[10];
848 filter_type = buf[11];
849 interlace_type = buf[12];
850
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400851 /* Set internal variables */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800852 png_ptr->width = width;
853 png_ptr->height = height;
854 png_ptr->bit_depth = (png_byte)bit_depth;
855 png_ptr->interlaced = (png_byte)interlace_type;
856 png_ptr->color_type = (png_byte)color_type;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400857#ifdef PNG_MNG_FEATURES_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800858 png_ptr->filter_type = (png_byte)filter_type;
859#endif
860 png_ptr->compression_type = (png_byte)compression_type;
861
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400862 /* Find number of channels */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800863 switch (png_ptr->color_type)
864 {
Chris Craikb50c2172013-07-29 15:28:30 -0700865 default: /* invalid, png_set_IHDR calls png_error */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800866 case PNG_COLOR_TYPE_GRAY:
867 case PNG_COLOR_TYPE_PALETTE:
868 png_ptr->channels = 1;
869 break;
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400870
The Android Open Source Project893912b2009-03-03 19:30:05 -0800871 case PNG_COLOR_TYPE_RGB:
872 png_ptr->channels = 3;
873 break;
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400874
The Android Open Source Project893912b2009-03-03 19:30:05 -0800875 case PNG_COLOR_TYPE_GRAY_ALPHA:
876 png_ptr->channels = 2;
877 break;
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400878
The Android Open Source Project893912b2009-03-03 19:30:05 -0800879 case PNG_COLOR_TYPE_RGB_ALPHA:
880 png_ptr->channels = 4;
881 break;
882 }
883
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400884 /* Set up other useful info */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800885 png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth *
886 png_ptr->channels);
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700887 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width);
888 png_debug1(3, "bit_depth = %d", png_ptr->bit_depth);
889 png_debug1(3, "channels = %d", png_ptr->channels);
Chris Craikb50c2172013-07-29 15:28:30 -0700890 png_debug1(3, "rowbytes = %lu", (unsigned long)png_ptr->rowbytes);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800891 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
Chris Craikb50c2172013-07-29 15:28:30 -0700892 color_type, interlace_type, compression_type, filter_type);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800893}
894
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400895/* Read and check the palette */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800896void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -0700897png_handle_PLTE(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800898{
899 png_color palette[PNG_MAX_PALETTE_LENGTH];
900 int num, i;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400901#ifdef PNG_POINTER_INDEXING_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800902 png_colorp pal_ptr;
903#endif
904
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700905 png_debug(1, "in png_handle_PLTE");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800906
907 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Chris Craikb50c2172013-07-29 15:28:30 -0700908 png_chunk_error(png_ptr, "missing IHDR");
909
910 /* Moved to before the 'after IDAT' check below because otherwise duplicate
911 * PLTE chunks are potentially ignored (the spec says there shall not be more
912 * than one PLTE, the error is not treated as benign, so this check trumps
913 * the requirement that PLTE appears before IDAT.)
914 */
915 else if (png_ptr->mode & PNG_HAVE_PLTE)
916 png_chunk_error(png_ptr, "duplicate");
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400917
The Android Open Source Project893912b2009-03-03 19:30:05 -0800918 else if (png_ptr->mode & PNG_HAVE_IDAT)
919 {
Chris Craikb50c2172013-07-29 15:28:30 -0700920 /* This is benign because the non-benign error happened before, when an
921 * IDAT was encountered in a color-mapped image with no PLTE.
922 */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800923 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -0700924 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800925 return;
926 }
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400927
The Android Open Source Project893912b2009-03-03 19:30:05 -0800928 png_ptr->mode |= PNG_HAVE_PLTE;
929
Chris Craikb50c2172013-07-29 15:28:30 -0700930 if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR))
The Android Open Source Project893912b2009-03-03 19:30:05 -0800931 {
The Android Open Source Project893912b2009-03-03 19:30:05 -0800932 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -0700933 png_chunk_benign_error(png_ptr, "ignored in grayscale PNG");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800934 return;
935 }
Chris Craikb50c2172013-07-29 15:28:30 -0700936
Patrick Scott5f6bd842010-06-28 16:55:16 -0400937#ifndef PNG_READ_OPT_PLTE_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800938 if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
939 {
940 png_crc_finish(png_ptr, length);
941 return;
942 }
943#endif
944
945 if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3)
946 {
Chris Craikb50c2172013-07-29 15:28:30 -0700947 png_crc_finish(png_ptr, length);
948
The Android Open Source Project893912b2009-03-03 19:30:05 -0800949 if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
Chris Craikb50c2172013-07-29 15:28:30 -0700950 png_chunk_benign_error(png_ptr, "invalid");
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400951
The Android Open Source Project893912b2009-03-03 19:30:05 -0800952 else
Chris Craikb50c2172013-07-29 15:28:30 -0700953 png_chunk_error(png_ptr, "invalid");
954
955 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800956 }
957
Chris Craikb50c2172013-07-29 15:28:30 -0700958 /* The cast is safe because 'length' is less than 3*PNG_MAX_PALETTE_LENGTH */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800959 num = (int)length / 3;
960
Patrick Scott5f6bd842010-06-28 16:55:16 -0400961#ifdef PNG_POINTER_INDEXING_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800962 for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)
963 {
964 png_byte buf[3];
965
966 png_crc_read(png_ptr, buf, 3);
967 pal_ptr->red = buf[0];
968 pal_ptr->green = buf[1];
969 pal_ptr->blue = buf[2];
970 }
971#else
972 for (i = 0; i < num; i++)
973 {
974 png_byte buf[3];
975
976 png_crc_read(png_ptr, buf, 3);
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400977 /* Don't depend upon png_color being any order */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800978 palette[i].red = buf[0];
979 palette[i].green = buf[1];
980 palette[i].blue = buf[2];
981 }
982#endif
983
Chris Craikb50c2172013-07-29 15:28:30 -0700984 /* If we actually need the PLTE chunk (ie for a paletted image), we do
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400985 * whatever the normal CRC configuration tells us. However, if we
986 * have an RGB image, the PLTE can be considered ancillary, so
987 * we will act as though it is.
988 */
Patrick Scott5f6bd842010-06-28 16:55:16 -0400989#ifndef PNG_READ_OPT_PLTE_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800990 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
991#endif
992 {
993 png_crc_finish(png_ptr, 0);
994 }
Chris Craikb50c2172013-07-29 15:28:30 -0700995
Patrick Scott5f6bd842010-06-28 16:55:16 -0400996#ifndef PNG_READ_OPT_PLTE_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800997 else if (png_crc_error(png_ptr)) /* Only if we have a CRC error */
998 {
999 /* If we don't want to use the data from an ancillary chunk,
Chris Craikb50c2172013-07-29 15:28:30 -07001000 * we have two options: an error abort, or a warning and we
1001 * ignore the data in this chunk (which should be OK, since
1002 * it's considered ancillary for a RGB or RGBA image).
1003 *
1004 * IMPLEMENTATION NOTE: this is only here because png_crc_finish uses the
1005 * chunk type to determine whether to check the ancillary or the critical
1006 * flags.
1007 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001008 if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE))
1009 {
1010 if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)
1011 {
Chris Craikb50c2172013-07-29 15:28:30 -07001012 png_chunk_benign_error(png_ptr, "CRC error");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001013 }
Chris Craikb50c2172013-07-29 15:28:30 -07001014
The Android Open Source Project893912b2009-03-03 19:30:05 -08001015 else
1016 {
1017 png_chunk_warning(png_ptr, "CRC error");
1018 return;
1019 }
1020 }
Chris Craikb50c2172013-07-29 15:28:30 -07001021
The Android Open Source Project893912b2009-03-03 19:30:05 -08001022 /* Otherwise, we (optionally) emit a warning and use the chunk. */
1023 else if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN))
1024 {
1025 png_chunk_warning(png_ptr, "CRC error");
1026 }
1027 }
1028#endif
1029
Chris Craikb50c2172013-07-29 15:28:30 -07001030 /* TODO: png_set_PLTE has the side effect of setting png_ptr->palette to its
1031 * own copy of the palette. This has the side effect that when png_start_row
1032 * is called (this happens after any call to png_read_update_info) the
1033 * info_ptr palette gets changed. This is extremely unexpected and
1034 * confusing.
1035 *
1036 * Fix this by not sharing the palette in this way.
1037 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001038 png_set_PLTE(png_ptr, info_ptr, palette, num);
1039
Chris Craikb50c2172013-07-29 15:28:30 -07001040 /* The three chunks, bKGD, hIST and tRNS *must* appear after PLTE and before
1041 * IDAT. Prior to 1.6.0 this was not checked; instead the code merely
1042 * checked the apparent validity of a tRNS chunk inserted before PLTE on a
1043 * palette PNG. 1.6.0 attempts to rigorously follow the standard and
1044 * therefore does a benign error if the erroneous condition is detected *and*
1045 * cancels the tRNS if the benign error returns. The alternative is to
1046 * amend the standard since it would be rather hypocritical of the standards
1047 * maintainers to ignore it.
1048 */
Patrick Scott5f6bd842010-06-28 16:55:16 -04001049#ifdef PNG_READ_tRNS_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001050 if (png_ptr->num_trans > 0 ||
1051 (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS) != 0))
The Android Open Source Project893912b2009-03-03 19:30:05 -08001052 {
Chris Craikb50c2172013-07-29 15:28:30 -07001053 /* Cancel this because otherwise it would be used if the transforms
1054 * require it. Don't cancel the 'valid' flag because this would prevent
1055 * detection of duplicate chunks.
1056 */
1057 png_ptr->num_trans = 0;
1058
1059 if (info_ptr != NULL)
1060 info_ptr->num_trans = 0;
1061
1062 png_chunk_benign_error(png_ptr, "tRNS must be after");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001063 }
1064#endif
1065
Chris Craikb50c2172013-07-29 15:28:30 -07001066#ifdef PNG_READ_hIST_SUPPORTED
1067 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) != 0)
1068 png_chunk_benign_error(png_ptr, "hIST must be after");
1069#endif
1070
1071#ifdef PNG_READ_bKGD_SUPPORTED
1072 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) != 0)
1073 png_chunk_benign_error(png_ptr, "bKGD must be after");
1074#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -08001075}
1076
1077void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07001078png_handle_IEND(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001079{
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001080 png_debug(1, "in png_handle_IEND");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001081
1082 if (!(png_ptr->mode & PNG_HAVE_IHDR) || !(png_ptr->mode & PNG_HAVE_IDAT))
Chris Craikb50c2172013-07-29 15:28:30 -07001083 png_chunk_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001084
1085 png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND);
1086
The Android Open Source Project893912b2009-03-03 19:30:05 -08001087 png_crc_finish(png_ptr, length);
1088
Chris Craikb50c2172013-07-29 15:28:30 -07001089 if (length != 0)
1090 png_chunk_benign_error(png_ptr, "invalid");
1091
1092 PNG_UNUSED(info_ptr)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001093}
1094
Patrick Scott5f6bd842010-06-28 16:55:16 -04001095#ifdef PNG_READ_gAMA_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001096void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07001097png_handle_gAMA(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001098{
1099 png_fixed_point igamma;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001100 png_byte buf[4];
1101
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001102 png_debug(1, "in png_handle_gAMA");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001103
1104 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Chris Craikb50c2172013-07-29 15:28:30 -07001105 png_chunk_error(png_ptr, "missing IHDR");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001106
Chris Craikb50c2172013-07-29 15:28:30 -07001107 else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE))
The Android Open Source Project893912b2009-03-03 19:30:05 -08001108 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001109 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001110 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001111 return;
1112 }
1113
1114 if (length != 4)
1115 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001116 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001117 png_chunk_benign_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001118 return;
1119 }
1120
1121 png_crc_read(png_ptr, buf, 4);
Chris Craikb50c2172013-07-29 15:28:30 -07001122
The Android Open Source Project893912b2009-03-03 19:30:05 -08001123 if (png_crc_finish(png_ptr, 0))
1124 return;
1125
Chris Craikb50c2172013-07-29 15:28:30 -07001126 igamma = png_get_fixed_point(NULL, buf);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001127
Chris Craikb50c2172013-07-29 15:28:30 -07001128 png_colorspace_set_gamma(png_ptr, &png_ptr->colorspace, igamma);
1129 png_colorspace_sync(png_ptr, info_ptr);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001130}
1131#endif
1132
Patrick Scott5f6bd842010-06-28 16:55:16 -04001133#ifdef PNG_READ_sBIT_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001134void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07001135png_handle_sBIT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001136{
Chris Craikb50c2172013-07-29 15:28:30 -07001137 unsigned int truelen;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001138 png_byte buf[4];
1139
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001140 png_debug(1, "in png_handle_sBIT");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001141
1142 buf[0] = buf[1] = buf[2] = buf[3] = 0;
1143
1144 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Chris Craikb50c2172013-07-29 15:28:30 -07001145 png_chunk_error(png_ptr, "missing IHDR");
1146
1147 else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE))
The Android Open Source Project893912b2009-03-03 19:30:05 -08001148 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001149 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001150 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001151 return;
1152 }
Chris Craikb50c2172013-07-29 15:28:30 -07001153
The Android Open Source Project893912b2009-03-03 19:30:05 -08001154 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT))
1155 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001156 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001157 png_chunk_benign_error(png_ptr, "duplicate");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001158 return;
1159 }
1160
1161 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1162 truelen = 3;
Chris Craikb50c2172013-07-29 15:28:30 -07001163
The Android Open Source Project893912b2009-03-03 19:30:05 -08001164 else
Chris Craikb50c2172013-07-29 15:28:30 -07001165 truelen = png_ptr->channels;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001166
1167 if (length != truelen || length > 4)
1168 {
Chris Craikb50c2172013-07-29 15:28:30 -07001169 png_chunk_benign_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001170 png_crc_finish(png_ptr, length);
1171 return;
1172 }
1173
1174 png_crc_read(png_ptr, buf, truelen);
Chris Craikb50c2172013-07-29 15:28:30 -07001175
The Android Open Source Project893912b2009-03-03 19:30:05 -08001176 if (png_crc_finish(png_ptr, 0))
1177 return;
1178
1179 if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
1180 {
1181 png_ptr->sig_bit.red = buf[0];
1182 png_ptr->sig_bit.green = buf[1];
1183 png_ptr->sig_bit.blue = buf[2];
1184 png_ptr->sig_bit.alpha = buf[3];
1185 }
Chris Craikb50c2172013-07-29 15:28:30 -07001186
The Android Open Source Project893912b2009-03-03 19:30:05 -08001187 else
1188 {
1189 png_ptr->sig_bit.gray = buf[0];
1190 png_ptr->sig_bit.red = buf[0];
1191 png_ptr->sig_bit.green = buf[0];
1192 png_ptr->sig_bit.blue = buf[0];
1193 png_ptr->sig_bit.alpha = buf[1];
1194 }
Chris Craikb50c2172013-07-29 15:28:30 -07001195
The Android Open Source Project893912b2009-03-03 19:30:05 -08001196 png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
1197}
1198#endif
1199
Patrick Scott5f6bd842010-06-28 16:55:16 -04001200#ifdef PNG_READ_cHRM_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001201void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07001202png_handle_cHRM(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001203{
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001204 png_byte buf[32];
Chris Craikb50c2172013-07-29 15:28:30 -07001205 png_xy xy;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001206
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001207 png_debug(1, "in png_handle_cHRM");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001208
1209 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Chris Craikb50c2172013-07-29 15:28:30 -07001210 png_chunk_error(png_ptr, "missing IHDR");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001211
Chris Craikb50c2172013-07-29 15:28:30 -07001212 else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE))
The Android Open Source Project893912b2009-03-03 19:30:05 -08001213 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001214 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001215 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001216 return;
1217 }
1218
1219 if (length != 32)
1220 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001221 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001222 png_chunk_benign_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001223 return;
1224 }
1225
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001226 png_crc_read(png_ptr, buf, 32);
Chris Craikb50c2172013-07-29 15:28:30 -07001227
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001228 if (png_crc_finish(png_ptr, 0))
The Android Open Source Project893912b2009-03-03 19:30:05 -08001229 return;
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001230
Chris Craikb50c2172013-07-29 15:28:30 -07001231 xy.whitex = png_get_fixed_point(NULL, buf);
1232 xy.whitey = png_get_fixed_point(NULL, buf + 4);
1233 xy.redx = png_get_fixed_point(NULL, buf + 8);
1234 xy.redy = png_get_fixed_point(NULL, buf + 12);
1235 xy.greenx = png_get_fixed_point(NULL, buf + 16);
1236 xy.greeny = png_get_fixed_point(NULL, buf + 20);
1237 xy.bluex = png_get_fixed_point(NULL, buf + 24);
1238 xy.bluey = png_get_fixed_point(NULL, buf + 28);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001239
Chris Craikb50c2172013-07-29 15:28:30 -07001240 if (xy.whitex == PNG_FIXED_ERROR ||
1241 xy.whitey == PNG_FIXED_ERROR ||
1242 xy.redx == PNG_FIXED_ERROR ||
1243 xy.redy == PNG_FIXED_ERROR ||
1244 xy.greenx == PNG_FIXED_ERROR ||
1245 xy.greeny == PNG_FIXED_ERROR ||
1246 xy.bluex == PNG_FIXED_ERROR ||
1247 xy.bluey == PNG_FIXED_ERROR)
1248 {
1249 png_chunk_benign_error(png_ptr, "invalid values");
1250 return;
1251 }
The Android Open Source Project893912b2009-03-03 19:30:05 -08001252
Chris Craikb50c2172013-07-29 15:28:30 -07001253 /* If a colorspace error has already been output skip this chunk */
1254 if (png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID)
1255 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001256
Chris Craikb50c2172013-07-29 15:28:30 -07001257 if (png_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM)
1258 {
1259 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1260 png_colorspace_sync(png_ptr, info_ptr);
1261 png_chunk_benign_error(png_ptr, "duplicate");
1262 return;
1263 }
The Android Open Source Project893912b2009-03-03 19:30:05 -08001264
Chris Craikb50c2172013-07-29 15:28:30 -07001265 png_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM;
1266 (void)png_colorspace_set_chromaticities(png_ptr, &png_ptr->colorspace, &xy,
1267 1/*prefer cHRM values*/);
1268 png_colorspace_sync(png_ptr, info_ptr);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001269}
1270#endif
1271
Patrick Scott5f6bd842010-06-28 16:55:16 -04001272#ifdef PNG_READ_sRGB_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001273void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07001274png_handle_sRGB(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001275{
Chris Craikb50c2172013-07-29 15:28:30 -07001276 png_byte intent;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001277
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001278 png_debug(1, "in png_handle_sRGB");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001279
1280 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Chris Craikb50c2172013-07-29 15:28:30 -07001281 png_chunk_error(png_ptr, "missing IHDR");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001282
Chris Craikb50c2172013-07-29 15:28:30 -07001283 else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE))
The Android Open Source Project893912b2009-03-03 19:30:05 -08001284 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001285 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001286 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001287 return;
1288 }
1289
1290 if (length != 1)
1291 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001292 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001293 png_chunk_benign_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001294 return;
1295 }
1296
Chris Craikb50c2172013-07-29 15:28:30 -07001297 png_crc_read(png_ptr, &intent, 1);
1298
The Android Open Source Project893912b2009-03-03 19:30:05 -08001299 if (png_crc_finish(png_ptr, 0))
1300 return;
1301
Chris Craikb50c2172013-07-29 15:28:30 -07001302 /* If a colorspace error has already been output skip this chunk */
1303 if (png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID)
1304 return;
1305
1306 /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect
1307 * this.
1308 */
1309 if (png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001310 {
Chris Craikb50c2172013-07-29 15:28:30 -07001311 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1312 png_colorspace_sync(png_ptr, info_ptr);
1313 png_chunk_benign_error(png_ptr, "too many profiles");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001314 return;
1315 }
1316
Chris Craikb50c2172013-07-29 15:28:30 -07001317 (void)png_colorspace_set_sRGB(png_ptr, &png_ptr->colorspace, intent);
1318 png_colorspace_sync(png_ptr, info_ptr);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001319}
1320#endif /* PNG_READ_sRGB_SUPPORTED */
1321
Patrick Scott5f6bd842010-06-28 16:55:16 -04001322#ifdef PNG_READ_iCCP_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001323void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07001324png_handle_iCCP(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1325/* Note: this does not properly handle profiles that are > 64K under DOS */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001326{
Chris Craikb50c2172013-07-29 15:28:30 -07001327 png_const_charp errmsg = NULL; /* error message output, or no error */
1328 int finished = 0; /* crc checked */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001329
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001330 png_debug(1, "in png_handle_iCCP");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001331
1332 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Chris Craikb50c2172013-07-29 15:28:30 -07001333 png_chunk_error(png_ptr, "missing IHDR");
1334
1335 else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE))
The Android Open Source Project893912b2009-03-03 19:30:05 -08001336 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001337 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001338 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001339 return;
1340 }
1341
Chris Craikb50c2172013-07-29 15:28:30 -07001342 /* Consistent with all the above colorspace handling an obviously *invalid*
1343 * chunk is just ignored, so does not invalidate the color space. An
1344 * alternative is to set the 'invalid' flags at the start of this routine
1345 * and only clear them in they were not set before and all the tests pass.
1346 * The minimum 'deflate' stream is assumed to be just the 2 byte header and 4
1347 * byte checksum. The keyword must be one character and there is a
1348 * terminator (0) byte and the compression method.
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001349 */
Chris Craikb50c2172013-07-29 15:28:30 -07001350 if (length < 9)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001351 {
Chris Craikb50c2172013-07-29 15:28:30 -07001352 png_crc_finish(png_ptr, length);
1353 png_chunk_benign_error(png_ptr, "too short");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001354 return;
1355 }
1356
Chris Craikb50c2172013-07-29 15:28:30 -07001357 /* If a colorspace error has already been output skip this chunk */
1358 if (png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001359 {
Chris Craikb50c2172013-07-29 15:28:30 -07001360 png_crc_finish(png_ptr, length);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001361 return;
1362 }
1363
Chris Craikb50c2172013-07-29 15:28:30 -07001364 /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect
1365 * this.
1366 */
1367 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) == 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001368 {
Chris Craikb50c2172013-07-29 15:28:30 -07001369 uInt read_length, keyword_length;
1370 char keyword[81];
1371
1372 /* Find the keyword; the keyword plus separator and compression method
1373 * bytes can be at most 81 characters long.
1374 */
1375 read_length = 81; /* maximum */
1376 if (read_length > length)
1377 read_length = (uInt)length;
1378
1379 png_crc_read(png_ptr, (png_bytep)keyword, read_length);
1380 length -= read_length;
1381
1382 keyword_length = 0;
1383 while (keyword_length < 80 && keyword_length < read_length &&
1384 keyword[keyword_length] != 0)
1385 ++keyword_length;
1386
1387 /* TODO: make the keyword checking common */
1388 if (keyword_length >= 1 && keyword_length <= 79)
1389 {
1390 /* We only understand '0' compression - deflate - so if we get a
1391 * different value we can't safely decode the chunk.
1392 */
1393 if (keyword_length+1 < read_length &&
1394 keyword[keyword_length+1] == PNG_COMPRESSION_TYPE_BASE)
1395 {
1396 read_length -= keyword_length+2;
1397
1398 if (png_inflate_claim(png_ptr, png_iCCP) == Z_OK)
1399 {
1400 Byte profile_header[132];
1401 Byte local_buffer[PNG_INFLATE_BUF_SIZE];
1402 png_alloc_size_t size = (sizeof profile_header);
1403
1404 png_ptr->zstream.next_in = (Bytef*)keyword + (keyword_length+2);
1405 png_ptr->zstream.avail_in = read_length;
1406 (void)png_inflate_read(png_ptr, local_buffer,
1407 (sizeof local_buffer), &length, profile_header, &size,
1408 0/*finish: don't, because the output is too small*/);
1409
1410 if (size == 0)
1411 {
1412 /* We have the ICC profile header; do the basic header checks.
1413 */
1414 const png_uint_32 profile_length =
1415 png_get_uint_32(profile_header);
1416
1417 if (png_icc_check_length(png_ptr, &png_ptr->colorspace,
1418 keyword, profile_length))
1419 {
1420 /* The length is apparently ok, so we can check the 132
1421 * byte header.
1422 */
1423 if (png_icc_check_header(png_ptr, &png_ptr->colorspace,
1424 keyword, profile_length, profile_header,
1425 png_ptr->color_type))
1426 {
1427 /* Now read the tag table; a variable size buffer is
1428 * needed at this point, allocate one for the whole
1429 * profile. The header check has already validated
1430 * that none of these stuff will overflow.
1431 */
1432 const png_uint_32 tag_count = png_get_uint_32(
1433 profile_header+128);
1434 png_bytep profile = png_read_buffer(png_ptr,
1435 profile_length, 2/*silent*/);
1436
1437 if (profile != NULL)
1438 {
1439 memcpy(profile, profile_header,
1440 (sizeof profile_header));
1441
1442 size = 12 * tag_count;
1443
1444 (void)png_inflate_read(png_ptr, local_buffer,
1445 (sizeof local_buffer), &length,
1446 profile + (sizeof profile_header), &size, 0);
1447
1448 /* Still expect a a buffer error because we expect
1449 * there to be some tag data!
1450 */
1451 if (size == 0)
1452 {
1453 if (png_icc_check_tag_table(png_ptr,
1454 &png_ptr->colorspace, keyword, profile_length,
1455 profile))
1456 {
1457 /* The profile has been validated for basic
1458 * security issues, so read the whole thing in.
1459 */
1460 size = profile_length - (sizeof profile_header)
1461 - 12 * tag_count;
1462
1463 (void)png_inflate_read(png_ptr, local_buffer,
1464 (sizeof local_buffer), &length,
1465 profile + (sizeof profile_header) +
1466 12 * tag_count, &size, 1/*finish*/);
1467
1468 if (length > 0 && !(png_ptr->flags &
1469 PNG_FLAG_BENIGN_ERRORS_WARN))
1470 errmsg = "extra compressed data";
1471
1472 /* But otherwise allow extra data: */
1473 else if (size == 0)
1474 {
1475 if (length > 0)
1476 {
1477 /* This can be handled completely, so
1478 * keep going.
1479 */
1480 png_chunk_warning(png_ptr,
1481 "extra compressed data");
1482 }
1483
1484 png_crc_finish(png_ptr, length);
1485 finished = 1;
1486
1487# ifdef PNG_sRGB_SUPPORTED
1488 /* Check for a match against sRGB */
1489 png_icc_set_sRGB(png_ptr,
1490 &png_ptr->colorspace, profile,
1491 png_ptr->zstream.adler);
1492# endif
1493
1494 /* Steal the profile for info_ptr. */
1495 if (info_ptr != NULL)
1496 {
1497 png_free_data(png_ptr, info_ptr,
1498 PNG_FREE_ICCP, 0);
1499
1500 info_ptr->iccp_name = png_voidcast(char*,
1501 png_malloc_base(png_ptr,
1502 keyword_length+1));
1503 if (info_ptr->iccp_name != NULL)
1504 {
1505 memcpy(info_ptr->iccp_name, keyword,
1506 keyword_length+1);
1507 info_ptr->iccp_proflen =
1508 profile_length;
1509 info_ptr->iccp_profile = profile;
1510 png_ptr->read_buffer = NULL; /*steal*/
1511 info_ptr->free_me |= PNG_FREE_ICCP;
1512 info_ptr->valid |= PNG_INFO_iCCP;
1513 }
1514
1515 else
1516 {
1517 png_ptr->colorspace.flags |=
1518 PNG_COLORSPACE_INVALID;
1519 errmsg = "out of memory";
1520 }
1521 }
1522
1523 /* else the profile remains in the read
1524 * buffer which gets reused for subsequent
1525 * chunks.
1526 */
1527
1528 if (info_ptr != NULL)
1529 png_colorspace_sync(png_ptr, info_ptr);
1530
1531 if (errmsg == NULL)
1532 {
1533 png_ptr->zowner = 0;
1534 return;
1535 }
1536 }
1537
1538 else if (size > 0)
1539 errmsg = "truncated";
1540
1541 else
1542 errmsg = png_ptr->zstream.msg;
1543 }
1544
1545 /* else png_icc_check_tag_table output an error */
1546 }
1547
1548 else /* profile truncated */
1549 errmsg = png_ptr->zstream.msg;
1550 }
1551
1552 else
1553 errmsg = "out of memory";
1554 }
1555
1556 /* else png_icc_check_header output an error */
1557 }
1558
1559 /* else png_icc_check_length output an error */
1560 }
1561
1562 else /* profile truncated */
1563 errmsg = png_ptr->zstream.msg;
1564
1565 /* Release the stream */
1566 png_ptr->zowner = 0;
1567 }
1568
1569 else /* png_inflate_claim failed */
1570 errmsg = png_ptr->zstream.msg;
1571 }
1572
1573 else
1574 errmsg = "bad compression method"; /* or missing */
1575 }
1576
1577 else
1578 errmsg = "bad keyword";
The Android Open Source Project893912b2009-03-03 19:30:05 -08001579 }
1580
Chris Craikb50c2172013-07-29 15:28:30 -07001581 else
1582 errmsg = "too many profiles";
1583
1584 /* Failure: the reason is in 'errmsg' */
1585 if (!finished)
1586 png_crc_finish(png_ptr, length);
1587
1588 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1589 png_colorspace_sync(png_ptr, info_ptr);
1590 if (errmsg != NULL) /* else already output */
1591 png_chunk_benign_error(png_ptr, errmsg);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001592}
1593#endif /* PNG_READ_iCCP_SUPPORTED */
1594
Patrick Scott5f6bd842010-06-28 16:55:16 -04001595#ifdef PNG_READ_sPLT_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001596void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07001597png_handle_sPLT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001598/* Note: this does not properly handle chunks that are > 64K under DOS */
1599{
Chris Craikb50c2172013-07-29 15:28:30 -07001600 png_bytep entry_start, buffer;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001601 png_sPLT_t new_palette;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001602 png_sPLT_entryp pp;
Chris Craikb50c2172013-07-29 15:28:30 -07001603 png_uint_32 data_length;
1604 int entry_size, i;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001605 png_uint_32 skip = 0;
Chris Craikb50c2172013-07-29 15:28:30 -07001606 png_uint_32 dl;
1607 png_size_t max_dl;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001608
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001609 png_debug(1, "in png_handle_sPLT");
1610
Patrick Scott5f6bd842010-06-28 16:55:16 -04001611#ifdef PNG_USER_LIMITS_SUPPORTED
Patrick Scott5f6bd842010-06-28 16:55:16 -04001612 if (png_ptr->user_chunk_cache_max != 0)
1613 {
1614 if (png_ptr->user_chunk_cache_max == 1)
1615 {
1616 png_crc_finish(png_ptr, length);
1617 return;
1618 }
Chris Craikb50c2172013-07-29 15:28:30 -07001619
Patrick Scott5f6bd842010-06-28 16:55:16 -04001620 if (--png_ptr->user_chunk_cache_max == 1)
1621 {
1622 png_warning(png_ptr, "No space in chunk cache for sPLT");
1623 png_crc_finish(png_ptr, length);
1624 return;
1625 }
1626 }
1627#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -08001628
1629 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Chris Craikb50c2172013-07-29 15:28:30 -07001630 png_chunk_error(png_ptr, "missing IHDR");
1631
The Android Open Source Project893912b2009-03-03 19:30:05 -08001632 else if (png_ptr->mode & PNG_HAVE_IDAT)
1633 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001634 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001635 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001636 return;
1637 }
1638
1639#ifdef PNG_MAX_MALLOC_64K
Chris Craikb50c2172013-07-29 15:28:30 -07001640 if (length > 65535U)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001641 {
Chris Craikb50c2172013-07-29 15:28:30 -07001642 png_crc_finish(png_ptr, length);
1643 png_chunk_benign_error(png_ptr, "too large to fit in memory");
1644 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001645 }
1646#endif
1647
Chris Craikb50c2172013-07-29 15:28:30 -07001648 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
1649 if (buffer == NULL)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001650 {
Chris Craikb50c2172013-07-29 15:28:30 -07001651 png_crc_finish(png_ptr, length);
1652 png_chunk_benign_error(png_ptr, "out of memory");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001653 return;
1654 }
1655
The Android Open Source Project893912b2009-03-03 19:30:05 -08001656
Chris Craikb50c2172013-07-29 15:28:30 -07001657 /* WARNING: this may break if size_t is less than 32 bits; it is assumed
1658 * that the PNG_MAX_MALLOC_64K test is enabled in this case, but this is a
1659 * potential breakage point if the types in pngconf.h aren't exactly right.
1660 */
1661 png_crc_read(png_ptr, buffer, length);
1662
1663 if (png_crc_finish(png_ptr, skip))
1664 return;
1665
1666 buffer[length] = 0;
1667
1668 for (entry_start = buffer; *entry_start; entry_start++)
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001669 /* Empty loop to find end of name */ ;
Chris Craikb50c2172013-07-29 15:28:30 -07001670
The Android Open Source Project893912b2009-03-03 19:30:05 -08001671 ++entry_start;
1672
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001673 /* A sample depth should follow the separator, and we should be on it */
Chris Craikb50c2172013-07-29 15:28:30 -07001674 if (entry_start > buffer + length - 2)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001675 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001676 png_warning(png_ptr, "malformed sPLT chunk");
1677 return;
1678 }
1679
1680 new_palette.depth = *entry_start++;
1681 entry_size = (new_palette.depth == 8 ? 6 : 10);
Chris Craikb50c2172013-07-29 15:28:30 -07001682 /* This must fit in a png_uint_32 because it is derived from the original
1683 * chunk data length.
1684 */
1685 data_length = length - (png_uint_32)(entry_start - buffer);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001686
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001687 /* Integrity-check the data length */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001688 if (data_length % entry_size)
1689 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001690 png_warning(png_ptr, "sPLT chunk has bad length");
1691 return;
1692 }
1693
Chris Craikb50c2172013-07-29 15:28:30 -07001694 dl = (png_int_32)(data_length / entry_size);
1695 max_dl = PNG_SIZE_MAX / (sizeof (png_sPLT_entry));
1696
1697 if (dl > max_dl)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001698 {
1699 png_warning(png_ptr, "sPLT chunk too long");
1700 return;
1701 }
Chris Craikb50c2172013-07-29 15:28:30 -07001702
1703 new_palette.nentries = (png_int_32)(data_length / entry_size);
1704
The Android Open Source Project893912b2009-03-03 19:30:05 -08001705 new_palette.entries = (png_sPLT_entryp)png_malloc_warn(
Chris Craikb50c2172013-07-29 15:28:30 -07001706 png_ptr, new_palette.nentries * (sizeof (png_sPLT_entry)));
1707
The Android Open Source Project893912b2009-03-03 19:30:05 -08001708 if (new_palette.entries == NULL)
1709 {
1710 png_warning(png_ptr, "sPLT chunk requires too much memory");
1711 return;
1712 }
1713
Patrick Scott5f6bd842010-06-28 16:55:16 -04001714#ifdef PNG_POINTER_INDEXING_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001715 for (i = 0; i < new_palette.nentries; i++)
1716 {
Patrick Scott5f6bd842010-06-28 16:55:16 -04001717 pp = new_palette.entries + i;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001718
1719 if (new_palette.depth == 8)
1720 {
Chris Craikb50c2172013-07-29 15:28:30 -07001721 pp->red = *entry_start++;
1722 pp->green = *entry_start++;
1723 pp->blue = *entry_start++;
1724 pp->alpha = *entry_start++;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001725 }
Chris Craikb50c2172013-07-29 15:28:30 -07001726
The Android Open Source Project893912b2009-03-03 19:30:05 -08001727 else
1728 {
Chris Craikb50c2172013-07-29 15:28:30 -07001729 pp->red = png_get_uint_16(entry_start); entry_start += 2;
1730 pp->green = png_get_uint_16(entry_start); entry_start += 2;
1731 pp->blue = png_get_uint_16(entry_start); entry_start += 2;
1732 pp->alpha = png_get_uint_16(entry_start); entry_start += 2;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001733 }
Chris Craikb50c2172013-07-29 15:28:30 -07001734
The Android Open Source Project893912b2009-03-03 19:30:05 -08001735 pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
1736 }
1737#else
1738 pp = new_palette.entries;
Chris Craikb50c2172013-07-29 15:28:30 -07001739
The Android Open Source Project893912b2009-03-03 19:30:05 -08001740 for (i = 0; i < new_palette.nentries; i++)
1741 {
1742
1743 if (new_palette.depth == 8)
1744 {
Chris Craikb50c2172013-07-29 15:28:30 -07001745 pp[i].red = *entry_start++;
1746 pp[i].green = *entry_start++;
1747 pp[i].blue = *entry_start++;
1748 pp[i].alpha = *entry_start++;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001749 }
Chris Craikb50c2172013-07-29 15:28:30 -07001750
The Android Open Source Project893912b2009-03-03 19:30:05 -08001751 else
1752 {
Chris Craikb50c2172013-07-29 15:28:30 -07001753 pp[i].red = png_get_uint_16(entry_start); entry_start += 2;
1754 pp[i].green = png_get_uint_16(entry_start); entry_start += 2;
1755 pp[i].blue = png_get_uint_16(entry_start); entry_start += 2;
1756 pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001757 }
Chris Craikb50c2172013-07-29 15:28:30 -07001758
1759 pp[i].frequency = png_get_uint_16(entry_start); entry_start += 2;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001760 }
1761#endif
1762
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001763 /* Discard all chunk data except the name and stash that */
Chris Craikb50c2172013-07-29 15:28:30 -07001764 new_palette.name = (png_charp)buffer;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001765
1766 png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);
1767
The Android Open Source Project893912b2009-03-03 19:30:05 -08001768 png_free(png_ptr, new_palette.entries);
1769}
1770#endif /* PNG_READ_sPLT_SUPPORTED */
1771
Patrick Scott5f6bd842010-06-28 16:55:16 -04001772#ifdef PNG_READ_tRNS_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001773void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07001774png_handle_tRNS(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001775{
1776 png_byte readbuf[PNG_MAX_PALETTE_LENGTH];
1777
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001778 png_debug(1, "in png_handle_tRNS");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001779
1780 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Chris Craikb50c2172013-07-29 15:28:30 -07001781 png_chunk_error(png_ptr, "missing IHDR");
1782
The Android Open Source Project893912b2009-03-03 19:30:05 -08001783 else if (png_ptr->mode & PNG_HAVE_IDAT)
1784 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001785 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001786 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001787 return;
1788 }
Chris Craikb50c2172013-07-29 15:28:30 -07001789
The Android Open Source Project893912b2009-03-03 19:30:05 -08001790 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
1791 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001792 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001793 png_chunk_benign_error(png_ptr, "duplicate");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001794 return;
1795 }
1796
1797 if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
1798 {
1799 png_byte buf[2];
1800
1801 if (length != 2)
1802 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001803 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001804 png_chunk_benign_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001805 return;
1806 }
1807
1808 png_crc_read(png_ptr, buf, 2);
1809 png_ptr->num_trans = 1;
Chris Craikb50c2172013-07-29 15:28:30 -07001810 png_ptr->trans_color.gray = png_get_uint_16(buf);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001811 }
Chris Craikb50c2172013-07-29 15:28:30 -07001812
The Android Open Source Project893912b2009-03-03 19:30:05 -08001813 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
1814 {
1815 png_byte buf[6];
1816
1817 if (length != 6)
1818 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001819 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001820 png_chunk_benign_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001821 return;
1822 }
Chris Craikb50c2172013-07-29 15:28:30 -07001823
1824 png_crc_read(png_ptr, buf, length);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001825 png_ptr->num_trans = 1;
Chris Craikb50c2172013-07-29 15:28:30 -07001826 png_ptr->trans_color.red = png_get_uint_16(buf);
1827 png_ptr->trans_color.green = png_get_uint_16(buf + 2);
1828 png_ptr->trans_color.blue = png_get_uint_16(buf + 4);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001829 }
Chris Craikb50c2172013-07-29 15:28:30 -07001830
The Android Open Source Project893912b2009-03-03 19:30:05 -08001831 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1832 {
1833 if (!(png_ptr->mode & PNG_HAVE_PLTE))
1834 {
Chris Craikb50c2172013-07-29 15:28:30 -07001835 /* TODO: is this actually an error in the ISO spec? */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001836 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001837 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001838 return;
1839 }
Chris Craikb50c2172013-07-29 15:28:30 -07001840
1841 if (length > png_ptr->num_palette || length > PNG_MAX_PALETTE_LENGTH ||
1842 length == 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001843 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001844 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001845 png_chunk_benign_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001846 return;
1847 }
Chris Craikb50c2172013-07-29 15:28:30 -07001848
1849 png_crc_read(png_ptr, readbuf, length);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001850 png_ptr->num_trans = (png_uint_16)length;
1851 }
Chris Craikb50c2172013-07-29 15:28:30 -07001852
The Android Open Source Project893912b2009-03-03 19:30:05 -08001853 else
1854 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001855 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001856 png_chunk_benign_error(png_ptr, "invalid with alpha channel");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001857 return;
1858 }
1859
1860 if (png_crc_finish(png_ptr, 0))
1861 {
1862 png_ptr->num_trans = 0;
1863 return;
1864 }
1865
Chris Craikb50c2172013-07-29 15:28:30 -07001866 /* TODO: this is a horrible side effect in the palette case because the
1867 * png_struct ends up with a pointer to the tRNS buffer owned by the
1868 * png_info. Fix this.
1869 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001870 png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,
Chris Craikb50c2172013-07-29 15:28:30 -07001871 &(png_ptr->trans_color));
The Android Open Source Project893912b2009-03-03 19:30:05 -08001872}
1873#endif
1874
Patrick Scott5f6bd842010-06-28 16:55:16 -04001875#ifdef PNG_READ_bKGD_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001876void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07001877png_handle_bKGD(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001878{
Chris Craikb50c2172013-07-29 15:28:30 -07001879 unsigned int truelen;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001880 png_byte buf[6];
Chris Craikb50c2172013-07-29 15:28:30 -07001881 png_color_16 background;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001882
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001883 png_debug(1, "in png_handle_bKGD");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001884
1885 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Chris Craikb50c2172013-07-29 15:28:30 -07001886 png_chunk_error(png_ptr, "missing IHDR");
1887
1888 else if ((png_ptr->mode & PNG_HAVE_IDAT) ||
1889 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
1890 !(png_ptr->mode & PNG_HAVE_PLTE)))
The Android Open Source Project893912b2009-03-03 19:30:05 -08001891 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001892 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001893 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001894 return;
1895 }
Chris Craikb50c2172013-07-29 15:28:30 -07001896
The Android Open Source Project893912b2009-03-03 19:30:05 -08001897 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD))
1898 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001899 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001900 png_chunk_benign_error(png_ptr, "duplicate");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001901 return;
1902 }
1903
1904 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1905 truelen = 1;
Chris Craikb50c2172013-07-29 15:28:30 -07001906
The Android Open Source Project893912b2009-03-03 19:30:05 -08001907 else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
1908 truelen = 6;
Chris Craikb50c2172013-07-29 15:28:30 -07001909
The Android Open Source Project893912b2009-03-03 19:30:05 -08001910 else
1911 truelen = 2;
1912
1913 if (length != truelen)
1914 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001915 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001916 png_chunk_benign_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001917 return;
1918 }
1919
1920 png_crc_read(png_ptr, buf, truelen);
Chris Craikb50c2172013-07-29 15:28:30 -07001921
The Android Open Source Project893912b2009-03-03 19:30:05 -08001922 if (png_crc_finish(png_ptr, 0))
1923 return;
1924
1925 /* We convert the index value into RGB components so that we can allow
1926 * arbitrary RGB values for background when we have transparency, and
1927 * so it is easy to determine the RGB values of the background color
Chris Craikb50c2172013-07-29 15:28:30 -07001928 * from the info_ptr struct.
1929 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001930 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1931 {
Chris Craikb50c2172013-07-29 15:28:30 -07001932 background.index = buf[0];
1933
The Android Open Source Project893912b2009-03-03 19:30:05 -08001934 if (info_ptr && info_ptr->num_palette)
1935 {
Chris Craikb50c2172013-07-29 15:28:30 -07001936 if (buf[0] >= info_ptr->num_palette)
1937 {
1938 png_chunk_benign_error(png_ptr, "invalid index");
1939 return;
1940 }
1941
1942 background.red = (png_uint_16)png_ptr->palette[buf[0]].red;
1943 background.green = (png_uint_16)png_ptr->palette[buf[0]].green;
1944 background.blue = (png_uint_16)png_ptr->palette[buf[0]].blue;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001945 }
Chris Craikb50c2172013-07-29 15:28:30 -07001946
1947 else
1948 background.red = background.green = background.blue = 0;
1949
1950 background.gray = 0;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001951 }
1952
Chris Craikb50c2172013-07-29 15:28:30 -07001953 else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) /* GRAY */
1954 {
1955 background.index = 0;
1956 background.red =
1957 background.green =
1958 background.blue =
1959 background.gray = png_get_uint_16(buf);
1960 }
1961
1962 else
1963 {
1964 background.index = 0;
1965 background.red = png_get_uint_16(buf);
1966 background.green = png_get_uint_16(buf + 2);
1967 background.blue = png_get_uint_16(buf + 4);
1968 background.gray = 0;
1969 }
1970
1971 png_set_bKGD(png_ptr, info_ptr, &background);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001972}
1973#endif
1974
Patrick Scott5f6bd842010-06-28 16:55:16 -04001975#ifdef PNG_READ_hIST_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001976void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07001977png_handle_hIST(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001978{
1979 unsigned int num, i;
1980 png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];
1981
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001982 png_debug(1, "in png_handle_hIST");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001983
1984 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Chris Craikb50c2172013-07-29 15:28:30 -07001985 png_chunk_error(png_ptr, "missing IHDR");
1986
1987 else if ((png_ptr->mode & PNG_HAVE_IDAT) || !(png_ptr->mode & PNG_HAVE_PLTE))
The Android Open Source Project893912b2009-03-03 19:30:05 -08001988 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001989 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001990 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001991 return;
1992 }
Chris Craikb50c2172013-07-29 15:28:30 -07001993
The Android Open Source Project893912b2009-03-03 19:30:05 -08001994 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST))
1995 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001996 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07001997 png_chunk_benign_error(png_ptr, "duplicate");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001998 return;
1999 }
2000
2001 num = length / 2 ;
Chris Craikb50c2172013-07-29 15:28:30 -07002002
2003 if (num != png_ptr->num_palette || num > PNG_MAX_PALETTE_LENGTH)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002004 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002005 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002006 png_chunk_benign_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002007 return;
2008 }
2009
2010 for (i = 0; i < num; i++)
2011 {
2012 png_byte buf[2];
2013
2014 png_crc_read(png_ptr, buf, 2);
2015 readbuf[i] = png_get_uint_16(buf);
2016 }
2017
2018 if (png_crc_finish(png_ptr, 0))
2019 return;
2020
2021 png_set_hIST(png_ptr, info_ptr, readbuf);
2022}
2023#endif
2024
Patrick Scott5f6bd842010-06-28 16:55:16 -04002025#ifdef PNG_READ_pHYs_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08002026void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07002027png_handle_pHYs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002028{
2029 png_byte buf[9];
2030 png_uint_32 res_x, res_y;
2031 int unit_type;
2032
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002033 png_debug(1, "in png_handle_pHYs");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002034
2035 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Chris Craikb50c2172013-07-29 15:28:30 -07002036 png_chunk_error(png_ptr, "missing IHDR");
2037
The Android Open Source Project893912b2009-03-03 19:30:05 -08002038 else if (png_ptr->mode & PNG_HAVE_IDAT)
2039 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002040 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002041 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002042 return;
2043 }
Chris Craikb50c2172013-07-29 15:28:30 -07002044
The Android Open Source Project893912b2009-03-03 19:30:05 -08002045 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
2046 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002047 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002048 png_chunk_benign_error(png_ptr, "duplicate");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002049 return;
2050 }
2051
2052 if (length != 9)
2053 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002054 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002055 png_chunk_benign_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002056 return;
2057 }
2058
2059 png_crc_read(png_ptr, buf, 9);
Chris Craikb50c2172013-07-29 15:28:30 -07002060
The Android Open Source Project893912b2009-03-03 19:30:05 -08002061 if (png_crc_finish(png_ptr, 0))
2062 return;
2063
2064 res_x = png_get_uint_32(buf);
2065 res_y = png_get_uint_32(buf + 4);
2066 unit_type = buf[8];
2067 png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
2068}
2069#endif
2070
Patrick Scott5f6bd842010-06-28 16:55:16 -04002071#ifdef PNG_READ_oFFs_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08002072void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07002073png_handle_oFFs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002074{
2075 png_byte buf[9];
2076 png_int_32 offset_x, offset_y;
2077 int unit_type;
2078
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002079 png_debug(1, "in png_handle_oFFs");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002080
2081 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Chris Craikb50c2172013-07-29 15:28:30 -07002082 png_chunk_error(png_ptr, "missing IHDR");
2083
The Android Open Source Project893912b2009-03-03 19:30:05 -08002084 else if (png_ptr->mode & PNG_HAVE_IDAT)
2085 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002086 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002087 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002088 return;
2089 }
Chris Craikb50c2172013-07-29 15:28:30 -07002090
The Android Open Source Project893912b2009-03-03 19:30:05 -08002091 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs))
2092 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002093 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002094 png_chunk_benign_error(png_ptr, "duplicate");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002095 return;
2096 }
2097
2098 if (length != 9)
2099 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002100 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002101 png_chunk_benign_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002102 return;
2103 }
2104
2105 png_crc_read(png_ptr, buf, 9);
Chris Craikb50c2172013-07-29 15:28:30 -07002106
The Android Open Source Project893912b2009-03-03 19:30:05 -08002107 if (png_crc_finish(png_ptr, 0))
2108 return;
2109
2110 offset_x = png_get_int_32(buf);
2111 offset_y = png_get_int_32(buf + 4);
2112 unit_type = buf[8];
2113 png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
2114}
2115#endif
2116
Patrick Scott5f6bd842010-06-28 16:55:16 -04002117#ifdef PNG_READ_pCAL_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -04002118/* Read the pCAL chunk (described in the PNG Extensions document) */
The Android Open Source Project893912b2009-03-03 19:30:05 -08002119void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07002120png_handle_pCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002121{
The Android Open Source Project893912b2009-03-03 19:30:05 -08002122 png_int_32 X0, X1;
2123 png_byte type, nparams;
Chris Craikb50c2172013-07-29 15:28:30 -07002124 png_bytep buffer, buf, units, endptr;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002125 png_charpp params;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002126 int i;
2127
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002128 png_debug(1, "in png_handle_pCAL");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002129
2130 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Chris Craikb50c2172013-07-29 15:28:30 -07002131 png_chunk_error(png_ptr, "missing IHDR");
2132
The Android Open Source Project893912b2009-03-03 19:30:05 -08002133 else if (png_ptr->mode & PNG_HAVE_IDAT)
2134 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002135 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002136 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002137 return;
2138 }
Chris Craikb50c2172013-07-29 15:28:30 -07002139
The Android Open Source Project893912b2009-03-03 19:30:05 -08002140 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL))
2141 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002142 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002143 png_chunk_benign_error(png_ptr, "duplicate");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002144 return;
2145 }
2146
Chris Craikb50c2172013-07-29 15:28:30 -07002147 png_debug1(2, "Allocating and reading pCAL chunk data (%u bytes)",
2148 length + 1);
2149
2150 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
2151
2152 if (buffer == NULL)
2153 {
2154 png_crc_finish(png_ptr, length);
2155 png_chunk_benign_error(png_ptr, "out of memory");
2156 return;
2157 }
2158
2159 png_crc_read(png_ptr, buffer, length);
The Android Open Source Project893912b2009-03-03 19:30:05 -08002160
2161 if (png_crc_finish(png_ptr, 0))
The Android Open Source Project893912b2009-03-03 19:30:05 -08002162 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002163
Chris Craikb50c2172013-07-29 15:28:30 -07002164 buffer[length] = 0; /* Null terminate the last string */
The Android Open Source Project893912b2009-03-03 19:30:05 -08002165
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002166 png_debug(3, "Finding end of pCAL purpose string");
Chris Craikb50c2172013-07-29 15:28:30 -07002167 for (buf = buffer; *buf; buf++)
Patrick Scotta0bb96c2009-07-22 11:50:02 -04002168 /* Empty loop */ ;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002169
Chris Craikb50c2172013-07-29 15:28:30 -07002170 endptr = buffer + length;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002171
2172 /* We need to have at least 12 bytes after the purpose string
Chris Craikb50c2172013-07-29 15:28:30 -07002173 * in order to get the parameter information.
2174 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08002175 if (endptr <= buf + 12)
2176 {
Chris Craikb50c2172013-07-29 15:28:30 -07002177 png_chunk_benign_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002178 return;
2179 }
2180
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002181 png_debug(3, "Reading pCAL X0, X1, type, nparams, and units");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002182 X0 = png_get_int_32((png_bytep)buf+1);
2183 X1 = png_get_int_32((png_bytep)buf+5);
2184 type = buf[9];
2185 nparams = buf[10];
2186 units = buf + 11;
2187
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002188 png_debug(3, "Checking pCAL equation type and number of parameters");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002189 /* Check that we have the right number of parameters for known
Chris Craikb50c2172013-07-29 15:28:30 -07002190 * equation types.
2191 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08002192 if ((type == PNG_EQUATION_LINEAR && nparams != 2) ||
2193 (type == PNG_EQUATION_BASE_E && nparams != 3) ||
2194 (type == PNG_EQUATION_ARBITRARY && nparams != 3) ||
2195 (type == PNG_EQUATION_HYPERBOLIC && nparams != 4))
2196 {
Chris Craikb50c2172013-07-29 15:28:30 -07002197 png_chunk_benign_error(png_ptr, "invalid parameter count");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002198 return;
2199 }
Chris Craikb50c2172013-07-29 15:28:30 -07002200
The Android Open Source Project893912b2009-03-03 19:30:05 -08002201 else if (type >= PNG_EQUATION_LAST)
2202 {
Chris Craikb50c2172013-07-29 15:28:30 -07002203 png_chunk_benign_error(png_ptr, "unrecognized equation type");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002204 }
2205
2206 for (buf = units; *buf; buf++)
2207 /* Empty loop to move past the units string. */ ;
2208
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002209 png_debug(3, "Allocating pCAL parameters array");
Chris Craikb50c2172013-07-29 15:28:30 -07002210
2211 params = png_voidcast(png_charpp, png_malloc_warn(png_ptr,
2212 nparams * (sizeof (png_charp))));
2213
The Android Open Source Project893912b2009-03-03 19:30:05 -08002214 if (params == NULL)
Chris Craikb50c2172013-07-29 15:28:30 -07002215 {
2216 png_chunk_benign_error(png_ptr, "out of memory");
2217 return;
2218 }
The Android Open Source Project893912b2009-03-03 19:30:05 -08002219
2220 /* Get pointers to the start of each parameter string. */
Chris Craikb50c2172013-07-29 15:28:30 -07002221 for (i = 0; i < nparams; i++)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002222 {
2223 buf++; /* Skip the null string terminator from previous parameter. */
2224
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002225 png_debug1(3, "Reading pCAL parameter %d", i);
Chris Craikb50c2172013-07-29 15:28:30 -07002226
2227 for (params[i] = (png_charp)buf; buf <= endptr && *buf != 0; buf++)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002228 /* Empty loop to move past each parameter string */ ;
2229
2230 /* Make sure we haven't run out of data yet */
2231 if (buf > endptr)
2232 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002233 png_free(png_ptr, params);
Chris Craikb50c2172013-07-29 15:28:30 -07002234 png_chunk_benign_error(png_ptr, "invalid data");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002235 return;
2236 }
2237 }
2238
Chris Craikb50c2172013-07-29 15:28:30 -07002239 png_set_pCAL(png_ptr, info_ptr, (png_charp)buffer, X0, X1, type, nparams,
2240 (png_charp)units, params);
The Android Open Source Project893912b2009-03-03 19:30:05 -08002241
The Android Open Source Project893912b2009-03-03 19:30:05 -08002242 png_free(png_ptr, params);
2243}
2244#endif
2245
Patrick Scott5f6bd842010-06-28 16:55:16 -04002246#ifdef PNG_READ_sCAL_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -04002247/* Read the sCAL chunk */
The Android Open Source Project893912b2009-03-03 19:30:05 -08002248void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07002249png_handle_sCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002250{
Chris Craikb50c2172013-07-29 15:28:30 -07002251 png_bytep buffer;
2252 png_size_t i;
2253 int state;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002254
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002255 png_debug(1, "in png_handle_sCAL");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002256
2257 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Chris Craikb50c2172013-07-29 15:28:30 -07002258 png_chunk_error(png_ptr, "missing IHDR");
2259
The Android Open Source Project893912b2009-03-03 19:30:05 -08002260 else if (png_ptr->mode & PNG_HAVE_IDAT)
2261 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002262 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002263 png_chunk_benign_error(png_ptr, "out of place");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002264 return;
2265 }
Chris Craikb50c2172013-07-29 15:28:30 -07002266
The Android Open Source Project893912b2009-03-03 19:30:05 -08002267 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL))
2268 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002269 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002270 png_chunk_benign_error(png_ptr, "duplicate");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002271 return;
2272 }
2273
Eric Vannier66dce0d2011-07-20 17:03:29 -07002274 /* Need unit type, width, \0, height: minimum 4 bytes */
2275 else if (length < 4)
2276 {
Chris Craikb50c2172013-07-29 15:28:30 -07002277 png_crc_finish(png_ptr, length);
2278 png_chunk_benign_error(png_ptr, "invalid");
2279 return;
2280 }
2281
2282 png_debug1(2, "Allocating and reading sCAL chunk data (%u bytes)",
2283 length + 1);
2284
2285 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
2286
2287 if (buffer == NULL)
2288 {
2289 png_chunk_benign_error(png_ptr, "out of memory");
Eric Vannier66dce0d2011-07-20 17:03:29 -07002290 png_crc_finish(png_ptr, length);
2291 return;
2292 }
2293
Chris Craikb50c2172013-07-29 15:28:30 -07002294 png_crc_read(png_ptr, buffer, length);
2295 buffer[length] = 0; /* Null terminate the last string */
The Android Open Source Project893912b2009-03-03 19:30:05 -08002296
2297 if (png_crc_finish(png_ptr, 0))
Chris Craikb50c2172013-07-29 15:28:30 -07002298 return;
2299
2300 /* Validate the unit. */
2301 if (buffer[0] != 1 && buffer[0] != 2)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002302 {
Chris Craikb50c2172013-07-29 15:28:30 -07002303 png_chunk_benign_error(png_ptr, "invalid unit");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002304 return;
2305 }
2306
Chris Craikb50c2172013-07-29 15:28:30 -07002307 /* Validate the ASCII numbers, need two ASCII numbers separated by
2308 * a '\0' and they need to fit exactly in the chunk data.
2309 */
2310 i = 1;
2311 state = 0;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002312
Chris Craikb50c2172013-07-29 15:28:30 -07002313 if (!png_check_fp_number((png_const_charp)buffer, length, &state, &i) ||
2314 i >= length || buffer[i++] != 0)
2315 png_chunk_benign_error(png_ptr, "bad width format");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002316
Chris Craikb50c2172013-07-29 15:28:30 -07002317 else if (!PNG_FP_IS_POSITIVE(state))
2318 png_chunk_benign_error(png_ptr, "non-positive width");
2319
2320 else
The Android Open Source Project893912b2009-03-03 19:30:05 -08002321 {
Chris Craikb50c2172013-07-29 15:28:30 -07002322 png_size_t heighti = i;
2323
2324 state = 0;
2325 if (!png_check_fp_number((png_const_charp)buffer, length, &state, &i) ||
2326 i != length)
2327 png_chunk_benign_error(png_ptr, "bad height format");
2328
2329 else if (!PNG_FP_IS_POSITIVE(state))
2330 png_chunk_benign_error(png_ptr, "non-positive height");
2331
2332 else
2333 /* This is the (only) success case. */
2334 png_set_sCAL_s(png_ptr, info_ptr, buffer[0],
2335 (png_charp)buffer+1, (png_charp)buffer+heighti);
The Android Open Source Project893912b2009-03-03 19:30:05 -08002336 }
The Android Open Source Project893912b2009-03-03 19:30:05 -08002337}
2338#endif
2339
Patrick Scott5f6bd842010-06-28 16:55:16 -04002340#ifdef PNG_READ_tIME_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08002341void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07002342png_handle_tIME(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002343{
2344 png_byte buf[7];
2345 png_time mod_time;
2346
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002347 png_debug(1, "in png_handle_tIME");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002348
2349 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Chris Craikb50c2172013-07-29 15:28:30 -07002350 png_chunk_error(png_ptr, "missing IHDR");
2351
The Android Open Source Project893912b2009-03-03 19:30:05 -08002352 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME))
2353 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002354 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002355 png_chunk_benign_error(png_ptr, "duplicate");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002356 return;
2357 }
2358
2359 if (png_ptr->mode & PNG_HAVE_IDAT)
2360 png_ptr->mode |= PNG_AFTER_IDAT;
2361
2362 if (length != 7)
2363 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08002364 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002365 png_chunk_benign_error(png_ptr, "invalid");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002366 return;
2367 }
2368
2369 png_crc_read(png_ptr, buf, 7);
Chris Craikb50c2172013-07-29 15:28:30 -07002370
The Android Open Source Project893912b2009-03-03 19:30:05 -08002371 if (png_crc_finish(png_ptr, 0))
2372 return;
2373
2374 mod_time.second = buf[6];
2375 mod_time.minute = buf[5];
2376 mod_time.hour = buf[4];
2377 mod_time.day = buf[3];
2378 mod_time.month = buf[2];
2379 mod_time.year = png_get_uint_16(buf);
2380
2381 png_set_tIME(png_ptr, info_ptr, &mod_time);
2382}
2383#endif
2384
Patrick Scott5f6bd842010-06-28 16:55:16 -04002385#ifdef PNG_READ_tEXt_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08002386/* Note: this does not properly handle chunks that are > 64K under DOS */
2387void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07002388png_handle_tEXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002389{
Chris Craikb50c2172013-07-29 15:28:30 -07002390 png_text text_info;
2391 png_bytep buffer;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002392 png_charp key;
2393 png_charp text;
2394 png_uint_32 skip = 0;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002395
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002396 png_debug(1, "in png_handle_tEXt");
2397
Patrick Scott5f6bd842010-06-28 16:55:16 -04002398#ifdef PNG_USER_LIMITS_SUPPORTED
2399 if (png_ptr->user_chunk_cache_max != 0)
2400 {
2401 if (png_ptr->user_chunk_cache_max == 1)
2402 {
2403 png_crc_finish(png_ptr, length);
2404 return;
2405 }
Chris Craikb50c2172013-07-29 15:28:30 -07002406
Patrick Scott5f6bd842010-06-28 16:55:16 -04002407 if (--png_ptr->user_chunk_cache_max == 1)
2408 {
Patrick Scott5f6bd842010-06-28 16:55:16 -04002409 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002410 png_chunk_benign_error(png_ptr, "no space in chunk cache");
Patrick Scott5f6bd842010-06-28 16:55:16 -04002411 return;
2412 }
2413 }
2414#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -08002415
2416 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Chris Craikb50c2172013-07-29 15:28:30 -07002417 png_chunk_error(png_ptr, "missing IHDR");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002418
2419 if (png_ptr->mode & PNG_HAVE_IDAT)
2420 png_ptr->mode |= PNG_AFTER_IDAT;
2421
2422#ifdef PNG_MAX_MALLOC_64K
Chris Craikb50c2172013-07-29 15:28:30 -07002423 if (length > 65535U)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002424 {
Chris Craikb50c2172013-07-29 15:28:30 -07002425 png_crc_finish(png_ptr, length);
2426 png_chunk_benign_error(png_ptr, "too large to fit in memory");
2427 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002428 }
2429#endif
2430
Chris Craikb50c2172013-07-29 15:28:30 -07002431 buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002432
Chris Craikb50c2172013-07-29 15:28:30 -07002433 if (buffer == NULL)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002434 {
Chris Craikb50c2172013-07-29 15:28:30 -07002435 png_chunk_benign_error(png_ptr, "out of memory");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002436 return;
2437 }
Chris Craikb50c2172013-07-29 15:28:30 -07002438
2439 png_crc_read(png_ptr, buffer, length);
The Android Open Source Project893912b2009-03-03 19:30:05 -08002440
2441 if (png_crc_finish(png_ptr, skip))
The Android Open Source Project893912b2009-03-03 19:30:05 -08002442 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002443
Chris Craikb50c2172013-07-29 15:28:30 -07002444 key = (png_charp)buffer;
2445 key[length] = 0;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002446
2447 for (text = key; *text; text++)
Patrick Scotta0bb96c2009-07-22 11:50:02 -04002448 /* Empty loop to find end of key */ ;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002449
Chris Craikb50c2172013-07-29 15:28:30 -07002450 if (text != key + length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002451 text++;
2452
Chris Craikb50c2172013-07-29 15:28:30 -07002453 text_info.compression = PNG_TEXT_COMPRESSION_NONE;
2454 text_info.key = key;
2455 text_info.lang = NULL;
2456 text_info.lang_key = NULL;
2457 text_info.itxt_length = 0;
2458 text_info.text = text;
2459 text_info.text_length = strlen(text);
The Android Open Source Project893912b2009-03-03 19:30:05 -08002460
Chris Craikb50c2172013-07-29 15:28:30 -07002461 if (png_set_text_2(png_ptr, info_ptr, &text_info, 1))
2462 png_warning(png_ptr, "Insufficient memory to process text chunk");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002463}
2464#endif
2465
Patrick Scott5f6bd842010-06-28 16:55:16 -04002466#ifdef PNG_READ_zTXt_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -04002467/* Note: this does not correctly handle chunks that are > 64K under DOS */
The Android Open Source Project893912b2009-03-03 19:30:05 -08002468void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07002469png_handle_zTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002470{
Chris Craikb50c2172013-07-29 15:28:30 -07002471 png_const_charp errmsg = NULL;
2472 png_bytep buffer;
2473 png_uint_32 keyword_length;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002474
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002475 png_debug(1, "in png_handle_zTXt");
2476
Patrick Scott5f6bd842010-06-28 16:55:16 -04002477#ifdef PNG_USER_LIMITS_SUPPORTED
2478 if (png_ptr->user_chunk_cache_max != 0)
2479 {
2480 if (png_ptr->user_chunk_cache_max == 1)
2481 {
2482 png_crc_finish(png_ptr, length);
2483 return;
2484 }
Chris Craikb50c2172013-07-29 15:28:30 -07002485
Patrick Scott5f6bd842010-06-28 16:55:16 -04002486 if (--png_ptr->user_chunk_cache_max == 1)
2487 {
Patrick Scott5f6bd842010-06-28 16:55:16 -04002488 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002489 png_chunk_benign_error(png_ptr, "no space in chunk cache");
Patrick Scott5f6bd842010-06-28 16:55:16 -04002490 return;
2491 }
2492 }
2493#endif
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002494
The Android Open Source Project893912b2009-03-03 19:30:05 -08002495 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Chris Craikb50c2172013-07-29 15:28:30 -07002496 png_chunk_error(png_ptr, "missing IHDR");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002497
2498 if (png_ptr->mode & PNG_HAVE_IDAT)
2499 png_ptr->mode |= PNG_AFTER_IDAT;
2500
Chris Craikb50c2172013-07-29 15:28:30 -07002501 buffer = png_read_buffer(png_ptr, length, 2/*silent*/);
The Android Open Source Project893912b2009-03-03 19:30:05 -08002502
Chris Craikb50c2172013-07-29 15:28:30 -07002503 if (buffer == NULL)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002504 {
Chris Craikb50c2172013-07-29 15:28:30 -07002505 png_crc_finish(png_ptr, length);
2506 png_chunk_benign_error(png_ptr, "out of memory");
2507 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002508 }
Chris Craikb50c2172013-07-29 15:28:30 -07002509
2510 png_crc_read(png_ptr, buffer, length);
2511
The Android Open Source Project893912b2009-03-03 19:30:05 -08002512 if (png_crc_finish(png_ptr, 0))
The Android Open Source Project893912b2009-03-03 19:30:05 -08002513 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002514
Chris Craikb50c2172013-07-29 15:28:30 -07002515 /* TODO: also check that the keyword contents match the spec! */
2516 for (keyword_length = 0;
2517 keyword_length < length && buffer[keyword_length] != 0;
2518 ++keyword_length)
2519 /* Empty loop to find end of name */ ;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002520
Chris Craikb50c2172013-07-29 15:28:30 -07002521 if (keyword_length > 79 || keyword_length < 1)
2522 errmsg = "bad keyword";
The Android Open Source Project893912b2009-03-03 19:30:05 -08002523
Chris Craikb50c2172013-07-29 15:28:30 -07002524 /* zTXt must have some LZ data after the keyword, although it may expand to
2525 * zero bytes; we need a '\0' at the end of the keyword, the compression type
2526 * then the LZ data:
2527 */
2528 else if (keyword_length + 3 > length)
2529 errmsg = "truncated";
2530
2531 else if (buffer[keyword_length+1] != PNG_COMPRESSION_TYPE_BASE)
2532 errmsg = "unknown compression type";
2533
The Android Open Source Project893912b2009-03-03 19:30:05 -08002534 else
2535 {
Chris Craikb50c2172013-07-29 15:28:30 -07002536 png_alloc_size_t uncompressed_length = PNG_SIZE_MAX;
2537
2538 /* TODO: at present png_decompress_chunk imposes a single application
2539 * level memory limit, this should be split to different values for iCCP
2540 * and text chunks.
2541 */
2542 if (png_decompress_chunk(png_ptr, length, keyword_length+2,
2543 &uncompressed_length, 1/*terminate*/) == Z_STREAM_END)
2544 {
2545 png_text text;
2546
2547 /* It worked; png_ptr->read_buffer now looks like a tEXt chunk except
2548 * for the extra compression type byte and the fact that it isn't
2549 * necessarily '\0' terminated.
2550 */
2551 buffer = png_ptr->read_buffer;
2552 buffer[uncompressed_length+(keyword_length+2)] = 0;
2553
2554 text.compression = PNG_TEXT_COMPRESSION_zTXt;
2555 text.key = (png_charp)buffer;
2556 text.text = (png_charp)(buffer + keyword_length+2);
2557 text.text_length = uncompressed_length;
2558 text.itxt_length = 0;
2559 text.lang = NULL;
2560 text.lang_key = NULL;
2561
2562 if (png_set_text_2(png_ptr, info_ptr, &text, 1))
2563 errmsg = "insufficient memory";
2564 }
2565
2566 else
2567 errmsg = png_ptr->zstream.msg;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002568 }
The Android Open Source Project893912b2009-03-03 19:30:05 -08002569
Chris Craikb50c2172013-07-29 15:28:30 -07002570 if (errmsg != NULL)
2571 png_chunk_benign_error(png_ptr, errmsg);
The Android Open Source Project893912b2009-03-03 19:30:05 -08002572}
2573#endif
2574
Patrick Scott5f6bd842010-06-28 16:55:16 -04002575#ifdef PNG_READ_iTXt_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -04002576/* Note: this does not correctly handle chunks that are > 64K under DOS */
The Android Open Source Project893912b2009-03-03 19:30:05 -08002577void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07002578png_handle_iTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002579{
Chris Craikb50c2172013-07-29 15:28:30 -07002580 png_const_charp errmsg = NULL;
2581 png_bytep buffer;
2582 png_uint_32 prefix_length;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002583
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002584 png_debug(1, "in png_handle_iTXt");
2585
Patrick Scott5f6bd842010-06-28 16:55:16 -04002586#ifdef PNG_USER_LIMITS_SUPPORTED
2587 if (png_ptr->user_chunk_cache_max != 0)
2588 {
2589 if (png_ptr->user_chunk_cache_max == 1)
2590 {
2591 png_crc_finish(png_ptr, length);
2592 return;
2593 }
Chris Craikb50c2172013-07-29 15:28:30 -07002594
Patrick Scott5f6bd842010-06-28 16:55:16 -04002595 if (--png_ptr->user_chunk_cache_max == 1)
2596 {
Patrick Scott5f6bd842010-06-28 16:55:16 -04002597 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -07002598 png_chunk_benign_error(png_ptr, "no space in chunk cache");
Patrick Scott5f6bd842010-06-28 16:55:16 -04002599 return;
2600 }
2601 }
2602#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -08002603
2604 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Chris Craikb50c2172013-07-29 15:28:30 -07002605 png_chunk_error(png_ptr, "missing IHDR");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002606
2607 if (png_ptr->mode & PNG_HAVE_IDAT)
2608 png_ptr->mode |= PNG_AFTER_IDAT;
2609
Chris Craikb50c2172013-07-29 15:28:30 -07002610 buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
The Android Open Source Project893912b2009-03-03 19:30:05 -08002611
Chris Craikb50c2172013-07-29 15:28:30 -07002612 if (buffer == NULL)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002613 {
Chris Craikb50c2172013-07-29 15:28:30 -07002614 png_crc_finish(png_ptr, length);
2615 png_chunk_benign_error(png_ptr, "out of memory");
2616 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002617 }
Chris Craikb50c2172013-07-29 15:28:30 -07002618
2619 png_crc_read(png_ptr, buffer, length);
2620
The Android Open Source Project893912b2009-03-03 19:30:05 -08002621 if (png_crc_finish(png_ptr, 0))
The Android Open Source Project893912b2009-03-03 19:30:05 -08002622 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002623
Chris Craikb50c2172013-07-29 15:28:30 -07002624 /* First the keyword. */
2625 for (prefix_length=0;
2626 prefix_length < length && buffer[prefix_length] != 0;
2627 ++prefix_length)
Patrick Scotta0bb96c2009-07-22 11:50:02 -04002628 /* Empty loop */ ;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002629
Chris Craikb50c2172013-07-29 15:28:30 -07002630 /* Perform a basic check on the keyword length here. */
2631 if (prefix_length > 79 || prefix_length < 1)
2632 errmsg = "bad keyword";
2633
2634 /* Expect keyword, compression flag, compression type, language, translated
2635 * keyword (both may be empty but are 0 terminated) then the text, which may
2636 * be empty.
Patrick Scotta0bb96c2009-07-22 11:50:02 -04002637 */
Chris Craikb50c2172013-07-29 15:28:30 -07002638 else if (prefix_length + 5 > length)
2639 errmsg = "truncated";
The Android Open Source Project893912b2009-03-03 19:30:05 -08002640
Chris Craikb50c2172013-07-29 15:28:30 -07002641 else if (buffer[prefix_length+1] == 0 ||
2642 (buffer[prefix_length+1] == 1 &&
2643 buffer[prefix_length+2] == PNG_COMPRESSION_TYPE_BASE))
The Android Open Source Project893912b2009-03-03 19:30:05 -08002644 {
Chris Craikb50c2172013-07-29 15:28:30 -07002645 int compressed = buffer[prefix_length+1] != 0;
2646 png_uint_32 language_offset, translated_keyword_offset;
2647 png_alloc_size_t uncompressed_length = 0;
2648
2649 /* Now the language tag */
2650 prefix_length += 3;
2651 language_offset = prefix_length;
2652
2653 for (; prefix_length < length && buffer[prefix_length] != 0;
2654 ++prefix_length)
2655 /* Empty loop */ ;
2656
2657 /* WARNING: the length may be invalid here, this is checked below. */
2658 translated_keyword_offset = ++prefix_length;
2659
2660 for (; prefix_length < length && buffer[prefix_length] != 0;
2661 ++prefix_length)
2662 /* Empty loop */ ;
2663
2664 /* prefix_length should now be at the trailing '\0' of the translated
2665 * keyword, but it may already be over the end. None of this arithmetic
2666 * can overflow because chunks are at most 2^31 bytes long, but on 16-bit
2667 * systems the available allocaton may overflow.
2668 */
2669 ++prefix_length;
2670
2671 if (!compressed && prefix_length <= length)
2672 uncompressed_length = length - prefix_length;
2673
2674 else if (compressed && prefix_length < length)
2675 {
2676 uncompressed_length = PNG_SIZE_MAX;
2677
2678 /* TODO: at present png_decompress_chunk imposes a single application
2679 * level memory limit, this should be split to different values for
2680 * iCCP and text chunks.
2681 */
2682 if (png_decompress_chunk(png_ptr, length, prefix_length,
2683 &uncompressed_length, 1/*terminate*/) == Z_STREAM_END)
2684 buffer = png_ptr->read_buffer;
2685
2686 else
2687 errmsg = png_ptr->zstream.msg;
2688 }
2689
2690 else
2691 errmsg = "truncated";
2692
2693 if (errmsg == NULL)
2694 {
2695 png_text text;
2696
2697 buffer[uncompressed_length+prefix_length] = 0;
2698
2699 if (compressed)
2700 text.compression = PNG_ITXT_COMPRESSION_NONE;
2701
2702 else
2703 text.compression = PNG_ITXT_COMPRESSION_zTXt;
2704
2705 text.key = (png_charp)buffer;
2706 text.lang = (png_charp)buffer + language_offset;
2707 text.lang_key = (png_charp)buffer + translated_keyword_offset;
2708 text.text = (png_charp)buffer + prefix_length;
2709 text.text_length = 0;
2710 text.itxt_length = uncompressed_length;
2711
2712 if (png_set_text_2(png_ptr, info_ptr, &text, 1))
2713 errmsg = "insufficient memory";
2714 }
The Android Open Source Project893912b2009-03-03 19:30:05 -08002715 }
Chris Craikb50c2172013-07-29 15:28:30 -07002716
The Android Open Source Project893912b2009-03-03 19:30:05 -08002717 else
Chris Craikb50c2172013-07-29 15:28:30 -07002718 errmsg = "bad compression info";
The Android Open Source Project893912b2009-03-03 19:30:05 -08002719
Chris Craikb50c2172013-07-29 15:28:30 -07002720 if (errmsg != NULL)
2721 png_chunk_benign_error(png_ptr, errmsg);
The Android Open Source Project893912b2009-03-03 19:30:05 -08002722}
2723#endif
2724
Chris Craikb50c2172013-07-29 15:28:30 -07002725#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
2726/* Utility function for png_handle_unknown; set up png_ptr::unknown_chunk */
2727static int
2728png_cache_unknown_chunk(png_structrp png_ptr, png_uint_32 length)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002729{
Chris Craikb50c2172013-07-29 15:28:30 -07002730 png_alloc_size_t limit = PNG_SIZE_MAX;
2731
2732 if (png_ptr->unknown_chunk.data != NULL)
2733 {
2734 png_free(png_ptr, png_ptr->unknown_chunk.data);
2735 png_ptr->unknown_chunk.data = NULL;
2736 }
2737
2738# ifdef PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED
2739 if (png_ptr->user_chunk_malloc_max > 0 &&
2740 png_ptr->user_chunk_malloc_max < limit)
2741 limit = png_ptr->user_chunk_malloc_max;
2742
2743# elif PNG_USER_CHUNK_MALLOC_MAX > 0
2744 if (PNG_USER_CHUNK_MALLOC_MAX < limit)
2745 limit = PNG_USER_CHUNK_MALLOC_MAX;
2746# endif
2747
2748 if (length <= limit)
2749 {
2750 PNG_CSTRING_FROM_CHUNK(png_ptr->unknown_chunk.name, png_ptr->chunk_name);
2751 /* The following is safe because of the PNG_SIZE_MAX init above */
2752 png_ptr->unknown_chunk.size = (png_size_t)length/*SAFE*/;
2753 /* 'mode' is a flag array, only the bottom four bits matter here */
2754 png_ptr->unknown_chunk.location = (png_byte)png_ptr->mode/*SAFE*/;
2755
2756 if (length == 0)
2757 png_ptr->unknown_chunk.data = NULL;
2758
2759 else
2760 {
2761 /* Do a 'warn' here - it is handled below. */
2762 png_ptr->unknown_chunk.data = png_voidcast(png_bytep,
2763 png_malloc_warn(png_ptr, length));
2764 }
2765 }
2766
2767 if (png_ptr->unknown_chunk.data == NULL && length > 0)
2768 {
2769 /* This is benign because we clean up correctly */
2770 png_crc_finish(png_ptr, length);
2771 png_chunk_benign_error(png_ptr, "unknown chunk exceeds memory limits");
2772 return 0;
2773 }
2774
2775 else
2776 {
2777 if (length > 0)
2778 png_crc_read(png_ptr, png_ptr->unknown_chunk.data, length);
2779 png_crc_finish(png_ptr, 0);
2780 return 1;
2781 }
2782}
2783#endif /* PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */
2784
2785/* Handle an unknown, or known but disabled, chunk */
2786void /* PRIVATE */
2787png_handle_unknown(png_structrp png_ptr, png_inforp info_ptr,
2788 png_uint_32 length, int keep)
2789{
2790 int handled = 0; /* the chunk was handled */
The Android Open Source Project893912b2009-03-03 19:30:05 -08002791
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002792 png_debug(1, "in png_handle_unknown");
2793
Patrick Scott5f6bd842010-06-28 16:55:16 -04002794#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07002795 /* NOTE: this code is based on the code in libpng-1.4.12 except for fixing
2796 * the bug which meant that setting a non-default behavior for a specific
2797 * chunk would be ignored (the default was always used unless a user
2798 * callback was installed).
2799 *
2800 * 'keep' is the value from the png_chunk_unknown_handling, the setting for
2801 * this specific chunk_name, if PNG_HANDLE_AS_UNKNOWN_SUPPORTED, if not it
2802 * will always be PNG_HANDLE_CHUNK_AS_DEFAULT and it needs to be set here.
2803 * This is just an optimization to avoid multiple calls to the lookup
2804 * function.
2805 */
2806# ifndef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
2807# ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
2808 keep = png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name);
2809# endif
2810# endif
The Android Open Source Project893912b2009-03-03 19:30:05 -08002811
Chris Craikb50c2172013-07-29 15:28:30 -07002812 /* One of the following methods will read the chunk or skip it (at least one
2813 * of these is always defined because this is the only way to switch on
2814 * PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
2815 */
2816# ifdef PNG_READ_USER_CHUNKS_SUPPORTED
2817 /* The user callback takes precedence over the chunk keep value, but the
2818 * keep value is still required to validate a save of a critical chunk.
2819 */
2820 if (png_ptr->read_user_chunk_fn != NULL)
2821 {
2822 if (png_cache_unknown_chunk(png_ptr, length))
2823 {
2824 /* Callback to user unknown chunk handler */
2825 int ret = (*(png_ptr->read_user_chunk_fn))(png_ptr,
2826 &png_ptr->unknown_chunk);
The Android Open Source Project893912b2009-03-03 19:30:05 -08002827
Chris Craikb50c2172013-07-29 15:28:30 -07002828 /* ret is:
2829 * negative: An error occured, png_chunk_error will be called.
2830 * zero: The chunk was not handled, the chunk will be discarded
2831 * unless png_set_keep_unknown_chunks has been used to set
2832 * a 'keep' behavior for this particular chunk, in which
2833 * case that will be used. A critical chunk will cause an
2834 * error at this point unless it is to be saved.
2835 * positive: The chunk was handled, libpng will ignore/discard it.
2836 */
2837 if (ret < 0)
2838 png_chunk_error(png_ptr, "error in user chunk");
2839
2840 else if (ret == 0)
2841 {
2842 /* If the keep value is 'default' or 'never' override it, but
2843 * still error out on critical chunks unless the keep value is
2844 * 'always' While this is weird it is the behavior in 1.4.12.
2845 * A possible improvement would be to obey the value set for the
2846 * chunk, but this would be an API change that would probably
2847 * damage some applications.
2848 *
2849 * The png_app_warning below catches the case that matters, where
2850 * the application has not set specific save or ignore for this
2851 * chunk or global save or ignore.
2852 */
2853 if (keep < PNG_HANDLE_CHUNK_IF_SAFE)
2854 {
2855# ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
2856 if (png_ptr->unknown_default < PNG_HANDLE_CHUNK_IF_SAFE)
2857 {
2858 png_chunk_warning(png_ptr, "Saving unknown chunk:");
2859 png_app_warning(png_ptr,
2860 "forcing save of an unhandled chunk;"
2861 " please call png_set_keep_unknown_chunks");
2862 /* with keep = PNG_HANDLE_CHUNK_IF_SAFE */
2863 }
2864# endif
2865 keep = PNG_HANDLE_CHUNK_IF_SAFE;
2866 }
2867 }
2868
2869 else /* chunk was handled */
2870 {
2871 handled = 1;
2872 /* Critical chunks can be safely discarded at this point. */
2873 keep = PNG_HANDLE_CHUNK_NEVER;
2874 }
2875 }
2876
2877 else
2878 keep = PNG_HANDLE_CHUNK_NEVER; /* insufficient memory */
2879 }
2880
2881 else
2882 /* Use the SAVE_UNKNOWN_CHUNKS code or skip the chunk */
2883# endif /* PNG_READ_USER_CHUNKS_SUPPORTED */
2884
2885# ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
2886 {
2887 /* keep is currently just the per-chunk setting, if there was no
2888 * setting change it to the global default now (not that this may
2889 * still be AS_DEFAULT) then obtain the cache of the chunk if required,
2890 * if not simply skip the chunk.
2891 */
2892 if (keep == PNG_HANDLE_CHUNK_AS_DEFAULT)
2893 keep = png_ptr->unknown_default;
2894
2895 if (keep == PNG_HANDLE_CHUNK_ALWAYS ||
2896 (keep == PNG_HANDLE_CHUNK_IF_SAFE &&
2897 PNG_CHUNK_ANCILLARY(png_ptr->chunk_name)))
2898 {
2899 if (!png_cache_unknown_chunk(png_ptr, length))
2900 keep = PNG_HANDLE_CHUNK_NEVER;
2901 }
2902
2903 else
2904 png_crc_finish(png_ptr, length);
2905 }
2906# else
2907# ifndef PNG_READ_USER_CHUNKS_SUPPORTED
2908# error no method to support READ_UNKNOWN_CHUNKS
2909# endif
2910
2911 {
2912 /* If here there is no read callback pointer set and no support is
2913 * compiled in to just save the unknown chunks, so simply skip this
2914 * chunk. If 'keep' is something other than AS_DEFAULT or NEVER then
2915 * the app has erroneously asked for unknown chunk saving when there
2916 * is no support.
2917 */
2918 if (keep > PNG_HANDLE_CHUNK_NEVER)
2919 png_app_error(png_ptr, "no unknown chunk support available");
2920
2921 png_crc_finish(png_ptr, length);
2922 }
2923# endif
2924
2925# ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
2926 /* Now store the chunk in the chunk list if appropriate, and if the limits
2927 * permit it.
2928 */
2929 if (keep == PNG_HANDLE_CHUNK_ALWAYS ||
2930 (keep == PNG_HANDLE_CHUNK_IF_SAFE &&
2931 PNG_CHUNK_ANCILLARY(png_ptr->chunk_name)))
2932 {
2933# ifdef PNG_USER_LIMITS_SUPPORTED
2934 switch (png_ptr->user_chunk_cache_max)
2935 {
2936 case 2:
2937 png_ptr->user_chunk_cache_max = 1;
2938 png_chunk_benign_error(png_ptr, "no space in chunk cache");
2939 /* FALL THROUGH */
2940 case 1:
2941 /* NOTE: prior to 1.6.0 this case resulted in an unknown critical
2942 * chunk being skipped, now there will be a hard error below.
2943 */
2944 break;
2945
2946 default: /* not at limit */
2947 --(png_ptr->user_chunk_cache_max);
2948 /* FALL THROUGH */
2949 case 0: /* no limit */
2950# endif /* PNG_USER_LIMITS_SUPPORTED */
2951 /* Here when the limit isn't reached or when limits are compiled
2952 * out; store the chunk.
2953 */
2954 png_set_unknown_chunks(png_ptr, info_ptr,
2955 &png_ptr->unknown_chunk, 1);
2956 handled = 1;
2957# ifdef PNG_USER_LIMITS_SUPPORTED
2958 break;
2959 }
2960# endif
2961 }
2962# else /* no store support! */
2963 PNG_UNUSED(info_ptr)
2964# error untested code (reading unknown chunks with no store support)
2965# endif
2966
2967 /* Regardless of the error handling below the cached data (if any) can be
2968 * freed now. Notice that the data is not freed if there is a png_error, but
2969 * it will be freed by destroy_read_struct.
2970 */
2971 if (png_ptr->unknown_chunk.data != NULL)
2972 png_free(png_ptr, png_ptr->unknown_chunk.data);
2973 png_ptr->unknown_chunk.data = NULL;
2974
2975#else /* !PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */
2976 /* There is no support to read an unknown chunk, so just skip it. */
2977 png_crc_finish(png_ptr, length);
2978 PNG_UNUSED(info_ptr)
2979 PNG_UNUSED(keep)
2980#endif /* !PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */
2981
2982 /* Check for unhandled critical chunks */
2983 if (!handled && PNG_CHUNK_CRITICAL(png_ptr->chunk_name))
2984 png_chunk_error(png_ptr, "unhandled critical chunk");
The Android Open Source Project893912b2009-03-03 19:30:05 -08002985}
2986
2987/* This function is called to verify that a chunk name is valid.
Chris Craikb50c2172013-07-29 15:28:30 -07002988 * This function can't have the "critical chunk check" incorporated
2989 * into it, since in the future we will need to be able to call user
2990 * functions to handle unknown critical chunks after we check that
2991 * the chunk name itself is valid.
2992 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08002993
Chris Craikb50c2172013-07-29 15:28:30 -07002994/* Bit hacking: the test for an invalid byte in the 4 byte chunk name is:
2995 *
2996 * ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
2997 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08002998
2999void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07003000png_check_chunk_name(png_structrp png_ptr, png_uint_32 chunk_name)
The Android Open Source Project893912b2009-03-03 19:30:05 -08003001{
Chris Craikb50c2172013-07-29 15:28:30 -07003002 int i;
3003
The Android Open Source Project4215dd12009-03-09 11:52:12 -07003004 png_debug(1, "in png_check_chunk_name");
Chris Craikb50c2172013-07-29 15:28:30 -07003005
3006 for (i=1; i<=4; ++i)
The Android Open Source Project893912b2009-03-03 19:30:05 -08003007 {
Chris Craikb50c2172013-07-29 15:28:30 -07003008 int c = chunk_name & 0xff;
3009
3010 if (c < 65 || c > 122 || (c > 90 && c < 97))
3011 png_chunk_error(png_ptr, "invalid chunk type");
3012
3013 chunk_name >>= 8;
The Android Open Source Project893912b2009-03-03 19:30:05 -08003014 }
3015}
3016
Chris Craikb50c2172013-07-29 15:28:30 -07003017/* Combines the row recently read in with the existing pixels in the row. This
3018 * routine takes care of alpha and transparency if requested. This routine also
3019 * handles the two methods of progressive display of interlaced images,
3020 * depending on the 'display' value; if 'display' is true then the whole row
3021 * (dp) is filled from the start by replicating the available pixels. If
3022 * 'display' is false only those pixels present in the pass are filled in.
3023 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08003024void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07003025png_combine_row(png_const_structrp png_ptr, png_bytep dp, int display)
The Android Open Source Project893912b2009-03-03 19:30:05 -08003026{
Chris Craikb50c2172013-07-29 15:28:30 -07003027 unsigned int pixel_depth = png_ptr->transformed_pixel_depth;
3028 png_const_bytep sp = png_ptr->row_buf + 1;
3029 png_uint_32 row_width = png_ptr->width;
3030 unsigned int pass = png_ptr->pass;
3031 png_bytep end_ptr = 0;
3032 png_byte end_byte = 0;
3033 unsigned int end_mask;
3034
The Android Open Source Project4215dd12009-03-09 11:52:12 -07003035 png_debug(1, "in png_combine_row");
Chris Craikb50c2172013-07-29 15:28:30 -07003036
3037 /* Added in 1.5.6: it should not be possible to enter this routine until at
3038 * least one row has been read from the PNG data and transformed.
3039 */
3040 if (pixel_depth == 0)
3041 png_error(png_ptr, "internal row logic error");
3042
3043 /* Added in 1.5.4: the pixel depth should match the information returned by
3044 * any call to png_read_update_info at this point. Do not continue if we got
3045 * this wrong.
3046 */
3047 if (png_ptr->info_rowbytes != 0 && png_ptr->info_rowbytes !=
3048 PNG_ROWBYTES(pixel_depth, row_width))
3049 png_error(png_ptr, "internal row size calculation error");
3050
3051 /* Don't expect this to ever happen: */
3052 if (row_width == 0)
3053 png_error(png_ptr, "internal row width error");
3054
3055 /* Preserve the last byte in cases where only part of it will be overwritten,
3056 * the multiply below may overflow, we don't care because ANSI-C guarantees
3057 * we get the low bits.
3058 */
3059 end_mask = (pixel_depth * row_width) & 7;
3060 if (end_mask != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08003061 {
Chris Craikb50c2172013-07-29 15:28:30 -07003062 /* end_ptr == NULL is a flag to say do nothing */
3063 end_ptr = dp + PNG_ROWBYTES(pixel_depth, row_width) - 1;
3064 end_byte = *end_ptr;
3065# ifdef PNG_READ_PACKSWAP_SUPPORTED
3066 if (png_ptr->transformations & PNG_PACKSWAP) /* little-endian byte */
3067 end_mask = 0xff << end_mask;
3068
3069 else /* big-endian byte */
3070# endif
3071 end_mask = 0xff >> end_mask;
3072 /* end_mask is now the bits to *keep* from the destination row */
The Android Open Source Project893912b2009-03-03 19:30:05 -08003073 }
Chris Craikb50c2172013-07-29 15:28:30 -07003074
3075 /* For non-interlaced images this reduces to a memcpy(). A memcpy()
3076 * will also happen if interlacing isn't supported or if the application
3077 * does not call png_set_interlace_handling(). In the latter cases the
3078 * caller just gets a sequence of the unexpanded rows from each interlace
3079 * pass.
3080 */
3081#ifdef PNG_READ_INTERLACING_SUPPORTED
3082 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE) &&
3083 pass < 6 && (display == 0 ||
3084 /* The following copies everything for 'display' on passes 0, 2 and 4. */
3085 (display == 1 && (pass & 1) != 0)))
The Android Open Source Project893912b2009-03-03 19:30:05 -08003086 {
Chris Craikb50c2172013-07-29 15:28:30 -07003087 /* Narrow images may have no bits in a pass; the caller should handle
3088 * this, but this test is cheap:
3089 */
3090 if (row_width <= PNG_PASS_START_COL(pass))
3091 return;
3092
3093 if (pixel_depth < 8)
The Android Open Source Project893912b2009-03-03 19:30:05 -08003094 {
Chris Craikb50c2172013-07-29 15:28:30 -07003095 /* For pixel depths up to 4 bpp the 8-pixel mask can be expanded to fit
3096 * into 32 bits, then a single loop over the bytes using the four byte
3097 * values in the 32-bit mask can be used. For the 'display' option the
3098 * expanded mask may also not require any masking within a byte. To
3099 * make this work the PACKSWAP option must be taken into account - it
3100 * simply requires the pixels to be reversed in each byte.
3101 *
3102 * The 'regular' case requires a mask for each of the first 6 passes,
3103 * the 'display' case does a copy for the even passes in the range
3104 * 0..6. This has already been handled in the test above.
3105 *
3106 * The masks are arranged as four bytes with the first byte to use in
3107 * the lowest bits (little-endian) regardless of the order (PACKSWAP or
3108 * not) of the pixels in each byte.
3109 *
3110 * NOTE: the whole of this logic depends on the caller of this function
3111 * only calling it on rows appropriate to the pass. This function only
3112 * understands the 'x' logic; the 'y' logic is handled by the caller.
3113 *
3114 * The following defines allow generation of compile time constant bit
3115 * masks for each pixel depth and each possibility of swapped or not
3116 * swapped bytes. Pass 'p' is in the range 0..6; 'x', a pixel index,
3117 * is in the range 0..7; and the result is 1 if the pixel is to be
3118 * copied in the pass, 0 if not. 'S' is for the sparkle method, 'B'
3119 * for the block method.
3120 *
3121 * With some compilers a compile time expression of the general form:
3122 *
3123 * (shift >= 32) ? (a >> (shift-32)) : (b >> shift)
3124 *
3125 * Produces warnings with values of 'shift' in the range 33 to 63
3126 * because the right hand side of the ?: expression is evaluated by
3127 * the compiler even though it isn't used. Microsoft Visual C (various
3128 * versions) and the Intel C compiler are known to do this. To avoid
3129 * this the following macros are used in 1.5.6. This is a temporary
3130 * solution to avoid destabilizing the code during the release process.
3131 */
3132# if PNG_USE_COMPILE_TIME_MASKS
3133# define PNG_LSR(x,s) ((x)>>((s) & 0x1f))
3134# define PNG_LSL(x,s) ((x)<<((s) & 0x1f))
3135# else
3136# define PNG_LSR(x,s) ((x)>>(s))
3137# define PNG_LSL(x,s) ((x)<<(s))
3138# endif
3139# define S_COPY(p,x) (((p)<4 ? PNG_LSR(0x80088822,(3-(p))*8+(7-(x))) :\
3140 PNG_LSR(0xaa55ff00,(7-(p))*8+(7-(x)))) & 1)
3141# define B_COPY(p,x) (((p)<4 ? PNG_LSR(0xff0fff33,(3-(p))*8+(7-(x))) :\
3142 PNG_LSR(0xff55ff00,(7-(p))*8+(7-(x)))) & 1)
The Android Open Source Project893912b2009-03-03 19:30:05 -08003143
Chris Craikb50c2172013-07-29 15:28:30 -07003144 /* Return a mask for pass 'p' pixel 'x' at depth 'd'. The mask is
3145 * little endian - the first pixel is at bit 0 - however the extra
3146 * parameter 's' can be set to cause the mask position to be swapped
3147 * within each byte, to match the PNG format. This is done by XOR of
3148 * the shift with 7, 6 or 4 for bit depths 1, 2 and 4.
3149 */
3150# define PIXEL_MASK(p,x,d,s) \
3151 (PNG_LSL(((PNG_LSL(1U,(d)))-1),(((x)*(d))^((s)?8-(d):0))))
3152
3153 /* Hence generate the appropriate 'block' or 'sparkle' pixel copy mask.
3154 */
3155# define S_MASKx(p,x,d,s) (S_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
3156# define B_MASKx(p,x,d,s) (B_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
3157
3158 /* Combine 8 of these to get the full mask. For the 1-bpp and 2-bpp
3159 * cases the result needs replicating, for the 4-bpp case the above
3160 * generates a full 32 bits.
3161 */
3162# define MASK_EXPAND(m,d) ((m)*((d)==1?0x01010101:((d)==2?0x00010001:1)))
3163
3164# define S_MASK(p,d,s) MASK_EXPAND(S_MASKx(p,0,d,s) + S_MASKx(p,1,d,s) +\
3165 S_MASKx(p,2,d,s) + S_MASKx(p,3,d,s) + S_MASKx(p,4,d,s) +\
3166 S_MASKx(p,5,d,s) + S_MASKx(p,6,d,s) + S_MASKx(p,7,d,s), d)
3167
3168# define B_MASK(p,d,s) MASK_EXPAND(B_MASKx(p,0,d,s) + B_MASKx(p,1,d,s) +\
3169 B_MASKx(p,2,d,s) + B_MASKx(p,3,d,s) + B_MASKx(p,4,d,s) +\
3170 B_MASKx(p,5,d,s) + B_MASKx(p,6,d,s) + B_MASKx(p,7,d,s), d)
3171
3172#if PNG_USE_COMPILE_TIME_MASKS
3173 /* Utility macros to construct all the masks for a depth/swap
3174 * combination. The 's' parameter says whether the format is PNG
3175 * (big endian bytes) or not. Only the three odd-numbered passes are
3176 * required for the display/block algorithm.
3177 */
3178# define S_MASKS(d,s) { S_MASK(0,d,s), S_MASK(1,d,s), S_MASK(2,d,s),\
3179 S_MASK(3,d,s), S_MASK(4,d,s), S_MASK(5,d,s) }
3180
3181# define B_MASKS(d,s) { B_MASK(1,d,s), S_MASK(3,d,s), S_MASK(5,d,s) }
3182
3183# define DEPTH_INDEX(d) ((d)==1?0:((d)==2?1:2))
3184
3185 /* Hence the pre-compiled masks indexed by PACKSWAP (or not), depth and
3186 * then pass:
3187 */
3188 static PNG_CONST png_uint_32 row_mask[2/*PACKSWAP*/][3/*depth*/][6] =
3189 {
3190 /* Little-endian byte masks for PACKSWAP */
3191 { S_MASKS(1,0), S_MASKS(2,0), S_MASKS(4,0) },
3192 /* Normal (big-endian byte) masks - PNG format */
3193 { S_MASKS(1,1), S_MASKS(2,1), S_MASKS(4,1) }
3194 };
3195
3196 /* display_mask has only three entries for the odd passes, so index by
3197 * pass>>1.
3198 */
3199 static PNG_CONST png_uint_32 display_mask[2][3][3] =
3200 {
3201 /* Little-endian byte masks for PACKSWAP */
3202 { B_MASKS(1,0), B_MASKS(2,0), B_MASKS(4,0) },
3203 /* Normal (big-endian byte) masks - PNG format */
3204 { B_MASKS(1,1), B_MASKS(2,1), B_MASKS(4,1) }
3205 };
3206
3207# define MASK(pass,depth,display,png)\
3208 ((display)?display_mask[png][DEPTH_INDEX(depth)][pass>>1]:\
3209 row_mask[png][DEPTH_INDEX(depth)][pass])
3210
3211#else /* !PNG_USE_COMPILE_TIME_MASKS */
3212 /* This is the runtime alternative: it seems unlikely that this will
3213 * ever be either smaller or faster than the compile time approach.
3214 */
3215# define MASK(pass,depth,display,png)\
3216 ((display)?B_MASK(pass,depth,png):S_MASK(pass,depth,png))
3217#endif /* !PNG_USE_COMPILE_TIME_MASKS */
3218
3219 /* Use the appropriate mask to copy the required bits. In some cases
3220 * the byte mask will be 0 or 0xff, optimize these cases. row_width is
3221 * the number of pixels, but the code copies bytes, so it is necessary
3222 * to special case the end.
3223 */
3224 png_uint_32 pixels_per_byte = 8 / pixel_depth;
3225 png_uint_32 mask;
3226
3227# ifdef PNG_READ_PACKSWAP_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08003228 if (png_ptr->transformations & PNG_PACKSWAP)
Chris Craikb50c2172013-07-29 15:28:30 -07003229 mask = MASK(pass, pixel_depth, display, 0);
3230
The Android Open Source Project893912b2009-03-03 19:30:05 -08003231 else
Chris Craikb50c2172013-07-29 15:28:30 -07003232# endif
3233 mask = MASK(pass, pixel_depth, display, 1);
The Android Open Source Project893912b2009-03-03 19:30:05 -08003234
Chris Craikb50c2172013-07-29 15:28:30 -07003235 for (;;)
The Android Open Source Project893912b2009-03-03 19:30:05 -08003236 {
Chris Craikb50c2172013-07-29 15:28:30 -07003237 png_uint_32 m;
The Android Open Source Project893912b2009-03-03 19:30:05 -08003238
Chris Craikb50c2172013-07-29 15:28:30 -07003239 /* It doesn't matter in the following if png_uint_32 has more than
3240 * 32 bits because the high bits always match those in m<<24; it is,
3241 * however, essential to use OR here, not +, because of this.
3242 */
3243 m = mask;
3244 mask = (m >> 8) | (m << 24); /* rotate right to good compilers */
3245 m &= 0xff;
3246
3247 if (m != 0) /* something to copy */
The Android Open Source Project893912b2009-03-03 19:30:05 -08003248 {
Chris Craikb50c2172013-07-29 15:28:30 -07003249 if (m != 0xff)
3250 *dp = (png_byte)((*dp & ~m) | (*sp & m));
The Android Open Source Project893912b2009-03-03 19:30:05 -08003251 else
Chris Craikb50c2172013-07-29 15:28:30 -07003252 *dp = *sp;
The Android Open Source Project893912b2009-03-03 19:30:05 -08003253 }
The Android Open Source Project893912b2009-03-03 19:30:05 -08003254
Chris Craikb50c2172013-07-29 15:28:30 -07003255 /* NOTE: this may overwrite the last byte with garbage if the image
3256 * is not an exact number of bytes wide; libpng has always done
3257 * this.
3258 */
3259 if (row_width <= pixels_per_byte)
3260 break; /* May need to restore part of the last byte */
The Android Open Source Project893912b2009-03-03 19:30:05 -08003261
Chris Craikb50c2172013-07-29 15:28:30 -07003262 row_width -= pixels_per_byte;
3263 ++dp;
3264 ++sp;
The Android Open Source Project893912b2009-03-03 19:30:05 -08003265 }
3266 }
Chris Craikb50c2172013-07-29 15:28:30 -07003267
3268 else /* pixel_depth >= 8 */
3269 {
3270 unsigned int bytes_to_copy, bytes_to_jump;
3271
3272 /* Validate the depth - it must be a multiple of 8 */
3273 if (pixel_depth & 7)
3274 png_error(png_ptr, "invalid user transform pixel depth");
3275
3276 pixel_depth >>= 3; /* now in bytes */
3277 row_width *= pixel_depth;
3278
3279 /* Regardless of pass number the Adam 7 interlace always results in a
3280 * fixed number of pixels to copy then to skip. There may be a
3281 * different number of pixels to skip at the start though.
3282 */
3283 {
3284 unsigned int offset = PNG_PASS_START_COL(pass) * pixel_depth;
3285
3286 row_width -= offset;
3287 dp += offset;
3288 sp += offset;
3289 }
3290
3291 /* Work out the bytes to copy. */
3292 if (display)
3293 {
3294 /* When doing the 'block' algorithm the pixel in the pass gets
3295 * replicated to adjacent pixels. This is why the even (0,2,4,6)
3296 * passes are skipped above - the entire expanded row is copied.
3297 */
3298 bytes_to_copy = (1<<((6-pass)>>1)) * pixel_depth;
3299
3300 /* But don't allow this number to exceed the actual row width. */
3301 if (bytes_to_copy > row_width)
3302 bytes_to_copy = row_width;
3303 }
3304
3305 else /* normal row; Adam7 only ever gives us one pixel to copy. */
3306 bytes_to_copy = pixel_depth;
3307
3308 /* In Adam7 there is a constant offset between where the pixels go. */
3309 bytes_to_jump = PNG_PASS_COL_OFFSET(pass) * pixel_depth;
3310
3311 /* And simply copy these bytes. Some optimization is possible here,
3312 * depending on the value of 'bytes_to_copy'. Special case the low
3313 * byte counts, which we know to be frequent.
3314 *
3315 * Notice that these cases all 'return' rather than 'break' - this
3316 * avoids an unnecessary test on whether to restore the last byte
3317 * below.
3318 */
3319 switch (bytes_to_copy)
3320 {
3321 case 1:
3322 for (;;)
3323 {
3324 *dp = *sp;
3325
3326 if (row_width <= bytes_to_jump)
3327 return;
3328
3329 dp += bytes_to_jump;
3330 sp += bytes_to_jump;
3331 row_width -= bytes_to_jump;
3332 }
3333
3334 case 2:
3335 /* There is a possibility of a partial copy at the end here; this
3336 * slows the code down somewhat.
3337 */
3338 do
3339 {
3340 dp[0] = sp[0], dp[1] = sp[1];
3341
3342 if (row_width <= bytes_to_jump)
3343 return;
3344
3345 sp += bytes_to_jump;
3346 dp += bytes_to_jump;
3347 row_width -= bytes_to_jump;
3348 }
3349 while (row_width > 1);
3350
3351 /* And there can only be one byte left at this point: */
3352 *dp = *sp;
3353 return;
3354
3355 case 3:
3356 /* This can only be the RGB case, so each copy is exactly one
3357 * pixel and it is not necessary to check for a partial copy.
3358 */
3359 for(;;)
3360 {
3361 dp[0] = sp[0], dp[1] = sp[1], dp[2] = sp[2];
3362
3363 if (row_width <= bytes_to_jump)
3364 return;
3365
3366 sp += bytes_to_jump;
3367 dp += bytes_to_jump;
3368 row_width -= bytes_to_jump;
3369 }
3370
3371 default:
3372#if PNG_ALIGN_TYPE != PNG_ALIGN_NONE
3373 /* Check for double byte alignment and, if possible, use a
3374 * 16-bit copy. Don't attempt this for narrow images - ones that
3375 * are less than an interlace panel wide. Don't attempt it for
3376 * wide bytes_to_copy either - use the memcpy there.
3377 */
3378 if (bytes_to_copy < 16 /*else use memcpy*/ &&
3379 png_isaligned(dp, png_uint_16) &&
3380 png_isaligned(sp, png_uint_16) &&
3381 bytes_to_copy % (sizeof (png_uint_16)) == 0 &&
3382 bytes_to_jump % (sizeof (png_uint_16)) == 0)
3383 {
3384 /* Everything is aligned for png_uint_16 copies, but try for
3385 * png_uint_32 first.
3386 */
3387 if (png_isaligned(dp, png_uint_32) &&
3388 png_isaligned(sp, png_uint_32) &&
3389 bytes_to_copy % (sizeof (png_uint_32)) == 0 &&
3390 bytes_to_jump % (sizeof (png_uint_32)) == 0)
3391 {
3392 png_uint_32p dp32 = png_aligncast(png_uint_32p,dp);
3393 png_const_uint_32p sp32 = png_aligncastconst(
3394 png_const_uint_32p, sp);
3395 size_t skip = (bytes_to_jump-bytes_to_copy) /
3396 (sizeof (png_uint_32));
3397
3398 do
3399 {
3400 size_t c = bytes_to_copy;
3401 do
3402 {
3403 *dp32++ = *sp32++;
3404 c -= (sizeof (png_uint_32));
3405 }
3406 while (c > 0);
3407
3408 if (row_width <= bytes_to_jump)
3409 return;
3410
3411 dp32 += skip;
3412 sp32 += skip;
3413 row_width -= bytes_to_jump;
3414 }
3415 while (bytes_to_copy <= row_width);
3416
3417 /* Get to here when the row_width truncates the final copy.
3418 * There will be 1-3 bytes left to copy, so don't try the
3419 * 16-bit loop below.
3420 */
3421 dp = (png_bytep)dp32;
3422 sp = (png_const_bytep)sp32;
3423 do
3424 *dp++ = *sp++;
3425 while (--row_width > 0);
3426 return;
3427 }
3428
3429 /* Else do it in 16-bit quantities, but only if the size is
3430 * not too large.
3431 */
3432 else
3433 {
3434 png_uint_16p dp16 = png_aligncast(png_uint_16p, dp);
3435 png_const_uint_16p sp16 = png_aligncastconst(
3436 png_const_uint_16p, sp);
3437 size_t skip = (bytes_to_jump-bytes_to_copy) /
3438 (sizeof (png_uint_16));
3439
3440 do
3441 {
3442 size_t c = bytes_to_copy;
3443 do
3444 {
3445 *dp16++ = *sp16++;
3446 c -= (sizeof (png_uint_16));
3447 }
3448 while (c > 0);
3449
3450 if (row_width <= bytes_to_jump)
3451 return;
3452
3453 dp16 += skip;
3454 sp16 += skip;
3455 row_width -= bytes_to_jump;
3456 }
3457 while (bytes_to_copy <= row_width);
3458
3459 /* End of row - 1 byte left, bytes_to_copy > row_width: */
3460 dp = (png_bytep)dp16;
3461 sp = (png_const_bytep)sp16;
3462 do
3463 *dp++ = *sp++;
3464 while (--row_width > 0);
3465 return;
3466 }
3467 }
3468#endif /* PNG_ALIGN_ code */
3469
3470 /* The true default - use a memcpy: */
3471 for (;;)
3472 {
3473 memcpy(dp, sp, bytes_to_copy);
3474
3475 if (row_width <= bytes_to_jump)
3476 return;
3477
3478 sp += bytes_to_jump;
3479 dp += bytes_to_jump;
3480 row_width -= bytes_to_jump;
3481 if (bytes_to_copy > row_width)
3482 bytes_to_copy = row_width;
3483 }
3484 }
3485
3486 /* NOT REACHED*/
3487 } /* pixel_depth >= 8 */
3488
3489 /* Here if pixel_depth < 8 to check 'end_ptr' below. */
The Android Open Source Project893912b2009-03-03 19:30:05 -08003490 }
Chris Craikb50c2172013-07-29 15:28:30 -07003491 else
3492#endif
3493
3494 /* If here then the switch above wasn't used so just memcpy the whole row
3495 * from the temporary row buffer (notice that this overwrites the end of the
3496 * destination row if it is a partial byte.)
3497 */
3498 memcpy(dp, sp, PNG_ROWBYTES(pixel_depth, row_width));
3499
3500 /* Restore the overwritten bits from the last byte if necessary. */
3501 if (end_ptr != NULL)
3502 *end_ptr = (png_byte)((end_byte & end_mask) | (*end_ptr & ~end_mask));
The Android Open Source Project893912b2009-03-03 19:30:05 -08003503}
3504
3505#ifdef PNG_READ_INTERLACING_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08003506void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07003507png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
3508 png_uint_32 transformations /* Because these may affect the byte layout */)
The Android Open Source Project893912b2009-03-03 19:30:05 -08003509{
Patrick Scotta0bb96c2009-07-22 11:50:02 -04003510 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
3511 /* Offset to next interlace block */
Chris Craikb50c2172013-07-29 15:28:30 -07003512 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 -08003513
The Android Open Source Project4215dd12009-03-09 11:52:12 -07003514 png_debug(1, "in png_do_read_interlace");
The Android Open Source Project893912b2009-03-03 19:30:05 -08003515 if (row != NULL && row_info != NULL)
3516 {
3517 png_uint_32 final_width;
3518
3519 final_width = row_info->width * png_pass_inc[pass];
3520
3521 switch (row_info->pixel_depth)
3522 {
3523 case 1:
3524 {
3525 png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3);
3526 png_bytep dp = row + (png_size_t)((final_width - 1) >> 3);
3527 int sshift, dshift;
3528 int s_start, s_end, s_inc;
3529 int jstop = png_pass_inc[pass];
3530 png_byte v;
3531 png_uint_32 i;
3532 int j;
3533
Patrick Scott5f6bd842010-06-28 16:55:16 -04003534#ifdef PNG_READ_PACKSWAP_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08003535 if (transformations & PNG_PACKSWAP)
3536 {
3537 sshift = (int)((row_info->width + 7) & 0x07);
3538 dshift = (int)((final_width + 7) & 0x07);
3539 s_start = 7;
3540 s_end = 0;
3541 s_inc = -1;
3542 }
Chris Craikb50c2172013-07-29 15:28:30 -07003543
The Android Open Source Project893912b2009-03-03 19:30:05 -08003544 else
3545#endif
3546 {
3547 sshift = 7 - (int)((row_info->width + 7) & 0x07);
3548 dshift = 7 - (int)((final_width + 7) & 0x07);
3549 s_start = 0;
3550 s_end = 7;
3551 s_inc = 1;
3552 }
3553
3554 for (i = 0; i < row_info->width; i++)
3555 {
3556 v = (png_byte)((*sp >> sshift) & 0x01);
3557 for (j = 0; j < jstop; j++)
3558 {
Chris Craikb50c2172013-07-29 15:28:30 -07003559 unsigned int tmp = *dp & (0x7f7f >> (7 - dshift));
3560 tmp |= v << dshift;
3561 *dp = (png_byte)(tmp & 0xff);
3562
The Android Open Source Project893912b2009-03-03 19:30:05 -08003563 if (dshift == s_end)
3564 {
3565 dshift = s_start;
3566 dp--;
3567 }
Chris Craikb50c2172013-07-29 15:28:30 -07003568
The Android Open Source Project893912b2009-03-03 19:30:05 -08003569 else
3570 dshift += s_inc;
3571 }
Chris Craikb50c2172013-07-29 15:28:30 -07003572
The Android Open Source Project893912b2009-03-03 19:30:05 -08003573 if (sshift == s_end)
3574 {
3575 sshift = s_start;
3576 sp--;
3577 }
Chris Craikb50c2172013-07-29 15:28:30 -07003578
The Android Open Source Project893912b2009-03-03 19:30:05 -08003579 else
3580 sshift += s_inc;
3581 }
3582 break;
3583 }
Chris Craikb50c2172013-07-29 15:28:30 -07003584
The Android Open Source Project893912b2009-03-03 19:30:05 -08003585 case 2:
3586 {
3587 png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);
3588 png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);
3589 int sshift, dshift;
3590 int s_start, s_end, s_inc;
3591 int jstop = png_pass_inc[pass];
3592 png_uint_32 i;
3593
Patrick Scott5f6bd842010-06-28 16:55:16 -04003594#ifdef PNG_READ_PACKSWAP_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08003595 if (transformations & PNG_PACKSWAP)
3596 {
3597 sshift = (int)(((row_info->width + 3) & 0x03) << 1);
3598 dshift = (int)(((final_width + 3) & 0x03) << 1);
3599 s_start = 6;
3600 s_end = 0;
3601 s_inc = -2;
3602 }
Chris Craikb50c2172013-07-29 15:28:30 -07003603
The Android Open Source Project893912b2009-03-03 19:30:05 -08003604 else
3605#endif
3606 {
3607 sshift = (int)((3 - ((row_info->width + 3) & 0x03)) << 1);
3608 dshift = (int)((3 - ((final_width + 3) & 0x03)) << 1);
3609 s_start = 0;
3610 s_end = 6;
3611 s_inc = 2;
3612 }
3613
3614 for (i = 0; i < row_info->width; i++)
3615 {
3616 png_byte v;
3617 int j;
3618
3619 v = (png_byte)((*sp >> sshift) & 0x03);
3620 for (j = 0; j < jstop; j++)
3621 {
Chris Craikb50c2172013-07-29 15:28:30 -07003622 unsigned int tmp = *dp & (0x3f3f >> (6 - dshift));
3623 tmp |= v << dshift;
3624 *dp = (png_byte)(tmp & 0xff);
3625
The Android Open Source Project893912b2009-03-03 19:30:05 -08003626 if (dshift == s_end)
3627 {
3628 dshift = s_start;
3629 dp--;
3630 }
Chris Craikb50c2172013-07-29 15:28:30 -07003631
The Android Open Source Project893912b2009-03-03 19:30:05 -08003632 else
3633 dshift += s_inc;
3634 }
Chris Craikb50c2172013-07-29 15:28:30 -07003635
The Android Open Source Project893912b2009-03-03 19:30:05 -08003636 if (sshift == s_end)
3637 {
3638 sshift = s_start;
3639 sp--;
3640 }
Chris Craikb50c2172013-07-29 15:28:30 -07003641
The Android Open Source Project893912b2009-03-03 19:30:05 -08003642 else
3643 sshift += s_inc;
3644 }
3645 break;
3646 }
Chris Craikb50c2172013-07-29 15:28:30 -07003647
The Android Open Source Project893912b2009-03-03 19:30:05 -08003648 case 4:
3649 {
3650 png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1);
3651 png_bytep dp = row + (png_size_t)((final_width - 1) >> 1);
3652 int sshift, dshift;
3653 int s_start, s_end, s_inc;
3654 png_uint_32 i;
3655 int jstop = png_pass_inc[pass];
3656
Patrick Scott5f6bd842010-06-28 16:55:16 -04003657#ifdef PNG_READ_PACKSWAP_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08003658 if (transformations & PNG_PACKSWAP)
3659 {
3660 sshift = (int)(((row_info->width + 1) & 0x01) << 2);
3661 dshift = (int)(((final_width + 1) & 0x01) << 2);
3662 s_start = 4;
3663 s_end = 0;
3664 s_inc = -4;
3665 }
Chris Craikb50c2172013-07-29 15:28:30 -07003666
The Android Open Source Project893912b2009-03-03 19:30:05 -08003667 else
3668#endif
3669 {
3670 sshift = (int)((1 - ((row_info->width + 1) & 0x01)) << 2);
3671 dshift = (int)((1 - ((final_width + 1) & 0x01)) << 2);
3672 s_start = 0;
3673 s_end = 4;
3674 s_inc = 4;
3675 }
3676
3677 for (i = 0; i < row_info->width; i++)
3678 {
Chris Craikb50c2172013-07-29 15:28:30 -07003679 png_byte v = (png_byte)((*sp >> sshift) & 0x0f);
The Android Open Source Project893912b2009-03-03 19:30:05 -08003680 int j;
3681
3682 for (j = 0; j < jstop; j++)
3683 {
Chris Craikb50c2172013-07-29 15:28:30 -07003684 unsigned int tmp = *dp & (0xf0f >> (4 - dshift));
3685 tmp |= v << dshift;
3686 *dp = (png_byte)(tmp & 0xff);
3687
The Android Open Source Project893912b2009-03-03 19:30:05 -08003688 if (dshift == s_end)
3689 {
3690 dshift = s_start;
3691 dp--;
3692 }
Chris Craikb50c2172013-07-29 15:28:30 -07003693
The Android Open Source Project893912b2009-03-03 19:30:05 -08003694 else
3695 dshift += s_inc;
3696 }
Chris Craikb50c2172013-07-29 15:28:30 -07003697
The Android Open Source Project893912b2009-03-03 19:30:05 -08003698 if (sshift == s_end)
3699 {
3700 sshift = s_start;
3701 sp--;
3702 }
Chris Craikb50c2172013-07-29 15:28:30 -07003703
The Android Open Source Project893912b2009-03-03 19:30:05 -08003704 else
3705 sshift += s_inc;
3706 }
3707 break;
3708 }
Chris Craikb50c2172013-07-29 15:28:30 -07003709
The Android Open Source Project893912b2009-03-03 19:30:05 -08003710 default:
3711 {
3712 png_size_t pixel_bytes = (row_info->pixel_depth >> 3);
Chris Craikb50c2172013-07-29 15:28:30 -07003713
Patrick Scott5f6bd842010-06-28 16:55:16 -04003714 png_bytep sp = row + (png_size_t)(row_info->width - 1)
3715 * pixel_bytes;
Chris Craikb50c2172013-07-29 15:28:30 -07003716
The Android Open Source Project893912b2009-03-03 19:30:05 -08003717 png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes;
3718
3719 int jstop = png_pass_inc[pass];
3720 png_uint_32 i;
3721
3722 for (i = 0; i < row_info->width; i++)
3723 {
Chris Craikb50c2172013-07-29 15:28:30 -07003724 png_byte v[8]; /* SAFE; pixel_depth does not exceed 64 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08003725 int j;
3726
Chris Craikb50c2172013-07-29 15:28:30 -07003727 memcpy(v, sp, pixel_bytes);
3728
The Android Open Source Project893912b2009-03-03 19:30:05 -08003729 for (j = 0; j < jstop; j++)
3730 {
Chris Craikb50c2172013-07-29 15:28:30 -07003731 memcpy(dp, v, pixel_bytes);
The Android Open Source Project893912b2009-03-03 19:30:05 -08003732 dp -= pixel_bytes;
3733 }
Chris Craikb50c2172013-07-29 15:28:30 -07003734
The Android Open Source Project893912b2009-03-03 19:30:05 -08003735 sp -= pixel_bytes;
3736 }
3737 break;
3738 }
3739 }
Chris Craikb50c2172013-07-29 15:28:30 -07003740
The Android Open Source Project893912b2009-03-03 19:30:05 -08003741 row_info->width = final_width;
The Android Open Source Project4215dd12009-03-09 11:52:12 -07003742 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width);
The Android Open Source Project893912b2009-03-03 19:30:05 -08003743 }
Patrick Scott5f6bd842010-06-28 16:55:16 -04003744#ifndef PNG_READ_PACKSWAP_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07003745 PNG_UNUSED(transformations) /* Silence compiler warning */
The Android Open Source Project893912b2009-03-03 19:30:05 -08003746#endif
3747}
3748#endif /* PNG_READ_INTERLACING_SUPPORTED */
3749
Chris Craikb50c2172013-07-29 15:28:30 -07003750static void
3751png_read_filter_row_sub(png_row_infop row_info, png_bytep row,
3752 png_const_bytep prev_row)
The Android Open Source Project893912b2009-03-03 19:30:05 -08003753{
Chris Craikb50c2172013-07-29 15:28:30 -07003754 png_size_t i;
3755 png_size_t istop = row_info->rowbytes;
3756 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
3757 png_bytep rp = row + bpp;
3758
3759 PNG_UNUSED(prev_row)
3760
3761 for (i = bpp; i < istop; i++)
The Android Open Source Project893912b2009-03-03 19:30:05 -08003762 {
Chris Craikb50c2172013-07-29 15:28:30 -07003763 *rp = (png_byte)(((int)(*rp) + (int)(*(rp-bpp))) & 0xff);
3764 rp++;
The Android Open Source Project893912b2009-03-03 19:30:05 -08003765 }
3766}
3767
Chris Craikb50c2172013-07-29 15:28:30 -07003768static void
3769png_read_filter_row_up(png_row_infop row_info, png_bytep row,
3770 png_const_bytep prev_row)
Joseph Wen4ce0ee12010-08-20 10:42:22 +08003771{
Chris Craikb50c2172013-07-29 15:28:30 -07003772 png_size_t i;
3773 png_size_t istop = row_info->rowbytes;
3774 png_bytep rp = row;
3775 png_const_bytep pp = prev_row;
Joseph Wen4ce0ee12010-08-20 10:42:22 +08003776
Chris Craikb50c2172013-07-29 15:28:30 -07003777 for (i = 0; i < istop; i++)
3778 {
3779 *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
3780 rp++;
3781 }
Joseph Wen4ce0ee12010-08-20 10:42:22 +08003782}
Chris Craikb50c2172013-07-29 15:28:30 -07003783
3784static void
3785png_read_filter_row_avg(png_row_infop row_info, png_bytep row,
3786 png_const_bytep prev_row)
3787{
3788 png_size_t i;
3789 png_bytep rp = row;
3790 png_const_bytep pp = prev_row;
3791 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
3792 png_size_t istop = row_info->rowbytes - bpp;
3793
3794 for (i = 0; i < bpp; i++)
3795 {
3796 *rp = (png_byte)(((int)(*rp) +
3797 ((int)(*pp++) / 2 )) & 0xff);
3798
3799 rp++;
3800 }
3801
3802 for (i = 0; i < istop; i++)
3803 {
3804 *rp = (png_byte)(((int)(*rp) +
3805 (int)(*pp++ + *(rp-bpp)) / 2 ) & 0xff);
3806
3807 rp++;
3808 }
3809}
3810
3811static void
3812png_read_filter_row_paeth_1byte_pixel(png_row_infop row_info, png_bytep row,
3813 png_const_bytep prev_row)
3814{
3815 png_bytep rp_end = row + row_info->rowbytes;
3816 int a, c;
3817
3818 /* First pixel/byte */
3819 c = *prev_row++;
3820 a = *row + c;
3821 *row++ = (png_byte)a;
3822
3823 /* Remainder */
3824 while (row < rp_end)
3825 {
3826 int b, pa, pb, pc, p;
3827
3828 a &= 0xff; /* From previous iteration or start */
3829 b = *prev_row++;
3830
3831 p = b - c;
3832 pc = a - c;
3833
3834# ifdef PNG_USE_ABS
3835 pa = abs(p);
3836 pb = abs(pc);
3837 pc = abs(p + pc);
3838# else
3839 pa = p < 0 ? -p : p;
3840 pb = pc < 0 ? -pc : pc;
3841 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
3842# endif
3843
3844 /* Find the best predictor, the least of pa, pb, pc favoring the earlier
3845 * ones in the case of a tie.
3846 */
3847 if (pb < pa) pa = pb, a = b;
3848 if (pc < pa) a = c;
3849
3850 /* Calculate the current pixel in a, and move the previous row pixel to c
3851 * for the next time round the loop
3852 */
3853 c = b;
3854 a += *row;
3855 *row++ = (png_byte)a;
3856 }
3857}
3858
3859static void
3860png_read_filter_row_paeth_multibyte_pixel(png_row_infop row_info, png_bytep row,
3861 png_const_bytep prev_row)
3862{
3863 int bpp = (row_info->pixel_depth + 7) >> 3;
3864 png_bytep rp_end = row + bpp;
3865
3866 /* Process the first pixel in the row completely (this is the same as 'up'
3867 * because there is only one candidate predictor for the first row).
3868 */
3869 while (row < rp_end)
3870 {
3871 int a = *row + *prev_row++;
3872 *row++ = (png_byte)a;
3873 }
3874
3875 /* Remainder */
3876 rp_end += row_info->rowbytes - bpp;
3877
3878 while (row < rp_end)
3879 {
3880 int a, b, c, pa, pb, pc, p;
3881
3882 c = *(prev_row - bpp);
3883 a = *(row - bpp);
3884 b = *prev_row++;
3885
3886 p = b - c;
3887 pc = a - c;
3888
3889# ifdef PNG_USE_ABS
3890 pa = abs(p);
3891 pb = abs(pc);
3892 pc = abs(p + pc);
3893# else
3894 pa = p < 0 ? -p : p;
3895 pb = pc < 0 ? -pc : pc;
3896 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
3897# endif
3898
3899 if (pb < pa) pa = pb, a = b;
3900 if (pc < pa) a = c;
3901
3902 c = b;
3903 a += *row;
3904 *row++ = (png_byte)a;
3905 }
3906}
3907
3908static void
3909png_init_filter_functions(png_structrp pp)
3910 /* This function is called once for every PNG image to set the
3911 * implementations required to reverse the filtering of PNG rows. Reversing
3912 * the filter is the first transformation performed on the row data. It is
3913 * performed in place, therefore an implementation can be selected based on
3914 * the image pixel format. If the implementation depends on image width then
3915 * take care to ensure that it works correctly if the image is interlaced -
3916 * interlacing causes the actual row width to vary.
3917 */
3918{
3919 unsigned int bpp = (pp->pixel_depth + 7) >> 3;
3920
3921 pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub;
3922 pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up;
3923 pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg;
3924 if (bpp == 1)
3925 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
3926 png_read_filter_row_paeth_1byte_pixel;
3927 else
3928 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
3929 png_read_filter_row_paeth_multibyte_pixel;
3930
3931#ifdef PNG_FILTER_OPTIMIZATIONS
3932 /* To use this define PNG_FILTER_OPTIMIZATIONS as the name of a function to
3933 * call to install hardware optimizations for the above functions; simply
3934 * replace whatever elements of the pp->read_filter[] array with a hardware
3935 * specific (or, for that matter, generic) optimization.
3936 *
3937 * To see an example of this examine what configure.ac does when
3938 * --enable-arm-neon is specified on the command line.
3939 */
3940 PNG_FILTER_OPTIMIZATIONS(pp, bpp);
Joseph Wen4ce0ee12010-08-20 10:42:22 +08003941#endif
Chris Craikb50c2172013-07-29 15:28:30 -07003942}
3943
3944void /* PRIVATE */
3945png_read_filter_row(png_structrp pp, png_row_infop row_info, png_bytep row,
3946 png_const_bytep prev_row, int filter)
3947{
3948 /* OPTIMIZATION: DO NOT MODIFY THIS FUNCTION, instead #define
3949 * PNG_FILTER_OPTIMIZATIONS to a function that overrides the generic
3950 * implementations. See png_init_filter_functions above.
3951 */
3952 if (pp->read_filter[0] == NULL)
3953 png_init_filter_functions(pp);
3954 if (filter > PNG_FILTER_VALUE_NONE && filter < PNG_FILTER_VALUE_LAST)
3955 pp->read_filter[filter-1](row_info, row, prev_row);
3956}
Joseph Wen4ce0ee12010-08-20 10:42:22 +08003957
Patrick Scott5f6bd842010-06-28 16:55:16 -04003958#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08003959void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07003960png_read_IDAT_data(png_structrp png_ptr, png_bytep output,
3961 png_alloc_size_t avail_out)
3962{
3963 /* Loop reading IDATs and decompressing the result into output[avail_out] */
3964 png_ptr->zstream.next_out = output;
3965 png_ptr->zstream.avail_out = 0; /* safety: set below */
3966
3967 if (output == NULL)
3968 avail_out = 0;
3969
3970 do
3971 {
3972 int ret;
3973 png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];
3974
3975 if (png_ptr->zstream.avail_in == 0)
3976 {
3977 uInt avail_in;
3978 png_bytep buffer;
3979
3980 while (png_ptr->idat_size == 0)
3981 {
John Reck90bf0d22013-08-14 10:37:42 -07003982#ifdef PNG_INDEX_SUPPORTED
3983 if (png_ptr->index) {
3984 png_opt_crc_finish(png_ptr, 0);
3985 png_ptr->index->stream_idat_position = png_ptr->total_data_read;
3986 } else
3987#endif
Chris Craikb50c2172013-07-29 15:28:30 -07003988 png_crc_finish(png_ptr, 0);
3989
3990 png_ptr->idat_size = png_read_chunk_header(png_ptr);
3991 /* This is an error even in the 'check' case because the code just
3992 * consumed a non-IDAT header.
3993 */
3994 if (png_ptr->chunk_name != png_IDAT)
3995 png_error(png_ptr, "Not enough image data");
3996 }
3997
3998 avail_in = png_ptr->IDAT_read_size;
3999
4000 if (avail_in > png_ptr->idat_size)
4001 avail_in = (uInt)png_ptr->idat_size;
4002
4003 /* A PNG with a gradually increasing IDAT size will defeat this attempt
4004 * to minimize memory usage by causing lots of re-allocs, but
4005 * realistically doing IDAT_read_size re-allocs is not likely to be a
4006 * big problem.
4007 */
4008 buffer = png_read_buffer(png_ptr, avail_in, 0/*error*/);
4009
4010 png_crc_read(png_ptr, buffer, avail_in);
4011 png_ptr->idat_size -= avail_in;
4012
4013 png_ptr->zstream.next_in = buffer;
4014 png_ptr->zstream.avail_in = avail_in;
4015 }
4016
4017 /* And set up the output side. */
4018 if (output != NULL) /* standard read */
4019 {
4020 uInt out = ZLIB_IO_MAX;
4021
4022 if (out > avail_out)
4023 out = (uInt)avail_out;
4024
4025 avail_out -= out;
4026 png_ptr->zstream.avail_out = out;
4027 }
4028
4029 else /* after last row, checking for end */
4030 {
4031 png_ptr->zstream.next_out = tmpbuf;
4032 png_ptr->zstream.avail_out = (sizeof tmpbuf);
4033 }
4034
4035 /* Use NO_FLUSH; this gives zlib the maximum opportunity to optimize the
4036 * process. If the LZ stream is truncated the sequential reader will
4037 * terminally damage the stream, above, by reading the chunk header of the
4038 * following chunk (it then exits with png_error).
4039 *
4040 * TODO: deal more elegantly with truncated IDAT lists.
4041 */
4042 ret = inflate(&png_ptr->zstream, Z_NO_FLUSH);
4043
4044 /* Take the unconsumed output back. */
4045 if (output != NULL)
4046 avail_out += png_ptr->zstream.avail_out;
4047
4048 else /* avail_out counts the extra bytes */
4049 avail_out += (sizeof tmpbuf) - png_ptr->zstream.avail_out;
4050
4051 png_ptr->zstream.avail_out = 0;
4052
4053 if (ret == Z_STREAM_END)
4054 {
4055 /* Do this for safety; we won't read any more into this row. */
4056 png_ptr->zstream.next_out = NULL;
4057
4058 png_ptr->mode |= PNG_AFTER_IDAT;
4059 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
4060
4061 if (png_ptr->zstream.avail_in > 0 || png_ptr->idat_size > 0)
4062 png_chunk_benign_error(png_ptr, "Extra compressed data");
4063 break;
4064 }
4065
4066 if (ret != Z_OK)
John Reck90bf0d22013-08-14 10:37:42 -07004067#ifdef PNG_INDEX_SUPPORTED
4068 if (png_ptr->index && png_ptr->row_number != png_ptr->height - 1)
4069#endif
Chris Craikb50c2172013-07-29 15:28:30 -07004070 {
4071 png_zstream_error(png_ptr, ret);
4072
4073 if (output != NULL)
4074 png_chunk_error(png_ptr, png_ptr->zstream.msg);
4075
4076 else /* checking */
4077 {
4078 png_chunk_benign_error(png_ptr, png_ptr->zstream.msg);
4079 return;
4080 }
4081 }
4082 } while (avail_out > 0);
4083
4084 if (avail_out > 0)
4085 {
4086 /* The stream ended before the image; this is the same as too few IDATs so
4087 * should be handled the same way.
4088 */
4089 if (output != NULL)
4090 png_error(png_ptr, "Not enough image data");
4091
4092 else /* the deflate stream contained extra data */
4093 png_chunk_benign_error(png_ptr, "Too much image data");
4094 }
4095}
4096
4097void /* PRIVATE */
4098png_read_finish_IDAT(png_structrp png_ptr)
4099{
4100 /* We don't need any more data and the stream should have ended, however the
4101 * LZ end code may actually not have been processed. In this case we must
4102 * read it otherwise stray unread IDAT data or, more likely, an IDAT chunk
4103 * may still remain to be consumed.
4104 */
4105 if (!(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED))
4106 {
4107 /* The NULL causes png_read_IDAT_data to swallow any remaining bytes in
4108 * the compressed stream, but the stream may be damaged too, so even after
4109 * this call we may need to terminate the zstream ownership.
4110 */
4111 png_read_IDAT_data(png_ptr, NULL, 0);
4112 png_ptr->zstream.next_out = NULL; /* safety */
4113
4114 /* Now clear everything out for safety; the following may not have been
4115 * done.
4116 */
4117 if (!(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED))
4118 {
4119 png_ptr->mode |= PNG_AFTER_IDAT;
4120 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
4121 }
4122 }
4123
4124 /* If the zstream has not been released do it now *and* terminate the reading
4125 * of the final IDAT chunk.
4126 */
4127 if (png_ptr->zowner == png_IDAT)
4128 {
4129 /* Always do this; the pointers otherwise point into the read buffer. */
4130 png_ptr->zstream.next_in = NULL;
4131 png_ptr->zstream.avail_in = 0;
4132
4133 /* Now we no longer own the zstream. */
4134 png_ptr->zowner = 0;
4135
4136 /* The slightly weird semantics of the sequential IDAT reading is that we
4137 * are always in or at the end of an IDAT chunk, so we always need to do a
4138 * crc_finish here. If idat_size is non-zero we also need to read the
4139 * spurious bytes at the end of the chunk now.
4140 */
4141 (void)png_crc_finish(png_ptr, png_ptr->idat_size);
4142 }
4143}
4144
John Reck90bf0d22013-08-14 10:37:42 -07004145#ifdef PNG_INDEX_SUPPORTED
4146void /* PRIVATE */
4147png_set_interlaced_pass(png_structp png_ptr, int pass)
4148{
4149 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
4150 /* Start of interlace block */
4151 PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
4152 /* Offset to next interlace block */
4153 PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
4154 /* Start of interlace block in the y direction */
4155 PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
4156 /* Offset to next interlace block in the y direction */
4157 PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
4158 png_ptr->pass = pass;
4159 png_ptr->iwidth = (png_ptr->width +
4160 png_pass_inc[png_ptr->pass] - 1 -
4161 png_pass_start[png_ptr->pass]) /
4162 png_pass_inc[png_ptr->pass];
4163}
4164#endif
4165
Chris Craikb50c2172013-07-29 15:28:30 -07004166void /* PRIVATE */
4167png_read_finish_row(png_structrp png_ptr)
The Android Open Source Project893912b2009-03-03 19:30:05 -08004168{
The Android Open Source Project893912b2009-03-03 19:30:05 -08004169#ifdef PNG_READ_INTERLACING_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004170 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
The Android Open Source Project893912b2009-03-03 19:30:05 -08004171
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004172 /* Start of interlace block */
Chris Craikb50c2172013-07-29 15:28:30 -07004173 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 -08004174
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004175 /* Offset to next interlace block */
Chris Craikb50c2172013-07-29 15:28:30 -07004176 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 -08004177
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004178 /* Start of interlace block in the y direction */
Chris Craikb50c2172013-07-29 15:28:30 -07004179 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 -08004180
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004181 /* Offset to next interlace block in the y direction */
Chris Craikb50c2172013-07-29 15:28:30 -07004182 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 -08004183#endif /* PNG_READ_INTERLACING_SUPPORTED */
The Android Open Source Project893912b2009-03-03 19:30:05 -08004184
The Android Open Source Project4215dd12009-03-09 11:52:12 -07004185 png_debug(1, "in png_read_finish_row");
The Android Open Source Project893912b2009-03-03 19:30:05 -08004186 png_ptr->row_number++;
4187 if (png_ptr->row_number < png_ptr->num_rows)
4188 return;
4189
4190#ifdef PNG_READ_INTERLACING_SUPPORTED
4191 if (png_ptr->interlaced)
4192 {
4193 png_ptr->row_number = 0;
Chris Craikb50c2172013-07-29 15:28:30 -07004194
4195 /* TO DO: don't do this if prev_row isn't needed (requires
4196 * read-ahead of the next row's filter byte.
4197 */
4198 memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
4199
The Android Open Source Project893912b2009-03-03 19:30:05 -08004200 do
4201 {
4202 png_ptr->pass++;
Chris Craikb50c2172013-07-29 15:28:30 -07004203
The Android Open Source Project893912b2009-03-03 19:30:05 -08004204 if (png_ptr->pass >= 7)
4205 break;
Chris Craikb50c2172013-07-29 15:28:30 -07004206
The Android Open Source Project893912b2009-03-03 19:30:05 -08004207 png_ptr->iwidth = (png_ptr->width +
4208 png_pass_inc[png_ptr->pass] - 1 -
4209 png_pass_start[png_ptr->pass]) /
4210 png_pass_inc[png_ptr->pass];
4211
The Android Open Source Project893912b2009-03-03 19:30:05 -08004212 if (!(png_ptr->transformations & PNG_INTERLACE))
4213 {
4214 png_ptr->num_rows = (png_ptr->height +
Chris Craikb50c2172013-07-29 15:28:30 -07004215 png_pass_yinc[png_ptr->pass] - 1 -
4216 png_pass_ystart[png_ptr->pass]) /
4217 png_pass_yinc[png_ptr->pass];
The Android Open Source Project893912b2009-03-03 19:30:05 -08004218 }
Chris Craikb50c2172013-07-29 15:28:30 -07004219
The Android Open Source Project893912b2009-03-03 19:30:05 -08004220 else /* if (png_ptr->transformations & PNG_INTERLACE) */
Chris Craikb50c2172013-07-29 15:28:30 -07004221 break; /* libpng deinterlacing sees every row */
4222
4223 } while (png_ptr->num_rows == 0 || png_ptr->iwidth == 0);
The Android Open Source Project893912b2009-03-03 19:30:05 -08004224
4225 if (png_ptr->pass < 7)
4226 return;
4227 }
4228#endif /* PNG_READ_INTERLACING_SUPPORTED */
4229
Chris Craikb50c2172013-07-29 15:28:30 -07004230 /* Here after at the end of the last row of the last pass. */
4231 png_read_finish_IDAT(png_ptr);
The Android Open Source Project893912b2009-03-03 19:30:05 -08004232}
Patrick Scott5f6bd842010-06-28 16:55:16 -04004233#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
The Android Open Source Project893912b2009-03-03 19:30:05 -08004234
4235void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -07004236png_read_start_row(png_structrp png_ptr)
The Android Open Source Project893912b2009-03-03 19:30:05 -08004237{
The Android Open Source Project893912b2009-03-03 19:30:05 -08004238#ifdef PNG_READ_INTERLACING_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004239 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
The Android Open Source Project893912b2009-03-03 19:30:05 -08004240
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004241 /* Start of interlace block */
Chris Craikb50c2172013-07-29 15:28:30 -07004242 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 -08004243
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004244 /* Offset to next interlace block */
Chris Craikb50c2172013-07-29 15:28:30 -07004245 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 -08004246
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004247 /* Start of interlace block in the y direction */
Chris Craikb50c2172013-07-29 15:28:30 -07004248 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 -08004249
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004250 /* Offset to next interlace block in the y direction */
Chris Craikb50c2172013-07-29 15:28:30 -07004251 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 -08004252#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -08004253
4254 int max_pixel_depth;
The Android Open Source Project4215dd12009-03-09 11:52:12 -07004255 png_size_t row_bytes;
The Android Open Source Project893912b2009-03-03 19:30:05 -08004256
The Android Open Source Project4215dd12009-03-09 11:52:12 -07004257 png_debug(1, "in png_read_start_row");
Chris Craikb50c2172013-07-29 15:28:30 -07004258
4259#ifdef PNG_READ_TRANSFORMS_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08004260 png_init_read_transformations(png_ptr);
Chris Craikb50c2172013-07-29 15:28:30 -07004261#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -08004262#ifdef PNG_READ_INTERLACING_SUPPORTED
4263 if (png_ptr->interlaced)
4264 {
4265 if (!(png_ptr->transformations & PNG_INTERLACE))
4266 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
Chris Craikb50c2172013-07-29 15:28:30 -07004267 png_pass_ystart[0]) / png_pass_yinc[0];
4268
The Android Open Source Project893912b2009-03-03 19:30:05 -08004269 else
4270 png_ptr->num_rows = png_ptr->height;
4271
4272 png_ptr->iwidth = (png_ptr->width +
Chris Craikb50c2172013-07-29 15:28:30 -07004273 png_pass_inc[png_ptr->pass] - 1 -
4274 png_pass_start[png_ptr->pass]) /
4275 png_pass_inc[png_ptr->pass];
The Android Open Source Project893912b2009-03-03 19:30:05 -08004276 }
Chris Craikb50c2172013-07-29 15:28:30 -07004277
The Android Open Source Project893912b2009-03-03 19:30:05 -08004278 else
4279#endif /* PNG_READ_INTERLACING_SUPPORTED */
4280 {
4281 png_ptr->num_rows = png_ptr->height;
4282 png_ptr->iwidth = png_ptr->width;
The Android Open Source Project893912b2009-03-03 19:30:05 -08004283 }
Chris Craikb50c2172013-07-29 15:28:30 -07004284
The Android Open Source Project893912b2009-03-03 19:30:05 -08004285 max_pixel_depth = png_ptr->pixel_depth;
4286
Chris Craikb50c2172013-07-29 15:28:30 -07004287 /* WARNING: * png_read_transform_info (pngrtran.c) performs a simpliar set of
4288 * calculations to calculate the final pixel depth, then
4289 * png_do_read_transforms actually does the transforms. This means that the
4290 * code which effectively calculates this value is actually repeated in three
4291 * separate places. They must all match. Innocent changes to the order of
4292 * transformations can and will break libpng in a way that causes memory
4293 * overwrites.
4294 *
4295 * TODO: fix this.
4296 */
Patrick Scott5f6bd842010-06-28 16:55:16 -04004297#ifdef PNG_READ_PACK_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08004298 if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8)
4299 max_pixel_depth = 8;
4300#endif
4301
Patrick Scott5f6bd842010-06-28 16:55:16 -04004302#ifdef PNG_READ_EXPAND_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08004303 if (png_ptr->transformations & PNG_EXPAND)
4304 {
4305 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
4306 {
4307 if (png_ptr->num_trans)
4308 max_pixel_depth = 32;
Chris Craikb50c2172013-07-29 15:28:30 -07004309
The Android Open Source Project893912b2009-03-03 19:30:05 -08004310 else
4311 max_pixel_depth = 24;
4312 }
Chris Craikb50c2172013-07-29 15:28:30 -07004313
The Android Open Source Project893912b2009-03-03 19:30:05 -08004314 else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
4315 {
4316 if (max_pixel_depth < 8)
4317 max_pixel_depth = 8;
Chris Craikb50c2172013-07-29 15:28:30 -07004318
The Android Open Source Project893912b2009-03-03 19:30:05 -08004319 if (png_ptr->num_trans)
4320 max_pixel_depth *= 2;
4321 }
Chris Craikb50c2172013-07-29 15:28:30 -07004322
The Android Open Source Project893912b2009-03-03 19:30:05 -08004323 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
4324 {
4325 if (png_ptr->num_trans)
4326 {
4327 max_pixel_depth *= 4;
4328 max_pixel_depth /= 3;
4329 }
4330 }
4331 }
4332#endif
4333
Chris Craikb50c2172013-07-29 15:28:30 -07004334#ifdef PNG_READ_EXPAND_16_SUPPORTED
4335 if (png_ptr->transformations & PNG_EXPAND_16)
4336 {
4337# ifdef PNG_READ_EXPAND_SUPPORTED
4338 /* In fact it is an error if it isn't supported, but checking is
4339 * the safe way.
4340 */
4341 if (png_ptr->transformations & PNG_EXPAND)
4342 {
4343 if (png_ptr->bit_depth < 16)
4344 max_pixel_depth *= 2;
4345 }
4346 else
4347# endif
4348 png_ptr->transformations &= ~PNG_EXPAND_16;
4349 }
4350#endif
4351
Patrick Scott5f6bd842010-06-28 16:55:16 -04004352#ifdef PNG_READ_FILLER_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08004353 if (png_ptr->transformations & (PNG_FILLER))
4354 {
Chris Craikb50c2172013-07-29 15:28:30 -07004355 if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
The Android Open Source Project893912b2009-03-03 19:30:05 -08004356 {
4357 if (max_pixel_depth <= 8)
4358 max_pixel_depth = 16;
Chris Craikb50c2172013-07-29 15:28:30 -07004359
The Android Open Source Project893912b2009-03-03 19:30:05 -08004360 else
4361 max_pixel_depth = 32;
4362 }
Chris Craikb50c2172013-07-29 15:28:30 -07004363
4364 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB ||
4365 png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
The Android Open Source Project893912b2009-03-03 19:30:05 -08004366 {
4367 if (max_pixel_depth <= 32)
4368 max_pixel_depth = 32;
Chris Craikb50c2172013-07-29 15:28:30 -07004369
The Android Open Source Project893912b2009-03-03 19:30:05 -08004370 else
4371 max_pixel_depth = 64;
4372 }
4373 }
4374#endif
4375
Patrick Scott5f6bd842010-06-28 16:55:16 -04004376#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08004377 if (png_ptr->transformations & PNG_GRAY_TO_RGB)
4378 {
4379 if (
Patrick Scott5f6bd842010-06-28 16:55:16 -04004380#ifdef PNG_READ_EXPAND_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07004381 (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) ||
The Android Open Source Project893912b2009-03-03 19:30:05 -08004382#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04004383#ifdef PNG_READ_FILLER_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07004384 (png_ptr->transformations & (PNG_FILLER)) ||
The Android Open Source Project893912b2009-03-03 19:30:05 -08004385#endif
Chris Craikb50c2172013-07-29 15:28:30 -07004386 png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
The Android Open Source Project893912b2009-03-03 19:30:05 -08004387 {
4388 if (max_pixel_depth <= 16)
4389 max_pixel_depth = 32;
Chris Craikb50c2172013-07-29 15:28:30 -07004390
The Android Open Source Project893912b2009-03-03 19:30:05 -08004391 else
4392 max_pixel_depth = 64;
4393 }
Chris Craikb50c2172013-07-29 15:28:30 -07004394
The Android Open Source Project893912b2009-03-03 19:30:05 -08004395 else
4396 {
4397 if (max_pixel_depth <= 8)
Chris Craikb50c2172013-07-29 15:28:30 -07004398 {
4399 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
The Android Open Source Project893912b2009-03-03 19:30:05 -08004400 max_pixel_depth = 32;
Chris Craikb50c2172013-07-29 15:28:30 -07004401
4402 else
The Android Open Source Project893912b2009-03-03 19:30:05 -08004403 max_pixel_depth = 24;
Chris Craikb50c2172013-07-29 15:28:30 -07004404 }
4405
The Android Open Source Project893912b2009-03-03 19:30:05 -08004406 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4407 max_pixel_depth = 64;
Chris Craikb50c2172013-07-29 15:28:30 -07004408
The Android Open Source Project893912b2009-03-03 19:30:05 -08004409 else
4410 max_pixel_depth = 48;
4411 }
4412 }
4413#endif
4414
4415#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
4416defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
The Android Open Source Project4215dd12009-03-09 11:52:12 -07004417 if (png_ptr->transformations & PNG_USER_TRANSFORM)
Chris Craikb50c2172013-07-29 15:28:30 -07004418 {
4419 int user_pixel_depth = png_ptr->user_transform_depth *
The Android Open Source Project893912b2009-03-03 19:30:05 -08004420 png_ptr->user_transform_channels;
Chris Craikb50c2172013-07-29 15:28:30 -07004421
4422 if (user_pixel_depth > max_pixel_depth)
4423 max_pixel_depth = user_pixel_depth;
4424 }
The Android Open Source Project893912b2009-03-03 19:30:05 -08004425#endif
4426
Chris Craikb50c2172013-07-29 15:28:30 -07004427 /* This value is stored in png_struct and double checked in the row read
4428 * code.
4429 */
4430 png_ptr->maximum_pixel_depth = (png_byte)max_pixel_depth;
4431 png_ptr->transformed_pixel_depth = 0; /* calculated on demand */
4432
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004433 /* Align the width on the next larger 8 pixels. Mainly used
4434 * for interlacing
4435 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08004436 row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004437 /* Calculate the maximum bytes needed, adding a byte and a pixel
4438 * for safety's sake
4439 */
The Android Open Source Project4215dd12009-03-09 11:52:12 -07004440 row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) +
Chris Craikb50c2172013-07-29 15:28:30 -07004441 1 + ((max_pixel_depth + 7) >> 3);
4442
The Android Open Source Project893912b2009-03-03 19:30:05 -08004443#ifdef PNG_MAX_MALLOC_64K
4444 if (row_bytes > (png_uint_32)65536L)
4445 png_error(png_ptr, "This image requires a row greater than 64KB");
4446#endif
4447
Chris Craikb50c2172013-07-29 15:28:30 -07004448 if (row_bytes + 48 > png_ptr->old_big_row_buf_size)
The Android Open Source Project893912b2009-03-03 19:30:05 -08004449 {
The Android Open Source Project4215dd12009-03-09 11:52:12 -07004450 png_free(png_ptr, png_ptr->big_row_buf);
Chris Craikb50c2172013-07-29 15:28:30 -07004451 png_free(png_ptr, png_ptr->big_prev_row);
4452
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004453 if (png_ptr->interlaced)
Patrick Scott5f6bd842010-06-28 16:55:16 -04004454 png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr,
Chris Craikb50c2172013-07-29 15:28:30 -07004455 row_bytes + 48);
Patrick Scott5f6bd842010-06-28 16:55:16 -04004456
Chris Craikb50c2172013-07-29 15:28:30 -07004457 else
4458 png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
4459
4460 png_ptr->big_prev_row = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
4461
4462#ifdef PNG_ALIGNED_MEMORY_SUPPORTED
4463 /* Use 16-byte aligned memory for row_buf with at least 16 bytes
4464 * of padding before and after row_buf; treat prev_row similarly.
4465 * NOTE: the alignment is to the start of the pixels, one beyond the start
4466 * of the buffer, because of the filter byte. Prior to libpng 1.5.6 this
4467 * was incorrect; the filter byte was aligned, which had the exact
4468 * opposite effect of that intended.
4469 */
4470 {
4471 png_bytep temp = png_ptr->big_row_buf + 32;
4472 int extra = (int)((temp - (png_bytep)0) & 0x0f);
4473 png_ptr->row_buf = temp - extra - 1/*filter byte*/;
4474
4475 temp = png_ptr->big_prev_row + 32;
4476 extra = (int)((temp - (png_bytep)0) & 0x0f);
4477 png_ptr->prev_row = temp - extra - 1/*filter byte*/;
4478 }
4479
4480#else
4481 /* Use 31 bytes of padding before and 17 bytes after row_buf. */
4482 png_ptr->row_buf = png_ptr->big_row_buf + 31;
4483 png_ptr->prev_row = png_ptr->big_prev_row + 31;
4484#endif
4485 png_ptr->old_big_row_buf_size = row_bytes + 48;
The Android Open Source Project893912b2009-03-03 19:30:05 -08004486 }
4487
4488#ifdef PNG_MAX_MALLOC_64K
Chris Craikb50c2172013-07-29 15:28:30 -07004489 if (png_ptr->rowbytes > 65535)
The Android Open Source Project893912b2009-03-03 19:30:05 -08004490 png_error(png_ptr, "This image requires a row greater than 64KB");
The Android Open Source Project893912b2009-03-03 19:30:05 -08004491
Chris Craikb50c2172013-07-29 15:28:30 -07004492#endif
4493 if (png_ptr->rowbytes > (PNG_SIZE_MAX - 1))
4494 png_error(png_ptr, "Row has too many bytes to allocate in memory");
4495
4496 memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
4497
4498 png_debug1(3, "width = %u,", png_ptr->width);
4499 png_debug1(3, "height = %u,", png_ptr->height);
4500 png_debug1(3, "iwidth = %u,", png_ptr->iwidth);
4501 png_debug1(3, "num_rows = %u,", png_ptr->num_rows);
4502 png_debug1(3, "rowbytes = %lu,", (unsigned long)png_ptr->rowbytes);
4503 png_debug1(3, "irowbytes = %lu",
4504 (unsigned long)PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1);
4505
4506 /* The sequential reader needs a buffer for IDAT, but the progressive reader
4507 * does not, so free the read buffer now regardless; the sequential reader
4508 * reallocates it on demand.
4509 */
4510 if (png_ptr->read_buffer)
The Android Open Source Project893912b2009-03-03 19:30:05 -08004511 {
Chris Craikb50c2172013-07-29 15:28:30 -07004512 png_bytep buffer = png_ptr->read_buffer;
4513
4514 png_ptr->read_buffer_size = 0;
4515 png_ptr->read_buffer = NULL;
4516 png_free(png_ptr, buffer);
The Android Open Source Project893912b2009-03-03 19:30:05 -08004517 }
4518
Chris Craikb50c2172013-07-29 15:28:30 -07004519 /* Finally claim the zstream for the inflate of the IDAT data, use the bits
4520 * value from the stream (note that this will result in a fatal error if the
4521 * IDAT stream has a bogus deflate header window_bits value, but this should
4522 * not be happening any longer!)
4523 */
4524 if (png_inflate_claim(png_ptr, png_IDAT) != Z_OK)
4525 png_error(png_ptr, png_ptr->zstream.msg);
The Android Open Source Project893912b2009-03-03 19:30:05 -08004526
4527 png_ptr->flags |= PNG_FLAG_ROW_INIT;
4528}
4529#endif /* PNG_READ_SUPPORTED */