2 /* pngpread.c - read a png file in push mode
4 * Last changed in libpng 1.4.3 [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
14 #define PNG_NO_PEDANTIC_WARNINGS
16 #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
)
157 #ifdef PNG_READ_bKGD_SUPPORTED
160 #ifdef PNG_READ_cHRM_SUPPORTED
163 #ifdef PNG_READ_gAMA_SUPPORTED
166 #ifdef PNG_READ_hIST_SUPPORTED
169 #ifdef PNG_READ_iCCP_SUPPORTED
172 #ifdef PNG_READ_iTXt_SUPPORTED
175 #ifdef PNG_READ_oFFs_SUPPORTED
178 #ifdef PNG_READ_pCAL_SUPPORTED
181 #ifdef PNG_READ_pHYs_SUPPORTED
184 #ifdef PNG_READ_sBIT_SUPPORTED
187 #ifdef PNG_READ_sCAL_SUPPORTED
190 #ifdef PNG_READ_sRGB_SUPPORTED
193 #ifdef PNG_READ_sPLT_SUPPORTED
196 #ifdef PNG_READ_tEXt_SUPPORTED
199 #ifdef PNG_READ_tIME_SUPPORTED
202 #ifdef PNG_READ_tRNS_SUPPORTED
205 #ifdef PNG_READ_zTXt_SUPPORTED
209 /* First we make sure we have enough data for the 4 byte chunk name
210 * and the 4 byte chunk length before proceeding with decoding the
211 * chunk data. To fully decode each of these chunks, we also make
212 * sure we have enough data in the buffer for the 4 byte CRC at the
213 * end of every chunk (except IDAT, which is handled separately).
215 if (!(png_ptr
->mode
& PNG_HAVE_CHUNK_HEADER
))
217 png_byte chunk_length
[4];
219 if (png_ptr
->buffer_size
< 8)
221 png_push_save_buffer(png_ptr
);
225 png_push_fill_buffer(png_ptr
, chunk_length
, 4);
226 png_ptr
->push_length
= png_get_uint_31(png_ptr
, chunk_length
);
227 png_reset_crc(png_ptr
);
228 png_crc_read(png_ptr
, png_ptr
->chunk_name
, 4);
229 png_check_chunk_name(png_ptr
, png_ptr
->chunk_name
);
230 png_ptr
->mode
|= PNG_HAVE_CHUNK_HEADER
;
233 if (!png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
234 if (png_ptr
->mode
& PNG_AFTER_IDAT
)
235 png_ptr
->mode
|= PNG_HAVE_CHUNK_AFTER_IDAT
;
237 if (!png_memcmp(png_ptr
->chunk_name
, png_IHDR
, 4))
239 if (png_ptr
->push_length
!= 13)
240 png_error(png_ptr
, "Invalid IHDR length");
242 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
244 png_push_save_buffer(png_ptr
);
248 png_handle_IHDR(png_ptr
, info_ptr
, png_ptr
->push_length
);
251 else if (!png_memcmp(png_ptr
->chunk_name
, png_IEND
, 4))
253 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
255 png_push_save_buffer(png_ptr
);
259 png_handle_IEND(png_ptr
, info_ptr
, png_ptr
->push_length
);
261 png_ptr
->process_mode
= PNG_READ_DONE_MODE
;
262 png_push_have_end(png_ptr
, info_ptr
);
265 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
266 else if (png_handle_as_unknown(png_ptr
, png_ptr
->chunk_name
))
268 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
270 png_push_save_buffer(png_ptr
);
274 if (!png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
275 png_ptr
->mode
|= PNG_HAVE_IDAT
;
277 png_handle_unknown(png_ptr
, info_ptr
, png_ptr
->push_length
);
279 if (!png_memcmp(png_ptr
->chunk_name
, png_PLTE
, 4))
280 png_ptr
->mode
|= PNG_HAVE_PLTE
;
282 else if (!png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
284 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
285 png_error(png_ptr
, "Missing IHDR before IDAT");
287 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
&&
288 !(png_ptr
->mode
& PNG_HAVE_PLTE
))
289 png_error(png_ptr
, "Missing PLTE before IDAT");
294 else if (!png_memcmp(png_ptr
->chunk_name
, png_PLTE
, 4))
296 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
298 png_push_save_buffer(png_ptr
);
301 png_handle_PLTE(png_ptr
, info_ptr
, png_ptr
->push_length
);
304 else if (!png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
306 /* If we reach an IDAT chunk, this means we have read all of the
307 * header chunks, and we can start reading the image (or if this
308 * is called after the image has been read - we have an error).
311 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
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
))
316 png_error(png_ptr
, "Missing PLTE before IDAT");
318 if (png_ptr
->mode
& PNG_HAVE_IDAT
)
320 if (!(png_ptr
->mode
& PNG_HAVE_CHUNK_AFTER_IDAT
))
321 if (png_ptr
->push_length
== 0)
324 if (png_ptr
->mode
& PNG_AFTER_IDAT
)
325 png_benign_error(png_ptr
, "Too many IDATs found");
328 png_ptr
->idat_size
= png_ptr
->push_length
;
329 png_ptr
->mode
|= PNG_HAVE_IDAT
;
330 png_ptr
->process_mode
= PNG_READ_IDAT_MODE
;
331 png_push_have_info(png_ptr
, info_ptr
);
332 png_ptr
->zstream
.avail_out
=
333 (uInt
) PNG_ROWBYTES(png_ptr
->pixel_depth
,
334 png_ptr
->iwidth
) + 1;
335 png_ptr
->zstream
.next_out
= png_ptr
->row_buf
;
339 #ifdef PNG_READ_gAMA_SUPPORTED
340 else if (!png_memcmp(png_ptr
->chunk_name
, png_gAMA
, 4))
342 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
344 png_push_save_buffer(png_ptr
);
348 png_handle_gAMA(png_ptr
, info_ptr
, png_ptr
->push_length
);
352 #ifdef PNG_READ_sBIT_SUPPORTED
353 else if (!png_memcmp(png_ptr
->chunk_name
, png_sBIT
, 4))
355 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
357 png_push_save_buffer(png_ptr
);
361 png_handle_sBIT(png_ptr
, info_ptr
, png_ptr
->push_length
);
365 #ifdef PNG_READ_cHRM_SUPPORTED
366 else if (!png_memcmp(png_ptr
->chunk_name
, png_cHRM
, 4))
368 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
370 png_push_save_buffer(png_ptr
);
374 png_handle_cHRM(png_ptr
, info_ptr
, png_ptr
->push_length
);
378 #ifdef PNG_READ_sRGB_SUPPORTED
379 else if (!png_memcmp(png_ptr
->chunk_name
, png_sRGB
, 4))
381 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
383 png_push_save_buffer(png_ptr
);
387 png_handle_sRGB(png_ptr
, info_ptr
, png_ptr
->push_length
);
391 #ifdef PNG_READ_iCCP_SUPPORTED
392 else if (!png_memcmp(png_ptr
->chunk_name
, png_iCCP
, 4))
394 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
396 png_push_save_buffer(png_ptr
);
400 png_handle_iCCP(png_ptr
, info_ptr
, png_ptr
->push_length
);
404 #ifdef PNG_READ_sPLT_SUPPORTED
405 else if (!png_memcmp(png_ptr
->chunk_name
, png_sPLT
, 4))
407 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
409 png_push_save_buffer(png_ptr
);
413 png_handle_sPLT(png_ptr
, info_ptr
, png_ptr
->push_length
);
417 #ifdef PNG_READ_tRNS_SUPPORTED
418 else if (!png_memcmp(png_ptr
->chunk_name
, png_tRNS
, 4))
420 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
422 png_push_save_buffer(png_ptr
);
426 png_handle_tRNS(png_ptr
, info_ptr
, png_ptr
->push_length
);
430 #ifdef PNG_READ_bKGD_SUPPORTED
431 else if (!png_memcmp(png_ptr
->chunk_name
, png_bKGD
, 4))
433 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
435 png_push_save_buffer(png_ptr
);
439 png_handle_bKGD(png_ptr
, info_ptr
, png_ptr
->push_length
);
443 #ifdef PNG_READ_hIST_SUPPORTED
444 else if (!png_memcmp(png_ptr
->chunk_name
, png_hIST
, 4))
446 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
448 png_push_save_buffer(png_ptr
);
452 png_handle_hIST(png_ptr
, info_ptr
, png_ptr
->push_length
);
456 #ifdef PNG_READ_pHYs_SUPPORTED
457 else if (!png_memcmp(png_ptr
->chunk_name
, png_pHYs
, 4))
459 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
461 png_push_save_buffer(png_ptr
);
465 png_handle_pHYs(png_ptr
, info_ptr
, png_ptr
->push_length
);
469 #ifdef PNG_READ_oFFs_SUPPORTED
470 else if (!png_memcmp(png_ptr
->chunk_name
, png_oFFs
, 4))
472 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
474 png_push_save_buffer(png_ptr
);
478 png_handle_oFFs(png_ptr
, info_ptr
, png_ptr
->push_length
);
482 #ifdef PNG_READ_pCAL_SUPPORTED
483 else if (!png_memcmp(png_ptr
->chunk_name
, png_pCAL
, 4))
485 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
487 png_push_save_buffer(png_ptr
);
491 png_handle_pCAL(png_ptr
, info_ptr
, png_ptr
->push_length
);
495 #ifdef PNG_READ_sCAL_SUPPORTED
496 else if (!png_memcmp(png_ptr
->chunk_name
, png_sCAL
, 4))
498 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
500 png_push_save_buffer(png_ptr
);
504 png_handle_sCAL(png_ptr
, info_ptr
, png_ptr
->push_length
);
508 #ifdef PNG_READ_tIME_SUPPORTED
509 else if (!png_memcmp(png_ptr
->chunk_name
, png_tIME
, 4))
511 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
513 png_push_save_buffer(png_ptr
);
517 png_handle_tIME(png_ptr
, info_ptr
, png_ptr
->push_length
);
521 #ifdef PNG_READ_tEXt_SUPPORTED
522 else if (!png_memcmp(png_ptr
->chunk_name
, png_tEXt
, 4))
524 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
526 png_push_save_buffer(png_ptr
);
530 png_push_handle_tEXt(png_ptr
, info_ptr
, png_ptr
->push_length
);
534 #ifdef PNG_READ_zTXt_SUPPORTED
535 else if (!png_memcmp(png_ptr
->chunk_name
, png_zTXt
, 4))
537 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
539 png_push_save_buffer(png_ptr
);
543 png_push_handle_zTXt(png_ptr
, info_ptr
, png_ptr
->push_length
);
547 #ifdef PNG_READ_iTXt_SUPPORTED
548 else if (!png_memcmp(png_ptr
->chunk_name
, png_iTXt
, 4))
550 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
552 png_push_save_buffer(png_ptr
);
556 png_push_handle_iTXt(png_ptr
, info_ptr
, png_ptr
->push_length
);
562 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
564 png_push_save_buffer(png_ptr
);
567 png_push_handle_unknown(png_ptr
, info_ptr
, png_ptr
->push_length
);
570 png_ptr
->mode
&= ~PNG_HAVE_CHUNK_HEADER
;
574 png_push_crc_skip(png_structp png_ptr
, png_uint_32 skip
)
576 png_ptr
->process_mode
= PNG_SKIP_MODE
;
577 png_ptr
->skip_length
= skip
;
581 png_push_crc_finish(png_structp png_ptr
)
583 if (png_ptr
->skip_length
&& png_ptr
->save_buffer_size
)
585 png_size_t save_size
;
587 if (png_ptr
->skip_length
< (png_uint_32
)png_ptr
->save_buffer_size
)
588 save_size
= (png_size_t
)png_ptr
->skip_length
;
590 save_size
= png_ptr
->save_buffer_size
;
592 png_calculate_crc(png_ptr
, png_ptr
->save_buffer_ptr
, save_size
);
594 png_ptr
->skip_length
-= save_size
;
595 png_ptr
->buffer_size
-= save_size
;
596 png_ptr
->save_buffer_size
-= save_size
;
597 png_ptr
->save_buffer_ptr
+= save_size
;
599 if (png_ptr
->skip_length
&& png_ptr
->current_buffer_size
)
601 png_size_t save_size
;
603 if (png_ptr
->skip_length
< (png_uint_32
)png_ptr
->current_buffer_size
)
604 save_size
= (png_size_t
)png_ptr
->skip_length
;
606 save_size
= png_ptr
->current_buffer_size
;
608 png_calculate_crc(png_ptr
, png_ptr
->current_buffer_ptr
, save_size
);
610 png_ptr
->skip_length
-= save_size
;
611 png_ptr
->buffer_size
-= save_size
;
612 png_ptr
->current_buffer_size
-= save_size
;
613 png_ptr
->current_buffer_ptr
+= save_size
;
615 if (!png_ptr
->skip_length
)
617 if (png_ptr
->buffer_size
< 4)
619 png_push_save_buffer(png_ptr
);
623 png_crc_finish(png_ptr
, 0);
624 png_ptr
->process_mode
= PNG_READ_CHUNK_MODE
;
629 png_push_fill_buffer(png_structp png_ptr
, png_bytep buffer
, png_size_t length
)
637 if (png_ptr
->save_buffer_size
)
639 png_size_t save_size
;
641 if (length
< png_ptr
->save_buffer_size
)
644 save_size
= png_ptr
->save_buffer_size
;
646 png_memcpy(ptr
, png_ptr
->save_buffer_ptr
, save_size
);
649 png_ptr
->buffer_size
-= save_size
;
650 png_ptr
->save_buffer_size
-= save_size
;
651 png_ptr
->save_buffer_ptr
+= save_size
;
653 if (length
&& png_ptr
->current_buffer_size
)
655 png_size_t save_size
;
657 if (length
< png_ptr
->current_buffer_size
)
661 save_size
= png_ptr
->current_buffer_size
;
663 png_memcpy(ptr
, png_ptr
->current_buffer_ptr
, save_size
);
664 png_ptr
->buffer_size
-= save_size
;
665 png_ptr
->current_buffer_size
-= save_size
;
666 png_ptr
->current_buffer_ptr
+= save_size
;
671 png_push_save_buffer(png_structp png_ptr
)
673 if (png_ptr
->save_buffer_size
)
675 if (png_ptr
->save_buffer_ptr
!= png_ptr
->save_buffer
)
681 istop
= png_ptr
->save_buffer_size
;
682 for (i
= 0, sp
= png_ptr
->save_buffer_ptr
, dp
= png_ptr
->save_buffer
;
683 i
< istop
; i
++, sp
++, dp
++)
689 if (png_ptr
->save_buffer_size
+ png_ptr
->current_buffer_size
>
690 png_ptr
->save_buffer_max
)
693 png_bytep old_buffer
;
695 if (png_ptr
->save_buffer_size
> PNG_SIZE_MAX
-
696 (png_ptr
->current_buffer_size
+ 256))
698 png_error(png_ptr
, "Potential overflow of save_buffer");
701 new_max
= png_ptr
->save_buffer_size
+ png_ptr
->current_buffer_size
+ 256;
702 old_buffer
= png_ptr
->save_buffer
;
703 png_ptr
->save_buffer
= (png_bytep
)png_malloc_warn(png_ptr
,
704 (png_size_t
)new_max
);
705 if (png_ptr
->save_buffer
== NULL
)
707 png_free(png_ptr
, old_buffer
);
708 png_error(png_ptr
, "Insufficient memory for save_buffer");
710 png_memcpy(png_ptr
->save_buffer
, old_buffer
, png_ptr
->save_buffer_size
);
711 png_free(png_ptr
, old_buffer
);
712 png_ptr
->save_buffer_max
= new_max
;
714 if (png_ptr
->current_buffer_size
)
716 png_memcpy(png_ptr
->save_buffer
+ png_ptr
->save_buffer_size
,
717 png_ptr
->current_buffer_ptr
, png_ptr
->current_buffer_size
);
718 png_ptr
->save_buffer_size
+= png_ptr
->current_buffer_size
;
719 png_ptr
->current_buffer_size
= 0;
721 png_ptr
->save_buffer_ptr
= png_ptr
->save_buffer
;
722 png_ptr
->buffer_size
= 0;
726 png_push_restore_buffer(png_structp png_ptr
, png_bytep buffer
,
727 png_size_t buffer_length
)
729 png_ptr
->current_buffer
= buffer
;
730 png_ptr
->current_buffer_size
= buffer_length
;
731 png_ptr
->buffer_size
= buffer_length
+ png_ptr
->save_buffer_size
;
732 png_ptr
->current_buffer_ptr
= png_ptr
->current_buffer
;
736 png_push_read_IDAT(png_structp png_ptr
)
739 if (!(png_ptr
->mode
& PNG_HAVE_CHUNK_HEADER
))
741 png_byte chunk_length
[4];
743 if (png_ptr
->buffer_size
< 8)
745 png_push_save_buffer(png_ptr
);
749 png_push_fill_buffer(png_ptr
, chunk_length
, 4);
750 png_ptr
->push_length
= png_get_uint_31(png_ptr
, chunk_length
);
751 png_reset_crc(png_ptr
);
752 png_crc_read(png_ptr
, png_ptr
->chunk_name
, 4);
753 png_ptr
->mode
|= PNG_HAVE_CHUNK_HEADER
;
755 if (png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
757 png_ptr
->process_mode
= PNG_READ_CHUNK_MODE
;
758 if (!(png_ptr
->flags
& PNG_FLAG_ZLIB_FINISHED
))
759 png_error(png_ptr
, "Not enough compressed data");
763 png_ptr
->idat_size
= png_ptr
->push_length
;
765 if (png_ptr
->idat_size
&& png_ptr
->save_buffer_size
)
767 png_size_t save_size
;
769 if (png_ptr
->idat_size
< (png_uint_32
)png_ptr
->save_buffer_size
)
771 save_size
= (png_size_t
)png_ptr
->idat_size
;
773 /* Check for overflow */
774 if ((png_uint_32
)save_size
!= png_ptr
->idat_size
)
775 png_error(png_ptr
, "save_size overflowed in pngpread");
778 save_size
= png_ptr
->save_buffer_size
;
780 png_calculate_crc(png_ptr
, png_ptr
->save_buffer_ptr
, save_size
);
782 png_process_IDAT_data(png_ptr
, png_ptr
->save_buffer_ptr
, save_size
);
784 png_ptr
->idat_size
-= save_size
;
785 png_ptr
->buffer_size
-= save_size
;
786 png_ptr
->save_buffer_size
-= save_size
;
787 png_ptr
->save_buffer_ptr
+= save_size
;
789 if (png_ptr
->idat_size
&& png_ptr
->current_buffer_size
)
791 png_size_t save_size
;
793 if (png_ptr
->idat_size
< (png_uint_32
)png_ptr
->current_buffer_size
)
795 save_size
= (png_size_t
)png_ptr
->idat_size
;
797 /* Check for overflow */
798 if ((png_uint_32
)save_size
!= png_ptr
->idat_size
)
799 png_error(png_ptr
, "save_size overflowed in pngpread");
802 save_size
= png_ptr
->current_buffer_size
;
804 png_calculate_crc(png_ptr
, png_ptr
->current_buffer_ptr
, save_size
);
806 png_process_IDAT_data(png_ptr
, png_ptr
->current_buffer_ptr
, save_size
);
808 png_ptr
->idat_size
-= save_size
;
809 png_ptr
->buffer_size
-= save_size
;
810 png_ptr
->current_buffer_size
-= save_size
;
811 png_ptr
->current_buffer_ptr
+= save_size
;
813 if (!png_ptr
->idat_size
)
815 if (png_ptr
->buffer_size
< 4)
817 png_push_save_buffer(png_ptr
);
821 png_crc_finish(png_ptr
, 0);
822 png_ptr
->mode
&= ~PNG_HAVE_CHUNK_HEADER
;
823 png_ptr
->mode
|= PNG_AFTER_IDAT
;
828 png_process_IDAT_data(png_structp png_ptr
, png_bytep buffer
,
829 png_size_t buffer_length
)
831 /* The caller checks for a non-zero buffer length. */
832 if (!(buffer_length
> 0) || buffer
== NULL
)
833 png_error(png_ptr
, "No IDAT data (internal error)");
835 /* This routine must process all the data it has been given
836 * before returning, calling the row callback as required to
837 * handle the uncompressed results.
839 png_ptr
->zstream
.next_in
= buffer
;
840 png_ptr
->zstream
.avail_in
= (uInt
)buffer_length
;
842 /* Keep going until the decompressed data is all processed
843 * or the stream marked as finished.
845 while (png_ptr
->zstream
.avail_in
> 0 &&
846 !(png_ptr
->flags
& PNG_FLAG_ZLIB_FINISHED
))
850 /* We have data for zlib, but we must check that zlib
851 * has somewhere to put the results. It doesn't matter
852 * if we don't expect any results -- it may be the input
853 * data is just the LZ end code.
855 if (!(png_ptr
->zstream
.avail_out
> 0))
857 png_ptr
->zstream
.avail_out
=
858 (uInt
) PNG_ROWBYTES(png_ptr
->pixel_depth
,
859 png_ptr
->iwidth
) + 1;
860 png_ptr
->zstream
.next_out
= png_ptr
->row_buf
;
863 /* Using Z_SYNC_FLUSH here means that an unterminated
864 * LZ stream can still be handled (a stream with a missing
865 * end code), otherwise (Z_NO_FLUSH) a future zlib
866 * implementation might defer output and, therefore,
867 * change the current behavior. (See comments in inflate.c
868 * for why this doesn't happen at present with zlib 1.2.5.)
870 ret
= inflate(&png_ptr
->zstream
, Z_SYNC_FLUSH
);
872 /* Check for any failure before proceeding. */
873 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
)
875 /* Terminate the decompression. */
876 png_ptr
->flags
|= PNG_FLAG_ZLIB_FINISHED
;
878 /* This may be a truncated stream (missing or
879 * damaged end code). Treat that as a warning.
881 if (png_ptr
->row_number
>= png_ptr
->num_rows
||
883 png_warning(png_ptr
, "Truncated compressed data in IDAT");
885 png_error(png_ptr
, "Decompression error in IDAT");
887 /* Skip the check on unprocessed input */
891 /* Did inflate output any data? */
892 if (png_ptr
->zstream
.next_out
!= png_ptr
->row_buf
)
894 /* Is this unexpected data after the last row?
895 * If it is, artificially terminate the LZ output
898 if (png_ptr
->row_number
>= png_ptr
->num_rows
||
902 png_warning(png_ptr
, "Extra compressed data in IDAT");
903 png_ptr
->flags
|= PNG_FLAG_ZLIB_FINISHED
;
904 /* Do no more processing; skip the unprocessed
910 /* Do we have a complete row? */
911 if (png_ptr
->zstream
.avail_out
== 0)
912 png_push_process_row(png_ptr
);
915 /* And check for the end of the stream. */
916 if (ret
== Z_STREAM_END
)
917 png_ptr
->flags
|= PNG_FLAG_ZLIB_FINISHED
;
920 /* All the data should have been processed, if anything
921 * is left at this point we have bytes of IDAT data
922 * after the zlib end code.
924 if (png_ptr
->zstream
.avail_in
> 0)
925 png_warning(png_ptr
, "Extra compression data");
929 png_push_process_row(png_structp png_ptr
)
931 png_ptr
->row_info
.color_type
= png_ptr
->color_type
;
932 png_ptr
->row_info
.width
= png_ptr
->iwidth
;
933 png_ptr
->row_info
.channels
= png_ptr
->channels
;
934 png_ptr
->row_info
.bit_depth
= png_ptr
->bit_depth
;
935 png_ptr
->row_info
.pixel_depth
= png_ptr
->pixel_depth
;
937 png_ptr
->row_info
.rowbytes
= PNG_ROWBYTES(png_ptr
->row_info
.pixel_depth
,
938 png_ptr
->row_info
.width
);
940 png_read_filter_row(png_ptr
, &(png_ptr
->row_info
),
941 png_ptr
->row_buf
+ 1, png_ptr
->prev_row
+ 1,
942 (int)(png_ptr
->row_buf
[0]));
944 png_memcpy(png_ptr
->prev_row
, png_ptr
->row_buf
, png_ptr
->rowbytes
+ 1);
946 if (png_ptr
->transformations
|| (png_ptr
->flags
&PNG_FLAG_STRIP_ALPHA
))
947 png_do_read_transformations(png_ptr
);
949 #ifdef PNG_READ_INTERLACING_SUPPORTED
950 /* Blow up interlaced rows to full size */
951 if (png_ptr
->interlaced
&& (png_ptr
->transformations
& PNG_INTERLACE
))
953 if (png_ptr
->pass
< 6)
954 /* old interface (pre-1.0.9):
955 png_do_read_interlace(&(png_ptr->row_info),
956 png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
958 png_do_read_interlace(png_ptr
);
960 switch (png_ptr
->pass
)
965 for (i
= 0; i
< 8 && png_ptr
->pass
== 0; i
++)
967 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
968 png_read_push_finish_row(png_ptr
); /* Updates png_ptr->pass */
971 if (png_ptr
->pass
== 2) /* Pass 1 might be empty */
973 for (i
= 0; i
< 4 && png_ptr
->pass
== 2; i
++)
975 png_push_have_row(png_ptr
, NULL
);
976 png_read_push_finish_row(png_ptr
);
980 if (png_ptr
->pass
== 4 && png_ptr
->height
<= 4)
982 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
984 png_push_have_row(png_ptr
, NULL
);
985 png_read_push_finish_row(png_ptr
);
989 if (png_ptr
->pass
== 6 && png_ptr
->height
<= 4)
991 png_push_have_row(png_ptr
, NULL
);
992 png_read_push_finish_row(png_ptr
);
1001 for (i
= 0; i
< 8 && png_ptr
->pass
== 1; i
++)
1003 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
1004 png_read_push_finish_row(png_ptr
);
1007 if (png_ptr
->pass
== 2) /* Skip top 4 generated rows */
1009 for (i
= 0; i
< 4 && png_ptr
->pass
== 2; i
++)
1011 png_push_have_row(png_ptr
, NULL
);
1012 png_read_push_finish_row(png_ptr
);
1023 for (i
= 0; i
< 4 && png_ptr
->pass
== 2; i
++)
1025 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
1026 png_read_push_finish_row(png_ptr
);
1029 for (i
= 0; i
< 4 && png_ptr
->pass
== 2; i
++)
1031 png_push_have_row(png_ptr
, NULL
);
1032 png_read_push_finish_row(png_ptr
);
1035 if (png_ptr
->pass
== 4) /* Pass 3 might be empty */
1037 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
1039 png_push_have_row(png_ptr
, NULL
);
1040 png_read_push_finish_row(png_ptr
);
1051 for (i
= 0; i
< 4 && png_ptr
->pass
== 3; i
++)
1053 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
1054 png_read_push_finish_row(png_ptr
);
1057 if (png_ptr
->pass
== 4) /* Skip top two generated rows */
1059 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
1061 png_push_have_row(png_ptr
, NULL
);
1062 png_read_push_finish_row(png_ptr
);
1073 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
1075 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
1076 png_read_push_finish_row(png_ptr
);
1079 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
1081 png_push_have_row(png_ptr
, NULL
);
1082 png_read_push_finish_row(png_ptr
);
1085 if (png_ptr
->pass
== 6) /* Pass 5 might be empty */
1087 png_push_have_row(png_ptr
, NULL
);
1088 png_read_push_finish_row(png_ptr
);
1098 for (i
= 0; i
< 2 && png_ptr
->pass
== 5; i
++)
1100 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
1101 png_read_push_finish_row(png_ptr
);
1104 if (png_ptr
->pass
== 6) /* Skip top generated row */
1106 png_push_have_row(png_ptr
, NULL
);
1107 png_read_push_finish_row(png_ptr
);
1114 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
1115 png_read_push_finish_row(png_ptr
);
1117 if (png_ptr
->pass
!= 6)
1120 png_push_have_row(png_ptr
, NULL
);
1121 png_read_push_finish_row(png_ptr
);
1128 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
1129 png_read_push_finish_row(png_ptr
);
1134 png_read_push_finish_row(png_structp png_ptr
)
1136 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1138 /* Start of interlace block */
1139 PNG_CONST
int FARDATA png_pass_start
[] = {0, 4, 0, 2, 0, 1, 0};
1141 /* Offset to next interlace block */
1142 PNG_CONST
int FARDATA png_pass_inc
[] = {8, 8, 4, 4, 2, 2, 1};
1144 /* Start of interlace block in the y direction */
1145 PNG_CONST
int FARDATA png_pass_ystart
[] = {0, 0, 4, 0, 2, 0, 1};
1147 /* Offset to next interlace block in the y direction */
1148 PNG_CONST
int FARDATA png_pass_yinc
[] = {8, 8, 8, 4, 4, 2, 2};
1150 /* Height of interlace block. This is not currently used - if you need
1151 * it, uncomment it here and in png.h
1152 PNG_CONST int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
1155 png_ptr
->row_number
++;
1156 if (png_ptr
->row_number
< png_ptr
->num_rows
)
1159 #ifdef PNG_READ_INTERLACING_SUPPORTED
1160 if (png_ptr
->interlaced
)
1162 png_ptr
->row_number
= 0;
1163 png_memset(png_ptr
->prev_row
, 0,
1164 png_ptr
->rowbytes
+ 1);
1168 if ((png_ptr
->pass
== 1 && png_ptr
->width
< 5) ||
1169 (png_ptr
->pass
== 3 && png_ptr
->width
< 3) ||
1170 (png_ptr
->pass
== 5 && png_ptr
->width
< 2))
1173 if (png_ptr
->pass
> 7)
1176 if (png_ptr
->pass
>= 7)
1179 png_ptr
->iwidth
= (png_ptr
->width
+
1180 png_pass_inc
[png_ptr
->pass
] - 1 -
1181 png_pass_start
[png_ptr
->pass
]) /
1182 png_pass_inc
[png_ptr
->pass
];
1184 if (png_ptr
->transformations
& PNG_INTERLACE
)
1187 png_ptr
->num_rows
= (png_ptr
->height
+
1188 png_pass_yinc
[png_ptr
->pass
] - 1 -
1189 png_pass_ystart
[png_ptr
->pass
]) /
1190 png_pass_yinc
[png_ptr
->pass
];
1192 } while (png_ptr
->iwidth
== 0 || png_ptr
->num_rows
== 0);
1194 #endif /* PNG_READ_INTERLACING_SUPPORTED */
1197 #ifdef PNG_READ_tEXt_SUPPORTED
1199 png_push_handle_tEXt(png_structp png_ptr
, png_infop info_ptr
, png_uint_32
1202 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
) || (png_ptr
->mode
& PNG_HAVE_IEND
))
1204 png_error(png_ptr
, "Out of place tEXt");
1205 info_ptr
= info_ptr
; /* To quiet some compiler warnings */
1208 #ifdef PNG_MAX_MALLOC_64K
1209 png_ptr
->skip_length
= 0; /* This may not be necessary */
1211 if (length
> (png_uint_32
)65535L) /* Can't hold entire string in memory */
1213 png_warning(png_ptr
, "tEXt chunk too large to fit in memory");
1214 png_ptr
->skip_length
= length
- (png_uint_32
)65535L;
1215 length
= (png_uint_32
)65535L;
1219 png_ptr
->current_text
= (png_charp
)png_malloc(png_ptr
,
1220 (png_size_t
)(length
+ 1));
1221 png_ptr
->current_text
[length
] = '\0';
1222 png_ptr
->current_text_ptr
= png_ptr
->current_text
;
1223 png_ptr
->current_text_size
= (png_size_t
)length
;
1224 png_ptr
->current_text_left
= (png_size_t
)length
;
1225 png_ptr
->process_mode
= PNG_READ_tEXt_MODE
;
1229 png_push_read_tEXt(png_structp png_ptr
, png_infop info_ptr
)
1231 if (png_ptr
->buffer_size
&& png_ptr
->current_text_left
)
1233 png_size_t text_size
;
1235 if (png_ptr
->buffer_size
< png_ptr
->current_text_left
)
1236 text_size
= png_ptr
->buffer_size
;
1239 text_size
= png_ptr
->current_text_left
;
1241 png_crc_read(png_ptr
, (png_bytep
)png_ptr
->current_text_ptr
, text_size
);
1242 png_ptr
->current_text_left
-= text_size
;
1243 png_ptr
->current_text_ptr
+= text_size
;
1245 if (!(png_ptr
->current_text_left
))
1252 if (png_ptr
->buffer_size
< 4)
1254 png_push_save_buffer(png_ptr
);
1258 png_push_crc_finish(png_ptr
);
1260 #ifdef PNG_MAX_MALLOC_64K
1261 if (png_ptr
->skip_length
)
1265 key
= png_ptr
->current_text
;
1267 for (text
= key
; *text
; text
++)
1270 if (text
< key
+ png_ptr
->current_text_size
)
1273 text_ptr
= (png_textp
)png_malloc(png_ptr
,
1274 png_sizeof(png_text
));
1275 text_ptr
->compression
= PNG_TEXT_COMPRESSION_NONE
;
1276 text_ptr
->key
= key
;
1277 #ifdef PNG_iTXt_SUPPORTED
1278 text_ptr
->lang
= NULL
;
1279 text_ptr
->lang_key
= NULL
;
1281 text_ptr
->text
= text
;
1283 ret
= png_set_text_2(png_ptr
, info_ptr
, text_ptr
, 1);
1285 png_free(png_ptr
, key
);
1286 png_free(png_ptr
, text_ptr
);
1287 png_ptr
->current_text
= NULL
;
1290 png_warning(png_ptr
, "Insufficient memory to store text chunk");
1295 #ifdef PNG_READ_zTXt_SUPPORTED
1297 png_push_handle_zTXt(png_structp png_ptr
, png_infop info_ptr
, png_uint_32
1300 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
) || (png_ptr
->mode
& PNG_HAVE_IEND
))
1302 png_error(png_ptr
, "Out of place zTXt");
1303 info_ptr
= info_ptr
; /* To quiet some compiler warnings */
1306 #ifdef PNG_MAX_MALLOC_64K
1307 /* We can't handle zTXt chunks > 64K, since we don't have enough space
1308 * to be able to store the uncompressed data. Actually, the threshold
1309 * is probably around 32K, but it isn't as definite as 64K is.
1311 if (length
> (png_uint_32
)65535L)
1313 png_warning(png_ptr
, "zTXt chunk too large to fit in memory");
1314 png_push_crc_skip(png_ptr
, length
);
1319 png_ptr
->current_text
= (png_charp
)png_malloc(png_ptr
,
1320 (png_size_t
)(length
+ 1));
1321 png_ptr
->current_text
[length
] = '\0';
1322 png_ptr
->current_text_ptr
= png_ptr
->current_text
;
1323 png_ptr
->current_text_size
= (png_size_t
)length
;
1324 png_ptr
->current_text_left
= (png_size_t
)length
;
1325 png_ptr
->process_mode
= PNG_READ_zTXt_MODE
;
1329 png_push_read_zTXt(png_structp png_ptr
, png_infop info_ptr
)
1331 if (png_ptr
->buffer_size
&& png_ptr
->current_text_left
)
1333 png_size_t text_size
;
1335 if (png_ptr
->buffer_size
< (png_uint_32
)png_ptr
->current_text_left
)
1336 text_size
= png_ptr
->buffer_size
;
1339 text_size
= png_ptr
->current_text_left
;
1341 png_crc_read(png_ptr
, (png_bytep
)png_ptr
->current_text_ptr
, text_size
);
1342 png_ptr
->current_text_left
-= text_size
;
1343 png_ptr
->current_text_ptr
+= text_size
;
1345 if (!(png_ptr
->current_text_left
))
1351 png_size_t text_size
, key_size
;
1353 if (png_ptr
->buffer_size
< 4)
1355 png_push_save_buffer(png_ptr
);
1359 png_push_crc_finish(png_ptr
);
1361 key
= png_ptr
->current_text
;
1363 for (text
= key
; *text
; text
++)
1366 /* zTXt can't have zero text */
1367 if (text
>= key
+ png_ptr
->current_text_size
)
1369 png_ptr
->current_text
= NULL
;
1370 png_free(png_ptr
, key
);
1376 if (*text
!= PNG_TEXT_COMPRESSION_zTXt
) /* Check compression byte */
1378 png_ptr
->current_text
= NULL
;
1379 png_free(png_ptr
, key
);
1385 png_ptr
->zstream
.next_in
= (png_bytep
)text
;
1386 png_ptr
->zstream
.avail_in
= (uInt
)(png_ptr
->current_text_size
-
1388 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
1389 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
1391 key_size
= text
- key
;
1396 while (png_ptr
->zstream
.avail_in
)
1398 ret
= inflate(&png_ptr
->zstream
, Z_PARTIAL_FLUSH
);
1399 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
)
1401 inflateReset(&png_ptr
->zstream
);
1402 png_ptr
->zstream
.avail_in
= 0;
1403 png_ptr
->current_text
= NULL
;
1404 png_free(png_ptr
, key
);
1405 png_free(png_ptr
, text
);
1408 if (!(png_ptr
->zstream
.avail_out
) || ret
== Z_STREAM_END
)
1412 text
= (png_charp
)png_malloc(png_ptr
,
1414 - png_ptr
->zstream
.avail_out
+ key_size
+ 1));
1416 png_memcpy(text
+ key_size
, png_ptr
->zbuf
,
1417 png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
);
1419 png_memcpy(text
, key
, key_size
);
1421 text_size
= key_size
+ png_ptr
->zbuf_size
-
1422 png_ptr
->zstream
.avail_out
;
1424 *(text
+ text_size
) = '\0';
1431 text
= (png_charp
)png_malloc(png_ptr
, text_size
+
1433 - png_ptr
->zstream
.avail_out
+ 1));
1435 png_memcpy(text
, tmp
, text_size
);
1436 png_free(png_ptr
, tmp
);
1438 png_memcpy(text
+ text_size
, png_ptr
->zbuf
,
1439 png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
);
1441 text_size
+= png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
;
1442 *(text
+ text_size
) = '\0';
1444 if (ret
!= Z_STREAM_END
)
1446 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
1447 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
1455 if (ret
== Z_STREAM_END
)
1459 inflateReset(&png_ptr
->zstream
);
1460 png_ptr
->zstream
.avail_in
= 0;
1462 if (ret
!= Z_STREAM_END
)
1464 png_ptr
->current_text
= NULL
;
1465 png_free(png_ptr
, key
);
1466 png_free(png_ptr
, text
);
1470 png_ptr
->current_text
= NULL
;
1471 png_free(png_ptr
, key
);
1475 text_ptr
= (png_textp
)png_malloc(png_ptr
,
1476 png_sizeof(png_text
));
1477 text_ptr
->compression
= PNG_TEXT_COMPRESSION_zTXt
;
1478 text_ptr
->key
= key
;
1479 #ifdef PNG_iTXt_SUPPORTED
1480 text_ptr
->lang
= NULL
;
1481 text_ptr
->lang_key
= NULL
;
1483 text_ptr
->text
= text
;
1485 ret
= png_set_text_2(png_ptr
, info_ptr
, text_ptr
, 1);
1487 png_free(png_ptr
, key
);
1488 png_free(png_ptr
, text_ptr
);
1491 png_warning(png_ptr
, "Insufficient memory to store text chunk");
1496 #ifdef PNG_READ_iTXt_SUPPORTED
1498 png_push_handle_iTXt(png_structp png_ptr
, png_infop info_ptr
, png_uint_32
1501 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
) || (png_ptr
->mode
& PNG_HAVE_IEND
))
1503 png_error(png_ptr
, "Out of place iTXt");
1504 info_ptr
= info_ptr
; /* To quiet some compiler warnings */
1507 #ifdef PNG_MAX_MALLOC_64K
1508 png_ptr
->skip_length
= 0; /* This may not be necessary */
1510 if (length
> (png_uint_32
)65535L) /* Can't hold entire string in memory */
1512 png_warning(png_ptr
, "iTXt chunk too large to fit in memory");
1513 png_ptr
->skip_length
= length
- (png_uint_32
)65535L;
1514 length
= (png_uint_32
)65535L;
1518 png_ptr
->current_text
= (png_charp
)png_malloc(png_ptr
,
1519 (png_size_t
)(length
+ 1));
1520 png_ptr
->current_text
[length
] = '\0';
1521 png_ptr
->current_text_ptr
= png_ptr
->current_text
;
1522 png_ptr
->current_text_size
= (png_size_t
)length
;
1523 png_ptr
->current_text_left
= (png_size_t
)length
;
1524 png_ptr
->process_mode
= PNG_READ_iTXt_MODE
;
1528 png_push_read_iTXt(png_structp png_ptr
, png_infop info_ptr
)
1531 if (png_ptr
->buffer_size
&& png_ptr
->current_text_left
)
1533 png_size_t text_size
;
1535 if (png_ptr
->buffer_size
< png_ptr
->current_text_left
)
1536 text_size
= png_ptr
->buffer_size
;
1539 text_size
= png_ptr
->current_text_left
;
1541 png_crc_read(png_ptr
, (png_bytep
)png_ptr
->current_text_ptr
, text_size
);
1542 png_ptr
->current_text_left
-= text_size
;
1543 png_ptr
->current_text_ptr
+= text_size
;
1545 if (!(png_ptr
->current_text_left
))
1555 if (png_ptr
->buffer_size
< 4)
1557 png_push_save_buffer(png_ptr
);
1561 png_push_crc_finish(png_ptr
);
1563 #ifdef PNG_MAX_MALLOC_64K
1564 if (png_ptr
->skip_length
)
1568 key
= png_ptr
->current_text
;
1570 for (lang
= key
; *lang
; lang
++)
1573 if (lang
< key
+ png_ptr
->current_text_size
- 3)
1576 comp_flag
= *lang
++;
1577 lang
++; /* Skip comp_type, always zero */
1579 for (lang_key
= lang
; *lang_key
; lang_key
++)
1582 lang_key
++; /* Skip NUL separator */
1586 if (lang_key
< key
+ png_ptr
->current_text_size
- 1)
1588 for (; *text
; text
++)
1592 if (text
< key
+ png_ptr
->current_text_size
)
1595 text_ptr
= (png_textp
)png_malloc(png_ptr
,
1596 png_sizeof(png_text
));
1598 text_ptr
->compression
= comp_flag
+ 2;
1599 text_ptr
->key
= key
;
1600 text_ptr
->lang
= lang
;
1601 text_ptr
->lang_key
= lang_key
;
1602 text_ptr
->text
= text
;
1603 text_ptr
->text_length
= 0;
1604 text_ptr
->itxt_length
= png_strlen(text
);
1606 ret
= png_set_text_2(png_ptr
, info_ptr
, text_ptr
, 1);
1608 png_ptr
->current_text
= NULL
;
1610 png_free(png_ptr
, text_ptr
);
1612 png_warning(png_ptr
, "Insufficient memory to store iTXt chunk");
1617 /* This function is called when we haven't found a handler for this
1618 * chunk. If there isn't a problem with the chunk itself (ie a bad chunk
1619 * name or a critical chunk), the chunk is (currently) silently ignored.
1622 png_push_handle_unknown(png_structp png_ptr
, png_infop info_ptr
, png_uint_32
1625 png_uint_32 skip
= 0;
1627 if (!(png_ptr
->chunk_name
[0] & 0x20))
1629 #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
1630 if (png_handle_as_unknown(png_ptr
, png_ptr
->chunk_name
) !=
1631 PNG_HANDLE_CHUNK_ALWAYS
1632 #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
1633 && png_ptr
->read_user_chunk_fn
== NULL
1637 png_chunk_error(png_ptr
, "unknown critical chunk");
1639 info_ptr
= info_ptr
; /* To quiet some compiler warnings */
1642 #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
1643 if (png_ptr
->flags
& PNG_FLAG_KEEP_UNKNOWN_CHUNKS
)
1645 #ifdef PNG_MAX_MALLOC_64K
1646 if (length
> (png_uint_32
)65535L)
1648 png_warning(png_ptr
, "unknown chunk too large to fit in memory");
1649 skip
= length
- (png_uint_32
)65535L;
1650 length
= (png_uint_32
)65535L;
1653 png_memcpy((png_charp
)png_ptr
->unknown_chunk
.name
,
1654 (png_charp
)png_ptr
->chunk_name
,
1655 png_sizeof(png_ptr
->unknown_chunk
.name
));
1656 png_ptr
->unknown_chunk
.name
[png_sizeof(png_ptr
->unknown_chunk
.name
) - 1]
1659 png_ptr
->unknown_chunk
.size
= (png_size_t
)length
;
1662 png_ptr
->unknown_chunk
.data
= NULL
;
1666 png_ptr
->unknown_chunk
.data
= (png_bytep
)png_malloc(png_ptr
,
1667 (png_size_t
)length
);
1668 png_crc_read(png_ptr
, (png_bytep
)png_ptr
->unknown_chunk
.data
, length
);
1671 #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
1672 if (png_ptr
->read_user_chunk_fn
!= NULL
)
1674 /* Callback to user unknown chunk handler */
1676 ret
= (*(png_ptr
->read_user_chunk_fn
))
1677 (png_ptr
, &png_ptr
->unknown_chunk
);
1680 png_chunk_error(png_ptr
, "error in user chunk");
1684 if (!(png_ptr
->chunk_name
[0] & 0x20))
1685 if (png_handle_as_unknown(png_ptr
, png_ptr
->chunk_name
) !=
1686 PNG_HANDLE_CHUNK_ALWAYS
)
1687 png_chunk_error(png_ptr
, "unknown critical chunk");
1688 png_set_unknown_chunks(png_ptr
, info_ptr
,
1689 &png_ptr
->unknown_chunk
, 1);
1695 png_set_unknown_chunks(png_ptr
, info_ptr
, &png_ptr
->unknown_chunk
, 1);
1696 png_free(png_ptr
, png_ptr
->unknown_chunk
.data
);
1697 png_ptr
->unknown_chunk
.data
= NULL
;
1703 png_push_crc_skip(png_ptr
, skip
);
1707 png_push_have_info(png_structp png_ptr
, png_infop info_ptr
)
1709 if (png_ptr
->info_fn
!= NULL
)
1710 (*(png_ptr
->info_fn
))(png_ptr
, info_ptr
);
1714 png_push_have_end(png_structp png_ptr
, png_infop info_ptr
)
1716 if (png_ptr
->end_fn
!= NULL
)
1717 (*(png_ptr
->end_fn
))(png_ptr
, info_ptr
);
1721 png_push_have_row(png_structp png_ptr
, png_bytep row
)
1723 if (png_ptr
->row_fn
!= NULL
)
1724 (*(png_ptr
->row_fn
))(png_ptr
, row
, png_ptr
->row_number
,
1725 (int)png_ptr
->pass
);
1729 png_progressive_combine_row (png_structp png_ptr
,
1730 png_bytep old_row
, png_bytep new_row
)
1732 PNG_CONST
int FARDATA png_pass_dsp_mask
[7] =
1733 {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
1735 if (png_ptr
== NULL
)
1738 if (new_row
!= NULL
) /* new_row must == png_ptr->row_buf here. */
1739 png_combine_row(png_ptr
, old_row
, png_pass_dsp_mask
[png_ptr
->pass
]);
1743 png_set_progressive_read_fn(png_structp png_ptr
, png_voidp progressive_ptr
,
1744 png_progressive_info_ptr info_fn
, png_progressive_row_ptr row_fn
,
1745 png_progressive_end_ptr end_fn
)
1747 if (png_ptr
== NULL
)
1750 png_ptr
->info_fn
= info_fn
;
1751 png_ptr
->row_fn
= row_fn
;
1752 png_ptr
->end_fn
= end_fn
;
1754 png_set_read_fn(png_ptr
, progressive_ptr
, png_push_fill_buffer
);
1758 png_get_progressive_ptr(png_structp png_ptr
)
1760 if (png_ptr
== NULL
)
1763 return png_ptr
->io_ptr
;
1765 #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */