126
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)
133
#ifdef PNG_READ_bKGD_SUPPORTED
136
#ifdef PNG_READ_cHRM_SUPPORTED
139
#ifdef PNG_READ_gAMA_SUPPORTED
142
#ifdef PNG_READ_hIST_SUPPORTED
145
#ifdef PNG_READ_iCCP_SUPPORTED
148
#ifdef PNG_READ_iTXt_SUPPORTED
151
#ifdef PNG_READ_oFFs_SUPPORTED
154
#ifdef PNG_READ_pCAL_SUPPORTED
157
#ifdef PNG_READ_pHYs_SUPPORTED
160
#ifdef PNG_READ_sBIT_SUPPORTED
163
#ifdef PNG_READ_sCAL_SUPPORTED
166
#ifdef PNG_READ_sRGB_SUPPORTED
169
#ifdef PNG_READ_sPLT_SUPPORTED
172
#ifdef PNG_READ_tEXt_SUPPORTED
175
#ifdef PNG_READ_tIME_SUPPORTED
178
#ifdef PNG_READ_tRNS_SUPPORTED
181
#ifdef PNG_READ_zTXt_SUPPORTED
169
png_uint_32 chunk_name;
170
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
171
int keep; /* unknown handling method */
185
/* First we make sure we have enough data for the 4 byte chunk name
186
* and the 4 byte chunk length before proceeding with decoding the
174
/* First we make sure we have enough data for the 4-byte chunk name
175
* and the 4-byte chunk length before proceeding with decoding the
187
176
* chunk data. To fully decode each of these chunks, we also make
188
* 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
189
178
* end of every chunk (except IDAT, which is handled separately).
191
if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
180
if ((png_ptr->mode & PNG_HAVE_CHUNK_HEADER) == 0)
193
182
png_byte chunk_length[4];
195
if (png_ptr->buffer_size < 8)
197
png_push_save_buffer(png_ptr);
183
png_byte chunk_tag[4];
185
PNG_PUSH_SAVE_BUFFER_IF_LT(8)
201
186
png_push_fill_buffer(png_ptr, chunk_length, 4);
202
187
png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
203
188
png_reset_crc(png_ptr);
204
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);
205
191
png_check_chunk_name(png_ptr, png_ptr->chunk_name);
206
192
png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
209
if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
210
if (png_ptr->mode & PNG_AFTER_IDAT)
211
png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
213
if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
195
chunk_name = png_ptr->chunk_name;
197
if (chunk_name == png_IDAT)
199
if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
200
png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
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).
206
if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
207
png_error(png_ptr, "Missing IHDR before IDAT");
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");
213
png_ptr->mode |= PNG_HAVE_IDAT;
214
png_ptr->process_mode = PNG_READ_IDAT_MODE;
216
if ((png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) == 0)
217
if (png_ptr->push_length == 0)
220
if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
221
png_benign_error(png_ptr, "Too many IDATs found");
224
if (chunk_name == png_IHDR)
215
226
if (png_ptr->push_length != 13)
216
227
png_error(png_ptr, "Invalid IHDR length");
218
if (png_ptr->push_length + 4 > png_ptr->buffer_size)
220
png_push_save_buffer(png_ptr);
229
PNG_PUSH_SAVE_BUFFER_IF_FULL
224
230
png_handle_IHDR(png_ptr, info_ptr, png_ptr->push_length);
227
else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
233
else if (chunk_name == png_IEND)
229
if (png_ptr->push_length + 4 > png_ptr->buffer_size)
231
png_push_save_buffer(png_ptr);
235
PNG_PUSH_SAVE_BUFFER_IF_FULL
235
236
png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length);
237
238
png_ptr->process_mode = PNG_READ_DONE_MODE;
241
242
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
242
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)
244
if (png_ptr->push_length + 4 > png_ptr->buffer_size)
246
png_push_save_buffer(png_ptr);
250
if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
251
png_ptr->mode |= PNG_HAVE_IDAT;
253
png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length);
255
if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
245
PNG_PUSH_SAVE_BUFFER_IF_FULL
246
png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length, keep);
248
if (chunk_name == png_PLTE)
256
249
png_ptr->mode |= PNG_HAVE_PLTE;
258
else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
260
if (!(png_ptr->mode & PNG_HAVE_IHDR))
261
png_error(png_ptr, "Missing IHDR before IDAT");
263
else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
264
!(png_ptr->mode & PNG_HAVE_PLTE))
265
png_error(png_ptr, "Missing PLTE before IDAT");
270
else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
253
else if (chunk_name == png_PLTE)
272
if (png_ptr->push_length + 4 > png_ptr->buffer_size)
274
png_push_save_buffer(png_ptr);
255
PNG_PUSH_SAVE_BUFFER_IF_FULL
277
256
png_handle_PLTE(png_ptr, info_ptr, png_ptr->push_length);
280
else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
259
else if (chunk_name == png_IDAT)
282
/* If we reach an IDAT chunk, this means we have read all of the
283
* header chunks, and we can start reading the image (or if this
284
* is called after the image has been read - we have an error).
287
if (!(png_ptr->mode & PNG_HAVE_IHDR))
288
png_error(png_ptr, "Missing IHDR before IDAT");
290
else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
291
!(png_ptr->mode & PNG_HAVE_PLTE))
292
png_error(png_ptr, "Missing PLTE before IDAT");
294
if (png_ptr->mode & PNG_HAVE_IDAT)
296
if (!(png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
297
if (png_ptr->push_length == 0)
300
if (png_ptr->mode & PNG_AFTER_IDAT)
301
png_benign_error(png_ptr, "Too many IDATs found");
304
261
png_ptr->idat_size = png_ptr->push_length;
305
png_ptr->mode |= PNG_HAVE_IDAT;
306
262
png_ptr->process_mode = PNG_READ_IDAT_MODE;
307
263
png_push_have_info(png_ptr, info_ptr);
308
264
png_ptr->zstream.avail_out =
315
271
#ifdef PNG_READ_gAMA_SUPPORTED
316
else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
272
else if (png_ptr->chunk_name == png_gAMA)
318
if (png_ptr->push_length + 4 > png_ptr->buffer_size)
320
png_push_save_buffer(png_ptr);
274
PNG_PUSH_SAVE_BUFFER_IF_FULL
324
275
png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length);
328
279
#ifdef PNG_READ_sBIT_SUPPORTED
329
else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
280
else if (png_ptr->chunk_name == png_sBIT)
331
if (png_ptr->push_length + 4 > png_ptr->buffer_size)
333
png_push_save_buffer(png_ptr);
282
PNG_PUSH_SAVE_BUFFER_IF_FULL
337
283
png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length);
341
287
#ifdef PNG_READ_cHRM_SUPPORTED
342
else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
288
else if (png_ptr->chunk_name == png_cHRM)
344
if (png_ptr->push_length + 4 > png_ptr->buffer_size)
346
png_push_save_buffer(png_ptr);
290
PNG_PUSH_SAVE_BUFFER_IF_FULL
350
291
png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length);
354
295
#ifdef PNG_READ_sRGB_SUPPORTED
355
else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
296
else if (chunk_name == png_sRGB)
357
if (png_ptr->push_length + 4 > png_ptr->buffer_size)
359
png_push_save_buffer(png_ptr);
298
PNG_PUSH_SAVE_BUFFER_IF_FULL
363
299
png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length);
367
303
#ifdef PNG_READ_iCCP_SUPPORTED
368
else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4))
304
else if (png_ptr->chunk_name == png_iCCP)
370
if (png_ptr->push_length + 4 > png_ptr->buffer_size)
372
png_push_save_buffer(png_ptr);
306
PNG_PUSH_SAVE_BUFFER_IF_FULL
376
307
png_handle_iCCP(png_ptr, info_ptr, png_ptr->push_length);
380
311
#ifdef PNG_READ_sPLT_SUPPORTED
381
else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4))
312
else if (chunk_name == png_sPLT)
383
if (png_ptr->push_length + 4 > png_ptr->buffer_size)
385
png_push_save_buffer(png_ptr);
314
PNG_PUSH_SAVE_BUFFER_IF_FULL
389
315
png_handle_sPLT(png_ptr, info_ptr, png_ptr->push_length);
393
319
#ifdef PNG_READ_tRNS_SUPPORTED
394
else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
320
else if (chunk_name == png_tRNS)
396
if (png_ptr->push_length + 4 > png_ptr->buffer_size)
398
png_push_save_buffer(png_ptr);
322
PNG_PUSH_SAVE_BUFFER_IF_FULL
402
323
png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length);
406
327
#ifdef PNG_READ_bKGD_SUPPORTED
407
else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
328
else if (chunk_name == png_bKGD)
409
if (png_ptr->push_length + 4 > png_ptr->buffer_size)
411
png_push_save_buffer(png_ptr);
330
PNG_PUSH_SAVE_BUFFER_IF_FULL
415
331
png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length);
419
335
#ifdef PNG_READ_hIST_SUPPORTED
420
else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
336
else if (chunk_name == png_hIST)
422
if (png_ptr->push_length + 4 > png_ptr->buffer_size)
424
png_push_save_buffer(png_ptr);
338
PNG_PUSH_SAVE_BUFFER_IF_FULL
428
339
png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length);
432
343
#ifdef PNG_READ_pHYs_SUPPORTED
433
else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
344
else if (chunk_name == png_pHYs)
435
if (png_ptr->push_length + 4 > png_ptr->buffer_size)
437
png_push_save_buffer(png_ptr);
346
PNG_PUSH_SAVE_BUFFER_IF_FULL
441
347
png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length);
445
351
#ifdef PNG_READ_oFFs_SUPPORTED
446
else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
352
else if (chunk_name == png_oFFs)
448
if (png_ptr->push_length + 4 > png_ptr->buffer_size)
450
png_push_save_buffer(png_ptr);
354
PNG_PUSH_SAVE_BUFFER_IF_FULL
454
355
png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length);
458
359
#ifdef PNG_READ_pCAL_SUPPORTED
459
else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
360
else if (chunk_name == png_pCAL)
461
if (png_ptr->push_length + 4 > png_ptr->buffer_size)
463
png_push_save_buffer(png_ptr);
362
PNG_PUSH_SAVE_BUFFER_IF_FULL
467
363
png_handle_pCAL(png_ptr, info_ptr, png_ptr->push_length);
471
367
#ifdef PNG_READ_sCAL_SUPPORTED
472
else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4))
368
else if (chunk_name == png_sCAL)
474
if (png_ptr->push_length + 4 > png_ptr->buffer_size)
476
png_push_save_buffer(png_ptr);
370
PNG_PUSH_SAVE_BUFFER_IF_FULL
480
371
png_handle_sCAL(png_ptr, info_ptr, png_ptr->push_length);
484
375
#ifdef PNG_READ_tIME_SUPPORTED
485
else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
376
else if (chunk_name == png_tIME)
487
if (png_ptr->push_length + 4 > png_ptr->buffer_size)
489
png_push_save_buffer(png_ptr);
378
PNG_PUSH_SAVE_BUFFER_IF_FULL
493
379
png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length);
497
383
#ifdef PNG_READ_tEXt_SUPPORTED
498
else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
384
else if (chunk_name == png_tEXt)
500
if (png_ptr->push_length + 4 > png_ptr->buffer_size)
502
png_push_save_buffer(png_ptr);
386
PNG_PUSH_SAVE_BUFFER_IF_FULL
506
387
png_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length);
510
391
#ifdef PNG_READ_zTXt_SUPPORTED
511
else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
392
else if (chunk_name == png_zTXt)
513
if (png_ptr->push_length + 4 > png_ptr->buffer_size)
515
png_push_save_buffer(png_ptr);
394
PNG_PUSH_SAVE_BUFFER_IF_FULL
519
395
png_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length);
523
399
#ifdef PNG_READ_iTXt_SUPPORTED
524
else if (!png_memcmp(png_ptr->chunk_name, png_iTXt, 4))
400
else if (chunk_name == png_iTXt)
526
if (png_ptr->push_length + 4 > png_ptr->buffer_size)
528
png_push_save_buffer(png_ptr);
402
PNG_PUSH_SAVE_BUFFER_IF_FULL
532
403
png_handle_iTXt(png_ptr, info_ptr, png_ptr->push_length);
538
if (png_ptr->push_length + 4 > png_ptr->buffer_size)
540
png_push_save_buffer(png_ptr);
543
png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length);
409
PNG_PUSH_SAVE_BUFFER_IF_FULL
410
png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length,
411
PNG_HANDLE_CHUNK_AS_DEFAULT);
546
414
png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
550
png_push_crc_skip(png_structp png_ptr, png_uint_32 skip)
552
png_ptr->process_mode = PNG_SKIP_MODE;
553
png_ptr->skip_length = skip;
557
png_push_crc_finish(png_structp png_ptr)
559
if (png_ptr->skip_length && png_ptr->save_buffer_size)
561
png_size_t save_size;
563
if (png_ptr->skip_length < (png_uint_32)png_ptr->save_buffer_size)
564
save_size = (png_size_t)png_ptr->skip_length;
566
save_size = png_ptr->save_buffer_size;
568
png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
570
png_ptr->skip_length -= save_size;
571
png_ptr->buffer_size -= save_size;
572
png_ptr->save_buffer_size -= save_size;
573
png_ptr->save_buffer_ptr += save_size;
575
if (png_ptr->skip_length && png_ptr->current_buffer_size)
577
png_size_t save_size;
579
if (png_ptr->skip_length < (png_uint_32)png_ptr->current_buffer_size)
580
save_size = (png_size_t)png_ptr->skip_length;
582
save_size = png_ptr->current_buffer_size;
584
png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
586
png_ptr->skip_length -= save_size;
587
png_ptr->buffer_size -= save_size;
588
png_ptr->current_buffer_size -= save_size;
589
png_ptr->current_buffer_ptr += save_size;
591
if (!png_ptr->skip_length)
593
if (png_ptr->buffer_size < 4)
595
png_push_save_buffer(png_ptr);
599
png_crc_finish(png_ptr, 0);
600
png_ptr->process_mode = PNG_READ_CHUNK_MODE;
605
418
png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length)
657
471
istop = png_ptr->save_buffer_size;
658
472
for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer;
659
i < istop; i++, sp++, dp++)
473
i < istop; i++, sp++, dp++)
665
479
if (png_ptr->save_buffer_size + png_ptr->current_buffer_size >
666
png_ptr->save_buffer_max)
480
png_ptr->save_buffer_max)
668
482
png_size_t new_max;
669
483
png_bytep old_buffer;
671
485
if (png_ptr->save_buffer_size > PNG_SIZE_MAX -
672
(png_ptr->current_buffer_size + 256))
486
(png_ptr->current_buffer_size + 256))
674
png_error(png_ptr, "Potential overflow of save_buffer");
488
png_error(png_ptr, "Potential overflow of save_buffer");
677
491
new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;
678
492
old_buffer = png_ptr->save_buffer;
679
493
png_ptr->save_buffer = (png_bytep)png_malloc_warn(png_ptr,
494
(png_size_t)new_max);
681
496
if (png_ptr->save_buffer == NULL)
683
png_free(png_ptr, old_buffer);
684
png_error(png_ptr, "Insufficient memory for save_buffer");
498
png_free(png_ptr, old_buffer);
499
png_error(png_ptr, "Insufficient memory for save_buffer");
686
png_memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
502
memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
687
503
png_free(png_ptr, old_buffer);
688
504
png_ptr->save_buffer_max = new_max;
690
506
if (png_ptr->current_buffer_size)
692
png_memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
508
memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
693
509
png_ptr->current_buffer_ptr, png_ptr->current_buffer_size);
694
510
png_ptr->save_buffer_size += png_ptr->current_buffer_size;
695
511
png_ptr->current_buffer_size = 0;
711
527
void /* PRIVATE */
712
png_push_read_IDAT(png_structp png_ptr)
528
png_push_read_IDAT(png_structrp png_ptr)
715
if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
530
if ((png_ptr->mode & PNG_HAVE_CHUNK_HEADER) == 0)
717
532
png_byte chunk_length[4];
719
if (png_ptr->buffer_size < 8)
721
png_push_save_buffer(png_ptr);
533
png_byte chunk_tag[4];
535
/* TODO: this code can be commoned up with the same code in push_read */
536
PNG_PUSH_SAVE_BUFFER_IF_LT(8)
725
537
png_push_fill_buffer(png_ptr, chunk_length, 4);
726
538
png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
727
539
png_reset_crc(png_ptr);
728
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);
729
542
png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
731
if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
544
if (png_ptr->chunk_name != png_IDAT)
733
546
png_ptr->process_mode = PNG_READ_CHUNK_MODE;
734
if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
548
if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
735
549
png_error(png_ptr, "Not enough compressed data");
739
554
png_ptr->idat_size = png_ptr->push_length;
741
if (png_ptr->idat_size && png_ptr->save_buffer_size)
557
if (png_ptr->idat_size != 0 && png_ptr->save_buffer_size != 0)
743
png_size_t save_size;
745
if (png_ptr->idat_size < (png_uint_32)png_ptr->save_buffer_size)
747
save_size = (png_size_t)png_ptr->idat_size;
749
/* Check for overflow */
750
if ((png_uint_32)save_size != png_ptr->idat_size)
751
png_error(png_ptr, "save_size overflowed in pngpread");
559
png_size_t save_size = png_ptr->save_buffer_size;
560
png_uint_32 idat_size = png_ptr->idat_size;
562
/* We want the smaller of 'idat_size' and 'current_buffer_size', but they
563
* are of different types and we don't know which variable has the fewest
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 or 64 bit platforms.
568
if (idat_size < save_size)
569
save_size = (png_size_t)idat_size;
754
save_size = png_ptr->save_buffer_size;
572
idat_size = (png_uint_32)save_size;
756
574
png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
758
576
png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size);
760
png_ptr->idat_size -= save_size;
578
png_ptr->idat_size -= idat_size;
761
579
png_ptr->buffer_size -= save_size;
762
580
png_ptr->save_buffer_size -= save_size;
763
581
png_ptr->save_buffer_ptr += save_size;
765
if (png_ptr->idat_size && png_ptr->current_buffer_size)
584
if (png_ptr->idat_size != 0 && png_ptr->current_buffer_size != 0)
767
png_size_t save_size;
769
if (png_ptr->idat_size < (png_uint_32)png_ptr->current_buffer_size)
771
save_size = (png_size_t)png_ptr->idat_size;
773
/* Check for overflow */
774
if ((png_uint_32)save_size != png_ptr->idat_size)
775
png_error(png_ptr, "save_size overflowed in pngpread");
586
png_size_t save_size = png_ptr->current_buffer_size;
587
png_uint_32 idat_size = png_ptr->idat_size;
589
/* We want the smaller of 'idat_size' and 'current_buffer_size', but they
590
* are of different types and we don't know which variable has the fewest
591
* bits. Carefully select the smaller and cast it to the type of the
592
* larger - this cannot overflow.
594
if (idat_size < save_size)
595
save_size = (png_size_t)idat_size;
778
save_size = png_ptr->current_buffer_size;
598
idat_size = (png_uint_32)save_size;
780
600
png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
782
602
png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size);
784
png_ptr->idat_size -= save_size;
604
png_ptr->idat_size -= idat_size;
785
605
png_ptr->buffer_size -= save_size;
786
606
png_ptr->current_buffer_size -= save_size;
787
607
png_ptr->current_buffer_ptr += save_size;
789
if (!png_ptr->idat_size)
610
if (png_ptr->idat_size == 0)
791
if (png_ptr->buffer_size < 4)
793
png_push_save_buffer(png_ptr);
612
PNG_PUSH_SAVE_BUFFER_IF_LT(4)
797
613
png_crc_finish(png_ptr, 0);
798
614
png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
799
615
png_ptr->mode |= PNG_AFTER_IDAT;
803
620
void /* PRIVATE */
804
png_process_IDAT_data(png_structp png_ptr, png_bytep buffer,
621
png_process_IDAT_data(png_structrp png_ptr, png_bytep buffer,
805
622
png_size_t buffer_length)
807
624
/* The caller checks for a non-zero buffer length. */
813
630
* handle the uncompressed results.
815
632
png_ptr->zstream.next_in = buffer;
633
/* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
816
634
png_ptr->zstream.avail_in = (uInt)buffer_length;
818
636
/* Keep going until the decompressed data is all processed
819
637
* or the stream marked as finished.
821
639
while (png_ptr->zstream.avail_in > 0 &&
822
!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
640
(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
826
644
/* We have data for zlib, but we must check that zlib
827
* has somewhere to put the results. It doesn't matter
645
* has someplace to put the results. It doesn't matter
828
646
* if we don't expect any results -- it may be the input
829
647
* data is just the LZ end code.
831
649
if (!(png_ptr->zstream.avail_out > 0))
833
png_ptr->zstream.avail_out =
834
(uInt) PNG_ROWBYTES(png_ptr->pixel_depth,
835
png_ptr->iwidth) + 1;
651
/* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
652
png_ptr->zstream.avail_out = (uInt)(PNG_ROWBYTES(png_ptr->pixel_depth,
653
png_ptr->iwidth) + 1);
836
655
png_ptr->zstream.next_out = png_ptr->row_buf;
839
658
/* Using Z_SYNC_FLUSH here means that an unterminated
840
* LZ stream can still be handled (a stream with a missing
841
* end code), otherwise (Z_NO_FLUSH) a future zlib
842
* implementation might defer output and, therefore,
843
* change the current behavior. (See comments in inflate.c
844
* for why this doesn't happen at present with zlib 1.2.5.)
659
* LZ stream (a stream with a missing end code) can still
660
* be handled, otherwise (Z_NO_FLUSH) a future zlib
661
* implementation might defer output and therefore
662
* change the current behavior (see comments in inflate.c
663
* for why this doesn't happen at present with zlib 1.2.5).
846
665
ret = inflate(&png_ptr->zstream, Z_SYNC_FLUSH);
898
721
* after the zlib end code.
900
723
if (png_ptr->zstream.avail_in > 0)
901
png_warning(png_ptr, "Extra compression data");
724
png_warning(png_ptr, "Extra compression data in IDAT");
904
727
void /* PRIVATE */
905
png_push_process_row(png_structp png_ptr)
728
png_push_process_row(png_structrp png_ptr)
907
png_ptr->row_info.color_type = png_ptr->color_type;
908
png_ptr->row_info.width = png_ptr->iwidth;
909
png_ptr->row_info.channels = png_ptr->channels;
910
png_ptr->row_info.bit_depth = png_ptr->bit_depth;
911
png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
913
png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
914
png_ptr->row_info.width);
916
png_read_filter_row(png_ptr, &(png_ptr->row_info),
917
png_ptr->row_buf + 1, png_ptr->prev_row + 1,
918
(int)(png_ptr->row_buf[0]));
920
png_memcpy(png_ptr->prev_row, png_ptr->row_buf, png_ptr->rowbytes + 1);
922
if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA))
923
png_do_read_transformations(png_ptr);
730
/* 1.5.6: row_info moved out of png_struct to a local here. */
731
png_row_info row_info;
733
row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */
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);
740
if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
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]);
746
png_error(png_ptr, "bad adaptive filter value");
749
/* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
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:
754
memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
756
#ifdef PNG_READ_TRANSFORMS_SUPPORTED
757
if (png_ptr->transformations != 0)
758
png_do_read_transformations(png_ptr, &row_info);
761
/* The transformed pixel depth should match the depth now in row_info. */
762
if (png_ptr->transformed_pixel_depth == 0)
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");
769
else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
770
png_error(png_ptr, "internal progressive row size calculation error");
925
773
#ifdef PNG_READ_INTERLACING_SUPPORTED
926
/* Blow up interlaced rows to full size */
927
if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
774
/* Expand interlaced rows to full size */
775
if (png_ptr->interlaced != 0 &&
776
(png_ptr->transformations & PNG_INTERLACE) != 0)
929
778
if (png_ptr->pass < 6)
930
/* old interface (pre-1.0.9):
931
png_do_read_interlace(&(png_ptr->row_info),
932
png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
934
png_do_read_interlace(png_ptr);
779
png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
780
png_ptr->transformations);
936
switch (png_ptr->pass)
782
switch (png_ptr->pass)
1111
957
void /* PRIVATE */
1112
png_read_push_finish_row(png_structp png_ptr)
958
png_read_push_finish_row(png_structrp png_ptr)
960
#ifdef PNG_READ_INTERLACING_SUPPORTED
1114
961
/* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1116
963
/* Start of interlace block */
1117
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};
1119
966
/* Offset to next interlace block */
1120
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};
1122
969
/* Start of interlace block in the y direction */
1123
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};
1125
972
/* Offset to next interlace block in the y direction */
1126
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};
1128
975
/* Height of interlace block. This is not currently used - if you need
1129
976
* it, uncomment it here and in png.h
1130
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};
1133
981
png_ptr->row_number++;
1134
982
if (png_ptr->row_number < png_ptr->num_rows)
1137
985
#ifdef PNG_READ_INTERLACING_SUPPORTED
1138
if (png_ptr->interlaced)
986
if (png_ptr->interlaced != 0)
1140
988
png_ptr->row_number = 0;
1141
png_memset(png_ptr->prev_row, 0,
1142
png_ptr->rowbytes + 1);
989
memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
1145
993
png_ptr->pass++;
1146
994
if ((png_ptr->pass == 1 && png_ptr->width < 5) ||
1147
995
(png_ptr->pass == 3 && png_ptr->width < 3) ||
1148
996
(png_ptr->pass == 5 && png_ptr->width < 2))
1151
999
if (png_ptr->pass > 7)
1152
1000
png_ptr->pass--;
1157
1005
png_ptr->iwidth = (png_ptr->width +
1158
png_pass_inc[png_ptr->pass] - 1 -
1159
png_pass_start[png_ptr->pass]) /
1160
png_pass_inc[png_ptr->pass];
1006
png_pass_inc[png_ptr->pass] - 1 -
1007
png_pass_start[png_ptr->pass]) /
1008
png_pass_inc[png_ptr->pass];
1162
if (png_ptr->transformations & PNG_INTERLACE)
1010
if ((png_ptr->transformations & PNG_INTERLACE) != 0)
1165
1013
png_ptr->num_rows = (png_ptr->height +
1166
png_pass_yinc[png_ptr->pass] - 1 -
1167
png_pass_ystart[png_ptr->pass]) /
1168
png_pass_yinc[png_ptr->pass];
1014
png_pass_yinc[png_ptr->pass] - 1 -
1015
png_pass_ystart[png_ptr->pass]) /
1016
png_pass_yinc[png_ptr->pass];
1170
1018
} while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0);
1172
#endif /* PNG_READ_INTERLACING_SUPPORTED */
1020
#endif /* READ_INTERLACING */
1175
1023
void /* PRIVATE */
1176
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)
1178
1026
if (png_ptr->info_fn != NULL)
1179
1027
(*(png_ptr->info_fn))(png_ptr, info_ptr);
1182
1030
void /* PRIVATE */
1183
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)
1185
1033
if (png_ptr->end_fn != NULL)
1186
1034
(*(png_ptr->end_fn))(png_ptr, info_ptr);
1189
1037
void /* PRIVATE */
1190
png_push_have_row(png_structp png_ptr, png_bytep row)
1038
png_push_have_row(png_structrp png_ptr, png_bytep row)
1192
1040
if (png_ptr->row_fn != NULL)
1193
1041
(*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number,
1194
1042
(int)png_ptr->pass);
1045
#ifdef PNG_READ_INTERLACING_SUPPORTED
1198
png_progressive_combine_row(png_structp png_ptr,
1199
png_bytep old_row, png_bytep new_row)
1047
png_progressive_combine_row(png_const_structrp png_ptr, png_bytep old_row,
1048
png_const_bytep new_row)
1201
PNG_CONST int FARDATA png_pass_dsp_mask[7] =
1202
{0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
1204
1050
if (png_ptr == NULL)
1207
if (new_row != NULL) /* new_row must == png_ptr->row_buf here. */
1208
png_combine_row(png_ptr, old_row, png_pass_dsp_mask[png_ptr->pass]);
1053
/* new_row is a flag here - if it is NULL then the app callback was called
1054
* from an empty row (see the calls to png_struct::row_fn below), otherwise
1055
* it must be png_ptr->row_buf+1
1057
if (new_row != NULL)
1058
png_combine_row(png_ptr, old_row, 1/*blocky display*/);
1060
#endif /* READ_INTERLACING */
1212
png_set_progressive_read_fn(png_structp png_ptr, png_voidp progressive_ptr,
1213
png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
1214
png_progressive_end_ptr end_fn)
1063
png_set_progressive_read_fn(png_structrp png_ptr, png_voidp progressive_ptr,
1064
png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
1065
png_progressive_end_ptr end_fn)
1216
1067
if (png_ptr == NULL)