Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(150)

Side by Side Diff: third_party/libpng/pngrutil.c

Issue 1591483003: XFA: Upgrade libpng to 1.6.20. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@xfa
Patch Set: rebase Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/libpng/pngrtran.c ('k') | third_party/libpng/pngset.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1
1 /* pngrutil.c - utilities to read a PNG file 2 /* pngrutil.c - utilities to read a PNG file
2 * 3 *
3 * Last changed in libpng 1.6.3 [July 18, 2013] 4 * Last changed in libpng 1.6.20 [December 3, 2015]
4 * Copyright (c) 1998-2013 Glenn Randers-Pehrson 5 * Copyright (c) 1998-2015 Glenn Randers-Pehrson
5 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) 6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
6 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) 7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
7 * 8 *
8 * This code is released under the libpng license. 9 * This code is released under the libpng license.
9 * For conditions of distribution and use, see the disclaimer 10 * For conditions of distribution and use, see the disclaimer
10 * and license in png.h 11 * and license in png.h
11 * 12 *
12 * This file contains routines that are only called from within 13 * This file contains routines that are only called from within
13 * libpng itself during the course of reading an image. 14 * libpng itself during the course of reading an image.
14 */ 15 */
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 * the following code does a two's complement to native conversion. 82 * the following code does a two's complement to native conversion.
82 */ 83 */
83 png_int_32 (PNGAPI 84 png_int_32 (PNGAPI
84 png_get_int_32)(png_const_bytep buf) 85 png_get_int_32)(png_const_bytep buf)
85 { 86 {
86 png_uint_32 uval = png_get_uint_32(buf); 87 png_uint_32 uval = png_get_uint_32(buf);
87 if ((uval & 0x80000000) == 0) /* non-negative */ 88 if ((uval & 0x80000000) == 0) /* non-negative */
88 return uval; 89 return uval;
89 90
90 uval = (uval ^ 0xffffffff) + 1; /* 2's complement: -x = ~x+1 */ 91 uval = (uval ^ 0xffffffff) + 1; /* 2's complement: -x = ~x+1 */
91 return -(png_int_32)uval; 92 if ((uval & 0x80000000) == 0) /* no overflow */
93 return -(png_int_32)uval;
94 /* The following has to be safe; this function only gets called on PNG data
95 * and if we get here that data is invalid. 0 is the most safe value and
96 * if not then an attacker would surely just generate a PNG with 0 instead.
97 */
98 return 0;
92 } 99 }
93 100
94 /* Grab an unsigned 16-bit integer from a buffer in big-endian format. */ 101 /* Grab an unsigned 16-bit integer from a buffer in big-endian format. */
95 png_uint_16 (PNGAPI 102 png_uint_16 (PNGAPI
96 png_get_uint_16)(png_const_bytep buf) 103 png_get_uint_16)(png_const_bytep buf)
97 { 104 {
98 /* ANSI-C requires an int value to accomodate at least 16 bits so this 105 /* ANSI-C requires an int value to accomodate at least 16 bits so this
99 * works and allows the compiler not to worry about possible narrowing 106 * works and allows the compiler not to worry about possible narrowing
100 * on 32 bit systems. (Pre-ANSI systems did not make integers smaller 107 * on 32-bit systems. (Pre-ANSI systems did not make integers smaller
101 * than 16 bits either.) 108 * than 16 bits either.)
102 */ 109 */
103 unsigned int val = 110 unsigned int val =
104 ((unsigned int)(*buf) << 8) + 111 ((unsigned int)(*buf) << 8) +
105 ((unsigned int)(*(buf + 1))); 112 ((unsigned int)(*(buf + 1)));
106 113
107 return (png_uint_16)val; 114 return (png_uint_16)val;
108 } 115 }
109 116
110 #endif /* PNG_READ_INT_FUNCTIONS_SUPPORTED */ 117 #endif /* READ_INT_FUNCTIONS */
111 118
112 /* Read and check the PNG file signature */ 119 /* Read and check the PNG file signature */
113 void /* PRIVATE */ 120 void /* PRIVATE */
114 png_read_sig(png_structrp png_ptr, png_inforp info_ptr) 121 png_read_sig(png_structrp png_ptr, png_inforp info_ptr)
115 { 122 {
116 png_size_t num_checked, num_to_check; 123 png_size_t num_checked, num_to_check;
117 124
118 /* Exit if the user application does not expect a signature. */ 125 /* Exit if the user application does not expect a signature. */
119 if (png_ptr->sig_bytes >= 8) 126 if (png_ptr->sig_bytes >= 8)
120 return; 127 return;
121 128
122 num_checked = png_ptr->sig_bytes; 129 num_checked = png_ptr->sig_bytes;
123 num_to_check = 8 - num_checked; 130 num_to_check = 8 - num_checked;
124 131
125 #ifdef PNG_IO_STATE_SUPPORTED 132 #ifdef PNG_IO_STATE_SUPPORTED
126 png_ptr->io_state = PNG_IO_READING | PNG_IO_SIGNATURE; 133 png_ptr->io_state = PNG_IO_READING | PNG_IO_SIGNATURE;
127 #endif 134 #endif
128 135
129 /* The signature must be serialized in a single I/O call. */ 136 /* The signature must be serialized in a single I/O call. */
130 png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check); 137 png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
131 png_ptr->sig_bytes = 8; 138 png_ptr->sig_bytes = 8;
132 139
133 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check)) 140 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check) != 0)
134 { 141 {
135 if (num_checked < 4 && 142 if (num_checked < 4 &&
136 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4)) 143 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
137 png_error(png_ptr, "Not a PNG file"); 144 png_error(png_ptr, "Not a PNG file");
138 else 145 else
139 png_error(png_ptr, "PNG file corrupted by ASCII conversion"); 146 png_error(png_ptr, "PNG file corrupted by ASCII conversion");
140 } 147 }
141 if (num_checked < 3) 148 if (num_checked < 3)
142 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE; 149 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
143 } 150 }
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 png_byte tmpbuf[PNG_INFLATE_BUF_SIZE]; 216 png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];
210 217
211 len = (sizeof tmpbuf); 218 len = (sizeof tmpbuf);
212 if (len > skip) 219 if (len > skip)
213 len = skip; 220 len = skip;
214 skip -= len; 221 skip -= len;
215 222
216 png_crc_read(png_ptr, tmpbuf, len); 223 png_crc_read(png_ptr, tmpbuf, len);
217 } 224 }
218 225
219 if (png_crc_error(png_ptr)) 226 if (png_crc_error(png_ptr) != 0)
220 { 227 {
221 if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) ? 228 if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) != 0 ?
222 !(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) : 229 (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) == 0 :
223 (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE)) 230 (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE) != 0)
224 { 231 {
225 png_chunk_warning(png_ptr, "CRC error"); 232 png_chunk_warning(png_ptr, "CRC error");
226 } 233 }
227 234
228 else 235 else
229 { 236 png_chunk_error(png_ptr, "CRC error");
230 png_chunk_benign_error(png_ptr, "CRC error");
231 return (0);
232 }
233 237
234 return (1); 238 return (1);
235 } 239 }
236 240
237 return (0); 241 return (0);
238 } 242 }
239 243
240 /* Compare the CRC stored in the PNG file with that calculated by libpng from 244 /* Compare the CRC stored in the PNG file with that calculated by libpng from
241 * the data it has read thus far. 245 * the data it has read thus far.
242 */ 246 */
243 int /* PRIVATE */ 247 int /* PRIVATE */
244 png_crc_error(png_structrp png_ptr) 248 png_crc_error(png_structrp png_ptr)
245 { 249 {
246 png_byte crc_bytes[4]; 250 png_byte crc_bytes[4];
247 png_uint_32 crc; 251 png_uint_32 crc;
248 int need_crc = 1; 252 int need_crc = 1;
249 253
250 if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name)) 254 if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) != 0)
251 { 255 {
252 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) == 256 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
253 (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN)) 257 (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
254 need_crc = 0; 258 need_crc = 0;
255 } 259 }
256 260
257 else /* critical */ 261 else /* critical */
258 { 262 {
259 if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) 263 if ((png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) != 0)
260 need_crc = 0; 264 need_crc = 0;
261 } 265 }
262 266
263 #ifdef PNG_IO_STATE_SUPPORTED 267 #ifdef PNG_IO_STATE_SUPPORTED
264 png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_CRC; 268 png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_CRC;
265 #endif 269 #endif
266 270
267 /* The chunk CRC must be serialized in a single I/O call. */ 271 /* The chunk CRC must be serialized in a single I/O call. */
268 png_read_data(png_ptr, crc_bytes, 4); 272 png_read_data(png_ptr, crc_bytes, 4);
269 273
270 if (need_crc) 274 if (need_crc != 0)
271 { 275 {
272 crc = png_get_uint_32(crc_bytes); 276 crc = png_get_uint_32(crc_bytes);
273 return ((int)(crc != png_ptr->crc)); 277 return ((int)(crc != png_ptr->crc));
274 } 278 }
275 279
276 else 280 else
277 return (0); 281 return (0);
278 } 282 }
279 283
284 #if defined(PNG_READ_iCCP_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) ||\
285 defined(PNG_READ_pCAL_SUPPORTED) || defined(PNG_READ_sCAL_SUPPORTED) ||\
286 defined(PNG_READ_sPLT_SUPPORTED) || defined(PNG_READ_tEXt_SUPPORTED) ||\
287 defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_SEQUENTIAL_READ_SUPPORTED)
280 /* Manage the read buffer; this simply reallocates the buffer if it is not small 288 /* Manage the read buffer; this simply reallocates the buffer if it is not small
281 * enough (or if it is not allocated). The routine returns a pointer to the 289 * enough (or if it is not allocated). The routine returns a pointer to the
282 * buffer; if an error occurs and 'warn' is set the routine returns NULL, else 290 * buffer; if an error occurs and 'warn' is set the routine returns NULL, else
283 * it will call png_error (via png_malloc) on failure. (warn == 2 means 291 * it will call png_error (via png_malloc) on failure. (warn == 2 means
284 * 'silent'). 292 * 'silent').
285 */ 293 */
286 static png_bytep 294 static png_bytep
287 png_read_buffer(png_structrp png_ptr, png_alloc_size_t new_size, int warn) 295 png_read_buffer(png_structrp png_ptr, png_alloc_size_t new_size, int warn)
288 { 296 {
289 png_bytep buffer = png_ptr->read_buffer; 297 png_bytep buffer = png_ptr->read_buffer;
(...skipping 12 matching lines...) Expand all
302 buffer = png_voidcast(png_bytep, png_malloc_base(png_ptr, new_size)); 310 buffer = png_voidcast(png_bytep, png_malloc_base(png_ptr, new_size));
303 311
304 if (buffer != NULL) 312 if (buffer != NULL)
305 { 313 {
306 png_ptr->read_buffer = buffer; 314 png_ptr->read_buffer = buffer;
307 png_ptr->read_buffer_size = new_size; 315 png_ptr->read_buffer_size = new_size;
308 } 316 }
309 317
310 else if (warn < 2) /* else silent */ 318 else if (warn < 2) /* else silent */
311 { 319 {
312 #ifdef PNG_WARNINGS_SUPPORTED 320 if (warn != 0)
313 if (warn)
314 png_chunk_warning(png_ptr, "insufficient memory to read chunk"); 321 png_chunk_warning(png_ptr, "insufficient memory to read chunk");
322
315 else 323 else
316 #endif
317 {
318 #ifdef PNG_ERROR_TEXT_SUPPORTED
319 png_chunk_error(png_ptr, "insufficient memory to read chunk"); 324 png_chunk_error(png_ptr, "insufficient memory to read chunk");
320 #endif
321 }
322 } 325 }
323 } 326 }
324 327
325 return buffer; 328 return buffer;
326 } 329 }
330 #endif /* READ_iCCP|iTXt|pCAL|sCAL|sPLT|tEXt|zTXt|SEQUENTIAL_READ */
327 331
328 /* png_inflate_claim: claim the zstream for some nefarious purpose that involves 332 /* png_inflate_claim: claim the zstream for some nefarious purpose that involves
329 * decompression. Returns Z_OK on success, else a zlib error code. It checks 333 * decompression. Returns Z_OK on success, else a zlib error code. It checks
330 * the owner but, in final release builds, just issues a warning if some other 334 * the owner but, in final release builds, just issues a warning if some other
331 * chunk apparently owns the stream. Prior to release it does a png_error. 335 * chunk apparently owns the stream. Prior to release it does a png_error.
332 */ 336 */
333 static int 337 static int
334 png_inflate_claim(png_structrp png_ptr, png_uint_32 owner) 338 png_inflate_claim(png_structrp png_ptr, png_uint_32 owner)
335 { 339 {
336 if (png_ptr->zowner != 0) 340 if (png_ptr->zowner != 0)
337 { 341 {
338 char msg[64]; 342 char msg[64];
339 343
340 PNG_STRING_FROM_CHUNK(msg, png_ptr->zowner); 344 PNG_STRING_FROM_CHUNK(msg, png_ptr->zowner);
341 /* So the message that results is "<chunk> using zstream"; this is an 345 /* So the message that results is "<chunk> using zstream"; this is an
342 * internal error, but is very useful for debugging. i18n requirements 346 * internal error, but is very useful for debugging. i18n requirements
343 * are minimal. 347 * are minimal.
344 */ 348 */
345 (void)png_safecat(msg, (sizeof msg), 4, " using zstream"); 349 (void)png_safecat(msg, (sizeof msg), 4, " using zstream");
346 # if PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC 350 #if PNG_RELEASE_BUILD
347 png_chunk_warning(png_ptr, msg); 351 png_chunk_warning(png_ptr, msg);
348 png_ptr->zowner = 0; 352 png_ptr->zowner = 0;
349 # else 353 #else
350 png_chunk_error(png_ptr, msg); 354 png_chunk_error(png_ptr, msg);
351 # endif 355 #endif
352 } 356 }
353 357
354 /* Implementation note: unlike 'png_deflate_claim' this internal function 358 /* Implementation note: unlike 'png_deflate_claim' this internal function
355 * does not take the size of the data as an argument. Some efficiency could 359 * does not take the size of the data as an argument. Some efficiency could
356 * be gained by using this when it is known *if* the zlib stream itself does 360 * be gained by using this when it is known *if* the zlib stream itself does
357 * not record the number; however, this is an illusion: the original writer 361 * not record the number; however, this is an illusion: the original writer
358 * of the PNG may have selected a lower window size, and we really must 362 * of the PNG may have selected a lower window size, and we really must
359 * follow that because, for systems with with limited capabilities, we 363 * follow that because, for systems with with limited capabilities, we
360 * would otherwise reject the application's attempts to use a smaller window 364 * would otherwise reject the application's attempts to use a smaller window
361 * size (zlib doesn't have an interface to say "this or lower"!). 365 * size (zlib doesn't have an interface to say "this or lower"!).
362 * 366 *
363 * inflateReset2 was added to zlib 1.2.4; before this the window could not be 367 * inflateReset2 was added to zlib 1.2.4; before this the window could not be
364 * reset, therefore it is necessary to always allocate the maximum window 368 * reset, therefore it is necessary to always allocate the maximum window
365 * size with earlier zlibs just in case later compressed chunks need it. 369 * size with earlier zlibs just in case later compressed chunks need it.
366 */ 370 */
367 { 371 {
368 int ret; /* zlib return code */ 372 int ret; /* zlib return code */
369 # if PNG_ZLIB_VERNUM >= 0x1240 373 #if PNG_ZLIB_VERNUM >= 0x1240
370 374
371 # if defined(PNG_SET_OPTION_SUPPORTED) && \ 375 # if defined(PNG_SET_OPTION_SUPPORTED) && defined(PNG_MAXIMUM_INFLATE_WINDOW)
372 defined(PNG_MAXIMUM_INFLATE_WINDOW) 376 int window_bits;
373 int window_bits; 377
374 378 if (((png_ptr->options >> PNG_MAXIMUM_INFLATE_WINDOW) & 3) ==
375 if (((png_ptr->options >> PNG_MAXIMUM_INFLATE_WINDOW) & 3) == 379 PNG_OPTION_ON)
376 PNG_OPTION_ON) 380 {
377 window_bits = 15; 381 window_bits = 15;
378 382 png_ptr->zstream_start = 0; /* fixed window size */
379 else 383 }
380 window_bits = 0; 384
381 # else 385 else
382 # define window_bits 0 386 {
383 # endif 387 window_bits = 0;
384 # endif 388 png_ptr->zstream_start = 1;
389 }
390 # else
391 # define window_bits 0
392 # endif
393 #endif
385 394
386 /* Set this for safety, just in case the previous owner left pointers to 395 /* Set this for safety, just in case the previous owner left pointers to
387 * memory allocations. 396 * memory allocations.
388 */ 397 */
389 png_ptr->zstream.next_in = NULL; 398 png_ptr->zstream.next_in = NULL;
390 png_ptr->zstream.avail_in = 0; 399 png_ptr->zstream.avail_in = 0;
391 png_ptr->zstream.next_out = NULL; 400 png_ptr->zstream.next_out = NULL;
392 png_ptr->zstream.avail_out = 0; 401 png_ptr->zstream.avail_out = 0;
393 402
394 if (png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) 403 if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0)
395 { 404 {
396 # if PNG_ZLIB_VERNUM < 0x1240 405 #if PNG_ZLIB_VERNUM < 0x1240
397 ret = inflateReset(&png_ptr->zstream); 406 ret = inflateReset(&png_ptr->zstream);
398 # else 407 #else
399 ret = inflateReset2(&png_ptr->zstream, window_bits); 408 ret = inflateReset2(&png_ptr->zstream, window_bits);
400 # endif 409 #endif
401 } 410 }
402 411
403 else 412 else
404 { 413 {
405 # if PNG_ZLIB_VERNUM < 0x1240 414 #if PNG_ZLIB_VERNUM < 0x1240
406 ret = inflateInit(&png_ptr->zstream); 415 ret = inflateInit(&png_ptr->zstream);
407 # else 416 #else
408 ret = inflateInit2(&png_ptr->zstream, window_bits); 417 ret = inflateInit2(&png_ptr->zstream, window_bits);
409 # endif 418 #endif
410 419
411 if (ret == Z_OK) 420 if (ret == Z_OK)
412 png_ptr->flags |= PNG_FLAG_ZSTREAM_INITIALIZED; 421 png_ptr->flags |= PNG_FLAG_ZSTREAM_INITIALIZED;
413 } 422 }
414 423
415 if (ret == Z_OK) 424 if (ret == Z_OK)
416 png_ptr->zowner = owner; 425 png_ptr->zowner = owner;
417 426
418 else 427 else
419 png_zstream_error(png_ptr, ret); 428 png_zstream_error(png_ptr, ret);
420 429
421 return ret; 430 return ret;
422 } 431 }
423 432
424 # ifdef window_bits 433 #ifdef window_bits
425 # undef window_bits 434 # undef window_bits
426 # endif 435 #endif
427 } 436 }
437
438 #if PNG_ZLIB_VERNUM >= 0x1240
439 /* Handle the start of the inflate stream if we called inflateInit2(strm,0);
440 * in this case some zlib versions skip validation of the CINFO field and, in
441 * certain circumstances, libpng may end up displaying an invalid image, in
442 * contrast to implementations that call zlib in the normal way (e.g. libpng
443 * 1.5).
444 */
445 int /* PRIVATE */
446 png_zlib_inflate(png_structrp png_ptr, int flush)
447 {
448 if (png_ptr->zstream_start && png_ptr->zstream.avail_in > 0)
449 {
450 if ((*png_ptr->zstream.next_in >> 4) > 7)
451 {
452 png_ptr->zstream.msg = "invalid window size (libpng)";
453 return Z_DATA_ERROR;
454 }
455
456 png_ptr->zstream_start = 0;
457 }
458
459 return inflate(&png_ptr->zstream, flush);
460 }
461 #endif /* Zlib >= 1.2.4 */
428 462
429 #ifdef PNG_READ_COMPRESSED_TEXT_SUPPORTED 463 #ifdef PNG_READ_COMPRESSED_TEXT_SUPPORTED
430 /* png_inflate now returns zlib error codes including Z_OK and Z_STREAM_END to 464 /* png_inflate now returns zlib error codes including Z_OK and Z_STREAM_END to
431 * allow the caller to do multiple calls if required. If the 'finish' flag is 465 * allow the caller to do multiple calls if required. If the 'finish' flag is
432 * set Z_FINISH will be passed to the final inflate() call and Z_STREAM_END must 466 * set Z_FINISH will be passed to the final inflate() call and Z_STREAM_END must
433 * be returned or there has been a problem, otherwise Z_SYNC_FLUSH is used and 467 * be returned or there has been a problem, otherwise Z_SYNC_FLUSH is used and
434 * Z_OK or Z_STREAM_END will be returned on success. 468 * Z_OK or Z_STREAM_END will be returned on success.
435 * 469 *
436 * The input and output sizes are updated to the actual amounts of data consumed 470 * The input and output sizes are updated to the actual amounts of data consumed
437 * or written, not the amount available (as in a z_stream). The data pointers 471 * or written, not the amount available (as in a z_stream). The data pointers
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
512 546
513 png_ptr->zstream.avail_out = avail; 547 png_ptr->zstream.avail_out = avail;
514 avail_out -= avail; 548 avail_out -= avail;
515 549
516 /* zlib inflate call */ 550 /* zlib inflate call */
517 /* In fact 'avail_out' may be 0 at this point, that happens at the end 551 /* In fact 'avail_out' may be 0 at this point, that happens at the end
518 * of the read when the final LZ end code was not passed at the end of 552 * of the read when the final LZ end code was not passed at the end of
519 * the previous chunk of input data. Tell zlib if we have reached the 553 * the previous chunk of input data. Tell zlib if we have reached the
520 * end of the output buffer. 554 * end of the output buffer.
521 */ 555 */
522 ret = inflate(&png_ptr->zstream, avail_out > 0 ? Z_NO_FLUSH : 556 ret = PNG_INFLATE(png_ptr, avail_out > 0 ? Z_NO_FLUSH :
523 (finish ? Z_FINISH : Z_SYNC_FLUSH)); 557 (finish ? Z_FINISH : Z_SYNC_FLUSH));
524 } while (ret == Z_OK); 558 } while (ret == Z_OK);
525 559
526 /* For safety kill the local buffer pointer now */ 560 /* For safety kill the local buffer pointer now */
527 if (output == NULL) 561 if (output == NULL)
528 png_ptr->zstream.next_out = NULL; 562 png_ptr->zstream.next_out = NULL;
529 563
530 /* Claw back the 'size' and 'remaining_space' byte counts. */ 564 /* Claw back the 'size' and 'remaining_space' byte counts. */
531 avail_in += png_ptr->zstream.avail_in; 565 avail_in += png_ptr->zstream.avail_in;
532 avail_out += png_ptr->zstream.avail_out; 566 avail_out += png_ptr->zstream.avail_out;
533 567
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
571 { 605 {
572 /* TODO: implement different limits for different types of chunk. 606 /* TODO: implement different limits for different types of chunk.
573 * 607 *
574 * The caller supplies *newlength set to the maximum length of the 608 * The caller supplies *newlength set to the maximum length of the
575 * uncompressed data, but this routine allocates space for the prefix and 609 * uncompressed data, but this routine allocates space for the prefix and
576 * maybe a '\0' terminator too. We have to assume that 'prefix_size' is 610 * maybe a '\0' terminator too. We have to assume that 'prefix_size' is
577 * limited only by the maximum chunk size. 611 * limited only by the maximum chunk size.
578 */ 612 */
579 png_alloc_size_t limit = PNG_SIZE_MAX; 613 png_alloc_size_t limit = PNG_SIZE_MAX;
580 614
581 # ifdef PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED 615 # ifdef PNG_SET_USER_LIMITS_SUPPORTED
582 if (png_ptr->user_chunk_malloc_max > 0 && 616 if (png_ptr->user_chunk_malloc_max > 0 &&
583 png_ptr->user_chunk_malloc_max < limit) 617 png_ptr->user_chunk_malloc_max < limit)
584 limit = png_ptr->user_chunk_malloc_max; 618 limit = png_ptr->user_chunk_malloc_max;
585 # elif PNG_USER_CHUNK_MALLOC_MAX > 0 619 # elif PNG_USER_CHUNK_MALLOC_MAX > 0
586 if (PNG_USER_CHUNK_MALLOC_MAX < limit) 620 if (PNG_USER_CHUNK_MALLOC_MAX < limit)
587 limit = PNG_USER_CHUNK_MALLOC_MAX; 621 limit = PNG_USER_CHUNK_MALLOC_MAX;
588 # endif 622 # endif
589 623
590 if (limit >= prefix_size + (terminate != 0)) 624 if (limit >= prefix_size + (terminate != 0))
591 { 625 {
592 int ret; 626 int ret;
593 627
594 limit -= prefix_size + (terminate != 0); 628 limit -= prefix_size + (terminate != 0);
595 629
596 if (limit < *newlength) 630 if (limit < *newlength)
597 *newlength = limit; 631 *newlength = limit;
598 632
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
632 if (text != NULL) 666 if (text != NULL)
633 { 667 {
634 ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/, 668 ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/,
635 png_ptr->read_buffer + prefix_size, &lzsize, 669 png_ptr->read_buffer + prefix_size, &lzsize,
636 text + prefix_size, newlength); 670 text + prefix_size, newlength);
637 671
638 if (ret == Z_STREAM_END) 672 if (ret == Z_STREAM_END)
639 { 673 {
640 if (new_size == *newlength) 674 if (new_size == *newlength)
641 { 675 {
642 if (terminate) 676 if (terminate != 0)
643 text[prefix_size + *newlength] = 0; 677 text[prefix_size + *newlength] = 0;
644 678
645 if (prefix_size > 0) 679 if (prefix_size > 0)
646 memcpy(text, png_ptr->read_buffer, prefix_size); 680 memcpy(text, png_ptr->read_buffer, prefix_size);
647 681
648 { 682 {
649 png_bytep old_ptr = png_ptr->read_buffer; 683 png_bytep old_ptr = png_ptr->read_buffer;
650 684
651 png_ptr->read_buffer = text; 685 png_ptr->read_buffer = text;
652 png_ptr->read_buffer_size = buffer_size; 686 png_ptr->read_buffer_size = buffer_size;
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
713 return ret; 747 return ret;
714 } 748 }
715 749
716 else 750 else
717 { 751 {
718 /* Application/configuration limits exceeded */ 752 /* Application/configuration limits exceeded */
719 png_zstream_error(png_ptr, Z_MEM_ERROR); 753 png_zstream_error(png_ptr, Z_MEM_ERROR);
720 return Z_MEM_ERROR; 754 return Z_MEM_ERROR;
721 } 755 }
722 } 756 }
723 #endif /* PNG_READ_COMPRESSED_TEXT_SUPPORTED */ 757 #endif /* READ_COMPRESSED_TEXT */
724 758
725 #ifdef PNG_READ_iCCP_SUPPORTED 759 #ifdef PNG_READ_iCCP_SUPPORTED
726 /* Perform a partial read and decompress, producing 'avail_out' bytes and 760 /* Perform a partial read and decompress, producing 'avail_out' bytes and
727 * reading from the current chunk as required. 761 * reading from the current chunk as required.
728 */ 762 */
729 static int 763 static int
730 png_inflate_read(png_structrp png_ptr, png_bytep read_buffer, uInt read_size, 764 png_inflate_read(png_structrp png_ptr, png_bytep read_buffer, uInt read_size,
731 png_uint_32p chunk_bytes, png_bytep next_out, png_alloc_size_t *out_size, 765 png_uint_32p chunk_bytes, png_bytep next_out, png_alloc_size_t *out_size,
732 int finish) 766 int finish)
733 { 767 {
(...skipping 27 matching lines...) Expand all
761 avail = (uInt)*out_size; 795 avail = (uInt)*out_size;
762 *out_size -= avail; 796 *out_size -= avail;
763 797
764 png_ptr->zstream.avail_out = avail; 798 png_ptr->zstream.avail_out = avail;
765 } 799 }
766 800
767 /* Use Z_SYNC_FLUSH when there is no more chunk data to ensure that all 801 /* Use Z_SYNC_FLUSH when there is no more chunk data to ensure that all
768 * the available output is produced; this allows reading of truncated 802 * the available output is produced; this allows reading of truncated
769 * streams. 803 * streams.
770 */ 804 */
771 ret = inflate(&png_ptr->zstream, 805 ret = PNG_INFLATE(png_ptr,
772 *chunk_bytes > 0 ? Z_NO_FLUSH : (finish ? Z_FINISH : Z_SYNC_FLUSH)); 806 *chunk_bytes > 0 ? Z_NO_FLUSH : (finish ? Z_FINISH : Z_SYNC_FLUSH));
773 } 807 }
774 while (ret == Z_OK && (*out_size > 0 || png_ptr->zstream.avail_out > 0)); 808 while (ret == Z_OK && (*out_size > 0 || png_ptr->zstream.avail_out > 0));
775 809
776 *out_size += png_ptr->zstream.avail_out; 810 *out_size += png_ptr->zstream.avail_out;
777 png_ptr->zstream.avail_out = 0; /* Should not be required, but is safe */ 811 png_ptr->zstream.avail_out = 0; /* Should not be required, but is safe */
778 812
779 /* Ensure the error message pointer is always set: */ 813 /* Ensure the error message pointer is always set: */
780 png_zstream_error(png_ptr, ret); 814 png_zstream_error(png_ptr, ret);
781 return ret; 815 return ret;
782 } 816 }
783 817
784 else 818 else
785 { 819 {
786 png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed"); 820 png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed");
787 return Z_STREAM_ERROR; 821 return Z_STREAM_ERROR;
788 } 822 }
789 } 823 }
790 #endif 824 #endif
791 825
792 /* Read and check the IDHR chunk */ 826 /* Read and check the IDHR chunk */
827
793 void /* PRIVATE */ 828 void /* PRIVATE */
794 png_handle_IHDR(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) 829 png_handle_IHDR(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
795 { 830 {
796 png_byte buf[13]; 831 png_byte buf[13];
797 png_uint_32 width, height; 832 png_uint_32 width, height;
798 int bit_depth, color_type, compression_type, filter_type; 833 int bit_depth, color_type, compression_type, filter_type;
799 int interlace_type; 834 int interlace_type;
800 835
801 png_debug(1, "in png_handle_IHDR"); 836 png_debug(1, "in png_handle_IHDR");
802 837
803 if (png_ptr->mode & PNG_HAVE_IHDR) 838 if ((png_ptr->mode & PNG_HAVE_IHDR) != 0)
804 png_chunk_error(png_ptr, "out of place"); 839 png_chunk_error(png_ptr, "out of place");
805 840
806 /* Check the length */ 841 /* Check the length */
807 if (length != 13) 842 if (length != 13)
808 png_chunk_error(png_ptr, "invalid"); 843 png_chunk_error(png_ptr, "invalid");
809 844
810 png_ptr->mode |= PNG_HAVE_IHDR; 845 png_ptr->mode |= PNG_HAVE_IHDR;
811 846
812 png_crc_read(png_ptr, buf, 13); 847 png_crc_read(png_ptr, buf, 13);
813 png_crc_finish(png_ptr, 0); 848 png_crc_finish(png_ptr, 0);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
847 case PNG_COLOR_TYPE_GRAY_ALPHA: 882 case PNG_COLOR_TYPE_GRAY_ALPHA:
848 png_ptr->channels = 2; 883 png_ptr->channels = 2;
849 break; 884 break;
850 885
851 case PNG_COLOR_TYPE_RGB_ALPHA: 886 case PNG_COLOR_TYPE_RGB_ALPHA:
852 png_ptr->channels = 4; 887 png_ptr->channels = 4;
853 break; 888 break;
854 } 889 }
855 890
856 /* Set up other useful info */ 891 /* Set up other useful info */
857 png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth * 892 png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth * png_ptr->channels);
858 png_ptr->channels);
859 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width); 893 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width);
860 png_debug1(3, "bit_depth = %d", png_ptr->bit_depth); 894 png_debug1(3, "bit_depth = %d", png_ptr->bit_depth);
861 png_debug1(3, "channels = %d", png_ptr->channels); 895 png_debug1(3, "channels = %d", png_ptr->channels);
862 png_debug1(3, "rowbytes = %lu", (unsigned long)png_ptr->rowbytes); 896 png_debug1(3, "rowbytes = %lu", (unsigned long)png_ptr->rowbytes);
863 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, 897 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
864 color_type, interlace_type, compression_type, filter_type); 898 color_type, interlace_type, compression_type, filter_type);
865 } 899 }
866 900
867 /* Read and check the palette */ 901 /* Read and check the palette */
868 void /* PRIVATE */ 902 void /* PRIVATE */
869 png_handle_PLTE(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) 903 png_handle_PLTE(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
870 { 904 {
871 png_color palette[PNG_MAX_PALETTE_LENGTH]; 905 png_color palette[PNG_MAX_PALETTE_LENGTH];
872 int num, i; 906 int max_palette_length, num, i;
873 #ifdef PNG_POINTER_INDEXING_SUPPORTED 907 #ifdef PNG_POINTER_INDEXING_SUPPORTED
874 png_colorp pal_ptr; 908 png_colorp pal_ptr;
875 #endif 909 #endif
876 910
877 png_debug(1, "in png_handle_PLTE"); 911 png_debug(1, "in png_handle_PLTE");
878 912
879 if (!(png_ptr->mode & PNG_HAVE_IHDR)) 913 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
880 png_chunk_error(png_ptr, "missing IHDR"); 914 png_chunk_error(png_ptr, "missing IHDR");
881 915
882 /* Moved to before the 'after IDAT' check below because otherwise duplicate 916 /* Moved to before the 'after IDAT' check below because otherwise duplicate
883 * PLTE chunks are potentially ignored (the spec says there shall not be more 917 * PLTE chunks are potentially ignored (the spec says there shall not be more
884 * than one PLTE, the error is not treated as benign, so this check trumps 918 * than one PLTE, the error is not treated as benign, so this check trumps
885 * the requirement that PLTE appears before IDAT.) 919 * the requirement that PLTE appears before IDAT.)
886 */ 920 */
887 else if (png_ptr->mode & PNG_HAVE_PLTE) 921 else if ((png_ptr->mode & PNG_HAVE_PLTE) != 0)
888 png_chunk_error(png_ptr, "duplicate"); 922 png_chunk_error(png_ptr, "duplicate");
889 923
890 else if (png_ptr->mode & PNG_HAVE_IDAT) 924 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
891 { 925 {
892 /* This is benign because the non-benign error happened before, when an 926 /* This is benign because the non-benign error happened before, when an
893 * IDAT was encountered in a color-mapped image with no PLTE. 927 * IDAT was encountered in a color-mapped image with no PLTE.
894 */ 928 */
895 png_crc_finish(png_ptr, length); 929 png_crc_finish(png_ptr, length);
896 png_chunk_benign_error(png_ptr, "out of place"); 930 png_chunk_benign_error(png_ptr, "out of place");
897 return; 931 return;
898 } 932 }
899 933
900 png_ptr->mode |= PNG_HAVE_PLTE; 934 png_ptr->mode |= PNG_HAVE_PLTE;
901 935
902 if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) 936 if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0)
903 { 937 {
904 png_crc_finish(png_ptr, length); 938 png_crc_finish(png_ptr, length);
905 png_chunk_benign_error(png_ptr, "ignored in grayscale PNG"); 939 png_chunk_benign_error(png_ptr, "ignored in grayscale PNG");
906 return; 940 return;
907 } 941 }
908 942
909 #ifndef PNG_READ_OPT_PLTE_SUPPORTED 943 #ifndef PNG_READ_OPT_PLTE_SUPPORTED
910 if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE) 944 if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
911 { 945 {
912 png_crc_finish(png_ptr, length); 946 png_crc_finish(png_ptr, length);
(...skipping 10 matching lines...) Expand all
923 957
924 else 958 else
925 png_chunk_error(png_ptr, "invalid"); 959 png_chunk_error(png_ptr, "invalid");
926 960
927 return; 961 return;
928 } 962 }
929 963
930 /* The cast is safe because 'length' is less than 3*PNG_MAX_PALETTE_LENGTH */ 964 /* The cast is safe because 'length' is less than 3*PNG_MAX_PALETTE_LENGTH */
931 num = (int)length / 3; 965 num = (int)length / 3;
932 966
967 /* If the palette has 256 or fewer entries but is too large for the bit
968 * depth, we don't issue an error, to preserve the behavior of previous
969 * libpng versions. We silently truncate the unused extra palette entries
970 * here.
971 */
972 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
973 max_palette_length = (1 << png_ptr->bit_depth);
974 else
975 max_palette_length = PNG_MAX_PALETTE_LENGTH;
976
977 if (num > max_palette_length)
978 num = max_palette_length;
979
933 #ifdef PNG_POINTER_INDEXING_SUPPORTED 980 #ifdef PNG_POINTER_INDEXING_SUPPORTED
934 for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++) 981 for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)
935 { 982 {
936 png_byte buf[3]; 983 png_byte buf[3];
937 984
938 png_crc_read(png_ptr, buf, 3); 985 png_crc_read(png_ptr, buf, 3);
939 pal_ptr->red = buf[0]; 986 pal_ptr->red = buf[0];
940 pal_ptr->green = buf[1]; 987 pal_ptr->green = buf[1];
941 pal_ptr->blue = buf[2]; 988 pal_ptr->blue = buf[2];
942 } 989 }
(...skipping 12 matching lines...) Expand all
955 1002
956 /* If we actually need the PLTE chunk (ie for a paletted image), we do 1003 /* If we actually need the PLTE chunk (ie for a paletted image), we do
957 * whatever the normal CRC configuration tells us. However, if we 1004 * whatever the normal CRC configuration tells us. However, if we
958 * have an RGB image, the PLTE can be considered ancillary, so 1005 * have an RGB image, the PLTE can be considered ancillary, so
959 * we will act as though it is. 1006 * we will act as though it is.
960 */ 1007 */
961 #ifndef PNG_READ_OPT_PLTE_SUPPORTED 1008 #ifndef PNG_READ_OPT_PLTE_SUPPORTED
962 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) 1009 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
963 #endif 1010 #endif
964 { 1011 {
965 png_crc_finish(png_ptr, 0); 1012 png_crc_finish(png_ptr, (int) length - num * 3);
966 } 1013 }
967 1014
968 #ifndef PNG_READ_OPT_PLTE_SUPPORTED 1015 #ifndef PNG_READ_OPT_PLTE_SUPPORTED
969 else if (png_crc_error(png_ptr)) /* Only if we have a CRC error */ 1016 else if (png_crc_error(png_ptr) != 0) /* Only if we have a CRC error */
970 { 1017 {
971 /* If we don't want to use the data from an ancillary chunk, 1018 /* If we don't want to use the data from an ancillary chunk,
972 * we have two options: an error abort, or a warning and we 1019 * we have two options: an error abort, or a warning and we
973 * ignore the data in this chunk (which should be OK, since 1020 * ignore the data in this chunk (which should be OK, since
974 * it's considered ancillary for a RGB or RGBA image). 1021 * it's considered ancillary for a RGB or RGBA image).
975 * 1022 *
976 * IMPLEMENTATION NOTE: this is only here because png_crc_finish uses the 1023 * IMPLEMENTATION NOTE: this is only here because png_crc_finish uses the
977 * chunk type to determine whether to check the ancillary or the critical 1024 * chunk type to determine whether to check the ancillary or the critical
978 * flags. 1025 * flags.
979 */ 1026 */
980 if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE)) 1027 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE) == 0)
981 { 1028 {
982 if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) 1029 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) != 0)
983 { 1030 return;
984 png_chunk_benign_error(png_ptr, "CRC error");
985 }
986 1031
987 else 1032 else
988 { 1033 png_chunk_error(png_ptr, "CRC error");
989 png_chunk_warning(png_ptr, "CRC error");
990 return;
991 }
992 } 1034 }
993 1035
994 /* Otherwise, we (optionally) emit a warning and use the chunk. */ 1036 /* Otherwise, we (optionally) emit a warning and use the chunk. */
995 else if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)) 1037 else if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) == 0)
996 {
997 png_chunk_warning(png_ptr, "CRC error"); 1038 png_chunk_warning(png_ptr, "CRC error");
998 }
999 } 1039 }
1000 #endif 1040 #endif
1001 1041
1002 /* TODO: png_set_PLTE has the side effect of setting png_ptr->palette to its 1042 /* TODO: png_set_PLTE has the side effect of setting png_ptr->palette to its
1003 * own copy of the palette. This has the side effect that when png_start_row 1043 * own copy of the palette. This has the side effect that when png_start_row
1004 * is called (this happens after any call to png_read_update_info) the 1044 * is called (this happens after any call to png_read_update_info) the
1005 * info_ptr palette gets changed. This is extremely unexpected and 1045 * info_ptr palette gets changed. This is extremely unexpected and
1006 * confusing. 1046 * confusing.
1007 * 1047 *
1008 * Fix this by not sharing the palette in this way. 1048 * Fix this by not sharing the palette in this way.
1009 */ 1049 */
1010 png_set_PLTE(png_ptr, info_ptr, palette, num); 1050 png_set_PLTE(png_ptr, info_ptr, palette, num);
1011 1051
1012 /* The three chunks, bKGD, hIST and tRNS *must* appear after PLTE and before 1052 /* The three chunks, bKGD, hIST and tRNS *must* appear after PLTE and before
1013 * IDAT. Prior to 1.6.0 this was not checked; instead the code merely 1053 * IDAT. Prior to 1.6.0 this was not checked; instead the code merely
1014 * checked the apparent validity of a tRNS chunk inserted before PLTE on a 1054 * checked the apparent validity of a tRNS chunk inserted before PLTE on a
1015 * palette PNG. 1.6.0 attempts to rigorously follow the standard and 1055 * palette PNG. 1.6.0 attempts to rigorously follow the standard and
1016 * therefore does a benign error if the erroneous condition is detected *and* 1056 * therefore does a benign error if the erroneous condition is detected *and*
1017 * cancels the tRNS if the benign error returns. The alternative is to 1057 * cancels the tRNS if the benign error returns. The alternative is to
1018 * amend the standard since it would be rather hypocritical of the standards 1058 * amend the standard since it would be rather hypocritical of the standards
1019 * maintainers to ignore it. 1059 * maintainers to ignore it.
1020 */ 1060 */
1021 #ifdef PNG_READ_tRNS_SUPPORTED 1061 #ifdef PNG_READ_tRNS_SUPPORTED
1022 if (png_ptr->num_trans > 0 || 1062 if (png_ptr->num_trans > 0 ||
1023 (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS) != 0)) 1063 (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS) != 0))
1024 { 1064 {
1025 /* Cancel this because otherwise it would be used if the transforms 1065 /* Cancel this because otherwise it would be used if the transforms
1026 * require it. Don't cancel the 'valid' flag because this would prevent 1066 * require it. Don't cancel the 'valid' flag because this would prevent
1027 * detection of duplicate chunks. 1067 * detection of duplicate chunks.
1028 */ 1068 */
1029 png_ptr->num_trans = 0; 1069 png_ptr->num_trans = 0;
1030 1070
1031 if (info_ptr != NULL) 1071 if (info_ptr != NULL)
1032 info_ptr->num_trans = 0; 1072 info_ptr->num_trans = 0;
1033 1073
(...skipping 10 matching lines...) Expand all
1044 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) != 0) 1084 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) != 0)
1045 png_chunk_benign_error(png_ptr, "bKGD must be after"); 1085 png_chunk_benign_error(png_ptr, "bKGD must be after");
1046 #endif 1086 #endif
1047 } 1087 }
1048 1088
1049 void /* PRIVATE */ 1089 void /* PRIVATE */
1050 png_handle_IEND(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) 1090 png_handle_IEND(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1051 { 1091 {
1052 png_debug(1, "in png_handle_IEND"); 1092 png_debug(1, "in png_handle_IEND");
1053 1093
1054 if (!(png_ptr->mode & PNG_HAVE_IHDR) || !(png_ptr->mode & PNG_HAVE_IDAT)) 1094 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0 ||
1095 (png_ptr->mode & PNG_HAVE_IDAT) == 0)
1055 png_chunk_error(png_ptr, "out of place"); 1096 png_chunk_error(png_ptr, "out of place");
1056 1097
1057 png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND); 1098 png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND);
1058 1099
1059 png_crc_finish(png_ptr, length); 1100 png_crc_finish(png_ptr, length);
1060 1101
1061 if (length != 0) 1102 if (length != 0)
1062 png_chunk_benign_error(png_ptr, "invalid"); 1103 png_chunk_benign_error(png_ptr, "invalid");
1063 1104
1064 PNG_UNUSED(info_ptr) 1105 PNG_UNUSED(info_ptr)
1065 } 1106 }
1066 1107
1067 #ifdef PNG_READ_gAMA_SUPPORTED 1108 #ifdef PNG_READ_gAMA_SUPPORTED
1068 void /* PRIVATE */ 1109 void /* PRIVATE */
1069 png_handle_gAMA(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) 1110 png_handle_gAMA(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1070 { 1111 {
1071 png_fixed_point igamma; 1112 png_fixed_point igamma;
1072 png_byte buf[4]; 1113 png_byte buf[4];
1073 1114
1074 png_debug(1, "in png_handle_gAMA"); 1115 png_debug(1, "in png_handle_gAMA");
1075 1116
1076 if (!(png_ptr->mode & PNG_HAVE_IHDR)) 1117 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1077 png_chunk_error(png_ptr, "missing IHDR"); 1118 png_chunk_error(png_ptr, "missing IHDR");
1078 1119
1079 else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) 1120 else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
1080 { 1121 {
1081 png_crc_finish(png_ptr, length); 1122 png_crc_finish(png_ptr, length);
1082 png_chunk_benign_error(png_ptr, "out of place"); 1123 png_chunk_benign_error(png_ptr, "out of place");
1083 return; 1124 return;
1084 } 1125 }
1085 1126
1086 if (length != 4) 1127 if (length != 4)
1087 { 1128 {
1088 png_crc_finish(png_ptr, length); 1129 png_crc_finish(png_ptr, length);
1089 png_chunk_benign_error(png_ptr, "invalid"); 1130 png_chunk_benign_error(png_ptr, "invalid");
1090 return; 1131 return;
1091 } 1132 }
1092 1133
1093 png_crc_read(png_ptr, buf, 4); 1134 png_crc_read(png_ptr, buf, 4);
1094 1135
1095 if (png_crc_finish(png_ptr, 0)) 1136 if (png_crc_finish(png_ptr, 0) != 0)
1096 return; 1137 return;
1097 1138
1098 igamma = png_get_fixed_point(NULL, buf); 1139 igamma = png_get_fixed_point(NULL, buf);
1099 1140
1100 png_colorspace_set_gamma(png_ptr, &png_ptr->colorspace, igamma); 1141 png_colorspace_set_gamma(png_ptr, &png_ptr->colorspace, igamma);
1101 png_colorspace_sync(png_ptr, info_ptr); 1142 png_colorspace_sync(png_ptr, info_ptr);
1102 } 1143 }
1103 #endif 1144 #endif
1104 1145
1105 #ifdef PNG_READ_sBIT_SUPPORTED 1146 #ifdef PNG_READ_sBIT_SUPPORTED
1106 void /* PRIVATE */ 1147 void /* PRIVATE */
1107 png_handle_sBIT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) 1148 png_handle_sBIT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1108 { 1149 {
1109 unsigned int truelen; 1150 unsigned int truelen, i;
1151 png_byte sample_depth;
1110 png_byte buf[4]; 1152 png_byte buf[4];
1111 1153
1112 png_debug(1, "in png_handle_sBIT"); 1154 png_debug(1, "in png_handle_sBIT");
1113 1155
1114 buf[0] = buf[1] = buf[2] = buf[3] = 0; 1156 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1115
1116 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1117 png_chunk_error(png_ptr, "missing IHDR"); 1157 png_chunk_error(png_ptr, "missing IHDR");
1118 1158
1119 else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) 1159 else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
1120 { 1160 {
1121 png_crc_finish(png_ptr, length); 1161 png_crc_finish(png_ptr, length);
1122 png_chunk_benign_error(png_ptr, "out of place"); 1162 png_chunk_benign_error(png_ptr, "out of place");
1123 return; 1163 return;
1124 } 1164 }
1125 1165
1126 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT)) 1166 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT) != 0)
1127 { 1167 {
1128 png_crc_finish(png_ptr, length); 1168 png_crc_finish(png_ptr, length);
1129 png_chunk_benign_error(png_ptr, "duplicate"); 1169 png_chunk_benign_error(png_ptr, "duplicate");
1130 return; 1170 return;
1131 } 1171 }
1132 1172
1133 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) 1173 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1174 {
1134 truelen = 3; 1175 truelen = 3;
1176 sample_depth = 8;
1177 }
1135 1178
1136 else 1179 else
1180 {
1137 truelen = png_ptr->channels; 1181 truelen = png_ptr->channels;
1182 sample_depth = png_ptr->bit_depth;
1183 }
1138 1184
1139 if (length != truelen || length > 4) 1185 if (length != truelen || length > 4)
1140 { 1186 {
1141 png_chunk_benign_error(png_ptr, "invalid"); 1187 png_chunk_benign_error(png_ptr, "invalid");
1142 png_crc_finish(png_ptr, length); 1188 png_crc_finish(png_ptr, length);
1143 return; 1189 return;
1144 } 1190 }
1145 1191
1192 buf[0] = buf[1] = buf[2] = buf[3] = sample_depth;
1146 png_crc_read(png_ptr, buf, truelen); 1193 png_crc_read(png_ptr, buf, truelen);
1147 1194
1148 if (png_crc_finish(png_ptr, 0)) 1195 if (png_crc_finish(png_ptr, 0) != 0)
1149 return; 1196 return;
1150 1197
1151 if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) 1198 for (i=0; i<truelen; ++i)
1199 {
1200 if (buf[i] == 0 || buf[i] > sample_depth)
1201 {
1202 png_chunk_benign_error(png_ptr, "invalid");
1203 return;
1204 }
1205 }
1206
1207 if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
1152 { 1208 {
1153 png_ptr->sig_bit.red = buf[0]; 1209 png_ptr->sig_bit.red = buf[0];
1154 png_ptr->sig_bit.green = buf[1]; 1210 png_ptr->sig_bit.green = buf[1];
1155 png_ptr->sig_bit.blue = buf[2]; 1211 png_ptr->sig_bit.blue = buf[2];
1156 png_ptr->sig_bit.alpha = buf[3]; 1212 png_ptr->sig_bit.alpha = buf[3];
1157 } 1213 }
1158 1214
1159 else 1215 else
1160 { 1216 {
1161 png_ptr->sig_bit.gray = buf[0]; 1217 png_ptr->sig_bit.gray = buf[0];
1162 png_ptr->sig_bit.red = buf[0]; 1218 png_ptr->sig_bit.red = buf[0];
1163 png_ptr->sig_bit.green = buf[0]; 1219 png_ptr->sig_bit.green = buf[0];
1164 png_ptr->sig_bit.blue = buf[0]; 1220 png_ptr->sig_bit.blue = buf[0];
1165 png_ptr->sig_bit.alpha = buf[1]; 1221 png_ptr->sig_bit.alpha = buf[1];
1166 } 1222 }
1167 1223
1168 png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit)); 1224 png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
1169 } 1225 }
1170 #endif 1226 #endif
1171 1227
1172 #ifdef PNG_READ_cHRM_SUPPORTED 1228 #ifdef PNG_READ_cHRM_SUPPORTED
1173 void /* PRIVATE */ 1229 void /* PRIVATE */
1174 png_handle_cHRM(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) 1230 png_handle_cHRM(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1175 { 1231 {
1176 png_byte buf[32]; 1232 png_byte buf[32];
1177 png_xy xy; 1233 png_xy xy;
1178 1234
1179 png_debug(1, "in png_handle_cHRM"); 1235 png_debug(1, "in png_handle_cHRM");
1180 1236
1181 if (!(png_ptr->mode & PNG_HAVE_IHDR)) 1237 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1182 png_chunk_error(png_ptr, "missing IHDR"); 1238 png_chunk_error(png_ptr, "missing IHDR");
1183 1239
1184 else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) 1240 else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
1185 { 1241 {
1186 png_crc_finish(png_ptr, length); 1242 png_crc_finish(png_ptr, length);
1187 png_chunk_benign_error(png_ptr, "out of place"); 1243 png_chunk_benign_error(png_ptr, "out of place");
1188 return; 1244 return;
1189 } 1245 }
1190 1246
1191 if (length != 32) 1247 if (length != 32)
1192 { 1248 {
1193 png_crc_finish(png_ptr, length); 1249 png_crc_finish(png_ptr, length);
1194 png_chunk_benign_error(png_ptr, "invalid"); 1250 png_chunk_benign_error(png_ptr, "invalid");
1195 return; 1251 return;
1196 } 1252 }
1197 1253
1198 png_crc_read(png_ptr, buf, 32); 1254 png_crc_read(png_ptr, buf, 32);
1199 1255
1200 if (png_crc_finish(png_ptr, 0)) 1256 if (png_crc_finish(png_ptr, 0) != 0)
1201 return; 1257 return;
1202 1258
1203 xy.whitex = png_get_fixed_point(NULL, buf); 1259 xy.whitex = png_get_fixed_point(NULL, buf);
1204 xy.whitey = png_get_fixed_point(NULL, buf + 4); 1260 xy.whitey = png_get_fixed_point(NULL, buf + 4);
1205 xy.redx = png_get_fixed_point(NULL, buf + 8); 1261 xy.redx = png_get_fixed_point(NULL, buf + 8);
1206 xy.redy = png_get_fixed_point(NULL, buf + 12); 1262 xy.redy = png_get_fixed_point(NULL, buf + 12);
1207 xy.greenx = png_get_fixed_point(NULL, buf + 16); 1263 xy.greenx = png_get_fixed_point(NULL, buf + 16);
1208 xy.greeny = png_get_fixed_point(NULL, buf + 20); 1264 xy.greeny = png_get_fixed_point(NULL, buf + 20);
1209 xy.bluex = png_get_fixed_point(NULL, buf + 24); 1265 xy.bluex = png_get_fixed_point(NULL, buf + 24);
1210 xy.bluey = png_get_fixed_point(NULL, buf + 28); 1266 xy.bluey = png_get_fixed_point(NULL, buf + 28);
1211 1267
1212 if (xy.whitex == PNG_FIXED_ERROR || 1268 if (xy.whitex == PNG_FIXED_ERROR ||
1213 xy.whitey == PNG_FIXED_ERROR || 1269 xy.whitey == PNG_FIXED_ERROR ||
1214 xy.redx == PNG_FIXED_ERROR || 1270 xy.redx == PNG_FIXED_ERROR ||
1215 xy.redy == PNG_FIXED_ERROR || 1271 xy.redy == PNG_FIXED_ERROR ||
1216 xy.greenx == PNG_FIXED_ERROR || 1272 xy.greenx == PNG_FIXED_ERROR ||
1217 xy.greeny == PNG_FIXED_ERROR || 1273 xy.greeny == PNG_FIXED_ERROR ||
1218 xy.bluex == PNG_FIXED_ERROR || 1274 xy.bluex == PNG_FIXED_ERROR ||
1219 xy.bluey == PNG_FIXED_ERROR) 1275 xy.bluey == PNG_FIXED_ERROR)
1220 { 1276 {
1221 png_chunk_benign_error(png_ptr, "invalid values"); 1277 png_chunk_benign_error(png_ptr, "invalid values");
1222 return; 1278 return;
1223 } 1279 }
1224 1280
1225 /* If a colorspace error has already been output skip this chunk */ 1281 /* If a colorspace error has already been output skip this chunk */
1226 if (png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) 1282 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0)
1227 return; 1283 return;
1228 1284
1229 if (png_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM) 1285 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM) != 0)
1230 { 1286 {
1231 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID; 1287 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1232 png_colorspace_sync(png_ptr, info_ptr); 1288 png_colorspace_sync(png_ptr, info_ptr);
1233 png_chunk_benign_error(png_ptr, "duplicate"); 1289 png_chunk_benign_error(png_ptr, "duplicate");
1234 return; 1290 return;
1235 } 1291 }
1236 1292
1237 png_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM; 1293 png_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM;
1238 (void)png_colorspace_set_chromaticities(png_ptr, &png_ptr->colorspace, &xy, 1294 (void)png_colorspace_set_chromaticities(png_ptr, &png_ptr->colorspace, &xy,
1239 1/*prefer cHRM values*/); 1295 1/*prefer cHRM values*/);
1240 png_colorspace_sync(png_ptr, info_ptr); 1296 png_colorspace_sync(png_ptr, info_ptr);
1241 } 1297 }
1242 #endif 1298 #endif
1243 1299
1244 #ifdef PNG_READ_sRGB_SUPPORTED 1300 #ifdef PNG_READ_sRGB_SUPPORTED
1245 void /* PRIVATE */ 1301 void /* PRIVATE */
1246 png_handle_sRGB(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) 1302 png_handle_sRGB(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1247 { 1303 {
1248 png_byte intent; 1304 png_byte intent;
1249 1305
1250 png_debug(1, "in png_handle_sRGB"); 1306 png_debug(1, "in png_handle_sRGB");
1251 1307
1252 if (!(png_ptr->mode & PNG_HAVE_IHDR)) 1308 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1253 png_chunk_error(png_ptr, "missing IHDR"); 1309 png_chunk_error(png_ptr, "missing IHDR");
1254 1310
1255 else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) 1311 else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
1256 { 1312 {
1257 png_crc_finish(png_ptr, length); 1313 png_crc_finish(png_ptr, length);
1258 png_chunk_benign_error(png_ptr, "out of place"); 1314 png_chunk_benign_error(png_ptr, "out of place");
1259 return; 1315 return;
1260 } 1316 }
1261 1317
1262 if (length != 1) 1318 if (length != 1)
1263 { 1319 {
1264 png_crc_finish(png_ptr, length); 1320 png_crc_finish(png_ptr, length);
1265 png_chunk_benign_error(png_ptr, "invalid"); 1321 png_chunk_benign_error(png_ptr, "invalid");
1266 return; 1322 return;
1267 } 1323 }
1268 1324
1269 png_crc_read(png_ptr, &intent, 1); 1325 png_crc_read(png_ptr, &intent, 1);
1270 1326
1271 if (png_crc_finish(png_ptr, 0)) 1327 if (png_crc_finish(png_ptr, 0) != 0)
1272 return; 1328 return;
1273 1329
1274 /* If a colorspace error has already been output skip this chunk */ 1330 /* If a colorspace error has already been output skip this chunk */
1275 if (png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) 1331 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0)
1276 return; 1332 return;
1277 1333
1278 /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect 1334 /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect
1279 * this. 1335 * this.
1280 */ 1336 */
1281 if (png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) 1337 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) != 0)
1282 { 1338 {
1283 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID; 1339 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1284 png_colorspace_sync(png_ptr, info_ptr); 1340 png_colorspace_sync(png_ptr, info_ptr);
1285 png_chunk_benign_error(png_ptr, "too many profiles"); 1341 png_chunk_benign_error(png_ptr, "too many profiles");
1286 return; 1342 return;
1287 } 1343 }
1288 1344
1289 (void)png_colorspace_set_sRGB(png_ptr, &png_ptr->colorspace, intent); 1345 (void)png_colorspace_set_sRGB(png_ptr, &png_ptr->colorspace, intent);
1290 png_colorspace_sync(png_ptr, info_ptr); 1346 png_colorspace_sync(png_ptr, info_ptr);
1291 } 1347 }
1292 #endif /* PNG_READ_sRGB_SUPPORTED */ 1348 #endif /* READ_sRGB */
1293 1349
1294 #ifdef PNG_READ_iCCP_SUPPORTED 1350 #ifdef PNG_READ_iCCP_SUPPORTED
1295 void /* PRIVATE */ 1351 void /* PRIVATE */
1296 png_handle_iCCP(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) 1352 png_handle_iCCP(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1297 /* Note: this does not properly handle profiles that are > 64K under DOS */ 1353 /* Note: this does not properly handle profiles that are > 64K under DOS */
1298 { 1354 {
1299 png_const_charp errmsg = NULL; /* error message output, or no error */ 1355 png_const_charp errmsg = NULL; /* error message output, or no error */
1300 int finished = 0; /* crc checked */ 1356 int finished = 0; /* crc checked */
1301 1357
1302 png_debug(1, "in png_handle_iCCP"); 1358 png_debug(1, "in png_handle_iCCP");
1303 1359
1304 if (!(png_ptr->mode & PNG_HAVE_IHDR)) 1360 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1305 png_chunk_error(png_ptr, "missing IHDR"); 1361 png_chunk_error(png_ptr, "missing IHDR");
1306 1362
1307 else if (png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) 1363 else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
1308 { 1364 {
1309 png_crc_finish(png_ptr, length); 1365 png_crc_finish(png_ptr, length);
1310 png_chunk_benign_error(png_ptr, "out of place"); 1366 png_chunk_benign_error(png_ptr, "out of place");
1311 return; 1367 return;
1312 } 1368 }
1313 1369
1314 /* Consistent with all the above colorspace handling an obviously *invalid* 1370 /* Consistent with all the above colorspace handling an obviously *invalid*
1315 * chunk is just ignored, so does not invalidate the color space. An 1371 * chunk is just ignored, so does not invalidate the color space. An
1316 * alternative is to set the 'invalid' flags at the start of this routine 1372 * alternative is to set the 'invalid' flags at the start of this routine
1317 * and only clear them in they were not set before and all the tests pass. 1373 * and only clear them in they were not set before and all the tests pass.
1318 * The minimum 'deflate' stream is assumed to be just the 2 byte header and 4 1374 * The minimum 'deflate' stream is assumed to be just the 2 byte header and
1319 * byte checksum. The keyword must be one character and there is a 1375 * 4 byte checksum. The keyword must be at least one character and there is
1320 * terminator (0) byte and the compression method. 1376 * a terminator (0) byte and the compression method.
1321 */ 1377 */
1322 if (length < 9) 1378 if (length < 9)
1323 { 1379 {
1324 png_crc_finish(png_ptr, length); 1380 png_crc_finish(png_ptr, length);
1325 png_chunk_benign_error(png_ptr, "too short"); 1381 png_chunk_benign_error(png_ptr, "too short");
1326 return; 1382 return;
1327 } 1383 }
1328 1384
1329 /* If a colorspace error has already been output skip this chunk */ 1385 /* If a colorspace error has already been output skip this chunk */
1330 if (png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) 1386 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0)
1331 { 1387 {
1332 png_crc_finish(png_ptr, length); 1388 png_crc_finish(png_ptr, length);
1333 return; 1389 return;
1334 } 1390 }
1335 1391
1336 /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect 1392 /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect
1337 * this. 1393 * this.
1338 */ 1394 */
1339 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) == 0) 1395 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) == 0)
1340 { 1396 {
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
1380 0/*finish: don't, because the output is too small*/); 1436 0/*finish: don't, because the output is too small*/);
1381 1437
1382 if (size == 0) 1438 if (size == 0)
1383 { 1439 {
1384 /* We have the ICC profile header; do the basic header checks. 1440 /* We have the ICC profile header; do the basic header checks.
1385 */ 1441 */
1386 const png_uint_32 profile_length = 1442 const png_uint_32 profile_length =
1387 png_get_uint_32(profile_header); 1443 png_get_uint_32(profile_header);
1388 1444
1389 if (png_icc_check_length(png_ptr, &png_ptr->colorspace, 1445 if (png_icc_check_length(png_ptr, &png_ptr->colorspace,
1390 keyword, profile_length)) 1446 keyword, profile_length) != 0)
1391 { 1447 {
1392 /* The length is apparently ok, so we can check the 132 1448 /* The length is apparently ok, so we can check the 132
1393 * byte header. 1449 * byte header.
1394 */ 1450 */
1395 if (png_icc_check_header(png_ptr, &png_ptr->colorspace, 1451 if (png_icc_check_header(png_ptr, &png_ptr->colorspace,
1396 keyword, profile_length, profile_header, 1452 keyword, profile_length, profile_header,
1397 png_ptr->color_type)) 1453 png_ptr->color_type) != 0)
1398 { 1454 {
1399 /* Now read the tag table; a variable size buffer is 1455 /* Now read the tag table; a variable size buffer is
1400 * needed at this point, allocate one for the whole 1456 * needed at this point, allocate one for the whole
1401 * profile. The header check has already validated 1457 * profile. The header check has already validated
1402 * that none of these stuff will overflow. 1458 * that none of these stuff will overflow.
1403 */ 1459 */
1404 const png_uint_32 tag_count = png_get_uint_32( 1460 const png_uint_32 tag_count = png_get_uint_32(
1405 profile_header+128); 1461 profile_header+128);
1406 png_bytep profile = png_read_buffer(png_ptr, 1462 png_bytep profile = png_read_buffer(png_ptr,
1407 profile_length, 2/*silent*/); 1463 profile_length, 2/*silent*/);
1408 1464
1409 if (profile != NULL) 1465 if (profile != NULL)
1410 { 1466 {
1411 memcpy(profile, profile_header, 1467 memcpy(profile, profile_header,
1412 (sizeof profile_header)); 1468 (sizeof profile_header));
1413 1469
1414 size = 12 * tag_count; 1470 size = 12 * tag_count;
1415 1471
1416 (void)png_inflate_read(png_ptr, local_buffer, 1472 (void)png_inflate_read(png_ptr, local_buffer,
1417 (sizeof local_buffer), &length, 1473 (sizeof local_buffer), &length,
1418 profile + (sizeof profile_header), &size, 0); 1474 profile + (sizeof profile_header), &size, 0);
1419 1475
1420 /* Still expect a a buffer error because we expect 1476 /* Still expect a buffer error because we expect
1421 * there to be some tag data! 1477 * there to be some tag data!
1422 */ 1478 */
1423 if (size == 0) 1479 if (size == 0)
1424 { 1480 {
1425 if (png_icc_check_tag_table(png_ptr, 1481 if (png_icc_check_tag_table(png_ptr,
1426 &png_ptr->colorspace, keyword, profile_length, 1482 &png_ptr->colorspace, keyword, profile_length,
1427 profile)) 1483 profile) != 0)
1428 { 1484 {
1429 /* The profile has been validated for basic 1485 /* The profile has been validated for basic
1430 * security issues, so read the whole thing in. 1486 * security issues, so read the whole thing in.
1431 */ 1487 */
1432 size = profile_length - (sizeof profile_header) 1488 size = profile_length - (sizeof profile_header)
1433 - 12 * tag_count; 1489 - 12 * tag_count;
1434 1490
1435 (void)png_inflate_read(png_ptr, local_buffer, 1491 (void)png_inflate_read(png_ptr, local_buffer,
1436 (sizeof local_buffer), &length, 1492 (sizeof local_buffer), &length,
1437 profile + (sizeof profile_header) + 1493 profile + (sizeof profile_header) +
(...skipping 12 matching lines...) Expand all
1450 * keep going. 1506 * keep going.
1451 */ 1507 */
1452 png_chunk_warning(png_ptr, 1508 png_chunk_warning(png_ptr,
1453 "extra compressed data"); 1509 "extra compressed data");
1454 } 1510 }
1455 1511
1456 png_crc_finish(png_ptr, length); 1512 png_crc_finish(png_ptr, length);
1457 finished = 1; 1513 finished = 1;
1458 1514
1459 # ifdef PNG_sRGB_SUPPORTED 1515 # ifdef PNG_sRGB_SUPPORTED
1460 /* Check for a match against sRGB */ 1516 /* Check for a match against sRGB */
1461 png_icc_set_sRGB(png_ptr, 1517 png_icc_set_sRGB(png_ptr,
1462 &png_ptr->colorspace, profile, 1518 &png_ptr->colorspace, profile,
1463 png_ptr->zstream.adler); 1519 png_ptr->zstream.adler);
1464 # endif 1520 # endif
1465 1521
1466 /* Steal the profile for info_ptr. */ 1522 /* Steal the profile for info_ptr. */
1467 if (info_ptr != NULL) 1523 if (info_ptr != NULL)
1468 { 1524 {
1469 png_free_data(png_ptr, info_ptr, 1525 png_free_data(png_ptr, info_ptr,
1470 PNG_FREE_ICCP, 0); 1526 PNG_FREE_ICCP, 0);
1471 1527
1472 info_ptr->iccp_name = png_voidcast(char*, 1528 info_ptr->iccp_name = png_voidcast(char*,
1473 png_malloc_base(png_ptr, 1529 png_malloc_base(png_ptr,
(...skipping 29 matching lines...) Expand all
1503 if (errmsg == NULL) 1559 if (errmsg == NULL)
1504 { 1560 {
1505 png_ptr->zowner = 0; 1561 png_ptr->zowner = 0;
1506 return; 1562 return;
1507 } 1563 }
1508 } 1564 }
1509 1565
1510 else if (size > 0) 1566 else if (size > 0)
1511 errmsg = "truncated"; 1567 errmsg = "truncated";
1512 1568
1569 #ifndef __COVERITY__
1513 else 1570 else
1514 errmsg = png_ptr->zstream.msg; 1571 errmsg = png_ptr->zstream.msg;
1572 #endif
1515 } 1573 }
1516 1574
1517 /* else png_icc_check_tag_table output an error */ 1575 /* else png_icc_check_tag_table output an error */
1518 } 1576 }
1519 1577
1520 else /* profile truncated */ 1578 else /* profile truncated */
1521 errmsg = png_ptr->zstream.msg; 1579 errmsg = png_ptr->zstream.msg;
1522 } 1580 }
1523 1581
1524 else 1582 else
(...skipping 22 matching lines...) Expand all
1547 } 1605 }
1548 1606
1549 else 1607 else
1550 errmsg = "bad keyword"; 1608 errmsg = "bad keyword";
1551 } 1609 }
1552 1610
1553 else 1611 else
1554 errmsg = "too many profiles"; 1612 errmsg = "too many profiles";
1555 1613
1556 /* Failure: the reason is in 'errmsg' */ 1614 /* Failure: the reason is in 'errmsg' */
1557 if (!finished) 1615 if (finished == 0)
1558 png_crc_finish(png_ptr, length); 1616 png_crc_finish(png_ptr, length);
1559 1617
1560 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID; 1618 png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1561 png_colorspace_sync(png_ptr, info_ptr); 1619 png_colorspace_sync(png_ptr, info_ptr);
1562 if (errmsg != NULL) /* else already output */ 1620 if (errmsg != NULL) /* else already output */
1563 png_chunk_benign_error(png_ptr, errmsg); 1621 png_chunk_benign_error(png_ptr, errmsg);
1564 } 1622 }
1565 #endif /* PNG_READ_iCCP_SUPPORTED */ 1623 #endif /* READ_iCCP */
1566 1624
1567 #ifdef PNG_READ_sPLT_SUPPORTED 1625 #ifdef PNG_READ_sPLT_SUPPORTED
1568 void /* PRIVATE */ 1626 void /* PRIVATE */
1569 png_handle_sPLT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) 1627 png_handle_sPLT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1570 /* Note: this does not properly handle chunks that are > 64K under DOS */ 1628 /* Note: this does not properly handle chunks that are > 64K under DOS */
1571 { 1629 {
1572 png_bytep entry_start, buffer; 1630 png_bytep entry_start, buffer;
1573 png_sPLT_t new_palette; 1631 png_sPLT_t new_palette;
1574 png_sPLT_entryp pp; 1632 png_sPLT_entryp pp;
1575 png_uint_32 data_length; 1633 png_uint_32 data_length;
(...skipping 15 matching lines...) Expand all
1591 1649
1592 if (--png_ptr->user_chunk_cache_max == 1) 1650 if (--png_ptr->user_chunk_cache_max == 1)
1593 { 1651 {
1594 png_warning(png_ptr, "No space in chunk cache for sPLT"); 1652 png_warning(png_ptr, "No space in chunk cache for sPLT");
1595 png_crc_finish(png_ptr, length); 1653 png_crc_finish(png_ptr, length);
1596 return; 1654 return;
1597 } 1655 }
1598 } 1656 }
1599 #endif 1657 #endif
1600 1658
1601 if (!(png_ptr->mode & PNG_HAVE_IHDR)) 1659 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1602 png_chunk_error(png_ptr, "missing IHDR"); 1660 png_chunk_error(png_ptr, "missing IHDR");
1603 1661
1604 else if (png_ptr->mode & PNG_HAVE_IDAT) 1662 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
1605 { 1663 {
1606 png_crc_finish(png_ptr, length); 1664 png_crc_finish(png_ptr, length);
1607 png_chunk_benign_error(png_ptr, "out of place"); 1665 png_chunk_benign_error(png_ptr, "out of place");
1608 return; 1666 return;
1609 } 1667 }
1610 1668
1611 #ifdef PNG_MAX_MALLOC_64K 1669 #ifdef PNG_MAX_MALLOC_64K
1612 if (length > 65535U) 1670 if (length > 65535U)
1613 { 1671 {
1614 png_crc_finish(png_ptr, length); 1672 png_crc_finish(png_ptr, length);
(...skipping 10 matching lines...) Expand all
1625 return; 1683 return;
1626 } 1684 }
1627 1685
1628 1686
1629 /* WARNING: this may break if size_t is less than 32 bits; it is assumed 1687 /* WARNING: this may break if size_t is less than 32 bits; it is assumed
1630 * that the PNG_MAX_MALLOC_64K test is enabled in this case, but this is a 1688 * that the PNG_MAX_MALLOC_64K test is enabled in this case, but this is a
1631 * potential breakage point if the types in pngconf.h aren't exactly right. 1689 * potential breakage point if the types in pngconf.h aren't exactly right.
1632 */ 1690 */
1633 png_crc_read(png_ptr, buffer, length); 1691 png_crc_read(png_ptr, buffer, length);
1634 1692
1635 if (png_crc_finish(png_ptr, skip)) 1693 if (png_crc_finish(png_ptr, skip) != 0)
1636 return; 1694 return;
1637 1695
1638 buffer[length] = 0; 1696 buffer[length] = 0;
1639 1697
1640 for (entry_start = buffer; *entry_start; entry_start++) 1698 for (entry_start = buffer; *entry_start; entry_start++)
1641 /* Empty loop to find end of name */ ; 1699 /* Empty loop to find end of name */ ;
1642 1700
1643 ++entry_start; 1701 ++entry_start;
1644 1702
1645 /* A sample depth should follow the separator, and we should be on it */ 1703 /* A sample depth should follow the separator, and we should be on it */
1646 if (entry_start > buffer + length - 2) 1704 if (length < 2U || entry_start > buffer + (length - 2U))
1647 { 1705 {
1648 png_warning(png_ptr, "malformed sPLT chunk"); 1706 png_warning(png_ptr, "malformed sPLT chunk");
1649 return; 1707 return;
1650 } 1708 }
1651 1709
1652 new_palette.depth = *entry_start++; 1710 new_palette.depth = *entry_start++;
1653 entry_size = (new_palette.depth == 8 ? 6 : 10); 1711 entry_size = (new_palette.depth == 8 ? 6 : 10);
1654 /* This must fit in a png_uint_32 because it is derived from the original 1712 /* This must fit in a png_uint_32 because it is derived from the original
1655 * chunk data length. 1713 * chunk data length.
1656 */ 1714 */
1657 data_length = length - (png_uint_32)(entry_start - buffer); 1715 data_length = length - (png_uint_32)(entry_start - buffer);
1658 1716
1659 /* Integrity-check the data length */ 1717 /* Integrity-check the data length */
1660 if (data_length % entry_size) 1718 if ((data_length % entry_size) != 0)
1661 { 1719 {
1662 png_warning(png_ptr, "sPLT chunk has bad length"); 1720 png_warning(png_ptr, "sPLT chunk has bad length");
1663 return; 1721 return;
1664 } 1722 }
1665 1723
1666 dl = (png_int_32)(data_length / entry_size); 1724 dl = (png_int_32)(data_length / entry_size);
1667 max_dl = PNG_SIZE_MAX / (sizeof (png_sPLT_entry)); 1725 max_dl = PNG_SIZE_MAX / (sizeof (png_sPLT_entry));
1668 1726
1669 if (dl > max_dl) 1727 if (dl > max_dl)
1670 { 1728 {
1671 png_warning(png_ptr, "sPLT chunk too long"); 1729 png_warning(png_ptr, "sPLT chunk too long");
1672 return; 1730 return;
1673 } 1731 }
1674 1732
1675 new_palette.nentries = (png_int_32)(data_length / entry_size); 1733 new_palette.nentries = (png_int_32)(data_length / entry_size);
1676 1734
1677 new_palette.entries = (png_sPLT_entryp)png_malloc_warn( 1735 new_palette.entries = (png_sPLT_entryp)png_malloc_warn(
1678 png_ptr, new_palette.nentries * (sizeof (png_sPLT_entry))); 1736 png_ptr, new_palette.nentries * (sizeof (png_sPLT_entry)));
1679 1737
1680 if (new_palette.entries == NULL) 1738 if (new_palette.entries == NULL)
1681 { 1739 {
1682 png_warning(png_ptr, "sPLT chunk requires too much memory"); 1740 png_warning(png_ptr, "sPLT chunk requires too much memory");
1683 return; 1741 return;
1684 } 1742 }
1685 1743
1686 #ifdef PNG_POINTER_INDEXING_SUPPORTED 1744 #ifdef PNG_POINTER_INDEXING_SUPPORTED
1687 for (i = 0; i < new_palette.nentries; i++) 1745 for (i = 0; i < new_palette.nentries; i++)
1688 { 1746 {
1689 pp = new_palette.entries + i; 1747 pp = new_palette.entries + i;
1690 1748
1691 if (new_palette.depth == 8) 1749 if (new_palette.depth == 8)
1692 { 1750 {
1693 pp->red = *entry_start++; 1751 pp->red = *entry_start++;
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
1732 } 1790 }
1733 #endif 1791 #endif
1734 1792
1735 /* Discard all chunk data except the name and stash that */ 1793 /* Discard all chunk data except the name and stash that */
1736 new_palette.name = (png_charp)buffer; 1794 new_palette.name = (png_charp)buffer;
1737 1795
1738 png_set_sPLT(png_ptr, info_ptr, &new_palette, 1); 1796 png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);
1739 1797
1740 png_free(png_ptr, new_palette.entries); 1798 png_free(png_ptr, new_palette.entries);
1741 } 1799 }
1742 #endif /* PNG_READ_sPLT_SUPPORTED */ 1800 #endif /* READ_sPLT */
1743 1801
1744 #ifdef PNG_READ_tRNS_SUPPORTED 1802 #ifdef PNG_READ_tRNS_SUPPORTED
1745 void /* PRIVATE */ 1803 void /* PRIVATE */
1746 png_handle_tRNS(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) 1804 png_handle_tRNS(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1747 { 1805 {
1748 png_byte readbuf[PNG_MAX_PALETTE_LENGTH]; 1806 png_byte readbuf[PNG_MAX_PALETTE_LENGTH];
1749 1807
1750 png_debug(1, "in png_handle_tRNS"); 1808 png_debug(1, "in png_handle_tRNS");
1751 1809
1752 if (!(png_ptr->mode & PNG_HAVE_IHDR)) 1810 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1753 png_chunk_error(png_ptr, "missing IHDR"); 1811 png_chunk_error(png_ptr, "missing IHDR");
1754 1812
1755 else if (png_ptr->mode & PNG_HAVE_IDAT) 1813 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
1756 { 1814 {
1757 png_crc_finish(png_ptr, length); 1815 png_crc_finish(png_ptr, length);
1758 png_chunk_benign_error(png_ptr, "out of place"); 1816 png_chunk_benign_error(png_ptr, "out of place");
1759 return; 1817 return;
1760 } 1818 }
1761 1819
1762 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS)) 1820 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS) != 0)
1763 { 1821 {
1764 png_crc_finish(png_ptr, length); 1822 png_crc_finish(png_ptr, length);
1765 png_chunk_benign_error(png_ptr, "duplicate"); 1823 png_chunk_benign_error(png_ptr, "duplicate");
1766 return; 1824 return;
1767 } 1825 }
1768 1826
1769 if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) 1827 if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
1770 { 1828 {
1771 png_byte buf[2]; 1829 png_byte buf[2];
1772 1830
(...skipping 22 matching lines...) Expand all
1795 1853
1796 png_crc_read(png_ptr, buf, length); 1854 png_crc_read(png_ptr, buf, length);
1797 png_ptr->num_trans = 1; 1855 png_ptr->num_trans = 1;
1798 png_ptr->trans_color.red = png_get_uint_16(buf); 1856 png_ptr->trans_color.red = png_get_uint_16(buf);
1799 png_ptr->trans_color.green = png_get_uint_16(buf + 2); 1857 png_ptr->trans_color.green = png_get_uint_16(buf + 2);
1800 png_ptr->trans_color.blue = png_get_uint_16(buf + 4); 1858 png_ptr->trans_color.blue = png_get_uint_16(buf + 4);
1801 } 1859 }
1802 1860
1803 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) 1861 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1804 { 1862 {
1805 if (!(png_ptr->mode & PNG_HAVE_PLTE)) 1863 if ((png_ptr->mode & PNG_HAVE_PLTE) == 0)
1806 { 1864 {
1807 /* TODO: is this actually an error in the ISO spec? */ 1865 /* TODO: is this actually an error in the ISO spec? */
1808 png_crc_finish(png_ptr, length); 1866 png_crc_finish(png_ptr, length);
1809 png_chunk_benign_error(png_ptr, "out of place"); 1867 png_chunk_benign_error(png_ptr, "out of place");
1810 return; 1868 return;
1811 } 1869 }
1812 1870
1813 if (length > png_ptr->num_palette || length > PNG_MAX_PALETTE_LENGTH || 1871 if (length > (unsigned int) png_ptr->num_palette ||
1872 length > (unsigned int) PNG_MAX_PALETTE_LENGTH ||
1814 length == 0) 1873 length == 0)
1815 { 1874 {
1816 png_crc_finish(png_ptr, length); 1875 png_crc_finish(png_ptr, length);
1817 png_chunk_benign_error(png_ptr, "invalid"); 1876 png_chunk_benign_error(png_ptr, "invalid");
1818 return; 1877 return;
1819 } 1878 }
1820 1879
1821 png_crc_read(png_ptr, readbuf, length); 1880 png_crc_read(png_ptr, readbuf, length);
1822 png_ptr->num_trans = (png_uint_16)length; 1881 png_ptr->num_trans = (png_uint_16)length;
1823 } 1882 }
1824 1883
1825 else 1884 else
1826 { 1885 {
1827 png_crc_finish(png_ptr, length); 1886 png_crc_finish(png_ptr, length);
1828 png_chunk_benign_error(png_ptr, "invalid with alpha channel"); 1887 png_chunk_benign_error(png_ptr, "invalid with alpha channel");
1829 return; 1888 return;
1830 } 1889 }
1831 1890
1832 if (png_crc_finish(png_ptr, 0)) 1891 if (png_crc_finish(png_ptr, 0) != 0)
1833 { 1892 {
1834 png_ptr->num_trans = 0; 1893 png_ptr->num_trans = 0;
1835 return; 1894 return;
1836 } 1895 }
1837 1896
1838 /* TODO: this is a horrible side effect in the palette case because the 1897 /* TODO: this is a horrible side effect in the palette case because the
1839 * png_struct ends up with a pointer to the tRNS buffer owned by the 1898 * png_struct ends up with a pointer to the tRNS buffer owned by the
1840 * png_info. Fix this. 1899 * png_info. Fix this.
1841 */ 1900 */
1842 png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans, 1901 png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,
1843 &(png_ptr->trans_color)); 1902 &(png_ptr->trans_color));
1844 } 1903 }
1845 #endif 1904 #endif
1846 1905
1847 #ifdef PNG_READ_bKGD_SUPPORTED 1906 #ifdef PNG_READ_bKGD_SUPPORTED
1848 void /* PRIVATE */ 1907 void /* PRIVATE */
1849 png_handle_bKGD(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) 1908 png_handle_bKGD(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1850 { 1909 {
1851 unsigned int truelen; 1910 unsigned int truelen;
1852 png_byte buf[6]; 1911 png_byte buf[6];
1853 png_color_16 background; 1912 png_color_16 background;
1854 1913
1855 png_debug(1, "in png_handle_bKGD"); 1914 png_debug(1, "in png_handle_bKGD");
1856 1915
1857 if (!(png_ptr->mode & PNG_HAVE_IHDR)) 1916 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1858 png_chunk_error(png_ptr, "missing IHDR"); 1917 png_chunk_error(png_ptr, "missing IHDR");
1859 1918
1860 else if ((png_ptr->mode & PNG_HAVE_IDAT) || 1919 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0 ||
1861 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && 1920 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
1862 !(png_ptr->mode & PNG_HAVE_PLTE))) 1921 (png_ptr->mode & PNG_HAVE_PLTE) == 0))
1863 { 1922 {
1864 png_crc_finish(png_ptr, length); 1923 png_crc_finish(png_ptr, length);
1865 png_chunk_benign_error(png_ptr, "out of place"); 1924 png_chunk_benign_error(png_ptr, "out of place");
1866 return; 1925 return;
1867 } 1926 }
1868 1927
1869 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD)) 1928 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) != 0)
1870 { 1929 {
1871 png_crc_finish(png_ptr, length); 1930 png_crc_finish(png_ptr, length);
1872 png_chunk_benign_error(png_ptr, "duplicate"); 1931 png_chunk_benign_error(png_ptr, "duplicate");
1873 return; 1932 return;
1874 } 1933 }
1875 1934
1876 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) 1935 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1877 truelen = 1; 1936 truelen = 1;
1878 1937
1879 else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) 1938 else if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
1880 truelen = 6; 1939 truelen = 6;
1881 1940
1882 else 1941 else
1883 truelen = 2; 1942 truelen = 2;
1884 1943
1885 if (length != truelen) 1944 if (length != truelen)
1886 { 1945 {
1887 png_crc_finish(png_ptr, length); 1946 png_crc_finish(png_ptr, length);
1888 png_chunk_benign_error(png_ptr, "invalid"); 1947 png_chunk_benign_error(png_ptr, "invalid");
1889 return; 1948 return;
1890 } 1949 }
1891 1950
1892 png_crc_read(png_ptr, buf, truelen); 1951 png_crc_read(png_ptr, buf, truelen);
1893 1952
1894 if (png_crc_finish(png_ptr, 0)) 1953 if (png_crc_finish(png_ptr, 0) != 0)
1895 return; 1954 return;
1896 1955
1897 /* We convert the index value into RGB components so that we can allow 1956 /* We convert the index value into RGB components so that we can allow
1898 * arbitrary RGB values for background when we have transparency, and 1957 * arbitrary RGB values for background when we have transparency, and
1899 * so it is easy to determine the RGB values of the background color 1958 * so it is easy to determine the RGB values of the background color
1900 * from the info_ptr struct. 1959 * from the info_ptr struct.
1901 */ 1960 */
1902 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) 1961 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1903 { 1962 {
1904 background.index = buf[0]; 1963 background.index = buf[0];
1905 1964
1906 if (info_ptr && info_ptr->num_palette) 1965 if (info_ptr != NULL && info_ptr->num_palette != 0)
1907 { 1966 {
1908 if (buf[0] >= info_ptr->num_palette) 1967 if (buf[0] >= info_ptr->num_palette)
1909 { 1968 {
1910 png_chunk_benign_error(png_ptr, "invalid index"); 1969 png_chunk_benign_error(png_ptr, "invalid index");
1911 return; 1970 return;
1912 } 1971 }
1913 1972
1914 background.red = (png_uint_16)png_ptr->palette[buf[0]].red; 1973 background.red = (png_uint_16)png_ptr->palette[buf[0]].red;
1915 background.green = (png_uint_16)png_ptr->palette[buf[0]].green; 1974 background.green = (png_uint_16)png_ptr->palette[buf[0]].green;
1916 background.blue = (png_uint_16)png_ptr->palette[buf[0]].blue; 1975 background.blue = (png_uint_16)png_ptr->palette[buf[0]].blue;
1917 } 1976 }
1918 1977
1919 else 1978 else
1920 background.red = background.green = background.blue = 0; 1979 background.red = background.green = background.blue = 0;
1921 1980
1922 background.gray = 0; 1981 background.gray = 0;
1923 } 1982 }
1924 1983
1925 else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) /* GRAY */ 1984 else if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0) /* GRAY */
1926 { 1985 {
1927 background.index = 0; 1986 background.index = 0;
1928 background.red = 1987 background.red =
1929 background.green = 1988 background.green =
1930 background.blue = 1989 background.blue =
1931 background.gray = png_get_uint_16(buf); 1990 background.gray = png_get_uint_16(buf);
1932 } 1991 }
1933 1992
1934 else 1993 else
1935 { 1994 {
(...skipping 10 matching lines...) Expand all
1946 2005
1947 #ifdef PNG_READ_hIST_SUPPORTED 2006 #ifdef PNG_READ_hIST_SUPPORTED
1948 void /* PRIVATE */ 2007 void /* PRIVATE */
1949 png_handle_hIST(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) 2008 png_handle_hIST(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1950 { 2009 {
1951 unsigned int num, i; 2010 unsigned int num, i;
1952 png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH]; 2011 png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];
1953 2012
1954 png_debug(1, "in png_handle_hIST"); 2013 png_debug(1, "in png_handle_hIST");
1955 2014
1956 if (!(png_ptr->mode & PNG_HAVE_IHDR)) 2015 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1957 png_chunk_error(png_ptr, "missing IHDR"); 2016 png_chunk_error(png_ptr, "missing IHDR");
1958 2017
1959 else if ((png_ptr->mode & PNG_HAVE_IDAT) || !(png_ptr->mode & PNG_HAVE_PLTE)) 2018 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0 ||
2019 (png_ptr->mode & PNG_HAVE_PLTE) == 0)
1960 { 2020 {
1961 png_crc_finish(png_ptr, length); 2021 png_crc_finish(png_ptr, length);
1962 png_chunk_benign_error(png_ptr, "out of place"); 2022 png_chunk_benign_error(png_ptr, "out of place");
1963 return; 2023 return;
1964 } 2024 }
1965 2025
1966 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST)) 2026 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) != 0)
1967 { 2027 {
1968 png_crc_finish(png_ptr, length); 2028 png_crc_finish(png_ptr, length);
1969 png_chunk_benign_error(png_ptr, "duplicate"); 2029 png_chunk_benign_error(png_ptr, "duplicate");
1970 return; 2030 return;
1971 } 2031 }
1972 2032
1973 num = length / 2 ; 2033 num = length / 2 ;
1974 2034
1975 if (num != png_ptr->num_palette || num > PNG_MAX_PALETTE_LENGTH) 2035 if (num != (unsigned int) png_ptr->num_palette ||
2036 num > (unsigned int) PNG_MAX_PALETTE_LENGTH)
1976 { 2037 {
1977 png_crc_finish(png_ptr, length); 2038 png_crc_finish(png_ptr, length);
1978 png_chunk_benign_error(png_ptr, "invalid"); 2039 png_chunk_benign_error(png_ptr, "invalid");
1979 return; 2040 return;
1980 } 2041 }
1981 2042
1982 for (i = 0; i < num; i++) 2043 for (i = 0; i < num; i++)
1983 { 2044 {
1984 png_byte buf[2]; 2045 png_byte buf[2];
1985 2046
1986 png_crc_read(png_ptr, buf, 2); 2047 png_crc_read(png_ptr, buf, 2);
1987 readbuf[i] = png_get_uint_16(buf); 2048 readbuf[i] = png_get_uint_16(buf);
1988 } 2049 }
1989 2050
1990 if (png_crc_finish(png_ptr, 0)) 2051 if (png_crc_finish(png_ptr, 0) != 0)
1991 return; 2052 return;
1992 2053
1993 png_set_hIST(png_ptr, info_ptr, readbuf); 2054 png_set_hIST(png_ptr, info_ptr, readbuf);
1994 } 2055 }
1995 #endif 2056 #endif
1996 2057
1997 #ifdef PNG_READ_pHYs_SUPPORTED 2058 #ifdef PNG_READ_pHYs_SUPPORTED
1998 void /* PRIVATE */ 2059 void /* PRIVATE */
1999 png_handle_pHYs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) 2060 png_handle_pHYs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2000 { 2061 {
2001 png_byte buf[9]; 2062 png_byte buf[9];
2002 png_uint_32 res_x, res_y; 2063 png_uint_32 res_x, res_y;
2003 int unit_type; 2064 int unit_type;
2004 2065
2005 png_debug(1, "in png_handle_pHYs"); 2066 png_debug(1, "in png_handle_pHYs");
2006 2067
2007 if (!(png_ptr->mode & PNG_HAVE_IHDR)) 2068 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2008 png_chunk_error(png_ptr, "missing IHDR"); 2069 png_chunk_error(png_ptr, "missing IHDR");
2009 2070
2010 else if (png_ptr->mode & PNG_HAVE_IDAT) 2071 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2011 { 2072 {
2012 png_crc_finish(png_ptr, length); 2073 png_crc_finish(png_ptr, length);
2013 png_chunk_benign_error(png_ptr, "out of place"); 2074 png_chunk_benign_error(png_ptr, "out of place");
2014 return; 2075 return;
2015 } 2076 }
2016 2077
2017 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs)) 2078 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs) != 0)
2018 { 2079 {
2019 png_crc_finish(png_ptr, length); 2080 png_crc_finish(png_ptr, length);
2020 png_chunk_benign_error(png_ptr, "duplicate"); 2081 png_chunk_benign_error(png_ptr, "duplicate");
2021 return; 2082 return;
2022 } 2083 }
2023 2084
2024 if (length != 9) 2085 if (length != 9)
2025 { 2086 {
2026 png_crc_finish(png_ptr, length); 2087 png_crc_finish(png_ptr, length);
2027 png_chunk_benign_error(png_ptr, "invalid"); 2088 png_chunk_benign_error(png_ptr, "invalid");
2028 return; 2089 return;
2029 } 2090 }
2030 2091
2031 png_crc_read(png_ptr, buf, 9); 2092 png_crc_read(png_ptr, buf, 9);
2032 2093
2033 if (png_crc_finish(png_ptr, 0)) 2094 if (png_crc_finish(png_ptr, 0) != 0)
2034 return; 2095 return;
2035 2096
2036 res_x = png_get_uint_32(buf); 2097 res_x = png_get_uint_32(buf);
2037 res_y = png_get_uint_32(buf + 4); 2098 res_y = png_get_uint_32(buf + 4);
2038 unit_type = buf[8]; 2099 unit_type = buf[8];
2039 png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type); 2100 png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
2040 } 2101 }
2041 #endif 2102 #endif
2042 2103
2043 #ifdef PNG_READ_oFFs_SUPPORTED 2104 #ifdef PNG_READ_oFFs_SUPPORTED
2044 void /* PRIVATE */ 2105 void /* PRIVATE */
2045 png_handle_oFFs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) 2106 png_handle_oFFs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2046 { 2107 {
2047 png_byte buf[9]; 2108 png_byte buf[9];
2048 png_int_32 offset_x, offset_y; 2109 png_int_32 offset_x, offset_y;
2049 int unit_type; 2110 int unit_type;
2050 2111
2051 png_debug(1, "in png_handle_oFFs"); 2112 png_debug(1, "in png_handle_oFFs");
2052 2113
2053 if (!(png_ptr->mode & PNG_HAVE_IHDR)) 2114 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2054 png_chunk_error(png_ptr, "missing IHDR"); 2115 png_chunk_error(png_ptr, "missing IHDR");
2055 2116
2056 else if (png_ptr->mode & PNG_HAVE_IDAT) 2117 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2057 { 2118 {
2058 png_crc_finish(png_ptr, length); 2119 png_crc_finish(png_ptr, length);
2059 png_chunk_benign_error(png_ptr, "out of place"); 2120 png_chunk_benign_error(png_ptr, "out of place");
2060 return; 2121 return;
2061 } 2122 }
2062 2123
2063 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs)) 2124 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs) != 0)
2064 { 2125 {
2065 png_crc_finish(png_ptr, length); 2126 png_crc_finish(png_ptr, length);
2066 png_chunk_benign_error(png_ptr, "duplicate"); 2127 png_chunk_benign_error(png_ptr, "duplicate");
2067 return; 2128 return;
2068 } 2129 }
2069 2130
2070 if (length != 9) 2131 if (length != 9)
2071 { 2132 {
2072 png_crc_finish(png_ptr, length); 2133 png_crc_finish(png_ptr, length);
2073 png_chunk_benign_error(png_ptr, "invalid"); 2134 png_chunk_benign_error(png_ptr, "invalid");
2074 return; 2135 return;
2075 } 2136 }
2076 2137
2077 png_crc_read(png_ptr, buf, 9); 2138 png_crc_read(png_ptr, buf, 9);
2078 2139
2079 if (png_crc_finish(png_ptr, 0)) 2140 if (png_crc_finish(png_ptr, 0) != 0)
2080 return; 2141 return;
2081 2142
2082 offset_x = png_get_int_32(buf); 2143 offset_x = png_get_int_32(buf);
2083 offset_y = png_get_int_32(buf + 4); 2144 offset_y = png_get_int_32(buf + 4);
2084 unit_type = buf[8]; 2145 unit_type = buf[8];
2085 png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type); 2146 png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
2086 } 2147 }
2087 #endif 2148 #endif
2088 2149
2089 #ifdef PNG_READ_pCAL_SUPPORTED 2150 #ifdef PNG_READ_pCAL_SUPPORTED
2090 /* Read the pCAL chunk (described in the PNG Extensions document) */ 2151 /* Read the pCAL chunk (described in the PNG Extensions document) */
2091 void /* PRIVATE */ 2152 void /* PRIVATE */
2092 png_handle_pCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) 2153 png_handle_pCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2093 { 2154 {
2094 png_int_32 X0, X1; 2155 png_int_32 X0, X1;
2095 png_byte type, nparams; 2156 png_byte type, nparams;
2096 png_bytep buffer, buf, units, endptr; 2157 png_bytep buffer, buf, units, endptr;
2097 png_charpp params; 2158 png_charpp params;
2098 int i; 2159 int i;
2099 2160
2100 png_debug(1, "in png_handle_pCAL"); 2161 png_debug(1, "in png_handle_pCAL");
2101 2162
2102 if (!(png_ptr->mode & PNG_HAVE_IHDR)) 2163 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2103 png_chunk_error(png_ptr, "missing IHDR"); 2164 png_chunk_error(png_ptr, "missing IHDR");
2104 2165
2105 else if (png_ptr->mode & PNG_HAVE_IDAT) 2166 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2106 { 2167 {
2107 png_crc_finish(png_ptr, length); 2168 png_crc_finish(png_ptr, length);
2108 png_chunk_benign_error(png_ptr, "out of place"); 2169 png_chunk_benign_error(png_ptr, "out of place");
2109 return; 2170 return;
2110 } 2171 }
2111 2172
2112 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL)) 2173 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL) != 0)
2113 { 2174 {
2114 png_crc_finish(png_ptr, length); 2175 png_crc_finish(png_ptr, length);
2115 png_chunk_benign_error(png_ptr, "duplicate"); 2176 png_chunk_benign_error(png_ptr, "duplicate");
2116 return; 2177 return;
2117 } 2178 }
2118 2179
2119 png_debug1(2, "Allocating and reading pCAL chunk data (%u bytes)", 2180 png_debug1(2, "Allocating and reading pCAL chunk data (%u bytes)",
2120 length + 1); 2181 length + 1);
2121 2182
2122 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/); 2183 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
2123 2184
2124 if (buffer == NULL) 2185 if (buffer == NULL)
2125 { 2186 {
2126 png_crc_finish(png_ptr, length); 2187 png_crc_finish(png_ptr, length);
2127 png_chunk_benign_error(png_ptr, "out of memory"); 2188 png_chunk_benign_error(png_ptr, "out of memory");
2128 return; 2189 return;
2129 } 2190 }
2130 2191
2131 png_crc_read(png_ptr, buffer, length); 2192 png_crc_read(png_ptr, buffer, length);
2132 2193
2133 if (png_crc_finish(png_ptr, 0)) 2194 if (png_crc_finish(png_ptr, 0) != 0)
2134 return; 2195 return;
2135 2196
2136 buffer[length] = 0; /* Null terminate the last string */ 2197 buffer[length] = 0; /* Null terminate the last string */
2137 2198
2138 png_debug(3, "Finding end of pCAL purpose string"); 2199 png_debug(3, "Finding end of pCAL purpose string");
2139 for (buf = buffer; *buf; buf++) 2200 for (buf = buffer; *buf; buf++)
2140 /* Empty loop */ ; 2201 /* Empty loop */ ;
2141 2202
2142 endptr = buffer + length; 2203 endptr = buffer + length;
2143 2204
2144 /* We need to have at least 12 bytes after the purpose string 2205 /* We need to have at least 12 bytes after the purpose string
2145 * in order to get the parameter information. 2206 * in order to get the parameter information.
2146 */ 2207 */
2147 if (endptr <= buf + 12) 2208 if (endptr - buf <= 12)
2148 { 2209 {
2149 png_chunk_benign_error(png_ptr, "invalid"); 2210 png_chunk_benign_error(png_ptr, "invalid");
2150 return; 2211 return;
2151 } 2212 }
2152 2213
2153 png_debug(3, "Reading pCAL X0, X1, type, nparams, and units"); 2214 png_debug(3, "Reading pCAL X0, X1, type, nparams, and units");
2154 X0 = png_get_int_32((png_bytep)buf+1); 2215 X0 = png_get_int_32((png_bytep)buf+1);
2155 X1 = png_get_int_32((png_bytep)buf+5); 2216 X1 = png_get_int_32((png_bytep)buf+5);
2156 type = buf[9]; 2217 type = buf[9];
2157 nparams = buf[10]; 2218 nparams = buf[10];
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
2219 /* Read the sCAL chunk */ 2280 /* Read the sCAL chunk */
2220 void /* PRIVATE */ 2281 void /* PRIVATE */
2221 png_handle_sCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) 2282 png_handle_sCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2222 { 2283 {
2223 png_bytep buffer; 2284 png_bytep buffer;
2224 png_size_t i; 2285 png_size_t i;
2225 int state; 2286 int state;
2226 2287
2227 png_debug(1, "in png_handle_sCAL"); 2288 png_debug(1, "in png_handle_sCAL");
2228 2289
2229 if (!(png_ptr->mode & PNG_HAVE_IHDR)) 2290 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2230 png_chunk_error(png_ptr, "missing IHDR"); 2291 png_chunk_error(png_ptr, "missing IHDR");
2231 2292
2232 else if (png_ptr->mode & PNG_HAVE_IDAT) 2293 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2233 { 2294 {
2234 png_crc_finish(png_ptr, length); 2295 png_crc_finish(png_ptr, length);
2235 png_chunk_benign_error(png_ptr, "out of place"); 2296 png_chunk_benign_error(png_ptr, "out of place");
2236 return; 2297 return;
2237 } 2298 }
2238 2299
2239 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL)) 2300 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL) != 0)
2240 { 2301 {
2241 png_crc_finish(png_ptr, length); 2302 png_crc_finish(png_ptr, length);
2242 png_chunk_benign_error(png_ptr, "duplicate"); 2303 png_chunk_benign_error(png_ptr, "duplicate");
2243 return; 2304 return;
2244 } 2305 }
2245 2306
2246 /* Need unit type, width, \0, height: minimum 4 bytes */ 2307 /* Need unit type, width, \0, height: minimum 4 bytes */
2247 else if (length < 4) 2308 else if (length < 4)
2248 { 2309 {
2249 png_crc_finish(png_ptr, length); 2310 png_crc_finish(png_ptr, length);
2250 png_chunk_benign_error(png_ptr, "invalid"); 2311 png_chunk_benign_error(png_ptr, "invalid");
2251 return; 2312 return;
2252 } 2313 }
2253 2314
2254 png_debug1(2, "Allocating and reading sCAL chunk data (%u bytes)", 2315 png_debug1(2, "Allocating and reading sCAL chunk data (%u bytes)",
2255 length + 1); 2316 length + 1);
2256 2317
2257 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/); 2318 buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
2258 2319
2259 if (buffer == NULL) 2320 if (buffer == NULL)
2260 { 2321 {
2261 png_chunk_benign_error(png_ptr, "out of memory"); 2322 png_chunk_benign_error(png_ptr, "out of memory");
2262 png_crc_finish(png_ptr, length); 2323 png_crc_finish(png_ptr, length);
2263 return; 2324 return;
2264 } 2325 }
2265 2326
2266 png_crc_read(png_ptr, buffer, length); 2327 png_crc_read(png_ptr, buffer, length);
2267 buffer[length] = 0; /* Null terminate the last string */ 2328 buffer[length] = 0; /* Null terminate the last string */
2268 2329
2269 if (png_crc_finish(png_ptr, 0)) 2330 if (png_crc_finish(png_ptr, 0) != 0)
2270 return; 2331 return;
2271 2332
2272 /* Validate the unit. */ 2333 /* Validate the unit. */
2273 if (buffer[0] != 1 && buffer[0] != 2) 2334 if (buffer[0] != 1 && buffer[0] != 2)
2274 { 2335 {
2275 png_chunk_benign_error(png_ptr, "invalid unit"); 2336 png_chunk_benign_error(png_ptr, "invalid unit");
2276 return; 2337 return;
2277 } 2338 }
2278 2339
2279 /* Validate the ASCII numbers, need two ASCII numbers separated by 2340 /* Validate the ASCII numbers, need two ASCII numbers separated by
2280 * a '\0' and they need to fit exactly in the chunk data. 2341 * a '\0' and they need to fit exactly in the chunk data.
2281 */ 2342 */
2282 i = 1; 2343 i = 1;
2283 state = 0; 2344 state = 0;
2284 2345
2285 if (!png_check_fp_number((png_const_charp)buffer, length, &state, &i) || 2346 if (png_check_fp_number((png_const_charp)buffer, length, &state, &i) == 0 ||
2286 i >= length || buffer[i++] != 0) 2347 i >= length || buffer[i++] != 0)
2287 png_chunk_benign_error(png_ptr, "bad width format"); 2348 png_chunk_benign_error(png_ptr, "bad width format");
2288 2349
2289 else if (!PNG_FP_IS_POSITIVE(state)) 2350 else if (PNG_FP_IS_POSITIVE(state) == 0)
2290 png_chunk_benign_error(png_ptr, "non-positive width"); 2351 png_chunk_benign_error(png_ptr, "non-positive width");
2291 2352
2292 else 2353 else
2293 { 2354 {
2294 png_size_t heighti = i; 2355 png_size_t heighti = i;
2295 2356
2296 state = 0; 2357 state = 0;
2297 if (!png_check_fp_number((png_const_charp)buffer, length, &state, &i) || 2358 if (png_check_fp_number((png_const_charp)buffer, length,
2298 i != length) 2359 &state, &i) == 0 || i != length)
2299 png_chunk_benign_error(png_ptr, "bad height format"); 2360 png_chunk_benign_error(png_ptr, "bad height format");
2300 2361
2301 else if (!PNG_FP_IS_POSITIVE(state)) 2362 else if (PNG_FP_IS_POSITIVE(state) == 0)
2302 png_chunk_benign_error(png_ptr, "non-positive height"); 2363 png_chunk_benign_error(png_ptr, "non-positive height");
2303 2364
2304 else 2365 else
2305 /* This is the (only) success case. */ 2366 /* This is the (only) success case. */
2306 png_set_sCAL_s(png_ptr, info_ptr, buffer[0], 2367 png_set_sCAL_s(png_ptr, info_ptr, buffer[0],
2307 (png_charp)buffer+1, (png_charp)buffer+heighti); 2368 (png_charp)buffer+1, (png_charp)buffer+heighti);
2308 } 2369 }
2309 } 2370 }
2310 #endif 2371 #endif
2311 2372
2312 #ifdef PNG_READ_tIME_SUPPORTED 2373 #ifdef PNG_READ_tIME_SUPPORTED
2313 void /* PRIVATE */ 2374 void /* PRIVATE */
2314 png_handle_tIME(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) 2375 png_handle_tIME(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2315 { 2376 {
2316 png_byte buf[7]; 2377 png_byte buf[7];
2317 png_time mod_time; 2378 png_time mod_time;
2318 2379
2319 png_debug(1, "in png_handle_tIME"); 2380 png_debug(1, "in png_handle_tIME");
2320 2381
2321 if (!(png_ptr->mode & PNG_HAVE_IHDR)) 2382 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2322 png_chunk_error(png_ptr, "missing IHDR"); 2383 png_chunk_error(png_ptr, "missing IHDR");
2323 2384
2324 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME)) 2385 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME) != 0)
2325 { 2386 {
2326 png_crc_finish(png_ptr, length); 2387 png_crc_finish(png_ptr, length);
2327 png_chunk_benign_error(png_ptr, "duplicate"); 2388 png_chunk_benign_error(png_ptr, "duplicate");
2328 return; 2389 return;
2329 } 2390 }
2330 2391
2331 if (png_ptr->mode & PNG_HAVE_IDAT) 2392 if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2332 png_ptr->mode |= PNG_AFTER_IDAT; 2393 png_ptr->mode |= PNG_AFTER_IDAT;
2333 2394
2334 if (length != 7) 2395 if (length != 7)
2335 { 2396 {
2336 png_crc_finish(png_ptr, length); 2397 png_crc_finish(png_ptr, length);
2337 png_chunk_benign_error(png_ptr, "invalid"); 2398 png_chunk_benign_error(png_ptr, "invalid");
2338 return; 2399 return;
2339 } 2400 }
2340 2401
2341 png_crc_read(png_ptr, buf, 7); 2402 png_crc_read(png_ptr, buf, 7);
2342 2403
2343 if (png_crc_finish(png_ptr, 0)) 2404 if (png_crc_finish(png_ptr, 0) != 0)
2344 return; 2405 return;
2345 2406
2346 mod_time.second = buf[6]; 2407 mod_time.second = buf[6];
2347 mod_time.minute = buf[5]; 2408 mod_time.minute = buf[5];
2348 mod_time.hour = buf[4]; 2409 mod_time.hour = buf[4];
2349 mod_time.day = buf[3]; 2410 mod_time.day = buf[3];
2350 mod_time.month = buf[2]; 2411 mod_time.month = buf[2];
2351 mod_time.year = png_get_uint_16(buf); 2412 mod_time.year = png_get_uint_16(buf);
2352 2413
2353 png_set_tIME(png_ptr, info_ptr, &mod_time); 2414 png_set_tIME(png_ptr, info_ptr, &mod_time);
(...skipping 24 matching lines...) Expand all
2378 2439
2379 if (--png_ptr->user_chunk_cache_max == 1) 2440 if (--png_ptr->user_chunk_cache_max == 1)
2380 { 2441 {
2381 png_crc_finish(png_ptr, length); 2442 png_crc_finish(png_ptr, length);
2382 png_chunk_benign_error(png_ptr, "no space in chunk cache"); 2443 png_chunk_benign_error(png_ptr, "no space in chunk cache");
2383 return; 2444 return;
2384 } 2445 }
2385 } 2446 }
2386 #endif 2447 #endif
2387 2448
2388 if (!(png_ptr->mode & PNG_HAVE_IHDR)) 2449 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2389 png_chunk_error(png_ptr, "missing IHDR"); 2450 png_chunk_error(png_ptr, "missing IHDR");
2390 2451
2391 if (png_ptr->mode & PNG_HAVE_IDAT) 2452 if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2392 png_ptr->mode |= PNG_AFTER_IDAT; 2453 png_ptr->mode |= PNG_AFTER_IDAT;
2393 2454
2394 #ifdef PNG_MAX_MALLOC_64K 2455 #ifdef PNG_MAX_MALLOC_64K
2395 if (length > 65535U) 2456 if (length > 65535U)
2396 { 2457 {
2397 png_crc_finish(png_ptr, length); 2458 png_crc_finish(png_ptr, length);
2398 png_chunk_benign_error(png_ptr, "too large to fit in memory"); 2459 png_chunk_benign_error(png_ptr, "too large to fit in memory");
2399 return; 2460 return;
2400 } 2461 }
2401 #endif 2462 #endif
2402 2463
2403 buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/); 2464 buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
2404 2465
2405 if (buffer == NULL) 2466 if (buffer == NULL)
2406 { 2467 {
2407 png_chunk_benign_error(png_ptr, "out of memory"); 2468 png_chunk_benign_error(png_ptr, "out of memory");
2408 return; 2469 return;
2409 } 2470 }
2410 2471
2411 png_crc_read(png_ptr, buffer, length); 2472 png_crc_read(png_ptr, buffer, length);
2412 2473
2413 if (png_crc_finish(png_ptr, skip)) 2474 if (png_crc_finish(png_ptr, skip) != 0)
2414 return; 2475 return;
2415 2476
2416 key = (png_charp)buffer; 2477 key = (png_charp)buffer;
2417 key[length] = 0; 2478 key[length] = 0;
2418 2479
2419 for (text = key; *text; text++) 2480 for (text = key; *text; text++)
2420 /* Empty loop to find end of key */ ; 2481 /* Empty loop to find end of key */ ;
2421 2482
2422 if (text != key + length) 2483 if (text != key + length)
2423 text++; 2484 text++;
2424 2485
2425 text_info.compression = PNG_TEXT_COMPRESSION_NONE; 2486 text_info.compression = PNG_TEXT_COMPRESSION_NONE;
2426 text_info.key = key; 2487 text_info.key = key;
2427 text_info.lang = NULL; 2488 text_info.lang = NULL;
2428 text_info.lang_key = NULL; 2489 text_info.lang_key = NULL;
2429 text_info.itxt_length = 0; 2490 text_info.itxt_length = 0;
2430 text_info.text = text; 2491 text_info.text = text;
2431 text_info.text_length = strlen(text); 2492 text_info.text_length = strlen(text);
2432 2493
2433 if (png_set_text_2(png_ptr, info_ptr, &text_info, 1)) 2494 if (png_set_text_2(png_ptr, info_ptr, &text_info, 1) != 0)
2434 png_warning(png_ptr, "Insufficient memory to process text chunk"); 2495 png_warning(png_ptr, "Insufficient memory to process text chunk");
2435 } 2496 }
2436 #endif 2497 #endif
2437 2498
2438 #ifdef PNG_READ_zTXt_SUPPORTED 2499 #ifdef PNG_READ_zTXt_SUPPORTED
2439 /* Note: this does not correctly handle chunks that are > 64K under DOS */ 2500 /* Note: this does not correctly handle chunks that are > 64K under DOS */
2440 void /* PRIVATE */ 2501 void /* PRIVATE */
2441 png_handle_zTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) 2502 png_handle_zTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2442 { 2503 {
2443 png_const_charp errmsg = NULL; 2504 png_const_charp errmsg = NULL;
(...skipping 13 matching lines...) Expand all
2457 2518
2458 if (--png_ptr->user_chunk_cache_max == 1) 2519 if (--png_ptr->user_chunk_cache_max == 1)
2459 { 2520 {
2460 png_crc_finish(png_ptr, length); 2521 png_crc_finish(png_ptr, length);
2461 png_chunk_benign_error(png_ptr, "no space in chunk cache"); 2522 png_chunk_benign_error(png_ptr, "no space in chunk cache");
2462 return; 2523 return;
2463 } 2524 }
2464 } 2525 }
2465 #endif 2526 #endif
2466 2527
2467 if (!(png_ptr->mode & PNG_HAVE_IHDR)) 2528 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2468 png_chunk_error(png_ptr, "missing IHDR"); 2529 png_chunk_error(png_ptr, "missing IHDR");
2469 2530
2470 if (png_ptr->mode & PNG_HAVE_IDAT) 2531 if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2471 png_ptr->mode |= PNG_AFTER_IDAT; 2532 png_ptr->mode |= PNG_AFTER_IDAT;
2472 2533
2473 buffer = png_read_buffer(png_ptr, length, 2/*silent*/); 2534 buffer = png_read_buffer(png_ptr, length, 2/*silent*/);
2474 2535
2475 if (buffer == NULL) 2536 if (buffer == NULL)
2476 { 2537 {
2477 png_crc_finish(png_ptr, length); 2538 png_crc_finish(png_ptr, length);
2478 png_chunk_benign_error(png_ptr, "out of memory"); 2539 png_chunk_benign_error(png_ptr, "out of memory");
2479 return; 2540 return;
2480 } 2541 }
2481 2542
2482 png_crc_read(png_ptr, buffer, length); 2543 png_crc_read(png_ptr, buffer, length);
2483 2544
2484 if (png_crc_finish(png_ptr, 0)) 2545 if (png_crc_finish(png_ptr, 0) != 0)
2485 return; 2546 return;
2486 2547
2487 /* TODO: also check that the keyword contents match the spec! */ 2548 /* TODO: also check that the keyword contents match the spec! */
2488 for (keyword_length = 0; 2549 for (keyword_length = 0;
2489 keyword_length < length && buffer[keyword_length] != 0; 2550 keyword_length < length && buffer[keyword_length] != 0;
2490 ++keyword_length) 2551 ++keyword_length)
2491 /* Empty loop to find end of name */ ; 2552 /* Empty loop to find end of name */ ;
2492 2553
2493 if (keyword_length > 79 || keyword_length < 1) 2554 if (keyword_length > 79 || keyword_length < 1)
2494 errmsg = "bad keyword"; 2555 errmsg = "bad keyword";
(...skipping 29 matching lines...) Expand all
2524 buffer[uncompressed_length+(keyword_length+2)] = 0; 2585 buffer[uncompressed_length+(keyword_length+2)] = 0;
2525 2586
2526 text.compression = PNG_TEXT_COMPRESSION_zTXt; 2587 text.compression = PNG_TEXT_COMPRESSION_zTXt;
2527 text.key = (png_charp)buffer; 2588 text.key = (png_charp)buffer;
2528 text.text = (png_charp)(buffer + keyword_length+2); 2589 text.text = (png_charp)(buffer + keyword_length+2);
2529 text.text_length = uncompressed_length; 2590 text.text_length = uncompressed_length;
2530 text.itxt_length = 0; 2591 text.itxt_length = 0;
2531 text.lang = NULL; 2592 text.lang = NULL;
2532 text.lang_key = NULL; 2593 text.lang_key = NULL;
2533 2594
2534 if (png_set_text_2(png_ptr, info_ptr, &text, 1)) 2595 if (png_set_text_2(png_ptr, info_ptr, &text, 1) != 0)
2535 errmsg = "insufficient memory"; 2596 errmsg = "insufficient memory";
2536 } 2597 }
2537 2598
2538 else 2599 else
2539 errmsg = png_ptr->zstream.msg; 2600 errmsg = png_ptr->zstream.msg;
2540 } 2601 }
2541 2602
2542 if (errmsg != NULL) 2603 if (errmsg != NULL)
2543 png_chunk_benign_error(png_ptr, errmsg); 2604 png_chunk_benign_error(png_ptr, errmsg);
2544 } 2605 }
(...skipping 21 matching lines...) Expand all
2566 2627
2567 if (--png_ptr->user_chunk_cache_max == 1) 2628 if (--png_ptr->user_chunk_cache_max == 1)
2568 { 2629 {
2569 png_crc_finish(png_ptr, length); 2630 png_crc_finish(png_ptr, length);
2570 png_chunk_benign_error(png_ptr, "no space in chunk cache"); 2631 png_chunk_benign_error(png_ptr, "no space in chunk cache");
2571 return; 2632 return;
2572 } 2633 }
2573 } 2634 }
2574 #endif 2635 #endif
2575 2636
2576 if (!(png_ptr->mode & PNG_HAVE_IHDR)) 2637 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2577 png_chunk_error(png_ptr, "missing IHDR"); 2638 png_chunk_error(png_ptr, "missing IHDR");
2578 2639
2579 if (png_ptr->mode & PNG_HAVE_IDAT) 2640 if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2580 png_ptr->mode |= PNG_AFTER_IDAT; 2641 png_ptr->mode |= PNG_AFTER_IDAT;
2581 2642
2582 buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/); 2643 buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
2583 2644
2584 if (buffer == NULL) 2645 if (buffer == NULL)
2585 { 2646 {
2586 png_crc_finish(png_ptr, length); 2647 png_crc_finish(png_ptr, length);
2587 png_chunk_benign_error(png_ptr, "out of memory"); 2648 png_chunk_benign_error(png_ptr, "out of memory");
2588 return; 2649 return;
2589 } 2650 }
2590 2651
2591 png_crc_read(png_ptr, buffer, length); 2652 png_crc_read(png_ptr, buffer, length);
2592 2653
2593 if (png_crc_finish(png_ptr, 0)) 2654 if (png_crc_finish(png_ptr, 0) != 0)
2594 return; 2655 return;
2595 2656
2596 /* First the keyword. */ 2657 /* First the keyword. */
2597 for (prefix_length=0; 2658 for (prefix_length=0;
2598 prefix_length < length && buffer[prefix_length] != 0; 2659 prefix_length < length && buffer[prefix_length] != 0;
2599 ++prefix_length) 2660 ++prefix_length)
2600 /* Empty loop */ ; 2661 /* Empty loop */ ;
2601 2662
2602 /* Perform a basic check on the keyword length here. */ 2663 /* Perform a basic check on the keyword length here. */
2603 if (prefix_length > 79 || prefix_length < 1) 2664 if (prefix_length > 79 || prefix_length < 1)
(...skipping 25 matching lines...) Expand all
2629 /* WARNING: the length may be invalid here, this is checked below. */ 2690 /* WARNING: the length may be invalid here, this is checked below. */
2630 translated_keyword_offset = ++prefix_length; 2691 translated_keyword_offset = ++prefix_length;
2631 2692
2632 for (; prefix_length < length && buffer[prefix_length] != 0; 2693 for (; prefix_length < length && buffer[prefix_length] != 0;
2633 ++prefix_length) 2694 ++prefix_length)
2634 /* Empty loop */ ; 2695 /* Empty loop */ ;
2635 2696
2636 /* prefix_length should now be at the trailing '\0' of the translated 2697 /* prefix_length should now be at the trailing '\0' of the translated
2637 * keyword, but it may already be over the end. None of this arithmetic 2698 * keyword, but it may already be over the end. None of this arithmetic
2638 * can overflow because chunks are at most 2^31 bytes long, but on 16-bit 2699 * can overflow because chunks are at most 2^31 bytes long, but on 16-bit
2639 * systems the available allocaton may overflow. 2700 * systems the available allocation may overflow.
2640 */ 2701 */
2641 ++prefix_length; 2702 ++prefix_length;
2642 2703
2643 if (!compressed && prefix_length <= length) 2704 if (compressed == 0 && prefix_length <= length)
2644 uncompressed_length = length - prefix_length; 2705 uncompressed_length = length - prefix_length;
2645 2706
2646 else if (compressed && prefix_length < length) 2707 else if (compressed != 0 && prefix_length < length)
2647 { 2708 {
2648 uncompressed_length = PNG_SIZE_MAX; 2709 uncompressed_length = PNG_SIZE_MAX;
2649 2710
2650 /* TODO: at present png_decompress_chunk imposes a single application 2711 /* TODO: at present png_decompress_chunk imposes a single application
2651 * level memory limit, this should be split to different values for 2712 * level memory limit, this should be split to different values for
2652 * iCCP and text chunks. 2713 * iCCP and text chunks.
2653 */ 2714 */
2654 if (png_decompress_chunk(png_ptr, length, prefix_length, 2715 if (png_decompress_chunk(png_ptr, length, prefix_length,
2655 &uncompressed_length, 1/*terminate*/) == Z_STREAM_END) 2716 &uncompressed_length, 1/*terminate*/) == Z_STREAM_END)
2656 buffer = png_ptr->read_buffer; 2717 buffer = png_ptr->read_buffer;
2657 2718
2658 else 2719 else
2659 errmsg = png_ptr->zstream.msg; 2720 errmsg = png_ptr->zstream.msg;
2660 } 2721 }
2661 2722
2662 else 2723 else
2663 errmsg = "truncated"; 2724 errmsg = "truncated";
2664 2725
2665 if (errmsg == NULL) 2726 if (errmsg == NULL)
2666 { 2727 {
2667 png_text text; 2728 png_text text;
2668 2729
2669 buffer[uncompressed_length+prefix_length] = 0; 2730 buffer[uncompressed_length+prefix_length] = 0;
2670 2731
2671 if (compressed) 2732 if (compressed == 0)
2672 text.compression = PNG_ITXT_COMPRESSION_NONE; 2733 text.compression = PNG_ITXT_COMPRESSION_NONE;
2673 2734
2674 else 2735 else
2675 text.compression = PNG_ITXT_COMPRESSION_zTXt; 2736 text.compression = PNG_ITXT_COMPRESSION_zTXt;
2676 2737
2677 text.key = (png_charp)buffer; 2738 text.key = (png_charp)buffer;
2678 text.lang = (png_charp)buffer + language_offset; 2739 text.lang = (png_charp)buffer + language_offset;
2679 text.lang_key = (png_charp)buffer + translated_keyword_offset; 2740 text.lang_key = (png_charp)buffer + translated_keyword_offset;
2680 text.text = (png_charp)buffer + prefix_length; 2741 text.text = (png_charp)buffer + prefix_length;
2681 text.text_length = 0; 2742 text.text_length = 0;
2682 text.itxt_length = uncompressed_length; 2743 text.itxt_length = uncompressed_length;
2683 2744
2684 if (png_set_text_2(png_ptr, info_ptr, &text, 1)) 2745 if (png_set_text_2(png_ptr, info_ptr, &text, 1) != 0)
2685 errmsg = "insufficient memory"; 2746 errmsg = "insufficient memory";
2686 } 2747 }
2687 } 2748 }
2688 2749
2689 else 2750 else
2690 errmsg = "bad compression info"; 2751 errmsg = "bad compression info";
2691 2752
2692 if (errmsg != NULL) 2753 if (errmsg != NULL)
2693 png_chunk_benign_error(png_ptr, errmsg); 2754 png_chunk_benign_error(png_ptr, errmsg);
2694 } 2755 }
2695 #endif 2756 #endif
2696 2757
2697 #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED 2758 #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
2698 /* Utility function for png_handle_unknown; set up png_ptr::unknown_chunk */ 2759 /* Utility function for png_handle_unknown; set up png_ptr::unknown_chunk */
2699 static int 2760 static int
2700 png_cache_unknown_chunk(png_structrp png_ptr, png_uint_32 length) 2761 png_cache_unknown_chunk(png_structrp png_ptr, png_uint_32 length)
2701 { 2762 {
2702 png_alloc_size_t limit = PNG_SIZE_MAX; 2763 png_alloc_size_t limit = PNG_SIZE_MAX;
2703 2764
2704 if (png_ptr->unknown_chunk.data != NULL) 2765 if (png_ptr->unknown_chunk.data != NULL)
2705 { 2766 {
2706 png_free(png_ptr, png_ptr->unknown_chunk.data); 2767 png_free(png_ptr, png_ptr->unknown_chunk.data);
2707 png_ptr->unknown_chunk.data = NULL; 2768 png_ptr->unknown_chunk.data = NULL;
2708 } 2769 }
2709 2770
2710 # ifdef PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED 2771 # ifdef PNG_SET_USER_LIMITS_SUPPORTED
2711 if (png_ptr->user_chunk_malloc_max > 0 && 2772 if (png_ptr->user_chunk_malloc_max > 0 &&
2712 png_ptr->user_chunk_malloc_max < limit) 2773 png_ptr->user_chunk_malloc_max < limit)
2713 limit = png_ptr->user_chunk_malloc_max; 2774 limit = png_ptr->user_chunk_malloc_max;
2714 2775
2715 # elif PNG_USER_CHUNK_MALLOC_MAX > 0 2776 # elif PNG_USER_CHUNK_MALLOC_MAX > 0
2716 if (PNG_USER_CHUNK_MALLOC_MAX < limit) 2777 if (PNG_USER_CHUNK_MALLOC_MAX < limit)
2717 limit = PNG_USER_CHUNK_MALLOC_MAX; 2778 limit = PNG_USER_CHUNK_MALLOC_MAX;
2718 # endif 2779 # endif
2719 2780
2720 if (length <= limit) 2781 if (length <= limit)
2721 { 2782 {
2722 PNG_CSTRING_FROM_CHUNK(png_ptr->unknown_chunk.name, png_ptr->chunk_name); 2783 PNG_CSTRING_FROM_CHUNK(png_ptr->unknown_chunk.name, png_ptr->chunk_name);
2723 /* The following is safe because of the PNG_SIZE_MAX init above */ 2784 /* The following is safe because of the PNG_SIZE_MAX init above */
2724 png_ptr->unknown_chunk.size = (png_size_t)length/*SAFE*/; 2785 png_ptr->unknown_chunk.size = (png_size_t)length/*SAFE*/;
2725 /* 'mode' is a flag array, only the bottom four bits matter here */ 2786 /* 'mode' is a flag array, only the bottom four bits matter here */
2726 png_ptr->unknown_chunk.location = (png_byte)png_ptr->mode/*SAFE*/; 2787 png_ptr->unknown_chunk.location = (png_byte)png_ptr->mode/*SAFE*/;
2727 2788
(...skipping 17 matching lines...) Expand all
2745 } 2806 }
2746 2807
2747 else 2808 else
2748 { 2809 {
2749 if (length > 0) 2810 if (length > 0)
2750 png_crc_read(png_ptr, png_ptr->unknown_chunk.data, length); 2811 png_crc_read(png_ptr, png_ptr->unknown_chunk.data, length);
2751 png_crc_finish(png_ptr, 0); 2812 png_crc_finish(png_ptr, 0);
2752 return 1; 2813 return 1;
2753 } 2814 }
2754 } 2815 }
2755 #endif /* PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */ 2816 #endif /* READ_UNKNOWN_CHUNKS */
2756 2817
2757 /* Handle an unknown, or known but disabled, chunk */ 2818 /* Handle an unknown, or known but disabled, chunk */
2758 void /* PRIVATE */ 2819 void /* PRIVATE */
2759 png_handle_unknown(png_structrp png_ptr, png_inforp info_ptr, 2820 png_handle_unknown(png_structrp png_ptr, png_inforp info_ptr,
2760 png_uint_32 length, int keep) 2821 png_uint_32 length, int keep)
2761 { 2822 {
2762 int handled = 0; /* the chunk was handled */ 2823 int handled = 0; /* the chunk was handled */
2763 2824
2764 png_debug(1, "in png_handle_unknown"); 2825 png_debug(1, "in png_handle_unknown");
2765 2826
2766 #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED 2827 #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
2767 /* NOTE: this code is based on the code in libpng-1.4.12 except for fixing 2828 /* NOTE: this code is based on the code in libpng-1.4.12 except for fixing
2768 * the bug which meant that setting a non-default behavior for a specific 2829 * the bug which meant that setting a non-default behavior for a specific
2769 * chunk would be ignored (the default was always used unless a user 2830 * chunk would be ignored (the default was always used unless a user
2770 * callback was installed). 2831 * callback was installed).
2771 * 2832 *
2772 * 'keep' is the value from the png_chunk_unknown_handling, the setting for 2833 * 'keep' is the value from the png_chunk_unknown_handling, the setting for
2773 * this specific chunk_name, if PNG_HANDLE_AS_UNKNOWN_SUPPORTED, if not it 2834 * this specific chunk_name, if PNG_HANDLE_AS_UNKNOWN_SUPPORTED, if not it
2774 * will always be PNG_HANDLE_CHUNK_AS_DEFAULT and it needs to be set here. 2835 * will always be PNG_HANDLE_CHUNK_AS_DEFAULT and it needs to be set here.
2775 * This is just an optimization to avoid multiple calls to the lookup 2836 * This is just an optimization to avoid multiple calls to the lookup
2776 * function. 2837 * function.
2777 */ 2838 */
2778 # ifndef PNG_HANDLE_AS_UNKNOWN_SUPPORTED 2839 # ifndef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
2779 # ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED 2840 # ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
2780 keep = png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name); 2841 keep = png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name);
2781 # endif 2842 # endif
2782 # endif 2843 # endif
2783 2844
2784 /* One of the following methods will read the chunk or skip it (at least one 2845 /* One of the following methods will read the chunk or skip it (at least one
2785 * of these is always defined because this is the only way to switch on 2846 * of these is always defined because this is the only way to switch on
2786 * PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) 2847 * PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
2787 */ 2848 */
2788 # ifdef PNG_READ_USER_CHUNKS_SUPPORTED 2849 # ifdef PNG_READ_USER_CHUNKS_SUPPORTED
2789 /* The user callback takes precedence over the chunk keep value, but the 2850 /* The user callback takes precedence over the chunk keep value, but the
2790 * keep value is still required to validate a save of a critical chunk. 2851 * keep value is still required to validate a save of a critical chunk.
2791 */ 2852 */
2792 if (png_ptr->read_user_chunk_fn != NULL) 2853 if (png_ptr->read_user_chunk_fn != NULL)
2793 { 2854 {
2794 if (png_cache_unknown_chunk(png_ptr, length)) 2855 if (png_cache_unknown_chunk(png_ptr, length) != 0)
2856 {
2857 /* Callback to user unknown chunk handler */
2858 int ret = (*(png_ptr->read_user_chunk_fn))(png_ptr,
2859 &png_ptr->unknown_chunk);
2860
2861 /* ret is:
2862 * negative: An error occurred; png_chunk_error will be called.
2863 * zero: The chunk was not handled, the chunk will be discarded
2864 * unless png_set_keep_unknown_chunks has been used to set
2865 * a 'keep' behavior for this particular chunk, in which
2866 * case that will be used. A critical chunk will cause an
2867 * error at this point unless it is to be saved.
2868 * positive: The chunk was handled, libpng will ignore/discard it.
2869 */
2870 if (ret < 0)
2871 png_chunk_error(png_ptr, "error in user chunk");
2872
2873 else if (ret == 0)
2795 { 2874 {
2796 /* Callback to user unknown chunk handler */ 2875 /* If the keep value is 'default' or 'never' override it, but
2797 int ret = (*(png_ptr->read_user_chunk_fn))(png_ptr, 2876 * still error out on critical chunks unless the keep value is
2798 &png_ptr->unknown_chunk); 2877 * 'always' While this is weird it is the behavior in 1.4.12.
2799 2878 * A possible improvement would be to obey the value set for the
2800 /* ret is: 2879 * chunk, but this would be an API change that would probably
2801 * negative: An error occured, png_chunk_error will be called. 2880 * damage some applications.
2802 * zero: The chunk was not handled, the chunk will be discarded 2881 *
2803 * unless png_set_keep_unknown_chunks has been used to set 2882 * The png_app_warning below catches the case that matters, where
2804 * a 'keep' behavior for this particular chunk, in which 2883 * the application has not set specific save or ignore for this
2805 * case that will be used. A critical chunk will cause an 2884 * chunk or global save or ignore.
2806 * error at this point unless it is to be saved.
2807 * positive: The chunk was handled, libpng will ignore/discard it.
2808 */ 2885 */
2809 if (ret < 0) 2886 if (keep < PNG_HANDLE_CHUNK_IF_SAFE)
2810 png_chunk_error(png_ptr, "error in user chunk");
2811
2812 else if (ret == 0)
2813 { 2887 {
2814 /* If the keep value is 'default' or 'never' override it, but 2888 # ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
2815 * still error out on critical chunks unless the keep value is 2889 if (png_ptr->unknown_default < PNG_HANDLE_CHUNK_IF_SAFE)
2816 * 'always' While this is weird it is the behavior in 1.4.12.
2817 * A possible improvement would be to obey the value set for the
2818 * chunk, but this would be an API change that would probably
2819 * damage some applications.
2820 *
2821 * The png_app_warning below catches the case that matters, where
2822 * the application has not set specific save or ignore for this
2823 * chunk or global save or ignore.
2824 */
2825 if (keep < PNG_HANDLE_CHUNK_IF_SAFE)
2826 { 2890 {
2827 # ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED 2891 png_chunk_warning(png_ptr, "Saving unknown chunk:");
2828 if (png_ptr->unknown_default < PNG_HANDLE_CHUNK_IF_SAFE) 2892 png_app_warning(png_ptr,
2829 { 2893 "forcing save of an unhandled chunk;"
2830 png_chunk_warning(png_ptr, "Saving unknown chunk:"); 2894 " please call png_set_keep_unknown_chunks");
2831 png_app_warning(png_ptr, 2895 /* with keep = PNG_HANDLE_CHUNK_IF_SAFE */
2832 "forcing save of an unhandled chunk;"
2833 " please call png_set_keep_unknown_chunks");
2834 /* with keep = PNG_HANDLE_CHUNK_IF_SAFE */
2835 }
2836 # endif
2837 keep = PNG_HANDLE_CHUNK_IF_SAFE;
2838 } 2896 }
2839 } 2897 # endif
2840 2898 keep = PNG_HANDLE_CHUNK_IF_SAFE;
2841 else /* chunk was handled */
2842 {
2843 handled = 1;
2844 /* Critical chunks can be safely discarded at this point. */
2845 keep = PNG_HANDLE_CHUNK_NEVER;
2846 } 2899 }
2847 } 2900 }
2848 2901
2849 else 2902 else /* chunk was handled */
2850 keep = PNG_HANDLE_CHUNK_NEVER; /* insufficient memory */ 2903 {
2904 handled = 1;
2905 /* Critical chunks can be safely discarded at this point. */
2906 keep = PNG_HANDLE_CHUNK_NEVER;
2907 }
2851 } 2908 }
2852 2909
2853 else 2910 else
2854 /* Use the SAVE_UNKNOWN_CHUNKS code or skip the chunk */ 2911 keep = PNG_HANDLE_CHUNK_NEVER; /* insufficient memory */
2855 # endif /* PNG_READ_USER_CHUNKS_SUPPORTED */ 2912 }
2913
2914 else
2915 /* Use the SAVE_UNKNOWN_CHUNKS code or skip the chunk */
2916 # endif /* READ_USER_CHUNKS */
2856 2917
2857 # ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED 2918 # ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
2858 { 2919 {
2859 /* keep is currently just the per-chunk setting, if there was no 2920 /* keep is currently just the per-chunk setting, if there was no
2860 * setting change it to the global default now (not that this may 2921 * setting change it to the global default now (not that this may
2861 * still be AS_DEFAULT) then obtain the cache of the chunk if required, 2922 * still be AS_DEFAULT) then obtain the cache of the chunk if required,
2862 * if not simply skip the chunk. 2923 * if not simply skip the chunk.
2863 */ 2924 */
2864 if (keep == PNG_HANDLE_CHUNK_AS_DEFAULT) 2925 if (keep == PNG_HANDLE_CHUNK_AS_DEFAULT)
2865 keep = png_ptr->unknown_default; 2926 keep = png_ptr->unknown_default;
2866 2927
2867 if (keep == PNG_HANDLE_CHUNK_ALWAYS || 2928 if (keep == PNG_HANDLE_CHUNK_ALWAYS ||
2868 (keep == PNG_HANDLE_CHUNK_IF_SAFE && 2929 (keep == PNG_HANDLE_CHUNK_IF_SAFE &&
2869 PNG_CHUNK_ANCILLARY(png_ptr->chunk_name))) 2930 PNG_CHUNK_ANCILLARY(png_ptr->chunk_name)))
2870 { 2931 {
2871 if (!png_cache_unknown_chunk(png_ptr, length)) 2932 if (png_cache_unknown_chunk(png_ptr, length) == 0)
2872 keep = PNG_HANDLE_CHUNK_NEVER; 2933 keep = PNG_HANDLE_CHUNK_NEVER;
2873 } 2934 }
2874 2935
2875 else 2936 else
2876 png_crc_finish(png_ptr, length); 2937 png_crc_finish(png_ptr, length);
2877 } 2938 }
2878 # else 2939 # else
2879 # ifndef PNG_READ_USER_CHUNKS_SUPPORTED 2940 # ifndef PNG_READ_USER_CHUNKS_SUPPORTED
2880 # error no method to support READ_UNKNOWN_CHUNKS 2941 # error no method to support READ_UNKNOWN_CHUNKS
2881 # endif 2942 # endif
2882 2943
2883 { 2944 {
2884 /* If here there is no read callback pointer set and no support is 2945 /* If here there is no read callback pointer set and no support is
2885 * compiled in to just save the unknown chunks, so simply skip this 2946 * compiled in to just save the unknown chunks, so simply skip this
2886 * chunk. If 'keep' is something other than AS_DEFAULT or NEVER then 2947 * chunk. If 'keep' is something other than AS_DEFAULT or NEVER then
2887 * the app has erroneously asked for unknown chunk saving when there 2948 * the app has erroneously asked for unknown chunk saving when there
2888 * is no support. 2949 * is no support.
2889 */ 2950 */
2890 if (keep > PNG_HANDLE_CHUNK_NEVER) 2951 if (keep > PNG_HANDLE_CHUNK_NEVER)
2891 png_app_error(png_ptr, "no unknown chunk support available"); 2952 png_app_error(png_ptr, "no unknown chunk support available");
2892 2953
2893 png_crc_finish(png_ptr, length); 2954 png_crc_finish(png_ptr, length);
2894 } 2955 }
2895 # endif 2956 # endif
2896 2957
2897 # ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED 2958 # ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
2898 /* Now store the chunk in the chunk list if appropriate, and if the limits 2959 /* Now store the chunk in the chunk list if appropriate, and if the limits
2899 * permit it. 2960 * permit it.
2900 */ 2961 */
2901 if (keep == PNG_HANDLE_CHUNK_ALWAYS || 2962 if (keep == PNG_HANDLE_CHUNK_ALWAYS ||
2902 (keep == PNG_HANDLE_CHUNK_IF_SAFE && 2963 (keep == PNG_HANDLE_CHUNK_IF_SAFE &&
2903 PNG_CHUNK_ANCILLARY(png_ptr->chunk_name))) 2964 PNG_CHUNK_ANCILLARY(png_ptr->chunk_name)))
2904 { 2965 {
2905 # ifdef PNG_USER_LIMITS_SUPPORTED 2966 # ifdef PNG_USER_LIMITS_SUPPORTED
2906 switch (png_ptr->user_chunk_cache_max) 2967 switch (png_ptr->user_chunk_cache_max)
2907 { 2968 {
2908 case 2: 2969 case 2:
2909 png_ptr->user_chunk_cache_max = 1; 2970 png_ptr->user_chunk_cache_max = 1;
2910 png_chunk_benign_error(png_ptr, "no space in chunk cache"); 2971 png_chunk_benign_error(png_ptr, "no space in chunk cache");
2911 /* FALL THROUGH */ 2972 /* FALL THROUGH */
2912 case 1: 2973 case 1:
2913 /* NOTE: prior to 1.6.0 this case resulted in an unknown critical 2974 /* NOTE: prior to 1.6.0 this case resulted in an unknown critical
2914 * chunk being skipped, now there will be a hard error below. 2975 * chunk being skipped, now there will be a hard error below.
2915 */ 2976 */
2916 break; 2977 break;
2917 2978
2918 default: /* not at limit */ 2979 default: /* not at limit */
2919 --(png_ptr->user_chunk_cache_max); 2980 --(png_ptr->user_chunk_cache_max);
2920 /* FALL THROUGH */ 2981 /* FALL THROUGH */
2921 case 0: /* no limit */ 2982 case 0: /* no limit */
2922 # endif /* PNG_USER_LIMITS_SUPPORTED */ 2983 # endif /* USER_LIMITS */
2923 /* Here when the limit isn't reached or when limits are compiled 2984 /* Here when the limit isn't reached or when limits are compiled
2924 * out; store the chunk. 2985 * out; store the chunk.
2925 */ 2986 */
2926 png_set_unknown_chunks(png_ptr, info_ptr, 2987 png_set_unknown_chunks(png_ptr, info_ptr,
2927 &png_ptr->unknown_chunk, 1); 2988 &png_ptr->unknown_chunk, 1);
2928 handled = 1; 2989 handled = 1;
2929 # ifdef PNG_USER_LIMITS_SUPPORTED 2990 # ifdef PNG_USER_LIMITS_SUPPORTED
2930 break; 2991 break;
2931 } 2992 }
2932 # endif 2993 # endif
2933 } 2994 }
2934 # else /* no store support! */ 2995 # else /* no store support: the chunk must be handled by the user callback */
2935 PNG_UNUSED(info_ptr) 2996 PNG_UNUSED(info_ptr)
2936 # error untested code (reading unknown chunks with no store support)
2937 # endif 2997 # endif
2938 2998
2939 /* Regardless of the error handling below the cached data (if any) can be 2999 /* Regardless of the error handling below the cached data (if any) can be
2940 * freed now. Notice that the data is not freed if there is a png_error, but 3000 * freed now. Notice that the data is not freed if there is a png_error, but
2941 * it will be freed by destroy_read_struct. 3001 * it will be freed by destroy_read_struct.
2942 */ 3002 */
2943 if (png_ptr->unknown_chunk.data != NULL) 3003 if (png_ptr->unknown_chunk.data != NULL)
2944 png_free(png_ptr, png_ptr->unknown_chunk.data); 3004 png_free(png_ptr, png_ptr->unknown_chunk.data);
2945 png_ptr->unknown_chunk.data = NULL; 3005 png_ptr->unknown_chunk.data = NULL;
2946 3006
2947 #else /* !PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */ 3007 #else /* !PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */
2948 /* There is no support to read an unknown chunk, so just skip it. */ 3008 /* There is no support to read an unknown chunk, so just skip it. */
2949 png_crc_finish(png_ptr, length); 3009 png_crc_finish(png_ptr, length);
2950 PNG_UNUSED(info_ptr) 3010 PNG_UNUSED(info_ptr)
2951 PNG_UNUSED(keep) 3011 PNG_UNUSED(keep)
2952 #endif /* !PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */ 3012 #endif /* !READ_UNKNOWN_CHUNKS */
2953 3013
2954 /* Check for unhandled critical chunks */ 3014 /* Check for unhandled critical chunks */
2955 if (!handled && PNG_CHUNK_CRITICAL(png_ptr->chunk_name)) 3015 if (handled == 0 && PNG_CHUNK_CRITICAL(png_ptr->chunk_name))
2956 png_chunk_error(png_ptr, "unhandled critical chunk"); 3016 png_chunk_error(png_ptr, "unhandled critical chunk");
2957 } 3017 }
2958 3018
2959 /* This function is called to verify that a chunk name is valid. 3019 /* This function is called to verify that a chunk name is valid.
2960 * This function can't have the "critical chunk check" incorporated 3020 * This function can't have the "critical chunk check" incorporated
2961 * into it, since in the future we will need to be able to call user 3021 * into it, since in the future we will need to be able to call user
2962 * functions to handle unknown critical chunks after we check that 3022 * functions to handle unknown critical chunks after we check that
2963 * the chunk name itself is valid. 3023 * the chunk name itself is valid.
2964 */ 3024 */
2965 3025
(...skipping 25 matching lines...) Expand all
2991 * handles the two methods of progressive display of interlaced images, 3051 * handles the two methods of progressive display of interlaced images,
2992 * depending on the 'display' value; if 'display' is true then the whole row 3052 * depending on the 'display' value; if 'display' is true then the whole row
2993 * (dp) is filled from the start by replicating the available pixels. If 3053 * (dp) is filled from the start by replicating the available pixels. If
2994 * 'display' is false only those pixels present in the pass are filled in. 3054 * 'display' is false only those pixels present in the pass are filled in.
2995 */ 3055 */
2996 void /* PRIVATE */ 3056 void /* PRIVATE */
2997 png_combine_row(png_const_structrp png_ptr, png_bytep dp, int display) 3057 png_combine_row(png_const_structrp png_ptr, png_bytep dp, int display)
2998 { 3058 {
2999 unsigned int pixel_depth = png_ptr->transformed_pixel_depth; 3059 unsigned int pixel_depth = png_ptr->transformed_pixel_depth;
3000 png_const_bytep sp = png_ptr->row_buf + 1; 3060 png_const_bytep sp = png_ptr->row_buf + 1;
3001 png_uint_32 row_width = png_ptr->width; 3061 png_alloc_size_t row_width = png_ptr->width;
3002 unsigned int pass = png_ptr->pass; 3062 unsigned int pass = png_ptr->pass;
3003 png_bytep end_ptr = 0; 3063 png_bytep end_ptr = 0;
3004 png_byte end_byte = 0; 3064 png_byte end_byte = 0;
3005 unsigned int end_mask; 3065 unsigned int end_mask;
3006 3066
3007 png_debug(1, "in png_combine_row"); 3067 png_debug(1, "in png_combine_row");
3008 3068
3009 /* Added in 1.5.6: it should not be possible to enter this routine until at 3069 /* Added in 1.5.6: it should not be possible to enter this routine until at
3010 * least one row has been read from the PNG data and transformed. 3070 * least one row has been read from the PNG data and transformed.
3011 */ 3071 */
(...skipping 16 matching lines...) Expand all
3028 * the multiply below may overflow, we don't care because ANSI-C guarantees 3088 * the multiply below may overflow, we don't care because ANSI-C guarantees
3029 * we get the low bits. 3089 * we get the low bits.
3030 */ 3090 */
3031 end_mask = (pixel_depth * row_width) & 7; 3091 end_mask = (pixel_depth * row_width) & 7;
3032 if (end_mask != 0) 3092 if (end_mask != 0)
3033 { 3093 {
3034 /* end_ptr == NULL is a flag to say do nothing */ 3094 /* end_ptr == NULL is a flag to say do nothing */
3035 end_ptr = dp + PNG_ROWBYTES(pixel_depth, row_width) - 1; 3095 end_ptr = dp + PNG_ROWBYTES(pixel_depth, row_width) - 1;
3036 end_byte = *end_ptr; 3096 end_byte = *end_ptr;
3037 # ifdef PNG_READ_PACKSWAP_SUPPORTED 3097 # ifdef PNG_READ_PACKSWAP_SUPPORTED
3038 if (png_ptr->transformations & PNG_PACKSWAP) /* little-endian byte */ 3098 if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
3039 end_mask = 0xff << end_mask; 3099 /* little-endian byte */
3040 3100 end_mask = 0xff << end_mask;
3041 else /* big-endian byte */ 3101
3102 else /* big-endian byte */
3042 # endif 3103 # endif
3043 end_mask = 0xff >> end_mask; 3104 end_mask = 0xff >> end_mask;
3044 /* end_mask is now the bits to *keep* from the destination row */ 3105 /* end_mask is now the bits to *keep* from the destination row */
3045 } 3106 }
3046 3107
3047 /* For non-interlaced images this reduces to a memcpy(). A memcpy() 3108 /* For non-interlaced images this reduces to a memcpy(). A memcpy()
3048 * will also happen if interlacing isn't supported or if the application 3109 * will also happen if interlacing isn't supported or if the application
3049 * does not call png_set_interlace_handling(). In the latter cases the 3110 * does not call png_set_interlace_handling(). In the latter cases the
3050 * caller just gets a sequence of the unexpanded rows from each interlace 3111 * caller just gets a sequence of the unexpanded rows from each interlace
3051 * pass. 3112 * pass.
3052 */ 3113 */
3053 #ifdef PNG_READ_INTERLACING_SUPPORTED 3114 #ifdef PNG_READ_INTERLACING_SUPPORTED
3054 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE) && 3115 if (png_ptr->interlaced != 0 &&
3055 pass < 6 && (display == 0 || 3116 (png_ptr->transformations & PNG_INTERLACE) != 0 &&
3056 /* The following copies everything for 'display' on passes 0, 2 and 4. */ 3117 pass < 6 && (display == 0 ||
3057 (display == 1 && (pass & 1) != 0))) 3118 /* The following copies everything for 'display' on passes 0, 2 and 4. */
3119 (display == 1 && (pass & 1) != 0)))
3058 { 3120 {
3059 /* Narrow images may have no bits in a pass; the caller should handle 3121 /* Narrow images may have no bits in a pass; the caller should handle
3060 * this, but this test is cheap: 3122 * this, but this test is cheap:
3061 */ 3123 */
3062 if (row_width <= PNG_PASS_START_COL(pass)) 3124 if (row_width <= PNG_PASS_START_COL(pass))
3063 return; 3125 return;
3064 3126
3065 if (pixel_depth < 8) 3127 if (pixel_depth < 8)
3066 { 3128 {
3067 /* For pixel depths up to 4 bpp the 8-pixel mask can be expanded to fit 3129 /* For pixel depths up to 4 bpp the 8-pixel mask can be expanded to fit
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
3143 3205
3144 #if PNG_USE_COMPILE_TIME_MASKS 3206 #if PNG_USE_COMPILE_TIME_MASKS
3145 /* Utility macros to construct all the masks for a depth/swap 3207 /* Utility macros to construct all the masks for a depth/swap
3146 * combination. The 's' parameter says whether the format is PNG 3208 * combination. The 's' parameter says whether the format is PNG
3147 * (big endian bytes) or not. Only the three odd-numbered passes are 3209 * (big endian bytes) or not. Only the three odd-numbered passes are
3148 * required for the display/block algorithm. 3210 * required for the display/block algorithm.
3149 */ 3211 */
3150 # define S_MASKS(d,s) { S_MASK(0,d,s), S_MASK(1,d,s), S_MASK(2,d,s),\ 3212 # define S_MASKS(d,s) { S_MASK(0,d,s), S_MASK(1,d,s), S_MASK(2,d,s),\
3151 S_MASK(3,d,s), S_MASK(4,d,s), S_MASK(5,d,s) } 3213 S_MASK(3,d,s), S_MASK(4,d,s), S_MASK(5,d,s) }
3152 3214
3153 # define B_MASKS(d,s) { B_MASK(1,d,s), S_MASK(3,d,s), S_MASK(5,d,s) } 3215 # define B_MASKS(d,s) { B_MASK(1,d,s), B_MASK(3,d,s), B_MASK(5,d,s) }
3154 3216
3155 # define DEPTH_INDEX(d) ((d)==1?0:((d)==2?1:2)) 3217 # define DEPTH_INDEX(d) ((d)==1?0:((d)==2?1:2))
3156 3218
3157 /* Hence the pre-compiled masks indexed by PACKSWAP (or not), depth and 3219 /* Hence the pre-compiled masks indexed by PACKSWAP (or not), depth and
3158 * then pass: 3220 * then pass:
3159 */ 3221 */
3160 static PNG_CONST png_uint_32 row_mask[2/*PACKSWAP*/][3/*depth*/][6] = 3222 static PNG_CONST png_uint_32 row_mask[2/*PACKSWAP*/][3/*depth*/][6] =
3161 { 3223 {
3162 /* Little-endian byte masks for PACKSWAP */ 3224 /* Little-endian byte masks for PACKSWAP */
3163 { S_MASKS(1,0), S_MASKS(2,0), S_MASKS(4,0) }, 3225 { S_MASKS(1,0), S_MASKS(2,0), S_MASKS(4,0) },
(...skipping 15 matching lines...) Expand all
3179 # define MASK(pass,depth,display,png)\ 3241 # define MASK(pass,depth,display,png)\
3180 ((display)?display_mask[png][DEPTH_INDEX(depth)][pass>>1]:\ 3242 ((display)?display_mask[png][DEPTH_INDEX(depth)][pass>>1]:\
3181 row_mask[png][DEPTH_INDEX(depth)][pass]) 3243 row_mask[png][DEPTH_INDEX(depth)][pass])
3182 3244
3183 #else /* !PNG_USE_COMPILE_TIME_MASKS */ 3245 #else /* !PNG_USE_COMPILE_TIME_MASKS */
3184 /* This is the runtime alternative: it seems unlikely that this will 3246 /* This is the runtime alternative: it seems unlikely that this will
3185 * ever be either smaller or faster than the compile time approach. 3247 * ever be either smaller or faster than the compile time approach.
3186 */ 3248 */
3187 # define MASK(pass,depth,display,png)\ 3249 # define MASK(pass,depth,display,png)\
3188 ((display)?B_MASK(pass,depth,png):S_MASK(pass,depth,png)) 3250 ((display)?B_MASK(pass,depth,png):S_MASK(pass,depth,png))
3189 #endif /* !PNG_USE_COMPILE_TIME_MASKS */ 3251 #endif /* !USE_COMPILE_TIME_MASKS */
3190 3252
3191 /* Use the appropriate mask to copy the required bits. In some cases 3253 /* Use the appropriate mask to copy the required bits. In some cases
3192 * the byte mask will be 0 or 0xff, optimize these cases. row_width is 3254 * the byte mask will be 0 or 0xff; optimize these cases. row_width is
3193 * the number of pixels, but the code copies bytes, so it is necessary 3255 * the number of pixels, but the code copies bytes, so it is necessary
3194 * to special case the end. 3256 * to special case the end.
3195 */ 3257 */
3196 png_uint_32 pixels_per_byte = 8 / pixel_depth; 3258 png_uint_32 pixels_per_byte = 8 / pixel_depth;
3197 png_uint_32 mask; 3259 png_uint_32 mask;
3198 3260
3199 # ifdef PNG_READ_PACKSWAP_SUPPORTED 3261 # ifdef PNG_READ_PACKSWAP_SUPPORTED
3200 if (png_ptr->transformations & PNG_PACKSWAP) 3262 if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
3201 mask = MASK(pass, pixel_depth, display, 0); 3263 mask = MASK(pass, pixel_depth, display, 0);
3202 3264
3203 else 3265 else
3204 # endif 3266 # endif
3205 mask = MASK(pass, pixel_depth, display, 1); 3267 mask = MASK(pass, pixel_depth, display, 1);
3206 3268
3207 for (;;) 3269 for (;;)
3208 { 3270 {
3209 png_uint_32 m; 3271 png_uint_32 m;
3210 3272
3211 /* It doesn't matter in the following if png_uint_32 has more than 3273 /* It doesn't matter in the following if png_uint_32 has more than
3212 * 32 bits because the high bits always match those in m<<24; it is, 3274 * 32 bits because the high bits always match those in m<<24; it is,
3213 * however, essential to use OR here, not +, because of this. 3275 * however, essential to use OR here, not +, because of this.
3214 */ 3276 */
3215 m = mask; 3277 m = mask;
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
3254 */ 3316 */
3255 { 3317 {
3256 unsigned int offset = PNG_PASS_START_COL(pass) * pixel_depth; 3318 unsigned int offset = PNG_PASS_START_COL(pass) * pixel_depth;
3257 3319
3258 row_width -= offset; 3320 row_width -= offset;
3259 dp += offset; 3321 dp += offset;
3260 sp += offset; 3322 sp += offset;
3261 } 3323 }
3262 3324
3263 /* Work out the bytes to copy. */ 3325 /* Work out the bytes to copy. */
3264 if (display) 3326 if (display != 0)
3265 { 3327 {
3266 /* When doing the 'block' algorithm the pixel in the pass gets 3328 /* When doing the 'block' algorithm the pixel in the pass gets
3267 * replicated to adjacent pixels. This is why the even (0,2,4,6) 3329 * replicated to adjacent pixels. This is why the even (0,2,4,6)
3268 * passes are skipped above - the entire expanded row is copied. 3330 * passes are skipped above - the entire expanded row is copied.
3269 */ 3331 */
3270 bytes_to_copy = (1<<((6-pass)>>1)) * pixel_depth; 3332 bytes_to_copy = (1<<((6-pass)>>1)) * pixel_depth;
3271 3333
3272 /* But don't allow this number to exceed the actual row width. */ 3334 /* But don't allow this number to exceed the actual row width. */
3273 if (bytes_to_copy > row_width) 3335 if (bytes_to_copy > row_width)
3274 bytes_to_copy = row_width; 3336 bytes_to_copy = (unsigned int)/*SAFE*/row_width;
3275 } 3337 }
3276 3338
3277 else /* normal row; Adam7 only ever gives us one pixel to copy. */ 3339 else /* normal row; Adam7 only ever gives us one pixel to copy. */
3278 bytes_to_copy = pixel_depth; 3340 bytes_to_copy = pixel_depth;
3279 3341
3280 /* In Adam7 there is a constant offset between where the pixels go. */ 3342 /* In Adam7 there is a constant offset between where the pixels go. */
3281 bytes_to_jump = PNG_PASS_COL_OFFSET(pass) * pixel_depth; 3343 bytes_to_jump = PNG_PASS_COL_OFFSET(pass) * pixel_depth;
3282 3344
3283 /* And simply copy these bytes. Some optimization is possible here, 3345 /* And simply copy these bytes. Some optimization is possible here,
3284 * depending on the value of 'bytes_to_copy'. Special case the low 3346 * depending on the value of 'bytes_to_copy'. Special case the low
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
3321 while (row_width > 1); 3383 while (row_width > 1);
3322 3384
3323 /* And there can only be one byte left at this point: */ 3385 /* And there can only be one byte left at this point: */
3324 *dp = *sp; 3386 *dp = *sp;
3325 return; 3387 return;
3326 3388
3327 case 3: 3389 case 3:
3328 /* This can only be the RGB case, so each copy is exactly one 3390 /* This can only be the RGB case, so each copy is exactly one
3329 * pixel and it is not necessary to check for a partial copy. 3391 * pixel and it is not necessary to check for a partial copy.
3330 */ 3392 */
3331 for(;;) 3393 for (;;)
3332 { 3394 {
3333 dp[0] = sp[0], dp[1] = sp[1], dp[2] = sp[2]; 3395 dp[0] = sp[0], dp[1] = sp[1], dp[2] = sp[2];
3334 3396
3335 if (row_width <= bytes_to_jump) 3397 if (row_width <= bytes_to_jump)
3336 return; 3398 return;
3337 3399
3338 sp += bytes_to_jump; 3400 sp += bytes_to_jump;
3339 dp += bytes_to_jump; 3401 dp += bytes_to_jump;
3340 row_width -= bytes_to_jump; 3402 row_width -= bytes_to_jump;
3341 } 3403 }
3342 3404
3343 default: 3405 default:
3344 #if PNG_ALIGN_TYPE != PNG_ALIGN_NONE 3406 #if PNG_ALIGN_TYPE != PNG_ALIGN_NONE
3345 /* Check for double byte alignment and, if possible, use a 3407 /* Check for double byte alignment and, if possible, use a
3346 * 16-bit copy. Don't attempt this for narrow images - ones that 3408 * 16-bit copy. Don't attempt this for narrow images - ones that
3347 * are less than an interlace panel wide. Don't attempt it for 3409 * are less than an interlace panel wide. Don't attempt it for
3348 * wide bytes_to_copy either - use the memcpy there. 3410 * wide bytes_to_copy either - use the memcpy there.
3349 */ 3411 */
3350 if (bytes_to_copy < 16 /*else use memcpy*/ && 3412 if (bytes_to_copy < 16 /*else use memcpy*/ &&
3351 png_isaligned(dp, png_uint_16) && 3413 png_isaligned(dp, png_uint_16) &&
3352 png_isaligned(sp, png_uint_16) && 3414 png_isaligned(sp, png_uint_16) &&
3353 bytes_to_copy % (sizeof (png_uint_16)) == 0 && 3415 bytes_to_copy % (sizeof (png_uint_16)) == 0 &&
3354 bytes_to_jump % (sizeof (png_uint_16)) == 0) 3416 bytes_to_jump % (sizeof (png_uint_16)) == 0)
3355 { 3417 {
3356 /* Everything is aligned for png_uint_16 copies, but try for 3418 /* Everything is aligned for png_uint_16 copies, but try for
3357 * png_uint_32 first. 3419 * png_uint_32 first.
3358 */ 3420 */
3359 if (png_isaligned(dp, png_uint_32) && 3421 if (png_isaligned(dp, png_uint_32) != 0 &&
3360 png_isaligned(sp, png_uint_32) && 3422 png_isaligned(sp, png_uint_32) != 0 &&
3361 bytes_to_copy % (sizeof (png_uint_32)) == 0 && 3423 bytes_to_copy % (sizeof (png_uint_32)) == 0 &&
3362 bytes_to_jump % (sizeof (png_uint_32)) == 0) 3424 bytes_to_jump % (sizeof (png_uint_32)) == 0)
3363 { 3425 {
3364 png_uint_32p dp32 = png_aligncast(png_uint_32p,dp); 3426 png_uint_32p dp32 = png_aligncast(png_uint_32p,dp);
3365 png_const_uint_32p sp32 = png_aligncastconst( 3427 png_const_uint_32p sp32 = png_aligncastconst(
3366 png_const_uint_32p, sp); 3428 png_const_uint_32p, sp);
3367 size_t skip = (bytes_to_jump-bytes_to_copy) / 3429 size_t skip = (bytes_to_jump-bytes_to_copy) /
3368 (sizeof (png_uint_32)); 3430 (sizeof (png_uint_32));
3369 3431
3370 do 3432 do
3371 { 3433 {
3372 size_t c = bytes_to_copy; 3434 size_t c = bytes_to_copy;
3373 do 3435 do
3374 { 3436 {
3375 *dp32++ = *sp32++; 3437 *dp32++ = *sp32++;
3376 c -= (sizeof (png_uint_32)); 3438 c -= (sizeof (png_uint_32));
3377 } 3439 }
3378 while (c > 0); 3440 while (c > 0);
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
3430 3492
3431 /* End of row - 1 byte left, bytes_to_copy > row_width: */ 3493 /* End of row - 1 byte left, bytes_to_copy > row_width: */
3432 dp = (png_bytep)dp16; 3494 dp = (png_bytep)dp16;
3433 sp = (png_const_bytep)sp16; 3495 sp = (png_const_bytep)sp16;
3434 do 3496 do
3435 *dp++ = *sp++; 3497 *dp++ = *sp++;
3436 while (--row_width > 0); 3498 while (--row_width > 0);
3437 return; 3499 return;
3438 } 3500 }
3439 } 3501 }
3440 #endif /* PNG_ALIGN_ code */ 3502 #endif /* ALIGN_TYPE code */
3441 3503
3442 /* The true default - use a memcpy: */ 3504 /* The true default - use a memcpy: */
3443 for (;;) 3505 for (;;)
3444 { 3506 {
3445 memcpy(dp, sp, bytes_to_copy); 3507 memcpy(dp, sp, bytes_to_copy);
3446 3508
3447 if (row_width <= bytes_to_jump) 3509 if (row_width <= bytes_to_jump)
3448 return; 3510 return;
3449 3511
3450 sp += bytes_to_jump; 3512 sp += bytes_to_jump;
3451 dp += bytes_to_jump; 3513 dp += bytes_to_jump;
3452 row_width -= bytes_to_jump; 3514 row_width -= bytes_to_jump;
3453 if (bytes_to_copy > row_width) 3515 if (bytes_to_copy > row_width)
3454 bytes_to_copy = row_width; 3516 bytes_to_copy = (unsigned int)/*SAFE*/row_width;
3455 } 3517 }
3456 } 3518 }
3457 3519
3458 /* NOT REACHED*/ 3520 /* NOT REACHED*/
3459 } /* pixel_depth >= 8 */ 3521 } /* pixel_depth >= 8 */
3460 3522
3461 /* Here if pixel_depth < 8 to check 'end_ptr' below. */ 3523 /* Here if pixel_depth < 8 to check 'end_ptr' below. */
3462 } 3524 }
3463 else 3525 else
3464 #endif 3526 #endif /* READ_INTERLACING */
3465 3527
3466 /* If here then the switch above wasn't used so just memcpy the whole row 3528 /* If here then the switch above wasn't used so just memcpy the whole row
3467 * from the temporary row buffer (notice that this overwrites the end of the 3529 * from the temporary row buffer (notice that this overwrites the end of the
3468 * destination row if it is a partial byte.) 3530 * destination row if it is a partial byte.)
3469 */ 3531 */
3470 memcpy(dp, sp, PNG_ROWBYTES(pixel_depth, row_width)); 3532 memcpy(dp, sp, PNG_ROWBYTES(pixel_depth, row_width));
3471 3533
3472 /* Restore the overwritten bits from the last byte if necessary. */ 3534 /* Restore the overwritten bits from the last byte if necessary. */
3473 if (end_ptr != NULL) 3535 if (end_ptr != NULL)
3474 *end_ptr = (png_byte)((end_byte & end_mask) | (*end_ptr & ~end_mask)); 3536 *end_ptr = (png_byte)((end_byte & end_mask) | (*end_ptr & ~end_mask));
(...skipping 22 matching lines...) Expand all
3497 png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3); 3559 png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3);
3498 png_bytep dp = row + (png_size_t)((final_width - 1) >> 3); 3560 png_bytep dp = row + (png_size_t)((final_width - 1) >> 3);
3499 int sshift, dshift; 3561 int sshift, dshift;
3500 int s_start, s_end, s_inc; 3562 int s_start, s_end, s_inc;
3501 int jstop = png_pass_inc[pass]; 3563 int jstop = png_pass_inc[pass];
3502 png_byte v; 3564 png_byte v;
3503 png_uint_32 i; 3565 png_uint_32 i;
3504 int j; 3566 int j;
3505 3567
3506 #ifdef PNG_READ_PACKSWAP_SUPPORTED 3568 #ifdef PNG_READ_PACKSWAP_SUPPORTED
3507 if (transformations & PNG_PACKSWAP) 3569 if ((transformations & PNG_PACKSWAP) != 0)
3508 { 3570 {
3509 sshift = (int)((row_info->width + 7) & 0x07); 3571 sshift = (int)((row_info->width + 7) & 0x07);
3510 dshift = (int)((final_width + 7) & 0x07); 3572 dshift = (int)((final_width + 7) & 0x07);
3511 s_start = 7; 3573 s_start = 7;
3512 s_end = 0; 3574 s_end = 0;
3513 s_inc = -1; 3575 s_inc = -1;
3514 } 3576 }
3515 3577
3516 else 3578 else
3517 #endif 3579 #endif
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
3557 case 2: 3619 case 2:
3558 { 3620 {
3559 png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2); 3621 png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);
3560 png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2); 3622 png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);
3561 int sshift, dshift; 3623 int sshift, dshift;
3562 int s_start, s_end, s_inc; 3624 int s_start, s_end, s_inc;
3563 int jstop = png_pass_inc[pass]; 3625 int jstop = png_pass_inc[pass];
3564 png_uint_32 i; 3626 png_uint_32 i;
3565 3627
3566 #ifdef PNG_READ_PACKSWAP_SUPPORTED 3628 #ifdef PNG_READ_PACKSWAP_SUPPORTED
3567 if (transformations & PNG_PACKSWAP) 3629 if ((transformations & PNG_PACKSWAP) != 0)
3568 { 3630 {
3569 sshift = (int)(((row_info->width + 3) & 0x03) << 1); 3631 sshift = (int)(((row_info->width + 3) & 0x03) << 1);
3570 dshift = (int)(((final_width + 3) & 0x03) << 1); 3632 dshift = (int)(((final_width + 3) & 0x03) << 1);
3571 s_start = 6; 3633 s_start = 6;
3572 s_end = 0; 3634 s_end = 0;
3573 s_inc = -2; 3635 s_inc = -2;
3574 } 3636 }
3575 3637
3576 else 3638 else
3577 #endif 3639 #endif
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
3620 case 4: 3682 case 4:
3621 { 3683 {
3622 png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1); 3684 png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1);
3623 png_bytep dp = row + (png_size_t)((final_width - 1) >> 1); 3685 png_bytep dp = row + (png_size_t)((final_width - 1) >> 1);
3624 int sshift, dshift; 3686 int sshift, dshift;
3625 int s_start, s_end, s_inc; 3687 int s_start, s_end, s_inc;
3626 png_uint_32 i; 3688 png_uint_32 i;
3627 int jstop = png_pass_inc[pass]; 3689 int jstop = png_pass_inc[pass];
3628 3690
3629 #ifdef PNG_READ_PACKSWAP_SUPPORTED 3691 #ifdef PNG_READ_PACKSWAP_SUPPORTED
3630 if (transformations & PNG_PACKSWAP) 3692 if ((transformations & PNG_PACKSWAP) != 0)
3631 { 3693 {
3632 sshift = (int)(((row_info->width + 1) & 0x01) << 2); 3694 sshift = (int)(((row_info->width + 1) & 0x01) << 2);
3633 dshift = (int)(((final_width + 1) & 0x01) << 2); 3695 dshift = (int)(((final_width + 1) & 0x01) << 2);
3634 s_start = 4; 3696 s_start = 4;
3635 s_end = 0; 3697 s_end = 0;
3636 s_inc = -4; 3698 s_inc = -4;
3637 } 3699 }
3638 3700
3639 else 3701 else
3640 #endif 3702 #endif
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
3710 } 3772 }
3711 } 3773 }
3712 3774
3713 row_info->width = final_width; 3775 row_info->width = final_width;
3714 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width); 3776 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width);
3715 } 3777 }
3716 #ifndef PNG_READ_PACKSWAP_SUPPORTED 3778 #ifndef PNG_READ_PACKSWAP_SUPPORTED
3717 PNG_UNUSED(transformations) /* Silence compiler warning */ 3779 PNG_UNUSED(transformations) /* Silence compiler warning */
3718 #endif 3780 #endif
3719 } 3781 }
3720 #endif /* PNG_READ_INTERLACING_SUPPORTED */ 3782 #endif /* READ_INTERLACING */
3721 3783
3722 static void 3784 static void
3723 png_read_filter_row_sub(png_row_infop row_info, png_bytep row, 3785 png_read_filter_row_sub(png_row_infop row_info, png_bytep row,
3724 png_const_bytep prev_row) 3786 png_const_bytep prev_row)
3725 { 3787 {
3726 png_size_t i; 3788 png_size_t i;
3727 png_size_t istop = row_info->rowbytes; 3789 png_size_t istop = row_info->rowbytes;
3728 unsigned int bpp = (row_info->pixel_depth + 7) >> 3; 3790 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
3729 png_bytep rp = row + bpp; 3791 png_bytep rp = row + bpp;
3730 3792
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
3796 while (row < rp_end) 3858 while (row < rp_end)
3797 { 3859 {
3798 int b, pa, pb, pc, p; 3860 int b, pa, pb, pc, p;
3799 3861
3800 a &= 0xff; /* From previous iteration or start */ 3862 a &= 0xff; /* From previous iteration or start */
3801 b = *prev_row++; 3863 b = *prev_row++;
3802 3864
3803 p = b - c; 3865 p = b - c;
3804 pc = a - c; 3866 pc = a - c;
3805 3867
3806 # ifdef PNG_USE_ABS 3868 #ifdef PNG_USE_ABS
3807 pa = abs(p); 3869 pa = abs(p);
3808 pb = abs(pc); 3870 pb = abs(pc);
3809 pc = abs(p + pc); 3871 pc = abs(p + pc);
3810 # else 3872 #else
3811 pa = p < 0 ? -p : p; 3873 pa = p < 0 ? -p : p;
3812 pb = pc < 0 ? -pc : pc; 3874 pb = pc < 0 ? -pc : pc;
3813 pc = (p + pc) < 0 ? -(p + pc) : p + pc; 3875 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
3814 # endif 3876 #endif
3815 3877
3816 /* Find the best predictor, the least of pa, pb, pc favoring the earlier 3878 /* Find the best predictor, the least of pa, pb, pc favoring the earlier
3817 * ones in the case of a tie. 3879 * ones in the case of a tie.
3818 */ 3880 */
3819 if (pb < pa) pa = pb, a = b; 3881 if (pb < pa) pa = pb, a = b;
3820 if (pc < pa) a = c; 3882 if (pc < pa) a = c;
3821 3883
3822 /* Calculate the current pixel in a, and move the previous row pixel to c 3884 /* Calculate the current pixel in a, and move the previous row pixel to c
3823 * for the next time round the loop 3885 * for the next time round the loop
3824 */ 3886 */
(...skipping 26 matching lines...) Expand all
3851 { 3913 {
3852 int a, b, c, pa, pb, pc, p; 3914 int a, b, c, pa, pb, pc, p;
3853 3915
3854 c = *(prev_row - bpp); 3916 c = *(prev_row - bpp);
3855 a = *(row - bpp); 3917 a = *(row - bpp);
3856 b = *prev_row++; 3918 b = *prev_row++;
3857 3919
3858 p = b - c; 3920 p = b - c;
3859 pc = a - c; 3921 pc = a - c;
3860 3922
3861 # ifdef PNG_USE_ABS 3923 #ifdef PNG_USE_ABS
3862 pa = abs(p); 3924 pa = abs(p);
3863 pb = abs(pc); 3925 pb = abs(pc);
3864 pc = abs(p + pc); 3926 pc = abs(p + pc);
3865 # else 3927 #else
3866 pa = p < 0 ? -p : p; 3928 pa = p < 0 ? -p : p;
3867 pb = pc < 0 ? -pc : pc; 3929 pb = pc < 0 ? -pc : pc;
3868 pc = (p + pc) < 0 ? -(p + pc) : p + pc; 3930 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
3869 # endif 3931 #endif
3870 3932
3871 if (pb < pa) pa = pb, a = b; 3933 if (pb < pa) pa = pb, a = b;
3872 if (pc < pa) a = c; 3934 if (pc < pa) a = c;
3873 3935
3874 c = b;
3875 a += *row; 3936 a += *row;
3876 *row++ = (png_byte)a; 3937 *row++ = (png_byte)a;
3877 } 3938 }
3878 } 3939 }
3879 3940
3880 static void 3941 static void
3881 png_init_filter_functions(png_structrp pp) 3942 png_init_filter_functions(png_structrp pp)
3882 /* This function is called once for every PNG image to set the 3943 /* This function is called once for every PNG image (except for PNG images
3944 * that only use PNG_FILTER_VALUE_NONE for all rows) to set the
3883 * implementations required to reverse the filtering of PNG rows. Reversing 3945 * implementations required to reverse the filtering of PNG rows. Reversing
3884 * the filter is the first transformation performed on the row data. It is 3946 * the filter is the first transformation performed on the row data. It is
3885 * performed in place, therefore an implementation can be selected based on 3947 * performed in place, therefore an implementation can be selected based on
3886 * the image pixel format. If the implementation depends on image width then 3948 * the image pixel format. If the implementation depends on image width then
3887 * take care to ensure that it works correctly if the image is interlaced - 3949 * take care to ensure that it works correctly if the image is interlaced -
3888 * interlacing causes the actual row width to vary. 3950 * interlacing causes the actual row width to vary.
3889 */ 3951 */
3890 { 3952 {
3891 unsigned int bpp = (pp->pixel_depth + 7) >> 3; 3953 unsigned int bpp = (pp->pixel_depth + 7) >> 3;
3892 3954
(...skipping 21 matching lines...) Expand all
3914 } 3976 }
3915 3977
3916 void /* PRIVATE */ 3978 void /* PRIVATE */
3917 png_read_filter_row(png_structrp pp, png_row_infop row_info, png_bytep row, 3979 png_read_filter_row(png_structrp pp, png_row_infop row_info, png_bytep row,
3918 png_const_bytep prev_row, int filter) 3980 png_const_bytep prev_row, int filter)
3919 { 3981 {
3920 /* OPTIMIZATION: DO NOT MODIFY THIS FUNCTION, instead #define 3982 /* OPTIMIZATION: DO NOT MODIFY THIS FUNCTION, instead #define
3921 * PNG_FILTER_OPTIMIZATIONS to a function that overrides the generic 3983 * PNG_FILTER_OPTIMIZATIONS to a function that overrides the generic
3922 * implementations. See png_init_filter_functions above. 3984 * implementations. See png_init_filter_functions above.
3923 */ 3985 */
3924 if (pp->read_filter[0] == NULL)
3925 png_init_filter_functions(pp);
3926 if (filter > PNG_FILTER_VALUE_NONE && filter < PNG_FILTER_VALUE_LAST) 3986 if (filter > PNG_FILTER_VALUE_NONE && filter < PNG_FILTER_VALUE_LAST)
3987 {
3988 if (pp->read_filter[0] == NULL)
3989 png_init_filter_functions(pp);
3990
3927 pp->read_filter[filter-1](row_info, row, prev_row); 3991 pp->read_filter[filter-1](row_info, row, prev_row);
3992 }
3928 } 3993 }
3929 3994
3930 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED 3995 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
3931 void /* PRIVATE */ 3996 void /* PRIVATE */
3932 png_read_IDAT_data(png_structrp png_ptr, png_bytep output, 3997 png_read_IDAT_data(png_structrp png_ptr, png_bytep output,
3933 png_alloc_size_t avail_out) 3998 png_alloc_size_t avail_out)
3934 { 3999 {
3935 /* Loop reading IDATs and decompressing the result into output[avail_out] */ 4000 /* Loop reading IDATs and decompressing the result into output[avail_out] */
3936 png_ptr->zstream.next_out = output; 4001 png_ptr->zstream.next_out = output;
3937 png_ptr->zstream.avail_out = 0; /* safety: set below */ 4002 png_ptr->zstream.avail_out = 0; /* safety: set below */
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
3998 png_ptr->zstream.avail_out = (sizeof tmpbuf); 4063 png_ptr->zstream.avail_out = (sizeof tmpbuf);
3999 } 4064 }
4000 4065
4001 /* Use NO_FLUSH; this gives zlib the maximum opportunity to optimize the 4066 /* Use NO_FLUSH; this gives zlib the maximum opportunity to optimize the
4002 * process. If the LZ stream is truncated the sequential reader will 4067 * process. If the LZ stream is truncated the sequential reader will
4003 * terminally damage the stream, above, by reading the chunk header of the 4068 * terminally damage the stream, above, by reading the chunk header of the
4004 * following chunk (it then exits with png_error). 4069 * following chunk (it then exits with png_error).
4005 * 4070 *
4006 * TODO: deal more elegantly with truncated IDAT lists. 4071 * TODO: deal more elegantly with truncated IDAT lists.
4007 */ 4072 */
4008 ret = inflate(&png_ptr->zstream, Z_NO_FLUSH); 4073 ret = PNG_INFLATE(png_ptr, Z_NO_FLUSH);
4009 4074
4010 /* Take the unconsumed output back. */ 4075 /* Take the unconsumed output back. */
4011 if (output != NULL) 4076 if (output != NULL)
4012 avail_out += png_ptr->zstream.avail_out; 4077 avail_out += png_ptr->zstream.avail_out;
4013 4078
4014 else /* avail_out counts the extra bytes */ 4079 else /* avail_out counts the extra bytes */
4015 avail_out += (sizeof tmpbuf) - png_ptr->zstream.avail_out; 4080 avail_out += (sizeof tmpbuf) - png_ptr->zstream.avail_out;
4016 4081
4017 png_ptr->zstream.avail_out = 0; 4082 png_ptr->zstream.avail_out = 0;
4018 4083
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
4058 } 4123 }
4059 4124
4060 void /* PRIVATE */ 4125 void /* PRIVATE */
4061 png_read_finish_IDAT(png_structrp png_ptr) 4126 png_read_finish_IDAT(png_structrp png_ptr)
4062 { 4127 {
4063 /* We don't need any more data and the stream should have ended, however the 4128 /* We don't need any more data and the stream should have ended, however the
4064 * LZ end code may actually not have been processed. In this case we must 4129 * LZ end code may actually not have been processed. In this case we must
4065 * read it otherwise stray unread IDAT data or, more likely, an IDAT chunk 4130 * read it otherwise stray unread IDAT data or, more likely, an IDAT chunk
4066 * may still remain to be consumed. 4131 * may still remain to be consumed.
4067 */ 4132 */
4068 if (!(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED)) 4133 if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
4069 { 4134 {
4070 /* The NULL causes png_read_IDAT_data to swallow any remaining bytes in 4135 /* The NULL causes png_read_IDAT_data to swallow any remaining bytes in
4071 * the compressed stream, but the stream may be damaged too, so even after 4136 * the compressed stream, but the stream may be damaged too, so even after
4072 * this call we may need to terminate the zstream ownership. 4137 * this call we may need to terminate the zstream ownership.
4073 */ 4138 */
4074 png_read_IDAT_data(png_ptr, NULL, 0); 4139 png_read_IDAT_data(png_ptr, NULL, 0);
4075 png_ptr->zstream.next_out = NULL; /* safety */ 4140 png_ptr->zstream.next_out = NULL; /* safety */
4076 4141
4077 /* Now clear everything out for safety; the following may not have been 4142 /* Now clear everything out for safety; the following may not have been
4078 * done. 4143 * done.
4079 */ 4144 */
4080 if (!(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED)) 4145 if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
4081 { 4146 {
4082 png_ptr->mode |= PNG_AFTER_IDAT; 4147 png_ptr->mode |= PNG_AFTER_IDAT;
4083 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED; 4148 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
4084 } 4149 }
4085 } 4150 }
4086 4151
4087 /* If the zstream has not been released do it now *and* terminate the reading 4152 /* If the zstream has not been released do it now *and* terminate the reading
4088 * of the final IDAT chunk. 4153 * of the final IDAT chunk.
4089 */ 4154 */
4090 if (png_ptr->zowner == png_IDAT) 4155 if (png_ptr->zowner == png_IDAT)
(...skipping 10 matching lines...) Expand all
4101 * crc_finish here. If idat_size is non-zero we also need to read the 4166 * crc_finish here. If idat_size is non-zero we also need to read the
4102 * spurious bytes at the end of the chunk now. 4167 * spurious bytes at the end of the chunk now.
4103 */ 4168 */
4104 (void)png_crc_finish(png_ptr, png_ptr->idat_size); 4169 (void)png_crc_finish(png_ptr, png_ptr->idat_size);
4105 } 4170 }
4106 } 4171 }
4107 4172
4108 void /* PRIVATE */ 4173 void /* PRIVATE */
4109 png_read_finish_row(png_structrp png_ptr) 4174 png_read_finish_row(png_structrp png_ptr)
4110 { 4175 {
4111 #ifdef PNG_READ_INTERLACING_SUPPORTED
4112 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ 4176 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
4113 4177
4114 /* Start of interlace block */ 4178 /* Start of interlace block */
4115 static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; 4179 static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
4116 4180
4117 /* Offset to next interlace block */ 4181 /* Offset to next interlace block */
4118 static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; 4182 static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
4119 4183
4120 /* Start of interlace block in the y direction */ 4184 /* Start of interlace block in the y direction */
4121 static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; 4185 static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
4122 4186
4123 /* Offset to next interlace block in the y direction */ 4187 /* Offset to next interlace block in the y direction */
4124 static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; 4188 static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
4125 #endif /* PNG_READ_INTERLACING_SUPPORTED */
4126 4189
4127 png_debug(1, "in png_read_finish_row"); 4190 png_debug(1, "in png_read_finish_row");
4128 png_ptr->row_number++; 4191 png_ptr->row_number++;
4129 if (png_ptr->row_number < png_ptr->num_rows) 4192 if (png_ptr->row_number < png_ptr->num_rows)
4130 return; 4193 return;
4131 4194
4132 #ifdef PNG_READ_INTERLACING_SUPPORTED 4195 if (png_ptr->interlaced != 0)
4133 if (png_ptr->interlaced)
4134 { 4196 {
4135 png_ptr->row_number = 0; 4197 png_ptr->row_number = 0;
4136 4198
4137 /* TO DO: don't do this if prev_row isn't needed (requires 4199 /* TO DO: don't do this if prev_row isn't needed (requires
4138 * read-ahead of the next row's filter byte. 4200 * read-ahead of the next row's filter byte.
4139 */ 4201 */
4140 memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1); 4202 memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
4141 4203
4142 do 4204 do
4143 { 4205 {
4144 png_ptr->pass++; 4206 png_ptr->pass++;
4145 4207
4146 if (png_ptr->pass >= 7) 4208 if (png_ptr->pass >= 7)
4147 break; 4209 break;
4148 4210
4149 png_ptr->iwidth = (png_ptr->width + 4211 png_ptr->iwidth = (png_ptr->width +
4150 png_pass_inc[png_ptr->pass] - 1 - 4212 png_pass_inc[png_ptr->pass] - 1 -
4151 png_pass_start[png_ptr->pass]) / 4213 png_pass_start[png_ptr->pass]) /
4152 png_pass_inc[png_ptr->pass]; 4214 png_pass_inc[png_ptr->pass];
4153 4215
4154 if (!(png_ptr->transformations & PNG_INTERLACE)) 4216 if ((png_ptr->transformations & PNG_INTERLACE) == 0)
4155 { 4217 {
4156 png_ptr->num_rows = (png_ptr->height + 4218 png_ptr->num_rows = (png_ptr->height +
4157 png_pass_yinc[png_ptr->pass] - 1 - 4219 png_pass_yinc[png_ptr->pass] - 1 -
4158 png_pass_ystart[png_ptr->pass]) / 4220 png_pass_ystart[png_ptr->pass]) /
4159 png_pass_yinc[png_ptr->pass]; 4221 png_pass_yinc[png_ptr->pass];
4160 } 4222 }
4161 4223
4162 else /* if (png_ptr->transformations & PNG_INTERLACE) */ 4224 else /* if (png_ptr->transformations & PNG_INTERLACE) */
4163 break; /* libpng deinterlacing sees every row */ 4225 break; /* libpng deinterlacing sees every row */
4164 4226
4165 } while (png_ptr->num_rows == 0 || png_ptr->iwidth == 0); 4227 } while (png_ptr->num_rows == 0 || png_ptr->iwidth == 0);
4166 4228
4167 if (png_ptr->pass < 7) 4229 if (png_ptr->pass < 7)
4168 return; 4230 return;
4169 } 4231 }
4170 #endif /* PNG_READ_INTERLACING_SUPPORTED */
4171 4232
4172 /* Here after at the end of the last row of the last pass. */ 4233 /* Here after at the end of the last row of the last pass. */
4173 png_read_finish_IDAT(png_ptr); 4234 png_read_finish_IDAT(png_ptr);
4174 } 4235 }
4175 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ 4236 #endif /* SEQUENTIAL_READ */
4176 4237
4177 void /* PRIVATE */ 4238 void /* PRIVATE */
4178 png_read_start_row(png_structrp png_ptr) 4239 png_read_start_row(png_structrp png_ptr)
4179 { 4240 {
4180 #ifdef PNG_READ_INTERLACING_SUPPORTED
4181 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ 4241 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
4182 4242
4183 /* Start of interlace block */ 4243 /* Start of interlace block */
4184 static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; 4244 static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
4185 4245
4186 /* Offset to next interlace block */ 4246 /* Offset to next interlace block */
4187 static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; 4247 static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
4188 4248
4189 /* Start of interlace block in the y direction */ 4249 /* Start of interlace block in the y direction */
4190 static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; 4250 static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
4191 4251
4192 /* Offset to next interlace block in the y direction */ 4252 /* Offset to next interlace block in the y direction */
4193 static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; 4253 static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
4194 #endif
4195 4254
4196 int max_pixel_depth; 4255 int max_pixel_depth;
4197 png_size_t row_bytes; 4256 png_size_t row_bytes;
4198 4257
4199 png_debug(1, "in png_read_start_row"); 4258 png_debug(1, "in png_read_start_row");
4200 4259
4201 #ifdef PNG_READ_TRANSFORMS_SUPPORTED 4260 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
4202 png_init_read_transformations(png_ptr); 4261 png_init_read_transformations(png_ptr);
4203 #endif 4262 #endif
4204 #ifdef PNG_READ_INTERLACING_SUPPORTED 4263 if (png_ptr->interlaced != 0)
4205 if (png_ptr->interlaced) 4264 {
4206 { 4265 if ((png_ptr->transformations & PNG_INTERLACE) == 0)
4207 if (!(png_ptr->transformations & PNG_INTERLACE))
4208 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 - 4266 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
4209 png_pass_ystart[0]) / png_pass_yinc[0]; 4267 png_pass_ystart[0]) / png_pass_yinc[0];
4210 4268
4211 else 4269 else
4212 png_ptr->num_rows = png_ptr->height; 4270 png_ptr->num_rows = png_ptr->height;
4213 4271
4214 png_ptr->iwidth = (png_ptr->width + 4272 png_ptr->iwidth = (png_ptr->width +
4215 png_pass_inc[png_ptr->pass] - 1 - 4273 png_pass_inc[png_ptr->pass] - 1 -
4216 png_pass_start[png_ptr->pass]) / 4274 png_pass_start[png_ptr->pass]) /
4217 png_pass_inc[png_ptr->pass]; 4275 png_pass_inc[png_ptr->pass];
4218 } 4276 }
4219 4277
4220 else 4278 else
4221 #endif /* PNG_READ_INTERLACING_SUPPORTED */
4222 { 4279 {
4223 png_ptr->num_rows = png_ptr->height; 4280 png_ptr->num_rows = png_ptr->height;
4224 png_ptr->iwidth = png_ptr->width; 4281 png_ptr->iwidth = png_ptr->width;
4225 } 4282 }
4226 4283
4227 max_pixel_depth = png_ptr->pixel_depth; 4284 max_pixel_depth = png_ptr->pixel_depth;
4228 4285
4229 /* WARNING: * png_read_transform_info (pngrtran.c) performs a simpliar set of 4286 /* WARNING: * png_read_transform_info (pngrtran.c) performs a simpler set of
4230 * calculations to calculate the final pixel depth, then 4287 * calculations to calculate the final pixel depth, then
4231 * png_do_read_transforms actually does the transforms. This means that the 4288 * png_do_read_transforms actually does the transforms. This means that the
4232 * code which effectively calculates this value is actually repeated in three 4289 * code which effectively calculates this value is actually repeated in three
4233 * separate places. They must all match. Innocent changes to the order of 4290 * separate places. They must all match. Innocent changes to the order of
4234 * transformations can and will break libpng in a way that causes memory 4291 * transformations can and will break libpng in a way that causes memory
4235 * overwrites. 4292 * overwrites.
4236 * 4293 *
4237 * TODO: fix this. 4294 * TODO: fix this.
4238 */ 4295 */
4239 #ifdef PNG_READ_PACK_SUPPORTED 4296 #ifdef PNG_READ_PACK_SUPPORTED
4240 if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8) 4297 if ((png_ptr->transformations & PNG_PACK) != 0 && png_ptr->bit_depth < 8)
4241 max_pixel_depth = 8; 4298 max_pixel_depth = 8;
4242 #endif 4299 #endif
4243 4300
4244 #ifdef PNG_READ_EXPAND_SUPPORTED 4301 #ifdef PNG_READ_EXPAND_SUPPORTED
4245 if (png_ptr->transformations & PNG_EXPAND) 4302 if ((png_ptr->transformations & PNG_EXPAND) != 0)
4246 { 4303 {
4247 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) 4304 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
4248 { 4305 {
4249 if (png_ptr->num_trans) 4306 if (png_ptr->num_trans != 0)
4250 max_pixel_depth = 32; 4307 max_pixel_depth = 32;
4251 4308
4252 else 4309 else
4253 max_pixel_depth = 24; 4310 max_pixel_depth = 24;
4254 } 4311 }
4255 4312
4256 else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) 4313 else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
4257 { 4314 {
4258 if (max_pixel_depth < 8) 4315 if (max_pixel_depth < 8)
4259 max_pixel_depth = 8; 4316 max_pixel_depth = 8;
4260 4317
4261 if (png_ptr->num_trans) 4318 if (png_ptr->num_trans != 0)
4262 max_pixel_depth *= 2; 4319 max_pixel_depth *= 2;
4263 } 4320 }
4264 4321
4265 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB) 4322 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
4266 { 4323 {
4267 if (png_ptr->num_trans) 4324 if (png_ptr->num_trans != 0)
4268 { 4325 {
4269 max_pixel_depth *= 4; 4326 max_pixel_depth *= 4;
4270 max_pixel_depth /= 3; 4327 max_pixel_depth /= 3;
4271 } 4328 }
4272 } 4329 }
4273 } 4330 }
4274 #endif 4331 #endif
4275 4332
4276 #ifdef PNG_READ_EXPAND_16_SUPPORTED 4333 #ifdef PNG_READ_EXPAND_16_SUPPORTED
4277 if (png_ptr->transformations & PNG_EXPAND_16) 4334 if ((png_ptr->transformations & PNG_EXPAND_16) != 0)
4278 { 4335 {
4279 # ifdef PNG_READ_EXPAND_SUPPORTED 4336 # ifdef PNG_READ_EXPAND_SUPPORTED
4280 /* In fact it is an error if it isn't supported, but checking is 4337 /* In fact it is an error if it isn't supported, but checking is
4281 * the safe way. 4338 * the safe way.
4282 */ 4339 */
4283 if (png_ptr->transformations & PNG_EXPAND) 4340 if ((png_ptr->transformations & PNG_EXPAND) != 0)
4284 { 4341 {
4285 if (png_ptr->bit_depth < 16) 4342 if (png_ptr->bit_depth < 16)
4286 max_pixel_depth *= 2; 4343 max_pixel_depth *= 2;
4287 } 4344 }
4288 else 4345 else
4289 # endif 4346 # endif
4290 png_ptr->transformations &= ~PNG_EXPAND_16; 4347 png_ptr->transformations &= ~PNG_EXPAND_16;
4291 } 4348 }
4292 #endif 4349 #endif
4293 4350
4294 #ifdef PNG_READ_FILLER_SUPPORTED 4351 #ifdef PNG_READ_FILLER_SUPPORTED
4295 if (png_ptr->transformations & (PNG_FILLER)) 4352 if ((png_ptr->transformations & (PNG_FILLER)) != 0)
4296 { 4353 {
4297 if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) 4354 if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
4298 { 4355 {
4299 if (max_pixel_depth <= 8) 4356 if (max_pixel_depth <= 8)
4300 max_pixel_depth = 16; 4357 max_pixel_depth = 16;
4301 4358
4302 else 4359 else
4303 max_pixel_depth = 32; 4360 max_pixel_depth = 32;
4304 } 4361 }
4305 4362
4306 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB || 4363 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB ||
4307 png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) 4364 png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
4308 { 4365 {
4309 if (max_pixel_depth <= 32) 4366 if (max_pixel_depth <= 32)
4310 max_pixel_depth = 32; 4367 max_pixel_depth = 32;
4311 4368
4312 else 4369 else
4313 max_pixel_depth = 64; 4370 max_pixel_depth = 64;
4314 } 4371 }
4315 } 4372 }
4316 #endif 4373 #endif
4317 4374
4318 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED 4375 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
4319 if (png_ptr->transformations & PNG_GRAY_TO_RGB) 4376 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0)
4320 { 4377 {
4321 if ( 4378 if (
4322 #ifdef PNG_READ_EXPAND_SUPPORTED 4379 #ifdef PNG_READ_EXPAND_SUPPORTED
4323 (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) || 4380 (png_ptr->num_trans != 0 &&
4381 (png_ptr->transformations & PNG_EXPAND) != 0) ||
4324 #endif 4382 #endif
4325 #ifdef PNG_READ_FILLER_SUPPORTED 4383 #ifdef PNG_READ_FILLER_SUPPORTED
4326 (png_ptr->transformations & (PNG_FILLER)) || 4384 (png_ptr->transformations & (PNG_FILLER)) != 0 ||
4327 #endif 4385 #endif
4328 png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) 4386 png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
4329 { 4387 {
4330 if (max_pixel_depth <= 16) 4388 if (max_pixel_depth <= 16)
4331 max_pixel_depth = 32; 4389 max_pixel_depth = 32;
4332 4390
4333 else 4391 else
4334 max_pixel_depth = 64; 4392 max_pixel_depth = 64;
4335 } 4393 }
4336 4394
(...skipping 12 matching lines...) Expand all
4349 max_pixel_depth = 64; 4407 max_pixel_depth = 64;
4350 4408
4351 else 4409 else
4352 max_pixel_depth = 48; 4410 max_pixel_depth = 48;
4353 } 4411 }
4354 } 4412 }
4355 #endif 4413 #endif
4356 4414
4357 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \ 4415 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
4358 defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) 4416 defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
4359 if (png_ptr->transformations & PNG_USER_TRANSFORM) 4417 if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0)
4360 { 4418 {
4361 int user_pixel_depth = png_ptr->user_transform_depth * 4419 int user_pixel_depth = png_ptr->user_transform_depth *
4362 png_ptr->user_transform_channels; 4420 png_ptr->user_transform_channels;
4363 4421
4364 if (user_pixel_depth > max_pixel_depth) 4422 if (user_pixel_depth > max_pixel_depth)
4365 max_pixel_depth = user_pixel_depth; 4423 max_pixel_depth = user_pixel_depth;
4366 } 4424 }
4367 #endif 4425 #endif
4368 4426
4369 /* This value is stored in png_struct and double checked in the row read 4427 /* This value is stored in png_struct and double checked in the row read
(...skipping 15 matching lines...) Expand all
4385 #ifdef PNG_MAX_MALLOC_64K 4443 #ifdef PNG_MAX_MALLOC_64K
4386 if (row_bytes > (png_uint_32)65536L) 4444 if (row_bytes > (png_uint_32)65536L)
4387 png_error(png_ptr, "This image requires a row greater than 64KB"); 4445 png_error(png_ptr, "This image requires a row greater than 64KB");
4388 #endif 4446 #endif
4389 4447
4390 if (row_bytes + 48 > png_ptr->old_big_row_buf_size) 4448 if (row_bytes + 48 > png_ptr->old_big_row_buf_size)
4391 { 4449 {
4392 png_free(png_ptr, png_ptr->big_row_buf); 4450 png_free(png_ptr, png_ptr->big_row_buf);
4393 png_free(png_ptr, png_ptr->big_prev_row); 4451 png_free(png_ptr, png_ptr->big_prev_row);
4394 4452
4395 if (png_ptr->interlaced) 4453 if (png_ptr->interlaced != 0)
4396 png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr, 4454 png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr,
4397 row_bytes + 48); 4455 row_bytes + 48);
4398 4456
4399 else 4457 else
4400 png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes + 48); 4458 png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
4401 4459
4402 png_ptr->big_prev_row = (png_bytep)png_malloc(png_ptr, row_bytes + 48); 4460 png_ptr->big_prev_row = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
4403 4461
4404 #ifdef PNG_ALIGNED_MEMORY_SUPPORTED 4462 #ifdef PNG_ALIGNED_MEMORY_SUPPORTED
4405 /* Use 16-byte aligned memory for row_buf with at least 16 bytes 4463 /* Use 16-byte aligned memory for row_buf with at least 16 bytes
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
4442 png_debug1(3, "iwidth = %u,", png_ptr->iwidth); 4500 png_debug1(3, "iwidth = %u,", png_ptr->iwidth);
4443 png_debug1(3, "num_rows = %u,", png_ptr->num_rows); 4501 png_debug1(3, "num_rows = %u,", png_ptr->num_rows);
4444 png_debug1(3, "rowbytes = %lu,", (unsigned long)png_ptr->rowbytes); 4502 png_debug1(3, "rowbytes = %lu,", (unsigned long)png_ptr->rowbytes);
4445 png_debug1(3, "irowbytes = %lu", 4503 png_debug1(3, "irowbytes = %lu",
4446 (unsigned long)PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1); 4504 (unsigned long)PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1);
4447 4505
4448 /* The sequential reader needs a buffer for IDAT, but the progressive reader 4506 /* The sequential reader needs a buffer for IDAT, but the progressive reader
4449 * does not, so free the read buffer now regardless; the sequential reader 4507 * does not, so free the read buffer now regardless; the sequential reader
4450 * reallocates it on demand. 4508 * reallocates it on demand.
4451 */ 4509 */
4452 if (png_ptr->read_buffer) 4510 if (png_ptr->read_buffer != 0)
4453 { 4511 {
4454 png_bytep buffer = png_ptr->read_buffer; 4512 png_bytep buffer = png_ptr->read_buffer;
4455 4513
4456 png_ptr->read_buffer_size = 0; 4514 png_ptr->read_buffer_size = 0;
4457 png_ptr->read_buffer = NULL; 4515 png_ptr->read_buffer = NULL;
4458 png_free(png_ptr, buffer); 4516 png_free(png_ptr, buffer);
4459 } 4517 }
4460 4518
4461 /* Finally claim the zstream for the inflate of the IDAT data, use the bits 4519 /* Finally claim the zstream for the inflate of the IDAT data, use the bits
4462 * value from the stream (note that this will result in a fatal error if the 4520 * value from the stream (note that this will result in a fatal error if the
4463 * IDAT stream has a bogus deflate header window_bits value, but this should 4521 * IDAT stream has a bogus deflate header window_bits value, but this should
4464 * not be happening any longer!) 4522 * not be happening any longer!)
4465 */ 4523 */
4466 if (png_inflate_claim(png_ptr, png_IDAT) != Z_OK) 4524 if (png_inflate_claim(png_ptr, png_IDAT) != Z_OK)
4467 png_error(png_ptr, png_ptr->zstream.msg); 4525 png_error(png_ptr, png_ptr->zstream.msg);
4468 4526
4469 png_ptr->flags |= PNG_FLAG_ROW_INIT; 4527 png_ptr->flags |= PNG_FLAG_ROW_INIT;
4470 } 4528 }
4471 #endif /* PNG_READ_SUPPORTED */ 4529 #endif /* READ */
OLDNEW
« no previous file with comments | « third_party/libpng/pngrtran.c ('k') | third_party/libpng/pngset.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698