Debian Codegraph @

PNG/libpng/pngrutil.c

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