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
) != 0 ||
206 png_ptr
->num_frames_read
> 1)
207 png_error(png_ptr
, "out of place IDAT");
209 PNG_PUSH_SAVE_BUFFER_IF_FULL
210 png_crc_finish(png_ptr
, png_ptr
->push_length
);
211 png_ptr
->mode
&= ~PNG_HAVE_CHUNK_HEADER
;
214 else if (chunk_name
== png_fdAT
)
216 PNG_PUSH_SAVE_BUFFER_IF_LT(4)
217 png_ensure_sequence_number(png_ptr
, 4);
219 if ((png_ptr
->mode
& PNG_HAVE_fcTL
) == 0)
221 /* Discard trailing fdATs for frames other than the first */
222 if (png_ptr
->num_frames_read
< 2)
223 png_error(png_ptr
, "out of place fdAT");
225 PNG_PUSH_SAVE_BUFFER_IF_FULL
226 png_crc_finish(png_ptr
, png_ptr
->push_length
);
227 png_ptr
->mode
&= ~PNG_HAVE_CHUNK_HEADER
;
232 /* frame data follows */
233 png_ptr
->idat_size
= png_ptr
->push_length
- 4;
234 png_ptr
->mode
|= PNG_HAVE_IDAT
;
235 png_ptr
->process_mode
= PNG_READ_IDAT_MODE
;
239 else if (chunk_name
== png_fcTL
)
241 PNG_PUSH_SAVE_BUFFER_IF_FULL
242 png_read_reset(png_ptr
);
243 png_ptr
->mode
&= ~PNG_HAVE_fcTL
;
245 png_handle_fcTL(png_ptr
, info_ptr
, png_ptr
->push_length
);
247 if ((png_ptr
->mode
& PNG_HAVE_fcTL
) == 0)
248 png_error(png_ptr
, "missing required fcTL chunk");
250 png_read_reinit(png_ptr
, info_ptr
);
251 png_progressive_read_reset(png_ptr
);
253 if (png_ptr
->frame_info_fn
!= NULL
)
254 (*(png_ptr
->frame_info_fn
))(png_ptr
, png_ptr
->num_frames_read
);
256 png_ptr
->mode
&= ~PNG_HAVE_CHUNK_HEADER
;
259 else if (chunk_name
== png_IEND
)
261 PNG_PUSH_SAVE_BUFFER_IF_FULL
262 png_warning(png_ptr
, "Number of actual frames fewer than expected");
263 png_crc_finish(png_ptr
, png_ptr
->push_length
);
264 png_ptr
->process_mode
= PNG_READ_DONE_MODE
;
265 png_push_have_end(png_ptr
, info_ptr
);
270 PNG_PUSH_SAVE_BUFFER_IF_FULL
271 png_warning(png_ptr
, "Skipped (ignored) a chunk "
272 "between APNG chunks");
273 png_crc_finish(png_ptr
, png_ptr
->push_length
);
274 png_ptr
->mode
&= ~PNG_HAVE_CHUNK_HEADER
;
279 #endif /* READ_APNG */
281 if (chunk_name
== png_IDAT
)
283 if ((png_ptr
->mode
& PNG_AFTER_IDAT
) != 0)
284 png_ptr
->mode
|= PNG_HAVE_CHUNK_AFTER_IDAT
;
286 /* If we reach an IDAT chunk, this means we have read all of the
287 * header chunks, and we can start reading the image (or if this
288 * is called after the image has been read - we have an error).
290 if ((png_ptr
->mode
& PNG_HAVE_IHDR
) == 0)
291 png_error(png_ptr
, "Missing IHDR before IDAT");
293 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
&&
294 (png_ptr
->mode
& PNG_HAVE_PLTE
) == 0)
295 png_error(png_ptr
, "Missing PLTE before IDAT");
297 png_ptr
->process_mode
= PNG_READ_IDAT_MODE
;
299 if ((png_ptr
->mode
& PNG_HAVE_IDAT
) != 0)
300 if ((png_ptr
->mode
& PNG_HAVE_CHUNK_AFTER_IDAT
) == 0)
301 if (png_ptr
->push_length
== 0)
304 png_ptr
->mode
|= PNG_HAVE_IDAT
;
306 if ((png_ptr
->mode
& PNG_AFTER_IDAT
) != 0)
307 png_benign_error(png_ptr
, "Too many IDATs found");
310 if (chunk_name
== png_IHDR
)
312 if (png_ptr
->push_length
!= 13)
313 png_error(png_ptr
, "Invalid IHDR length");
315 PNG_PUSH_SAVE_BUFFER_IF_FULL
316 png_handle_IHDR(png_ptr
, info_ptr
, png_ptr
->push_length
);
319 else if (chunk_name
== png_IEND
)
321 PNG_PUSH_SAVE_BUFFER_IF_FULL
322 png_handle_IEND(png_ptr
, info_ptr
, png_ptr
->push_length
);
324 png_ptr
->process_mode
= PNG_READ_DONE_MODE
;
325 png_push_have_end(png_ptr
, info_ptr
);
328 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
329 else if ((keep
= png_chunk_unknown_handling(png_ptr
, chunk_name
)) != 0)
331 PNG_PUSH_SAVE_BUFFER_IF_FULL
332 png_handle_unknown(png_ptr
, info_ptr
, png_ptr
->push_length
, keep
);
334 if (chunk_name
== png_PLTE
)
335 png_ptr
->mode
|= PNG_HAVE_PLTE
;
339 else if (chunk_name
== png_PLTE
)
341 PNG_PUSH_SAVE_BUFFER_IF_FULL
342 png_handle_PLTE(png_ptr
, info_ptr
, png_ptr
->push_length
);
345 else if (chunk_name
== png_IDAT
)
347 #ifdef PNG_READ_APNG_SUPPORTED
348 png_have_info(png_ptr
, info_ptr
);
350 png_ptr
->idat_size
= png_ptr
->push_length
;
351 png_ptr
->process_mode
= PNG_READ_IDAT_MODE
;
352 png_push_have_info(png_ptr
, info_ptr
);
353 png_ptr
->zstream
.avail_out
=
354 (uInt
) PNG_ROWBYTES(png_ptr
->pixel_depth
,
355 png_ptr
->iwidth
) + 1;
356 png_ptr
->zstream
.next_out
= png_ptr
->row_buf
;
360 #ifdef PNG_READ_gAMA_SUPPORTED
361 else if (png_ptr
->chunk_name
== png_gAMA
)
363 PNG_PUSH_SAVE_BUFFER_IF_FULL
364 png_handle_gAMA(png_ptr
, info_ptr
, png_ptr
->push_length
);
368 #ifdef PNG_READ_sBIT_SUPPORTED
369 else if (png_ptr
->chunk_name
== png_sBIT
)
371 PNG_PUSH_SAVE_BUFFER_IF_FULL
372 png_handle_sBIT(png_ptr
, info_ptr
, png_ptr
->push_length
);
376 #ifdef PNG_READ_cHRM_SUPPORTED
377 else if (png_ptr
->chunk_name
== png_cHRM
)
379 PNG_PUSH_SAVE_BUFFER_IF_FULL
380 png_handle_cHRM(png_ptr
, info_ptr
, png_ptr
->push_length
);
384 #ifdef PNG_READ_sRGB_SUPPORTED
385 else if (chunk_name
== png_sRGB
)
387 PNG_PUSH_SAVE_BUFFER_IF_FULL
388 png_handle_sRGB(png_ptr
, info_ptr
, png_ptr
->push_length
);
392 #ifdef PNG_READ_iCCP_SUPPORTED
393 else if (png_ptr
->chunk_name
== png_iCCP
)
395 PNG_PUSH_SAVE_BUFFER_IF_FULL
396 png_handle_iCCP(png_ptr
, info_ptr
, png_ptr
->push_length
);
400 #ifdef PNG_READ_sPLT_SUPPORTED
401 else if (chunk_name
== png_sPLT
)
403 PNG_PUSH_SAVE_BUFFER_IF_FULL
404 png_handle_sPLT(png_ptr
, info_ptr
, png_ptr
->push_length
);
408 #ifdef PNG_READ_tRNS_SUPPORTED
409 else if (chunk_name
== png_tRNS
)
411 PNG_PUSH_SAVE_BUFFER_IF_FULL
412 png_handle_tRNS(png_ptr
, info_ptr
, png_ptr
->push_length
);
416 #ifdef PNG_READ_bKGD_SUPPORTED
417 else if (chunk_name
== png_bKGD
)
419 PNG_PUSH_SAVE_BUFFER_IF_FULL
420 png_handle_bKGD(png_ptr
, info_ptr
, png_ptr
->push_length
);
424 #ifdef PNG_READ_hIST_SUPPORTED
425 else if (chunk_name
== png_hIST
)
427 PNG_PUSH_SAVE_BUFFER_IF_FULL
428 png_handle_hIST(png_ptr
, info_ptr
, png_ptr
->push_length
);
432 #ifdef PNG_READ_pHYs_SUPPORTED
433 else if (chunk_name
== png_pHYs
)
435 PNG_PUSH_SAVE_BUFFER_IF_FULL
436 png_handle_pHYs(png_ptr
, info_ptr
, png_ptr
->push_length
);
440 #ifdef PNG_READ_oFFs_SUPPORTED
441 else if (chunk_name
== png_oFFs
)
443 PNG_PUSH_SAVE_BUFFER_IF_FULL
444 png_handle_oFFs(png_ptr
, info_ptr
, png_ptr
->push_length
);
448 #ifdef PNG_READ_pCAL_SUPPORTED
449 else if (chunk_name
== png_pCAL
)
451 PNG_PUSH_SAVE_BUFFER_IF_FULL
452 png_handle_pCAL(png_ptr
, info_ptr
, png_ptr
->push_length
);
456 #ifdef PNG_READ_sCAL_SUPPORTED
457 else if (chunk_name
== png_sCAL
)
459 PNG_PUSH_SAVE_BUFFER_IF_FULL
460 png_handle_sCAL(png_ptr
, info_ptr
, png_ptr
->push_length
);
464 #ifdef PNG_READ_tIME_SUPPORTED
465 else if (chunk_name
== png_tIME
)
467 PNG_PUSH_SAVE_BUFFER_IF_FULL
468 png_handle_tIME(png_ptr
, info_ptr
, png_ptr
->push_length
);
472 #ifdef PNG_READ_tEXt_SUPPORTED
473 else if (chunk_name
== png_tEXt
)
475 PNG_PUSH_SAVE_BUFFER_IF_FULL
476 png_handle_tEXt(png_ptr
, info_ptr
, png_ptr
->push_length
);
480 #ifdef PNG_READ_zTXt_SUPPORTED
481 else if (chunk_name
== png_zTXt
)
483 PNG_PUSH_SAVE_BUFFER_IF_FULL
484 png_handle_zTXt(png_ptr
, info_ptr
, png_ptr
->push_length
);
488 #ifdef PNG_READ_iTXt_SUPPORTED
489 else if (chunk_name
== png_iTXt
)
491 PNG_PUSH_SAVE_BUFFER_IF_FULL
492 png_handle_iTXt(png_ptr
, info_ptr
, png_ptr
->push_length
);
496 #ifdef PNG_READ_APNG_SUPPORTED
497 else if (chunk_name
== png_acTL
)
499 PNG_PUSH_SAVE_BUFFER_IF_FULL
500 png_handle_acTL(png_ptr
, info_ptr
, png_ptr
->push_length
);
503 else if (chunk_name
== png_fcTL
)
505 PNG_PUSH_SAVE_BUFFER_IF_FULL
506 png_handle_fcTL(png_ptr
, info_ptr
, png_ptr
->push_length
);
509 #endif /* READ_APNG */
512 PNG_PUSH_SAVE_BUFFER_IF_FULL
513 png_handle_unknown(png_ptr
, info_ptr
, png_ptr
->push_length
,
514 PNG_HANDLE_CHUNK_AS_DEFAULT
);
517 png_ptr
->mode
&= ~PNG_HAVE_CHUNK_HEADER
;
521 png_push_fill_buffer(png_structp png_ptr
, png_bytep buffer
, size_t length
)
529 if (png_ptr
->save_buffer_size
!= 0)
533 if (length
< png_ptr
->save_buffer_size
)
537 save_size
= png_ptr
->save_buffer_size
;
539 memcpy(ptr
, png_ptr
->save_buffer_ptr
, save_size
);
542 png_ptr
->buffer_size
-= save_size
;
543 png_ptr
->save_buffer_size
-= save_size
;
544 png_ptr
->save_buffer_ptr
+= save_size
;
546 if (length
!= 0 && png_ptr
->current_buffer_size
!= 0)
550 if (length
< png_ptr
->current_buffer_size
)
554 save_size
= png_ptr
->current_buffer_size
;
556 memcpy(ptr
, png_ptr
->current_buffer_ptr
, save_size
);
557 png_ptr
->buffer_size
-= save_size
;
558 png_ptr
->current_buffer_size
-= save_size
;
559 png_ptr
->current_buffer_ptr
+= save_size
;
564 png_push_save_buffer(png_structrp png_ptr
)
566 if (png_ptr
->save_buffer_size
!= 0)
568 if (png_ptr
->save_buffer_ptr
!= png_ptr
->save_buffer
)
574 istop
= png_ptr
->save_buffer_size
;
575 for (i
= 0, sp
= png_ptr
->save_buffer_ptr
, dp
= png_ptr
->save_buffer
;
576 i
< istop
; i
++, sp
++, dp
++)
582 if (png_ptr
->save_buffer_size
+ png_ptr
->current_buffer_size
>
583 png_ptr
->save_buffer_max
)
586 png_bytep old_buffer
;
588 if (png_ptr
->save_buffer_size
> PNG_SIZE_MAX
-
589 (png_ptr
->current_buffer_size
+ 256))
591 png_error(png_ptr
, "Potential overflow of save_buffer");
594 new_max
= png_ptr
->save_buffer_size
+ png_ptr
->current_buffer_size
+ 256;
595 old_buffer
= png_ptr
->save_buffer
;
596 png_ptr
->save_buffer
= (png_bytep
)png_malloc_warn(png_ptr
,
599 if (png_ptr
->save_buffer
== NULL
)
601 png_free(png_ptr
, old_buffer
);
602 png_error(png_ptr
, "Insufficient memory for save_buffer");
606 memcpy(png_ptr
->save_buffer
, old_buffer
, png_ptr
->save_buffer_size
);
607 else if (png_ptr
->save_buffer_size
)
608 png_error(png_ptr
, "save_buffer error");
609 png_free(png_ptr
, old_buffer
);
610 png_ptr
->save_buffer_max
= new_max
;
612 if (png_ptr
->current_buffer_size
)
614 memcpy(png_ptr
->save_buffer
+ png_ptr
->save_buffer_size
,
615 png_ptr
->current_buffer_ptr
, png_ptr
->current_buffer_size
);
616 png_ptr
->save_buffer_size
+= png_ptr
->current_buffer_size
;
617 png_ptr
->current_buffer_size
= 0;
619 png_ptr
->save_buffer_ptr
= png_ptr
->save_buffer
;
620 png_ptr
->buffer_size
= 0;
624 png_push_restore_buffer(png_structrp png_ptr
, png_bytep buffer
,
625 size_t buffer_length
)
627 png_ptr
->current_buffer
= buffer
;
628 png_ptr
->current_buffer_size
= buffer_length
;
629 png_ptr
->buffer_size
= buffer_length
+ png_ptr
->save_buffer_size
;
630 png_ptr
->current_buffer_ptr
= png_ptr
->current_buffer
;
634 png_push_read_IDAT(png_structrp png_ptr
)
636 if ((png_ptr
->mode
& PNG_HAVE_CHUNK_HEADER
) == 0)
638 png_byte chunk_length
[4];
639 png_byte chunk_tag
[4];
641 /* TODO: this code can be commoned up with the same code in push_read */
642 #ifdef PNG_READ_APNG_SUPPORTED
643 PNG_PUSH_SAVE_BUFFER_IF_LT(12)
645 PNG_PUSH_SAVE_BUFFER_IF_LT(8)
647 png_push_fill_buffer(png_ptr
, chunk_length
, 4);
648 png_ptr
->push_length
= png_get_uint_31(png_ptr
, chunk_length
);
649 png_reset_crc(png_ptr
);
650 png_crc_read(png_ptr
, chunk_tag
, 4);
651 png_ptr
->chunk_name
= PNG_CHUNK_FROM_STRING(chunk_tag
);
652 png_ptr
->mode
|= PNG_HAVE_CHUNK_HEADER
;
654 #ifdef PNG_READ_APNG_SUPPORTED
655 if (png_ptr
->chunk_name
!= png_fdAT
&& png_ptr
->num_frames_read
> 0)
657 if ((png_ptr
->flags
& PNG_FLAG_ZSTREAM_ENDED
) != 0)
659 png_ptr
->process_mode
= PNG_READ_CHUNK_MODE
;
660 if (png_ptr
->frame_end_fn
!= NULL
)
661 (*(png_ptr
->frame_end_fn
))(png_ptr
, png_ptr
->num_frames_read
);
662 png_ptr
->num_frames_read
++;
667 if (png_ptr
->chunk_name
== png_IEND
)
668 png_error(png_ptr
, "Not enough image data");
669 PNG_PUSH_SAVE_BUFFER_IF_FULL
670 png_warning(png_ptr
, "Skipping (ignoring) a chunk between "
672 png_crc_finish(png_ptr
, png_ptr
->push_length
);
673 png_ptr
->mode
&= ~PNG_HAVE_CHUNK_HEADER
;
679 #ifdef PNG_READ_APNG_SUPPORTED
680 if (png_ptr
->chunk_name
!= png_IDAT
&& png_ptr
->num_frames_read
== 0)
682 if (png_ptr
->chunk_name
!= png_IDAT
)
685 png_ptr
->process_mode
= PNG_READ_CHUNK_MODE
;
687 if ((png_ptr
->flags
& PNG_FLAG_ZSTREAM_ENDED
) == 0)
688 png_error(png_ptr
, "Not enough compressed data");
690 #ifdef PNG_READ_APNG_SUPPORTED
691 if (png_ptr
->frame_end_fn
!= NULL
)
692 (*(png_ptr
->frame_end_fn
))(png_ptr
, png_ptr
->num_frames_read
);
693 png_ptr
->num_frames_read
++;
699 png_ptr
->idat_size
= png_ptr
->push_length
;
701 #ifdef PNG_READ_APNG_SUPPORTED
702 if (png_ptr
->num_frames_read
> 0)
704 png_ensure_sequence_number(png_ptr
, 4);
705 png_ptr
->idat_size
-= 4;
710 if (png_ptr
->idat_size
!= 0 && png_ptr
->save_buffer_size
!= 0)
712 size_t save_size
= png_ptr
->save_buffer_size
;
713 png_uint_32 idat_size
= png_ptr
->idat_size
;
715 /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
716 * are of different types and we don't know which variable has the fewest
717 * bits. Carefully select the smaller and cast it to the type of the
718 * larger - this cannot overflow. Do not cast in the following test - it
719 * will break on either 16-bit or 64-bit platforms.
721 if (idat_size
< save_size
)
722 save_size
= (size_t)idat_size
;
725 idat_size
= (png_uint_32
)save_size
;
727 png_calculate_crc(png_ptr
, png_ptr
->save_buffer_ptr
, save_size
);
729 png_process_IDAT_data(png_ptr
, png_ptr
->save_buffer_ptr
, save_size
);
731 png_ptr
->idat_size
-= idat_size
;
732 png_ptr
->buffer_size
-= save_size
;
733 png_ptr
->save_buffer_size
-= save_size
;
734 png_ptr
->save_buffer_ptr
+= save_size
;
737 if (png_ptr
->idat_size
!= 0 && png_ptr
->current_buffer_size
!= 0)
739 size_t save_size
= png_ptr
->current_buffer_size
;
740 png_uint_32 idat_size
= png_ptr
->idat_size
;
742 /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
743 * are of different types and we don't know which variable has the fewest
744 * bits. Carefully select the smaller and cast it to the type of the
745 * larger - this cannot overflow.
747 if (idat_size
< save_size
)
748 save_size
= (size_t)idat_size
;
751 idat_size
= (png_uint_32
)save_size
;
753 png_calculate_crc(png_ptr
, png_ptr
->current_buffer_ptr
, save_size
);
755 png_process_IDAT_data(png_ptr
, png_ptr
->current_buffer_ptr
, save_size
);
757 png_ptr
->idat_size
-= idat_size
;
758 png_ptr
->buffer_size
-= save_size
;
759 png_ptr
->current_buffer_size
-= save_size
;
760 png_ptr
->current_buffer_ptr
+= save_size
;
763 if (png_ptr
->idat_size
== 0)
765 PNG_PUSH_SAVE_BUFFER_IF_LT(4)
766 png_crc_finish(png_ptr
, 0);
767 png_ptr
->mode
&= ~PNG_HAVE_CHUNK_HEADER
;
768 png_ptr
->mode
|= PNG_AFTER_IDAT
;
774 png_process_IDAT_data(png_structrp png_ptr
, png_bytep buffer
,
775 size_t buffer_length
)
777 /* The caller checks for a non-zero buffer length. */
778 if (!(buffer_length
> 0) || buffer
== NULL
)
779 png_error(png_ptr
, "No IDAT data (internal error)");
781 #ifdef PNG_READ_APNG_SUPPORTED
782 /* If the app is not APNG-aware, decode only the first frame */
783 if ((png_ptr
->apng_flags
& PNG_APNG_APP
) == 0 &&
784 png_ptr
->num_frames_read
> 0)
786 png_ptr
->flags
|= PNG_FLAG_ZSTREAM_ENDED
;
791 /* This routine must process all the data it has been given
792 * before returning, calling the row callback as required to
793 * handle the uncompressed results.
795 png_ptr
->zstream
.next_in
= buffer
;
796 /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
797 png_ptr
->zstream
.avail_in
= (uInt
)buffer_length
;
799 /* Keep going until the decompressed data is all processed
800 * or the stream marked as finished.
802 while (png_ptr
->zstream
.avail_in
> 0 &&
803 (png_ptr
->flags
& PNG_FLAG_ZSTREAM_ENDED
) == 0)
807 /* We have data for zlib, but we must check that zlib
808 * has someplace to put the results. It doesn't matter
809 * if we don't expect any results -- it may be the input
810 * data is just the LZ end code.
812 if (!(png_ptr
->zstream
.avail_out
> 0))
814 /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
815 png_ptr
->zstream
.avail_out
= (uInt
)(PNG_ROWBYTES(png_ptr
->pixel_depth
,
816 png_ptr
->iwidth
) + 1);
818 png_ptr
->zstream
.next_out
= png_ptr
->row_buf
;
821 /* Using Z_SYNC_FLUSH here means that an unterminated
822 * LZ stream (a stream with a missing end code) can still
823 * be handled, otherwise (Z_NO_FLUSH) a future zlib
824 * implementation might defer output and therefore
825 * change the current behavior (see comments in inflate.c
826 * for why this doesn't happen at present with zlib 1.2.5).
828 ret
= PNG_INFLATE(png_ptr
, Z_SYNC_FLUSH
);
830 /* Check for any failure before proceeding. */
831 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
)
833 /* Terminate the decompression. */
834 png_ptr
->flags
|= PNG_FLAG_ZSTREAM_ENDED
;
837 /* This may be a truncated stream (missing or
838 * damaged end code). Treat that as a warning.
840 if (png_ptr
->row_number
>= png_ptr
->num_rows
||
842 png_warning(png_ptr
, "Truncated compressed data in IDAT");
846 if (ret
== Z_DATA_ERROR
)
847 png_benign_error(png_ptr
, "IDAT: ADLER32 checksum mismatch");
849 png_error(png_ptr
, "Decompression error in IDAT");
852 /* Skip the check on unprocessed input */
856 /* Did inflate output any data? */
857 if (png_ptr
->zstream
.next_out
!= png_ptr
->row_buf
)
859 /* Is this unexpected data after the last row?
860 * If it is, artificially terminate the LZ output
863 if (png_ptr
->row_number
>= png_ptr
->num_rows
||
867 png_warning(png_ptr
, "Extra compressed data in IDAT");
868 png_ptr
->flags
|= PNG_FLAG_ZSTREAM_ENDED
;
871 /* Do no more processing; skip the unprocessed
877 /* Do we have a complete row? */
878 if (png_ptr
->zstream
.avail_out
== 0)
879 png_push_process_row(png_ptr
);
882 /* And check for the end of the stream. */
883 if (ret
== Z_STREAM_END
)
884 png_ptr
->flags
|= PNG_FLAG_ZSTREAM_ENDED
;
887 /* All the data should have been processed, if anything
888 * is left at this point we have bytes of IDAT data
889 * after the zlib end code.
891 if (png_ptr
->zstream
.avail_in
> 0)
892 png_warning(png_ptr
, "Extra compression data in IDAT");
896 png_push_process_row(png_structrp png_ptr
)
898 /* 1.5.6: row_info moved out of png_struct to a local here. */
899 png_row_info row_info
;
901 row_info
.width
= png_ptr
->iwidth
; /* NOTE: width of current interlaced row */
902 row_info
.color_type
= png_ptr
->color_type
;
903 row_info
.bit_depth
= png_ptr
->bit_depth
;
904 row_info
.channels
= png_ptr
->channels
;
905 row_info
.pixel_depth
= png_ptr
->pixel_depth
;
906 row_info
.rowbytes
= PNG_ROWBYTES(row_info
.pixel_depth
, row_info
.width
);
908 if (png_ptr
->row_buf
[0] > PNG_FILTER_VALUE_NONE
)
910 if (png_ptr
->row_buf
[0] < PNG_FILTER_VALUE_LAST
)
911 png_read_filter_row(png_ptr
, &row_info
, png_ptr
->row_buf
+ 1,
912 png_ptr
->prev_row
+ 1, png_ptr
->row_buf
[0]);
914 png_error(png_ptr
, "bad adaptive filter value");
917 /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
918 * 1.5.6, while the buffer really is this big in current versions of libpng
919 * it may not be in the future, so this was changed just to copy the
920 * interlaced row count:
922 memcpy(png_ptr
->prev_row
, png_ptr
->row_buf
, row_info
.rowbytes
+ 1);
924 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
925 if (png_ptr
->transformations
!= 0)
926 png_do_read_transformations(png_ptr
, &row_info
);
929 /* The transformed pixel depth should match the depth now in row_info. */
930 if (png_ptr
->transformed_pixel_depth
== 0)
932 png_ptr
->transformed_pixel_depth
= row_info
.pixel_depth
;
933 if (row_info
.pixel_depth
> png_ptr
->maximum_pixel_depth
)
934 png_error(png_ptr
, "progressive row overflow");
937 else if (png_ptr
->transformed_pixel_depth
!= row_info
.pixel_depth
)
938 png_error(png_ptr
, "internal progressive row size calculation error");
941 #ifdef PNG_READ_INTERLACING_SUPPORTED
942 /* Expand interlaced rows to full size */
943 if (png_ptr
->interlaced
!= 0 &&
944 (png_ptr
->transformations
& PNG_INTERLACE
) != 0)
946 if (png_ptr
->pass
< 6)
947 png_do_read_interlace(&row_info
, png_ptr
->row_buf
+ 1, png_ptr
->pass
,
948 png_ptr
->transformations
);
950 switch (png_ptr
->pass
)
955 for (i
= 0; i
< 8 && png_ptr
->pass
== 0; i
++)
957 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
958 png_read_push_finish_row(png_ptr
); /* Updates png_ptr->pass */
961 if (png_ptr
->pass
== 2) /* Pass 1 might be empty */
963 for (i
= 0; i
< 4 && png_ptr
->pass
== 2; i
++)
965 png_push_have_row(png_ptr
, NULL
);
966 png_read_push_finish_row(png_ptr
);
970 if (png_ptr
->pass
== 4 && png_ptr
->height
<= 4)
972 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
974 png_push_have_row(png_ptr
, NULL
);
975 png_read_push_finish_row(png_ptr
);
979 if (png_ptr
->pass
== 6 && png_ptr
->height
<= 4)
981 png_push_have_row(png_ptr
, NULL
);
982 png_read_push_finish_row(png_ptr
);
991 for (i
= 0; i
< 8 && png_ptr
->pass
== 1; i
++)
993 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
994 png_read_push_finish_row(png_ptr
);
997 if (png_ptr
->pass
== 2) /* Skip top 4 generated rows */
999 for (i
= 0; i
< 4 && png_ptr
->pass
== 2; i
++)
1001 png_push_have_row(png_ptr
, NULL
);
1002 png_read_push_finish_row(png_ptr
);
1013 for (i
= 0; i
< 4 && png_ptr
->pass
== 2; i
++)
1015 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
1016 png_read_push_finish_row(png_ptr
);
1019 for (i
= 0; i
< 4 && png_ptr
->pass
== 2; i
++)
1021 png_push_have_row(png_ptr
, NULL
);
1022 png_read_push_finish_row(png_ptr
);
1025 if (png_ptr
->pass
== 4) /* Pass 3 might be empty */
1027 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
1029 png_push_have_row(png_ptr
, NULL
);
1030 png_read_push_finish_row(png_ptr
);
1041 for (i
= 0; i
< 4 && png_ptr
->pass
== 3; i
++)
1043 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
1044 png_read_push_finish_row(png_ptr
);
1047 if (png_ptr
->pass
== 4) /* Skip top two generated rows */
1049 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
1051 png_push_have_row(png_ptr
, NULL
);
1052 png_read_push_finish_row(png_ptr
);
1063 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
1065 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
1066 png_read_push_finish_row(png_ptr
);
1069 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
1071 png_push_have_row(png_ptr
, NULL
);
1072 png_read_push_finish_row(png_ptr
);
1075 if (png_ptr
->pass
== 6) /* Pass 5 might be empty */
1077 png_push_have_row(png_ptr
, NULL
);
1078 png_read_push_finish_row(png_ptr
);
1088 for (i
= 0; i
< 2 && png_ptr
->pass
== 5; i
++)
1090 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
1091 png_read_push_finish_row(png_ptr
);
1094 if (png_ptr
->pass
== 6) /* Skip top generated row */
1096 png_push_have_row(png_ptr
, NULL
);
1097 png_read_push_finish_row(png_ptr
);
1106 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
1107 png_read_push_finish_row(png_ptr
);
1109 if (png_ptr
->pass
!= 6)
1112 png_push_have_row(png_ptr
, NULL
);
1113 png_read_push_finish_row(png_ptr
);
1120 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
1121 png_read_push_finish_row(png_ptr
);
1126 png_read_push_finish_row(png_structrp png_ptr
)
1128 #ifdef PNG_READ_INTERLACING_SUPPORTED
1129 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1131 /* Start of interlace block */
1132 static const png_byte png_pass_start
[] = {0, 4, 0, 2, 0, 1, 0};
1134 /* Offset to next interlace block */
1135 static const png_byte png_pass_inc
[] = {8, 8, 4, 4, 2, 2, 1};
1137 /* Start of interlace block in the y direction */
1138 static const png_byte png_pass_ystart
[] = {0, 0, 4, 0, 2, 0, 1};
1140 /* Offset to next interlace block in the y direction */
1141 static const png_byte png_pass_yinc
[] = {8, 8, 8, 4, 4, 2, 2};
1143 /* Height of interlace block. This is not currently used - if you need
1144 * it, uncomment it here and in png.h
1145 static const png_byte png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
1149 png_ptr
->row_number
++;
1150 if (png_ptr
->row_number
< png_ptr
->num_rows
)
1153 #ifdef PNG_READ_INTERLACING_SUPPORTED
1154 if (png_ptr
->interlaced
!= 0)
1156 png_ptr
->row_number
= 0;
1157 memset(png_ptr
->prev_row
, 0, png_ptr
->rowbytes
+ 1);
1162 if ((png_ptr
->pass
== 1 && png_ptr
->width
< 5) ||
1163 (png_ptr
->pass
== 3 && png_ptr
->width
< 3) ||
1164 (png_ptr
->pass
== 5 && png_ptr
->width
< 2))
1167 if (png_ptr
->pass
> 7)
1170 if (png_ptr
->pass
>= 7)
1173 png_ptr
->iwidth
= (png_ptr
->width
+
1174 png_pass_inc
[png_ptr
->pass
] - 1 -
1175 png_pass_start
[png_ptr
->pass
]) /
1176 png_pass_inc
[png_ptr
->pass
];
1178 if ((png_ptr
->transformations
& PNG_INTERLACE
) != 0)
1181 png_ptr
->num_rows
= (png_ptr
->height
+
1182 png_pass_yinc
[png_ptr
->pass
] - 1 -
1183 png_pass_ystart
[png_ptr
->pass
]) /
1184 png_pass_yinc
[png_ptr
->pass
];
1186 } while (png_ptr
->iwidth
== 0 || png_ptr
->num_rows
== 0);
1188 #endif /* READ_INTERLACING */
1192 png_push_have_info(png_structrp png_ptr
, png_inforp info_ptr
)
1194 if (png_ptr
->info_fn
!= NULL
)
1195 (*(png_ptr
->info_fn
))(png_ptr
, info_ptr
);
1199 png_push_have_end(png_structrp png_ptr
, png_inforp info_ptr
)
1201 if (png_ptr
->end_fn
!= NULL
)
1202 (*(png_ptr
->end_fn
))(png_ptr
, info_ptr
);
1206 png_push_have_row(png_structrp png_ptr
, png_bytep row
)
1208 if (png_ptr
->row_fn
!= NULL
)
1209 (*(png_ptr
->row_fn
))(png_ptr
, row
, png_ptr
->row_number
,
1210 (int)png_ptr
->pass
);
1213 #ifdef PNG_READ_INTERLACING_SUPPORTED
1215 png_progressive_combine_row(png_const_structrp png_ptr
, png_bytep old_row
,
1216 png_const_bytep new_row
)
1218 if (png_ptr
== NULL
)
1221 /* new_row is a flag here - if it is NULL then the app callback was called
1222 * from an empty row (see the calls to png_struct::row_fn below), otherwise
1223 * it must be png_ptr->row_buf+1
1225 if (new_row
!= NULL
)
1226 png_combine_row(png_ptr
, old_row
, 1/*blocky display*/);
1228 #endif /* READ_INTERLACING */
1231 png_set_progressive_read_fn(png_structrp png_ptr
, png_voidp progressive_ptr
,
1232 png_progressive_info_ptr info_fn
, png_progressive_row_ptr row_fn
,
1233 png_progressive_end_ptr end_fn
)
1235 if (png_ptr
== NULL
)
1238 png_ptr
->info_fn
= info_fn
;
1239 png_ptr
->row_fn
= row_fn
;
1240 png_ptr
->end_fn
= end_fn
;
1242 png_set_read_fn(png_ptr
, progressive_ptr
, png_push_fill_buffer
);
1245 #ifdef PNG_READ_APNG_SUPPORTED
1247 png_set_progressive_frame_fn(png_structp png_ptr
,
1248 png_progressive_frame_ptr frame_info_fn
,
1249 png_progressive_frame_ptr frame_end_fn
)
1251 png_ptr
->frame_info_fn
= frame_info_fn
;
1252 png_ptr
->frame_end_fn
= frame_end_fn
;
1253 png_ptr
->apng_flags
|= PNG_APNG_APP
;
1258 png_get_progressive_ptr(png_const_structrp png_ptr
)
1260 if (png_ptr
== NULL
)
1263 return png_ptr
->io_ptr
;
1265 #endif /* PROGRESSIVE_READ */