Coverage Report

Created: 2026-05-16 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opencv/3rdparty/libpng/pngpread.c
Line
Count
Source
1
/* pngpread.c - read a png file in push mode
2
 *
3
 * Copyright (c) 2018-2025 Cosmin Truta
4
 * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson
5
 * Copyright (c) 1996-1997 Andreas Dilger
6
 * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.
7
 *
8
 * This code is released under the libpng license.
9
 * For conditions of distribution and use, see the disclaimer
10
 * and license in png.h
11
 */
12
13
#include "pngpriv.h"
14
15
#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
16
17
/* Push model modes */
18
13
#define PNG_READ_SIG_MODE   0
19
497
#define PNG_READ_CHUNK_MODE 1
20
48
#define PNG_READ_IDAT_MODE  2
21
#define PNG_READ_tEXt_MODE  4
22
#define PNG_READ_zTXt_MODE  5
23
1
#define PNG_READ_DONE_MODE  6
24
#define PNG_READ_iTXt_MODE  7
25
#define PNG_ERROR_MODE      8
26
27
462
#define PNG_PUSH_SAVE_BUFFER_IF_FULL \
28
462
if (png_ptr->push_length + 4 > png_ptr->buffer_size) \
29
462
   { png_push_save_buffer(png_ptr); return; }
30
488
#define PNG_PUSH_SAVE_BUFFER_IF_LT(N) \
31
488
if (png_ptr->buffer_size < N) \
32
488
   { png_push_save_buffer(png_ptr); return; }
33
34
#ifdef PNG_READ_INTERLACING_SUPPORTED
35
/* Arrays to facilitate interlacing - use pass (0 - 6) as index. */
36
37
/* Start of interlace block */
38
static const png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
39
/* Offset to next interlace block */
40
static const png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
41
/* Start of interlace block in the y direction */
42
static const png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
43
/* Offset to next interlace block in the y direction */
44
static const png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
45
46
/* TODO: Move these arrays to a common utility module to avoid duplication. */
47
#endif
48
49
void PNGAPI
50
png_process_data(png_structrp png_ptr, png_inforp info_ptr,
51
    png_bytep buffer, size_t buffer_size)
52
488
{
53
488
   if (png_ptr == NULL || info_ptr == NULL)
54
0
      return;
55
56
488
   png_push_restore_buffer(png_ptr, buffer, buffer_size);
57
58
998
   while (png_ptr->buffer_size)
59
510
   {
60
510
      png_process_some_data(png_ptr, info_ptr);
61
510
   }
62
488
}
63
64
size_t PNGAPI
65
png_process_data_pause(png_structrp png_ptr, int save)
66
0
{
67
0
   if (png_ptr != NULL)
68
0
   {
69
      /* It's easiest for the caller if we do the save; then the caller doesn't
70
       * have to supply the same data again:
71
       */
72
0
      if (save != 0)
73
0
         png_push_save_buffer(png_ptr);
74
0
      else
75
0
      {
76
         /* This includes any pending saved bytes: */
77
0
         size_t remaining = png_ptr->buffer_size;
78
0
         png_ptr->buffer_size = 0;
79
80
         /* So subtract the saved buffer size, unless all the data
81
          * is actually 'saved', in which case we just return 0
82
          */
83
0
         if (png_ptr->save_buffer_size < remaining)
84
0
            return remaining - png_ptr->save_buffer_size;
85
0
      }
86
0
   }
87
88
0
   return 0;
89
0
}
90
91
png_uint_32 PNGAPI
92
png_process_data_skip(png_structrp png_ptr)
93
0
{
94
/* TODO: Deprecate and remove this API.
95
 * Somewhere the implementation of this seems to have been lost,
96
 * or abandoned.  It was only to support some internal back-door access
97
 * to png_struct) in libpng-1.4.x.
98
 */
99
0
   png_app_warning(png_ptr,
100
0
"png_process_data_skip is not implemented in any current version of libpng");
101
0
   return 0;
102
0
}
103
104
/* What we do with the incoming data depends on what we were previously
105
 * doing before we ran out of data...
106
 */
107
void /* PRIVATE */
108
png_process_some_data(png_structrp png_ptr, png_inforp info_ptr)
109
510
{
110
510
   if (png_ptr == NULL)
111
0
      return;
112
113
510
   switch (png_ptr->process_mode)
114
510
   {
115
13
      case PNG_READ_SIG_MODE:
116
13
      {
117
13
         png_push_read_sig(png_ptr, info_ptr);
118
13
         break;
119
0
      }
120
121
475
      case PNG_READ_CHUNK_MODE:
122
475
      {
123
475
         png_push_read_chunk(png_ptr, info_ptr);
124
475
         break;
125
0
      }
126
127
22
      case PNG_READ_IDAT_MODE:
128
22
      {
129
22
         png_push_read_IDAT(png_ptr);
130
22
         break;
131
0
      }
132
133
0
      default:
134
0
      {
135
0
         png_ptr->buffer_size = 0;
136
0
         break;
137
0
      }
138
510
   }
139
510
}
140
141
/* Read any remaining signature bytes from the stream and compare them with
142
 * the correct PNG signature.  It is possible that this routine is called
143
 * with bytes already read from the signature, either because they have been
144
 * checked by the calling application, or because of multiple calls to this
145
 * routine.
146
 */
147
void /* PRIVATE */
148
png_push_read_sig(png_structrp png_ptr, png_inforp info_ptr)
149
13
{
150
13
   size_t num_checked = png_ptr->sig_bytes; /* SAFE, does not exceed 8 */
151
13
   size_t num_to_check = 8 - num_checked;
152
153
13
   if (png_ptr->buffer_size < num_to_check)
154
0
   {
155
0
      num_to_check = png_ptr->buffer_size;
156
0
   }
157
158
13
   png_push_fill_buffer(png_ptr, &(info_ptr->signature[num_checked]),
159
13
       num_to_check);
160
13
   png_ptr->sig_bytes = (png_byte)(png_ptr->sig_bytes + num_to_check);
161
162
13
   if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check) != 0)
163
0
   {
164
0
      if (num_checked < 4 &&
165
0
          png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4) != 0)
166
0
         png_error(png_ptr, "Not a PNG file");
167
168
0
      else
169
0
         png_error(png_ptr, "PNG file corrupted by ASCII conversion");
170
0
   }
171
13
   else
172
13
   {
173
13
      if (png_ptr->sig_bytes >= 8)
174
13
      {
175
13
         png_ptr->process_mode = PNG_READ_CHUNK_MODE;
176
13
      }
177
13
   }
178
13
}
179
180
void /* PRIVATE */
181
png_push_read_chunk(png_structrp png_ptr, png_inforp info_ptr)
182
475
{
183
475
   png_uint_32 chunk_name;
184
475
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
185
475
   int keep; /* unknown handling method */
186
475
#endif
187
188
   /* First we make sure we have enough data for the 4-byte chunk name
189
    * and the 4-byte chunk length before proceeding with decoding the
190
    * chunk data.  To fully decode each of these chunks, we also make
191
    * sure we have enough data in the buffer for the 4-byte CRC at the
192
    * end of every chunk (except IDAT, which is handled separately).
193
    */
194
475
   if ((png_ptr->mode & PNG_HAVE_CHUNK_HEADER) == 0)
195
466
   {
196
466
      PNG_PUSH_SAVE_BUFFER_IF_LT(8)
197
466
      png_ptr->push_length = png_read_chunk_header(png_ptr);
198
466
      png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
199
466
   }
200
201
475
   chunk_name = png_ptr->chunk_name;
202
203
475
   if (chunk_name == png_IDAT)
204
13
   {
205
13
      if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
206
0
         png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
207
208
      /* If we reach an IDAT chunk, this means we have read all of the
209
       * header chunks, and we can start reading the image (or if this
210
       * is called after the image has been read - we have an error).
211
       */
212
13
      if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
213
0
         png_error(png_ptr, "Missing IHDR before IDAT");
214
215
13
      else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
216
6
          (png_ptr->mode & PNG_HAVE_PLTE) == 0)
217
0
         png_error(png_ptr, "Missing PLTE before IDAT");
218
219
13
      png_ptr->process_mode = PNG_READ_IDAT_MODE;
220
221
13
      if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
222
0
         if ((png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) == 0)
223
0
            if (png_ptr->push_length == 0)
224
0
               return;
225
226
13
      png_ptr->mode |= PNG_HAVE_IDAT;
227
228
13
      if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
229
0
         png_benign_error(png_ptr, "Too many IDATs found");
230
13
   }
231
232
462
   else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
233
439
   {
234
      /* These flags must be set consistently for all non-IDAT chunks,
235
       * including the unknown chunks.
236
       */
237
439
      png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT | PNG_AFTER_IDAT;
238
439
   }
239
240
475
   if (chunk_name == png_IHDR)
241
13
   {
242
13
      if (png_ptr->push_length != 13)
243
0
         png_error(png_ptr, "Invalid IHDR length");
244
245
13
      PNG_PUSH_SAVE_BUFFER_IF_FULL
246
13
      png_handle_chunk(png_ptr, info_ptr, png_ptr->push_length);
247
13
   }
248
249
462
   else if (chunk_name == png_IEND)
250
1
   {
251
1
      PNG_PUSH_SAVE_BUFFER_IF_FULL
252
1
      png_handle_chunk(png_ptr, info_ptr, png_ptr->push_length);
253
254
1
      png_ptr->process_mode = PNG_READ_DONE_MODE;
255
1
      png_push_have_end(png_ptr, info_ptr);
256
1
   }
257
258
461
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
259
461
   else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
260
0
   {
261
0
      PNG_PUSH_SAVE_BUFFER_IF_FULL
262
0
      png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length, keep);
263
264
0
      if (chunk_name == png_PLTE)
265
0
         png_ptr->mode |= PNG_HAVE_PLTE;
266
0
   }
267
461
#endif
268
269
461
   else if (chunk_name == png_IDAT)
270
13
   {
271
13
      png_ptr->idat_size = png_ptr->push_length;
272
13
      png_ptr->process_mode = PNG_READ_IDAT_MODE;
273
13
      png_push_have_info(png_ptr, info_ptr);
274
13
      png_ptr->zstream.avail_out =
275
13
          (uInt) PNG_ROWBYTES(png_ptr->pixel_depth,
276
13
          png_ptr->iwidth) + 1;
277
13
      png_ptr->zstream.next_out = png_ptr->row_buf;
278
13
      return;
279
13
   }
280
281
448
   else
282
448
   {
283
448
      PNG_PUSH_SAVE_BUFFER_IF_FULL
284
448
      png_handle_chunk(png_ptr, info_ptr, png_ptr->push_length);
285
448
   }
286
287
462
   png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
288
462
}
289
290
void PNGCBAPI
291
png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, size_t length)
292
1.40k
{
293
1.40k
   png_bytep ptr;
294
295
1.40k
   if (png_ptr == NULL)
296
0
      return;
297
298
1.40k
   ptr = buffer;
299
1.40k
   if (png_ptr->save_buffer_size != 0)
300
0
   {
301
0
      size_t save_size;
302
303
0
      if (length < png_ptr->save_buffer_size)
304
0
         save_size = length;
305
306
0
      else
307
0
         save_size = png_ptr->save_buffer_size;
308
309
0
      memcpy(ptr, png_ptr->save_buffer_ptr, save_size);
310
0
      length -= save_size;
311
0
      ptr += save_size;
312
0
      png_ptr->buffer_size -= save_size;
313
0
      png_ptr->save_buffer_size -= save_size;
314
0
      png_ptr->save_buffer_ptr += save_size;
315
0
   }
316
1.40k
   if (length != 0 && png_ptr->current_buffer_size != 0)
317
1.40k
   {
318
1.40k
      size_t save_size;
319
320
1.40k
      if (length < png_ptr->current_buffer_size)
321
918
         save_size = length;
322
323
487
      else
324
487
         save_size = png_ptr->current_buffer_size;
325
326
1.40k
      memcpy(ptr, png_ptr->current_buffer_ptr, save_size);
327
1.40k
      png_ptr->buffer_size -= save_size;
328
1.40k
      png_ptr->current_buffer_size -= save_size;
329
1.40k
      png_ptr->current_buffer_ptr += save_size;
330
1.40k
   }
331
1.40k
}
332
333
void /* PRIVATE */
334
png_push_save_buffer(png_structrp png_ptr)
335
0
{
336
0
   if (png_ptr->save_buffer_size != 0)
337
0
   {
338
0
      if (png_ptr->save_buffer_ptr != png_ptr->save_buffer)
339
0
      {
340
0
         size_t i, istop;
341
0
         png_bytep sp;
342
0
         png_bytep dp;
343
344
0
         istop = png_ptr->save_buffer_size;
345
0
         for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer;
346
0
             i < istop; i++, sp++, dp++)
347
0
         {
348
0
            *dp = *sp;
349
0
         }
350
0
      }
351
0
   }
