2 /* pngpread.c - read a png file in push mode
4 * Copyright (c) 2018 Cosmin Truta
5 * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson
6 * Copyright (c) 1996-1997 Andreas Dilger
7 * 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
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_READ_tEXt_MODE 4
23 #define PNG_READ_zTXt_MODE 5
24 #define PNG_READ_DONE_MODE 6
25 #define PNG_READ_iTXt_MODE 7
26 #define PNG_ERROR_MODE 8
28 #define PNG_PUSH_SAVE_BUFFER_IF_FULL \
29 if (png_ptr->push_length + 4 > png_ptr->buffer_size) \
30 { png_push_save_buffer(png_ptr); return; }
31 #define PNG_PUSH_SAVE_BUFFER_IF_LT(N) \
32 if (png_ptr->buffer_size < N) \
33 { png_push_save_buffer(png_ptr); return; }
36 png_process_data(png_structrp png_ptr, png_inforp info_ptr,
37 png_bytep buffer, size_t buffer_size)
39 if (png_ptr == NULL || info_ptr == NULL)
42 png_push_restore_buffer(png_ptr, buffer, buffer_size);
44 while (png_ptr->buffer_size)
46 png_process_some_data(png_ptr, info_ptr);
51 png_process_data_pause(png_structrp png_ptr, int save)
55 /* It's easiest for the caller if we do the save; then the caller doesn't
56 * have to supply the same data again:
59 png_push_save_buffer(png_ptr);
62 /* This includes any pending saved bytes: */
63 size_t remaining = png_ptr->buffer_size;
64 png_ptr->buffer_size = 0;
66 /* So subtract the saved buffer size, unless all the data
67 * is actually 'saved', in which case we just return 0
69 if (png_ptr->save_buffer_size < remaining)
70 return remaining - png_ptr->save_buffer_size;
78 png_process_data_skip(png_structrp png_ptr)
80 /* TODO: Deprecate and remove this API.
81 * Somewhere the implementation of this seems to have been lost,
82 * or abandoned. It was only to support some internal back-door access
83 * to png_struct) in libpng-1.4.x.
85 png_app_warning(png_ptr,
86 "png_process_data_skip is not implemented in any current version of libpng");
90 /* What we do with the incoming data depends on what we were previously
91 * doing before we ran out of data...
94 png_process_some_data(png_structrp png_ptr, png_inforp info_ptr)
99 switch (png_ptr->process_mode)
101 case PNG_READ_SIG_MODE:
103 png_push_read_sig(png_ptr, info_ptr);
107 case PNG_READ_CHUNK_MODE:
109 png_push_read_chunk(png_ptr, info_ptr);
113 case PNG_READ_IDAT_MODE:
115 png_push_read_IDAT(png_ptr);
121 png_ptr->buffer_size = 0;
127 /* Read any remaining signature bytes from the stream and compare them with
128 * the correct PNG signature. It is possible that this routine is called
129 * with bytes already read from the signature, either because they have been
130 * checked by the calling application, or because of multiple calls to this
134 png_push_read_sig(png_structrp png_ptr, png_inforp info_ptr)
136 size_t num_checked = png_ptr->sig_bytes; /* SAFE, does not exceed 8 */
137 size_t num_to_check = 8 - num_checked;
139 if (png_ptr->buffer_size < num_to_check)
141 num_to_check = png_ptr->buffer_size;
144 png_push_fill_buffer(png_ptr, &(info_ptr->signature[num_checked]),
146 png_ptr->sig_bytes = (png_byte)(png_ptr->sig_bytes + num_to_check);
148 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
150 if (num_checked < 4 &&
151 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
152 png_error(png_ptr, "Not a PNG file");
155 png_error(png_ptr, "PNG file corrupted by ASCII conversion");
159 if (png_ptr->sig_bytes >= 8)
161 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
167 png_push_read_chunk(png_structrp png_ptr, png_inforp info_ptr)
169 png_uint_32 chunk_name;
170 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
171 int keep; /* unknown handling method */
174 /* First we make sure we have enough data for the 4-byte chunk name
175 * and the 4-byte chunk length before proceeding with decoding the
176 * chunk data. To fully decode each of these chunks, we also make
177 * sure we have enough data in the buffer for the 4-byte CRC at the
178 * end of every chunk (except IDAT, which is handled separately).
180 if ((png_ptr->mode & PNG_HAVE_CHUNK_HEADER) == 0)
182 png_byte chunk_length[4];
183 png_byte chunk_tag[4];
185 PNG_PUSH_SAVE_BUFFER_IF_LT(8)
186 png_push_fill_buffer(png_ptr, chunk_length, 4);
187 png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
188 png_reset_crc(png_ptr);
189 png_crc_read(png_ptr, chunk_tag, 4);
190 png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(chunk_tag);
191 png_check_chunk_name(png_ptr, png_ptr->chunk_name);
192 png_check_chunk_length(png_ptr, png_ptr->push_length);
193 png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
196 chunk_name = png_ptr->chunk_name;
198 #ifdef PNG_READ_APNG_SUPPORTED
199 if (png_ptr->num_frames_read > 0 &&
200 png_ptr->num_frames_read < info_ptr->num_frames)
202 if (chunk_name == png_IDAT)
204 /* Discard trailing IDATs for the first frame */
205 if (png_ptr->mode & PNG_HAVE_fcTL || png_ptr->num_frames_read > 1)
206 png_error(png_ptr, "out of place IDAT");
208 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
210 png_push_save_buffer(png_ptr);
214 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
217 else if (chunk_name == png_fdAT)
219 if (png_ptr->buffer_size < 4)
221 png_push_save_buffer(png_ptr);
225 png_ensure_sequence_number(png_ptr, 4);
227 if (!(png_ptr->mode & PNG_HAVE_fcTL))
229 /* Discard trailing fdATs for frames other than the first */
230 if (png_ptr->num_frames_read < 2)
231 png_error(png_ptr, "out of place fdAT");
233 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
235 png_push_save_buffer(png_ptr);
239 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
245 /* frame data follows */
246 png_ptr->idat_size = png_ptr->push_length - 4;
247 png_ptr->mode |= PNG_HAVE_IDAT;
248 png_ptr->process_mode = PNG_READ_IDAT_MODE;
254 else if (chunk_name == png_fcTL)
256 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
258 png_push_save_buffer(png_ptr);
262 png_read_reset(png_ptr);
263 png_ptr->mode &= ~PNG_HAVE_fcTL;
265 png_handle_fcTL(png_ptr, info_ptr, png_ptr->push_length);
267 if (!(png_ptr->mode & PNG_HAVE_fcTL))
268 png_error(png_ptr, "missing required fcTL chunk");
270 png_read_reinit(png_ptr, info_ptr);
271 png_progressive_read_reset(png_ptr);
273 if (png_ptr->frame_info_fn != NULL)
274 (*(png_ptr->frame_info_fn))(png_ptr, png_ptr->num_frames_read);
276 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
283 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
285 png_push_save_buffer(png_ptr);
288 png_warning(png_ptr, "Skipped (ignored) a chunk "
289 "between APNG chunks");
290 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
296 #endif /* PNG_READ_APNG_SUPPORTED */
298 if (chunk_name == png_IDAT)
300 if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
301 png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
303 /* If we reach an IDAT chunk, this means we have read all of the
304 * header chunks, and we can start reading the image (or if this
305 * is called after the image has been read - we have an error).
307 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
308 png_error(png_ptr, "Missing IHDR before IDAT");
310 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
311 (png_ptr->mode & PNG_HAVE_PLTE) == 0)
312 png_error(png_ptr, "Missing PLTE before IDAT");
314 png_ptr->process_mode = PNG_READ_IDAT_MODE;
316 if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
317 if ((png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) == 0)
318 if (png_ptr->push_length == 0)
321 png_ptr->mode |= PNG_HAVE_IDAT;
323 if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
324 png_benign_error(png_ptr, "Too many IDATs found");
327 if (chunk_name == png_IHDR)
329 if (png_ptr->push_length != 13)
330 png_error(png_ptr, "Invalid IHDR length");
332 PNG_PUSH_SAVE_BUFFER_IF_FULL
333 png_handle_IHDR(png_ptr, info_ptr, png_ptr->push_length);
336 else if (chunk_name == png_IEND)
338 PNG_PUSH_SAVE_BUFFER_IF_FULL
339 png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length);
341 png_ptr->process_mode = PNG_READ_DONE_MODE;
342 png_push_have_end(png_ptr, info_ptr);
345 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
346 else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
348 PNG_PUSH_SAVE_BUFFER_IF_FULL
349 png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length, keep);
351 if (chunk_name == png_PLTE)
352 png_ptr->mode |= PNG_HAVE_PLTE;
356 else if (chunk_name == png_PLTE)
358 PNG_PUSH_SAVE_BUFFER_IF_FULL
359 png_handle_PLTE(png_ptr, info_ptr, png_ptr->push_length);
362 else if (chunk_name == png_IDAT)
364 #ifdef PNG_READ_APNG_SUPPORTED
365 png_have_info(png_ptr, info_ptr);
367 png_ptr->idat_size = png_ptr->push_length;
368 png_ptr->process_mode = PNG_READ_IDAT_MODE;
369 png_push_have_info(png_ptr, info_ptr);
370 png_ptr->zstream.avail_out =
371 (uInt) PNG_ROWBYTES(png_ptr->pixel_depth,
372 png_ptr->iwidth) + 1;
373 png_ptr->zstream.next_out = png_ptr->row_buf;
377 #ifdef PNG_READ_gAMA_SUPPORTED
378 else if (png_ptr->chunk_name == png_gAMA)
380 PNG_PUSH_SAVE_BUFFER_IF_FULL
381 png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length);
385 #ifdef PNG_READ_sBIT_SUPPORTED
386 else if (png_ptr->chunk_name == png_sBIT)
388 PNG_PUSH_SAVE_BUFFER_IF_FULL
389 png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length);
393 #ifdef PNG_READ_cHRM_SUPPORTED
394 else if (png_ptr->chunk_name == png_cHRM)
396 PNG_PUSH_SAVE_BUFFER_IF_FULL
397 png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length);
401 #ifdef PNG_READ_sRGB_SUPPORTED
402 else if (chunk_name == png_sRGB)
404 PNG_PUSH_SAVE_BUFFER_IF_FULL
405 png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length);
409 #ifdef PNG_READ_iCCP_SUPPORTED
410 else if (png_ptr->chunk_name == png_iCCP)
412 PNG_PUSH_SAVE_BUFFER_IF_FULL
413 png_handle_iCCP(png_ptr, info_ptr, png_ptr->push_length);
417 #ifdef PNG_READ_sPLT_SUPPORTED
418 else if (chunk_name == png_sPLT)
420 PNG_PUSH_SAVE_BUFFER_IF_FULL
421 png_handle_sPLT(png_ptr, info_ptr, png_ptr->push_length);
425 #ifdef PNG_READ_tRNS_SUPPORTED
426 else if (chunk_name == png_tRNS)
428 PNG_PUSH_SAVE_BUFFER_IF_FULL
429 png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length);
433 #ifdef PNG_READ_bKGD_SUPPORTED
434 else if (chunk_name == png_bKGD)
436 PNG_PUSH_SAVE_BUFFER_IF_FULL
437 png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length);
441 #ifdef PNG_READ_hIST_SUPPORTED
442 else if (chunk_name == png_hIST)
444 PNG_PUSH_SAVE_BUFFER_IF_FULL
445 png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length);
449 #ifdef PNG_READ_pHYs_SUPPORTED
450 else if (chunk_name == png_pHYs)
452 PNG_PUSH_SAVE_BUFFER_IF_FULL
453 png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length);
457 #ifdef PNG_READ_oFFs_SUPPORTED
458 else if (chunk_name == png_oFFs)
460 PNG_PUSH_SAVE_BUFFER_IF_FULL
461 png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length);
465 #ifdef PNG_READ_pCAL_SUPPORTED
466 else if (chunk_name == png_pCAL)
468 PNG_PUSH_SAVE_BUFFER_IF_FULL
469 png_handle_pCAL(png_ptr, info_ptr, png_ptr->push_length);
473 #ifdef PNG_READ_sCAL_SUPPORTED
474 else if (chunk_name == png_sCAL)
476 PNG_PUSH_SAVE_BUFFER_IF_FULL
477 png_handle_sCAL(png_ptr, info_ptr, png_ptr->push_length);
481 #ifdef PNG_READ_tIME_SUPPORTED
482 else if (chunk_name == png_tIME)
484 PNG_PUSH_SAVE_BUFFER_IF_FULL
485 png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length);
489 #ifdef PNG_READ_tEXt_SUPPORTED
490 else if (chunk_name == png_tEXt)
492 PNG_PUSH_SAVE_BUFFER_IF_FULL
493 png_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length);
497 #ifdef PNG_READ_zTXt_SUPPORTED
498 else if (chunk_name == png_zTXt)
500 PNG_PUSH_SAVE_BUFFER_IF_FULL
501 png_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length);
505 #ifdef PNG_READ_iTXt_SUPPORTED
506 else if (chunk_name == png_iTXt)
508 PNG_PUSH_SAVE_BUFFER_IF_FULL
509 png_handle_iTXt(png_ptr, info_ptr, png_ptr->push_length);
512 #ifdef PNG_READ_APNG_SUPPORTED
513 else if (chunk_name == png_acTL)
515 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
517 png_push_save_buffer(png_ptr);
521 png_handle_acTL(png_ptr, info_ptr, png_ptr->push_length);
524 else if (chunk_name == png_fcTL)
526 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
528 png_push_save_buffer(png_ptr);
532 png_handle_fcTL(png_ptr, info_ptr, png_ptr->push_length);
535 #endif /* PNG_READ_APNG_SUPPORTED */
539 PNG_PUSH_SAVE_BUFFER_IF_FULL
540 png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length,
541 PNG_HANDLE_CHUNK_AS_DEFAULT);
544 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
548 png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, size_t length)
556 if (png_ptr->save_buffer_size != 0)
560 if (length < png_ptr->save_buffer_size)
564 save_size = png_ptr->save_buffer_size;
566 memcpy(ptr, png_ptr->save_buffer_ptr, save_size);
569 png_ptr->buffer_size -= save_size;
570 png_ptr->save_buffer_size -= save_size;
571 png_ptr->save_buffer_ptr += save_size;
573 if (length != 0 && png_ptr->current_buffer_size != 0)
577 if (length < png_ptr->current_buffer_size)
581 save_size = png_ptr->current_buffer_size;
583 memcpy(ptr, png_ptr->current_buffer_ptr, save_size);
584 png_ptr->buffer_size -= save_size;
585 png_ptr->current_buffer_size -= save_size;
586 png_ptr->current_buffer_ptr += save_size;
591 png_push_save_buffer(png_structrp png_ptr)
593 if (png_ptr->save_buffer_size != 0)
595 if (png_ptr->save_buffer_ptr != png_ptr->save_buffer)
601 istop = png_ptr->save_buffer_size;
602 for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer;
603 i < istop; i++, sp++, dp++)
609 if (png_ptr->save_buffer_size + png_ptr->current_buffer_size >
610 png_ptr->save_buffer_max)
613 png_bytep old_buffer;
615 if (png_ptr->save_buffer_size > PNG_SIZE_MAX -
616 (png_ptr->current_buffer_size + 256))
618 png_error(png_ptr, "Potential overflow of save_buffer");
621 new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;
622 old_buffer = png_ptr->save_buffer;
623 png_ptr->save_buffer = (png_bytep)png_malloc_warn(png_ptr,
626 if (png_ptr->save_buffer == NULL)
628 png_free(png_ptr, old_buffer);
629 png_error(png_ptr, "Insufficient memory for save_buffer");
633 memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
634 else if (png_ptr->save_buffer_size)
635 png_error(png_ptr, "save_buffer error");
636 png_free(png_ptr, old_buffer);
637 png_ptr->save_buffer_max = new_max;
639 if (png_ptr->current_buffer_size)
641 memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
642 png_ptr->current_buffer_ptr, png_ptr->current_buffer_size);
643 png_ptr->save_buffer_size += png_ptr->current_buffer_size;
644 png_ptr->current_buffer_size = 0;
646 png_ptr->save_buffer_ptr = png_ptr->save_buffer;
647 png_ptr->buffer_size = 0;
651 png_push_restore_buffer(png_structrp png_ptr, png_bytep buffer,
652 size_t buffer_length)
654 png_ptr->current_buffer = buffer;
655 png_ptr->current_buffer_size = buffer_length;
656 png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size;
657 png_ptr->current_buffer_ptr = png_ptr->current_buffer;
661 png_push_read_IDAT(png_structrp png_ptr)
663 if ((png_ptr->mode & PNG_HAVE_CHUNK_HEADER) == 0)
665 png_byte chunk_length[4];
666 png_byte chunk_tag[4];
668 /* TODO: this code can be commoned up with the same code in push_read */
669 #ifdef PNG_READ_APNG_SUPPORTED
670 PNG_PUSH_SAVE_BUFFER_IF_LT(12)
672 PNG_PUSH_SAVE_BUFFER_IF_LT(8)
674 png_push_fill_buffer(png_ptr, chunk_length, 4);
675 png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
676 png_reset_crc(png_ptr);
677 png_crc_read(png_ptr, chunk_tag, 4);
678 png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(chunk_tag);
679 png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
681 #ifdef PNG_READ_APNG_SUPPORTED
682 if (png_ptr->chunk_name != png_fdAT && png_ptr->num_frames_read > 0)
684 if (png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED)
686 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
687 if (png_ptr->frame_end_fn != NULL)
688 (*(png_ptr->frame_end_fn))(png_ptr, png_ptr->num_frames_read);
689 png_ptr->num_frames_read++;
694 if (png_ptr->chunk_name == png_IEND)
695 png_error(png_ptr, "Not enough image data");
696 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
698 png_push_save_buffer(png_ptr);
701 png_warning(png_ptr, "Skipping (ignoring) a chunk between "
703 png_crc_finish(png_ptr, png_ptr->push_length);
704 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
710 #ifdef PNG_READ_APNG_SUPPORTED
711 if (png_ptr->chunk_name != png_IDAT && png_ptr->num_frames_read == 0)
713 if (png_ptr->chunk_name != png_IDAT)
716 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
718 if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
719 png_error(png_ptr, "Not enough compressed data");
721 #ifdef PNG_READ_APNG_SUPPORTED
722 if (png_ptr->frame_end_fn != NULL)
723 (*(png_ptr->frame_end_fn))(png_ptr, png_ptr->num_frames_read);
724 png_ptr->num_frames_read++;
730 png_ptr->idat_size = png_ptr->push_length;
732 #ifdef PNG_READ_APNG_SUPPORTED
733 if (png_ptr->num_frames_read > 0)
735 png_ensure_sequence_number(png_ptr, 4);
736 png_ptr->idat_size -= 4;
741 if (png_ptr->idat_size != 0 && png_ptr->save_buffer_size != 0)
743 size_t save_size = png_ptr->save_buffer_size;
744 png_uint_32 idat_size = png_ptr->idat_size;
746 /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
747 * are of different types and we don't know which variable has the fewest
748 * bits. Carefully select the smaller and cast it to the type of the
749 * larger - this cannot overflow. Do not cast in the following test - it
750 * will break on either 16-bit or 64-bit platforms.
752 if (idat_size < save_size)
753 save_size = (size_t)idat_size;
756 idat_size = (png_uint_32)save_size;
758 png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
760 png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size);
762 png_ptr->idat_size -= idat_size;
763 png_ptr->buffer_size -= save_size;
764 png_ptr->save_buffer_size -= save_size;
765 png_ptr->save_buffer_ptr += save_size;
768 if (png_ptr->idat_size != 0 && png_ptr->current_buffer_size != 0)
770 size_t save_size = png_ptr->current_buffer_size;
771 png_uint_32 idat_size = png_ptr->idat_size;
773 /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
774 * are of different types and we don't know which variable has the fewest
775 * bits. Carefully select the smaller and cast it to the type of the
776 * larger - this cannot overflow.
778 if (idat_size < save_size)
779 save_size = (size_t)idat_size;
782 idat_size = (png_uint_32)save_size;
784 png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
786 png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size);
788 png_ptr->idat_size -= idat_size;
789 png_ptr->buffer_size -= save_size;
790 png_ptr->current_buffer_size -= save_size;
791 png_ptr->current_buffer_ptr += save_size;
794 if (png_ptr->idat_size == 0)
796 PNG_PUSH_SAVE_BUFFER_IF_LT(4)
797 png_crc_finish(png_ptr, 0);
798 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
799 png_ptr->mode |= PNG_AFTER_IDAT;
805 png_process_IDAT_data(png_structrp png_ptr, png_bytep buffer,
806 size_t buffer_length)
808 /* The caller checks for a non-zero buffer length. */
809 if (!(buffer_length > 0) || buffer == NULL)
810 png_error(png_ptr, "No IDAT data (internal error)");
812 #ifdef PNG_READ_APNG_SUPPORTED
813 /* If the app is not APNG-aware, decode only the first frame */
814 if (!(png_ptr->apng_flags & PNG_APNG_APP) && png_ptr->num_frames_read > 0)
816 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
821 /* This routine must process all the data it has been given
822 * before returning, calling the row callback as required to
823 * handle the uncompressed results.
825 png_ptr->zstream.next_in = buffer;
826 /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
827 png_ptr->zstream.avail_in = (uInt)buffer_length;
829 /* Keep going until the decompressed data is all processed
830 * or the stream marked as finished.
832 while (png_ptr->zstream.avail_in > 0 &&
833 (png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
837 /* We have data for zlib, but we must check that zlib
838 * has someplace to put the results. It doesn't matter
839 * if we don't expect any results -- it may be the input
840 * data is just the LZ end code.
842 if (!(png_ptr->zstream.avail_out > 0))
844 /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
845 png_ptr->zstream.avail_out = (uInt)(PNG_ROWBYTES(png_ptr->pixel_depth,
846 png_ptr->iwidth) + 1);
848 png_ptr->zstream.next_out = png_ptr->row_buf;
851 /* Using Z_SYNC_FLUSH here means that an unterminated
852 * LZ stream (a stream with a missing end code) can still
853 * be handled, otherwise (Z_NO_FLUSH) a future zlib
854 * implementation might defer output and therefore
855 * change the current behavior (see comments in inflate.c
856 * for why this doesn't happen at present with zlib 1.2.5).
858 ret = PNG_INFLATE(png_ptr, Z_SYNC_FLUSH);
860 /* Check for any failure before proceeding. */
861 if (ret != Z_OK && ret != Z_STREAM_END)
863 /* Terminate the decompression. */
864 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
867 /* This may be a truncated stream (missing or
868 * damaged end code). Treat that as a warning.
870 if (png_ptr->row_number >= png_ptr->num_rows ||
872 png_warning(png_ptr, "Truncated compressed data in IDAT");
876 if (ret == Z_DATA_ERROR)
877 png_benign_error(png_ptr, "IDAT: ADLER32 checksum mismatch");
879 png_error(png_ptr, "Decompression error in IDAT");
882 /* Skip the check on unprocessed input */
886 /* Did inflate output any data? */
887 if (png_ptr->zstream.next_out != png_ptr->row_buf)
889 /* Is this unexpected data after the last row?
890 * If it is, artificially terminate the LZ output
893 if (png_ptr->row_number >= png_ptr->num_rows ||
897 png_warning(png_ptr, "Extra compressed data in IDAT");
898 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
901 /* Do no more processing; skip the unprocessed
907 /* Do we have a complete row? */
908 if (png_ptr->zstream.avail_out == 0)
909 png_push_process_row(png_ptr);
912 /* And check for the end of the stream. */
913 if (ret == Z_STREAM_END)
914 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
917 /* All the data should have been processed, if anything
918 * is left at this point we have bytes of IDAT data
919 * after the zlib end code.
921 if (png_ptr->zstream.avail_in > 0)
922 png_warning(png_ptr, "Extra compression data in IDAT");
926 png_push_process_row(png_structrp png_ptr)
928 /* 1.5.6: row_info moved out of png_struct to a local here. */
929 png_row_info row_info;
931 row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */
932 row_info.color_type = png_ptr->color_type;
933 row_info.bit_depth = png_ptr->bit_depth;
934 row_info.channels = png_ptr->channels;
935 row_info.pixel_depth = png_ptr->pixel_depth;
936 row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
938 if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
940 if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
941 png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1,
942 png_ptr->prev_row + 1, png_ptr->row_buf[0]);
944 png_error(png_ptr, "bad adaptive filter value");
947 /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
948 * 1.5.6, while the buffer really is this big in current versions of libpng
949 * it may not be in the future, so this was changed just to copy the
950 * interlaced row count:
952 memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
954 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
955 if (png_ptr->transformations != 0)
956 png_do_read_transformations(png_ptr, &row_info);
959 /* The transformed pixel depth should match the depth now in row_info. */
960 if (png_ptr->transformed_pixel_depth == 0)
962 png_ptr->transformed_pixel_depth = row_info.pixel_depth;
963 if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
964 png_error(png_ptr, "progressive row overflow");
967 else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
968 png_error(png_ptr, "internal progressive row size calculation error");
971 #ifdef PNG_READ_INTERLACING_SUPPORTED
972 /* Expand interlaced rows to full size */
973 if (png_ptr->interlaced != 0 &&
974 (png_ptr->transformations & PNG_INTERLACE) != 0)
976 if (png_ptr->pass < 6)
977 png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
978 png_ptr->transformations);
980 switch (png_ptr->pass)
985 for (i = 0; i < 8 && png_ptr->pass == 0; i++)
987 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
988 png_read_push_finish_row(png_ptr); /* Updates png_ptr->pass */
991 if (png_ptr->pass == 2) /* Pass 1 might be empty */
993 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
995 png_push_have_row(png_ptr, NULL);
996 png_read_push_finish_row(png_ptr);
1000 if (png_ptr->pass == 4 && png_ptr->height <= 4)
1002 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1004 png_push_have_row(png_ptr, NULL);
1005 png_read_push_finish_row(png_ptr);
1009 if (png_ptr->pass == 6 && png_ptr->height <= 4)
1011 png_push_have_row(png_ptr, NULL);
1012 png_read_push_finish_row(png_ptr);
1021 for (i = 0; i < 8 && png_ptr->pass == 1; i++)
1023 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1024 png_read_push_finish_row(png_ptr);
1027 if (png_ptr->pass == 2) /* Skip top 4 generated rows */
1029 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
1031 png_push_have_row(png_ptr, NULL);
1032 png_read_push_finish_row(png_ptr);
1043 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
1045 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1046 png_read_push_finish_row(png_ptr);
1049 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
1051 png_push_have_row(png_ptr, NULL);
1052 png_read_push_finish_row(png_ptr);
1055 if (png_ptr->pass == 4) /* Pass 3 might be empty */
1057 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1059 png_push_have_row(png_ptr, NULL);
1060 png_read_push_finish_row(png_ptr);
1071 for (i = 0; i < 4 && png_ptr->pass == 3; i++)
1073 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1074 png_read_push_finish_row(png_ptr);
1077 if (png_ptr->pass == 4) /* Skip top two generated rows */
1079 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1081 png_push_have_row(png_ptr, NULL);
1082 png_read_push_finish_row(png_ptr);
1093 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1095 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1096 png_read_push_finish_row(png_ptr);
1099 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1101 png_push_have_row(png_ptr, NULL);
1102 png_read_push_finish_row(png_ptr);
1105 if (png_ptr->pass == 6) /* Pass 5 might be empty */
1107 png_push_have_row(png_ptr, NULL);
1108 png_read_push_finish_row(png_ptr);
1118 for (i = 0; i < 2 && png_ptr->pass == 5; i++)
1120 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1121 png_read_push_finish_row(png_ptr);
1124 if (png_ptr->pass == 6) /* Skip top generated row */
1126 png_push_have_row(png_ptr, NULL);
1127 png_read_push_finish_row(png_ptr);
1136 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1137 png_read_push_finish_row(png_ptr);
1139 if (png_ptr->pass != 6)
1142 png_push_have_row(png_ptr, NULL);
1143 png_read_push_finish_row(png_ptr);
1150 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1151 png_read_push_finish_row(png_ptr);
1156 png_read_push_finish_row(png_structrp png_ptr)
1158 #ifdef PNG_READ_INTERLACING_SUPPORTED
1159 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1161 /* Start of interlace block */
1162 static const png_byte png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
1164 /* Offset to next interlace block */
1165 static const png_byte png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
1167 /* Start of interlace block in the y direction */
1168 static const png_byte png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
1170 /* Offset to next interlace block in the y direction */
1171 static const png_byte png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
1173 /* Height of interlace block. This is not currently used - if you need
1174 * it, uncomment it here and in png.h
1175 static const png_byte png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
1179 png_ptr->row_number++;
1180 if (png_ptr->row_number < png_ptr->num_rows)
1183 #ifdef PNG_READ_INTERLACING_SUPPORTED
1184 if (png_ptr->interlaced != 0)
1186 png_ptr->row_number = 0;
1187 memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
1192 if ((png_ptr->pass == 1 && png_ptr->width < 5) ||
1193 (png_ptr->pass == 3 && png_ptr->width < 3) ||
1194 (png_ptr->pass == 5 && png_ptr->width < 2))
1197 if (png_ptr->pass > 7)
1200 if (png_ptr->pass >= 7)
1203 png_ptr->iwidth = (png_ptr->width +
1204 png_pass_inc[png_ptr->pass] - 1 -
1205 png_pass_start[png_ptr->pass]) /
1206 png_pass_inc[png_ptr->pass];
1208 if ((png_ptr->transformations & PNG_INTERLACE) != 0)
1211 png_ptr->num_rows = (png_ptr->height +
1212 png_pass_yinc[png_ptr->pass] - 1 -
1213 png_pass_ystart[png_ptr->pass]) /
1214 png_pass_yinc[png_ptr->pass];
1216 } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0);
1218 #endif /* READ_INTERLACING */
1222 png_push_have_info(png_structrp png_ptr, png_inforp info_ptr)
1224 if (png_ptr->info_fn != NULL)
1225 (*(png_ptr->info_fn))(png_ptr, info_ptr);
1229 png_push_have_end(png_structrp png_ptr, png_inforp info_ptr)
1231 if (png_ptr->end_fn != NULL)
1232 (*(png_ptr->end_fn))(png_ptr, info_ptr);
1236 png_push_have_row(png_structrp png_ptr, png_bytep row)
1238 if (png_ptr->row_fn != NULL)
1239 (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number,
1240 (int)png_ptr->pass);
1243 #ifdef PNG_READ_INTERLACING_SUPPORTED
1245 png_progressive_combine_row(png_const_structrp png_ptr, png_bytep old_row,
1246 png_const_bytep new_row)
1248 if (png_ptr == NULL)
1251 /* new_row is a flag here - if it is NULL then the app callback was called
1252 * from an empty row (see the calls to png_struct::row_fn below), otherwise
1253 * it must be png_ptr->row_buf+1
1255 if (new_row != NULL)
1256 png_combine_row(png_ptr, old_row, 1/*blocky display*/);
1258 #endif /* READ_INTERLACING */
1261 png_set_progressive_read_fn(png_structrp png_ptr, png_voidp progressive_ptr,
1262 png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
1263 png_progressive_end_ptr end_fn)
1265 if (png_ptr == NULL)
1268 png_ptr->info_fn = info_fn;
1269 png_ptr->row_fn = row_fn;
1270 png_ptr->end_fn = end_fn;
1272 png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer);
1275 #ifdef PNG_READ_APNG_SUPPORTED
1277 png_set_progressive_frame_fn(png_structp png_ptr,
1278 png_progressive_frame_ptr frame_info_fn,
1279 png_progressive_frame_ptr frame_end_fn)
1281 png_ptr->frame_info_fn = frame_info_fn;
1282 png_ptr->frame_end_fn = frame_end_fn;
1283 png_ptr->apng_flags |= PNG_APNG_APP;
1288 png_get_progressive_ptr(png_const_structrp png_ptr)
1290 if (png_ptr == NULL)
1293 return png_ptr->io_ptr;
1295 #endif /* PROGRESSIVE_READ */