Bringing libpng-1.5.10 into the main branch
[AROS.git] / workbench / libs / png / pngpread.c
blob8a5aa29ec8dff7d654418f4d1f1064531fad4c41
2 /* pngpread.c - read a png file in push mode
4 * Last changed in libpng 1.5.9 [February 18, 2012]
5 * Copyright (c) 1998-2012 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.)
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
14 #include "pngpriv.h"
16 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
18 /* Push model modes */
19 #define PNG_READ_SIG_MODE 0
20 #define PNG_READ_CHUNK_MODE 1
21 #define PNG_READ_IDAT_MODE 2
22 #define PNG_SKIP_MODE 3
23 #define PNG_READ_tEXt_MODE 4
24 #define PNG_READ_zTXt_MODE 5
25 #define PNG_READ_DONE_MODE 6
26 #define PNG_READ_iTXt_MODE 7
27 #define PNG_ERROR_MODE 8
29 void PNGAPI
30 png_process_data(png_structp png_ptr, png_infop info_ptr,
31 png_bytep buffer, png_size_t buffer_size)
33 if (png_ptr == NULL || info_ptr == NULL)
34 return;
36 png_push_restore_buffer(png_ptr, buffer, buffer_size);
38 while (png_ptr->buffer_size)
40 png_process_some_data(png_ptr, info_ptr);
44 png_size_t PNGAPI
45 png_process_data_pause(png_structp png_ptr, int save)
47 if (png_ptr != NULL)
49 /* It's easiest for the caller if we do the save, then the caller doesn't
50 * have to supply the same data again:
52 if (save)
53 png_push_save_buffer(png_ptr);
54 else
56 /* This includes any pending saved bytes: */
57 png_size_t remaining = png_ptr->buffer_size;
58 png_ptr->buffer_size = 0;
60 /* So subtract the saved buffer size, unless all the data
61 * is actually 'saved', in which case we just return 0
63 if (png_ptr->save_buffer_size < remaining)
64 return remaining - png_ptr->save_buffer_size;
68 return 0;
71 png_uint_32 PNGAPI
72 png_process_data_skip(png_structp png_ptr)
74 png_uint_32 remaining = 0;
76 if (png_ptr != NULL && png_ptr->process_mode == PNG_SKIP_MODE &&
77 png_ptr->skip_length > 0)
79 /* At the end of png_process_data the buffer size must be 0 (see the loop
80 * above) so we can detect a broken call here:
82 if (png_ptr->buffer_size != 0)
83 png_error(png_ptr,
84 "png_process_data_skip called inside png_process_data");
86 /* If is impossible for there to be a saved buffer at this point -
87 * otherwise we could not be in SKIP mode. This will also happen if
88 * png_process_skip is called inside png_process_data (but only very
89 * rarely.)
91 if (png_ptr->save_buffer_size != 0)
92 png_error(png_ptr, "png_process_data_skip called with saved data");
94 remaining = png_ptr->skip_length;
95 png_ptr->skip_length = 0;
96 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
99 return remaining;
102 /* What we do with the incoming data depends on what we were previously
103 * doing before we ran out of data...
105 void /* PRIVATE */
106 png_process_some_data(png_structp png_ptr, png_infop info_ptr)
108 if (png_ptr == NULL)
109 return;
111 switch (png_ptr->process_mode)
113 case PNG_READ_SIG_MODE:
115 png_push_read_sig(png_ptr, info_ptr);
116 break;
119 case PNG_READ_CHUNK_MODE:
121 png_push_read_chunk(png_ptr, info_ptr);
122 break;
125 case PNG_READ_IDAT_MODE:
127 png_push_read_IDAT(png_ptr);
128 break;
131 case PNG_SKIP_MODE:
133 png_push_crc_finish(png_ptr);
134 break;
137 default:
139 png_ptr->buffer_size = 0;
140 break;
145 /* Read any remaining signature bytes from the stream and compare them with
146 * the correct PNG signature. It is possible that this routine is called
147 * with bytes already read from the signature, either because they have been
148 * checked by the calling application, or because of multiple calls to this
149 * routine.
151 void /* PRIVATE */
152 png_push_read_sig(png_structp png_ptr, png_infop info_ptr)
154 png_size_t num_checked = png_ptr->sig_bytes,
155 num_to_check = 8 - num_checked;
157 if (png_ptr->buffer_size < num_to_check)
159 num_to_check = png_ptr->buffer_size;
162 png_push_fill_buffer(png_ptr, &(info_ptr->signature[num_checked]),
163 num_to_check);
164 png_ptr->sig_bytes = (png_byte)(png_ptr->sig_bytes + num_to_check);
166 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
168 if (num_checked < 4 &&
169 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
170 png_error(png_ptr, "Not a PNG file");
172 else
173 png_error(png_ptr, "PNG file corrupted by ASCII conversion");
176 else
178 if (png_ptr->sig_bytes >= 8)
180 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
185 void /* PRIVATE */
186 png_push_read_chunk(png_structp png_ptr, png_infop info_ptr)
188 png_uint_32 chunk_name;
190 /* First we make sure we have enough data for the 4 byte chunk name
191 * and the 4 byte chunk length before proceeding with decoding the
192 * chunk data. To fully decode each of these chunks, we also make
193 * sure we have enough data in the buffer for the 4 byte CRC at the
194 * end of every chunk (except IDAT, which is handled separately).
196 if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
198 png_byte chunk_length[4];
199 png_byte chunk_tag[4];
201 if (png_ptr->buffer_size < 8)
203 png_push_save_buffer(png_ptr);
204 return;
207 png_push_fill_buffer(png_ptr, chunk_length, 4);
208 png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
209 png_reset_crc(png_ptr);
210 png_crc_read(png_ptr, chunk_tag, 4);
211 png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(chunk_tag);
212 png_check_chunk_name(png_ptr, png_ptr->chunk_name);
213 png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
216 chunk_name = png_ptr->chunk_name;
218 if (chunk_name == png_IDAT)
220 /* This is here above the if/else case statement below because if the
221 * unknown handling marks 'IDAT' as unknown then the IDAT handling case is
222 * completely skipped.
224 * TODO: there must be a better way of doing this.
226 if (png_ptr->mode & PNG_AFTER_IDAT)
227 png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
230 if (chunk_name == png_IHDR)
232 if (png_ptr->push_length != 13)
233 png_error(png_ptr, "Invalid IHDR length");
235 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
237 png_push_save_buffer(png_ptr);
238 return;
241 png_handle_IHDR(png_ptr, info_ptr, png_ptr->push_length);
244 else if (chunk_name == png_IEND)
246 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
248 png_push_save_buffer(png_ptr);
249 return;
252 png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length);
254 png_ptr->process_mode = PNG_READ_DONE_MODE;
255 png_push_have_end(png_ptr, info_ptr);
258 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
259 else if (png_chunk_unknown_handling(png_ptr, chunk_name))
261 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
263 png_push_save_buffer(png_ptr);
264 return;
267 if (chunk_name == png_IDAT)
268 png_ptr->mode |= PNG_HAVE_IDAT;
270 png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length);
272 if (chunk_name == png_PLTE)
273 png_ptr->mode |= PNG_HAVE_PLTE;
275 else if (chunk_name == png_IDAT)
277 if (!(png_ptr->mode & PNG_HAVE_IHDR))
278 png_error(png_ptr, "Missing IHDR before IDAT");
280 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
281 !(png_ptr->mode & PNG_HAVE_PLTE))
282 png_error(png_ptr, "Missing PLTE before IDAT");
286 #endif
287 else if (chunk_name == png_PLTE)
289 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
291 png_push_save_buffer(png_ptr);
292 return;
294 png_handle_PLTE(png_ptr, info_ptr, png_ptr->push_length);
297 else if (chunk_name == png_IDAT)
299 /* If we reach an IDAT chunk, this means we have read all of the
300 * header chunks, and we can start reading the image (or if this
301 * is called after the image has been read - we have an error).
304 if (!(png_ptr->mode & PNG_HAVE_IHDR))
305 png_error(png_ptr, "Missing IHDR before IDAT");
307 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
308 !(png_ptr->mode & PNG_HAVE_PLTE))
309 png_error(png_ptr, "Missing PLTE before IDAT");
311 if (png_ptr->mode & PNG_HAVE_IDAT)
313 if (!(png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
314 if (png_ptr->push_length == 0)
315 return;
317 if (png_ptr->mode & PNG_AFTER_IDAT)
318 png_benign_error(png_ptr, "Too many IDATs found");
321 png_ptr->idat_size = png_ptr->push_length;
322 png_ptr->mode |= PNG_HAVE_IDAT;
323 png_ptr->process_mode = PNG_READ_IDAT_MODE;
324 png_push_have_info(png_ptr, info_ptr);
325 png_ptr->zstream.avail_out =
326 (uInt) PNG_ROWBYTES(png_ptr->pixel_depth,
327 png_ptr->iwidth) + 1;
328 png_ptr->zstream.next_out = png_ptr->row_buf;
329 return;
332 #ifdef PNG_READ_gAMA_SUPPORTED
333 else if (png_ptr->chunk_name == png_gAMA)
335 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
337 png_push_save_buffer(png_ptr);
338 return;
341 png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length);
344 #endif
345 #ifdef PNG_READ_sBIT_SUPPORTED
346 else if (png_ptr->chunk_name == png_sBIT)
348 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
350 png_push_save_buffer(png_ptr);
351 return;
354 png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length);
357 #endif
358 #ifdef PNG_READ_cHRM_SUPPORTED
359 else if (png_ptr->chunk_name == png_cHRM)
361 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
363 png_push_save_buffer(png_ptr);
364 return;
367 png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length);
370 #endif
371 #ifdef PNG_READ_sRGB_SUPPORTED
372 else if (chunk_name == png_sRGB)
374 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
376 png_push_save_buffer(png_ptr);
377 return;
380 png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length);
383 #endif
384 #ifdef PNG_READ_iCCP_SUPPORTED
385 else if (png_ptr->chunk_name == png_iCCP)
387 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
389 png_push_save_buffer(png_ptr);
390 return;
393 png_handle_iCCP(png_ptr, info_ptr, png_ptr->push_length);
396 #endif
397 #ifdef PNG_READ_sPLT_SUPPORTED
398 else if (chunk_name == png_sPLT)
400 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
402 png_push_save_buffer(png_ptr);
403 return;
406 png_handle_sPLT(png_ptr, info_ptr, png_ptr->push_length);
409 #endif
410 #ifdef PNG_READ_tRNS_SUPPORTED
411 else if (chunk_name == png_tRNS)
413 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
415 png_push_save_buffer(png_ptr);
416 return;
419 png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length);
422 #endif
423 #ifdef PNG_READ_bKGD_SUPPORTED
424 else if (chunk_name == png_bKGD)
426 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
428 png_push_save_buffer(png_ptr);
429 return;
432 png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length);
435 #endif
436 #ifdef PNG_READ_hIST_SUPPORTED
437 else if (chunk_name == png_hIST)
439 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
441 png_push_save_buffer(png_ptr);
442 return;
445 png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length);
448 #endif
449 #ifdef PNG_READ_pHYs_SUPPORTED
450 else if (chunk_name == png_pHYs)
452 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
454 png_push_save_buffer(png_ptr);
455 return;
458 png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length);
461 #endif
462 #ifdef PNG_READ_oFFs_SUPPORTED
463 else if (chunk_name == png_oFFs)
465 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
467 png_push_save_buffer(png_ptr);
468 return;
471 png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length);
473 #endif
475 #ifdef PNG_READ_pCAL_SUPPORTED
476 else if (chunk_name == png_pCAL)
478 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
480 png_push_save_buffer(png_ptr);
481 return;
484 png_handle_pCAL(png_ptr, info_ptr, png_ptr->push_length);
487 #endif
488 #ifdef PNG_READ_sCAL_SUPPORTED
489 else if (chunk_name == png_sCAL)
491 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
493 png_push_save_buffer(png_ptr);
494 return;
497 png_handle_sCAL(png_ptr, info_ptr, png_ptr->push_length);
500 #endif
501 #ifdef PNG_READ_tIME_SUPPORTED
502 else if (chunk_name == png_tIME)
504 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
506 png_push_save_buffer(png_ptr);
507 return;
510 png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length);
513 #endif
514 #ifdef PNG_READ_tEXt_SUPPORTED
515 else if (chunk_name == png_tEXt)
517 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
519 png_push_save_buffer(png_ptr);
520 return;
523 png_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length);
526 #endif
527 #ifdef PNG_READ_zTXt_SUPPORTED
528 else if (chunk_name == png_zTXt)
530 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
532 png_push_save_buffer(png_ptr);
533 return;
536 png_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length);
539 #endif
540 #ifdef PNG_READ_iTXt_SUPPORTED
541 else if (chunk_name == png_iTXt)
543 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
545 png_push_save_buffer(png_ptr);
546 return;
549 png_handle_iTXt(png_ptr, info_ptr, png_ptr->push_length);
552 #endif
553 else
555 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
557 png_push_save_buffer(png_ptr);
558 return;
560 png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length);
563 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
566 void /* PRIVATE */
567 png_push_crc_skip(png_structp png_ptr, png_uint_32 skip)
569 png_ptr->process_mode = PNG_SKIP_MODE;
570 png_ptr->skip_length = skip;
573 void /* PRIVATE */
574 png_push_crc_finish(png_structp png_ptr)
576 if (png_ptr->skip_length && png_ptr->save_buffer_size)
578 png_size_t save_size = png_ptr->save_buffer_size;
579 png_uint_32 skip_length = png_ptr->skip_length;
581 /* We want the smaller of 'skip_length' and 'save_buffer_size', but
582 * they are of different types and we don't know which variable has the
583 * fewest bits. Carefully select the smaller and cast it to the type of
584 * the larger - this cannot overflow. Do not cast in the following test
585 * - it will break on either 16 or 64 bit platforms.
587 if (skip_length < save_size)
588 save_size = (png_size_t)skip_length;
590 else
591 skip_length = (png_uint_32)save_size;
593 png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
595 png_ptr->skip_length -= skip_length;
596 png_ptr->buffer_size -= save_size;
597 png_ptr->save_buffer_size -= save_size;
598 png_ptr->save_buffer_ptr += save_size;
601 if (png_ptr->skip_length && png_ptr->current_buffer_size)
603 png_size_t save_size = png_ptr->current_buffer_size;
604 png_uint_32 skip_length = png_ptr->skip_length;
606 /* We want the smaller of 'skip_length' and 'current_buffer_size', here,
607 * the same problem exists as above and the same solution.
609 if (skip_length < save_size)
610 save_size = (png_size_t)skip_length;
612 else
613 skip_length = (png_uint_32)save_size;
615 png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
617 png_ptr->skip_length -= skip_length;
618 png_ptr->buffer_size -= save_size;
619 png_ptr->current_buffer_size -= save_size;
620 png_ptr->current_buffer_ptr += save_size;
623 if (!png_ptr->skip_length)
625 if (png_ptr->buffer_size < 4)
627 png_push_save_buffer(png_ptr);
628 return;
631 png_crc_finish(png_ptr, 0);
632 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
636 void PNGCBAPI
637 png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length)
639 png_bytep ptr;
641 if (png_ptr == NULL)
642 return;
644 ptr = buffer;
646 if (png_ptr->save_buffer_size)
648 png_size_t save_size;
650 if (length < png_ptr->save_buffer_size)
651 save_size = length;
653 else
654 save_size = png_ptr->save_buffer_size;
656 png_memcpy(ptr, png_ptr->save_buffer_ptr, save_size);
657 length -= save_size;
658 ptr += save_size;
659 png_ptr->buffer_size -= save_size;
660 png_ptr->save_buffer_size -= save_size;
661 png_ptr->save_buffer_ptr += save_size;
664 if (length && png_ptr->current_buffer_size)
666 png_size_t save_size;
668 if (length < png_ptr->current_buffer_size)
669 save_size = length;
671 else
672 save_size = png_ptr->current_buffer_size;
674 png_memcpy(ptr, png_ptr->current_buffer_ptr, save_size);
675 png_ptr->buffer_size -= save_size;
676 png_ptr->current_buffer_size -= save_size;
677 png_ptr->current_buffer_ptr += save_size;
681 void /* PRIVATE */
682 png_push_save_buffer(png_structp png_ptr)
684 if (png_ptr->save_buffer_size)
686 if (png_ptr->save_buffer_ptr != png_ptr->save_buffer)
688 png_size_t i, istop;
689 png_bytep sp;
690 png_bytep dp;
692 istop = png_ptr->save_buffer_size;
694 for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer;
695 i < istop; i++, sp++, dp++)
697 *dp = *sp;
702 if (png_ptr->save_buffer_size + png_ptr->current_buffer_size >
703 png_ptr->save_buffer_max)
705 png_size_t new_max;
706 png_bytep old_buffer;
708 if (png_ptr->save_buffer_size > PNG_SIZE_MAX -
709 (png_ptr->current_buffer_size + 256))
711 png_error(png_ptr, "Potential overflow of save_buffer");
714 new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;
715 old_buffer = png_ptr->save_buffer;
716 png_ptr->save_buffer = (png_bytep)png_malloc_warn(png_ptr, new_max);
718 if (png_ptr->save_buffer == NULL)
720 png_free(png_ptr, old_buffer);
721 png_error(png_ptr, "Insufficient memory for save_buffer");
724 png_memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
725 png_free(png_ptr, old_buffer);
726 png_ptr->save_buffer_max = new_max;
729 if (png_ptr->current_buffer_size)
731 png_memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
732 png_ptr->current_buffer_ptr, png_ptr->current_buffer_size);
733 png_ptr->save_buffer_size += png_ptr->current_buffer_size;
734 png_ptr->current_buffer_size = 0;
737 png_ptr->save_buffer_ptr = png_ptr->save_buffer;
738 png_ptr->buffer_size = 0;
741 void /* PRIVATE */
742 png_push_restore_buffer(png_structp png_ptr, png_bytep buffer,
743 png_size_t buffer_length)
745 png_ptr->current_buffer = buffer;
746 png_ptr->current_buffer_size = buffer_length;
747 png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size;
748 png_ptr->current_buffer_ptr = png_ptr->current_buffer;
751 void /* PRIVATE */
752 png_push_read_IDAT(png_structp png_ptr)
754 if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
756 png_byte chunk_length[4];
757 png_byte chunk_tag[4];
759 /* TODO: this code can be commoned up with the same code in push_read */
760 if (png_ptr->buffer_size < 8)
762 png_push_save_buffer(png_ptr);
763 return;
766 png_push_fill_buffer(png_ptr, chunk_length, 4);
767 png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
768 png_reset_crc(png_ptr);
769 png_crc_read(png_ptr, chunk_tag, 4);
770 png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(chunk_tag);
771 png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
773 if (png_ptr->chunk_name != png_IDAT)
775 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
777 if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
778 png_error(png_ptr, "Not enough compressed data");
780 return;
783 png_ptr->idat_size = png_ptr->push_length;
786 if (png_ptr->idat_size && png_ptr->save_buffer_size)
788 png_size_t save_size = png_ptr->save_buffer_size;
789 png_uint_32 idat_size = png_ptr->idat_size;
791 /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
792 * are of different types and we don't know which variable has the fewest
793 * bits. Carefully select the smaller and cast it to the type of the
794 * larger - this cannot overflow. Do not cast in the following test - it
795 * will break on either 16 or 64 bit platforms.
797 if (idat_size < save_size)
798 save_size = (png_size_t)idat_size;
800 else
801 idat_size = (png_uint_32)save_size;
803 png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
805 png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size);
807 png_ptr->idat_size -= idat_size;
808 png_ptr->buffer_size -= save_size;
809 png_ptr->save_buffer_size -= save_size;
810 png_ptr->save_buffer_ptr += save_size;
813 if (png_ptr->idat_size && png_ptr->current_buffer_size)
815 png_size_t save_size = png_ptr->current_buffer_size;
816 png_uint_32 idat_size = png_ptr->idat_size;
818 /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
819 * are of different types and we don't know which variable has the fewest
820 * bits. Carefully select the smaller and cast it to the type of the
821 * larger - this cannot overflow.
823 if (idat_size < save_size)
824 save_size = (png_size_t)idat_size;
826 else
827 idat_size = (png_uint_32)save_size;
829 png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
831 png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size);
833 png_ptr->idat_size -= idat_size;
834 png_ptr->buffer_size -= save_size;
835 png_ptr->current_buffer_size -= save_size;
836 png_ptr->current_buffer_ptr += save_size;
839 if (!png_ptr->idat_size)
841 if (png_ptr->buffer_size < 4)
843 png_push_save_buffer(png_ptr);
844 return;
847 png_crc_finish(png_ptr, 0);
848 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
849 png_ptr->mode |= PNG_AFTER_IDAT;
853 void /* PRIVATE */
854 png_process_IDAT_data(png_structp png_ptr, png_bytep buffer,
855 png_size_t buffer_length)
857 /* The caller checks for a non-zero buffer length. */
858 if (!(buffer_length > 0) || buffer == NULL)
859 png_error(png_ptr, "No IDAT data (internal error)");
861 /* This routine must process all the data it has been given
862 * before returning, calling the row callback as required to
863 * handle the uncompressed results.
865 png_ptr->zstream.next_in = buffer;
866 png_ptr->zstream.avail_in = (uInt)buffer_length;
868 /* Keep going until the decompressed data is all processed
869 * or the stream marked as finished.
871 while (png_ptr->zstream.avail_in > 0 &&
872 !(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
874 int ret;
876 /* We have data for zlib, but we must check that zlib
877 * has someplace to put the results. It doesn't matter
878 * if we don't expect any results -- it may be the input
879 * data is just the LZ end code.
881 if (!(png_ptr->zstream.avail_out > 0))
883 png_ptr->zstream.avail_out =
884 (uInt) PNG_ROWBYTES(png_ptr->pixel_depth,
885 png_ptr->iwidth) + 1;
887 png_ptr->zstream.next_out = png_ptr->row_buf;
890 /* Using Z_SYNC_FLUSH here means that an unterminated
891 * LZ stream (a stream with a missing end code) can still
892 * be handled, otherwise (Z_NO_FLUSH) a future zlib
893 * implementation might defer output and therefore
894 * change the current behavior (see comments in inflate.c
895 * for why this doesn't happen at present with zlib 1.2.5).
897 ret = inflate(&png_ptr->zstream, Z_SYNC_FLUSH);
899 /* Check for any failure before proceeding. */
900 if (ret != Z_OK && ret != Z_STREAM_END)
902 /* Terminate the decompression. */
903 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
905 /* This may be a truncated stream (missing or
906 * damaged end code). Treat that as a warning.
908 if (png_ptr->row_number >= png_ptr->num_rows ||
909 png_ptr->pass > 6)
910 png_warning(png_ptr, "Truncated compressed data in IDAT");
912 else
913 png_error(png_ptr, "Decompression error in IDAT");
915 /* Skip the check on unprocessed input */
916 return;
919 /* Did inflate output any data? */
920 if (png_ptr->zstream.next_out != png_ptr->row_buf)
922 /* Is this unexpected data after the last row?
923 * If it is, artificially terminate the LZ output
924 * here.
926 if (png_ptr->row_number >= png_ptr->num_rows ||
927 png_ptr->pass > 6)
929 /* Extra data. */
930 png_warning(png_ptr, "Extra compressed data in IDAT");
931 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
933 /* Do no more processing; skip the unprocessed
934 * input check below.
936 return;
939 /* Do we have a complete row? */
940 if (png_ptr->zstream.avail_out == 0)
941 png_push_process_row(png_ptr);
944 /* And check for the end of the stream. */
945 if (ret == Z_STREAM_END)
946 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
949 /* All the data should have been processed, if anything
950 * is left at this point we have bytes of IDAT data
951 * after the zlib end code.
953 if (png_ptr->zstream.avail_in > 0)
954 png_warning(png_ptr, "Extra compression data in IDAT");
957 void /* PRIVATE */
958 png_push_process_row(png_structp png_ptr)
960 /* 1.5.6: row_info moved out of png_struct to a local here. */
961 png_row_info row_info;
963 row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */
964 row_info.color_type = png_ptr->color_type;
965 row_info.bit_depth = png_ptr->bit_depth;
966 row_info.channels = png_ptr->channels;
967 row_info.pixel_depth = png_ptr->pixel_depth;
968 row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
970 if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
972 if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
973 png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1,
974 png_ptr->prev_row + 1, png_ptr->row_buf[0]);
975 else
976 png_error(png_ptr, "bad adaptive filter value");
979 /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
980 * 1.5.6, while the buffer really is this big in current versions of libpng
981 * it may not be in the future, so this was changed just to copy the
982 * interlaced row count:
984 png_memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
986 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
987 if (png_ptr->transformations)
988 png_do_read_transformations(png_ptr, &row_info);
989 #endif
991 /* The transformed pixel depth should match the depth now in row_info. */
992 if (png_ptr->transformed_pixel_depth == 0)
994 png_ptr->transformed_pixel_depth = row_info.pixel_depth;
995 if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
996 png_error(png_ptr, "progressive row overflow");
999 else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
1000 png_error(png_ptr, "internal progressive row size calculation error");
1003 #ifdef PNG_READ_INTERLACING_SUPPORTED
1004 /* Blow up interlaced rows to full size */
1005 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
1007 if (png_ptr->pass < 6)
1008 png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
1009 png_ptr->transformations);
1011 switch (png_ptr->pass)
1013 case 0:
1015 int i;
1016 for (i = 0; i < 8 && png_ptr->pass == 0; i++)
1018 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1019 png_read_push_finish_row(png_ptr); /* Updates png_ptr->pass */
1022 if (png_ptr->pass == 2) /* Pass 1 might be empty */
1024 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
1026 png_push_have_row(png_ptr, NULL);
1027 png_read_push_finish_row(png_ptr);
1031 if (png_ptr->pass == 4 && png_ptr->height <= 4)
1033 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1035 png_push_have_row(png_ptr, NULL);
1036 png_read_push_finish_row(png_ptr);
1040 if (png_ptr->pass == 6 && png_ptr->height <= 4)
1042 png_push_have_row(png_ptr, NULL);
1043 png_read_push_finish_row(png_ptr);
1046 break;
1049 case 1:
1051 int i;
1052 for (i = 0; i < 8 && png_ptr->pass == 1; i++)
1054 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1055 png_read_push_finish_row(png_ptr);
1058 if (png_ptr->pass == 2) /* Skip top 4 generated rows */
1060 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
1062 png_push_have_row(png_ptr, NULL);
1063 png_read_push_finish_row(png_ptr);
1067 break;
1070 case 2:
1072 int i;
1074 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
1076 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1077 png_read_push_finish_row(png_ptr);
1080 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
1082 png_push_have_row(png_ptr, NULL);
1083 png_read_push_finish_row(png_ptr);
1086 if (png_ptr->pass == 4) /* Pass 3 might be empty */
1088 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1090 png_push_have_row(png_ptr, NULL);
1091 png_read_push_finish_row(png_ptr);
1095 break;
1098 case 3:
1100 int i;
1102 for (i = 0; i < 4 && png_ptr->pass == 3; i++)
1104 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1105 png_read_push_finish_row(png_ptr);
1108 if (png_ptr->pass == 4) /* Skip top two generated rows */
1110 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1112 png_push_have_row(png_ptr, NULL);
1113 png_read_push_finish_row(png_ptr);
1117 break;
1120 case 4:
1122 int i;
1124 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1126 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1127 png_read_push_finish_row(png_ptr);
1130 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1132 png_push_have_row(png_ptr, NULL);
1133 png_read_push_finish_row(png_ptr);
1136 if (png_ptr->pass == 6) /* Pass 5 might be empty */
1138 png_push_have_row(png_ptr, NULL);
1139 png_read_push_finish_row(png_ptr);
1142 break;
1145 case 5:
1147 int i;
1149 for (i = 0; i < 2 && png_ptr->pass == 5; i++)
1151 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1152 png_read_push_finish_row(png_ptr);
1155 if (png_ptr->pass == 6) /* Skip top generated row */
1157 png_push_have_row(png_ptr, NULL);
1158 png_read_push_finish_row(png_ptr);
1161 break;
1164 default:
1165 case 6:
1167 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1168 png_read_push_finish_row(png_ptr);
1170 if (png_ptr->pass != 6)
1171 break;
1173 png_push_have_row(png_ptr, NULL);
1174 png_read_push_finish_row(png_ptr);
1178 else
1179 #endif
1181 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1182 png_read_push_finish_row(png_ptr);
1186 void /* PRIVATE */
1187 png_read_push_finish_row(png_structp png_ptr)
1189 #ifdef PNG_READ_INTERLACING_SUPPORTED
1190 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1192 /* Start of interlace block */
1193 static PNG_CONST png_byte FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
1195 /* Offset to next interlace block */
1196 static PNG_CONST png_byte FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
1198 /* Start of interlace block in the y direction */
1199 static PNG_CONST png_byte FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
1201 /* Offset to next interlace block in the y direction */
1202 static PNG_CONST png_byte FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
1204 /* Height of interlace block. This is not currently used - if you need
1205 * it, uncomment it here and in png.h
1206 static PNG_CONST png_byte FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
1208 #endif
1210 png_ptr->row_number++;
1211 if (png_ptr->row_number < png_ptr->num_rows)
1212 return;
1214 #ifdef PNG_READ_INTERLACING_SUPPORTED
1215 if (png_ptr->interlaced)
1217 png_ptr->row_number = 0;
1218 png_memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
1222 png_ptr->pass++;
1223 if ((png_ptr->pass == 1 && png_ptr->width < 5) ||
1224 (png_ptr->pass == 3 && png_ptr->width < 3) ||
1225 (png_ptr->pass == 5 && png_ptr->width < 2))
1226 png_ptr->pass++;
1228 if (png_ptr->pass > 7)
1229 png_ptr->pass--;
1231 if (png_ptr->pass >= 7)
1232 break;
1234 png_ptr->iwidth = (png_ptr->width +
1235 png_pass_inc[png_ptr->pass] - 1 -
1236 png_pass_start[png_ptr->pass]) /
1237 png_pass_inc[png_ptr->pass];
1239 if (png_ptr->transformations & PNG_INTERLACE)
1240 break;
1242 png_ptr->num_rows = (png_ptr->height +
1243 png_pass_yinc[png_ptr->pass] - 1 -
1244 png_pass_ystart[png_ptr->pass]) /
1245 png_pass_yinc[png_ptr->pass];
1247 } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0);
1249 #endif /* PNG_READ_INTERLACING_SUPPORTED */
1252 void /* PRIVATE */
1253 png_push_have_info(png_structp png_ptr, png_infop info_ptr)
1255 if (png_ptr->info_fn != NULL)
1256 (*(png_ptr->info_fn))(png_ptr, info_ptr);
1259 void /* PRIVATE */
1260 png_push_have_end(png_structp png_ptr, png_infop info_ptr)
1262 if (png_ptr->end_fn != NULL)
1263 (*(png_ptr->end_fn))(png_ptr, info_ptr);
1266 void /* PRIVATE */
1267 png_push_have_row(png_structp png_ptr, png_bytep row)
1269 if (png_ptr->row_fn != NULL)
1270 (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number,
1271 (int)png_ptr->pass);
1274 #ifdef PNG_READ_INTERLACING_SUPPORTED
1275 void PNGAPI
1276 png_progressive_combine_row (png_structp png_ptr, png_bytep old_row,
1277 png_const_bytep new_row)
1279 if (png_ptr == NULL)
1280 return;
1282 /* new_row is a flag here - if it is NULL then the app callback was called
1283 * from an empty row (see the calls to png_struct::row_fn below), otherwise
1284 * it must be png_ptr->row_buf+1
1286 if (new_row != NULL)
1287 png_combine_row(png_ptr, old_row, 1/*display*/);
1289 #endif /* PNG_READ_INTERLACING_SUPPORTED */
1291 void PNGAPI
1292 png_set_progressive_read_fn(png_structp png_ptr, png_voidp progressive_ptr,
1293 png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
1294 png_progressive_end_ptr end_fn)
1296 if (png_ptr == NULL)
1297 return;
1299 png_ptr->info_fn = info_fn;
1300 png_ptr->row_fn = row_fn;
1301 png_ptr->end_fn = end_fn;
1303 png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer);
1306 png_voidp PNGAPI
1307 png_get_progressive_ptr(png_const_structp png_ptr)
1309 if (png_ptr == NULL)
1310 return (NULL);
1312 return png_ptr->io_ptr;
1314 #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */