blob: b4c602051735fa31fbad74128fe5af927d4309a5 [file] [log] [blame]
Guy Schalnat0d580581995-07-20 02:43:20 -05001
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002/* png.c - location for general purpose libpng functions
3 *
Glenn Randers-Pehrson866b62a2009-08-08 16:33:14 -05004 * Last changed in libpng 1.4.0 [August 8, 2009]
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06005 * Copyright (c) 1998-2009 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrson3e61d792009-06-24 09:31:28 -05008 *
Glenn Randers-Pehrsonbfbf8652009-06-26 21:46:52 -05009 * This code is released under the libpng license.
Glenn Randers-Pehrsonc332bbc2009-06-25 13:43:50 -050010 * For conditions of distribution and use, see the disclaimer
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050011 * and license in png.h
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060012 */
Guy Schalnat0d580581995-07-20 02:43:20 -050013
Guy Schalnat0d580581995-07-20 02:43:20 -050014#define PNG_NO_EXTERN
15#include "png.h"
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050016#include "pngpriv.h"
Guy Schalnat0d580581995-07-20 02:43:20 -050017
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -060018/* Generate a compiler error if there is an old png.h in the search path. */
Glenn Randers-Pehrson866b62a2009-08-08 16:33:14 -050019typedef version_1_4_0beta74 Your_png_h_is_not_version_1_4_0beta74;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -060020
Andreas Dilger47a0c421997-05-16 02:46:07 -050021/* Version information for C files. This had better match the version
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -060022 * string defined in png.h. */
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -060023
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -060024#ifdef PNG_USE_GLOBAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -060025/* png_libpng_ver was changed to a function in version 1.0.5c */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050026PNG_CONST char png_libpng_ver[18] = PNG_LIBPNG_VER_STRING;
Guy Schalnat51f0eb41995-09-26 05:22:39 -050027
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060028#ifdef PNG_READ_SUPPORTED
29
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -060030/* png_sig was changed to a function in version 1.0.5c */
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060031/* Place to hold the signature string for a PNG file. */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050032PNG_CONST png_byte FARDATA png_sig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060033#endif /* PNG_READ_SUPPORTED */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060034
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -060035/* Invoke global declarations for constant strings for known chunk types */
36PNG_IHDR;
37PNG_IDAT;
38PNG_IEND;
39PNG_PLTE;
40PNG_bKGD;
41PNG_cHRM;
42PNG_gAMA;
43PNG_hIST;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060044PNG_iCCP;
45PNG_iTXt;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -060046PNG_oFFs;
47PNG_pCAL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060048PNG_sCAL;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -060049PNG_pHYs;
50PNG_sBIT;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060051PNG_sPLT;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -060052PNG_sRGB;
53PNG_tEXt;
54PNG_tIME;
55PNG_tRNS;
56PNG_zTXt;
Guy Schalnat0d580581995-07-20 02:43:20 -050057
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060058#ifdef PNG_READ_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050059/* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Guy Schalnat0d580581995-07-20 02:43:20 -050060
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050061/* Start of interlace block */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050062PNG_CONST int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
Guy Schalnat0d580581995-07-20 02:43:20 -050063
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050064/* Offset to next interlace block */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050065PNG_CONST int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
Guy Schalnat0d580581995-07-20 02:43:20 -050066
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050067/* Start of interlace block in the y direction */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050068PNG_CONST int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
Guy Schalnat0d580581995-07-20 02:43:20 -050069
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050070/* Offset to next interlace block in the y direction */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050071PNG_CONST int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
Guy Schalnat0d580581995-07-20 02:43:20 -050072
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060073/* Height of interlace block. This is not currently used - if you need
74 * it, uncomment it here and in png.h
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050075PNG_CONST int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
Guy Schalnat0d580581995-07-20 02:43:20 -050076*/
77
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060078/* Mask to determine which pixels are valid in a pass */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050079PNG_CONST int FARDATA png_pass_mask[] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
Guy Schalnat0d580581995-07-20 02:43:20 -050080
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060081/* Mask to determine which pixels to overwrite while displaying */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050082PNG_CONST int FARDATA png_pass_dsp_mask[]
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -060083 = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
Guy Schalnat0d580581995-07-20 02:43:20 -050084
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060085#endif /* PNG_READ_SUPPORTED */
Glenn Randers-Pehrson16e11662004-11-01 14:13:40 -060086#endif /* PNG_USE_GLOBAL_ARRAYS */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -060087
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060088/* Tells libpng that we have already handled the first "num_bytes" bytes
89 * of the PNG file signature. If the PNG data is embedded into another
90 * stream we can set num_bytes = 8 so that libpng will not attempt to read
91 * or write any of the magic bytes before it starts on the IHDR.
92 */
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -050093
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060094#ifdef PNG_READ_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050095void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060096png_set_sig_bytes(png_structp png_ptr, int num_bytes)
97{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050098 if (png_ptr == NULL)
99 return;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500100 png_debug(1, "in png_set_sig_bytes");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600101 if (num_bytes > 8)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500102 png_error(png_ptr, "Too many bytes for PNG signature");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600103
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500104 png_ptr->sig_bytes = (png_byte)(num_bytes < 0 ? 0 : num_bytes);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600105}
106
107/* Checks whether the supplied bytes match the PNG signature. We allow
108 * checking less than the full 8-byte signature so that those apps that
109 * already read the first few bytes of a file to determine the file type
110 * can simply check the remaining bytes for extra assurance. Returns
111 * an integer less than, equal to, or greater than zero if sig is found,
112 * respectively, to be less than, to match, or be greater than the correct
113 * PNG signature (this is the same behaviour as strcmp, memcmp, etc).
114 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500115int PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500116png_sig_cmp(png_bytep sig, png_size_t start, png_size_t num_to_check)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600117{
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600118 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600119 if (num_to_check > 8)
120 num_to_check = 8;
121 else if (num_to_check < 1)
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600122 return (-1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600123
Andreas Dilger47a0c421997-05-16 02:46:07 -0500124 if (start > 7)
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600125 return (-1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600126
127 if (start + num_to_check > 8)
128 num_to_check = 8 - start;
129
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500130 return ((int)(png_memcmp(&sig[start], &png_signature[start], num_to_check)));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600131}
132
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600133#endif /* PNG_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500134
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600135#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500136/* Function to allocate memory for zlib and clear it to 0. */
Glenn Randers-Pehrson3f705ba2009-07-23 12:53:06 -0500137voidpf /* PRIVATE */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500138png_zalloc(voidpf png_ptr, uInt items, uInt size)
Guy Schalnat0d580581995-07-20 02:43:20 -0500139{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500140 png_voidp ptr;
141 png_structp p=(png_structp)png_ptr;
142 png_uint_32 save_flags=p->flags;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500143 png_alloc_size_t num_bytes;
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500144
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500145 if (png_ptr == NULL)
146 return (NULL);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500147 if (items > PNG_UINT_32_MAX/size)
148 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500149 png_warning (p, "Potential overflow in png_zalloc()");
150 return (NULL);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500151 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500152 num_bytes = (png_alloc_size_t)items * size;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600153
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500154 p->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
155 ptr = (png_voidp)png_malloc((png_structp)png_ptr, num_bytes);
156 p->flags=save_flags;
Guy Schalnat6d764711995-12-19 03:22:19 -0600157
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500158 return ((voidpf)ptr);
159}
160
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500161/* Function to free memory for zlib */
Glenn Randers-Pehrson3f705ba2009-07-23 12:53:06 -0500162void /* PRIVATE */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500163png_zfree(voidpf png_ptr, voidpf ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500164{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600165 png_free((png_structp)png_ptr, (png_voidp)ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500166}
167
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600168/* Reset the CRC variable to 32 bits of 1's. Care must be taken
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600169 * in case CRC is > 32 bits to leave the top bits 0.
170 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500171void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600172png_reset_crc(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500173{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600174 png_ptr->crc = crc32(0, Z_NULL, 0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500175}
176
Andreas Dilger47a0c421997-05-16 02:46:07 -0500177/* Calculate the CRC over a section of data. We can only pass as
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600178 * much data to this routine as the largest single buffer size. We
179 * also check that this data will actually be used before going to the
180 * trouble of calculating it.
181 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500182void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500183png_calculate_crc(png_structp png_ptr, png_bytep ptr, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500184{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500185 int need_crc = 1;
186
187 if (png_ptr->chunk_name[0] & 0x20) /* ancillary */
188 {
189 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
190 (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
191 need_crc = 0;
192 }
193 else /* critical */
194 {
195 if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
196 need_crc = 0;
197 }
198
199 if (need_crc)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600200 png_ptr->crc = crc32(png_ptr->crc, ptr, (uInt)length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500201}
Guy Schalnate5a37791996-06-05 15:50:50 -0500202
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600203/* Allocate the memory for an info_struct for the application. We don't
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600204 * really need the png_ptr, but it could potentially be useful in the
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500205 * future. This should be used in favour of malloc(png_sizeof(png_info))
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600206 * and png_info_init() so that applications that want to use a shared
207 * libpng don't have to be recompiled if png_info changes size.
208 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500209png_infop PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -0500210png_create_info_struct(png_structp png_ptr)
211{
212 png_infop info_ptr;
213
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500214 png_debug(1, "in png_create_info_struct");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500215 if (png_ptr == NULL)
216 return (NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500217#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600218 info_ptr = (png_infop)png_create_struct_2(PNG_STRUCT_INFO,
219 png_ptr->malloc_fn, png_ptr->mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500220#else
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600221 info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500222#endif
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600223 if (info_ptr != NULL)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500224 png_info_init_3(&info_ptr, png_sizeof(png_info));
Guy Schalnate5a37791996-06-05 15:50:50 -0500225
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600226 return (info_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500227}
228
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600229/* This function frees the memory associated with a single info struct.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600230 * Normally, one would use either png_destroy_read_struct() or
231 * png_destroy_write_struct() to free an info struct, but this may be
232 * useful for some applications.
233 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500234void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600235png_destroy_info_struct(png_structp png_ptr, png_infopp info_ptr_ptr)
236{
237 png_infop info_ptr = NULL;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500238 if (png_ptr == NULL)
239 return;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600240
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500241 png_debug(1, "in png_destroy_info_struct");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500242 if (info_ptr_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600243 info_ptr = *info_ptr_ptr;
244
Andreas Dilger47a0c421997-05-16 02:46:07 -0500245 if (info_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600246 {
247 png_info_destroy(png_ptr, info_ptr);
248
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500249#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500250 png_destroy_struct_2((png_voidp)info_ptr, png_ptr->free_fn,
251 png_ptr->mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500252#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600253 png_destroy_struct((png_voidp)info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500254#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500255 *info_ptr_ptr = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600256 }
257}
258
259/* Initialize the info structure. This is now an internal function (0.89)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600260 * and applications using it are urged to use png_create_info_struct()
261 * instead.
262 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500263
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500264void PNGAPI
265png_info_init_3(png_infopp ptr_ptr, png_size_t png_info_struct_size)
266{
267 png_infop info_ptr = *ptr_ptr;
268
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500269 if (info_ptr == NULL)
270 return;
Glenn Randers-Pehrson6b12c082006-11-14 10:53:30 -0600271
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500272 png_debug(1, "in png_info_init_3");
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500273
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500274 if (png_sizeof(png_info) > png_info_struct_size)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500275 {
276 png_destroy_struct(info_ptr);
277 info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
278 *ptr_ptr = info_ptr;
279 }
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500280
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500281 /* Set everything to 0 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500282 png_memset(info_ptr, 0, png_sizeof(png_info));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600283}
284
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500285#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500286void PNGAPI
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500287png_data_freer(png_structp png_ptr, png_infop info_ptr,
288 int freer, png_uint_32 mask)
289{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500290 png_debug(1, "in png_data_freer");
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500291 if (png_ptr == NULL || info_ptr == NULL)
292 return;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500293 if (freer == PNG_DESTROY_WILL_FREE_DATA)
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500294 info_ptr->free_me |= mask;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500295 else if (freer == PNG_USER_WILL_FREE_DATA)
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500296 info_ptr->free_me &= ~mask;
297 else
298 png_warning(png_ptr,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500299 "Unknown freer parameter in png_data_freer");
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500300}
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500301#endif
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500302
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500303void PNGAPI
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500304png_free_data(png_structp png_ptr, png_infop info_ptr, png_uint_32 mask,
305 int num)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600306{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500307 png_debug(1, "in png_free_data");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600308 if (png_ptr == NULL || info_ptr == NULL)
309 return;
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600310
311#if defined(PNG_TEXT_SUPPORTED)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500312 /* Free text item num or (if num == -1) all text items */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500313#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500314 if ((mask & PNG_FREE_TEXT) & info_ptr->free_me)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500315#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500316 if (mask & PNG_FREE_TEXT)
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500317#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600318 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500319 if (num != -1)
320 {
321 if (info_ptr->text && info_ptr->text[num].key)
322 {
323 png_free(png_ptr, info_ptr->text[num].key);
324 info_ptr->text[num].key = NULL;
325 }
326 }
327 else
328 {
329 int i;
330 for (i = 0; i < info_ptr->num_text; i++)
331 png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, i);
332 png_free(png_ptr, info_ptr->text);
333 info_ptr->text = NULL;
334 info_ptr->num_text=0;
335 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600336 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600337#endif
338
339#if defined(PNG_tRNS_SUPPORTED)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500340 /* Free any tRNS entry */
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500341#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500342 if ((mask & PNG_FREE_TRNS) & info_ptr->free_me)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500343#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500344 if ((mask & PNG_FREE_TRNS) && (png_ptr->flags & PNG_FLAG_FREE_TRNS))
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500345#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500346 {
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -0500347 png_free(png_ptr, info_ptr->trans_alpha);
348 info_ptr->trans_alpha = NULL;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500349 info_ptr->valid &= ~PNG_INFO_tRNS;
Glenn Randers-Pehrsonc1bfe682002-03-06 22:08:00 -0600350#ifndef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500351 png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
Glenn Randers-Pehrsonc1bfe682002-03-06 22:08:00 -0600352#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500353 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600354#endif
355
356#if defined(PNG_sCAL_SUPPORTED)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500357 /* Free any sCAL entry */
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500358#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500359 if ((mask & PNG_FREE_SCAL) & info_ptr->free_me)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500360#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500361 if (mask & PNG_FREE_SCAL)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500362#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500363 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600364#if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500365 png_free(png_ptr, info_ptr->scal_s_width);
366 png_free(png_ptr, info_ptr->scal_s_height);
367 info_ptr->scal_s_width = NULL;
368 info_ptr->scal_s_height = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600369#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500370 info_ptr->valid &= ~PNG_INFO_sCAL;
371 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600372#endif
373
374#if defined(PNG_pCAL_SUPPORTED)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500375 /* Free any pCAL entry */
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500376#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500377 if ((mask & PNG_FREE_PCAL) & info_ptr->free_me)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500378#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500379 if (mask & PNG_FREE_PCAL)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500380#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500381 {
382 png_free(png_ptr, info_ptr->pcal_purpose);
383 png_free(png_ptr, info_ptr->pcal_units);
384 info_ptr->pcal_purpose = NULL;
385 info_ptr->pcal_units = NULL;
386 if (info_ptr->pcal_params != NULL)
387 {
388 int i;
389 for (i = 0; i < (int)info_ptr->pcal_nparams; i++)
390 {
391 png_free(png_ptr, info_ptr->pcal_params[i]);
392 info_ptr->pcal_params[i]=NULL;
393 }
394 png_free(png_ptr, info_ptr->pcal_params);
395 info_ptr->pcal_params = NULL;
396 }
397 info_ptr->valid &= ~PNG_INFO_pCAL;
398 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600399#endif
400
401#if defined(PNG_iCCP_SUPPORTED)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500402 /* Free any iCCP entry */
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500403#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500404 if ((mask & PNG_FREE_ICCP) & info_ptr->free_me)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500405#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500406 if (mask & PNG_FREE_ICCP)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500407#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500408 {
409 png_free(png_ptr, info_ptr->iccp_name);
410 png_free(png_ptr, info_ptr->iccp_profile);
411 info_ptr->iccp_name = NULL;
412 info_ptr->iccp_profile = NULL;
413 info_ptr->valid &= ~PNG_INFO_iCCP;
414 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600415#endif
416
417#if defined(PNG_sPLT_SUPPORTED)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500418 /* Free a given sPLT entry, or (if num == -1) all sPLT entries */
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500419#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500420 if ((mask & PNG_FREE_SPLT) & info_ptr->free_me)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500421#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500422 if (mask & PNG_FREE_SPLT)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500423#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600424 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500425 if (num != -1)
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500426 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500427 if (info_ptr->splt_palettes)
428 {
429 png_free(png_ptr, info_ptr->splt_palettes[num].name);
430 png_free(png_ptr, info_ptr->splt_palettes[num].entries);
431 info_ptr->splt_palettes[num].name = NULL;
432 info_ptr->splt_palettes[num].entries = NULL;
433 }
434 }
435 else
436 {
437 if (info_ptr->splt_palettes_num)
438 {
439 int i;
440 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
441 png_free_data(png_ptr, info_ptr, PNG_FREE_SPLT, i);
442
443 png_free(png_ptr, info_ptr->splt_palettes);
444 info_ptr->splt_palettes = NULL;
445 info_ptr->splt_palettes_num = 0;
446 }
447 info_ptr->valid &= ~PNG_INFO_sPLT;
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500448 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600449 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600450#endif
451
452#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500453 if (png_ptr->unknown_chunk.data)
454 {
455 png_free(png_ptr, png_ptr->unknown_chunk.data);
456 png_ptr->unknown_chunk.data = NULL;
457 }
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500458
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500459#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500460 if ((mask & PNG_FREE_UNKN) & info_ptr->free_me)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500461#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500462 if (mask & PNG_FREE_UNKN)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500463#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600464 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500465 if (num != -1)
466 {
467 if (info_ptr->unknown_chunks)
468 {
469 png_free(png_ptr, info_ptr->unknown_chunks[num].data);
470 info_ptr->unknown_chunks[num].data = NULL;
471 }
472 }
473 else
474 {
475 int i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600476
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500477 if (info_ptr->unknown_chunks_num)
478 {
479 for (i = 0; i < (int)info_ptr->unknown_chunks_num; i++)
480 png_free_data(png_ptr, info_ptr, PNG_FREE_UNKN, i);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600481
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500482 png_free(png_ptr, info_ptr->unknown_chunks);
483 info_ptr->unknown_chunks = NULL;
484 info_ptr->unknown_chunks_num = 0;
485 }
486 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600487 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600488#endif
489
490#if defined(PNG_hIST_SUPPORTED)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500491 /* Free any hIST entry */
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500492#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500493 if ((mask & PNG_FREE_HIST) & info_ptr->free_me)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500494#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500495 if ((mask & PNG_FREE_HIST) && (png_ptr->flags & PNG_FLAG_FREE_HIST))
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500496#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500497 {
498 png_free(png_ptr, info_ptr->hist);
499 info_ptr->hist = NULL;
500 info_ptr->valid &= ~PNG_INFO_hIST;
Glenn Randers-Pehrsonc1bfe682002-03-06 22:08:00 -0600501#ifndef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500502 png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
Glenn Randers-Pehrsonc1bfe682002-03-06 22:08:00 -0600503#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500504 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600505#endif
506
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500507 /* Free any PLTE entry that was internally allocated */
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500508#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500509 if ((mask & PNG_FREE_PLTE) & info_ptr->free_me)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500510#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500511 if ((mask & PNG_FREE_PLTE) && (png_ptr->flags & PNG_FLAG_FREE_PLTE))
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500512#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500513 {
514 png_zfree(png_ptr, info_ptr->palette);
515 info_ptr->palette = NULL;
516 info_ptr->valid &= ~PNG_INFO_PLTE;
Glenn Randers-Pehrsonc1bfe682002-03-06 22:08:00 -0600517#ifndef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500518 png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
Glenn Randers-Pehrsonc1bfe682002-03-06 22:08:00 -0600519#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500520 info_ptr->num_palette = 0;
521 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600522
523#if defined(PNG_INFO_IMAGE_SUPPORTED)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500524 /* Free any image bits attached to the info structure */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500525#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500526 if ((mask & PNG_FREE_ROWS) & info_ptr->free_me)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500527#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500528 if (mask & PNG_FREE_ROWS)
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500529#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500530 {
531 if (info_ptr->row_pointers)
532 {
533 int row;
534 for (row = 0; row < (int)info_ptr->height; row++)
535 {
536 png_free(png_ptr, info_ptr->row_pointers[row]);
537 }
538 png_free(png_ptr, info_ptr->row_pointers);
539 }
540 info_ptr->valid &= ~PNG_INFO_IDAT;
541 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600542#endif
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500543
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500544#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500545 if (num == -1)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500546 info_ptr->free_me &= ~mask;
Glenn Randers-Pehrsonec61c232000-05-16 06:17:36 -0500547 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500548 info_ptr->free_me &= ~(mask & ~PNG_FREE_MUL);
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500549#endif
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600550}
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600551
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600552/* This is an internal routine to free any memory that the info struct is
Andreas Dilger47a0c421997-05-16 02:46:07 -0500553 * pointing to before re-using it or freeing the struct itself. Recall
554 * that png_free() checks for NULL pointers for us.
555 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500556void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600557png_info_destroy(png_structp png_ptr, png_infop info_ptr)
558{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500559 png_debug(1, "in png_info_destroy");
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600560
561 png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
Glenn Randers-Pehrsond56aca72000-11-23 11:51:42 -0600562
Glenn Randers-Pehrson7824a702009-06-13 10:05:05 -0500563#if defined(PNG_HANDLE_AS_UNKNOWN_SUPPORTED)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600564 if (png_ptr->num_chunk_list)
565 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500566 png_free(png_ptr, png_ptr->chunk_list);
567 png_ptr->num_chunk_list = 0;
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600568 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600569#endif
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600570
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500571 png_info_init_3(&info_ptr, png_sizeof(png_info));
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500572}
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600573#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
Guy Schalnat0d580581995-07-20 02:43:20 -0500574
Guy Schalnate5a37791996-06-05 15:50:50 -0500575/* This function returns a pointer to the io_ptr associated with the user
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600576 * functions. The application should free any memory associated with this
577 * pointer before png_write_destroy() or png_read_destroy() are called.
578 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500579png_voidp PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -0500580png_get_io_ptr(png_structp png_ptr)
581{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500582 if (png_ptr == NULL)
583 return (NULL);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600584 return (png_ptr->io_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500585}
Andreas Dilger47a0c421997-05-16 02:46:07 -0500586
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600587#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500588#if !defined(PNG_NO_STDIO)
589/* Initialize the default input/output functions for the PNG file. If you
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600590 * use your own read or write routines, you can call either png_set_read_fn()
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500591 * or png_set_write_fn() instead of png_init_io(). If you have defined
592 * PNG_NO_STDIO, you must use a function of your own because "FILE *" isn't
593 * necessarily available.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600594 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500595void PNGAPI
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500596png_init_io(png_structp png_ptr, png_FILE_p fp)
Guy Schalnate5a37791996-06-05 15:50:50 -0500597{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500598 png_debug(1, "in png_init_io");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500599 if (png_ptr == NULL)
600 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500601 png_ptr->io_ptr = (png_voidp)fp;
602}
Andreas Dilger47a0c421997-05-16 02:46:07 -0500603#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500604
605#if defined(PNG_TIME_RFC1123_SUPPORTED)
606/* Convert the supplied time into an RFC 1123 string suitable for use in
607 * a "Creation Time" or other text-based time string.
608 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500609png_charp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500610png_convert_to_rfc1123(png_structp png_ptr, png_timep ptime)
611{
612 static PNG_CONST char short_months[12][4] =
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600613 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
614 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500615
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500616 if (png_ptr == NULL)
617 return (NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500618 if (png_ptr->time_buffer == NULL)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500619 {
620 png_ptr->time_buffer = (png_charp)png_malloc(png_ptr, (png_uint_32)(29*
621 png_sizeof(char)));
622 }
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500623
624#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500625 {
626 char near_time_buf[29];
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500627 png_snprintf6(near_time_buf, 29, "%d %s %d %02d:%02d:%02d +0000",
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500628 ptime->day % 32, short_months[(ptime->month - 1) % 12],
629 ptime->year, ptime->hour % 24, ptime->minute % 60,
630 ptime->second % 61);
631 png_memcpy(png_ptr->time_buffer, near_time_buf,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500632 29*png_sizeof(char));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500633 }
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500634#else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500635 png_snprintf6(png_ptr->time_buffer, 29, "%d %s %d %02d:%02d:%02d +0000",
Glenn Randers-Pehrsone1eff582001-04-14 20:15:41 -0500636 ptime->day % 32, short_months[(ptime->month - 1) % 12],
637 ptime->year, ptime->hour % 24, ptime->minute % 60,
638 ptime->second % 61);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500639#endif
640 return ((png_charp)png_ptr->time_buffer);
641}
642#endif /* PNG_TIME_RFC1123_SUPPORTED */
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600643
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600644#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600645
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500646png_charp PNGAPI
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600647png_get_copyright(png_structp png_ptr)
648{
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500649 png_ptr = png_ptr; /* Silence compiler warning about unused png_ptr */
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500650#ifdef PNG_STRING_COPYRIGHT
651 return PNG_STRING_COPYRIGHT
652#else
653#ifdef __STDC__
Glenn Randers-Pehrson43aaf6e2008-08-05 22:17:03 -0500654 return ((png_charp) PNG_STRING_NEWLINE \
Glenn Randers-Pehrson866b62a2009-08-08 16:33:14 -0500655 "libpng version x 1.4.0beta74 - August 8, 2009" PNG_STRING_NEWLINE \
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600656 "Copyright (c) 1998-2009 Glenn Randers-Pehrson" PNG_STRING_NEWLINE \
Glenn Randers-Pehrson43aaf6e2008-08-05 22:17:03 -0500657 "Copyright (c) 1996-1997 Andreas Dilger" PNG_STRING_NEWLINE \
658 "Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc." \
659 PNG_STRING_NEWLINE);
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500660#else
Glenn Randers-Pehrson866b62a2009-08-08 16:33:14 -0500661 return ((png_charp) "libpng version 1.4.0beta74 - August 8, 2009\
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600662 Copyright (c) 1998-2009 Glenn Randers-Pehrson\
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500663 Copyright (c) 1996-1997 Andreas Dilger\
664 Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.");
665#endif
666#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600667}
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -0500668
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600669/* The following return the library version as a short string in the
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500670 * format 1.0.0 through 99.99.99zz. To get the version of *.h files
671 * used with your application, print out PNG_LIBPNG_VER_STRING, which
672 * is defined in png.h.
673 * Note: now there is no difference between png_get_libpng_ver() and
674 * png_get_header_ver(). Due to the version_nn_nn_nn typedef guard,
675 * it is guaranteed that png.c uses the correct version of png.h.
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600676 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500677png_charp PNGAPI
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600678png_get_libpng_ver(png_structp png_ptr)
679{
680 /* Version of *.c files used when building libpng */
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500681 png_ptr = png_ptr; /* Silence compiler warning about unused png_ptr */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500682 return ((png_charp) PNG_LIBPNG_VER_STRING);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600683}
684
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500685png_charp PNGAPI
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600686png_get_header_ver(png_structp png_ptr)
687{
688 /* Version of *.h files used when building libpng */
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500689 png_ptr = png_ptr; /* Silence compiler warning about unused png_ptr */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500690 return ((png_charp) PNG_LIBPNG_VER_STRING);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600691}
692
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500693png_charp PNGAPI
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600694png_get_header_version(png_structp png_ptr)
695{
696 /* Returns longer string containing both version and date */
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500697 png_ptr = png_ptr; /* Silence compiler warning about unused png_ptr */
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500698#ifdef __STDC__
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500699 return ((png_charp) PNG_HEADER_VERSION_STRING
700#ifndef PNG_READ_SUPPORTED
701 " (NO READ SUPPORT)"
702#endif
Glenn Randers-Pehrson43aaf6e2008-08-05 22:17:03 -0500703 PNG_STRING_NEWLINE);
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500704#else
705 return ((png_charp) PNG_HEADER_VERSION_STRING);
706#endif
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600707}
708
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600709#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600710#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsonc1bfe682002-03-06 22:08:00 -0600711int PNGAPI
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600712png_handle_as_unknown(png_structp png_ptr, png_bytep chunk_name)
713{
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500714 /* Check chunk_name and return "keep" value if it's on the list, else 0 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600715 int i;
716 png_bytep p;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500717 if (png_ptr == NULL || chunk_name == NULL || png_ptr->num_chunk_list<=0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600718 return 0;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500719 p = png_ptr->chunk_list + png_ptr->num_chunk_list*5 - 5;
720 for (i = png_ptr->num_chunk_list; i; i--, p -= 5)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600721 if (!png_memcmp(chunk_name, p, 4))
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500722 return ((int)*(p + 4));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600723 return 0;
724}
725#endif
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -0500726
727/* This function, added to libpng-1.0.6g, is untested. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500728int PNGAPI
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -0500729png_reset_zstream(png_structp png_ptr)
730{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500731 if (png_ptr == NULL)
732 return Z_STREAM_ERROR;
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -0500733 return (inflateReset(&png_ptr->zstream));
734}
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600735#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500736
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600737/* This function was added to libpng-1.0.7 */
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500738png_uint_32 PNGAPI
739png_access_version_number(void)
740{
741 /* Version of *.c files used when building libpng */
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500742 return((png_uint_32) PNG_LIBPNG_VER);
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500743}
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -0600744
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500745
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500746
747#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
748#ifdef PNG_SIZE_T
749/* Added at libpng version 1.2.6 */
750 PNG_EXTERN png_size_t PNGAPI png_convert_size PNGARG((size_t size));
751png_size_t PNGAPI
752png_convert_size(size_t size)
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500753{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500754 if (size > (png_size_t)-1)
755 PNG_ABORT(); /* We haven't got access to png_ptr, so no png_error() */
756 return ((png_size_t)size);
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500757}
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500758#endif /* PNG_SIZE_T */
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600759
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600760/* Added at libpng version 1.2.34 and 1.4.0 (moved from pngset.c) */
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600761#if defined(PNG_cHRM_SUPPORTED)
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -0600762#if !defined(PNG_NO_CHECK_cHRM)
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600763
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600764/*
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500765 * Multiply two 32-bit numbers, V1 and V2, using 32-bit
766 * arithmetic, to produce a 64 bit result in the HI/LO words.
767 *
768 * A B
769 * x C D
770 * ------
771 * AD || BD
772 * AC || CB || 0
773 *
774 * where A and B are the high and low 16-bit words of V1,
775 * C and D are the 16-bit words of V2, AD is the product of
776 * A and D, and X || Y is (X << 16) + Y.
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600777*/
778
Glenn Randers-Pehrson3f705ba2009-07-23 12:53:06 -0500779void /* PRIVATE */
780png_64bit_product (long v1, long v2, unsigned long *hi_product,
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600781 unsigned long *lo_product)
782{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500783 int a, b, c, d;
784 long lo, hi, x, y;
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600785
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500786 a = (v1 >> 16) & 0xffff;
787 b = v1 & 0xffff;
788 c = (v2 >> 16) & 0xffff;
789 d = v2 & 0xffff;
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600790
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500791 lo = b * d; /* BD */
792 x = a * d + c * b; /* AD + CB */
793 y = ((lo >> 16) & 0xffff) + x;
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600794
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500795 lo = (lo & 0xffff) | ((y & 0xffff) << 16);
796 hi = (y >> 16) & 0xffff;
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600797
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500798 hi += a * c; /* AC */
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600799
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500800 *hi_product = (unsigned long)hi;
801 *lo_product = (unsigned long)lo;
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600802}
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500803
Glenn Randers-Pehrson3f705ba2009-07-23 12:53:06 -0500804int /* PRIVATE */
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600805png_check_cHRM_fixed(png_structp png_ptr,
806 png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
807 png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
808 png_fixed_point blue_x, png_fixed_point blue_y)
809{
810 int ret = 1;
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600811 unsigned long xy_hi,xy_lo,yx_hi,yx_lo;
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600812
813 png_debug(1, "in function png_check_cHRM_fixed");
814 if (png_ptr == NULL)
815 return 0;
816
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600817 if (white_x < 0 || white_y <= 0 ||
818 red_x < 0 || red_y < 0 ||
819 green_x < 0 || green_y < 0 ||
820 blue_x < 0 || blue_y < 0)
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600821 {
822 png_warning(png_ptr,
823 "Ignoring attempt to set negative chromaticity value");
824 ret = 0;
825 }
826 if (white_x > (png_fixed_point) PNG_UINT_31_MAX ||
827 white_y > (png_fixed_point) PNG_UINT_31_MAX ||
828 red_x > (png_fixed_point) PNG_UINT_31_MAX ||
829 red_y > (png_fixed_point) PNG_UINT_31_MAX ||
830 green_x > (png_fixed_point) PNG_UINT_31_MAX ||
831 green_y > (png_fixed_point) PNG_UINT_31_MAX ||
832 blue_x > (png_fixed_point) PNG_UINT_31_MAX ||
833 blue_y > (png_fixed_point) PNG_UINT_31_MAX )
834 {
835 png_warning(png_ptr,
836 "Ignoring attempt to set chromaticity value exceeding 21474.83");
837 ret = 0;
838 }
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -0600839 if (white_x > 100000L - white_y)
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600840 {
841 png_warning(png_ptr, "Invalid cHRM white point");
842 ret = 0;
843 }
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -0600844 if (red_x > 100000L - red_y)
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600845 {
846 png_warning(png_ptr, "Invalid cHRM red point");
847 ret = 0;
848 }
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -0600849 if (green_x > 100000L - green_y)
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600850 {
851 png_warning(png_ptr, "Invalid cHRM green point");
852 ret = 0;
853 }
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -0600854 if (blue_x > 100000L - blue_y)
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600855 {
856 png_warning(png_ptr, "Invalid cHRM blue point");
857 ret = 0;
858 }
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600859
860 png_64bit_product(green_x - red_x, blue_y - red_y, &xy_hi, &xy_lo);
861 png_64bit_product(green_y - red_y, blue_x - red_x, &yx_hi, &yx_lo);
862
863 if (xy_hi == yx_hi && xy_lo == yx_lo)
864 {
865 png_warning(png_ptr,
866 "Ignoring attempt to set cHRM RGB triangle with zero area");
867 ret = 0;
868 }
869
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600870 return ret;
871}
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -0600872#endif /* NO_PNG_CHECK_cHRM */
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600873#endif /* PNG_cHRM_SUPPORTED */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500874#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */