Support unrar64.dll
[xy_vsfilter.git] / src / libpng / pngpread.c
blob5c73923fefb590a8df72544c000923450ea3b719
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.)
9 */
11 #define PNG_INTERNAL
12 #include "png.h"
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
26 void PNGAPI
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)
31 return;
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...
44 void /* PRIVATE */
45 png_process_some_data(png_structp png_ptr, png_infop info_ptr)
47 if (png_ptr == NULL)
48 return;
50 switch (png_ptr->process_mode)
52 case PNG_READ_SIG_MODE:
54 png_push_read_sig(png_ptr, info_ptr);
55 break;
58 case PNG_READ_CHUNK_MODE:
60 png_push_read_chunk(png_ptr, info_ptr);
61 break;
64 case PNG_READ_IDAT_MODE:
66 png_push_read_IDAT(png_ptr);
67 break;
70 #if defined(PNG_READ_tEXt_SUPPORTED)
71 case PNG_READ_tEXt_MODE:
73 png_push_read_tEXt(png_ptr, info_ptr);
74 break;
77 #endif
78 #if defined(PNG_READ_zTXt_SUPPORTED)
79 case PNG_READ_zTXt_MODE:
81 png_push_read_zTXt(png_ptr, info_ptr);
82 break;
85 #endif
86 #if defined(PNG_READ_iTXt_SUPPORTED)
87 case PNG_READ_iTXt_MODE:
89 png_push_read_iTXt(png_ptr, info_ptr);
90 break;
93 #endif
94 case PNG_SKIP_MODE:
96 png_push_crc_finish(png_ptr);
97 break;
100 default:
102 png_ptr->buffer_size = 0;
103 break;
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
112 * routine.
114 void /* PRIVATE */
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]),
126 num_to_check);
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");
134 else
135 png_error(png_ptr, "PNG file corrupted by ASCII conversion");
137 else
139 if (png_ptr->sig_bytes >= 8)
141 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
146 void /* PRIVATE */
147 png_push_read_chunk(png_structp png_ptr, png_infop info_ptr)
149 #ifdef PNG_USE_LOCAL_ARRAYS
150 PNG_CONST PNG_IHDR;
151 PNG_CONST PNG_IDAT;
152 PNG_CONST PNG_IEND;
153 PNG_CONST PNG_PLTE;
154 #if defined(PNG_READ_bKGD_SUPPORTED)
155 PNG_CONST PNG_bKGD;
156 #endif
157 #if defined(PNG_READ_cHRM_SUPPORTED)
158 PNG_CONST PNG_cHRM;
159 #endif
160 #if defined(PNG_READ_gAMA_SUPPORTED)
161 PNG_CONST PNG_gAMA;
162 #endif
163 #if defined(PNG_READ_hIST_SUPPORTED)
164 PNG_CONST PNG_hIST;
165 #endif
166 #if defined(PNG_READ_iCCP_SUPPORTED)
167 PNG_CONST PNG_iCCP;
168 #endif
169 #if defined(PNG_READ_iTXt_SUPPORTED)
170 PNG_CONST PNG_iTXt;
171 #endif
172 #if defined(PNG_READ_oFFs_SUPPORTED)
173 PNG_CONST PNG_oFFs;
174 #endif
175 #if defined(PNG_READ_pCAL_SUPPORTED)
176 PNG_CONST PNG_pCAL;
177 #endif
178 #if defined(PNG_READ_pHYs_SUPPORTED)
179 PNG_CONST PNG_pHYs;
180 #endif
181 #if defined(PNG_READ_sBIT_SUPPORTED)
182 PNG_CONST PNG_sBIT;
183 #endif
184 #if defined(PNG_READ_sCAL_SUPPORTED)
185 PNG_CONST PNG_sCAL;
186 #endif
187 #if defined(PNG_READ_sRGB_SUPPORTED)
188 PNG_CONST PNG_sRGB;
189 #endif
190 #if defined(PNG_READ_sPLT_SUPPORTED)
191 PNG_CONST PNG_sPLT;
192 #endif
193 #if defined(PNG_READ_tEXt_SUPPORTED)
194 PNG_CONST PNG_tEXt;
195 #endif
196 #if defined(PNG_READ_tIME_SUPPORTED)
197 PNG_CONST PNG_tIME;
198 #endif
199 #if defined(PNG_READ_tRNS_SUPPORTED)
200 PNG_CONST PNG_tRNS;
201 #endif
202 #if defined(PNG_READ_zTXt_SUPPORTED)
203 PNG_CONST PNG_zTXt;
204 #endif
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);
219 return;
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);
242 return;
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);
253 return;
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);
268 return;
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");
290 #endif
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);
296 return;
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)
319 return;
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;
331 return;
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);
340 return;
343 png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length);
346 #endif
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);
353 return;
356 png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length);
359 #endif
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);
366 return;
369 png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length);
372 #endif
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);
379 return;
382 png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length);
385 #endif
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);
392 return;
395 png_handle_iCCP(png_ptr, info_ptr, png_ptr->push_length);
398 #endif
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);
405 return;
408 png_handle_sPLT(png_ptr, info_ptr, png_ptr->push_length);
411 #endif
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);
418 return;
421 png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length);
424 #endif
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);
431 return;
434 png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length);
437 #endif
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);
444 return;
447 png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length);
450 #endif
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);
457 return;
460 png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length);
463 #endif
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);
470 return;
473 png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length);
475 #endif
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);
483 return;
486 png_handle_pCAL(png_ptr, info_ptr, png_ptr->push_length);
489 #endif
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);
496 return;
499 png_handle_sCAL(png_ptr, info_ptr, png_ptr->push_length);
502 #endif
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);
509 return;
512 png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length);
515 #endif
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);
522 return;
525 png_push_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length);
528 #endif
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);
535 return;
538 png_push_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length);
541 #endif
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);
548 return;
551 png_push_handle_iTXt(png_ptr, info_ptr, png_ptr->push_length);
554 #endif
555 else
557 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
559 png_push_save_buffer(png_ptr);
560 return;
562 png_push_handle_unknown(png_ptr, info_ptr, png_ptr->push_length);
565 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
568 void /* PRIVATE */
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;
575 void /* PRIVATE */
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;
584 else
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;
600 else
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);
615 return;
618 png_crc_finish(png_ptr, 0);
619 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
623 void PNGAPI
624 png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length)
626 png_bytep ptr;
628 if (png_ptr == NULL)
629 return;
631 ptr = buffer;
632 if (png_ptr->save_buffer_size)
634 png_size_t save_size;
636 if (length < png_ptr->save_buffer_size)
637 save_size = length;
638 else
639 save_size = png_ptr->save_buffer_size;
641 png_memcpy(ptr, png_ptr->save_buffer_ptr, save_size);
642 length -= save_size;
643 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)
653 save_size = length;
655 else
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;
665 void /* PRIVATE */
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)
672 png_size_t i, istop;
673 png_bytep sp;
674 png_bytep dp;
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++)
680 *dp = *sp;
684 if (png_ptr->save_buffer_size + png_ptr->current_buffer_size >
685 png_ptr->save_buffer_max)
687 png_size_t new_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;
715 void /* PRIVATE */
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;
725 void /* PRIVATE */
726 png_push_read_IDAT(png_structp png_ptr)
728 #ifdef PNG_USE_LOCAL_ARRAYS
729 PNG_CONST PNG_IDAT;
730 #endif
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);
738 return;
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");
752 return;
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");
769 else
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");
794 else
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);
811 return;
814 png_crc_finish(png_ptr, 0);
815 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
816 png_ptr->mode |= PNG_AFTER_IDAT;
820 void /* PRIVATE */
821 png_process_IDAT_data(png_structp png_ptr, png_bytep buffer,
822 png_size_t buffer_length)
824 int ret;
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;
831 for (;;)
833 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
834 if (ret != Z_OK)
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;
848 break;
850 else if (ret == Z_BUF_ERROR)
851 break;
853 else
854 png_error(png_ptr, "Decompression Error");
856 if (!(png_ptr->zstream.avail_out))
858 if ((
859 #if defined(PNG_READ_INTERLACING_SUPPORTED)
860 png_ptr->interlaced && png_ptr->pass > 6) ||
861 (!png_ptr->interlaced &&
862 #endif
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;
868 break;
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;
875 else
876 break;
880 void /* PRIVATE */
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)
915 case 0:
917 int i;
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);
948 break;
951 case 1:
953 int i;
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);
969 break;
972 case 2:
974 int i;
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);
997 break;
1000 case 3:
1002 int i;
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);
1019 break;
1022 case 4:
1024 int i;
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);
1044 break;
1047 case 5:
1049 int i;
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);
1063 break;
1065 case 6:
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)
1071 break;
1073 png_push_have_row(png_ptr, png_bytep_NULL);
1074 png_read_push_finish_row(png_ptr);
1078 else
1079 #endif
1081 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1082 png_read_push_finish_row(png_ptr);
1086 void /* PRIVATE */
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};
1108 #endif
1110 png_ptr->row_number++;
1111 if (png_ptr->row_number < png_ptr->num_rows)
1112 return;
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);
1122 png_ptr->pass++;
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))
1126 png_ptr->pass++;
1128 if (png_ptr->pass > 7)
1129 png_ptr->pass--;
1131 if (png_ptr->pass >= 7)
1132 break;
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)
1143 break;
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)
1156 void /* PRIVATE */
1157 png_push_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
1158 length)
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;
1175 #endif
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;
1186 void /* PRIVATE */
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;
1196 else
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))
1205 png_textp text_ptr;
1206 png_charp text;
1207 png_charp key;
1208 int ret;
1210 if (png_ptr->buffer_size < 4)
1212 png_push_save_buffer(png_ptr);
1213 return;
1216 png_push_crc_finish(png_ptr);
1218 #if defined(PNG_MAX_MALLOC_64K)
1219 if (png_ptr->skip_length)
1220 return;
1221 #endif
1223 key = png_ptr->current_text;
1225 for (text = key; *text; text++)
1226 /* Empty loop */ ;
1228 if (text < key + png_ptr->current_text_size)
1229 text++;
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;
1238 #endif
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;
1247 if (ret)
1248 png_warning(png_ptr, "Insufficient memory to store text chunk.");
1251 #endif
1253 #if defined(PNG_READ_zTXt_SUPPORTED)
1254 void /* PRIVATE */
1255 png_push_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
1256 length)
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);
1273 return;
1275 #endif
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;
1286 void /* PRIVATE */
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;
1296 else
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))
1305 png_textp text_ptr;
1306 png_charp text;
1307 png_charp key;
1308 int ret;
1309 png_size_t text_size, key_size;
1311 if (png_ptr->buffer_size < 4)
1313 png_push_save_buffer(png_ptr);
1314 return;
1317 png_push_crc_finish(png_ptr);
1319 key = png_ptr->current_text;
1321 for (text = key; *text; text++)
1322 /* Empty loop */ ;
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);
1329 return;
1332 text++;
1334 if (*text != PNG_TEXT_COMPRESSION_zTXt) /* Check compression byte */
1336 png_ptr->current_text = NULL;
1337 png_free(png_ptr, key);
1338 return;
1341 text++;
1343 png_ptr->zstream.next_in = (png_bytep )text;
1344 png_ptr->zstream.avail_in = (uInt)(png_ptr->current_text_size -
1345 (text - key));
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;
1350 text_size = 0;
1351 text = NULL;
1352 ret = Z_STREAM_END;
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);
1364 return;
1366 if (!(png_ptr->zstream.avail_out) || ret == Z_STREAM_END)
1368 if (text == NULL)
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';
1384 else
1386 png_charp tmp;
1388 tmp = text;
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;
1408 else
1410 break;
1413 if (ret == Z_STREAM_END)
1414 break;
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);
1425 return;
1428 png_ptr->current_text = NULL;
1429 png_free(png_ptr, key);
1430 key = text;
1431 text += key_size;
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;
1440 #endif
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);
1448 if (ret)
1449 png_warning(png_ptr, "Insufficient memory to store text chunk.");
1452 #endif
1454 #if defined(PNG_READ_iTXt_SUPPORTED)
1455 void /* PRIVATE */
1456 png_push_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
1457 length)
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;
1474 #endif
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;
1485 void /* PRIVATE */
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;
1496 else
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))
1505 png_textp text_ptr;
1506 png_charp key;
1507 int comp_flag;
1508 png_charp lang;
1509 png_charp lang_key;
1510 png_charp text;
1511 int ret;
1513 if (png_ptr->buffer_size < 4)
1515 png_push_save_buffer(png_ptr);
1516 return;
1519 png_push_crc_finish(png_ptr);
1521 #if defined(PNG_MAX_MALLOC_64K)
1522 if (png_ptr->skip_length)
1523 return;
1524 #endif
1526 key = png_ptr->current_text;
1528 for (lang = key; *lang; lang++)
1529 /* Empty loop */ ;
1531 if (lang < key + png_ptr->current_text_size - 3)
1532 lang++;
1534 comp_flag = *lang++;
1535 lang++; /* Skip comp_type, always zero */
1537 for (lang_key = lang; *lang_key; lang_key++)
1538 /* Empty loop */ ;
1540 lang_key++; /* Skip NUL separator */
1542 text=lang_key;
1544 if (lang_key < key + png_ptr->current_text_size - 1)
1546 for (; *text; text++)
1547 /* Empty loop */ ;
1550 if (text < key + png_ptr->current_text_size)
1551 text++;
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);
1569 if (ret)
1570 png_warning(png_ptr, "Insufficient memory to store iTXt chunk.");
1573 #endif
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.
1579 void /* PRIVATE */
1580 png_push_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32
1581 length)
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
1592 #endif
1594 #endif
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;
1610 #endif
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]
1615 = '\0';
1617 png_ptr->unknown_chunk.size = (png_size_t)length;
1619 if (length == 0)
1620 png_ptr->unknown_chunk.data = NULL;
1622 else
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 */
1633 int ret;
1634 ret = (*(png_ptr->read_user_chunk_fn))
1635 (png_ptr, &png_ptr->unknown_chunk);
1637 if (ret < 0)
1638 png_chunk_error(png_ptr, "error in user chunk");
1640 if (ret == 0)
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);
1651 else
1652 #endif
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;
1658 else
1659 #endif
1660 skip=length;
1661 png_push_crc_skip(png_ptr, skip);
1664 void /* PRIVATE */
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);
1671 void /* PRIVATE */
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);
1678 void /* PRIVATE */
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);
1686 void PNGAPI
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};
1693 #endif
1694 if (png_ptr == NULL)
1695 return;
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]);
1701 void PNGAPI
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)
1707 return;
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);
1716 png_voidp PNGAPI
1717 png_get_progressive_ptr(png_structp png_ptr)
1719 if (png_ptr == NULL)
1720 return (NULL);
1722 return png_ptr->io_ptr;
1724 #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */