Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(788)

Side by Side Diff: third_party/libpng/pngpread.c

Issue 2021403002: Update libpng to 1.6.22 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rearrange pnglibconf.h Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/libpng/pngmem.c ('k') | third_party/libpng/pngprefix.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* pngpread.c - read a png file in push mode 2 /* pngpread.c - read a png file in push mode
3 * 3 *
4 * Last changed in libpng 1.2.44 [June 26, 2010] 4 * Last changed in libpng 1.6.18 [July 23, 2015]
5 * Copyright (c) 1998-2002,2004,2006-2010 Glenn Randers-Pehrson 5 * Copyright (c) 1998-2002,2004,2006-2015 Glenn Randers-Pehrson
6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) 6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) 7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
8 * 8 *
9 * This code is released under the libpng license. 9 * This code is released under the libpng license.
10 * For conditions of distribution and use, see the disclaimer 10 * For conditions of distribution and use, see the disclaimer
11 * and license in png.h 11 * and license in png.h
12 */ 12 */
13 13
14 #define PNG_INTERNAL 14 #include "pngpriv.h"
15 #define PNG_NO_PEDANTIC_WARNINGS 15
16 #include "png.h"
17 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED 16 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
18 17
19 /* Push model modes */ 18 /* Push model modes */
20 #define PNG_READ_SIG_MODE 0 19 #define PNG_READ_SIG_MODE 0
21 #define PNG_READ_CHUNK_MODE 1 20 #define PNG_READ_CHUNK_MODE 1
22 #define PNG_READ_IDAT_MODE 2 21 #define PNG_READ_IDAT_MODE 2
23 #define PNG_SKIP_MODE 3
24 #define PNG_READ_tEXt_MODE 4 22 #define PNG_READ_tEXt_MODE 4
25 #define PNG_READ_zTXt_MODE 5 23 #define PNG_READ_zTXt_MODE 5
26 #define PNG_READ_DONE_MODE 6 24 #define PNG_READ_DONE_MODE 6
27 #define PNG_READ_iTXt_MODE 7 25 #define PNG_READ_iTXt_MODE 7
28 #define PNG_ERROR_MODE 8 26 #define PNG_ERROR_MODE 8
29 27
28 #define PNG_PUSH_SAVE_BUFFER_IF_FULL \
29 if (png_ptr->push_length + 4 > png_ptr->buffer_size) \
30 { png_push_save_buffer(png_ptr); return; }
31 #define PNG_PUSH_SAVE_BUFFER_IF_LT(N) \
32 if (png_ptr->buffer_size < N) \
33 { png_push_save_buffer(png_ptr); return; }
34
30 void PNGAPI 35 void PNGAPI
31 png_process_data(png_structp png_ptr, png_infop info_ptr, 36 png_process_data(png_structrp png_ptr, png_inforp info_ptr,
32 png_bytep buffer, png_size_t buffer_size) 37 png_bytep buffer, png_size_t buffer_size)
33 { 38 {
34 if (png_ptr == NULL || info_ptr == NULL) 39 if (png_ptr == NULL || info_ptr == NULL)
35 return; 40 return;
36 41
37 png_push_restore_buffer(png_ptr, buffer, buffer_size); 42 png_push_restore_buffer(png_ptr, buffer, buffer_size);
38 43
39 while (png_ptr->buffer_size) 44 while (png_ptr->buffer_size)
40 { 45 {
41 png_process_some_data(png_ptr, info_ptr); 46 png_process_some_data(png_ptr, info_ptr);
42 } 47 }
43 } 48 }
44 49
50 png_size_t PNGAPI
51 png_process_data_pause(png_structrp png_ptr, int save)
52 {
53 if (png_ptr != NULL)
54 {
55 /* It's easiest for the caller if we do the save; then the caller doesn't
56 * have to supply the same data again:
57 */
58 if (save != 0)
59 png_push_save_buffer(png_ptr);
60 else
61 {
62 /* This includes any pending saved bytes: */
63 png_size_t remaining = png_ptr->buffer_size;
64 png_ptr->buffer_size = 0;
65
66 /* So subtract the saved buffer size, unless all the data
67 * is actually 'saved', in which case we just return 0
68 */
69 if (png_ptr->save_buffer_size < remaining)
70 return remaining - png_ptr->save_buffer_size;
71 }
72 }
73
74 return 0;
75 }
76
77 png_uint_32 PNGAPI
78 png_process_data_skip(png_structrp png_ptr)
79 {
80 /* TODO: Deprecate and remove this API.
81 * Somewhere the implementation of this seems to have been lost,
82 * or abandoned. It was only to support some internal back-door access
83 * to png_struct) in libpng-1.4.x.
84 */
85 png_app_warning(png_ptr,
86 "png_process_data_skip is not implemented in any current version of libpng");
87 return 0;
88 }
89
45 /* What we do with the incoming data depends on what we were previously 90 /* What we do with the incoming data depends on what we were previously
46 * doing before we ran out of data... 91 * doing before we ran out of data...
47 */ 92 */
48 void /* PRIVATE */ 93 void /* PRIVATE */
49 png_process_some_data(png_structp png_ptr, png_infop info_ptr) 94 png_process_some_data(png_structrp png_ptr, png_inforp info_ptr)
50 { 95 {
51 if (png_ptr == NULL) 96 if (png_ptr == NULL)
52 return; 97 return;
53 98
54 switch (png_ptr->process_mode) 99 switch (png_ptr->process_mode)
55 { 100 {
56 case PNG_READ_SIG_MODE: 101 case PNG_READ_SIG_MODE:
57 { 102 {
58 png_push_read_sig(png_ptr, info_ptr); 103 png_push_read_sig(png_ptr, info_ptr);
59 break; 104 break;
60 } 105 }
61 106
62 case PNG_READ_CHUNK_MODE: 107 case PNG_READ_CHUNK_MODE:
63 { 108 {
64 png_push_read_chunk(png_ptr, info_ptr); 109 png_push_read_chunk(png_ptr, info_ptr);
65 break; 110 break;
66 } 111 }
67 112
68 case PNG_READ_IDAT_MODE: 113 case PNG_READ_IDAT_MODE:
69 { 114 {
70 png_push_read_IDAT(png_ptr); 115 png_push_read_IDAT(png_ptr);
71 break; 116 break;
72 } 117 }
73 118
74 case PNG_SKIP_MODE:
75 {
76 png_push_crc_finish(png_ptr);
77 break;
78 }
79
80 default: 119 default:
81 { 120 {
82 png_ptr->buffer_size = 0; 121 png_ptr->buffer_size = 0;
83 break; 122 break;
84 } 123 }
85 } 124 }
86 } 125 }
87 126
88 /* Read any remaining signature bytes from the stream and compare them with 127 /* Read any remaining signature bytes from the stream and compare them with
89 * the correct PNG signature. It is possible that this routine is called 128 * the correct PNG signature. It is possible that this routine is called
90 * with bytes already read from the signature, either because they have been 129 * with bytes already read from the signature, either because they have been
91 * checked by the calling application, or because of multiple calls to this 130 * checked by the calling application, or because of multiple calls to this
92 * routine. 131 * routine.
93 */ 132 */
94 void /* PRIVATE */ 133 void /* PRIVATE */
95 png_push_read_sig(png_structp png_ptr, png_infop info_ptr) 134 png_push_read_sig(png_structrp png_ptr, png_inforp info_ptr)
96 { 135 {
97 png_size_t num_checked = png_ptr->sig_bytes, 136 png_size_t num_checked = png_ptr->sig_bytes, /* SAFE, does not exceed 8 */
98 num_to_check = 8 - num_checked; 137 num_to_check = 8 - num_checked;
99 138
100 if (png_ptr->buffer_size < num_to_check) 139 if (png_ptr->buffer_size < num_to_check)
101 { 140 {
102 num_to_check = png_ptr->buffer_size; 141 num_to_check = png_ptr->buffer_size;
103 } 142 }
104 143
105 png_push_fill_buffer(png_ptr, &(info_ptr->signature[num_checked]), 144 png_push_fill_buffer(png_ptr, &(info_ptr->signature[num_checked]),
106 num_to_check); 145 num_to_check);
107 png_ptr->sig_bytes = (png_byte)(png_ptr->sig_bytes + num_to_check); 146 png_ptr->sig_bytes = (png_byte)(png_ptr->sig_bytes + num_to_check);
108 147
109 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check)) 148 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
110 { 149 {
111 if (num_checked < 4 && 150 if (num_checked < 4 &&
112 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4)) 151 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
113 png_error(png_ptr, "Not a PNG file"); 152 png_error(png_ptr, "Not a PNG file");
153
114 else 154 else
115 png_error(png_ptr, "PNG file corrupted by ASCII conversion"); 155 png_error(png_ptr, "PNG file corrupted by ASCII conversion");
116 } 156 }
117 else 157 else
118 { 158 {
119 if (png_ptr->sig_bytes >= 8) 159 if (png_ptr->sig_bytes >= 8)
120 { 160 {
121 png_ptr->process_mode = PNG_READ_CHUNK_MODE; 161 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
122 } 162 }
123 } 163 }
124 } 164 }
125 165
126 void /* PRIVATE */ 166 void /* PRIVATE */
127 png_push_read_chunk(png_structp png_ptr, png_infop info_ptr) 167 png_push_read_chunk(png_structrp png_ptr, png_inforp info_ptr)
128 { 168 {
129 #ifdef PNG_USE_LOCAL_ARRAYS 169 png_uint_32 chunk_name;
130 PNG_CONST PNG_IHDR; 170 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
131 PNG_CONST PNG_IDAT; 171 int keep; /* unknown handling method */
132 PNG_CONST PNG_IEND;
133 PNG_CONST PNG_PLTE;
134 #ifdef PNG_READ_bKGD_SUPPORTED
135 PNG_CONST PNG_bKGD;
136 #endif 172 #endif
137 #ifdef PNG_READ_cHRM_SUPPORTED
138 PNG_CONST PNG_cHRM;
139 #endif
140 #ifdef PNG_READ_gAMA_SUPPORTED
141 PNG_CONST PNG_gAMA;
142 #endif
143 #ifdef PNG_READ_hIST_SUPPORTED
144 PNG_CONST PNG_hIST;
145 #endif
146 #ifdef PNG_READ_iCCP_SUPPORTED
147 PNG_CONST PNG_iCCP;
148 #endif
149 #ifdef PNG_READ_iTXt_SUPPORTED
150 PNG_CONST PNG_iTXt;
151 #endif
152 #ifdef PNG_READ_oFFs_SUPPORTED
153 PNG_CONST PNG_oFFs;
154 #endif
155 #ifdef PNG_READ_pCAL_SUPPORTED
156 PNG_CONST PNG_pCAL;
157 #endif
158 #ifdef PNG_READ_pHYs_SUPPORTED
159 PNG_CONST PNG_pHYs;
160 #endif
161 #ifdef PNG_READ_sBIT_SUPPORTED
162 PNG_CONST PNG_sBIT;
163 #endif
164 #ifdef PNG_READ_sCAL_SUPPORTED
165 PNG_CONST PNG_sCAL;
166 #endif
167 #ifdef PNG_READ_sRGB_SUPPORTED
168 PNG_CONST PNG_sRGB;
169 #endif
170 #ifdef PNG_READ_sPLT_SUPPORTED
171 PNG_CONST PNG_sPLT;
172 #endif
173 #ifdef PNG_READ_tEXt_SUPPORTED
174 PNG_CONST PNG_tEXt;
175 #endif
176 #ifdef PNG_READ_tIME_SUPPORTED
177 PNG_CONST PNG_tIME;
178 #endif
179 #ifdef PNG_READ_tRNS_SUPPORTED
180 PNG_CONST PNG_tRNS;
181 #endif
182 #ifdef PNG_READ_zTXt_SUPPORTED
183 PNG_CONST PNG_zTXt;
184 #endif
185 #endif /* PNG_USE_LOCAL_ARRAYS */
186 173
187 /* First we make sure we have enough data for the 4 byte chunk name 174 /* First we make sure we have enough data for the 4-byte chunk name
188 * and the 4 byte chunk length before proceeding with decoding the 175 * and the 4-byte chunk length before proceeding with decoding the
189 * chunk data. To fully decode each of these chunks, we also make 176 * chunk data. To fully decode each of these chunks, we also make
190 * sure we have enough data in the buffer for the 4 byte CRC at the 177 * sure we have enough data in the buffer for the 4-byte CRC at the
191 * end of every chunk (except IDAT, which is handled separately). 178 * end of every chunk (except IDAT, which is handled separately).
192 */ 179 */
193 if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER)) 180 if ((png_ptr->mode & PNG_HAVE_CHUNK_HEADER) == 0)
194 { 181 {
195 png_byte chunk_length[4]; 182 png_byte chunk_length[4];
183 png_byte chunk_tag[4];
196 184
197 if (png_ptr->buffer_size < 8) 185 PNG_PUSH_SAVE_BUFFER_IF_LT(8)
198 {
199 png_push_save_buffer(png_ptr);
200 return;
201 }
202
203 png_push_fill_buffer(png_ptr, chunk_length, 4); 186 png_push_fill_buffer(png_ptr, chunk_length, 4);
204 png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length); 187 png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
205 png_reset_crc(png_ptr); 188 png_reset_crc(png_ptr);
206 png_crc_read(png_ptr, png_ptr->chunk_name, 4); 189 png_crc_read(png_ptr, chunk_tag, 4);
190 png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(chunk_tag);
207 png_check_chunk_name(png_ptr, png_ptr->chunk_name); 191 png_check_chunk_name(png_ptr, png_ptr->chunk_name);
208 png_ptr->mode |= PNG_HAVE_CHUNK_HEADER; 192 png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
209 } 193 }
210 194
211 if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) 195 chunk_name = png_ptr->chunk_name;
212 if (png_ptr->mode & PNG_AFTER_IDAT)
213 png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
214 196
215 if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4)) 197 if (chunk_name == png_IDAT)
198 {
199 if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
200 png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
201
202 /* If we reach an IDAT chunk, this means we have read all of the
203 * header chunks, and we can start reading the image (or if this
204 * is called after the image has been read - we have an error).
205 */
206 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
207 png_error(png_ptr, "Missing IHDR before IDAT");
208
209 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
210 (png_ptr->mode & PNG_HAVE_PLTE) == 0)
211 png_error(png_ptr, "Missing PLTE before IDAT");
212
213 png_ptr->process_mode = PNG_READ_IDAT_MODE;
214
215 if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
216 if ((png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) == 0)
217 if (png_ptr->push_length == 0)
218 return;
219
220 png_ptr->mode |= PNG_HAVE_IDAT;
221
222 if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
223 png_benign_error(png_ptr, "Too many IDATs found");
224 }
225
226 if (chunk_name == png_IHDR)
216 { 227 {
217 if (png_ptr->push_length != 13) 228 if (png_ptr->push_length != 13)
218 png_error(png_ptr, "Invalid IHDR length"); 229 png_error(png_ptr, "Invalid IHDR length");
219 230
220 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 231 PNG_PUSH_SAVE_BUFFER_IF_FULL
221 {
222 png_push_save_buffer(png_ptr);
223 return;
224 }
225
226 png_handle_IHDR(png_ptr, info_ptr, png_ptr->push_length); 232 png_handle_IHDR(png_ptr, info_ptr, png_ptr->push_length);
227 } 233 }
228 234
229 else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4)) 235 else if (chunk_name == png_IEND)
230 { 236 {
231 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 237 PNG_PUSH_SAVE_BUFFER_IF_FULL
232 {
233 png_push_save_buffer(png_ptr);
234 return;
235 }
236
237 png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length); 238 png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length);
238 239
239 png_ptr->process_mode = PNG_READ_DONE_MODE; 240 png_ptr->process_mode = PNG_READ_DONE_MODE;
240 png_push_have_end(png_ptr, info_ptr); 241 png_push_have_end(png_ptr, info_ptr);
241 } 242 }
242 243
243 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED 244 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
244 else if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name)) 245 else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
245 { 246 {
246 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 247 PNG_PUSH_SAVE_BUFFER_IF_FULL
247 { 248 png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length, keep);
248 png_push_save_buffer(png_ptr);
249 return;
250 }
251 249
252 if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) 250 if (chunk_name == png_PLTE)
253 png_ptr->mode |= PNG_HAVE_IDAT; 251 png_ptr->mode |= PNG_HAVE_PLTE;
252 }
253 #endif
254 254
255 png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length); 255 else if (chunk_name == png_PLTE)
256
257 if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
258 png_ptr->mode |= PNG_HAVE_PLTE;
259
260 else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
261 {
262 if (!(png_ptr->mode & PNG_HAVE_IHDR))
263 png_error(png_ptr, "Missing IHDR before IDAT");
264
265 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
266 !(png_ptr->mode & PNG_HAVE_PLTE))
267 png_error(png_ptr, "Missing PLTE before IDAT");
268 }
269 }
270
271 #endif
272 else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
273 { 256 {
274 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 257 PNG_PUSH_SAVE_BUFFER_IF_FULL
275 {
276 png_push_save_buffer(png_ptr);
277 return;
278 }
279 png_handle_PLTE(png_ptr, info_ptr, png_ptr->push_length); 258 png_handle_PLTE(png_ptr, info_ptr, png_ptr->push_length);
280 } 259 }
281 260
282 else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) 261 else if (chunk_name == png_IDAT)
283 { 262 {
284 /* If we reach an IDAT chunk, this means we have read all of the
285 * header chunks, and we can start reading the image (or if this
286 * is called after the image has been read - we have an error).
287 */
288
289 if (!(png_ptr->mode & PNG_HAVE_IHDR))
290 png_error(png_ptr, "Missing IHDR before IDAT");
291
292 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
293 !(png_ptr->mode & PNG_HAVE_PLTE))
294 png_error(png_ptr, "Missing PLTE before IDAT");
295
296 if (png_ptr->mode & PNG_HAVE_IDAT)
297 {
298 if (!(png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
299 if (png_ptr->push_length == 0)
300 return;
301
302 if (png_ptr->mode & PNG_AFTER_IDAT)
303 png_error(png_ptr, "Too many IDAT's found");
304 }
305
306 png_ptr->idat_size = png_ptr->push_length; 263 png_ptr->idat_size = png_ptr->push_length;
307 png_ptr->mode |= PNG_HAVE_IDAT;
308 png_ptr->process_mode = PNG_READ_IDAT_MODE; 264 png_ptr->process_mode = PNG_READ_IDAT_MODE;
309 png_push_have_info(png_ptr, info_ptr); 265 png_push_have_info(png_ptr, info_ptr);
310 png_ptr->zstream.avail_out = 266 png_ptr->zstream.avail_out =
311 (uInt) PNG_ROWBYTES(png_ptr->pixel_depth, 267 (uInt) PNG_ROWBYTES(png_ptr->pixel_depth,
312 png_ptr->iwidth) + 1; 268 png_ptr->iwidth) + 1;
313 png_ptr->zstream.next_out = png_ptr->row_buf; 269 png_ptr->zstream.next_out = png_ptr->row_buf;
314 return; 270 return;
315 } 271 }
316 272
317 #ifdef PNG_READ_gAMA_SUPPORTED 273 #ifdef PNG_READ_gAMA_SUPPORTED
318 else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4)) 274 else if (png_ptr->chunk_name == png_gAMA)
319 { 275 {
320 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 276 PNG_PUSH_SAVE_BUFFER_IF_FULL
321 {
322 png_push_save_buffer(png_ptr);
323 return;
324 }
325
326 png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length); 277 png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length);
327 } 278 }
328 279
329 #endif 280 #endif
330 #ifdef PNG_READ_sBIT_SUPPORTED 281 #ifdef PNG_READ_sBIT_SUPPORTED
331 else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4)) 282 else if (png_ptr->chunk_name == png_sBIT)
332 { 283 {
333 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 284 PNG_PUSH_SAVE_BUFFER_IF_FULL
334 {
335 png_push_save_buffer(png_ptr);
336 return;
337 }
338
339 png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length); 285 png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length);
340 } 286 }
341 287
342 #endif 288 #endif
343 #ifdef PNG_READ_cHRM_SUPPORTED 289 #ifdef PNG_READ_cHRM_SUPPORTED
344 else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4)) 290 else if (png_ptr->chunk_name == png_cHRM)
345 { 291 {
346 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 292 PNG_PUSH_SAVE_BUFFER_IF_FULL
347 {
348 png_push_save_buffer(png_ptr);
349 return;
350 }
351
352 png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length); 293 png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length);
353 } 294 }
354 295
355 #endif 296 #endif
356 #ifdef PNG_READ_sRGB_SUPPORTED 297 #ifdef PNG_READ_sRGB_SUPPORTED
357 else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4)) 298 else if (chunk_name == png_sRGB)
358 { 299 {
359 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 300 PNG_PUSH_SAVE_BUFFER_IF_FULL
360 {
361 png_push_save_buffer(png_ptr);
362 return;
363 }
364
365 png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length); 301 png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length);
366 } 302 }
367 303
368 #endif 304 #endif
369 #ifdef PNG_READ_iCCP_SUPPORTED 305 #ifdef PNG_READ_iCCP_SUPPORTED
370 else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4)) 306 else if (png_ptr->chunk_name == png_iCCP)
371 { 307 {
372 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 308 PNG_PUSH_SAVE_BUFFER_IF_FULL
373 {
374 png_push_save_buffer(png_ptr);
375 return;
376 }
377
378 png_handle_iCCP(png_ptr, info_ptr, png_ptr->push_length); 309 png_handle_iCCP(png_ptr, info_ptr, png_ptr->push_length);
379 } 310 }
380 311
381 #endif 312 #endif
382 #ifdef PNG_READ_sPLT_SUPPORTED 313 #ifdef PNG_READ_sPLT_SUPPORTED
383 else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4)) 314 else if (chunk_name == png_sPLT)
384 { 315 {
385 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 316 PNG_PUSH_SAVE_BUFFER_IF_FULL
386 {
387 png_push_save_buffer(png_ptr);
388 return;
389 }
390
391 png_handle_sPLT(png_ptr, info_ptr, png_ptr->push_length); 317 png_handle_sPLT(png_ptr, info_ptr, png_ptr->push_length);
392 } 318 }
393 319
394 #endif 320 #endif
395 #ifdef PNG_READ_tRNS_SUPPORTED 321 #ifdef PNG_READ_tRNS_SUPPORTED
396 else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4)) 322 else if (chunk_name == png_tRNS)
397 { 323 {
398 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 324 PNG_PUSH_SAVE_BUFFER_IF_FULL
399 {
400 png_push_save_buffer(png_ptr);
401 return;
402 }
403
404 png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length); 325 png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length);
405 } 326 }
406 327
407 #endif 328 #endif
408 #ifdef PNG_READ_bKGD_SUPPORTED 329 #ifdef PNG_READ_bKGD_SUPPORTED
409 else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4)) 330 else if (chunk_name == png_bKGD)
410 { 331 {
411 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 332 PNG_PUSH_SAVE_BUFFER_IF_FULL
412 {
413 png_push_save_buffer(png_ptr);
414 return;
415 }
416
417 png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length); 333 png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length);
418 } 334 }
419 335
420 #endif 336 #endif
421 #ifdef PNG_READ_hIST_SUPPORTED 337 #ifdef PNG_READ_hIST_SUPPORTED
422 else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4)) 338 else if (chunk_name == png_hIST)
423 { 339 {
424 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 340 PNG_PUSH_SAVE_BUFFER_IF_FULL
425 {
426 png_push_save_buffer(png_ptr);
427 return;
428 }
429
430 png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length); 341 png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length);
431 } 342 }
432 343
433 #endif 344 #endif
434 #ifdef PNG_READ_pHYs_SUPPORTED 345 #ifdef PNG_READ_pHYs_SUPPORTED
435 else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4)) 346 else if (chunk_name == png_pHYs)
436 { 347 {
437 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 348 PNG_PUSH_SAVE_BUFFER_IF_FULL
438 {
439 png_push_save_buffer(png_ptr);
440 return;
441 }
442
443 png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length); 349 png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length);
444 } 350 }
445 351
446 #endif 352 #endif
447 #ifdef PNG_READ_oFFs_SUPPORTED 353 #ifdef PNG_READ_oFFs_SUPPORTED
448 else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4)) 354 else if (chunk_name == png_oFFs)
449 { 355 {
450 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 356 PNG_PUSH_SAVE_BUFFER_IF_FULL
451 {
452 png_push_save_buffer(png_ptr);
453 return;
454 }
455
456 png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length); 357 png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length);
457 } 358 }
458 #endif 359 #endif
459 360
460 #ifdef PNG_READ_pCAL_SUPPORTED 361 #ifdef PNG_READ_pCAL_SUPPORTED
461 else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4)) 362 else if (chunk_name == png_pCAL)
462 { 363 {
463 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 364 PNG_PUSH_SAVE_BUFFER_IF_FULL
464 {
465 png_push_save_buffer(png_ptr);
466 return;
467 }
468
469 png_handle_pCAL(png_ptr, info_ptr, png_ptr->push_length); 365 png_handle_pCAL(png_ptr, info_ptr, png_ptr->push_length);
470 } 366 }
471 367
472 #endif 368 #endif
473 #ifdef PNG_READ_sCAL_SUPPORTED 369 #ifdef PNG_READ_sCAL_SUPPORTED
474 else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4)) 370 else if (chunk_name == png_sCAL)
475 { 371 {
476 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 372 PNG_PUSH_SAVE_BUFFER_IF_FULL
477 {
478 png_push_save_buffer(png_ptr);
479 return;
480 }
481
482 png_handle_sCAL(png_ptr, info_ptr, png_ptr->push_length); 373 png_handle_sCAL(png_ptr, info_ptr, png_ptr->push_length);
483 } 374 }
484 375
485 #endif 376 #endif
486 #ifdef PNG_READ_tIME_SUPPORTED 377 #ifdef PNG_READ_tIME_SUPPORTED
487 else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4)) 378 else if (chunk_name == png_tIME)
488 { 379 {
489 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 380 PNG_PUSH_SAVE_BUFFER_IF_FULL
490 {
491 png_push_save_buffer(png_ptr);
492 return;
493 }
494
495 png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length); 381 png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length);
496 } 382 }
497 383
498 #endif 384 #endif
499 #ifdef PNG_READ_tEXt_SUPPORTED 385 #ifdef PNG_READ_tEXt_SUPPORTED
500 else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4)) 386 else if (chunk_name == png_tEXt)
501 { 387 {
502 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 388 PNG_PUSH_SAVE_BUFFER_IF_FULL
503 {
504 png_push_save_buffer(png_ptr);
505 return;
506 }
507
508 png_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length); 389 png_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length);
509 } 390 }
510 391
511 #endif 392 #endif
512 #ifdef PNG_READ_zTXt_SUPPORTED 393 #ifdef PNG_READ_zTXt_SUPPORTED
513 else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4)) 394 else if (chunk_name == png_zTXt)
514 { 395 {
515 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 396 PNG_PUSH_SAVE_BUFFER_IF_FULL
516 {
517 png_push_save_buffer(png_ptr);
518 return;
519 }
520
521 png_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length); 397 png_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length);
522 } 398 }
523 399
524 #endif 400 #endif
525 #ifdef PNG_READ_iTXt_SUPPORTED 401 #ifdef PNG_READ_iTXt_SUPPORTED
526 else if (!png_memcmp(png_ptr->chunk_name, png_iTXt, 4)) 402 else if (chunk_name == png_iTXt)
527 { 403 {
528 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 404 PNG_PUSH_SAVE_BUFFER_IF_FULL
529 {
530 png_push_save_buffer(png_ptr);
531 return;
532 }
533
534 png_handle_iTXt(png_ptr, info_ptr, png_ptr->push_length); 405 png_handle_iTXt(png_ptr, info_ptr, png_ptr->push_length);
535 } 406 }
407 #endif
536 408
537 #endif
538 else 409 else
539 { 410 {
540 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 411 PNG_PUSH_SAVE_BUFFER_IF_FULL
541 { 412 png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length,
542 png_push_save_buffer(png_ptr); 413 PNG_HANDLE_CHUNK_AS_DEFAULT);
543 return;
544 }
545 png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length);
546 } 414 }
547 415
548 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER; 416 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
549 } 417 }
550 418
551 void /* PRIVATE */ 419 void PNGCBAPI
552 png_push_crc_skip(png_structp png_ptr, png_uint_32 skip)
553 {
554 png_ptr->process_mode = PNG_SKIP_MODE;
555 png_ptr->skip_length = skip;
556 }
557
558 void /* PRIVATE */
559 png_push_crc_finish(png_structp png_ptr)
560 {
561 if (png_ptr->skip_length && png_ptr->save_buffer_size)
562 {
563 png_size_t save_size;
564
565 if (png_ptr->skip_length < (png_uint_32)png_ptr->save_buffer_size)
566 save_size = (png_size_t)png_ptr->skip_length;
567 else
568 save_size = png_ptr->save_buffer_size;
569
570 png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
571
572 png_ptr->skip_length -= save_size;
573 png_ptr->buffer_size -= save_size;
574 png_ptr->save_buffer_size -= save_size;
575 png_ptr->save_buffer_ptr += save_size;
576 }
577 if (png_ptr->skip_length && png_ptr->current_buffer_size)
578 {
579 png_size_t save_size;
580
581 if (png_ptr->skip_length < (png_uint_32)png_ptr->current_buffer_size)
582 save_size = (png_size_t)png_ptr->skip_length;
583 else
584 save_size = png_ptr->current_buffer_size;
585
586 png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
587
588 png_ptr->skip_length -= save_size;
589 png_ptr->buffer_size -= save_size;
590 png_ptr->current_buffer_size -= save_size;
591 png_ptr->current_buffer_ptr += save_size;
592 }
593 if (!png_ptr->skip_length)
594 {
595 if (png_ptr->buffer_size < 4)
596 {
597 png_push_save_buffer(png_ptr);
598 return;
599 }
600
601 png_crc_finish(png_ptr, 0);
602 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
603 }
604 }
605
606 void PNGAPI
607 png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length) 420 png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length)
608 { 421 {
609 png_bytep ptr; 422 png_bytep ptr;
610 423
611 if (png_ptr == NULL) 424 if (png_ptr == NULL)
612 return; 425 return;
613 426
614 ptr = buffer; 427 ptr = buffer;
615 if (png_ptr->save_buffer_size) 428 if (png_ptr->save_buffer_size != 0)
616 { 429 {
617 png_size_t save_size; 430 png_size_t save_size;
618 431
619 if (length < png_ptr->save_buffer_size) 432 if (length < png_ptr->save_buffer_size)
620 save_size = length; 433 save_size = length;
434
621 else 435 else
622 save_size = png_ptr->save_buffer_size; 436 save_size = png_ptr->save_buffer_size;
623 437
624 png_memcpy(ptr, png_ptr->save_buffer_ptr, save_size); 438 memcpy(ptr, png_ptr->save_buffer_ptr, save_size);
625 length -= save_size; 439 length -= save_size;
626 ptr += save_size; 440 ptr += save_size;
627 png_ptr->buffer_size -= save_size; 441 png_ptr->buffer_size -= save_size;
628 png_ptr->save_buffer_size -= save_size; 442 png_ptr->save_buffer_size -= save_size;
629 png_ptr->save_buffer_ptr += save_size; 443 png_ptr->save_buffer_ptr += save_size;
630 } 444 }
631 if (length && png_ptr->current_buffer_size) 445 if (length != 0 && png_ptr->current_buffer_size != 0)
632 { 446 {
633 png_size_t save_size; 447 png_size_t save_size;
634 448
635 if (length < png_ptr->current_buffer_size) 449 if (length < png_ptr->current_buffer_size)
636 save_size = length; 450 save_size = length;
637 451
638 else 452 else
639 save_size = png_ptr->current_buffer_size; 453 save_size = png_ptr->current_buffer_size;
640 454
641 png_memcpy(ptr, png_ptr->current_buffer_ptr, save_size); 455 memcpy(ptr, png_ptr->current_buffer_ptr, save_size);
642 png_ptr->buffer_size -= save_size; 456 png_ptr->buffer_size -= save_size;
643 png_ptr->current_buffer_size -= save_size; 457 png_ptr->current_buffer_size -= save_size;
644 png_ptr->current_buffer_ptr += save_size; 458 png_ptr->current_buffer_ptr += save_size;
645 } 459 }
646 } 460 }
647 461
648 void /* PRIVATE */ 462 void /* PRIVATE */
649 png_push_save_buffer(png_structp png_ptr) 463 png_push_save_buffer(png_structrp png_ptr)
650 { 464 {
651 if (png_ptr->save_buffer_size) 465 if (png_ptr->save_buffer_size != 0)
652 { 466 {
653 if (png_ptr->save_buffer_ptr != png_ptr->save_buffer) 467 if (png_ptr->save_buffer_ptr != png_ptr->save_buffer)
654 { 468 {
655 png_size_t i, istop; 469 png_size_t i, istop;
656 png_bytep sp; 470 png_bytep sp;
657 png_bytep dp; 471 png_bytep dp;
658 472
659 istop = png_ptr->save_buffer_size; 473 istop = png_ptr->save_buffer_size;
660 for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer; 474 for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer;
661 i < istop; i++, sp++, dp++) 475 i < istop; i++, sp++, dp++)
662 { 476 {
663 *dp = *sp; 477 *dp = *sp;
664 } 478 }
665 } 479 }
666 } 480 }
667 if (png_ptr->save_buffer_size + png_ptr->current_buffer_size > 481 if (png_ptr->save_buffer_size + png_ptr->current_buffer_size >
668 png_ptr->save_buffer_max) 482 png_ptr->save_buffer_max)
669 { 483 {
670 png_size_t new_max; 484 png_size_t new_max;
671 png_bytep old_buffer; 485 png_bytep old_buffer;
672 486
673 if (png_ptr->save_buffer_size > PNG_SIZE_MAX - 487 if (png_ptr->save_buffer_size > PNG_SIZE_MAX -
674 (png_ptr->current_buffer_size + 256)) 488 (png_ptr->current_buffer_size + 256))
675 { 489 {
676 png_error(png_ptr, "Potential overflow of save_buffer"); 490 png_error(png_ptr, "Potential overflow of save_buffer");
677 } 491 }
678 492
679 new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256; 493 new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;
680 old_buffer = png_ptr->save_buffer; 494 old_buffer = png_ptr->save_buffer;
681 png_ptr->save_buffer = (png_bytep)png_malloc_warn(png_ptr, 495 png_ptr->save_buffer = (png_bytep)png_malloc_warn(png_ptr,
682 (png_uint_32)new_max); 496 (png_size_t)new_max);
497
683 if (png_ptr->save_buffer == NULL) 498 if (png_ptr->save_buffer == NULL)
684 { 499 {
685 png_free(png_ptr, old_buffer); 500 png_free(png_ptr, old_buffer);
686 png_error(png_ptr, "Insufficient memory for save_buffer"); 501 png_error(png_ptr, "Insufficient memory for save_buffer");
687 } 502 }
688 else 503
689 { 504 memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
690 png_memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size); 505 png_free(png_ptr, old_buffer);
691 png_free(png_ptr, old_buffer); 506 png_ptr->save_buffer_max = new_max;
692 png_ptr->save_buffer_max = new_max;
693 }
694 } 507 }
695 if (png_ptr->current_buffer_size) 508 if (png_ptr->current_buffer_size)
696 { 509 {
697 png_memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size, 510 memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
698 png_ptr->current_buffer_ptr, png_ptr->current_buffer_size); 511 png_ptr->current_buffer_ptr, png_ptr->current_buffer_size);
699 png_ptr->save_buffer_size += png_ptr->current_buffer_size; 512 png_ptr->save_buffer_size += png_ptr->current_buffer_size;
700 png_ptr->current_buffer_size = 0; 513 png_ptr->current_buffer_size = 0;
701 } 514 }
702 png_ptr->save_buffer_ptr = png_ptr->save_buffer; 515 png_ptr->save_buffer_ptr = png_ptr->save_buffer;
703 png_ptr->buffer_size = 0; 516 png_ptr->buffer_size = 0;
704 } 517 }
705 518
706 void /* PRIVATE */ 519 void /* PRIVATE */
707 png_push_restore_buffer(png_structp png_ptr, png_bytep buffer, 520 png_push_restore_buffer(png_structrp png_ptr, png_bytep buffer,
708 png_size_t buffer_length) 521 png_size_t buffer_length)
709 { 522 {
710 png_ptr->current_buffer = buffer; 523 png_ptr->current_buffer = buffer;
711 png_ptr->current_buffer_size = buffer_length; 524 png_ptr->current_buffer_size = buffer_length;
712 png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size; 525 png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size;
713 png_ptr->current_buffer_ptr = png_ptr->current_buffer; 526 png_ptr->current_buffer_ptr = png_ptr->current_buffer;
714 } 527 }
715 528
716 void /* PRIVATE */ 529 void /* PRIVATE */
717 png_push_read_IDAT(png_structp png_ptr) 530 png_push_read_IDAT(png_structrp png_ptr)
718 { 531 {
719 #ifdef PNG_USE_LOCAL_ARRAYS 532 if ((png_ptr->mode & PNG_HAVE_CHUNK_HEADER) == 0)
720 PNG_CONST PNG_IDAT;
721 #endif
722 if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
723 { 533 {
724 png_byte chunk_length[4]; 534 png_byte chunk_length[4];
535 png_byte chunk_tag[4];
725 536
726 if (png_ptr->buffer_size < 8) 537 /* TODO: this code can be commoned up with the same code in push_read */
727 { 538 PNG_PUSH_SAVE_BUFFER_IF_LT(8)
728 png_push_save_buffer(png_ptr);
729 return;
730 }
731
732 png_push_fill_buffer(png_ptr, chunk_length, 4); 539 png_push_fill_buffer(png_ptr, chunk_length, 4);
733 png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length); 540 png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
734 png_reset_crc(png_ptr); 541 png_reset_crc(png_ptr);
735 png_crc_read(png_ptr, png_ptr->chunk_name, 4); 542 png_crc_read(png_ptr, chunk_tag, 4);
543 png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(chunk_tag);
736 png_ptr->mode |= PNG_HAVE_CHUNK_HEADER; 544 png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
737 545
738 if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) 546 if (png_ptr->chunk_name != png_IDAT)
739 { 547 {
740 png_ptr->process_mode = PNG_READ_CHUNK_MODE; 548 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
741 if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED)) 549
550 if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
742 png_error(png_ptr, "Not enough compressed data"); 551 png_error(png_ptr, "Not enough compressed data");
552
743 return; 553 return;
744 } 554 }
745 555
746 png_ptr->idat_size = png_ptr->push_length; 556 png_ptr->idat_size = png_ptr->push_length;
747 } 557 }
748 if (png_ptr->idat_size && png_ptr->save_buffer_size) 558
559 if (png_ptr->idat_size != 0 && png_ptr->save_buffer_size != 0)
749 { 560 {
750 png_size_t save_size; 561 png_size_t save_size = png_ptr->save_buffer_size;
562 png_uint_32 idat_size = png_ptr->idat_size;
751 563
752 if (png_ptr->idat_size < (png_uint_32)png_ptr->save_buffer_size) 564 /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
753 { 565 * are of different types and we don't know which variable has the fewest
754 save_size = (png_size_t)png_ptr->idat_size; 566 * bits. Carefully select the smaller and cast it to the type of the
567 * larger - this cannot overflow. Do not cast in the following test - it
568 * will break on either 16-bit or 64-bit platforms.
569 */
570 if (idat_size < save_size)
571 save_size = (png_size_t)idat_size;
755 572
756 /* Check for overflow */
757 if ((png_uint_32)save_size != png_ptr->idat_size)
758 png_error(png_ptr, "save_size overflowed in pngpread");
759 }
760 else 573 else
761 save_size = png_ptr->save_buffer_size; 574 idat_size = (png_uint_32)save_size;
762 575
763 png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size); 576 png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
764 577
765 png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size); 578 png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size);
766 579
767 png_ptr->idat_size -= save_size; 580 png_ptr->idat_size -= idat_size;
768 png_ptr->buffer_size -= save_size; 581 png_ptr->buffer_size -= save_size;
769 png_ptr->save_buffer_size -= save_size; 582 png_ptr->save_buffer_size -= save_size;
770 png_ptr->save_buffer_ptr += save_size; 583 png_ptr->save_buffer_ptr += save_size;
771 } 584 }
772 if (png_ptr->idat_size && png_ptr->current_buffer_size) 585
586 if (png_ptr->idat_size != 0 && png_ptr->current_buffer_size != 0)
773 { 587 {
774 png_size_t save_size; 588 png_size_t save_size = png_ptr->current_buffer_size;
589 png_uint_32 idat_size = png_ptr->idat_size;
775 590
776 if (png_ptr->idat_size < (png_uint_32)png_ptr->current_buffer_size) 591 /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
777 { 592 * are of different types and we don't know which variable has the fewest
778 save_size = (png_size_t)png_ptr->idat_size; 593 * bits. Carefully select the smaller and cast it to the type of the
594 * larger - this cannot overflow.
595 */
596 if (idat_size < save_size)
597 save_size = (png_size_t)idat_size;
779 598
780 /* Check for overflow */
781 if ((png_uint_32)save_size != png_ptr->idat_size)
782 png_error(png_ptr, "save_size overflowed in pngpread");
783 }
784 else 599 else
785 save_size = png_ptr->current_buffer_size; 600 idat_size = (png_uint_32)save_size;
786 601
787 png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size); 602 png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
788 603
789 png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size); 604 png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size);
790 605
791 png_ptr->idat_size -= save_size; 606 png_ptr->idat_size -= idat_size;
792 png_ptr->buffer_size -= save_size; 607 png_ptr->buffer_size -= save_size;
793 png_ptr->current_buffer_size -= save_size; 608 png_ptr->current_buffer_size -= save_size;
794 png_ptr->current_buffer_ptr += save_size; 609 png_ptr->current_buffer_ptr += save_size;
795 } 610 }
796 if (!png_ptr->idat_size) 611
612 if (png_ptr->idat_size == 0)
797 { 613 {
798 if (png_ptr->buffer_size < 4) 614 PNG_PUSH_SAVE_BUFFER_IF_LT(4)
799 {
800 png_push_save_buffer(png_ptr);
801 return;
802 }
803
804 png_crc_finish(png_ptr, 0); 615 png_crc_finish(png_ptr, 0);
805 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER; 616 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
806 png_ptr->mode |= PNG_AFTER_IDAT; 617 png_ptr->mode |= PNG_AFTER_IDAT;
618 png_ptr->zowner = 0;
807 } 619 }
808 } 620 }
809 621
810 void /* PRIVATE */ 622 void /* PRIVATE */
811 png_process_IDAT_data(png_structp png_ptr, png_bytep buffer, 623 png_process_IDAT_data(png_structrp png_ptr, png_bytep buffer,
812 png_size_t buffer_length) 624 png_size_t buffer_length)
813 { 625 {
814 /* The caller checks for a non-zero buffer length. */ 626 /* The caller checks for a non-zero buffer length. */
815 if (!(buffer_length > 0) || buffer == NULL) 627 if (!(buffer_length > 0) || buffer == NULL)
816 png_error(png_ptr, "No IDAT data (internal error)"); 628 png_error(png_ptr, "No IDAT data (internal error)");
817 629
818 /* This routine must process all the data it has been given 630 /* This routine must process all the data it has been given
819 * before returning, calling the row callback as required to 631 * before returning, calling the row callback as required to
820 * handle the uncompressed results. 632 * handle the uncompressed results.
821 */ 633 */
822 png_ptr->zstream.next_in = buffer; 634 png_ptr->zstream.next_in = buffer;
635 /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
823 png_ptr->zstream.avail_in = (uInt)buffer_length; 636 png_ptr->zstream.avail_in = (uInt)buffer_length;
824 637
825 /* Keep going until the decompressed data is all processed 638 /* Keep going until the decompressed data is all processed
826 * or the stream marked as finished. 639 * or the stream marked as finished.
827 */ 640 */
828 while (png_ptr->zstream.avail_in > 0 && 641 while (png_ptr->zstream.avail_in > 0 &&
829 » !(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED)) 642 (png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
830 { 643 {
831 int ret; 644 int ret;
832 645
833 /* We have data for zlib, but we must check that zlib 646 /* We have data for zlib, but we must check that zlib
834 * has somewhere to put the results. It doesn't matter 647 * has someplace to put the results. It doesn't matter
835 * if we don't expect any results -- it may be the input 648 * if we don't expect any results -- it may be the input
836 * data is just the LZ end code. 649 * data is just the LZ end code.
837 */ 650 */
838 if (!(png_ptr->zstream.avail_out > 0)) 651 if (!(png_ptr->zstream.avail_out > 0))
839 { 652 {
840 png_ptr->zstream.avail_out = 653 /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
841 (uInt) PNG_ROWBYTES(png_ptr->pixel_depth, 654 png_ptr->zstream.avail_out = (uInt)(PNG_ROWBYTES(png_ptr->pixel_depth,
842 png_ptr->iwidth) + 1; 655 png_ptr->iwidth) + 1);
656
843 png_ptr->zstream.next_out = png_ptr->row_buf; 657 png_ptr->zstream.next_out = png_ptr->row_buf;
844 } 658 }
845 659
846 /* Using Z_SYNC_FLUSH here means that an unterminated 660 /* Using Z_SYNC_FLUSH here means that an unterminated
847 * LZ stream can still be handled (a stream with a missing 661 * LZ stream (a stream with a missing end code) can still
848 * end code), otherwise (Z_NO_FLUSH) a future zlib 662 * be handled, otherwise (Z_NO_FLUSH) a future zlib
849 * implementation might defer output and, therefore, 663 * implementation might defer output and therefore
850 * change the current behavior. (See comments in inflate.c 664 * change the current behavior (see comments in inflate.c
851 * for why this doesn't happen at present with zlib 1.2.5.) 665 * for why this doesn't happen at present with zlib 1.2.5).
852 */ 666 */
853 ret = inflate(&png_ptr->zstream, Z_SYNC_FLUSH); 667 ret = PNG_INFLATE(png_ptr, Z_SYNC_FLUSH);
854 668
855 /* Check for any failure before proceeding. */ 669 /* Check for any failure before proceeding. */
856 if (ret != Z_OK && ret != Z_STREAM_END) 670 if (ret != Z_OK && ret != Z_STREAM_END)
857 { 671 {
858 » /* Terminate the decompression. */ 672 /* Terminate the decompression. */
859 » png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; 673 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
674 png_ptr->zowner = 0;
860 675
861 /* This may be a truncated stream (missing or 676 /* This may be a truncated stream (missing or
862 » * damaged end code). Treat that as a warning. 677 * damaged end code). Treat that as a warning.
863 » */ 678 */
864 if (png_ptr->row_number >= png_ptr->num_rows || 679 if (png_ptr->row_number >= png_ptr->num_rows ||
865 » png_ptr->pass > 6) 680 png_ptr->pass > 6)
866 » png_warning(png_ptr, "Truncated compressed data in IDAT"); 681 png_warning(png_ptr, "Truncated compressed data in IDAT");
867 » else
868 » png_error(png_ptr, "Decompression error in IDAT");
869 682
870 » /* Skip the check on unprocessed input */ 683 else
684 png_error(png_ptr, "Decompression error in IDAT");
685
686 /* Skip the check on unprocessed input */
871 return; 687 return;
872 } 688 }
873 689
874 /* Did inflate output any data? */ 690 /* Did inflate output any data? */
875 if (png_ptr->zstream.next_out != png_ptr->row_buf) 691 if (png_ptr->zstream.next_out != png_ptr->row_buf)
876 { 692 {
877 » /* Is this unexpected data after the last row? 693 /* Is this unexpected data after the last row?
878 » * If it is, artificially terminate the LZ output 694 * If it is, artificially terminate the LZ output
879 » * here. 695 * here.
880 » */ 696 */
881 if (png_ptr->row_number >= png_ptr->num_rows || 697 if (png_ptr->row_number >= png_ptr->num_rows ||
882 » png_ptr->pass > 6) 698 png_ptr->pass > 6)
883 { 699 {
884 » /* Extra data. */ 700 /* Extra data. */
885 » png_warning(png_ptr, "Extra compressed data in IDAT"); 701 png_warning(png_ptr, "Extra compressed data in IDAT");
886 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; 702 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
887 » /* Do no more processing; skip the unprocessed 703 png_ptr->zowner = 0;
888 » * input check below. 704
889 » */ 705 /* Do no more processing; skip the unprocessed
706 * input check below.
707 */
890 return; 708 return;
891 » } 709 }
892 710
893 » /* Do we have a complete row? */ 711 /* Do we have a complete row? */
894 » if (png_ptr->zstream.avail_out == 0) 712 if (png_ptr->zstream.avail_out == 0)
895 » png_push_process_row(png_ptr); 713 png_push_process_row(png_ptr);
896 } 714 }
897 715
898 /* And check for the end of the stream. */ 716 /* And check for the end of the stream. */
899 if (ret == Z_STREAM_END) 717 if (ret == Z_STREAM_END)
900 » png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; 718 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
901 } 719 }
902 720
903 /* All the data should have been processed, if anything 721 /* All the data should have been processed, if anything
904 * is left at this point we have bytes of IDAT data 722 * is left at this point we have bytes of IDAT data
905 * after the zlib end code. 723 * after the zlib end code.
906 */ 724 */
907 if (png_ptr->zstream.avail_in > 0) 725 if (png_ptr->zstream.avail_in > 0)
908 png_warning(png_ptr, "Extra compression data"); 726 png_warning(png_ptr, "Extra compression data in IDAT");
909 } 727 }
910 728
911 void /* PRIVATE */ 729 void /* PRIVATE */
912 png_push_process_row(png_structp png_ptr) 730 png_push_process_row(png_structrp png_ptr)
913 { 731 {
914 png_ptr->row_info.color_type = png_ptr->color_type; 732 /* 1.5.6: row_info moved out of png_struct to a local here. */
915 png_ptr->row_info.width = png_ptr->iwidth; 733 png_row_info row_info;
916 png_ptr->row_info.channels = png_ptr->channels;
917 png_ptr->row_info.bit_depth = png_ptr->bit_depth;
918 png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
919 734
920 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth, 735 row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */
921 png_ptr->row_info.width); 736 row_info.color_type = png_ptr->color_type;
737 row_info.bit_depth = png_ptr->bit_depth;
738 row_info.channels = png_ptr->channels;
739 row_info.pixel_depth = png_ptr->pixel_depth;
740 row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
922 741
923 png_read_filter_row(png_ptr, &(png_ptr->row_info), 742 if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
924 png_ptr->row_buf + 1, png_ptr->prev_row + 1, 743 {
925 (int)(png_ptr->row_buf[0])); 744 if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
745 png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1,
746 png_ptr->prev_row + 1, png_ptr->row_buf[0]);
747 else
748 png_error(png_ptr, "bad adaptive filter value");
749 }
926 750
927 png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf, 751 /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
928 png_ptr->rowbytes + 1); 752 * 1.5.6, while the buffer really is this big in current versions of libpng
753 * it may not be in the future, so this was changed just to copy the
754 * interlaced row count:
755 */
756 memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
929 757
930 if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA)) 758 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
931 png_do_read_transformations(png_ptr); 759 if (png_ptr->transformations != 0)
760 png_do_read_transformations(png_ptr, &row_info);
761 #endif
762
763 /* The transformed pixel depth should match the depth now in row_info. */
764 if (png_ptr->transformed_pixel_depth == 0)
765 {
766 png_ptr->transformed_pixel_depth = row_info.pixel_depth;
767 if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
768 png_error(png_ptr, "progressive row overflow");
769 }
770
771 else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
772 png_error(png_ptr, "internal progressive row size calculation error");
773
932 774
933 #ifdef PNG_READ_INTERLACING_SUPPORTED 775 #ifdef PNG_READ_INTERLACING_SUPPORTED
934 /* Blow up interlaced rows to full size */ 776 /* Expand interlaced rows to full size */
935 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE)) 777 if (png_ptr->interlaced != 0 &&
778 (png_ptr->transformations & PNG_INTERLACE) != 0)
936 { 779 {
937 if (png_ptr->pass < 6) 780 if (png_ptr->pass < 6)
938 /* old interface (pre-1.0.9): 781 png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
939 png_do_read_interlace(&(png_ptr->row_info), 782 png_ptr->transformations);
940 png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
941 */
942 png_do_read_interlace(png_ptr);
943 783
944 switch (png_ptr->pass) 784 switch (png_ptr->pass)
945 { 785 {
946 case 0: 786 case 0:
947 { 787 {
948 int i; 788 int i;
949 for (i = 0; i < 8 && png_ptr->pass == 0; i++) 789 for (i = 0; i < 8 && png_ptr->pass == 0; i++)
950 { 790 {
951 png_push_have_row(png_ptr, png_ptr->row_buf + 1); 791 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
952 png_read_push_finish_row(png_ptr); /* Updates png_ptr->pass */ 792 png_read_push_finish_row(png_ptr); /* Updates png_ptr->pass */
953 } 793 }
954 794
955 if (png_ptr->pass == 2) /* Pass 1 might be empty */ 795 if (png_ptr->pass == 2) /* Pass 1 might be empty */
956 { 796 {
957 for (i = 0; i < 4 && png_ptr->pass == 2; i++) 797 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
958 { 798 {
959 png_push_have_row(png_ptr, png_bytep_NULL); 799 png_push_have_row(png_ptr, NULL);
960 png_read_push_finish_row(png_ptr); 800 png_read_push_finish_row(png_ptr);
961 } 801 }
962 } 802 }
963 803
964 if (png_ptr->pass == 4 && png_ptr->height <= 4) 804 if (png_ptr->pass == 4 && png_ptr->height <= 4)
965 { 805 {
966 for (i = 0; i < 2 && png_ptr->pass == 4; i++) 806 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
967 { 807 {
968 png_push_have_row(png_ptr, png_bytep_NULL); 808 png_push_have_row(png_ptr, NULL);
969 png_read_push_finish_row(png_ptr); 809 png_read_push_finish_row(png_ptr);
970 } 810 }
971 } 811 }
972 812
973 if (png_ptr->pass == 6 && png_ptr->height <= 4) 813 if (png_ptr->pass == 6 && png_ptr->height <= 4)
974 { 814 {
975 png_push_have_row(png_ptr, png_bytep_NULL); 815 png_push_have_row(png_ptr, NULL);
976 png_read_push_finish_row(png_ptr); 816 png_read_push_finish_row(png_ptr);
977 } 817 }
978 818
979 break; 819 break;
980 } 820 }
981 821
982 case 1: 822 case 1:
983 { 823 {
984 int i; 824 int i;
985 for (i = 0; i < 8 && png_ptr->pass == 1; i++) 825 for (i = 0; i < 8 && png_ptr->pass == 1; i++)
986 { 826 {
987 png_push_have_row(png_ptr, png_ptr->row_buf + 1); 827 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
988 png_read_push_finish_row(png_ptr); 828 png_read_push_finish_row(png_ptr);
989 } 829 }
990 830
991 if (png_ptr->pass == 2) /* Skip top 4 generated rows */ 831 if (png_ptr->pass == 2) /* Skip top 4 generated rows */
992 { 832 {
993 for (i = 0; i < 4 && png_ptr->pass == 2; i++) 833 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
994 { 834 {
995 png_push_have_row(png_ptr, png_bytep_NULL); 835 png_push_have_row(png_ptr, NULL);
996 png_read_push_finish_row(png_ptr); 836 png_read_push_finish_row(png_ptr);
997 } 837 }
998 } 838 }
999 839
1000 break; 840 break;
1001 } 841 }
1002 842
1003 case 2: 843 case 2:
1004 { 844 {
1005 int i; 845 int i;
1006 846
1007 for (i = 0; i < 4 && png_ptr->pass == 2; i++) 847 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
1008 { 848 {
1009 png_push_have_row(png_ptr, png_ptr->row_buf + 1); 849 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1010 png_read_push_finish_row(png_ptr); 850 png_read_push_finish_row(png_ptr);
1011 } 851 }
1012 852
1013 for (i = 0; i < 4 && png_ptr->pass == 2; i++) 853 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
1014 { 854 {
1015 png_push_have_row(png_ptr, png_bytep_NULL); 855 png_push_have_row(png_ptr, NULL);
1016 png_read_push_finish_row(png_ptr); 856 png_read_push_finish_row(png_ptr);
1017 } 857 }
1018 858
1019 if (png_ptr->pass == 4) /* Pass 3 might be empty */ 859 if (png_ptr->pass == 4) /* Pass 3 might be empty */
1020 { 860 {
1021 for (i = 0; i < 2 && png_ptr->pass == 4; i++) 861 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1022 { 862 {
1023 png_push_have_row(png_ptr, png_bytep_NULL); 863 png_push_have_row(png_ptr, NULL);
1024 png_read_push_finish_row(png_ptr); 864 png_read_push_finish_row(png_ptr);
1025 } 865 }
1026 } 866 }
1027 867
1028 break; 868 break;
1029 } 869 }
1030 870
1031 case 3: 871 case 3:
1032 { 872 {
1033 int i; 873 int i;
1034 874
1035 for (i = 0; i < 4 && png_ptr->pass == 3; i++) 875 for (i = 0; i < 4 && png_ptr->pass == 3; i++)
1036 { 876 {
1037 png_push_have_row(png_ptr, png_ptr->row_buf + 1); 877 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1038 png_read_push_finish_row(png_ptr); 878 png_read_push_finish_row(png_ptr);
1039 } 879 }
1040 880
1041 if (png_ptr->pass == 4) /* Skip top two generated rows */ 881 if (png_ptr->pass == 4) /* Skip top two generated rows */
1042 { 882 {
1043 for (i = 0; i < 2 && png_ptr->pass == 4; i++) 883 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1044 { 884 {
1045 png_push_have_row(png_ptr, png_bytep_NULL); 885 png_push_have_row(png_ptr, NULL);
1046 png_read_push_finish_row(png_ptr); 886 png_read_push_finish_row(png_ptr);
1047 } 887 }
1048 } 888 }
1049 889
1050 break; 890 break;
1051 } 891 }
1052 892
1053 case 4: 893 case 4:
1054 { 894 {
1055 int i; 895 int i;
1056 896
1057 for (i = 0; i < 2 && png_ptr->pass == 4; i++) 897 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1058 { 898 {
1059 png_push_have_row(png_ptr, png_ptr->row_buf + 1); 899 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1060 png_read_push_finish_row(png_ptr); 900 png_read_push_finish_row(png_ptr);
1061 } 901 }
1062 902
1063 for (i = 0; i < 2 && png_ptr->pass == 4; i++) 903 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1064 { 904 {
1065 png_push_have_row(png_ptr, png_bytep_NULL); 905 png_push_have_row(png_ptr, NULL);
1066 png_read_push_finish_row(png_ptr); 906 png_read_push_finish_row(png_ptr);
1067 } 907 }
1068 908
1069 if (png_ptr->pass == 6) /* Pass 5 might be empty */ 909 if (png_ptr->pass == 6) /* Pass 5 might be empty */
1070 { 910 {
1071 png_push_have_row(png_ptr, png_bytep_NULL); 911 png_push_have_row(png_ptr, NULL);
1072 png_read_push_finish_row(png_ptr); 912 png_read_push_finish_row(png_ptr);
1073 } 913 }
1074 914
1075 break; 915 break;
1076 } 916 }
1077 917
1078 case 5: 918 case 5:
1079 { 919 {
1080 int i; 920 int i;
1081 921
1082 for (i = 0; i < 2 && png_ptr->pass == 5; i++) 922 for (i = 0; i < 2 && png_ptr->pass == 5; i++)
1083 { 923 {
1084 png_push_have_row(png_ptr, png_ptr->row_buf + 1); 924 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1085 png_read_push_finish_row(png_ptr); 925 png_read_push_finish_row(png_ptr);
1086 } 926 }
1087 927
1088 if (png_ptr->pass == 6) /* Skip top generated row */ 928 if (png_ptr->pass == 6) /* Skip top generated row */
1089 { 929 {
1090 png_push_have_row(png_ptr, png_bytep_NULL); 930 png_push_have_row(png_ptr, NULL);
1091 png_read_push_finish_row(png_ptr); 931 png_read_push_finish_row(png_ptr);
1092 } 932 }
1093 933
1094 break; 934 break;
1095 } 935 }
936
937 default:
1096 case 6: 938 case 6:
1097 { 939 {
1098 png_push_have_row(png_ptr, png_ptr->row_buf + 1); 940 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1099 png_read_push_finish_row(png_ptr); 941 png_read_push_finish_row(png_ptr);
1100 942
1101 if (png_ptr->pass != 6) 943 if (png_ptr->pass != 6)
1102 break; 944 break;
1103 945
1104 png_push_have_row(png_ptr, png_bytep_NULL); 946 png_push_have_row(png_ptr, NULL);
1105 png_read_push_finish_row(png_ptr); 947 png_read_push_finish_row(png_ptr);
1106 } 948 }
1107 } 949 }
1108 } 950 }
1109 else 951 else
1110 #endif 952 #endif
1111 { 953 {
1112 png_push_have_row(png_ptr, png_ptr->row_buf + 1); 954 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1113 png_read_push_finish_row(png_ptr); 955 png_read_push_finish_row(png_ptr);
1114 } 956 }
1115 } 957 }
1116 958
1117 void /* PRIVATE */ 959 void /* PRIVATE */
1118 png_read_push_finish_row(png_structp png_ptr) 960 png_read_push_finish_row(png_structrp png_ptr)
1119 { 961 {
1120 #ifdef PNG_USE_LOCAL_ARRAYS 962 #ifdef PNG_READ_INTERLACING_SUPPORTED
1121 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ 963 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1122 964
1123 /* Start of interlace block */ 965 /* Start of interlace block */
1124 PNG_CONST int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0}; 966 static PNG_CONST png_byte png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
1125 967
1126 /* Offset to next interlace block */ 968 /* Offset to next interlace block */
1127 PNG_CONST int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1}; 969 static PNG_CONST png_byte png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
1128 970
1129 /* Start of interlace block in the y direction */ 971 /* Start of interlace block in the y direction */
1130 PNG_CONST int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1}; 972 static PNG_CONST png_byte png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
1131 973
1132 /* Offset to next interlace block in the y direction */ 974 /* Offset to next interlace block in the y direction */
1133 PNG_CONST int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2}; 975 static PNG_CONST png_byte png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
1134 976
1135 /* Height of interlace block. This is not currently used - if you need 977 /* Height of interlace block. This is not currently used - if you need
1136 * it, uncomment it here and in png.h 978 * it, uncomment it here and in png.h
1137 PNG_CONST int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1}; 979 static PNG_CONST png_byte png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
1138 */ 980 */
1139 #endif 981 #endif
1140 982
1141 png_ptr->row_number++; 983 png_ptr->row_number++;
1142 if (png_ptr->row_number < png_ptr->num_rows) 984 if (png_ptr->row_number < png_ptr->num_rows)
1143 return; 985 return;
1144 986
1145 #ifdef PNG_READ_INTERLACING_SUPPORTED 987 #ifdef PNG_READ_INTERLACING_SUPPORTED
1146 if (png_ptr->interlaced) 988 if (png_ptr->interlaced != 0)
1147 { 989 {
1148 png_ptr->row_number = 0; 990 png_ptr->row_number = 0;
1149 png_memset_check(png_ptr, png_ptr->prev_row, 0, 991 memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
1150 png_ptr->rowbytes + 1); 992
1151 do 993 do
1152 { 994 {
1153 png_ptr->pass++; 995 png_ptr->pass++;
1154 if ((png_ptr->pass == 1 && png_ptr->width < 5) || 996 if ((png_ptr->pass == 1 && png_ptr->width < 5) ||
1155 (png_ptr->pass == 3 && png_ptr->width < 3) || 997 (png_ptr->pass == 3 && png_ptr->width < 3) ||
1156 (png_ptr->pass == 5 && png_ptr->width < 2)) 998 (png_ptr->pass == 5 && png_ptr->width < 2))
1157 png_ptr->pass++; 999 png_ptr->pass++;
1158 1000
1159 if (png_ptr->pass > 7) 1001 if (png_ptr->pass > 7)
1160 png_ptr->pass--; 1002 png_ptr->pass--;
1161 1003
1162 if (png_ptr->pass >= 7) 1004 if (png_ptr->pass >= 7)
1163 break; 1005 break;
1164 1006
1165 png_ptr->iwidth = (png_ptr->width + 1007 png_ptr->iwidth = (png_ptr->width +
1166 png_pass_inc[png_ptr->pass] - 1 - 1008 png_pass_inc[png_ptr->pass] - 1 -
1167 png_pass_start[png_ptr->pass]) / 1009 png_pass_start[png_ptr->pass]) /
1168 png_pass_inc[png_ptr->pass]; 1010 png_pass_inc[png_ptr->pass];
1169 1011
1170 if (png_ptr->transformations & PNG_INTERLACE) 1012 if ((png_ptr->transformations & PNG_INTERLACE) != 0)
1171 break; 1013 break;
1172 1014
1173 png_ptr->num_rows = (png_ptr->height + 1015 png_ptr->num_rows = (png_ptr->height +
1174 png_pass_yinc[png_ptr->pass] - 1 - 1016 png_pass_yinc[png_ptr->pass] - 1 -
1175 png_pass_ystart[png_ptr->pass]) / 1017 png_pass_ystart[png_ptr->pass]) /
1176 png_pass_yinc[png_ptr->pass]; 1018 png_pass_yinc[png_ptr->pass];
1177 1019
1178 } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0); 1020 } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0);
1179 } 1021 }
1180 #endif /* PNG_READ_INTERLACING_SUPPORTED */ 1022 #endif /* READ_INTERLACING */
1181 } 1023 }
1182 1024
1183 void /* PRIVATE */ 1025 void /* PRIVATE */
1184 png_push_have_info(png_structp png_ptr, png_infop info_ptr) 1026 png_push_have_info(png_structrp png_ptr, png_inforp info_ptr)
1185 { 1027 {
1186 if (png_ptr->info_fn != NULL) 1028 if (png_ptr->info_fn != NULL)
1187 (*(png_ptr->info_fn))(png_ptr, info_ptr); 1029 (*(png_ptr->info_fn))(png_ptr, info_ptr);
1188 } 1030 }
1189 1031
1190 void /* PRIVATE */ 1032 void /* PRIVATE */
1191 png_push_have_end(png_structp png_ptr, png_infop info_ptr) 1033 png_push_have_end(png_structrp png_ptr, png_inforp info_ptr)
1192 { 1034 {
1193 if (png_ptr->end_fn != NULL) 1035 if (png_ptr->end_fn != NULL)
1194 (*(png_ptr->end_fn))(png_ptr, info_ptr); 1036 (*(png_ptr->end_fn))(png_ptr, info_ptr);
1195 } 1037 }
1196 1038
1197 void /* PRIVATE */ 1039 void /* PRIVATE */
1198 png_push_have_row(png_structp png_ptr, png_bytep row) 1040 png_push_have_row(png_structrp png_ptr, png_bytep row)
1199 { 1041 {
1200 if (png_ptr->row_fn != NULL) 1042 if (png_ptr->row_fn != NULL)
1201 (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number, 1043 (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number,
1202 (int)png_ptr->pass); 1044 (int)png_ptr->pass);
1203 } 1045 }
1204 1046
1047 #ifdef PNG_READ_INTERLACING_SUPPORTED
1205 void PNGAPI 1048 void PNGAPI
1206 png_progressive_combine_row (png_structp png_ptr, 1049 png_progressive_combine_row(png_const_structrp png_ptr, png_bytep old_row,
1207 png_bytep old_row, png_bytep new_row) 1050 png_const_bytep new_row)
1208 { 1051 {
1209 #ifdef PNG_USE_LOCAL_ARRAYS
1210 PNG_CONST int FARDATA png_pass_dsp_mask[7] =
1211 {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
1212 #endif
1213
1214 if (png_ptr == NULL) 1052 if (png_ptr == NULL)
1215 return; 1053 return;
1216 1054
1217 if (new_row != NULL) /* new_row must == png_ptr->row_buf here. */ 1055 /* new_row is a flag here - if it is NULL then the app callback was called
1218 png_combine_row(png_ptr, old_row, png_pass_dsp_mask[png_ptr->pass]); 1056 * from an empty row (see the calls to png_struct::row_fn below), otherwise
1057 * it must be png_ptr->row_buf+1
1058 */
1059 if (new_row != NULL)
1060 png_combine_row(png_ptr, old_row, 1/*blocky display*/);
1219 } 1061 }
1062 #endif /* READ_INTERLACING */
1220 1063
1221 void PNGAPI 1064 void PNGAPI
1222 png_set_progressive_read_fn(png_structp png_ptr, png_voidp progressive_ptr, 1065 png_set_progressive_read_fn(png_structrp png_ptr, png_voidp progressive_ptr,
1223 png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn, 1066 png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
1224 png_progressive_end_ptr end_fn) 1067 png_progressive_end_ptr end_fn)
1225 { 1068 {
1226 if (png_ptr == NULL) 1069 if (png_ptr == NULL)
1227 return; 1070 return;
1228 1071
1229 png_ptr->info_fn = info_fn; 1072 png_ptr->info_fn = info_fn;
1230 png_ptr->row_fn = row_fn; 1073 png_ptr->row_fn = row_fn;
1231 png_ptr->end_fn = end_fn; 1074 png_ptr->end_fn = end_fn;
1232 1075
1233 png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer); 1076 png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer);
1234 } 1077 }
1235 1078
1236 png_voidp PNGAPI 1079 png_voidp PNGAPI
1237 png_get_progressive_ptr(png_structp png_ptr) 1080 png_get_progressive_ptr(png_const_structrp png_ptr)
1238 { 1081 {
1239 if (png_ptr == NULL) 1082 if (png_ptr == NULL)
1240 return (NULL); 1083 return (NULL);
1241 1084
1242 return png_ptr->io_ptr; 1085 return png_ptr->io_ptr;
1243 } 1086 }
1244 #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */ 1087 #endif /* PROGRESSIVE_READ */
OLDNEW
« no previous file with comments | « third_party/libpng/pngmem.c ('k') | third_party/libpng/pngprefix.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698