[uxtheme]
[reactos.git] / reactos / dll / 3rdparty / libpng / pngrutil.c
1
2 /* pngrutil.c - utilities to read a PNG file
3 *
4 * Last changed in libpng 1.5.2 [March 31, 2011]
5 * Copyright (c) 1998-2011 Glenn Randers-Pehrson
6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
8 *
9 * This code is released under the libpng license.
10 * For conditions of distribution and use, see the disclaimer
11 * and license in png.h
12 *
13 * This file contains routines that are only called from within
14 * libpng itself during the course of reading an image.
15 */
16
17 #include "pngpriv.h"
18
19 #ifdef PNG_READ_SUPPORTED
20
21 #define png_strtod(p,a,b) strtod(a,b)
22
23 png_uint_32 PNGAPI
24 png_get_uint_31(png_structp png_ptr, png_const_bytep buf)
25 {
26 png_uint_32 uval = png_get_uint_32(buf);
27
28 if (uval > PNG_UINT_31_MAX)
29 png_error(png_ptr, "PNG unsigned integer out of range");
30
31 return (uval);
32 }
33
34 #if defined(PNG_READ_gAMA_SUPPORTED) || defined(PNG_READ_cHRM_SUPPORTED)
35 /* The following is a variation on the above for use with the fixed
36 * point values used for gAMA and cHRM. Instead of png_error it
37 * issues a warning and returns (-1) - an invalid value because both
38 * gAMA and cHRM use *unsigned* integers for fixed point values.
39 */
40 #define PNG_FIXED_ERROR (-1)
41
42 static png_fixed_point /* PRIVATE */
43 png_get_fixed_point(png_structp png_ptr, png_const_bytep buf)
44 {
45 png_uint_32 uval = png_get_uint_32(buf);
46
47 if (uval <= PNG_UINT_31_MAX)
48 return (png_fixed_point)uval; /* known to be in range */
49
50 /* The caller can turn off the warning by passing NULL. */
51 if (png_ptr != NULL)
52 png_warning(png_ptr, "PNG fixed point integer out of range");
53
54 return PNG_FIXED_ERROR;
55 }
56 #endif
57
58 #ifdef PNG_READ_INT_FUNCTIONS_SUPPORTED
59 /* NOTE: the read macros will obscure these definitions, so that if
60 * PNG_USE_READ_MACROS is set the library will not use them internally,
61 * but the APIs will still be available externally.
62 *
63 * The parentheses around "PNGAPI function_name" in the following three
64 * functions are necessary because they allow the macros to co-exist with
65 * these (unused but exported) functions.
66 */
67
68 /* Grab an unsigned 32-bit integer from a buffer in big-endian format. */
69 png_uint_32 (PNGAPI
70 png_get_uint_32)(png_const_bytep buf)
71 {
72 png_uint_32 uval =
73 ((png_uint_32)(*(buf )) << 24) +
74 ((png_uint_32)(*(buf + 1)) << 16) +
75 ((png_uint_32)(*(buf + 2)) << 8) +
76 ((png_uint_32)(*(buf + 3)) ) ;
77
78 return uval;
79 }
80
81 /* Grab a signed 32-bit integer from a buffer in big-endian format. The
82 * data is stored in the PNG file in two's complement format and there
83 * is no guarantee that a 'png_int_32' is exactly 32 bits, therefore
84 * the following code does a two's complement to native conversion.
85 */
86 png_int_32 (PNGAPI
87 png_get_int_32)(png_const_bytep buf)
88 {
89 png_uint_32 uval = png_get_uint_32(buf);
90 if ((uval & 0x80000000L) == 0) /* non-negative */
91 return uval;
92
93 uval = (uval ^ 0xffffffffL) + 1; /* 2's complement: -x = ~x+1 */
94 return -(png_int_32)uval;
95 }
96
97 /* Grab an unsigned 16-bit integer from a buffer in big-endian format. */
98 png_uint_16 (PNGAPI
99 png_get_uint_16)(png_const_bytep buf)
100 {
101 /* ANSI-C requires an int value to accomodate at least 16 bits so this
102 * works and allows the compiler not to worry about possible narrowing
103 * on 32 bit systems. (Pre-ANSI systems did not make integers smaller
104 * than 16 bits either.)
105 */
106 unsigned int val =
107 ((unsigned int)(*buf) << 8) +
108 ((unsigned int)(*(buf + 1)));
109
110 return (png_uint_16)val;
111 }
112
113 #endif /* PNG_READ_INT_FUNCTIONS_SUPPORTED */
114
115 /* Read and check the PNG file signature */
116 void /* PRIVATE */
117 png_read_sig(png_structp png_ptr, png_infop info_ptr)
118 {
119 png_size_t num_checked, num_to_check;
120
121 /* Exit if the user application does not expect a signature. */
122 if (png_ptr->sig_bytes >= 8)
123 return;
124
125 num_checked = png_ptr->sig_bytes;
126 num_to_check = 8 - num_checked;
127
128 #ifdef PNG_IO_STATE_SUPPORTED
129 png_ptr->io_state = PNG_IO_READING | PNG_IO_SIGNATURE;
130 #endif
131
132 /* The signature must be serialized in a single I/O call. */
133 png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
134 png_ptr->sig_bytes = 8;
135
136 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
137 {
138 if (num_checked < 4 &&
139 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
140 png_error(png_ptr, "Not a PNG file");
141 else
142 png_error(png_ptr, "PNG file corrupted by ASCII conversion");
143 }
144 if (num_checked < 3)
145 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
146 }
147
148 /* Read the chunk header (length + type name).
149 * Put the type name into png_ptr->chunk_name, and return the length.
150 */
151 png_uint_32 /* PRIVATE */
152 png_read_chunk_header(png_structp png_ptr)
153 {
154 png_byte buf[8];
155 png_uint_32 length;
156
157 #ifdef PNG_IO_STATE_SUPPORTED
158 png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_HDR;
159 #endif
160
161 /* Read the length and the chunk name.
162 * This must be performed in a single I/O call.
163 */
164 png_read_data(png_ptr, buf, 8);
165 length = png_get_uint_31(png_ptr, buf);
166
167 /* Put the chunk name into png_ptr->chunk_name. */
168 png_memcpy(png_ptr->chunk_name, buf + 4, 4);
169
170 png_debug2(0, "Reading %s chunk, length = %u",
171 png_ptr->chunk_name, length);
172
173 /* Reset the crc and run it over the chunk name. */
174 png_reset_crc(png_ptr);
175 png_calculate_crc(png_ptr, png_ptr->chunk_name, 4);
176
177 /* Check to see if chunk name is valid. */
178 png_check_chunk_name(png_ptr, png_ptr->chunk_name);
179
180 #ifdef PNG_IO_STATE_SUPPORTED
181 png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_DATA;
182 #endif
183
184 return length;
185 }
186
187 /* Read data, and (optionally) run it through the CRC. */
188 void /* PRIVATE */
189 png_crc_read(png_structp png_ptr, png_bytep buf, png_size_t length)
190 {
191 if (png_ptr == NULL)
192 return;
193
194 png_read_data(png_ptr, buf, length);
195 png_calculate_crc(png_ptr, buf, length);
196 }
197
198 /* Optionally skip data and then check the CRC. Depending on whether we
199 * are reading a ancillary or critical chunk, and how the program has set
200 * things up, we may calculate the CRC on the data and print a message.
201 * Returns '1' if there was a CRC error, '0' otherwise.
202 */
203 int /* PRIVATE */
204 png_crc_finish(png_structp png_ptr, png_uint_32 skip)
205 {
206 png_size_t i;
207 png_size_t istop = png_ptr->zbuf_size;
208
209 for (i = (png_size_t)skip; i > istop; i -= istop)
210 {
211 png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
212 }
213
214 if (i)
215 {
216 png_crc_read(png_ptr, png_ptr->zbuf, i);
217 }
218
219 if (png_crc_error(png_ptr))
220 {
221 if (((png_ptr->chunk_name[0] & 0x20) && /* Ancillary */
222 !(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)) ||
223 (!(png_ptr->chunk_name[0] & 0x20) && /* Critical */
224 (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE)))
225 {
226 png_chunk_warning(png_ptr, "CRC error");
227 }
228
229 else
230 {
231 png_chunk_benign_error(png_ptr, "CRC error");
232 return (0);
233 }
234
235 return (1);
236 }
237
238 return (0);
239 }
240
241 /* Compare the CRC stored in the PNG file with that calculated by libpng from
242 * the data it has read thus far.
243 */
244 int /* PRIVATE */
245 png_crc_error(png_structp png_ptr)
246 {
247 png_byte crc_bytes[4];
248 png_uint_32 crc;
249 int need_crc = 1;
250
251 if (png_ptr->chunk_name[0] & 0x20) /* ancillary */
252 {
253 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
254 (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
255 need_crc = 0;
256 }
257
258 else /* critical */
259 {
260 if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
261 need_crc = 0;
262 }
263
264 #ifdef PNG_IO_STATE_SUPPORTED
265 png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_CRC;
266 #endif
267
268 /* The chunk CRC must be serialized in a single I/O call. */
269 png_read_data(png_ptr, crc_bytes, 4);
270
271 if (need_crc)
272 {
273 crc = png_get_uint_32(crc_bytes);
274 return ((int)(crc != png_ptr->crc));
275 }
276
277 else
278 return (0);
279 }
280
281 #if defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) || \
282 defined(PNG_READ_iCCP_SUPPORTED)
283 static png_size_t
284 png_inflate(png_structp png_ptr, png_bytep data, png_size_t size,
285 png_bytep output, png_size_t output_size)
286 {
287 png_size_t count = 0;
288
289 /* zlib can't necessarily handle more than 65535 bytes at once (i.e. it can't
290 * even necessarily handle 65536 bytes) because the type uInt is "16 bits or
291 * more". Consequently it is necessary to chunk the input to zlib. This
292 * code uses ZLIB_IO_MAX, from pngpriv.h, as the maximum (the maximum value
293 * that can be stored in a uInt.) It is possible to set ZLIB_IO_MAX to a
294 * lower value in pngpriv.h and this may sometimes have a performance
295 * advantage, because it forces access of the input data to be separated from
296 * at least some of the use by some period of time.
297 */
298 png_ptr->zstream.next_in = data;
299 /* avail_in is set below from 'size' */
300 png_ptr->zstream.avail_in = 0;
301
302 while (1)
303 {
304 int ret, avail;
305
306 /* The setting of 'avail_in' used to be outside the loop, by setting it
307 * inside it is possible to chunk the input to zlib and simply rely on
308 * zlib to advance the 'next_in' pointer. This allows arbitrary amounts o
309 * data to be passed through zlib at the unavoidable cost of requiring a
310 * window save (memcpy of up to 32768 output bytes) every ZLIB_IO_MAX
311 * input bytes.
312 */
313 if (png_ptr->zstream.avail_in == 0 && size > 0)
314 {
315 if (size <= ZLIB_IO_MAX)
316 {
317 /* The value is less than ZLIB_IO_MAX so the cast is safe: */
318 png_ptr->zstream.avail_in = (uInt)size;
319 size = 0;
320 }
321
322 else
323 {
324 png_ptr->zstream.avail_in = ZLIB_IO_MAX;
325 size -= ZLIB_IO_MAX;
326 }
327 }
328
329 /* Reset the output buffer each time round - we empty it
330 * after every inflate call.
331 */
332 png_ptr->zstream.next_out = png_ptr->zbuf;
333 png_ptr->zstream.avail_out = png_ptr->zbuf_size;
334
335 ret = inflate(&png_ptr->zstream, Z_NO_FLUSH);
336 avail = png_ptr->zbuf_size - png_ptr->zstream.avail_out;
337
338 /* First copy/count any new output - but only if we didn't
339 * get an error code.
340 */
341 if ((ret == Z_OK || ret == Z_STREAM_END) && avail > 0)
342 {
343 png_size_t space = avail; /* > 0, see above */
344
345 if (output != 0 && output_size > count)
346 {
347 png_size_t copy = output_size - count;
348
349 if (space < copy)
350 copy = space;
351
352 png_memcpy(output + count, png_ptr->zbuf, copy);
353 }
354 count += space;
355 }
356
357 if (ret == Z_OK)
358 continue;
359
360 /* Termination conditions - always reset the zstream, it
361 * must be left in inflateInit state.
362 */
363 png_ptr->zstream.avail_in = 0;
364 inflateReset(&png_ptr->zstream);
365
366 if (ret == Z_STREAM_END)
367 return count; /* NOTE: may be zero. */
368
369 /* Now handle the error codes - the API always returns 0
370 * and the error message is dumped into the uncompressed
371 * buffer if available.
372 */
373 {
374 PNG_CONST char *msg;
375 #ifdef PNG_CONSOLE_IO_SUPPORTED
376 char umsg[52];
377 #endif
378 if (png_ptr->zstream.msg != 0)
379 msg = png_ptr->zstream.msg;
380
381 else
382 {
383 #ifdef PNG_CONSOLE_IO_SUPPORTED
384 switch (ret)
385 {
386 case Z_BUF_ERROR:
387 msg = "Buffer error in compressed datastream in %s chunk";
388 break;
389
390 case Z_DATA_ERROR:
391 msg = "Data error in compressed datastream in %s chunk";
392 break;
393
394 default:
395 msg = "Incomplete compressed datastream in %s chunk";
396 break;
397 }
398
399 png_snprintf(umsg, sizeof umsg, msg, png_ptr->chunk_name);
400 msg = umsg;
401 #else
402 msg = "Damaged compressed datastream in chunk other than IDAT";
403 #endif
404 }
405
406 png_warning(png_ptr, msg);
407 }
408
409 /* 0 means an error - notice that this code simply ignores
410 * zero length compressed chunks as a result.
411 */
412 return 0;
413 }
414 }
415
416 /*
417 * Decompress trailing data in a chunk. The assumption is that chunkdata
418 * points at an allocated area holding the contents of a chunk with a
419 * trailing compressed part. What we get back is an allocated area
420 * holding the original prefix part and an uncompressed version of the
421 * trailing part (the malloc area passed in is freed).
422 */
423 void /* PRIVATE */
424 png_decompress_chunk(png_structp png_ptr, int comp_type,
425 png_size_t chunklength,
426 png_size_t prefix_size, png_size_t *newlength)
427 {
428 /* The caller should guarantee this */
429 if (prefix_size > chunklength)
430 {
431 /* The recovery is to delete the chunk. */
432 png_warning(png_ptr, "invalid chunklength");
433 prefix_size = 0; /* To delete everything */
434 }
435
436 else if (comp_type == PNG_COMPRESSION_TYPE_BASE)
437 {
438 png_size_t expanded_size = png_inflate(png_ptr,
439 (png_bytep)(png_ptr->chunkdata + prefix_size),
440 chunklength - prefix_size,
441 0, /*output*/
442 0); /*output size*/
443
444 /* Now check the limits on this chunk - if the limit fails the
445 * compressed data will be removed, the prefix will remain.
446 */
447 #ifdef PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED
448 if (png_ptr->user_chunk_malloc_max &&
449 (prefix_size + expanded_size >= png_ptr->user_chunk_malloc_max - 1))
450 #else
451 # ifdef PNG_USER_CHUNK_MALLOC_MAX
452 if ((PNG_USER_CHUNK_MALLOC_MAX > 0) &&
453 prefix_size + expanded_size >= PNG_USER_CHUNK_MALLOC_MAX - 1)
454 # endif
455 #endif
456 png_warning(png_ptr, "Exceeded size limit while expanding chunk");
457
458 /* If the size is zero either there was an error and a message
459 * has already been output (warning) or the size really is zero
460 * and we have nothing to do - the code will exit through the
461 * error case below.
462 */
463 #if defined(PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED) || \
464 defined(PNG_USER_CHUNK_MALLOC_MAX)
465 else if (expanded_size > 0)
466 #else
467 if (expanded_size > 0)
468 #endif
469 {
470 /* Success (maybe) - really uncompress the chunk. */
471 png_size_t new_size = 0;
472 png_charp text = png_malloc_warn(png_ptr,
473 prefix_size + expanded_size + 1);
474
475 if (text != NULL)
476 {
477 png_memcpy(text, png_ptr->chunkdata, prefix_size);
478 new_size = png_inflate(png_ptr,
479 (png_bytep)(png_ptr->chunkdata + prefix_size),
480 chunklength - prefix_size,
481 (png_bytep)(text + prefix_size), expanded_size);
482 text[prefix_size + expanded_size] = 0; /* just in case */
483
484 if (new_size == expanded_size)
485 {
486 png_free(png_ptr, png_ptr->chunkdata);
487 png_ptr->chunkdata = text;
488 *newlength = prefix_size + expanded_size;
489 return; /* The success return! */
490 }
491
492 png_warning(png_ptr, "png_inflate logic error");
493 png_free(png_ptr, text);
494 }
495
496 else
497 png_warning(png_ptr, "Not enough memory to decompress chunk");
498 }
499 }
500
501 else /* if (comp_type != PNG_COMPRESSION_TYPE_BASE) */
502 {
503 #ifdef PNG_STDIO_SUPPORTED
504 char umsg[50];
505
506 png_snprintf(umsg, sizeof umsg,
507 "Unknown zTXt compression type %d", comp_type);
508 png_warning(png_ptr, umsg);
509 #else
510 png_warning(png_ptr, "Unknown zTXt compression type");
511 #endif
512
513 /* The recovery is to simply drop the data. */
514 }
515
516 /* Generic error return - leave the prefix, delete the compressed
517 * data, reallocate the chunkdata to remove the potentially large
518 * amount of compressed data.
519 */
520 {
521 png_charp text = png_malloc_warn(png_ptr, prefix_size + 1);
522
523 if (text != NULL)
524 {
525 if (prefix_size > 0)
526 png_memcpy(text, png_ptr->chunkdata, prefix_size);
527
528 png_free(png_ptr, png_ptr->chunkdata);
529 png_ptr->chunkdata = text;
530
531 /* This is an extra zero in the 'uncompressed' part. */
532 *(png_ptr->chunkdata + prefix_size) = 0x00;
533 }
534 /* Ignore a malloc error here - it is safe. */
535 }
536
537 *newlength = prefix_size;
538 }
539 #endif
540
541 /* Read and check the IDHR chunk */
542 void /* PRIVATE */
543 png_handle_IHDR(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
544 {
545 png_byte buf[13];
546 png_uint_32 width, height;
547 int bit_depth, color_type, compression_type, filter_type;
548 int interlace_type;
549
550 png_debug(1, "in png_handle_IHDR");
551
552 if (png_ptr->mode & PNG_HAVE_IHDR)
553 png_error(png_ptr, "Out of place IHDR");
554
555 /* Check the length */
556 if (length != 13)
557 png_error(png_ptr, "Invalid IHDR chunk");
558
559 png_ptr->mode |= PNG_HAVE_IHDR;
560
561 png_crc_read(png_ptr, buf, 13);
562 png_crc_finish(png_ptr, 0);
563
564 width = png_get_uint_31(png_ptr, buf);
565 height = png_get_uint_31(png_ptr, buf + 4);
566 bit_depth = buf[8];
567 color_type = buf[9];
568 compression_type = buf[10];
569 filter_type = buf[11];
570 interlace_type = buf[12];
571
572 /* Set internal variables */
573 png_ptr->width = width;
574 png_ptr->height = height;
575 png_ptr->bit_depth = (png_byte)bit_depth;
576 png_ptr->interlaced = (png_byte)interlace_type;
577 png_ptr->color_type = (png_byte)color_type;
578 #ifdef PNG_MNG_FEATURES_SUPPORTED
579 png_ptr->filter_type = (png_byte)filter_type;
580 #endif
581 png_ptr->compression_type = (png_byte)compression_type;
582
583 /* Find number of channels */
584 switch (png_ptr->color_type)
585 {
586 default: /* invalid, png_set_IHDR calls png_error */
587 case PNG_COLOR_TYPE_GRAY:
588 case PNG_COLOR_TYPE_PALETTE:
589 png_ptr->channels = 1;
590 break;
591
592 case PNG_COLOR_TYPE_RGB:
593 png_ptr->channels = 3;
594 break;
595
596 case PNG_COLOR_TYPE_GRAY_ALPHA:
597 png_ptr->channels = 2;
598 break;
599
600 case PNG_COLOR_TYPE_RGB_ALPHA:
601 png_ptr->channels = 4;
602 break;
603 }
604
605 /* Set up other useful info */
606 png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth *
607 png_ptr->channels);
608 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width);
609 png_debug1(3, "bit_depth = %d", png_ptr->bit_depth);
610 png_debug1(3, "channels = %d", png_ptr->channels);
611 png_debug1(3, "rowbytes = %lu", (unsigned long)png_ptr->rowbytes);
612 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
613 color_type, interlace_type, compression_type, filter_type);
614 }
615
616 /* Read and check the palette */
617 void /* PRIVATE */
618 png_handle_PLTE(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
619 {
620 png_color palette[PNG_MAX_PALETTE_LENGTH];
621 int num, i;
622 #ifdef PNG_POINTER_INDEXING_SUPPORTED
623 png_colorp pal_ptr;
624 #endif
625
626 png_debug(1, "in png_handle_PLTE");
627
628 if (!(png_ptr->mode & PNG_HAVE_IHDR))
629 png_error(png_ptr, "Missing IHDR before PLTE");
630
631 else if (png_ptr->mode & PNG_HAVE_IDAT)
632 {
633 png_warning(png_ptr, "Invalid PLTE after IDAT");
634 png_crc_finish(png_ptr, length);
635 return;
636 }
637
638 else if (png_ptr->mode & PNG_HAVE_PLTE)
639 png_error(png_ptr, "Duplicate PLTE chunk");
640
641 png_ptr->mode |= PNG_HAVE_PLTE;
642
643 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
644 {
645 png_warning(png_ptr,
646 "Ignoring PLTE chunk in grayscale PNG");
647 png_crc_finish(png_ptr, length);
648 return;
649 }
650
651 #ifndef PNG_READ_OPT_PLTE_SUPPORTED
652 if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
653 {
654 png_crc_finish(png_ptr, length);
655 return;
656 }
657 #endif
658
659 if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3)
660 {
661 if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
662 {
663 png_warning(png_ptr, "Invalid palette chunk");
664 png_crc_finish(png_ptr, length);
665 return;
666 }
667
668 else
669 {
670 png_error(png_ptr, "Invalid palette chunk");
671 }
672 }
673
674 num = (int)length / 3;
675
676 #ifdef PNG_POINTER_INDEXING_SUPPORTED
677 for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)
678 {
679 png_byte buf[3];
680
681 png_crc_read(png_ptr, buf, 3);
682 pal_ptr->red = buf[0];
683 pal_ptr->green = buf[1];
684 pal_ptr->blue = buf[2];
685 }
686 #else
687 for (i = 0; i < num; i++)
688 {
689 png_byte buf[3];
690
691 png_crc_read(png_ptr, buf, 3);
692 /* Don't depend upon png_color being any order */
693 palette[i].red = buf[0];
694 palette[i].green = buf[1];
695 palette[i].blue = buf[2];
696 }
697 #endif
698
699 /* If we actually need the PLTE chunk (ie for a paletted image), we do
700 * whatever the normal CRC configuration tells us. However, if we
701 * have an RGB image, the PLTE can be considered ancillary, so
702 * we will act as though it is.
703 */
704 #ifndef PNG_READ_OPT_PLTE_SUPPORTED
705 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
706 #endif
707 {
708 png_crc_finish(png_ptr, 0);
709 }
710
711 #ifndef PNG_READ_OPT_PLTE_SUPPORTED
712 else if (png_crc_error(png_ptr)) /* Only if we have a CRC error */
713 {
714 /* If we don't want to use the data from an ancillary chunk,
715 * we have two options: an error abort, or a warning and we
716 * ignore the data in this chunk (which should be OK, since
717 * it's considered ancillary for a RGB or RGBA image).
718 */
719 if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE))
720 {
721 if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)
722 {
723 png_chunk_benign_error(png_ptr, "CRC error");
724 }
725
726 else
727 {
728 png_chunk_warning(png_ptr, "CRC error");
729 return;
730 }
731 }
732
733 /* Otherwise, we (optionally) emit a warning and use the chunk. */
734 else if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN))
735 {
736 png_chunk_warning(png_ptr, "CRC error");
737 }
738 }
739 #endif
740
741 png_set_PLTE(png_ptr, info_ptr, palette, num);
742
743 #ifdef PNG_READ_tRNS_SUPPORTED
744 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
745 {
746 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
747 {
748 if (png_ptr->num_trans > (png_uint_16)num)
749 {
750 png_warning(png_ptr, "Truncating incorrect tRNS chunk length");
751 png_ptr->num_trans = (png_uint_16)num;
752 }
753
754 if (info_ptr->num_trans > (png_uint_16)num)
755 {
756 png_warning(png_ptr, "Truncating incorrect info tRNS chunk length");
757 info_ptr->num_trans = (png_uint_16)num;
758 }
759 }
760 }
761 #endif
762
763 }
764
765 void /* PRIVATE */
766 png_handle_IEND(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
767 {
768 png_debug(1, "in png_handle_IEND");
769
770 if (!(png_ptr->mode & PNG_HAVE_IHDR) || !(png_ptr->mode & PNG_HAVE_IDAT))
771 {
772 png_error(png_ptr, "No image in file");
773 }
774
775 png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND);
776
777 if (length != 0)
778 {
779 png_warning(png_ptr, "Incorrect IEND chunk length");
780 }
781
782 png_crc_finish(png_ptr, length);
783
784 PNG_UNUSED(info_ptr) /* Quiet compiler warnings about unused info_ptr */
785 }
786
787 #ifdef PNG_READ_gAMA_SUPPORTED
788 void /* PRIVATE */
789 png_handle_gAMA(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
790 {
791 png_fixed_point igamma;
792 png_byte buf[4];
793
794 png_debug(1, "in png_handle_gAMA");
795
796 if (!(png_ptr->mode & PNG_HAVE_IHDR))
797 png_error(png_ptr, "Missing IHDR before gAMA");
798
799 else if (png_ptr->mode & PNG_HAVE_IDAT)
800 {
801 png_warning(png_ptr, "Invalid gAMA after IDAT");
802 png_crc_finish(png_ptr, length);
803 return;
804 }
805
806 else if (png_ptr->mode & PNG_HAVE_PLTE)
807 /* Should be an error, but we can cope with it */
808 png_warning(png_ptr, "Out of place gAMA chunk");
809
810 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA)
811 #ifdef PNG_READ_sRGB_SUPPORTED
812 && !(info_ptr->valid & PNG_INFO_sRGB)
813 #endif
814 )
815 {
816 png_warning(png_ptr, "Duplicate gAMA chunk");
817 png_crc_finish(png_ptr, length);
818 return;
819 }
820
821 if (length != 4)
822 {
823 png_warning(png_ptr, "Incorrect gAMA chunk length");
824 png_crc_finish(png_ptr, length);
825 return;
826 }
827
828 png_crc_read(png_ptr, buf, 4);
829
830 if (png_crc_finish(png_ptr, 0))
831 return;
832
833 igamma = png_get_fixed_point(NULL, buf);
834
835 /* Check for zero gamma or an error. */
836 if (igamma <= 0)
837 {
838 png_warning(png_ptr,
839 "Ignoring gAMA chunk with out of range gamma");
840
841 return;
842 }
843
844 # ifdef PNG_READ_sRGB_SUPPORTED
845 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB))
846 {
847 if (PNG_OUT_OF_RANGE(igamma, 45500L, 500))
848 {
849 png_warning(png_ptr,
850 "Ignoring incorrect gAMA value when sRGB is also present");
851
852 # ifdef PNG_CONSOLE_IO_SUPPORTED
853 fprintf(stderr, "gamma = (%d/100000)", (int)igamma);
854 # endif
855 return;
856 }
857 }
858 # endif /* PNG_READ_sRGB_SUPPORTED */
859
860 # ifdef PNG_READ_GAMMA_SUPPORTED
861 /* Gamma correction on read is supported. */
862 png_ptr->gamma = igamma;
863 # endif
864 /* And set the 'info' structure members. */
865 png_set_gAMA_fixed(png_ptr, info_ptr, igamma);
866 }
867 #endif
868
869 #ifdef PNG_READ_sBIT_SUPPORTED
870 void /* PRIVATE */
871 png_handle_sBIT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
872 {
873 png_size_t truelen;
874 png_byte buf[4];
875
876 png_debug(1, "in png_handle_sBIT");
877
878 buf[0] = buf[1] = buf[2] = buf[3] = 0;
879
880 if (!(png_ptr->mode & PNG_HAVE_IHDR))
881 png_error(png_ptr, "Missing IHDR before sBIT");
882
883 else if (png_ptr->mode & PNG_HAVE_IDAT)
884 {
885 png_warning(png_ptr, "Invalid sBIT after IDAT");
886 png_crc_finish(png_ptr, length);
887 return;
888 }
889
890 else if (png_ptr->mode & PNG_HAVE_PLTE)
891 {
892 /* Should be an error, but we can cope with it */
893 png_warning(png_ptr, "Out of place sBIT chunk");
894 }
895
896 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT))
897 {
898 png_warning(png_ptr, "Duplicate sBIT chunk");
899 png_crc_finish(png_ptr, length);
900 return;
901 }
902
903 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
904 truelen = 3;
905
906 else
907 truelen = (png_size_t)png_ptr->channels;
908
909 if (length != truelen || length > 4)
910 {
911 png_warning(png_ptr, "Incorrect sBIT chunk length");
912 png_crc_finish(png_ptr, length);
913 return;
914 }
915
916 png_crc_read(png_ptr, buf, truelen);
917
918 if (png_crc_finish(png_ptr, 0))
919 return;
920
921 if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
922 {
923 png_ptr->sig_bit.red = buf[0];
924 png_ptr->sig_bit.green = buf[1];
925 png_ptr->sig_bit.blue = buf[2];
926 png_ptr->sig_bit.alpha = buf[3];
927 }
928
929 else
930 {
931 png_ptr->sig_bit.gray = buf[0];
932 png_ptr->sig_bit.red = buf[0];
933 png_ptr->sig_bit.green = buf[0];
934 png_ptr->sig_bit.blue = buf[0];
935 png_ptr->sig_bit.alpha = buf[1];
936 }
937
938 png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
939 }
940 #endif
941
942 #ifdef PNG_READ_cHRM_SUPPORTED
943 void /* PRIVATE */
944 png_handle_cHRM(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
945 {
946 png_byte buf[32];
947 png_fixed_point x_white, y_white, x_red, y_red, x_green, y_green, x_blue,
948 y_blue;
949
950 png_debug(1, "in png_handle_cHRM");
951
952 if (!(png_ptr->mode & PNG_HAVE_IHDR))
953 png_error(png_ptr, "Missing IHDR before cHRM");
954
955 else if (png_ptr->mode & PNG_HAVE_IDAT)
956 {
957 png_warning(png_ptr, "Invalid cHRM after IDAT");
958 png_crc_finish(png_ptr, length);
959 return;
960 }
961
962 else if (png_ptr->mode & PNG_HAVE_PLTE)
963 /* Should be an error, but we can cope with it */
964 png_warning(png_ptr, "Missing PLTE before cHRM");
965
966 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM)
967 # ifdef PNG_READ_sRGB_SUPPORTED
968 && !(info_ptr->valid & PNG_INFO_sRGB)
969 # endif
970 )
971 {
972 png_warning(png_ptr, "Duplicate cHRM chunk");
973 png_crc_finish(png_ptr, length);
974 return;
975 }
976
977 if (length != 32)
978 {
979 png_warning(png_ptr, "Incorrect cHRM chunk length");
980 png_crc_finish(png_ptr, length);
981 return;
982 }
983
984 png_crc_read(png_ptr, buf, 32);
985
986 if (png_crc_finish(png_ptr, 0))
987 return;
988
989 x_white = png_get_fixed_point(NULL, buf);
990 y_white = png_get_fixed_point(NULL, buf + 4);
991 x_red = png_get_fixed_point(NULL, buf + 8);
992 y_red = png_get_fixed_point(NULL, buf + 12);
993 x_green = png_get_fixed_point(NULL, buf + 16);
994 y_green = png_get_fixed_point(NULL, buf + 20);
995 x_blue = png_get_fixed_point(NULL, buf + 24);
996 y_blue = png_get_fixed_point(NULL, buf + 28);
997
998 if (x_white == PNG_FIXED_ERROR ||
999 y_white == PNG_FIXED_ERROR ||
1000 x_red == PNG_FIXED_ERROR ||
1001 y_red == PNG_FIXED_ERROR ||
1002 x_green == PNG_FIXED_ERROR ||
1003 y_green == PNG_FIXED_ERROR ||
1004 x_blue == PNG_FIXED_ERROR ||
1005 y_blue == PNG_FIXED_ERROR)
1006 {
1007 png_warning(png_ptr, "Ignoring cHRM chunk with negative chromaticities");
1008 return;
1009 }
1010
1011 #ifdef PNG_READ_sRGB_SUPPORTED
1012 if ((info_ptr != NULL) && (info_ptr->valid & PNG_INFO_sRGB))
1013 {
1014 if (PNG_OUT_OF_RANGE(x_white, 31270, 1000) ||
1015 PNG_OUT_OF_RANGE(y_white, 32900, 1000) ||
1016 PNG_OUT_OF_RANGE(x_red, 64000L, 1000) ||
1017 PNG_OUT_OF_RANGE(y_red, 33000, 1000) ||
1018 PNG_OUT_OF_RANGE(x_green, 30000, 1000) ||
1019 PNG_OUT_OF_RANGE(y_green, 60000L, 1000) ||
1020 PNG_OUT_OF_RANGE(x_blue, 15000, 1000) ||
1021 PNG_OUT_OF_RANGE(y_blue, 6000, 1000))
1022 {
1023 png_warning(png_ptr,
1024 "Ignoring incorrect cHRM value when sRGB is also present");
1025
1026 #ifdef PNG_CONSOLE_IO_SUPPORTED
1027 fprintf(stderr, "wx=%d, wy=%d, rx=%d, ry=%d\n",
1028 x_white, y_white, x_red, y_red);
1029
1030 fprintf(stderr, "gx=%d, gy=%d, bx=%d, by=%d\n",
1031 x_green, y_green, x_blue, y_blue);
1032 #endif /* PNG_CONSOLE_IO_SUPPORTED */
1033 }
1034 return;
1035 }
1036 #endif /* PNG_READ_sRGB_SUPPORTED */
1037
1038 png_set_cHRM_fixed(png_ptr, info_ptr, x_white, y_white, x_red, y_red,
1039 x_green, y_green, x_blue, y_blue);
1040 }
1041 #endif
1042
1043 #ifdef PNG_READ_sRGB_SUPPORTED
1044 void /* PRIVATE */
1045 png_handle_sRGB(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1046 {
1047 int intent;
1048 png_byte buf[1];
1049
1050 png_debug(1, "in png_handle_sRGB");
1051
1052 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1053 png_error(png_ptr, "Missing IHDR before sRGB");
1054
1055 else if (png_ptr->mode & PNG_HAVE_IDAT)
1056 {
1057 png_warning(png_ptr, "Invalid sRGB after IDAT");
1058 png_crc_finish(png_ptr, length);
1059 return;
1060 }
1061
1062 else if (png_ptr->mode & PNG_HAVE_PLTE)
1063 /* Should be an error, but we can cope with it */
1064 png_warning(png_ptr, "Out of place sRGB chunk");
1065
1066 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB))
1067 {
1068 png_warning(png_ptr, "Duplicate sRGB chunk");
1069 png_crc_finish(png_ptr, length);
1070 return;
1071 }
1072
1073 if (length != 1)
1074 {
1075 png_warning(png_ptr, "Incorrect sRGB chunk length");
1076 png_crc_finish(png_ptr, length);
1077 return;
1078 }
1079
1080 png_crc_read(png_ptr, buf, 1);
1081
1082 if (png_crc_finish(png_ptr, 0))
1083 return;
1084
1085 intent = buf[0];
1086
1087 /* Check for bad intent */
1088 if (intent >= PNG_sRGB_INTENT_LAST)
1089 {
1090 png_warning(png_ptr, "Unknown sRGB intent");
1091 return;
1092 }
1093
1094 #if defined(PNG_READ_gAMA_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED)
1095 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA))
1096 {
1097 if (PNG_OUT_OF_RANGE(info_ptr->gamma, 45500L, 500))
1098 {
1099 png_warning(png_ptr,
1100 "Ignoring incorrect gAMA value when sRGB is also present");
1101 #ifdef PNG_CONSOLE_IO_SUPPORTED
1102 fprintf(stderr, "incorrect gamma=(%d/100000)\n", info_ptr->gamma);
1103 #endif
1104 }
1105 }
1106 #endif /* PNG_READ_gAMA_SUPPORTED */
1107
1108 #ifdef PNG_READ_cHRM_SUPPORTED
1109 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM))
1110 if (PNG_OUT_OF_RANGE(info_ptr->x_white, 31270, 1000) ||
1111 PNG_OUT_OF_RANGE(info_ptr->y_white, 32900, 1000) ||
1112 PNG_OUT_OF_RANGE(info_ptr->x_red, 64000L, 1000) ||
1113 PNG_OUT_OF_RANGE(info_ptr->y_red, 33000, 1000) ||
1114 PNG_OUT_OF_RANGE(info_ptr->x_green, 30000, 1000) ||
1115 PNG_OUT_OF_RANGE(info_ptr->y_green, 60000L, 1000) ||
1116 PNG_OUT_OF_RANGE(info_ptr->x_blue, 15000, 1000) ||
1117 PNG_OUT_OF_RANGE(info_ptr->y_blue, 6000, 1000))
1118 {
1119 png_warning(png_ptr,
1120 "Ignoring incorrect cHRM value when sRGB is also present");
1121 }
1122 #endif /* PNG_READ_cHRM_SUPPORTED */
1123
1124 png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, intent);
1125 }
1126 #endif /* PNG_READ_sRGB_SUPPORTED */
1127
1128 #ifdef PNG_READ_iCCP_SUPPORTED
1129 void /* PRIVATE */
1130 png_handle_iCCP(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1131 /* Note: this does not properly handle chunks that are > 64K under DOS */
1132 {
1133 png_byte compression_type;
1134 png_bytep pC;
1135 png_charp profile;
1136 png_uint_32 skip = 0;
1137 png_uint_32 profile_size;
1138 png_alloc_size_t profile_length;
1139 png_size_t slength, prefix_length, data_length;
1140
1141 png_debug(1, "in png_handle_iCCP");
1142
1143 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1144 png_error(png_ptr, "Missing IHDR before iCCP");
1145
1146 else if (png_ptr->mode & PNG_HAVE_IDAT)
1147 {
1148 png_warning(png_ptr, "Invalid iCCP after IDAT");
1149 png_crc_finish(png_ptr, length);
1150 return;
1151 }
1152
1153 else if (png_ptr->mode & PNG_HAVE_PLTE)
1154 /* Should be an error, but we can cope with it */
1155 png_warning(png_ptr, "Out of place iCCP chunk");
1156
1157 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_iCCP))
1158 {
1159 png_warning(png_ptr, "Duplicate iCCP chunk");
1160 png_crc_finish(png_ptr, length);
1161 return;
1162 }
1163
1164 #ifdef PNG_MAX_MALLOC_64K
1165 if (length > (png_uint_32)65535L)
1166 {
1167 png_warning(png_ptr, "iCCP chunk too large to fit in memory");
1168 skip = length - (png_uint_32)65535L;
1169 length = (png_uint_32)65535L;
1170 }
1171 #endif
1172
1173 png_free(png_ptr, png_ptr->chunkdata);
1174 png_ptr->chunkdata = (png_charp)png_malloc(png_ptr, length + 1);
1175 slength = (png_size_t)length;
1176 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
1177
1178 if (png_crc_finish(png_ptr, skip))
1179 {
1180 png_free(png_ptr, png_ptr->chunkdata);
1181 png_ptr->chunkdata = NULL;
1182 return;
1183 }
1184
1185 png_ptr->chunkdata[slength] = 0x00;
1186
1187 for (profile = png_ptr->chunkdata; *profile; profile++)
1188 /* Empty loop to find end of name */ ;
1189
1190 ++profile;
1191
1192 /* There should be at least one zero (the compression type byte)
1193 * following the separator, and we should be on it
1194 */
1195 if (profile >= png_ptr->chunkdata + slength - 1)
1196 {
1197 png_free(png_ptr, png_ptr->chunkdata);
1198 png_ptr->chunkdata = NULL;
1199 png_warning(png_ptr, "Malformed iCCP chunk");
1200 return;
1201 }
1202
1203 /* Compression_type should always be zero */
1204 compression_type = *profile++;
1205
1206 if (compression_type)
1207 {
1208 png_warning(png_ptr, "Ignoring nonzero compression type in iCCP chunk");
1209 compression_type = 0x00; /* Reset it to zero (libpng-1.0.6 through 1.0.8
1210 wrote nonzero) */
1211 }
1212
1213 prefix_length = profile - png_ptr->chunkdata;
1214 png_decompress_chunk(png_ptr, compression_type,
1215 slength, prefix_length, &data_length);
1216
1217 profile_length = data_length - prefix_length;
1218
1219 if (prefix_length > data_length || profile_length < 4)
1220 {
1221 png_free(png_ptr, png_ptr->chunkdata);
1222 png_ptr->chunkdata = NULL;
1223 png_warning(png_ptr, "Profile size field missing from iCCP chunk");
1224 return;
1225 }
1226
1227 /* Check the profile_size recorded in the first 32 bits of the ICC profile */
1228 pC = (png_bytep)(png_ptr->chunkdata + prefix_length);
1229 profile_size = ((*(pC )) << 24) |
1230 ((*(pC + 1)) << 16) |
1231 ((*(pC + 2)) << 8) |
1232 ((*(pC + 3)) );
1233
1234 /* NOTE: the following guarantees that 'profile_length' fits into 32 bits,
1235 * because profile_size is a 32 bit value.
1236 */
1237 if (profile_size < profile_length)
1238 profile_length = profile_size;
1239
1240 /* And the following guarantees that profile_size == profile_length. */
1241 if (profile_size > profile_length)
1242 {
1243 png_free(png_ptr, png_ptr->chunkdata);
1244 png_ptr->chunkdata = NULL;
1245 #ifdef PNG_STDIO_SUPPORTED
1246 {
1247 char umsg[80];
1248
1249 png_snprintf2(umsg, 80,
1250 "Ignoring iCCP chunk with declared size = %u "
1251 "and actual length = %u",
1252 (unsigned int) profile_size,
1253 (unsigned int) profile_length);
1254 png_warning(png_ptr, umsg);
1255 }
1256 #else
1257 png_warning(png_ptr,
1258 "Ignoring iCCP chunk with uncompressed size mismatch");
1259 #endif
1260 return;
1261 }
1262
1263 png_set_iCCP(png_ptr, info_ptr, png_ptr->chunkdata,
1264 compression_type, (png_bytep)png_ptr->chunkdata + prefix_length,
1265 profile_size);
1266 png_free(png_ptr, png_ptr->chunkdata);
1267 png_ptr->chunkdata = NULL;
1268 }
1269 #endif /* PNG_READ_iCCP_SUPPORTED */
1270
1271 #ifdef PNG_READ_sPLT_SUPPORTED
1272 void /* PRIVATE */
1273 png_handle_sPLT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1274 /* Note: this does not properly handle chunks that are > 64K under DOS */
1275 {
1276 png_bytep entry_start;
1277 png_sPLT_t new_palette;
1278 png_sPLT_entryp pp;
1279 png_uint_32 data_length;
1280 int entry_size, i;
1281 png_uint_32 skip = 0;
1282 png_size_t slength;
1283 png_uint_32 dl;
1284 png_size_t max_dl;
1285
1286 png_debug(1, "in png_handle_sPLT");
1287
1288 #ifdef PNG_USER_LIMITS_SUPPORTED
1289
1290 if (png_ptr->user_chunk_cache_max != 0)
1291 {
1292 if (png_ptr->user_chunk_cache_max == 1)
1293 {
1294 png_crc_finish(png_ptr, length);
1295 return;
1296 }
1297
1298 if (--png_ptr->user_chunk_cache_max == 1)
1299 {
1300 png_warning(png_ptr, "No space in chunk cache for sPLT");
1301 png_crc_finish(png_ptr, length);
1302 return;
1303 }
1304 }
1305 #endif
1306
1307 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1308 png_error(png_ptr, "Missing IHDR before sPLT");
1309
1310 else if (png_ptr->mode & PNG_HAVE_IDAT)
1311 {
1312 png_warning(png_ptr, "Invalid sPLT after IDAT");
1313 png_crc_finish(png_ptr, length);
1314 return;
1315 }
1316
1317 #ifdef PNG_MAX_MALLOC_64K
1318 if (length > (png_uint_32)65535L)
1319 {
1320 png_warning(png_ptr, "sPLT chunk too large to fit in memory");
1321 skip = length - (png_uint_32)65535L;
1322 length = (png_uint_32)65535L;
1323 }
1324 #endif
1325
1326 png_free(png_ptr, png_ptr->chunkdata);
1327 png_ptr->chunkdata = (png_charp)png_malloc(png_ptr, length + 1);
1328
1329 /* WARNING: this may break if size_t is less than 32 bits; it is assumed
1330 * that the PNG_MAX_MALLOC_64K test is enabled in this case, but this is a
1331 * potential breakage point if the types in pngconf.h aren't exactly right.
1332 */
1333 slength = (png_size_t)length;
1334 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
1335
1336 if (png_crc_finish(png_ptr, skip))
1337 {
1338 png_free(png_ptr, png_ptr->chunkdata);
1339 png_ptr->chunkdata = NULL;
1340 return;
1341 }
1342
1343 png_ptr->chunkdata[slength] = 0x00;
1344
1345 for (entry_start = (png_bytep)png_ptr->chunkdata; *entry_start;
1346 entry_start++)
1347 /* Empty loop to find end of name */ ;
1348
1349 ++entry_start;
1350
1351 /* A sample depth should follow the separator, and we should be on it */
1352 if (entry_start > (png_bytep)png_ptr->chunkdata + slength - 2)
1353 {
1354 png_free(png_ptr, png_ptr->chunkdata);
1355 png_ptr->chunkdata = NULL;
1356 png_warning(png_ptr, "malformed sPLT chunk");
1357 return;
1358 }
1359
1360 new_palette.depth = *entry_start++;
1361 entry_size = (new_palette.depth == 8 ? 6 : 10);
1362 /* This must fit in a png_uint_32 because it is derived from the original
1363 * chunk data length (and use 'length', not 'slength' here for clarity -
1364 * they are guaranteed to be the same, see the tests above.)
1365 */
1366 data_length = length - (png_uint_32)(entry_start -
1367 (png_bytep)png_ptr->chunkdata);
1368
1369 /* Integrity-check the data length */
1370 if (data_length % entry_size)
1371 {
1372 png_free(png_ptr, png_ptr->chunkdata);
1373 png_ptr->chunkdata = NULL;
1374 png_warning(png_ptr, "sPLT chunk has bad length");
1375 return;
1376 }
1377
1378 dl = (png_int_32)(data_length / entry_size);
1379 max_dl = PNG_SIZE_MAX / png_sizeof(png_sPLT_entry);
1380
1381 if (dl > max_dl)
1382 {
1383 png_warning(png_ptr, "sPLT chunk too long");
1384 return;
1385 }
1386
1387 new_palette.nentries = (png_int_32)(data_length / entry_size);
1388
1389 new_palette.entries = (png_sPLT_entryp)png_malloc_warn(
1390 png_ptr, new_palette.nentries * png_sizeof(png_sPLT_entry));
1391
1392 if (new_palette.entries == NULL)
1393 {
1394 png_warning(png_ptr, "sPLT chunk requires too much memory");
1395 return;
1396 }
1397
1398 #ifdef PNG_POINTER_INDEXING_SUPPORTED
1399 for (i = 0; i < new_palette.nentries; i++)
1400 {
1401 pp = new_palette.entries + i;
1402
1403 if (new_palette.depth == 8)
1404 {
1405 pp->red = *entry_start++;
1406 pp->green = *entry_start++;
1407 pp->blue = *entry_start++;
1408 pp->alpha = *entry_start++;
1409 }
1410
1411 else
1412 {
1413 pp->red = png_get_uint_16(entry_start); entry_start += 2;
1414 pp->green = png_get_uint_16(entry_start); entry_start += 2;
1415 pp->blue = png_get_uint_16(entry_start); entry_start += 2;
1416 pp->alpha = png_get_uint_16(entry_start); entry_start += 2;
1417 }
1418
1419 pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
1420 }
1421 #else
1422 pp = new_palette.entries;
1423
1424 for (i = 0; i < new_palette.nentries; i++)
1425 {
1426
1427 if (new_palette.depth == 8)
1428 {
1429 pp[i].red = *entry_start++;
1430 pp[i].green = *entry_start++;
1431 pp[i].blue = *entry_start++;
1432 pp[i].alpha = *entry_start++;
1433 }
1434
1435 else
1436 {
1437 pp[i].red = png_get_uint_16(entry_start); entry_start += 2;
1438 pp[i].green = png_get_uint_16(entry_start); entry_start += 2;
1439 pp[i].blue = png_get_uint_16(entry_start); entry_start += 2;
1440 pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2;
1441 }
1442
1443 pp[i].frequency = png_get_uint_16(entry_start); entry_start += 2;
1444 }
1445 #endif
1446
1447 /* Discard all chunk data except the name and stash that */
1448 new_palette.name = png_ptr->chunkdata;
1449
1450 png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);
1451
1452 png_free(png_ptr, png_ptr->chunkdata);
1453 png_ptr->chunkdata = NULL;
1454 png_free(png_ptr, new_palette.entries);
1455 }
1456 #endif /* PNG_READ_sPLT_SUPPORTED */
1457
1458 #ifdef PNG_READ_tRNS_SUPPORTED
1459 void /* PRIVATE */
1460 png_handle_tRNS(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1461 {
1462 png_byte readbuf[PNG_MAX_PALETTE_LENGTH];
1463
1464 png_debug(1, "in png_handle_tRNS");
1465
1466 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1467 png_error(png_ptr, "Missing IHDR before tRNS");
1468
1469 else if (png_ptr->mode & PNG_HAVE_IDAT)
1470 {
1471 png_warning(png_ptr, "Invalid tRNS after IDAT");
1472 png_crc_finish(png_ptr, length);
1473 return;
1474 }
1475
1476 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
1477 {
1478 png_warning(png_ptr, "Duplicate tRNS chunk");
1479 png_crc_finish(png_ptr, length);
1480 return;
1481 }
1482
1483 if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
1484 {
1485 png_byte buf[2];
1486
1487 if (length != 2)
1488 {
1489 png_warning(png_ptr, "Incorrect tRNS chunk length");
1490 png_crc_finish(png_ptr, length);
1491 return;
1492 }
1493
1494 png_crc_read(png_ptr, buf, 2);
1495 png_ptr->num_trans = 1;
1496 png_ptr->trans_color.gray = png_get_uint_16(buf);
1497 }
1498
1499 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
1500 {
1501 png_byte buf[6];
1502
1503 if (length != 6)
1504 {
1505 png_warning(png_ptr, "Incorrect tRNS chunk length");
1506 png_crc_finish(png_ptr, length);
1507 return;
1508 }
1509
1510 png_crc_read(png_ptr, buf, (png_size_t)length);
1511 png_ptr->num_trans = 1;
1512 png_ptr->trans_color.red = png_get_uint_16(buf);
1513 png_ptr->trans_color.green = png_get_uint_16(buf + 2);
1514 png_ptr->trans_color.blue = png_get_uint_16(buf + 4);
1515 }
1516
1517 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1518 {
1519 if (!(png_ptr->mode & PNG_HAVE_PLTE))
1520 {
1521 /* Should be an error, but we can cope with it. */
1522 png_warning(png_ptr, "Missing PLTE before tRNS");
1523 }
1524
1525 if (length > (png_uint_32)png_ptr->num_palette ||
1526 length > PNG_MAX_PALETTE_LENGTH)
1527 {
1528 png_warning(png_ptr, "Incorrect tRNS chunk length");
1529 png_crc_finish(png_ptr, length);
1530 return;
1531 }
1532
1533 if (length == 0)
1534 {
1535 png_warning(png_ptr, "Zero length tRNS chunk");
1536 png_crc_finish(png_ptr, length);
1537 return;
1538 }
1539
1540 png_crc_read(png_ptr, readbuf, (png_size_t)length);
1541 png_ptr->num_trans = (png_uint_16)length;
1542 }
1543
1544 else
1545 {
1546 png_warning(png_ptr, "tRNS chunk not allowed with alpha channel");
1547 png_crc_finish(png_ptr, length);
1548 return;
1549 }
1550
1551 if (png_crc_finish(png_ptr, 0))
1552 {
1553 png_ptr->num_trans = 0;
1554 return;
1555 }
1556
1557 png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,
1558 &(png_ptr->trans_color));
1559 }
1560 #endif
1561
1562 #ifdef PNG_READ_bKGD_SUPPORTED
1563 void /* PRIVATE */
1564 png_handle_bKGD(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1565 {
1566 png_size_t truelen;
1567 png_byte buf[6];
1568
1569 png_debug(1, "in png_handle_bKGD");
1570
1571 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1572 png_error(png_ptr, "Missing IHDR before bKGD");
1573
1574 else if (png_ptr->mode & PNG_HAVE_IDAT)
1575 {
1576 png_warning(png_ptr, "Invalid bKGD after IDAT");
1577 png_crc_finish(png_ptr, length);
1578 return;
1579 }
1580
1581 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
1582 !(png_ptr->mode & PNG_HAVE_PLTE))
1583 {
1584 png_warning(png_ptr, "Missing PLTE before bKGD");
1585 png_crc_finish(png_ptr, length);
1586 return;
1587 }
1588
1589 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD))
1590 {
1591 png_warning(png_ptr, "Duplicate bKGD chunk");
1592 png_crc_finish(png_ptr, length);
1593 return;
1594 }
1595
1596 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1597 truelen = 1;
1598
1599 else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
1600 truelen = 6;
1601
1602 else
1603 truelen = 2;
1604
1605 if (length != truelen)
1606 {
1607 png_warning(png_ptr, "Incorrect bKGD chunk length");
1608 png_crc_finish(png_ptr, length);
1609 return;
1610 }
1611
1612 png_crc_read(png_ptr, buf, truelen);
1613
1614 if (png_crc_finish(png_ptr, 0))
1615 return;
1616
1617 /* We convert the index value into RGB components so that we can allow
1618 * arbitrary RGB values for background when we have transparency, and
1619 * so it is easy to determine the RGB values of the background color
1620 * from the info_ptr struct.
1621 */
1622 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1623 {
1624 png_ptr->background.index = buf[0];
1625
1626 if (info_ptr && info_ptr->num_palette)
1627 {
1628 if (buf[0] >= info_ptr->num_palette)
1629 {
1630 png_warning(png_ptr, "Incorrect bKGD chunk index value");
1631 return;
1632 }
1633
1634 png_ptr->background.red =
1635 (png_uint_16)png_ptr->palette[buf[0]].red;
1636
1637 png_ptr->background.green =
1638 (png_uint_16)png_ptr->palette[buf[0]].green;
1639
1640 png_ptr->background.blue =
1641 (png_uint_16)png_ptr->palette[buf[0]].blue;
1642 }
1643 }
1644
1645 else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) /* GRAY */
1646 {
1647 png_ptr->background.red =
1648 png_ptr->background.green =
1649 png_ptr->background.blue =
1650 png_ptr->background.gray = png_get_uint_16(buf);
1651 }
1652
1653 else
1654 {
1655 png_ptr->background.red = png_get_uint_16(buf);
1656 png_ptr->background.green = png_get_uint_16(buf + 2);
1657 png_ptr->background.blue = png_get_uint_16(buf + 4);
1658 }
1659
1660 png_set_bKGD(png_ptr, info_ptr, &(png_ptr->background));
1661 }
1662 #endif
1663
1664 #ifdef PNG_READ_hIST_SUPPORTED
1665 void /* PRIVATE */
1666 png_handle_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1667 {
1668 unsigned int num, i;
1669 png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];
1670
1671 png_debug(1, "in png_handle_hIST");
1672
1673 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1674 png_error(png_ptr, "Missing IHDR before hIST");
1675
1676 else if (png_ptr->mode & PNG_HAVE_IDAT)
1677 {
1678 png_warning(png_ptr, "Invalid hIST after IDAT");
1679 png_crc_finish(png_ptr, length);
1680 return;
1681 }
1682
1683 else if (!(png_ptr->mode & PNG_HAVE_PLTE))
1684 {
1685 png_warning(png_ptr, "Missing PLTE before hIST");
1686 png_crc_finish(png_ptr, length);
1687 return;
1688 }
1689
1690 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST))
1691 {
1692 png_warning(png_ptr, "Duplicate hIST chunk");
1693 png_crc_finish(png_ptr, length);
1694 return;
1695 }
1696
1697 num = length / 2 ;
1698
1699 if (num != (unsigned int)png_ptr->num_palette || num >
1700 (unsigned int)PNG_MAX_PALETTE_LENGTH)
1701 {
1702 png_warning(png_ptr, "Incorrect hIST chunk length");
1703 png_crc_finish(png_ptr, length);
1704 return;
1705 }
1706
1707 for (i = 0; i < num; i++)
1708 {
1709 png_byte buf[2];
1710
1711 png_crc_read(png_ptr, buf, 2);
1712 readbuf[i] = png_get_uint_16(buf);
1713 }
1714
1715 if (png_crc_finish(png_ptr, 0))
1716 return;
1717
1718 png_set_hIST(png_ptr, info_ptr, readbuf);
1719 }
1720 #endif
1721
1722 #ifdef PNG_READ_pHYs_SUPPORTED
1723 void /* PRIVATE */
1724 png_handle_pHYs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1725 {
1726 png_byte buf[9];
1727 png_uint_32 res_x, res_y;
1728 int unit_type;
1729
1730 png_debug(1, "in png_handle_pHYs");
1731
1732 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1733 png_error(png_ptr, "Missing IHDR before pHYs");
1734
1735 else if (png_ptr->mode & PNG_HAVE_IDAT)
1736 {
1737 png_warning(png_ptr, "Invalid pHYs after IDAT");
1738 png_crc_finish(png_ptr, length);
1739 return;
1740 }
1741
1742 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
1743 {
1744 png_warning(png_ptr, "Duplicate pHYs chunk");
1745 png_crc_finish(png_ptr, length);
1746 return;
1747 }
1748
1749 if (length != 9)
1750 {
1751 png_warning(png_ptr, "Incorrect pHYs chunk length");
1752 png_crc_finish(png_ptr, length);
1753 return;
1754 }
1755
1756 png_crc_read(png_ptr, buf, 9);
1757
1758 if (png_crc_finish(png_ptr, 0))
1759 return;
1760
1761 res_x = png_get_uint_32(buf);
1762 res_y = png_get_uint_32(buf + 4);
1763 unit_type = buf[8];
1764 png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
1765 }
1766 #endif
1767
1768 #ifdef PNG_READ_oFFs_SUPPORTED
1769 void /* PRIVATE */
1770 png_handle_oFFs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1771 {
1772 png_byte buf[9];
1773 png_int_32 offset_x, offset_y;
1774 int unit_type;
1775
1776 png_debug(1, "in png_handle_oFFs");
1777
1778 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1779 png_error(png_ptr, "Missing IHDR before oFFs");
1780
1781 else if (png_ptr->mode & PNG_HAVE_IDAT)
1782 {
1783 png_warning(png_ptr, "Invalid oFFs after IDAT");
1784 png_crc_finish(png_ptr, length);
1785 return;
1786 }
1787
1788 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs))
1789 {
1790 png_warning(png_ptr, "Duplicate oFFs chunk");
1791 png_crc_finish(png_ptr, length);
1792 return;
1793 }
1794
1795 if (length != 9)
1796 {
1797 png_warning(png_ptr, "Incorrect oFFs chunk length");
1798 png_crc_finish(png_ptr, length);
1799 return;
1800 }
1801
1802 png_crc_read(png_ptr, buf, 9);
1803
1804 if (png_crc_finish(png_ptr, 0))
1805 return;
1806
1807 offset_x = png_get_int_32(buf);
1808 offset_y = png_get_int_32(buf + 4);
1809 unit_type = buf[8];
1810 png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
1811 }
1812 #endif
1813
1814 #ifdef PNG_READ_pCAL_SUPPORTED
1815 /* Read the pCAL chunk (described in the PNG Extensions document) */
1816 void /* PRIVATE */
1817 png_handle_pCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1818 {
1819 png_int_32 X0, X1;
1820 png_byte type, nparams;
1821 png_charp buf, units, endptr;
1822 png_charpp params;
1823 png_size_t slength;
1824 int i;
1825
1826 png_debug(1, "in png_handle_pCAL");
1827
1828 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1829 png_error(png_ptr, "Missing IHDR before pCAL");
1830
1831 else if (png_ptr->mode & PNG_HAVE_IDAT)
1832 {
1833 png_warning(png_ptr, "Invalid pCAL after IDAT");
1834 png_crc_finish(png_ptr, length);
1835 return;
1836 }
1837
1838 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL))
1839 {
1840 png_warning(png_ptr, "Duplicate pCAL chunk");
1841 png_crc_finish(png_ptr, length);
1842 return;
1843 }
1844
1845 png_debug1(2, "Allocating and reading pCAL chunk data (%u bytes)",
1846 length + 1);
1847 png_free(png_ptr, png_ptr->chunkdata);
1848 png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
1849
1850 if (png_ptr->chunkdata == NULL)
1851 {
1852 png_warning(png_ptr, "No memory for pCAL purpose");
1853 return;
1854 }
1855
1856 slength = (png_size_t)length;
1857 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
1858
1859 if (png_crc_finish(png_ptr, 0))
1860 {
1861 png_free(png_ptr, png_ptr->chunkdata);
1862 png_ptr->chunkdata = NULL;
1863 return;
1864 }
1865
1866 png_ptr->chunkdata[slength] = 0x00; /* Null terminate the last string */
1867
1868 png_debug(3, "Finding end of pCAL purpose string");
1869 for (buf = png_ptr->chunkdata; *buf; buf++)
1870 /* Empty loop */ ;
1871
1872 endptr = png_ptr->chunkdata + slength;
1873
1874 /* We need to have at least 12 bytes after the purpose string
1875 * in order to get the parameter information.
1876 */
1877 if (endptr <= buf + 12)
1878 {
1879 png_warning(png_ptr, "Invalid pCAL data");
1880 png_free(png_ptr, png_ptr->chunkdata);
1881 png_ptr->chunkdata = NULL;
1882 return;
1883 }
1884
1885 png_debug(3, "Reading pCAL X0, X1, type, nparams, and units");
1886 X0 = png_get_int_32((png_bytep)buf+1);
1887 X1 = png_get_int_32((png_bytep)buf+5);
1888 type = buf[9];
1889 nparams = buf[10];
1890 units = buf + 11;
1891
1892 png_debug(3, "Checking pCAL equation type and number of parameters");
1893 /* Check that we have the right number of parameters for known
1894 * equation types.
1895 */
1896 if ((type == PNG_EQUATION_LINEAR && nparams != 2) ||
1897 (type == PNG_EQUATION_BASE_E && nparams != 3) ||
1898 (type == PNG_EQUATION_ARBITRARY && nparams != 3) ||
1899 (type == PNG_EQUATION_HYPERBOLIC && nparams != 4))
1900 {
1901 png_warning(png_ptr, "Invalid pCAL parameters for equation type");
1902 png_free(png_ptr, png_ptr->chunkdata);
1903 png_ptr->chunkdata = NULL;
1904 return;
1905 }
1906
1907 else if (type >= PNG_EQUATION_LAST)
1908 {
1909 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1910 }
1911
1912 for (buf = units; *buf; buf++)
1913 /* Empty loop to move past the units string. */ ;
1914
1915 png_debug(3, "Allocating pCAL parameters array");
1916
1917 params = (png_charpp)png_malloc_warn(png_ptr,
1918 (png_size_t)(nparams * png_sizeof(png_charp)));
1919
1920 if (params == NULL)
1921 {
1922 png_free(png_ptr, png_ptr->chunkdata);
1923 png_ptr->chunkdata = NULL;
1924 png_warning(png_ptr, "No memory for pCAL params");
1925 return;
1926 }
1927
1928 /* Get pointers to the start of each parameter string. */
1929 for (i = 0; i < (int)nparams; i++)
1930 {
1931 buf++; /* Skip the null string terminator from previous parameter. */
1932
1933 png_debug1(3, "Reading pCAL parameter %d", i);
1934
1935 for (params[i] = buf; buf <= endptr && *buf != 0x00; buf++)
1936 /* Empty loop to move past each parameter string */ ;
1937
1938 /* Make sure we haven't run out of data yet */
1939 if (buf > endptr)
1940 {
1941 png_warning(png_ptr, "Invalid pCAL data");
1942 png_free(png_ptr, png_ptr->chunkdata);
1943 png_ptr->chunkdata = NULL;
1944 png_free(png_ptr, params);
1945 return;
1946 }
1947 }
1948
1949 png_set_pCAL(png_ptr, info_ptr, png_ptr->chunkdata, X0, X1, type, nparams,
1950 units, params);
1951
1952 png_free(png_ptr, png_ptr->chunkdata);
1953 png_ptr->chunkdata = NULL;
1954 png_free(png_ptr, params);
1955 }
1956 #endif
1957
1958 #ifdef PNG_READ_sCAL_SUPPORTED
1959 /* Read the sCAL chunk */
1960 void /* PRIVATE */
1961 png_handle_sCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1962 {
1963 png_size_t slength, i;
1964 int state;
1965
1966 png_debug(1, "in png_handle_sCAL");
1967
1968 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1969 png_error(png_ptr, "Missing IHDR before sCAL");
1970
1971 else if (png_ptr->mode & PNG_HAVE_IDAT)
1972 {
1973 png_warning(png_ptr, "Invalid sCAL after IDAT");
1974 png_crc_finish(png_ptr, length);
1975 return;
1976 }
1977
1978 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL))
1979 {
1980 png_warning(png_ptr, "Duplicate sCAL chunk");
1981 png_crc_finish(png_ptr, length);
1982 return;
1983 }
1984
1985 png_debug1(2, "Allocating and reading sCAL chunk data (%u bytes)",
1986 length + 1);
1987
1988 png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
1989
1990 if (png_ptr->chunkdata == NULL)
1991 {
1992 png_warning(png_ptr, "Out of memory while processing sCAL chunk");
1993 png_crc_finish(png_ptr, length);
1994 return;
1995 }
1996
1997 slength = (png_size_t)length;
1998 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
1999 png_ptr->chunkdata[slength] = 0x00; /* Null terminate the last string */
2000
2001 if (png_crc_finish(png_ptr, 0))
2002 {
2003 png_free(png_ptr, png_ptr->chunkdata);
2004 png_ptr->chunkdata = NULL;
2005 return;
2006 }
2007
2008 /* Validate the unit. */
2009 if (png_ptr->chunkdata[0] != 1 && png_ptr->chunkdata[0] != 2)
2010 {
2011 png_warning(png_ptr, "Invalid sCAL ignored: invalid unit");
2012 png_free(png_ptr, png_ptr->chunkdata);
2013 png_ptr->chunkdata = NULL;
2014 return;
2015 }
2016
2017 /* Validate the ASCII numbers, need two ASCII numbers separated by
2018 * a '\0' and they need to fit exactly in the chunk data.
2019 */
2020 i = 0;
2021 state = 0;
2022
2023 if (png_ptr->chunkdata[1] == 45 /* negative width */ ||
2024 !png_check_fp_number(png_ptr->chunkdata, slength, &state, &i) ||
2025 i >= slength || png_ptr->chunkdata[i++] != 0)
2026 png_warning(png_ptr, "Invalid sCAL chunk ignored: bad width format");
2027
2028 else
2029 {
2030 png_size_t heighti = i;
2031
2032 if (png_ptr->chunkdata[i] == 45 /* negative height */ ||
2033 !png_check_fp_number(png_ptr->chunkdata, slength, &state, &i) ||
2034 i != slength)
2035 png_warning(png_ptr, "Invalid sCAL chunk ignored: bad height format");
2036
2037 else
2038 /* This is the (only) success case. */
2039 png_set_sCAL_s(png_ptr, info_ptr, png_ptr->chunkdata[0],
2040 png_ptr->chunkdata+1, png_ptr->chunkdata+heighti);
2041 }
2042
2043 /* Clean up - just free the temporarily allocated buffer. */
2044 png_free(png_ptr, png_ptr->chunkdata);
2045 png_ptr->chunkdata = NULL;
2046 }
2047 #endif
2048
2049 #ifdef PNG_READ_tIME_SUPPORTED
2050 void /* PRIVATE */
2051 png_handle_tIME(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
2052 {
2053 png_byte buf[7];
2054 png_time mod_time;
2055
2056 png_debug(1, "in png_handle_tIME");
2057
2058 if (!(png_ptr->mode & PNG_HAVE_IHDR))
2059 png_error(png_ptr, "Out of place tIME chunk");
2060
2061 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME))
2062 {
2063 png_warning(png_ptr, "Duplicate tIME chunk");
2064 png_crc_finish(png_ptr, length);
2065 return;
2066 }
2067
2068 if (png_ptr->mode & PNG_HAVE_IDAT)
2069 png_ptr->mode |= PNG_AFTER_IDAT;
2070
2071 if (length != 7)
2072 {
2073 png_warning(png_ptr, "Incorrect tIME chunk length");
2074 png_crc_finish(png_ptr, length);
2075 return;
2076 }
2077
2078 png_crc_read(png_ptr, buf, 7);
2079
2080 if (png_crc_finish(png_ptr, 0))
2081 return;
2082
2083 mod_time.second = buf[6];
2084 mod_time.minute = buf[5];
2085 mod_time.hour = buf[4];
2086 mod_time.day = buf[3];
2087 mod_time.month = buf[2];
2088 mod_time.year = png_get_uint_16(buf);
2089
2090 png_set_tIME(png_ptr, info_ptr, &mod_time);
2091 }
2092 #endif
2093
2094 #ifdef PNG_READ_tEXt_SUPPORTED
2095 /* Note: this does not properly handle chunks that are > 64K under DOS */
2096 void /* PRIVATE */
2097 png_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
2098 {
2099 png_textp text_ptr;
2100 png_charp key;
2101 png_charp text;
2102 png_uint_32 skip = 0;
2103 png_size_t slength;
2104 int ret;
2105
2106 png_debug(1, "in png_handle_tEXt");
2107
2108 #ifdef PNG_USER_LIMITS_SUPPORTED
2109 if (png_ptr->user_chunk_cache_max != 0)
2110 {
2111 if (png_ptr->user_chunk_cache_max == 1)
2112 {
2113 png_crc_finish(png_ptr, length);
2114 return;
2115 }
2116
2117 if (--png_ptr->user_chunk_cache_max == 1)
2118 {
2119 png_warning(png_ptr, "No space in chunk cache for tEXt");
2120 png_crc_finish(png_ptr, length);
2121 return;
2122 }
2123 }
2124 #endif
2125
2126 if (!(png_ptr->mode & PNG_HAVE_IHDR))
2127 png_error(png_ptr, "Missing IHDR before tEXt");
2128
2129 if (png_ptr->mode & PNG_HAVE_IDAT)
2130 png_ptr->mode |= PNG_AFTER_IDAT;
2131
2132 #ifdef PNG_MAX_MALLOC_64K
2133 if (length > (png_uint_32)65535L)
2134 {
2135 png_warning(png_ptr, "tEXt chunk too large to fit in memory");
2136 skip = length - (png_uint_32)65535L;
2137 length = (png_uint_32)65535L;
2138 }
2139 #endif
2140
2141 png_free(png_ptr, png_ptr->chunkdata);
2142
2143 png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
2144
2145 if (png_ptr->chunkdata == NULL)
2146 {
2147 png_warning(png_ptr, "No memory to process text chunk");
2148 return;
2149 }
2150
2151 slength = (png_size_t)length;
2152 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
2153
2154 if (png_crc_finish(png_ptr, skip))
2155 {
2156 png_free(png_ptr, png_ptr->chunkdata);
2157 png_ptr->chunkdata = NULL;
2158 return;
2159 }
2160
2161 key = png_ptr->chunkdata;
2162
2163 key[slength] = 0x00;
2164
2165 for (text = key; *text; text++)
2166 /* Empty loop to find end of key */ ;
2167
2168 if (text != key + slength)
2169 text++;
2170
2171 text_ptr = (png_textp)png_malloc_warn(png_ptr,
2172 png_sizeof(png_text));
2173
2174 if (text_ptr == NULL)
2175 {
2176 png_warning(png_ptr, "Not enough memory to process text chunk");
2177 png_free(png_ptr, png_ptr->chunkdata);
2178 png_ptr->chunkdata = NULL;
2179 return;
2180 }
2181
2182 text_ptr->compression = PNG_TEXT_COMPRESSION_NONE;
2183 text_ptr->key = key;
2184 text_ptr->lang = NULL;
2185 text_ptr->lang_key = NULL;
2186 text_ptr->itxt_length = 0;
2187 text_ptr->text = text;
2188 text_ptr->text_length = png_strlen(text);
2189
2190 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
2191
2192 png_free(png_ptr, png_ptr->chunkdata);
2193 png_ptr->chunkdata = NULL;
2194 png_free(png_ptr, text_ptr);
2195
2196 if (ret)
2197 png_warning(png_ptr, "Insufficient memory to process text chunk");
2198 }
2199 #endif
2200
2201 #ifdef PNG_READ_zTXt_SUPPORTED
2202 /* Note: this does not correctly handle chunks that are > 64K under DOS */
2203 void /* PRIVATE */
2204 png_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
2205 {
2206 png_textp text_ptr;
2207 png_charp text;
2208 int comp_type;
2209 int ret;
2210 png_size_t slength, prefix_len, data_len;
2211
2212 png_debug(1, "in png_handle_zTXt");
2213
2214 #ifdef PNG_USER_LIMITS_SUPPORTED
2215 if (png_ptr->user_chunk_cache_max != 0)
2216 {
2217 if (png_ptr->user_chunk_cache_max == 1)
2218 {
2219 png_crc_finish(png_ptr, length);
2220 return;
2221 }
2222
2223 if (--png_ptr->user_chunk_cache_max == 1)
2224 {
2225 png_warning(png_ptr, "No space in chunk cache for zTXt");
2226 png_crc_finish(png_ptr, length);
2227 return;
2228 }
2229 }
2230 #endif
2231
2232 if (!(png_ptr->mode & PNG_HAVE_IHDR))
2233 png_error(png_ptr, "Missing IHDR before zTXt");
2234
2235 if (png_ptr->mode & PNG_HAVE_IDAT)
2236 png_ptr->mode |= PNG_AFTER_IDAT;
2237
2238 #ifdef PNG_MAX_MALLOC_64K
2239 /* We will no doubt have problems with chunks even half this size, but
2240 * there is no hard and fast rule to tell us where to stop.
2241 */
2242 if (length > (png_uint_32)65535L)
2243 {
2244 png_warning(png_ptr, "zTXt chunk too large to fit in memory");
2245 png_crc_finish(png_ptr, length);
2246 return;
2247 }
2248 #endif
2249
2250 png_free(png_ptr, png_ptr->chunkdata);
2251 png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
2252
2253 if (png_ptr->chunkdata == NULL)
2254 {
2255 png_warning(png_ptr, "Out of memory processing zTXt chunk");
2256 return;
2257 }
2258
2259 slength = (png_size_t)length;
2260 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
2261
2262 if (png_crc_finish(png_ptr, 0))
2263 {
2264 png_free(png_ptr, png_ptr->chunkdata);
2265 png_ptr->chunkdata = NULL;
2266 return;
2267 }
2268
2269 png_ptr->chunkdata[slength] = 0x00;
2270
2271 for (text = png_ptr->chunkdata; *text; text++)
2272 /* Empty loop */ ;
2273
2274 /* zTXt must have some text after the chunkdataword */
2275 if (text >= png_ptr->chunkdata + slength - 2)
2276 {
2277 png_warning(png_ptr, "Truncated zTXt chunk");
2278 png_free(png_ptr, png_ptr->chunkdata);
2279 png_ptr->chunkdata = NULL;
2280 return;
2281 }
2282
2283 else
2284 {
2285 comp_type = *(++text);
2286
2287 if (comp_type != PNG_TEXT_COMPRESSION_zTXt)
2288 {
2289 png_warning(png_ptr, "Unknown compression type in zTXt chunk");
2290 comp_type = PNG_TEXT_COMPRESSION_zTXt;
2291 }
2292
2293 text++; /* Skip the compression_method byte */
2294 }
2295
2296 prefix_len = text - png_ptr->chunkdata;
2297
2298 png_decompress_chunk(png_ptr, comp_type,
2299 (png_size_t)length, prefix_len, &data_len);
2300
2301 text_ptr = (png_textp)png_malloc_warn(png_ptr,
2302 png_sizeof(png_text));
2303
2304 if (text_ptr == NULL)
2305 {
2306 png_warning(png_ptr, "Not enough memory to process zTXt chunk");
2307 png_free(png_ptr, png_ptr->chunkdata);
2308 png_ptr->chunkdata = NULL;
2309 return;
2310 }
2311
2312 text_ptr->compression = comp_type;
2313 text_ptr->key = png_ptr->chunkdata;
2314 text_ptr->lang = NULL;
2315 text_ptr->lang_key = NULL;
2316 text_ptr->itxt_length = 0;
2317 text_ptr->text = png_ptr->chunkdata + prefix_len;
2318 text_ptr->text_length = data_len;
2319
2320 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
2321
2322 png_free(png_ptr, text_ptr);
2323 png_free(png_ptr, png_ptr->chunkdata);
2324 png_ptr->chunkdata = NULL;
2325
2326 if (ret)
2327 png_error(png_ptr, "Insufficient memory to store zTXt chunk");
2328 }
2329 #endif
2330
2331 #ifdef PNG_READ_iTXt_SUPPORTED
2332 /* Note: this does not correctly handle chunks that are > 64K under DOS */
2333 void /* PRIVATE */
2334 png_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
2335 {
2336 png_textp text_ptr;
2337 png_charp key, lang, text, lang_key;
2338 int comp_flag;
2339 int comp_type = 0;
2340 int ret;
2341 png_size_t slength, prefix_len, data_len;
2342
2343 png_debug(1, "in png_handle_iTXt");
2344
2345 #ifdef PNG_USER_LIMITS_SUPPORTED
2346 if (png_ptr->user_chunk_cache_max != 0)
2347 {
2348 if (png_ptr->user_chunk_cache_max == 1)
2349 {
2350 png_crc_finish(png_ptr, length);
2351 return;
2352 }
2353
2354 if (--png_ptr->user_chunk_cache_max == 1)
2355 {
2356 png_warning(png_ptr, "No space in chunk cache for iTXt");
2357 png_crc_finish(png_ptr, length);
2358 return;
2359 }
2360 }
2361 #endif
2362
2363 if (!(png_ptr->mode & PNG_HAVE_IHDR))
2364 png_error(png_ptr, "Missing IHDR before iTXt");
2365
2366 if (png_ptr->mode & PNG_HAVE_IDAT)
2367 png_ptr->mode |= PNG_AFTER_IDAT;
2368
2369 #ifdef PNG_MAX_MALLOC_64K
2370 /* We will no doubt have problems with chunks even half this size, but
2371 * there is no hard and fast rule to tell us where to stop.
2372 */
2373 if (length > (png_uint_32)65535L)
2374 {
2375 png_warning(png_ptr, "iTXt chunk too large to fit in memory");
2376 png_crc_finish(png_ptr, length);
2377 return;
2378 }
2379 #endif
2380
2381 png_free(png_ptr, png_ptr->chunkdata);
2382 png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
2383
2384 if (png_ptr->chunkdata == NULL)
2385 {
2386 png_warning(png_ptr, "No memory to process iTXt chunk");
2387 return;
2388 }
2389
2390 slength = (png_size_t)length;
2391 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
2392
2393 if (png_crc_finish(png_ptr, 0))
2394 {
2395 png_free(png_ptr, png_ptr->chunkdata);
2396 png_ptr->chunkdata = NULL;
2397 return;
2398 }
2399
2400 png_ptr->chunkdata[slength] = 0x00;
2401
2402 for (lang = png_ptr->chunkdata; *lang; lang++)
2403 /* Empty loop */ ;
2404
2405 lang++; /* Skip NUL separator */
2406
2407 /* iTXt must have a language tag (possibly empty), two compression bytes,
2408 * translated keyword (possibly empty), and possibly some text after the
2409 * keyword
2410 */
2411
2412 if (lang >= png_ptr->chunkdata + slength - 3)
2413 {
2414 png_warning(png_ptr, "Truncated iTXt chunk");
2415 png_free(png_ptr, png_ptr->chunkdata);
2416 png_ptr->chunkdata = NULL;
2417 return;
2418 }
2419
2420 else
2421 {
2422 comp_flag = *lang++;
2423 comp_type = *lang++;
2424 }
2425
2426 for (lang_key = lang; *lang_key; lang_key++)
2427 /* Empty loop */ ;
2428
2429 lang_key++; /* Skip NUL separator */
2430
2431 if (lang_key >= png_ptr->chunkdata + slength)
2432 {
2433 png_warning(png_ptr, "Truncated iTXt chunk");
2434 png_free(png_ptr, png_ptr->chunkdata);
2435 png_ptr->chunkdata = NULL;
2436 return;
2437 }
2438
2439 for (text = lang_key; *text; text++)
2440 /* Empty loop */ ;
2441
2442 text++; /* Skip NUL separator */
2443
2444 if (text >= png_ptr->chunkdata + slength)
2445 {
2446 png_warning(png_ptr, "Malformed iTXt chunk");
2447 png_free(png_ptr, png_ptr->chunkdata);
2448 png_ptr->chunkdata = NULL;
2449 return;
2450 }
2451
2452 prefix_len = text - png_ptr->chunkdata;
2453
2454 key=png_ptr->chunkdata;
2455
2456 if (comp_flag)
2457 png_decompress_chunk(png_ptr, comp_type,
2458 (size_t)length, prefix_len, &data_len);
2459
2460 else
2461 data_len = png_strlen(png_ptr->chunkdata + prefix_len);
2462
2463 text_ptr = (png_textp)png_malloc_warn(png_ptr,
2464 png_sizeof(png_text));
2465
2466 if (text_ptr == NULL)
2467 {
2468 png_warning(png_ptr, "Not enough memory to process iTXt chunk");
2469 png_free(png_ptr, png_ptr->chunkdata);
2470 png_ptr->chunkdata = NULL;
2471 return;
2472 }
2473
2474 text_ptr->compression = (int)comp_flag + 1;
2475 text_ptr->lang_key = png_ptr->chunkdata + (lang_key - key);
2476 text_ptr->lang = png_ptr->chunkdata + (lang - key);
2477 text_ptr->itxt_length = data_len;
2478 text_ptr->text_length = 0;
2479 text_ptr->key = png_ptr->chunkdata;
2480 text_ptr->text = png_ptr->chunkdata + prefix_len;
2481
2482 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
2483
2484 png_free(png_ptr, text_ptr);
2485 png_free(png_ptr, png_ptr->chunkdata);
2486 png_ptr->chunkdata = NULL;
2487
2488 if (ret)
2489 png_error(png_ptr, "Insufficient memory to store iTXt chunk");
2490 }
2491 #endif
2492
2493 /* This function is called when we haven't found a handler for a
2494 * chunk. If there isn't a problem with the chunk itself (ie bad
2495 * chunk name, CRC, or a critical chunk), the chunk is silently ignored
2496 * -- unless the PNG_FLAG_UNKNOWN_CHUNKS_SUPPORTED flag is on in which
2497 * case it will be saved away to be written out later.
2498 */
2499 void /* PRIVATE */
2500 png_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
2501 {
2502 png_uint_32 skip = 0;
2503
2504 png_debug(1, "in png_handle_unknown");
2505
2506 #ifdef PNG_USER_LIMITS_SUPPORTED
2507 if (png_ptr->user_chunk_cache_max != 0)
2508 {
2509 if (png_ptr->user_chunk_cache_max == 1)
2510 {
2511 png_crc_finish(png_ptr, length);
2512 return;
2513 }
2514
2515 if (--png_ptr->user_chunk_cache_max == 1)
2516 {
2517 png_warning(png_ptr, "No space in chunk cache for unknown chunk");
2518 png_crc_finish(png_ptr, length);
2519 return;
2520 }
2521 }
2522 #endif
2523
2524 if (png_ptr->mode & PNG_HAVE_IDAT)
2525 {
2526 PNG_IDAT;
2527
2528 if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) /* Not an IDAT */
2529 png_ptr->mode |= PNG_AFTER_IDAT;
2530 }
2531
2532 if (!(png_ptr->chunk_name[0] & 0x20))
2533 {
2534 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
2535 if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
2536 PNG_HANDLE_CHUNK_ALWAYS
2537 #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
2538 && png_ptr->read_user_chunk_fn == NULL
2539 #endif
2540 )
2541 #endif
2542 png_chunk_error(png_ptr, "unknown critical chunk");
2543 }
2544
2545 #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
2546 if ((png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS)
2547 #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
2548 || (png_ptr->read_user_chunk_fn != NULL)
2549 #endif
2550 )
2551 {
2552 #ifdef PNG_MAX_MALLOC_64K
2553 if (length > (png_uint_32)65535L)
2554 {
2555 png_warning(png_ptr, "unknown chunk too large to fit in memory");
2556 skip = length - (png_uint_32)65535L;
2557 length = (png_uint_32)65535L;
2558 }
2559 #endif
2560
2561 png_memcpy((png_charp)png_ptr->unknown_chunk.name,
2562 (png_charp)png_ptr->chunk_name,
2563 png_sizeof(png_ptr->unknown_chunk.name));
2564
2565 png_ptr->unknown_chunk.name[png_sizeof(png_ptr->unknown_chunk.name)-1]
2566 = '\0';
2567
2568 png_ptr->unknown_chunk.size = (png_size_t)length;
2569
2570 if (length == 0)
2571 png_ptr->unknown_chunk.data = NULL;
2572
2573 else
2574 {
2575 png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr, length);
2576 png_crc_read(png_ptr, (png_bytep)png_ptr->unknown_chunk.data, length);
2577 }
2578
2579 #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
2580 if (png_ptr->read_user_chunk_fn != NULL)
2581 {
2582 /* Callback to user unknown chunk handler */
2583 int ret;
2584
2585 ret = (*(png_ptr->read_user_chunk_fn))
2586 (png_ptr, &png_ptr->unknown_chunk);
2587
2588 if (ret < 0)
2589 png_chunk_error(png_ptr, "error in user chunk");
2590
2591 if (ret == 0)
2592 {
2593 if (!(png_ptr->chunk_name[0] & 0x20))
2594 {
2595 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
2596 if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
2597 PNG_HANDLE_CHUNK_ALWAYS)
2598 #endif
2599 png_chunk_error(png_ptr, "unknown critical chunk");
2600 }
2601
2602 png_set_unknown_chunks(png_ptr, info_ptr,
2603 &png_ptr->unknown_chunk, 1);
2604 }
2605 }
2606
2607 else
2608 #endif
2609 png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1);
2610
2611 png_free(png_ptr, png_ptr->unknown_chunk.data);
2612 png_ptr->unknown_chunk.data = NULL;
2613 }
2614
2615 else
2616 #endif
2617 skip = length;
2618
2619 png_crc_finish(png_ptr, skip);
2620
2621 #ifndef PNG_READ_USER_CHUNKS_SUPPORTED
2622 PNG_UNUSED(info_ptr) /* Quiet compiler warnings about unused info_ptr */
2623 #endif
2624 }
2625
2626 /* This function is called to verify that a chunk name is valid.
2627 * This function can't have the "critical chunk check" incorporated
2628 * into it, since in the future we will need to be able to call user
2629 * functions to handle unknown critical chunks after we check that
2630 * the chunk name itself is valid.
2631 */
2632
2633 #define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
2634
2635 void /* PRIVATE */
2636 png_check_chunk_name(png_structp png_ptr, png_const_bytep chunk_name)
2637 {
2638 png_debug(1, "in png_check_chunk_name");
2639 if (isnonalpha(chunk_name[0]) || isnonalpha(chunk_name[1]) ||
2640 isnonalpha(chunk_name[2]) || isnonalpha(chunk_name[3]))
2641 {
2642 png_chunk_error(png_ptr, "invalid chunk type");
2643 }
2644 }
2645
2646 /* Combines the row recently read in with the existing pixels in the
2647 * row. This routine takes care of alpha and transparency if requested.
2648 * This routine also handles the two methods of progressive display
2649 * of interlaced images, depending on the mask value.
2650 * The mask value describes which pixels are to be combined with
2651 * the row. The pattern always repeats every 8 pixels, so just 8
2652 * bits are needed. A one indicates the pixel is to be combined,
2653 * a zero indicates the pixel is to be skipped. This is in addition
2654 * to any alpha or transparency value associated with the pixel. If
2655 * you want all pixels to be combined, pass 0xff (255) in mask.
2656 */
2657
2658 void /* PRIVATE */
2659 png_combine_row(png_structp png_ptr, png_bytep row, int mask)
2660 {
2661 png_debug(1, "in png_combine_row");
2662
2663 if (mask == 0xff)
2664 {
2665 png_memcpy(row, png_ptr->row_buf + 1,
2666 PNG_ROWBYTES(png_ptr->row_info.pixel_depth, png_ptr->width));
2667 }
2668
2669 else
2670 {
2671 switch (png_ptr->row_info.pixel_depth)
2672 {
2673 case 1:
2674 {
2675 png_bytep sp = png_ptr->row_buf + 1;
2676 png_bytep dp = row;
2677 int s_inc, s_start, s_end;
2678 int m = 0x80;
2679 int shift;
2680 png_uint_32 i;
2681 png_uint_32 row_width = png_ptr->width;
2682
2683 #ifdef PNG_READ_PACKSWAP_SUPPORTED
2684 if (png_ptr->transformations & PNG_PACKSWAP)
2685 {
2686 s_start = 0;
2687 s_end = 7;
2688 s_inc = 1;
2689 }
2690
2691 else
2692 #endif
2693 {
2694 s_start = 7;
2695 s_end = 0;
2696 s_inc = -1;
2697 }
2698
2699 shift = s_start;
2700
2701 for (i = 0; i < row_width; i++)
2702 {
2703 if (m & mask)
2704 {
2705 int value;
2706
2707 value = (*sp >> shift) & 0x01;
2708 *dp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
2709 *dp |= (png_byte)(value << shift);
2710 }
2711
2712 if (shift == s_end)
2713 {
2714 shift = s_start;
2715 sp++;
2716 dp++;
2717 }
2718
2719 else
2720 shift += s_inc;
2721
2722 if (m == 1)
2723 m = 0x80;
2724
2725 else
2726 m >>= 1;
2727 }
2728 break;
2729 }
2730
2731 case 2:
2732 {
2733 png_bytep sp = png_ptr->row_buf + 1;
2734 png_bytep dp = row;
2735 int s_start, s_end, s_inc;
2736 int m = 0x80;
2737 int shift;
2738 png_uint_32 i;
2739 png_uint_32 row_width = png_ptr->width;
2740 int value;
2741
2742 #ifdef PNG_READ_PACKSWAP_SUPPORTED
2743 if (png_ptr->transformations & PNG_PACKSWAP)
2744 {
2745 s_start = 0;
2746 s_end = 6;
2747 s_inc = 2;
2748 }
2749
2750 else
2751 #endif
2752 {
2753 s_start = 6;
2754 s_end = 0;
2755 s_inc = -2;
2756 }
2757
2758 shift = s_start;
2759
2760 for (i = 0; i < row_width; i++)
2761 {
2762 if (m & mask)
2763 {
2764 value = (*sp >> shift) & 0x03;
2765 *dp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
2766 *dp |= (png_byte)(value << shift);
2767 }
2768
2769 if (shift == s_end)
2770 {
2771 shift = s_start;
2772 sp++;
2773 dp++;
2774 }
2775
2776 else
2777 shift += s_inc;
2778
2779 if (m == 1)
2780 m = 0x80;
2781
2782 else
2783 m >>= 1;
2784 }
2785 break;
2786 }
2787
2788 case 4:
2789 {
2790 png_bytep sp = png_ptr->row_buf + 1;
2791 png_bytep dp = row;
2792 int s_start, s_end, s_inc;
2793 int m = 0x80;
2794 int shift;
2795 png_uint_32 i;
2796 png_uint_32 row_width = png_ptr->width;
2797 int value;
2798
2799 #ifdef PNG_READ_PACKSWAP_SUPPORTED
2800 if (png_ptr->transformations & PNG_PACKSWAP)
2801 {
2802 s_start = 0;
2803 s_end = 4;
2804 s_inc = 4;
2805 }
2806
2807 else
2808 #endif
2809 {
2810 s_start = 4;
2811 s_end = 0;
2812 s_inc = -4;
2813 }
2814 shift = s_start;
2815
2816 for (i = 0; i < row_width; i++)
2817 {
2818 if (m & mask)
2819 {
2820 value = (*sp >> shift) & 0xf;
2821 *dp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
2822 *dp |= (png_byte)(value << shift);
2823 }
2824
2825 if (shift == s_end)
2826 {
2827 shift = s_start;
2828 sp++;
2829 dp++;
2830 }
2831
2832 else
2833 shift += s_inc;
2834
2835 if (m == 1)
2836 m = 0x80;
2837
2838 else
2839 m >>= 1;
2840 }
2841 break;
2842 }
2843
2844 default:
2845 {
2846 png_bytep sp = png_ptr->row_buf + 1;
2847 png_bytep dp = row;
2848 png_size_t pixel_bytes = (png_ptr->row_info.pixel_depth >> 3);
2849 png_uint_32 i;
2850 png_uint_32 row_width = png_ptr->width;
2851 png_byte m = 0x80;
2852
2853 for (i = 0; i < row_width; i++)
2854 {
2855 if (m & mask)
2856 {
2857 png_memcpy(dp, sp, pixel_bytes);
2858 }
2859
2860 sp += pixel_bytes;
2861 dp += pixel_bytes;
2862
2863 if (m == 1)
2864 m = 0x80;
2865
2866 else
2867 m >>= 1;
2868 }
2869 break;
2870 }
2871 }
2872 }
2873 }
2874
2875 #ifdef PNG_READ_INTERLACING_SUPPORTED
2876 void /* PRIVATE */
2877 png_do_read_interlace(png_structp png_ptr)
2878 {
2879 png_row_infop row_info = &(png_ptr->row_info);
2880 png_bytep row = png_ptr->row_buf + 1;
2881 int pass = png_ptr->pass;
2882 png_uint_32 transformations = png_ptr->transformations;
2883 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
2884 /* Offset to next interlace block */
2885 PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
2886
2887 png_debug(1, "in png_do_read_interlace");
2888 if (row != NULL && row_info != NULL)
2889 {
2890 png_uint_32 final_width;
2891
2892 final_width = row_info->width * png_pass_inc[pass];
2893
2894 switch (row_info->pixel_depth)
2895 {
2896 case 1:
2897 {
2898 png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3);
2899 png_bytep dp = row + (png_size_t)((final_width - 1) >> 3);
2900 int sshift, dshift;
2901 int s_start, s_end, s_inc;
2902 int jstop = png_pass_inc[pass];
2903 png_byte v;
2904 png_uint_32 i;
2905 int j;
2906
2907 #ifdef PNG_READ_PACKSWAP_SUPPORTED
2908 if (transformations & PNG_PACKSWAP)
2909 {
2910 sshift = (int)((row_info->width + 7) & 0x07);
2911 dshift = (int)((final_width + 7) & 0x07);
2912 s_start = 7;
2913 s_end = 0;
2914 s_inc = -1;
2915 }
2916
2917 else
2918 #endif
2919 {
2920 sshift = 7 - (int)((row_info->width + 7) & 0x07);
2921 dshift = 7 - (int)((final_width + 7) & 0x07);
2922 s_start = 0;
2923 s_end = 7;
2924 s_inc = 1;
2925 }
2926
2927 for (i = 0; i < row_info->width; i++)
2928 {
2929 v = (png_byte)((*sp >> sshift) & 0x01);
2930 for (j = 0; j < jstop; j++)
2931 {
2932 *dp &= (png_byte)((0x7f7f >> (7 - dshift)) & 0xff);
2933 *dp |= (png_byte)(v << dshift);
2934
2935 if (dshift == s_end)
2936 {
2937 dshift = s_start;
2938 dp--;
2939 }
2940
2941 else
2942 dshift += s_inc;
2943 }
2944
2945 if (sshift == s_end)
2946 {
2947 sshift = s_start;
2948 sp--;
2949 }
2950
2951 else
2952 sshift += s_inc;
2953 }
2954 break;
2955 }
2956
2957 case 2:
2958 {
2959 png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);
2960 png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);
2961 int sshift, dshift;
2962 int s_start, s_end, s_inc;
2963 int jstop = png_pass_inc[pass];
2964 png_uint_32 i;
2965
2966 #ifdef PNG_READ_PACKSWAP_SUPPORTED
2967 if (transformations & PNG_PACKSWAP)
2968 {
2969 sshift = (int)(((row_info->width + 3) & 0x03) << 1);
2970 dshift = (int)(((final_width + 3) & 0x03) << 1);
2971 s_start = 6;
2972 s_end = 0;
2973 s_inc = -2;
2974 }
2975
2976 else
2977 #endif
2978 {
2979 sshift = (int)((3 - ((row_info->width + 3) & 0x03)) << 1);
2980 dshift = (int)((3 - ((final_width + 3) & 0x03)) << 1);
2981 s_start = 0;
2982 s_end = 6;
2983 s_inc = 2;
2984 }
2985
2986 for (i = 0; i < row_info->width; i++)
2987 {
2988 png_byte v;
2989 int j;
2990
2991 v = (png_byte)((*sp >> sshift) & 0x03);
2992 for (j = 0; j < jstop; j++)
2993 {
2994 *dp &= (png_byte)((0x3f3f >> (6 - dshift)) & 0xff);
2995 *dp |= (png_byte)(v << dshift);
2996
2997 if (dshift == s_end)
2998 {
2999 dshift = s_start;
3000 dp--;
3001 }
3002
3003 else
3004 dshift += s_inc;
3005 }
3006
3007 if (sshift == s_end)
3008 {
3009 sshift = s_start;
3010 sp--;
3011 }
3012
3013 else
3014 sshift += s_inc;
3015 }
3016 break;
3017 }
3018
3019 case 4:
3020 {
3021 png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1);
3022 png_bytep dp = row + (png_size_t)((final_width - 1) >> 1);
3023 int sshift, dshift;
3024 int s_start, s_end, s_inc;
3025 png_uint_32 i;
3026 int jstop = png_pass_inc[pass];
3027
3028 #ifdef PNG_READ_PACKSWAP_SUPPORTED
3029 if (transformations & PNG_PACKSWAP)
3030 {
3031 sshift = (int)(((row_info->width + 1) & 0x01) << 2);
3032 dshift = (int)(((final_width + 1) & 0x01) << 2);
3033 s_start = 4;
3034 s_end = 0;
3035 s_inc = -4;
3036 }
3037
3038 else
3039 #endif
3040 {
3041 sshift = (int)((1 - ((row_info->width + 1) & 0x01)) << 2);
3042 dshift = (int)((1 - ((final_width + 1) & 0x01)) << 2);
3043 s_start = 0;
3044 s_end = 4;
3045 s_inc = 4;
3046 }
3047
3048 for (i = 0; i < row_info->width; i++)
3049 {
3050 png_byte v = (png_byte)((*sp >> sshift) & 0xf);
3051 int j;
3052
3053 for (j = 0; j < jstop; j++)
3054 {
3055 *dp &= (png_byte)((0xf0f >> (4 - dshift)) & 0xff);
3056 *dp |= (png_byte)(v << dshift);
3057
3058 if (dshift == s_end)
3059 {
3060 dshift = s_start;
3061 dp--;
3062 }
3063
3064 else
3065 dshift += s_inc;
3066 }
3067
3068 if (sshift == s_end)
3069 {
3070 sshift = s_start;
3071 sp--;
3072 }
3073
3074 else
3075 sshift += s_inc;
3076 }
3077 break;
3078 }
3079 default:
3080 {
3081 png_size_t pixel_bytes = (row_info->pixel_depth >> 3);
3082
3083 png_bytep sp = row + (png_size_t)(row_info->width - 1)
3084 * pixel_bytes;
3085
3086 png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes;
3087
3088 int jstop = png_pass_inc[pass];
3089 png_uint_32 i;
3090
3091 for (i = 0; i < row_info->width; i++)
3092 {
3093 png_byte v[8];
3094 int j;
3095
3096 png_memcpy(v, sp, pixel_bytes);
3097
3098 for (j = 0; j < jstop; j++)
3099 {
3100 png_memcpy(dp, v, pixel_bytes);
3101 dp -= pixel_bytes;
3102 }
3103
3104 sp -= pixel_bytes;
3105 }
3106 break;
3107 }
3108 }
3109 row_info->width = final_width;
3110 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width);
3111 }
3112 #ifndef PNG_READ_PACKSWAP_SUPPORTED
3113 PNG_UNUSED(transformations) /* Silence compiler warning */
3114 #endif
3115 }
3116 #endif /* PNG_READ_INTERLACING_SUPPORTED */
3117
3118 void /* PRIVATE */
3119 png_read_filter_row(png_structp png_ptr, png_row_infop row_info, png_bytep row,
3120 png_const_bytep prev_row, int filter)
3121 {
3122 png_debug(1, "in png_read_filter_row");
3123 png_debug2(2, "row = %u, filter = %d", png_ptr->row_number, filter);
3124 switch (filter)
3125 {
3126 case PNG_FILTER_VALUE_NONE:
3127 break;
3128
3129 case PNG_FILTER_VALUE_SUB:
3130 {
3131 png_size_t i;
3132 png_size_t istop = row_info->rowbytes;
3133 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
3134 png_bytep rp = row + bpp;
3135 png_bytep lp = row;
3136
3137 for (i = bpp; i < istop; i++)
3138 {
3139 *rp = (png_byte)(((int)(*rp) + (int)(*lp++)) & 0xff);
3140 rp++;
3141 }
3142 break;
3143 }
3144 case PNG_FILTER_VALUE_UP:
3145 {
3146 png_size_t i;
3147 png_size_t istop = row_info->rowbytes;
3148 png_bytep rp = row;
3149 png_const_bytep pp = prev_row;
3150
3151 for (i = 0; i < istop; i++)
3152 {
3153 *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
3154 rp++;
3155 }
3156 break;
3157 }
3158 case PNG_FILTER_VALUE_AVG:
3159 {
3160 png_size_t i;
3161 png_bytep rp = row;
3162 png_const_bytep pp = prev_row;
3163 png_bytep lp = row;
3164 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
3165 png_size_t istop = row_info->rowbytes - bpp;
3166
3167 for (i = 0; i < bpp; i++)
3168 {
3169 *rp = (png_byte)(((int)(*rp) +
3170 ((int)(*pp++) / 2 )) & 0xff);
3171
3172 rp++;
3173 }
3174
3175 for (i = 0; i < istop; i++)
3176 {
3177 *rp = (png_byte)(((int)(*rp) +
3178 (int)(*pp++ + *lp++) / 2 ) & 0xff);
3179
3180 rp++;
3181 }
3182 break;
3183 }
3184 case PNG_FILTER_VALUE_PAETH:
3185 {
3186 png_size_t i;
3187 png_bytep rp = row;
3188 png_const_bytep pp = prev_row;
3189 png_bytep lp = row;
3190 png_const_bytep cp = prev_row;
3191 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
3192 png_size_t istop=row_info->rowbytes - bpp;
3193
3194 for (i = 0; i < bpp; i++)
3195 {
3196 *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
3197 rp++;
3198 }
3199
3200 for (i = 0; i < istop; i++) /* Use leftover rp,pp */
3201 {
3202 int a, b, c, pa, pb, pc, p;
3203
3204 a = *lp++;
3205 b = *pp++;
3206 c = *cp++;
3207
3208 p = b - c;
3209 pc = a - c;
3210
3211 #ifdef PNG_USE_ABS
3212 pa = abs(p);
3213 pb = abs(pc);
3214 pc = abs(p + pc);
3215 #else
3216 pa = p < 0 ? -p : p;
3217 pb = pc < 0 ? -pc : pc;
3218 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
3219 #endif
3220
3221 /*
3222 if (pa <= pb && pa <= pc)
3223 p = a;
3224
3225 else if (pb <= pc)
3226 p = b;
3227
3228 else
3229 p = c;
3230 */
3231
3232 p = (pa <= pb && pa <= pc) ? a : (pb <= pc) ? b : c;
3233
3234 *rp = (png_byte)(((int)(*rp) + p) & 0xff);
3235 rp++;
3236 }
3237 break;
3238 }
3239 default:
3240 png_error(png_ptr, "Ignoring bad adaptive filter type");
3241 /*NOT REACHED */
3242 break;
3243 }
3244 }
3245
3246 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
3247 void /* PRIVATE */
3248 png_read_finish_row(png_structp png_ptr)
3249 {
3250 #ifdef PNG_READ_INTERLACING_SUPPORTED
3251 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
3252
3253 /* Start of interlace block */
3254 PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
3255
3256 /* Offset to next interlace block */
3257 PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
3258
3259 /* Start of interlace block in the y direction */
3260 PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
3261
3262 /* Offset to next interlace block in the y direction */
3263 PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
3264 #endif /* PNG_READ_INTERLACING_SUPPORTED */
3265
3266 png_debug(1, "in png_read_finish_row");
3267 png_ptr->row_number++;
3268 if (png_ptr->row_number < png_ptr->num_rows)
3269 return;
3270
3271 #ifdef PNG_READ_INTERLACING_SUPPORTED
3272 if (png_ptr->interlaced)
3273 {
3274 png_ptr->row_number = 0;
3275
3276 png_memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
3277
3278 do
3279 {
3280 png_ptr->pass++;
3281
3282 if (png_ptr->pass >= 7)
3283 break;
3284
3285 png_ptr->iwidth = (png_ptr->width +
3286 png_pass_inc[png_ptr->pass] - 1 -
3287 png_pass_start[png_ptr->pass]) /
3288 png_pass_inc[png_ptr->pass];
3289
3290 if (!(png_ptr->transformations & PNG_INTERLACE))
3291 {
3292 png_ptr->num_rows = (png_ptr->height +
3293 png_pass_yinc[png_ptr->pass] - 1 -
3294 png_pass_ystart[png_ptr->pass]) /
3295 png_pass_yinc[png_ptr->pass];
3296 }
3297
3298 else /* if (png_ptr->transformations & PNG_INTERLACE) */
3299 break; /* libpng deinterlacing sees every row */
3300
3301 } while (png_ptr->num_rows == 0 || png_ptr->iwidth == 0);
3302
3303 if (png_ptr->pass < 7)
3304 return;
3305 }
3306 #endif /* PNG_READ_INTERLACING_SUPPORTED */
3307
3308 if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
3309 {
3310 PNG_IDAT;
3311 char extra;
3312 int ret;
3313
3314 png_ptr->zstream.next_out = (Byte *)&extra;
3315 png_ptr->zstream.avail_out = (uInt)1;
3316
3317 for (;;)
3318 {
3319 if (!(png_ptr->zstream.avail_in))
3320 {
3321 while (!png_ptr->idat_size)
3322 {
3323 png_crc_finish(png_ptr, 0);
3324 png_ptr->idat_size = png_read_chunk_header(png_ptr);
3325 if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
3326 png_error(png_ptr, "Not enough image data");
3327 }
3328
3329 png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
3330 png_ptr->zstream.next_in = png_ptr->zbuf;
3331
3332 if (png_ptr->zbuf_size > png_ptr->idat_size)
3333 png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
3334
3335 png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zstream.avail_in);
3336 png_ptr->idat_size -= png_ptr->zstream.avail_in;
3337 }
3338
3339 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
3340
3341 if (ret == Z_STREAM_END)
3342 {
3343 if (!(png_ptr->zstream.avail_out) || png_ptr->zstream.avail_in ||
3344 png_ptr->idat_size)
3345 png_warning(png_ptr, "Extra compressed data");
3346
3347 png_ptr->mode |= PNG_AFTER_IDAT;
3348 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
3349 break;
3350 }
3351
3352 if (ret != Z_OK)
3353 png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
3354 "Decompression Error");
3355
3356 if (!(png_ptr->zstream.avail_out))
3357 {
3358 png_warning(png_ptr, "Extra compressed data");
3359 png_ptr->mode |= PNG_AFTER_IDAT;
3360 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
3361 break;
3362 }
3363
3364 }
3365 png_ptr->zstream.avail_out = 0;
3366 }
3367
3368 if (png_ptr->idat_size || png_ptr->zstream.avail_in)
3369 png_warning(png_ptr, "Extra compression data");
3370
3371 inflateReset(&png_ptr->zstream);
3372
3373 png_ptr->mode |= PNG_AFTER_IDAT;
3374 }
3375 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
3376
3377 void /* PRIVATE */
3378 png_read_start_row(png_structp png_ptr)
3379 {
3380 #ifdef PNG_READ_INTERLACING_SUPPORTED
3381 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
3382
3383 /* Start of interlace block */
3384 PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
3385
3386 /* Offset to next interlace block */
3387 PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
3388
3389 /* Start of interlace block in the y direction */
3390 PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
3391
3392 /* Offset to next interlace block in the y direction */
3393 PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
3394 #endif
3395
3396 int max_pixel_depth;
3397 png_size_t row_bytes;
3398
3399 png_debug(1, "in png_read_start_row");
3400 png_ptr->zstream.avail_in = 0;
3401 png_init_read_transformations(png_ptr);
3402 #ifdef PNG_READ_INTERLACING_SUPPORTED
3403 if (png_ptr->interlaced)
3404 {
3405 if (!(png_ptr->transformations & PNG_INTERLACE))
3406 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
3407 png_pass_ystart[0]) / png_pass_yinc[0];
3408
3409 else
3410 png_ptr->num_rows = png_ptr->height;
3411
3412 png_ptr->iwidth = (png_ptr->width +
3413 png_pass_inc[png_ptr->pass] - 1 -
3414 png_pass_start[png_ptr->pass]) /
3415 png_pass_inc[png_ptr->pass];
3416 }
3417
3418 else
3419 #endif /* PNG_READ_INTERLACING_SUPPORTED */
3420 {
3421 png_ptr->num_rows = png_ptr->height;
3422 png_ptr->iwidth = png_ptr->width;
3423 }
3424
3425 max_pixel_depth = png_ptr->pixel_depth;
3426
3427 #ifdef PNG_READ_PACK_SUPPORTED
3428 if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8)
3429 max_pixel_depth = 8;
3430 #endif
3431
3432 #ifdef PNG_READ_EXPAND_SUPPORTED
3433 if (png_ptr->transformations & PNG_EXPAND)
3434 {
3435 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
3436 {
3437 if (png_ptr->num_trans)
3438 max_pixel_depth = 32;
3439
3440 else
3441 max_pixel_depth = 24;
3442 }
3443
3444 else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
3445 {
3446 if (max_pixel_depth < 8)
3447 max_pixel_depth = 8;
3448
3449 if (png_ptr->num_trans)
3450 max_pixel_depth *= 2;
3451 }
3452
3453 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
3454 {
3455 if (png_ptr->num_trans)
3456 {
3457 max_pixel_depth *= 4;
3458 max_pixel_depth /= 3;
3459 }
3460 }
3461 }
3462 #endif
3463
3464 #ifdef PNG_READ_EXPAND_16_SUPPORTED
3465 if (png_ptr->transformations & PNG_EXPAND_16)
3466 {
3467 # ifdef PNG_READ_EXPAND_SUPPORTED
3468 /* In fact it is an error if it isn't supported, but checking is
3469 * the safe way.
3470 */
3471 if (png_ptr->transformations & PNG_EXPAND)
3472 {
3473 if (png_ptr->bit_depth < 16)
3474 max_pixel_depth *= 2;
3475 }
3476 else
3477 # endif
3478 png_ptr->transformations &= ~PNG_EXPAND_16;
3479 }
3480 #endif
3481
3482 #ifdef PNG_READ_FILLER_SUPPORTED
3483 if (png_ptr->transformations & (PNG_FILLER))
3484 {
3485 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
3486 max_pixel_depth = 32;
3487
3488 else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
3489 {
3490 if (max_pixel_depth <= 8)
3491 max_pixel_depth = 16;
3492
3493 else
3494 max_pixel_depth = 32;
3495 }
3496
3497 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
3498 {
3499 if (max_pixel_depth <= 32)
3500 max_pixel_depth = 32;
3501
3502 else
3503 max_pixel_depth = 64;
3504 }
3505 }
3506 #endif
3507
3508 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
3509 if (png_ptr->transformations & PNG_GRAY_TO_RGB)
3510 {
3511 if (
3512 #ifdef PNG_READ_EXPAND_SUPPORTED
3513 (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) ||
3514 #endif
3515 #ifdef PNG_READ_FILLER_SUPPORTED
3516 (png_ptr->transformations & (PNG_FILLER)) ||
3517 #endif
3518 png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
3519 {
3520 if (max_pixel_depth <= 16)
3521 max_pixel_depth = 32;
3522
3523 else
3524 max_pixel_depth = 64;
3525 }
3526
3527 else
3528 {
3529 if (max_pixel_depth <= 8)
3530 {
3531 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
3532 max_pixel_depth = 32;
3533
3534 else
3535 max_pixel_depth = 24;
3536 }
3537
3538 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
3539 max_pixel_depth = 64;
3540
3541 else
3542 max_pixel_depth = 48;
3543 }
3544 }
3545 #endif
3546
3547 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
3548 defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
3549 if (png_ptr->transformations & PNG_USER_TRANSFORM)
3550 {
3551 int user_pixel_depth = png_ptr->user_transform_depth*
3552 png_ptr->user_transform_channels;
3553
3554 if (user_pixel_depth > max_pixel_depth)
3555 max_pixel_depth=user_pixel_depth;
3556 }
3557 #endif
3558
3559 /* Align the width on the next larger 8 pixels. Mainly used
3560 * for interlacing
3561 */
3562 row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
3563 /* Calculate the maximum bytes needed, adding a byte and a pixel
3564 * for safety's sake
3565 */
3566 row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) +
3567 1 + ((max_pixel_depth + 7) >> 3);
3568
3569 #ifdef PNG_MAX_MALLOC_64K
3570 if (row_bytes > (png_uint_32)65536L)
3571 png_error(png_ptr, "This image requires a row greater than 64KB");
3572 #endif
3573
3574 if (row_bytes + 48 > png_ptr->old_big_row_buf_size)
3575 {
3576 png_free(png_ptr, png_ptr->big_row_buf);
3577
3578 if (png_ptr->interlaced)
3579 png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr,
3580 row_bytes + 48);
3581
3582 else
3583 png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr,
3584 row_bytes + 48);
3585
3586 png_ptr->old_big_row_buf_size = row_bytes + 48;
3587
3588 #ifdef PNG_ALIGNED_MEMORY_SUPPORTED
3589 /* Use 16-byte aligned memory for row_buf with at least 16 bytes
3590 * of padding before and after row_buf.
3591 */
3592 png_ptr->row_buf = png_ptr->big_row_buf + 32 -
3593 (((png_alloc_size_t)png_ptr->big_row_buf + 15) & 0x0F);
3594
3595 png_ptr->old_big_row_buf_size = row_bytes + 48;
3596 #else
3597 /* Use 32 bytes of padding before and 16 bytes after row_buf. */
3598 png_ptr->row_buf = png_ptr->big_row_buf + 32;
3599 #endif
3600 png_ptr->old_big_row_buf_size = row_bytes + 48;
3601 }
3602
3603 #ifdef PNG_MAX_MALLOC_64K
3604 if (png_ptr->rowbytes > 65535)
3605 png_error(png_ptr, "This image requires a row greater than 64KB");
3606
3607 #endif
3608 if (png_ptr->rowbytes > (PNG_SIZE_MAX - 1))
3609 png_error(png_ptr, "Row has too many bytes to allocate in memory");
3610
3611 if (png_ptr->rowbytes + 1 > png_ptr->old_prev_row_size)
3612 {
3613 png_free(png_ptr, png_ptr->prev_row);
3614
3615 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, png_ptr->rowbytes + 1);
3616
3617 png_ptr->old_prev_row_size = png_ptr->rowbytes + 1;
3618 }
3619
3620 png_memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
3621
3622 png_debug1(3, "width = %u,", png_ptr->width);
3623 png_debug1(3, "height = %u,", png_ptr->height);
3624 png_debug1(3, "iwidth = %u,", png_ptr->iwidth);
3625 png_debug1(3, "num_rows = %u,", png_ptr->num_rows);
3626 png_debug1(3, "rowbytes = %lu,", (unsigned long)png_ptr->rowbytes);
3627 png_debug1(3, "irowbytes = %lu",
3628 (unsigned long)PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1);
3629
3630 png_ptr->flags |= PNG_FLAG_ROW_INIT;
3631 }
3632 #endif /* PNG_READ_SUPPORTED */