Coverage Report

Created: 2025-09-27 07:21

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-2024 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
244
#define PNG_READ_SIG_MODE   0
19
3.68k
#define PNG_READ_CHUNK_MODE 1
20
988
#define PNG_READ_IDAT_MODE  2
21
#define PNG_READ_tEXt_MODE  4
22
#define PNG_READ_zTXt_MODE  5
23
29
#define PNG_READ_DONE_MODE  6
24
#define PNG_READ_iTXt_MODE  7
25
#define PNG_ERROR_MODE      8
26
27
3.01k
#define PNG_PUSH_SAVE_BUFFER_IF_FULL \
28
3.01k
if (png_ptr->push_length + 4 > png_ptr->buffer_size) \
29
3.01k
   { png_push_save_buffer(png_ptr); return; }
30
3.67k
#define PNG_PUSH_SAVE_BUFFER_IF_LT(N) \
31
3.67k
if (png_ptr->buffer_size < N) \
32
3.67k
   { 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
3.60k
{
53
3.60k
   if (png_ptr == NULL || info_ptr == NULL)
54
0
      return;
55
56
3.60k
   png_push_restore_buffer(png_ptr, buffer, buffer_size);
57
58
7.61k
   while (png_ptr->buffer_size)
59
4.00k
   {
60
4.00k
      png_process_some_data(png_ptr, info_ptr);
61
4.00k
   }
62
3.60k
}
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
4.00k
{
110
4.00k
   if (png_ptr == NULL)
111
0
      return;
112
113
4.00k
   switch (png_ptr->process_mode)
114
4.00k
   {
115
244
      case PNG_READ_SIG_MODE:
116
244
      {
117
244
         png_push_read_sig(png_ptr, info_ptr);
118
244
         break;
119
0
      }
120
121
3.26k
      case PNG_READ_CHUNK_MODE:
122
3.26k
      {
123
3.26k
         png_push_read_chunk(png_ptr, info_ptr);
124
3.26k
         break;
125
0
      }
126
127
500
      case PNG_READ_IDAT_MODE:
128
500
      {
129
500
         png_push_read_IDAT(png_ptr);
130
500
         break;
131
0
      }
132
133
0
      default:
134
0
      {
135
0
         png_ptr->buffer_size = 0;
136
0
         break;
137
0
      }
138
4.00k
   }
139
4.00k
}
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
244
{
150
244
   size_t num_checked = png_ptr->sig_bytes; /* SAFE, does not exceed 8 */
151
244
   size_t num_to_check = 8 - num_checked;
152
153
244
   if (png_ptr->buffer_size < num_to_check)
154
0
   {
155
0
      num_to_check = png_ptr->buffer_size;
156
0
   }
157
158
244
   png_push_fill_buffer(png_ptr, &(info_ptr->signature[num_checked]),
159
244
       num_to_check);
160
244
   png_ptr->sig_bytes = (png_byte)(png_ptr->sig_bytes + num_to_check);
161
162
244
   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
244
   else
172
244
   {
173
244
      if (png_ptr->sig_bytes >= 8)
174
244
      {
175
244
         png_ptr->process_mode = PNG_READ_CHUNK_MODE;
176
244
      }
177
244
   }
178
244
}
179
180
void /* PRIVATE */
181
png_push_read_chunk(png_structrp png_ptr, png_inforp info_ptr)
182
3.26k
{
183
3.26k
   png_uint_32 chunk_name;
184
3.26k
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
185
3.26k
   int keep; /* unknown handling method */
186
3.26k
#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
3.26k
   if ((png_ptr->mode & PNG_HAVE_CHUNK_HEADER) == 0)
195
3.09k
   {
196
3.09k
      png_byte chunk_length[4];
197
3.09k
      png_byte chunk_tag[4];
198
199
3.09k
      PNG_PUSH_SAVE_BUFFER_IF_LT(8)
200
3.09k
      png_push_fill_buffer(png_ptr, chunk_length, 4);
201
3.09k
      png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
202
3.09k
      png_reset_crc(png_ptr);
203
3.09k
      png_crc_read(png_ptr, chunk_tag, 4);
204
3.09k
      png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(chunk_tag);
205
3.09k
      png_check_chunk_name(png_ptr, png_ptr->chunk_name);
206
3.09k
      png_check_chunk_length(png_ptr, png_ptr->push_length);
207
3.09k
      png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
208
3.09k
   }
209
210
3.26k
   chunk_name = png_ptr->chunk_name;
211
212
3.26k
   if (chunk_name == png_IDAT)
213
244
   {
214
244
      if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
215
5
         png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
216
217
      /* If we reach an IDAT chunk, this means we have read all of the
218
       * header chunks, and we can start reading the image (or if this
219
       * is called after the image has been read - we have an error).
220
       */
221
244
      if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
222
0
         png_error(png_ptr, "Missing IHDR before IDAT");
223
224
244
      else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
225
116
          (png_ptr->mode & PNG_HAVE_PLTE) == 0)
226
0
         png_error(png_ptr, "Missing PLTE before IDAT");
227
228
244
      png_ptr->process_mode = PNG_READ_IDAT_MODE;
229
230
244
      if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
231
5
         if ((png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) == 0)
232
0
            if (png_ptr->push_length == 0)
233
0
               return;
234
235
244
      png_ptr->mode |= PNG_HAVE_IDAT;
236
237
244
      if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
238
5
         png_benign_error(png_ptr, "Too many IDATs found");
239
244
   }
240
241
3.26k
   if (chunk_name == png_IHDR)
242
244
   {
243
244
      if (png_ptr->push_length != 13)
244
0
         png_error(png_ptr, "Invalid IHDR length");
245
246
244
      PNG_PUSH_SAVE_BUFFER_IF_FULL
247
244
      png_handle_IHDR(png_ptr, info_ptr, png_ptr->push_length);
248
244
   }
249
250
3.01k
   else if (chunk_name == png_IEND)
251
29
   {
252
29
      PNG_PUSH_SAVE_BUFFER_IF_FULL
253
29
      png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length);
254
255
29
      png_ptr->process_mode = PNG_READ_DONE_MODE;
256
29
      png_push_have_end(png_ptr, info_ptr);
257
29
   }
258
259
2.98k
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
260
2.98k
   else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
261
0
   {
262
0
      PNG_PUSH_SAVE_BUFFER_IF_FULL
263
0
      png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length, keep);
264
265
0
      if (chunk_name == png_PLTE)
266
0
         png_ptr->mode |= PNG_HAVE_PLTE;
267
0
   }
268
2.98k
#endif
269
270
2.98k
   else if (chunk_name == png_PLTE)
271
181
   {
272
181
      PNG_PUSH_SAVE_BUFFER_IF_FULL
273
181
      png_handle_PLTE(png_ptr, info_ptr, png_ptr->push_length);
274
181
   }
275
276
2.80k
   else if (chunk_name == png_IDAT)
277
244
   {
278
244
      png_ptr->idat_size = png_ptr->push_length;
279
244
      png_ptr->process_mode = PNG_READ_IDAT_MODE;
280
244
      png_push_have_info(png_ptr, info_ptr);
281
244
      png_ptr->zstream.avail_out =
282
244
          (uInt) PNG_ROWBYTES(png_ptr->pixel_depth,
283
244
          png_ptr->iwidth) + 1;
284
244
      png_ptr->zstream.next_out = png_ptr->row_buf;
285
244
      return;
286
244
   }
287
288
2.56k
#ifdef PNG_READ_gAMA_SUPPORTED
289
2.56k
   else if (png_ptr->chunk_name == png_gAMA)
290
8
   {
291
8
      PNG_PUSH_SAVE_BUFFER_IF_FULL
292
8
      png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length);
293
8
   }
294
295
2.55k
#endif
296
2.55k
#ifdef PNG_READ_sBIT_SUPPORTED
297
2.55k
   else if (png_ptr->chunk_name == png_sBIT)
298
14
   {
299
14
      PNG_PUSH_SAVE_BUFFER_IF_FULL
300
14
      png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length);
301
14
   }
302
303
2.54k
#endif
304
2.54k
#ifdef PNG_READ_cHRM_SUPPORTED
305
2.54k
   else if (png_ptr->chunk_name == png_cHRM)
306
53
   {
307
53
      PNG_PUSH_SAVE_BUFFER_IF_FULL
308
53
      png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length);
309
53
   }
310
311
2.48k
#endif
312
2.48k
#ifdef PNG_READ_cICP_SUPPORTED
313
2.48k
   else if (png_ptr->chunk_name == png_cICP)
314
0
   {
315
0
      PNG_PUSH_SAVE_BUFFER_IF_FULL
316
0
      png_handle_cICP(png_ptr, info_ptr, png_ptr->push_length);
317
0
   }
318
319
2.48k
#endif
320
2.48k
#ifdef PNG_READ_eXIf_SUPPORTED
321
2.48k
   else if (png_ptr->chunk_name == png_eXIf)
322
20
   {
323
20
      PNG_PUSH_SAVE_BUFFER_IF_FULL
324
20
      png_handle_eXIf(png_ptr, info_ptr, png_ptr->push_length);
325
20
   }
326
327
2.46k
#endif
328
2.46k
#ifdef PNG_READ_sRGB_SUPPORTED
329
2.46k
   else if (chunk_name == png_sRGB)
330
19
   {
331
19
      PNG_PUSH_SAVE_BUFFER_IF_FULL
332
19
      png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length);
333
19
   }
334
335
2.45k
#endif
336
2.45k
#ifdef PNG_READ_iCCP_SUPPORTED
337
2.45k
   else if (png_ptr->chunk_name == png_iCCP)
338
1
   {
339
1
      PNG_PUSH_SAVE_BUFFER_IF_FULL
340
1
      png_handle_iCCP(png_ptr, info_ptr, png_ptr->push_length);
341
1
   }
342
343
2.44k
#endif
344
2.44k
#ifdef PNG_READ_sPLT_SUPPORTED
345
2.44k
   else if (chunk_name == png_sPLT)
346
3
   {
347
3
      PNG_PUSH_SAVE_BUFFER_IF_FULL
348
3
      png_handle_sPLT(png_ptr, info_ptr, png_ptr->push_length);
349
3
   }
350
351
2.44k
#endif
352
2.44k
#ifdef PNG_READ_tRNS_SUPPORTED
353
2.44k
   else if (chunk_name == png_tRNS)
354
154
   {
355
154
      PNG_PUSH_SAVE_BUFFER_IF_FULL
356
154
      png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length);
357
154
   }
358
359
2.29k
#endif
360
2.29k
#ifdef PNG_READ_bKGD_SUPPORTED
361
2.29k
   else if (chunk_name == png_bKGD)
362
110
   {
363
110
      PNG_PUSH_SAVE_BUFFER_IF_FULL
364
110
      png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length);
365
110
   }
366
367
2.18k
#endif
368
2.18k
#ifdef PNG_READ_hIST_SUPPORTED
369
2.18k
   else if (chunk_name == png_hIST)
370
0
   {
371
0
      PNG_PUSH_SAVE_BUFFER_IF_FULL
372
0
      png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length);
373
0
   }
374
375
2.18k
#endif
376
2.18k
#ifdef PNG_READ_pHYs_SUPPORTED
377
2.18k
   else if (chunk_name == png_pHYs)
378
64
   {
379
64
      PNG_PUSH_SAVE_BUFFER_IF_FULL
380
64
      png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length);
381
64
   }
382
383
2.11k
#endif
384
2.11k
#ifdef PNG_READ_oFFs_SUPPORTED
385
2.11k
   else if (chunk_name == png_oFFs)
386
130
   {
387
130
      PNG_PUSH_SAVE_BUFFER_IF_FULL
388
130
      png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length);
389
130
   }
390
1.98k
#endif
391
392
1.98k
#ifdef PNG_READ_pCAL_SUPPORTED
393
1.98k
   else if (chunk_name == png_pCAL)
394
17
   {
395
17
      PNG_PUSH_SAVE_BUFFER_IF_FULL
396
17
      png_handle_pCAL(png_ptr, info_ptr, png_ptr->push_length);
397
17
   }
398
399
1.97k
#endif
400
1.97k
#ifdef PNG_READ_sCAL_SUPPORTED
401
1.97k
   else if (chunk_name == png_sCAL)
402
7
   {
403
7
      PNG_PUSH_SAVE_BUFFER_IF_FULL
404
7
      png_handle_sCAL(png_ptr, info_ptr, png_ptr->push_length);
405
7
   }
406
407
1.96k
#endif
408
1.96k
#ifdef PNG_READ_tIME_SUPPORTED
409
1.96k
   else if (chunk_name == png_tIME)
410
5
   {
411
5
      PNG_PUSH_SAVE_BUFFER_IF_FULL
412
5
      png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length);
413
5
   }
414
415
1.95k
#endif
416
1.95k
#ifdef PNG_READ_tEXt_SUPPORTED
417
1.95k
   else if (chunk_name == png_tEXt)
418
34
   {
419
34
      PNG_PUSH_SAVE_BUFFER_IF_FULL
420
34
      png_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length);
421
34
   }
422
423
1.92k
#endif
424
1.92k
#ifdef PNG_READ_zTXt_SUPPORTED
425
1.92k
   else if (chunk_name == png_zTXt)
426
14
   {
427
14
      PNG_PUSH_SAVE_BUFFER_IF_FULL
428
14
      png_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length);
429
14
   }
430
431
1.91k
#endif
432
1.91k
#ifdef PNG_READ_iTXt_SUPPORTED
433
1.91k
   else if (chunk_name == png_iTXt)
434
198
   {
435
198
      PNG_PUSH_SAVE_BUFFER_IF_FULL
436
198
      png_handle_iTXt(png_ptr, info_ptr, png_ptr->push_length);
437
198
   }
438
1.71k
#endif
439
440
1.71k
   else
441
1.71k
   {
442
1.71k
      PNG_PUSH_SAVE_BUFFER_IF_FULL
443
1.71k
      png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length,
444
1.71k
          PNG_HANDLE_CHUNK_AS_DEFAULT);
445
1.71k
   }
446
447
3.01k
   png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
448
3.01k
}
449
450
void PNGCBAPI
451
png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, size_t length)
452
40.4k
{
453
40.4k
   png_bytep ptr;
454
455
40.4k
   if (png_ptr == NULL)
456
0
      return;
457
458
40.4k
   ptr = buffer;
459
40.4k
   if (png_ptr->save_buffer_size != 0)
460
0
   {
461
0
      size_t save_size;
462
463
0
      if (length < png_ptr->save_buffer_size)
464
0
         save_size = length;
465
466
0
      else
467
0
         save_size = png_ptr->save_buffer_size;
468
469
0
      memcpy(ptr, png_ptr->save_buffer_ptr, save_size);
470
0
      length -= save_size;
471
0
      ptr += save_size;
472
0
      png_ptr->buffer_size -= save_size;
473
0
      png_ptr->save_buffer_size -= save_size;
474
0
      png_ptr->save_buffer_ptr += save_size;
475
0
   }
476
40.4k
   if (length != 0 && png_ptr->current_buffer_size != 0)
477
40.3k
   {
478
40.3k
      size_t save_size;
479
480
40.3k
      if (length < png_ptr->current_buffer_size)
481
36.8k
         save_size = length;
482
483
3.53k
      else
484
3.53k
         save_size = png_ptr->current_buffer_size;
485
486
40.3k
      memcpy(ptr, png_ptr->current_buffer_ptr, save_size);
487
40.3k
      png_ptr->buffer_size -= save_size;
488
40.3k
      png_ptr->current_buffer_size -= save_size;
489
40.3k
      png_ptr->current_buffer_ptr += save_size;
490
40.3k
   }
491
40.4k
}
492
493
void /* PRIVATE */
494
png_push_save_buffer(png_structrp png_ptr)
495
0
{
496
0
   if (png_ptr->save_buffer_size != 0)
497
0
   {
498
0
      if (png_ptr->save_buffer_ptr != png_ptr->save_buffer)
499
0
      {
500
0
         size_t i, istop;
501
0
         png_bytep sp;
502
0
         png_bytep dp;
503
504
0
         istop = png_ptr->save_buffer_size;
505
0
         for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer;
506
0
             i < istop; i++, sp++, dp++)
507
0
         {
508
0
            *dp = *sp;
509
0
         }
510
0
      }
511
0
   }
512
0
   if (png_ptr->save_buffer_size + png_ptr->current_buffer_size >
513
0
       png_ptr->save_buffer_max)
514
0
   {
515
0
      size_t new_max;
516
0
      png_bytep old_buffer;
517
518
0
      if (png_ptr->save_buffer_size > PNG_SIZE_MAX -
519
0
          (png_ptr->current_buffer_size + 256))
520
0
      {
521
0
         png_error(png_ptr, "Potential overflow of save_buffer");
522
0
      }
523
524
0
      new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;
525
0
      old_buffer = png_ptr->save_buffer;
526
0
      png_ptr->save_buffer = (png_bytep)png_malloc_warn(png_ptr,
527
0
          (size_t)new_max);
528
529
0
      if (png_ptr->save_buffer == NULL)
530
0
      {
531
0
         png_free(png_ptr, old_buffer);
532
0
         png_error(png_ptr, "Insufficient memory for save_buffer");
533
0
      }
534
535
0
      if (old_buffer)
536
0
         memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
537
0
      else if (png_ptr->save_buffer_size)
538
0
         png_error(png_ptr, "save_buffer error");
539
0
      png_free(png_ptr, old_buffer);
540
0
      png_ptr->save_buffer_max = new_max;
541
0
   }
542
0
   if (png_ptr->current_buffer_size)
543
0
   {
544
0
      memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
545
0
         png_ptr->current_buffer_ptr, png_ptr->current_buffer_size);
546
0
      png_ptr->save_buffer_size += png_ptr->current_buffer_size;
547
0
      png_ptr->current_buffer_size = 0;
548
0
   }
549
0
   png_ptr->save_buffer_ptr = png_ptr->save_buffer;
550
0
   png_ptr->buffer_size = 0;
551
0
}
552
553
void /* PRIVATE */
554
png_push_restore_buffer(png_structrp png_ptr, png_bytep buffer,
555
    size_t buffer_length)
556
3.60k
{
557
3.60k
   png_ptr->current_buffer = buffer;
558
3.60k
   png_ptr->current_buffer_size = buffer_length;
559
3.60k
   png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size;
560
3.60k
   png_ptr->current_buffer_ptr = png_ptr->current_buffer;
561
3.60k
}
562
563
void /* PRIVATE */
564
png_push_read_IDAT(png_structrp png_ptr)
565
500
{
566
500
   if ((png_ptr->mode & PNG_HAVE_CHUNK_HEADER) == 0)
567
261
   {
568
261
      png_byte chunk_length[4];
569
261
      png_byte chunk_tag[4];
570
571
      /* TODO: this code can be commoned up with the same code in push_read */
572
261
      PNG_PUSH_SAVE_BUFFER_IF_LT(8)
573
261
      png_push_fill_buffer(png_ptr, chunk_length, 4);
574
261
      png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
575
261
      png_reset_crc(png_ptr);
576
261
      png_crc_read(png_ptr, chunk_tag, 4);
577
261
      png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(chunk_tag);
578
261
      png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
579
580
261
      if (png_ptr->chunk_name != png_IDAT)
581
176
      {
582
176
         png_ptr->process_mode = PNG_READ_CHUNK_MODE;
583
584
176
         if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
585
13
            png_error(png_ptr, "Not enough compressed data");
586
587
163
         return;
588
176
      }
589
590
85
      png_ptr->idat_size = png_ptr->push_length;
591
85
   }
592
593
324
   if (png_ptr->idat_size != 0 && png_ptr->save_buffer_size != 0)
594
0
   {
595
0
      size_t save_size = png_ptr->save_buffer_size;
596
0
      png_uint_32 idat_size = png_ptr->idat_size;
597
598
      /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
599
       * are of different types and we don't know which variable has the fewest
600
       * bits.  Carefully select the smaller and cast it to the type of the
601
       * larger - this cannot overflow.  Do not cast in the following test - it
602
       * will break on either 16-bit or 64-bit platforms.
603
       */
604
0
      if (idat_size < save_size)
605
0
         save_size = (size_t)idat_size;
606
607
0
      else
608
0
         idat_size = (png_uint_32)save_size;
609
610
0
      png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
611
612
0
      png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size);
613
614
0
      png_ptr->idat_size -= idat_size;
615
0
      png_ptr->buffer_size -= save_size;
616
0
      png_ptr->save_buffer_size -= save_size;
617
0
      png_ptr->save_buffer_ptr += save_size;
618
0
   }
619
620
324
   if (png_ptr->idat_size != 0 && png_ptr->current_buffer_size != 0)
621
324
   {
622
324
      size_t save_size = png_ptr->current_buffer_size;
623
324
      png_uint_32 idat_size = png_ptr->idat_size;
624
625
      /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
626
       * are of different types and we don't know which variable has the fewest
627
       * bits.  Carefully select the smaller and cast it to the type of the
628
       * larger - this cannot overflow.
629
       */
630
324
      if (idat_size < save_size)
631
324
         save_size = (size_t)idat_size;
632
633
0
      else
634
0
         idat_size = (png_uint_32)save_size;
635
636
324
      png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
637
638
324
      png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size);
639
640
324
      png_ptr->idat_size -= idat_size;
641
324
      png_ptr->buffer_size -= save_size;
642
324
      png_ptr->current_buffer_size -= save_size;
643
324
      png_ptr->current_buffer_ptr += save_size;
644
324
   }
645
646
324
   if (png_ptr->idat_size == 0)
647
311
   {
648
311
      PNG_PUSH_SAVE_BUFFER_IF_LT(4)
649
311
      png_crc_finish(png_ptr, 0);
650
311
      png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
651
311
      png_ptr->mode |= PNG_AFTER_IDAT;
652
311
      png_ptr->zowner = 0;
653
311
   }
654
324
}
655
656
void /* PRIVATE */
657
png_process_IDAT_data(png_structrp png_ptr, png_bytep buffer,
658
    size_t buffer_length)
659
324
{
660
   /* The caller checks for a non-zero buffer length. */
661
324
   if (!(buffer_length > 0) || buffer == NULL)
662
0
      png_error(png_ptr, "No IDAT data (internal error)");
663
664
   /* This routine must process all the data it has been given
665
    * before returning, calling the row callback as required to
666
    * handle the uncompressed results.
667
    */
668
324
   png_ptr->zstream.next_in = buffer;
669
   /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
670
324
   png_ptr->zstream.avail_in = (uInt)buffer_length;
671
672
   /* Keep going until the decompressed data is all processed
673
    * or the stream marked as finished.
674
    */
675
1.59k
   while (png_ptr->zstream.avail_in > 0 &&
676
1.54k
      (png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
677
1.46k
   {
678
1.46k
      int ret;
679
680
      /* We have data for zlib, but we must check that zlib
681
       * has someplace to put the results.  It doesn't matter
682
       * if we don't expect any results -- it may be the input
683
       * data is just the LZ end code.
684
       */
685
1.46k
      if (!(png_ptr->zstream.avail_out > 0))
686
1.21k
      {
687
         /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
688
1.21k
         png_ptr->zstream.avail_out = (uInt)(PNG_ROWBYTES(png_ptr->pixel_depth,
689
1.21k
             png_ptr->iwidth) + 1);
690
691
1.21k
         png_ptr->zstream.next_out = png_ptr->row_buf;
692
1.21k
      }
693
694
      /* Using Z_SYNC_FLUSH here means that an unterminated
695
       * LZ stream (a stream with a missing end code) can still
696
       * be handled, otherwise (Z_NO_FLUSH) a future zlib
697
       * implementation might defer output and therefore
698
       * change the current behavior (see comments in inflate.c
699
       * for why this doesn't happen at present with zlib 1.2.5).
700
       */
701
1.46k
      ret = PNG_INFLATE(png_ptr, Z_SYNC_FLUSH);
702
703
      /* Check for any failure before proceeding. */
704
1.46k
      if (ret != Z_OK && ret != Z_STREAM_END)
705
186
      {
706
         /* Terminate the decompression. */
707
186
         png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
708
186
         png_ptr->zowner = 0;
709
710
         /* This may be a truncated stream (missing or
711
          * damaged end code).  Treat that as a warning.
712
          */
713
186
         if (png_ptr->row_number >= png_ptr->num_rows ||
714
184
             png_ptr->pass > 6)
715
2
            png_warning(png_ptr, "Truncated compressed data in IDAT");
716
717
184
         else
718
184
         {
719
184
            if (ret == Z_DATA_ERROR)
720
183
               png_benign_error(png_ptr, "IDAT: ADLER32 checksum mismatch");
721
1
            else
722
1
               png_error(png_ptr, "Decompression error in IDAT");
723
184
         }
724
725
         /* Skip the check on unprocessed input */
726
185
         return;
727
186
      }
728
729
      /* Did inflate output any data? */
730
1.27k
      if (png_ptr->zstream.next_out != png_ptr->row_buf)
731
1.25k
      {
732
         /* Is this unexpected data after the last row?
733
          * If it is, artificially terminate the LZ output
734
          * here.
735
          */
736
1.25k
         if (png_ptr->row_number >= png_ptr->num_rows ||
737
1.25k
             png_ptr->pass > 6)
738
2
         {
739
            /* Extra data. */
740
2
            png_warning(png_ptr, "Extra compressed data in IDAT");
741
2
            png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
742
2
            png_ptr->zowner = 0;
743
744
            /* Do no more processing; skip the unprocessed
745
             * input check below.
746
             */
747
2
            return;
748
2
         }
749
750
         /* Do we have a complete row? */
751
1.25k
         if (png_ptr->zstream.avail_out == 0)
752
1.24k
            png_push_process_row(png_ptr);
753
1.25k
      }
754
755
      /* And check for the end of the stream. */
756
1.27k
      if (ret == Z_STREAM_END)
757
5
         png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
758
1.27k
   }
759
760
   /* All the data should have been processed, if anything
761
    * is left at this point we have bytes of IDAT data
762
    * after the zlib end code.
763
    */
764
136
   if (png_ptr->zstream.avail_in > 0)
765
78
      png_warning(png_ptr, "Extra compression data in IDAT");
766
136
}
767
768
void /* PRIVATE */
769
png_push_process_row(png_structrp png_ptr)
770
1.24k
{
771
   /* 1.5.6: row_info moved out of png_struct to a local here. */
772
1.24k
   png_row_info row_info;
773
774
1.24k
   row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */
775
1.24k
   row_info.color_type = png_ptr->color_type;
776
1.24k
   row_info.bit_depth = png_ptr->bit_depth;
777
1.24k
   row_info.channels = png_ptr->channels;
778
1.24k
   row_info.pixel_depth = png_ptr->pixel_depth;
779
1.24k
   row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
780
781
1.24k
   if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
782
870
   {
783
870
      if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
784
858
         png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1,
785
858
            png_ptr->prev_row + 1, png_ptr->row_buf[0]);
786
12
      else
787
12
         png_error(png_ptr, "bad adaptive filter value");
788
870
   }
789
790
   /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
791
    * 1.5.6, while the buffer really is this big in current versions of libpng
792
    * it may not be in the future, so this was changed just to copy the
793
    * interlaced row count:
794
    */
795
1.22k
   memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
796
797
1.22k
#ifdef PNG_READ_TRANSFORMS_SUPPORTED
798
1.22k
   if (png_ptr->transformations != 0)
799
1.22k
      png_do_read_transformations(png_ptr, &row_info);
800
1.22k
#endif
801
802
   /* The transformed pixel depth should match the depth now in row_info. */
803
1.22k
   if (png_ptr->transformed_pixel_depth == 0)
804
53
   {
805
53
      png_ptr->transformed_pixel_depth = row_info.pixel_depth;
806
53
      if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
807
0
         png_error(png_ptr, "progressive row overflow");
808
53
   }
809
810
1.17k
   else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
811
0
      png_error(png_ptr, "internal progressive row size calculation error");
812
813
814
1.22k
#ifdef PNG_READ_INTERLACING_SUPPORTED
815
   /* Expand interlaced rows to full size */
816
1.22k
   if (png_ptr->interlaced != 0 &&
817
659
       (png_ptr->transformations & PNG_INTERLACE) != 0)
818
659
   {
819
659
      if (png_ptr->pass < 6)
820
639
         png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
821
639
             png_ptr->transformations);
822
823
659
      switch (png_ptr->pass)
824
659
      {
825
113
         case 0:
826
113
         {
827
113
            int i;
828
1.01k
            for (i = 0; i < 8 && png_ptr->pass == 0; i++)
829
897
            {
830
897
               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
831
897
               png_read_push_finish_row(png_ptr); /* Updates png_ptr->pass */
832
897
            }
833
834
113
            if (png_ptr->pass == 2) /* Pass 1 might be empty */
835
0
            {
836
0
               for (i = 0; i < 4 && png_ptr->pass == 2; i++)
837
0
               {
838
0
                  png_push_have_row(png_ptr, NULL);
839
0
                  png_read_push_finish_row(png_ptr);
840
0
               }
841
0
            }
842
843
113
            if (png_ptr->pass == 4 && png_ptr->height <= 4)
844
0
            {
845
0
               for (i = 0; i < 2 && png_ptr->pass == 4; i++)
846
0
               {
847
0
                  png_push_have_row(png_ptr, NULL);
848
0
                  png_read_push_finish_row(png_ptr);
849
0
               }
850
0
            }
851
852
113
            if (png_ptr->pass == 6 && png_ptr->height <= 4)
853
0
            {
854
0
                png_push_have_row(png_ptr, NULL);
855
0
                png_read_push_finish_row(png_ptr);
856
0
            }
857
858
113
            break;
859
0
         }
860
861
97
         case 1:
862
97
         {
863
97
            int i;
864
870
            for (i = 0; i < 8 && png_ptr->pass == 1; i++)
865
773
            {
866
773
               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
867
773
               png_read_push_finish_row(png_ptr);
868
773
            }
869
870
97
            if (png_ptr->pass == 2) /* Skip top 4 generated rows */
871
23
            {
872
115
               for (i = 0; i < 4 && png_ptr->pass == 2; i++)
873
92
               {
874
92
                  png_push_have_row(png_ptr, NULL);
875
92
                  png_read_push_finish_row(png_ptr);
876
92
               }
877
23
            }
878
879
97
            break;
880
0
         }
881
882
83
         case 2:
883
83
         {
884
83
            int i;
885
886
412
            for (i = 0; i < 4 && png_ptr->pass == 2; i++)
887
329
            {
888
329
               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
889
329
               png_read_push_finish_row(png_ptr);
890
329
            }
891
892
335
            for (i = 0; i < 4 && png_ptr->pass == 2; i++)
893
252
            {
894
252
               png_push_have_row(png_ptr, NULL);
895
252
               png_read_push_finish_row(png_ptr);
896
252
            }
897
898
83
            if (png_ptr->pass == 4) /* Pass 3 might be empty */
899
0
            {
900
0
               for (i = 0; i < 2 && png_ptr->pass == 4; i++)
901
0
               {
902
0
                  png_push_have_row(png_ptr, NULL);
903
0
                  png_read_push_finish_row(png_ptr);
904
0
               }
905
0
            }
906
907
83
            break;
908
0
         }
909
910
135
         case 3:
911
135
         {
912
135
            int i;
913
914
672
            for (i = 0; i < 4 && png_ptr->pass == 3; i++)
915
537
            {
916
537
               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
917
537
               png_read_push_finish_row(png_ptr);
918
537
            }
919
920
135
            if (png_ptr->pass == 4) /* Skip top two generated rows */
921
16
            {
922
48
               for (i = 0; i < 2 && png_ptr->pass == 4; i++)
923
32
               {
924
32
                  png_push_have_row(png_ptr, NULL);
925
32
                  png_read_push_finish_row(png_ptr);
926
32
               }
927
16
            }
928
929
135
            break;
930
0
         }
931
932
94
         case 4:
933
94
         {
934
94
            int i;
935
936
282
            for (i = 0; i < 2 && png_ptr->pass == 4; i++)
937
188
            {
938
188
               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
939
188
               png_read_push_finish_row(png_ptr);
940
188
            }
941
942
261
            for (i = 0; i < 2 && png_ptr->pass == 4; i++)
943
167
            {
944
167
               png_push_have_row(png_ptr, NULL);
945
167
               png_read_push_finish_row(png_ptr);
946
167
            }
947
948
94
            if (png_ptr->pass == 6) /* Pass 5 might be empty */
949
0
            {
950
0
               png_push_have_row(png_ptr, NULL);
951
0
               png_read_push_finish_row(png_ptr);
952
0
            }
953
954
94
            break;
955
0
         }
956
957
117
         case 5:
958
117
         {
959
117
            int i;
960
961
350
            for (i = 0; i < 2 && png_ptr->pass == 5; i++)
962
233
            {
963
233
               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
964
233
               png_read_push_finish_row(png_ptr);
965
233
            }
966
967
117
            if (png_ptr->pass == 6) /* Skip top generated row */
968
7
            {
969
7
               png_push_have_row(png_ptr, NULL);
970
7
               png_read_push_finish_row(png_ptr);
971
7
            }
972
973
117
            break;
974
0
         }
975
976
0
         default:
977
20
         case 6:
978
20
         {
979
20
            png_push_have_row(png_ptr, png_ptr->row_buf + 1);
980
20
            png_read_push_finish_row(png_ptr);
981
982
20
            if (png_ptr->pass != 6)
983
0
               break;
984
985
20
            png_push_have_row(png_ptr, NULL);
986
20
            png_read_push_finish_row(png_ptr);
987
20
         }
988
659
      }
989
659
   }
990
570
   else
991
570
#endif
992
570
   {
993
570
      png_push_have_row(png_ptr, png_ptr->row_buf + 1);
994
570
      png_read_push_finish_row(png_ptr);
995
570
   }
996
1.22k
}
997
998
void /* PRIVATE */
999
png_read_push_finish_row(png_structrp png_ptr)
1000
4.11k
{
1001
4.11k
   png_ptr->row_number++;
1002
4.11k
   if (png_ptr->row_number < png_ptr->num_rows)
1003
4.00k
      return;
1004
1005
114
#ifdef PNG_READ_INTERLACING_SUPPORTED
1006
114
   if (png_ptr->interlaced != 0)
1007
106
   {
1008
106
      png_ptr->row_number = 0;
1009
106
      memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
1010
1011
106
      do
1012
106
      {
1013
106
         png_ptr->pass++;
1014
106
         if ((png_ptr->pass == 1 && png_ptr->width < 5) ||
1015
106
             (png_ptr->pass == 3 && png_ptr->width < 3) ||
1016
106
             (png_ptr->pass == 5 && png_ptr->width < 2))
1017
0
            png_ptr->pass++;
1018
1019
106
         if (png_ptr->pass > 7)
1020
0
            png_ptr->pass--;
1021
1022
106
         if (png_ptr->pass >= 7)
1023
1
            break;
1024
1025
105
         png_ptr->iwidth = (png_ptr->width +
1026
105
             png_pass_inc[png_ptr->pass] - 1 -
1027
105
             png_pass_start[png_ptr->pass]) /
1028
105
             png_pass_inc[png_ptr->pass];
1029
1030
105
         if ((png_ptr->transformations & PNG_INTERLACE) != 0)
1031
105
            break;
1032
1033
0
         png_ptr->num_rows = (png_ptr->height +
1034
0
             png_pass_yinc[png_ptr->pass] - 1 -
1035
0
             png_pass_ystart[png_ptr->pass]) /
1036
0
             png_pass_yinc[png_ptr->pass];
1037
1038
0
      } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0);
1039
106
   }
1040
114
#endif /* READ_INTERLACING */
1041
114
}
1042
1043
void /* PRIVATE */
1044
png_push_have_info(png_structrp png_ptr, png_inforp info_ptr)
1045
244
{
1046
244
   if (png_ptr->info_fn != NULL)
1047
244
      (*(png_ptr->info_fn))(png_ptr, info_ptr);
1048
244
}
1049
1050
void /* PRIVATE */
1051
png_push_have_end(png_structrp png_ptr, png_inforp info_ptr)
1052
29
{
1053
29
   if (png_ptr->end_fn != NULL)
1054
0
      (*(png_ptr->end_fn))(png_ptr, info_ptr);
1055
29
}
1056
1057
void /* PRIVATE */
1058
png_push_have_row(png_structrp png_ptr, png_bytep row)
1059
4.11k
{
1060
4.11k
   if (png_ptr->row_fn != NULL)
1061
4.11k
      (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number,
1062
4.11k
          (int)png_ptr->pass);
1063
4.11k
}
1064
1065
#ifdef PNG_READ_INTERLACING_SUPPORTED
1066
void PNGAPI
1067
png_progressive_combine_row(png_const_structrp png_ptr, png_bytep old_row,
1068
    png_const_bytep new_row)
1069
4.11k
{
1070
4.11k
   if (png_ptr == NULL)
1071
0
      return;
1072
1073
   /* new_row is a flag here - if it is NULL then the app callback was called
1074
    * from an empty row (see the calls to png_struct::row_fn below), otherwise
1075
    * it must be png_ptr->row_buf+1
1076
    */
1077
4.11k
   if (new_row != NULL)
1078
3.54k
      png_combine_row(png_ptr, old_row, 1/*blocky display*/);
1079
4.11k
}
1080
#endif /* READ_INTERLACING */
1081
1082
void PNGAPI
1083
png_set_progressive_read_fn(png_structrp png_ptr, png_voidp progressive_ptr,
1084
    png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
1085
    png_progressive_end_ptr end_fn)
1086
244
{
1087
244
   if (png_ptr == NULL)
1088
0
      return;
1089
1090
244
   png_ptr->info_fn = info_fn;
1091
244
   png_ptr->row_fn = row_fn;
1092
244
   png_ptr->end_fn = end_fn;
1093
1094
244
   png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer);
1095
244
}
1096
1097
png_voidp PNGAPI
1098
png_get_progressive_ptr(png_const_structrp png_ptr)
1099
4.11k
{
1100
4.11k
   if (png_ptr == NULL)
1101
0
      return NULL;
1102
1103
4.11k
   return png_ptr->io_ptr;
1104
4.11k
}
1105
#endif /* PROGRESSIVE_READ */