blob: 9fa4720ef38e9fa22cbf146635f37c5ea87689d9 [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-Pehrson166c5a31999-12-10 09:43:02 -06004 * libpng 1.0.5h - December 10, 1999
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
7 * Copyright (c) 1996, 1997 Andreas Dilger
Glenn Randers-Pehrsonc9442291999-01-06 21:50:16 -06008 * Copyright (c) 1998, 1999 Glenn Randers-Pehrson
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
Guy Schalnat6d764711995-12-19 03:22:19 -060027void
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 */
Guy Schalnat6d764711995-12-19 03:22:19 -060042void
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 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600102void
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
134void
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
154#if defined(PNG_READ_oFFs_SUPPORTED)
155 PNG_oFFs;
156#endif
157#if defined(PNG_READ_pCAL_SUPPORTED)
158 PNG_pCAL;
159#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600160#if defined(PNG_READ_sCAL_SUPPORTED)
161 PNG_sCAL;
162#endif
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600163#if defined(PNG_READ_pHYs_SUPPORTED)
164 PNG_pHYs;
165#endif
166#if defined(PNG_READ_sBIT_SUPPORTED)
167 PNG_sBIT;
168#endif
169#if defined(PNG_READ_sRGB_SUPPORTED)
170 PNG_sRGB;
171#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600172#if defined(PNG_READ_iCCP_SUPPORTED)
173 PNG_iCCP;
174#endif
175#if defined(PNG_READ_sPLT_SUPPORTED)
176 PNG_sPLT;
177#endif
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600178#if defined(PNG_READ_tEXt_SUPPORTED)
179 PNG_tEXt;
180#endif
181#if defined(PNG_READ_tIME_SUPPORTED)
182 PNG_tIME;
183#endif
184#if defined(PNG_READ_tRNS_SUPPORTED)
185 PNG_tRNS;
186#endif
187#if defined(PNG_READ_zTXt_SUPPORTED)
188 PNG_zTXt;
189#endif
190#endif /* PNG_USE_LOCAL_ARRAYS */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600191 /* First we make sure we have enough data for the 4 byte chunk name
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600192 * and the 4 byte chunk length before proceeding with decoding the
193 * chunk data. To fully decode each of these chunks, we also make
194 * sure we have enough data in the buffer for the 4 byte CRC at the
195 * end of every chunk (except IDAT, which is handled separately).
196 */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600197 if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600198 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600199 png_byte chunk_length[4];
Guy Schalnat6d764711995-12-19 03:22:19 -0600200
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600201 if (png_ptr->buffer_size < 8)
202 {
203 png_push_save_buffer(png_ptr);
204 return;
205 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600206
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600207 png_push_fill_buffer(png_ptr, chunk_length, 4);
208 png_ptr->push_length = png_get_uint_32(chunk_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600209 png_reset_crc(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500210 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600211 png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600212 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600213
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600214 if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600215 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600216 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
217 {
218 png_push_save_buffer(png_ptr);
219 return;
220 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600221
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600222 png_handle_IHDR(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600223 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600224 else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600225 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600226 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600227 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600228 png_push_save_buffer(png_ptr);
229 return;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600230 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600231
232 png_handle_PLTE(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600233 }
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600234 else if (!png_memcmp(png_ptr->chunk_name, (png_bytep)png_IDAT, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600235 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600236 /* If we reach an IDAT chunk, this means we have read all of the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600237 * header chunks, and we can start reading the image (or if this
238 * is called after the image has been read - we have an error).
239 */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600240 if (png_ptr->mode & PNG_HAVE_IDAT)
241 {
242 if (png_ptr->push_length == 0)
243 return;
244
245 if (png_ptr->mode & PNG_AFTER_IDAT)
246 png_error(png_ptr, "Too many IDAT's found");
247 }
248
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600249 png_ptr->idat_size = png_ptr->push_length;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600250 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600251 png_ptr->process_mode = PNG_READ_IDAT_MODE;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600252 png_push_have_info(png_ptr, info_ptr);
253 png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
254 png_ptr->zstream.next_out = png_ptr->row_buf;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600255 return;
256 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600257 else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600258 {
259 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
260 {
261 png_push_save_buffer(png_ptr);
262 return;
263 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600264
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600265 png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length);
266 png_ptr->process_mode = PNG_READ_DONE_MODE;
267 png_push_have_end(png_ptr, info_ptr);
268 }
269#if defined(PNG_READ_gAMA_SUPPORTED)
270 else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
271 {
272 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
273 {
274 png_push_save_buffer(png_ptr);
275 return;
276 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600277
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600278 png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600279 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600280#endif
281#if defined(PNG_READ_sBIT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600282 else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600283 {
284 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
285 {
286 png_push_save_buffer(png_ptr);
287 return;
288 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600289
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600290 png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600291 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600292#endif
293#if defined(PNG_READ_cHRM_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600294 else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600295 {
296 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
297 {
298 png_push_save_buffer(png_ptr);
299 return;
300 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600301
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600302 png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600303 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600304#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600305#if defined(PNG_READ_sRGB_SUPPORTED)
306 else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
307 {
308 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
309 {
310 png_push_save_buffer(png_ptr);
311 return;
312 }
313
314 png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length);
315 }
316#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600317#if defined(PNG_READ_iCCP_SUPPORTED)
318 else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4))
319 {
320 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
321 {
322 png_push_save_buffer(png_ptr);
323 return;
324 }
325
326 png_handle_iCCP(png_ptr, info_ptr, png_ptr->push_length);
327 }
328#endif
329#if defined(PNG_READ_sPLT_SUPPORTED)
330 else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4))
331 {
332 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
333 {
334 png_push_save_buffer(png_ptr);
335 return;
336 }
337
338 png_handle_sPLT(png_ptr, info_ptr, png_ptr->push_length);
339 }
340#endif
Guy Schalnat6d764711995-12-19 03:22:19 -0600341#if defined(PNG_READ_tRNS_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600342 else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600343 {
344 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
345 {
346 png_push_save_buffer(png_ptr);
347 return;
348 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600349
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 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600361
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600362 png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600363 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600364#endif
365#if defined(PNG_READ_hIST_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600366 else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 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 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600373
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600374 png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600375 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600376#endif
377#if defined(PNG_READ_pHYs_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600378 else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600379 {
380 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
381 {
382 png_push_save_buffer(png_ptr);
383 return;
384 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600385
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600386 png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600387 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600388#endif
389#if defined(PNG_READ_oFFs_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600390 else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600391 {
392 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
393 {
394 png_push_save_buffer(png_ptr);
395 return;
396 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600397
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600398 png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600399 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600400#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500401#if defined(PNG_READ_pCAL_SUPPORTED)
402 else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
403 {
404 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
405 {
406 png_push_save_buffer(png_ptr);
407 return;
408 }
409
410 png_handle_pCAL(png_ptr, info_ptr, png_ptr->push_length);
411 }
412#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600413#if defined(PNG_READ_sCAL_SUPPORTED)
414 else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4))
415 {
416 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
417 {
418 png_push_save_buffer(png_ptr);
419 return;
420 }
421
422 png_handle_sCAL(png_ptr, info_ptr, png_ptr->push_length);
423 }
424#endif
Guy Schalnat6d764711995-12-19 03:22:19 -0600425#if defined(PNG_READ_tIME_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600426 else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600427 {
428 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
429 {
430 png_push_save_buffer(png_ptr);
431 return;
432 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600433
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600434 png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600435 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600436#endif
437#if defined(PNG_READ_tEXt_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600438 else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600439 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600440 png_push_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600441 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600442#endif
443#if defined(PNG_READ_zTXt_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600444 else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600445 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600446 png_push_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600447 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600448#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600449#if defined(PNG_READ_iTXt_SUPPORTED)
450 else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
451 {
452 png_push_handle_iTXt(png_ptr, info_ptr, png_ptr->push_length);
453 }
454#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600455 else
456 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600457 png_push_handle_unknown(png_ptr, info_ptr, png_ptr->push_length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600458 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600459
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600460 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
Guy Schalnat6d764711995-12-19 03:22:19 -0600461}
462
463void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500464png_push_crc_skip(png_structp png_ptr, png_uint_32 skip)
Guy Schalnat6d764711995-12-19 03:22:19 -0600465{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600466 png_ptr->process_mode = PNG_SKIP_MODE;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500467 png_ptr->skip_length = skip;
Guy Schalnat6d764711995-12-19 03:22:19 -0600468}
469
470void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500471png_push_crc_finish(png_structp png_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -0600472{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600473 if (png_ptr->skip_length && png_ptr->save_buffer_size)
474 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500475 png_size_t save_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600476
Andreas Dilger47a0c421997-05-16 02:46:07 -0500477 if (png_ptr->skip_length < (png_uint_32)png_ptr->save_buffer_size)
478 save_size = (png_size_t)png_ptr->skip_length;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600479 else
480 save_size = png_ptr->save_buffer_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600481
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600482 png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
Guy Schalnat6d764711995-12-19 03:22:19 -0600483
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600484 png_ptr->skip_length -= save_size;
485 png_ptr->buffer_size -= save_size;
486 png_ptr->save_buffer_size -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500487 png_ptr->save_buffer_ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600488 }
489 if (png_ptr->skip_length && png_ptr->current_buffer_size)
490 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500491 png_size_t save_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600492
Andreas Dilger47a0c421997-05-16 02:46:07 -0500493 if (png_ptr->skip_length < (png_uint_32)png_ptr->current_buffer_size)
494 save_size = (png_size_t)png_ptr->skip_length;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600495 else
496 save_size = png_ptr->current_buffer_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600497
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600498 png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
Guy Schalnat6d764711995-12-19 03:22:19 -0600499
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600500 png_ptr->skip_length -= save_size;
501 png_ptr->buffer_size -= save_size;
502 png_ptr->current_buffer_size -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500503 png_ptr->current_buffer_ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600504 }
505 if (!png_ptr->skip_length)
506 {
507 if (png_ptr->buffer_size < 4)
508 {
509 png_push_save_buffer(png_ptr);
510 return;
511 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600512
513 png_crc_finish(png_ptr, 0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500514 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600515 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600516}
517
518void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500519png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length)
Guy Schalnat6d764711995-12-19 03:22:19 -0600520{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600521 png_bytep ptr;
Guy Schalnat6d764711995-12-19 03:22:19 -0600522
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600523 ptr = buffer;
524 if (png_ptr->save_buffer_size)
525 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500526 png_size_t save_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600527
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600528 if (length < png_ptr->save_buffer_size)
529 save_size = length;
530 else
531 save_size = png_ptr->save_buffer_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600532
Andreas Dilger47a0c421997-05-16 02:46:07 -0500533 png_memcpy(ptr, png_ptr->save_buffer_ptr, save_size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600534 length -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500535 ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600536 png_ptr->buffer_size -= save_size;
537 png_ptr->save_buffer_size -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500538 png_ptr->save_buffer_ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600539 }
540 if (length && png_ptr->current_buffer_size)
541 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500542 png_size_t save_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600543
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600544 if (length < png_ptr->current_buffer_size)
545 save_size = length;
546 else
547 save_size = png_ptr->current_buffer_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600548
Andreas Dilger47a0c421997-05-16 02:46:07 -0500549 png_memcpy(ptr, png_ptr->current_buffer_ptr, save_size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600550 png_ptr->buffer_size -= save_size;
551 png_ptr->current_buffer_size -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500552 png_ptr->current_buffer_ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600553 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600554}
555
556void
557png_push_save_buffer(png_structp png_ptr)
558{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600559 if (png_ptr->save_buffer_size)
560 {
561 if (png_ptr->save_buffer_ptr != png_ptr->save_buffer)
562 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500563 png_size_t i,istop;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600564 png_bytep sp;
565 png_bytep dp;
Guy Schalnat6d764711995-12-19 03:22:19 -0600566
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500567 istop = png_ptr->save_buffer_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600568 for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500569 i < istop; i++, sp++, dp++)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600570 {
571 *dp = *sp;
572 }
573 }
574 }
575 if (png_ptr->save_buffer_size + png_ptr->current_buffer_size >
576 png_ptr->save_buffer_max)
577 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500578 png_size_t new_max;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600579 png_bytep old_buffer;
Guy Schalnat6d764711995-12-19 03:22:19 -0600580
Andreas Dilger47a0c421997-05-16 02:46:07 -0500581 new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600582 old_buffer = png_ptr->save_buffer;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600583 png_ptr->save_buffer = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600584 (png_uint_32)new_max);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500585 png_memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600586 png_free(png_ptr, old_buffer);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500587 png_ptr->save_buffer_max = new_max;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600588 }
589 if (png_ptr->current_buffer_size)
590 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500591 png_memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
592 png_ptr->current_buffer_ptr, png_ptr->current_buffer_size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600593 png_ptr->save_buffer_size += png_ptr->current_buffer_size;
594 png_ptr->current_buffer_size = 0;
595 }
596 png_ptr->save_buffer_ptr = png_ptr->save_buffer;
597 png_ptr->buffer_size = 0;
Guy Schalnat6d764711995-12-19 03:22:19 -0600598}
599
600void
601png_push_restore_buffer(png_structp png_ptr, png_bytep buffer,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500602 png_size_t buffer_length)
Guy Schalnat6d764711995-12-19 03:22:19 -0600603{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600604 png_ptr->current_buffer = buffer;
605 png_ptr->current_buffer_size = buffer_length;
606 png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size;
607 png_ptr->current_buffer_ptr = png_ptr->current_buffer;
Guy Schalnat6d764711995-12-19 03:22:19 -0600608}
609
610void
Guy Schalnate5a37791996-06-05 15:50:50 -0500611png_push_read_IDAT(png_structp png_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -0600612{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600613#ifdef PNG_USE_LOCAL_ARRAYS
614 PNG_IDAT;
615#endif
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600616 if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600617 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600618 png_byte chunk_length[4];
Guy Schalnat6d764711995-12-19 03:22:19 -0600619
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600620 if (png_ptr->buffer_size < 8)
621 {
622 png_push_save_buffer(png_ptr);
623 return;
624 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600625
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600626 png_push_fill_buffer(png_ptr, chunk_length, 4);
627 png_ptr->push_length = png_get_uint_32(chunk_length);
628
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600629 png_reset_crc(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500630 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600631 png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600632
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600633 if (png_memcmp(png_ptr->chunk_name, (png_bytep)png_IDAT, 4))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600634 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500635 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500636 if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600637 png_error(png_ptr, "Not enough compressed data");
638 return;
639 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600640
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600641 png_ptr->idat_size = png_ptr->push_length;
642 }
643 if (png_ptr->idat_size && png_ptr->save_buffer_size)
644 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500645 png_size_t save_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600646
Andreas Dilger47a0c421997-05-16 02:46:07 -0500647 if (png_ptr->idat_size < (png_uint_32)png_ptr->save_buffer_size)
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600648 {
649 save_size = (png_size_t)png_ptr->idat_size;
650 /* check for overflow */
651 if((png_uint_32)save_size != png_ptr->idat_size)
652 png_error(png_ptr, "save_size overflowed in pngpread");
653 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600654 else
655 save_size = png_ptr->save_buffer_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600656
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600657 png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
658 png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size);
Guy Schalnat6d764711995-12-19 03:22:19 -0600659
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600660 png_ptr->idat_size -= save_size;
661 png_ptr->buffer_size -= save_size;
662 png_ptr->save_buffer_size -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500663 png_ptr->save_buffer_ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600664 }
665 if (png_ptr->idat_size && png_ptr->current_buffer_size)
666 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500667 png_size_t save_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600668
Andreas Dilger47a0c421997-05-16 02:46:07 -0500669 if (png_ptr->idat_size < (png_uint_32)png_ptr->current_buffer_size)
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600670 {
671 save_size = (png_size_t)png_ptr->idat_size;
672 /* check for overflow */
673 if((png_uint_32)save_size != png_ptr->idat_size)
674 png_error(png_ptr, "save_size overflowed in pngpread");
675 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600676 else
677 save_size = png_ptr->current_buffer_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600678
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600679 png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
680 png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size);
Guy Schalnat6d764711995-12-19 03:22:19 -0600681
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600682 png_ptr->idat_size -= save_size;
683 png_ptr->buffer_size -= save_size;
684 png_ptr->current_buffer_size -= save_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500685 png_ptr->current_buffer_ptr += save_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600686 }
687 if (!png_ptr->idat_size)
688 {
689 if (png_ptr->buffer_size < 4)
690 {
691 png_push_save_buffer(png_ptr);
692 return;
693 }
694
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600695 png_crc_finish(png_ptr, 0);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600696 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600697 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600698}
699
700void
701png_process_IDAT_data(png_structp png_ptr, png_bytep buffer,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500702 png_size_t buffer_length)
Guy Schalnat6d764711995-12-19 03:22:19 -0600703{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600704 int ret;
Guy Schalnat6d764711995-12-19 03:22:19 -0600705
Guy Schalnate5a37791996-06-05 15:50:50 -0500706 if ((png_ptr->flags & PNG_FLAG_ZLIB_FINISHED) && buffer_length)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600707 png_error(png_ptr, "Extra compression data");
Guy Schalnat6d764711995-12-19 03:22:19 -0600708
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600709 png_ptr->zstream.next_in = buffer;
710 png_ptr->zstream.avail_in = (uInt)buffer_length;
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -0600711 for(;;)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600712 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600713 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600714 if (ret == Z_STREAM_END)
715 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600716 if (png_ptr->zstream.avail_in)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600717 png_error(png_ptr, "Extra compressed data");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500718 if (!(png_ptr->zstream.avail_out))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600719 {
720 png_push_process_row(png_ptr);
721 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500722
723 png_ptr->mode |= PNG_AFTER_IDAT;
Guy Schalnate5a37791996-06-05 15:50:50 -0500724 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600725 break;
726 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500727 else if (ret == Z_BUF_ERROR)
728 break;
729 else if (ret != Z_OK)
Guy Schalnate5a37791996-06-05 15:50:50 -0500730 png_error(png_ptr, "Decompression Error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600731 if (!(png_ptr->zstream.avail_out))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600732 {
733 png_push_process_row(png_ptr);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600734 png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
735 png_ptr->zstream.next_out = png_ptr->row_buf;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600736 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500737 else
738 break;
739 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600740}
741
742void
743png_push_process_row(png_structp png_ptr)
744{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600745 png_ptr->row_info.color_type = png_ptr->color_type;
Guy Schalnat6d764711995-12-19 03:22:19 -0600746 png_ptr->row_info.width = png_ptr->iwidth;
747 png_ptr->row_info.channels = png_ptr->channels;
748 png_ptr->row_info.bit_depth = png_ptr->bit_depth;
749 png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600750
Guy Schalnat6d764711995-12-19 03:22:19 -0600751 png_ptr->row_info.rowbytes = ((png_ptr->row_info.width *
752 (png_uint_32)png_ptr->row_info.pixel_depth + 7) >> 3);
753
Guy Schalnate5a37791996-06-05 15:50:50 -0500754 png_read_filter_row(png_ptr, &(png_ptr->row_info),
755 png_ptr->row_buf + 1, png_ptr->prev_row + 1,
756 (int)(png_ptr->row_buf[0]));
Guy Schalnat6d764711995-12-19 03:22:19 -0600757
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600758 png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600759 png_ptr->rowbytes + 1);
Guy Schalnat6d764711995-12-19 03:22:19 -0600760
761 if (png_ptr->transformations)
762 png_do_read_transformations(png_ptr);
763
764#if defined(PNG_READ_INTERLACING_SUPPORTED)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600765 /* blow up interlaced rows to full size */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500766 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600767 {
768 if (png_ptr->pass < 6)
769 png_do_read_interlace(&(png_ptr->row_info),
Andreas Dilger47a0c421997-05-16 02:46:07 -0500770 png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
Guy Schalnat6d764711995-12-19 03:22:19 -0600771
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600772 switch (png_ptr->pass)
773 {
774 case 0:
775 {
776 int i;
777 for (i = 0; i < 8 && png_ptr->pass == 0; i++)
778 {
779 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
780 png_read_push_finish_row(png_ptr);
781 }
782 break;
783 }
784 case 1:
785 {
786 int i;
787 for (i = 0; i < 8 && png_ptr->pass == 1; i++)
788 {
789 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
790 png_read_push_finish_row(png_ptr);
791 }
792 if (png_ptr->pass == 2)
793 {
794 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
795 {
796 png_push_have_row(png_ptr, NULL);
797 png_read_push_finish_row(png_ptr);
798 }
799 }
800 break;
801 }
802 case 2:
803 {
804 int i;
805 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
806 {
807 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
808 png_read_push_finish_row(png_ptr);
809 }
810 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
811 {
812 png_push_have_row(png_ptr, NULL);
813 png_read_push_finish_row(png_ptr);
814 }
815 break;
816 }
817 case 3:
818 {
819 int i;
820 for (i = 0; i < 4 && png_ptr->pass == 3; i++)
821 {
822 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
823 png_read_push_finish_row(png_ptr);
824 }
825 if (png_ptr->pass == 4)
826 {
827 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
828 {
829 png_push_have_row(png_ptr, NULL);
830 png_read_push_finish_row(png_ptr);
831 }
832 }
833 break;
834 }
835 case 4:
836 {
837 int i;
838 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
839 {
840 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
841 png_read_push_finish_row(png_ptr);
842 }
843 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
844 {
845 png_push_have_row(png_ptr, NULL);
846 png_read_push_finish_row(png_ptr);
847 }
848 break;
849 }
850 case 5:
851 {
852 int i;
853 for (i = 0; i < 2 && png_ptr->pass == 5; i++)
854 {
855 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
856 png_read_push_finish_row(png_ptr);
857 }
858 if (png_ptr->pass == 6)
859 {
860 png_push_have_row(png_ptr, NULL);
861 png_read_push_finish_row(png_ptr);
862 }
863 break;
864 }
865 case 6:
866 {
867 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
868 png_read_push_finish_row(png_ptr);
869 if (png_ptr->pass != 6)
870 break;
871 png_push_have_row(png_ptr, NULL);
872 png_read_push_finish_row(png_ptr);
873 }
874 }
875 }
876 else
Guy Schalnat6d764711995-12-19 03:22:19 -0600877#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600878 {
879 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
880 png_read_push_finish_row(png_ptr);
881 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600882}
883
884void
885png_read_push_finish_row(png_structp png_ptr)
886{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600887#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600888 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600889
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600890 /* start of interlace block */
891 const int png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600892
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600893 /* offset to next interlace block */
894 const int png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600895
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600896 /* start of interlace block in the y direction */
897 const int png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600898
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600899 /* offset to next interlace block in the y direction */
900 const int png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600901
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600902 /* Width of interlace block. This is not currently used - if you need
903 * it, uncomment it here and in png.h
904 const int png_pass_width[] = {8, 4, 4, 2, 2, 1, 1};
905 */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600906
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600907 /* Height of interlace block. This is not currently used - if you need
908 * it, uncomment it here and in png.h
909 const int png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
910 */
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600911#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600912
Guy Schalnat6d764711995-12-19 03:22:19 -0600913 png_ptr->row_number++;
914 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600915 return;
Guy Schalnat6d764711995-12-19 03:22:19 -0600916
917 if (png_ptr->interlaced)
918 {
919 png_ptr->row_number = 0;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600920 png_memset_check(png_ptr, png_ptr->prev_row, 0,
921 png_ptr->rowbytes + 1);
Guy Schalnat6d764711995-12-19 03:22:19 -0600922 do
923 {
924 png_ptr->pass++;
925 if (png_ptr->pass >= 7)
926 break;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600927
Guy Schalnat6d764711995-12-19 03:22:19 -0600928 png_ptr->iwidth = (png_ptr->width +
929 png_pass_inc[png_ptr->pass] - 1 -
930 png_pass_start[png_ptr->pass]) /
931 png_pass_inc[png_ptr->pass];
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600932
933 png_ptr->irowbytes = ((png_ptr->iwidth *
934 png_ptr->pixel_depth + 7) >> 3) + 1;
935
Guy Schalnat6d764711995-12-19 03:22:19 -0600936 if (png_ptr->transformations & PNG_INTERLACE)
937 break;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600938
939 png_ptr->num_rows = (png_ptr->height +
940 png_pass_yinc[png_ptr->pass] - 1 -
941 png_pass_ystart[png_ptr->pass]) /
942 png_pass_yinc[png_ptr->pass];
943
944 } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0);
Guy Schalnat6d764711995-12-19 03:22:19 -0600945 }
946}
947
Guy Schalnat6d764711995-12-19 03:22:19 -0600948#if defined(PNG_READ_tEXt_SUPPORTED)
949void
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600950png_push_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
Guy Schalnat6d764711995-12-19 03:22:19 -0600951{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600952 if (png_ptr->mode == PNG_BEFORE_IHDR || png_ptr->mode & PNG_HAVE_IEND)
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600953 {
954 png_error(png_ptr, "Out of place tEXt");
955 /* to quiet some compiler warnings */
956 if(info_ptr == NULL) return;
957 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600958
Andreas Dilger47a0c421997-05-16 02:46:07 -0500959#ifdef PNG_MAX_MALLOC_64K
960 png_ptr->skip_length = 0; /* This may not be necessary */
961
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600962 if (length > (png_uint_32)65535L) /* Can't hold the entire string in memory */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500963 {
964 png_warning(png_ptr, "tEXt chunk too large to fit in memory");
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600965 png_ptr->skip_length = length - (png_uint_32)65535L;
966 length = (png_uint_32)65535L;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500967 }
968#endif
969
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600970 png_ptr->current_text = (png_charp)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600971 (png_uint_32)(length+1));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500972 png_ptr->current_text[length] = '\0';
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600973 png_ptr->current_text_ptr = png_ptr->current_text;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600974 png_ptr->current_text_size = (png_size_t)length;
975 png_ptr->current_text_left = (png_size_t)length;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600976 png_ptr->process_mode = PNG_READ_tEXt_MODE;
Guy Schalnat6d764711995-12-19 03:22:19 -0600977}
978
979void
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600980png_push_read_tEXt(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -0600981{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600982 if (png_ptr->buffer_size && png_ptr->current_text_left)
983 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500984 png_size_t text_size;
Guy Schalnat6d764711995-12-19 03:22:19 -0600985
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600986 if (png_ptr->buffer_size < png_ptr->current_text_left)
987 text_size = png_ptr->buffer_size;
988 else
989 text_size = png_ptr->current_text_left;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500990 png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600991 png_ptr->current_text_left -= text_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500992 png_ptr->current_text_ptr += text_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600993 }
994 if (!(png_ptr->current_text_left))
995 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500996 png_textp text_ptr;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600997 png_charp text;
998 png_charp key;
Guy Schalnat6d764711995-12-19 03:22:19 -0600999
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001000 if (png_ptr->buffer_size < 4)
1001 {
1002 png_push_save_buffer(png_ptr);
1003 return;
1004 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001005
Andreas Dilger47a0c421997-05-16 02:46:07 -05001006 png_push_crc_finish(png_ptr);
1007
1008#if defined(PNG_MAX_MALLOC_64K)
1009 if (png_ptr->skip_length)
1010 return;
1011#endif
Guy Schalnat6d764711995-12-19 03:22:19 -06001012
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001013 key = png_ptr->current_text;
1014 png_ptr->current_text = 0;
Guy Schalnat6d764711995-12-19 03:22:19 -06001015
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001016 for (text = key; *text; text++)
1017 /* empty loop */ ;
Guy Schalnat6d764711995-12-19 03:22:19 -06001018
Andreas Dilger47a0c421997-05-16 02:46:07 -05001019 if (text != key + png_ptr->current_text_size)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001020 text++;
1021
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001022 text_ptr = (png_textp)png_malloc(png_ptr, (png_uint_32)sizeof(png_text));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001023 text_ptr->compression = PNG_TEXT_COMPRESSION_NONE;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001024 text_ptr->lang = (char *)NULL;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001025 text_ptr->key = key;
1026 text_ptr->text = text;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001027
Andreas Dilger47a0c421997-05-16 02:46:07 -05001028 png_set_text(png_ptr, info_ptr, text_ptr, 1);
1029
1030 png_free(png_ptr, text_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001031 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001032}
1033#endif
1034
1035#if defined(PNG_READ_zTXt_SUPPORTED)
1036void
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001037png_push_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
Guy Schalnat6d764711995-12-19 03:22:19 -06001038{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001039 if (png_ptr->mode == PNG_BEFORE_IHDR || png_ptr->mode & PNG_HAVE_IEND)
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001040 {
1041 png_error(png_ptr, "Out of place zTXt");
1042 /* to quiet some compiler warnings */
1043 if(info_ptr == NULL) return;
1044 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001045
Andreas Dilger47a0c421997-05-16 02:46:07 -05001046#ifdef PNG_MAX_MALLOC_64K
1047 /* We can't handle zTXt chunks > 64K, since we don't have enough space
1048 * to be able to store the uncompressed data. Actually, the threshold
1049 * is probably around 32K, but it isn't as definite as 64K is.
1050 */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001051 if (length > (png_uint_32)65535L)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001052 {
1053 png_warning(png_ptr, "zTXt chunk too large to fit in memory");
1054 png_push_crc_skip(png_ptr, length);
1055 return;
1056 }
1057#endif
1058
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001059 png_ptr->current_text = (png_charp)png_malloc(png_ptr,
1060 (png_uint_32)(length+1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001061 png_ptr->current_text[length] = '\0';
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001062 png_ptr->current_text_ptr = png_ptr->current_text;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001063 png_ptr->current_text_size = (png_size_t)length;
1064 png_ptr->current_text_left = (png_size_t)length;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001065 png_ptr->process_mode = PNG_READ_zTXt_MODE;
Guy Schalnat6d764711995-12-19 03:22:19 -06001066}
1067
1068void
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001069png_push_read_zTXt(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -06001070{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001071 if (png_ptr->buffer_size && png_ptr->current_text_left)
1072 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001073 png_size_t text_size;
Guy Schalnat6d764711995-12-19 03:22:19 -06001074
Andreas Dilger47a0c421997-05-16 02:46:07 -05001075 if (png_ptr->buffer_size < (png_uint_32)png_ptr->current_text_left)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001076 text_size = png_ptr->buffer_size;
1077 else
1078 text_size = png_ptr->current_text_left;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001079 png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001080 png_ptr->current_text_left -= text_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001081 png_ptr->current_text_ptr += text_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001082 }
1083 if (!(png_ptr->current_text_left))
1084 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001085 png_textp text_ptr;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001086 png_charp text;
1087 png_charp key;
1088 int ret;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001089 png_size_t text_size, key_size;
Guy Schalnat6d764711995-12-19 03:22:19 -06001090
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001091 if (png_ptr->buffer_size < 4)
1092 {
1093 png_push_save_buffer(png_ptr);
1094 return;
1095 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001096
Andreas Dilger47a0c421997-05-16 02:46:07 -05001097 png_push_crc_finish(png_ptr);
Guy Schalnat6d764711995-12-19 03:22:19 -06001098
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001099 key = png_ptr->current_text;
1100 png_ptr->current_text = 0;
Guy Schalnat6d764711995-12-19 03:22:19 -06001101
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001102 for (text = key; *text; text++)
1103 /* empty loop */ ;
Guy Schalnat6d764711995-12-19 03:22:19 -06001104
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001105 /* zTXt can't have zero text */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001106 if (text == key + png_ptr->current_text_size)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001107 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001108 png_free(png_ptr, key);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001109 return;
1110 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001111
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001112 text++;
Guy Schalnat6d764711995-12-19 03:22:19 -06001113
Andreas Dilger47a0c421997-05-16 02:46:07 -05001114 if (*text != PNG_TEXT_COMPRESSION_zTXt) /* check compression byte */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001115 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001116 png_free(png_ptr, key);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001117 return;
1118 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001119
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001120 text++;
Guy Schalnat6d764711995-12-19 03:22:19 -06001121
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001122 png_ptr->zstream.next_in = (png_bytep )text;
1123 png_ptr->zstream.avail_in = (uInt)(png_ptr->current_text_size -
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001124 (text - key));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001125 png_ptr->zstream.next_out = png_ptr->zbuf;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001126 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnat6d764711995-12-19 03:22:19 -06001127
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001128 key_size = text - key;
1129 text_size = 0;
1130 text = NULL;
1131 ret = Z_STREAM_END;
Guy Schalnat6d764711995-12-19 03:22:19 -06001132
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001133 while (png_ptr->zstream.avail_in)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001134 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001135 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001136 if (ret != Z_OK && ret != Z_STREAM_END)
1137 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001138 inflateReset(&png_ptr->zstream);
1139 png_ptr->zstream.avail_in = 0;
1140 png_free(png_ptr, key);
1141 png_free(png_ptr, text);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001142 return;
1143 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001144 if (!(png_ptr->zstream.avail_out) || ret == Z_STREAM_END)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001145 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001146 if (text == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001147 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001148 text = (png_charp)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001149 (png_uint_32)(png_ptr->zbuf_size - png_ptr->zstream.avail_out +
1150 key_size + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001151 png_memcpy(text + key_size, png_ptr->zbuf,
1152 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
1153 png_memcpy(text, key, key_size);
1154 text_size = key_size + png_ptr->zbuf_size -
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001155 png_ptr->zstream.avail_out;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001156 *(text + text_size) = '\0';
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001157 }
1158 else
1159 {
1160 png_charp tmp;
Guy Schalnat6d764711995-12-19 03:22:19 -06001161
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001162 tmp = text;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001163 text = (png_charp)png_malloc(png_ptr, text_size +
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001164 (png_uint_32)(png_ptr->zbuf_size - png_ptr->zstream.avail_out
1165 + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001166 png_memcpy(text, tmp, text_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001167 png_free(png_ptr, tmp);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001168 png_memcpy(text + text_size, png_ptr->zbuf,
1169 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001170 text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001171 *(text + text_size) = '\0';
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001172 }
1173 if (ret != Z_STREAM_END)
1174 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001175 png_ptr->zstream.next_out = png_ptr->zbuf;
1176 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001177 }
1178 }
1179 else
1180 {
1181 break;
1182 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001183
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001184 if (ret == Z_STREAM_END)
1185 break;
1186 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001187
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001188 inflateReset(&png_ptr->zstream);
1189 png_ptr->zstream.avail_in = 0;
Guy Schalnat6d764711995-12-19 03:22:19 -06001190
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001191 if (ret != Z_STREAM_END)
1192 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001193 png_free(png_ptr, key);
1194 png_free(png_ptr, text);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001195 return;
1196 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001197
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001198 png_free(png_ptr, key);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001199 key = text;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001200 text += key_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001201
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001202 text_ptr = (png_textp)png_malloc(png_ptr, (png_uint_32)sizeof(png_text));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001203 text_ptr->compression = PNG_TEXT_COMPRESSION_zTXt;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001204 text_ptr->lang = (char *)NULL;
1205 text_ptr->key = key;
1206 text_ptr->text = text;
1207
1208 png_set_text(png_ptr, info_ptr, text_ptr, 1);
1209
1210 png_free(png_ptr, text_ptr);
1211 }
1212}
1213#endif
1214
1215#if defined(PNG_READ_iTXt_SUPPORTED)
1216void
1217png_push_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1218{
1219 if (png_ptr->mode == PNG_BEFORE_IHDR || png_ptr->mode & PNG_HAVE_IEND)
1220 {
1221 png_error(png_ptr, "Out of place iTXt");
1222 /* to quiet some compiler warnings */
1223 if(info_ptr == NULL) return;
1224 }
1225
1226#ifdef PNG_MAX_MALLOC_64K
1227 png_ptr->skip_length = 0; /* This may not be necessary */
1228
1229 if (length > (png_uint_32)65535L) /* Can't hold the entire string in memory */
1230 {
1231 png_warning(png_ptr, "iTXt chunk too large to fit in memory");
1232 png_ptr->skip_length = length - (png_uint_32)65535L;
1233 length = (png_uint_32)65535L;
1234 }
1235#endif
1236
1237 png_ptr->current_text = (png_charp)png_malloc(png_ptr,
1238 (png_uint_32)(length+1));
1239 png_ptr->current_text[length] = '\0';
1240 png_ptr->current_text_ptr = png_ptr->current_text;
1241 png_ptr->current_text_size = (png_size_t)length;
1242 png_ptr->current_text_left = (png_size_t)length;
1243 png_ptr->process_mode = PNG_READ_iTXt_MODE;
1244}
1245
1246void
1247png_push_read_iTXt(png_structp png_ptr, png_infop info_ptr)
1248{
1249 if (png_ptr->buffer_size && png_ptr->current_text_left)
1250 {
1251 png_size_t text_size;
1252
1253 if (png_ptr->buffer_size < png_ptr->current_text_left)
1254 text_size = png_ptr->buffer_size;
1255 else
1256 text_size = png_ptr->current_text_left;
1257 png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
1258 png_ptr->current_text_left -= text_size;
1259 png_ptr->current_text_ptr += text_size;
1260 }
1261 if (!(png_ptr->current_text_left))
1262 {
1263 png_textp text_ptr;
1264 png_charp text;
1265 png_charp lang;
1266 png_charp key;
1267
1268 if (png_ptr->buffer_size < 4)
1269 {
1270 png_push_save_buffer(png_ptr);
1271 return;
1272 }
1273
1274 png_push_crc_finish(png_ptr);
1275
1276#if defined(PNG_MAX_MALLOC_64K)
1277 if (png_ptr->skip_length)
1278 return;
1279#endif
1280
1281 lang = png_ptr->current_text;
1282 png_ptr->current_text = 0;
1283
1284 for (key = lang; *key; key++)
1285 /* empty loop */ ;
1286
1287 if (key != lang + png_ptr->current_text_size)
1288 key++;
1289
1290 for (text = key; *text; text++)
1291 /* empty loop */ ;
1292
1293 if (text != key + png_ptr->current_text_size)
1294 text++;
1295
1296 text_ptr = (png_textp)png_malloc(png_ptr, (png_uint_32)sizeof(png_text));
1297 text_ptr->compression = PNG_TEXT_COMPRESSION_NONE;
1298 text_ptr->lang = lang;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001299 text_ptr->key = key;
1300 text_ptr->text = text;
1301
1302 png_set_text(png_ptr, info_ptr, text_ptr, 1);
1303
1304 png_free(png_ptr, text_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001305 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001306}
1307#endif
1308
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001309/* This function is called when we haven't found a handler for this
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05001310 * chunk. In the future we will have code here that can handle
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001311 * user-defined callback functions for unknown chunks before they are
1312 * ignored or cause an error. If there isn't a problem with the
1313 * chunk itself (ie a bad chunk name or a critical chunk), the chunk
1314 * is (currently) silently ignored.
1315 */
Guy Schalnat6d764711995-12-19 03:22:19 -06001316void
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001317png_push_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
Guy Schalnat6d764711995-12-19 03:22:19 -06001318{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001319 png_check_chunk_name(png_ptr, png_ptr->chunk_name);
1320
1321 if (!(png_ptr->chunk_name[0] & 0x20))
1322 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001323 png_chunk_error(png_ptr, "unknown critical chunk");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001324 /* to quiet some compiler warnings */
1325 if(info_ptr == NULL) return;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001326 }
1327
1328 png_push_crc_skip(png_ptr, length);
Guy Schalnat6d764711995-12-19 03:22:19 -06001329}
1330
1331void
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001332png_push_have_info(png_structp png_ptr, png_infop info_ptr)
1333{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001334 if (png_ptr->info_fn != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001335 (*(png_ptr->info_fn))(png_ptr, info_ptr);
1336}
1337
1338void
1339png_push_have_end(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -06001340{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001341 if (png_ptr->end_fn != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001342 (*(png_ptr->end_fn))(png_ptr, info_ptr);
Guy Schalnat6d764711995-12-19 03:22:19 -06001343}
1344
1345void
1346png_push_have_row(png_structp png_ptr, png_bytep row)
1347{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001348 if (png_ptr->row_fn != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001349 (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number,
1350 (int)png_ptr->pass);
Guy Schalnat6d764711995-12-19 03:22:19 -06001351}
1352
Guy Schalnat6d764711995-12-19 03:22:19 -06001353void
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001354png_progressive_combine_row (png_structp png_ptr,
1355 png_bytep old_row, png_bytep new_row)
Guy Schalnat6d764711995-12-19 03:22:19 -06001356{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001357#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001358 const int png_pass_dsp_mask[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001359#endif
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -05001360 if (new_row != NULL) /* new_row must == png_ptr->row_buf here. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001361 png_combine_row(png_ptr, old_row, png_pass_dsp_mask[png_ptr->pass]);
Guy Schalnat6d764711995-12-19 03:22:19 -06001362}
1363
1364void
1365png_set_progressive_read_fn(png_structp png_ptr, png_voidp progressive_ptr,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001366 png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
1367 png_progressive_end_ptr end_fn)
Guy Schalnat6d764711995-12-19 03:22:19 -06001368{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001369 png_ptr->info_fn = info_fn;
1370 png_ptr->row_fn = row_fn;
Guy Schalnat6d764711995-12-19 03:22:19 -06001371 png_ptr->end_fn = end_fn;
Guy Schalnate5a37791996-06-05 15:50:50 -05001372
1373 png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer);
Guy Schalnat6d764711995-12-19 03:22:19 -06001374}
1375
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001376png_voidp
1377png_get_progressive_ptr(png_structp png_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -06001378{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001379 return png_ptr->io_ptr;
Guy Schalnat6d764711995-12-19 03:22:19 -06001380}
1381
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001382#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
1383