libpng 1.6.32 [August 24, 2017]

Courtesy of @mrfixit3000 ( https://github.com/reicast/reicast-emulator/issues/1121#issuecomment-383257219 )
This commit is contained in:
TwistedUmbrella 2018-04-20 22:46:51 -04:00 committed by Ender's Games
parent 85d271a7b1
commit 66c3cb8a33
26 changed files with 2891 additions and 1779 deletions

View File

@ -1,9 +1,9 @@
/* arm_init.c - NEON optimised filter functions /* arm_init.c - NEON optimised filter functions
* *
* Copyright (c) 2014 Glenn Randers-Pehrson * Copyright (c) 2014,2016 Glenn Randers-Pehrson
* Written by Mans Rullgard, 2011. * Written by Mans Rullgard, 2011.
* Last changed in libpng 1.6.16 [December 22, 2014] * Last changed in libpng 1.6.22 [May 26, 2016]
* *
* This code is released under the libpng license. * This code is released under the libpng license.
* For conditions of distribution and use, see the disclaimer * For conditions of distribution and use, see the disclaimer
@ -66,6 +66,7 @@ png_init_filter_functions_neon(png_structp pp, unsigned int bpp)
* wrong order of the 'ON' and 'default' cases. UNSET now defaults to OFF, * wrong order of the 'ON' and 'default' cases. UNSET now defaults to OFF,
* as documented in png.h * as documented in png.h
*/ */
png_debug(1, "in png_init_filter_functions_neon");
#ifdef PNG_ARM_NEON_API_SUPPORTED #ifdef PNG_ARM_NEON_API_SUPPORTED
switch ((pp->options >> PNG_ARM_NEON) & 3) switch ((pp->options >> PNG_ARM_NEON) & 3)
{ {

View File

@ -1,9 +1,9 @@
/* filter_neon.S - NEON optimised filter functions /* filter_neon.S - NEON optimised filter functions
* *
* Copyright (c) 2014 Glenn Randers-Pehrson * Copyright (c) 2014,2017 Glenn Randers-Pehrson
* Written by Mans Rullgard, 2011. * Written by Mans Rullgard, 2011.
* Last changed in libpng 1.6.16 [December 22, 2014] * Last changed in libpng 1.6.31 [July 27, 2017]
* *
* This code is released under the libpng license. * This code is released under the libpng license.
* For conditions of distribution and use, see the disclaimer * For conditions of distribution and use, see the disclaimer
@ -16,7 +16,7 @@
#define PNG_VERSION_INFO_ONLY #define PNG_VERSION_INFO_ONLY
#include "../pngpriv.h" #include "../pngpriv.h"
#if defined(__linux__) && defined(__ELF__) #if (defined(__linux__) || defined(__FreeBSD__)) && defined(__ELF__)
.section .note.GNU-stack,"",%progbits /* mark stack as non-executable */ .section .note.GNU-stack,"",%progbits /* mark stack as non-executable */
#endif #endif

View File

@ -1,11 +1,11 @@
/* filter_neon_intrinsics.c - NEON optimised filter functions /* filter_neon_intrinsics.c - NEON optimised filter functions
* *
* Copyright (c) 2014 Glenn Randers-Pehrson * Copyright (c) 2014,2016 Glenn Randers-Pehrson
* Written by James Yu <james.yu at linaro.org>, October 2013. * Written by James Yu <james.yu at linaro.org>, October 2013.
* Based on filter_neon.S, written by Mans Rullgard, 2011. * Based on filter_neon.S, written by Mans Rullgard, 2011.
* *
* Last changed in libpng 1.6.16 [December 22, 2014] * Last changed in libpng 1.6.22 [May 26, 2016]
* *
* This code is released under the libpng license. * This code is released under the libpng license.
* For conditions of distribution and use, see the disclaimer * For conditions of distribution and use, see the disclaimer
@ -47,6 +47,8 @@ png_read_filter_row_up_neon(png_row_infop row_info, png_bytep row,
png_bytep rp_stop = row + row_info->rowbytes; png_bytep rp_stop = row + row_info->rowbytes;
png_const_bytep pp = prev_row; png_const_bytep pp = prev_row;
png_debug(1, "in png_read_filter_row_up_neon");
for (; rp < rp_stop; rp += 16, pp += 16) for (; rp < rp_stop; rp += 16, pp += 16)
{ {
uint8x16_t qrp, qpp; uint8x16_t qrp, qpp;
@ -72,6 +74,8 @@ png_read_filter_row_sub3_neon(png_row_infop row_info, png_bytep row,
uint8x8x4_t vdest; uint8x8x4_t vdest;
vdest.val[3] = vdup_n_u8(0); vdest.val[3] = vdup_n_u8(0);
png_debug(1, "in png_read_filter_row_sub3_neon");
for (; rp < rp_stop;) for (; rp < rp_stop;)
{ {
uint8x8_t vtmp1, vtmp2; uint8x8_t vtmp1, vtmp2;
@ -113,6 +117,8 @@ png_read_filter_row_sub4_neon(png_row_infop row_info, png_bytep row,
uint8x8x4_t vdest; uint8x8x4_t vdest;
vdest.val[3] = vdup_n_u8(0); vdest.val[3] = vdup_n_u8(0);
png_debug(1, "in png_read_filter_row_sub4_neon");
for (; rp < rp_stop; rp += 16) for (; rp < rp_stop; rp += 16)
{ {
uint32x2x4_t vtmp = vld4_u32(png_ptr(uint32_t,rp)); uint32x2x4_t vtmp = vld4_u32(png_ptr(uint32_t,rp));
@ -148,6 +154,8 @@ png_read_filter_row_avg3_neon(png_row_infop row_info, png_bytep row,
vrpt = png_ptr(uint8x8x2_t,&vtmp); vrpt = png_ptr(uint8x8x2_t,&vtmp);
vrp = *vrpt; vrp = *vrpt;
png_debug(1, "in png_read_filter_row_avg3_neon");
for (; rp < rp_stop; pp += 12) for (; rp < rp_stop; pp += 12)
{ {
uint8x8_t vtmp1, vtmp2, vtmp3; uint8x8_t vtmp1, vtmp2, vtmp3;
@ -207,6 +215,8 @@ png_read_filter_row_avg4_neon(png_row_infop row_info, png_bytep row,
uint8x8x4_t vdest; uint8x8x4_t vdest;
vdest.val[3] = vdup_n_u8(0); vdest.val[3] = vdup_n_u8(0);
png_debug(1, "in png_read_filter_row_avg4_neon");
for (; rp < rp_stop; rp += 16, pp += 16) for (; rp < rp_stop; rp += 16, pp += 16)
{ {
uint32x2x4_t vtmp; uint32x2x4_t vtmp;
@ -280,6 +290,8 @@ png_read_filter_row_paeth3_neon(png_row_infop row_info, png_bytep row,
vrpt = png_ptr(uint8x8x2_t,&vtmp); vrpt = png_ptr(uint8x8x2_t,&vtmp);
vrp = *vrpt; vrp = *vrpt;
png_debug(1, "in png_read_filter_row_paeth3_neon");
for (; rp < rp_stop; pp += 12) for (; rp < rp_stop; pp += 12)
{ {
uint8x8x2_t *vppt; uint8x8x2_t *vppt;
@ -339,6 +351,8 @@ png_read_filter_row_paeth4_neon(png_row_infop row_info, png_bytep row,
uint8x8x4_t vdest; uint8x8x4_t vdest;
vdest.val[3] = vdup_n_u8(0); vdest.val[3] = vdup_n_u8(0);
png_debug(1, "in png_read_filter_row_paeth4_neon");
for (; rp < rp_stop; rp += 16, pp += 16) for (; rp < rp_stop; rp += 16, pp += 16)
{ {
uint32x2x4_t vtmp; uint32x2x4_t vtmp;

View File

@ -2,13 +2,13 @@
/* config.h.in. Generated from configure.ac by autoheader. */ /* config.h.in. Generated from configure.ac by autoheader. */
/* Define to 1 if you have the <dlfcn.h> header file. */ /* Define to 1 if you have the <dlfcn.h> header file. */
#define HAVE_DLFCN_H 0 #define HAVE_DLFCN_H 1
/* Define to 1 if you have the `feenableexcept' function. */ /* Define to 1 if you have the `feenableexcept' function. */
#define HAVE_FEENABLEEXCEPT 1 #define HAVE_FEENABLEEXCEPT 1
/* Define to 1 if you have the <inttypes.h> header file. */ /* Define to 1 if you have the <inttypes.h> header file. */
#define HAVE_INTTYPES_H 0 #define HAVE_INTTYPES_H 1
/* Define to 1 if you have the `m' library (-lm). */ /* Define to 1 if you have the `m' library (-lm). */
#define HAVE_LIBM 1 #define HAVE_LIBM 1
@ -17,7 +17,7 @@
#define HAVE_LIBZ 1 #define HAVE_LIBZ 1
/* Define to 1 if you have the <memory.h> header file. */ /* Define to 1 if you have the <memory.h> header file. */
#define HAVE_MEMORY_H 0 #define HAVE_MEMORY_H 1
/* Define to 1 if you have the `memset' function. */ /* Define to 1 if you have the `memset' function. */
#define HAVE_MEMSET 1 #define HAVE_MEMSET 1
@ -59,7 +59,7 @@
#define PACKAGE_NAME "libpng" #define PACKAGE_NAME "libpng"
/* Define to the full name and version of this package. */ /* Define to the full name and version of this package. */
#define PACKAGE_STRING "libpng 1.6.18" #define PACKAGE_STRING "libpng 1.6.32"
/* Define to the one symbol short name of this package. */ /* Define to the one symbol short name of this package. */
#define PACKAGE_TARNAME "libpng" #define PACKAGE_TARNAME "libpng"
@ -68,7 +68,7 @@
#define PACKAGE_URL "" #define PACKAGE_URL ""
/* Define to the version of this package. */ /* Define to the version of this package. */
#define PACKAGE_VERSION "1.6.18" #define PACKAGE_VERSION "1.6.32"
/* Turn on ARM Neon optimizations at run-time */ /* Turn on ARM Neon optimizations at run-time */
/* #undef PNG_ARM_NEON_API_SUPPORTED */ /* #undef PNG_ARM_NEON_API_SUPPORTED */
@ -77,7 +77,28 @@
/* #undef PNG_ARM_NEON_CHECK_SUPPORTED */ /* #undef PNG_ARM_NEON_CHECK_SUPPORTED */
/* Enable ARM Neon optimizations */ /* Enable ARM Neon optimizations */
/* #undef PNG_ARM_NEON_OPT */ #define PNG_ARM_NEON_OPT 2
/* Enable Intel SSE optimizations */
#define PNG_INTEL_SSE_OPT 0
/* Turn on MIPS MSA optimizations at run-time */
/* #undef PNG_MIPS_MSA_API_SUPPORTED */
/* Check for MIPS MSA support at run-time */
/* #undef PNG_MIPS_MSA_CHECK_SUPPORTED */
/* Enable MIPS MSA optimizations */
/* #undef PNG_MIPS_MSA_OPT */
/* Turn on POWERPC VSX optimizations at run-time */
/* #undef PNG_POWERPC_VSX_API_SUPPORTED */
/* Check for POWERPC VSX support at run-time */
/* #undef PNG_POWERPC_VSX_CHECK_SUPPORTED */
/* Enable POWERPC VSX optimizations */
/* #undef PNG_POWERPC_VSX_OPT */
/* Define to 1 if you have the ANSI C header files. */ /* Define to 1 if you have the ANSI C header files. */
#define STDC_HEADERS 1 #define STDC_HEADERS 1
@ -86,7 +107,7 @@
/* #undef TM_IN_SYS_TIME */ /* #undef TM_IN_SYS_TIME */
/* Version number of package */ /* Version number of package */
#define VERSION "1.6.18" #define VERSION "1.6.32"
/* Define to empty if `const' does not conform to ANSI C. */ /* Define to empty if `const' does not conform to ANSI C. */
/* #undef const */ /* #undef const */

View File

@ -1,8 +1,8 @@
/* png.c - location for general purpose libpng functions /* png.c - location for general purpose libpng functions
* *
* Last changed in libpng 1.6.18 [July 23, 2015] * Last changed in libpng 1.6.32 [August 24, 2017]
* Copyright (c) 1998-2015 Glenn Randers-Pehrson * Copyright (c) 1998-2002,2004,2006-2017 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
* *
@ -14,7 +14,27 @@
#include "pngpriv.h" #include "pngpriv.h"
/* Generate a compiler error if there is an old png.h in the search path. */ /* Generate a compiler error if there is an old png.h in the search path. */
typedef png_libpng_version_1_6_18 Your_png_h_is_not_version_1_6_18; typedef png_libpng_version_1_6_32 Your_png_h_is_not_version_1_6_32;
#ifdef __GNUC__
/* The version tests may need to be added to, but the problem warning has
* consistently been fixed in GCC versions which obtain wide-spread release.
* The problem is that many versions of GCC rearrange comparison expressions in
* the optimizer in such a way that the results of the comparison will change
* if signed integer overflow occurs. Such comparisons are not permitted in
* ANSI C90, however GCC isn't clever enough to work out that that do not occur
* below in png_ascii_from_fp and png_muldiv, so it produces a warning with
* -Wextra. Unfortunately this is highly dependent on the optimizer and the
* machine architecture so the warning comes and goes unpredictably and is
* impossible to "fix", even were that a good idea.
*/
#if __GNUC__ == 7 && __GNUC_MINOR__ == 1
#define GCC_STRICT_OVERFLOW 1
#endif /* GNU 7.1.x */
#endif /* GNU */
#ifndef GCC_STRICT_OVERFLOW
#define GCC_STRICT_OVERFLOW 0
#endif
/* Tells libpng that we have already handled the first "num_bytes" bytes /* Tells libpng that we have already handled the first "num_bytes" bytes
* of the PNG file signature. If the PNG data is embedded into another * of the PNG file signature. If the PNG data is embedded into another
@ -26,15 +46,20 @@ typedef png_libpng_version_1_6_18 Your_png_h_is_not_version_1_6_18;
void PNGAPI void PNGAPI
png_set_sig_bytes(png_structrp png_ptr, int num_bytes) png_set_sig_bytes(png_structrp png_ptr, int num_bytes)
{ {
unsigned int nb = (unsigned int)num_bytes;
png_debug(1, "in png_set_sig_bytes"); png_debug(1, "in png_set_sig_bytes");
if (png_ptr == NULL) if (png_ptr == NULL)
return; return;
if (num_bytes > 8) if (num_bytes < 0)
nb = 0;
if (nb > 8)
png_error(png_ptr, "Too many bytes for PNG signature"); png_error(png_ptr, "Too many bytes for PNG signature");
png_ptr->sig_bytes = (png_byte)((num_bytes < 0 ? 0 : num_bytes) & 0xff); png_ptr->sig_bytes = (png_byte)nb;
} }
/* Checks whether the supplied bytes match the PNG signature. We allow /* Checks whether the supplied bytes match the PNG signature. We allow
@ -413,6 +438,8 @@ png_info_init_3,(png_infopp ptr_ptr, png_size_t png_info_struct_size),
free(info_ptr); free(info_ptr);
info_ptr = png_voidcast(png_inforp, png_malloc_base(NULL, info_ptr = png_voidcast(png_inforp, png_malloc_base(NULL,
(sizeof *info_ptr))); (sizeof *info_ptr)));
if (info_ptr == NULL)
return;
*ptr_ptr = info_ptr; *ptr_ptr = info_ptr;
} }
@ -451,7 +478,7 @@ png_free_data(png_const_structrp png_ptr, png_inforp info_ptr, png_uint_32 mask,
#ifdef PNG_TEXT_SUPPORTED #ifdef PNG_TEXT_SUPPORTED
/* Free text item num or (if num == -1) all text items */ /* Free text item num or (if num == -1) all text items */
if (info_ptr->text != 0 && if (info_ptr->text != NULL &&
((mask & PNG_FREE_TEXT) & info_ptr->free_me) != 0) ((mask & PNG_FREE_TEXT) & info_ptr->free_me) != 0)
{ {
if (num != -1) if (num != -1)
@ -470,6 +497,7 @@ png_free_data(png_const_structrp png_ptr, png_inforp info_ptr, png_uint_32 mask,
png_free(png_ptr, info_ptr->text); png_free(png_ptr, info_ptr->text);
info_ptr->text = NULL; info_ptr->text = NULL;
info_ptr->num_text = 0; info_ptr->num_text = 0;
info_ptr->max_text = 0;
} }
} }
#endif #endif
@ -534,7 +562,7 @@ png_free_data(png_const_structrp png_ptr, png_inforp info_ptr, png_uint_32 mask,
#ifdef PNG_sPLT_SUPPORTED #ifdef PNG_sPLT_SUPPORTED
/* Free a given sPLT entry, or (if num == -1) all sPLT entries */ /* Free a given sPLT entry, or (if num == -1) all sPLT entries */
if (info_ptr->splt_palettes != 0 && if (info_ptr->splt_palettes != NULL &&
((mask & PNG_FREE_SPLT) & info_ptr->free_me) != 0) ((mask & PNG_FREE_SPLT) & info_ptr->free_me) != 0)
{ {
if (num != -1) if (num != -1)
@ -564,7 +592,7 @@ png_free_data(png_const_structrp png_ptr, png_inforp info_ptr, png_uint_32 mask,
#endif #endif
#ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED #ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
if (info_ptr->unknown_chunks != 0 && if (info_ptr->unknown_chunks != NULL &&
((mask & PNG_FREE_UNKN) & info_ptr->free_me) != 0) ((mask & PNG_FREE_UNKN) & info_ptr->free_me) != 0)
{ {
if (num != -1) if (num != -1)
@ -587,6 +615,26 @@ png_free_data(png_const_structrp png_ptr, png_inforp info_ptr, png_uint_32 mask,
} }
#endif #endif
#ifdef PNG_eXIf_SUPPORTED
/* Free any eXIf entry */
if (((mask & PNG_FREE_EXIF) & info_ptr->free_me) != 0)
{
# ifdef PNG_READ_eXIf_SUPPORTED
if (info_ptr->eXIf_buf)
{
png_free(png_ptr, info_ptr->eXIf_buf);
info_ptr->eXIf_buf = NULL;
}
# endif
if (info_ptr->exif)
{
png_free(png_ptr, info_ptr->exif);
info_ptr->exif = NULL;
}
info_ptr->valid &= ~PNG_INFO_eXIf;
}
#endif
#ifdef PNG_hIST_SUPPORTED #ifdef PNG_hIST_SUPPORTED
/* Free any hIST entry */ /* Free any hIST entry */
if (((mask & PNG_FREE_HIST) & info_ptr->free_me) != 0) if (((mask & PNG_FREE_HIST) & info_ptr->free_me) != 0)
@ -610,7 +658,7 @@ png_free_data(png_const_structrp png_ptr, png_inforp info_ptr, png_uint_32 mask,
/* Free any image bits attached to the info structure */ /* Free any image bits attached to the info structure */
if (((mask & PNG_FREE_ROWS) & info_ptr->free_me) != 0) if (((mask & PNG_FREE_ROWS) & info_ptr->free_me) != 0)
{ {
if (info_ptr->row_pointers != 0) if (info_ptr->row_pointers != NULL)
{ {
png_uint_32 row; png_uint_32 row;
for (row = 0; row < info_ptr->height; row++) for (row = 0; row < info_ptr->height; row++)
@ -664,19 +712,20 @@ png_init_io(png_structrp png_ptr, png_FILE_p fp)
# endif # endif
# ifdef PNG_SAVE_INT_32_SUPPORTED # ifdef PNG_SAVE_INT_32_SUPPORTED
/* The png_save_int_32 function assumes integers are stored in two's /* PNG signed integers are saved in 32-bit 2's complement format. ANSI C-90
* complement format. If this isn't the case, then this routine needs to * defines a cast of a signed integer to an unsigned integer either to preserve
* be modified to write data in two's complement format. Note that, * the value, if it is positive, or to calculate:
* the following works correctly even if png_int_32 has more than 32 bits *
* (compare the more complex code required on read for sign extension.) * (UNSIGNED_MAX+1) + integer
*
* Where UNSIGNED_MAX is the appropriate maximum unsigned value, so when the
* negative integral value is added the result will be an unsigned value
* correspnding to the 2's complement representation.
*/ */
void PNGAPI void PNGAPI
png_save_int_32(png_bytep buf, png_int_32 i) png_save_int_32(png_bytep buf, png_int_32 i)
{ {
buf[0] = (png_byte)((i >> 24) & 0xff); png_save_uint_32(buf, (png_uint_32)i);
buf[1] = (png_byte)((i >> 16) & 0xff);
buf[2] = (png_byte)((i >> 8) & 0xff);
buf[3] = (png_byte)(i & 0xff);
} }
# endif # endif
@ -722,6 +771,7 @@ png_convert_to_rfc1123_buffer(char out[29], png_const_timep ptime)
APPEND(':'); APPEND(':');
APPEND_NUMBER(PNG_NUMBER_FORMAT_02u, (unsigned)ptime->second); APPEND_NUMBER(PNG_NUMBER_FORMAT_02u, (unsigned)ptime->second);
APPEND_STRING(" +0000"); /* This reliably terminates the buffer */ APPEND_STRING(" +0000"); /* This reliably terminates the buffer */
PNG_UNUSED (pos)
# undef APPEND # undef APPEND
# undef APPEND_NUMBER # undef APPEND_NUMBER
@ -766,14 +816,15 @@ png_get_copyright(png_const_structrp png_ptr)
#else #else
# ifdef __STDC__ # ifdef __STDC__
return PNG_STRING_NEWLINE \ return PNG_STRING_NEWLINE \
"libpng version 1.6.18 - July 23, 2015" PNG_STRING_NEWLINE \ "libpng version 1.6.32 - August 24, 2017" PNG_STRING_NEWLINE \
"Copyright (c) 1998-2015 Glenn Randers-Pehrson" PNG_STRING_NEWLINE \ "Copyright (c) 1998-2002,2004,2006-2017 Glenn Randers-Pehrson" \
PNG_STRING_NEWLINE \
"Copyright (c) 1996-1997 Andreas Dilger" PNG_STRING_NEWLINE \ "Copyright (c) 1996-1997 Andreas Dilger" PNG_STRING_NEWLINE \
"Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc." \ "Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc." \
PNG_STRING_NEWLINE; PNG_STRING_NEWLINE;
# else # else
return "libpng version 1.6.18 - July 23, 2015\ return "libpng version 1.6.32 - August 24, 2017\
Copyright (c) 1998-2015 Glenn Randers-Pehrson\ Copyright (c) 1998-2002,2004,2006-2017 Glenn Randers-Pehrson\
Copyright (c) 1996-1997 Andreas Dilger\ Copyright (c) 1996-1997 Andreas Dilger\
Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc."; Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.";
# endif # endif
@ -1703,7 +1754,6 @@ png_colorspace_set_chromaticities(png_const_structrp png_ptr,
*/ */
colorspace->flags |= PNG_COLORSPACE_INVALID; colorspace->flags |= PNG_COLORSPACE_INVALID;
png_error(png_ptr, "internal error checking chromaticities"); png_error(png_ptr, "internal error checking chromaticities");
break;
} }
return 0; /* failed */ return 0; /* failed */
@ -1731,7 +1781,6 @@ png_colorspace_set_endpoints(png_const_structrp png_ptr,
default: default:
colorspace->flags |= PNG_COLORSPACE_INVALID; colorspace->flags |= PNG_COLORSPACE_INVALID;
png_error(png_ptr, "internal error checking chromaticities"); png_error(png_ptr, "internal error checking chromaticities");
break;
} }
return 0; /* failed */ return 0; /* failed */
@ -1923,8 +1972,8 @@ png_colorspace_set_sRGB(png_const_structrp png_ptr, png_colorspacerp colorspace,
static const png_byte D50_nCIEXYZ[12] = static const png_byte D50_nCIEXYZ[12] =
{ 0x00, 0x00, 0xf6, 0xd6, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xd3, 0x2d }; { 0x00, 0x00, 0xf6, 0xd6, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xd3, 0x2d };
int /* PRIVATE */ static int /* bool */
png_icc_check_length(png_const_structrp png_ptr, png_colorspacerp colorspace, icc_check_length(png_const_structrp png_ptr, png_colorspacerp colorspace,
png_const_charp name, png_uint_32 profile_length) png_const_charp name, png_uint_32 profile_length)
{ {
if (profile_length < 132) if (profile_length < 132)
@ -1934,6 +1983,40 @@ png_icc_check_length(png_const_structrp png_ptr, png_colorspacerp colorspace,
return 1; return 1;
} }
#ifdef PNG_READ_iCCP_SUPPORTED
int /* PRIVATE */
png_icc_check_length(png_const_structrp png_ptr, png_colorspacerp colorspace,
png_const_charp name, png_uint_32 profile_length)
{
if (!icc_check_length(png_ptr, colorspace, name, profile_length))
return 0;
/* This needs to be here because the 'normal' check is in
* png_decompress_chunk, yet this happens after the attempt to
* png_malloc_base the required data. We only need this on read; on write
* the caller supplies the profile buffer so libpng doesn't allocate it. See
* the call to icc_check_length below (the write case).
*/
# ifdef PNG_SET_USER_LIMITS_SUPPORTED
else if (png_ptr->user_chunk_malloc_max > 0 &&
png_ptr->user_chunk_malloc_max < profile_length)
return png_icc_profile_error(png_ptr, colorspace, name, profile_length,
"exceeds application limits");
# elif PNG_USER_CHUNK_MALLOC_MAX > 0
else if (PNG_USER_CHUNK_MALLOC_MAX < profile_length)
return png_icc_profile_error(png_ptr, colorspace, name, profile_length,
"exceeds libpng limits");
# else /* !SET_USER_LIMITS */
/* This will get compiled out on all 32-bit and better systems. */
else if (PNG_SIZE_MAX < profile_length)
return png_icc_profile_error(png_ptr, colorspace, name, profile_length,
"exceeds system limits");
# endif /* !SET_USER_LIMITS */
return 1;
}
#endif /* READ_iCCP */
int /* PRIVATE */ int /* PRIVATE */
png_icc_check_header(png_const_structrp png_ptr, png_colorspacerp colorspace, png_icc_check_header(png_const_structrp png_ptr, png_colorspacerp colorspace,
png_const_charp name, png_uint_32 profile_length, png_const_charp name, png_uint_32 profile_length,
@ -2057,8 +2140,8 @@ png_icc_check_header(png_const_structrp png_ptr, png_colorspacerp colorspace,
temp = png_get_uint_32(profile+12); /* profile/device class */ temp = png_get_uint_32(profile+12); /* profile/device class */
switch (temp) switch (temp)
{ {
case 0x73636E72: /* 'scnr' */ case 0x73636e72: /* 'scnr' */
case 0x6D6E7472: /* 'mntr' */ case 0x6d6e7472: /* 'mntr' */
case 0x70727472: /* 'prtr' */ case 0x70727472: /* 'prtr' */
case 0x73706163: /* 'spac' */ case 0x73706163: /* 'spac' */
/* All supported */ /* All supported */
@ -2069,7 +2152,7 @@ png_icc_check_header(png_const_structrp png_ptr, png_colorspacerp colorspace,
return png_icc_profile_error(png_ptr, colorspace, name, temp, return png_icc_profile_error(png_ptr, colorspace, name, temp,
"invalid embedded Abstract ICC profile"); "invalid embedded Abstract ICC profile");
case 0x6C696E6B: /* 'link' */ case 0x6c696e6b: /* 'link' */
/* DeviceLink profiles cannot be interpreted in a non-device specific /* DeviceLink profiles cannot be interpreted in a non-device specific
* fashion, if an app uses the AToB0Tag in the profile the results are * fashion, if an app uses the AToB0Tag in the profile the results are
* undefined unless the result is sent to the intended device, * undefined unless the result is sent to the intended device,
@ -2079,7 +2162,7 @@ png_icc_check_header(png_const_structrp png_ptr, png_colorspacerp colorspace,
return png_icc_profile_error(png_ptr, colorspace, name, temp, return png_icc_profile_error(png_ptr, colorspace, name, temp,
"unexpected DeviceLink ICC profile class"); "unexpected DeviceLink ICC profile class");
case 0x6E6D636C: /* 'nmcl' */ case 0x6e6d636c: /* 'nmcl' */
/* A NamedColor profile is also device specific, however it doesn't /* A NamedColor profile is also device specific, however it doesn't
* contain an AToB0 tag that is open to misinterpretation. Almost * contain an AToB0 tag that is open to misinterpretation. Almost
* certainly it will fail the tests below. * certainly it will fail the tests below.
@ -2105,8 +2188,8 @@ png_icc_check_header(png_const_structrp png_ptr, png_colorspacerp colorspace,
temp = png_get_uint_32(profile+20); temp = png_get_uint_32(profile+20);
switch (temp) switch (temp)
{ {
case 0x58595A20: /* 'XYZ ' */ case 0x58595a20: /* 'XYZ ' */
case 0x4C616220: /* 'Lab ' */ case 0x4c616220: /* 'Lab ' */
break; break;
default: default:
@ -2276,7 +2359,7 @@ png_compare_ICC_profile_with_sRGB(png_const_structrp png_ptr,
} }
/* Length *and* intent must match */ /* Length *and* intent must match */
if (length == png_sRGB_checks[i].length && if (length == (png_uint_32) png_sRGB_checks[i].length &&
intent == (png_uint_32) png_sRGB_checks[i].intent) intent == (png_uint_32) png_sRGB_checks[i].intent)
{ {
/* Now calculate the adler32 if not done already. */ /* Now calculate the adler32 if not done already. */
@ -2346,7 +2429,6 @@ png_compare_ICC_profile_with_sRGB(png_const_structrp png_ptr,
return 0; /* no match */ return 0; /* no match */
} }
#endif /* PNG_sRGB_PROFILE_CHECKS >= 0 */
void /* PRIVATE */ void /* PRIVATE */
png_icc_set_sRGB(png_const_structrp png_ptr, png_icc_set_sRGB(png_const_structrp png_ptr,
@ -2355,12 +2437,11 @@ png_icc_set_sRGB(png_const_structrp png_ptr,
/* Is this profile one of the known ICC sRGB profiles? If it is, just set /* Is this profile one of the known ICC sRGB profiles? If it is, just set
* the sRGB information. * the sRGB information.
*/ */
#if PNG_sRGB_PROFILE_CHECKS >= 0
if (png_compare_ICC_profile_with_sRGB(png_ptr, profile, adler) != 0) if (png_compare_ICC_profile_with_sRGB(png_ptr, profile, adler) != 0)
#endif
(void)png_colorspace_set_sRGB(png_ptr, colorspace, (void)png_colorspace_set_sRGB(png_ptr, colorspace,
(int)/*already checked*/png_get_uint_32(profile+64)); (int)/*already checked*/png_get_uint_32(profile+64));
} }
#endif /* PNG_sRGB_PROFILE_CHECKS >= 0 */
#endif /* sRGB */ #endif /* sRGB */
int /* PRIVATE */ int /* PRIVATE */
@ -2371,13 +2452,13 @@ png_colorspace_set_ICC(png_const_structrp png_ptr, png_colorspacerp colorspace,
if ((colorspace->flags & PNG_COLORSPACE_INVALID) != 0) if ((colorspace->flags & PNG_COLORSPACE_INVALID) != 0)
return 0; return 0;
if (png_icc_check_length(png_ptr, colorspace, name, profile_length) != 0 && if (icc_check_length(png_ptr, colorspace, name, profile_length) != 0 &&
png_icc_check_header(png_ptr, colorspace, name, profile_length, profile, png_icc_check_header(png_ptr, colorspace, name, profile_length, profile,
color_type) != 0 && color_type) != 0 &&
png_icc_check_tag_table(png_ptr, colorspace, name, profile_length, png_icc_check_tag_table(png_ptr, colorspace, name, profile_length,
profile) != 0) profile) != 0)
{ {
# ifdef PNG_sRGB_SUPPORTED # if defined(PNG_sRGB_SUPPORTED) && PNG_sRGB_PROFILE_CHECKS >= 0
/* If no sRGB support, don't try storing sRGB information */ /* If no sRGB support, don't try storing sRGB information */
png_icc_set_sRGB(png_ptr, colorspace, profile, 0); png_icc_set_sRGB(png_ptr, colorspace, profile, 0);
# endif # endif
@ -2489,7 +2570,7 @@ png_check_IHDR(png_const_structrp png_ptr,
error = 1; error = 1;
} }
if (png_gt(((width + 7) & (~7)), if (png_gt(((width + 7) & (~7U)),
((PNG_SIZE_MAX ((PNG_SIZE_MAX
- 48 /* big_row_buf hack */ - 48 /* big_row_buf hack */
- 1) /* filter byte */ - 1) /* filter byte */
@ -2791,7 +2872,7 @@ png_pow10(int power)
if (power < 0) if (power < 0)
{ {
if (power < DBL_MIN_10_EXP) return 0; if (power < DBL_MIN_10_EXP) return 0;
recip = 1, power = -power; recip = 1; power = -power;
} }
if (power > 0) if (power > 0)
@ -2816,6 +2897,14 @@ png_pow10(int power)
/* Function to format a floating point value in ASCII with a given /* Function to format a floating point value in ASCII with a given
* precision. * precision.
*/ */
#if GCC_STRICT_OVERFLOW
#pragma GCC diagnostic push
/* The problem arises below with exp_b10, which can never overflow because it
* comes, originally, from frexp and is therefore limited to a range which is
* typically +/-710 (log2(DBL_MAX)/log2(DBL_MIN)).
*/
#pragma GCC diagnostic warning "-Wstrict-overflow=2"
#endif /* GCC_STRICT_OVERFLOW */
void /* PRIVATE */ void /* PRIVATE */
png_ascii_from_fp(png_const_structrp png_ptr, png_charp ascii, png_size_t size, png_ascii_from_fp(png_const_structrp png_ptr, png_charp ascii, png_size_t size,
double fp, unsigned int precision) double fp, unsigned int precision)
@ -2869,7 +2958,9 @@ png_ascii_from_fp(png_const_structrp png_ptr, png_charp ascii, png_size_t size,
double test = png_pow10(exp_b10+1); double test = png_pow10(exp_b10+1);
if (test <= DBL_MAX) if (test <= DBL_MAX)
++exp_b10, base = test; {
++exp_b10; base = test;
}
else else
break; break;
@ -2883,7 +2974,10 @@ png_ascii_from_fp(png_const_structrp png_ptr, png_charp ascii, png_size_t size,
* test on DBL_MAX above. * test on DBL_MAX above.
*/ */
fp /= base; fp /= base;
while (fp >= 1) fp /= 10, ++exp_b10; while (fp >= 1)
{
fp /= 10; ++exp_b10;
}
/* Because of the code above fp may, at this point, be /* Because of the code above fp may, at this point, be
* less than .1, this is ok because the code below can * less than .1, this is ok because the code below can
@ -2900,7 +2994,7 @@ png_ascii_from_fp(png_const_structrp png_ptr, png_charp ascii, png_size_t size,
*/ */
if (exp_b10 < 0 && exp_b10 > -3) /* PLUS 3 TOTAL 4 */ if (exp_b10 < 0 && exp_b10 > -3) /* PLUS 3 TOTAL 4 */
{ {
czero = -exp_b10; /* PLUS 2 digits: TOTAL 3 */ czero = 0U-exp_b10; /* PLUS 2 digits: TOTAL 3 */
exp_b10 = 0; /* Dot added below before first output. */ exp_b10 = 0; /* Dot added below before first output. */
} }
else else
@ -2934,7 +3028,7 @@ png_ascii_from_fp(png_const_structrp png_ptr, png_charp ascii, png_size_t size,
/* Rounding up to 10, handle that here. */ /* Rounding up to 10, handle that here. */
if (czero > 0) if (czero > 0)
{ {
--czero, d = 1; --czero; d = 1;
if (cdigits == 0) --clead; if (cdigits == 0) --clead;
} }
else else
@ -2948,7 +3042,7 @@ png_ascii_from_fp(png_const_structrp png_ptr, png_charp ascii, png_size_t size,
else if (ch == 46) else if (ch == 46)
{ {
ch = *--ascii, ++size; ch = *--ascii; ++size;
/* Advance exp_b10 to '1', so that the /* Advance exp_b10 to '1', so that the
* decimal point happens after the * decimal point happens after the
* previous digit. * previous digit.
@ -2975,7 +3069,9 @@ png_ascii_from_fp(png_const_structrp png_ptr, png_charp ascii, png_size_t size,
int ch = *--ascii; int ch = *--ascii;
if (ch == 46) if (ch == 46)
++size, exp_b10 = 1; {
++size; exp_b10 = 1;
}
/* Else lost a leading zero, so 'exp_b10' is /* Else lost a leading zero, so 'exp_b10' is
* still ok at (-1) * still ok at (-1)
@ -3011,21 +3107,26 @@ png_ascii_from_fp(png_const_structrp png_ptr, png_charp ascii, png_size_t size,
*/ */
if (exp_b10 != (-1)) if (exp_b10 != (-1))
{ {
if (exp_b10 == 0) *ascii++ = 46, --size; if (exp_b10 == 0)
{
*ascii++ = 46; --size;
}
/* PLUS 1: TOTAL 4 */ /* PLUS 1: TOTAL 4 */
--exp_b10; --exp_b10;
} }
*ascii++ = 48, --czero; *ascii++ = 48; --czero;
} }
if (exp_b10 != (-1)) if (exp_b10 != (-1))
{ {
if (exp_b10 == 0) if (exp_b10 == 0)
*ascii++ = 46, --size; /* counted above */ {
*ascii++ = 46; --size; /* counted above */
}
--exp_b10; --exp_b10;
} }
*ascii++ = (char)(48 + (int)d), ++cdigits; *ascii++ = (char)(48 + (int)d); ++cdigits;
} }
} }
while (cdigits+czero < precision+clead && fp > DBL_MIN); while (cdigits+czero < precision+clead && fp > DBL_MIN);
@ -3034,7 +3135,7 @@ png_ascii_from_fp(png_const_structrp png_ptr, png_charp ascii, png_size_t size,
/* Check for an exponent, if we don't need one we are /* Check for an exponent, if we don't need one we are
* done and just need to terminate the string. At * done and just need to terminate the string. At
* this point exp_b10==(-1) is effectively if flag - it got * this point exp_b10==(-1) is effectively a flag - it got
* to '-1' because of the decrement after outputting * to '-1' because of the decrement after outputting
* the decimal point above (the exponent required is * the decimal point above (the exponent required is
* *not* -1!) * *not* -1!)
@ -3048,7 +3149,7 @@ png_ascii_from_fp(png_const_structrp png_ptr, png_charp ascii, png_size_t size,
* zeros were *not* output, so this doesn't increase * zeros were *not* output, so this doesn't increase
* the output count. * the output count.
*/ */
while (--exp_b10 >= 0) *ascii++ = 48; while (exp_b10-- > 0) *ascii++ = 48;
*ascii = 0; *ascii = 0;
@ -3066,7 +3167,7 @@ png_ascii_from_fp(png_const_structrp png_ptr, png_charp ascii, png_size_t size,
*/ */
size -= cdigits; size -= cdigits;
*ascii++ = 69, --size; /* 'E': PLUS 1 TOTAL 2+precision */ *ascii++ = 69; --size; /* 'E': PLUS 1 TOTAL 2+precision */
/* The following use of an unsigned temporary avoids ambiguities in /* The following use of an unsigned temporary avoids ambiguities in
* the signed arithmetic on exp_b10 and permits GCC at least to do * the signed arithmetic on exp_b10 and permits GCC at least to do
@ -3077,12 +3178,12 @@ png_ascii_from_fp(png_const_structrp png_ptr, png_charp ascii, png_size_t size,
if (exp_b10 < 0) if (exp_b10 < 0)
{ {
*ascii++ = 45, --size; /* '-': PLUS 1 TOTAL 3+precision */ *ascii++ = 45; --size; /* '-': PLUS 1 TOTAL 3+precision */
uexp_b10 = -exp_b10; uexp_b10 = 0U-exp_b10;
} }
else else
uexp_b10 = exp_b10; uexp_b10 = 0U+exp_b10;
cdigits = 0; cdigits = 0;
@ -3125,6 +3226,9 @@ png_ascii_from_fp(png_const_structrp png_ptr, png_charp ascii, png_size_t size,
/* Here on buffer too small. */ /* Here on buffer too small. */
png_error(png_ptr, "ASCII conversion buffer too small"); png_error(png_ptr, "ASCII conversion buffer too small");
} }
#if GCC_STRICT_OVERFLOW
#pragma GCC diagnostic pop
#endif /* GCC_STRICT_OVERFLOW */
# endif /* FLOATING_POINT */ # endif /* FLOATING_POINT */
@ -3144,9 +3248,11 @@ png_ascii_from_fixed(png_const_structrp png_ptr, png_charp ascii,
/* Avoid overflow here on the minimum integer. */ /* Avoid overflow here on the minimum integer. */
if (fp < 0) if (fp < 0)
*ascii++ = 45, --size, num = -fp; {
*ascii++ = 45; num = (png_uint_32)(-fp);
}
else else
num = fp; num = (png_uint_32)fp;
if (num <= 0x80000000) /* else overflowed */ if (num <= 0x80000000) /* else overflowed */
{ {
@ -3182,7 +3288,10 @@ png_ascii_from_fixed(png_const_structrp png_ptr, png_charp ascii,
* then ndigits digits to first: * then ndigits digits to first:
*/ */
i = 5; i = 5;
while (ndigits < i) *ascii++ = 48, --i; while (ndigits < i)
{
*ascii++ = 48; --i;
}
while (ndigits >= first) *ascii++ = digits[--ndigits]; while (ndigits >= first) *ascii++ = digits[--ndigits];
/* Don't output the trailing zeros! */ /* Don't output the trailing zeros! */
} }
@ -3233,6 +3342,15 @@ png_fixed(png_const_structrp png_ptr, double fp, png_const_charp text)
* the nearest .00001). Overflow and divide by zero are signalled in * the nearest .00001). Overflow and divide by zero are signalled in
* the result, a boolean - true on success, false on overflow. * the result, a boolean - true on success, false on overflow.
*/ */
#if GCC_STRICT_OVERFLOW /* from above */
/* It is not obvious which comparison below gets optimized in such a way that
* signed overflow would change the result; looking through the code does not
* reveal any tests which have the form GCC complains about, so presumably the
* optimizer is moving an add or subtract into the 'if' somewhere.
*/
#pragma GCC diagnostic push
#pragma GCC diagnostic warning "-Wstrict-overflow=2"
#endif /* GCC_STRICT_OVERFLOW */
int int
png_muldiv(png_fixed_point_p res, png_fixed_point a, png_int_32 times, png_muldiv(png_fixed_point_p res, png_fixed_point a, png_int_32 times,
png_int_32 divisor) png_int_32 divisor)
@ -3347,6 +3465,9 @@ png_muldiv(png_fixed_point_p res, png_fixed_point a, png_int_32 times,
return 0; return 0;
} }
#if GCC_STRICT_OVERFLOW
#pragma GCC diagnostic pop
#endif /* GCC_STRICT_OVERFLOW */
#endif /* READ_GAMMA || INCH_CONVERSIONS */ #endif /* READ_GAMMA || INCH_CONVERSIONS */
#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_INCH_CONVERSIONS_SUPPORTED) #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_INCH_CONVERSIONS_SUPPORTED)
@ -3675,7 +3796,7 @@ png_exp(png_fixed_point x)
if (x > 0 && x <= 0xfffff) /* Else overflow or zero (underflow) */ if (x > 0 && x <= 0xfffff) /* Else overflow or zero (underflow) */
{ {
/* Obtain a 4-bit approximation */ /* Obtain a 4-bit approximation */
png_uint_32 e = png_32bit_exp[(x >> 12) & 0xf]; png_uint_32 e = png_32bit_exp[(x >> 12) & 0x0f];
/* Incorporate the low 12 bits - these decrease the returned value by /* Incorporate the low 12 bits - these decrease the returned value by
* multiplying by a number less than 1 if the bit is set. The multiplier * multiplying by a number less than 1 if the bit is set. The multiplier
@ -4084,9 +4205,9 @@ png_build_gamma_table(png_structrp png_ptr, int bit_depth)
/* Remove any existing table; this copes with multiple calls to /* Remove any existing table; this copes with multiple calls to
* png_read_update_info. The warning is because building the gamma tables * png_read_update_info. The warning is because building the gamma tables
* multiple times is a performance hit - it's harmless but the ability to call * multiple times is a performance hit - it's harmless but the ability to
* png_read_update_info() multiple times is new in 1.5.6 so it seems sensible * call png_read_update_info() multiple times is new in 1.5.6 so it seems
* to warn if the app introduces such a hit. * sensible to warn if the app introduces such a hit.
*/ */
if (png_ptr->gamma_table != NULL || png_ptr->gamma_16_table != NULL) if (png_ptr->gamma_table != NULL || png_ptr->gamma_16_table != NULL)
{ {
@ -4097,7 +4218,8 @@ png_build_gamma_table(png_structrp png_ptr, int bit_depth)
if (bit_depth <= 8) if (bit_depth <= 8)
{ {
png_build_8bit_table(png_ptr, &png_ptr->gamma_table, png_build_8bit_table(png_ptr, &png_ptr->gamma_table,
png_ptr->screen_gamma > 0 ? png_reciprocal2(png_ptr->colorspace.gamma, png_ptr->screen_gamma > 0 ?
png_reciprocal2(png_ptr->colorspace.gamma,
png_ptr->screen_gamma) : PNG_FP_1); png_ptr->screen_gamma) : PNG_FP_1);
#if defined(PNG_READ_BACKGROUND_SUPPORTED) || \ #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
@ -4109,7 +4231,8 @@ png_build_gamma_table(png_structrp png_ptr, int bit_depth)
png_reciprocal(png_ptr->colorspace.gamma)); png_reciprocal(png_ptr->colorspace.gamma));
png_build_8bit_table(png_ptr, &png_ptr->gamma_from_1, png_build_8bit_table(png_ptr, &png_ptr->gamma_from_1,
png_ptr->screen_gamma > 0 ? png_reciprocal(png_ptr->screen_gamma) : png_ptr->screen_gamma > 0 ?
png_reciprocal(png_ptr->screen_gamma) :
png_ptr->colorspace.gamma/* Probably doing rgb_to_gray */); png_ptr->colorspace.gamma/* Probably doing rgb_to_gray */);
} }
#endif /* READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY */ #endif /* READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY */
@ -4140,8 +4263,8 @@ png_build_gamma_table(png_structrp png_ptr, int bit_depth)
* pow(iv, gamma). * pow(iv, gamma).
* *
* Thus the gamma table consists of up to 256 256-entry tables. The table * Thus the gamma table consists of up to 256 256-entry tables. The table
* is selected by the (8-gamma_shift) most significant of the low 8 bits of * is selected by the (8-gamma_shift) most significant of the low 8 bits
* the color value then indexed by the upper 8 bits: * of the color value then indexed by the upper 8 bits:
* *
* table[low bits][high 8 bits] * table[low bits][high 8 bits]
* *
@ -4174,8 +4297,8 @@ png_build_gamma_table(png_structrp png_ptr, int bit_depth)
/* NOTE: prior to 1.5.4 this test used to include PNG_BACKGROUND (now /* NOTE: prior to 1.5.4 this test used to include PNG_BACKGROUND (now
* PNG_COMPOSE). This effectively smashed the background calculation for * PNG_COMPOSE). This effectively smashed the background calculation for
* 16-bit output because the 8-bit table assumes the result will be reduced * 16-bit output because the 8-bit table assumes the result will be
* to 8 bits. * reduced to 8 bits.
*/ */
if ((png_ptr->transformations & (PNG_16_TO_8 | PNG_SCALE_16_TO_8)) != 0) if ((png_ptr->transformations & (PNG_16_TO_8 | PNG_SCALE_16_TO_8)) != 0)
png_build_16to8_table(png_ptr, &png_ptr->gamma_16_table, shift, png_build_16to8_table(png_ptr, &png_ptr->gamma_16_table, shift,
@ -4217,13 +4340,13 @@ png_set_option(png_structrp png_ptr, int option, int onoff)
if (png_ptr != NULL && option >= 0 && option < PNG_OPTION_NEXT && if (png_ptr != NULL && option >= 0 && option < PNG_OPTION_NEXT &&
(option & 1) == 0) (option & 1) == 0)
{ {
int mask = 3 << option; png_uint_32 mask = 3U << option;
int setting = (2 + (onoff != 0)) << option; png_uint_32 setting = (2U + (onoff != 0)) << option;
int current = png_ptr->options; png_uint_32 current = png_ptr->options;
png_ptr->options = (png_byte)(((current & ~mask) | setting) & 0xff); png_ptr->options = (png_uint_32)(((current & ~mask) | setting) & 0xff);
return (current & mask) >> option; return (int)(current & mask) >> option;
} }
return PNG_OPTION_INVALID; return PNG_OPTION_INVALID;
@ -4235,7 +4358,7 @@ png_set_option(png_structrp png_ptr, int option, int onoff)
defined(PNG_SIMPLIFIED_WRITE_SUPPORTED) defined(PNG_SIMPLIFIED_WRITE_SUPPORTED)
/* sRGB conversion tables; these are machine generated with the code in /* sRGB conversion tables; these are machine generated with the code in
* contrib/tools/makesRGB.c. The actual sRGB transfer curve defined in the * contrib/tools/makesRGB.c. The actual sRGB transfer curve defined in the
* specification (see the article at http://en.wikipedia.org/wiki/SRGB) * specification (see the article at https://en.wikipedia.org/wiki/SRGB)
* is used, not the gamma=1/2.2 approximation use elsewhere in libpng. * is used, not the gamma=1/2.2 approximation use elsewhere in libpng.
* The sRGB to linear table is exact (to the nearest 16-bit linear fraction). * The sRGB to linear table is exact (to the nearest 16-bit linear fraction).
* The inverse (linear to sRGB) table has accuracies as follows: * The inverse (linear to sRGB) table has accuracies as follows:

File diff suppressed because it is too large Load Diff

View File

@ -1,9 +1,9 @@
/* pngconf.h - machine configurable file for libpng /* pngconf.h - machine configurable file for libpng
* *
* libpng version 1.6.18, July 23, 2015 * libpng version 1.6.32, August 24, 2017
* *
* Copyright (c) 1998-2015 Glenn Randers-Pehrson * Copyright (c) 1998-2002,2004,2006-2016 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
* *
@ -63,7 +63,7 @@
*/ */
#define PNG_CONST const /* backward compatibility only */ #define PNG_CONST const /* backward compatibility only */
/* This controls optimization of the reading of 16 and 32 bit values /* This controls optimization of the reading of 16-bit and 32-bit values
* from PNG files. It can be set on a per-app-file basis - it * from PNG files. It can be set on a per-app-file basis - it
* just changes whether a macro is used when the function is called. * just changes whether a macro is used when the function is called.
* The library builder sets the default; if read functions are not * The library builder sets the default; if read functions are not
@ -480,7 +480,7 @@
#if CHAR_BIT == 8 && UCHAR_MAX == 255 #if CHAR_BIT == 8 && UCHAR_MAX == 255
typedef unsigned char png_byte; typedef unsigned char png_byte;
#else #else
# error "libpng requires 8 bit bytes" # error "libpng requires 8-bit bytes"
#endif #endif
#if INT_MIN == -32768 && INT_MAX == 32767 #if INT_MIN == -32768 && INT_MAX == 32767
@ -488,7 +488,7 @@
#elif SHRT_MIN == -32768 && SHRT_MAX == 32767 #elif SHRT_MIN == -32768 && SHRT_MAX == 32767
typedef short png_int_16; typedef short png_int_16;
#else #else
# error "libpng requires a signed 16 bit type" # error "libpng requires a signed 16-bit type"
#endif #endif
#if UINT_MAX == 65535 #if UINT_MAX == 65535
@ -496,7 +496,7 @@
#elif USHRT_MAX == 65535 #elif USHRT_MAX == 65535
typedef unsigned short png_uint_16; typedef unsigned short png_uint_16;
#else #else
# error "libpng requires an unsigned 16 bit type" # error "libpng requires an unsigned 16-bit type"
#endif #endif
#if INT_MIN < -2147483646 && INT_MAX > 2147483646 #if INT_MIN < -2147483646 && INT_MAX > 2147483646
@ -504,15 +504,15 @@
#elif LONG_MIN < -2147483646 && LONG_MAX > 2147483646 #elif LONG_MIN < -2147483646 && LONG_MAX > 2147483646
typedef long int png_int_32; typedef long int png_int_32;
#else #else
# error "libpng requires a signed 32 bit (or more) type" # error "libpng requires a signed 32-bit (or more) type"
#endif #endif
#if UINT_MAX > 4294967294 #if UINT_MAX > 4294967294U
typedef unsigned int png_uint_32; typedef unsigned int png_uint_32;
#elif ULONG_MAX > 4294967294 #elif ULONG_MAX > 4294967294U
typedef unsigned long int png_uint_32; typedef unsigned long int png_uint_32;
#else #else
# error "libpng requires an unsigned 32 bit (or more) type" # error "libpng requires an unsigned 32-bit (or more) type"
#endif #endif
/* Prior to 1.6.0 it was possible to disable the use of size_t, 1.6.0, however, /* Prior to 1.6.0 it was possible to disable the use of size_t, 1.6.0, however,

View File

@ -2,7 +2,7 @@
/* pngdebug.h - Debugging macros for libpng, also used in pngtest.c /* pngdebug.h - Debugging macros for libpng, also used in pngtest.c
* *
* Last changed in libpng 1.6.8 [December 19, 2013] * Last changed in libpng 1.6.8 [December 19, 2013]
* Copyright (c) 1998-2013 Glenn Randers-Pehrson * Copyright (c) 1998-2002,2004,2006-2013 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
* *

View File

@ -1,8 +1,8 @@
/* pngerror.c - stub functions for i/o and memory allocation /* pngerror.c - stub functions for i/o and memory allocation
* *
* Last changed in libpng 1.6.15 [November 20, 2014] * Last changed in libpng 1.6.31 [July 27, 2017]
* Copyright (c) 1998-2014 Glenn Randers-Pehrson * Copyright (c) 1998-2002,2004,2006-2017 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
* *
@ -44,7 +44,7 @@ png_error,(png_const_structrp png_ptr, png_const_charp error_message),
if (png_ptr != NULL) if (png_ptr != NULL)
{ {
if ((png_ptr->flags & if ((png_ptr->flags &
(PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT)) != 0 (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT)) != 0)
{ {
if (*error_message == PNG_LITERAL_SHARP) if (*error_message == PNG_LITERAL_SHARP)
{ {
@ -163,7 +163,7 @@ png_format_number(png_const_charp start, png_charp end, int format,
case PNG_NUMBER_FORMAT_02u: case PNG_NUMBER_FORMAT_02u:
/* Expects at least 2 digits. */ /* Expects at least 2 digits. */
mincount = 2; mincount = 2;
/* FALL THROUGH */ /* FALLTHROUGH */
case PNG_NUMBER_FORMAT_u: case PNG_NUMBER_FORMAT_u:
*--end = digits[number % 10]; *--end = digits[number % 10];
@ -173,7 +173,7 @@ png_format_number(png_const_charp start, png_charp end, int format,
case PNG_NUMBER_FORMAT_02x: case PNG_NUMBER_FORMAT_02x:
/* This format expects at least two digits */ /* This format expects at least two digits */
mincount = 2; mincount = 2;
/* FALL THROUGH */ /* FALLTHROUGH */
case PNG_NUMBER_FORMAT_x: case PNG_NUMBER_FORMAT_x:
*--end = digits[number & 0xf]; *--end = digits[number & 0xf];
@ -573,7 +573,7 @@ png_fixed_error,(png_const_structrp png_ptr, png_const_charp name),PNG_NORETURN)
{ {
# define fixed_message "fixed point overflow in " # define fixed_message "fixed point overflow in "
# define fixed_message_ln ((sizeof fixed_message)-1) # define fixed_message_ln ((sizeof fixed_message)-1)
int iin; unsigned int iin;
char msg[fixed_message_ln+PNG_MAX_ERROR_TEXT]; char msg[fixed_message_ln+PNG_MAX_ERROR_TEXT];
memcpy(msg, fixed_message, fixed_message_ln); memcpy(msg, fixed_message, fixed_message_ln);
iin = 0; iin = 0;

View File

@ -1,8 +1,8 @@
/* pngget.c - retrieval of values from info struct /* pngget.c - retrieval of values from info struct
* *
* Last changed in libpng 1.6.17 [March 26, 2015] * Last changed in libpng 1.6.32 [August 24, 2017]
* Copyright (c) 1998-2015 Glenn Randers-Pehrson * Copyright (c) 1998-2002,2004,2006-2017 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
* *
@ -338,7 +338,7 @@ ppi_from_ppm(png_uint_32 ppm)
png_fixed_point result; png_fixed_point result;
if (ppm <= PNG_UINT_31_MAX && png_muldiv(&result, (png_int_32)ppm, 127, if (ppm <= PNG_UINT_31_MAX && png_muldiv(&result, (png_int_32)ppm, 127,
5000) != 0) 5000) != 0)
return result; return (png_uint_32)result;
/* Overflow. */ /* Overflow. */
return 0; return 0;
@ -773,6 +773,35 @@ png_get_sPLT(png_const_structrp png_ptr, png_inforp info_ptr,
} }
#endif #endif
#ifdef PNG_eXIf_SUPPORTED
png_uint_32 PNGAPI
png_get_eXIf(png_const_structrp png_ptr, png_inforp info_ptr,
png_bytep *exif)
{
png_warning(png_ptr, "png_get_eXIf does not work; use png_get_eXIf_1");
PNG_UNUSED(info_ptr)
PNG_UNUSED(exif)
return 0;
}
png_uint_32 PNGAPI
png_get_eXIf_1(png_const_structrp png_ptr, png_const_inforp info_ptr,
png_uint_32 *num_exif, png_bytep *exif)
{
png_debug1(1, "in %s retrieval function", "eXIf");
if (png_ptr != NULL && info_ptr != NULL &&
(info_ptr->valid & PNG_INFO_eXIf) != 0 && exif != NULL)
{
*num_exif = info_ptr->num_exif;
*exif = info_ptr->exif;
return (PNG_INFO_eXIf);
}
return (0);
}
#endif
#ifdef PNG_hIST_SUPPORTED #ifdef PNG_hIST_SUPPORTED
png_uint_32 PNGAPI png_uint_32 PNGAPI
png_get_hIST(png_const_structrp png_ptr, png_inforp info_ptr, png_get_hIST(png_const_structrp png_ptr, png_inforp info_ptr,

View File

@ -2,7 +2,7 @@
/* pnginfo.h - header file for PNG reference library /* pnginfo.h - header file for PNG reference library
* *
* Last changed in libpng 1.6.1 [March 28, 2013] * Last changed in libpng 1.6.1 [March 28, 2013]
* Copyright (c) 1998-2013 Glenn Randers-Pehrson * Copyright (c) 1998-2002,2004,2006-2013 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
* *
@ -185,6 +185,14 @@ defined(PNG_READ_BACKGROUND_SUPPORTED)
png_byte phys_unit_type; /* resolution type (see PNG_RESOLUTION_ below) */ png_byte phys_unit_type; /* resolution type (see PNG_RESOLUTION_ below) */
#endif #endif
#ifdef PNG_eXIf_SUPPORTED
int num_exif; /* Added at libpng-1.6.31 */
png_bytep exif;
# ifdef PNG_READ_eXIf_SUPPORTED
png_bytep eXIf_buf; /* Added at libpng-1.6.32 */
# endif
#endif
#ifdef PNG_hIST_SUPPORTED #ifdef PNG_hIST_SUPPORTED
/* The hIST chunk contains the relative frequency or importance of the /* The hIST chunk contains the relative frequency or importance of the
* various palette entries, so that a viewer can intelligently select a * various palette entries, so that a viewer can intelligently select a

View File

@ -1,10 +1,8 @@
/* libpng 1.6.18 STANDARD API DEFINITION */
/* pnglibconf.h - library build configuration */ /* pnglibconf.h - library build configuration */
/* Libpng version 1.6.18 - July 23, 2015 */ /* libpng version 1.6.32, August 24, 2017 */
/* Copyright (c) 1998-2014 Glenn Randers-Pehrson */ /* Copyright (c) 1998-2017 Glenn Randers-Pehrson */
/* This code is released under the libpng license. */ /* This code is released under the libpng license. */
/* For conditions of distribution and use, see the disclaimer */ /* For conditions of distribution and use, see the disclaimer */
@ -44,6 +42,8 @@
#define PNG_IO_STATE_SUPPORTED #define PNG_IO_STATE_SUPPORTED
#define PNG_MNG_FEATURES_SUPPORTED #define PNG_MNG_FEATURES_SUPPORTED
#define PNG_POINTER_INDEXING_SUPPORTED #define PNG_POINTER_INDEXING_SUPPORTED
/*#undef PNG_POWERPC_VSX_API_SUPPORTED*/
/*#undef PNG_POWERPC_VSX_CHECK_SUPPORTED*/
#define PNG_PROGRESSIVE_READ_SUPPORTED #define PNG_PROGRESSIVE_READ_SUPPORTED
#define PNG_READ_16BIT_SUPPORTED #define PNG_READ_16BIT_SUPPORTED
#define PNG_READ_ALPHA_MODE_SUPPORTED #define PNG_READ_ALPHA_MODE_SUPPORTED
@ -82,6 +82,7 @@
#define PNG_READ_USER_TRANSFORM_SUPPORTED #define PNG_READ_USER_TRANSFORM_SUPPORTED
#define PNG_READ_bKGD_SUPPORTED #define PNG_READ_bKGD_SUPPORTED
#define PNG_READ_cHRM_SUPPORTED #define PNG_READ_cHRM_SUPPORTED
#define PNG_READ_eXIf_SUPPORTED
#define PNG_READ_gAMA_SUPPORTED #define PNG_READ_gAMA_SUPPORTED
#define PNG_READ_hIST_SUPPORTED #define PNG_READ_hIST_SUPPORTED
#define PNG_READ_iCCP_SUPPORTED #define PNG_READ_iCCP_SUPPORTED
@ -109,6 +110,7 @@
#define PNG_SIMPLIFIED_READ_SUPPORTED #define PNG_SIMPLIFIED_READ_SUPPORTED
#define PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED #define PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
#define PNG_SIMPLIFIED_WRITE_BGR_SUPPORTED #define PNG_SIMPLIFIED_WRITE_BGR_SUPPORTED
#define PNG_SIMPLIFIED_WRITE_STDIO_SUPPORTED
#define PNG_SIMPLIFIED_WRITE_SUPPORTED #define PNG_SIMPLIFIED_WRITE_SUPPORTED
#define PNG_STDIO_SUPPORTED #define PNG_STDIO_SUPPORTED
#define PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED #define PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
@ -150,6 +152,7 @@
#define PNG_WRITE_WEIGHTED_FILTER_SUPPORTED #define PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
#define PNG_WRITE_bKGD_SUPPORTED #define PNG_WRITE_bKGD_SUPPORTED
#define PNG_WRITE_cHRM_SUPPORTED #define PNG_WRITE_cHRM_SUPPORTED
#define PNG_WRITE_eXIf_SUPPORTED
#define PNG_WRITE_gAMA_SUPPORTED #define PNG_WRITE_gAMA_SUPPORTED
#define PNG_WRITE_hIST_SUPPORTED #define PNG_WRITE_hIST_SUPPORTED
#define PNG_WRITE_iCCP_SUPPORTED #define PNG_WRITE_iCCP_SUPPORTED
@ -167,6 +170,7 @@
#define PNG_WRITE_zTXt_SUPPORTED #define PNG_WRITE_zTXt_SUPPORTED
#define PNG_bKGD_SUPPORTED #define PNG_bKGD_SUPPORTED
#define PNG_cHRM_SUPPORTED #define PNG_cHRM_SUPPORTED
#define PNG_eXIf_SUPPORTED
#define PNG_gAMA_SUPPORTED #define PNG_gAMA_SUPPORTED
#define PNG_hIST_SUPPORTED #define PNG_hIST_SUPPORTED
#define PNG_iCCP_SUPPORTED #define PNG_iCCP_SUPPORTED
@ -185,7 +189,7 @@
/* end of options */ /* end of options */
/* settings */ /* settings */
#define PNG_API_RULE 0 #define PNG_API_RULE 0
#define PNG_COST_SHIFT 3 #define PNG_ARM_NEON_OPT 2
#define PNG_DEFAULT_READ_MACROS 1 #define PNG_DEFAULT_READ_MACROS 1
#define PNG_GAMMA_THRESHOLD_FIXED 5000 #define PNG_GAMMA_THRESHOLD_FIXED 5000
#define PNG_IDAT_READ_SIZE PNG_ZBUF_SIZE #define PNG_IDAT_READ_SIZE PNG_ZBUF_SIZE
@ -204,9 +208,8 @@
#define PNG_USER_CHUNK_MALLOC_MAX 8000000 #define PNG_USER_CHUNK_MALLOC_MAX 8000000
#define PNG_USER_HEIGHT_MAX 1000000 #define PNG_USER_HEIGHT_MAX 1000000
#define PNG_USER_WIDTH_MAX 1000000 #define PNG_USER_WIDTH_MAX 1000000
#define PNG_WEIGHT_SHIFT 8
#define PNG_ZBUF_SIZE 8192 #define PNG_ZBUF_SIZE 8192
#define PNG_ZLIB_VERNUM 0 /* unknown */ #define PNG_ZLIB_VERNUM 0x12b0
#define PNG_Z_DEFAULT_COMPRESSION (-1) #define PNG_Z_DEFAULT_COMPRESSION (-1)
#define PNG_Z_DEFAULT_NOFILTER_STRATEGY 0 #define PNG_Z_DEFAULT_NOFILTER_STRATEGY 0
#define PNG_Z_DEFAULT_STRATEGY 1 #define PNG_Z_DEFAULT_STRATEGY 1

View File

@ -1,8 +1,8 @@
/* pngmem.c - stub functions for memory allocation /* pngmem.c - stub functions for memory allocation
* *
* Last changed in libpng 1.6.15 [November 20, 2014] * Last changed in libpng 1.6.26 [October 20, 2016]
* Copyright (c) 1998-2014 Glenn Randers-Pehrson * Copyright (c) 1998-2002,2004,2006-2014,2016 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
* *
@ -109,7 +109,7 @@ static png_voidp
png_malloc_array_checked(png_const_structrp png_ptr, int nelements, png_malloc_array_checked(png_const_structrp png_ptr, int nelements,
size_t element_size) size_t element_size)
{ {
png_alloc_size_t req = nelements; /* known to be > 0 */ png_alloc_size_t req = (png_alloc_size_t)nelements; /* known to be > 0 */
if (req <= PNG_SIZE_MAX/element_size) if (req <= PNG_SIZE_MAX/element_size)
return png_malloc_base(png_ptr, req * element_size); return png_malloc_base(png_ptr, req * element_size);

View File

@ -1,8 +1,8 @@
/* pngpread.c - read a png file in push mode /* pngpread.c - read a png file in push mode
* *
* Last changed in libpng 1.6.18 [July 23, 2015] * Last changed in libpng 1.6.32 [August 24, 2017]
* Copyright (c) 1998-2015 Glenn Randers-Pehrson * Copyright (c) 1998-2002,2004,2006-2017 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
* *
@ -77,7 +77,7 @@ png_process_data_pause(png_structrp png_ptr, int save)
png_uint_32 PNGAPI png_uint_32 PNGAPI
png_process_data_skip(png_structrp png_ptr) png_process_data_skip(png_structrp png_ptr)
{ {
/* TODO: Deprecate and remove this API. /* TODO: Deprecate and remove this API.
* Somewhere the implementation of this seems to have been lost, * Somewhere the implementation of this seems to have been lost,
* or abandoned. It was only to support some internal back-door access * or abandoned. It was only to support some internal back-door access
* to png_struct) in libpng-1.4.x. * to png_struct) in libpng-1.4.x.
@ -189,6 +189,7 @@ png_push_read_chunk(png_structrp png_ptr, png_inforp info_ptr)
png_crc_read(png_ptr, chunk_tag, 4); png_crc_read(png_ptr, chunk_tag, 4);
png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(chunk_tag); png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(chunk_tag);
png_check_chunk_name(png_ptr, png_ptr->chunk_name); png_check_chunk_name(png_ptr, png_ptr->chunk_name);
png_check_chunk_length(png_ptr, png_ptr->push_length);
png_ptr->mode |= PNG_HAVE_CHUNK_HEADER; png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
} }
@ -210,13 +211,15 @@ png_push_read_chunk(png_structrp png_ptr, png_inforp info_ptr)
(png_ptr->mode & PNG_HAVE_PLTE) == 0) (png_ptr->mode & PNG_HAVE_PLTE) == 0)
png_error(png_ptr, "Missing PLTE before IDAT"); png_error(png_ptr, "Missing PLTE before IDAT");
png_ptr->mode |= PNG_HAVE_IDAT;
png_ptr->process_mode = PNG_READ_IDAT_MODE; png_ptr->process_mode = PNG_READ_IDAT_MODE;
if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
if ((png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) == 0) if ((png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) == 0)
if (png_ptr->push_length == 0) if (png_ptr->push_length == 0)
return; return;
png_ptr->mode |= PNG_HAVE_IDAT;
if ((png_ptr->mode & PNG_AFTER_IDAT) != 0) if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
png_benign_error(png_ptr, "Too many IDATs found"); png_benign_error(png_ptr, "Too many IDATs found");
} }
@ -499,7 +502,10 @@ png_push_save_buffer(png_structrp png_ptr)
png_error(png_ptr, "Insufficient memory for save_buffer"); png_error(png_ptr, "Insufficient memory for save_buffer");
} }
if (old_buffer)
memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size); memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
else if (png_ptr->save_buffer_size)
png_error(png_ptr, "save_buffer error");
png_free(png_ptr, old_buffer); png_free(png_ptr, old_buffer);
png_ptr->save_buffer_max = new_max; png_ptr->save_buffer_max = new_max;
} }
@ -563,7 +569,7 @@ png_push_read_IDAT(png_structrp png_ptr)
* are of different types and we don't know which variable has the fewest * are of different types and we don't know which variable has the fewest
* bits. Carefully select the smaller and cast it to the type of the * bits. Carefully select the smaller and cast it to the type of the
* larger - this cannot overflow. Do not cast in the following test - it * larger - this cannot overflow. Do not cast in the following test - it
* will break on either 16 or 64 bit platforms. * will break on either 16-bit or 64-bit platforms.
*/ */
if (idat_size < save_size) if (idat_size < save_size)
save_size = (png_size_t)idat_size; save_size = (png_size_t)idat_size;
@ -662,7 +668,7 @@ png_process_IDAT_data(png_structrp png_ptr, png_bytep buffer,
* change the current behavior (see comments in inflate.c * change the current behavior (see comments in inflate.c
* for why this doesn't happen at present with zlib 1.2.5). * for why this doesn't happen at present with zlib 1.2.5).
*/ */
ret = inflate(&png_ptr->zstream, Z_SYNC_FLUSH); ret = PNG_INFLATE(png_ptr, Z_SYNC_FLUSH);
/* Check for any failure before proceeding. */ /* Check for any failure before proceeding. */
if (ret != Z_OK && ret != Z_STREAM_END) if (ret != Z_OK && ret != Z_STREAM_END)
@ -678,8 +684,13 @@ png_process_IDAT_data(png_structrp png_ptr, png_bytep buffer,
png_ptr->pass > 6) png_ptr->pass > 6)
png_warning(png_ptr, "Truncated compressed data in IDAT"); png_warning(png_ptr, "Truncated compressed data in IDAT");
else
{
if (ret == Z_DATA_ERROR)
png_benign_error(png_ptr, "IDAT: ADLER32 checksum mismatch");
else else
png_error(png_ptr, "Decompression error in IDAT"); png_error(png_ptr, "Decompression error in IDAT");
}
/* Skip the check on unprocessed input */ /* Skip the check on unprocessed input */
return; return;

View File

@ -1,8 +1,8 @@
/* pngpriv.h - private declarations for use inside libpng /* pngpriv.h - private declarations for use inside libpng
* *
* Last changed in libpng 1.6.18 [July 23, 2015] * Last changed in libpng 1.6.32 [August 24, 2017]
* Copyright (c) 1998-2015 Glenn Randers-Pehrson * Copyright (c) 1998-2002,2004,2006-2017 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
* *
@ -35,7 +35,9 @@
* Windows/Visual Studio) there is no effect; the OS specific tests below are * Windows/Visual Studio) there is no effect; the OS specific tests below are
* still required (as of 2011-05-02.) * still required (as of 2011-05-02.)
*/ */
#define _POSIX_SOURCE 1 /* Just the POSIX 1003.1 and C89 APIs */ #ifndef _POSIX_SOURCE
# define _POSIX_SOURCE 1 /* Just the POSIX 1003.1 and C89 APIs */
#endif
#ifndef PNG_VERSION_INFO_ONLY #ifndef PNG_VERSION_INFO_ONLY
/* Standard library headers not required by png.h: */ /* Standard library headers not required by png.h: */
@ -182,6 +184,84 @@
# endif # endif
#endif /* PNG_ARM_NEON_OPT > 0 */ #endif /* PNG_ARM_NEON_OPT > 0 */
#ifndef PNG_MIPS_MSA_OPT
# if defined(__mips_msa) && (__mips_isa_rev >= 5) && defined(PNG_ALIGNED_MEMORY_SUPPORTED)
# define PNG_MIPS_MSA_OPT 2
# else
# define PNG_MIPS_MSA_OPT 0
# endif
#endif
#ifndef PNG_POWERPC_VSX_OPT
# if defined(__PPC64__) && defined(__ALTIVEC__) && defined(__VSX__)
# define PNG_POWERPC_VSX_OPT 2
# else
# define PNG_POWERPC_VSX_OPT 0
# endif
#endif
#ifndef PNG_INTEL_SSE_OPT
# ifdef PNG_INTEL_SSE
/* Only check for SSE if the build configuration has been modified to
* enable SSE optimizations. This means that these optimizations will
* be off by default. See contrib/intel for more details.
*/
# if defined(__SSE4_1__) || defined(__AVX__) || defined(__SSSE3__) || \
defined(__SSE2__) || defined(_M_X64) || defined(_M_AMD64) || \
(defined(_M_IX86_FP) && _M_IX86_FP >= 2)
# define PNG_INTEL_SSE_OPT 1
# endif
# endif
#endif
#if PNG_INTEL_SSE_OPT > 0
# ifndef PNG_INTEL_SSE_IMPLEMENTATION
# if defined(__SSE4_1__) || defined(__AVX__)
/* We are not actually using AVX, but checking for AVX is the best
way we can detect SSE4.1 and SSSE3 on MSVC.
*/
# define PNG_INTEL_SSE_IMPLEMENTATION 3
# elif defined(__SSSE3__)
# define PNG_INTEL_SSE_IMPLEMENTATION 2
# elif defined(__SSE2__) || defined(_M_X64) || defined(_M_AMD64) || \
(defined(_M_IX86_FP) && _M_IX86_FP >= 2)
# define PNG_INTEL_SSE_IMPLEMENTATION 1
# else
# define PNG_INTEL_SSE_IMPLEMENTATION 0
# endif
# endif
# if PNG_INTEL_SSE_IMPLEMENTATION > 0
# define PNG_FILTER_OPTIMIZATIONS png_init_filter_functions_sse2
# endif
#endif
#if PNG_MIPS_MSA_OPT > 0
# define PNG_FILTER_OPTIMIZATIONS png_init_filter_functions_msa
# ifndef PNG_MIPS_MSA_IMPLEMENTATION
# if defined(__mips_msa)
# if defined(__clang__)
# elif defined(__GNUC__)
# if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 7)
# define PNG_MIPS_MSA_IMPLEMENTATION 2
# endif /* no GNUC support */
# endif /* __GNUC__ */
# else /* !defined __mips_msa */
# define PNG_MIPS_MSA_IMPLEMENTATION 2
# endif /* __mips_msa */
# endif /* !PNG_MIPS_MSA_IMPLEMENTATION */
# ifndef PNG_MIPS_MSA_IMPLEMENTATION
# define PNG_MIPS_MSA_IMPLEMENTATION 1
# endif
#endif /* PNG_MIPS_MSA_OPT > 0 */
#if PNG_POWERPC_VSX_OPT > 0
# define PNG_FILTER_OPTIMIZATIONS png_init_filter_functions_vsx
# define PNG_POWERPC_VSX_IMPLEMENTATION 1
#endif
/* Is this a build of a DLL where compilation of the object modules requires /* Is this a build of a DLL where compilation of the object modules requires
* different preprocessor settings to those required for a simple library? If * different preprocessor settings to those required for a simple library? If
* so PNG_BUILD_DLL must be set. * so PNG_BUILD_DLL must be set.
@ -374,25 +454,6 @@
# define png_fixed_error(s1,s2) png_err(s1) # define png_fixed_error(s1,s2) png_err(s1)
#endif #endif
/* C allows up-casts from (void*) to any pointer and (const void*) to any
* pointer to a const object. C++ regards this as a type error and requires an
* explicit, static, cast and provides the static_cast<> rune to ensure that
* const is not cast away.
*/
#ifdef __cplusplus
# define png_voidcast(type, value) static_cast<type>(value)
# define png_constcast(type, value) const_cast<type>(value)
# define png_aligncast(type, value) \
static_cast<type>(static_cast<void*>(value))
# define png_aligncastconst(type, value) \
static_cast<type>(static_cast<const void*>(value))
#else
# define png_voidcast(type, value) (value)
# define png_constcast(type, value) ((type)(value))
# define png_aligncast(type, value) ((void*)(value))
# define png_aligncastconst(type, value) ((const void*)(value))
#endif /* __cplusplus */
/* Some fixed point APIs are still required even if not exported because /* Some fixed point APIs are still required even if not exported because
* they get used by the corresponding floating point APIs. This magic * they get used by the corresponding floating point APIs. This magic
* deals with this: * deals with this:
@ -407,6 +468,35 @@
/* Other defines specific to compilers can go here. Try to keep /* Other defines specific to compilers can go here. Try to keep
* them inside an appropriate ifdef/endif pair for portability. * them inside an appropriate ifdef/endif pair for portability.
*/ */
/* C allows up-casts from (void*) to any pointer and (const void*) to any
* pointer to a const object. C++ regards this as a type error and requires an
* explicit, static, cast and provides the static_cast<> rune to ensure that
* const is not cast away.
*/
#ifdef __cplusplus
# define png_voidcast(type, value) static_cast<type>(value)
# define png_constcast(type, value) const_cast<type>(value)
# define png_aligncast(type, value) \
static_cast<type>(static_cast<void*>(value))
# define png_aligncastconst(type, value) \
static_cast<type>(static_cast<const void*>(value))
#else
# define png_voidcast(type, value) (value)
# ifdef _WIN64
# ifdef __GNUC__
typedef unsigned long long png_ptruint;
# else
typedef unsigned __int64 png_ptruint;
# endif
# else
typedef unsigned long png_ptruint;
# endif
# define png_constcast(type, value) ((type)(png_ptruint)(const void*)(value))
# define png_aligncast(type, value) ((void*)(value))
# define png_aligncastconst(type, value) ((const void*)(value))
#endif /* __cplusplus */
#if defined(PNG_FLOATING_POINT_SUPPORTED) ||\ #if defined(PNG_FLOATING_POINT_SUPPORTED) ||\
defined(PNG_FLOATING_ARITHMETIC_SUPPORTED) defined(PNG_FLOATING_ARITHMETIC_SUPPORTED)
/* png.c requires the following ANSI-C constants if the conversion of /* png.c requires the following ANSI-C constants if the conversion of
@ -504,7 +594,8 @@
/* This implicitly assumes alignment is always to a power of 2. */ /* This implicitly assumes alignment is always to a power of 2. */
#ifdef png_alignof #ifdef png_alignof
# define png_isaligned(ptr, type)\ # define png_isaligned(ptr, type)\
((((const char*)ptr-(const char*)0) & (png_alignof(type)-1)) == 0) (((type)((const char*)ptr-(const char*)0) & \
(type)(png_alignof(type)-1)) == 0)
#else #else
# define png_isaligned(ptr, type) 0 # define png_isaligned(ptr, type) 0
#endif #endif
@ -521,96 +612,92 @@
* are defined in png.h because they need to be visible to applications * are defined in png.h because they need to be visible to applications
* that call png_set_unknown_chunk(). * that call png_set_unknown_chunk().
*/ */
/* #define PNG_HAVE_IHDR 0x01 (defined in png.h) */ /* #define PNG_HAVE_IHDR 0x01U (defined in png.h) */
/* #define PNG_HAVE_PLTE 0x02 (defined in png.h) */ /* #define PNG_HAVE_PLTE 0x02U (defined in png.h) */
#define PNG_HAVE_IDAT 0x04 #define PNG_HAVE_IDAT 0x04U
/* #define PNG_AFTER_IDAT 0x08 (defined in png.h) */ /* #define PNG_AFTER_IDAT 0x08U (defined in png.h) */
#define PNG_HAVE_IEND 0x10 #define PNG_HAVE_IEND 0x10U
/* 0x20 (unused) */ /* 0x20U (unused) */
/* 0x40 (unused) */ /* 0x40U (unused) */
/* 0x80 (unused) */ /* 0x80U (unused) */
#define PNG_HAVE_CHUNK_HEADER 0x100 #define PNG_HAVE_CHUNK_HEADER 0x100U
#define PNG_WROTE_tIME 0x200 #define PNG_WROTE_tIME 0x200U
#define PNG_WROTE_INFO_BEFORE_PLTE 0x400 #define PNG_WROTE_INFO_BEFORE_PLTE 0x400U
#define PNG_BACKGROUND_IS_GRAY 0x800 #define PNG_BACKGROUND_IS_GRAY 0x800U
#define PNG_HAVE_PNG_SIGNATURE 0x1000 #define PNG_HAVE_PNG_SIGNATURE 0x1000U
#define PNG_HAVE_CHUNK_AFTER_IDAT 0x2000 /* Have another chunk after IDAT */ #define PNG_HAVE_CHUNK_AFTER_IDAT 0x2000U /* Have another chunk after IDAT */
/* 0x4000 (unused) */ /* 0x4000U (unused) */
#define PNG_IS_READ_STRUCT 0x8000 /* Else is a write struct */ #define PNG_IS_READ_STRUCT 0x8000U /* Else is a write struct */
/* Flags for the transformations the PNG library does on the image data */ /* Flags for the transformations the PNG library does on the image data */
#define PNG_BGR 0x0001 #define PNG_BGR 0x0001U
#define PNG_INTERLACE 0x0002 #define PNG_INTERLACE 0x0002U
#define PNG_PACK 0x0004 #define PNG_PACK 0x0004U
#define PNG_SHIFT 0x0008 #define PNG_SHIFT 0x0008U
#define PNG_SWAP_BYTES 0x0010 #define PNG_SWAP_BYTES 0x0010U
#define PNG_INVERT_MONO 0x0020 #define PNG_INVERT_MONO 0x0020U
#define PNG_QUANTIZE 0x0040 #define PNG_QUANTIZE 0x0040U
#define PNG_COMPOSE 0x0080 /* Was PNG_BACKGROUND */ #define PNG_COMPOSE 0x0080U /* Was PNG_BACKGROUND */
#define PNG_BACKGROUND_EXPAND 0x0100 #define PNG_BACKGROUND_EXPAND 0x0100U
#define PNG_EXPAND_16 0x0200 /* Added to libpng 1.5.2 */ #define PNG_EXPAND_16 0x0200U /* Added to libpng 1.5.2 */
#define PNG_16_TO_8 0x0400 /* Becomes 'chop' in 1.5.4 */ #define PNG_16_TO_8 0x0400U /* Becomes 'chop' in 1.5.4 */
#define PNG_RGBA 0x0800 #define PNG_RGBA 0x0800U
#define PNG_EXPAND 0x1000 #define PNG_EXPAND 0x1000U
#define PNG_GAMMA 0x2000 #define PNG_GAMMA 0x2000U
#define PNG_GRAY_TO_RGB 0x4000 #define PNG_GRAY_TO_RGB 0x4000U
#define PNG_FILLER 0x8000 #define PNG_FILLER 0x8000U
#define PNG_PACKSWAP 0x10000 #define PNG_PACKSWAP 0x10000U
#define PNG_SWAP_ALPHA 0x20000 #define PNG_SWAP_ALPHA 0x20000U
#define PNG_STRIP_ALPHA 0x40000 #define PNG_STRIP_ALPHA 0x40000U
#define PNG_INVERT_ALPHA 0x80000 #define PNG_INVERT_ALPHA 0x80000U
#define PNG_USER_TRANSFORM 0x100000 #define PNG_USER_TRANSFORM 0x100000U
#define PNG_RGB_TO_GRAY_ERR 0x200000 #define PNG_RGB_TO_GRAY_ERR 0x200000U
#define PNG_RGB_TO_GRAY_WARN 0x400000 #define PNG_RGB_TO_GRAY_WARN 0x400000U
#define PNG_RGB_TO_GRAY 0x600000 /* two bits, RGB_TO_GRAY_ERR|WARN */ #define PNG_RGB_TO_GRAY 0x600000U /* two bits, RGB_TO_GRAY_ERR|WARN */
#define PNG_ENCODE_ALPHA 0x800000 /* Added to libpng-1.5.4 */ #define PNG_ENCODE_ALPHA 0x800000U /* Added to libpng-1.5.4 */
#define PNG_ADD_ALPHA 0x1000000 /* Added to libpng-1.2.7 */ #define PNG_ADD_ALPHA 0x1000000U /* Added to libpng-1.2.7 */
#define PNG_EXPAND_tRNS 0x2000000 /* Added to libpng-1.2.9 */ #define PNG_EXPAND_tRNS 0x2000000U /* Added to libpng-1.2.9 */
#define PNG_SCALE_16_TO_8 0x4000000 /* Added to libpng-1.5.4 */ #define PNG_SCALE_16_TO_8 0x4000000U /* Added to libpng-1.5.4 */
/* 0x8000000 unused */ /* 0x8000000U unused */
/* 0x10000000 unused */ /* 0x10000000U unused */
/* 0x20000000 unused */ /* 0x20000000U unused */
/* 0x40000000 unused */ /* 0x40000000U unused */
/* Flags for png_create_struct */ /* Flags for png_create_struct */
#define PNG_STRUCT_PNG 0x0001 #define PNG_STRUCT_PNG 0x0001U
#define PNG_STRUCT_INFO 0x0002 #define PNG_STRUCT_INFO 0x0002U
/* Scaling factor for filter heuristic weighting calculations */
#define PNG_WEIGHT_FACTOR (1<<(PNG_WEIGHT_SHIFT))
#define PNG_COST_FACTOR (1<<(PNG_COST_SHIFT))
/* Flags for the png_ptr->flags rather than declaring a byte for each one */ /* Flags for the png_ptr->flags rather than declaring a byte for each one */
#define PNG_FLAG_ZLIB_CUSTOM_STRATEGY 0x0001 #define PNG_FLAG_ZLIB_CUSTOM_STRATEGY 0x0001U
#define PNG_FLAG_ZSTREAM_INITIALIZED 0x0002 /* Added to libpng-1.6.0 */ #define PNG_FLAG_ZSTREAM_INITIALIZED 0x0002U /* Added to libpng-1.6.0 */
/* 0x0004 unused */ /* 0x0004U unused */
#define PNG_FLAG_ZSTREAM_ENDED 0x0008 /* Added to libpng-1.6.0 */ #define PNG_FLAG_ZSTREAM_ENDED 0x0008U /* Added to libpng-1.6.0 */
/* 0x0010 unused */ /* 0x0010U unused */
/* 0x0020 unused */ /* 0x0020U unused */
#define PNG_FLAG_ROW_INIT 0x0040 #define PNG_FLAG_ROW_INIT 0x0040U
#define PNG_FLAG_FILLER_AFTER 0x0080 #define PNG_FLAG_FILLER_AFTER 0x0080U
#define PNG_FLAG_CRC_ANCILLARY_USE 0x0100 #define PNG_FLAG_CRC_ANCILLARY_USE 0x0100U
#define PNG_FLAG_CRC_ANCILLARY_NOWARN 0x0200 #define PNG_FLAG_CRC_ANCILLARY_NOWARN 0x0200U
#define PNG_FLAG_CRC_CRITICAL_USE 0x0400 #define PNG_FLAG_CRC_CRITICAL_USE 0x0400U
#define PNG_FLAG_CRC_CRITICAL_IGNORE 0x0800 #define PNG_FLAG_CRC_CRITICAL_IGNORE 0x0800U
#define PNG_FLAG_ASSUME_sRGB 0x1000 /* Added to libpng-1.5.4 */ #define PNG_FLAG_ASSUME_sRGB 0x1000U /* Added to libpng-1.5.4 */
#define PNG_FLAG_OPTIMIZE_ALPHA 0x2000 /* Added to libpng-1.5.4 */ #define PNG_FLAG_OPTIMIZE_ALPHA 0x2000U /* Added to libpng-1.5.4 */
#define PNG_FLAG_DETECT_UNINITIALIZED 0x4000 /* Added to libpng-1.5.4 */ #define PNG_FLAG_DETECT_UNINITIALIZED 0x4000U /* Added to libpng-1.5.4 */
/* #define PNG_FLAG_KEEP_UNKNOWN_CHUNKS 0x8000 */ /* #define PNG_FLAG_KEEP_UNKNOWN_CHUNKS 0x8000U */
/* #define PNG_FLAG_KEEP_UNSAFE_CHUNKS 0x10000 */ /* #define PNG_FLAG_KEEP_UNSAFE_CHUNKS 0x10000U */
#define PNG_FLAG_LIBRARY_MISMATCH 0x20000 #define PNG_FLAG_LIBRARY_MISMATCH 0x20000U
#define PNG_FLAG_STRIP_ERROR_NUMBERS 0x40000 #define PNG_FLAG_STRIP_ERROR_NUMBERS 0x40000U
#define PNG_FLAG_STRIP_ERROR_TEXT 0x80000 #define PNG_FLAG_STRIP_ERROR_TEXT 0x80000U
#define PNG_FLAG_BENIGN_ERRORS_WARN 0x100000 /* Added to libpng-1.4.0 */ #define PNG_FLAG_BENIGN_ERRORS_WARN 0x100000U /* Added to libpng-1.4.0 */
#define PNG_FLAG_APP_WARNINGS_WARN 0x200000 /* Added to libpng-1.6.0 */ #define PNG_FLAG_APP_WARNINGS_WARN 0x200000U /* Added to libpng-1.6.0 */
#define PNG_FLAG_APP_ERRORS_WARN 0x400000 /* Added to libpng-1.6.0 */ #define PNG_FLAG_APP_ERRORS_WARN 0x400000U /* Added to libpng-1.6.0 */
/* 0x800000 unused */ /* 0x800000U unused */
/* 0x1000000 unused */ /* 0x1000000U unused */
/* 0x2000000 unused */ /* 0x2000000U unused */
/* 0x4000000 unused */ /* 0x4000000U unused */
/* 0x8000000 unused */ /* 0x8000000U unused */
/* 0x10000000 unused */ /* 0x10000000U unused */
/* 0x20000000 unused */ /* 0x20000000U unused */
/* 0x40000000 unused */ /* 0x40000000U unused */
#define PNG_FLAG_CRC_ANCILLARY_MASK (PNG_FLAG_CRC_ANCILLARY_USE | \ #define PNG_FLAG_CRC_ANCILLARY_MASK (PNG_FLAG_CRC_ANCILLARY_USE | \
PNG_FLAG_CRC_ANCILLARY_NOWARN) PNG_FLAG_CRC_ANCILLARY_NOWARN)
@ -644,6 +731,24 @@
((png_size_t)(width) * (((png_size_t)(pixel_bits)) >> 3)) : \ ((png_size_t)(width) * (((png_size_t)(pixel_bits)) >> 3)) : \
(( ((png_size_t)(width) * ((png_size_t)(pixel_bits))) + 7) >> 3) ) (( ((png_size_t)(width) * ((png_size_t)(pixel_bits))) + 7) >> 3) )
/* This returns the number of trailing bits in the last byte of a row, 0 if the
* last byte is completely full of pixels. It is, in principle, (pixel_bits x
* width) % 8, but that would overflow for large 'width'. The second macro is
* the same except that it returns the number of unused bits in the last byte;
* (8-TRAILBITS), but 0 when TRAILBITS is 0.
*
* NOTE: these macros are intended to be self-evidently correct and never
* overflow on the assumption that pixel_bits is in the range 0..255. The
* arguments are evaluated only once and they can be signed (e.g. as a result of
* the integral promotions). The result of the expression always has type
* (png_uint_32), however the compiler always knows it is in the range 0..7.
*/
#define PNG_TRAILBITS(pixel_bits, width) \
(((pixel_bits) * ((width) % (png_uint_32)8)) % 8)
#define PNG_PADBITS(pixel_bits, width) \
((8 - PNG_TRAILBITS(pixel_bits, width)) % 8)
/* PNG_OUT_OF_RANGE returns true if value is outside the range /* PNG_OUT_OF_RANGE returns true if value is outside the range
* ideal-delta..ideal+delta. Each argument is evaluated twice. * ideal-delta..ideal+delta. Each argument is evaluated twice.
* "ideal" and "delta" should be constants, normally simple * "ideal" and "delta" should be constants, normally simple
@ -669,7 +774,7 @@
/* The fixed point conversion performs range checking and evaluates /* The fixed point conversion performs range checking and evaluates
* its argument multiple times, so must be used with care. The * its argument multiple times, so must be used with care. The
* range checking uses the PNG specification values for a signed * range checking uses the PNG specification values for a signed
* 32 bit fixed point value except that the values are deliberately * 32-bit fixed point value except that the values are deliberately
* rounded-to-zero to an integral value - 21474 (21474.83 is roughly * rounded-to-zero to an integral value - 21474 (21474.83 is roughly
* (2^31-1) * 100000). 's' is a string that describes the value being * (2^31-1) * 100000). 's' is a string that describes the value being
* converted. * converted.
@ -737,6 +842,7 @@
#define png_PLTE PNG_U32( 80, 76, 84, 69) #define png_PLTE PNG_U32( 80, 76, 84, 69)
#define png_bKGD PNG_U32( 98, 75, 71, 68) #define png_bKGD PNG_U32( 98, 75, 71, 68)
#define png_cHRM PNG_U32( 99, 72, 82, 77) #define png_cHRM PNG_U32( 99, 72, 82, 77)
#define png_eXIf PNG_U32(101, 88, 73, 102) /* registered July 2017 */
#define png_fRAc PNG_U32(102, 82, 65, 99) /* registered, not defined */ #define png_fRAc PNG_U32(102, 82, 65, 99) /* registered, not defined */
#define png_gAMA PNG_U32(103, 65, 77, 65) #define png_gAMA PNG_U32(103, 65, 77, 65)
#define png_gIFg PNG_U32(103, 73, 70, 103) #define png_gIFg PNG_U32(103, 73, 70, 103)
@ -816,7 +922,7 @@
*/ */
#endif #endif
/* This is used for 16 bit gamma tables -- only the top level pointers are /* This is used for 16-bit gamma tables -- only the top level pointers are
* const; this could be changed: * const; this could be changed:
*/ */
typedef const png_uint_16p * png_const_uint_16pp; typedef const png_uint_16p * png_const_uint_16pp;
@ -1037,6 +1143,11 @@ PNG_INTERNAL_FUNCTION(void,png_write_sRGB,(png_structrp png_ptr,
int intent),PNG_EMPTY); int intent),PNG_EMPTY);
#endif #endif
#ifdef PNG_WRITE_eXIf_SUPPORTED
PNG_INTERNAL_FUNCTION(void,png_write_eXIf,(png_structrp png_ptr,
png_bytep exif, int num_exif),PNG_EMPTY);
#endif
#ifdef PNG_WRITE_iCCP_SUPPORTED #ifdef PNG_WRITE_iCCP_SUPPORTED
PNG_INTERNAL_FUNCTION(void,png_write_iCCP,(png_structrp png_ptr, PNG_INTERNAL_FUNCTION(void,png_write_iCCP,(png_structrp png_ptr,
png_const_charp name, png_const_bytep profile), PNG_EMPTY); png_const_charp name, png_const_bytep profile), PNG_EMPTY);
@ -1178,6 +1289,7 @@ PNG_INTERNAL_FUNCTION(void,png_do_write_interlace,(png_row_infop row_info,
PNG_INTERNAL_FUNCTION(void,png_read_filter_row,(png_structrp pp, png_row_infop PNG_INTERNAL_FUNCTION(void,png_read_filter_row,(png_structrp pp, png_row_infop
row_info, png_bytep row, png_const_bytep prev_row, int filter),PNG_EMPTY); row_info, png_bytep row, png_const_bytep prev_row, int filter),PNG_EMPTY);
#if PNG_ARM_NEON_OPT > 0
PNG_INTERNAL_FUNCTION(void,png_read_filter_row_up_neon,(png_row_infop row_info, PNG_INTERNAL_FUNCTION(void,png_read_filter_row_up_neon,(png_row_infop row_info,
png_bytep row, png_const_bytep prev_row),PNG_EMPTY); png_bytep row, png_const_bytep prev_row),PNG_EMPTY);
PNG_INTERNAL_FUNCTION(void,png_read_filter_row_sub3_neon,(png_row_infop PNG_INTERNAL_FUNCTION(void,png_read_filter_row_sub3_neon,(png_row_infop
@ -1192,6 +1304,56 @@ PNG_INTERNAL_FUNCTION(void,png_read_filter_row_paeth3_neon,(png_row_infop
row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY); row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY);
PNG_INTERNAL_FUNCTION(void,png_read_filter_row_paeth4_neon,(png_row_infop PNG_INTERNAL_FUNCTION(void,png_read_filter_row_paeth4_neon,(png_row_infop
row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY); row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY);
#endif
#if PNG_MIPS_MSA_OPT > 0
PNG_INTERNAL_FUNCTION(void,png_read_filter_row_up_msa,(png_row_infop row_info,
png_bytep row, png_const_bytep prev_row),PNG_EMPTY);
PNG_INTERNAL_FUNCTION(void,png_read_filter_row_sub3_msa,(png_row_infop
row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY);
PNG_INTERNAL_FUNCTION(void,png_read_filter_row_sub4_msa,(png_row_infop
row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY);
PNG_INTERNAL_FUNCTION(void,png_read_filter_row_avg3_msa,(png_row_infop
row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY);
PNG_INTERNAL_FUNCTION(void,png_read_filter_row_avg4_msa,(png_row_infop
row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY);
PNG_INTERNAL_FUNCTION(void,png_read_filter_row_paeth3_msa,(png_row_infop
row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY);
PNG_INTERNAL_FUNCTION(void,png_read_filter_row_paeth4_msa,(png_row_infop
row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY);
#endif
#if PNG_POWERPC_VSX_OPT > 0
PNG_INTERNAL_FUNCTION(void,png_read_filter_row_up_vsx,(png_row_infop row_info,
png_bytep row, png_const_bytep prev_row),PNG_EMPTY);
PNG_INTERNAL_FUNCTION(void,png_read_filter_row_sub3_vsx,(png_row_infop
row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY);
PNG_INTERNAL_FUNCTION(void,png_read_filter_row_sub4_vsx,(png_row_infop
row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY);
PNG_INTERNAL_FUNCTION(void,png_read_filter_row_avg3_vsx,(png_row_infop
row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY);
PNG_INTERNAL_FUNCTION(void,png_read_filter_row_avg4_vsx,(png_row_infop
row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY);
PNG_INTERNAL_FUNCTION(void,png_read_filter_row_paeth3_vsx,(png_row_infop
row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY);
PNG_INTERNAL_FUNCTION(void,png_read_filter_row_paeth4_vsx,(png_row_infop
row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY);
#endif
#if PNG_INTEL_SSE_IMPLEMENTATION > 0
PNG_INTERNAL_FUNCTION(void,png_read_filter_row_sub3_sse2,(png_row_infop
row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY);
PNG_INTERNAL_FUNCTION(void,png_read_filter_row_sub4_sse2,(png_row_infop
row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY);
PNG_INTERNAL_FUNCTION(void,png_read_filter_row_avg3_sse2,(png_row_infop
row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY);
PNG_INTERNAL_FUNCTION(void,png_read_filter_row_avg4_sse2,(png_row_infop
row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY);
PNG_INTERNAL_FUNCTION(void,png_read_filter_row_paeth3_sse2,(png_row_infop
row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY);
PNG_INTERNAL_FUNCTION(void,png_read_filter_row_paeth4_sse2,(png_row_infop
row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY);
#endif
/* Choose the best filter to use and filter the row data */ /* Choose the best filter to use and filter the row data */
PNG_INTERNAL_FUNCTION(void,png_write_find_filter,(png_structrp png_ptr, PNG_INTERNAL_FUNCTION(void,png_write_find_filter,(png_structrp png_ptr,
@ -1219,6 +1381,14 @@ PNG_INTERNAL_FUNCTION(void,png_read_finish_row,(png_structrp png_ptr),
/* Initialize the row buffers, etc. */ /* Initialize the row buffers, etc. */
PNG_INTERNAL_FUNCTION(void,png_read_start_row,(png_structrp png_ptr),PNG_EMPTY); PNG_INTERNAL_FUNCTION(void,png_read_start_row,(png_structrp png_ptr),PNG_EMPTY);
#if ZLIB_VERNUM >= 0x1240
PNG_INTERNAL_FUNCTION(int,png_zlib_inflate,(png_structrp png_ptr, int flush),
PNG_EMPTY);
# define PNG_INFLATE(pp, flush) png_zlib_inflate(pp, flush)
#else /* Zlib < 1.2.4 */
# define PNG_INFLATE(pp, flush) inflate(&(pp)->zstream, flush)
#endif /* Zlib < 1.2.4 */
#ifdef PNG_READ_TRANSFORMS_SUPPORTED #ifdef PNG_READ_TRANSFORMS_SUPPORTED
/* Optional call to update the users info structure */ /* Optional call to update the users info structure */
PNG_INTERNAL_FUNCTION(void,png_read_transform_info,(png_structrp png_ptr, PNG_INTERNAL_FUNCTION(void,png_read_transform_info,(png_structrp png_ptr,
@ -1277,6 +1447,11 @@ PNG_INTERNAL_FUNCTION(void,png_handle_cHRM,(png_structrp png_ptr,
png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); png_inforp info_ptr, png_uint_32 length),PNG_EMPTY);
#endif #endif
#ifdef PNG_READ_eXIf_SUPPORTED
PNG_INTERNAL_FUNCTION(void,png_handle_eXIf,(png_structrp png_ptr,
png_inforp info_ptr, png_uint_32 length),PNG_EMPTY);
#endif
#ifdef PNG_READ_gAMA_SUPPORTED #ifdef PNG_READ_gAMA_SUPPORTED
PNG_INTERNAL_FUNCTION(void,png_handle_gAMA,(png_structrp png_ptr, PNG_INTERNAL_FUNCTION(void,png_handle_gAMA,(png_structrp png_ptr,
png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); png_inforp info_ptr, png_uint_32 length),PNG_EMPTY);
@ -1352,8 +1527,11 @@ PNG_INTERNAL_FUNCTION(void,png_handle_zTXt,(png_structrp png_ptr,
png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); png_inforp info_ptr, png_uint_32 length),PNG_EMPTY);
#endif #endif
PNG_INTERNAL_FUNCTION(void,png_check_chunk_name,(png_structrp png_ptr, PNG_INTERNAL_FUNCTION(void,png_check_chunk_name,(png_const_structrp png_ptr,
png_uint_32 chunk_name),PNG_EMPTY); const png_uint_32 chunk_name),PNG_EMPTY);
PNG_INTERNAL_FUNCTION(void,png_check_chunk_length,(png_const_structrp png_ptr,
const png_uint_32 chunk_length),PNG_EMPTY);
PNG_INTERNAL_FUNCTION(void,png_handle_unknown,(png_structrp png_ptr, PNG_INTERNAL_FUNCTION(void,png_handle_unknown,(png_structrp png_ptr,
png_inforp info_ptr, png_uint_32 length, int keep),PNG_EMPTY); png_inforp info_ptr, png_uint_32 length, int keep),PNG_EMPTY);
@ -1488,9 +1666,11 @@ PNG_INTERNAL_FUNCTION(int,png_colorspace_set_ICC,(png_const_structrp png_ptr,
/* The 'name' is used for information only */ /* The 'name' is used for information only */
/* Routines for checking parts of an ICC profile. */ /* Routines for checking parts of an ICC profile. */
#ifdef PNG_READ_iCCP_SUPPORTED
PNG_INTERNAL_FUNCTION(int,png_icc_check_length,(png_const_structrp png_ptr, PNG_INTERNAL_FUNCTION(int,png_icc_check_length,(png_const_structrp png_ptr,
png_colorspacerp colorspace, png_const_charp name, png_colorspacerp colorspace, png_const_charp name,
png_uint_32 profile_length), PNG_EMPTY); png_uint_32 profile_length), PNG_EMPTY);
#endif /* READ_iCCP */
PNG_INTERNAL_FUNCTION(int,png_icc_check_header,(png_const_structrp png_ptr, PNG_INTERNAL_FUNCTION(int,png_icc_check_header,(png_const_structrp png_ptr,
png_colorspacerp colorspace, png_const_charp name, png_colorspacerp colorspace, png_const_charp name,
png_uint_32 profile_length, png_uint_32 profile_length,
@ -1909,10 +2089,25 @@ PNG_INTERNAL_FUNCTION(void, PNG_FILTER_OPTIMIZATIONS, (png_structp png_ptr,
* the builder of libpng passes the definition of PNG_FILTER_OPTIMIZATIONS in * the builder of libpng passes the definition of PNG_FILTER_OPTIMIZATIONS in
* CFLAGS in place of CPPFLAGS *and* uses symbol prefixing. * CFLAGS in place of CPPFLAGS *and* uses symbol prefixing.
*/ */
# if PNG_ARM_NEON_OPT > 0
PNG_INTERNAL_FUNCTION(void, png_init_filter_functions_neon, PNG_INTERNAL_FUNCTION(void, png_init_filter_functions_neon,
(png_structp png_ptr, unsigned int bpp), PNG_EMPTY); (png_structp png_ptr, unsigned int bpp), PNG_EMPTY);
#endif #endif
#if PNG_MIPS_MSA_OPT > 0
PNG_INTERNAL_FUNCTION(void, png_init_filter_functions_msa,
(png_structp png_ptr, unsigned int bpp), PNG_EMPTY);
#endif
# if PNG_INTEL_SSE_IMPLEMENTATION > 0
PNG_INTERNAL_FUNCTION(void, png_init_filter_functions_sse2,
(png_structp png_ptr, unsigned int bpp), PNG_EMPTY);
# endif
#endif
PNG_INTERNAL_FUNCTION(png_uint_32, png_check_keyword, (png_structrp png_ptr,
png_const_charp key, png_bytep new_key), PNG_EMPTY);
/* Maintainer: Put new private prototypes here ^ */ /* Maintainer: Put new private prototypes here ^ */
#include "pngdebug.h" #include "pngdebug.h"

View File

@ -1,8 +1,8 @@
/* pngread.c - read a PNG file /* pngread.c - read a PNG file
* *
* Last changed in libpng 1.6.17 [March 26, 2015] * Last changed in libpng 1.6.32 [August 24, 2017]
* Copyright (c) 1998-2015 Glenn Randers-Pehrson * Copyright (c) 1998-2002,2004,2006-2017 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
* *
@ -127,7 +127,10 @@ png_read_info(png_structrp png_ptr, png_inforp info_ptr)
} }
else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0) else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
{
png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
png_ptr->mode |= PNG_AFTER_IDAT; png_ptr->mode |= PNG_AFTER_IDAT;
}
/* This should be a binary subdivision search or a hash for /* This should be a binary subdivision search or a hash for
* matching the chunk name rather than a linear search. * matching the chunk name rather than a linear search.
@ -172,6 +175,11 @@ png_read_info(png_structrp png_ptr, png_inforp info_ptr)
png_handle_cHRM(png_ptr, info_ptr, length); png_handle_cHRM(png_ptr, info_ptr, length);
#endif #endif
#ifdef PNG_READ_eXIf_SUPPORTED
else if (chunk_name == png_eXIf)
png_handle_eXIf(png_ptr, info_ptr, length);
#endif
#ifdef PNG_READ_gAMA_SUPPORTED #ifdef PNG_READ_gAMA_SUPPORTED
else if (chunk_name == png_gAMA) else if (chunk_name == png_gAMA)
png_handle_gAMA(png_ptr, info_ptr, length); png_handle_gAMA(png_ptr, info_ptr, length);
@ -356,9 +364,9 @@ png_do_read_intrapixel(png_row_infop row_info, png_bytep row)
for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
{ {
png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1); png_uint_32 s0 = (png_uint_32)(*(rp ) << 8) | *(rp + 1);
png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3); png_uint_32 s1 = (png_uint_32)(*(rp + 2) << 8) | *(rp + 3);
png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5); png_uint_32 s2 = (png_uint_32)(*(rp + 4) << 8) | *(rp + 5);
png_uint_32 red = (s0 + s1 + 65536) & 0xffff; png_uint_32 red = (s0 + s1 + 65536) & 0xffff;
png_uint_32 blue = (s2 + s1 + 65536) & 0xffff; png_uint_32 blue = (s2 + s1 + 65536) & 0xffff;
*(rp ) = (png_byte)((red >> 8) & 0xff); *(rp ) = (png_byte)((red >> 8) & 0xff);
@ -531,6 +539,7 @@ png_read_row(png_structrp png_ptr, png_bytep row, png_bytep dsp_row)
png_error(png_ptr, "Invalid attempt to read row data"); png_error(png_ptr, "Invalid attempt to read row data");
/* Fill the row with IDAT data: */ /* Fill the row with IDAT data: */
png_ptr->row_buf[0]=255; /* to force error if no data was found */
png_read_IDAT_data(png_ptr, png_ptr->row_buf, row_info.rowbytes + 1); png_read_IDAT_data(png_ptr, png_ptr->row_buf, row_info.rowbytes + 1);
if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE) if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
@ -785,6 +794,9 @@ png_read_end(png_structrp png_ptr, png_inforp info_ptr)
png_uint_32 length = png_read_chunk_header(png_ptr); png_uint_32 length = png_read_chunk_header(png_ptr);
png_uint_32 chunk_name = png_ptr->chunk_name; png_uint_32 chunk_name = png_ptr->chunk_name;
if (chunk_name != png_IDAT)
png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
if (chunk_name == png_IEND) if (chunk_name == png_IEND)
png_handle_IEND(png_ptr, info_ptr, length); png_handle_IEND(png_ptr, info_ptr, length);
@ -799,9 +811,9 @@ png_read_end(png_structrp png_ptr, png_inforp info_ptr)
{ {
if (chunk_name == png_IDAT) if (chunk_name == png_IDAT)
{ {
if ((length > 0) || if ((length > 0 && !(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED))
(png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) != 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) != 0)
png_benign_error(png_ptr, "Too many IDATs found"); png_benign_error(png_ptr, ".Too many IDATs found");
} }
png_handle_unknown(png_ptr, info_ptr, length, keep); png_handle_unknown(png_ptr, info_ptr, length, keep);
if (chunk_name == png_PLTE) if (chunk_name == png_PLTE)
@ -812,10 +824,14 @@ png_read_end(png_structrp png_ptr, png_inforp info_ptr)
else if (chunk_name == png_IDAT) else if (chunk_name == png_IDAT)
{ {
/* Zero length IDATs are legal after the last IDAT has been /* Zero length IDATs are legal after the last IDAT has been
* read, but not after other chunks have been read. * read, but not after other chunks have been read. 1.6 does not
* always read all the deflate data; specifically it cannot be relied
* upon to read the Adler32 at the end. If it doesn't ignore IDAT
* chunks which are longer than zero as well:
*/ */
if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) != 0) if ((length > 0 && !(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED))
png_benign_error(png_ptr, "Too many IDATs found"); || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) != 0)
png_benign_error(png_ptr, "..Too many IDATs found");
png_crc_finish(png_ptr, length); png_crc_finish(png_ptr, length);
} }
@ -832,6 +848,11 @@ png_read_end(png_structrp png_ptr, png_inforp info_ptr)
png_handle_cHRM(png_ptr, info_ptr, length); png_handle_cHRM(png_ptr, info_ptr, length);
#endif #endif
#ifdef PNG_READ_eXIf_SUPPORTED
else if (chunk_name == png_eXIf)
png_handle_eXIf(png_ptr, info_ptr, length);
#endif
#ifdef PNG_READ_gAMA_SUPPORTED #ifdef PNG_READ_gAMA_SUPPORTED
else if (chunk_name == png_gAMA) else if (chunk_name == png_gAMA)
png_handle_gAMA(png_ptr, info_ptr, length); png_handle_gAMA(png_ptr, info_ptr, length);
@ -1020,8 +1041,7 @@ png_set_read_status_fn(png_structrp png_ptr, png_read_status_ptr read_row_fn)
#ifdef PNG_INFO_IMAGE_SUPPORTED #ifdef PNG_INFO_IMAGE_SUPPORTED
void PNGAPI void PNGAPI
png_read_png(png_structrp png_ptr, png_inforp info_ptr, png_read_png(png_structrp png_ptr, png_inforp info_ptr,
int transforms, int transforms, voidp params)
voidp params)
{ {
if (png_ptr == NULL || info_ptr == NULL) if (png_ptr == NULL || info_ptr == NULL)
return; return;
@ -1384,7 +1404,9 @@ png_image_read_header(png_voidp argument)
png_structrp png_ptr = image->opaque->png_ptr; png_structrp png_ptr = image->opaque->png_ptr;
png_inforp info_ptr = image->opaque->info_ptr; png_inforp info_ptr = image->opaque->info_ptr;
#ifdef PNG_BENIGN_ERRORS_SUPPORTED
png_set_benign_errors(png_ptr, 1/*warn*/); png_set_benign_errors(png_ptr, 1/*warn*/);
#endif
png_read_info(png_ptr, info_ptr); png_read_info(png_ptr, info_ptr);
/* Do this the fast way; just read directly out of png_struct. */ /* Do this the fast way; just read directly out of png_struct. */
@ -1422,7 +1444,7 @@ png_image_read_header(png_voidp argument)
break; break;
case PNG_COLOR_TYPE_PALETTE: case PNG_COLOR_TYPE_PALETTE:
cmap_entries = png_ptr->num_palette; cmap_entries = (png_uint_32)png_ptr->num_palette;
break; break;
default: default:
@ -1683,10 +1705,11 @@ decode_gamma(png_image_read_control *display, png_uint_32 value, int encoding)
value *= 257; value *= 257;
break; break;
#ifdef __GNUC__
default: default:
png_error(display->image->opaque->png_ptr, png_error(display->image->opaque->png_ptr,
"unexpected encoding (internal error)"); "unexpected encoding (internal error)");
break; #endif
} }
return value; return value;
@ -1871,7 +1894,7 @@ png_create_colormap_entry(png_image_read_control *display,
{ {
case 4: case 4:
entry[afirst ? 0 : 3] = (png_uint_16)alpha; entry[afirst ? 0 : 3] = (png_uint_16)alpha;
/* FALL THROUGH */ /* FALLTHROUGH */
case 3: case 3:
if (alpha < 65535) if (alpha < 65535)
@ -1893,7 +1916,7 @@ png_create_colormap_entry(png_image_read_control *display,
case 2: case 2:
entry[1 ^ afirst] = (png_uint_16)alpha; entry[1 ^ afirst] = (png_uint_16)alpha;
/* FALL THROUGH */ /* FALLTHROUGH */
case 1: case 1:
if (alpha < 65535) if (alpha < 65535)
@ -1922,6 +1945,7 @@ png_create_colormap_entry(png_image_read_control *display,
{ {
case 4: case 4:
entry[afirst ? 0 : 3] = (png_byte)alpha; entry[afirst ? 0 : 3] = (png_byte)alpha;
/* FALLTHROUGH */
case 3: case 3:
entry[afirst + (2 ^ bgr)] = (png_byte)blue; entry[afirst + (2 ^ bgr)] = (png_byte)blue;
entry[afirst + 1] = (png_byte)green; entry[afirst + 1] = (png_byte)green;
@ -1930,6 +1954,7 @@ png_create_colormap_entry(png_image_read_control *display,
case 2: case 2:
entry[1 ^ afirst] = (png_byte)alpha; entry[1 ^ afirst] = (png_byte)alpha;
/* FALLTHROUGH */
case 1: case 1:
entry[afirst] = (png_byte)green; entry[afirst] = (png_byte)green;
break; break;
@ -1956,7 +1981,7 @@ make_gray_file_colormap(png_image_read_control *display)
for (i=0; i<256; ++i) for (i=0; i<256; ++i)
png_create_colormap_entry(display, i, i, i, i, 255, P_FILE); png_create_colormap_entry(display, i, i, i, i, 255, P_FILE);
return i; return (int)i;
} }
static int static int
@ -1967,7 +1992,7 @@ make_gray_colormap(png_image_read_control *display)
for (i=0; i<256; ++i) for (i=0; i<256; ++i)
png_create_colormap_entry(display, i, i, i, i, 255, P_sRGB); png_create_colormap_entry(display, i, i, i, i, 255, P_sRGB);
return i; return (int)i;
} }
#define PNG_GRAY_COLORMAP_ENTRIES 256 #define PNG_GRAY_COLORMAP_ENTRIES 256
@ -2021,7 +2046,7 @@ make_ga_colormap(png_image_read_control *display)
P_sRGB); P_sRGB);
} }
return i; return (int)i;
} }
#define PNG_GA_COLORMAP_ENTRIES 256 #define PNG_GA_COLORMAP_ENTRIES 256
@ -2046,7 +2071,7 @@ make_rgb_colormap(png_image_read_control *display)
} }
} }
return i; return (int)i;
} }
#define PNG_RGB_COLORMAP_ENTRIES 216 #define PNG_RGB_COLORMAP_ENTRIES 216
@ -2094,7 +2119,7 @@ png_image_read_colormap(png_voidp argument)
else if (display->background == NULL /* no way to remove it */) else if (display->background == NULL /* no way to remove it */)
png_error(png_ptr, png_error(png_ptr,
"a background color must be supplied to remove alpha/transparency"); "background color must be supplied to remove alpha/transparency");
/* Get a copy of the background color (this avoids repeating the checks /* Get a copy of the background color (this avoids repeating the checks
* below.) The encoding is 8-bit sRGB or 16-bit linear, depending on the * below.) The encoding is 8-bit sRGB or 16-bit linear, depending on the
@ -2239,7 +2264,7 @@ png_image_read_colormap(png_voidp argument)
if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries) if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
png_error(png_ptr, "gray[16] color-map: too few entries"); png_error(png_ptr, "gray[16] color-map: too few entries");
cmap_entries = make_gray_colormap(display); cmap_entries = (unsigned int)make_gray_colormap(display);
if (png_ptr->num_trans > 0) if (png_ptr->num_trans > 0)
{ {
@ -2337,7 +2362,7 @@ png_image_read_colormap(png_voidp argument)
if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries) if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
png_error(png_ptr, "gray+alpha color-map: too few entries"); png_error(png_ptr, "gray+alpha color-map: too few entries");
cmap_entries = make_ga_colormap(display); cmap_entries = (unsigned int)make_ga_colormap(display);
background_index = PNG_CMAP_GA_BACKGROUND; background_index = PNG_CMAP_GA_BACKGROUND;
output_processing = PNG_CMAP_GA; output_processing = PNG_CMAP_GA;
@ -2371,7 +2396,7 @@ png_image_read_colormap(png_voidp argument)
if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries) if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
png_error(png_ptr, "gray-alpha color-map: too few entries"); png_error(png_ptr, "gray-alpha color-map: too few entries");
cmap_entries = make_gray_colormap(display); cmap_entries = (unsigned int)make_gray_colormap(display);
if (output_encoding == P_LINEAR) if (output_encoding == P_LINEAR)
{ {
@ -2419,8 +2444,8 @@ png_image_read_colormap(png_voidp argument)
background_index = i; background_index = i;
png_create_colormap_entry(display, i++, back_r, back_g, back_b, png_create_colormap_entry(display, i++, back_r, back_g, back_b,
#ifdef __COVERITY__ #ifdef __COVERITY__
/* Coverity claims that output_encoding cannot be 2 (P_LINEAR) /* Coverity claims that output_encoding
* here. * cannot be 2 (P_LINEAR) here.
*/ 255U, */ 255U,
#else #else
output_encoding == P_LINEAR ? 65535U : 255U, output_encoding == P_LINEAR ? 65535U : 255U,
@ -2510,7 +2535,7 @@ png_image_read_colormap(png_voidp argument)
if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries) if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
png_error(png_ptr, "rgb[ga] color-map: too few entries"); png_error(png_ptr, "rgb[ga] color-map: too few entries");
cmap_entries = make_ga_colormap(display); cmap_entries = (unsigned int)make_ga_colormap(display);
background_index = PNG_CMAP_GA_BACKGROUND; background_index = PNG_CMAP_GA_BACKGROUND;
output_processing = PNG_CMAP_GA; output_processing = PNG_CMAP_GA;
} }
@ -2536,12 +2561,12 @@ png_image_read_colormap(png_voidp argument)
png_ptr->num_trans > 0) && png_ptr->num_trans > 0) &&
png_gamma_not_sRGB(png_ptr->colorspace.gamma) != 0) png_gamma_not_sRGB(png_ptr->colorspace.gamma) != 0)
{ {
cmap_entries = make_gray_file_colormap(display); cmap_entries = (unsigned int)make_gray_file_colormap(display);
data_encoding = P_FILE; data_encoding = P_FILE;
} }
else else
cmap_entries = make_gray_colormap(display); cmap_entries = (unsigned int)make_gray_colormap(display);
/* But if the input has alpha or transparency it must be removed /* But if the input has alpha or transparency it must be removed
*/ */
@ -2629,7 +2654,7 @@ png_image_read_colormap(png_voidp argument)
if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries) if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries)
png_error(png_ptr, "rgb+alpha color-map: too few entries"); png_error(png_ptr, "rgb+alpha color-map: too few entries");
cmap_entries = make_rgb_colormap(display); cmap_entries = (unsigned int)make_rgb_colormap(display);
/* Add a transparent entry. */ /* Add a transparent entry. */
png_create_colormap_entry(display, cmap_entries, 255, 255, png_create_colormap_entry(display, cmap_entries, 255, 255,
@ -2678,7 +2703,7 @@ png_image_read_colormap(png_voidp argument)
if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries) if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries)
png_error(png_ptr, "rgb-alpha color-map: too few entries"); png_error(png_ptr, "rgb-alpha color-map: too few entries");
cmap_entries = make_rgb_colormap(display); cmap_entries = (unsigned int)make_rgb_colormap(display);
png_create_colormap_entry(display, cmap_entries, back_r, png_create_colormap_entry(display, cmap_entries, back_r,
back_g, back_b, 0/*unused*/, output_encoding); back_g, back_b, 0/*unused*/, output_encoding);
@ -2763,7 +2788,7 @@ png_image_read_colormap(png_voidp argument)
if (PNG_RGB_COLORMAP_ENTRIES > image->colormap_entries) if (PNG_RGB_COLORMAP_ENTRIES > image->colormap_entries)
png_error(png_ptr, "rgb color-map: too few entries"); png_error(png_ptr, "rgb color-map: too few entries");
cmap_entries = make_rgb_colormap(display); cmap_entries = (unsigned int)make_rgb_colormap(display);
output_processing = PNG_CMAP_RGB; output_processing = PNG_CMAP_RGB;
} }
} }
@ -2787,11 +2812,11 @@ png_image_read_colormap(png_voidp argument)
output_processing = PNG_CMAP_NONE; output_processing = PNG_CMAP_NONE;
data_encoding = P_FILE; /* Don't change from color-map indices */ data_encoding = P_FILE; /* Don't change from color-map indices */
cmap_entries = png_ptr->num_palette; cmap_entries = (unsigned int)png_ptr->num_palette;
if (cmap_entries > 256) if (cmap_entries > 256)
cmap_entries = 256; cmap_entries = 256;
if (cmap_entries > image->colormap_entries) if (cmap_entries > (unsigned int)image->colormap_entries)
png_error(png_ptr, "palette color-map: too few entries"); png_error(png_ptr, "palette color-map: too few entries");
for (i=0; i < cmap_entries; ++i) for (i=0; i < cmap_entries; ++i)
@ -2808,12 +2833,12 @@ png_image_read_colormap(png_voidp argument)
* on the sRGB color in 'back'. * on the sRGB color in 'back'.
*/ */
png_create_colormap_entry(display, i, png_create_colormap_entry(display, i,
png_colormap_compose(display, colormap[i].red, P_FILE, png_colormap_compose(display, colormap[i].red,
trans[i], back_r, output_encoding), P_FILE, trans[i], back_r, output_encoding),
png_colormap_compose(display, colormap[i].green, P_FILE, png_colormap_compose(display, colormap[i].green,
trans[i], back_g, output_encoding), P_FILE, trans[i], back_g, output_encoding),
png_colormap_compose(display, colormap[i].blue, P_FILE, png_colormap_compose(display, colormap[i].blue,
trans[i], back_b, output_encoding), P_FILE, trans[i], back_b, output_encoding),
output_encoding == P_LINEAR ? trans[i] * 257U : output_encoding == P_LINEAR ? trans[i] * 257U :
trans[i], trans[i],
output_encoding); output_encoding);
@ -2837,7 +2862,6 @@ png_image_read_colormap(png_voidp argument)
default: default:
png_error(png_ptr, "invalid PNG color type"); png_error(png_ptr, "invalid PNG color type");
/*NOT REACHED*/ /*NOT REACHED*/
break;
} }
/* Now deal with the output processing */ /* Now deal with the output processing */
@ -2847,19 +2871,20 @@ png_image_read_colormap(png_voidp argument)
switch (data_encoding) switch (data_encoding)
{ {
default:
png_error(png_ptr, "bad data option (internal error)");
break;
case P_sRGB: case P_sRGB:
/* Change to 8-bit sRGB */ /* Change to 8-bit sRGB */
png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, PNG_GAMMA_sRGB); png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, PNG_GAMMA_sRGB);
/* FALL THROUGH */ /* FALLTHROUGH */
case P_FILE: case P_FILE:
if (png_ptr->bit_depth > 8) if (png_ptr->bit_depth > 8)
png_set_scale_16(png_ptr); png_set_scale_16(png_ptr);
break; break;
#ifdef __GNUC__
default:
png_error(png_ptr, "bad data option (internal error)");
#endif
} }
if (cmap_entries > 256 || cmap_entries > image->colormap_entries) if (cmap_entries > 256 || cmap_entries > image->colormap_entries)
@ -2903,7 +2928,7 @@ png_image_read_colormap(png_voidp argument)
png_error(png_ptr, "bad background index (internal error)"); png_error(png_ptr, "bad background index (internal error)");
} }
display->colormap_processing = output_processing; display->colormap_processing = (int)output_processing;
return 1/*ok*/; return 1/*ok*/;
} }
@ -3167,8 +3192,7 @@ png_image_read_colormapped(png_voidp argument)
image->colormap_entries == 244 /* 216 + 1 + 27 */) image->colormap_entries == 244 /* 216 + 1 + 27 */)
break; break;
/* goto bad_output; */ goto bad_output;
/* FALL THROUGH */
default: default:
bad_output: bad_output:
@ -3212,14 +3236,14 @@ png_image_read_colormapped(png_voidp argument)
else else
{ {
png_alloc_size_t row_bytes = display->row_bytes; png_alloc_size_t row_bytes = (png_alloc_size_t)display->row_bytes;
while (--passes >= 0) while (--passes >= 0)
{ {
png_uint_32 y = image->height; png_uint_32 y = image->height;
png_bytep row = png_voidcast(png_bytep, display->first_row); png_bytep row = png_voidcast(png_bytep, display->first_row);
while (y-- > 0) for (; y > 0; --y)
{ {
png_read_row(png_ptr, row, NULL); png_read_row(png_ptr, row, NULL);
row += row_bytes; row += row_bytes;
@ -3410,10 +3434,6 @@ png_image_read_background(png_voidp argument)
*/ */
switch (info_ptr->bit_depth) switch (info_ptr->bit_depth)
{ {
default:
png_error(png_ptr, "unexpected bit depth");
break;
case 8: case 8:
/* 8-bit sRGB gray values with an alpha channel; the alpha channel is /* 8-bit sRGB gray values with an alpha channel; the alpha channel is
* to be removed by composing on a background: either the row if * to be removed by composing on a background: either the row if
@ -3426,8 +3446,7 @@ png_image_read_background(png_voidp argument)
for (pass = 0; pass < passes; ++pass) for (pass = 0; pass < passes; ++pass)
{ {
png_bytep row = png_voidcast(png_bytep, png_bytep row = png_voidcast(png_bytep, display->first_row);
display->first_row);
unsigned int startx, stepx, stepy; unsigned int startx, stepx, stepy;
png_uint_32 y; png_uint_32 y;
@ -3552,8 +3571,9 @@ png_image_read_background(png_voidp argument)
* stride which was multiplied by 2 (below) to get row_bytes. * stride which was multiplied by 2 (below) to get row_bytes.
*/ */
ptrdiff_t step_row = display->row_bytes / 2; ptrdiff_t step_row = display->row_bytes / 2;
int preserve_alpha = (image->format & PNG_FORMAT_FLAG_ALPHA) != 0; unsigned int preserve_alpha = (image->format &
unsigned int outchannels = 1+preserve_alpha; PNG_FORMAT_FLAG_ALPHA) != 0;
unsigned int outchannels = 1U+preserve_alpha;
int swap_alpha = 0; int swap_alpha = 0;
# ifdef PNG_SIMPLIFIED_READ_AFIRST_SUPPORTED # ifdef PNG_SIMPLIFIED_READ_AFIRST_SUPPORTED
@ -3631,6 +3651,11 @@ png_image_read_background(png_voidp argument)
} }
} }
break; break;
#ifdef __GNUC__
default:
png_error(png_ptr, "unexpected bit depth");
#endif
} }
return 1; return 1;
@ -3846,7 +3871,7 @@ png_image_read_direct(png_voidp argument)
else else
filler = 255; filler = 255;
# ifdef PNG_FORMAT_AFIRST_SUPPORTED #ifdef PNG_FORMAT_AFIRST_SUPPORTED
if ((format & PNG_FORMAT_FLAG_AFIRST) != 0) if ((format & PNG_FORMAT_FLAG_AFIRST) != 0)
{ {
where = PNG_FILLER_BEFORE; where = PNG_FILLER_BEFORE;
@ -3854,7 +3879,7 @@ png_image_read_direct(png_voidp argument)
} }
else else
# endif #endif
where = PNG_FILLER_AFTER; where = PNG_FILLER_AFTER;
png_set_add_alpha(png_ptr, filler, where); png_set_add_alpha(png_ptr, filler, where);
@ -3963,12 +3988,12 @@ png_image_read_direct(png_voidp argument)
if (info_ptr->bit_depth == 16) if (info_ptr->bit_depth == 16)
info_format |= PNG_FORMAT_FLAG_LINEAR; info_format |= PNG_FORMAT_FLAG_LINEAR;
# ifdef PNG_FORMAT_BGR_SUPPORTED #ifdef PNG_FORMAT_BGR_SUPPORTED
if ((png_ptr->transformations & PNG_BGR) != 0) if ((png_ptr->transformations & PNG_BGR) != 0)
info_format |= PNG_FORMAT_FLAG_BGR; info_format |= PNG_FORMAT_FLAG_BGR;
# endif #endif
# ifdef PNG_FORMAT_AFIRST_SUPPORTED #ifdef PNG_FORMAT_AFIRST_SUPPORTED
if (do_local_background == 2) if (do_local_background == 2)
{ {
if ((format & PNG_FORMAT_FLAG_AFIRST) != 0) if ((format & PNG_FORMAT_FLAG_AFIRST) != 0)
@ -4045,14 +4070,14 @@ png_image_read_direct(png_voidp argument)
else else
{ {
png_alloc_size_t row_bytes = display->row_bytes; png_alloc_size_t row_bytes = (png_alloc_size_t)display->row_bytes;
while (--passes >= 0) while (--passes >= 0)
{ {
png_uint_32 y = image->height; png_uint_32 y = image->height;
png_bytep row = png_voidcast(png_bytep, display->first_row); png_bytep row = png_voidcast(png_bytep, display->first_row);
while (y-- > 0) for (; y > 0; --y)
{ {
png_read_row(png_ptr, row, NULL); png_read_row(png_ptr, row, NULL);
row += row_bytes; row += row_bytes;
@ -4068,20 +4093,57 @@ png_image_finish_read(png_imagep image, png_const_colorp background,
void *buffer, png_int_32 row_stride, void *colormap) void *buffer, png_int_32 row_stride, void *colormap)
{ {
if (image != NULL && image->version == PNG_IMAGE_VERSION) if (image != NULL && image->version == PNG_IMAGE_VERSION)
{
/* Check for row_stride overflow. This check is not performed on the
* original PNG format because it may not occur in the output PNG format
* and libpng deals with the issues of reading the original.
*/
const unsigned int channels = PNG_IMAGE_PIXEL_CHANNELS(image->format);
/* The following checks just the 'row_stride' calculation to ensure it
* fits in a signed 32-bit value. Because channels/components can be
* either 1 or 2 bytes in size the length of a row can still overflow 32
* bits; this is just to verify that the 'row_stride' argument can be
* represented.
*/
if (image->width <= 0x7fffffffU/channels) /* no overflow */
{ {
png_uint_32 check; png_uint_32 check;
const png_uint_32 png_row_stride = image->width * channels;
if (row_stride == 0) if (row_stride == 0)
row_stride = PNG_IMAGE_ROW_STRIDE(*image); row_stride = (png_int_32)/*SAFE*/png_row_stride;
if (row_stride < 0) if (row_stride < 0)
check = -row_stride; check = (png_uint_32)(-row_stride);
else else
check = row_stride; check = (png_uint_32)row_stride;
if (image->opaque != NULL && buffer != NULL && /* This verifies 'check', the absolute value of the actual stride
check >= PNG_IMAGE_ROW_STRIDE(*image)) * passed in and detects overflow in the application calculation (i.e.
* if the app did actually pass in a non-zero 'row_stride'.
*/
if (image->opaque != NULL && buffer != NULL && check >= png_row_stride)
{
/* Now check for overflow of the image buffer calculation; this
* limits the whole image size to 32 bits for API compatibility with
* the current, 32-bit, PNG_IMAGE_BUFFER_SIZE macro.
*
* The PNG_IMAGE_BUFFER_SIZE macro is:
*
* (PNG_IMAGE_PIXEL_COMPONENT_SIZE(fmt)*height*(row_stride))
*
* And the component size is always 1 or 2, so make sure that the
* number of *bytes* that the application is saying are available
* does actually fit into a 32-bit number.
*
* NOTE: this will be changed in 1.7 because PNG_IMAGE_BUFFER_SIZE
* will be changed to use png_alloc_size_t; bigger images can be
* accomodated on 64-bit systems.
*/
if (image->height <=
0xffffffffU/PNG_IMAGE_PIXEL_COMPONENT_SIZE(image->format)/check)
{ {
if ((image->format & PNG_FORMAT_FLAG_COLORMAP) == 0 || if ((image->format & PNG_FORMAT_FLAG_COLORMAP) == 0 ||
(image->colormap_entries > 0 && colormap != NULL)) (image->colormap_entries > 0 && colormap != NULL))
@ -4097,17 +4159,20 @@ png_image_finish_read(png_imagep image, png_const_colorp background,
display.background = background; display.background = background;
display.local_row = NULL; display.local_row = NULL;
/* Choose the correct 'end' routine; for the color-map case all the /* Choose the correct 'end' routine; for the color-map case
* setup has already been done. * all the setup has already been done.
*/ */
if ((image->format & PNG_FORMAT_FLAG_COLORMAP) != 0) if ((image->format & PNG_FORMAT_FLAG_COLORMAP) != 0)
result = result =
png_safe_execute(image, png_image_read_colormap, &display) && png_safe_execute(image,
png_safe_execute(image, png_image_read_colormapped, &display); png_image_read_colormap, &display) &&
png_safe_execute(image,
png_image_read_colormapped, &display);
else else
result = result =
png_safe_execute(image, png_image_read_direct, &display); png_safe_execute(image,
png_image_read_direct, &display);
png_image_free(image); png_image_free(image);
return result; return result;
@ -4118,11 +4183,21 @@ png_image_finish_read(png_imagep image, png_const_colorp background,
"png_image_finish_read[color-map]: no color-map"); "png_image_finish_read[color-map]: no color-map");
} }
else
return png_image_error(image,
"png_image_finish_read: image too large");
}
else else
return png_image_error(image, return png_image_error(image,
"png_image_finish_read: invalid argument"); "png_image_finish_read: invalid argument");
} }
else
return png_image_error(image,
"png_image_finish_read: row_stride too large");
}
else if (image != NULL) else if (image != NULL)
return png_image_error(image, return png_image_error(image,
"png_image_finish_read: damaged PNG_IMAGE_VERSION"); "png_image_finish_read: damaged PNG_IMAGE_VERSION");

View File

@ -1,8 +1,8 @@
/* pngrio.c - functions for data input /* pngrio.c - functions for data input
* *
* Last changed in libpng 1.6.17 [March 26, 2015] * Last changed in libpng 1.6.24 [August 4, 2016]
* Copyright (c) 1998-2015 Glenn Randers-Pehrson * Copyright (c) 1998-2002,2004,2006-2016 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
* *
@ -26,7 +26,7 @@
* reads from a file pointer. Note that this routine sometimes gets called * reads from a file pointer. Note that this routine sometimes gets called
* with very small lengths, so you should implement some kind of simple * with very small lengths, so you should implement some kind of simple
* buffering if you are using unbuffered reads. This should never be asked * buffering if you are using unbuffered reads. This should never be asked
* to read more than 64K on a 16 bit machine. * to read more than 64K on a 16-bit machine.
*/ */
void /* PRIVATE */ void /* PRIVATE */
png_read_data(png_structrp png_ptr, png_bytep data, png_size_t length) png_read_data(png_structrp png_ptr, png_bytep data, png_size_t length)

View File

@ -1,8 +1,8 @@
/* pngrtran.c - transforms the data in a row for PNG readers /* pngrtran.c - transforms the data in a row for PNG readers
* *
* Last changed in libpng 1.6.18 [July 23, 2015] * Last changed in libpng 1.6.31 [July 27, 2017]
* Copyright (c) 1998-2015 Glenn Randers-Pehrson * Copyright (c) 1998-2002,2004,2006-2017 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
* *
@ -49,6 +49,7 @@ png_set_crc_action(png_structrp png_ptr, int crit_action, int ancil_action)
case PNG_CRC_WARN_DISCARD: /* Not a valid action for critical data */ case PNG_CRC_WARN_DISCARD: /* Not a valid action for critical data */
png_warning(png_ptr, png_warning(png_ptr,
"Can't discard critical data on CRC error"); "Can't discard critical data on CRC error");
/* FALLTHROUGH */
case PNG_CRC_ERROR_QUIT: /* Error/quit */ case PNG_CRC_ERROR_QUIT: /* Error/quit */
case PNG_CRC_DEFAULT: case PNG_CRC_DEFAULT:
@ -289,9 +290,12 @@ png_set_alpha_mode_fixed(png_structrp png_ptr, int mode,
* is expected to be 1 or greater, but this range test allows for some * is expected to be 1 or greater, but this range test allows for some
* viewing correction values. The intent is to weed out users of this API * viewing correction values. The intent is to weed out users of this API
* who use the inverse of the gamma value accidentally! Since some of these * who use the inverse of the gamma value accidentally! Since some of these
* values are reasonable this may have to be changed. * values are reasonable this may have to be changed:
*
* 1.6.x: changed from 0.07..3 to 0.01..100 (to accomodate the optimal 16-bit
* gamma of 36, and its reciprocal.)
*/ */
if (output_gamma < 70000 || output_gamma > 300000) if (output_gamma < 1000 || output_gamma > 10000000)
png_error(png_ptr, "output gamma out of expected range"); png_error(png_ptr, "output gamma out of expected range");
/* The default file gamma is the inverse of the output gamma; the output /* The default file gamma is the inverse of the output gamma; the output
@ -426,7 +430,7 @@ png_set_quantize(png_structrp png_ptr, png_colorp palette,
int i; int i;
png_ptr->quantize_index = (png_bytep)png_malloc(png_ptr, png_ptr->quantize_index = (png_bytep)png_malloc(png_ptr,
(png_uint_32)(num_palette * (sizeof (png_byte)))); (png_uint_32)((png_uint_32)num_palette * (sizeof (png_byte))));
for (i = 0; i < num_palette; i++) for (i = 0; i < num_palette; i++)
png_ptr->quantize_index[i] = (png_byte)i; png_ptr->quantize_index[i] = (png_byte)i;
} }
@ -443,7 +447,7 @@ png_set_quantize(png_structrp png_ptr, png_colorp palette,
/* Initialize an array to sort colors */ /* Initialize an array to sort colors */
png_ptr->quantize_sort = (png_bytep)png_malloc(png_ptr, png_ptr->quantize_sort = (png_bytep)png_malloc(png_ptr,
(png_uint_32)(num_palette * (sizeof (png_byte)))); (png_uint_32)((png_uint_32)num_palette * (sizeof (png_byte))));
/* Initialize the quantize_sort array */ /* Initialize the quantize_sort array */
for (i = 0; i < num_palette; i++) for (i = 0; i < num_palette; i++)
@ -577,9 +581,9 @@ png_set_quantize(png_structrp png_ptr, png_colorp palette,
/* Initialize palette index arrays */ /* Initialize palette index arrays */
png_ptr->index_to_palette = (png_bytep)png_malloc(png_ptr, png_ptr->index_to_palette = (png_bytep)png_malloc(png_ptr,
(png_uint_32)(num_palette * (sizeof (png_byte)))); (png_uint_32)((png_uint_32)num_palette * (sizeof (png_byte))));
png_ptr->palette_to_index = (png_bytep)png_malloc(png_ptr, png_ptr->palette_to_index = (png_bytep)png_malloc(png_ptr,
(png_uint_32)(num_palette * (sizeof (png_byte)))); (png_uint_32)((png_uint_32)num_palette * (sizeof (png_byte))));
/* Initialize the sort array */ /* Initialize the sort array */
for (i = 0; i < num_palette; i++) for (i = 0; i < num_palette; i++)
@ -976,7 +980,6 @@ png_set_rgb_to_gray_fixed(png_structrp png_ptr, int error_action,
default: default:
png_error(png_ptr, "invalid error action to rgb_to_gray"); png_error(png_ptr, "invalid error action to rgb_to_gray");
break;
} }
if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
@ -1251,7 +1254,7 @@ png_init_rgb_transformations(png_structrp png_ptr)
default: default:
case 8: case 8:
/* FALL THROUGH (Already 8 bits) */ /* FALLTHROUGH */ /* (Already 8 bits) */
case 16: case 16:
/* Already a full 16 bits */ /* Already a full 16 bits */
@ -1997,7 +2000,7 @@ png_read_transform_info(png_structrp png_ptr, png_inforp info_ptr)
# endif # endif
# else # else
/* No 16 bit support: force chopping 16-bit input down to 8, in this case /* No 16-bit support: force chopping 16-bit input down to 8, in this case
* the app program can chose if both APIs are available by setting the * the app program can chose if both APIs are available by setting the
* correct scaling to use. * correct scaling to use.
*/ */
@ -2098,10 +2101,10 @@ png_read_transform_info(png_structrp png_ptr, png_inforp info_ptr)
defined(PNG_READ_USER_TRANSFORM_SUPPORTED) defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0) if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0)
{ {
if (info_ptr->bit_depth < png_ptr->user_transform_depth) if (png_ptr->user_transform_depth != 0)
info_ptr->bit_depth = png_ptr->user_transform_depth; info_ptr->bit_depth = png_ptr->user_transform_depth;
if (info_ptr->channels < png_ptr->user_transform_channels) if (png_ptr->user_transform_channels != 0)
info_ptr->channels = png_ptr->user_transform_channels; info_ptr->channels = png_ptr->user_transform_channels;
} }
#endif #endif
@ -2148,7 +2151,7 @@ png_do_unpack(png_row_infop row_info, png_bytep row)
{ {
png_bytep sp = row + (png_size_t)((row_width - 1) >> 3); png_bytep sp = row + (png_size_t)((row_width - 1) >> 3);
png_bytep dp = row + (png_size_t)row_width - 1; png_bytep dp = row + (png_size_t)row_width - 1;
png_uint_32 shift = 7 - (int)((row_width + 7) & 0x07); png_uint_32 shift = 7U - ((row_width + 7U) & 0x07);
for (i = 0; i < row_width; i++) for (i = 0; i < row_width; i++)
{ {
*dp = (png_byte)((*sp >> shift) & 0x01); *dp = (png_byte)((*sp >> shift) & 0x01);
@ -2172,7 +2175,7 @@ png_do_unpack(png_row_infop row_info, png_bytep row)
png_bytep sp = row + (png_size_t)((row_width - 1) >> 2); png_bytep sp = row + (png_size_t)((row_width - 1) >> 2);
png_bytep dp = row + (png_size_t)row_width - 1; png_bytep dp = row + (png_size_t)row_width - 1;
png_uint_32 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1); png_uint_32 shift = ((3U - ((row_width + 3U) & 0x03)) << 1);
for (i = 0; i < row_width; i++) for (i = 0; i < row_width; i++)
{ {
*dp = (png_byte)((*sp >> shift) & 0x03); *dp = (png_byte)((*sp >> shift) & 0x03);
@ -2195,7 +2198,7 @@ png_do_unpack(png_row_infop row_info, png_bytep row)
{ {
png_bytep sp = row + (png_size_t)((row_width - 1) >> 1); png_bytep sp = row + (png_size_t)((row_width - 1) >> 1);
png_bytep dp = row + (png_size_t)row_width - 1; png_bytep dp = row + (png_size_t)row_width - 1;
png_uint_32 shift = (int)((1 - ((row_width + 1) & 0x01)) << 2); png_uint_32 shift = ((1U - ((row_width + 1U) & 0x01)) << 2);
for (i = 0; i < row_width; i++) for (i = 0; i < row_width; i++)
{ {
*dp = (png_byte)((*sp >> shift) & 0x0f); *dp = (png_byte)((*sp >> shift) & 0x0f);
@ -2382,8 +2385,8 @@ png_do_scale_16_to_8(png_row_infop row_info, png_bytep row)
while (sp < ep) while (sp < ep)
{ {
/* The input is an array of 16 bit components, these must be scaled to /* The input is an array of 16-bit components, these must be scaled to
* 8 bits each. For a 16 bit value V the required value (from the PNG * 8 bits each. For a 16-bit value V the required value (from the PNG
* specification) is: * specification) is:
* *
* (V * 255) / 65535 * (V * 255) / 65535
@ -2404,7 +2407,7 @@ png_do_scale_16_to_8(png_row_infop row_info, png_bytep row)
* *
* The approximate differs from the exact answer only when (vlo-vhi) is * The approximate differs from the exact answer only when (vlo-vhi) is
* 128; it then gives a correction of +1 when the exact correction is * 128; it then gives a correction of +1 when the exact correction is
* 0. This gives 128 errors. The exact answer (correct for all 16 bit * 0. This gives 128 errors. The exact answer (correct for all 16-bit
* input values) is: * input values) is:
* *
* error = (vlo-vhi+128)*65535 >> 24; * error = (vlo-vhi+128)*65535 >> 24;
@ -2932,7 +2935,7 @@ png_do_gray_to_rgb(png_row_infop row_info, png_bytep row)
* using the equation given in Poynton's ColorFAQ of 1998-01-04 at * using the equation given in Poynton's ColorFAQ of 1998-01-04 at
* <http://www.inforamp.net/~poynton/> (THIS LINK IS DEAD June 2008 but * <http://www.inforamp.net/~poynton/> (THIS LINK IS DEAD June 2008 but
* versions dated 1998 through November 2002 have been archived at * versions dated 1998 through November 2002 have been archived at
* http://web.archive.org/web/20000816232553/http://www.inforamp.net/ * https://web.archive.org/web/20000816232553/www.inforamp.net/
* ~poynton/notes/colour_and_gamma/ColorFAQ.txt ) * ~poynton/notes/colour_and_gamma/ColorFAQ.txt )
* Charles Poynton poynton at poynton.com * Charles Poynton poynton at poynton.com
* *
@ -3148,9 +3151,9 @@ png_do_rgb_to_gray(png_structrp png_ptr, png_row_infop row_info, png_bytep row)
if (red != green || red != blue) if (red != green || red != blue)
rgb_error |= 1; rgb_error |= 1;
/* From 1.5.5 in the 16 bit case do the accurate conversion even /* From 1.5.5 in the 16-bit case do the accurate conversion even
* in the 'fast' case - this is because this is where the code * in the 'fast' case - this is because this is where the code
* ends up when handling linear 16 bit data. * ends up when handling linear 16-bit data.
*/ */
gray16 = (png_uint_16)((rc*red + gc*green + bc*blue + 16384) >> gray16 = (png_uint_16)((rc*red + gc*green + bc*blue + 16384) >>
15); 15);
@ -3221,7 +3224,8 @@ png_do_compose(png_row_infop row_info, png_bytep row, png_structrp png_ptr)
== png_ptr->trans_color.gray) == png_ptr->trans_color.gray)
{ {
unsigned int tmp = *sp & (0x7f7f >> (7 - shift)); unsigned int tmp = *sp & (0x7f7f >> (7 - shift));
tmp |= png_ptr->background.gray << shift; tmp |=
(unsigned int)(png_ptr->background.gray << shift);
*sp = (png_byte)(tmp & 0xff); *sp = (png_byte)(tmp & 0xff);
} }
@ -3250,7 +3254,8 @@ png_do_compose(png_row_infop row_info, png_bytep row, png_structrp png_ptr)
== png_ptr->trans_color.gray) == png_ptr->trans_color.gray)
{ {
unsigned int tmp = *sp & (0x3f3f >> (6 - shift)); unsigned int tmp = *sp & (0x3f3f >> (6 - shift));
tmp |= png_ptr->background.gray << shift; tmp |=
(unsigned int)png_ptr->background.gray << shift;
*sp = (png_byte)(tmp & 0xff); *sp = (png_byte)(tmp & 0xff);
} }
@ -3260,7 +3265,7 @@ png_do_compose(png_row_infop row_info, png_bytep row, png_structrp png_ptr)
unsigned int g = (gamma_table [p | (p << 2) | unsigned int g = (gamma_table [p | (p << 2) |
(p << 4) | (p << 6)] >> 6) & 0x03; (p << 4) | (p << 6)] >> 6) & 0x03;
unsigned int tmp = *sp & (0x3f3f >> (6 - shift)); unsigned int tmp = *sp & (0x3f3f >> (6 - shift));
tmp |= g << shift; tmp |= (unsigned int)(g << shift);
*sp = (png_byte)(tmp & 0xff); *sp = (png_byte)(tmp & 0xff);
} }
@ -3286,7 +3291,8 @@ png_do_compose(png_row_infop row_info, png_bytep row, png_structrp png_ptr)
== png_ptr->trans_color.gray) == png_ptr->trans_color.gray)
{ {
unsigned int tmp = *sp & (0x3f3f >> (6 - shift)); unsigned int tmp = *sp & (0x3f3f >> (6 - shift));
tmp |= png_ptr->background.gray << shift; tmp |=
(unsigned int)png_ptr->background.gray << shift;
*sp = (png_byte)(tmp & 0xff); *sp = (png_byte)(tmp & 0xff);
} }
@ -3315,8 +3321,9 @@ png_do_compose(png_row_infop row_info, png_bytep row, png_structrp png_ptr)
if ((png_uint_16)((*sp >> shift) & 0x0f) if ((png_uint_16)((*sp >> shift) & 0x0f)
== png_ptr->trans_color.gray) == png_ptr->trans_color.gray)
{ {
unsigned int tmp = *sp & (0xf0f >> (4 - shift)); unsigned int tmp = *sp & (0x0f0f >> (4 - shift));
tmp |= png_ptr->background.gray << shift; tmp |=
(unsigned int)(png_ptr->background.gray << shift);
*sp = (png_byte)(tmp & 0xff); *sp = (png_byte)(tmp & 0xff);
} }
@ -3325,8 +3332,8 @@ png_do_compose(png_row_infop row_info, png_bytep row, png_structrp png_ptr)
unsigned int p = (*sp >> shift) & 0x0f; unsigned int p = (*sp >> shift) & 0x0f;
unsigned int g = (gamma_table[p | (p << 4)] >> 4) & unsigned int g = (gamma_table[p | (p << 4)] >> 4) &
0x0f; 0x0f;
unsigned int tmp = *sp & (0xf0f >> (4 - shift)); unsigned int tmp = *sp & (0x0f0f >> (4 - shift));
tmp |= g << shift; tmp |= (unsigned int)(g << shift);
*sp = (png_byte)(tmp & 0xff); *sp = (png_byte)(tmp & 0xff);
} }
@ -3351,8 +3358,9 @@ png_do_compose(png_row_infop row_info, png_bytep row, png_structrp png_ptr)
if ((png_uint_16)((*sp >> shift) & 0x0f) if ((png_uint_16)((*sp >> shift) & 0x0f)
== png_ptr->trans_color.gray) == png_ptr->trans_color.gray)
{ {
unsigned int tmp = *sp & (0xf0f >> (4 - shift)); unsigned int tmp = *sp & (0x0f0f >> (4 - shift));
tmp |= png_ptr->background.gray << shift; tmp |=
(unsigned int)(png_ptr->background.gray << shift);
*sp = (png_byte)(tmp & 0xff); *sp = (png_byte)(tmp & 0xff);
} }
@ -4295,7 +4303,7 @@ png_do_expand_palette(png_row_infop row_info, png_bytep row,
if (num_trans > 0) if (num_trans > 0)
{ {
sp = row + (png_size_t)row_width - 1; sp = row + (png_size_t)row_width - 1;
dp = row + (png_size_t)(row_width << 2) - 1; dp = row + ((png_size_t)row_width << 2) - 1;
for (i = 0; i < row_width; i++) for (i = 0; i < row_width; i++)
{ {
@ -4456,7 +4464,7 @@ png_do_expand(png_row_infop row_info, png_bytep row,
{ {
gray = gray & 0xff; gray = gray & 0xff;
sp = row + (png_size_t)row_width - 1; sp = row + (png_size_t)row_width - 1;
dp = row + (png_size_t)(row_width << 1) - 1; dp = row + ((png_size_t)row_width << 1) - 1;
for (i = 0; i < row_width; i++) for (i = 0; i < row_width; i++)
{ {
@ -4512,7 +4520,7 @@ png_do_expand(png_row_infop row_info, png_bytep row,
png_byte green = (png_byte)(trans_color->green & 0xff); png_byte green = (png_byte)(trans_color->green & 0xff);
png_byte blue = (png_byte)(trans_color->blue & 0xff); png_byte blue = (png_byte)(trans_color->blue & 0xff);
sp = row + (png_size_t)row_info->rowbytes - 1; sp = row + (png_size_t)row_info->rowbytes - 1;
dp = row + (png_size_t)(row_width << 2) - 1; dp = row + ((png_size_t)row_width << 2) - 1;
for (i = 0; i < row_width; i++) for (i = 0; i < row_width; i++)
{ {
if (*(sp - 2) == red && *(sp - 1) == green && *(sp) == blue) if (*(sp - 2) == red && *(sp - 1) == green && *(sp) == blue)
@ -4535,7 +4543,7 @@ png_do_expand(png_row_infop row_info, png_bytep row,
png_byte green_low = (png_byte)(trans_color->green & 0xff); png_byte green_low = (png_byte)(trans_color->green & 0xff);
png_byte blue_low = (png_byte)(trans_color->blue & 0xff); png_byte blue_low = (png_byte)(trans_color->blue & 0xff);
sp = row + row_info->rowbytes - 1; sp = row + row_info->rowbytes - 1;
dp = row + (png_size_t)(row_width << 3) - 1; dp = row + ((png_size_t)row_width << 3) - 1;
for (i = 0; i < row_width; i++) for (i = 0; i < row_width; i++)
{ {
if (*(sp - 5) == red_high && if (*(sp - 5) == red_high &&
@ -4594,7 +4602,9 @@ png_do_expand_16(png_row_infop row_info, png_bytep row)
png_byte *sp = row + row_info->rowbytes; /* source, last byte + 1 */ png_byte *sp = row + row_info->rowbytes; /* source, last byte + 1 */
png_byte *dp = sp + row_info->rowbytes; /* destination, end + 1 */ png_byte *dp = sp + row_info->rowbytes; /* destination, end + 1 */
while (dp > sp) while (dp > sp)
dp[-2] = dp[-1] = *--sp, dp -= 2; {
dp[-2] = dp[-1] = *--sp; dp -= 2;
}
row_info->rowbytes *= 2; row_info->rowbytes *= 2;
row_info->bit_depth = 16; row_info->bit_depth = 16;

View File

@ -1,8 +1,8 @@
/* pngrutil.c - utilities to read a PNG file /* pngrutil.c - utilities to read a PNG file
* *
* Last changed in libpng 1.6.18 [July 23, 2015] * Last changed in libpng 1.6.32 [August 24, 2017]
* Copyright (c) 1998-2015 Glenn Randers-Pehrson * Copyright (c) 1998-2002,2004,2006-2017 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
* *
@ -86,10 +86,16 @@ png_get_int_32)(png_const_bytep buf)
{ {
png_uint_32 uval = png_get_uint_32(buf); png_uint_32 uval = png_get_uint_32(buf);
if ((uval & 0x80000000) == 0) /* non-negative */ if ((uval & 0x80000000) == 0) /* non-negative */
return uval; return (png_int_32)uval;
uval = (uval ^ 0xffffffff) + 1; /* 2's complement: -x = ~x+1 */ uval = (uval ^ 0xffffffff) + 1; /* 2's complement: -x = ~x+1 */
if ((uval & 0x80000000) == 0) /* no overflow */
return -(png_int_32)uval; return -(png_int_32)uval;
/* The following has to be safe; this function only gets called on PNG data
* and if we get here that data is invalid. 0 is the most safe value and
* if not then an attacker would surely just generate a PNG with 0 instead.
*/
return 0;
} }
/* Grab an unsigned 16-bit integer from a buffer in big-endian format. */ /* Grab an unsigned 16-bit integer from a buffer in big-endian format. */
@ -98,7 +104,7 @@ png_get_uint_16)(png_const_bytep buf)
{ {
/* ANSI-C requires an int value to accomodate at least 16 bits so this /* ANSI-C requires an int value to accomodate at least 16 bits so this
* works and allows the compiler not to worry about possible narrowing * works and allows the compiler not to worry about possible narrowing
* on 32 bit systems. (Pre-ANSI systems did not make integers smaller * on 32-bit systems. (Pre-ANSI systems did not make integers smaller
* than 16 bits either.) * than 16 bits either.)
*/ */
unsigned int val = unsigned int val =
@ -175,6 +181,9 @@ png_read_chunk_header(png_structrp png_ptr)
/* Check to see if chunk name is valid. */ /* Check to see if chunk name is valid. */
png_check_chunk_name(png_ptr, png_ptr->chunk_name); png_check_chunk_name(png_ptr, png_ptr->chunk_name);
/* Check for too-large chunk length */
png_check_chunk_length(png_ptr, length);
#ifdef PNG_IO_STATE_SUPPORTED #ifdef PNG_IO_STATE_SUPPORTED
png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_DATA; png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_DATA;
#endif #endif
@ -364,21 +373,24 @@ png_inflate_claim(png_structrp png_ptr, png_uint_32 owner)
*/ */
{ {
int ret; /* zlib return code */ int ret; /* zlib return code */
#if PNG_ZLIB_VERNUM >= 0x1240 #if ZLIB_VERNUM >= 0x1240
int window_bits = 0;
# if defined(PNG_SET_OPTION_SUPPORTED) && defined(PNG_MAXIMUM_INFLATE_WINDOW) # if defined(PNG_SET_OPTION_SUPPORTED) && defined(PNG_MAXIMUM_INFLATE_WINDOW)
int window_bits;
if (((png_ptr->options >> PNG_MAXIMUM_INFLATE_WINDOW) & 3) == if (((png_ptr->options >> PNG_MAXIMUM_INFLATE_WINDOW) & 3) ==
PNG_OPTION_ON) PNG_OPTION_ON)
{
window_bits = 15; window_bits = 15;
png_ptr->zstream_start = 0; /* fixed window size */
}
else else
window_bits = 0; {
# else png_ptr->zstream_start = 1;
# define window_bits 0 }
# endif # endif
#endif
#endif /* ZLIB_VERNUM >= 0x1240 */
/* Set this for safety, just in case the previous owner left pointers to /* Set this for safety, just in case the previous owner left pointers to
* memory allocations. * memory allocations.
@ -390,25 +402,32 @@ png_inflate_claim(png_structrp png_ptr, png_uint_32 owner)
if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0) if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0)
{ {
#if PNG_ZLIB_VERNUM < 0x1240 #if ZLIB_VERNUM >= 0x1240
ret = inflateReset(&png_ptr->zstream);
#else
ret = inflateReset2(&png_ptr->zstream, window_bits); ret = inflateReset2(&png_ptr->zstream, window_bits);
#else
ret = inflateReset(&png_ptr->zstream);
#endif #endif
} }
else else
{ {
#if PNG_ZLIB_VERNUM < 0x1240 #if ZLIB_VERNUM >= 0x1240
ret = inflateInit(&png_ptr->zstream);
#else
ret = inflateInit2(&png_ptr->zstream, window_bits); ret = inflateInit2(&png_ptr->zstream, window_bits);
#else
ret = inflateInit(&png_ptr->zstream);
#endif #endif
if (ret == Z_OK) if (ret == Z_OK)
png_ptr->flags |= PNG_FLAG_ZSTREAM_INITIALIZED; png_ptr->flags |= PNG_FLAG_ZSTREAM_INITIALIZED;
} }
#if ZLIB_VERNUM >= 0x1290 && \
defined(PNG_SET_OPTION_SUPPORTED) && defined(PNG_IGNORE_ADLER32)
if (((png_ptr->options >> PNG_IGNORE_ADLER32) & 3) == PNG_OPTION_ON)
/* Turn off validation of the ADLER32 checksum in IDAT chunks */
ret = inflateValidate(&png_ptr->zstream, 0);
#endif
if (ret == Z_OK) if (ret == Z_OK)
png_ptr->zowner = owner; png_ptr->zowner = owner;
@ -423,7 +442,33 @@ png_inflate_claim(png_structrp png_ptr, png_uint_32 owner)
#endif #endif
} }
#if ZLIB_VERNUM >= 0x1240
/* Handle the start of the inflate stream if we called inflateInit2(strm,0);
* in this case some zlib versions skip validation of the CINFO field and, in
* certain circumstances, libpng may end up displaying an invalid image, in
* contrast to implementations that call zlib in the normal way (e.g. libpng
* 1.5).
*/
int /* PRIVATE */
png_zlib_inflate(png_structrp png_ptr, int flush)
{
if (png_ptr->zstream_start && png_ptr->zstream.avail_in > 0)
{
if ((*png_ptr->zstream.next_in >> 4) > 7)
{
png_ptr->zstream.msg = "invalid window size (libpng)";
return Z_DATA_ERROR;
}
png_ptr->zstream_start = 0;
}
return inflate(&png_ptr->zstream, flush);
}
#endif /* Zlib >= 1.2.4 */
#ifdef PNG_READ_COMPRESSED_TEXT_SUPPORTED #ifdef PNG_READ_COMPRESSED_TEXT_SUPPORTED
#if defined(PNG_READ_zTXt_SUPPORTED) || defined (PNG_READ_iTXt_SUPPORTED)
/* png_inflate now returns zlib error codes including Z_OK and Z_STREAM_END to /* png_inflate now returns zlib error codes including Z_OK and Z_STREAM_END to
* allow the caller to do multiple calls if required. If the 'finish' flag is * allow the caller to do multiple calls if required. If the 'finish' flag is
* set Z_FINISH will be passed to the final inflate() call and Z_STREAM_END must * set Z_FINISH will be passed to the final inflate() call and Z_STREAM_END must
@ -516,7 +561,7 @@ png_inflate(png_structrp png_ptr, png_uint_32 owner, int finish,
* the previous chunk of input data. Tell zlib if we have reached the * the previous chunk of input data. Tell zlib if we have reached the
* end of the output buffer. * end of the output buffer.
*/ */
ret = inflate(&png_ptr->zstream, avail_out > 0 ? Z_NO_FLUSH : ret = PNG_INFLATE(png_ptr, avail_out > 0 ? Z_NO_FLUSH :
(finish ? Z_FINISH : Z_SYNC_FLUSH)); (finish ? Z_FINISH : Z_SYNC_FLUSH));
} while (ret == Z_OK); } while (ret == Z_OK);
@ -717,6 +762,7 @@ png_decompress_chunk(png_structrp png_ptr,
return Z_MEM_ERROR; return Z_MEM_ERROR;
} }
} }
#endif /* READ_zTXt || READ_iTXt */
#endif /* READ_COMPRESSED_TEXT */ #endif /* READ_COMPRESSED_TEXT */
#ifdef PNG_READ_iCCP_SUPPORTED #ifdef PNG_READ_iCCP_SUPPORTED
@ -765,8 +811,8 @@ png_inflate_read(png_structrp png_ptr, png_bytep read_buffer, uInt read_size,
* the available output is produced; this allows reading of truncated * the available output is produced; this allows reading of truncated
* streams. * streams.
*/ */
ret = inflate(&png_ptr->zstream, ret = PNG_INFLATE(png_ptr, *chunk_bytes > 0 ?
*chunk_bytes > 0 ? Z_NO_FLUSH : (finish ? Z_FINISH : Z_SYNC_FLUSH)); Z_NO_FLUSH : (finish ? Z_FINISH : Z_SYNC_FLUSH));
} }
while (ret == Z_OK && (*out_size > 0 || png_ptr->zstream.avail_out > 0)); while (ret == Z_OK && (*out_size > 0 || png_ptr->zstream.avail_out > 0));
@ -784,7 +830,7 @@ png_inflate_read(png_structrp png_ptr, png_bytep read_buffer, uInt read_size,
return Z_STREAM_ERROR; return Z_STREAM_ERROR;
} }
} }
#endif #endif /* READ_iCCP */
/* Read and check the IDHR chunk */ /* Read and check the IDHR chunk */
@ -866,7 +912,7 @@ void /* PRIVATE */
png_handle_PLTE(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) png_handle_PLTE(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
{ {
png_color palette[PNG_MAX_PALETTE_LENGTH]; png_color palette[PNG_MAX_PALETTE_LENGTH];
int num, i; int max_palette_length, num, i;
#ifdef PNG_POINTER_INDEXING_SUPPORTED #ifdef PNG_POINTER_INDEXING_SUPPORTED
png_colorp pal_ptr; png_colorp pal_ptr;
#endif #endif
@ -927,6 +973,19 @@ png_handle_PLTE(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
/* The cast is safe because 'length' is less than 3*PNG_MAX_PALETTE_LENGTH */ /* The cast is safe because 'length' is less than 3*PNG_MAX_PALETTE_LENGTH */
num = (int)length / 3; num = (int)length / 3;
/* If the palette has 256 or fewer entries but is too large for the bit
* depth, we don't issue an error, to preserve the behavior of previous
* libpng versions. We silently truncate the unused extra palette entries
* here.
*/
if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
max_palette_length = (1 << png_ptr->bit_depth);
else
max_palette_length = PNG_MAX_PALETTE_LENGTH;
if (num > max_palette_length)
num = max_palette_length;
#ifdef PNG_POINTER_INDEXING_SUPPORTED #ifdef PNG_POINTER_INDEXING_SUPPORTED
for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++) for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)
{ {
@ -959,7 +1018,7 @@ png_handle_PLTE(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
#endif #endif
{ {
png_crc_finish(png_ptr, 0); png_crc_finish(png_ptr, (png_uint_32) (length - (unsigned int)num * 3));
} }
#ifndef PNG_READ_OPT_PLTE_SUPPORTED #ifndef PNG_READ_OPT_PLTE_SUPPORTED
@ -1321,11 +1380,13 @@ png_handle_iCCP(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
* chunk is just ignored, so does not invalidate the color space. An * chunk is just ignored, so does not invalidate the color space. An
* alternative is to set the 'invalid' flags at the start of this routine * alternative is to set the 'invalid' flags at the start of this routine
* and only clear them in they were not set before and all the tests pass. * and only clear them in they were not set before and all the tests pass.
* The minimum 'deflate' stream is assumed to be just the 2 byte header and
* 4 byte checksum. The keyword must be at least one character and there is
* a terminator (0) byte and the compression method.
*/ */
if (length < 9)
/* The keyword must be at least one character and there is a
* terminator (0) byte and the compression method byte, and the
* 'zlib' datastream is at least 11 bytes.
*/
if (length < 14)
{ {
png_crc_finish(png_ptr, length); png_crc_finish(png_ptr, length);
png_chunk_benign_error(png_ptr, "too short"); png_chunk_benign_error(png_ptr, "too short");
@ -1357,6 +1418,16 @@ png_handle_iCCP(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
png_crc_read(png_ptr, (png_bytep)keyword, read_length); png_crc_read(png_ptr, (png_bytep)keyword, read_length);
length -= read_length; length -= read_length;
/* The minimum 'zlib' stream is assumed to be just the 2 byte header,
* 5 bytes minimum 'deflate' stream, and the 4 byte checksum.
*/
if (length < 11)
{
png_crc_finish(png_ptr, length);
png_chunk_benign_error(png_ptr, "too short");
return;
}
keyword_length = 0; keyword_length = 0;
while (keyword_length < 80 && keyword_length < read_length && while (keyword_length < 80 && keyword_length < read_length &&
keyword[keyword_length] != 0) keyword[keyword_length] != 0)
@ -1375,7 +1446,7 @@ png_handle_iCCP(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
if (png_inflate_claim(png_ptr, png_iCCP) == Z_OK) if (png_inflate_claim(png_ptr, png_iCCP) == Z_OK)
{ {
Byte profile_header[132]; Byte profile_header[132]={0};
Byte local_buffer[PNG_INFLATE_BUF_SIZE]; Byte local_buffer[PNG_INFLATE_BUF_SIZE];
png_alloc_size_t size = (sizeof profile_header); png_alloc_size_t size = (sizeof profile_header);
@ -1462,7 +1533,7 @@ png_handle_iCCP(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
png_crc_finish(png_ptr, length); png_crc_finish(png_ptr, length);
finished = 1; finished = 1;
# ifdef PNG_sRGB_SUPPORTED # if defined(PNG_sRGB_SUPPORTED) && PNG_sRGB_PROFILE_CHECKS >= 0
/* Check for a match against sRGB */ /* Check for a match against sRGB */
png_icc_set_sRGB(png_ptr, png_icc_set_sRGB(png_ptr,
&png_ptr->colorspace, profile, &png_ptr->colorspace, profile,
@ -1651,7 +1722,7 @@ png_handle_sPLT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
++entry_start; ++entry_start;
/* A sample depth should follow the separator, and we should be on it */ /* A sample depth should follow the separator, and we should be on it */
if (entry_start > buffer + length - 2) if (length < 2U || entry_start > buffer + (length - 2U))
{ {
png_warning(png_ptr, "malformed sPLT chunk"); png_warning(png_ptr, "malformed sPLT chunk");
return; return;
@ -1665,13 +1736,13 @@ png_handle_sPLT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
data_length = length - (png_uint_32)(entry_start - buffer); data_length = length - (png_uint_32)(entry_start - buffer);
/* Integrity-check the data length */ /* Integrity-check the data length */
if ((data_length % entry_size) != 0) if ((data_length % (unsigned int)entry_size) != 0)
{ {
png_warning(png_ptr, "sPLT chunk has bad length"); png_warning(png_ptr, "sPLT chunk has bad length");
return; return;
} }
dl = (png_int_32)(data_length / entry_size); dl = (png_uint_32)(data_length / (unsigned int)entry_size);
max_dl = PNG_SIZE_MAX / (sizeof (png_sPLT_entry)); max_dl = PNG_SIZE_MAX / (sizeof (png_sPLT_entry));
if (dl > max_dl) if (dl > max_dl)
@ -1680,10 +1751,10 @@ png_handle_sPLT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
return; return;
} }
new_palette.nentries = (png_int_32)(data_length / entry_size); new_palette.nentries = (png_int_32)(data_length / (unsigned int)entry_size);
new_palette.entries = (png_sPLT_entryp)png_malloc_warn( new_palette.entries = (png_sPLT_entryp)png_malloc_warn(png_ptr,
png_ptr, new_palette.nentries * (sizeof (png_sPLT_entry))); (png_alloc_size_t) new_palette.nentries * (sizeof (png_sPLT_entry)));
if (new_palette.entries == NULL) if (new_palette.entries == NULL)
{ {
@ -1818,7 +1889,7 @@ png_handle_tRNS(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
return; return;
} }
if (length > png_ptr->num_palette || if (length > (unsigned int) png_ptr->num_palette ||
length > (unsigned int) PNG_MAX_PALETTE_LENGTH || length > (unsigned int) PNG_MAX_PALETTE_LENGTH ||
length == 0) length == 0)
{ {
@ -1953,6 +2024,69 @@ png_handle_bKGD(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
} }
#endif #endif
#ifdef PNG_READ_eXIf_SUPPORTED
void /* PRIVATE */
png_handle_eXIf(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
{
unsigned int i;
png_debug(1, "in png_handle_eXIf");
if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
png_chunk_error(png_ptr, "missing IHDR");
if (length < 2)
{
png_crc_finish(png_ptr, length);
png_chunk_benign_error(png_ptr, "too short");
return;
}
else if (info_ptr == NULL || (info_ptr->valid & PNG_INFO_eXIf) != 0)
{
png_crc_finish(png_ptr, length);
png_chunk_benign_error(png_ptr, "duplicate");
return;
}
info_ptr->free_me |= PNG_FREE_EXIF;
info_ptr->eXIf_buf = png_voidcast(png_bytep,
png_malloc_warn(png_ptr, length));
if (info_ptr->eXIf_buf == NULL)
{
png_crc_finish(png_ptr, length);
png_chunk_benign_error(png_ptr, "out of memory");
return;
}
for (i = 0; i < length; i++)
{
png_byte buf[1];
png_crc_read(png_ptr, buf, 1);
info_ptr->eXIf_buf[i] = buf[0];
if (i == 1 && buf[0] != 'M' && buf[0] != 'I'
&& info_ptr->eXIf_buf[0] != buf[0])
{
png_crc_finish(png_ptr, length);
png_chunk_benign_error(png_ptr, "incorrect byte-order specifier");
png_free(png_ptr, info_ptr->eXIf_buf);
info_ptr->eXIf_buf = NULL;
return;
}
}
if (png_crc_finish(png_ptr, 0) != 0)
return;
png_set_eXIf_1(png_ptr, info_ptr, length, info_ptr->eXIf_buf);
png_free(png_ptr, info_ptr->eXIf_buf);
info_ptr->eXIf_buf = NULL;
}
#endif
#ifdef PNG_READ_hIST_SUPPORTED #ifdef PNG_READ_hIST_SUPPORTED
void /* PRIVATE */ void /* PRIVATE */
png_handle_hIST(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) png_handle_hIST(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
@ -1982,7 +2116,8 @@ png_handle_hIST(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
num = length / 2 ; num = length / 2 ;
if (num != png_ptr->num_palette || num > (unsigned int) PNG_MAX_PALETTE_LENGTH) if (num != (unsigned int) png_ptr->num_palette ||
num > (unsigned int) PNG_MAX_PALETTE_LENGTH)
{ {
png_crc_finish(png_ptr, length); png_crc_finish(png_ptr, length);
png_chunk_benign_error(png_ptr, "invalid"); png_chunk_benign_error(png_ptr, "invalid");
@ -2154,7 +2289,7 @@ png_handle_pCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
/* We need to have at least 12 bytes after the purpose string /* We need to have at least 12 bytes after the purpose string
* in order to get the parameter information. * in order to get the parameter information.
*/ */
if (endptr <= buf + 12) if (endptr - buf <= 12)
{ {
png_chunk_benign_error(png_ptr, "invalid"); png_chunk_benign_error(png_ptr, "invalid");
return; return;
@ -2480,6 +2615,9 @@ png_handle_zTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
if ((png_ptr->mode & PNG_HAVE_IDAT) != 0) if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
png_ptr->mode |= PNG_AFTER_IDAT; png_ptr->mode |= PNG_AFTER_IDAT;
/* Note, "length" is sufficient here; we won't be adding
* a null terminator later.
*/
buffer = png_read_buffer(png_ptr, length, 2/*silent*/); buffer = png_read_buffer(png_ptr, length, 2/*silent*/);
if (buffer == NULL) if (buffer == NULL)
@ -2526,9 +2664,13 @@ png_handle_zTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
{ {
png_text text; png_text text;
/* It worked; png_ptr->read_buffer now looks like a tEXt chunk except if (png_ptr->read_buffer == NULL)
* for the extra compression type byte and the fact that it isn't errmsg="Read failure in png_handle_zTXt";
* necessarily '\0' terminated. else
{
/* It worked; png_ptr->read_buffer now looks like a tEXt chunk
* except for the extra compression type byte and the fact that
* it isn't necessarily '\0' terminated.
*/ */
buffer = png_ptr->read_buffer; buffer = png_ptr->read_buffer;
buffer[uncompressed_length+(keyword_length+2)] = 0; buffer[uncompressed_length+(keyword_length+2)] = 0;
@ -2544,6 +2686,7 @@ png_handle_zTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
if (png_set_text_2(png_ptr, info_ptr, &text, 1) != 0) if (png_set_text_2(png_ptr, info_ptr, &text, 1) != 0)
errmsg = "insufficient memory"; errmsg = "insufficient memory";
} }
}
else else
errmsg = png_ptr->zstream.msg; errmsg = png_ptr->zstream.msg;
@ -2918,7 +3061,7 @@ png_handle_unknown(png_structrp png_ptr, png_inforp info_ptr,
case 2: case 2:
png_ptr->user_chunk_cache_max = 1; png_ptr->user_chunk_cache_max = 1;
png_chunk_benign_error(png_ptr, "no space in chunk cache"); png_chunk_benign_error(png_ptr, "no space in chunk cache");
/* FALL THROUGH */ /* FALLTHROUGH */
case 1: case 1:
/* NOTE: prior to 1.6.0 this case resulted in an unknown critical /* NOTE: prior to 1.6.0 this case resulted in an unknown critical
* chunk being skipped, now there will be a hard error below. * chunk being skipped, now there will be a hard error below.
@ -2927,7 +3070,7 @@ png_handle_unknown(png_structrp png_ptr, png_inforp info_ptr,
default: /* not at limit */ default: /* not at limit */
--(png_ptr->user_chunk_cache_max); --(png_ptr->user_chunk_cache_max);
/* FALL THROUGH */ /* FALLTHROUGH */
case 0: /* no limit */ case 0: /* no limit */
# endif /* USER_LIMITS */ # endif /* USER_LIMITS */
/* Here when the limit isn't reached or when limits are compiled /* Here when the limit isn't reached or when limits are compiled
@ -2978,20 +3121,58 @@ png_handle_unknown(png_structrp png_ptr, png_inforp info_ptr,
*/ */
void /* PRIVATE */ void /* PRIVATE */
png_check_chunk_name(png_structrp png_ptr, png_uint_32 chunk_name) png_check_chunk_name(png_const_structrp png_ptr, const png_uint_32 chunk_name)
{ {
int i; int i;
png_uint_32 cn=chunk_name;
png_debug(1, "in png_check_chunk_name"); png_debug(1, "in png_check_chunk_name");
for (i=1; i<=4; ++i) for (i=1; i<=4; ++i)
{ {
int c = chunk_name & 0xff; int c = cn & 0xff;
if (c < 65 || c > 122 || (c > 90 && c < 97)) if (c < 65 || c > 122 || (c > 90 && c < 97))
png_chunk_error(png_ptr, "invalid chunk type"); png_chunk_error(png_ptr, "invalid chunk type");
chunk_name >>= 8; cn >>= 8;
}
}
void /* PRIVATE */
png_check_chunk_length(png_const_structrp png_ptr, const png_uint_32 length)
{
png_alloc_size_t limit = PNG_UINT_31_MAX;
if (png_ptr->chunk_name != png_IDAT)
{
# ifdef PNG_SET_USER_LIMITS_SUPPORTED
if (png_ptr->user_chunk_malloc_max > 0 &&
png_ptr->user_chunk_malloc_max < limit)
limit = png_ptr->user_chunk_malloc_max;
# elif PNG_USER_CHUNK_MALLOC_MAX > 0
if (PNG_USER_CHUNK_MALLOC_MAX < limit)
limit = PNG_USER_CHUNK_MALLOC_MAX;
# endif
}
else
{
size_t row_factor =
(png_ptr->width * png_ptr->channels * (png_ptr->bit_depth > 8? 2: 1)
+ 1 + (png_ptr->interlaced? 6: 0));
if (png_ptr->height > PNG_UINT_32_MAX/row_factor)
limit=PNG_UINT_31_MAX;
else
limit = png_ptr->height * row_factor;
limit += 6 + 5*(limit/32566+1); /* zlib+deflate overhead */
limit=limit < PNG_UINT_31_MAX? limit : PNG_UINT_31_MAX;
}
if (length > limit)
{
png_debug2(0," length = %lu, limit = %lu",
(unsigned long)length,(unsigned long)limit);
png_chunk_error(png_ptr, "chunk data is too large");
} }
} }
@ -3046,7 +3227,7 @@ png_combine_row(png_const_structrp png_ptr, png_bytep dp, int display)
# ifdef PNG_READ_PACKSWAP_SUPPORTED # ifdef PNG_READ_PACKSWAP_SUPPORTED
if ((png_ptr->transformations & PNG_PACKSWAP) != 0) if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
/* little-endian byte */ /* little-endian byte */
end_mask = 0xff << end_mask; end_mask = (unsigned int)(0xff << end_mask);
else /* big-endian byte */ else /* big-endian byte */
# endif # endif
@ -3320,7 +3501,7 @@ png_combine_row(png_const_structrp png_ptr, png_bytep dp, int display)
*/ */
do do
{ {
dp[0] = sp[0], dp[1] = sp[1]; dp[0] = sp[0]; dp[1] = sp[1];
if (row_width <= bytes_to_jump) if (row_width <= bytes_to_jump)
return; return;
@ -3341,7 +3522,7 @@ png_combine_row(png_const_structrp png_ptr, png_bytep dp, int display)
*/ */
for (;;) for (;;)
{ {
dp[0] = sp[0], dp[1] = sp[1], dp[2] = sp[2]; dp[0] = sp[0]; dp[1] = sp[1]; dp[2] = sp[2];
if (row_width <= bytes_to_jump) if (row_width <= bytes_to_jump)
return; return;
@ -3367,8 +3548,8 @@ png_combine_row(png_const_structrp png_ptr, png_bytep dp, int display)
/* Everything is aligned for png_uint_16 copies, but try for /* Everything is aligned for png_uint_16 copies, but try for
* png_uint_32 first. * png_uint_32 first.
*/ */
if (png_isaligned(dp, png_uint_32) != 0 && if (png_isaligned(dp, png_uint_32) &&
png_isaligned(sp, png_uint_32) != 0 && png_isaligned(sp, png_uint_32) &&
bytes_to_copy % (sizeof (png_uint_32)) == 0 && bytes_to_copy % (sizeof (png_uint_32)) == 0 &&
bytes_to_jump % (sizeof (png_uint_32)) == 0) bytes_to_jump % (sizeof (png_uint_32)) == 0)
{ {
@ -3492,7 +3673,7 @@ png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
{ {
/* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
/* Offset to next interlace block */ /* Offset to next interlace block */
static PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; static PNG_CONST unsigned int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
png_debug(1, "in png_do_read_interlace"); png_debug(1, "in png_do_read_interlace");
if (row != NULL && row_info != NULL) if (row != NULL && row_info != NULL)
@ -3507,9 +3688,10 @@ png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
{ {
png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3); png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3);
png_bytep dp = row + (png_size_t)((final_width - 1) >> 3); png_bytep dp = row + (png_size_t)((final_width - 1) >> 3);
int sshift, dshift; unsigned int sshift, dshift;
int s_start, s_end, s_inc; unsigned int s_start, s_end;
int jstop = png_pass_inc[pass]; int s_inc;
int jstop = (int)png_pass_inc[pass];
png_byte v; png_byte v;
png_uint_32 i; png_uint_32 i;
int j; int j;
@ -3517,8 +3699,8 @@ png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
#ifdef PNG_READ_PACKSWAP_SUPPORTED #ifdef PNG_READ_PACKSWAP_SUPPORTED
if ((transformations & PNG_PACKSWAP) != 0) if ((transformations & PNG_PACKSWAP) != 0)
{ {
sshift = (int)((row_info->width + 7) & 0x07); sshift = ((row_info->width + 7) & 0x07);
dshift = (int)((final_width + 7) & 0x07); dshift = ((final_width + 7) & 0x07);
s_start = 7; s_start = 7;
s_end = 0; s_end = 0;
s_inc = -1; s_inc = -1;
@ -3527,8 +3709,8 @@ png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
else else
#endif #endif
{ {
sshift = 7 - (int)((row_info->width + 7) & 0x07); sshift = 7 - ((row_info->width + 7) & 0x07);
dshift = 7 - (int)((final_width + 7) & 0x07); dshift = 7 - ((final_width + 7) & 0x07);
s_start = 0; s_start = 0;
s_end = 7; s_end = 7;
s_inc = 1; s_inc = 1;
@ -3540,7 +3722,7 @@ png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
for (j = 0; j < jstop; j++) for (j = 0; j < jstop; j++)
{ {
unsigned int tmp = *dp & (0x7f7f >> (7 - dshift)); unsigned int tmp = *dp & (0x7f7f >> (7 - dshift));
tmp |= v << dshift; tmp |= (unsigned int)(v << dshift);
*dp = (png_byte)(tmp & 0xff); *dp = (png_byte)(tmp & 0xff);
if (dshift == s_end) if (dshift == s_end)
@ -3550,7 +3732,7 @@ png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
} }
else else
dshift += s_inc; dshift = (unsigned int)((int)dshift + s_inc);
} }
if (sshift == s_end) if (sshift == s_end)
@ -3560,7 +3742,7 @@ png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
} }
else else
sshift += s_inc; sshift = (unsigned int)((int)sshift + s_inc);
} }
break; break;
} }
@ -3569,16 +3751,17 @@ png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
{ {
png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2); png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);
png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2); png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);
int sshift, dshift; unsigned int sshift, dshift;
int s_start, s_end, s_inc; unsigned int s_start, s_end;
int jstop = png_pass_inc[pass]; int s_inc;
int jstop = (int)png_pass_inc[pass];
png_uint_32 i; png_uint_32 i;
#ifdef PNG_READ_PACKSWAP_SUPPORTED #ifdef PNG_READ_PACKSWAP_SUPPORTED
if ((transformations & PNG_PACKSWAP) != 0) if ((transformations & PNG_PACKSWAP) != 0)
{ {
sshift = (int)(((row_info->width + 3) & 0x03) << 1); sshift = (((row_info->width + 3) & 0x03) << 1);
dshift = (int)(((final_width + 3) & 0x03) << 1); dshift = (((final_width + 3) & 0x03) << 1);
s_start = 6; s_start = 6;
s_end = 0; s_end = 0;
s_inc = -2; s_inc = -2;
@ -3587,8 +3770,8 @@ png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
else else
#endif #endif
{ {
sshift = (int)((3 - ((row_info->width + 3) & 0x03)) << 1); sshift = ((3 - ((row_info->width + 3) & 0x03)) << 1);
dshift = (int)((3 - ((final_width + 3) & 0x03)) << 1); dshift = ((3 - ((final_width + 3) & 0x03)) << 1);
s_start = 0; s_start = 0;
s_end = 6; s_end = 6;
s_inc = 2; s_inc = 2;
@ -3603,7 +3786,7 @@ png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
for (j = 0; j < jstop; j++) for (j = 0; j < jstop; j++)
{ {
unsigned int tmp = *dp & (0x3f3f >> (6 - dshift)); unsigned int tmp = *dp & (0x3f3f >> (6 - dshift));
tmp |= v << dshift; tmp |= (unsigned int)(v << dshift);
*dp = (png_byte)(tmp & 0xff); *dp = (png_byte)(tmp & 0xff);
if (dshift == s_end) if (dshift == s_end)
@ -3613,7 +3796,7 @@ png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
} }
else else
dshift += s_inc; dshift = (unsigned int)((int)dshift + s_inc);
} }
if (sshift == s_end) if (sshift == s_end)
@ -3623,7 +3806,7 @@ png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
} }
else else
sshift += s_inc; sshift = (unsigned int)((int)sshift + s_inc);
} }
break; break;
} }
@ -3632,16 +3815,17 @@ png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
{ {
png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1); png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1);
png_bytep dp = row + (png_size_t)((final_width - 1) >> 1); png_bytep dp = row + (png_size_t)((final_width - 1) >> 1);
int sshift, dshift; unsigned int sshift, dshift;
int s_start, s_end, s_inc; unsigned int s_start, s_end;
int s_inc;
png_uint_32 i; png_uint_32 i;
int jstop = png_pass_inc[pass]; int jstop = (int)png_pass_inc[pass];
#ifdef PNG_READ_PACKSWAP_SUPPORTED #ifdef PNG_READ_PACKSWAP_SUPPORTED
if ((transformations & PNG_PACKSWAP) != 0) if ((transformations & PNG_PACKSWAP) != 0)
{ {
sshift = (int)(((row_info->width + 1) & 0x01) << 2); sshift = (((row_info->width + 1) & 0x01) << 2);
dshift = (int)(((final_width + 1) & 0x01) << 2); dshift = (((final_width + 1) & 0x01) << 2);
s_start = 4; s_start = 4;
s_end = 0; s_end = 0;
s_inc = -4; s_inc = -4;
@ -3650,8 +3834,8 @@ png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
else else
#endif #endif
{ {
sshift = (int)((1 - ((row_info->width + 1) & 0x01)) << 2); sshift = ((1 - ((row_info->width + 1) & 0x01)) << 2);
dshift = (int)((1 - ((final_width + 1) & 0x01)) << 2); dshift = ((1 - ((final_width + 1) & 0x01)) << 2);
s_start = 0; s_start = 0;
s_end = 4; s_end = 4;
s_inc = 4; s_inc = 4;
@ -3665,7 +3849,7 @@ png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
for (j = 0; j < jstop; j++) for (j = 0; j < jstop; j++)
{ {
unsigned int tmp = *dp & (0xf0f >> (4 - dshift)); unsigned int tmp = *dp & (0xf0f >> (4 - dshift));
tmp |= v << dshift; tmp |= (unsigned int)(v << dshift);
*dp = (png_byte)(tmp & 0xff); *dp = (png_byte)(tmp & 0xff);
if (dshift == s_end) if (dshift == s_end)
@ -3675,7 +3859,7 @@ png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
} }
else else
dshift += s_inc; dshift = (unsigned int)((int)dshift + s_inc);
} }
if (sshift == s_end) if (sshift == s_end)
@ -3685,7 +3869,7 @@ png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
} }
else else
sshift += s_inc; sshift = (unsigned int)((int)sshift + s_inc);
} }
break; break;
} }
@ -3699,7 +3883,7 @@ png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes; png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes;
int jstop = png_pass_inc[pass]; int jstop = (int)png_pass_inc[pass];
png_uint_32 i; png_uint_32 i;
for (i = 0; i < row_info->width; i++) for (i = 0; i < row_info->width; i++)
@ -3827,7 +4011,10 @@ png_read_filter_row_paeth_1byte_pixel(png_row_infop row_info, png_bytep row,
/* Find the best predictor, the least of pa, pb, pc favoring the earlier /* Find the best predictor, the least of pa, pb, pc favoring the earlier
* ones in the case of a tie. * ones in the case of a tie.
*/ */
if (pb < pa) pa = pb, a = b; if (pb < pa)
{
pa = pb; a = b;
}
if (pc < pa) a = c; if (pc < pa) a = c;
/* Calculate the current pixel in a, and move the previous row pixel to c /* Calculate the current pixel in a, and move the previous row pixel to c
@ -3843,7 +4030,7 @@ static void
png_read_filter_row_paeth_multibyte_pixel(png_row_infop row_info, png_bytep row, png_read_filter_row_paeth_multibyte_pixel(png_row_infop row_info, png_bytep row,
png_const_bytep prev_row) png_const_bytep prev_row)
{ {
int bpp = (row_info->pixel_depth + 7) >> 3; unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
png_bytep rp_end = row + bpp; png_bytep rp_end = row + bpp;
/* Process the first pixel in the row completely (this is the same as 'up' /* Process the first pixel in the row completely (this is the same as 'up'
@ -3856,7 +4043,7 @@ png_read_filter_row_paeth_multibyte_pixel(png_row_infop row_info, png_bytep row,
} }
/* Remainder */ /* Remainder */
rp_end += row_info->rowbytes - bpp; rp_end = rp_end + (row_info->rowbytes - bpp);
while (row < rp_end) while (row < rp_end)
{ {
@ -3879,7 +4066,10 @@ png_read_filter_row_paeth_multibyte_pixel(png_row_infop row_info, png_bytep row,
pc = (p + pc) < 0 ? -(p + pc) : p + pc; pc = (p + pc) < 0 ? -(p + pc) : p + pc;
#endif #endif
if (pb < pa) pa = pb, a = b; if (pb < pa)
{
pa = pb; a = b;
}
if (pc < pa) a = c; if (pc < pa) a = c;
a += *row; a += *row;
@ -4019,7 +4209,7 @@ png_read_IDAT_data(png_structrp png_ptr, png_bytep output,
* *
* TODO: deal more elegantly with truncated IDAT lists. * TODO: deal more elegantly with truncated IDAT lists.
*/ */
ret = inflate(&png_ptr->zstream, Z_NO_FLUSH); ret = PNG_INFLATE(png_ptr, Z_NO_FLUSH);
/* Take the unconsumed output back. */ /* Take the unconsumed output back. */
if (output != NULL) if (output != NULL)
@ -4201,7 +4391,7 @@ png_read_start_row(png_structrp png_ptr)
/* Offset to next interlace block in the y direction */ /* Offset to next interlace block in the y direction */
static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
int max_pixel_depth; unsigned int max_pixel_depth;
png_size_t row_bytes; png_size_t row_bytes;
png_debug(1, "in png_read_start_row"); png_debug(1, "in png_read_start_row");
@ -4230,7 +4420,7 @@ png_read_start_row(png_structrp png_ptr)
png_ptr->iwidth = png_ptr->width; png_ptr->iwidth = png_ptr->width;
} }
max_pixel_depth = png_ptr->pixel_depth; max_pixel_depth = (unsigned int)png_ptr->pixel_depth;
/* WARNING: * png_read_transform_info (pngrtran.c) performs a simpler set of /* WARNING: * png_read_transform_info (pngrtran.c) performs a simpler set of
* calculations to calculate the final pixel depth, then * calculations to calculate the final pixel depth, then
@ -4365,7 +4555,7 @@ png_read_start_row(png_structrp png_ptr)
defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0) if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0)
{ {
int user_pixel_depth = png_ptr->user_transform_depth * unsigned int user_pixel_depth = png_ptr->user_transform_depth *
png_ptr->user_transform_channels; png_ptr->user_transform_channels;
if (user_pixel_depth > max_pixel_depth) if (user_pixel_depth > max_pixel_depth)
@ -4387,7 +4577,7 @@ defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
* for safety's sake * for safety's sake
*/ */
row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) + row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) +
1 + ((max_pixel_depth + 7) >> 3); 1 + ((max_pixel_depth + 7) >> 3U);
#ifdef PNG_MAX_MALLOC_64K #ifdef PNG_MAX_MALLOC_64K
if (row_bytes > (png_uint_32)65536L) if (row_bytes > (png_uint_32)65536L)
@ -4456,7 +4646,7 @@ defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
* does not, so free the read buffer now regardless; the sequential reader * does not, so free the read buffer now regardless; the sequential reader
* reallocates it on demand. * reallocates it on demand.
*/ */
if (png_ptr->read_buffer != 0) if (png_ptr->read_buffer != NULL)
{ {
png_bytep buffer = png_ptr->read_buffer; png_bytep buffer = png_ptr->read_buffer;

View File

@ -1,8 +1,8 @@
/* pngset.c - storage of image information into info struct /* pngset.c - storage of image information into info struct
* *
* Last changed in libpng 1.6.18 [July 23, 2015] * Last changed in libpng 1.6.32 [August 24, 2017]
* Copyright (c) 1998-2015 Glenn Randers-Pehrson * Copyright (c) 1998-2017 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
* *
@ -123,17 +123,64 @@ png_set_cHRM_XYZ(png_const_structrp png_ptr, png_inforp info_ptr, double red_X,
png_fixed(png_ptr, red_X, "cHRM Red X"), png_fixed(png_ptr, red_X, "cHRM Red X"),
png_fixed(png_ptr, red_Y, "cHRM Red Y"), png_fixed(png_ptr, red_Y, "cHRM Red Y"),
png_fixed(png_ptr, red_Z, "cHRM Red Z"), png_fixed(png_ptr, red_Z, "cHRM Red Z"),
png_fixed(png_ptr, green_X, "cHRM Red X"), png_fixed(png_ptr, green_X, "cHRM Green X"),
png_fixed(png_ptr, green_Y, "cHRM Red Y"), png_fixed(png_ptr, green_Y, "cHRM Green Y"),
png_fixed(png_ptr, green_Z, "cHRM Red Z"), png_fixed(png_ptr, green_Z, "cHRM Green Z"),
png_fixed(png_ptr, blue_X, "cHRM Red X"), png_fixed(png_ptr, blue_X, "cHRM Blue X"),
png_fixed(png_ptr, blue_Y, "cHRM Red Y"), png_fixed(png_ptr, blue_Y, "cHRM Blue Y"),
png_fixed(png_ptr, blue_Z, "cHRM Red Z")); png_fixed(png_ptr, blue_Z, "cHRM Blue Z"));
} }
# endif /* FLOATING_POINT */ # endif /* FLOATING_POINT */
#endif /* cHRM */ #endif /* cHRM */
#ifdef PNG_eXIf_SUPPORTED
void PNGAPI
png_set_eXIf(png_const_structrp png_ptr, png_inforp info_ptr,
const png_bytep eXIf_buf)
{
png_warning(png_ptr, "png_set_eXIf does not work; use png_set_eXIf_1");
PNG_UNUSED(info_ptr)
PNG_UNUSED(eXIf_buf)
}
void PNGAPI
png_set_eXIf_1(png_const_structrp png_ptr, png_inforp info_ptr,
const png_uint_32 num_exif, const png_bytep eXIf_buf)
{
int i;
png_debug1(1, "in %s storage function", "eXIf");
if (png_ptr == NULL || info_ptr == NULL)
return;
if (info_ptr->exif)
{
png_free(png_ptr, info_ptr->exif);
info_ptr->exif = NULL;
}
info_ptr->num_exif = num_exif;
info_ptr->exif = png_voidcast(png_bytep, png_malloc_warn(png_ptr,
info_ptr->num_exif));
if (info_ptr->exif == NULL)
{
png_warning(png_ptr, "Insufficient memory for eXIf chunk data");
return;
}
info_ptr->free_me |= PNG_FREE_EXIF;
for (i = 0; i < (int) info_ptr->num_exif; i++)
info_ptr->exif[i] = eXIf_buf[i];
info_ptr->valid |= PNG_INFO_eXIf;
}
#endif /* eXIf */
#ifdef PNG_gAMA_SUPPORTED #ifdef PNG_gAMA_SUPPORTED
void PNGFAPI void PNGFAPI
png_set_gAMA_fixed(png_const_structrp png_ptr, png_inforp info_ptr, png_set_gAMA_fixed(png_const_structrp png_ptr, png_inforp info_ptr,
@ -283,17 +330,29 @@ png_set_pCAL(png_const_structrp png_ptr, png_inforp info_ptr,
/* Check that the type matches the specification. */ /* Check that the type matches the specification. */
if (type < 0 || type > 3) if (type < 0 || type > 3)
png_error(png_ptr, "Invalid pCAL equation type"); {
png_chunk_report(png_ptr, "Invalid pCAL equation type",
PNG_CHUNK_WRITE_ERROR);
return;
}
if (nparams < 0 || nparams > 255) if (nparams < 0 || nparams > 255)
png_error(png_ptr, "Invalid pCAL parameter count"); {
png_chunk_report(png_ptr, "Invalid pCAL parameter count",
PNG_CHUNK_WRITE_ERROR);
return;
}
/* Validate params[nparams] */ /* Validate params[nparams] */
for (i=0; i<nparams; ++i) for (i=0; i<nparams; ++i)
{ {
if (params[i] == NULL || if (params[i] == NULL ||
!png_check_fp_string(params[i], strlen(params[i]))) !png_check_fp_string(params[i], strlen(params[i])))
png_error(png_ptr, "Invalid format for pCAL parameter"); {
png_chunk_report(png_ptr, "Invalid format for pCAL parameter",
PNG_CHUNK_WRITE_ERROR);
return;
}
} }
info_ptr->pcal_purpose = png_voidcast(png_charp, info_ptr->pcal_purpose = png_voidcast(png_charp,
@ -301,8 +360,8 @@ png_set_pCAL(png_const_structrp png_ptr, png_inforp info_ptr,
if (info_ptr->pcal_purpose == NULL) if (info_ptr->pcal_purpose == NULL)
{ {
png_warning(png_ptr, "Insufficient memory for pCAL purpose"); png_chunk_report(png_ptr, "Insufficient memory for pCAL purpose",
PNG_CHUNK_WRITE_ERROR);
return; return;
} }
@ -331,7 +390,7 @@ png_set_pCAL(png_const_structrp png_ptr, png_inforp info_ptr,
memcpy(info_ptr->pcal_units, units, length); memcpy(info_ptr->pcal_units, units, length);
info_ptr->pcal_params = png_voidcast(png_charpp, png_malloc_warn(png_ptr, info_ptr->pcal_params = png_voidcast(png_charpp, png_malloc_warn(png_ptr,
(png_size_t)((nparams + 1) * (sizeof (png_charp))))); (png_size_t)(((unsigned int)nparams + 1) * (sizeof (png_charp)))));
if (info_ptr->pcal_params == NULL) if (info_ptr->pcal_params == NULL)
{ {
@ -340,7 +399,8 @@ png_set_pCAL(png_const_structrp png_ptr, png_inforp info_ptr,
return; return;
} }
memset(info_ptr->pcal_params, 0, (nparams + 1) * (sizeof (png_charp))); memset(info_ptr->pcal_params, 0, ((unsigned int)nparams + 1) *
(sizeof (png_charp)));
for (i = 0; i < nparams; i++) for (i = 0; i < nparams; i++)
{ {
@ -513,12 +573,17 @@ png_set_PLTE(png_structrp png_ptr, png_inforp info_ptr,
png_const_colorp palette, int num_palette) png_const_colorp palette, int num_palette)
{ {
png_uint_32 max_palette_length;
png_debug1(1, "in %s storage function", "PLTE"); png_debug1(1, "in %s storage function", "PLTE");
if (png_ptr == NULL || info_ptr == NULL) if (png_ptr == NULL || info_ptr == NULL)
return; return;
if (num_palette < 0 || num_palette > PNG_MAX_PALETTE_LENGTH) max_palette_length = (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ?
(1 << info_ptr->bit_depth) : PNG_MAX_PALETTE_LENGTH;
if (num_palette < 0 || num_palette > (int) max_palette_length)
{ {
if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
png_error(png_ptr, "Invalid palette length"); png_error(png_ptr, "Invalid palette length");
@ -551,14 +616,15 @@ png_set_PLTE(png_structrp png_ptr, png_inforp info_ptr,
png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0); png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0);
/* Changed in libpng-1.2.1 to allocate PNG_MAX_PALETTE_LENGTH instead /* Changed in libpng-1.2.1 to allocate PNG_MAX_PALETTE_LENGTH instead
* of num_palette entries, in case of an invalid PNG file that has * of num_palette entries, in case of an invalid PNG file or incorrect
* too-large sample values. * call to png_set_PLTE() with too-large sample values.
*/ */
png_ptr->palette = png_voidcast(png_colorp, png_calloc(png_ptr, png_ptr->palette = png_voidcast(png_colorp, png_calloc(png_ptr,
PNG_MAX_PALETTE_LENGTH * (sizeof (png_color)))); PNG_MAX_PALETTE_LENGTH * (sizeof (png_color))));
if (num_palette > 0) if (num_palette > 0)
memcpy(png_ptr->palette, palette, num_palette * (sizeof (png_color))); memcpy(png_ptr->palette, palette, (unsigned int)num_palette *
(sizeof (png_color)));
info_ptr->palette = png_ptr->palette; info_ptr->palette = png_ptr->palette;
info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette; info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette;
@ -709,7 +775,7 @@ png_set_text_2(png_const_structrp png_ptr, png_inforp info_ptr,
{ {
int i; int i;
png_debug1(1, "in %lx storage function", png_ptr == NULL ? 0xabadca11 : png_debug1(1, "in %lx storage function", png_ptr == NULL ? 0xabadca11U :
(unsigned long)png_ptr->chunk_name); (unsigned long)png_ptr->chunk_name);
if (png_ptr == NULL || info_ptr == NULL || num_text <= 0 || text_ptr == NULL) if (png_ptr == NULL || info_ptr == NULL || num_text <= 0 || text_ptr == NULL)
@ -947,13 +1013,15 @@ png_set_tRNS(png_structrp png_ptr, png_inforp info_ptr,
png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0); png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);
/* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */
png_ptr->trans_alpha = info_ptr->trans_alpha = png_voidcast(png_bytep,
png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH));
if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH) if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH)
{
/* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */
info_ptr->trans_alpha = png_voidcast(png_bytep,
png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH));
memcpy(info_ptr->trans_alpha, trans_alpha, (png_size_t)num_trans); memcpy(info_ptr->trans_alpha, trans_alpha, (png_size_t)num_trans);
} }
png_ptr->trans_alpha = info_ptr->trans_alpha;
}
if (trans_color != NULL) if (trans_color != NULL)
{ {
@ -1073,7 +1141,7 @@ png_set_sPLT(png_const_structrp png_ptr,
* checked it when doing the allocation. * checked it when doing the allocation.
*/ */
memcpy(np->entries, entries->entries, memcpy(np->entries, entries->entries,
entries->nentries * sizeof (png_sPLT_entry)); (unsigned int)entries->nentries * sizeof (png_sPLT_entry));
/* Note that 'continue' skips the advance of the out pointer and out /* Note that 'continue' skips the advance of the out pointer and out
* count, so an invalid entry is not added. * count, so an invalid entry is not added.
@ -1081,8 +1149,9 @@ png_set_sPLT(png_const_structrp png_ptr,
info_ptr->valid |= PNG_INFO_sPLT; info_ptr->valid |= PNG_INFO_sPLT;
++(info_ptr->splt_palettes_num); ++(info_ptr->splt_palettes_num);
++np; ++np;
++entries;
} }
while (++entries, --nentries); while (--nentries);
if (nentries > 0) if (nentries > 0)
png_chunk_report(png_ptr, "sPLT out of memory", PNG_CHUNK_WRITE_ERROR); png_chunk_report(png_ptr, "sPLT out of memory", PNG_CHUNK_WRITE_ERROR);
@ -1242,7 +1311,7 @@ png_set_unknown_chunk_location(png_const_structrp png_ptr, png_inforp info_ptr,
{ {
png_app_error(png_ptr, "invalid unknown chunk location"); png_app_error(png_ptr, "invalid unknown chunk location");
/* Fake out the pre 1.6.0 behavior: */ /* Fake out the pre 1.6.0 behavior: */
if ((location & PNG_HAVE_IDAT) != 0) /* undocumented! */ if (((unsigned int)location & PNG_HAVE_IDAT) != 0) /* undocumented! */
location = PNG_AFTER_IDAT; location = PNG_AFTER_IDAT;
else else
@ -1333,6 +1402,7 @@ png_set_keep_unknown_chunks(png_structrp png_ptr, int keep,
static PNG_CONST png_byte chunks_to_ignore[] = { static PNG_CONST png_byte chunks_to_ignore[] = {
98, 75, 71, 68, '\0', /* bKGD */ 98, 75, 71, 68, '\0', /* bKGD */
99, 72, 82, 77, '\0', /* cHRM */ 99, 72, 82, 77, '\0', /* cHRM */
101, 88, 73, 102, '\0', /* eXIf */
103, 65, 77, 65, '\0', /* gAMA */ 103, 65, 77, 65, '\0', /* gAMA */
104, 73, 83, 84, '\0', /* hIST */ 104, 73, 83, 84, '\0', /* hIST */
105, 67, 67, 80, '\0', /* iCCP */ 105, 67, 67, 80, '\0', /* iCCP */
@ -1366,7 +1436,7 @@ png_set_keep_unknown_chunks(png_structrp png_ptr, int keep,
return; return;
} }
num_chunks = num_chunks_in; num_chunks = (unsigned int)num_chunks_in;
} }
old_num_chunks = png_ptr->num_chunk_list; old_num_chunks = png_ptr->num_chunk_list;
@ -1556,7 +1626,7 @@ void PNGAPI
png_set_invalid(png_const_structrp png_ptr, png_inforp info_ptr, int mask) png_set_invalid(png_const_structrp png_ptr, png_inforp info_ptr, int mask)
{ {
if (png_ptr != NULL && info_ptr != NULL) if (png_ptr != NULL && info_ptr != NULL)
info_ptr->valid &= ~mask; info_ptr->valid &= (unsigned int)(~mask);
} }
@ -1568,7 +1638,7 @@ png_set_user_limits (png_structrp png_ptr, png_uint_32 user_width_max,
{ {
/* Images with dimensions larger than these limits will be /* Images with dimensions larger than these limits will be
* rejected by png_set_IHDR(). To accept any PNG datastream * rejected by png_set_IHDR(). To accept any PNG datastream
* regardless of dimensions, set both limits to 0x7ffffffL. * regardless of dimensions, set both limits to 0x7fffffff.
*/ */
if (png_ptr == NULL) if (png_ptr == NULL)
return; return;
@ -1639,4 +1709,94 @@ png_set_check_for_invalid_index(png_structrp png_ptr, int allowed)
png_ptr->num_palette_max = -1; png_ptr->num_palette_max = -1;
} }
#endif #endif
#if defined(PNG_TEXT_SUPPORTED) || defined(PNG_pCAL_SUPPORTED) || \
defined(PNG_iCCP_SUPPORTED) || defined(PNG_sPLT_SUPPORTED)
/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
* and if invalid, correct the keyword rather than discarding the entire
* chunk. The PNG 1.0 specification requires keywords 1-79 characters in
* length, forbids leading or trailing whitespace, multiple internal spaces,
* and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
*
* The 'new_key' buffer must be 80 characters in size (for the keyword plus a
* trailing '\0'). If this routine returns 0 then there was no keyword, or a
* valid one could not be generated, and the caller must png_error.
*/
png_uint_32 /* PRIVATE */
png_check_keyword(png_structrp png_ptr, png_const_charp key, png_bytep new_key)
{
#ifdef PNG_WARNINGS_SUPPORTED
png_const_charp orig_key = key;
#endif
png_uint_32 key_len = 0;
int bad_character = 0;
int space = 1;
png_debug(1, "in png_check_keyword");
if (key == NULL)
{
*new_key = 0;
return 0;
}
while (*key && key_len < 79)
{
png_byte ch = (png_byte)*key++;
if ((ch > 32 && ch <= 126) || (ch >= 161 /*&& ch <= 255*/))
{
*new_key++ = ch; ++key_len; space = 0;
}
else if (space == 0)
{
/* A space or an invalid character when one wasn't seen immediately
* before; output just a space.
*/
*new_key++ = 32; ++key_len; space = 1;
/* If the character was not a space then it is invalid. */
if (ch != 32)
bad_character = ch;
}
else if (bad_character == 0)
bad_character = ch; /* just skip it, record the first error */
}
if (key_len > 0 && space != 0) /* trailing space */
{
--key_len; --new_key;
if (bad_character == 0)
bad_character = 32;
}
/* Terminate the keyword */
*new_key = 0;
if (key_len == 0)
return 0;
#ifdef PNG_WARNINGS_SUPPORTED
/* Try to only output one warning per keyword: */
if (*key != 0) /* keyword too long */
png_warning(png_ptr, "keyword truncated");
else if (bad_character != 0)
{
PNG_WARNING_PARAMETERS(p)
png_warning_parameter(p, 1, orig_key);
png_warning_parameter_signed(p, 2, PNG_NUMBER_FORMAT_02x, bad_character);
png_formatted_warning(png_ptr, p, "keyword \"@1\": bad character '0x@2'");
}
#else /* !WARNINGS */
PNG_UNUSED(png_ptr)
#endif /* !WARNINGS */
return key_len;
}
#endif /* TEXT || pCAL || iCCP || sPLT */
#endif /* READ || WRITE */ #endif /* READ || WRITE */

View File

@ -1,8 +1,8 @@
/* pngstruct.h - header file for PNG reference library /* pngstruct.h - header file for PNG reference library
* *
* Last changed in libpng 1.6.18 [July 23, 2015] * Last changed in libpng 1.6.32 [August 24, 2017]
* Copyright (c) 1998-2015 Glenn Randers-Pehrson * Copyright (c) 1998-2002,2004,2006-2017 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
* *
@ -27,7 +27,7 @@
/* We must ensure that zlib uses 'const' in declarations. */ /* We must ensure that zlib uses 'const' in declarations. */
# define ZLIB_CONST # define ZLIB_CONST
#endif #endif
#include "../zlib/zlib.h" #include "zlib.h"
#ifdef const #ifdef const
/* zlib.h sometimes #defines const to nothing, undo this. */ /* zlib.h sometimes #defines const to nothing, undo this. */
# undef const # undef const
@ -249,7 +249,7 @@ struct png_struct_def
png_byte filter; /* file filter type (always 0) */ png_byte filter; /* file filter type (always 0) */
png_byte interlaced; /* PNG_INTERLACE_NONE, PNG_INTERLACE_ADAM7 */ png_byte interlaced; /* PNG_INTERLACE_NONE, PNG_INTERLACE_ADAM7 */
png_byte pass; /* current interlace pass (0 - 6) */ png_byte pass; /* current interlace pass (0 - 6) */
png_byte do_filter; /* row filter flags (see PNG_FILTER_ below ) */ png_byte do_filter; /* row filter flags (see PNG_FILTER_ in png.h ) */
png_byte color_type; /* color type of file */ png_byte color_type; /* color type of file */
png_byte bit_depth; /* bit depth of file */ png_byte bit_depth; /* bit depth of file */
png_byte usr_bit_depth; /* bit depth of users row: write only */ png_byte usr_bit_depth; /* bit depth of users row: write only */
@ -263,6 +263,9 @@ struct png_struct_def
/* pixel depth used for the row buffers */ /* pixel depth used for the row buffers */
png_byte transformed_pixel_depth; png_byte transformed_pixel_depth;
/* pixel depth after read/write transforms */ /* pixel depth after read/write transforms */
#if ZLIB_VERNUM >= 0x1240
png_byte zstream_start; /* at start of an input zlib stream */
#endif /* Zlib >= 1.2.4 */
#if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED) #if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED)
png_uint_16 filler; /* filler bytes for pixel expansion */ png_uint_16 filler; /* filler bytes for pixel expansion */
#endif #endif
@ -350,7 +353,7 @@ struct png_struct_def
/* Options */ /* Options */
#ifdef PNG_SET_OPTION_SUPPORTED #ifdef PNG_SET_OPTION_SUPPORTED
png_byte options; /* On/off state (up to 4 options) */ png_uint_32 options; /* On/off state (up to 16 options) */
#endif #endif
#if PNG_LIBPNG_VER < 10700 #if PNG_LIBPNG_VER < 10700

View File

@ -1,8 +1,8 @@
/* pngtrans.c - transforms the data in a row (used by both readers and writers) /* pngtrans.c - transforms the data in a row (used by both readers and writers)
* *
* Last changed in libpng 1.6.18 [July 23, 2015] * Last changed in libpng 1.6.30 [June 28, 2017]
* Copyright (c) 1998-2015 Glenn Randers-Pehrson * Copyright (c) 1998-2002,2004,2006-2017 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
* *
@ -30,7 +30,7 @@ png_set_bgr(png_structrp png_ptr)
#endif #endif
#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED) #if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED)
/* Turn on 16 bit byte swapping */ /* Turn on 16-bit byte swapping */
void PNGAPI void PNGAPI
png_set_swap(png_structrp png_ptr) png_set_swap(png_structrp png_ptr)
{ {
@ -172,7 +172,8 @@ png_set_filler(png_structrp png_ptr, png_uint_32 filler, int filler_loc)
* size! * size!
*/ */
png_app_error(png_ptr, png_app_error(png_ptr,
"png_set_filler is invalid for low bit depth gray output"); "png_set_filler is invalid for"
" low bit depth gray output");
return; return;
} }
@ -313,7 +314,7 @@ png_do_invert(png_row_infop row_info, png_bytep row)
#ifdef PNG_16BIT_SUPPORTED #ifdef PNG_16BIT_SUPPORTED
#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED) #if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED)
/* Swaps byte order on 16 bit depth images */ /* Swaps byte order on 16-bit depth images */
void /* PRIVATE */ void /* PRIVATE */
png_do_swap(png_row_infop row_info, png_bytep row) png_do_swap(png_row_infop row_info, png_bytep row)
{ {
@ -513,11 +514,15 @@ png_do_strip_channel(png_row_infop row_info, png_bytep row, int at_start)
if (at_start != 0) /* Skip initial filler */ if (at_start != 0) /* Skip initial filler */
++sp; ++sp;
else /* Skip initial channel and, for sp, the filler */ else /* Skip initial channel and, for sp, the filler */
sp += 2, ++dp; {
sp += 2; ++dp;
}
/* For a 1 pixel wide image there is nothing to do */ /* For a 1 pixel wide image there is nothing to do */
while (sp < ep) while (sp < ep)
*dp++ = *sp, sp += 2; {
*dp++ = *sp; sp += 2;
}
row_info->pixel_depth = 8; row_info->pixel_depth = 8;
} }
@ -527,10 +532,14 @@ png_do_strip_channel(png_row_infop row_info, png_bytep row, int at_start)
if (at_start != 0) /* Skip initial filler */ if (at_start != 0) /* Skip initial filler */
sp += 2; sp += 2;
else /* Skip initial channel and, for sp, the filler */ else /* Skip initial channel and, for sp, the filler */
sp += 4, dp += 2; {
sp += 4; dp += 2;
}
while (sp < ep) while (sp < ep)
*dp++ = *sp++, *dp++ = *sp, sp += 3; {
*dp++ = *sp++; *dp++ = *sp; sp += 3;
}
row_info->pixel_depth = 16; row_info->pixel_depth = 16;
} }
@ -553,11 +562,15 @@ png_do_strip_channel(png_row_infop row_info, png_bytep row, int at_start)
if (at_start != 0) /* Skip initial filler */ if (at_start != 0) /* Skip initial filler */
++sp; ++sp;
else /* Skip initial channels and, for sp, the filler */ else /* Skip initial channels and, for sp, the filler */
sp += 4, dp += 3; {
sp += 4; dp += 3;
}
/* Note that the loop adds 3 to dp and 4 to sp each time. */ /* Note that the loop adds 3 to dp and 4 to sp each time. */
while (sp < ep) while (sp < ep)
*dp++ = *sp++, *dp++ = *sp++, *dp++ = *sp, sp += 2; {
*dp++ = *sp++; *dp++ = *sp++; *dp++ = *sp; sp += 2;
}
row_info->pixel_depth = 24; row_info->pixel_depth = 24;
} }
@ -567,14 +580,16 @@ png_do_strip_channel(png_row_infop row_info, png_bytep row, int at_start)
if (at_start != 0) /* Skip initial filler */ if (at_start != 0) /* Skip initial filler */
sp += 2; sp += 2;
else /* Skip initial channels and, for sp, the filler */ else /* Skip initial channels and, for sp, the filler */
sp += 8, dp += 6; {
sp += 8; dp += 6;
}
while (sp < ep) while (sp < ep)
{ {
/* Copy 6 bytes, skip 2 */ /* Copy 6 bytes, skip 2 */
*dp++ = *sp++, *dp++ = *sp++; *dp++ = *sp++; *dp++ = *sp++;
*dp++ = *sp++, *dp++ = *sp++; *dp++ = *sp++; *dp++ = *sp++;
*dp++ = *sp++, *dp++ = *sp, sp += 3; *dp++ = *sp++; *dp++ = *sp; sp += 3;
} }
row_info->pixel_depth = 48; row_info->pixel_depth = 48;
@ -594,7 +609,7 @@ png_do_strip_channel(png_row_infop row_info, png_bytep row, int at_start)
return; /* The filler channel has gone already */ return; /* The filler channel has gone already */
/* Fix the rowbytes value. */ /* Fix the rowbytes value. */
row_info->rowbytes = dp-row; row_info->rowbytes = (unsigned int)(dp-row);
} }
#endif #endif
@ -692,7 +707,7 @@ png_do_check_palette_indexes(png_structrp png_ptr, png_row_infop row_info)
* and this calculation is used because it avoids warnings that other * and this calculation is used because it avoids warnings that other
* forms produced on either GCC or MSVC. * forms produced on either GCC or MSVC.
*/ */
int padding = (-row_info->pixel_depth * row_info->width) & 7; int padding = PNG_PADBITS(row_info->pixel_depth, row_info->width);
png_bytep rp = png_ptr->row_buf + row_info->rowbytes; png_bytep rp = png_ptr->row_buf + row_info->rowbytes;
switch (row_info->bit_depth) switch (row_info->bit_depth)

View File

@ -1,8 +1,8 @@
/* pngwio.c - functions for data output /* pngwio.c - functions for data output
* *
* Last changed in libpng 1.6.15 [November 20, 2014] * Last changed in libpng 1.6.24 [August 4, 2016]
* Copyright (c) 1998-2014 Glenn Randers-Pehrson * Copyright (c) 1998-2002,2004,2006-2014,2016 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
* *
@ -26,7 +26,7 @@
* writes to a file pointer. Note that this routine sometimes gets called * writes to a file pointer. Note that this routine sometimes gets called
* with very small lengths, so you should implement some kind of simple * with very small lengths, so you should implement some kind of simple
* buffering if you are using unbuffered writes. This should never be asked * buffering if you are using unbuffered writes. This should never be asked
* to write more than 64K on a 16 bit machine. * to write more than 64K on a 16-bit machine.
*/ */
void /* PRIVATE */ void /* PRIVATE */

View File

@ -1,8 +1,8 @@
/* pngwrite.c - general routines to write a PNG file /* pngwrite.c - general routines to write a PNG file
* *
* Last changed in libpng 1.6.18 [July 23, 2015] * Last changed in libpng 1.6.32 [August 24, 2017]
* Copyright (c) 1998-2015 Glenn Randers-Pehrson * Copyright (c) 1998-2002,2004,2006-2017 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
* *
@ -12,9 +12,9 @@
*/ */
#include "pngpriv.h" #include "pngpriv.h"
#if defined(PNG_SIMPLIFIED_WRITE_SUPPORTED) && defined(PNG_STDIO_SUPPORTED) #ifdef PNG_SIMPLIFIED_WRITE_STDIO_SUPPORTED
# include <errno.h> # include <errno.h>
#endif #endif /* SIMPLIFIED_WRITE_STDIO */
#ifdef PNG_WRITE_SUPPORTED #ifdef PNG_WRITE_SUPPORTED
@ -206,7 +206,7 @@ png_write_info(png_structrp png_ptr, png_const_inforp info_ptr)
png_write_PLTE(png_ptr, info_ptr->palette, png_write_PLTE(png_ptr, info_ptr->palette,
(png_uint_32)info_ptr->num_palette); (png_uint_32)info_ptr->num_palette);
else if ((info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) !=0) else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
png_error(png_ptr, "Valid palette required for paletted images"); png_error(png_ptr, "Valid palette required for paletted images");
#ifdef PNG_WRITE_tRNS_SUPPORTED #ifdef PNG_WRITE_tRNS_SUPPORTED
@ -237,6 +237,11 @@ png_write_info(png_structrp png_ptr, png_const_inforp info_ptr)
png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type); png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
#endif #endif
#ifdef PNG_WRITE_eXIf_SUPPORTED
if ((info_ptr->valid & PNG_INFO_eXIf) != 0)
png_write_eXIf(png_ptr, info_ptr->exif, info_ptr->num_exif);
#endif
#ifdef PNG_WRITE_hIST_SUPPORTED #ifdef PNG_WRITE_hIST_SUPPORTED
if ((info_ptr->valid & PNG_INFO_hIST) != 0) if ((info_ptr->valid & PNG_INFO_hIST) != 0)
png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette); png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
@ -432,6 +437,12 @@ png_write_end(png_structrp png_ptr, png_inforp info_ptr)
} }
} }
#endif #endif
#ifdef PNG_WRITE_eXIf_SUPPORTED
if ((info_ptr->valid & PNG_INFO_eXIf) != 0)
png_write_eXIf(png_ptr, info_ptr->exif, info_ptr->num_exif);
#endif
#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
write_unknown_chunks(png_ptr, info_ptr, PNG_AFTER_IDAT); write_unknown_chunks(png_ptr, info_ptr, PNG_AFTER_IDAT);
#endif #endif
@ -666,9 +677,9 @@ png_do_write_intrapixel(png_row_infop row_info, png_bytep row)
for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
{ {
png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1); png_uint_32 s0 = (png_uint_32)(*(rp ) << 8) | *(rp + 1);
png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3); png_uint_32 s1 = (png_uint_32)(*(rp + 2) << 8) | *(rp + 3);
png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5); png_uint_32 s2 = (png_uint_32)(*(rp + 4) << 8) | *(rp + 5);
png_uint_32 red = (png_uint_32)((s0 - s1) & 0xffffL); png_uint_32 red = (png_uint_32)((s0 - s1) & 0xffffL);
png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL); png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL);
*(rp ) = (png_byte)(red >> 8); *(rp ) = (png_byte)(red >> 8);
@ -901,7 +912,7 @@ png_set_flush(png_structrp png_ptr, int nrows)
if (png_ptr == NULL) if (png_ptr == NULL)
return; return;
png_ptr->flush_dist = (nrows < 0 ? 0 : nrows); png_ptr->flush_dist = (nrows < 0 ? 0 : (png_uint_32)nrows);
} }
/* Flush the current output buffers now */ /* Flush the current output buffers now */
@ -1007,8 +1018,8 @@ png_set_filter(png_structrp png_ptr, int method, int filters)
case 5: case 5:
case 6: case 6:
case 7: png_app_error(png_ptr, "Unknown row filter for method 0"); case 7: png_app_error(png_ptr, "Unknown row filter for method 0");
/* FALL THROUGH */
#endif /* WRITE_FILTER */ #endif /* WRITE_FILTER */
/* FALLTHROUGH */
case PNG_FILTER_VALUE_NONE: case PNG_FILTER_VALUE_NONE:
png_ptr->do_filter = PNG_FILTER_NONE; break; png_ptr->do_filter = PNG_FILTER_NONE; break;
@ -1422,7 +1433,7 @@ png_write_png(png_structrp png_ptr, png_inforp info_ptr,
png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ENDIAN not supported"); png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ENDIAN not supported");
#endif #endif
/* Swap bits of 1, 2, 4 bit packed pixel formats */ /* Swap bits of 1-bit, 2-bit, 4-bit packed pixel formats */
if ((transforms & PNG_TRANSFORM_PACKSWAP) != 0) if ((transforms & PNG_TRANSFORM_PACKSWAP) != 0)
#ifdef PNG_WRITE_PACKSWAP_SUPPORTED #ifdef PNG_WRITE_PACKSWAP_SUPPORTED
png_set_packswap(png_ptr); png_set_packswap(png_ptr);
@ -1452,7 +1463,6 @@ png_write_png(png_structrp png_ptr, png_inforp info_ptr,
#ifdef PNG_SIMPLIFIED_WRITE_SUPPORTED #ifdef PNG_SIMPLIFIED_WRITE_SUPPORTED
# ifdef PNG_STDIO_SUPPORTED /* currently required for png_image_write_* */
/* Initialize the write structure - general purpose utility. */ /* Initialize the write structure - general purpose utility. */
static int static int
png_image_write_init(png_imagep image) png_image_write_init(png_imagep image)
@ -1504,6 +1514,10 @@ typedef struct
png_const_voidp first_row; png_const_voidp first_row;
ptrdiff_t row_bytes; ptrdiff_t row_bytes;
png_voidp local_row; png_voidp local_row;
/* Byte count for memory writing */
png_bytep memory;
png_alloc_size_t memory_bytes; /* not used for STDIO */
png_alloc_size_t output_bytes; /* running total */
} png_image_write_control; } png_image_write_control;
/* Write png_uint_16 input to a 16-bit PNG; the png_ptr has already been set to /* Write png_uint_16 input to a 16-bit PNG; the png_ptr has already been set to
@ -1522,7 +1536,8 @@ png_write_image_16bit(png_voidp argument)
display->first_row); display->first_row);
png_uint_16p output_row = png_voidcast(png_uint_16p, display->local_row); png_uint_16p output_row = png_voidcast(png_uint_16p, display->local_row);
png_uint_16p row_end; png_uint_16p row_end;
const int channels = (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ? 3 : 1; const unsigned int channels = (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ?
3 : 1;
int aindex = 0; int aindex = 0;
png_uint_32 y = image->height; png_uint_32 y = image->height;
@ -1536,9 +1551,9 @@ png_write_image_16bit(png_voidp argument)
++output_row; ++output_row;
} }
else else
aindex = channels; aindex = (int)channels;
# else # else
aindex = channels; aindex = (int)channels;
# endif # endif
} }
@ -1551,7 +1566,7 @@ png_write_image_16bit(png_voidp argument)
*/ */
row_end = output_row + image->width * (channels+1); row_end = output_row + image->width * (channels+1);
while (y-- > 0) for (; y > 0; --y)
{ {
png_const_uint_16p in_ptr = input_row; png_const_uint_16p in_ptr = input_row;
png_uint_16p out_ptr = output_row; png_uint_16p out_ptr = output_row;
@ -1572,7 +1587,7 @@ png_write_image_16bit(png_voidp argument)
if (alpha > 0 && alpha < 65535) if (alpha > 0 && alpha < 65535)
reciprocal = ((0xffff<<15)+(alpha>>1))/alpha; reciprocal = ((0xffff<<15)+(alpha>>1))/alpha;
c = channels; c = (int)channels;
do /* always at least one channel */ do /* always at least one channel */
{ {
png_uint_16 component = *in_ptr++; png_uint_16 component = *in_ptr++;
@ -1607,7 +1622,7 @@ png_write_image_16bit(png_voidp argument)
} }
png_write_row(png_ptr, png_voidcast(png_const_bytep, display->local_row)); png_write_row(png_ptr, png_voidcast(png_const_bytep, display->local_row));
input_row += display->row_bytes/(sizeof (png_uint_16)); input_row += (png_uint_16)display->row_bytes/(sizeof (png_uint_16));
} }
return 1; return 1;
@ -1680,7 +1695,8 @@ png_write_image_8bit(png_voidp argument)
display->first_row); display->first_row);
png_bytep output_row = png_voidcast(png_bytep, display->local_row); png_bytep output_row = png_voidcast(png_bytep, display->local_row);
png_uint_32 y = image->height; png_uint_32 y = image->height;
const int channels = (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ? 3 : 1; const unsigned int channels = (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ?
3 : 1;
if ((image->format & PNG_FORMAT_FLAG_ALPHA) != 0) if ((image->format & PNG_FORMAT_FLAG_ALPHA) != 0)
{ {
@ -1697,12 +1713,12 @@ png_write_image_8bit(png_voidp argument)
else else
# endif # endif
aindex = channels; aindex = (int)channels;
/* Use row_end in place of a loop counter: */ /* Use row_end in place of a loop counter: */
row_end = output_row + image->width * (channels+1); row_end = output_row + image->width * (channels+1);
while (y-- > 0) for (; y > 0; --y)
{ {
png_const_uint_16p in_ptr = input_row; png_const_uint_16p in_ptr = input_row;
png_bytep out_ptr = output_row; png_bytep out_ptr = output_row;
@ -1720,7 +1736,7 @@ png_write_image_8bit(png_voidp argument)
if (alphabyte > 0 && alphabyte < 255) if (alphabyte > 0 && alphabyte < 255)
reciprocal = UNP_RECIPROCAL(alpha); reciprocal = UNP_RECIPROCAL(alpha);
c = channels; c = (int)channels;
do /* always at least one channel */ do /* always at least one channel */
*out_ptr++ = png_unpremultiply(*in_ptr++, alpha, reciprocal); *out_ptr++ = png_unpremultiply(*in_ptr++, alpha, reciprocal);
while (--c > 0); while (--c > 0);
@ -1732,7 +1748,7 @@ png_write_image_8bit(png_voidp argument)
png_write_row(png_ptr, png_voidcast(png_const_bytep, png_write_row(png_ptr, png_voidcast(png_const_bytep,
display->local_row)); display->local_row));
input_row += display->row_bytes/(sizeof (png_uint_16)); input_row += (png_uint_16)display->row_bytes/(sizeof (png_uint_16));
} /* while y */ } /* while y */
} }
@ -1743,7 +1759,7 @@ png_write_image_8bit(png_voidp argument)
*/ */
png_bytep row_end = output_row + image->width * channels; png_bytep row_end = output_row + image->width * channels;
while (y-- > 0) for (; y > 0; --y)
{ {
png_const_uint_16p in_ptr = input_row; png_const_uint_16p in_ptr = input_row;
png_bytep out_ptr = output_row; png_bytep out_ptr = output_row;
@ -1757,7 +1773,7 @@ png_write_image_8bit(png_voidp argument)
} }
png_write_row(png_ptr, output_row); png_write_row(png_ptr, output_row);
input_row += display->row_bytes/(sizeof (png_uint_16)); input_row += (png_uint_16)display->row_bytes/(sizeof (png_uint_16));
} }
} }
@ -1774,7 +1790,7 @@ png_image_set_PLTE(png_image_write_control *display)
/* NOTE: the caller must check for cmap != NULL and entries != 0 */ /* NOTE: the caller must check for cmap != NULL and entries != 0 */
const png_uint_32 format = image->format; const png_uint_32 format = image->format;
const int channels = PNG_IMAGE_SAMPLE_CHANNELS(format); const unsigned int channels = PNG_IMAGE_SAMPLE_CHANNELS(format);
# if defined(PNG_FORMAT_BGR_SUPPORTED) &&\ # if defined(PNG_FORMAT_BGR_SUPPORTED) &&\
defined(PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED) defined(PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED)
@ -1806,7 +1822,7 @@ png_image_set_PLTE(png_image_write_control *display)
{ {
png_const_uint_16p entry = png_voidcast(png_const_uint_16p, cmap); png_const_uint_16p entry = png_voidcast(png_const_uint_16p, cmap);
entry += i * channels; entry += (unsigned int)i * channels;
if ((channels & 1) != 0) /* no alpha */ if ((channels & 1) != 0) /* no alpha */
{ {
@ -1862,7 +1878,7 @@ png_image_set_PLTE(png_image_write_control *display)
{ {
png_const_bytep entry = png_voidcast(png_const_bytep, cmap); png_const_bytep entry = png_voidcast(png_const_bytep, cmap);
entry += i * channels; entry += (unsigned int)i * channels;
switch (channels) switch (channels)
{ {
@ -1870,7 +1886,7 @@ png_image_set_PLTE(png_image_write_control *display)
tRNS[i] = entry[afirst ? 0 : 3]; tRNS[i] = entry[afirst ? 0 : 3];
if (tRNS[i] < 255) if (tRNS[i] < 255)
num_trans = i+1; num_trans = i+1;
/* FALL THROUGH */ /* FALLTHROUGH */
case 3: case 3:
palette[i].blue = entry[afirst + (2 ^ bgr)]; palette[i].blue = entry[afirst + (2 ^ bgr)];
palette[i].green = entry[afirst + 1]; palette[i].green = entry[afirst + 1];
@ -1881,7 +1897,7 @@ png_image_set_PLTE(png_image_write_control *display)
tRNS[i] = entry[1 ^ afirst]; tRNS[i] = entry[1 ^ afirst];
if (tRNS[i] < 255) if (tRNS[i] < 255)
num_trans = i+1; num_trans = i+1;
/* FALL THROUGH */ /* FALLTHROUGH */
case 1: case 1:
palette[i].blue = palette[i].red = palette[i].green = palette[i].blue = palette[i].red = palette[i].green =
entry[afirst]; entry[afirst];
@ -1907,7 +1923,7 @@ png_image_set_PLTE(png_image_write_control *display)
png_set_tRNS(image->opaque->png_ptr, image->opaque->info_ptr, tRNS, png_set_tRNS(image->opaque->png_ptr, image->opaque->info_ptr, tRNS,
num_trans, NULL); num_trans, NULL);
image->colormap_entries = entries; image->colormap_entries = (png_uint_32)entries;
} }
static int static int
@ -1931,9 +1947,43 @@ png_image_write_main(png_voidp argument)
png_set_benign_errors(png_ptr, 0/*error*/); png_set_benign_errors(png_ptr, 0/*error*/);
# endif # endif
/* Default the 'row_stride' parameter if required. */ /* Default the 'row_stride' parameter if required, also check the row stride
* and total image size to ensure that they are within the system limits.
*/
{
const unsigned int channels = PNG_IMAGE_PIXEL_CHANNELS(image->format);
if (image->width <= 0x7fffffffU/channels) /* no overflow */
{
png_uint_32 check;
const png_uint_32 png_row_stride = image->width * channels;
if (display->row_stride == 0) if (display->row_stride == 0)
display->row_stride = PNG_IMAGE_ROW_STRIDE(*image); display->row_stride = (png_int_32)/*SAFE*/png_row_stride;
if (display->row_stride < 0)
check = (png_uint_32)(-display->row_stride);
else
check = (png_uint_32)display->row_stride;
if (check >= png_row_stride)
{
/* Now check for overflow of the image buffer calculation; this
* limits the whole image size to 32 bits for API compatibility with
* the current, 32-bit, PNG_IMAGE_BUFFER_SIZE macro.
*/
if (image->height > 0xffffffffU/png_row_stride)
png_error(image->opaque->png_ptr, "memory image too large");
}
else
png_error(image->opaque->png_ptr, "supplied row stride too small");
}
else
png_error(image->opaque->png_ptr, "image row stride too large");
}
/* Set the required transforms then write the rows in the correct order. */ /* Set the required transforms then write the rows in the correct order. */
if ((format & PNG_FORMAT_FLAG_COLORMAP) != 0) if ((format & PNG_FORMAT_FLAG_COLORMAP) != 0)
@ -1998,7 +2048,7 @@ png_image_write_main(png_voidp argument)
/* Now set up the data transformations (*after* the header is written), /* Now set up the data transformations (*after* the header is written),
* remove the handled transformations from the 'format' flags for checking. * remove the handled transformations from the 'format' flags for checking.
* *
* First check for a little endian system if writing 16 bit files. * First check for a little endian system if writing 16-bit files.
*/ */
if (write_16bit != 0) if (write_16bit != 0)
{ {
@ -2099,7 +2149,7 @@ png_image_write_main(png_voidp argument)
ptrdiff_t row_bytes = display->row_bytes; ptrdiff_t row_bytes = display->row_bytes;
png_uint_32 y = image->height; png_uint_32 y = image->height;
while (y-- > 0) for (; y > 0; --y)
{ {
png_write_row(png_ptr, row); png_write_row(png_ptr, row);
row += row_bytes; row += row_bytes;
@ -2110,6 +2160,122 @@ png_image_write_main(png_voidp argument)
return 1; return 1;
} }
static void (PNGCBAPI
image_memory_write)(png_structp png_ptr, png_bytep/*const*/ data,
png_size_t size)
{
png_image_write_control *display = png_voidcast(png_image_write_control*,
png_ptr->io_ptr/*backdoor: png_get_io_ptr(png_ptr)*/);
const png_alloc_size_t ob = display->output_bytes;
/* Check for overflow; this should never happen: */
if (size <= ((png_alloc_size_t)-1) - ob)
{
/* I don't think libpng ever does this, but just in case: */
if (size > 0)
{
if (display->memory_bytes >= ob+size) /* writing */
memcpy(display->memory+ob, data, size);
/* Always update the size: */
display->output_bytes = ob+size;
}
}
else
png_error(png_ptr, "png_image_write_to_memory: PNG too big");
}
static void (PNGCBAPI
image_memory_flush)(png_structp png_ptr)
{
PNG_UNUSED(png_ptr)
}
static int
png_image_write_memory(png_voidp argument)
{
png_image_write_control *display = png_voidcast(png_image_write_control*,
argument);
/* The rest of the memory-specific init and write_main in an error protected
* environment. This case needs to use callbacks for the write operations
* since libpng has no built in support for writing to memory.
*/
png_set_write_fn(display->image->opaque->png_ptr, display/*io_ptr*/,
image_memory_write, image_memory_flush);
return png_image_write_main(display);
}
int PNGAPI
png_image_write_to_memory(png_imagep image, void *memory,
png_alloc_size_t * PNG_RESTRICT memory_bytes, int convert_to_8bit,
const void *buffer, png_int_32 row_stride, const void *colormap)
{
/* Write the image to the given buffer, or count the bytes if it is NULL */
if (image != NULL && image->version == PNG_IMAGE_VERSION)
{
if (memory_bytes != NULL && buffer != NULL)
{
/* This is to give the caller an easier error detection in the NULL
* case and guard against uninitialized variable problems:
*/
if (memory == NULL)
*memory_bytes = 0;
if (png_image_write_init(image) != 0)
{
png_image_write_control display;
int result;
memset(&display, 0, (sizeof display));
display.image = image;
display.buffer = buffer;
display.row_stride = row_stride;
display.colormap = colormap;
display.convert_to_8bit = convert_to_8bit;
display.memory = png_voidcast(png_bytep, memory);
display.memory_bytes = *memory_bytes;
display.output_bytes = 0;
result = png_safe_execute(image, png_image_write_memory, &display);
png_image_free(image);
/* write_memory returns true even if we ran out of buffer. */
if (result)
{
/* On out-of-buffer this function returns '0' but still updates
* memory_bytes:
*/
if (memory != NULL && display.output_bytes > *memory_bytes)
result = 0;
*memory_bytes = display.output_bytes;
}
return result;
}
else
return 0;
}
else
return png_image_error(image,
"png_image_write_to_memory: invalid argument");
}
else if (image != NULL)
return png_image_error(image,
"png_image_write_to_memory: incorrect PNG_IMAGE_VERSION");
else
return 0;
}
#ifdef PNG_SIMPLIFIED_WRITE_STDIO_SUPPORTED
int PNGAPI int PNGAPI
png_image_write_to_stdio(png_imagep image, FILE *file, int convert_to_8bit, png_image_write_to_stdio(png_imagep image, FILE *file, int convert_to_8bit,
const void *buffer, png_int_32 row_stride, const void *colormap) const void *buffer, png_int_32 row_stride, const void *colormap)
@ -2117,7 +2283,7 @@ png_image_write_to_stdio(png_imagep image, FILE *file, int convert_to_8bit,
/* Write the image to the given (FILE*). */ /* Write the image to the given (FILE*). */
if (image != NULL && image->version == PNG_IMAGE_VERSION) if (image != NULL && image->version == PNG_IMAGE_VERSION)
{ {
if (file != NULL) if (file != NULL && buffer != NULL)
{ {
if (png_image_write_init(image) != 0) if (png_image_write_init(image) != 0)
{ {
@ -2167,7 +2333,7 @@ png_image_write_to_file(png_imagep image, const char *file_name,
/* Write the image to the named file. */ /* Write the image to the named file. */
if (image != NULL && image->version == PNG_IMAGE_VERSION) if (image != NULL && image->version == PNG_IMAGE_VERSION)
{ {
if (file_name != NULL) if (file_name != NULL && buffer != NULL)
{ {
FILE *fp = fopen(file_name, "wb"); FILE *fp = fopen(file_name, "wb");
@ -2225,6 +2391,6 @@ png_image_write_to_file(png_imagep image, const char *file_name,
else else
return 0; return 0;
} }
# endif /* STDIO */ #endif /* SIMPLIFIED_WRITE_STDIO */
#endif /* SIMPLIFIED_WRITE */ #endif /* SIMPLIFIED_WRITE */
#endif /* WRITE */ #endif /* WRITE */

View File

@ -1,8 +1,8 @@
/* pngwtran.c - transforms the data in a row for PNG writers /* pngwtran.c - transforms the data in a row for PNG writers
* *
* Last changed in libpng 1.6.18 [July 23, 2015] * Last changed in libpng 1.6.26 [October 20, 2016]
* Copyright (c) 1998-2015 Glenn Randers-Pehrson * Copyright (c) 1998-2002,2004,2006-2016 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
* *
@ -177,7 +177,7 @@ png_do_shift(png_row_infop row_info, png_bytep row,
if (row_info->color_type != PNG_COLOR_TYPE_PALETTE) if (row_info->color_type != PNG_COLOR_TYPE_PALETTE)
{ {
int shift_start[4], shift_dec[4]; int shift_start[4], shift_dec[4];
int channels = 0; unsigned int channels = 0;
if ((row_info->color_type & PNG_COLOR_MASK_COLOR) != 0) if ((row_info->color_type & PNG_COLOR_MASK_COLOR) != 0)
{ {

View File

@ -1,8 +1,8 @@
/* pngwutil.c - utilities to write a PNG file /* pngwutil.c - utilities to write a PNG file
* *
* Last changed in libpng 1.6.18 [July 23, 2015] * Last changed in libpng 1.6.32 [August 24, 2017]
* Copyright (c) 1998-2015 Glenn Randers-Pehrson * Copyright (c) 1998-2002,2004,2006-2017 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
* *
@ -23,10 +23,10 @@
void PNGAPI void PNGAPI
png_save_uint_32(png_bytep buf, png_uint_32 i) png_save_uint_32(png_bytep buf, png_uint_32 i)
{ {
buf[0] = (png_byte)(i >> 24); buf[0] = (png_byte)((i >> 24) & 0xffU);
buf[1] = (png_byte)(i >> 16); buf[1] = (png_byte)((i >> 16) & 0xffU);
buf[2] = (png_byte)(i >> 8); buf[2] = (png_byte)((i >> 8) & 0xffU);
buf[3] = (png_byte)(i ); buf[3] = (png_byte)( i & 0xffU);
} }
/* Place a 16-bit number into a buffer in PNG byte order. /* Place a 16-bit number into a buffer in PNG byte order.
@ -36,8 +36,8 @@ png_save_uint_32(png_bytep buf, png_uint_32 i)
void PNGAPI void PNGAPI
png_save_uint_16(png_bytep buf, unsigned int i) png_save_uint_16(png_bytep buf, unsigned int i)
{ {
buf[0] = (png_byte)(i >> 8); buf[0] = (png_byte)((i >> 8) & 0xffU);
buf[1] = (png_byte)(i ); buf[1] = (png_byte)( i & 0xffU);
} }
#endif #endif
@ -179,7 +179,7 @@ png_write_complete_chunk(png_structrp png_ptr, png_uint_32 chunk_name,
if (png_ptr == NULL) if (png_ptr == NULL)
return; return;
/* On 64 bit architectures 'length' may not fit in a png_uint_32. */ /* On 64-bit architectures 'length' may not fit in a png_uint_32. */
if (length > PNG_UINT_31_MAX) if (length > PNG_UINT_31_MAX)
png_error(png_ptr, "length exceeds PNG maximum"); png_error(png_ptr, "length exceeds PNG maximum");
@ -408,7 +408,7 @@ png_deflate_claim(png_structrp png_ptr, png_uint_32 owner,
png_ptr->zstream.avail_out = 0; png_ptr->zstream.avail_out = 0;
/* Now initialize if required, setting the new parameters, otherwise just /* Now initialize if required, setting the new parameters, otherwise just
* to a simple reset to the previous parameters. * do a simple reset to the previous parameters.
*/ */
if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0) if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0)
ret = deflateReset(&png_ptr->zstream); ret = deflateReset(&png_ptr->zstream);
@ -665,90 +665,6 @@ png_write_compressed_data_out(png_structrp png_ptr, compression_state *comp)
} }
#endif /* WRITE_COMPRESSED_TEXT */ #endif /* WRITE_COMPRESSED_TEXT */
#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
* and if invalid, correct the keyword rather than discarding the entire
* chunk. The PNG 1.0 specification requires keywords 1-79 characters in
* length, forbids leading or trailing whitespace, multiple internal spaces,
* and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
*
* The 'new_key' buffer must be 80 characters in size (for the keyword plus a
* trailing '\0'). If this routine returns 0 then there was no keyword, or a
* valid one could not be generated, and the caller must png_error.
*/
static png_uint_32
png_check_keyword(png_structrp png_ptr, png_const_charp key, png_bytep new_key)
{
png_const_charp orig_key = key;
png_uint_32 key_len = 0;
int bad_character = 0;
int space = 1;
png_debug(1, "in png_check_keyword");
if (key == NULL)
{
*new_key = 0;
return 0;
}
while (*key && key_len < 79)
{
png_byte ch = (png_byte)*key++;
if ((ch > 32 && ch <= 126) || (ch >= 161 /*&& ch <= 255*/))
*new_key++ = ch, ++key_len, space = 0;
else if (space == 0)
{
/* A space or an invalid character when one wasn't seen immediately
* before; output just a space.
*/
*new_key++ = 32, ++key_len, space = 1;
/* If the character was not a space then it is invalid. */
if (ch != 32)
bad_character = ch;
}
else if (bad_character == 0)
bad_character = ch; /* just skip it, record the first error */
}
if (key_len > 0 && space != 0) /* trailing space */
{
--key_len, --new_key;
if (bad_character == 0)
bad_character = 32;
}
/* Terminate the keyword */
*new_key = 0;
if (key_len == 0)
return 0;
#ifdef PNG_WARNINGS_SUPPORTED
/* Try to only output one warning per keyword: */
if (*key != 0) /* keyword too long */
png_warning(png_ptr, "keyword truncated");
else if (bad_character != 0)
{
PNG_WARNING_PARAMETERS(p)
png_warning_parameter(p, 1, orig_key);
png_warning_parameter_signed(p, 2, PNG_NUMBER_FORMAT_02x, bad_character);
png_formatted_warning(png_ptr, p, "keyword \"@1\": bad character '0x@2'");
}
#endif /* WARNINGS */
return key_len;
}
#endif /* WRITE_TEXT || WRITE_pCAL || WRITE_iCCP || WRITE_sPLT */
/* Write the IHDR chunk, and update the png_struct with the necessary /* Write the IHDR chunk, and update the png_struct with the necessary
* information. Note that the rest of this code depends upon this * information. Note that the rest of this code depends upon this
* information being correct. * information being correct.
@ -759,6 +675,7 @@ png_write_IHDR(png_structrp png_ptr, png_uint_32 width, png_uint_32 height,
int interlace_type) int interlace_type)
{ {
png_byte buf[13]; /* Buffer to store the IHDR info */ png_byte buf[13]; /* Buffer to store the IHDR info */
int is_invalid_depth;
png_debug(1, "in png_write_IHDR"); png_debug(1, "in png_write_IHDR");
@ -784,11 +701,11 @@ png_write_IHDR(png_structrp png_ptr, png_uint_32 width, png_uint_32 height,
break; break;
case PNG_COLOR_TYPE_RGB: case PNG_COLOR_TYPE_RGB:
is_invalid_depth = (bit_depth != 8);
#ifdef PNG_WRITE_16BIT_SUPPORTED #ifdef PNG_WRITE_16BIT_SUPPORTED
if (bit_depth != 8 && bit_depth != 16) is_invalid_depth = (is_invalid_depth && bit_depth != 16);
#else
if (bit_depth != 8)
#endif #endif
if (is_invalid_depth)
png_error(png_ptr, "Invalid bit depth for RGB image"); png_error(png_ptr, "Invalid bit depth for RGB image");
png_ptr->channels = 3; png_ptr->channels = 3;
@ -810,18 +727,22 @@ png_write_IHDR(png_structrp png_ptr, png_uint_32 width, png_uint_32 height,
break; break;
case PNG_COLOR_TYPE_GRAY_ALPHA: case PNG_COLOR_TYPE_GRAY_ALPHA:
if (bit_depth != 8 && bit_depth != 16) is_invalid_depth = (bit_depth != 8);
#ifdef PNG_WRITE_16BIT_SUPPORTED
is_invalid_depth = (is_invalid_depth && bit_depth != 16);
#endif
if (is_invalid_depth)
png_error(png_ptr, "Invalid bit depth for grayscale+alpha image"); png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
png_ptr->channels = 2; png_ptr->channels = 2;
break; break;
case PNG_COLOR_TYPE_RGB_ALPHA: case PNG_COLOR_TYPE_RGB_ALPHA:
is_invalid_depth = (bit_depth != 8);
#ifdef PNG_WRITE_16BIT_SUPPORTED #ifdef PNG_WRITE_16BIT_SUPPORTED
if (bit_depth != 8 && bit_depth != 16) is_invalid_depth = (is_invalid_depth && bit_depth != 16);
#else
if (bit_depth != 8)
#endif #endif
if (is_invalid_depth)
png_error(png_ptr, "Invalid bit depth for RGBA image"); png_error(png_ptr, "Invalid bit depth for RGBA image");
png_ptr->channels = 4; png_ptr->channels = 4;
@ -922,17 +843,20 @@ void /* PRIVATE */
png_write_PLTE(png_structrp png_ptr, png_const_colorp palette, png_write_PLTE(png_structrp png_ptr, png_const_colorp palette,
png_uint_32 num_pal) png_uint_32 num_pal)
{ {
png_uint_32 i; png_uint_32 max_palette_length, i;
png_const_colorp pal_ptr; png_const_colorp pal_ptr;
png_byte buf[3]; png_byte buf[3];
png_debug(1, "in png_write_PLTE"); png_debug(1, "in png_write_PLTE");
max_palette_length = (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ?
(1 << png_ptr->bit_depth) : PNG_MAX_PALETTE_LENGTH;
if (( if ((
#ifdef PNG_MNG_FEATURES_SUPPORTED #ifdef PNG_MNG_FEATURES_SUPPORTED
(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) == 0 && (png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) == 0 &&
#endif #endif
num_pal == 0) || num_pal > 256) num_pal == 0) || num_pal > max_palette_length)
{ {
if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
{ {
@ -1079,6 +1003,7 @@ png_compress_IDAT(png_structrp png_ptr, png_const_bytep input,
optimize_cmf(data, png_image_size(png_ptr)); optimize_cmf(data, png_image_size(png_ptr));
#endif #endif
if (size > 0)
png_write_complete_chunk(png_ptr, png_IDAT, data, size); png_write_complete_chunk(png_ptr, png_IDAT, data, size);
png_ptr->mode |= PNG_HAVE_IDAT; png_ptr->mode |= PNG_HAVE_IDAT;
@ -1125,6 +1050,7 @@ png_compress_IDAT(png_structrp png_ptr, png_const_bytep input,
optimize_cmf(data, png_image_size(png_ptr)); optimize_cmf(data, png_image_size(png_ptr));
#endif #endif
if (size > 0)
png_write_complete_chunk(png_ptr, png_IDAT, data, size); png_write_complete_chunk(png_ptr, png_IDAT, data, size);
png_ptr->zstream.avail_out = 0; png_ptr->zstream.avail_out = 0;
png_ptr->zstream.next_out = NULL; png_ptr->zstream.next_out = NULL;
@ -1257,7 +1183,7 @@ png_write_sPLT(png_structrp png_ptr, png_const_sPLT_tp spalette)
png_byte new_name[80]; png_byte new_name[80];
png_byte entrybuf[10]; png_byte entrybuf[10];
png_size_t entry_size = (spalette->depth == 8 ? 6 : 10); png_size_t entry_size = (spalette->depth == 8 ? 6 : 10);
png_size_t palette_size = entry_size * spalette->nentries; png_size_t palette_size = entry_size * (png_size_t)spalette->nentries;
png_sPLT_entryp ep; png_sPLT_entryp ep;
#ifndef PNG_POINTER_INDEXING_SUPPORTED #ifndef PNG_POINTER_INDEXING_SUPPORTED
int i; int i;
@ -1444,7 +1370,7 @@ png_write_tRNS(png_structrp png_ptr, png_const_bytep trans_alpha,
else if (color_type == PNG_COLOR_TYPE_GRAY) else if (color_type == PNG_COLOR_TYPE_GRAY)
{ {
/* One 16 bit value */ /* One 16-bit value */
if (tran->gray >= (1 << png_ptr->bit_depth)) if (tran->gray >= (1 << png_ptr->bit_depth))
{ {
png_app_warning(png_ptr, png_app_warning(png_ptr,
@ -1459,7 +1385,7 @@ png_write_tRNS(png_structrp png_ptr, png_const_bytep trans_alpha,
else if (color_type == PNG_COLOR_TYPE_RGB) else if (color_type == PNG_COLOR_TYPE_RGB)
{ {
/* Three 16 bit values */ /* Three 16-bit values */
png_save_uint_16(buf, tran->red); png_save_uint_16(buf, tran->red);
png_save_uint_16(buf + 2, tran->green); png_save_uint_16(buf + 2, tran->green);
png_save_uint_16(buf + 4, tran->blue); png_save_uint_16(buf + 4, tran->blue);
@ -1522,7 +1448,8 @@ png_write_bKGD(png_structrp png_ptr, png_const_color_16p back, int color_type)
#endif #endif
{ {
png_warning(png_ptr, png_warning(png_ptr,
"Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8"); "Ignoring attempt to write 16-bit bKGD chunk "
"when bit_depth is 8");
return; return;
} }
@ -1546,6 +1473,28 @@ png_write_bKGD(png_structrp png_ptr, png_const_color_16p back, int color_type)
} }
#endif #endif
#ifdef PNG_WRITE_eXIf_SUPPORTED
/* Write the Exif data */
void /* PRIVATE */
png_write_eXIf(png_structrp png_ptr, png_bytep exif, int num_exif)
{
int i;
png_byte buf[1];
png_debug(1, "in png_write_eXIf");
png_write_chunk_header(png_ptr, png_eXIf, (png_uint_32)(num_exif));
for (i = 0; i < num_exif; i++)
{
buf[0] = exif[i];
png_write_chunk_data(png_ptr, buf, (png_size_t)1);
}
png_write_chunk_end(png_ptr);
}
#endif
#ifdef PNG_WRITE_hIST_SUPPORTED #ifdef PNG_WRITE_hIST_SUPPORTED
/* Write the histogram */ /* Write the histogram */
void /* PRIVATE */ void /* PRIVATE */
@ -1823,7 +1772,7 @@ png_write_pCAL(png_structrp png_ptr, png_charp purpose, png_int_32 X0,
total_len = purpose_len + units_len + 10; total_len = purpose_len + units_len + 10;
params_len = (png_size_tp)png_malloc(png_ptr, params_len = (png_size_tp)png_malloc(png_ptr,
(png_alloc_size_t)(nparams * (sizeof (png_size_t)))); (png_alloc_size_t)((png_alloc_size_t)nparams * (sizeof (png_size_t))));
/* Find the length of each parameter, making sure we don't count the /* Find the length of each parameter, making sure we don't count the
* null terminator for the last parameter. * null terminator for the last parameter.
@ -2335,7 +2284,7 @@ png_setup_sub_row(png_structrp png_ptr, const png_uint_32 bpp,
png_bytep rp, dp, lp; png_bytep rp, dp, lp;
png_size_t i; png_size_t i;
png_size_t sum = 0; png_size_t sum = 0;
int v; unsigned int v;
png_ptr->try_row[0] = PNG_FILTER_VALUE_SUB; png_ptr->try_row[0] = PNG_FILTER_VALUE_SUB;
@ -2343,14 +2292,22 @@ png_setup_sub_row(png_structrp png_ptr, const png_uint_32 bpp,
i++, rp++, dp++) i++, rp++, dp++)
{ {
v = *dp = *rp; v = *dp = *rp;
#ifdef PNG_USE_ABS
sum += 128 - abs((int)v - 128);
#else
sum += (v < 128) ? v : 256 - v; sum += (v < 128) ? v : 256 - v;
#endif
} }
for (lp = png_ptr->row_buf + 1; i < row_bytes; for (lp = png_ptr->row_buf + 1; i < row_bytes;
i++, rp++, lp++, dp++) i++, rp++, lp++, dp++)
{ {
v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff); v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
#ifdef PNG_USE_ABS
sum += 128 - abs((int)v - 128);
#else
sum += (v < 128) ? v : 256 - v; sum += (v < 128) ? v : 256 - v;
#endif
if (sum > lmins) /* We are already worse, don't continue. */ if (sum > lmins) /* We are already worse, don't continue. */
break; break;
@ -2359,6 +2316,28 @@ png_setup_sub_row(png_structrp png_ptr, const png_uint_32 bpp,
return (sum); return (sum);
} }
static void /* PRIVATE */
png_setup_sub_row_only(png_structrp png_ptr, const png_uint_32 bpp,
const png_size_t row_bytes)
{
png_bytep rp, dp, lp;
png_size_t i;
png_ptr->try_row[0] = PNG_FILTER_VALUE_SUB;
for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1; i < bpp;
i++, rp++, dp++)
{
*dp = *rp;
}
for (lp = png_ptr->row_buf + 1; i < row_bytes;
i++, rp++, lp++, dp++)
{
*dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
}
}
static png_size_t /* PRIVATE */ static png_size_t /* PRIVATE */
png_setup_up_row(png_structrp png_ptr, const png_size_t row_bytes, png_setup_up_row(png_structrp png_ptr, const png_size_t row_bytes,
const png_size_t lmins) const png_size_t lmins)
@ -2366,7 +2345,7 @@ png_setup_up_row(png_structrp png_ptr, const png_size_t row_bytes,
png_bytep rp, dp, pp; png_bytep rp, dp, pp;
png_size_t i; png_size_t i;
png_size_t sum = 0; png_size_t sum = 0;
int v; unsigned int v;
png_ptr->try_row[0] = PNG_FILTER_VALUE_UP; png_ptr->try_row[0] = PNG_FILTER_VALUE_UP;
@ -2375,7 +2354,11 @@ png_setup_up_row(png_structrp png_ptr, const png_size_t row_bytes,
i++, rp++, pp++, dp++) i++, rp++, pp++, dp++)
{ {
v = *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff); v = *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
#ifdef PNG_USE_ABS
sum += 128 - abs((int)v - 128);
#else
sum += (v < 128) ? v : 256 - v; sum += (v < 128) ? v : 256 - v;
#endif
if (sum > lmins) /* We are already worse, don't continue. */ if (sum > lmins) /* We are already worse, don't continue. */
break; break;
@ -2383,6 +2366,21 @@ png_setup_up_row(png_structrp png_ptr, const png_size_t row_bytes,
return (sum); return (sum);
} }
static void /* PRIVATE */
png_setup_up_row_only(png_structrp png_ptr, const png_size_t row_bytes)
{
png_bytep rp, dp, pp;
png_size_t i;
png_ptr->try_row[0] = PNG_FILTER_VALUE_UP;
for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1,
pp = png_ptr->prev_row + 1; i < row_bytes;
i++, rp++, pp++, dp++)
{
*dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
}
}
static png_size_t /* PRIVATE */ static png_size_t /* PRIVATE */
png_setup_avg_row(png_structrp png_ptr, const png_uint_32 bpp, png_setup_avg_row(png_structrp png_ptr, const png_uint_32 bpp,
@ -2391,7 +2389,7 @@ png_setup_avg_row(png_structrp png_ptr, const png_uint_32 bpp,
png_bytep rp, dp, pp, lp; png_bytep rp, dp, pp, lp;
png_uint_32 i; png_uint_32 i;
png_size_t sum = 0; png_size_t sum = 0;
int v; unsigned int v;
png_ptr->try_row[0] = PNG_FILTER_VALUE_AVG; png_ptr->try_row[0] = PNG_FILTER_VALUE_AVG;
@ -2400,7 +2398,11 @@ png_setup_avg_row(png_structrp png_ptr, const png_uint_32 bpp,
{ {
v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff); v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
#ifdef PNG_USE_ABS
sum += 128 - abs((int)v - 128);
#else
sum += (v < 128) ? v : 256 - v; sum += (v < 128) ? v : 256 - v;
#endif
} }
for (lp = png_ptr->row_buf + 1; i < row_bytes; i++) for (lp = png_ptr->row_buf + 1; i < row_bytes; i++)
@ -2408,7 +2410,11 @@ png_setup_avg_row(png_structrp png_ptr, const png_uint_32 bpp,
v = *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) v = *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
& 0xff); & 0xff);
#ifdef PNG_USE_ABS
sum += 128 - abs((int)v - 128);
#else
sum += (v < 128) ? v : 256 - v; sum += (v < 128) ? v : 256 - v;
#endif
if (sum > lmins) /* We are already worse, don't continue. */ if (sum > lmins) /* We are already worse, don't continue. */
break; break;
@ -2416,6 +2422,27 @@ png_setup_avg_row(png_structrp png_ptr, const png_uint_32 bpp,
return (sum); return (sum);
} }
static void /* PRIVATE */
png_setup_avg_row_only(png_structrp png_ptr, const png_uint_32 bpp,
const png_size_t row_bytes)
{
png_bytep rp, dp, pp, lp;
png_uint_32 i;
png_ptr->try_row[0] = PNG_FILTER_VALUE_AVG;
for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1,
pp = png_ptr->prev_row + 1; i < bpp; i++)
{
*dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
}
for (lp = png_ptr->row_buf + 1; i < row_bytes; i++)
{
*dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
& 0xff);
}
}
static png_size_t /* PRIVATE */ static png_size_t /* PRIVATE */
png_setup_paeth_row(png_structrp png_ptr, const png_uint_32 bpp, png_setup_paeth_row(png_structrp png_ptr, const png_uint_32 bpp,
@ -2424,7 +2451,7 @@ png_setup_paeth_row(png_structrp png_ptr, const png_uint_32 bpp,
png_bytep rp, dp, pp, cp, lp; png_bytep rp, dp, pp, cp, lp;
png_size_t i; png_size_t i;
png_size_t sum = 0; png_size_t sum = 0;
int v; unsigned int v;
png_ptr->try_row[0] = PNG_FILTER_VALUE_PAETH; png_ptr->try_row[0] = PNG_FILTER_VALUE_PAETH;
@ -2433,7 +2460,11 @@ png_setup_paeth_row(png_structrp png_ptr, const png_uint_32 bpp,
{ {
v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff); v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
#ifdef PNG_USE_ABS
sum += 128 - abs((int)v - 128);
#else
sum += (v < 128) ? v : 256 - v; sum += (v < 128) ? v : 256 - v;
#endif
} }
for (lp = png_ptr->row_buf + 1, cp = png_ptr->prev_row + 1; i < row_bytes; for (lp = png_ptr->row_buf + 1, cp = png_ptr->prev_row + 1; i < row_bytes;
@ -2462,7 +2493,11 @@ png_setup_paeth_row(png_structrp png_ptr, const png_uint_32 bpp,
v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff); v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
#ifdef PNG_USE_ABS
sum += 128 - abs((int)v - 128);
#else
sum += (v < 128) ? v : 256 - v; sum += (v < 128) ? v : 256 - v;
#endif
if (sum > lmins) /* We are already worse, don't continue. */ if (sum > lmins) /* We are already worse, don't continue. */
break; break;
@ -2470,6 +2505,48 @@ png_setup_paeth_row(png_structrp png_ptr, const png_uint_32 bpp,
return (sum); return (sum);
} }
static void /* PRIVATE */
png_setup_paeth_row_only(png_structrp png_ptr, const png_uint_32 bpp,
const png_size_t row_bytes)
{
png_bytep rp, dp, pp, cp, lp;
png_size_t i;
png_ptr->try_row[0] = PNG_FILTER_VALUE_PAETH;
for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1,
pp = png_ptr->prev_row + 1; i < bpp; i++)
{
*dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
}
for (lp = png_ptr->row_buf + 1, cp = png_ptr->prev_row + 1; i < row_bytes;
i++)
{
int a, b, c, pa, pb, pc, p;
b = *pp++;
c = *cp++;
a = *lp++;
p = b - c;
pc = a - c;
#ifdef PNG_USE_ABS
pa = abs(p);
pb = abs(pc);
pc = abs(p + pc);
#else
pa = p < 0 ? -p : p;
pb = pc < 0 ? -pc : pc;
pc = (p + pc) < 0 ? -(p + pc) : p + pc;
#endif
p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
*dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
}
}
#endif /* WRITE_FILTER */ #endif /* WRITE_FILTER */
void /* PRIVATE */ void /* PRIVATE */
@ -2478,7 +2555,7 @@ png_write_find_filter(png_structrp png_ptr, png_row_infop row_info)
#ifndef PNG_WRITE_FILTER_SUPPORTED #ifndef PNG_WRITE_FILTER_SUPPORTED
png_write_filtered_row(png_ptr, png_ptr->row_buf, row_info->rowbytes+1); png_write_filtered_row(png_ptr, png_ptr->row_buf, row_info->rowbytes+1);
#else #else
png_byte filter_to_do = png_ptr->do_filter; unsigned int filter_to_do = png_ptr->do_filter;
png_bytep row_buf; png_bytep row_buf;
png_bytep best_row; png_bytep best_row;
png_uint_32 bpp; png_uint_32 bpp;
@ -2524,32 +2601,33 @@ png_write_find_filter(png_structrp png_ptr, png_row_infop row_info)
*/ */
best_row = png_ptr->row_buf; best_row = png_ptr->row_buf;
if (PNG_SIZE_MAX/128 <= row_bytes)
if ((filter_to_do & PNG_FILTER_NONE) != 0 && filter_to_do != PNG_FILTER_NONE)
{ {
/* Overflow can occur in the calculation, just select the lowest set
* filter.
*/
filter_to_do &= 0U-filter_to_do;
}
else if ((filter_to_do & PNG_FILTER_NONE) != 0 &&
filter_to_do != PNG_FILTER_NONE)
{
/* Overflow not possible and multiple filters in the list, including the
* 'none' filter.
*/
png_bytep rp; png_bytep rp;
png_size_t sum = 0; png_size_t sum = 0;
png_size_t i; png_size_t i;
int v; unsigned int v;
if (PNG_SIZE_MAX/128 <= row_bytes)
{
for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
{
/* Check for overflow */
if (sum > PNG_SIZE_MAX/128 - 256)
break;
v = *rp;
sum += (v < 128) ? v : 256 - v;
}
}
else /* Overflow is not possible */
{ {
for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++) for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
{ {
v = *rp; v = *rp;
#ifdef PNG_USE_ABS
sum += 128 - abs((int)v - 128);
#else
sum += (v < 128) ? v : 256 - v; sum += (v < 128) ? v : 256 - v;
#endif
} }
} }
@ -2560,7 +2638,7 @@ png_write_find_filter(png_structrp png_ptr, png_row_infop row_info)
if (filter_to_do == PNG_FILTER_SUB) if (filter_to_do == PNG_FILTER_SUB)
/* It's the only filter so no testing is needed */ /* It's the only filter so no testing is needed */
{ {
(void) png_setup_sub_row(png_ptr, bpp, row_bytes, mins); png_setup_sub_row_only(png_ptr, bpp, row_bytes);
best_row = png_ptr->try_row; best_row = png_ptr->try_row;
} }
@ -2586,7 +2664,7 @@ png_write_find_filter(png_structrp png_ptr, png_row_infop row_info)
/* Up filter */ /* Up filter */
if (filter_to_do == PNG_FILTER_UP) if (filter_to_do == PNG_FILTER_UP)
{ {
(void) png_setup_up_row(png_ptr, row_bytes, mins); png_setup_up_row_only(png_ptr, row_bytes);
best_row = png_ptr->try_row; best_row = png_ptr->try_row;
} }
@ -2612,7 +2690,7 @@ png_write_find_filter(png_structrp png_ptr, png_row_infop row_info)
/* Avg filter */ /* Avg filter */
if (filter_to_do == PNG_FILTER_AVG) if (filter_to_do == PNG_FILTER_AVG)
{ {
(void) png_setup_avg_row(png_ptr, bpp, row_bytes, mins); png_setup_avg_row_only(png_ptr, bpp, row_bytes);
best_row = png_ptr->try_row; best_row = png_ptr->try_row;
} }
@ -2636,9 +2714,9 @@ png_write_find_filter(png_structrp png_ptr, png_row_infop row_info)
} }
/* Paeth filter */ /* Paeth filter */
if ((filter_to_do == PNG_FILTER_PAETH) != 0) if (filter_to_do == PNG_FILTER_PAETH)
{ {
(void) png_setup_paeth_row(png_ptr, bpp, row_bytes, mins); png_setup_paeth_row_only(png_ptr, bpp, row_bytes);
best_row = png_ptr->try_row; best_row = png_ptr->try_row;
} }
@ -2651,7 +2729,6 @@ png_write_find_filter(png_structrp png_ptr, png_row_infop row_info)
if (sum < mins) if (sum < mins)
{ {
mins = sum;
best_row = png_ptr->try_row; best_row = png_ptr->try_row;
if (png_ptr->tst_row != NULL) if (png_ptr->tst_row != NULL)
{ {