Bumping manifests a=b2g-bump
[gecko.git] / media / libpng / pngpread.c
blobff847ccf6a36ece25b9230bfd9815bc0ab45ecc6
2 /* pngpread.c - read a png file in push mode
4 * Last changed in libpng 1.6.15 [November 20, 2014]
5 * Copyright (c) 1998-2014 Glenn Randers-Pehrson
6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
9 * This code is released under the libpng license.
10 * For conditions of distribution and use, see the disclaimer
11 * and license in png.h
14 #include "pngpriv.h"
16 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
18 /* Push model modes */
19 #define PNG_READ_SIG_MODE 0
20 #define PNG_READ_CHUNK_MODE 1
21 #define PNG_READ_IDAT_MODE 2
22 #define PNG_SKIP_MODE 3
23 #define PNG_READ_tEXt_MODE 4
24 #define PNG_READ_zTXt_MODE 5
25 #define PNG_READ_DONE_MODE 6
26 #define PNG_READ_iTXt_MODE 7
27 #define PNG_ERROR_MODE 8
29 #define PNG_PUSH_SAVE_BUFFER_IF_FULL \
30 if (png_ptr->push_length + 4 > png_ptr->buffer_size) \
31 { png_push_save_buffer(png_ptr); return; }
32 #define PNG_PUSH_SAVE_BUFFER_IF_LT(N) \
33 if (png_ptr->buffer_size < N) \
34 { png_push_save_buffer(png_ptr); return; }
36 void PNGAPI
37 png_process_data(png_structrp png_ptr, png_inforp info_ptr,
38 png_bytep buffer, png_size_t buffer_size)
40 if (png_ptr == NULL || info_ptr == NULL)
41 return;
43 png_push_restore_buffer(png_ptr, buffer, buffer_size);
45 while (png_ptr->buffer_size)
47 png_process_some_data(png_ptr, info_ptr);
51 png_size_t PNGAPI
52 png_process_data_pause(png_structrp png_ptr, int save)
54 if (png_ptr != NULL)
56 /* It's easiest for the caller if we do the save; then the caller doesn't
57 * have to supply the same data again:
59 if (save != 0)
60 png_push_save_buffer(png_ptr);
61 else
63 /* This includes any pending saved bytes: */
64 png_size_t remaining = png_ptr->buffer_size;
65 png_ptr->buffer_size = 0;
67 /* So subtract the saved buffer size, unless all the data
68 * is actually 'saved', in which case we just return 0
70 if (png_ptr->save_buffer_size < remaining)
71 return remaining - png_ptr->save_buffer_size;
75 return 0;
78 png_uint_32 PNGAPI
79 png_process_data_skip(png_structrp png_ptr)
81 png_uint_32 remaining = 0;
83 if (png_ptr != NULL && png_ptr->process_mode == PNG_SKIP_MODE &&
84 png_ptr->skip_length > 0)
86 /* At the end of png_process_data the buffer size must be 0 (see the loop
87 * above) so we can detect a broken call here:
89 if (png_ptr->buffer_size != 0)
90 png_error(png_ptr,
91 "png_process_data_skip called inside png_process_data");
93 /* If is impossible for there to be a saved buffer at this point -
94 * otherwise we could not be in SKIP mode. This will also happen if
95 * png_process_skip is called inside png_process_data (but only very
96 * rarely.)
98 if (png_ptr->save_buffer_size != 0)
99 png_error(png_ptr, "png_process_data_skip called with saved data");
101 remaining = png_ptr->skip_length;
102 png_ptr->skip_length = 0;
103 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
106 return remaining;
109 /* What we do with the incoming data depends on what we were previously
110 * doing before we ran out of data...
112 void /* PRIVATE */
113 png_process_some_data(png_structrp png_ptr, png_inforp info_ptr)
115 if (png_ptr == NULL)
116 return;
118 switch (png_ptr->process_mode)
120 case PNG_READ_SIG_MODE:
122 png_push_read_sig(png_ptr, info_ptr);
123 break;
126 case PNG_READ_CHUNK_MODE:
128 png_push_read_chunk(png_ptr, info_ptr);
129 break;
132 case PNG_READ_IDAT_MODE:
134 png_push_read_IDAT(png_ptr);
135 break;
138 case PNG_SKIP_MODE:
140 png_push_crc_finish(png_ptr);
141 break;
144 default:
146 png_ptr->buffer_size = 0;
147 break;
152 /* Read any remaining signature bytes from the stream and compare them with
153 * the correct PNG signature. It is possible that this routine is called
154 * with bytes already read from the signature, either because they have been
155 * checked by the calling application, or because of multiple calls to this
156 * routine.
158 void /* PRIVATE */
159 png_push_read_sig(png_structrp png_ptr, png_inforp info_ptr)
161 png_size_t num_checked = png_ptr->sig_bytes, /* SAFE, does not exceed 8 */
162 num_to_check = 8 - num_checked;
164 if (png_ptr->buffer_size < num_to_check)
166 num_to_check = png_ptr->buffer_size;
169 png_push_fill_buffer(png_ptr, &(info_ptr->signature[num_checked]),
170 num_to_check);
171 png_ptr->sig_bytes = (png_byte)(png_ptr->sig_bytes + num_to_check);
173 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
175 if (num_checked < 4 &&
176 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
177 png_error(png_ptr, "Not a PNG file");
179 else
180 png_error(png_ptr, "PNG file corrupted by ASCII conversion");
182 else
184 if (png_ptr->sig_bytes >= 8)
186 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
191 void /* PRIVATE */
192 png_push_read_chunk(png_structrp png_ptr, png_inforp info_ptr)
194 png_uint_32 chunk_name;
195 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
196 int keep; /* unknown handling method */
197 #endif
199 /* First we make sure we have enough data for the 4-byte chunk name
200 * and the 4-byte chunk length before proceeding with decoding the
201 * chunk data. To fully decode each of these chunks, we also make
202 * sure we have enough data in the buffer for the 4-byte CRC at the
203 * end of every chunk (except IDAT, which is handled separately).
205 if ((png_ptr->mode & PNG_HAVE_CHUNK_HEADER) == 0)
207 png_byte chunk_length[4];
208 png_byte chunk_tag[4];
210 PNG_PUSH_SAVE_BUFFER_IF_LT(8)
211 png_push_fill_buffer(png_ptr, chunk_length, 4);
212 png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
213 png_reset_crc(png_ptr);
214 png_crc_read(png_ptr, chunk_tag, 4);
215 png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(chunk_tag);
216 png_check_chunk_name(png_ptr, png_ptr->chunk_name);
217 png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
220 chunk_name = png_ptr->chunk_name;
222 #ifdef PNG_READ_APNG_SUPPORTED
223 if (png_ptr->num_frames_read > 0 &&
224 png_ptr->num_frames_read < info_ptr->num_frames)
226 if (chunk_name == png_IDAT)
228 /* Discard trailing IDATs for the first frame */
229 if ((png_ptr->mode & PNG_HAVE_fcTL) != 0 ||
230 png_ptr->num_frames_read > 1)
231 png_error(png_ptr, "out of place IDAT");
233 PNG_PUSH_SAVE_BUFFER_IF_FULL
234 png_push_crc_skip(png_ptr, png_ptr->push_length);
235 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
236 return;
238 else if (chunk_name == png_fdAT)
240 PNG_PUSH_SAVE_BUFFER_IF_LT(4)
241 png_ensure_sequence_number(png_ptr, 4);
243 if ((png_ptr->mode & PNG_HAVE_fcTL) == 0)
245 /* Discard trailing fdATs for frames other than the first */
246 if (png_ptr->num_frames_read < 2)
247 png_error(png_ptr, "out of place fdAT");
249 PNG_PUSH_SAVE_BUFFER_IF_FULL
250 png_push_crc_skip(png_ptr, png_ptr->push_length);
251 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
252 return;
255 else
257 /* frame data follows */
258 png_ptr->idat_size = png_ptr->push_length - 4;
259 png_ptr->mode |= PNG_HAVE_IDAT;
260 png_ptr->process_mode = PNG_READ_IDAT_MODE;
262 return;
266 else if (chunk_name == png_fcTL)
268 PNG_PUSH_SAVE_BUFFER_IF_FULL
269 png_read_reset(png_ptr);
270 png_ptr->mode &= ~PNG_HAVE_fcTL;
272 png_handle_fcTL(png_ptr, info_ptr, png_ptr->push_length);
274 if ((png_ptr->mode & PNG_HAVE_fcTL) == 0)
275 png_error(png_ptr, "missing required fcTL chunk");
277 png_read_reinit(png_ptr, info_ptr);
278 png_progressive_read_reset(png_ptr);
280 if (png_ptr->frame_info_fn != NULL)
281 (*(png_ptr->frame_info_fn))(png_ptr, png_ptr->num_frames_read);
283 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
285 return;
288 else
290 PNG_PUSH_SAVE_BUFFER_IF_FULL
291 png_warning(png_ptr, "Skipped (ignored) a chunk "
292 "between APNG chunks");
293 png_push_crc_skip(png_ptr, png_ptr->push_length);
294 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
295 return;
298 return;
300 #endif /* READ_APNG */
302 if (chunk_name == png_IDAT)
304 if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
305 png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
307 /* If we reach an IDAT chunk, this means we have read all of the
308 * header chunks, and we can start reading the image (or if this
309 * is called after the image has been read - we have an error).
311 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
312 png_error(png_ptr, "Missing IHDR before IDAT");
314 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
315 (png_ptr->mode & PNG_HAVE_PLTE) == 0)
316 png_error(png_ptr, "Missing PLTE before IDAT");
318 png_ptr->mode |= PNG_HAVE_IDAT;
319 png_ptr->process_mode = PNG_READ_IDAT_MODE;
321 if ((png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) == 0)
322 if (png_ptr->push_length == 0)
323 return;
325 if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
326 png_benign_error(png_ptr, "Too many IDATs found");
329 if (chunk_name == png_IHDR)
331 if (png_ptr->push_length != 13)
332 png_error(png_ptr, "Invalid IHDR length");
334 PNG_PUSH_SAVE_BUFFER_IF_FULL
335 png_handle_IHDR(png_ptr, info_ptr, png_ptr->push_length);
338 else if (chunk_name == png_IEND)
340 PNG_PUSH_SAVE_BUFFER_IF_FULL
341 png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length);
343 png_ptr->process_mode = PNG_READ_DONE_MODE;
344 png_push_have_end(png_ptr, info_ptr);
347 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
348 else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
350 PNG_PUSH_SAVE_BUFFER_IF_FULL
351 png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length, keep);
353 if (chunk_name == png_PLTE)
354 png_ptr->mode |= PNG_HAVE_PLTE;
356 #endif
358 else if (chunk_name == png_PLTE)
360 PNG_PUSH_SAVE_BUFFER_IF_FULL
361 png_handle_PLTE(png_ptr, info_ptr, png_ptr->push_length);
364 else if (chunk_name == png_IDAT)
366 #ifdef PNG_READ_APNG_SUPPORTED
367 png_have_info(png_ptr, info_ptr);
368 #endif
369 png_ptr->idat_size = png_ptr->push_length;
370 png_ptr->process_mode = PNG_READ_IDAT_MODE;
371 png_push_have_info(png_ptr, info_ptr);
372 png_ptr->zstream.avail_out =
373 (uInt) PNG_ROWBYTES(png_ptr->pixel_depth,
374 png_ptr->iwidth) + 1;
375 png_ptr->zstream.next_out = png_ptr->row_buf;
376 return;
379 #ifdef PNG_READ_gAMA_SUPPORTED
380 else if (png_ptr->chunk_name == png_gAMA)
382 PNG_PUSH_SAVE_BUFFER_IF_FULL
383 png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length);
386 #endif
387 #ifdef PNG_READ_sBIT_SUPPORTED
388 else if (png_ptr->chunk_name == png_sBIT)
390 PNG_PUSH_SAVE_BUFFER_IF_FULL
391 png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length);
394 #endif
395 #ifdef PNG_READ_cHRM_SUPPORTED
396 else if (png_ptr->chunk_name == png_cHRM)
398 PNG_PUSH_SAVE_BUFFER_IF_FULL
399 png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length);
402 #endif
403 #ifdef PNG_READ_sRGB_SUPPORTED
404 else if (chunk_name == png_sRGB)
406 PNG_PUSH_SAVE_BUFFER_IF_FULL
407 png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length);
410 #endif
411 #ifdef PNG_READ_iCCP_SUPPORTED
412 else if (png_ptr->chunk_name == png_iCCP)
414 PNG_PUSH_SAVE_BUFFER_IF_FULL
415 png_handle_iCCP(png_ptr, info_ptr, png_ptr->push_length);
418 #endif
419 #ifdef PNG_READ_sPLT_SUPPORTED
420 else if (chunk_name == png_sPLT)
422 PNG_PUSH_SAVE_BUFFER_IF_FULL
423 png_handle_sPLT(png_ptr, info_ptr, png_ptr->push_length);
426 #endif
427 #ifdef PNG_READ_tRNS_SUPPORTED
428 else if (chunk_name == png_tRNS)
430 PNG_PUSH_SAVE_BUFFER_IF_FULL
431 png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length);
434 #endif
435 #ifdef PNG_READ_bKGD_SUPPORTED
436 else if (chunk_name == png_bKGD)
438 PNG_PUSH_SAVE_BUFFER_IF_FULL
439 png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length);
442 #endif
443 #ifdef PNG_READ_hIST_SUPPORTED
444 else if (chunk_name == png_hIST)
446 PNG_PUSH_SAVE_BUFFER_IF_FULL
447 png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length);
450 #endif
451 #ifdef PNG_READ_pHYs_SUPPORTED
452 else if (chunk_name == png_pHYs)
454 PNG_PUSH_SAVE_BUFFER_IF_FULL
455 png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length);
458 #endif
459 #ifdef PNG_READ_oFFs_SUPPORTED
460 else if (chunk_name == png_oFFs)
462 PNG_PUSH_SAVE_BUFFER_IF_FULL
463 png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length);
465 #endif
467 #ifdef PNG_READ_pCAL_SUPPORTED
468 else if (chunk_name == png_pCAL)
470 PNG_PUSH_SAVE_BUFFER_IF_FULL
471 png_handle_pCAL(png_ptr, info_ptr, png_ptr->push_length);
474 #endif
475 #ifdef PNG_READ_sCAL_SUPPORTED
476 else if (chunk_name == png_sCAL)
478 PNG_PUSH_SAVE_BUFFER_IF_FULL
479 png_handle_sCAL(png_ptr, info_ptr, png_ptr->push_length);
482 #endif
483 #ifdef PNG_READ_tIME_SUPPORTED
484 else if (chunk_name == png_tIME)
486 PNG_PUSH_SAVE_BUFFER_IF_FULL
487 png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length);
490 #endif
491 #ifdef PNG_READ_tEXt_SUPPORTED
492 else if (chunk_name == png_tEXt)
494 PNG_PUSH_SAVE_BUFFER_IF_FULL
495 png_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length);
498 #endif
499 #ifdef PNG_READ_zTXt_SUPPORTED
500 else if (chunk_name == png_zTXt)
502 PNG_PUSH_SAVE_BUFFER_IF_FULL
503 png_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length);
506 #endif
507 #ifdef PNG_READ_iTXt_SUPPORTED
508 else if (chunk_name == png_iTXt)
510 PNG_PUSH_SAVE_BUFFER_IF_FULL
511 png_handle_iTXt(png_ptr, info_ptr, png_ptr->push_length);
513 #endif
515 #ifdef PNG_READ_APNG_SUPPORTED
516 else if (chunk_name == png_acTL)
518 PNG_PUSH_SAVE_BUFFER_IF_FULL
519 png_handle_acTL(png_ptr, info_ptr, png_ptr->push_length);
522 else if (chunk_name == png_fcTL)
524 PNG_PUSH_SAVE_BUFFER_IF_FULL
525 png_handle_fcTL(png_ptr, info_ptr, png_ptr->push_length);
528 #endif /* READ_APNG */
529 else
531 PNG_PUSH_SAVE_BUFFER_IF_FULL
532 png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length,
533 PNG_HANDLE_CHUNK_AS_DEFAULT);
536 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
539 void /* PRIVATE */
540 png_push_crc_skip(png_structrp png_ptr, png_uint_32 skip)
542 png_ptr->process_mode = PNG_SKIP_MODE;
543 png_ptr->skip_length = skip;
546 void /* PRIVATE */
547 png_push_crc_finish(png_structrp png_ptr)
549 if (png_ptr->skip_length != 0 && png_ptr->save_buffer_size != 0)
551 png_size_t save_size = png_ptr->save_buffer_size;
552 png_uint_32 skip_length = png_ptr->skip_length;
554 /* We want the smaller of 'skip_length' and 'save_buffer_size', but
555 * they are of different types and we don't know which variable has the
556 * fewest bits. Carefully select the smaller and cast it to the type of
557 * the larger - this cannot overflow. Do not cast in the following test
558 * - it will break on either 16 or 64 bit platforms.
560 if (skip_length < save_size)
561 save_size = (png_size_t)skip_length;
563 else
564 skip_length = (png_uint_32)save_size;
566 png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
568 png_ptr->skip_length -= skip_length;
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 (png_ptr->skip_length != 0 && png_ptr->current_buffer_size != 0)
575 png_size_t save_size = png_ptr->current_buffer_size;
576 png_uint_32 skip_length = png_ptr->skip_length;
578 /* We want the smaller of 'skip_length' and 'current_buffer_size', here,
579 * the same problem exists as above and the same solution.
581 if (skip_length < save_size)
582 save_size = (png_size_t)skip_length;
584 else
585 skip_length = (png_uint_32)save_size;
587 png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
589 png_ptr->skip_length -= skip_length;
590 png_ptr->buffer_size -= save_size;
591 png_ptr->current_buffer_size -= save_size;
592 png_ptr->current_buffer_ptr += save_size;
594 if (png_ptr->skip_length == 0)
596 PNG_PUSH_SAVE_BUFFER_IF_LT(4)
597 png_crc_finish(png_ptr, 0);
598 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
602 void PNGCBAPI
603 png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length)
605 png_bytep ptr;
607 if (png_ptr == NULL)
608 return;
610 ptr = buffer;
611 if (png_ptr->save_buffer_size != 0)
613 png_size_t save_size;
615 if (length < png_ptr->save_buffer_size)
616 save_size = length;
618 else
619 save_size = png_ptr->save_buffer_size;
621 memcpy(ptr, png_ptr->save_buffer_ptr, save_size);
622 length -= save_size;
623 ptr += save_size;
624 png_ptr->buffer_size -= save_size;
625 png_ptr->save_buffer_size -= save_size;
626 png_ptr->save_buffer_ptr += save_size;
628 if (length != 0 && png_ptr->current_buffer_size != 0)
630 png_size_t save_size;
632 if (length < png_ptr->current_buffer_size)
633 save_size = length;
635 else
636 save_size = png_ptr->current_buffer_size;
638 memcpy(ptr, png_ptr->current_buffer_ptr, save_size);
639 png_ptr->buffer_size -= save_size;
640 png_ptr->current_buffer_size -= save_size;
641 png_ptr->current_buffer_ptr += save_size;
645 void /* PRIVATE */
646 png_push_save_buffer(png_structrp png_ptr)
648 if (png_ptr->save_buffer_size != 0)
650 if (png_ptr->save_buffer_ptr != png_ptr->save_buffer)
652 png_size_t i, istop;
653 png_bytep sp;
654 png_bytep dp;
656 istop = png_ptr->save_buffer_size;
657 for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer;
658 i < istop; i++, sp++, dp++)
660 *dp = *sp;
664 if (png_ptr->save_buffer_size + png_ptr->current_buffer_size >
665 png_ptr->save_buffer_max)
667 png_size_t new_max;
668 png_bytep old_buffer;
670 if (png_ptr->save_buffer_size > PNG_SIZE_MAX -
671 (png_ptr->current_buffer_size + 256))
673 png_error(png_ptr, "Potential overflow of save_buffer");
676 new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;
677 old_buffer = png_ptr->save_buffer;
678 png_ptr->save_buffer = (png_bytep)png_malloc_warn(png_ptr,
679 (png_size_t)new_max);
681 if (png_ptr->save_buffer == NULL)
683 png_free(png_ptr, old_buffer);
684 old_buffer = NULL;
685 png_error(png_ptr, "Insufficient memory for save_buffer");
688 memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
689 png_free(png_ptr, old_buffer);
690 old_buffer = NULL;
691 png_ptr->save_buffer_max = new_max;
693 if (png_ptr->current_buffer_size)
695 memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
696 png_ptr->current_buffer_ptr, png_ptr->current_buffer_size);
697 png_ptr->save_buffer_size += png_ptr->current_buffer_size;
698 png_ptr->current_buffer_size = 0;
700 png_ptr->save_buffer_ptr = png_ptr->save_buffer;
701 png_ptr->buffer_size = 0;
704 void /* PRIVATE */
705 png_push_restore_buffer(png_structrp png_ptr, png_bytep buffer,
706 png_size_t buffer_length)
708 png_ptr->current_buffer = buffer;
709 png_ptr->current_buffer_size = buffer_length;
710 png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size;
711 png_ptr->current_buffer_ptr = png_ptr->current_buffer;
714 void /* PRIVATE */
715 png_push_read_IDAT(png_structrp png_ptr)
717 if ((png_ptr->mode & PNG_HAVE_CHUNK_HEADER) == 0)
719 png_byte chunk_length[4];
720 png_byte chunk_tag[4];
722 /* TODO: this code can be commoned up with the same code in push_read */
723 #ifdef PNG_READ_APNG_SUPPORTED
724 PNG_PUSH_SAVE_BUFFER_IF_LT(12)
725 #else
726 PNG_PUSH_SAVE_BUFFER_IF_LT(8)
727 #endif
728 png_push_fill_buffer(png_ptr, chunk_length, 4);
729 png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
730 png_reset_crc(png_ptr);
731 png_crc_read(png_ptr, chunk_tag, 4);
732 png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(chunk_tag);
733 png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
735 #ifdef PNG_READ_APNG_SUPPORTED
736 if (png_ptr->chunk_name != png_fdAT && png_ptr->num_frames_read > 0)
738 if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) != 0)
740 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
741 if (png_ptr->frame_end_fn != NULL)
742 (*(png_ptr->frame_end_fn))(png_ptr, png_ptr->num_frames_read);
743 png_ptr->num_frames_read++;
744 return;
746 else
748 if (png_ptr->chunk_name == png_IEND)
749 png_error(png_ptr, "Not enough image data");
750 PNG_PUSH_SAVE_BUFFER_IF_FULL
751 png_warning(png_ptr, "Skipping (ignoring) a chunk between "
752 "APNG chunks");
753 png_crc_finish(png_ptr, png_ptr->push_length);
754 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
755 return;
758 else
759 #endif
760 #ifdef PNG_READ_APNG_SUPPORTED
761 if (png_ptr->chunk_name != png_IDAT && png_ptr->num_frames_read == 0)
762 #else
763 if (png_ptr->chunk_name != png_IDAT)
764 #endif
766 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
768 if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
769 png_error(png_ptr, "Not enough compressed data");
771 #ifdef PNG_READ_APNG_SUPPORTED
772 if (png_ptr->frame_end_fn != NULL)
773 (*(png_ptr->frame_end_fn))(png_ptr, png_ptr->num_frames_read);
774 png_ptr->num_frames_read++;
775 #endif
777 return;
780 png_ptr->idat_size = png_ptr->push_length;
782 #ifdef PNG_READ_APNG_SUPPORTED
783 if (png_ptr->num_frames_read > 0)
785 png_ensure_sequence_number(png_ptr, 4);
786 png_ptr->idat_size -= 4;
788 #endif
791 if (png_ptr->idat_size != 0 && png_ptr->save_buffer_size != 0)
793 png_size_t save_size = png_ptr->save_buffer_size;
794 png_uint_32 idat_size = png_ptr->idat_size;
796 /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
797 * are of different types and we don't know which variable has the fewest
798 * bits. Carefully select the smaller and cast it to the type of the
799 * larger - this cannot overflow. Do not cast in the following test - it
800 * will break on either 16 or 64 bit platforms.
802 if (idat_size < save_size)
803 save_size = (png_size_t)idat_size;
805 else
806 idat_size = (png_uint_32)save_size;
808 png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
810 png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size);
812 png_ptr->idat_size -= idat_size;
813 png_ptr->buffer_size -= save_size;
814 png_ptr->save_buffer_size -= save_size;
815 png_ptr->save_buffer_ptr += save_size;
818 if (png_ptr->idat_size != 0 && png_ptr->current_buffer_size != 0)
820 png_size_t save_size = png_ptr->current_buffer_size;
821 png_uint_32 idat_size = png_ptr->idat_size;
823 /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
824 * are of different types and we don't know which variable has the fewest
825 * bits. Carefully select the smaller and cast it to the type of the
826 * larger - this cannot overflow.
828 if (idat_size < save_size)
829 save_size = (png_size_t)idat_size;
831 else
832 idat_size = (png_uint_32)save_size;
834 png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
836 png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size);
838 png_ptr->idat_size -= idat_size;
839 png_ptr->buffer_size -= save_size;
840 png_ptr->current_buffer_size -= save_size;
841 png_ptr->current_buffer_ptr += save_size;
843 if (png_ptr->idat_size == 0)
845 PNG_PUSH_SAVE_BUFFER_IF_LT(4)
846 png_crc_finish(png_ptr, 0);
847 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
848 png_ptr->mode |= PNG_AFTER_IDAT;
849 png_ptr->zowner = 0;
853 void /* PRIVATE */
854 png_process_IDAT_data(png_structrp png_ptr, png_bytep buffer,
855 png_size_t buffer_length)
857 /* The caller checks for a non-zero buffer length. */
858 if (!(buffer_length > 0) || buffer == NULL)
859 png_error(png_ptr, "No IDAT data (internal error)");
861 #ifdef PNG_READ_APNG_SUPPORTED
862 /* If the app is not APNG-aware, decode only the first frame */
863 if ((png_ptr->apng_flags & PNG_APNG_APP) == 0 &&
864 png_ptr->num_frames_read > 0)
866 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
867 return;
869 #endif
871 /* This routine must process all the data it has been given
872 * before returning, calling the row callback as required to
873 * handle the uncompressed results.
875 png_ptr->zstream.next_in = buffer;
876 /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
877 png_ptr->zstream.avail_in = (uInt)buffer_length;
879 /* Keep going until the decompressed data is all processed
880 * or the stream marked as finished.
882 while (png_ptr->zstream.avail_in > 0 &&
883 !(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED))
885 int ret;
887 /* We have data for zlib, but we must check that zlib
888 * has someplace to put the results. It doesn't matter
889 * if we don't expect any results -- it may be the input
890 * data is just the LZ end code.
892 if (!(png_ptr->zstream.avail_out > 0))
894 /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
895 png_ptr->zstream.avail_out = (uInt)(PNG_ROWBYTES(png_ptr->pixel_depth,
896 png_ptr->iwidth) + 1);
898 png_ptr->zstream.next_out = png_ptr->row_buf;
901 /* Using Z_SYNC_FLUSH here means that an unterminated
902 * LZ stream (a stream with a missing end code) can still
903 * be handled, otherwise (Z_NO_FLUSH) a future zlib
904 * implementation might defer output and therefore
905 * change the current behavior (see comments in inflate.c
906 * for why this doesn't happen at present with zlib 1.2.5).
908 ret = inflate(&png_ptr->zstream, Z_SYNC_FLUSH);
910 /* Check for any failure before proceeding. */
911 if (ret != Z_OK && ret != Z_STREAM_END)
913 /* Terminate the decompression. */
914 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
915 png_ptr->zowner = 0;
917 /* This may be a truncated stream (missing or
918 * damaged end code). Treat that as a warning.
920 if (png_ptr->row_number >= png_ptr->num_rows ||
921 png_ptr->pass > 6)
922 png_warning(png_ptr, "Truncated compressed data in IDAT");
924 else
925 png_error(png_ptr, "Decompression error in IDAT");
927 /* Skip the check on unprocessed input */
928 return;
931 /* Did inflate output any data? */
932 if (png_ptr->zstream.next_out != png_ptr->row_buf)
934 /* Is this unexpected data after the last row?
935 * If it is, artificially terminate the LZ output
936 * here.
938 if (png_ptr->row_number >= png_ptr->num_rows ||
939 png_ptr->pass > 6)
941 /* Extra data. */
942 png_warning(png_ptr, "Extra compressed data in IDAT");
943 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
944 png_ptr->zowner = 0;
946 /* Do no more processing; skip the unprocessed
947 * input check below.
949 return;
952 /* Do we have a complete row? */
953 if (png_ptr->zstream.avail_out == 0)
954 png_push_process_row(png_ptr);
957 /* And check for the end of the stream. */
958 if (ret == Z_STREAM_END)
959 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
962 /* All the data should have been processed, if anything
963 * is left at this point we have bytes of IDAT data
964 * after the zlib end code.
966 if (png_ptr->zstream.avail_in > 0)
967 png_warning(png_ptr, "Extra compression data in IDAT");
970 void /* PRIVATE */
971 png_push_process_row(png_structrp png_ptr)
973 /* 1.5.6: row_info moved out of png_struct to a local here. */
974 png_row_info row_info;
976 row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */
977 row_info.color_type = png_ptr->color_type;
978 row_info.bit_depth = png_ptr->bit_depth;
979 row_info.channels = png_ptr->channels;
980 row_info.pixel_depth = png_ptr->pixel_depth;
981 row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
983 if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
985 if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
986 png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1,
987 png_ptr->prev_row + 1, png_ptr->row_buf[0]);
988 else
989 png_error(png_ptr, "bad adaptive filter value");
992 /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
993 * 1.5.6, while the buffer really is this big in current versions of libpng
994 * it may not be in the future, so this was changed just to copy the
995 * interlaced row count:
997 memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
999 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
1000 if (png_ptr->transformations != 0)
1001 png_do_read_transformations(png_ptr, &row_info);
1002 #endif
1004 /* The transformed pixel depth should match the depth now in row_info. */
1005 if (png_ptr->transformed_pixel_depth == 0)
1007 png_ptr->transformed_pixel_depth = row_info.pixel_depth;
1008 if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
1009 png_error(png_ptr, "progressive row overflow");
1012 else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
1013 png_error(png_ptr, "internal progressive row size calculation error");
1016 #ifdef PNG_READ_INTERLACING_SUPPORTED
1017 /* Expand interlaced rows to full size */
1018 if (png_ptr->interlaced != 0 &&
1019 (png_ptr->transformations & PNG_INTERLACE) != 0)
1021 if (png_ptr->pass < 6)
1022 png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
1023 png_ptr->transformations);
1025 switch (png_ptr->pass)
1027 case 0:
1029 int i;
1030 for (i = 0; i < 8 && png_ptr->pass == 0; i++)
1032 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1033 png_read_push_finish_row(png_ptr); /* Updates png_ptr->pass */
1036 if (png_ptr->pass == 2) /* Pass 1 might be empty */
1038 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
1040 png_push_have_row(png_ptr, NULL);
1041 png_read_push_finish_row(png_ptr);
1045 if (png_ptr->pass == 4 && png_ptr->height <= 4)
1047 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1049 png_push_have_row(png_ptr, NULL);
1050 png_read_push_finish_row(png_ptr);
1054 if (png_ptr->pass == 6 && png_ptr->height <= 4)
1056 png_push_have_row(png_ptr, NULL);
1057 png_read_push_finish_row(png_ptr);
1060 break;
1063 case 1:
1065 int i;
1066 for (i = 0; i < 8 && png_ptr->pass == 1; i++)
1068 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1069 png_read_push_finish_row(png_ptr);
1072 if (png_ptr->pass == 2) /* Skip top 4 generated rows */
1074 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
1076 png_push_have_row(png_ptr, NULL);
1077 png_read_push_finish_row(png_ptr);
1081 break;
1084 case 2:
1086 int i;
1088 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
1090 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1091 png_read_push_finish_row(png_ptr);
1094 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
1096 png_push_have_row(png_ptr, NULL);
1097 png_read_push_finish_row(png_ptr);
1100 if (png_ptr->pass == 4) /* Pass 3 might be empty */
1102 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1104 png_push_have_row(png_ptr, NULL);
1105 png_read_push_finish_row(png_ptr);
1109 break;
1112 case 3:
1114 int i;
1116 for (i = 0; i < 4 && png_ptr->pass == 3; i++)
1118 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1119 png_read_push_finish_row(png_ptr);
1122 if (png_ptr->pass == 4) /* Skip top two generated rows */
1124 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1126 png_push_have_row(png_ptr, NULL);
1127 png_read_push_finish_row(png_ptr);
1131 break;
1134 case 4:
1136 int i;
1138 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1140 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1141 png_read_push_finish_row(png_ptr);
1144 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1146 png_push_have_row(png_ptr, NULL);
1147 png_read_push_finish_row(png_ptr);
1150 if (png_ptr->pass == 6) /* Pass 5 might be empty */
1152 png_push_have_row(png_ptr, NULL);
1153 png_read_push_finish_row(png_ptr);
1156 break;
1159 case 5:
1161 int i;
1163 for (i = 0; i < 2 && png_ptr->pass == 5; i++)
1165 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1166 png_read_push_finish_row(png_ptr);
1169 if (png_ptr->pass == 6) /* Skip top generated row */
1171 png_push_have_row(png_ptr, NULL);
1172 png_read_push_finish_row(png_ptr);
1175 break;
1178 default:
1179 case 6:
1181 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1182 png_read_push_finish_row(png_ptr);
1184 if (png_ptr->pass != 6)
1185 break;
1187 png_push_have_row(png_ptr, NULL);
1188 png_read_push_finish_row(png_ptr);
1192 else
1194 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1195 png_read_push_finish_row(png_ptr);
1199 void /* PRIVATE */
1200 png_read_push_finish_row(png_structrp png_ptr)
1202 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1204 /* Start of interlace block */
1205 static PNG_CONST png_byte png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
1207 /* Offset to next interlace block */
1208 static PNG_CONST png_byte png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
1210 /* Start of interlace block in the y direction */
1211 static PNG_CONST png_byte png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
1213 /* Offset to next interlace block in the y direction */
1214 static PNG_CONST png_byte png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
1216 /* Height of interlace block. This is not currently used - if you need
1217 * it, uncomment it here and in png.h
1218 static PNG_CONST png_byte png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
1220 #endif
1222 png_ptr->row_number++;
1223 if (png_ptr->row_number < png_ptr->num_rows)
1224 return;
1226 if (png_ptr->interlaced != 0)
1228 png_ptr->row_number = 0;
1229 memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
1233 png_ptr->pass++;
1234 if ((png_ptr->pass == 1 && png_ptr->width < 5) ||
1235 (png_ptr->pass == 3 && png_ptr->width < 3) ||
1236 (png_ptr->pass == 5 && png_ptr->width < 2))
1237 png_ptr->pass++;
1239 if (png_ptr->pass > 7)
1240 png_ptr->pass--;
1242 if (png_ptr->pass >= 7)
1243 break;
1245 png_ptr->iwidth = (png_ptr->width +
1246 png_pass_inc[png_ptr->pass] - 1 -
1247 png_pass_start[png_ptr->pass]) /
1248 png_pass_inc[png_ptr->pass];
1250 if ((png_ptr->transformations & PNG_INTERLACE) != 0)
1251 break;
1253 png_ptr->num_rows = (png_ptr->height +
1254 png_pass_yinc[png_ptr->pass] - 1 -
1255 png_pass_ystart[png_ptr->pass]) /
1256 png_pass_yinc[png_ptr->pass];
1258 } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0);
1262 void /* PRIVATE */
1263 png_push_have_info(png_structrp png_ptr, png_inforp info_ptr)
1265 if (png_ptr->info_fn != NULL)
1266 (*(png_ptr->info_fn))(png_ptr, info_ptr);
1269 void /* PRIVATE */
1270 png_push_have_end(png_structrp png_ptr, png_inforp info_ptr)
1272 if (png_ptr->end_fn != NULL)
1273 (*(png_ptr->end_fn))(png_ptr, info_ptr);
1276 void /* PRIVATE */
1277 png_push_have_row(png_structrp png_ptr, png_bytep row)
1279 if (png_ptr->row_fn != NULL)
1280 (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number,
1281 (int)png_ptr->pass);
1284 void PNGAPI
1285 png_progressive_combine_row(png_const_structrp png_ptr, png_bytep old_row,
1286 png_const_bytep new_row)
1288 if (png_ptr == NULL)
1289 return;
1291 /* new_row is a flag here - if it is NULL then the app callback was called
1292 * from an empty row (see the calls to png_struct::row_fn below), otherwise
1293 * it must be png_ptr->row_buf+1
1295 if (new_row != NULL)
1296 png_combine_row(png_ptr, old_row, 1/*blocky display*/);
1299 void PNGAPI
1300 png_set_progressive_read_fn(png_structrp png_ptr, png_voidp progressive_ptr,
1301 png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
1302 png_progressive_end_ptr end_fn)
1304 if (png_ptr == NULL)
1305 return;
1307 png_ptr->info_fn = info_fn;
1308 png_ptr->row_fn = row_fn;
1309 png_ptr->end_fn = end_fn;
1311 png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer);
1314 #ifdef PNG_READ_APNG_SUPPORTED
1315 void PNGAPI
1316 png_set_progressive_frame_fn(png_structp png_ptr,
1317 png_progressive_frame_ptr frame_info_fn,
1318 png_progressive_frame_ptr frame_end_fn)
1320 png_ptr->frame_info_fn = frame_info_fn;
1321 png_ptr->frame_end_fn = frame_end_fn;
1322 png_ptr->apng_flags |= PNG_APNG_APP;
1324 #endif
1326 png_voidp PNGAPI
1327 png_get_progressive_ptr(png_const_structrp png_ptr)
1329 if (png_ptr == NULL)
1330 return (NULL);
1332 return png_ptr->io_ptr;
1334 #endif /* PROGRESSIVE_READ */