Coverage Report

Created: 2026-03-12 06:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libpng-1.6.34/pngrutil.c
Line
Count
Source
1
2
/* pngrutil.c - utilities to read a PNG file
3
 *
4
 * Last changed in libpng 1.6.33 [September 28, 2017]
5
 * Copyright (c) 1998-2002,2004,2006-2017 Glenn Randers-Pehrson
6
 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7
 * (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
 *
13
 * This file contains routines that are only called from within
14
 * libpng itself during the course of reading an image.
15
 */
16
17
#include "pngpriv.h"
18
19
#ifdef PNG_READ_SUPPORTED
20
21
png_uint_32 PNGAPI
22
png_get_uint_31(png_const_structrp png_ptr, png_const_bytep buf)
23
0
{
24
0
   png_uint_32 uval = png_get_uint_32(buf);
25
26
0
   if (uval > PNG_UINT_31_MAX)
27
0
      png_error(png_ptr, "PNG unsigned integer out of range");
28
29
0
   return (uval);
30
0
}
31
32
#if defined(PNG_READ_gAMA_SUPPORTED) || defined(PNG_READ_cHRM_SUPPORTED)
33
/* The following is a variation on the above for use with the fixed
34
 * point values used for gAMA and cHRM.  Instead of png_error it
35
 * issues a warning and returns (-1) - an invalid value because both
36
 * gAMA and cHRM use *unsigned* integers for fixed point values.
37
 */
38
0
#define PNG_FIXED_ERROR (-1)
39
40
static png_fixed_point /* PRIVATE */
41
png_get_fixed_point(png_structrp png_ptr, png_const_bytep buf)
42
0
{
43
0
   png_uint_32 uval = png_get_uint_32(buf);
44
45
0
   if (uval <= PNG_UINT_31_MAX)
46
0
      return (png_fixed_point)uval; /* known to be in range */
47
48
   /* The caller can turn off the warning by passing NULL. */
49
0
   if (png_ptr != NULL)
50
0
      png_warning(png_ptr, "PNG fixed point integer out of range");
51
52
0
   return PNG_FIXED_ERROR;
53
0
}
54
#endif
55
56
#ifdef PNG_READ_INT_FUNCTIONS_SUPPORTED
57
/* NOTE: the read macros will obscure these definitions, so that if
58
 * PNG_USE_READ_MACROS is set the library will not use them internally,
59
 * but the APIs will still be available externally.
60
 *
61
 * The parentheses around "PNGAPI function_name" in the following three
62
 * functions are necessary because they allow the macros to co-exist with
63
 * these (unused but exported) functions.
64
 */
65
66
/* Grab an unsigned 32-bit integer from a buffer in big-endian format. */
67
png_uint_32 (PNGAPI
68
png_get_uint_32)(png_const_bytep buf)
69
0
{
70
0
   png_uint_32 uval =
71
0
       ((png_uint_32)(*(buf    )) << 24) +
72
0
       ((png_uint_32)(*(buf + 1)) << 16) +
73
0
       ((png_uint_32)(*(buf + 2)) <<  8) +
74
0
       ((png_uint_32)(*(buf + 3))      ) ;
75
76
0
   return uval;
77
0
}
78
79
/* Grab a signed 32-bit integer from a buffer in big-endian format.  The
80
 * data is stored in the PNG file in two's complement format and there
81
 * is no guarantee that a 'png_int_32' is exactly 32 bits, therefore
82
 * the following code does a two's complement to native conversion.
83
 */
84
png_int_32 (PNGAPI
85
png_get_int_32)(png_const_bytep buf)
86
0
{
87
0
   png_uint_32 uval = png_get_uint_32(buf);
88
0
   if ((uval & 0x80000000) == 0) /* non-negative */
89
0
      return (png_int_32)uval;
90
91
0
   uval = (uval ^ 0xffffffff) + 1;  /* 2's complement: -x = ~x+1 */
92
0
   if ((uval & 0x80000000) == 0) /* no overflow */
93
0
      return -(png_int_32)uval;
94
   /* The following has to be safe; this function only gets called on PNG data
95
    * and if we get here that data is invalid.  0 is the most safe value and
96
    * if not then an attacker would surely just generate a PNG with 0 instead.
97
    */
98
0
   return 0;
99
0
}
100
101
/* Grab an unsigned 16-bit integer from a buffer in big-endian format. */
102
png_uint_16 (PNGAPI
103
png_get_uint_16)(png_const_bytep buf)
104
0
{
105
   /* ANSI-C requires an int value to accomodate at least 16 bits so this
106
    * works and allows the compiler not to worry about possible narrowing
107
    * on 32-bit systems.  (Pre-ANSI systems did not make integers smaller
108
    * than 16 bits either.)
109
    */
110
0
   unsigned int val =
111
0
       ((unsigned int)(*buf) << 8) +
112
0
       ((unsigned int)(*(buf + 1)));
113
114
0
   return (png_uint_16)val;
115
0
}
116
117
#endif /* READ_INT_FUNCTIONS */
118
119
/* Read and check the PNG file signature */
120
void /* PRIVATE */
121
png_read_sig(png_structrp png_ptr, png_inforp info_ptr)
122
0
{
123
0
   png_size_t num_checked, num_to_check;
124
125
   /* Exit if the user application does not expect a signature. */
126
0
   if (png_ptr->sig_bytes >= 8)
127
0
      return;
128
129
0
   num_checked = png_ptr->sig_bytes;
130
0
   num_to_check = 8 - num_checked;
131
132
0
#ifdef PNG_IO_STATE_SUPPORTED
133
0
   png_ptr->io_state = PNG_IO_READING | PNG_IO_SIGNATURE;
134
0
#endif
135
136
   /* The signature must be serialized in a single I/O call. */
137
0
   png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
138
0
   png_ptr->sig_bytes = 8;
139
140
0
   if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check) != 0)
141
0
   {
142
0
      if (num_checked < 4 &&
143
0
          png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
144
0
         png_error(png_ptr, "Not a PNG file");
145
0
      else
146
0
         png_error(png_ptr, "PNG file corrupted by ASCII conversion");
147
0
   }
148
0
   if (num_checked < 3)
149
0
      png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
150
0
}
151
152
/* Read the chunk header (length + type name).
153
 * Put the type name into png_ptr->chunk_name, and return the length.
154
 */
155
png_uint_32 /* PRIVATE */
156
png_read_chunk_header(png_structrp png_ptr)
157
0
{
158
0
   png_byte buf[8];
159
0
   png_uint_32 length;
160
161
0
#ifdef PNG_IO_STATE_SUPPORTED
162
0
   png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_HDR;
163
0
#endif
164
165
   /* Read the length and the chunk name.
166
    * This must be performed in a single I/O call.
167
    */
168
0
   png_read_data(png_ptr, buf, 8);
169
0
   length = png_get_uint_31(png_ptr, buf);
170
171
   /* Put the chunk name into png_ptr->chunk_name. */
172
0
   png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(buf+4);
173
174
0
   png_debug2(0, "Reading %lx chunk, length = %lu",
175
0
       (unsigned long)png_ptr->chunk_name, (unsigned long)length);
176
177
   /* Reset the crc and run it over the chunk name. */
178
0
   png_reset_crc(png_ptr);
179
0
   png_calculate_crc(png_ptr, buf + 4, 4);
180
181
   /* Check to see if chunk name is valid. */
182
0
   png_check_chunk_name(png_ptr, png_ptr->chunk_name);
183
184
   /* Check for too-large chunk length */
185
0
   png_check_chunk_length(png_ptr, length);
186
187
0
#ifdef PNG_IO_STATE_SUPPORTED
188
0
   png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_DATA;
189
0
#endif
190
191
0
   return length;
192
0
}
193
194
/* Read data, and (optionally) run it through the CRC. */
195
void /* PRIVATE */
196
png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length)
197
0
{
198
0
   if (png_ptr == NULL)
199
0
      return;
200
201
0
   png_read_data(png_ptr, buf, length);
202
0
   png_calculate_crc(png_ptr, buf, length);
203
0
}
204
205
/* Optionally skip data and then check the CRC.  Depending on whether we
206
 * are reading an ancillary or critical chunk, and how the program has set
207
 * things up, we may calculate the CRC on the data and print a message.
208
 * Returns '1' if there was a CRC error, '0' otherwise.
209
 */
210
int /* PRIVATE */
211
png_crc_finish(png_structrp png_ptr, png_uint_32 skip)
212
0
{
213
   /* The size of the local buffer for inflate is a good guess as to a
214
    * reasonable size to use for buffering reads from the application.
215
    */
216
0
   while (skip > 0)
217
0
   {
218
0
      png_uint_32 len;
219
0
      png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];
220
221
0
      len = (sizeof tmpbuf);
222
0
      if (len > skip)
223
0
         len = skip;
224
0
      skip -= len;
225
226
0
      png_crc_read(png_ptr, tmpbuf, len);
227
0
   }
228
229
0
   if (png_crc_error(png_ptr) != 0)
230
0
   {
231
0
      if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) != 0 ?
232
0
          (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) == 0 :
233
0
          (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE) != 0)
234
0
      {
235
0
         png_chunk_warning(png_ptr, "CRC error");
236
0
      }
237
238
0
      else
239
0
         png_chunk_error(png_ptr, "CRC error");
240
241
0
      return (1);
242
0
   }
243
244
0
   return (0);
245
0
}
246
247
/* Compare the CRC stored in the PNG file with that calculated by libpng from
248
 * the data it has read thus far.
249
 */
250
int /* PRIVATE */
251
png_crc_error(png_structrp png_ptr)
252
0
{
253
0
   png_byte crc_bytes[4];
254
0
   png_uint_32 crc;
255
0
   int need_crc = 1;
256
257
0
   if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) != 0)
258
0
   {
259
0
      if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
260
0
          (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
261
0
         need_crc = 0;
262
0
   }
263
264
0
   else /* critical */
265
0
   {
266
0
      if ((png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) != 0)
267
0
         need_crc = 0;
268
0
   }
269
270
0
#ifdef PNG_IO_STATE_SUPPORTED
271
0
   png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_CRC;
272
0
#endif
273
274
   /* The chunk CRC must be serialized in a single I/O call. */
275
0
   png_read_data(png_ptr, crc_bytes, 4);
276
277
0
   if (need_crc != 0)
278
0
   {
279
0
      crc = png_get_uint_32(crc_bytes);
280
0
      return ((int)(crc != png_ptr->crc));
281
0
   }
282
283
0
   else
284
0
      return (0);
285
0
}
286
287
#if defined(PNG_READ_iCCP_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) ||\
288
    defined(PNG_READ_pCAL_SUPPORTED) || defined(PNG_READ_sCAL_SUPPORTED) ||\
289
    defined(PNG_READ_sPLT_SUPPORTED) || defined(PNG_READ_tEXt_SUPPORTED) ||\
290
    defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_SEQUENTIAL_READ_SUPPORTED)
291
/* Manage the read buffer; this simply reallocates the buffer if it is not small
292
 * enough (or if it is not allocated).  The routine returns a pointer to the
293
 * buffer; if an error occurs and 'warn' is set the routine returns NULL, else
294
 * it will call png_error (via png_malloc) on failure.  (warn == 2 means
295
 * 'silent').
296
 */
297
static png_bytep
298
png_read_buffer(png_structrp png_ptr, png_alloc_size_t new_size, int warn)
299
0
{
300
0
   png_bytep buffer = png_ptr->read_buffer;
301
302
0
   if (buffer != NULL && new_size > png_ptr->read_buffer_size)
303
0
   {
304
0
      png_ptr->read_buffer = NULL;
305
0
      png_ptr->read_buffer = NULL;
306
0
      png_ptr->read_buffer_size = 0;
307
0
      png_free(png_ptr, buffer);
308
0
      buffer = NULL;
309
0
   }
310
311
0
   if (buffer == NULL)
312
0
   {
313
0
      buffer = png_voidcast(png_bytep, png_malloc_base(png_ptr, new_size));
314
315
0
      if (buffer != NULL)
316
0
      {
317
0
         memset(buffer, 0, new_size); /* just in case */
318
0
         png_ptr->read_buffer = buffer;
319
0
         png_ptr->read_buffer_size = new_size;
320
0
      }
321
322
0
      else if (warn < 2) /* else silent */
323
0
      {
324
0
         if (warn != 0)
325
0
             png_chunk_warning(png_ptr, "insufficient memory to read chunk");
326
327
0
         else
328
0
             png_chunk_error(png_ptr, "insufficient memory to read chunk");
329
0
      }
330
0
   }
331
332
0
   return buffer;
333
0
}
334
#endif /* READ_iCCP|iTXt|pCAL|sCAL|sPLT|tEXt|zTXt|SEQUENTIAL_READ */
335
336
/* png_inflate_claim: claim the zstream for some nefarious purpose that involves
337
 * decompression.  Returns Z_OK on success, else a zlib error code.  It checks
338
 * the owner but, in final release builds, just issues a warning if some other
339
 * chunk apparently owns the stream.  Prior to release it does a png_error.
340
 */
341
static int
342
png_inflate_claim(png_structrp png_ptr, png_uint_32 owner)
343
0
{
344
0
   if (png_ptr->zowner != 0)
345
0
   {
346
0
      char msg[64];
347
348
0
      PNG_STRING_FROM_CHUNK(msg, png_ptr->zowner);
349
      /* So the message that results is "<chunk> using zstream"; this is an
350
       * internal error, but is very useful for debugging.  i18n requirements
351
       * are minimal.
352
       */
353
0
      (void)png_safecat(msg, (sizeof msg), 4, " using zstream");
354
0
#if PNG_RELEASE_BUILD
355
0
      png_chunk_warning(png_ptr, msg);
356
0
      png_ptr->zowner = 0;
357
#else
358
      png_chunk_error(png_ptr, msg);
359
#endif
360
0
   }
361
362
   /* Implementation note: unlike 'png_deflate_claim' this internal function
363
    * does not take the size of the data as an argument.  Some efficiency could
364
    * be gained by using this when it is known *if* the zlib stream itself does
365
    * not record the number; however, this is an illusion: the original writer
366
    * of the PNG may have selected a lower window size, and we really must
367
    * follow that because, for systems with with limited capabilities, we
368
    * would otherwise reject the application's attempts to use a smaller window
369
    * size (zlib doesn't have an interface to say "this or lower"!).
370
    *
371
    * inflateReset2 was added to zlib 1.2.4; before this the window could not be
372
    * reset, therefore it is necessary to always allocate the maximum window
373
    * size with earlier zlibs just in case later compressed chunks need it.
374
    */
375
0
   {
376
0
      int ret; /* zlib return code */
377
0
#if ZLIB_VERNUM >= 0x1240
378
0
      int window_bits = 0;
379
380
0
# if defined(PNG_SET_OPTION_SUPPORTED) && defined(PNG_MAXIMUM_INFLATE_WINDOW)
381
0
      if (((png_ptr->options >> PNG_MAXIMUM_INFLATE_WINDOW) & 3) ==
382
0
          PNG_OPTION_ON)
383
0
      {
384
0
         window_bits = 15;
385
0
         png_ptr->zstream_start = 0; /* fixed window size */
386
0
      }
387
388
0
      else
389
0
      {
390
0
         png_ptr->zstream_start = 1;
391
0
      }
392
0
# endif
393
394
0
#endif /* ZLIB_VERNUM >= 0x1240 */
395
396
      /* Set this for safety, just in case the previous owner left pointers to
397
       * memory allocations.
398
       */
399
0
      png_ptr->zstream.next_in = NULL;
400
0
      png_ptr->zstream.avail_in = 0;
401
0
      png_ptr->zstream.next_out = NULL;
402
0
      png_ptr->zstream.avail_out = 0;
403
404
0
      if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0)
405
0
      {
406
0
#if ZLIB_VERNUM >= 0x1240
407
0
         ret = inflateReset2(&png_ptr->zstream, window_bits);
408
#else
409
         ret = inflateReset(&png_ptr->zstream);
410
#endif
411
0
      }
412
413
0
      else
414
0
      {
415
0
#if ZLIB_VERNUM >= 0x1240
416
0
         ret = inflateInit2(&png_ptr->zstream, window_bits);
417
#else
418
         ret = inflateInit(&png_ptr->zstream);
419
#endif
420
421
0
         if (ret == Z_OK)
422
0
            png_ptr->flags |= PNG_FLAG_ZSTREAM_INITIALIZED;
423
0
      }
424
425
0
#if ZLIB_VERNUM >= 0x1290 && \
426
0
   defined(PNG_SET_OPTION_SUPPORTED) && defined(PNG_IGNORE_ADLER32)
427
0
      if (((png_ptr->options >> PNG_IGNORE_ADLER32) & 3) == PNG_OPTION_ON)
428
         /* Turn off validation of the ADLER32 checksum in IDAT chunks */
429
0
         ret = inflateValidate(&png_ptr->zstream, 0);
430
0
#endif
431
432
0
      if (ret == Z_OK)
433
0
         png_ptr->zowner = owner;
434
435
0
      else
436
0
         png_zstream_error(png_ptr, ret);
437
438
0
      return ret;
439
0
   }
440
441
#ifdef window_bits
442
# undef window_bits
443
#endif
444
0
}
445
446
#if ZLIB_VERNUM >= 0x1240
447
/* Handle the start of the inflate stream if we called inflateInit2(strm,0);
448
 * in this case some zlib versions skip validation of the CINFO field and, in
449
 * certain circumstances, libpng may end up displaying an invalid image, in
450
 * contrast to implementations that call zlib in the normal way (e.g. libpng
451
 * 1.5).
452
 */
453
int /* PRIVATE */
454
png_zlib_inflate(png_structrp png_ptr, int flush)
455
0
{
456
0
   if (png_ptr->zstream_start && png_ptr->zstream.avail_in > 0)
457
0
   {
458
0
      if ((*png_ptr->zstream.next_in >> 4) > 7)
459
0
      {
460
0
         png_ptr->zstream.msg = "invalid window size (libpng)";
461
0
         return Z_DATA_ERROR;
462
0
      }
463
464
0
      png_ptr->zstream_start = 0;
465
0
   }
466
467
0
   return inflate(&png_ptr->zstream, flush);
468
0
}
469
#endif /* Zlib >= 1.2.4 */
470
471
#ifdef PNG_READ_COMPRESSED_TEXT_SUPPORTED
472
#if defined(PNG_READ_zTXt_SUPPORTED) || defined (PNG_READ_iTXt_SUPPORTED)
473
/* png_inflate now returns zlib error codes including Z_OK and Z_STREAM_END to
474
 * allow the caller to do multiple calls if required.  If the 'finish' flag is
475
 * set Z_FINISH will be passed to the final inflate() call and Z_STREAM_END must
476
 * be returned or there has been a problem, otherwise Z_SYNC_FLUSH is used and
477
 * Z_OK or Z_STREAM_END will be returned on success.
478
 *
479
 * The input and output sizes are updated to the actual amounts of data consumed
480
 * or written, not the amount available (as in a z_stream).  The data pointers
481
 * are not changed, so the next input is (data+input_size) and the next
482
 * available output is (output+output_size).
483
 */
484
static int
485
png_inflate(png_structrp png_ptr, png_uint_32 owner, int finish,
486
    /* INPUT: */ png_const_bytep input, png_uint_32p input_size_ptr,
487
    /* OUTPUT: */ png_bytep output, png_alloc_size_t *output_size_ptr)
488
0
{
489
0
   if (png_ptr->zowner == owner) /* Else not claimed */
490
0
   {
491
0
      int ret;
492
0
      png_alloc_size_t avail_out = *output_size_ptr;
493
0
      png_uint_32 avail_in = *input_size_ptr;
494
495
      /* zlib can't necessarily handle more than 65535 bytes at once (i.e. it
496
       * can't even necessarily handle 65536 bytes) because the type uInt is
497
       * "16 bits or more".  Consequently it is necessary to chunk the input to
498
       * zlib.  This code uses ZLIB_IO_MAX, from pngpriv.h, as the maximum (the
499
       * maximum value that can be stored in a uInt.)  It is possible to set
500
       * ZLIB_IO_MAX to a lower value in pngpriv.h and this may sometimes have
501
       * a performance advantage, because it reduces the amount of data accessed
502
       * at each step and that may give the OS more time to page it in.
503
       */
504
0
      png_ptr->zstream.next_in = PNGZ_INPUT_CAST(input);
505
      /* avail_in and avail_out are set below from 'size' */
506
0
      png_ptr->zstream.avail_in = 0;
507
0
      png_ptr->zstream.avail_out = 0;
508
509
      /* Read directly into the output if it is available (this is set to
510
       * a local buffer below if output is NULL).
511
       */
512
0
      if (output != NULL)
513
0
         png_ptr->zstream.next_out = output;
514
515
0
      do
516
0
      {
517
0
         uInt avail;
518
0
         Byte local_buffer[PNG_INFLATE_BUF_SIZE];
519
520
         /* zlib INPUT BUFFER */
521
         /* The setting of 'avail_in' used to be outside the loop; by setting it
522
          * inside it is possible to chunk the input to zlib and simply rely on
523
          * zlib to advance the 'next_in' pointer.  This allows arbitrary
524
          * amounts of data to be passed through zlib at the unavoidable cost of
525
          * requiring a window save (memcpy of up to 32768 output bytes)
526
          * every ZLIB_IO_MAX input bytes.
527
          */
528
0
         avail_in += png_ptr->zstream.avail_in; /* not consumed last time */
529
530
0
         avail = ZLIB_IO_MAX;
531
532
0
         if (avail_in < avail)
533
0
            avail = (uInt)avail_in; /* safe: < than ZLIB_IO_MAX */
534
535
0
         avail_in -= avail;
536
0
         png_ptr->zstream.avail_in = avail;
537
538
         /* zlib OUTPUT BUFFER */
539
0
         avail_out += png_ptr->zstream.avail_out; /* not written last time */
540
541
0
         avail = ZLIB_IO_MAX; /* maximum zlib can process */
542
543
0
         if (output == NULL)
544
0
         {
545
            /* Reset the output buffer each time round if output is NULL and
546
             * make available the full buffer, up to 'remaining_space'
547
             */
548
0
            png_ptr->zstream.next_out = local_buffer;
549
0
            if ((sizeof local_buffer) < avail)
550
0
               avail = (sizeof local_buffer);
551
0
         }
552
553
0
         if (avail_out < avail)
554
0
            avail = (uInt)avail_out; /* safe: < ZLIB_IO_MAX */
555
556
0
         png_ptr->zstream.avail_out = avail;
557
0
         avail_out -= avail;
558
559
         /* zlib inflate call */
560
         /* In fact 'avail_out' may be 0 at this point, that happens at the end
561
          * of the read when the final LZ end code was not passed at the end of
562
          * the previous chunk of input data.  Tell zlib if we have reached the
563
          * end of the output buffer.
564
          */
565
0
         ret = PNG_INFLATE(png_ptr, avail_out > 0 ? Z_NO_FLUSH :
566
0
             (finish ? Z_FINISH : Z_SYNC_FLUSH));
567
0
      } while (ret == Z_OK);
568
569
      /* For safety kill the local buffer pointer now */
570
0
      if (output == NULL)
571
0
         png_ptr->zstream.next_out = NULL;
572
573
      /* Claw back the 'size' and 'remaining_space' byte counts. */
574
0
      avail_in += png_ptr->zstream.avail_in;
575
0
      avail_out += png_ptr->zstream.avail_out;
576
577
      /* Update the input and output sizes; the updated values are the amount
578
       * consumed or written, effectively the inverse of what zlib uses.
579
       */
580
0
      if (avail_out > 0)
581
0
         *output_size_ptr -= avail_out;
582
583
0
      if (avail_in > 0)
584
0
         *input_size_ptr -= avail_in;
585
586
      /* Ensure png_ptr->zstream.msg is set (even in the success case!) */
587
0
      png_zstream_error(png_ptr, ret);
588
0
      return ret;
589
0
   }
590
591
0
   else
592
0
   {
593
      /* This is a bad internal error.  The recovery assigns to the zstream msg
594
       * pointer, which is not owned by the caller, but this is safe; it's only
595
       * used on errors!
596
       */
597
0
      png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed");
598
0
      return Z_STREAM_ERROR;
599
0
   }
600
0
}
601
602
/*
603
 * Decompress trailing data in a chunk.  The assumption is that read_buffer
604
 * points at an allocated area holding the contents of a chunk with a
605
 * trailing compressed part.  What we get back is an allocated area
606
 * holding the original prefix part and an uncompressed version of the
607
 * trailing part (the malloc area passed in is freed).
608
 */
609
static int
610
png_decompress_chunk(png_structrp png_ptr,
611
    png_uint_32 chunklength, png_uint_32 prefix_size,
612
    png_alloc_size_t *newlength /* must be initialized to the maximum! */,
613
    int terminate /*add a '\0' to the end of the uncompressed data*/)
614
0
{
615
   /* TODO: implement different limits for different types of chunk.
616
    *
617
    * The caller supplies *newlength set to the maximum length of the
618
    * uncompressed data, but this routine allocates space for the prefix and
619
    * maybe a '\0' terminator too.  We have to assume that 'prefix_size' is
620
    * limited only by the maximum chunk size.
621
    */
622
0
   png_alloc_size_t limit = PNG_SIZE_MAX;
623
624
0
# ifdef PNG_SET_USER_LIMITS_SUPPORTED
625
0
   if (png_ptr->user_chunk_malloc_max > 0 &&
626
0
       png_ptr->user_chunk_malloc_max < limit)
627
0
      limit = png_ptr->user_chunk_malloc_max;
628
# elif PNG_USER_CHUNK_MALLOC_MAX > 0
629
   if (PNG_USER_CHUNK_MALLOC_MAX < limit)
630
      limit = PNG_USER_CHUNK_MALLOC_MAX;
631
# endif
632
633
0
   if (limit >= prefix_size + (terminate != 0))
634
0
   {
635
0
      int ret;
636
637
0
      limit -= prefix_size + (terminate != 0);
638
639
0
      if (limit < *newlength)
640
0
         *newlength = limit;
641
642
      /* Now try to claim the stream. */
643
0
      ret = png_inflate_claim(png_ptr, png_ptr->chunk_name);
644
645
0
      if (ret == Z_OK)
646
0
      {
647
0
         png_uint_32 lzsize = chunklength - prefix_size;
648
649
0
         ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/,
650
0
             /* input: */ png_ptr->read_buffer + prefix_size, &lzsize,
651
0
             /* output: */ NULL, newlength);
652
653
0
         if (ret == Z_STREAM_END)
654
0
         {
655
            /* Use 'inflateReset' here, not 'inflateReset2' because this
656
             * preserves the previously decided window size (otherwise it would
657
             * be necessary to store the previous window size.)  In practice
658
             * this doesn't matter anyway, because png_inflate will call inflate
659
             * with Z_FINISH in almost all cases, so the window will not be
660
             * maintained.
661
             */
662
0
            if (inflateReset(&png_ptr->zstream) == Z_OK)
663
0
            {
664
               /* Because of the limit checks above we know that the new,
665
                * expanded, size will fit in a size_t (let alone an
666
                * png_alloc_size_t).  Use png_malloc_base here to avoid an
667
                * extra OOM message.
668
                */
669
0
               png_alloc_size_t new_size = *newlength;
670
0
               png_alloc_size_t buffer_size = prefix_size + new_size +
671
0
                   (terminate != 0);
672
0
               png_bytep text = png_voidcast(png_bytep, png_malloc_base(png_ptr,
673
0
                   buffer_size));
674
675
0
               if (text != NULL)
676
0
               {
677
0
                  memset(text, 0, buffer_size);
678
679
0
                  ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/,
680
0
                      png_ptr->read_buffer + prefix_size, &lzsize,
681
0
                      text + prefix_size, newlength);
682
683
0
                  if (ret == Z_STREAM_END)
684
0
                  {
685
0
                     if (new_size == *newlength)
686
0
                     {
687
0
                        if (terminate != 0)
688
0
                           text[prefix_size + *newlength] = 0;
689
690
0
                        if (prefix_size > 0)
691
0
                           memcpy(text, png_ptr->read_buffer, prefix_size);
692
693
0
                        {
694
0
                           png_bytep old_ptr = png_ptr->read_buffer;
695
696
0
                           png_ptr->read_buffer = text;
697
0
                           png_ptr->read_buffer_size = buffer_size;
698
0
                           text = old_ptr; /* freed below */
699
0
                        }
700
0
                     }
701
702
0
                     else
703
0
                     {
704
                        /* The size changed on the second read, there can be no
705
                         * guarantee that anything is correct at this point.
706
                         * The 'msg' pointer has been set to "unexpected end of
707
                         * LZ stream", which is fine, but return an error code
708
                         * that the caller won't accept.
709
                         */
710
0
                        ret = PNG_UNEXPECTED_ZLIB_RETURN;
711
0
                     }
712
0
                  }
713
714
0
                  else if (ret == Z_OK)
715
0
                     ret = PNG_UNEXPECTED_ZLIB_RETURN; /* for safety */
716
717
                  /* Free the text pointer (this is the old read_buffer on
718
                   * success)
719
                   */
720
0
                  png_free(png_ptr, text);
721
722
                  /* This really is very benign, but it's still an error because
723
                   * the extra space may otherwise be used as a Trojan Horse.
724
                   */
725
0
                  if (ret == Z_STREAM_END &&
726
0
                      chunklength - prefix_size != lzsize)
727
0
                     png_chunk_benign_error(png_ptr, "extra compressed data");
728
0
               }
729
730
0
               else
731
0
               {
732
                  /* Out of memory allocating the buffer */
733
0
                  ret = Z_MEM_ERROR;
734
0
                  png_zstream_error(png_ptr, Z_MEM_ERROR);
735
0
               }
736
0
            }
737
738
0
            else
739
0
            {
740
               /* inflateReset failed, store the error message */
741
0
               png_zstream_error(png_ptr, ret);
742
0
               ret = PNG_UNEXPECTED_ZLIB_RETURN;
743
0
            }
744
0
         }
745
746
0
         else if (ret == Z_OK)
747
0
            ret = PNG_UNEXPECTED_ZLIB_RETURN;
748
749
         /* Release the claimed stream */
750
0
         png_ptr->zowner = 0;
751
0
      }
752
753
0
      else /* the claim failed */ if (ret == Z_STREAM_END) /* impossible! */
754
0
         ret = PNG_UNEXPECTED_ZLIB_RETURN;
755
756
0
      return ret;
757
0
   }
758
759
0
   else
760
0
   {
761
      /* Application/configuration limits exceeded */
762
0
      png_zstream_error(png_ptr, Z_MEM_ERROR);
763
0
      return Z_MEM_ERROR;
764
0
   }
765
0
}
766
#endif /* READ_zTXt || READ_iTXt */
767
#endif /* READ_COMPRESSED_TEXT */
768
769
#ifdef PNG_READ_iCCP_SUPPORTED
770
/* Perform a partial read and decompress, producing 'avail_out' bytes and
771
 * reading from the current chunk as required.
772
 */
773
static int
774
png_inflate_read(png_structrp png_ptr, png_bytep read_buffer, uInt read_size,
775
    png_uint_32p chunk_bytes, png_bytep next_out, png_alloc_size_t *out_size,
776
    int finish)
777
0
{
778
0
   if (png_ptr->zowner == png_ptr->chunk_name)
779
0
   {
780
0
      int ret;
781
782
      /* next_in and avail_in must have been initialized by the caller. */
783
0
      png_ptr->zstream.next_out = next_out;
784
0
      png_ptr->zstream.avail_out = 0; /* set in the loop */
785
786
0
      do
787
0
      {
788
0
         if (png_ptr->zstream.avail_in == 0)
789
0
         {
790
0
            if (read_size > *chunk_bytes)
791
0
               read_size = (uInt)*chunk_bytes;
792
0
            *chunk_bytes -= read_size;
793
794
0
            if (read_size > 0)
795
0
               png_crc_read(png_ptr, read_buffer, read_size);
796
797
0
            png_ptr->zstream.next_in = read_buffer;
798
0
            png_ptr->zstream.avail_in = read_size;
799
0
         }
800
801
0
         if (png_ptr->zstream.avail_out == 0)
802
0
         {
803
0
            uInt avail = ZLIB_IO_MAX;
804
0
            if (avail > *out_size)
805
0
               avail = (uInt)*out_size;
806
0
            *out_size -= avail;
807
808
0
            png_ptr->zstream.avail_out = avail;
809
0
         }
810
811
         /* Use Z_SYNC_FLUSH when there is no more chunk data to ensure that all
812
          * the available output is produced; this allows reading of truncated
813
          * streams.
814
          */
815
0
         ret = PNG_INFLATE(png_ptr, *chunk_bytes > 0 ?
816
0
             Z_NO_FLUSH : (finish ? Z_FINISH : Z_SYNC_FLUSH));
817
0
      }
818
0
      while (ret == Z_OK && (*out_size > 0 || png_ptr->zstream.avail_out > 0));
819
820
0
      *out_size += png_ptr->zstream.avail_out;
821
0
      png_ptr->zstream.avail_out = 0; /* Should not be required, but is safe */
822
823
      /* Ensure the error message pointer is always set: */
824
0
      png_zstream_error(png_ptr, ret);
825
0
      return ret;
826
0
   }
827
828
0
   else
829
0
   {
830
0
      png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed");
831
0
      return Z_STREAM_ERROR;
832
0
   }
833
0
}
834
#endif /* READ_iCCP */
835
836
/* Read and check the IDHR chunk */
837
838
void /* PRIVATE */
839
png_handle_IHDR(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
840
0
{
841
0
   png_byte buf[13];
842
0
   png_uint_32 width, height;
843
0
   int bit_depth, color_type, compression_type, filter_type;
844
0
   int interlace_type;
845
846
0
   png_debug(1, "in png_handle_IHDR");
847
848
0
   if ((png_ptr->mode & PNG_HAVE_IHDR) != 0)
849
0
      png_chunk_error(png_ptr, "out of place");
850
851
   /* Check the length */
852
0
   if (length != 13)
853
0
      png_chunk_error(png_ptr, "invalid");
854
855
0
   png_ptr->mode |= PNG_HAVE_IHDR;
856
857
0
   png_crc_read(png_ptr, buf, 13);
858
0
   png_crc_finish(png_ptr, 0);
859
860
0
   width = png_get_uint_31(png_ptr, buf);
861
0
   height = png_get_uint_31(png_ptr, buf + 4);
862
0
   bit_depth = buf[8];
863
0
   color_type = buf[9];
864
0
   compression_type = buf[10];
865
0
   filter_type = buf[11];
866
0
   interlace_type = buf[12];
867
868
   /* Set internal variables */
869
0
   png_ptr->width = width;
870
0
   png_ptr->height = height;
871
0
   png_ptr->bit_depth = (png_byte)bit_depth;
872
0
   png_ptr->interlaced = (png_byte)interlace_type;
873
0
   png_ptr->color_type = (png_byte)color_type;
874
0
#ifdef PNG_MNG_FEATURES_SUPPORTED
875
0
   png_ptr->filter_type = (png_byte)filter_type;
876
0
#endif
877
0
   png_ptr->compression_type = (png_byte)compression_type;
878
879
   /* Find number of channels */
880
0
   switch (png_ptr->color_type)
881
0
   {
882
0
      default: /* invalid, png_set_IHDR calls png_error */
883
0
      case PNG_COLOR_TYPE_GRAY:
884
0
      case PNG_COLOR_TYPE_PALETTE:
885
0
         png_ptr->channels = 1;
886
0
         break;
887
888
0
      case PNG_COLOR_TYPE_RGB:
889
0
         png_ptr->channels = 3;
890
0
         break;
891
892
0
      case PNG_COLOR_TYPE_GRAY_ALPHA:
893
0
         png_ptr->channels = 2;
894
0
         break;
895
896
0
      case PNG_COLOR_TYPE_RGB_ALPHA:
897
0
         png_ptr->channels = 4;
898
0
         break;
899
0
   }
900
901
   /* Set up other useful info */
902
0
   png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth * png_ptr->channels);
903
0
   png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width);
904
0
   png_debug1(3, "bit_depth = %d", png_ptr->bit_depth);
905
0
   png_debug1(3, "channels = %d", png_ptr->channels);
906
0
   png_debug1(3, "rowbytes = %lu", (unsigned long)png_ptr->rowbytes);
907
0
   png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
908
0
       color_type, interlace_type, compression_type, filter_type);
909
0
}
910
911
/* Read and check the palette */
912
void /* PRIVATE */
913
png_handle_PLTE(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
914
0
{
915
0
   png_color palette[PNG_MAX_PALETTE_LENGTH];
916
0
   int max_palette_length, num, i;
917
0
#ifdef PNG_POINTER_INDEXING_SUPPORTED
918
0
   png_colorp pal_ptr;
919
0
#endif
920
921
0
   png_debug(1, "in png_handle_PLTE");
922
923
0
   if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
924
0
      png_chunk_error(png_ptr, "missing IHDR");
925
926
   /* Moved to before the 'after IDAT' check below because otherwise duplicate
927
    * PLTE chunks are potentially ignored (the spec says there shall not be more
928
    * than one PLTE, the error is not treated as benign, so this check trumps
929
    * the requirement that PLTE appears before IDAT.)
930
    */
931
0
   else if ((png_ptr->mode & PNG_HAVE_PLTE) != 0)
932
0
      png_chunk_error(png_ptr, "duplicate");
933
934
0
   else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
935
0
   {
936
      /* This is benign because the non-benign error happened before, when an
937
       * IDAT was encountered in a color-mapped image with no PLTE.
938
       */
939
0
      png_crc_finish(png_ptr, length);
940
0
      png_chunk_benign_error(png_ptr, "out of place");
941
0
      return;
942
0
   }
943
944
0
   png_ptr->mode |= PNG_HAVE_PLTE;
945
946
0
   if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0)
947
0
   {
948
0
      png_crc_finish(png_ptr, length);
949
0
      png_chunk_benign_error(png_ptr, "ignored in grayscale PNG");
950
0
      return;
951
0
   }
952
953
#ifndef PNG_READ_OPT_PLTE_SUPPORTED
954
   if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
955
   {
956
      png_crc_finish(png_ptr, length);
957
      return;
958
   }
959
#endif
960
961
0
   if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3)
962
0
   {
963
0
      png_crc_finish(png_ptr, length);
964
965
0
      if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
966
0
         png_chunk_benign_error(png_ptr, "invalid");
967
968
0
      else
969
0
         png_chunk_error(png_ptr, "invalid");
970
971
0
      return;
972
0
   }
973
974
   /* The cast is safe because 'length' is less than 3*PNG_MAX_PALETTE_LENGTH */
975
0
   num = (int)length / 3;
976
977
   /* If the palette has 256 or fewer entries but is too large for the bit
978
    * depth, we don't issue an error, to preserve the behavior of previous
979
    * libpng versions. We silently truncate the unused extra palette entries
980
    * here.
981
    */
982
0
   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
983
0
      max_palette_length = (1 << png_ptr->bit_depth);
984
0
   else
985
0
      max_palette_length = PNG_MAX_PALETTE_LENGTH;
986
987
0
   if (num > max_palette_length)
988
0
      num = max_palette_length;
989
990
0
#ifdef PNG_POINTER_INDEXING_SUPPORTED
991
0
   for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)
992
0
   {
993
0
      png_byte buf[3];
994
995
0
      png_crc_read(png_ptr, buf, 3);
996
0
      pal_ptr->red = buf[0];
997
0
      pal_ptr->green = buf[1];
998
0
      pal_ptr->blue = buf[2];
999
0
   }
1000
#else
1001
   for (i = 0; i < num; i++)
1002
   {
1003
      png_byte buf[3];
1004
1005
      png_crc_read(png_ptr, buf, 3);
1006
      /* Don't depend upon png_color being any order */
1007
      palette[i].red = buf[0];
1008
      palette[i].green = buf[1];
1009
      palette[i].blue = buf[2];
1010
   }
1011
#endif
1012
1013
   /* If we actually need the PLTE chunk (ie for a paletted image), we do
1014
    * whatever the normal CRC configuration tells us.  However, if we
1015
    * have an RGB image, the PLTE can be considered ancillary, so
1016
    * we will act as though it is.
1017
    */
1018
#ifndef PNG_READ_OPT_PLTE_SUPPORTED
1019
   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1020
#endif
1021
0
   {
1022
0
      png_crc_finish(png_ptr, (png_uint_32) (length - (unsigned int)num * 3));
1023
0
   }
1024
1025
#ifndef PNG_READ_OPT_PLTE_SUPPORTED
1026
   else if (png_crc_error(png_ptr) != 0)  /* Only if we have a CRC error */
1027
   {
1028
      /* If we don't want to use the data from an ancillary chunk,
1029
       * we have two options: an error abort, or a warning and we
1030
       * ignore the data in this chunk (which should be OK, since
1031
       * it's considered ancillary for a RGB or RGBA image).
1032
       *
1033
       * IMPLEMENTATION NOTE: this is only here because png_crc_finish uses the
1034
       * chunk type to determine whether to check the ancillary or the critical
1035
       * flags.
1036
       */
1037
      if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE) == 0)
1038
      {
1039
         if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) != 0)
1040
            return;
1041
1042
         else
1043
            png_chunk_error(png_ptr, "CRC error");
1044
      }
1045
1046
      /* Otherwise, we (optionally) emit a warning and use the chunk. */
1047
      else if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) == 0)
1048
         png_chunk_warning(png_ptr, "CRC error");
1049
   }
1050
#endif
1051
1052
   /* TODO: png_set_PLTE has the side effect of setting png_ptr->palette to its
1053
    * own copy of the palette.  This has the side effect that when png_start_row
1054
    * is called (this happens after any call to png_read_update_info) the
1055
    * info_ptr palette gets changed.  This is extremely unexpected and
1056
    * confusing.
1057
    *
1058
    * Fix this by not sharing the palette in this way.
1059
    */
1060
0
   png_set_PLTE(png_ptr, info_ptr, palette, num);
1061
1062
   /* The three chunks, bKGD, hIST and tRNS *must* appear after PLTE and before
1063
    * IDAT.  Prior to 1.6.0 this was not checked; instead the code merely
1064
    * checked the apparent validity of a tRNS chunk inserted before PLTE on a
1065
    * palette PNG.  1.6.0 attempts to rigorously follow the standard and
1066
    * therefore does a benign error if the erroneous condition is detected *and*
1067
    * cancels the tRNS if the benign error returns.  The alternative is to
1068
    * amend the standard since it would be rather hypocritical of the standards
1069
    * maintainers to ignore it.
1070
    */
1071
0
#ifdef PNG_READ_tRNS_SUPPORTED
1072
0
   if (png_ptr->num_trans > 0 ||
1073
0
       (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS) != 0))
1074
0
   {
1075
      /* Cancel this because otherwise it would be used if the transforms
1076
       * require it.  Don't cancel the 'valid' flag because this would prevent
1077
       * detection of duplicate chunks.
1078
       */
1079
0
      png_ptr->num_trans = 0;
1080
1081
0
      if (info_ptr != NULL)
1082
0
         info_ptr->num_trans = 0;
1083
1084
0
      png_chunk_benign_error(png_ptr, "tRNS must be after");
1085
0
   }
1086
0
#endif
1087
1088
0
#ifdef PNG_READ_hIST_SUPPORTED
1089
0
   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) != 0)
1090
0
      png_chunk_benign_error(png_ptr, "hIST must be after");
1091
0
#endif
1092
1093
0
#ifdef PNG_READ_bKGD_SUPPORTED
1094
0
   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) != 0)
1095
0
      png_chunk_benign_error(png_ptr, "bKGD must be after");
1096
0
#endif
1097
0
}
1098
1099
void /* PRIVATE */
1100
png_handle_IEND(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1101
0
{
1102
0
   png_debug(1, "in png_handle_IEND");
1103
1104
0
   if ((png_ptr->mode & PNG_HAVE_IHDR) == 0 ||
1105
0
       (png_ptr->mode & PNG_HAVE_IDAT) == 0)
1106
0
      png_chunk_error(png_ptr, "out of place");
1107
1108
0
   png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND);
1109
1110
0
   png_crc_finish(png_ptr, length);
1111
1112
0
   if (length != 0)
1113
0
      png_chunk_benign_error(png_ptr, "invalid");
1114
1115
0
   PNG_UNUSED(info_ptr)
1116
0
}
1117
1118
#ifdef PNG_READ_gAMA_SUPPORTED
1119
void /* PRIVATE */
1120
png_handle_gAMA(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1121
0
{
1122
0
   png_fixed_point igamma;
1123
0
   png_byte buf[4];
1124
1125
0
   png_debug(1, "in png_handle_gAMA");
1126
1127
0
   if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1128
0
      png_chunk_error(png_ptr, "missing IHDR");
1129
1130
0
   else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
1131
0
   {
1132
0
      png_crc_finish(png_ptr, length);
1133
0
      png_chunk_benign_error(png_ptr, "out of place");
1134
0
      return;
1135
0
   }
1136
1137
0
   if (length != 4)
1138
0
   {
1139
0
      png_crc_finish(png_ptr, length);
1140
0
      png_chunk_benign_error(png_ptr, "invalid");
1141
0
      return;
1142
0
   }
1143
1144
0
   png_crc_read(png_ptr, buf, 4);
1145
1146
0
   if (png_crc_finish(png_ptr, 0) != 0)
1147
0
      return;
1148
1149
0
   igamma = png_get_fixed_point(NULL, buf);
1150
1151
0
   png_colorspace_set_gamma(png_ptr, &png_ptr->colorspace, igamma);
1152
0
   png_colorspace_sync(png_ptr, info_ptr);
1153
0
}
1154
#endif
1155
1156
#ifdef PNG_READ_sBIT_SUPPORTED
1157
void /* PRIVATE */
1158
png_handle_sBIT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1159
0
{
1160
0
   unsigned int truelen, i;
1161
0
   png_byte sample_depth;
1162
0
   png_byte buf[4];
1163
1164
0
   png_debug(1, "in png_handle_sBIT");
1165
1166
0
   if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1167
0
      png_chunk_error(png_ptr, "missing IHDR");
1168
1169
0
   else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
1170
0
   {
1171
0
      png_crc_finish(png_ptr, length);
1172
0
      png_chunk_benign_error(png_ptr, "out of place");
1173
0
      return;
1174
0
   }
1175
1176
0
   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT) != 0)
1177
0
   {
1178
0
      png_crc_finish(png_ptr, length);
1179
0
      png_chunk_benign_error(png_ptr, "duplicate");
1180
0
      return;
1181
0
   }
1182
1183
0
   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1184
0
   {
1185
0
      truelen = 3;
1186
0
      sample_depth = 8;
1187
0
   }
1188
1189
0
   else
1190
0
   {
1191
0
      truelen = png_ptr->channels;
1192
0
      sample_depth = png_ptr->bit_depth;
1193
0
   }
1194
1195
0
   if (length != truelen || length > 4)
1196
0
   {
1197
0
      png_chunk_benign_error(png_ptr, "invalid");
1198
0
      png_crc_finish(png_ptr, length);
1199
0
      return;
1200
0
   }
1201
1202
0
   buf[0] = buf[1] = buf[2] = buf[3] = sample_depth;
1203
0
   png_crc_read(png_ptr, buf, truelen);
1204
1205
0
   if (png_crc_finish(png_ptr, 0) != 0)
1206
0
      return;
1207
1208
0
   for (i=0; i<truelen; ++i)
1209
0
   {
1210
0
      if (buf[i] == 0 || buf[i] > sample_depth)
1211
0
      {
1212
0
         png_chunk_benign_error(png_ptr, "invalid");
1213
0
         return;
1214
0
      }
1215
0
   }
1216
1217
0
   if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
1218
0
   {
1219
0
      png_ptr->sig_bit.red = buf[0];
1220
0
      png_ptr->sig_bit.green = buf[1];
1221
0
      png_ptr->sig_bit.blue = buf[2];
1222
0
      png_ptr->sig_bit.alpha = buf[3];
1223
0
   }
1224
1225
0
   else
1226
0
   {
1227
0
      png_ptr->sig_bit.gray = buf[0];
1228
0
      png_ptr->sig_bit.red = buf[0];
1229
0
      png_ptr->sig_bit.green = buf[0];
1230
0
      png_ptr->sig_bit.blue = buf[0];
1231
0
      png_ptr->sig_bit.alpha = buf[1];
1232
0
   }
1233
1234
0
   png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
1235
0
}
1236
#endif
1237
1238
#ifdef PNG_READ_cHRM_SUPPORTED
1239
void /* PRIVATE */
1240
png_handle_cHRM(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1241
0
{
1242
0
   png_byte buf[32];
1243
0
   png_xy xy;
1244
1245
0
   png_debug(1, "in png_handle_cHRM");
1246
1247
0
   if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1248
0
      png_chunk_error(png_ptr, "missing IHDR");
1249
1250
0
   else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
1251
0
   {
1252
0
      png_crc_finish(png_ptr, length);
1253
0
      png_chunk_benign_error(png_ptr, "out of place");
1254
0
      return;
1255
0
   }
1256
1257
0
   if (length != 32)
1258
0
   {
1259
0
      png_crc_finish(png_ptr, length);
1260
0
      png_chunk_benign_error(png_ptr, "invalid");
1261
0
      return;
1262
0
   }
1263
1264
0
   png_crc_read(png_ptr, buf, 32);
1265
1266
0
   if (png_crc_finish(png_ptr, 0) != 0)
1267
0
      return;
1268
1269
0
   xy.whitex = png_get_fixed_point(NULL, buf);
1270
0
   xy.whitey = png_get_fixed_point(NULL, buf + 4);
1271
0
   xy.redx   = png_get_fixed_point(NULL, buf + 8);
1272
0
   xy.redy   = png_get_fixed_point(NULL, buf + 12);
1273
0
   xy.greenx = png_get_fixed_point(NULL, buf + 16);
1274
0
   xy.greeny = png_get_fixed_point(NULL, buf + 20);
1275
0
   xy.bluex  = png_get_fixed_point(NULL, buf + 24);
1276
0
   xy.bluey  = png_get_fixed_point(NULL, buf + 28);
1277
1278
0
   if (xy.whitex == PNG_FIXED_ERROR ||
1279
0
       xy.whitey == PNG_FIXED_ERROR ||
1280
0
       xy.redx   == PNG_FIXED_ERROR ||
1281
0
       xy.redy   == PNG_FIXED_ERROR ||
1282
0
       xy.greenx == PNG_FIXED_ERROR ||
1283
0
       xy.greeny == PNG_FIXED_ERROR ||
1284
0
       xy.bluex  == PNG_FIXED_ERROR ||
1285
0
       xy.bluey  == PNG_FIXED_ERROR)
1286
0
   {
1287
0
      png_chunk_benign_error(png_ptr, "invalid values");
1288
0
      return;
1289
0
   }
1290
1291
   /* If a colorspace error has already been output skip this chunk */
1292
0
   if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0)
1293
0
      return;
1294
1295
0
   if ((png_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM) != 0)
1296
0
   {
1297
0
      png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1298
0
      png_colorspace_sync(png_ptr, info_ptr);
1299
0
      png_chunk_benign_error(png_ptr, "duplicate");
1300
0
      return;
1301
0
   }
1302
1303
0
   png_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM;
1304
0
   (void)png_colorspace_set_chromaticities(png_ptr, &png_ptr->colorspace, &xy,
1305
0
       1/*prefer cHRM values*/);
1306
0
   png_colorspace_sync(png_ptr, info_ptr);
1307
0
}
1308
#endif
1309
1310
#ifdef PNG_READ_sRGB_SUPPORTED
1311
void /* PRIVATE */
1312
png_handle_sRGB(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1313
0
{
1314
0
   png_byte intent;
1315
1316
0
   png_debug(1, "in png_handle_sRGB");
1317
1318
0
   if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1319
0
      png_chunk_error(png_ptr, "missing IHDR");
1320
1321
0
   else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
1322
0
   {
1323
0
      png_crc_finish(png_ptr, length);
1324
0
      png_chunk_benign_error(png_ptr, "out of place");
1325
0
      return;
1326
0
   }
1327
1328
0
   if (length != 1)
1329
0
   {
1330
0
      png_crc_finish(png_ptr, length);
1331
0
      png_chunk_benign_error(png_ptr, "invalid");
1332
0
      return;
1333
0
   }
1334
1335
0
   png_crc_read(png_ptr, &intent, 1);
1336
1337
0
   if (png_crc_finish(png_ptr, 0) != 0)
1338
0
      return;
1339
1340
   /* If a colorspace error has already been output skip this chunk */
1341
0
   if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0)
1342
0
      return;
1343
1344
   /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect
1345
    * this.
1346
    */
1347
0
   if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) != 0)
1348
0
   {
1349
0
      png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1350
0
      png_colorspace_sync(png_ptr, info_ptr);
1351
0
      png_chunk_benign_error(png_ptr, "too many profiles");
1352
0
      return;
1353
0
   }
1354
1355
0
   (void)png_colorspace_set_sRGB(png_ptr, &png_ptr->colorspace, intent);
1356
0
   png_colorspace_sync(png_ptr, info_ptr);
1357
0
}
1358
#endif /* READ_sRGB */
1359
1360
#ifdef PNG_READ_iCCP_SUPPORTED
1361
void /* PRIVATE */
1362
png_handle_iCCP(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1363
/* Note: this does not properly handle profiles that are > 64K under DOS */
1364
0
{
1365
0
   png_const_charp errmsg = NULL; /* error message output, or no error */
1366
0
   int finished = 0; /* crc checked */
1367
1368
0
   png_debug(1, "in png_handle_iCCP");
1369
1370
0
   if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1371
0
      png_chunk_error(png_ptr, "missing IHDR");
1372
1373
0
   else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
1374
0
   {
1375
0
      png_crc_finish(png_ptr, length);
1376
0
      png_chunk_benign_error(png_ptr, "out of place");
1377
0
      return;
1378
0
   }
1379
1380
   /* Consistent with all the above colorspace handling an obviously *invalid*
1381
    * chunk is just ignored, so does not invalidate the color space.  An
1382
    * alternative is to set the 'invalid' flags at the start of this routine
1383
    * and only clear them in they were not set before and all the tests pass.
1384
    */
1385
1386
   /* The keyword must be at least one character and there is a
1387
    * terminator (0) byte and the compression method byte, and the
1388
    * 'zlib' datastream is at least 11 bytes.
1389
    */
1390
0
   if (length < 14)
1391
0
   {
1392
0
      png_crc_finish(png_ptr, length);
1393
0
      png_chunk_benign_error(png_ptr, "too short");
1394
0
      return;
1395
0
   }
1396
1397
   /* If a colorspace error has already been output skip this chunk */
1398
0
   if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0)
1399
0
   {
1400
0
      png_crc_finish(png_ptr, length);
1401
0
      return;
1402
0
   }
1403
1404
   /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect
1405
    * this.
1406
    */
1407
0
   if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) == 0)
1408
0
   {
1409
0
      uInt read_length, keyword_length;
1410
0
      char keyword[81];
1411
1412
      /* Find the keyword; the keyword plus separator and compression method
1413
       * bytes can be at most 81 characters long.
1414
       */
1415
0
      read_length = 81; /* maximum */
1416
0
      if (read_length > length)
1417
0
         read_length = (uInt)length;
1418
1419
0
      png_crc_read(png_ptr, (png_bytep)keyword, read_length);
1420
0
      length -= read_length;
1421
1422
      /* The minimum 'zlib' stream is assumed to be just the 2 byte header,
1423
       * 5 bytes minimum 'deflate' stream, and the 4 byte checksum.
1424
       */
1425
0
      if (length < 11)
1426
0
      {
1427
0
         png_crc_finish(png_ptr, length);
1428
0
         png_chunk_benign_error(png_ptr, "too short");
1429
0
         return;
1430
0
      }
1431
1432
0
      keyword_length = 0;
1433
0
      while (keyword_length < 80 && keyword_length < read_length &&
1434
0
         keyword[keyword_length] != 0)
1435
0
         ++keyword_length;
1436
1437
      /* TODO: make the keyword checking common */
1438
0
      if (keyword_length >= 1 && keyword_length <= 79)
1439
0
      {
1440
         /* We only understand '0' compression - deflate - so if we get a
1441
          * different value we can't safely decode the chunk.
1442
          */
1443
0
         if (keyword_length+1 < read_length &&
1444
0
            keyword[keyword_length+1] == PNG_COMPRESSION_TYPE_BASE)
1445
0
         {
1446
0
            read_length -= keyword_length+2;
1447
1448
0
            if (png_inflate_claim(png_ptr, png_iCCP) == Z_OK)
1449
0
            {
1450
0
               Byte profile_header[132]={0};
1451
0
               Byte local_buffer[PNG_INFLATE_BUF_SIZE];
1452
0
               png_alloc_size_t size = (sizeof profile_header);
1453
1454
0
               png_ptr->zstream.next_in = (Bytef*)keyword + (keyword_length+2);
1455
0
               png_ptr->zstream.avail_in = read_length;
1456
0
               (void)png_inflate_read(png_ptr, local_buffer,
1457
0
                   (sizeof local_buffer), &length, profile_header, &size,
1458
0
                   0/*finish: don't, because the output is too small*/);
1459
1460
0
               if (size == 0)
1461
0
               {
1462
                  /* We have the ICC profile header; do the basic header checks.
1463
                   */
1464
0
                  const png_uint_32 profile_length =
1465
0
                     png_get_uint_32(profile_header);
1466
1467
0
                  if (png_icc_check_length(png_ptr, &png_ptr->colorspace,
1468
0
                      keyword, profile_length) != 0)
1469
0
                  {
1470
                     /* The length is apparently ok, so we can check the 132
1471
                      * byte header.
1472
                      */
1473
0
                     if (png_icc_check_header(png_ptr, &png_ptr->colorspace,
1474
0
                         keyword, profile_length, profile_header,
1475
0
                         png_ptr->color_type) != 0)
1476
0
                     {
1477
                        /* Now read the tag table; a variable size buffer is
1478
                         * needed at this point, allocate one for the whole
1479
                         * profile.  The header check has already validated
1480
                         * that none of this stuff will overflow.
1481
                         */
1482
0
                        const png_uint_32 tag_count = png_get_uint_32(
1483
0
                            profile_header+128);
1484
0
                        png_bytep profile = png_read_buffer(png_ptr,
1485
0
                            profile_length, 2/*silent*/);
1486
1487
0
                        if (profile != NULL)
1488
0
                        {
1489
0
                           memcpy(profile, profile_header,
1490
0
                               (sizeof profile_header));
1491
1492
0
                           size = 12 * tag_count;
1493
1494
0
                           (void)png_inflate_read(png_ptr, local_buffer,
1495
0
                               (sizeof local_buffer), &length,
1496
0
                               profile + (sizeof profile_header), &size, 0);
1497
1498
                           /* Still expect a buffer error because we expect
1499
                            * there to be some tag data!
1500
                            */
1501
0
                           if (size == 0)
1502
0
                           {
1503
0
                              if (png_icc_check_tag_table(png_ptr,
1504
0
                                  &png_ptr->colorspace, keyword, profile_length,
1505
0
                                  profile) != 0)
1506
0
                              {
1507
                                 /* The profile has been validated for basic
1508
                                  * security issues, so read the whole thing in.
1509
                                  */
1510
0
                                 size = profile_length - (sizeof profile_header)
1511
0
                                     - 12 * tag_count;
1512
1513
0
                                 (void)png_inflate_read(png_ptr, local_buffer,
1514
0
                                     (sizeof local_buffer), &length,
1515
0
                                     profile + (sizeof profile_header) +
1516
0
                                     12 * tag_count, &size, 1/*finish*/);
1517
1518
0
                                 if (length > 0 && !(png_ptr->flags &
1519
0
                                     PNG_FLAG_BENIGN_ERRORS_WARN))
1520
0
                                    errmsg = "extra compressed data";
1521
1522
                                 /* But otherwise allow extra data: */
1523
0
                                 else if (size == 0)
1524
0
                                 {
1525
0
                                    if (length > 0)
1526
0
                                    {
1527
                                       /* This can be handled completely, so
1528
                                        * keep going.
1529
                                        */
1530
0
                                       png_chunk_warning(png_ptr,
1531
0
                                           "extra compressed data");
1532
0
                                    }
1533
1534
0
                                    png_crc_finish(png_ptr, length);
1535
0
                                    finished = 1;
1536
1537
0
# if defined(PNG_sRGB_SUPPORTED) && PNG_sRGB_PROFILE_CHECKS >= 0
1538
                                    /* Check for a match against sRGB */
1539
0
                                    png_icc_set_sRGB(png_ptr,
1540
0
                                        &png_ptr->colorspace, profile,
1541
0
                                        png_ptr->zstream.adler);
1542
0
# endif
1543
1544
                                    /* Steal the profile for info_ptr. */
1545
0
                                    if (info_ptr != NULL)
1546
0
                                    {
1547
0
                                       png_free_data(png_ptr, info_ptr,
1548
0
                                           PNG_FREE_ICCP, 0);
1549
1550
0
                                       info_ptr->iccp_name = png_voidcast(char*,
1551
0
                                           png_malloc_base(png_ptr,
1552
0
                                           keyword_length+1));
1553
0
                                       if (info_ptr->iccp_name != NULL)
1554
0
                                       {
1555
0
                                          memcpy(info_ptr->iccp_name, keyword,
1556
0
                                              keyword_length+1);
1557
0
                                          info_ptr->iccp_proflen =
1558
0
                                              profile_length;
1559
0
                                          info_ptr->iccp_profile = profile;
1560
0
                                          png_ptr->read_buffer = NULL; /*steal*/
1561
0
                                          info_ptr->free_me |= PNG_FREE_ICCP;
1562
0
                                          info_ptr->valid |= PNG_INFO_iCCP;
1563
0
                                       }
1564
1565
0
                                       else
1566
0
                                       {
1567
0
                                          png_ptr->colorspace.flags |=
1568
0
                                             PNG_COLORSPACE_INVALID;
1569
0
                                          errmsg = "out of memory";
1570
0
                                       }
1571
0
                                    }
1572
1573
                                    /* else the profile remains in the read
1574
                                     * buffer which gets reused for subsequent
1575
                                     * chunks.
1576
                                     */
1577
1578
0
                                    if (info_ptr != NULL)
1579
0
                                       png_colorspace_sync(png_ptr, info_ptr);
1580
1581
0
                                    if (errmsg == NULL)
1582
0
                                    {
1583
0
                                       png_ptr->zowner = 0;
1584
0
                                       return;
1585
0
                                    }
1586
0
                                 }
1587
0
                                 if (errmsg == NULL)
1588
0
                                    errmsg = png_ptr->zstream.msg;
1589
0
                              }
1590
                              /* else png_icc_check_tag_table output an error */
1591
0
                           }
1592
0
                           else /* profile truncated */
1593
0
                              errmsg = png_ptr->zstream.msg;
1594
0
                        }
1595
1596
0
                        else
1597
0
                           errmsg = "out of memory";
1598
0
                     }
1599
1600
                     /* else png_icc_check_header output an error */
1601
0
                  }
1602
1603
                  /* else png_icc_check_length output an error */
1604
0
               }
1605
1606
0
               else /* profile truncated */
1607
0
                  errmsg = png_ptr->zstream.msg;
1608
1609
               /* Release the stream */
1610
0
               png_ptr->zowner = 0;
1611
0
            }
1612
1613
0
            else /* png_inflate_claim failed */
1614
0
               errmsg = png_ptr->zstream.msg;
1615
0
         }
1616
1617
0
         else
1618
0
            errmsg = "bad compression method"; /* or missing */
1619
0
      }
1620
1621
0
      else
1622
0
         errmsg = "bad keyword";
1623
0
   }
1624
1625
0
   else
1626
0
      errmsg = "too many profiles";
1627
1628
   /* Failure: the reason is in 'errmsg' */
1629
0
   if (finished == 0)
1630
0
      png_crc_finish(png_ptr, length);
1631
1632
0
   png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1633
0
   png_colorspace_sync(png_ptr, info_ptr);
1634
0
   if (errmsg != NULL) /* else already output */
1635
0
      png_chunk_benign_error(png_ptr, errmsg);
1636
0
}
1637
#endif /* READ_iCCP */
1638
1639
#ifdef PNG_READ_sPLT_SUPPORTED
1640
void /* PRIVATE */
1641
png_handle_sPLT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1642
/* Note: this does not properly handle chunks that are > 64K under DOS */
1643
0
{
1644
0
   png_bytep entry_start, buffer;
1645
0
   png_sPLT_t new_palette;
1646
0
   png_sPLT_entryp pp;
1647
0
   png_uint_32 data_length;
1648
0
   int entry_size, i;
1649
0
   png_uint_32 skip = 0;
1650
0
   png_uint_32 dl;
1651
0
   png_size_t max_dl;
1652
1653
0
   png_debug(1, "in png_handle_sPLT");
1654
1655
0
#ifdef PNG_USER_LIMITS_SUPPORTED
1656
0
   if (png_ptr->user_chunk_cache_max != 0)
1657
0
   {
1658
0
      if (png_ptr->user_chunk_cache_max == 1)
1659
0
      {
1660
0
         png_crc_finish(png_ptr, length);
1661
0
         return;
1662
0
      }
1663
1664
0
      if (--png_ptr->user_chunk_cache_max == 1)
1665
0
      {
1666
0
         png_warning(png_ptr, "No space in chunk cache for sPLT");
1667
0
         png_crc_finish(png_ptr, length);
1668
0
         return;
1669
0
      }
1670
0
   }
1671
0
#endif
1672
1673
0
   if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1674
0
      png_chunk_error(png_ptr, "missing IHDR");
1675
1676
0
   else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
1677
0
   {
1678
0
      png_crc_finish(png_ptr, length);
1679
0
      png_chunk_benign_error(png_ptr, "out of place");
1680
0
      return;
1681
0
   }
1682
1683
#ifdef PNG_MAX_MALLOC_64K
1684
   if (length > 65535U)
1685
   {
1686
      png_crc_finish(png_ptr, length);
1687
      png_chunk_benign_error(png_ptr, "too large to fit in memory");
1688
      return;
1689
   }
1690
#endif
1691
1692
0
   buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
1693
0
   if (buffer == NULL)
1694
0
   {
1695
0
      png_crc_finish(png_ptr, length);
1696
0
      png_chunk_benign_error(png_ptr, "out of memory");
1697
0
      return;
1698
0
   }
1699
1700
1701
   /* WARNING: this may break if size_t is less than 32 bits; it is assumed
1702
    * that the PNG_MAX_MALLOC_64K test is enabled in this case, but this is a
1703
    * potential breakage point if the types in pngconf.h aren't exactly right.
1704
    */
1705
0
   png_crc_read(png_ptr, buffer, length);
1706
1707
0
   if (png_crc_finish(png_ptr, skip) != 0)
1708
0
      return;
1709
1710
0
   buffer[length] = 0;
1711
1712
0
   for (entry_start = buffer; *entry_start; entry_start++)
1713
0
      /* Empty loop to find end of name */ ;
1714
1715
0
   ++entry_start;
1716
1717
   /* A sample depth should follow the separator, and we should be on it  */
1718
0
   if (length < 2U || entry_start > buffer + (length - 2U))
1719
0
   {
1720
0
      png_warning(png_ptr, "malformed sPLT chunk");
1721
0
      return;
1722
0
   }
1723
1724
0
   new_palette.depth = *entry_start++;
1725
0
   entry_size = (new_palette.depth == 8 ? 6 : 10);
1726
   /* This must fit in a png_uint_32 because it is derived from the original
1727
    * chunk data length.
1728
    */
1729
0
   data_length = length - (png_uint_32)(entry_start - buffer);
1730
1731
   /* Integrity-check the data length */
1732
0
   if ((data_length % (unsigned int)entry_size) != 0)
1733
0
   {
1734
0
      png_warning(png_ptr, "sPLT chunk has bad length");
1735
0
      return;
1736
0
   }
1737
1738
0
   dl = (png_uint_32)(data_length / (unsigned int)entry_size);
1739
0
   max_dl = PNG_SIZE_MAX / (sizeof (png_sPLT_entry));
1740
1741
0
   if (dl > max_dl)
1742
0
   {
1743
0
      png_warning(png_ptr, "sPLT chunk too long");
1744
0
      return;
1745
0
   }
1746
1747
0
   new_palette.nentries = (png_int_32)(data_length / (unsigned int)entry_size);
1748
1749
0
   new_palette.entries = (png_sPLT_entryp)png_malloc_warn(png_ptr,
1750
0
       (png_alloc_size_t) new_palette.nentries * (sizeof (png_sPLT_entry)));
1751
1752
0
   if (new_palette.entries == NULL)
1753
0
   {
1754
0
      png_warning(png_ptr, "sPLT chunk requires too much memory");
1755
0
      return;
1756
0
   }
1757
1758
0
#ifdef PNG_POINTER_INDEXING_SUPPORTED
1759
0
   for (i = 0; i < new_palette.nentries; i++)
1760
0
   {
1761
0
      pp = new_palette.entries + i;
1762
1763
0
      if (new_palette.depth == 8)
1764
0
      {
1765
0
         pp->red = *entry_start++;
1766
0
         pp->green = *entry_start++;
1767
0
         pp->blue = *entry_start++;
1768
0
         pp->alpha = *entry_start++;
1769
0
      }
1770
1771
0
      else
1772
0
      {
1773
0
         pp->red   = png_get_uint_16(entry_start); entry_start += 2;
1774
0
         pp->green = png_get_uint_16(entry_start); entry_start += 2;
1775
0
         pp->blue  = png_get_uint_16(entry_start); entry_start += 2;
1776
0
         pp->alpha = png_get_uint_16(entry_start); entry_start += 2;
1777
0
      }
1778
1779
0
      pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
1780
0
   }
1781
#else
1782
   pp = new_palette.entries;
1783
1784
   for (i = 0; i < new_palette.nentries; i++)
1785
   {
1786
1787
      if (new_palette.depth == 8)
1788
      {
1789
         pp[i].red   = *entry_start++;
1790
         pp[i].green = *entry_start++;
1791
         pp[i].blue  = *entry_start++;
1792
         pp[i].alpha = *entry_start++;
1793
      }
1794
1795
      else
1796
      {
1797
         pp[i].red   = png_get_uint_16(entry_start); entry_start += 2;
1798
         pp[i].green = png_get_uint_16(entry_start); entry_start += 2;
1799
         pp[i].blue  = png_get_uint_16(entry_start); entry_start += 2;
1800
         pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2;
1801
      }
1802
1803
      pp[i].frequency = png_get_uint_16(entry_start); entry_start += 2;
1804
   }
1805
#endif
1806
1807
   /* Discard all chunk data except the name and stash that */
1808
0
   new_palette.name = (png_charp)buffer;
1809
1810
0
   png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);
1811
1812
0
   png_free(png_ptr, new_palette.entries);
1813
0
}
1814
#endif /* READ_sPLT */
1815
1816
#ifdef PNG_READ_tRNS_SUPPORTED
1817
void /* PRIVATE */
1818
png_handle_tRNS(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1819
0
{
1820
0
   png_byte readbuf[PNG_MAX_PALETTE_LENGTH];
1821
1822
0
   png_debug(1, "in png_handle_tRNS");
1823
1824
0
   if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1825
0
      png_chunk_error(png_ptr, "missing IHDR");
1826
1827
0
   else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
1828
0
   {
1829
0
      png_crc_finish(png_ptr, length);
1830
0
      png_chunk_benign_error(png_ptr, "out of place");
1831
0
      return;
1832
0
   }
1833
1834
0
   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS) != 0)
1835
0
   {
1836
0
      png_crc_finish(png_ptr, length);
1837
0
      png_chunk_benign_error(png_ptr, "duplicate");
1838
0
      return;
1839
0
   }
1840
1841
0
   if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
1842
0
   {
1843
0
      png_byte buf[2];
1844
1845
0
      if (length != 2)
1846
0
      {
1847
0
         png_crc_finish(png_ptr, length);
1848
0
         png_chunk_benign_error(png_ptr, "invalid");
1849
0
         return;
1850
0
      }
1851
1852
0
      png_crc_read(png_ptr, buf, 2);
1853
0
      png_ptr->num_trans = 1;
1854
0
      png_ptr->trans_color.gray = png_get_uint_16(buf);
1855
0
   }
1856
1857
0
   else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
1858
0
   {
1859
0
      png_byte buf[6];
1860
1861
0
      if (length != 6)
1862
0
      {
1863
0
         png_crc_finish(png_ptr, length);
1864
0
         png_chunk_benign_error(png_ptr, "invalid");
1865
0
         return;
1866
0
      }
1867
1868
0
      png_crc_read(png_ptr, buf, length);
1869
0
      png_ptr->num_trans = 1;
1870
0
      png_ptr->trans_color.red = png_get_uint_16(buf);
1871
0
      png_ptr->trans_color.green = png_get_uint_16(buf + 2);
1872
0
      png_ptr->trans_color.blue = png_get_uint_16(buf + 4);
1873
0
   }
1874
1875
0
   else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1876
0
   {
1877
0
      if ((png_ptr->mode & PNG_HAVE_PLTE) == 0)
1878
0
      {
1879
         /* TODO: is this actually an error in the ISO spec? */
1880
0
         png_crc_finish(png_ptr, length);
1881
0
         png_chunk_benign_error(png_ptr, "out of place");
1882
0
         return;
1883
0
      }
1884
1885
0
      if (length > (unsigned int) png_ptr->num_palette ||
1886
0
         length > (unsigned int) PNG_MAX_PALETTE_LENGTH ||
1887
0
         length == 0)
1888
0
      {
1889
0
         png_crc_finish(png_ptr, length);
1890
0
         png_chunk_benign_error(png_ptr, "invalid");
1891
0
         return;
1892
0
      }
1893
1894
0
      png_crc_read(png_ptr, readbuf, length);
1895
0
      png_ptr->num_trans = (png_uint_16)length;
1896
0
   }
1897
1898
0
   else
1899
0
   {
1900
0
      png_crc_finish(png_ptr, length);
1901
0
      png_chunk_benign_error(png_ptr, "invalid with alpha channel");
1902
0
      return;
1903
0
   }
1904
1905
0
   if (png_crc_finish(png_ptr, 0) != 0)
1906
0
   {
1907
0
      png_ptr->num_trans = 0;
1908
0
      return;
1909
0
   }
1910
1911
   /* TODO: this is a horrible side effect in the palette case because the
1912
    * png_struct ends up with a pointer to the tRNS buffer owned by the
1913
    * png_info.  Fix this.
1914
    */
1915
0
   png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,
1916
0
       &(png_ptr->trans_color));
1917
0
}
1918
#endif
1919
1920
#ifdef PNG_READ_bKGD_SUPPORTED
1921
void /* PRIVATE */
1922
png_handle_bKGD(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1923
0
{
1924
0
   unsigned int truelen;
1925
0
   png_byte buf[6];
1926
0
   png_color_16 background;
1927
1928
0
   png_debug(1, "in png_handle_bKGD");
1929
1930
0
   if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1931
0
      png_chunk_error(png_ptr, "missing IHDR");
1932
1933
0
   else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0 ||
1934
0
       (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
1935
0
       (png_ptr->mode & PNG_HAVE_PLTE) == 0))
1936
0
   {
1937
0
      png_crc_finish(png_ptr, length);
1938
0
      png_chunk_benign_error(png_ptr, "out of place");
1939
0
      return;
1940
0
   }
1941
1942
0
   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) != 0)
1943
0
   {
1944
0
      png_crc_finish(png_ptr, length);
1945
0
      png_chunk_benign_error(png_ptr, "duplicate");
1946
0
      return;
1947
0
   }
1948
1949
0
   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1950
0
      truelen = 1;
1951
1952
0
   else if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
1953
0
      truelen = 6;
1954
1955
0
   else
1956
0
      truelen = 2;
1957
1958
0
   if (length != truelen)
1959
0
   {
1960
0
      png_crc_finish(png_ptr, length);
1961
0
      png_chunk_benign_error(png_ptr, "invalid");
1962
0
      return;
1963
0
   }
1964
1965
0
   png_crc_read(png_ptr, buf, truelen);
1966
1967
0
   if (png_crc_finish(png_ptr, 0) != 0)
1968
0
      return;
1969
1970
   /* We convert the index value into RGB components so that we can allow
1971
    * arbitrary RGB values for background when we have transparency, and
1972
    * so it is easy to determine the RGB values of the background color
1973
    * from the info_ptr struct.
1974
    */
1975
0
   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1976
0
   {
1977
0
      background.index = buf[0];
1978
1979
0
      if (info_ptr != NULL && info_ptr->num_palette != 0)
1980
0
      {
1981
0
         if (buf[0] >= info_ptr->num_palette)
1982
0
         {
1983
0
            png_chunk_benign_error(png_ptr, "invalid index");
1984
0
            return;
1985
0
         }
1986
1987
0
         background.red = (png_uint_16)png_ptr->palette[buf[0]].red;
1988
0
         background.green = (png_uint_16)png_ptr->palette[buf[0]].green;
1989
0
         background.blue = (png_uint_16)png_ptr->palette[buf[0]].blue;
1990
0
      }
1991
1992
0
      else
1993
0
         background.red = background.green = background.blue = 0;
1994
1995
0
      background.gray = 0;
1996
0
   }
1997
1998
0
   else if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0) /* GRAY */
1999
0
   {
2000
0
      background.index = 0;
2001
0
      background.red =
2002
0
      background.green =
2003
0
      background.blue =
2004
0
      background.gray = png_get_uint_16(buf);
2005
0
   }
2006
2007
0
   else
2008
0
   {
2009
0
      background.index = 0;
2010
0
      background.red = png_get_uint_16(buf);
2011
0
      background.green = png_get_uint_16(buf + 2);
2012
0
      background.blue = png_get_uint_16(buf + 4);
2013
0
      background.gray = 0;
2014
0
   }
2015
2016
0
   png_set_bKGD(png_ptr, info_ptr, &background);
2017
0
}
2018
#endif
2019
2020
#ifdef PNG_READ_eXIf_SUPPORTED
2021
void /* PRIVATE */
2022
png_handle_eXIf(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2023
0
{
2024
0
   unsigned int i;
2025
2026
0
   png_debug(1, "in png_handle_eXIf");
2027
2028
0
   if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2029
0
      png_chunk_error(png_ptr, "missing IHDR");
2030
2031
0
   if (length < 2)
2032
0
   {
2033
0
      png_crc_finish(png_ptr, length);
2034
0
      png_chunk_benign_error(png_ptr, "too short");
2035
0
      return;
2036
0
   }
2037
2038
0
   else if (info_ptr == NULL || (info_ptr->valid & PNG_INFO_eXIf) != 0)
2039
0
   {
2040
0
      png_crc_finish(png_ptr, length);
2041
0
      png_chunk_benign_error(png_ptr, "duplicate");
2042
0
      return;
2043
0
   }
2044
2045
0
   info_ptr->free_me |= PNG_FREE_EXIF;
2046
2047
0
   info_ptr->eXIf_buf = png_voidcast(png_bytep,
2048
0
             png_malloc_warn(png_ptr, length));
2049
2050
0
   if (info_ptr->eXIf_buf == NULL)
2051
0
   {
2052
0
      png_crc_finish(png_ptr, length);
2053
0
      png_chunk_benign_error(png_ptr, "out of memory");
2054
0
      return;
2055
0
   }
2056
2057
0
   for (i = 0; i < length; i++)
2058
0
   {
2059
0
      png_byte buf[1];
2060
0
      png_crc_read(png_ptr, buf, 1);
2061
0
      info_ptr->eXIf_buf[i] = buf[0];
2062
0
      if (i == 1 && buf[0] != 'M' && buf[0] != 'I'
2063
0
                 && info_ptr->eXIf_buf[0] != buf[0])
2064
0
      {
2065
0
         png_crc_finish(png_ptr, length);
2066
0
         png_chunk_benign_error(png_ptr, "incorrect byte-order specifier");
2067
0
         png_free(png_ptr, info_ptr->eXIf_buf);
2068
0
         info_ptr->eXIf_buf = NULL;
2069
0
         return;
2070
0
      }
2071
0
   }
2072
2073
0
   if (png_crc_finish(png_ptr, 0) != 0)
2074
0
      return;
2075
2076
0
   png_set_eXIf_1(png_ptr, info_ptr, length, info_ptr->eXIf_buf);
2077
2078
0
   png_free(png_ptr, info_ptr->eXIf_buf);
2079
0
   info_ptr->eXIf_buf = NULL;
2080
0
}
2081
#endif
2082
2083
#ifdef PNG_READ_hIST_SUPPORTED
2084
void /* PRIVATE */
2085
png_handle_hIST(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2086
0
{
2087
0
   unsigned int num, i;
2088
0
   png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];
2089
2090
0
   png_debug(1, "in png_handle_hIST");
2091
2092
0
   if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2093
0
      png_chunk_error(png_ptr, "missing IHDR");
2094
2095
0
   else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0 ||
2096
0
       (png_ptr->mode & PNG_HAVE_PLTE) == 0)
2097
0
   {
2098
0
      png_crc_finish(png_ptr, length);
2099
0
      png_chunk_benign_error(png_ptr, "out of place");
2100
0
      return;
2101
0
   }
2102
2103
0
   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) != 0)
2104
0
   {
2105
0
      png_crc_finish(png_ptr, length);
2106
0
      png_chunk_benign_error(png_ptr, "duplicate");
2107
0
      return;
2108
0
   }
2109
2110
0
   num = length / 2 ;
2111
2112
0
   if (num != (unsigned int) png_ptr->num_palette ||
2113
0
       num > (unsigned int) PNG_MAX_PALETTE_LENGTH)
2114
0
   {
2115
0
      png_crc_finish(png_ptr, length);
2116
0
      png_chunk_benign_error(png_ptr, "invalid");
2117
0
      return;
2118
0
   }
2119
2120
0
   for (i = 0; i < num; i++)
2121
0
   {
2122
0
      png_byte buf[2];
2123
2124
0
      png_crc_read(png_ptr, buf, 2);
2125
0
      readbuf[i] = png_get_uint_16(buf);
2126
0
   }
2127
2128
0
   if (png_crc_finish(png_ptr, 0) != 0)
2129
0
      return;
2130
2131
0
   png_set_hIST(png_ptr, info_ptr, readbuf);
2132
0
}
2133
#endif
2134
2135
#ifdef PNG_READ_pHYs_SUPPORTED
2136
void /* PRIVATE */
2137
png_handle_pHYs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2138
0
{
2139
0
   png_byte buf[9];
2140
0
   png_uint_32 res_x, res_y;
2141
0
   int unit_type;
2142
2143
0
   png_debug(1, "in png_handle_pHYs");
2144
2145
0
   if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2146
0
      png_chunk_error(png_ptr, "missing IHDR");
2147
2148
0
   else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2149
0
   {
2150
0
      png_crc_finish(png_ptr, length);
2151
0
      png_chunk_benign_error(png_ptr, "out of place");
2152
0
      return;
2153
0
   }
2154
2155
0
   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs) != 0)
2156
0
   {
2157
0
      png_crc_finish(png_ptr, length);
2158
0
      png_chunk_benign_error(png_ptr, "duplicate");
2159
0
      return;
2160
0
   }
2161
2162
0
   if (length != 9)
2163
0
   {
2164
0
      png_crc_finish(png_ptr, length);
2165
0
      png_chunk_benign_error(png_ptr, "invalid");
2166
0
      return;
2167
0
   }
2168
2169
0
   png_crc_read(png_ptr, buf, 9);
2170
2171
0
   if (png_crc_finish(png_ptr, 0) != 0)
2172
0
      return;
2173
2174
0
   res_x = png_get_uint_32(buf);
2175
0
   res_y = png_get_uint_32(buf + 4);
2176
0
   unit_type = buf[8];
2177
0
   png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
2178
0
}
2179
#endif
2180
2181
#ifdef PNG_READ_oFFs_SUPPORTED
2182
void /* PRIVATE */
2183
png_handle_oFFs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2184
0
{
2185
0
   png_byte buf[9];
2186
0
   png_int_32 offset_x, offset_y;
2187
0
   int unit_type;
2188
2189
0
   png_debug(1, "in png_handle_oFFs");
2190
2191
0
   if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2192
0
      png_chunk_error(png_ptr, "missing IHDR");
2193
2194
0
   else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2195
0
   {
2196
0
      png_crc_finish(png_ptr, length);
2197
0
      png_chunk_benign_error(png_ptr, "out of place");
2198
0
      return;
2199
0
   }
2200
2201
0
   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs) != 0)
2202
0
   {
2203
0
      png_crc_finish(png_ptr, length);
2204
0
      png_chunk_benign_error(png_ptr, "duplicate");
2205
0
      return;
2206
0
   }
2207
2208
0
   if (length != 9)
2209
0
   {
2210
0
      png_crc_finish(png_ptr, length);
2211
0
      png_chunk_benign_error(png_ptr, "invalid");
2212
0
      return;
2213
0
   }
2214
2215
0
   png_crc_read(png_ptr, buf, 9);
2216
2217
0
   if (png_crc_finish(png_ptr, 0) != 0)
2218
0
      return;
2219
2220
0
   offset_x = png_get_int_32(buf);
2221
0
   offset_y = png_get_int_32(buf + 4);
2222
0
   unit_type = buf[8];
2223
0
   png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
2224
0
}
2225
#endif
2226
2227
#ifdef PNG_READ_pCAL_SUPPORTED
2228
/* Read the pCAL chunk (described in the PNG Extensions document) */
2229
void /* PRIVATE */
2230
png_handle_pCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2231
0
{
2232
0
   png_int_32 X0, X1;
2233
0
   png_byte type, nparams;
2234
0
   png_bytep buffer, buf, units, endptr;
2235
0
   png_charpp params;
2236
0
   int i;
2237
2238
0
   png_debug(1, "in png_handle_pCAL");
2239
2240
0
   if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2241
0
      png_chunk_error(png_ptr, "missing IHDR");
2242
2243
0
   else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2244
0
   {
2245
0
      png_crc_finish(png_ptr, length);
2246
0
      png_chunk_benign_error(png_ptr, "out of place");
2247
0
      return;
2248
0
   }
2249
2250
0
   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL) != 0)
2251
0
   {
2252
0
      png_crc_finish(png_ptr, length);
2253
0
      png_chunk_benign_error(png_ptr, "duplicate");
2254
0
      return;
2255
0
   }
2256
2257
0
   png_debug1(2, "Allocating and reading pCAL chunk data (%u bytes)",
2258
0
       length + 1);
2259
2260
0
   buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
2261
2262
0
   if (buffer == NULL)
2263
0
   {
2264
0
      png_crc_finish(png_ptr, length);
2265
0
      png_chunk_benign_error(png_ptr, "out of memory");
2266
0
      return;
2267
0
   }
2268
2269
0
   png_crc_read(png_ptr, buffer, length);
2270
2271
0
   if (png_crc_finish(png_ptr, 0) != 0)
2272
0
      return;
2273
2274
0
   buffer[length] = 0; /* Null terminate the last string */
2275
2276
0
   png_debug(3, "Finding end of pCAL purpose string");
2277
0
   for (buf = buffer; *buf; buf++)
2278
0
      /* Empty loop */ ;
2279
2280
0
   endptr = buffer + length;
2281
2282
   /* We need to have at least 12 bytes after the purpose string
2283
    * in order to get the parameter information.
2284
    */
2285
0
   if (endptr - buf <= 12)
2286
0
   {
2287
0
      png_chunk_benign_error(png_ptr, "invalid");
2288
0
      return;
2289
0
   }
2290
2291
0
   png_debug(3, "Reading pCAL X0, X1, type, nparams, and units");
2292
0
   X0 = png_get_int_32((png_bytep)buf+1);
2293
0
   X1 = png_get_int_32((png_bytep)buf+5);
2294
0
   type = buf[9];
2295
0
   nparams = buf[10];
2296
0
   units = buf + 11;
2297
2298
0
   png_debug(3, "Checking pCAL equation type and number of parameters");
2299
   /* Check that we have the right number of parameters for known
2300
    * equation types.
2301
    */
2302
0
   if ((type == PNG_EQUATION_LINEAR && nparams != 2) ||
2303
0
       (type == PNG_EQUATION_BASE_E && nparams != 3) ||
2304
0
       (type == PNG_EQUATION_ARBITRARY && nparams != 3) ||
2305
0
       (type == PNG_EQUATION_HYPERBOLIC && nparams != 4))
2306
0
   {
2307
0
      png_chunk_benign_error(png_ptr, "invalid parameter count");
2308
0
      return;
2309
0
   }
2310
2311
0
   else if (type >= PNG_EQUATION_LAST)
2312
0
   {
2313
0
      png_chunk_benign_error(png_ptr, "unrecognized equation type");
2314
0
   }
2315
2316
0
   for (buf = units; *buf; buf++)
2317
0
      /* Empty loop to move past the units string. */ ;
2318
2319
0
   png_debug(3, "Allocating pCAL parameters array");
2320
2321
0
   params = png_voidcast(png_charpp, png_malloc_warn(png_ptr,
2322
0
       nparams * (sizeof (png_charp))));
2323
2324
0
   if (params == NULL)
2325
0
   {
2326
0
      png_chunk_benign_error(png_ptr, "out of memory");
2327
0
      return;
2328
0
   }
2329
2330
   /* Get pointers to the start of each parameter string. */
2331
0
   for (i = 0; i < nparams; i++)
2332
0
   {
2333
0
      buf++; /* Skip the null string terminator from previous parameter. */
2334
2335
0
      png_debug1(3, "Reading pCAL parameter %d", i);
2336
2337
0
      for (params[i] = (png_charp)buf; buf <= endptr && *buf != 0; buf++)
2338
0
         /* Empty loop to move past each parameter string */ ;
2339
2340
      /* Make sure we haven't run out of data yet */
2341
0
      if (buf > endptr)
2342
0
      {
2343
0
         png_free(png_ptr, params);
2344
0
         png_chunk_benign_error(png_ptr, "invalid data");
2345
0
         return;
2346
0
      }
2347
0
   }
2348
2349
0
   png_set_pCAL(png_ptr, info_ptr, (png_charp)buffer, X0, X1, type, nparams,
2350
0
       (png_charp)units, params);
2351
2352
0
   png_free(png_ptr, params);
2353
0
}
2354
#endif
2355
2356
#ifdef PNG_READ_sCAL_SUPPORTED
2357
/* Read the sCAL chunk */
2358
void /* PRIVATE */
2359
png_handle_sCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2360
0
{
2361
0
   png_bytep buffer;
2362
0
   png_size_t i;
2363
0
   int state;
2364
2365
0
   png_debug(1, "in png_handle_sCAL");
2366
2367
0
   if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2368
0
      png_chunk_error(png_ptr, "missing IHDR");
2369
2370
0
   else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2371
0
   {
2372
0
      png_crc_finish(png_ptr, length);
2373
0
      png_chunk_benign_error(png_ptr, "out of place");
2374
0
      return;
2375
0
   }
2376
2377
0
   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL) != 0)
2378
0
   {
2379
0
      png_crc_finish(png_ptr, length);
2380
0
      png_chunk_benign_error(png_ptr, "duplicate");
2381
0
      return;
2382
0
   }
2383
2384
   /* Need unit type, width, \0, height: minimum 4 bytes */
2385
0
   else if (length < 4)
2386
0
   {
2387
0
      png_crc_finish(png_ptr, length);
2388
0
      png_chunk_benign_error(png_ptr, "invalid");
2389
0
      return;
2390
0
   }
2391
2392
0
   png_debug1(2, "Allocating and reading sCAL chunk data (%u bytes)",
2393
0
       length + 1);
2394
2395
0
   buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
2396
2397
0
   if (buffer == NULL)
2398
0
   {
2399
0
      png_chunk_benign_error(png_ptr, "out of memory");
2400
0
      png_crc_finish(png_ptr, length);
2401
0
      return;
2402
0
   }
2403
2404
0
   png_crc_read(png_ptr, buffer, length);
2405
0
   buffer[length] = 0; /* Null terminate the last string */
2406
2407
0
   if (png_crc_finish(png_ptr, 0) != 0)
2408
0
      return;
2409
2410
   /* Validate the unit. */
2411
0
   if (buffer[0] != 1 && buffer[0] != 2)
2412
0
   {
2413
0
      png_chunk_benign_error(png_ptr, "invalid unit");
2414
0
      return;
2415
0
   }
2416
2417
   /* Validate the ASCII numbers, need two ASCII numbers separated by
2418
    * a '\0' and they need to fit exactly in the chunk data.
2419
    */
2420
0
   i = 1;
2421
0
   state = 0;
2422
2423
0
   if (png_check_fp_number((png_const_charp)buffer, length, &state, &i) == 0 ||
2424
0
       i >= length || buffer[i++] != 0)
2425
0
      png_chunk_benign_error(png_ptr, "bad width format");
2426
2427
0
   else if (PNG_FP_IS_POSITIVE(state) == 0)
2428
0
      png_chunk_benign_error(png_ptr, "non-positive width");
2429
2430
0
   else
2431
0
   {
2432
0
      png_size_t heighti = i;
2433
2434
0
      state = 0;
2435
0
      if (png_check_fp_number((png_const_charp)buffer, length,
2436
0
          &state, &i) == 0 || i != length)
2437
0
         png_chunk_benign_error(png_ptr, "bad height format");
2438
2439
0
      else if (PNG_FP_IS_POSITIVE(state) == 0)
2440
0
         png_chunk_benign_error(png_ptr, "non-positive height");
2441
2442
0
      else
2443
         /* This is the (only) success case. */
2444
0
         png_set_sCAL_s(png_ptr, info_ptr, buffer[0],
2445
0
             (png_charp)buffer+1, (png_charp)buffer+heighti);
2446
0
   }
2447
0
}
2448
#endif
2449
2450
#ifdef PNG_READ_tIME_SUPPORTED
2451
void /* PRIVATE */
2452
png_handle_tIME(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2453
0
{
2454
0
   png_byte buf[7];
2455
0
   png_time mod_time;
2456
2457
0
   png_debug(1, "in png_handle_tIME");
2458
2459
0
   if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2460
0
      png_chunk_error(png_ptr, "missing IHDR");
2461
2462
0
   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME) != 0)
2463
0
   {
2464
0
      png_crc_finish(png_ptr, length);
2465
0
      png_chunk_benign_error(png_ptr, "duplicate");
2466
0
      return;
2467
0
   }
2468
2469
0
   if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2470
0
      png_ptr->mode |= PNG_AFTER_IDAT;
2471
2472
0
   if (length != 7)
2473
0
   {
2474
0
      png_crc_finish(png_ptr, length);
2475
0
      png_chunk_benign_error(png_ptr, "invalid");
2476
0
      return;
2477
0
   }
2478
2479
0
   png_crc_read(png_ptr, buf, 7);
2480
2481
0
   if (png_crc_finish(png_ptr, 0) != 0)
2482
0
      return;
2483
2484
0
   mod_time.second = buf[6];
2485
0
   mod_time.minute = buf[5];
2486
0
   mod_time.hour = buf[4];
2487
0
   mod_time.day = buf[3];
2488
0
   mod_time.month = buf[2];
2489
0
   mod_time.year = png_get_uint_16(buf);
2490
2491
0
   png_set_tIME(png_ptr, info_ptr, &mod_time);
2492
0
}
2493
#endif
2494
2495
#ifdef PNG_READ_tEXt_SUPPORTED
2496
/* Note: this does not properly handle chunks that are > 64K under DOS */
2497
void /* PRIVATE */
2498
png_handle_tEXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2499
0
{
2500
0
   png_text  text_info;
2501
0
   png_bytep buffer;
2502
0
   png_charp key;
2503
0
   png_charp text;
2504
0
   png_uint_32 skip = 0;
2505
2506
0
   png_debug(1, "in png_handle_tEXt");
2507
2508
0
#ifdef PNG_USER_LIMITS_SUPPORTED
2509
0
   if (png_ptr->user_chunk_cache_max != 0)
2510
0
   {
2511
0
      if (png_ptr->user_chunk_cache_max == 1)
2512
0
      {
2513
0
         png_crc_finish(png_ptr, length);
2514
0
         return;
2515
0
      }
2516
2517
0
      if (--png_ptr->user_chunk_cache_max == 1)
2518
0
      {
2519
0
         png_crc_finish(png_ptr, length);
2520
0
         png_chunk_benign_error(png_ptr, "no space in chunk cache");
2521
0
         return;
2522
0
      }
2523
0
   }
2524
0
#endif
2525
2526
0
   if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2527
0
      png_chunk_error(png_ptr, "missing IHDR");
2528
2529
0
   if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2530
0
      png_ptr->mode |= PNG_AFTER_IDAT;
2531
2532
#ifdef PNG_MAX_MALLOC_64K
2533
   if (length > 65535U)
2534
   {
2535
      png_crc_finish(png_ptr, length);
2536
      png_chunk_benign_error(png_ptr, "too large to fit in memory");
2537
      return;
2538
   }
2539
#endif
2540
2541
0
   buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
2542
2543
0
   if (buffer == NULL)
2544
0
   {
2545
0
      png_chunk_benign_error(png_ptr, "out of memory");
2546
0
      return;
2547
0
   }
2548
2549
0
   png_crc_read(png_ptr, buffer, length);
2550
2551
0
   if (png_crc_finish(png_ptr, skip) != 0)
2552
0
      return;
2553
2554
0
   key = (png_charp)buffer;
2555
0
   key[length] = 0;
2556
2557
0
   for (text = key; *text; text++)
2558
0
      /* Empty loop to find end of key */ ;
2559
2560
0
   if (text != key + length)
2561
0
      text++;
2562
2563
0
   text_info.compression = PNG_TEXT_COMPRESSION_NONE;
2564
0
   text_info.key = key;
2565
0
   text_info.lang = NULL;
2566
0
   text_info.lang_key = NULL;
2567
0
   text_info.itxt_length = 0;
2568
0
   text_info.text = text;
2569
0
   text_info.text_length = strlen(text);
2570
2571
0
   if (png_set_text_2(png_ptr, info_ptr, &text_info, 1) != 0)
2572
0
      png_warning(png_ptr, "Insufficient memory to process text chunk");
2573
0
}
2574
#endif
2575
2576
#ifdef PNG_READ_zTXt_SUPPORTED
2577
/* Note: this does not correctly handle chunks that are > 64K under DOS */
2578
void /* PRIVATE */
2579
png_handle_zTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2580
0
{
2581
0
   png_const_charp errmsg = NULL;
2582
0
   png_bytep       buffer;
2583
0
   png_uint_32     keyword_length;
2584
2585
0
   png_debug(1, "in png_handle_zTXt");
2586
2587
0
#ifdef PNG_USER_LIMITS_SUPPORTED
2588
0
   if (png_ptr->user_chunk_cache_max != 0)
2589
0
   {
2590
0
      if (png_ptr->user_chunk_cache_max == 1)
2591
0
      {
2592
0
         png_crc_finish(png_ptr, length);
2593
0
         return;
2594
0
      }
2595
2596
0
      if (--png_ptr->user_chunk_cache_max == 1)
2597
0
      {
2598
0
         png_crc_finish(png_ptr, length);
2599
0
         png_chunk_benign_error(png_ptr, "no space in chunk cache");
2600
0
         return;
2601
0
      }
2602
0
   }
2603
0
#endif
2604
2605
0
   if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2606
0
      png_chunk_error(png_ptr, "missing IHDR");
2607
2608
0
   if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2609
0
      png_ptr->mode |= PNG_AFTER_IDAT;
2610
2611
   /* Note, "length" is sufficient here; we won't be adding
2612
    * a null terminator later.
2613
    */
2614
0
   buffer = png_read_buffer(png_ptr, length, 2/*silent*/);
2615
2616
0
   if (buffer == NULL)
2617
0
   {
2618
0
      png_crc_finish(png_ptr, length);
2619
0
      png_chunk_benign_error(png_ptr, "out of memory");
2620
0
      return;
2621
0
   }
2622
2623
0
   png_crc_read(png_ptr, buffer, length);
2624
2625
0
   if (png_crc_finish(png_ptr, 0) != 0)
2626
0
      return;
2627
2628
   /* TODO: also check that the keyword contents match the spec! */
2629
0
   for (keyword_length = 0;
2630
0
      keyword_length < length && buffer[keyword_length] != 0;
2631
0
      ++keyword_length)
2632
0
      /* Empty loop to find end of name */ ;
2633
2634
0
   if (keyword_length > 79 || keyword_length < 1)
2635
0
      errmsg = "bad keyword";
2636
2637
   /* zTXt must have some LZ data after the keyword, although it may expand to
2638
    * zero bytes; we need a '\0' at the end of the keyword, the compression type
2639
    * then the LZ data:
2640
    */
2641
0
   else if (keyword_length + 3 > length)
2642
0
      errmsg = "truncated";
2643
2644
0
   else if (buffer[keyword_length+1] != PNG_COMPRESSION_TYPE_BASE)
2645
0
      errmsg = "unknown compression type";
2646
2647
0
   else
2648
0
   {
2649
0
      png_alloc_size_t uncompressed_length = PNG_SIZE_MAX;
2650
2651
      /* TODO: at present png_decompress_chunk imposes a single application
2652
       * level memory limit, this should be split to different values for iCCP
2653
       * and text chunks.
2654
       */
2655
0
      if (png_decompress_chunk(png_ptr, length, keyword_length+2,
2656
0
          &uncompressed_length, 1/*terminate*/) == Z_STREAM_END)
2657
0
      {
2658
0
         png_text text;
2659
2660
0
         if (png_ptr->read_buffer == NULL)
2661
0
           errmsg="Read failure in png_handle_zTXt";
2662
0
         else
2663
0
         {
2664
            /* It worked; png_ptr->read_buffer now looks like a tEXt chunk
2665
             * except for the extra compression type byte and the fact that
2666
             * it isn't necessarily '\0' terminated.
2667
             */
2668
0
            buffer = png_ptr->read_buffer;
2669
0
            buffer[uncompressed_length+(keyword_length+2)] = 0;
2670
2671
0
            text.compression = PNG_TEXT_COMPRESSION_zTXt;
2672
0
            text.key = (png_charp)buffer;
2673
0
            text.text = (png_charp)(buffer + keyword_length+2);
2674
0
            text.text_length = uncompressed_length;
2675
0
            text.itxt_length = 0;
2676
0
            text.lang = NULL;
2677
0
            text.lang_key = NULL;
2678
2679
0
            if (png_set_text_2(png_ptr, info_ptr, &text, 1) != 0)
2680
0
               errmsg = "insufficient memory";
2681
0
         }
2682
0
      }
2683
2684
0
      else
2685
0
         errmsg = png_ptr->zstream.msg;
2686
0
   }
2687
2688
0
   if (errmsg != NULL)
2689
0
      png_chunk_benign_error(png_ptr, errmsg);
2690
0
}
2691
#endif
2692
2693
#ifdef PNG_READ_iTXt_SUPPORTED
2694
/* Note: this does not correctly handle chunks that are > 64K under DOS */
2695
void /* PRIVATE */
2696
png_handle_iTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2697
0
{
2698
0
   png_const_charp errmsg = NULL;
2699
0
   png_bytep buffer;
2700
0
   png_uint_32 prefix_length;
2701
2702
0
   png_debug(1, "in png_handle_iTXt");
2703
2704
0
#ifdef PNG_USER_LIMITS_SUPPORTED
2705
0
   if (png_ptr->user_chunk_cache_max != 0)
2706
0
   {
2707
0
      if (png_ptr->user_chunk_cache_max == 1)
2708
0
      {
2709
0
         png_crc_finish(png_ptr, length);
2710
0
         return;
2711
0
      }
2712
2713
0
      if (--png_ptr->user_chunk_cache_max == 1)
2714
0
      {
2715
0
         png_crc_finish(png_ptr, length);
2716
0
         png_chunk_benign_error(png_ptr, "no space in chunk cache");
2717
0
         return;
2718
0
      }
2719
0
   }
2720
0
#endif
2721
2722
0
   if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2723
0
      png_chunk_error(png_ptr, "missing IHDR");
2724
2725
0
   if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2726
0
      png_ptr->mode |= PNG_AFTER_IDAT;
2727
2728
0
   buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
2729
2730
0
   if (buffer == NULL)
2731
0
   {
2732
0
      png_crc_finish(png_ptr, length);
2733
0
      png_chunk_benign_error(png_ptr, "out of memory");
2734
0
      return;
2735
0
   }
2736
2737
0
   png_crc_read(png_ptr, buffer, length);
2738
2739
0
   if (png_crc_finish(png_ptr, 0) != 0)
2740
0
      return;
2741
2742
   /* First the keyword. */
2743
0
   for (prefix_length=0;
2744
0
      prefix_length < length && buffer[prefix_length] != 0;
2745
0
      ++prefix_length)
2746
0
      /* Empty loop */ ;
2747
2748
   /* Perform a basic check on the keyword length here. */
2749
0
   if (prefix_length > 79 || prefix_length < 1)
2750
0
      errmsg = "bad keyword";
2751
2752
   /* Expect keyword, compression flag, compression type, language, translated
2753
    * keyword (both may be empty but are 0 terminated) then the text, which may
2754
    * be empty.
2755
    */
2756
0
   else if (prefix_length + 5 > length)
2757
0
      errmsg = "truncated";
2758
2759
0
   else if (buffer[prefix_length+1] == 0 ||
2760
0
      (buffer[prefix_length+1] == 1 &&
2761
0
      buffer[prefix_length+2] == PNG_COMPRESSION_TYPE_BASE))
2762
0
   {
2763
0
      int compressed = buffer[prefix_length+1] != 0;
2764
0
      png_uint_32 language_offset, translated_keyword_offset;
2765
0
      png_alloc_size_t uncompressed_length = 0;
2766
2767
      /* Now the language tag */
2768
0
      prefix_length += 3;
2769
0
      language_offset = prefix_length;
2770
2771
0
      for (; prefix_length < length && buffer[prefix_length] != 0;
2772
0
         ++prefix_length)
2773
0
         /* Empty loop */ ;
2774
2775
      /* WARNING: the length may be invalid here, this is checked below. */
2776
0
      translated_keyword_offset = ++prefix_length;
2777
2778
0
      for (; prefix_length < length && buffer[prefix_length] != 0;
2779
0
         ++prefix_length)
2780
0
         /* Empty loop */ ;
2781
2782
      /* prefix_length should now be at the trailing '\0' of the translated
2783
       * keyword, but it may already be over the end.  None of this arithmetic
2784
       * can overflow because chunks are at most 2^31 bytes long, but on 16-bit
2785
       * systems the available allocation may overflow.
2786
       */
2787
0
      ++prefix_length;
2788
2789
0
      if (compressed == 0 && prefix_length <= length)
2790
0
         uncompressed_length = length - prefix_length;
2791
2792
0
      else if (compressed != 0 && prefix_length < length)
2793
0
      {
2794
0
         uncompressed_length = PNG_SIZE_MAX;
2795
2796
         /* TODO: at present png_decompress_chunk imposes a single application
2797
          * level memory limit, this should be split to different values for
2798
          * iCCP and text chunks.
2799
          */
2800
0
         if (png_decompress_chunk(png_ptr, length, prefix_length,
2801
0
             &uncompressed_length, 1/*terminate*/) == Z_STREAM_END)
2802
0
            buffer = png_ptr->read_buffer;
2803
2804
0
         else
2805
0
            errmsg = png_ptr->zstream.msg;
2806
0
      }
2807
2808
0
      else
2809
0
         errmsg = "truncated";
2810
2811
0
      if (errmsg == NULL)
2812
0
      {
2813
0
         png_text text;
2814
2815
0
         buffer[uncompressed_length+prefix_length] = 0;
2816
2817
0
         if (compressed == 0)
2818
0
            text.compression = PNG_ITXT_COMPRESSION_NONE;
2819
2820
0
         else
2821
0
            text.compression = PNG_ITXT_COMPRESSION_zTXt;
2822
2823
0
         text.key = (png_charp)buffer;
2824
0
         text.lang = (png_charp)buffer + language_offset;
2825
0
         text.lang_key = (png_charp)buffer + translated_keyword_offset;
2826
0
         text.text = (png_charp)buffer + prefix_length;
2827
0
         text.text_length = 0;
2828
0
         text.itxt_length = uncompressed_length;
2829
2830
0
         if (png_set_text_2(png_ptr, info_ptr, &text, 1) != 0)
2831
0
            errmsg = "insufficient memory";
2832
0
      }
2833
0
   }
2834
2835
0
   else
2836
0
      errmsg = "bad compression info";
2837
2838
0
   if (errmsg != NULL)
2839
0
      png_chunk_benign_error(png_ptr, errmsg);
2840
0
}
2841
#endif
2842
2843
#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
2844
/* Utility function for png_handle_unknown; set up png_ptr::unknown_chunk */
2845
static int
2846
png_cache_unknown_chunk(png_structrp png_ptr, png_uint_32 length)
2847
0
{
2848
0
   png_alloc_size_t limit = PNG_SIZE_MAX;
2849
2850
0
   if (png_ptr->unknown_chunk.data != NULL)
2851
0
   {
2852
0
      png_free(png_ptr, png_ptr->unknown_chunk.data);
2853
0
      png_ptr->unknown_chunk.data = NULL;
2854
0
   }
2855
2856
0
#  ifdef PNG_SET_USER_LIMITS_SUPPORTED
2857
0
   if (png_ptr->user_chunk_malloc_max > 0 &&
2858
0
       png_ptr->user_chunk_malloc_max < limit)
2859
0
      limit = png_ptr->user_chunk_malloc_max;
2860
2861
#  elif PNG_USER_CHUNK_MALLOC_MAX > 0
2862
   if (PNG_USER_CHUNK_MALLOC_MAX < limit)
2863
      limit = PNG_USER_CHUNK_MALLOC_MAX;
2864
#  endif
2865
2866
0
   if (length <= limit)
2867
0
   {
2868
0
      PNG_CSTRING_FROM_CHUNK(png_ptr->unknown_chunk.name, png_ptr->chunk_name);
2869
      /* The following is safe because of the PNG_SIZE_MAX init above */
2870
0
      png_ptr->unknown_chunk.size = (png_size_t)length/*SAFE*/;
2871
      /* 'mode' is a flag array, only the bottom four bits matter here */
2872
0
      png_ptr->unknown_chunk.location = (png_byte)png_ptr->mode/*SAFE*/;
2873
2874
0
      if (length == 0)
2875
0
         png_ptr->unknown_chunk.data = NULL;
2876
2877
0
      else
2878
0
      {
2879
         /* Do a 'warn' here - it is handled below. */
2880
0
         png_ptr->unknown_chunk.data = png_voidcast(png_bytep,
2881
0
             png_malloc_warn(png_ptr, length));
2882
0
      }
2883
0
   }
2884
2885
0
   if (png_ptr->unknown_chunk.data == NULL && length > 0)
2886
0
   {
2887
      /* This is benign because we clean up correctly */
2888
0
      png_crc_finish(png_ptr, length);
2889
0
      png_chunk_benign_error(png_ptr, "unknown chunk exceeds memory limits");
2890
0
      return 0;
2891
0
   }
2892
2893
0
   else
2894
0
   {
2895
0
      if (length > 0)
2896
0
         png_crc_read(png_ptr, png_ptr->unknown_chunk.data, length);
2897
0
      png_crc_finish(png_ptr, 0);
2898
0
      return 1;
2899
0
   }
2900
0
}
2901
#endif /* READ_UNKNOWN_CHUNKS */
2902
2903
/* Handle an unknown, or known but disabled, chunk */
2904
void /* PRIVATE */
2905
png_handle_unknown(png_structrp png_ptr, png_inforp info_ptr,
2906
    png_uint_32 length, int keep)
2907
0
{
2908
0
   int handled = 0; /* the chunk was handled */
2909
2910
0
   png_debug(1, "in png_handle_unknown");
2911
2912
0
#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
2913
   /* NOTE: this code is based on the code in libpng-1.4.12 except for fixing
2914
    * the bug which meant that setting a non-default behavior for a specific
2915
    * chunk would be ignored (the default was always used unless a user
2916
    * callback was installed).
2917
    *
2918
    * 'keep' is the value from the png_chunk_unknown_handling, the setting for
2919
    * this specific chunk_name, if PNG_HANDLE_AS_UNKNOWN_SUPPORTED, if not it
2920
    * will always be PNG_HANDLE_CHUNK_AS_DEFAULT and it needs to be set here.
2921
    * This is just an optimization to avoid multiple calls to the lookup
2922
    * function.
2923
    */
2924
#  ifndef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
2925
#     ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
2926
   keep = png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name);
2927
#     endif
2928
#  endif
2929
2930
   /* One of the following methods will read the chunk or skip it (at least one
2931
    * of these is always defined because this is the only way to switch on
2932
    * PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
2933
    */
2934
0
#  ifdef PNG_READ_USER_CHUNKS_SUPPORTED
2935
   /* The user callback takes precedence over the chunk keep value, but the
2936
    * keep value is still required to validate a save of a critical chunk.
2937
    */
2938
0
   if (png_ptr->read_user_chunk_fn != NULL)
2939
0
   {
2940
0
      if (png_cache_unknown_chunk(png_ptr, length) != 0)
2941
0
      {
2942
         /* Callback to user unknown chunk handler */
2943
0
         int ret = (*(png_ptr->read_user_chunk_fn))(png_ptr,
2944
0
             &png_ptr->unknown_chunk);
2945
2946
         /* ret is:
2947
          * negative: An error occurred; png_chunk_error will be called.
2948
          *     zero: The chunk was not handled, the chunk will be discarded
2949
          *           unless png_set_keep_unknown_chunks has been used to set
2950
          *           a 'keep' behavior for this particular chunk, in which
2951
          *           case that will be used.  A critical chunk will cause an
2952
          *           error at this point unless it is to be saved.
2953
          * positive: The chunk was handled, libpng will ignore/discard it.
2954
          */
2955
0
         if (ret < 0)
2956
0
            png_chunk_error(png_ptr, "error in user chunk");
2957
2958
0
         else if (ret == 0)
2959
0
         {
2960
            /* If the keep value is 'default' or 'never' override it, but
2961
             * still error out on critical chunks unless the keep value is
2962
             * 'always'  While this is weird it is the behavior in 1.4.12.
2963
             * A possible improvement would be to obey the value set for the
2964
             * chunk, but this would be an API change that would probably
2965
             * damage some applications.
2966
             *
2967
             * The png_app_warning below catches the case that matters, where
2968
             * the application has not set specific save or ignore for this
2969
             * chunk or global save or ignore.
2970
             */
2971
0
            if (keep < PNG_HANDLE_CHUNK_IF_SAFE)
2972
0
            {
2973
0
#              ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
2974
0
               if (png_ptr->unknown_default < PNG_HANDLE_CHUNK_IF_SAFE)
2975
0
               {
2976
0
                  png_chunk_warning(png_ptr, "Saving unknown chunk:");
2977
0
                  png_app_warning(png_ptr,
2978
0
                      "forcing save of an unhandled chunk;"
2979
0
                      " please call png_set_keep_unknown_chunks");
2980
                      /* with keep = PNG_HANDLE_CHUNK_IF_SAFE */
2981
0
               }
2982
0
#              endif
2983
0
               keep = PNG_HANDLE_CHUNK_IF_SAFE;
2984
0
            }
2985
0
         }
2986
2987
0
         else /* chunk was handled */
2988
0
         {
2989
0
            handled = 1;
2990
            /* Critical chunks can be safely discarded at this point. */
2991
0
            keep = PNG_HANDLE_CHUNK_NEVER;
2992
0
         }
2993
0
      }
2994
2995
0
      else
2996
0
         keep = PNG_HANDLE_CHUNK_NEVER; /* insufficient memory */
2997
0
   }
2998
2999
0
   else
3000
   /* Use the SAVE_UNKNOWN_CHUNKS code or skip the chunk */
3001
0
#  endif /* READ_USER_CHUNKS */
3002
3003
0
#  ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
3004
0
   {
3005
      /* keep is currently just the per-chunk setting, if there was no
3006
       * setting change it to the global default now (not that this may
3007
       * still be AS_DEFAULT) then obtain the cache of the chunk if required,
3008
       * if not simply skip the chunk.
3009
       */
3010
0
      if (keep == PNG_HANDLE_CHUNK_AS_DEFAULT)
3011
0
         keep = png_ptr->unknown_default;
3012
3013
0
      if (keep == PNG_HANDLE_CHUNK_ALWAYS ||
3014
0
         (keep == PNG_HANDLE_CHUNK_IF_SAFE &&
3015
0
          PNG_CHUNK_ANCILLARY(png_ptr->chunk_name)))
3016
0
      {
3017
0
         if (png_cache_unknown_chunk(png_ptr, length) == 0)
3018
0
            keep = PNG_HANDLE_CHUNK_NEVER;
3019
0
      }
3020
3021
0
      else
3022
0
         png_crc_finish(png_ptr, length);
3023
0
   }
3024
#  else
3025
#     ifndef PNG_READ_USER_CHUNKS_SUPPORTED
3026
#        error no method to support READ_UNKNOWN_CHUNKS
3027
#     endif
3028
3029
   {
3030
      /* If here there is no read callback pointer set and no support is
3031
       * compiled in to just save the unknown chunks, so simply skip this
3032
       * chunk.  If 'keep' is something other than AS_DEFAULT or NEVER then
3033
       * the app has erroneously asked for unknown chunk saving when there
3034
       * is no support.
3035
       */
3036
      if (keep > PNG_HANDLE_CHUNK_NEVER)
3037
         png_app_error(png_ptr, "no unknown chunk support available");
3038
3039
      png_crc_finish(png_ptr, length);
3040
   }
3041
#  endif
3042
3043
0
#  ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
3044
   /* Now store the chunk in the chunk list if appropriate, and if the limits
3045
    * permit it.
3046
    */
3047
0
   if (keep == PNG_HANDLE_CHUNK_ALWAYS ||
3048
0
      (keep == PNG_HANDLE_CHUNK_IF_SAFE &&
3049
0
       PNG_CHUNK_ANCILLARY(png_ptr->chunk_name)))
3050
0
   {
3051
0
#     ifdef PNG_USER_LIMITS_SUPPORTED
3052
0
      switch (png_ptr->user_chunk_cache_max)
3053
0
      {
3054
0
         case 2:
3055
0
            png_ptr->user_chunk_cache_max = 1;
3056
0
            png_chunk_benign_error(png_ptr, "no space in chunk cache");
3057
            /* FALLTHROUGH */
3058
0
         case 1:
3059
            /* NOTE: prior to 1.6.0 this case resulted in an unknown critical
3060
             * chunk being skipped, now there will be a hard error below.
3061
             */
3062
0
            break;
3063
3064
0
         default: /* not at limit */
3065
0
            --(png_ptr->user_chunk_cache_max);
3066
            /* FALLTHROUGH */
3067
0
         case 0: /* no limit */
3068
0
#  endif /* USER_LIMITS */
3069
            /* Here when the limit isn't reached or when limits are compiled
3070
             * out; store the chunk.
3071
             */
3072
0
            png_set_unknown_chunks(png_ptr, info_ptr,
3073
0
                &png_ptr->unknown_chunk, 1);
3074
0
            handled = 1;
3075
0
#  ifdef PNG_USER_LIMITS_SUPPORTED
3076
0
            break;
3077
0
      }
3078
0
#  endif
3079
0
   }
3080
#  else /* no store support: the chunk must be handled by the user callback */
3081
   PNG_UNUSED(info_ptr)
3082
#  endif
3083
3084
   /* Regardless of the error handling below the cached data (if any) can be
3085
    * freed now.  Notice that the data is not freed if there is a png_error, but
3086
    * it will be freed by destroy_read_struct.
3087
    */
3088
0
   if (png_ptr->unknown_chunk.data != NULL)
3089
0
      png_free(png_ptr, png_ptr->unknown_chunk.data);
3090
0
   png_ptr->unknown_chunk.data = NULL;
3091
3092
#else /* !PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */
3093
   /* There is no support to read an unknown chunk, so just skip it. */
3094
   png_crc_finish(png_ptr, length);
3095
   PNG_UNUSED(info_ptr)
3096
   PNG_UNUSED(keep)
3097
#endif /* !READ_UNKNOWN_CHUNKS */
3098
3099
   /* Check for unhandled critical chunks */
3100
0
   if (handled == 0 && PNG_CHUNK_CRITICAL(png_ptr->chunk_name))
3101
0
      png_chunk_error(png_ptr, "unhandled critical chunk");
3102
0
}
3103
3104
/* This function is called to verify that a chunk name is valid.
3105
 * This function can't have the "critical chunk check" incorporated
3106
 * into it, since in the future we will need to be able to call user
3107
 * functions to handle unknown critical chunks after we check that
3108
 * the chunk name itself is valid.
3109
 */
3110
3111
/* Bit hacking: the test for an invalid byte in the 4 byte chunk name is:
3112
 *
3113
 * ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
3114
 */
3115
3116
void /* PRIVATE */
3117
png_check_chunk_name(png_const_structrp png_ptr, const png_uint_32 chunk_name)
3118
0
{
3119
0
   int i;
3120
0
   png_uint_32 cn=chunk_name;
3121
3122
0
   png_debug(1, "in png_check_chunk_name");
3123
3124
0
   for (i=1; i<=4; ++i)
3125
0
   {
3126
0
      int c = cn & 0xff;
3127
3128
0
      if (c < 65 || c > 122 || (c > 90 && c < 97))
3129
0
         png_chunk_error(png_ptr, "invalid chunk type");
3130
3131
0
      cn >>= 8;
3132
0
   }
3133
0
}
3134
3135
void /* PRIVATE */
3136
png_check_chunk_length(png_const_structrp png_ptr, const png_uint_32 length)
3137
0
{
3138
0
   png_alloc_size_t limit = PNG_UINT_31_MAX;
3139
3140
0
# ifdef PNG_SET_USER_LIMITS_SUPPORTED
3141
0
   if (png_ptr->user_chunk_malloc_max > 0 &&
3142
0
       png_ptr->user_chunk_malloc_max < limit)
3143
0
      limit = png_ptr->user_chunk_malloc_max;
3144
# elif PNG_USER_CHUNK_MALLOC_MAX > 0
3145
   if (PNG_USER_CHUNK_MALLOC_MAX < limit)
3146
      limit = PNG_USER_CHUNK_MALLOC_MAX;
3147
# endif
3148
0
   if (png_ptr->chunk_name == png_IDAT)
3149
0
   {
3150
0
      png_alloc_size_t idat_limit = PNG_UINT_31_MAX;
3151
0
      size_t row_factor =
3152
0
         (png_ptr->width * png_ptr->channels * (png_ptr->bit_depth > 8? 2: 1)
3153
0
          + 1 + (png_ptr->interlaced? 6: 0));
3154
0
      if (png_ptr->height > PNG_UINT_32_MAX/row_factor)
3155
0
         idat_limit=PNG_UINT_31_MAX;
3156
0
      else
3157
0
         idat_limit = png_ptr->height * row_factor;
3158
0
      row_factor = row_factor > 32566? 32566 : row_factor;
3159
0
      idat_limit += 6 + 5*(idat_limit/row_factor+1); /* zlib+deflate overhead */
3160
0
      idat_limit=idat_limit < PNG_UINT_31_MAX? idat_limit : PNG_UINT_31_MAX;
3161
0
      limit = limit < idat_limit? idat_limit : limit;
3162
0
   }
3163
3164
0
   if (length > limit)
3165
0
   {
3166
0
      png_debug2(0," length = %lu, limit = %lu",
3167
0
         (unsigned long)length,(unsigned long)limit);
3168
0
      png_chunk_error(png_ptr, "chunk data is too large");
3169
0
   }
3170
0
}
3171
3172
/* Combines the row recently read in with the existing pixels in the row.  This
3173
 * routine takes care of alpha and transparency if requested.  This routine also
3174
 * handles the two methods of progressive display of interlaced images,
3175
 * depending on the 'display' value; if 'display' is true then the whole row
3176
 * (dp) is filled from the start by replicating the available pixels.  If
3177
 * 'display' is false only those pixels present in the pass are filled in.
3178
 */
3179
void /* PRIVATE */
3180
png_combine_row(png_const_structrp png_ptr, png_bytep dp, int display)
3181
0
{
3182
0
   unsigned int pixel_depth = png_ptr->transformed_pixel_depth;
3183
0
   png_const_bytep sp = png_ptr->row_buf + 1;
3184
0
   png_alloc_size_t row_width = png_ptr->width;
3185
0
   unsigned int pass = png_ptr->pass;
3186
0
   png_bytep end_ptr = 0;
3187
0
   png_byte end_byte = 0;
3188
0
   unsigned int end_mask;
3189
3190
0
   png_debug(1, "in png_combine_row");
3191
3192
   /* Added in 1.5.6: it should not be possible to enter this routine until at
3193
    * least one row has been read from the PNG data and transformed.
3194
    */
3195
0
   if (pixel_depth == 0)
3196
0
      png_error(png_ptr, "internal row logic error");
3197
3198
   /* Added in 1.5.4: the pixel depth should match the information returned by
3199
    * any call to png_read_update_info at this point.  Do not continue if we got
3200
    * this wrong.
3201
    */
3202
0
   if (png_ptr->info_rowbytes != 0 && png_ptr->info_rowbytes !=
3203
0
          PNG_ROWBYTES(pixel_depth, row_width))
3204
0
      png_error(png_ptr, "internal row size calculation error");
3205
3206
   /* Don't expect this to ever happen: */
3207
0
   if (row_width == 0)
3208
0
      png_error(png_ptr, "internal row width error");
3209
3210
   /* Preserve the last byte in cases where only part of it will be overwritten,
3211
    * the multiply below may overflow, we don't care because ANSI-C guarantees
3212
    * we get the low bits.
3213
    */
3214
0
   end_mask = (pixel_depth * row_width) & 7;
3215
0
   if (end_mask != 0)
3216
0
   {
3217
      /* end_ptr == NULL is a flag to say do nothing */
3218
0
      end_ptr = dp + PNG_ROWBYTES(pixel_depth, row_width) - 1;
3219
0
      end_byte = *end_ptr;
3220
0
#     ifdef PNG_READ_PACKSWAP_SUPPORTED
3221
0
      if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
3222
         /* little-endian byte */
3223
0
         end_mask = (unsigned int)(0xff << end_mask);
3224
3225
0
      else /* big-endian byte */
3226
0
#     endif
3227
0
      end_mask = 0xff >> end_mask;
3228
      /* end_mask is now the bits to *keep* from the destination row */
3229
0
   }
3230
3231
   /* For non-interlaced images this reduces to a memcpy(). A memcpy()
3232
    * will also happen if interlacing isn't supported or if the application
3233
    * does not call png_set_interlace_handling().  In the latter cases the
3234
    * caller just gets a sequence of the unexpanded rows from each interlace
3235
    * pass.
3236
    */
3237
0
#ifdef PNG_READ_INTERLACING_SUPPORTED
3238
0
   if (png_ptr->interlaced != 0 &&
3239
0
       (png_ptr->transformations & PNG_INTERLACE) != 0 &&
3240
0
       pass < 6 && (display == 0 ||
3241
       /* The following copies everything for 'display' on passes 0, 2 and 4. */
3242
0
       (display == 1 && (pass & 1) != 0)))
3243
0
   {
3244
      /* Narrow images may have no bits in a pass; the caller should handle
3245
       * this, but this test is cheap:
3246
       */
3247
0
      if (row_width <= PNG_PASS_START_COL(pass))
3248
0
         return;
3249
3250
0
      if (pixel_depth < 8)
3251
0
      {
3252
         /* For pixel depths up to 4 bpp the 8-pixel mask can be expanded to fit
3253
          * into 32 bits, then a single loop over the bytes using the four byte
3254
          * values in the 32-bit mask can be used.  For the 'display' option the
3255
          * expanded mask may also not require any masking within a byte.  To
3256
          * make this work the PACKSWAP option must be taken into account - it
3257
          * simply requires the pixels to be reversed in each byte.
3258
          *
3259
          * The 'regular' case requires a mask for each of the first 6 passes,
3260
          * the 'display' case does a copy for the even passes in the range
3261
          * 0..6.  This has already been handled in the test above.
3262
          *
3263
          * The masks are arranged as four bytes with the first byte to use in
3264
          * the lowest bits (little-endian) regardless of the order (PACKSWAP or
3265
          * not) of the pixels in each byte.
3266
          *
3267
          * NOTE: the whole of this logic depends on the caller of this function
3268
          * only calling it on rows appropriate to the pass.  This function only
3269
          * understands the 'x' logic; the 'y' logic is handled by the caller.
3270
          *
3271
          * The following defines allow generation of compile time constant bit
3272
          * masks for each pixel depth and each possibility of swapped or not
3273
          * swapped bytes.  Pass 'p' is in the range 0..6; 'x', a pixel index,
3274
          * is in the range 0..7; and the result is 1 if the pixel is to be
3275
          * copied in the pass, 0 if not.  'S' is for the sparkle method, 'B'
3276
          * for the block method.
3277
          *
3278
          * With some compilers a compile time expression of the general form:
3279
          *
3280
          *    (shift >= 32) ? (a >> (shift-32)) : (b >> shift)
3281
          *
3282
          * Produces warnings with values of 'shift' in the range 33 to 63
3283
          * because the right hand side of the ?: expression is evaluated by
3284
          * the compiler even though it isn't used.  Microsoft Visual C (various
3285
          * versions) and the Intel C compiler are known to do this.  To avoid
3286
          * this the following macros are used in 1.5.6.  This is a temporary
3287
          * solution to avoid destabilizing the code during the release process.
3288
          */
3289
0
#        if PNG_USE_COMPILE_TIME_MASKS
3290
0
#           define PNG_LSR(x,s) ((x)>>((s) & 0x1f))
3291
0
#           define PNG_LSL(x,s) ((x)<<((s) & 0x1f))
3292
#        else
3293
#           define PNG_LSR(x,s) ((x)>>(s))
3294
#           define PNG_LSL(x,s) ((x)<<(s))
3295
#        endif
3296
0
#        define S_COPY(p,x) (((p)<4 ? PNG_LSR(0x80088822,(3-(p))*8+(7-(x))) :\
3297
0
           PNG_LSR(0xaa55ff00,(7-(p))*8+(7-(x)))) & 1)
3298
0
#        define B_COPY(p,x) (((p)<4 ? PNG_LSR(0xff0fff33,(3-(p))*8+(7-(x))) :\
3299
0
           PNG_LSR(0xff55ff00,(7-(p))*8+(7-(x)))) & 1)
3300
3301
         /* Return a mask for pass 'p' pixel 'x' at depth 'd'.  The mask is
3302
          * little endian - the first pixel is at bit 0 - however the extra
3303
          * parameter 's' can be set to cause the mask position to be swapped
3304
          * within each byte, to match the PNG format.  This is done by XOR of
3305
          * the shift with 7, 6 or 4 for bit depths 1, 2 and 4.
3306
          */
3307
0
#        define PIXEL_MASK(p,x,d,s) \
3308
0
            (PNG_LSL(((PNG_LSL(1U,(d)))-1),(((x)*(d))^((s)?8-(d):0))))
3309
3310
         /* Hence generate the appropriate 'block' or 'sparkle' pixel copy mask.
3311
          */
3312
0
#        define S_MASKx(p,x,d,s) (S_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
3313
0
#        define B_MASKx(p,x,d,s) (B_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
3314
3315
         /* Combine 8 of these to get the full mask.  For the 1-bpp and 2-bpp
3316
          * cases the result needs replicating, for the 4-bpp case the above
3317
          * generates a full 32 bits.
3318
          */
3319
0
#        define MASK_EXPAND(m,d) ((m)*((d)==1?0x01010101:((d)==2?0x00010001:1)))
3320
3321
0
#        define S_MASK(p,d,s) MASK_EXPAND(S_MASKx(p,0,d,s) + S_MASKx(p,1,d,s) +\
3322
0
            S_MASKx(p,2,d,s) + S_MASKx(p,3,d,s) + S_MASKx(p,4,d,s) +\
3323
0
            S_MASKx(p,5,d,s) + S_MASKx(p,6,d,s) + S_MASKx(p,7,d,s), d)
3324
3325
0
#        define B_MASK(p,d,s) MASK_EXPAND(B_MASKx(p,0,d,s) + B_MASKx(p,1,d,s) +\
3326
0
            B_MASKx(p,2,d,s) + B_MASKx(p,3,d,s) + B_MASKx(p,4,d,s) +\
3327
0
            B_MASKx(p,5,d,s) + B_MASKx(p,6,d,s) + B_MASKx(p,7,d,s), d)
3328
3329
0
#if PNG_USE_COMPILE_TIME_MASKS
3330
         /* Utility macros to construct all the masks for a depth/swap
3331
          * combination.  The 's' parameter says whether the format is PNG
3332
          * (big endian bytes) or not.  Only the three odd-numbered passes are
3333
          * required for the display/block algorithm.
3334
          */
3335
0
#        define S_MASKS(d,s) { S_MASK(0,d,s), S_MASK(1,d,s), S_MASK(2,d,s),\
3336
0
            S_MASK(3,d,s), S_MASK(4,d,s), S_MASK(5,d,s) }
3337
3338
0
#        define B_MASKS(d,s) { B_MASK(1,d,s), B_MASK(3,d,s), B_MASK(5,d,s) }
3339
3340
0
#        define DEPTH_INDEX(d) ((d)==1?0:((d)==2?1:2))
3341
3342
         /* Hence the pre-compiled masks indexed by PACKSWAP (or not), depth and
3343
          * then pass:
3344
          */
3345
0
         static PNG_CONST png_uint_32 row_mask[2/*PACKSWAP*/][3/*depth*/][6] =
3346
0
         {
3347
            /* Little-endian byte masks for PACKSWAP */
3348
0
            { S_MASKS(1,0), S_MASKS(2,0), S_MASKS(4,0) },
3349
            /* Normal (big-endian byte) masks - PNG format */
3350
0
            { S_MASKS(1,1), S_MASKS(2,1), S_MASKS(4,1) }
3351
0
         };
3352
3353
         /* display_mask has only three entries for the odd passes, so index by
3354
          * pass>>1.
3355
          */
3356
0
         static PNG_CONST png_uint_32 display_mask[2][3][3] =
3357
0
         {
3358
            /* Little-endian byte masks for PACKSWAP */
3359
0
            { B_MASKS(1,0), B_MASKS(2,0), B_MASKS(4,0) },
3360
            /* Normal (big-endian byte) masks - PNG format */
3361
0
            { B_MASKS(1,1), B_MASKS(2,1), B_MASKS(4,1) }
3362
0
         };
3363
3364
0
#        define MASK(pass,depth,display,png)\
3365
0
            ((display)?display_mask[png][DEPTH_INDEX(depth)][pass>>1]:\
3366
0
               row_mask[png][DEPTH_INDEX(depth)][pass])
3367
3368
#else /* !PNG_USE_COMPILE_TIME_MASKS */
3369
         /* This is the runtime alternative: it seems unlikely that this will
3370
          * ever be either smaller or faster than the compile time approach.
3371
          */
3372
#        define MASK(pass,depth,display,png)\
3373
            ((display)?B_MASK(pass,depth,png):S_MASK(pass,depth,png))
3374
#endif /* !USE_COMPILE_TIME_MASKS */
3375
3376
         /* Use the appropriate mask to copy the required bits.  In some cases
3377
          * the byte mask will be 0 or 0xff; optimize these cases.  row_width is
3378
          * the number of pixels, but the code copies bytes, so it is necessary
3379
          * to special case the end.
3380
          */
3381
0
         png_uint_32 pixels_per_byte = 8 / pixel_depth;
3382
0
         png_uint_32 mask;
3383
3384
0
#        ifdef PNG_READ_PACKSWAP_SUPPORTED
3385
0
         if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
3386
0
            mask = MASK(pass, pixel_depth, display, 0);
3387
3388
0
         else
3389
0
#        endif
3390
0
         mask = MASK(pass, pixel_depth, display, 1);
3391
3392
0
         for (;;)
3393
0
         {
3394
0
            png_uint_32 m;
3395
3396
            /* It doesn't matter in the following if png_uint_32 has more than
3397
             * 32 bits because the high bits always match those in m<<24; it is,
3398
             * however, essential to use OR here, not +, because of this.
3399
             */
3400
0
            m = mask;
3401
0
            mask = (m >> 8) | (m << 24); /* rotate right to good compilers */
3402
0
            m &= 0xff;
3403
3404
0
            if (m != 0) /* something to copy */
3405
0
            {
3406
0
               if (m != 0xff)
3407
0
                  *dp = (png_byte)((*dp & ~m) | (*sp & m));
3408
0
               else
3409
0
                  *dp = *sp;
3410
0
            }
3411
3412
            /* NOTE: this may overwrite the last byte with garbage if the image
3413
             * is not an exact number of bytes wide; libpng has always done
3414
             * this.
3415
             */
3416
0
            if (row_width <= pixels_per_byte)
3417
0
               break; /* May need to restore part of the last byte */
3418
3419
0
            row_width -= pixels_per_byte;
3420
0
            ++dp;
3421
0
            ++sp;
3422
0
         }
3423
0
      }
3424
3425
0
      else /* pixel_depth >= 8 */
3426
0
      {
3427
0
         unsigned int bytes_to_copy, bytes_to_jump;
3428
3429
         /* Validate the depth - it must be a multiple of 8 */
3430
0
         if (pixel_depth & 7)
3431
0
            png_error(png_ptr, "invalid user transform pixel depth");
3432
3433
0
         pixel_depth >>= 3; /* now in bytes */
3434
0
         row_width *= pixel_depth;
3435
3436
         /* Regardless of pass number the Adam 7 interlace always results in a
3437
          * fixed number of pixels to copy then to skip.  There may be a
3438
          * different number of pixels to skip at the start though.
3439
          */
3440
0
         {
3441
0
            unsigned int offset = PNG_PASS_START_COL(pass) * pixel_depth;
3442
3443
0
            row_width -= offset;
3444
0
            dp += offset;
3445
0
            sp += offset;
3446
0
         }
3447
3448
         /* Work out the bytes to copy. */
3449
0
         if (display != 0)
3450
0
         {
3451
            /* When doing the 'block' algorithm the pixel in the pass gets
3452
             * replicated to adjacent pixels.  This is why the even (0,2,4,6)
3453
             * passes are skipped above - the entire expanded row is copied.
3454
             */
3455
0
            bytes_to_copy = (1<<((6-pass)>>1)) * pixel_depth;
3456
3457
            /* But don't allow this number to exceed the actual row width. */
3458
0
            if (bytes_to_copy > row_width)
3459
0
               bytes_to_copy = (unsigned int)/*SAFE*/row_width;
3460
0
         }
3461
3462
0
         else /* normal row; Adam7 only ever gives us one pixel to copy. */
3463
0
            bytes_to_copy = pixel_depth;
3464
3465
         /* In Adam7 there is a constant offset between where the pixels go. */
3466
0
         bytes_to_jump = PNG_PASS_COL_OFFSET(pass) * pixel_depth;
3467
3468
         /* And simply copy these bytes.  Some optimization is possible here,
3469
          * depending on the value of 'bytes_to_copy'.  Special case the low
3470
          * byte counts, which we know to be frequent.
3471
          *
3472
          * Notice that these cases all 'return' rather than 'break' - this
3473
          * avoids an unnecessary test on whether to restore the last byte
3474
          * below.
3475
          */
3476
0
         switch (bytes_to_copy)
3477
0
         {
3478
0
            case 1:
3479
0
               for (;;)
3480
0
               {
3481
0
                  *dp = *sp;
3482
3483
0
                  if (row_width <= bytes_to_jump)
3484
0
                     return;
3485
3486
0
                  dp += bytes_to_jump;
3487
0
                  sp += bytes_to_jump;
3488
0
                  row_width -= bytes_to_jump;
3489
0
               }
3490
3491
0
            case 2:
3492
               /* There is a possibility of a partial copy at the end here; this
3493
                * slows the code down somewhat.
3494
                */
3495
0
               do
3496
0
               {
3497
0
                  dp[0] = sp[0]; dp[1] = sp[1];
3498
3499
0
                  if (row_width <= bytes_to_jump)
3500
0
                     return;
3501
3502
0
                  sp += bytes_to_jump;
3503
0
                  dp += bytes_to_jump;
3504
0
                  row_width -= bytes_to_jump;
3505
0
               }
3506
0
               while (row_width > 1);
3507
3508
               /* And there can only be one byte left at this point: */
3509
0
               *dp = *sp;
3510
0
               return;
3511
3512
0
            case 3:
3513
               /* This can only be the RGB case, so each copy is exactly one
3514
                * pixel and it is not necessary to check for a partial copy.
3515
                */
3516
0
               for (;;)
3517
0
               {
3518
0
                  dp[0] = sp[0]; dp[1] = sp[1]; dp[2] = sp[2];
3519
3520
0
                  if (row_width <= bytes_to_jump)
3521
0
                     return;
3522
3523
0
                  sp += bytes_to_jump;
3524
0
                  dp += bytes_to_jump;
3525
0
                  row_width -= bytes_to_jump;
3526
0
               }
3527
3528
0
            default:
3529
0
#if PNG_ALIGN_TYPE != PNG_ALIGN_NONE
3530
               /* Check for double byte alignment and, if possible, use a
3531
                * 16-bit copy.  Don't attempt this for narrow images - ones that
3532
                * are less than an interlace panel wide.  Don't attempt it for
3533
                * wide bytes_to_copy either - use the memcpy there.
3534
                */
3535
0
               if (bytes_to_copy < 16 /*else use memcpy*/ &&
3536
0
                   png_isaligned(dp, png_uint_16) &&
3537
0
                   png_isaligned(sp, png_uint_16) &&
3538
0
                   bytes_to_copy % (sizeof (png_uint_16)) == 0 &&
3539
0
                   bytes_to_jump % (sizeof (png_uint_16)) == 0)
3540
0
               {
3541
                  /* Everything is aligned for png_uint_16 copies, but try for
3542
                   * png_uint_32 first.
3543
                   */
3544
0
                  if (png_isaligned(dp, png_uint_32) &&
3545
0
                      png_isaligned(sp, png_uint_32) &&
3546
0
                      bytes_to_copy % (sizeof (png_uint_32)) == 0 &&
3547
0
                      bytes_to_jump % (sizeof (png_uint_32)) == 0)
3548
0
                  {
3549
0
                     png_uint_32p dp32 = png_aligncast(png_uint_32p,dp);
3550
0
                     png_const_uint_32p sp32 = png_aligncastconst(
3551
0
                         png_const_uint_32p, sp);
3552
0
                     size_t skip = (bytes_to_jump-bytes_to_copy) /
3553
0
                         (sizeof (png_uint_32));
3554
3555
0
                     do
3556
0
                     {
3557
0
                        size_t c = bytes_to_copy;
3558
0
                        do
3559
0
                        {
3560
0
                           *dp32++ = *sp32++;
3561
0
                           c -= (sizeof (png_uint_32));
3562
0
                        }
3563
0
                        while (c > 0);
3564
3565
0
                        if (row_width <= bytes_to_jump)
3566
0
                           return;
3567
3568
0
                        dp32 += skip;
3569
0
                        sp32 += skip;
3570
0
                        row_width -= bytes_to_jump;
3571
0
                     }
3572
0
                     while (bytes_to_copy <= row_width);
3573
3574
                     /* Get to here when the row_width truncates the final copy.
3575
                      * There will be 1-3 bytes left to copy, so don't try the
3576
                      * 16-bit loop below.
3577
                      */
3578
0
                     dp = (png_bytep)dp32;
3579
0
                     sp = (png_const_bytep)sp32;
3580
0
                     do
3581
0
                        *dp++ = *sp++;
3582
0
                     while (--row_width > 0);
3583
0
                     return;
3584
0
                  }
3585
3586
                  /* Else do it in 16-bit quantities, but only if the size is
3587
                   * not too large.
3588
                   */
3589
0
                  else
3590
0
                  {
3591
0
                     png_uint_16p dp16 = png_aligncast(png_uint_16p, dp);
3592
0
                     png_const_uint_16p sp16 = png_aligncastconst(
3593
0
                        png_const_uint_16p, sp);
3594
0
                     size_t skip = (bytes_to_jump-bytes_to_copy) /
3595
0
                        (sizeof (png_uint_16));
3596
3597
0
                     do
3598
0
                     {
3599
0
                        size_t c = bytes_to_copy;
3600
0
                        do
3601
0
                        {
3602
0
                           *dp16++ = *sp16++;
3603
0
                           c -= (sizeof (png_uint_16));
3604
0
                        }
3605
0
                        while (c > 0);
3606
3607
0
                        if (row_width <= bytes_to_jump)
3608
0
                           return;
3609
3610
0
                        dp16 += skip;
3611
0
                        sp16 += skip;
3612
0
                        row_width -= bytes_to_jump;
3613
0
                     }
3614
0
                     while (bytes_to_copy <= row_width);
3615
3616
                     /* End of row - 1 byte left, bytes_to_copy > row_width: */
3617
0
                     dp = (png_bytep)dp16;
3618
0
                     sp = (png_const_bytep)sp16;
3619
0
                     do
3620
0
                        *dp++ = *sp++;
3621
0
                     while (--row_width > 0);
3622
0
                     return;
3623
0
                  }
3624
0
               }
3625
0
#endif /* ALIGN_TYPE code */
3626
3627
               /* The true default - use a memcpy: */
3628
0
               for (;;)
3629
0
               {
3630
0
                  memcpy(dp, sp, bytes_to_copy);
3631
3632
0
                  if (row_width <= bytes_to_jump)
3633
0
                     return;
3634
3635
0
                  sp += bytes_to_jump;
3636
0
                  dp += bytes_to_jump;
3637
0
                  row_width -= bytes_to_jump;
3638
0
                  if (bytes_to_copy > row_width)
3639
0
                     bytes_to_copy = (unsigned int)/*SAFE*/row_width;
3640
0
               }
3641
0
         }
3642
3643
         /* NOT REACHED*/
3644
0
      } /* pixel_depth >= 8 */
3645
3646
      /* Here if pixel_depth < 8 to check 'end_ptr' below. */
3647
0
   }
3648
0
   else
3649
0
#endif /* READ_INTERLACING */
3650
3651
   /* If here then the switch above wasn't used so just memcpy the whole row
3652
    * from the temporary row buffer (notice that this overwrites the end of the
3653
    * destination row if it is a partial byte.)
3654
    */
3655
0
   memcpy(dp, sp, PNG_ROWBYTES(pixel_depth, row_width));
3656
3657
   /* Restore the overwritten bits from the last byte if necessary. */
3658
0
   if (end_ptr != NULL)
3659
0
      *end_ptr = (png_byte)((end_byte & end_mask) | (*end_ptr & ~end_mask));
3660
0
}
3661
3662
#ifdef PNG_READ_INTERLACING_SUPPORTED
3663
void /* PRIVATE */
3664
png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
3665
    png_uint_32 transformations /* Because these may affect the byte layout */)
3666
0
{
3667
   /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
3668
   /* Offset to next interlace block */
3669
0
   static PNG_CONST unsigned int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
3670
3671
0
   png_debug(1, "in png_do_read_interlace");
3672
0
   if (row != NULL && row_info != NULL)
3673
0
   {
3674
0
      png_uint_32 final_width;
3675
3676
0
      final_width = row_info->width * png_pass_inc[pass];
3677
3678
0
      switch (row_info->pixel_depth)
3679
0
      {
3680
0
         case 1:
3681
0
         {
3682
0
            png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3);
3683
0
            png_bytep dp = row + (png_size_t)((final_width - 1) >> 3);
3684
0
            unsigned int sshift, dshift;
3685
0
            unsigned int s_start, s_end;
3686
0
            int s_inc;
3687
0
            int jstop = (int)png_pass_inc[pass];
3688
0
            png_byte v;
3689
0
            png_uint_32 i;
3690
0
            int j;
3691
3692
0
#ifdef PNG_READ_PACKSWAP_SUPPORTED
3693
0
            if ((transformations & PNG_PACKSWAP) != 0)
3694
0
            {
3695
0
                sshift = ((row_info->width + 7) & 0x07);
3696
0
                dshift = ((final_width + 7) & 0x07);
3697
0
                s_start = 7;
3698
0
                s_end = 0;
3699
0
                s_inc = -1;
3700
0
            }
3701
3702
0
            else
3703
0
#endif
3704
0
            {
3705
0
                sshift = 7 - ((row_info->width + 7) & 0x07);
3706
0
                dshift = 7 - ((final_width + 7) & 0x07);
3707
0
                s_start = 0;
3708
0
                s_end = 7;
3709
0
                s_inc = 1;
3710
0
            }
3711
3712
0
            for (i = 0; i < row_info->width; i++)
3713
0
            {
3714
0
               v = (png_byte)((*sp >> sshift) & 0x01);
3715
0
               for (j = 0; j < jstop; j++)
3716
0
               {
3717
0
                  unsigned int tmp = *dp & (0x7f7f >> (7 - dshift));
3718
0
                  tmp |= (unsigned int)(v << dshift);
3719
0
                  *dp = (png_byte)(tmp & 0xff);
3720
3721
0
                  if (dshift == s_end)
3722
0
                  {
3723
0
                     dshift = s_start;
3724
0
                     dp--;
3725
0
                  }
3726
3727
0
                  else
3728
0
                     dshift = (unsigned int)((int)dshift + s_inc);
3729
0
               }
3730
3731
0
               if (sshift == s_end)
3732
0
               {
3733
0
                  sshift = s_start;
3734
0
                  sp--;
3735
0
               }
3736
3737
0
               else
3738
0
                  sshift = (unsigned int)((int)sshift + s_inc);
3739
0
            }
3740
0
            break;
3741
0
         }
3742
3743
0
         case 2:
3744
0
         {
3745
0
            png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);
3746
0
            png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);
3747
0
            unsigned int sshift, dshift;
3748
0
            unsigned int s_start, s_end;
3749
0
            int s_inc;
3750
0
            int jstop = (int)png_pass_inc[pass];
3751
0
            png_uint_32 i;
3752
3753
0
#ifdef PNG_READ_PACKSWAP_SUPPORTED
3754
0
            if ((transformations & PNG_PACKSWAP) != 0)
3755
0
            {
3756
0
               sshift = (((row_info->width + 3) & 0x03) << 1);
3757
0
               dshift = (((final_width + 3) & 0x03) << 1);
3758
0
               s_start = 6;
3759
0
               s_end = 0;
3760
0
               s_inc = -2;
3761
0
            }
3762
3763
0
            else
3764
0
#endif
3765
0
            {
3766
0
               sshift = ((3 - ((row_info->width + 3) & 0x03)) << 1);
3767
0
               dshift = ((3 - ((final_width + 3) & 0x03)) << 1);
3768
0
               s_start = 0;
3769
0
               s_end = 6;
3770
0
               s_inc = 2;
3771
0
            }
3772
3773
0
            for (i = 0; i < row_info->width; i++)
3774
0
            {
3775
0
               png_byte v;
3776
0
               int j;
3777
3778
0
               v = (png_byte)((*sp >> sshift) & 0x03);
3779
0
               for (j = 0; j < jstop; j++)
3780
0
               {
3781
0
                  unsigned int tmp = *dp & (0x3f3f >> (6 - dshift));
3782
0
                  tmp |= (unsigned int)(v << dshift);
3783
0
                  *dp = (png_byte)(tmp & 0xff);
3784
3785
0
                  if (dshift == s_end)
3786
0
                  {
3787
0
                     dshift = s_start;
3788
0
                     dp--;
3789
0
                  }
3790
3791
0
                  else
3792
0
                     dshift = (unsigned int)((int)dshift + s_inc);
3793
0
               }
3794
3795
0
               if (sshift == s_end)
3796
0
               {
3797
0
                  sshift = s_start;
3798
0
                  sp--;
3799
0
               }
3800
3801
0
               else
3802
0
                  sshift = (unsigned int)((int)sshift + s_inc);
3803
0
            }
3804
0
            break;
3805
0
         }
3806
3807
0
         case 4:
3808
0
         {
3809
0
            png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1);
3810
0
            png_bytep dp = row + (png_size_t)((final_width - 1) >> 1);
3811
0
            unsigned int sshift, dshift;
3812
0
            unsigned int s_start, s_end;
3813
0
            int s_inc;
3814
0
            png_uint_32 i;
3815
0
            int jstop = (int)png_pass_inc[pass];
3816
3817
0
#ifdef PNG_READ_PACKSWAP_SUPPORTED
3818
0
            if ((transformations & PNG_PACKSWAP) != 0)
3819
0
            {
3820
0
               sshift = (((row_info->width + 1) & 0x01) << 2);
3821
0
               dshift = (((final_width + 1) & 0x01) << 2);
3822
0
               s_start = 4;
3823
0
               s_end = 0;
3824
0
               s_inc = -4;
3825
0
            }
3826
3827
0
            else
3828
0
#endif
3829
0
            {
3830
0
               sshift = ((1 - ((row_info->width + 1) & 0x01)) << 2);
3831
0
               dshift = ((1 - ((final_width + 1) & 0x01)) << 2);
3832
0
               s_start = 0;
3833
0
               s_end = 4;
3834
0
               s_inc = 4;
3835
0
            }
3836
3837
0
            for (i = 0; i < row_info->width; i++)
3838
0
            {
3839
0
               png_byte v = (png_byte)((*sp >> sshift) & 0x0f);
3840
0
               int j;
3841
3842
0
               for (j = 0; j < jstop; j++)
3843
0
               {
3844
0
                  unsigned int tmp = *dp & (0xf0f >> (4 - dshift));
3845
0
                  tmp |= (unsigned int)(v << dshift);
3846
0
                  *dp = (png_byte)(tmp & 0xff);
3847
3848
0
                  if (dshift == s_end)
3849
0
                  {
3850
0
                     dshift = s_start;
3851
0
                     dp--;
3852
0
                  }
3853
3854
0
                  else
3855
0
                     dshift = (unsigned int)((int)dshift + s_inc);
3856
0
               }
3857
3858
0
               if (sshift == s_end)
3859
0
               {
3860
0
                  sshift = s_start;
3861
0
                  sp--;
3862
0
               }
3863
3864
0
               else
3865
0
                  sshift = (unsigned int)((int)sshift + s_inc);
3866
0
            }
3867
0
            break;
3868
0
         }
3869
3870
0
         default:
3871
0
         {
3872
0
            png_size_t pixel_bytes = (row_info->pixel_depth >> 3);
3873
3874
0
            png_bytep sp = row + (png_size_t)(row_info->width - 1)
3875
0
                * pixel_bytes;
3876
3877
0
            png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes;
3878
3879
0
            int jstop = (int)png_pass_inc[pass];
3880
0
            png_uint_32 i;
3881
3882
0
            for (i = 0; i < row_info->width; i++)
3883
0
            {
3884
0
               png_byte v[8]; /* SAFE; pixel_depth does not exceed 64 */
3885
0
               int j;
3886
3887
0
               memcpy(v, sp, pixel_bytes);
3888
3889
0
               for (j = 0; j < jstop; j++)
3890
0
               {
3891
0
                  memcpy(dp, v, pixel_bytes);
3892
0
                  dp -= pixel_bytes;
3893
0
               }
3894
3895
0
               sp -= pixel_bytes;
3896
0
            }
3897
0
            break;
3898
0
         }
3899
0
      }
3900
3901
0
      row_info->width = final_width;
3902
0
      row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width);
3903
0
   }
3904
#ifndef PNG_READ_PACKSWAP_SUPPORTED
3905
   PNG_UNUSED(transformations)  /* Silence compiler warning */
3906
#endif
3907
0
}
3908
#endif /* READ_INTERLACING */
3909
3910
static void
3911
png_read_filter_row_sub(png_row_infop row_info, png_bytep row,
3912
    png_const_bytep prev_row)
3913
0
{
3914
0
   png_size_t i;
3915
0
   png_size_t istop = row_info->rowbytes;
3916
0
   unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
3917
0
   png_bytep rp = row + bpp;
3918
3919
0
   PNG_UNUSED(prev_row)
3920
3921
0
   for (i = bpp; i < istop; i++)
3922
0
   {
3923
0
      *rp = (png_byte)(((int)(*rp) + (int)(*(rp-bpp))) & 0xff);
3924
0
      rp++;
3925
0
   }
3926
0
}
3927
3928
static void
3929
png_read_filter_row_up(png_row_infop row_info, png_bytep row,
3930
    png_const_bytep prev_row)
3931
0
{
3932
0
   png_size_t i;
3933
0
   png_size_t istop = row_info->rowbytes;
3934
0
   png_bytep rp = row;
3935
0
   png_const_bytep pp = prev_row;
3936
3937
0
   for (i = 0; i < istop; i++)
3938
0
   {
3939
0
      *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
3940
0
      rp++;
3941
0
   }
3942
0
}
3943
3944
static void
3945
png_read_filter_row_avg(png_row_infop row_info, png_bytep row,
3946
    png_const_bytep prev_row)
3947
0
{
3948
0
   png_size_t i;
3949
0
   png_bytep rp = row;
3950
0
   png_const_bytep pp = prev_row;
3951
0
   unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
3952
0
   png_size_t istop = row_info->rowbytes - bpp;
3953
3954
0
   for (i = 0; i < bpp; i++)
3955
0
   {
3956
0
      *rp = (png_byte)(((int)(*rp) +
3957
0
         ((int)(*pp++) / 2 )) & 0xff);
3958
3959
0
      rp++;
3960
0
   }
3961
3962
0
   for (i = 0; i < istop; i++)
3963
0
   {
3964
0
      *rp = (png_byte)(((int)(*rp) +
3965
0
         (int)(*pp++ + *(rp-bpp)) / 2 ) & 0xff);
3966
3967
0
      rp++;
3968
0
   }
3969
0
}
3970
3971
static void
3972
png_read_filter_row_paeth_1byte_pixel(png_row_infop row_info, png_bytep row,
3973
    png_const_bytep prev_row)
3974
0
{
3975
0
   png_bytep rp_end = row + row_info->rowbytes;
3976
0
   int a, c;
3977
3978
   /* First pixel/byte */
3979
0
   c = *prev_row++;
3980
0
   a = *row + c;
3981
0
   *row++ = (png_byte)a;
3982
3983
   /* Remainder */
3984
0
   while (row < rp_end)
3985
0
   {
3986
0
      int b, pa, pb, pc, p;
3987
3988
0
      a &= 0xff; /* From previous iteration or start */
3989
0
      b = *prev_row++;
3990
3991
0
      p = b - c;
3992
0
      pc = a - c;
3993
3994
#ifdef PNG_USE_ABS
3995
      pa = abs(p);
3996
      pb = abs(pc);
3997
      pc = abs(p + pc);
3998
#else
3999
0
      pa = p < 0 ? -p : p;
4000
0
      pb = pc < 0 ? -pc : pc;
4001
0
      pc = (p + pc) < 0 ? -(p + pc) : p + pc;
4002
0
#endif
4003
4004
      /* Find the best predictor, the least of pa, pb, pc favoring the earlier
4005
       * ones in the case of a tie.
4006
       */
4007
0
      if (pb < pa)
4008
0
      {
4009
0
         pa = pb; a = b;
4010
0
      }
4011
0
      if (pc < pa) a = c;
4012
4013
      /* Calculate the current pixel in a, and move the previous row pixel to c
4014
       * for the next time round the loop
4015
       */
4016
0
      c = b;
4017
0
      a += *row;
4018
0
      *row++ = (png_byte)a;
4019
0
   }
4020
0
}
4021
4022
static void
4023
png_read_filter_row_paeth_multibyte_pixel(png_row_infop row_info, png_bytep row,
4024
    png_const_bytep prev_row)
4025
0
{
4026
0
   unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
4027
0
   png_bytep rp_end = row + bpp;
4028
4029
   /* Process the first pixel in the row completely (this is the same as 'up'
4030
    * because there is only one candidate predictor for the first row).
4031
    */
4032
0
   while (row < rp_end)
4033
0
   {
4034
0
      int a = *row + *prev_row++;
4035
0
      *row++ = (png_byte)a;
4036
0
   }
4037
4038
   /* Remainder */
4039
0
   rp_end = rp_end + (row_info->rowbytes - bpp);
4040
4041
0
   while (row < rp_end)
4042
0
   {
4043
0
      int a, b, c, pa, pb, pc, p;
4044
4045
0
      c = *(prev_row - bpp);
4046
0
      a = *(row - bpp);
4047
0
      b = *prev_row++;
4048
4049
0
      p = b - c;
4050
0
      pc = a - c;
4051
4052
#ifdef PNG_USE_ABS
4053
      pa = abs(p);
4054
      pb = abs(pc);
4055
      pc = abs(p + pc);
4056
#else
4057
0
      pa = p < 0 ? -p : p;
4058
0
      pb = pc < 0 ? -pc : pc;
4059
0
      pc = (p + pc) < 0 ? -(p + pc) : p + pc;
4060
0
#endif
4061
4062
0
      if (pb < pa)
4063
0
      {
4064
0
         pa = pb; a = b;
4065
0
      }
4066
0
      if (pc < pa) a = c;
4067
4068
0
      a += *row;
4069
0
      *row++ = (png_byte)a;
4070
0
   }
4071
0
}
4072
4073
static void
4074
png_init_filter_functions(png_structrp pp)
4075
   /* This function is called once for every PNG image (except for PNG images
4076
    * that only use PNG_FILTER_VALUE_NONE for all rows) to set the
4077
    * implementations required to reverse the filtering of PNG rows.  Reversing
4078
    * the filter is the first transformation performed on the row data.  It is
4079
    * performed in place, therefore an implementation can be selected based on
4080
    * the image pixel format.  If the implementation depends on image width then
4081
    * take care to ensure that it works correctly if the image is interlaced -
4082
    * interlacing causes the actual row width to vary.
4083
    */
4084
0
{
4085
0
   unsigned int bpp = (pp->pixel_depth + 7) >> 3;
4086
4087
0
   pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub;
4088
0
   pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up;
4089
0
   pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg;
4090
0
   if (bpp == 1)
4091
0
      pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
4092
0
         png_read_filter_row_paeth_1byte_pixel;
4093
0
   else
4094
0
      pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
4095
0
         png_read_filter_row_paeth_multibyte_pixel;
4096
4097
#ifdef PNG_FILTER_OPTIMIZATIONS
4098
   /* To use this define PNG_FILTER_OPTIMIZATIONS as the name of a function to
4099
    * call to install hardware optimizations for the above functions; simply
4100
    * replace whatever elements of the pp->read_filter[] array with a hardware
4101
    * specific (or, for that matter, generic) optimization.
4102
    *
4103
    * To see an example of this examine what configure.ac does when
4104
    * --enable-arm-neon is specified on the command line.
4105
    */
4106
   PNG_FILTER_OPTIMIZATIONS(pp, bpp);
4107
#endif
4108
0
}
4109
4110
void /* PRIVATE */
4111
png_read_filter_row(png_structrp pp, png_row_infop row_info, png_bytep row,
4112
    png_const_bytep prev_row, int filter)
4113
0
{
4114
   /* OPTIMIZATION: DO NOT MODIFY THIS FUNCTION, instead #define
4115
    * PNG_FILTER_OPTIMIZATIONS to a function that overrides the generic
4116
    * implementations.  See png_init_filter_functions above.
4117
    */
4118
0
   if (filter > PNG_FILTER_VALUE_NONE && filter < PNG_FILTER_VALUE_LAST)
4119
0
   {
4120
0
      if (pp->read_filter[0] == NULL)
4121
0
         png_init_filter_functions(pp);
4122
4123
0
      pp->read_filter[filter-1](row_info, row, prev_row);
4124
0
   }
4125
0
}
4126
4127
#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
4128
void /* PRIVATE */
4129
png_read_IDAT_data(png_structrp png_ptr, png_bytep output,
4130
    png_alloc_size_t avail_out)
4131
0
{
4132
   /* Loop reading IDATs and decompressing the result into output[avail_out] */
4133
0
   png_ptr->zstream.next_out = output;
4134
0
   png_ptr->zstream.avail_out = 0; /* safety: set below */
4135
4136
0
   if (output == NULL)
4137
0
      avail_out = 0;
4138
4139
0
   do
4140
0
   {
4141
0
      int ret;
4142
0
      png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];
4143
4144
0
      if (png_ptr->zstream.avail_in == 0)
4145
0
      {
4146
0
         uInt avail_in;
4147
0
         png_bytep buffer;
4148
4149
0
         while (png_ptr->idat_size == 0)
4150
0
         {
4151
0
            png_crc_finish(png_ptr, 0);
4152
4153
0
            png_ptr->idat_size = png_read_chunk_header(png_ptr);
4154
            /* This is an error even in the 'check' case because the code just
4155
             * consumed a non-IDAT header.
4156
             */
4157
0
            if (png_ptr->chunk_name != png_IDAT)
4158
0
               png_error(png_ptr, "Not enough image data");
4159
0
         }
4160
4161
0
         avail_in = png_ptr->IDAT_read_size;
4162
4163
0
         if (avail_in > png_ptr->idat_size)
4164
0
            avail_in = (uInt)png_ptr->idat_size;
4165
4166
         /* A PNG with a gradually increasing IDAT size will defeat this attempt
4167
          * to minimize memory usage by causing lots of re-allocs, but
4168
          * realistically doing IDAT_read_size re-allocs is not likely to be a
4169
          * big problem.
4170
          */
4171
0
         buffer = png_read_buffer(png_ptr, avail_in, 0/*error*/);
4172
4173
0
         png_crc_read(png_ptr, buffer, avail_in);
4174
0
         png_ptr->idat_size -= avail_in;
4175
4176
0
         png_ptr->zstream.next_in = buffer;
4177
0
         png_ptr->zstream.avail_in = avail_in;
4178
0
      }
4179
4180
      /* And set up the output side. */
4181
0
      if (output != NULL) /* standard read */
4182
0
      {
4183
0
         uInt out = ZLIB_IO_MAX;
4184
4185
0
         if (out > avail_out)
4186
0
            out = (uInt)avail_out;
4187
4188
0
         avail_out -= out;
4189
0
         png_ptr->zstream.avail_out = out;
4190
0
      }
4191
4192
0
      else /* after last row, checking for end */
4193
0
      {
4194
0
         png_ptr->zstream.next_out = tmpbuf;
4195
0
         png_ptr->zstream.avail_out = (sizeof tmpbuf);
4196
0
      }
4197
4198
      /* Use NO_FLUSH; this gives zlib the maximum opportunity to optimize the
4199
       * process.  If the LZ stream is truncated the sequential reader will
4200
       * terminally damage the stream, above, by reading the chunk header of the
4201
       * following chunk (it then exits with png_error).
4202
       *
4203
       * TODO: deal more elegantly with truncated IDAT lists.
4204
       */
4205
0
      ret = PNG_INFLATE(png_ptr, Z_NO_FLUSH);
4206
4207
      /* Take the unconsumed output back. */
4208
0
      if (output != NULL)
4209
0
         avail_out += png_ptr->zstream.avail_out;
4210
4211
0
      else /* avail_out counts the extra bytes */
4212
0
         avail_out += (sizeof tmpbuf) - png_ptr->zstream.avail_out;
4213
4214
0
      png_ptr->zstream.avail_out = 0;
4215
4216
0
      if (ret == Z_STREAM_END)
4217
0
      {
4218
         /* Do this for safety; we won't read any more into this row. */
4219
0
         png_ptr->zstream.next_out = NULL;
4220
4221
0
         png_ptr->mode |= PNG_AFTER_IDAT;
4222
0
         png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
4223
4224
0
         if (png_ptr->zstream.avail_in > 0 || png_ptr->idat_size > 0)
4225
0
            png_chunk_benign_error(png_ptr, "Extra compressed data");
4226
0
         break;
4227
0
      }
4228
4229
0
      if (ret != Z_OK)
4230
0
      {
4231
0
         png_zstream_error(png_ptr, ret);
4232
4233
0
         if (output != NULL)
4234
0
            png_chunk_error(png_ptr, png_ptr->zstream.msg);
4235
4236
0
         else /* checking */
4237
0
         {
4238
0
            png_chunk_benign_error(png_ptr, png_ptr->zstream.msg);
4239
0
            return;
4240
0
         }
4241
0
      }
4242
0
   } while (avail_out > 0);
4243
4244
0
   if (avail_out > 0)
4245
0
   {
4246
      /* The stream ended before the image; this is the same as too few IDATs so
4247
       * should be handled the same way.
4248
       */
4249
0
      if (output != NULL)
4250
0
         png_error(png_ptr, "Not enough image data");
4251
4252
0
      else /* the deflate stream contained extra data */
4253
0
         png_chunk_benign_error(png_ptr, "Too much image data");
4254
0
   }
4255
0
}
4256
4257
void /* PRIVATE */
4258
png_read_finish_IDAT(png_structrp png_ptr)
4259
0
{
4260
   /* We don't need any more data and the stream should have ended, however the
4261
    * LZ end code may actually not have been processed.  In this case we must
4262
    * read it otherwise stray unread IDAT data or, more likely, an IDAT chunk
4263
    * may still remain to be consumed.
4264
    */
4265
0
   if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
4266
0
   {
4267
      /* The NULL causes png_read_IDAT_data to swallow any remaining bytes in
4268
       * the compressed stream, but the stream may be damaged too, so even after
4269
       * this call we may need to terminate the zstream ownership.
4270
       */
4271
0
      png_read_IDAT_data(png_ptr, NULL, 0);
4272
0
      png_ptr->zstream.next_out = NULL; /* safety */
4273
4274
      /* Now clear everything out for safety; the following may not have been
4275
       * done.
4276
       */
4277
0
      if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
4278
0
      {
4279
0
         png_ptr->mode |= PNG_AFTER_IDAT;
4280
0
         png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
4281
0
      }
4282
0
   }
4283
4284
   /* If the zstream has not been released do it now *and* terminate the reading
4285
    * of the final IDAT chunk.
4286
    */
4287
0
   if (png_ptr->zowner == png_IDAT)
4288
0
   {
4289
      /* Always do this; the pointers otherwise point into the read buffer. */
4290
0
      png_ptr->zstream.next_in = NULL;
4291
0
      png_ptr->zstream.avail_in = 0;
4292
4293
      /* Now we no longer own the zstream. */
4294
0
      png_ptr->zowner = 0;
4295
4296
      /* The slightly weird semantics of the sequential IDAT reading is that we
4297
       * are always in or at the end of an IDAT chunk, so we always need to do a
4298
       * crc_finish here.  If idat_size is non-zero we also need to read the
4299
       * spurious bytes at the end of the chunk now.
4300
       */
4301
0
      (void)png_crc_finish(png_ptr, png_ptr->idat_size);
4302
0
   }
4303
0
}
4304
4305
void /* PRIVATE */
4306
png_read_finish_row(png_structrp png_ptr)
4307
0
{
4308
   /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
4309
4310
   /* Start of interlace block */
4311
0
   static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
4312
4313
   /* Offset to next interlace block */
4314
0
   static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
4315
4316
   /* Start of interlace block in the y direction */
4317
0
   static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
4318
4319
   /* Offset to next interlace block in the y direction */
4320
0
   static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
4321
4322
0
   png_debug(1, "in png_read_finish_row");
4323
0
   png_ptr->row_number++;
4324
0
   if (png_ptr->row_number < png_ptr->num_rows)
4325
0
      return;
4326
4327
0
   if (png_ptr->interlaced != 0)
4328
0
   {
4329
0
      png_ptr->row_number = 0;
4330
4331
      /* TO DO: don't do this if prev_row isn't needed (requires
4332
       * read-ahead of the next row's filter byte.
4333
       */
4334
0
      memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
4335
4336
0
      do
4337
0
      {
4338
0
         png_ptr->pass++;
4339
4340
0
         if (png_ptr->pass >= 7)
4341
0
            break;
4342
4343
0
         png_ptr->iwidth = (png_ptr->width +
4344
0
            png_pass_inc[png_ptr->pass] - 1 -
4345
0
            png_pass_start[png_ptr->pass]) /
4346
0
            png_pass_inc[png_ptr->pass];
4347
4348
0
         if ((png_ptr->transformations & PNG_INTERLACE) == 0)
4349
0
         {
4350
0
            png_ptr->num_rows = (png_ptr->height +
4351
0
                png_pass_yinc[png_ptr->pass] - 1 -
4352
0
                png_pass_ystart[png_ptr->pass]) /
4353
0
                png_pass_yinc[png_ptr->pass];
4354
0
         }
4355
4356
0
         else  /* if (png_ptr->transformations & PNG_INTERLACE) */
4357
0
            break; /* libpng deinterlacing sees every row */
4358
4359
0
      } while (png_ptr->num_rows == 0 || png_ptr->iwidth == 0);
4360
4361
0
      if (png_ptr->pass < 7)
4362
0
         return;
4363
0
   }
4364
4365
   /* Here after at the end of the last row of the last pass. */
4366
0
   png_read_finish_IDAT(png_ptr);
4367
0
}
4368
#endif /* SEQUENTIAL_READ */
4369
4370
void /* PRIVATE */
4371
png_read_start_row(png_structrp png_ptr)
4372
0
{
4373
   /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
4374
4375
   /* Start of interlace block */
4376
0
   static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
4377
4378
   /* Offset to next interlace block */
4379
0
   static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
4380
4381
   /* Start of interlace block in the y direction */
4382
0
   static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
4383
4384
   /* Offset to next interlace block in the y direction */
4385
0
   static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
4386
4387
0
   unsigned int max_pixel_depth;
4388
0
   png_size_t row_bytes;
4389
4390
0
   png_debug(1, "in png_read_start_row");
4391
4392
0
#ifdef PNG_READ_TRANSFORMS_SUPPORTED
4393
0
   png_init_read_transformations(png_ptr);
4394
0
#endif
4395
0
   if (png_ptr->interlaced != 0)
4396
0
   {
4397
0
      if ((png_ptr->transformations & PNG_INTERLACE) == 0)
4398
0
         png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
4399
0
             png_pass_ystart[0]) / png_pass_yinc[0];
4400
4401
0
      else
4402
0
         png_ptr->num_rows = png_ptr->height;
4403
4404
0
      png_ptr->iwidth = (png_ptr->width +
4405
0
          png_pass_inc[png_ptr->pass] - 1 -
4406
0
          png_pass_start[png_ptr->pass]) /
4407
0
          png_pass_inc[png_ptr->pass];
4408
0
   }
4409
4410
0
   else
4411
0
   {
4412
0
      png_ptr->num_rows = png_ptr->height;
4413
0
      png_ptr->iwidth = png_ptr->width;
4414
0
   }
4415
4416
0
   max_pixel_depth = (unsigned int)png_ptr->pixel_depth;
4417
4418
   /* WARNING: * png_read_transform_info (pngrtran.c) performs a simpler set of
4419
    * calculations to calculate the final pixel depth, then
4420
    * png_do_read_transforms actually does the transforms.  This means that the
4421
    * code which effectively calculates this value is actually repeated in three
4422
    * separate places.  They must all match.  Innocent changes to the order of
4423
    * transformations can and will break libpng in a way that causes memory
4424
    * overwrites.
4425
    *
4426
    * TODO: fix this.
4427
    */
4428
0
#ifdef PNG_READ_PACK_SUPPORTED
4429
0
   if ((png_ptr->transformations & PNG_PACK) != 0 && png_ptr->bit_depth < 8)
4430
0
      max_pixel_depth = 8;
4431
0
#endif
4432
4433
0
#ifdef PNG_READ_EXPAND_SUPPORTED
4434
0
   if ((png_ptr->transformations & PNG_EXPAND) != 0)
4435
0
   {
4436
0
      if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
4437
0
      {
4438
0
         if (png_ptr->num_trans != 0)
4439
0
            max_pixel_depth = 32;
4440
4441
0
         else
4442
0
            max_pixel_depth = 24;
4443
0
      }
4444
4445
0
      else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
4446
0
      {
4447
0
         if (max_pixel_depth < 8)
4448
0
            max_pixel_depth = 8;
4449
4450
0
         if (png_ptr->num_trans != 0)
4451
0
            max_pixel_depth *= 2;
4452
0
      }
4453
4454
0
      else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
4455
0
      {
4456
0
         if (png_ptr->num_trans != 0)
4457
0
         {
4458
0
            max_pixel_depth *= 4;
4459
0
            max_pixel_depth /= 3;
4460
0
         }
4461
0
      }
4462
0
   }
4463
0
#endif
4464
4465
0
#ifdef PNG_READ_EXPAND_16_SUPPORTED
4466
0
   if ((png_ptr->transformations & PNG_EXPAND_16) != 0)
4467
0
   {
4468
0
#  ifdef PNG_READ_EXPAND_SUPPORTED
4469
      /* In fact it is an error if it isn't supported, but checking is
4470
       * the safe way.
4471
       */
4472
0
      if ((png_ptr->transformations & PNG_EXPAND) != 0)
4473
0
      {
4474
0
         if (png_ptr->bit_depth < 16)
4475
0
            max_pixel_depth *= 2;
4476
0
      }
4477
0
      else
4478
0
#  endif
4479
0
      png_ptr->transformations &= ~PNG_EXPAND_16;
4480
0
   }
4481
0
#endif
4482
4483
0
#ifdef PNG_READ_FILLER_SUPPORTED
4484
0
   if ((png_ptr->transformations & (PNG_FILLER)) != 0)
4485
0
   {
4486
0
      if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
4487
0
      {
4488
0
         if (max_pixel_depth <= 8)
4489
0
            max_pixel_depth = 16;
4490
4491
0
         else
4492
0
            max_pixel_depth = 32;
4493
0
      }
4494
4495
0
      else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB ||
4496
0
         png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
4497
0
      {
4498
0
         if (max_pixel_depth <= 32)
4499
0
            max_pixel_depth = 32;
4500
4501
0
         else
4502
0
            max_pixel_depth = 64;
4503
0
      }
4504
0
   }
4505
0
#endif
4506
4507
0
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
4508
0
   if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0)
4509
0
   {
4510
0
      if (
4511
0
#ifdef PNG_READ_EXPAND_SUPPORTED
4512
0
          (png_ptr->num_trans != 0 &&
4513
0
          (png_ptr->transformations & PNG_EXPAND) != 0) ||
4514
0
#endif
4515
0
#ifdef PNG_READ_FILLER_SUPPORTED
4516
0
          (png_ptr->transformations & (PNG_FILLER)) != 0 ||
4517
0
#endif
4518
0
          png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
4519
0
      {
4520
0
         if (max_pixel_depth <= 16)
4521
0
            max_pixel_depth = 32;
4522
4523
0
         else
4524
0
            max_pixel_depth = 64;
4525
0
      }
4526
4527
0
      else
4528
0
      {
4529
0
         if (max_pixel_depth <= 8)
4530
0
         {
4531
0
            if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4532
0
               max_pixel_depth = 32;
4533
4534
0
            else
4535
0
               max_pixel_depth = 24;
4536
0
         }
4537
4538
0
         else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4539
0
            max_pixel_depth = 64;
4540
4541
0
         else
4542
0
            max_pixel_depth = 48;
4543
0
      }
4544
0
   }
4545
0
#endif
4546
4547
0
#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
4548
0
defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
4549
0
   if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0)
4550
0
   {
4551
0
      unsigned int user_pixel_depth = png_ptr->user_transform_depth *
4552
0
         png_ptr->user_transform_channels;
4553
4554
0
      if (user_pixel_depth > max_pixel_depth)
4555
0
         max_pixel_depth = user_pixel_depth;
4556
0
   }
4557
0
#endif
4558
4559
   /* This value is stored in png_struct and double checked in the row read
4560
    * code.
4561
    */
4562
0
   png_ptr->maximum_pixel_depth = (png_byte)max_pixel_depth;
4563
0
   png_ptr->transformed_pixel_depth = 0; /* calculated on demand */
4564
4565
   /* Align the width on the next larger 8 pixels.  Mainly used
4566
    * for interlacing
4567
    */
4568
0
   row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
4569
   /* Calculate the maximum bytes needed, adding a byte and a pixel
4570
    * for safety's sake
4571
    */
4572
0
   row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) +
4573
0
       1 + ((max_pixel_depth + 7) >> 3U);
4574
4575
#ifdef PNG_MAX_MALLOC_64K
4576
   if (row_bytes > (png_uint_32)65536L)
4577
      png_error(png_ptr, "This image requires a row greater than 64KB");
4578
#endif
4579
4580
0
   if (row_bytes + 48 > png_ptr->old_big_row_buf_size)
4581
0
   {
4582
0
      png_free(png_ptr, png_ptr->big_row_buf);
4583
0
      png_free(png_ptr, png_ptr->big_prev_row);
4584
4585
0
      if (png_ptr->interlaced != 0)
4586
0
         png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr,
4587
0
             row_bytes + 48);
4588
4589
0
      else
4590
0
         png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
4591
4592
0
      png_ptr->big_prev_row = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
4593
4594
0
#ifdef PNG_ALIGNED_MEMORY_SUPPORTED
4595
      /* Use 16-byte aligned memory for row_buf with at least 16 bytes
4596
       * of padding before and after row_buf; treat prev_row similarly.
4597
       * NOTE: the alignment is to the start of the pixels, one beyond the start
4598
       * of the buffer, because of the filter byte.  Prior to libpng 1.5.6 this
4599
       * was incorrect; the filter byte was aligned, which had the exact
4600
       * opposite effect of that intended.
4601
       */
4602
0
      {
4603
0
         png_bytep temp = png_ptr->big_row_buf + 32;
4604
0
         int extra = (int)((temp - (png_bytep)0) & 0x0f);
4605
0
         png_ptr->row_buf = temp - extra - 1/*filter byte*/;
4606
4607
0
         temp = png_ptr->big_prev_row + 32;
4608
0
         extra = (int)((temp - (png_bytep)0) & 0x0f);
4609
0
         png_ptr->prev_row = temp - extra - 1/*filter byte*/;
4610
0
      }
4611
4612
#else
4613
      /* Use 31 bytes of padding before and 17 bytes after row_buf. */
4614
      png_ptr->row_buf = png_ptr->big_row_buf + 31;
4615
      png_ptr->prev_row = png_ptr->big_prev_row + 31;
4616
#endif
4617
0
      png_ptr->old_big_row_buf_size = row_bytes + 48;
4618
0
   }
4619
4620
#ifdef PNG_MAX_MALLOC_64K
4621
   if (png_ptr->rowbytes > 65535)
4622
      png_error(png_ptr, "This image requires a row greater than 64KB");
4623
4624
#endif
4625
0
   if (png_ptr->rowbytes > (PNG_SIZE_MAX - 1))
4626
0
      png_error(png_ptr, "Row has too many bytes to allocate in memory");
4627
4628
0
   memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
4629
4630
0
   png_debug1(3, "width = %u,", png_ptr->width);
4631
0
   png_debug1(3, "height = %u,", png_ptr->height);
4632
0
   png_debug1(3, "iwidth = %u,", png_ptr->iwidth);
4633
0
   png_debug1(3, "num_rows = %u,", png_ptr->num_rows);
4634
0
   png_debug1(3, "rowbytes = %lu,", (unsigned long)png_ptr->rowbytes);
4635
0
   png_debug1(3, "irowbytes = %lu",
4636
0
       (unsigned long)PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1);
4637
4638
   /* The sequential reader needs a buffer for IDAT, but the progressive reader
4639
    * does not, so free the read buffer now regardless; the sequential reader
4640
    * reallocates it on demand.
4641
    */
4642
0
   if (png_ptr->read_buffer != NULL)
4643
0
   {
4644
0
      png_bytep buffer = png_ptr->read_buffer;
4645
4646
0
      png_ptr->read_buffer_size = 0;
4647
0
      png_ptr->read_buffer = NULL;
4648
0
      png_free(png_ptr, buffer);
4649
0
   }
4650
4651
   /* Finally claim the zstream for the inflate of the IDAT data, use the bits
4652
    * value from the stream (note that this will result in a fatal error if the
4653
    * IDAT stream has a bogus deflate header window_bits value, but this should
4654
    * not be happening any longer!)
4655
    */
4656
0
   if (png_inflate_claim(png_ptr, png_IDAT) != Z_OK)
4657
0
      png_error(png_ptr, png_ptr->zstream.msg);
4658
4659
0
   png_ptr->flags |= PNG_FLAG_ROW_INIT;
4660
0
}
4661
#endif /* READ */