2 /* pngpread.c - read a png file in push mode
4 * Last changed in libpng 1.2.44 [June 26, 2010]
5 * Copyright (c) 1998-2010 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
15 #define PNG_NO_PEDANTIC_WARNINGS
17 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
19 /* Push model modes */
20 #define PNG_READ_SIG_MODE 0
21 #define PNG_READ_CHUNK_MODE 1
22 #define PNG_READ_IDAT_MODE 2
23 #define PNG_SKIP_MODE 3
24 #define PNG_READ_tEXt_MODE 4
25 #define PNG_READ_zTXt_MODE 5
26 #define PNG_READ_DONE_MODE 6
27 #define PNG_READ_iTXt_MODE 7
28 #define PNG_ERROR_MODE 8
31 png_process_data(png_structp png_ptr
, png_infop info_ptr
,
32 png_bytep buffer
, png_size_t buffer_size
)
34 if (png_ptr
== NULL
|| info_ptr
== NULL
)
37 png_push_restore_buffer(png_ptr
, buffer
, buffer_size
);
39 while (png_ptr
->buffer_size
)
41 png_process_some_data(png_ptr
, info_ptr
);
45 /* What we do with the incoming data depends on what we were previously
46 * doing before we ran out of data...
49 png_process_some_data(png_structp png_ptr
, png_infop info_ptr
)
54 switch (png_ptr
->process_mode
)
56 case PNG_READ_SIG_MODE
:
58 png_push_read_sig(png_ptr
, info_ptr
);
62 case PNG_READ_CHUNK_MODE
:
64 png_push_read_chunk(png_ptr
, info_ptr
);
68 case PNG_READ_IDAT_MODE
:
70 png_push_read_IDAT(png_ptr
);
74 #ifdef PNG_READ_tEXt_SUPPORTED
75 case PNG_READ_tEXt_MODE
:
77 png_push_read_tEXt(png_ptr
, info_ptr
);
82 #ifdef PNG_READ_zTXt_SUPPORTED
83 case PNG_READ_zTXt_MODE
:
85 png_push_read_zTXt(png_ptr
, info_ptr
);
90 #ifdef PNG_READ_iTXt_SUPPORTED
91 case PNG_READ_iTXt_MODE
:
93 png_push_read_iTXt(png_ptr
, info_ptr
);
100 png_push_crc_finish(png_ptr
);
106 png_ptr
->buffer_size
= 0;
112 /* Read any remaining signature bytes from the stream and compare them with
113 * the correct PNG signature. It is possible that this routine is called
114 * with bytes already read from the signature, either because they have been
115 * checked by the calling application, or because of multiple calls to this
119 png_push_read_sig(png_structp png_ptr
, png_infop info_ptr
)
121 png_size_t num_checked
= png_ptr
->sig_bytes
,
122 num_to_check
= 8 - num_checked
;
124 if (png_ptr
->buffer_size
< num_to_check
)
126 num_to_check
= png_ptr
->buffer_size
;
129 png_push_fill_buffer(png_ptr
, &(info_ptr
->signature
[num_checked
]),
131 png_ptr
->sig_bytes
= (png_byte
)(png_ptr
->sig_bytes
+ num_to_check
);
133 if (png_sig_cmp(info_ptr
->signature
, num_checked
, num_to_check
))
135 if (num_checked
< 4 &&
136 png_sig_cmp(info_ptr
->signature
, num_checked
, num_to_check
- 4))
137 png_error(png_ptr
, "Not a PNG file");
139 png_error(png_ptr
, "PNG file corrupted by ASCII conversion");
143 if (png_ptr
->sig_bytes
>= 8)
145 png_ptr
->process_mode
= PNG_READ_CHUNK_MODE
;
151 png_push_read_chunk(png_structp png_ptr
, png_infop info_ptr
)
153 #ifdef PNG_USE_LOCAL_ARRAYS
158 #ifdef PNG_READ_bKGD_SUPPORTED
161 #ifdef PNG_READ_cHRM_SUPPORTED
164 #ifdef PNG_READ_gAMA_SUPPORTED
167 #ifdef PNG_READ_hIST_SUPPORTED
170 #ifdef PNG_READ_iCCP_SUPPORTED
173 #ifdef PNG_READ_iTXt_SUPPORTED
176 #ifdef PNG_READ_oFFs_SUPPORTED
179 #ifdef PNG_READ_pCAL_SUPPORTED
182 #ifdef PNG_READ_pHYs_SUPPORTED
185 #ifdef PNG_READ_sBIT_SUPPORTED
188 #ifdef PNG_READ_sCAL_SUPPORTED
191 #ifdef PNG_READ_sRGB_SUPPORTED
194 #ifdef PNG_READ_sPLT_SUPPORTED
197 #ifdef PNG_READ_tEXt_SUPPORTED
200 #ifdef PNG_READ_tIME_SUPPORTED
203 #ifdef PNG_READ_tRNS_SUPPORTED
206 #ifdef PNG_READ_zTXt_SUPPORTED
209 #endif /* PNG_USE_LOCAL_ARRAYS */
211 /* First we make sure we have enough data for the 4 byte chunk name
212 * and the 4 byte chunk length before proceeding with decoding the
213 * chunk data. To fully decode each of these chunks, we also make
214 * sure we have enough data in the buffer for the 4 byte CRC at the
215 * end of every chunk (except IDAT, which is handled separately).
217 if (!(png_ptr
->mode
& PNG_HAVE_CHUNK_HEADER
))
219 png_byte chunk_length
[4];
221 if (png_ptr
->buffer_size
< 8)
223 png_push_save_buffer(png_ptr
);
227 png_push_fill_buffer(png_ptr
, chunk_length
, 4);
228 png_ptr
->push_length
= png_get_uint_31(png_ptr
, chunk_length
);
229 png_reset_crc(png_ptr
);
230 png_crc_read(png_ptr
, png_ptr
->chunk_name
, 4);
231 png_check_chunk_name(png_ptr
, png_ptr
->chunk_name
);
232 png_ptr
->mode
|= PNG_HAVE_CHUNK_HEADER
;
235 if (!png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
236 if (png_ptr
->mode
& PNG_AFTER_IDAT
)
237 png_ptr
->mode
|= PNG_HAVE_CHUNK_AFTER_IDAT
;
239 if (!png_memcmp(png_ptr
->chunk_name
, png_IHDR
, 4))
241 if (png_ptr
->push_length
!= 13)
242 png_error(png_ptr
, "Invalid IHDR length");
244 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
246 png_push_save_buffer(png_ptr
);
250 png_handle_IHDR(png_ptr
, info_ptr
, png_ptr
->push_length
);
253 else if (!png_memcmp(png_ptr
->chunk_name
, png_IEND
, 4))
255 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
257 png_push_save_buffer(png_ptr
);
261 png_handle_IEND(png_ptr
, info_ptr
, png_ptr
->push_length
);
263 png_ptr
->process_mode
= PNG_READ_DONE_MODE
;
264 png_push_have_end(png_ptr
, info_ptr
);
267 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
268 else if (png_handle_as_unknown(png_ptr
, png_ptr
->chunk_name
))
270 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
272 png_push_save_buffer(png_ptr
);
276 if (!png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
277 png_ptr
->mode
|= PNG_HAVE_IDAT
;
279 png_handle_unknown(png_ptr
, info_ptr
, png_ptr
->push_length
);
281 if (!png_memcmp(png_ptr
->chunk_name
, png_PLTE
, 4))
282 png_ptr
->mode
|= PNG_HAVE_PLTE
;
284 else if (!png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
286 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
287 png_error(png_ptr
, "Missing IHDR before IDAT");
289 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
&&
290 !(png_ptr
->mode
& PNG_HAVE_PLTE
))
291 png_error(png_ptr
, "Missing PLTE before IDAT");
296 else if (!png_memcmp(png_ptr
->chunk_name
, png_PLTE
, 4))
298 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
300 png_push_save_buffer(png_ptr
);
303 png_handle_PLTE(png_ptr
, info_ptr
, png_ptr
->push_length
);
306 else if (!png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
308 /* If we reach an IDAT chunk, this means we have read all of the
309 * header chunks, and we can start reading the image (or if this
310 * is called after the image has been read - we have an error).
313 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
314 png_error(png_ptr
, "Missing IHDR before IDAT");
316 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
&&
317 !(png_ptr
->mode
& PNG_HAVE_PLTE
))
318 png_error(png_ptr
, "Missing PLTE before IDAT");
320 if (png_ptr
->mode
& PNG_HAVE_IDAT
)
322 if (!(png_ptr
->mode
& PNG_HAVE_CHUNK_AFTER_IDAT
))
323 if (png_ptr
->push_length
== 0)
326 if (png_ptr
->mode
& PNG_AFTER_IDAT
)
327 png_error(png_ptr
, "Too many IDAT's found");
330 png_ptr
->idat_size
= png_ptr
->push_length
;
331 png_ptr
->mode
|= PNG_HAVE_IDAT
;
332 png_ptr
->process_mode
= PNG_READ_IDAT_MODE
;
333 png_push_have_info(png_ptr
, info_ptr
);
334 png_ptr
->zstream
.avail_out
=
335 (uInt
) PNG_ROWBYTES(png_ptr
->pixel_depth
,
336 png_ptr
->iwidth
) + 1;
337 png_ptr
->zstream
.next_out
= png_ptr
->row_buf
;
341 #ifdef PNG_READ_gAMA_SUPPORTED
342 else if (!png_memcmp(png_ptr
->chunk_name
, png_gAMA
, 4))
344 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
346 png_push_save_buffer(png_ptr
);
350 png_handle_gAMA(png_ptr
, info_ptr
, png_ptr
->push_length
);
354 #ifdef PNG_READ_sBIT_SUPPORTED
355 else if (!png_memcmp(png_ptr
->chunk_name
, png_sBIT
, 4))
357 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
359 png_push_save_buffer(png_ptr
);
363 png_handle_sBIT(png_ptr
, info_ptr
, png_ptr
->push_length
);
367 #ifdef PNG_READ_cHRM_SUPPORTED
368 else if (!png_memcmp(png_ptr
->chunk_name
, png_cHRM
, 4))
370 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
372 png_push_save_buffer(png_ptr
);
376 png_handle_cHRM(png_ptr
, info_ptr
, png_ptr
->push_length
);
380 #ifdef PNG_READ_sRGB_SUPPORTED
381 else if (!png_memcmp(png_ptr
->chunk_name
, png_sRGB
, 4))
383 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
385 png_push_save_buffer(png_ptr
);
389 png_handle_sRGB(png_ptr
, info_ptr
, png_ptr
->push_length
);
393 #ifdef PNG_READ_iCCP_SUPPORTED
394 else if (!png_memcmp(png_ptr
->chunk_name
, png_iCCP
, 4))
396 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
398 png_push_save_buffer(png_ptr
);
402 png_handle_iCCP(png_ptr
, info_ptr
, png_ptr
->push_length
);
406 #ifdef PNG_READ_sPLT_SUPPORTED
407 else if (!png_memcmp(png_ptr
->chunk_name
, png_sPLT
, 4))
409 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
411 png_push_save_buffer(png_ptr
);
415 png_handle_sPLT(png_ptr
, info_ptr
, png_ptr
->push_length
);
419 #ifdef PNG_READ_tRNS_SUPPORTED
420 else if (!png_memcmp(png_ptr
->chunk_name
, png_tRNS
, 4))
422 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
424 png_push_save_buffer(png_ptr
);
428 png_handle_tRNS(png_ptr
, info_ptr
, png_ptr
->push_length
);
432 #ifdef PNG_READ_bKGD_SUPPORTED
433 else if (!png_memcmp(png_ptr
->chunk_name
, png_bKGD
, 4))
435 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
437 png_push_save_buffer(png_ptr
);
441 png_handle_bKGD(png_ptr
, info_ptr
, png_ptr
->push_length
);
445 #ifdef PNG_READ_hIST_SUPPORTED
446 else if (!png_memcmp(png_ptr
->chunk_name
, png_hIST
, 4))
448 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
450 png_push_save_buffer(png_ptr
);
454 png_handle_hIST(png_ptr
, info_ptr
, png_ptr
->push_length
);
458 #ifdef PNG_READ_pHYs_SUPPORTED
459 else if (!png_memcmp(png_ptr
->chunk_name
, png_pHYs
, 4))
461 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
463 png_push_save_buffer(png_ptr
);
467 png_handle_pHYs(png_ptr
, info_ptr
, png_ptr
->push_length
);
471 #ifdef PNG_READ_oFFs_SUPPORTED
472 else if (!png_memcmp(png_ptr
->chunk_name
, png_oFFs
, 4))
474 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
476 png_push_save_buffer(png_ptr
);
480 png_handle_oFFs(png_ptr
, info_ptr
, png_ptr
->push_length
);
484 #ifdef PNG_READ_pCAL_SUPPORTED
485 else if (!png_memcmp(png_ptr
->chunk_name
, png_pCAL
, 4))
487 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
489 png_push_save_buffer(png_ptr
);
493 png_handle_pCAL(png_ptr
, info_ptr
, png_ptr
->push_length
);
497 #ifdef PNG_READ_sCAL_SUPPORTED
498 else if (!png_memcmp(png_ptr
->chunk_name
, png_sCAL
, 4))
500 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
502 png_push_save_buffer(png_ptr
);
506 png_handle_sCAL(png_ptr
, info_ptr
, png_ptr
->push_length
);
510 #ifdef PNG_READ_tIME_SUPPORTED
511 else if (!png_memcmp(png_ptr
->chunk_name
, png_tIME
, 4))
513 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
515 png_push_save_buffer(png_ptr
);
519 png_handle_tIME(png_ptr
, info_ptr
, png_ptr
->push_length
);
523 #ifdef PNG_READ_tEXt_SUPPORTED
524 else if (!png_memcmp(png_ptr
->chunk_name
, png_tEXt
, 4))
526 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
528 png_push_save_buffer(png_ptr
);
532 png_push_handle_tEXt(png_ptr
, info_ptr
, png_ptr
->push_length
);
536 #ifdef PNG_READ_zTXt_SUPPORTED
537 else if (!png_memcmp(png_ptr
->chunk_name
, png_zTXt
, 4))
539 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
541 png_push_save_buffer(png_ptr
);
545 png_push_handle_zTXt(png_ptr
, info_ptr
, png_ptr
->push_length
);
549 #ifdef PNG_READ_iTXt_SUPPORTED
550 else if (!png_memcmp(png_ptr
->chunk_name
, png_iTXt
, 4))
552 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
554 png_push_save_buffer(png_ptr
);
558 png_push_handle_iTXt(png_ptr
, info_ptr
, png_ptr
->push_length
);
564 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
566 png_push_save_buffer(png_ptr
);
569 png_push_handle_unknown(png_ptr
, info_ptr
, png_ptr
->push_length
);
572 png_ptr
->mode
&= ~PNG_HAVE_CHUNK_HEADER
;
576 png_push_crc_skip(png_structp png_ptr
, png_uint_32 skip
)
578 png_ptr
->process_mode
= PNG_SKIP_MODE
;
579 png_ptr
->skip_length
= skip
;
583 png_push_crc_finish(png_structp png_ptr
)
585 if (png_ptr
->skip_length
&& png_ptr
->save_buffer_size
)
587 png_size_t save_size
;
589 if (png_ptr
->skip_length
< (png_uint_32
)png_ptr
->save_buffer_size
)
590 save_size
= (png_size_t
)png_ptr
->skip_length
;
592 save_size
= png_ptr
->save_buffer_size
;
594 png_calculate_crc(png_ptr
, png_ptr
->save_buffer_ptr
, save_size
);
596 png_ptr
->skip_length
-= save_size
;
597 png_ptr
->buffer_size
-= save_size
;
598 png_ptr
->save_buffer_size
-= save_size
;
599 png_ptr
->save_buffer_ptr
+= save_size
;
601 if (png_ptr
->skip_length
&& png_ptr
->current_buffer_size
)
603 png_size_t save_size
;
605 if (png_ptr
->skip_length
< (png_uint_32
)png_ptr
->current_buffer_size
)
606 save_size
= (png_size_t
)png_ptr
->skip_length
;
608 save_size
= png_ptr
->current_buffer_size
;
610 png_calculate_crc(png_ptr
, png_ptr
->current_buffer_ptr
, save_size
);
612 png_ptr
->skip_length
-= save_size
;
613 png_ptr
->buffer_size
-= save_size
;
614 png_ptr
->current_buffer_size
-= save_size
;
615 png_ptr
->current_buffer_ptr
+= save_size
;
617 if (!png_ptr
->skip_length
)
619 if (png_ptr
->buffer_size
< 4)
621 png_push_save_buffer(png_ptr
);
625 png_crc_finish(png_ptr
, 0);
626 png_ptr
->process_mode
= PNG_READ_CHUNK_MODE
;
631 png_push_fill_buffer(png_structp png_ptr
, png_bytep buffer
, png_size_t length
)
639 if (png_ptr
->save_buffer_size
)
641 png_size_t save_size
;
643 if (length
< png_ptr
->save_buffer_size
)
646 save_size
= png_ptr
->save_buffer_size
;
648 png_memcpy(ptr
, png_ptr
->save_buffer_ptr
, save_size
);
651 png_ptr
->buffer_size
-= save_size
;
652 png_ptr
->save_buffer_size
-= save_size
;
653 png_ptr
->save_buffer_ptr
+= save_size
;
655 if (length
&& png_ptr
->current_buffer_size
)
657 png_size_t save_size
;
659 if (length
< png_ptr
->current_buffer_size
)
663 save_size
= png_ptr
->current_buffer_size
;
665 png_memcpy(ptr
, png_ptr
->current_buffer_ptr
, save_size
);
666 png_ptr
->buffer_size
-= save_size
;
667 png_ptr
->current_buffer_size
-= save_size
;
668 png_ptr
->current_buffer_ptr
+= save_size
;
673 png_push_save_buffer(png_structp png_ptr
)
675 if (png_ptr
->save_buffer_size
)
677 if (png_ptr
->save_buffer_ptr
!= png_ptr
->save_buffer
)
683 istop
= png_ptr
->save_buffer_size
;
684 for (i
= 0, sp
= png_ptr
->save_buffer_ptr
, dp
= png_ptr
->save_buffer
;
685 i
< istop
; i
++, sp
++, dp
++)
691 if (png_ptr
->save_buffer_size
+ png_ptr
->current_buffer_size
>
692 png_ptr
->save_buffer_max
)
695 png_bytep old_buffer
;
697 if (png_ptr
->save_buffer_size
> PNG_SIZE_MAX
-
698 (png_ptr
->current_buffer_size
+ 256))
700 png_error(png_ptr
, "Potential overflow of save_buffer");
703 new_max
= png_ptr
->save_buffer_size
+ png_ptr
->current_buffer_size
+ 256;
704 old_buffer
= png_ptr
->save_buffer
;
705 png_ptr
->save_buffer
= (png_bytep
)png_malloc_warn(png_ptr
,
706 (png_uint_32
)new_max
);
707 if (png_ptr
->save_buffer
== NULL
)
709 png_free(png_ptr
, old_buffer
);
710 png_error(png_ptr
, "Insufficient memory for save_buffer");
712 png_memcpy(png_ptr
->save_buffer
, old_buffer
, png_ptr
->save_buffer_size
);
713 png_free(png_ptr
, old_buffer
);
714 png_ptr
->save_buffer_max
= new_max
;
716 if (png_ptr
->current_buffer_size
)
718 png_memcpy(png_ptr
->save_buffer
+ png_ptr
->save_buffer_size
,
719 png_ptr
->current_buffer_ptr
, png_ptr
->current_buffer_size
);
720 png_ptr
->save_buffer_size
+= png_ptr
->current_buffer_size
;
721 png_ptr
->current_buffer_size
= 0;
723 png_ptr
->save_buffer_ptr
= png_ptr
->save_buffer
;
724 png_ptr
->buffer_size
= 0;
728 png_push_restore_buffer(png_structp png_ptr
, png_bytep buffer
,
729 png_size_t buffer_length
)
731 png_ptr
->current_buffer
= buffer
;
732 png_ptr
->current_buffer_size
= buffer_length
;
733 png_ptr
->buffer_size
= buffer_length
+ png_ptr
->save_buffer_size
;
734 png_ptr
->current_buffer_ptr
= png_ptr
->current_buffer
;
738 png_push_read_IDAT(png_structp png_ptr
)
740 #ifdef PNG_USE_LOCAL_ARRAYS
743 if (!(png_ptr
->mode
& PNG_HAVE_CHUNK_HEADER
))
745 png_byte chunk_length
[4];
747 if (png_ptr
->buffer_size
< 8)
749 png_push_save_buffer(png_ptr
);
753 png_push_fill_buffer(png_ptr
, chunk_length
, 4);
754 png_ptr
->push_length
= png_get_uint_31(png_ptr
, chunk_length
);
755 png_reset_crc(png_ptr
);
756 png_crc_read(png_ptr
, png_ptr
->chunk_name
, 4);
757 png_ptr
->mode
|= PNG_HAVE_CHUNK_HEADER
;
759 if (png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
761 png_ptr
->process_mode
= PNG_READ_CHUNK_MODE
;
762 if (!(png_ptr
->flags
& PNG_FLAG_ZLIB_FINISHED
))
763 png_error(png_ptr
, "Not enough compressed data");
767 png_ptr
->idat_size
= png_ptr
->push_length
;
769 if (png_ptr
->idat_size
&& png_ptr
->save_buffer_size
)
771 png_size_t save_size
;
773 if (png_ptr
->idat_size
< (png_uint_32
)png_ptr
->save_buffer_size
)
775 save_size
= (png_size_t
)png_ptr
->idat_size
;
777 /* Check for overflow */
778 if ((png_uint_32
)save_size
!= png_ptr
->idat_size
)
779 png_error(png_ptr
, "save_size overflowed in pngpread");
782 save_size
= png_ptr
->save_buffer_size
;
784 png_calculate_crc(png_ptr
, png_ptr
->save_buffer_ptr
, save_size
);
786 png_process_IDAT_data(png_ptr
, png_ptr
->save_buffer_ptr
, save_size
);
788 png_ptr
->idat_size
-= save_size
;
789 png_ptr
->buffer_size
-= save_size
;
790 png_ptr
->save_buffer_size
-= save_size
;
791 png_ptr
->save_buffer_ptr
+= save_size
;
793 if (png_ptr
->idat_size
&& png_ptr
->current_buffer_size
)
795 png_size_t save_size
;
797 if (png_ptr
->idat_size
< (png_uint_32
)png_ptr
->current_buffer_size
)
799 save_size
= (png_size_t
)png_ptr
->idat_size
;
801 /* Check for overflow */
802 if ((png_uint_32
)save_size
!= png_ptr
->idat_size
)
803 png_error(png_ptr
, "save_size overflowed in pngpread");
806 save_size
= png_ptr
->current_buffer_size
;
808 png_calculate_crc(png_ptr
, png_ptr
->current_buffer_ptr
, save_size
);
810 png_process_IDAT_data(png_ptr
, png_ptr
->current_buffer_ptr
, save_size
);
812 png_ptr
->idat_size
-= save_size
;
813 png_ptr
->buffer_size
-= save_size
;
814 png_ptr
->current_buffer_size
-= save_size
;
815 png_ptr
->current_buffer_ptr
+= save_size
;
817 if (!png_ptr
->idat_size
)
819 if (png_ptr
->buffer_size
< 4)
821 png_push_save_buffer(png_ptr
);
825 png_crc_finish(png_ptr
, 0);
826 png_ptr
->mode
&= ~PNG_HAVE_CHUNK_HEADER
;
827 png_ptr
->mode
|= PNG_AFTER_IDAT
;
832 png_process_IDAT_data(png_structp png_ptr
, png_bytep buffer
,
833 png_size_t buffer_length
)
835 /* The caller checks for a non-zero buffer length. */
836 if (!(buffer_length
> 0) || buffer
== NULL
)
837 png_error(png_ptr
, "No IDAT data (internal error)");
839 /* This routine must process all the data it has been given
840 * before returning, calling the row callback as required to
841 * handle the uncompressed results.
843 png_ptr
->zstream
.next_in
= buffer
;
844 png_ptr
->zstream
.avail_in
= (uInt
)buffer_length
;
846 /* Keep going until the decompressed data is all processed
847 * or the stream marked as finished.
849 while (png_ptr
->zstream
.avail_in
> 0 &&
850 !(png_ptr
->flags
& PNG_FLAG_ZLIB_FINISHED
))
854 /* We have data for zlib, but we must check that zlib
855 * has somewhere to put the results. It doesn't matter
856 * if we don't expect any results -- it may be the input
857 * data is just the LZ end code.
859 if (!(png_ptr
->zstream
.avail_out
> 0))
861 png_ptr
->zstream
.avail_out
=
862 (uInt
) PNG_ROWBYTES(png_ptr
->pixel_depth
,
863 png_ptr
->iwidth
) + 1;
864 png_ptr
->zstream
.next_out
= png_ptr
->row_buf
;
867 /* Using Z_SYNC_FLUSH here means that an unterminated
868 * LZ stream can still be handled (a stream with a missing
869 * end code), otherwise (Z_NO_FLUSH) a future zlib
870 * implementation might defer output and, therefore,
871 * change the current behavior. (See comments in inflate.c
872 * for why this doesn't happen at present with zlib 1.2.5.)
874 ret
= inflate(&png_ptr
->zstream
, Z_SYNC_FLUSH
);
876 /* Check for any failure before proceeding. */
877 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
)
879 /* Terminate the decompression. */
880 png_ptr
->flags
|= PNG_FLAG_ZLIB_FINISHED
;
882 /* This may be a truncated stream (missing or
883 * damaged end code). Treat that as a warning.
885 if (png_ptr
->row_number
>= png_ptr
->num_rows
||
887 png_warning(png_ptr
, "Truncated compressed data in IDAT");
889 png_error(png_ptr
, "Decompression error in IDAT");
891 /* Skip the check on unprocessed input */
895 /* Did inflate output any data? */
896 if (png_ptr
->zstream
.next_out
!= png_ptr
->row_buf
)
898 /* Is this unexpected data after the last row?
899 * If it is, artificially terminate the LZ output
902 if (png_ptr
->row_number
>= png_ptr
->num_rows
||
906 png_warning(png_ptr
, "Extra compressed data in IDAT");
907 png_ptr
->flags
|= PNG_FLAG_ZLIB_FINISHED
;
908 /* Do no more processing; skip the unprocessed
914 /* Do we have a complete row? */
915 if (png_ptr
->zstream
.avail_out
== 0)
916 png_push_process_row(png_ptr
);
919 /* And check for the end of the stream. */
920 if (ret
== Z_STREAM_END
)
921 png_ptr
->flags
|= PNG_FLAG_ZLIB_FINISHED
;
924 /* All the data should have been processed, if anything
925 * is left at this point we have bytes of IDAT data
926 * after the zlib end code.
928 if (png_ptr
->zstream
.avail_in
> 0)
929 png_warning(png_ptr
, "Extra compression data");
933 png_push_process_row(png_structp png_ptr
)
935 png_ptr
->row_info
.color_type
= png_ptr
->color_type
;
936 png_ptr
->row_info
.width
= png_ptr
->iwidth
;
937 png_ptr
->row_info
.channels
= png_ptr
->channels
;
938 png_ptr
->row_info
.bit_depth
= png_ptr
->bit_depth
;
939 png_ptr
->row_info
.pixel_depth
= png_ptr
->pixel_depth
;
941 png_ptr
->row_info
.rowbytes
= PNG_ROWBYTES(png_ptr
->row_info
.pixel_depth
,
942 png_ptr
->row_info
.width
);
944 png_read_filter_row(png_ptr
, &(png_ptr
->row_info
),
945 png_ptr
->row_buf
+ 1, png_ptr
->prev_row
+ 1,
946 (int)(png_ptr
->row_buf
[0]));
948 png_memcpy_check(png_ptr
, png_ptr
->prev_row
, png_ptr
->row_buf
,
949 png_ptr
->rowbytes
+ 1);
951 if (png_ptr
->transformations
|| (png_ptr
->flags
&PNG_FLAG_STRIP_ALPHA
))
952 png_do_read_transformations(png_ptr
);
954 #ifdef PNG_READ_INTERLACING_SUPPORTED
955 /* Blow up interlaced rows to full size */
956 if (png_ptr
->interlaced
&& (png_ptr
->transformations
& PNG_INTERLACE
))
958 if (png_ptr
->pass
< 6)
959 /* old interface (pre-1.0.9):
960 png_do_read_interlace(&(png_ptr->row_info),
961 png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
963 png_do_read_interlace(png_ptr
);
965 switch (png_ptr
->pass
)
970 for (i
= 0; i
< 8 && png_ptr
->pass
== 0; i
++)
972 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
973 png_read_push_finish_row(png_ptr
); /* Updates png_ptr->pass */
976 if (png_ptr
->pass
== 2) /* Pass 1 might be empty */
978 for (i
= 0; i
< 4 && png_ptr
->pass
== 2; i
++)
980 png_push_have_row(png_ptr
, png_bytep_NULL
);
981 png_read_push_finish_row(png_ptr
);
985 if (png_ptr
->pass
== 4 && png_ptr
->height
<= 4)
987 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
989 png_push_have_row(png_ptr
, png_bytep_NULL
);
990 png_read_push_finish_row(png_ptr
);
994 if (png_ptr
->pass
== 6 && png_ptr
->height
<= 4)
996 png_push_have_row(png_ptr
, png_bytep_NULL
);
997 png_read_push_finish_row(png_ptr
);
1006 for (i
= 0; i
< 8 && png_ptr
->pass
== 1; i
++)
1008 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
1009 png_read_push_finish_row(png_ptr
);
1012 if (png_ptr
->pass
== 2) /* Skip top 4 generated rows */
1014 for (i
= 0; i
< 4 && png_ptr
->pass
== 2; i
++)
1016 png_push_have_row(png_ptr
, png_bytep_NULL
);
1017 png_read_push_finish_row(png_ptr
);
1028 for (i
= 0; i
< 4 && png_ptr
->pass
== 2; i
++)
1030 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
1031 png_read_push_finish_row(png_ptr
);
1034 for (i
= 0; i
< 4 && png_ptr
->pass
== 2; i
++)
1036 png_push_have_row(png_ptr
, png_bytep_NULL
);
1037 png_read_push_finish_row(png_ptr
);
1040 if (png_ptr
->pass
== 4) /* Pass 3 might be empty */
1042 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
1044 png_push_have_row(png_ptr
, png_bytep_NULL
);
1045 png_read_push_finish_row(png_ptr
);
1056 for (i
= 0; i
< 4 && png_ptr
->pass
== 3; i
++)
1058 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
1059 png_read_push_finish_row(png_ptr
);
1062 if (png_ptr
->pass
== 4) /* Skip top two generated rows */
1064 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
1066 png_push_have_row(png_ptr
, png_bytep_NULL
);
1067 png_read_push_finish_row(png_ptr
);
1078 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
1080 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
1081 png_read_push_finish_row(png_ptr
);
1084 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
1086 png_push_have_row(png_ptr
, png_bytep_NULL
);
1087 png_read_push_finish_row(png_ptr
);
1090 if (png_ptr
->pass
== 6) /* Pass 5 might be empty */
1092 png_push_have_row(png_ptr
, png_bytep_NULL
);
1093 png_read_push_finish_row(png_ptr
);
1103 for (i
= 0; i
< 2 && png_ptr
->pass
== 5; i
++)
1105 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
1106 png_read_push_finish_row(png_ptr
);
1109 if (png_ptr
->pass
== 6) /* Skip top generated row */
1111 png_push_have_row(png_ptr
, png_bytep_NULL
);
1112 png_read_push_finish_row(png_ptr
);
1119 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
1120 png_read_push_finish_row(png_ptr
);
1122 if (png_ptr
->pass
!= 6)
1125 png_push_have_row(png_ptr
, png_bytep_NULL
);
1126 png_read_push_finish_row(png_ptr
);
1133 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
1134 png_read_push_finish_row(png_ptr
);
1139 png_read_push_finish_row(png_structp png_ptr
)
1141 #ifdef PNG_USE_LOCAL_ARRAYS
1142 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1144 /* Start of interlace block */
1145 PNG_CONST
int FARDATA png_pass_start
[] = {0, 4, 0, 2, 0, 1, 0};
1147 /* Offset to next interlace block */
1148 PNG_CONST
int FARDATA png_pass_inc
[] = {8, 8, 4, 4, 2, 2, 1};
1150 /* Start of interlace block in the y direction */
1151 PNG_CONST
int FARDATA png_pass_ystart
[] = {0, 0, 4, 0, 2, 0, 1};
1153 /* Offset to next interlace block in the y direction */
1154 PNG_CONST
int FARDATA png_pass_yinc
[] = {8, 8, 8, 4, 4, 2, 2};
1156 /* Height of interlace block. This is not currently used - if you need
1157 * it, uncomment it here and in png.h
1158 PNG_CONST int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
1162 png_ptr
->row_number
++;
1163 if (png_ptr
->row_number
< png_ptr
->num_rows
)
1166 #ifdef PNG_READ_INTERLACING_SUPPORTED
1167 if (png_ptr
->interlaced
)
1169 png_ptr
->row_number
= 0;
1170 png_memset_check(png_ptr
, png_ptr
->prev_row
, 0,
1171 png_ptr
->rowbytes
+ 1);
1175 if ((png_ptr
->pass
== 1 && png_ptr
->width
< 5) ||
1176 (png_ptr
->pass
== 3 && png_ptr
->width
< 3) ||
1177 (png_ptr
->pass
== 5 && png_ptr
->width
< 2))
1180 if (png_ptr
->pass
> 7)
1183 if (png_ptr
->pass
>= 7)
1186 png_ptr
->iwidth
= (png_ptr
->width
+
1187 png_pass_inc
[png_ptr
->pass
] - 1 -
1188 png_pass_start
[png_ptr
->pass
]) /
1189 png_pass_inc
[png_ptr
->pass
];
1191 if (png_ptr
->transformations
& PNG_INTERLACE
)
1194 png_ptr
->num_rows
= (png_ptr
->height
+
1195 png_pass_yinc
[png_ptr
->pass
] - 1 -
1196 png_pass_ystart
[png_ptr
->pass
]) /
1197 png_pass_yinc
[png_ptr
->pass
];
1199 } while (png_ptr
->iwidth
== 0 || png_ptr
->num_rows
== 0);
1201 #endif /* PNG_READ_INTERLACING_SUPPORTED */
1204 #ifdef PNG_READ_tEXt_SUPPORTED
1206 png_push_handle_tEXt(png_structp png_ptr
, png_infop info_ptr
, png_uint_32
1209 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
) || (png_ptr
->mode
& PNG_HAVE_IEND
))
1211 png_error(png_ptr
, "Out of place tEXt");
1212 info_ptr
= info_ptr
; /* To quiet some compiler warnings */
1215 #ifdef PNG_MAX_MALLOC_64K
1216 png_ptr
->skip_length
= 0; /* This may not be necessary */
1218 if (length
> (png_uint_32
)65535L) /* Can't hold entire string in memory */
1220 png_warning(png_ptr
, "tEXt chunk too large to fit in memory");
1221 png_ptr
->skip_length
= length
- (png_uint_32
)65535L;
1222 length
= (png_uint_32
)65535L;
1226 png_ptr
->current_text
= (png_charp
)png_malloc(png_ptr
,
1227 (png_uint_32
)(length
+ 1));
1228 png_ptr
->current_text
[length
] = '\0';
1229 png_ptr
->current_text_ptr
= png_ptr
->current_text
;
1230 png_ptr
->current_text_size
= (png_size_t
)length
;
1231 png_ptr
->current_text_left
= (png_size_t
)length
;
1232 png_ptr
->process_mode
= PNG_READ_tEXt_MODE
;
1236 png_push_read_tEXt(png_structp png_ptr
, png_infop info_ptr
)
1238 if (png_ptr
->buffer_size
&& png_ptr
->current_text_left
)
1240 png_size_t text_size
;
1242 if (png_ptr
->buffer_size
< png_ptr
->current_text_left
)
1243 text_size
= png_ptr
->buffer_size
;
1246 text_size
= png_ptr
->current_text_left
;
1248 png_crc_read(png_ptr
, (png_bytep
)png_ptr
->current_text_ptr
, text_size
);
1249 png_ptr
->current_text_left
-= text_size
;
1250 png_ptr
->current_text_ptr
+= text_size
;
1252 if (!(png_ptr
->current_text_left
))
1259 if (png_ptr
->buffer_size
< 4)
1261 png_push_save_buffer(png_ptr
);
1265 png_push_crc_finish(png_ptr
);
1267 #ifdef PNG_MAX_MALLOC_64K
1268 if (png_ptr
->skip_length
)
1272 key
= png_ptr
->current_text
;
1274 for (text
= key
; *text
; text
++)
1277 if (text
< key
+ png_ptr
->current_text_size
)
1280 text_ptr
= (png_textp
)png_malloc(png_ptr
,
1281 (png_uint_32
)png_sizeof(png_text
));
1282 text_ptr
->compression
= PNG_TEXT_COMPRESSION_NONE
;
1283 text_ptr
->key
= key
;
1284 #ifdef PNG_iTXt_SUPPORTED
1285 text_ptr
->lang
= NULL
;
1286 text_ptr
->lang_key
= NULL
;
1288 text_ptr
->text
= text
;
1290 ret
= png_set_text_2(png_ptr
, info_ptr
, text_ptr
, 1);
1292 png_free(png_ptr
, key
);
1293 png_free(png_ptr
, text_ptr
);
1294 png_ptr
->current_text
= NULL
;
1297 png_warning(png_ptr
, "Insufficient memory to store text chunk.");
1302 #ifdef PNG_READ_zTXt_SUPPORTED
1304 png_push_handle_zTXt(png_structp png_ptr
, png_infop info_ptr
, png_uint_32
1307 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
) || (png_ptr
->mode
& PNG_HAVE_IEND
))
1309 png_error(png_ptr
, "Out of place zTXt");
1310 info_ptr
= info_ptr
; /* To quiet some compiler warnings */
1313 #ifdef PNG_MAX_MALLOC_64K
1314 /* We can't handle zTXt chunks > 64K, since we don't have enough space
1315 * to be able to store the uncompressed data. Actually, the threshold
1316 * is probably around 32K, but it isn't as definite as 64K is.
1318 if (length
> (png_uint_32
)65535L)
1320 png_warning(png_ptr
, "zTXt chunk too large to fit in memory");
1321 png_push_crc_skip(png_ptr
, length
);
1326 png_ptr
->current_text
= (png_charp
)png_malloc(png_ptr
,
1327 (png_uint_32
)(length
+ 1));
1328 png_ptr
->current_text
[length
] = '\0';
1329 png_ptr
->current_text_ptr
= png_ptr
->current_text
;
1330 png_ptr
->current_text_size
= (png_size_t
)length
;
1331 png_ptr
->current_text_left
= (png_size_t
)length
;
1332 png_ptr
->process_mode
= PNG_READ_zTXt_MODE
;
1336 png_push_read_zTXt(png_structp png_ptr
, png_infop info_ptr
)
1338 if (png_ptr
->buffer_size
&& png_ptr
->current_text_left
)
1340 png_size_t text_size
;
1342 if (png_ptr
->buffer_size
< (png_uint_32
)png_ptr
->current_text_left
)
1343 text_size
= png_ptr
->buffer_size
;
1346 text_size
= png_ptr
->current_text_left
;
1348 png_crc_read(png_ptr
, (png_bytep
)png_ptr
->current_text_ptr
, text_size
);
1349 png_ptr
->current_text_left
-= text_size
;
1350 png_ptr
->current_text_ptr
+= text_size
;
1352 if (!(png_ptr
->current_text_left
))
1358 png_size_t text_size
, key_size
;
1360 if (png_ptr
->buffer_size
< 4)
1362 png_push_save_buffer(png_ptr
);
1366 png_push_crc_finish(png_ptr
);
1368 key
= png_ptr
->current_text
;
1370 for (text
= key
; *text
; text
++)
1373 /* zTXt can't have zero text */
1374 if (text
>= key
+ png_ptr
->current_text_size
)
1376 png_ptr
->current_text
= NULL
;
1377 png_free(png_ptr
, key
);
1383 if (*text
!= PNG_TEXT_COMPRESSION_zTXt
) /* Check compression byte */
1385 png_ptr
->current_text
= NULL
;
1386 png_free(png_ptr
, key
);
1392 png_ptr
->zstream
.next_in
= (png_bytep
)text
;
1393 png_ptr
->zstream
.avail_in
= (uInt
)(png_ptr
->current_text_size
-
1395 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
1396 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
1398 key_size
= text
- key
;
1403 while (png_ptr
->zstream
.avail_in
)
1405 ret
= inflate(&png_ptr
->zstream
, Z_PARTIAL_FLUSH
);
1406 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
)
1408 inflateReset(&png_ptr
->zstream
);
1409 png_ptr
->zstream
.avail_in
= 0;
1410 png_ptr
->current_text
= NULL
;
1411 png_free(png_ptr
, key
);
1412 png_free(png_ptr
, text
);
1415 if (!(png_ptr
->zstream
.avail_out
) || ret
== Z_STREAM_END
)
1419 text
= (png_charp
)png_malloc(png_ptr
,
1420 (png_uint_32
)(png_ptr
->zbuf_size
1421 - png_ptr
->zstream
.avail_out
+ key_size
+ 1));
1423 png_memcpy(text
+ key_size
, png_ptr
->zbuf
,
1424 png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
);
1426 png_memcpy(text
, key
, key_size
);
1428 text_size
= key_size
+ png_ptr
->zbuf_size
-
1429 png_ptr
->zstream
.avail_out
;
1431 *(text
+ text_size
) = '\0';
1438 text
= (png_charp
)png_malloc(png_ptr
, text_size
+
1439 (png_uint_32
)(png_ptr
->zbuf_size
1440 - png_ptr
->zstream
.avail_out
+ 1));
1442 png_memcpy(text
, tmp
, text_size
);
1443 png_free(png_ptr
, tmp
);
1445 png_memcpy(text
+ text_size
, png_ptr
->zbuf
,
1446 png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
);
1448 text_size
+= png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
;
1449 *(text
+ text_size
) = '\0';
1451 if (ret
!= Z_STREAM_END
)
1453 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
1454 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
1462 if (ret
== Z_STREAM_END
)
1466 inflateReset(&png_ptr
->zstream
);
1467 png_ptr
->zstream
.avail_in
= 0;
1469 if (ret
!= Z_STREAM_END
)
1471 png_ptr
->current_text
= NULL
;
1472 png_free(png_ptr
, key
);
1473 png_free(png_ptr
, text
);
1477 png_ptr
->current_text
= NULL
;
1478 png_free(png_ptr
, key
);
1482 text_ptr
= (png_textp
)png_malloc(png_ptr
,
1483 (png_uint_32
)png_sizeof(png_text
));
1484 text_ptr
->compression
= PNG_TEXT_COMPRESSION_zTXt
;
1485 text_ptr
->key
= key
;
1486 #ifdef PNG_iTXt_SUPPORTED
1487 text_ptr
->lang
= NULL
;
1488 text_ptr
->lang_key
= NULL
;
1490 text_ptr
->text
= text
;
1492 ret
= png_set_text_2(png_ptr
, info_ptr
, text_ptr
, 1);
1494 png_free(png_ptr
, key
);
1495 png_free(png_ptr
, text_ptr
);
1498 png_warning(png_ptr
, "Insufficient memory to store text chunk.");
1503 #ifdef PNG_READ_iTXt_SUPPORTED
1505 png_push_handle_iTXt(png_structp png_ptr
, png_infop info_ptr
, png_uint_32
1508 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
) || (png_ptr
->mode
& PNG_HAVE_IEND
))
1510 png_error(png_ptr
, "Out of place iTXt");
1511 info_ptr
= info_ptr
; /* To quiet some compiler warnings */
1514 #ifdef PNG_MAX_MALLOC_64K
1515 png_ptr
->skip_length
= 0; /* This may not be necessary */
1517 if (length
> (png_uint_32
)65535L) /* Can't hold entire string in memory */
1519 png_warning(png_ptr
, "iTXt chunk too large to fit in memory");
1520 png_ptr
->skip_length
= length
- (png_uint_32
)65535L;
1521 length
= (png_uint_32
)65535L;
1525 png_ptr
->current_text
= (png_charp
)png_malloc(png_ptr
,
1526 (png_uint_32
)(length
+ 1));
1527 png_ptr
->current_text
[length
] = '\0';
1528 png_ptr
->current_text_ptr
= png_ptr
->current_text
;
1529 png_ptr
->current_text_size
= (png_size_t
)length
;
1530 png_ptr
->current_text_left
= (png_size_t
)length
;
1531 png_ptr
->process_mode
= PNG_READ_iTXt_MODE
;
1535 png_push_read_iTXt(png_structp png_ptr
, png_infop info_ptr
)
1538 if (png_ptr
->buffer_size
&& png_ptr
->current_text_left
)
1540 png_size_t text_size
;
1542 if (png_ptr
->buffer_size
< png_ptr
->current_text_left
)
1543 text_size
= png_ptr
->buffer_size
;
1546 text_size
= png_ptr
->current_text_left
;
1548 png_crc_read(png_ptr
, (png_bytep
)png_ptr
->current_text_ptr
, text_size
);
1549 png_ptr
->current_text_left
-= text_size
;
1550 png_ptr
->current_text_ptr
+= text_size
;
1552 if (!(png_ptr
->current_text_left
))
1562 if (png_ptr
->buffer_size
< 4)
1564 png_push_save_buffer(png_ptr
);
1568 png_push_crc_finish(png_ptr
);
1570 #ifdef PNG_MAX_MALLOC_64K
1571 if (png_ptr
->skip_length
)
1575 key
= png_ptr
->current_text
;
1577 for (lang
= key
; *lang
; lang
++)
1580 if (lang
< key
+ png_ptr
->current_text_size
- 3)
1583 comp_flag
= *lang
++;
1584 lang
++; /* Skip comp_type, always zero */
1586 for (lang_key
= lang
; *lang_key
; lang_key
++)
1589 lang_key
++; /* Skip NUL separator */
1593 if (lang_key
< key
+ png_ptr
->current_text_size
- 1)
1595 for (; *text
; text
++)
1599 if (text
< key
+ png_ptr
->current_text_size
)
1602 text_ptr
= (png_textp
)png_malloc(png_ptr
,
1603 (png_uint_32
)png_sizeof(png_text
));
1605 text_ptr
->compression
= comp_flag
+ 2;
1606 text_ptr
->key
= key
;
1607 text_ptr
->lang
= lang
;
1608 text_ptr
->lang_key
= lang_key
;
1609 text_ptr
->text
= text
;
1610 text_ptr
->text_length
= 0;
1611 text_ptr
->itxt_length
= png_strlen(text
);
1613 ret
= png_set_text_2(png_ptr
, info_ptr
, text_ptr
, 1);
1615 png_ptr
->current_text
= NULL
;
1617 png_free(png_ptr
, text_ptr
);
1619 png_warning(png_ptr
, "Insufficient memory to store iTXt chunk.");
1624 /* This function is called when we haven't found a handler for this
1625 * chunk. If there isn't a problem with the chunk itself (ie a bad chunk
1626 * name or a critical chunk), the chunk is (currently) silently ignored.
1629 png_push_handle_unknown(png_structp png_ptr
, png_infop info_ptr
, png_uint_32
1632 png_uint_32 skip
= 0;
1634 if (!(png_ptr
->chunk_name
[0] & 0x20))
1636 #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
1637 if (png_handle_as_unknown(png_ptr
, png_ptr
->chunk_name
) !=
1638 PNG_HANDLE_CHUNK_ALWAYS
1639 #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
1640 && png_ptr
->read_user_chunk_fn
== NULL
1644 png_chunk_error(png_ptr
, "unknown critical chunk");
1646 info_ptr
= info_ptr
; /* To quiet some compiler warnings */
1649 #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
1650 if (png_ptr
->flags
& PNG_FLAG_KEEP_UNKNOWN_CHUNKS
)
1652 #ifdef PNG_MAX_MALLOC_64K
1653 if (length
> (png_uint_32
)65535L)
1655 png_warning(png_ptr
, "unknown chunk too large to fit in memory");
1656 skip
= length
- (png_uint_32
)65535L;
1657 length
= (png_uint_32
)65535L;
1660 png_memcpy((png_charp
)png_ptr
->unknown_chunk
.name
,
1661 (png_charp
)png_ptr
->chunk_name
,
1662 png_sizeof(png_ptr
->unknown_chunk
.name
));
1663 png_ptr
->unknown_chunk
.name
[png_sizeof(png_ptr
->unknown_chunk
.name
) - 1]
1666 png_ptr
->unknown_chunk
.size
= (png_size_t
)length
;
1669 png_ptr
->unknown_chunk
.data
= NULL
;
1673 png_ptr
->unknown_chunk
.data
= (png_bytep
)png_malloc(png_ptr
,
1674 (png_uint_32
)length
);
1675 png_crc_read(png_ptr
, (png_bytep
)png_ptr
->unknown_chunk
.data
, length
);
1678 #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
1679 if (png_ptr
->read_user_chunk_fn
!= NULL
)
1681 /* Callback to user unknown chunk handler */
1683 ret
= (*(png_ptr
->read_user_chunk_fn
))
1684 (png_ptr
, &png_ptr
->unknown_chunk
);
1687 png_chunk_error(png_ptr
, "error in user chunk");
1691 if (!(png_ptr
->chunk_name
[0] & 0x20))
1692 if (png_handle_as_unknown(png_ptr
, png_ptr
->chunk_name
) !=
1693 PNG_HANDLE_CHUNK_ALWAYS
)
1694 png_chunk_error(png_ptr
, "unknown critical chunk");
1695 png_set_unknown_chunks(png_ptr
, info_ptr
,
1696 &png_ptr
->unknown_chunk
, 1);
1702 png_set_unknown_chunks(png_ptr
, info_ptr
, &png_ptr
->unknown_chunk
, 1);
1703 png_free(png_ptr
, png_ptr
->unknown_chunk
.data
);
1704 png_ptr
->unknown_chunk
.data
= NULL
;
1710 png_push_crc_skip(png_ptr
, skip
);
1714 png_push_have_info(png_structp png_ptr
, png_infop info_ptr
)
1716 if (png_ptr
->info_fn
!= NULL
)
1717 (*(png_ptr
->info_fn
))(png_ptr
, info_ptr
);
1721 png_push_have_end(png_structp png_ptr
, png_infop info_ptr
)
1723 if (png_ptr
->end_fn
!= NULL
)
1724 (*(png_ptr
->end_fn
))(png_ptr
, info_ptr
);
1728 png_push_have_row(png_structp png_ptr
, png_bytep row
)
1730 if (png_ptr
->row_fn
!= NULL
)
1731 (*(png_ptr
->row_fn
))(png_ptr
, row
, png_ptr
->row_number
,
1732 (int)png_ptr
->pass
);
1736 png_progressive_combine_row (png_structp png_ptr
,
1737 png_bytep old_row
, png_bytep new_row
)
1739 #ifdef PNG_USE_LOCAL_ARRAYS
1740 PNG_CONST
int FARDATA png_pass_dsp_mask
[7] =
1741 {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
1744 if (png_ptr
== NULL
)
1747 if (new_row
!= NULL
) /* new_row must == png_ptr->row_buf here. */
1748 png_combine_row(png_ptr
, old_row
, png_pass_dsp_mask
[png_ptr
->pass
]);
1752 png_set_progressive_read_fn(png_structp png_ptr
, png_voidp progressive_ptr
,
1753 png_progressive_info_ptr info_fn
, png_progressive_row_ptr row_fn
,
1754 png_progressive_end_ptr end_fn
)
1756 if (png_ptr
== NULL
)
1759 png_ptr
->info_fn
= info_fn
;
1760 png_ptr
->row_fn
= row_fn
;
1761 png_ptr
->end_fn
= end_fn
;
1763 png_set_read_fn(png_ptr
, progressive_ptr
, png_push_fill_buffer
);
1767 png_get_progressive_ptr(png_structp png_ptr
)
1769 if (png_ptr
== NULL
)
1772 return png_ptr
->io_ptr
;
1774 #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */