/* png.c - location for general purpose libpng functions
*
- * libpng version 1.2.4 - July 8, 2002
- * Copyright (c) 1998-2002 Glenn Randers-Pehrson
+ * Last changed in libpng 1.2.34 [December 18, 2008]
+ * For conditions of distribution and use, see copyright notice in png.h
+ * Copyright (c) 1998-2008 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
- *
*/
#define PNG_INTERNAL
#include "png.h"
/* Generate a compiler error if there is an old png.h in the search path. */
-typedef version_1_2_4 Your_png_h_is_not_version_1_2_4;
+typedef version_1_2_34 Your_png_h_is_not_version_1_2_34;
/* Version information for C files. This had better match the version
* string defined in png.h. */
#ifdef PNG_USE_GLOBAL_ARRAYS
/* png_libpng_ver was changed to a function in version 1.0.5c */
-const char png_libpng_ver[18] = "1.2.4";
+PNG_CONST char png_libpng_ver[18] = PNG_LIBPNG_VER_STRING;
+
+#ifdef PNG_READ_SUPPORTED
/* png_sig was changed to a function in version 1.0.5c */
/* Place to hold the signature string for a PNG file. */
-const png_byte FARDATA png_sig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
+PNG_CONST png_byte FARDATA png_sig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
+#endif /* PNG_READ_SUPPORTED */
/* Invoke global declarations for constant strings for known chunk types */
PNG_IHDR;
PNG_tRNS;
PNG_zTXt;
+#ifdef PNG_READ_SUPPORTED
/* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
/* start of interlace block */
-const int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
+PNG_CONST int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
/* offset to next interlace block */
-const int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
+PNG_CONST int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
/* start of interlace block in the y direction */
-const int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
+PNG_CONST int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
/* offset to next interlace block in the y direction */
-const int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
-
-/* width of interlace block (used in assembler routines only) */
-#ifdef PNG_HAVE_ASSEMBLER_COMBINE_ROW
-const int FARDATA png_pass_width[] = {8, 4, 4, 2, 2, 1, 1};
-#endif
+PNG_CONST int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
/* Height of interlace block. This is not currently used - if you need
* it, uncomment it here and in png.h
-const int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
+PNG_CONST int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
*/
/* Mask to determine which pixels are valid in a pass */
-const int FARDATA png_pass_mask[] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
+PNG_CONST int FARDATA png_pass_mask[] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
/* Mask to determine which pixels to overwrite while displaying */
-const int FARDATA png_pass_dsp_mask[]
+PNG_CONST int FARDATA png_pass_dsp_mask[]
= {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
-#endif
+#endif /* PNG_READ_SUPPORTED */
+#endif /* PNG_USE_GLOBAL_ARRAYS */
/* 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
* or write any of the magic bytes before it starts on the IHDR.
*/
+#ifdef PNG_READ_SUPPORTED
void PNGAPI
png_set_sig_bytes(png_structp png_ptr, int num_bytes)
{
- png_debug(1, "in png_set_sig_bytes\n");
+ if (png_ptr == NULL) return;
+ png_debug(1, "in png_set_sig_bytes");
if (num_bytes > 8)
png_error(png_ptr, "Too many bytes for PNG signature.");
if (num_to_check > 8)
num_to_check = 8;
else if (num_to_check < 1)
- return (0);
+ return (-1);
if (start > 7)
- return (0);
+ return (-1);
if (start + num_to_check > 8)
num_to_check = 8 - start;
return ((int)(png_memcmp(&sig[start], &png_signature[start], num_to_check)));
}
+#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
/* (Obsolete) function to check signature bytes. It does not allow one
* to check a partial signature. This function might be removed in the
- * future - use png_sig_cmp(). Returns true (nonzero) if the file is a PNG.
+ * future - use png_sig_cmp(). Returns true (nonzero) if the file is PNG.
*/
int PNGAPI
png_check_sig(png_bytep sig, int num)
{
return ((int)!png_sig_cmp(sig, (png_size_t)0, (png_size_t)num));
}
+#endif
+#endif /* PNG_READ_SUPPORTED */
+#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
/* Function to allocate memory for zlib and clear it to 0. */
#ifdef PNG_1_0_X
-void PNGAPI
+voidpf PNGAPI
#else
voidpf /* private */
#endif
png_zalloc(voidpf png_ptr, uInt items, uInt size)
{
- png_uint_32 num_bytes = (png_uint_32)items * size;
png_voidp ptr;
- png_structp p=png_ptr;
+ png_structp p=(png_structp)png_ptr;
png_uint_32 save_flags=p->flags;
+ png_uint_32 num_bytes;
+
+ if (png_ptr == NULL) return (NULL);
+ if (items > PNG_UINT_32_MAX/size)
+ {
+ png_warning (p, "Potential overflow in png_zalloc()");
+ return (NULL);
+ }
+ num_bytes = (png_uint_32)items * size;
p->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
ptr = (png_voidp)png_malloc((png_structp)png_ptr, num_bytes);
p->flags=save_flags;
-#ifndef PNG_NO_ZALLOC_ZERO
+#if defined(PNG_1_0_X) && !defined(PNG_NO_ZALLOC_ZERO)
if (ptr == NULL)
return ((voidpf)ptr);
/* Allocate the memory for an info_struct for the application. We don't
* really need the png_ptr, but it could potentially be useful in the
- * future. This should be used in favour of malloc(sizeof(png_info))
+ * future. This should be used in favour of malloc(png_sizeof(png_info))
* and png_info_init() so that applications that want to use a shared
* libpng don't have to be recompiled if png_info changes size.
*/
{
png_infop info_ptr;
- png_debug(1, "in png_create_info_struct\n");
- if(png_ptr == NULL) return (NULL);
+ png_debug(1, "in png_create_info_struct");
+ if (png_ptr == NULL) return (NULL);
#ifdef PNG_USER_MEM_SUPPORTED
info_ptr = (png_infop)png_create_struct_2(PNG_STRUCT_INFO,
png_ptr->malloc_fn, png_ptr->mem_ptr);
info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
#endif
if (info_ptr != NULL)
- png_info_init_3(&info_ptr, sizeof(png_info));
+ png_info_init_3(&info_ptr, png_sizeof(png_info));
return (info_ptr);
}
png_destroy_info_struct(png_structp png_ptr, png_infopp info_ptr_ptr)
{
png_infop info_ptr = NULL;
+ if (png_ptr == NULL) return;
- png_debug(1, "in png_destroy_info_struct\n");
+ png_debug(1, "in png_destroy_info_struct");
if (info_ptr_ptr != NULL)
info_ptr = *info_ptr_ptr;
* and applications using it are urged to use png_create_info_struct()
* instead.
*/
+#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
#undef png_info_init
void PNGAPI
png_info_init(png_infop info_ptr)
/* We only come here via pre-1.0.12-compiled applications */
png_info_init_3(&info_ptr, 0);
}
+#endif
void PNGAPI
png_info_init_3(png_infopp ptr_ptr, png_size_t png_info_struct_size)
{
png_infop info_ptr = *ptr_ptr;
- png_debug(1, "in png_info_init_3\n");
+ if (info_ptr == NULL) return;
+
+ png_debug(1, "in png_info_init_3");
- if(sizeof(png_info) > png_info_struct_size)
+ if (png_sizeof(png_info) > png_info_struct_size)
{
png_destroy_struct(info_ptr);
info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
}
/* set everything to 0 */
- png_memset(info_ptr, 0, sizeof (png_info));
+ png_memset(info_ptr, 0, png_sizeof(png_info));
}
#ifdef PNG_FREE_ME_SUPPORTED
png_data_freer(png_structp png_ptr, png_infop info_ptr,
int freer, png_uint_32 mask)
{
- png_debug(1, "in png_data_freer\n");
+ png_debug(1, "in png_data_freer");
if (png_ptr == NULL || info_ptr == NULL)
return;
- if(freer == PNG_DESTROY_WILL_FREE_DATA)
+ if (freer == PNG_DESTROY_WILL_FREE_DATA)
info_ptr->free_me |= mask;
- else if(freer == PNG_USER_WILL_FREE_DATA)
+ else if (freer == PNG_USER_WILL_FREE_DATA)
info_ptr->free_me &= ~mask;
else
png_warning(png_ptr,
png_free_data(png_structp png_ptr, png_infop info_ptr, png_uint_32 mask,
int num)
{
- png_debug(1, "in png_free_data\n");
+ png_debug(1, "in png_free_data");
if (png_ptr == NULL || info_ptr == NULL)
return;
#endif
{
png_free(png_ptr, info_ptr->trans);
+ info_ptr->trans = NULL;
info_ptr->valid &= ~PNG_INFO_tRNS;
#ifndef PNG_FREE_ME_SUPPORTED
png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
#endif
- info_ptr->trans = NULL;
}
#endif
{
if (num != -1)
{
- if(info_ptr->splt_palettes)
+ if (info_ptr->splt_palettes)
{
png_free(png_ptr, info_ptr->splt_palettes[num].name);
png_free(png_ptr, info_ptr->splt_palettes[num].entries);
}
else
{
- if(info_ptr->splt_palettes_num)
+ if (info_ptr->splt_palettes_num)
{
int i;
for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
#endif
#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
+ if (png_ptr->unknown_chunk.data)
+ {
+ png_free(png_ptr, png_ptr->unknown_chunk.data);
+ png_ptr->unknown_chunk.data = NULL;
+ }
+
#ifdef PNG_FREE_ME_SUPPORTED
if ((mask & PNG_FREE_UNKN) & info_ptr->free_me)
#else
{
if (num != -1)
{
- if(info_ptr->unknown_chunks)
+ if (info_ptr->unknown_chunks)
{
png_free(png_ptr, info_ptr->unknown_chunks[num].data);
info_ptr->unknown_chunks[num].data = NULL;
{
int i;
- if(info_ptr->unknown_chunks_num)
+ if (info_ptr->unknown_chunks_num)
{
for (i = 0; i < (int)info_ptr->unknown_chunks_num; i++)
png_free_data(png_ptr, info_ptr, PNG_FREE_UNKN, i);
if (mask & PNG_FREE_ROWS)
#endif
{
- if(info_ptr->row_pointers)
+ if (info_ptr->row_pointers)
{
int row;
for (row = 0; row < (int)info_ptr->height; row++)
#endif
#ifdef PNG_FREE_ME_SUPPORTED
- if(num == -1)
+ if (num == -1)
info_ptr->free_me &= ~mask;
else
info_ptr->free_me &= ~(mask & ~PNG_FREE_MUL);
void /* PRIVATE */
png_info_destroy(png_structp png_ptr, png_infop info_ptr)
{
- png_debug(1, "in png_info_destroy\n");
+ png_debug(1, "in png_info_destroy");
png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
{
png_free(png_ptr, png_ptr->chunk_list);
png_ptr->chunk_list=NULL;
- png_ptr->num_chunk_list=0;
+ png_ptr->num_chunk_list = 0;
}
#endif
- png_info_init_3(&info_ptr, sizeof(png_info));
+ png_info_init_3(&info_ptr, png_sizeof(png_info));
}
+#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
/* This function returns a pointer to the io_ptr associated with the user
* functions. The application should free any memory associated with this
png_voidp PNGAPI
png_get_io_ptr(png_structp png_ptr)
{
+ if (png_ptr == NULL) return (NULL);
return (png_ptr->io_ptr);
}
+#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
#if !defined(PNG_NO_STDIO)
/* Initialize the default input/output functions for the PNG file. If you
* use your own read or write routines, you can call either png_set_read_fn()
void PNGAPI
png_init_io(png_structp png_ptr, png_FILE_p fp)
{
- png_debug(1, "in png_init_io\n");
+ png_debug(1, "in png_init_io");
+ if (png_ptr == NULL) return;
png_ptr->io_ptr = (png_voidp)fp;
}
#endif
{"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
+ if (png_ptr == NULL) return (NULL);
if (png_ptr->time_buffer == NULL)
{
png_ptr->time_buffer = (png_charp)png_malloc(png_ptr, (png_uint_32)(29*
- sizeof(char)));
+ png_sizeof(char)));
}
#if defined(_WIN32_WCE)
#ifdef USE_FAR_KEYWORD
{
char near_time_buf[29];
- sprintf(near_time_buf, "%d %s %d %02d:%02d:%02d +0000",
+ png_snprintf6(near_time_buf, 29, "%d %s %d %02d:%02d:%02d +0000",
ptime->day % 32, short_months[(ptime->month - 1) % 12],
ptime->year, ptime->hour % 24, ptime->minute % 60,
ptime->second % 61);
png_memcpy(png_ptr->time_buffer, near_time_buf,
- 29*sizeof(char));
+ 29*png_sizeof(char));
}
#else
- sprintf(png_ptr->time_buffer, "%d %s %d %02d:%02d:%02d +0000",
+ png_snprintf6(png_ptr->time_buffer, 29, "%d %s %d %02d:%02d:%02d +0000",
ptime->day % 32, short_months[(ptime->month - 1) % 12],
ptime->year, ptime->hour % 24, ptime->minute % 60,
ptime->second % 61);
}
#endif /* PNG_TIME_RFC1123_SUPPORTED */
-#if 0
-/* Signature string for a PNG file. */
-png_bytep PNGAPI
-png_sig_bytes(void)
-{
- return ((png_bytep)"\211\120\116\107\015\012\032\012");
-}
-#endif
+#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
png_charp PNGAPI
png_get_copyright(png_structp png_ptr)
{
- if (png_ptr != NULL || png_ptr == NULL) /* silence compiler warning */
- return ((png_charp) "\n libpng version 1.2.4 - July 8, 2002\n\
- Copyright (c) 1998-2002 Glenn Randers-Pehrson\n\
+ png_ptr = png_ptr; /* silence compiler warning about unused png_ptr */
+ return ((png_charp) "\n libpng version 1.2.34 - December 18, 2008\n\
+ Copyright (c) 1998-2008 Glenn Randers-Pehrson\n\
Copyright (c) 1996-1997 Andreas Dilger\n\
Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.\n");
- return ((png_charp) "");
}
/* The following return the library version as a short string in the
- * format 1.0.0 through 99.99.99zz. To get the version of *.h files used
- * with your application, print out PNG_LIBPNG_VER_STRING, which is defined
- * in png.h.
+ * format 1.0.0 through 99.99.99zz. To get the version of *.h files
+ * used with your application, print out PNG_LIBPNG_VER_STRING, which
+ * is defined in png.h.
+ * Note: now there is no difference between png_get_libpng_ver() and
+ * png_get_header_ver(). Due to the version_nn_nn_nn typedef guard,
+ * it is guaranteed that png.c uses the correct version of png.h.
*/
-
png_charp PNGAPI
png_get_libpng_ver(png_structp png_ptr)
{
/* Version of *.c files used when building libpng */
- if(png_ptr != NULL) /* silence compiler warning about unused png_ptr */
- return((png_charp) "1.2.4");
- return((png_charp) "1.2.4");
+ png_ptr = png_ptr; /* silence compiler warning about unused png_ptr */
+ return ((png_charp) PNG_LIBPNG_VER_STRING);
}
png_charp PNGAPI
png_get_header_ver(png_structp png_ptr)
{
/* Version of *.h files used when building libpng */
- if(png_ptr != NULL) /* silence compiler warning about unused png_ptr */
- return((png_charp) PNG_LIBPNG_VER_STRING);
- return((png_charp) PNG_LIBPNG_VER_STRING);
+ png_ptr = png_ptr; /* silence compiler warning about unused png_ptr */
+ return ((png_charp) PNG_LIBPNG_VER_STRING);
}
png_charp PNGAPI
png_get_header_version(png_structp png_ptr)
{
/* Returns longer string containing both version and date */
- if(png_ptr != NULL) /* silence compiler warning about unused png_ptr */
- return((png_charp) PNG_HEADER_VERSION_STRING);
- return((png_charp) PNG_HEADER_VERSION_STRING);
+ png_ptr = png_ptr; /* silence compiler warning about unused png_ptr */
+ return ((png_charp) PNG_HEADER_VERSION_STRING
+#ifndef PNG_READ_SUPPORTED
+ " (NO READ SUPPORT)"
+#endif
+ "\n");
}
+#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
int PNGAPI
png_handle_as_unknown(png_structp png_ptr, png_bytep chunk_name)
/* check chunk_name and return "keep" value if it's on the list, else 0 */
int i;
png_bytep p;
- if((png_ptr == NULL && chunk_name == NULL) || png_ptr->num_chunk_list<=0)
+ if (png_ptr == NULL || chunk_name == NULL || png_ptr->num_chunk_list<=0)
return 0;
- p=png_ptr->chunk_list+png_ptr->num_chunk_list*5-5;
- for (i = png_ptr->num_chunk_list; i; i--, p-=5)
+ p = png_ptr->chunk_list + png_ptr->num_chunk_list*5 - 5;
+ for (i = png_ptr->num_chunk_list; i; i--, p -= 5)
if (!png_memcmp(chunk_name, p, 4))
- return ((int)*(p+4));
+ return ((int)*(p + 4));
return 0;
}
#endif
int PNGAPI
png_reset_zstream(png_structp png_ptr)
{
+ if (png_ptr == NULL) return Z_STREAM_ERROR;
return (inflateReset(&png_ptr->zstream));
}
+#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
/* This function was added to libpng-1.0.7 */
png_uint_32 PNGAPI
png_access_version_number(void)
{
/* Version of *.c files used when building libpng */
- return((png_uint_32) 10204L);
+ return((png_uint_32) PNG_LIBPNG_VER);
}
+#if defined(PNG_READ_SUPPORTED) && defined(PNG_ASSEMBLER_CODE_SUPPORTED)
#if !defined(PNG_1_0_X)
-#if defined(PNG_ASSEMBLER_CODE_SUPPORTED)
- /* GRR: could add this: && defined(PNG_MMX_CODE_SUPPORTED) */
-/* this INTERNAL function was added to libpng 1.2.0 */
-void /* PRIVATE */
-png_init_mmx_flags (png_structp png_ptr)
-{
- png_ptr->mmx_rowbytes_threshold = 0;
- png_ptr->mmx_bitdepth_threshold = 0;
-
-# if (defined(PNG_USE_PNGVCRD) || defined(PNG_USE_PNGGCCRD))
-
- png_ptr->asm_flags |= PNG_ASM_FLAG_MMX_SUPPORT_COMPILED;
-
- if (png_mmx_support()) {
- png_ptr->asm_flags |= PNG_ASM_FLAG_MMX_SUPPORT_IN_CPU
-# ifdef PNG_HAVE_ASSEMBLER_COMBINE_ROW
- | PNG_ASM_FLAG_MMX_READ_COMBINE_ROW
-# endif
-# ifdef PNG_HAVE_ASSEMBLER_READ_INTERLACE
- | PNG_ASM_FLAG_MMX_READ_INTERLACE
-# endif
-# ifndef PNG_HAVE_ASSEMBLER_READ_FILTER_ROW
- ;
-# else
- | PNG_ASM_FLAG_MMX_READ_FILTER_SUB
- | PNG_ASM_FLAG_MMX_READ_FILTER_UP
- | PNG_ASM_FLAG_MMX_READ_FILTER_AVG
- | PNG_ASM_FLAG_MMX_READ_FILTER_PAETH ;
-
- png_ptr->mmx_rowbytes_threshold = PNG_MMX_ROWBYTES_THRESHOLD_DEFAULT;
- png_ptr->mmx_bitdepth_threshold = PNG_MMX_BITDEPTH_THRESHOLD_DEFAULT;
-# endif
- } else {
- png_ptr->asm_flags &= ~( PNG_ASM_FLAG_MMX_SUPPORT_IN_CPU
- | PNG_MMX_READ_FLAGS
- | PNG_MMX_WRITE_FLAGS );
- }
-
-# else /* !((PNGVCRD || PNGGCCRD) && PNG_ASSEMBLER_CODE_SUPPORTED)) */
-
- /* clear all MMX flags; no support is compiled in */
- png_ptr->asm_flags &= ~( PNG_MMX_FLAGS );
-
-# endif /* ?(PNGVCRD || PNGGCCRD) */
-}
-
-#endif /* !(PNG_ASSEMBLER_CODE_SUPPORTED) */
-
/* this function was added to libpng 1.2.0 */
-#if !defined(PNG_USE_PNGGCCRD) && \
- !(defined(PNG_ASSEMBLER_CODE_SUPPORTED) && defined(PNG_USE_PNGVCRD))
int PNGAPI
png_mmx_support(void)
{
+ /* obsolete, to be removed from libpng-1.4.0 */
return -1;
}
-#endif
#endif /* PNG_1_0_X */
+#endif /* PNG_READ_SUPPORTED && PNG_ASSEMBLER_CODE_SUPPORTED */
+
+#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
+#ifdef PNG_SIZE_T
+/* Added at libpng version 1.2.6 */
+ PNG_EXTERN png_size_t PNGAPI png_convert_size PNGARG((size_t size));
+png_size_t PNGAPI
+png_convert_size(size_t size)
+{
+ if (size > (png_size_t)-1)
+ PNG_ABORT(); /* We haven't got access to png_ptr, so no png_error() */
+ return ((png_size_t)size);
+}
+#endif /* PNG_SIZE_T */
+
+/* Added at libpng version 1.2.34 and 1.4.0 (moved from pngset.c) */
+#if defined(PNG_cHRM_SUPPORTED)
+#if !defined(PNG_NO_CHECK_cHRM)
+/*
+ Multiply two 32-bit numbers, V1 and V2, using 32-bit
+ arithmetic, to produce a 64 bit result in the HI/LO words.
+
+ A B
+ x C D
+ ------
+ AD || BD
+AC || CB || 0
+
+ where A and B are the high and low 16-bit words of V1,
+ C and D are the 16-bit words of V2, AD is the product of
+ A and D, and X || Y is (X << 16) + Y.
+*/
+
+void png_64bit_product (long v1, long v2, unsigned long *hi_product,
+ unsigned long *lo_product)
+{
+ int a, b, c, d;
+ long lo, hi, x, y;
+
+ a = (v1 >> 16) & 0xffff;
+ b = v1 & 0xffff;
+ c = (v2 >> 16) & 0xffff;
+ d = v2 & 0xffff;
+
+ lo = b * d; /* BD */
+ x = a * d + c * b; /* AD + CB */
+ y = ((lo >> 16) & 0xffff) + x;
+
+ lo = (lo & 0xffff) | ((y & 0xffff) << 16);
+ hi = (y >> 16) & 0xffff;
+
+ hi += a * c; /* AC */
+
+ *hi_product = (unsigned long)hi;
+ *lo_product = (unsigned long)lo;
+}
+int /* private */
+png_check_cHRM_fixed(png_structp png_ptr,
+ png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
+ png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
+ png_fixed_point blue_x, png_fixed_point blue_y)
+{
+ int ret = 1;
+ unsigned long xy_hi,xy_lo,yx_hi,yx_lo;
+
+ png_debug(1, "in function png_check_cHRM_fixed");
+ if (png_ptr == NULL)
+ return 0;
+
+ if (white_x < 0 || white_y <= 0 ||
+ red_x < 0 || red_y < 0 ||
+ green_x < 0 || green_y < 0 ||
+ blue_x < 0 || blue_y < 0)
+ {
+ png_warning(png_ptr,
+ "Ignoring attempt to set negative chromaticity value");
+ ret = 0;
+ }
+ if (white_x > (png_fixed_point) PNG_UINT_31_MAX ||
+ white_y > (png_fixed_point) PNG_UINT_31_MAX ||
+ red_x > (png_fixed_point) PNG_UINT_31_MAX ||
+ red_y > (png_fixed_point) PNG_UINT_31_MAX ||
+ green_x > (png_fixed_point) PNG_UINT_31_MAX ||
+ green_y > (png_fixed_point) PNG_UINT_31_MAX ||
+ blue_x > (png_fixed_point) PNG_UINT_31_MAX ||
+ blue_y > (png_fixed_point) PNG_UINT_31_MAX )
+ {
+ png_warning(png_ptr,
+ "Ignoring attempt to set chromaticity value exceeding 21474.83");
+ ret = 0;
+ }
+ if (white_x > 100000L - white_y)
+ {
+ png_warning(png_ptr, "Invalid cHRM white point");
+ ret = 0;
+ }
+ if (red_x > 100000L - red_y)
+ {
+ png_warning(png_ptr, "Invalid cHRM red point");
+ ret = 0;
+ }
+ if (green_x > 100000L - green_y)
+ {
+ png_warning(png_ptr, "Invalid cHRM green point");
+ ret = 0;
+ }
+ if (blue_x > 100000L - blue_y)
+ {
+ png_warning(png_ptr, "Invalid cHRM blue point");
+ ret = 0;
+ }
+
+ png_64bit_product(green_x - red_x, blue_y - red_y, &xy_hi, &xy_lo);
+ png_64bit_product(green_y - red_y, blue_x - red_x, &yx_hi, &yx_lo);
+
+ if (xy_hi == yx_hi && xy_lo == yx_lo)
+ {
+ png_warning(png_ptr,
+ "Ignoring attempt to set cHRM RGB triangle with zero area");
+ ret = 0;
+ }
+
+ return ret;
+}
+#endif /* NO_PNG_CHECK_cHRM */
+#endif /* PNG_cHRM_SUPPORTED */
+#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */