blob: 78d75d1218d0ddfb27d73c844f1fce85c53da1f4 [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-Pehrsond1e8c862002-06-20 06:54:34 -05004 * libpng 1.2.4beta2 - June 20, 2002
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
Glenn Randers-Pehrsonc6de22d2002-02-23 18:55:25 -06006 * Copyright (c) 1998-2002 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);
211 png_ptr->push_length = png_get_uint_32(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 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600226 else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600227 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600228 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600229 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600230 png_push_save_buffer(png_ptr);
231 return;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600232 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600233 png_handle_PLTE(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600234 }
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600235 else if (!png_memcmp(png_ptr->chunk_name, (png_bytep)png_IDAT, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600236 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600237 /* If we reach an IDAT chunk, this means we have read all of the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600238 * header chunks, and we can start reading the image (or if this
239 * is called after the image has been read - we have an error).
240 */
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600241 if (!(png_ptr->mode & PNG_HAVE_IHDR))
242 png_error(png_ptr, "Missing IHDR before IDAT");
243 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500244 !(png_ptr->mode & PNG_HAVE_PLTE))
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600245 png_error(png_ptr, "Missing PLTE before IDAT");
246
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600247 if (png_ptr->mode & PNG_HAVE_IDAT)
248 {
249 if (png_ptr->push_length == 0)
250 return;
251
252 if (png_ptr->mode & PNG_AFTER_IDAT)
253 png_error(png_ptr, "Too many IDAT's found");
254 }
255
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600256 png_ptr->idat_size = png_ptr->push_length;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600257 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600258 png_ptr->process_mode = PNG_READ_IDAT_MODE;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600259 png_push_have_info(png_ptr, info_ptr);
260 png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
261 png_ptr->zstream.next_out = png_ptr->row_buf;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600262 return;
263 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600264 else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600265 {
266 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
267 {
268 png_push_save_buffer(png_ptr);
269 return;
270 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600271 png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length);
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500272
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600273 png_ptr->process_mode = PNG_READ_DONE_MODE;
274 png_push_have_end(png_ptr, info_ptr);
275 }
276#if defined(PNG_READ_gAMA_SUPPORTED)
277 else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
278 {
279 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
280 {
281 png_push_save_buffer(png_ptr);
282 return;
283 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600284 png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600285 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600286#endif
287#if defined(PNG_READ_sBIT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600288 else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600289 {
290 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
291 {
292 png_push_save_buffer(png_ptr);
293 return;
294 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600295 png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600296 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600297#endif
298#if defined(PNG_READ_cHRM_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600299 else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600300 {
301 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
302 {
303 png_push_save_buffer(png_ptr);
304 return;
305 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600306 png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600307 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600308#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600309#if defined(PNG_READ_sRGB_SUPPORTED)
310 else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
311 {
312 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
313 {
314 png_push_save_buffer(png_ptr);
315 return;
316 }
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600317 png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length);
318 }
319#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600320#if defined(PNG_READ_iCCP_SUPPORTED)
321 else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4))
322 {
323 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
324 {
325 png_push_save_buffer(png_ptr);
326 return;
327 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600328 png_handle_iCCP(png_ptr, info_ptr, png_ptr->push_length);
329 }
330#endif
331#if defined(PNG_READ_sPLT_SUPPORTED)
332 else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4))
333 {
334 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
335 {
336 png_push_save_buffer(png_ptr);
337 return;
338 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600339 png_handle_sPLT(png_ptr, info_ptr, png_ptr->push_length);
340 }
341#endif
Guy Schalnat6d764711995-12-19 03:22:19 -0600342#if defined(PNG_READ_tRNS_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600343 else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600344 {
345 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
346 {
347 png_push_save_buffer(png_ptr);
348 return;
349 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600350 png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600351 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600352#endif
353#if defined(PNG_READ_bKGD_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600354 else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600355 {
356 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
357 {
358 png_push_save_buffer(png_ptr);
359 return;
360 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600361 png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600362 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600363#endif
364#if defined(PNG_READ_hIST_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600365 else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600366 {
367 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
368 {
369 png_push_save_buffer(png_ptr);
370 return;
371 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600372 png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600373 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600374#endif
375#if defined(PNG_READ_pHYs_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600376 else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600377 {
378 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
379 {
380 png_push_save_buffer(png_ptr);
381 return;
382 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600383 png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600384 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600385#endif
386#if defined(PNG_READ_oFFs_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600387 else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600388 {
389 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
390 {
391 png_push_save_buffer(png_ptr);
392 return;
393 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600394 png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600395 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600396#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500397#if defined(PNG_READ_pCAL_SUPPORTED)
398 else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
399 {
400 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
401 {
402 png_push_save_buffer(png_ptr);
403 return;
404 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500405 png_handle_pCAL(png_ptr, info_ptr, png_ptr->push_length);
406 }
407#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600408#if defined(PNG_READ_sCAL_SUPPORTED)
409 else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4))
410 {
411 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
412 {
413 png_push_save_buffer(png_ptr);
414 return;
415 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600416 png_handle_sCAL(png_ptr, info_ptr, png_ptr->push_length);
417 }
418#endif
Guy Schalnat6d764711995-12-19 03:22:19 -0600419#if defined(PNG_READ_tIME_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600420 else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600421 {
422 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
423 {
424 png_push_save_buffer(png_ptr);
425 return;
426 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600427 png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600428 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600429#endif
430#if defined(PNG_READ_tEXt_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600431 else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600432 {
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500433 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
434 {
435 png_push_save_buffer(png_ptr);
436 return;
437 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600438 png_push_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600439 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600440#endif
441#if defined(PNG_READ_zTXt_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600442 else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600443 {
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500444 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
445 {
446 png_push_save_buffer(png_ptr);
447 return;
448 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600449 png_push_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600450 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600451#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600452#if defined(PNG_READ_iTXt_SUPPORTED)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600453 else if (!png_memcmp(png_ptr->chunk_name, png_iTXt, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600454 {
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500455 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
456 {
457 png_push_save_buffer(png_ptr);
458 return;
459 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600460 png_push_handle_iTXt(png_ptr, info_ptr, png_ptr->push_length);
461 }
462#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600463 else
464 {
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500465 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
466 {
467 png_push_save_buffer(png_ptr);
468 return;
469 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600470 png_push_handle_unknown(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600471 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600472
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600473 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
Guy Schalnat6d764711995-12-19 03:22:19 -0600474}
475
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500476void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500477png_push_crc_skip(png_structp png_ptr, png_uint_32 skip)
Guy Schalnat6d764711995-12-19 03:22:19 -0600478{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600479 png_ptr->process_mode = PNG_SKIP_MODE;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500480 png_ptr->skip_length = skip;
Guy Schalnat6d764711995-12-19 03:22:19 -0600481}
482
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500483void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500484png_push_crc_finish(png_structp png_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -0600485{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600486 if (png_ptr->skip_length && png_ptr->save_buffer_size)
487 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500488 png_size_t save_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600489
Andreas Dilger47a0c421997-05-16 02:46:07 -0500490 if (png_ptr->skip_length < (png_uint_32)png_ptr->save_buffer_size)
491 save_size = (png_size_t)png_ptr->skip_length;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600492 else
493 save_size = png_ptr->save_buffer_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600494
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600495 png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
Guy Schalnat6d764711995-12-19 03:22:19 -0600496
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600497 png_ptr->skip_length -= save_size;
498 png_ptr->buffer_size -= save_size;
499 png_ptr->save_buffer_size -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500500 png_ptr->save_buffer_ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600501 }
502 if (png_ptr->skip_length && png_ptr->current_buffer_size)
503 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500504 png_size_t save_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600505
Andreas Dilger47a0c421997-05-16 02:46:07 -0500506 if (png_ptr->skip_length < (png_uint_32)png_ptr->current_buffer_size)
507 save_size = (png_size_t)png_ptr->skip_length;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600508 else
509 save_size = png_ptr->current_buffer_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600510
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600511 png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
Guy Schalnat6d764711995-12-19 03:22:19 -0600512
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600513 png_ptr->skip_length -= save_size;
514 png_ptr->buffer_size -= save_size;
515 png_ptr->current_buffer_size -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500516 png_ptr->current_buffer_ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600517 }
518 if (!png_ptr->skip_length)
519 {
520 if (png_ptr->buffer_size < 4)
521 {
522 png_push_save_buffer(png_ptr);
523 return;
524 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600525
526 png_crc_finish(png_ptr, 0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500527 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600528 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600529}
530
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -0500531void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500532png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length)
Guy Schalnat6d764711995-12-19 03:22:19 -0600533{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600534 png_bytep ptr;
Guy Schalnat6d764711995-12-19 03:22:19 -0600535
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600536 ptr = buffer;
537 if (png_ptr->save_buffer_size)
538 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500539 png_size_t save_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600540
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600541 if (length < png_ptr->save_buffer_size)
542 save_size = length;
543 else
544 save_size = png_ptr->save_buffer_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600545
Andreas Dilger47a0c421997-05-16 02:46:07 -0500546 png_memcpy(ptr, png_ptr->save_buffer_ptr, save_size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600547 length -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500548 ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600549 png_ptr->buffer_size -= save_size;
550 png_ptr->save_buffer_size -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500551 png_ptr->save_buffer_ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600552 }
553 if (length && png_ptr->current_buffer_size)
554 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500555 png_size_t save_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600556
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600557 if (length < png_ptr->current_buffer_size)
558 save_size = length;
559 else
560 save_size = png_ptr->current_buffer_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600561
Andreas Dilger47a0c421997-05-16 02:46:07 -0500562 png_memcpy(ptr, png_ptr->current_buffer_ptr, save_size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600563 png_ptr->buffer_size -= save_size;
564 png_ptr->current_buffer_size -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500565 png_ptr->current_buffer_ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600566 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600567}
568
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500569void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600570png_push_save_buffer(png_structp png_ptr)
571{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600572 if (png_ptr->save_buffer_size)
573 {
574 if (png_ptr->save_buffer_ptr != png_ptr->save_buffer)
575 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500576 png_size_t i,istop;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600577 png_bytep sp;
578 png_bytep dp;
Guy Schalnat6d764711995-12-19 03:22:19 -0600579
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500580 istop = png_ptr->save_buffer_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600581 for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500582 i < istop; i++, sp++, dp++)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600583 {
584 *dp = *sp;
585 }
586 }
587 }
588 if (png_ptr->save_buffer_size + png_ptr->current_buffer_size >
589 png_ptr->save_buffer_max)
590 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500591 png_size_t new_max;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600592 png_bytep old_buffer;
Guy Schalnat6d764711995-12-19 03:22:19 -0600593
Andreas Dilger47a0c421997-05-16 02:46:07 -0500594 new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600595 old_buffer = png_ptr->save_buffer;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600596 png_ptr->save_buffer = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600597 (png_uint_32)new_max);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500598 png_memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
599 png_free(png_ptr, old_buffer);
600 png_ptr->save_buffer_max = new_max;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600601 }
602 if (png_ptr->current_buffer_size)
603 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500604 png_memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
605 png_ptr->current_buffer_ptr, png_ptr->current_buffer_size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600606 png_ptr->save_buffer_size += png_ptr->current_buffer_size;
607 png_ptr->current_buffer_size = 0;
608 }
609 png_ptr->save_buffer_ptr = png_ptr->save_buffer;
610 png_ptr->buffer_size = 0;
Guy Schalnat6d764711995-12-19 03:22:19 -0600611}
612
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500613void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600614png_push_restore_buffer(png_structp png_ptr, png_bytep buffer,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500615 png_size_t buffer_length)
Guy Schalnat6d764711995-12-19 03:22:19 -0600616{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600617 png_ptr->current_buffer = buffer;
618 png_ptr->current_buffer_size = buffer_length;
619 png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size;
620 png_ptr->current_buffer_ptr = png_ptr->current_buffer;
Guy Schalnat6d764711995-12-19 03:22:19 -0600621}
622
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500623void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -0500624png_push_read_IDAT(png_structp png_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -0600625{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600626#ifdef PNG_USE_LOCAL_ARRAYS
627 PNG_IDAT;
628#endif
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600629 if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600630 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600631 png_byte chunk_length[4];
Guy Schalnat6d764711995-12-19 03:22:19 -0600632
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600633 if (png_ptr->buffer_size < 8)
634 {
635 png_push_save_buffer(png_ptr);
636 return;
637 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600638
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600639 png_push_fill_buffer(png_ptr, chunk_length, 4);
640 png_ptr->push_length = png_get_uint_32(chunk_length);
641
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600642 png_reset_crc(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500643 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600644 png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600645
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600646 if (png_memcmp(png_ptr->chunk_name, (png_bytep)png_IDAT, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600647 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500648 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500649 if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600650 png_error(png_ptr, "Not enough compressed data");
651 return;
652 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600653
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600654 png_ptr->idat_size = png_ptr->push_length;
655 }
656 if (png_ptr->idat_size && png_ptr->save_buffer_size)
657 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500658 png_size_t save_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600659
Andreas Dilger47a0c421997-05-16 02:46:07 -0500660 if (png_ptr->idat_size < (png_uint_32)png_ptr->save_buffer_size)
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600661 {
662 save_size = (png_size_t)png_ptr->idat_size;
663 /* check for overflow */
664 if((png_uint_32)save_size != png_ptr->idat_size)
665 png_error(png_ptr, "save_size overflowed in pngpread");
666 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600667 else
668 save_size = png_ptr->save_buffer_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600669
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600670 png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
671 png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size);
Guy Schalnat6d764711995-12-19 03:22:19 -0600672
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600673 png_ptr->idat_size -= save_size;
674 png_ptr->buffer_size -= save_size;
675 png_ptr->save_buffer_size -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500676 png_ptr->save_buffer_ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600677 }
678 if (png_ptr->idat_size && png_ptr->current_buffer_size)
679 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500680 png_size_t save_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600681
Andreas Dilger47a0c421997-05-16 02:46:07 -0500682 if (png_ptr->idat_size < (png_uint_32)png_ptr->current_buffer_size)
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600683 {
684 save_size = (png_size_t)png_ptr->idat_size;
685 /* check for overflow */
686 if((png_uint_32)save_size != png_ptr->idat_size)
687 png_error(png_ptr, "save_size overflowed in pngpread");
688 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600689 else
690 save_size = png_ptr->current_buffer_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600691
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600692 png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
693 png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size);
Guy Schalnat6d764711995-12-19 03:22:19 -0600694
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600695 png_ptr->idat_size -= save_size;
696 png_ptr->buffer_size -= save_size;
697 png_ptr->current_buffer_size -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500698 png_ptr->current_buffer_ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600699 }
700 if (!png_ptr->idat_size)
701 {
702 if (png_ptr->buffer_size < 4)
703 {
704 png_push_save_buffer(png_ptr);
705 return;
706 }
707
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600708 png_crc_finish(png_ptr, 0);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600709 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600710 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600711}
712
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500713void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600714png_process_IDAT_data(png_structp png_ptr, png_bytep buffer,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500715 png_size_t buffer_length)
Guy Schalnat6d764711995-12-19 03:22:19 -0600716{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600717 int ret;
Guy Schalnat6d764711995-12-19 03:22:19 -0600718
Guy Schalnate5a37791996-06-05 15:50:50 -0500719 if ((png_ptr->flags & PNG_FLAG_ZLIB_FINISHED) && buffer_length)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600720 png_error(png_ptr, "Extra compression data");
Guy Schalnat6d764711995-12-19 03:22:19 -0600721
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600722 png_ptr->zstream.next_in = buffer;
723 png_ptr->zstream.avail_in = (uInt)buffer_length;
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -0600724 for(;;)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600725 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600726 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500727 if (ret != Z_OK)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600728 {
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500729 if (ret == Z_STREAM_END)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600730 {
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500731 if (png_ptr->zstream.avail_in)
732 png_error(png_ptr, "Extra compressed data");
733 if (!(png_ptr->zstream.avail_out))
734 {
735 png_push_process_row(png_ptr);
736 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500737
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500738 png_ptr->mode |= PNG_AFTER_IDAT;
739 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
740 break;
741 }
742 else if (ret == Z_BUF_ERROR)
743 break;
744 else
745 png_error(png_ptr, "Decompression Error");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600746 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600747 if (!(png_ptr->zstream.avail_out))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600748 {
749 png_push_process_row(png_ptr);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600750 png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
751 png_ptr->zstream.next_out = png_ptr->row_buf;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600752 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500753 else
754 break;
755 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600756}
757
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500758void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600759png_push_process_row(png_structp png_ptr)
760{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600761 png_ptr->row_info.color_type = png_ptr->color_type;
Guy Schalnat6d764711995-12-19 03:22:19 -0600762 png_ptr->row_info.width = png_ptr->iwidth;
763 png_ptr->row_info.channels = png_ptr->channels;
764 png_ptr->row_info.bit_depth = png_ptr->bit_depth;
765 png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600766
Guy Schalnat6d764711995-12-19 03:22:19 -0600767 png_ptr->row_info.rowbytes = ((png_ptr->row_info.width *
768 (png_uint_32)png_ptr->row_info.pixel_depth + 7) >> 3);
769
Guy Schalnate5a37791996-06-05 15:50:50 -0500770 png_read_filter_row(png_ptr, &(png_ptr->row_info),
771 png_ptr->row_buf + 1, png_ptr->prev_row + 1,
772 (int)(png_ptr->row_buf[0]));
Guy Schalnat6d764711995-12-19 03:22:19 -0600773
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600774 png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600775 png_ptr->rowbytes + 1);
Guy Schalnat6d764711995-12-19 03:22:19 -0600776
777 if (png_ptr->transformations)
778 png_do_read_transformations(png_ptr);
779
780#if defined(PNG_READ_INTERLACING_SUPPORTED)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600781 /* blow up interlaced rows to full size */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500782 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600783 {
784 if (png_ptr->pass < 6)
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -0600785/* old interface (pre-1.0.9):
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600786 png_do_read_interlace(&(png_ptr->row_info),
Andreas Dilger47a0c421997-05-16 02:46:07 -0500787 png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -0600788 */
789 png_do_read_interlace(png_ptr);
Guy Schalnat6d764711995-12-19 03:22:19 -0600790
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -0600791 switch (png_ptr->pass)
792 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600793 case 0:
794 {
795 int i;
796 for (i = 0; i < 8 && png_ptr->pass == 0; i++)
797 {
798 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
Glenn Randers-Pehrsonbe9de0f2001-01-22 08:52:16 -0600799 png_read_push_finish_row(png_ptr); /* updates png_ptr->pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600800 }
Glenn Randers-Pehrsonbe9de0f2001-01-22 08:52:16 -0600801 if (png_ptr->pass == 2) /* pass 1 might be empty */
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -0600802 {
803 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
804 {
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500805 png_push_have_row(png_ptr, png_bytep_NULL);
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -0600806 png_read_push_finish_row(png_ptr);
807 }
808 }
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500809 if (png_ptr->pass == 4 && png_ptr->height <= 4)
810 {
811 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
812 {
813 png_push_have_row(png_ptr, png_bytep_NULL);
814 png_read_push_finish_row(png_ptr);
815 }
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500816 }
817 if (png_ptr->pass == 6 && png_ptr->height <= 4)
818 {
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500819 png_push_have_row(png_ptr, png_bytep_NULL);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500820 png_read_push_finish_row(png_ptr);
821 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600822 break;
823 }
824 case 1:
825 {
826 int i;
827 for (i = 0; i < 8 && png_ptr->pass == 1; i++)
828 {
829 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
830 png_read_push_finish_row(png_ptr);
831 }
Glenn Randers-Pehrsonbe9de0f2001-01-22 08:52:16 -0600832 if (png_ptr->pass == 2) /* skip top 4 generated rows */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600833 {
834 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
835 {
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500836 png_push_have_row(png_ptr, png_bytep_NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600837 png_read_push_finish_row(png_ptr);
838 }
839 }
840 break;
841 }
842 case 2:
843 {
844 int i;
845 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
846 {
847 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
848 png_read_push_finish_row(png_ptr);
849 }
850 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
851 {
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500852 png_push_have_row(png_ptr, png_bytep_NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600853 png_read_push_finish_row(png_ptr);
854 }
Glenn Randers-Pehrsonbe9de0f2001-01-22 08:52:16 -0600855 if (png_ptr->pass == 4) /* pass 3 might be empty */
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -0600856 {
857 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
858 {
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500859 png_push_have_row(png_ptr, png_bytep_NULL);
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -0600860 png_read_push_finish_row(png_ptr);
861 }
862 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600863 break;
864 }
865 case 3:
866 {
867 int i;
868 for (i = 0; i < 4 && png_ptr->pass == 3; 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 == 4) /* skip top two generated rows */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600874 {
875 for (i = 0; i < 2 && png_ptr->pass == 4; 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 4:
884 {
885 int i;
886 for (i = 0; i < 2 && png_ptr->pass == 4; 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 < 2 && png_ptr->pass == 4; 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 == 6) /* pass 5 might be empty */
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -0600897 {
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500898 png_push_have_row(png_ptr, png_bytep_NULL);
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -0600899 png_read_push_finish_row(png_ptr);
900 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600901 break;
902 }
903 case 5:
904 {
905 int i;
906 for (i = 0; i < 2 && png_ptr->pass == 5; i++)
907 {
908 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
909 png_read_push_finish_row(png_ptr);
910 }
Glenn Randers-Pehrsonbe9de0f2001-01-22 08:52:16 -0600911 if (png_ptr->pass == 6) /* skip top generated row */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600912 {
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500913 png_push_have_row(png_ptr, png_bytep_NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600914 png_read_push_finish_row(png_ptr);
915 }
916 break;
917 }
918 case 6:
919 {
920 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
921 png_read_push_finish_row(png_ptr);
922 if (png_ptr->pass != 6)
923 break;
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500924 png_push_have_row(png_ptr, png_bytep_NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600925 png_read_push_finish_row(png_ptr);
926 }
927 }
928 }
929 else
Guy Schalnat6d764711995-12-19 03:22:19 -0600930#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600931 {
932 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
933 png_read_push_finish_row(png_ptr);
934 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600935}
936
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500937void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600938png_read_push_finish_row(png_structp png_ptr)
939{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600940#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600941 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600942
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600943 /* start of interlace block */
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600944 const int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600945
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600946 /* offset to next interlace block */
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600947 const int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600948
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600949 /* start of interlace block in the y direction */
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600950 const int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600951
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600952 /* offset to next interlace block in the y direction */
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600953 const int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600954
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600955 /* Width of interlace block. This is not currently used - if you need
956 * it, uncomment it here and in png.h
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600957 const int FARDATA png_pass_width[] = {8, 4, 4, 2, 2, 1, 1};
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600958 */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600959
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600960 /* Height of interlace block. This is not currently used - if you need
961 * it, uncomment it here and in png.h
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600962 const int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600963 */
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600964#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600965
Guy Schalnat6d764711995-12-19 03:22:19 -0600966 png_ptr->row_number++;
967 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600968 return;
Guy Schalnat6d764711995-12-19 03:22:19 -0600969
970 if (png_ptr->interlaced)
971 {
972 png_ptr->row_number = 0;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600973 png_memset_check(png_ptr, png_ptr->prev_row, 0,
974 png_ptr->rowbytes + 1);
Guy Schalnat6d764711995-12-19 03:22:19 -0600975 do
976 {
977 png_ptr->pass++;
Glenn Randers-Pehrsoncdf140b2001-01-15 12:22:32 -0600978 if ((png_ptr->pass == 1 && png_ptr->width < 5) ||
979 (png_ptr->pass == 3 && png_ptr->width < 3) ||
980 (png_ptr->pass == 5 && png_ptr->width < 2))
981 png_ptr->pass++;
982
Guy Schalnat6d764711995-12-19 03:22:19 -0600983 if (png_ptr->pass >= 7)
984 break;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600985
Guy Schalnat6d764711995-12-19 03:22:19 -0600986 png_ptr->iwidth = (png_ptr->width +
987 png_pass_inc[png_ptr->pass] - 1 -
988 png_pass_start[png_ptr->pass]) /
989 png_pass_inc[png_ptr->pass];
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600990
991 png_ptr->irowbytes = ((png_ptr->iwidth *
992 png_ptr->pixel_depth + 7) >> 3) + 1;
993
Guy Schalnat6d764711995-12-19 03:22:19 -0600994 if (png_ptr->transformations & PNG_INTERLACE)
995 break;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600996
997 png_ptr->num_rows = (png_ptr->height +
998 png_pass_yinc[png_ptr->pass] - 1 -
999 png_pass_ystart[png_ptr->pass]) /
1000 png_pass_yinc[png_ptr->pass];
1001
1002 } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0);
Guy Schalnat6d764711995-12-19 03:22:19 -06001003 }
1004}
1005
Guy Schalnat6d764711995-12-19 03:22:19 -06001006#if defined(PNG_READ_tEXt_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001007void /* PRIVATE */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001008png_push_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
1009 length)
Guy Schalnat6d764711995-12-19 03:22:19 -06001010{
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001011 if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001012 {
1013 png_error(png_ptr, "Out of place tEXt");
1014 /* to quiet some compiler warnings */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001015 if(info_ptr == NULL) return;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001016 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001017
Andreas Dilger47a0c421997-05-16 02:46:07 -05001018#ifdef PNG_MAX_MALLOC_64K
1019 png_ptr->skip_length = 0; /* This may not be necessary */
1020
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001021 if (length > (png_uint_32)65535L) /* Can't hold entire string in memory */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001022 {
1023 png_warning(png_ptr, "tEXt chunk too large to fit in memory");
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001024 png_ptr->skip_length = length - (png_uint_32)65535L;
1025 length = (png_uint_32)65535L;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001026 }
1027#endif
1028
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001029 png_ptr->current_text = (png_charp)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001030 (png_uint_32)(length+1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001031 png_ptr->current_text[length] = '\0';
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001032 png_ptr->current_text_ptr = png_ptr->current_text;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001033 png_ptr->current_text_size = (png_size_t)length;
1034 png_ptr->current_text_left = (png_size_t)length;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001035 png_ptr->process_mode = PNG_READ_tEXt_MODE;
Guy Schalnat6d764711995-12-19 03:22:19 -06001036}
1037
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001038void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001039png_push_read_tEXt(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -06001040{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001041 if (png_ptr->buffer_size && png_ptr->current_text_left)
1042 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001043 png_size_t text_size;
Guy Schalnat6d764711995-12-19 03:22:19 -06001044
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001045 if (png_ptr->buffer_size < png_ptr->current_text_left)
1046 text_size = png_ptr->buffer_size;
1047 else
1048 text_size = png_ptr->current_text_left;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001049 png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001050 png_ptr->current_text_left -= text_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001051 png_ptr->current_text_ptr += text_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001052 }
1053 if (!(png_ptr->current_text_left))
1054 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001055 png_textp text_ptr;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001056 png_charp text;
1057 png_charp key;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001058 int ret;
Guy Schalnat6d764711995-12-19 03:22:19 -06001059
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001060 if (png_ptr->buffer_size < 4)
1061 {
1062 png_push_save_buffer(png_ptr);
1063 return;
1064 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001065
Andreas Dilger47a0c421997-05-16 02:46:07 -05001066 png_push_crc_finish(png_ptr);
1067
1068#if defined(PNG_MAX_MALLOC_64K)
1069 if (png_ptr->skip_length)
1070 return;
1071#endif
Guy Schalnat6d764711995-12-19 03:22:19 -06001072
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001073 key = png_ptr->current_text;
Guy Schalnat6d764711995-12-19 03:22:19 -06001074
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001075 for (text = key; *text; text++)
1076 /* empty loop */ ;
Guy Schalnat6d764711995-12-19 03:22:19 -06001077
Andreas Dilger47a0c421997-05-16 02:46:07 -05001078 if (text != key + png_ptr->current_text_size)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001079 text++;
1080
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001081 text_ptr = (png_textp)png_malloc(png_ptr, (png_uint_32)sizeof(png_text));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001082 text_ptr->compression = PNG_TEXT_COMPRESSION_NONE;
1083 text_ptr->key = key;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001084#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001085 text_ptr->lang = NULL;
1086 text_ptr->lang_key = NULL;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001087#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001088 text_ptr->text = text;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001089
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001090 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001091
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001092 png_free(png_ptr, key);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001093 png_free(png_ptr, text_ptr);
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001094 png_ptr->current_text = NULL;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001095
1096 if (ret)
1097 png_warning(png_ptr, "Insufficient memory to store text chunk.");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001098 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001099}
1100#endif
1101
1102#if defined(PNG_READ_zTXt_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001103void /* PRIVATE */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001104png_push_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
1105 length)
Guy Schalnat6d764711995-12-19 03:22:19 -06001106{
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001107 if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001108 {
1109 png_error(png_ptr, "Out of place zTXt");
1110 /* to quiet some compiler warnings */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001111 if(info_ptr == NULL) return;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001112 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001113
Andreas Dilger47a0c421997-05-16 02:46:07 -05001114#ifdef PNG_MAX_MALLOC_64K
1115 /* We can't handle zTXt chunks > 64K, since we don't have enough space
1116 * to be able to store the uncompressed data. Actually, the threshold
1117 * is probably around 32K, but it isn't as definite as 64K is.
1118 */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001119 if (length > (png_uint_32)65535L)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001120 {
1121 png_warning(png_ptr, "zTXt chunk too large to fit in memory");
1122 png_push_crc_skip(png_ptr, length);
1123 return;
1124 }
1125#endif
1126
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001127 png_ptr->current_text = (png_charp)png_malloc(png_ptr,
1128 (png_uint_32)(length+1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001129 png_ptr->current_text[length] = '\0';
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001130 png_ptr->current_text_ptr = png_ptr->current_text;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001131 png_ptr->current_text_size = (png_size_t)length;
1132 png_ptr->current_text_left = (png_size_t)length;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001133 png_ptr->process_mode = PNG_READ_zTXt_MODE;
Guy Schalnat6d764711995-12-19 03:22:19 -06001134}
1135
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001136void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001137png_push_read_zTXt(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -06001138{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001139 if (png_ptr->buffer_size && png_ptr->current_text_left)
1140 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001141 png_size_t text_size;
Guy Schalnat6d764711995-12-19 03:22:19 -06001142
Andreas Dilger47a0c421997-05-16 02:46:07 -05001143 if (png_ptr->buffer_size < (png_uint_32)png_ptr->current_text_left)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001144 text_size = png_ptr->buffer_size;
1145 else
1146 text_size = png_ptr->current_text_left;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001147 png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001148 png_ptr->current_text_left -= text_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001149 png_ptr->current_text_ptr += text_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001150 }
1151 if (!(png_ptr->current_text_left))
1152 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001153 png_textp text_ptr;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001154 png_charp text;
1155 png_charp key;
1156 int ret;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001157 png_size_t text_size, key_size;
Guy Schalnat6d764711995-12-19 03:22:19 -06001158
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001159 if (png_ptr->buffer_size < 4)
1160 {
1161 png_push_save_buffer(png_ptr);
1162 return;
1163 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001164
Andreas Dilger47a0c421997-05-16 02:46:07 -05001165 png_push_crc_finish(png_ptr);
Guy Schalnat6d764711995-12-19 03:22:19 -06001166
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001167 key = png_ptr->current_text;
Guy Schalnat6d764711995-12-19 03:22:19 -06001168
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001169 for (text = key; *text; text++)
1170 /* empty loop */ ;
Guy Schalnat6d764711995-12-19 03:22:19 -06001171
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001172 /* zTXt can't have zero text */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001173 if (text == key + png_ptr->current_text_size)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001174 {
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001175 png_ptr->current_text = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001176 png_free(png_ptr, key);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001177 return;
1178 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001179
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001180 text++;
Guy Schalnat6d764711995-12-19 03:22:19 -06001181
Andreas Dilger47a0c421997-05-16 02:46:07 -05001182 if (*text != PNG_TEXT_COMPRESSION_zTXt) /* check compression byte */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001183 {
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001184 png_ptr->current_text = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001185 png_free(png_ptr, key);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001186 return;
1187 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001188
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001189 text++;
Guy Schalnat6d764711995-12-19 03:22:19 -06001190
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001191 png_ptr->zstream.next_in = (png_bytep )text;
1192 png_ptr->zstream.avail_in = (uInt)(png_ptr->current_text_size -
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001193 (text - key));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001194 png_ptr->zstream.next_out = png_ptr->zbuf;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001195 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnat6d764711995-12-19 03:22:19 -06001196
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001197 key_size = text - key;
1198 text_size = 0;
1199 text = NULL;
1200 ret = Z_STREAM_END;
Guy Schalnat6d764711995-12-19 03:22:19 -06001201
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001202 while (png_ptr->zstream.avail_in)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001203 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001204 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001205 if (ret != Z_OK && ret != Z_STREAM_END)
1206 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001207 inflateReset(&png_ptr->zstream);
1208 png_ptr->zstream.avail_in = 0;
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001209 png_ptr->current_text = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001210 png_free(png_ptr, key);
1211 png_free(png_ptr, text);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001212 return;
1213 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001214 if (!(png_ptr->zstream.avail_out) || ret == Z_STREAM_END)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001215 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001216 if (text == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001217 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001218 text = (png_charp)png_malloc(png_ptr,
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001219 (png_uint_32)(png_ptr->zbuf_size - png_ptr->zstream.avail_out
1220 + key_size + 1));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001221 png_memcpy(text + key_size, png_ptr->zbuf,
1222 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
1223 png_memcpy(text, key, key_size);
1224 text_size = key_size + png_ptr->zbuf_size -
1225 png_ptr->zstream.avail_out;
1226 *(text + text_size) = '\0';
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001227 }
1228 else
1229 {
1230 png_charp tmp;
Guy Schalnat6d764711995-12-19 03:22:19 -06001231
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001232 tmp = text;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001233 text = (png_charp)png_malloc(png_ptr, text_size +
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001234 (png_uint_32)(png_ptr->zbuf_size - png_ptr->zstream.avail_out
1235 + 1));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001236 png_memcpy(text, tmp, text_size);
1237 png_free(png_ptr, tmp);
1238 png_memcpy(text + text_size, png_ptr->zbuf,
1239 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
1240 text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out;
1241 *(text + text_size) = '\0';
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001242 }
1243 if (ret != Z_STREAM_END)
1244 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001245 png_ptr->zstream.next_out = png_ptr->zbuf;
1246 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001247 }
1248 }
1249 else
1250 {
1251 break;
1252 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001253
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001254 if (ret == Z_STREAM_END)
1255 break;
1256 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001257
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001258 inflateReset(&png_ptr->zstream);
1259 png_ptr->zstream.avail_in = 0;
Guy Schalnat6d764711995-12-19 03:22:19 -06001260
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001261 if (ret != Z_STREAM_END)
1262 {
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001263 png_ptr->current_text = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001264 png_free(png_ptr, key);
1265 png_free(png_ptr, text);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001266 return;
1267 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001268
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001269 png_ptr->current_text = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001270 png_free(png_ptr, key);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001271 key = text;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001272 text += key_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001273
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001274 text_ptr = (png_textp)png_malloc(png_ptr, (png_uint_32)sizeof(png_text));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001275 text_ptr->compression = PNG_TEXT_COMPRESSION_zTXt;
1276 text_ptr->key = key;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001277#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001278 text_ptr->lang = NULL;
1279 text_ptr->lang_key = NULL;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001280#endif
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001281 text_ptr->text = text;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001282
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001283 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001284
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001285 png_free(png_ptr, key);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001286 png_free(png_ptr, text_ptr);
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001287
1288 if (ret)
1289 png_warning(png_ptr, "Insufficient memory to store text chunk.");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001290 }
1291}
1292#endif
1293
1294#if defined(PNG_READ_iTXt_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001295void /* PRIVATE */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001296png_push_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
1297 length)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001298{
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001299 if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001300 {
1301 png_error(png_ptr, "Out of place iTXt");
1302 /* to quiet some compiler warnings */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001303 if(info_ptr == NULL) return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001304 }
1305
1306#ifdef PNG_MAX_MALLOC_64K
1307 png_ptr->skip_length = 0; /* This may not be necessary */
1308
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001309 if (length > (png_uint_32)65535L) /* Can't hold entire string in memory */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001310 {
1311 png_warning(png_ptr, "iTXt chunk too large to fit in memory");
1312 png_ptr->skip_length = length - (png_uint_32)65535L;
1313 length = (png_uint_32)65535L;
1314 }
1315#endif
1316
1317 png_ptr->current_text = (png_charp)png_malloc(png_ptr,
1318 (png_uint_32)(length+1));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001319 png_ptr->current_text[length] = '\0';
1320 png_ptr->current_text_ptr = png_ptr->current_text;
1321 png_ptr->current_text_size = (png_size_t)length;
1322 png_ptr->current_text_left = (png_size_t)length;
1323 png_ptr->process_mode = PNG_READ_iTXt_MODE;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001324}
1325
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001326void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001327png_push_read_iTXt(png_structp png_ptr, png_infop info_ptr)
1328{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001329
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001330 if (png_ptr->buffer_size && png_ptr->current_text_left)
1331 {
1332 png_size_t text_size;
1333
1334 if (png_ptr->buffer_size < png_ptr->current_text_left)
1335 text_size = png_ptr->buffer_size;
1336 else
1337 text_size = png_ptr->current_text_left;
1338 png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
1339 png_ptr->current_text_left -= text_size;
1340 png_ptr->current_text_ptr += text_size;
1341 }
1342 if (!(png_ptr->current_text_left))
1343 {
1344 png_textp text_ptr;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001345 png_charp key;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001346 int comp_flag;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001347 png_charp lang;
1348 png_charp lang_key;
1349 png_charp text;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001350 int ret;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001351
1352 if (png_ptr->buffer_size < 4)
1353 {
1354 png_push_save_buffer(png_ptr);
1355 return;
1356 }
1357
1358 png_push_crc_finish(png_ptr);
1359
1360#if defined(PNG_MAX_MALLOC_64K)
1361 if (png_ptr->skip_length)
1362 return;
1363#endif
1364
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001365 key = png_ptr->current_text;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001366
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001367 for (lang = key; *lang; lang++)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001368 /* empty loop */ ;
1369
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001370 if (lang != key + png_ptr->current_text_size)
1371 lang++;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001372
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001373 comp_flag = *lang++;
1374 lang++; /* skip comp_type, always zero */
1375
1376 for (lang_key = lang; *lang_key; lang_key++)
1377 /* empty loop */ ;
1378 lang_key++; /* skip NUL separator */
1379
1380 for (text = lang_key; *text; text++)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001381 /* empty loop */ ;
1382
1383 if (text != key + png_ptr->current_text_size)
1384 text++;
1385
1386 text_ptr = (png_textp)png_malloc(png_ptr, (png_uint_32)sizeof(png_text));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001387 text_ptr->compression = comp_flag + 2;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001388 text_ptr->key = key;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001389 text_ptr->lang = lang;
1390 text_ptr->lang_key = lang_key;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001391 text_ptr->text = text;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001392 text_ptr->text_length = 0;
1393 text_ptr->itxt_length = png_strlen(text);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001394
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001395 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001396
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001397 png_ptr->current_text = NULL;
1398
Andreas Dilger47a0c421997-05-16 02:46:07 -05001399 png_free(png_ptr, text_ptr);
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001400 if (ret)
1401 png_warning(png_ptr, "Insufficient memory to store iTXt chunk.");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001402 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001403}
1404#endif
1405
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001406/* This function is called when we haven't found a handler for this
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001407 * chunk. If there isn't a problem with the chunk itself (ie a bad chunk
1408 * name or a critical chunk), the chunk is (currently) silently ignored.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001409 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001410void /* PRIVATE */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001411png_push_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32
1412 length)
Guy Schalnat6d764711995-12-19 03:22:19 -06001413{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001414 png_uint_32 skip=0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001415 png_check_chunk_name(png_ptr, png_ptr->chunk_name);
1416
1417 if (!(png_ptr->chunk_name[0] & 0x20))
1418 {
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001419#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001420 if(png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
1421 HANDLE_CHUNK_ALWAYS
1422#if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001423 && png_ptr->read_user_chunk_fn == NULL
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001424#endif
1425 )
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001426#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001427 png_chunk_error(png_ptr, "unknown critical chunk");
1428
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001429 /* to quiet compiler warnings about unused info_ptr */
1430 if (info_ptr == NULL)
1431 return;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001432 }
1433
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001434#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
1435 if (png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS)
1436 {
1437 png_unknown_chunk chunk;
1438
1439#ifdef PNG_MAX_MALLOC_64K
1440 if (length > (png_uint_32)65535L)
1441 {
1442 png_warning(png_ptr, "unknown chunk too large to fit in memory");
1443 skip = length - (png_uint_32)65535L;
1444 length = (png_uint_32)65535L;
1445 }
1446#endif
1447
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001448 png_strcpy((png_charp)chunk.name, (png_charp)png_ptr->chunk_name);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001449 chunk.data = (png_bytep)png_malloc(png_ptr, length);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001450 png_crc_read(png_ptr, chunk.data, length);
1451 chunk.size = length;
Glenn Randers-Pehrsone1eff582001-04-14 20:15:41 -05001452#if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001453 if(png_ptr->read_user_chunk_fn != NULL)
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001454 {
1455 /* callback to user unknown chunk handler */
1456 if ((*(png_ptr->read_user_chunk_fn)) (png_ptr, &chunk) <= 0)
1457 {
1458 if (!(png_ptr->chunk_name[0] & 0x20))
1459 if(png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
1460 HANDLE_CHUNK_ALWAYS)
1461 png_chunk_error(png_ptr, "unknown critical chunk");
1462 }
1463 png_set_unknown_chunks(png_ptr, info_ptr, &chunk, 1);
Glenn Randers-Pehrsone1eff582001-04-14 20:15:41 -05001464 }
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001465 else
1466#endif
1467 png_set_unknown_chunks(png_ptr, info_ptr, &chunk, 1);
1468 png_free(png_ptr, chunk.data);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001469 }
1470 else
1471#endif
1472 skip=length;
1473 png_push_crc_skip(png_ptr, skip);
Guy Schalnat6d764711995-12-19 03:22:19 -06001474}
1475
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001476void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001477png_push_have_info(png_structp png_ptr, png_infop info_ptr)
1478{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001479 if (png_ptr->info_fn != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001480 (*(png_ptr->info_fn))(png_ptr, info_ptr);
1481}
1482
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001483void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001484png_push_have_end(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -06001485{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001486 if (png_ptr->end_fn != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001487 (*(png_ptr->end_fn))(png_ptr, info_ptr);
Guy Schalnat6d764711995-12-19 03:22:19 -06001488}
1489
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001490void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001491png_push_have_row(png_structp png_ptr, png_bytep row)
1492{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001493 if (png_ptr->row_fn != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001494 (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number,
1495 (int)png_ptr->pass);
Guy Schalnat6d764711995-12-19 03:22:19 -06001496}
1497
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001498void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001499png_progressive_combine_row (png_structp png_ptr,
1500 png_bytep old_row, png_bytep new_row)
Guy Schalnat6d764711995-12-19 03:22:19 -06001501{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001502#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001503 const int FARDATA png_pass_dsp_mask[7] =
1504 {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001505#endif
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -05001506 if (new_row != NULL) /* new_row must == png_ptr->row_buf here. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001507 png_combine_row(png_ptr, old_row, png_pass_dsp_mask[png_ptr->pass]);
Guy Schalnat6d764711995-12-19 03:22:19 -06001508}
1509
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001510void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001511png_set_progressive_read_fn(png_structp png_ptr, png_voidp progressive_ptr,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001512 png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
1513 png_progressive_end_ptr end_fn)
Guy Schalnat6d764711995-12-19 03:22:19 -06001514{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001515 png_ptr->info_fn = info_fn;
1516 png_ptr->row_fn = row_fn;
Guy Schalnat6d764711995-12-19 03:22:19 -06001517 png_ptr->end_fn = end_fn;
Guy Schalnate5a37791996-06-05 15:50:50 -05001518
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -05001519 png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer);
Guy Schalnat6d764711995-12-19 03:22:19 -06001520}
1521
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001522png_voidp PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001523png_get_progressive_ptr(png_structp png_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -06001524{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001525 return png_ptr->io_ptr;
Guy Schalnat6d764711995-12-19 03:22:19 -06001526}
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001527#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */