2 /* pngpread.c - read a png file in push mode
4 * libpng version 1.2.7 - September 12, 2004
5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1998-2004 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.)
14 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
16 /* push model modes */
17 #define PNG_READ_SIG_MODE 0
18 #define PNG_READ_CHUNK_MODE 1
19 #define PNG_READ_IDAT_MODE 2
20 #define PNG_SKIP_MODE 3
21 #define PNG_READ_tEXt_MODE 4
22 #define PNG_READ_zTXt_MODE 5
23 #define PNG_READ_DONE_MODE 6
24 #define PNG_READ_iTXt_MODE 7
25 #define PNG_ERROR_MODE 8
28 png_process_data(png_structp png_ptr
, png_infop info_ptr
,
29 png_bytep buffer
, png_size_t buffer_size
)
31 png_push_restore_buffer(png_ptr
, buffer
, buffer_size
);
33 while (png_ptr
->buffer_size
)
35 png_process_some_data(png_ptr
, info_ptr
);
39 /* What we do with the incoming data depends on what we were previously
40 * doing before we ran out of data...
43 png_process_some_data(png_structp png_ptr
, png_infop info_ptr
)
45 switch (png_ptr
->process_mode
)
47 case PNG_READ_SIG_MODE
:
49 png_push_read_sig(png_ptr
, info_ptr
);
52 case PNG_READ_CHUNK_MODE
:
54 png_push_read_chunk(png_ptr
, info_ptr
);
57 case PNG_READ_IDAT_MODE
:
59 png_push_read_IDAT(png_ptr
);
62 #if defined(PNG_READ_tEXt_SUPPORTED)
63 case PNG_READ_tEXt_MODE
:
65 png_push_read_tEXt(png_ptr
, info_ptr
);
69 #if defined(PNG_READ_zTXt_SUPPORTED)
70 case PNG_READ_zTXt_MODE
:
72 png_push_read_zTXt(png_ptr
, info_ptr
);
76 #if defined(PNG_READ_iTXt_SUPPORTED)
77 case PNG_READ_iTXt_MODE
:
79 png_push_read_iTXt(png_ptr
, info_ptr
);
85 png_push_crc_finish(png_ptr
);
90 png_ptr
->buffer_size
= 0;
96 /* Read any remaining signature bytes from the stream and compare them with
97 * the correct PNG signature. It is possible that this routine is called
98 * with bytes already read from the signature, either because they have been
99 * checked by the calling application, or because of multiple calls to this
103 png_push_read_sig(png_structp png_ptr
, png_infop info_ptr
)
105 png_size_t num_checked
= png_ptr
->sig_bytes
,
106 num_to_check
= 8 - num_checked
;
108 if (png_ptr
->buffer_size
< num_to_check
)
110 num_to_check
= png_ptr
->buffer_size
;
113 png_push_fill_buffer(png_ptr
, &(info_ptr
->signature
[num_checked
]),
115 png_ptr
->sig_bytes
= (png_byte
)(png_ptr
->sig_bytes
+num_to_check
);
117 if (png_sig_cmp(info_ptr
->signature
, num_checked
, num_to_check
))
119 if (num_checked
< 4 &&
120 png_sig_cmp(info_ptr
->signature
, num_checked
, num_to_check
- 4))
121 png_error(png_ptr
, "Not a PNG file");
123 png_error(png_ptr
, "PNG file corrupted by ASCII conversion");
127 if (png_ptr
->sig_bytes
>= 8)
129 png_ptr
->process_mode
= PNG_READ_CHUNK_MODE
;
135 png_push_read_chunk(png_structp png_ptr
, png_infop info_ptr
)
137 #ifdef PNG_USE_LOCAL_ARRAYS
142 #if defined(PNG_READ_bKGD_SUPPORTED)
145 #if defined(PNG_READ_cHRM_SUPPORTED)
148 #if defined(PNG_READ_gAMA_SUPPORTED)
151 #if defined(PNG_READ_hIST_SUPPORTED)
154 #if defined(PNG_READ_iCCP_SUPPORTED)
157 #if defined(PNG_READ_iTXt_SUPPORTED)
160 #if defined(PNG_READ_oFFs_SUPPORTED)
163 #if defined(PNG_READ_pCAL_SUPPORTED)
166 #if defined(PNG_READ_pHYs_SUPPORTED)
169 #if defined(PNG_READ_sBIT_SUPPORTED)
172 #if defined(PNG_READ_sCAL_SUPPORTED)
175 #if defined(PNG_READ_sRGB_SUPPORTED)
178 #if defined(PNG_READ_sPLT_SUPPORTED)
181 #if defined(PNG_READ_tEXt_SUPPORTED)
184 #if defined(PNG_READ_tIME_SUPPORTED)
187 #if defined(PNG_READ_tRNS_SUPPORTED)
190 #if defined(PNG_READ_zTXt_SUPPORTED)
193 #endif /* PNG_USE_LOCAL_ARRAYS */
194 /* First we make sure we have enough data for the 4 byte chunk name
195 * and the 4 byte chunk length before proceeding with decoding the
196 * chunk data. To fully decode each of these chunks, we also make
197 * sure we have enough data in the buffer for the 4 byte CRC at the
198 * end of every chunk (except IDAT, which is handled separately).
200 if (!(png_ptr
->mode
& PNG_HAVE_CHUNK_HEADER
))
202 png_byte chunk_length
[4];
204 if (png_ptr
->buffer_size
< 8)
206 png_push_save_buffer(png_ptr
);
210 png_push_fill_buffer(png_ptr
, chunk_length
, 4);
211 png_ptr
->push_length
= png_get_uint_31(png_ptr
,chunk_length
);
212 png_reset_crc(png_ptr
);
213 png_crc_read(png_ptr
, png_ptr
->chunk_name
, 4);
214 png_ptr
->mode
|= PNG_HAVE_CHUNK_HEADER
;
217 if (!png_memcmp(png_ptr
->chunk_name
, png_IHDR
, 4))
219 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
221 png_push_save_buffer(png_ptr
);
224 png_handle_IHDR(png_ptr
, info_ptr
, png_ptr
->push_length
);
226 else if (!png_memcmp(png_ptr
->chunk_name
, png_IEND
, 4))
228 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
230 png_push_save_buffer(png_ptr
);
233 png_handle_IEND(png_ptr
, info_ptr
, png_ptr
->push_length
);
235 png_ptr
->process_mode
= PNG_READ_DONE_MODE
;
236 png_push_have_end(png_ptr
, info_ptr
);
238 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
239 else if (png_handle_as_unknown(png_ptr
, png_ptr
->chunk_name
))
241 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
243 png_push_save_buffer(png_ptr
);
246 if (!png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
247 png_ptr
->mode
|= PNG_HAVE_IDAT
;
248 png_handle_unknown(png_ptr
, info_ptr
, png_ptr
->push_length
);
249 if (!png_memcmp(png_ptr
->chunk_name
, png_PLTE
, 4))
250 png_ptr
->mode
|= PNG_HAVE_PLTE
;
251 else if (!png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
253 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
254 png_error(png_ptr
, "Missing IHDR before IDAT");
255 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
&&
256 !(png_ptr
->mode
& PNG_HAVE_PLTE
))
257 png_error(png_ptr
, "Missing PLTE before IDAT");
261 else if (!png_memcmp(png_ptr
->chunk_name
, png_PLTE
, 4))
263 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
265 png_push_save_buffer(png_ptr
);
268 png_handle_PLTE(png_ptr
, info_ptr
, png_ptr
->push_length
);
270 else if (!png_memcmp(png_ptr
->chunk_name
, (png_bytep
)png_IDAT
, 4))
272 /* If we reach an IDAT chunk, this means we have read all of the
273 * header chunks, and we can start reading the image (or if this
274 * is called after the image has been read - we have an error).
276 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
277 png_error(png_ptr
, "Missing IHDR before IDAT");
278 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
&&
279 !(png_ptr
->mode
& PNG_HAVE_PLTE
))
280 png_error(png_ptr
, "Missing PLTE before IDAT");
282 if (png_ptr
->mode
& PNG_HAVE_IDAT
)
284 if (png_ptr
->push_length
== 0)
287 if (png_ptr
->mode
& PNG_AFTER_IDAT
)
288 png_error(png_ptr
, "Too many IDAT's found");
291 png_ptr
->idat_size
= png_ptr
->push_length
;
292 png_ptr
->mode
|= PNG_HAVE_IDAT
;
293 png_ptr
->process_mode
= PNG_READ_IDAT_MODE
;
294 png_push_have_info(png_ptr
, info_ptr
);
295 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->irowbytes
;
296 png_ptr
->zstream
.next_out
= png_ptr
->row_buf
;
299 #if defined(PNG_READ_gAMA_SUPPORTED)
300 else if (!png_memcmp(png_ptr
->chunk_name
, png_gAMA
, 4))
302 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
304 png_push_save_buffer(png_ptr
);
307 png_handle_gAMA(png_ptr
, info_ptr
, png_ptr
->push_length
);
310 #if defined(PNG_READ_sBIT_SUPPORTED)
311 else if (!png_memcmp(png_ptr
->chunk_name
, png_sBIT
, 4))
313 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
315 png_push_save_buffer(png_ptr
);
318 png_handle_sBIT(png_ptr
, info_ptr
, png_ptr
->push_length
);
321 #if defined(PNG_READ_cHRM_SUPPORTED)
322 else if (!png_memcmp(png_ptr
->chunk_name
, png_cHRM
, 4))
324 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
326 png_push_save_buffer(png_ptr
);
329 png_handle_cHRM(png_ptr
, info_ptr
, png_ptr
->push_length
);
332 #if defined(PNG_READ_sRGB_SUPPORTED)
333 else if (!png_memcmp(png_ptr
->chunk_name
, png_sRGB
, 4))
335 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
337 png_push_save_buffer(png_ptr
);
340 png_handle_sRGB(png_ptr
, info_ptr
, png_ptr
->push_length
);
343 #if defined(PNG_READ_iCCP_SUPPORTED)
344 else if (!png_memcmp(png_ptr
->chunk_name
, png_iCCP
, 4))
346 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
348 png_push_save_buffer(png_ptr
);
351 png_handle_iCCP(png_ptr
, info_ptr
, png_ptr
->push_length
);
354 #if defined(PNG_READ_sPLT_SUPPORTED)
355 else if (!png_memcmp(png_ptr
->chunk_name
, png_sPLT
, 4))
357 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
359 png_push_save_buffer(png_ptr
);
362 png_handle_sPLT(png_ptr
, info_ptr
, png_ptr
->push_length
);
365 #if defined(PNG_READ_tRNS_SUPPORTED)
366 else if (!png_memcmp(png_ptr
->chunk_name
, png_tRNS
, 4))
368 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
370 png_push_save_buffer(png_ptr
);
373 png_handle_tRNS(png_ptr
, info_ptr
, png_ptr
->push_length
);
376 #if defined(PNG_READ_bKGD_SUPPORTED)
377 else if (!png_memcmp(png_ptr
->chunk_name
, png_bKGD
, 4))
379 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
381 png_push_save_buffer(png_ptr
);
384 png_handle_bKGD(png_ptr
, info_ptr
, png_ptr
->push_length
);
387 #if defined(PNG_READ_hIST_SUPPORTED)
388 else if (!png_memcmp(png_ptr
->chunk_name
, png_hIST
, 4))
390 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
392 png_push_save_buffer(png_ptr
);
395 png_handle_hIST(png_ptr
, info_ptr
, png_ptr
->push_length
);
398 #if defined(PNG_READ_pHYs_SUPPORTED)
399 else if (!png_memcmp(png_ptr
->chunk_name
, png_pHYs
, 4))
401 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
403 png_push_save_buffer(png_ptr
);
406 png_handle_pHYs(png_ptr
, info_ptr
, png_ptr
->push_length
);
409 #if defined(PNG_READ_oFFs_SUPPORTED)
410 else if (!png_memcmp(png_ptr
->chunk_name
, png_oFFs
, 4))
412 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
414 png_push_save_buffer(png_ptr
);
417 png_handle_oFFs(png_ptr
, info_ptr
, png_ptr
->push_length
);
420 #if defined(PNG_READ_pCAL_SUPPORTED)
421 else if (!png_memcmp(png_ptr
->chunk_name
, png_pCAL
, 4))
423 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
425 png_push_save_buffer(png_ptr
);
428 png_handle_pCAL(png_ptr
, info_ptr
, png_ptr
->push_length
);
431 #if defined(PNG_READ_sCAL_SUPPORTED)
432 else if (!png_memcmp(png_ptr
->chunk_name
, png_sCAL
, 4))
434 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
436 png_push_save_buffer(png_ptr
);
439 png_handle_sCAL(png_ptr
, info_ptr
, png_ptr
->push_length
);
442 #if defined(PNG_READ_tIME_SUPPORTED)
443 else if (!png_memcmp(png_ptr
->chunk_name
, png_tIME
, 4))
445 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
447 png_push_save_buffer(png_ptr
);
450 png_handle_tIME(png_ptr
, info_ptr
, png_ptr
->push_length
);
453 #if defined(PNG_READ_tEXt_SUPPORTED)
454 else if (!png_memcmp(png_ptr
->chunk_name
, png_tEXt
, 4))
456 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
458 png_push_save_buffer(png_ptr
);
461 png_push_handle_tEXt(png_ptr
, info_ptr
, png_ptr
->push_length
);
464 #if defined(PNG_READ_zTXt_SUPPORTED)
465 else if (!png_memcmp(png_ptr
->chunk_name
, png_zTXt
, 4))
467 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
469 png_push_save_buffer(png_ptr
);
472 png_push_handle_zTXt(png_ptr
, info_ptr
, png_ptr
->push_length
);
475 #if defined(PNG_READ_iTXt_SUPPORTED)
476 else if (!png_memcmp(png_ptr
->chunk_name
, png_iTXt
, 4))
478 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
480 png_push_save_buffer(png_ptr
);
483 png_push_handle_iTXt(png_ptr
, info_ptr
, png_ptr
->push_length
);
488 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
490 png_push_save_buffer(png_ptr
);
493 png_push_handle_unknown(png_ptr
, info_ptr
, png_ptr
->push_length
);
496 png_ptr
->mode
&= ~PNG_HAVE_CHUNK_HEADER
;
500 png_push_crc_skip(png_structp png_ptr
, png_uint_32 skip
)
502 png_ptr
->process_mode
= PNG_SKIP_MODE
;
503 png_ptr
->skip_length
= skip
;
507 png_push_crc_finish(png_structp png_ptr
)
509 if (png_ptr
->skip_length
&& png_ptr
->save_buffer_size
)
511 png_size_t save_size
;
513 if (png_ptr
->skip_length
< (png_uint_32
)png_ptr
->save_buffer_size
)
514 save_size
= (png_size_t
)png_ptr
->skip_length
;
516 save_size
= png_ptr
->save_buffer_size
;
518 png_calculate_crc(png_ptr
, png_ptr
->save_buffer_ptr
, save_size
);
520 png_ptr
->skip_length
-= save_size
;
521 png_ptr
->buffer_size
-= save_size
;
522 png_ptr
->save_buffer_size
-= save_size
;
523 png_ptr
->save_buffer_ptr
+= save_size
;
525 if (png_ptr
->skip_length
&& png_ptr
->current_buffer_size
)
527 png_size_t save_size
;
529 if (png_ptr
->skip_length
< (png_uint_32
)png_ptr
->current_buffer_size
)
530 save_size
= (png_size_t
)png_ptr
->skip_length
;
532 save_size
= png_ptr
->current_buffer_size
;
534 png_calculate_crc(png_ptr
, png_ptr
->current_buffer_ptr
, save_size
);
536 png_ptr
->skip_length
-= save_size
;
537 png_ptr
->buffer_size
-= save_size
;
538 png_ptr
->current_buffer_size
-= save_size
;
539 png_ptr
->current_buffer_ptr
+= save_size
;
541 if (!png_ptr
->skip_length
)
543 if (png_ptr
->buffer_size
< 4)
545 png_push_save_buffer(png_ptr
);
549 png_crc_finish(png_ptr
, 0);
550 png_ptr
->process_mode
= PNG_READ_CHUNK_MODE
;
555 png_push_fill_buffer(png_structp png_ptr
, png_bytep buffer
, png_size_t length
)
560 if (png_ptr
->save_buffer_size
)
562 png_size_t save_size
;
564 if (length
< png_ptr
->save_buffer_size
)
567 save_size
= png_ptr
->save_buffer_size
;
569 png_memcpy(ptr
, png_ptr
->save_buffer_ptr
, save_size
);
572 png_ptr
->buffer_size
-= save_size
;
573 png_ptr
->save_buffer_size
-= save_size
;
574 png_ptr
->save_buffer_ptr
+= save_size
;
576 if (length
&& png_ptr
->current_buffer_size
)
578 png_size_t save_size
;
580 if (length
< png_ptr
->current_buffer_size
)
583 save_size
= png_ptr
->current_buffer_size
;
585 png_memcpy(ptr
, png_ptr
->current_buffer_ptr
, save_size
);
586 png_ptr
->buffer_size
-= save_size
;
587 png_ptr
->current_buffer_size
-= save_size
;
588 png_ptr
->current_buffer_ptr
+= save_size
;
593 png_push_save_buffer(png_structp png_ptr
)
595 if (png_ptr
->save_buffer_size
)
597 if (png_ptr
->save_buffer_ptr
!= png_ptr
->save_buffer
)
603 istop
= png_ptr
->save_buffer_size
;
604 for (i
= 0, sp
= png_ptr
->save_buffer_ptr
, dp
= png_ptr
->save_buffer
;
605 i
< istop
; i
++, sp
++, dp
++)
611 if (png_ptr
->save_buffer_size
+ png_ptr
->current_buffer_size
>
612 png_ptr
->save_buffer_max
)
615 png_bytep old_buffer
;
617 if (png_ptr
->save_buffer_size
> PNG_SIZE_MAX
-
618 (png_ptr
->current_buffer_size
+ 256))
620 png_error(png_ptr
, "Potential overflow of save_buffer");
622 new_max
= png_ptr
->save_buffer_size
+ png_ptr
->current_buffer_size
+ 256;
623 old_buffer
= png_ptr
->save_buffer
;
624 png_ptr
->save_buffer
= (png_bytep
)png_malloc(png_ptr
,
625 (png_uint_32
)new_max
);
626 png_memcpy(png_ptr
->save_buffer
, old_buffer
, png_ptr
->save_buffer_size
);
627 png_free(png_ptr
, old_buffer
);
628 png_ptr
->save_buffer_max
= new_max
;
630 if (png_ptr
->current_buffer_size
)
632 png_memcpy(png_ptr
->save_buffer
+ png_ptr
->save_buffer_size
,
633 png_ptr
->current_buffer_ptr
, png_ptr
->current_buffer_size
);
634 png_ptr
->save_buffer_size
+= png_ptr
->current_buffer_size
;
635 png_ptr
->current_buffer_size
= 0;
637 png_ptr
->save_buffer_ptr
= png_ptr
->save_buffer
;
638 png_ptr
->buffer_size
= 0;
642 png_push_restore_buffer(png_structp png_ptr
, png_bytep buffer
,
643 png_size_t buffer_length
)
645 png_ptr
->current_buffer
= buffer
;
646 png_ptr
->current_buffer_size
= buffer_length
;
647 png_ptr
->buffer_size
= buffer_length
+ png_ptr
->save_buffer_size
;
648 png_ptr
->current_buffer_ptr
= png_ptr
->current_buffer
;
652 png_push_read_IDAT(png_structp png_ptr
)
654 #ifdef PNG_USE_LOCAL_ARRAYS
657 if (!(png_ptr
->mode
& PNG_HAVE_CHUNK_HEADER
))
659 png_byte chunk_length
[4];
661 if (png_ptr
->buffer_size
< 8)
663 png_push_save_buffer(png_ptr
);
667 png_push_fill_buffer(png_ptr
, chunk_length
, 4);
668 png_ptr
->push_length
= png_get_uint_31(png_ptr
,chunk_length
);
669 png_reset_crc(png_ptr
);
670 png_crc_read(png_ptr
, png_ptr
->chunk_name
, 4);
671 png_ptr
->mode
|= PNG_HAVE_CHUNK_HEADER
;
673 if (png_memcmp(png_ptr
->chunk_name
, (png_bytep
)png_IDAT
, 4))
675 png_ptr
->process_mode
= PNG_READ_CHUNK_MODE
;
676 if (!(png_ptr
->flags
& PNG_FLAG_ZLIB_FINISHED
))
677 png_error(png_ptr
, "Not enough compressed data");
681 png_ptr
->idat_size
= png_ptr
->push_length
;
683 if (png_ptr
->idat_size
&& png_ptr
->save_buffer_size
)
685 png_size_t save_size
;
687 if (png_ptr
->idat_size
< (png_uint_32
)png_ptr
->save_buffer_size
)
689 save_size
= (png_size_t
)png_ptr
->idat_size
;
690 /* check for overflow */
691 if((png_uint_32
)save_size
!= png_ptr
->idat_size
)
692 png_error(png_ptr
, "save_size overflowed in pngpread");
695 save_size
= png_ptr
->save_buffer_size
;
697 png_calculate_crc(png_ptr
, png_ptr
->save_buffer_ptr
, save_size
);
698 if (!(png_ptr
->flags
& PNG_FLAG_ZLIB_FINISHED
))
699 png_process_IDAT_data(png_ptr
, png_ptr
->save_buffer_ptr
, save_size
);
700 png_ptr
->idat_size
-= save_size
;
701 png_ptr
->buffer_size
-= save_size
;
702 png_ptr
->save_buffer_size
-= save_size
;
703 png_ptr
->save_buffer_ptr
+= save_size
;
705 if (png_ptr
->idat_size
&& png_ptr
->current_buffer_size
)
707 png_size_t save_size
;
709 if (png_ptr
->idat_size
< (png_uint_32
)png_ptr
->current_buffer_size
)
711 save_size
= (png_size_t
)png_ptr
->idat_size
;
712 /* check for overflow */
713 if((png_uint_32
)save_size
!= png_ptr
->idat_size
)
714 png_error(png_ptr
, "save_size overflowed in pngpread");
717 save_size
= png_ptr
->current_buffer_size
;
719 png_calculate_crc(png_ptr
, png_ptr
->current_buffer_ptr
, save_size
);
720 if (!(png_ptr
->flags
& PNG_FLAG_ZLIB_FINISHED
))
721 png_process_IDAT_data(png_ptr
, png_ptr
->current_buffer_ptr
, save_size
);
723 png_ptr
->idat_size
-= save_size
;
724 png_ptr
->buffer_size
-= save_size
;
725 png_ptr
->current_buffer_size
-= save_size
;
726 png_ptr
->current_buffer_ptr
+= save_size
;
728 if (!png_ptr
->idat_size
)
730 if (png_ptr
->buffer_size
< 4)
732 png_push_save_buffer(png_ptr
);
736 png_crc_finish(png_ptr
, 0);
737 png_ptr
->mode
&= ~PNG_HAVE_CHUNK_HEADER
;
738 png_ptr
->mode
|= PNG_AFTER_IDAT
;
743 png_process_IDAT_data(png_structp png_ptr
, png_bytep buffer
,
744 png_size_t buffer_length
)
748 if ((png_ptr
->flags
& PNG_FLAG_ZLIB_FINISHED
) && buffer_length
)
749 png_error(png_ptr
, "Extra compression data");
751 png_ptr
->zstream
.next_in
= buffer
;
752 png_ptr
->zstream
.avail_in
= (uInt
)buffer_length
;
755 ret
= inflate(&png_ptr
->zstream
, Z_PARTIAL_FLUSH
);
758 if (ret
== Z_STREAM_END
)
760 if (png_ptr
->zstream
.avail_in
)
761 png_error(png_ptr
, "Extra compressed data");
762 if (!(png_ptr
->zstream
.avail_out
))
764 png_push_process_row(png_ptr
);
767 png_ptr
->mode
|= PNG_AFTER_IDAT
;
768 png_ptr
->flags
|= PNG_FLAG_ZLIB_FINISHED
;
771 else if (ret
== Z_BUF_ERROR
)
774 png_error(png_ptr
, "Decompression Error");
776 if (!(png_ptr
->zstream
.avail_out
))
779 #if defined(PNG_READ_INTERLACING_SUPPORTED)
780 png_ptr
->interlaced
&& png_ptr
->pass
> 6) ||
781 (!png_ptr
->interlaced
&&
783 png_ptr
->row_number
== png_ptr
->num_rows
))
785 if (png_ptr
->zstream
.avail_in
)
786 png_warning(png_ptr
, "Too much data in IDAT chunks");
787 png_ptr
->flags
|= PNG_FLAG_ZLIB_FINISHED
;
790 png_push_process_row(png_ptr
);
791 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->irowbytes
;
792 png_ptr
->zstream
.next_out
= png_ptr
->row_buf
;
800 png_push_process_row(png_structp png_ptr
)
802 png_ptr
->row_info
.color_type
= png_ptr
->color_type
;
803 png_ptr
->row_info
.width
= png_ptr
->iwidth
;
804 png_ptr
->row_info
.channels
= png_ptr
->channels
;
805 png_ptr
->row_info
.bit_depth
= png_ptr
->bit_depth
;
806 png_ptr
->row_info
.pixel_depth
= png_ptr
->pixel_depth
;
808 png_ptr
->row_info
.rowbytes
= PNG_ROWBYTES(png_ptr
->row_info
.pixel_depth
,
809 png_ptr
->row_info
.width
);
811 png_read_filter_row(png_ptr
, &(png_ptr
->row_info
),
812 png_ptr
->row_buf
+ 1, png_ptr
->prev_row
+ 1,
813 (int)(png_ptr
->row_buf
[0]));
815 png_memcpy_check(png_ptr
, png_ptr
->prev_row
, png_ptr
->row_buf
,
816 png_ptr
->rowbytes
+ 1);
818 if (png_ptr
->transformations
)
819 png_do_read_transformations(png_ptr
);
821 #if defined(PNG_READ_INTERLACING_SUPPORTED)
822 /* blow up interlaced rows to full size */
823 if (png_ptr
->interlaced
&& (png_ptr
->transformations
& PNG_INTERLACE
))
825 if (png_ptr
->pass
< 6)
826 /* old interface (pre-1.0.9):
827 png_do_read_interlace(&(png_ptr->row_info),
828 png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
830 png_do_read_interlace(png_ptr
);
832 switch (png_ptr
->pass
)
837 for (i
= 0; i
< 8 && png_ptr
->pass
== 0; i
++)
839 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
840 png_read_push_finish_row(png_ptr
); /* updates png_ptr->pass */
842 if (png_ptr
->pass
== 2) /* pass 1 might be empty */
844 for (i
= 0; i
< 4 && png_ptr
->pass
== 2; i
++)
846 png_push_have_row(png_ptr
, png_bytep_NULL
);
847 png_read_push_finish_row(png_ptr
);
850 if (png_ptr
->pass
== 4 && png_ptr
->height
<= 4)
852 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
854 png_push_have_row(png_ptr
, png_bytep_NULL
);
855 png_read_push_finish_row(png_ptr
);
858 if (png_ptr
->pass
== 6 && png_ptr
->height
<= 4)
860 png_push_have_row(png_ptr
, png_bytep_NULL
);
861 png_read_push_finish_row(png_ptr
);
868 for (i
= 0; i
< 8 && png_ptr
->pass
== 1; i
++)
870 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
871 png_read_push_finish_row(png_ptr
);
873 if (png_ptr
->pass
== 2) /* skip top 4 generated rows */
875 for (i
= 0; i
< 4 && png_ptr
->pass
== 2; i
++)
877 png_push_have_row(png_ptr
, png_bytep_NULL
);
878 png_read_push_finish_row(png_ptr
);
886 for (i
= 0; i
< 4 && png_ptr
->pass
== 2; i
++)
888 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
889 png_read_push_finish_row(png_ptr
);
891 for (i
= 0; i
< 4 && png_ptr
->pass
== 2; i
++)
893 png_push_have_row(png_ptr
, png_bytep_NULL
);
894 png_read_push_finish_row(png_ptr
);
896 if (png_ptr
->pass
== 4) /* pass 3 might be empty */
898 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
900 png_push_have_row(png_ptr
, png_bytep_NULL
);
901 png_read_push_finish_row(png_ptr
);
909 for (i
= 0; i
< 4 && png_ptr
->pass
== 3; i
++)
911 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
912 png_read_push_finish_row(png_ptr
);
914 if (png_ptr
->pass
== 4) /* skip top two generated rows */
916 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
918 png_push_have_row(png_ptr
, png_bytep_NULL
);
919 png_read_push_finish_row(png_ptr
);
927 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
929 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
930 png_read_push_finish_row(png_ptr
);
932 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
934 png_push_have_row(png_ptr
, png_bytep_NULL
);
935 png_read_push_finish_row(png_ptr
);
937 if (png_ptr
->pass
== 6) /* pass 5 might be empty */
939 png_push_have_row(png_ptr
, png_bytep_NULL
);
940 png_read_push_finish_row(png_ptr
);
947 for (i
= 0; i
< 2 && png_ptr
->pass
== 5; i
++)
949 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
950 png_read_push_finish_row(png_ptr
);
952 if (png_ptr
->pass
== 6) /* skip top generated row */
954 png_push_have_row(png_ptr
, png_bytep_NULL
);
955 png_read_push_finish_row(png_ptr
);
961 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
962 png_read_push_finish_row(png_ptr
);
963 if (png_ptr
->pass
!= 6)
965 png_push_have_row(png_ptr
, png_bytep_NULL
);
966 png_read_push_finish_row(png_ptr
);
973 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
974 png_read_push_finish_row(png_ptr
);
979 png_read_push_finish_row(png_structp png_ptr
)
981 #ifdef PNG_USE_LOCAL_ARRAYS
982 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
984 /* start of interlace block */
985 const int FARDATA png_pass_start
[] = {0, 4, 0, 2, 0, 1, 0};
987 /* offset to next interlace block */
988 const int FARDATA png_pass_inc
[] = {8, 8, 4, 4, 2, 2, 1};
990 /* start of interlace block in the y direction */
991 const int FARDATA png_pass_ystart
[] = {0, 0, 4, 0, 2, 0, 1};
993 /* offset to next interlace block in the y direction */
994 const int FARDATA png_pass_yinc
[] = {8, 8, 8, 4, 4, 2, 2};
996 /* Width of interlace block. This is not currently used - if you need
997 * it, uncomment it here and in png.h
998 const int FARDATA png_pass_width[] = {8, 4, 4, 2, 2, 1, 1};
1001 /* Height of interlace block. This is not currently used - if you need
1002 * it, uncomment it here and in png.h
1003 const int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
1007 png_ptr
->row_number
++;
1008 if (png_ptr
->row_number
< png_ptr
->num_rows
)
1011 if (png_ptr
->interlaced
)
1013 png_ptr
->row_number
= 0;
1014 png_memset_check(png_ptr
, png_ptr
->prev_row
, 0,
1015 png_ptr
->rowbytes
+ 1);
1019 if ((png_ptr
->pass
== 1 && png_ptr
->width
< 5) ||
1020 (png_ptr
->pass
== 3 && png_ptr
->width
< 3) ||
1021 (png_ptr
->pass
== 5 && png_ptr
->width
< 2))
1024 if (png_ptr
->pass
> 7)
1026 if (png_ptr
->pass
>= 7)
1029 png_ptr
->iwidth
= (png_ptr
->width
+
1030 png_pass_inc
[png_ptr
->pass
] - 1 -
1031 png_pass_start
[png_ptr
->pass
]) /
1032 png_pass_inc
[png_ptr
->pass
];
1034 png_ptr
->irowbytes
= PNG_ROWBYTES(png_ptr
->pixel_depth
,
1035 png_ptr
->iwidth
) + 1;
1037 if (png_ptr
->transformations
& PNG_INTERLACE
)
1040 png_ptr
->num_rows
= (png_ptr
->height
+
1041 png_pass_yinc
[png_ptr
->pass
] - 1 -
1042 png_pass_ystart
[png_ptr
->pass
]) /
1043 png_pass_yinc
[png_ptr
->pass
];
1045 } while (png_ptr
->iwidth
== 0 || png_ptr
->num_rows
== 0);
1049 #if defined(PNG_READ_tEXt_SUPPORTED)
1051 png_push_handle_tEXt(png_structp png_ptr
, png_infop info_ptr
, png_uint_32
1054 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
) || (png_ptr
->mode
& PNG_HAVE_IEND
))
1056 png_error(png_ptr
, "Out of place tEXt");
1057 /* to quiet some compiler warnings */
1058 if(info_ptr
== NULL
) return;
1061 #ifdef PNG_MAX_MALLOC_64K
1062 png_ptr
->skip_length
= 0; /* This may not be necessary */
1064 if (length
> (png_uint_32
)65535L) /* Can't hold entire string in memory */
1066 png_warning(png_ptr
, "tEXt chunk too large to fit in memory");
1067 png_ptr
->skip_length
= length
- (png_uint_32
)65535L;
1068 length
= (png_uint_32
)65535L;
1072 png_ptr
->current_text
= (png_charp
)png_malloc(png_ptr
,
1073 (png_uint_32
)(length
+1));
1074 png_ptr
->current_text
[length
] = '\0';
1075 png_ptr
->current_text_ptr
= png_ptr
->current_text
;
1076 png_ptr
->current_text_size
= (png_size_t
)length
;
1077 png_ptr
->current_text_left
= (png_size_t
)length
;
1078 png_ptr
->process_mode
= PNG_READ_tEXt_MODE
;
1082 png_push_read_tEXt(png_structp png_ptr
, png_infop info_ptr
)
1084 if (png_ptr
->buffer_size
&& png_ptr
->current_text_left
)
1086 png_size_t text_size
;
1088 if (png_ptr
->buffer_size
< png_ptr
->current_text_left
)
1089 text_size
= png_ptr
->buffer_size
;
1091 text_size
= png_ptr
->current_text_left
;
1092 png_crc_read(png_ptr
, (png_bytep
)png_ptr
->current_text_ptr
, text_size
);
1093 png_ptr
->current_text_left
-= text_size
;
1094 png_ptr
->current_text_ptr
+= text_size
;
1096 if (!(png_ptr
->current_text_left
))
1103 if (png_ptr
->buffer_size
< 4)
1105 png_push_save_buffer(png_ptr
);
1109 png_push_crc_finish(png_ptr
);
1111 #if defined(PNG_MAX_MALLOC_64K)
1112 if (png_ptr
->skip_length
)
1116 key
= png_ptr
->current_text
;
1118 for (text
= key
; *text
; text
++)
1121 if (text
!= key
+ png_ptr
->current_text_size
)
1124 text_ptr
= (png_textp
)png_malloc(png_ptr
,
1125 (png_uint_32
)png_sizeof(png_text
));
1126 text_ptr
->compression
= PNG_TEXT_COMPRESSION_NONE
;
1127 text_ptr
->key
= key
;
1128 #ifdef PNG_iTXt_SUPPORTED
1129 text_ptr
->lang
= NULL
;
1130 text_ptr
->lang_key
= NULL
;
1132 text_ptr
->text
= text
;
1134 ret
= png_set_text_2(png_ptr
, info_ptr
, text_ptr
, 1);
1136 png_free(png_ptr
, key
);
1137 png_free(png_ptr
, text_ptr
);
1138 png_ptr
->current_text
= NULL
;
1141 png_warning(png_ptr
, "Insufficient memory to store text chunk.");
1146 #if defined(PNG_READ_zTXt_SUPPORTED)
1148 png_push_handle_zTXt(png_structp png_ptr
, png_infop info_ptr
, png_uint_32
1151 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
) || (png_ptr
->mode
& PNG_HAVE_IEND
))
1153 png_error(png_ptr
, "Out of place zTXt");
1154 /* to quiet some compiler warnings */
1155 if(info_ptr
== NULL
) return;
1158 #ifdef PNG_MAX_MALLOC_64K
1159 /* We can't handle zTXt chunks > 64K, since we don't have enough space
1160 * to be able to store the uncompressed data. Actually, the threshold
1161 * is probably around 32K, but it isn't as definite as 64K is.
1163 if (length
> (png_uint_32
)65535L)
1165 png_warning(png_ptr
, "zTXt chunk too large to fit in memory");
1166 png_push_crc_skip(png_ptr
, length
);
1171 png_ptr
->current_text
= (png_charp
)png_malloc(png_ptr
,
1172 (png_uint_32
)(length
+1));
1173 png_ptr
->current_text
[length
] = '\0';
1174 png_ptr
->current_text_ptr
= png_ptr
->current_text
;
1175 png_ptr
->current_text_size
= (png_size_t
)length
;
1176 png_ptr
->current_text_left
= (png_size_t
)length
;
1177 png_ptr
->process_mode
= PNG_READ_zTXt_MODE
;
1181 png_push_read_zTXt(png_structp png_ptr
, png_infop info_ptr
)
1183 if (png_ptr
->buffer_size
&& png_ptr
->current_text_left
)
1185 png_size_t text_size
;
1187 if (png_ptr
->buffer_size
< (png_uint_32
)png_ptr
->current_text_left
)
1188 text_size
= png_ptr
->buffer_size
;
1190 text_size
= png_ptr
->current_text_left
;
1191 png_crc_read(png_ptr
, (png_bytep
)png_ptr
->current_text_ptr
, text_size
);
1192 png_ptr
->current_text_left
-= text_size
;
1193 png_ptr
->current_text_ptr
+= text_size
;
1195 if (!(png_ptr
->current_text_left
))
1201 png_size_t text_size
, key_size
;
1203 if (png_ptr
->buffer_size
< 4)
1205 png_push_save_buffer(png_ptr
);
1209 png_push_crc_finish(png_ptr
);
1211 key
= png_ptr
->current_text
;
1213 for (text
= key
; *text
; text
++)
1216 /* zTXt can't have zero text */
1217 if (text
== key
+ png_ptr
->current_text_size
)
1219 png_ptr
->current_text
= NULL
;
1220 png_free(png_ptr
, key
);
1226 if (*text
!= PNG_TEXT_COMPRESSION_zTXt
) /* check compression byte */
1228 png_ptr
->current_text
= NULL
;
1229 png_free(png_ptr
, key
);
1235 png_ptr
->zstream
.next_in
= (png_bytep
)text
;
1236 png_ptr
->zstream
.avail_in
= (uInt
)(png_ptr
->current_text_size
-
1238 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
1239 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
1241 key_size
= text
- key
;
1246 while (png_ptr
->zstream
.avail_in
)
1248 ret
= inflate(&png_ptr
->zstream
, Z_PARTIAL_FLUSH
);
1249 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
)
1251 inflateReset(&png_ptr
->zstream
);
1252 png_ptr
->zstream
.avail_in
= 0;
1253 png_ptr
->current_text
= NULL
;
1254 png_free(png_ptr
, key
);
1255 png_free(png_ptr
, text
);
1258 if (!(png_ptr
->zstream
.avail_out
) || ret
== Z_STREAM_END
)
1262 text
= (png_charp
)png_malloc(png_ptr
,
1263 (png_uint_32
)(png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
1265 png_memcpy(text
+ key_size
, png_ptr
->zbuf
,
1266 png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
);
1267 png_memcpy(text
, key
, key_size
);
1268 text_size
= key_size
+ png_ptr
->zbuf_size
-
1269 png_ptr
->zstream
.avail_out
;
1270 *(text
+ text_size
) = '\0';
1277 text
= (png_charp
)png_malloc(png_ptr
, text_size
+
1278 (png_uint_32
)(png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
1280 png_memcpy(text
, tmp
, text_size
);
1281 png_free(png_ptr
, tmp
);
1282 png_memcpy(text
+ text_size
, png_ptr
->zbuf
,
1283 png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
);
1284 text_size
+= png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
;
1285 *(text
+ text_size
) = '\0';
1287 if (ret
!= Z_STREAM_END
)
1289 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
1290 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
1298 if (ret
== Z_STREAM_END
)
1302 inflateReset(&png_ptr
->zstream
);
1303 png_ptr
->zstream
.avail_in
= 0;
1305 if (ret
!= Z_STREAM_END
)
1307 png_ptr
->current_text
= NULL
;
1308 png_free(png_ptr
, key
);
1309 png_free(png_ptr
, text
);
1313 png_ptr
->current_text
= NULL
;
1314 png_free(png_ptr
, key
);
1318 text_ptr
= (png_textp
)png_malloc(png_ptr
,
1319 (png_uint_32
)png_sizeof(png_text
));
1320 text_ptr
->compression
= PNG_TEXT_COMPRESSION_zTXt
;
1321 text_ptr
->key
= key
;
1322 #ifdef PNG_iTXt_SUPPORTED
1323 text_ptr
->lang
= NULL
;
1324 text_ptr
->lang_key
= NULL
;
1326 text_ptr
->text
= text
;
1328 ret
= png_set_text_2(png_ptr
, info_ptr
, text_ptr
, 1);
1330 png_free(png_ptr
, key
);
1331 png_free(png_ptr
, text_ptr
);
1334 png_warning(png_ptr
, "Insufficient memory to store text chunk.");
1339 #if defined(PNG_READ_iTXt_SUPPORTED)
1341 png_push_handle_iTXt(png_structp png_ptr
, png_infop info_ptr
, png_uint_32
1344 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
) || (png_ptr
->mode
& PNG_HAVE_IEND
))
1346 png_error(png_ptr
, "Out of place iTXt");
1347 /* to quiet some compiler warnings */
1348 if(info_ptr
== NULL
) return;
1351 #ifdef PNG_MAX_MALLOC_64K
1352 png_ptr
->skip_length
= 0; /* This may not be necessary */
1354 if (length
> (png_uint_32
)65535L) /* Can't hold entire string in memory */
1356 png_warning(png_ptr
, "iTXt chunk too large to fit in memory");
1357 png_ptr
->skip_length
= length
- (png_uint_32
)65535L;
1358 length
= (png_uint_32
)65535L;
1362 png_ptr
->current_text
= (png_charp
)png_malloc(png_ptr
,
1363 (png_uint_32
)(length
+1));
1364 png_ptr
->current_text
[length
] = '\0';
1365 png_ptr
->current_text_ptr
= png_ptr
->current_text
;
1366 png_ptr
->current_text_size
= (png_size_t
)length
;
1367 png_ptr
->current_text_left
= (png_size_t
)length
;
1368 png_ptr
->process_mode
= PNG_READ_iTXt_MODE
;
1372 png_push_read_iTXt(png_structp png_ptr
, png_infop info_ptr
)
1375 if (png_ptr
->buffer_size
&& png_ptr
->current_text_left
)
1377 png_size_t text_size
;
1379 if (png_ptr
->buffer_size
< png_ptr
->current_text_left
)
1380 text_size
= png_ptr
->buffer_size
;
1382 text_size
= png_ptr
->current_text_left
;
1383 png_crc_read(png_ptr
, (png_bytep
)png_ptr
->current_text_ptr
, text_size
);
1384 png_ptr
->current_text_left
-= text_size
;
1385 png_ptr
->current_text_ptr
+= text_size
;
1387 if (!(png_ptr
->current_text_left
))
1397 if (png_ptr
->buffer_size
< 4)
1399 png_push_save_buffer(png_ptr
);
1403 png_push_crc_finish(png_ptr
);
1405 #if defined(PNG_MAX_MALLOC_64K)
1406 if (png_ptr
->skip_length
)
1410 key
= png_ptr
->current_text
;
1412 for (lang
= key
; *lang
; lang
++)
1415 if (lang
!= key
+ png_ptr
->current_text_size
)
1418 comp_flag
= *lang
++;
1419 lang
++; /* skip comp_type, always zero */
1421 for (lang_key
= lang
; *lang_key
; lang_key
++)
1423 lang_key
++; /* skip NUL separator */
1425 for (text
= lang_key
; *text
; text
++)
1428 if (text
!= key
+ png_ptr
->current_text_size
)
1431 text_ptr
= (png_textp
)png_malloc(png_ptr
,
1432 (png_uint_32
)png_sizeof(png_text
));
1433 text_ptr
->compression
= comp_flag
+ 2;
1434 text_ptr
->key
= key
;
1435 text_ptr
->lang
= lang
;
1436 text_ptr
->lang_key
= lang_key
;
1437 text_ptr
->text
= text
;
1438 text_ptr
->text_length
= 0;
1439 text_ptr
->itxt_length
= png_strlen(text
);
1441 ret
= png_set_text_2(png_ptr
, info_ptr
, text_ptr
, 1);
1443 png_ptr
->current_text
= NULL
;
1445 png_free(png_ptr
, text_ptr
);
1447 png_warning(png_ptr
, "Insufficient memory to store iTXt chunk.");
1452 /* This function is called when we haven't found a handler for this
1453 * chunk. If there isn't a problem with the chunk itself (ie a bad chunk
1454 * name or a critical chunk), the chunk is (currently) silently ignored.
1457 png_push_handle_unknown(png_structp png_ptr
, png_infop info_ptr
, png_uint_32
1461 png_check_chunk_name(png_ptr
, png_ptr
->chunk_name
);
1463 if (!(png_ptr
->chunk_name
[0] & 0x20))
1465 #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
1466 if(png_handle_as_unknown(png_ptr
, png_ptr
->chunk_name
) !=
1467 PNG_HANDLE_CHUNK_ALWAYS
1468 #if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
1469 && png_ptr
->read_user_chunk_fn
== NULL
1473 png_chunk_error(png_ptr
, "unknown critical chunk");
1475 /* to quiet compiler warnings about unused info_ptr */
1476 if (info_ptr
== NULL
)
1480 #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
1481 if (png_ptr
->flags
& PNG_FLAG_KEEP_UNKNOWN_CHUNKS
)
1483 png_unknown_chunk chunk
;
1485 #ifdef PNG_MAX_MALLOC_64K
1486 if (length
> (png_uint_32
)65535L)
1488 png_warning(png_ptr
, "unknown chunk too large to fit in memory");
1489 skip
= length
- (png_uint_32
)65535L;
1490 length
= (png_uint_32
)65535L;
1494 png_strcpy((png_charp
)chunk
.name
, (png_charp
)png_ptr
->chunk_name
);
1495 chunk
.data
= (png_bytep
)png_malloc(png_ptr
, length
);
1496 png_crc_read(png_ptr
, chunk
.data
, length
);
1497 chunk
.size
= length
;
1498 #if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
1499 if(png_ptr
->read_user_chunk_fn
!= NULL
)
1501 /* callback to user unknown chunk handler */
1502 if ((*(png_ptr
->read_user_chunk_fn
)) (png_ptr
, &chunk
) <= 0)
1504 if (!(png_ptr
->chunk_name
[0] & 0x20))
1505 if(png_handle_as_unknown(png_ptr
, png_ptr
->chunk_name
) !=
1506 PNG_HANDLE_CHUNK_ALWAYS
)
1507 png_chunk_error(png_ptr
, "unknown critical chunk");
1509 png_set_unknown_chunks(png_ptr
, info_ptr
, &chunk
, 1);
1513 png_set_unknown_chunks(png_ptr
, info_ptr
, &chunk
, 1);
1514 png_free(png_ptr
, chunk
.data
);
1519 png_push_crc_skip(png_ptr
, skip
);
1523 png_push_have_info(png_structp png_ptr
, png_infop info_ptr
)
1525 if (png_ptr
->info_fn
!= NULL
)
1526 (*(png_ptr
->info_fn
))(png_ptr
, info_ptr
);
1530 png_push_have_end(png_structp png_ptr
, png_infop info_ptr
)
1532 if (png_ptr
->end_fn
!= NULL
)
1533 (*(png_ptr
->end_fn
))(png_ptr
, info_ptr
);
1537 png_push_have_row(png_structp png_ptr
, png_bytep row
)
1539 if (png_ptr
->row_fn
!= NULL
)
1540 (*(png_ptr
->row_fn
))(png_ptr
, row
, png_ptr
->row_number
,
1541 (int)png_ptr
->pass
);
1545 png_progressive_combine_row (png_structp png_ptr
,
1546 png_bytep old_row
, png_bytep new_row
)
1548 #ifdef PNG_USE_LOCAL_ARRAYS
1549 const int FARDATA png_pass_dsp_mask
[7] =
1550 {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
1552 if (new_row
!= NULL
) /* new_row must == png_ptr->row_buf here. */
1553 png_combine_row(png_ptr
, old_row
, png_pass_dsp_mask
[png_ptr
->pass
]);
1557 png_set_progressive_read_fn(png_structp png_ptr
, png_voidp progressive_ptr
,
1558 png_progressive_info_ptr info_fn
, png_progressive_row_ptr row_fn
,
1559 png_progressive_end_ptr end_fn
)
1561 png_ptr
->info_fn
= info_fn
;
1562 png_ptr
->row_fn
= row_fn
;
1563 png_ptr
->end_fn
= end_fn
;
1565 png_set_read_fn(png_ptr
, progressive_ptr
, png_push_fill_buffer
);
1569 png_get_progressive_ptr(png_structp png_ptr
)
1571 return png_ptr
->io_ptr
;
1573 #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */