Bug 1874684 - Part 28: Return DateDuration from DifferenceISODateTime. r=mgaudet
[gecko.git] / media / libpng / pngpread.c
blob826d879cb21324cce07ab5f46384fab2f16f25ee
2 /* pngpread.c - read a png file in push mode
4 * Copyright (c) 2018-2024 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
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_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; }
35 void PNGAPI
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)
40 return;
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);
50 size_t PNGAPI
51 png_process_data_pause(png_structrp png_ptr, int save)
53 if (png_ptr != NULL)
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:
58 if (save != 0)
59 png_push_save_buffer(png_ptr);
60 else
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;
74 return 0;
77 png_uint_32 PNGAPI
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");
87 return 0;
90 /* What we do with the incoming data depends on what we were previously
91 * doing before we ran out of data...
93 void /* PRIVATE */
94 png_process_some_data(png_structrp png_ptr, png_inforp info_ptr)
96 if (png_ptr == NULL)
97 return;
99 switch (png_ptr->process_mode)
101 case PNG_READ_SIG_MODE:
103 png_push_read_sig(png_ptr, info_ptr);
104 break;
107 case PNG_READ_CHUNK_MODE:
109 png_push_read_chunk(png_ptr, info_ptr);
110 break;
113 case PNG_READ_IDAT_MODE:
115 png_push_read_IDAT(png_ptr);
116 break;
119 default:
121 png_ptr->buffer_size = 0;
122 break;
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
131 * routine.
133 void /* PRIVATE */
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]),
145 num_to_check);
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) != 0)
150 if (num_checked < 4 &&
151 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4) != 0)
152 png_error(png_ptr, "Not a PNG file");
154 else
155 png_error(png_ptr, "PNG file corrupted by ASCII conversion");
157 else
159 if (png_ptr->sig_bytes >= 8)
161 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
166 void /* PRIVATE */
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 */
172 #endif
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;
230 else
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);
268 else
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;
277 return;
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)
302 return;
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;
337 #endif
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);
349 #endif
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;
357 return;
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);
367 #endif
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);
375 #endif
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);
383 #endif
384 #ifdef PNG_READ_eXIf_SUPPORTED
385 else if (png_ptr->chunk_name == png_eXIf)
387 PNG_PUSH_SAVE_BUFFER_IF_FULL
388 png_handle_eXIf(png_ptr, info_ptr, png_ptr->push_length);
391 #endif
392 #ifdef PNG_READ_sRGB_SUPPORTED
393 else if (chunk_name == png_sRGB)
395 PNG_PUSH_SAVE_BUFFER_IF_FULL
396 png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length);
399 #endif
400 #ifdef PNG_READ_iCCP_SUPPORTED
401 else if (png_ptr->chunk_name == png_iCCP)
403 PNG_PUSH_SAVE_BUFFER_IF_FULL
404 png_handle_iCCP(png_ptr, info_ptr, png_ptr->push_length);
407 #endif
408 #ifdef PNG_READ_sPLT_SUPPORTED
409 else if (chunk_name == png_sPLT)
411 PNG_PUSH_SAVE_BUFFER_IF_FULL
412 png_handle_sPLT(png_ptr, info_ptr, png_ptr->push_length);
415 #endif
416 #ifdef PNG_READ_tRNS_SUPPORTED
417 else if (chunk_name == png_tRNS)
419 PNG_PUSH_SAVE_BUFFER_IF_FULL
420 png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length);
423 #endif
424 #ifdef PNG_READ_bKGD_SUPPORTED
425 else if (chunk_name == png_bKGD)
427 PNG_PUSH_SAVE_BUFFER_IF_FULL
428 png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length);
431 #endif
432 #ifdef PNG_READ_hIST_SUPPORTED
433 else if (chunk_name == png_hIST)
435 PNG_PUSH_SAVE_BUFFER_IF_FULL
436 png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length);
439 #endif
440 #ifdef PNG_READ_pHYs_SUPPORTED
441 else if (chunk_name == png_pHYs)
443 PNG_PUSH_SAVE_BUFFER_IF_FULL
444 png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length);
447 #endif
448 #ifdef PNG_READ_oFFs_SUPPORTED
449 else if (chunk_name == png_oFFs)
451 PNG_PUSH_SAVE_BUFFER_IF_FULL
452 png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length);
454 #endif
456 #ifdef PNG_READ_pCAL_SUPPORTED
457 else if (chunk_name == png_pCAL)
459 PNG_PUSH_SAVE_BUFFER_IF_FULL
460 png_handle_pCAL(png_ptr, info_ptr, png_ptr->push_length);
463 #endif
464 #ifdef PNG_READ_sCAL_SUPPORTED
465 else if (chunk_name == png_sCAL)
467 PNG_PUSH_SAVE_BUFFER_IF_FULL
468 png_handle_sCAL(png_ptr, info_ptr, png_ptr->push_length);
471 #endif
472 #ifdef PNG_READ_tIME_SUPPORTED
473 else if (chunk_name == png_tIME)
475 PNG_PUSH_SAVE_BUFFER_IF_FULL
476 png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length);
479 #endif
480 #ifdef PNG_READ_tEXt_SUPPORTED
481 else if (chunk_name == png_tEXt)
483 PNG_PUSH_SAVE_BUFFER_IF_FULL
484 png_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length);
487 #endif
488 #ifdef PNG_READ_zTXt_SUPPORTED
489 else if (chunk_name == png_zTXt)
491 PNG_PUSH_SAVE_BUFFER_IF_FULL
492 png_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length);
495 #endif
496 #ifdef PNG_READ_iTXt_SUPPORTED
497 else if (chunk_name == png_iTXt)
499 PNG_PUSH_SAVE_BUFFER_IF_FULL
500 png_handle_iTXt(png_ptr, info_ptr, png_ptr->push_length);
502 #endif
504 #ifdef PNG_READ_APNG_SUPPORTED
505 else if (chunk_name == png_acTL)
507 PNG_PUSH_SAVE_BUFFER_IF_FULL
508 png_handle_acTL(png_ptr, info_ptr, png_ptr->push_length);
511 else if (chunk_name == png_fcTL)
513 PNG_PUSH_SAVE_BUFFER_IF_FULL
514 png_handle_fcTL(png_ptr, info_ptr, png_ptr->push_length);
517 #endif /* READ_APNG */
518 else
520 PNG_PUSH_SAVE_BUFFER_IF_FULL
521 png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length,
522 PNG_HANDLE_CHUNK_AS_DEFAULT);
525 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
528 void PNGCBAPI
529 png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, size_t length)
531 png_bytep ptr;
533 if (png_ptr == NULL)
534 return;
536 ptr = buffer;
537 if (png_ptr->save_buffer_size != 0)
539 size_t save_size;
541 if (length < png_ptr->save_buffer_size)
542 save_size = length;
544 else
545 save_size = png_ptr->save_buffer_size;
547 memcpy(ptr, png_ptr->save_buffer_ptr, save_size);
548 length -= save_size;
549 ptr += save_size;
550 png_ptr->buffer_size -= save_size;
551 png_ptr->save_buffer_size -= save_size;
552 png_ptr->save_buffer_ptr += save_size;
554 if (length != 0 && png_ptr->current_buffer_size != 0)
556 size_t save_size;
558 if (length < png_ptr->current_buffer_size)
559 save_size = length;
561 else
562 save_size = png_ptr->current_buffer_size;
564 memcpy(ptr, png_ptr->current_buffer_ptr, save_size);
565 png_ptr->buffer_size -= save_size;
566 png_ptr->current_buffer_size -= save_size;
567 png_ptr->current_buffer_ptr += save_size;
571 void /* PRIVATE */
572 png_push_save_buffer(png_structrp png_ptr)
574 if (png_ptr->save_buffer_size != 0)
576 if (png_ptr->save_buffer_ptr != png_ptr->save_buffer)
578 size_t i, istop;
579 png_bytep sp;
580 png_bytep dp;
582 istop = png_ptr->save_buffer_size;
583 for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer;
584 i < istop; i++, sp++, dp++)
586 *dp = *sp;
590 if (png_ptr->save_buffer_size + png_ptr->current_buffer_size >
591 png_ptr->save_buffer_max)
593 size_t new_max;
594 png_bytep old_buffer;
596 if (png_ptr->save_buffer_size > PNG_SIZE_MAX -
597 (png_ptr->current_buffer_size + 256))
599 png_error(png_ptr, "Potential overflow of save_buffer");
602 new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;
603 old_buffer = png_ptr->save_buffer;
604 png_ptr->save_buffer = (png_bytep)png_malloc_warn(png_ptr,
605 (size_t)new_max);
607 if (png_ptr->save_buffer == NULL)
609 png_free(png_ptr, old_buffer);
610 png_error(png_ptr, "Insufficient memory for save_buffer");
613 if (old_buffer)
614 memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
615 else if (png_ptr->save_buffer_size)
616 png_error(png_ptr, "save_buffer error");
617 png_free(png_ptr, old_buffer);
618 png_ptr->save_buffer_max = new_max;
620 if (png_ptr->current_buffer_size)
622 memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
623 png_ptr->current_buffer_ptr, png_ptr->current_buffer_size);
624 png_ptr->save_buffer_size += png_ptr->current_buffer_size;
625 png_ptr->current_buffer_size = 0;
627 png_ptr->save_buffer_ptr = png_ptr->save_buffer;
628 png_ptr->buffer_size = 0;
631 void /* PRIVATE */
632 png_push_restore_buffer(png_structrp png_ptr, png_bytep buffer,
633 size_t buffer_length)
635 png_ptr->current_buffer = buffer;
636 png_ptr->current_buffer_size = buffer_length;
637 png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size;
638 png_ptr->current_buffer_ptr = png_ptr->current_buffer;
641 void /* PRIVATE */
642 png_push_read_IDAT(png_structrp png_ptr)
644 if ((png_ptr->mode & PNG_HAVE_CHUNK_HEADER) == 0)
646 png_byte chunk_length[4];
647 png_byte chunk_tag[4];
649 /* TODO: this code can be commoned up with the same code in push_read */
650 #ifdef PNG_READ_APNG_SUPPORTED
651 PNG_PUSH_SAVE_BUFFER_IF_LT(12)
652 #else
653 PNG_PUSH_SAVE_BUFFER_IF_LT(8)
654 #endif
655 png_push_fill_buffer(png_ptr, chunk_length, 4);
656 png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
657 png_reset_crc(png_ptr);
658 png_crc_read(png_ptr, chunk_tag, 4);
659 png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(chunk_tag);
660 png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
662 #ifdef PNG_READ_APNG_SUPPORTED
663 if (png_ptr->chunk_name != png_fdAT && png_ptr->num_frames_read > 0)
665 if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) != 0)
667 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
668 if (png_ptr->frame_end_fn != NULL)
669 (*(png_ptr->frame_end_fn))(png_ptr, png_ptr->num_frames_read);
670 png_ptr->num_frames_read++;
671 return;
673 else
675 if (png_ptr->chunk_name == png_IEND)
676 png_error(png_ptr, "Not enough image data");
677 PNG_PUSH_SAVE_BUFFER_IF_FULL
678 png_warning(png_ptr, "Skipping (ignoring) a chunk between "
679 "APNG chunks");
680 png_crc_finish(png_ptr, png_ptr->push_length);
681 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
682 return;
685 else
686 #endif
687 #ifdef PNG_READ_APNG_SUPPORTED
688 if (png_ptr->chunk_name != png_IDAT && png_ptr->num_frames_read == 0)
689 #else
690 if (png_ptr->chunk_name != png_IDAT)
691 #endif
693 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
695 if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
696 png_error(png_ptr, "Not enough compressed data");
698 #ifdef PNG_READ_APNG_SUPPORTED
699 if (png_ptr->frame_end_fn != NULL)
700 (*(png_ptr->frame_end_fn))(png_ptr, png_ptr->num_frames_read);
701 png_ptr->num_frames_read++;
702 #endif
704 return;
707 png_ptr->idat_size = png_ptr->push_length;
709 #ifdef PNG_READ_APNG_SUPPORTED
710 if (png_ptr->num_frames_read > 0)
712 png_ensure_sequence_number(png_ptr, 4);
713 png_ptr->idat_size -= 4;
715 #endif
718 if (png_ptr->idat_size != 0 && png_ptr->save_buffer_size != 0)
720 size_t save_size = png_ptr->save_buffer_size;
721 png_uint_32 idat_size = png_ptr->idat_size;
723 /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
724 * are of different types and we don't know which variable has the fewest
725 * bits. Carefully select the smaller and cast it to the type of the
726 * larger - this cannot overflow. Do not cast in the following test - it
727 * will break on either 16-bit or 64-bit platforms.
729 if (idat_size < save_size)
730 save_size = (size_t)idat_size;
732 else
733 idat_size = (png_uint_32)save_size;
735 png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
737 png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size);
739 png_ptr->idat_size -= idat_size;
740 png_ptr->buffer_size -= save_size;
741 png_ptr->save_buffer_size -= save_size;
742 png_ptr->save_buffer_ptr += save_size;
745 if (png_ptr->idat_size != 0 && png_ptr->current_buffer_size != 0)
747 size_t save_size = png_ptr->current_buffer_size;
748 png_uint_32 idat_size = png_ptr->idat_size;
750 /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
751 * are of different types and we don't know which variable has the fewest
752 * bits. Carefully select the smaller and cast it to the type of the
753 * larger - this cannot overflow.
755 if (idat_size < save_size)
756 save_size = (size_t)idat_size;
758 else
759 idat_size = (png_uint_32)save_size;
761 png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
763 png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size);
765 png_ptr->idat_size -= idat_size;
766 png_ptr->buffer_size -= save_size;
767 png_ptr->current_buffer_size -= save_size;
768 png_ptr->current_buffer_ptr += save_size;
771 if (png_ptr->idat_size == 0)
773 PNG_PUSH_SAVE_BUFFER_IF_LT(4)
774 png_crc_finish(png_ptr, 0);
775 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
776 png_ptr->mode |= PNG_AFTER_IDAT;
777 png_ptr->zowner = 0;
781 void /* PRIVATE */
782 png_process_IDAT_data(png_structrp png_ptr, png_bytep buffer,
783 size_t buffer_length)
785 /* The caller checks for a non-zero buffer length. */
786 if (!(buffer_length > 0) || buffer == NULL)
787 png_error(png_ptr, "No IDAT data (internal error)");
789 #ifdef PNG_READ_APNG_SUPPORTED
790 /* If the app is not APNG-aware, decode only the first frame */
791 if ((png_ptr->apng_flags & PNG_APNG_APP) == 0 &&
792 png_ptr->num_frames_read > 0)
794 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
795 return;
797 #endif
799 /* This routine must process all the data it has been given
800 * before returning, calling the row callback as required to
801 * handle the uncompressed results.
803 png_ptr->zstream.next_in = buffer;
804 /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
805 png_ptr->zstream.avail_in = (uInt)buffer_length;
807 /* Keep going until the decompressed data is all processed
808 * or the stream marked as finished.
810 while (png_ptr->zstream.avail_in > 0 &&
811 (png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
813 int ret;
815 /* We have data for zlib, but we must check that zlib
816 * has someplace to put the results. It doesn't matter
817 * if we don't expect any results -- it may be the input
818 * data is just the LZ end code.
820 if (!(png_ptr->zstream.avail_out > 0))
822 /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
823 png_ptr->zstream.avail_out = (uInt)(PNG_ROWBYTES(png_ptr->pixel_depth,
824 png_ptr->iwidth) + 1);
826 png_ptr->zstream.next_out = png_ptr->row_buf;
829 /* Using Z_SYNC_FLUSH here means that an unterminated
830 * LZ stream (a stream with a missing end code) can still
831 * be handled, otherwise (Z_NO_FLUSH) a future zlib
832 * implementation might defer output and therefore
833 * change the current behavior (see comments in inflate.c
834 * for why this doesn't happen at present with zlib 1.2.5).
836 ret = PNG_INFLATE(png_ptr, Z_SYNC_FLUSH);
838 /* Check for any failure before proceeding. */
839 if (ret != Z_OK && ret != Z_STREAM_END)
841 /* Terminate the decompression. */
842 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
843 png_ptr->zowner = 0;
845 /* This may be a truncated stream (missing or
846 * damaged end code). Treat that as a warning.
848 if (png_ptr->row_number >= png_ptr->num_rows ||
849 png_ptr->pass > 6)
850 png_warning(png_ptr, "Truncated compressed data in IDAT");
852 else
854 if (ret == Z_DATA_ERROR)
855 png_benign_error(png_ptr, "IDAT: ADLER32 checksum mismatch");
856 else
857 png_error(png_ptr, "Decompression error in IDAT");
860 /* Skip the check on unprocessed input */
861 return;
864 /* Did inflate output any data? */
865 if (png_ptr->zstream.next_out != png_ptr->row_buf)
867 /* Is this unexpected data after the last row?
868 * If it is, artificially terminate the LZ output
869 * here.
871 if (png_ptr->row_number >= png_ptr->num_rows ||
872 png_ptr->pass > 6)
874 /* Extra data. */
875 png_warning(png_ptr, "Extra compressed data in IDAT");
876 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
877 png_ptr->zowner = 0;
879 /* Do no more processing; skip the unprocessed
880 * input check below.
882 return;
885 /* Do we have a complete row? */
886 if (png_ptr->zstream.avail_out == 0)
887 png_push_process_row(png_ptr);
890 /* And check for the end of the stream. */
891 if (ret == Z_STREAM_END)
892 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
895 /* All the data should have been processed, if anything
896 * is left at this point we have bytes of IDAT data
897 * after the zlib end code.
899 if (png_ptr->zstream.avail_in > 0)
900 png_warning(png_ptr, "Extra compression data in IDAT");
903 void /* PRIVATE */
904 png_push_process_row(png_structrp png_ptr)
906 /* 1.5.6: row_info moved out of png_struct to a local here. */
907 png_row_info row_info;
909 row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */
910 row_info.color_type = png_ptr->color_type;
911 row_info.bit_depth = png_ptr->bit_depth;
912 row_info.channels = png_ptr->channels;
913 row_info.pixel_depth = png_ptr->pixel_depth;
914 row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
916 if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
918 if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
919 png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1,
920 png_ptr->prev_row + 1, png_ptr->row_buf[0]);
921 else
922 png_error(png_ptr, "bad adaptive filter value");
925 /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
926 * 1.5.6, while the buffer really is this big in current versions of libpng
927 * it may not be in the future, so this was changed just to copy the
928 * interlaced row count:
930 memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
932 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
933 if (png_ptr->transformations != 0)
934 png_do_read_transformations(png_ptr, &row_info);
935 #endif
937 /* The transformed pixel depth should match the depth now in row_info. */
938 if (png_ptr->transformed_pixel_depth == 0)
940 png_ptr->transformed_pixel_depth = row_info.pixel_depth;
941 if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
942 png_error(png_ptr, "progressive row overflow");
945 else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
946 png_error(png_ptr, "internal progressive row size calculation error");
949 #ifdef PNG_READ_INTERLACING_SUPPORTED
950 /* Expand interlaced rows to full size */
951 if (png_ptr->interlaced != 0 &&
952 (png_ptr->transformations & PNG_INTERLACE) != 0)
954 if (png_ptr->pass < 6)
955 png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
956 png_ptr->transformations);
958 switch (png_ptr->pass)
960 case 0:
962 int i;
963 for (i = 0; i < 8 && png_ptr->pass == 0; i++)
965 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
966 png_read_push_finish_row(png_ptr); /* Updates png_ptr->pass */
969 if (png_ptr->pass == 2) /* Pass 1 might be empty */
971 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
973 png_push_have_row(png_ptr, NULL);
974 png_read_push_finish_row(png_ptr);
978 if (png_ptr->pass == 4 && png_ptr->height <= 4)
980 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
982 png_push_have_row(png_ptr, NULL);
983 png_read_push_finish_row(png_ptr);
987 if (png_ptr->pass == 6 && png_ptr->height <= 4)
989 png_push_have_row(png_ptr, NULL);
990 png_read_push_finish_row(png_ptr);
993 break;
996 case 1:
998 int i;
999 for (i = 0; i < 8 && png_ptr->pass == 1; i++)
1001 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1002 png_read_push_finish_row(png_ptr);
1005 if (png_ptr->pass == 2) /* Skip top 4 generated rows */
1007 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
1009 png_push_have_row(png_ptr, NULL);
1010 png_read_push_finish_row(png_ptr);
1014 break;
1017 case 2:
1019 int i;
1021 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
1023 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1024 png_read_push_finish_row(png_ptr);
1027 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
1029 png_push_have_row(png_ptr, NULL);
1030 png_read_push_finish_row(png_ptr);
1033 if (png_ptr->pass == 4) /* Pass 3 might be empty */
1035 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1037 png_push_have_row(png_ptr, NULL);
1038 png_read_push_finish_row(png_ptr);
1042 break;
1045 case 3:
1047 int i;
1049 for (i = 0; i < 4 && png_ptr->pass == 3; i++)
1051 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1052 png_read_push_finish_row(png_ptr);
1055 if (png_ptr->pass == 4) /* Skip top two generated rows */
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);
1064 break;
1067 case 4:
1069 int i;
1071 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1073 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1074 png_read_push_finish_row(png_ptr);
1077 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1079 png_push_have_row(png_ptr, NULL);
1080 png_read_push_finish_row(png_ptr);
1083 if (png_ptr->pass == 6) /* Pass 5 might be empty */
1085 png_push_have_row(png_ptr, NULL);
1086 png_read_push_finish_row(png_ptr);
1089 break;
1092 case 5:
1094 int i;
1096 for (i = 0; i < 2 && png_ptr->pass == 5; i++)
1098 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1099 png_read_push_finish_row(png_ptr);
1102 if (png_ptr->pass == 6) /* Skip top generated row */
1104 png_push_have_row(png_ptr, NULL);
1105 png_read_push_finish_row(png_ptr);
1108 break;
1111 default:
1112 case 6:
1114 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1115 png_read_push_finish_row(png_ptr);
1117 if (png_ptr->pass != 6)
1118 break;
1120 png_push_have_row(png_ptr, NULL);
1121 png_read_push_finish_row(png_ptr);
1125 else
1126 #endif
1128 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1129 png_read_push_finish_row(png_ptr);
1133 void /* PRIVATE */
1134 png_read_push_finish_row(png_structrp png_ptr)
1136 #ifdef PNG_READ_INTERLACING_SUPPORTED
1137 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1139 /* Start of interlace block */
1140 static const png_byte png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
1142 /* Offset to next interlace block */
1143 static const png_byte png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
1145 /* Start of interlace block in the y direction */
1146 static const png_byte png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
1148 /* Offset to next interlace block in the y direction */
1149 static const png_byte png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
1151 /* Height of interlace block. This is not currently used - if you need
1152 * it, uncomment it here and in png.h
1153 static const png_byte png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
1155 #endif
1157 png_ptr->row_number++;
1158 if (png_ptr->row_number < png_ptr->num_rows)
1159 return;
1161 #ifdef PNG_READ_INTERLACING_SUPPORTED
1162 if (png_ptr->interlaced != 0)
1164 png_ptr->row_number = 0;
1165 memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
1169 png_ptr->pass++;
1170 if ((png_ptr->pass == 1 && png_ptr->width < 5) ||
1171 (png_ptr->pass == 3 && png_ptr->width < 3) ||
1172 (png_ptr->pass == 5 && png_ptr->width < 2))
1173 png_ptr->pass++;
1175 if (png_ptr->pass > 7)
1176 png_ptr->pass--;
1178 if (png_ptr->pass >= 7)
1179 break;
1181 png_ptr->iwidth = (png_ptr->width +
1182 png_pass_inc[png_ptr->pass] - 1 -
1183 png_pass_start[png_ptr->pass]) /
1184 png_pass_inc[png_ptr->pass];
1186 if ((png_ptr->transformations & PNG_INTERLACE) != 0)
1187 break;
1189 png_ptr->num_rows = (png_ptr->height +
1190 png_pass_yinc[png_ptr->pass] - 1 -
1191 png_pass_ystart[png_ptr->pass]) /
1192 png_pass_yinc[png_ptr->pass];
1194 } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0);
1196 #endif /* READ_INTERLACING */
1199 void /* PRIVATE */
1200 png_push_have_info(png_structrp png_ptr, png_inforp info_ptr)
1202 if (png_ptr->info_fn != NULL)
1203 (*(png_ptr->info_fn))(png_ptr, info_ptr);
1206 void /* PRIVATE */
1207 png_push_have_end(png_structrp png_ptr, png_inforp info_ptr)
1209 if (png_ptr->end_fn != NULL)
1210 (*(png_ptr->end_fn))(png_ptr, info_ptr);
1213 void /* PRIVATE */
1214 png_push_have_row(png_structrp png_ptr, png_bytep row)
1216 if (png_ptr->row_fn != NULL)
1217 (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number,
1218 (int)png_ptr->pass);
1221 #ifdef PNG_READ_INTERLACING_SUPPORTED
1222 void PNGAPI
1223 png_progressive_combine_row(png_const_structrp png_ptr, png_bytep old_row,
1224 png_const_bytep new_row)
1226 if (png_ptr == NULL)
1227 return;
1229 /* new_row is a flag here - if it is NULL then the app callback was called
1230 * from an empty row (see the calls to png_struct::row_fn below), otherwise
1231 * it must be png_ptr->row_buf+1
1233 if (new_row != NULL)
1234 png_combine_row(png_ptr, old_row, 1/*blocky display*/);
1236 #endif /* READ_INTERLACING */
1238 void PNGAPI
1239 png_set_progressive_read_fn(png_structrp png_ptr, png_voidp progressive_ptr,
1240 png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
1241 png_progressive_end_ptr end_fn)
1243 if (png_ptr == NULL)
1244 return;
1246 png_ptr->info_fn = info_fn;
1247 png_ptr->row_fn = row_fn;
1248 png_ptr->end_fn = end_fn;
1250 png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer);
1253 #ifdef PNG_READ_APNG_SUPPORTED
1254 void PNGAPI
1255 png_set_progressive_frame_fn(png_structp png_ptr,
1256 png_progressive_frame_ptr frame_info_fn,
1257 png_progressive_frame_ptr frame_end_fn)
1259 png_ptr->frame_info_fn = frame_info_fn;
1260 png_ptr->frame_end_fn = frame_end_fn;
1261 png_ptr->apng_flags |= PNG_APNG_APP;
1263 #endif
1265 png_voidp PNGAPI
1266 png_get_progressive_ptr(png_const_structrp png_ptr)
1268 if (png_ptr == NULL)
1269 return NULL;
1271 return png_ptr->io_ptr;
1273 #endif /* PROGRESSIVE_READ */