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

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

Issue 1509773004: CL for perf tryjob on android Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years 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/pngpriv.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-2010 Glenn Randers-Pehrson 5 * Copyright (c) 1998-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->mode |= PNG_HAVE_IDAT;
214 png_ptr->process_mode = PNG_READ_IDAT_MODE;
215
216 if ((png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) == 0)
217 if (png_ptr->push_length == 0)
218 return;
219
220 if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
221 png_benign_error(png_ptr, "Too many IDATs found");
222 }
223
224 if (chunk_name == png_IHDR)
216 { 225 {
217 if (png_ptr->push_length != 13) 226 if (png_ptr->push_length != 13)
218 png_error(png_ptr, "Invalid IHDR length"); 227 png_error(png_ptr, "Invalid IHDR length");
219 228
220 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 229 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); 230 png_handle_IHDR(png_ptr, info_ptr, png_ptr->push_length);
227 } 231 }
228 232
229 else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4)) 233 else if (chunk_name == png_IEND)
230 { 234 {
231 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 235 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); 236 png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length);
238 237
239 png_ptr->process_mode = PNG_READ_DONE_MODE; 238 png_ptr->process_mode = PNG_READ_DONE_MODE;
240 png_push_have_end(png_ptr, info_ptr); 239 png_push_have_end(png_ptr, info_ptr);
241 } 240 }
242 241
243 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED 242 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
244 else if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name)) 243 else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
245 { 244 {
246 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 245 PNG_PUSH_SAVE_BUFFER_IF_FULL
247 { 246 png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length, keep);
248 png_push_save_buffer(png_ptr);
249 return;
250 }
251 247
252 if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) 248 if (chunk_name == png_PLTE)
253 png_ptr->mode |= PNG_HAVE_IDAT; 249 png_ptr->mode |= PNG_HAVE_PLTE;
250 }
251 #endif
254 252
255 png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length); 253 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 { 254 {
274 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 255 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); 256 png_handle_PLTE(png_ptr, info_ptr, png_ptr->push_length);
280 } 257 }
281 258
282 else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) 259 else if (chunk_name == png_IDAT)
283 { 260 {
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; 261 png_ptr->idat_size = png_ptr->push_length;
307 png_ptr->mode |= PNG_HAVE_IDAT;
308 png_ptr->process_mode = PNG_READ_IDAT_MODE; 262 png_ptr->process_mode = PNG_READ_IDAT_MODE;
309 png_push_have_info(png_ptr, info_ptr); 263 png_push_have_info(png_ptr, info_ptr);
310 png_ptr->zstream.avail_out = 264 png_ptr->zstream.avail_out =
311 (uInt) PNG_ROWBYTES(png_ptr->pixel_depth, 265 (uInt) PNG_ROWBYTES(png_ptr->pixel_depth,
312 png_ptr->iwidth) + 1; 266 png_ptr->iwidth) + 1;
313 png_ptr->zstream.next_out = png_ptr->row_buf; 267 png_ptr->zstream.next_out = png_ptr->row_buf;
314 return; 268 return;
315 } 269 }
316 270
317 #ifdef PNG_READ_gAMA_SUPPORTED 271 #ifdef PNG_READ_gAMA_SUPPORTED
318 else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4)) 272 else if (png_ptr->chunk_name == png_gAMA)
319 { 273 {
320 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 274 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); 275 png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length);
327 } 276 }
328 277
329 #endif 278 #endif
330 #ifdef PNG_READ_sBIT_SUPPORTED 279 #ifdef PNG_READ_sBIT_SUPPORTED
331 else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4)) 280 else if (png_ptr->chunk_name == png_sBIT)
332 { 281 {
333 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 282 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); 283 png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length);
340 } 284 }
341 285
342 #endif 286 #endif
343 #ifdef PNG_READ_cHRM_SUPPORTED 287 #ifdef PNG_READ_cHRM_SUPPORTED
344 else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4)) 288 else if (png_ptr->chunk_name == png_cHRM)
345 { 289 {
346 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 290 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); 291 png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length);
353 } 292 }
354 293
355 #endif 294 #endif
356 #ifdef PNG_READ_sRGB_SUPPORTED 295 #ifdef PNG_READ_sRGB_SUPPORTED
357 else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4)) 296 else if (chunk_name == png_sRGB)
358 { 297 {
359 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 298 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); 299 png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length);
366 } 300 }
367 301
368 #endif 302 #endif
369 #ifdef PNG_READ_iCCP_SUPPORTED 303 #ifdef PNG_READ_iCCP_SUPPORTED
370 else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4)) 304 else if (png_ptr->chunk_name == png_iCCP)
371 { 305 {
372 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 306 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); 307 png_handle_iCCP(png_ptr, info_ptr, png_ptr->push_length);
379 } 308 }
380 309
381 #endif 310 #endif
382 #ifdef PNG_READ_sPLT_SUPPORTED 311 #ifdef PNG_READ_sPLT_SUPPORTED
383 else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4)) 312 else if (chunk_name == png_sPLT)
384 { 313 {
385 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 314 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); 315 png_handle_sPLT(png_ptr, info_ptr, png_ptr->push_length);
392 } 316 }
393 317
394 #endif 318 #endif
395 #ifdef PNG_READ_tRNS_SUPPORTED 319 #ifdef PNG_READ_tRNS_SUPPORTED
396 else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4)) 320 else if (chunk_name == png_tRNS)
397 { 321 {
398 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 322 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); 323 png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length);
405 } 324 }
406 325
407 #endif 326 #endif
408 #ifdef PNG_READ_bKGD_SUPPORTED 327 #ifdef PNG_READ_bKGD_SUPPORTED
409 else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4)) 328 else if (chunk_name == png_bKGD)
410 { 329 {
411 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 330 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); 331 png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length);
418 } 332 }
419 333
420 #endif 334 #endif
421 #ifdef PNG_READ_hIST_SUPPORTED 335 #ifdef PNG_READ_hIST_SUPPORTED
422 else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4)) 336 else if (chunk_name == png_hIST)
423 { 337 {
424 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 338 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); 339 png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length);
431 } 340 }
432 341
433 #endif 342 #endif
434 #ifdef PNG_READ_pHYs_SUPPORTED 343 #ifdef PNG_READ_pHYs_SUPPORTED
435 else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4)) 344 else if (chunk_name == png_pHYs)
436 { 345 {
437 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 346 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); 347 png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length);
444 } 348 }
445 349
446 #endif 350 #endif
447 #ifdef PNG_READ_oFFs_SUPPORTED 351 #ifdef PNG_READ_oFFs_SUPPORTED
448 else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4)) 352 else if (chunk_name == png_oFFs)
449 { 353 {
450 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 354 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); 355 png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length);
457 } 356 }
458 #endif 357 #endif
459 358
460 #ifdef PNG_READ_pCAL_SUPPORTED 359 #ifdef PNG_READ_pCAL_SUPPORTED
461 else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4)) 360 else if (chunk_name == png_pCAL)
462 { 361 {
463 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 362 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); 363 png_handle_pCAL(png_ptr, info_ptr, png_ptr->push_length);
470 } 364 }
471 365
472 #endif 366 #endif
473 #ifdef PNG_READ_sCAL_SUPPORTED 367 #ifdef PNG_READ_sCAL_SUPPORTED
474 else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4)) 368 else if (chunk_name == png_sCAL)
475 { 369 {
476 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 370 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); 371 png_handle_sCAL(png_ptr, info_ptr, png_ptr->push_length);
483 } 372 }
484 373
485 #endif 374 #endif
486 #ifdef PNG_READ_tIME_SUPPORTED 375 #ifdef PNG_READ_tIME_SUPPORTED
487 else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4)) 376 else if (chunk_name == png_tIME)
488 { 377 {
489 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 378 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); 379 png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length);
496 } 380 }
497 381
498 #endif 382 #endif
499 #ifdef PNG_READ_tEXt_SUPPORTED 383 #ifdef PNG_READ_tEXt_SUPPORTED
500 else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4)) 384 else if (chunk_name == png_tEXt)
501 { 385 {
502 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 386 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); 387 png_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length);
509 } 388 }
510 389
511 #endif 390 #endif
512 #ifdef PNG_READ_zTXt_SUPPORTED 391 #ifdef PNG_READ_zTXt_SUPPORTED
513 else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4)) 392 else if (chunk_name == png_zTXt)
514 { 393 {
515 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 394 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); 395 png_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length);
522 } 396 }
523 397
524 #endif 398 #endif
525 #ifdef PNG_READ_iTXt_SUPPORTED 399 #ifdef PNG_READ_iTXt_SUPPORTED
526 else if (!png_memcmp(png_ptr->chunk_name, png_iTXt, 4)) 400 else if (chunk_name == png_iTXt)
527 { 401 {
528 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 402 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); 403 png_handle_iTXt(png_ptr, info_ptr, png_ptr->push_length);
535 } 404 }
405 #endif
536 406
537 #endif
538 else 407 else
539 { 408 {
540 if (png_ptr->push_length + 4 > png_ptr->buffer_size) 409 PNG_PUSH_SAVE_BUFFER_IF_FULL
541 { 410 png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length,
542 png_push_save_buffer(png_ptr); 411 PNG_HANDLE_CHUNK_AS_DEFAULT);
543 return;
544 }
545 png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length);
546 } 412 }
547 413
548 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER; 414 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
549 } 415 }
550 416
551 void /* PRIVATE */ 417 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) 418 png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length)
608 { 419 {
609 png_bytep ptr; 420 png_bytep ptr;
610 421
611 if (png_ptr == NULL) 422 if (png_ptr == NULL)
612 return; 423 return;
613 424
614 ptr = buffer; 425 ptr = buffer;
615 if (png_ptr->save_buffer_size) 426 if (png_ptr->save_buffer_size != 0)
616 { 427 {
617 png_size_t save_size; 428 png_size_t save_size;
618 429
619 if (length < png_ptr->save_buffer_size) 430 if (length < png_ptr->save_buffer_size)
620 save_size = length; 431 save_size = length;
432
621 else 433 else
622 save_size = png_ptr->save_buffer_size; 434 save_size = png_ptr->save_buffer_size;
623 435
624 png_memcpy(ptr, png_ptr->save_buffer_ptr, save_size); 436 memcpy(ptr, png_ptr->save_buffer_ptr, save_size);
625 length -= save_size; 437 length -= save_size;
626 ptr += save_size; 438 ptr += save_size;
627 png_ptr->buffer_size -= save_size; 439 png_ptr->buffer_size -= save_size;
628 png_ptr->save_buffer_size -= save_size; 440 png_ptr->save_buffer_size -= save_size;
629 png_ptr->save_buffer_ptr += save_size; 441 png_ptr->save_buffer_ptr += save_size;
630 } 442 }
631 if (length && png_ptr->current_buffer_size) 443 if (length != 0 && png_ptr->current_buffer_size != 0)
632 { 444 {
633 png_size_t save_size; 445 png_size_t save_size;
634 446
635 if (length < png_ptr->current_buffer_size) 447 if (length < png_ptr->current_buffer_size)
636 save_size = length; 448 save_size = length;
637 449
638 else 450 else
639 save_size = png_ptr->current_buffer_size; 451 save_size = png_ptr->current_buffer_size;
640 452
641 png_memcpy(ptr, png_ptr->current_buffer_ptr, save_size); 453 memcpy(ptr, png_ptr->current_buffer_ptr, save_size);
642 png_ptr->buffer_size -= save_size; 454 png_ptr->buffer_size -= save_size;
643 png_ptr->current_buffer_size -= save_size; 455 png_ptr->current_buffer_size -= save_size;
644 png_ptr->current_buffer_ptr += save_size; 456 png_ptr->current_buffer_ptr += save_size;
645 } 457 }
646 } 458 }
647 459
648 void /* PRIVATE */ 460 void /* PRIVATE */
649 png_push_save_buffer(png_structp png_ptr) 461 png_push_save_buffer(png_structrp png_ptr)
650 { 462 {
651 if (png_ptr->save_buffer_size) 463 if (png_ptr->save_buffer_size != 0)
652 { 464 {
653 if (png_ptr->save_buffer_ptr != png_ptr->save_buffer) 465 if (png_ptr->save_buffer_ptr != png_ptr->save_buffer)
654 { 466 {
655 png_size_t i, istop; 467 png_size_t i, istop;
656 png_bytep sp; 468 png_bytep sp;
657 png_bytep dp; 469 png_bytep dp;
658 470
659 istop = png_ptr->save_buffer_size; 471 istop = png_ptr->save_buffer_size;
660 for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer; 472 for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer;
661 i < istop; i++, sp++, dp++) 473 i < istop; i++, sp++, dp++)
662 { 474 {
663 *dp = *sp; 475 *dp = *sp;
664 } 476 }
665 } 477 }
666 } 478 }
667 if (png_ptr->save_buffer_size + png_ptr->current_buffer_size > 479 if (png_ptr->save_buffer_size + png_ptr->current_buffer_size >
668 png_ptr->save_buffer_max) 480 png_ptr->save_buffer_max)
669 { 481 {
670 png_size_t new_max; 482 png_size_t new_max;
671 png_bytep old_buffer; 483 png_bytep old_buffer;
672 484
673 if (png_ptr->save_buffer_size > PNG_SIZE_MAX - 485 if (png_ptr->save_buffer_size > PNG_SIZE_MAX -
674 (png_ptr->current_buffer_size + 256)) 486 (png_ptr->current_buffer_size + 256))
675 { 487 {
676 png_error(png_ptr, "Potential overflow of save_buffer"); 488 png_error(png_ptr, "Potential overflow of save_buffer");
677 } 489 }
678 490
679 new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256; 491 new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;
680 old_buffer = png_ptr->save_buffer; 492 old_buffer = png_ptr->save_buffer;
681 png_ptr->save_buffer = (png_bytep)png_malloc_warn(png_ptr, 493 png_ptr->save_buffer = (png_bytep)png_malloc_warn(png_ptr,
682 (png_uint_32)new_max); 494 (png_size_t)new_max);
495
683 if (png_ptr->save_buffer == NULL) 496 if (png_ptr->save_buffer == NULL)
684 { 497 {
685 png_free(png_ptr, old_buffer); 498 png_free(png_ptr, old_buffer);
686 png_error(png_ptr, "Insufficient memory for save_buffer"); 499 png_error(png_ptr, "Insufficient memory for save_buffer");
687 } 500 }
688 else 501
689 { 502 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); 503 png_free(png_ptr, old_buffer);
691 png_free(png_ptr, old_buffer); 504 png_ptr->save_buffer_max = new_max;
692 png_ptr->save_buffer_max = new_max;
693 }
694 } 505 }
695 if (png_ptr->current_buffer_size) 506 if (png_ptr->current_buffer_size)
696 { 507 {
697 png_memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size, 508 memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
698 png_ptr->current_buffer_ptr, png_ptr->current_buffer_size); 509 png_ptr->current_buffer_ptr, png_ptr->current_buffer_size);
699 png_ptr->save_buffer_size += png_ptr->current_buffer_size; 510 png_ptr->save_buffer_size += png_ptr->current_buffer_size;
700 png_ptr->current_buffer_size = 0; 511 png_ptr->current_buffer_size = 0;
701 } 512 }
702 png_ptr->save_buffer_ptr = png_ptr->save_buffer; 513 png_ptr->save_buffer_ptr = png_ptr->save_buffer;
703 png_ptr->buffer_size = 0; 514 png_ptr->buffer_size = 0;
704 } 515 }
705 516
706 void /* PRIVATE */ 517 void /* PRIVATE */
707 png_push_restore_buffer(png_structp png_ptr, png_bytep buffer, 518 png_push_restore_buffer(png_structrp png_ptr, png_bytep buffer,
708 png_size_t buffer_length) 519 png_size_t buffer_length)
709 { 520 {
710 png_ptr->current_buffer = buffer; 521 png_ptr->current_buffer = buffer;
711 png_ptr->current_buffer_size = buffer_length; 522 png_ptr->current_buffer_size = buffer_length;
712 png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size; 523 png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size;
713 png_ptr->current_buffer_ptr = png_ptr->current_buffer; 524 png_ptr->current_buffer_ptr = png_ptr->current_buffer;
714 } 525 }
715 526
716 void /* PRIVATE */ 527 void /* PRIVATE */
717 png_push_read_IDAT(png_structp png_ptr) 528 png_push_read_IDAT(png_structrp png_ptr)
718 { 529 {
719 #ifdef PNG_USE_LOCAL_ARRAYS 530 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 { 531 {
724 png_byte chunk_length[4]; 532 png_byte chunk_length[4];
533 png_byte chunk_tag[4];
725 534
726 if (png_ptr->buffer_size < 8) 535 /* TODO: this code can be commoned up with the same code in push_read */
727 { 536 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); 537 png_push_fill_buffer(png_ptr, chunk_length, 4);
733 png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length); 538 png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
734 png_reset_crc(png_ptr); 539 png_reset_crc(png_ptr);
735 png_crc_read(png_ptr, png_ptr->chunk_name, 4); 540 png_crc_read(png_ptr, chunk_tag, 4);
541 png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(chunk_tag);
736 png_ptr->mode |= PNG_HAVE_CHUNK_HEADER; 542 png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
737 543
738 if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) 544 if (png_ptr->chunk_name != png_IDAT)
739 { 545 {
740 png_ptr->process_mode = PNG_READ_CHUNK_MODE; 546 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
741 if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED)) 547
548 if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
742 png_error(png_ptr, "Not enough compressed data"); 549 png_error(png_ptr, "Not enough compressed data");
550
743 return; 551 return;
744 } 552 }
745 553
746 png_ptr->idat_size = png_ptr->push_length; 554 png_ptr->idat_size = png_ptr->push_length;
747 } 555 }
748 if (png_ptr->idat_size && png_ptr->save_buffer_size) 556
557 if (png_ptr->idat_size != 0 && png_ptr->save_buffer_size != 0)
749 { 558 {
750 png_size_t save_size; 559 png_size_t save_size = png_ptr->save_buffer_size;
560 png_uint_32 idat_size = png_ptr->idat_size;
751 561
752 if (png_ptr->idat_size < (png_uint_32)png_ptr->save_buffer_size) 562 /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
753 { 563 * are of different types and we don't know which variable has the fewest
754 save_size = (png_size_t)png_ptr->idat_size; 564 * bits. Carefully select the smaller and cast it to the type of the
565 * larger - this cannot overflow. Do not cast in the following test - it
566 * will break on either 16-bit or 64-bit platforms.
567 */
568 if (idat_size < save_size)
569 save_size = (png_size_t)idat_size;
755 570
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 571 else
761 save_size = png_ptr->save_buffer_size; 572 idat_size = (png_uint_32)save_size;
762 573
763 png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size); 574 png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
764 575
765 png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size); 576 png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size);
766 577
767 png_ptr->idat_size -= save_size; 578 png_ptr->idat_size -= idat_size;
768 png_ptr->buffer_size -= save_size; 579 png_ptr->buffer_size -= save_size;
769 png_ptr->save_buffer_size -= save_size; 580 png_ptr->save_buffer_size -= save_size;
770 png_ptr->save_buffer_ptr += save_size; 581 png_ptr->save_buffer_ptr += save_size;
771 } 582 }
772 if (png_ptr->idat_size && png_ptr->current_buffer_size) 583
584 if (png_ptr->idat_size != 0 && png_ptr->current_buffer_size != 0)
773 { 585 {
774 png_size_t save_size; 586 png_size_t save_size = png_ptr->current_buffer_size;
587 png_uint_32 idat_size = png_ptr->idat_size;
775 588
776 if (png_ptr->idat_size < (png_uint_32)png_ptr->current_buffer_size) 589 /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
777 { 590 * are of different types and we don't know which variable has the fewest
778 save_size = (png_size_t)png_ptr->idat_size; 591 * bits. Carefully select the smaller and cast it to the type of the
592 * larger - this cannot overflow.
593 */
594 if (idat_size < save_size)
595 save_size = (png_size_t)idat_size;
779 596
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 597 else
785 save_size = png_ptr->current_buffer_size; 598 idat_size = (png_uint_32)save_size;
786 599
787 png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size); 600 png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
788 601
789 png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size); 602 png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size);
790 603
791 png_ptr->idat_size -= save_size; 604 png_ptr->idat_size -= idat_size;
792 png_ptr->buffer_size -= save_size; 605 png_ptr->buffer_size -= save_size;
793 png_ptr->current_buffer_size -= save_size; 606 png_ptr->current_buffer_size -= save_size;
794 png_ptr->current_buffer_ptr += save_size; 607 png_ptr->current_buffer_ptr += save_size;
795 } 608 }
796 if (!png_ptr->idat_size) 609
610 if (png_ptr->idat_size == 0)
797 { 611 {
798 if (png_ptr->buffer_size < 4) 612 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); 613 png_crc_finish(png_ptr, 0);
805 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER; 614 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
806 png_ptr->mode |= PNG_AFTER_IDAT; 615 png_ptr->mode |= PNG_AFTER_IDAT;
616 png_ptr->zowner = 0;
807 } 617 }
808 } 618 }
809 619
810 void /* PRIVATE */ 620 void /* PRIVATE */
811 png_process_IDAT_data(png_structp png_ptr, png_bytep buffer, 621 png_process_IDAT_data(png_structrp png_ptr, png_bytep buffer,
812 png_size_t buffer_length) 622 png_size_t buffer_length)
813 { 623 {
814 /* The caller checks for a non-zero buffer length. */ 624 /* The caller checks for a non-zero buffer length. */
815 if (!(buffer_length > 0) || buffer == NULL) 625 if (!(buffer_length > 0) || buffer == NULL)
816 png_error(png_ptr, "No IDAT data (internal error)"); 626 png_error(png_ptr, "No IDAT data (internal error)");
817 627
818 /* This routine must process all the data it has been given 628 /* This routine must process all the data it has been given
819 * before returning, calling the row callback as required to 629 * before returning, calling the row callback as required to
820 * handle the uncompressed results. 630 * handle the uncompressed results.
821 */ 631 */
822 png_ptr->zstream.next_in = buffer; 632 png_ptr->zstream.next_in = buffer;
633 /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
823 png_ptr->zstream.avail_in = (uInt)buffer_length; 634 png_ptr->zstream.avail_in = (uInt)buffer_length;
824 635
825 /* Keep going until the decompressed data is all processed 636 /* Keep going until the decompressed data is all processed
826 * or the stream marked as finished. 637 * or the stream marked as finished.
827 */ 638 */
828 while (png_ptr->zstream.avail_in > 0 && 639 while (png_ptr->zstream.avail_in > 0 &&
829 » !(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED)) 640 (png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
830 { 641 {
831 int ret; 642 int ret;
832 643
833 /* We have data for zlib, but we must check that zlib 644 /* We have data for zlib, but we must check that zlib
834 * has somewhere to put the results. It doesn't matter 645 * has someplace to put the results. It doesn't matter
835 * if we don't expect any results -- it may be the input 646 * if we don't expect any results -- it may be the input
836 * data is just the LZ end code. 647 * data is just the LZ end code.
837 */ 648 */
838 if (!(png_ptr->zstream.avail_out > 0)) 649 if (!(png_ptr->zstream.avail_out > 0))
839 { 650 {
840 png_ptr->zstream.avail_out = 651 /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
841 (uInt) PNG_ROWBYTES(png_ptr->pixel_depth, 652 png_ptr->zstream.avail_out = (uInt)(PNG_ROWBYTES(png_ptr->pixel_depth,
842 png_ptr->iwidth) + 1; 653 png_ptr->iwidth) + 1);
654
843 png_ptr->zstream.next_out = png_ptr->row_buf; 655 png_ptr->zstream.next_out = png_ptr->row_buf;
844 } 656 }
845 657
846 /* Using Z_SYNC_FLUSH here means that an unterminated 658 /* Using Z_SYNC_FLUSH here means that an unterminated
847 * LZ stream can still be handled (a stream with a missing 659 * LZ stream (a stream with a missing end code) can still
848 * end code), otherwise (Z_NO_FLUSH) a future zlib 660 * be handled, otherwise (Z_NO_FLUSH) a future zlib
849 * implementation might defer output and, therefore, 661 * implementation might defer output and therefore
850 * change the current behavior. (See comments in inflate.c 662 * change the current behavior (see comments in inflate.c
851 * for why this doesn't happen at present with zlib 1.2.5.) 663 * for why this doesn't happen at present with zlib 1.2.5).
852 */ 664 */
853 ret = inflate(&png_ptr->zstream, Z_SYNC_FLUSH); 665 ret = inflate(&png_ptr->zstream, Z_SYNC_FLUSH);
854 666
855 /* Check for any failure before proceeding. */ 667 /* Check for any failure before proceeding. */
856 if (ret != Z_OK && ret != Z_STREAM_END) 668 if (ret != Z_OK && ret != Z_STREAM_END)
857 { 669 {
858 » /* Terminate the decompression. */ 670 /* Terminate the decompression. */
859 » png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; 671 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
672 png_ptr->zowner = 0;
860 673
861 /* This may be a truncated stream (missing or 674 /* This may be a truncated stream (missing or
862 » * damaged end code). Treat that as a warning. 675 * damaged end code). Treat that as a warning.
863 » */ 676 */
864 if (png_ptr->row_number >= png_ptr->num_rows || 677 if (png_ptr->row_number >= png_ptr->num_rows ||
865 » png_ptr->pass > 6) 678 png_ptr->pass > 6)
866 » png_warning(png_ptr, "Truncated compressed data in IDAT"); 679 png_warning(png_ptr, "Truncated compressed data in IDAT");
867 » else
868 » png_error(png_ptr, "Decompression error in IDAT");
869 680
870 » /* Skip the check on unprocessed input */ 681 else
682 png_error(png_ptr, "Decompression error in IDAT");
683
684 /* Skip the check on unprocessed input */
871 return; 685 return;
872 } 686 }
873 687
874 /* Did inflate output any data? */ 688 /* Did inflate output any data? */
875 if (png_ptr->zstream.next_out != png_ptr->row_buf) 689 if (png_ptr->zstream.next_out != png_ptr->row_buf)
876 { 690 {
877 » /* Is this unexpected data after the last row? 691 /* Is this unexpected data after the last row?
878 » * If it is, artificially terminate the LZ output 692 * If it is, artificially terminate the LZ output
879 » * here. 693 * here.
880 » */ 694 */
881 if (png_ptr->row_number >= png_ptr->num_rows || 695 if (png_ptr->row_number >= png_ptr->num_rows ||
882 » png_ptr->pass > 6) 696 png_ptr->pass > 6)
883 { 697 {
884 » /* Extra data. */ 698 /* Extra data. */
885 » png_warning(png_ptr, "Extra compressed data in IDAT"); 699 png_warning(png_ptr, "Extra compressed data in IDAT");
886 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; 700 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
887 » /* Do no more processing; skip the unprocessed 701 png_ptr->zowner = 0;
888 » * input check below. 702
889 » */ 703 /* Do no more processing; skip the unprocessed
704 * input check below.
705 */
890 return; 706 return;
891 » } 707 }
892 708
893 » /* Do we have a complete row? */ 709 /* Do we have a complete row? */
894 » if (png_ptr->zstream.avail_out == 0) 710 if (png_ptr->zstream.avail_out == 0)
895 » png_push_process_row(png_ptr); 711 png_push_process_row(png_ptr);
896 } 712 }
897 713
898 /* And check for the end of the stream. */ 714 /* And check for the end of the stream. */
899 if (ret == Z_STREAM_END) 715 if (ret == Z_STREAM_END)
900 » png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; 716 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
901 } 717 }
902 718
903 /* All the data should have been processed, if anything 719 /* All the data should have been processed, if anything
904 * is left at this point we have bytes of IDAT data 720 * is left at this point we have bytes of IDAT data
905 * after the zlib end code. 721 * after the zlib end code.
906 */ 722 */
907 if (png_ptr->zstream.avail_in > 0) 723 if (png_ptr->zstream.avail_in > 0)
908 png_warning(png_ptr, "Extra compression data"); 724 png_warning(png_ptr, "Extra compression data in IDAT");
909 } 725 }
910 726
911 void /* PRIVATE */ 727 void /* PRIVATE */
912 png_push_process_row(png_structp png_ptr) 728 png_push_process_row(png_structrp png_ptr)
913 { 729 {
914 png_ptr->row_info.color_type = png_ptr->color_type; 730 /* 1.5.6: row_info moved out of png_struct to a local here. */
915 png_ptr->row_info.width = png_ptr->iwidth; 731 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 732
920 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth, 733 row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */
921 png_ptr->row_info.width); 734 row_info.color_type = png_ptr->color_type;
735 row_info.bit_depth = png_ptr->bit_depth;
736 row_info.channels = png_ptr->channels;
737 row_info.pixel_depth = png_ptr->pixel_depth;
738 row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
922 739
923 png_read_filter_row(png_ptr, &(png_ptr->row_info), 740 if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
924 png_ptr->row_buf + 1, png_ptr->prev_row + 1, 741 {
925 (int)(png_ptr->row_buf[0])); 742 if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
743 png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1,
744 png_ptr->prev_row + 1, png_ptr->row_buf[0]);
745 else
746 png_error(png_ptr, "bad adaptive filter value");
747 }
926 748
927 png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf, 749 /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
928 png_ptr->rowbytes + 1); 750 * 1.5.6, while the buffer really is this big in current versions of libpng
751 * it may not be in the future, so this was changed just to copy the
752 * interlaced row count:
753 */
754 memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
929 755
930 if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA)) 756 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
931 png_do_read_transformations(png_ptr); 757 if (png_ptr->transformations != 0)
758 png_do_read_transformations(png_ptr, &row_info);
759 #endif
760
761 /* The transformed pixel depth should match the depth now in row_info. */
762 if (png_ptr->transformed_pixel_depth == 0)
763 {
764 png_ptr->transformed_pixel_depth = row_info.pixel_depth;
765 if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
766 png_error(png_ptr, "progressive row overflow");
767 }
768
769 else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
770 png_error(png_ptr, "internal progressive row size calculation error");
771
932 772
933 #ifdef PNG_READ_INTERLACING_SUPPORTED 773 #ifdef PNG_READ_INTERLACING_SUPPORTED
934 /* Blow up interlaced rows to full size */ 774 /* Expand interlaced rows to full size */
935 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE)) 775 if (png_ptr->interlaced != 0 &&
776 (png_ptr->transformations & PNG_INTERLACE) != 0)
936 { 777 {
937 if (png_ptr->pass < 6) 778 if (png_ptr->pass < 6)
938 /* old interface (pre-1.0.9): 779 png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
939 png_do_read_interlace(&(png_ptr->row_info), 780 png_ptr->transformations);
940 png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
941 */
942 png_do_read_interlace(png_ptr);
943 781
944 switch (png_ptr->pass) 782 switch (png_ptr->pass)
945 { 783 {
946 case 0: 784 case 0:
947 { 785 {
948 int i; 786 int i;
949 for (i = 0; i < 8 && png_ptr->pass == 0; i++) 787 for (i = 0; i < 8 && png_ptr->pass == 0; i++)
950 { 788 {
951 png_push_have_row(png_ptr, png_ptr->row_buf + 1); 789 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
952 png_read_push_finish_row(png_ptr); /* Updates png_ptr->pass */ 790 png_read_push_finish_row(png_ptr); /* Updates png_ptr->pass */
953 } 791 }
954 792
955 if (png_ptr->pass == 2) /* Pass 1 might be empty */ 793 if (png_ptr->pass == 2) /* Pass 1 might be empty */
956 { 794 {
957 for (i = 0; i < 4 && png_ptr->pass == 2; i++) 795 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
958 { 796 {
959 png_push_have_row(png_ptr, png_bytep_NULL); 797 png_push_have_row(png_ptr, NULL);
960 png_read_push_finish_row(png_ptr); 798 png_read_push_finish_row(png_ptr);
961 } 799 }
962 } 800 }
963 801
964 if (png_ptr->pass == 4 && png_ptr->height <= 4) 802 if (png_ptr->pass == 4 && png_ptr->height <= 4)
965 { 803 {
966 for (i = 0; i < 2 && png_ptr->pass == 4; i++) 804 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
967 { 805 {
968 png_push_have_row(png_ptr, png_bytep_NULL); 806 png_push_have_row(png_ptr, NULL);
969 png_read_push_finish_row(png_ptr); 807 png_read_push_finish_row(png_ptr);
970 } 808 }
971 } 809 }
972 810
973 if (png_ptr->pass == 6 && png_ptr->height <= 4) 811 if (png_ptr->pass == 6 && png_ptr->height <= 4)
974 { 812 {
975 png_push_have_row(png_ptr, png_bytep_NULL); 813 png_push_have_row(png_ptr, NULL);
976 png_read_push_finish_row(png_ptr); 814 png_read_push_finish_row(png_ptr);
977 } 815 }
978 816
979 break; 817 break;
980 } 818 }
981 819
982 case 1: 820 case 1:
983 { 821 {
984 int i; 822 int i;
985 for (i = 0; i < 8 && png_ptr->pass == 1; i++) 823 for (i = 0; i < 8 && png_ptr->pass == 1; i++)
986 { 824 {
987 png_push_have_row(png_ptr, png_ptr->row_buf + 1); 825 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
988 png_read_push_finish_row(png_ptr); 826 png_read_push_finish_row(png_ptr);
989 } 827 }
990 828
991 if (png_ptr->pass == 2) /* Skip top 4 generated rows */ 829 if (png_ptr->pass == 2) /* Skip top 4 generated rows */
992 { 830 {
993 for (i = 0; i < 4 && png_ptr->pass == 2; i++) 831 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
994 { 832 {
995 png_push_have_row(png_ptr, png_bytep_NULL); 833 png_push_have_row(png_ptr, NULL);
996 png_read_push_finish_row(png_ptr); 834 png_read_push_finish_row(png_ptr);
997 } 835 }
998 } 836 }
999 837
1000 break; 838 break;
1001 } 839 }
1002 840
1003 case 2: 841 case 2:
1004 { 842 {
1005 int i; 843 int i;
1006 844
1007 for (i = 0; i < 4 && png_ptr->pass == 2; i++) 845 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
1008 { 846 {
1009 png_push_have_row(png_ptr, png_ptr->row_buf + 1); 847 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1010 png_read_push_finish_row(png_ptr); 848 png_read_push_finish_row(png_ptr);
1011 } 849 }
1012 850
1013 for (i = 0; i < 4 && png_ptr->pass == 2; i++) 851 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
1014 { 852 {
1015 png_push_have_row(png_ptr, png_bytep_NULL); 853 png_push_have_row(png_ptr, NULL);
1016 png_read_push_finish_row(png_ptr); 854 png_read_push_finish_row(png_ptr);
1017 } 855 }
1018 856
1019 if (png_ptr->pass == 4) /* Pass 3 might be empty */ 857 if (png_ptr->pass == 4) /* Pass 3 might be empty */
1020 { 858 {
1021 for (i = 0; i < 2 && png_ptr->pass == 4; i++) 859 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1022 { 860 {
1023 png_push_have_row(png_ptr, png_bytep_NULL); 861 png_push_have_row(png_ptr, NULL);
1024 png_read_push_finish_row(png_ptr); 862 png_read_push_finish_row(png_ptr);
1025 } 863 }
1026 } 864 }
1027 865
1028 break; 866 break;
1029 } 867 }
1030 868
1031 case 3: 869 case 3:
1032 { 870 {
1033 int i; 871 int i;
1034 872
1035 for (i = 0; i < 4 && png_ptr->pass == 3; i++) 873 for (i = 0; i < 4 && png_ptr->pass == 3; i++)
1036 { 874 {
1037 png_push_have_row(png_ptr, png_ptr->row_buf + 1); 875 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1038 png_read_push_finish_row(png_ptr); 876 png_read_push_finish_row(png_ptr);
1039 } 877 }
1040 878
1041 if (png_ptr->pass == 4) /* Skip top two generated rows */ 879 if (png_ptr->pass == 4) /* Skip top two generated rows */
1042 { 880 {
1043 for (i = 0; i < 2 && png_ptr->pass == 4; i++) 881 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1044 { 882 {
1045 png_push_have_row(png_ptr, png_bytep_NULL); 883 png_push_have_row(png_ptr, NULL);
1046 png_read_push_finish_row(png_ptr); 884 png_read_push_finish_row(png_ptr);
1047 } 885 }
1048 } 886 }
1049 887
1050 break; 888 break;
1051 } 889 }
1052 890
1053 case 4: 891 case 4:
1054 { 892 {
1055 int i; 893 int i;
1056 894
1057 for (i = 0; i < 2 && png_ptr->pass == 4; i++) 895 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1058 { 896 {
1059 png_push_have_row(png_ptr, png_ptr->row_buf + 1); 897 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1060 png_read_push_finish_row(png_ptr); 898 png_read_push_finish_row(png_ptr);
1061 } 899 }
1062 900
1063 for (i = 0; i < 2 && png_ptr->pass == 4; i++) 901 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
1064 { 902 {
1065 png_push_have_row(png_ptr, png_bytep_NULL); 903 png_push_have_row(png_ptr, NULL);
1066 png_read_push_finish_row(png_ptr); 904 png_read_push_finish_row(png_ptr);
1067 } 905 }
1068 906
1069 if (png_ptr->pass == 6) /* Pass 5 might be empty */ 907 if (png_ptr->pass == 6) /* Pass 5 might be empty */
1070 { 908 {
1071 png_push_have_row(png_ptr, png_bytep_NULL); 909 png_push_have_row(png_ptr, NULL);
1072 png_read_push_finish_row(png_ptr); 910 png_read_push_finish_row(png_ptr);
1073 } 911 }
1074 912
1075 break; 913 break;
1076 } 914 }
1077 915
1078 case 5: 916 case 5:
1079 { 917 {
1080 int i; 918 int i;
1081 919
1082 for (i = 0; i < 2 && png_ptr->pass == 5; i++) 920 for (i = 0; i < 2 && png_ptr->pass == 5; i++)
1083 { 921 {
1084 png_push_have_row(png_ptr, png_ptr->row_buf + 1); 922 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1085 png_read_push_finish_row(png_ptr); 923 png_read_push_finish_row(png_ptr);
1086 } 924 }
1087 925
1088 if (png_ptr->pass == 6) /* Skip top generated row */ 926 if (png_ptr->pass == 6) /* Skip top generated row */
1089 { 927 {
1090 png_push_have_row(png_ptr, png_bytep_NULL); 928 png_push_have_row(png_ptr, NULL);
1091 png_read_push_finish_row(png_ptr); 929 png_read_push_finish_row(png_ptr);
1092 } 930 }
1093 931
1094 break; 932 break;
1095 } 933 }
934
935 default:
1096 case 6: 936 case 6:
1097 { 937 {
1098 png_push_have_row(png_ptr, png_ptr->row_buf + 1); 938 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1099 png_read_push_finish_row(png_ptr); 939 png_read_push_finish_row(png_ptr);
1100 940
1101 if (png_ptr->pass != 6) 941 if (png_ptr->pass != 6)
1102 break; 942 break;
1103 943
1104 png_push_have_row(png_ptr, png_bytep_NULL); 944 png_push_have_row(png_ptr, NULL);
1105 png_read_push_finish_row(png_ptr); 945 png_read_push_finish_row(png_ptr);
1106 } 946 }
1107 } 947 }
1108 } 948 }
1109 else 949 else
1110 #endif 950 #endif
1111 { 951 {
1112 png_push_have_row(png_ptr, png_ptr->row_buf + 1); 952 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1113 png_read_push_finish_row(png_ptr); 953 png_read_push_finish_row(png_ptr);
1114 } 954 }
1115 } 955 }
1116 956
1117 void /* PRIVATE */ 957 void /* PRIVATE */
1118 png_read_push_finish_row(png_structp png_ptr) 958 png_read_push_finish_row(png_structrp png_ptr)
1119 { 959 {
1120 #ifdef PNG_USE_LOCAL_ARRAYS 960 #ifdef PNG_READ_INTERLACING_SUPPORTED
1121 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ 961 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1122 962
1123 /* Start of interlace block */ 963 /* Start of interlace block */
1124 PNG_CONST int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0}; 964 static PNG_CONST png_byte png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
1125 965
1126 /* Offset to next interlace block */ 966 /* Offset to next interlace block */
1127 PNG_CONST int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1}; 967 static PNG_CONST png_byte png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
1128 968
1129 /* Start of interlace block in the y direction */ 969 /* Start of interlace block in the y direction */
1130 PNG_CONST int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1}; 970 static PNG_CONST png_byte png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
1131 971
1132 /* Offset to next interlace block in the y direction */ 972 /* Offset to next interlace block in the y direction */
1133 PNG_CONST int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2}; 973 static PNG_CONST png_byte png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
1134 974
1135 /* Height of interlace block. This is not currently used - if you need 975 /* Height of interlace block. This is not currently used - if you need
1136 * it, uncomment it here and in png.h 976 * it, uncomment it here and in png.h
1137 PNG_CONST int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1}; 977 static PNG_CONST png_byte png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
1138 */ 978 */
1139 #endif 979 #endif
1140 980
1141 png_ptr->row_number++; 981 png_ptr->row_number++;
1142 if (png_ptr->row_number < png_ptr->num_rows) 982 if (png_ptr->row_number < png_ptr->num_rows)
1143 return; 983 return;
1144 984
1145 #ifdef PNG_READ_INTERLACING_SUPPORTED 985 #ifdef PNG_READ_INTERLACING_SUPPORTED
1146 if (png_ptr->interlaced) 986 if (png_ptr->interlaced != 0)
1147 { 987 {
1148 png_ptr->row_number = 0; 988 png_ptr->row_number = 0;
1149 png_memset_check(png_ptr, png_ptr->prev_row, 0, 989 memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
1150 png_ptr->rowbytes + 1); 990
1151 do 991 do
1152 { 992 {
1153 png_ptr->pass++; 993 png_ptr->pass++;
1154 if ((png_ptr->pass == 1 && png_ptr->width < 5) || 994 if ((png_ptr->pass == 1 && png_ptr->width < 5) ||
1155 (png_ptr->pass == 3 && png_ptr->width < 3) || 995 (png_ptr->pass == 3 && png_ptr->width < 3) ||
1156 (png_ptr->pass == 5 && png_ptr->width < 2)) 996 (png_ptr->pass == 5 && png_ptr->width < 2))
1157 png_ptr->pass++; 997 png_ptr->pass++;
1158 998
1159 if (png_ptr->pass > 7) 999 if (png_ptr->pass > 7)
1160 png_ptr->pass--; 1000 png_ptr->pass--;
1161 1001
1162 if (png_ptr->pass >= 7) 1002 if (png_ptr->pass >= 7)
1163 break; 1003 break;
1164 1004
1165 png_ptr->iwidth = (png_ptr->width + 1005 png_ptr->iwidth = (png_ptr->width +
1166 png_pass_inc[png_ptr->pass] - 1 - 1006 png_pass_inc[png_ptr->pass] - 1 -
1167 png_pass_start[png_ptr->pass]) / 1007 png_pass_start[png_ptr->pass]) /
1168 png_pass_inc[png_ptr->pass]; 1008 png_pass_inc[png_ptr->pass];
1169 1009
1170 if (png_ptr->transformations & PNG_INTERLACE) 1010 if ((png_ptr->transformations & PNG_INTERLACE) != 0)
1171 break; 1011 break;
1172 1012
1173 png_ptr->num_rows = (png_ptr->height + 1013 png_ptr->num_rows = (png_ptr->height +
1174 png_pass_yinc[png_ptr->pass] - 1 - 1014 png_pass_yinc[png_ptr->pass] - 1 -
1175 png_pass_ystart[png_ptr->pass]) / 1015 png_pass_ystart[png_ptr->pass]) /
1176 png_pass_yinc[png_ptr->pass]; 1016 png_pass_yinc[png_ptr->pass];
1177 1017
1178 } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0); 1018 } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0);
1179 } 1019 }
1180 #endif /* PNG_READ_INTERLACING_SUPPORTED */ 1020 #endif /* READ_INTERLACING */
1181 } 1021 }
1182 1022
1183 void /* PRIVATE */ 1023 void /* PRIVATE */
1184 png_push_have_info(png_structp png_ptr, png_infop info_ptr) 1024 png_push_have_info(png_structrp png_ptr, png_inforp info_ptr)
1185 { 1025 {
1186 if (png_ptr->info_fn != NULL) 1026 if (png_ptr->info_fn != NULL)
1187 (*(png_ptr->info_fn))(png_ptr, info_ptr); 1027 (*(png_ptr->info_fn))(png_ptr, info_ptr);
1188 } 1028 }
1189 1029
1190 void /* PRIVATE */ 1030 void /* PRIVATE */
1191 png_push_have_end(png_structp png_ptr, png_infop info_ptr) 1031 png_push_have_end(png_structrp png_ptr, png_inforp info_ptr)
1192 { 1032 {
1193 if (png_ptr->end_fn != NULL) 1033 if (png_ptr->end_fn != NULL)
1194 (*(png_ptr->end_fn))(png_ptr, info_ptr); 1034 (*(png_ptr->end_fn))(png_ptr, info_ptr);
1195 } 1035 }
1196 1036
1197 void /* PRIVATE */ 1037 void /* PRIVATE */
1198 png_push_have_row(png_structp png_ptr, png_bytep row) 1038 png_push_have_row(png_structrp png_ptr, png_bytep row)
1199 { 1039 {
1200 if (png_ptr->row_fn != NULL) 1040 if (png_ptr->row_fn != NULL)
1201 (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number, 1041 (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number,
1202 (int)png_ptr->pass); 1042 (int)png_ptr->pass);
1203 } 1043 }
1204 1044
1045 #ifdef PNG_READ_INTERLACING_SUPPORTED
1205 void PNGAPI 1046 void PNGAPI
1206 png_progressive_combine_row (png_structp png_ptr, 1047 png_progressive_combine_row(png_const_structrp png_ptr, png_bytep old_row,
1207 png_bytep old_row, png_bytep new_row) 1048 png_const_bytep new_row)
1208 { 1049 {
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) 1050 if (png_ptr == NULL)
1215 return; 1051 return;
1216 1052
1217 if (new_row != NULL) /* new_row must == png_ptr->row_buf here. */ 1053 /* 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]); 1054 * from an empty row (see the calls to png_struct::row_fn below), otherwise
1055 * it must be png_ptr->row_buf+1
1056 */
1057 if (new_row != NULL)
1058 png_combine_row(png_ptr, old_row, 1/*blocky display*/);
1219 } 1059 }
1060 #endif /* READ_INTERLACING */
1220 1061
1221 void PNGAPI 1062 void PNGAPI
1222 png_set_progressive_read_fn(png_structp png_ptr, png_voidp progressive_ptr, 1063 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, 1064 png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
1224 png_progressive_end_ptr end_fn) 1065 png_progressive_end_ptr end_fn)
1225 { 1066 {
1226 if (png_ptr == NULL) 1067 if (png_ptr == NULL)
1227 return; 1068 return;
1228 1069
1229 png_ptr->info_fn = info_fn; 1070 png_ptr->info_fn = info_fn;
1230 png_ptr->row_fn = row_fn; 1071 png_ptr->row_fn = row_fn;
1231 png_ptr->end_fn = end_fn; 1072 png_ptr->end_fn = end_fn;
1232 1073
1233 png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer); 1074 png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer);
1234 } 1075 }
1235 1076
1236 png_voidp PNGAPI 1077 png_voidp PNGAPI
1237 png_get_progressive_ptr(png_structp png_ptr) 1078 png_get_progressive_ptr(png_const_structrp png_ptr)
1238 { 1079 {
1239 if (png_ptr == NULL) 1080 if (png_ptr == NULL)
1240 return (NULL); 1081 return (NULL);
1241 1082
1242 return png_ptr->io_ptr; 1083 return png_ptr->io_ptr;
1243 } 1084 }
1244 #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */ 1085 #endif /* PROGRESSIVE_READ */
OLDNEW
« no previous file with comments | « third_party/libpng/pngmem.c ('k') | third_party/libpng/pngpriv.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698