blob: 67455c3405005271b5da8a69501dd7fa5724e3ca [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-Pehrsonf0eda4e2010-10-15 15:01:57 -05004 * Last changed in libpng 1.5.0 [(PENDING RELEASE)]
Glenn Randers-Pehrsone69b55d2010-01-01 10:29:06 -06005 * Copyright (c) 1998-2010 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-Pehrson145f5c82008-07-10 09:13:13 -050014#include "pngpriv.h"
Guy Schalnat4ee97b01996-01-16 01:51:56 -060015
Glenn Randers-Pehrsonc3cd22b2010-03-08 21:10:25 -060016#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
17
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050018/* Push model modes */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060019#define PNG_READ_SIG_MODE 0
20#define PNG_READ_CHUNK_MODE 1
21#define PNG_READ_IDAT_MODE 2
22#define PNG_SKIP_MODE 3
23#define PNG_READ_tEXt_MODE 4
24#define PNG_READ_zTXt_MODE 5
25#define PNG_READ_DONE_MODE 6
26#define PNG_READ_iTXt_MODE 7
27#define PNG_ERROR_MODE 8
28
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050029void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060030png_process_data(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -060031 png_bytep buffer, png_size_t buffer_size)
Guy Schalnat6d764711995-12-19 03:22:19 -060032{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050033 if (png_ptr == NULL || info_ptr == NULL)
34 return;
35
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060036 png_push_restore_buffer(png_ptr, buffer, buffer_size);
Guy Schalnat6d764711995-12-19 03:22:19 -060037
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060038 while (png_ptr->buffer_size)
39 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060040 png_process_some_data(png_ptr, info_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060041 }
Guy Schalnat6d764711995-12-19 03:22:19 -060042}
43
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060044/* What we do with the incoming data depends on what we were previously
45 * doing before we ran out of data...
46 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050047void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060048png_process_some_data(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -060049{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050050 if (png_ptr == NULL)
51 return;
52
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060053 switch (png_ptr->process_mode)
54 {
55 case PNG_READ_SIG_MODE:
56 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060057 png_push_read_sig(png_ptr, info_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060058 break;
59 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050060
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060061 case PNG_READ_CHUNK_MODE:
62 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060063 png_push_read_chunk(png_ptr, info_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060064 break;
65 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050066
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060067 case PNG_READ_IDAT_MODE:
68 {
Guy Schalnate5a37791996-06-05 15:50:50 -050069 png_push_read_IDAT(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060070 break;
71 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050072
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050073#ifdef PNG_READ_tEXt_SUPPORTED
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060074 case PNG_READ_tEXt_MODE:
75 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060076 png_push_read_tEXt(png_ptr, info_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060077 break;
78 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050079
Guy Schalnat6d764711995-12-19 03:22:19 -060080#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050081#ifdef PNG_READ_zTXt_SUPPORTED
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060082 case PNG_READ_zTXt_MODE:
83 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060084 png_push_read_zTXt(png_ptr, info_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060085 break;
86 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050087
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060088#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050089#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060090 case PNG_READ_iTXt_MODE:
91 {
92 png_push_read_iTXt(png_ptr, info_ptr);
93 break;
94 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050095
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060096#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060097 case PNG_SKIP_MODE:
98 {
Andreas Dilger47a0c421997-05-16 02:46:07 -050099 png_push_crc_finish(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600100 break;
101 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500102
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600103 default:
104 {
105 png_ptr->buffer_size = 0;
106 break;
107 }
108 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600109}
110
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600111/* Read any remaining signature bytes from the stream and compare them with
112 * the correct PNG signature. It is possible that this routine is called
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500113 * with bytes already read from the signature, either because they have been
114 * checked by the calling application, or because of multiple calls to this
115 * routine.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600116 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500117void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600118png_push_read_sig(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -0600119{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500120 png_size_t num_checked = png_ptr->sig_bytes,
121 num_to_check = 8 - num_checked;
Guy Schalnat6d764711995-12-19 03:22:19 -0600122
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600123 if (png_ptr->buffer_size < num_to_check)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600124 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600125 num_to_check = png_ptr->buffer_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600126 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600127
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600128 png_push_fill_buffer(png_ptr, &(info_ptr->signature[num_checked]),
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600129 num_to_check);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500130 png_ptr->sig_bytes = (png_byte)(png_ptr->sig_bytes + num_to_check);
Guy Schalnat6d764711995-12-19 03:22:19 -0600131
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600132 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600133 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600134 if (num_checked < 4 &&
135 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
136 png_error(png_ptr, "Not a PNG file");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500137
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600138 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-Pehrson262d0ff2010-03-03 07:06:54 -0600234 if (png_ptr->mode & PNG_AFTER_IDAT)
235 png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500236
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 &&
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600288 !(png_ptr->mode & PNG_HAVE_PLTE))
Glenn Randers-Pehrsond029a752004-08-09 21:50:32 -0500289 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);
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600332 png_ptr->zstream.avail_out =
333 (uInt) PNG_ROWBYTES(png_ptr->pixel_depth,
334 png_ptr->iwidth) + 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600335 png_ptr->zstream.next_out = png_ptr->row_buf;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600336 return;
337 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500338
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500339#ifdef PNG_READ_gAMA_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600340 else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
341 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500342 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
343 {
344 png_push_save_buffer(png_ptr);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600345 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500346 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500347
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600348 png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600349 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500350
Guy Schalnat6d764711995-12-19 03:22:19 -0600351#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500352#ifdef PNG_READ_sBIT_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600353 else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600354 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500355 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
356 {
357 png_push_save_buffer(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600358 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500359 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500360
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600361 png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600362 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500363
Guy Schalnat6d764711995-12-19 03:22:19 -0600364#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500365#ifdef PNG_READ_cHRM_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600366 else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600367 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500368 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
369 {
370 png_push_save_buffer(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600371 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500372 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500373
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600374 png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600375 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500376
Guy Schalnat6d764711995-12-19 03:22:19 -0600377#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500378#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600379 else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
380 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500381 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
382 {
383 png_push_save_buffer(png_ptr);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600384 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500385 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500386
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600387 png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length);
388 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500389
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600390#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500391#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600392 else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4))
393 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500394 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
395 {
396 png_push_save_buffer(png_ptr);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600397 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500398 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500399
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600400 png_handle_iCCP(png_ptr, info_ptr, png_ptr->push_length);
401 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500402
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600403#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500404#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600405 else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4))
406 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500407 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
408 {
409 png_push_save_buffer(png_ptr);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600410 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500411 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500412
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600413 png_handle_sPLT(png_ptr, info_ptr, png_ptr->push_length);
414 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500415
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600416#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500417#ifdef PNG_READ_tRNS_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600418 else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600419 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500420 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
421 {
422 png_push_save_buffer(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600423 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500424 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500425
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600426 png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600427 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500428
Guy Schalnat6d764711995-12-19 03:22:19 -0600429#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500430#ifdef PNG_READ_bKGD_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600431 else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600432 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500433 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
434 {
435 png_push_save_buffer(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600436 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500437 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500438
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600439 png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600440 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500441
Guy Schalnat6d764711995-12-19 03:22:19 -0600442#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500443#ifdef PNG_READ_hIST_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600444 else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600445 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500446 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
447 {
448 png_push_save_buffer(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600449 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500450 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500451
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600452 png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600453 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500454
Guy Schalnat6d764711995-12-19 03:22:19 -0600455#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500456#ifdef PNG_READ_pHYs_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600457 else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600458 {
459 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
460 {
461 png_push_save_buffer(png_ptr);
462 return;
463 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500464
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600465 png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600466 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500467
Guy Schalnat6d764711995-12-19 03:22:19 -0600468#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500469#ifdef PNG_READ_oFFs_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600470 else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600471 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500472 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
473 {
474 png_push_save_buffer(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600475 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500476 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500477
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600478 png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600479 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600480#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500481
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500482#ifdef PNG_READ_pCAL_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500483 else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
484 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500485 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
486 {
487 png_push_save_buffer(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500488 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500489 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500490
Andreas Dilger47a0c421997-05-16 02:46:07 -0500491 png_handle_pCAL(png_ptr, info_ptr, png_ptr->push_length);
492 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500493
Andreas Dilger47a0c421997-05-16 02:46:07 -0500494#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500495#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600496 else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4))
497 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500498 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
499 {
500 png_push_save_buffer(png_ptr);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600501 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500502 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500503
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600504 png_handle_sCAL(png_ptr, info_ptr, png_ptr->push_length);
505 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500506
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600507#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500508#ifdef PNG_READ_tIME_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600509 else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600510 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500511 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
512 {
513 png_push_save_buffer(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600514 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500515 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500516
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600517 png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600518 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500519
Guy Schalnat6d764711995-12-19 03:22:19 -0600520#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500521#ifdef PNG_READ_tEXt_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600522 else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600523 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500524 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
525 {
526 png_push_save_buffer(png_ptr);
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500527 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500528 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500529
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600530 png_push_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600531 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500532
Guy Schalnat6d764711995-12-19 03:22:19 -0600533#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500534#ifdef PNG_READ_zTXt_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600535 else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600536 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500537 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
538 {
539 png_push_save_buffer(png_ptr);
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500540 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500541 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500542
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600543 png_push_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600544 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500545
Guy Schalnat6d764711995-12-19 03:22:19 -0600546#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500547#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600548 else if (!png_memcmp(png_ptr->chunk_name, png_iTXt, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600549 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500550 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
551 {
552 png_push_save_buffer(png_ptr);
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500553 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500554 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500555
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600556 png_push_handle_iTXt(png_ptr, info_ptr, png_ptr->push_length);
557 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500558
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600559#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600560 else
561 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500562 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
563 {
564 png_push_save_buffer(png_ptr);
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500565 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500566 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600567 png_push_handle_unknown(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600568 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600569
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600570 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
Guy Schalnat6d764711995-12-19 03:22:19 -0600571}
572
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500573void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500574png_push_crc_skip(png_structp png_ptr, png_uint_32 skip)
Guy Schalnat6d764711995-12-19 03:22:19 -0600575{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600576 png_ptr->process_mode = PNG_SKIP_MODE;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500577 png_ptr->skip_length = skip;
Guy Schalnat6d764711995-12-19 03:22:19 -0600578}
579
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500580void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500581png_push_crc_finish(png_structp png_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -0600582{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600583 if (png_ptr->skip_length && png_ptr->save_buffer_size)
584 {
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500585 png_size_t save_size = png_ptr->current_buffer_size;
586 png_uint_32 skip_length = png_ptr->skip_length;
Guy Schalnat6d764711995-12-19 03:22:19 -0600587
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500588 /* We want the smaller of 'skip_length' and 'current_buffer_size', but
589 * they are of different types and we don't know which variable has the
590 * fewest bits. Carefully select the smaller and cast it to the type of
591 * the larger - this cannot overflow. Do not cast in the following test
592 * - it will break on either 16 or 64 bit platforms.
593 */
594 if (skip_length < save_size)
595 save_size = (png_size_t)skip_length;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500596
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600597 else
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500598 skip_length = (png_uint_32)save_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600599
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600600 png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
Guy Schalnat6d764711995-12-19 03:22:19 -0600601
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500602 png_ptr->skip_length -= skip_length;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600603 png_ptr->buffer_size -= save_size;
604 png_ptr->save_buffer_size -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500605 png_ptr->save_buffer_ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600606 }
607 if (png_ptr->skip_length && png_ptr->current_buffer_size)
608 {
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500609 png_size_t save_size = png_ptr->current_buffer_size;
610 png_uint_32 skip_length = png_ptr->skip_length;
Guy Schalnat6d764711995-12-19 03:22:19 -0600611
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500612 /* We want the smaller of 'skip_length' and 'current_buffer_size', but
613 * they are of different types and we don't know which variable has the
614 * fewest bits. Carefully select the smaller and cast it to the type of
615 * the larger - this cannot overflow.
616 */
617 if (skip_length < save_size)
618 save_size = (png_size_t)skip_length;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500619
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600620 else
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500621 skip_length = (png_uint_32)save_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600622
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600623 png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
Guy Schalnat6d764711995-12-19 03:22:19 -0600624
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500625 png_ptr->skip_length -= skip_length;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600626 png_ptr->buffer_size -= save_size;
627 png_ptr->current_buffer_size -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500628 png_ptr->current_buffer_ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600629 }
630 if (!png_ptr->skip_length)
631 {
632 if (png_ptr->buffer_size < 4)
633 {
634 png_push_save_buffer(png_ptr);
635 return;
636 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600637
638 png_crc_finish(png_ptr, 0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500639 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600640 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600641}
642
Glenn Randers-Pehrsoneae8e362010-03-12 17:36:53 -0600643void PNGCBAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500644png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length)
Guy Schalnat6d764711995-12-19 03:22:19 -0600645{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600646 png_bytep ptr;
Guy Schalnat6d764711995-12-19 03:22:19 -0600647
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500648 if (png_ptr == NULL)
649 return;
650
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600651 ptr = buffer;
652 if (png_ptr->save_buffer_size)
653 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500654 png_size_t save_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600655
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600656 if (length < png_ptr->save_buffer_size)
657 save_size = length;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500658
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600659 else
660 save_size = png_ptr->save_buffer_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600661
Andreas Dilger47a0c421997-05-16 02:46:07 -0500662 png_memcpy(ptr, png_ptr->save_buffer_ptr, save_size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600663 length -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500664 ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600665 png_ptr->buffer_size -= save_size;
666 png_ptr->save_buffer_size -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500667 png_ptr->save_buffer_ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600668 }
669 if (length && png_ptr->current_buffer_size)
670 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500671 png_size_t save_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600672
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600673 if (length < png_ptr->current_buffer_size)
674 save_size = length;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500675
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600676 else
677 save_size = png_ptr->current_buffer_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600678
Andreas Dilger47a0c421997-05-16 02:46:07 -0500679 png_memcpy(ptr, png_ptr->current_buffer_ptr, save_size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600680 png_ptr->buffer_size -= save_size;
681 png_ptr->current_buffer_size -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500682 png_ptr->current_buffer_ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600683 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600684}
685
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500686void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600687png_push_save_buffer(png_structp png_ptr)
688{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600689 if (png_ptr->save_buffer_size)
690 {
691 if (png_ptr->save_buffer_ptr != png_ptr->save_buffer)
692 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500693 png_size_t i, istop;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600694 png_bytep sp;
695 png_bytep dp;
Guy Schalnat6d764711995-12-19 03:22:19 -0600696
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500697 istop = png_ptr->save_buffer_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600698 for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer;
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600699 i < istop; i++, sp++, dp++)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600700 {
701 *dp = *sp;
702 }
703 }
704 }
Glenn Randers-Pehrsone00ed362010-02-07 22:42:58 -0600705 if (png_ptr->save_buffer_size + png_ptr->current_buffer_size >
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600706 png_ptr->save_buffer_max)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600707 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500708 png_size_t new_max;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600709 png_bytep old_buffer;
Guy Schalnat6d764711995-12-19 03:22:19 -0600710
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600711 if (png_ptr->save_buffer_size > PNG_SIZE_MAX -
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600712 (png_ptr->current_buffer_size + 256))
Glenn Randers-Pehrsone00ed362010-02-07 22:42:58 -0600713 {
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600714 png_error(png_ptr, "Potential overflow of save_buffer");
Glenn Randers-Pehrsone00ed362010-02-07 22:42:58 -0600715 }
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600716
Glenn Randers-Pehrsone00ed362010-02-07 22:42:58 -0600717 new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600718 old_buffer = png_ptr->save_buffer;
Glenn Randers-Pehrsonf1eb9182010-02-08 15:36:41 -0600719 png_ptr->save_buffer = (png_bytep)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600720 (png_size_t)new_max);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500721
Glenn Randers-Pehrsonf1eb9182010-02-08 15:36:41 -0600722 if (png_ptr->save_buffer == NULL)
723 {
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600724 png_free(png_ptr, old_buffer);
725 png_error(png_ptr, "Insufficient memory for save_buffer");
Glenn Randers-Pehrsonf1eb9182010-02-08 15:36:41 -0600726 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500727
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500728 png_memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
729 png_free(png_ptr, old_buffer);
730 png_ptr->save_buffer_max = new_max;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600731 }
732 if (png_ptr->current_buffer_size)
733 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500734 png_memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
735 png_ptr->current_buffer_ptr, png_ptr->current_buffer_size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600736 png_ptr->save_buffer_size += png_ptr->current_buffer_size;
737 png_ptr->current_buffer_size = 0;
738 }
739 png_ptr->save_buffer_ptr = png_ptr->save_buffer;
740 png_ptr->buffer_size = 0;
Guy Schalnat6d764711995-12-19 03:22:19 -0600741}
742
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500743void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600744png_push_restore_buffer(png_structp png_ptr, png_bytep buffer,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500745 png_size_t buffer_length)
Guy Schalnat6d764711995-12-19 03:22:19 -0600746{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600747 png_ptr->current_buffer = buffer;
748 png_ptr->current_buffer_size = buffer_length;
749 png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size;
750 png_ptr->current_buffer_ptr = png_ptr->current_buffer;
Guy Schalnat6d764711995-12-19 03:22:19 -0600751}
752
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500753void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -0500754png_push_read_IDAT(png_structp png_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -0600755{
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500756 PNG_IDAT;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600757 if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600758 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600759 png_byte chunk_length[4];
Guy Schalnat6d764711995-12-19 03:22:19 -0600760
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600761 if (png_ptr->buffer_size < 8)
762 {
763 png_push_save_buffer(png_ptr);
764 return;
765 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600766
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600767 png_push_fill_buffer(png_ptr, chunk_length, 4);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500768 png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600769 png_reset_crc(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500770 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600771 png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600772
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500773 if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600774 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500775 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500776
Guy Schalnate5a37791996-06-05 15:50:50 -0500777 if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600778 png_error(png_ptr, "Not enough compressed data");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500779
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600780 return;
781 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600782
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600783 png_ptr->idat_size = png_ptr->push_length;
784 }
785 if (png_ptr->idat_size && png_ptr->save_buffer_size)
786 {
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500787 png_size_t save_size = png_ptr->save_buffer_size;
788 png_uint_32 idat_size = png_ptr->idat_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600789
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500790 /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
791 * are of different types and we don't know which variable has the fewest
792 * bits. Carefully select the smaller and cast it to the type of the
793 * larger - this cannot overflow. Do not cast in the following test - it
794 * will break on either 16 or 64 bit platforms.
795 */
796 if (idat_size < save_size)
797 save_size = (png_size_t)idat_size;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500798
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600799 else
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500800 idat_size = (png_uint_32)save_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600801
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600802 png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500803
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500804 png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500805
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500806 png_ptr->idat_size -= idat_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600807 png_ptr->buffer_size -= save_size;
808 png_ptr->save_buffer_size -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500809 png_ptr->save_buffer_ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600810 }
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500811
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600812 if (png_ptr->idat_size && png_ptr->current_buffer_size)
813 {
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500814 png_size_t save_size = png_ptr->current_buffer_size;
815 png_uint_32 idat_size = png_ptr->idat_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600816
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500817 /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
818 * are of different types and we don't know which variable has the fewest
819 * bits. Carefully select the smaller and cast it to the type of the
820 * larger - this cannot overflow.
821 */
822 if (idat_size < save_size)
823 save_size = (png_size_t)idat_size;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500824
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600825 else
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500826 idat_size = (png_uint_32)save_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600827
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600828 png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500829
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500830 png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size);
Guy Schalnat6d764711995-12-19 03:22:19 -0600831
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500832 png_ptr->idat_size -= idat_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600833 png_ptr->buffer_size -= save_size;
834 png_ptr->current_buffer_size -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500835 png_ptr->current_buffer_ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600836 }
837 if (!png_ptr->idat_size)
838 {
839 if (png_ptr->buffer_size < 4)
840 {
841 png_push_save_buffer(png_ptr);
842 return;
843 }
844
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600845 png_crc_finish(png_ptr, 0);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600846 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
Glenn Randers-Pehrson859665d2002-08-06 18:06:11 -0500847 png_ptr->mode |= PNG_AFTER_IDAT;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600848 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600849}
850
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500851void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600852png_process_IDAT_data(png_structp png_ptr, png_bytep buffer,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500853 png_size_t buffer_length)
Guy Schalnat6d764711995-12-19 03:22:19 -0600854{
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500855 /* The caller checks for a non-zero buffer length. */
Glenn Randers-Pehrsonef3831a2010-06-22 13:03:32 -0500856 if (!(buffer_length > 0) || buffer == NULL)
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500857 png_error(png_ptr, "No IDAT data (internal error)");
Guy Schalnat6d764711995-12-19 03:22:19 -0600858
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500859 /* This routine must process all the data it has been given
860 * before returning, calling the row callback as required to
Glenn Randers-Pehrson5feb87c2010-06-21 12:28:05 -0500861 * handle the uncompressed results.
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500862 */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600863 png_ptr->zstream.next_in = buffer;
864 png_ptr->zstream.avail_in = (uInt)buffer_length;
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500865
866 /* Keep going until the decompressed data is all processed
867 * or the stream marked as finished.
868 */
869 while (png_ptr->zstream.avail_in > 0 &&
Glenn Randers-Pehrsond9d4cec2010-07-12 07:12:09 -0500870 !(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600871 {
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500872 int ret;
873
874 /* We have data for zlib, but we must check that zlib
Glenn Randers-Pehrsonef3831a2010-06-22 13:03:32 -0500875 * has someplace to put the results. It doesn't matter
Glenn Randers-Pehrson5feb87c2010-06-21 12:28:05 -0500876 * if we don't expect any results -- it may be the input
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500877 * data is just the LZ end code.
878 */
879 if (!(png_ptr->zstream.avail_out > 0))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600880 {
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600881 png_ptr->zstream.avail_out =
882 (uInt) PNG_ROWBYTES(png_ptr->pixel_depth,
883 png_ptr->iwidth) + 1;
Glenn Randers-Pehrson5feb87c2010-06-21 12:28:05 -0500884
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600885 png_ptr->zstream.next_out = png_ptr->row_buf;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600886 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500887
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500888 /* Using Z_SYNC_FLUSH here means that an unterminated
Glenn Randers-Pehrsonef3831a2010-06-22 13:03:32 -0500889 * LZ stream (a stream with a missing end code) can still
890 * be handled, otherwise (Z_NO_FLUSH) a future zlib
891 * implementation might defer output and therefore
892 * change the current behavior (see comments in inflate.c
893 * for why this doesn't happen at present with zlib 1.2.5).
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500894 */
895 ret = inflate(&png_ptr->zstream, Z_SYNC_FLUSH);
896
897 /* Check for any failure before proceeding. */
898 if (ret != Z_OK && ret != Z_STREAM_END)
899 {
Glenn Randers-Pehrsond9d4cec2010-07-12 07:12:09 -0500900 /* Terminate the decompression. */
901 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500902
903 /* This may be a truncated stream (missing or
Glenn Randers-Pehrsond9d4cec2010-07-12 07:12:09 -0500904 * damaged end code). Treat that as a warning.
905 */
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500906 if (png_ptr->row_number >= png_ptr->num_rows ||
Glenn Randers-Pehrsond9d4cec2010-07-12 07:12:09 -0500907 png_ptr->pass > 6)
908 png_warning(png_ptr, "Truncated compressed data in IDAT");
Glenn Randers-Pehrson5feb87c2010-06-21 12:28:05 -0500909
Glenn Randers-Pehrsond9d4cec2010-07-12 07:12:09 -0500910 else
911 png_error(png_ptr, "Decompression error in IDAT");
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500912
Glenn Randers-Pehrsond9d4cec2010-07-12 07:12:09 -0500913 /* Skip the check on unprocessed input */
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500914 return;
915 }
916
917 /* Did inflate output any data? */
918 if (png_ptr->zstream.next_out != png_ptr->row_buf)
919 {
Glenn Randers-Pehrsond9d4cec2010-07-12 07:12:09 -0500920 /* Is this unexpected data after the last row?
921 * If it is, artificially terminate the LZ output
922 * here.
923 */
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500924 if (png_ptr->row_number >= png_ptr->num_rows ||
Glenn Randers-Pehrsond9d4cec2010-07-12 07:12:09 -0500925 png_ptr->pass > 6)
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500926 {
Glenn Randers-Pehrsond9d4cec2010-07-12 07:12:09 -0500927 /* Extra data. */
928 png_warning(png_ptr, "Extra compressed data in IDAT");
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500929 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
Glenn Randers-Pehrson5feb87c2010-06-21 12:28:05 -0500930
Glenn Randers-Pehrsond9d4cec2010-07-12 07:12:09 -0500931 /* Do no more processing; skip the unprocessed
932 * input check below.
933 */
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500934 return;
Glenn Randers-Pehrsond9d4cec2010-07-12 07:12:09 -0500935 }
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500936
Glenn Randers-Pehrsond9d4cec2010-07-12 07:12:09 -0500937 /* Do we have a complete row? */
938 if (png_ptr->zstream.avail_out == 0)
939 png_push_process_row(png_ptr);
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500940 }
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500941
942 /* And check for the end of the stream. */
943 if (ret == Z_STREAM_END)
Glenn Randers-Pehrsond9d4cec2010-07-12 07:12:09 -0500944 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500945 }
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500946
947 /* All the data should have been processed, if anything
948 * is left at this point we have bytes of IDAT data
949 * after the zlib end code.
950 */
951 if (png_ptr->zstream.avail_in > 0)
Glenn Randers-Pehrson5feb87c2010-06-21 12:28:05 -0500952 png_warning(png_ptr, "Extra compression data in IDAT");
Guy Schalnat6d764711995-12-19 03:22:19 -0600953}
954
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500955void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600956png_push_process_row(png_structp png_ptr)
957{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600958 png_ptr->row_info.color_type = png_ptr->color_type;
Guy Schalnat6d764711995-12-19 03:22:19 -0600959 png_ptr->row_info.width = png_ptr->iwidth;
960 png_ptr->row_info.channels = png_ptr->channels;
961 png_ptr->row_info.bit_depth = png_ptr->bit_depth;
962 png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600963
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500964 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
965 png_ptr->row_info.width);
Guy Schalnat6d764711995-12-19 03:22:19 -0600966
Guy Schalnate5a37791996-06-05 15:50:50 -0500967 png_read_filter_row(png_ptr, &(png_ptr->row_info),
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500968 png_ptr->row_buf + 1, png_ptr->prev_row + 1,
969 (int)(png_ptr->row_buf[0]));
Guy Schalnat6d764711995-12-19 03:22:19 -0600970
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500971 png_memcpy(png_ptr->prev_row, png_ptr->row_buf, png_ptr->rowbytes + 1);
Guy Schalnat6d764711995-12-19 03:22:19 -0600972
Glenn Randers-Pehrson73b029f2004-11-26 17:28:09 -0600973 if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA))
Guy Schalnat6d764711995-12-19 03:22:19 -0600974 png_do_read_transformations(png_ptr);
975
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500976#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500977 /* Blow up interlaced rows to full size */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500978 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600979 {
980 if (png_ptr->pass < 6)
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -0600981/* old interface (pre-1.0.9):
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600982 png_do_read_interlace(&(png_ptr->row_info),
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600983 png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -0600984 */
985 png_do_read_interlace(png_ptr);
Guy Schalnat6d764711995-12-19 03:22:19 -0600986
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -0600987 switch (png_ptr->pass)
988 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600989 case 0:
990 {
991 int i;
992 for (i = 0; i < 8 && png_ptr->pass == 0; i++)
993 {
994 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500995 png_read_push_finish_row(png_ptr); /* Updates png_ptr->pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600996 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500997
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500998 if (png_ptr->pass == 2) /* Pass 1 might be empty */
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -0600999 {
1000 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
1001 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001002 png_push_have_row(png_ptr, NULL);
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -06001003 png_read_push_finish_row(png_ptr);
1004 }
1005 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001006
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001007 if (png_ptr->pass == 4 && png_ptr->height <= 4)
1008 {
1009 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1010 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001011 png_push_have_row(png_ptr, NULL);
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001012 png_read_push_finish_row(png_ptr);
1013 }
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001014 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001015
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001016 if (png_ptr->pass == 6 && png_ptr->height <= 4)
1017 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001018 png_push_have_row(png_ptr, NULL);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001019 png_read_push_finish_row(png_ptr);
1020 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001021
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001022 break;
1023 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001024
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001025 case 1:
1026 {
1027 int i;
1028 for (i = 0; i < 8 && png_ptr->pass == 1; i++)
1029 {
1030 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1031 png_read_push_finish_row(png_ptr);
1032 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001033
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001034 if (png_ptr->pass == 2) /* Skip top 4 generated rows */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001035 {
1036 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
1037 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001038 png_push_have_row(png_ptr, NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001039 png_read_push_finish_row(png_ptr);
1040 }
1041 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001042
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001043 break;
1044 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001045
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001046 case 2:
1047 {
1048 int i;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001049
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001050 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
1051 {
1052 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1053 png_read_push_finish_row(png_ptr);
1054 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001055
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001056 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
1057 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001058 png_push_have_row(png_ptr, NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001059 png_read_push_finish_row(png_ptr);
1060 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001061
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001062 if (png_ptr->pass == 4) /* Pass 3 might be empty */
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -06001063 {
1064 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1065 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001066 png_push_have_row(png_ptr, NULL);
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -06001067 png_read_push_finish_row(png_ptr);
1068 }
1069 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001070
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001071 break;
1072 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001073
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001074 case 3:
1075 {
1076 int i;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001077
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001078 for (i = 0; i < 4 && png_ptr->pass == 3; i++)
1079 {
1080 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1081 png_read_push_finish_row(png_ptr);
1082 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001083
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001084 if (png_ptr->pass == 4) /* Skip top two generated rows */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001085 {
1086 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1087 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001088 png_push_have_row(png_ptr, NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001089 png_read_push_finish_row(png_ptr);
1090 }
1091 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001092
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001093 break;
1094 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001095
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001096 case 4:
1097 {
1098 int i;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001099
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001100 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1101 {
1102 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1103 png_read_push_finish_row(png_ptr);
1104 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001105
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001106 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1107 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001108 png_push_have_row(png_ptr, NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001109 png_read_push_finish_row(png_ptr);
1110 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001111
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001112 if (png_ptr->pass == 6) /* Pass 5 might be empty */
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -06001113 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001114 png_push_have_row(png_ptr, NULL);
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -06001115 png_read_push_finish_row(png_ptr);
1116 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001117
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001118 break;
1119 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001120
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001121 case 5:
1122 {
1123 int i;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001124
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001125 for (i = 0; i < 2 && png_ptr->pass == 5; i++)
1126 {
1127 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1128 png_read_push_finish_row(png_ptr);
1129 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001130
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001131 if (png_ptr->pass == 6) /* Skip top generated row */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001132 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001133 png_push_have_row(png_ptr, NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001134 png_read_push_finish_row(png_ptr);
1135 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001136
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001137 break;
1138 }
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001139
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001140 case 6:
1141 {
1142 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1143 png_read_push_finish_row(png_ptr);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001144
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001145 if (png_ptr->pass != 6)
1146 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001147
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001148 png_push_have_row(png_ptr, NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001149 png_read_push_finish_row(png_ptr);
1150 }
1151 }
1152 }
1153 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001154#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001155 {
1156 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1157 png_read_push_finish_row(png_ptr);
1158 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001159}
1160
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001161void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001162png_read_push_finish_row(png_structp png_ptr)
1163{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001164 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001165
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001166 /* Start of interlace block */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001167 PNG_CONST int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001168
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001169 /* Offset to next interlace block */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001170 PNG_CONST int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001171
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001172 /* Start of interlace block in the y direction */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001173 PNG_CONST int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001174
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001175 /* Offset to next interlace block in the y direction */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001176 PNG_CONST int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001177
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001178 /* Height of interlace block. This is not currently used - if you need
1179 * it, uncomment it here and in png.h
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001180 PNG_CONST int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001181 */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001182
Guy Schalnat6d764711995-12-19 03:22:19 -06001183 png_ptr->row_number++;
1184 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001185 return;
Guy Schalnat6d764711995-12-19 03:22:19 -06001186
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001187#ifdef PNG_READ_INTERLACING_SUPPORTED
Guy Schalnat6d764711995-12-19 03:22:19 -06001188 if (png_ptr->interlaced)
1189 {
1190 png_ptr->row_number = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001191 png_memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
1192
Guy Schalnat6d764711995-12-19 03:22:19 -06001193 do
1194 {
1195 png_ptr->pass++;
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -06001196 if ((png_ptr->pass == 1 && png_ptr->width < 5) ||
1197 (png_ptr->pass == 3 && png_ptr->width < 3) ||
1198 (png_ptr->pass == 5 && png_ptr->width < 2))
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001199 png_ptr->pass++;
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -06001200
Glenn Randers-Pehrson859665d2002-08-06 18:06:11 -05001201 if (png_ptr->pass > 7)
1202 png_ptr->pass--;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001203
Guy Schalnat6d764711995-12-19 03:22:19 -06001204 if (png_ptr->pass >= 7)
1205 break;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001206
Guy Schalnat6d764711995-12-19 03:22:19 -06001207 png_ptr->iwidth = (png_ptr->width +
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001208 png_pass_inc[png_ptr->pass] - 1 -
1209 png_pass_start[png_ptr->pass]) /
1210 png_pass_inc[png_ptr->pass];
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001211
Guy Schalnat6d764711995-12-19 03:22:19 -06001212 if (png_ptr->transformations & PNG_INTERLACE)
1213 break;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001214
1215 png_ptr->num_rows = (png_ptr->height +
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001216 png_pass_yinc[png_ptr->pass] - 1 -
1217 png_pass_ystart[png_ptr->pass]) /
1218 png_pass_yinc[png_ptr->pass];
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001219
1220 } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0);
Guy Schalnat6d764711995-12-19 03:22:19 -06001221 }
Glenn Randers-Pehrsona31c74f2009-05-18 15:52:01 -05001222#endif /* PNG_READ_INTERLACING_SUPPORTED */
Guy Schalnat6d764711995-12-19 03:22:19 -06001223}
1224
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001225#ifdef PNG_READ_tEXt_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001226void /* PRIVATE */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001227png_push_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001228 length)
Guy Schalnat6d764711995-12-19 03:22:19 -06001229{
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001230 if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001231 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001232 info_ptr = info_ptr; /* To quiet some compiler warnings */
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001233 png_error(png_ptr, "Out of place tEXt");
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001234 /*NOT REACHED*/
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001235 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001236
Andreas Dilger47a0c421997-05-16 02:46:07 -05001237#ifdef PNG_MAX_MALLOC_64K
1238 png_ptr->skip_length = 0; /* This may not be necessary */
1239
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001240 if (length > (png_uint_32)65535L) /* Can't hold entire string in memory */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001241 {
1242 png_warning(png_ptr, "tEXt chunk too large to fit in memory");
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001243 png_ptr->skip_length = length - (png_uint_32)65535L;
1244 length = (png_uint_32)65535L;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001245 }
1246#endif
1247
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001248 png_ptr->current_text = (png_charp)png_malloc(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001249 (png_size_t)(length + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001250 png_ptr->current_text[length] = '\0';
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001251 png_ptr->current_text_ptr = png_ptr->current_text;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001252 png_ptr->current_text_size = (png_size_t)length;
1253 png_ptr->current_text_left = (png_size_t)length;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001254 png_ptr->process_mode = PNG_READ_tEXt_MODE;
Guy Schalnat6d764711995-12-19 03:22:19 -06001255}
1256
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001257void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001258png_push_read_tEXt(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -06001259{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001260 if (png_ptr->buffer_size && png_ptr->current_text_left)
1261 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001262 png_size_t text_size;
Guy Schalnat6d764711995-12-19 03:22:19 -06001263
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001264 if (png_ptr->buffer_size < png_ptr->current_text_left)
1265 text_size = png_ptr->buffer_size;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001266
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001267 else
1268 text_size = png_ptr->current_text_left;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001269
Andreas Dilger47a0c421997-05-16 02:46:07 -05001270 png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001271 png_ptr->current_text_left -= text_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001272 png_ptr->current_text_ptr += text_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001273 }
1274 if (!(png_ptr->current_text_left))
1275 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001276 png_textp text_ptr;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001277 png_charp text;
1278 png_charp key;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001279 int ret;
Guy Schalnat6d764711995-12-19 03:22:19 -06001280
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001281 if (png_ptr->buffer_size < 4)
1282 {
1283 png_push_save_buffer(png_ptr);
1284 return;
1285 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001286
Andreas Dilger47a0c421997-05-16 02:46:07 -05001287 png_push_crc_finish(png_ptr);
1288
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001289#ifdef PNG_MAX_MALLOC_64K
Andreas Dilger47a0c421997-05-16 02:46:07 -05001290 if (png_ptr->skip_length)
1291 return;
1292#endif
Guy Schalnat6d764711995-12-19 03:22:19 -06001293
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001294 key = png_ptr->current_text;
Guy Schalnat6d764711995-12-19 03:22:19 -06001295
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001296 for (text = key; *text; text++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001297 /* Empty loop */ ;
Guy Schalnat6d764711995-12-19 03:22:19 -06001298
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001299 if (text < key + png_ptr->current_text_size)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001300 text++;
1301
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001302 text_ptr = (png_textp)png_malloc(png_ptr, png_sizeof(png_text));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001303 text_ptr->compression = PNG_TEXT_COMPRESSION_NONE;
1304 text_ptr->key = key;
Glenn Randers-Pehrsondd78d522010-03-30 08:34:02 -05001305 text_ptr->itxt_length = 0;
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001306 text_ptr->lang = NULL;
1307 text_ptr->lang_key = NULL;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001308 text_ptr->text = text;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001309
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001310 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001311
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001312 png_free(png_ptr, key);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001313 png_free(png_ptr, text_ptr);
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001314 png_ptr->current_text = NULL;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001315
1316 if (ret)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001317 png_warning(png_ptr, "Insufficient memory to store text chunk");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001318 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001319}
1320#endif
1321
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001322#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001323void /* PRIVATE */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001324png_push_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
1325 length)
Guy Schalnat6d764711995-12-19 03:22:19 -06001326{
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001327 if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001328 {
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001329 info_ptr = info_ptr; /* To quiet some compiler warnings */
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001330 png_error(png_ptr, "Out of place zTXt");
1331 /*NOT REACHED*/
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001332 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001333
Andreas Dilger47a0c421997-05-16 02:46:07 -05001334#ifdef PNG_MAX_MALLOC_64K
1335 /* We can't handle zTXt chunks > 64K, since we don't have enough space
1336 * to be able to store the uncompressed data. Actually, the threshold
1337 * is probably around 32K, but it isn't as definite as 64K is.
1338 */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001339 if (length > (png_uint_32)65535L)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001340 {
1341 png_warning(png_ptr, "zTXt chunk too large to fit in memory");
1342 png_push_crc_skip(png_ptr, length);
1343 return;
1344 }
1345#endif
1346
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001347 png_ptr->current_text = (png_charp)png_malloc(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001348 (png_size_t)(length + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001349 png_ptr->current_text[length] = '\0';
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001350 png_ptr->current_text_ptr = png_ptr->current_text;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001351 png_ptr->current_text_size = (png_size_t)length;
1352 png_ptr->current_text_left = (png_size_t)length;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001353 png_ptr->process_mode = PNG_READ_zTXt_MODE;
Guy Schalnat6d764711995-12-19 03:22:19 -06001354}
1355
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001356void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001357png_push_read_zTXt(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -06001358{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001359 if (png_ptr->buffer_size && png_ptr->current_text_left)
1360 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001361 png_size_t text_size;
Guy Schalnat6d764711995-12-19 03:22:19 -06001362
Andreas Dilger47a0c421997-05-16 02:46:07 -05001363 if (png_ptr->buffer_size < (png_uint_32)png_ptr->current_text_left)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001364 text_size = png_ptr->buffer_size;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001365
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001366 else
1367 text_size = png_ptr->current_text_left;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001368
Andreas Dilger47a0c421997-05-16 02:46:07 -05001369 png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001370 png_ptr->current_text_left -= text_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001371 png_ptr->current_text_ptr += text_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001372 }
1373 if (!(png_ptr->current_text_left))
1374 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001375 png_textp text_ptr;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001376 png_charp text;
1377 png_charp key;
1378 int ret;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001379 png_size_t text_size, key_size;
Guy Schalnat6d764711995-12-19 03:22:19 -06001380
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001381 if (png_ptr->buffer_size < 4)
1382 {
1383 png_push_save_buffer(png_ptr);
1384 return;
1385 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001386
Andreas Dilger47a0c421997-05-16 02:46:07 -05001387 png_push_crc_finish(png_ptr);
Guy Schalnat6d764711995-12-19 03:22:19 -06001388
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001389 key = png_ptr->current_text;
Guy Schalnat6d764711995-12-19 03:22:19 -06001390
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001391 for (text = key; *text; text++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001392 /* Empty loop */ ;
Guy Schalnat6d764711995-12-19 03:22:19 -06001393
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001394 /* zTXt can't have zero text */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001395 if (text >= key + png_ptr->current_text_size)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001396 {
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001397 png_ptr->current_text = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001398 png_free(png_ptr, key);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001399 return;
1400 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001401
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001402 text++;
Guy Schalnat6d764711995-12-19 03:22:19 -06001403
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001404 if (*text != PNG_TEXT_COMPRESSION_zTXt) /* Check compression byte */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001405 {
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001406 png_ptr->current_text = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001407 png_free(png_ptr, key);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001408 return;
1409 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001410
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001411 text++;
Guy Schalnat6d764711995-12-19 03:22:19 -06001412
Glenn Randers-Pehrsonbc363ec2010-10-12 21:17:00 -05001413 png_ptr->zstream.next_in = (png_bytep)text;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001414 png_ptr->zstream.avail_in = (uInt)(png_ptr->current_text_size -
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001415 (text - key));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001416 png_ptr->zstream.next_out = png_ptr->zbuf;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001417 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnat6d764711995-12-19 03:22:19 -06001418
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001419 key_size = text - key;
1420 text_size = 0;
1421 text = NULL;
1422 ret = Z_STREAM_END;
Guy Schalnat6d764711995-12-19 03:22:19 -06001423
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001424 while (png_ptr->zstream.avail_in)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001425 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001426 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001427 if (ret != Z_OK && ret != Z_STREAM_END)
1428 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001429 inflateReset(&png_ptr->zstream);
1430 png_ptr->zstream.avail_in = 0;
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001431 png_ptr->current_text = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001432 png_free(png_ptr, key);
1433 png_free(png_ptr, text);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001434 return;
1435 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001436
Andreas Dilger47a0c421997-05-16 02:46:07 -05001437 if (!(png_ptr->zstream.avail_out) || ret == Z_STREAM_END)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001438 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001439 if (text == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001440 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001441 text = (png_charp)png_malloc(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001442 (png_ptr->zbuf_size
1443 - png_ptr->zstream.avail_out + key_size + 1));
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001444
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001445 png_memcpy(text + key_size, png_ptr->zbuf,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001446 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001447
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001448 png_memcpy(text, key, key_size);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001449
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001450 text_size = key_size + png_ptr->zbuf_size -
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001451 png_ptr->zstream.avail_out;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001452
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001453 *(text + text_size) = '\0';
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001454 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001455
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001456 else
1457 {
1458 png_charp tmp;
Guy Schalnat6d764711995-12-19 03:22:19 -06001459
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001460 tmp = text;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001461 text = (png_charp)png_malloc(png_ptr, text_size +
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -05001462 (png_ptr->zbuf_size
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001463 - png_ptr->zstream.avail_out + 1));
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001464
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001465 png_memcpy(text, tmp, text_size);
1466 png_free(png_ptr, tmp);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001467
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001468 png_memcpy(text + text_size, png_ptr->zbuf,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001469 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001470
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001471 text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out;
1472 *(text + text_size) = '\0';
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001473 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001474
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001475 if (ret != Z_STREAM_END)
1476 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001477 png_ptr->zstream.next_out = png_ptr->zbuf;
1478 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001479 }
1480 }
1481 else
1482 {
1483 break;
1484 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001485
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001486 if (ret == Z_STREAM_END)
1487 break;
1488 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001489
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001490 inflateReset(&png_ptr->zstream);
1491 png_ptr->zstream.avail_in = 0;
Guy Schalnat6d764711995-12-19 03:22:19 -06001492
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001493 if (ret != Z_STREAM_END)
1494 {
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001495 png_ptr->current_text = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001496 png_free(png_ptr, key);
1497 png_free(png_ptr, text);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001498 return;
1499 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001500
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001501 png_ptr->current_text = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001502 png_free(png_ptr, key);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001503 key = text;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001504 text += key_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001505
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001506 text_ptr = (png_textp)png_malloc(png_ptr,
1507 png_sizeof(png_text));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001508 text_ptr->compression = PNG_TEXT_COMPRESSION_zTXt;
1509 text_ptr->key = key;
Glenn Randers-Pehrsondd78d522010-03-30 08:34:02 -05001510 text_ptr->itxt_length = 0;
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001511 text_ptr->lang = NULL;
1512 text_ptr->lang_key = NULL;
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001513 text_ptr->text = text;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001514
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001515 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001516
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001517 png_free(png_ptr, key);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001518 png_free(png_ptr, text_ptr);
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001519
1520 if (ret)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001521 png_warning(png_ptr, "Insufficient memory to store text chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001522 }
1523}
1524#endif
1525
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001526#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001527void /* PRIVATE */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001528png_push_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001529 length)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001530{
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001531 if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001532 {
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001533 info_ptr = info_ptr; /* To quiet some compiler warnings */
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001534 png_error(png_ptr, "Out of place iTXt");
1535 /*NOT REACHED*/
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001536 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001537
1538#ifdef PNG_MAX_MALLOC_64K
1539 png_ptr->skip_length = 0; /* This may not be necessary */
1540
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001541 if (length > (png_uint_32)65535L) /* Can't hold entire string in memory */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001542 {
1543 png_warning(png_ptr, "iTXt chunk too large to fit in memory");
1544 png_ptr->skip_length = length - (png_uint_32)65535L;
1545 length = (png_uint_32)65535L;
1546 }
1547#endif
1548
1549 png_ptr->current_text = (png_charp)png_malloc(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001550 (png_size_t)(length + 1));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001551 png_ptr->current_text[length] = '\0';
1552 png_ptr->current_text_ptr = png_ptr->current_text;
1553 png_ptr->current_text_size = (png_size_t)length;
1554 png_ptr->current_text_left = (png_size_t)length;
1555 png_ptr->process_mode = PNG_READ_iTXt_MODE;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001556}
1557
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001558void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001559png_push_read_iTXt(png_structp png_ptr, png_infop info_ptr)
1560{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001561
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001562 if (png_ptr->buffer_size && png_ptr->current_text_left)
1563 {
1564 png_size_t text_size;
1565
1566 if (png_ptr->buffer_size < png_ptr->current_text_left)
1567 text_size = png_ptr->buffer_size;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001568
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001569 else
1570 text_size = png_ptr->current_text_left;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001571
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001572 png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
1573 png_ptr->current_text_left -= text_size;
1574 png_ptr->current_text_ptr += text_size;
1575 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001576
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001577 if (!(png_ptr->current_text_left))
1578 {
1579 png_textp text_ptr;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001580 png_charp key;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001581 int comp_flag;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001582 png_charp lang;
1583 png_charp lang_key;
1584 png_charp text;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001585 int ret;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001586
1587 if (png_ptr->buffer_size < 4)
1588 {
1589 png_push_save_buffer(png_ptr);
1590 return;
1591 }
1592
1593 png_push_crc_finish(png_ptr);
1594
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001595#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001596 if (png_ptr->skip_length)
1597 return;
1598#endif
1599
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001600 key = png_ptr->current_text;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001601
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001602 for (lang = key; *lang; lang++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001603 /* Empty loop */ ;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001604
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001605 if (lang < key + png_ptr->current_text_size - 3)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001606 lang++;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001607
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001608 comp_flag = *lang++;
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001609 lang++; /* Skip comp_type, always zero */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001610
1611 for (lang_key = lang; *lang_key; lang_key++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001612 /* Empty loop */ ;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001613
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001614 lang_key++; /* Skip NUL separator */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001615
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001616 text=lang_key;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001617
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001618 if (lang_key < key + png_ptr->current_text_size - 1)
1619 {
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001620 for (; *text; text++)
1621 /* Empty loop */ ;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001622 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001623
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001624 if (text < key + png_ptr->current_text_size)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001625 text++;
1626
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001627 text_ptr = (png_textp)png_malloc(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001628 png_sizeof(png_text));
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001629
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001630 text_ptr->compression = comp_flag + 2;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001631 text_ptr->key = key;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001632 text_ptr->lang = lang;
1633 text_ptr->lang_key = lang_key;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001634 text_ptr->text = text;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001635 text_ptr->text_length = 0;
1636 text_ptr->itxt_length = png_strlen(text);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001637
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001638 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001639
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001640 png_ptr->current_text = NULL;
1641
Andreas Dilger47a0c421997-05-16 02:46:07 -05001642 png_free(png_ptr, text_ptr);
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001643 if (ret)
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -06001644 png_warning(png_ptr, "Insufficient memory to store iTXt chunk");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001645 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001646}
1647#endif
1648
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001649/* This function is called when we haven't found a handler for this
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001650 * chunk. If there isn't a problem with the chunk itself (ie a bad chunk
1651 * name or a critical chunk), the chunk is (currently) silently ignored.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001652 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001653void /* PRIVATE */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001654png_push_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001655 length)
Guy Schalnat6d764711995-12-19 03:22:19 -06001656{
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -05001657 png_uint_32 skip = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001658
1659 if (!(png_ptr->chunk_name[0] & 0x20))
1660 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001661#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001662 if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001663 PNG_HANDLE_CHUNK_ALWAYS
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001664#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001665 && png_ptr->read_user_chunk_fn == NULL
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001666#endif
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001667 )
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001668#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001669 png_chunk_error(png_ptr, "unknown critical chunk");
1670
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001671 info_ptr = info_ptr; /* To quiet some compiler warnings */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001672 }
1673
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001674#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001675 if (png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS)
1676 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001677#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001678 if (length > (png_uint_32)65535L)
1679 {
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001680 png_warning(png_ptr, "unknown chunk too large to fit in memory");
1681 skip = length - (png_uint_32)65535L;
1682 length = (png_uint_32)65535L;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001683 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001684#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001685 png_memcpy((png_charp)png_ptr->unknown_chunk.name,
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -05001686 (png_charp)png_ptr->chunk_name,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001687 png_sizeof(png_ptr->unknown_chunk.name));
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -05001688 png_ptr->unknown_chunk.name[png_sizeof(png_ptr->unknown_chunk.name) - 1]
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001689 = '\0';
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001690
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001691 png_ptr->unknown_chunk.size = (png_size_t)length;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001692
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001693 if (length == 0)
1694 png_ptr->unknown_chunk.data = NULL;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001695
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001696 else
1697 {
1698 png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001699 (png_size_t)length);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001700 png_crc_read(png_ptr, (png_bytep)png_ptr->unknown_chunk.data, length);
1701 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001702
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001703#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001704 if (png_ptr->read_user_chunk_fn != NULL)
1705 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001706 /* Callback to user unknown chunk handler */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001707 int ret;
1708 ret = (*(png_ptr->read_user_chunk_fn))
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001709 (png_ptr, &png_ptr->unknown_chunk);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001710
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001711 if (ret < 0)
1712 png_chunk_error(png_ptr, "error in user chunk");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001713
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001714 if (ret == 0)
1715 {
1716 if (!(png_ptr->chunk_name[0] & 0x20))
1717 if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001718 PNG_HANDLE_CHUNK_ALWAYS)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001719 png_chunk_error(png_ptr, "unknown critical chunk");
1720 png_set_unknown_chunks(png_ptr, info_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001721 &png_ptr->unknown_chunk, 1);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001722 }
1723 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001724
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001725 else
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001726#endif
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001727 png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001728 png_free(png_ptr, png_ptr->unknown_chunk.data);
1729 png_ptr->unknown_chunk.data = NULL;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001730 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001731
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001732 else
1733#endif
1734 skip=length;
1735 png_push_crc_skip(png_ptr, skip);
Guy Schalnat6d764711995-12-19 03:22:19 -06001736}
1737
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001738void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001739png_push_have_info(png_structp png_ptr, png_infop info_ptr)
1740{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001741 if (png_ptr->info_fn != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001742 (*(png_ptr->info_fn))(png_ptr, info_ptr);
1743}
1744
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001745void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001746png_push_have_end(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -06001747{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001748 if (png_ptr->end_fn != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001749 (*(png_ptr->end_fn))(png_ptr, info_ptr);
Guy Schalnat6d764711995-12-19 03:22:19 -06001750}
1751
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001752void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001753png_push_have_row(png_structp png_ptr, png_bytep row)
1754{
Glenn Randers-Pehrson78d55452010-06-19 07:03:50 -05001755 if (png_ptr->row_fn != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001756 (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number,
1757 (int)png_ptr->pass);
Guy Schalnat6d764711995-12-19 03:22:19 -06001758}
1759
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001760void PNGAPI
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001761png_progressive_combine_row (png_structp png_ptr, png_bytep old_row,
1762 png_const_bytep new_row)
Guy Schalnat6d764711995-12-19 03:22:19 -06001763{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001764 PNG_CONST int FARDATA png_pass_dsp_mask[7] =
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001765 {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
Glenn Randers-Pehrson86312502009-08-31 14:17:23 -05001766
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001767 if (png_ptr == NULL)
1768 return;
1769
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -05001770 if (new_row != NULL) /* new_row must == png_ptr->row_buf here. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001771 png_combine_row(png_ptr, old_row, png_pass_dsp_mask[png_ptr->pass]);
Guy Schalnat6d764711995-12-19 03:22:19 -06001772}
1773
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001774void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001775png_set_progressive_read_fn(png_structp png_ptr, png_voidp progressive_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001776 png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
1777 png_progressive_end_ptr end_fn)
Guy Schalnat6d764711995-12-19 03:22:19 -06001778{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001779 if (png_ptr == NULL)
1780 return;
1781
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001782 png_ptr->info_fn = info_fn;
1783 png_ptr->row_fn = row_fn;
Guy Schalnat6d764711995-12-19 03:22:19 -06001784 png_ptr->end_fn = end_fn;
Guy Schalnate5a37791996-06-05 15:50:50 -05001785
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -05001786 png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer);
Guy Schalnat6d764711995-12-19 03:22:19 -06001787}
1788
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001789png_voidp PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001790png_get_progressive_ptr(png_structp png_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -06001791{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001792 if (png_ptr == NULL)
1793 return (NULL);
1794
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001795 return png_ptr->io_ptr;
Guy Schalnat6d764711995-12-19 03:22:19 -06001796}
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001797#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */