blob: 13d48311fbec14eed97f5c7337ff75280052721c [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-Pehrson3d3aae12011-02-02 22:58:27 -06004 * Last changed in libpng 1.5.1 [February 3, 2011]
Glenn Randers-Pehrson64b863c2011-01-04 09:57:06 -06005 * Copyright (c) 1998-2011 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
John Bowler0a5c9c02011-01-22 17:36:34 -060044png_size_t PNGAPI
45png_process_data_pause(png_structp png_ptr, int save)
46{
47 if (png_ptr != NULL)
48 {
49 /* It's easiest for the caller if we do the save, then the caller doesn't
50 * have to supply the same data again:
51 */
52 if (save)
53 png_push_save_buffer(png_ptr);
54 else
55 {
56 /* This includes any pending saved bytes: */
57 png_size_t remaining = png_ptr->buffer_size;
58 png_ptr->buffer_size = 0;
59
60 /* So subtract the saved buffer size, unless all the data
61 * is actually 'saved', in which case we just return 0
62 */
63 if (png_ptr->save_buffer_size < remaining)
64 return remaining - png_ptr->save_buffer_size;
65 }
66 }
67
68 return 0;
69}
70
71png_uint_32 PNGAPI
72png_process_data_skip(png_structp png_ptr)
73{
John Bowlerfca50df2011-01-28 06:34:02 -060074 png_uint_32 remaining = 0;
John Bowler0a5c9c02011-01-22 17:36:34 -060075
76 if (png_ptr != NULL && png_ptr->process_mode == PNG_SKIP_MODE &&
77 png_ptr->skip_length > 0)
78 {
79 /* At the end of png_process_data the buffer size must be 0 (see the loop
80 * above) so we can detect a broken call here:
81 */
82 if (png_ptr->buffer_size != 0)
83 png_error(png_ptr,
84 "png_process_data_skip called inside png_process_data");
85
86 /* If is impossible for there to be a saved buffer at this point -
87 * otherwise we could not be in SKIP mode. This will also happen if
88 * png_process_skip is called inside png_process_data (but only very
89 * rarely.)
90 */
91 if (png_ptr->save_buffer_size != 0)
92 png_error(png_ptr, "png_process_data_skip called with saved data");
93
94 remaining = png_ptr->skip_length;
95 png_ptr->skip_length = 0;
96 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
97 }
98
99 return remaining;
100}
101
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600102/* What we do with the incoming data depends on what we were previously
103 * doing before we ran out of data...
104 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500105void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600106png_process_some_data(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -0600107{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500108 if (png_ptr == NULL)
109 return;
110
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600111 switch (png_ptr->process_mode)
112 {
113 case PNG_READ_SIG_MODE:
114 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600115 png_push_read_sig(png_ptr, info_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600116 break;
117 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500118
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600119 case PNG_READ_CHUNK_MODE:
120 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600121 png_push_read_chunk(png_ptr, info_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600122 break;
123 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500124
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600125 case PNG_READ_IDAT_MODE:
126 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500127 png_push_read_IDAT(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600128 break;
129 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500130
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500131#ifdef PNG_READ_tEXt_SUPPORTED
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600132 case PNG_READ_tEXt_MODE:
133 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600134 png_push_read_tEXt(png_ptr, info_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600135 break;
136 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500137
Guy Schalnat6d764711995-12-19 03:22:19 -0600138#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500139#ifdef PNG_READ_zTXt_SUPPORTED
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600140 case PNG_READ_zTXt_MODE:
141 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600142 png_push_read_zTXt(png_ptr, info_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600143 break;
144 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500145
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600146#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500147#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600148 case PNG_READ_iTXt_MODE:
149 {
150 png_push_read_iTXt(png_ptr, info_ptr);
151 break;
152 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500153
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600154#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600155 case PNG_SKIP_MODE:
156 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500157 png_push_crc_finish(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600158 break;
159 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500160
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600161 default:
162 {
163 png_ptr->buffer_size = 0;
164 break;
165 }
166 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600167}
168
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600169/* Read any remaining signature bytes from the stream and compare them with
170 * the correct PNG signature. It is possible that this routine is called
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500171 * with bytes already read from the signature, either because they have been
172 * checked by the calling application, or because of multiple calls to this
173 * routine.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600174 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500175void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600176png_push_read_sig(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -0600177{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500178 png_size_t num_checked = png_ptr->sig_bytes,
179 num_to_check = 8 - num_checked;
Guy Schalnat6d764711995-12-19 03:22:19 -0600180
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600181 if (png_ptr->buffer_size < num_to_check)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600182 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600183 num_to_check = png_ptr->buffer_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600184 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600185
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600186 png_push_fill_buffer(png_ptr, &(info_ptr->signature[num_checked]),
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600187 num_to_check);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500188 png_ptr->sig_bytes = (png_byte)(png_ptr->sig_bytes + num_to_check);
Guy Schalnat6d764711995-12-19 03:22:19 -0600189
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600190 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600191 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600192 if (num_checked < 4 &&
193 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
194 png_error(png_ptr, "Not a PNG file");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500195
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600196 else
197 png_error(png_ptr, "PNG file corrupted by ASCII conversion");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600198 }
199 else
200 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600201 if (png_ptr->sig_bytes >= 8)
202 {
203 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
204 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600205 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600206}
207
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500208void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600209png_push_read_chunk(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -0600210{
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500211 PNG_IHDR;
212 PNG_IDAT;
213 PNG_IEND;
214 PNG_PLTE;
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500215#ifdef PNG_READ_bKGD_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500216 PNG_bKGD;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600217#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500218#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500219 PNG_cHRM;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600220#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500221#ifdef PNG_READ_gAMA_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500222 PNG_gAMA;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600223#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500224#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500225 PNG_hIST;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600226#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500227#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500228 PNG_iCCP;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600229#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500230#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500231 PNG_iTXt;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600232#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500233#ifdef PNG_READ_oFFs_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500234 PNG_oFFs;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600235#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500236#ifdef PNG_READ_pCAL_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500237 PNG_pCAL;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600238#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500239#ifdef PNG_READ_pHYs_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500240 PNG_pHYs;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600241#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500242#ifdef PNG_READ_sBIT_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500243 PNG_sBIT;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600244#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500245#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500246 PNG_sCAL;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600247#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500248#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500249 PNG_sRGB;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600250#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500251#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500252 PNG_sPLT;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600253#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500254#ifdef PNG_READ_tEXt_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500255 PNG_tEXt;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600256#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500257#ifdef PNG_READ_tIME_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500258 PNG_tIME;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600259#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500260#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500261 PNG_tRNS;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600262#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500263#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500264 PNG_zTXt;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600265#endif
Glenn Randers-Pehrsona2567be2009-10-19 20:31:31 -0500266
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600267 /* First we make sure we have enough data for the 4 byte chunk name
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600268 * and the 4 byte chunk length before proceeding with decoding the
269 * chunk data. To fully decode each of these chunks, we also make
270 * sure we have enough data in the buffer for the 4 byte CRC at the
271 * end of every chunk (except IDAT, which is handled separately).
272 */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600273 if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600274 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600275 png_byte chunk_length[4];
Guy Schalnat6d764711995-12-19 03:22:19 -0600276
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600277 if (png_ptr->buffer_size < 8)
278 {
279 png_push_save_buffer(png_ptr);
280 return;
281 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600282
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600283 png_push_fill_buffer(png_ptr, chunk_length, 4);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500284 png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600285 png_reset_crc(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500286 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
Glenn Randers-Pehrsoneb580912008-07-30 14:47:09 -0500287 png_check_chunk_name(png_ptr, png_ptr->chunk_name);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600288 png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600289 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600290
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500291 if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600292 if (png_ptr->mode & PNG_AFTER_IDAT)
293 png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500294
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600295 if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600296 {
Glenn Randers-Pehrsoneb580912008-07-30 14:47:09 -0500297 if (png_ptr->push_length != 13)
298 png_error(png_ptr, "Invalid IHDR length");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500299
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500300 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
301 {
302 png_push_save_buffer(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600303 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500304 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500305
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600306 png_handle_IHDR(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600307 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500308
Glenn Randers-Pehrsond029a752004-08-09 21:50:32 -0500309 else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
310 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500311 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
312 {
313 png_push_save_buffer(png_ptr);
Glenn Randers-Pehrsond029a752004-08-09 21:50:32 -0500314 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500315 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500316
Glenn Randers-Pehrsond029a752004-08-09 21:50:32 -0500317 png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length);
318
319 png_ptr->process_mode = PNG_READ_DONE_MODE;
320 png_push_have_end(png_ptr, info_ptr);
321 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500322
Glenn Randers-Pehrsond029a752004-08-09 21:50:32 -0500323#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
324 else if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name))
325 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500326 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
327 {
328 png_push_save_buffer(png_ptr);
Glenn Randers-Pehrsond029a752004-08-09 21:50:32 -0500329 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500330 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500331
Glenn Randers-Pehrsond029a752004-08-09 21:50:32 -0500332 if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
333 png_ptr->mode |= PNG_HAVE_IDAT;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500334
Glenn Randers-Pehrsond029a752004-08-09 21:50:32 -0500335 png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500336
Glenn Randers-Pehrsond029a752004-08-09 21:50:32 -0500337 if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
338 png_ptr->mode |= PNG_HAVE_PLTE;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500339
Glenn Randers-Pehrsond029a752004-08-09 21:50:32 -0500340 else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
341 {
342 if (!(png_ptr->mode & PNG_HAVE_IHDR))
343 png_error(png_ptr, "Missing IHDR before IDAT");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500344
Glenn Randers-Pehrsond029a752004-08-09 21:50:32 -0500345 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600346 !(png_ptr->mode & PNG_HAVE_PLTE))
Glenn Randers-Pehrsond029a752004-08-09 21:50:32 -0500347 png_error(png_ptr, "Missing PLTE before IDAT");
348 }
349 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500350
Glenn Randers-Pehrsond029a752004-08-09 21:50:32 -0500351#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600352 else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600353 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500354 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
355 {
356 png_push_save_buffer(png_ptr);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600357 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500358 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600359 png_handle_PLTE(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600360 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500361
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500362 else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600363 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600364 /* If we reach an IDAT chunk, this means we have read all of the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600365 * header chunks, and we can start reading the image (or if this
366 * is called after the image has been read - we have an error).
367 */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500368
369 if (!(png_ptr->mode & PNG_HAVE_IHDR))
370 png_error(png_ptr, "Missing IHDR before IDAT");
371
372 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
373 !(png_ptr->mode & PNG_HAVE_PLTE))
374 png_error(png_ptr, "Missing PLTE before IDAT");
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600375
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600376 if (png_ptr->mode & PNG_HAVE_IDAT)
377 {
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500378 if (!(png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500379 if (png_ptr->push_length == 0)
380 return;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600381
382 if (png_ptr->mode & PNG_AFTER_IDAT)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500383 png_benign_error(png_ptr, "Too many IDATs found");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600384 }
385
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600386 png_ptr->idat_size = png_ptr->push_length;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600387 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600388 png_ptr->process_mode = PNG_READ_IDAT_MODE;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600389 png_push_have_info(png_ptr, info_ptr);
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600390 png_ptr->zstream.avail_out =
391 (uInt) PNG_ROWBYTES(png_ptr->pixel_depth,
392 png_ptr->iwidth) + 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600393 png_ptr->zstream.next_out = png_ptr->row_buf;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600394 return;
395 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500396
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500397#ifdef PNG_READ_gAMA_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600398 else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
399 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500400 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
401 {
402 png_push_save_buffer(png_ptr);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600403 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500404 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500405
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600406 png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600407 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500408
Guy Schalnat6d764711995-12-19 03:22:19 -0600409#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500410#ifdef PNG_READ_sBIT_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600411 else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600412 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500413 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
414 {
415 png_push_save_buffer(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600416 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500417 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500418
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600419 png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600420 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500421
Guy Schalnat6d764711995-12-19 03:22:19 -0600422#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500423#ifdef PNG_READ_cHRM_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600424 else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600425 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500426 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
427 {
428 png_push_save_buffer(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600429 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500430 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500431
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600432 png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600433 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500434
Guy Schalnat6d764711995-12-19 03:22:19 -0600435#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500436#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600437 else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
438 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500439 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
440 {
441 png_push_save_buffer(png_ptr);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600442 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500443 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500444
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600445 png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length);
446 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500447
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600448#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500449#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600450 else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4))
451 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500452 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
453 {
454 png_push_save_buffer(png_ptr);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600455 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500456 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500457
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600458 png_handle_iCCP(png_ptr, info_ptr, png_ptr->push_length);
459 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500460
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600461#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500462#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600463 else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4))
464 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500465 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
466 {
467 png_push_save_buffer(png_ptr);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600468 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500469 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500470
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600471 png_handle_sPLT(png_ptr, info_ptr, png_ptr->push_length);
472 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500473
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600474#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500475#ifdef PNG_READ_tRNS_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600476 else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600477 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500478 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
479 {
480 png_push_save_buffer(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600481 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500482 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500483
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600484 png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600485 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500486
Guy Schalnat6d764711995-12-19 03:22:19 -0600487#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500488#ifdef PNG_READ_bKGD_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600489 else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600490 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500491 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
492 {
493 png_push_save_buffer(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600494 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500495 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500496
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600497 png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600498 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500499
Guy Schalnat6d764711995-12-19 03:22:19 -0600500#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500501#ifdef PNG_READ_hIST_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600502 else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600503 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500504 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
505 {
506 png_push_save_buffer(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600507 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500508 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500509
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600510 png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600511 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500512
Guy Schalnat6d764711995-12-19 03:22:19 -0600513#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500514#ifdef PNG_READ_pHYs_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600515 else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600516 {
517 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
518 {
519 png_push_save_buffer(png_ptr);
520 return;
521 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500522
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600523 png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600524 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500525
Guy Schalnat6d764711995-12-19 03:22:19 -0600526#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500527#ifdef PNG_READ_oFFs_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600528 else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600529 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500530 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
531 {
532 png_push_save_buffer(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600533 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500534 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500535
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600536 png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600537 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600538#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500539
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500540#ifdef PNG_READ_pCAL_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500541 else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
542 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500543 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
544 {
545 png_push_save_buffer(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500546 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500547 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500548
Andreas Dilger47a0c421997-05-16 02:46:07 -0500549 png_handle_pCAL(png_ptr, info_ptr, png_ptr->push_length);
550 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500551
Andreas Dilger47a0c421997-05-16 02:46:07 -0500552#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500553#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600554 else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4))
555 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500556 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
557 {
558 png_push_save_buffer(png_ptr);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600559 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500560 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500561
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600562 png_handle_sCAL(png_ptr, info_ptr, png_ptr->push_length);
563 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500564
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600565#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500566#ifdef PNG_READ_tIME_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600567 else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600568 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500569 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
570 {
571 png_push_save_buffer(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600572 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500573 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500574
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600575 png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600576 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500577
Guy Schalnat6d764711995-12-19 03:22:19 -0600578#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500579#ifdef PNG_READ_tEXt_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600580 else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600581 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500582 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
583 {
584 png_push_save_buffer(png_ptr);
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500585 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500586 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500587
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600588 png_push_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600589 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500590
Guy Schalnat6d764711995-12-19 03:22:19 -0600591#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500592#ifdef PNG_READ_zTXt_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600593 else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600594 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500595 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
596 {
597 png_push_save_buffer(png_ptr);
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500598 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500599 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500600
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600601 png_push_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600602 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500603
Guy Schalnat6d764711995-12-19 03:22:19 -0600604#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500605#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600606 else if (!png_memcmp(png_ptr->chunk_name, png_iTXt, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600607 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500608 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
609 {
610 png_push_save_buffer(png_ptr);
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500611 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500612 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500613
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600614 png_push_handle_iTXt(png_ptr, info_ptr, png_ptr->push_length);
615 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500616
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600617#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600618 else
619 {
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500620 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
621 {
622 png_push_save_buffer(png_ptr);
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500623 return;
Glenn Randers-Pehrson79084212008-08-04 13:31:41 -0500624 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600625 png_push_handle_unknown(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600626 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600627
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600628 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
Guy Schalnat6d764711995-12-19 03:22:19 -0600629}
630
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500631void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500632png_push_crc_skip(png_structp png_ptr, png_uint_32 skip)
Guy Schalnat6d764711995-12-19 03:22:19 -0600633{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600634 png_ptr->process_mode = PNG_SKIP_MODE;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500635 png_ptr->skip_length = skip;
Guy Schalnat6d764711995-12-19 03:22:19 -0600636}
637
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500638void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500639png_push_crc_finish(png_structp png_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -0600640{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600641 if (png_ptr->skip_length && png_ptr->save_buffer_size)
642 {
Glenn Randers-Pehrson1408c2a2011-01-16 14:16:28 -0600643 png_size_t save_size = png_ptr->save_buffer_size;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500644 png_uint_32 skip_length = png_ptr->skip_length;
Guy Schalnat6d764711995-12-19 03:22:19 -0600645
John Bowler168a4332011-01-16 19:32:22 -0600646 /* We want the smaller of 'skip_length' and 'save_buffer_size', but
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500647 * they are of different types and we don't know which variable has the
648 * fewest bits. Carefully select the smaller and cast it to the type of
649 * the larger - this cannot overflow. Do not cast in the following test
650 * - it will break on either 16 or 64 bit platforms.
651 */
652 if (skip_length < save_size)
653 save_size = (png_size_t)skip_length;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500654
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600655 else
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500656 skip_length = (png_uint_32)save_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600657
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600658 png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
Guy Schalnat6d764711995-12-19 03:22:19 -0600659
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500660 png_ptr->skip_length -= skip_length;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600661 png_ptr->buffer_size -= save_size;
662 png_ptr->save_buffer_size -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500663 png_ptr->save_buffer_ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600664 }
665 if (png_ptr->skip_length && png_ptr->current_buffer_size)
666 {
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500667 png_size_t save_size = png_ptr->current_buffer_size;
668 png_uint_32 skip_length = png_ptr->skip_length;
Guy Schalnat6d764711995-12-19 03:22:19 -0600669
John Bowler168a4332011-01-16 19:32:22 -0600670 /* We want the smaller of 'skip_length' and 'current_buffer_size', here,
671 * the same problem exists as above and the same solution.
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500672 */
673 if (skip_length < save_size)
674 save_size = (png_size_t)skip_length;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500675
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600676 else
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500677 skip_length = (png_uint_32)save_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600678
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600679 png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
Guy Schalnat6d764711995-12-19 03:22:19 -0600680
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500681 png_ptr->skip_length -= skip_length;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600682 png_ptr->buffer_size -= save_size;
683 png_ptr->current_buffer_size -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500684 png_ptr->current_buffer_ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600685 }
686 if (!png_ptr->skip_length)
687 {
688 if (png_ptr->buffer_size < 4)
689 {
690 png_push_save_buffer(png_ptr);
691 return;
692 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600693
694 png_crc_finish(png_ptr, 0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500695 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600696 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600697}
698
Glenn Randers-Pehrsoneae8e362010-03-12 17:36:53 -0600699void PNGCBAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500700png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length)
Guy Schalnat6d764711995-12-19 03:22:19 -0600701{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600702 png_bytep ptr;
Guy Schalnat6d764711995-12-19 03:22:19 -0600703
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500704 if (png_ptr == NULL)
705 return;
706
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600707 ptr = buffer;
708 if (png_ptr->save_buffer_size)
709 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500710 png_size_t save_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600711
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600712 if (length < png_ptr->save_buffer_size)
713 save_size = length;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500714
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600715 else
716 save_size = png_ptr->save_buffer_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600717
Andreas Dilger47a0c421997-05-16 02:46:07 -0500718 png_memcpy(ptr, png_ptr->save_buffer_ptr, save_size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600719 length -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500720 ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600721 png_ptr->buffer_size -= save_size;
722 png_ptr->save_buffer_size -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500723 png_ptr->save_buffer_ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600724 }
725 if (length && png_ptr->current_buffer_size)
726 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500727 png_size_t save_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600728
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600729 if (length < png_ptr->current_buffer_size)
730 save_size = length;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500731
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600732 else
733 save_size = png_ptr->current_buffer_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600734
Andreas Dilger47a0c421997-05-16 02:46:07 -0500735 png_memcpy(ptr, png_ptr->current_buffer_ptr, save_size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600736 png_ptr->buffer_size -= save_size;
737 png_ptr->current_buffer_size -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500738 png_ptr->current_buffer_ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600739 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600740}
741
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500742void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600743png_push_save_buffer(png_structp png_ptr)
744{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600745 if (png_ptr->save_buffer_size)
746 {
747 if (png_ptr->save_buffer_ptr != png_ptr->save_buffer)
748 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500749 png_size_t i, istop;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600750 png_bytep sp;
751 png_bytep dp;
Guy Schalnat6d764711995-12-19 03:22:19 -0600752
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500753 istop = png_ptr->save_buffer_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600754 for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer;
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600755 i < istop; i++, sp++, dp++)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600756 {
757 *dp = *sp;
758 }
759 }
760 }
Glenn Randers-Pehrsone00ed362010-02-07 22:42:58 -0600761 if (png_ptr->save_buffer_size + png_ptr->current_buffer_size >
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600762 png_ptr->save_buffer_max)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600763 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500764 png_size_t new_max;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600765 png_bytep old_buffer;
Guy Schalnat6d764711995-12-19 03:22:19 -0600766
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600767 if (png_ptr->save_buffer_size > PNG_SIZE_MAX -
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600768 (png_ptr->current_buffer_size + 256))
Glenn Randers-Pehrsone00ed362010-02-07 22:42:58 -0600769 {
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600770 png_error(png_ptr, "Potential overflow of save_buffer");
Glenn Randers-Pehrsone00ed362010-02-07 22:42:58 -0600771 }
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600772
Glenn Randers-Pehrsone00ed362010-02-07 22:42:58 -0600773 new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600774 old_buffer = png_ptr->save_buffer;
Glenn Randers-Pehrsonf1eb9182010-02-08 15:36:41 -0600775 png_ptr->save_buffer = (png_bytep)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600776 (png_size_t)new_max);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500777
Glenn Randers-Pehrsonf1eb9182010-02-08 15:36:41 -0600778 if (png_ptr->save_buffer == NULL)
779 {
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600780 png_free(png_ptr, old_buffer);
781 png_error(png_ptr, "Insufficient memory for save_buffer");
Glenn Randers-Pehrsonf1eb9182010-02-08 15:36:41 -0600782 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500783
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500784 png_memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
785 png_free(png_ptr, old_buffer);
786 png_ptr->save_buffer_max = new_max;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600787 }
788 if (png_ptr->current_buffer_size)
789 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500790 png_memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
791 png_ptr->current_buffer_ptr, png_ptr->current_buffer_size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600792 png_ptr->save_buffer_size += png_ptr->current_buffer_size;
793 png_ptr->current_buffer_size = 0;
794 }
795 png_ptr->save_buffer_ptr = png_ptr->save_buffer;
796 png_ptr->buffer_size = 0;
Guy Schalnat6d764711995-12-19 03:22:19 -0600797}
798
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500799void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600800png_push_restore_buffer(png_structp png_ptr, png_bytep buffer,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500801 png_size_t buffer_length)
Guy Schalnat6d764711995-12-19 03:22:19 -0600802{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600803 png_ptr->current_buffer = buffer;
804 png_ptr->current_buffer_size = buffer_length;
805 png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size;
806 png_ptr->current_buffer_ptr = png_ptr->current_buffer;
Guy Schalnat6d764711995-12-19 03:22:19 -0600807}
808
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500809void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -0500810png_push_read_IDAT(png_structp png_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -0600811{
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500812 PNG_IDAT;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600813 if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600814 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600815 png_byte chunk_length[4];
Guy Schalnat6d764711995-12-19 03:22:19 -0600816
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600817 if (png_ptr->buffer_size < 8)
818 {
819 png_push_save_buffer(png_ptr);
820 return;
821 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600822
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600823 png_push_fill_buffer(png_ptr, chunk_length, 4);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500824 png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600825 png_reset_crc(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500826 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600827 png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600828
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500829 if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600830 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500831 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500832
Guy Schalnate5a37791996-06-05 15:50:50 -0500833 if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600834 png_error(png_ptr, "Not enough compressed data");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500835
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600836 return;
837 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600838
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600839 png_ptr->idat_size = png_ptr->push_length;
840 }
841 if (png_ptr->idat_size && png_ptr->save_buffer_size)
842 {
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500843 png_size_t save_size = png_ptr->save_buffer_size;
844 png_uint_32 idat_size = png_ptr->idat_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600845
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500846 /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
847 * are of different types and we don't know which variable has the fewest
848 * bits. Carefully select the smaller and cast it to the type of the
849 * larger - this cannot overflow. Do not cast in the following test - it
850 * will break on either 16 or 64 bit platforms.
851 */
852 if (idat_size < save_size)
853 save_size = (png_size_t)idat_size;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500854
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600855 else
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500856 idat_size = (png_uint_32)save_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600857
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600858 png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500859
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500860 png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500861
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500862 png_ptr->idat_size -= idat_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600863 png_ptr->buffer_size -= save_size;
864 png_ptr->save_buffer_size -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500865 png_ptr->save_buffer_ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600866 }
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500867
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600868 if (png_ptr->idat_size && png_ptr->current_buffer_size)
869 {
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500870 png_size_t save_size = png_ptr->current_buffer_size;
871 png_uint_32 idat_size = png_ptr->idat_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600872
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500873 /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
874 * are of different types and we don't know which variable has the fewest
875 * bits. Carefully select the smaller and cast it to the type of the
876 * larger - this cannot overflow.
877 */
878 if (idat_size < save_size)
879 save_size = (png_size_t)idat_size;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500880
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600881 else
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500882 idat_size = (png_uint_32)save_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600883
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600884 png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500885
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500886 png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size);
Guy Schalnat6d764711995-12-19 03:22:19 -0600887
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500888 png_ptr->idat_size -= idat_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600889 png_ptr->buffer_size -= save_size;
890 png_ptr->current_buffer_size -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500891 png_ptr->current_buffer_ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600892 }
893 if (!png_ptr->idat_size)
894 {
895 if (png_ptr->buffer_size < 4)
896 {
897 png_push_save_buffer(png_ptr);
898 return;
899 }
900
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600901 png_crc_finish(png_ptr, 0);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600902 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
Glenn Randers-Pehrson859665d2002-08-06 18:06:11 -0500903 png_ptr->mode |= PNG_AFTER_IDAT;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600904 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600905}
906
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500907void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600908png_process_IDAT_data(png_structp png_ptr, png_bytep buffer,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500909 png_size_t buffer_length)
Guy Schalnat6d764711995-12-19 03:22:19 -0600910{
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500911 /* The caller checks for a non-zero buffer length. */
Glenn Randers-Pehrsonef3831a2010-06-22 13:03:32 -0500912 if (!(buffer_length > 0) || buffer == NULL)
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500913 png_error(png_ptr, "No IDAT data (internal error)");
Guy Schalnat6d764711995-12-19 03:22:19 -0600914
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500915 /* This routine must process all the data it has been given
916 * before returning, calling the row callback as required to
Glenn Randers-Pehrson5feb87c2010-06-21 12:28:05 -0500917 * handle the uncompressed results.
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500918 */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600919 png_ptr->zstream.next_in = buffer;
920 png_ptr->zstream.avail_in = (uInt)buffer_length;
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500921
922 /* Keep going until the decompressed data is all processed
923 * or the stream marked as finished.
924 */
925 while (png_ptr->zstream.avail_in > 0 &&
Glenn Randers-Pehrsond9d4cec2010-07-12 07:12:09 -0500926 !(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600927 {
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500928 int ret;
929
930 /* We have data for zlib, but we must check that zlib
Glenn Randers-Pehrsonef3831a2010-06-22 13:03:32 -0500931 * has someplace to put the results. It doesn't matter
Glenn Randers-Pehrson5feb87c2010-06-21 12:28:05 -0500932 * if we don't expect any results -- it may be the input
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500933 * data is just the LZ end code.
934 */
935 if (!(png_ptr->zstream.avail_out > 0))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600936 {
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600937 png_ptr->zstream.avail_out =
938 (uInt) PNG_ROWBYTES(png_ptr->pixel_depth,
939 png_ptr->iwidth) + 1;
Glenn Randers-Pehrson5feb87c2010-06-21 12:28:05 -0500940
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600941 png_ptr->zstream.next_out = png_ptr->row_buf;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600942 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500943
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500944 /* Using Z_SYNC_FLUSH here means that an unterminated
Glenn Randers-Pehrsonef3831a2010-06-22 13:03:32 -0500945 * LZ stream (a stream with a missing end code) can still
946 * be handled, otherwise (Z_NO_FLUSH) a future zlib
947 * implementation might defer output and therefore
948 * change the current behavior (see comments in inflate.c
949 * for why this doesn't happen at present with zlib 1.2.5).
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500950 */
951 ret = inflate(&png_ptr->zstream, Z_SYNC_FLUSH);
952
953 /* Check for any failure before proceeding. */
954 if (ret != Z_OK && ret != Z_STREAM_END)
955 {
Glenn Randers-Pehrsond9d4cec2010-07-12 07:12:09 -0500956 /* Terminate the decompression. */
957 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500958
959 /* This may be a truncated stream (missing or
Glenn Randers-Pehrsond9d4cec2010-07-12 07:12:09 -0500960 * damaged end code). Treat that as a warning.
961 */
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500962 if (png_ptr->row_number >= png_ptr->num_rows ||
Glenn Randers-Pehrsond9d4cec2010-07-12 07:12:09 -0500963 png_ptr->pass > 6)
964 png_warning(png_ptr, "Truncated compressed data in IDAT");
Glenn Randers-Pehrson5feb87c2010-06-21 12:28:05 -0500965
Glenn Randers-Pehrsond9d4cec2010-07-12 07:12:09 -0500966 else
967 png_error(png_ptr, "Decompression error in IDAT");
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500968
Glenn Randers-Pehrsond9d4cec2010-07-12 07:12:09 -0500969 /* Skip the check on unprocessed input */
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500970 return;
971 }
972
973 /* Did inflate output any data? */
974 if (png_ptr->zstream.next_out != png_ptr->row_buf)
975 {
Glenn Randers-Pehrsond9d4cec2010-07-12 07:12:09 -0500976 /* Is this unexpected data after the last row?
977 * If it is, artificially terminate the LZ output
978 * here.
979 */
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500980 if (png_ptr->row_number >= png_ptr->num_rows ||
Glenn Randers-Pehrsond9d4cec2010-07-12 07:12:09 -0500981 png_ptr->pass > 6)
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500982 {
Glenn Randers-Pehrsond9d4cec2010-07-12 07:12:09 -0500983 /* Extra data. */
984 png_warning(png_ptr, "Extra compressed data in IDAT");
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500985 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
Glenn Randers-Pehrson5feb87c2010-06-21 12:28:05 -0500986
Glenn Randers-Pehrsond9d4cec2010-07-12 07:12:09 -0500987 /* Do no more processing; skip the unprocessed
988 * input check below.
989 */
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500990 return;
Glenn Randers-Pehrsond9d4cec2010-07-12 07:12:09 -0500991 }
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500992
Glenn Randers-Pehrsond9d4cec2010-07-12 07:12:09 -0500993 /* Do we have a complete row? */
994 if (png_ptr->zstream.avail_out == 0)
995 png_push_process_row(png_ptr);
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -0500996 }
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500997
998 /* And check for the end of the stream. */
999 if (ret == Z_STREAM_END)
Glenn Randers-Pehrsond9d4cec2010-07-12 07:12:09 -05001000 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001001 }
Glenn Randers-Pehrson57d96172010-06-19 19:25:17 -05001002
1003 /* All the data should have been processed, if anything
1004 * is left at this point we have bytes of IDAT data
1005 * after the zlib end code.
1006 */
1007 if (png_ptr->zstream.avail_in > 0)
Glenn Randers-Pehrson5feb87c2010-06-21 12:28:05 -05001008 png_warning(png_ptr, "Extra compression data in IDAT");
Guy Schalnat6d764711995-12-19 03:22:19 -06001009}
1010
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001011void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001012png_push_process_row(png_structp png_ptr)
1013{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001014 png_ptr->row_info.color_type = png_ptr->color_type;
Guy Schalnat6d764711995-12-19 03:22:19 -06001015 png_ptr->row_info.width = png_ptr->iwidth;
1016 png_ptr->row_info.channels = png_ptr->channels;
1017 png_ptr->row_info.bit_depth = png_ptr->bit_depth;
1018 png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001019
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001020 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
1021 png_ptr->row_info.width);
Guy Schalnat6d764711995-12-19 03:22:19 -06001022
Guy Schalnate5a37791996-06-05 15:50:50 -05001023 png_read_filter_row(png_ptr, &(png_ptr->row_info),
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001024 png_ptr->row_buf + 1, png_ptr->prev_row + 1,
1025 (int)(png_ptr->row_buf[0]));
Guy Schalnat6d764711995-12-19 03:22:19 -06001026
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001027 png_memcpy(png_ptr->prev_row, png_ptr->row_buf, png_ptr->rowbytes + 1);
Guy Schalnat6d764711995-12-19 03:22:19 -06001028
John Bowler9b872f42011-02-12 09:00:16 -06001029 if (png_ptr->transformations)
Guy Schalnat6d764711995-12-19 03:22:19 -06001030 png_do_read_transformations(png_ptr);
1031
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001032#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001033 /* Blow up interlaced rows to full size */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001034 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001035 {
1036 if (png_ptr->pass < 6)
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06001037/* old interface (pre-1.0.9):
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001038 png_do_read_interlace(&(png_ptr->row_info),
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001039 png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06001040 */
1041 png_do_read_interlace(png_ptr);
Guy Schalnat6d764711995-12-19 03:22:19 -06001042
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -06001043 switch (png_ptr->pass)
1044 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001045 case 0:
1046 {
1047 int i;
1048 for (i = 0; i < 8 && png_ptr->pass == 0; i++)
1049 {
1050 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001051 png_read_push_finish_row(png_ptr); /* Updates png_ptr->pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001052 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001053
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001054 if (png_ptr->pass == 2) /* Pass 1 might be empty */
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -06001055 {
1056 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);
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -06001059 png_read_push_finish_row(png_ptr);
1060 }
1061 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001062
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001063 if (png_ptr->pass == 4 && png_ptr->height <= 4)
1064 {
1065 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1066 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001067 png_push_have_row(png_ptr, NULL);
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001068 png_read_push_finish_row(png_ptr);
1069 }
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001070 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001071
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001072 if (png_ptr->pass == 6 && png_ptr->height <= 4)
1073 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001074 png_push_have_row(png_ptr, NULL);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001075 png_read_push_finish_row(png_ptr);
1076 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001077
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001078 break;
1079 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001080
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001081 case 1:
1082 {
1083 int i;
1084 for (i = 0; i < 8 && png_ptr->pass == 1; i++)
1085 {
1086 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1087 png_read_push_finish_row(png_ptr);
1088 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001089
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001090 if (png_ptr->pass == 2) /* Skip top 4 generated rows */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001091 {
1092 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
1093 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001094 png_push_have_row(png_ptr, NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001095 png_read_push_finish_row(png_ptr);
1096 }
1097 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001098
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001099 break;
1100 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001101
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001102 case 2:
1103 {
1104 int i;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001105
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001106 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
1107 {
1108 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1109 png_read_push_finish_row(png_ptr);
1110 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001111
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001112 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
1113 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001114 png_push_have_row(png_ptr, NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001115 png_read_push_finish_row(png_ptr);
1116 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001117
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001118 if (png_ptr->pass == 4) /* Pass 3 might be empty */
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -06001119 {
1120 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1121 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001122 png_push_have_row(png_ptr, NULL);
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -06001123 png_read_push_finish_row(png_ptr);
1124 }
1125 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001126
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001127 break;
1128 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001129
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001130 case 3:
1131 {
1132 int i;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001133
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001134 for (i = 0; i < 4 && png_ptr->pass == 3; i++)
1135 {
1136 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1137 png_read_push_finish_row(png_ptr);
1138 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001139
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001140 if (png_ptr->pass == 4) /* Skip top two generated rows */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001141 {
1142 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1143 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001144 png_push_have_row(png_ptr, NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001145 png_read_push_finish_row(png_ptr);
1146 }
1147 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001148
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001149 break;
1150 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001151
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001152 case 4:
1153 {
1154 int i;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001155
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001156 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1157 {
1158 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1159 png_read_push_finish_row(png_ptr);
1160 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001161
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001162 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1163 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001164 png_push_have_row(png_ptr, NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001165 png_read_push_finish_row(png_ptr);
1166 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001167
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001168 if (png_ptr->pass == 6) /* Pass 5 might be empty */
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -06001169 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001170 png_push_have_row(png_ptr, NULL);
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -06001171 png_read_push_finish_row(png_ptr);
1172 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001173
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001174 break;
1175 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001176
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001177 case 5:
1178 {
1179 int i;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001180
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001181 for (i = 0; i < 2 && png_ptr->pass == 5; i++)
1182 {
1183 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1184 png_read_push_finish_row(png_ptr);
1185 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001186
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001187 if (png_ptr->pass == 6) /* Skip top generated row */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001188 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001189 png_push_have_row(png_ptr, NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001190 png_read_push_finish_row(png_ptr);
1191 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001192
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001193 break;
1194 }
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001195
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -06001196 default:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001197 case 6:
1198 {
1199 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1200 png_read_push_finish_row(png_ptr);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001201
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001202 if (png_ptr->pass != 6)
1203 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001204
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001205 png_push_have_row(png_ptr, NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001206 png_read_push_finish_row(png_ptr);
1207 }
1208 }
1209 }
1210 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001211#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001212 {
1213 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1214 png_read_push_finish_row(png_ptr);
1215 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001216}
1217
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001218void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001219png_read_push_finish_row(png_structp png_ptr)
1220{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001221 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001222
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001223 /* Start of interlace block */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001224 PNG_CONST int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001225
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001226 /* Offset to next interlace block */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001227 PNG_CONST int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001228
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001229 /* Start of interlace block in the y direction */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001230 PNG_CONST int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001231
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001232 /* Offset to next interlace block in the y direction */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001233 PNG_CONST int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001234
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001235 /* Height of interlace block. This is not currently used - if you need
1236 * it, uncomment it here and in png.h
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001237 PNG_CONST int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001238 */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001239
Guy Schalnat6d764711995-12-19 03:22:19 -06001240 png_ptr->row_number++;
1241 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001242 return;
Guy Schalnat6d764711995-12-19 03:22:19 -06001243
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001244#ifdef PNG_READ_INTERLACING_SUPPORTED
Guy Schalnat6d764711995-12-19 03:22:19 -06001245 if (png_ptr->interlaced)
1246 {
1247 png_ptr->row_number = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001248 png_memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
1249
Guy Schalnat6d764711995-12-19 03:22:19 -06001250 do
1251 {
1252 png_ptr->pass++;
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -06001253 if ((png_ptr->pass == 1 && png_ptr->width < 5) ||
1254 (png_ptr->pass == 3 && png_ptr->width < 3) ||
1255 (png_ptr->pass == 5 && png_ptr->width < 2))
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001256 png_ptr->pass++;
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -06001257
Glenn Randers-Pehrson859665d2002-08-06 18:06:11 -05001258 if (png_ptr->pass > 7)
1259 png_ptr->pass--;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001260
Guy Schalnat6d764711995-12-19 03:22:19 -06001261 if (png_ptr->pass >= 7)
1262 break;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001263
Guy Schalnat6d764711995-12-19 03:22:19 -06001264 png_ptr->iwidth = (png_ptr->width +
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001265 png_pass_inc[png_ptr->pass] - 1 -
1266 png_pass_start[png_ptr->pass]) /
1267 png_pass_inc[png_ptr->pass];
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001268
Guy Schalnat6d764711995-12-19 03:22:19 -06001269 if (png_ptr->transformations & PNG_INTERLACE)
1270 break;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001271
1272 png_ptr->num_rows = (png_ptr->height +
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001273 png_pass_yinc[png_ptr->pass] - 1 -
1274 png_pass_ystart[png_ptr->pass]) /
1275 png_pass_yinc[png_ptr->pass];
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001276
1277 } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0);
Guy Schalnat6d764711995-12-19 03:22:19 -06001278 }
Glenn Randers-Pehrsona31c74f2009-05-18 15:52:01 -05001279#endif /* PNG_READ_INTERLACING_SUPPORTED */
Guy Schalnat6d764711995-12-19 03:22:19 -06001280}
1281
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001282#ifdef PNG_READ_tEXt_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001283void /* PRIVATE */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001284png_push_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001285 length)
Guy Schalnat6d764711995-12-19 03:22:19 -06001286{
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001287 if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001288 {
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -06001289 PNG_UNUSED(info_ptr) /* To quiet some compiler warnings */
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001290 png_error(png_ptr, "Out of place tEXt");
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001291 /*NOT REACHED*/
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001292 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001293
Andreas Dilger47a0c421997-05-16 02:46:07 -05001294#ifdef PNG_MAX_MALLOC_64K
1295 png_ptr->skip_length = 0; /* This may not be necessary */
1296
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001297 if (length > (png_uint_32)65535L) /* Can't hold entire string in memory */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001298 {
1299 png_warning(png_ptr, "tEXt chunk too large to fit in memory");
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001300 png_ptr->skip_length = length - (png_uint_32)65535L;
1301 length = (png_uint_32)65535L;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001302 }
1303#endif
1304
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001305 png_ptr->current_text = (png_charp)png_malloc(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001306 (png_size_t)(length + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001307 png_ptr->current_text[length] = '\0';
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001308 png_ptr->current_text_ptr = png_ptr->current_text;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001309 png_ptr->current_text_size = (png_size_t)length;
1310 png_ptr->current_text_left = (png_size_t)length;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001311 png_ptr->process_mode = PNG_READ_tEXt_MODE;
Guy Schalnat6d764711995-12-19 03:22:19 -06001312}
1313
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001314void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001315png_push_read_tEXt(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -06001316{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001317 if (png_ptr->buffer_size && png_ptr->current_text_left)
1318 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001319 png_size_t text_size;
Guy Schalnat6d764711995-12-19 03:22:19 -06001320
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001321 if (png_ptr->buffer_size < png_ptr->current_text_left)
1322 text_size = png_ptr->buffer_size;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001323
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001324 else
1325 text_size = png_ptr->current_text_left;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001326
Andreas Dilger47a0c421997-05-16 02:46:07 -05001327 png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001328 png_ptr->current_text_left -= text_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001329 png_ptr->current_text_ptr += text_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001330 }
1331 if (!(png_ptr->current_text_left))
1332 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001333 png_textp text_ptr;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001334 png_charp text;
1335 png_charp key;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001336 int ret;
Guy Schalnat6d764711995-12-19 03:22:19 -06001337
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001338 if (png_ptr->buffer_size < 4)
1339 {
1340 png_push_save_buffer(png_ptr);
1341 return;
1342 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001343
Andreas Dilger47a0c421997-05-16 02:46:07 -05001344 png_push_crc_finish(png_ptr);
1345
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001346#ifdef PNG_MAX_MALLOC_64K
Andreas Dilger47a0c421997-05-16 02:46:07 -05001347 if (png_ptr->skip_length)
1348 return;
1349#endif
Guy Schalnat6d764711995-12-19 03:22:19 -06001350
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001351 key = png_ptr->current_text;
Guy Schalnat6d764711995-12-19 03:22:19 -06001352
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001353 for (text = key; *text; text++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001354 /* Empty loop */ ;
Guy Schalnat6d764711995-12-19 03:22:19 -06001355
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001356 if (text < key + png_ptr->current_text_size)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001357 text++;
1358
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001359 text_ptr = (png_textp)png_malloc(png_ptr, png_sizeof(png_text));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001360 text_ptr->compression = PNG_TEXT_COMPRESSION_NONE;
1361 text_ptr->key = key;
Glenn Randers-Pehrsondd78d522010-03-30 08:34:02 -05001362 text_ptr->itxt_length = 0;
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001363 text_ptr->lang = NULL;
1364 text_ptr->lang_key = NULL;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001365 text_ptr->text = text;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001366
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001367 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001368
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001369 png_free(png_ptr, key);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001370 png_free(png_ptr, text_ptr);
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001371 png_ptr->current_text = NULL;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001372
1373 if (ret)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001374 png_warning(png_ptr, "Insufficient memory to store text chunk");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001375 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001376}
1377#endif
1378
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001379#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001380void /* PRIVATE */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001381png_push_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
1382 length)
Guy Schalnat6d764711995-12-19 03:22:19 -06001383{
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001384 if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001385 {
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -06001386 PNG_UNUSED(info_ptr) /* To quiet some compiler warnings */
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001387 png_error(png_ptr, "Out of place zTXt");
1388 /*NOT REACHED*/
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001389 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001390
Andreas Dilger47a0c421997-05-16 02:46:07 -05001391#ifdef PNG_MAX_MALLOC_64K
1392 /* We can't handle zTXt chunks > 64K, since we don't have enough space
1393 * to be able to store the uncompressed data. Actually, the threshold
1394 * is probably around 32K, but it isn't as definite as 64K is.
1395 */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001396 if (length > (png_uint_32)65535L)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001397 {
1398 png_warning(png_ptr, "zTXt chunk too large to fit in memory");
1399 png_push_crc_skip(png_ptr, length);
1400 return;
1401 }
1402#endif
1403
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001404 png_ptr->current_text = (png_charp)png_malloc(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001405 (png_size_t)(length + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001406 png_ptr->current_text[length] = '\0';
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001407 png_ptr->current_text_ptr = png_ptr->current_text;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001408 png_ptr->current_text_size = (png_size_t)length;
1409 png_ptr->current_text_left = (png_size_t)length;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001410 png_ptr->process_mode = PNG_READ_zTXt_MODE;
Guy Schalnat6d764711995-12-19 03:22:19 -06001411}
1412
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001413void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001414png_push_read_zTXt(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -06001415{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001416 if (png_ptr->buffer_size && png_ptr->current_text_left)
1417 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001418 png_size_t text_size;
Guy Schalnat6d764711995-12-19 03:22:19 -06001419
Andreas Dilger47a0c421997-05-16 02:46:07 -05001420 if (png_ptr->buffer_size < (png_uint_32)png_ptr->current_text_left)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001421 text_size = png_ptr->buffer_size;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001422
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001423 else
1424 text_size = png_ptr->current_text_left;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001425
Andreas Dilger47a0c421997-05-16 02:46:07 -05001426 png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001427 png_ptr->current_text_left -= text_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001428 png_ptr->current_text_ptr += text_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001429 }
1430 if (!(png_ptr->current_text_left))
1431 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001432 png_textp text_ptr;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001433 png_charp text;
1434 png_charp key;
1435 int ret;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001436 png_size_t text_size, key_size;
Guy Schalnat6d764711995-12-19 03:22:19 -06001437
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001438 if (png_ptr->buffer_size < 4)
1439 {
1440 png_push_save_buffer(png_ptr);
1441 return;
1442 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001443
Andreas Dilger47a0c421997-05-16 02:46:07 -05001444 png_push_crc_finish(png_ptr);
Guy Schalnat6d764711995-12-19 03:22:19 -06001445
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001446 key = png_ptr->current_text;
Guy Schalnat6d764711995-12-19 03:22:19 -06001447
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001448 for (text = key; *text; text++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001449 /* Empty loop */ ;
Guy Schalnat6d764711995-12-19 03:22:19 -06001450
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001451 /* zTXt can't have zero text */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001452 if (text >= key + png_ptr->current_text_size)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001453 {
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001454 png_ptr->current_text = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001455 png_free(png_ptr, key);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001456 return;
1457 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001458
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001459 text++;
Guy Schalnat6d764711995-12-19 03:22:19 -06001460
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001461 if (*text != PNG_TEXT_COMPRESSION_zTXt) /* Check compression byte */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001462 {
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001463 png_ptr->current_text = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001464 png_free(png_ptr, key);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001465 return;
1466 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001467
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001468 text++;
Guy Schalnat6d764711995-12-19 03:22:19 -06001469
Glenn Randers-Pehrsonbc363ec2010-10-12 21:17:00 -05001470 png_ptr->zstream.next_in = (png_bytep)text;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001471 png_ptr->zstream.avail_in = (uInt)(png_ptr->current_text_size -
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001472 (text - key));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001473 png_ptr->zstream.next_out = png_ptr->zbuf;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001474 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnat6d764711995-12-19 03:22:19 -06001475
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001476 key_size = text - key;
1477 text_size = 0;
1478 text = NULL;
1479 ret = Z_STREAM_END;
Guy Schalnat6d764711995-12-19 03:22:19 -06001480
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001481 while (png_ptr->zstream.avail_in)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001482 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001483 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001484 if (ret != Z_OK && ret != Z_STREAM_END)
1485 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001486 inflateReset(&png_ptr->zstream);
1487 png_ptr->zstream.avail_in = 0;
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001488 png_ptr->current_text = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001489 png_free(png_ptr, key);
1490 png_free(png_ptr, text);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001491 return;
1492 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001493
Andreas Dilger47a0c421997-05-16 02:46:07 -05001494 if (!(png_ptr->zstream.avail_out) || ret == Z_STREAM_END)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001495 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001496 if (text == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001497 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001498 text = (png_charp)png_malloc(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001499 (png_ptr->zbuf_size
1500 - png_ptr->zstream.avail_out + key_size + 1));
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001501
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001502 png_memcpy(text + key_size, png_ptr->zbuf,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001503 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001504
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001505 png_memcpy(text, key, key_size);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001506
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001507 text_size = key_size + png_ptr->zbuf_size -
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001508 png_ptr->zstream.avail_out;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001509
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001510 *(text + text_size) = '\0';
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001511 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001512
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001513 else
1514 {
1515 png_charp tmp;
Guy Schalnat6d764711995-12-19 03:22:19 -06001516
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001517 tmp = text;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001518 text = (png_charp)png_malloc(png_ptr, text_size +
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -05001519 (png_ptr->zbuf_size
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001520 - png_ptr->zstream.avail_out + 1));
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001521
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001522 png_memcpy(text, tmp, text_size);
1523 png_free(png_ptr, tmp);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001524
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001525 png_memcpy(text + text_size, png_ptr->zbuf,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001526 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001527
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001528 text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out;
1529 *(text + text_size) = '\0';
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001530 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001531
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001532 if (ret != Z_STREAM_END)
1533 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001534 png_ptr->zstream.next_out = png_ptr->zbuf;
1535 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001536 }
1537 }
1538 else
1539 {
1540 break;
1541 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001542
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001543 if (ret == Z_STREAM_END)
1544 break;
1545 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001546
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001547 inflateReset(&png_ptr->zstream);
1548 png_ptr->zstream.avail_in = 0;
Guy Schalnat6d764711995-12-19 03:22:19 -06001549
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001550 if (ret != Z_STREAM_END)
1551 {
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001552 png_ptr->current_text = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001553 png_free(png_ptr, key);
1554 png_free(png_ptr, text);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001555 return;
1556 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001557
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001558 png_ptr->current_text = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001559 png_free(png_ptr, key);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001560 key = text;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001561 text += key_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001562
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001563 text_ptr = (png_textp)png_malloc(png_ptr,
1564 png_sizeof(png_text));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001565 text_ptr->compression = PNG_TEXT_COMPRESSION_zTXt;
1566 text_ptr->key = key;
Glenn Randers-Pehrsondd78d522010-03-30 08:34:02 -05001567 text_ptr->itxt_length = 0;
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001568 text_ptr->lang = NULL;
1569 text_ptr->lang_key = NULL;
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001570 text_ptr->text = text;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001571
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001572 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001573
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001574 png_free(png_ptr, key);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001575 png_free(png_ptr, text_ptr);
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001576
1577 if (ret)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001578 png_warning(png_ptr, "Insufficient memory to store text chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001579 }
1580}
1581#endif
1582
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001583#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001584void /* PRIVATE */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001585png_push_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001586 length)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001587{
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001588 if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001589 {
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -06001590 PNG_UNUSED(info_ptr) /* To quiet some compiler warnings */
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001591 png_error(png_ptr, "Out of place iTXt");
1592 /*NOT REACHED*/
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001593 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001594
1595#ifdef PNG_MAX_MALLOC_64K
1596 png_ptr->skip_length = 0; /* This may not be necessary */
1597
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001598 if (length > (png_uint_32)65535L) /* Can't hold entire string in memory */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001599 {
1600 png_warning(png_ptr, "iTXt chunk too large to fit in memory");
1601 png_ptr->skip_length = length - (png_uint_32)65535L;
1602 length = (png_uint_32)65535L;
1603 }
1604#endif
1605
1606 png_ptr->current_text = (png_charp)png_malloc(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001607 (png_size_t)(length + 1));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001608 png_ptr->current_text[length] = '\0';
1609 png_ptr->current_text_ptr = png_ptr->current_text;
1610 png_ptr->current_text_size = (png_size_t)length;
1611 png_ptr->current_text_left = (png_size_t)length;
1612 png_ptr->process_mode = PNG_READ_iTXt_MODE;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001613}
1614
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001615void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001616png_push_read_iTXt(png_structp png_ptr, png_infop info_ptr)
1617{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001618
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001619 if (png_ptr->buffer_size && png_ptr->current_text_left)
1620 {
1621 png_size_t text_size;
1622
1623 if (png_ptr->buffer_size < png_ptr->current_text_left)
1624 text_size = png_ptr->buffer_size;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001625
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001626 else
1627 text_size = png_ptr->current_text_left;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001628
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001629 png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
1630 png_ptr->current_text_left -= text_size;
1631 png_ptr->current_text_ptr += text_size;
1632 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001633
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001634 if (!(png_ptr->current_text_left))
1635 {
1636 png_textp text_ptr;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001637 png_charp key;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001638 int comp_flag;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001639 png_charp lang;
1640 png_charp lang_key;
1641 png_charp text;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001642 int ret;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001643
1644 if (png_ptr->buffer_size < 4)
1645 {
1646 png_push_save_buffer(png_ptr);
1647 return;
1648 }
1649
1650 png_push_crc_finish(png_ptr);
1651
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001652#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001653 if (png_ptr->skip_length)
1654 return;
1655#endif
1656
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001657 key = png_ptr->current_text;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001658
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001659 for (lang = key; *lang; lang++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001660 /* Empty loop */ ;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001661
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001662 if (lang < key + png_ptr->current_text_size - 3)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001663 lang++;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001664
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001665 comp_flag = *lang++;
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001666 lang++; /* Skip comp_type, always zero */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001667
1668 for (lang_key = lang; *lang_key; lang_key++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001669 /* Empty loop */ ;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001670
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001671 lang_key++; /* Skip NUL separator */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001672
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001673 text=lang_key;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001674
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001675 if (lang_key < key + png_ptr->current_text_size - 1)
1676 {
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001677 for (; *text; text++)
1678 /* Empty loop */ ;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001679 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001680
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001681 if (text < key + png_ptr->current_text_size)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001682 text++;
1683
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001684 text_ptr = (png_textp)png_malloc(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001685 png_sizeof(png_text));
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001686
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001687 text_ptr->compression = comp_flag + 2;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001688 text_ptr->key = key;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001689 text_ptr->lang = lang;
1690 text_ptr->lang_key = lang_key;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001691 text_ptr->text = text;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001692 text_ptr->text_length = 0;
1693 text_ptr->itxt_length = png_strlen(text);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001694
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001695 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001696
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001697 png_ptr->current_text = NULL;
1698
Andreas Dilger47a0c421997-05-16 02:46:07 -05001699 png_free(png_ptr, text_ptr);
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001700 if (ret)
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -06001701 png_warning(png_ptr, "Insufficient memory to store iTXt chunk");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001702 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001703}
1704#endif
1705
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001706/* This function is called when we haven't found a handler for this
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001707 * chunk. If there isn't a problem with the chunk itself (ie a bad chunk
1708 * name or a critical chunk), the chunk is (currently) silently ignored.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001709 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001710void /* PRIVATE */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001711png_push_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001712 length)
Guy Schalnat6d764711995-12-19 03:22:19 -06001713{
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -05001714 png_uint_32 skip = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001715
1716 if (!(png_ptr->chunk_name[0] & 0x20))
1717 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001718#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001719 if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001720 PNG_HANDLE_CHUNK_ALWAYS
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001721#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001722 && png_ptr->read_user_chunk_fn == NULL
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001723#endif
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001724 )
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001725#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001726 png_chunk_error(png_ptr, "unknown critical chunk");
1727
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -06001728 PNG_UNUSED(info_ptr) /* To quiet some compiler warnings */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001729 }
1730
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001731#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001732 if (png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS)
1733 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001734#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001735 if (length > (png_uint_32)65535L)
1736 {
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001737 png_warning(png_ptr, "unknown chunk too large to fit in memory");
1738 skip = length - (png_uint_32)65535L;
1739 length = (png_uint_32)65535L;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001740 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001741#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001742 png_memcpy((png_charp)png_ptr->unknown_chunk.name,
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -05001743 (png_charp)png_ptr->chunk_name,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001744 png_sizeof(png_ptr->unknown_chunk.name));
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -05001745 png_ptr->unknown_chunk.name[png_sizeof(png_ptr->unknown_chunk.name) - 1]
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001746 = '\0';
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001747
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001748 png_ptr->unknown_chunk.size = (png_size_t)length;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001749
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001750 if (length == 0)
1751 png_ptr->unknown_chunk.data = NULL;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001752
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001753 else
1754 {
1755 png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001756 (png_size_t)length);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001757 png_crc_read(png_ptr, (png_bytep)png_ptr->unknown_chunk.data, length);
1758 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001759
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001760#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001761 if (png_ptr->read_user_chunk_fn != NULL)
1762 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001763 /* Callback to user unknown chunk handler */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001764 int ret;
1765 ret = (*(png_ptr->read_user_chunk_fn))
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001766 (png_ptr, &png_ptr->unknown_chunk);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001767
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001768 if (ret < 0)
1769 png_chunk_error(png_ptr, "error in user chunk");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001770
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001771 if (ret == 0)
1772 {
1773 if (!(png_ptr->chunk_name[0] & 0x20))
1774 if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001775 PNG_HANDLE_CHUNK_ALWAYS)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001776 png_chunk_error(png_ptr, "unknown critical chunk");
1777 png_set_unknown_chunks(png_ptr, info_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001778 &png_ptr->unknown_chunk, 1);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001779 }
1780 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001781
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001782 else
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001783#endif
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001784 png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001785 png_free(png_ptr, png_ptr->unknown_chunk.data);
1786 png_ptr->unknown_chunk.data = NULL;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001787 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001788
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001789 else
1790#endif
1791 skip=length;
1792 png_push_crc_skip(png_ptr, skip);
Guy Schalnat6d764711995-12-19 03:22:19 -06001793}
1794
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001795void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001796png_push_have_info(png_structp png_ptr, png_infop info_ptr)
1797{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001798 if (png_ptr->info_fn != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001799 (*(png_ptr->info_fn))(png_ptr, info_ptr);
1800}
1801
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001802void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001803png_push_have_end(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -06001804{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001805 if (png_ptr->end_fn != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001806 (*(png_ptr->end_fn))(png_ptr, info_ptr);
Guy Schalnat6d764711995-12-19 03:22:19 -06001807}
1808
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001809void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001810png_push_have_row(png_structp png_ptr, png_bytep row)
1811{
Glenn Randers-Pehrson78d55452010-06-19 07:03:50 -05001812 if (png_ptr->row_fn != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001813 (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number,
1814 (int)png_ptr->pass);
Guy Schalnat6d764711995-12-19 03:22:19 -06001815}
1816
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001817void PNGAPI
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001818png_progressive_combine_row (png_structp png_ptr, png_bytep old_row,
1819 png_const_bytep new_row)
Guy Schalnat6d764711995-12-19 03:22:19 -06001820{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001821 PNG_CONST int FARDATA png_pass_dsp_mask[7] =
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001822 {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
Glenn Randers-Pehrson86312502009-08-31 14:17:23 -05001823
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001824 if (png_ptr == NULL)
1825 return;
1826
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -05001827 if (new_row != NULL) /* new_row must == png_ptr->row_buf here. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001828 png_combine_row(png_ptr, old_row, png_pass_dsp_mask[png_ptr->pass]);
Guy Schalnat6d764711995-12-19 03:22:19 -06001829}
1830
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001831void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001832png_set_progressive_read_fn(png_structp png_ptr, png_voidp progressive_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001833 png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
1834 png_progressive_end_ptr end_fn)
Guy Schalnat6d764711995-12-19 03:22:19 -06001835{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001836 if (png_ptr == NULL)
1837 return;
1838
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001839 png_ptr->info_fn = info_fn;
1840 png_ptr->row_fn = row_fn;
Guy Schalnat6d764711995-12-19 03:22:19 -06001841 png_ptr->end_fn = end_fn;
Guy Schalnate5a37791996-06-05 15:50:50 -05001842
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -05001843 png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer);
Guy Schalnat6d764711995-12-19 03:22:19 -06001844}
1845
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001846png_voidp PNGAPI
John Bowler0a5c9c02011-01-22 17:36:34 -06001847png_get_progressive_ptr(png_const_structp png_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -06001848{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001849 if (png_ptr == NULL)
1850 return (NULL);
1851
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001852 return png_ptr->io_ptr;
Guy Schalnat6d764711995-12-19 03:22:19 -06001853}
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001854#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */