2 /* pngpread.c - read a png file in push mode
4 * Last changed in libpng 1.2.37 [June 4, 2009]
5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1998-2009 Glenn Randers-Pehrson
7 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
13 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
15 /* Push model modes */
16 #define PNG_READ_SIG_MODE 0
17 #define PNG_READ_CHUNK_MODE 1
18 #define PNG_READ_IDAT_MODE 2
19 #define PNG_SKIP_MODE 3
20 #define PNG_READ_tEXt_MODE 4
21 #define PNG_READ_zTXt_MODE 5
22 #define PNG_READ_DONE_MODE 6
23 #define PNG_READ_iTXt_MODE 7
24 #define PNG_ERROR_MODE 8
27 png_process_data(png_structp png_ptr
, png_infop info_ptr
,
28 png_bytep buffer
, png_size_t buffer_size
)
30 if (png_ptr
== NULL
|| info_ptr
== NULL
)
33 png_push_restore_buffer(png_ptr
, buffer
, buffer_size
);
35 while (png_ptr
->buffer_size
)
37 png_process_some_data(png_ptr
, info_ptr
);
41 /* What we do with the incoming data depends on what we were previously
42 * doing before we ran out of data...
45 png_process_some_data(png_structp png_ptr
, png_infop info_ptr
)
50 switch (png_ptr
->process_mode
)
52 case PNG_READ_SIG_MODE
:
54 png_push_read_sig(png_ptr
, info_ptr
);
58 case PNG_READ_CHUNK_MODE
:
60 png_push_read_chunk(png_ptr
, info_ptr
);
64 case PNG_READ_IDAT_MODE
:
66 png_push_read_IDAT(png_ptr
);
70 #if defined(PNG_READ_tEXt_SUPPORTED)
71 case PNG_READ_tEXt_MODE
:
73 png_push_read_tEXt(png_ptr
, info_ptr
);
78 #if defined(PNG_READ_zTXt_SUPPORTED)
79 case PNG_READ_zTXt_MODE
:
81 png_push_read_zTXt(png_ptr
, info_ptr
);
86 #if defined(PNG_READ_iTXt_SUPPORTED)
87 case PNG_READ_iTXt_MODE
:
89 png_push_read_iTXt(png_ptr
, info_ptr
);
96 png_push_crc_finish(png_ptr
);
102 png_ptr
->buffer_size
= 0;
108 /* Read any remaining signature bytes from the stream and compare them with
109 * the correct PNG signature. It is possible that this routine is called
110 * with bytes already read from the signature, either because they have been
111 * checked by the calling application, or because of multiple calls to this
115 png_push_read_sig(png_structp png_ptr
, png_infop info_ptr
)
117 png_size_t num_checked
= png_ptr
->sig_bytes
,
118 num_to_check
= 8 - num_checked
;
120 if (png_ptr
->buffer_size
< num_to_check
)
122 num_to_check
= png_ptr
->buffer_size
;
125 png_push_fill_buffer(png_ptr
, &(info_ptr
->signature
[num_checked
]),
127 png_ptr
->sig_bytes
= (png_byte
)(png_ptr
->sig_bytes
+ num_to_check
);
129 if (png_sig_cmp(info_ptr
->signature
, num_checked
, num_to_check
))
131 if (num_checked
< 4 &&
132 png_sig_cmp(info_ptr
->signature
, num_checked
, num_to_check
- 4))
133 png_error(png_ptr
, "Not a PNG file");
135 png_error(png_ptr
, "PNG file corrupted by ASCII conversion");
139 if (png_ptr
->sig_bytes
>= 8)
141 png_ptr
->process_mode
= PNG_READ_CHUNK_MODE
;
147 png_push_read_chunk(png_structp png_ptr
, png_infop info_ptr
)
149 #ifdef PNG_USE_LOCAL_ARRAYS
154 #if defined(PNG_READ_bKGD_SUPPORTED)
157 #if defined(PNG_READ_cHRM_SUPPORTED)
160 #if defined(PNG_READ_gAMA_SUPPORTED)
163 #if defined(PNG_READ_hIST_SUPPORTED)
166 #if defined(PNG_READ_iCCP_SUPPORTED)
169 #if defined(PNG_READ_iTXt_SUPPORTED)
172 #if defined(PNG_READ_oFFs_SUPPORTED)
175 #if defined(PNG_READ_pCAL_SUPPORTED)
178 #if defined(PNG_READ_pHYs_SUPPORTED)
181 #if defined(PNG_READ_sBIT_SUPPORTED)
184 #if defined(PNG_READ_sCAL_SUPPORTED)
187 #if defined(PNG_READ_sRGB_SUPPORTED)
190 #if defined(PNG_READ_sPLT_SUPPORTED)
193 #if defined(PNG_READ_tEXt_SUPPORTED)
196 #if defined(PNG_READ_tIME_SUPPORTED)
199 #if defined(PNG_READ_tRNS_SUPPORTED)
202 #if defined(PNG_READ_zTXt_SUPPORTED)
205 #endif /* PNG_USE_LOCAL_ARRAYS */
206 /* First we make sure we have enough data for the 4 byte chunk name
207 * and the 4 byte chunk length before proceeding with decoding the
208 * chunk data. To fully decode each of these chunks, we also make
209 * sure we have enough data in the buffer for the 4 byte CRC at the
210 * end of every chunk (except IDAT, which is handled separately).
212 if (!(png_ptr
->mode
& PNG_HAVE_CHUNK_HEADER
))
214 png_byte chunk_length
[4];
216 if (png_ptr
->buffer_size
< 8)
218 png_push_save_buffer(png_ptr
);
222 png_push_fill_buffer(png_ptr
, chunk_length
, 4);
223 png_ptr
->push_length
= png_get_uint_31(png_ptr
, chunk_length
);
224 png_reset_crc(png_ptr
);
225 png_crc_read(png_ptr
, png_ptr
->chunk_name
, 4);
226 png_check_chunk_name(png_ptr
, png_ptr
->chunk_name
);
227 png_ptr
->mode
|= PNG_HAVE_CHUNK_HEADER
;
230 if (!png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
231 if (png_ptr
->mode
& PNG_AFTER_IDAT
)
232 png_ptr
->mode
|= PNG_HAVE_CHUNK_AFTER_IDAT
;
234 if (!png_memcmp(png_ptr
->chunk_name
, png_IHDR
, 4))
236 if (png_ptr
->push_length
!= 13)
237 png_error(png_ptr
, "Invalid IHDR length");
239 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
241 png_push_save_buffer(png_ptr
);
245 png_handle_IHDR(png_ptr
, info_ptr
, png_ptr
->push_length
);
248 else if (!png_memcmp(png_ptr
->chunk_name
, png_IEND
, 4))
250 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
252 png_push_save_buffer(png_ptr
);
256 png_handle_IEND(png_ptr
, info_ptr
, png_ptr
->push_length
);
258 png_ptr
->process_mode
= PNG_READ_DONE_MODE
;
259 png_push_have_end(png_ptr
, info_ptr
);
262 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
263 else if (png_handle_as_unknown(png_ptr
, png_ptr
->chunk_name
))
265 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
267 png_push_save_buffer(png_ptr
);
271 if (!png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
272 png_ptr
->mode
|= PNG_HAVE_IDAT
;
274 png_handle_unknown(png_ptr
, info_ptr
, png_ptr
->push_length
);
276 if (!png_memcmp(png_ptr
->chunk_name
, png_PLTE
, 4))
277 png_ptr
->mode
|= PNG_HAVE_PLTE
;
279 else if (!png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
281 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
282 png_error(png_ptr
, "Missing IHDR before IDAT");
284 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
&&
285 !(png_ptr
->mode
& PNG_HAVE_PLTE
))
286 png_error(png_ptr
, "Missing PLTE before IDAT");
291 else if (!png_memcmp(png_ptr
->chunk_name
, png_PLTE
, 4))
293 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
295 png_push_save_buffer(png_ptr
);
298 png_handle_PLTE(png_ptr
, info_ptr
, png_ptr
->push_length
);
301 else if (!png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
303 /* If we reach an IDAT chunk, this means we have read all of the
304 * header chunks, and we can start reading the image (or if this
305 * is called after the image has been read - we have an error).
308 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
309 png_error(png_ptr
, "Missing IHDR before IDAT");
311 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
&&
312 !(png_ptr
->mode
& PNG_HAVE_PLTE
))
313 png_error(png_ptr
, "Missing PLTE before IDAT");
315 if (png_ptr
->mode
& PNG_HAVE_IDAT
)
317 if (!(png_ptr
->mode
& PNG_HAVE_CHUNK_AFTER_IDAT
))
318 if (png_ptr
->push_length
== 0)
321 if (png_ptr
->mode
& PNG_AFTER_IDAT
)
322 png_error(png_ptr
, "Too many IDAT's found");
325 png_ptr
->idat_size
= png_ptr
->push_length
;
326 png_ptr
->mode
|= PNG_HAVE_IDAT
;
327 png_ptr
->process_mode
= PNG_READ_IDAT_MODE
;
328 png_push_have_info(png_ptr
, info_ptr
);
329 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->irowbytes
;
330 png_ptr
->zstream
.next_out
= png_ptr
->row_buf
;
334 #if defined(PNG_READ_gAMA_SUPPORTED)
335 else if (!png_memcmp(png_ptr
->chunk_name
, png_gAMA
, 4))
337 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
339 png_push_save_buffer(png_ptr
);
343 png_handle_gAMA(png_ptr
, info_ptr
, png_ptr
->push_length
);
347 #if defined(PNG_READ_sBIT_SUPPORTED)
348 else if (!png_memcmp(png_ptr
->chunk_name
, png_sBIT
, 4))
350 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
352 png_push_save_buffer(png_ptr
);
356 png_handle_sBIT(png_ptr
, info_ptr
, png_ptr
->push_length
);
360 #if defined(PNG_READ_cHRM_SUPPORTED)
361 else if (!png_memcmp(png_ptr
->chunk_name
, png_cHRM
, 4))
363 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
365 png_push_save_buffer(png_ptr
);
369 png_handle_cHRM(png_ptr
, info_ptr
, png_ptr
->push_length
);
373 #if defined(PNG_READ_sRGB_SUPPORTED)
374 else if (!png_memcmp(png_ptr
->chunk_name
, png_sRGB
, 4))
376 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
378 png_push_save_buffer(png_ptr
);
382 png_handle_sRGB(png_ptr
, info_ptr
, png_ptr
->push_length
);
386 #if defined(PNG_READ_iCCP_SUPPORTED)
387 else if (!png_memcmp(png_ptr
->chunk_name
, png_iCCP
, 4))
389 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
391 png_push_save_buffer(png_ptr
);
395 png_handle_iCCP(png_ptr
, info_ptr
, png_ptr
->push_length
);
399 #if defined(PNG_READ_sPLT_SUPPORTED)
400 else if (!png_memcmp(png_ptr
->chunk_name
, png_sPLT
, 4))
402 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
404 png_push_save_buffer(png_ptr
);
408 png_handle_sPLT(png_ptr
, info_ptr
, png_ptr
->push_length
);
412 #if defined(PNG_READ_tRNS_SUPPORTED)
413 else if (!png_memcmp(png_ptr
->chunk_name
, png_tRNS
, 4))
415 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
417 png_push_save_buffer(png_ptr
);
421 png_handle_tRNS(png_ptr
, info_ptr
, png_ptr
->push_length
);
425 #if defined(PNG_READ_bKGD_SUPPORTED)
426 else if (!png_memcmp(png_ptr
->chunk_name
, png_bKGD
, 4))
428 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
430 png_push_save_buffer(png_ptr
);
434 png_handle_bKGD(png_ptr
, info_ptr
, png_ptr
->push_length
);
438 #if defined(PNG_READ_hIST_SUPPORTED)
439 else if (!png_memcmp(png_ptr
->chunk_name
, png_hIST
, 4))
441 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
443 png_push_save_buffer(png_ptr
);
447 png_handle_hIST(png_ptr
, info_ptr
, png_ptr
->push_length
);
451 #if defined(PNG_READ_pHYs_SUPPORTED)
452 else if (!png_memcmp(png_ptr
->chunk_name
, png_pHYs
, 4))
454 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
456 png_push_save_buffer(png_ptr
);
460 png_handle_pHYs(png_ptr
, info_ptr
, png_ptr
->push_length
);
464 #if defined(PNG_READ_oFFs_SUPPORTED)
465 else if (!png_memcmp(png_ptr
->chunk_name
, png_oFFs
, 4))
467 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
469 png_push_save_buffer(png_ptr
);
473 png_handle_oFFs(png_ptr
, info_ptr
, png_ptr
->push_length
);
477 #if defined(PNG_READ_pCAL_SUPPORTED)
478 else if (!png_memcmp(png_ptr
->chunk_name
, png_pCAL
, 4))
480 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
482 png_push_save_buffer(png_ptr
);
486 png_handle_pCAL(png_ptr
, info_ptr
, png_ptr
->push_length
);
490 #if defined(PNG_READ_sCAL_SUPPORTED)
491 else if (!png_memcmp(png_ptr
->chunk_name
, png_sCAL
, 4))
493 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
495 png_push_save_buffer(png_ptr
);
499 png_handle_sCAL(png_ptr
, info_ptr
, png_ptr
->push_length
);
503 #if defined(PNG_READ_tIME_SUPPORTED)
504 else if (!png_memcmp(png_ptr
->chunk_name
, png_tIME
, 4))
506 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
508 png_push_save_buffer(png_ptr
);
512 png_handle_tIME(png_ptr
, info_ptr
, png_ptr
->push_length
);
516 #if defined(PNG_READ_tEXt_SUPPORTED)
517 else if (!png_memcmp(png_ptr
->chunk_name
, png_tEXt
, 4))
519 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
521 png_push_save_buffer(png_ptr
);
525 png_push_handle_tEXt(png_ptr
, info_ptr
, png_ptr
->push_length
);
529 #if defined(PNG_READ_zTXt_SUPPORTED)
530 else if (!png_memcmp(png_ptr
->chunk_name
, png_zTXt
, 4))
532 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
534 png_push_save_buffer(png_ptr
);
538 png_push_handle_zTXt(png_ptr
, info_ptr
, png_ptr
->push_length
);
542 #if defined(PNG_READ_iTXt_SUPPORTED)
543 else if (!png_memcmp(png_ptr
->chunk_name
, png_iTXt
, 4))
545 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
547 png_push_save_buffer(png_ptr
);
551 png_push_handle_iTXt(png_ptr
, info_ptr
, png_ptr
->push_length
);
557 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
559 png_push_save_buffer(png_ptr
);
562 png_push_handle_unknown(png_ptr
, info_ptr
, png_ptr
->push_length
);
565 png_ptr
->mode
&= ~PNG_HAVE_CHUNK_HEADER
;
569 png_push_crc_skip(png_structp png_ptr
, png_uint_32 skip
)
571 png_ptr
->process_mode
= PNG_SKIP_MODE
;
572 png_ptr
->skip_length
= skip
;
576 png_push_crc_finish(png_structp png_ptr
)
578 if (png_ptr
->skip_length
&& png_ptr
->save_buffer_size
)
580 png_size_t save_size
;
582 if (png_ptr
->skip_length
< (png_uint_32
)png_ptr
->save_buffer_size
)
583 save_size
= (png_size_t
)png_ptr
->skip_length
;
585 save_size
= png_ptr
->save_buffer_size
;
587 png_calculate_crc(png_ptr
, png_ptr
->save_buffer_ptr
, save_size
);
589 png_ptr
->skip_length
-= save_size
;
590 png_ptr
->buffer_size
-= save_size
;
591 png_ptr
->save_buffer_size
-= save_size
;
592 png_ptr
->save_buffer_ptr
+= save_size
;
594 if (png_ptr
->skip_length
&& png_ptr
->current_buffer_size
)
596 png_size_t save_size
;
598 if (png_ptr
->skip_length
< (png_uint_32
)png_ptr
->current_buffer_size
)
599 save_size
= (png_size_t
)png_ptr
->skip_length
;
601 save_size
= png_ptr
->current_buffer_size
;
603 png_calculate_crc(png_ptr
, png_ptr
->current_buffer_ptr
, save_size
);
605 png_ptr
->skip_length
-= save_size
;
606 png_ptr
->buffer_size
-= save_size
;
607 png_ptr
->current_buffer_size
-= save_size
;
608 png_ptr
->current_buffer_ptr
+= save_size
;
610 if (!png_ptr
->skip_length
)
612 if (png_ptr
->buffer_size
< 4)
614 png_push_save_buffer(png_ptr
);
618 png_crc_finish(png_ptr
, 0);
619 png_ptr
->process_mode
= PNG_READ_CHUNK_MODE
;
624 png_push_fill_buffer(png_structp png_ptr
, png_bytep buffer
, png_size_t length
)
632 if (png_ptr
->save_buffer_size
)
634 png_size_t save_size
;
636 if (length
< png_ptr
->save_buffer_size
)
639 save_size
= png_ptr
->save_buffer_size
;
641 png_memcpy(ptr
, png_ptr
->save_buffer_ptr
, save_size
);
644 png_ptr
->buffer_size
-= save_size
;
645 png_ptr
->save_buffer_size
-= save_size
;
646 png_ptr
->save_buffer_ptr
+= save_size
;
648 if (length
&& png_ptr
->current_buffer_size
)
650 png_size_t save_size
;
652 if (length
< png_ptr
->current_buffer_size
)
656 save_size
= png_ptr
->current_buffer_size
;
658 png_memcpy(ptr
, png_ptr
->current_buffer_ptr
, save_size
);
659 png_ptr
->buffer_size
-= save_size
;
660 png_ptr
->current_buffer_size
-= save_size
;
661 png_ptr
->current_buffer_ptr
+= save_size
;
666 png_push_save_buffer(png_structp png_ptr
)
668 if (png_ptr
->save_buffer_size
)
670 if (png_ptr
->save_buffer_ptr
!= png_ptr
->save_buffer
)
676 istop
= png_ptr
->save_buffer_size
;
677 for (i
= 0, sp
= png_ptr
->save_buffer_ptr
, dp
= png_ptr
->save_buffer
;
678 i
< istop
; i
++, sp
++, dp
++)
684 if (png_ptr
->save_buffer_size
+ png_ptr
->current_buffer_size
>
685 png_ptr
->save_buffer_max
)
688 png_bytep old_buffer
;
690 if (png_ptr
->save_buffer_size
> PNG_SIZE_MAX
-
691 (png_ptr
->current_buffer_size
+ 256))
693 png_error(png_ptr
, "Potential overflow of save_buffer");
696 new_max
= png_ptr
->save_buffer_size
+ png_ptr
->current_buffer_size
+ 256;
697 old_buffer
= png_ptr
->save_buffer
;
698 png_ptr
->save_buffer
= (png_bytep
)png_malloc(png_ptr
,
699 (png_uint_32
)new_max
);
700 png_memcpy(png_ptr
->save_buffer
, old_buffer
, png_ptr
->save_buffer_size
);
701 png_free(png_ptr
, old_buffer
);
702 png_ptr
->save_buffer_max
= new_max
;
704 if (png_ptr
->current_buffer_size
)
706 png_memcpy(png_ptr
->save_buffer
+ png_ptr
->save_buffer_size
,
707 png_ptr
->current_buffer_ptr
, png_ptr
->current_buffer_size
);
708 png_ptr
->save_buffer_size
+= png_ptr
->current_buffer_size
;
709 png_ptr
->current_buffer_size
= 0;
711 png_ptr
->save_buffer_ptr
= png_ptr
->save_buffer
;
712 png_ptr
->buffer_size
= 0;
716 png_push_restore_buffer(png_structp png_ptr
, png_bytep buffer
,
717 png_size_t buffer_length
)
719 png_ptr
->current_buffer
= buffer
;
720 png_ptr
->current_buffer_size
= buffer_length
;
721 png_ptr
->buffer_size
= buffer_length
+ png_ptr
->save_buffer_size
;
722 png_ptr
->current_buffer_ptr
= png_ptr
->current_buffer
;
726 png_push_read_IDAT(png_structp png_ptr
)
728 #ifdef PNG_USE_LOCAL_ARRAYS
731 if (!(png_ptr
->mode
& PNG_HAVE_CHUNK_HEADER
))
733 png_byte chunk_length
[4];
735 if (png_ptr
->buffer_size
< 8)
737 png_push_save_buffer(png_ptr
);
741 png_push_fill_buffer(png_ptr
, chunk_length
, 4);
742 png_ptr
->push_length
= png_get_uint_31(png_ptr
, chunk_length
);
743 png_reset_crc(png_ptr
);
744 png_crc_read(png_ptr
, png_ptr
->chunk_name
, 4);
745 png_ptr
->mode
|= PNG_HAVE_CHUNK_HEADER
;
747 if (png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
749 png_ptr
->process_mode
= PNG_READ_CHUNK_MODE
;
750 if (!(png_ptr
->flags
& PNG_FLAG_ZLIB_FINISHED
))
751 png_error(png_ptr
, "Not enough compressed data");
755 png_ptr
->idat_size
= png_ptr
->push_length
;
757 if (png_ptr
->idat_size
&& png_ptr
->save_buffer_size
)
759 png_size_t save_size
;
761 if (png_ptr
->idat_size
< (png_uint_32
)png_ptr
->save_buffer_size
)
763 save_size
= (png_size_t
)png_ptr
->idat_size
;
765 /* Check for overflow */
766 if ((png_uint_32
)save_size
!= png_ptr
->idat_size
)
767 png_error(png_ptr
, "save_size overflowed in pngpread");
770 save_size
= png_ptr
->save_buffer_size
;
772 png_calculate_crc(png_ptr
, png_ptr
->save_buffer_ptr
, save_size
);
774 if (!(png_ptr
->flags
& PNG_FLAG_ZLIB_FINISHED
))
775 png_process_IDAT_data(png_ptr
, png_ptr
->save_buffer_ptr
, save_size
);
777 png_ptr
->idat_size
-= save_size
;
778 png_ptr
->buffer_size
-= save_size
;
779 png_ptr
->save_buffer_size
-= save_size
;
780 png_ptr
->save_buffer_ptr
+= save_size
;
782 if (png_ptr
->idat_size
&& png_ptr
->current_buffer_size
)
784 png_size_t save_size
;
786 if (png_ptr
->idat_size
< (png_uint_32
)png_ptr
->current_buffer_size
)
788 save_size
= (png_size_t
)png_ptr
->idat_size
;
790 /* Check for overflow */
791 if ((png_uint_32
)save_size
!= png_ptr
->idat_size
)
792 png_error(png_ptr
, "save_size overflowed in pngpread");
795 save_size
= png_ptr
->current_buffer_size
;
797 png_calculate_crc(png_ptr
, png_ptr
->current_buffer_ptr
, save_size
);
798 if (!(png_ptr
->flags
& PNG_FLAG_ZLIB_FINISHED
))
799 png_process_IDAT_data(png_ptr
, png_ptr
->current_buffer_ptr
, save_size
);
801 png_ptr
->idat_size
-= save_size
;
802 png_ptr
->buffer_size
-= save_size
;
803 png_ptr
->current_buffer_size
-= save_size
;
804 png_ptr
->current_buffer_ptr
+= save_size
;
806 if (!png_ptr
->idat_size
)
808 if (png_ptr
->buffer_size
< 4)
810 png_push_save_buffer(png_ptr
);
814 png_crc_finish(png_ptr
, 0);
815 png_ptr
->mode
&= ~PNG_HAVE_CHUNK_HEADER
;
816 png_ptr
->mode
|= PNG_AFTER_IDAT
;
821 png_process_IDAT_data(png_structp png_ptr
, png_bytep buffer
,
822 png_size_t buffer_length
)
826 if ((png_ptr
->flags
& PNG_FLAG_ZLIB_FINISHED
) && buffer_length
)
827 png_error(png_ptr
, "Extra compression data");
829 png_ptr
->zstream
.next_in
= buffer
;
830 png_ptr
->zstream
.avail_in
= (uInt
)buffer_length
;
833 ret
= inflate(&png_ptr
->zstream
, Z_PARTIAL_FLUSH
);
836 if (ret
== Z_STREAM_END
)
838 if (png_ptr
->zstream
.avail_in
)
839 png_error(png_ptr
, "Extra compressed data");
841 if (!(png_ptr
->zstream
.avail_out
))
843 png_push_process_row(png_ptr
);
846 png_ptr
->mode
|= PNG_AFTER_IDAT
;
847 png_ptr
->flags
|= PNG_FLAG_ZLIB_FINISHED
;
850 else if (ret
== Z_BUF_ERROR
)
854 png_error(png_ptr
, "Decompression Error");
856 if (!(png_ptr
->zstream
.avail_out
))
859 #if defined(PNG_READ_INTERLACING_SUPPORTED)
860 png_ptr
->interlaced
&& png_ptr
->pass
> 6) ||
861 (!png_ptr
->interlaced
&&
863 png_ptr
->row_number
== png_ptr
->num_rows
))
865 if (png_ptr
->zstream
.avail_in
)
866 png_warning(png_ptr
, "Too much data in IDAT chunks");
867 png_ptr
->flags
|= PNG_FLAG_ZLIB_FINISHED
;
870 png_push_process_row(png_ptr
);
871 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->irowbytes
;
872 png_ptr
->zstream
.next_out
= png_ptr
->row_buf
;
881 png_push_process_row(png_structp png_ptr
)
883 png_ptr
->row_info
.color_type
= png_ptr
->color_type
;
884 png_ptr
->row_info
.width
= png_ptr
->iwidth
;
885 png_ptr
->row_info
.channels
= png_ptr
->channels
;
886 png_ptr
->row_info
.bit_depth
= png_ptr
->bit_depth
;
887 png_ptr
->row_info
.pixel_depth
= png_ptr
->pixel_depth
;
889 png_ptr
->row_info
.rowbytes
= PNG_ROWBYTES(png_ptr
->row_info
.pixel_depth
,
890 png_ptr
->row_info
.width
);
892 png_read_filter_row(png_ptr
, &(png_ptr
->row_info
),
893 png_ptr
->row_buf
+ 1, png_ptr
->prev_row
+ 1,
894 (int)(png_ptr
->row_buf
[0]));
896 png_memcpy_check(png_ptr
, png_ptr
->prev_row
, png_ptr
->row_buf
,
897 png_ptr
->rowbytes
+ 1);
899 if (png_ptr
->transformations
|| (png_ptr
->flags
&PNG_FLAG_STRIP_ALPHA
))
900 png_do_read_transformations(png_ptr
);
902 #if defined(PNG_READ_INTERLACING_SUPPORTED)
903 /* Blow up interlaced rows to full size */
904 if (png_ptr
->interlaced
&& (png_ptr
->transformations
& PNG_INTERLACE
))
906 if (png_ptr
->pass
< 6)
907 /* old interface (pre-1.0.9):
908 png_do_read_interlace(&(png_ptr->row_info),
909 png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
911 png_do_read_interlace(png_ptr
);
913 switch (png_ptr
->pass
)
918 for (i
= 0; i
< 8 && png_ptr
->pass
== 0; i
++)
920 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
921 png_read_push_finish_row(png_ptr
); /* Updates png_ptr->pass */
924 if (png_ptr
->pass
== 2) /* Pass 1 might be empty */
926 for (i
= 0; i
< 4 && png_ptr
->pass
== 2; i
++)
928 png_push_have_row(png_ptr
, png_bytep_NULL
);
929 png_read_push_finish_row(png_ptr
);
933 if (png_ptr
->pass
== 4 && png_ptr
->height
<= 4)
935 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
937 png_push_have_row(png_ptr
, png_bytep_NULL
);
938 png_read_push_finish_row(png_ptr
);
942 if (png_ptr
->pass
== 6 && png_ptr
->height
<= 4)
944 png_push_have_row(png_ptr
, png_bytep_NULL
);
945 png_read_push_finish_row(png_ptr
);
954 for (i
= 0; i
< 8 && png_ptr
->pass
== 1; i
++)
956 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
957 png_read_push_finish_row(png_ptr
);
960 if (png_ptr
->pass
== 2) /* Skip top 4 generated rows */
962 for (i
= 0; i
< 4 && png_ptr
->pass
== 2; i
++)
964 png_push_have_row(png_ptr
, png_bytep_NULL
);
965 png_read_push_finish_row(png_ptr
);
976 for (i
= 0; i
< 4 && png_ptr
->pass
== 2; i
++)
978 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
979 png_read_push_finish_row(png_ptr
);
982 for (i
= 0; i
< 4 && png_ptr
->pass
== 2; i
++)
984 png_push_have_row(png_ptr
, png_bytep_NULL
);
985 png_read_push_finish_row(png_ptr
);
988 if (png_ptr
->pass
== 4) /* Pass 3 might be empty */
990 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
992 png_push_have_row(png_ptr
, png_bytep_NULL
);
993 png_read_push_finish_row(png_ptr
);
1004 for (i
= 0; i
< 4 && png_ptr
->pass
== 3; i
++)
1006 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
1007 png_read_push_finish_row(png_ptr
);
1010 if (png_ptr
->pass
== 4) /* Skip top two generated rows */
1012 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
1014 png_push_have_row(png_ptr
, png_bytep_NULL
);
1015 png_read_push_finish_row(png_ptr
);
1026 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
1028 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
1029 png_read_push_finish_row(png_ptr
);
1032 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
1034 png_push_have_row(png_ptr
, png_bytep_NULL
);
1035 png_read_push_finish_row(png_ptr
);
1038 if (png_ptr
->pass
== 6) /* Pass 5 might be empty */
1040 png_push_have_row(png_ptr
, png_bytep_NULL
);
1041 png_read_push_finish_row(png_ptr
);
1051 for (i
= 0; i
< 2 && png_ptr
->pass
== 5; 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
== 6) /* Skip top generated row */
1059 png_push_have_row(png_ptr
, png_bytep_NULL
);
1060 png_read_push_finish_row(png_ptr
);
1067 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
1068 png_read_push_finish_row(png_ptr
);
1070 if (png_ptr
->pass
!= 6)
1073 png_push_have_row(png_ptr
, png_bytep_NULL
);
1074 png_read_push_finish_row(png_ptr
);
1081 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
1082 png_read_push_finish_row(png_ptr
);
1087 png_read_push_finish_row(png_structp png_ptr
)
1089 #ifdef PNG_USE_LOCAL_ARRAYS
1090 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1092 /* Start of interlace block */
1093 PNG_CONST
int FARDATA png_pass_start
[] = {0, 4, 0, 2, 0, 1, 0};
1095 /* Offset to next interlace block */
1096 PNG_CONST
int FARDATA png_pass_inc
[] = {8, 8, 4, 4, 2, 2, 1};
1098 /* Start of interlace block in the y direction */
1099 PNG_CONST
int FARDATA png_pass_ystart
[] = {0, 0, 4, 0, 2, 0, 1};
1101 /* Offset to next interlace block in the y direction */
1102 PNG_CONST
int FARDATA png_pass_yinc
[] = {8, 8, 8, 4, 4, 2, 2};
1104 /* Height of interlace block. This is not currently used - if you need
1105 * it, uncomment it here and in png.h
1106 PNG_CONST int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
1110 png_ptr
->row_number
++;
1111 if (png_ptr
->row_number
< png_ptr
->num_rows
)
1114 #if defined(PNG_READ_INTERLACING_SUPPORTED)
1115 if (png_ptr
->interlaced
)
1117 png_ptr
->row_number
= 0;
1118 png_memset_check(png_ptr
, png_ptr
->prev_row
, 0,
1119 png_ptr
->rowbytes
+ 1);
1123 if ((png_ptr
->pass
== 1 && png_ptr
->width
< 5) ||
1124 (png_ptr
->pass
== 3 && png_ptr
->width
< 3) ||
1125 (png_ptr
->pass
== 5 && png_ptr
->width
< 2))
1128 if (png_ptr
->pass
> 7)
1131 if (png_ptr
->pass
>= 7)
1134 png_ptr
->iwidth
= (png_ptr
->width
+
1135 png_pass_inc
[png_ptr
->pass
] - 1 -
1136 png_pass_start
[png_ptr
->pass
]) /
1137 png_pass_inc
[png_ptr
->pass
];
1139 png_ptr
->irowbytes
= PNG_ROWBYTES(png_ptr
->pixel_depth
,
1140 png_ptr
->iwidth
) + 1;
1142 if (png_ptr
->transformations
& PNG_INTERLACE
)
1145 png_ptr
->num_rows
= (png_ptr
->height
+
1146 png_pass_yinc
[png_ptr
->pass
] - 1 -
1147 png_pass_ystart
[png_ptr
->pass
]) /
1148 png_pass_yinc
[png_ptr
->pass
];
1150 } while (png_ptr
->iwidth
== 0 || png_ptr
->num_rows
== 0);
1152 #endif /* PNG_READ_INTERLACING_SUPPORTED */
1155 #if defined(PNG_READ_tEXt_SUPPORTED)
1157 png_push_handle_tEXt(png_structp png_ptr
, png_infop info_ptr
, png_uint_32
1160 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
) || (png_ptr
->mode
& PNG_HAVE_IEND
))
1162 png_error(png_ptr
, "Out of place tEXt");
1163 info_ptr
= info_ptr
; /* To quiet some compiler warnings */
1166 #ifdef PNG_MAX_MALLOC_64K
1167 png_ptr
->skip_length
= 0; /* This may not be necessary */
1169 if (length
> (png_uint_32
)65535L) /* Can't hold entire string in memory */
1171 png_warning(png_ptr
, "tEXt chunk too large to fit in memory");
1172 png_ptr
->skip_length
= length
- (png_uint_32
)65535L;
1173 length
= (png_uint_32
)65535L;
1177 png_ptr
->current_text
= (png_charp
)png_malloc(png_ptr
,
1178 (png_uint_32
)(length
+ 1));
1179 png_ptr
->current_text
[length
] = '\0';
1180 png_ptr
->current_text_ptr
= png_ptr
->current_text
;
1181 png_ptr
->current_text_size
= (png_size_t
)length
;
1182 png_ptr
->current_text_left
= (png_size_t
)length
;
1183 png_ptr
->process_mode
= PNG_READ_tEXt_MODE
;
1187 png_push_read_tEXt(png_structp png_ptr
, png_infop info_ptr
)
1189 if (png_ptr
->buffer_size
&& png_ptr
->current_text_left
)
1191 png_size_t text_size
;
1193 if (png_ptr
->buffer_size
< png_ptr
->current_text_left
)
1194 text_size
= png_ptr
->buffer_size
;
1197 text_size
= png_ptr
->current_text_left
;
1199 png_crc_read(png_ptr
, (png_bytep
)png_ptr
->current_text_ptr
, text_size
);
1200 png_ptr
->current_text_left
-= text_size
;
1201 png_ptr
->current_text_ptr
+= text_size
;
1203 if (!(png_ptr
->current_text_left
))
1210 if (png_ptr
->buffer_size
< 4)
1212 png_push_save_buffer(png_ptr
);
1216 png_push_crc_finish(png_ptr
);
1218 #if defined(PNG_MAX_MALLOC_64K)
1219 if (png_ptr
->skip_length
)
1223 key
= png_ptr
->current_text
;
1225 for (text
= key
; *text
; text
++)
1228 if (text
< key
+ png_ptr
->current_text_size
)
1231 text_ptr
= (png_textp
)png_malloc(png_ptr
,
1232 (png_uint_32
)png_sizeof(png_text
));
1233 text_ptr
->compression
= PNG_TEXT_COMPRESSION_NONE
;
1234 text_ptr
->key
= key
;
1235 #ifdef PNG_iTXt_SUPPORTED
1236 text_ptr
->lang
= NULL
;
1237 text_ptr
->lang_key
= NULL
;
1239 text_ptr
->text
= text
;
1241 ret
= png_set_text_2(png_ptr
, info_ptr
, text_ptr
, 1);
1243 png_free(png_ptr
, key
);
1244 png_free(png_ptr
, text_ptr
);
1245 png_ptr
->current_text
= NULL
;
1248 png_warning(png_ptr
, "Insufficient memory to store text chunk.");
1253 #if defined(PNG_READ_zTXt_SUPPORTED)
1255 png_push_handle_zTXt(png_structp png_ptr
, png_infop info_ptr
, png_uint_32
1258 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
) || (png_ptr
->mode
& PNG_HAVE_IEND
))
1260 png_error(png_ptr
, "Out of place zTXt");
1261 info_ptr
= info_ptr
; /* To quiet some compiler warnings */
1264 #ifdef PNG_MAX_MALLOC_64K
1265 /* We can't handle zTXt chunks > 64K, since we don't have enough space
1266 * to be able to store the uncompressed data. Actually, the threshold
1267 * is probably around 32K, but it isn't as definite as 64K is.
1269 if (length
> (png_uint_32
)65535L)
1271 png_warning(png_ptr
, "zTXt chunk too large to fit in memory");
1272 png_push_crc_skip(png_ptr
, length
);
1277 png_ptr
->current_text
= (png_charp
)png_malloc(png_ptr
,
1278 (png_uint_32
)(length
+ 1));
1279 png_ptr
->current_text
[length
] = '\0';
1280 png_ptr
->current_text_ptr
= png_ptr
->current_text
;
1281 png_ptr
->current_text_size
= (png_size_t
)length
;
1282 png_ptr
->current_text_left
= (png_size_t
)length
;
1283 png_ptr
->process_mode
= PNG_READ_zTXt_MODE
;
1287 png_push_read_zTXt(png_structp png_ptr
, png_infop info_ptr
)
1289 if (png_ptr
->buffer_size
&& png_ptr
->current_text_left
)
1291 png_size_t text_size
;
1293 if (png_ptr
->buffer_size
< (png_uint_32
)png_ptr
->current_text_left
)
1294 text_size
= png_ptr
->buffer_size
;
1297 text_size
= png_ptr
->current_text_left
;
1299 png_crc_read(png_ptr
, (png_bytep
)png_ptr
->current_text_ptr
, text_size
);
1300 png_ptr
->current_text_left
-= text_size
;
1301 png_ptr
->current_text_ptr
+= text_size
;
1303 if (!(png_ptr
->current_text_left
))
1309 png_size_t text_size
, key_size
;
1311 if (png_ptr
->buffer_size
< 4)
1313 png_push_save_buffer(png_ptr
);
1317 png_push_crc_finish(png_ptr
);
1319 key
= png_ptr
->current_text
;
1321 for (text
= key
; *text
; text
++)
1324 /* zTXt can't have zero text */
1325 if (text
>= key
+ png_ptr
->current_text_size
)
1327 png_ptr
->current_text
= NULL
;
1328 png_free(png_ptr
, key
);
1334 if (*text
!= PNG_TEXT_COMPRESSION_zTXt
) /* Check compression byte */
1336 png_ptr
->current_text
= NULL
;
1337 png_free(png_ptr
, key
);
1343 png_ptr
->zstream
.next_in
= (png_bytep
)text
;
1344 png_ptr
->zstream
.avail_in
= (uInt
)(png_ptr
->current_text_size
-
1346 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
1347 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
1349 key_size
= text
- key
;
1354 while (png_ptr
->zstream
.avail_in
)
1356 ret
= inflate(&png_ptr
->zstream
, Z_PARTIAL_FLUSH
);
1357 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
)
1359 inflateReset(&png_ptr
->zstream
);
1360 png_ptr
->zstream
.avail_in
= 0;
1361 png_ptr
->current_text
= NULL
;
1362 png_free(png_ptr
, key
);
1363 png_free(png_ptr
, text
);
1366 if (!(png_ptr
->zstream
.avail_out
) || ret
== Z_STREAM_END
)
1370 text
= (png_charp
)png_malloc(png_ptr
,
1371 (png_uint_32
)(png_ptr
->zbuf_size
1372 - png_ptr
->zstream
.avail_out
+ key_size
+ 1));
1374 png_memcpy(text
+ key_size
, png_ptr
->zbuf
,
1375 png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
);
1377 png_memcpy(text
, key
, key_size
);
1379 text_size
= key_size
+ png_ptr
->zbuf_size
-
1380 png_ptr
->zstream
.avail_out
;
1382 *(text
+ text_size
) = '\0';
1389 text
= (png_charp
)png_malloc(png_ptr
, text_size
+
1390 (png_uint_32
)(png_ptr
->zbuf_size
1391 - png_ptr
->zstream
.avail_out
+ 1));
1393 png_memcpy(text
, tmp
, text_size
);
1394 png_free(png_ptr
, tmp
);
1396 png_memcpy(text
+ text_size
, png_ptr
->zbuf
,
1397 png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
);
1399 text_size
+= png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
;
1400 *(text
+ text_size
) = '\0';
1402 if (ret
!= Z_STREAM_END
)
1404 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
1405 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
1413 if (ret
== Z_STREAM_END
)
1417 inflateReset(&png_ptr
->zstream
);
1418 png_ptr
->zstream
.avail_in
= 0;
1420 if (ret
!= Z_STREAM_END
)
1422 png_ptr
->current_text
= NULL
;
1423 png_free(png_ptr
, key
);
1424 png_free(png_ptr
, text
);
1428 png_ptr
->current_text
= NULL
;
1429 png_free(png_ptr
, key
);
1433 text_ptr
= (png_textp
)png_malloc(png_ptr
,
1434 (png_uint_32
)png_sizeof(png_text
));
1435 text_ptr
->compression
= PNG_TEXT_COMPRESSION_zTXt
;
1436 text_ptr
->key
= key
;
1437 #ifdef PNG_iTXt_SUPPORTED
1438 text_ptr
->lang
= NULL
;
1439 text_ptr
->lang_key
= NULL
;
1441 text_ptr
->text
= text
;
1443 ret
= png_set_text_2(png_ptr
, info_ptr
, text_ptr
, 1);
1445 png_free(png_ptr
, key
);
1446 png_free(png_ptr
, text_ptr
);
1449 png_warning(png_ptr
, "Insufficient memory to store text chunk.");
1454 #if defined(PNG_READ_iTXt_SUPPORTED)
1456 png_push_handle_iTXt(png_structp png_ptr
, png_infop info_ptr
, png_uint_32
1459 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
) || (png_ptr
->mode
& PNG_HAVE_IEND
))
1461 png_error(png_ptr
, "Out of place iTXt");
1462 info_ptr
= info_ptr
; /* To quiet some compiler warnings */
1465 #ifdef PNG_MAX_MALLOC_64K
1466 png_ptr
->skip_length
= 0; /* This may not be necessary */
1468 if (length
> (png_uint_32
)65535L) /* Can't hold entire string in memory */
1470 png_warning(png_ptr
, "iTXt chunk too large to fit in memory");
1471 png_ptr
->skip_length
= length
- (png_uint_32
)65535L;
1472 length
= (png_uint_32
)65535L;
1476 png_ptr
->current_text
= (png_charp
)png_malloc(png_ptr
,
1477 (png_uint_32
)(length
+ 1));
1478 png_ptr
->current_text
[length
] = '\0';
1479 png_ptr
->current_text_ptr
= png_ptr
->current_text
;
1480 png_ptr
->current_text_size
= (png_size_t
)length
;
1481 png_ptr
->current_text_left
= (png_size_t
)length
;
1482 png_ptr
->process_mode
= PNG_READ_iTXt_MODE
;
1486 png_push_read_iTXt(png_structp png_ptr
, png_infop info_ptr
)
1489 if (png_ptr
->buffer_size
&& png_ptr
->current_text_left
)
1491 png_size_t text_size
;
1493 if (png_ptr
->buffer_size
< png_ptr
->current_text_left
)
1494 text_size
= png_ptr
->buffer_size
;
1497 text_size
= png_ptr
->current_text_left
;
1499 png_crc_read(png_ptr
, (png_bytep
)png_ptr
->current_text_ptr
, text_size
);
1500 png_ptr
->current_text_left
-= text_size
;
1501 png_ptr
->current_text_ptr
+= text_size
;
1503 if (!(png_ptr
->current_text_left
))
1513 if (png_ptr
->buffer_size
< 4)
1515 png_push_save_buffer(png_ptr
);
1519 png_push_crc_finish(png_ptr
);
1521 #if defined(PNG_MAX_MALLOC_64K)
1522 if (png_ptr
->skip_length
)
1526 key
= png_ptr
->current_text
;
1528 for (lang
= key
; *lang
; lang
++)
1531 if (lang
< key
+ png_ptr
->current_text_size
- 3)
1534 comp_flag
= *lang
++;
1535 lang
++; /* Skip comp_type, always zero */
1537 for (lang_key
= lang
; *lang_key
; lang_key
++)
1540 lang_key
++; /* Skip NUL separator */
1544 if (lang_key
< key
+ png_ptr
->current_text_size
- 1)
1546 for (; *text
; text
++)
1550 if (text
< key
+ png_ptr
->current_text_size
)
1553 text_ptr
= (png_textp
)png_malloc(png_ptr
,
1554 (png_uint_32
)png_sizeof(png_text
));
1556 text_ptr
->compression
= comp_flag
+ 2;
1557 text_ptr
->key
= key
;
1558 text_ptr
->lang
= lang
;
1559 text_ptr
->lang_key
= lang_key
;
1560 text_ptr
->text
= text
;
1561 text_ptr
->text_length
= 0;
1562 text_ptr
->itxt_length
= png_strlen(text
);
1564 ret
= png_set_text_2(png_ptr
, info_ptr
, text_ptr
, 1);
1566 png_ptr
->current_text
= NULL
;
1568 png_free(png_ptr
, text_ptr
);
1570 png_warning(png_ptr
, "Insufficient memory to store iTXt chunk.");
1575 /* This function is called when we haven't found a handler for this
1576 * chunk. If there isn't a problem with the chunk itself (ie a bad chunk
1577 * name or a critical chunk), the chunk is (currently) silently ignored.
1580 png_push_handle_unknown(png_structp png_ptr
, png_infop info_ptr
, png_uint_32
1583 png_uint_32 skip
= 0;
1585 if (!(png_ptr
->chunk_name
[0] & 0x20))
1587 #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
1588 if (png_handle_as_unknown(png_ptr
, png_ptr
->chunk_name
) !=
1589 PNG_HANDLE_CHUNK_ALWAYS
1590 #if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
1591 && png_ptr
->read_user_chunk_fn
== NULL
1595 png_chunk_error(png_ptr
, "unknown critical chunk");
1597 info_ptr
= info_ptr
; /* To quiet some compiler warnings */
1600 #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
1601 if (png_ptr
->flags
& PNG_FLAG_KEEP_UNKNOWN_CHUNKS
)
1603 #ifdef PNG_MAX_MALLOC_64K
1604 if (length
> (png_uint_32
)65535L)
1606 png_warning(png_ptr
, "unknown chunk too large to fit in memory");
1607 skip
= length
- (png_uint_32
)65535L;
1608 length
= (png_uint_32
)65535L;
1611 png_memcpy((png_charp
)png_ptr
->unknown_chunk
.name
,
1612 (png_charp
)png_ptr
->chunk_name
,
1613 png_sizeof(png_ptr
->unknown_chunk
.name
));
1614 png_ptr
->unknown_chunk
.name
[png_sizeof(png_ptr
->unknown_chunk
.name
) - 1]
1617 png_ptr
->unknown_chunk
.size
= (png_size_t
)length
;
1620 png_ptr
->unknown_chunk
.data
= NULL
;
1624 png_ptr
->unknown_chunk
.data
= (png_bytep
)png_malloc(png_ptr
,
1625 (png_uint_32
)length
);
1626 png_crc_read(png_ptr
, (png_bytep
)png_ptr
->unknown_chunk
.data
, length
);
1629 #if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
1630 if (png_ptr
->read_user_chunk_fn
!= NULL
)
1632 /* Callback to user unknown chunk handler */
1634 ret
= (*(png_ptr
->read_user_chunk_fn
))
1635 (png_ptr
, &png_ptr
->unknown_chunk
);
1638 png_chunk_error(png_ptr
, "error in user chunk");
1642 if (!(png_ptr
->chunk_name
[0] & 0x20))
1643 if (png_handle_as_unknown(png_ptr
, png_ptr
->chunk_name
) !=
1644 PNG_HANDLE_CHUNK_ALWAYS
)
1645 png_chunk_error(png_ptr
, "unknown critical chunk");
1646 png_set_unknown_chunks(png_ptr
, info_ptr
,
1647 &png_ptr
->unknown_chunk
, 1);
1653 png_set_unknown_chunks(png_ptr
, info_ptr
, &png_ptr
->unknown_chunk
, 1);
1654 png_free(png_ptr
, png_ptr
->unknown_chunk
.data
);
1655 png_ptr
->unknown_chunk
.data
= NULL
;
1661 png_push_crc_skip(png_ptr
, skip
);
1665 png_push_have_info(png_structp png_ptr
, png_infop info_ptr
)
1667 if (png_ptr
->info_fn
!= NULL
)
1668 (*(png_ptr
->info_fn
))(png_ptr
, info_ptr
);
1672 png_push_have_end(png_structp png_ptr
, png_infop info_ptr
)
1674 if (png_ptr
->end_fn
!= NULL
)
1675 (*(png_ptr
->end_fn
))(png_ptr
, info_ptr
);
1679 png_push_have_row(png_structp png_ptr
, png_bytep row
)
1681 if (png_ptr
->row_fn
!= NULL
)
1682 (*(png_ptr
->row_fn
))(png_ptr
, row
, png_ptr
->row_number
,
1683 (int)png_ptr
->pass
);
1687 png_progressive_combine_row (png_structp png_ptr
,
1688 png_bytep old_row
, png_bytep new_row
)
1690 #ifdef PNG_USE_LOCAL_ARRAYS
1691 PNG_CONST
int FARDATA png_pass_dsp_mask
[7] =
1692 {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
1694 if (png_ptr
== NULL
)
1697 if (new_row
!= NULL
) /* new_row must == png_ptr->row_buf here. */
1698 png_combine_row(png_ptr
, old_row
, png_pass_dsp_mask
[png_ptr
->pass
]);
1702 png_set_progressive_read_fn(png_structp png_ptr
, png_voidp progressive_ptr
,
1703 png_progressive_info_ptr info_fn
, png_progressive_row_ptr row_fn
,
1704 png_progressive_end_ptr end_fn
)
1706 if (png_ptr
== NULL
)
1709 png_ptr
->info_fn
= info_fn
;
1710 png_ptr
->row_fn
= row_fn
;
1711 png_ptr
->end_fn
= end_fn
;
1713 png_set_read_fn(png_ptr
, progressive_ptr
, png_push_fill_buffer
);
1717 png_get_progressive_ptr(png_structp png_ptr
)
1719 if (png_ptr
== NULL
)
1722 return png_ptr
->io_ptr
;
1724 #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */