blob: a50292a76ad6113ae57413914461d2dd2c48f0c6 [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-Pehrsondcc35052011-03-31 11:23:49 -05004 * Last changed in libpng 1.5.2 [March 31, 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 Bowler4a12f4a2011-04-17 18:34:22 -05001029#ifdef PNG_READ_TRANSFORMS_SUPPORTED
John Bowler9b872f42011-02-12 09:00:16 -06001030 if (png_ptr->transformations)
Guy Schalnat6d764711995-12-19 03:22:19 -06001031 png_do_read_transformations(png_ptr);
John Bowler4a12f4a2011-04-17 18:34:22 -05001032#endif
Guy Schalnat6d764711995-12-19 03:22:19 -06001033
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001034#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001035 /* Blow up interlaced rows to full size */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001036 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001037 {
1038 if (png_ptr->pass < 6)
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06001039/* old interface (pre-1.0.9):
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001040 png_do_read_interlace(&(png_ptr->row_info),
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001041 png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06001042 */
1043 png_do_read_interlace(png_ptr);
Guy Schalnat6d764711995-12-19 03:22:19 -06001044
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -06001045 switch (png_ptr->pass)
1046 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001047 case 0:
1048 {
1049 int i;
1050 for (i = 0; i < 8 && png_ptr->pass == 0; i++)
1051 {
1052 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001053 png_read_push_finish_row(png_ptr); /* Updates png_ptr->pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001054 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001055
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001056 if (png_ptr->pass == 2) /* Pass 1 might be empty */
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -06001057 {
1058 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
1059 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001060 png_push_have_row(png_ptr, NULL);
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -06001061 png_read_push_finish_row(png_ptr);
1062 }
1063 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001064
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001065 if (png_ptr->pass == 4 && png_ptr->height <= 4)
1066 {
1067 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1068 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001069 png_push_have_row(png_ptr, NULL);
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001070 png_read_push_finish_row(png_ptr);
1071 }
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001072 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001073
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001074 if (png_ptr->pass == 6 && png_ptr->height <= 4)
1075 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001076 png_push_have_row(png_ptr, NULL);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001077 png_read_push_finish_row(png_ptr);
1078 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001079
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001080 break;
1081 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001082
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001083 case 1:
1084 {
1085 int i;
1086 for (i = 0; i < 8 && png_ptr->pass == 1; i++)
1087 {
1088 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1089 png_read_push_finish_row(png_ptr);
1090 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001091
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001092 if (png_ptr->pass == 2) /* Skip top 4 generated rows */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001093 {
1094 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
1095 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001096 png_push_have_row(png_ptr, NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001097 png_read_push_finish_row(png_ptr);
1098 }
1099 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001100
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001101 break;
1102 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001103
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001104 case 2:
1105 {
1106 int i;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001107
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001108 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
1109 {
1110 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1111 png_read_push_finish_row(png_ptr);
1112 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001113
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001114 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
1115 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001116 png_push_have_row(png_ptr, NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001117 png_read_push_finish_row(png_ptr);
1118 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001119
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001120 if (png_ptr->pass == 4) /* Pass 3 might be empty */
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -06001121 {
1122 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1123 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001124 png_push_have_row(png_ptr, NULL);
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -06001125 png_read_push_finish_row(png_ptr);
1126 }
1127 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001128
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001129 break;
1130 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001131
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001132 case 3:
1133 {
1134 int i;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001135
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001136 for (i = 0; i < 4 && png_ptr->pass == 3; i++)
1137 {
1138 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1139 png_read_push_finish_row(png_ptr);
1140 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001141
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001142 if (png_ptr->pass == 4) /* Skip top two generated rows */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001143 {
1144 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1145 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001146 png_push_have_row(png_ptr, NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001147 png_read_push_finish_row(png_ptr);
1148 }
1149 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001150
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001151 break;
1152 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001153
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001154 case 4:
1155 {
1156 int i;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001157
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001158 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1159 {
1160 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1161 png_read_push_finish_row(png_ptr);
1162 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001163
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001164 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1165 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001166 png_push_have_row(png_ptr, NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001167 png_read_push_finish_row(png_ptr);
1168 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001169
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001170 if (png_ptr->pass == 6) /* Pass 5 might be empty */
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -06001171 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001172 png_push_have_row(png_ptr, NULL);
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -06001173 png_read_push_finish_row(png_ptr);
1174 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001175
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001176 break;
1177 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001178
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001179 case 5:
1180 {
1181 int i;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001182
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001183 for (i = 0; i < 2 && png_ptr->pass == 5; i++)
1184 {
1185 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1186 png_read_push_finish_row(png_ptr);
1187 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001188
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001189 if (png_ptr->pass == 6) /* Skip top generated row */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001190 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001191 png_push_have_row(png_ptr, NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001192 png_read_push_finish_row(png_ptr);
1193 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001194
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001195 break;
1196 }
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001197
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -06001198 default:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001199 case 6:
1200 {
1201 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1202 png_read_push_finish_row(png_ptr);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001203
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001204 if (png_ptr->pass != 6)
1205 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001206
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001207 png_push_have_row(png_ptr, NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001208 png_read_push_finish_row(png_ptr);
1209 }
1210 }
1211 }
1212 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001213#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001214 {
1215 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1216 png_read_push_finish_row(png_ptr);
1217 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001218}
1219
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001220void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001221png_read_push_finish_row(png_structp png_ptr)
1222{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001223 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001224
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001225 /* Start of interlace block */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001226 PNG_CONST int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001227
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001228 /* Offset to next interlace block */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001229 PNG_CONST int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001230
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001231 /* Start of interlace block in the y direction */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001232 PNG_CONST int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001233
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001234 /* Offset to next interlace block in the y direction */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001235 PNG_CONST int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001236
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001237 /* Height of interlace block. This is not currently used - if you need
1238 * it, uncomment it here and in png.h
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001239 PNG_CONST int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001240 */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001241
Guy Schalnat6d764711995-12-19 03:22:19 -06001242 png_ptr->row_number++;
1243 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001244 return;
Guy Schalnat6d764711995-12-19 03:22:19 -06001245
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001246#ifdef PNG_READ_INTERLACING_SUPPORTED
Guy Schalnat6d764711995-12-19 03:22:19 -06001247 if (png_ptr->interlaced)
1248 {
1249 png_ptr->row_number = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001250 png_memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
1251
Guy Schalnat6d764711995-12-19 03:22:19 -06001252 do
1253 {
1254 png_ptr->pass++;
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -06001255 if ((png_ptr->pass == 1 && png_ptr->width < 5) ||
1256 (png_ptr->pass == 3 && png_ptr->width < 3) ||
1257 (png_ptr->pass == 5 && png_ptr->width < 2))
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001258 png_ptr->pass++;
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -06001259
Glenn Randers-Pehrson859665d2002-08-06 18:06:11 -05001260 if (png_ptr->pass > 7)
1261 png_ptr->pass--;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001262
Guy Schalnat6d764711995-12-19 03:22:19 -06001263 if (png_ptr->pass >= 7)
1264 break;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001265
Guy Schalnat6d764711995-12-19 03:22:19 -06001266 png_ptr->iwidth = (png_ptr->width +
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001267 png_pass_inc[png_ptr->pass] - 1 -
1268 png_pass_start[png_ptr->pass]) /
1269 png_pass_inc[png_ptr->pass];
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001270
Guy Schalnat6d764711995-12-19 03:22:19 -06001271 if (png_ptr->transformations & PNG_INTERLACE)
1272 break;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001273
1274 png_ptr->num_rows = (png_ptr->height +
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001275 png_pass_yinc[png_ptr->pass] - 1 -
1276 png_pass_ystart[png_ptr->pass]) /
1277 png_pass_yinc[png_ptr->pass];
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001278
1279 } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0);
Guy Schalnat6d764711995-12-19 03:22:19 -06001280 }
Glenn Randers-Pehrsona31c74f2009-05-18 15:52:01 -05001281#endif /* PNG_READ_INTERLACING_SUPPORTED */
Guy Schalnat6d764711995-12-19 03:22:19 -06001282}
1283
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001284#ifdef PNG_READ_tEXt_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001285void /* PRIVATE */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001286png_push_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001287 length)
Guy Schalnat6d764711995-12-19 03:22:19 -06001288{
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001289 if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001290 {
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -06001291 PNG_UNUSED(info_ptr) /* To quiet some compiler warnings */
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001292 png_error(png_ptr, "Out of place tEXt");
Glenn Randers-Pehrsonb75b2412011-04-16 19:35:05 -05001293 /* NOT REACHED */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001294 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001295
Andreas Dilger47a0c421997-05-16 02:46:07 -05001296#ifdef PNG_MAX_MALLOC_64K
1297 png_ptr->skip_length = 0; /* This may not be necessary */
1298
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001299 if (length > (png_uint_32)65535L) /* Can't hold entire string in memory */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001300 {
1301 png_warning(png_ptr, "tEXt chunk too large to fit in memory");
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001302 png_ptr->skip_length = length - (png_uint_32)65535L;
1303 length = (png_uint_32)65535L;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001304 }
1305#endif
1306
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001307 png_ptr->current_text = (png_charp)png_malloc(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001308 (png_size_t)(length + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001309 png_ptr->current_text[length] = '\0';
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001310 png_ptr->current_text_ptr = png_ptr->current_text;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001311 png_ptr->current_text_size = (png_size_t)length;
1312 png_ptr->current_text_left = (png_size_t)length;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001313 png_ptr->process_mode = PNG_READ_tEXt_MODE;
Guy Schalnat6d764711995-12-19 03:22:19 -06001314}
1315
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001316void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001317png_push_read_tEXt(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -06001318{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001319 if (png_ptr->buffer_size && png_ptr->current_text_left)
1320 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001321 png_size_t text_size;
Guy Schalnat6d764711995-12-19 03:22:19 -06001322
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001323 if (png_ptr->buffer_size < png_ptr->current_text_left)
1324 text_size = png_ptr->buffer_size;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001325
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001326 else
1327 text_size = png_ptr->current_text_left;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001328
Andreas Dilger47a0c421997-05-16 02:46:07 -05001329 png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001330 png_ptr->current_text_left -= text_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001331 png_ptr->current_text_ptr += text_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001332 }
1333 if (!(png_ptr->current_text_left))
1334 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001335 png_textp text_ptr;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001336 png_charp text;
1337 png_charp key;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001338 int ret;
Guy Schalnat6d764711995-12-19 03:22:19 -06001339
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001340 if (png_ptr->buffer_size < 4)
1341 {
1342 png_push_save_buffer(png_ptr);
1343 return;
1344 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001345
Andreas Dilger47a0c421997-05-16 02:46:07 -05001346 png_push_crc_finish(png_ptr);
1347
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001348#ifdef PNG_MAX_MALLOC_64K
Andreas Dilger47a0c421997-05-16 02:46:07 -05001349 if (png_ptr->skip_length)
1350 return;
1351#endif
Guy Schalnat6d764711995-12-19 03:22:19 -06001352
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001353 key = png_ptr->current_text;
Guy Schalnat6d764711995-12-19 03:22:19 -06001354
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001355 for (text = key; *text; text++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001356 /* Empty loop */ ;
Guy Schalnat6d764711995-12-19 03:22:19 -06001357
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001358 if (text < key + png_ptr->current_text_size)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001359 text++;
1360
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001361 text_ptr = (png_textp)png_malloc(png_ptr, png_sizeof(png_text));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001362 text_ptr->compression = PNG_TEXT_COMPRESSION_NONE;
1363 text_ptr->key = key;
Glenn Randers-Pehrsondd78d522010-03-30 08:34:02 -05001364 text_ptr->itxt_length = 0;
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001365 text_ptr->lang = NULL;
1366 text_ptr->lang_key = NULL;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001367 text_ptr->text = text;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001368
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001369 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001370
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001371 png_free(png_ptr, key);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001372 png_free(png_ptr, text_ptr);
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001373 png_ptr->current_text = NULL;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001374
1375 if (ret)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001376 png_warning(png_ptr, "Insufficient memory to store text chunk");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001377 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001378}
1379#endif
1380
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001381#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001382void /* PRIVATE */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001383png_push_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
1384 length)
Guy Schalnat6d764711995-12-19 03:22:19 -06001385{
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001386 if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001387 {
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -06001388 PNG_UNUSED(info_ptr) /* To quiet some compiler warnings */
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001389 png_error(png_ptr, "Out of place zTXt");
Glenn Randers-Pehrsonb75b2412011-04-16 19:35:05 -05001390 /* NOT REACHED */
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001391 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001392
Andreas Dilger47a0c421997-05-16 02:46:07 -05001393#ifdef PNG_MAX_MALLOC_64K
1394 /* We can't handle zTXt chunks > 64K, since we don't have enough space
1395 * to be able to store the uncompressed data. Actually, the threshold
1396 * is probably around 32K, but it isn't as definite as 64K is.
1397 */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001398 if (length > (png_uint_32)65535L)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001399 {
1400 png_warning(png_ptr, "zTXt chunk too large to fit in memory");
1401 png_push_crc_skip(png_ptr, length);
1402 return;
1403 }
1404#endif
1405
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001406 png_ptr->current_text = (png_charp)png_malloc(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001407 (png_size_t)(length + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001408 png_ptr->current_text[length] = '\0';
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001409 png_ptr->current_text_ptr = png_ptr->current_text;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001410 png_ptr->current_text_size = (png_size_t)length;
1411 png_ptr->current_text_left = (png_size_t)length;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001412 png_ptr->process_mode = PNG_READ_zTXt_MODE;
Guy Schalnat6d764711995-12-19 03:22:19 -06001413}
1414
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001415void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001416png_push_read_zTXt(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -06001417{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001418 if (png_ptr->buffer_size && png_ptr->current_text_left)
1419 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001420 png_size_t text_size;
Guy Schalnat6d764711995-12-19 03:22:19 -06001421
Andreas Dilger47a0c421997-05-16 02:46:07 -05001422 if (png_ptr->buffer_size < (png_uint_32)png_ptr->current_text_left)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001423 text_size = png_ptr->buffer_size;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001424
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001425 else
1426 text_size = png_ptr->current_text_left;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001427
Andreas Dilger47a0c421997-05-16 02:46:07 -05001428 png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001429 png_ptr->current_text_left -= text_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001430 png_ptr->current_text_ptr += text_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001431 }
1432 if (!(png_ptr->current_text_left))
1433 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001434 png_textp text_ptr;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001435 png_charp text;
1436 png_charp key;
1437 int ret;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001438 png_size_t text_size, key_size;
Guy Schalnat6d764711995-12-19 03:22:19 -06001439
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001440 if (png_ptr->buffer_size < 4)
1441 {
1442 png_push_save_buffer(png_ptr);
1443 return;
1444 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001445
Andreas Dilger47a0c421997-05-16 02:46:07 -05001446 png_push_crc_finish(png_ptr);
Guy Schalnat6d764711995-12-19 03:22:19 -06001447
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001448 key = png_ptr->current_text;
Guy Schalnat6d764711995-12-19 03:22:19 -06001449
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001450 for (text = key; *text; text++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001451 /* Empty loop */ ;
Guy Schalnat6d764711995-12-19 03:22:19 -06001452
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001453 /* zTXt can't have zero text */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001454 if (text >= key + png_ptr->current_text_size)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001455 {
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001456 png_ptr->current_text = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001457 png_free(png_ptr, key);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001458 return;
1459 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001460
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001461 text++;
Guy Schalnat6d764711995-12-19 03:22:19 -06001462
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001463 if (*text != PNG_TEXT_COMPRESSION_zTXt) /* Check compression byte */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001464 {
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001465 png_ptr->current_text = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001466 png_free(png_ptr, key);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001467 return;
1468 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001469
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001470 text++;
Guy Schalnat6d764711995-12-19 03:22:19 -06001471
Glenn Randers-Pehrsonbc363ec2010-10-12 21:17:00 -05001472 png_ptr->zstream.next_in = (png_bytep)text;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001473 png_ptr->zstream.avail_in = (uInt)(png_ptr->current_text_size -
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001474 (text - key));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001475 png_ptr->zstream.next_out = png_ptr->zbuf;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001476 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnat6d764711995-12-19 03:22:19 -06001477
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001478 key_size = text - key;
1479 text_size = 0;
1480 text = NULL;
1481 ret = Z_STREAM_END;
Guy Schalnat6d764711995-12-19 03:22:19 -06001482
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001483 while (png_ptr->zstream.avail_in)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001484 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001485 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001486 if (ret != Z_OK && ret != Z_STREAM_END)
1487 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001488 inflateReset(&png_ptr->zstream);
1489 png_ptr->zstream.avail_in = 0;
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001490 png_ptr->current_text = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001491 png_free(png_ptr, key);
1492 png_free(png_ptr, text);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001493 return;
1494 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001495
Andreas Dilger47a0c421997-05-16 02:46:07 -05001496 if (!(png_ptr->zstream.avail_out) || ret == Z_STREAM_END)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001497 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001498 if (text == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001499 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001500 text = (png_charp)png_malloc(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001501 (png_ptr->zbuf_size
1502 - png_ptr->zstream.avail_out + key_size + 1));
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001503
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001504 png_memcpy(text + key_size, png_ptr->zbuf,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001505 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001506
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001507 png_memcpy(text, key, key_size);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001508
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001509 text_size = key_size + png_ptr->zbuf_size -
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001510 png_ptr->zstream.avail_out;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001511
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001512 *(text + text_size) = '\0';
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001513 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001514
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001515 else
1516 {
1517 png_charp tmp;
Guy Schalnat6d764711995-12-19 03:22:19 -06001518
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001519 tmp = text;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001520 text = (png_charp)png_malloc(png_ptr, text_size +
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -05001521 (png_ptr->zbuf_size
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001522 - png_ptr->zstream.avail_out + 1));
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001523
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001524 png_memcpy(text, tmp, text_size);
1525 png_free(png_ptr, tmp);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001526
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001527 png_memcpy(text + text_size, png_ptr->zbuf,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001528 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001529
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001530 text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out;
1531 *(text + text_size) = '\0';
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001532 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001533
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001534 if (ret != Z_STREAM_END)
1535 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001536 png_ptr->zstream.next_out = png_ptr->zbuf;
1537 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001538 }
1539 }
1540 else
1541 {
1542 break;
1543 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001544
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001545 if (ret == Z_STREAM_END)
1546 break;
1547 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001548
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001549 inflateReset(&png_ptr->zstream);
1550 png_ptr->zstream.avail_in = 0;
Guy Schalnat6d764711995-12-19 03:22:19 -06001551
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001552 if (ret != Z_STREAM_END)
1553 {
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001554 png_ptr->current_text = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001555 png_free(png_ptr, key);
1556 png_free(png_ptr, text);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001557 return;
1558 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001559
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001560 png_ptr->current_text = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001561 png_free(png_ptr, key);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001562 key = text;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001563 text += key_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001564
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001565 text_ptr = (png_textp)png_malloc(png_ptr,
1566 png_sizeof(png_text));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001567 text_ptr->compression = PNG_TEXT_COMPRESSION_zTXt;
1568 text_ptr->key = key;
Glenn Randers-Pehrsondd78d522010-03-30 08:34:02 -05001569 text_ptr->itxt_length = 0;
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001570 text_ptr->lang = NULL;
1571 text_ptr->lang_key = NULL;
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001572 text_ptr->text = text;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001573
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001574 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001575
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001576 png_free(png_ptr, key);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001577 png_free(png_ptr, text_ptr);
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001578
1579 if (ret)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001580 png_warning(png_ptr, "Insufficient memory to store text chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001581 }
1582}
1583#endif
1584
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001585#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001586void /* PRIVATE */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001587png_push_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001588 length)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001589{
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001590 if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001591 {
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -06001592 PNG_UNUSED(info_ptr) /* To quiet some compiler warnings */
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001593 png_error(png_ptr, "Out of place iTXt");
Glenn Randers-Pehrsonb75b2412011-04-16 19:35:05 -05001594 /* NOT REACHED */
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001595 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001596
1597#ifdef PNG_MAX_MALLOC_64K
1598 png_ptr->skip_length = 0; /* This may not be necessary */
1599
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001600 if (length > (png_uint_32)65535L) /* Can't hold entire string in memory */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001601 {
1602 png_warning(png_ptr, "iTXt chunk too large to fit in memory");
1603 png_ptr->skip_length = length - (png_uint_32)65535L;
1604 length = (png_uint_32)65535L;
1605 }
1606#endif
1607
1608 png_ptr->current_text = (png_charp)png_malloc(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001609 (png_size_t)(length + 1));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001610 png_ptr->current_text[length] = '\0';
1611 png_ptr->current_text_ptr = png_ptr->current_text;
1612 png_ptr->current_text_size = (png_size_t)length;
1613 png_ptr->current_text_left = (png_size_t)length;
1614 png_ptr->process_mode = PNG_READ_iTXt_MODE;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001615}
1616
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001617void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001618png_push_read_iTXt(png_structp png_ptr, png_infop info_ptr)
1619{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001620
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001621 if (png_ptr->buffer_size && png_ptr->current_text_left)
1622 {
1623 png_size_t text_size;
1624
1625 if (png_ptr->buffer_size < png_ptr->current_text_left)
1626 text_size = png_ptr->buffer_size;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001627
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001628 else
1629 text_size = png_ptr->current_text_left;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001630
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001631 png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
1632 png_ptr->current_text_left -= text_size;
1633 png_ptr->current_text_ptr += text_size;
1634 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001635
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001636 if (!(png_ptr->current_text_left))
1637 {
1638 png_textp text_ptr;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001639 png_charp key;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001640 int comp_flag;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001641 png_charp lang;
1642 png_charp lang_key;
1643 png_charp text;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001644 int ret;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001645
1646 if (png_ptr->buffer_size < 4)
1647 {
1648 png_push_save_buffer(png_ptr);
1649 return;
1650 }
1651
1652 png_push_crc_finish(png_ptr);
1653
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001654#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001655 if (png_ptr->skip_length)
1656 return;
1657#endif
1658
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001659 key = png_ptr->current_text;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001660
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001661 for (lang = key; *lang; lang++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001662 /* Empty loop */ ;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001663
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001664 if (lang < key + png_ptr->current_text_size - 3)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001665 lang++;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001666
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001667 comp_flag = *lang++;
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001668 lang++; /* Skip comp_type, always zero */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001669
1670 for (lang_key = lang; *lang_key; lang_key++)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001671 /* Empty loop */ ;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001672
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001673 lang_key++; /* Skip NUL separator */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001674
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001675 text=lang_key;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001676
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001677 if (lang_key < key + png_ptr->current_text_size - 1)
1678 {
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001679 for (; *text; text++)
1680 /* Empty loop */ ;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001681 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001682
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001683 if (text < key + png_ptr->current_text_size)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001684 text++;
1685
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001686 text_ptr = (png_textp)png_malloc(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001687 png_sizeof(png_text));
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001688
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001689 text_ptr->compression = comp_flag + 2;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001690 text_ptr->key = key;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001691 text_ptr->lang = lang;
1692 text_ptr->lang_key = lang_key;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001693 text_ptr->text = text;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001694 text_ptr->text_length = 0;
1695 text_ptr->itxt_length = png_strlen(text);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001696
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001697 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001698
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001699 png_ptr->current_text = NULL;
1700
Andreas Dilger47a0c421997-05-16 02:46:07 -05001701 png_free(png_ptr, text_ptr);
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001702 if (ret)
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -06001703 png_warning(png_ptr, "Insufficient memory to store iTXt chunk");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001704 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001705}
1706#endif
1707
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001708/* This function is called when we haven't found a handler for this
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001709 * chunk. If there isn't a problem with the chunk itself (ie a bad chunk
1710 * name or a critical chunk), the chunk is (currently) silently ignored.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001711 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001712void /* PRIVATE */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001713png_push_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001714 length)
Guy Schalnat6d764711995-12-19 03:22:19 -06001715{
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -05001716 png_uint_32 skip = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001717
1718 if (!(png_ptr->chunk_name[0] & 0x20))
1719 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001720#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001721 if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001722 PNG_HANDLE_CHUNK_ALWAYS
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001723#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001724 && png_ptr->read_user_chunk_fn == NULL
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001725#endif
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001726 )
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001727#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001728 png_chunk_error(png_ptr, "unknown critical chunk");
1729
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -06001730 PNG_UNUSED(info_ptr) /* To quiet some compiler warnings */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001731 }
1732
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001733#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001734 if (png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS)
1735 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001736#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001737 if (length > (png_uint_32)65535L)
1738 {
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001739 png_warning(png_ptr, "unknown chunk too large to fit in memory");
1740 skip = length - (png_uint_32)65535L;
1741 length = (png_uint_32)65535L;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001742 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001743#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001744 png_memcpy((png_charp)png_ptr->unknown_chunk.name,
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -05001745 (png_charp)png_ptr->chunk_name,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001746 png_sizeof(png_ptr->unknown_chunk.name));
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -05001747 png_ptr->unknown_chunk.name[png_sizeof(png_ptr->unknown_chunk.name) - 1]
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001748 = '\0';
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001749
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001750 png_ptr->unknown_chunk.size = (png_size_t)length;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001751
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001752 if (length == 0)
1753 png_ptr->unknown_chunk.data = NULL;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001754
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001755 else
1756 {
1757 png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001758 (png_size_t)length);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001759 png_crc_read(png_ptr, (png_bytep)png_ptr->unknown_chunk.data, length);
1760 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001761
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001762#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001763 if (png_ptr->read_user_chunk_fn != NULL)
1764 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001765 /* Callback to user unknown chunk handler */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001766 int ret;
1767 ret = (*(png_ptr->read_user_chunk_fn))
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001768 (png_ptr, &png_ptr->unknown_chunk);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001769
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001770 if (ret < 0)
1771 png_chunk_error(png_ptr, "error in user chunk");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001772
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001773 if (ret == 0)
1774 {
1775 if (!(png_ptr->chunk_name[0] & 0x20))
1776 if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001777 PNG_HANDLE_CHUNK_ALWAYS)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001778 png_chunk_error(png_ptr, "unknown critical chunk");
1779 png_set_unknown_chunks(png_ptr, info_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001780 &png_ptr->unknown_chunk, 1);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001781 }
1782 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001783
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001784 else
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001785#endif
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001786 png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001787 png_free(png_ptr, png_ptr->unknown_chunk.data);
1788 png_ptr->unknown_chunk.data = NULL;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001789 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001790
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001791 else
1792#endif
1793 skip=length;
1794 png_push_crc_skip(png_ptr, skip);
Guy Schalnat6d764711995-12-19 03:22:19 -06001795}
1796
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001797void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001798png_push_have_info(png_structp png_ptr, png_infop info_ptr)
1799{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001800 if (png_ptr->info_fn != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001801 (*(png_ptr->info_fn))(png_ptr, info_ptr);
1802}
1803
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001804void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001805png_push_have_end(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -06001806{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001807 if (png_ptr->end_fn != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001808 (*(png_ptr->end_fn))(png_ptr, info_ptr);
Guy Schalnat6d764711995-12-19 03:22:19 -06001809}
1810
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001811void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001812png_push_have_row(png_structp png_ptr, png_bytep row)
1813{
Glenn Randers-Pehrson78d55452010-06-19 07:03:50 -05001814 if (png_ptr->row_fn != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001815 (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number,
1816 (int)png_ptr->pass);
Guy Schalnat6d764711995-12-19 03:22:19 -06001817}
1818
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001819void PNGAPI
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001820png_progressive_combine_row (png_structp png_ptr, png_bytep old_row,
1821 png_const_bytep new_row)
Guy Schalnat6d764711995-12-19 03:22:19 -06001822{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001823 PNG_CONST int FARDATA png_pass_dsp_mask[7] =
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001824 {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
Glenn Randers-Pehrson86312502009-08-31 14:17:23 -05001825
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001826 if (png_ptr == NULL)
1827 return;
1828
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -05001829 if (new_row != NULL) /* new_row must == png_ptr->row_buf here. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001830 png_combine_row(png_ptr, old_row, png_pass_dsp_mask[png_ptr->pass]);
Guy Schalnat6d764711995-12-19 03:22:19 -06001831}
1832
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001833void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001834png_set_progressive_read_fn(png_structp png_ptr, png_voidp progressive_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001835 png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
1836 png_progressive_end_ptr end_fn)
Guy Schalnat6d764711995-12-19 03:22:19 -06001837{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001838 if (png_ptr == NULL)
1839 return;
1840
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001841 png_ptr->info_fn = info_fn;
1842 png_ptr->row_fn = row_fn;
Guy Schalnat6d764711995-12-19 03:22:19 -06001843 png_ptr->end_fn = end_fn;
Guy Schalnate5a37791996-06-05 15:50:50 -05001844
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -05001845 png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer);
Guy Schalnat6d764711995-12-19 03:22:19 -06001846}
1847
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001848png_voidp PNGAPI
John Bowler0a5c9c02011-01-22 17:36:34 -06001849png_get_progressive_ptr(png_const_structp png_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -06001850{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001851 if (png_ptr == NULL)
1852 return (NULL);
1853
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001854 return png_ptr->io_ptr;
Guy Schalnat6d764711995-12-19 03:22:19 -06001855}
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001856#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */