blob: 9e4d644c84a064068f712dedbb44544676f1731c [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-Pehrsondbab08f2009-05-18 16:58:37 -05004 * Last changed in libpng 1.4.0 [May 18, 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-Pehrson76546822009-05-16 07:25:42 -050016typedef version_1_4_0beta60 Your_png_h_is_not_version_1_4_0beta60;
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-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050095 if (png_ptr == NULL)
96 return;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050097 png_debug(1, "in png_set_sig_bytes");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060098 if (num_bytes > 8)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050099 png_error(png_ptr, "Too many bytes for PNG signature");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600100
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500101 png_ptr->sig_bytes = (png_byte)(num_bytes < 0 ? 0 : num_bytes);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600102}
103
104/* Checks whether the supplied bytes match the PNG signature. We allow
105 * checking less than the full 8-byte signature so that those apps that
106 * already read the first few bytes of a file to determine the file type
107 * can simply check the remaining bytes for extra assurance. Returns
108 * an integer less than, equal to, or greater than zero if sig is found,
109 * respectively, to be less than, to match, or be greater than the correct
110 * PNG signature (this is the same behaviour as strcmp, memcmp, etc).
111 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500112int PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500113png_sig_cmp(png_bytep sig, png_size_t start, png_size_t num_to_check)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600114{
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600115 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600116 if (num_to_check > 8)
117 num_to_check = 8;
118 else if (num_to_check < 1)
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600119 return (-1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600120
Andreas Dilger47a0c421997-05-16 02:46:07 -0500121 if (start > 7)
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600122 return (-1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600123
124 if (start + num_to_check > 8)
125 num_to_check = 8 - start;
126
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500127 return ((int)(png_memcmp(&sig[start], &png_signature[start], num_to_check)));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600128}
129
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600130#endif /* PNG_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500131
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600132#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500133/* Function to allocate memory for zlib and clear it to 0. */
Glenn Randers-Pehrson22f28962002-05-13 18:17:09 -0500134voidpf /* private */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500135png_zalloc(voidpf png_ptr, uInt items, uInt size)
Guy Schalnat0d580581995-07-20 02:43:20 -0500136{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500137 png_voidp ptr;
138 png_structp p=(png_structp)png_ptr;
139 png_uint_32 save_flags=p->flags;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500140 png_alloc_size_t num_bytes;
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500141
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500142 if (png_ptr == NULL)
143 return (NULL);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500144 if (items > PNG_UINT_32_MAX/size)
145 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500146 png_warning (p, "Potential overflow in png_zalloc()");
147 return (NULL);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500148 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500149 num_bytes = (png_alloc_size_t)items * size;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600150
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500151 p->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
152 ptr = (png_voidp)png_malloc((png_structp)png_ptr, num_bytes);
153 p->flags=save_flags;
Guy Schalnat6d764711995-12-19 03:22:19 -0600154
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500155 return ((voidpf)ptr);
156}
157
158/* function to free memory for zlib */
Glenn Randers-Pehrson22f28962002-05-13 18:17:09 -0500159void /* private */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500160png_zfree(voidpf png_ptr, voidpf ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500161{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600162 png_free((png_structp)png_ptr, (png_voidp)ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500163}
164
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600165/* Reset the CRC variable to 32 bits of 1's. Care must be taken
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600166 * in case CRC is > 32 bits to leave the top bits 0.
167 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500168void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600169png_reset_crc(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500170{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600171 png_ptr->crc = crc32(0, Z_NULL, 0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500172}
173
Andreas Dilger47a0c421997-05-16 02:46:07 -0500174/* Calculate the CRC over a section of data. We can only pass as
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600175 * much data to this routine as the largest single buffer size. We
176 * also check that this data will actually be used before going to the
177 * trouble of calculating it.
178 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500179void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500180png_calculate_crc(png_structp png_ptr, png_bytep ptr, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500181{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500182 int need_crc = 1;
183
184 if (png_ptr->chunk_name[0] & 0x20) /* ancillary */
185 {
186 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
187 (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
188 need_crc = 0;
189 }
190 else /* critical */
191 {
192 if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
193 need_crc = 0;
194 }
195
196 if (need_crc)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600197 png_ptr->crc = crc32(png_ptr->crc, ptr, (uInt)length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500198}
Guy Schalnate5a37791996-06-05 15:50:50 -0500199
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600200/* Allocate the memory for an info_struct for the application. We don't
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600201 * really need the png_ptr, but it could potentially be useful in the
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500202 * future. This should be used in favour of malloc(png_sizeof(png_info))
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600203 * and png_info_init() so that applications that want to use a shared
204 * libpng don't have to be recompiled if png_info changes size.
205 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500206png_infop PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -0500207png_create_info_struct(png_structp png_ptr)
208{
209 png_infop info_ptr;
210
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500211 png_debug(1, "in png_create_info_struct");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500212 if (png_ptr == NULL)
213 return (NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500214#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600215 info_ptr = (png_infop)png_create_struct_2(PNG_STRUCT_INFO,
216 png_ptr->malloc_fn, png_ptr->mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500217#else
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600218 info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500219#endif
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600220 if (info_ptr != NULL)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500221 png_info_init_3(&info_ptr, png_sizeof(png_info));
Guy Schalnate5a37791996-06-05 15:50:50 -0500222
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600223 return (info_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500224}
225
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600226/* This function frees the memory associated with a single info struct.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600227 * Normally, one would use either png_destroy_read_struct() or
228 * png_destroy_write_struct() to free an info struct, but this may be
229 * useful for some applications.
230 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500231void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600232png_destroy_info_struct(png_structp png_ptr, png_infopp info_ptr_ptr)
233{
234 png_infop info_ptr = NULL;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500235 if (png_ptr == NULL)
236 return;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600237
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500238 png_debug(1, "in png_destroy_info_struct");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500239 if (info_ptr_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600240 info_ptr = *info_ptr_ptr;
241
Andreas Dilger47a0c421997-05-16 02:46:07 -0500242 if (info_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600243 {
244 png_info_destroy(png_ptr, info_ptr);
245
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500246#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500247 png_destroy_struct_2((png_voidp)info_ptr, png_ptr->free_fn,
248 png_ptr->mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500249#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600250 png_destroy_struct((png_voidp)info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500251#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500252 *info_ptr_ptr = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600253 }
254}
255
256/* Initialize the info structure. This is now an internal function (0.89)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600257 * and applications using it are urged to use png_create_info_struct()
258 * instead.
259 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500260
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500261void PNGAPI
262png_info_init_3(png_infopp ptr_ptr, png_size_t png_info_struct_size)
263{
264 png_infop info_ptr = *ptr_ptr;
265
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500266 if (info_ptr == NULL)
267 return;
Glenn Randers-Pehrson6b12c082006-11-14 10:53:30 -0600268
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500269 png_debug(1, "in png_info_init_3");
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500270
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500271 if (png_sizeof(png_info) > png_info_struct_size)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500272 {
273 png_destroy_struct(info_ptr);
274 info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
275 *ptr_ptr = info_ptr;
276 }
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500277
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500278 /* set everything to 0 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500279 png_memset(info_ptr, 0, png_sizeof(png_info));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600280}
281
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500282#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500283void PNGAPI
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500284png_data_freer(png_structp png_ptr, png_infop info_ptr,
285 int freer, png_uint_32 mask)
286{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500287 png_debug(1, "in png_data_freer");
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500288 if (png_ptr == NULL || info_ptr == NULL)
289 return;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500290 if (freer == PNG_DESTROY_WILL_FREE_DATA)
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500291 info_ptr->free_me |= mask;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500292 else if (freer == PNG_USER_WILL_FREE_DATA)
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500293 info_ptr->free_me &= ~mask;
294 else
295 png_warning(png_ptr,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500296 "Unknown freer parameter in png_data_freer");
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500297}
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500298#endif
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500299
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500300void PNGAPI
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500301png_free_data(png_structp png_ptr, png_infop info_ptr, png_uint_32 mask,
302 int num)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600303{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500304 png_debug(1, "in png_free_data");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600305 if (png_ptr == NULL || info_ptr == NULL)
306 return;
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600307
308#if defined(PNG_TEXT_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500309 /* free text item num or (if num == -1) all text items */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500310#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500311 if ((mask & PNG_FREE_TEXT) & info_ptr->free_me)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500312#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500313 if (mask & PNG_FREE_TEXT)
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500314#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600315 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500316 if (num != -1)
317 {
318 if (info_ptr->text && info_ptr->text[num].key)
319 {
320 png_free(png_ptr, info_ptr->text[num].key);
321 info_ptr->text[num].key = NULL;
322 }
323 }
324 else
325 {
326 int i;
327 for (i = 0; i < info_ptr->num_text; i++)
328 png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, i);
329 png_free(png_ptr, info_ptr->text);
330 info_ptr->text = NULL;
331 info_ptr->num_text=0;
332 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600333 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600334#endif
335
336#if defined(PNG_tRNS_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500337 /* free any tRNS entry */
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500338#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500339 if ((mask & PNG_FREE_TRNS) & info_ptr->free_me)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500340#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500341 if ((mask & PNG_FREE_TRNS) && (png_ptr->flags & PNG_FLAG_FREE_TRNS))
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500342#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500343 {
344 png_free(png_ptr, info_ptr->trans);
345 info_ptr->trans = NULL;
346 info_ptr->valid &= ~PNG_INFO_tRNS;
Glenn Randers-Pehrsonc1bfe682002-03-06 22:08:00 -0600347#ifndef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500348 png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
Glenn Randers-Pehrsonc1bfe682002-03-06 22:08:00 -0600349#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500350 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600351#endif
352
353#if defined(PNG_sCAL_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500354 /* free any sCAL entry */
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500355#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500356 if ((mask & PNG_FREE_SCAL) & info_ptr->free_me)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500357#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500358 if (mask & PNG_FREE_SCAL)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500359#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500360 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600361#if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500362 png_free(png_ptr, info_ptr->scal_s_width);
363 png_free(png_ptr, info_ptr->scal_s_height);
364 info_ptr->scal_s_width = NULL;
365 info_ptr->scal_s_height = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600366#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500367 info_ptr->valid &= ~PNG_INFO_sCAL;
368 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600369#endif
370
371#if defined(PNG_pCAL_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500372 /* free any pCAL entry */
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500373#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500374 if ((mask & PNG_FREE_PCAL) & info_ptr->free_me)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500375#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500376 if (mask & PNG_FREE_PCAL)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500377#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500378 {
379 png_free(png_ptr, info_ptr->pcal_purpose);
380 png_free(png_ptr, info_ptr->pcal_units);
381 info_ptr->pcal_purpose = NULL;
382 info_ptr->pcal_units = NULL;
383 if (info_ptr->pcal_params != NULL)
384 {
385 int i;
386 for (i = 0; i < (int)info_ptr->pcal_nparams; i++)
387 {
388 png_free(png_ptr, info_ptr->pcal_params[i]);
389 info_ptr->pcal_params[i]=NULL;
390 }
391 png_free(png_ptr, info_ptr->pcal_params);
392 info_ptr->pcal_params = NULL;
393 }
394 info_ptr->valid &= ~PNG_INFO_pCAL;
395 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600396#endif
397
398#if defined(PNG_iCCP_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500399 /* free any iCCP entry */
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500400#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500401 if ((mask & PNG_FREE_ICCP) & info_ptr->free_me)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500402#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500403 if (mask & PNG_FREE_ICCP)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500404#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500405 {
406 png_free(png_ptr, info_ptr->iccp_name);
407 png_free(png_ptr, info_ptr->iccp_profile);
408 info_ptr->iccp_name = NULL;
409 info_ptr->iccp_profile = NULL;
410 info_ptr->valid &= ~PNG_INFO_iCCP;
411 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600412#endif
413
414#if defined(PNG_sPLT_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500415 /* free a given sPLT entry, or (if num == -1) all sPLT entries */
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500416#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500417 if ((mask & PNG_FREE_SPLT) & info_ptr->free_me)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500418#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500419 if (mask & PNG_FREE_SPLT)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500420#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600421 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500422 if (num != -1)
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500423 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500424 if (info_ptr->splt_palettes)
425 {
426 png_free(png_ptr, info_ptr->splt_palettes[num].name);
427 png_free(png_ptr, info_ptr->splt_palettes[num].entries);
428 info_ptr->splt_palettes[num].name = NULL;
429 info_ptr->splt_palettes[num].entries = NULL;
430 }
431 }
432 else
433 {
434 if (info_ptr->splt_palettes_num)
435 {
436 int i;
437 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
438 png_free_data(png_ptr, info_ptr, PNG_FREE_SPLT, i);
439
440 png_free(png_ptr, info_ptr->splt_palettes);
441 info_ptr->splt_palettes = NULL;
442 info_ptr->splt_palettes_num = 0;
443 }
444 info_ptr->valid &= ~PNG_INFO_sPLT;
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500445 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600446 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600447#endif
448
449#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500450 if (png_ptr->unknown_chunk.data)
451 {
452 png_free(png_ptr, png_ptr->unknown_chunk.data);
453 png_ptr->unknown_chunk.data = NULL;
454 }
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500455
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500456#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500457 if ((mask & PNG_FREE_UNKN) & info_ptr->free_me)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500458#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500459 if (mask & PNG_FREE_UNKN)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500460#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600461 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500462 if (num != -1)
463 {
464 if (info_ptr->unknown_chunks)
465 {
466 png_free(png_ptr, info_ptr->unknown_chunks[num].data);
467 info_ptr->unknown_chunks[num].data = NULL;
468 }
469 }
470 else
471 {
472 int i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600473
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500474 if (info_ptr->unknown_chunks_num)
475 {
476 for (i = 0; i < (int)info_ptr->unknown_chunks_num; i++)
477 png_free_data(png_ptr, info_ptr, PNG_FREE_UNKN, i);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600478
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500479 png_free(png_ptr, info_ptr->unknown_chunks);
480 info_ptr->unknown_chunks = NULL;
481 info_ptr->unknown_chunks_num = 0;
482 }
483 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600484 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600485#endif
486
487#if defined(PNG_hIST_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500488 /* free any hIST entry */
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500489#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500490 if ((mask & PNG_FREE_HIST) & info_ptr->free_me)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500491#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500492 if ((mask & PNG_FREE_HIST) && (png_ptr->flags & PNG_FLAG_FREE_HIST))
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500493#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500494 {
495 png_free(png_ptr, info_ptr->hist);
496 info_ptr->hist = NULL;
497 info_ptr->valid &= ~PNG_INFO_hIST;
Glenn Randers-Pehrsonc1bfe682002-03-06 22:08:00 -0600498#ifndef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500499 png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
Glenn Randers-Pehrsonc1bfe682002-03-06 22:08:00 -0600500#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500501 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600502#endif
503
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500504 /* free any PLTE entry that was internally allocated */
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500505#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500506 if ((mask & PNG_FREE_PLTE) & info_ptr->free_me)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500507#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500508 if ((mask & PNG_FREE_PLTE) && (png_ptr->flags & PNG_FLAG_FREE_PLTE))
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500509#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500510 {
511 png_zfree(png_ptr, info_ptr->palette);
512 info_ptr->palette = NULL;
513 info_ptr->valid &= ~PNG_INFO_PLTE;
Glenn Randers-Pehrsonc1bfe682002-03-06 22:08:00 -0600514#ifndef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500515 png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
Glenn Randers-Pehrsonc1bfe682002-03-06 22:08:00 -0600516#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500517 info_ptr->num_palette = 0;
518 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600519
520#if defined(PNG_INFO_IMAGE_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500521 /* free any image bits attached to the info structure */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500522#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500523 if ((mask & PNG_FREE_ROWS) & info_ptr->free_me)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500524#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500525 if (mask & PNG_FREE_ROWS)
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500526#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500527 {
528 if (info_ptr->row_pointers)
529 {
530 int row;
531 for (row = 0; row < (int)info_ptr->height; row++)
532 {
533 png_free(png_ptr, info_ptr->row_pointers[row]);
534 }
535 png_free(png_ptr, info_ptr->row_pointers);
536 }
537 info_ptr->valid &= ~PNG_INFO_IDAT;
538 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600539#endif
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500540
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500541#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500542 if (num == -1)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500543 info_ptr->free_me &= ~mask;
Glenn Randers-Pehrsonec61c232000-05-16 06:17:36 -0500544 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500545 info_ptr->free_me &= ~(mask & ~PNG_FREE_MUL);
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500546#endif
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600547}
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600548
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600549/* This is an internal routine to free any memory that the info struct is
Andreas Dilger47a0c421997-05-16 02:46:07 -0500550 * pointing to before re-using it or freeing the struct itself. Recall
551 * that png_free() checks for NULL pointers for us.
552 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500553void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600554png_info_destroy(png_structp png_ptr, png_infop info_ptr)
555{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500556 png_debug(1, "in png_info_destroy");
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600557
558 png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
Glenn Randers-Pehrsond56aca72000-11-23 11:51:42 -0600559
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500560#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600561 if (png_ptr->num_chunk_list)
562 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500563 png_free(png_ptr, png_ptr->chunk_list);
564 png_ptr->num_chunk_list = 0;
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600565 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600566#endif
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600567
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500568 png_info_init_3(&info_ptr, png_sizeof(png_info));
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500569}
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600570#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
Guy Schalnat0d580581995-07-20 02:43:20 -0500571
Guy Schalnate5a37791996-06-05 15:50:50 -0500572/* This function returns a pointer to the io_ptr associated with the user
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600573 * functions. The application should free any memory associated with this
574 * pointer before png_write_destroy() or png_read_destroy() are called.
575 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500576png_voidp PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -0500577png_get_io_ptr(png_structp png_ptr)
578{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500579 if (png_ptr == NULL)
580 return (NULL);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600581 return (png_ptr->io_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500582}
Andreas Dilger47a0c421997-05-16 02:46:07 -0500583
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600584#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500585#if !defined(PNG_NO_STDIO)
586/* Initialize the default input/output functions for the PNG file. If you
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600587 * use your own read or write routines, you can call either png_set_read_fn()
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500588 * or png_set_write_fn() instead of png_init_io(). If you have defined
589 * PNG_NO_STDIO, you must use a function of your own because "FILE *" isn't
590 * necessarily available.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600591 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500592void PNGAPI
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500593png_init_io(png_structp png_ptr, png_FILE_p fp)
Guy Schalnate5a37791996-06-05 15:50:50 -0500594{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500595 png_debug(1, "in png_init_io");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500596 if (png_ptr == NULL)
597 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500598 png_ptr->io_ptr = (png_voidp)fp;
599}
Andreas Dilger47a0c421997-05-16 02:46:07 -0500600#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500601
602#if defined(PNG_TIME_RFC1123_SUPPORTED)
603/* Convert the supplied time into an RFC 1123 string suitable for use in
604 * a "Creation Time" or other text-based time string.
605 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500606png_charp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500607png_convert_to_rfc1123(png_structp png_ptr, png_timep ptime)
608{
609 static PNG_CONST char short_months[12][4] =
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600610 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
611 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500612
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500613 if (png_ptr == NULL)
614 return (NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500615 if (png_ptr->time_buffer == NULL)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500616 {
617 png_ptr->time_buffer = (png_charp)png_malloc(png_ptr, (png_uint_32)(29*
618 png_sizeof(char)));
619 }
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500620
621#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500622 {
623 char near_time_buf[29];
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500624 png_snprintf6(near_time_buf, 29, "%d %s %d %02d:%02d:%02d +0000",
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500625 ptime->day % 32, short_months[(ptime->month - 1) % 12],
626 ptime->year, ptime->hour % 24, ptime->minute % 60,
627 ptime->second % 61);
628 png_memcpy(png_ptr->time_buffer, near_time_buf,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500629 29*png_sizeof(char));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500630 }
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500631#else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500632 png_snprintf6(png_ptr->time_buffer, 29, "%d %s %d %02d:%02d:%02d +0000",
Glenn Randers-Pehrsone1eff582001-04-14 20:15:41 -0500633 ptime->day % 32, short_months[(ptime->month - 1) % 12],
634 ptime->year, ptime->hour % 24, ptime->minute % 60,
635 ptime->second % 61);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500636#endif
637 return ((png_charp)png_ptr->time_buffer);
638}
639#endif /* PNG_TIME_RFC1123_SUPPORTED */
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600640
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600641#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600642
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500643png_charp PNGAPI
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600644png_get_copyright(png_structp png_ptr)
645{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500646 png_ptr = png_ptr; /* silence compiler warning about unused png_ptr */
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500647#ifdef PNG_STRING_COPYRIGHT
648 return PNG_STRING_COPYRIGHT
649#else
650#ifdef __STDC__
Glenn Randers-Pehrson43aaf6e2008-08-05 22:17:03 -0500651 return ((png_charp) PNG_STRING_NEWLINE \
Glenn Randers-Pehrsondbab08f2009-05-18 16:58:37 -0500652 "libpng version x 1.4.0beta60 - May 18, 2009" PNG_STRING_NEWLINE \
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600653 "Copyright (c) 1998-2009 Glenn Randers-Pehrson" PNG_STRING_NEWLINE \
Glenn Randers-Pehrson43aaf6e2008-08-05 22:17:03 -0500654 "Copyright (c) 1996-1997 Andreas Dilger" PNG_STRING_NEWLINE \
655 "Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc." \
656 PNG_STRING_NEWLINE);
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500657#else
Glenn Randers-Pehrsondbab08f2009-05-18 16:58:37 -0500658 return ((png_charp) "libpng version 1.4.0beta60 - May 18, 2009\
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600659 Copyright (c) 1998-2009 Glenn Randers-Pehrson\
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500660 Copyright (c) 1996-1997 Andreas Dilger\
661 Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.");
662#endif
663#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600664}
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -0500665
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600666/* The following return the library version as a short string in the
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500667 * format 1.0.0 through 99.99.99zz. To get the version of *.h files
668 * used with your application, print out PNG_LIBPNG_VER_STRING, which
669 * is defined in png.h.
670 * Note: now there is no difference between png_get_libpng_ver() and
671 * png_get_header_ver(). Due to the version_nn_nn_nn typedef guard,
672 * it is guaranteed that png.c uses the correct version of png.h.
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600673 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500674png_charp PNGAPI
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600675png_get_libpng_ver(png_structp png_ptr)
676{
677 /* Version of *.c 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_ver(png_structp png_ptr)
684{
685 /* Version of *.h files used when building libpng */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500686 png_ptr = png_ptr; /* silence compiler warning about unused png_ptr */
687 return ((png_charp) PNG_LIBPNG_VER_STRING);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600688}
689
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500690png_charp PNGAPI
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600691png_get_header_version(png_structp png_ptr)
692{
693 /* Returns longer string containing both version and date */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500694 png_ptr = png_ptr; /* silence compiler warning about unused png_ptr */
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500695#ifdef __STDC__
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500696 return ((png_charp) PNG_HEADER_VERSION_STRING
697#ifndef PNG_READ_SUPPORTED
698 " (NO READ SUPPORT)"
699#endif
Glenn Randers-Pehrson43aaf6e2008-08-05 22:17:03 -0500700 PNG_STRING_NEWLINE);
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500701#else
702 return ((png_charp) PNG_HEADER_VERSION_STRING);
703#endif
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600704}
705
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600706#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600707#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsonc1bfe682002-03-06 22:08:00 -0600708int PNGAPI
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600709png_handle_as_unknown(png_structp png_ptr, png_bytep chunk_name)
710{
711 /* check chunk_name and return "keep" value if it's on the list, else 0 */
712 int i;
713 png_bytep p;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500714 if (png_ptr == NULL || chunk_name == NULL || png_ptr->num_chunk_list<=0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600715 return 0;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500716 p = png_ptr->chunk_list + png_ptr->num_chunk_list*5 - 5;
717 for (i = png_ptr->num_chunk_list; i; i--, p -= 5)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600718 if (!png_memcmp(chunk_name, p, 4))
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500719 return ((int)*(p + 4));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600720 return 0;
721}
722#endif
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -0500723
724/* This function, added to libpng-1.0.6g, is untested. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500725int PNGAPI
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -0500726png_reset_zstream(png_structp png_ptr)
727{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500728 if (png_ptr == NULL)
729 return Z_STREAM_ERROR;
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -0500730 return (inflateReset(&png_ptr->zstream));
731}
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600732#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500733
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600734/* This function was added to libpng-1.0.7 */
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500735png_uint_32 PNGAPI
736png_access_version_number(void)
737{
738 /* Version of *.c files used when building libpng */
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500739 return((png_uint_32) PNG_LIBPNG_VER);
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500740}
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -0600741
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500742
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500743
744#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
745#ifdef PNG_SIZE_T
746/* Added at libpng version 1.2.6 */
747 PNG_EXTERN png_size_t PNGAPI png_convert_size PNGARG((size_t size));
748png_size_t PNGAPI
749png_convert_size(size_t size)
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500750{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500751 if (size > (png_size_t)-1)
752 PNG_ABORT(); /* We haven't got access to png_ptr, so no png_error() */
753 return ((png_size_t)size);
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500754}
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500755#endif /* PNG_SIZE_T */
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600756
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600757/* Added at libpng version 1.2.34 and 1.4.0 (moved from pngset.c) */
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600758#if defined(PNG_cHRM_SUPPORTED)
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -0600759#if !defined(PNG_NO_CHECK_cHRM)
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600760
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600761/*
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500762 * Multiply two 32-bit numbers, V1 and V2, using 32-bit
763 * arithmetic, to produce a 64 bit result in the HI/LO words.
764 *
765 * A B
766 * x C D
767 * ------
768 * AD || BD
769 * AC || CB || 0
770 *
771 * where A and B are the high and low 16-bit words of V1,
772 * C and D are the 16-bit words of V2, AD is the product of
773 * A and D, and X || Y is (X << 16) + Y.
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600774*/
775
776void png_64bit_product (long v1, long v2, unsigned long *hi_product,
777 unsigned long *lo_product)
778{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500779 int a, b, c, d;
780 long lo, hi, x, y;
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600781
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500782 a = (v1 >> 16) & 0xffff;
783 b = v1 & 0xffff;
784 c = (v2 >> 16) & 0xffff;
785 d = v2 & 0xffff;
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600786
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500787 lo = b * d; /* BD */
788 x = a * d + c * b; /* AD + CB */
789 y = ((lo >> 16) & 0xffff) + x;
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600790
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500791 lo = (lo & 0xffff) | ((y & 0xffff) << 16);
792 hi = (y >> 16) & 0xffff;
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600793
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500794 hi += a * c; /* AC */
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600795
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500796 *hi_product = (unsigned long)hi;
797 *lo_product = (unsigned long)lo;
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600798}
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500799
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600800int /* private */
801png_check_cHRM_fixed(png_structp png_ptr,
802 png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
803 png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
804 png_fixed_point blue_x, png_fixed_point blue_y)
805{
806 int ret = 1;
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600807 unsigned long xy_hi,xy_lo,yx_hi,yx_lo;
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600808
809 png_debug(1, "in function png_check_cHRM_fixed");
810 if (png_ptr == NULL)
811 return 0;
812
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600813 if (white_x < 0 || white_y <= 0 ||
814 red_x < 0 || red_y < 0 ||
815 green_x < 0 || green_y < 0 ||
816 blue_x < 0 || blue_y < 0)
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600817 {
818 png_warning(png_ptr,
819 "Ignoring attempt to set negative chromaticity value");
820 ret = 0;
821 }
822 if (white_x > (png_fixed_point) PNG_UINT_31_MAX ||
823 white_y > (png_fixed_point) PNG_UINT_31_MAX ||
824 red_x > (png_fixed_point) PNG_UINT_31_MAX ||
825 red_y > (png_fixed_point) PNG_UINT_31_MAX ||
826 green_x > (png_fixed_point) PNG_UINT_31_MAX ||
827 green_y > (png_fixed_point) PNG_UINT_31_MAX ||
828 blue_x > (png_fixed_point) PNG_UINT_31_MAX ||
829 blue_y > (png_fixed_point) PNG_UINT_31_MAX )
830 {
831 png_warning(png_ptr,
832 "Ignoring attempt to set chromaticity value exceeding 21474.83");
833 ret = 0;
834 }
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -0600835 if (white_x > 100000L - white_y)
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600836 {
837 png_warning(png_ptr, "Invalid cHRM white point");
838 ret = 0;
839 }
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -0600840 if (red_x > 100000L - red_y)
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600841 {
842 png_warning(png_ptr, "Invalid cHRM red point");
843 ret = 0;
844 }
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -0600845 if (green_x > 100000L - green_y)
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600846 {
847 png_warning(png_ptr, "Invalid cHRM green point");
848 ret = 0;
849 }
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -0600850 if (blue_x > 100000L - blue_y)
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600851 {
852 png_warning(png_ptr, "Invalid cHRM blue point");
853 ret = 0;
854 }
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600855
856 png_64bit_product(green_x - red_x, blue_y - red_y, &xy_hi, &xy_lo);
857 png_64bit_product(green_y - red_y, blue_x - red_x, &yx_hi, &yx_lo);
858
859 if (xy_hi == yx_hi && xy_lo == yx_lo)
860 {
861 png_warning(png_ptr,
862 "Ignoring attempt to set cHRM RGB triangle with zero area");
863 ret = 0;
864 }
865
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600866 return ret;
867}
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -0600868#endif /* NO_PNG_CHECK_cHRM */
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600869#endif /* PNG_cHRM_SUPPORTED */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500870#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */