blob: c735e4f175956480ae18392f6a2c4d74d6161bb2 [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-Pehrson73b029f2004-11-26 17:28:09 -06004 * libpng version 1.2.8rc2 - November 26, 2004
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05006 * Copyright (c) 1998-2004 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05007 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06009 */
Guy Schalnat6d764711995-12-19 03:22:19 -060010
11#define PNG_INTERNAL
12#include "png.h"
13
Guy Schalnat4ee97b01996-01-16 01:51:56 -060014#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
15
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060016/* push model modes */
17#define PNG_READ_SIG_MODE 0
18#define PNG_READ_CHUNK_MODE 1
19#define PNG_READ_IDAT_MODE 2
20#define PNG_SKIP_MODE 3
21#define PNG_READ_tEXt_MODE 4
22#define PNG_READ_zTXt_MODE 5
23#define PNG_READ_DONE_MODE 6
24#define PNG_READ_iTXt_MODE 7
25#define PNG_ERROR_MODE 8
26
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050027void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060028png_process_data(png_structp png_ptr, png_infop info_ptr,
Andreas Dilger47a0c421997-05-16 02:46:07 -050029 png_bytep buffer, png_size_t buffer_size)
Guy Schalnat6d764711995-12-19 03:22:19 -060030{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060031 png_push_restore_buffer(png_ptr, buffer, buffer_size);
Guy Schalnat6d764711995-12-19 03:22:19 -060032
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060033 while (png_ptr->buffer_size)
34 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060035 png_process_some_data(png_ptr, info_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060036 }
Guy Schalnat6d764711995-12-19 03:22:19 -060037}
38
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060039/* What we do with the incoming data depends on what we were previously
40 * doing before we ran out of data...
41 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050042void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060043png_process_some_data(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -060044{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060045 switch (png_ptr->process_mode)
46 {
47 case PNG_READ_SIG_MODE:
48 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060049 png_push_read_sig(png_ptr, info_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060050 break;
51 }
52 case PNG_READ_CHUNK_MODE:
53 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060054 png_push_read_chunk(png_ptr, info_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060055 break;
56 }
57 case PNG_READ_IDAT_MODE:
58 {
Guy Schalnate5a37791996-06-05 15:50:50 -050059 png_push_read_IDAT(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060060 break;
61 }
Guy Schalnat6d764711995-12-19 03:22:19 -060062#if defined(PNG_READ_tEXt_SUPPORTED)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060063 case PNG_READ_tEXt_MODE:
64 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060065 png_push_read_tEXt(png_ptr, info_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060066 break;
67 }
Guy Schalnat6d764711995-12-19 03:22:19 -060068#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060069#if defined(PNG_READ_zTXt_SUPPORTED)
70 case PNG_READ_zTXt_MODE:
71 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060072 png_push_read_zTXt(png_ptr, info_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060073 break;
74 }
75#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060076#if defined(PNG_READ_iTXt_SUPPORTED)
77 case PNG_READ_iTXt_MODE:
78 {
79 png_push_read_iTXt(png_ptr, info_ptr);
80 break;
81 }
82#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060083 case PNG_SKIP_MODE:
84 {
Andreas Dilger47a0c421997-05-16 02:46:07 -050085 png_push_crc_finish(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060086 break;
87 }
88 default:
89 {
90 png_ptr->buffer_size = 0;
91 break;
92 }
93 }
Guy Schalnat6d764711995-12-19 03:22:19 -060094}
95
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060096/* Read any remaining signature bytes from the stream and compare them with
97 * the correct PNG signature. It is possible that this routine is called
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -050098 * with bytes already read from the signature, either because they have been
99 * checked by the calling application, or because of multiple calls to this
100 * routine.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600101 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500102void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600103png_push_read_sig(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -0600104{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500105 png_size_t num_checked = png_ptr->sig_bytes,
106 num_to_check = 8 - num_checked;
Guy Schalnat6d764711995-12-19 03:22:19 -0600107
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600108 if (png_ptr->buffer_size < num_to_check)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600109 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600110 num_to_check = png_ptr->buffer_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600111 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600112
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600113 png_push_fill_buffer(png_ptr, &(info_ptr->signature[num_checked]),
Andreas Dilger47a0c421997-05-16 02:46:07 -0500114 num_to_check);
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500115 png_ptr->sig_bytes = (png_byte)(png_ptr->sig_bytes+num_to_check);
Guy Schalnat6d764711995-12-19 03:22:19 -0600116
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600117 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600118 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600119 if (num_checked < 4 &&
120 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
121 png_error(png_ptr, "Not a PNG file");
122 else
123 png_error(png_ptr, "PNG file corrupted by ASCII conversion");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600124 }
125 else
126 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600127 if (png_ptr->sig_bytes >= 8)
128 {
129 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
130 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600131 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600132}
133
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500134void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600135png_push_read_chunk(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -0600136{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600137#ifdef PNG_USE_LOCAL_ARRAYS
138 PNG_IHDR;
139 PNG_IDAT;
140 PNG_IEND;
141 PNG_PLTE;
142#if defined(PNG_READ_bKGD_SUPPORTED)
143 PNG_bKGD;
144#endif
145#if defined(PNG_READ_cHRM_SUPPORTED)
146 PNG_cHRM;
147#endif
148#if defined(PNG_READ_gAMA_SUPPORTED)
149 PNG_gAMA;
150#endif
151#if defined(PNG_READ_hIST_SUPPORTED)
152 PNG_hIST;
153#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600154#if defined(PNG_READ_iCCP_SUPPORTED)
155 PNG_iCCP;
156#endif
157#if defined(PNG_READ_iTXt_SUPPORTED)
158 PNG_iTXt;
159#endif
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600160#if defined(PNG_READ_oFFs_SUPPORTED)
161 PNG_oFFs;
162#endif
163#if defined(PNG_READ_pCAL_SUPPORTED)
164 PNG_pCAL;
165#endif
166#if defined(PNG_READ_pHYs_SUPPORTED)
167 PNG_pHYs;
168#endif
169#if defined(PNG_READ_sBIT_SUPPORTED)
170 PNG_sBIT;
171#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600172#if defined(PNG_READ_sCAL_SUPPORTED)
173 PNG_sCAL;
174#endif
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600175#if defined(PNG_READ_sRGB_SUPPORTED)
176 PNG_sRGB;
177#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600178#if defined(PNG_READ_sPLT_SUPPORTED)
179 PNG_sPLT;
180#endif
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600181#if defined(PNG_READ_tEXt_SUPPORTED)
182 PNG_tEXt;
183#endif
184#if defined(PNG_READ_tIME_SUPPORTED)
185 PNG_tIME;
186#endif
187#if defined(PNG_READ_tRNS_SUPPORTED)
188 PNG_tRNS;
189#endif
190#if defined(PNG_READ_zTXt_SUPPORTED)
191 PNG_zTXt;
192#endif
193#endif /* PNG_USE_LOCAL_ARRAYS */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600194 /* First we make sure we have enough data for the 4 byte chunk name
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600195 * and the 4 byte chunk length before proceeding with decoding the
196 * chunk data. To fully decode each of these chunks, we also make
197 * sure we have enough data in the buffer for the 4 byte CRC at the
198 * end of every chunk (except IDAT, which is handled separately).
199 */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600200 if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600201 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600202 png_byte chunk_length[4];
Guy Schalnat6d764711995-12-19 03:22:19 -0600203
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600204 if (png_ptr->buffer_size < 8)
205 {
206 png_push_save_buffer(png_ptr);
207 return;
208 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600209
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600210 png_push_fill_buffer(png_ptr, chunk_length, 4);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500211 png_ptr->push_length = png_get_uint_31(png_ptr,chunk_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600212 png_reset_crc(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500213 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600214 png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600215 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600216
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600217 if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600218 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600219 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
220 {
221 png_push_save_buffer(png_ptr);
222 return;
223 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600224 png_handle_IHDR(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600225 }
Glenn Randers-Pehrsond029a752004-08-09 21:50:32 -0500226 else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
227 {
228 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
229 {
230 png_push_save_buffer(png_ptr);
231 return;
232 }
233 png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length);
234
235 png_ptr->process_mode = PNG_READ_DONE_MODE;
236 png_push_have_end(png_ptr, info_ptr);
237 }
238#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
239 else if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name))
240 {
241 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
242 {
243 png_push_save_buffer(png_ptr);
244 return;
245 }
246 if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
247 png_ptr->mode |= PNG_HAVE_IDAT;
248 png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length);
249 if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
250 png_ptr->mode |= PNG_HAVE_PLTE;
251 else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
252 {
253 if (!(png_ptr->mode & PNG_HAVE_IHDR))
254 png_error(png_ptr, "Missing IHDR before IDAT");
255 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
256 !(png_ptr->mode & PNG_HAVE_PLTE))
257 png_error(png_ptr, "Missing PLTE before IDAT");
258 }
259 }
260#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600261 else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600262 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600263 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600264 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600265 png_push_save_buffer(png_ptr);
266 return;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600267 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600268 png_handle_PLTE(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600269 }
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600270 else if (!png_memcmp(png_ptr->chunk_name, (png_bytep)png_IDAT, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600271 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600272 /* If we reach an IDAT chunk, this means we have read all of the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600273 * header chunks, and we can start reading the image (or if this
274 * is called after the image has been read - we have an error).
275 */
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600276 if (!(png_ptr->mode & PNG_HAVE_IHDR))
277 png_error(png_ptr, "Missing IHDR before IDAT");
278 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500279 !(png_ptr->mode & PNG_HAVE_PLTE))
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600280 png_error(png_ptr, "Missing PLTE before IDAT");
281
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600282 if (png_ptr->mode & PNG_HAVE_IDAT)
283 {
284 if (png_ptr->push_length == 0)
285 return;
286
287 if (png_ptr->mode & PNG_AFTER_IDAT)
288 png_error(png_ptr, "Too many IDAT's found");
289 }
290
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600291 png_ptr->idat_size = png_ptr->push_length;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600292 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600293 png_ptr->process_mode = PNG_READ_IDAT_MODE;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600294 png_push_have_info(png_ptr, info_ptr);
295 png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
296 png_ptr->zstream.next_out = png_ptr->row_buf;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600297 return;
298 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600299#if defined(PNG_READ_gAMA_SUPPORTED)
300 else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
301 {
302 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
303 {
304 png_push_save_buffer(png_ptr);
305 return;
306 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600307 png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600308 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600309#endif
310#if defined(PNG_READ_sBIT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600311 else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600312 {
313 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
314 {
315 png_push_save_buffer(png_ptr);
316 return;
317 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600318 png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600319 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600320#endif
321#if defined(PNG_READ_cHRM_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600322 else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600323 {
324 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
325 {
326 png_push_save_buffer(png_ptr);
327 return;
328 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600329 png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600330 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600331#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600332#if defined(PNG_READ_sRGB_SUPPORTED)
333 else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
334 {
335 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
336 {
337 png_push_save_buffer(png_ptr);
338 return;
339 }
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600340 png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length);
341 }
342#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600343#if defined(PNG_READ_iCCP_SUPPORTED)
344 else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4))
345 {
346 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
347 {
348 png_push_save_buffer(png_ptr);
349 return;
350 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600351 png_handle_iCCP(png_ptr, info_ptr, png_ptr->push_length);
352 }
353#endif
354#if defined(PNG_READ_sPLT_SUPPORTED)
355 else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4))
356 {
357 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
358 {
359 png_push_save_buffer(png_ptr);
360 return;
361 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600362 png_handle_sPLT(png_ptr, info_ptr, png_ptr->push_length);
363 }
364#endif
Guy Schalnat6d764711995-12-19 03:22:19 -0600365#if defined(PNG_READ_tRNS_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600366 else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600367 {
368 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
369 {
370 png_push_save_buffer(png_ptr);
371 return;
372 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600373 png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600374 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600375#endif
376#if defined(PNG_READ_bKGD_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600377 else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600378 {
379 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
380 {
381 png_push_save_buffer(png_ptr);
382 return;
383 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600384 png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600385 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600386#endif
387#if defined(PNG_READ_hIST_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600388 else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600389 {
390 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
391 {
392 png_push_save_buffer(png_ptr);
393 return;
394 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600395 png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600396 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600397#endif
398#if defined(PNG_READ_pHYs_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600399 else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600400 {
401 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
402 {
403 png_push_save_buffer(png_ptr);
404 return;
405 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600406 png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600407 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600408#endif
409#if defined(PNG_READ_oFFs_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600410 else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600411 {
412 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
413 {
414 png_push_save_buffer(png_ptr);
415 return;
416 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600417 png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600418 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600419#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500420#if defined(PNG_READ_pCAL_SUPPORTED)
421 else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
422 {
423 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
424 {
425 png_push_save_buffer(png_ptr);
426 return;
427 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500428 png_handle_pCAL(png_ptr, info_ptr, png_ptr->push_length);
429 }
430#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600431#if defined(PNG_READ_sCAL_SUPPORTED)
432 else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4))
433 {
434 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
435 {
436 png_push_save_buffer(png_ptr);
437 return;
438 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600439 png_handle_sCAL(png_ptr, info_ptr, png_ptr->push_length);
440 }
441#endif
Guy Schalnat6d764711995-12-19 03:22:19 -0600442#if defined(PNG_READ_tIME_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600443 else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600444 {
445 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
446 {
447 png_push_save_buffer(png_ptr);
448 return;
449 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600450 png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600451 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600452#endif
453#if defined(PNG_READ_tEXt_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600454 else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600455 {
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500456 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
457 {
458 png_push_save_buffer(png_ptr);
459 return;
460 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600461 png_push_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600462 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600463#endif
464#if defined(PNG_READ_zTXt_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600465 else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600466 {
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500467 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
468 {
469 png_push_save_buffer(png_ptr);
470 return;
471 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600472 png_push_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600473 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600474#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600475#if defined(PNG_READ_iTXt_SUPPORTED)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600476 else if (!png_memcmp(png_ptr->chunk_name, png_iTXt, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600477 {
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500478 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
479 {
480 png_push_save_buffer(png_ptr);
481 return;
482 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600483 png_push_handle_iTXt(png_ptr, info_ptr, png_ptr->push_length);
484 }
485#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600486 else
487 {
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500488 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
489 {
490 png_push_save_buffer(png_ptr);
491 return;
492 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600493 png_push_handle_unknown(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600494 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600495
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600496 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
Guy Schalnat6d764711995-12-19 03:22:19 -0600497}
498
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500499void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500500png_push_crc_skip(png_structp png_ptr, png_uint_32 skip)
Guy Schalnat6d764711995-12-19 03:22:19 -0600501{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600502 png_ptr->process_mode = PNG_SKIP_MODE;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500503 png_ptr->skip_length = skip;
Guy Schalnat6d764711995-12-19 03:22:19 -0600504}
505
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500506void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500507png_push_crc_finish(png_structp png_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -0600508{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600509 if (png_ptr->skip_length && png_ptr->save_buffer_size)
510 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500511 png_size_t save_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600512
Andreas Dilger47a0c421997-05-16 02:46:07 -0500513 if (png_ptr->skip_length < (png_uint_32)png_ptr->save_buffer_size)
514 save_size = (png_size_t)png_ptr->skip_length;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600515 else
516 save_size = png_ptr->save_buffer_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600517
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600518 png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
Guy Schalnat6d764711995-12-19 03:22:19 -0600519
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600520 png_ptr->skip_length -= save_size;
521 png_ptr->buffer_size -= save_size;
522 png_ptr->save_buffer_size -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500523 png_ptr->save_buffer_ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600524 }
525 if (png_ptr->skip_length && png_ptr->current_buffer_size)
526 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500527 png_size_t save_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600528
Andreas Dilger47a0c421997-05-16 02:46:07 -0500529 if (png_ptr->skip_length < (png_uint_32)png_ptr->current_buffer_size)
530 save_size = (png_size_t)png_ptr->skip_length;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600531 else
532 save_size = png_ptr->current_buffer_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600533
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600534 png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
Guy Schalnat6d764711995-12-19 03:22:19 -0600535
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600536 png_ptr->skip_length -= save_size;
537 png_ptr->buffer_size -= save_size;
538 png_ptr->current_buffer_size -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500539 png_ptr->current_buffer_ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600540 }
541 if (!png_ptr->skip_length)
542 {
543 if (png_ptr->buffer_size < 4)
544 {
545 png_push_save_buffer(png_ptr);
546 return;
547 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600548
549 png_crc_finish(png_ptr, 0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500550 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600551 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600552}
553
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -0500554void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500555png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length)
Guy Schalnat6d764711995-12-19 03:22:19 -0600556{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600557 png_bytep ptr;
Guy Schalnat6d764711995-12-19 03:22:19 -0600558
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600559 ptr = buffer;
560 if (png_ptr->save_buffer_size)
561 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500562 png_size_t save_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600563
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600564 if (length < png_ptr->save_buffer_size)
565 save_size = length;
566 else
567 save_size = png_ptr->save_buffer_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600568
Andreas Dilger47a0c421997-05-16 02:46:07 -0500569 png_memcpy(ptr, png_ptr->save_buffer_ptr, save_size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600570 length -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500571 ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600572 png_ptr->buffer_size -= save_size;
573 png_ptr->save_buffer_size -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500574 png_ptr->save_buffer_ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600575 }
576 if (length && png_ptr->current_buffer_size)
577 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500578 png_size_t save_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600579
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600580 if (length < png_ptr->current_buffer_size)
581 save_size = length;
582 else
583 save_size = png_ptr->current_buffer_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600584
Andreas Dilger47a0c421997-05-16 02:46:07 -0500585 png_memcpy(ptr, png_ptr->current_buffer_ptr, save_size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600586 png_ptr->buffer_size -= save_size;
587 png_ptr->current_buffer_size -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500588 png_ptr->current_buffer_ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600589 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600590}
591
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500592void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600593png_push_save_buffer(png_structp png_ptr)
594{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600595 if (png_ptr->save_buffer_size)
596 {
597 if (png_ptr->save_buffer_ptr != png_ptr->save_buffer)
598 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500599 png_size_t i,istop;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600600 png_bytep sp;
601 png_bytep dp;
Guy Schalnat6d764711995-12-19 03:22:19 -0600602
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500603 istop = png_ptr->save_buffer_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600604 for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500605 i < istop; i++, sp++, dp++)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600606 {
607 *dp = *sp;
608 }
609 }
610 }
611 if (png_ptr->save_buffer_size + png_ptr->current_buffer_size >
612 png_ptr->save_buffer_max)
613 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500614 png_size_t new_max;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600615 png_bytep old_buffer;
Guy Schalnat6d764711995-12-19 03:22:19 -0600616
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500617 if (png_ptr->save_buffer_size > PNG_SIZE_MAX -
618 (png_ptr->current_buffer_size + 256))
619 {
620 png_error(png_ptr, "Potential overflow of save_buffer");
621 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500622 new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600623 old_buffer = png_ptr->save_buffer;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600624 png_ptr->save_buffer = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600625 (png_uint_32)new_max);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500626 png_memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
627 png_free(png_ptr, old_buffer);
628 png_ptr->save_buffer_max = new_max;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600629 }
630 if (png_ptr->current_buffer_size)
631 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500632 png_memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
633 png_ptr->current_buffer_ptr, png_ptr->current_buffer_size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600634 png_ptr->save_buffer_size += png_ptr->current_buffer_size;
635 png_ptr->current_buffer_size = 0;
636 }
637 png_ptr->save_buffer_ptr = png_ptr->save_buffer;
638 png_ptr->buffer_size = 0;
Guy Schalnat6d764711995-12-19 03:22:19 -0600639}
640
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500641void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600642png_push_restore_buffer(png_structp png_ptr, png_bytep buffer,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500643 png_size_t buffer_length)
Guy Schalnat6d764711995-12-19 03:22:19 -0600644{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600645 png_ptr->current_buffer = buffer;
646 png_ptr->current_buffer_size = buffer_length;
647 png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size;
648 png_ptr->current_buffer_ptr = png_ptr->current_buffer;
Guy Schalnat6d764711995-12-19 03:22:19 -0600649}
650
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500651void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -0500652png_push_read_IDAT(png_structp png_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -0600653{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600654#ifdef PNG_USE_LOCAL_ARRAYS
655 PNG_IDAT;
656#endif
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600657 if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600658 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600659 png_byte chunk_length[4];
Guy Schalnat6d764711995-12-19 03:22:19 -0600660
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600661 if (png_ptr->buffer_size < 8)
662 {
663 png_push_save_buffer(png_ptr);
664 return;
665 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600666
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600667 png_push_fill_buffer(png_ptr, chunk_length, 4);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500668 png_ptr->push_length = png_get_uint_31(png_ptr,chunk_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600669 png_reset_crc(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500670 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600671 png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600672
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600673 if (png_memcmp(png_ptr->chunk_name, (png_bytep)png_IDAT, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600674 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500675 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500676 if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600677 png_error(png_ptr, "Not enough compressed data");
678 return;
679 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600680
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600681 png_ptr->idat_size = png_ptr->push_length;
682 }
683 if (png_ptr->idat_size && png_ptr->save_buffer_size)
684 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500685 png_size_t save_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600686
Andreas Dilger47a0c421997-05-16 02:46:07 -0500687 if (png_ptr->idat_size < (png_uint_32)png_ptr->save_buffer_size)
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600688 {
689 save_size = (png_size_t)png_ptr->idat_size;
690 /* check for overflow */
691 if((png_uint_32)save_size != png_ptr->idat_size)
692 png_error(png_ptr, "save_size overflowed in pngpread");
693 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600694 else
695 save_size = png_ptr->save_buffer_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600696
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600697 png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
Glenn Randers-Pehrson859665d2002-08-06 18:06:11 -0500698 if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
699 png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600700 png_ptr->idat_size -= save_size;
701 png_ptr->buffer_size -= save_size;
702 png_ptr->save_buffer_size -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500703 png_ptr->save_buffer_ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600704 }
705 if (png_ptr->idat_size && png_ptr->current_buffer_size)
706 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500707 png_size_t save_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600708
Andreas Dilger47a0c421997-05-16 02:46:07 -0500709 if (png_ptr->idat_size < (png_uint_32)png_ptr->current_buffer_size)
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600710 {
711 save_size = (png_size_t)png_ptr->idat_size;
712 /* check for overflow */
713 if((png_uint_32)save_size != png_ptr->idat_size)
714 png_error(png_ptr, "save_size overflowed in pngpread");
715 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600716 else
717 save_size = png_ptr->current_buffer_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600718
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600719 png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
Glenn Randers-Pehrson859665d2002-08-06 18:06:11 -0500720 if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
721 png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size);
Guy Schalnat6d764711995-12-19 03:22:19 -0600722
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600723 png_ptr->idat_size -= save_size;
724 png_ptr->buffer_size -= save_size;
725 png_ptr->current_buffer_size -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500726 png_ptr->current_buffer_ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600727 }
728 if (!png_ptr->idat_size)
729 {
730 if (png_ptr->buffer_size < 4)
731 {
732 png_push_save_buffer(png_ptr);
733 return;
734 }
735
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600736 png_crc_finish(png_ptr, 0);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600737 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
Glenn Randers-Pehrson859665d2002-08-06 18:06:11 -0500738 png_ptr->mode |= PNG_AFTER_IDAT;
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_process_IDAT_data(png_structp png_ptr, png_bytep buffer,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500744 png_size_t buffer_length)
Guy Schalnat6d764711995-12-19 03:22:19 -0600745{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600746 int ret;
Guy Schalnat6d764711995-12-19 03:22:19 -0600747
Guy Schalnate5a37791996-06-05 15:50:50 -0500748 if ((png_ptr->flags & PNG_FLAG_ZLIB_FINISHED) && buffer_length)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600749 png_error(png_ptr, "Extra compression data");
Guy Schalnat6d764711995-12-19 03:22:19 -0600750
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600751 png_ptr->zstream.next_in = buffer;
752 png_ptr->zstream.avail_in = (uInt)buffer_length;
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -0600753 for(;;)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600754 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600755 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500756 if (ret != Z_OK)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600757 {
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500758 if (ret == Z_STREAM_END)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600759 {
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500760 if (png_ptr->zstream.avail_in)
761 png_error(png_ptr, "Extra compressed data");
762 if (!(png_ptr->zstream.avail_out))
763 {
764 png_push_process_row(png_ptr);
765 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500766
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500767 png_ptr->mode |= PNG_AFTER_IDAT;
768 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
769 break;
770 }
771 else if (ret == Z_BUF_ERROR)
772 break;
773 else
774 png_error(png_ptr, "Decompression Error");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600775 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600776 if (!(png_ptr->zstream.avail_out))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600777 {
Glenn Randers-Pehrsond020e9d2002-06-28 09:34:00 -0500778 if ((
779#if defined(PNG_READ_INTERLACING_SUPPORTED)
780 png_ptr->interlaced && png_ptr->pass > 6) ||
781 (!png_ptr->interlaced &&
782#endif
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500783 png_ptr->row_number == png_ptr->num_rows))
Glenn Randers-Pehrson859665d2002-08-06 18:06:11 -0500784 {
Glenn Randers-Pehrson8a7df002002-08-15 22:02:57 -0500785 if (png_ptr->zstream.avail_in)
786 png_warning(png_ptr, "Too much data in IDAT chunks");
Glenn Randers-Pehrson859665d2002-08-06 18:06:11 -0500787 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
788 break;
789 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600790 png_push_process_row(png_ptr);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600791 png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
792 png_ptr->zstream.next_out = png_ptr->row_buf;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600793 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500794 else
795 break;
796 }
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_process_row(png_structp png_ptr)
801{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600802 png_ptr->row_info.color_type = png_ptr->color_type;
Guy Schalnat6d764711995-12-19 03:22:19 -0600803 png_ptr->row_info.width = png_ptr->iwidth;
804 png_ptr->row_info.channels = png_ptr->channels;
805 png_ptr->row_info.bit_depth = png_ptr->bit_depth;
806 png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600807
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500808 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
809 png_ptr->row_info.width);
Guy Schalnat6d764711995-12-19 03:22:19 -0600810
Guy Schalnate5a37791996-06-05 15:50:50 -0500811 png_read_filter_row(png_ptr, &(png_ptr->row_info),
812 png_ptr->row_buf + 1, png_ptr->prev_row + 1,
813 (int)(png_ptr->row_buf[0]));
Guy Schalnat6d764711995-12-19 03:22:19 -0600814
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600815 png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600816 png_ptr->rowbytes + 1);
Guy Schalnat6d764711995-12-19 03:22:19 -0600817
Glenn Randers-Pehrson73b029f2004-11-26 17:28:09 -0600818 if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA))
Guy Schalnat6d764711995-12-19 03:22:19 -0600819 png_do_read_transformations(png_ptr);
820
821#if defined(PNG_READ_INTERLACING_SUPPORTED)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600822 /* blow up interlaced rows to full size */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500823 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600824 {
825 if (png_ptr->pass < 6)
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -0600826/* old interface (pre-1.0.9):
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600827 png_do_read_interlace(&(png_ptr->row_info),
Andreas Dilger47a0c421997-05-16 02:46:07 -0500828 png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -0600829 */
830 png_do_read_interlace(png_ptr);
Guy Schalnat6d764711995-12-19 03:22:19 -0600831
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -0600832 switch (png_ptr->pass)
833 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600834 case 0:
835 {
836 int i;
837 for (i = 0; i < 8 && png_ptr->pass == 0; i++)
838 {
839 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
Glenn Randers-Pehrsonbe9de0f2001-01-22 08:52:16 -0600840 png_read_push_finish_row(png_ptr); /* updates png_ptr->pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600841 }
Glenn Randers-Pehrsonbe9de0f2001-01-22 08:52:16 -0600842 if (png_ptr->pass == 2) /* pass 1 might be empty */
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -0600843 {
844 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
845 {
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500846 png_push_have_row(png_ptr, png_bytep_NULL);
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -0600847 png_read_push_finish_row(png_ptr);
848 }
849 }
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500850 if (png_ptr->pass == 4 && png_ptr->height <= 4)
851 {
852 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
853 {
854 png_push_have_row(png_ptr, png_bytep_NULL);
855 png_read_push_finish_row(png_ptr);
856 }
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500857 }
858 if (png_ptr->pass == 6 && png_ptr->height <= 4)
859 {
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500860 png_push_have_row(png_ptr, png_bytep_NULL);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500861 png_read_push_finish_row(png_ptr);
862 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600863 break;
864 }
865 case 1:
866 {
867 int i;
868 for (i = 0; i < 8 && png_ptr->pass == 1; i++)
869 {
870 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
871 png_read_push_finish_row(png_ptr);
872 }
Glenn Randers-Pehrsonbe9de0f2001-01-22 08:52:16 -0600873 if (png_ptr->pass == 2) /* skip top 4 generated rows */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600874 {
875 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
876 {
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500877 png_push_have_row(png_ptr, png_bytep_NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600878 png_read_push_finish_row(png_ptr);
879 }
880 }
881 break;
882 }
883 case 2:
884 {
885 int i;
886 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
887 {
888 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
889 png_read_push_finish_row(png_ptr);
890 }
891 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
892 {
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500893 png_push_have_row(png_ptr, png_bytep_NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600894 png_read_push_finish_row(png_ptr);
895 }
Glenn Randers-Pehrsonbe9de0f2001-01-22 08:52:16 -0600896 if (png_ptr->pass == 4) /* pass 3 might be empty */
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -0600897 {
898 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
899 {
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500900 png_push_have_row(png_ptr, png_bytep_NULL);
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -0600901 png_read_push_finish_row(png_ptr);
902 }
903 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600904 break;
905 }
906 case 3:
907 {
908 int i;
909 for (i = 0; i < 4 && png_ptr->pass == 3; i++)
910 {
911 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
912 png_read_push_finish_row(png_ptr);
913 }
Glenn Randers-Pehrsonbe9de0f2001-01-22 08:52:16 -0600914 if (png_ptr->pass == 4) /* skip top two generated rows */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600915 {
916 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
917 {
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500918 png_push_have_row(png_ptr, png_bytep_NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600919 png_read_push_finish_row(png_ptr);
920 }
921 }
922 break;
923 }
924 case 4:
925 {
926 int i;
927 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
928 {
929 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
930 png_read_push_finish_row(png_ptr);
931 }
932 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
933 {
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500934 png_push_have_row(png_ptr, png_bytep_NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600935 png_read_push_finish_row(png_ptr);
936 }
Glenn Randers-Pehrsonbe9de0f2001-01-22 08:52:16 -0600937 if (png_ptr->pass == 6) /* pass 5 might be empty */
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -0600938 {
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500939 png_push_have_row(png_ptr, png_bytep_NULL);
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -0600940 png_read_push_finish_row(png_ptr);
941 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600942 break;
943 }
944 case 5:
945 {
946 int i;
947 for (i = 0; i < 2 && png_ptr->pass == 5; i++)
948 {
949 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
950 png_read_push_finish_row(png_ptr);
951 }
Glenn Randers-Pehrsonbe9de0f2001-01-22 08:52:16 -0600952 if (png_ptr->pass == 6) /* skip top generated row */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600953 {
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500954 png_push_have_row(png_ptr, png_bytep_NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600955 png_read_push_finish_row(png_ptr);
956 }
957 break;
958 }
959 case 6:
960 {
961 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
962 png_read_push_finish_row(png_ptr);
963 if (png_ptr->pass != 6)
964 break;
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500965 png_push_have_row(png_ptr, png_bytep_NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600966 png_read_push_finish_row(png_ptr);
967 }
968 }
969 }
970 else
Guy Schalnat6d764711995-12-19 03:22:19 -0600971#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600972 {
973 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
974 png_read_push_finish_row(png_ptr);
975 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600976}
977
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500978void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600979png_read_push_finish_row(png_structp png_ptr)
980{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600981#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600982 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600983
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600984 /* start of interlace block */
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600985 const int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600986
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600987 /* offset to next interlace block */
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600988 const int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600989
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600990 /* start of interlace block in the y direction */
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600991 const int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600992
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600993 /* offset to next interlace block in the y direction */
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600994 const int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600995
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600996 /* Width of interlace block. This is not currently used - if you need
997 * it, uncomment it here and in png.h
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600998 const int FARDATA png_pass_width[] = {8, 4, 4, 2, 2, 1, 1};
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600999 */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001000
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001001 /* Height of interlace block. This is not currently used - if you need
1002 * it, uncomment it here and in png.h
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001003 const int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001004 */
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001005#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001006
Guy Schalnat6d764711995-12-19 03:22:19 -06001007 png_ptr->row_number++;
1008 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001009 return;
Guy Schalnat6d764711995-12-19 03:22:19 -06001010
1011 if (png_ptr->interlaced)
1012 {
1013 png_ptr->row_number = 0;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001014 png_memset_check(png_ptr, png_ptr->prev_row, 0,
1015 png_ptr->rowbytes + 1);
Guy Schalnat6d764711995-12-19 03:22:19 -06001016 do
1017 {
1018 png_ptr->pass++;
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -06001019 if ((png_ptr->pass == 1 && png_ptr->width < 5) ||
1020 (png_ptr->pass == 3 && png_ptr->width < 3) ||
1021 (png_ptr->pass == 5 && png_ptr->width < 2))
1022 png_ptr->pass++;
1023
Glenn Randers-Pehrson859665d2002-08-06 18:06:11 -05001024 if (png_ptr->pass > 7)
1025 png_ptr->pass--;
Guy Schalnat6d764711995-12-19 03:22:19 -06001026 if (png_ptr->pass >= 7)
1027 break;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001028
Guy Schalnat6d764711995-12-19 03:22:19 -06001029 png_ptr->iwidth = (png_ptr->width +
1030 png_pass_inc[png_ptr->pass] - 1 -
1031 png_pass_start[png_ptr->pass]) /
1032 png_pass_inc[png_ptr->pass];
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001033
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001034 png_ptr->irowbytes = PNG_ROWBYTES(png_ptr->pixel_depth,
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -05001035 png_ptr->iwidth) + 1;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001036
Guy Schalnat6d764711995-12-19 03:22:19 -06001037 if (png_ptr->transformations & PNG_INTERLACE)
1038 break;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001039
1040 png_ptr->num_rows = (png_ptr->height +
1041 png_pass_yinc[png_ptr->pass] - 1 -
1042 png_pass_ystart[png_ptr->pass]) /
1043 png_pass_yinc[png_ptr->pass];
1044
1045 } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0);
Guy Schalnat6d764711995-12-19 03:22:19 -06001046 }
1047}
1048
Guy Schalnat6d764711995-12-19 03:22:19 -06001049#if defined(PNG_READ_tEXt_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001050void /* PRIVATE */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001051png_push_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
1052 length)
Guy Schalnat6d764711995-12-19 03:22:19 -06001053{
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001054 if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001055 {
1056 png_error(png_ptr, "Out of place tEXt");
1057 /* to quiet some compiler warnings */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001058 if(info_ptr == NULL) return;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001059 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001060
Andreas Dilger47a0c421997-05-16 02:46:07 -05001061#ifdef PNG_MAX_MALLOC_64K
1062 png_ptr->skip_length = 0; /* This may not be necessary */
1063
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001064 if (length > (png_uint_32)65535L) /* Can't hold entire string in memory */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001065 {
1066 png_warning(png_ptr, "tEXt chunk too large to fit in memory");
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001067 png_ptr->skip_length = length - (png_uint_32)65535L;
1068 length = (png_uint_32)65535L;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001069 }
1070#endif
1071
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001072 png_ptr->current_text = (png_charp)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001073 (png_uint_32)(length+1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001074 png_ptr->current_text[length] = '\0';
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001075 png_ptr->current_text_ptr = png_ptr->current_text;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001076 png_ptr->current_text_size = (png_size_t)length;
1077 png_ptr->current_text_left = (png_size_t)length;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001078 png_ptr->process_mode = PNG_READ_tEXt_MODE;
Guy Schalnat6d764711995-12-19 03:22:19 -06001079}
1080
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001081void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001082png_push_read_tEXt(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -06001083{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001084 if (png_ptr->buffer_size && png_ptr->current_text_left)
1085 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001086 png_size_t text_size;
Guy Schalnat6d764711995-12-19 03:22:19 -06001087
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001088 if (png_ptr->buffer_size < png_ptr->current_text_left)
1089 text_size = png_ptr->buffer_size;
1090 else
1091 text_size = png_ptr->current_text_left;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001092 png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001093 png_ptr->current_text_left -= text_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001094 png_ptr->current_text_ptr += text_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001095 }
1096 if (!(png_ptr->current_text_left))
1097 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001098 png_textp text_ptr;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001099 png_charp text;
1100 png_charp key;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001101 int ret;
Guy Schalnat6d764711995-12-19 03:22:19 -06001102
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001103 if (png_ptr->buffer_size < 4)
1104 {
1105 png_push_save_buffer(png_ptr);
1106 return;
1107 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001108
Andreas Dilger47a0c421997-05-16 02:46:07 -05001109 png_push_crc_finish(png_ptr);
1110
1111#if defined(PNG_MAX_MALLOC_64K)
1112 if (png_ptr->skip_length)
1113 return;
1114#endif
Guy Schalnat6d764711995-12-19 03:22:19 -06001115
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001116 key = png_ptr->current_text;
Guy Schalnat6d764711995-12-19 03:22:19 -06001117
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001118 for (text = key; *text; text++)
1119 /* empty loop */ ;
Guy Schalnat6d764711995-12-19 03:22:19 -06001120
Andreas Dilger47a0c421997-05-16 02:46:07 -05001121 if (text != key + png_ptr->current_text_size)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001122 text++;
1123
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001124 text_ptr = (png_textp)png_malloc(png_ptr,
1125 (png_uint_32)png_sizeof(png_text));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001126 text_ptr->compression = PNG_TEXT_COMPRESSION_NONE;
1127 text_ptr->key = key;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001128#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001129 text_ptr->lang = NULL;
1130 text_ptr->lang_key = NULL;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001131#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001132 text_ptr->text = text;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001133
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001134 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001135
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001136 png_free(png_ptr, key);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001137 png_free(png_ptr, text_ptr);
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001138 png_ptr->current_text = NULL;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001139
1140 if (ret)
1141 png_warning(png_ptr, "Insufficient memory to store text chunk.");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001142 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001143}
1144#endif
1145
1146#if defined(PNG_READ_zTXt_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001147void /* PRIVATE */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001148png_push_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
1149 length)
Guy Schalnat6d764711995-12-19 03:22:19 -06001150{
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001151 if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001152 {
1153 png_error(png_ptr, "Out of place zTXt");
1154 /* to quiet some compiler warnings */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001155 if(info_ptr == NULL) return;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001156 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001157
Andreas Dilger47a0c421997-05-16 02:46:07 -05001158#ifdef PNG_MAX_MALLOC_64K
1159 /* We can't handle zTXt chunks > 64K, since we don't have enough space
1160 * to be able to store the uncompressed data. Actually, the threshold
1161 * is probably around 32K, but it isn't as definite as 64K is.
1162 */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001163 if (length > (png_uint_32)65535L)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001164 {
1165 png_warning(png_ptr, "zTXt chunk too large to fit in memory");
1166 png_push_crc_skip(png_ptr, length);
1167 return;
1168 }
1169#endif
1170
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001171 png_ptr->current_text = (png_charp)png_malloc(png_ptr,
1172 (png_uint_32)(length+1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001173 png_ptr->current_text[length] = '\0';
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001174 png_ptr->current_text_ptr = png_ptr->current_text;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001175 png_ptr->current_text_size = (png_size_t)length;
1176 png_ptr->current_text_left = (png_size_t)length;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001177 png_ptr->process_mode = PNG_READ_zTXt_MODE;
Guy Schalnat6d764711995-12-19 03:22:19 -06001178}
1179
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001180void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001181png_push_read_zTXt(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -06001182{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001183 if (png_ptr->buffer_size && png_ptr->current_text_left)
1184 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001185 png_size_t text_size;
Guy Schalnat6d764711995-12-19 03:22:19 -06001186
Andreas Dilger47a0c421997-05-16 02:46:07 -05001187 if (png_ptr->buffer_size < (png_uint_32)png_ptr->current_text_left)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001188 text_size = png_ptr->buffer_size;
1189 else
1190 text_size = png_ptr->current_text_left;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001191 png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001192 png_ptr->current_text_left -= text_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001193 png_ptr->current_text_ptr += text_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001194 }
1195 if (!(png_ptr->current_text_left))
1196 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001197 png_textp text_ptr;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001198 png_charp text;
1199 png_charp key;
1200 int ret;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001201 png_size_t text_size, key_size;
Guy Schalnat6d764711995-12-19 03:22:19 -06001202
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001203 if (png_ptr->buffer_size < 4)
1204 {
1205 png_push_save_buffer(png_ptr);
1206 return;
1207 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001208
Andreas Dilger47a0c421997-05-16 02:46:07 -05001209 png_push_crc_finish(png_ptr);
Guy Schalnat6d764711995-12-19 03:22:19 -06001210
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001211 key = png_ptr->current_text;
Guy Schalnat6d764711995-12-19 03:22:19 -06001212
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001213 for (text = key; *text; text++)
1214 /* empty loop */ ;
Guy Schalnat6d764711995-12-19 03:22:19 -06001215
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001216 /* zTXt can't have zero text */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001217 if (text == key + png_ptr->current_text_size)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001218 {
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001219 png_ptr->current_text = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001220 png_free(png_ptr, key);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001221 return;
1222 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001223
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001224 text++;
Guy Schalnat6d764711995-12-19 03:22:19 -06001225
Andreas Dilger47a0c421997-05-16 02:46:07 -05001226 if (*text != PNG_TEXT_COMPRESSION_zTXt) /* check compression byte */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001227 {
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001228 png_ptr->current_text = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001229 png_free(png_ptr, key);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001230 return;
1231 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001232
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001233 text++;
Guy Schalnat6d764711995-12-19 03:22:19 -06001234
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001235 png_ptr->zstream.next_in = (png_bytep )text;
1236 png_ptr->zstream.avail_in = (uInt)(png_ptr->current_text_size -
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001237 (text - key));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001238 png_ptr->zstream.next_out = png_ptr->zbuf;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001239 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnat6d764711995-12-19 03:22:19 -06001240
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001241 key_size = text - key;
1242 text_size = 0;
1243 text = NULL;
1244 ret = Z_STREAM_END;
Guy Schalnat6d764711995-12-19 03:22:19 -06001245
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001246 while (png_ptr->zstream.avail_in)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001247 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001248 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001249 if (ret != Z_OK && ret != Z_STREAM_END)
1250 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001251 inflateReset(&png_ptr->zstream);
1252 png_ptr->zstream.avail_in = 0;
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001253 png_ptr->current_text = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001254 png_free(png_ptr, key);
1255 png_free(png_ptr, text);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001256 return;
1257 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001258 if (!(png_ptr->zstream.avail_out) || ret == Z_STREAM_END)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001259 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001260 if (text == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001261 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001262 text = (png_charp)png_malloc(png_ptr,
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001263 (png_uint_32)(png_ptr->zbuf_size - png_ptr->zstream.avail_out
1264 + key_size + 1));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001265 png_memcpy(text + key_size, png_ptr->zbuf,
1266 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
1267 png_memcpy(text, key, key_size);
1268 text_size = key_size + png_ptr->zbuf_size -
1269 png_ptr->zstream.avail_out;
1270 *(text + text_size) = '\0';
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001271 }
1272 else
1273 {
1274 png_charp tmp;
Guy Schalnat6d764711995-12-19 03:22:19 -06001275
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001276 tmp = text;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001277 text = (png_charp)png_malloc(png_ptr, text_size +
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001278 (png_uint_32)(png_ptr->zbuf_size - png_ptr->zstream.avail_out
1279 + 1));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001280 png_memcpy(text, tmp, text_size);
1281 png_free(png_ptr, tmp);
1282 png_memcpy(text + text_size, png_ptr->zbuf,
1283 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
1284 text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out;
1285 *(text + text_size) = '\0';
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001286 }
1287 if (ret != Z_STREAM_END)
1288 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001289 png_ptr->zstream.next_out = png_ptr->zbuf;
1290 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001291 }
1292 }
1293 else
1294 {
1295 break;
1296 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001297
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001298 if (ret == Z_STREAM_END)
1299 break;
1300 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001301
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001302 inflateReset(&png_ptr->zstream);
1303 png_ptr->zstream.avail_in = 0;
Guy Schalnat6d764711995-12-19 03:22:19 -06001304
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001305 if (ret != Z_STREAM_END)
1306 {
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001307 png_ptr->current_text = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001308 png_free(png_ptr, key);
1309 png_free(png_ptr, text);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001310 return;
1311 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001312
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001313 png_ptr->current_text = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001314 png_free(png_ptr, key);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001315 key = text;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001316 text += key_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001317
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001318 text_ptr = (png_textp)png_malloc(png_ptr,
1319 (png_uint_32)png_sizeof(png_text));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001320 text_ptr->compression = PNG_TEXT_COMPRESSION_zTXt;
1321 text_ptr->key = key;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001322#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001323 text_ptr->lang = NULL;
1324 text_ptr->lang_key = NULL;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001325#endif
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001326 text_ptr->text = text;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001327
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001328 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001329
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001330 png_free(png_ptr, key);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001331 png_free(png_ptr, text_ptr);
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001332
1333 if (ret)
1334 png_warning(png_ptr, "Insufficient memory to store text chunk.");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001335 }
1336}
1337#endif
1338
1339#if defined(PNG_READ_iTXt_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001340void /* PRIVATE */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001341png_push_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
1342 length)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001343{
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001344 if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001345 {
1346 png_error(png_ptr, "Out of place iTXt");
1347 /* to quiet some compiler warnings */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001348 if(info_ptr == NULL) return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001349 }
1350
1351#ifdef PNG_MAX_MALLOC_64K
1352 png_ptr->skip_length = 0; /* This may not be necessary */
1353
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001354 if (length > (png_uint_32)65535L) /* Can't hold entire string in memory */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001355 {
1356 png_warning(png_ptr, "iTXt chunk too large to fit in memory");
1357 png_ptr->skip_length = length - (png_uint_32)65535L;
1358 length = (png_uint_32)65535L;
1359 }
1360#endif
1361
1362 png_ptr->current_text = (png_charp)png_malloc(png_ptr,
1363 (png_uint_32)(length+1));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001364 png_ptr->current_text[length] = '\0';
1365 png_ptr->current_text_ptr = png_ptr->current_text;
1366 png_ptr->current_text_size = (png_size_t)length;
1367 png_ptr->current_text_left = (png_size_t)length;
1368 png_ptr->process_mode = PNG_READ_iTXt_MODE;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001369}
1370
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001371void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001372png_push_read_iTXt(png_structp png_ptr, png_infop info_ptr)
1373{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001374
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001375 if (png_ptr->buffer_size && png_ptr->current_text_left)
1376 {
1377 png_size_t text_size;
1378
1379 if (png_ptr->buffer_size < png_ptr->current_text_left)
1380 text_size = png_ptr->buffer_size;
1381 else
1382 text_size = png_ptr->current_text_left;
1383 png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
1384 png_ptr->current_text_left -= text_size;
1385 png_ptr->current_text_ptr += text_size;
1386 }
1387 if (!(png_ptr->current_text_left))
1388 {
1389 png_textp text_ptr;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001390 png_charp key;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001391 int comp_flag;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001392 png_charp lang;
1393 png_charp lang_key;
1394 png_charp text;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001395 int ret;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001396
1397 if (png_ptr->buffer_size < 4)
1398 {
1399 png_push_save_buffer(png_ptr);
1400 return;
1401 }
1402
1403 png_push_crc_finish(png_ptr);
1404
1405#if defined(PNG_MAX_MALLOC_64K)
1406 if (png_ptr->skip_length)
1407 return;
1408#endif
1409
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001410 key = png_ptr->current_text;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001411
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001412 for (lang = key; *lang; lang++)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001413 /* empty loop */ ;
1414
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001415 if (lang != key + png_ptr->current_text_size)
1416 lang++;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001417
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001418 comp_flag = *lang++;
1419 lang++; /* skip comp_type, always zero */
1420
1421 for (lang_key = lang; *lang_key; lang_key++)
1422 /* empty loop */ ;
1423 lang_key++; /* skip NUL separator */
1424
1425 for (text = lang_key; *text; text++)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001426 /* empty loop */ ;
1427
1428 if (text != key + png_ptr->current_text_size)
1429 text++;
1430
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001431 text_ptr = (png_textp)png_malloc(png_ptr,
1432 (png_uint_32)png_sizeof(png_text));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001433 text_ptr->compression = comp_flag + 2;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001434 text_ptr->key = key;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001435 text_ptr->lang = lang;
1436 text_ptr->lang_key = lang_key;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001437 text_ptr->text = text;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001438 text_ptr->text_length = 0;
1439 text_ptr->itxt_length = png_strlen(text);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001440
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001441 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001442
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001443 png_ptr->current_text = NULL;
1444
Andreas Dilger47a0c421997-05-16 02:46:07 -05001445 png_free(png_ptr, text_ptr);
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001446 if (ret)
1447 png_warning(png_ptr, "Insufficient memory to store iTXt chunk.");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001448 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001449}
1450#endif
1451
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001452/* This function is called when we haven't found a handler for this
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001453 * chunk. If there isn't a problem with the chunk itself (ie a bad chunk
1454 * name or a critical chunk), the chunk is (currently) silently ignored.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001455 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001456void /* PRIVATE */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001457png_push_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32
1458 length)
Guy Schalnat6d764711995-12-19 03:22:19 -06001459{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001460 png_uint_32 skip=0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001461 png_check_chunk_name(png_ptr, png_ptr->chunk_name);
1462
1463 if (!(png_ptr->chunk_name[0] & 0x20))
1464 {
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001465#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001466 if(png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -05001467 PNG_HANDLE_CHUNK_ALWAYS
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001468#if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001469 && png_ptr->read_user_chunk_fn == NULL
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001470#endif
1471 )
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001472#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001473 png_chunk_error(png_ptr, "unknown critical chunk");
1474
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001475 /* to quiet compiler warnings about unused info_ptr */
1476 if (info_ptr == NULL)
1477 return;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001478 }
1479
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001480#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
1481 if (png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS)
1482 {
1483 png_unknown_chunk chunk;
1484
1485#ifdef PNG_MAX_MALLOC_64K
1486 if (length > (png_uint_32)65535L)
1487 {
1488 png_warning(png_ptr, "unknown chunk too large to fit in memory");
1489 skip = length - (png_uint_32)65535L;
1490 length = (png_uint_32)65535L;
1491 }
1492#endif
1493
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001494 png_strcpy((png_charp)chunk.name, (png_charp)png_ptr->chunk_name);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001495 chunk.data = (png_bytep)png_malloc(png_ptr, length);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001496 png_crc_read(png_ptr, chunk.data, length);
1497 chunk.size = length;
Glenn Randers-Pehrsone1eff582001-04-14 20:15:41 -05001498#if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001499 if(png_ptr->read_user_chunk_fn != NULL)
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001500 {
1501 /* callback to user unknown chunk handler */
1502 if ((*(png_ptr->read_user_chunk_fn)) (png_ptr, &chunk) <= 0)
1503 {
1504 if (!(png_ptr->chunk_name[0] & 0x20))
1505 if(png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -05001506 PNG_HANDLE_CHUNK_ALWAYS)
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001507 png_chunk_error(png_ptr, "unknown critical chunk");
1508 }
1509 png_set_unknown_chunks(png_ptr, info_ptr, &chunk, 1);
Glenn Randers-Pehrsone1eff582001-04-14 20:15:41 -05001510 }
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001511 else
1512#endif
1513 png_set_unknown_chunks(png_ptr, info_ptr, &chunk, 1);
1514 png_free(png_ptr, chunk.data);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001515 }
1516 else
1517#endif
1518 skip=length;
1519 png_push_crc_skip(png_ptr, skip);
Guy Schalnat6d764711995-12-19 03:22:19 -06001520}
1521
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001522void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001523png_push_have_info(png_structp png_ptr, png_infop info_ptr)
1524{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001525 if (png_ptr->info_fn != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001526 (*(png_ptr->info_fn))(png_ptr, info_ptr);
1527}
1528
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001529void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001530png_push_have_end(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -06001531{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001532 if (png_ptr->end_fn != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001533 (*(png_ptr->end_fn))(png_ptr, info_ptr);
Guy Schalnat6d764711995-12-19 03:22:19 -06001534}
1535
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001536void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001537png_push_have_row(png_structp png_ptr, png_bytep row)
1538{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001539 if (png_ptr->row_fn != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001540 (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number,
1541 (int)png_ptr->pass);
Guy Schalnat6d764711995-12-19 03:22:19 -06001542}
1543
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001544void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001545png_progressive_combine_row (png_structp png_ptr,
1546 png_bytep old_row, png_bytep new_row)
Guy Schalnat6d764711995-12-19 03:22:19 -06001547{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001548#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001549 const int FARDATA png_pass_dsp_mask[7] =
1550 {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001551#endif
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -05001552 if (new_row != NULL) /* new_row must == png_ptr->row_buf here. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001553 png_combine_row(png_ptr, old_row, png_pass_dsp_mask[png_ptr->pass]);
Guy Schalnat6d764711995-12-19 03:22:19 -06001554}
1555
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001556void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001557png_set_progressive_read_fn(png_structp png_ptr, png_voidp progressive_ptr,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001558 png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
1559 png_progressive_end_ptr end_fn)
Guy Schalnat6d764711995-12-19 03:22:19 -06001560{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001561 png_ptr->info_fn = info_fn;
1562 png_ptr->row_fn = row_fn;
Guy Schalnat6d764711995-12-19 03:22:19 -06001563 png_ptr->end_fn = end_fn;
Guy Schalnate5a37791996-06-05 15:50:50 -05001564
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -05001565 png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer);
Guy Schalnat6d764711995-12-19 03:22:19 -06001566}
1567
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001568png_voidp PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001569png_get_progressive_ptr(png_structp png_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -06001570{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001571 return png_ptr->io_ptr;
Guy Schalnat6d764711995-12-19 03:22:19 -06001572}
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001573#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */