blob: 8c4389a1440f796e518fe2da6ff05866618c4520 [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-Pehrsonacfe7dc2009-05-14 05:11:20 -05004 * Last changed in libpng 1.4.0 [May 14, 2009]
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05005 * For conditions of distribution and use, see copyright notice in png.h
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06006 * Copyright (c) 1998-2009 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05007 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06009 */
Guy Schalnat0d580581995-07-20 02:43:20 -050010
Guy Schalnat0d580581995-07-20 02:43:20 -050011#define PNG_NO_EXTERN
12#include "png.h"
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050013#include "pngpriv.h"
Guy Schalnat0d580581995-07-20 02:43:20 -050014
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -060015/* Generate a compiler error if there is an old png.h in the search path. */
Glenn Randers-Pehrson glennrp@comcast.net389c6552009-05-02 21:52:10 -050016typedef version_1_4_0beta58 Your_png_h_is_not_version_1_4_0beta58;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -060017
Andreas Dilger47a0c421997-05-16 02:46:07 -050018/* Version information for C files. This had better match the version
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -060019 * string defined in png.h. */
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -060020
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -060021#ifdef PNG_USE_GLOBAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -060022/* png_libpng_ver was changed to a function in version 1.0.5c */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050023PNG_CONST char png_libpng_ver[18] = PNG_LIBPNG_VER_STRING;
Guy Schalnat51f0eb41995-09-26 05:22:39 -050024
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060025#ifdef PNG_READ_SUPPORTED
26
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -060027/* png_sig was changed to a function in version 1.0.5c */
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060028/* Place to hold the signature string for a PNG file. */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050029PNG_CONST png_byte FARDATA png_sig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060030#endif /* PNG_READ_SUPPORTED */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060031
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -060032/* Invoke global declarations for constant strings for known chunk types */
33PNG_IHDR;
34PNG_IDAT;
35PNG_IEND;
36PNG_PLTE;
37PNG_bKGD;
38PNG_cHRM;
39PNG_gAMA;
40PNG_hIST;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060041PNG_iCCP;
42PNG_iTXt;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -060043PNG_oFFs;
44PNG_pCAL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060045PNG_sCAL;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -060046PNG_pHYs;
47PNG_sBIT;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060048PNG_sPLT;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -060049PNG_sRGB;
50PNG_tEXt;
51PNG_tIME;
52PNG_tRNS;
53PNG_zTXt;
Guy Schalnat0d580581995-07-20 02:43:20 -050054
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060055#ifdef PNG_READ_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -050056/* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
57
58/* start of interlace block */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050059PNG_CONST int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
Guy Schalnat0d580581995-07-20 02:43:20 -050060
61/* offset to next interlace block */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050062PNG_CONST int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
Guy Schalnat0d580581995-07-20 02:43:20 -050063
64/* start of interlace block in the y direction */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050065PNG_CONST int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
Guy Schalnat0d580581995-07-20 02:43:20 -050066
67/* offset to next interlace block in the y direction */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050068PNG_CONST int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
Guy Schalnat0d580581995-07-20 02:43:20 -050069
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060070/* Height of interlace block. This is not currently used - if you need
71 * it, uncomment it here and in png.h
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050072PNG_CONST int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
Guy Schalnat0d580581995-07-20 02:43:20 -050073*/
74
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060075/* Mask to determine which pixels are valid in a pass */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050076PNG_CONST int FARDATA png_pass_mask[] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
Guy Schalnat0d580581995-07-20 02:43:20 -050077
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060078/* Mask to determine which pixels to overwrite while displaying */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050079PNG_CONST int FARDATA png_pass_dsp_mask[]
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -060080 = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
Guy Schalnat0d580581995-07-20 02:43:20 -050081
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060082#endif /* PNG_READ_SUPPORTED */
Glenn Randers-Pehrson16e11662004-11-01 14:13:40 -060083#endif /* PNG_USE_GLOBAL_ARRAYS */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -060084
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060085/* Tells libpng that we have already handled the first "num_bytes" bytes
86 * of the PNG file signature. If the PNG data is embedded into another
87 * stream we can set num_bytes = 8 so that libpng will not attempt to read
88 * or write any of the magic bytes before it starts on the IHDR.
89 */
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -050090
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060091#ifdef PNG_READ_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050092void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060093png_set_sig_bytes(png_structp png_ptr, int num_bytes)
94{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050095 if (png_ptr == NULL) return;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050096 png_debug(1, "in png_set_sig_bytes");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060097 if (num_bytes > 8)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050098 png_error(png_ptr, "Too many bytes for PNG signature");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060099
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500100 png_ptr->sig_bytes = (png_byte)(num_bytes < 0 ? 0 : num_bytes);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600101}
102
103/* Checks whether the supplied bytes match the PNG signature. We allow
104 * checking less than the full 8-byte signature so that those apps that
105 * already read the first few bytes of a file to determine the file type
106 * can simply check the remaining bytes for extra assurance. Returns
107 * an integer less than, equal to, or greater than zero if sig is found,
108 * respectively, to be less than, to match, or be greater than the correct
109 * PNG signature (this is the same behaviour as strcmp, memcmp, etc).
110 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500111int PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500112png_sig_cmp(png_bytep sig, png_size_t start, png_size_t num_to_check)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600113{
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600114 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600115 if (num_to_check > 8)
116 num_to_check = 8;
117 else if (num_to_check < 1)
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600118 return (-1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600119
Andreas Dilger47a0c421997-05-16 02:46:07 -0500120 if (start > 7)
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600121 return (-1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600122
123 if (start + num_to_check > 8)
124 num_to_check = 8 - start;
125
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500126 return ((int)(png_memcmp(&sig[start], &png_signature[start], num_to_check)));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600127}
128
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600129#endif /* PNG_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500130
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600131#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500132/* Function to allocate memory for zlib and clear it to 0. */
Glenn Randers-Pehrson22f28962002-05-13 18:17:09 -0500133voidpf /* private */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500134png_zalloc(voidpf png_ptr, uInt items, uInt size)
Guy Schalnat0d580581995-07-20 02:43:20 -0500135{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500136 png_voidp ptr;
137 png_structp p=(png_structp)png_ptr;
138 png_uint_32 save_flags=p->flags;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500139 png_alloc_size_t num_bytes;
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500140
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500141 if (png_ptr == NULL) return (NULL);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500142 if (items > PNG_UINT_32_MAX/size)
143 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500144 png_warning (p, "Potential overflow in png_zalloc()");
145 return (NULL);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500146 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500147 num_bytes = (png_alloc_size_t)items * size;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600148
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500149 p->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
150 ptr = (png_voidp)png_malloc((png_structp)png_ptr, num_bytes);
151 p->flags=save_flags;
Guy Schalnat6d764711995-12-19 03:22:19 -0600152
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500153 return ((voidpf)ptr);
154}
155
156/* function to free memory for zlib */
Glenn Randers-Pehrson22f28962002-05-13 18:17:09 -0500157void /* private */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500158png_zfree(voidpf png_ptr, voidpf ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500159{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600160 png_free((png_structp)png_ptr, (png_voidp)ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500161}
162
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600163/* Reset the CRC variable to 32 bits of 1's. Care must be taken
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600164 * in case CRC is > 32 bits to leave the top bits 0.
165 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500166void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600167png_reset_crc(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500168{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600169 png_ptr->crc = crc32(0, Z_NULL, 0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500170}
171
Andreas Dilger47a0c421997-05-16 02:46:07 -0500172/* Calculate the CRC over a section of data. We can only pass as
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600173 * much data to this routine as the largest single buffer size. We
174 * also check that this data will actually be used before going to the
175 * trouble of calculating it.
176 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500177void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500178png_calculate_crc(png_structp png_ptr, png_bytep ptr, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500179{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500180 int need_crc = 1;
181
182 if (png_ptr->chunk_name[0] & 0x20) /* ancillary */
183 {
184 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
185 (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
186 need_crc = 0;
187 }
188 else /* critical */
189 {
190 if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
191 need_crc = 0;
192 }
193
194 if (need_crc)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600195 png_ptr->crc = crc32(png_ptr->crc, ptr, (uInt)length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500196}
Guy Schalnate5a37791996-06-05 15:50:50 -0500197
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600198/* Allocate the memory for an info_struct for the application. We don't
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600199 * really need the png_ptr, but it could potentially be useful in the
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500200 * future. This should be used in favour of malloc(png_sizeof(png_info))
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600201 * and png_info_init() so that applications that want to use a shared
202 * libpng don't have to be recompiled if png_info changes size.
203 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500204png_infop PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -0500205png_create_info_struct(png_structp png_ptr)
206{
207 png_infop info_ptr;
208
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500209 png_debug(1, "in png_create_info_struct");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500210 if (png_ptr == NULL) return (NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500211#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600212 info_ptr = (png_infop)png_create_struct_2(PNG_STRUCT_INFO,
213 png_ptr->malloc_fn, png_ptr->mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500214#else
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600215 info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500216#endif
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600217 if (info_ptr != NULL)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500218 png_info_init_3(&info_ptr, png_sizeof(png_info));
Guy Schalnate5a37791996-06-05 15:50:50 -0500219
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600220 return (info_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500221}
222
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600223/* This function frees the memory associated with a single info struct.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600224 * Normally, one would use either png_destroy_read_struct() or
225 * png_destroy_write_struct() to free an info struct, but this may be
226 * useful for some applications.
227 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500228void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600229png_destroy_info_struct(png_structp png_ptr, png_infopp info_ptr_ptr)
230{
231 png_infop info_ptr = NULL;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500232 if (png_ptr == NULL) return;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600233
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500234 png_debug(1, "in png_destroy_info_struct");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500235 if (info_ptr_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600236 info_ptr = *info_ptr_ptr;
237
Andreas Dilger47a0c421997-05-16 02:46:07 -0500238 if (info_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600239 {
240 png_info_destroy(png_ptr, info_ptr);
241
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500242#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500243 png_destroy_struct_2((png_voidp)info_ptr, png_ptr->free_fn,
244 png_ptr->mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500245#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600246 png_destroy_struct((png_voidp)info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500247#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500248 *info_ptr_ptr = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600249 }
250}
251
252/* Initialize the info structure. This is now an internal function (0.89)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600253 * and applications using it are urged to use png_create_info_struct()
254 * instead.
255 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500256
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500257void PNGAPI
258png_info_init_3(png_infopp ptr_ptr, png_size_t png_info_struct_size)
259{
260 png_infop info_ptr = *ptr_ptr;
261
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500262 if (info_ptr == NULL) return;
Glenn Randers-Pehrson6b12c082006-11-14 10:53:30 -0600263
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500264 png_debug(1, "in png_info_init_3");
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500265
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500266 if (png_sizeof(png_info) > png_info_struct_size)
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500267 {
268 png_destroy_struct(info_ptr);
269 info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
270 *ptr_ptr = info_ptr;
271 }
272
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500273 /* set everything to 0 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500274 png_memset(info_ptr, 0, png_sizeof(png_info));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600275}
276
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500277#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500278void PNGAPI
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500279png_data_freer(png_structp png_ptr, png_infop info_ptr,
280 int freer, png_uint_32 mask)
281{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500282 png_debug(1, "in png_data_freer");
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500283 if (png_ptr == NULL || info_ptr == NULL)
284 return;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500285 if (freer == PNG_DESTROY_WILL_FREE_DATA)
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500286 info_ptr->free_me |= mask;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500287 else if (freer == PNG_USER_WILL_FREE_DATA)
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500288 info_ptr->free_me &= ~mask;
289 else
290 png_warning(png_ptr,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500291 "Unknown freer parameter in png_data_freer");
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500292}
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500293#endif
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500294
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500295void PNGAPI
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500296png_free_data(png_structp png_ptr, png_infop info_ptr, png_uint_32 mask,
297 int num)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600298{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500299 png_debug(1, "in png_free_data");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600300 if (png_ptr == NULL || info_ptr == NULL)
301 return;
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600302
303#if defined(PNG_TEXT_SUPPORTED)
304/* free text item num or (if num == -1) all text items */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500305#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500306if ((mask & PNG_FREE_TEXT) & info_ptr->free_me)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500307#else
308if (mask & PNG_FREE_TEXT)
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500309#endif
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600310{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600311 if (num != -1)
312 {
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500313 if (info_ptr->text && info_ptr->text[num].key)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600314 {
315 png_free(png_ptr, info_ptr->text[num].key);
316 info_ptr->text[num].key = NULL;
317 }
318 }
Glenn Randers-Pehrsonec61c232000-05-16 06:17:36 -0500319 else
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600320 {
Glenn Randers-Pehrsonec61c232000-05-16 06:17:36 -0500321 int i;
322 for (i = 0; i < info_ptr->num_text; i++)
323 png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, i);
324 png_free(png_ptr, info_ptr->text);
325 info_ptr->text = NULL;
326 info_ptr->num_text=0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600327 }
328}
329#endif
330
331#if defined(PNG_tRNS_SUPPORTED)
332/* free any tRNS entry */
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500333#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500334if ((mask & PNG_FREE_TRNS) & info_ptr->free_me)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500335#else
336if ((mask & PNG_FREE_TRNS) && (png_ptr->flags & PNG_FLAG_FREE_TRNS))
337#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600338{
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500339 png_free(png_ptr, info_ptr->trans);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500340 info_ptr->trans = NULL;
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500341 info_ptr->valid &= ~PNG_INFO_tRNS;
Glenn Randers-Pehrsonc1bfe682002-03-06 22:08:00 -0600342#ifndef PNG_FREE_ME_SUPPORTED
343 png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
344#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600345}
346#endif
347
348#if defined(PNG_sCAL_SUPPORTED)
349/* free any sCAL entry */
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500350#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500351if ((mask & PNG_FREE_SCAL) & info_ptr->free_me)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500352#else
353if (mask & PNG_FREE_SCAL)
354#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600355{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600356#if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500357 png_free(png_ptr, info_ptr->scal_s_width);
358 png_free(png_ptr, info_ptr->scal_s_height);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500359 info_ptr->scal_s_width = NULL;
360 info_ptr->scal_s_height = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600361#endif
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500362 info_ptr->valid &= ~PNG_INFO_sCAL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600363}
364#endif
365
366#if defined(PNG_pCAL_SUPPORTED)
367/* free any pCAL entry */
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500368#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500369if ((mask & PNG_FREE_PCAL) & info_ptr->free_me)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500370#else
371if (mask & PNG_FREE_PCAL)
372#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600373{
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500374 png_free(png_ptr, info_ptr->pcal_purpose);
375 png_free(png_ptr, info_ptr->pcal_units);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500376 info_ptr->pcal_purpose = NULL;
377 info_ptr->pcal_units = NULL;
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500378 if (info_ptr->pcal_params != NULL)
379 {
380 int i;
381 for (i = 0; i < (int)info_ptr->pcal_nparams; i++)
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500382 {
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500383 png_free(png_ptr, info_ptr->pcal_params[i]);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500384 info_ptr->pcal_params[i]=NULL;
385 }
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500386 png_free(png_ptr, info_ptr->pcal_params);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500387 info_ptr->pcal_params = NULL;
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500388 }
389 info_ptr->valid &= ~PNG_INFO_pCAL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600390}
391#endif
392
393#if defined(PNG_iCCP_SUPPORTED)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600394/* free any iCCP entry */
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500395#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500396if ((mask & PNG_FREE_ICCP) & info_ptr->free_me)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500397#else
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600398if (mask & PNG_FREE_ICCP)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500399#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600400{
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500401 png_free(png_ptr, info_ptr->iccp_name);
402 png_free(png_ptr, info_ptr->iccp_profile);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500403 info_ptr->iccp_name = NULL;
404 info_ptr->iccp_profile = NULL;
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500405 info_ptr->valid &= ~PNG_INFO_iCCP;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600406}
407#endif
408
409#if defined(PNG_sPLT_SUPPORTED)
410/* free a given sPLT entry, or (if num == -1) all sPLT entries */
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500411#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500412if ((mask & PNG_FREE_SPLT) & info_ptr->free_me)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500413#else
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600414if (mask & PNG_FREE_SPLT)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500415#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600416{
417 if (num != -1)
418 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500419 if (info_ptr->splt_palettes)
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500420 {
421 png_free(png_ptr, info_ptr->splt_palettes[num].name);
422 png_free(png_ptr, info_ptr->splt_palettes[num].entries);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500423 info_ptr->splt_palettes[num].name = NULL;
424 info_ptr->splt_palettes[num].entries = NULL;
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500425 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600426 }
427 else
428 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500429 if (info_ptr->splt_palettes_num)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600430 {
431 int i;
432 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
433 png_free_data(png_ptr, info_ptr, PNG_FREE_SPLT, i);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600434
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600435 png_free(png_ptr, info_ptr->splt_palettes);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500436 info_ptr->splt_palettes = NULL;
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600437 info_ptr->splt_palettes_num = 0;
438 }
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500439 info_ptr->valid &= ~PNG_INFO_sPLT;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600440 }
441}
442#endif
443
444#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500445 if (png_ptr->unknown_chunk.data)
446 {
447 png_free(png_ptr, png_ptr->unknown_chunk.data);
448 png_ptr->unknown_chunk.data = NULL;
449 }
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500450
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500451#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500452if ((mask & PNG_FREE_UNKN) & info_ptr->free_me)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500453#else
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600454if (mask & PNG_FREE_UNKN)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500455#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600456{
457 if (num != -1)
458 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500459 if (info_ptr->unknown_chunks)
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500460 {
461 png_free(png_ptr, info_ptr->unknown_chunks[num].data);
462 info_ptr->unknown_chunks[num].data = NULL;
463 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600464 }
465 else
466 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600467 int i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600468
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500469 if (info_ptr->unknown_chunks_num)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600470 {
471 for (i = 0; i < (int)info_ptr->unknown_chunks_num; i++)
472 png_free_data(png_ptr, info_ptr, PNG_FREE_UNKN, i);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600473
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600474 png_free(png_ptr, info_ptr->unknown_chunks);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500475 info_ptr->unknown_chunks = NULL;
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600476 info_ptr->unknown_chunks_num = 0;
477 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600478 }
479}
480#endif
481
482#if defined(PNG_hIST_SUPPORTED)
483/* free any hIST entry */
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500484#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500485if ((mask & PNG_FREE_HIST) & info_ptr->free_me)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500486#else
487if ((mask & PNG_FREE_HIST) && (png_ptr->flags & PNG_FLAG_FREE_HIST))
488#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600489{
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500490 png_free(png_ptr, info_ptr->hist);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500491 info_ptr->hist = NULL;
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500492 info_ptr->valid &= ~PNG_INFO_hIST;
Glenn Randers-Pehrsonc1bfe682002-03-06 22:08:00 -0600493#ifndef PNG_FREE_ME_SUPPORTED
494 png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
495#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600496}
497#endif
498
499/* free any PLTE entry that was internally allocated */
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500500#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500501if ((mask & PNG_FREE_PLTE) & info_ptr->free_me)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500502#else
503if ((mask & PNG_FREE_PLTE) && (png_ptr->flags & PNG_FLAG_FREE_PLTE))
504#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600505{
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500506 png_zfree(png_ptr, info_ptr->palette);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500507 info_ptr->palette = NULL;
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500508 info_ptr->valid &= ~PNG_INFO_PLTE;
Glenn Randers-Pehrsonc1bfe682002-03-06 22:08:00 -0600509#ifndef PNG_FREE_ME_SUPPORTED
510 png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
511#endif
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500512 info_ptr->num_palette = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600513}
514
515#if defined(PNG_INFO_IMAGE_SUPPORTED)
516/* free any image bits attached to the info structure */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500517#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500518if ((mask & PNG_FREE_ROWS) & info_ptr->free_me)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500519#else
520if (mask & PNG_FREE_ROWS)
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500521#endif
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500522{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500523 if (info_ptr->row_pointers)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500524 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600525 int row;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600526 for (row = 0; row < (int)info_ptr->height; row++)
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500527 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600528 png_free(png_ptr, info_ptr->row_pointers[row]);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500529 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600530 png_free(png_ptr, info_ptr->row_pointers);
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500531 }
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500532 info_ptr->valid &= ~PNG_INFO_IDAT;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600533}
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600534#endif
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500535
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500536#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500537 if (num == -1)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600538 info_ptr->free_me &= ~mask;
Glenn Randers-Pehrsonec61c232000-05-16 06:17:36 -0500539 else
540 info_ptr->free_me &= ~(mask & ~PNG_FREE_MUL);
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500541#endif
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600542}
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600543
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600544/* This is an internal routine to free any memory that the info struct is
Andreas Dilger47a0c421997-05-16 02:46:07 -0500545 * pointing to before re-using it or freeing the struct itself. Recall
546 * that png_free() checks for NULL pointers for us.
547 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500548void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600549png_info_destroy(png_structp png_ptr, png_infop info_ptr)
550{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500551 png_debug(1, "in png_info_destroy");
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600552
553 png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
Glenn Randers-Pehrsond56aca72000-11-23 11:51:42 -0600554
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500555#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600556 if (png_ptr->num_chunk_list)
557 {
558 png_free(png_ptr, png_ptr->chunk_list);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500559 png_ptr->num_chunk_list = 0;
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600560 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600561#endif
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600562
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500563 png_info_init_3(&info_ptr, png_sizeof(png_info));
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500564}
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600565#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
Guy Schalnat0d580581995-07-20 02:43:20 -0500566
Guy Schalnate5a37791996-06-05 15:50:50 -0500567/* This function returns a pointer to the io_ptr associated with the user
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600568 * functions. The application should free any memory associated with this
569 * pointer before png_write_destroy() or png_read_destroy() are called.
570 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500571png_voidp PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -0500572png_get_io_ptr(png_structp png_ptr)
573{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500574 if (png_ptr == NULL) return (NULL);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600575 return (png_ptr->io_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500576}
Andreas Dilger47a0c421997-05-16 02:46:07 -0500577
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600578#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500579#if !defined(PNG_NO_STDIO)
580/* Initialize the default input/output functions for the PNG file. If you
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600581 * use your own read or write routines, you can call either png_set_read_fn()
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500582 * or png_set_write_fn() instead of png_init_io(). If you have defined
583 * PNG_NO_STDIO, you must use a function of your own because "FILE *" isn't
584 * necessarily available.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600585 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500586void PNGAPI
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500587png_init_io(png_structp png_ptr, png_FILE_p fp)
Guy Schalnate5a37791996-06-05 15:50:50 -0500588{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500589 png_debug(1, "in png_init_io");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500590 if (png_ptr == NULL) return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500591 png_ptr->io_ptr = (png_voidp)fp;
592}
Andreas Dilger47a0c421997-05-16 02:46:07 -0500593#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500594
595#if defined(PNG_TIME_RFC1123_SUPPORTED)
596/* Convert the supplied time into an RFC 1123 string suitable for use in
597 * a "Creation Time" or other text-based time string.
598 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500599png_charp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500600png_convert_to_rfc1123(png_structp png_ptr, png_timep ptime)
601{
602 static PNG_CONST char short_months[12][4] =
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600603 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
604 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500605
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500606 if (png_ptr == NULL) return (NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500607 if (png_ptr->time_buffer == NULL)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500608 {
609 png_ptr->time_buffer = (png_charp)png_malloc(png_ptr, (png_uint_32)(29*
610 png_sizeof(char)));
611 }
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500612
613#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500614 {
615 char near_time_buf[29];
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500616 png_snprintf6(near_time_buf, 29, "%d %s %d %02d:%02d:%02d +0000",
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500617 ptime->day % 32, short_months[(ptime->month - 1) % 12],
618 ptime->year, ptime->hour % 24, ptime->minute % 60,
619 ptime->second % 61);
620 png_memcpy(png_ptr->time_buffer, near_time_buf,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500621 29*png_sizeof(char));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500622 }
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500623#else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500624 png_snprintf6(png_ptr->time_buffer, 29, "%d %s %d %02d:%02d:%02d +0000",
Glenn Randers-Pehrsone1eff582001-04-14 20:15:41 -0500625 ptime->day % 32, short_months[(ptime->month - 1) % 12],
626 ptime->year, ptime->hour % 24, ptime->minute % 60,
627 ptime->second % 61);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500628#endif
629 return ((png_charp)png_ptr->time_buffer);
630}
631#endif /* PNG_TIME_RFC1123_SUPPORTED */
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600632
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600633#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600634
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500635png_charp PNGAPI
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600636png_get_copyright(png_structp png_ptr)
637{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500638 png_ptr = png_ptr; /* silence compiler warning about unused png_ptr */
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500639#ifdef PNG_STRING_COPYRIGHT
640 return PNG_STRING_COPYRIGHT
641#else
642#ifdef __STDC__
Glenn Randers-Pehrson43aaf6e2008-08-05 22:17:03 -0500643 return ((png_charp) PNG_STRING_NEWLINE \
Glenn Randers-Pehrsonacfe7dc2009-05-14 05:11:20 -0500644 "libpng version x 1.4.0beta58 - May 14, 2009" PNG_STRING_NEWLINE \
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600645 "Copyright (c) 1998-2009 Glenn Randers-Pehrson" PNG_STRING_NEWLINE \
Glenn Randers-Pehrson43aaf6e2008-08-05 22:17:03 -0500646 "Copyright (c) 1996-1997 Andreas Dilger" PNG_STRING_NEWLINE \
647 "Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc." \
648 PNG_STRING_NEWLINE);
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500649#else
Glenn Randers-Pehrsonacfe7dc2009-05-14 05:11:20 -0500650 return ((png_charp) "libpng version 1.4.0beta58 - May 14, 2009\
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600651 Copyright (c) 1998-2009 Glenn Randers-Pehrson\
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500652 Copyright (c) 1996-1997 Andreas Dilger\
653 Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.");
654#endif
655#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600656}
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -0500657
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600658/* The following return the library version as a short string in the
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500659 * format 1.0.0 through 99.99.99zz. To get the version of *.h files
660 * used with your application, print out PNG_LIBPNG_VER_STRING, which
661 * is defined in png.h.
662 * Note: now there is no difference between png_get_libpng_ver() and
663 * png_get_header_ver(). Due to the version_nn_nn_nn typedef guard,
664 * it is guaranteed that png.c uses the correct version of png.h.
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600665 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500666png_charp PNGAPI
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600667png_get_libpng_ver(png_structp png_ptr)
668{
669 /* Version of *.c files used when building libpng */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500670 png_ptr = png_ptr; /* silence compiler warning about unused png_ptr */
671 return ((png_charp) PNG_LIBPNG_VER_STRING);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600672}
673
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500674png_charp PNGAPI
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600675png_get_header_ver(png_structp png_ptr)
676{
677 /* Version of *.h files used when building libpng */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500678 png_ptr = png_ptr; /* silence compiler warning about unused png_ptr */
679 return ((png_charp) PNG_LIBPNG_VER_STRING);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600680}
681
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500682png_charp PNGAPI
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600683png_get_header_version(png_structp png_ptr)
684{
685 /* Returns longer string containing both version and date */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500686 png_ptr = png_ptr; /* silence compiler warning about unused png_ptr */
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500687#ifdef __STDC__
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500688 return ((png_charp) PNG_HEADER_VERSION_STRING
689#ifndef PNG_READ_SUPPORTED
690 " (NO READ SUPPORT)"
691#endif
Glenn Randers-Pehrson43aaf6e2008-08-05 22:17:03 -0500692 PNG_STRING_NEWLINE);
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500693#else
694 return ((png_charp) PNG_HEADER_VERSION_STRING);
695#endif
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600696}
697
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600698#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600699#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsonc1bfe682002-03-06 22:08:00 -0600700int PNGAPI
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600701png_handle_as_unknown(png_structp png_ptr, png_bytep chunk_name)
702{
703 /* check chunk_name and return "keep" value if it's on the list, else 0 */
704 int i;
705 png_bytep p;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500706 if (png_ptr == NULL || chunk_name == NULL || png_ptr->num_chunk_list<=0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600707 return 0;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500708 p = png_ptr->chunk_list + png_ptr->num_chunk_list*5 - 5;
709 for (i = png_ptr->num_chunk_list; i; i--, p -= 5)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600710 if (!png_memcmp(chunk_name, p, 4))
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500711 return ((int)*(p + 4));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600712 return 0;
713}
714#endif
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -0500715
716/* This function, added to libpng-1.0.6g, is untested. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500717int PNGAPI
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -0500718png_reset_zstream(png_structp png_ptr)
719{
Glenn Randers-Pehrson6b12c082006-11-14 10:53:30 -0600720 if (png_ptr == NULL) return Z_STREAM_ERROR;
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -0500721 return (inflateReset(&png_ptr->zstream));
722}
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600723#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500724
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600725/* This function was added to libpng-1.0.7 */
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500726png_uint_32 PNGAPI
727png_access_version_number(void)
728{
729 /* Version of *.c files used when building libpng */
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500730 return((png_uint_32) PNG_LIBPNG_VER);
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500731}
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -0600732
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500733
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500734
735#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
736#ifdef PNG_SIZE_T
737/* Added at libpng version 1.2.6 */
738 PNG_EXTERN png_size_t PNGAPI png_convert_size PNGARG((size_t size));
739png_size_t PNGAPI
740png_convert_size(size_t size)
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500741{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500742 if (size > (png_size_t)-1)
743 PNG_ABORT(); /* We haven't got access to png_ptr, so no png_error() */
744 return ((png_size_t)size);
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500745}
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500746#endif /* PNG_SIZE_T */
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600747
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600748/* Added at libpng version 1.2.34 and 1.4.0 (moved from pngset.c) */
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600749#if defined(PNG_cHRM_SUPPORTED)
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -0600750#if !defined(PNG_NO_CHECK_cHRM)
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600751
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600752/*
753 Multiply two 32-bit numbers, V1 and V2, using 32-bit
754 arithmetic, to produce a 64 bit result in the HI/LO words.
755
756 A B
757 x C D
758 ------
759 AD || BD
760AC || CB || 0
761
762 where A and B are the high and low 16-bit words of V1,
763 C and D are the 16-bit words of V2, AD is the product of
764 A and D, and X || Y is (X << 16) + Y.
765*/
766
767void png_64bit_product (long v1, long v2, unsigned long *hi_product,
768 unsigned long *lo_product)
769{
770 int a, b, c, d;
771 long lo, hi, x, y;
772
773 a = (v1 >> 16) & 0xffff;
774 b = v1 & 0xffff;
775 c = (v2 >> 16) & 0xffff;
776 d = v2 & 0xffff;
777
778 lo = b * d; /* BD */
779 x = a * d + c * b; /* AD + CB */
780 y = ((lo >> 16) & 0xffff) + x;
781
782 lo = (lo & 0xffff) | ((y & 0xffff) << 16);
783 hi = (y >> 16) & 0xffff;
784
785 hi += a * c; /* AC */
786
787 *hi_product = (unsigned long)hi;
788 *lo_product = (unsigned long)lo;
789}
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600790int /* private */
791png_check_cHRM_fixed(png_structp png_ptr,
792 png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
793 png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
794 png_fixed_point blue_x, png_fixed_point blue_y)
795{
796 int ret = 1;
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600797 unsigned long xy_hi,xy_lo,yx_hi,yx_lo;
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600798
799 png_debug(1, "in function png_check_cHRM_fixed");
800 if (png_ptr == NULL)
801 return 0;
802
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600803 if (white_x < 0 || white_y <= 0 ||
804 red_x < 0 || red_y < 0 ||
805 green_x < 0 || green_y < 0 ||
806 blue_x < 0 || blue_y < 0)
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600807 {
808 png_warning(png_ptr,
809 "Ignoring attempt to set negative chromaticity value");
810 ret = 0;
811 }
812 if (white_x > (png_fixed_point) PNG_UINT_31_MAX ||
813 white_y > (png_fixed_point) PNG_UINT_31_MAX ||
814 red_x > (png_fixed_point) PNG_UINT_31_MAX ||
815 red_y > (png_fixed_point) PNG_UINT_31_MAX ||
816 green_x > (png_fixed_point) PNG_UINT_31_MAX ||
817 green_y > (png_fixed_point) PNG_UINT_31_MAX ||
818 blue_x > (png_fixed_point) PNG_UINT_31_MAX ||
819 blue_y > (png_fixed_point) PNG_UINT_31_MAX )
820 {
821 png_warning(png_ptr,
822 "Ignoring attempt to set chromaticity value exceeding 21474.83");
823 ret = 0;
824 }
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -0600825 if (white_x > 100000L - white_y)
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600826 {
827 png_warning(png_ptr, "Invalid cHRM white point");
828 ret = 0;
829 }
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -0600830 if (red_x > 100000L - red_y)
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600831 {
832 png_warning(png_ptr, "Invalid cHRM red point");
833 ret = 0;
834 }
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -0600835 if (green_x > 100000L - green_y)
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600836 {
837 png_warning(png_ptr, "Invalid cHRM green point");
838 ret = 0;
839 }
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -0600840 if (blue_x > 100000L - blue_y)
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600841 {
842 png_warning(png_ptr, "Invalid cHRM blue point");
843 ret = 0;
844 }
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600845
846 png_64bit_product(green_x - red_x, blue_y - red_y, &xy_hi, &xy_lo);
847 png_64bit_product(green_y - red_y, blue_x - red_x, &yx_hi, &yx_lo);
848
849 if (xy_hi == yx_hi && xy_lo == yx_lo)
850 {
851 png_warning(png_ptr,
852 "Ignoring attempt to set cHRM RGB triangle with zero area");
853 ret = 0;
854 }
855
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600856 return ret;
857}
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -0600858#endif /* NO_PNG_CHECK_cHRM */
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600859#endif /* PNG_cHRM_SUPPORTED */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500860#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */