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