2 /* pngpread.c - read a png file in push mode
4 * Last changed in libpng 1.5.1 [February 3, 2011]
5 * Copyright (c) 1998-2011 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
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
30 png_process_data(png_structp png_ptr
, png_infop info_ptr
,
31 png_bytep buffer
, png_size_t buffer_size
)
33 if (png_ptr
== NULL
|| info_ptr
== NULL
)
36 png_push_restore_buffer(png_ptr
, buffer
, buffer_size
);
38 while (png_ptr
->buffer_size
)
40 png_process_some_data(png_ptr
, info_ptr
);
45 png_process_data_pause(png_structp png_ptr
, int save
)
49 /* It's easiest for the caller if we do the save, then the caller doesn't
50 * have to supply the same data again:
53 png_push_save_buffer(png_ptr
);
56 /* This includes any pending saved bytes: */
57 png_size_t remaining
= png_ptr
->buffer_size
;
58 png_ptr
->buffer_size
= 0;
60 /* So subtract the saved buffer size, unless all the data
61 * is actually 'saved', in which case we just return 0
63 if (png_ptr
->save_buffer_size
< remaining
)
64 return remaining
- png_ptr
->save_buffer_size
;
72 png_process_data_skip(png_structp png_ptr
)
74 png_uint_32 remaining
= 0;
76 if (png_ptr
!= NULL
&& png_ptr
->process_mode
== PNG_SKIP_MODE
&&
77 png_ptr
->skip_length
> 0)
79 /* At the end of png_process_data the buffer size must be 0 (see the loop
80 * above) so we can detect a broken call here:
82 if (png_ptr
->buffer_size
!= 0)
84 "png_process_data_skip called inside png_process_data");
86 /* If is impossible for there to be a saved buffer at this point -
87 * otherwise we could not be in SKIP mode. This will also happen if
88 * png_process_skip is called inside png_process_data (but only very
91 if (png_ptr
->save_buffer_size
!= 0)
92 png_error(png_ptr
, "png_process_data_skip called with saved data");
94 remaining
= png_ptr
->skip_length
;
95 png_ptr
->skip_length
= 0;
96 png_ptr
->process_mode
= PNG_READ_CHUNK_MODE
;
102 /* What we do with the incoming data depends on what we were previously
103 * doing before we ran out of data...
106 png_process_some_data(png_structp png_ptr
, png_infop info_ptr
)
111 switch (png_ptr
->process_mode
)
113 case PNG_READ_SIG_MODE
:
115 png_push_read_sig(png_ptr
, info_ptr
);
119 case PNG_READ_CHUNK_MODE
:
121 png_push_read_chunk(png_ptr
, info_ptr
);
125 case PNG_READ_IDAT_MODE
:
127 png_push_read_IDAT(png_ptr
);
131 #ifdef PNG_READ_tEXt_SUPPORTED
132 case PNG_READ_tEXt_MODE
:
134 png_push_read_tEXt(png_ptr
, info_ptr
);
139 #ifdef PNG_READ_zTXt_SUPPORTED
140 case PNG_READ_zTXt_MODE
:
142 png_push_read_zTXt(png_ptr
, info_ptr
);
147 #ifdef PNG_READ_iTXt_SUPPORTED
148 case PNG_READ_iTXt_MODE
:
150 png_push_read_iTXt(png_ptr
, info_ptr
);
157 png_push_crc_finish(png_ptr
);
163 png_ptr
->buffer_size
= 0;
169 /* Read any remaining signature bytes from the stream and compare them with
170 * the correct PNG signature. It is possible that this routine is called
171 * with bytes already read from the signature, either because they have been
172 * checked by the calling application, or because of multiple calls to this
176 png_push_read_sig(png_structp png_ptr
, png_infop info_ptr
)
178 png_size_t num_checked
= png_ptr
->sig_bytes
,
179 num_to_check
= 8 - num_checked
;
181 if (png_ptr
->buffer_size
< num_to_check
)
183 num_to_check
= png_ptr
->buffer_size
;
186 png_push_fill_buffer(png_ptr
, &(info_ptr
->signature
[num_checked
]),
188 png_ptr
->sig_bytes
= (png_byte
)(png_ptr
->sig_bytes
+ num_to_check
);
190 if (png_sig_cmp(info_ptr
->signature
, num_checked
, num_to_check
))
192 if (num_checked
< 4 &&
193 png_sig_cmp(info_ptr
->signature
, num_checked
, num_to_check
- 4))
194 png_error(png_ptr
, "Not a PNG file");
197 png_error(png_ptr
, "PNG file corrupted by ASCII conversion");
201 if (png_ptr
->sig_bytes
>= 8)
203 png_ptr
->process_mode
= PNG_READ_CHUNK_MODE
;
209 png_push_read_chunk(png_structp png_ptr
, png_infop info_ptr
)
215 #ifdef PNG_READ_bKGD_SUPPORTED
218 #ifdef PNG_READ_cHRM_SUPPORTED
221 #ifdef PNG_READ_gAMA_SUPPORTED
224 #ifdef PNG_READ_hIST_SUPPORTED
227 #ifdef PNG_READ_iCCP_SUPPORTED
230 #ifdef PNG_READ_iTXt_SUPPORTED
233 #ifdef PNG_READ_oFFs_SUPPORTED
236 #ifdef PNG_READ_pCAL_SUPPORTED
239 #ifdef PNG_READ_pHYs_SUPPORTED
242 #ifdef PNG_READ_sBIT_SUPPORTED
245 #ifdef PNG_READ_sCAL_SUPPORTED
248 #ifdef PNG_READ_sRGB_SUPPORTED
251 #ifdef PNG_READ_sPLT_SUPPORTED
254 #ifdef PNG_READ_tEXt_SUPPORTED
257 #ifdef PNG_READ_tIME_SUPPORTED
260 #ifdef PNG_READ_tRNS_SUPPORTED
263 #ifdef PNG_READ_zTXt_SUPPORTED
267 /* First we make sure we have enough data for the 4 byte chunk name
268 * and the 4 byte chunk length before proceeding with decoding the
269 * chunk data. To fully decode each of these chunks, we also make
270 * sure we have enough data in the buffer for the 4 byte CRC at the
271 * end of every chunk (except IDAT, which is handled separately).
273 if (!(png_ptr
->mode
& PNG_HAVE_CHUNK_HEADER
))
275 png_byte chunk_length
[4];
277 if (png_ptr
->buffer_size
< 8)
279 png_push_save_buffer(png_ptr
);
283 png_push_fill_buffer(png_ptr
, chunk_length
, 4);
284 png_ptr
->push_length
= png_get_uint_31(png_ptr
, chunk_length
);
285 png_reset_crc(png_ptr
);
286 png_crc_read(png_ptr
, png_ptr
->chunk_name
, 4);
287 png_check_chunk_name(png_ptr
, png_ptr
->chunk_name
);
288 png_ptr
->mode
|= PNG_HAVE_CHUNK_HEADER
;
291 if (!png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
292 if (png_ptr
->mode
& PNG_AFTER_IDAT
)
293 png_ptr
->mode
|= PNG_HAVE_CHUNK_AFTER_IDAT
;
295 if (!png_memcmp(png_ptr
->chunk_name
, png_IHDR
, 4))
297 if (png_ptr
->push_length
!= 13)
298 png_error(png_ptr
, "Invalid IHDR length");
300 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
302 png_push_save_buffer(png_ptr
);
306 png_handle_IHDR(png_ptr
, info_ptr
, png_ptr
->push_length
);
309 else if (!png_memcmp(png_ptr
->chunk_name
, png_IEND
, 4))
311 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
313 png_push_save_buffer(png_ptr
);
317 png_handle_IEND(png_ptr
, info_ptr
, png_ptr
->push_length
);
319 png_ptr
->process_mode
= PNG_READ_DONE_MODE
;
320 png_push_have_end(png_ptr
, info_ptr
);
323 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
324 else if (png_handle_as_unknown(png_ptr
, png_ptr
->chunk_name
))
326 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
328 png_push_save_buffer(png_ptr
);
332 if (!png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
333 png_ptr
->mode
|= PNG_HAVE_IDAT
;
335 png_handle_unknown(png_ptr
, info_ptr
, png_ptr
->push_length
);
337 if (!png_memcmp(png_ptr
->chunk_name
, png_PLTE
, 4))
338 png_ptr
->mode
|= PNG_HAVE_PLTE
;
340 else if (!png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
342 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
343 png_error(png_ptr
, "Missing IHDR before IDAT");
345 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
&&
346 !(png_ptr
->mode
& PNG_HAVE_PLTE
))
347 png_error(png_ptr
, "Missing PLTE before IDAT");
352 else if (!png_memcmp(png_ptr
->chunk_name
, png_PLTE
, 4))
354 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
356 png_push_save_buffer(png_ptr
);
359 png_handle_PLTE(png_ptr
, info_ptr
, png_ptr
->push_length
);
362 else if (!png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
364 /* If we reach an IDAT chunk, this means we have read all of the
365 * header chunks, and we can start reading the image (or if this
366 * is called after the image has been read - we have an error).
369 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
370 png_error(png_ptr
, "Missing IHDR before IDAT");
372 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
&&
373 !(png_ptr
->mode
& PNG_HAVE_PLTE
))
374 png_error(png_ptr
, "Missing PLTE before IDAT");
376 if (png_ptr
->mode
& PNG_HAVE_IDAT
)
378 if (!(png_ptr
->mode
& PNG_HAVE_CHUNK_AFTER_IDAT
))
379 if (png_ptr
->push_length
== 0)
382 if (png_ptr
->mode
& PNG_AFTER_IDAT
)
383 png_benign_error(png_ptr
, "Too many IDATs found");
386 png_ptr
->idat_size
= png_ptr
->push_length
;
387 png_ptr
->mode
|= PNG_HAVE_IDAT
;
388 png_ptr
->process_mode
= PNG_READ_IDAT_MODE
;
389 png_push_have_info(png_ptr
, info_ptr
);
390 png_ptr
->zstream
.avail_out
=
391 (uInt
) PNG_ROWBYTES(png_ptr
->pixel_depth
,
392 png_ptr
->iwidth
) + 1;
393 png_ptr
->zstream
.next_out
= png_ptr
->row_buf
;
397 #ifdef PNG_READ_gAMA_SUPPORTED
398 else if (!png_memcmp(png_ptr
->chunk_name
, png_gAMA
, 4))
400 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
402 png_push_save_buffer(png_ptr
);
406 png_handle_gAMA(png_ptr
, info_ptr
, png_ptr
->push_length
);
410 #ifdef PNG_READ_sBIT_SUPPORTED
411 else if (!png_memcmp(png_ptr
->chunk_name
, png_sBIT
, 4))
413 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
415 png_push_save_buffer(png_ptr
);
419 png_handle_sBIT(png_ptr
, info_ptr
, png_ptr
->push_length
);
423 #ifdef PNG_READ_cHRM_SUPPORTED
424 else if (!png_memcmp(png_ptr
->chunk_name
, png_cHRM
, 4))
426 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
428 png_push_save_buffer(png_ptr
);
432 png_handle_cHRM(png_ptr
, info_ptr
, png_ptr
->push_length
);
436 #ifdef PNG_READ_sRGB_SUPPORTED
437 else if (!png_memcmp(png_ptr
->chunk_name
, png_sRGB
, 4))
439 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
441 png_push_save_buffer(png_ptr
);
445 png_handle_sRGB(png_ptr
, info_ptr
, png_ptr
->push_length
);
449 #ifdef PNG_READ_iCCP_SUPPORTED
450 else if (!png_memcmp(png_ptr
->chunk_name
, png_iCCP
, 4))
452 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
454 png_push_save_buffer(png_ptr
);
458 png_handle_iCCP(png_ptr
, info_ptr
, png_ptr
->push_length
);
462 #ifdef PNG_READ_sPLT_SUPPORTED
463 else if (!png_memcmp(png_ptr
->chunk_name
, png_sPLT
, 4))
465 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
467 png_push_save_buffer(png_ptr
);
471 png_handle_sPLT(png_ptr
, info_ptr
, png_ptr
->push_length
);
475 #ifdef PNG_READ_tRNS_SUPPORTED
476 else if (!png_memcmp(png_ptr
->chunk_name
, png_tRNS
, 4))
478 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
480 png_push_save_buffer(png_ptr
);
484 png_handle_tRNS(png_ptr
, info_ptr
, png_ptr
->push_length
);
488 #ifdef PNG_READ_bKGD_SUPPORTED
489 else if (!png_memcmp(png_ptr
->chunk_name
, png_bKGD
, 4))
491 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
493 png_push_save_buffer(png_ptr
);
497 png_handle_bKGD(png_ptr
, info_ptr
, png_ptr
->push_length
);
501 #ifdef PNG_READ_hIST_SUPPORTED
502 else if (!png_memcmp(png_ptr
->chunk_name
, png_hIST
, 4))
504 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
506 png_push_save_buffer(png_ptr
);
510 png_handle_hIST(png_ptr
, info_ptr
, png_ptr
->push_length
);
514 #ifdef PNG_READ_pHYs_SUPPORTED
515 else if (!png_memcmp(png_ptr
->chunk_name
, png_pHYs
, 4))
517 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
519 png_push_save_buffer(png_ptr
);
523 png_handle_pHYs(png_ptr
, info_ptr
, png_ptr
->push_length
);
527 #ifdef PNG_READ_oFFs_SUPPORTED
528 else if (!png_memcmp(png_ptr
->chunk_name
, png_oFFs
, 4))
530 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
532 png_push_save_buffer(png_ptr
);
536 png_handle_oFFs(png_ptr
, info_ptr
, png_ptr
->push_length
);
540 #ifdef PNG_READ_pCAL_SUPPORTED
541 else if (!png_memcmp(png_ptr
->chunk_name
, png_pCAL
, 4))
543 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
545 png_push_save_buffer(png_ptr
);
549 png_handle_pCAL(png_ptr
, info_ptr
, png_ptr
->push_length
);
553 #ifdef PNG_READ_sCAL_SUPPORTED
554 else if (!png_memcmp(png_ptr
->chunk_name
, png_sCAL
, 4))
556 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
558 png_push_save_buffer(png_ptr
);
562 png_handle_sCAL(png_ptr
, info_ptr
, png_ptr
->push_length
);
566 #ifdef PNG_READ_tIME_SUPPORTED
567 else if (!png_memcmp(png_ptr
->chunk_name
, png_tIME
, 4))
569 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
571 png_push_save_buffer(png_ptr
);
575 png_handle_tIME(png_ptr
, info_ptr
, png_ptr
->push_length
);
579 #ifdef PNG_READ_tEXt_SUPPORTED
580 else if (!png_memcmp(png_ptr
->chunk_name
, png_tEXt
, 4))
582 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
584 png_push_save_buffer(png_ptr
);
588 png_push_handle_tEXt(png_ptr
, info_ptr
, png_ptr
->push_length
);
592 #ifdef PNG_READ_zTXt_SUPPORTED
593 else if (!png_memcmp(png_ptr
->chunk_name
, png_zTXt
, 4))
595 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
597 png_push_save_buffer(png_ptr
);
601 png_push_handle_zTXt(png_ptr
, info_ptr
, png_ptr
->push_length
);
605 #ifdef PNG_READ_iTXt_SUPPORTED
606 else if (!png_memcmp(png_ptr
->chunk_name
, png_iTXt
, 4))
608 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
610 png_push_save_buffer(png_ptr
);
614 png_push_handle_iTXt(png_ptr
, info_ptr
, png_ptr
->push_length
);
620 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
622 png_push_save_buffer(png_ptr
);
625 png_push_handle_unknown(png_ptr
, info_ptr
, png_ptr
->push_length
);
628 png_ptr
->mode
&= ~PNG_HAVE_CHUNK_HEADER
;
632 png_push_crc_skip(png_structp png_ptr
, png_uint_32 skip
)
634 png_ptr
->process_mode
= PNG_SKIP_MODE
;
635 png_ptr
->skip_length
= skip
;
639 png_push_crc_finish(png_structp png_ptr
)
641 if (png_ptr
->skip_length
&& png_ptr
->save_buffer_size
)
643 png_size_t save_size
= png_ptr
->save_buffer_size
;
644 png_uint_32 skip_length
= png_ptr
->skip_length
;
646 /* We want the smaller of 'skip_length' and 'save_buffer_size', but
647 * they are of different types and we don't know which variable has the
648 * fewest bits. Carefully select the smaller and cast it to the type of
649 * the larger - this cannot overflow. Do not cast in the following test
650 * - it will break on either 16 or 64 bit platforms.
652 if (skip_length
< save_size
)
653 save_size
= (png_size_t
)skip_length
;
656 skip_length
= (png_uint_32
)save_size
;
658 png_calculate_crc(png_ptr
, png_ptr
->save_buffer_ptr
, save_size
);
660 png_ptr
->skip_length
-= skip_length
;
661 png_ptr
->buffer_size
-= save_size
;
662 png_ptr
->save_buffer_size
-= save_size
;
663 png_ptr
->save_buffer_ptr
+= save_size
;
665 if (png_ptr
->skip_length
&& png_ptr
->current_buffer_size
)
667 png_size_t save_size
= png_ptr
->current_buffer_size
;
668 png_uint_32 skip_length
= png_ptr
->skip_length
;
670 /* We want the smaller of 'skip_length' and 'current_buffer_size', here,
671 * the same problem exists as above and the same solution.
673 if (skip_length
< save_size
)
674 save_size
= (png_size_t
)skip_length
;
677 skip_length
= (png_uint_32
)save_size
;
679 png_calculate_crc(png_ptr
, png_ptr
->current_buffer_ptr
, save_size
);
681 png_ptr
->skip_length
-= skip_length
;
682 png_ptr
->buffer_size
-= save_size
;
683 png_ptr
->current_buffer_size
-= save_size
;
684 png_ptr
->current_buffer_ptr
+= save_size
;
686 if (!png_ptr
->skip_length
)
688 if (png_ptr
->buffer_size
< 4)
690 png_push_save_buffer(png_ptr
);
694 png_crc_finish(png_ptr
, 0);
695 png_ptr
->process_mode
= PNG_READ_CHUNK_MODE
;
700 png_push_fill_buffer(png_structp png_ptr
, png_bytep buffer
, png_size_t length
)
708 if (png_ptr
->save_buffer_size
)
710 png_size_t save_size
;
712 if (length
< png_ptr
->save_buffer_size
)
716 save_size
= png_ptr
->save_buffer_size
;
718 png_memcpy(ptr
, png_ptr
->save_buffer_ptr
, save_size
);
721 png_ptr
->buffer_size
-= save_size
;
722 png_ptr
->save_buffer_size
-= save_size
;
723 png_ptr
->save_buffer_ptr
+= save_size
;
725 if (length
&& png_ptr
->current_buffer_size
)
727 png_size_t save_size
;
729 if (length
< png_ptr
->current_buffer_size
)
733 save_size
= png_ptr
->current_buffer_size
;
735 png_memcpy(ptr
, png_ptr
->current_buffer_ptr
, save_size
);
736 png_ptr
->buffer_size
-= save_size
;
737 png_ptr
->current_buffer_size
-= save_size
;
738 png_ptr
->current_buffer_ptr
+= save_size
;
743 png_push_save_buffer(png_structp png_ptr
)
745 if (png_ptr
->save_buffer_size
)
747 if (png_ptr
->save_buffer_ptr
!= png_ptr
->save_buffer
)
753 istop
= png_ptr
->save_buffer_size
;
754 for (i
= 0, sp
= png_ptr
->save_buffer_ptr
, dp
= png_ptr
->save_buffer
;
755 i
< istop
; i
++, sp
++, dp
++)
761 if (png_ptr
->save_buffer_size
+ png_ptr
->current_buffer_size
>
762 png_ptr
->save_buffer_max
)
765 png_bytep old_buffer
;
767 if (png_ptr
->save_buffer_size
> PNG_SIZE_MAX
-
768 (png_ptr
->current_buffer_size
+ 256))
770 png_error(png_ptr
, "Potential overflow of save_buffer");
773 new_max
= png_ptr
->save_buffer_size
+ png_ptr
->current_buffer_size
+ 256;
774 old_buffer
= png_ptr
->save_buffer
;
775 png_ptr
->save_buffer
= (png_bytep
)png_malloc_warn(png_ptr
,
776 (png_size_t
)new_max
);
778 if (png_ptr
->save_buffer
== NULL
)
780 png_free(png_ptr
, old_buffer
);
781 png_error(png_ptr
, "Insufficient memory for save_buffer");
784 png_memcpy(png_ptr
->save_buffer
, old_buffer
, png_ptr
->save_buffer_size
);
785 png_free(png_ptr
, old_buffer
);
786 png_ptr
->save_buffer_max
= new_max
;
788 if (png_ptr
->current_buffer_size
)
790 png_memcpy(png_ptr
->save_buffer
+ png_ptr
->save_buffer_size
,
791 png_ptr
->current_buffer_ptr
, png_ptr
->current_buffer_size
);
792 png_ptr
->save_buffer_size
+= png_ptr
->current_buffer_size
;
793 png_ptr
->current_buffer_size
= 0;
795 png_ptr
->save_buffer_ptr
= png_ptr
->save_buffer
;
796 png_ptr
->buffer_size
= 0;
800 png_push_restore_buffer(png_structp png_ptr
, png_bytep buffer
,
801 png_size_t buffer_length
)
803 png_ptr
->current_buffer
= buffer
;
804 png_ptr
->current_buffer_size
= buffer_length
;
805 png_ptr
->buffer_size
= buffer_length
+ png_ptr
->save_buffer_size
;
806 png_ptr
->current_buffer_ptr
= png_ptr
->current_buffer
;
810 png_push_read_IDAT(png_structp png_ptr
)
813 if (!(png_ptr
->mode
& PNG_HAVE_CHUNK_HEADER
))
815 png_byte chunk_length
[4];
817 if (png_ptr
->buffer_size
< 8)
819 png_push_save_buffer(png_ptr
);
823 png_push_fill_buffer(png_ptr
, chunk_length
, 4);
824 png_ptr
->push_length
= png_get_uint_31(png_ptr
, chunk_length
);
825 png_reset_crc(png_ptr
);
826 png_crc_read(png_ptr
, png_ptr
->chunk_name
, 4);
827 png_ptr
->mode
|= PNG_HAVE_CHUNK_HEADER
;
829 if (png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
831 png_ptr
->process_mode
= PNG_READ_CHUNK_MODE
;
833 if (!(png_ptr
->flags
& PNG_FLAG_ZLIB_FINISHED
))
834 png_error(png_ptr
, "Not enough compressed data");
839 png_ptr
->idat_size
= png_ptr
->push_length
;
841 if (png_ptr
->idat_size
&& png_ptr
->save_buffer_size
)
843 png_size_t save_size
= png_ptr
->save_buffer_size
;
844 png_uint_32 idat_size
= png_ptr
->idat_size
;
846 /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
847 * are of different types and we don't know which variable has the fewest
848 * bits. Carefully select the smaller and cast it to the type of the
849 * larger - this cannot overflow. Do not cast in the following test - it
850 * will break on either 16 or 64 bit platforms.
852 if (idat_size
< save_size
)
853 save_size
= (png_size_t
)idat_size
;
856 idat_size
= (png_uint_32
)save_size
;
858 png_calculate_crc(png_ptr
, png_ptr
->save_buffer_ptr
, save_size
);
860 png_process_IDAT_data(png_ptr
, png_ptr
->save_buffer_ptr
, save_size
);
862 png_ptr
->idat_size
-= idat_size
;
863 png_ptr
->buffer_size
-= save_size
;
864 png_ptr
->save_buffer_size
-= save_size
;
865 png_ptr
->save_buffer_ptr
+= save_size
;
868 if (png_ptr
->idat_size
&& png_ptr
->current_buffer_size
)
870 png_size_t save_size
= png_ptr
->current_buffer_size
;
871 png_uint_32 idat_size
= png_ptr
->idat_size
;
873 /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
874 * are of different types and we don't know which variable has the fewest
875 * bits. Carefully select the smaller and cast it to the type of the
876 * larger - this cannot overflow.
878 if (idat_size
< save_size
)
879 save_size
= (png_size_t
)idat_size
;
882 idat_size
= (png_uint_32
)save_size
;
884 png_calculate_crc(png_ptr
, png_ptr
->current_buffer_ptr
, save_size
);
886 png_process_IDAT_data(png_ptr
, png_ptr
->current_buffer_ptr
, save_size
);
888 png_ptr
->idat_size
-= idat_size
;
889 png_ptr
->buffer_size
-= save_size
;
890 png_ptr
->current_buffer_size
-= save_size
;
891 png_ptr
->current_buffer_ptr
+= save_size
;
893 if (!png_ptr
->idat_size
)
895 if (png_ptr
->buffer_size
< 4)
897 png_push_save_buffer(png_ptr
);
901 png_crc_finish(png_ptr
, 0);
902 png_ptr
->mode
&= ~PNG_HAVE_CHUNK_HEADER
;
903 png_ptr
->mode
|= PNG_AFTER_IDAT
;
908 png_process_IDAT_data(png_structp png_ptr
, png_bytep buffer
,
909 png_size_t buffer_length
)
911 /* The caller checks for a non-zero buffer length. */
912 if (!(buffer_length
> 0) || buffer
== NULL
)
913 png_error(png_ptr
, "No IDAT data (internal error)");
915 /* This routine must process all the data it has been given
916 * before returning, calling the row callback as required to
917 * handle the uncompressed results.
919 png_ptr
->zstream
.next_in
= buffer
;
920 png_ptr
->zstream
.avail_in
= (uInt
)buffer_length
;
922 /* Keep going until the decompressed data is all processed
923 * or the stream marked as finished.
925 while (png_ptr
->zstream
.avail_in
> 0 &&
926 !(png_ptr
->flags
& PNG_FLAG_ZLIB_FINISHED
))
930 /* We have data for zlib, but we must check that zlib
931 * has someplace to put the results. It doesn't matter
932 * if we don't expect any results -- it may be the input
933 * data is just the LZ end code.
935 if (!(png_ptr
->zstream
.avail_out
> 0))
937 png_ptr
->zstream
.avail_out
=
938 (uInt
) PNG_ROWBYTES(png_ptr
->pixel_depth
,
939 png_ptr
->iwidth
) + 1;
941 png_ptr
->zstream
.next_out
= png_ptr
->row_buf
;
944 /* Using Z_SYNC_FLUSH here means that an unterminated
945 * LZ stream (a stream with a missing end code) can still
946 * be handled, otherwise (Z_NO_FLUSH) a future zlib
947 * implementation might defer output and therefore
948 * change the current behavior (see comments in inflate.c
949 * for why this doesn't happen at present with zlib 1.2.5).
951 ret
= inflate(&png_ptr
->zstream
, Z_SYNC_FLUSH
);
953 /* Check for any failure before proceeding. */
954 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
)
956 /* Terminate the decompression. */
957 png_ptr
->flags
|= PNG_FLAG_ZLIB_FINISHED
;
959 /* This may be a truncated stream (missing or
960 * damaged end code). Treat that as a warning.
962 if (png_ptr
->row_number
>= png_ptr
->num_rows
||
964 png_warning(png_ptr
, "Truncated compressed data in IDAT");
967 png_error(png_ptr
, "Decompression error in IDAT");
969 /* Skip the check on unprocessed input */
973 /* Did inflate output any data? */
974 if (png_ptr
->zstream
.next_out
!= png_ptr
->row_buf
)
976 /* Is this unexpected data after the last row?
977 * If it is, artificially terminate the LZ output
980 if (png_ptr
->row_number
>= png_ptr
->num_rows
||
984 png_warning(png_ptr
, "Extra compressed data in IDAT");
985 png_ptr
->flags
|= PNG_FLAG_ZLIB_FINISHED
;
987 /* Do no more processing; skip the unprocessed
993 /* Do we have a complete row? */
994 if (png_ptr
->zstream
.avail_out
== 0)
995 png_push_process_row(png_ptr
);
998 /* And check for the end of the stream. */
999 if (ret
== Z_STREAM_END
)
1000 png_ptr
->flags
|= PNG_FLAG_ZLIB_FINISHED
;
1003 /* All the data should have been processed, if anything
1004 * is left at this point we have bytes of IDAT data
1005 * after the zlib end code.
1007 if (png_ptr
->zstream
.avail_in
> 0)
1008 png_warning(png_ptr
, "Extra compression data in IDAT");
1012 png_push_process_row(png_structp png_ptr
)
1014 png_ptr
->row_info
.color_type
= png_ptr
->color_type
;
1015 png_ptr
->row_info
.width
= png_ptr
->iwidth
;
1016 png_ptr
->row_info
.channels
= png_ptr
->channels
;
1017 png_ptr
->row_info
.bit_depth
= png_ptr
->bit_depth
;
1018 png_ptr
->row_info
.pixel_depth
= png_ptr
->pixel_depth
;
1020 png_ptr
->row_info
.rowbytes
= PNG_ROWBYTES(png_ptr
->row_info
.pixel_depth
,
1021 png_ptr
->row_info
.width
);
1023 png_read_filter_row(png_ptr
, &(png_ptr
->row_info
),
1024 png_ptr
->row_buf
+ 1, png_ptr
->prev_row
+ 1,
1025 (int)(png_ptr
->row_buf
[0]));
1027 png_memcpy(png_ptr
->prev_row
, png_ptr
->row_buf
, png_ptr
->rowbytes
+ 1);
1029 if (png_ptr
->transformations
|| (png_ptr
->flags
&PNG_FLAG_STRIP_ALPHA
))
1030 png_do_read_transformations(png_ptr
);
1032 #ifdef PNG_READ_INTERLACING_SUPPORTED
1033 /* Blow up interlaced rows to full size */
1034 if (png_ptr
->interlaced
&& (png_ptr
->transformations
& PNG_INTERLACE
))
1036 if (png_ptr
->pass
< 6)
1037 /* old interface (pre-1.0.9):
1038 png_do_read_interlace(&(png_ptr->row_info),
1039 png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
1041 png_do_read_interlace(png_ptr
);
1043 switch (png_ptr
->pass
)
1048 for (i
= 0; i
< 8 && png_ptr
->pass
== 0; i
++)
1050 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
1051 png_read_push_finish_row(png_ptr
); /* Updates png_ptr->pass */
1054 if (png_ptr
->pass
== 2) /* Pass 1 might be empty */
1056 for (i
= 0; i
< 4 && png_ptr
->pass
== 2; i
++)
1058 png_push_have_row(png_ptr
, NULL
);
1059 png_read_push_finish_row(png_ptr
);
1063 if (png_ptr
->pass
== 4 && png_ptr
->height
<= 4)
1065 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
1067 png_push_have_row(png_ptr
, NULL
);
1068 png_read_push_finish_row(png_ptr
);
1072 if (png_ptr
->pass
== 6 && png_ptr
->height
<= 4)
1074 png_push_have_row(png_ptr
, NULL
);
1075 png_read_push_finish_row(png_ptr
);
1084 for (i
= 0; i
< 8 && png_ptr
->pass
== 1; i
++)
1086 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
1087 png_read_push_finish_row(png_ptr
);
1090 if (png_ptr
->pass
== 2) /* Skip top 4 generated rows */
1092 for (i
= 0; i
< 4 && png_ptr
->pass
== 2; i
++)
1094 png_push_have_row(png_ptr
, NULL
);
1095 png_read_push_finish_row(png_ptr
);
1106 for (i
= 0; i
< 4 && png_ptr
->pass
== 2; i
++)
1108 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
1109 png_read_push_finish_row(png_ptr
);
1112 for (i
= 0; i
< 4 && png_ptr
->pass
== 2; i
++)
1114 png_push_have_row(png_ptr
, NULL
);
1115 png_read_push_finish_row(png_ptr
);
1118 if (png_ptr
->pass
== 4) /* Pass 3 might be empty */
1120 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
1122 png_push_have_row(png_ptr
, NULL
);
1123 png_read_push_finish_row(png_ptr
);
1134 for (i
= 0; i
< 4 && png_ptr
->pass
== 3; i
++)
1136 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
1137 png_read_push_finish_row(png_ptr
);
1140 if (png_ptr
->pass
== 4) /* Skip top two generated rows */
1142 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
1144 png_push_have_row(png_ptr
, NULL
);
1145 png_read_push_finish_row(png_ptr
);
1156 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
1158 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
1159 png_read_push_finish_row(png_ptr
);
1162 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
1164 png_push_have_row(png_ptr
, NULL
);
1165 png_read_push_finish_row(png_ptr
);
1168 if (png_ptr
->pass
== 6) /* Pass 5 might be empty */
1170 png_push_have_row(png_ptr
, NULL
);
1171 png_read_push_finish_row(png_ptr
);
1181 for (i
= 0; i
< 2 && png_ptr
->pass
== 5; i
++)
1183 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
1184 png_read_push_finish_row(png_ptr
);
1187 if (png_ptr
->pass
== 6) /* Skip top generated row */
1189 png_push_have_row(png_ptr
, NULL
);
1190 png_read_push_finish_row(png_ptr
);
1199 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
1200 png_read_push_finish_row(png_ptr
);
1202 if (png_ptr
->pass
!= 6)
1205 png_push_have_row(png_ptr
, NULL
);
1206 png_read_push_finish_row(png_ptr
);
1213 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
1214 png_read_push_finish_row(png_ptr
);
1219 png_read_push_finish_row(png_structp png_ptr
)
1221 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1223 /* Start of interlace block */
1224 PNG_CONST
int FARDATA png_pass_start
[] = {0, 4, 0, 2, 0, 1, 0};
1226 /* Offset to next interlace block */
1227 PNG_CONST
int FARDATA png_pass_inc
[] = {8, 8, 4, 4, 2, 2, 1};
1229 /* Start of interlace block in the y direction */
1230 PNG_CONST
int FARDATA png_pass_ystart
[] = {0, 0, 4, 0, 2, 0, 1};
1232 /* Offset to next interlace block in the y direction */
1233 PNG_CONST
int FARDATA png_pass_yinc
[] = {8, 8, 8, 4, 4, 2, 2};
1235 /* Height of interlace block. This is not currently used - if you need
1236 * it, uncomment it here and in png.h
1237 PNG_CONST int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
1240 png_ptr
->row_number
++;
1241 if (png_ptr
->row_number
< png_ptr
->num_rows
)
1244 #ifdef PNG_READ_INTERLACING_SUPPORTED
1245 if (png_ptr
->interlaced
)
1247 png_ptr
->row_number
= 0;
1248 png_memset(png_ptr
->prev_row
, 0, png_ptr
->rowbytes
+ 1);
1253 if ((png_ptr
->pass
== 1 && png_ptr
->width
< 5) ||
1254 (png_ptr
->pass
== 3 && png_ptr
->width
< 3) ||
1255 (png_ptr
->pass
== 5 && png_ptr
->width
< 2))
1258 if (png_ptr
->pass
> 7)
1261 if (png_ptr
->pass
>= 7)
1264 png_ptr
->iwidth
= (png_ptr
->width
+
1265 png_pass_inc
[png_ptr
->pass
] - 1 -
1266 png_pass_start
[png_ptr
->pass
]) /
1267 png_pass_inc
[png_ptr
->pass
];
1269 if (png_ptr
->transformations
& PNG_INTERLACE
)
1272 png_ptr
->num_rows
= (png_ptr
->height
+
1273 png_pass_yinc
[png_ptr
->pass
] - 1 -
1274 png_pass_ystart
[png_ptr
->pass
]) /
1275 png_pass_yinc
[png_ptr
->pass
];
1277 } while (png_ptr
->iwidth
== 0 || png_ptr
->num_rows
== 0);
1279 #endif /* PNG_READ_INTERLACING_SUPPORTED */
1282 #ifdef PNG_READ_tEXt_SUPPORTED
1284 png_push_handle_tEXt(png_structp png_ptr
, png_infop info_ptr
, png_uint_32
1287 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
) || (png_ptr
->mode
& PNG_HAVE_IEND
))
1289 PNG_UNUSED(info_ptr
) /* To quiet some compiler warnings */
1290 png_error(png_ptr
, "Out of place tEXt");
1294 #ifdef PNG_MAX_MALLOC_64K
1295 png_ptr
->skip_length
= 0; /* This may not be necessary */
1297 if (length
> (png_uint_32
)65535L) /* Can't hold entire string in memory */
1299 png_warning(png_ptr
, "tEXt chunk too large to fit in memory");
1300 png_ptr
->skip_length
= length
- (png_uint_32
)65535L;
1301 length
= (png_uint_32
)65535L;
1305 png_ptr
->current_text
= (png_charp
)png_malloc(png_ptr
,
1306 (png_size_t
)(length
+ 1));
1307 png_ptr
->current_text
[length
] = '\0';
1308 png_ptr
->current_text_ptr
= png_ptr
->current_text
;
1309 png_ptr
->current_text_size
= (png_size_t
)length
;
1310 png_ptr
->current_text_left
= (png_size_t
)length
;
1311 png_ptr
->process_mode
= PNG_READ_tEXt_MODE
;
1315 png_push_read_tEXt(png_structp png_ptr
, png_infop info_ptr
)
1317 if (png_ptr
->buffer_size
&& png_ptr
->current_text_left
)
1319 png_size_t text_size
;
1321 if (png_ptr
->buffer_size
< png_ptr
->current_text_left
)
1322 text_size
= png_ptr
->buffer_size
;
1325 text_size
= png_ptr
->current_text_left
;
1327 png_crc_read(png_ptr
, (png_bytep
)png_ptr
->current_text_ptr
, text_size
);
1328 png_ptr
->current_text_left
-= text_size
;
1329 png_ptr
->current_text_ptr
+= text_size
;
1331 if (!(png_ptr
->current_text_left
))
1338 if (png_ptr
->buffer_size
< 4)
1340 png_push_save_buffer(png_ptr
);
1344 png_push_crc_finish(png_ptr
);
1346 #ifdef PNG_MAX_MALLOC_64K
1347 if (png_ptr
->skip_length
)
1351 key
= png_ptr
->current_text
;
1353 for (text
= key
; *text
; text
++)
1356 if (text
< key
+ png_ptr
->current_text_size
)
1359 text_ptr
= (png_textp
)png_malloc(png_ptr
, png_sizeof(png_text
));
1360 text_ptr
->compression
= PNG_TEXT_COMPRESSION_NONE
;
1361 text_ptr
->key
= key
;
1362 text_ptr
->itxt_length
= 0;
1363 text_ptr
->lang
= NULL
;
1364 text_ptr
->lang_key
= NULL
;
1365 text_ptr
->text
= text
;
1367 ret
= png_set_text_2(png_ptr
, info_ptr
, text_ptr
, 1);
1369 png_free(png_ptr
, key
);
1370 png_free(png_ptr
, text_ptr
);
1371 png_ptr
->current_text
= NULL
;
1374 png_warning(png_ptr
, "Insufficient memory to store text chunk");
1379 #ifdef PNG_READ_zTXt_SUPPORTED
1381 png_push_handle_zTXt(png_structp png_ptr
, png_infop info_ptr
, png_uint_32
1384 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
) || (png_ptr
->mode
& PNG_HAVE_IEND
))
1386 PNG_UNUSED(info_ptr
) /* To quiet some compiler warnings */
1387 png_error(png_ptr
, "Out of place zTXt");
1391 #ifdef PNG_MAX_MALLOC_64K
1392 /* We can't handle zTXt chunks > 64K, since we don't have enough space
1393 * to be able to store the uncompressed data. Actually, the threshold
1394 * is probably around 32K, but it isn't as definite as 64K is.
1396 if (length
> (png_uint_32
)65535L)
1398 png_warning(png_ptr
, "zTXt chunk too large to fit in memory");
1399 png_push_crc_skip(png_ptr
, length
);
1404 png_ptr
->current_text
= (png_charp
)png_malloc(png_ptr
,
1405 (png_size_t
)(length
+ 1));
1406 png_ptr
->current_text
[length
] = '\0';
1407 png_ptr
->current_text_ptr
= png_ptr
->current_text
;
1408 png_ptr
->current_text_size
= (png_size_t
)length
;
1409 png_ptr
->current_text_left
= (png_size_t
)length
;
1410 png_ptr
->process_mode
= PNG_READ_zTXt_MODE
;
1414 png_push_read_zTXt(png_structp png_ptr
, png_infop info_ptr
)
1416 if (png_ptr
->buffer_size
&& png_ptr
->current_text_left
)
1418 png_size_t text_size
;
1420 if (png_ptr
->buffer_size
< (png_uint_32
)png_ptr
->current_text_left
)
1421 text_size
= png_ptr
->buffer_size
;
1424 text_size
= png_ptr
->current_text_left
;
1426 png_crc_read(png_ptr
, (png_bytep
)png_ptr
->current_text_ptr
, text_size
);
1427 png_ptr
->current_text_left
-= text_size
;
1428 png_ptr
->current_text_ptr
+= text_size
;
1430 if (!(png_ptr
->current_text_left
))
1436 png_size_t text_size
, key_size
;
1438 if (png_ptr
->buffer_size
< 4)
1440 png_push_save_buffer(png_ptr
);
1444 png_push_crc_finish(png_ptr
);
1446 key
= png_ptr
->current_text
;
1448 for (text
= key
; *text
; text
++)
1451 /* zTXt can't have zero text */
1452 if (text
>= key
+ png_ptr
->current_text_size
)
1454 png_ptr
->current_text
= NULL
;
1455 png_free(png_ptr
, key
);
1461 if (*text
!= PNG_TEXT_COMPRESSION_zTXt
) /* Check compression byte */
1463 png_ptr
->current_text
= NULL
;
1464 png_free(png_ptr
, key
);
1470 png_ptr
->zstream
.next_in
= (png_bytep
)text
;
1471 png_ptr
->zstream
.avail_in
= (uInt
)(png_ptr
->current_text_size
-
1473 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
1474 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
1476 key_size
= text
- key
;
1481 while (png_ptr
->zstream
.avail_in
)
1483 ret
= inflate(&png_ptr
->zstream
, Z_PARTIAL_FLUSH
);
1484 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
)
1486 inflateReset(&png_ptr
->zstream
);
1487 png_ptr
->zstream
.avail_in
= 0;
1488 png_ptr
->current_text
= NULL
;
1489 png_free(png_ptr
, key
);
1490 png_free(png_ptr
, text
);
1494 if (!(png_ptr
->zstream
.avail_out
) || ret
== Z_STREAM_END
)
1498 text
= (png_charp
)png_malloc(png_ptr
,
1500 - png_ptr
->zstream
.avail_out
+ key_size
+ 1));
1502 png_memcpy(text
+ key_size
, png_ptr
->zbuf
,
1503 png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
);
1505 png_memcpy(text
, key
, key_size
);
1507 text_size
= key_size
+ png_ptr
->zbuf_size
-
1508 png_ptr
->zstream
.avail_out
;
1510 *(text
+ text_size
) = '\0';
1518 text
= (png_charp
)png_malloc(png_ptr
, text_size
+
1520 - png_ptr
->zstream
.avail_out
+ 1));
1522 png_memcpy(text
, tmp
, text_size
);
1523 png_free(png_ptr
, tmp
);
1525 png_memcpy(text
+ text_size
, png_ptr
->zbuf
,
1526 png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
);
1528 text_size
+= png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
;
1529 *(text
+ text_size
) = '\0';
1532 if (ret
!= Z_STREAM_END
)
1534 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
1535 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
1543 if (ret
== Z_STREAM_END
)
1547 inflateReset(&png_ptr
->zstream
);
1548 png_ptr
->zstream
.avail_in
= 0;
1550 if (ret
!= Z_STREAM_END
)
1552 png_ptr
->current_text
= NULL
;
1553 png_free(png_ptr
, key
);
1554 png_free(png_ptr
, text
);
1558 png_ptr
->current_text
= NULL
;
1559 png_free(png_ptr
, key
);
1563 text_ptr
= (png_textp
)png_malloc(png_ptr
,
1564 png_sizeof(png_text
));
1565 text_ptr
->compression
= PNG_TEXT_COMPRESSION_zTXt
;
1566 text_ptr
->key
= key
;
1567 text_ptr
->itxt_length
= 0;
1568 text_ptr
->lang
= NULL
;
1569 text_ptr
->lang_key
= NULL
;
1570 text_ptr
->text
= text
;
1572 ret
= png_set_text_2(png_ptr
, info_ptr
, text_ptr
, 1);
1574 png_free(png_ptr
, key
);
1575 png_free(png_ptr
, text_ptr
);
1578 png_warning(png_ptr
, "Insufficient memory to store text chunk");
1583 #ifdef PNG_READ_iTXt_SUPPORTED
1585 png_push_handle_iTXt(png_structp png_ptr
, png_infop info_ptr
, png_uint_32
1588 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
) || (png_ptr
->mode
& PNG_HAVE_IEND
))
1590 PNG_UNUSED(info_ptr
) /* To quiet some compiler warnings */
1591 png_error(png_ptr
, "Out of place iTXt");
1595 #ifdef PNG_MAX_MALLOC_64K
1596 png_ptr
->skip_length
= 0; /* This may not be necessary */
1598 if (length
> (png_uint_32
)65535L) /* Can't hold entire string in memory */
1600 png_warning(png_ptr
, "iTXt chunk too large to fit in memory");
1601 png_ptr
->skip_length
= length
- (png_uint_32
)65535L;
1602 length
= (png_uint_32
)65535L;
1606 png_ptr
->current_text
= (png_charp
)png_malloc(png_ptr
,
1607 (png_size_t
)(length
+ 1));
1608 png_ptr
->current_text
[length
] = '\0';
1609 png_ptr
->current_text_ptr
= png_ptr
->current_text
;
1610 png_ptr
->current_text_size
= (png_size_t
)length
;
1611 png_ptr
->current_text_left
= (png_size_t
)length
;
1612 png_ptr
->process_mode
= PNG_READ_iTXt_MODE
;
1616 png_push_read_iTXt(png_structp png_ptr
, png_infop info_ptr
)
1619 if (png_ptr
->buffer_size
&& png_ptr
->current_text_left
)
1621 png_size_t text_size
;
1623 if (png_ptr
->buffer_size
< png_ptr
->current_text_left
)
1624 text_size
= png_ptr
->buffer_size
;
1627 text_size
= png_ptr
->current_text_left
;
1629 png_crc_read(png_ptr
, (png_bytep
)png_ptr
->current_text_ptr
, text_size
);
1630 png_ptr
->current_text_left
-= text_size
;
1631 png_ptr
->current_text_ptr
+= text_size
;
1634 if (!(png_ptr
->current_text_left
))
1644 if (png_ptr
->buffer_size
< 4)
1646 png_push_save_buffer(png_ptr
);
1650 png_push_crc_finish(png_ptr
);
1652 #ifdef PNG_MAX_MALLOC_64K
1653 if (png_ptr
->skip_length
)
1657 key
= png_ptr
->current_text
;
1659 for (lang
= key
; *lang
; lang
++)
1662 if (lang
< key
+ png_ptr
->current_text_size
- 3)
1665 comp_flag
= *lang
++;
1666 lang
++; /* Skip comp_type, always zero */
1668 for (lang_key
= lang
; *lang_key
; lang_key
++)
1671 lang_key
++; /* Skip NUL separator */
1675 if (lang_key
< key
+ png_ptr
->current_text_size
- 1)
1677 for (; *text
; text
++)
1681 if (text
< key
+ png_ptr
->current_text_size
)
1684 text_ptr
= (png_textp
)png_malloc(png_ptr
,
1685 png_sizeof(png_text
));
1687 text_ptr
->compression
= comp_flag
+ 2;
1688 text_ptr
->key
= key
;
1689 text_ptr
->lang
= lang
;
1690 text_ptr
->lang_key
= lang_key
;
1691 text_ptr
->text
= text
;
1692 text_ptr
->text_length
= 0;
1693 text_ptr
->itxt_length
= png_strlen(text
);
1695 ret
= png_set_text_2(png_ptr
, info_ptr
, text_ptr
, 1);
1697 png_ptr
->current_text
= NULL
;
1699 png_free(png_ptr
, text_ptr
);
1701 png_warning(png_ptr
, "Insufficient memory to store iTXt chunk");
1706 /* This function is called when we haven't found a handler for this
1707 * chunk. If there isn't a problem with the chunk itself (ie a bad chunk
1708 * name or a critical chunk), the chunk is (currently) silently ignored.
1711 png_push_handle_unknown(png_structp png_ptr
, png_infop info_ptr
, png_uint_32
1714 png_uint_32 skip
= 0;
1716 if (!(png_ptr
->chunk_name
[0] & 0x20))
1718 #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
1719 if (png_handle_as_unknown(png_ptr
, png_ptr
->chunk_name
) !=
1720 PNG_HANDLE_CHUNK_ALWAYS
1721 #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
1722 && png_ptr
->read_user_chunk_fn
== NULL
1726 png_chunk_error(png_ptr
, "unknown critical chunk");
1728 PNG_UNUSED(info_ptr
) /* To quiet some compiler warnings */
1731 #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
1732 if (png_ptr
->flags
& PNG_FLAG_KEEP_UNKNOWN_CHUNKS
)
1734 #ifdef PNG_MAX_MALLOC_64K
1735 if (length
> (png_uint_32
)65535L)
1737 png_warning(png_ptr
, "unknown chunk too large to fit in memory");
1738 skip
= length
- (png_uint_32
)65535L;
1739 length
= (png_uint_32
)65535L;
1742 png_memcpy((png_charp
)png_ptr
->unknown_chunk
.name
,
1743 (png_charp
)png_ptr
->chunk_name
,
1744 png_sizeof(png_ptr
->unknown_chunk
.name
));
1745 png_ptr
->unknown_chunk
.name
[png_sizeof(png_ptr
->unknown_chunk
.name
) - 1]
1748 png_ptr
->unknown_chunk
.size
= (png_size_t
)length
;
1751 png_ptr
->unknown_chunk
.data
= NULL
;
1755 png_ptr
->unknown_chunk
.data
= (png_bytep
)png_malloc(png_ptr
,
1756 (png_size_t
)length
);
1757 png_crc_read(png_ptr
, (png_bytep
)png_ptr
->unknown_chunk
.data
, length
);
1760 #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
1761 if (png_ptr
->read_user_chunk_fn
!= NULL
)
1763 /* Callback to user unknown chunk handler */
1765 ret
= (*(png_ptr
->read_user_chunk_fn
))
1766 (png_ptr
, &png_ptr
->unknown_chunk
);
1769 png_chunk_error(png_ptr
, "error in user chunk");
1773 if (!(png_ptr
->chunk_name
[0] & 0x20))
1774 if (png_handle_as_unknown(png_ptr
, png_ptr
->chunk_name
) !=
1775 PNG_HANDLE_CHUNK_ALWAYS
)
1776 png_chunk_error(png_ptr
, "unknown critical chunk");
1777 png_set_unknown_chunks(png_ptr
, info_ptr
,
1778 &png_ptr
->unknown_chunk
, 1);
1784 png_set_unknown_chunks(png_ptr
, info_ptr
, &png_ptr
->unknown_chunk
, 1);
1785 png_free(png_ptr
, png_ptr
->unknown_chunk
.data
);
1786 png_ptr
->unknown_chunk
.data
= NULL
;
1792 png_push_crc_skip(png_ptr
, skip
);
1796 png_push_have_info(png_structp png_ptr
, png_infop info_ptr
)
1798 if (png_ptr
->info_fn
!= NULL
)
1799 (*(png_ptr
->info_fn
))(png_ptr
, info_ptr
);
1803 png_push_have_end(png_structp png_ptr
, png_infop info_ptr
)
1805 if (png_ptr
->end_fn
!= NULL
)
1806 (*(png_ptr
->end_fn
))(png_ptr
, info_ptr
);
1810 png_push_have_row(png_structp png_ptr
, png_bytep row
)
1812 if (png_ptr
->row_fn
!= NULL
)
1813 (*(png_ptr
->row_fn
))(png_ptr
, row
, png_ptr
->row_number
,
1814 (int)png_ptr
->pass
);
1818 png_progressive_combine_row (png_structp png_ptr
, png_bytep old_row
,
1819 png_const_bytep new_row
)
1821 PNG_CONST
int FARDATA png_pass_dsp_mask
[7] =
1822 {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
1824 if (png_ptr
== NULL
)
1827 if (new_row
!= NULL
) /* new_row must == png_ptr->row_buf here. */
1828 png_combine_row(png_ptr
, old_row
, png_pass_dsp_mask
[png_ptr
->pass
]);
1832 png_set_progressive_read_fn(png_structp png_ptr
, png_voidp progressive_ptr
,
1833 png_progressive_info_ptr info_fn
, png_progressive_row_ptr row_fn
,
1834 png_progressive_end_ptr end_fn
)
1836 if (png_ptr
== NULL
)
1839 png_ptr
->info_fn
= info_fn
;
1840 png_ptr
->row_fn
= row_fn
;
1841 png_ptr
->end_fn
= end_fn
;
1843 png_set_read_fn(png_ptr
, progressive_ptr
, png_push_fill_buffer
);
1847 png_get_progressive_ptr(png_const_structp png_ptr
)
1849 if (png_ptr
== NULL
)
1852 return png_ptr
->io_ptr
;
1854 #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */