blob: 1120a2455b731eb83d7daad2c42cfb40e2b3bae8 [file] [log] [blame]
Guy Schalnat6d764711995-12-19 03:22:19 -06001
2/* pngpread.c - read a png file in push mode
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003 *
Glenn Randers-Pehrson5265c382009-12-22 09:09:34 -06004 * Last changed in libpng 1.4.0 [December 22, 2009]
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06005 * Copyright (c) 1998-2009 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrson3e61d792009-06-24 09:31:28 -05008 *
Glenn Randers-Pehrsonbfbf8652009-06-26 21:46:52 -05009 * This code is released under the libpng license.
Glenn Randers-Pehrsonc332bbc2009-06-25 13:43:50 -050010 * For conditions of distribution and use, see the disclaimer
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050011 * and license in png.h
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060012 */
Guy Schalnat6d764711995-12-19 03:22:19 -060013
Glenn Randers-Pehrson03f9b022009-12-04 08:40:41 -060014#define PNG_NO_PEDANTIC_WARNINGS
Guy Schalnat6d764711995-12-19 03:22:19 -060015#include "png.h"
Guy Schalnat4ee97b01996-01-16 01:51:56 -060016#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050017#include "pngpriv.h"
Guy Schalnat4ee97b01996-01-16 01:51:56 -060018
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050019/* Push model modes */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060020#define PNG_READ_SIG_MODE 0
21#define PNG_READ_CHUNK_MODE 1
22#define PNG_READ_IDAT_MODE 2
23#define PNG_SKIP_MODE 3
24#define PNG_READ_tEXt_MODE 4
25#define PNG_READ_zTXt_MODE 5
26#define PNG_READ_DONE_MODE 6
27#define PNG_READ_iTXt_MODE 7
28#define PNG_ERROR_MODE 8
29
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050030void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060031png_process_data(png_structp png_ptr, png_infop info_ptr,
Andreas Dilger47a0c421997-05-16 02:46:07 -050032 png_bytep buffer, png_size_t buffer_size)
Guy Schalnat6d764711995-12-19 03:22:19 -060033{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050034 if (png_ptr == NULL || info_ptr == NULL)
35 return;
36
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060037 png_push_restore_buffer(png_ptr, buffer, buffer_size);
Guy Schalnat6d764711995-12-19 03:22:19 -060038
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060039 while (png_ptr->buffer_size)
40 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060041 png_process_some_data(png_ptr, info_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060042 }
Guy Schalnat6d764711995-12-19 03:22:19 -060043}
44
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060045/* What we do with the incoming data depends on what we were previously
46 * doing before we ran out of data...
47 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050048void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060049png_process_some_data(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -060050{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050051 if (png_ptr == NULL)
52 return;
53
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060054 switch (png_ptr->process_mode)
55 {
56 case PNG_READ_SIG_MODE:
57 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060058 png_push_read_sig(png_ptr, info_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060059 break;
60 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050061
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060062 case PNG_READ_CHUNK_MODE:
63 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060064 png_push_read_chunk(png_ptr, info_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060065 break;
66 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050067
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060068 case PNG_READ_IDAT_MODE:
69 {
Guy Schalnate5a37791996-06-05 15:50:50 -050070 png_push_read_IDAT(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060071 break;
72 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050073
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050074#ifdef PNG_READ_tEXt_SUPPORTED
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060075 case PNG_READ_tEXt_MODE:
76 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060077 png_push_read_tEXt(png_ptr, info_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060078 break;
79 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050080
Guy Schalnat6d764711995-12-19 03:22:19 -060081#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050082#ifdef PNG_READ_zTXt_SUPPORTED
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060083 case PNG_READ_zTXt_MODE:
84 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060085 png_push_read_zTXt(png_ptr, info_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060086 break;
87 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050088
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060089#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050090#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060091 case PNG_READ_iTXt_MODE:
92 {
93 png_push_read_iTXt(png_ptr, info_ptr);
94 break;
95 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050096
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060097#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060098 case PNG_SKIP_MODE:
99 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500100 png_push_crc_finish(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600101 break;
102 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500103
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600104 default:
105 {
106 png_ptr->buffer_size = 0;
107 break;
108 }
109 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600110}
111
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600112/* Read any remaining signature bytes from the stream and compare them with
113 * the correct PNG signature. It is possible that this routine is called
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500114 * with bytes already read from the signature, either because they have been
115 * checked by the calling application, or because of multiple calls to this
116 * routine.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600117 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500118void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600119png_push_read_sig(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -0600120{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500121 png_size_t num_checked = png_ptr->sig_bytes,
122 num_to_check = 8 - num_checked;
Guy Schalnat6d764711995-12-19 03:22:19 -0600123
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600124 if (png_ptr->buffer_size < num_to_check)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600125 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600126 num_to_check = png_ptr->buffer_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600127 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600128
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600129 png_push_fill_buffer(png_ptr, &(info_ptr->signature[num_checked]),
Andreas Dilger47a0c421997-05-16 02:46:07 -0500130 num_to_check);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500131 png_ptr->sig_bytes = (png_byte)(png_ptr->sig_bytes + num_to_check);
Guy Schalnat6d764711995-12-19 03:22:19 -0600132
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600133 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600134 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600135 if (num_checked < 4 &&
136 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
137 png_error(png_ptr, "Not a PNG file");
138 else
139 png_error(png_ptr, "PNG file corrupted by ASCII conversion");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600140 }
141 else
142 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600143 if (png_ptr->sig_bytes >= 8)
144 {
145 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
146 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600147 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600148}
149
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500150void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600151png_push_read_chunk(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -0600152{
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500153 PNG_IHDR;
154 PNG_IDAT;
155 PNG_IEND;
156 PNG_PLTE;
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500157#ifdef PNG_READ_bKGD_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500158 PNG_bKGD;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600159#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500160#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500161 PNG_cHRM;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600162#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500163#ifdef PNG_READ_gAMA_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500164 PNG_gAMA;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600165#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500166#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500167 PNG_hIST;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600168#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500169#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500170 PNG_iCCP;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600171#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500172#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500173 PNG_iTXt;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600174#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500175#ifdef PNG_READ_oFFs_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500176 PNG_oFFs;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600177#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500178#ifdef PNG_READ_pCAL_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500179 PNG_pCAL;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600180#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500181#ifdef PNG_READ_pHYs_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500182 PNG_pHYs;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600183#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500184#ifdef PNG_READ_sBIT_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500185 PNG_sBIT;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600186#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500187#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500188 PNG_sCAL;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600189#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500190#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500191 PNG_sRGB;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600192#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500193#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500194 PNG_sPLT;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600195#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500196#ifdef PNG_READ_tEXt_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500197 PNG_tEXt;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600198#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500199#ifdef PNG_READ_tIME_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500200 PNG_tIME;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600201#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500202#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500203 PNG_tRNS;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600204#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500205#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500206 PNG_zTXt;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600207#endif
Glenn Randers-Pehrsona2567be2009-10-19 20:31:31 -0500208
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600209 /* First we make sure we have enough data for the 4 byte chunk name
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600210 * and the 4 byte chunk length before proceeding with decoding the
211 * chunk data. To fully decode each of these chunks, we also make
212 * sure we have enough data in the buffer for the 4 byte CRC at the
213 * end of every chunk (except IDAT, which is handled separately).
214 */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600215 if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600216 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600217 png_byte chunk_length[4];
Guy Schalnat6d764711995-12-19 03:22:19 -0600218
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600219 if (png_ptr->buffer_size < 8)
220 {
221 png_push_save_buffer(png_ptr);
222 return;
223 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600224
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600225 png_push_fill_buffer(png_ptr, chunk_length, 4);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500226 png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600227 png_reset_crc(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500228 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
Glenn Randers-Pehrsoneb580912008-07-30 14:47:09 -0500229 png_check_chunk_name(png_ptr, png_ptr->chunk_name);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600230 png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600231 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600232
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500233 if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500234 if (png_ptr->mode & PNG_AFTER_IDAT)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500235 png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
236
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600237 if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600238 {
Glenn Randers-Pehrsoneb580912008-07-30 14:47:09 -0500239 if (png_ptr->push_length != 13)
240 png_error(png_ptr, "Invalid IHDR length");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500241
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500242 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
243 {
244 png_push_save_buffer(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600245 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500246 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500247
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600248 png_handle_IHDR(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600249 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500250
Glenn Randers-Pehrsond029a752004-08-09 21:50:32 -0500251 else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
252 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500253 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
254 {
255 png_push_save_buffer(png_ptr);
Glenn Randers-Pehrsond029a752004-08-09 21:50:32 -0500256 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500257 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500258
Glenn Randers-Pehrsond029a752004-08-09 21:50:32 -0500259 png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length);
260
261 png_ptr->process_mode = PNG_READ_DONE_MODE;
262 png_push_have_end(png_ptr, info_ptr);
263 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500264
Glenn Randers-Pehrsond029a752004-08-09 21:50:32 -0500265#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
266 else if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name))
267 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500268 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
269 {
270 png_push_save_buffer(png_ptr);
Glenn Randers-Pehrsond029a752004-08-09 21:50:32 -0500271 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500272 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500273
Glenn Randers-Pehrsond029a752004-08-09 21:50:32 -0500274 if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
275 png_ptr->mode |= PNG_HAVE_IDAT;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500276
Glenn Randers-Pehrsond029a752004-08-09 21:50:32 -0500277 png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500278
Glenn Randers-Pehrsond029a752004-08-09 21:50:32 -0500279 if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
280 png_ptr->mode |= PNG_HAVE_PLTE;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500281
Glenn Randers-Pehrsond029a752004-08-09 21:50:32 -0500282 else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
283 {
284 if (!(png_ptr->mode & PNG_HAVE_IHDR))
285 png_error(png_ptr, "Missing IHDR before IDAT");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500286
Glenn Randers-Pehrsond029a752004-08-09 21:50:32 -0500287 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
288 !(png_ptr->mode & PNG_HAVE_PLTE))
289 png_error(png_ptr, "Missing PLTE before IDAT");
290 }
291 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500292
Glenn Randers-Pehrsond029a752004-08-09 21:50:32 -0500293#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600294 else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600295 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500296 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
297 {
298 png_push_save_buffer(png_ptr);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600299 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500300 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600301 png_handle_PLTE(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600302 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500303
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500304 else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600305 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600306 /* If we reach an IDAT chunk, this means we have read all of the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600307 * header chunks, and we can start reading the image (or if this
308 * is called after the image has been read - we have an error).
309 */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500310
311 if (!(png_ptr->mode & PNG_HAVE_IHDR))
312 png_error(png_ptr, "Missing IHDR before IDAT");
313
314 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
315 !(png_ptr->mode & PNG_HAVE_PLTE))
316 png_error(png_ptr, "Missing PLTE before IDAT");
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600317
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600318 if (png_ptr->mode & PNG_HAVE_IDAT)
319 {
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500320 if (!(png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500321 if (png_ptr->push_length == 0)
322 return;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600323
324 if (png_ptr->mode & PNG_AFTER_IDAT)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500325 png_benign_error(png_ptr, "Too many IDATs found");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600326 }
327
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600328 png_ptr->idat_size = png_ptr->push_length;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600329 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600330 png_ptr->process_mode = PNG_READ_IDAT_MODE;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600331 png_push_have_info(png_ptr, info_ptr);
332 png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
333 png_ptr->zstream.next_out = png_ptr->row_buf;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600334 return;
335 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500336
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500337#ifdef PNG_READ_gAMA_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600338 else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
339 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500340 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
341 {
342 png_push_save_buffer(png_ptr);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600343 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500344 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500345
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600346 png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600347 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500348
Guy Schalnat6d764711995-12-19 03:22:19 -0600349#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500350#ifdef PNG_READ_sBIT_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600351 else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600352 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500353 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
354 {
355 png_push_save_buffer(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600356 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500357 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500358
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600359 png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600360 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500361
Guy Schalnat6d764711995-12-19 03:22:19 -0600362#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500363#ifdef PNG_READ_cHRM_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600364 else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600365 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500366 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
367 {
368 png_push_save_buffer(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600369 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500370 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500371
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600372 png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600373 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500374
Guy Schalnat6d764711995-12-19 03:22:19 -0600375#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500376#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600377 else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
378 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500379 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
380 {
381 png_push_save_buffer(png_ptr);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600382 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500383 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500384
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600385 png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length);
386 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500387
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600388#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500389#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600390 else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4))
391 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500392 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
393 {
394 png_push_save_buffer(png_ptr);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600395 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500396 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500397
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600398 png_handle_iCCP(png_ptr, info_ptr, png_ptr->push_length);
399 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500400
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600401#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500402#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600403 else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4))
404 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500405 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
406 {
407 png_push_save_buffer(png_ptr);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600408 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500409 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500410
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600411 png_handle_sPLT(png_ptr, info_ptr, png_ptr->push_length);
412 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500413
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600414#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500415#ifdef PNG_READ_tRNS_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600416 else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600417 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500418 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
419 {
420 png_push_save_buffer(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600421 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500422 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500423
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600424 png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600425 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500426
Guy Schalnat6d764711995-12-19 03:22:19 -0600427#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500428#ifdef PNG_READ_bKGD_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600429 else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600430 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500431 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
432 {
433 png_push_save_buffer(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600434 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500435 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500436
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600437 png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600438 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500439
Guy Schalnat6d764711995-12-19 03:22:19 -0600440#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500441#ifdef PNG_READ_hIST_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600442 else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600443 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500444 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
445 {
446 png_push_save_buffer(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600447 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500448 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500449
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600450 png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600451 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500452
Guy Schalnat6d764711995-12-19 03:22:19 -0600453#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500454#ifdef PNG_READ_pHYs_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600455 else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600456 {
457 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
458 {
459 png_push_save_buffer(png_ptr);
460 return;
461 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500462
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600463 png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600464 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500465
Guy Schalnat6d764711995-12-19 03:22:19 -0600466#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500467#ifdef PNG_READ_oFFs_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600468 else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600469 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500470 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
471 {
472 png_push_save_buffer(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600473 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500474 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500475
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600476 png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600477 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600478#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500479
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500480#ifdef PNG_READ_pCAL_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500481 else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
482 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500483 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
484 {
485 png_push_save_buffer(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500486 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500487 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500488
Andreas Dilger47a0c421997-05-16 02:46:07 -0500489 png_handle_pCAL(png_ptr, info_ptr, png_ptr->push_length);
490 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500491
Andreas Dilger47a0c421997-05-16 02:46:07 -0500492#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500493#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600494 else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4))
495 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500496 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
497 {
498 png_push_save_buffer(png_ptr);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600499 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500500 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500501
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600502 png_handle_sCAL(png_ptr, info_ptr, png_ptr->push_length);
503 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500504
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600505#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500506#ifdef PNG_READ_tIME_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600507 else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600508 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500509 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
510 {
511 png_push_save_buffer(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600512 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500513 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500514
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600515 png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600516 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500517
Guy Schalnat6d764711995-12-19 03:22:19 -0600518#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500519#ifdef PNG_READ_tEXt_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600520 else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600521 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500522 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
523 {
524 png_push_save_buffer(png_ptr);
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500525 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500526 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500527
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600528 png_push_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600529 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500530
Guy Schalnat6d764711995-12-19 03:22:19 -0600531#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500532#ifdef PNG_READ_zTXt_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600533 else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600534 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500535 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
536 {
537 png_push_save_buffer(png_ptr);
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500538 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500539 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500540
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600541 png_push_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600542 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500543
Guy Schalnat6d764711995-12-19 03:22:19 -0600544#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500545#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600546 else if (!png_memcmp(png_ptr->chunk_name, png_iTXt, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600547 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500548 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
549 {
550 png_push_save_buffer(png_ptr);
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500551 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500552 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500553
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600554 png_push_handle_iTXt(png_ptr, info_ptr, png_ptr->push_length);
555 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500556
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600557#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600558 else
559 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500560 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
561 {
562 png_push_save_buffer(png_ptr);
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500563 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500564 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600565 png_push_handle_unknown(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600566 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600567
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600568 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
Guy Schalnat6d764711995-12-19 03:22:19 -0600569}
570
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500571void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500572png_push_crc_skip(png_structp png_ptr, png_uint_32 skip)
Guy Schalnat6d764711995-12-19 03:22:19 -0600573{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600574 png_ptr->process_mode = PNG_SKIP_MODE;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500575 png_ptr->skip_length = skip;
Guy Schalnat6d764711995-12-19 03:22:19 -0600576}
577
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500578void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500579png_push_crc_finish(png_structp png_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -0600580{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600581 if (png_ptr->skip_length && png_ptr->save_buffer_size)
582 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500583 png_size_t save_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600584
Andreas Dilger47a0c421997-05-16 02:46:07 -0500585 if (png_ptr->skip_length < (png_uint_32)png_ptr->save_buffer_size)
586 save_size = (png_size_t)png_ptr->skip_length;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600587 else
588 save_size = png_ptr->save_buffer_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600589
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600590 png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
Guy Schalnat6d764711995-12-19 03:22:19 -0600591
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600592 png_ptr->skip_length -= save_size;
593 png_ptr->buffer_size -= save_size;
594 png_ptr->save_buffer_size -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500595 png_ptr->save_buffer_ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600596 }
597 if (png_ptr->skip_length && png_ptr->current_buffer_size)
598 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500599 png_size_t save_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600600
Andreas Dilger47a0c421997-05-16 02:46:07 -0500601 if (png_ptr->skip_length < (png_uint_32)png_ptr->current_buffer_size)
602 save_size = (png_size_t)png_ptr->skip_length;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600603 else
604 save_size = png_ptr->current_buffer_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600605
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600606 png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
Guy Schalnat6d764711995-12-19 03:22:19 -0600607
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600608 png_ptr->skip_length -= save_size;
609 png_ptr->buffer_size -= save_size;
610 png_ptr->current_buffer_size -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500611 png_ptr->current_buffer_ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600612 }
613 if (!png_ptr->skip_length)
614 {
615 if (png_ptr->buffer_size < 4)
616 {
617 png_push_save_buffer(png_ptr);
618 return;
619 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600620
621 png_crc_finish(png_ptr, 0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500622 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600623 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600624}
625
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -0500626void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500627png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length)
Guy Schalnat6d764711995-12-19 03:22:19 -0600628{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600629 png_bytep ptr;
Guy Schalnat6d764711995-12-19 03:22:19 -0600630
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500631 if (png_ptr == NULL)
632 return;
633
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600634 ptr = buffer;
635 if (png_ptr->save_buffer_size)
636 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500637 png_size_t save_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600638
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600639 if (length < png_ptr->save_buffer_size)
640 save_size = length;
641 else
642 save_size = png_ptr->save_buffer_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600643
Andreas Dilger47a0c421997-05-16 02:46:07 -0500644 png_memcpy(ptr, png_ptr->save_buffer_ptr, save_size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600645 length -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500646 ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600647 png_ptr->buffer_size -= save_size;
648 png_ptr->save_buffer_size -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500649 png_ptr->save_buffer_ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600650 }
651 if (length && png_ptr->current_buffer_size)
652 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500653 png_size_t save_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600654
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600655 if (length < png_ptr->current_buffer_size)
656 save_size = length;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500657
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600658 else
659 save_size = png_ptr->current_buffer_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600660
Andreas Dilger47a0c421997-05-16 02:46:07 -0500661 png_memcpy(ptr, png_ptr->current_buffer_ptr, save_size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600662 png_ptr->buffer_size -= save_size;
663 png_ptr->current_buffer_size -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500664 png_ptr->current_buffer_ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600665 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600666}
667
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500668void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600669png_push_save_buffer(png_structp png_ptr)
670{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600671 if (png_ptr->save_buffer_size)
672 {
673 if (png_ptr->save_buffer_ptr != png_ptr->save_buffer)
674 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500675 png_size_t i, istop;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600676 png_bytep sp;
677 png_bytep dp;
Guy Schalnat6d764711995-12-19 03:22:19 -0600678
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500679 istop = png_ptr->save_buffer_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600680 for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500681 i < istop; i++, sp++, dp++)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600682 {
683 *dp = *sp;
684 }
685 }
686 }
687 if (png_ptr->save_buffer_size + png_ptr->current_buffer_size >
688 png_ptr->save_buffer_max)
689 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500690 png_size_t new_max;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600691 png_bytep old_buffer;
Guy Schalnat6d764711995-12-19 03:22:19 -0600692
Glenn Randers-Pehrsone6474622006-03-04 16:50:47 -0600693 if (png_ptr->save_buffer_size > PNG_SIZE_MAX -
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500694 (png_ptr->current_buffer_size + 256))
695 {
696 png_error(png_ptr, "Potential overflow of save_buffer");
697 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500698
Andreas Dilger47a0c421997-05-16 02:46:07 -0500699 new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600700 old_buffer = png_ptr->save_buffer;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500701 png_ptr->save_buffer = (png_bytep)png_malloc(png_ptr,
702 (png_size_t)new_max);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500703 png_memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
704 png_free(png_ptr, old_buffer);
705 png_ptr->save_buffer_max = new_max;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600706 }
707 if (png_ptr->current_buffer_size)
708 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500709 png_memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
710 png_ptr->current_buffer_ptr, png_ptr->current_buffer_size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600711 png_ptr->save_buffer_size += png_ptr->current_buffer_size;
712 png_ptr->current_buffer_size = 0;
713 }
714 png_ptr->save_buffer_ptr = png_ptr->save_buffer;
715 png_ptr->buffer_size = 0;
Guy Schalnat6d764711995-12-19 03:22:19 -0600716}
717
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500718void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600719png_push_restore_buffer(png_structp png_ptr, png_bytep buffer,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500720 png_size_t buffer_length)
Guy Schalnat6d764711995-12-19 03:22:19 -0600721{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600722 png_ptr->current_buffer = buffer;
723 png_ptr->current_buffer_size = buffer_length;
724 png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size;
725 png_ptr->current_buffer_ptr = png_ptr->current_buffer;
Guy Schalnat6d764711995-12-19 03:22:19 -0600726}
727
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500728void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -0500729png_push_read_IDAT(png_structp png_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -0600730{
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500731 PNG_IDAT;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600732 if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600733 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600734 png_byte chunk_length[4];
Guy Schalnat6d764711995-12-19 03:22:19 -0600735
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600736 if (png_ptr->buffer_size < 8)
737 {
738 png_push_save_buffer(png_ptr);
739 return;
740 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600741
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600742 png_push_fill_buffer(png_ptr, chunk_length, 4);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500743 png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600744 png_reset_crc(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500745 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600746 png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600747
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500748 if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600749 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500750 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500751 if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600752 png_error(png_ptr, "Not enough compressed data");
753 return;
754 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600755
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600756 png_ptr->idat_size = png_ptr->push_length;
757 }
758 if (png_ptr->idat_size && png_ptr->save_buffer_size)
759 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500760 png_size_t save_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600761
Andreas Dilger47a0c421997-05-16 02:46:07 -0500762 if (png_ptr->idat_size < (png_uint_32)png_ptr->save_buffer_size)
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600763 {
764 save_size = (png_size_t)png_ptr->idat_size;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500765
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500766 /* Check for overflow */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500767 if ((png_uint_32)save_size != png_ptr->idat_size)
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600768 png_error(png_ptr, "save_size overflowed in pngpread");
769 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600770 else
771 save_size = png_ptr->save_buffer_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600772
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600773 png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500774
Glenn Randers-Pehrson859665d2002-08-06 18:06:11 -0500775 if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
776 png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500777
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600778 png_ptr->idat_size -= save_size;
779 png_ptr->buffer_size -= save_size;
780 png_ptr->save_buffer_size -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500781 png_ptr->save_buffer_ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600782 }
783 if (png_ptr->idat_size && png_ptr->current_buffer_size)
784 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500785 png_size_t save_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600786
Andreas Dilger47a0c421997-05-16 02:46:07 -0500787 if (png_ptr->idat_size < (png_uint_32)png_ptr->current_buffer_size)
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600788 {
789 save_size = (png_size_t)png_ptr->idat_size;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500790
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500791 /* Check for overflow */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500792 if ((png_uint_32)save_size != png_ptr->idat_size)
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600793 png_error(png_ptr, "save_size overflowed in pngpread");
794 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600795 else
796 save_size = png_ptr->current_buffer_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600797
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600798 png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
Glenn Randers-Pehrson859665d2002-08-06 18:06:11 -0500799 if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
800 png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size);
Guy Schalnat6d764711995-12-19 03:22:19 -0600801
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600802 png_ptr->idat_size -= save_size;
803 png_ptr->buffer_size -= save_size;
804 png_ptr->current_buffer_size -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500805 png_ptr->current_buffer_ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600806 }
807 if (!png_ptr->idat_size)
808 {
809 if (png_ptr->buffer_size < 4)
810 {
811 png_push_save_buffer(png_ptr);
812 return;
813 }
814
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600815 png_crc_finish(png_ptr, 0);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600816 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
Glenn Randers-Pehrson859665d2002-08-06 18:06:11 -0500817 png_ptr->mode |= PNG_AFTER_IDAT;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600818 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600819}
820
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500821void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600822png_process_IDAT_data(png_structp png_ptr, png_bytep buffer,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500823 png_size_t buffer_length)
Guy Schalnat6d764711995-12-19 03:22:19 -0600824{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600825 int ret;
Guy Schalnat6d764711995-12-19 03:22:19 -0600826
Guy Schalnate5a37791996-06-05 15:50:50 -0500827 if ((png_ptr->flags & PNG_FLAG_ZLIB_FINISHED) && buffer_length)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500828 png_benign_error(png_ptr, "Extra compression data");
Guy Schalnat6d764711995-12-19 03:22:19 -0600829
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600830 png_ptr->zstream.next_in = buffer;
831 png_ptr->zstream.avail_in = (uInt)buffer_length;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500832 for (;;)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600833 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600834 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500835 if (ret != Z_OK)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600836 {
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500837 if (ret == Z_STREAM_END)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600838 {
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500839 if (png_ptr->zstream.avail_in)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500840 png_benign_error(png_ptr, "Extra compressed data");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500841
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500842 if (!(png_ptr->zstream.avail_out))
843 {
844 png_push_process_row(png_ptr);
845 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500846
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500847 png_ptr->mode |= PNG_AFTER_IDAT;
848 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
849 break;
850 }
851 else if (ret == Z_BUF_ERROR)
852 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500853
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500854 else
855 png_error(png_ptr, "Decompression Error");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600856 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600857 if (!(png_ptr->zstream.avail_out))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600858 {
Glenn Randers-Pehrsond020e9d2002-06-28 09:34:00 -0500859 if ((
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500860#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrsond020e9d2002-06-28 09:34:00 -0500861 png_ptr->interlaced && png_ptr->pass > 6) ||
862 (!png_ptr->interlaced &&
863#endif
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500864 png_ptr->row_number == png_ptr->num_rows))
Glenn Randers-Pehrson859665d2002-08-06 18:06:11 -0500865 {
Glenn Randers-Pehrson8a7df002002-08-15 22:02:57 -0500866 if (png_ptr->zstream.avail_in)
867 png_warning(png_ptr, "Too much data in IDAT chunks");
Glenn Randers-Pehrson859665d2002-08-06 18:06:11 -0500868 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
869 break;
870 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600871 png_push_process_row(png_ptr);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600872 png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
873 png_ptr->zstream.next_out = png_ptr->row_buf;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600874 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500875
Andreas Dilger47a0c421997-05-16 02:46:07 -0500876 else
877 break;
878 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600879}
880
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500881void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600882png_push_process_row(png_structp png_ptr)
883{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600884 png_ptr->row_info.color_type = png_ptr->color_type;
Guy Schalnat6d764711995-12-19 03:22:19 -0600885 png_ptr->row_info.width = png_ptr->iwidth;
886 png_ptr->row_info.channels = png_ptr->channels;
887 png_ptr->row_info.bit_depth = png_ptr->bit_depth;
888 png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600889
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500890 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
891 png_ptr->row_info.width);
Guy Schalnat6d764711995-12-19 03:22:19 -0600892
Guy Schalnate5a37791996-06-05 15:50:50 -0500893 png_read_filter_row(png_ptr, &(png_ptr->row_info),
894 png_ptr->row_buf + 1, png_ptr->prev_row + 1,
895 (int)(png_ptr->row_buf[0]));
Guy Schalnat6d764711995-12-19 03:22:19 -0600896
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500897 png_memcpy(png_ptr->prev_row, png_ptr->row_buf, png_ptr->rowbytes + 1);
Guy Schalnat6d764711995-12-19 03:22:19 -0600898
Glenn Randers-Pehrson73b029f2004-11-26 17:28:09 -0600899 if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA))
Guy Schalnat6d764711995-12-19 03:22:19 -0600900 png_do_read_transformations(png_ptr);
901
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500902#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500903 /* Blow up interlaced rows to full size */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500904 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600905 {
906 if (png_ptr->pass < 6)
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -0600907/* old interface (pre-1.0.9):
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600908 png_do_read_interlace(&(png_ptr->row_info),
Andreas Dilger47a0c421997-05-16 02:46:07 -0500909 png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -0600910 */
911 png_do_read_interlace(png_ptr);
Guy Schalnat6d764711995-12-19 03:22:19 -0600912
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -0600913 switch (png_ptr->pass)
914 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600915 case 0:
916 {
917 int i;
918 for (i = 0; i < 8 && png_ptr->pass == 0; i++)
919 {
920 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500921 png_read_push_finish_row(png_ptr); /* Updates png_ptr->pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600922 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500923
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500924 if (png_ptr->pass == 2) /* Pass 1 might be empty */
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -0600925 {
926 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
927 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500928 png_push_have_row(png_ptr, NULL);
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -0600929 png_read_push_finish_row(png_ptr);
930 }
931 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500932
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500933 if (png_ptr->pass == 4 && png_ptr->height <= 4)
934 {
935 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
936 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500937 png_push_have_row(png_ptr, NULL);
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500938 png_read_push_finish_row(png_ptr);
939 }
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500940 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500941
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500942 if (png_ptr->pass == 6 && png_ptr->height <= 4)
943 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500944 png_push_have_row(png_ptr, NULL);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500945 png_read_push_finish_row(png_ptr);
946 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500947
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600948 break;
949 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500950
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600951 case 1:
952 {
953 int i;
954 for (i = 0; i < 8 && png_ptr->pass == 1; i++)
955 {
956 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
957 png_read_push_finish_row(png_ptr);
958 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500959
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500960 if (png_ptr->pass == 2) /* Skip top 4 generated rows */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600961 {
962 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
963 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500964 png_push_have_row(png_ptr, NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600965 png_read_push_finish_row(png_ptr);
966 }
967 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500968
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600969 break;
970 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500971
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600972 case 2:
973 {
974 int i;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500975
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600976 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
977 {
978 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
979 png_read_push_finish_row(png_ptr);
980 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500981
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600982 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
983 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500984 png_push_have_row(png_ptr, NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600985 png_read_push_finish_row(png_ptr);
986 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500987
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500988 if (png_ptr->pass == 4) /* Pass 3 might be empty */
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -0600989 {
990 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
991 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500992 png_push_have_row(png_ptr, NULL);
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -0600993 png_read_push_finish_row(png_ptr);
994 }
995 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500996
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600997 break;
998 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500999
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001000 case 3:
1001 {
1002 int i;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001003
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001004 for (i = 0; i < 4 && png_ptr->pass == 3; i++)
1005 {
1006 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1007 png_read_push_finish_row(png_ptr);
1008 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001009
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001010 if (png_ptr->pass == 4) /* Skip top two generated rows */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001011 {
1012 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1013 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001014 png_push_have_row(png_ptr, NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001015 png_read_push_finish_row(png_ptr);
1016 }
1017 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001018
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001019 break;
1020 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001021
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001022 case 4:
1023 {
1024 int i;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001025
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001026 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1027 {
1028 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1029 png_read_push_finish_row(png_ptr);
1030 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001031
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001032 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1033 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001034 png_push_have_row(png_ptr, NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001035 png_read_push_finish_row(png_ptr);
1036 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001037
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001038 if (png_ptr->pass == 6) /* Pass 5 might be empty */
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -06001039 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001040 png_push_have_row(png_ptr, NULL);
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -06001041 png_read_push_finish_row(png_ptr);
1042 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001043
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001044 break;
1045 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001046
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001047 case 5:
1048 {
1049 int i;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001050
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001051 for (i = 0; i < 2 && png_ptr->pass == 5; i++)
1052 {
1053 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1054 png_read_push_finish_row(png_ptr);
1055 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001056
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001057 if (png_ptr->pass == 6) /* Skip top generated row */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001058 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001059 png_push_have_row(png_ptr, NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001060 png_read_push_finish_row(png_ptr);
1061 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001062
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001063 break;
1064 }
1065 case 6:
1066 {
1067 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1068 png_read_push_finish_row(png_ptr);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001069
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001070 if (png_ptr->pass != 6)
1071 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001072
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001073 png_push_have_row(png_ptr, NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001074 png_read_push_finish_row(png_ptr);
1075 }
1076 }
1077 }
1078 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001079#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001080 {
1081 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1082 png_read_push_finish_row(png_ptr);
1083 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001084}
1085
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001086void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001087png_read_push_finish_row(png_structp png_ptr)
1088{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001089 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001090
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001091 /* Start of interlace block */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001092 PNG_CONST int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001093
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001094 /* Offset to next interlace block */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001095 PNG_CONST int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001096
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001097 /* Start of interlace block in the y direction */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001098 PNG_CONST int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001099
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001100 /* Offset to next interlace block in the y direction */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001101 PNG_CONST int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001102
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001103 /* Height of interlace block. This is not currently used - if you need
1104 * it, uncomment it here and in png.h
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001105 PNG_CONST int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001106 */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001107
Guy Schalnat6d764711995-12-19 03:22:19 -06001108 png_ptr->row_number++;
1109 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001110 return;
Guy Schalnat6d764711995-12-19 03:22:19 -06001111
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001112#ifdef PNG_READ_INTERLACING_SUPPORTED
Guy Schalnat6d764711995-12-19 03:22:19 -06001113 if (png_ptr->interlaced)
1114 {
1115 png_ptr->row_number = 0;
Glenn Randers-Pehrsona31c74f2009-05-18 15:52:01 -05001116 png_memset(png_ptr->prev_row, 0,
1117 png_ptr->rowbytes + 1);
Guy Schalnat6d764711995-12-19 03:22:19 -06001118 do
1119 {
1120 png_ptr->pass++;
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -06001121 if ((png_ptr->pass == 1 && png_ptr->width < 5) ||
1122 (png_ptr->pass == 3 && png_ptr->width < 3) ||
1123 (png_ptr->pass == 5 && png_ptr->width < 2))
1124 png_ptr->pass++;
1125
Glenn Randers-Pehrson859665d2002-08-06 18:06:11 -05001126 if (png_ptr->pass > 7)
1127 png_ptr->pass--;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001128
Guy Schalnat6d764711995-12-19 03:22:19 -06001129 if (png_ptr->pass >= 7)
1130 break;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001131
Guy Schalnat6d764711995-12-19 03:22:19 -06001132 png_ptr->iwidth = (png_ptr->width +
1133 png_pass_inc[png_ptr->pass] - 1 -
1134 png_pass_start[png_ptr->pass]) /
1135 png_pass_inc[png_ptr->pass];
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001136
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001137 png_ptr->irowbytes = PNG_ROWBYTES(png_ptr->pixel_depth,
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -05001138 png_ptr->iwidth) + 1;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001139
Guy Schalnat6d764711995-12-19 03:22:19 -06001140 if (png_ptr->transformations & PNG_INTERLACE)
1141 break;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001142
1143 png_ptr->num_rows = (png_ptr->height +
1144 png_pass_yinc[png_ptr->pass] - 1 -
1145 png_pass_ystart[png_ptr->pass]) /
1146 png_pass_yinc[png_ptr->pass];
1147
1148 } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0);
Guy Schalnat6d764711995-12-19 03:22:19 -06001149 }
Glenn Randers-Pehrsona31c74f2009-05-18 15:52:01 -05001150#endif /* PNG_READ_INTERLACING_SUPPORTED */
Guy Schalnat6d764711995-12-19 03:22:19 -06001151}
1152
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001153#ifdef PNG_READ_tEXt_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001154void /* PRIVATE */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001155png_push_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
1156 length)
Guy Schalnat6d764711995-12-19 03:22:19 -06001157{
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001158 if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001159 {
1160 png_error(png_ptr, "Out of place tEXt");
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001161 info_ptr = info_ptr; /* To quiet some compiler warnings */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001162 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001163
Andreas Dilger47a0c421997-05-16 02:46:07 -05001164#ifdef PNG_MAX_MALLOC_64K
1165 png_ptr->skip_length = 0; /* This may not be necessary */
1166
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001167 if (length > (png_uint_32)65535L) /* Can't hold entire string in memory */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001168 {
1169 png_warning(png_ptr, "tEXt chunk too large to fit in memory");
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001170 png_ptr->skip_length = length - (png_uint_32)65535L;
1171 length = (png_uint_32)65535L;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001172 }
1173#endif
1174
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001175 png_ptr->current_text = (png_charp)png_malloc(png_ptr,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001176 (png_size_t)(length + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001177 png_ptr->current_text[length] = '\0';
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001178 png_ptr->current_text_ptr = png_ptr->current_text;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001179 png_ptr->current_text_size = (png_size_t)length;
1180 png_ptr->current_text_left = (png_size_t)length;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001181 png_ptr->process_mode = PNG_READ_tEXt_MODE;
Guy Schalnat6d764711995-12-19 03:22:19 -06001182}
1183
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001184void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001185png_push_read_tEXt(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -06001186{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001187 if (png_ptr->buffer_size && png_ptr->current_text_left)
1188 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001189 png_size_t text_size;
Guy Schalnat6d764711995-12-19 03:22:19 -06001190
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001191 if (png_ptr->buffer_size < png_ptr->current_text_left)
1192 text_size = png_ptr->buffer_size;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001193
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001194 else
1195 text_size = png_ptr->current_text_left;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001196
Andreas Dilger47a0c421997-05-16 02:46:07 -05001197 png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001198 png_ptr->current_text_left -= text_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001199 png_ptr->current_text_ptr += text_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001200 }
1201 if (!(png_ptr->current_text_left))
1202 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001203 png_textp text_ptr;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001204 png_charp text;
1205 png_charp key;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001206 int ret;
Guy Schalnat6d764711995-12-19 03:22:19 -06001207
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001208 if (png_ptr->buffer_size < 4)
1209 {
1210 png_push_save_buffer(png_ptr);
1211 return;
1212 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001213
Andreas Dilger47a0c421997-05-16 02:46:07 -05001214 png_push_crc_finish(png_ptr);
1215
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001216#ifdef PNG_MAX_MALLOC_64K
Andreas Dilger47a0c421997-05-16 02:46:07 -05001217 if (png_ptr->skip_length)
1218 return;
1219#endif
Guy Schalnat6d764711995-12-19 03:22:19 -06001220
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001221 key = png_ptr->current_text;
Guy Schalnat6d764711995-12-19 03:22:19 -06001222
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001223 for (text = key; *text; text++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001224 /* Empty loop */ ;
Guy Schalnat6d764711995-12-19 03:22:19 -06001225
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001226 if (text < key + png_ptr->current_text_size)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001227 text++;
1228
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001229 text_ptr = (png_textp)png_malloc(png_ptr,
1230 png_sizeof(png_text));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001231 text_ptr->compression = PNG_TEXT_COMPRESSION_NONE;
1232 text_ptr->key = key;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001233#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001234 text_ptr->lang = NULL;
1235 text_ptr->lang_key = NULL;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001236#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001237 text_ptr->text = text;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001238
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001239 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001240
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001241 png_free(png_ptr, key);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001242 png_free(png_ptr, text_ptr);
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001243 png_ptr->current_text = NULL;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001244
1245 if (ret)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001246 png_warning(png_ptr, "Insufficient memory to store text chunk");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001247 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001248}
1249#endif
1250
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001251#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001252void /* PRIVATE */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001253png_push_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
1254 length)
Guy Schalnat6d764711995-12-19 03:22:19 -06001255{
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001256 if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001257 {
1258 png_error(png_ptr, "Out of place zTXt");
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001259 info_ptr = info_ptr; /* To quiet some compiler warnings */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001260 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001261
Andreas Dilger47a0c421997-05-16 02:46:07 -05001262#ifdef PNG_MAX_MALLOC_64K
1263 /* We can't handle zTXt chunks > 64K, since we don't have enough space
1264 * to be able to store the uncompressed data. Actually, the threshold
1265 * is probably around 32K, but it isn't as definite as 64K is.
1266 */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001267 if (length > (png_uint_32)65535L)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001268 {
1269 png_warning(png_ptr, "zTXt chunk too large to fit in memory");
1270 png_push_crc_skip(png_ptr, length);
1271 return;
1272 }
1273#endif
1274
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001275 png_ptr->current_text = (png_charp)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001276 (png_size_t)(length + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001277 png_ptr->current_text[length] = '\0';
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001278 png_ptr->current_text_ptr = png_ptr->current_text;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001279 png_ptr->current_text_size = (png_size_t)length;
1280 png_ptr->current_text_left = (png_size_t)length;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001281 png_ptr->process_mode = PNG_READ_zTXt_MODE;
Guy Schalnat6d764711995-12-19 03:22:19 -06001282}
1283
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001284void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001285png_push_read_zTXt(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -06001286{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001287 if (png_ptr->buffer_size && png_ptr->current_text_left)
1288 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001289 png_size_t text_size;
Guy Schalnat6d764711995-12-19 03:22:19 -06001290
Andreas Dilger47a0c421997-05-16 02:46:07 -05001291 if (png_ptr->buffer_size < (png_uint_32)png_ptr->current_text_left)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001292 text_size = png_ptr->buffer_size;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001293
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001294 else
1295 text_size = png_ptr->current_text_left;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001296
Andreas Dilger47a0c421997-05-16 02:46:07 -05001297 png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001298 png_ptr->current_text_left -= text_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001299 png_ptr->current_text_ptr += text_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001300 }
1301 if (!(png_ptr->current_text_left))
1302 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001303 png_textp text_ptr;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001304 png_charp text;
1305 png_charp key;
1306 int ret;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001307 png_size_t text_size, key_size;
Guy Schalnat6d764711995-12-19 03:22:19 -06001308
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001309 if (png_ptr->buffer_size < 4)
1310 {
1311 png_push_save_buffer(png_ptr);
1312 return;
1313 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001314
Andreas Dilger47a0c421997-05-16 02:46:07 -05001315 png_push_crc_finish(png_ptr);
Guy Schalnat6d764711995-12-19 03:22:19 -06001316
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001317 key = png_ptr->current_text;
Guy Schalnat6d764711995-12-19 03:22:19 -06001318
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001319 for (text = key; *text; text++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001320 /* Empty loop */ ;
Guy Schalnat6d764711995-12-19 03:22:19 -06001321
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001322 /* zTXt can't have zero text */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001323 if (text >= key + png_ptr->current_text_size)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001324 {
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001325 png_ptr->current_text = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001326 png_free(png_ptr, key);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001327 return;
1328 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001329
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001330 text++;
Guy Schalnat6d764711995-12-19 03:22:19 -06001331
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001332 if (*text != PNG_TEXT_COMPRESSION_zTXt) /* Check compression byte */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001333 {
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001334 png_ptr->current_text = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001335 png_free(png_ptr, key);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001336 return;
1337 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001338
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001339 text++;
Guy Schalnat6d764711995-12-19 03:22:19 -06001340
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001341 png_ptr->zstream.next_in = (png_bytep )text;
1342 png_ptr->zstream.avail_in = (uInt)(png_ptr->current_text_size -
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001343 (text - key));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001344 png_ptr->zstream.next_out = png_ptr->zbuf;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001345 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnat6d764711995-12-19 03:22:19 -06001346
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001347 key_size = text - key;
1348 text_size = 0;
1349 text = NULL;
1350 ret = Z_STREAM_END;
Guy Schalnat6d764711995-12-19 03:22:19 -06001351
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001352 while (png_ptr->zstream.avail_in)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001353 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001354 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001355 if (ret != Z_OK && ret != Z_STREAM_END)
1356 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001357 inflateReset(&png_ptr->zstream);
1358 png_ptr->zstream.avail_in = 0;
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001359 png_ptr->current_text = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001360 png_free(png_ptr, key);
1361 png_free(png_ptr, text);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001362 return;
1363 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001364 if (!(png_ptr->zstream.avail_out) || ret == Z_STREAM_END)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001365 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001366 if (text == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001367 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001368 text = (png_charp)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001369 (png_ptr->zbuf_size
1370 - png_ptr->zstream.avail_out + key_size + 1));
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001371
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001372 png_memcpy(text + key_size, png_ptr->zbuf,
1373 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001374
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001375 png_memcpy(text, key, key_size);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001376
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001377 text_size = key_size + png_ptr->zbuf_size -
1378 png_ptr->zstream.avail_out;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001379
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001380 *(text + text_size) = '\0';
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001381 }
1382 else
1383 {
1384 png_charp tmp;
Guy Schalnat6d764711995-12-19 03:22:19 -06001385
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001386 tmp = text;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001387 text = (png_charp)png_malloc(png_ptr, text_size +
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001388 (png_ptr->zbuf_size
Glenn Randers-Pehrsona5fa5c92008-09-06 07:06:22 -05001389 - png_ptr->zstream.avail_out + 1));
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001390
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001391 png_memcpy(text, tmp, text_size);
1392 png_free(png_ptr, tmp);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001393
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001394 png_memcpy(text + text_size, png_ptr->zbuf,
1395 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001396
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001397 text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out;
1398 *(text + text_size) = '\0';
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001399 }
1400 if (ret != Z_STREAM_END)
1401 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001402 png_ptr->zstream.next_out = png_ptr->zbuf;
1403 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001404 }
1405 }
1406 else
1407 {
1408 break;
1409 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001410
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001411 if (ret == Z_STREAM_END)
1412 break;
1413 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001414
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001415 inflateReset(&png_ptr->zstream);
1416 png_ptr->zstream.avail_in = 0;
Guy Schalnat6d764711995-12-19 03:22:19 -06001417
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001418 if (ret != Z_STREAM_END)
1419 {
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001420 png_ptr->current_text = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001421 png_free(png_ptr, key);
1422 png_free(png_ptr, text);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001423 return;
1424 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001425
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001426 png_ptr->current_text = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001427 png_free(png_ptr, key);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001428 key = text;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001429 text += key_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001430
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001431 text_ptr = (png_textp)png_malloc(png_ptr,
1432 png_sizeof(png_text));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001433 text_ptr->compression = PNG_TEXT_COMPRESSION_zTXt;
1434 text_ptr->key = key;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001435#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001436 text_ptr->lang = NULL;
1437 text_ptr->lang_key = NULL;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001438#endif
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001439 text_ptr->text = text;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001440
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001441 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001442
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001443 png_free(png_ptr, key);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001444 png_free(png_ptr, text_ptr);
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001445
1446 if (ret)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001447 png_warning(png_ptr, "Insufficient memory to store text chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001448 }
1449}
1450#endif
1451
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001452#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001453void /* PRIVATE */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001454png_push_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
1455 length)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001456{
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001457 if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001458 {
1459 png_error(png_ptr, "Out of place iTXt");
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001460 info_ptr = info_ptr; /* To quiet some compiler warnings */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001461 }
1462
1463#ifdef PNG_MAX_MALLOC_64K
1464 png_ptr->skip_length = 0; /* This may not be necessary */
1465
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001466 if (length > (png_uint_32)65535L) /* Can't hold entire string in memory */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001467 {
1468 png_warning(png_ptr, "iTXt chunk too large to fit in memory");
1469 png_ptr->skip_length = length - (png_uint_32)65535L;
1470 length = (png_uint_32)65535L;
1471 }
1472#endif
1473
1474 png_ptr->current_text = (png_charp)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001475 (png_size_t)(length + 1));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001476 png_ptr->current_text[length] = '\0';
1477 png_ptr->current_text_ptr = png_ptr->current_text;
1478 png_ptr->current_text_size = (png_size_t)length;
1479 png_ptr->current_text_left = (png_size_t)length;
1480 png_ptr->process_mode = PNG_READ_iTXt_MODE;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001481}
1482
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001483void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001484png_push_read_iTXt(png_structp png_ptr, png_infop info_ptr)
1485{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001486
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001487 if (png_ptr->buffer_size && png_ptr->current_text_left)
1488 {
1489 png_size_t text_size;
1490
1491 if (png_ptr->buffer_size < png_ptr->current_text_left)
1492 text_size = png_ptr->buffer_size;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001493
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001494 else
1495 text_size = png_ptr->current_text_left;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001496
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001497 png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
1498 png_ptr->current_text_left -= text_size;
1499 png_ptr->current_text_ptr += text_size;
1500 }
1501 if (!(png_ptr->current_text_left))
1502 {
1503 png_textp text_ptr;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001504 png_charp key;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001505 int comp_flag;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001506 png_charp lang;
1507 png_charp lang_key;
1508 png_charp text;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001509 int ret;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001510
1511 if (png_ptr->buffer_size < 4)
1512 {
1513 png_push_save_buffer(png_ptr);
1514 return;
1515 }
1516
1517 png_push_crc_finish(png_ptr);
1518
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001519#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001520 if (png_ptr->skip_length)
1521 return;
1522#endif
1523
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001524 key = png_ptr->current_text;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001525
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001526 for (lang = key; *lang; lang++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001527 /* Empty loop */ ;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001528
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001529 if (lang < key + png_ptr->current_text_size - 3)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001530 lang++;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001531
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001532 comp_flag = *lang++;
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001533 lang++; /* Skip comp_type, always zero */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001534
1535 for (lang_key = lang; *lang_key; lang_key++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001536 /* Empty loop */ ;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001537
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001538 lang_key++; /* Skip NUL separator */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001539
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001540 text=lang_key;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001541
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001542 if (lang_key < key + png_ptr->current_text_size - 1)
1543 {
1544 for (; *text; text++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001545 /* Empty loop */ ;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001546 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001547
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001548 if (text < key + png_ptr->current_text_size)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001549 text++;
1550
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001551 text_ptr = (png_textp)png_malloc(png_ptr,
1552 png_sizeof(png_text));
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001553
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001554 text_ptr->compression = comp_flag + 2;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001555 text_ptr->key = key;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001556 text_ptr->lang = lang;
1557 text_ptr->lang_key = lang_key;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001558 text_ptr->text = text;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001559 text_ptr->text_length = 0;
1560 text_ptr->itxt_length = png_strlen(text);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001561
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001562 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001563
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001564 png_ptr->current_text = NULL;
1565
Andreas Dilger47a0c421997-05-16 02:46:07 -05001566 png_free(png_ptr, text_ptr);
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001567 if (ret)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001568 png_warning(png_ptr, "Insufficient memory to store iTXt chunk");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001569 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001570}
1571#endif
1572
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001573/* This function is called when we haven't found a handler for this
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001574 * chunk. If there isn't a problem with the chunk itself (ie a bad chunk
1575 * name or a critical chunk), the chunk is (currently) silently ignored.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001576 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001577void /* PRIVATE */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001578png_push_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32
1579 length)
Guy Schalnat6d764711995-12-19 03:22:19 -06001580{
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -05001581 png_uint_32 skip = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001582
1583 if (!(png_ptr->chunk_name[0] & 0x20))
1584 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001585#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001586 if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
1587 PNG_HANDLE_CHUNK_ALWAYS
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001588#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001589 && png_ptr->read_user_chunk_fn == NULL
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001590#endif
1591 )
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001592#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001593 png_chunk_error(png_ptr, "unknown critical chunk");
1594
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001595 info_ptr = info_ptr; /* To quiet some compiler warnings */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001596 }
1597
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001598#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001599 if (png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS)
1600 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001601#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001602 if (length > (png_uint_32)65535L)
1603 {
1604 png_warning(png_ptr, "unknown chunk too large to fit in memory");
1605 skip = length - (png_uint_32)65535L;
1606 length = (png_uint_32)65535L;
1607 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001608#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001609 png_memcpy((png_charp)png_ptr->unknown_chunk.name,
1610 (png_charp)png_ptr->chunk_name,
1611 png_sizeof(png_ptr->unknown_chunk.name));
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -05001612 png_ptr->unknown_chunk.name[png_sizeof(png_ptr->unknown_chunk.name) - 1]
1613 = '\0';
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001614
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001615 png_ptr->unknown_chunk.size = (png_size_t)length;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001616
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001617 if (length == 0)
1618 png_ptr->unknown_chunk.data = NULL;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001619
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001620 else
1621 {
1622 png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr,
1623 (png_size_t)length);
1624 png_crc_read(png_ptr, (png_bytep)png_ptr->unknown_chunk.data, length);
1625 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001626
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001627#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001628 if (png_ptr->read_user_chunk_fn != NULL)
1629 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001630 /* Callback to user unknown chunk handler */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001631 int ret;
1632 ret = (*(png_ptr->read_user_chunk_fn))
1633 (png_ptr, &png_ptr->unknown_chunk);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001634
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001635 if (ret < 0)
1636 png_chunk_error(png_ptr, "error in user chunk");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001637
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001638 if (ret == 0)
1639 {
1640 if (!(png_ptr->chunk_name[0] & 0x20))
1641 if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
1642 PNG_HANDLE_CHUNK_ALWAYS)
1643 png_chunk_error(png_ptr, "unknown critical chunk");
1644 png_set_unknown_chunks(png_ptr, info_ptr,
1645 &png_ptr->unknown_chunk, 1);
1646 }
1647 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001648
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001649 else
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001650#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001651 png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1);
1652 png_free(png_ptr, png_ptr->unknown_chunk.data);
1653 png_ptr->unknown_chunk.data = NULL;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001654 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001655
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001656 else
1657#endif
1658 skip=length;
1659 png_push_crc_skip(png_ptr, skip);
Guy Schalnat6d764711995-12-19 03:22:19 -06001660}
1661
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001662void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001663png_push_have_info(png_structp png_ptr, png_infop info_ptr)
1664{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001665 if (png_ptr->info_fn != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001666 (*(png_ptr->info_fn))(png_ptr, info_ptr);
1667}
1668
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001669void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001670png_push_have_end(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -06001671{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001672 if (png_ptr->end_fn != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001673 (*(png_ptr->end_fn))(png_ptr, info_ptr);
Guy Schalnat6d764711995-12-19 03:22:19 -06001674}
1675
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001676void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001677png_push_have_row(png_structp png_ptr, png_bytep row)
1678{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001679 if (png_ptr->row_fn != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001680 (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number,
1681 (int)png_ptr->pass);
Guy Schalnat6d764711995-12-19 03:22:19 -06001682}
1683
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001684void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001685png_progressive_combine_row (png_structp png_ptr,
1686 png_bytep old_row, png_bytep new_row)
Guy Schalnat6d764711995-12-19 03:22:19 -06001687{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001688 PNG_CONST int FARDATA png_pass_dsp_mask[7] =
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001689 {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
Glenn Randers-Pehrson86312502009-08-31 14:17:23 -05001690
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001691 if (png_ptr == NULL)
1692 return;
1693
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -05001694 if (new_row != NULL) /* new_row must == png_ptr->row_buf here. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001695 png_combine_row(png_ptr, old_row, png_pass_dsp_mask[png_ptr->pass]);
Guy Schalnat6d764711995-12-19 03:22:19 -06001696}
1697
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001698void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001699png_set_progressive_read_fn(png_structp png_ptr, png_voidp progressive_ptr,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001700 png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
1701 png_progressive_end_ptr end_fn)
Guy Schalnat6d764711995-12-19 03:22:19 -06001702{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001703 if (png_ptr == NULL)
1704 return;
1705
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001706 png_ptr->info_fn = info_fn;
1707 png_ptr->row_fn = row_fn;
Guy Schalnat6d764711995-12-19 03:22:19 -06001708 png_ptr->end_fn = end_fn;
Guy Schalnate5a37791996-06-05 15:50:50 -05001709
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -05001710 png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer);
Guy Schalnat6d764711995-12-19 03:22:19 -06001711}
1712
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001713png_voidp PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001714png_get_progressive_ptr(png_structp png_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -06001715{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001716 if (png_ptr == NULL)
1717 return (NULL);
1718
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001719 return png_ptr->io_ptr;
Guy Schalnat6d764711995-12-19 03:22:19 -06001720}
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001721#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */