pngrutil.c 92.6 KB
Newer Older
1

A
Andreas Dilger 已提交
2
/* pngrutil.c - utilities to read a PNG file
3
 *
4
 * Last changed in libpng 1.4.0 [August 4, 2008]
5
 * For conditions of distribution and use, see copyright notice in png.h
6
 * Copyright (c) 1998-2008 Glenn Randers-Pehrson
7 8
 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
9
 *
10
 * This file contains routines that are only called from within
11 12
 * libpng itself during the course of reading an image.
 */
G
Guy Schalnat 已提交
13 14

#include "png.h"
15
#if defined(PNG_READ_SUPPORTED)
16
#include "pngpriv.h"
17

18
#    define png_strtod(p,a,b) strtod(a,b)
19
png_uint_32 PNGAPI
20 21 22 23
png_get_uint_31(png_structp png_ptr, png_bytep buf)
{
   png_uint_32 i = png_get_uint_32(buf);
   if (i > PNG_UINT_31_MAX)
24
     png_error(png_ptr, "PNG unsigned integer out of range");
25 26
   return (i);
}
27
#ifndef PNG_USE_READ_MACROS
28
/* Grab an unsigned 32-bit integer from a buffer in big-endian format. */
29
png_uint_32 PNGAPI
G
Guy Schalnat 已提交
30
png_get_uint_32(png_bytep buf)
G
Guy Schalnat 已提交
31
{
32
   png_uint_32 i = ((png_uint_32)(*buf) << 24) +
G
Guy Schalnat 已提交
33 34 35 36
      ((png_uint_32)(*(buf + 1)) << 16) +
      ((png_uint_32)(*(buf + 2)) << 8) +
      (png_uint_32)(*(buf + 3));

37
   return (i);
G
Guy Schalnat 已提交
38 39
}

40
/* Grab a signed 32-bit integer from a buffer in big-endian format.  The
A
Andreas Dilger 已提交
41 42
 * data is stored in the PNG file in two's complement format, and it is
 * assumed that the machine format for signed integers is the same. */
43
png_int_32 PNGAPI
A
Andreas Dilger 已提交
44 45
png_get_int_32(png_bytep buf)
{
46
   png_int_32 i = ((png_int_32)(*buf) << 24) +
A
Andreas Dilger 已提交
47 48 49 50
      ((png_int_32)(*(buf + 1)) << 16) +
      ((png_int_32)(*(buf + 2)) << 8) +
      (png_int_32)(*(buf + 3));

51
   return (i);
A
Andreas Dilger 已提交
52 53
}

54
/* Grab an unsigned 16-bit integer from a buffer in big-endian format. */
55
png_uint_16 PNGAPI
G
Guy Schalnat 已提交
56
png_get_uint_16(png_bytep buf)
G
Guy Schalnat 已提交
57
{
58
   png_uint_16 i = (png_uint_16)(((png_uint_16)(*buf) << 8) +
G
Guy Schalnat 已提交
59
      (png_uint_16)(*(buf + 1)));
G
Guy Schalnat 已提交
60

61
   return (i);
G
Guy Schalnat 已提交
62
}
63
#endif /* PNG_USE_READ_MACROS */
G
Guy Schalnat 已提交
64

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
/* Read the chunk header (length + type name).
 * Put the type name into png_ptr->chunk_name, and return the length.
 */
png_uint_32 /* PRIVATE */
png_read_chunk_header(png_structp png_ptr)
{
   png_byte buf[8];
   png_uint_32 length;

#ifdef PNG_IO_STATE_SUPPORTED
   /* Inform the I/O callback that the chunk header is being read.
    * PNG_IO_CHUNK_HDR requires a single I/O call.
    */
   png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_HDR;
#endif

   /* read the length and the chunk name */
   png_read_data(png_ptr, buf, 8);
   length = png_get_uint_31(png_ptr, buf);

   /* put the chunk name into png_ptr->chunk_name */
   png_memcpy(png_ptr->chunk_name, buf + 4, 4);

   png_debug2(0, "Reading %s chunk, length = %lu\n",
      png_ptr->chunk_name, length);

   /* reset the crc and run it over the chunk name */
   png_reset_crc(png_ptr);
   png_calculate_crc(png_ptr, png_ptr->chunk_name, 4);

95 96 97
   /* check to see if chunk name is valid */
   png_check_chunk_name(png_ptr, png_ptr->chunk_name);

98 99 100 101 102 103 104 105 106 107
#ifdef PNG_IO_STATE_SUPPORTED
   /* Inform the I/O callback that chunk data will (possibly) be read.
    * PNG_IO_CHUNK_DATA does NOT require a specific number of I/O calls.
    */
   png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_DATA;
#endif

   return length;
}

108
/* Read data, and (optionally) run it through the CRC. */
109
void /* PRIVATE */
A
Andreas Dilger 已提交
110
png_crc_read(png_structp png_ptr, png_bytep buf, png_size_t length)
G
Guy Schalnat 已提交
111
{
112
   if (png_ptr == NULL) return;
G
Guy Schalnat 已提交
113
   png_read_data(png_ptr, buf, length);
A
Andreas Dilger 已提交
114
   png_calculate_crc(png_ptr, buf, length);
G
Guy Schalnat 已提交
115 116
}

A
Andreas Dilger 已提交
117 118
/* Optionally skip data and then check the CRC.  Depending on whether we
   are reading a ancillary or critical chunk, and how the program has set
A
Andreas Dilger 已提交
119 120
   things up, we may calculate the CRC on the data and print a message.
   Returns '1' if there was a CRC error, '0' otherwise. */
121
int /* PRIVATE */
A
Andreas Dilger 已提交
122
png_crc_finish(png_structp png_ptr, png_uint_32 skip)
G
Guy Schalnat 已提交
123
{
124 125
   png_size_t i;
   png_size_t istop = png_ptr->zbuf_size;
G
Guy Schalnat 已提交
126

127
   for (i = (png_size_t)skip; i > istop; i -= istop)
A
Andreas Dilger 已提交
128
   {
A
Andreas Dilger 已提交
129
      png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
G
Guy Schalnat 已提交
130
   }
131
   if (i)
G
Guy Schalnat 已提交
132
   {
133
      png_crc_read(png_ptr, png_ptr->zbuf, i);
A
Andreas Dilger 已提交
134 135
   }

A
Andreas Dilger 已提交
136
   if (png_crc_error(png_ptr))
A
Andreas Dilger 已提交
137
   {
138
      if (((png_ptr->chunk_name[0] & 0x20) &&                /* Ancillary */
A
Andreas Dilger 已提交
139
           !(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)) ||
140
          (!(png_ptr->chunk_name[0] & 0x20) &&             /* Critical  */
141
          (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE)))
A
Andreas Dilger 已提交
142
      {
143
         png_chunk_warning(png_ptr, "CRC error");
A
Andreas Dilger 已提交
144 145 146
      }
      else
      {
147 148
         png_chunk_benign_error(png_ptr, "CRC error");
         return (0);
A
Andreas Dilger 已提交
149
      }
150
      return (1);
G
Guy Schalnat 已提交
151
   }
A
Andreas Dilger 已提交
152

153
   return (0);
G
Guy Schalnat 已提交
154 155
}

156
/* Compare the CRC stored in the PNG file with that calculated by libpng from
A
Andreas Dilger 已提交
157
   the data it has read thus far. */
158
int /* PRIVATE */
A
Andreas Dilger 已提交
159 160 161 162
png_crc_error(png_structp png_ptr)
{
   png_byte crc_bytes[4];
   png_uint_32 crc;
A
Andreas Dilger 已提交
163
   int need_crc = 1;
A
Andreas Dilger 已提交
164

A
Andreas Dilger 已提交
165 166 167 168 169 170 171 172 173 174 175
   if (png_ptr->chunk_name[0] & 0x20)                     /* ancillary */
   {
      if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
          (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
         need_crc = 0;
   }
   else                                                    /* critical */
   {
      if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
         need_crc = 0;
   }
A
Andreas Dilger 已提交
176

177 178 179 180 181
#ifdef PNG_IO_STATE_SUPPORTED
   /* inform the I/O callback that the chunk CRC is being read */
   /* PNG_IO_CHUNK_CRC requires the I/O to be done at once */
   png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_CRC;
#endif
182

A
Andreas Dilger 已提交
183
   png_read_data(png_ptr, crc_bytes, 4);
A
Andreas Dilger 已提交
184

A
Andreas Dilger 已提交
185 186 187
   if (need_crc)
   {
      crc = png_get_uint_32(crc_bytes);
188
      return ((int)(crc != png_ptr->crc));
A
Andreas Dilger 已提交
189 190
   }
   else
191
      return (0);
A
Andreas Dilger 已提交
192 193
}

194
#if defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) || \
195
    defined(PNG_READ_iCCP_SUPPORTED)
196 197 198 199 200 201 202
/*
 * Decompress trailing data in a chunk.  The assumption is that chunkdata
 * points at an allocated area holding the contents of a chunk with a
 * trailing compressed part.  What we get back is an allocated area
 * holding the original prefix part and an uncompressed version of the
 * trailing part (the malloc area passed in is freed).
 */
203
void /* PRIVATE */
204
png_decompress_chunk(png_structp png_ptr, int comp_type,
205
                              png_size_t chunklength,
206
                              png_size_t prefix_size, png_size_t *newlength)
207
{
208
   static PNG_CONST char msg[] = "Error decoding compressed text";
209
   png_charp text;
210
   png_size_t text_size;
211

212
   if (comp_type == PNG_COMPRESSION_TYPE_BASE)
213
   {
214
      int ret = Z_OK;
215
      png_ptr->zstream.next_in = (png_bytep)(png_ptr->chunkdata + prefix_size);
216 217 218 219 220 221 222 223 224
      png_ptr->zstream.avail_in = (uInt)(chunklength - prefix_size);
      png_ptr->zstream.next_out = png_ptr->zbuf;
      png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;

      text_size = 0;
      text = NULL;

      while (png_ptr->zstream.avail_in)
      {
225
         ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
226 227 228 229 230 231 232 233 234 235 236
         if (ret != Z_OK && ret != Z_STREAM_END)
         {
            if (png_ptr->zstream.msg != NULL)
               png_warning(png_ptr, png_ptr->zstream.msg);
            else
               png_warning(png_ptr, msg);
            inflateReset(&png_ptr->zstream);
            png_ptr->zstream.avail_in = 0;

            if (text ==  NULL)
            {
237
               text_size = prefix_size + png_sizeof(msg) + 1;
238 239 240
               text = (png_charp)png_malloc_warn(png_ptr, text_size);
               if (text ==  NULL)
                 {
241
                    png_free(png_ptr, png_ptr->chunkdata);
242
                    png_ptr->chunkdata = NULL;
243
                    png_error(png_ptr, "Not enough memory to decompress chunk");
244
                 }
245
               png_memcpy(text, png_ptr->chunkdata, prefix_size);
246 247
            }

248
            text[text_size - 1] = 0x00;
249

250
            /* Copy what we can of the error message into the text chunk */
251 252
            text_size = (png_size_t)(chunklength -
              (text - png_ptr->chunkdata) - 1);
253 254 255
            if (text_size > png_sizeof(msg))
               text_size = png_sizeof(msg);
            png_memcpy(text + prefix_size, msg, text_size);
256 257 258 259 260 261 262 263
            break;
         }
         if (!png_ptr->zstream.avail_out || ret == Z_STREAM_END)
         {
            if (text == NULL)
            {
               text_size = prefix_size +
                   png_ptr->zbuf_size - png_ptr->zstream.avail_out;
264 265
               text = (png_charp)png_malloc_warn(png_ptr, text_size + 1);
               if (text ==  NULL)
266
               {
267
                  png_free(png_ptr, png_ptr->chunkdata);
268
                  png_ptr->chunkdata = NULL;
269 270
                  png_error(png_ptr,
                    "Not enough memory to decompress chunk");
271
               }
272
               png_memcpy(text + prefix_size, png_ptr->zbuf,
273
                    text_size - prefix_size);
274
               png_memcpy(text, png_ptr->chunkdata, prefix_size);
275
               *(text + text_size) = 0x00;
276 277 278 279 280 281
            }
            else
            {
               png_charp tmp;

               tmp = text;
282
               text = (png_charp)png_malloc_warn(png_ptr,
283
                  (png_size_t)(text_size +
284
                  png_ptr->zbuf_size - png_ptr->zstream.avail_out + 1));
285 286 287
               if (text == NULL)
               {
                  png_free(png_ptr, tmp);
288
                  png_free(png_ptr, png_ptr->chunkdata);
289
                  png_ptr->chunkdata = NULL;
290 291
                  png_error(png_ptr,
                    "Not enough memory to decompress chunk");
292
               }
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
               png_memcpy(text, tmp, text_size);
               png_free(png_ptr, tmp);
               png_memcpy(text + text_size, png_ptr->zbuf,
                  (png_ptr->zbuf_size - png_ptr->zstream.avail_out));
               text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out;
               *(text + text_size) = 0x00;
            }
            if (ret == Z_STREAM_END)
               break;
            else
            {
               png_ptr->zstream.next_out = png_ptr->zbuf;
               png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
            }
         }
      }
309
      if (ret != Z_STREAM_END)
310
      {
311
#if !defined(PNG_NO_STDIO)
312
         char umsg[52];
313

314
         if (ret == Z_BUF_ERROR)
315 316
            png_snprintf(umsg, 52,
                "Buffer error in compressed datastream in %s chunk",
317 318
                png_ptr->chunk_name);
         else if (ret == Z_DATA_ERROR)
319 320
            png_snprintf(umsg, 52,
                "Data error in compressed datastream in %s chunk",
321 322
                png_ptr->chunk_name);
         else
323 324
            png_snprintf(umsg, 52,
                "Incomplete compressed datastream in %s chunk",
325
                png_ptr->chunk_name);
326 327
         png_warning(png_ptr, umsg);
#else
328
         png_warning(png_ptr,
329 330
            "Incomplete compressed datastream in chunk other than IDAT");
#endif
331
         text_size = prefix_size;
332 333
         if (text ==  NULL)
         {
334 335 336
            text = (png_charp)png_malloc_warn(png_ptr, text_size+1);
            if (text == NULL)
              {
337
                png_free(png_ptr, png_ptr->chunkdata);
338
                png_ptr->chunkdata = NULL;
339
                png_error(png_ptr, "Not enough memory for text");
340
              }
341
            png_memcpy(text, png_ptr->chunkdata, prefix_size);
342
         }
343
         *(text + text_size) = 0x00;
344
      }
345 346 347 348

      inflateReset(&png_ptr->zstream);
      png_ptr->zstream.avail_in = 0;

349 350
      png_free(png_ptr, png_ptr->chunkdata);
      png_ptr->chunkdata = text;
351
      *newlength=text_size;
352
   }
353
   else /* if (comp_type != PNG_COMPRESSION_TYPE_BASE) */
354
   {
355
#if !defined(PNG_NO_STDIO)
356 357
      char umsg[50];

358
      png_snprintf(umsg, 50, "Unknown zTXt compression type %d", comp_type);
359 360 361 362 363
      png_warning(png_ptr, umsg);
#else
      png_warning(png_ptr, "Unknown zTXt compression type");
#endif

364
      *(png_ptr->chunkdata + prefix_size) = 0x00;
365
      *newlength = prefix_size;
366 367 368
   }
}
#endif
A
Andreas Dilger 已提交
369

G
Guy Schalnat 已提交
370
/* read and check the IDHR chunk */
371
void /* PRIVATE */
A
Andreas Dilger 已提交
372
png_handle_IHDR(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
G
Guy Schalnat 已提交
373 374 375 376 377 378
{
   png_byte buf[13];
   png_uint_32 width, height;
   int bit_depth, color_type, compression_type, filter_type;
   int interlace_type;

A
Andreas Dilger 已提交
379 380
   png_debug(1, "in png_handle_IHDR\n");

381
   if (png_ptr->mode & PNG_HAVE_IHDR)
G
Guy Schalnat 已提交
382 383
      png_error(png_ptr, "Out of place IHDR");

G
Guy Schalnat 已提交
384 385
   /* check the length */
   if (length != 13)
G
Guy Schalnat 已提交
386
      png_error(png_ptr, "Invalid IHDR chunk");
G
Guy Schalnat 已提交
387

A
Andreas Dilger 已提交
388 389
   png_ptr->mode |= PNG_HAVE_IHDR;

G
Guy Schalnat 已提交
390
   png_crc_read(png_ptr, buf, 13);
A
Andreas Dilger 已提交
391
   png_crc_finish(png_ptr, 0);
G
Guy Schalnat 已提交
392

393 394
   width = png_get_uint_31(png_ptr, buf);
   height = png_get_uint_31(png_ptr, buf + 4);
G
Guy Schalnat 已提交
395 396 397 398 399 400 401 402 403
   bit_depth = buf[8];
   color_type = buf[9];
   compression_type = buf[10];
   filter_type = buf[11];
   interlace_type = buf[12];

   /* set internal variables */
   png_ptr->width = width;
   png_ptr->height = height;
G
Guy Schalnat 已提交
404 405 406
   png_ptr->bit_depth = (png_byte)bit_depth;
   png_ptr->interlaced = (png_byte)interlace_type;
   png_ptr->color_type = (png_byte)color_type;
407
#if defined(PNG_MNG_FEATURES_SUPPORTED)
408
   png_ptr->filter_type = (png_byte)filter_type;
409
#endif
410
   png_ptr->compression_type = (png_byte)compression_type;
G
Guy Schalnat 已提交
411 412 413 414

   /* find number of channels */
   switch (png_ptr->color_type)
   {
A
Andreas Dilger 已提交
415 416
      case PNG_COLOR_TYPE_GRAY:
      case PNG_COLOR_TYPE_PALETTE:
G
Guy Schalnat 已提交
417 418
         png_ptr->channels = 1;
         break;
A
Andreas Dilger 已提交
419
      case PNG_COLOR_TYPE_RGB:
G
Guy Schalnat 已提交
420 421
         png_ptr->channels = 3;
         break;
A
Andreas Dilger 已提交
422
      case PNG_COLOR_TYPE_GRAY_ALPHA:
G
Guy Schalnat 已提交
423 424
         png_ptr->channels = 2;
         break;
A
Andreas Dilger 已提交
425
      case PNG_COLOR_TYPE_RGB_ALPHA:
G
Guy Schalnat 已提交
426 427 428
         png_ptr->channels = 4;
         break;
   }
A
Andreas Dilger 已提交
429

G
Guy Schalnat 已提交
430
   /* set up other useful info */
G
Guy Schalnat 已提交
431
   png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth *
432
   png_ptr->channels);
433 434 435 436
   png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width);
   png_debug1(3, "bit_depth = %d\n", png_ptr->bit_depth);
   png_debug1(3, "channels = %d\n", png_ptr->channels);
   png_debug1(3, "rowbytes = %lu\n", png_ptr->rowbytes);
A
Andreas Dilger 已提交
437 438
   png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
      color_type, interlace_type, compression_type, filter_type);
G
Guy Schalnat 已提交
439 440 441
}

/* read and check the palette */
442
void /* PRIVATE */
A
Andreas Dilger 已提交
443
png_handle_PLTE(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
G
Guy Schalnat 已提交
444
{
445
   png_color palette[PNG_MAX_PALETTE_LENGTH];
A
Andreas Dilger 已提交
446
   int num, i;
447 448 449
#ifndef PNG_NO_POINTER_INDEXING
   png_colorp pal_ptr;
#endif
G
Guy Schalnat 已提交
450

A
Andreas Dilger 已提交
451 452
   png_debug(1, "in png_handle_PLTE\n");

G
Guy Schalnat 已提交
453 454
   if (!(png_ptr->mode & PNG_HAVE_IHDR))
      png_error(png_ptr, "Missing IHDR before PLTE");
A
Andreas Dilger 已提交
455 456 457 458 459 460
   else if (png_ptr->mode & PNG_HAVE_IDAT)
   {
      png_warning(png_ptr, "Invalid PLTE after IDAT");
      png_crc_finish(png_ptr, length);
      return;
   }
G
Guy Schalnat 已提交
461
   else if (png_ptr->mode & PNG_HAVE_PLTE)
A
Andreas Dilger 已提交
462 463 464
      png_error(png_ptr, "Duplicate PLTE chunk");

   png_ptr->mode |= PNG_HAVE_PLTE;
G
Guy Schalnat 已提交
465

466 467 468 469 470 471 472
   if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
   {
      png_warning(png_ptr,
        "Ignoring PLTE chunk in grayscale PNG");
      png_crc_finish(png_ptr, length);
      return;
   }
G
Guy Schalnat 已提交
473 474 475
#if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
   if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
   {
A
Andreas Dilger 已提交
476
      png_crc_finish(png_ptr, length);
G
Guy Schalnat 已提交
477 478 479 480
      return;
   }
#endif

481
   if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3)
G
Guy Schalnat 已提交
482 483 484 485
   {
      if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
      {
         png_warning(png_ptr, "Invalid palette chunk");
A
Andreas Dilger 已提交
486
         png_crc_finish(png_ptr, length);
G
Guy Schalnat 已提交
487 488 489 490 491 492 493
         return;
      }
      else
      {
         png_error(png_ptr, "Invalid palette chunk");
      }
   }
G
Guy Schalnat 已提交
494 495

   num = (int)length / 3;
496

497 498 499 500 501 502 503 504 505 506 507
#ifndef PNG_NO_POINTER_INDEXING
   for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)
   {
      png_byte buf[3];

      png_crc_read(png_ptr, buf, 3);
      pal_ptr->red = buf[0];
      pal_ptr->green = buf[1];
      pal_ptr->blue = buf[2];
   }
#else
G
Guy Schalnat 已提交
508
   for (i = 0; i < num; i++)
G
Guy Schalnat 已提交
509 510 511 512 513 514 515 516 517
   {
      png_byte buf[3];

      png_crc_read(png_ptr, buf, 3);
      /* don't depend upon png_color being any order */
      palette[i].red = buf[0];
      palette[i].green = buf[1];
      palette[i].blue = buf[2];
   }
518
#endif
A
Andreas Dilger 已提交
519 520 521 522 523

   /* If we actually NEED the PLTE chunk (ie for a paletted image), we do
      whatever the normal CRC configuration tells us.  However, if we
      have an RGB image, the PLTE can be considered ancillary, so
      we will act as though it is. */
524
#if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
A
Andreas Dilger 已提交
525
   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
526
#endif
A
Andreas Dilger 已提交
527
   {
A
Andreas Dilger 已提交
528
      png_crc_finish(png_ptr, 0);
A
Andreas Dilger 已提交
529
   }
530
#if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
A
Andreas Dilger 已提交
531 532 533 534 535 536 537 538 539 540
   else if (png_crc_error(png_ptr))  /* Only if we have a CRC error */
   {
      /* If we don't want to use the data from an ancillary chunk,
         we have two options: an error abort, or a warning and we
         ignore the data in this chunk (which should be OK, since
         it's considered ancillary for a RGB or RGBA image). */
      if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE))
      {
         if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)
         {
541
            png_chunk_benign_error(png_ptr, "CRC error");
A
Andreas Dilger 已提交
542 543 544
         }
         else
         {
545
            png_chunk_warning(png_ptr, "CRC error");
A
Andreas Dilger 已提交
546 547 548
            return;
         }
      }
A
Andreas Dilger 已提交
549
      /* Otherwise, we (optionally) emit a warning and use the chunk. */
A
Andreas Dilger 已提交
550 551
      else if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN))
      {
552
         png_chunk_warning(png_ptr, "CRC error");
A
Andreas Dilger 已提交
553 554
      }
   }
555
#endif
556

A
Andreas Dilger 已提交
557
   png_set_PLTE(png_ptr, info_ptr, palette, num);
558

559
#if defined(PNG_READ_tRNS_SUPPORTED)
560 561
   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
   {
562
      if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
563
      {
564
         if (png_ptr->num_trans > (png_uint_16)num)
565 566
         {
            png_warning(png_ptr, "Truncating incorrect tRNS chunk length");
567 568 569 570 571 572
            png_ptr->num_trans = (png_uint_16)num;
         }
         if (info_ptr->num_trans > (png_uint_16)num)
         {
            png_warning(png_ptr, "Truncating incorrect info tRNS chunk length");
            info_ptr->num_trans = (png_uint_16)num;
573 574 575 576 577
         }
      }
   }
#endif

A
Andreas Dilger 已提交
578
}
G
Guy Schalnat 已提交
579

580
void /* PRIVATE */
A
Andreas Dilger 已提交
581 582
png_handle_IEND(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
{
A
Andreas Dilger 已提交
583 584
   png_debug(1, "in png_handle_IEND\n");

A
Andreas Dilger 已提交
585 586 587 588 589
   if (!(png_ptr->mode & PNG_HAVE_IHDR) || !(png_ptr->mode & PNG_HAVE_IDAT))
   {
      png_error(png_ptr, "No image in file");
   }

590
   png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND);
A
Andreas Dilger 已提交
591 592 593 594 595

   if (length != 0)
   {
      png_warning(png_ptr, "Incorrect IEND chunk length");
   }
A
Andreas Dilger 已提交
596
   png_crc_finish(png_ptr, length);
597

598
   info_ptr = info_ptr; /* quiet compiler warnings about unused info_ptr */
G
Guy Schalnat 已提交
599 600
}

G
Guy Schalnat 已提交
601
#if defined(PNG_READ_gAMA_SUPPORTED)
602
void /* PRIVATE */
A
Andreas Dilger 已提交
603
png_handle_gAMA(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
G
Guy Schalnat 已提交
604
{
605
   png_fixed_point igamma;
606
#ifdef PNG_FLOATING_POINT_SUPPORTED
A
Andreas Dilger 已提交
607
   float file_gamma;
608
#endif
G
Guy Schalnat 已提交
609 610
   png_byte buf[4];

A
Andreas Dilger 已提交
611 612
   png_debug(1, "in png_handle_gAMA\n");

G
Guy Schalnat 已提交
613 614
   if (!(png_ptr->mode & PNG_HAVE_IHDR))
      png_error(png_ptr, "Missing IHDR before gAMA");
A
Andreas Dilger 已提交
615 616 617 618 619 620
   else if (png_ptr->mode & PNG_HAVE_IDAT)
   {
      png_warning(png_ptr, "Invalid gAMA after IDAT");
      png_crc_finish(png_ptr, length);
      return;
   }
G
Guy Schalnat 已提交
621 622 623
   else if (png_ptr->mode & PNG_HAVE_PLTE)
      /* Should be an error, but we can cope with it */
      png_warning(png_ptr, "Out of place gAMA chunk");
624

625
   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA)
626 627 628 629
#if defined(PNG_READ_sRGB_SUPPORTED)
      && !(info_ptr->valid & PNG_INFO_sRGB)
#endif
      )
A
Andreas Dilger 已提交
630 631 632 633 634
   {
      png_warning(png_ptr, "Duplicate gAMA chunk");
      png_crc_finish(png_ptr, length);
      return;
   }
G
Guy Schalnat 已提交
635

G
Guy Schalnat 已提交
636 637
   if (length != 4)
   {
G
Guy Schalnat 已提交
638
      png_warning(png_ptr, "Incorrect gAMA chunk length");
A
Andreas Dilger 已提交
639
      png_crc_finish(png_ptr, length);
G
Guy Schalnat 已提交
640 641 642 643
      return;
   }

   png_crc_read(png_ptr, buf, 4);
A
Andreas Dilger 已提交
644 645 646
   if (png_crc_finish(png_ptr, 0))
      return;

647
   igamma = (png_fixed_point)png_get_uint_32(buf);
G
Guy Schalnat 已提交
648
   /* check for zero gamma */
A
Andreas Dilger 已提交
649
   if (igamma == 0)
650 651 652 653 654
      {
         png_warning(png_ptr,
           "Ignoring gAMA chunk with gamma=0");
         return;
      }
G
Guy Schalnat 已提交
655

656
#if defined(PNG_READ_sRGB_SUPPORTED)
657
   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB))
658
      if (PNG_OUT_OF_RANGE(igamma, 45500L, 500))
659 660 661
      {
         png_warning(png_ptr,
           "Ignoring incorrect gAMA value when sRGB is also present");
662
#ifndef PNG_NO_CONSOLE_IO
663
         fprintf(stderr, "gamma = (%d/100000)\n", (int)igamma);
664
#endif
665 666 667 668
         return;
      }
#endif /* PNG_READ_sRGB_SUPPORTED */

669
#ifdef PNG_FLOATING_POINT_SUPPORTED
670
   file_gamma = (float)igamma / (float)100000.0;
671 672 673 674
#  ifdef PNG_READ_GAMMA_SUPPORTED
     png_ptr->gamma = file_gamma;
#  endif
     png_set_gAMA(png_ptr, info_ptr, file_gamma);
675 676 677 678
#endif
#ifdef PNG_FIXED_POINT_SUPPORTED
   png_set_gAMA_fixed(png_ptr, info_ptr, igamma);
#endif
G
Guy Schalnat 已提交
679
}
G
Guy Schalnat 已提交
680
#endif
G
Guy Schalnat 已提交
681

G
Guy Schalnat 已提交
682
#if defined(PNG_READ_sBIT_SUPPORTED)
683
void /* PRIVATE */
A
Andreas Dilger 已提交
684
png_handle_sBIT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
G
Guy Schalnat 已提交
685
{
A
Andreas Dilger 已提交
686
   png_size_t truelen;
G
Guy Schalnat 已提交
687
   png_byte buf[4];
G
Guy Schalnat 已提交
688

A
Andreas Dilger 已提交
689 690
   png_debug(1, "in png_handle_sBIT\n");

G
Guy Schalnat 已提交
691
   buf[0] = buf[1] = buf[2] = buf[3] = 0;
G
Guy Schalnat 已提交
692

G
Guy Schalnat 已提交
693 694
   if (!(png_ptr->mode & PNG_HAVE_IHDR))
      png_error(png_ptr, "Missing IHDR before sBIT");
A
Andreas Dilger 已提交
695 696 697 698 699 700
   else if (png_ptr->mode & PNG_HAVE_IDAT)
   {
      png_warning(png_ptr, "Invalid sBIT after IDAT");
      png_crc_finish(png_ptr, length);
      return;
   }
G
Guy Schalnat 已提交
701
   else if (png_ptr->mode & PNG_HAVE_PLTE)
702
   {
G
Guy Schalnat 已提交
703 704
      /* Should be an error, but we can cope with it */
      png_warning(png_ptr, "Out of place sBIT chunk");
705
   }
706
   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT))
A
Andreas Dilger 已提交
707 708 709 710 711
   {
      png_warning(png_ptr, "Duplicate sBIT chunk");
      png_crc_finish(png_ptr, length);
      return;
   }
G
Guy Schalnat 已提交
712

G
Guy Schalnat 已提交
713
   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
A
Andreas Dilger 已提交
714
      truelen = 3;
G
Guy Schalnat 已提交
715
   else
A
Andreas Dilger 已提交
716
      truelen = (png_size_t)png_ptr->channels;
G
Guy Schalnat 已提交
717

718
   if (length != truelen || length > 4)
G
Guy Schalnat 已提交
719
   {
G
Guy Schalnat 已提交
720
      png_warning(png_ptr, "Incorrect sBIT chunk length");
A
Andreas Dilger 已提交
721
      png_crc_finish(png_ptr, length);
G
Guy Schalnat 已提交
722
      return;
G
Guy Schalnat 已提交
723 724
   }

A
Andreas Dilger 已提交
725
   png_crc_read(png_ptr, buf, truelen);
A
Andreas Dilger 已提交
726 727 728
   if (png_crc_finish(png_ptr, 0))
      return;

G
Guy Schalnat 已提交
729 730
   if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
   {
G
Guy Schalnat 已提交
731 732 733 734
      png_ptr->sig_bit.red = buf[0];
      png_ptr->sig_bit.green = buf[1];
      png_ptr->sig_bit.blue = buf[2];
      png_ptr->sig_bit.alpha = buf[3];
G
Guy Schalnat 已提交
735 736 737
   }
   else
   {
G
Guy Schalnat 已提交
738
      png_ptr->sig_bit.gray = buf[0];
739 740 741
      png_ptr->sig_bit.red = buf[0];
      png_ptr->sig_bit.green = buf[0];
      png_ptr->sig_bit.blue = buf[0];
G
Guy Schalnat 已提交
742
      png_ptr->sig_bit.alpha = buf[1];
G
Guy Schalnat 已提交
743
   }
A
Andreas Dilger 已提交
744
   png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
G
Guy Schalnat 已提交
745
}
G
Guy Schalnat 已提交
746
#endif
G
Guy Schalnat 已提交
747

G
Guy Schalnat 已提交
748
#if defined(PNG_READ_cHRM_SUPPORTED)
749
void /* PRIVATE */
A
Andreas Dilger 已提交
750
png_handle_cHRM(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
G
Guy Schalnat 已提交
751
{
752
   png_byte buf[32];
753
#ifdef PNG_FLOATING_POINT_SUPPORTED
G
Guy Schalnat 已提交
754
   float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y;
755
#endif
756
   png_fixed_point int_x_white, int_y_white, int_x_red, int_y_red, int_x_green,
757
      int_y_green, int_x_blue, int_y_blue;
G
Guy Schalnat 已提交
758

759 760
   png_uint_32 uint_x, uint_y;

A
Andreas Dilger 已提交
761 762
   png_debug(1, "in png_handle_cHRM\n");

G
Guy Schalnat 已提交
763
   if (!(png_ptr->mode & PNG_HAVE_IHDR))
764
      png_error(png_ptr, "Missing IHDR before cHRM");
A
Andreas Dilger 已提交
765 766 767 768 769 770
   else if (png_ptr->mode & PNG_HAVE_IDAT)
   {
      png_warning(png_ptr, "Invalid cHRM after IDAT");
      png_crc_finish(png_ptr, length);
      return;
   }
G
Guy Schalnat 已提交
771 772 773
   else if (png_ptr->mode & PNG_HAVE_PLTE)
      /* Should be an error, but we can cope with it */
      png_warning(png_ptr, "Missing PLTE before cHRM");
774

775
   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM)
776 777 778 779
#if defined(PNG_READ_sRGB_SUPPORTED)
      && !(info_ptr->valid & PNG_INFO_sRGB)
#endif
      )
A
Andreas Dilger 已提交
780 781 782 783 784
   {
      png_warning(png_ptr, "Duplicate cHRM chunk");
      png_crc_finish(png_ptr, length);
      return;
   }
G
Guy Schalnat 已提交
785

G
Guy Schalnat 已提交
786 787
   if (length != 32)
   {
G
Guy Schalnat 已提交
788
      png_warning(png_ptr, "Incorrect cHRM chunk length");
A
Andreas Dilger 已提交
789
      png_crc_finish(png_ptr, length);
G
Guy Schalnat 已提交
790
      return;
G
Guy Schalnat 已提交
791 792
   }

793 794 795
   png_crc_read(png_ptr, buf, 32);
   if (png_crc_finish(png_ptr, 0))
      return;
A
Andreas Dilger 已提交
796

797 798
   uint_x = png_get_uint_32(buf);
   uint_y = png_get_uint_32(buf + 4);
799 800
   if (uint_x > 80000L || uint_y > 80000L ||
      uint_x + uint_y > 100000L)
A
Andreas Dilger 已提交
801 802 803 804
   {
      png_warning(png_ptr, "Invalid cHRM white point");
      return;
   }
805 806
   int_x_white = (png_fixed_point)uint_x;
   int_y_white = (png_fixed_point)uint_y;
G
Guy Schalnat 已提交
807

808 809
   uint_x = png_get_uint_32(buf + 8);
   uint_y = png_get_uint_32(buf + 12);
810
   if (uint_x + uint_y > 100000L)
A
Andreas Dilger 已提交
811 812 813 814
   {
      png_warning(png_ptr, "Invalid cHRM red point");
      return;
   }
815 816
   int_x_red = (png_fixed_point)uint_x;
   int_y_red = (png_fixed_point)uint_y;
G
Guy Schalnat 已提交
817

818 819
   uint_x = png_get_uint_32(buf + 16);
   uint_y = png_get_uint_32(buf + 20);
820
   if (uint_x + uint_y > 100000L)
A
Andreas Dilger 已提交
821 822 823 824
   {
      png_warning(png_ptr, "Invalid cHRM green point");
      return;
   }
825 826
   int_x_green = (png_fixed_point)uint_x;
   int_y_green = (png_fixed_point)uint_y;
G
Guy Schalnat 已提交
827

828 829
   uint_x = png_get_uint_32(buf + 24);
   uint_y = png_get_uint_32(buf + 28);
830
   if (uint_x + uint_y > 100000L)
A
Andreas Dilger 已提交
831 832 833 834
   {
      png_warning(png_ptr, "Invalid cHRM blue point");
      return;
   }
835 836 837
   int_x_blue = (png_fixed_point)uint_x;
   int_y_blue = (png_fixed_point)uint_y;

838 839 840 841 842 843 844 845 846 847
#ifdef PNG_FLOATING_POINT_SUPPORTED
   white_x = (float)int_x_white / (float)100000.0;
   white_y = (float)int_y_white / (float)100000.0;
   red_x   = (float)int_x_red   / (float)100000.0;
   red_y   = (float)int_y_red   / (float)100000.0;
   green_x = (float)int_x_green / (float)100000.0;
   green_y = (float)int_y_green / (float)100000.0;
   blue_x  = (float)int_x_blue  / (float)100000.0;
   blue_y  = (float)int_y_blue  / (float)100000.0;
#endif
G
Guy Schalnat 已提交
848

849
#if defined(PNG_READ_sRGB_SUPPORTED)
850
   if ((info_ptr != NULL) && (info_ptr->valid & PNG_INFO_sRGB))
851
      {
852 853 854 855 856 857 858 859
      if (PNG_OUT_OF_RANGE(int_x_white, 31270,  1000) ||
          PNG_OUT_OF_RANGE(int_y_white, 32900,  1000) ||
          PNG_OUT_OF_RANGE(int_x_red,   64000L, 1000) ||
          PNG_OUT_OF_RANGE(int_y_red,   33000,  1000) ||
          PNG_OUT_OF_RANGE(int_x_green, 30000,  1000) ||
          PNG_OUT_OF_RANGE(int_y_green, 60000L, 1000) ||
          PNG_OUT_OF_RANGE(int_x_blue,  15000,  1000) ||
          PNG_OUT_OF_RANGE(int_y_blue,   6000,  1000))
860 861 862
         {
            png_warning(png_ptr,
              "Ignoring incorrect cHRM value when sRGB is also present");
863
#ifndef PNG_NO_CONSOLE_IO
864
#ifdef PNG_FLOATING_POINT_SUPPORTED
865
            fprintf(stderr, "wx=%f, wy=%f, rx=%f, ry=%f\n",
866
               white_x, white_y, red_x, red_y);
867
            fprintf(stderr, "gx=%f, gy=%f, bx=%f, by=%f\n",
868
               green_x, green_y, blue_x, blue_y);
869
#else
870
            fprintf(stderr, "wx=%ld, wy=%ld, rx=%ld, ry=%ld\n",
871
               int_x_white, int_y_white, int_x_red, int_y_red);
872
            fprintf(stderr, "gx=%ld, gy=%ld, bx=%ld, by=%ld\n",
873
               int_x_green, int_y_green, int_x_blue, int_y_blue);
874
#endif
875
#endif /* PNG_NO_CONSOLE_IO */
876
         }
877 878 879 880
         return;
      }
#endif /* PNG_READ_sRGB_SUPPORTED */

881
#ifdef PNG_FLOATING_POINT_SUPPORTED
A
Andreas Dilger 已提交
882
   png_set_cHRM(png_ptr, info_ptr,
G
Guy Schalnat 已提交
883
      white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y);
884 885 886 887 888 889
#endif
#ifdef PNG_FIXED_POINT_SUPPORTED
   png_set_cHRM_fixed(png_ptr, info_ptr,
      int_x_white, int_y_white, int_x_red, int_y_red, int_x_green,
      int_y_green, int_x_blue, int_y_blue);
#endif
890 891
   if (png_crc_finish(png_ptr, 0))
      return;
G
Guy Schalnat 已提交
892
}
G
Guy Schalnat 已提交
893
#endif
G
Guy Schalnat 已提交
894

895
#if defined(PNG_READ_sRGB_SUPPORTED)
896
void /* PRIVATE */
897 898
png_handle_sRGB(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
{
899
   int intent;
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
   png_byte buf[1];

   png_debug(1, "in png_handle_sRGB\n");

   if (!(png_ptr->mode & PNG_HAVE_IHDR))
      png_error(png_ptr, "Missing IHDR before sRGB");
   else if (png_ptr->mode & PNG_HAVE_IDAT)
   {
      png_warning(png_ptr, "Invalid sRGB after IDAT");
      png_crc_finish(png_ptr, length);
      return;
   }
   else if (png_ptr->mode & PNG_HAVE_PLTE)
      /* Should be an error, but we can cope with it */
      png_warning(png_ptr, "Out of place sRGB chunk");
915

916
   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB))
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
   {
      png_warning(png_ptr, "Duplicate sRGB chunk");
      png_crc_finish(png_ptr, length);
      return;
   }

   if (length != 1)
   {
      png_warning(png_ptr, "Incorrect sRGB chunk length");
      png_crc_finish(png_ptr, length);
      return;
   }

   png_crc_read(png_ptr, buf, 1);
   if (png_crc_finish(png_ptr, 0))
      return;

   intent = buf[0];
   /* check for bad intent */
936
   if (intent >= PNG_sRGB_INTENT_LAST)
937 938 939 940 941
   {
      png_warning(png_ptr, "Unknown sRGB intent");
      return;
   }

942
#if defined(PNG_READ_gAMA_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED)
943
   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA))
944
   {
945
   png_fixed_point igamma;
946
#ifdef PNG_FIXED_POINT_SUPPORTED
947
      igamma=info_ptr->int_gamma;
948
#else
949
#  ifdef PNG_FLOATING_POINT_SUPPORTED
950
      igamma=(png_fixed_point)(info_ptr->gamma * 100000.);
951 952
#  endif
#endif
953
      if (PNG_OUT_OF_RANGE(igamma, 45500L, 500))
954 955 956
      {
         png_warning(png_ptr,
           "Ignoring incorrect gAMA value when sRGB is also present");
957
#ifndef PNG_NO_CONSOLE_IO
958
#  ifdef PNG_FIXED_POINT_SUPPORTED
959 960
         fprintf(stderr, "incorrect gamma=(%d/100000)\n",
            (int)png_ptr->int_gamma);
961 962
#  else
#    ifdef PNG_FLOATING_POINT_SUPPORTED
963
         fprintf(stderr, "incorrect gamma=%f\n", png_ptr->gamma);
964 965
#    endif
#  endif
966 967
#endif
      }
968
   }
969 970 971
#endif /* PNG_READ_gAMA_SUPPORTED */

#ifdef PNG_READ_cHRM_SUPPORTED
972
#ifdef PNG_FIXED_POINT_SUPPORTED
973
   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM))
974 975 976 977 978 979 980 981
      if (PNG_OUT_OF_RANGE(info_ptr->int_x_white, 31270,  1000) ||
          PNG_OUT_OF_RANGE(info_ptr->int_y_white, 32900,  1000) ||
          PNG_OUT_OF_RANGE(info_ptr->int_x_red,   64000L, 1000) ||
          PNG_OUT_OF_RANGE(info_ptr->int_y_red,   33000,  1000) ||
          PNG_OUT_OF_RANGE(info_ptr->int_x_green, 30000,  1000) ||
          PNG_OUT_OF_RANGE(info_ptr->int_y_green, 60000L, 1000) ||
          PNG_OUT_OF_RANGE(info_ptr->int_x_blue,  15000,  1000) ||
          PNG_OUT_OF_RANGE(info_ptr->int_y_blue,   6000,  1000))
982 983 984 985
         {
            png_warning(png_ptr,
              "Ignoring incorrect cHRM value when sRGB is also present");
         }
986
#endif /* PNG_FIXED_POINT_SUPPORTED */
987
#endif /* PNG_READ_cHRM_SUPPORTED */
988

989
   png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, intent);
990
}
991 992
#endif /* PNG_READ_sRGB_SUPPORTED */

993
#if defined(PNG_READ_iCCP_SUPPORTED)
994
void /* PRIVATE */
995 996 997 998
png_handle_iCCP(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
/* Note: this does not properly handle chunks that are > 64K under DOS */
{
   png_byte compression_type;
999
   png_bytep pC;
1000 1001
   png_charp profile;
   png_uint_32 skip = 0;
1002
   png_uint_32 profile_size, profile_length;
1003
   png_size_t slength, prefix_length, data_length;
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018

   png_debug(1, "in png_handle_iCCP\n");

   if (!(png_ptr->mode & PNG_HAVE_IHDR))
      png_error(png_ptr, "Missing IHDR before iCCP");
   else if (png_ptr->mode & PNG_HAVE_IDAT)
   {
      png_warning(png_ptr, "Invalid iCCP after IDAT");
      png_crc_finish(png_ptr, length);
      return;
   }
   else if (png_ptr->mode & PNG_HAVE_PLTE)
      /* Should be an error, but we can cope with it */
      png_warning(png_ptr, "Out of place iCCP chunk");

1019
   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_iCCP))
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
   {
      png_warning(png_ptr, "Duplicate iCCP chunk");
      png_crc_finish(png_ptr, length);
      return;
   }

#ifdef PNG_MAX_MALLOC_64K
   if (length > (png_uint_32)65535L)
   {
      png_warning(png_ptr, "iCCP chunk too large to fit in memory");
      skip = length - (png_uint_32)65535L;
      length = (png_uint_32)65535L;
   }
#endif

1035 1036
   png_free(png_ptr, png_ptr->chunkdata);
   png_ptr->chunkdata = (png_charp)png_malloc(png_ptr, length + 1);
1037
   slength = (png_size_t)length;
1038
   png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
1039 1040 1041

   if (png_crc_finish(png_ptr, skip))
   {
1042 1043
      png_free(png_ptr, png_ptr->chunkdata);
      png_ptr->chunkdata = NULL;
1044 1045 1046
      return;
   }

1047
   png_ptr->chunkdata[slength] = 0x00;
1048

1049
   for (profile = png_ptr->chunkdata; *profile; profile++)
1050
      /* empty loop to find end of name */ ;
1051

1052 1053
   ++profile;

1054
   /* there should be at least one zero (the compression type byte)
1055
      following the separator, and we should be on it  */
1056
   if ( profile >= png_ptr->chunkdata + slength - 1)
1057
   {
1058 1059
      png_free(png_ptr, png_ptr->chunkdata);
      png_ptr->chunkdata = NULL;
1060
      png_warning(png_ptr, "Malformed iCCP chunk");
1061
      return;
1062 1063
   }

1064
   /* compression_type should always be zero */
1065
   compression_type = *profile++;
1066 1067 1068
   if (compression_type)
   {
      png_warning(png_ptr, "Ignoring nonzero compression type in iCCP chunk");
1069
      compression_type = 0x00;  /* Reset it to zero (libpng-1.0.6 through 1.0.8
1070 1071
                                 wrote nonzero) */
   }
1072

1073
   prefix_length = profile - png_ptr->chunkdata;
1074 1075
   png_decompress_chunk(png_ptr, compression_type,
     slength, prefix_length, &data_length);
1076

1077
   profile_length = data_length - prefix_length;
1078

1079
   if ( prefix_length > data_length || profile_length < 4)
1080
   {
1081 1082
      png_free(png_ptr, png_ptr->chunkdata);
      png_ptr->chunkdata = NULL;
1083 1084 1085 1086
      png_warning(png_ptr, "Profile size field missing from iCCP chunk");
      return;
   }

1087
   /* Check the profile_size recorded in the first 32 bits of the ICC profile */
1088
   pC = (png_bytep)(png_ptr->chunkdata + prefix_length);
1089
   profile_size = ((*(pC    ))<<24) |
1090 1091 1092
                  ((*(pC + 1))<<16) |
                  ((*(pC + 2))<< 8) |
                  ((*(pC + 3))    );
1093

1094
   if (profile_size < profile_length)
1095 1096
      profile_length = profile_size;

1097
   if (profile_size > profile_length)
1098
   {
1099 1100
      png_free(png_ptr, png_ptr->chunkdata);
      png_ptr->chunkdata = NULL;
1101
      png_warning(png_ptr, "Ignoring truncated iCCP profile");
1102 1103 1104
      return;
   }

1105 1106 1107 1108
   png_set_iCCP(png_ptr, info_ptr, png_ptr->chunkdata,
     compression_type, png_ptr->chunkdata + prefix_length, profile_length);
   png_free(png_ptr, png_ptr->chunkdata);
   png_ptr->chunkdata = NULL;
1109 1110 1111 1112
}
#endif /* PNG_READ_iCCP_SUPPORTED */

#if defined(PNG_READ_sPLT_SUPPORTED)
1113
void /* PRIVATE */
1114 1115 1116 1117
png_handle_sPLT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
/* Note: this does not properly handle chunks that are > 64K under DOS */
{
   png_bytep entry_start;
1118
   png_sPLT_t new_palette;
1119 1120 1121
#ifdef PNG_NO_POINTER_INDEXING
   png_sPLT_entryp pp;
#endif
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
   int data_length, entry_size, i;
   png_uint_32 skip = 0;
   png_size_t slength;

   png_debug(1, "in png_handle_sPLT\n");

   if (!(png_ptr->mode & PNG_HAVE_IHDR))
      png_error(png_ptr, "Missing IHDR before sPLT");
   else if (png_ptr->mode & PNG_HAVE_IDAT)
   {
      png_warning(png_ptr, "Invalid sPLT after IDAT");
      png_crc_finish(png_ptr, length);
      return;
   }

#ifdef PNG_MAX_MALLOC_64K
   if (length > (png_uint_32)65535L)
   {
      png_warning(png_ptr, "sPLT chunk too large to fit in memory");
      skip = length - (png_uint_32)65535L;
      length = (png_uint_32)65535L;
   }
#endif

1146 1147
   png_free(png_ptr, png_ptr->chunkdata);
   png_ptr->chunkdata = (png_charp)png_malloc(png_ptr, length + 1);
1148
   slength = (png_size_t)length;
1149
   png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
1150 1151 1152

   if (png_crc_finish(png_ptr, skip))
   {
1153 1154
      png_free(png_ptr, png_ptr->chunkdata);
      png_ptr->chunkdata = NULL;
1155 1156 1157
      return;
   }

1158
   png_ptr->chunkdata[slength] = 0x00;
1159

1160
   for (entry_start = (png_bytep)png_ptr->chunkdata; *entry_start; entry_start++)
1161 1162 1163 1164
      /* empty loop to find end of name */ ;
   ++entry_start;

   /* a sample depth should follow the separator, and we should be on it  */
1165
   if (entry_start > (png_bytep)png_ptr->chunkdata + slength - 2)
1166
   {
1167 1168
      png_free(png_ptr, png_ptr->chunkdata);
      png_ptr->chunkdata = NULL;
1169 1170
      png_warning(png_ptr, "malformed sPLT chunk");
      return;
1171 1172 1173 1174
   }

   new_palette.depth = *entry_start++;
   entry_size = (new_palette.depth == 8 ? 6 : 10);
1175
   data_length = (slength - (entry_start - (png_bytep)png_ptr->chunkdata));
1176 1177 1178 1179

   /* integrity-check the data length */
   if (data_length % entry_size)
   {
1180 1181
      png_free(png_ptr, png_ptr->chunkdata);
      png_ptr->chunkdata = NULL;
1182 1183
      png_warning(png_ptr, "sPLT chunk has bad length");
      return;
1184 1185
   }

1186
   new_palette.nentries = (png_int_32) ( data_length / entry_size);
1187
   if ((png_uint_32) new_palette.nentries >
1188
       (png_uint_32) (PNG_SIZE_MAX / png_sizeof(png_sPLT_entry)))
1189 1190 1191 1192 1193
   {
       png_warning(png_ptr, "sPLT chunk too long");
       return;
   }
   new_palette.entries = (png_sPLT_entryp)png_malloc_warn(
1194
       png_ptr, new_palette.nentries * png_sizeof(png_sPLT_entry));
1195 1196 1197 1198 1199
   if (new_palette.entries == NULL)
   {
       png_warning(png_ptr, "sPLT chunk requires too much memory");
       return;
   }
1200

1201
#ifndef PNG_NO_POINTER_INDEXING
1202 1203
   for (i = 0; i < new_palette.nentries; i++)
   {
1204
      png_sPLT_entryp pp = new_palette.entries + i;
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221

      if (new_palette.depth == 8)
      {
          pp->red = *entry_start++;
          pp->green = *entry_start++;
          pp->blue = *entry_start++;
          pp->alpha = *entry_start++;
      }
      else
      {
          pp->red   = png_get_uint_16(entry_start); entry_start += 2;
          pp->green = png_get_uint_16(entry_start); entry_start += 2;
          pp->blue  = png_get_uint_16(entry_start); entry_start += 2;
          pp->alpha = png_get_uint_16(entry_start); entry_start += 2;
      }
      pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
   }
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
#else
   pp = new_palette.entries;
   for (i = 0; i < new_palette.nentries; i++)
   {

      if (new_palette.depth == 8)
      {
          pp[i].red   = *entry_start++;
          pp[i].green = *entry_start++;
          pp[i].blue  = *entry_start++;
          pp[i].alpha = *entry_start++;
      }
      else
      {
          pp[i].red   = png_get_uint_16(entry_start); entry_start += 2;
          pp[i].green = png_get_uint_16(entry_start); entry_start += 2;
          pp[i].blue  = png_get_uint_16(entry_start); entry_start += 2;
          pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2;
      }
      pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
   }
#endif
1244 1245

   /* discard all chunk data except the name and stash that */
1246
   new_palette.name = png_ptr->chunkdata;
1247

1248
   png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);
1249

1250 1251
   png_free(png_ptr, png_ptr->chunkdata);
   png_ptr->chunkdata = NULL;
1252 1253 1254 1255
   png_free(png_ptr, new_palette.entries);
}
#endif /* PNG_READ_sPLT_SUPPORTED */

G
Guy Schalnat 已提交
1256
#if defined(PNG_READ_tRNS_SUPPORTED)
1257
void /* PRIVATE */
A
Andreas Dilger 已提交
1258
png_handle_tRNS(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
G
Guy Schalnat 已提交
1259
{
1260
   png_byte readbuf[PNG_MAX_PALETTE_LENGTH];
1261

A
Andreas Dilger 已提交
1262 1263
   png_debug(1, "in png_handle_tRNS\n");

G
Guy Schalnat 已提交
1264 1265
   if (!(png_ptr->mode & PNG_HAVE_IHDR))
      png_error(png_ptr, "Missing IHDR before tRNS");
A
Andreas Dilger 已提交
1266 1267 1268 1269 1270 1271
   else if (png_ptr->mode & PNG_HAVE_IDAT)
   {
      png_warning(png_ptr, "Invalid tRNS after IDAT");
      png_crc_finish(png_ptr, length);
      return;
   }
1272
   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
A
Andreas Dilger 已提交
1273
   {
1274
      png_warning(png_ptr, "Duplicate tRNS chunk");
A
Andreas Dilger 已提交
1275 1276 1277
      png_crc_finish(png_ptr, length);
      return;
   }
G
Guy Schalnat 已提交
1278

1279
   if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
G
Guy Schalnat 已提交
1280
   {
1281 1282 1283
      png_byte buf[2];

      if (length != 2)
G
Guy Schalnat 已提交
1284
      {
G
Guy Schalnat 已提交
1285
         png_warning(png_ptr, "Incorrect tRNS chunk length");
A
Andreas Dilger 已提交
1286
         png_crc_finish(png_ptr, length);
G
Guy Schalnat 已提交
1287 1288 1289
         return;
      }

1290 1291 1292
      png_crc_read(png_ptr, buf, 2);
      png_ptr->num_trans = 1;
      png_ptr->trans_values.gray = png_get_uint_16(buf);
G
Guy Schalnat 已提交
1293 1294 1295 1296 1297 1298 1299
   }
   else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
   {
      png_byte buf[6];

      if (length != 6)
      {
G
Guy Schalnat 已提交
1300
         png_warning(png_ptr, "Incorrect tRNS chunk length");
A
Andreas Dilger 已提交
1301
         png_crc_finish(png_ptr, length);
G
Guy Schalnat 已提交
1302 1303
         return;
      }
A
Andreas Dilger 已提交
1304
      png_crc_read(png_ptr, buf, (png_size_t)length);
1305
      png_ptr->num_trans = 1;
G
Guy Schalnat 已提交
1306 1307 1308 1309
      png_ptr->trans_values.red = png_get_uint_16(buf);
      png_ptr->trans_values.green = png_get_uint_16(buf + 2);
      png_ptr->trans_values.blue = png_get_uint_16(buf + 4);
   }
1310
   else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
G
Guy Schalnat 已提交
1311
   {
1312 1313 1314 1315 1316 1317 1318
      if (!(png_ptr->mode & PNG_HAVE_PLTE))
      {
         /* Should be an error, but we can cope with it. */
         png_warning(png_ptr, "Missing PLTE before tRNS");
      }
      if (length > (png_uint_32)png_ptr->num_palette ||
          length > PNG_MAX_PALETTE_LENGTH)
G
Guy Schalnat 已提交
1319
      {
G
Guy Schalnat 已提交
1320
         png_warning(png_ptr, "Incorrect tRNS chunk length");
A
Andreas Dilger 已提交
1321
         png_crc_finish(png_ptr, length);
G
Guy Schalnat 已提交
1322 1323
         return;
      }
1324 1325 1326 1327 1328 1329 1330 1331
      if (length == 0)
      {
         png_warning(png_ptr, "Zero length tRNS chunk");
         png_crc_finish(png_ptr, length);
         return;
      }
      png_crc_read(png_ptr, readbuf, (png_size_t)length);
      png_ptr->num_trans = (png_uint_16)length;
G
Guy Schalnat 已提交
1332 1333
   }
   else
G
Guy Schalnat 已提交
1334 1335
   {
      png_warning(png_ptr, "tRNS chunk not allowed with alpha channel");
A
Andreas Dilger 已提交
1336
      png_crc_finish(png_ptr, length);
G
Guy Schalnat 已提交
1337 1338
      return;
   }
G
Guy Schalnat 已提交
1339

A
Andreas Dilger 已提交
1340
   if (png_crc_finish(png_ptr, 0))
1341 1342
   {
      png_ptr->num_trans = 0;
A
Andreas Dilger 已提交
1343
      return;
1344
   }
A
Andreas Dilger 已提交
1345

1346
   png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,
G
Guy Schalnat 已提交
1347 1348
      &(png_ptr->trans_values));
}
G
Guy Schalnat 已提交
1349
#endif
G
Guy Schalnat 已提交
1350

G
Guy Schalnat 已提交
1351
#if defined(PNG_READ_bKGD_SUPPORTED)
1352
void /* PRIVATE */
A
Andreas Dilger 已提交
1353
png_handle_bKGD(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
G
Guy Schalnat 已提交
1354
{
A
Andreas Dilger 已提交
1355
   png_size_t truelen;
G
Guy Schalnat 已提交
1356 1357
   png_byte buf[6];

A
Andreas Dilger 已提交
1358 1359
   png_debug(1, "in png_handle_bKGD\n");

G
Guy Schalnat 已提交
1360 1361
   if (!(png_ptr->mode & PNG_HAVE_IHDR))
      png_error(png_ptr, "Missing IHDR before bKGD");
A
Andreas Dilger 已提交
1362 1363 1364 1365 1366 1367
   else if (png_ptr->mode & PNG_HAVE_IDAT)
   {
      png_warning(png_ptr, "Invalid bKGD after IDAT");
      png_crc_finish(png_ptr, length);
      return;
   }
G
Guy Schalnat 已提交
1368 1369 1370 1371
   else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
            !(png_ptr->mode & PNG_HAVE_PLTE))
   {
      png_warning(png_ptr, "Missing PLTE before bKGD");
A
Andreas Dilger 已提交
1372 1373 1374
      png_crc_finish(png_ptr, length);
      return;
   }
1375
   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD))
A
Andreas Dilger 已提交
1376 1377 1378
   {
      png_warning(png_ptr, "Duplicate bKGD chunk");
      png_crc_finish(png_ptr, length);
G
Guy Schalnat 已提交
1379 1380 1381
      return;
   }

G
Guy Schalnat 已提交
1382 1383 1384 1385 1386 1387 1388
   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
      truelen = 1;
   else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
      truelen = 6;
   else
      truelen = 2;

A
Andreas Dilger 已提交
1389
   if (length != truelen)
G
Guy Schalnat 已提交
1390
   {
G
Guy Schalnat 已提交
1391
      png_warning(png_ptr, "Incorrect bKGD chunk length");
A
Andreas Dilger 已提交
1392
      png_crc_finish(png_ptr, length);
G
Guy Schalnat 已提交
1393 1394 1395
      return;
   }

A
Andreas Dilger 已提交
1396
   png_crc_read(png_ptr, buf, truelen);
A
Andreas Dilger 已提交
1397 1398 1399
   if (png_crc_finish(png_ptr, 0))
      return;

G
Guy Schalnat 已提交
1400 1401 1402
   /* We convert the index value into RGB components so that we can allow
    * arbitrary RGB values for background when we have transparency, and
    * so it is easy to determine the RGB values of the background color
A
Andreas Dilger 已提交
1403
    * from the info_ptr struct. */
G
Guy Schalnat 已提交
1404
   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
G
Guy Schalnat 已提交
1405
   {
G
Guy Schalnat 已提交
1406
      png_ptr->background.index = buf[0];
1407
      if (info_ptr && info_ptr->num_palette)
1408
      {
1409
          if (buf[0] > info_ptr->num_palette)
1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
          {
             png_warning(png_ptr, "Incorrect bKGD chunk index value");
             return;
          }
          png_ptr->background.red =
             (png_uint_16)png_ptr->palette[buf[0]].red;
          png_ptr->background.green =
             (png_uint_16)png_ptr->palette[buf[0]].green;
          png_ptr->background.blue =
             (png_uint_16)png_ptr->palette[buf[0]].blue;
      }
G
Guy Schalnat 已提交
1421
   }
A
Andreas Dilger 已提交
1422
   else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) /* GRAY */
G
Guy Schalnat 已提交
1423 1424 1425 1426
   {
      png_ptr->background.red =
      png_ptr->background.green =
      png_ptr->background.blue =
G
Guy Schalnat 已提交
1427
      png_ptr->background.gray = png_get_uint_16(buf);
G
Guy Schalnat 已提交
1428
   }
G
Guy Schalnat 已提交
1429 1430 1431 1432 1433 1434 1435
   else
   {
      png_ptr->background.red = png_get_uint_16(buf);
      png_ptr->background.green = png_get_uint_16(buf + 2);
      png_ptr->background.blue = png_get_uint_16(buf + 4);
   }

A
Andreas Dilger 已提交
1436
   png_set_bKGD(png_ptr, info_ptr, &(png_ptr->background));
G
Guy Schalnat 已提交
1437
}
G
Guy Schalnat 已提交
1438
#endif
G
Guy Schalnat 已提交
1439

G
Guy Schalnat 已提交
1440
#if defined(PNG_READ_hIST_SUPPORTED)
1441
void /* PRIVATE */
A
Andreas Dilger 已提交
1442
png_handle_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
G
Guy Schalnat 已提交
1443
{
1444
   unsigned int num, i;
1445
   png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];
G
Guy Schalnat 已提交
1446

A
Andreas Dilger 已提交
1447 1448
   png_debug(1, "in png_handle_hIST\n");

G
Guy Schalnat 已提交
1449 1450
   if (!(png_ptr->mode & PNG_HAVE_IHDR))
      png_error(png_ptr, "Missing IHDR before hIST");
A
Andreas Dilger 已提交
1451 1452 1453 1454 1455 1456
   else if (png_ptr->mode & PNG_HAVE_IDAT)
   {
      png_warning(png_ptr, "Invalid hIST after IDAT");
      png_crc_finish(png_ptr, length);
      return;
   }
G
Guy Schalnat 已提交
1457 1458 1459
   else if (!(png_ptr->mode & PNG_HAVE_PLTE))
   {
      png_warning(png_ptr, "Missing PLTE before hIST");
A
Andreas Dilger 已提交
1460 1461 1462
      png_crc_finish(png_ptr, length);
      return;
   }
1463
   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST))
A
Andreas Dilger 已提交
1464 1465 1466
   {
      png_warning(png_ptr, "Duplicate hIST chunk");
      png_crc_finish(png_ptr, length);
G
Guy Schalnat 已提交
1467 1468 1469
      return;
   }

1470
   num = length / 2 ;
1471 1472
   if (num != (unsigned int) png_ptr->num_palette || num >
      (unsigned int) PNG_MAX_PALETTE_LENGTH)
G
Guy Schalnat 已提交
1473 1474
   {
      png_warning(png_ptr, "Incorrect hIST chunk length");
A
Andreas Dilger 已提交
1475
      png_crc_finish(png_ptr, length);
G
Guy Schalnat 已提交
1476 1477
      return;
   }
G
Guy Schalnat 已提交
1478

G
Guy Schalnat 已提交
1479
   for (i = 0; i < num; i++)
G
Guy Schalnat 已提交
1480 1481 1482 1483
   {
      png_byte buf[2];

      png_crc_read(png_ptr, buf, 2);
1484
      readbuf[i] = png_get_uint_16(buf);
G
Guy Schalnat 已提交
1485
   }
A
Andreas Dilger 已提交
1486 1487 1488 1489

   if (png_crc_finish(png_ptr, 0))
      return;

1490
   png_set_hIST(png_ptr, info_ptr, readbuf);
G
Guy Schalnat 已提交
1491
}
G
Guy Schalnat 已提交
1492
#endif
G
Guy Schalnat 已提交
1493

G
Guy Schalnat 已提交
1494
#if defined(PNG_READ_pHYs_SUPPORTED)
1495
void /* PRIVATE */
A
Andreas Dilger 已提交
1496
png_handle_pHYs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
G
Guy Schalnat 已提交
1497 1498 1499 1500 1501
{
   png_byte buf[9];
   png_uint_32 res_x, res_y;
   int unit_type;

A
Andreas Dilger 已提交
1502 1503
   png_debug(1, "in png_handle_pHYs\n");

G
Guy Schalnat 已提交
1504
   if (!(png_ptr->mode & PNG_HAVE_IHDR))
1505
      png_error(png_ptr, "Missing IHDR before pHYs");
A
Andreas Dilger 已提交
1506 1507
   else if (png_ptr->mode & PNG_HAVE_IDAT)
   {
1508
      png_warning(png_ptr, "Invalid pHYs after IDAT");
A
Andreas Dilger 已提交
1509 1510 1511
      png_crc_finish(png_ptr, length);
      return;
   }
1512
   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
A
Andreas Dilger 已提交
1513
   {
1514
      png_warning(png_ptr, "Duplicate pHYs chunk");
A
Andreas Dilger 已提交
1515 1516 1517
      png_crc_finish(png_ptr, length);
      return;
   }
G
Guy Schalnat 已提交
1518

G
Guy Schalnat 已提交
1519 1520
   if (length != 9)
   {
G
Guy Schalnat 已提交
1521
      png_warning(png_ptr, "Incorrect pHYs chunk length");
A
Andreas Dilger 已提交
1522
      png_crc_finish(png_ptr, length);
G
Guy Schalnat 已提交
1523
      return;
G
Guy Schalnat 已提交
1524
   }
G
Guy Schalnat 已提交
1525 1526

   png_crc_read(png_ptr, buf, 9);
A
Andreas Dilger 已提交
1527 1528
   if (png_crc_finish(png_ptr, 0))
      return;
G
Guy Schalnat 已提交
1529 1530 1531 1532

   res_x = png_get_uint_32(buf);
   res_y = png_get_uint_32(buf + 4);
   unit_type = buf[8];
A
Andreas Dilger 已提交
1533
   png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
G
Guy Schalnat 已提交
1534
}
G
Guy Schalnat 已提交
1535
#endif
G
Guy Schalnat 已提交
1536

G
Guy Schalnat 已提交
1537
#if defined(PNG_READ_oFFs_SUPPORTED)
1538
void /* PRIVATE */
A
Andreas Dilger 已提交
1539
png_handle_oFFs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
G
Guy Schalnat 已提交
1540 1541
{
   png_byte buf[9];
1542
   png_int_32 offset_x, offset_y;
G
Guy Schalnat 已提交
1543 1544
   int unit_type;

A
Andreas Dilger 已提交
1545 1546
   png_debug(1, "in png_handle_oFFs\n");

G
Guy Schalnat 已提交
1547 1548
   if (!(png_ptr->mode & PNG_HAVE_IHDR))
      png_error(png_ptr, "Missing IHDR before oFFs");
A
Andreas Dilger 已提交
1549 1550 1551 1552 1553 1554
   else if (png_ptr->mode & PNG_HAVE_IDAT)
   {
      png_warning(png_ptr, "Invalid oFFs after IDAT");
      png_crc_finish(png_ptr, length);
      return;
   }
1555
   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs))
A
Andreas Dilger 已提交
1556 1557 1558 1559 1560
   {
      png_warning(png_ptr, "Duplicate oFFs chunk");
      png_crc_finish(png_ptr, length);
      return;
   }
G
Guy Schalnat 已提交
1561

G
Guy Schalnat 已提交
1562 1563
   if (length != 9)
   {
G
Guy Schalnat 已提交
1564
      png_warning(png_ptr, "Incorrect oFFs chunk length");
A
Andreas Dilger 已提交
1565
      png_crc_finish(png_ptr, length);
G
Guy Schalnat 已提交
1566 1567 1568
      return;
   }

G
Guy Schalnat 已提交
1569
   png_crc_read(png_ptr, buf, 9);
A
Andreas Dilger 已提交
1570 1571
   if (png_crc_finish(png_ptr, 0))
      return;
G
Guy Schalnat 已提交
1572

1573 1574
   offset_x = png_get_int_32(buf);
   offset_y = png_get_int_32(buf + 4);
G
Guy Schalnat 已提交
1575
   unit_type = buf[8];
A
Andreas Dilger 已提交
1576 1577 1578 1579 1580
   png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
}
#endif

#if defined(PNG_READ_pCAL_SUPPORTED)
1581
/* read the pCAL chunk (described in the PNG Extensions document) */
1582
void /* PRIVATE */
A
Andreas Dilger 已提交
1583 1584 1585 1586 1587 1588
png_handle_pCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
{
   png_int_32 X0, X1;
   png_byte type, nparams;
   png_charp buf, units, endptr;
   png_charpp params;
1589
   png_size_t slength;
A
Andreas Dilger 已提交
1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601
   int i;

   png_debug(1, "in png_handle_pCAL\n");

   if (!(png_ptr->mode & PNG_HAVE_IHDR))
      png_error(png_ptr, "Missing IHDR before pCAL");
   else if (png_ptr->mode & PNG_HAVE_IDAT)
   {
      png_warning(png_ptr, "Invalid pCAL after IDAT");
      png_crc_finish(png_ptr, length);
      return;
   }
1602
   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL))
A
Andreas Dilger 已提交
1603 1604 1605 1606 1607 1608
   {
      png_warning(png_ptr, "Duplicate pCAL chunk");
      png_crc_finish(png_ptr, length);
      return;
   }

1609
   png_debug1(2, "Allocating and reading pCAL chunk data (%lu bytes)\n",
1610
      length + 1);
1611 1612 1613
   png_free(png_ptr, png_ptr->chunkdata);
   png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
   if (png_ptr->chunkdata == NULL)
1614
     {
1615
       png_warning(png_ptr, "No memory for pCAL purpose");
1616 1617
       return;
     }
1618
   slength = (png_size_t)length;
1619
   png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
A
Andreas Dilger 已提交
1620 1621 1622

   if (png_crc_finish(png_ptr, 0))
   {
1623 1624
      png_free(png_ptr, png_ptr->chunkdata);
      png_ptr->chunkdata = NULL;
A
Andreas Dilger 已提交
1625 1626 1627
      return;
   }

1628
   png_ptr->chunkdata[slength] = 0x00; /* null terminate the last string */
A
Andreas Dilger 已提交
1629 1630

   png_debug(3, "Finding end of pCAL purpose string\n");
1631
   for (buf = png_ptr->chunkdata; *buf; buf++)
1632
      /* empty loop */ ;
A
Andreas Dilger 已提交
1633

1634
   endptr = png_ptr->chunkdata + slength;
A
Andreas Dilger 已提交
1635 1636 1637 1638 1639 1640

   /* We need to have at least 12 bytes after the purpose string
      in order to get the parameter information. */
   if (endptr <= buf + 12)
   {
      png_warning(png_ptr, "Invalid pCAL data");
1641 1642
      png_free(png_ptr, png_ptr->chunkdata);
      png_ptr->chunkdata = NULL;
A
Andreas Dilger 已提交
1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661
      return;
   }

   png_debug(3, "Reading pCAL X0, X1, type, nparams, and units\n");
   X0 = png_get_int_32((png_bytep)buf+1);
   X1 = png_get_int_32((png_bytep)buf+5);
   type = buf[9];
   nparams = buf[10];
   units = buf + 11;

   png_debug(3, "Checking pCAL equation type and number of parameters\n");
   /* Check that we have the right number of parameters for known
      equation types. */
   if ((type == PNG_EQUATION_LINEAR && nparams != 2) ||
       (type == PNG_EQUATION_BASE_E && nparams != 3) ||
       (type == PNG_EQUATION_ARBITRARY && nparams != 3) ||
       (type == PNG_EQUATION_HYPERBOLIC && nparams != 4))
   {
      png_warning(png_ptr, "Invalid pCAL parameters for equation type");
1662 1663
      png_free(png_ptr, png_ptr->chunkdata);
      png_ptr->chunkdata = NULL;
A
Andreas Dilger 已提交
1664 1665 1666 1667 1668 1669 1670
      return;
   }
   else if (type >= PNG_EQUATION_LAST)
   {
      png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
   }

1671
   for (buf = units; *buf; buf++)
1672
      /* Empty loop to move past the units string. */ ;
A
Andreas Dilger 已提交
1673 1674

   png_debug(3, "Allocating pCAL parameters array\n");
1675
   params = (png_charpp)png_malloc_warn(png_ptr,
1676
      (png_size_t)(nparams * png_sizeof(png_charp)));
1677 1678
   if (params == NULL)
     {
1679 1680
       png_free(png_ptr, png_ptr->chunkdata);
       png_ptr->chunkdata = NULL;
1681
       png_warning(png_ptr, "No memory for pCAL params");
1682 1683
       return;
     }
A
Andreas Dilger 已提交
1684 1685

   /* Get pointers to the start of each parameter string. */
1686
   for (i = 0; i < (int)nparams; i++)
A
Andreas Dilger 已提交
1687 1688 1689 1690
   {
      buf++; /* Skip the null string terminator from previous parameter. */

      png_debug1(3, "Reading pCAL parameter %d\n", i);
1691
      for (params[i] = buf; buf <= endptr && *buf != 0x00; buf++)
1692
         /* Empty loop to move past each parameter string */ ;
A
Andreas Dilger 已提交
1693 1694 1695 1696 1697

      /* Make sure we haven't run out of data yet */
      if (buf > endptr)
      {
         png_warning(png_ptr, "Invalid pCAL data");
1698 1699
         png_free(png_ptr, png_ptr->chunkdata);
         png_ptr->chunkdata = NULL;
A
Andreas Dilger 已提交
1700 1701 1702 1703 1704
         png_free(png_ptr, params);
         return;
      }
   }

1705
   png_set_pCAL(png_ptr, info_ptr, png_ptr->chunkdata, X0, X1, type, nparams,
A
Andreas Dilger 已提交
1706 1707
      units, params);

1708 1709
   png_free(png_ptr, png_ptr->chunkdata);
   png_ptr->chunkdata = NULL;
A
Andreas Dilger 已提交
1710
   png_free(png_ptr, params);
G
Guy Schalnat 已提交
1711
}
G
Guy Schalnat 已提交
1712
#endif
G
Guy Schalnat 已提交
1713

1714 1715
#if defined(PNG_READ_sCAL_SUPPORTED)
/* read the sCAL chunk */
1716
void /* PRIVATE */
1717 1718
png_handle_sCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
{
1719
   png_charp ep;
1720
#ifdef PNG_FLOATING_POINT_SUPPORTED
1721
   double width, height;
1722
   png_charp vp;
1723 1724 1725 1726
#else
#ifdef PNG_FIXED_POINT_SUPPORTED
   png_charp swidth, sheight;
#endif
1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739
#endif
   png_size_t slength;

   png_debug(1, "in png_handle_sCAL\n");

   if (!(png_ptr->mode & PNG_HAVE_IHDR))
      png_error(png_ptr, "Missing IHDR before sCAL");
   else if (png_ptr->mode & PNG_HAVE_IDAT)
   {
      png_warning(png_ptr, "Invalid sCAL after IDAT");
      png_crc_finish(png_ptr, length);
      return;
   }
1740
   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL))
1741 1742 1743 1744 1745 1746
   {
      png_warning(png_ptr, "Duplicate sCAL chunk");
      png_crc_finish(png_ptr, length);
      return;
   }

1747
   png_debug1(2, "Allocating and reading sCAL chunk data (%lu bytes)\n",
1748
      length + 1);
1749 1750
   png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
   if (png_ptr->chunkdata == NULL)
1751 1752 1753 1754
   {
      png_warning(png_ptr, "Out of memory while processing sCAL chunk");
      return;
   }
1755
   slength = (png_size_t)length;
1756
   png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
1757 1758 1759

   if (png_crc_finish(png_ptr, 0))
   {
1760 1761
      png_free(png_ptr, png_ptr->chunkdata);
      png_ptr->chunkdata = NULL;
1762 1763 1764
      return;
   }

1765
   png_ptr->chunkdata[slength] = 0x00; /* null terminate the last string */
1766

1767
   ep = png_ptr->chunkdata + 1;        /* skip unit byte */
1768 1769

#ifdef PNG_FLOATING_POINT_SUPPORTED
1770
   width = png_strtod(png_ptr, ep, &vp);
1771
   if (*vp)
1772
   {
1773 1774
      png_warning(png_ptr, "malformed width string in sCAL chunk");
      return;
1775
   }
1776 1777
#else
#ifdef PNG_FIXED_POINT_SUPPORTED
1778 1779
   swidth = (png_charp)png_malloc_warn(png_ptr, png_strlen(ep) + 1);
   if (swidth == NULL)
1780 1781 1782 1783 1784
   {
      png_warning(png_ptr, "Out of memory while processing sCAL chunk width");
      return;
   }
   png_memcpy(swidth, ep, png_strlen(ep));
1785 1786
#endif
#endif
1787

1788
   for (ep = png_ptr->chunkdata; *ep; ep++)
1789 1790 1791
      /* empty loop */ ;
   ep++;

1792
   if (png_ptr->chunkdata + slength < ep)
1793 1794 1795 1796 1797 1798
   {
      png_warning(png_ptr, "Truncated sCAL chunk");
#if defined(PNG_FIXED_POINT_SUPPORTED) && \
    !defined(PNG_FLOATING_POINT_SUPPORTED)
      png_free(png_ptr, swidth);
#endif
1799 1800
      png_free(png_ptr, png_ptr->chunkdata);
      png_ptr->chunkdata = NULL;
1801 1802 1803
      return;
   }

1804
#ifdef PNG_FLOATING_POINT_SUPPORTED
1805
   height = png_strtod(png_ptr, ep, &vp);
1806
   if (*vp)
1807
   {
1808 1809
      png_warning(png_ptr, "malformed height string in sCAL chunk");
      return;
1810
   }
1811 1812
#else
#ifdef PNG_FIXED_POINT_SUPPORTED
1813
   sheight = (png_charp)png_malloc_warn(png_ptr, png_strlen(ep) + 1);
1814
   if (sheight == NULL)
1815 1816 1817 1818 1819
   {
      png_warning(png_ptr, "Out of memory while processing sCAL chunk height");
      return;
   }
   png_memcpy(sheight, ep, png_strlen(ep));
1820 1821
#endif
#endif
1822

1823
   if (png_ptr->chunkdata + slength < ep
1824 1825 1826 1827 1828 1829
#ifdef PNG_FLOATING_POINT_SUPPORTED
      || width <= 0. || height <= 0.
#endif
      )
   {
      png_warning(png_ptr, "Invalid sCAL data");
1830 1831
      png_free(png_ptr, png_ptr->chunkdata);
      png_ptr->chunkdata = NULL;
1832
#if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
1833 1834
      png_free(png_ptr, swidth);
      png_free(png_ptr, sheight);
1835
#endif
1836 1837 1838 1839 1840
      return;
   }


#ifdef PNG_FLOATING_POINT_SUPPORTED
1841
   png_set_sCAL(png_ptr, info_ptr, png_ptr->chunkdata[0], width, height);
1842
#else
1843
#ifdef PNG_FIXED_POINT_SUPPORTED
1844
   png_set_sCAL_s(png_ptr, info_ptr, png_ptr->chunkdata[0], swidth, sheight);
1845
#endif
1846 1847
#endif

1848 1849
   png_free(png_ptr, png_ptr->chunkdata);
   png_ptr->chunkdata = NULL;
1850 1851 1852 1853
#if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
   png_free(png_ptr, swidth);
   png_free(png_ptr, sheight);
#endif
1854 1855 1856
}
#endif

G
Guy Schalnat 已提交
1857
#if defined(PNG_READ_tIME_SUPPORTED)
1858
void /* PRIVATE */
A
Andreas Dilger 已提交
1859
png_handle_tIME(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
G
Guy Schalnat 已提交
1860 1861 1862 1863
{
   png_byte buf[7];
   png_time mod_time;

A
Andreas Dilger 已提交
1864 1865
   png_debug(1, "in png_handle_tIME\n");

G
Guy Schalnat 已提交
1866
   if (!(png_ptr->mode & PNG_HAVE_IHDR))
A
Andreas Dilger 已提交
1867
      png_error(png_ptr, "Out of place tIME chunk");
1868
   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME))
A
Andreas Dilger 已提交
1869 1870 1871 1872 1873 1874 1875 1876
   {
      png_warning(png_ptr, "Duplicate tIME chunk");
      png_crc_finish(png_ptr, length);
      return;
   }

   if (png_ptr->mode & PNG_HAVE_IDAT)
      png_ptr->mode |= PNG_AFTER_IDAT;
G
Guy Schalnat 已提交
1877

G
Guy Schalnat 已提交
1878 1879
   if (length != 7)
   {
G
Guy Schalnat 已提交
1880
      png_warning(png_ptr, "Incorrect tIME chunk length");
A
Andreas Dilger 已提交
1881
      png_crc_finish(png_ptr, length);
G
Guy Schalnat 已提交
1882 1883 1884 1885
      return;
   }

   png_crc_read(png_ptr, buf, 7);
A
Andreas Dilger 已提交
1886 1887
   if (png_crc_finish(png_ptr, 0))
      return;
G
Guy Schalnat 已提交
1888 1889 1890 1891 1892 1893

   mod_time.second = buf[6];
   mod_time.minute = buf[5];
   mod_time.hour = buf[4];
   mod_time.day = buf[3];
   mod_time.month = buf[2];
G
Guy Schalnat 已提交
1894
   mod_time.year = png_get_uint_16(buf);
G
Guy Schalnat 已提交
1895

A
Andreas Dilger 已提交
1896
   png_set_tIME(png_ptr, info_ptr, &mod_time);
G
Guy Schalnat 已提交
1897
}
G
Guy Schalnat 已提交
1898
#endif
G
Guy Schalnat 已提交
1899

G
Guy Schalnat 已提交
1900
#if defined(PNG_READ_tEXt_SUPPORTED)
A
Andreas Dilger 已提交
1901
/* Note: this does not properly handle chunks that are > 64K under DOS */
1902
void /* PRIVATE */
A
Andreas Dilger 已提交
1903
png_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
G
Guy Schalnat 已提交
1904
{
A
Andreas Dilger 已提交
1905
   png_textp text_ptr;
G
Guy Schalnat 已提交
1906
   png_charp key;
G
Guy Schalnat 已提交
1907
   png_charp text;
A
Andreas Dilger 已提交
1908
   png_uint_32 skip = 0;
1909
   png_size_t slength;
1910
   int ret;
A
Andreas Dilger 已提交
1911 1912

   png_debug(1, "in png_handle_tEXt\n");
G
Guy Schalnat 已提交
1913

G
Guy Schalnat 已提交
1914 1915 1916
   if (!(png_ptr->mode & PNG_HAVE_IHDR))
      png_error(png_ptr, "Missing IHDR before tEXt");

A
Andreas Dilger 已提交
1917 1918 1919
   if (png_ptr->mode & PNG_HAVE_IDAT)
      png_ptr->mode |= PNG_AFTER_IDAT;

A
Andreas Dilger 已提交
1920
#ifdef PNG_MAX_MALLOC_64K
1921
   if (length > (png_uint_32)65535L)
A
Andreas Dilger 已提交
1922 1923
   {
      png_warning(png_ptr, "tEXt chunk too large to fit in memory");
1924 1925
      skip = length - (png_uint_32)65535L;
      length = (png_uint_32)65535L;
A
Andreas Dilger 已提交
1926 1927 1928
   }
#endif

1929 1930 1931
   key = (png_charp)png_malloc_warn(png_ptr, length + 1);
   if (key == NULL)
   {
1932
     png_warning(png_ptr, "No memory to process text chunk");
1933 1934
     return;
   }
1935
   slength = (png_size_t)length;
1936
   png_crc_read(png_ptr, (png_bytep)key, slength);
A
Andreas Dilger 已提交
1937 1938

   if (png_crc_finish(png_ptr, skip))
A
Andreas Dilger 已提交
1939 1940 1941 1942 1943
   {
      png_free(png_ptr, key);
      return;
   }

1944
   key[slength] = 0x00;
G
Guy Schalnat 已提交
1945 1946

   for (text = key; *text; text++)
A
Andreas Dilger 已提交
1947
      /* empty loop to find end of key */ ;
G
Guy Schalnat 已提交
1948

1949
   if (text != key + slength)
G
Guy Schalnat 已提交
1950 1951
      text++;

1952 1953
   text_ptr = (png_textp)png_malloc_warn(png_ptr,
      png_sizeof(png_text));
1954 1955
   if (text_ptr == NULL)
   {
1956
     png_warning(png_ptr, "Not enough memory to process text chunk");
1957 1958 1959
     png_free(png_ptr, key);
     return;
   }
A
Andreas Dilger 已提交
1960 1961
   text_ptr->compression = PNG_TEXT_COMPRESSION_NONE;
   text_ptr->key = key;
1962
#ifdef PNG_iTXt_SUPPORTED
1963 1964
   text_ptr->lang = NULL;
   text_ptr->lang_key = NULL;
1965 1966
   text_ptr->itxt_length = 0;
#endif
A
Andreas Dilger 已提交
1967
   text_ptr->text = text;
1968
   text_ptr->text_length = png_strlen(text);
A
Andreas Dilger 已提交
1969

1970
   ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
A
Andreas Dilger 已提交
1971

1972
   png_free(png_ptr, key);
1973
   png_free(png_ptr, text_ptr);
1974
   if (ret)
1975
     png_warning(png_ptr, "Insufficient memory to process text chunk");
G
Guy Schalnat 已提交
1976
}
G
Guy Schalnat 已提交
1977
#endif
G
Guy Schalnat 已提交
1978

G
Guy Schalnat 已提交
1979
#if defined(PNG_READ_zTXt_SUPPORTED)
A
Andreas Dilger 已提交
1980
/* note: this does not correctly handle chunks that are > 64K under DOS */
1981
void /* PRIVATE */
A
Andreas Dilger 已提交
1982
png_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
G
Guy Schalnat 已提交
1983
{
A
Andreas Dilger 已提交
1984
   png_textp text_ptr;
G
Guy Schalnat 已提交
1985
   png_charp text;
1986
   int comp_type;
1987
   int ret;
1988
   png_size_t slength, prefix_len, data_len;
A
Andreas Dilger 已提交
1989 1990

   png_debug(1, "in png_handle_zTXt\n");
G
Guy Schalnat 已提交
1991 1992 1993
   if (!(png_ptr->mode & PNG_HAVE_IHDR))
      png_error(png_ptr, "Missing IHDR before zTXt");

A
Andreas Dilger 已提交
1994 1995 1996
   if (png_ptr->mode & PNG_HAVE_IDAT)
      png_ptr->mode |= PNG_AFTER_IDAT;

A
Andreas Dilger 已提交
1997 1998 1999
#ifdef PNG_MAX_MALLOC_64K
   /* We will no doubt have problems with chunks even half this size, but
      there is no hard and fast rule to tell us where to stop. */
2000
   if (length > (png_uint_32)65535L)
A
Andreas Dilger 已提交
2001
   {
2002
     png_warning(png_ptr, "zTXt chunk too large to fit in memory");
A
Andreas Dilger 已提交
2003 2004 2005 2006 2007
     png_crc_finish(png_ptr, length);
     return;
   }
#endif

2008 2009 2010
   png_free(png_ptr,png_ptr->chunkdata);
   png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
   if (png_ptr->chunkdata == NULL)
2011
   {
2012
     png_warning(png_ptr, "Out of memory processing zTXt chunk");
2013 2014 2015
     return;
   }
   slength = (png_size_t)length;
2016
   png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
A
Andreas Dilger 已提交
2017 2018
   if (png_crc_finish(png_ptr, 0))
   {
2019 2020
      png_free(png_ptr, png_ptr->chunkdata);
      png_ptr->chunkdata = NULL;
A
Andreas Dilger 已提交
2021 2022 2023
      return;
   }

2024
   png_ptr->chunkdata[slength] = 0x00;
G
Guy Schalnat 已提交
2025

2026
   for (text = png_ptr->chunkdata; *text; text++)
G
Guy Schalnat 已提交
2027 2028
      /* empty loop */ ;

2029
   /* zTXt must have some text after the chunkdataword */
2030
   if (text >= png_ptr->chunkdata + slength - 2)
G
Guy Schalnat 已提交
2031
   {
2032
      png_warning(png_ptr, "Truncated zTXt chunk");
2033 2034
      png_free(png_ptr, png_ptr->chunkdata);
      png_ptr->chunkdata = NULL;
2035
      return;
G
Guy Schalnat 已提交
2036
   }
2037
   else
G
Guy Schalnat 已提交
2038
   {
2039
       comp_type = *(++text);
2040 2041 2042 2043 2044
       if (comp_type != PNG_TEXT_COMPRESSION_zTXt)
       {
          png_warning(png_ptr, "Unknown compression type in zTXt chunk");
          comp_type = PNG_TEXT_COMPRESSION_zTXt;
       }
2045
       text++;        /* skip the compression_method byte */
2046
   }
2047
   prefix_len = text - png_ptr->chunkdata;
G
Guy Schalnat 已提交
2048

2049 2050
   png_decompress_chunk(png_ptr, comp_type,
     (png_size_t)length, prefix_len, &data_len);
G
Guy Schalnat 已提交
2051

2052 2053
   text_ptr = (png_textp)png_malloc_warn(png_ptr,
      png_sizeof(png_text));
2054 2055
   if (text_ptr == NULL)
   {
2056
     png_warning(png_ptr, "Not enough memory to process zTXt chunk");
2057 2058
     png_free(png_ptr, png_ptr->chunkdata);
     png_ptr->chunkdata = NULL;
2059 2060
     return;
   }
2061
   text_ptr->compression = comp_type;
2062
   text_ptr->key = png_ptr->chunkdata;
2063
#ifdef PNG_iTXt_SUPPORTED
2064 2065 2066
   text_ptr->lang = NULL;
   text_ptr->lang_key = NULL;
   text_ptr->itxt_length = 0;
2067
#endif
2068
   text_ptr->text = png_ptr->chunkdata + prefix_len;
2069
   text_ptr->text_length = data_len;
G
Guy Schalnat 已提交
2070

2071
   ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
G
Guy Schalnat 已提交
2072

2073
   png_free(png_ptr, text_ptr);
2074 2075
   png_free(png_ptr, png_ptr->chunkdata);
   png_ptr->chunkdata = NULL;
2076
   if (ret)
2077
     png_error(png_ptr, "Insufficient memory to store zTXt chunk");
2078 2079
}
#endif
G
Guy Schalnat 已提交
2080

2081 2082
#if defined(PNG_READ_iTXt_SUPPORTED)
/* note: this does not correctly handle chunks that are > 64K under DOS */
2083
void /* PRIVATE */
2084 2085 2086
png_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
{
   png_textp text_ptr;
2087
   png_charp key, lang, text, lang_key;
2088
   int comp_flag;
2089
   int comp_type = 0;
2090
   int ret;
2091
   png_size_t slength, prefix_len, data_len;
G
Guy Schalnat 已提交
2092

2093
   png_debug(1, "in png_handle_iTXt\n");
G
Guy Schalnat 已提交
2094

2095 2096
   if (!(png_ptr->mode & PNG_HAVE_IHDR))
      png_error(png_ptr, "Missing IHDR before iTXt");
G
Guy Schalnat 已提交
2097

2098 2099
   if (png_ptr->mode & PNG_HAVE_IDAT)
      png_ptr->mode |= PNG_AFTER_IDAT;
G
Guy Schalnat 已提交
2100

2101 2102 2103 2104 2105
#ifdef PNG_MAX_MALLOC_64K
   /* We will no doubt have problems with chunks even half this size, but
      there is no hard and fast rule to tell us where to stop. */
   if (length > (png_uint_32)65535L)
   {
2106
     png_warning(png_ptr, "iTXt chunk too large to fit in memory");
2107 2108
     png_crc_finish(png_ptr, length);
     return;
G
Guy Schalnat 已提交
2109
   }
2110 2111
#endif

2112 2113 2114
   png_free(png_ptr, png_ptr->chunkdata);
   png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
   if (png_ptr->chunkdata == NULL)
2115
   {
2116
     png_warning(png_ptr, "No memory to process iTXt chunk");
2117 2118
     return;
   }
2119
   slength = (png_size_t)length;
2120
   png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
2121
   if (png_crc_finish(png_ptr, 0))
A
Andreas Dilger 已提交
2122
   {
2123 2124
      png_free(png_ptr, png_ptr->chunkdata);
      png_ptr->chunkdata = NULL;
2125 2126
      return;
   }
A
Andreas Dilger 已提交
2127

2128
   png_ptr->chunkdata[slength] = 0x00;
A
Andreas Dilger 已提交
2129

2130
   for (lang = png_ptr->chunkdata; *lang; lang++)
2131 2132 2133
      /* empty loop */ ;
   lang++;        /* skip NUL separator */

2134 2135 2136 2137
   /* iTXt must have a language tag (possibly empty), two compression bytes,
      translated keyword (possibly empty), and possibly some text after the
      keyword */

2138
   if (lang >= png_ptr->chunkdata + slength - 3)
2139
   {
2140
      png_warning(png_ptr, "Truncated iTXt chunk");
2141 2142
      png_free(png_ptr, png_ptr->chunkdata);
      png_ptr->chunkdata = NULL;
2143
      return;
A
Andreas Dilger 已提交
2144
   }
2145 2146 2147 2148 2149 2150
   else
   {
       comp_flag = *lang++;
       comp_type = *lang++;
   }

2151 2152 2153 2154
   for (lang_key = lang; *lang_key; lang_key++)
      /* empty loop */ ;
   lang_key++;        /* skip NUL separator */

2155
   if (lang_key >= png_ptr->chunkdata + slength)
2156 2157
   {
      png_warning(png_ptr, "Truncated iTXt chunk");
2158 2159
      png_free(png_ptr, png_ptr->chunkdata);
      png_ptr->chunkdata = NULL;
2160 2161 2162
      return;
   }

2163
   for (text = lang_key; *text; text++)
2164 2165
      /* empty loop */ ;
   text++;        /* skip NUL separator */
2166
   if (text >= png_ptr->chunkdata + slength)
2167 2168
   {
      png_warning(png_ptr, "Malformed iTXt chunk");
2169 2170
      png_free(png_ptr, png_ptr->chunkdata);
      png_ptr->chunkdata = NULL;
2171 2172
      return;
   }
2173

2174
   prefix_len = text - png_ptr->chunkdata;
2175

2176
   key=png_ptr->chunkdata;
2177
   if (comp_flag)
2178 2179
       png_decompress_chunk(png_ptr, comp_type,
         (size_t)length, prefix_len, &data_len);
2180
   else
2181
       data_len = png_strlen(png_ptr->chunkdata + prefix_len);
2182 2183
   text_ptr = (png_textp)png_malloc_warn(png_ptr,
      png_sizeof(png_text));
2184 2185
   if (text_ptr == NULL)
   {
2186
     png_warning(png_ptr, "Not enough memory to process iTXt chunk");
2187 2188
     png_free(png_ptr, png_ptr->chunkdata);
     png_ptr->chunkdata = NULL;
2189 2190
     return;
   }
2191
   text_ptr->compression = (int)comp_flag + 1;
2192 2193
   text_ptr->lang_key = png_ptr->chunkdata + (lang_key - key);
   text_ptr->lang = png_ptr->chunkdata + (lang - key);
2194 2195
   text_ptr->itxt_length = data_len;
   text_ptr->text_length = 0;
2196 2197
   text_ptr->key = png_ptr->chunkdata;
   text_ptr->text = png_ptr->chunkdata + prefix_len;
G
Guy Schalnat 已提交
2198

2199
   ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
A
Andreas Dilger 已提交
2200

2201
   png_free(png_ptr, text_ptr);
2202 2203
   png_free(png_ptr, png_ptr->chunkdata);
   png_ptr->chunkdata = NULL;
2204
   if (ret)
2205
     png_error(png_ptr, "Insufficient memory to store iTXt chunk");
2206 2207 2208
}
#endif

A
Andreas Dilger 已提交
2209 2210
/* This function is called when we haven't found a handler for a
   chunk.  If there isn't a problem with the chunk itself (ie bad
2211 2212 2213
   chunk name, CRC, or a critical chunk), the chunk is silently ignored
   -- unless the PNG_FLAG_UNKNOWN_CHUNKS_SUPPORTED flag is on in which
   case it will be saved away to be written out later. */
2214
void /* PRIVATE */
A
Andreas Dilger 已提交
2215 2216
png_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
{
2217 2218
   png_uint_32 skip = 0;

A
Andreas Dilger 已提交
2219 2220
   png_debug(1, "in png_handle_unknown\n");

2221
   if (png_ptr->mode & PNG_HAVE_IDAT)
2222 2223
   {
#ifdef PNG_USE_LOCAL_ARRAYS
2224
      PNG_CONST PNG_IDAT;
2225 2226 2227 2228
#endif
      if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))  /* not an IDAT */
         png_ptr->mode |= PNG_AFTER_IDAT;
   }
2229

A
Andreas Dilger 已提交
2230 2231
   if (!(png_ptr->chunk_name[0] & 0x20))
   {
2232
#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
2233
      if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
2234
           PNG_HANDLE_CHUNK_ALWAYS
2235
#if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
2236
           && png_ptr->read_user_chunk_fn == NULL
2237 2238
#endif
        )
2239
#endif
2240
          png_chunk_error(png_ptr, "unknown critical chunk");
A
Andreas Dilger 已提交
2241 2242
   }

2243
#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
2244 2245
   if ((png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS) ||
       (png_ptr->read_user_chunk_fn != NULL))
2246 2247 2248 2249 2250 2251 2252 2253 2254
   {
#ifdef PNG_MAX_MALLOC_64K
       if (length > (png_uint_32)65535L)
       {
           png_warning(png_ptr, "unknown chunk too large to fit in memory");
           skip = length - (png_uint_32)65535L;
           length = (png_uint_32)65535L;
       }
#endif
2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266
       png_memcpy((png_charp)png_ptr->unknown_chunk.name,
                  (png_charp)png_ptr->chunk_name, 
                  png_sizeof(png_ptr->unknown_chunk.name));
       png_ptr->unknown_chunk.name[png_sizeof(png_ptr->unknown_chunk.name)-1] = '\0';
       png_ptr->unknown_chunk.size = (png_size_t)length;
       if (length == 0)
         png_ptr->unknown_chunk.data = NULL;
       else
       {
         png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr, length);
         png_crc_read(png_ptr, (png_bytep)png_ptr->unknown_chunk.data, length);
       }
2267
#if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
2268
       if (png_ptr->read_user_chunk_fn != NULL)
2269 2270
       {
          /* callback to user unknown chunk handler */
2271 2272 2273 2274 2275 2276
          int ret;
          ret = (*(png_ptr->read_user_chunk_fn))
            (png_ptr, &png_ptr->unknown_chunk);
          if (ret < 0)
             png_chunk_error(png_ptr, "error in user chunk");
          if (ret == 0)
2277 2278
          {
             if (!(png_ptr->chunk_name[0] & 0x20))
2279
                if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
2280
                     PNG_HANDLE_CHUNK_ALWAYS)
2281
                   png_chunk_error(png_ptr, "unknown critical chunk");
2282 2283
             png_set_unknown_chunks(png_ptr, info_ptr,
               &png_ptr->unknown_chunk, 1);
2284 2285 2286 2287
          }
       }
       else
#endif
2288 2289 2290
       png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1);
       png_free(png_ptr, png_ptr->unknown_chunk.data);
       png_ptr->unknown_chunk.data = NULL;
2291 2292 2293
   }
   else
#endif
2294
      skip = length;
2295

2296
   png_crc_finish(png_ptr, skip);
2297 2298

#if !defined(PNG_READ_USER_CHUNKS_SUPPORTED)
2299
   info_ptr = info_ptr; /* quiet compiler warnings about unused info_ptr */
2300
#endif
A
Andreas Dilger 已提交
2301 2302 2303 2304
}

/* This function is called to verify that a chunk name is valid.
   This function can't have the "critical chunk check" incorporated
A
Andreas Dilger 已提交
2305
   into it, since in the future we will need to be able to call user
A
Andreas Dilger 已提交
2306 2307
   functions to handle unknown critical chunks after we check that
   the chunk name itself is valid. */
A
Andreas Dilger 已提交
2308

2309
#define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
A
Andreas Dilger 已提交
2310

2311
void /* PRIVATE */
A
Andreas Dilger 已提交
2312 2313
png_check_chunk_name(png_structp png_ptr, png_bytep chunk_name)
{
A
Andreas Dilger 已提交
2314 2315
   png_debug(1, "in png_check_chunk_name\n");
   if (isnonalpha(chunk_name[0]) || isnonalpha(chunk_name[1]) ||
2316
       isnonalpha(chunk_name[2]) || isnonalpha(chunk_name[3]))
A
Andreas Dilger 已提交
2317
   {
2318
      png_chunk_error(png_ptr, "invalid chunk type");
A
Andreas Dilger 已提交
2319 2320 2321
   }
}

2322 2323
/* Combines the row recently read in with the existing pixels in the
   row.  This routine takes care of alpha and transparency if requested.
G
Guy Schalnat 已提交
2324 2325 2326 2327
   This routine also handles the two methods of progressive display
   of interlaced images, depending on the mask value.
   The mask value describes which pixels are to be combined with
   the row.  The pattern always repeats every 8 pixels, so just 8
2328
   bits are needed.  A one indicates the pixel is to be combined,
G
Guy Schalnat 已提交
2329 2330
   a zero indicates the pixel is to be skipped.  This is in addition
   to any alpha or transparency value associated with the pixel.  If
A
Andreas Dilger 已提交
2331
   you want all pixels to be combined, pass 0xff (255) in mask.  */
2332

2333
void /* PRIVATE */
2334
png_combine_row(png_structp png_ptr, png_bytep row, int mask)
G
Guy Schalnat 已提交
2335
{
2336
   png_debug(1, "in png_combine_row\n");
G
Guy Schalnat 已提交
2337 2338
   if (mask == 0xff)
   {
G
Guy Schalnat 已提交
2339
      png_memcpy(row, png_ptr->row_buf + 1,
2340
         PNG_ROWBYTES(png_ptr->row_info.pixel_depth, png_ptr->width));
G
Guy Schalnat 已提交
2341 2342 2343 2344 2345 2346 2347
   }
   else
   {
      switch (png_ptr->row_info.pixel_depth)
      {
         case 1:
         {
2348 2349
            png_bytep sp = png_ptr->row_buf + 1;
            png_bytep dp = row;
2350 2351 2352
            int s_inc, s_start, s_end;
            int m = 0x80;
            int shift;
2353
            png_uint_32 i;
2354
            png_uint_32 row_width = png_ptr->width;
G
Guy Schalnat 已提交
2355

A
Andreas Dilger 已提交
2356 2357
#if defined(PNG_READ_PACKSWAP_SUPPORTED)
            if (png_ptr->transformations & PNG_PACKSWAP)
2358 2359 2360 2361 2362 2363
            {
                s_start = 0;
                s_end = 7;
                s_inc = 1;
            }
            else
A
Andreas Dilger 已提交
2364 2365
#endif
            {
2366 2367 2368
                s_start = 7;
                s_end = 0;
                s_inc = -1;
A
Andreas Dilger 已提交
2369
            }
2370 2371 2372 2373

            shift = s_start;

            for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
2374
            {
2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396
               if (m & mask)
               {
                  int value;

                  value = (*sp >> shift) & 0x01;
                  *dp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
                  *dp |= (png_byte)(value << shift);
               }

               if (shift == s_end)
               {
                  shift = s_start;
                  sp++;
                  dp++;
               }
               else
                  shift += s_inc;

               if (m == 1)
                  m = 0x80;
               else
                  m >>= 1;
G
Guy Schalnat 已提交
2397 2398 2399 2400 2401
            }
            break;
         }
         case 2:
         {
2402 2403
            png_bytep sp = png_ptr->row_buf + 1;
            png_bytep dp = row;
2404
            int s_start, s_end, s_inc;
2405
            int m = 0x80;
2406 2407 2408 2409
            int shift;
            png_uint_32 i;
            png_uint_32 row_width = png_ptr->width;
            int value;
G
Guy Schalnat 已提交
2410

A
Andreas Dilger 已提交
2411 2412 2413
#if defined(PNG_READ_PACKSWAP_SUPPORTED)
            if (png_ptr->transformations & PNG_PACKSWAP)
            {
2414 2415 2416
               s_start = 0;
               s_end = 6;
               s_inc = 2;
A
Andreas Dilger 已提交
2417
            }
2418 2419
            else
#endif
A
Andreas Dilger 已提交
2420
            {
2421 2422 2423
               s_start = 6;
               s_end = 0;
               s_inc = -2;
A
Andreas Dilger 已提交
2424
            }
2425 2426 2427 2428

            shift = s_start;

            for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
2429
            {
2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448
               if (m & mask)
               {
                  value = (*sp >> shift) & 0x03;
                  *dp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
                  *dp |= (png_byte)(value << shift);
               }

               if (shift == s_end)
               {
                  shift = s_start;
                  sp++;
                  dp++;
               }
               else
                  shift += s_inc;
               if (m == 1)
                  m = 0x80;
               else
                  m >>= 1;
G
Guy Schalnat 已提交
2449 2450 2451 2452 2453
            }
            break;
         }
         case 4:
         {
2454 2455
            png_bytep sp = png_ptr->row_buf + 1;
            png_bytep dp = row;
2456
            int s_start, s_end, s_inc;
2457
            int m = 0x80;
2458 2459 2460 2461
            int shift;
            png_uint_32 i;
            png_uint_32 row_width = png_ptr->width;
            int value;
G
Guy Schalnat 已提交
2462

A
Andreas Dilger 已提交
2463 2464 2465
#if defined(PNG_READ_PACKSWAP_SUPPORTED)
            if (png_ptr->transformations & PNG_PACKSWAP)
            {
2466 2467 2468
               s_start = 0;
               s_end = 4;
               s_inc = 4;
A
Andreas Dilger 已提交
2469
            }
2470 2471
            else
#endif
A
Andreas Dilger 已提交
2472
            {
2473 2474 2475
               s_start = 4;
               s_end = 0;
               s_inc = -4;
A
Andreas Dilger 已提交
2476
            }
2477 2478 2479
            shift = s_start;

            for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
2480
            {
2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499
               if (m & mask)
               {
                  value = (*sp >> shift) & 0xf;
                  *dp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
                  *dp |= (png_byte)(value << shift);
               }

               if (shift == s_end)
               {
                  shift = s_start;
                  sp++;
                  dp++;
               }
               else
                  shift += s_inc;
               if (m == 1)
                  m = 0x80;
               else
                  m >>= 1;
G
Guy Schalnat 已提交
2500 2501 2502 2503 2504
            }
            break;
         }
         default:
         {
2505 2506 2507 2508
            png_bytep sp = png_ptr->row_buf + 1;
            png_bytep dp = row;
            png_size_t pixel_bytes = (png_ptr->row_info.pixel_depth >> 3);
            png_uint_32 i;
2509 2510 2511
            png_uint_32 row_width = png_ptr->width;
            png_byte m = 0x80;

2512

2513
            for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
2514
            {
2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526
               if (m & mask)
               {
                  png_memcpy(dp, sp, pixel_bytes);
               }

               sp += pixel_bytes;
               dp += pixel_bytes;

               if (m == 1)
                  m = 0x80;
               else
                  m >>= 1;
G
Guy Schalnat 已提交
2527 2528 2529 2530 2531 2532 2533
            }
            break;
         }
      }
   }
}

2534
#ifdef PNG_READ_INTERLACING_SUPPORTED
2535 2536 2537 2538
/* OLD pre-1.0.9 interface:
void png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
   png_uint_32 transformations)
 */
2539
void /* PRIVATE */
2540
png_do_read_interlace(png_structp png_ptr)
G
Guy Schalnat 已提交
2541
{
2542 2543 2544 2545
   png_row_infop row_info = &(png_ptr->row_info);
   png_bytep row = png_ptr->row_buf + 1;
   int pass = png_ptr->pass;
   png_uint_32 transformations = png_ptr->transformations;
2546
#ifdef PNG_USE_LOCAL_ARRAYS
2547 2548
   /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
   /* offset to next interlace block */
2549
   PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
2550
#endif
2551

2552
   png_debug(1, "in png_do_read_interlace\n");
2553
   if (row != NULL && row_info != NULL)
G
Guy Schalnat 已提交
2554
   {
2555 2556 2557
      png_uint_32 final_width;

      final_width = row_info->width * png_pass_inc[pass];
G
Guy Schalnat 已提交
2558 2559 2560 2561 2562

      switch (row_info->pixel_depth)
      {
         case 1:
         {
2563 2564
            png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3);
            png_bytep dp = row + (png_size_t)((final_width - 1) >> 3);
2565 2566 2567 2568
            int sshift, dshift;
            int s_start, s_end, s_inc;
            int jstop = png_pass_inc[pass];
            png_byte v;
G
Guy Schalnat 已提交
2569
            png_uint_32 i;
2570
            int j;
G
Guy Schalnat 已提交
2571

A
Andreas Dilger 已提交
2572 2573 2574
#if defined(PNG_READ_PACKSWAP_SUPPORTED)
            if (transformations & PNG_PACKSWAP)
            {
2575 2576 2577 2578 2579
                sshift = (int)((row_info->width + 7) & 0x07);
                dshift = (int)((final_width + 7) & 0x07);
                s_start = 7;
                s_end = 0;
                s_inc = -1;
A
Andreas Dilger 已提交
2580
            }
2581
            else
A
Andreas Dilger 已提交
2582
#endif
G
Guy Schalnat 已提交
2583
            {
2584 2585 2586 2587 2588 2589
                sshift = 7 - (int)((row_info->width + 7) & 0x07);
                dshift = 7 - (int)((final_width + 7) & 0x07);
                s_start = 0;
                s_end = 7;
                s_inc = 1;
            }
2590

2591 2592 2593 2594
            for (i = 0; i < row_info->width; i++)
            {
               v = (png_byte)((*sp >> sshift) & 0x01);
               for (j = 0; j < jstop; j++)
G
Guy Schalnat 已提交
2595
               {
2596 2597 2598 2599 2600 2601 2602 2603 2604
                  *dp &= (png_byte)((0x7f7f >> (7 - dshift)) & 0xff);
                  *dp |= (png_byte)(v << dshift);
                  if (dshift == s_end)
                  {
                     dshift = s_start;
                     dp--;
                  }
                  else
                     dshift += s_inc;
G
Guy Schalnat 已提交
2605
               }
2606
               if (sshift == s_end)
G
Guy Schalnat 已提交
2607
               {
2608
                  sshift = s_start;
G
Guy Schalnat 已提交
2609 2610
                  sp--;
               }
2611 2612
               else
                  sshift += s_inc;
G
Guy Schalnat 已提交
2613 2614 2615 2616 2617
            }
            break;
         }
         case 2:
         {
2618 2619 2620 2621 2622
            png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);
            png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);
            int sshift, dshift;
            int s_start, s_end, s_inc;
            int jstop = png_pass_inc[pass];
A
Andreas Dilger 已提交
2623
            png_uint_32 i;
G
Guy Schalnat 已提交
2624

A
Andreas Dilger 已提交
2625 2626 2627
#if defined(PNG_READ_PACKSWAP_SUPPORTED)
            if (transformations & PNG_PACKSWAP)
            {
2628 2629 2630 2631 2632
               sshift = (int)(((row_info->width + 3) & 0x03) << 1);
               dshift = (int)(((final_width + 3) & 0x03) << 1);
               s_start = 6;
               s_end = 0;
               s_inc = -2;
A
Andreas Dilger 已提交
2633
            }
2634
            else
A
Andreas Dilger 已提交
2635
#endif
2636 2637 2638 2639 2640 2641 2642
            {
               sshift = (int)((3 - ((row_info->width + 3) & 0x03)) << 1);
               dshift = (int)((3 - ((final_width + 3) & 0x03)) << 1);
               s_start = 0;
               s_end = 6;
               s_inc = 2;
            }
A
Andreas Dilger 已提交
2643

2644
            for (i = 0; i < row_info->width; i++)
G
Guy Schalnat 已提交
2645
            {
2646 2647
               png_byte v;
               int j;
A
Andreas Dilger 已提交
2648

2649 2650
               v = (png_byte)((*sp >> sshift) & 0x03);
               for (j = 0; j < jstop; j++)
G
Guy Schalnat 已提交
2651
               {
2652 2653 2654 2655 2656 2657 2658 2659 2660
                  *dp &= (png_byte)((0x3f3f >> (6 - dshift)) & 0xff);
                  *dp |= (png_byte)(v << dshift);
                  if (dshift == s_end)
                  {
                     dshift = s_start;
                     dp--;
                  }
                  else
                     dshift += s_inc;
G
Guy Schalnat 已提交
2661
               }
2662
               if (sshift == s_end)
G
Guy Schalnat 已提交
2663
               {
2664
                  sshift = s_start;
G
Guy Schalnat 已提交
2665 2666
                  sp--;
               }
2667 2668
               else
                  sshift += s_inc;
G
Guy Schalnat 已提交
2669 2670 2671 2672 2673
            }
            break;
         }
         case 4:
         {
2674 2675
            png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1);
            png_bytep dp = row + (png_size_t)((final_width - 1) >> 1);
2676 2677
            int sshift, dshift;
            int s_start, s_end, s_inc;
G
Guy Schalnat 已提交
2678
            png_uint_32 i;
2679
            int jstop = png_pass_inc[pass];
G
Guy Schalnat 已提交
2680

A
Andreas Dilger 已提交
2681 2682 2683
#if defined(PNG_READ_PACKSWAP_SUPPORTED)
            if (transformations & PNG_PACKSWAP)
            {
2684 2685 2686 2687 2688
               sshift = (int)(((row_info->width + 1) & 0x01) << 2);
               dshift = (int)(((final_width + 1) & 0x01) << 2);
               s_start = 4;
               s_end = 0;
               s_inc = -4;
A
Andreas Dilger 已提交
2689
            }
2690
            else
A
Andreas Dilger 已提交
2691 2692
#endif
            {
2693 2694 2695 2696 2697 2698
               sshift = (int)((1 - ((row_info->width + 1) & 0x01)) << 2);
               dshift = (int)((1 - ((final_width + 1) & 0x01)) << 2);
               s_start = 0;
               s_end = 4;
               s_inc = 4;
            }
A
Andreas Dilger 已提交
2699

2700 2701 2702 2703
            for (i = 0; i < row_info->width; i++)
            {
               png_byte v = (png_byte)((*sp >> sshift) & 0xf);
               int j;
A
Andreas Dilger 已提交
2704

2705
               for (j = 0; j < jstop; j++)
G
Guy Schalnat 已提交
2706
               {
2707 2708 2709 2710 2711 2712 2713 2714 2715
                  *dp &= (png_byte)((0xf0f >> (4 - dshift)) & 0xff);
                  *dp |= (png_byte)(v << dshift);
                  if (dshift == s_end)
                  {
                     dshift = s_start;
                     dp--;
                  }
                  else
                     dshift += s_inc;
G
Guy Schalnat 已提交
2716
               }
2717
               if (sshift == s_end)
G
Guy Schalnat 已提交
2718
               {
2719
                  sshift = s_start;
G
Guy Schalnat 已提交
2720 2721
                  sp--;
               }
2722 2723
               else
                  sshift += s_inc;
G
Guy Schalnat 已提交
2724 2725 2726 2727 2728
            }
            break;
         }
         default:
         {
2729 2730
            png_size_t pixel_bytes = (row_info->pixel_depth >> 3);
            png_bytep sp = row + (png_size_t)(row_info->width - 1) * pixel_bytes;
2731 2732 2733
            png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes;

            int jstop = png_pass_inc[pass];
A
Andreas Dilger 已提交
2734
            png_uint_32 i;
G
Guy Schalnat 已提交
2735

2736
            for (i = 0; i < row_info->width; i++)
G
Guy Schalnat 已提交
2737
            {
2738 2739 2740 2741 2742 2743 2744 2745 2746 2747
               png_byte v[8];
               int j;

               png_memcpy(v, sp, pixel_bytes);
               for (j = 0; j < jstop; j++)
               {
                  png_memcpy(dp, v, pixel_bytes);
                  dp -= pixel_bytes;
               }
               sp -= pixel_bytes;
G
Guy Schalnat 已提交
2748 2749 2750 2751 2752
            }
            break;
         }
      }
      row_info->width = final_width;
2753
      row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width);
G
Guy Schalnat 已提交
2754
   }
2755
#if !defined(PNG_READ_PACKSWAP_SUPPORTED)
2756
   transformations = transformations; /* silence compiler warning */
2757
#endif
G
Guy Schalnat 已提交
2758
}
2759
#endif /* PNG_READ_INTERLACING_SUPPORTED */
G
Guy Schalnat 已提交
2760

2761
void /* PRIVATE */
2762
png_read_filter_row(png_structp png_ptr, png_row_infop row_info, png_bytep row,
2763 2764 2765
   png_bytep prev_row, int filter)
{
   png_debug(1, "in png_read_filter_row\n");
2766
   png_debug2(2, "row = %lu, filter = %d\n", png_ptr->row_number, filter);
2767 2768 2769 2770 2771 2772
   switch (filter)
   {
      case PNG_FILTER_VALUE_NONE:
         break;
      case PNG_FILTER_VALUE_SUB:
      {
2773 2774
         png_uint_32 i;
         png_uint_32 istop = row_info->rowbytes;
2775
         png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;
2776 2777
         png_bytep rp = row + bpp;
         png_bytep lp = row;
2778

2779
         for (i = bpp; i < istop; i++)
2780
         {
2781 2782
            *rp = (png_byte)(((int)(*rp) + (int)(*lp++)) & 0xff);
            rp++;
2783 2784 2785 2786 2787
         }
         break;
      }
      case PNG_FILTER_VALUE_UP:
      {
2788 2789 2790 2791
         png_uint_32 i;
         png_uint_32 istop = row_info->rowbytes;
         png_bytep rp = row;
         png_bytep pp = prev_row;
2792

2793
         for (i = 0; i < istop; i++)
2794
         {
2795 2796
            *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
            rp++;
2797 2798 2799 2800 2801
         }
         break;
      }
      case PNG_FILTER_VALUE_AVG:
      {
2802 2803 2804 2805
         png_uint_32 i;
         png_bytep rp = row;
         png_bytep pp = prev_row;
         png_bytep lp = row;
2806
         png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;
2807
         png_uint_32 istop = row_info->rowbytes - bpp;
2808

2809
         for (i = 0; i < bpp; i++)
2810
         {
2811
            *rp = (png_byte)(((int)(*rp) +
2812
               ((int)(*pp++) / 2 )) & 0xff);
2813
            rp++;
2814
         }
2815

2816
         for (i = 0; i < istop; i++)
2817
         {
2818
            *rp = (png_byte)(((int)(*rp) +
2819
               (int)(*pp++ + *lp++) / 2 ) & 0xff);
2820
            rp++;
2821 2822 2823 2824 2825
         }
         break;
      }
      case PNG_FILTER_VALUE_PAETH:
      {
2826 2827 2828 2829 2830
         png_uint_32 i;
         png_bytep rp = row;
         png_bytep pp = prev_row;
         png_bytep lp = row;
         png_bytep cp = prev_row;
2831
         png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;
2832
         png_uint_32 istop=row_info->rowbytes - bpp;
2833 2834

         for (i = 0; i < bpp; i++)
2835
         {
2836 2837
            *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
            rp++;
2838 2839
         }

2840
         for (i = 0; i < istop; i++)   /* use leftover rp,pp */
2841 2842 2843
         {
            int a, b, c, pa, pb, pc, p;

2844 2845 2846 2847 2848 2849
            a = *lp++;
            b = *pp++;
            c = *cp++;

            p = b - c;
            pc = a - c;
2850 2851

#ifdef PNG_USE_ABS
2852 2853 2854
            pa = abs(p);
            pb = abs(pc);
            pc = abs(p + pc);
2855
#else
2856 2857 2858
            pa = p < 0 ? -p : p;
            pb = pc < 0 ? -pc : pc;
            pc = (p + pc) < 0 ? -(p + pc) : p + pc;
2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869
#endif

            /*
               if (pa <= pb && pa <= pc)
                  p = a;
               else if (pb <= pc)
                  p = b;
               else
                  p = c;
             */

2870
            p = (pa <= pb && pa <= pc) ? a : (pb <= pc) ? b : c;
2871

2872 2873
            *rp = (png_byte)(((int)(*rp) + p) & 0xff);
            rp++;
2874 2875 2876 2877
         }
         break;
      }
      default:
2878
         png_warning(png_ptr, "Ignoring bad adaptive filter type");
2879
         *row = 0;
2880 2881 2882
         break;
   }
}
G
Guy Schalnat 已提交
2883

2884
void /* PRIVATE */
G
Guy Schalnat 已提交
2885
png_read_finish_row(png_structp png_ptr)
G
Guy Schalnat 已提交
2886
{
2887
#ifdef PNG_USE_LOCAL_ARRAYS
2888
#ifdef PNG_READ_INTERLACING_SUPPORTED
2889
   /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
2890

2891
   /* start of interlace block */
2892
   PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
2893

2894
   /* offset to next interlace block */
2895
   PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
2896

2897
   /* start of interlace block in the y direction */
2898
   PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
2899

2900
   /* offset to next interlace block in the y direction */
2901 2902
   PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
#endif /* PNG_READ_INTERLACING_SUPPORTED */
2903
#endif
2904

A
Andreas Dilger 已提交
2905
   png_debug(1, "in png_read_finish_row\n");
G
Guy Schalnat 已提交
2906 2907 2908 2909
   png_ptr->row_number++;
   if (png_ptr->row_number < png_ptr->num_rows)
      return;

2910
#ifdef PNG_READ_INTERLACING_SUPPORTED
G
Guy Schalnat 已提交
2911 2912 2913
   if (png_ptr->interlaced)
   {
      png_ptr->row_number = 0;
2914
      png_memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
G
Guy Schalnat 已提交
2915 2916 2917 2918 2919 2920 2921 2922 2923
      do
      {
         png_ptr->pass++;
         if (png_ptr->pass >= 7)
            break;
         png_ptr->iwidth = (png_ptr->width +
            png_pass_inc[png_ptr->pass] - 1 -
            png_pass_start[png_ptr->pass]) /
            png_pass_inc[png_ptr->pass];
2924 2925 2926

         png_ptr->irowbytes = PNG_ROWBYTES(png_ptr->pixel_depth,
            png_ptr->iwidth) + 1;
2927

G
Guy Schalnat 已提交
2928 2929 2930 2931 2932 2933 2934 2935 2936
         if (!(png_ptr->transformations & PNG_INTERLACE))
         {
            png_ptr->num_rows = (png_ptr->height +
               png_pass_yinc[png_ptr->pass] - 1 -
               png_pass_ystart[png_ptr->pass]) /
               png_pass_yinc[png_ptr->pass];
            if (!(png_ptr->num_rows))
               continue;
         }
2937
         else  /* if (png_ptr->transformations & PNG_INTERLACE) */
G
Guy Schalnat 已提交
2938
            break;
G
Guy Schalnat 已提交
2939 2940 2941 2942 2943
      } while (png_ptr->iwidth == 0);

      if (png_ptr->pass < 7)
         return;
   }
2944
#endif /* PNG_READ_INTERLACING_SUPPORTED */
G
Guy Schalnat 已提交
2945

G
Guy Schalnat 已提交
2946
   if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
G
Guy Schalnat 已提交
2947
   {
2948
#ifdef PNG_USE_LOCAL_ARRAYS
2949
      PNG_CONST PNG_IDAT;
2950
#endif
G
Guy Schalnat 已提交
2951 2952 2953
      char extra;
      int ret;

A
Andreas Dilger 已提交
2954 2955
      png_ptr->zstream.next_out = (Byte *)&extra;
      png_ptr->zstream.avail_out = (uInt)1;
2956
      for (;;)
G
Guy Schalnat 已提交
2957
      {
A
Andreas Dilger 已提交
2958
         if (!(png_ptr->zstream.avail_in))
G
Guy Schalnat 已提交
2959 2960 2961
         {
            while (!png_ptr->idat_size)
            {
A
Andreas Dilger 已提交
2962
               png_byte chunk_length[4];
G
Guy Schalnat 已提交
2963

A
Andreas Dilger 已提交
2964
               png_crc_finish(png_ptr, 0);
G
Guy Schalnat 已提交
2965

A
Andreas Dilger 已提交
2966
               png_read_data(png_ptr, chunk_length, 4);
2967
               png_ptr->idat_size = png_get_uint_31(png_ptr, chunk_length);
A
Andreas Dilger 已提交
2968 2969
               png_reset_crc(png_ptr);
               png_crc_read(png_ptr, png_ptr->chunk_name, 4);
2970
               if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
G
Guy Schalnat 已提交
2971
                  png_error(png_ptr, "Not enough image data");
G
Guy Schalnat 已提交
2972 2973

            }
A
Andreas Dilger 已提交
2974 2975
            png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
            png_ptr->zstream.next_in = png_ptr->zbuf;
G
Guy Schalnat 已提交
2976
            if (png_ptr->zbuf_size > png_ptr->idat_size)
A
Andreas Dilger 已提交
2977 2978 2979
               png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
            png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zstream.avail_in);
            png_ptr->idat_size -= png_ptr->zstream.avail_in;
G
Guy Schalnat 已提交
2980
         }
A
Andreas Dilger 已提交
2981
         ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
G
Guy Schalnat 已提交
2982 2983
         if (ret == Z_STREAM_END)
         {
A
Andreas Dilger 已提交
2984
            if (!(png_ptr->zstream.avail_out) || png_ptr->zstream.avail_in ||
G
Guy Schalnat 已提交
2985
               png_ptr->idat_size)
2986
               png_warning(png_ptr, "Extra compressed data");
A
Andreas Dilger 已提交
2987 2988
            png_ptr->mode |= PNG_AFTER_IDAT;
            png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
G
Guy Schalnat 已提交
2989 2990 2991
            break;
         }
         if (ret != Z_OK)
A
Andreas Dilger 已提交
2992
            png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
G
Guy Schalnat 已提交
2993
                      "Decompression Error");
G
Guy Schalnat 已提交
2994

A
Andreas Dilger 已提交
2995
         if (!(png_ptr->zstream.avail_out))
2996
         {
2997
            png_warning(png_ptr, "Extra compressed data");
2998 2999 3000 3001
            png_ptr->mode |= PNG_AFTER_IDAT;
            png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
            break;
         }
G
Guy Schalnat 已提交
3002

3003
      }
A
Andreas Dilger 已提交
3004
      png_ptr->zstream.avail_out = 0;
G
Guy Schalnat 已提交
3005 3006
   }

A
Andreas Dilger 已提交
3007
   if (png_ptr->idat_size || png_ptr->zstream.avail_in)
3008
      png_warning(png_ptr, "Extra compression data");
G
Guy Schalnat 已提交
3009

A
Andreas Dilger 已提交
3010
   inflateReset(&png_ptr->zstream);
G
Guy Schalnat 已提交
3011

A
Andreas Dilger 已提交
3012
   png_ptr->mode |= PNG_AFTER_IDAT;
G
Guy Schalnat 已提交
3013 3014
}

3015
void /* PRIVATE */
G
Guy Schalnat 已提交
3016
png_read_start_row(png_structp png_ptr)
G
Guy Schalnat 已提交
3017
{
3018
#ifdef PNG_USE_LOCAL_ARRAYS
3019
#ifdef PNG_READ_INTERLACING_SUPPORTED
3020
   /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
3021

3022
   /* start of interlace block */
3023
   PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
3024

3025
   /* offset to next interlace block */
3026
   PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
3027

3028
   /* start of interlace block in the y direction */
3029
   PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
3030

3031
   /* offset to next interlace block in the y direction */
3032 3033
   PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
#endif
3034
#endif
3035

G
Guy Schalnat 已提交
3036
   int max_pixel_depth;
3037
   png_size_t row_bytes;
G
Guy Schalnat 已提交
3038

A
Andreas Dilger 已提交
3039
   png_debug(1, "in png_read_start_row\n");
A
Andreas Dilger 已提交
3040
   png_ptr->zstream.avail_in = 0;
G
Guy Schalnat 已提交
3041
   png_init_read_transformations(png_ptr);
3042
#ifdef PNG_READ_INTERLACING_SUPPORTED
G
Guy Schalnat 已提交
3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054
   if (png_ptr->interlaced)
   {
      if (!(png_ptr->transformations & PNG_INTERLACE))
         png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
            png_pass_ystart[0]) / png_pass_yinc[0];
      else
         png_ptr->num_rows = png_ptr->height;

      png_ptr->iwidth = (png_ptr->width +
         png_pass_inc[png_ptr->pass] - 1 -
         png_pass_start[png_ptr->pass]) /
         png_pass_inc[png_ptr->pass];
3055

3056 3057
         png_ptr->irowbytes =
            PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1;
G
Guy Schalnat 已提交
3058 3059
   }
   else
3060
#endif /* PNG_READ_INTERLACING_SUPPORTED */
G
Guy Schalnat 已提交
3061 3062 3063 3064 3065 3066 3067
   {
      png_ptr->num_rows = png_ptr->height;
      png_ptr->iwidth = png_ptr->width;
      png_ptr->irowbytes = png_ptr->rowbytes + 1;
   }
   max_pixel_depth = png_ptr->pixel_depth;

G
Guy Schalnat 已提交
3068
#if defined(PNG_READ_PACK_SUPPORTED)
G
Guy Schalnat 已提交
3069 3070
   if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8)
      max_pixel_depth = 8;
G
Guy Schalnat 已提交
3071
#endif
G
Guy Schalnat 已提交
3072

G
Guy Schalnat 已提交
3073 3074
#if defined(PNG_READ_EXPAND_SUPPORTED)
   if (png_ptr->transformations & PNG_EXPAND)
G
Guy Schalnat 已提交
3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098
   {
      if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
      {
         if (png_ptr->num_trans)
            max_pixel_depth = 32;
         else
            max_pixel_depth = 24;
      }
      else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
      {
         if (max_pixel_depth < 8)
            max_pixel_depth = 8;
         if (png_ptr->num_trans)
            max_pixel_depth *= 2;
      }
      else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
      {
         if (png_ptr->num_trans)
         {
            max_pixel_depth *= 4;
            max_pixel_depth /= 3;
         }
      }
   }
G
Guy Schalnat 已提交
3099
#endif
G
Guy Schalnat 已提交
3100

G
Guy Schalnat 已提交
3101 3102
#if defined(PNG_READ_FILLER_SUPPORTED)
   if (png_ptr->transformations & (PNG_FILLER))
G
Guy Schalnat 已提交
3103
   {
3104 3105 3106
      if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
         max_pixel_depth = 32;
      else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119
      {
         if (max_pixel_depth <= 8)
            max_pixel_depth = 16;
         else
            max_pixel_depth = 32;
      }
      else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
      {
         if (max_pixel_depth <= 32)
            max_pixel_depth = 32;
         else
            max_pixel_depth = 64;
      }
G
Guy Schalnat 已提交
3120
   }
G
Guy Schalnat 已提交
3121
#endif
G
Guy Schalnat 已提交
3122

G
Guy Schalnat 已提交
3123
#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
G
Guy Schalnat 已提交
3124 3125
   if (png_ptr->transformations & PNG_GRAY_TO_RGB)
   {
3126 3127 3128 3129 3130 3131 3132 3133
      if (
#if defined(PNG_READ_EXPAND_SUPPORTED)
        (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) ||
#endif
#if defined(PNG_READ_FILLER_SUPPORTED)
        (png_ptr->transformations & (PNG_FILLER)) ||
#endif
        png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
G
Guy Schalnat 已提交
3134 3135 3136
      {
         if (max_pixel_depth <= 16)
            max_pixel_depth = 32;
3137
         else
G
Guy Schalnat 已提交
3138 3139 3140 3141 3142
            max_pixel_depth = 64;
      }
      else
      {
         if (max_pixel_depth <= 8)
3143 3144 3145 3146 3147 3148 3149 3150
           {
             if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
               max_pixel_depth = 32;
             else
               max_pixel_depth = 24;
           }
         else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
            max_pixel_depth = 64;
3151
         else
G
Guy Schalnat 已提交
3152 3153 3154
            max_pixel_depth = 48;
      }
   }
G
Guy Schalnat 已提交
3155
#endif
G
Guy Schalnat 已提交
3156

3157 3158
#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
3159
   if (png_ptr->transformations & PNG_USER_TRANSFORM)
3160
     {
3161
       int user_pixel_depth = png_ptr->user_transform_depth*
3162
         png_ptr->user_transform_channels;
3163
       if (user_pixel_depth > max_pixel_depth)
3164 3165 3166 3167
         max_pixel_depth=user_pixel_depth;
     }
#endif

G
Guy Schalnat 已提交
3168 3169
   /* align the width on the next larger 8 pixels.  Mainly used
      for interlacing */
3170
   row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
G
Guy Schalnat 已提交
3171
   /* calculate the maximum bytes needed, adding a byte and a pixel
3172
      for safety's sake */
3173
   row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) +
G
Guy Schalnat 已提交
3174 3175
      1 + ((max_pixel_depth + 7) >> 3);
#ifdef PNG_MAX_MALLOC_64K
3176
   if (row_bytes > (png_uint_32)65536L)
G
Guy Schalnat 已提交
3177
      png_error(png_ptr, "This image requires a row greater than 64KB");
G
Guy Schalnat 已提交
3178
#endif
3179 3180 3181 3182 3183 3184 3185 3186

   if (row_bytes + 64 > png_ptr->old_big_row_buf_size)
   {
     png_free(png_ptr, png_ptr->big_row_buf);
     png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes+64);
     png_ptr->row_buf = png_ptr->big_row_buf+32;
     png_ptr->old_big_row_buf_size = row_bytes+64;
   }
G
Guy Schalnat 已提交
3187 3188

#ifdef PNG_MAX_MALLOC_64K
3189
   if ((png_uint_32)png_ptr->rowbytes + 1 > (png_uint_32)65536L)
G
Guy Schalnat 已提交
3190
      png_error(png_ptr, "This image requires a row greater than 64KB");
G
Guy Schalnat 已提交
3191
#endif
3192
   if ((png_uint_32)png_ptr->rowbytes > (png_uint_32)(PNG_SIZE_MAX - 1))
3193
      png_error(png_ptr, "Row has too many bytes to allocate in memory");
3194 3195 3196 3197 3198 3199 3200 3201

   if (png_ptr->rowbytes+1 > png_ptr->old_prev_row_size)
   {
     png_free(png_ptr, png_ptr->prev_row);
     png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)(
        png_ptr->rowbytes + 1));
     png_ptr->old_prev_row_size = png_ptr->rowbytes+1;
   }
A
Andreas Dilger 已提交
3202

3203
   png_memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
G
Guy Schalnat 已提交
3204

3205 3206 3207 3208 3209 3210
   png_debug1(3, "width = %lu,\n", png_ptr->width);
   png_debug1(3, "height = %lu,\n", png_ptr->height);
   png_debug1(3, "iwidth = %lu,\n", png_ptr->iwidth);
   png_debug1(3, "num_rows = %lu\n", png_ptr->num_rows);
   png_debug1(3, "rowbytes = %lu,\n", png_ptr->rowbytes);
   png_debug1(3, "irowbytes = %lu,\n", png_ptr->irowbytes);
G
Guy Schalnat 已提交
3211

G
Guy Schalnat 已提交
3212
   png_ptr->flags |= PNG_FLAG_ROW_INIT;
G
Guy Schalnat 已提交
3213
}
3214
#endif /* PNG_READ_SUPPORTED */