VirtualBox

source: vbox/trunk/src/libs/libpng-1.6.45/pngrutil.c@ 107935

Last change on this file since 107935 was 107813, checked in by vboxsync, 3 weeks ago

libpng-1.6.45: Applied and adjusted our libpng changes to 1.6.45. bugref:8515

  • Property svn:eol-style set to native
File size: 144.3 KB
Line 
1/* pngrutil.c - utilities to read a PNG file
2 *
3 * Copyright (c) 2018-2024 Cosmin Truta
4 * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson
5 * Copyright (c) 1996-1997 Andreas Dilger
6 * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.
7 *
8 * This code is released under the libpng license.
9 * For conditions of distribution and use, see the disclaimer
10 * and license in png.h
11 *
12 * This file contains routines that are only called from within
13 * libpng itself during the course of reading an image.
14 */
15
16#include "pngpriv.h"
17
18#ifdef PNG_READ_SUPPORTED
19
20#ifdef PNG_READ_INTERLACING_SUPPORTED
21/* Arrays to facilitate interlacing - use pass (0 - 6) as index. */
22
23/* Start of interlace block */
24static const png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
25/* Offset to next interlace block */
26static const png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
27/* Start of interlace block in the y direction */
28static const png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
29/* Offset to next interlace block in the y direction */
30static const png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
31
32/* TODO: Move these arrays to a common utility module to avoid duplication. */
33#endif
34
35png_uint_32 PNGAPI
36png_get_uint_31(png_const_structrp png_ptr, png_const_bytep buf)
37{
38 png_uint_32 uval = png_get_uint_32(buf);
39
40 if (uval > PNG_UINT_31_MAX)
41 png_error(png_ptr, "PNG unsigned integer out of range");
42
43 return uval;
44}
45
46#if defined(PNG_READ_gAMA_SUPPORTED) || defined(PNG_READ_cHRM_SUPPORTED)
47/* The following is a variation on the above for use with the fixed
48 * point values used for gAMA and cHRM. Instead of png_error it
49 * issues a warning and returns (-1) - an invalid value because both
50 * gAMA and cHRM use *unsigned* integers for fixed point values.
51 */
52#define PNG_FIXED_ERROR (-1)
53
54static png_fixed_point /* PRIVATE */
55png_get_fixed_point(png_structrp png_ptr, png_const_bytep buf)
56{
57 png_uint_32 uval = png_get_uint_32(buf);
58
59 if (uval <= PNG_UINT_31_MAX)
60 return (png_fixed_point)uval; /* known to be in range */
61
62 /* The caller can turn off the warning by passing NULL. */
63 if (png_ptr != NULL)
64 png_warning(png_ptr, "PNG fixed point integer out of range");
65
66 return PNG_FIXED_ERROR;
67}
68#endif
69
70#ifdef PNG_READ_INT_FUNCTIONS_SUPPORTED
71/* NOTE: the read macros will obscure these definitions, so that if
72 * PNG_USE_READ_MACROS is set the library will not use them internally,
73 * but the APIs will still be available externally.
74 *
75 * The parentheses around "PNGAPI function_name" in the following three
76 * functions are necessary because they allow the macros to co-exist with
77 * these (unused but exported) functions.
78 */
79
80/* Grab an unsigned 32-bit integer from a buffer in big-endian format. */
81png_uint_32 (PNGAPI
82png_get_uint_32)(png_const_bytep buf)
83{
84 png_uint_32 uval =
85 ((png_uint_32)(*(buf )) << 24) +
86 ((png_uint_32)(*(buf + 1)) << 16) +
87 ((png_uint_32)(*(buf + 2)) << 8) +
88 ((png_uint_32)(*(buf + 3)) ) ;
89
90 return uval;
91}
92
93/* Grab a signed 32-bit integer from a buffer in big-endian format. The
94 * data is stored in the PNG file in two's complement format and there
95 * is no guarantee that a 'png_int_32' is exactly 32 bits, therefore
96 * the following code does a two's complement to native conversion.
97 */
98png_int_32 (PNGAPI
99png_get_int_32)(png_const_bytep buf)
100{
101 png_uint_32 uval = png_get_uint_32(buf);
102 if ((uval & 0x80000000) == 0) /* non-negative */
103 return (png_int_32)uval;
104
105 uval = (uval ^ 0xffffffff) + 1; /* 2's complement: -x = ~x+1 */
106 if ((uval & 0x80000000) == 0) /* no overflow */
107 return -(png_int_32)uval;
108 /* The following has to be safe; this function only gets called on PNG data
109 * and if we get here that data is invalid. 0 is the most safe value and
110 * if not then an attacker would surely just generate a PNG with 0 instead.
111 */
112 return 0;
113}
114
115/* Grab an unsigned 16-bit integer from a buffer in big-endian format. */
116png_uint_16 (PNGAPI
117png_get_uint_16)(png_const_bytep buf)
118{
119 /* ANSI-C requires an int value to accommodate at least 16 bits so this
120 * works and allows the compiler not to worry about possible narrowing
121 * on 32-bit systems. (Pre-ANSI systems did not make integers smaller
122 * than 16 bits either.)
123 */
124 unsigned int val =
125 ((unsigned int)(*buf) << 8) +
126 ((unsigned int)(*(buf + 1)));
127
128 return (png_uint_16)val;
129}
130
131#endif /* READ_INT_FUNCTIONS */
132
133/* Read and check the PNG file signature */
134void /* PRIVATE */
135png_read_sig(png_structrp png_ptr, png_inforp info_ptr)
136{
137 size_t num_checked, num_to_check;
138
139 /* Exit if the user application does not expect a signature. */
140 if (png_ptr->sig_bytes >= 8)
141 return;
142
143 num_checked = png_ptr->sig_bytes;
144 num_to_check = 8 - num_checked;
145
146#ifdef PNG_IO_STATE_SUPPORTED
147 png_ptr->io_state = PNG_IO_READING | PNG_IO_SIGNATURE;
148#endif
149
150 /* The signature must be serialized in a single I/O call. */
151 png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
152 png_ptr->sig_bytes = 8;
153
154 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check) != 0)
155 {
156 if (num_checked < 4 &&
157 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4) != 0)
158 png_error(png_ptr, "Not a PNG file");
159 else
160 png_error(png_ptr, "PNG file corrupted by ASCII conversion");
161 }
162 if (num_checked < 3)
163 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
164}
165
166/* Read the chunk header (length + type name).
167 * Put the type name into png_ptr->chunk_name, and return the length.
168 */
169png_uint_32 /* PRIVATE */
170png_read_chunk_header(png_structrp png_ptr)
171{
172 png_byte buf[8];
173 png_uint_32 length;
174
175#ifdef PNG_IO_STATE_SUPPORTED
176 png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_HDR;
177#endif
178
179 /* Read the length and the chunk name.
180 * This must be performed in a single I/O call.
181 */
182 png_read_data(png_ptr, buf, 8);
183 length = png_get_uint_31(png_ptr, buf);
184
185 /* Put the chunk name into png_ptr->chunk_name. */
186 png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(buf+4);
187
188 png_debug2(0, "Reading chunk typeid = 0x%lx, length = %lu",
189 (unsigned long)png_ptr->chunk_name, (unsigned long)length);
190
191 /* Reset the crc and run it over the chunk name. */
192 png_reset_crc(png_ptr);
193 png_calculate_crc(png_ptr, buf + 4, 4);
194
195 /* Check to see if chunk name is valid. */
196 png_check_chunk_name(png_ptr, png_ptr->chunk_name);
197
198 /* Check for too-large chunk length */
199 png_check_chunk_length(png_ptr, length);
200
201#ifdef PNG_IO_STATE_SUPPORTED
202 png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_DATA;
203#endif
204
205 return length;
206}
207
208/* Read data, and (optionally) run it through the CRC. */
209void /* PRIVATE */
210png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length)
211{
212 if (png_ptr == NULL)
213 return;
214
215 png_read_data(png_ptr, buf, length);
216 png_calculate_crc(png_ptr, buf, length);
217}
218
219/* Optionally skip data and then check the CRC. Depending on whether we
220 * are reading an ancillary or critical chunk, and how the program has set
221 * things up, we may calculate the CRC on the data and print a message.
222 * Returns '1' if there was a CRC error, '0' otherwise.
223 */
224int /* PRIVATE */
225png_crc_finish(png_structrp png_ptr, png_uint_32 skip)
226{
227 /* The size of the local buffer for inflate is a good guess as to a
228 * reasonable size to use for buffering reads from the application.
229 */
230 while (skip > 0)
231 {
232 png_uint_32 len;
233 png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];
234
235 len = (sizeof tmpbuf);
236 if (len > skip)
237 len = skip;
238 skip -= len;
239
240 png_crc_read(png_ptr, tmpbuf, len);
241 }
242
243 if (png_crc_error(png_ptr) != 0)
244 {
245 if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) != 0 ?
246 (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) == 0 :
247 (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE) != 0)
248 {
249 png_chunk_warning(png_ptr, "CRC error");
250 }
251
252 else
253 png_chunk_error(png_ptr, "CRC error");
254
255 return 1;
256 }
257
258 return 0;
259}
260
261/* Compare the CRC stored in the PNG file with that calculated by libpng from
262 * the data it has read thus far.
263 */
264int /* PRIVATE */
265png_crc_error(png_structrp png_ptr)
266{
267 png_byte crc_bytes[4];
268 png_uint_32 crc;
269 int need_crc = 1;
270
271 if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) != 0)
272 {
273 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
274 (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
275 need_crc = 0;
276 }
277
278 else /* critical */
279 {
280 if ((png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) != 0)
281 need_crc = 0;
282 }
283
284#ifdef PNG_IO_STATE_SUPPORTED
285 png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_CRC;
286#endif
287
288 /* The chunk CRC must be serialized in a single I/O call. */
289 png_read_data(png_ptr, crc_bytes, 4);
290
291 if (need_crc != 0)
292 {
293 crc = png_get_uint_32(crc_bytes);
294 return crc != png_ptr->crc;
295 }
296
297 else
298 return 0;
299}
300
301#if defined(PNG_READ_iCCP_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) ||\
302 defined(PNG_READ_pCAL_SUPPORTED) || defined(PNG_READ_sCAL_SUPPORTED) ||\
303 defined(PNG_READ_sPLT_SUPPORTED) || defined(PNG_READ_tEXt_SUPPORTED) ||\
304 defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_SEQUENTIAL_READ_SUPPORTED)
305/* Manage the read buffer; this simply reallocates the buffer if it is not small
306 * enough (or if it is not allocated). The routine returns a pointer to the
307 * buffer; if an error occurs and 'warn' is set the routine returns NULL, else
308 * it will call png_error (via png_malloc) on failure. (warn == 2 means
309 * 'silent').
310 */
311static png_bytep
312png_read_buffer(png_structrp png_ptr, png_alloc_size_t new_size, int warn)
313{
314 png_bytep buffer = png_ptr->read_buffer;
315
316 if (buffer != NULL && new_size > png_ptr->read_buffer_size)
317 {
318 png_ptr->read_buffer = NULL;
319 png_ptr->read_buffer_size = 0;
320 png_free(png_ptr, buffer);
321 buffer = NULL;
322 }
323
324 if (buffer == NULL)
325 {
326 buffer = png_voidcast(png_bytep, png_malloc_base(png_ptr, new_size));
327
328 if (buffer != NULL)
329 {
330 memset(buffer, 0, new_size); /* just in case */
331 png_ptr->read_buffer = buffer;
332 png_ptr->read_buffer_size = new_size;
333 }
334
335 else if (warn < 2) /* else silent */
336 {
337 if (warn != 0)
338 png_chunk_warning(png_ptr, "insufficient memory to read chunk");
339
340 else
341 png_chunk_error(png_ptr, "insufficient memory to read chunk");
342 }
343 }
344
345 return buffer;
346}
347#endif /* READ_iCCP|iTXt|pCAL|sCAL|sPLT|tEXt|zTXt|SEQUENTIAL_READ */
348
349/* png_inflate_claim: claim the zstream for some nefarious purpose that involves
350 * decompression. Returns Z_OK on success, else a zlib error code. It checks
351 * the owner but, in final release builds, just issues a warning if some other
352 * chunk apparently owns the stream. Prior to release it does a png_error.
353 */
354static int
355png_inflate_claim(png_structrp png_ptr, png_uint_32 owner)
356{
357 if (png_ptr->zowner != 0)
358 {
359 char msg[64];
360
361 PNG_STRING_FROM_CHUNK(msg, png_ptr->zowner);
362 /* So the message that results is "<chunk> using zstream"; this is an
363 * internal error, but is very useful for debugging. i18n requirements
364 * are minimal.
365 */
366 (void)png_safecat(msg, (sizeof msg), 4, " using zstream");
367#if PNG_RELEASE_BUILD
368 png_chunk_warning(png_ptr, msg);
369 png_ptr->zowner = 0;
370#else
371 png_chunk_error(png_ptr, msg);
372#endif
373 }
374
375 /* Implementation note: unlike 'png_deflate_claim' this internal function
376 * does not take the size of the data as an argument. Some efficiency could
377 * be gained by using this when it is known *if* the zlib stream itself does
378 * not record the number; however, this is an illusion: the original writer
379 * of the PNG may have selected a lower window size, and we really must
380 * follow that because, for systems with with limited capabilities, we
381 * would otherwise reject the application's attempts to use a smaller window
382 * size (zlib doesn't have an interface to say "this or lower"!).
383 *
384 * inflateReset2 was added to zlib 1.2.4; before this the window could not be
385 * reset, therefore it is necessary to always allocate the maximum window
386 * size with earlier zlibs just in case later compressed chunks need it.
387 */
388 {
389 int ret; /* zlib return code */
390#if ZLIB_VERNUM >= 0x1240
391 int window_bits = 0;
392
393# if defined(PNG_SET_OPTION_SUPPORTED) && defined(PNG_MAXIMUM_INFLATE_WINDOW)
394 if (((png_ptr->options >> PNG_MAXIMUM_INFLATE_WINDOW) & 3) ==
395 PNG_OPTION_ON)
396 {
397 window_bits = 15;
398 png_ptr->zstream_start = 0; /* fixed window size */
399 }
400
401 else
402 {
403 png_ptr->zstream_start = 1;
404 }
405# endif
406
407#endif /* ZLIB_VERNUM >= 0x1240 */
408
409 /* Set this for safety, just in case the previous owner left pointers to
410 * memory allocations.
411 */
412 png_ptr->zstream.next_in = NULL;
413 png_ptr->zstream.avail_in = 0;
414 png_ptr->zstream.next_out = NULL;
415 png_ptr->zstream.avail_out = 0;
416
417 if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0)
418 {
419#if ZLIB_VERNUM >= 0x1240
420 ret = inflateReset2(&png_ptr->zstream, window_bits);
421#else
422 ret = inflateReset(&png_ptr->zstream);
423#endif
424 }
425
426 else
427 {
428#if ZLIB_VERNUM >= 0x1240
429 ret = inflateInit2(&png_ptr->zstream, window_bits);
430#else
431 ret = inflateInit(&png_ptr->zstream);
432#endif
433
434 if (ret == Z_OK)
435 png_ptr->flags |= PNG_FLAG_ZSTREAM_INITIALIZED;
436 }
437
438#ifdef PNG_DISABLE_ADLER32_CHECK_SUPPORTED
439 if (((png_ptr->options >> PNG_IGNORE_ADLER32) & 3) == PNG_OPTION_ON)
440 /* Turn off validation of the ADLER32 checksum in IDAT chunks */
441 ret = inflateValidate(&png_ptr->zstream, 0);
442#endif
443
444 if (ret == Z_OK)
445 png_ptr->zowner = owner;
446
447 else
448 png_zstream_error(png_ptr, ret);
449
450 return ret;
451 }
452
453#ifdef window_bits
454# undef window_bits
455#endif
456}
457
458#if ZLIB_VERNUM >= 0x1240
459/* Handle the start of the inflate stream if we called inflateInit2(strm,0);
460 * in this case some zlib versions skip validation of the CINFO field and, in
461 * certain circumstances, libpng may end up displaying an invalid image, in
462 * contrast to implementations that call zlib in the normal way (e.g. libpng
463 * 1.5).
464 */
465int /* PRIVATE */
466png_zlib_inflate(png_structrp png_ptr, int flush)
467{
468 if (png_ptr->zstream_start && png_ptr->zstream.avail_in > 0)
469 {
470 if ((*png_ptr->zstream.next_in >> 4) > 7)
471 {
472 png_ptr->zstream.msg = "invalid window size (libpng)";
473 return Z_DATA_ERROR;
474 }
475
476 png_ptr->zstream_start = 0;
477 }
478
479 return inflate(&png_ptr->zstream, flush);
480}
481#endif /* Zlib >= 1.2.4 */
482
483#ifdef PNG_READ_COMPRESSED_TEXT_SUPPORTED
484#if defined(PNG_READ_zTXt_SUPPORTED) || defined (PNG_READ_iTXt_SUPPORTED)
485/* png_inflate now returns zlib error codes including Z_OK and Z_STREAM_END to
486 * allow the caller to do multiple calls if required. If the 'finish' flag is
487 * set Z_FINISH will be passed to the final inflate() call and Z_STREAM_END must
488 * be returned or there has been a problem, otherwise Z_SYNC_FLUSH is used and
489 * Z_OK or Z_STREAM_END will be returned on success.
490 *
491 * The input and output sizes are updated to the actual amounts of data consumed
492 * or written, not the amount available (as in a z_stream). The data pointers
493 * are not changed, so the next input is (data+input_size) and the next
494 * available output is (output+output_size).
495 */
496static int
497png_inflate(png_structrp png_ptr, png_uint_32 owner, int finish,
498 /* INPUT: */ png_const_bytep input, png_uint_32p input_size_ptr,
499 /* OUTPUT: */ png_bytep output, png_alloc_size_t *output_size_ptr)
500{
501 if (png_ptr->zowner == owner) /* Else not claimed */
502 {
503 int ret;
504 png_alloc_size_t avail_out = *output_size_ptr;
505 png_uint_32 avail_in = *input_size_ptr;
506
507 /* zlib can't necessarily handle more than 65535 bytes at once (i.e. it
508 * can't even necessarily handle 65536 bytes) because the type uInt is
509 * "16 bits or more". Consequently it is necessary to chunk the input to
510 * zlib. This code uses ZLIB_IO_MAX, from pngpriv.h, as the maximum (the
511 * maximum value that can be stored in a uInt.) It is possible to set
512 * ZLIB_IO_MAX to a lower value in pngpriv.h and this may sometimes have
513 * a performance advantage, because it reduces the amount of data accessed
514 * at each step and that may give the OS more time to page it in.
515 */
516 png_ptr->zstream.next_in = PNGZ_INPUT_CAST(input);
517 /* avail_in and avail_out are set below from 'size' */
518 png_ptr->zstream.avail_in = 0;
519 png_ptr->zstream.avail_out = 0;
520
521 /* Read directly into the output if it is available (this is set to
522 * a local buffer below if output is NULL).
523 */
524 if (output != NULL)
525 png_ptr->zstream.next_out = output;
526
527 do
528 {
529 uInt avail;
530 Byte local_buffer[PNG_INFLATE_BUF_SIZE];
531
532 /* zlib INPUT BUFFER */
533 /* The setting of 'avail_in' used to be outside the loop; by setting it
534 * inside it is possible to chunk the input to zlib and simply rely on
535 * zlib to advance the 'next_in' pointer. This allows arbitrary
536 * amounts of data to be passed through zlib at the unavoidable cost of
537 * requiring a window save (memcpy of up to 32768 output bytes)
538 * every ZLIB_IO_MAX input bytes.
539 */
540 avail_in += png_ptr->zstream.avail_in; /* not consumed last time */
541
542 avail = ZLIB_IO_MAX;
543
544 if (avail_in < avail)
545 avail = (uInt)avail_in; /* safe: < than ZLIB_IO_MAX */
546
547 avail_in -= avail;
548 png_ptr->zstream.avail_in = avail;
549
550 /* zlib OUTPUT BUFFER */
551 avail_out += png_ptr->zstream.avail_out; /* not written last time */
552
553 avail = ZLIB_IO_MAX; /* maximum zlib can process */
554
555 if (output == NULL)
556 {
557 /* Reset the output buffer each time round if output is NULL and
558 * make available the full buffer, up to 'remaining_space'
559 */
560 png_ptr->zstream.next_out = local_buffer;
561 if ((sizeof local_buffer) < avail)
562 avail = (sizeof local_buffer);
563 }
564
565 if (avail_out < avail)
566 avail = (uInt)avail_out; /* safe: < ZLIB_IO_MAX */
567
568 png_ptr->zstream.avail_out = avail;
569 avail_out -= avail;
570
571 /* zlib inflate call */
572 /* In fact 'avail_out' may be 0 at this point, that happens at the end
573 * of the read when the final LZ end code was not passed at the end of
574 * the previous chunk of input data. Tell zlib if we have reached the
575 * end of the output buffer.
576 */
577 ret = PNG_INFLATE(png_ptr, avail_out > 0 ? Z_NO_FLUSH :
578 (finish ? Z_FINISH : Z_SYNC_FLUSH));
579 } while (ret == Z_OK);
580
581 /* For safety kill the local buffer pointer now */
582 if (output == NULL)
583 png_ptr->zstream.next_out = NULL;
584
585 /* Claw back the 'size' and 'remaining_space' byte counts. */
586 avail_in += png_ptr->zstream.avail_in;
587 avail_out += png_ptr->zstream.avail_out;
588
589 /* Update the input and output sizes; the updated values are the amount
590 * consumed or written, effectively the inverse of what zlib uses.
591 */
592 if (avail_out > 0)
593 *output_size_ptr -= avail_out;
594
595 if (avail_in > 0)
596 *input_size_ptr -= avail_in;
597
598 /* Ensure png_ptr->zstream.msg is set (even in the success case!) */
599 png_zstream_error(png_ptr, ret);
600 return ret;
601 }
602
603 else
604 {
605 /* This is a bad internal error. The recovery assigns to the zstream msg
606 * pointer, which is not owned by the caller, but this is safe; it's only
607 * used on errors!
608 */
609 png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed");
610 return Z_STREAM_ERROR;
611 }
612}
613
614/*
615 * Decompress trailing data in a chunk. The assumption is that read_buffer
616 * points at an allocated area holding the contents of a chunk with a
617 * trailing compressed part. What we get back is an allocated area
618 * holding the original prefix part and an uncompressed version of the
619 * trailing part (the malloc area passed in is freed).
620 */
621static int
622png_decompress_chunk(png_structrp png_ptr,
623 png_uint_32 chunklength, png_uint_32 prefix_size,
624 png_alloc_size_t *newlength /* must be initialized to the maximum! */,
625 int terminate /*add a '\0' to the end of the uncompressed data*/)
626{
627 /* TODO: implement different limits for different types of chunk.
628 *
629 * The caller supplies *newlength set to the maximum length of the
630 * uncompressed data, but this routine allocates space for the prefix and
631 * maybe a '\0' terminator too. We have to assume that 'prefix_size' is
632 * limited only by the maximum chunk size.
633 */
634 png_alloc_size_t limit = PNG_SIZE_MAX;
635
636# ifdef PNG_SET_USER_LIMITS_SUPPORTED
637 if (png_ptr->user_chunk_malloc_max > 0 &&
638 png_ptr->user_chunk_malloc_max < limit)
639 limit = png_ptr->user_chunk_malloc_max;
640# elif PNG_USER_CHUNK_MALLOC_MAX > 0
641 if (PNG_USER_CHUNK_MALLOC_MAX < limit)
642 limit = PNG_USER_CHUNK_MALLOC_MAX;
643# endif
644
645 if (limit >= prefix_size + (terminate != 0))
646 {
647 int ret;
648
649 limit -= prefix_size + (terminate != 0);
650
651 if (limit < *newlength)
652 *newlength = limit;
653
654 /* Now try to claim the stream. */
655 ret = png_inflate_claim(png_ptr, png_ptr->chunk_name);
656
657 if (ret == Z_OK)
658 {
659 png_uint_32 lzsize = chunklength - prefix_size;
660
661 ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/,
662 /* input: */ png_ptr->read_buffer + prefix_size, &lzsize,
663 /* output: */ NULL, newlength);
664
665 if (ret == Z_STREAM_END)
666 {
667 /* Use 'inflateReset' here, not 'inflateReset2' because this
668 * preserves the previously decided window size (otherwise it would
669 * be necessary to store the previous window size.) In practice
670 * this doesn't matter anyway, because png_inflate will call inflate
671 * with Z_FINISH in almost all cases, so the window will not be
672 * maintained.
673 */
674 if (inflateReset(&png_ptr->zstream) == Z_OK)
675 {
676 /* Because of the limit checks above we know that the new,
677 * expanded, size will fit in a size_t (let alone an
678 * png_alloc_size_t). Use png_malloc_base here to avoid an
679 * extra OOM message.
680 */
681 png_alloc_size_t new_size = *newlength;
682 png_alloc_size_t buffer_size = prefix_size + new_size +
683 (terminate != 0);
684 png_bytep text = png_voidcast(png_bytep, png_malloc_base(png_ptr,
685 buffer_size));
686
687 if (text != NULL)
688 {
689 memset(text, 0, buffer_size);
690
691 ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/,
692 png_ptr->read_buffer + prefix_size, &lzsize,
693 text + prefix_size, newlength);
694
695 if (ret == Z_STREAM_END)
696 {
697 if (new_size == *newlength)
698 {
699 if (terminate != 0)
700 text[prefix_size + *newlength] = 0;
701
702 if (prefix_size > 0)
703 memcpy(text, png_ptr->read_buffer, prefix_size);
704
705 {
706 png_bytep old_ptr = png_ptr->read_buffer;
707
708 png_ptr->read_buffer = text;
709 png_ptr->read_buffer_size = buffer_size;
710 text = old_ptr; /* freed below */
711 }
712 }
713
714 else
715 {
716 /* The size changed on the second read, there can be no
717 * guarantee that anything is correct at this point.
718 * The 'msg' pointer has been set to "unexpected end of
719 * LZ stream", which is fine, but return an error code
720 * that the caller won't accept.
721 */
722 ret = PNG_UNEXPECTED_ZLIB_RETURN;
723 }
724 }
725
726 else if (ret == Z_OK)
727 ret = PNG_UNEXPECTED_ZLIB_RETURN; /* for safety */
728
729 /* Free the text pointer (this is the old read_buffer on
730 * success)
731 */
732 png_free(png_ptr, text);
733
734 /* This really is very benign, but it's still an error because
735 * the extra space may otherwise be used as a Trojan Horse.
736 */
737 if (ret == Z_STREAM_END &&
738 chunklength - prefix_size != lzsize)
739 png_chunk_benign_error(png_ptr, "extra compressed data");
740 }
741
742 else
743 {
744 /* Out of memory allocating the buffer */
745 ret = Z_MEM_ERROR;
746 png_zstream_error(png_ptr, Z_MEM_ERROR);
747 }
748 }
749
750 else
751 {
752 /* inflateReset failed, store the error message */
753 png_zstream_error(png_ptr, ret);
754 ret = PNG_UNEXPECTED_ZLIB_RETURN;
755 }
756 }
757
758 else if (ret == Z_OK)
759 ret = PNG_UNEXPECTED_ZLIB_RETURN;
760
761 /* Release the claimed stream */
762 png_ptr->zowner = 0;
763 }
764
765 else /* the claim failed */ if (ret == Z_STREAM_END) /* impossible! */
766 ret = PNG_UNEXPECTED_ZLIB_RETURN;
767
768 return ret;
769 }
770
771 else
772 {
773 /* Application/configuration limits exceeded */
774 png_zstream_error(png_ptr, Z_MEM_ERROR);
775 return Z_MEM_ERROR;
776 }
777}
778#endif /* READ_zTXt || READ_iTXt */
779#endif /* READ_COMPRESSED_TEXT */
780
781#ifdef PNG_READ_iCCP_SUPPORTED
782/* Perform a partial read and decompress, producing 'avail_out' bytes and
783 * reading from the current chunk as required.
784 */
785static int
786png_inflate_read(png_structrp png_ptr, png_bytep read_buffer, uInt read_size,
787 png_uint_32p chunk_bytes, png_bytep next_out, png_alloc_size_t *out_size,
788 int finish)
789{
790 if (png_ptr->zowner == png_ptr->chunk_name)
791 {
792 int ret;
793
794 /* next_in and avail_in must have been initialized by the caller. */
795 png_ptr->zstream.next_out = next_out;
796 png_ptr->zstream.avail_out = 0; /* set in the loop */
797
798 do
799 {
800 if (png_ptr->zstream.avail_in == 0)
801 {
802 if (read_size > *chunk_bytes)
803 read_size = (uInt)*chunk_bytes;
804 *chunk_bytes -= read_size;
805
806 if (read_size > 0)
807 png_crc_read(png_ptr, read_buffer, read_size);
808
809 png_ptr->zstream.next_in = read_buffer;
810 png_ptr->zstream.avail_in = read_size;
811 }
812
813 if (png_ptr->zstream.avail_out == 0)
814 {
815 uInt avail = ZLIB_IO_MAX;
816 if (avail > *out_size)
817 avail = (uInt)*out_size;
818 *out_size -= avail;
819
820 png_ptr->zstream.avail_out = avail;
821 }
822
823 /* Use Z_SYNC_FLUSH when there is no more chunk data to ensure that all
824 * the available output is produced; this allows reading of truncated
825 * streams.
826 */
827 ret = PNG_INFLATE(png_ptr, *chunk_bytes > 0 ?
828 Z_NO_FLUSH : (finish ? Z_FINISH : Z_SYNC_FLUSH));
829 }
830 while (ret == Z_OK && (*out_size > 0 || png_ptr->zstream.avail_out > 0));
831
832 *out_size += png_ptr->zstream.avail_out;
833 png_ptr->zstream.avail_out = 0; /* Should not be required, but is safe */
834
835 /* Ensure the error message pointer is always set: */
836 png_zstream_error(png_ptr, ret);
837 return ret;
838 }
839
840 else
841 {
842 png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed");
843 return Z_STREAM_ERROR;
844 }
845}
846#endif /* READ_iCCP */
847
848/* Read and check the IDHR chunk */
849
850void /* PRIVATE */
851png_handle_IHDR(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
852{
853 png_byte buf[13];
854 png_uint_32 width, height;
855 int bit_depth, color_type, compression_type, filter_type;
856 int interlace_type;
857
858 png_debug(1, "in png_handle_IHDR");
859
860 if ((png_ptr->mode & PNG_HAVE_IHDR) != 0)
861 png_chunk_error(png_ptr, "out of place");
862
863 /* Check the length */
864 if (length != 13)
865 png_chunk_error(png_ptr, "invalid");
866
867 png_ptr->mode |= PNG_HAVE_IHDR;
868
869 png_crc_read(png_ptr, buf, 13);
870 png_crc_finish(png_ptr, 0);
871
872 width = png_get_uint_31(png_ptr, buf);
873 height = png_get_uint_31(png_ptr, buf + 4);
874 bit_depth = buf[8];
875 color_type = buf[9];
876 compression_type = buf[10];
877 filter_type = buf[11];
878 interlace_type = buf[12];
879
880 /* Set internal variables */
881 png_ptr->width = width;
882 png_ptr->height = height;
883 png_ptr->bit_depth = (png_byte)bit_depth;
884 png_ptr->interlaced = (png_byte)interlace_type;
885 png_ptr->color_type = (png_byte)color_type;
886#ifdef PNG_MNG_FEATURES_SUPPORTED
887 png_ptr->filter_type = (png_byte)filter_type;
888#endif
889 png_ptr->compression_type = (png_byte)compression_type;
890
891 /* Find number of channels */
892 switch (png_ptr->color_type)
893 {
894 default: /* invalid, png_set_IHDR calls png_error */
895 case PNG_COLOR_TYPE_GRAY:
896 case PNG_COLOR_TYPE_PALETTE:
897 png_ptr->channels = 1;
898 break;
899
900 case PNG_COLOR_TYPE_RGB:
901 png_ptr->channels = 3;
902 break;
903
904 case PNG_COLOR_TYPE_GRAY_ALPHA:
905 png_ptr->channels = 2;
906 break;
907
908 case PNG_COLOR_TYPE_RGB_ALPHA:
909 png_ptr->channels = 4;
910 break;
911 }
912
913 /* Set up other useful info */
914 png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth * png_ptr->channels);
915 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width);
916 png_debug1(3, "bit_depth = %d", png_ptr->bit_depth);
917 png_debug1(3, "channels = %d", png_ptr->channels);
918 png_debug1(3, "rowbytes = %lu", (unsigned long)png_ptr->rowbytes);
919 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
920 color_type, interlace_type, compression_type, filter_type);
921}
922
923/* Read and check the palette */
924void /* PRIVATE */
925png_handle_PLTE(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
926{
927 png_color palette[PNG_MAX_PALETTE_LENGTH];
928 int max_palette_length, num, i;
929#ifdef PNG_POINTER_INDEXING_SUPPORTED
930 png_colorp pal_ptr;
931#endif
932
933 png_debug(1, "in png_handle_PLTE");
934
935 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
936 png_chunk_error(png_ptr, "missing IHDR");
937
938 /* Moved to before the 'after IDAT' check below because otherwise duplicate
939 * PLTE chunks are potentially ignored (the spec says there shall not be more
940 * than one PLTE, the error is not treated as benign, so this check trumps
941 * the requirement that PLTE appears before IDAT.)
942 */
943 else if ((png_ptr->mode & PNG_HAVE_PLTE) != 0)
944 png_chunk_error(png_ptr, "duplicate");
945
946 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
947 {
948 /* This is benign because the non-benign error happened before, when an
949 * IDAT was encountered in a color-mapped image with no PLTE.
950 */
951 png_crc_finish(png_ptr, length);
952 png_chunk_benign_error(png_ptr, "out of place");
953 return;
954 }
955
956 png_ptr->mode |= PNG_HAVE_PLTE;
957
958 if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0)
959 {
960 png_crc_finish(png_ptr, length);
961 png_chunk_benign_error(png_ptr, "ignored in grayscale PNG");
962 return;
963 }
964
965#ifndef PNG_READ_OPT_PLTE_SUPPORTED
966 if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
967 {
968 png_crc_finish(png_ptr, length);
969 return;
970 }
971#endif
972
973 if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3)
974 {
975 png_crc_finish(png_ptr, length);
976
977 if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
978 png_chunk_benign_error(png_ptr, "invalid");
979
980 else
981 png_chunk_error(png_ptr, "invalid");
982
983 return;
984 }
985
986 /* The cast is safe because 'length' is less than 3*PNG_MAX_PALETTE_LENGTH */
987 num = (int)length / 3;
988
989 /* If the palette has 256 or fewer entries but is too large for the bit
990 * depth, we don't issue an error, to preserve the behavior of previous
991 * libpng versions. We silently truncate the unused extra palette entries
992 * here.
993 */
994 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
995 max_palette_length = (1 << png_ptr->bit_depth);
996 else
997 max_palette_length = PNG_MAX_PALETTE_LENGTH;
998
999 if (num > max_palette_length)
1000 num = max_palette_length;
1001
1002#ifdef PNG_POINTER_INDEXING_SUPPORTED
1003 for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)
1004 {
1005 png_byte buf[3];
1006
1007 png_crc_read(png_ptr, buf, 3);
1008 pal_ptr->red = buf[0];
1009 pal_ptr->green = buf[1];
1010 pal_ptr->blue = buf[2];
1011 }
1012#else
1013 for (i = 0; i < num; i++)
1014 {
1015 png_byte buf[3];
1016
1017 png_crc_read(png_ptr, buf, 3);
1018 /* Don't depend upon png_color being any order */
1019 palette[i].red = buf[0];
1020 palette[i].green = buf[1];
1021 palette[i].blue = buf[2];
1022 }
1023#endif
1024
1025 /* If we actually need the PLTE chunk (ie for a paletted image), we do
1026 * whatever the normal CRC configuration tells us. However, if we
1027 * have an RGB image, the PLTE can be considered ancillary, so
1028 * we will act as though it is.
1029 */
1030#ifndef PNG_READ_OPT_PLTE_SUPPORTED
1031 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1032#endif
1033 {
1034 png_crc_finish(png_ptr, (png_uint_32) (length - (unsigned int)num * 3));
1035 }
1036
1037#ifndef PNG_READ_OPT_PLTE_SUPPORTED
1038 else if (png_crc_error(png_ptr) != 0) /* Only if we have a CRC error */
1039 {
1040 /* If we don't want to use the data from an ancillary chunk,
1041 * we have two options: an error abort, or a warning and we
1042 * ignore the data in this chunk (which should be OK, since
1043 * it's considered ancillary for a RGB or RGBA image).
1044 *
1045 * IMPLEMENTATION NOTE: this is only here because png_crc_finish uses the
1046 * chunk type to determine whether to check the ancillary or the critical
1047 * flags.
1048 */
1049 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE) == 0)
1050 {
1051 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) != 0)
1052 return;
1053
1054 else
1055 png_chunk_error(png_ptr, "CRC error");
1056 }
1057
1058 /* Otherwise, we (optionally) emit a warning and use the chunk. */
1059 else if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) == 0)
1060 png_chunk_warning(png_ptr, "CRC error");
1061 }
1062#endif
1063
1064 /* TODO: png_set_PLTE has the side effect of setting png_ptr->palette to its
1065 * own copy of the palette. This has the side effect that when png_start_row
1066 * is called (this happens after any call to png_read_update_info) the
1067 * info_ptr palette gets changed. This is extremely unexpected and
1068 * confusing.
1069 *
1070 * Fix this by not sharing the palette in this way.
1071 */
1072 png_set_PLTE(png_ptr, info_ptr, palette, num);
1073
1074 /* The three chunks, bKGD, hIST and tRNS *must* appear after PLTE and before
1075 * IDAT. Prior to 1.6.0 this was not checked; instead the code merely
1076 * checked the apparent validity of a tRNS chunk inserted before PLTE on a
1077 * palette PNG. 1.6.0 attempts to rigorously follow the standard and
1078 * therefore does a benign error if the erroneous condition is detected *and*
1079 * cancels the tRNS if the benign error returns. The alternative is to
1080 * amend the standard since it would be rather hypocritical of the standards
1081 * maintainers to ignore it.
1082 */
1083#ifdef PNG_READ_tRNS_SUPPORTED
1084 if (png_ptr->num_trans > 0 ||
1085 (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS) != 0))
1086 {
1087 /* Cancel this because otherwise it would be used if the transforms
1088 * require it. Don't cancel the 'valid' flag because this would prevent
1089 * detection of duplicate chunks.
1090 */
1091 png_ptr->num_trans = 0;
1092
1093 if (info_ptr != NULL)
1094 info_ptr->num_trans = 0;
1095
1096 png_chunk_benign_error(png_ptr, "tRNS must be after");
1097 }
1098#endif
1099
1100#ifdef PNG_READ_hIST_SUPPORTED
1101 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) != 0)
1102 png_chunk_benign_error(png_ptr, "hIST must be after");
1103#endif
1104
1105#ifdef PNG_READ_bKGD_SUPPORTED
1106 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) != 0)
1107 png_chunk_benign_error(png_ptr, "bKGD must be after");
1108#endif
1109}
1110
1111void /* PRIVATE */
1112png_handle_IEND(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1113{
1114 png_debug(1, "in png_handle_IEND");
1115
1116 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0 ||
1117 (png_ptr->mode & PNG_HAVE_IDAT) == 0)
1118 png_chunk_error(png_ptr, "out of place");
1119
1120 png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND);
1121
1122 png_crc_finish(png_ptr, length);
1123
1124 if (length != 0)
1125 png_chunk_benign_error(png_ptr, "invalid");
1126
1127 PNG_UNUSED(info_ptr)
1128}
1129
1130#ifdef PNG_READ_gAMA_SUPPORTED
1131void /* PRIVATE */
1132png_handle_gAMA(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1133{
1134 png_fixed_point igamma;
1135 png_byte buf[4];
1136
1137 png_debug(1, "in png_handle_gAMA");
1138
1139 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1140 png_chunk_error(png_ptr, "missing IHDR");
1141
1142 else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
1143 {
1144 png_crc_finish(png_ptr, length);
1145 png_chunk_benign_error(png_ptr, "out of place");
1146 return;
1147 }
1148
1149 if (length != 4)
1150 {
1151 png_crc_finish(png_ptr, length);
1152 png_chunk_benign_error(png_ptr, "invalid");
1153 return;
1154 }
1155
1156 png_crc_read(png_ptr, buf, 4);
1157
1158 if (png_crc_finish(png_ptr, 0) != 0)
1159 return;
1160
1161 igamma = png_get_fixed_point(NULL, buf);
1162
1163 png_colorspace_set_gamma(png_ptr, &png_ptr->colorspace, igamma);
1164 png_colorspace_sync(png_ptr, info_ptr);
1165}
1166#endif
1167
1168#ifdef PNG_READ_sBIT_SUPPORTED
1169void /* PRIVATE */
1170png_handle_sBIT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1171{
1172 unsigned int truelen, i;
1173 png_byte sample_depth;
1174 png_byte buf[4];
1175
1176 png_debug(1, "in png_handle_sBIT");
1177
1178 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1179 png_chunk_error(png_ptr, "missing IHDR");
1180
1181 else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
1182 {
1183 png_crc_finish(png_ptr, length);
1184 png_chunk_benign_error(png_ptr, "out of place");
1185 return;
1186 }
1187
1188 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT) != 0)
1189 {
1190 png_crc_finish(png_ptr, length);
1191 png_chunk_benign_error(png_ptr, "duplicate");
1192 return;
1193 }
1194
1195 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1196 {
1197 truelen = 3;
1198 sample_depth = 8;
1199 }
1200
1201 else
1202 {
1203 truelen = png_ptr->channels;
1204 sample_depth = png_ptr->bit_depth;
1205 }
1206
1207 if (length != truelen || length > 4)
1208 {
1209 png_chunk_benign_error(png_ptr, "invalid");
1210 png_crc_finish(png_ptr, length);
1211 return;
1212 }
1213
1214 buf[0] = buf[1] = buf[2] = buf[3] = sample_depth;
1215 png_crc_read(png_ptr, buf, truelen);
1216
1217 if (png_crc_finish(png_ptr, 0) != 0)
1218 return;
1219
1220 for (i=0; i<truelen; ++i)
1221 {
1222 if (buf[i] == 0 || buf[i] > sample_depth)
1223 {
1224 png_chunk_benign_error(png_ptr, "invalid");
1225 return;
1226 }
1227 }
1228
1229 if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
1230 {
1231 png_ptr->sig_bit.red = buf[0];
1232 png_ptr->sig_bit.green = buf[1];
1233 png_ptr->sig_bit.blue = buf[2];
1234 png_ptr->sig_bit.alpha = buf[3];
1235 }
1236
1237 else
1238 {
1239 png_ptr->sig_bit.gray = buf[0];
1240 png_ptr->sig_bit.red = buf[0];
1241 png_ptr->sig_bit.green = buf[0];
1242 png_ptr->sig_bit.blue = buf[0];
1243 png_ptr->sig_bit.alpha = buf[1];
1244 }
1245
1246 png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
1247}
1248#endif
1249
1250#ifdef PNG_READ_cHRM_SUPPORTED
1251void /* PRIVATE */
1252png_handle_cHRM(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1253{
1254 png_byte buf[32];
1255 png_xy xy;
1256
1257 png_debug(1, "in png_handle_cHRM");
1258
1259 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1260 png_chunk_error(png_ptr, "missing IHDR");
1261
1262 else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
1263 {
1264 png_crc_finish(png_ptr, length);
1265 png_chunk_benign_error(png_ptr, "out of place");
1266 return;
1267 }
1268
1269 if (length != 32)
1270 {
1271 png_crc_finish(png_ptr, length);
1272 png_chunk_benign_error(png_ptr, "invalid");
1273 return;
1274 }
1275
1276 png_crc_read(png_ptr, buf, 32);
1277
1278 if (png_crc_finish(png_ptr, 0) != 0)
1279 return;
1280
1281 xy.whitex = png_get_fixed_point(NULL, buf);
1282 xy.whitey = png_get_fixed_point(NULL, buf + 4);
1283 xy.redx = png_get_fixed_point(NULL, buf + 8);
1284 xy.redy = png_get_fixed_point(NULL, buf + 12);
1285 xy.greenx = png_get_fixed_point(NULL, buf + 16);
1286 xy.greeny = png_get_fixed_point(NULL, buf + 20);
1287 xy.bluex = png_get_fixed_point(NULL, buf + 24);
1288 xy.bluey = png_get_fixed_point(NULL, buf + 28);
1289
1290 if (xy.whitex == PNG_FIXED_ERROR ||
1291 xy.whitey == PNG_FIXED_ERROR ||
1292 xy.redx == PNG_FIXED_ERROR ||
1293 xy.redy == PNG_FIXED_ERROR ||
1294 xy.greenx == PNG_FIXED_ERROR ||
1295 xy.greeny == PNG_FIXED_ERROR ||
1296 xy.bluex == PNG_FIXED_ERROR ||
1297 xy.bluey == PNG_FIXED_ERROR)
1298 {
1299 png_chunk_benign_error(png_ptr, "invalid values");
1300 return;
1301 }
1302
1303 /* If a colorspace error has already been output skip this chunk */
1304 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0)
1305 return;
1306
1307 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM) != 0)
1308 {
1309 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1310 png_colorspace_sync(png_ptr, info_ptr);
1311 png_chunk_benign_error(png_ptr, "duplicate");
1312 return;
1313 }
1314
1315 png_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM;
1316 (void)png_colorspace_set_chromaticities(png_ptr, &png_ptr->colorspace, &xy,
1317 1/*prefer cHRM values*/);
1318 png_colorspace_sync(png_ptr, info_ptr);
1319}
1320#endif
1321
1322#ifdef PNG_READ_sRGB_SUPPORTED
1323void /* PRIVATE */
1324png_handle_sRGB(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1325{
1326 png_byte intent;
1327
1328 png_debug(1, "in png_handle_sRGB");
1329
1330 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1331 png_chunk_error(png_ptr, "missing IHDR");
1332
1333 else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
1334 {
1335 png_crc_finish(png_ptr, length);
1336 png_chunk_benign_error(png_ptr, "out of place");
1337 return;
1338 }
1339
1340 if (length != 1)
1341 {
1342 png_crc_finish(png_ptr, length);
1343 png_chunk_benign_error(png_ptr, "invalid");
1344 return;
1345 }
1346
1347 png_crc_read(png_ptr, &intent, 1);
1348
1349 if (png_crc_finish(png_ptr, 0) != 0)
1350 return;
1351
1352 /* If a colorspace error has already been output skip this chunk */
1353 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0)
1354 return;
1355
1356 /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect
1357 * this.
1358 */
1359 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) != 0)
1360 {
1361 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1362 png_colorspace_sync(png_ptr, info_ptr);
1363 png_chunk_benign_error(png_ptr, "too many profiles");
1364 return;
1365 }
1366
1367 (void)png_colorspace_set_sRGB(png_ptr, &png_ptr->colorspace, intent);
1368 png_colorspace_sync(png_ptr, info_ptr);
1369}
1370#endif /* READ_sRGB */
1371
1372#ifdef PNG_READ_iCCP_SUPPORTED
1373void /* PRIVATE */
1374png_handle_iCCP(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1375/* Note: this does not properly handle profiles that are > 64K under DOS */
1376{
1377 png_const_charp errmsg = NULL; /* error message output, or no error */
1378 int finished = 0; /* crc checked */
1379
1380 png_debug(1, "in png_handle_iCCP");
1381
1382 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1383 png_chunk_error(png_ptr, "missing IHDR");
1384
1385 else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
1386 {
1387 png_crc_finish(png_ptr, length);
1388 png_chunk_benign_error(png_ptr, "out of place");
1389 return;
1390 }
1391
1392 /* Consistent with all the above colorspace handling an obviously *invalid*
1393 * chunk is just ignored, so does not invalidate the color space. An
1394 * alternative is to set the 'invalid' flags at the start of this routine
1395 * and only clear them in they were not set before and all the tests pass.
1396 */
1397
1398 /* The keyword must be at least one character and there is a
1399 * terminator (0) byte and the compression method byte, and the
1400 * 'zlib' datastream is at least 11 bytes.
1401 */
1402 if (length < 14)
1403 {
1404 png_crc_finish(png_ptr, length);
1405 png_chunk_benign_error(png_ptr, "too short");
1406 return;
1407 }
1408
1409 /* If a colorspace error has already been output skip this chunk */
1410 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0)
1411 {
1412 png_crc_finish(png_ptr, length);
1413 return;
1414 }
1415
1416 /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect
1417 * this.
1418 */
1419 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) == 0)
1420 {
1421 uInt read_length, keyword_length;
1422 char keyword[81];
1423
1424 /* Find the keyword; the keyword plus separator and compression method
1425 * bytes can be at most 81 characters long.
1426 */
1427 read_length = 81; /* maximum */
1428 if (read_length > length)
1429 read_length = (uInt)length;
1430
1431 png_crc_read(png_ptr, (png_bytep)keyword, read_length);
1432 length -= read_length;
1433
1434 /* The minimum 'zlib' stream is assumed to be just the 2 byte header,
1435 * 5 bytes minimum 'deflate' stream, and the 4 byte checksum.
1436 */
1437 if (length < 11)
1438 {
1439 png_crc_finish(png_ptr, length);
1440 png_chunk_benign_error(png_ptr, "too short");
1441 return;
1442 }
1443
1444 keyword_length = 0;
1445 while (keyword_length < 80 && keyword_length < read_length &&
1446 keyword[keyword_length] != 0)
1447 ++keyword_length;
1448
1449 /* TODO: make the keyword checking common */
1450 if (keyword_length >= 1 && keyword_length <= 79)
1451 {
1452 /* We only understand '0' compression - deflate - so if we get a
1453 * different value we can't safely decode the chunk.
1454 */
1455 if (keyword_length+1 < read_length &&
1456 keyword[keyword_length+1] == PNG_COMPRESSION_TYPE_BASE)
1457 {
1458 read_length -= keyword_length+2;
1459
1460 if (png_inflate_claim(png_ptr, png_iCCP) == Z_OK)
1461 {
1462 Byte profile_header[132]={0};
1463 Byte local_buffer[PNG_INFLATE_BUF_SIZE];
1464 png_alloc_size_t size = (sizeof profile_header);
1465
1466 png_ptr->zstream.next_in = (Bytef*)keyword + (keyword_length+2);
1467 png_ptr->zstream.avail_in = read_length;
1468 (void)png_inflate_read(png_ptr, local_buffer,
1469 (sizeof local_buffer), &length, profile_header, &size,
1470 0/*finish: don't, because the output is too small*/);
1471
1472 if (size == 0)
1473 {
1474 /* We have the ICC profile header; do the basic header checks.
1475 */
1476 png_uint_32 profile_length = png_get_uint_32(profile_header);
1477
1478 if (png_icc_check_length(png_ptr, &png_ptr->colorspace,
1479 keyword, profile_length) != 0)
1480 {
1481 /* The length is apparently ok, so we can check the 132
1482 * byte header.
1483 */
1484 if (png_icc_check_header(png_ptr, &png_ptr->colorspace,
1485 keyword, profile_length, profile_header,
1486 png_ptr->color_type) != 0)
1487 {
1488 /* Now read the tag table; a variable size buffer is
1489 * needed at this point, allocate one for the whole
1490 * profile. The header check has already validated
1491 * that none of this stuff will overflow.
1492 */
1493 png_uint_32 tag_count =
1494 png_get_uint_32(profile_header + 128);
1495 png_bytep profile = png_read_buffer(png_ptr,
1496 profile_length, 2/*silent*/);
1497
1498 if (profile != NULL)
1499 {
1500 memcpy(profile, profile_header,
1501 (sizeof profile_header));
1502
1503 size = 12 * tag_count;
1504
1505 (void)png_inflate_read(png_ptr, local_buffer,
1506 (sizeof local_buffer), &length,
1507 profile + (sizeof profile_header), &size, 0);
1508
1509 /* Still expect a buffer error because we expect
1510 * there to be some tag data!
1511 */
1512 if (size == 0)
1513 {
1514 if (png_icc_check_tag_table(png_ptr,
1515 &png_ptr->colorspace, keyword, profile_length,
1516 profile) != 0)
1517 {
1518 /* The profile has been validated for basic
1519 * security issues, so read the whole thing in.
1520 */
1521 size = profile_length - (sizeof profile_header)
1522 - 12 * tag_count;
1523
1524 (void)png_inflate_read(png_ptr, local_buffer,
1525 (sizeof local_buffer), &length,
1526 profile + (sizeof profile_header) +
1527 12 * tag_count, &size, 1/*finish*/);
1528
1529 if (length > 0 && !(png_ptr->flags &
1530 PNG_FLAG_BENIGN_ERRORS_WARN))
1531 errmsg = "extra compressed data";
1532
1533 /* But otherwise allow extra data: */
1534 else if (size == 0)
1535 {
1536 if (length > 0)
1537 {
1538 /* This can be handled completely, so
1539 * keep going.
1540 */
1541 png_chunk_warning(png_ptr,
1542 "extra compressed data");
1543 }
1544
1545 png_crc_finish(png_ptr, length);
1546 finished = 1;
1547
1548# if defined(PNG_sRGB_SUPPORTED) && PNG_sRGB_PROFILE_CHECKS >= 0
1549 /* Check for a match against sRGB */
1550 png_icc_set_sRGB(png_ptr,
1551 &png_ptr->colorspace, profile,
1552 png_ptr->zstream.adler);
1553# endif
1554
1555 /* Steal the profile for info_ptr. */
1556 if (info_ptr != NULL)
1557 {
1558 png_free_data(png_ptr, info_ptr,
1559 PNG_FREE_ICCP, 0);
1560
1561 info_ptr->iccp_name = png_voidcast(char*,
1562 png_malloc_base(png_ptr,
1563 keyword_length+1));
1564 if (info_ptr->iccp_name != NULL)
1565 {
1566 memcpy(info_ptr->iccp_name, keyword,
1567 keyword_length+1);
1568 info_ptr->iccp_proflen =
1569 profile_length;
1570 info_ptr->iccp_profile = profile;
1571 png_ptr->read_buffer = NULL; /*steal*/
1572 info_ptr->free_me |= PNG_FREE_ICCP;
1573 info_ptr->valid |= PNG_INFO_iCCP;
1574 }
1575
1576 else
1577 {
1578 png_ptr->colorspace.flags |=
1579 PNG_COLORSPACE_INVALID;
1580 errmsg = "out of memory";
1581 }
1582 }
1583
1584 /* else the profile remains in the read
1585 * buffer which gets reused for subsequent
1586 * chunks.
1587 */
1588
1589 if (info_ptr != NULL)
1590 png_colorspace_sync(png_ptr, info_ptr);
1591
1592 if (errmsg == NULL)
1593 {
1594 png_ptr->zowner = 0;
1595 return;
1596 }
1597 }
1598 if (errmsg == NULL)
1599 errmsg = png_ptr->zstream.msg;
1600 }
1601 /* else png_icc_check_tag_table output an error */
1602 }
1603 else /* profile truncated */
1604 errmsg = png_ptr->zstream.msg;
1605 }
1606
1607 else
1608 errmsg = "out of memory";
1609 }
1610
1611 /* else png_icc_check_header output an error */
1612 }
1613
1614 /* else png_icc_check_length output an error */
1615 }
1616
1617 else /* profile truncated */
1618 errmsg = png_ptr->zstream.msg;
1619
1620 /* Release the stream */
1621 png_ptr->zowner = 0;
1622 }
1623
1624 else /* png_inflate_claim failed */
1625 errmsg = png_ptr->zstream.msg;
1626 }
1627
1628 else
1629 errmsg = "bad compression method"; /* or missing */
1630 }
1631
1632 else
1633 errmsg = "bad keyword";
1634 }
1635
1636 else
1637 errmsg = "too many profiles";
1638
1639 /* Failure: the reason is in 'errmsg' */
1640 if (finished == 0)
1641 png_crc_finish(png_ptr, length);
1642
1643 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1644 png_colorspace_sync(png_ptr, info_ptr);
1645 if (errmsg != NULL) /* else already output */
1646 png_chunk_benign_error(png_ptr, errmsg);
1647}
1648#endif /* READ_iCCP */
1649
1650#ifdef PNG_READ_sPLT_SUPPORTED
1651void /* PRIVATE */
1652png_handle_sPLT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1653/* Note: this does not properly handle chunks that are > 64K under DOS */
1654{
1655 png_bytep entry_start, buffer;
1656 png_sPLT_t new_palette;
1657 png_sPLT_entryp pp;
1658 png_uint_32 data_length;
1659 int entry_size, i;
1660 png_uint_32 skip = 0;
1661 png_uint_32 dl;
1662 size_t max_dl;
1663
1664 png_debug(1, "in png_handle_sPLT");
1665
1666#ifdef PNG_USER_LIMITS_SUPPORTED
1667 if (png_ptr->user_chunk_cache_max != 0)
1668 {
1669 if (png_ptr->user_chunk_cache_max == 1)
1670 {
1671 png_crc_finish(png_ptr, length);
1672 return;
1673 }
1674
1675 if (--png_ptr->user_chunk_cache_max == 1)
1676 {
1677 png_warning(png_ptr, "No space in chunk cache for sPLT");
1678 png_crc_finish(png_ptr, length);
1679 return;
1680 }
1681 }
1682#endif
1683
1684 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1685 png_chunk_error(png_ptr, "missing IHDR");
1686
1687 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
1688 {
1689 png_crc_finish(png_ptr, length);
1690 png_chunk_benign_error(png_ptr, "out of place");
1691 return;
1692 }
1693
1694#ifdef PNG_MAX_MALLOC_64K
1695 if (length > 65535U)
1696 {
1697 png_crc_finish(png_ptr, length);
1698 png_chunk_benign_error(png_ptr, "too large to fit in memory");
1699 return;
1700 }
1701#endif
1702
1703 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
1704 if (buffer == NULL)
1705 {
1706 png_crc_finish(png_ptr, length);
1707 png_chunk_benign_error(png_ptr, "out of memory");
1708 return;
1709 }
1710
1711
1712 /* WARNING: this may break if size_t is less than 32 bits; it is assumed
1713 * that the PNG_MAX_MALLOC_64K test is enabled in this case, but this is a
1714 * potential breakage point if the types in pngconf.h aren't exactly right.
1715 */
1716 png_crc_read(png_ptr, buffer, length);
1717
1718 if (png_crc_finish(png_ptr, skip) != 0)
1719 return;
1720
1721 buffer[length] = 0;
1722
1723 for (entry_start = buffer; *entry_start; entry_start++)
1724 /* Empty loop to find end of name */ ;
1725
1726 ++entry_start;
1727
1728 /* A sample depth should follow the separator, and we should be on it */
1729 if (length < 2U || entry_start > buffer + (length - 2U))
1730 {
1731 png_warning(png_ptr, "malformed sPLT chunk");
1732 return;
1733 }
1734
1735 new_palette.depth = *entry_start++;
1736 entry_size = (new_palette.depth == 8 ? 6 : 10);
1737 /* This must fit in a png_uint_32 because it is derived from the original
1738 * chunk data length.
1739 */
1740 data_length = length - (png_uint_32)(entry_start - buffer);
1741
1742 /* Integrity-check the data length */
1743 if ((data_length % (unsigned int)entry_size) != 0)
1744 {
1745 png_warning(png_ptr, "sPLT chunk has bad length");
1746 return;
1747 }
1748
1749 dl = (png_uint_32)(data_length / (unsigned int)entry_size);
1750 max_dl = PNG_SIZE_MAX / (sizeof (png_sPLT_entry));
1751
1752 if (dl > max_dl)
1753 {
1754 png_warning(png_ptr, "sPLT chunk too long");
1755 return;
1756 }
1757
1758 new_palette.nentries = (png_int_32)(data_length / (unsigned int)entry_size);
1759
1760 new_palette.entries = (png_sPLT_entryp)png_malloc_warn(png_ptr,
1761 (png_alloc_size_t) new_palette.nentries * (sizeof (png_sPLT_entry)));
1762
1763 if (new_palette.entries == NULL)
1764 {
1765 png_warning(png_ptr, "sPLT chunk requires too much memory");
1766 return;
1767 }
1768
1769#ifdef PNG_POINTER_INDEXING_SUPPORTED
1770 for (i = 0; i < new_palette.nentries; i++)
1771 {
1772 pp = new_palette.entries + i;
1773
1774 if (new_palette.depth == 8)
1775 {
1776 pp->red = *entry_start++;
1777 pp->green = *entry_start++;
1778 pp->blue = *entry_start++;
1779 pp->alpha = *entry_start++;
1780 }
1781
1782 else
1783 {
1784 pp->red = png_get_uint_16(entry_start); entry_start += 2;
1785 pp->green = png_get_uint_16(entry_start); entry_start += 2;
1786 pp->blue = png_get_uint_16(entry_start); entry_start += 2;
1787 pp->alpha = png_get_uint_16(entry_start); entry_start += 2;
1788 }
1789
1790 pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
1791 }
1792#else
1793 pp = new_palette.entries;
1794
1795 for (i = 0; i < new_palette.nentries; i++)
1796 {
1797
1798 if (new_palette.depth == 8)
1799 {
1800 pp[i].red = *entry_start++;
1801 pp[i].green = *entry_start++;
1802 pp[i].blue = *entry_start++;
1803 pp[i].alpha = *entry_start++;
1804 }
1805
1806 else
1807 {
1808 pp[i].red = png_get_uint_16(entry_start); entry_start += 2;
1809 pp[i].green = png_get_uint_16(entry_start); entry_start += 2;
1810 pp[i].blue = png_get_uint_16(entry_start); entry_start += 2;
1811 pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2;
1812 }
1813
1814 pp[i].frequency = png_get_uint_16(entry_start); entry_start += 2;
1815 }
1816#endif
1817
1818 /* Discard all chunk data except the name and stash that */
1819 new_palette.name = (png_charp)buffer;
1820
1821 png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);
1822
1823 png_free(png_ptr, new_palette.entries);
1824}
1825#endif /* READ_sPLT */
1826
1827#ifdef PNG_READ_tRNS_SUPPORTED
1828void /* PRIVATE */
1829png_handle_tRNS(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1830{
1831 png_byte readbuf[PNG_MAX_PALETTE_LENGTH];
1832
1833 png_debug(1, "in png_handle_tRNS");
1834
1835 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1836 png_chunk_error(png_ptr, "missing IHDR");
1837
1838 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
1839 {
1840 png_crc_finish(png_ptr, length);
1841 png_chunk_benign_error(png_ptr, "out of place");
1842 return;
1843 }
1844
1845 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS) != 0)
1846 {
1847 png_crc_finish(png_ptr, length);
1848 png_chunk_benign_error(png_ptr, "duplicate");
1849 return;
1850 }
1851
1852 if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
1853 {
1854 png_byte buf[2];
1855
1856 if (length != 2)
1857 {
1858 png_crc_finish(png_ptr, length);
1859 png_chunk_benign_error(png_ptr, "invalid");
1860 return;
1861 }
1862
1863 png_crc_read(png_ptr, buf, 2);
1864 png_ptr->num_trans = 1;
1865 png_ptr->trans_color.gray = png_get_uint_16(buf);
1866 }
1867
1868 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
1869 {
1870 png_byte buf[6];
1871
1872 if (length != 6)
1873 {
1874 png_crc_finish(png_ptr, length);
1875 png_chunk_benign_error(png_ptr, "invalid");
1876 return;
1877 }
1878
1879 png_crc_read(png_ptr, buf, length);
1880 png_ptr->num_trans = 1;
1881 png_ptr->trans_color.red = png_get_uint_16(buf);
1882 png_ptr->trans_color.green = png_get_uint_16(buf + 2);
1883 png_ptr->trans_color.blue = png_get_uint_16(buf + 4);
1884 }
1885
1886 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1887 {
1888 if ((png_ptr->mode & PNG_HAVE_PLTE) == 0)
1889 {
1890 /* TODO: is this actually an error in the ISO spec? */
1891 png_crc_finish(png_ptr, length);
1892 png_chunk_benign_error(png_ptr, "out of place");
1893 return;
1894 }
1895
1896 if (length > (unsigned int) png_ptr->num_palette ||
1897 length > (unsigned int) PNG_MAX_PALETTE_LENGTH ||
1898 length == 0)
1899 {
1900 png_crc_finish(png_ptr, length);
1901 png_chunk_benign_error(png_ptr, "invalid");
1902 return;
1903 }
1904
1905 png_crc_read(png_ptr, readbuf, length);
1906 png_ptr->num_trans = (png_uint_16)length;
1907 }
1908
1909 else
1910 {
1911 png_crc_finish(png_ptr, length);
1912 png_chunk_benign_error(png_ptr, "invalid with alpha channel");
1913 return;
1914 }
1915
1916 if (png_crc_finish(png_ptr, 0) != 0)
1917 {
1918 png_ptr->num_trans = 0;
1919 return;
1920 }
1921
1922 /* TODO: this is a horrible side effect in the palette case because the
1923 * png_struct ends up with a pointer to the tRNS buffer owned by the
1924 * png_info. Fix this.
1925 */
1926 png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,
1927 &(png_ptr->trans_color));
1928}
1929#endif
1930
1931#ifdef PNG_READ_bKGD_SUPPORTED
1932void /* PRIVATE */
1933png_handle_bKGD(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1934{
1935 unsigned int truelen;
1936 png_byte buf[6];
1937 png_color_16 background;
1938
1939 png_debug(1, "in png_handle_bKGD");
1940
1941 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1942 png_chunk_error(png_ptr, "missing IHDR");
1943
1944 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0 ||
1945 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
1946 (png_ptr->mode & PNG_HAVE_PLTE) == 0))
1947 {
1948 png_crc_finish(png_ptr, length);
1949 png_chunk_benign_error(png_ptr, "out of place");
1950 return;
1951 }
1952
1953 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) != 0)
1954 {
1955 png_crc_finish(png_ptr, length);
1956 png_chunk_benign_error(png_ptr, "duplicate");
1957 return;
1958 }
1959
1960 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1961 truelen = 1;
1962
1963 else if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
1964 truelen = 6;
1965
1966 else
1967 truelen = 2;
1968
1969 if (length != truelen)
1970 {
1971 png_crc_finish(png_ptr, length);
1972 png_chunk_benign_error(png_ptr, "invalid");
1973 return;
1974 }
1975
1976 png_crc_read(png_ptr, buf, truelen);
1977
1978 if (png_crc_finish(png_ptr, 0) != 0)
1979 return;
1980
1981 /* We convert the index value into RGB components so that we can allow
1982 * arbitrary RGB values for background when we have transparency, and
1983 * so it is easy to determine the RGB values of the background color
1984 * from the info_ptr struct.
1985 */
1986 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1987 {
1988 background.index = buf[0];
1989
1990 if (info_ptr != NULL && info_ptr->num_palette != 0)
1991 {
1992 if (buf[0] >= info_ptr->num_palette)
1993 {
1994 png_chunk_benign_error(png_ptr, "invalid index");
1995 return;
1996 }
1997
1998 background.red = (png_uint_16)png_ptr->palette[buf[0]].red;
1999 background.green = (png_uint_16)png_ptr->palette[buf[0]].green;
2000 background.blue = (png_uint_16)png_ptr->palette[buf[0]].blue;
2001 }
2002
2003 else
2004 background.red = background.green = background.blue = 0;
2005
2006 background.gray = 0;
2007 }
2008
2009 else if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0) /* GRAY */
2010 {
2011 if (png_ptr->bit_depth <= 8)
2012 {
2013 if (buf[0] != 0 || buf[1] >= (unsigned int)(1 << png_ptr->bit_depth))
2014 {
2015 png_chunk_benign_error(png_ptr, "invalid gray level");
2016 return;
2017 }
2018 }
2019
2020 background.index = 0;
2021 background.red =
2022 background.green =
2023 background.blue =
2024 background.gray = png_get_uint_16(buf);
2025 }
2026
2027 else
2028 {
2029 if (png_ptr->bit_depth <= 8)
2030 {
2031 if (buf[0] != 0 || buf[2] != 0 || buf[4] != 0)
2032 {
2033 png_chunk_benign_error(png_ptr, "invalid color");
2034 return;
2035 }
2036 }
2037
2038 background.index = 0;
2039 background.red = png_get_uint_16(buf);
2040 background.green = png_get_uint_16(buf + 2);
2041 background.blue = png_get_uint_16(buf + 4);
2042 background.gray = 0;
2043 }
2044
2045 png_set_bKGD(png_ptr, info_ptr, &background);
2046}
2047#endif
2048
2049#ifdef PNG_READ_cICP_SUPPORTED
2050void /* PRIVATE */
2051png_handle_cICP(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2052{
2053 png_byte buf[4];
2054
2055 png_debug(1, "in png_handle_cICP");
2056
2057 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2058 png_chunk_error(png_ptr, "missing IHDR");
2059
2060 else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
2061 {
2062 png_crc_finish(png_ptr, length);
2063 png_chunk_benign_error(png_ptr, "out of place");
2064 return;
2065 }
2066
2067 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cICP) != 0)
2068 {
2069 png_crc_finish(png_ptr, length);
2070 png_chunk_benign_error(png_ptr, "duplicate");
2071 return;
2072 }
2073
2074 else if (length != 4)
2075 {
2076 png_crc_finish(png_ptr, length);
2077 png_chunk_benign_error(png_ptr, "invalid");
2078 return;
2079 }
2080
2081 png_crc_read(png_ptr, buf, 4);
2082
2083 if (png_crc_finish(png_ptr, 0) != 0)
2084 return;
2085
2086 png_set_cICP(png_ptr, info_ptr, buf[0], buf[1], buf[2], buf[3]);
2087}
2088#endif
2089
2090#ifdef PNG_READ_eXIf_SUPPORTED
2091void /* PRIVATE */
2092png_handle_eXIf(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2093{
2094 unsigned int i;
2095
2096 png_debug(1, "in png_handle_eXIf");
2097
2098 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2099 png_chunk_error(png_ptr, "missing IHDR");
2100
2101 if (length < 2)
2102 {
2103 png_crc_finish(png_ptr, length);
2104 png_chunk_benign_error(png_ptr, "too short");
2105 return;
2106 }
2107
2108 else if (info_ptr == NULL || (info_ptr->valid & PNG_INFO_eXIf) != 0)
2109 {
2110 png_crc_finish(png_ptr, length);
2111 png_chunk_benign_error(png_ptr, "duplicate");
2112 return;
2113 }
2114
2115 info_ptr->free_me |= PNG_FREE_EXIF;
2116
2117 info_ptr->eXIf_buf = png_voidcast(png_bytep,
2118 png_malloc_warn(png_ptr, length));
2119
2120 if (info_ptr->eXIf_buf == NULL)
2121 {
2122 png_crc_finish(png_ptr, length);
2123 png_chunk_benign_error(png_ptr, "out of memory");
2124 return;
2125 }
2126
2127 for (i = 0; i < length; i++)
2128 {
2129 png_byte buf[1];
2130 png_crc_read(png_ptr, buf, 1);
2131 info_ptr->eXIf_buf[i] = buf[0];
2132 if (i == 1)
2133 {
2134 if ((buf[0] != 'M' && buf[0] != 'I') ||
2135 (info_ptr->eXIf_buf[0] != buf[0]))
2136 {
2137 png_crc_finish(png_ptr, length - 2);
2138 png_chunk_benign_error(png_ptr, "incorrect byte-order specifier");
2139 png_free(png_ptr, info_ptr->eXIf_buf);
2140 info_ptr->eXIf_buf = NULL;
2141 return;
2142 }
2143 }
2144 }
2145
2146 if (png_crc_finish(png_ptr, 0) == 0)
2147 png_set_eXIf_1(png_ptr, info_ptr, length, info_ptr->eXIf_buf);
2148
2149 png_free(png_ptr, info_ptr->eXIf_buf);
2150 info_ptr->eXIf_buf = NULL;
2151}
2152#endif
2153
2154#ifdef PNG_READ_hIST_SUPPORTED
2155void /* PRIVATE */
2156png_handle_hIST(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2157{
2158 unsigned int num, i;
2159 png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];
2160
2161 png_debug(1, "in png_handle_hIST");
2162
2163 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2164 png_chunk_error(png_ptr, "missing IHDR");
2165
2166 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0 ||
2167 (png_ptr->mode & PNG_HAVE_PLTE) == 0)
2168 {
2169 png_crc_finish(png_ptr, length);
2170 png_chunk_benign_error(png_ptr, "out of place");
2171 return;
2172 }
2173
2174 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) != 0)
2175 {
2176 png_crc_finish(png_ptr, length);
2177 png_chunk_benign_error(png_ptr, "duplicate");
2178 return;
2179 }
2180
2181 num = length / 2 ;
2182
2183 if (length != num * 2 ||
2184 num != (unsigned int)png_ptr->num_palette ||
2185 num > (unsigned int)PNG_MAX_PALETTE_LENGTH)
2186 {
2187 png_crc_finish(png_ptr, length);
2188 png_chunk_benign_error(png_ptr, "invalid");
2189 return;
2190 }
2191
2192 for (i = 0; i < num; i++)
2193 {
2194 png_byte buf[2];
2195
2196 png_crc_read(png_ptr, buf, 2);
2197 readbuf[i] = png_get_uint_16(buf);
2198 }
2199
2200 if (png_crc_finish(png_ptr, 0) != 0)
2201 return;
2202
2203 png_set_hIST(png_ptr, info_ptr, readbuf);
2204}
2205#endif
2206
2207#ifdef PNG_READ_pHYs_SUPPORTED
2208void /* PRIVATE */
2209png_handle_pHYs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2210{
2211 png_byte buf[9];
2212 png_uint_32 res_x, res_y;
2213 int unit_type;
2214
2215 png_debug(1, "in png_handle_pHYs");
2216
2217 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2218 png_chunk_error(png_ptr, "missing IHDR");
2219
2220 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2221 {
2222 png_crc_finish(png_ptr, length);
2223 png_chunk_benign_error(png_ptr, "out of place");
2224 return;
2225 }
2226
2227 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs) != 0)
2228 {
2229 png_crc_finish(png_ptr, length);
2230 png_chunk_benign_error(png_ptr, "duplicate");
2231 return;
2232 }
2233
2234 if (length != 9)
2235 {
2236 png_crc_finish(png_ptr, length);
2237 png_chunk_benign_error(png_ptr, "invalid");
2238 return;
2239 }
2240
2241 png_crc_read(png_ptr, buf, 9);
2242
2243 if (png_crc_finish(png_ptr, 0) != 0)
2244 return;
2245
2246 res_x = png_get_uint_32(buf);
2247 res_y = png_get_uint_32(buf + 4);
2248 unit_type = buf[8];
2249 png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
2250}
2251#endif
2252
2253#ifdef PNG_READ_oFFs_SUPPORTED
2254void /* PRIVATE */
2255png_handle_oFFs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2256{
2257 png_byte buf[9];
2258 png_int_32 offset_x, offset_y;
2259 int unit_type;
2260
2261 png_debug(1, "in png_handle_oFFs");
2262
2263 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2264 png_chunk_error(png_ptr, "missing IHDR");
2265
2266 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2267 {
2268 png_crc_finish(png_ptr, length);
2269 png_chunk_benign_error(png_ptr, "out of place");
2270 return;
2271 }
2272
2273 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs) != 0)
2274 {
2275 png_crc_finish(png_ptr, length);
2276 png_chunk_benign_error(png_ptr, "duplicate");
2277 return;
2278 }
2279
2280 if (length != 9)
2281 {
2282 png_crc_finish(png_ptr, length);
2283 png_chunk_benign_error(png_ptr, "invalid");
2284 return;
2285 }
2286
2287 png_crc_read(png_ptr, buf, 9);
2288
2289 if (png_crc_finish(png_ptr, 0) != 0)
2290 return;
2291
2292 offset_x = png_get_int_32(buf);
2293 offset_y = png_get_int_32(buf + 4);
2294 unit_type = buf[8];
2295 png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
2296}
2297#endif
2298
2299#ifdef PNG_READ_pCAL_SUPPORTED
2300/* Read the pCAL chunk (described in the PNG Extensions document) */
2301void /* PRIVATE */
2302png_handle_pCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2303{
2304 png_int_32 X0, X1;
2305 png_byte type, nparams;
2306 png_bytep buffer, buf, units, endptr;
2307 png_charpp params;
2308 int i;
2309
2310 png_debug(1, "in png_handle_pCAL");
2311
2312 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2313 png_chunk_error(png_ptr, "missing IHDR");
2314
2315 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2316 {
2317 png_crc_finish(png_ptr, length);
2318 png_chunk_benign_error(png_ptr, "out of place");
2319 return;
2320 }
2321
2322 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL) != 0)
2323 {
2324 png_crc_finish(png_ptr, length);
2325 png_chunk_benign_error(png_ptr, "duplicate");
2326 return;
2327 }
2328
2329 png_debug1(2, "Allocating and reading pCAL chunk data (%u bytes)",
2330 length + 1);
2331
2332 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
2333
2334 if (buffer == NULL)
2335 {
2336 png_crc_finish(png_ptr, length);
2337 png_chunk_benign_error(png_ptr, "out of memory");
2338 return;
2339 }
2340
2341 png_crc_read(png_ptr, buffer, length);
2342
2343 if (png_crc_finish(png_ptr, 0) != 0)
2344 return;
2345
2346 buffer[length] = 0; /* Null terminate the last string */
2347
2348 png_debug(3, "Finding end of pCAL purpose string");
2349 for (buf = buffer; *buf; buf++)
2350 /* Empty loop */ ;
2351
2352 endptr = buffer + length;
2353
2354 /* We need to have at least 12 bytes after the purpose string
2355 * in order to get the parameter information.
2356 */
2357 if (endptr - buf <= 12)
2358 {
2359 png_chunk_benign_error(png_ptr, "invalid");
2360 return;
2361 }
2362
2363 png_debug(3, "Reading pCAL X0, X1, type, nparams, and units");
2364 X0 = png_get_int_32((png_bytep)buf+1);
2365 X1 = png_get_int_32((png_bytep)buf+5);
2366 type = buf[9];
2367 nparams = buf[10];
2368 units = buf + 11;
2369
2370 png_debug(3, "Checking pCAL equation type and number of parameters");
2371 /* Check that we have the right number of parameters for known
2372 * equation types.
2373 */
2374 if ((type == PNG_EQUATION_LINEAR && nparams != 2) ||
2375 (type == PNG_EQUATION_BASE_E && nparams != 3) ||
2376 (type == PNG_EQUATION_ARBITRARY && nparams != 3) ||
2377 (type == PNG_EQUATION_HYPERBOLIC && nparams != 4))
2378 {
2379 png_chunk_benign_error(png_ptr, "invalid parameter count");
2380 return;
2381 }
2382
2383 else if (type >= PNG_EQUATION_LAST)
2384 {
2385 png_chunk_benign_error(png_ptr, "unrecognized equation type");
2386 }
2387
2388 for (buf = units; *buf; buf++)
2389 /* Empty loop to move past the units string. */ ;
2390
2391 png_debug(3, "Allocating pCAL parameters array");
2392
2393 params = png_voidcast(png_charpp, png_malloc_warn(png_ptr,
2394 nparams * (sizeof (png_charp))));
2395
2396 if (params == NULL)
2397 {
2398 png_chunk_benign_error(png_ptr, "out of memory");
2399 return;
2400 }
2401
2402 /* Get pointers to the start of each parameter string. */
2403 for (i = 0; i < nparams; i++)
2404 {
2405 buf++; /* Skip the null string terminator from previous parameter. */
2406
2407 png_debug1(3, "Reading pCAL parameter %d", i);
2408
2409 for (params[i] = (png_charp)buf; buf <= endptr && *buf != 0; buf++)
2410 /* Empty loop to move past each parameter string */ ;
2411
2412 /* Make sure we haven't run out of data yet */
2413 if (buf > endptr)
2414 {
2415 png_free(png_ptr, params);
2416 png_chunk_benign_error(png_ptr, "invalid data");
2417 return;
2418 }
2419 }
2420
2421 png_set_pCAL(png_ptr, info_ptr, (png_charp)buffer, X0, X1, type, nparams,
2422 (png_charp)units, params);
2423
2424 png_free(png_ptr, params);
2425}
2426#endif
2427
2428#ifdef PNG_READ_sCAL_SUPPORTED
2429/* Read the sCAL chunk */
2430void /* PRIVATE */
2431png_handle_sCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2432{
2433 png_bytep buffer;
2434 size_t i;
2435 int state;
2436
2437 png_debug(1, "in png_handle_sCAL");
2438
2439 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2440 png_chunk_error(png_ptr, "missing IHDR");
2441
2442 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2443 {
2444 png_crc_finish(png_ptr, length);
2445 png_chunk_benign_error(png_ptr, "out of place");
2446 return;
2447 }
2448
2449 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL) != 0)
2450 {
2451 png_crc_finish(png_ptr, length);
2452 png_chunk_benign_error(png_ptr, "duplicate");
2453 return;
2454 }
2455
2456 /* Need unit type, width, \0, height: minimum 4 bytes */
2457 else if (length < 4)
2458 {
2459 png_crc_finish(png_ptr, length);
2460 png_chunk_benign_error(png_ptr, "invalid");
2461 return;
2462 }
2463
2464 png_debug1(2, "Allocating and reading sCAL chunk data (%u bytes)",
2465 length + 1);
2466
2467 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
2468
2469 if (buffer == NULL)
2470 {
2471 png_chunk_benign_error(png_ptr, "out of memory");
2472 png_crc_finish(png_ptr, length);
2473 return;
2474 }
2475
2476 png_crc_read(png_ptr, buffer, length);
2477 buffer[length] = 0; /* Null terminate the last string */
2478
2479 if (png_crc_finish(png_ptr, 0) != 0)
2480 return;
2481
2482 /* Validate the unit. */
2483 if (buffer[0] != 1 && buffer[0] != 2)
2484 {
2485 png_chunk_benign_error(png_ptr, "invalid unit");
2486 return;
2487 }
2488
2489 /* Validate the ASCII numbers, need two ASCII numbers separated by
2490 * a '\0' and they need to fit exactly in the chunk data.
2491 */
2492 i = 1;
2493 state = 0;
2494
2495 if (png_check_fp_number((png_const_charp)buffer, length, &state, &i) == 0 ||
2496 i >= length || buffer[i++] != 0)
2497 png_chunk_benign_error(png_ptr, "bad width format");
2498
2499 else if (PNG_FP_IS_POSITIVE(state) == 0)
2500 png_chunk_benign_error(png_ptr, "non-positive width");
2501
2502 else
2503 {
2504 size_t heighti = i;
2505
2506 state = 0;
2507 if (png_check_fp_number((png_const_charp)buffer, length,
2508 &state, &i) == 0 || i != length)
2509 png_chunk_benign_error(png_ptr, "bad height format");
2510
2511 else if (PNG_FP_IS_POSITIVE(state) == 0)
2512 png_chunk_benign_error(png_ptr, "non-positive height");
2513
2514 else
2515 /* This is the (only) success case. */
2516 png_set_sCAL_s(png_ptr, info_ptr, buffer[0],
2517 (png_charp)buffer+1, (png_charp)buffer+heighti);
2518 }
2519}
2520#endif
2521
2522#ifdef PNG_READ_tIME_SUPPORTED
2523void /* PRIVATE */
2524png_handle_tIME(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2525{
2526 png_byte buf[7];
2527 png_time mod_time;
2528
2529 png_debug(1, "in png_handle_tIME");
2530
2531 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2532 png_chunk_error(png_ptr, "missing IHDR");
2533
2534 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME) != 0)
2535 {
2536 png_crc_finish(png_ptr, length);
2537 png_chunk_benign_error(png_ptr, "duplicate");
2538 return;
2539 }
2540
2541 if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2542 png_ptr->mode |= PNG_AFTER_IDAT;
2543
2544 if (length != 7)
2545 {
2546 png_crc_finish(png_ptr, length);
2547 png_chunk_benign_error(png_ptr, "invalid");
2548 return;
2549 }
2550
2551 png_crc_read(png_ptr, buf, 7);
2552
2553 if (png_crc_finish(png_ptr, 0) != 0)
2554 return;
2555
2556 mod_time.second = buf[6];
2557 mod_time.minute = buf[5];
2558 mod_time.hour = buf[4];
2559 mod_time.day = buf[3];
2560 mod_time.month = buf[2];
2561 mod_time.year = png_get_uint_16(buf);
2562
2563 png_set_tIME(png_ptr, info_ptr, &mod_time);
2564}
2565#endif
2566
2567#ifdef PNG_READ_tEXt_SUPPORTED
2568/* Note: this does not properly handle chunks that are > 64K under DOS */
2569void /* PRIVATE */
2570png_handle_tEXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2571{
2572 png_text text_info;
2573 png_bytep buffer;
2574 png_charp key;
2575 png_charp text;
2576 png_uint_32 skip = 0;
2577
2578 png_debug(1, "in png_handle_tEXt");
2579
2580#ifdef PNG_USER_LIMITS_SUPPORTED
2581 if (png_ptr->user_chunk_cache_max != 0)
2582 {
2583 if (png_ptr->user_chunk_cache_max == 1)
2584 {
2585 png_crc_finish(png_ptr, length);
2586 return;
2587 }
2588
2589 if (--png_ptr->user_chunk_cache_max == 1)
2590 {
2591 png_crc_finish(png_ptr, length);
2592 png_chunk_benign_error(png_ptr, "no space in chunk cache");
2593 return;
2594 }
2595 }
2596#endif
2597
2598 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2599 png_chunk_error(png_ptr, "missing IHDR");
2600
2601 if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2602 png_ptr->mode |= PNG_AFTER_IDAT;
2603
2604#ifdef PNG_MAX_MALLOC_64K
2605 if (length > 65535U)
2606 {
2607 png_crc_finish(png_ptr, length);
2608 png_chunk_benign_error(png_ptr, "too large to fit in memory");
2609 return;
2610 }
2611#endif
2612
2613 buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
2614
2615 if (buffer == NULL)
2616 {
2617 png_chunk_benign_error(png_ptr, "out of memory");
2618 return;
2619 }
2620
2621 png_crc_read(png_ptr, buffer, length);
2622
2623 if (png_crc_finish(png_ptr, skip) != 0)
2624 return;
2625
2626 key = (png_charp)buffer;
2627 key[length] = 0;
2628
2629 for (text = key; *text; text++)
2630 /* Empty loop to find end of key */ ;
2631
2632 if (text != key + length)
2633 text++;
2634
2635 text_info.compression = PNG_TEXT_COMPRESSION_NONE;
2636 text_info.key = key;
2637 text_info.lang = NULL;
2638 text_info.lang_key = NULL;
2639 text_info.itxt_length = 0;
2640 text_info.text = text;
2641 text_info.text_length = strlen(text);
2642
2643 if (png_set_text_2(png_ptr, info_ptr, &text_info, 1) != 0)
2644 png_warning(png_ptr, "Insufficient memory to process text chunk");
2645}
2646#endif
2647
2648#ifdef PNG_READ_zTXt_SUPPORTED
2649/* Note: this does not correctly handle chunks that are > 64K under DOS */
2650void /* PRIVATE */
2651png_handle_zTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2652{
2653 png_const_charp errmsg = NULL;
2654 png_bytep buffer;
2655 png_uint_32 keyword_length;
2656
2657 png_debug(1, "in png_handle_zTXt");
2658
2659#ifdef PNG_USER_LIMITS_SUPPORTED
2660 if (png_ptr->user_chunk_cache_max != 0)
2661 {
2662 if (png_ptr->user_chunk_cache_max == 1)
2663 {
2664 png_crc_finish(png_ptr, length);
2665 return;
2666 }
2667
2668 if (--png_ptr->user_chunk_cache_max == 1)
2669 {
2670 png_crc_finish(png_ptr, length);
2671 png_chunk_benign_error(png_ptr, "no space in chunk cache");
2672 return;
2673 }
2674 }
2675#endif
2676
2677 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2678 png_chunk_error(png_ptr, "missing IHDR");
2679
2680 if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2681 png_ptr->mode |= PNG_AFTER_IDAT;
2682
2683 /* Note, "length" is sufficient here; we won't be adding
2684 * a null terminator later.
2685 */
2686 buffer = png_read_buffer(png_ptr, length, 2/*silent*/);
2687
2688 if (buffer == NULL)
2689 {
2690 png_crc_finish(png_ptr, length);
2691 png_chunk_benign_error(png_ptr, "out of memory");
2692 return;
2693 }
2694
2695 png_crc_read(png_ptr, buffer, length);
2696
2697 if (png_crc_finish(png_ptr, 0) != 0)
2698 return;
2699
2700 /* TODO: also check that the keyword contents match the spec! */
2701 for (keyword_length = 0;
2702 keyword_length < length && buffer[keyword_length] != 0;
2703 ++keyword_length)
2704 /* Empty loop to find end of name */ ;
2705
2706 if (keyword_length > 79 || keyword_length < 1)
2707 errmsg = "bad keyword";
2708
2709 /* zTXt must have some LZ data after the keyword, although it may expand to
2710 * zero bytes; we need a '\0' at the end of the keyword, the compression type
2711 * then the LZ data:
2712 */
2713 else if (keyword_length + 3 > length)
2714 errmsg = "truncated";
2715
2716 else if (buffer[keyword_length+1] != PNG_COMPRESSION_TYPE_BASE)
2717 errmsg = "unknown compression type";
2718
2719 else
2720 {
2721 png_alloc_size_t uncompressed_length = PNG_SIZE_MAX;
2722
2723 /* TODO: at present png_decompress_chunk imposes a single application
2724 * level memory limit, this should be split to different values for iCCP
2725 * and text chunks.
2726 */
2727 if (png_decompress_chunk(png_ptr, length, keyword_length+2,
2728 &uncompressed_length, 1/*terminate*/) == Z_STREAM_END)
2729 {
2730 png_text text;
2731
2732 if (png_ptr->read_buffer == NULL)
2733 errmsg="Read failure in png_handle_zTXt";
2734 else
2735 {
2736 /* It worked; png_ptr->read_buffer now looks like a tEXt chunk
2737 * except for the extra compression type byte and the fact that
2738 * it isn't necessarily '\0' terminated.
2739 */
2740 buffer = png_ptr->read_buffer;
2741 buffer[uncompressed_length+(keyword_length+2)] = 0;
2742
2743 text.compression = PNG_TEXT_COMPRESSION_zTXt;
2744 text.key = (png_charp)buffer;
2745 text.text = (png_charp)(buffer + keyword_length+2);
2746 text.text_length = uncompressed_length;
2747 text.itxt_length = 0;
2748 text.lang = NULL;
2749 text.lang_key = NULL;
2750
2751 if (png_set_text_2(png_ptr, info_ptr, &text, 1) != 0)
2752 errmsg = "insufficient memory";
2753 }
2754 }
2755
2756 else
2757 errmsg = png_ptr->zstream.msg;
2758 }
2759
2760 if (errmsg != NULL)
2761 png_chunk_benign_error(png_ptr, errmsg);
2762}
2763#endif
2764
2765#ifdef PNG_READ_iTXt_SUPPORTED
2766/* Note: this does not correctly handle chunks that are > 64K under DOS */
2767void /* PRIVATE */
2768png_handle_iTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2769{
2770 png_const_charp errmsg = NULL;
2771 png_bytep buffer;
2772 png_uint_32 prefix_length;
2773
2774 png_debug(1, "in png_handle_iTXt");
2775
2776#ifdef PNG_USER_LIMITS_SUPPORTED
2777 if (png_ptr->user_chunk_cache_max != 0)
2778 {
2779 if (png_ptr->user_chunk_cache_max == 1)
2780 {
2781 png_crc_finish(png_ptr, length);
2782 return;
2783 }
2784
2785 if (--png_ptr->user_chunk_cache_max == 1)
2786 {
2787 png_crc_finish(png_ptr, length);
2788 png_chunk_benign_error(png_ptr, "no space in chunk cache");
2789 return;
2790 }
2791 }
2792#endif
2793
2794 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2795 png_chunk_error(png_ptr, "missing IHDR");
2796
2797 if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2798 png_ptr->mode |= PNG_AFTER_IDAT;
2799
2800 buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
2801
2802 if (buffer == NULL)
2803 {
2804 png_crc_finish(png_ptr, length);
2805 png_chunk_benign_error(png_ptr, "out of memory");
2806 return;
2807 }
2808
2809 png_crc_read(png_ptr, buffer, length);
2810
2811 if (png_crc_finish(png_ptr, 0) != 0)
2812 return;
2813
2814 /* First the keyword. */
2815 for (prefix_length=0;
2816 prefix_length < length && buffer[prefix_length] != 0;
2817 ++prefix_length)
2818 /* Empty loop */ ;
2819
2820 /* Perform a basic check on the keyword length here. */
2821 if (prefix_length > 79 || prefix_length < 1)
2822 errmsg = "bad keyword";
2823
2824 /* Expect keyword, compression flag, compression type, language, translated
2825 * keyword (both may be empty but are 0 terminated) then the text, which may
2826 * be empty.
2827 */
2828 else if (prefix_length + 5 > length)
2829 errmsg = "truncated";
2830
2831 else if (buffer[prefix_length+1] == 0 ||
2832 (buffer[prefix_length+1] == 1 &&
2833 buffer[prefix_length+2] == PNG_COMPRESSION_TYPE_BASE))
2834 {
2835 int compressed = buffer[prefix_length+1] != 0;
2836 png_uint_32 language_offset, translated_keyword_offset;
2837 png_alloc_size_t uncompressed_length = 0;
2838
2839 /* Now the language tag */
2840 prefix_length += 3;
2841 language_offset = prefix_length;
2842
2843 for (; prefix_length < length && buffer[prefix_length] != 0;
2844 ++prefix_length)
2845 /* Empty loop */ ;
2846
2847 /* WARNING: the length may be invalid here, this is checked below. */
2848 translated_keyword_offset = ++prefix_length;
2849
2850 for (; prefix_length < length && buffer[prefix_length] != 0;
2851 ++prefix_length)
2852 /* Empty loop */ ;
2853
2854 /* prefix_length should now be at the trailing '\0' of the translated
2855 * keyword, but it may already be over the end. None of this arithmetic
2856 * can overflow because chunks are at most 2^31 bytes long, but on 16-bit
2857 * systems the available allocation may overflow.
2858 */
2859 ++prefix_length;
2860
2861 if (compressed == 0 && prefix_length <= length)
2862 uncompressed_length = length - prefix_length;
2863
2864 else if (compressed != 0 && prefix_length < length)
2865 {
2866 uncompressed_length = PNG_SIZE_MAX;
2867
2868 /* TODO: at present png_decompress_chunk imposes a single application
2869 * level memory limit, this should be split to different values for
2870 * iCCP and text chunks.
2871 */
2872 if (png_decompress_chunk(png_ptr, length, prefix_length,
2873 &uncompressed_length, 1/*terminate*/) == Z_STREAM_END)
2874 buffer = png_ptr->read_buffer;
2875
2876 else
2877 errmsg = png_ptr->zstream.msg;
2878 }
2879
2880 else
2881 errmsg = "truncated";
2882
2883 if (errmsg == NULL)
2884 {
2885 png_text text;
2886
2887 buffer[uncompressed_length+prefix_length] = 0;
2888
2889 if (compressed == 0)
2890 text.compression = PNG_ITXT_COMPRESSION_NONE;
2891
2892 else
2893 text.compression = PNG_ITXT_COMPRESSION_zTXt;
2894
2895 text.key = (png_charp)buffer;
2896 text.lang = (png_charp)buffer + language_offset;
2897 text.lang_key = (png_charp)buffer + translated_keyword_offset;
2898 text.text = (png_charp)buffer + prefix_length;
2899 text.text_length = 0;
2900 text.itxt_length = uncompressed_length;
2901
2902 if (png_set_text_2(png_ptr, info_ptr, &text, 1) != 0)
2903 errmsg = "insufficient memory";
2904 }
2905 }
2906
2907 else
2908 errmsg = "bad compression info";
2909
2910 if (errmsg != NULL)
2911 png_chunk_benign_error(png_ptr, errmsg);
2912}
2913#endif
2914
2915#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
2916/* Utility function for png_handle_unknown; set up png_ptr::unknown_chunk */
2917static int
2918png_cache_unknown_chunk(png_structrp png_ptr, png_uint_32 length)
2919{
2920 png_alloc_size_t limit = PNG_SIZE_MAX;
2921
2922 if (png_ptr->unknown_chunk.data != NULL)
2923 {
2924 png_free(png_ptr, png_ptr->unknown_chunk.data);
2925 png_ptr->unknown_chunk.data = NULL;
2926 }
2927
2928# ifdef PNG_SET_USER_LIMITS_SUPPORTED
2929 if (png_ptr->user_chunk_malloc_max > 0 &&
2930 png_ptr->user_chunk_malloc_max < limit)
2931 limit = png_ptr->user_chunk_malloc_max;
2932
2933# elif PNG_USER_CHUNK_MALLOC_MAX > 0
2934 if (PNG_USER_CHUNK_MALLOC_MAX < limit)
2935 limit = PNG_USER_CHUNK_MALLOC_MAX;
2936# endif
2937
2938 if (length <= limit)
2939 {
2940 PNG_CSTRING_FROM_CHUNK(png_ptr->unknown_chunk.name, png_ptr->chunk_name);
2941 /* The following is safe because of the PNG_SIZE_MAX init above */
2942 png_ptr->unknown_chunk.size = (size_t)length/*SAFE*/;
2943 /* 'mode' is a flag array, only the bottom four bits matter here */
2944 png_ptr->unknown_chunk.location = (png_byte)png_ptr->mode/*SAFE*/;
2945
2946 if (length == 0)
2947 png_ptr->unknown_chunk.data = NULL;
2948
2949 else
2950 {
2951 /* Do a 'warn' here - it is handled below. */
2952 png_ptr->unknown_chunk.data = png_voidcast(png_bytep,
2953 png_malloc_warn(png_ptr, length));
2954 }
2955 }
2956
2957 if (png_ptr->unknown_chunk.data == NULL && length > 0)
2958 {
2959 /* This is benign because we clean up correctly */
2960 png_crc_finish(png_ptr, length);
2961 png_chunk_benign_error(png_ptr, "unknown chunk exceeds memory limits");
2962 return 0;
2963 }
2964
2965 else
2966 {
2967 if (length > 0)
2968 png_crc_read(png_ptr, png_ptr->unknown_chunk.data, length);
2969 png_crc_finish(png_ptr, 0);
2970 return 1;
2971 }
2972}
2973#endif /* READ_UNKNOWN_CHUNKS */
2974
2975/* Handle an unknown, or known but disabled, chunk */
2976void /* PRIVATE */
2977png_handle_unknown(png_structrp png_ptr, png_inforp info_ptr,
2978 png_uint_32 length, int keep)
2979{
2980 int handled = 0; /* the chunk was handled */
2981
2982 png_debug(1, "in png_handle_unknown");
2983
2984#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
2985 /* NOTE: this code is based on the code in libpng-1.4.12 except for fixing
2986 * the bug which meant that setting a non-default behavior for a specific
2987 * chunk would be ignored (the default was always used unless a user
2988 * callback was installed).
2989 *
2990 * 'keep' is the value from the png_chunk_unknown_handling, the setting for
2991 * this specific chunk_name, if PNG_HANDLE_AS_UNKNOWN_SUPPORTED, if not it
2992 * will always be PNG_HANDLE_CHUNK_AS_DEFAULT and it needs to be set here.
2993 * This is just an optimization to avoid multiple calls to the lookup
2994 * function.
2995 */
2996# ifndef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
2997# ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
2998 keep = png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name);
2999# endif
3000# endif
3001
3002 /* One of the following methods will read the chunk or skip it (at least one
3003 * of these is always defined because this is the only way to switch on
3004 * PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
3005 */
3006# ifdef PNG_READ_USER_CHUNKS_SUPPORTED
3007 /* The user callback takes precedence over the chunk keep value, but the
3008 * keep value is still required to validate a save of a critical chunk.
3009 */
3010 if (png_ptr->read_user_chunk_fn != NULL)
3011 {
3012 if (png_cache_unknown_chunk(png_ptr, length) != 0)
3013 {
3014 /* Callback to user unknown chunk handler */
3015 int ret = (*(png_ptr->read_user_chunk_fn))(png_ptr,
3016 &png_ptr->unknown_chunk);
3017
3018 /* ret is:
3019 * negative: An error occurred; png_chunk_error will be called.
3020 * zero: The chunk was not handled, the chunk will be discarded
3021 * unless png_set_keep_unknown_chunks has been used to set
3022 * a 'keep' behavior for this particular chunk, in which
3023 * case that will be used. A critical chunk will cause an
3024 * error at this point unless it is to be saved.
3025 * positive: The chunk was handled, libpng will ignore/discard it.
3026 */
3027 if (ret < 0)
3028 png_chunk_error(png_ptr, "error in user chunk");
3029
3030 else if (ret == 0)
3031 {
3032 /* If the keep value is 'default' or 'never' override it, but
3033 * still error out on critical chunks unless the keep value is
3034 * 'always' While this is weird it is the behavior in 1.4.12.
3035 * A possible improvement would be to obey the value set for the
3036 * chunk, but this would be an API change that would probably
3037 * damage some applications.
3038 *
3039 * The png_app_warning below catches the case that matters, where
3040 * the application has not set specific save or ignore for this
3041 * chunk or global save or ignore.
3042 */
3043 if (keep < PNG_HANDLE_CHUNK_IF_SAFE)
3044 {
3045# ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
3046 if (png_ptr->unknown_default < PNG_HANDLE_CHUNK_IF_SAFE)
3047 {
3048 png_chunk_warning(png_ptr, "Saving unknown chunk:");
3049 png_app_warning(png_ptr,
3050 "forcing save of an unhandled chunk;"
3051 " please call png_set_keep_unknown_chunks");
3052 /* with keep = PNG_HANDLE_CHUNK_IF_SAFE */
3053 }
3054# endif
3055 keep = PNG_HANDLE_CHUNK_IF_SAFE;
3056 }
3057 }
3058
3059 else /* chunk was handled */
3060 {
3061 handled = 1;
3062 /* Critical chunks can be safely discarded at this point. */
3063 keep = PNG_HANDLE_CHUNK_NEVER;
3064 }
3065 }
3066
3067 else
3068 keep = PNG_HANDLE_CHUNK_NEVER; /* insufficient memory */
3069 }
3070
3071 else
3072 /* Use the SAVE_UNKNOWN_CHUNKS code or skip the chunk */
3073# endif /* READ_USER_CHUNKS */
3074
3075# ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
3076 {
3077 /* keep is currently just the per-chunk setting, if there was no
3078 * setting change it to the global default now (not that this may
3079 * still be AS_DEFAULT) then obtain the cache of the chunk if required,
3080 * if not simply skip the chunk.
3081 */
3082 if (keep == PNG_HANDLE_CHUNK_AS_DEFAULT)
3083 keep = png_ptr->unknown_default;
3084
3085 if (keep == PNG_HANDLE_CHUNK_ALWAYS ||
3086 (keep == PNG_HANDLE_CHUNK_IF_SAFE &&
3087 PNG_CHUNK_ANCILLARY(png_ptr->chunk_name)))
3088 {
3089 if (png_cache_unknown_chunk(png_ptr, length) == 0)
3090 keep = PNG_HANDLE_CHUNK_NEVER;
3091 }
3092
3093 else
3094 png_crc_finish(png_ptr, length);
3095 }
3096# else
3097# ifndef PNG_READ_USER_CHUNKS_SUPPORTED
3098# error no method to support READ_UNKNOWN_CHUNKS
3099# endif
3100
3101 {
3102 /* If here there is no read callback pointer set and no support is
3103 * compiled in to just save the unknown chunks, so simply skip this
3104 * chunk. If 'keep' is something other than AS_DEFAULT or NEVER then
3105 * the app has erroneously asked for unknown chunk saving when there
3106 * is no support.
3107 */
3108 if (keep > PNG_HANDLE_CHUNK_NEVER)
3109 png_app_error(png_ptr, "no unknown chunk support available");
3110
3111 png_crc_finish(png_ptr, length);
3112 }
3113# endif
3114
3115# ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
3116 /* Now store the chunk in the chunk list if appropriate, and if the limits
3117 * permit it.
3118 */
3119 if (keep == PNG_HANDLE_CHUNK_ALWAYS ||
3120 (keep == PNG_HANDLE_CHUNK_IF_SAFE &&
3121 PNG_CHUNK_ANCILLARY(png_ptr->chunk_name)))
3122 {
3123# ifdef PNG_USER_LIMITS_SUPPORTED
3124 switch (png_ptr->user_chunk_cache_max)
3125 {
3126 case 2:
3127 png_ptr->user_chunk_cache_max = 1;
3128 png_chunk_benign_error(png_ptr, "no space in chunk cache");
3129 /* FALLTHROUGH */
3130 case 1:
3131 /* NOTE: prior to 1.6.0 this case resulted in an unknown critical
3132 * chunk being skipped, now there will be a hard error below.
3133 */
3134 break;
3135
3136 default: /* not at limit */
3137 --(png_ptr->user_chunk_cache_max);
3138 /* FALLTHROUGH */
3139 case 0: /* no limit */
3140# endif /* USER_LIMITS */
3141 /* Here when the limit isn't reached or when limits are compiled
3142 * out; store the chunk.
3143 */
3144 png_set_unknown_chunks(png_ptr, info_ptr,
3145 &png_ptr->unknown_chunk, 1);
3146 handled = 1;
3147# ifdef PNG_USER_LIMITS_SUPPORTED
3148 break;
3149 }
3150# endif
3151 }
3152# else /* no store support: the chunk must be handled by the user callback */
3153 PNG_UNUSED(info_ptr)
3154# endif
3155
3156 /* Regardless of the error handling below the cached data (if any) can be
3157 * freed now. Notice that the data is not freed if there is a png_error, but
3158 * it will be freed by destroy_read_struct.
3159 */
3160 if (png_ptr->unknown_chunk.data != NULL)
3161 png_free(png_ptr, png_ptr->unknown_chunk.data);
3162 png_ptr->unknown_chunk.data = NULL;
3163
3164#else /* !PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */
3165 /* There is no support to read an unknown chunk, so just skip it. */
3166 png_crc_finish(png_ptr, length);
3167 PNG_UNUSED(info_ptr)
3168 PNG_UNUSED(keep)
3169#endif /* !READ_UNKNOWN_CHUNKS */
3170
3171 /* Check for unhandled critical chunks */
3172 if (handled == 0 && PNG_CHUNK_CRITICAL(png_ptr->chunk_name))
3173 png_chunk_error(png_ptr, "unhandled critical chunk");
3174}
3175
3176/* This function is called to verify that a chunk name is valid.
3177 * This function can't have the "critical chunk check" incorporated
3178 * into it, since in the future we will need to be able to call user
3179 * functions to handle unknown critical chunks after we check that
3180 * the chunk name itself is valid.
3181 */
3182
3183/* Bit hacking: the test for an invalid byte in the 4 byte chunk name is:
3184 *
3185 * ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
3186 */
3187
3188void /* PRIVATE */
3189png_check_chunk_name(png_const_structrp png_ptr, png_uint_32 chunk_name)
3190{
3191 int i;
3192 png_uint_32 cn=chunk_name;
3193
3194 png_debug(1, "in png_check_chunk_name");
3195
3196 for (i=1; i<=4; ++i)
3197 {
3198 int c = cn & 0xff;
3199
3200 if (c < 65 || c > 122 || (c > 90 && c < 97))
3201 png_chunk_error(png_ptr, "invalid chunk type");
3202
3203 cn >>= 8;
3204 }
3205}
3206
3207void /* PRIVATE */
3208png_check_chunk_length(png_const_structrp png_ptr, png_uint_32 length)
3209{
3210 png_alloc_size_t limit = PNG_UINT_31_MAX;
3211
3212# ifdef PNG_SET_USER_LIMITS_SUPPORTED
3213 if (png_ptr->user_chunk_malloc_max > 0 &&
3214 png_ptr->user_chunk_malloc_max < limit)
3215 limit = png_ptr->user_chunk_malloc_max;
3216# elif PNG_USER_CHUNK_MALLOC_MAX > 0
3217 if (PNG_USER_CHUNK_MALLOC_MAX < limit)
3218 limit = PNG_USER_CHUNK_MALLOC_MAX;
3219# endif
3220 if (png_ptr->chunk_name == png_IDAT)
3221 {
3222 png_alloc_size_t idat_limit = PNG_UINT_31_MAX;
3223 size_t row_factor =
3224 (size_t)png_ptr->width
3225 * (size_t)png_ptr->channels
3226 * (png_ptr->bit_depth > 8? 2: 1)
3227 + 1
3228 + (png_ptr->interlaced? 6: 0);
3229 if (png_ptr->height > PNG_UINT_32_MAX/row_factor)
3230 idat_limit = PNG_UINT_31_MAX;
3231 else
3232 idat_limit = png_ptr->height * row_factor;
3233 row_factor = row_factor > 32566? 32566 : row_factor;
3234 idat_limit += 6 + 5*(idat_limit/row_factor+1); /* zlib+deflate overhead */
3235 idat_limit=idat_limit < PNG_UINT_31_MAX? idat_limit : PNG_UINT_31_MAX;
3236 limit = limit < idat_limit? idat_limit : limit;
3237 }
3238
3239 if (length > limit)
3240 {
3241 png_debug2(0," length = %lu, limit = %lu",
3242 (unsigned long)length,(unsigned long)limit);
3243 png_benign_error(png_ptr, "chunk data is too large");
3244 }
3245}
3246
3247/* Combines the row recently read in with the existing pixels in the row. This
3248 * routine takes care of alpha and transparency if requested. This routine also
3249 * handles the two methods of progressive display of interlaced images,
3250 * depending on the 'display' value; if 'display' is true then the whole row
3251 * (dp) is filled from the start by replicating the available pixels. If
3252 * 'display' is false only those pixels present in the pass are filled in.
3253 */
3254void /* PRIVATE */
3255png_combine_row(png_const_structrp png_ptr, png_bytep dp, int display)
3256{
3257 unsigned int pixel_depth = png_ptr->transformed_pixel_depth;
3258 png_const_bytep sp = png_ptr->row_buf + 1;
3259 png_alloc_size_t row_width = png_ptr->width;
3260 unsigned int pass = png_ptr->pass;
3261 png_bytep end_ptr = 0;
3262 png_byte end_byte = 0;
3263 unsigned int end_mask;
3264
3265 png_debug(1, "in png_combine_row");
3266
3267 /* Added in 1.5.6: it should not be possible to enter this routine until at
3268 * least one row has been read from the PNG data and transformed.
3269 */
3270 if (pixel_depth == 0)
3271 png_error(png_ptr, "internal row logic error");
3272
3273 /* Added in 1.5.4: the pixel depth should match the information returned by
3274 * any call to png_read_update_info at this point. Do not continue if we got
3275 * this wrong.
3276 */
3277 if (png_ptr->info_rowbytes != 0 && png_ptr->info_rowbytes !=
3278 PNG_ROWBYTES(pixel_depth, row_width))
3279 png_error(png_ptr, "internal row size calculation error");
3280
3281 /* Don't expect this to ever happen: */
3282 if (row_width == 0)
3283 png_error(png_ptr, "internal row width error");
3284
3285 /* Preserve the last byte in cases where only part of it will be overwritten,
3286 * the multiply below may overflow, we don't care because ANSI-C guarantees
3287 * we get the low bits.
3288 */
3289 end_mask = (pixel_depth * row_width) & 7;
3290 if (end_mask != 0)
3291 {
3292 /* end_ptr == NULL is a flag to say do nothing */
3293 end_ptr = dp + PNG_ROWBYTES(pixel_depth, row_width) - 1;
3294 end_byte = *end_ptr;
3295# ifdef PNG_READ_PACKSWAP_SUPPORTED
3296 if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
3297 /* little-endian byte */
3298 end_mask = (unsigned int)(0xff << end_mask);
3299
3300 else /* big-endian byte */
3301# endif
3302 end_mask = 0xff >> end_mask;
3303 /* end_mask is now the bits to *keep* from the destination row */
3304 }
3305
3306 /* For non-interlaced images this reduces to a memcpy(). A memcpy()
3307 * will also happen if interlacing isn't supported or if the application
3308 * does not call png_set_interlace_handling(). In the latter cases the
3309 * caller just gets a sequence of the unexpanded rows from each interlace
3310 * pass.
3311 */
3312#ifdef PNG_READ_INTERLACING_SUPPORTED
3313 if (png_ptr->interlaced != 0 &&
3314 (png_ptr->transformations & PNG_INTERLACE) != 0 &&
3315 pass < 6 && (display == 0 ||
3316 /* The following copies everything for 'display' on passes 0, 2 and 4. */
3317 (display == 1 && (pass & 1) != 0)))
3318 {
3319 /* Narrow images may have no bits in a pass; the caller should handle
3320 * this, but this test is cheap:
3321 */
3322 if (row_width <= PNG_PASS_START_COL(pass))
3323 return;
3324
3325 if (pixel_depth < 8)
3326 {
3327 /* For pixel depths up to 4 bpp the 8-pixel mask can be expanded to fit
3328 * into 32 bits, then a single loop over the bytes using the four byte
3329 * values in the 32-bit mask can be used. For the 'display' option the
3330 * expanded mask may also not require any masking within a byte. To
3331 * make this work the PACKSWAP option must be taken into account - it
3332 * simply requires the pixels to be reversed in each byte.
3333 *
3334 * The 'regular' case requires a mask for each of the first 6 passes,
3335 * the 'display' case does a copy for the even passes in the range
3336 * 0..6. This has already been handled in the test above.
3337 *
3338 * The masks are arranged as four bytes with the first byte to use in
3339 * the lowest bits (little-endian) regardless of the order (PACKSWAP or
3340 * not) of the pixels in each byte.
3341 *
3342 * NOTE: the whole of this logic depends on the caller of this function
3343 * only calling it on rows appropriate to the pass. This function only
3344 * understands the 'x' logic; the 'y' logic is handled by the caller.
3345 *
3346 * The following defines allow generation of compile time constant bit
3347 * masks for each pixel depth and each possibility of swapped or not
3348 * swapped bytes. Pass 'p' is in the range 0..6; 'x', a pixel index,
3349 * is in the range 0..7; and the result is 1 if the pixel is to be
3350 * copied in the pass, 0 if not. 'S' is for the sparkle method, 'B'
3351 * for the block method.
3352 *
3353 * With some compilers a compile time expression of the general form:
3354 *
3355 * (shift >= 32) ? (a >> (shift-32)) : (b >> shift)
3356 *
3357 * Produces warnings with values of 'shift' in the range 33 to 63
3358 * because the right hand side of the ?: expression is evaluated by
3359 * the compiler even though it isn't used. Microsoft Visual C (various
3360 * versions) and the Intel C compiler are known to do this. To avoid
3361 * this the following macros are used in 1.5.6. This is a temporary
3362 * solution to avoid destabilizing the code during the release process.
3363 */
3364# if PNG_USE_COMPILE_TIME_MASKS
3365# define PNG_LSR(x,s) ((x)>>((s) & 0x1f))
3366# define PNG_LSL(x,s) ((x)<<((s) & 0x1f))
3367# else
3368# define PNG_LSR(x,s) ((x)>>(s))
3369# define PNG_LSL(x,s) ((x)<<(s))
3370# endif
3371# define S_COPY(p,x) (((p)<4 ? PNG_LSR(0x80088822,(3-(p))*8+(7-(x))) :\
3372 PNG_LSR(0xaa55ff00,(7-(p))*8+(7-(x)))) & 1)
3373# define B_COPY(p,x) (((p)<4 ? PNG_LSR(0xff0fff33,(3-(p))*8+(7-(x))) :\
3374 PNG_LSR(0xff55ff00,(7-(p))*8+(7-(x)))) & 1)
3375
3376 /* Return a mask for pass 'p' pixel 'x' at depth 'd'. The mask is
3377 * little endian - the first pixel is at bit 0 - however the extra
3378 * parameter 's' can be set to cause the mask position to be swapped
3379 * within each byte, to match the PNG format. This is done by XOR of
3380 * the shift with 7, 6 or 4 for bit depths 1, 2 and 4.
3381 */
3382# define PIXEL_MASK(p,x,d,s) \
3383 (PNG_LSL(((PNG_LSL(1U,(d)))-1),(((x)*(d))^((s)?8-(d):0))))
3384
3385 /* Hence generate the appropriate 'block' or 'sparkle' pixel copy mask.
3386 */
3387# define S_MASKx(p,x,d,s) (S_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
3388# define B_MASKx(p,x,d,s) (B_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
3389
3390 /* Combine 8 of these to get the full mask. For the 1-bpp and 2-bpp
3391 * cases the result needs replicating, for the 4-bpp case the above
3392 * generates a full 32 bits.
3393 */
3394# define MASK_EXPAND(m,d) ((m)*((d)==1?0x01010101:((d)==2?0x00010001:1)))
3395
3396# define S_MASK(p,d,s) MASK_EXPAND(S_MASKx(p,0,d,s) + S_MASKx(p,1,d,s) +\
3397 S_MASKx(p,2,d,s) + S_MASKx(p,3,d,s) + S_MASKx(p,4,d,s) +\
3398 S_MASKx(p,5,d,s) + S_MASKx(p,6,d,s) + S_MASKx(p,7,d,s), d)
3399
3400# define B_MASK(p,d,s) MASK_EXPAND(B_MASKx(p,0,d,s) + B_MASKx(p,1,d,s) +\
3401 B_MASKx(p,2,d,s) + B_MASKx(p,3,d,s) + B_MASKx(p,4,d,s) +\
3402 B_MASKx(p,5,d,s) + B_MASKx(p,6,d,s) + B_MASKx(p,7,d,s), d)
3403
3404#if PNG_USE_COMPILE_TIME_MASKS
3405 /* Utility macros to construct all the masks for a depth/swap
3406 * combination. The 's' parameter says whether the format is PNG
3407 * (big endian bytes) or not. Only the three odd-numbered passes are
3408 * required for the display/block algorithm.
3409 */
3410# define S_MASKS(d,s) { S_MASK(0,d,s), S_MASK(1,d,s), S_MASK(2,d,s),\
3411 S_MASK(3,d,s), S_MASK(4,d,s), S_MASK(5,d,s) }
3412
3413# define B_MASKS(d,s) { B_MASK(1,d,s), B_MASK(3,d,s), B_MASK(5,d,s) }
3414
3415# define DEPTH_INDEX(d) ((d)==1?0:((d)==2?1:2))
3416
3417 /* Hence the pre-compiled masks indexed by PACKSWAP (or not), depth and
3418 * then pass:
3419 */
3420 static const png_uint_32 row_mask[2/*PACKSWAP*/][3/*depth*/][6] =
3421 {
3422 /* Little-endian byte masks for PACKSWAP */
3423 { S_MASKS(1,0), S_MASKS(2,0), S_MASKS(4,0) },
3424 /* Normal (big-endian byte) masks - PNG format */
3425 { S_MASKS(1,1), S_MASKS(2,1), S_MASKS(4,1) }
3426 };
3427
3428 /* display_mask has only three entries for the odd passes, so index by
3429 * pass>>1.
3430 */
3431 static const png_uint_32 display_mask[2][3][3] =
3432 {
3433 /* Little-endian byte masks for PACKSWAP */
3434 { B_MASKS(1,0), B_MASKS(2,0), B_MASKS(4,0) },
3435 /* Normal (big-endian byte) masks - PNG format */
3436 { B_MASKS(1,1), B_MASKS(2,1), B_MASKS(4,1) }
3437 };
3438
3439# define MASK(pass,depth,display,png)\
3440 ((display)?display_mask[png][DEPTH_INDEX(depth)][pass>>1]:\
3441 row_mask[png][DEPTH_INDEX(depth)][pass])
3442
3443#else /* !PNG_USE_COMPILE_TIME_MASKS */
3444 /* This is the runtime alternative: it seems unlikely that this will
3445 * ever be either smaller or faster than the compile time approach.
3446 */
3447# define MASK(pass,depth,display,png)\
3448 ((display)?B_MASK(pass,depth,png):S_MASK(pass,depth,png))
3449#endif /* !USE_COMPILE_TIME_MASKS */
3450
3451 /* Use the appropriate mask to copy the required bits. In some cases
3452 * the byte mask will be 0 or 0xff; optimize these cases. row_width is
3453 * the number of pixels, but the code copies bytes, so it is necessary
3454 * to special case the end.
3455 */
3456 png_uint_32 pixels_per_byte = 8 / pixel_depth;
3457 png_uint_32 mask;
3458
3459# ifdef PNG_READ_PACKSWAP_SUPPORTED
3460 if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
3461 mask = MASK(pass, pixel_depth, display, 0);
3462
3463 else
3464# endif
3465 mask = MASK(pass, pixel_depth, display, 1);
3466
3467 for (;;)
3468 {
3469 png_uint_32 m;
3470
3471 /* It doesn't matter in the following if png_uint_32 has more than
3472 * 32 bits because the high bits always match those in m<<24; it is,
3473 * however, essential to use OR here, not +, because of this.
3474 */
3475 m = mask;
3476 mask = (m >> 8) | (m << 24); /* rotate right to good compilers */
3477 m &= 0xff;
3478
3479 if (m != 0) /* something to copy */
3480 {
3481 if (m != 0xff)
3482 *dp = (png_byte)((*dp & ~m) | (*sp & m));
3483 else
3484 *dp = *sp;
3485 }
3486
3487 /* NOTE: this may overwrite the last byte with garbage if the image
3488 * is not an exact number of bytes wide; libpng has always done
3489 * this.
3490 */
3491 if (row_width <= pixels_per_byte)
3492 break; /* May need to restore part of the last byte */
3493
3494 row_width -= pixels_per_byte;
3495 ++dp;
3496 ++sp;
3497 }
3498 }
3499
3500 else /* pixel_depth >= 8 */
3501 {
3502 unsigned int bytes_to_copy, bytes_to_jump;
3503
3504 /* Validate the depth - it must be a multiple of 8 */
3505 if (pixel_depth & 7)
3506 png_error(png_ptr, "invalid user transform pixel depth");
3507
3508 pixel_depth >>= 3; /* now in bytes */
3509 row_width *= pixel_depth;
3510
3511 /* Regardless of pass number the Adam 7 interlace always results in a
3512 * fixed number of pixels to copy then to skip. There may be a
3513 * different number of pixels to skip at the start though.
3514 */
3515 {
3516 unsigned int offset = PNG_PASS_START_COL(pass) * pixel_depth;
3517
3518 row_width -= offset;
3519 dp += offset;
3520 sp += offset;
3521 }
3522
3523 /* Work out the bytes to copy. */
3524 if (display != 0)
3525 {
3526 /* When doing the 'block' algorithm the pixel in the pass gets
3527 * replicated to adjacent pixels. This is why the even (0,2,4,6)
3528 * passes are skipped above - the entire expanded row is copied.
3529 */
3530 bytes_to_copy = (1<<((6-pass)>>1)) * pixel_depth;
3531
3532 /* But don't allow this number to exceed the actual row width. */
3533 if (bytes_to_copy > row_width)
3534 bytes_to_copy = (unsigned int)/*SAFE*/row_width;
3535 }
3536
3537 else /* normal row; Adam7 only ever gives us one pixel to copy. */
3538 bytes_to_copy = pixel_depth;
3539
3540 /* In Adam7 there is a constant offset between where the pixels go. */
3541 bytes_to_jump = PNG_PASS_COL_OFFSET(pass) * pixel_depth;
3542
3543 /* And simply copy these bytes. Some optimization is possible here,
3544 * depending on the value of 'bytes_to_copy'. Special case the low
3545 * byte counts, which we know to be frequent.
3546 *
3547 * Notice that these cases all 'return' rather than 'break' - this
3548 * avoids an unnecessary test on whether to restore the last byte
3549 * below.
3550 */
3551 switch (bytes_to_copy)
3552 {
3553 case 1:
3554 for (;;)
3555 {
3556 *dp = *sp;
3557
3558 if (row_width <= bytes_to_jump)
3559 return;
3560
3561 dp += bytes_to_jump;
3562 sp += bytes_to_jump;
3563 row_width -= bytes_to_jump;
3564 }
3565
3566 case 2:
3567 /* There is a possibility of a partial copy at the end here; this
3568 * slows the code down somewhat.
3569 */
3570 do
3571 {
3572 dp[0] = sp[0]; dp[1] = sp[1];
3573
3574 if (row_width <= bytes_to_jump)
3575 return;
3576
3577 sp += bytes_to_jump;
3578 dp += bytes_to_jump;
3579 row_width -= bytes_to_jump;
3580 }
3581 while (row_width > 1);
3582
3583 /* And there can only be one byte left at this point: */
3584 *dp = *sp;
3585 return;
3586
3587 case 3:
3588 /* This can only be the RGB case, so each copy is exactly one
3589 * pixel and it is not necessary to check for a partial copy.
3590 */
3591 for (;;)
3592 {
3593 dp[0] = sp[0]; dp[1] = sp[1]; dp[2] = sp[2];
3594
3595 if (row_width <= bytes_to_jump)
3596 return;
3597
3598 sp += bytes_to_jump;
3599 dp += bytes_to_jump;
3600 row_width -= bytes_to_jump;
3601 }
3602
3603 default:
3604#if PNG_ALIGN_TYPE != PNG_ALIGN_NONE
3605 /* Check for double byte alignment and, if possible, use a
3606 * 16-bit copy. Don't attempt this for narrow images - ones that
3607 * are less than an interlace panel wide. Don't attempt it for
3608 * wide bytes_to_copy either - use the memcpy there.
3609 */
3610 if (bytes_to_copy < 16 /*else use memcpy*/ &&
3611 png_isaligned(dp, png_uint_16) &&
3612 png_isaligned(sp, png_uint_16) &&
3613 bytes_to_copy % (sizeof (png_uint_16)) == 0 &&
3614 bytes_to_jump % (sizeof (png_uint_16)) == 0)
3615 {
3616 /* Everything is aligned for png_uint_16 copies, but try for
3617 * png_uint_32 first.
3618 */
3619 if (png_isaligned(dp, png_uint_32) &&
3620 png_isaligned(sp, png_uint_32) &&
3621 bytes_to_copy % (sizeof (png_uint_32)) == 0 &&
3622 bytes_to_jump % (sizeof (png_uint_32)) == 0)
3623 {
3624 png_uint_32p dp32 = png_aligncast(png_uint_32p,dp);
3625 png_const_uint_32p sp32 = png_aligncastconst(
3626 png_const_uint_32p, sp);
3627 size_t skip = (bytes_to_jump-bytes_to_copy) /
3628 (sizeof (png_uint_32));
3629
3630 do
3631 {
3632 size_t c = bytes_to_copy;
3633 do
3634 {
3635 *dp32++ = *sp32++;
3636 c -= (sizeof (png_uint_32));
3637 }
3638 while (c > 0);
3639
3640 if (row_width <= bytes_to_jump)
3641 return;
3642
3643 dp32 += skip;
3644 sp32 += skip;
3645 row_width -= bytes_to_jump;
3646 }
3647 while (bytes_to_copy <= row_width);
3648
3649 /* Get to here when the row_width truncates the final copy.
3650 * There will be 1-3 bytes left to copy, so don't try the
3651 * 16-bit loop below.
3652 */
3653 dp = (png_bytep)dp32;
3654 sp = (png_const_bytep)sp32;
3655 do
3656 *dp++ = *sp++;
3657 while (--row_width > 0);
3658 return;
3659 }
3660
3661 /* Else do it in 16-bit quantities, but only if the size is
3662 * not too large.
3663 */
3664 else
3665 {
3666 png_uint_16p dp16 = png_aligncast(png_uint_16p, dp);
3667 png_const_uint_16p sp16 = png_aligncastconst(
3668 png_const_uint_16p, sp);
3669 size_t skip = (bytes_to_jump-bytes_to_copy) /
3670 (sizeof (png_uint_16));
3671
3672 do
3673 {
3674 size_t c = bytes_to_copy;
3675 do
3676 {
3677 *dp16++ = *sp16++;
3678 c -= (sizeof (png_uint_16));
3679 }
3680 while (c > 0);
3681
3682 if (row_width <= bytes_to_jump)
3683 return;
3684
3685 dp16 += skip;
3686 sp16 += skip;
3687 row_width -= bytes_to_jump;
3688 }
3689 while (bytes_to_copy <= row_width);
3690
3691 /* End of row - 1 byte left, bytes_to_copy > row_width: */
3692 dp = (png_bytep)dp16;
3693 sp = (png_const_bytep)sp16;
3694 do
3695 *dp++ = *sp++;
3696 while (--row_width > 0);
3697 return;
3698 }
3699 }
3700#endif /* ALIGN_TYPE code */
3701
3702 /* The true default - use a memcpy: */
3703 for (;;)
3704 {
3705 memcpy(dp, sp, bytes_to_copy);
3706
3707 if (row_width <= bytes_to_jump)
3708 return;
3709
3710 sp += bytes_to_jump;
3711 dp += bytes_to_jump;
3712 row_width -= bytes_to_jump;
3713 if (bytes_to_copy > row_width)
3714 bytes_to_copy = (unsigned int)/*SAFE*/row_width;
3715 }
3716 }
3717
3718 /* NOT REACHED*/
3719 } /* pixel_depth >= 8 */
3720
3721 /* Here if pixel_depth < 8 to check 'end_ptr' below. */
3722 }
3723 else
3724#endif /* READ_INTERLACING */
3725
3726 /* If here then the switch above wasn't used so just memcpy the whole row
3727 * from the temporary row buffer (notice that this overwrites the end of the
3728 * destination row if it is a partial byte.)
3729 */
3730 memcpy(dp, sp, PNG_ROWBYTES(pixel_depth, row_width));
3731
3732 /* Restore the overwritten bits from the last byte if necessary. */
3733 if (end_ptr != NULL)
3734 *end_ptr = (png_byte)((end_byte & end_mask) | (*end_ptr & ~end_mask));
3735}
3736
3737#ifdef PNG_READ_INTERLACING_SUPPORTED
3738void /* PRIVATE */
3739png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
3740 png_uint_32 transformations /* Because these may affect the byte layout */)
3741{
3742 png_debug(1, "in png_do_read_interlace");
3743 if (row != NULL && row_info != NULL)
3744 {
3745 png_uint_32 final_width;
3746
3747 final_width = row_info->width * png_pass_inc[pass];
3748
3749 switch (row_info->pixel_depth)
3750 {
3751 case 1:
3752 {
3753 png_bytep sp = row + (size_t)((row_info->width - 1) >> 3);
3754 png_bytep dp = row + (size_t)((final_width - 1) >> 3);
3755 unsigned int sshift, dshift;
3756 unsigned int s_start, s_end;
3757 int s_inc;
3758 int jstop = (int)png_pass_inc[pass];
3759 png_byte v;
3760 png_uint_32 i;
3761 int j;
3762
3763#ifdef PNG_READ_PACKSWAP_SUPPORTED
3764 if ((transformations & PNG_PACKSWAP) != 0)
3765 {
3766 sshift = ((row_info->width + 7) & 0x07);
3767 dshift = ((final_width + 7) & 0x07);
3768 s_start = 7;
3769 s_end = 0;
3770 s_inc = -1;
3771 }
3772
3773 else
3774#endif
3775 {
3776 sshift = 7 - ((row_info->width + 7) & 0x07);
3777 dshift = 7 - ((final_width + 7) & 0x07);
3778 s_start = 0;
3779 s_end = 7;
3780 s_inc = 1;
3781 }
3782
3783 for (i = 0; i < row_info->width; i++)
3784 {
3785 v = (png_byte)((*sp >> sshift) & 0x01);
3786 for (j = 0; j < jstop; j++)
3787 {
3788 unsigned int tmp = *dp & (0x7f7f >> (7 - dshift));
3789 tmp |= (unsigned int)(v << dshift);
3790 *dp = (png_byte)(tmp & 0xff);
3791
3792 if (dshift == s_end)
3793 {
3794 dshift = s_start;
3795 dp--;
3796 }
3797
3798 else
3799 dshift = (unsigned int)((int)dshift + s_inc);
3800 }
3801
3802 if (sshift == s_end)
3803 {
3804 sshift = s_start;
3805 sp--;
3806 }
3807
3808 else
3809 sshift = (unsigned int)((int)sshift + s_inc);
3810 }
3811 break;
3812 }
3813
3814 case 2:
3815 {
3816 png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);
3817 png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);
3818 unsigned int sshift, dshift;
3819 unsigned int s_start, s_end;
3820 int s_inc;
3821 int jstop = (int)png_pass_inc[pass];
3822 png_uint_32 i;
3823
3824#ifdef PNG_READ_PACKSWAP_SUPPORTED
3825 if ((transformations & PNG_PACKSWAP) != 0)
3826 {
3827 sshift = (((row_info->width + 3) & 0x03) << 1);
3828 dshift = (((final_width + 3) & 0x03) << 1);
3829 s_start = 6;
3830 s_end = 0;
3831 s_inc = -2;
3832 }
3833
3834 else
3835#endif
3836 {
3837 sshift = ((3 - ((row_info->width + 3) & 0x03)) << 1);
3838 dshift = ((3 - ((final_width + 3) & 0x03)) << 1);
3839 s_start = 0;
3840 s_end = 6;
3841 s_inc = 2;
3842 }
3843
3844 for (i = 0; i < row_info->width; i++)
3845 {
3846 png_byte v;
3847 int j;
3848
3849 v = (png_byte)((*sp >> sshift) & 0x03);
3850 for (j = 0; j < jstop; j++)
3851 {
3852 unsigned int tmp = *dp & (0x3f3f >> (6 - dshift));
3853 tmp |= (unsigned int)(v << dshift);
3854 *dp = (png_byte)(tmp & 0xff);
3855
3856 if (dshift == s_end)
3857 {
3858 dshift = s_start;
3859 dp--;
3860 }
3861
3862 else
3863 dshift = (unsigned int)((int)dshift + s_inc);
3864 }
3865
3866 if (sshift == s_end)
3867 {
3868 sshift = s_start;
3869 sp--;
3870 }
3871
3872 else
3873 sshift = (unsigned int)((int)sshift + s_inc);
3874 }
3875 break;
3876 }
3877
3878 case 4:
3879 {
3880 png_bytep sp = row + (size_t)((row_info->width - 1) >> 1);
3881 png_bytep dp = row + (size_t)((final_width - 1) >> 1);
3882 unsigned int sshift, dshift;
3883 unsigned int s_start, s_end;
3884 int s_inc;
3885 png_uint_32 i;
3886 int jstop = (int)png_pass_inc[pass];
3887
3888#ifdef PNG_READ_PACKSWAP_SUPPORTED
3889 if ((transformations & PNG_PACKSWAP) != 0)
3890 {
3891 sshift = (((row_info->width + 1) & 0x01) << 2);
3892 dshift = (((final_width + 1) & 0x01) << 2);
3893 s_start = 4;
3894 s_end = 0;
3895 s_inc = -4;
3896 }
3897
3898 else
3899#endif
3900 {
3901 sshift = ((1 - ((row_info->width + 1) & 0x01)) << 2);
3902 dshift = ((1 - ((final_width + 1) & 0x01)) << 2);
3903 s_start = 0;
3904 s_end = 4;
3905 s_inc = 4;
3906 }
3907
3908 for (i = 0; i < row_info->width; i++)
3909 {
3910 png_byte v = (png_byte)((*sp >> sshift) & 0x0f);
3911 int j;
3912
3913 for (j = 0; j < jstop; j++)
3914 {
3915 unsigned int tmp = *dp & (0xf0f >> (4 - dshift));
3916 tmp |= (unsigned int)(v << dshift);
3917 *dp = (png_byte)(tmp & 0xff);
3918
3919 if (dshift == s_end)
3920 {
3921 dshift = s_start;
3922 dp--;
3923 }
3924
3925 else
3926 dshift = (unsigned int)((int)dshift + s_inc);
3927 }
3928
3929 if (sshift == s_end)
3930 {
3931 sshift = s_start;
3932 sp--;
3933 }
3934
3935 else
3936 sshift = (unsigned int)((int)sshift + s_inc);
3937 }
3938 break;
3939 }
3940
3941 default:
3942 {
3943 size_t pixel_bytes = (row_info->pixel_depth >> 3);
3944
3945 png_bytep sp = row + (size_t)(row_info->width - 1)
3946 * pixel_bytes;
3947
3948 png_bytep dp = row + (size_t)(final_width - 1) * pixel_bytes;
3949
3950 int jstop = (int)png_pass_inc[pass];
3951 png_uint_32 i;
3952
3953 for (i = 0; i < row_info->width; i++)
3954 {
3955 png_byte v[8]; /* SAFE; pixel_depth does not exceed 64 */
3956 int j;
3957
3958 memcpy(v, sp, pixel_bytes);
3959
3960 for (j = 0; j < jstop; j++)
3961 {
3962 memcpy(dp, v, pixel_bytes);
3963 dp -= pixel_bytes;
3964 }
3965
3966 sp -= pixel_bytes;
3967 }
3968 break;
3969 }
3970 }
3971
3972 row_info->width = final_width;
3973 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width);
3974 }
3975#ifndef PNG_READ_PACKSWAP_SUPPORTED
3976 PNG_UNUSED(transformations) /* Silence compiler warning */
3977#endif
3978}
3979#endif /* READ_INTERLACING */
3980
3981static void
3982png_read_filter_row_sub(png_row_infop row_info, png_bytep row,
3983 png_const_bytep prev_row)
3984{
3985 size_t i;
3986 size_t istop = row_info->rowbytes;
3987 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
3988 png_bytep rp = row + bpp;
3989
3990 PNG_UNUSED(prev_row)
3991
3992 for (i = bpp; i < istop; i++)
3993 {
3994 *rp = (png_byte)(((int)(*rp) + (int)(*(rp-bpp))) & 0xff);
3995 rp++;
3996 }
3997}
3998
3999static void
4000png_read_filter_row_up(png_row_infop row_info, png_bytep row,
4001 png_const_bytep prev_row)
4002{
4003 size_t i;
4004 size_t istop = row_info->rowbytes;
4005 png_bytep rp = row;
4006 png_const_bytep pp = prev_row;
4007
4008 for (i = 0; i < istop; i++)
4009 {
4010 *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
4011 rp++;
4012 }
4013}
4014
4015static void
4016png_read_filter_row_avg(png_row_infop row_info, png_bytep row,
4017 png_const_bytep prev_row)
4018{
4019 size_t i;
4020 png_bytep rp = row;
4021 png_const_bytep pp = prev_row;
4022 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
4023 size_t istop = row_info->rowbytes - bpp;
4024
4025 for (i = 0; i < bpp; i++)
4026 {
4027 *rp = (png_byte)(((int)(*rp) +
4028 ((int)(*pp++) / 2 )) & 0xff);
4029
4030 rp++;
4031 }
4032
4033 for (i = 0; i < istop; i++)
4034 {
4035 *rp = (png_byte)(((int)(*rp) +
4036 (int)(*pp++ + *(rp-bpp)) / 2 ) & 0xff);
4037
4038 rp++;
4039 }
4040}
4041
4042static void
4043png_read_filter_row_paeth_1byte_pixel(png_row_infop row_info, png_bytep row,
4044 png_const_bytep prev_row)
4045{
4046 png_bytep rp_end = row + row_info->rowbytes;
4047 int a, c;
4048
4049 /* First pixel/byte */
4050 c = *prev_row++;
4051 a = *row + c;
4052 *row++ = (png_byte)a;
4053
4054 /* Remainder */
4055 while (row < rp_end)
4056 {
4057 int b, pa, pb, pc, p;
4058
4059 a &= 0xff; /* From previous iteration or start */
4060 b = *prev_row++;
4061
4062 p = b - c;
4063 pc = a - c;
4064
4065#ifdef PNG_USE_ABS
4066 pa = abs(p);
4067 pb = abs(pc);
4068 pc = abs(p + pc);
4069#else
4070 pa = p < 0 ? -p : p;
4071 pb = pc < 0 ? -pc : pc;
4072 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
4073#endif
4074
4075 /* Find the best predictor, the least of pa, pb, pc favoring the earlier
4076 * ones in the case of a tie.
4077 */
4078 if (pb < pa)
4079 {
4080 pa = pb; a = b;
4081 }
4082 if (pc < pa) a = c;
4083
4084 /* Calculate the current pixel in a, and move the previous row pixel to c
4085 * for the next time round the loop
4086 */
4087 c = b;
4088 a += *row;
4089 *row++ = (png_byte)a;
4090 }
4091}
4092
4093static void
4094png_read_filter_row_paeth_multibyte_pixel(png_row_infop row_info, png_bytep row,
4095 png_const_bytep prev_row)
4096{
4097 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
4098 png_bytep rp_end = row + bpp;
4099
4100 /* Process the first pixel in the row completely (this is the same as 'up'
4101 * because there is only one candidate predictor for the first row).
4102 */
4103 while (row < rp_end)
4104 {
4105 int a = *row + *prev_row++;
4106 *row++ = (png_byte)a;
4107 }
4108
4109 /* Remainder */
4110 rp_end = rp_end + (row_info->rowbytes - bpp);
4111
4112 while (row < rp_end)
4113 {
4114 int a, b, c, pa, pb, pc, p;
4115
4116 c = *(prev_row - bpp);
4117 a = *(row - bpp);
4118 b = *prev_row++;
4119
4120 p = b - c;
4121 pc = a - c;
4122
4123#ifdef PNG_USE_ABS
4124 pa = abs(p);
4125 pb = abs(pc);
4126 pc = abs(p + pc);
4127#else
4128 pa = p < 0 ? -p : p;
4129 pb = pc < 0 ? -pc : pc;
4130 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
4131#endif
4132
4133 if (pb < pa)
4134 {
4135 pa = pb; a = b;
4136 }
4137 if (pc < pa) a = c;
4138
4139 a += *row;
4140 *row++ = (png_byte)a;
4141 }
4142}
4143
4144static void
4145png_init_filter_functions(png_structrp pp)
4146 /* This function is called once for every PNG image (except for PNG images
4147 * that only use PNG_FILTER_VALUE_NONE for all rows) to set the
4148 * implementations required to reverse the filtering of PNG rows. Reversing
4149 * the filter is the first transformation performed on the row data. It is
4150 * performed in place, therefore an implementation can be selected based on
4151 * the image pixel format. If the implementation depends on image width then
4152 * take care to ensure that it works correctly if the image is interlaced -
4153 * interlacing causes the actual row width to vary.
4154 */
4155{
4156 unsigned int bpp = (pp->pixel_depth + 7) >> 3;
4157
4158 pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub;
4159 pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up;
4160 pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg;
4161 if (bpp == 1)
4162 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
4163 png_read_filter_row_paeth_1byte_pixel;
4164 else
4165 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
4166 png_read_filter_row_paeth_multibyte_pixel;
4167
4168#ifdef PNG_FILTER_OPTIMIZATIONS
4169 /* To use this define PNG_FILTER_OPTIMIZATIONS as the name of a function to
4170 * call to install hardware optimizations for the above functions; simply
4171 * replace whatever elements of the pp->read_filter[] array with a hardware
4172 * specific (or, for that matter, generic) optimization.
4173 *
4174 * To see an example of this examine what configure.ac does when
4175 * --enable-arm-neon is specified on the command line.
4176 */
4177 PNG_FILTER_OPTIMIZATIONS(pp, bpp);
4178#endif
4179}
4180
4181void /* PRIVATE */
4182png_read_filter_row(png_structrp pp, png_row_infop row_info, png_bytep row,
4183 png_const_bytep prev_row, int filter)
4184{
4185 /* OPTIMIZATION: DO NOT MODIFY THIS FUNCTION, instead #define
4186 * PNG_FILTER_OPTIMIZATIONS to a function that overrides the generic
4187 * implementations. See png_init_filter_functions above.
4188 */
4189 if (filter > PNG_FILTER_VALUE_NONE && filter < PNG_FILTER_VALUE_LAST)
4190 {
4191 if (pp->read_filter[0] == NULL)
4192 png_init_filter_functions(pp);
4193
4194 pp->read_filter[filter-1](row_info, row, prev_row);
4195 }
4196}
4197
4198#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
4199void /* PRIVATE */
4200png_read_IDAT_data(png_structrp png_ptr, png_bytep output,
4201 png_alloc_size_t avail_out)
4202{
4203 /* Loop reading IDATs and decompressing the result into output[avail_out] */
4204 png_ptr->zstream.next_out = output;
4205 png_ptr->zstream.avail_out = 0; /* safety: set below */
4206
4207 if (output == NULL)
4208 avail_out = 0;
4209
4210 do
4211 {
4212 int ret;
4213 png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];
4214
4215 if (png_ptr->zstream.avail_in == 0)
4216 {
4217 uInt avail_in;
4218 png_bytep buffer;
4219
4220 while (png_ptr->idat_size == 0)
4221 {
4222 png_crc_finish(png_ptr, 0);
4223
4224 png_ptr->idat_size = png_read_chunk_header(png_ptr);
4225 /* This is an error even in the 'check' case because the code just
4226 * consumed a non-IDAT header.
4227 */
4228 if (png_ptr->chunk_name != png_IDAT)
4229 png_error(png_ptr, "Not enough image data");
4230 }
4231
4232 avail_in = png_ptr->IDAT_read_size;
4233
4234 if (avail_in > png_ptr->idat_size)
4235 avail_in = (uInt)png_ptr->idat_size;
4236
4237 /* A PNG with a gradually increasing IDAT size will defeat this attempt
4238 * to minimize memory usage by causing lots of re-allocs, but
4239 * realistically doing IDAT_read_size re-allocs is not likely to be a
4240 * big problem.
4241 */
4242 buffer = png_read_buffer(png_ptr, avail_in, 0/*error*/);
4243
4244 png_crc_read(png_ptr, buffer, avail_in);
4245 png_ptr->idat_size -= avail_in;
4246
4247 png_ptr->zstream.next_in = buffer;
4248 png_ptr->zstream.avail_in = avail_in;
4249 }
4250
4251 /* And set up the output side. */
4252 if (output != NULL) /* standard read */
4253 {
4254 uInt out = ZLIB_IO_MAX;
4255
4256 if (out > avail_out)
4257 out = (uInt)avail_out;
4258
4259 avail_out -= out;
4260 png_ptr->zstream.avail_out = out;
4261 }
4262
4263 else /* after last row, checking for end */
4264 {
4265 png_ptr->zstream.next_out = tmpbuf;
4266 png_ptr->zstream.avail_out = (sizeof tmpbuf);
4267 }
4268
4269 /* Use NO_FLUSH; this gives zlib the maximum opportunity to optimize the
4270 * process. If the LZ stream is truncated the sequential reader will
4271 * terminally damage the stream, above, by reading the chunk header of the
4272 * following chunk (it then exits with png_error).
4273 *
4274 * TODO: deal more elegantly with truncated IDAT lists.
4275 */
4276 ret = PNG_INFLATE(png_ptr, Z_NO_FLUSH);
4277
4278 /* Take the unconsumed output back. */
4279 if (output != NULL)
4280 avail_out += png_ptr->zstream.avail_out;
4281
4282 else /* avail_out counts the extra bytes */
4283 avail_out += (sizeof tmpbuf) - png_ptr->zstream.avail_out;
4284
4285 png_ptr->zstream.avail_out = 0;
4286
4287 if (ret == Z_STREAM_END)
4288 {
4289 /* Do this for safety; we won't read any more into this row. */
4290 png_ptr->zstream.next_out = NULL;
4291
4292 png_ptr->mode |= PNG_AFTER_IDAT;
4293 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
4294
4295 if (png_ptr->zstream.avail_in > 0 || png_ptr->idat_size > 0)
4296 png_chunk_benign_error(png_ptr, "Extra compressed data");
4297 break;
4298 }
4299
4300 if (ret != Z_OK)
4301 {
4302 png_zstream_error(png_ptr, ret);
4303
4304 if (output != NULL)
4305 png_chunk_error(png_ptr, png_ptr->zstream.msg);
4306
4307 else /* checking */
4308 {
4309 png_chunk_benign_error(png_ptr, png_ptr->zstream.msg);
4310 return;
4311 }
4312 }
4313 } while (avail_out > 0);
4314
4315 if (avail_out > 0)
4316 {
4317 /* The stream ended before the image; this is the same as too few IDATs so
4318 * should be handled the same way.
4319 */
4320 if (output != NULL)
4321 png_error(png_ptr, "Not enough image data");
4322
4323 else /* the deflate stream contained extra data */
4324 png_chunk_benign_error(png_ptr, "Too much image data");
4325 }
4326}
4327
4328void /* PRIVATE */
4329png_read_finish_IDAT(png_structrp png_ptr)
4330{
4331 /* We don't need any more data and the stream should have ended, however the
4332 * LZ end code may actually not have been processed. In this case we must
4333 * read it otherwise stray unread IDAT data or, more likely, an IDAT chunk
4334 * may still remain to be consumed.
4335 */
4336 if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
4337 {
4338 /* The NULL causes png_read_IDAT_data to swallow any remaining bytes in
4339 * the compressed stream, but the stream may be damaged too, so even after
4340 * this call we may need to terminate the zstream ownership.
4341 */
4342 png_read_IDAT_data(png_ptr, NULL, 0);
4343 png_ptr->zstream.next_out = NULL; /* safety */
4344
4345 /* Now clear everything out for safety; the following may not have been
4346 * done.
4347 */
4348 if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
4349 {
4350 png_ptr->mode |= PNG_AFTER_IDAT;
4351 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
4352 }
4353 }
4354
4355 /* If the zstream has not been released do it now *and* terminate the reading
4356 * of the final IDAT chunk.
4357 */
4358 if (png_ptr->zowner == png_IDAT)
4359 {
4360 /* Always do this; the pointers otherwise point into the read buffer. */
4361 png_ptr->zstream.next_in = NULL;
4362 png_ptr->zstream.avail_in = 0;
4363
4364 /* Now we no longer own the zstream. */
4365 png_ptr->zowner = 0;
4366
4367 /* The slightly weird semantics of the sequential IDAT reading is that we
4368 * are always in or at the end of an IDAT chunk, so we always need to do a
4369 * crc_finish here. If idat_size is non-zero we also need to read the
4370 * spurious bytes at the end of the chunk now.
4371 */
4372 (void)png_crc_finish(png_ptr, png_ptr->idat_size);
4373 }
4374}
4375
4376void /* PRIVATE */
4377png_read_finish_row(png_structrp png_ptr)
4378{
4379 png_debug(1, "in png_read_finish_row");
4380 png_ptr->row_number++;
4381 if (png_ptr->row_number < png_ptr->num_rows)
4382 return;
4383
4384 if (png_ptr->interlaced != 0)
4385 {
4386 png_ptr->row_number = 0;
4387
4388 /* TO DO: don't do this if prev_row isn't needed (requires
4389 * read-ahead of the next row's filter byte.
4390 */
4391 memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
4392
4393 do
4394 {
4395 png_ptr->pass++;
4396
4397 if (png_ptr->pass >= 7)
4398 break;
4399
4400 png_ptr->iwidth = (png_ptr->width +
4401 png_pass_inc[png_ptr->pass] - 1 -
4402 png_pass_start[png_ptr->pass]) /
4403 png_pass_inc[png_ptr->pass];
4404
4405 if ((png_ptr->transformations & PNG_INTERLACE) == 0)
4406 {
4407 png_ptr->num_rows = (png_ptr->height +
4408 png_pass_yinc[png_ptr->pass] - 1 -
4409 png_pass_ystart[png_ptr->pass]) /
4410 png_pass_yinc[png_ptr->pass];
4411 }
4412
4413 else /* if (png_ptr->transformations & PNG_INTERLACE) */
4414 break; /* libpng deinterlacing sees every row */
4415
4416 } while (png_ptr->num_rows == 0 || png_ptr->iwidth == 0);
4417
4418 if (png_ptr->pass < 7)
4419 return;
4420 }
4421
4422 /* Here after at the end of the last row of the last pass. */
4423 png_read_finish_IDAT(png_ptr);
4424}
4425#endif /* SEQUENTIAL_READ */
4426
4427void /* PRIVATE */
4428png_read_start_row(png_structrp png_ptr)
4429{
4430 unsigned int max_pixel_depth;
4431 size_t row_bytes;
4432
4433 png_debug(1, "in png_read_start_row");
4434
4435#ifdef PNG_READ_TRANSFORMS_SUPPORTED
4436 png_init_read_transformations(png_ptr);
4437#endif
4438 if (png_ptr->interlaced != 0)
4439 {
4440 if ((png_ptr->transformations & PNG_INTERLACE) == 0)
4441 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
4442 png_pass_ystart[0]) / png_pass_yinc[0];
4443
4444 else
4445 png_ptr->num_rows = png_ptr->height;
4446
4447 png_ptr->iwidth = (png_ptr->width +
4448 png_pass_inc[png_ptr->pass] - 1 -
4449 png_pass_start[png_ptr->pass]) /
4450 png_pass_inc[png_ptr->pass];
4451 }
4452
4453 else
4454 {
4455 png_ptr->num_rows = png_ptr->height;
4456 png_ptr->iwidth = png_ptr->width;
4457 }
4458
4459 max_pixel_depth = (unsigned int)png_ptr->pixel_depth;
4460
4461 /* WARNING: * png_read_transform_info (pngrtran.c) performs a simpler set of
4462 * calculations to calculate the final pixel depth, then
4463 * png_do_read_transforms actually does the transforms. This means that the
4464 * code which effectively calculates this value is actually repeated in three
4465 * separate places. They must all match. Innocent changes to the order of
4466 * transformations can and will break libpng in a way that causes memory
4467 * overwrites.
4468 *
4469 * TODO: fix this.
4470 */
4471#ifdef PNG_READ_PACK_SUPPORTED
4472 if ((png_ptr->transformations & PNG_PACK) != 0 && png_ptr->bit_depth < 8)
4473 max_pixel_depth = 8;
4474#endif
4475
4476#ifdef PNG_READ_EXPAND_SUPPORTED
4477 if ((png_ptr->transformations & PNG_EXPAND) != 0)
4478 {
4479 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
4480 {
4481 if (png_ptr->num_trans != 0)
4482 max_pixel_depth = 32;
4483
4484 else
4485 max_pixel_depth = 24;
4486 }
4487
4488 else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
4489 {
4490 if (max_pixel_depth < 8)
4491 max_pixel_depth = 8;
4492
4493 if (png_ptr->num_trans != 0)
4494 max_pixel_depth *= 2;
4495 }
4496
4497 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
4498 {
4499 if (png_ptr->num_trans != 0)
4500 {
4501 max_pixel_depth *= 4;
4502 max_pixel_depth /= 3;
4503 }
4504 }
4505 }
4506#endif
4507
4508#ifdef PNG_READ_EXPAND_16_SUPPORTED
4509 if ((png_ptr->transformations & PNG_EXPAND_16) != 0)
4510 {
4511# ifdef PNG_READ_EXPAND_SUPPORTED
4512 /* In fact it is an error if it isn't supported, but checking is
4513 * the safe way.
4514 */
4515 if ((png_ptr->transformations & PNG_EXPAND) != 0)
4516 {
4517 if (png_ptr->bit_depth < 16)
4518 max_pixel_depth *= 2;
4519 }
4520 else
4521# endif
4522 png_ptr->transformations &= ~PNG_EXPAND_16;
4523 }
4524#endif
4525
4526#ifdef PNG_READ_FILLER_SUPPORTED
4527 if ((png_ptr->transformations & (PNG_FILLER)) != 0)
4528 {
4529 if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
4530 {
4531 if (max_pixel_depth <= 8)
4532 max_pixel_depth = 16;
4533
4534 else
4535 max_pixel_depth = 32;
4536 }
4537
4538 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB ||
4539 png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
4540 {
4541 if (max_pixel_depth <= 32)
4542 max_pixel_depth = 32;
4543
4544 else
4545 max_pixel_depth = 64;
4546 }
4547 }
4548#endif
4549
4550#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
4551 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0)
4552 {
4553 if (
4554#ifdef PNG_READ_EXPAND_SUPPORTED
4555 (png_ptr->num_trans != 0 &&
4556 (png_ptr->transformations & PNG_EXPAND) != 0) ||
4557#endif
4558#ifdef PNG_READ_FILLER_SUPPORTED
4559 (png_ptr->transformations & (PNG_FILLER)) != 0 ||
4560#endif
4561 png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
4562 {
4563 if (max_pixel_depth <= 16)
4564 max_pixel_depth = 32;
4565
4566 else
4567 max_pixel_depth = 64;
4568 }
4569
4570 else
4571 {
4572 if (max_pixel_depth <= 8)
4573 {
4574 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4575 max_pixel_depth = 32;
4576
4577 else
4578 max_pixel_depth = 24;
4579 }
4580
4581 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4582 max_pixel_depth = 64;
4583
4584 else
4585 max_pixel_depth = 48;
4586 }
4587 }
4588#endif
4589
4590#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
4591defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
4592 if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0)
4593 {
4594 unsigned int user_pixel_depth = png_ptr->user_transform_depth *
4595 png_ptr->user_transform_channels;
4596
4597 if (user_pixel_depth > max_pixel_depth)
4598 max_pixel_depth = user_pixel_depth;
4599 }
4600#endif
4601
4602 /* This value is stored in png_struct and double checked in the row read
4603 * code.
4604 */
4605 png_ptr->maximum_pixel_depth = (png_byte)max_pixel_depth;
4606 png_ptr->transformed_pixel_depth = 0; /* calculated on demand */
4607
4608 /* Align the width on the next larger 8 pixels. Mainly used
4609 * for interlacing
4610 */
4611 row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
4612 /* Calculate the maximum bytes needed, adding a byte and a pixel
4613 * for safety's sake
4614 */
4615 row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) +
4616 1 + ((max_pixel_depth + 7) >> 3U);
4617
4618#ifdef PNG_MAX_MALLOC_64K
4619 if (row_bytes > (png_uint_32)65536L)
4620 png_error(png_ptr, "This image requires a row greater than 64KB");
4621#endif
4622
4623 if (row_bytes + 48 > png_ptr->old_big_row_buf_size)
4624 {
4625 png_free(png_ptr, png_ptr->big_row_buf);
4626 png_free(png_ptr, png_ptr->big_prev_row);
4627
4628 if (png_ptr->interlaced != 0)
4629 png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr,
4630 row_bytes + 48);
4631
4632 else
4633 png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
4634
4635 png_ptr->big_prev_row = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
4636
4637#ifdef PNG_ALIGNED_MEMORY_SUPPORTED
4638 /* Use 16-byte aligned memory for row_buf with at least 16 bytes
4639 * of padding before and after row_buf; treat prev_row similarly.
4640 * NOTE: the alignment is to the start of the pixels, one beyond the start
4641 * of the buffer, because of the filter byte. Prior to libpng 1.5.6 this
4642 * was incorrect; the filter byte was aligned, which had the exact
4643 * opposite effect of that intended.
4644 */
4645 {
4646 png_bytep temp = png_ptr->big_row_buf + 32;
4647 size_t extra = (size_t)temp & 0x0f;
4648 png_ptr->row_buf = temp - extra - 1/*filter byte*/;
4649
4650 temp = png_ptr->big_prev_row + 32;
4651 extra = (size_t)temp & 0x0f;
4652 png_ptr->prev_row = temp - extra - 1/*filter byte*/;
4653 }
4654#else
4655 /* Use 31 bytes of padding before and 17 bytes after row_buf. */
4656 png_ptr->row_buf = png_ptr->big_row_buf + 31;
4657 png_ptr->prev_row = png_ptr->big_prev_row + 31;
4658#endif
4659 png_ptr->old_big_row_buf_size = row_bytes + 48;
4660 }
4661
4662#ifdef PNG_MAX_MALLOC_64K
4663 if (png_ptr->rowbytes > 65535)
4664 png_error(png_ptr, "This image requires a row greater than 64KB");
4665
4666#endif
4667 if (png_ptr->rowbytes > (PNG_SIZE_MAX - 1))
4668 png_error(png_ptr, "Row has too many bytes to allocate in memory");
4669
4670 memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
4671
4672 png_debug1(3, "width = %u,", png_ptr->width);
4673 png_debug1(3, "height = %u,", png_ptr->height);
4674 png_debug1(3, "iwidth = %u,", png_ptr->iwidth);
4675 png_debug1(3, "num_rows = %u,", png_ptr->num_rows);
4676 png_debug1(3, "rowbytes = %lu,", (unsigned long)png_ptr->rowbytes);
4677 png_debug1(3, "irowbytes = %lu",
4678 (unsigned long)PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1);
4679
4680 /* The sequential reader needs a buffer for IDAT, but the progressive reader
4681 * does not, so free the read buffer now regardless; the sequential reader
4682 * reallocates it on demand.
4683 */
4684 if (png_ptr->read_buffer != NULL)
4685 {
4686 png_bytep buffer = png_ptr->read_buffer;
4687
4688 png_ptr->read_buffer_size = 0;
4689 png_ptr->read_buffer = NULL;
4690 png_free(png_ptr, buffer);
4691 }
4692
4693 /* Finally claim the zstream for the inflate of the IDAT data, use the bits
4694 * value from the stream (note that this will result in a fatal error if the
4695 * IDAT stream has a bogus deflate header window_bits value, but this should
4696 * not be happening any longer!)
4697 */
4698 if (png_inflate_claim(png_ptr, png_IDAT) != Z_OK)
4699 png_error(png_ptr, png_ptr->zstream.msg);
4700
4701 png_ptr->flags |= PNG_FLAG_ROW_INIT;
4702}
4703#endif /* READ */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette