2
2
/* pngpread.c - read a png file in push mode
4
* Last changed in libpng 1.2.32 [September 18, 2008]
5
* For conditions of distribution and use, see copyright notice in png.h
6
* Copyright (c) 1998-2008 Glenn Randers-Pehrson
4
* Last changed in libpng 1.2.44 [June 26, 2010]
5
* Copyright (c) 1998-2010 Glenn Randers-Pehrson
7
6
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8
7
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
9
* This code is released under the libpng license.
10
* For conditions of distribution and use, see the disclaimer
11
* and license in png.h
11
14
#define PNG_INTERNAL
15
#define PNG_NO_PEDANTIC_WARNINGS
13
17
#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
15
/* push model modes */
19
/* Push model modes */
16
20
#define PNG_READ_SIG_MODE 0
17
21
#define PNG_READ_CHUNK_MODE 1
18
22
#define PNG_READ_IDAT_MODE 2
50
58
png_push_read_sig(png_ptr, info_ptr);
53
62
case PNG_READ_CHUNK_MODE:
55
64
png_push_read_chunk(png_ptr, info_ptr);
58
68
case PNG_READ_IDAT_MODE:
60
70
png_push_read_IDAT(png_ptr);
63
#if defined(PNG_READ_tEXt_SUPPORTED)
74
#ifdef PNG_READ_tEXt_SUPPORTED
64
75
case PNG_READ_tEXt_MODE:
66
77
png_push_read_tEXt(png_ptr, info_ptr);
70
#if defined(PNG_READ_zTXt_SUPPORTED)
82
#ifdef PNG_READ_zTXt_SUPPORTED
71
83
case PNG_READ_zTXt_MODE:
73
85
png_push_read_zTXt(png_ptr, info_ptr);
77
#if defined(PNG_READ_iTXt_SUPPORTED)
90
#ifdef PNG_READ_iTXt_SUPPORTED
78
91
case PNG_READ_iTXt_MODE:
80
93
png_push_read_iTXt(png_ptr, info_ptr);
84
98
case PNG_SKIP_MODE:
86
100
png_push_crc_finish(png_ptr);
91
106
png_ptr->buffer_size = 0;
140
155
PNG_CONST PNG_IDAT;
141
156
PNG_CONST PNG_IEND;
142
157
PNG_CONST PNG_PLTE;
143
#if defined(PNG_READ_bKGD_SUPPORTED)
158
#ifdef PNG_READ_bKGD_SUPPORTED
144
159
PNG_CONST PNG_bKGD;
146
#if defined(PNG_READ_cHRM_SUPPORTED)
161
#ifdef PNG_READ_cHRM_SUPPORTED
147
162
PNG_CONST PNG_cHRM;
149
#if defined(PNG_READ_gAMA_SUPPORTED)
164
#ifdef PNG_READ_gAMA_SUPPORTED
150
165
PNG_CONST PNG_gAMA;
152
#if defined(PNG_READ_hIST_SUPPORTED)
167
#ifdef PNG_READ_hIST_SUPPORTED
153
168
PNG_CONST PNG_hIST;
155
#if defined(PNG_READ_iCCP_SUPPORTED)
170
#ifdef PNG_READ_iCCP_SUPPORTED
156
171
PNG_CONST PNG_iCCP;
158
#if defined(PNG_READ_iTXt_SUPPORTED)
173
#ifdef PNG_READ_iTXt_SUPPORTED
159
174
PNG_CONST PNG_iTXt;
161
#if defined(PNG_READ_oFFs_SUPPORTED)
176
#ifdef PNG_READ_oFFs_SUPPORTED
162
177
PNG_CONST PNG_oFFs;
164
#if defined(PNG_READ_pCAL_SUPPORTED)
179
#ifdef PNG_READ_pCAL_SUPPORTED
165
180
PNG_CONST PNG_pCAL;
167
#if defined(PNG_READ_pHYs_SUPPORTED)
182
#ifdef PNG_READ_pHYs_SUPPORTED
168
183
PNG_CONST PNG_pHYs;
170
#if defined(PNG_READ_sBIT_SUPPORTED)
185
#ifdef PNG_READ_sBIT_SUPPORTED
171
186
PNG_CONST PNG_sBIT;
173
#if defined(PNG_READ_sCAL_SUPPORTED)
188
#ifdef PNG_READ_sCAL_SUPPORTED
174
189
PNG_CONST PNG_sCAL;
176
#if defined(PNG_READ_sRGB_SUPPORTED)
191
#ifdef PNG_READ_sRGB_SUPPORTED
177
192
PNG_CONST PNG_sRGB;
179
#if defined(PNG_READ_sPLT_SUPPORTED)
194
#ifdef PNG_READ_sPLT_SUPPORTED
180
195
PNG_CONST PNG_sPLT;
182
#if defined(PNG_READ_tEXt_SUPPORTED)
197
#ifdef PNG_READ_tEXt_SUPPORTED
183
198
PNG_CONST PNG_tEXt;
185
#if defined(PNG_READ_tIME_SUPPORTED)
200
#ifdef PNG_READ_tIME_SUPPORTED
186
201
PNG_CONST PNG_tIME;
188
#if defined(PNG_READ_tRNS_SUPPORTED)
203
#ifdef PNG_READ_tRNS_SUPPORTED
189
204
PNG_CONST PNG_tRNS;
191
#if defined(PNG_READ_zTXt_SUPPORTED)
206
#ifdef PNG_READ_zTXt_SUPPORTED
192
207
PNG_CONST PNG_zTXt;
194
209
#endif /* PNG_USE_LOCAL_ARRAYS */
195
211
/* First we make sure we have enough data for the 4 byte chunk name
196
212
* and the 4 byte chunk length before proceeding with decoding the
197
213
* chunk data. To fully decode each of these chunks, we also make
251
272
png_push_save_buffer(png_ptr);
254
276
if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
255
277
png_ptr->mode |= PNG_HAVE_IDAT;
256
279
png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length);
257
281
if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
258
282
png_ptr->mode |= PNG_HAVE_PLTE;
259
284
else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
261
286
if (!(png_ptr->mode & PNG_HAVE_IHDR))
262
287
png_error(png_ptr, "Missing IHDR before IDAT");
263
289
else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
264
290
!(png_ptr->mode & PNG_HAVE_PLTE))
265
291
png_error(png_ptr, "Missing PLTE before IDAT");
269
296
else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
276
303
png_handle_PLTE(png_ptr, info_ptr, png_ptr->push_length);
278
306
else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
280
308
/* If we reach an IDAT chunk, this means we have read all of the
281
309
* header chunks, and we can start reading the image (or if this
282
310
* is called after the image has been read - we have an error).
284
if (!(png_ptr->mode & PNG_HAVE_IHDR))
285
png_error(png_ptr, "Missing IHDR before IDAT");
286
else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
287
!(png_ptr->mode & PNG_HAVE_PLTE))
288
png_error(png_ptr, "Missing PLTE before IDAT");
313
if (!(png_ptr->mode & PNG_HAVE_IHDR))
314
png_error(png_ptr, "Missing IHDR before IDAT");
316
else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
317
!(png_ptr->mode & PNG_HAVE_PLTE))
318
png_error(png_ptr, "Missing PLTE before IDAT");
290
320
if (png_ptr->mode & PNG_HAVE_IDAT)
292
322
if (!(png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
293
if (png_ptr->push_length == 0)
323
if (png_ptr->push_length == 0)
296
326
if (png_ptr->mode & PNG_AFTER_IDAT)
297
327
png_error(png_ptr, "Too many IDAT's found");
630
700
png_error(png_ptr, "Potential overflow of save_buffer");
632
703
new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;
633
704
old_buffer = png_ptr->save_buffer;
634
png_ptr->save_buffer = (png_bytep)png_malloc(png_ptr,
705
png_ptr->save_buffer = (png_bytep)png_malloc_warn(png_ptr,
635
706
(png_uint_32)new_max);
707
if (png_ptr->save_buffer == NULL)
709
png_free(png_ptr, old_buffer);
710
png_error(png_ptr, "Insufficient memory for save_buffer");
636
712
png_memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
637
713
png_free(png_ptr, old_buffer);
638
714
png_ptr->save_buffer_max = new_max;
753
832
png_process_IDAT_data(png_structp png_ptr, png_bytep buffer,
754
833
png_size_t buffer_length)
758
if ((png_ptr->flags & PNG_FLAG_ZLIB_FINISHED) && buffer_length)
759
png_error(png_ptr, "Extra compression data");
835
/* The caller checks for a non-zero buffer length. */
836
if (!(buffer_length > 0) || buffer == NULL)
837
png_error(png_ptr, "No IDAT data (internal error)");
839
/* This routine must process all the data it has been given
840
* before returning, calling the row callback as required to
841
* handle the uncompressed results.
761
843
png_ptr->zstream.next_in = buffer;
762
844
png_ptr->zstream.avail_in = (uInt)buffer_length;
846
/* Keep going until the decompressed data is all processed
847
* or the stream marked as finished.
849
while (png_ptr->zstream.avail_in > 0 &&
850
!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
765
ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
768
if (ret == Z_STREAM_END)
854
/* We have data for zlib, but we must check that zlib
855
* has somewhere to put the results. It doesn't matter
856
* if we don't expect any results -- it may be the input
857
* data is just the LZ end code.
859
if (!(png_ptr->zstream.avail_out > 0))
861
png_ptr->zstream.avail_out =
862
(uInt) PNG_ROWBYTES(png_ptr->pixel_depth,
863
png_ptr->iwidth) + 1;
864
png_ptr->zstream.next_out = png_ptr->row_buf;
867
/* Using Z_SYNC_FLUSH here means that an unterminated
868
* LZ stream can still be handled (a stream with a missing
869
* end code), otherwise (Z_NO_FLUSH) a future zlib
870
* implementation might defer output and, therefore,
871
* change the current behavior. (See comments in inflate.c
872
* for why this doesn't happen at present with zlib 1.2.5.)
874
ret = inflate(&png_ptr->zstream, Z_SYNC_FLUSH);
876
/* Check for any failure before proceeding. */
877
if (ret != Z_OK && ret != Z_STREAM_END)
879
/* Terminate the decompression. */
880
png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
882
/* This may be a truncated stream (missing or
883
* damaged end code). Treat that as a warning.
885
if (png_ptr->row_number >= png_ptr->num_rows ||
887
png_warning(png_ptr, "Truncated compressed data in IDAT");
889
png_error(png_ptr, "Decompression error in IDAT");
891
/* Skip the check on unprocessed input */
895
/* Did inflate output any data? */
896
if (png_ptr->zstream.next_out != png_ptr->row_buf)
898
/* Is this unexpected data after the last row?
899
* If it is, artificially terminate the LZ output
902
if (png_ptr->row_number >= png_ptr->num_rows ||
770
if (png_ptr->zstream.avail_in)
771
png_error(png_ptr, "Extra compressed data");
772
if (!(png_ptr->zstream.avail_out))
774
png_push_process_row(png_ptr);
777
png_ptr->mode |= PNG_AFTER_IDAT;
906
png_warning(png_ptr, "Extra compressed data in IDAT");
778
907
png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
781
else if (ret == Z_BUF_ERROR)
784
png_error(png_ptr, "Decompression Error");
786
if (!(png_ptr->zstream.avail_out))
789
#if defined(PNG_READ_INTERLACING_SUPPORTED)
790
png_ptr->interlaced && png_ptr->pass > 6) ||
791
(!png_ptr->interlaced &&
793
png_ptr->row_number == png_ptr->num_rows))
795
if (png_ptr->zstream.avail_in)
796
png_warning(png_ptr, "Too much data in IDAT chunks");
797
png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
800
png_push_process_row(png_ptr);
801
png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
802
png_ptr->zstream.next_out = png_ptr->row_buf;
908
/* Do no more processing; skip the unprocessed
914
/* Do we have a complete row? */
915
if (png_ptr->zstream.avail_out == 0)
916
png_push_process_row(png_ptr);
919
/* And check for the end of the stream. */
920
if (ret == Z_STREAM_END)
921
png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
924
/* All the data should have been processed, if anything
925
* is left at this point we have bytes of IDAT data
926
* after the zlib end code.
928
if (png_ptr->zstream.avail_in > 0)
929
png_warning(png_ptr, "Extra compression data");
809
932
void /* PRIVATE */
828
951
if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA))
829
952
png_do_read_transformations(png_ptr);
831
#if defined(PNG_READ_INTERLACING_SUPPORTED)
832
/* blow up interlaced rows to full size */
954
#ifdef PNG_READ_INTERLACING_SUPPORTED
955
/* Blow up interlaced rows to full size */
833
956
if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
835
958
if (png_ptr->pass < 6)
836
959
/* old interface (pre-1.0.9):
837
960
png_do_read_interlace(&(png_ptr->row_info),
838
png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
961
png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
840
963
png_do_read_interlace(png_ptr);
929
1067
png_read_push_finish_row(png_ptr);
937
1078
for (i = 0; i < 2 && png_ptr->pass == 4; i++)
939
1080
png_push_have_row(png_ptr, png_ptr->row_buf + 1);
940
1081
png_read_push_finish_row(png_ptr);
942
1084
for (i = 0; i < 2 && png_ptr->pass == 4; i++)
944
png_push_have_row(png_ptr, png_bytep_NULL);
1086
png_push_have_row(png_ptr, png_bytep_NULL);
945
1087
png_read_push_finish_row(png_ptr);
947
if (png_ptr->pass == 6) /* pass 5 might be empty */
1090
if (png_ptr->pass == 6) /* Pass 5 might be empty */
949
png_push_have_row(png_ptr, png_bytep_NULL);
1092
png_push_have_row(png_ptr, png_bytep_NULL);
950
1093
png_read_push_finish_row(png_ptr);
957
1103
for (i = 0; i < 2 && png_ptr->pass == 5; i++)
959
1105
png_push_have_row(png_ptr, png_ptr->row_buf + 1);
960
1106
png_read_push_finish_row(png_ptr);
962
if (png_ptr->pass == 6) /* skip top generated row */
1109
if (png_ptr->pass == 6) /* Skip top generated row */
964
png_push_have_row(png_ptr, png_bytep_NULL);
1111
png_push_have_row(png_ptr, png_bytep_NULL);
965
1112
png_read_push_finish_row(png_ptr);
971
1119
png_push_have_row(png_ptr, png_ptr->row_buf + 1);
972
1120
png_read_push_finish_row(png_ptr);
973
1122
if (png_ptr->pass != 6)
975
png_push_have_row(png_ptr, png_bytep_NULL);
1125
png_push_have_row(png_ptr, png_bytep_NULL);
976
1126
png_read_push_finish_row(png_ptr);
989
1139
png_read_push_finish_row(png_structp png_ptr)
991
1141
#ifdef PNG_USE_LOCAL_ARRAYS
992
/* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1142
/* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
994
/* start of interlace block */
1144
/* Start of interlace block */
995
1145
PNG_CONST int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
997
/* offset to next interlace block */
1147
/* Offset to next interlace block */
998
1148
PNG_CONST int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
1000
/* start of interlace block in the y direction */
1150
/* Start of interlace block in the y direction */
1001
1151
PNG_CONST int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
1003
/* offset to next interlace block in the y direction */
1153
/* Offset to next interlace block in the y direction */
1004
1154
PNG_CONST int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
1006
1156
/* Height of interlace block. This is not currently used - if you need
1468
1634
if (!(png_ptr->chunk_name[0] & 0x20))
1470
#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
1636
#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
1471
1637
if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
1472
1638
PNG_HANDLE_CHUNK_ALWAYS
1473
#if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
1639
#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
1474
1640
&& png_ptr->read_user_chunk_fn == NULL
1478
1644
png_chunk_error(png_ptr, "unknown critical chunk");
1480
info_ptr = info_ptr; /* to quiet some compiler warnings */
1646
info_ptr = info_ptr; /* To quiet some compiler warnings */
1483
#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
1649
#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
1484
1650
if (png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS)
1486
1652
#ifdef PNG_MAX_MALLOC_64K
1500
1666
png_ptr->unknown_chunk.size = (png_size_t)length;
1501
1668
if (length == 0)
1502
1669
png_ptr->unknown_chunk.data = NULL;
1505
1673
png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr,
1506
(png_uint_32)length);
1674
(png_uint_32)length);
1507
1675
png_crc_read(png_ptr, (png_bytep)png_ptr->unknown_chunk.data, length);
1509
#if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
1678
#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
1510
1679
if (png_ptr->read_user_chunk_fn != NULL)
1512
/* callback to user unknown chunk handler */
1681
/* Callback to user unknown chunk handler */
1514
1683
ret = (*(png_ptr->read_user_chunk_fn))
1515
1684
(png_ptr, &png_ptr->unknown_chunk);
1517
1687
png_chunk_error(png_ptr, "error in user chunk");
1520
1691
if (!(png_ptr->chunk_name[0] & 0x20))