pngpread.c 35.5 KB
Newer Older
G
Guy Schalnat 已提交
1 2

/* pngpread.c - read a png file in push mode
3
 *
4
 * Last changed in libpng 1.6.8 [(PENDING RELEASE)]
5
 * Copyright (c) 1998-2013 Glenn Randers-Pehrson
6 7
 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
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
12
 */
G
Guy Schalnat 已提交
13

14
#include "pngpriv.h"
G
Guy Schalnat 已提交
15

16 17
#ifdef PNG_PROGRESSIVE_READ_SUPPORTED

18
/* Push model modes */
19 20 21 22 23 24 25 26 27 28
#define PNG_READ_SIG_MODE   0
#define PNG_READ_CHUNK_MODE 1
#define PNG_READ_IDAT_MODE  2
#define PNG_SKIP_MODE       3
#define PNG_READ_tEXt_MODE  4
#define PNG_READ_zTXt_MODE  5
#define PNG_READ_DONE_MODE  6
#define PNG_READ_iTXt_MODE  7
#define PNG_ERROR_MODE      8

29
void PNGAPI
30
png_process_data(png_structrp png_ptr, png_inforp info_ptr,
31
    png_bytep buffer, png_size_t buffer_size)
G
Guy Schalnat 已提交
32
{
33 34 35
   if (png_ptr == NULL || info_ptr == NULL)
      return;

G
Guy Schalnat 已提交
36
   png_push_restore_buffer(png_ptr, buffer, buffer_size);
G
Guy Schalnat 已提交
37

G
Guy Schalnat 已提交
38 39
   while (png_ptr->buffer_size)
   {
A
Andreas Dilger 已提交
40
      png_process_some_data(png_ptr, info_ptr);
G
Guy Schalnat 已提交
41
   }
G
Guy Schalnat 已提交
42 43
}

44
png_size_t PNGAPI
45
png_process_data_pause(png_structrp png_ptr, int save)
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
{
   if (png_ptr != NULL)
   {
      /* It's easiest for the caller if we do the save, then the caller doesn't
       * have to supply the same data again:
       */
      if (save)
         png_push_save_buffer(png_ptr);
      else
      {
         /* This includes any pending saved bytes: */
         png_size_t remaining = png_ptr->buffer_size;
         png_ptr->buffer_size = 0;

         /* So subtract the saved buffer size, unless all the data
          * is actually 'saved', in which case we just return 0
          */
         if (png_ptr->save_buffer_size < remaining)
            return remaining - png_ptr->save_buffer_size;
      }
   }

   return 0;
}

png_uint_32 PNGAPI
72
png_process_data_skip(png_structrp png_ptr)
73
{
74
   png_uint_32 remaining = 0;
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

   if (png_ptr != NULL && png_ptr->process_mode == PNG_SKIP_MODE &&
      png_ptr->skip_length > 0)
   {
      /* At the end of png_process_data the buffer size must be 0 (see the loop
       * above) so we can detect a broken call here:
       */
      if (png_ptr->buffer_size != 0)
         png_error(png_ptr,
            "png_process_data_skip called inside png_process_data");

      /* If is impossible for there to be a saved buffer at this point -
       * otherwise we could not be in SKIP mode.  This will also happen if
       * png_process_skip is called inside png_process_data (but only very
       * rarely.)
       */
      if (png_ptr->save_buffer_size != 0)
         png_error(png_ptr, "png_process_data_skip called with saved data");

      remaining = png_ptr->skip_length;
      png_ptr->skip_length = 0;
      png_ptr->process_mode = PNG_READ_CHUNK_MODE;
   }

   return remaining;
}

A
Andreas Dilger 已提交
102 103 104
/* What we do with the incoming data depends on what we were previously
 * doing before we ran out of data...
 */
105
void /* PRIVATE */
106
png_process_some_data(png_structrp png_ptr, png_inforp info_ptr)
G
Guy Schalnat 已提交
107
{
108 109 110
   if (png_ptr == NULL)
      return;

G
Guy Schalnat 已提交
111 112 113 114
   switch (png_ptr->process_mode)
   {
      case PNG_READ_SIG_MODE:
      {
A
Andreas Dilger 已提交
115
         png_push_read_sig(png_ptr, info_ptr);
G
Guy Schalnat 已提交
116 117
         break;
      }
118

G
Guy Schalnat 已提交
119 120
      case PNG_READ_CHUNK_MODE:
      {
A
Andreas Dilger 已提交
121
         png_push_read_chunk(png_ptr, info_ptr);
G
Guy Schalnat 已提交
122 123
         break;
      }
124

G
Guy Schalnat 已提交
125 126
      case PNG_READ_IDAT_MODE:
      {
G
Guy Schalnat 已提交
127
         png_push_read_IDAT(png_ptr);
G
Guy Schalnat 已提交
128 129
         break;
      }
130

G
Guy Schalnat 已提交
131 132
      case PNG_SKIP_MODE:
      {
A
Andreas Dilger 已提交
133
         png_push_crc_finish(png_ptr);
G
Guy Schalnat 已提交
134 135
         break;
      }
136

G
Guy Schalnat 已提交
137 138 139 140 141 142
      default:
      {
         png_ptr->buffer_size = 0;
         break;
      }
   }
G
Guy Schalnat 已提交
143 144
}

A
Andreas Dilger 已提交
145 146
/* Read any remaining signature bytes from the stream and compare them with
 * the correct PNG signature.  It is possible that this routine is called
147 148 149
 * with bytes already read from the signature, either because they have been
 * checked by the calling application, or because of multiple calls to this
 * routine.
A
Andreas Dilger 已提交
150
 */
151
void /* PRIVATE */
152
png_push_read_sig(png_structrp png_ptr, png_inforp info_ptr)
G
Guy Schalnat 已提交
153
{
154
   png_size_t num_checked = png_ptr->sig_bytes, /* SAFE, does not exceed 8 */ 
A
Andreas Dilger 已提交
155
             num_to_check = 8 - num_checked;
G
Guy Schalnat 已提交
156

A
Andreas Dilger 已提交
157
   if (png_ptr->buffer_size < num_to_check)
G
Guy Schalnat 已提交
158
   {
A
Andreas Dilger 已提交
159
      num_to_check = png_ptr->buffer_size;
G
Guy Schalnat 已提交
160 161
   }

162
   png_push_fill_buffer(png_ptr, &(info_ptr->signature[num_checked]),
163
       num_to_check);
164
   png_ptr->sig_bytes = (png_byte)(png_ptr->sig_bytes + num_to_check);
G
Guy Schalnat 已提交
165

A
Andreas Dilger 已提交
166
   if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
G
Guy Schalnat 已提交
167
   {
A
Andreas Dilger 已提交
168 169 170
      if (num_checked < 4 &&
          png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
         png_error(png_ptr, "Not a PNG file");
171

A
Andreas Dilger 已提交
172 173
      else
         png_error(png_ptr, "PNG file corrupted by ASCII conversion");
G
Guy Schalnat 已提交
174 175 176
   }
   else
   {
A
Andreas Dilger 已提交
177 178 179 180
      if (png_ptr->sig_bytes >= 8)
      {
         png_ptr->process_mode = PNG_READ_CHUNK_MODE;
      }
G
Guy Schalnat 已提交
181
   }
G
Guy Schalnat 已提交
182 183
}

184
void /* PRIVATE */
185
png_push_read_chunk(png_structrp png_ptr, png_inforp info_ptr)
G
Guy Schalnat 已提交
186
{
187
   png_uint_32 chunk_name;
188 189 190
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
   int keep; /* unknown handling method */
#endif
191

A
Andreas Dilger 已提交
192
   /* First we make sure we have enough data for the 4 byte chunk name
193 194 195 196 197
    * and the 4 byte chunk length before proceeding with decoding the
    * chunk data.  To fully decode each of these chunks, we also make
    * sure we have enough data in the buffer for the 4 byte CRC at the
    * end of every chunk (except IDAT, which is handled separately).
    */
198
   if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
G
Guy Schalnat 已提交
199
   {
A
Andreas Dilger 已提交
200
      png_byte chunk_length[4];
201
      png_byte chunk_tag[4];
G
Guy Schalnat 已提交
202 203 204 205 206 207 208

      if (png_ptr->buffer_size < 8)
      {
         png_push_save_buffer(png_ptr);
         return;
      }

A
Andreas Dilger 已提交
209
      png_push_fill_buffer(png_ptr, chunk_length, 4);
210
      png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
G
Guy Schalnat 已提交
211
      png_reset_crc(png_ptr);
212 213
      png_crc_read(png_ptr, chunk_tag, 4);
      png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(chunk_tag);
214
      png_check_chunk_name(png_ptr, png_ptr->chunk_name);
215
      png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
G
Guy Schalnat 已提交
216 217
   }

218 219 220
   chunk_name = png_ptr->chunk_name;

   if (chunk_name == png_IDAT)
221
   {
222
      if (png_ptr->mode & PNG_AFTER_IDAT)
223
         png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243

      /* If we reach an IDAT chunk, this means we have read all of the
       * header chunks, and we can start reading the image (or if this
       * is called after the image has been read - we have an error).
       */
      if (!(png_ptr->mode & PNG_HAVE_IHDR))
         png_error(png_ptr, "Missing IHDR before IDAT");

      else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
          !(png_ptr->mode & PNG_HAVE_PLTE))
         png_error(png_ptr, "Missing PLTE before IDAT");

      png_ptr->mode |= PNG_HAVE_IDAT;

      if (!(png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
         if (png_ptr->push_length == 0)
            return;

      if (png_ptr->mode & PNG_AFTER_IDAT)
         png_benign_error(png_ptr, "Too many IDATs found");
244
   }
245

246
   if (chunk_name == png_IHDR)
G
Guy Schalnat 已提交
247
   {
248 249
      if (png_ptr->push_length != 13)
         png_error(png_ptr, "Invalid IHDR length");
250

251 252 253
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
G
Guy Schalnat 已提交
254
         return;
255
      }
256

A
Andreas Dilger 已提交
257
      png_handle_IHDR(png_ptr, info_ptr, png_ptr->push_length);
G
Guy Schalnat 已提交
258
   }
259

260
   else if (chunk_name == png_IEND)
261
   {
262 263 264
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
265
         return;
266
      }
267

268 269 270 271 272
      png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length);

      png_ptr->process_mode = PNG_READ_DONE_MODE;
      png_push_have_end(png_ptr, info_ptr);
   }
273

274
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
275
   else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
276
   {
277 278 279
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
280
         return;
281
      }
282

283
      png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length, keep);
284

285
      if (chunk_name == png_PLTE)
286 287
         png_ptr->mode |= PNG_HAVE_PLTE;
   }
288

289
#endif
290
   else if (chunk_name == png_PLTE)
G
Guy Schalnat 已提交
291
   {
292 293 294
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
A
Andreas Dilger 已提交
295
         return;
296
      }
A
Andreas Dilger 已提交
297
      png_handle_PLTE(png_ptr, info_ptr, png_ptr->push_length);
G
Guy Schalnat 已提交
298
   }
299

300
   else if (chunk_name == png_IDAT)
G
Guy Schalnat 已提交
301 302 303
   {
      png_ptr->idat_size = png_ptr->push_length;
      png_ptr->process_mode = PNG_READ_IDAT_MODE;
A
Andreas Dilger 已提交
304
      png_push_have_info(png_ptr, info_ptr);
305 306 307
      png_ptr->zstream.avail_out =
          (uInt) PNG_ROWBYTES(png_ptr->pixel_depth,
          png_ptr->iwidth) + 1;
A
Andreas Dilger 已提交
308
      png_ptr->zstream.next_out = png_ptr->row_buf;
G
Guy Schalnat 已提交
309 310
      return;
   }
311

312
#ifdef PNG_READ_gAMA_SUPPORTED
313
   else if (png_ptr->chunk_name == png_gAMA)
G
Guy Schalnat 已提交
314
   {
315 316 317
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
G
Guy Schalnat 已提交
318
         return;
319
      }
320

A
Andreas Dilger 已提交
321
      png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length);
G
Guy Schalnat 已提交
322
   }
323

G
Guy Schalnat 已提交
324
#endif
325
#ifdef PNG_READ_sBIT_SUPPORTED
326
   else if (png_ptr->chunk_name == png_sBIT)
G
Guy Schalnat 已提交
327
   {
328 329 330
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
G
Guy Schalnat 已提交
331
         return;
332
      }
333

A
Andreas Dilger 已提交
334
      png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length);
G
Guy Schalnat 已提交
335
   }
336

G
Guy Schalnat 已提交
337
#endif
338
#ifdef PNG_READ_cHRM_SUPPORTED
339
   else if (png_ptr->chunk_name == png_cHRM)
G
Guy Schalnat 已提交
340
   {
341 342 343
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
G
Guy Schalnat 已提交
344
         return;
345
      }
346

A
Andreas Dilger 已提交
347
      png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length);
G
Guy Schalnat 已提交
348
   }
349

G
Guy Schalnat 已提交
350
#endif
351
#ifdef PNG_READ_sRGB_SUPPORTED
352
   else if (chunk_name == png_sRGB)
353
   {
354 355 356
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
357
         return;
358
      }
359

360 361
      png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length);
   }
362

363
#endif
364
#ifdef PNG_READ_iCCP_SUPPORTED
365
   else if (png_ptr->chunk_name == png_iCCP)
366
   {
367 368 369
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
370
         return;
371
      }
372

373 374
      png_handle_iCCP(png_ptr, info_ptr, png_ptr->push_length);
   }
375

376
#endif
377
#ifdef PNG_READ_sPLT_SUPPORTED
378
   else if (chunk_name == png_sPLT)
379
   {
380 381 382
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
383
         return;
384
      }
385

386 387
      png_handle_sPLT(png_ptr, info_ptr, png_ptr->push_length);
   }
388

389
#endif
390
#ifdef PNG_READ_tRNS_SUPPORTED
391
   else if (chunk_name == png_tRNS)
G
Guy Schalnat 已提交
392
   {
393 394 395
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
G
Guy Schalnat 已提交
396
         return;
397
      }
398

A
Andreas Dilger 已提交
399
      png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length);
G
Guy Schalnat 已提交
400
   }
401

G
Guy Schalnat 已提交
402
#endif
403
#ifdef PNG_READ_bKGD_SUPPORTED
404
   else if (chunk_name == png_bKGD)
G
Guy Schalnat 已提交
405
   {
406 407 408
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
G
Guy Schalnat 已提交
409
         return;
410
      }
411

A
Andreas Dilger 已提交
412
      png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length);
G
Guy Schalnat 已提交
413
   }
414

G
Guy Schalnat 已提交
415
#endif
416
#ifdef PNG_READ_hIST_SUPPORTED
417
   else if (chunk_name == png_hIST)
G
Guy Schalnat 已提交
418
   {
419 420 421
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
G
Guy Schalnat 已提交
422
         return;
423
      }
424

A
Andreas Dilger 已提交
425
      png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length);
G
Guy Schalnat 已提交
426
   }
427

G
Guy Schalnat 已提交
428
#endif
429
#ifdef PNG_READ_pHYs_SUPPORTED
430
   else if (chunk_name == png_pHYs)
G
Guy Schalnat 已提交
431 432 433 434 435 436
   {
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
         return;
      }
437

A
Andreas Dilger 已提交
438
      png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length);
G
Guy Schalnat 已提交
439
   }
440

G
Guy Schalnat 已提交
441
#endif
442
#ifdef PNG_READ_oFFs_SUPPORTED
443
   else if (chunk_name == png_oFFs)
G
Guy Schalnat 已提交
444
   {
445 446 447
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
G
Guy Schalnat 已提交
448
         return;
449
      }
450

A
Andreas Dilger 已提交
451
      png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length);
G
Guy Schalnat 已提交
452
   }
G
Guy Schalnat 已提交
453
#endif
454

455
#ifdef PNG_READ_pCAL_SUPPORTED
456
   else if (chunk_name == png_pCAL)
A
Andreas Dilger 已提交
457
   {
458 459 460
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
A
Andreas Dilger 已提交
461
         return;
462
      }
463

A
Andreas Dilger 已提交
464 465
      png_handle_pCAL(png_ptr, info_ptr, png_ptr->push_length);
   }
466

A
Andreas Dilger 已提交
467
#endif
468
#ifdef PNG_READ_sCAL_SUPPORTED
469
   else if (chunk_name == png_sCAL)
470
   {
471 472 473
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
474
         return;
475
      }
476

477 478
      png_handle_sCAL(png_ptr, info_ptr, png_ptr->push_length);
   }
479

480
#endif
481
#ifdef PNG_READ_tIME_SUPPORTED
482
   else if (chunk_name == png_tIME)
G
Guy Schalnat 已提交
483
   {
484 485 486
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
G
Guy Schalnat 已提交
487
         return;
488
      }
489

A
Andreas Dilger 已提交
490
      png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length);
G
Guy Schalnat 已提交
491
   }
492

G
Guy Schalnat 已提交
493
#endif
494
#ifdef PNG_READ_tEXt_SUPPORTED
495
   else if (chunk_name == png_tEXt)
G
Guy Schalnat 已提交
496
   {
497 498 499
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
500
         return;
501
      }
502

503
      png_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length);
G
Guy Schalnat 已提交
504
   }
505

G
Guy Schalnat 已提交
506
#endif
507
#ifdef PNG_READ_zTXt_SUPPORTED
508
   else if (chunk_name == png_zTXt)
G
Guy Schalnat 已提交
509
   {
510 511 512
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
513
         return;
514
      }
515

516
      png_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length);
G
Guy Schalnat 已提交
517
   }
518

519
#endif
520
#ifdef PNG_READ_iTXt_SUPPORTED
521
   else if (chunk_name == png_iTXt)
522
   {
523 524 525
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
526
         return;
527
      }
528

529
      png_handle_iTXt(png_ptr, info_ptr, png_ptr->push_length);
530
   }
G
Guy Schalnat 已提交
531
#endif
532 533

#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
G
Guy Schalnat 已提交
534 535
   else
   {
536 537 538
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
539
         return;
540
      }
541 542
      png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length,
         PNG_HANDLE_CHUNK_AS_DEFAULT);
G
Guy Schalnat 已提交
543
   }
544
#endif
G
Guy Schalnat 已提交
545

546
   png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
G
Guy Schalnat 已提交
547 548
}

549
void /* PRIVATE */
550
png_push_crc_skip(png_structrp png_ptr, png_uint_32 skip)
G
Guy Schalnat 已提交
551
{
G
Guy Schalnat 已提交
552
   png_ptr->process_mode = PNG_SKIP_MODE;
A
Andreas Dilger 已提交
553
   png_ptr->skip_length = skip;
G
Guy Schalnat 已提交
554 555
}

556
void /* PRIVATE */
557
png_push_crc_finish(png_structrp png_ptr)
G
Guy Schalnat 已提交
558
{
G
Guy Schalnat 已提交
559 560
   if (png_ptr->skip_length && png_ptr->save_buffer_size)
   {
561
      png_size_t save_size = png_ptr->save_buffer_size;
562
      png_uint_32 skip_length = png_ptr->skip_length;
G
Guy Schalnat 已提交
563

564
      /* We want the smaller of 'skip_length' and 'save_buffer_size', but
565 566 567 568 569 570 571
       * they are of different types and we don't know which variable has the
       * fewest bits.  Carefully select the smaller and cast it to the type of
       * the larger - this cannot overflow.  Do not cast in the following test
       * - it will break on either 16 or 64 bit platforms.
       */
      if (skip_length < save_size)
         save_size = (png_size_t)skip_length;
572

G
Guy Schalnat 已提交
573
      else
574
         skip_length = (png_uint_32)save_size;
G
Guy Schalnat 已提交
575 576 577

      png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);

578
      png_ptr->skip_length -= skip_length;
G
Guy Schalnat 已提交
579 580
      png_ptr->buffer_size -= save_size;
      png_ptr->save_buffer_size -= save_size;
A
Andreas Dilger 已提交
581
      png_ptr->save_buffer_ptr += save_size;
G
Guy Schalnat 已提交
582 583 584
   }
   if (png_ptr->skip_length && png_ptr->current_buffer_size)
   {
585 586
      png_size_t save_size = png_ptr->current_buffer_size;
      png_uint_32 skip_length = png_ptr->skip_length;
G
Guy Schalnat 已提交
587

588 589
      /* We want the smaller of 'skip_length' and 'current_buffer_size', here,
       * the same problem exists as above and the same solution.
590 591 592
       */
      if (skip_length < save_size)
         save_size = (png_size_t)skip_length;
593

G
Guy Schalnat 已提交
594
      else
595
         skip_length = (png_uint_32)save_size;
G
Guy Schalnat 已提交
596 597 598

      png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);

599
      png_ptr->skip_length -= skip_length;
G
Guy Schalnat 已提交
600 601
      png_ptr->buffer_size -= save_size;
      png_ptr->current_buffer_size -= save_size;
A
Andreas Dilger 已提交
602
      png_ptr->current_buffer_ptr += save_size;
G
Guy Schalnat 已提交
603 604 605 606 607 608 609 610
   }
   if (!png_ptr->skip_length)
   {
      if (png_ptr->buffer_size < 4)
      {
         png_push_save_buffer(png_ptr);
         return;
      }
A
Andreas Dilger 已提交
611 612

      png_crc_finish(png_ptr, 0);
A
Andreas Dilger 已提交
613
      png_ptr->process_mode = PNG_READ_CHUNK_MODE;
G
Guy Schalnat 已提交
614
   }
G
Guy Schalnat 已提交
615 616
}

617
void PNGCBAPI
A
Andreas Dilger 已提交
618
png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length)
G
Guy Schalnat 已提交
619
{
G
Guy Schalnat 已提交
620 621
   png_bytep ptr;

622 623 624
   if (png_ptr == NULL)
      return;

G
Guy Schalnat 已提交
625 626 627
   ptr = buffer;
   if (png_ptr->save_buffer_size)
   {
A
Andreas Dilger 已提交
628
      png_size_t save_size;
G
Guy Schalnat 已提交
629 630 631

      if (length < png_ptr->save_buffer_size)
         save_size = length;
632

G
Guy Schalnat 已提交
633 634 635
      else
         save_size = png_ptr->save_buffer_size;

636
      memcpy(ptr, png_ptr->save_buffer_ptr, save_size);
G
Guy Schalnat 已提交
637
      length -= save_size;
A
Andreas Dilger 已提交
638
      ptr += save_size;
G
Guy Schalnat 已提交
639 640
      png_ptr->buffer_size -= save_size;
      png_ptr->save_buffer_size -= save_size;
A
Andreas Dilger 已提交
641
      png_ptr->save_buffer_ptr += save_size;
G
Guy Schalnat 已提交
642 643 644
   }
   if (length && png_ptr->current_buffer_size)
   {
A
Andreas Dilger 已提交
645
      png_size_t save_size;
G
Guy Schalnat 已提交
646 647 648

      if (length < png_ptr->current_buffer_size)
         save_size = length;
649

G
Guy Schalnat 已提交
650 651 652
      else
         save_size = png_ptr->current_buffer_size;

653
      memcpy(ptr, png_ptr->current_buffer_ptr, save_size);
G
Guy Schalnat 已提交
654 655
      png_ptr->buffer_size -= save_size;
      png_ptr->current_buffer_size -= save_size;
A
Andreas Dilger 已提交
656
      png_ptr->current_buffer_ptr += save_size;
G
Guy Schalnat 已提交
657
   }
G
Guy Schalnat 已提交
658 659
}

660
void /* PRIVATE */
661
png_push_save_buffer(png_structrp png_ptr)
G
Guy Schalnat 已提交
662
{
G
Guy Schalnat 已提交
663 664 665 666
   if (png_ptr->save_buffer_size)
   {
      if (png_ptr->save_buffer_ptr != png_ptr->save_buffer)
      {
667
         png_size_t i, istop;
G
Guy Schalnat 已提交
668 669 670
         png_bytep sp;
         png_bytep dp;

671
         istop = png_ptr->save_buffer_size;
G
Guy Schalnat 已提交
672
         for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer;
673
             i < istop; i++, sp++, dp++)
G
Guy Schalnat 已提交
674 675 676 677 678
         {
            *dp = *sp;
         }
      }
   }
679
   if (png_ptr->save_buffer_size + png_ptr->current_buffer_size >
680
       png_ptr->save_buffer_max)
G
Guy Schalnat 已提交
681
   {
A
Andreas Dilger 已提交
682
      png_size_t new_max;
G
Guy Schalnat 已提交
683 684
      png_bytep old_buffer;

685
      if (png_ptr->save_buffer_size > PNG_SIZE_MAX -
686
          (png_ptr->current_buffer_size + 256))
687
      {
688
         png_error(png_ptr, "Potential overflow of save_buffer");
689
      }
690

691
      new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;
G
Guy Schalnat 已提交
692
      old_buffer = png_ptr->save_buffer;
693
      png_ptr->save_buffer = (png_bytep)png_malloc_warn(png_ptr,
694
          (png_size_t)new_max);
695

696 697
      if (png_ptr->save_buffer == NULL)
      {
698 699
         png_free(png_ptr, old_buffer);
         png_error(png_ptr, "Insufficient memory for save_buffer");
700
      }
701

702
      memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
703 704
      png_free(png_ptr, old_buffer);
      png_ptr->save_buffer_max = new_max;
G
Guy Schalnat 已提交
705 706 707
   }
   if (png_ptr->current_buffer_size)
   {
708
      memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
A
Andreas Dilger 已提交
709
         png_ptr->current_buffer_ptr, png_ptr->current_buffer_size);
G
Guy Schalnat 已提交
710 711 712 713 714
      png_ptr->save_buffer_size += png_ptr->current_buffer_size;
      png_ptr->current_buffer_size = 0;
   }
   png_ptr->save_buffer_ptr = png_ptr->save_buffer;
   png_ptr->buffer_size = 0;
G
Guy Schalnat 已提交
715 716
}

717
void /* PRIVATE */
718
png_push_restore_buffer(png_structrp png_ptr, png_bytep buffer,
A
Andreas Dilger 已提交
719
   png_size_t buffer_length)
G
Guy Schalnat 已提交
720
{
G
Guy Schalnat 已提交
721 722 723 724
   png_ptr->current_buffer = buffer;
   png_ptr->current_buffer_size = buffer_length;
   png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size;
   png_ptr->current_buffer_ptr = png_ptr->current_buffer;
G
Guy Schalnat 已提交
725 726
}

727
void /* PRIVATE */
728
png_push_read_IDAT(png_structrp png_ptr)
G
Guy Schalnat 已提交
729
{
730
   if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
G
Guy Schalnat 已提交
731
   {
A
Andreas Dilger 已提交
732
      png_byte chunk_length[4];
733
      png_byte chunk_tag[4];
G
Guy Schalnat 已提交
734

735
      /* TODO: this code can be commoned up with the same code in push_read */
G
Guy Schalnat 已提交
736 737 738 739 740 741
      if (png_ptr->buffer_size < 8)
      {
         png_push_save_buffer(png_ptr);
         return;
      }

A
Andreas Dilger 已提交
742
      png_push_fill_buffer(png_ptr, chunk_length, 4);
743
      png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
G
Guy Schalnat 已提交
744
      png_reset_crc(png_ptr);
745 746
      png_crc_read(png_ptr, chunk_tag, 4);
      png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(chunk_tag);
747
      png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
A
Andreas Dilger 已提交
748

749
      if (png_ptr->chunk_name != png_IDAT)
G
Guy Schalnat 已提交
750
      {
A
Andreas Dilger 已提交
751
         png_ptr->process_mode = PNG_READ_CHUNK_MODE;
752

753
         if (!(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED))
G
Guy Schalnat 已提交
754
            png_error(png_ptr, "Not enough compressed data");
755

G
Guy Schalnat 已提交
756 757 758 759 760
         return;
      }

      png_ptr->idat_size = png_ptr->push_length;
   }
761

G
Guy Schalnat 已提交
762 763
   if (png_ptr->idat_size && png_ptr->save_buffer_size)
   {
764 765
      png_size_t save_size = png_ptr->save_buffer_size;
      png_uint_32 idat_size = png_ptr->idat_size;
766

767 768 769 770 771 772 773 774
      /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
       * are of different types and we don't know which variable has the fewest
       * bits.  Carefully select the smaller and cast it to the type of the
       * larger - this cannot overflow.  Do not cast in the following test - it
       * will break on either 16 or 64 bit platforms.
       */
      if (idat_size < save_size)
         save_size = (png_size_t)idat_size;
775

G
Guy Schalnat 已提交
776
      else
777
         idat_size = (png_uint_32)save_size;
G
Guy Schalnat 已提交
778 779

      png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
780

781
      png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size);
782

783
      png_ptr->idat_size -= idat_size;
G
Guy Schalnat 已提交
784 785
      png_ptr->buffer_size -= save_size;
      png_ptr->save_buffer_size -= save_size;
A
Andreas Dilger 已提交
786
      png_ptr->save_buffer_ptr += save_size;
G
Guy Schalnat 已提交
787
   }
788

G
Guy Schalnat 已提交
789 790
   if (png_ptr->idat_size && png_ptr->current_buffer_size)
   {
791 792
      png_size_t save_size = png_ptr->current_buffer_size;
      png_uint_32 idat_size = png_ptr->idat_size;
G
Guy Schalnat 已提交
793

794 795 796 797 798 799 800
      /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
       * are of different types and we don't know which variable has the fewest
       * bits.  Carefully select the smaller and cast it to the type of the
       * larger - this cannot overflow.
       */
      if (idat_size < save_size)
         save_size = (png_size_t)idat_size;
801

G
Guy Schalnat 已提交
802
      else
803
         idat_size = (png_uint_32)save_size;
G
Guy Schalnat 已提交
804 805

      png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
806

807
      png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size);
G
Guy Schalnat 已提交
808

809
      png_ptr->idat_size -= idat_size;
G
Guy Schalnat 已提交
810 811
      png_ptr->buffer_size -= save_size;
      png_ptr->current_buffer_size -= save_size;
A
Andreas Dilger 已提交
812
      png_ptr->current_buffer_ptr += save_size;
G
Guy Schalnat 已提交
813 814 815 816 817 818 819 820 821
   }
   if (!png_ptr->idat_size)
   {
      if (png_ptr->buffer_size < 4)
      {
         png_push_save_buffer(png_ptr);
         return;
      }

A
Andreas Dilger 已提交
822
      png_crc_finish(png_ptr, 0);
823
      png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
824
      png_ptr->mode |= PNG_AFTER_IDAT;
825
      png_ptr->zowner = 0;
G
Guy Schalnat 已提交
826
   }
G
Guy Schalnat 已提交
827 828
}

829
void /* PRIVATE */
830
png_process_IDAT_data(png_structrp png_ptr, png_bytep buffer,
A
Andreas Dilger 已提交
831
   png_size_t buffer_length)
G
Guy Schalnat 已提交
832
{
833
   /* The caller checks for a non-zero buffer length. */
834
   if (!(buffer_length > 0) || buffer == NULL)
835
      png_error(png_ptr, "No IDAT data (internal error)");
G
Guy Schalnat 已提交
836

837 838
   /* This routine must process all the data it has been given
    * before returning, calling the row callback as required to
839
    * handle the uncompressed results.
840
    */
A
Andreas Dilger 已提交
841
   png_ptr->zstream.next_in = buffer;
842
   /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
A
Andreas Dilger 已提交
843
   png_ptr->zstream.avail_in = (uInt)buffer_length;
844 845 846 847 848

   /* Keep going until the decompressed data is all processed
    * or the stream marked as finished.
    */
   while (png_ptr->zstream.avail_in > 0 &&
849
      !(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED))
G
Guy Schalnat 已提交
850
   {
851 852 853
      int ret;

      /* We have data for zlib, but we must check that zlib
854
       * has someplace to put the results.  It doesn't matter
855
       * if we don't expect any results -- it may be the input
856 857 858
       * data is just the LZ end code.
       */
      if (!(png_ptr->zstream.avail_out > 0))
G
Guy Schalnat 已提交
859
      {
860 861 862
         /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
         png_ptr->zstream.avail_out = (uInt)(PNG_ROWBYTES(png_ptr->pixel_depth,
             png_ptr->iwidth) + 1);
863

864 865 866 867
         png_ptr->zstream.next_out = png_ptr->row_buf;
      }

      /* Using Z_SYNC_FLUSH here means that an unterminated
868 869 870 871 872
       * LZ stream (a stream with a missing end code) can still
       * be handled, otherwise (Z_NO_FLUSH) a future zlib
       * implementation might defer output and therefore
       * change the current behavior (see comments in inflate.c
       * for why this doesn't happen at present with zlib 1.2.5).
873 874
       */
      ret = inflate(&png_ptr->zstream, Z_SYNC_FLUSH);
875

876 877 878
      /* Check for any failure before proceeding. */
      if (ret != Z_OK && ret != Z_STREAM_END)
      {
879
         /* Terminate the decompression. */
880 881
         png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
         png_ptr->zowner = 0;
882

883
         /* This may be a truncated stream (missing or
884 885
          * damaged end code).  Treat that as a warning.
          */
886
         if (png_ptr->row_number >= png_ptr->num_rows ||
887 888
             png_ptr->pass > 6)
            png_warning(png_ptr, "Truncated compressed data in IDAT");
889

890 891
         else
            png_error(png_ptr, "Decompression error in IDAT");
892

893
         /* Skip the check on unprocessed input */
894
         return;
G
Guy Schalnat 已提交
895
      }
896 897 898

      /* Did inflate output any data? */
      if (png_ptr->zstream.next_out != png_ptr->row_buf)
G
Guy Schalnat 已提交
899
      {
900 901 902 903
         /* Is this unexpected data after the last row?
          * If it is, artificially terminate the LZ output
          * here.
          */
904
         if (png_ptr->row_number >= png_ptr->num_rows ||
905
             png_ptr->pass > 6)
906
         {
907 908
            /* Extra data. */
            png_warning(png_ptr, "Extra compressed data in IDAT");
909 910
            png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
            png_ptr->zowner = 0;
911

912 913 914
            /* Do no more processing; skip the unprocessed
             * input check below.
             */
915
            return;
916
         }
917

918 919 920
         /* Do we have a complete row? */
         if (png_ptr->zstream.avail_out == 0)
            png_push_process_row(png_ptr);
G
Guy Schalnat 已提交
921
      }
922 923 924

      /* And check for the end of the stream. */
      if (ret == Z_STREAM_END)
925
         png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
A
Andreas Dilger 已提交
926
   }
927 928 929 930 931 932

   /* All the data should have been processed, if anything
    * is left at this point we have bytes of IDAT data
    * after the zlib end code.
    */
   if (png_ptr->zstream.avail_in > 0)
933
      png_warning(png_ptr, "Extra compression data in IDAT");
G
Guy Schalnat 已提交
934 935
}

936
void /* PRIVATE */
937
png_push_process_row(png_structrp png_ptr)
G
Guy Schalnat 已提交
938
{
939 940
   /* 1.5.6: row_info moved out of png_struct to a local here. */
   png_row_info row_info;
941

942 943 944 945 946 947
   row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */
   row_info.color_type = png_ptr->color_type;
   row_info.bit_depth = png_ptr->bit_depth;
   row_info.channels = png_ptr->channels;
   row_info.pixel_depth = png_ptr->pixel_depth;
   row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
G
Guy Schalnat 已提交
948

949 950 951
   if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
   {
      if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
952
         png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1,
953 954 955 956
            png_ptr->prev_row + 1, png_ptr->row_buf[0]);
      else
         png_error(png_ptr, "bad adaptive filter value");
   }
G
Guy Schalnat 已提交
957

958 959 960 961 962
   /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
    * 1.5.6, while the buffer really is this big in current versions of libpng
    * it may not be in the future, so this was changed just to copy the
    * interlaced row count:
    */
963
   memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
G
Guy Schalnat 已提交
964

965
#ifdef PNG_READ_TRANSFORMS_SUPPORTED
966
   if (png_ptr->transformations)
967
      png_do_read_transformations(png_ptr, &row_info);
968
#endif
G
Guy Schalnat 已提交
969

970 971 972 973 974 975 976 977 978 979 980 981
   /* The transformed pixel depth should match the depth now in row_info. */
   if (png_ptr->transformed_pixel_depth == 0)
   {
      png_ptr->transformed_pixel_depth = row_info.pixel_depth;
      if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
         png_error(png_ptr, "progressive row overflow");
   }

   else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
      png_error(png_ptr, "internal progressive row size calculation error");


982
#ifdef PNG_READ_INTERLACING_SUPPORTED
983
   /* Blow up interlaced rows to full size */
A
Andreas Dilger 已提交
984
   if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
G
Guy Schalnat 已提交
985 986
   {
      if (png_ptr->pass < 6)
987 988
         png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
            png_ptr->transformations);
G
Guy Schalnat 已提交
989

990 991
    switch (png_ptr->pass)
    {
G
Guy Schalnat 已提交
992 993 994 995 996 997
         case 0:
         {
            int i;
            for (i = 0; i < 8 && png_ptr->pass == 0; i++)
            {
               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
998
               png_read_push_finish_row(png_ptr); /* Updates png_ptr->pass */
G
Guy Schalnat 已提交
999
            }
1000

1001
            if (png_ptr->pass == 2) /* Pass 1 might be empty */
1002 1003 1004
            {
               for (i = 0; i < 4 && png_ptr->pass == 2; i++)
               {
1005
                  png_push_have_row(png_ptr, NULL);
1006 1007 1008
                  png_read_push_finish_row(png_ptr);
               }
            }
1009

1010 1011 1012 1013
            if (png_ptr->pass == 4 && png_ptr->height <= 4)
            {
               for (i = 0; i < 2 && png_ptr->pass == 4; i++)
               {
1014
                  png_push_have_row(png_ptr, NULL);
1015 1016
                  png_read_push_finish_row(png_ptr);
               }
1017
            }
1018

1019 1020
            if (png_ptr->pass == 6 && png_ptr->height <= 4)
            {
1021
                png_push_have_row(png_ptr, NULL);
1022 1023
                png_read_push_finish_row(png_ptr);
            }
1024

G
Guy Schalnat 已提交
1025 1026
            break;
         }
1027

G
Guy Schalnat 已提交
1028 1029 1030 1031 1032 1033 1034 1035
         case 1:
         {
            int i;
            for (i = 0; i < 8 && png_ptr->pass == 1; i++)
            {
               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
               png_read_push_finish_row(png_ptr);
            }
1036

1037
            if (png_ptr->pass == 2) /* Skip top 4 generated rows */
G
Guy Schalnat 已提交
1038 1039 1040
            {
               for (i = 0; i < 4 && png_ptr->pass == 2; i++)
               {
1041
                  png_push_have_row(png_ptr, NULL);
G
Guy Schalnat 已提交
1042 1043 1044
                  png_read_push_finish_row(png_ptr);
               }
            }
1045

G
Guy Schalnat 已提交
1046 1047
            break;
         }
1048

G
Guy Schalnat 已提交
1049 1050 1051
         case 2:
         {
            int i;
1052

G
Guy Schalnat 已提交
1053 1054 1055 1056 1057
            for (i = 0; i < 4 && png_ptr->pass == 2; i++)
            {
               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
               png_read_push_finish_row(png_ptr);
            }
1058

G
Guy Schalnat 已提交
1059 1060
            for (i = 0; i < 4 && png_ptr->pass == 2; i++)
            {
1061
               png_push_have_row(png_ptr, NULL);
G
Guy Schalnat 已提交
1062 1063
               png_read_push_finish_row(png_ptr);
            }
1064

1065
            if (png_ptr->pass == 4) /* Pass 3 might be empty */
1066 1067 1068
            {
               for (i = 0; i < 2 && png_ptr->pass == 4; i++)
               {
1069
                  png_push_have_row(png_ptr, NULL);
1070 1071 1072
                  png_read_push_finish_row(png_ptr);
               }
            }
1073

G
Guy Schalnat 已提交
1074 1075
            break;
         }
1076

G
Guy Schalnat 已提交
1077 1078 1079
         case 3:
         {
            int i;
1080

G
Guy Schalnat 已提交
1081 1082 1083 1084 1085
            for (i = 0; i < 4 && png_ptr->pass == 3; i++)
            {
               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
               png_read_push_finish_row(png_ptr);
            }
1086

1087
            if (png_ptr->pass == 4) /* Skip top two generated rows */
G
Guy Schalnat 已提交
1088 1089 1090
            {
               for (i = 0; i < 2 && png_ptr->pass == 4; i++)
               {
1091
                  png_push_have_row(png_ptr, NULL);
G
Guy Schalnat 已提交
1092 1093 1094
                  png_read_push_finish_row(png_ptr);
               }
            }
1095

G
Guy Schalnat 已提交
1096 1097
            break;
         }
1098

G
Guy Schalnat 已提交
1099 1100 1101
         case 4:
         {
            int i;
1102

G
Guy Schalnat 已提交
1103 1104 1105 1106 1107
            for (i = 0; i < 2 && png_ptr->pass == 4; i++)
            {
               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
               png_read_push_finish_row(png_ptr);
            }
1108

G
Guy Schalnat 已提交
1109 1110
            for (i = 0; i < 2 && png_ptr->pass == 4; i++)
            {
1111
               png_push_have_row(png_ptr, NULL);
G
Guy Schalnat 已提交
1112 1113
               png_read_push_finish_row(png_ptr);
            }
1114

1115
            if (png_ptr->pass == 6) /* Pass 5 might be empty */
1116
            {
1117
               png_push_have_row(png_ptr, NULL);
1118 1119
               png_read_push_finish_row(png_ptr);
            }
1120

G
Guy Schalnat 已提交
1121 1122
            break;
         }
1123

G
Guy Schalnat 已提交
1124 1125 1126
         case 5:
         {
            int i;
1127

G
Guy Schalnat 已提交
1128 1129 1130 1131 1132
            for (i = 0; i < 2 && png_ptr->pass == 5; i++)
            {
               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
               png_read_push_finish_row(png_ptr);
            }
1133

1134
            if (png_ptr->pass == 6) /* Skip top generated row */
G
Guy Schalnat 已提交
1135
            {
1136
               png_push_have_row(png_ptr, NULL);
G
Guy Schalnat 已提交
1137 1138
               png_read_push_finish_row(png_ptr);
            }
1139

G
Guy Schalnat 已提交
1140 1141
            break;
         }
1142

1143
         default:
G
Guy Schalnat 已提交
1144 1145 1146 1147
         case 6:
         {
            png_push_have_row(png_ptr, png_ptr->row_buf + 1);
            png_read_push_finish_row(png_ptr);
1148

G
Guy Schalnat 已提交
1149 1150
            if (png_ptr->pass != 6)
               break;
1151

1152
            png_push_have_row(png_ptr, NULL);
G
Guy Schalnat 已提交
1153 1154 1155 1156 1157
            png_read_push_finish_row(png_ptr);
         }
      }
   }
   else
G
Guy Schalnat 已提交
1158
#endif
G
Guy Schalnat 已提交
1159 1160 1161 1162
   {
      png_push_have_row(png_ptr, png_ptr->row_buf + 1);
      png_read_push_finish_row(png_ptr);
   }
G
Guy Schalnat 已提交
1163 1164
}

1165
void /* PRIVATE */
1166
png_read_push_finish_row(png_structrp png_ptr)
G
Guy Schalnat 已提交
1167
{
1168
#ifdef PNG_READ_INTERLACING_SUPPORTED
1169
   /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1170

1171
   /* Start of interlace block */
1172
   static PNG_CONST png_byte png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
1173

1174
   /* Offset to next interlace block */
1175
   static PNG_CONST png_byte png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
1176

1177
   /* Start of interlace block in the y direction */
1178
   static PNG_CONST png_byte png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
1179

1180
   /* Offset to next interlace block in the y direction */
1181
   static PNG_CONST png_byte png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
1182

1183 1184
   /* Height of interlace block.  This is not currently used - if you need
    * it, uncomment it here and in png.h
1185
   static PNG_CONST png_byte png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
1186
   */
1187
#endif
1188

G
Guy Schalnat 已提交
1189 1190
   png_ptr->row_number++;
   if (png_ptr->row_number < png_ptr->num_rows)
G
Guy Schalnat 已提交
1191
      return;
G
Guy Schalnat 已提交
1192

1193
#ifdef PNG_READ_INTERLACING_SUPPORTED
G
Guy Schalnat 已提交
1194 1195 1196
   if (png_ptr->interlaced)
   {
      png_ptr->row_number = 0;
1197
      memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
1198

G
Guy Schalnat 已提交
1199 1200 1201
      do
      {
         png_ptr->pass++;
1202 1203 1204
         if ((png_ptr->pass == 1 && png_ptr->width < 5) ||
             (png_ptr->pass == 3 && png_ptr->width < 3) ||
             (png_ptr->pass == 5 && png_ptr->width < 2))
1205
            png_ptr->pass++;
1206

1207 1208
         if (png_ptr->pass > 7)
            png_ptr->pass--;
1209

G
Guy Schalnat 已提交
1210 1211
         if (png_ptr->pass >= 7)
            break;
1212

G
Guy Schalnat 已提交
1213
         png_ptr->iwidth = (png_ptr->width +
1214 1215 1216
             png_pass_inc[png_ptr->pass] - 1 -
             png_pass_start[png_ptr->pass]) /
             png_pass_inc[png_ptr->pass];
1217

G
Guy Schalnat 已提交
1218 1219
         if (png_ptr->transformations & PNG_INTERLACE)
            break;
1220 1221

         png_ptr->num_rows = (png_ptr->height +
1222 1223 1224
             png_pass_yinc[png_ptr->pass] - 1 -
             png_pass_ystart[png_ptr->pass]) /
             png_pass_yinc[png_ptr->pass];
1225 1226

      } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0);
G
Guy Schalnat 已提交
1227
   }
1228
#endif /* PNG_READ_INTERLACING_SUPPORTED */
G
Guy Schalnat 已提交
1229 1230
}

1231
void /* PRIVATE */
1232
png_push_have_info(png_structrp png_ptr, png_inforp info_ptr)
G
Guy Schalnat 已提交
1233
{
A
Andreas Dilger 已提交
1234
   if (png_ptr->info_fn != NULL)
A
Andreas Dilger 已提交
1235
      (*(png_ptr->info_fn))(png_ptr, info_ptr);
G
Guy Schalnat 已提交
1236 1237
}

1238
void /* PRIVATE */
1239
png_push_have_end(png_structrp png_ptr, png_inforp info_ptr)
G
Guy Schalnat 已提交
1240
{
A
Andreas Dilger 已提交
1241
   if (png_ptr->end_fn != NULL)
A
Andreas Dilger 已提交
1242
      (*(png_ptr->end_fn))(png_ptr, info_ptr);
G
Guy Schalnat 已提交
1243 1244
}

1245
void /* PRIVATE */
1246
png_push_have_row(png_structrp png_ptr, png_bytep row)
G
Guy Schalnat 已提交
1247
{
1248
   if (png_ptr->row_fn != NULL)
G
Guy Schalnat 已提交
1249 1250
      (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number,
         (int)png_ptr->pass);
G
Guy Schalnat 已提交
1251 1252
}

1253
#ifdef PNG_READ_INTERLACING_SUPPORTED
1254
void PNGAPI
1255
png_progressive_combine_row(png_const_structrp png_ptr, png_bytep old_row,
1256
    png_const_bytep new_row)
G
Guy Schalnat 已提交
1257
{
1258 1259 1260
   if (png_ptr == NULL)
      return;

1261 1262 1263 1264 1265 1266
   /* new_row is a flag here - if it is NULL then the app callback was called
    * from an empty row (see the calls to png_struct::row_fn below), otherwise
    * it must be png_ptr->row_buf+1
    */
   if (new_row != NULL)
      png_combine_row(png_ptr, old_row, 1/*display*/);
G
Guy Schalnat 已提交
1267
}
1268
#endif /* PNG_READ_INTERLACING_SUPPORTED */
G
Guy Schalnat 已提交
1269

1270
void PNGAPI
1271
png_set_progressive_read_fn(png_structrp png_ptr, png_voidp progressive_ptr,
1272 1273
    png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
    png_progressive_end_ptr end_fn)
G
Guy Schalnat 已提交
1274
{
1275 1276 1277
   if (png_ptr == NULL)
      return;

G
Guy Schalnat 已提交
1278 1279
   png_ptr->info_fn = info_fn;
   png_ptr->row_fn = row_fn;
G
Guy Schalnat 已提交
1280
   png_ptr->end_fn = end_fn;
G
Guy Schalnat 已提交
1281

1282
   png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer);
G
Guy Schalnat 已提交
1283 1284
}

1285
png_voidp PNGAPI
1286
png_get_progressive_ptr(png_const_structrp png_ptr)
G
Guy Schalnat 已提交
1287
{
1288 1289 1290
   if (png_ptr == NULL)
      return (NULL);

A
Andreas Dilger 已提交
1291
   return png_ptr->io_ptr;
G
Guy Schalnat 已提交
1292
}
G
Guy Schalnat 已提交
1293
#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */