]> git.saurik.com Git - wxWidgets.git/blame - src/png/pngpread.c
typo (thanks Nerijus)
[wxWidgets.git] / src / png / pngpread.c
CommitLineData
c801d85f
KB
1
2/* pngpread.c - read a png file in push mode
3 *
a626cc03 4 * libpng 1.0.3 - January 14, 1999
c801d85f
KB
5 * 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
a626cc03 8 * Copyright (c) 1998, 1999 Glenn Randers-Pehrson
c801d85f
KB
9 */
10
11#define PNG_INTERNAL
a626cc03 12#include "png.h"
c801d85f
KB
13
14#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
15
16void
17png_process_data(png_structp png_ptr, png_infop info_ptr,
18 png_bytep buffer, png_size_t buffer_size)
19{
20 png_push_restore_buffer(png_ptr, buffer, buffer_size);
21
22 while (png_ptr->buffer_size)
23 {
24 png_process_some_data(png_ptr, info_ptr);
25 }
26}
27
28/* What we do with the incoming data depends on what we were previously
29 * doing before we ran out of data...
30 */
31void
32png_process_some_data(png_structp png_ptr, png_infop info_ptr)
33{
34 switch (png_ptr->process_mode)
35 {
36 case PNG_READ_SIG_MODE:
37 {
38 png_push_read_sig(png_ptr, info_ptr);
39 break;
40 }
41 case PNG_READ_CHUNK_MODE:
42 {
43 png_push_read_chunk(png_ptr, info_ptr);
44 break;
45 }
46 case PNG_READ_IDAT_MODE:
47 {
48 png_push_read_IDAT(png_ptr);
49 break;
50 }
51#if defined(PNG_READ_tEXt_SUPPORTED)
52 case PNG_READ_tEXt_MODE:
53 {
54 png_push_read_tEXt(png_ptr, info_ptr);
55 break;
56 }
57#endif
58#if defined(PNG_READ_zTXt_SUPPORTED)
59 case PNG_READ_zTXt_MODE:
60 {
61 png_push_read_zTXt(png_ptr, info_ptr);
62 break;
63 }
64#endif
65 case PNG_SKIP_MODE:
66 {
67 png_push_crc_finish(png_ptr);
68 break;
69 }
70 default:
71 {
72 png_ptr->buffer_size = 0;
73 break;
74 }
75 }
76}
77
78/* Read any remaining signature bytes from the stream and compare them with
79 * the correct PNG signature. It is possible that this routine is called
a626cc03
RR
80 * with bytes already read from the signature, either because they have been
81 * checked by the calling application, or because of multiple calls to this
82 * routine.
c801d85f
KB
83 */
84void
85png_push_read_sig(png_structp png_ptr, png_infop info_ptr)
86{
87 png_size_t num_checked = png_ptr->sig_bytes,
88 num_to_check = 8 - num_checked;
89
90 if (png_ptr->buffer_size < num_to_check)
91 {
92 num_to_check = png_ptr->buffer_size;
93 }
94
a626cc03 95 png_push_fill_buffer(png_ptr, &(info_ptr->signature[num_checked]),
c801d85f
KB
96 num_to_check);
97 png_ptr->sig_bytes += num_to_check;
98
99 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
100 {
101 if (num_checked < 4 &&
102 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
103 png_error(png_ptr, "Not a PNG file");
104 else
105 png_error(png_ptr, "PNG file corrupted by ASCII conversion");
106 }
107 else
108 {
109 if (png_ptr->sig_bytes >= 8)
110 {
111 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
112 }
113 }
114}
115
116void
117png_push_read_chunk(png_structp png_ptr, png_infop info_ptr)
118{
119 /* First we make sure we have enough data for the 4 byte chunk name
120 * and the 4 byte chunk length before proceeding with decoding the
121 * chunk data. To fully decode each of these chunks, we also make
122 * sure we have enough data in the buffer for the 4 byte CRC at the
123 * end of every chunk (except IDAT, which is handled separately).
124 */
125 if (!(png_ptr->flags & PNG_FLAG_HAVE_CHUNK_HEADER))
126 {
127 png_byte chunk_length[4];
128
129 if (png_ptr->buffer_size < 8)
130 {
131 png_push_save_buffer(png_ptr);
132 return;
133 }
134
135 png_push_fill_buffer(png_ptr, chunk_length, 4);
136 png_ptr->push_length = png_get_uint_32(chunk_length);
137 png_reset_crc(png_ptr);
138 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
139 png_ptr->flags |= PNG_FLAG_HAVE_CHUNK_HEADER;
140 }
141
142 if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
143 {
144 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
145 {
146 png_push_save_buffer(png_ptr);
147 return;
148 }
149
150 png_handle_IHDR(png_ptr, info_ptr, png_ptr->push_length);
151 }
152 else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
153 {
154 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
155 {
156 png_push_save_buffer(png_ptr);
157 return;
158 }
159
160 png_handle_PLTE(png_ptr, info_ptr, png_ptr->push_length);
161 }
162 else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
163 {
164 /* If we reach an IDAT chunk, this means we have read all of the
165 * header chunks, and we can start reading the image (or if this
166 * is called after the image has been read - we have an error).
167 */
168 if (png_ptr->mode & PNG_HAVE_IDAT)
169 {
170 if (png_ptr->push_length == 0)
171 return;
172
173 if (png_ptr->mode & PNG_AFTER_IDAT)
174 png_error(png_ptr, "Too many IDAT's found");
175 }
176
177 png_ptr->idat_size = png_ptr->push_length;
178 png_ptr->mode |= PNG_HAVE_IDAT;
179 png_ptr->process_mode = PNG_READ_IDAT_MODE;
180 png_push_have_info(png_ptr, info_ptr);
181 png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
182 png_ptr->zstream.next_out = png_ptr->row_buf;
183 return;
184 }
185 else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
186 {
187 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
188 {
189 png_push_save_buffer(png_ptr);
190 return;
191 }
192
193 png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length);
194 png_ptr->process_mode = PNG_READ_DONE_MODE;
195 png_push_have_end(png_ptr, info_ptr);
196 }
197#if defined(PNG_READ_gAMA_SUPPORTED)
198 else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
199 {
200 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
201 {
202 png_push_save_buffer(png_ptr);
203 return;
204 }
205
206 png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length);
207 }
208#endif
209#if defined(PNG_READ_sBIT_SUPPORTED)
210 else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
211 {
212 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
213 {
214 png_push_save_buffer(png_ptr);
215 return;
216 }
217
218 png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length);
219 }
220#endif
221#if defined(PNG_READ_cHRM_SUPPORTED)
222 else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
223 {
224 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
225 {
226 png_push_save_buffer(png_ptr);
227 return;
228 }
229
230 png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length);
231 }
232#endif
233#if defined(PNG_READ_sRGB_SUPPORTED)
234 else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
235 {
236 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
237 {
238 png_push_save_buffer(png_ptr);
239 return;
240 }
241
242 png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length);
243 }
244#endif
245#if defined(PNG_READ_tRNS_SUPPORTED)
246 else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
247 {
248 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
249 {
250 png_push_save_buffer(png_ptr);
251 return;
252 }
253
254 png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length);
255 }
256#endif
257#if defined(PNG_READ_bKGD_SUPPORTED)
258 else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
259 {
260 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
261 {
262 png_push_save_buffer(png_ptr);
263 return;
264 }
265
266 png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length);
267 }
268#endif
269#if defined(PNG_READ_hIST_SUPPORTED)
270 else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
271 {
272 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
273 {
274 png_push_save_buffer(png_ptr);
275 return;
276 }
277
278 png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length);
279 }
280#endif
281#if defined(PNG_READ_pHYs_SUPPORTED)
282 else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
283 {
284 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
285 {
286 png_push_save_buffer(png_ptr);
287 return;
288 }
289
290 png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length);
291 }
292#endif
293#if defined(PNG_READ_oFFs_SUPPORTED)
294 else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
295 {
296 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
297 {
298 png_push_save_buffer(png_ptr);
299 return;
300 }
301
302 png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length);
303 }
304#endif
305#if defined(PNG_READ_pCAL_SUPPORTED)
306 else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 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_pCAL(png_ptr, info_ptr, png_ptr->push_length);
315 }
316#endif
317#if defined(PNG_READ_tIME_SUPPORTED)
318 else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 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_tIME(png_ptr, info_ptr, png_ptr->push_length);
327 }
328#endif
329#if defined(PNG_READ_tEXt_SUPPORTED)
330 else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
331 {
332 png_push_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length);
333 }
334#endif
335#if defined(PNG_READ_zTXt_SUPPORTED)
336 else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
337 {
338 png_push_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length);
339 }
340#endif
341 else
342 {
343 png_push_handle_unknown(png_ptr, info_ptr, png_ptr->push_length);
344 }
345
346 png_ptr->flags &= ~PNG_FLAG_HAVE_CHUNK_HEADER;
347}
348
349void
350png_push_crc_skip(png_structp png_ptr, png_uint_32 skip)
351{
352 png_ptr->process_mode = PNG_SKIP_MODE;
353 png_ptr->skip_length = skip;
354}
355
356void
357png_push_crc_finish(png_structp png_ptr)
358{
359 if (png_ptr->skip_length && png_ptr->save_buffer_size)
360 {
361 png_size_t save_size;
362
363 if (png_ptr->skip_length < (png_uint_32)png_ptr->save_buffer_size)
364 save_size = (png_size_t)png_ptr->skip_length;
365 else
366 save_size = png_ptr->save_buffer_size;
367
368 png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
369
370 png_ptr->skip_length -= save_size;
371 png_ptr->buffer_size -= save_size;
372 png_ptr->save_buffer_size -= save_size;
373 png_ptr->save_buffer_ptr += save_size;
374 }
375 if (png_ptr->skip_length && png_ptr->current_buffer_size)
376 {
377 png_size_t save_size;
378
379 if (png_ptr->skip_length < (png_uint_32)png_ptr->current_buffer_size)
380 save_size = (png_size_t)png_ptr->skip_length;
381 else
382 save_size = png_ptr->current_buffer_size;
383
384 png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
385
386 png_ptr->skip_length -= save_size;
387 png_ptr->buffer_size -= save_size;
388 png_ptr->current_buffer_size -= save_size;
389 png_ptr->current_buffer_ptr += save_size;
390 }
391 if (!png_ptr->skip_length)
392 {
393 if (png_ptr->buffer_size < 4)
394 {
395 png_push_save_buffer(png_ptr);
396 return;
397 }
398
399 png_crc_finish(png_ptr, 0);
400 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
401 }
402}
403
404void
405png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length)
406{
407 png_bytep ptr;
408
409 ptr = buffer;
410 if (png_ptr->save_buffer_size)
411 {
412 png_size_t save_size;
413
414 if (length < png_ptr->save_buffer_size)
415 save_size = length;
416 else
417 save_size = png_ptr->save_buffer_size;
418
419 png_memcpy(ptr, png_ptr->save_buffer_ptr, save_size);
420 length -= save_size;
421 ptr += save_size;
422 png_ptr->buffer_size -= save_size;
423 png_ptr->save_buffer_size -= save_size;
424 png_ptr->save_buffer_ptr += save_size;
425 }
426 if (length && png_ptr->current_buffer_size)
427 {
428 png_size_t save_size;
429
430 if (length < png_ptr->current_buffer_size)
431 save_size = length;
432 else
433 save_size = png_ptr->current_buffer_size;
434
435 png_memcpy(ptr, png_ptr->current_buffer_ptr, save_size);
436 png_ptr->buffer_size -= save_size;
437 png_ptr->current_buffer_size -= save_size;
438 png_ptr->current_buffer_ptr += save_size;
439 }
440}
441
442void
443png_push_save_buffer(png_structp png_ptr)
444{
445 if (png_ptr->save_buffer_size)
446 {
447 if (png_ptr->save_buffer_ptr != png_ptr->save_buffer)
448 {
a626cc03 449 png_size_t i,istop;
c801d85f
KB
450 png_bytep sp;
451 png_bytep dp;
452
a626cc03 453 istop = png_ptr->save_buffer_size;
c801d85f 454 for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer;
a626cc03 455 i < istop; i++, sp++, dp++)
c801d85f
KB
456 {
457 *dp = *sp;
458 }
459 }
460 }
461 if (png_ptr->save_buffer_size + png_ptr->current_buffer_size >
462 png_ptr->save_buffer_max)
463 {
464 png_size_t new_max;
465 png_bytep old_buffer;
466
467 new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;
468 old_buffer = png_ptr->save_buffer;
a626cc03 469 png_ptr->save_buffer = (png_bytep)png_malloc(png_ptr,
c801d85f
KB
470 (png_uint_32)new_max);
471 png_memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
472 png_free(png_ptr, old_buffer);
473 png_ptr->save_buffer_max = new_max;
474 }
475 if (png_ptr->current_buffer_size)
476 {
477 png_memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
478 png_ptr->current_buffer_ptr, png_ptr->current_buffer_size);
479 png_ptr->save_buffer_size += png_ptr->current_buffer_size;
480 png_ptr->current_buffer_size = 0;
481 }
482 png_ptr->save_buffer_ptr = png_ptr->save_buffer;
483 png_ptr->buffer_size = 0;
484}
485
486void
487png_push_restore_buffer(png_structp png_ptr, png_bytep buffer,
488 png_size_t buffer_length)
489{
490 png_ptr->current_buffer = buffer;
491 png_ptr->current_buffer_size = buffer_length;
492 png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size;
493 png_ptr->current_buffer_ptr = png_ptr->current_buffer;
494}
495
496void
497png_push_read_IDAT(png_structp png_ptr)
498{
499 if (!(png_ptr->flags & PNG_FLAG_HAVE_CHUNK_HEADER))
500 {
501 png_byte chunk_length[4];
502
503 if (png_ptr->buffer_size < 8)
504 {
505 png_push_save_buffer(png_ptr);
506 return;
507 }
508
509 png_push_fill_buffer(png_ptr, chunk_length, 4);
510 png_ptr->push_length = png_get_uint_32(chunk_length);
511
512 png_reset_crc(png_ptr);
513 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
514 png_ptr->flags |= PNG_FLAG_HAVE_CHUNK_HEADER;
515
516 if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
517 {
518 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
519 if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
520 png_error(png_ptr, "Not enough compressed data");
521 return;
522 }
523
524 png_ptr->idat_size = png_ptr->push_length;
525 }
526 if (png_ptr->idat_size && png_ptr->save_buffer_size)
527 {
528 png_size_t save_size;
529
530 if (png_ptr->idat_size < (png_uint_32)png_ptr->save_buffer_size)
531 {
532 save_size = (png_size_t)png_ptr->idat_size;
533 /* check for overflow */
534 if((png_uint_32)save_size != png_ptr->idat_size)
535 png_error(png_ptr, "save_size overflowed in pngpread");
536 }
537 else
538 save_size = png_ptr->save_buffer_size;
539
540 png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
541 png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size);
542
543 png_ptr->idat_size -= save_size;
544 png_ptr->buffer_size -= save_size;
545 png_ptr->save_buffer_size -= save_size;
546 png_ptr->save_buffer_ptr += save_size;
547 }
548 if (png_ptr->idat_size && png_ptr->current_buffer_size)
549 {
550 png_size_t save_size;
551
552 if (png_ptr->idat_size < (png_uint_32)png_ptr->current_buffer_size)
553 {
554 save_size = (png_size_t)png_ptr->idat_size;
555 /* check for overflow */
556 if((png_uint_32)save_size != png_ptr->idat_size)
557 png_error(png_ptr, "save_size overflowed in pngpread");
558 }
559 else
560 save_size = png_ptr->current_buffer_size;
561
562 png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
563 png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size);
564
565 png_ptr->idat_size -= save_size;
566 png_ptr->buffer_size -= save_size;
567 png_ptr->current_buffer_size -= save_size;
568 png_ptr->current_buffer_ptr += save_size;
569 }
570 if (!png_ptr->idat_size)
571 {
572 if (png_ptr->buffer_size < 4)
573 {
574 png_push_save_buffer(png_ptr);
575 return;
576 }
577
578 png_crc_finish(png_ptr, 0);
579 png_ptr->flags &= ~PNG_FLAG_HAVE_CHUNK_HEADER;
580 }
581}
582
583void
584png_process_IDAT_data(png_structp png_ptr, png_bytep buffer,
585 png_size_t buffer_length)
586{
587 int ret;
588
589 if ((png_ptr->flags & PNG_FLAG_ZLIB_FINISHED) && buffer_length)
590 png_error(png_ptr, "Extra compression data");
591
592 png_ptr->zstream.next_in = buffer;
593 png_ptr->zstream.avail_in = (uInt)buffer_length;
594 for(;;)
595 {
596 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
597 if (ret == Z_STREAM_END)
598 {
599 if (png_ptr->zstream.avail_in)
600 png_error(png_ptr, "Extra compressed data");
601 if (!(png_ptr->zstream.avail_out))
602 {
603 png_push_process_row(png_ptr);
604 }
605
606 png_ptr->mode |= PNG_AFTER_IDAT;
607 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
608 break;
609 }
610 else if (ret == Z_BUF_ERROR)
611 break;
612 else if (ret != Z_OK)
613 png_error(png_ptr, "Decompression Error");
614 if (!(png_ptr->zstream.avail_out))
615 {
616 png_push_process_row(png_ptr);
617 png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
618 png_ptr->zstream.next_out = png_ptr->row_buf;
619 }
620 else
621 break;
622 }
623}
624
625void
626png_push_process_row(png_structp png_ptr)
627{
628 png_ptr->row_info.color_type = png_ptr->color_type;
629 png_ptr->row_info.width = png_ptr->iwidth;
630 png_ptr->row_info.channels = png_ptr->channels;
631 png_ptr->row_info.bit_depth = png_ptr->bit_depth;
632 png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
a626cc03 633
c801d85f
KB
634 png_ptr->row_info.rowbytes = ((png_ptr->row_info.width *
635 (png_uint_32)png_ptr->row_info.pixel_depth + 7) >> 3);
636
637 png_read_filter_row(png_ptr, &(png_ptr->row_info),
638 png_ptr->row_buf + 1, png_ptr->prev_row + 1,
639 (int)(png_ptr->row_buf[0]));
640
641 png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf,
642 png_ptr->rowbytes + 1);
643
644 if (png_ptr->transformations)
645 png_do_read_transformations(png_ptr);
646
647#if defined(PNG_READ_INTERLACING_SUPPORTED)
648 /* blow up interlaced rows to full size */
649 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
650 {
651 if (png_ptr->pass < 6)
652 png_do_read_interlace(&(png_ptr->row_info),
653 png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
654
655 switch (png_ptr->pass)
656 {
657 case 0:
658 {
659 int i;
660 for (i = 0; i < 8 && png_ptr->pass == 0; i++)
661 {
662 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
663 png_read_push_finish_row(png_ptr);
664 }
665 break;
666 }
667 case 1:
668 {
669 int i;
670 for (i = 0; i < 8 && png_ptr->pass == 1; i++)
671 {
672 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
673 png_read_push_finish_row(png_ptr);
674 }
675 if (png_ptr->pass == 2)
676 {
677 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
678 {
679 png_push_have_row(png_ptr, NULL);
680 png_read_push_finish_row(png_ptr);
681 }
682 }
683 break;
684 }
685 case 2:
686 {
687 int i;
688 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
689 {
690 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
691 png_read_push_finish_row(png_ptr);
692 }
693 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
694 {
695 png_push_have_row(png_ptr, NULL);
696 png_read_push_finish_row(png_ptr);
697 }
698 break;
699 }
700 case 3:
701 {
702 int i;
703 for (i = 0; i < 4 && png_ptr->pass == 3; i++)
704 {
705 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
706 png_read_push_finish_row(png_ptr);
707 }
708 if (png_ptr->pass == 4)
709 {
710 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
711 {
712 png_push_have_row(png_ptr, NULL);
713 png_read_push_finish_row(png_ptr);
714 }
715 }
716 break;
717 }
718 case 4:
719 {
720 int i;
721 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
722 {
723 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
724 png_read_push_finish_row(png_ptr);
725 }
726 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
727 {
728 png_push_have_row(png_ptr, NULL);
729 png_read_push_finish_row(png_ptr);
730 }
731 break;
732 }
733 case 5:
734 {
735 int i;
736 for (i = 0; i < 2 && png_ptr->pass == 5; i++)
737 {
738 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
739 png_read_push_finish_row(png_ptr);
740 }
741 if (png_ptr->pass == 6)
742 {
743 png_push_have_row(png_ptr, NULL);
744 png_read_push_finish_row(png_ptr);
745 }
746 break;
747 }
748 case 6:
749 {
750 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
751 png_read_push_finish_row(png_ptr);
752 if (png_ptr->pass != 6)
753 break;
754 png_push_have_row(png_ptr, NULL);
755 png_read_push_finish_row(png_ptr);
756 }
757 }
758 }
759 else
760#endif
761 {
762 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
763 png_read_push_finish_row(png_ptr);
764 }
765}
766
767void
768png_read_push_finish_row(png_structp png_ptr)
769{
770 png_ptr->row_number++;
771 if (png_ptr->row_number < png_ptr->num_rows)
772 return;
773
774 if (png_ptr->interlaced)
775 {
776 png_ptr->row_number = 0;
777 png_memset_check(png_ptr, png_ptr->prev_row, 0,
778 png_ptr->rowbytes + 1);
779 do
780 {
781 png_ptr->pass++;
782 if (png_ptr->pass >= 7)
783 break;
a626cc03 784
c801d85f
KB
785 png_ptr->iwidth = (png_ptr->width +
786 png_pass_inc[png_ptr->pass] - 1 -
787 png_pass_start[png_ptr->pass]) /
788 png_pass_inc[png_ptr->pass];
789
790 png_ptr->irowbytes = ((png_ptr->iwidth *
791 png_ptr->pixel_depth + 7) >> 3) + 1;
792
c801d85f
KB
793 if (png_ptr->transformations & PNG_INTERLACE)
794 break;
a626cc03
RR
795
796 png_ptr->num_rows = (png_ptr->height +
797 png_pass_yinc[png_ptr->pass] - 1 -
798 png_pass_ystart[png_ptr->pass]) /
799 png_pass_yinc[png_ptr->pass];
800
801 } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0);
c801d85f
KB
802 }
803}
804
805#if defined(PNG_READ_tEXt_SUPPORTED)
806void
807png_push_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
808{
809 if (png_ptr->mode == PNG_BEFORE_IHDR || png_ptr->mode & PNG_HAVE_IEND)
810 {
811 png_error(png_ptr, "Out of place tEXt");
812 /* to quiet some compiler warnings */
813 if(info_ptr == NULL) return;
814 }
815
816#ifdef PNG_MAX_MALLOC_64K
817 png_ptr->skip_length = 0; /* This may not be necessary */
818
819 if (length > (png_uint_32)65535L) /* Can't hold the entire string in memory */
820 {
821 png_warning(png_ptr, "tEXt chunk too large to fit in memory");
822 png_ptr->skip_length = length - (png_uint_32)65535L;
823 length = (png_uint_32)65535L;
824 }
825#endif
826
a626cc03 827 png_ptr->current_text = (png_charp)png_malloc(png_ptr,
c801d85f
KB
828 (png_uint_32)(length+1));
829 png_ptr->current_text[length] = '\0';
830 png_ptr->current_text_ptr = png_ptr->current_text;
831 png_ptr->current_text_size = (png_size_t)length;
832 png_ptr->current_text_left = (png_size_t)length;
833 png_ptr->process_mode = PNG_READ_tEXt_MODE;
834}
835
836void
837png_push_read_tEXt(png_structp png_ptr, png_infop info_ptr)
838{
839 if (png_ptr->buffer_size && png_ptr->current_text_left)
840 {
841 png_size_t text_size;
842
843 if (png_ptr->buffer_size < png_ptr->current_text_left)
844 text_size = png_ptr->buffer_size;
845 else
846 text_size = png_ptr->current_text_left;
847 png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
848 png_ptr->current_text_left -= text_size;
849 png_ptr->current_text_ptr += text_size;
850 }
851 if (!(png_ptr->current_text_left))
852 {
853 png_textp text_ptr;
854 png_charp text;
855 png_charp key;
856
857 if (png_ptr->buffer_size < 4)
858 {
859 png_push_save_buffer(png_ptr);
860 return;
861 }
862
863 png_push_crc_finish(png_ptr);
864
865#if defined(PNG_MAX_MALLOC_64K)
866 if (png_ptr->skip_length)
867 return;
868#endif
869
870 key = png_ptr->current_text;
871 png_ptr->current_text = 0;
872
873 for (text = key; *text; text++)
874 /* empty loop */ ;
875
876 if (text != key + png_ptr->current_text_size)
877 text++;
878
879 text_ptr = (png_textp)png_malloc(png_ptr, (png_uint_32)sizeof(png_text));
880 text_ptr->compression = PNG_TEXT_COMPRESSION_NONE;
881 text_ptr->key = key;
882 text_ptr->text = text;
883
884 png_set_text(png_ptr, info_ptr, text_ptr, 1);
885
886 png_free(png_ptr, text_ptr);
887 }
888}
889#endif
890
891#if defined(PNG_READ_zTXt_SUPPORTED)
892void
893png_push_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
894{
895 if (png_ptr->mode == PNG_BEFORE_IHDR || png_ptr->mode & PNG_HAVE_IEND)
896 {
897 png_error(png_ptr, "Out of place zTXt");
898 /* to quiet some compiler warnings */
899 if(info_ptr == NULL) return;
900 }
901
902#ifdef PNG_MAX_MALLOC_64K
903 /* We can't handle zTXt chunks > 64K, since we don't have enough space
904 * to be able to store the uncompressed data. Actually, the threshold
905 * is probably around 32K, but it isn't as definite as 64K is.
906 */
907 if (length > (png_uint_32)65535L)
908 {
909 png_warning(png_ptr, "zTXt chunk too large to fit in memory");
910 png_push_crc_skip(png_ptr, length);
911 return;
912 }
913#endif
914
915 png_ptr->current_text = (png_charp)png_malloc(png_ptr,
916 (png_uint_32)(length+1));
917 png_ptr->current_text[length] = '\0';
918 png_ptr->current_text_ptr = png_ptr->current_text;
919 png_ptr->current_text_size = (png_size_t)length;
920 png_ptr->current_text_left = (png_size_t)length;
921 png_ptr->process_mode = PNG_READ_zTXt_MODE;
922}
923
924void
925png_push_read_zTXt(png_structp png_ptr, png_infop info_ptr)
926{
927 if (png_ptr->buffer_size && png_ptr->current_text_left)
928 {
929 png_size_t text_size;
930
931 if (png_ptr->buffer_size < (png_uint_32)png_ptr->current_text_left)
932 text_size = png_ptr->buffer_size;
933 else
934 text_size = png_ptr->current_text_left;
935 png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
936 png_ptr->current_text_left -= text_size;
937 png_ptr->current_text_ptr += text_size;
938 }
939 if (!(png_ptr->current_text_left))
940 {
941 png_textp text_ptr;
942 png_charp text;
943 png_charp key;
944 int ret;
945 png_size_t text_size, key_size;
946
947 if (png_ptr->buffer_size < 4)
948 {
949 png_push_save_buffer(png_ptr);
950 return;
951 }
952
953 png_push_crc_finish(png_ptr);
954
955 key = png_ptr->current_text;
956 png_ptr->current_text = 0;
957
958 for (text = key; *text; text++)
959 /* empty loop */ ;
960
961 /* zTXt can't have zero text */
962 if (text == key + png_ptr->current_text_size)
963 {
964 png_free(png_ptr, key);
965 return;
966 }
967
968 text++;
969
970 if (*text != PNG_TEXT_COMPRESSION_zTXt) /* check compression byte */
971 {
972 png_free(png_ptr, key);
973 return;
974 }
975
976 text++;
977
978 png_ptr->zstream.next_in = (png_bytep )text;
979 png_ptr->zstream.avail_in = (uInt)(png_ptr->current_text_size -
980 (text - key));
981 png_ptr->zstream.next_out = png_ptr->zbuf;
982 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
983
984 key_size = text - key;
985 text_size = 0;
986 text = NULL;
987 ret = Z_STREAM_END;
988
989 while (png_ptr->zstream.avail_in)
990 {
991 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
992 if (ret != Z_OK && ret != Z_STREAM_END)
993 {
994 inflateReset(&png_ptr->zstream);
995 png_ptr->zstream.avail_in = 0;
996 png_free(png_ptr, key);
997 png_free(png_ptr, text);
998 return;
999 }
1000 if (!(png_ptr->zstream.avail_out) || ret == Z_STREAM_END)
1001 {
1002 if (text == NULL)
1003 {
1004 text = (png_charp)png_malloc(png_ptr,
1005 (png_uint_32)(png_ptr->zbuf_size - png_ptr->zstream.avail_out +
1006 key_size + 1));
1007 png_memcpy(text + key_size, png_ptr->zbuf,
1008 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
1009 png_memcpy(text, key, key_size);
1010 text_size = key_size + png_ptr->zbuf_size -
1011 png_ptr->zstream.avail_out;
1012 *(text + text_size) = '\0';
1013 }
1014 else
1015 {
1016 png_charp tmp;
1017
1018 tmp = text;
1019 text = (png_charp)png_malloc(png_ptr, text_size +
1020 (png_uint_32)(png_ptr->zbuf_size - png_ptr->zstream.avail_out
1021 + 1));
1022 png_memcpy(text, tmp, text_size);
1023 png_free(png_ptr, tmp);
1024 png_memcpy(text + text_size, png_ptr->zbuf,
1025 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
1026 text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out;
1027 *(text + text_size) = '\0';
1028 }
1029 if (ret != Z_STREAM_END)
1030 {
1031 png_ptr->zstream.next_out = png_ptr->zbuf;
1032 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1033 }
1034 }
1035 else
1036 {
1037 break;
1038 }
1039
1040 if (ret == Z_STREAM_END)
1041 break;
1042 }
1043
1044 inflateReset(&png_ptr->zstream);
1045 png_ptr->zstream.avail_in = 0;
1046
1047 if (ret != Z_STREAM_END)
1048 {
1049 png_free(png_ptr, key);
1050 png_free(png_ptr, text);
1051 return;
1052 }
1053
1054 png_free(png_ptr, key);
1055 key = text;
1056 text += key_size;
1057
1058 text_ptr = (png_textp)png_malloc(png_ptr, (png_uint_32)sizeof(png_text));
1059 text_ptr->compression = PNG_TEXT_COMPRESSION_zTXt;
1060 text_ptr->key = key;
1061 text_ptr->text = text;
1062
1063 png_set_text(png_ptr, info_ptr, text_ptr, 1);
1064
1065 png_free(png_ptr, text_ptr);
1066 }
1067}
1068#endif
1069
1070/* This function is called when we haven't found a handler for this
a626cc03 1071 * chunk. In the future we will have code here that can handle
c801d85f
KB
1072 * user-defined callback functions for unknown chunks before they are
1073 * ignored or cause an error. If there isn't a problem with the
1074 * chunk itself (ie a bad chunk name or a critical chunk), the chunk
1075 * is (currently) silently ignored.
1076 */
1077void
1078png_push_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1079{
1080 png_check_chunk_name(png_ptr, png_ptr->chunk_name);
1081
1082 if (!(png_ptr->chunk_name[0] & 0x20))
1083 {
1084 png_chunk_error(png_ptr, "unknown critical chunk");
1085 /* to quiet some compiler warnings */
1086 if(info_ptr == NULL) return;
1087 }
1088
1089 png_push_crc_skip(png_ptr, length);
1090}
1091
1092void
1093png_push_have_info(png_structp png_ptr, png_infop info_ptr)
1094{
1095 if (png_ptr->info_fn != NULL)
1096 (*(png_ptr->info_fn))(png_ptr, info_ptr);
1097}
1098
1099void
1100png_push_have_end(png_structp png_ptr, png_infop info_ptr)
1101{
1102 if (png_ptr->end_fn != NULL)
1103 (*(png_ptr->end_fn))(png_ptr, info_ptr);
1104}
1105
1106void
1107png_push_have_row(png_structp png_ptr, png_bytep row)
1108{
1109 if (png_ptr->row_fn != NULL)
1110 (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number,
1111 (int)png_ptr->pass);
1112}
1113
1114void
1115png_progressive_combine_row (png_structp png_ptr,
1116 png_bytep old_row, png_bytep new_row)
1117{
a626cc03 1118 if (new_row != NULL) /* new_row must == png_ptr->row_buf here. */
c801d85f
KB
1119 png_combine_row(png_ptr, old_row, png_pass_dsp_mask[png_ptr->pass]);
1120}
1121
1122void
1123png_set_progressive_read_fn(png_structp png_ptr, png_voidp progressive_ptr,
1124 png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
1125 png_progressive_end_ptr end_fn)
1126{
1127 png_ptr->info_fn = info_fn;
1128 png_ptr->row_fn = row_fn;
1129 png_ptr->end_fn = end_fn;
1130
1131 png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer);
1132}
1133
1134png_voidp
1135png_get_progressive_ptr(png_structp png_ptr)
1136{
1137 return png_ptr->io_ptr;
1138}
1139
1140#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
1141