352
0
   if (png_ptr->save_buffer_size + png_ptr->current_buffer_size >
353
0
       png_ptr->save_buffer_max)
354
0
   {
355
0
      size_t new_max;
356
0
      png_bytep old_buffer;
357
358
0
      if (png_ptr->save_buffer_size > PNG_SIZE_MAX -
359
0
          (png_ptr->current_buffer_size + 256))
360
0
      {
361
0
         png_error(png_ptr, "Potential overflow of save_buffer");
362
0
      }
363
364
0
      new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;
365
0
      old_buffer = png_ptr->save_buffer;
366
0
      png_ptr->save_buffer = (png_bytep)png_malloc_warn(png_ptr,
367
0
          (size_t)new_max);
368
369
0
      if (png_ptr->save_buffer == NULL)
370
0
      {
371
0
         png_free(png_ptr, old_buffer);
372
0
         png_error(png_ptr, "Insufficient memory for save_buffer");
373
0
      }
374
375
0
      if (old_buffer)
376
0
         memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
377
0
      else if (png_ptr->save_buffer_size)
378
0
         png_error(png_ptr, "save_buffer error");
379
0
      png_free(png_ptr, old_buffer);
380
0
      png_ptr->save_buffer_max = new_max;
381
0
   }
382
0
   if (png_ptr->current_buffer_size)
383
0
   {
384
0
      memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
385
0
         png_ptr->current_buffer_ptr, png_ptr->current_buffer_size);
386
0
      png_ptr->save_buffer_size += png_ptr->current_buffer_size;
387
0
      png_ptr->current_buffer_size = 0;
388
0
   }
389
0
   png_ptr->save_buffer_ptr = png_ptr->save_buffer;
390
0
   png_ptr->buffer_size = 0;
391
0
}
392
393
void /* PRIVATE */
394
png_push_restore_buffer(png_structrp png_ptr, png_bytep buffer,
395
    size_t buffer_length)
396
488
{
397
488
   png_ptr->current_buffer = buffer;
398
488
   png_ptr->current_buffer_size = buffer_length;
399
488
   png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size;
400
488
   png_ptr->current_buffer_ptr = png_ptr->current_buffer;
401
488
}
402
403
void /* PRIVATE */
404
png_push_read_IDAT(png_structrp png_ptr)
405
22
{
406
22
   if ((png_ptr->mode & PNG_HAVE_CHUNK_HEADER) == 0)
407
9
   {
408
9
      png_byte chunk_length[4];
409
9
      png_byte chunk_tag[4];
410
411
      /* TODO: this code can be commoned up with the same code in push_read */
412
9
      PNG_PUSH_SAVE_BUFFER_IF_LT(8)
413
9
      png_push_fill_buffer(png_ptr, chunk_length, 4);
414
9
      png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
415
9
      png_reset_crc(png_ptr);
416
9
      png_crc_read(png_ptr, chunk_tag, 4);
417
9
      png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(chunk_tag);
418
9
      png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
419
420
9
      if (png_ptr->chunk_name != png_IDAT)
421
9
      {
422
9
         png_ptr->process_mode = PNG_READ_CHUNK_MODE;
423
424
9
         if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
425
0
            png_error(png_ptr, "Not enough compressed data");
426
427
9
         return;
428
9
      }
429
430
0
      png_ptr->idat_size = png_ptr->push_length;
431
0
   }
432
433
13
   if (png_ptr->idat_size != 0 && png_ptr->save_buffer_size != 0)
434
0
   {
435
0
      size_t save_size = png_ptr->save_buffer_size;
436
0
      png_uint_32 idat_size = png_ptr->idat_size;
437
438
      /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
439
       * are of different types and we don't know which variable has the fewest
440
       * bits.  Carefully select the smaller and cast it to the type of the
441
       * larger - this cannot overflow.  Do not cast in the following test - it
442
       * will break on either 16-bit or 64-bit platforms.
443
       */
444
0
      if (idat_size < save_size)
445
0
         save_size = (size_t)idat_size;
446
447
0
      else
448
0
         idat_size = (png_uint_32)save_size;
449
450
0
      png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
451
452
0
      png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size);
453
454
0
      png_ptr->idat_size -= idat_size;
455
0
      png_ptr->buffer_size -= save_size;
456
0
      png_ptr->save_buffer_size -= save_size;
457
0
      png_ptr->save_buffer_ptr += save_size;
458
0
   }
459
460
13
   if (png_ptr->idat_size != 0 && png_ptr->current_buffer_size != 0)
461
13
   {
462
13
      size_t save_size = png_ptr->current_buffer_size;
463
13
      png_uint_32 idat_size = png_ptr->idat_size;
464
465
      /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
466
       * are of different types and we don't know which variable has the fewest
467
       * bits.  Carefully select the smaller and cast it to the type of the
468
       * larger - this cannot overflow.
469
       */
470
13
      if (idat_size < save_size)
471
13
         save_size = (size_t)idat_size;
472
473
0
      else
474
0
         idat_size = (png_uint_32)save_size;
475
476
13
      png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
477
478
13
      png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size);
479
480
13
      png_ptr->idat_size -= idat_size;
481
13
      png_ptr->buffer_size -= save_size;
482
13
      png_ptr->current_buffer_size -= save_size;
483
13
      png_ptr->current_buffer_ptr += save_size;
484
13
   }
485
486
13
   if (png_ptr->idat_size == 0)
487
13
   {
488
13
      PNG_PUSH_SAVE_BUFFER_IF_LT(4)
489
13
      png_crc_finish(png_ptr, 0);
490
13
      png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
491
13
      png_ptr->mode |= PNG_AFTER_IDAT;
492
13
      png_ptr->zowner = 0;
493
13
   }
494
13
}
495
496
void /* PRIVATE */
497
png_process_IDAT_data(png_structrp png_ptr, png_bytep buffer,
498
    size_t buffer_length)
499
13
{
500
   /* The caller checks for a non-zero buffer length. */
501
13
   if (!(buffer_length > 0) || buffer == NULL)
502
0
      png_error(png_ptr, "No IDAT data (internal error)");
503
504
   /* This routine must process all the data it has been given
505
    * before returning, calling the row callback as required to
506
    * handle the uncompressed results.
507
    */
508
13
   png_ptr->zstream.next_in = buffer;
509
   /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
510
13
   png_ptr->zstream.avail_in = (uInt)buffer_length;
511
512
   /* Keep going until the decompressed data is all processed
513
    * or the stream marked as finished.
514
    */
515
154
   while (png_ptr->zstream.avail_in > 0 &&
516
154
      (png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
517
154
   {
518
154
      int ret;
519
520
      /* We have data for zlib, but we must check that zlib
521
       * has someplace to put the results.  It doesn't matter
522
       * if we don't expect any results -- it may be the input
523
       * data is just the LZ end code.
524
       */
525
154
      if (!(png_ptr->zstream.avail_out > 0))
526
141
      {
527
         /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
528
141
         png_ptr->zstream.avail_out = (uInt)(PNG_ROWBYTES(png_ptr->pixel_depth,
529
141
             png_ptr->iwidth) + 1);
530
531
141
         png_ptr->zstream.next_out = png_ptr->row_buf;
532
141
      }
533
534
      /* Using Z_SYNC_FLUSH here means that an unterminated
535
       * LZ stream (a stream with a missing end code) can still
536
       * be handled, otherwise (Z_NO_FLUSH) a future zlib
537
       * implementation might defer output and therefore
538
       * change the current behavior (see comments in inflate.c
539
       * for why this doesn't happen at present with zlib 1.2.5).
540
       */
541
154
      ret = PNG_INFLATE(png_ptr, Z_SYNC_FLUSH);
542
543
      /* Check for any failure before proceeding. */
544
154
      if (ret != Z_OK && ret != Z_STREAM_END)
545
13
      {
546
         /* Terminate the decompression. */
547
13
         png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
548
13
         png_ptr->zowner = 0;
549
550
         /* This may be a truncated stream (missing or
551
          * damaged end code).  Treat that as a warning.
552
          */
553
13
         if (png_ptr->row_number >= png_ptr->num_rows ||
554
13
             png_ptr->pass > 6)
555
0
            png_warning(png_ptr, "Truncated compressed data in IDAT");
556
557
13
         else
558
13
         {
559
13
            if (ret == Z_DATA_ERROR)
560
13
               png_benign_error(png_ptr, "IDAT: ADLER32 checksum mismatch");
561
0
            else
562
0
               png_error(png_ptr, "Decompression error in IDAT");
563
13
         }
564
565
         /* Skip the check on unprocessed input */
566
13
         return;
567
13
      }
568
569
      /* Did inflate output any data? */
570
141
      if (png_ptr->zstream.next_out != png_ptr->row_buf)
571
141
      {
572
         /* Is this unexpected data after the last row?
573
          * If it is, artificially terminate the LZ output
574
          * here.
575
          */
576
141
         if (png_ptr->row_number >= png_ptr->num_rows ||
577
141
             png_ptr->pass > 6)
578
0
         {
579
            /* Extra data. */
580
0
            png_warning(png_ptr, "Extra compressed data in IDAT");
581
0
            png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
582
0
            png_ptr->zowner = 0;
583
584
            /* Do no more processing; skip the unprocessed
585
             * input check below.
586
             */
587
0
            return;
588
0
         }
589
590
         /* Do we have a complete row? */
591
141
         if (png_ptr->zstream.avail_out == 0)
592
141
            png_push_process_row(png_ptr);
593
141
      }
594
595
      /* And check for the end of the stream. */
596
141
      if (ret == Z_STREAM_END)
597
0
         png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
598
141
   }
599
600
   /* All the data should have been processed, if anything
601
    * is left at this point we have bytes of IDAT data
602
    * after the zlib end code.
603
    */
604
0
   if (png_ptr->zstream.avail_in > 0)
605
0
      png_warning(png_ptr, "Extra compression data in IDAT");
606
0
}
607
608
void /* PRIVATE */
609
png_push_process_row(png_structrp png_ptr)
610
141
{
611
   /* 1.5.6: row_info moved out of png_struct to a local here. */
612
141
   png_row_info row_info;
613
614
141
   row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */
615
141
   row_info.color_type = png_ptr->color_type;
616
141
   row_info.bit_depth = png_ptr->bit_depth;
617
141
   row_info.channels = png_ptr->channels;
618
141
   row_info.pixel_depth = png_ptr->pixel_depth;
619
141
   row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
620
621
141
   if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
622
0
   {
623
0
      if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
624
0
         png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1,
625
0
            png_ptr->prev_row + 1, png_ptr->row_buf[0]);
626
0
      else
627
0
         png_error(png_ptr, "bad adaptive filter value");
628
0
   }
629
630
   /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
631
    * 1.5.6, while the buffer really is this big in current versions of libpng
632
    * it may not be in the future, so this was changed just to copy the
633
    * interlaced row count:
634
    */
635
141
   memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
636
637
141
#ifdef PNG_READ_TRANSFORMS_SUPPORTED
638
141
   if (png_ptr->transformations != 0)
639
141
      png_do_read_transformations(png_ptr, &row_info);
640
141
#endif
641
642
   /* The transformed pixel depth should match the depth now in row_info. */
643
141
   if (png_ptr->transformed_pixel_depth == 0)
644
5
   {
645
5
      png_ptr->transformed_pixel_depth = row_info.pixel_depth;
646
5
      if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
647
0
         png_error(png_ptr, "progressive row overflow");
648
5
   }
649
650
136
   else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
651
0
      png_error(png_ptr, "internal progressive row size calculation error");
652
653
654
141
#ifdef PNG_READ_INTERLACING_SUPPORTED
655
   /* Expand interlaced rows to full size */
656
141
   if (png_ptr->interlaced != 0 &&
657
0
       (png_ptr->transformations & PNG_INTERLACE) != 0)
658
0
   {
659
0
      if (png_ptr->pass < 6)
660
0
         png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
661
0
             png_ptr->transformations);
662
663
0
      switch (png_ptr->pass)
664
0
      {
665
0
         case 0:
666
0
         {
667
0
            int i;
668
0
            for (i = 0; i < 8 && png_ptr->pass == 0; i++)
669
0
            {
670
0
               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
671
0
               png_read_push_finish_row(png_ptr); /* Updates png_ptr->pass */
672
0
            }
673
674
0
            if (png_ptr->pass == 2) /* Pass 1 might be empty */
675
0
            {
676
0
               for (i = 0; i < 4 && png_ptr->pass == 2; i++)
677
0
               {
678
0
                  png_push_have_row(png_ptr, NULL);
679
0
                  png_read_push_finish_row(png_ptr);
680
0
               }
681
0
            }
682
683
0
            if (png_ptr->pass == 4 && png_ptr->height <= 4)
684
0
            {
685
0
               for (i = 0; i < 2 && png_ptr->pass == 4; i++)
686
0
               {
687
0
                  png_push_have_row(png_ptr, NULL);
688
0
                  png_read_push_finish_row(png_ptr);
689
0
               }
690
0
            }
691
692
0
            if (png_ptr->pass == 6 && png_ptr->height <= 4)
693
0
            {
694
0
                png_push_have_row(png_ptr, NULL);
695
0
                png_read_push_finish_row(png_ptr);
696
0
            }
697
698
0
            break;
699
0
         }
700
701
0
         case 1:
702
0
         {
703
0
            int i;
704
0
            for (i = 0; i < 8 && png_ptr->pass == 1; i++)
705
0
            {
706
0
               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
707
0
               png_read_push_finish_row(png_ptr);
708
0
            }
709
710
0
            if (png_ptr->pass == 2) /* Skip top 4 generated rows */
711
0
            {
712
0
               for (i = 0; i < 4 && png_ptr->pass == 2; i++)
713
0
               {
714
0
                  png_push_have_row(png_ptr, NULL);
715
0
                  png_read_push_finish_row(png_ptr);
716
0
               }
717
0
            }
718
719
0
            break;
720
0
         }
721
722
0
         case 2:
723
0
         {
724
0
            int i;
725
726
0
            for (i = 0; i < 4 && png_ptr->pass == 2; i++)
727
0
            {
728
0
               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
729
0
               png_read_push_finish_row(png_ptr);
730
0
            }
731
732
0
            for (i = 0; i < 4 && png_ptr->pass == 2; i++)
733
0
            {
734
0
               png_push_have_row(png_ptr, NULL);
735
0
               png_read_push_finish_row(png_ptr);
736
0
            }
737
738
0
            if (png_ptr->pass == 4) /* Pass 3 might be empty */
739
0
            {
740
0
               for (i = 0; i < 2 && png_ptr->pass == 4; i++)
741
0
               {
742
0
                  png_push_have_row(png_ptr, NULL);
743
0
                  png_read_push_finish_row(png_ptr);
744
0
               }
745
0
            }
746
747
0
            break;
748
0
         }
749
750
0
         case 3:
751
0
         {
752
0
            int i;
753
754
0
            for (i = 0; i < 4 && png_ptr->pass == 3; i++)
755
0
            {
756
0
               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
757
0
               png_read_push_finish_row(png_ptr);
758
0
            }
759
760
0
            if (png_ptr->pass == 4) /* Skip top two generated rows */
761
0
            {
762
0
               for (i = 0; i < 2 && png_ptr->pass == 4; i++)
763
0
               {
764
0
                  png_push_have_row(png_ptr, NULL);
765
0
                  png_read_push_finish_row(png_ptr);
766
0
               }
767
0
            }
768
769
0
            break;
770
0
         }
771
772
0
         case 4:
773
0
         {
774
0
            int i;
775
776
0
            for (i = 0; i < 2 && png_ptr->pass == 4; i++)
777
0
            {
778
0
               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
779
0
               png_read_push_finish_row(png_ptr);
780
0
            }
781
782
0
            for (i = 0; i < 2 && png_ptr->pass == 4; i++)
783
0
            {
784
0
               png_push_have_row(png_ptr, NULL);
785
0
               png_read_push_finish_row(png_ptr);
786
0
            }
787
788
0
            if (png_ptr->pass == 6) /* Pass 5 might be empty */
789
0
            {
790
0
               png_push_have_row(png_ptr, NULL);
791
0
               png_read_push_finish_row(png_ptr);
792
0
            }
793
794
0
            break;
795
0
         }
796
797
0
         case 5:
798
0
         {
799
0
            int i;
800
801
0
            for (i = 0; i < 2 && png_ptr->pass == 5; i++)
802
0
            {
803
0
               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
804
0
               png_read_push_finish_row(png_ptr);
805
0
            }
806
807
0
            if (png_ptr->pass == 6) /* Skip top generated row */
808
0
            {
809
0
               png_push_have_row(png_ptr, NULL);
810
0
               png_read_push_finish_row(png_ptr);
811
0
            }
812
813
0
            break;
814
0
         }
815
816
0
         default:
817
0
         case 6:
818
0
         {
819
0
            png_push_have_row(png_ptr, png_ptr->row_buf + 1);
820
0
            png_read_push_finish_row(png_ptr);
821
822
0
            if (png_ptr->pass != 6)
823
0
               break;
824
825
0
            png_push_have_row(png_ptr, NULL);
826
0
            png_read_push_finish_row(png_ptr);
827
0
         }
828
0
      }
829
0
   }
830
141
   else
831
141
#endif
832
141
   {
833
141
      png_push_have_row(png_ptr, png_ptr->row_buf + 1);
834
141
      png_read_push_finish_row(png_ptr);
835
141
   }
836
141
}
837
838
void /* PRIVATE */
839
png_read_push_finish_row(png_structrp png_ptr)
840
141
{
841
141
   png_ptr->row_number++;
842
141
   if (png_ptr->row_number < png_ptr->num_rows)
843
141
      return;
844
845
0
#ifdef PNG_READ_INTERLACING_SUPPORTED
846
0
   if (png_ptr->interlaced != 0)
847
0
   {
848
0
      png_ptr->row_number = 0;
849
0
      memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
850
851
0
      do
852
0
      {
853
0
         png_ptr->pass++;
854
0
         if ((png_ptr->pass == 1 && png_ptr->width < 5) ||
855
0
             (png_ptr->pass == 3 && png_ptr->width < 3) ||
856
0
             (png_ptr->pass == 5 && png_ptr->width < 2))
857
0
            png_ptr->pass++;
858
859
0
         if (png_ptr->pass > 7)
860
0
            png_ptr->pass--;
861
862
0
         if (png_ptr->pass >= 7)
863
0
            break;
864
865
0
         png_ptr->iwidth = (png_ptr->width +
866
0
             png_pass_inc[png_ptr->pass] - 1 -
867
0
             png_pass_start[png_ptr->pass]) /
868
0
             png_pass_inc[png_ptr->pass];
869
870
0
         if ((png_ptr->transformations & PNG_INTERLACE) != 0)
871
0
            break;
872
873
0
         png_ptr->num_rows = (png_ptr->height +
874
0
             png_pass_yinc[png_ptr->pass] - 1 -
875
0
             png_pass_ystart[png_ptr->pass]) /
876
0
             png_pass_yinc[png_ptr->pass];
877
878
0
      } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0);
879
0
   }
880
0
#endif /* READ_INTERLACING */
881
0
}
882
883
void /* PRIVATE */
884
png_push_have_info(png_structrp png_ptr, png_inforp info_ptr)
885
13
{
886
13
   if (png_ptr->info_fn != NULL)
887
13
      (*(png_ptr->info_fn))(png_ptr, info_ptr);
888
13
}
889
890
void /* PRIVATE */
891
png_push_have_end(png_structrp png_ptr, png_inforp info_ptr)
892
1
{
893
1
   if (png_ptr->end_fn != NULL)
894
0
      (*(png_ptr->end_fn))(png_ptr, info_ptr);
895
1
}
896
897
void /* PRIVATE */
898
png_push_have_row(png_structrp png_ptr, png_bytep row)
899
141
{
900
141
   if (png_ptr->row_fn != NULL)
901
141
      (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number,
902
141
          (int)png_ptr->pass);
903
141
}
904
905
#ifdef PNG_READ_INTERLACING_SUPPORTED
906
void PNGAPI
907
png_progressive_combine_row(png_const_structrp png_ptr, png_bytep old_row,
908
    png_const_bytep new_row)
909
141
{
910
141
   if (png_ptr == NULL)
911
0
      return;
912
913
   /* new_row is a flag here - if it is NULL then the app callback was called
914
    * from an empty row (see the calls to png_struct::row_fn below), otherwise
915
    * it must be png_ptr->row_buf+1
916
    */
917
141
   if (new_row != NULL)
918
141
      png_combine_row(png_ptr, old_row, 1/*blocky display*/);
919
141
}
920
#endif /* READ_INTERLACING */
921
922
void PNGAPI
923
png_set_progressive_read_fn(png_structrp png_ptr, png_voidp progressive_ptr,
924
    png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
925
    png_progressive_end_ptr end_fn)
926
13
{
927
13
   if (png_ptr == NULL)
928
0
      return;
929
930
13
   png_ptr->info_fn = info_fn;
931
13
   png_ptr->row_fn = row_fn;
932
13
   png_ptr->end_fn = end_fn;
933
934
13
   png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer);
935
13
}
936
937
png_voidp PNGAPI
938
png_get_progressive_ptr(png_const_structrp png_ptr)
939
141
{
940
141
   if (png_ptr == NULL)
941
0
      return NULL;
942
943
141
   return png_ptr->io_ptr;
944
141
}
945
#endif /* PROGRESSIVE_READ */