2 /* pngpread.c - read a png file in push mode
4 * Last changed in libpng 1.2.17 May 15, 2007
5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1998-2007 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 if(png_ptr
== NULL
) return;
32 png_push_restore_buffer(png_ptr
, buffer
, buffer_size
);
34 while (png_ptr
->buffer_size
)
36 png_process_some_data(png_ptr
, info_ptr
);
40 /* What we do with the incoming data depends on what we were previously
41 * doing before we ran out of data...
44 png_process_some_data(png_structp png_ptr
, png_infop info_ptr
)
46 if(png_ptr
== NULL
) return;
47 switch (png_ptr
->process_mode
)
49 case PNG_READ_SIG_MODE
:
51 png_push_read_sig(png_ptr
, info_ptr
);
54 case PNG_READ_CHUNK_MODE
:
56 png_push_read_chunk(png_ptr
, info_ptr
);
59 case PNG_READ_IDAT_MODE
:
61 png_push_read_IDAT(png_ptr
);
64 #if defined(PNG_READ_tEXt_SUPPORTED)
65 case PNG_READ_tEXt_MODE
:
67 png_push_read_tEXt(png_ptr
, info_ptr
);
71 #if defined(PNG_READ_zTXt_SUPPORTED)
72 case PNG_READ_zTXt_MODE
:
74 png_push_read_zTXt(png_ptr
, info_ptr
);
78 #if defined(PNG_READ_iTXt_SUPPORTED)
79 case PNG_READ_iTXt_MODE
:
81 png_push_read_iTXt(png_ptr
, info_ptr
);
87 png_push_crc_finish(png_ptr
);
92 png_ptr
->buffer_size
= 0;
98 /* Read any remaining signature bytes from the stream and compare them with
99 * the correct PNG signature. It is possible that this routine is called
100 * with bytes already read from the signature, either because they have been
101 * checked by the calling application, or because of multiple calls to this
105 png_push_read_sig(png_structp png_ptr
, png_infop info_ptr
)
107 png_size_t num_checked
= png_ptr
->sig_bytes
,
108 num_to_check
= 8 - num_checked
;
110 if (png_ptr
->buffer_size
< num_to_check
)
112 num_to_check
= png_ptr
->buffer_size
;
115 png_push_fill_buffer(png_ptr
, &(info_ptr
->signature
[num_checked
]),
117 png_ptr
->sig_bytes
= (png_byte
)(png_ptr
->sig_bytes
+num_to_check
);
119 if (png_sig_cmp(info_ptr
->signature
, num_checked
, num_to_check
))
121 if (num_checked
< 4 &&
122 png_sig_cmp(info_ptr
->signature
, num_checked
, num_to_check
- 4))
123 png_error(png_ptr
, "Not a PNG file");
125 png_error(png_ptr
, "PNG file corrupted by ASCII conversion");
129 if (png_ptr
->sig_bytes
>= 8)
131 png_ptr
->process_mode
= PNG_READ_CHUNK_MODE
;
137 png_push_read_chunk(png_structp png_ptr
, png_infop info_ptr
)
139 #ifdef PNG_USE_LOCAL_ARRAYS
144 #if defined(PNG_READ_bKGD_SUPPORTED)
147 #if defined(PNG_READ_cHRM_SUPPORTED)
150 #if defined(PNG_READ_gAMA_SUPPORTED)
153 #if defined(PNG_READ_hIST_SUPPORTED)
156 #if defined(PNG_READ_iCCP_SUPPORTED)
159 #if defined(PNG_READ_iTXt_SUPPORTED)
162 #if defined(PNG_READ_oFFs_SUPPORTED)
165 #if defined(PNG_READ_pCAL_SUPPORTED)
168 #if defined(PNG_READ_pHYs_SUPPORTED)
171 #if defined(PNG_READ_sBIT_SUPPORTED)
174 #if defined(PNG_READ_sCAL_SUPPORTED)
177 #if defined(PNG_READ_sRGB_SUPPORTED)
180 #if defined(PNG_READ_sPLT_SUPPORTED)
183 #if defined(PNG_READ_tEXt_SUPPORTED)
186 #if defined(PNG_READ_tIME_SUPPORTED)
189 #if defined(PNG_READ_tRNS_SUPPORTED)
192 #if defined(PNG_READ_zTXt_SUPPORTED)
195 #endif /* PNG_USE_LOCAL_ARRAYS */
196 /* First we make sure we have enough data for the 4 byte chunk name
197 * and the 4 byte chunk length before proceeding with decoding the
198 * chunk data. To fully decode each of these chunks, we also make
199 * sure we have enough data in the buffer for the 4 byte CRC at the
200 * end of every chunk (except IDAT, which is handled separately).
202 if (!(png_ptr
->mode
& PNG_HAVE_CHUNK_HEADER
))
204 png_byte chunk_length
[4];
206 if (png_ptr
->buffer_size
< 8)
208 png_push_save_buffer(png_ptr
);
212 png_push_fill_buffer(png_ptr
, chunk_length
, 4);
213 png_ptr
->push_length
= png_get_uint_31(png_ptr
,chunk_length
);
214 png_reset_crc(png_ptr
);
215 png_crc_read(png_ptr
, png_ptr
->chunk_name
, 4);
216 png_ptr
->mode
|= PNG_HAVE_CHUNK_HEADER
;
219 if (!png_memcmp(png_ptr
->chunk_name
, (png_bytep
)png_IDAT
, 4))
220 if(png_ptr
->mode
& PNG_AFTER_IDAT
)
221 png_ptr
->mode
|= PNG_HAVE_CHUNK_AFTER_IDAT
;
223 if (!png_memcmp(png_ptr
->chunk_name
, png_IHDR
, 4))
225 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
227 png_push_save_buffer(png_ptr
);
230 png_handle_IHDR(png_ptr
, info_ptr
, png_ptr
->push_length
);
232 else if (!png_memcmp(png_ptr
->chunk_name
, png_IEND
, 4))
234 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
236 png_push_save_buffer(png_ptr
);
239 png_handle_IEND(png_ptr
, info_ptr
, png_ptr
->push_length
);
241 png_ptr
->process_mode
= PNG_READ_DONE_MODE
;
242 png_push_have_end(png_ptr
, info_ptr
);
244 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
245 else if (png_handle_as_unknown(png_ptr
, png_ptr
->chunk_name
))
247 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
249 png_push_save_buffer(png_ptr
);
252 if (!png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
253 png_ptr
->mode
|= PNG_HAVE_IDAT
;
254 png_handle_unknown(png_ptr
, info_ptr
, png_ptr
->push_length
);
255 if (!png_memcmp(png_ptr
->chunk_name
, png_PLTE
, 4))
256 png_ptr
->mode
|= PNG_HAVE_PLTE
;
257 else if (!png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
259 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
260 png_error(png_ptr
, "Missing IHDR before IDAT");
261 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
&&
262 !(png_ptr
->mode
& PNG_HAVE_PLTE
))
263 png_error(png_ptr
, "Missing PLTE before IDAT");
267 else if (!png_memcmp(png_ptr
->chunk_name
, png_PLTE
, 4))
269 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
271 png_push_save_buffer(png_ptr
);
274 png_handle_PLTE(png_ptr
, info_ptr
, png_ptr
->push_length
);
276 else if (!png_memcmp(png_ptr
->chunk_name
, (png_bytep
)png_IDAT
, 4))
278 /* If we reach an IDAT chunk, this means we have read all of the
279 * header chunks, and we can start reading the image (or if this
280 * is called after the image has been read - we have an error).
282 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
283 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");
288 if (png_ptr
->mode
& PNG_HAVE_IDAT
)
290 if (!(png_ptr
->mode
& PNG_HAVE_CHUNK_AFTER_IDAT
))
291 if (png_ptr
->push_length
== 0)
294 if (png_ptr
->mode
& PNG_AFTER_IDAT
)
295 png_error(png_ptr
, "Too many IDAT's found");
298 png_ptr
->idat_size
= png_ptr
->push_length
;
299 png_ptr
->mode
|= PNG_HAVE_IDAT
;
300 png_ptr
->process_mode
= PNG_READ_IDAT_MODE
;
301 png_push_have_info(png_ptr
, info_ptr
);
302 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->irowbytes
;
303 png_ptr
->zstream
.next_out
= png_ptr
->row_buf
;
306 #if defined(PNG_READ_gAMA_SUPPORTED)
307 else if (!png_memcmp(png_ptr
->chunk_name
, png_gAMA
, 4))
309 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
311 png_push_save_buffer(png_ptr
);
314 png_handle_gAMA(png_ptr
, info_ptr
, png_ptr
->push_length
);
317 #if defined(PNG_READ_sBIT_SUPPORTED)
318 else if (!png_memcmp(png_ptr
->chunk_name
, png_sBIT
, 4))
320 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
322 png_push_save_buffer(png_ptr
);
325 png_handle_sBIT(png_ptr
, info_ptr
, png_ptr
->push_length
);
328 #if defined(PNG_READ_cHRM_SUPPORTED)
329 else if (!png_memcmp(png_ptr
->chunk_name
, png_cHRM
, 4))
331 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
333 png_push_save_buffer(png_ptr
);
336 png_handle_cHRM(png_ptr
, info_ptr
, png_ptr
->push_length
);
339 #if defined(PNG_READ_sRGB_SUPPORTED)
340 else if (!png_memcmp(png_ptr
->chunk_name
, png_sRGB
, 4))
342 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
344 png_push_save_buffer(png_ptr
);
347 png_handle_sRGB(png_ptr
, info_ptr
, png_ptr
->push_length
);
350 #if defined(PNG_READ_iCCP_SUPPORTED)
351 else if (!png_memcmp(png_ptr
->chunk_name
, png_iCCP
, 4))
353 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
355 png_push_save_buffer(png_ptr
);
358 png_handle_iCCP(png_ptr
, info_ptr
, png_ptr
->push_length
);
361 #if defined(PNG_READ_sPLT_SUPPORTED)
362 else if (!png_memcmp(png_ptr
->chunk_name
, png_sPLT
, 4))
364 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
366 png_push_save_buffer(png_ptr
);
369 png_handle_sPLT(png_ptr
, info_ptr
, png_ptr
->push_length
);
372 #if defined(PNG_READ_tRNS_SUPPORTED)
373 else if (!png_memcmp(png_ptr
->chunk_name
, png_tRNS
, 4))
375 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
377 png_push_save_buffer(png_ptr
);
380 png_handle_tRNS(png_ptr
, info_ptr
, png_ptr
->push_length
);
383 #if defined(PNG_READ_bKGD_SUPPORTED)
384 else if (!png_memcmp(png_ptr
->chunk_name
, png_bKGD
, 4))
386 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
388 png_push_save_buffer(png_ptr
);
391 png_handle_bKGD(png_ptr
, info_ptr
, png_ptr
->push_length
);
394 #if defined(PNG_READ_hIST_SUPPORTED)
395 else if (!png_memcmp(png_ptr
->chunk_name
, png_hIST
, 4))
397 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
399 png_push_save_buffer(png_ptr
);
402 png_handle_hIST(png_ptr
, info_ptr
, png_ptr
->push_length
);
405 #if defined(PNG_READ_pHYs_SUPPORTED)
406 else if (!png_memcmp(png_ptr
->chunk_name
, png_pHYs
, 4))
408 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
410 png_push_save_buffer(png_ptr
);
413 png_handle_pHYs(png_ptr
, info_ptr
, png_ptr
->push_length
);
416 #if defined(PNG_READ_oFFs_SUPPORTED)
417 else if (!png_memcmp(png_ptr
->chunk_name
, png_oFFs
, 4))
419 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
421 png_push_save_buffer(png_ptr
);
424 png_handle_oFFs(png_ptr
, info_ptr
, png_ptr
->push_length
);
427 #if defined(PNG_READ_pCAL_SUPPORTED)
428 else if (!png_memcmp(png_ptr
->chunk_name
, png_pCAL
, 4))
430 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
432 png_push_save_buffer(png_ptr
);
435 png_handle_pCAL(png_ptr
, info_ptr
, png_ptr
->push_length
);
438 #if defined(PNG_READ_sCAL_SUPPORTED)
439 else if (!png_memcmp(png_ptr
->chunk_name
, png_sCAL
, 4))
441 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
443 png_push_save_buffer(png_ptr
);
446 png_handle_sCAL(png_ptr
, info_ptr
, png_ptr
->push_length
);
449 #if defined(PNG_READ_tIME_SUPPORTED)
450 else if (!png_memcmp(png_ptr
->chunk_name
, png_tIME
, 4))
452 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
454 png_push_save_buffer(png_ptr
);
457 png_handle_tIME(png_ptr
, info_ptr
, png_ptr
->push_length
);
460 #if defined(PNG_READ_tEXt_SUPPORTED)
461 else if (!png_memcmp(png_ptr
->chunk_name
, png_tEXt
, 4))
463 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
465 png_push_save_buffer(png_ptr
);
468 png_push_handle_tEXt(png_ptr
, info_ptr
, png_ptr
->push_length
);
471 #if defined(PNG_READ_zTXt_SUPPORTED)
472 else if (!png_memcmp(png_ptr
->chunk_name
, png_zTXt
, 4))
474 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
476 png_push_save_buffer(png_ptr
);
479 png_push_handle_zTXt(png_ptr
, info_ptr
, png_ptr
->push_length
);
482 #if defined(PNG_READ_iTXt_SUPPORTED)
483 else if (!png_memcmp(png_ptr
->chunk_name
, png_iTXt
, 4))
485 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
487 png_push_save_buffer(png_ptr
);
490 png_push_handle_iTXt(png_ptr
, info_ptr
, png_ptr
->push_length
);
495 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
497 png_push_save_buffer(png_ptr
);
500 png_push_handle_unknown(png_ptr
, info_ptr
, png_ptr
->push_length
);
503 png_ptr
->mode
&= ~PNG_HAVE_CHUNK_HEADER
;
507 png_push_crc_skip(png_structp png_ptr
, png_uint_32 skip
)
509 png_ptr
->process_mode
= PNG_SKIP_MODE
;
510 png_ptr
->skip_length
= skip
;
514 png_push_crc_finish(png_structp png_ptr
)
516 if (png_ptr
->skip_length
&& png_ptr
->save_buffer_size
)
518 png_size_t save_size
;
520 if (png_ptr
->skip_length
< (png_uint_32
)png_ptr
->save_buffer_size
)
521 save_size
= (png_size_t
)png_ptr
->skip_length
;
523 save_size
= png_ptr
->save_buffer_size
;
525 png_calculate_crc(png_ptr
, png_ptr
->save_buffer_ptr
, save_size
);
527 png_ptr
->skip_length
-= save_size
;
528 png_ptr
->buffer_size
-= save_size
;
529 png_ptr
->save_buffer_size
-= save_size
;
530 png_ptr
->save_buffer_ptr
+= save_size
;
532 if (png_ptr
->skip_length
&& png_ptr
->current_buffer_size
)
534 png_size_t save_size
;
536 if (png_ptr
->skip_length
< (png_uint_32
)png_ptr
->current_buffer_size
)
537 save_size
= (png_size_t
)png_ptr
->skip_length
;
539 save_size
= png_ptr
->current_buffer_size
;
541 png_calculate_crc(png_ptr
, png_ptr
->current_buffer_ptr
, save_size
);
543 png_ptr
->skip_length
-= save_size
;
544 png_ptr
->buffer_size
-= save_size
;
545 png_ptr
->current_buffer_size
-= save_size
;
546 png_ptr
->current_buffer_ptr
+= save_size
;
548 if (!png_ptr
->skip_length
)
550 if (png_ptr
->buffer_size
< 4)
552 png_push_save_buffer(png_ptr
);
556 png_crc_finish(png_ptr
, 0);
557 png_ptr
->process_mode
= PNG_READ_CHUNK_MODE
;
562 png_push_fill_buffer(png_structp png_ptr
, png_bytep buffer
, png_size_t length
)
566 if(png_ptr
== NULL
) return;
568 if (png_ptr
->save_buffer_size
)
570 png_size_t save_size
;
572 if (length
< png_ptr
->save_buffer_size
)
575 save_size
= png_ptr
->save_buffer_size
;
577 png_memcpy(ptr
, png_ptr
->save_buffer_ptr
, save_size
);
580 png_ptr
->buffer_size
-= save_size
;
581 png_ptr
->save_buffer_size
-= save_size
;
582 png_ptr
->save_buffer_ptr
+= save_size
;
584 if (length
&& png_ptr
->current_buffer_size
)
586 png_size_t save_size
;
588 if (length
< png_ptr
->current_buffer_size
)
591 save_size
= png_ptr
->current_buffer_size
;
593 png_memcpy(ptr
, png_ptr
->current_buffer_ptr
, save_size
);
594 png_ptr
->buffer_size
-= save_size
;
595 png_ptr
->current_buffer_size
-= save_size
;
596 png_ptr
->current_buffer_ptr
+= save_size
;
601 png_push_save_buffer(png_structp png_ptr
)
603 if (png_ptr
->save_buffer_size
)
605 if (png_ptr
->save_buffer_ptr
!= png_ptr
->save_buffer
)
611 istop
= png_ptr
->save_buffer_size
;
612 for (i
= 0, sp
= png_ptr
->save_buffer_ptr
, dp
= png_ptr
->save_buffer
;
613 i
< istop
; i
++, sp
++, dp
++)
619 if (png_ptr
->save_buffer_size
+ png_ptr
->current_buffer_size
>
620 png_ptr
->save_buffer_max
)
623 png_bytep old_buffer
;
625 if (png_ptr
->save_buffer_size
> PNG_SIZE_MAX
-
626 (png_ptr
->current_buffer_size
+ 256))
628 png_error(png_ptr
, "Potential overflow of save_buffer");
630 new_max
= png_ptr
->save_buffer_size
+ png_ptr
->current_buffer_size
+ 256;
631 old_buffer
= png_ptr
->save_buffer
;
632 png_ptr
->save_buffer
= (png_bytep
)png_malloc(png_ptr
,
633 (png_uint_32
)new_max
);
634 png_memcpy(png_ptr
->save_buffer
, old_buffer
, png_ptr
->save_buffer_size
);
635 png_free(png_ptr
, old_buffer
);
636 png_ptr
->save_buffer_max
= new_max
;
638 if (png_ptr
->current_buffer_size
)
640 png_memcpy(png_ptr
->save_buffer
+ png_ptr
->save_buffer_size
,
641 png_ptr
->current_buffer_ptr
, png_ptr
->current_buffer_size
);
642 png_ptr
->save_buffer_size
+= png_ptr
->current_buffer_size
;
643 png_ptr
->current_buffer_size
= 0;
645 png_ptr
->save_buffer_ptr
= png_ptr
->save_buffer
;
646 png_ptr
->buffer_size
= 0;
650 png_push_restore_buffer(png_structp png_ptr
, png_bytep buffer
,
651 png_size_t buffer_length
)
653 png_ptr
->current_buffer
= buffer
;
654 png_ptr
->current_buffer_size
= buffer_length
;
655 png_ptr
->buffer_size
= buffer_length
+ png_ptr
->save_buffer_size
;
656 png_ptr
->current_buffer_ptr
= png_ptr
->current_buffer
;
660 png_push_read_IDAT(png_structp png_ptr
)
662 #ifdef PNG_USE_LOCAL_ARRAYS
665 if (!(png_ptr
->mode
& PNG_HAVE_CHUNK_HEADER
))
667 png_byte chunk_length
[4];
669 if (png_ptr
->buffer_size
< 8)
671 png_push_save_buffer(png_ptr
);
675 png_push_fill_buffer(png_ptr
, chunk_length
, 4);
676 png_ptr
->push_length
= png_get_uint_31(png_ptr
,chunk_length
);
677 png_reset_crc(png_ptr
);
678 png_crc_read(png_ptr
, png_ptr
->chunk_name
, 4);
679 png_ptr
->mode
|= PNG_HAVE_CHUNK_HEADER
;
681 if (png_memcmp(png_ptr
->chunk_name
, (png_bytep
)png_IDAT
, 4))
683 png_ptr
->process_mode
= PNG_READ_CHUNK_MODE
;
684 if (!(png_ptr
->flags
& PNG_FLAG_ZLIB_FINISHED
))
685 png_error(png_ptr
, "Not enough compressed data");
689 png_ptr
->idat_size
= png_ptr
->push_length
;
691 if (png_ptr
->idat_size
&& png_ptr
->save_buffer_size
)
693 png_size_t save_size
;
695 if (png_ptr
->idat_size
< (png_uint_32
)png_ptr
->save_buffer_size
)
697 save_size
= (png_size_t
)png_ptr
->idat_size
;
698 /* check for overflow */
699 if((png_uint_32
)save_size
!= png_ptr
->idat_size
)
700 png_error(png_ptr
, "save_size overflowed in pngpread");
703 save_size
= png_ptr
->save_buffer_size
;
705 png_calculate_crc(png_ptr
, png_ptr
->save_buffer_ptr
, save_size
);
706 if (!(png_ptr
->flags
& PNG_FLAG_ZLIB_FINISHED
))
707 png_process_IDAT_data(png_ptr
, png_ptr
->save_buffer_ptr
, save_size
);
708 png_ptr
->idat_size
-= save_size
;
709 png_ptr
->buffer_size
-= save_size
;
710 png_ptr
->save_buffer_size
-= save_size
;
711 png_ptr
->save_buffer_ptr
+= save_size
;
713 if (png_ptr
->idat_size
&& png_ptr
->current_buffer_size
)
715 png_size_t save_size
;
717 if (png_ptr
->idat_size
< (png_uint_32
)png_ptr
->current_buffer_size
)
719 save_size
= (png_size_t
)png_ptr
->idat_size
;
720 /* check for overflow */
721 if((png_uint_32
)save_size
!= png_ptr
->idat_size
)
722 png_error(png_ptr
, "save_size overflowed in pngpread");
725 save_size
= png_ptr
->current_buffer_size
;
727 png_calculate_crc(png_ptr
, png_ptr
->current_buffer_ptr
, save_size
);
728 if (!(png_ptr
->flags
& PNG_FLAG_ZLIB_FINISHED
))
729 png_process_IDAT_data(png_ptr
, png_ptr
->current_buffer_ptr
, save_size
);
731 png_ptr
->idat_size
-= save_size
;
732 png_ptr
->buffer_size
-= save_size
;
733 png_ptr
->current_buffer_size
-= save_size
;
734 png_ptr
->current_buffer_ptr
+= save_size
;
736 if (!png_ptr
->idat_size
)
738 if (png_ptr
->buffer_size
< 4)
740 png_push_save_buffer(png_ptr
);
744 png_crc_finish(png_ptr
, 0);
745 png_ptr
->mode
&= ~PNG_HAVE_CHUNK_HEADER
;
746 png_ptr
->mode
|= PNG_AFTER_IDAT
;
751 png_process_IDAT_data(png_structp png_ptr
, png_bytep buffer
,
752 png_size_t buffer_length
)
756 if ((png_ptr
->flags
& PNG_FLAG_ZLIB_FINISHED
) && buffer_length
)
757 png_error(png_ptr
, "Extra compression data");
759 png_ptr
->zstream
.next_in
= buffer
;
760 png_ptr
->zstream
.avail_in
= (uInt
)buffer_length
;
763 ret
= inflate(&png_ptr
->zstream
, Z_PARTIAL_FLUSH
);
766 if (ret
== Z_STREAM_END
)
768 if (png_ptr
->zstream
.avail_in
)
769 png_error(png_ptr
, "Extra compressed data");
770 if (!(png_ptr
->zstream
.avail_out
))
772 png_push_process_row(png_ptr
);
775 png_ptr
->mode
|= PNG_AFTER_IDAT
;
776 png_ptr
->flags
|= PNG_FLAG_ZLIB_FINISHED
;
779 else if (ret
== Z_BUF_ERROR
)
782 png_error(png_ptr
, "Decompression Error");
784 if (!(png_ptr
->zstream
.avail_out
))
787 #if defined(PNG_READ_INTERLACING_SUPPORTED)
788 png_ptr
->interlaced
&& png_ptr
->pass
> 6) ||
789 (!png_ptr
->interlaced
&&
791 png_ptr
->row_number
== png_ptr
->num_rows
))
793 if (png_ptr
->zstream
.avail_in
)
794 png_warning(png_ptr
, "Too much data in IDAT chunks");
795 png_ptr
->flags
|= PNG_FLAG_ZLIB_FINISHED
;
798 png_push_process_row(png_ptr
);
799 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->irowbytes
;
800 png_ptr
->zstream
.next_out
= png_ptr
->row_buf
;
808 png_push_process_row(png_structp png_ptr
)
810 png_ptr
->row_info
.color_type
= png_ptr
->color_type
;
811 png_ptr
->row_info
.width
= png_ptr
->iwidth
;
812 png_ptr
->row_info
.channels
= png_ptr
->channels
;
813 png_ptr
->row_info
.bit_depth
= png_ptr
->bit_depth
;
814 png_ptr
->row_info
.pixel_depth
= png_ptr
->pixel_depth
;
816 png_ptr
->row_info
.rowbytes
= PNG_ROWBYTES(png_ptr
->row_info
.pixel_depth
,
817 png_ptr
->row_info
.width
);
819 png_read_filter_row(png_ptr
, &(png_ptr
->row_info
),
820 png_ptr
->row_buf
+ 1, png_ptr
->prev_row
+ 1,
821 (int)(png_ptr
->row_buf
[0]));
823 png_memcpy_check(png_ptr
, png_ptr
->prev_row
, png_ptr
->row_buf
,
824 png_ptr
->rowbytes
+ 1);
826 if (png_ptr
->transformations
|| (png_ptr
->flags
&PNG_FLAG_STRIP_ALPHA
))
827 png_do_read_transformations(png_ptr
);
829 #if defined(PNG_READ_INTERLACING_SUPPORTED)
830 /* blow up interlaced rows to full size */
831 if (png_ptr
->interlaced
&& (png_ptr
->transformations
& PNG_INTERLACE
))
833 if (png_ptr
->pass
< 6)
834 /* old interface (pre-1.0.9):
835 png_do_read_interlace(&(png_ptr->row_info),
836 png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
838 png_do_read_interlace(png_ptr
);
840 switch (png_ptr
->pass
)
845 for (i
= 0; i
< 8 && png_ptr
->pass
== 0; i
++)
847 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
848 png_read_push_finish_row(png_ptr
); /* updates png_ptr->pass */
850 if (png_ptr
->pass
== 2) /* pass 1 might be empty */
852 for (i
= 0; i
< 4 && png_ptr
->pass
== 2; i
++)
854 png_push_have_row(png_ptr
, png_bytep_NULL
);
855 png_read_push_finish_row(png_ptr
);
858 if (png_ptr
->pass
== 4 && png_ptr
->height
<= 4)
860 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
862 png_push_have_row(png_ptr
, png_bytep_NULL
);
863 png_read_push_finish_row(png_ptr
);
866 if (png_ptr
->pass
== 6 && png_ptr
->height
<= 4)
868 png_push_have_row(png_ptr
, png_bytep_NULL
);
869 png_read_push_finish_row(png_ptr
);
876 for (i
= 0; i
< 8 && png_ptr
->pass
== 1; i
++)
878 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
879 png_read_push_finish_row(png_ptr
);
881 if (png_ptr
->pass
== 2) /* skip top 4 generated rows */
883 for (i
= 0; i
< 4 && png_ptr
->pass
== 2; i
++)
885 png_push_have_row(png_ptr
, png_bytep_NULL
);
886 png_read_push_finish_row(png_ptr
);
894 for (i
= 0; i
< 4 && png_ptr
->pass
== 2; i
++)
896 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
897 png_read_push_finish_row(png_ptr
);
899 for (i
= 0; i
< 4 && png_ptr
->pass
== 2; i
++)
901 png_push_have_row(png_ptr
, png_bytep_NULL
);
902 png_read_push_finish_row(png_ptr
);
904 if (png_ptr
->pass
== 4) /* pass 3 might be empty */
906 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
908 png_push_have_row(png_ptr
, png_bytep_NULL
);
909 png_read_push_finish_row(png_ptr
);
917 for (i
= 0; i
< 4 && png_ptr
->pass
== 3; i
++)
919 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
920 png_read_push_finish_row(png_ptr
);
922 if (png_ptr
->pass
== 4) /* skip top two generated rows */
924 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
926 png_push_have_row(png_ptr
, png_bytep_NULL
);
927 png_read_push_finish_row(png_ptr
);
935 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
937 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
938 png_read_push_finish_row(png_ptr
);
940 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
942 png_push_have_row(png_ptr
, png_bytep_NULL
);
943 png_read_push_finish_row(png_ptr
);
945 if (png_ptr
->pass
== 6) /* pass 5 might be empty */
947 png_push_have_row(png_ptr
, png_bytep_NULL
);
948 png_read_push_finish_row(png_ptr
);
955 for (i
= 0; i
< 2 && png_ptr
->pass
== 5; i
++)
957 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
958 png_read_push_finish_row(png_ptr
);
960 if (png_ptr
->pass
== 6) /* skip top generated row */
962 png_push_have_row(png_ptr
, png_bytep_NULL
);
963 png_read_push_finish_row(png_ptr
);
969 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
970 png_read_push_finish_row(png_ptr
);
971 if (png_ptr
->pass
!= 6)
973 png_push_have_row(png_ptr
, png_bytep_NULL
);
974 png_read_push_finish_row(png_ptr
);
981 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
982 png_read_push_finish_row(png_ptr
);
987 png_read_push_finish_row(png_structp png_ptr
)
989 #ifdef PNG_USE_LOCAL_ARRAYS
990 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
992 /* start of interlace block */
993 const int FARDATA png_pass_start
[] = {0, 4, 0, 2, 0, 1, 0};
995 /* offset to next interlace block */
996 const int FARDATA png_pass_inc
[] = {8, 8, 4, 4, 2, 2, 1};
998 /* start of interlace block in the y direction */
999 const int FARDATA png_pass_ystart
[] = {0, 0, 4, 0, 2, 0, 1};
1001 /* offset to next interlace block in the y direction */
1002 const int FARDATA png_pass_yinc
[] = {8, 8, 8, 4, 4, 2, 2};
1004 /* Width of interlace block. This is not currently used - if you need
1005 * it, uncomment it here and in png.h
1006 const int FARDATA png_pass_width[] = {8, 4, 4, 2, 2, 1, 1};
1009 /* Height of interlace block. This is not currently used - if you need
1010 * it, uncomment it here and in png.h
1011 const int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
1015 png_ptr
->row_number
++;
1016 if (png_ptr
->row_number
< png_ptr
->num_rows
)
1019 if (png_ptr
->interlaced
)
1021 png_ptr
->row_number
= 0;
1022 png_memset_check(png_ptr
, png_ptr
->prev_row
, 0,
1023 png_ptr
->rowbytes
+ 1);
1027 if ((png_ptr
->pass
== 1 && png_ptr
->width
< 5) ||
1028 (png_ptr
->pass
== 3 && png_ptr
->width
< 3) ||
1029 (png_ptr
->pass
== 5 && png_ptr
->width
< 2))
1032 if (png_ptr
->pass
> 7)
1034 if (png_ptr
->pass
>= 7)
1037 png_ptr
->iwidth
= (png_ptr
->width
+
1038 png_pass_inc
[png_ptr
->pass
] - 1 -
1039 png_pass_start
[png_ptr
->pass
]) /
1040 png_pass_inc
[png_ptr
->pass
];
1042 png_ptr
->irowbytes
= PNG_ROWBYTES(png_ptr
->pixel_depth
,
1043 png_ptr
->iwidth
) + 1;
1045 if (png_ptr
->transformations
& PNG_INTERLACE
)
1048 png_ptr
->num_rows
= (png_ptr
->height
+
1049 png_pass_yinc
[png_ptr
->pass
] - 1 -
1050 png_pass_ystart
[png_ptr
->pass
]) /
1051 png_pass_yinc
[png_ptr
->pass
];
1053 } while (png_ptr
->iwidth
== 0 || png_ptr
->num_rows
== 0);
1057 #if defined(PNG_READ_tEXt_SUPPORTED)
1059 png_push_handle_tEXt(png_structp png_ptr
, png_infop info_ptr
, png_uint_32
1062 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
) || (png_ptr
->mode
& PNG_HAVE_IEND
))
1064 png_error(png_ptr
, "Out of place tEXt");
1065 /* to quiet some compiler warnings */
1066 if(info_ptr
== NULL
) return;
1069 #ifdef PNG_MAX_MALLOC_64K
1070 png_ptr
->skip_length
= 0; /* This may not be necessary */
1072 if (length
> (png_uint_32
)65535L) /* Can't hold entire string in memory */
1074 png_warning(png_ptr
, "tEXt chunk too large to fit in memory");
1075 png_ptr
->skip_length
= length
- (png_uint_32
)65535L;
1076 length
= (png_uint_32
)65535L;
1080 png_ptr
->current_text
= (png_charp
)png_malloc(png_ptr
,
1081 (png_uint_32
)(length
+1));
1082 png_ptr
->current_text
[length
] = '\0';
1083 png_ptr
->current_text_ptr
= png_ptr
->current_text
;
1084 png_ptr
->current_text_size
= (png_size_t
)length
;
1085 png_ptr
->current_text_left
= (png_size_t
)length
;
1086 png_ptr
->process_mode
= PNG_READ_tEXt_MODE
;
1090 png_push_read_tEXt(png_structp png_ptr
, png_infop info_ptr
)
1092 if (png_ptr
->buffer_size
&& png_ptr
->current_text_left
)
1094 png_size_t text_size
;
1096 if (png_ptr
->buffer_size
< png_ptr
->current_text_left
)
1097 text_size
= png_ptr
->buffer_size
;
1099 text_size
= png_ptr
->current_text_left
;
1100 png_crc_read(png_ptr
, (png_bytep
)png_ptr
->current_text_ptr
, text_size
);
1101 png_ptr
->current_text_left
-= text_size
;
1102 png_ptr
->current_text_ptr
+= text_size
;
1104 if (!(png_ptr
->current_text_left
))
1111 if (png_ptr
->buffer_size
< 4)
1113 png_push_save_buffer(png_ptr
);
1117 png_push_crc_finish(png_ptr
);
1119 #if defined(PNG_MAX_MALLOC_64K)
1120 if (png_ptr
->skip_length
)
1124 key
= png_ptr
->current_text
;
1126 for (text
= key
; *text
; text
++)
1129 if (text
!= key
+ png_ptr
->current_text_size
)
1132 text_ptr
= (png_textp
)png_malloc(png_ptr
,
1133 (png_uint_32
)png_sizeof(png_text
));
1134 text_ptr
->compression
= PNG_TEXT_COMPRESSION_NONE
;
1135 text_ptr
->key
= key
;
1136 #ifdef PNG_iTXt_SUPPORTED
1137 text_ptr
->lang
= NULL
;
1138 text_ptr
->lang_key
= NULL
;
1140 text_ptr
->text
= text
;
1142 ret
= png_set_text_2(png_ptr
, info_ptr
, text_ptr
, 1);
1144 png_free(png_ptr
, key
);
1145 png_free(png_ptr
, text_ptr
);
1146 png_ptr
->current_text
= NULL
;
1149 png_warning(png_ptr
, "Insufficient memory to store text chunk.");
1154 #if defined(PNG_READ_zTXt_SUPPORTED)
1156 png_push_handle_zTXt(png_structp png_ptr
, png_infop info_ptr
, png_uint_32
1159 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
) || (png_ptr
->mode
& PNG_HAVE_IEND
))
1161 png_error(png_ptr
, "Out of place zTXt");
1162 /* to quiet some compiler warnings */
1163 if(info_ptr
== NULL
) return;
1166 #ifdef PNG_MAX_MALLOC_64K
1167 /* We can't handle zTXt chunks > 64K, since we don't have enough space
1168 * to be able to store the uncompressed data. Actually, the threshold
1169 * is probably around 32K, but it isn't as definite as 64K is.
1171 if (length
> (png_uint_32
)65535L)
1173 png_warning(png_ptr
, "zTXt chunk too large to fit in memory");
1174 png_push_crc_skip(png_ptr
, length
);
1179 png_ptr
->current_text
= (png_charp
)png_malloc(png_ptr
,
1180 (png_uint_32
)(length
+1));
1181 png_ptr
->current_text
[length
] = '\0';
1182 png_ptr
->current_text_ptr
= png_ptr
->current_text
;
1183 png_ptr
->current_text_size
= (png_size_t
)length
;
1184 png_ptr
->current_text_left
= (png_size_t
)length
;
1185 png_ptr
->process_mode
= PNG_READ_zTXt_MODE
;
1189 png_push_read_zTXt(png_structp png_ptr
, png_infop info_ptr
)
1191 if (png_ptr
->buffer_size
&& png_ptr
->current_text_left
)
1193 png_size_t text_size
;
1195 if (png_ptr
->buffer_size
< (png_uint_32
)png_ptr
->current_text_left
)
1196 text_size
= png_ptr
->buffer_size
;
1198 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
))
1209 png_size_t text_size
, key_size
;
1211 if (png_ptr
->buffer_size
< 4)
1213 png_push_save_buffer(png_ptr
);
1217 png_push_crc_finish(png_ptr
);
1219 key
= png_ptr
->current_text
;
1221 for (text
= key
; *text
; text
++)
1224 /* zTXt can't have zero text */
1225 if (text
== key
+ png_ptr
->current_text_size
)
1227 png_ptr
->current_text
= NULL
;
1228 png_free(png_ptr
, key
);
1234 if (*text
!= PNG_TEXT_COMPRESSION_zTXt
) /* check compression byte */
1236 png_ptr
->current_text
= NULL
;
1237 png_free(png_ptr
, key
);
1243 png_ptr
->zstream
.next_in
= (png_bytep
)text
;
1244 png_ptr
->zstream
.avail_in
= (uInt
)(png_ptr
->current_text_size
-
1246 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
1247 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
1249 key_size
= text
- key
;
1254 while (png_ptr
->zstream
.avail_in
)
1256 ret
= inflate(&png_ptr
->zstream
, Z_PARTIAL_FLUSH
);
1257 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
)
1259 inflateReset(&png_ptr
->zstream
);
1260 png_ptr
->zstream
.avail_in
= 0;
1261 png_ptr
->current_text
= NULL
;
1262 png_free(png_ptr
, key
);
1263 png_free(png_ptr
, text
);
1266 if (!(png_ptr
->zstream
.avail_out
) || ret
== Z_STREAM_END
)
1270 text
= (png_charp
)png_malloc(png_ptr
,
1271 (png_uint_32
)(png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
1273 png_memcpy(text
+ key_size
, png_ptr
->zbuf
,
1274 png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
);
1275 png_memcpy(text
, key
, key_size
);
1276 text_size
= key_size
+ png_ptr
->zbuf_size
-
1277 png_ptr
->zstream
.avail_out
;
1278 *(text
+ text_size
) = '\0';
1285 text
= (png_charp
)png_malloc(png_ptr
, text_size
+
1286 (png_uint_32
)(png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
1288 png_memcpy(text
, tmp
, text_size
);
1289 png_free(png_ptr
, tmp
);
1290 png_memcpy(text
+ text_size
, png_ptr
->zbuf
,
1291 png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
);
1292 text_size
+= png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
;
1293 *(text
+ text_size
) = '\0';
1295 if (ret
!= Z_STREAM_END
)
1297 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
1298 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
1306 if (ret
== Z_STREAM_END
)
1310 inflateReset(&png_ptr
->zstream
);
1311 png_ptr
->zstream
.avail_in
= 0;
1313 if (ret
!= Z_STREAM_END
)
1315 png_ptr
->current_text
= NULL
;
1316 png_free(png_ptr
, key
);
1317 png_free(png_ptr
, text
);
1321 png_ptr
->current_text
= NULL
;
1322 png_free(png_ptr
, key
);
1326 text_ptr
= (png_textp
)png_malloc(png_ptr
,
1327 (png_uint_32
)png_sizeof(png_text
));
1328 text_ptr
->compression
= PNG_TEXT_COMPRESSION_zTXt
;
1329 text_ptr
->key
= key
;
1330 #ifdef PNG_iTXt_SUPPORTED
1331 text_ptr
->lang
= NULL
;
1332 text_ptr
->lang_key
= NULL
;
1334 text_ptr
->text
= text
;
1336 ret
= png_set_text_2(png_ptr
, info_ptr
, text_ptr
, 1);
1338 png_free(png_ptr
, key
);
1339 png_free(png_ptr
, text_ptr
);
1342 png_warning(png_ptr
, "Insufficient memory to store text chunk.");
1347 #if defined(PNG_READ_iTXt_SUPPORTED)
1349 png_push_handle_iTXt(png_structp png_ptr
, png_infop info_ptr
, png_uint_32
1352 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
) || (png_ptr
->mode
& PNG_HAVE_IEND
))
1354 png_error(png_ptr
, "Out of place iTXt");
1355 /* to quiet some compiler warnings */
1356 if(info_ptr
== NULL
) return;
1359 #ifdef PNG_MAX_MALLOC_64K
1360 png_ptr
->skip_length
= 0; /* This may not be necessary */
1362 if (length
> (png_uint_32
)65535L) /* Can't hold entire string in memory */
1364 png_warning(png_ptr
, "iTXt chunk too large to fit in memory");
1365 png_ptr
->skip_length
= length
- (png_uint_32
)65535L;
1366 length
= (png_uint_32
)65535L;
1370 png_ptr
->current_text
= (png_charp
)png_malloc(png_ptr
,
1371 (png_uint_32
)(length
+1));
1372 png_ptr
->current_text
[length
] = '\0';
1373 png_ptr
->current_text_ptr
= png_ptr
->current_text
;
1374 png_ptr
->current_text_size
= (png_size_t
)length
;
1375 png_ptr
->current_text_left
= (png_size_t
)length
;
1376 png_ptr
->process_mode
= PNG_READ_iTXt_MODE
;
1380 png_push_read_iTXt(png_structp png_ptr
, png_infop info_ptr
)
1383 if (png_ptr
->buffer_size
&& png_ptr
->current_text_left
)
1385 png_size_t text_size
;
1387 if (png_ptr
->buffer_size
< png_ptr
->current_text_left
)
1388 text_size
= png_ptr
->buffer_size
;
1390 text_size
= png_ptr
->current_text_left
;
1391 png_crc_read(png_ptr
, (png_bytep
)png_ptr
->current_text_ptr
, text_size
);
1392 png_ptr
->current_text_left
-= text_size
;
1393 png_ptr
->current_text_ptr
+= text_size
;
1395 if (!(png_ptr
->current_text_left
))
1405 if (png_ptr
->buffer_size
< 4)
1407 png_push_save_buffer(png_ptr
);
1411 png_push_crc_finish(png_ptr
);
1413 #if defined(PNG_MAX_MALLOC_64K)
1414 if (png_ptr
->skip_length
)
1418 key
= png_ptr
->current_text
;
1420 for (lang
= key
; *lang
; lang
++)
1423 if (lang
!= key
+ png_ptr
->current_text_size
)
1426 comp_flag
= *lang
++;
1427 lang
++; /* skip comp_type, always zero */
1429 for (lang_key
= lang
; *lang_key
; lang_key
++)
1431 lang_key
++; /* skip NUL separator */
1433 for (text
= lang_key
; *text
; text
++)
1436 if (text
!= key
+ png_ptr
->current_text_size
)
1439 text_ptr
= (png_textp
)png_malloc(png_ptr
,
1440 (png_uint_32
)png_sizeof(png_text
));
1441 text_ptr
->compression
= comp_flag
+ 2;
1442 text_ptr
->key
= key
;
1443 text_ptr
->lang
= lang
;
1444 text_ptr
->lang_key
= lang_key
;
1445 text_ptr
->text
= text
;
1446 text_ptr
->text_length
= 0;
1447 text_ptr
->itxt_length
= png_strlen(text
);
1449 ret
= png_set_text_2(png_ptr
, info_ptr
, text_ptr
, 1);
1451 png_ptr
->current_text
= NULL
;
1453 png_free(png_ptr
, text_ptr
);
1455 png_warning(png_ptr
, "Insufficient memory to store iTXt chunk.");
1460 /* This function is called when we haven't found a handler for this
1461 * chunk. If there isn't a problem with the chunk itself (ie a bad chunk
1462 * name or a critical chunk), the chunk is (currently) silently ignored.
1465 png_push_handle_unknown(png_structp png_ptr
, png_infop info_ptr
, png_uint_32
1469 png_check_chunk_name(png_ptr
, png_ptr
->chunk_name
);
1471 if (!(png_ptr
->chunk_name
[0] & 0x20))
1473 #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
1474 if(png_handle_as_unknown(png_ptr
, png_ptr
->chunk_name
) !=
1475 PNG_HANDLE_CHUNK_ALWAYS
1476 #if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
1477 && png_ptr
->read_user_chunk_fn
== NULL
1481 png_chunk_error(png_ptr
, "unknown critical chunk");
1483 /* to quiet compiler warnings about unused info_ptr */
1484 if (info_ptr
== NULL
)
1488 #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
1489 if (png_ptr
->flags
& PNG_FLAG_KEEP_UNKNOWN_CHUNKS
)
1491 #ifdef PNG_MAX_MALLOC_64K
1492 if (length
> (png_uint_32
)65535L)
1494 png_warning(png_ptr
, "unknown chunk too large to fit in memory");
1495 skip
= length
- (png_uint_32
)65535L;
1496 length
= (png_uint_32
)65535L;
1499 png_strcpy((png_charp
)png_ptr
->unknown_chunk
.name
,
1500 (png_charp
)png_ptr
->chunk_name
);
1501 png_ptr
->unknown_chunk
.data
= (png_bytep
)png_malloc(png_ptr
, length
);
1502 png_ptr
->unknown_chunk
.size
= (png_size_t
)length
;
1503 png_crc_read(png_ptr
, (png_bytep
)png_ptr
->unknown_chunk
.data
, length
);
1504 #if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
1505 if(png_ptr
->read_user_chunk_fn
!= NULL
)
1507 /* callback to user unknown chunk handler */
1509 ret
= (*(png_ptr
->read_user_chunk_fn
))
1510 (png_ptr
, &png_ptr
->unknown_chunk
);
1512 png_chunk_error(png_ptr
, "error in user chunk");
1515 if (!(png_ptr
->chunk_name
[0] & 0x20))
1516 if(png_handle_as_unknown(png_ptr
, png_ptr
->chunk_name
) !=
1517 PNG_HANDLE_CHUNK_ALWAYS
)
1518 png_chunk_error(png_ptr
, "unknown critical chunk");
1519 png_set_unknown_chunks(png_ptr
, info_ptr
,
1520 &png_ptr
->unknown_chunk
, 1);
1524 png_set_unknown_chunks(png_ptr
, info_ptr
, &png_ptr
->unknown_chunk
, 1);
1526 png_free(png_ptr
, png_ptr
->unknown_chunk
.data
);
1527 png_ptr
->unknown_chunk
.data
= NULL
;
1532 png_push_crc_skip(png_ptr
, skip
);
1536 png_push_have_info(png_structp png_ptr
, png_infop info_ptr
)
1538 if (png_ptr
->info_fn
!= NULL
)
1539 (*(png_ptr
->info_fn
))(png_ptr
, info_ptr
);
1543 png_push_have_end(png_structp png_ptr
, png_infop info_ptr
)
1545 if (png_ptr
->end_fn
!= NULL
)
1546 (*(png_ptr
->end_fn
))(png_ptr
, info_ptr
);
1550 png_push_have_row(png_structp png_ptr
, png_bytep row
)
1552 if (png_ptr
->row_fn
!= NULL
)
1553 (*(png_ptr
->row_fn
))(png_ptr
, row
, png_ptr
->row_number
,
1554 (int)png_ptr
->pass
);
1558 png_progressive_combine_row (png_structp png_ptr
,
1559 png_bytep old_row
, png_bytep new_row
)
1561 #ifdef PNG_USE_LOCAL_ARRAYS
1562 const int FARDATA png_pass_dsp_mask
[7] =
1563 {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
1565 if(png_ptr
== NULL
) return;
1566 if (new_row
!= NULL
) /* new_row must == png_ptr->row_buf here. */
1567 png_combine_row(png_ptr
, old_row
, png_pass_dsp_mask
[png_ptr
->pass
]);
1571 png_set_progressive_read_fn(png_structp png_ptr
, png_voidp progressive_ptr
,
1572 png_progressive_info_ptr info_fn
, png_progressive_row_ptr row_fn
,
1573 png_progressive_end_ptr end_fn
)
1575 if(png_ptr
== NULL
) return;
1576 png_ptr
->info_fn
= info_fn
;
1577 png_ptr
->row_fn
= row_fn
;
1578 png_ptr
->end_fn
= end_fn
;
1580 png_set_read_fn(png_ptr
, progressive_ptr
, png_push_fill_buffer
);
1584 png_get_progressive_ptr(png_structp png_ptr
)
1586 if(png_ptr
== NULL
) return (NULL
);
1587 return png_ptr
->io_ptr
;
1589 #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */